blob: 189d978f76d63b7fefd0883a28eb7bca594e9970 [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>
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>
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 Carstens5c699712008-01-26 14:11:01 +010030#include <linux/utsname.h>
Heiko Carstens5a62b192008-04-17 07:46:25 +020031#include <linux/tick.h>
Heiko Carstensa8061702008-04-17 07:46:26 +020032#include <linux/elfcore.h>
Martin Schwidefsky6f430922008-12-31 15:11:40 +010033#include <linux/kernel_stat.h>
Heiko Carstens26689452009-01-14 14:14:36 +010034#include <linux/syscalls.h>
Heiko Carstens77575912009-06-12 10:26:25 +020035#include <asm/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/uaccess.h>
37#include <asm/pgtable.h>
38#include <asm/system.h>
39#include <asm/io.h>
40#include <asm/processor.h>
41#include <asm/irq.h>
42#include <asm/timer.h>
Heiko Carstensf5daba12009-03-26 15:24:01 +010043#include <asm/nmi.h>
Heiko Carstensa8061702008-04-17 07:46:26 +020044#include "entry.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020046asmlinkage void ret_from_fork(void) asm ("ret_from_fork");
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48/*
49 * Return saved PC of a blocked thread. used in kernel/sched.
50 * resume in entry.S does not create a new stack frame, it
51 * just stores the registers %r6-%r15 to the frame given by
52 * schedule. We want to return the address of the caller of
53 * schedule, so we have to walk the backchain one time to
54 * find the frame schedule() store its return address.
55 */
56unsigned long thread_saved_pc(struct task_struct *tsk)
57{
Heiko Carstenseb33c192006-01-14 13:20:57 -080058 struct stack_frame *sf, *low, *high;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Heiko Carstenseb33c192006-01-14 13:20:57 -080060 if (!tsk || !task_stack_page(tsk))
61 return 0;
62 low = task_stack_page(tsk);
63 high = (struct stack_frame *) task_pt_regs(tsk);
64 sf = (struct stack_frame *) (tsk->thread.ksp & PSW_ADDR_INSN);
65 if (sf <= low || sf > high)
66 return 0;
67 sf = (struct stack_frame *) (sf->back_chain & PSW_ADDR_INSN);
68 if (sf <= low || sf > high)
69 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 return sf->gprs[8];
71}
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/*
74 * The idle loop on a S390...
75 */
Adrian Bunkcdb04522006-03-24 03:15:57 -080076static void default_idle(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 /* CPU is going idle. */
Nick Piggin64c7c8f2005-11-08 21:39:04 -080079 local_irq_disable();
80 if (need_resched()) {
81 local_irq_enable();
82 return;
83 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#ifdef CONFIG_HOTPLUG_CPU
Heiko Carstens43ca5c32008-04-17 07:46:23 +020085 if (cpu_is_offline(smp_processor_id())) {
Heiko Carstens1fca2512006-02-17 13:52:46 -080086 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 cpu_die();
Heiko Carstens1fca2512006-02-17 13:52:46 -080088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#endif
Heiko Carstens77fa2242005-06-25 14:55:30 -070090 local_mcck_disable();
91 if (test_thread_flag(TIF_MCCK_PENDING)) {
92 local_mcck_enable();
93 local_irq_enable();
94 s390_handle_mcck();
95 return;
96 }
Heiko Carstens1f194a42006-07-03 00:24:46 -070097 trace_hardirqs_on();
Heiko Carstens632448f2008-11-14 18:18:04 +010098 /* Don't trace preempt off for idle. */
99 stop_critical_timings();
Martin Schwidefsky9cfb9b32008-12-31 15:11:41 +0100100 /* Stop virtual timer and halt the cpu. */
101 vtime_stop_cpu();
102 /* Reenable preemption tracer. */
Heiko Carstens632448f2008-11-14 18:18:04 +0100103 start_critical_timings();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
106void cpu_idle(void)
107{
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800108 for (;;) {
Thomas Gleixnere3381252008-07-19 09:33:21 +0200109 tick_nohz_stop_sched_tick(1);
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800110 while (!need_resched())
111 default_idle();
Heiko Carstens5a62b192008-04-17 07:46:25 +0200112 tick_nohz_restart_sched_tick();
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800113 preempt_enable_no_resched();
114 schedule();
115 preempt_disable();
116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119extern void kernel_thread_starter(void);
120
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200121asm(
122 ".align 4\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"
127 " br 11\n");
128
129int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
130{
131 struct pt_regs regs;
132
133 memset(&regs, 0, sizeof(regs));
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100134 regs.psw.mask = psw_kernel_bits | PSW_MASK_IO | PSW_MASK_EXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 regs.psw.addr = (unsigned long) kernel_thread_starter | PSW_ADDR_AMODE;
136 regs.gprs[9] = (unsigned long) fn;
137 regs.gprs[10] = (unsigned long) arg;
138 regs.gprs[11] = (unsigned long) do_exit;
139 regs.orig_gpr2 = -1;
140
141 /* Ok, create the new process.. */
142 return do_fork(flags | CLONE_VM | CLONE_UNTRACED,
143 0, &regs, 0, NULL, NULL);
144}
Heiko Carstens1485c5c2009-03-26 15:24:04 +0100145EXPORT_SYMBOL(kernel_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147/*
148 * Free current thread data structures etc..
149 */
150void exit_thread(void)
151{
152}
153
154void flush_thread(void)
155{
156 clear_used_math();
157 clear_tsk_thread_flag(current, TIF_USEDFPU);
158}
159
160void release_thread(struct task_struct *dead_task)
161{
162}
163
Alexey Dobriyan6f2c55b2009-04-02 16:56:59 -0700164int copy_thread(unsigned long clone_flags, unsigned long new_stackp,
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100165 unsigned long unused,
166 struct task_struct *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Heiko Carstens5168ce2c2009-03-26 15:23:53 +0100168 struct thread_info *ti;
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100169 struct fake_frame
170 {
171 struct stack_frame sf;
172 struct pt_regs childregs;
173 } *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100175 frame = container_of(task_pt_regs(p), struct fake_frame, childregs);
176 p->thread.ksp = (unsigned long) frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 /* Store access registers to kernel stack of new process. */
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100178 frame->childregs = *regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 frame->childregs.gprs[2] = 0; /* child returns 0 on fork. */
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100180 frame->childregs.gprs[15] = new_stackp;
181 frame->sf.back_chain = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100183 /* new return point is ret_from_fork */
184 frame->sf.gprs[8] = (unsigned long) ret_from_fork;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100186 /* fake return stack for resume(), don't go back to schedule */
187 frame->sf.gprs[9] = (unsigned long) frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 /* Save access registers to new thread structure. */
190 save_access_regs(&p->thread.acrs[0]);
191
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800192#ifndef CONFIG_64BIT
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100193 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 * save fprs to current->thread.fp_regs to merge them with
195 * the emulated registers and then copy the result to the child.
196 */
197 save_fp_regs(&current->thread.fp_regs);
198 memcpy(&p->thread.fp_regs, &current->thread.fp_regs,
199 sizeof(s390_fp_regs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 /* Set a new TLS ? */
201 if (clone_flags & CLONE_SETTLS)
202 p->thread.acrs[0] = regs->gprs[6];
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800203#else /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 /* Save the fpu registers to new thread structure. */
205 save_fp_regs(&p->thread.fp_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 /* Set a new TLS ? */
207 if (clone_flags & CLONE_SETTLS) {
Heiko Carstens77575912009-06-12 10:26:25 +0200208 if (is_compat_task()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 p->thread.acrs[0] = (unsigned int) regs->gprs[6];
210 } else {
211 p->thread.acrs[0] = (unsigned int)(regs->gprs[6] >> 32);
212 p->thread.acrs[1] = (unsigned int) regs->gprs[6];
213 }
214 }
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800215#endif /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 /* start new process with ar4 pointing to the correct address space */
217 p->thread.mm_segment = get_fs();
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100218 /* Don't copy debug registers */
219 memset(&p->thread.per_info, 0, sizeof(p->thread.per_info));
Heiko Carstens5168ce2c2009-03-26 15:23:53 +0100220 /* Initialize per thread user and system timer values */
221 ti = task_thread_info(p);
222 ti->user_timer = 0;
223 ti->system_timer = 0;
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100224 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
Heiko Carstens26689452009-01-14 14:14:36 +0100227SYSCALL_DEFINE0(fork)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200229 struct pt_regs *regs = task_pt_regs(current);
230 return do_fork(SIGCHLD, regs->gprs[15], regs, 0, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
232
Heiko Carstens2d70ca22009-09-22 22:58:41 +0200233SYSCALL_DEFINE4(clone, unsigned long, newsp, unsigned long, clone_flags,
234 int __user *, parent_tidptr, int __user *, child_tidptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200236 struct pt_regs *regs = task_pt_regs(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200238 if (!newsp)
239 newsp = regs->gprs[15];
240 return do_fork(clone_flags, newsp, regs, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 parent_tidptr, child_tidptr);
242}
243
244/*
245 * This is trivial, and on the face of it looks like it
246 * could equally well be done in user mode.
247 *
248 * Not so, for quite unobvious reasons - register pressure.
249 * In user mode vfork() cannot have a stack frame, and if
250 * done by calling the "clone()" system call directly, you
251 * do not have enough call-clobbered registers to hold all
252 * the information you need.
253 */
Heiko Carstens26689452009-01-14 14:14:36 +0100254SYSCALL_DEFINE0(vfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200256 struct pt_regs *regs = task_pt_regs(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD,
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200258 regs->gprs[15], regs, 0, NULL, NULL);
259}
260
261asmlinkage void execve_tail(void)
262{
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200263 current->thread.fp_regs.fpc = 0;
264 if (MACHINE_HAS_IEEE)
265 asm volatile("sfpc %0,%0" : : "d" (0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
268/*
269 * sys_execve() executes a new program.
270 */
Heiko Carstens26689452009-01-14 14:14:36 +0100271SYSCALL_DEFINE0(execve)
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;
275 unsigned long result;
276 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200278 filename = getname((char __user *) regs->orig_gpr2);
279 if (IS_ERR(filename)) {
280 result = PTR_ERR(filename);
281 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200283 rc = do_execve(filename, (char __user * __user *) regs->gprs[3],
284 (char __user * __user *) regs->gprs[4], regs);
285 if (rc) {
286 result = rc;
287 goto out_putname;
288 }
289 execve_tail();
290 result = regs->gprs[2];
291out_putname:
292 putname(filename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293out:
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200294 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297/*
298 * fill in the FPU structure for a core dump.
299 */
300int dump_fpu (struct pt_regs * regs, s390_fp_regs *fpregs)
301{
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800302#ifndef CONFIG_64BIT
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100303 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 * save fprs to current->thread.fp_regs to merge them with
305 * the emulated registers and then copy the result to the dump.
306 */
307 save_fp_regs(&current->thread.fp_regs);
308 memcpy(fpregs, &current->thread.fp_regs, sizeof(s390_fp_regs));
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800309#else /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 save_fp_regs(fpregs);
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800311#endif /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 return 1;
313}
Heiko Carstens1485c5c2009-03-26 15:24:04 +0100314EXPORT_SYMBOL(dump_fpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316unsigned long get_wchan(struct task_struct *p)
317{
318 struct stack_frame *sf, *low, *high;
319 unsigned long return_address;
320 int count;
321
Al Viro30af7122006-01-12 01:05:50 -0800322 if (!p || p == current || p->state == TASK_RUNNING || !task_stack_page(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 return 0;
Al Viro30af7122006-01-12 01:05:50 -0800324 low = task_stack_page(p);
325 high = (struct stack_frame *) task_pt_regs(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 sf = (struct stack_frame *) (p->thread.ksp & PSW_ADDR_INSN);
327 if (sf <= low || sf > high)
328 return 0;
329 for (count = 0; count < 16; count++) {
330 sf = (struct stack_frame *) (sf->back_chain & PSW_ADDR_INSN);
331 if (sf <= low || sf > high)
332 return 0;
333 return_address = sf->gprs[8] & PSW_ADDR_INSN;
334 if (!in_sched_functions(return_address))
335 return return_address;
336 }
337 return 0;
338}