blob: e0563baa1cfe009f5d43510df2dd72c0a0d3696a [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/uaccess.h>
36#include <asm/pgtable.h>
37#include <asm/system.h>
38#include <asm/io.h>
39#include <asm/processor.h>
40#include <asm/irq.h>
41#include <asm/timer.h>
Heiko Carstensa8061702008-04-17 07:46:26 +020042#include "entry.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020044asmlinkage void ret_from_fork(void) asm ("ret_from_fork");
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46/*
47 * Return saved PC of a blocked thread. used in kernel/sched.
48 * resume in entry.S does not create a new stack frame, it
49 * just stores the registers %r6-%r15 to the frame given by
50 * schedule. We want to return the address of the caller of
51 * schedule, so we have to walk the backchain one time to
52 * find the frame schedule() store its return address.
53 */
54unsigned long thread_saved_pc(struct task_struct *tsk)
55{
Heiko Carstenseb33c192006-01-14 13:20:57 -080056 struct stack_frame *sf, *low, *high;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Heiko Carstenseb33c192006-01-14 13:20:57 -080058 if (!tsk || !task_stack_page(tsk))
59 return 0;
60 low = task_stack_page(tsk);
61 high = (struct stack_frame *) task_pt_regs(tsk);
62 sf = (struct stack_frame *) (tsk->thread.ksp & PSW_ADDR_INSN);
63 if (sf <= low || sf > high)
64 return 0;
65 sf = (struct stack_frame *) (sf->back_chain & PSW_ADDR_INSN);
66 if (sf <= low || sf > high)
67 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 return sf->gprs[8];
69}
70
Heiko Carstens77fa2242005-06-25 14:55:30 -070071extern void s390_handle_mcck(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/*
73 * The idle loop on a S390...
74 */
Adrian Bunkcdb04522006-03-24 03:15:57 -080075static void default_idle(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 /* CPU is going idle. */
Nick Piggin64c7c8f2005-11-08 21:39:04 -080078 local_irq_disable();
79 if (need_resched()) {
80 local_irq_enable();
81 return;
82 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#ifdef CONFIG_HOTPLUG_CPU
Heiko Carstens43ca5c32008-04-17 07:46:23 +020084 if (cpu_is_offline(smp_processor_id())) {
Heiko Carstens1fca2512006-02-17 13:52:46 -080085 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 cpu_die();
Heiko Carstens1fca2512006-02-17 13:52:46 -080087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#endif
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118extern void kernel_thread_starter(void);
119
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200120asm(
121 ".align 4\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 "kernel_thread_starter:\n"
123 " la 2,0(10)\n"
124 " basr 14,9\n"
125 " la 2,0\n"
126 " br 11\n");
127
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}
144
145/*
146 * Free current thread data structures etc..
147 */
148void exit_thread(void)
149{
150}
151
152void flush_thread(void)
153{
154 clear_used_math();
155 clear_tsk_thread_flag(current, TIF_USEDFPU);
156}
157
158void release_thread(struct task_struct *dead_task)
159{
160}
161
162int copy_thread(int nr, unsigned long clone_flags, unsigned long new_stackp,
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100163 unsigned long unused,
164 struct task_struct *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
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) {
205 if (test_thread_flag(TIF_31BIT)) {
206 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));
217 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
219
Heiko Carstens26689452009-01-14 14:14:36 +0100220SYSCALL_DEFINE0(fork)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200222 struct pt_regs *regs = task_pt_regs(current);
223 return do_fork(SIGCHLD, regs->gprs[15], regs, 0, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
Heiko Carstens26689452009-01-14 14:14:36 +0100226SYSCALL_DEFINE0(clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200228 struct pt_regs *regs = task_pt_regs(current);
229 unsigned long clone_flags;
230 unsigned long newsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 int __user *parent_tidptr, *child_tidptr;
232
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200233 clone_flags = regs->gprs[3];
234 newsp = regs->orig_gpr2;
235 parent_tidptr = (int __user *) regs->gprs[4];
236 child_tidptr = (int __user *) regs->gprs[5];
237 if (!newsp)
238 newsp = regs->gprs[15];
239 return do_fork(clone_flags, newsp, regs, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 parent_tidptr, child_tidptr);
241}
242
243/*
244 * This is trivial, and on the face of it looks like it
245 * could equally well be done in user mode.
246 *
247 * Not so, for quite unobvious reasons - register pressure.
248 * In user mode vfork() cannot have a stack frame, and if
249 * done by calling the "clone()" system call directly, you
250 * do not have enough call-clobbered registers to hold all
251 * the information you need.
252 */
Heiko Carstens26689452009-01-14 14:14:36 +0100253SYSCALL_DEFINE0(vfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200255 struct pt_regs *regs = task_pt_regs(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD,
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200257 regs->gprs[15], regs, 0, NULL, NULL);
258}
259
260asmlinkage void execve_tail(void)
261{
262 task_lock(current);
263 current->ptrace &= ~PT_DTRACE;
264 task_unlock(current);
265 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 */
Heiko Carstens26689452009-01-14 14:14:36 +0100273SYSCALL_DEFINE0(execve)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200275 struct pt_regs *regs = task_pt_regs(current);
276 char *filename;
277 unsigned long result;
278 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200280 filename = getname((char __user *) regs->orig_gpr2);
281 if (IS_ERR(filename)) {
282 result = PTR_ERR(filename);
283 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200285 rc = do_execve(filename, (char __user * __user *) regs->gprs[3],
286 (char __user * __user *) regs->gprs[4], regs);
287 if (rc) {
288 result = rc;
289 goto out_putname;
290 }
291 execve_tail();
292 result = regs->gprs[2];
293out_putname:
294 putname(filename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295out:
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200296 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299/*
300 * fill in the FPU structure for a core dump.
301 */
302int dump_fpu (struct pt_regs * regs, s390_fp_regs *fpregs)
303{
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800304#ifndef CONFIG_64BIT
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100305 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 * save fprs to current->thread.fp_regs to merge them with
307 * the emulated registers and then copy the result to the dump.
308 */
309 save_fp_regs(&current->thread.fp_regs);
310 memcpy(fpregs, &current->thread.fp_regs, sizeof(s390_fp_regs));
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800311#else /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 save_fp_regs(fpregs);
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800313#endif /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 return 1;
315}
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317unsigned long get_wchan(struct task_struct *p)
318{
319 struct stack_frame *sf, *low, *high;
320 unsigned long return_address;
321 int count;
322
Al Viro30af7122006-01-12 01:05:50 -0800323 if (!p || p == current || p->state == TASK_RUNNING || !task_stack_page(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 return 0;
Al Viro30af7122006-01-12 01:05:50 -0800325 low = task_stack_page(p);
326 high = (struct stack_frame *) task_pt_regs(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 sf = (struct stack_frame *) (p->thread.ksp & PSW_ADDR_INSN);
328 if (sf <= low || sf > high)
329 return 0;
330 for (count = 0; count < 16; count++) {
331 sf = (struct stack_frame *) (sf->back_chain & PSW_ADDR_INSN);
332 if (sf <= low || sf > high)
333 return 0;
334 return_address = sf->gprs[8] & PSW_ADDR_INSN;
335 if (!in_sched_functions(return_address))
336 return return_address;
337 }
338 return 0;
339}