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/sched.h>
13 #include <linux/kernel.h>
15 #include <linux/elfcore.h>
16 #include <linux/smp.h>
17 #include <linux/slab.h>
18 #include <linux/interrupt.h>
19 #include <linux/tick.h>
20 #include <linux/personality.h>
21 #include <linux/syscalls.h>
22 #include <linux/compat.h>
23 #include <linux/kprobes.h>
24 #include <linux/random.h>
25 #include <linux/module.h>
26 #include <asm/system.h>
28 #include <asm/processor.h>
30 #include <asm/timer.h>
35 asmlinkage void ret_from_fork(void) asm ("ret_from_fork");
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.
45 unsigned long thread_saved_pc(struct task_struct *tsk)
47 struct stack_frame *sf, *low, *high;
49 if (!tsk || !task_stack_page(tsk))
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)
56 sf = (struct stack_frame *) (sf->back_chain & PSW_ADDR_INSN);
57 if (sf <= low || sf > high)
63 * The idle loop on a S390...
65 static void default_idle(void)
67 if (cpu_is_offline(smp_processor_id()))
75 if (test_thread_flag(TIF_MCCK_PENDING)) {
82 /* Don't trace preempt off for idle. */
83 stop_critical_timings();
84 /* Stop virtual timer and halt the cpu. */
86 /* Reenable preemption tracer. */
87 start_critical_timings();
93 tick_nohz_stop_sched_tick(1);
94 while (!need_resched())
96 tick_nohz_restart_sched_tick();
97 preempt_enable_no_resched();
103 extern void __kprobes kernel_thread_starter(void);
106 ".section .kprobes.text, \"ax\"\n"
107 ".global kernel_thread_starter\n"
108 "kernel_thread_starter:\n"
115 int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
119 memset(®s, 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;
128 /* Ok, create the new process.. */
129 return do_fork(flags | CLONE_VM | CLONE_UNTRACED,
130 0, ®s, 0, NULL, NULL);
132 EXPORT_SYMBOL(kernel_thread);
135 * Free current thread data structures etc..
137 void exit_thread(void)
141 void flush_thread(void)
145 void release_thread(struct task_struct *dead_task)
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)
153 struct thread_info *ti;
156 struct stack_frame sf;
157 struct pt_regs childregs;
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;
168 /* new return point is ret_from_fork */
169 frame->sf.gprs[8] = (unsigned long) ret_from_fork;
171 /* fake return stack for resume(), don't go back to schedule */
172 frame->sf.gprs[9] = (unsigned long) frame;
174 /* Save access registers to new thread structure. */
175 save_access_regs(&p->thread.acrs[0]);
179 * save fprs to current->thread.fp_regs to merge them with
180 * the emulated registers and then copy the result to the child.
182 save_fp_regs(¤t->thread.fp_regs);
183 memcpy(&p->thread.fp_regs, ¤t->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];
196 p->thread.acrs[0] = (unsigned int)(regs->gprs[6] >> 32);
197 p->thread.acrs[1] = (unsigned int) regs->gprs[6];
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);
211 ti->system_timer = 0;
215 SYSCALL_DEFINE0(fork)
217 struct pt_regs *regs = task_pt_regs(current);
218 return do_fork(SIGCHLD, regs->gprs[15], regs, 0, NULL, NULL);
221 SYSCALL_DEFINE4(clone, unsigned long, newsp, unsigned long, clone_flags,
222 int __user *, parent_tidptr, int __user *, child_tidptr)
224 struct pt_regs *regs = task_pt_regs(current);
227 newsp = regs->gprs[15];
228 return do_fork(clone_flags, newsp, regs, 0,
229 parent_tidptr, child_tidptr);
233 * This is trivial, and on the face of it looks like it
234 * could equally well be done in user mode.
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.
242 SYSCALL_DEFINE0(vfork)
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);
249 asmlinkage void execve_tail(void)
251 current->thread.fp_regs.fpc = 0;
252 if (MACHINE_HAS_IEEE)
253 asm volatile("sfpc %0,%0" : : "d" (0));
257 * sys_execve() executes a new program.
259 SYSCALL_DEFINE3(execve, const char __user *, name,
260 const char __user *const __user *, argv,
261 const char __user *const __user *, envp)
263 struct pt_regs *regs = task_pt_regs(current);
267 filename = getname(name);
268 rc = PTR_ERR(filename);
269 if (IS_ERR(filename))
271 rc = do_execve(filename, argv, envp, regs);
282 * fill in the FPU structure for a core dump.
284 int dump_fpu (struct pt_regs * regs, s390_fp_regs *fpregs)
288 * save fprs to current->thread.fp_regs to merge them with
289 * the emulated registers and then copy the result to the dump.
291 save_fp_regs(¤t->thread.fp_regs);
292 memcpy(fpregs, ¤t->thread.fp_regs, sizeof(s390_fp_regs));
293 #else /* CONFIG_64BIT */
294 save_fp_regs(fpregs);
295 #endif /* CONFIG_64BIT */
298 EXPORT_SYMBOL(dump_fpu);
300 unsigned long get_wchan(struct task_struct *p)
302 struct stack_frame *sf, *low, *high;
303 unsigned long return_address;
306 if (!p || p == current || p->state == TASK_RUNNING || !task_stack_page(p))
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)
313 for (count = 0; count < 16; count++) {
314 sf = (struct stack_frame *) (sf->back_chain & PSW_ADDR_INSN);
315 if (sf <= low || sf > high)
317 return_address = sf->gprs[8] & PSW_ADDR_INSN;
318 if (!in_sched_functions(return_address))
319 return return_address;
324 unsigned long arch_align_stack(unsigned long sp)
326 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
327 sp -= get_random_int() & ~PAGE_MASK;
331 static inline unsigned long brk_rnd(void)
333 /* 8MB for 32bit, 1GB for 64bit */
335 return (get_random_int() & 0x7ffUL) << PAGE_SHIFT;
337 return (get_random_int() & 0x3ffffUL) << PAGE_SHIFT;
340 unsigned long arch_randomize_brk(struct mm_struct *mm)
342 unsigned long ret = PAGE_ALIGN(mm->brk + brk_rnd());
349 unsigned long randomize_et_dyn(unsigned long base)
351 unsigned long ret = PAGE_ALIGN(base + brk_rnd());
353 if (!(current->flags & PF_RANDOMIZE))