blob: 59fe6ecc6ed33389fed8947f996b2a9cd5c39e6f [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 Carstens3e86a8c2009-09-22 22:58:42 +020035#include <linux/compat.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 Carstensa8061702008-04-17 07:46:26 +020045#include "entry.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020047asmlinkage void ret_from_fork(void) asm ("ret_from_fork");
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49/*
50 * Return saved PC of a blocked thread. used in kernel/sched.
51 * resume in entry.S does not create a new stack frame, it
52 * just stores the registers %r6-%r15 to the frame given by
53 * schedule. We want to return the address of the caller of
54 * schedule, so we have to walk the backchain one time to
55 * find the frame schedule() store its return address.
56 */
57unsigned long thread_saved_pc(struct task_struct *tsk)
58{
Heiko Carstenseb33c192006-01-14 13:20:57 -080059 struct stack_frame *sf, *low, *high;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Heiko Carstenseb33c192006-01-14 13:20:57 -080061 if (!tsk || !task_stack_page(tsk))
62 return 0;
63 low = task_stack_page(tsk);
64 high = (struct stack_frame *) task_pt_regs(tsk);
65 sf = (struct stack_frame *) (tsk->thread.ksp & PSW_ADDR_INSN);
66 if (sf <= low || sf > high)
67 return 0;
68 sf = (struct stack_frame *) (sf->back_chain & PSW_ADDR_INSN);
69 if (sf <= low || sf > high)
70 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 return sf->gprs[8];
72}
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074/*
75 * The idle loop on a S390...
76 */
Adrian Bunkcdb04522006-03-24 03:15:57 -080077static void default_idle(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 /* CPU is going idle. */
Nick Piggin64c7c8f2005-11-08 21:39:04 -080080 local_irq_disable();
81 if (need_resched()) {
82 local_irq_enable();
83 return;
84 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#ifdef CONFIG_HOTPLUG_CPU
Heiko Carstens43ca5c32008-04-17 07:46:23 +020086 if (cpu_is_offline(smp_processor_id())) {
Heiko Carstens1fca2512006-02-17 13:52:46 -080087 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 cpu_die();
Heiko Carstens1fca2512006-02-17 13:52:46 -080089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#endif
Heiko Carstens77fa2242005-06-25 14:55:30 -070091 local_mcck_disable();
92 if (test_thread_flag(TIF_MCCK_PENDING)) {
93 local_mcck_enable();
94 local_irq_enable();
95 s390_handle_mcck();
96 return;
97 }
Heiko Carstens1f194a42006-07-03 00:24:46 -070098 trace_hardirqs_on();
Heiko Carstens632448f2008-11-14 18:18:04 +010099 /* Don't trace preempt off for idle. */
100 stop_critical_timings();
Martin Schwidefsky9cfb9b32008-12-31 15:11:41 +0100101 /* Stop virtual timer and halt the cpu. */
102 vtime_stop_cpu();
103 /* Reenable preemption tracer. */
Heiko Carstens632448f2008-11-14 18:18:04 +0100104 start_critical_timings();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
107void cpu_idle(void)
108{
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800109 for (;;) {
Thomas Gleixnere3381252008-07-19 09:33:21 +0200110 tick_nohz_stop_sched_tick(1);
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800111 while (!need_resched())
112 default_idle();
Heiko Carstens5a62b192008-04-17 07:46:25 +0200113 tick_nohz_restart_sched_tick();
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800114 preempt_enable_no_resched();
115 schedule();
116 preempt_disable();
117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120extern void kernel_thread_starter(void);
121
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200122asm(
123 ".align 4\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 "kernel_thread_starter:\n"
125 " la 2,0(10)\n"
126 " basr 14,9\n"
127 " la 2,0\n"
128 " br 11\n");
129
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{
157 clear_used_math();
158 clear_tsk_thread_flag(current, TIF_USEDFPU);
159}
160
161void release_thread(struct task_struct *dead_task)
162{
163}
164
Alexey Dobriyan6f2c55b2009-04-02 16:56:59 -0700165int copy_thread(unsigned long clone_flags, unsigned long new_stackp,
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100166 unsigned long unused,
167 struct task_struct *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Heiko Carstens5168ce22009-03-26 15:23:53 +0100169 struct thread_info *ti;
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100170 struct fake_frame
171 {
172 struct stack_frame sf;
173 struct pt_regs childregs;
174 } *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100176 frame = container_of(task_pt_regs(p), struct fake_frame, childregs);
177 p->thread.ksp = (unsigned long) frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 /* Store access registers to kernel stack of new process. */
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100179 frame->childregs = *regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 frame->childregs.gprs[2] = 0; /* child returns 0 on fork. */
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100181 frame->childregs.gprs[15] = new_stackp;
182 frame->sf.back_chain = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100184 /* new return point is ret_from_fork */
185 frame->sf.gprs[8] = (unsigned long) ret_from_fork;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100187 /* fake return stack for resume(), don't go back to schedule */
188 frame->sf.gprs[9] = (unsigned long) frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 /* Save access registers to new thread structure. */
191 save_access_regs(&p->thread.acrs[0]);
192
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800193#ifndef CONFIG_64BIT
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100194 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 * save fprs to current->thread.fp_regs to merge them with
196 * the emulated registers and then copy the result to the child.
197 */
198 save_fp_regs(&current->thread.fp_regs);
199 memcpy(&p->thread.fp_regs, &current->thread.fp_regs,
200 sizeof(s390_fp_regs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 /* Set a new TLS ? */
202 if (clone_flags & CLONE_SETTLS)
203 p->thread.acrs[0] = regs->gprs[6];
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800204#else /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 /* Save the fpu registers to new thread structure. */
206 save_fp_regs(&p->thread.fp_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 /* Set a new TLS ? */
208 if (clone_flags & CLONE_SETTLS) {
Heiko Carstens77575912009-06-12 10:26:25 +0200209 if (is_compat_task()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 p->thread.acrs[0] = (unsigned int) regs->gprs[6];
211 } else {
212 p->thread.acrs[0] = (unsigned int)(regs->gprs[6] >> 32);
213 p->thread.acrs[1] = (unsigned int) regs->gprs[6];
214 }
215 }
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800216#endif /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 /* start new process with ar4 pointing to the correct address space */
218 p->thread.mm_segment = get_fs();
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100219 /* Don't copy debug registers */
220 memset(&p->thread.per_info, 0, sizeof(p->thread.per_info));
Heiko Carstens5168ce22009-03-26 15:23:53 +0100221 /* Initialize per thread user and system timer values */
222 ti = task_thread_info(p);
223 ti->user_timer = 0;
224 ti->system_timer = 0;
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100225 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
Heiko Carstens26689452009-01-14 14:14:36 +0100228SYSCALL_DEFINE0(fork)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200230 struct pt_regs *regs = task_pt_regs(current);
231 return do_fork(SIGCHLD, regs->gprs[15], regs, 0, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
Heiko Carstens2d70ca22009-09-22 22:58:41 +0200234SYSCALL_DEFINE4(clone, unsigned long, newsp, unsigned long, clone_flags,
235 int __user *, parent_tidptr, int __user *, child_tidptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200237 struct pt_regs *regs = task_pt_regs(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200239 if (!newsp)
240 newsp = regs->gprs[15];
241 return do_fork(clone_flags, newsp, regs, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 parent_tidptr, child_tidptr);
243}
244
245/*
246 * This is trivial, and on the face of it looks like it
247 * could equally well be done in user mode.
248 *
249 * Not so, for quite unobvious reasons - register pressure.
250 * In user mode vfork() cannot have a stack frame, and if
251 * done by calling the "clone()" system call directly, you
252 * do not have enough call-clobbered registers to hold all
253 * the information you need.
254 */
Heiko Carstens26689452009-01-14 14:14:36 +0100255SYSCALL_DEFINE0(vfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200257 struct pt_regs *regs = task_pt_regs(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD,
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200259 regs->gprs[15], regs, 0, NULL, NULL);
260}
261
262asmlinkage void execve_tail(void)
263{
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200264 current->thread.fp_regs.fpc = 0;
265 if (MACHINE_HAS_IEEE)
266 asm volatile("sfpc %0,%0" : : "d" (0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
269/*
270 * sys_execve() executes a new program.
271 */
Heiko Carstens3e86a8c2009-09-22 22:58:42 +0200272SYSCALL_DEFINE3(execve, char __user *, name, char __user * __user *, argv,
273 char __user * __user *, envp)
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;
Heiko Carstens3e86a8c2009-09-22 22:58:42 +0200277 long rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Heiko Carstens3e86a8c2009-09-22 22:58:42 +0200279 filename = getname(name);
280 rc = PTR_ERR(filename);
281 if (IS_ERR(filename))
282 return rc;
283 rc = do_execve(filename, argv, envp, regs);
284 if (rc)
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200285 goto out;
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200286 execve_tail();
Heiko Carstens3e86a8c2009-09-22 22:58:42 +0200287 rc = regs->gprs[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288out:
Heiko Carstens3e86a8c2009-09-22 22:58:42 +0200289 putname(filename);
290 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293/*
294 * fill in the FPU structure for a core dump.
295 */
296int dump_fpu (struct pt_regs * regs, s390_fp_regs *fpregs)
297{
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800298#ifndef CONFIG_64BIT
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100299 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 * save fprs to current->thread.fp_regs to merge them with
301 * the emulated registers and then copy the result to the dump.
302 */
303 save_fp_regs(&current->thread.fp_regs);
304 memcpy(fpregs, &current->thread.fp_regs, sizeof(s390_fp_regs));
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800305#else /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 save_fp_regs(fpregs);
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800307#endif /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 return 1;
309}
Heiko Carstens1485c5c2009-03-26 15:24:04 +0100310EXPORT_SYMBOL(dump_fpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312unsigned long get_wchan(struct task_struct *p)
313{
314 struct stack_frame *sf, *low, *high;
315 unsigned long return_address;
316 int count;
317
Al Viro30af7122006-01-12 01:05:50 -0800318 if (!p || p == current || p->state == TASK_RUNNING || !task_stack_page(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 return 0;
Al Viro30af7122006-01-12 01:05:50 -0800320 low = task_stack_page(p);
321 high = (struct stack_frame *) task_pt_regs(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 sf = (struct stack_frame *) (p->thread.ksp & PSW_ADDR_INSN);
323 if (sf <= low || sf > high)
324 return 0;
325 for (count = 0; count < 16; count++) {
326 sf = (struct stack_frame *) (sf->back_chain & PSW_ADDR_INSN);
327 if (sf <= low || sf > high)
328 return 0;
329 return_address = sf->gprs[8] & PSW_ADDR_INSN;
330 if (!in_sched_functions(return_address))
331 return return_address;
332 }
333 return 0;
334}