]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
cbdc2292 | 2 | * This file handles the architecture dependent parts of process handling. |
1da177e4 | 3 | * |
cbdc2292 HC |
4 | * 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, | |
1da177e4 LT |
8 | */ |
9 | ||
1da177e4 LT |
10 | #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> | |
4e950f6f | 16 | #include <linux/fs.h> |
1da177e4 | 17 | #include <linux/smp.h> |
1da177e4 | 18 | #include <linux/stddef.h> |
5a0e3ad6 | 19 | #include <linux/slab.h> |
1da177e4 LT |
20 | #include <linux/unistd.h> |
21 | #include <linux/ptrace.h> | |
1da177e4 LT |
22 | #include <linux/vmalloc.h> |
23 | #include <linux/user.h> | |
1da177e4 LT |
24 | #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> | |
5a62b192 | 30 | #include <linux/tick.h> |
a806170e | 31 | #include <linux/elfcore.h> |
6f430924 | 32 | #include <linux/kernel_stat.h> |
9887a1fc | 33 | #include <linux/personality.h> |
26689452 | 34 | #include <linux/syscalls.h> |
3e86a8c6 | 35 | #include <linux/compat.h> |
860dba45 | 36 | #include <linux/kprobes.h> |
9887a1fc | 37 | #include <linux/random.h> |
7757591a | 38 | #include <asm/compat.h> |
1da177e4 LT |
39 | #include <asm/uaccess.h> |
40 | #include <asm/pgtable.h> | |
41 | #include <asm/system.h> | |
42 | #include <asm/io.h> | |
43 | #include <asm/processor.h> | |
44 | #include <asm/irq.h> | |
45 | #include <asm/timer.h> | |
f5daba1d | 46 | #include <asm/nmi.h> |
da7f51c1 | 47 | #include <asm/smp.h> |
a806170e | 48 | #include "entry.h" |
1da177e4 | 49 | |
94c12cc7 | 50 | asmlinkage void ret_from_fork(void) asm ("ret_from_fork"); |
1da177e4 LT |
51 | |
52 | /* | |
53 | * Return saved PC of a blocked thread. used in kernel/sched. | |
54 | * resume in entry.S does not create a new stack frame, it | |
55 | * just stores the registers %r6-%r15 to the frame given by | |
56 | * schedule. We want to return the address of the caller of | |
57 | * schedule, so we have to walk the backchain one time to | |
58 | * find the frame schedule() store its return address. | |
59 | */ | |
60 | unsigned long thread_saved_pc(struct task_struct *tsk) | |
61 | { | |
eb33c190 | 62 | struct stack_frame *sf, *low, *high; |
1da177e4 | 63 | |
eb33c190 HC |
64 | if (!tsk || !task_stack_page(tsk)) |
65 | return 0; | |
66 | low = task_stack_page(tsk); | |
67 | high = (struct stack_frame *) task_pt_regs(tsk); | |
68 | sf = (struct stack_frame *) (tsk->thread.ksp & PSW_ADDR_INSN); | |
69 | if (sf <= low || sf > high) | |
70 | return 0; | |
71 | sf = (struct stack_frame *) (sf->back_chain & PSW_ADDR_INSN); | |
72 | if (sf <= low || sf > high) | |
73 | return 0; | |
1da177e4 LT |
74 | return sf->gprs[8]; |
75 | } | |
76 | ||
1da177e4 LT |
77 | /* |
78 | * The idle loop on a S390... | |
79 | */ | |
cdb04527 | 80 | static void default_idle(void) |
1da177e4 | 81 | { |
da7f51c1 | 82 | if (cpu_is_offline(smp_processor_id())) |
1da177e4 | 83 | cpu_die(); |
6931be08 HC |
84 | local_irq_disable(); |
85 | if (need_resched()) { | |
86 | local_irq_enable(); | |
87 | return; | |
88 | } | |
77fa2245 HC |
89 | 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 | } | |
1f194a4c | 96 | trace_hardirqs_on(); |
632448f6 HC |
97 | /* Don't trace preempt off for idle. */ |
98 | stop_critical_timings(); | |
9cfb9b3c MS |
99 | /* Stop virtual timer and halt the cpu. */ |
100 | vtime_stop_cpu(); | |
101 | /* Reenable preemption tracer. */ | |
632448f6 | 102 | start_critical_timings(); |
1da177e4 LT |
103 | } |
104 | ||
105 | void cpu_idle(void) | |
106 | { | |
5bfb5d69 | 107 | for (;;) { |
e338125b | 108 | tick_nohz_stop_sched_tick(1); |
5bfb5d69 NP |
109 | while (!need_resched()) |
110 | default_idle(); | |
5a62b192 | 111 | tick_nohz_restart_sched_tick(); |
5bfb5d69 NP |
112 | preempt_enable_no_resched(); |
113 | schedule(); | |
114 | preempt_disable(); | |
115 | } | |
1da177e4 LT |
116 | } |
117 | ||
860dba45 | 118 | extern void __kprobes kernel_thread_starter(void); |
1da177e4 | 119 | |
94c12cc7 | 120 | asm( |
860dba45 MS |
121 | ".section .kprobes.text, \"ax\"\n" |
122 | ".global kernel_thread_starter\n" | |
1da177e4 LT |
123 | "kernel_thread_starter:\n" |
124 | " la 2,0(10)\n" | |
125 | " basr 14,9\n" | |
126 | " la 2,0\n" | |
860dba45 MS |
127 | " br 11\n" |
128 | ".previous\n"); | |
1da177e4 LT |
129 | |
130 | int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags) | |
131 | { | |
132 | struct pt_regs regs; | |
133 | ||
134 | memset(®s, 0, sizeof(regs)); | |
c1821c2e | 135 | regs.psw.mask = psw_kernel_bits | PSW_MASK_IO | PSW_MASK_EXT; |
1da177e4 LT |
136 | 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, ®s, 0, NULL, NULL); | |
145 | } | |
1485c5c8 | 146 | EXPORT_SYMBOL(kernel_thread); |
1da177e4 LT |
147 | |
148 | /* | |
149 | * Free current thread data structures etc.. | |
150 | */ | |
151 | void exit_thread(void) | |
152 | { | |
153 | } | |
154 | ||
155 | void flush_thread(void) | |
156 | { | |
1da177e4 LT |
157 | } |
158 | ||
159 | void release_thread(struct task_struct *dead_task) | |
160 | { | |
161 | } | |
162 | ||
6f2c55b8 | 163 | int copy_thread(unsigned long clone_flags, unsigned long new_stackp, |
cbdc2292 HC |
164 | unsigned long unused, |
165 | struct task_struct *p, struct pt_regs *regs) | |
1da177e4 | 166 | { |
5168ce2c | 167 | struct thread_info *ti; |
cbdc2292 HC |
168 | struct fake_frame |
169 | { | |
170 | struct stack_frame sf; | |
171 | struct pt_regs childregs; | |
172 | } *frame; | |
173 | ||
174 | frame = container_of(task_pt_regs(p), struct fake_frame, childregs); | |
175 | p->thread.ksp = (unsigned long) frame; | |
1da177e4 | 176 | /* Store access registers to kernel stack of new process. */ |
cbdc2292 | 177 | frame->childregs = *regs; |
1da177e4 | 178 | frame->childregs.gprs[2] = 0; /* child returns 0 on fork. */ |
cbdc2292 HC |
179 | frame->childregs.gprs[15] = new_stackp; |
180 | frame->sf.back_chain = 0; | |
1da177e4 | 181 | |
cbdc2292 HC |
182 | /* new return point is ret_from_fork */ |
183 | frame->sf.gprs[8] = (unsigned long) ret_from_fork; | |
1da177e4 | 184 | |
cbdc2292 HC |
185 | /* fake return stack for resume(), don't go back to schedule */ |
186 | frame->sf.gprs[9] = (unsigned long) frame; | |
1da177e4 LT |
187 | |
188 | /* Save access registers to new thread structure. */ | |
189 | save_access_regs(&p->thread.acrs[0]); | |
190 | ||
347a8dc3 | 191 | #ifndef CONFIG_64BIT |
cbdc2292 | 192 | /* |
1da177e4 LT |
193 | * save fprs to current->thread.fp_regs to merge them with |
194 | * the emulated registers and then copy the result to the child. | |
195 | */ | |
196 | save_fp_regs(¤t->thread.fp_regs); | |
197 | memcpy(&p->thread.fp_regs, ¤t->thread.fp_regs, | |
198 | sizeof(s390_fp_regs)); | |
1da177e4 LT |
199 | /* Set a new TLS ? */ |
200 | if (clone_flags & CLONE_SETTLS) | |
201 | p->thread.acrs[0] = regs->gprs[6]; | |
347a8dc3 | 202 | #else /* CONFIG_64BIT */ |
1da177e4 LT |
203 | /* Save the fpu registers to new thread structure. */ |
204 | save_fp_regs(&p->thread.fp_regs); | |
1da177e4 LT |
205 | /* Set a new TLS ? */ |
206 | if (clone_flags & CLONE_SETTLS) { | |
7757591a | 207 | if (is_compat_task()) { |
1da177e4 LT |
208 | p->thread.acrs[0] = (unsigned int) regs->gprs[6]; |
209 | } else { | |
210 | p->thread.acrs[0] = (unsigned int)(regs->gprs[6] >> 32); | |
211 | p->thread.acrs[1] = (unsigned int) regs->gprs[6]; | |
212 | } | |
213 | } | |
347a8dc3 | 214 | #endif /* CONFIG_64BIT */ |
1da177e4 LT |
215 | /* start new process with ar4 pointing to the correct address space */ |
216 | p->thread.mm_segment = get_fs(); | |
cbdc2292 | 217 | /* Don't copy debug registers */ |
5e9a2692 MS |
218 | memset(&p->thread.per_user, 0, sizeof(p->thread.per_user)); |
219 | memset(&p->thread.per_event, 0, sizeof(p->thread.per_event)); | |
f8d5faf7 | 220 | clear_tsk_thread_flag(p, TIF_SINGLE_STEP); |
5e9a2692 | 221 | clear_tsk_thread_flag(p, TIF_PER_TRAP); |
5168ce2c HC |
222 | /* Initialize per thread user and system timer values */ |
223 | ti = task_thread_info(p); | |
224 | ti->user_timer = 0; | |
225 | ti->system_timer = 0; | |
cbdc2292 | 226 | return 0; |
1da177e4 LT |
227 | } |
228 | ||
26689452 | 229 | SYSCALL_DEFINE0(fork) |
1da177e4 | 230 | { |
03ff9a23 MS |
231 | struct pt_regs *regs = task_pt_regs(current); |
232 | return do_fork(SIGCHLD, regs->gprs[15], regs, 0, NULL, NULL); | |
1da177e4 LT |
233 | } |
234 | ||
2d70ca23 HC |
235 | SYSCALL_DEFINE4(clone, unsigned long, newsp, unsigned long, clone_flags, |
236 | int __user *, parent_tidptr, int __user *, child_tidptr) | |
1da177e4 | 237 | { |
03ff9a23 | 238 | struct pt_regs *regs = task_pt_regs(current); |
1da177e4 | 239 | |
03ff9a23 MS |
240 | if (!newsp) |
241 | newsp = regs->gprs[15]; | |
242 | return do_fork(clone_flags, newsp, regs, 0, | |
1da177e4 LT |
243 | parent_tidptr, child_tidptr); |
244 | } | |
245 | ||
246 | /* | |
247 | * This is trivial, and on the face of it looks like it | |
248 | * could equally well be done in user mode. | |
249 | * | |
250 | * Not so, for quite unobvious reasons - register pressure. | |
251 | * In user mode vfork() cannot have a stack frame, and if | |
252 | * done by calling the "clone()" system call directly, you | |
253 | * do not have enough call-clobbered registers to hold all | |
254 | * the information you need. | |
255 | */ | |
26689452 | 256 | SYSCALL_DEFINE0(vfork) |
1da177e4 | 257 | { |
03ff9a23 | 258 | struct pt_regs *regs = task_pt_regs(current); |
1da177e4 | 259 | return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, |
03ff9a23 MS |
260 | regs->gprs[15], regs, 0, NULL, NULL); |
261 | } | |
262 | ||
263 | asmlinkage void execve_tail(void) | |
264 | { | |
03ff9a23 MS |
265 | current->thread.fp_regs.fpc = 0; |
266 | if (MACHINE_HAS_IEEE) | |
267 | asm volatile("sfpc %0,%0" : : "d" (0)); | |
1da177e4 LT |
268 | } |
269 | ||
270 | /* | |
271 | * sys_execve() executes a new program. | |
272 | */ | |
d7627467 DH |
273 | SYSCALL_DEFINE3(execve, const char __user *, name, |
274 | const char __user *const __user *, argv, | |
275 | const char __user *const __user *, envp) | |
1da177e4 | 276 | { |
03ff9a23 MS |
277 | struct pt_regs *regs = task_pt_regs(current); |
278 | char *filename; | |
3e86a8c6 | 279 | long rc; |
03ff9a23 | 280 | |
3e86a8c6 HC |
281 | filename = getname(name); |
282 | rc = PTR_ERR(filename); | |
283 | if (IS_ERR(filename)) | |
284 | return rc; | |
285 | rc = do_execve(filename, argv, envp, regs); | |
286 | if (rc) | |
03ff9a23 | 287 | goto out; |
03ff9a23 | 288 | execve_tail(); |
3e86a8c6 | 289 | rc = regs->gprs[2]; |
1da177e4 | 290 | out: |
3e86a8c6 HC |
291 | putname(filename); |
292 | return rc; | |
1da177e4 LT |
293 | } |
294 | ||
1da177e4 LT |
295 | /* |
296 | * fill in the FPU structure for a core dump. | |
297 | */ | |
298 | int dump_fpu (struct pt_regs * regs, s390_fp_regs *fpregs) | |
299 | { | |
347a8dc3 | 300 | #ifndef CONFIG_64BIT |
cbdc2292 | 301 | /* |
1da177e4 LT |
302 | * save fprs to current->thread.fp_regs to merge them with |
303 | * the emulated registers and then copy the result to the dump. | |
304 | */ | |
305 | save_fp_regs(¤t->thread.fp_regs); | |
306 | memcpy(fpregs, ¤t->thread.fp_regs, sizeof(s390_fp_regs)); | |
347a8dc3 | 307 | #else /* CONFIG_64BIT */ |
1da177e4 | 308 | save_fp_regs(fpregs); |
347a8dc3 | 309 | #endif /* CONFIG_64BIT */ |
1da177e4 LT |
310 | return 1; |
311 | } | |
1485c5c8 | 312 | EXPORT_SYMBOL(dump_fpu); |
1da177e4 | 313 | |
1da177e4 LT |
314 | unsigned long get_wchan(struct task_struct *p) |
315 | { | |
316 | struct stack_frame *sf, *low, *high; | |
317 | unsigned long return_address; | |
318 | int count; | |
319 | ||
30af7120 | 320 | if (!p || p == current || p->state == TASK_RUNNING || !task_stack_page(p)) |
1da177e4 | 321 | return 0; |
30af7120 AV |
322 | low = task_stack_page(p); |
323 | high = (struct stack_frame *) task_pt_regs(p); | |
1da177e4 LT |
324 | sf = (struct stack_frame *) (p->thread.ksp & PSW_ADDR_INSN); |
325 | if (sf <= low || sf > high) | |
326 | return 0; | |
327 | for (count = 0; count < 16; count++) { | |
328 | sf = (struct stack_frame *) (sf->back_chain & PSW_ADDR_INSN); | |
329 | if (sf <= low || sf > high) | |
330 | return 0; | |
331 | return_address = sf->gprs[8] & PSW_ADDR_INSN; | |
332 | if (!in_sched_functions(return_address)) | |
333 | return return_address; | |
334 | } | |
335 | return 0; | |
336 | } | |
9887a1fc HC |
337 | |
338 | unsigned long arch_align_stack(unsigned long sp) | |
339 | { | |
340 | if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space) | |
341 | sp -= get_random_int() & ~PAGE_MASK; | |
342 | return sp & ~0xf; | |
343 | } | |
33519182 HC |
344 | |
345 | static inline unsigned long brk_rnd(void) | |
346 | { | |
347 | /* 8MB for 32bit, 1GB for 64bit */ | |
348 | if (is_32bit_task()) | |
349 | return (get_random_int() & 0x7ffUL) << PAGE_SHIFT; | |
350 | else | |
351 | return (get_random_int() & 0x3ffffUL) << PAGE_SHIFT; | |
352 | } | |
353 | ||
354 | unsigned long arch_randomize_brk(struct mm_struct *mm) | |
355 | { | |
356 | unsigned long ret = PAGE_ALIGN(mm->brk + brk_rnd()); | |
357 | ||
358 | if (ret < mm->brk) | |
359 | return mm->brk; | |
360 | return ret; | |
361 | } | |
d2c9dfcc HC |
362 | |
363 | unsigned long randomize_et_dyn(unsigned long base) | |
364 | { | |
365 | unsigned long ret = PAGE_ALIGN(base + brk_rnd()); | |
366 | ||
367 | if (!(current->flags & PF_RANDOMIZE)) | |
368 | return base; | |
369 | if (ret < base) | |
370 | return base; | |
371 | return ret; | |
372 | } |