[S390] cputime: initialize per thread timer values on fork
[deliverable/linux.git] / arch / s390 / kernel / process.c
CommitLineData
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
LT
18#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>
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>
5c699714 30#include <linux/utsname.h>
5a62b192 31#include <linux/tick.h>
a806170e 32#include <linux/elfcore.h>
6f430924 33#include <linux/kernel_stat.h>
26689452 34#include <linux/syscalls.h>
1da177e4
LT
35#include <asm/uaccess.h>
36#include <asm/pgtable.h>
37#include <asm/system.h>
38#include <asm/io.h>
39#include <asm/processor.h>
40#include <asm/irq.h>
41#include <asm/timer.h>
a806170e 42#include "entry.h"
1da177e4 43
94c12cc7 44asmlinkage void ret_from_fork(void) asm ("ret_from_fork");
1da177e4
LT
45
46/*
47 * Return saved PC of a blocked thread. used in kernel/sched.
48 * resume in entry.S does not create a new stack frame, it
49 * just stores the registers %r6-%r15 to the frame given by
50 * schedule. We want to return the address of the caller of
51 * schedule, so we have to walk the backchain one time to
52 * find the frame schedule() store its return address.
53 */
54unsigned long thread_saved_pc(struct task_struct *tsk)
55{
eb33c190 56 struct stack_frame *sf, *low, *high;
1da177e4 57
eb33c190
HC
58 if (!tsk || !task_stack_page(tsk))
59 return 0;
60 low = task_stack_page(tsk);
61 high = (struct stack_frame *) task_pt_regs(tsk);
62 sf = (struct stack_frame *) (tsk->thread.ksp & PSW_ADDR_INSN);
63 if (sf <= low || sf > high)
64 return 0;
65 sf = (struct stack_frame *) (sf->back_chain & PSW_ADDR_INSN);
66 if (sf <= low || sf > high)
67 return 0;
1da177e4
LT
68 return sf->gprs[8];
69}
70
77fa2245 71extern void s390_handle_mcck(void);
1da177e4
LT
72/*
73 * The idle loop on a S390...
74 */
cdb04527 75static void default_idle(void)
1da177e4 76{
64c7c8f8 77 /* CPU is going idle. */
1da177e4 78 local_irq_disable();
64c7c8f8 79 if (need_resched()) {
1da177e4 80 local_irq_enable();
64c7c8f8
NP
81 return;
82 }
1da177e4 83#ifdef CONFIG_HOTPLUG_CPU
43ca5c3a 84 if (cpu_is_offline(smp_processor_id())) {
1fca251f 85 preempt_enable_no_resched();
1da177e4 86 cpu_die();
1fca251f 87 }
1da177e4 88#endif
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
105void 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
1da177e4
LT
118extern void kernel_thread_starter(void);
119
94c12cc7
MS
120asm(
121 ".align 4\n"
1da177e4
LT
122 "kernel_thread_starter:\n"
123 " la 2,0(10)\n"
124 " basr 14,9\n"
125 " la 2,0\n"
126 " br 11\n");
127
128int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
129{
130 struct pt_regs regs;
131
132 memset(&regs, 0, sizeof(regs));
c1821c2e 133 regs.psw.mask = psw_kernel_bits | PSW_MASK_IO | PSW_MASK_EXT;
1da177e4
LT
134 regs.psw.addr = (unsigned long) kernel_thread_starter | PSW_ADDR_AMODE;
135 regs.gprs[9] = (unsigned long) fn;
136 regs.gprs[10] = (unsigned long) arg;
137 regs.gprs[11] = (unsigned long) do_exit;
138 regs.orig_gpr2 = -1;
139
140 /* Ok, create the new process.. */
141 return do_fork(flags | CLONE_VM | CLONE_UNTRACED,
142 0, &regs, 0, NULL, NULL);
143}
144
145/*
146 * Free current thread data structures etc..
147 */
148void exit_thread(void)
149{
150}
151
152void flush_thread(void)
153{
154 clear_used_math();
155 clear_tsk_thread_flag(current, TIF_USEDFPU);
156}
157
158void release_thread(struct task_struct *dead_task)
159{
160}
161
162int copy_thread(int nr, 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(&current->thread.fp_regs);
196 memcpy(&p->thread.fp_regs, &current->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) {
206 if (test_thread_flag(TIF_31BIT)) {
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));
5168ce2c
HC
218 /* Initialize per thread user and system timer values */
219 ti = task_thread_info(p);
220 ti->user_timer = 0;
221 ti->system_timer = 0;
cbdc2292 222 return 0;
1da177e4
LT
223}
224
26689452 225SYSCALL_DEFINE0(fork)
1da177e4 226{
03ff9a23
MS
227 struct pt_regs *regs = task_pt_regs(current);
228 return do_fork(SIGCHLD, regs->gprs[15], regs, 0, NULL, NULL);
1da177e4
LT
229}
230
26689452 231SYSCALL_DEFINE0(clone)
1da177e4 232{
03ff9a23
MS
233 struct pt_regs *regs = task_pt_regs(current);
234 unsigned long clone_flags;
235 unsigned long newsp;
1da177e4
LT
236 int __user *parent_tidptr, *child_tidptr;
237
03ff9a23
MS
238 clone_flags = regs->gprs[3];
239 newsp = regs->orig_gpr2;
240 parent_tidptr = (int __user *) regs->gprs[4];
241 child_tidptr = (int __user *) regs->gprs[5];
242 if (!newsp)
243 newsp = regs->gprs[15];
244 return do_fork(clone_flags, newsp, regs, 0,
1da177e4
LT
245 parent_tidptr, child_tidptr);
246}
247
248/*
249 * This is trivial, and on the face of it looks like it
250 * could equally well be done in user mode.
251 *
252 * Not so, for quite unobvious reasons - register pressure.
253 * In user mode vfork() cannot have a stack frame, and if
254 * done by calling the "clone()" system call directly, you
255 * do not have enough call-clobbered registers to hold all
256 * the information you need.
257 */
26689452 258SYSCALL_DEFINE0(vfork)
1da177e4 259{
03ff9a23 260 struct pt_regs *regs = task_pt_regs(current);
1da177e4 261 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD,
03ff9a23
MS
262 regs->gprs[15], regs, 0, NULL, NULL);
263}
264
265asmlinkage void execve_tail(void)
266{
267 task_lock(current);
268 current->ptrace &= ~PT_DTRACE;
269 task_unlock(current);
270 current->thread.fp_regs.fpc = 0;
271 if (MACHINE_HAS_IEEE)
272 asm volatile("sfpc %0,%0" : : "d" (0));
1da177e4
LT
273}
274
275/*
276 * sys_execve() executes a new program.
277 */
26689452 278SYSCALL_DEFINE0(execve)
1da177e4 279{
03ff9a23
MS
280 struct pt_regs *regs = task_pt_regs(current);
281 char *filename;
282 unsigned long result;
283 int rc;
284
285 filename = getname((char __user *) regs->orig_gpr2);
286 if (IS_ERR(filename)) {
287 result = PTR_ERR(filename);
288 goto out;
1da177e4 289 }
03ff9a23
MS
290 rc = do_execve(filename, (char __user * __user *) regs->gprs[3],
291 (char __user * __user *) regs->gprs[4], regs);
292 if (rc) {
293 result = rc;
294 goto out_putname;
295 }
296 execve_tail();
297 result = regs->gprs[2];
298out_putname:
299 putname(filename);
1da177e4 300out:
03ff9a23 301 return result;
1da177e4
LT
302}
303
1da177e4
LT
304/*
305 * fill in the FPU structure for a core dump.
306 */
307int dump_fpu (struct pt_regs * regs, s390_fp_regs *fpregs)
308{
347a8dc3 309#ifndef CONFIG_64BIT
cbdc2292 310 /*
1da177e4
LT
311 * save fprs to current->thread.fp_regs to merge them with
312 * the emulated registers and then copy the result to the dump.
313 */
314 save_fp_regs(&current->thread.fp_regs);
315 memcpy(fpregs, &current->thread.fp_regs, sizeof(s390_fp_regs));
347a8dc3 316#else /* CONFIG_64BIT */
1da177e4 317 save_fp_regs(fpregs);
347a8dc3 318#endif /* CONFIG_64BIT */
1da177e4
LT
319 return 1;
320}
321
1da177e4
LT
322unsigned long get_wchan(struct task_struct *p)
323{
324 struct stack_frame *sf, *low, *high;
325 unsigned long return_address;
326 int count;
327
30af7120 328 if (!p || p == current || p->state == TASK_RUNNING || !task_stack_page(p))
1da177e4 329 return 0;
30af7120
AV
330 low = task_stack_page(p);
331 high = (struct stack_frame *) task_pt_regs(p);
1da177e4
LT
332 sf = (struct stack_frame *) (p->thread.ksp & PSW_ADDR_INSN);
333 if (sf <= low || sf > high)
334 return 0;
335 for (count = 0; count < 16; count++) {
336 sf = (struct stack_frame *) (sf->back_chain & PSW_ADDR_INSN);
337 if (sf <= low || sf > high)
338 return 0;
339 return_address = sf->gprs[8] & PSW_ADDR_INSN;
340 if (!in_sched_functions(return_address))
341 return return_address;
342 }
343 return 0;
344}
This page took 0.422146 seconds and 5 git commands to generate.