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