blob: 541a7509faebd47f7b9d5a72a1f779c3efa2e1f4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Heiko Carstenscbdc2292009-03-26 15:23:52 +01002 * This file handles the architecture dependent parts of process handling.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Heiko Carstenscbdc2292009-03-26 15:23:52 +01004 * 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,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/compiler.h>
11#include <linux/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/sched.h>
13#include <linux/kernel.h>
14#include <linux/mm.h>
15#include <linux/smp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/interrupt.h>
Heiko Carstens5a62b192008-04-17 07:46:25 +020018#include <linux/tick.h>
Heiko Carstens9887a1f2011-01-12 09:55:28 +010019#include <linux/personality.h>
Heiko Carstens26689452009-01-14 14:14:36 +010020#include <linux/syscalls.h>
Heiko Carstens3e86a8c2009-09-22 22:58:42 +020021#include <linux/compat.h>
Martin Schwidefsky860dba42011-01-05 12:47:25 +010022#include <linux/kprobes.h>
Heiko Carstens9887a1f2011-01-12 09:55:28 +010023#include <linux/random.h>
Jan Glauber3af6fb62011-05-23 10:24:44 +020024#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/system.h>
26#include <asm/io.h>
27#include <asm/processor.h>
28#include <asm/irq.h>
29#include <asm/timer.h>
Heiko Carstensf5daba12009-03-26 15:24:01 +010030#include <asm/nmi.h>
Jan Glauber3af6fb62011-05-23 10:24:44 +020031#include <asm/compat.h>
Heiko Carstensda7f51c2011-01-05 12:48:09 +010032#include <asm/smp.h>
Heiko Carstensa8061702008-04-17 07:46:26 +020033#include "entry.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020035asmlinkage void ret_from_fork(void) asm ("ret_from_fork");
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
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 */
45unsigned long thread_saved_pc(struct task_struct *tsk)
46{
Heiko Carstenseb33c192006-01-14 13:20:57 -080047 struct stack_frame *sf, *low, *high;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Heiko Carstenseb33c192006-01-14 13:20:57 -080049 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 return sf->gprs[8];
60}
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062/*
63 * The idle loop on a S390...
64 */
Adrian Bunkcdb04522006-03-24 03:15:57 -080065static void default_idle(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Heiko Carstensda7f51c2011-01-05 12:48:09 +010067 if (cpu_is_offline(smp_processor_id()))
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 cpu_die();
Heiko Carstens6931be02010-10-25 16:10:12 +020069 local_irq_disable();
70 if (need_resched()) {
71 local_irq_enable();
72 return;
73 }
Heiko Carstens77fa2242005-06-25 14:55:30 -070074 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 }
Heiko Carstens1f194a42006-07-03 00:24:46 -070081 trace_hardirqs_on();
Heiko Carstens632448f2008-11-14 18:18:04 +010082 /* Don't trace preempt off for idle. */
83 stop_critical_timings();
Martin Schwidefsky9cfb9b32008-12-31 15:11:41 +010084 /* Stop virtual timer and halt the cpu. */
85 vtime_stop_cpu();
86 /* Reenable preemption tracer. */
Heiko Carstens632448f2008-11-14 18:18:04 +010087 start_critical_timings();
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
90void cpu_idle(void)
91{
Nick Piggin5bfb5d62005-11-08 21:39:01 -080092 for (;;) {
Thomas Gleixnere3381252008-07-19 09:33:21 +020093 tick_nohz_stop_sched_tick(1);
Nick Piggin5bfb5d62005-11-08 21:39:01 -080094 while (!need_resched())
95 default_idle();
Heiko Carstens5a62b192008-04-17 07:46:25 +020096 tick_nohz_restart_sched_tick();
Nick Piggin5bfb5d62005-11-08 21:39:01 -080097 preempt_enable_no_resched();
98 schedule();
99 preempt_disable();
100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
Martin Schwidefsky860dba42011-01-05 12:47:25 +0100103extern void __kprobes kernel_thread_starter(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200105asm(
Martin Schwidefsky860dba42011-01-05 12:47:25 +0100106 ".section .kprobes.text, \"ax\"\n"
107 ".global kernel_thread_starter\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 "kernel_thread_starter:\n"
109 " la 2,0(10)\n"
110 " basr 14,9\n"
111 " la 2,0\n"
Martin Schwidefsky860dba42011-01-05 12:47:25 +0100112 " br 11\n"
113 ".previous\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
116{
117 struct pt_regs regs;
118
119 memset(&regs, 0, sizeof(regs));
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100120 regs.psw.mask = psw_kernel_bits | PSW_MASK_IO | PSW_MASK_EXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 regs.psw.addr = (unsigned long) kernel_thread_starter | PSW_ADDR_AMODE;
122 regs.gprs[9] = (unsigned long) fn;
123 regs.gprs[10] = (unsigned long) arg;
124 regs.gprs[11] = (unsigned long) do_exit;
125 regs.orig_gpr2 = -1;
126
127 /* Ok, create the new process.. */
128 return do_fork(flags | CLONE_VM | CLONE_UNTRACED,
129 0, &regs, 0, NULL, NULL);
130}
Heiko Carstens1485c5c2009-03-26 15:24:04 +0100131EXPORT_SYMBOL(kernel_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133/*
134 * Free current thread data structures etc..
135 */
136void exit_thread(void)
137{
138}
139
140void flush_thread(void)
141{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
144void release_thread(struct task_struct *dead_task)
145{
146}
147
Alexey Dobriyan6f2c55b2009-04-02 16:56:59 -0700148int copy_thread(unsigned long clone_flags, unsigned long new_stackp,
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100149 unsigned long unused,
150 struct task_struct *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
Heiko Carstens5168ce22009-03-26 15:23:53 +0100152 struct thread_info *ti;
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100153 struct fake_frame
154 {
155 struct stack_frame sf;
156 struct pt_regs childregs;
157 } *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100159 frame = container_of(task_pt_regs(p), struct fake_frame, childregs);
160 p->thread.ksp = (unsigned long) frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 /* Store access registers to kernel stack of new process. */
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100162 frame->childregs = *regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 frame->childregs.gprs[2] = 0; /* child returns 0 on fork. */
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100164 frame->childregs.gprs[15] = new_stackp;
165 frame->sf.back_chain = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100167 /* new return point is ret_from_fork */
168 frame->sf.gprs[8] = (unsigned long) ret_from_fork;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100170 /* fake return stack for resume(), don't go back to schedule */
171 frame->sf.gprs[9] = (unsigned long) frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 /* Save access registers to new thread structure. */
174 save_access_regs(&p->thread.acrs[0]);
175
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800176#ifndef CONFIG_64BIT
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100177 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 * save fprs to current->thread.fp_regs to merge them with
179 * the emulated registers and then copy the result to the child.
180 */
181 save_fp_regs(&current->thread.fp_regs);
182 memcpy(&p->thread.fp_regs, &current->thread.fp_regs,
183 sizeof(s390_fp_regs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 /* Set a new TLS ? */
185 if (clone_flags & CLONE_SETTLS)
186 p->thread.acrs[0] = regs->gprs[6];
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800187#else /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 /* Save the fpu registers to new thread structure. */
189 save_fp_regs(&p->thread.fp_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 /* Set a new TLS ? */
191 if (clone_flags & CLONE_SETTLS) {
Heiko Carstens77575912009-06-12 10:26:25 +0200192 if (is_compat_task()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 p->thread.acrs[0] = (unsigned int) regs->gprs[6];
194 } else {
195 p->thread.acrs[0] = (unsigned int)(regs->gprs[6] >> 32);
196 p->thread.acrs[1] = (unsigned int) regs->gprs[6];
197 }
198 }
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800199#endif /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 /* start new process with ar4 pointing to the correct address space */
201 p->thread.mm_segment = get_fs();
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100202 /* Don't copy debug registers */
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100203 memset(&p->thread.per_user, 0, sizeof(p->thread.per_user));
204 memset(&p->thread.per_event, 0, sizeof(p->thread.per_event));
Martin Schwidefskyf8d5faf2010-01-13 20:44:26 +0100205 clear_tsk_thread_flag(p, TIF_SINGLE_STEP);
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100206 clear_tsk_thread_flag(p, TIF_PER_TRAP);
Heiko Carstens5168ce22009-03-26 15:23:53 +0100207 /* Initialize per thread user and system timer values */
208 ti = task_thread_info(p);
209 ti->user_timer = 0;
210 ti->system_timer = 0;
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100211 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
Heiko Carstens26689452009-01-14 14:14:36 +0100214SYSCALL_DEFINE0(fork)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200216 struct pt_regs *regs = task_pt_regs(current);
217 return do_fork(SIGCHLD, regs->gprs[15], regs, 0, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
219
Heiko Carstens2d70ca22009-09-22 22:58:41 +0200220SYSCALL_DEFINE4(clone, unsigned long, newsp, unsigned long, clone_flags,
221 int __user *, parent_tidptr, int __user *, child_tidptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200223 struct pt_regs *regs = task_pt_regs(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200225 if (!newsp)
226 newsp = regs->gprs[15];
227 return do_fork(clone_flags, newsp, regs, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 parent_tidptr, child_tidptr);
229}
230
231/*
232 * This is trivial, and on the face of it looks like it
233 * could equally well be done in user mode.
234 *
235 * Not so, for quite unobvious reasons - register pressure.
236 * In user mode vfork() cannot have a stack frame, and if
237 * done by calling the "clone()" system call directly, you
238 * do not have enough call-clobbered registers to hold all
239 * the information you need.
240 */
Heiko Carstens26689452009-01-14 14:14:36 +0100241SYSCALL_DEFINE0(vfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200243 struct pt_regs *regs = task_pt_regs(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD,
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200245 regs->gprs[15], regs, 0, NULL, NULL);
246}
247
248asmlinkage void execve_tail(void)
249{
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200250 current->thread.fp_regs.fpc = 0;
251 if (MACHINE_HAS_IEEE)
252 asm volatile("sfpc %0,%0" : : "d" (0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
255/*
256 * sys_execve() executes a new program.
257 */
David Howellsd7627462010-08-17 23:52:56 +0100258SYSCALL_DEFINE3(execve, const char __user *, name,
259 const char __user *const __user *, argv,
260 const char __user *const __user *, envp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200262 struct pt_regs *regs = task_pt_regs(current);
263 char *filename;
Heiko Carstens3e86a8c2009-09-22 22:58:42 +0200264 long rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Heiko Carstens3e86a8c2009-09-22 22:58:42 +0200266 filename = getname(name);
267 rc = PTR_ERR(filename);
268 if (IS_ERR(filename))
269 return rc;
270 rc = do_execve(filename, argv, envp, regs);
271 if (rc)
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200272 goto out;
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200273 execve_tail();
Heiko Carstens3e86a8c2009-09-22 22:58:42 +0200274 rc = regs->gprs[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275out:
Heiko Carstens3e86a8c2009-09-22 22:58:42 +0200276 putname(filename);
277 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280/*
281 * fill in the FPU structure for a core dump.
282 */
283int dump_fpu (struct pt_regs * regs, s390_fp_regs *fpregs)
284{
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800285#ifndef CONFIG_64BIT
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100286 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 * save fprs to current->thread.fp_regs to merge them with
288 * the emulated registers and then copy the result to the dump.
289 */
290 save_fp_regs(&current->thread.fp_regs);
291 memcpy(fpregs, &current->thread.fp_regs, sizeof(s390_fp_regs));
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800292#else /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 save_fp_regs(fpregs);
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800294#endif /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 return 1;
296}
Heiko Carstens1485c5c2009-03-26 15:24:04 +0100297EXPORT_SYMBOL(dump_fpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299unsigned long get_wchan(struct task_struct *p)
300{
301 struct stack_frame *sf, *low, *high;
302 unsigned long return_address;
303 int count;
304
Al Viro30af7122006-01-12 01:05:50 -0800305 if (!p || p == current || p->state == TASK_RUNNING || !task_stack_page(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 return 0;
Al Viro30af7122006-01-12 01:05:50 -0800307 low = task_stack_page(p);
308 high = (struct stack_frame *) task_pt_regs(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 sf = (struct stack_frame *) (p->thread.ksp & PSW_ADDR_INSN);
310 if (sf <= low || sf > high)
311 return 0;
312 for (count = 0; count < 16; count++) {
313 sf = (struct stack_frame *) (sf->back_chain & PSW_ADDR_INSN);
314 if (sf <= low || sf > high)
315 return 0;
316 return_address = sf->gprs[8] & PSW_ADDR_INSN;
317 if (!in_sched_functions(return_address))
318 return return_address;
319 }
320 return 0;
321}
Heiko Carstens9887a1f2011-01-12 09:55:28 +0100322
323unsigned long arch_align_stack(unsigned long sp)
324{
325 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
326 sp -= get_random_int() & ~PAGE_MASK;
327 return sp & ~0xf;
328}
Heiko Carstens33519182011-01-12 09:55:30 +0100329
330static inline unsigned long brk_rnd(void)
331{
332 /* 8MB for 32bit, 1GB for 64bit */
333 if (is_32bit_task())
334 return (get_random_int() & 0x7ffUL) << PAGE_SHIFT;
335 else
336 return (get_random_int() & 0x3ffffUL) << PAGE_SHIFT;
337}
338
339unsigned long arch_randomize_brk(struct mm_struct *mm)
340{
341 unsigned long ret = PAGE_ALIGN(mm->brk + brk_rnd());
342
343 if (ret < mm->brk)
344 return mm->brk;
345 return ret;
346}
Heiko Carstensd2c9dfc2011-01-12 09:55:31 +0100347
348unsigned long randomize_et_dyn(unsigned long base)
349{
350 unsigned long ret = PAGE_ALIGN(base + brk_rnd());
351
352 if (!(current->flags & PF_RANDOMIZE))
353 return base;
354 if (ret < base)
355 return base;
356 return ret;
357}