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