init from v2.6.32.60
[mach-moxart.git] / arch / s390 / kernel / process.c
blobcea285546e6578e0e6e2e662993e6f4d2df526eb
1 /*
2 * This file handles the architecture dependent parts of process handling.
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 */
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/unistd.h>
20 #include <linux/ptrace.h>
21 #include <linux/slab.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 <asm/uaccess.h>
36 #include <asm/pgtable.h>
37 #include <asm/system.h>
38 #include <asm/io.h>
39 #include <asm/processor.h>
40 #include <asm/irq.h>
41 #include <asm/timer.h>
42 #include <asm/nmi.h>
43 #include "entry.h"
45 asmlinkage void ret_from_fork(void) asm ("ret_from_fork");
48 * Return saved PC of a blocked thread. used in kernel/sched.
49 * resume in entry.S does not create a new stack frame, it
50 * just stores the registers %r6-%r15 to the frame given by
51 * schedule. We want to return the address of the caller of
52 * schedule, so we have to walk the backchain one time to
53 * find the frame schedule() store its return address.
55 unsigned long thread_saved_pc(struct task_struct *tsk)
57 struct stack_frame *sf, *low, *high;
59 if (!tsk || !task_stack_page(tsk))
60 return 0;
61 low = task_stack_page(tsk);
62 high = (struct stack_frame *) task_pt_regs(tsk);
63 sf = (struct stack_frame *) (tsk->thread.ksp & PSW_ADDR_INSN);
64 if (sf <= low || sf > high)
65 return 0;
66 sf = (struct stack_frame *) (sf->back_chain & PSW_ADDR_INSN);
67 if (sf <= low || sf > high)
68 return 0;
69 return sf->gprs[8];
73 * The idle loop on a S390...
75 static void default_idle(void)
77 /* CPU is going idle. */
78 local_irq_disable();
79 if (need_resched()) {
80 local_irq_enable();
81 return;
83 #ifdef CONFIG_HOTPLUG_CPU
84 if (cpu_is_offline(smp_processor_id())) {
85 preempt_enable_no_resched();
86 cpu_die();
88 #endif
89 local_mcck_disable();
90 if (test_thread_flag(TIF_MCCK_PENDING)) {
91 local_mcck_enable();
92 local_irq_enable();
93 s390_handle_mcck();
94 return;
96 trace_hardirqs_on();
97 /* Don't trace preempt off for idle. */
98 stop_critical_timings();
99 /* Stop virtual timer and halt the cpu. */
100 vtime_stop_cpu();
101 /* Reenable preemption tracer. */
102 start_critical_timings();
105 void cpu_idle(void)
107 for (;;) {
108 tick_nohz_stop_sched_tick(1);
109 while (!need_resched())
110 default_idle();
111 tick_nohz_restart_sched_tick();
112 preempt_enable_no_resched();
113 schedule();
114 preempt_disable();
118 extern void kernel_thread_starter(void);
120 asm(
121 ".align 4\n"
122 "kernel_thread_starter:\n"
123 " la 2,0(10)\n"
124 " basr 14,9\n"
125 " la 2,0\n"
126 " br 11\n");
128 int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
130 struct pt_regs regs;
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;
140 /* Ok, create the new process.. */
141 return do_fork(flags | CLONE_VM | CLONE_UNTRACED,
142 0, &regs, 0, NULL, NULL);
144 EXPORT_SYMBOL(kernel_thread);
147 * Free current thread data structures etc..
149 void exit_thread(void)
153 void flush_thread(void)
155 clear_used_math();
156 clear_tsk_thread_flag(current, TIF_USEDFPU);
159 void release_thread(struct task_struct *dead_task)
163 int copy_thread(unsigned long clone_flags, unsigned long new_stackp,
164 unsigned long unused,
165 struct task_struct *p, struct pt_regs *regs)
167 struct thread_info *ti;
168 struct fake_frame
170 struct stack_frame sf;
171 struct pt_regs childregs;
172 } *frame;
174 frame = container_of(task_pt_regs(p), struct fake_frame, childregs);
175 p->thread.ksp = (unsigned long) frame;
176 /* Store access registers to kernel stack of new process. */
177 frame->childregs = *regs;
178 frame->childregs.gprs[2] = 0; /* child returns 0 on fork. */
179 frame->childregs.gprs[15] = new_stackp;
180 frame->sf.back_chain = 0;
182 /* new return point is ret_from_fork */
183 frame->sf.gprs[8] = (unsigned long) ret_from_fork;
185 /* fake return stack for resume(), don't go back to schedule */
186 frame->sf.gprs[9] = (unsigned long) frame;
188 /* Save access registers to new thread structure. */
189 save_access_regs(&p->thread.acrs[0]);
191 #ifndef CONFIG_64BIT
193 * save fprs to current->thread.fp_regs to merge them with
194 * the emulated registers and then copy the result to the child.
196 save_fp_regs(&current->thread.fp_regs);
197 memcpy(&p->thread.fp_regs, &current->thread.fp_regs,
198 sizeof(s390_fp_regs));
199 /* Set a new TLS ? */
200 if (clone_flags & CLONE_SETTLS)
201 p->thread.acrs[0] = regs->gprs[6];
202 #else /* CONFIG_64BIT */
203 /* Save the fpu registers to new thread structure. */
204 save_fp_regs(&p->thread.fp_regs);
205 /* Set a new TLS ? */
206 if (clone_flags & CLONE_SETTLS) {
207 if (is_compat_task()) {
208 p->thread.acrs[0] = (unsigned int) regs->gprs[6];
209 } else {
210 p->thread.acrs[0] = (unsigned int)(regs->gprs[6] >> 32);
211 p->thread.acrs[1] = (unsigned int) regs->gprs[6];
214 #endif /* CONFIG_64BIT */
215 /* start new process with ar4 pointing to the correct address space */
216 p->thread.mm_segment = get_fs();
217 /* Don't copy debug registers */
218 memset(&p->thread.per_info, 0, sizeof(p->thread.per_info));
219 /* Initialize per thread user and system timer values */
220 ti = task_thread_info(p);
221 ti->user_timer = 0;
222 ti->system_timer = 0;
223 return 0;
226 SYSCALL_DEFINE0(fork)
228 struct pt_regs *regs = task_pt_regs(current);
229 return do_fork(SIGCHLD, regs->gprs[15], regs, 0, NULL, NULL);
232 SYSCALL_DEFINE4(clone, unsigned long, newsp, unsigned long, clone_flags,
233 int __user *, parent_tidptr, int __user *, child_tidptr)
235 struct pt_regs *regs = task_pt_regs(current);
237 if (!newsp)
238 newsp = regs->gprs[15];
239 return do_fork(clone_flags, newsp, regs, 0,
240 parent_tidptr, child_tidptr);
244 * This is trivial, and on the face of it looks like it
245 * could equally well be done in user mode.
247 * Not so, for quite unobvious reasons - register pressure.
248 * In user mode vfork() cannot have a stack frame, and if
249 * done by calling the "clone()" system call directly, you
250 * do not have enough call-clobbered registers to hold all
251 * the information you need.
253 SYSCALL_DEFINE0(vfork)
255 struct pt_regs *regs = task_pt_regs(current);
256 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD,
257 regs->gprs[15], regs, 0, NULL, NULL);
260 asmlinkage void execve_tail(void)
262 current->thread.fp_regs.fpc = 0;
263 if (MACHINE_HAS_IEEE)
264 asm volatile("sfpc %0,%0" : : "d" (0));
268 * sys_execve() executes a new program.
270 SYSCALL_DEFINE3(execve, char __user *, name, char __user * __user *, argv,
271 char __user * __user *, envp)
273 struct pt_regs *regs = task_pt_regs(current);
274 char *filename;
275 long rc;
277 filename = getname(name);
278 rc = PTR_ERR(filename);
279 if (IS_ERR(filename))
280 return rc;
281 rc = do_execve(filename, argv, envp, regs);
282 if (rc)
283 goto out;
284 execve_tail();
285 rc = regs->gprs[2];
286 out:
287 putname(filename);
288 return rc;
292 * fill in the FPU structure for a core dump.
294 int dump_fpu (struct pt_regs * regs, s390_fp_regs *fpregs)
296 #ifndef CONFIG_64BIT
298 * save fprs to current->thread.fp_regs to merge them with
299 * the emulated registers and then copy the result to the dump.
301 save_fp_regs(&current->thread.fp_regs);
302 memcpy(fpregs, &current->thread.fp_regs, sizeof(s390_fp_regs));
303 #else /* CONFIG_64BIT */
304 save_fp_regs(fpregs);
305 #endif /* CONFIG_64BIT */
306 return 1;
308 EXPORT_SYMBOL(dump_fpu);
310 unsigned long get_wchan(struct task_struct *p)
312 struct stack_frame *sf, *low, *high;
313 unsigned long return_address;
314 int count;
316 if (!p || p == current || p->state == TASK_RUNNING || !task_stack_page(p))
317 return 0;
318 low = task_stack_page(p);
319 high = (struct stack_frame *) task_pt_regs(p);
320 sf = (struct stack_frame *) (p->thread.ksp & PSW_ADDR_INSN);
321 if (sf <= low || sf > high)
322 return 0;
323 for (count = 0; count < 16; count++) {
324 sf = (struct stack_frame *) (sf->back_chain & PSW_ADDR_INSN);
325 if (sf <= low || sf > high)
326 return 0;
327 return_address = sf->gprs[8] & PSW_ADDR_INSN;
328 if (!in_sched_functions(return_address))
329 return return_address;
331 return 0;