BKL: remove extraneous #include <smp_lock.h>
[pandora-kernel.git] / arch / m68k / kernel / process.c
1 /*
2  *  linux/arch/m68k/kernel/process.c
3  *
4  *  Copyright (C) 1995  Hamish Macdonald
5  *
6  *  68060 fixes by Jesper Skov
7  */
8
9 /*
10  * This file handles the architecture-dependent parts of process handling..
11  */
12
13 #include <linux/errno.h>
14 #include <linux/module.h>
15 #include <linux/sched.h>
16 #include <linux/kernel.h>
17 #include <linux/mm.h>
18 #include <linux/slab.h>
19 #include <linux/fs.h>
20 #include <linux/smp.h>
21 #include <linux/stddef.h>
22 #include <linux/unistd.h>
23 #include <linux/ptrace.h>
24 #include <linux/user.h>
25 #include <linux/reboot.h>
26 #include <linux/init_task.h>
27 #include <linux/mqueue.h>
28
29 #include <asm/uaccess.h>
30 #include <asm/system.h>
31 #include <asm/traps.h>
32 #include <asm/machdep.h>
33 #include <asm/setup.h>
34 #include <asm/pgtable.h>
35
36 /*
37  * Initial task/thread structure. Make this a per-architecture thing,
38  * because different architectures tend to have different
39  * alignment requirements and potentially different initial
40  * setup.
41  */
42 static struct signal_struct init_signals = INIT_SIGNALS(init_signals);
43 static struct sighand_struct init_sighand = INIT_SIGHAND(init_sighand);
44 union thread_union init_thread_union __init_task_data
45         __attribute__((aligned(THREAD_SIZE))) =
46                 { INIT_THREAD_INFO(init_task) };
47
48 /* initial task structure */
49 struct task_struct init_task = INIT_TASK(init_task);
50
51 EXPORT_SYMBOL(init_task);
52
53 asmlinkage void ret_from_fork(void);
54
55
56 /*
57  * Return saved PC from a blocked thread
58  */
59 unsigned long thread_saved_pc(struct task_struct *tsk)
60 {
61         struct switch_stack *sw = (struct switch_stack *)tsk->thread.ksp;
62         /* Check whether the thread is blocked in resume() */
63         if (in_sched_functions(sw->retpc))
64                 return ((unsigned long *)sw->a6)[1];
65         else
66                 return sw->retpc;
67 }
68
69 /*
70  * The idle loop on an m68k..
71  */
72 static void default_idle(void)
73 {
74         if (!need_resched())
75 #if defined(MACH_ATARI_ONLY)
76                 /* block out HSYNC on the atari (falcon) */
77                 __asm__("stop #0x2200" : : : "cc");
78 #else
79                 __asm__("stop #0x2000" : : : "cc");
80 #endif
81 }
82
83 void (*idle)(void) = default_idle;
84
85 /*
86  * The idle thread. There's no useful work to be
87  * done, so just try to conserve power and have a
88  * low exit latency (ie sit in a loop waiting for
89  * somebody to say that they'd like to reschedule)
90  */
91 void cpu_idle(void)
92 {
93         /* endless idle loop with no priority at all */
94         while (1) {
95                 while (!need_resched())
96                         idle();
97                 preempt_enable_no_resched();
98                 schedule();
99                 preempt_disable();
100         }
101 }
102
103 void machine_restart(char * __unused)
104 {
105         if (mach_reset)
106                 mach_reset();
107         for (;;);
108 }
109
110 void machine_halt(void)
111 {
112         if (mach_halt)
113                 mach_halt();
114         for (;;);
115 }
116
117 void machine_power_off(void)
118 {
119         if (mach_power_off)
120                 mach_power_off();
121         for (;;);
122 }
123
124 void (*pm_power_off)(void) = machine_power_off;
125 EXPORT_SYMBOL(pm_power_off);
126
127 void show_regs(struct pt_regs * regs)
128 {
129         printk("\n");
130         printk("Format %02x  Vector: %04x  PC: %08lx  Status: %04x    %s\n",
131                regs->format, regs->vector, regs->pc, regs->sr, print_tainted());
132         printk("ORIG_D0: %08lx  D0: %08lx  A2: %08lx  A1: %08lx\n",
133                regs->orig_d0, regs->d0, regs->a2, regs->a1);
134         printk("A0: %08lx  D5: %08lx  D4: %08lx\n",
135                regs->a0, regs->d5, regs->d4);
136         printk("D3: %08lx  D2: %08lx  D1: %08lx\n",
137                regs->d3, regs->d2, regs->d1);
138         if (!(regs->sr & PS_S))
139                 printk("USP: %08lx\n", rdusp());
140 }
141
142 /*
143  * Create a kernel thread
144  */
145 int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
146 {
147         int pid;
148         mm_segment_t fs;
149
150         fs = get_fs();
151         set_fs (KERNEL_DS);
152
153         {
154         register long retval __asm__ ("d0");
155         register long clone_arg __asm__ ("d1") = flags | CLONE_VM | CLONE_UNTRACED;
156
157         retval = __NR_clone;
158         __asm__ __volatile__
159           ("clrl %%d2\n\t"
160            "trap #0\n\t"                /* Linux/m68k system call */
161            "tstl %0\n\t"                /* child or parent */
162            "jne 1f\n\t"                 /* parent - jump */
163            "lea %%sp@(%c7),%6\n\t"      /* reload current */
164            "movel %6@,%6\n\t"
165            "movel %3,%%sp@-\n\t"        /* push argument */
166            "jsr %4@\n\t"                /* call fn */
167            "movel %0,%%d1\n\t"          /* pass exit value */
168            "movel %2,%%d0\n\t"          /* exit */
169            "trap #0\n"
170            "1:"
171            : "+d" (retval)
172            : "i" (__NR_clone), "i" (__NR_exit),
173              "r" (arg), "a" (fn), "d" (clone_arg), "r" (current),
174              "i" (-THREAD_SIZE)
175            : "d2");
176
177         pid = retval;
178         }
179
180         set_fs (fs);
181         return pid;
182 }
183 EXPORT_SYMBOL(kernel_thread);
184
185 void flush_thread(void)
186 {
187         unsigned long zero = 0;
188         set_fs(USER_DS);
189         current->thread.fs = __USER_DS;
190         if (!FPU_IS_EMU)
191                 asm volatile (".chip 68k/68881\n\t"
192                               "frestore %0@\n\t"
193                               ".chip 68k" : : "a" (&zero));
194 }
195
196 /*
197  * "m68k_fork()".. By the time we get here, the
198  * non-volatile registers have also been saved on the
199  * stack. We do some ugly pointer stuff here.. (see
200  * also copy_thread)
201  */
202
203 asmlinkage int m68k_fork(struct pt_regs *regs)
204 {
205         return do_fork(SIGCHLD, rdusp(), regs, 0, NULL, NULL);
206 }
207
208 asmlinkage int m68k_vfork(struct pt_regs *regs)
209 {
210         return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, rdusp(), regs, 0,
211                        NULL, NULL);
212 }
213
214 asmlinkage int m68k_clone(struct pt_regs *regs)
215 {
216         unsigned long clone_flags;
217         unsigned long newsp;
218         int __user *parent_tidptr, *child_tidptr;
219
220         /* syscall2 puts clone_flags in d1 and usp in d2 */
221         clone_flags = regs->d1;
222         newsp = regs->d2;
223         parent_tidptr = (int __user *)regs->d3;
224         child_tidptr = (int __user *)regs->d4;
225         if (!newsp)
226                 newsp = rdusp();
227         return do_fork(clone_flags, newsp, regs, 0,
228                        parent_tidptr, child_tidptr);
229 }
230
231 int copy_thread(unsigned long clone_flags, unsigned long usp,
232                  unsigned long unused,
233                  struct task_struct * p, struct pt_regs * regs)
234 {
235         struct pt_regs * childregs;
236         struct switch_stack * childstack, *stack;
237         unsigned long *retp;
238
239         childregs = (struct pt_regs *) (task_stack_page(p) + THREAD_SIZE) - 1;
240
241         *childregs = *regs;
242         childregs->d0 = 0;
243
244         retp = ((unsigned long *) regs);
245         stack = ((struct switch_stack *) retp) - 1;
246
247         childstack = ((struct switch_stack *) childregs) - 1;
248         *childstack = *stack;
249         childstack->retpc = (unsigned long)ret_from_fork;
250
251         p->thread.usp = usp;
252         p->thread.ksp = (unsigned long)childstack;
253
254         if (clone_flags & CLONE_SETTLS)
255                 task_thread_info(p)->tp_value = regs->d5;
256
257         /*
258          * Must save the current SFC/DFC value, NOT the value when
259          * the parent was last descheduled - RGH  10-08-96
260          */
261         p->thread.fs = get_fs().seg;
262
263         if (!FPU_IS_EMU) {
264                 /* Copy the current fpu state */
265                 asm volatile ("fsave %0" : : "m" (p->thread.fpstate[0]) : "memory");
266
267                 if (!CPU_IS_060 ? p->thread.fpstate[0] : p->thread.fpstate[2])
268                   asm volatile ("fmovemx %/fp0-%/fp7,%0\n\t"
269                                 "fmoveml %/fpiar/%/fpcr/%/fpsr,%1"
270                                 : : "m" (p->thread.fp[0]), "m" (p->thread.fpcntl[0])
271                                 : "memory");
272                 /* Restore the state in case the fpu was busy */
273                 asm volatile ("frestore %0" : : "m" (p->thread.fpstate[0]));
274         }
275
276         return 0;
277 }
278
279 /* Fill in the fpu structure for a core dump.  */
280
281 int dump_fpu (struct pt_regs *regs, struct user_m68kfp_struct *fpu)
282 {
283         char fpustate[216];
284
285         if (FPU_IS_EMU) {
286                 int i;
287
288                 memcpy(fpu->fpcntl, current->thread.fpcntl, 12);
289                 memcpy(fpu->fpregs, current->thread.fp, 96);
290                 /* Convert internal fpu reg representation
291                  * into long double format
292                  */
293                 for (i = 0; i < 24; i += 3)
294                         fpu->fpregs[i] = ((fpu->fpregs[i] & 0xffff0000) << 15) |
295                                          ((fpu->fpregs[i] & 0x0000ffff) << 16);
296                 return 1;
297         }
298
299         /* First dump the fpu context to avoid protocol violation.  */
300         asm volatile ("fsave %0" :: "m" (fpustate[0]) : "memory");
301         if (!CPU_IS_060 ? !fpustate[0] : !fpustate[2])
302                 return 0;
303
304         asm volatile ("fmovem %/fpiar/%/fpcr/%/fpsr,%0"
305                 :: "m" (fpu->fpcntl[0])
306                 : "memory");
307         asm volatile ("fmovemx %/fp0-%/fp7,%0"
308                 :: "m" (fpu->fpregs[0])
309                 : "memory");
310         return 1;
311 }
312 EXPORT_SYMBOL(dump_fpu);
313
314 /*
315  * sys_execve() executes a new program.
316  */
317 asmlinkage int sys_execve(const char __user *name,
318                           const char __user *const __user *argv,
319                           const char __user *const __user *envp)
320 {
321         int error;
322         char * filename;
323         struct pt_regs *regs = (struct pt_regs *) &name;
324
325         filename = getname(name);
326         error = PTR_ERR(filename);
327         if (IS_ERR(filename))
328                 return error;
329         error = do_execve(filename, argv, envp, regs);
330         putname(filename);
331         return error;
332 }
333
334 unsigned long get_wchan(struct task_struct *p)
335 {
336         unsigned long fp, pc;
337         unsigned long stack_page;
338         int count = 0;
339         if (!p || p == current || p->state == TASK_RUNNING)
340                 return 0;
341
342         stack_page = (unsigned long)task_stack_page(p);
343         fp = ((struct switch_stack *)p->thread.ksp)->a6;
344         do {
345                 if (fp < stack_page+sizeof(struct thread_info) ||
346                     fp >= 8184+stack_page)
347                         return 0;
348                 pc = ((unsigned long *)fp)[1];
349                 if (!in_sched_functions(pc))
350                         return pc;
351                 fp = *(unsigned long *) fp;
352         } while (count++ < 16);
353         return 0;
354 }