Merge with Linux 2.4.0-test6-pre4.
[linux-2.6/linux-mips.git] / arch / s390 / kernel / process.c
blob6f6a5287b21e0afb3709ae5f0f28c7acff39ce02
1 /*
2 * arch/s390/kernel/process.c
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),
10 * Derived from "arch/i386/kernel/process.c"
11 * Copyright (C) 1995, Linus Torvalds
15 * This file handles the architecture-dependent parts of process handling..
18 #define __KERNEL_SYSCALLS__
19 #include <stdarg.h>
21 #include <linux/config.h>
22 #include <linux/errno.h>
23 #include <linux/sched.h>
24 #include <linux/kernel.h>
25 #include <linux/mm.h>
26 #include <linux/smp.h>
27 #include <linux/smp_lock.h>
28 #include <linux/stddef.h>
29 #include <linux/unistd.h>
30 #include <linux/ptrace.h>
31 #include <linux/malloc.h>
32 #include <linux/vmalloc.h>
33 #include <linux/user.h>
34 #include <linux/a.out.h>
35 #include <linux/interrupt.h>
36 #include <linux/delay.h>
37 #include <linux/reboot.h>
38 #include <linux/init.h>
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/misc390.h>
46 #include <asm/irq.h>
48 spinlock_t semaphore_wake_lock = SPIN_LOCK_UNLOCKED;
50 asmlinkage void ret_from_fork(void) __asm__("ret_from_fork");
53 * The idle loop on a S390...
56 static psw_t wait_psw;
58 int cpu_idle(void *unused)
60 /* endless idle loop with no priority at all */
61 init_idle();
62 current->nice = 20;
63 current->counter = -100;
64 wait_psw.mask = _WAIT_PSW_MASK;
65 wait_psw.addr = (unsigned long) &&idle_wakeup | 0x80000000L;
66 while(1) {
67 if (softirq_active(smp_processor_id()) &
68 softirq_mask(smp_processor_id())) {
69 do_softirq();
70 continue;
72 if (current->need_resched) {
73 schedule();
74 check_pgt_cache();
75 continue;
78 /* load wait psw */
79 asm volatile (
80 "lpsw %0"
81 : : "m" (wait_psw) );
82 idle_wakeup:
87 As all the register will only be made displayable to the root
88 user ( via printk ) or checking if the uid of the user is 0 from
89 the /proc filesystem please god this will be secure enough DJB.
90 The lines are given one at a time so as not to chew stack space in
91 printk on a crash & also for the proc filesystem when you get
92 0 returned you know you've got all the lines
95 int sprintf_regs(int line, char *buff, struct task_struct * task,
96 struct thread_struct *thread, struct pt_regs * regs)
98 int linelen=0;
99 int regno,chaincnt;
100 u32 backchain,prev_backchain,endchain;
102 enum
104 sp_linefeed,
105 sp_psw,
106 sp_ksp,
107 sp_gprs,
108 sp_gprs1,
109 sp_gprs2,
110 sp_gprs3,
111 sp_gprs4,
112 sp_acrs,
113 sp_acrs1,
114 sp_acrs2,
115 sp_acrs3,
116 sp_acrs4,
117 sp_kern_backchain,
118 sp_kern_backchain1
121 if(task)
122 thread = &task->thread;
123 if(thread)
124 regs = thread->regs;
125 switch (line) {
126 case sp_linefeed:
127 linelen=sprintf(buff,"\n");
128 break;
129 case sp_psw:
130 if(regs)
131 linelen = sprintf(buff,"User PSW: %08lx %08lx\n",
132 (unsigned long) regs->psw.mask,
133 (unsigned long) regs->psw.addr);
134 else
135 linelen = sprintf(buff,"pt_regs=NULL some info unavailable\n");
136 break;
137 case sp_ksp:
138 if (task)
139 linelen += sprintf(&buff[linelen],
140 "task: %08x ", (addr_t)task);
141 if (thread)
142 linelen += sprintf(&buff[linelen],
143 "thread: %08x ksp: %08x ",
144 (addr_t)thread,(addr_t)thread->ksp);
145 if (regs)
146 linelen += sprintf(&buff[linelen],
147 "pt_regs: %08x\n", (addr_t)regs);
148 break;
149 case sp_gprs:
150 if (regs)
151 linelen = sprintf(buff,"User GPRS:\n");
152 break;
153 case sp_gprs1 ... sp_gprs4:
154 if (regs) {
155 regno = (line-sp_gprs1)*4;
156 linelen = sprintf(buff,"%08x %08x %08x %08x\n",
157 regs->gprs[regno],
158 regs->gprs[regno+1],
159 regs->gprs[regno+2],
160 regs->gprs[regno+3]);
162 break;
163 case sp_acrs:
164 if (regs)
165 linelen = sprintf(buff,"User ACRS:\n");
166 break;
167 case sp_acrs1 ... sp_acrs4:
168 if (regs) {
169 regno = (line-sp_acrs1)*4;
170 linelen = sprintf(buff,"%08x %08x %08x %08x\n",
171 regs->acrs[regno],
172 regs->acrs[regno+1],
173 regs->acrs[regno+2],
174 regs->acrs[regno+3]);
176 break;
177 case sp_kern_backchain:
178 if (thread && thread->ksp && regs)
179 linelen = sprintf(buff,"Kernel BackChain CallChain BackChain CallChain\n");
180 break;
181 default:
182 if(thread && thread->ksp && regs) {
183 backchain = (thread->ksp & PSW_ADDR_MASK);
184 endchain = ((backchain & (-8192)) + 8192);
185 prev_backchain = backchain - 1;
186 line -= sp_kern_backchain1;
187 for (chaincnt = 0; ; chaincnt++) {
188 if ((backchain == 0) ||
189 (backchain >= endchain) ||
190 (chaincnt >= 8) ||
191 (prev_backchain >= backchain))
192 break;
193 if ((chaincnt >> 1) == line) {
194 linelen += sprintf(&buff[linelen],"%s%08x %08x ",
195 (chaincnt&1) ? "":" ",
196 backchain,*(u32 *)(backchain+56));
198 if ((chaincnt >> 1) > line)
199 break;
200 prev_backchain = backchain;
201 backchain = (*((u32 *)backchain)) & PSW_ADDR_MASK;
203 if (linelen)
204 linelen += sprintf(&buff[linelen],"\n");
207 return linelen;
211 void show_regs(struct task_struct *task, struct thread_struct *thread,
212 struct pt_regs *regs)
214 char buff[80];
215 int line;
217 for (line = 0; sprintf_regs(line,buff,task,thread,regs); line++)
218 printk(buff);
221 int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
223 int clone_arg = flags | CLONE_VM;
224 int retval;
226 __asm__ __volatile__(
227 " sr 2,2\n"
228 " lr 3,%1\n"
229 " l 4,%6\n" /* load kernel stack ptr of parent */
230 " svc %b2\n" /* Linux system call*/
231 " cl 4,%6\n" /* compare ksp's: child or parent ? */
232 " je 0f\n" /* parent - jump*/
233 " l 15,%6\n" /* fix kernel stack pointer*/
234 " ahi 15,%7\n"
235 " xc 0(96,15),0(15)\n" /* clear save area */
236 " lr 2,%4\n" /* load argument*/
237 " lr 14,%5\n" /* get fn-pointer*/
238 " basr 14,14\n" /* call fn*/
239 " svc %b3\n" /* Linux system call*/
240 "0: lr %0,2"
241 : "=a" (retval)
242 : "d" (clone_arg), "i" (__NR_clone), "i" (__NR_exit),
243 "d" (arg), "d" (fn), "i" (__LC_KERNEL_STACK) , "i" (-STACK_FRAME_OVERHEAD)
244 : "2", "3", "4" );
245 return retval;
249 * Free current thread data structures etc..
251 void exit_thread(void)
255 void flush_thread(void)
258 current->used_math = 0;
259 current->flags &= ~PF_USEDFPU;
262 void release_thread(struct task_struct *dead_task)
266 int copy_thread(int nr, unsigned long clone_flags, unsigned long new_stackp,
267 struct task_struct * p, struct pt_regs * regs)
269 struct stack_frame
271 unsigned long back_chain;
272 unsigned long eos;
273 unsigned long glue1;
274 unsigned long glue2;
275 unsigned long scratch[2];
276 unsigned long gprs[10]; /* gprs 6 -15 */
277 unsigned long fprs[4]; /* fpr 4 and 6 */
278 unsigned long empty[4];
279 #if CONFIG_REMOTE_DEBUG
280 gdb_pt_regs childregs;
281 #else
282 pt_regs childregs;
283 #endif
284 __u32 pgm_old_ilc; /* single step magic from entry.S */
285 __u32 pgm_svc_step;
286 } *frame;
288 frame = (struct stack_frame *) (2*PAGE_SIZE + (unsigned long) p) -1;
289 frame = (struct stack_frame *) (((unsigned long) frame)&-8L);
290 p->thread.regs = &frame->childregs;
291 p->thread.ksp = (unsigned long) frame;
292 frame->childregs = *regs;
293 frame->childregs.gprs[15] = new_stackp;
294 frame->eos = 0;
296 /* new return point is ret_from_sys_call */
297 frame->gprs[8] = ((unsigned long) &ret_from_fork) | 0x80000000;
299 /* fake return stack for resume(), don't go back to schedule */
300 frame->gprs[9] = (unsigned long) frame;
301 frame->pgm_svc_step = 0; /* Nope we aren't single stepping an svc */
302 /* save fprs, if used in last task */
303 save_fp_regs(&p->thread.fp_regs);
304 p->thread.user_seg = __pa((unsigned long) p->mm->pgd) | _SEGMENT_TABLE;
305 p->thread.fs = USER_DS;
306 /* Don't copy debug registers */
307 memset(&p->thread.per_info,0,sizeof(p->thread.per_info));
308 return 0;
311 asmlinkage int sys_fork(struct pt_regs regs)
313 int ret;
315 lock_kernel();
316 ret = do_fork(SIGCHLD, regs.gprs[15], &regs);
317 unlock_kernel();
318 return ret;
321 asmlinkage int sys_clone(struct pt_regs regs)
323 unsigned long clone_flags;
324 unsigned long newsp;
325 int ret;
327 lock_kernel();
328 clone_flags = regs.gprs[3];
329 newsp = regs.gprs[2];
330 if (!newsp)
331 newsp = regs.gprs[15];
332 ret = do_fork(clone_flags, newsp, &regs);
333 unlock_kernel();
334 return ret;
338 * This is trivial, and on the face of it looks like it
339 * could equally well be done in user mode.
341 * Not so, for quite unobvious reasons - register pressure.
342 * In user mode vfork() cannot have a stack frame, and if
343 * done by calling the "clone()" system call directly, you
344 * do not have enough call-clobbered registers to hold all
345 * the information you need.
347 asmlinkage int sys_vfork(struct pt_regs regs)
349 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD,
350 regs.gprs[15], &regs);
354 * sys_execve() executes a new program.
356 asmlinkage int sys_execve(struct pt_regs regs)
358 int error;
359 char * filename;
361 filename = getname((char *) regs.orig_gpr2);
362 error = PTR_ERR(filename);
363 if (IS_ERR(filename))
364 goto out;
365 error = do_execve(filename, (char **) regs.gprs[3], (char **) regs.gprs[4], &regs);
366 if (error == 0)
367 current->flags &= ~PF_DTRACE;
368 putname(filename);
369 out:
370 return error;
375 * fill in the FPU structure for a core dump.
377 int dump_fpu (struct pt_regs * regs, s390_fp_regs *fpregs)
379 save_fp_regs(fpregs);
380 return 1;
384 * fill in the user structure for a core dump..
386 void dump_thread(struct pt_regs * regs, struct user * dump)
389 /* changed the size calculations - should hopefully work better. lbt */
390 dump->magic = CMAGIC;
391 dump->start_code = 0;
392 dump->start_stack = regs->gprs[15] & ~(PAGE_SIZE - 1);
393 dump->u_tsize = ((unsigned long) current->mm->end_code) >> PAGE_SHIFT;
394 dump->u_dsize = ((unsigned long) (current->mm->brk + (PAGE_SIZE-1))) >> PAGE_SHIFT;
395 dump->u_dsize -= dump->u_tsize;
396 dump->u_ssize = 0;
397 if (dump->start_stack < TASK_SIZE)
398 dump->u_ssize = ((unsigned long) (TASK_SIZE - dump->start_stack)) >> PAGE_SHIFT;
399 memcpy(&dump->regs.gprs[0],regs,sizeof(s390_regs));
400 dump_fpu (regs, &dump->regs.fp_regs);
401 memcpy(&dump->regs.per_info,&current->thread.per_info,sizeof(per_struct));
405 * These bracket the sleeping functions..
407 extern void scheduling_functions_start_here(void);
408 extern void scheduling_functions_end_here(void);
409 #define first_sched ((unsigned long) scheduling_functions_start_here)
410 #define last_sched ((unsigned long) scheduling_functions_end_here)
412 unsigned long get_wchan(struct task_struct *p)
414 unsigned long r14, r15;
415 unsigned long stack_page;
416 int count = 0;
417 if (!p || p == current || p->state == TASK_RUNNING)
418 return 0;
419 stack_page = (unsigned long) p;
420 r15 = p->thread.ksp;
421 do {
422 r14 = *(unsigned long *) (r15+56);
423 if (r14 < first_sched || r14 >= last_sched)
424 return r14;
425 r15 = *(unsigned long *) (r15+60);
426 } while (count++ < 16);
427 return 0;
429 #undef last_sched
430 #undef first_sched