[S390] Add per-cpu idle time / idle count sysfs attributes.
[pandora-kernel.git] / arch / s390 / kernel / process.c
1 /*
2  *  arch/s390/kernel/process.c
3  *
4  *  S390 version
5  *    Copyright (C) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
6  *    Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
7  *               Hartmut Penner (hp@de.ibm.com),
8  *               Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com),
9  *
10  *  Derived from "arch/i386/kernel/process.c"
11  *    Copyright (C) 1995, Linus Torvalds
12  */
13
14 /*
15  * This file handles the architecture-dependent parts of process handling..
16  */
17
18 #include <linux/compiler.h>
19 #include <linux/cpu.h>
20 #include <linux/errno.h>
21 #include <linux/sched.h>
22 #include <linux/kernel.h>
23 #include <linux/mm.h>
24 #include <linux/fs.h>
25 #include <linux/smp.h>
26 #include <linux/stddef.h>
27 #include <linux/unistd.h>
28 #include <linux/ptrace.h>
29 #include <linux/slab.h>
30 #include <linux/vmalloc.h>
31 #include <linux/user.h>
32 #include <linux/a.out.h>
33 #include <linux/interrupt.h>
34 #include <linux/delay.h>
35 #include <linux/reboot.h>
36 #include <linux/init.h>
37 #include <linux/module.h>
38 #include <linux/notifier.h>
39
40 #include <asm/uaccess.h>
41 #include <asm/pgtable.h>
42 #include <asm/system.h>
43 #include <asm/io.h>
44 #include <asm/processor.h>
45 #include <asm/irq.h>
46 #include <asm/timer.h>
47 #include <asm/cpu.h>
48
49 asmlinkage void ret_from_fork(void) asm ("ret_from_fork");
50
51 /*
52  * Return saved PC of a blocked thread. used in kernel/sched.
53  * resume in entry.S does not create a new stack frame, it
54  * just stores the registers %r6-%r15 to the frame given by
55  * schedule. We want to return the address of the caller of
56  * schedule, so we have to walk the backchain one time to
57  * find the frame schedule() store its return address.
58  */
59 unsigned long thread_saved_pc(struct task_struct *tsk)
60 {
61         struct stack_frame *sf, *low, *high;
62
63         if (!tsk || !task_stack_page(tsk))
64                 return 0;
65         low = task_stack_page(tsk);
66         high = (struct stack_frame *) task_pt_regs(tsk);
67         sf = (struct stack_frame *) (tsk->thread.ksp & PSW_ADDR_INSN);
68         if (sf <= low || sf > high)
69                 return 0;
70         sf = (struct stack_frame *) (sf->back_chain & PSW_ADDR_INSN);
71         if (sf <= low || sf > high)
72                 return 0;
73         return sf->gprs[8];
74 }
75
76 /*
77  * Need to know about CPUs going idle?
78  */
79 static ATOMIC_NOTIFIER_HEAD(idle_chain);
80
81 int register_idle_notifier(struct notifier_block *nb)
82 {
83         return atomic_notifier_chain_register(&idle_chain, nb);
84 }
85 EXPORT_SYMBOL(register_idle_notifier);
86
87 int unregister_idle_notifier(struct notifier_block *nb)
88 {
89         return atomic_notifier_chain_unregister(&idle_chain, nb);
90 }
91 EXPORT_SYMBOL(unregister_idle_notifier);
92
93 void do_monitor_call(struct pt_regs *regs, long interruption_code)
94 {
95         struct s390_idle_data *idle;
96
97         idle = &__get_cpu_var(s390_idle);
98         spin_lock(&idle->lock);
99         idle->idle_time += get_clock() - idle->idle_enter;
100         idle->in_idle = 0;
101         spin_unlock(&idle->lock);
102
103         /* disable monitor call class 0 */
104         __ctl_clear_bit(8, 15);
105
106         atomic_notifier_call_chain(&idle_chain, S390_CPU_NOT_IDLE,
107                                    (void *)(long) smp_processor_id());
108 }
109
110 extern void s390_handle_mcck(void);
111 /*
112  * The idle loop on a S390...
113  */
114 static void default_idle(void)
115 {
116         int cpu, rc;
117         struct s390_idle_data *idle;
118
119         /* CPU is going idle. */
120         cpu = smp_processor_id();
121
122         local_irq_disable();
123         if (need_resched()) {
124                 local_irq_enable();
125                 return;
126         }
127
128         rc = atomic_notifier_call_chain(&idle_chain,
129                                         S390_CPU_IDLE, (void *)(long) cpu);
130         if (rc != NOTIFY_OK && rc != NOTIFY_DONE)
131                 BUG();
132         if (rc != NOTIFY_OK) {
133                 local_irq_enable();
134                 return;
135         }
136
137         /* enable monitor call class 0 */
138         __ctl_set_bit(8, 15);
139
140 #ifdef CONFIG_HOTPLUG_CPU
141         if (cpu_is_offline(cpu)) {
142                 preempt_enable_no_resched();
143                 cpu_die();
144         }
145 #endif
146
147         local_mcck_disable();
148         if (test_thread_flag(TIF_MCCK_PENDING)) {
149                 local_mcck_enable();
150                 local_irq_enable();
151                 s390_handle_mcck();
152                 return;
153         }
154
155         idle = &__get_cpu_var(s390_idle);
156         spin_lock(&idle->lock);
157         idle->idle_count++;
158         idle->in_idle = 1;
159         idle->idle_enter = get_clock();
160         spin_unlock(&idle->lock);
161         trace_hardirqs_on();
162         /* Wait for external, I/O or machine check interrupt. */
163         __load_psw_mask(psw_kernel_bits | PSW_MASK_WAIT |
164                         PSW_MASK_IO | PSW_MASK_EXT);
165 }
166
167 void cpu_idle(void)
168 {
169         for (;;) {
170                 while (!need_resched())
171                         default_idle();
172
173                 preempt_enable_no_resched();
174                 schedule();
175                 preempt_disable();
176         }
177 }
178
179 void show_regs(struct pt_regs *regs)
180 {
181         struct task_struct *tsk = current;
182
183         printk("CPU:    %d    %s\n", task_thread_info(tsk)->cpu, print_tainted());
184         printk("Process %s (pid: %d, task: %p, ksp: %p)\n",
185                current->comm, task_pid_nr(current), (void *) tsk,
186                (void *) tsk->thread.ksp);
187
188         show_registers(regs);
189         /* Show stack backtrace if pt_regs is from kernel mode */
190         if (!(regs->psw.mask & PSW_MASK_PSTATE))
191                 show_trace(NULL, (unsigned long *) regs->gprs[15]);
192 }
193
194 extern void kernel_thread_starter(void);
195
196 asm(
197         ".align 4\n"
198         "kernel_thread_starter:\n"
199         "    la    2,0(10)\n"
200         "    basr  14,9\n"
201         "    la    2,0\n"
202         "    br    11\n");
203
204 int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
205 {
206         struct pt_regs regs;
207
208         memset(&regs, 0, sizeof(regs));
209         regs.psw.mask = psw_kernel_bits | PSW_MASK_IO | PSW_MASK_EXT;
210         regs.psw.addr = (unsigned long) kernel_thread_starter | PSW_ADDR_AMODE;
211         regs.gprs[9] = (unsigned long) fn;
212         regs.gprs[10] = (unsigned long) arg;
213         regs.gprs[11] = (unsigned long) do_exit;
214         regs.orig_gpr2 = -1;
215
216         /* Ok, create the new process.. */
217         return do_fork(flags | CLONE_VM | CLONE_UNTRACED,
218                        0, &regs, 0, NULL, NULL);
219 }
220
221 /*
222  * Free current thread data structures etc..
223  */
224 void exit_thread(void)
225 {
226 }
227
228 void flush_thread(void)
229 {
230         clear_used_math();
231         clear_tsk_thread_flag(current, TIF_USEDFPU);
232 }
233
234 void release_thread(struct task_struct *dead_task)
235 {
236 }
237
238 int copy_thread(int nr, unsigned long clone_flags, unsigned long new_stackp,
239         unsigned long unused,
240         struct task_struct * p, struct pt_regs * regs)
241 {
242         struct fake_frame
243           {
244             struct stack_frame sf;
245             struct pt_regs childregs;
246           } *frame;
247
248         frame = container_of(task_pt_regs(p), struct fake_frame, childregs);
249         p->thread.ksp = (unsigned long) frame;
250         /* Store access registers to kernel stack of new process. */
251         frame->childregs = *regs;
252         frame->childregs.gprs[2] = 0;   /* child returns 0 on fork. */
253         frame->childregs.gprs[15] = new_stackp;
254         frame->sf.back_chain = 0;
255
256         /* new return point is ret_from_fork */
257         frame->sf.gprs[8] = (unsigned long) ret_from_fork;
258
259         /* fake return stack for resume(), don't go back to schedule */
260         frame->sf.gprs[9] = (unsigned long) frame;
261
262         /* Save access registers to new thread structure. */
263         save_access_regs(&p->thread.acrs[0]);
264
265 #ifndef CONFIG_64BIT
266         /*
267          * save fprs to current->thread.fp_regs to merge them with
268          * the emulated registers and then copy the result to the child.
269          */
270         save_fp_regs(&current->thread.fp_regs);
271         memcpy(&p->thread.fp_regs, &current->thread.fp_regs,
272                sizeof(s390_fp_regs));
273         p->thread.user_seg = __pa((unsigned long) p->mm->pgd) | _SEGMENT_TABLE;
274         /* Set a new TLS ?  */
275         if (clone_flags & CLONE_SETTLS)
276                 p->thread.acrs[0] = regs->gprs[6];
277 #else /* CONFIG_64BIT */
278         /* Save the fpu registers to new thread structure. */
279         save_fp_regs(&p->thread.fp_regs);
280         p->thread.user_seg = __pa((unsigned long) p->mm->pgd) | _REGION_TABLE;
281         /* Set a new TLS ?  */
282         if (clone_flags & CLONE_SETTLS) {
283                 if (test_thread_flag(TIF_31BIT)) {
284                         p->thread.acrs[0] = (unsigned int) regs->gprs[6];
285                 } else {
286                         p->thread.acrs[0] = (unsigned int)(regs->gprs[6] >> 32);
287                         p->thread.acrs[1] = (unsigned int) regs->gprs[6];
288                 }
289         }
290 #endif /* CONFIG_64BIT */
291         /* start new process with ar4 pointing to the correct address space */
292         p->thread.mm_segment = get_fs();
293         /* Don't copy debug registers */
294         memset(&p->thread.per_info,0,sizeof(p->thread.per_info));
295
296         return 0;
297 }
298
299 asmlinkage long sys_fork(void)
300 {
301         struct pt_regs *regs = task_pt_regs(current);
302         return do_fork(SIGCHLD, regs->gprs[15], regs, 0, NULL, NULL);
303 }
304
305 asmlinkage long sys_clone(void)
306 {
307         struct pt_regs *regs = task_pt_regs(current);
308         unsigned long clone_flags;
309         unsigned long newsp;
310         int __user *parent_tidptr, *child_tidptr;
311
312         clone_flags = regs->gprs[3];
313         newsp = regs->orig_gpr2;
314         parent_tidptr = (int __user *) regs->gprs[4];
315         child_tidptr = (int __user *) regs->gprs[5];
316         if (!newsp)
317                 newsp = regs->gprs[15];
318         return do_fork(clone_flags, newsp, regs, 0,
319                        parent_tidptr, child_tidptr);
320 }
321
322 /*
323  * This is trivial, and on the face of it looks like it
324  * could equally well be done in user mode.
325  *
326  * Not so, for quite unobvious reasons - register pressure.
327  * In user mode vfork() cannot have a stack frame, and if
328  * done by calling the "clone()" system call directly, you
329  * do not have enough call-clobbered registers to hold all
330  * the information you need.
331  */
332 asmlinkage long sys_vfork(void)
333 {
334         struct pt_regs *regs = task_pt_regs(current);
335         return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD,
336                        regs->gprs[15], regs, 0, NULL, NULL);
337 }
338
339 asmlinkage void execve_tail(void)
340 {
341         task_lock(current);
342         current->ptrace &= ~PT_DTRACE;
343         task_unlock(current);
344         current->thread.fp_regs.fpc = 0;
345         if (MACHINE_HAS_IEEE)
346                 asm volatile("sfpc %0,%0" : : "d" (0));
347 }
348
349 /*
350  * sys_execve() executes a new program.
351  */
352 asmlinkage long sys_execve(void)
353 {
354         struct pt_regs *regs = task_pt_regs(current);
355         char *filename;
356         unsigned long result;
357         int rc;
358
359         filename = getname((char __user *) regs->orig_gpr2);
360         if (IS_ERR(filename)) {
361                 result = PTR_ERR(filename);
362                 goto out;
363         }
364         rc = do_execve(filename, (char __user * __user *) regs->gprs[3],
365                        (char __user * __user *) regs->gprs[4], regs);
366         if (rc) {
367                 result = rc;
368                 goto out_putname;
369         }
370         execve_tail();
371         result = regs->gprs[2];
372 out_putname:
373         putname(filename);
374 out:
375         return result;
376 }
377
378 /*
379  * fill in the FPU structure for a core dump.
380  */
381 int dump_fpu (struct pt_regs * regs, s390_fp_regs *fpregs)
382 {
383 #ifndef CONFIG_64BIT
384         /*
385          * save fprs to current->thread.fp_regs to merge them with
386          * the emulated registers and then copy the result to the dump.
387          */
388         save_fp_regs(&current->thread.fp_regs);
389         memcpy(fpregs, &current->thread.fp_regs, sizeof(s390_fp_regs));
390 #else /* CONFIG_64BIT */
391         save_fp_regs(fpregs);
392 #endif /* CONFIG_64BIT */
393         return 1;
394 }
395
396 unsigned long get_wchan(struct task_struct *p)
397 {
398         struct stack_frame *sf, *low, *high;
399         unsigned long return_address;
400         int count;
401
402         if (!p || p == current || p->state == TASK_RUNNING || !task_stack_page(p))
403                 return 0;
404         low = task_stack_page(p);
405         high = (struct stack_frame *) task_pt_regs(p);
406         sf = (struct stack_frame *) (p->thread.ksp & PSW_ADDR_INSN);
407         if (sf <= low || sf > high)
408                 return 0;
409         for (count = 0; count < 16; count++) {
410                 sf = (struct stack_frame *) (sf->back_chain & PSW_ADDR_INSN);
411                 if (sf <= low || sf > high)
412                         return 0;
413                 return_address = sf->gprs[8] & PSW_ADDR_INSN;
414                 if (!in_sched_functions(return_address))
415                         return return_address;
416         }
417         return 0;
418 }
419