sched/rt: Use schedule_preempt_disabled()
[deliverable/linux.git] / arch / s390 / kernel / process.c
CommitLineData
1da177e4 1/*
cbdc2292 2 * This file handles the architecture dependent parts of process handling.
1da177e4 3 *
cbdc2292
HC
4 * Copyright IBM Corp. 1999,2009
5 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>,
6 * Hartmut Penner <hp@de.ibm.com>,
7 * Denis Joseph Barrow,
1da177e4
LT
8 */
9
1da177e4
LT
10#include <linux/compiler.h>
11#include <linux/cpu.h>
1da177e4
LT
12#include <linux/sched.h>
13#include <linux/kernel.h>
14#include <linux/mm.h>
638ad34a 15#include <linux/elfcore.h>
1da177e4 16#include <linux/smp.h>
5a0e3ad6 17#include <linux/slab.h>
1da177e4 18#include <linux/interrupt.h>
5a62b192 19#include <linux/tick.h>
9887a1fc 20#include <linux/personality.h>
26689452 21#include <linux/syscalls.h>
3e86a8c6 22#include <linux/compat.h>
860dba45 23#include <linux/kprobes.h>
9887a1fc 24#include <linux/random.h>
3af6fb68 25#include <linux/module.h>
1da177e4
LT
26#include <asm/system.h>
27#include <asm/io.h>
28#include <asm/processor.h>
29#include <asm/irq.h>
30#include <asm/timer.h>
f5daba1d 31#include <asm/nmi.h>
da7f51c1 32#include <asm/smp.h>
a806170e 33#include "entry.h"
1da177e4 34
94c12cc7 35asmlinkage void ret_from_fork(void) asm ("ret_from_fork");
1da177e4
LT
36
37/*
38 * Return saved PC of a blocked thread. used in kernel/sched.
39 * resume in entry.S does not create a new stack frame, it
40 * just stores the registers %r6-%r15 to the frame given by
41 * schedule. We want to return the address of the caller of
42 * schedule, so we have to walk the backchain one time to
43 * find the frame schedule() store its return address.
44 */
45unsigned long thread_saved_pc(struct task_struct *tsk)
46{
eb33c190 47 struct stack_frame *sf, *low, *high;
1da177e4 48
eb33c190
HC
49 if (!tsk || !task_stack_page(tsk))
50 return 0;
51 low = task_stack_page(tsk);
52 high = (struct stack_frame *) task_pt_regs(tsk);
53 sf = (struct stack_frame *) (tsk->thread.ksp & PSW_ADDR_INSN);
54 if (sf <= low || sf > high)
55 return 0;
56 sf = (struct stack_frame *) (sf->back_chain & PSW_ADDR_INSN);
57 if (sf <= low || sf > high)
58 return 0;
1da177e4
LT
59 return sf->gprs[8];
60}
61
1da177e4
LT
62/*
63 * The idle loop on a S390...
64 */
cdb04527 65static void default_idle(void)
1da177e4 66{
da7f51c1 67 if (cpu_is_offline(smp_processor_id()))
1da177e4 68 cpu_die();
6931be08
HC
69 local_irq_disable();
70 if (need_resched()) {
71 local_irq_enable();
72 return;
73 }
77fa2245
HC
74 local_mcck_disable();
75 if (test_thread_flag(TIF_MCCK_PENDING)) {
76 local_mcck_enable();
77 local_irq_enable();
77fa2245
HC
78 return;
79 }
1f194a4c 80 trace_hardirqs_on();
632448f6
HC
81 /* Don't trace preempt off for idle. */
82 stop_critical_timings();
9cfb9b3c
MS
83 /* Stop virtual timer and halt the cpu. */
84 vtime_stop_cpu();
85 /* Reenable preemption tracer. */
632448f6 86 start_critical_timings();
1da177e4
LT
87}
88
89void cpu_idle(void)
90{
5bfb5d69 91 for (;;) {
1268fbc7
FW
92 tick_nohz_idle_enter();
93 rcu_idle_enter();
f3612304 94 while (!need_resched() && !test_thread_flag(TIF_MCCK_PENDING))
5bfb5d69 95 default_idle();
1268fbc7
FW
96 rcu_idle_exit();
97 tick_nohz_idle_exit();
f3612304
HC
98 if (test_thread_flag(TIF_MCCK_PENDING))
99 s390_handle_mcck();
bd2f5536 100 schedule_preempt_disabled();
5bfb5d69 101 }
1da177e4
LT
102}
103
860dba45 104extern void __kprobes kernel_thread_starter(void);
1da177e4 105
94c12cc7 106asm(
860dba45
MS
107 ".section .kprobes.text, \"ax\"\n"
108 ".global kernel_thread_starter\n"
1da177e4
LT
109 "kernel_thread_starter:\n"
110 " la 2,0(10)\n"
111 " basr 14,9\n"
112 " la 2,0\n"
860dba45
MS
113 " br 11\n"
114 ".previous\n");
1da177e4
LT
115
116int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
117{
118 struct pt_regs regs;
119
120 memset(&regs, 0, sizeof(regs));
b50511e4
MS
121 regs.psw.mask = psw_kernel_bits |
122 PSW_MASK_DAT | PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK;
1da177e4
LT
123 regs.psw.addr = (unsigned long) kernel_thread_starter | PSW_ADDR_AMODE;
124 regs.gprs[9] = (unsigned long) fn;
125 regs.gprs[10] = (unsigned long) arg;
126 regs.gprs[11] = (unsigned long) do_exit;
127 regs.orig_gpr2 = -1;
128
129 /* Ok, create the new process.. */
130 return do_fork(flags | CLONE_VM | CLONE_UNTRACED,
131 0, &regs, 0, NULL, NULL);
132}
1485c5c8 133EXPORT_SYMBOL(kernel_thread);
1da177e4
LT
134
135/*
136 * Free current thread data structures etc..
137 */
138void exit_thread(void)
139{
140}
141
142void flush_thread(void)
143{
1da177e4
LT
144}
145
146void release_thread(struct task_struct *dead_task)
147{
148}
149
6f2c55b8 150int copy_thread(unsigned long clone_flags, unsigned long new_stackp,
cbdc2292
HC
151 unsigned long unused,
152 struct task_struct *p, struct pt_regs *regs)
1da177e4 153{
5168ce2c 154 struct thread_info *ti;
cbdc2292
HC
155 struct fake_frame
156 {
157 struct stack_frame sf;
158 struct pt_regs childregs;
159 } *frame;
160
161 frame = container_of(task_pt_regs(p), struct fake_frame, childregs);
162 p->thread.ksp = (unsigned long) frame;
1da177e4 163 /* Store access registers to kernel stack of new process. */
cbdc2292 164 frame->childregs = *regs;
1da177e4 165 frame->childregs.gprs[2] = 0; /* child returns 0 on fork. */
cbdc2292
HC
166 frame->childregs.gprs[15] = new_stackp;
167 frame->sf.back_chain = 0;
1da177e4 168
cbdc2292
HC
169 /* new return point is ret_from_fork */
170 frame->sf.gprs[8] = (unsigned long) ret_from_fork;
1da177e4 171
cbdc2292
HC
172 /* fake return stack for resume(), don't go back to schedule */
173 frame->sf.gprs[9] = (unsigned long) frame;
1da177e4
LT
174
175 /* Save access registers to new thread structure. */
176 save_access_regs(&p->thread.acrs[0]);
177
347a8dc3 178#ifndef CONFIG_64BIT
cbdc2292 179 /*
1da177e4
LT
180 * save fprs to current->thread.fp_regs to merge them with
181 * the emulated registers and then copy the result to the child.
182 */
183 save_fp_regs(&current->thread.fp_regs);
184 memcpy(&p->thread.fp_regs, &current->thread.fp_regs,
185 sizeof(s390_fp_regs));
1da177e4
LT
186 /* Set a new TLS ? */
187 if (clone_flags & CLONE_SETTLS)
188 p->thread.acrs[0] = regs->gprs[6];
347a8dc3 189#else /* CONFIG_64BIT */
1da177e4
LT
190 /* Save the fpu registers to new thread structure. */
191 save_fp_regs(&p->thread.fp_regs);
1da177e4
LT
192 /* Set a new TLS ? */
193 if (clone_flags & CLONE_SETTLS) {
7757591a 194 if (is_compat_task()) {
1da177e4
LT
195 p->thread.acrs[0] = (unsigned int) regs->gprs[6];
196 } else {
197 p->thread.acrs[0] = (unsigned int)(regs->gprs[6] >> 32);
198 p->thread.acrs[1] = (unsigned int) regs->gprs[6];
199 }
200 }
347a8dc3 201#endif /* CONFIG_64BIT */
1da177e4
LT
202 /* start new process with ar4 pointing to the correct address space */
203 p->thread.mm_segment = get_fs();
cbdc2292 204 /* Don't copy debug registers */
5e9a2692
MS
205 memset(&p->thread.per_user, 0, sizeof(p->thread.per_user));
206 memset(&p->thread.per_event, 0, sizeof(p->thread.per_event));
f8d5faf7 207 clear_tsk_thread_flag(p, TIF_SINGLE_STEP);
5e9a2692 208 clear_tsk_thread_flag(p, TIF_PER_TRAP);
5168ce2c
HC
209 /* Initialize per thread user and system timer values */
210 ti = task_thread_info(p);
211 ti->user_timer = 0;
212 ti->system_timer = 0;
cbdc2292 213 return 0;
1da177e4
LT
214}
215
26689452 216SYSCALL_DEFINE0(fork)
1da177e4 217{
03ff9a23
MS
218 struct pt_regs *regs = task_pt_regs(current);
219 return do_fork(SIGCHLD, regs->gprs[15], regs, 0, NULL, NULL);
1da177e4
LT
220}
221
2d70ca23
HC
222SYSCALL_DEFINE4(clone, unsigned long, newsp, unsigned long, clone_flags,
223 int __user *, parent_tidptr, int __user *, child_tidptr)
1da177e4 224{
03ff9a23 225 struct pt_regs *regs = task_pt_regs(current);
1da177e4 226
03ff9a23
MS
227 if (!newsp)
228 newsp = regs->gprs[15];
229 return do_fork(clone_flags, newsp, regs, 0,
1da177e4
LT
230 parent_tidptr, child_tidptr);
231}
232
233/*
234 * This is trivial, and on the face of it looks like it
235 * could equally well be done in user mode.
236 *
237 * Not so, for quite unobvious reasons - register pressure.
238 * In user mode vfork() cannot have a stack frame, and if
239 * done by calling the "clone()" system call directly, you
240 * do not have enough call-clobbered registers to hold all
241 * the information you need.
242 */
26689452 243SYSCALL_DEFINE0(vfork)
1da177e4 244{
03ff9a23 245 struct pt_regs *regs = task_pt_regs(current);
1da177e4 246 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD,
03ff9a23
MS
247 regs->gprs[15], regs, 0, NULL, NULL);
248}
249
250asmlinkage void execve_tail(void)
251{
03ff9a23
MS
252 current->thread.fp_regs.fpc = 0;
253 if (MACHINE_HAS_IEEE)
254 asm volatile("sfpc %0,%0" : : "d" (0));
1da177e4
LT
255}
256
257/*
258 * sys_execve() executes a new program.
259 */
d7627467
DH
260SYSCALL_DEFINE3(execve, const char __user *, name,
261 const char __user *const __user *, argv,
262 const char __user *const __user *, envp)
1da177e4 263{
03ff9a23
MS
264 struct pt_regs *regs = task_pt_regs(current);
265 char *filename;
3e86a8c6 266 long rc;
03ff9a23 267
3e86a8c6
HC
268 filename = getname(name);
269 rc = PTR_ERR(filename);
270 if (IS_ERR(filename))
271 return rc;
272 rc = do_execve(filename, argv, envp, regs);
273 if (rc)
03ff9a23 274 goto out;
03ff9a23 275 execve_tail();
3e86a8c6 276 rc = regs->gprs[2];
1da177e4 277out:
3e86a8c6
HC
278 putname(filename);
279 return rc;
1da177e4
LT
280}
281
1da177e4
LT
282/*
283 * fill in the FPU structure for a core dump.
284 */
285int dump_fpu (struct pt_regs * regs, s390_fp_regs *fpregs)
286{
347a8dc3 287#ifndef CONFIG_64BIT
cbdc2292 288 /*
1da177e4
LT
289 * save fprs to current->thread.fp_regs to merge them with
290 * the emulated registers and then copy the result to the dump.
291 */
292 save_fp_regs(&current->thread.fp_regs);
293 memcpy(fpregs, &current->thread.fp_regs, sizeof(s390_fp_regs));
347a8dc3 294#else /* CONFIG_64BIT */
1da177e4 295 save_fp_regs(fpregs);
347a8dc3 296#endif /* CONFIG_64BIT */
1da177e4
LT
297 return 1;
298}
1485c5c8 299EXPORT_SYMBOL(dump_fpu);
1da177e4 300
1da177e4
LT
301unsigned long get_wchan(struct task_struct *p)
302{
303 struct stack_frame *sf, *low, *high;
304 unsigned long return_address;
305 int count;
306
30af7120 307 if (!p || p == current || p->state == TASK_RUNNING || !task_stack_page(p))
1da177e4 308 return 0;
30af7120
AV
309 low = task_stack_page(p);
310 high = (struct stack_frame *) task_pt_regs(p);
1da177e4
LT
311 sf = (struct stack_frame *) (p->thread.ksp & PSW_ADDR_INSN);
312 if (sf <= low || sf > high)
313 return 0;
314 for (count = 0; count < 16; count++) {
315 sf = (struct stack_frame *) (sf->back_chain & PSW_ADDR_INSN);
316 if (sf <= low || sf > high)
317 return 0;
318 return_address = sf->gprs[8] & PSW_ADDR_INSN;
319 if (!in_sched_functions(return_address))
320 return return_address;
321 }
322 return 0;
323}
9887a1fc
HC
324
325unsigned long arch_align_stack(unsigned long sp)
326{
327 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
328 sp -= get_random_int() & ~PAGE_MASK;
329 return sp & ~0xf;
330}
33519182
HC
331
332static inline unsigned long brk_rnd(void)
333{
334 /* 8MB for 32bit, 1GB for 64bit */
335 if (is_32bit_task())
336 return (get_random_int() & 0x7ffUL) << PAGE_SHIFT;
337 else
338 return (get_random_int() & 0x3ffffUL) << PAGE_SHIFT;
339}
340
341unsigned long arch_randomize_brk(struct mm_struct *mm)
342{
343 unsigned long ret = PAGE_ALIGN(mm->brk + brk_rnd());
344
345 if (ret < mm->brk)
346 return mm->brk;
347 return ret;
348}
d2c9dfcc
HC
349
350unsigned long randomize_et_dyn(unsigned long base)
351{
352 unsigned long ret = PAGE_ALIGN(base + brk_rnd());
353
354 if (!(current->flags & PF_RANDOMIZE))
355 return base;
356 if (ret < base)
357 return base;
358 return ret;
359}
This page took 0.578384 seconds and 5 git commands to generate.