blob: 26d48fe72999de2ab257e59e29ec95736eb89523 [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>
12#include <linux/errno.h>
13#include <linux/sched.h>
14#include <linux/kernel.h>
15#include <linux/mm.h>
Alexey Dobriyan4e950f62007-07-30 02:36:13 +040016#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/stddef.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/unistd.h>
21#include <linux/ptrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/vmalloc.h>
23#include <linux/user.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#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>
Heiko Carstens5a62b192008-04-17 07:46:25 +020030#include <linux/tick.h>
Heiko Carstensa8061702008-04-17 07:46:26 +020031#include <linux/elfcore.h>
Martin Schwidefsky6f430922008-12-31 15:11:40 +010032#include <linux/kernel_stat.h>
Heiko Carstens9887a1f2011-01-12 09:55:28 +010033#include <linux/personality.h>
Heiko Carstens26689452009-01-14 14:14:36 +010034#include <linux/syscalls.h>
Heiko Carstens3e86a8c2009-09-22 22:58:42 +020035#include <linux/compat.h>
Martin Schwidefsky860dba42011-01-05 12:47:25 +010036#include <linux/kprobes.h>
Heiko Carstens9887a1f2011-01-12 09:55:28 +010037#include <linux/random.h>
Heiko Carstens77575912009-06-12 10:26:25 +020038#include <asm/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <asm/uaccess.h>
40#include <asm/pgtable.h>
41#include <asm/system.h>
42#include <asm/io.h>
43#include <asm/processor.h>
44#include <asm/irq.h>
45#include <asm/timer.h>
Heiko Carstensf5daba12009-03-26 15:24:01 +010046#include <asm/nmi.h>
Heiko Carstensda7f51c2011-01-05 12:48:09 +010047#include <asm/smp.h>
Heiko Carstensa8061702008-04-17 07:46:26 +020048#include "entry.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020050asmlinkage void ret_from_fork(void) asm ("ret_from_fork");
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52/*
53 * Return saved PC of a blocked thread. used in kernel/sched.
54 * resume in entry.S does not create a new stack frame, it
55 * just stores the registers %r6-%r15 to the frame given by
56 * schedule. We want to return the address of the caller of
57 * schedule, so we have to walk the backchain one time to
58 * find the frame schedule() store its return address.
59 */
60unsigned long thread_saved_pc(struct task_struct *tsk)
61{
Heiko Carstenseb33c192006-01-14 13:20:57 -080062 struct stack_frame *sf, *low, *high;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Heiko Carstenseb33c192006-01-14 13:20:57 -080064 if (!tsk || !task_stack_page(tsk))
65 return 0;
66 low = task_stack_page(tsk);
67 high = (struct stack_frame *) task_pt_regs(tsk);
68 sf = (struct stack_frame *) (tsk->thread.ksp & PSW_ADDR_INSN);
69 if (sf <= low || sf > high)
70 return 0;
71 sf = (struct stack_frame *) (sf->back_chain & PSW_ADDR_INSN);
72 if (sf <= low || sf > high)
73 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 return sf->gprs[8];
75}
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/*
78 * The idle loop on a S390...
79 */
Adrian Bunkcdb04522006-03-24 03:15:57 -080080static void default_idle(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
Heiko Carstensda7f51c2011-01-05 12:48:09 +010082 if (cpu_is_offline(smp_processor_id()))
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 cpu_die();
Heiko Carstens6931be02010-10-25 16:10:12 +020084 local_irq_disable();
85 if (need_resched()) {
86 local_irq_enable();
87 return;
88 }
Heiko Carstens77fa2242005-06-25 14:55:30 -070089 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;
95 }
Heiko Carstens1f194a42006-07-03 00:24:46 -070096 trace_hardirqs_on();
Heiko Carstens632448f2008-11-14 18:18:04 +010097 /* Don't trace preempt off for idle. */
98 stop_critical_timings();
Martin Schwidefsky9cfb9b32008-12-31 15:11:41 +010099 /* Stop virtual timer and halt the cpu. */
100 vtime_stop_cpu();
101 /* Reenable preemption tracer. */
Heiko Carstens632448f2008-11-14 18:18:04 +0100102 start_critical_timings();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
105void cpu_idle(void)
106{
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800107 for (;;) {
Thomas Gleixnere3381252008-07-19 09:33:21 +0200108 tick_nohz_stop_sched_tick(1);
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800109 while (!need_resched())
110 default_idle();
Heiko Carstens5a62b192008-04-17 07:46:25 +0200111 tick_nohz_restart_sched_tick();
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800112 preempt_enable_no_resched();
113 schedule();
114 preempt_disable();
115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
Martin Schwidefsky860dba42011-01-05 12:47:25 +0100118extern void __kprobes kernel_thread_starter(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200120asm(
Martin Schwidefsky860dba42011-01-05 12:47:25 +0100121 ".section .kprobes.text, \"ax\"\n"
122 ".global kernel_thread_starter\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 "kernel_thread_starter:\n"
124 " la 2,0(10)\n"
125 " basr 14,9\n"
126 " la 2,0\n"
Martin Schwidefsky860dba42011-01-05 12:47:25 +0100127 " br 11\n"
128 ".previous\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
131{
132 struct pt_regs regs;
133
134 memset(&regs, 0, sizeof(regs));
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100135 regs.psw.mask = psw_kernel_bits | PSW_MASK_IO | PSW_MASK_EXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 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;
140 regs.orig_gpr2 = -1;
141
142 /* Ok, create the new process.. */
143 return do_fork(flags | CLONE_VM | CLONE_UNTRACED,
144 0, &regs, 0, NULL, NULL);
145}
Heiko Carstens1485c5c2009-03-26 15:24:04 +0100146EXPORT_SYMBOL(kernel_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148/*
149 * Free current thread data structures etc..
150 */
151void exit_thread(void)
152{
153}
154
155void flush_thread(void)
156{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
159void release_thread(struct task_struct *dead_task)
160{
161}
162
Alexey Dobriyan6f2c55b2009-04-02 16:56:59 -0700163int copy_thread(unsigned long clone_flags, unsigned long new_stackp,
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100164 unsigned long unused,
165 struct task_struct *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Heiko Carstens5168ce2c2009-03-26 15:23:53 +0100167 struct thread_info *ti;
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100168 struct fake_frame
169 {
170 struct stack_frame sf;
171 struct pt_regs childregs;
172 } *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100174 frame = container_of(task_pt_regs(p), struct fake_frame, childregs);
175 p->thread.ksp = (unsigned long) frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 /* Store access registers to kernel stack of new process. */
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100177 frame->childregs = *regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 frame->childregs.gprs[2] = 0; /* child returns 0 on fork. */
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100179 frame->childregs.gprs[15] = new_stackp;
180 frame->sf.back_chain = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100182 /* new return point is ret_from_fork */
183 frame->sf.gprs[8] = (unsigned long) ret_from_fork;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100185 /* fake return stack for resume(), don't go back to schedule */
186 frame->sf.gprs[9] = (unsigned long) frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 /* Save access registers to new thread structure. */
189 save_access_regs(&p->thread.acrs[0]);
190
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800191#ifndef CONFIG_64BIT
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100192 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 * save fprs to current->thread.fp_regs to merge them with
194 * the emulated registers and then copy the result to the child.
195 */
196 save_fp_regs(&current->thread.fp_regs);
197 memcpy(&p->thread.fp_regs, &current->thread.fp_regs,
198 sizeof(s390_fp_regs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 /* Set a new TLS ? */
200 if (clone_flags & CLONE_SETTLS)
201 p->thread.acrs[0] = regs->gprs[6];
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800202#else /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 /* Save the fpu registers to new thread structure. */
204 save_fp_regs(&p->thread.fp_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 /* Set a new TLS ? */
206 if (clone_flags & CLONE_SETTLS) {
Heiko Carstens77575912009-06-12 10:26:25 +0200207 if (is_compat_task()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 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];
212 }
213 }
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800214#endif /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 /* start new process with ar4 pointing to the correct address space */
216 p->thread.mm_segment = get_fs();
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100217 /* Don't copy debug registers */
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100218 memset(&p->thread.per_user, 0, sizeof(p->thread.per_user));
219 memset(&p->thread.per_event, 0, sizeof(p->thread.per_event));
Martin Schwidefskyf8d5faf2010-01-13 20:44:26 +0100220 clear_tsk_thread_flag(p, TIF_SINGLE_STEP);
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100221 clear_tsk_thread_flag(p, TIF_PER_TRAP);
Heiko Carstens5168ce2c2009-03-26 15:23:53 +0100222 /* Initialize per thread user and system timer values */
223 ti = task_thread_info(p);
224 ti->user_timer = 0;
225 ti->system_timer = 0;
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100226 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
228
Heiko Carstens26689452009-01-14 14:14:36 +0100229SYSCALL_DEFINE0(fork)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200231 struct pt_regs *regs = task_pt_regs(current);
232 return do_fork(SIGCHLD, regs->gprs[15], regs, 0, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
Heiko Carstens2d70ca22009-09-22 22:58:41 +0200235SYSCALL_DEFINE4(clone, unsigned long, newsp, unsigned long, clone_flags,
236 int __user *, parent_tidptr, int __user *, child_tidptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200238 struct pt_regs *regs = task_pt_regs(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200240 if (!newsp)
241 newsp = regs->gprs[15];
242 return do_fork(clone_flags, newsp, regs, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 parent_tidptr, child_tidptr);
244}
245
246/*
247 * This is trivial, and on the face of it looks like it
248 * could equally well be done in user mode.
249 *
250 * Not so, for quite unobvious reasons - register pressure.
251 * In user mode vfork() cannot have a stack frame, and if
252 * done by calling the "clone()" system call directly, you
253 * do not have enough call-clobbered registers to hold all
254 * the information you need.
255 */
Heiko Carstens26689452009-01-14 14:14:36 +0100256SYSCALL_DEFINE0(vfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200258 struct pt_regs *regs = task_pt_regs(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD,
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200260 regs->gprs[15], regs, 0, NULL, NULL);
261}
262
263asmlinkage void execve_tail(void)
264{
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200265 current->thread.fp_regs.fpc = 0;
266 if (MACHINE_HAS_IEEE)
267 asm volatile("sfpc %0,%0" : : "d" (0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
270/*
271 * sys_execve() executes a new program.
272 */
David Howellsd7627462010-08-17 23:52:56 +0100273SYSCALL_DEFINE3(execve, const char __user *, name,
274 const char __user *const __user *, argv,
275 const char __user *const __user *, envp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200277 struct pt_regs *regs = task_pt_regs(current);
278 char *filename;
Heiko Carstens3e86a8c2009-09-22 22:58:42 +0200279 long rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Heiko Carstens3e86a8c2009-09-22 22:58:42 +0200281 filename = getname(name);
282 rc = PTR_ERR(filename);
283 if (IS_ERR(filename))
284 return rc;
285 rc = do_execve(filename, argv, envp, regs);
286 if (rc)
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200287 goto out;
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200288 execve_tail();
Heiko Carstens3e86a8c2009-09-22 22:58:42 +0200289 rc = regs->gprs[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290out:
Heiko Carstens3e86a8c2009-09-22 22:58:42 +0200291 putname(filename);
292 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295/*
296 * fill in the FPU structure for a core dump.
297 */
298int dump_fpu (struct pt_regs * regs, s390_fp_regs *fpregs)
299{
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800300#ifndef CONFIG_64BIT
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100301 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 * save fprs to current->thread.fp_regs to merge them with
303 * the emulated registers and then copy the result to the dump.
304 */
305 save_fp_regs(&current->thread.fp_regs);
306 memcpy(fpregs, &current->thread.fp_regs, sizeof(s390_fp_regs));
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800307#else /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 save_fp_regs(fpregs);
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800309#endif /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return 1;
311}
Heiko Carstens1485c5c2009-03-26 15:24:04 +0100312EXPORT_SYMBOL(dump_fpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314unsigned long get_wchan(struct task_struct *p)
315{
316 struct stack_frame *sf, *low, *high;
317 unsigned long return_address;
318 int count;
319
Al Viro30af7122006-01-12 01:05:50 -0800320 if (!p || p == current || p->state == TASK_RUNNING || !task_stack_page(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 return 0;
Al Viro30af7122006-01-12 01:05:50 -0800322 low = task_stack_page(p);
323 high = (struct stack_frame *) task_pt_regs(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 sf = (struct stack_frame *) (p->thread.ksp & PSW_ADDR_INSN);
325 if (sf <= low || sf > high)
326 return 0;
327 for (count = 0; count < 16; count++) {
328 sf = (struct stack_frame *) (sf->back_chain & PSW_ADDR_INSN);
329 if (sf <= low || sf > high)
330 return 0;
331 return_address = sf->gprs[8] & PSW_ADDR_INSN;
332 if (!in_sched_functions(return_address))
333 return return_address;
334 }
335 return 0;
336}
Heiko Carstens9887a1f2011-01-12 09:55:28 +0100337
338unsigned long arch_align_stack(unsigned long sp)
339{
340 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
341 sp -= get_random_int() & ~PAGE_MASK;
342 return sp & ~0xf;
343}
Heiko Carstens33519182011-01-12 09:55:30 +0100344
345static inline unsigned long brk_rnd(void)
346{
347 /* 8MB for 32bit, 1GB for 64bit */
348 if (is_32bit_task())
349 return (get_random_int() & 0x7ffUL) << PAGE_SHIFT;
350 else
351 return (get_random_int() & 0x3ffffUL) << PAGE_SHIFT;
352}
353
354unsigned long arch_randomize_brk(struct mm_struct *mm)
355{
356 unsigned long ret = PAGE_ALIGN(mm->brk + brk_rnd());
357
358 if (ret < mm->brk)
359 return mm->brk;
360 return ret;
361}