Linux-2.6.12-rc2
[safe/jmp/linux-2.6] / arch / m68knommu / kernel / process.c
1 /*
2  *  linux/arch/m68knommu/kernel/process.c
3  *
4  *  Copyright (C) 1995  Hamish Macdonald
5  *
6  *  68060 fixes by Jesper Skov
7  *
8  *  uClinux changes
9  *  Copyright (C) 2000-2002, David McCullough <davidm@snapgear.com>
10  */
11
12 /*
13  * This file handles the architecture-dependent parts of process handling..
14  */
15
16 #include <linux/config.h>
17 #include <linux/module.h>
18 #include <linux/errno.h>
19 #include <linux/sched.h>
20 #include <linux/kernel.h>
21 #include <linux/mm.h>
22 #include <linux/smp.h>
23 #include <linux/smp_lock.h>
24 #include <linux/stddef.h>
25 #include <linux/unistd.h>
26 #include <linux/ptrace.h>
27 #include <linux/slab.h>
28 #include <linux/user.h>
29 #include <linux/a.out.h>
30 #include <linux/interrupt.h>
31 #include <linux/reboot.h>
32
33 #include <asm/uaccess.h>
34 #include <asm/system.h>
35 #include <asm/traps.h>
36 #include <asm/machdep.h>
37 #include <asm/setup.h>
38 #include <asm/pgtable.h>
39
40 asmlinkage void ret_from_fork(void);
41
42
43 /*
44  * The idle loop on an m68knommu..
45  */
46 void default_idle(void)
47 {
48         while(1) {
49                 if (need_resched())
50                         __asm__("stop #0x2000" : : : "cc");
51                 schedule();
52         }
53 }
54
55 void (*idle)(void) = default_idle;
56
57 /*
58  * The idle thread. There's no useful work to be
59  * done, so just try to conserve power and have a
60  * low exit latency (ie sit in a loop waiting for
61  * somebody to say that they'd like to reschedule)
62  */
63 void cpu_idle(void)
64 {
65         /* endless idle loop with no priority at all */
66         idle();
67 }
68
69 void machine_restart(char * __unused)
70 {
71         if (mach_reset)
72                 mach_reset();
73         for (;;);
74 }
75
76 EXPORT_SYMBOL(machine_restart);
77
78 void machine_halt(void)
79 {
80         if (mach_halt)
81                 mach_halt();
82         for (;;);
83 }
84
85 EXPORT_SYMBOL(machine_halt);
86
87 void machine_power_off(void)
88 {
89         if (mach_power_off)
90                 mach_power_off();
91         for (;;);
92 }
93
94 EXPORT_SYMBOL(machine_power_off);
95
96 void show_regs(struct pt_regs * regs)
97 {
98         printk(KERN_NOTICE "\n");
99         printk(KERN_NOTICE "Format %02x  Vector: %04x  PC: %08lx  Status: %04x    %s\n",
100                regs->format, regs->vector, regs->pc, regs->sr, print_tainted());
101         printk(KERN_NOTICE "ORIG_D0: %08lx  D0: %08lx  A2: %08lx  A1: %08lx\n",
102                regs->orig_d0, regs->d0, regs->a2, regs->a1);
103         printk(KERN_NOTICE "A0: %08lx  D5: %08lx  D4: %08lx\n",
104                regs->a0, regs->d5, regs->d4);
105         printk(KERN_NOTICE "D3: %08lx  D2: %08lx  D1: %08lx\n",
106                regs->d3, regs->d2, regs->d1);
107         if (!(regs->sr & PS_S))
108                 printk(KERN_NOTICE "USP: %08lx\n", rdusp());
109 }
110
111 /*
112  * Create a kernel thread
113  */
114 int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
115 {
116         int retval;
117         long clone_arg = flags | CLONE_VM;
118         mm_segment_t fs;
119
120         fs = get_fs();
121         set_fs(KERNEL_DS);
122
123         __asm__ __volatile__ (
124                         "movel  %%sp, %%d2\n\t"
125                         "movel  %5, %%d1\n\t"
126                         "movel  %1, %%d0\n\t"
127                         "trap   #0\n\t"
128                         "cmpl   %%sp, %%d2\n\t"
129                         "jeq    1f\n\t"
130                         "movel  %3, %%sp@-\n\t"
131                         "jsr    %4@\n\t"
132                         "movel  %2, %%d0\n\t"
133                         "trap   #0\n"
134                         "1:\n\t"
135                         "movel  %%d0, %0\n"
136                 : "=d" (retval)
137                 : "i" (__NR_clone),
138                   "i" (__NR_exit),
139                   "a" (arg),
140                   "a" (fn),
141                   "a" (clone_arg)
142                 : "cc", "%d0", "%d1", "%d2");
143
144         set_fs(fs);
145         return retval;
146 }
147
148 void flush_thread(void)
149 {
150 #ifdef CONFIG_FPU
151         unsigned long zero = 0;
152 #endif
153         set_fs(USER_DS);
154         current->thread.fs = __USER_DS;
155 #ifdef CONFIG_FPU
156         if (!FPU_IS_EMU)
157                 asm volatile (".chip 68k/68881\n\t"
158                               "frestore %0@\n\t"
159                               ".chip 68k" : : "a" (&zero));
160 #endif
161 }
162
163 /*
164  * "m68k_fork()".. By the time we get here, the
165  * non-volatile registers have also been saved on the
166  * stack. We do some ugly pointer stuff here.. (see
167  * also copy_thread)
168  */
169
170 asmlinkage int m68k_fork(struct pt_regs *regs)
171 {
172         /* fork almost works, enough to trick you into looking elsewhere :-( */
173         return(-EINVAL);
174 }
175
176 asmlinkage int m68k_vfork(struct pt_regs *regs)
177 {
178         return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, rdusp(), regs, 0, NULL, NULL);
179 }
180
181 asmlinkage int m68k_clone(struct pt_regs *regs)
182 {
183         unsigned long clone_flags;
184         unsigned long newsp;
185
186         /* syscall2 puts clone_flags in d1 and usp in d2 */
187         clone_flags = regs->d1;
188         newsp = regs->d2;
189         if (!newsp)
190                 newsp = rdusp();
191         return do_fork(clone_flags, newsp, regs, 0, NULL, NULL);
192 }
193
194 int copy_thread(int nr, unsigned long clone_flags,
195                 unsigned long usp, unsigned long topstk,
196                 struct task_struct * p, struct pt_regs * regs)
197 {
198         struct pt_regs * childregs;
199         struct switch_stack * childstack, *stack;
200         unsigned long stack_offset, *retp;
201
202         stack_offset = THREAD_SIZE - sizeof(struct pt_regs);
203         childregs = (struct pt_regs *) ((unsigned long) p->thread_info + stack_offset);
204
205         *childregs = *regs;
206         childregs->d0 = 0;
207
208         retp = ((unsigned long *) regs);
209         stack = ((struct switch_stack *) retp) - 1;
210
211         childstack = ((struct switch_stack *) childregs) - 1;
212         *childstack = *stack;
213         childstack->retpc = (unsigned long)ret_from_fork;
214
215         p->thread.usp = usp;
216         p->thread.ksp = (unsigned long)childstack;
217         /*
218          * Must save the current SFC/DFC value, NOT the value when
219          * the parent was last descheduled - RGH  10-08-96
220          */
221         p->thread.fs = get_fs().seg;
222
223 #ifdef CONFIG_FPU
224         if (!FPU_IS_EMU) {
225                 /* Copy the current fpu state */
226                 asm volatile ("fsave %0" : : "m" (p->thread.fpstate[0]) : "memory");
227
228                 if (p->thread.fpstate[0])
229                   asm volatile ("fmovemx %/fp0-%/fp7,%0\n\t"
230                                 "fmoveml %/fpiar/%/fpcr/%/fpsr,%1"
231                                 : : "m" (p->thread.fp[0]), "m" (p->thread.fpcntl[0])
232                                 : "memory");
233                 /* Restore the state in case the fpu was busy */
234                 asm volatile ("frestore %0" : : "m" (p->thread.fpstate[0]));
235         }
236 #endif
237
238         return 0;
239 }
240
241 /* Fill in the fpu structure for a core dump.  */
242
243 int dump_fpu(struct pt_regs *regs, struct user_m68kfp_struct *fpu)
244 {
245 #ifdef CONFIG_FPU
246         char fpustate[216];
247
248         if (FPU_IS_EMU) {
249                 int i;
250
251                 memcpy(fpu->fpcntl, current->thread.fpcntl, 12);
252                 memcpy(fpu->fpregs, current->thread.fp, 96);
253                 /* Convert internal fpu reg representation
254                  * into long double format
255                  */
256                 for (i = 0; i < 24; i += 3)
257                         fpu->fpregs[i] = ((fpu->fpregs[i] & 0xffff0000) << 15) |
258                                          ((fpu->fpregs[i] & 0x0000ffff) << 16);
259                 return 1;
260         }
261
262         /* First dump the fpu context to avoid protocol violation.  */
263         asm volatile ("fsave %0" :: "m" (fpustate[0]) : "memory");
264         if (!fpustate[0])
265                 return 0;
266
267         asm volatile ("fmovem %/fpiar/%/fpcr/%/fpsr,%0"
268                 :: "m" (fpu->fpcntl[0])
269                 : "memory");
270         asm volatile ("fmovemx %/fp0-%/fp7,%0"
271                 :: "m" (fpu->fpregs[0])
272                 : "memory");
273 #endif
274         return 1;
275 }
276
277 /*
278  * fill in the user structure for a core dump..
279  */
280 void dump_thread(struct pt_regs * regs, struct user * dump)
281 {
282         struct switch_stack *sw;
283
284         /* changed the size calculations - should hopefully work better. lbt */
285         dump->magic = CMAGIC;
286         dump->start_code = 0;
287         dump->start_stack = rdusp() & ~(PAGE_SIZE - 1);
288         dump->u_tsize = ((unsigned long) current->mm->end_code) >> PAGE_SHIFT;
289         dump->u_dsize = ((unsigned long) (current->mm->brk +
290                                           (PAGE_SIZE-1))) >> PAGE_SHIFT;
291         dump->u_dsize -= dump->u_tsize;
292         dump->u_ssize = 0;
293
294         if (dump->start_stack < TASK_SIZE)
295                 dump->u_ssize = ((unsigned long) (TASK_SIZE - dump->start_stack)) >> PAGE_SHIFT;
296
297         dump->u_ar0 = (struct user_regs_struct *)((int)&dump->regs - (int)dump);
298         sw = ((struct switch_stack *)regs) - 1;
299         dump->regs.d1 = regs->d1;
300         dump->regs.d2 = regs->d2;
301         dump->regs.d3 = regs->d3;
302         dump->regs.d4 = regs->d4;
303         dump->regs.d5 = regs->d5;
304         dump->regs.d6 = sw->d6;
305         dump->regs.d7 = sw->d7;
306         dump->regs.a0 = regs->a0;
307         dump->regs.a1 = regs->a1;
308         dump->regs.a2 = regs->a2;
309         dump->regs.a3 = sw->a3;
310         dump->regs.a4 = sw->a4;
311         dump->regs.a5 = sw->a5;
312         dump->regs.a6 = sw->a6;
313         dump->regs.d0 = regs->d0;
314         dump->regs.orig_d0 = regs->orig_d0;
315         dump->regs.stkadj = regs->stkadj;
316         dump->regs.sr = regs->sr;
317         dump->regs.pc = regs->pc;
318         dump->regs.fmtvec = (regs->format << 12) | regs->vector;
319         /* dump floating point stuff */
320         dump->u_fpvalid = dump_fpu (regs, &dump->m68kfp);
321 }
322
323 /*
324  *      Generic dumping code. Used for panic and debug.
325  */
326 void dump(struct pt_regs *fp)
327 {
328         unsigned long   *sp;
329         unsigned char   *tp;
330         int             i;
331
332         printk(KERN_EMERG "\nCURRENT PROCESS:\n\n");
333         printk(KERN_EMERG "COMM=%s PID=%d\n", current->comm, current->pid);
334
335         if (current->mm) {
336                 printk(KERN_EMERG "TEXT=%08x-%08x DATA=%08x-%08x BSS=%08x-%08x\n",
337                         (int) current->mm->start_code,
338                         (int) current->mm->end_code,
339                         (int) current->mm->start_data,
340                         (int) current->mm->end_data,
341                         (int) current->mm->end_data,
342                         (int) current->mm->brk);
343                 printk(KERN_EMERG "USER-STACK=%08x  KERNEL-STACK=%08x\n\n",
344                         (int) current->mm->start_stack,
345                         (int)(((unsigned long) current) + THREAD_SIZE));
346         }
347
348         printk(KERN_EMERG "PC: %08lx\n", fp->pc);
349         printk(KERN_EMERG "SR: %08lx    SP: %08lx\n", (long) fp->sr, (long) fp);
350         printk(KERN_EMERG "d0: %08lx    d1: %08lx    d2: %08lx    d3: %08lx\n",
351                 fp->d0, fp->d1, fp->d2, fp->d3);
352         printk(KERN_EMERG "d4: %08lx    d5: %08lx    a0: %08lx    a1: %08lx\n",
353                 fp->d4, fp->d5, fp->a0, fp->a1);
354         printk(KERN_EMERG "\nUSP: %08x   TRAPFRAME: %08x\n", (unsigned int) rdusp(),
355                 (unsigned int) fp);
356
357         printk(KERN_EMERG "\nCODE:");
358         tp = ((unsigned char *) fp->pc) - 0x20;
359         for (sp = (unsigned long *) tp, i = 0; (i < 0x40);  i += 4) {
360                 if ((i % 0x10) == 0)
361                         printk(KERN_EMERG "\n%08x: ", (int) (tp + i));
362                 printk(KERN_EMERG "%08x ", (int) *sp++);
363         }
364         printk(KERN_EMERG "\n");
365
366         printk(KERN_EMERG "\nKERNEL STACK:");
367         tp = ((unsigned char *) fp) - 0x40;
368         for (sp = (unsigned long *) tp, i = 0; (i < 0xc0); i += 4) {
369                 if ((i % 0x10) == 0)
370                         printk(KERN_EMERG "\n%08x: ", (int) (tp + i));
371                 printk(KERN_EMERG "%08x ", (int) *sp++);
372         }
373         printk(KERN_EMERG "\n");
374         printk(KERN_EMERG "\n");
375
376         printk(KERN_EMERG "\nUSER STACK:");
377         tp = (unsigned char *) (rdusp() - 0x10);
378         for (sp = (unsigned long *) tp, i = 0; (i < 0x80); i += 4) {
379                 if ((i % 0x10) == 0)
380                         printk(KERN_EMERG "\n%08x: ", (int) (tp + i));
381                 printk(KERN_EMERG "%08x ", (int) *sp++);
382         }
383         printk(KERN_EMERG "\n\n");
384 }
385
386 /*
387  * sys_execve() executes a new program.
388  */
389 asmlinkage int sys_execve(char *name, char **argv, char **envp)
390 {
391         int error;
392         char * filename;
393         struct pt_regs *regs = (struct pt_regs *) &name;
394
395         lock_kernel();
396         filename = getname(name);
397         error = PTR_ERR(filename);
398         if (IS_ERR(filename))
399                 goto out;
400         error = do_execve(filename, argv, envp, regs);
401         putname(filename);
402 out:
403         unlock_kernel();
404         return error;
405 }
406
407 unsigned long get_wchan(struct task_struct *p)
408 {
409         unsigned long fp, pc;
410         unsigned long stack_page;
411         int count = 0;
412         if (!p || p == current || p->state == TASK_RUNNING)
413                 return 0;
414
415         stack_page = (unsigned long)p;
416         fp = ((struct switch_stack *)p->thread.ksp)->a6;
417         do {
418                 if (fp < stack_page+sizeof(struct thread_info) ||
419                     fp >= 8184+stack_page)
420                         return 0;
421                 pc = ((unsigned long *)fp)[1];
422                 if (!in_sched_functions(pc))
423                         return pc;
424                 fp = *(unsigned long *) fp;
425         } while (count++ < 16);
426         return 0;
427 }
428
429 /*
430  * Return saved PC of a blocked thread.
431  */
432 unsigned long thread_saved_pc(struct task_struct *tsk)
433 {
434         struct switch_stack *sw = (struct switch_stack *)tsk->thread.ksp;
435
436         /* Check whether the thread is blocked in resume() */
437         if (in_sched_functions(sw->retpc))
438                 return ((unsigned long *)sw->a6)[1];
439         else
440                 return sw->retpc;
441 }
442