blob: c2fffb57d727c3d919943654d82a9976312d7bcf [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 Carstens26689452009-01-14 14:14:36 +010033#include <linux/syscalls.h>
Heiko Carstens3e86a8c2009-09-22 22:58:42 +020034#include <linux/compat.h>
Martin Schwidefsky860dba42011-01-05 12:47:25 +010035#include <linux/kprobes.h>
Heiko Carstens77575912009-06-12 10:26:25 +020036#include <asm/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <asm/uaccess.h>
38#include <asm/pgtable.h>
39#include <asm/system.h>
40#include <asm/io.h>
41#include <asm/processor.h>
42#include <asm/irq.h>
43#include <asm/timer.h>
Heiko Carstensf5daba12009-03-26 15:24:01 +010044#include <asm/nmi.h>
Heiko Carstensda7f51c2011-01-05 12:48:09 +010045#include <asm/smp.h>
Heiko Carstensa8061702008-04-17 07:46:26 +020046#include "entry.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020048asmlinkage void ret_from_fork(void) asm ("ret_from_fork");
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50/*
51 * Return saved PC of a blocked thread. used in kernel/sched.
52 * resume in entry.S does not create a new stack frame, it
53 * just stores the registers %r6-%r15 to the frame given by
54 * schedule. We want to return the address of the caller of
55 * schedule, so we have to walk the backchain one time to
56 * find the frame schedule() store its return address.
57 */
58unsigned long thread_saved_pc(struct task_struct *tsk)
59{
Heiko Carstenseb33c192006-01-14 13:20:57 -080060 struct stack_frame *sf, *low, *high;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Heiko Carstenseb33c192006-01-14 13:20:57 -080062 if (!tsk || !task_stack_page(tsk))
63 return 0;
64 low = task_stack_page(tsk);
65 high = (struct stack_frame *) task_pt_regs(tsk);
66 sf = (struct stack_frame *) (tsk->thread.ksp & PSW_ADDR_INSN);
67 if (sf <= low || sf > high)
68 return 0;
69 sf = (struct stack_frame *) (sf->back_chain & PSW_ADDR_INSN);
70 if (sf <= low || sf > high)
71 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 return sf->gprs[8];
73}
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075/*
76 * The idle loop on a S390...
77 */
Adrian Bunkcdb04522006-03-24 03:15:57 -080078static void default_idle(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Heiko Carstensda7f51c2011-01-05 12:48:09 +010080 if (cpu_is_offline(smp_processor_id()))
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 cpu_die();
Heiko Carstens6931be02010-10-25 16:10:12 +020082 local_irq_disable();
83 if (need_resched()) {
84 local_irq_enable();
85 return;
86 }
Heiko Carstens77fa2242005-06-25 14:55:30 -070087 local_mcck_disable();
88 if (test_thread_flag(TIF_MCCK_PENDING)) {
89 local_mcck_enable();
90 local_irq_enable();
91 s390_handle_mcck();
92 return;
93 }
Heiko Carstens1f194a42006-07-03 00:24:46 -070094 trace_hardirqs_on();
Heiko Carstens632448f2008-11-14 18:18:04 +010095 /* Don't trace preempt off for idle. */
96 stop_critical_timings();
Martin Schwidefsky9cfb9b32008-12-31 15:11:41 +010097 /* Stop virtual timer and halt the cpu. */
98 vtime_stop_cpu();
99 /* Reenable preemption tracer. */
Heiko Carstens632448f2008-11-14 18:18:04 +0100100 start_critical_timings();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
103void cpu_idle(void)
104{
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800105 for (;;) {
Thomas Gleixnere3381252008-07-19 09:33:21 +0200106 tick_nohz_stop_sched_tick(1);
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800107 while (!need_resched())
108 default_idle();
Heiko Carstens5a62b192008-04-17 07:46:25 +0200109 tick_nohz_restart_sched_tick();
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800110 preempt_enable_no_resched();
111 schedule();
112 preempt_disable();
113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
Martin Schwidefsky860dba42011-01-05 12:47:25 +0100116extern void __kprobes kernel_thread_starter(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200118asm(
Martin Schwidefsky860dba42011-01-05 12:47:25 +0100119 ".section .kprobes.text, \"ax\"\n"
120 ".global kernel_thread_starter\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 "kernel_thread_starter:\n"
122 " la 2,0(10)\n"
123 " basr 14,9\n"
124 " la 2,0\n"
Martin Schwidefsky860dba42011-01-05 12:47:25 +0100125 " br 11\n"
126 ".previous\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
129{
130 struct pt_regs regs;
131
132 memset(&regs, 0, sizeof(regs));
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100133 regs.psw.mask = psw_kernel_bits | PSW_MASK_IO | PSW_MASK_EXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 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;
139
140 /* Ok, create the new process.. */
141 return do_fork(flags | CLONE_VM | CLONE_UNTRACED,
142 0, &regs, 0, NULL, NULL);
143}
Heiko Carstens1485c5c2009-03-26 15:24:04 +0100144EXPORT_SYMBOL(kernel_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146/*
147 * Free current thread data structures etc..
148 */
149void exit_thread(void)
150{
151}
152
153void flush_thread(void)
154{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
157void release_thread(struct task_struct *dead_task)
158{
159}
160
Alexey Dobriyan6f2c55b2009-04-02 16:56:59 -0700161int copy_thread(unsigned long clone_flags, unsigned long new_stackp,
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100162 unsigned long unused,
163 struct task_struct *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Heiko Carstens5168ce2c2009-03-26 15:23:53 +0100165 struct thread_info *ti;
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100166 struct fake_frame
167 {
168 struct stack_frame sf;
169 struct pt_regs childregs;
170 } *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100172 frame = container_of(task_pt_regs(p), struct fake_frame, childregs);
173 p->thread.ksp = (unsigned long) frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 /* Store access registers to kernel stack of new process. */
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100175 frame->childregs = *regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 frame->childregs.gprs[2] = 0; /* child returns 0 on fork. */
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100177 frame->childregs.gprs[15] = new_stackp;
178 frame->sf.back_chain = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100180 /* new return point is ret_from_fork */
181 frame->sf.gprs[8] = (unsigned long) ret_from_fork;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100183 /* fake return stack for resume(), don't go back to schedule */
184 frame->sf.gprs[9] = (unsigned long) frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 /* Save access registers to new thread structure. */
187 save_access_regs(&p->thread.acrs[0]);
188
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800189#ifndef CONFIG_64BIT
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100190 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 * save fprs to current->thread.fp_regs to merge them with
192 * the emulated registers and then copy the result to the child.
193 */
194 save_fp_regs(&current->thread.fp_regs);
195 memcpy(&p->thread.fp_regs, &current->thread.fp_regs,
196 sizeof(s390_fp_regs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 /* Set a new TLS ? */
198 if (clone_flags & CLONE_SETTLS)
199 p->thread.acrs[0] = regs->gprs[6];
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800200#else /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 /* Save the fpu registers to new thread structure. */
202 save_fp_regs(&p->thread.fp_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 /* Set a new TLS ? */
204 if (clone_flags & CLONE_SETTLS) {
Heiko Carstens77575912009-06-12 10:26:25 +0200205 if (is_compat_task()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 p->thread.acrs[0] = (unsigned int) regs->gprs[6];
207 } else {
208 p->thread.acrs[0] = (unsigned int)(regs->gprs[6] >> 32);
209 p->thread.acrs[1] = (unsigned int) regs->gprs[6];
210 }
211 }
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800212#endif /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 /* start new process with ar4 pointing to the correct address space */
214 p->thread.mm_segment = get_fs();
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100215 /* Don't copy debug registers */
216 memset(&p->thread.per_info, 0, sizeof(p->thread.per_info));
Martin Schwidefskyf8d5faf2010-01-13 20:44:26 +0100217 clear_tsk_thread_flag(p, TIF_SINGLE_STEP);
Heiko Carstens5168ce2c2009-03-26 15:23:53 +0100218 /* Initialize per thread user and system timer values */
219 ti = task_thread_info(p);
220 ti->user_timer = 0;
221 ti->system_timer = 0;
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100222 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
Heiko Carstens26689452009-01-14 14:14:36 +0100225SYSCALL_DEFINE0(fork)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200227 struct pt_regs *regs = task_pt_regs(current);
228 return do_fork(SIGCHLD, regs->gprs[15], regs, 0, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
230
Heiko Carstens2d70ca22009-09-22 22:58:41 +0200231SYSCALL_DEFINE4(clone, unsigned long, newsp, unsigned long, clone_flags,
232 int __user *, parent_tidptr, int __user *, child_tidptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200234 struct pt_regs *regs = task_pt_regs(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200236 if (!newsp)
237 newsp = regs->gprs[15];
238 return do_fork(clone_flags, newsp, regs, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 parent_tidptr, child_tidptr);
240}
241
242/*
243 * This is trivial, and on the face of it looks like it
244 * could equally well be done in user mode.
245 *
246 * Not so, for quite unobvious reasons - register pressure.
247 * In user mode vfork() cannot have a stack frame, and if
248 * done by calling the "clone()" system call directly, you
249 * do not have enough call-clobbered registers to hold all
250 * the information you need.
251 */
Heiko Carstens26689452009-01-14 14:14:36 +0100252SYSCALL_DEFINE0(vfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200254 struct pt_regs *regs = task_pt_regs(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD,
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200256 regs->gprs[15], regs, 0, NULL, NULL);
257}
258
259asmlinkage void execve_tail(void)
260{
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200261 current->thread.fp_regs.fpc = 0;
262 if (MACHINE_HAS_IEEE)
263 asm volatile("sfpc %0,%0" : : "d" (0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
266/*
267 * sys_execve() executes a new program.
268 */
David Howellsd7627462010-08-17 23:52:56 +0100269SYSCALL_DEFINE3(execve, const char __user *, name,
270 const char __user *const __user *, argv,
271 const char __user *const __user *, envp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200273 struct pt_regs *regs = task_pt_regs(current);
274 char *filename;
Heiko Carstens3e86a8c2009-09-22 22:58:42 +0200275 long rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Heiko Carstens3e86a8c2009-09-22 22:58:42 +0200277 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)
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200283 goto out;
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200284 execve_tail();
Heiko Carstens3e86a8c2009-09-22 22:58:42 +0200285 rc = regs->gprs[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286out:
Heiko Carstens3e86a8c2009-09-22 22:58:42 +0200287 putname(filename);
288 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291/*
292 * fill in the FPU structure for a core dump.
293 */
294int dump_fpu (struct pt_regs * regs, s390_fp_regs *fpregs)
295{
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800296#ifndef CONFIG_64BIT
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100297 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 * save fprs to current->thread.fp_regs to merge them with
299 * the emulated registers and then copy the result to the dump.
300 */
301 save_fp_regs(&current->thread.fp_regs);
302 memcpy(fpregs, &current->thread.fp_regs, sizeof(s390_fp_regs));
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800303#else /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 save_fp_regs(fpregs);
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800305#endif /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 return 1;
307}
Heiko Carstens1485c5c2009-03-26 15:24:04 +0100308EXPORT_SYMBOL(dump_fpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310unsigned long get_wchan(struct task_struct *p)
311{
312 struct stack_frame *sf, *low, *high;
313 unsigned long return_address;
314 int count;
315
Al Viro30af7122006-01-12 01:05:50 -0800316 if (!p || p == current || p->state == TASK_RUNNING || !task_stack_page(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return 0;
Al Viro30af7122006-01-12 01:05:50 -0800318 low = task_stack_page(p);
319 high = (struct stack_frame *) task_pt_regs(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 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;
330 }
331 return 0;
332}