[PATCH] m68knommu: task_stack_page()
[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         local_irq_disable();
49         while (!need_resched()) {
50                 /* This stop will re-enable interrupts */
51                 __asm__("stop #0x2000" : : : "cc");
52                 local_irq_disable();
53         }
54         local_irq_enable();
55 }
56
57 void (*idle)(void) = default_idle;
58
59 /*
60  * The idle thread. There's no useful work to be
61  * done, so just try to conserve power and have a
62  * low exit latency (ie sit in a loop waiting for
63  * somebody to say that they'd like to reschedule)
64  */
65 void cpu_idle(void)
66 {
67         /* endless idle loop with no priority at all */
68         while (1) {
69                 idle();
70                 preempt_enable_no_resched();
71                 schedule();
72                 preempt_disable();
73         }
74 }
75
76 void machine_restart(char * __unused)
77 {
78         if (mach_reset)
79                 mach_reset();
80         for (;;);
81 }
82
83 void machine_halt(void)
84 {
85         if (mach_halt)
86                 mach_halt();
87         for (;;);
88 }
89
90 void machine_power_off(void)
91 {
92         if (mach_power_off)
93                 mach_power_off();
94         for (;;);
95 }
96
97 void show_regs(struct pt_regs * regs)
98 {
99         printk(KERN_NOTICE "\n");
100         printk(KERN_NOTICE "Format %02x  Vector: %04x  PC: %08lx  Status: %04x    %s\n",
101                regs->format, regs->vector, regs->pc, regs->sr, print_tainted());
102         printk(KERN_NOTICE "ORIG_D0: %08lx  D0: %08lx  A2: %08lx  A1: %08lx\n",
103                regs->orig_d0, regs->d0, regs->a2, regs->a1);
104         printk(KERN_NOTICE "A0: %08lx  D5: %08lx  D4: %08lx\n",
105                regs->a0, regs->d5, regs->d4);
106         printk(KERN_NOTICE "D3: %08lx  D2: %08lx  D1: %08lx\n",
107                regs->d3, regs->d2, regs->d1);
108         if (!(regs->sr & PS_S))
109                 printk(KERN_NOTICE "USP: %08lx\n", rdusp());
110 }
111
112 /*
113  * Create a kernel thread
114  */
115 int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
116 {
117         int retval;
118         long clone_arg = flags | CLONE_VM;
119         mm_segment_t fs;
120
121         fs = get_fs();
122         set_fs(KERNEL_DS);
123
124         __asm__ __volatile__ (
125                         "movel  %%sp, %%d2\n\t"
126                         "movel  %5, %%d1\n\t"
127                         "movel  %1, %%d0\n\t"
128                         "trap   #0\n\t"
129                         "cmpl   %%sp, %%d2\n\t"
130                         "jeq    1f\n\t"
131                         "movel  %3, %%sp@-\n\t"
132                         "jsr    %4@\n\t"
133                         "movel  %2, %%d0\n\t"
134                         "trap   #0\n"
135                         "1:\n\t"
136                         "movel  %%d0, %0\n"
137                 : "=d" (retval)
138                 : "i" (__NR_clone),
139                   "i" (__NR_exit),
140                   "a" (arg),
141                   "a" (fn),
142                   "a" (clone_arg)
143                 : "cc", "%d0", "%d1", "%d2");
144
145         set_fs(fs);
146         return retval;
147 }
148
149 void flush_thread(void)
150 {
151 #ifdef CONFIG_FPU
152         unsigned long zero = 0;
153 #endif
154         set_fs(USER_DS);
155         current->thread.fs = __USER_DS;
156 #ifdef CONFIG_FPU
157         if (!FPU_IS_EMU)
158                 asm volatile (".chip 68k/68881\n\t"
159                               "frestore %0@\n\t"
160                               ".chip 68k" : : "a" (&zero));
161 #endif
162 }
163
164 /*
165  * "m68k_fork()".. By the time we get here, the
166  * non-volatile registers have also been saved on the
167  * stack. We do some ugly pointer stuff here.. (see
168  * also copy_thread)
169  */
170
171 asmlinkage int m68k_fork(struct pt_regs *regs)
172 {
173         /* fork almost works, enough to trick you into looking elsewhere :-( */
174         return(-EINVAL);
175 }
176
177 asmlinkage int m68k_vfork(struct pt_regs *regs)
178 {
179         return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, rdusp(), regs, 0, NULL, NULL);
180 }
181
182 asmlinkage int m68k_clone(struct pt_regs *regs)
183 {
184         unsigned long clone_flags;
185         unsigned long newsp;
186
187         /* syscall2 puts clone_flags in d1 and usp in d2 */
188         clone_flags = regs->d1;
189         newsp = regs->d2;
190         if (!newsp)
191                 newsp = rdusp();
192         return do_fork(clone_flags, newsp, regs, 0, NULL, NULL);
193 }
194
195 int copy_thread(int nr, unsigned long clone_flags,
196                 unsigned long usp, unsigned long topstk,
197                 struct task_struct * p, struct pt_regs * regs)
198 {
199         struct pt_regs * childregs;
200         struct switch_stack * childstack, *stack;
201         unsigned long *retp;
202
203         childregs = (struct pt_regs *) (task_stack_page(p) + THREAD_SIZE) - 1;
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  *      Generic dumping code. Used for panic and debug.
279  */
280 void dump(struct pt_regs *fp)
281 {
282         unsigned long   *sp;
283         unsigned char   *tp;
284         int             i;
285
286         printk(KERN_EMERG "\nCURRENT PROCESS:\n\n");
287         printk(KERN_EMERG "COMM=%s PID=%d\n", current->comm, current->pid);
288
289         if (current->mm) {
290                 printk(KERN_EMERG "TEXT=%08x-%08x DATA=%08x-%08x BSS=%08x-%08x\n",
291                         (int) current->mm->start_code,
292                         (int) current->mm->end_code,
293                         (int) current->mm->start_data,
294                         (int) current->mm->end_data,
295                         (int) current->mm->end_data,
296                         (int) current->mm->brk);
297                 printk(KERN_EMERG "USER-STACK=%08x  KERNEL-STACK=%08x\n\n",
298                         (int) current->mm->start_stack,
299                         (int)(((unsigned long) current) + THREAD_SIZE));
300         }
301
302         printk(KERN_EMERG "PC: %08lx\n", fp->pc);
303         printk(KERN_EMERG "SR: %08lx    SP: %08lx\n", (long) fp->sr, (long) fp);
304         printk(KERN_EMERG "d0: %08lx    d1: %08lx    d2: %08lx    d3: %08lx\n",
305                 fp->d0, fp->d1, fp->d2, fp->d3);
306         printk(KERN_EMERG "d4: %08lx    d5: %08lx    a0: %08lx    a1: %08lx\n",
307                 fp->d4, fp->d5, fp->a0, fp->a1);
308         printk(KERN_EMERG "\nUSP: %08x   TRAPFRAME: %08x\n", (unsigned int) rdusp(),
309                 (unsigned int) fp);
310
311         printk(KERN_EMERG "\nCODE:");
312         tp = ((unsigned char *) fp->pc) - 0x20;
313         for (sp = (unsigned long *) tp, i = 0; (i < 0x40);  i += 4) {
314                 if ((i % 0x10) == 0)
315                         printk(KERN_EMERG "\n%08x: ", (int) (tp + i));
316                 printk(KERN_EMERG "%08x ", (int) *sp++);
317         }
318         printk(KERN_EMERG "\n");
319
320         printk(KERN_EMERG "\nKERNEL STACK:");
321         tp = ((unsigned char *) fp) - 0x40;
322         for (sp = (unsigned long *) tp, i = 0; (i < 0xc0); i += 4) {
323                 if ((i % 0x10) == 0)
324                         printk(KERN_EMERG "\n%08x: ", (int) (tp + i));
325                 printk(KERN_EMERG "%08x ", (int) *sp++);
326         }
327         printk(KERN_EMERG "\n");
328         printk(KERN_EMERG "\n");
329
330         printk(KERN_EMERG "\nUSER STACK:");
331         tp = (unsigned char *) (rdusp() - 0x10);
332         for (sp = (unsigned long *) tp, i = 0; (i < 0x80); i += 4) {
333                 if ((i % 0x10) == 0)
334                         printk(KERN_EMERG "\n%08x: ", (int) (tp + i));
335                 printk(KERN_EMERG "%08x ", (int) *sp++);
336         }
337         printk(KERN_EMERG "\n\n");
338 }
339
340 /*
341  * sys_execve() executes a new program.
342  */
343 asmlinkage int sys_execve(char *name, char **argv, char **envp)
344 {
345         int error;
346         char * filename;
347         struct pt_regs *regs = (struct pt_regs *) &name;
348
349         lock_kernel();
350         filename = getname(name);
351         error = PTR_ERR(filename);
352         if (IS_ERR(filename))
353                 goto out;
354         error = do_execve(filename, argv, envp, regs);
355         putname(filename);
356 out:
357         unlock_kernel();
358         return error;
359 }
360
361 unsigned long get_wchan(struct task_struct *p)
362 {
363         unsigned long fp, pc;
364         unsigned long stack_page;
365         int count = 0;
366         if (!p || p == current || p->state == TASK_RUNNING)
367                 return 0;
368
369         stack_page = (unsigned long)p;
370         fp = ((struct switch_stack *)p->thread.ksp)->a6;
371         do {
372                 if (fp < stack_page+sizeof(struct thread_info) ||
373                     fp >= 8184+stack_page)
374                         return 0;
375                 pc = ((unsigned long *)fp)[1];
376                 if (!in_sched_functions(pc))
377                         return pc;
378                 fp = *(unsigned long *) fp;
379         } while (count++ < 16);
380         return 0;
381 }
382
383 /*
384  * Return saved PC of a blocked thread.
385  */
386 unsigned long thread_saved_pc(struct task_struct *tsk)
387 {
388         struct switch_stack *sw = (struct switch_stack *)tsk->thread.ksp;
389
390         /* Check whether the thread is blocked in resume() */
391         if (in_sched_functions(sw->retpc))
392                 return ((unsigned long *)sw->a6)[1];
393         else
394                 return sw->retpc;
395 }
396