Automatic merge with /usr/src/ntfs-2.6.git
[sfrench/cifs-2.6.git] / 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 EXPORT_SYMBOL(machine_restart);
84
85 void machine_halt(void)
86 {
87         if (mach_halt)
88                 mach_halt();
89         for (;;);
90 }
91
92 EXPORT_SYMBOL(machine_halt);
93
94 void machine_power_off(void)
95 {
96         if (mach_power_off)
97                 mach_power_off();
98         for (;;);
99 }
100
101 EXPORT_SYMBOL(machine_power_off);
102
103 void show_regs(struct pt_regs * regs)
104 {
105         printk(KERN_NOTICE "\n");
106         printk(KERN_NOTICE "Format %02x  Vector: %04x  PC: %08lx  Status: %04x    %s\n",
107                regs->format, regs->vector, regs->pc, regs->sr, print_tainted());
108         printk(KERN_NOTICE "ORIG_D0: %08lx  D0: %08lx  A2: %08lx  A1: %08lx\n",
109                regs->orig_d0, regs->d0, regs->a2, regs->a1);
110         printk(KERN_NOTICE "A0: %08lx  D5: %08lx  D4: %08lx\n",
111                regs->a0, regs->d5, regs->d4);
112         printk(KERN_NOTICE "D3: %08lx  D2: %08lx  D1: %08lx\n",
113                regs->d3, regs->d2, regs->d1);
114         if (!(regs->sr & PS_S))
115                 printk(KERN_NOTICE "USP: %08lx\n", rdusp());
116 }
117
118 /*
119  * Create a kernel thread
120  */
121 int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
122 {
123         int retval;
124         long clone_arg = flags | CLONE_VM;
125         mm_segment_t fs;
126
127         fs = get_fs();
128         set_fs(KERNEL_DS);
129
130         __asm__ __volatile__ (
131                         "movel  %%sp, %%d2\n\t"
132                         "movel  %5, %%d1\n\t"
133                         "movel  %1, %%d0\n\t"
134                         "trap   #0\n\t"
135                         "cmpl   %%sp, %%d2\n\t"
136                         "jeq    1f\n\t"
137                         "movel  %3, %%sp@-\n\t"
138                         "jsr    %4@\n\t"
139                         "movel  %2, %%d0\n\t"
140                         "trap   #0\n"
141                         "1:\n\t"
142                         "movel  %%d0, %0\n"
143                 : "=d" (retval)
144                 : "i" (__NR_clone),
145                   "i" (__NR_exit),
146                   "a" (arg),
147                   "a" (fn),
148                   "a" (clone_arg)
149                 : "cc", "%d0", "%d1", "%d2");
150
151         set_fs(fs);
152         return retval;
153 }
154
155 void flush_thread(void)
156 {
157 #ifdef CONFIG_FPU
158         unsigned long zero = 0;
159 #endif
160         set_fs(USER_DS);
161         current->thread.fs = __USER_DS;
162 #ifdef CONFIG_FPU
163         if (!FPU_IS_EMU)
164                 asm volatile (".chip 68k/68881\n\t"
165                               "frestore %0@\n\t"
166                               ".chip 68k" : : "a" (&zero));
167 #endif
168 }
169
170 /*
171  * "m68k_fork()".. By the time we get here, the
172  * non-volatile registers have also been saved on the
173  * stack. We do some ugly pointer stuff here.. (see
174  * also copy_thread)
175  */
176
177 asmlinkage int m68k_fork(struct pt_regs *regs)
178 {
179         /* fork almost works, enough to trick you into looking elsewhere :-( */
180         return(-EINVAL);
181 }
182
183 asmlinkage int m68k_vfork(struct pt_regs *regs)
184 {
185         return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, rdusp(), regs, 0, NULL, NULL);
186 }
187
188 asmlinkage int m68k_clone(struct pt_regs *regs)
189 {
190         unsigned long clone_flags;
191         unsigned long newsp;
192
193         /* syscall2 puts clone_flags in d1 and usp in d2 */
194         clone_flags = regs->d1;
195         newsp = regs->d2;
196         if (!newsp)
197                 newsp = rdusp();
198         return do_fork(clone_flags, newsp, regs, 0, NULL, NULL);
199 }
200
201 int copy_thread(int nr, unsigned long clone_flags,
202                 unsigned long usp, unsigned long topstk,
203                 struct task_struct * p, struct pt_regs * regs)
204 {
205         struct pt_regs * childregs;
206         struct switch_stack * childstack, *stack;
207         unsigned long stack_offset, *retp;
208
209         stack_offset = THREAD_SIZE - sizeof(struct pt_regs);
210         childregs = (struct pt_regs *) ((unsigned long) p->thread_info + stack_offset);
211
212         *childregs = *regs;
213         childregs->d0 = 0;
214
215         retp = ((unsigned long *) regs);
216         stack = ((struct switch_stack *) retp) - 1;
217
218         childstack = ((struct switch_stack *) childregs) - 1;
219         *childstack = *stack;
220         childstack->retpc = (unsigned long)ret_from_fork;
221
222         p->thread.usp = usp;
223         p->thread.ksp = (unsigned long)childstack;
224         /*
225          * Must save the current SFC/DFC value, NOT the value when
226          * the parent was last descheduled - RGH  10-08-96
227          */
228         p->thread.fs = get_fs().seg;
229
230 #ifdef CONFIG_FPU
231         if (!FPU_IS_EMU) {
232                 /* Copy the current fpu state */
233                 asm volatile ("fsave %0" : : "m" (p->thread.fpstate[0]) : "memory");
234
235                 if (p->thread.fpstate[0])
236                   asm volatile ("fmovemx %/fp0-%/fp7,%0\n\t"
237                                 "fmoveml %/fpiar/%/fpcr/%/fpsr,%1"
238                                 : : "m" (p->thread.fp[0]), "m" (p->thread.fpcntl[0])
239                                 : "memory");
240                 /* Restore the state in case the fpu was busy */
241                 asm volatile ("frestore %0" : : "m" (p->thread.fpstate[0]));
242         }
243 #endif
244
245         return 0;
246 }
247
248 /* Fill in the fpu structure for a core dump.  */
249
250 int dump_fpu(struct pt_regs *regs, struct user_m68kfp_struct *fpu)
251 {
252 #ifdef CONFIG_FPU
253         char fpustate[216];
254
255         if (FPU_IS_EMU) {
256                 int i;
257
258                 memcpy(fpu->fpcntl, current->thread.fpcntl, 12);
259                 memcpy(fpu->fpregs, current->thread.fp, 96);
260                 /* Convert internal fpu reg representation
261                  * into long double format
262                  */
263                 for (i = 0; i < 24; i += 3)
264                         fpu->fpregs[i] = ((fpu->fpregs[i] & 0xffff0000) << 15) |
265                                          ((fpu->fpregs[i] & 0x0000ffff) << 16);
266                 return 1;
267         }
268
269         /* First dump the fpu context to avoid protocol violation.  */
270         asm volatile ("fsave %0" :: "m" (fpustate[0]) : "memory");
271         if (!fpustate[0])
272                 return 0;
273
274         asm volatile ("fmovem %/fpiar/%/fpcr/%/fpsr,%0"
275                 :: "m" (fpu->fpcntl[0])
276                 : "memory");
277         asm volatile ("fmovemx %/fp0-%/fp7,%0"
278                 :: "m" (fpu->fpregs[0])
279                 : "memory");
280 #endif
281         return 1;
282 }
283
284 /*
285  * fill in the user structure for a core dump..
286  */
287 void dump_thread(struct pt_regs * regs, struct user * dump)
288 {
289         struct switch_stack *sw;
290
291         /* changed the size calculations - should hopefully work better. lbt */
292         dump->magic = CMAGIC;
293         dump->start_code = 0;
294         dump->start_stack = rdusp() & ~(PAGE_SIZE - 1);
295         dump->u_tsize = ((unsigned long) current->mm->end_code) >> PAGE_SHIFT;
296         dump->u_dsize = ((unsigned long) (current->mm->brk +
297                                           (PAGE_SIZE-1))) >> PAGE_SHIFT;
298         dump->u_dsize -= dump->u_tsize;
299         dump->u_ssize = 0;
300
301         if (dump->start_stack < TASK_SIZE)
302                 dump->u_ssize = ((unsigned long) (TASK_SIZE - dump->start_stack)) >> PAGE_SHIFT;
303
304         dump->u_ar0 = (struct user_regs_struct *)((int)&dump->regs - (int)dump);
305         sw = ((struct switch_stack *)regs) - 1;
306         dump->regs.d1 = regs->d1;
307         dump->regs.d2 = regs->d2;
308         dump->regs.d3 = regs->d3;
309         dump->regs.d4 = regs->d4;
310         dump->regs.d5 = regs->d5;
311         dump->regs.d6 = sw->d6;
312         dump->regs.d7 = sw->d7;
313         dump->regs.a0 = regs->a0;
314         dump->regs.a1 = regs->a1;
315         dump->regs.a2 = regs->a2;
316         dump->regs.a3 = sw->a3;
317         dump->regs.a4 = sw->a4;
318         dump->regs.a5 = sw->a5;
319         dump->regs.a6 = sw->a6;
320         dump->regs.d0 = regs->d0;
321         dump->regs.orig_d0 = regs->orig_d0;
322         dump->regs.stkadj = regs->stkadj;
323         dump->regs.sr = regs->sr;
324         dump->regs.pc = regs->pc;
325         dump->regs.fmtvec = (regs->format << 12) | regs->vector;
326         /* dump floating point stuff */
327         dump->u_fpvalid = dump_fpu (regs, &dump->m68kfp);
328 }
329
330 /*
331  *      Generic dumping code. Used for panic and debug.
332  */
333 void dump(struct pt_regs *fp)
334 {
335         unsigned long   *sp;
336         unsigned char   *tp;
337         int             i;
338
339         printk(KERN_EMERG "\nCURRENT PROCESS:\n\n");
340         printk(KERN_EMERG "COMM=%s PID=%d\n", current->comm, current->pid);
341
342         if (current->mm) {
343                 printk(KERN_EMERG "TEXT=%08x-%08x DATA=%08x-%08x BSS=%08x-%08x\n",
344                         (int) current->mm->start_code,
345                         (int) current->mm->end_code,
346                         (int) current->mm->start_data,
347                         (int) current->mm->end_data,
348                         (int) current->mm->end_data,
349                         (int) current->mm->brk);
350                 printk(KERN_EMERG "USER-STACK=%08x  KERNEL-STACK=%08x\n\n",
351                         (int) current->mm->start_stack,
352                         (int)(((unsigned long) current) + THREAD_SIZE));
353         }
354
355         printk(KERN_EMERG "PC: %08lx\n", fp->pc);
356         printk(KERN_EMERG "SR: %08lx    SP: %08lx\n", (long) fp->sr, (long) fp);
357         printk(KERN_EMERG "d0: %08lx    d1: %08lx    d2: %08lx    d3: %08lx\n",
358                 fp->d0, fp->d1, fp->d2, fp->d3);
359         printk(KERN_EMERG "d4: %08lx    d5: %08lx    a0: %08lx    a1: %08lx\n",
360                 fp->d4, fp->d5, fp->a0, fp->a1);
361         printk(KERN_EMERG "\nUSP: %08x   TRAPFRAME: %08x\n", (unsigned int) rdusp(),
362                 (unsigned int) fp);
363
364         printk(KERN_EMERG "\nCODE:");
365         tp = ((unsigned char *) fp->pc) - 0x20;
366         for (sp = (unsigned long *) tp, i = 0; (i < 0x40);  i += 4) {
367                 if ((i % 0x10) == 0)
368                         printk(KERN_EMERG "\n%08x: ", (int) (tp + i));
369                 printk(KERN_EMERG "%08x ", (int) *sp++);
370         }
371         printk(KERN_EMERG "\n");
372
373         printk(KERN_EMERG "\nKERNEL STACK:");
374         tp = ((unsigned char *) fp) - 0x40;
375         for (sp = (unsigned long *) tp, i = 0; (i < 0xc0); i += 4) {
376                 if ((i % 0x10) == 0)
377                         printk(KERN_EMERG "\n%08x: ", (int) (tp + i));
378                 printk(KERN_EMERG "%08x ", (int) *sp++);
379         }
380         printk(KERN_EMERG "\n");
381         printk(KERN_EMERG "\n");
382
383         printk(KERN_EMERG "\nUSER STACK:");
384         tp = (unsigned char *) (rdusp() - 0x10);
385         for (sp = (unsigned long *) tp, i = 0; (i < 0x80); i += 4) {
386                 if ((i % 0x10) == 0)
387                         printk(KERN_EMERG "\n%08x: ", (int) (tp + i));
388                 printk(KERN_EMERG "%08x ", (int) *sp++);
389         }
390         printk(KERN_EMERG "\n\n");
391 }
392
393 /*
394  * sys_execve() executes a new program.
395  */
396 asmlinkage int sys_execve(char *name, char **argv, char **envp)
397 {
398         int error;
399         char * filename;
400         struct pt_regs *regs = (struct pt_regs *) &name;
401
402         lock_kernel();
403         filename = getname(name);
404         error = PTR_ERR(filename);
405         if (IS_ERR(filename))
406                 goto out;
407         error = do_execve(filename, argv, envp, regs);
408         putname(filename);
409 out:
410         unlock_kernel();
411         return error;
412 }
413
414 unsigned long get_wchan(struct task_struct *p)
415 {
416         unsigned long fp, pc;
417         unsigned long stack_page;
418         int count = 0;
419         if (!p || p == current || p->state == TASK_RUNNING)
420                 return 0;
421
422         stack_page = (unsigned long)p;
423         fp = ((struct switch_stack *)p->thread.ksp)->a6;
424         do {
425                 if (fp < stack_page+sizeof(struct thread_info) ||
426                     fp >= 8184+stack_page)
427                         return 0;
428                 pc = ((unsigned long *)fp)[1];
429                 if (!in_sched_functions(pc))
430                         return pc;
431                 fp = *(unsigned long *) fp;
432         } while (count++ < 16);
433         return 0;
434 }
435
436 /*
437  * Return saved PC of a blocked thread.
438  */
439 unsigned long thread_saved_pc(struct task_struct *tsk)
440 {
441         struct switch_stack *sw = (struct switch_stack *)tsk->thread.ksp;
442
443         /* Check whether the thread is blocked in resume() */
444         if (in_sched_functions(sw->retpc))
445                 return ((unsigned long *)sw->a6)[1];
446         else
447                 return sw->retpc;
448 }
449