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