]> rtime.felk.cvut.cz Git - mcf548x/linux.git/blob - arch/m68k/kernel/process.c
Current (FEC from 2.6.31 port, no CAN, no I2C, no PCI)
[mcf548x/linux.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         
190 #ifndef CONFIG_COLDFIRE
191         current->thread.fs = __USER_DS;
192         if (!FPU_IS_EMU)
193                 asm volatile (".chip 68k/68881\n\t"
194                               "frestore %0@\n\t"
195                               ".chip 68k" : : "a" (&zero));
196 #else
197         current->thread.fs = USER_DS;
198
199 #ifdef CONFIG_FPU
200         if (!FPU_IS_EMU)
201                 asm volatile ("frestore %0@\n\t" : : "a" (&zero));
202 #endif
203 #endif
204 }
205
206 /*
207  * "m68k_fork()".. By the time we get here, the
208  * non-volatile registers have also been saved on the
209  * stack. We do some ugly pointer stuff here.. (see
210  * also copy_thread)
211  */
212
213 asmlinkage int m68k_fork(struct pt_regs *regs)
214 {
215         return do_fork(SIGCHLD, rdusp(), regs, 0, NULL, NULL);
216 }
217
218 asmlinkage int m68k_vfork(struct pt_regs *regs)
219 {
220         return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, rdusp(), regs, 0,
221                        NULL, NULL);
222 }
223
224 asmlinkage int m68k_clone(struct pt_regs *regs)
225 {
226         unsigned long clone_flags;
227         unsigned long newsp;
228         int __user *parent_tidptr, *child_tidptr;
229
230         /* syscall2 puts clone_flags in d1 and usp in d2 */
231         clone_flags = regs->d1;
232         newsp = regs->d2;
233         parent_tidptr = (int __user *)regs->d3;
234         child_tidptr = (int __user *)regs->d4;
235         if (!newsp)
236                 newsp = rdusp();
237         return do_fork(clone_flags, newsp, regs, 0,
238                        parent_tidptr, child_tidptr);
239 }
240
241 int copy_thread(unsigned long clone_flags, unsigned long usp,
242                  unsigned long unused,
243                  struct task_struct * p, struct pt_regs * regs)
244 {
245         struct pt_regs * childregs;
246         struct switch_stack * childstack, *stack;
247         unsigned long *retp;
248
249         childregs = (struct pt_regs *) (task_stack_page(p) + THREAD_SIZE) - 1;
250
251         *childregs = *regs;
252         childregs->d0 = 0;
253
254         retp = ((unsigned long *) regs);
255         stack = ((struct switch_stack *) retp) - 1;
256
257         childstack = ((struct switch_stack *) childregs) - 1;
258         *childstack = *stack;
259         childstack->retpc = (unsigned long)ret_from_fork;
260
261         p->thread.usp = usp;
262         p->thread.ksp = (unsigned long)childstack;
263
264         if (clone_flags & CLONE_SETTLS)
265                 task_thread_info(p)->tp_value = regs->d5;
266
267         /*
268          * Must save the current SFC/DFC value, NOT the value when
269          * the parent was last descheduled - RGH  10-08-96
270          */
271 #ifndef CONFIG_COLDFIRE 
272         p->thread.fs = get_fs().seg;
273
274         if (!FPU_IS_EMU) {
275                 /* Copy the current fpu state */
276                 asm volatile ("fsave %0" : : "m" (p->thread.fpstate[0]) : "memory");
277
278                 if (!CPU_IS_060 ? p->thread.fpstate[0] : p->thread.fpstate[2])
279                   asm volatile ("fmovemx %/fp0-%/fp7,%0\n\t"
280                                 "fmoveml %/fpiar/%/fpcr/%/fpsr,%1"
281                                 : : "m" (p->thread.fp[0]), "m" (p->thread.fpcntl[0])
282                                 : "memory");
283                 /* Restore the state in case the fpu was busy */
284                 asm volatile ("frestore %0" : : "m" (p->thread.fpstate[0]));
285         }
286 #else
287         p->thread.fs = get_fs(); 
288 #if defined(CONFIG_FPU)
289         if (!FPU_IS_EMU) {
290                 /* Copy the current fpu state */
291                 asm volatile ("fsave %0" : : "m" (p->thread.fpstate[0])
292                                 : "memory");
293
294                 if (p->thread.fpstate[0]) {
295                         asm volatile ("fmovemd %/fp0-%/fp7,%0"
296                                                                 : : "m" (p->thread.fp[0])
297                                                                 : "memory");
298                         asm volatile ("fmovel %/fpiar,%0"
299                                                                 : : "m" (p->thread.fpcntl[0])
300                                                                 : "memory");
301                         asm volatile ("fmovel %/fpcr,%0"
302                                                                 : : "m" (p->thread.fpcntl[1])
303                                                                 : "memory");
304                         asm volatile ("fmovel %/fpsr,%0"
305                                                                 : : "m" (p->thread.fpcntl[2])
306                                                                 : "memory");
307                 }
308                 /* Restore the state in case the fpu was busy */
309                 asm volatile ("frestore %0" : : "m" (p->thread.fpstate[0]));
310         }
311 #endif
312 #endif
313         return 0;
314 }
315
316 /* Fill in the fpu structure for a core dump.  */
317
318 int dump_fpu (struct pt_regs *regs, struct user_m68kfp_struct *fpu)
319 {
320         char fpustate[216];
321
322         if (FPU_IS_EMU) {
323                 int i;
324
325                 memcpy(fpu->fpcntl, current->thread.fpcntl, 12);
326                 memcpy(fpu->fpregs, current->thread.fp, 96);
327                 /* Convert internal fpu reg representation
328                  * into long double format
329                  */
330                 for (i = 0; i < 24; i += 3)
331                         fpu->fpregs[i] = ((fpu->fpregs[i] & 0xffff0000) << 15) |
332                                          ((fpu->fpregs[i] & 0x0000ffff) << 16);
333                 return 1;
334         }
335
336         /* First dump the fpu context to avoid protocol violation.  */
337 #ifndef CONFIG_COLDFIRE
338         asm volatile ("fsave %0" :: "m" (fpustate[0]) : "memory");
339         if (!CPU_IS_060 ? !fpustate[0] : !fpustate[2])
340                 return 0;
341
342         asm volatile ("fmovem %/fpiar/%/fpcr/%/fpsr,%0"
343                 :: "m" (fpu->fpcntl[0])
344                 : "memory");
345         asm volatile ("fmovemx %/fp0-%/fp7,%0"
346                 :: "m" (fpu->fpregs[0])
347                 : "memory");
348 #elif CONFIG_FPU
349         /* COLDFIRE + FPU */
350         asm volatile ("fsave %0" :: "m" (fpustate[0]) : "memory");
351         if (!CPU_IS_060 ? !fpustate[0] : !fpustate[2])
352                 return 0;
353
354         asm volatile ("fmovel %/fpiar,%0"
355                 : : "m" (fpu->fpcntl[0])
356                 : "memory");
357         asm volatile ("fmovel %/fpcr,%0"
358                 : : "m" (fpu->fpcntl[1])
359                 : "memory");
360         asm volatile ("fmovel %/fpsr,%0"
361                 : : "m" (fpu->fpcntl[2])
362                 : "memory");
363         asm volatile ("fmovemd %/fp0-%/fp7,%0"
364                 : : "m" (fpu->fpregs[0])
365                 : "memory");
366 #endif  
367         return 1;
368 }
369 EXPORT_SYMBOL(dump_fpu);
370
371 /*
372  * sys_execve() executes a new program.
373  */
374 asmlinkage int sys_execve(const char __user *name,
375                           const char __user *const __user *argv,
376                           const char __user *const __user *envp)
377 {
378         int error;
379         char * filename;
380         struct pt_regs *regs = (struct pt_regs *) &name;
381
382         filename = getname(name);
383         error = PTR_ERR(filename);
384         if (IS_ERR(filename))
385                 return error;
386         error = do_execve(filename, argv, envp, regs);
387         putname(filename);
388         return error;
389 }
390
391 unsigned long get_wchan(struct task_struct *p)
392 {
393         unsigned long fp, pc;
394         unsigned long stack_page;
395         int count = 0;
396         if (!p || p == current || p->state == TASK_RUNNING)
397                 return 0;
398
399         stack_page = (unsigned long)task_stack_page(p);
400         fp = ((struct switch_stack *)p->thread.ksp)->a6;
401         do {
402                 if (fp < stack_page+sizeof(struct thread_info) ||
403                     fp >= 8184+stack_page)
404                         return 0;
405                 pc = ((unsigned long *)fp)[1];
406                 if (!in_sched_functions(pc))
407                         return pc;
408                 fp = *(unsigned long *) fp;
409         } while (count++ < 16);
410         return 0;
411 }