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>,
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>
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/utsname.h>
31 #include <linux/tick.h>
32 #include <linux/elfcore.h>
33 #include <linux/kernel_stat.h>
34 #include <linux/syscalls.h>
35 #include <linux/compat.h>
36 #include <asm/compat.h>
37 #include <asm/uaccess.h>
38 #include <asm/pgtable.h>
39 #include <asm/system.h>
41 #include <asm/processor.h>
43 #include <asm/timer.h>
47 asmlinkage void ret_from_fork(void) asm ("ret_from_fork");
50 * Return saved PC of a blocked thread. used in kernel/sched.
51 * resume in entry.S does not create a new stack frame, it
52 * just stores the registers %r6-%r15 to the frame given by
53 * schedule. We want to return the address of the caller of
54 * schedule, so we have to walk the backchain one time to
55 * find the frame schedule() store its return address.
57 unsigned long thread_saved_pc(struct task_struct *tsk)
59 struct stack_frame *sf, *low, *high;
61 if (!tsk || !task_stack_page(tsk))
63 low = task_stack_page(tsk);
64 high = (struct stack_frame *) task_pt_regs(tsk);
65 sf = (struct stack_frame *) (tsk->thread.ksp & PSW_ADDR_INSN);
66 if (sf <= low || sf > high)
68 sf = (struct stack_frame *) (sf->back_chain & PSW_ADDR_INSN);
69 if (sf <= low || sf > high)
75 * The idle loop on a S390...
77 static void default_idle(void)
79 /* CPU is going idle. */
85 #ifdef CONFIG_HOTPLUG_CPU
86 if (cpu_is_offline(smp_processor_id())) {
87 preempt_enable_no_resched();
92 if (test_thread_flag(TIF_MCCK_PENDING)) {
99 /* Don't trace preempt off for idle. */
100 stop_critical_timings();
101 /* Stop virtual timer and halt the cpu. */
103 /* Reenable preemption tracer. */
104 start_critical_timings();
110 tick_nohz_stop_sched_tick(1);
111 while (!need_resched())
113 tick_nohz_restart_sched_tick();
114 preempt_enable_no_resched();
120 extern void kernel_thread_starter(void);
124 "kernel_thread_starter:\n"
130 int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
134 memset(®s, 0, sizeof(regs));
135 regs.psw.mask = psw_kernel_bits | PSW_MASK_IO | PSW_MASK_EXT;
136 regs.psw.addr = (unsigned long) kernel_thread_starter | PSW_ADDR_AMODE;
137 regs.gprs[9] = (unsigned long) fn;
138 regs.gprs[10] = (unsigned long) arg;
139 regs.gprs[11] = (unsigned long) do_exit;
142 /* Ok, create the new process.. */
143 return do_fork(flags | CLONE_VM | CLONE_UNTRACED,
144 0, ®s, 0, NULL, NULL);
146 EXPORT_SYMBOL(kernel_thread);
149 * Free current thread data structures etc..
151 void exit_thread(void)
155 void flush_thread(void)
158 clear_tsk_thread_flag(current, TIF_USEDFPU);
161 void release_thread(struct task_struct *dead_task)
165 int copy_thread(unsigned long clone_flags, unsigned long new_stackp,
166 unsigned long unused,
167 struct task_struct *p, struct pt_regs *regs)
169 struct thread_info *ti;
172 struct stack_frame sf;
173 struct pt_regs childregs;
176 frame = container_of(task_pt_regs(p), struct fake_frame, childregs);
177 p->thread.ksp = (unsigned long) frame;
178 /* Store access registers to kernel stack of new process. */
179 frame->childregs = *regs;
180 frame->childregs.gprs[2] = 0; /* child returns 0 on fork. */
181 frame->childregs.gprs[15] = new_stackp;
182 frame->sf.back_chain = 0;
184 /* new return point is ret_from_fork */
185 frame->sf.gprs[8] = (unsigned long) ret_from_fork;
187 /* fake return stack for resume(), don't go back to schedule */
188 frame->sf.gprs[9] = (unsigned long) frame;
190 /* Save access registers to new thread structure. */
191 save_access_regs(&p->thread.acrs[0]);
195 * save fprs to current->thread.fp_regs to merge them with
196 * the emulated registers and then copy the result to the child.
198 save_fp_regs(¤t->thread.fp_regs);
199 memcpy(&p->thread.fp_regs, ¤t->thread.fp_regs,
200 sizeof(s390_fp_regs));
201 /* Set a new TLS ? */
202 if (clone_flags & CLONE_SETTLS)
203 p->thread.acrs[0] = regs->gprs[6];
204 #else /* CONFIG_64BIT */
205 /* Save the fpu registers to new thread structure. */
206 save_fp_regs(&p->thread.fp_regs);
207 /* Set a new TLS ? */
208 if (clone_flags & CLONE_SETTLS) {
209 if (is_compat_task()) {
210 p->thread.acrs[0] = (unsigned int) regs->gprs[6];
212 p->thread.acrs[0] = (unsigned int)(regs->gprs[6] >> 32);
213 p->thread.acrs[1] = (unsigned int) regs->gprs[6];
216 #endif /* CONFIG_64BIT */
217 /* start new process with ar4 pointing to the correct address space */
218 p->thread.mm_segment = get_fs();
219 /* Don't copy debug registers */
220 memset(&p->thread.per_info, 0, sizeof(p->thread.per_info));
221 /* Initialize per thread user and system timer values */
222 ti = task_thread_info(p);
224 ti->system_timer = 0;
228 SYSCALL_DEFINE0(fork)
230 struct pt_regs *regs = task_pt_regs(current);
231 return do_fork(SIGCHLD, regs->gprs[15], regs, 0, NULL, NULL);
234 SYSCALL_DEFINE4(clone, unsigned long, newsp, unsigned long, clone_flags,
235 int __user *, parent_tidptr, int __user *, child_tidptr)
237 struct pt_regs *regs = task_pt_regs(current);
240 newsp = regs->gprs[15];
241 return do_fork(clone_flags, newsp, regs, 0,
242 parent_tidptr, child_tidptr);
246 * This is trivial, and on the face of it looks like it
247 * could equally well be done in user mode.
249 * Not so, for quite unobvious reasons - register pressure.
250 * In user mode vfork() cannot have a stack frame, and if
251 * done by calling the "clone()" system call directly, you
252 * do not have enough call-clobbered registers to hold all
253 * the information you need.
255 SYSCALL_DEFINE0(vfork)
257 struct pt_regs *regs = task_pt_regs(current);
258 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD,
259 regs->gprs[15], regs, 0, NULL, NULL);
262 asmlinkage void execve_tail(void)
264 current->thread.fp_regs.fpc = 0;
265 if (MACHINE_HAS_IEEE)
266 asm volatile("sfpc %0,%0" : : "d" (0));
270 * sys_execve() executes a new program.
272 SYSCALL_DEFINE3(execve, char __user *, name, char __user * __user *, argv,
273 char __user * __user *, envp)
275 struct pt_regs *regs = task_pt_regs(current);
279 filename = getname(name);
280 rc = PTR_ERR(filename);
281 if (IS_ERR(filename))
283 rc = do_execve(filename, argv, envp, regs);
294 * fill in the FPU structure for a core dump.
296 int dump_fpu (struct pt_regs * regs, s390_fp_regs *fpregs)
300 * save fprs to current->thread.fp_regs to merge them with
301 * the emulated registers and then copy the result to the dump.
303 save_fp_regs(¤t->thread.fp_regs);
304 memcpy(fpregs, ¤t->thread.fp_regs, sizeof(s390_fp_regs));
305 #else /* CONFIG_64BIT */
306 save_fp_regs(fpregs);
307 #endif /* CONFIG_64BIT */
310 EXPORT_SYMBOL(dump_fpu);
312 unsigned long get_wchan(struct task_struct *p)
314 struct stack_frame *sf, *low, *high;
315 unsigned long return_address;
318 if (!p || p == current || p->state == TASK_RUNNING || !task_stack_page(p))
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)
325 for (count = 0; count < 16; count++) {
326 sf = (struct stack_frame *) (sf->back_chain & PSW_ADDR_INSN);
327 if (sf <= low || sf > high)
329 return_address = sf->gprs[8] & PSW_ADDR_INSN;
330 if (!in_sched_functions(return_address))
331 return return_address;