patch-2_6_7-vs1_9_1_12
[linux-2.6.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/config.h>
19 #include <linux/compiler.h>
20 #include <linux/errno.h>
21 #include <linux/sched.h>
22 #include <linux/kernel.h>
23 #include <linux/mm.h>
24 #include <linux/smp.h>
25 #include <linux/smp_lock.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
38 #include <asm/uaccess.h>
39 #include <asm/pgtable.h>
40 #include <asm/system.h>
41 #include <asm/io.h>
42 #include <asm/processor.h>
43 #include <asm/irq.h>
44 #if defined(CONFIG_VIRT_TIMER) || defined (CONFIG_NO_IDLE_HZ)
45 #include <asm/timer.h>
46 #endif
47
48 asmlinkage void ret_from_fork(void) __asm__("ret_from_fork");
49
50 /*
51  * Return saved PC of a blocked thread. used in kernel/sched.
52  * resume in entry.S does not create a new stack frame, it
53  * just stores the registers %r6-%r15 to the frame given by
54  * schedule. We want to return the address of the caller of
55  * schedule, so we have to walk the backchain one time to
56  * find the frame schedule() store its return address.
57  */
58 unsigned long thread_saved_pc(struct task_struct *tsk)
59 {
60         unsigned long bc;
61
62         bc = *((unsigned long *) tsk->thread.ksp);
63 #ifndef CONFIG_ARCH_S390X
64         return *((unsigned long *) (bc+56));
65 #else
66         return *((unsigned long *) (bc+112));
67 #endif
68 }
69
70 /*
71  * The idle loop on a S390...
72  */
73
74 void default_idle(void)
75 {
76         psw_t wait_psw;
77         unsigned long reg;
78
79         local_irq_disable();
80         if (need_resched()) {
81                 local_irq_enable();
82                 schedule();
83                 return;
84         }
85
86 #if defined(CONFIG_VIRT_TIMER) || defined (CONFIG_NO_IDLE_HZ)
87         /*
88          * hook to stop timers that should not tick while CPU is idle
89          */
90         if (stop_timers()) {
91                 local_irq_enable();
92                 return;
93         }
94 #endif
95
96         /* 
97          * Wait for external, I/O or machine check interrupt and
98          * switch off machine check bit after the wait has ended.
99          */
100         wait_psw.mask = PSW_KERNEL_BITS | PSW_MASK_MCHECK | PSW_MASK_WAIT |
101                 PSW_MASK_IO | PSW_MASK_EXT;
102 #ifndef CONFIG_ARCH_S390X
103         asm volatile (
104                 "    basr %0,0\n"
105                 "0:  la   %0,1f-0b(%0)\n"
106                 "    st   %0,4(%1)\n"
107                 "    oi   4(%1),0x80\n"
108                 "    lpsw 0(%1)\n"
109                 "1:  la   %0,2f-1b(%0)\n"
110                 "    st   %0,4(%1)\n"
111                 "    oi   4(%1),0x80\n"
112                 "    ni   1(%1),0xf9\n"
113                 "    lpsw 0(%1)\n"
114                 "2:"
115                 : "=&a" (reg) : "a" (&wait_psw) : "memory", "cc" );
116 #else /* CONFIG_ARCH_S390X */
117         asm volatile (
118                 "    larl  %0,0f\n"
119                 "    stg   %0,8(%1)\n"
120                 "    lpswe 0(%1)\n"
121                 "0:  larl  %0,1f\n"
122                 "    stg   %0,8(%1)\n"
123                 "    ni    1(%1),0xf9\n"
124                 "    lpswe 0(%1)\n"
125                 "1:"
126                 : "=&a" (reg) : "a" (&wait_psw) : "memory", "cc" );
127 #endif /* CONFIG_ARCH_S390X */
128 }
129
130 int cpu_idle(void)
131 {
132         for (;;)
133                 default_idle();
134         return 0;
135 }
136
137 void show_regs(struct pt_regs *regs)
138 {
139         struct task_struct *tsk = current;
140
141         printk("CPU:    %d    %s\n", tsk->thread_info->cpu, print_tainted());
142         printk("Process %s (pid: %d, task: %p, ksp: %p)\n",
143                current->comm, current->pid, (void *) tsk,
144                (void *) tsk->thread.ksp);
145
146         show_registers(regs);
147         /* Show stack backtrace if pt_regs is from kernel mode */
148         if (!(regs->psw.mask & PSW_MASK_PSTATE))
149                 show_trace(0,(unsigned long *) regs->gprs[15]);
150 }
151
152 extern void kernel_thread_starter(void);
153
154 #ifndef CONFIG_ARCH_S390X
155
156 __asm__(".align 4\n"
157         "kernel_thread_starter:\n"
158         "    l     15,0(8)\n"
159         "    sr    15,7\n"
160         "    stosm 24(15),3\n"
161         "    lr    2,10\n"
162         "    basr  14,9\n"
163         "    sr    2,2\n"
164         "    br    11\n");
165
166 #else /* CONFIG_ARCH_S390X */
167
168 __asm__(".align 4\n"
169         "kernel_thread_starter:\n"
170         "    lg    15,0(8)\n"
171         "    sgr   15,7\n"
172         "    stosm 48(15),3\n"
173         "    lgr   2,10\n"
174         "    basr  14,9\n"
175         "    sgr   2,2\n"
176         "    br    11\n");
177
178 #endif /* CONFIG_ARCH_S390X */
179
180 int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
181 {
182         struct pt_regs regs;
183
184         memset(&regs, 0, sizeof(regs));
185         regs.psw.mask = PSW_KERNEL_BITS;
186         regs.psw.addr = (unsigned long) kernel_thread_starter | PSW_ADDR_AMODE;
187         regs.gprs[7] = STACK_FRAME_OVERHEAD + sizeof(struct pt_regs);
188         regs.gprs[8] = __LC_KERNEL_STACK;
189         regs.gprs[9] = (unsigned long) fn;
190         regs.gprs[10] = (unsigned long) arg;
191         regs.gprs[11] = (unsigned long) do_exit;
192         regs.orig_gpr2 = -1;
193
194         /* Ok, create the new process.. */
195         return do_fork(flags | CLONE_VM | CLONE_UNTRACED,
196                        0, &regs, 0, NULL, NULL);
197 }
198
199 /*
200  * Free current thread data structures etc..
201  */
202 void exit_thread(void)
203 {
204 }
205
206 void flush_thread(void)
207 {
208
209         current->used_math = 0;
210         clear_tsk_thread_flag(current, TIF_USEDFPU);
211 }
212
213 void release_thread(struct task_struct *dead_task)
214 {
215 }
216
217 int copy_thread(int nr, unsigned long clone_flags, unsigned long new_stackp,
218         unsigned long unused,
219         struct task_struct * p, struct pt_regs * regs)
220 {
221         struct stack_frame
222           {
223             unsigned long back_chain;
224             unsigned long eos;
225             unsigned long glue1;
226             unsigned long glue2;
227             unsigned long scratch[2];
228             unsigned long gprs[10];    /* gprs 6 -15                       */
229             unsigned int  fprs[4];     /* fpr 4 and 6                      */
230             unsigned int  empty[4];
231             struct pt_regs childregs;
232           } *frame;
233
234         frame = ((struct stack_frame *)
235                  (THREAD_SIZE + (unsigned long) p->thread_info)) - 1;
236         p->thread.ksp = (unsigned long) frame;
237         p->set_child_tid = p->clear_child_tid = NULL;
238         /* Store access registers to kernel stack of new process. */
239         frame->childregs = *regs;
240         frame->childregs.gprs[2] = 0;   /* child returns 0 on fork. */
241         frame->childregs.gprs[15] = new_stackp;
242         frame->back_chain = frame->eos = 0;
243
244         /* new return point is ret_from_fork */
245         frame->gprs[8] = (unsigned long) ret_from_fork;
246
247         /* fake return stack for resume(), don't go back to schedule */
248         frame->gprs[9] = (unsigned long) frame;
249
250         /* Save access registers to new thread structure. */
251         save_access_regs(&p->thread.acrs[0]);
252
253 #ifndef CONFIG_ARCH_S390X
254         /*
255          * save fprs to current->thread.fp_regs to merge them with
256          * the emulated registers and then copy the result to the child.
257          */
258         save_fp_regs(&current->thread.fp_regs);
259         memcpy(&p->thread.fp_regs, &current->thread.fp_regs,
260                sizeof(s390_fp_regs));
261         p->thread.user_seg = __pa((unsigned long) p->mm->pgd) | _SEGMENT_TABLE;
262         /* Set a new TLS ?  */
263         if (clone_flags & CLONE_SETTLS)
264                 p->thread.acrs[0] = regs->gprs[6];
265 #else /* CONFIG_ARCH_S390X */
266         /* Save the fpu registers to new thread structure. */
267         save_fp_regs(&p->thread.fp_regs);
268         p->thread.user_seg = __pa((unsigned long) p->mm->pgd) | _REGION_TABLE;
269         /* Set a new TLS ?  */
270         if (clone_flags & CLONE_SETTLS) {
271                 if (test_thread_flag(TIF_31BIT)) {
272                         p->thread.acrs[0] = (unsigned int) regs->gprs[6];
273                 } else {
274                         p->thread.acrs[0] = (unsigned int)(regs->gprs[6] >> 32);
275                         p->thread.acrs[1] = (unsigned int) regs->gprs[6];
276                 }
277         }
278 #endif /* CONFIG_ARCH_S390X */
279         /* start new process with ar4 pointing to the correct address space */
280         p->thread.mm_segment = get_fs();
281         /* Don't copy debug registers */
282         memset(&p->thread.per_info,0,sizeof(p->thread.per_info));
283
284         return 0;
285 }
286
287 asmlinkage long sys_fork(struct pt_regs regs)
288 {
289         return do_fork(SIGCHLD, regs.gprs[15], &regs, 0, NULL, NULL);
290 }
291
292 asmlinkage long sys_clone(struct pt_regs regs)
293 {
294         unsigned long clone_flags;
295         unsigned long newsp;
296         int __user *parent_tidptr, *child_tidptr;
297
298         clone_flags = regs.gprs[3];
299         newsp = regs.orig_gpr2;
300         parent_tidptr = (int __user *) regs.gprs[4];
301         child_tidptr = (int __user *) regs.gprs[5];
302         if (!newsp)
303                 newsp = regs.gprs[15];
304         return do_fork(clone_flags & ~CLONE_IDLETASK, newsp, &regs, 0,
305                        parent_tidptr, child_tidptr);
306 }
307
308 /*
309  * This is trivial, and on the face of it looks like it
310  * could equally well be done in user mode.
311  *
312  * Not so, for quite unobvious reasons - register pressure.
313  * In user mode vfork() cannot have a stack frame, and if
314  * done by calling the "clone()" system call directly, you
315  * do not have enough call-clobbered registers to hold all
316  * the information you need.
317  */
318 asmlinkage long sys_vfork(struct pt_regs regs)
319 {
320         return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD,
321                        regs.gprs[15], &regs, 0, NULL, NULL);
322 }
323
324 /*
325  * sys_execve() executes a new program.
326  */
327 asmlinkage long sys_execve(struct pt_regs regs)
328 {
329         int error;
330         char * filename;
331
332         filename = getname((char __user *) regs.orig_gpr2);
333         error = PTR_ERR(filename);
334         if (IS_ERR(filename))
335                 goto out;
336         error = do_execve(filename, (char __user * __user *) regs.gprs[3],
337                           (char __user * __user *) regs.gprs[4], &regs);
338         if (error == 0) {
339                 current->ptrace &= ~PT_DTRACE;
340                 current->thread.fp_regs.fpc = 0;
341                 if (MACHINE_HAS_IEEE)
342                         asm volatile("sfpc %0,%0" : : "d" (0));
343         }
344         putname(filename);
345 out:
346         return error;
347 }
348
349
350 /*
351  * fill in the FPU structure for a core dump.
352  */
353 int dump_fpu (struct pt_regs * regs, s390_fp_regs *fpregs)
354 {
355 #ifndef CONFIG_ARCH_S390X
356         /*
357          * save fprs to current->thread.fp_regs to merge them with
358          * the emulated registers and then copy the result to the dump.
359          */
360         save_fp_regs(&current->thread.fp_regs);
361         memcpy(fpregs, &current->thread.fp_regs, sizeof(s390_fp_regs));
362 #else /* CONFIG_ARCH_S390X */
363         save_fp_regs(fpregs);
364 #endif /* CONFIG_ARCH_S390X */
365         return 1;
366 }
367
368 /*
369  * fill in the user structure for a core dump..
370  */
371 void dump_thread(struct pt_regs * regs, struct user * dump)
372 {
373
374 /* changed the size calculations - should hopefully work better. lbt */
375         dump->magic = CMAGIC;
376         dump->start_code = 0;
377         dump->start_stack = regs->gprs[15] & ~(PAGE_SIZE - 1);
378         dump->u_tsize = current->mm->end_code >> PAGE_SHIFT;
379         dump->u_dsize = (current->mm->brk + PAGE_SIZE - 1) >> PAGE_SHIFT;
380         dump->u_dsize -= dump->u_tsize;
381         dump->u_ssize = 0;
382         if (dump->start_stack < TASK_SIZE)
383                 dump->u_ssize = (TASK_SIZE - dump->start_stack) >> PAGE_SHIFT;
384         memcpy(&dump->regs, regs, sizeof(s390_regs));
385         dump_fpu (regs, &dump->regs.fp_regs);
386         dump->regs.per_info = current->thread.per_info;
387 }
388
389 unsigned long get_wchan(struct task_struct *p)
390 {
391         unsigned long r14, r15, bc;
392         unsigned long stack_page;
393         int count = 0;
394         if (!p || p == current || p->state == TASK_RUNNING)
395                 return 0;
396         stack_page = (unsigned long) p->thread_info;
397         r15 = p->thread.ksp;
398         if (!stack_page || r15 < stack_page ||
399             r15 >= THREAD_SIZE - sizeof(unsigned long) + stack_page)
400                 return 0;
401         bc = (*(unsigned long *) r15) & PSW_ADDR_INSN;
402         do {
403                 if (bc < stack_page ||
404                     bc >= THREAD_SIZE - sizeof(unsigned long) + stack_page)
405                         return 0;
406 #ifndef CONFIG_ARCH_S390X
407                 r14 = (*(unsigned long *) (bc+56)) & PSW_ADDR_INSN;
408 #else
409                 r14 = *(unsigned long *) (bc+112);
410 #endif
411                 if (!in_sched_functions(r14))
412                         return r14;
413                 bc = (*(unsigned long *) bc) & PSW_ADDR_INSN;
414         } while (count++ < 16);
415         return 0;
416 }
417