blob: 5e64a9a29ea4c921e595e330bb8bb467095954c5 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/sched.h>
13#include <linux/kernel.h>
14#include <linux/mm.h>
15#include <linux/smp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/interrupt.h>
Heiko Carstens5a62b192008-04-17 07:46:25 +020018#include <linux/tick.h>
Heiko Carstens9887a1f2011-01-12 09:55:28 +010019#include <linux/personality.h>
Heiko Carstens26689452009-01-14 14:14:36 +010020#include <linux/syscalls.h>
Heiko Carstens3e86a8c2009-09-22 22:58:42 +020021#include <linux/compat.h>
Martin Schwidefsky860dba42011-01-05 12:47:25 +010022#include <linux/kprobes.h>
Heiko Carstens9887a1f2011-01-12 09:55:28 +010023#include <linux/random.h>
Jan Glauber3af6fb62011-05-23 10:24:44 +020024#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/system.h>
26#include <asm/io.h>
27#include <asm/processor.h>
28#include <asm/irq.h>
29#include <asm/timer.h>
Heiko Carstensf5daba12009-03-26 15:24:01 +010030#include <asm/nmi.h>
Jan Glauber3af6fb62011-05-23 10:24:44 +020031#include <asm/compat.h>
Heiko Carstensda7f51c2011-01-05 12:48:09 +010032#include <asm/smp.h>
Heiko Carstensa8061702008-04-17 07:46:26 +020033#include "entry.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020035asmlinkage void ret_from_fork(void) asm ("ret_from_fork");
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37/*
38 * Return saved PC of a blocked thread. used in kernel/sched.
39 * resume in entry.S does not create a new stack frame, it
40 * just stores the registers %r6-%r15 to the frame given by
41 * schedule. We want to return the address of the caller of
42 * schedule, so we have to walk the backchain one time to
43 * find the frame schedule() store its return address.
44 */
45unsigned long thread_saved_pc(struct task_struct *tsk)
46{
Heiko Carstenseb33c192006-01-14 13:20:57 -080047 struct stack_frame *sf, *low, *high;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Heiko Carstenseb33c192006-01-14 13:20:57 -080049 if (!tsk || !task_stack_page(tsk))
50 return 0;
51 low = task_stack_page(tsk);
52 high = (struct stack_frame *) task_pt_regs(tsk);
53 sf = (struct stack_frame *) (tsk->thread.ksp & PSW_ADDR_INSN);
54 if (sf <= low || sf > high)
55 return 0;
56 sf = (struct stack_frame *) (sf->back_chain & PSW_ADDR_INSN);
57 if (sf <= low || sf > high)
58 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 return sf->gprs[8];
60}
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062/*
63 * The idle loop on a S390...
64 */
Adrian Bunkcdb04522006-03-24 03:15:57 -080065static void default_idle(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Heiko Carstensda7f51c2011-01-05 12:48:09 +010067 if (cpu_is_offline(smp_processor_id()))
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 cpu_die();
Heiko Carstens6931be02010-10-25 16:10:12 +020069 local_irq_disable();
70 if (need_resched()) {
71 local_irq_enable();
72 return;
73 }
Heiko Carstens77fa2242005-06-25 14:55:30 -070074 local_mcck_disable();
75 if (test_thread_flag(TIF_MCCK_PENDING)) {
76 local_mcck_enable();
77 local_irq_enable();
78 s390_handle_mcck();
79 return;
80 }
Heiko Carstens1f194a42006-07-03 00:24:46 -070081 trace_hardirqs_on();
Heiko Carstens632448f2008-11-14 18:18:04 +010082 /* Don't trace preempt off for idle. */
83 stop_critical_timings();
Martin Schwidefsky9cfb9b32008-12-31 15:11:41 +010084 /* Stop virtual timer and halt the cpu. */
85 vtime_stop_cpu();
86 /* Reenable preemption tracer. */
Heiko Carstens632448f2008-11-14 18:18:04 +010087 start_critical_timings();
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
90void cpu_idle(void)
91{
Nick Piggin5bfb5d62005-11-08 21:39:01 -080092 for (;;) {
Thomas Gleixnere3381252008-07-19 09:33:21 +020093 tick_nohz_stop_sched_tick(1);
Nick Piggin5bfb5d62005-11-08 21:39:01 -080094 while (!need_resched())
95 default_idle();
Heiko Carstens5a62b192008-04-17 07:46:25 +020096 tick_nohz_restart_sched_tick();
Nick Piggin5bfb5d62005-11-08 21:39:01 -080097 preempt_enable_no_resched();
98 schedule();
99 preempt_disable();
100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
Martin Schwidefsky860dba42011-01-05 12:47:25 +0100103extern void __kprobes kernel_thread_starter(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200105asm(
Martin Schwidefsky860dba42011-01-05 12:47:25 +0100106 ".section .kprobes.text, \"ax\"\n"
107 ".global kernel_thread_starter\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 "kernel_thread_starter:\n"
109 " la 2,0(10)\n"
110 " basr 14,9\n"
111 " la 2,0\n"
Martin Schwidefsky860dba42011-01-05 12:47:25 +0100112 " br 11\n"
113 ".previous\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
116{
117 struct pt_regs regs;
118
119 memset(&regs, 0, sizeof(regs));
Martin Schwidefskyb50511e2011-10-30 15:16:50 +0100120 regs.psw.mask = psw_kernel_bits |
121 PSW_MASK_DAT | PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 regs.psw.addr = (unsigned long) kernel_thread_starter | PSW_ADDR_AMODE;
123 regs.gprs[9] = (unsigned long) fn;
124 regs.gprs[10] = (unsigned long) arg;
125 regs.gprs[11] = (unsigned long) do_exit;
126 regs.orig_gpr2 = -1;
127
128 /* Ok, create the new process.. */
129 return do_fork(flags | CLONE_VM | CLONE_UNTRACED,
130 0, &regs, 0, NULL, NULL);
131}
Heiko Carstens1485c5c2009-03-26 15:24:04 +0100132EXPORT_SYMBOL(kernel_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134/*
135 * Free current thread data structures etc..
136 */
137void exit_thread(void)
138{
139}
140
141void flush_thread(void)
142{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
145void release_thread(struct task_struct *dead_task)
146{
147}
148
Alexey Dobriyan6f2c55b2009-04-02 16:56:59 -0700149int copy_thread(unsigned long clone_flags, unsigned long new_stackp,
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100150 unsigned long unused,
151 struct task_struct *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Heiko Carstens5168ce22009-03-26 15:23:53 +0100153 struct thread_info *ti;
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100154 struct fake_frame
155 {
156 struct stack_frame sf;
157 struct pt_regs childregs;
158 } *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100160 frame = container_of(task_pt_regs(p), struct fake_frame, childregs);
161 p->thread.ksp = (unsigned long) frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 /* Store access registers to kernel stack of new process. */
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100163 frame->childregs = *regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 frame->childregs.gprs[2] = 0; /* child returns 0 on fork. */
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100165 frame->childregs.gprs[15] = new_stackp;
166 frame->sf.back_chain = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100168 /* new return point is ret_from_fork */
169 frame->sf.gprs[8] = (unsigned long) ret_from_fork;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100171 /* fake return stack for resume(), don't go back to schedule */
172 frame->sf.gprs[9] = (unsigned long) frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 /* Save access registers to new thread structure. */
175 save_access_regs(&p->thread.acrs[0]);
176
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800177#ifndef CONFIG_64BIT
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100178 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 * save fprs to current->thread.fp_regs to merge them with
180 * the emulated registers and then copy the result to the child.
181 */
182 save_fp_regs(&current->thread.fp_regs);
183 memcpy(&p->thread.fp_regs, &current->thread.fp_regs,
184 sizeof(s390_fp_regs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 /* Set a new TLS ? */
186 if (clone_flags & CLONE_SETTLS)
187 p->thread.acrs[0] = regs->gprs[6];
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800188#else /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 /* Save the fpu registers to new thread structure. */
190 save_fp_regs(&p->thread.fp_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 /* Set a new TLS ? */
192 if (clone_flags & CLONE_SETTLS) {
Heiko Carstens77575912009-06-12 10:26:25 +0200193 if (is_compat_task()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 p->thread.acrs[0] = (unsigned int) regs->gprs[6];
195 } else {
196 p->thread.acrs[0] = (unsigned int)(regs->gprs[6] >> 32);
197 p->thread.acrs[1] = (unsigned int) regs->gprs[6];
198 }
199 }
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800200#endif /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 /* start new process with ar4 pointing to the correct address space */
202 p->thread.mm_segment = get_fs();
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100203 /* Don't copy debug registers */
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100204 memset(&p->thread.per_user, 0, sizeof(p->thread.per_user));
205 memset(&p->thread.per_event, 0, sizeof(p->thread.per_event));
Martin Schwidefskyf8d5faf2010-01-13 20:44:26 +0100206 clear_tsk_thread_flag(p, TIF_SINGLE_STEP);
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100207 clear_tsk_thread_flag(p, TIF_PER_TRAP);
Heiko Carstens5168ce22009-03-26 15:23:53 +0100208 /* Initialize per thread user and system timer values */
209 ti = task_thread_info(p);
210 ti->user_timer = 0;
211 ti->system_timer = 0;
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100212 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}
214
Heiko Carstens26689452009-01-14 14:14:36 +0100215SYSCALL_DEFINE0(fork)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200217 struct pt_regs *regs = task_pt_regs(current);
218 return do_fork(SIGCHLD, regs->gprs[15], regs, 0, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
Heiko Carstens2d70ca22009-09-22 22:58:41 +0200221SYSCALL_DEFINE4(clone, unsigned long, newsp, unsigned long, clone_flags,
222 int __user *, parent_tidptr, int __user *, child_tidptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200224 struct pt_regs *regs = task_pt_regs(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200226 if (!newsp)
227 newsp = regs->gprs[15];
228 return do_fork(clone_flags, newsp, regs, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 parent_tidptr, child_tidptr);
230}
231
232/*
233 * This is trivial, and on the face of it looks like it
234 * could equally well be done in user mode.
235 *
236 * Not so, for quite unobvious reasons - register pressure.
237 * In user mode vfork() cannot have a stack frame, and if
238 * done by calling the "clone()" system call directly, you
239 * do not have enough call-clobbered registers to hold all
240 * the information you need.
241 */
Heiko Carstens26689452009-01-14 14:14:36 +0100242SYSCALL_DEFINE0(vfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200244 struct pt_regs *regs = task_pt_regs(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD,
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200246 regs->gprs[15], regs, 0, NULL, NULL);
247}
248
249asmlinkage void execve_tail(void)
250{
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200251 current->thread.fp_regs.fpc = 0;
252 if (MACHINE_HAS_IEEE)
253 asm volatile("sfpc %0,%0" : : "d" (0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
256/*
257 * sys_execve() executes a new program.
258 */
David Howellsd7627462010-08-17 23:52:56 +0100259SYSCALL_DEFINE3(execve, const char __user *, name,
260 const char __user *const __user *, argv,
261 const char __user *const __user *, envp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200263 struct pt_regs *regs = task_pt_regs(current);
264 char *filename;
Heiko Carstens3e86a8c2009-09-22 22:58:42 +0200265 long rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Heiko Carstens3e86a8c2009-09-22 22:58:42 +0200267 filename = getname(name);
268 rc = PTR_ERR(filename);
269 if (IS_ERR(filename))
270 return rc;
271 rc = do_execve(filename, argv, envp, regs);
272 if (rc)
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200273 goto out;
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200274 execve_tail();
Heiko Carstens3e86a8c2009-09-22 22:58:42 +0200275 rc = regs->gprs[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276out:
Heiko Carstens3e86a8c2009-09-22 22:58:42 +0200277 putname(filename);
278 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281/*
282 * fill in the FPU structure for a core dump.
283 */
284int dump_fpu (struct pt_regs * regs, s390_fp_regs *fpregs)
285{
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800286#ifndef CONFIG_64BIT
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100287 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 * save fprs to current->thread.fp_regs to merge them with
289 * the emulated registers and then copy the result to the dump.
290 */
291 save_fp_regs(&current->thread.fp_regs);
292 memcpy(fpregs, &current->thread.fp_regs, sizeof(s390_fp_regs));
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800293#else /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 save_fp_regs(fpregs);
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800295#endif /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 return 1;
297}
Heiko Carstens1485c5c2009-03-26 15:24:04 +0100298EXPORT_SYMBOL(dump_fpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300unsigned long get_wchan(struct task_struct *p)
301{
302 struct stack_frame *sf, *low, *high;
303 unsigned long return_address;
304 int count;
305
Al Viro30af7122006-01-12 01:05:50 -0800306 if (!p || p == current || p->state == TASK_RUNNING || !task_stack_page(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 return 0;
Al Viro30af7122006-01-12 01:05:50 -0800308 low = task_stack_page(p);
309 high = (struct stack_frame *) task_pt_regs(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 sf = (struct stack_frame *) (p->thread.ksp & PSW_ADDR_INSN);
311 if (sf <= low || sf > high)
312 return 0;
313 for (count = 0; count < 16; count++) {
314 sf = (struct stack_frame *) (sf->back_chain & PSW_ADDR_INSN);
315 if (sf <= low || sf > high)
316 return 0;
317 return_address = sf->gprs[8] & PSW_ADDR_INSN;
318 if (!in_sched_functions(return_address))
319 return return_address;
320 }
321 return 0;
322}
Heiko Carstens9887a1f2011-01-12 09:55:28 +0100323
324unsigned long arch_align_stack(unsigned long sp)
325{
326 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
327 sp -= get_random_int() & ~PAGE_MASK;
328 return sp & ~0xf;
329}
Heiko Carstens33519182011-01-12 09:55:30 +0100330
331static inline unsigned long brk_rnd(void)
332{
333 /* 8MB for 32bit, 1GB for 64bit */
334 if (is_32bit_task())
335 return (get_random_int() & 0x7ffUL) << PAGE_SHIFT;
336 else
337 return (get_random_int() & 0x3ffffUL) << PAGE_SHIFT;
338}
339
340unsigned long arch_randomize_brk(struct mm_struct *mm)
341{
342 unsigned long ret = PAGE_ALIGN(mm->brk + brk_rnd());
343
344 if (ret < mm->brk)
345 return mm->brk;
346 return ret;
347}
Heiko Carstensd2c9dfc2011-01-12 09:55:31 +0100348
349unsigned long randomize_et_dyn(unsigned long base)
350{
351 unsigned long ret = PAGE_ALIGN(base + brk_rnd());
352
353 if (!(current->flags & PF_RANDOMIZE))
354 return base;
355 if (ret < base)
356 return base;
357 return ret;
358}