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