2 * linux/arch/m68k/kernel/process.c
4 * Copyright (C) 1995 Hamish Macdonald
6 * 68060 fixes by Jesper Skov
10 * This file handles the architecture-dependent parts of process handling..
13 #include <linux/config.h>
14 #include <linux/errno.h>
15 #include <linux/sched.h>
16 #include <linux/kernel.h>
18 #include <linux/smp.h>
19 #include <linux/smp_lock.h>
20 #include <linux/stddef.h>
21 #include <linux/unistd.h>
22 #include <linux/ptrace.h>
23 #include <linux/slab.h>
24 #include <linux/user.h>
25 #include <linux/a.out.h>
26 #include <linux/reboot.h>
28 #include <asm/uaccess.h>
29 #include <asm/system.h>
30 #include <asm/traps.h>
31 #include <asm/machdep.h>
32 #include <asm/setup.h>
33 #include <asm/pgtable.h>
36 * Initial task structure. Make this a per-architecture thing,
37 * because different architectures tend to have different
38 * alignment requirements and potentially different initial
41 static struct fs_struct init_fs = INIT_FS;
42 static struct files_struct init_files = INIT_FILES;
43 static struct signal_struct init_signals = INIT_SIGNALS;
44 struct mm_struct init_mm = INIT_MM(init_mm);
46 union task_union init_task_union
47 __attribute__((section("init_task"), aligned(KTHREAD_SIZE)))
48 = { task: INIT_TASK(init_task_union.task) };
50 asmlinkage void ret_from_fork(void);
54 * The idle loop on an m68k..
56 static void default_idle(void)
59 if (!current->need_resched)
60 #if defined(MACH_ATARI_ONLY) && !defined(CONFIG_HADES)
61 /* block out HSYNC on the atari (falcon) */
62 __asm__("stop #0x2200" : : : "cc");
64 __asm__("stop #0x2000" : : : "cc");
71 void (*idle)(void) = default_idle;
74 * The idle thread. There's no useful work to be
75 * done, so just try to conserve power and have a
76 * low exit latency (ie sit in a loop waiting for
77 * somebody to say that they'd like to reschedule)
81 /* endless idle loop with no priority at all */
84 current->counter = -100;
88 void machine_restart(char * __unused)
95 void machine_halt(void)
102 void machine_power_off(void)
109 void show_regs(struct pt_regs * regs)
112 printk("Format %02x Vector: %04x PC: %08lx Status: %04x %s\n",
113 regs->format, regs->vector, regs->pc, regs->sr, print_tainted());
114 printk("ORIG_D0: %08lx D0: %08lx A2: %08lx A1: %08lx\n",
115 regs->orig_d0, regs->d0, regs->a2, regs->a1);
116 printk("A0: %08lx D5: %08lx D4: %08lx\n",
117 regs->a0, regs->d5, regs->d4);
118 printk("D3: %08lx D2: %08lx D1: %08lx\n",
119 regs->d3, regs->d2, regs->d1);
120 if (!(regs->sr & PS_S))
121 printk("USP: %08lx\n", rdusp());
125 * Create a kernel thread
127 int arch_kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
136 register long retval __asm__ ("d0");
137 register long clone_arg __asm__ ("d1") = flags | CLONE_VM;
142 "trap #0\n\t" /* Linux/m68k system call */
143 "tstl %0\n\t" /* child or parent */
144 "jne 1f\n\t" /* parent - jump */
145 "lea %%sp@(%c7),%6\n\t" /* reload current */
146 "movel %3,%%sp@-\n\t" /* push argument */
147 "jsr %4@\n\t" /* call fn */
148 "movel %0,%%d1\n\t" /* pass exit value */
149 "movel %2,%%d0\n\t" /* exit */
153 : "i" (__NR_clone), "i" (__NR_exit),
154 "r" (arg), "a" (fn), "d" (clone_arg), "r" (current),
165 void flush_thread(void)
167 unsigned long zero = 0;
169 current->thread.fs = __USER_DS;
171 asm volatile (".chip 68k/68881\n\t"
173 ".chip 68k" : : "a" (&zero));
177 * "m68k_fork()".. By the time we get here, the
178 * non-volatile registers have also been saved on the
179 * stack. We do some ugly pointer stuff here.. (see
183 asmlinkage int m68k_fork(struct pt_regs *regs)
185 return do_fork(SIGCHLD, rdusp(), regs, 0);
188 asmlinkage int m68k_vfork(struct pt_regs *regs)
190 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, rdusp(), regs, 0);
193 asmlinkage int m68k_clone(struct pt_regs *regs)
195 unsigned long clone_flags;
198 /* syscall2 puts clone_flags in d1 and usp in d2 */
199 clone_flags = regs->d1;
203 return do_fork(clone_flags, newsp, regs, 0);
206 int copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
207 unsigned long unused,
208 struct task_struct * p, struct pt_regs * regs)
210 struct pt_regs * childregs;
211 struct switch_stack * childstack, *stack;
212 unsigned long stack_offset, *retp;
214 stack_offset = KTHREAD_SIZE - sizeof(struct pt_regs);
215 childregs = (struct pt_regs *) ((unsigned long) p + stack_offset);
220 retp = ((unsigned long *) regs);
221 stack = ((struct switch_stack *) retp) - 1;
223 childstack = ((struct switch_stack *) childregs) - 1;
224 *childstack = *stack;
225 childstack->retpc = (unsigned long)ret_from_fork;
228 p->thread.ksp = (unsigned long)childstack;
230 * Must save the current SFC/DFC value, NOT the value when
231 * the parent was last descheduled - RGH 10-08-96
233 p->thread.fs = get_fs().seg;
236 /* Copy the current fpu state */
237 asm volatile ("fsave %0" : : "m" (p->thread.fpstate[0]) : "memory");
239 if (!CPU_IS_060 ? p->thread.fpstate[0] : p->thread.fpstate[2])
240 asm volatile ("fmovemx %/fp0-%/fp7,%0\n\t"
241 "fmoveml %/fpiar/%/fpcr/%/fpsr,%1"
242 : : "m" (p->thread.fp[0]), "m" (p->thread.fpcntl[0])
244 /* Restore the state in case the fpu was busy */
245 asm volatile ("frestore %0" : : "m" (p->thread.fpstate[0]));
251 /* Fill in the fpu structure for a core dump. */
253 int dump_fpu (struct pt_regs *regs, struct user_m68kfp_struct *fpu)
260 memcpy(fpu->fpcntl, current->thread.fpcntl, 12);
261 memcpy(fpu->fpregs, current->thread.fp, 96);
262 /* Convert internal fpu reg representation
263 * into long double format
265 for (i = 0; i < 24; i += 3)
266 fpu->fpregs[i] = ((fpu->fpregs[i] & 0xffff0000) << 15) |
267 ((fpu->fpregs[i] & 0x0000ffff) << 16);
271 /* First dump the fpu context to avoid protocol violation. */
272 asm volatile ("fsave %0" :: "m" (fpustate[0]) : "memory");
273 if (!CPU_IS_060 ? !fpustate[0] : !fpustate[2])
276 asm volatile ("fmovem %/fpiar/%/fpcr/%/fpsr,%0"
277 :: "m" (fpu->fpcntl[0])
279 asm volatile ("fmovemx %/fp0-%/fp7,%0"
280 :: "m" (fpu->fpregs[0])
286 * fill in the user structure for a core dump..
288 void dump_thread(struct pt_regs * regs, struct user * dump)
290 struct switch_stack *sw;
292 /* changed the size calculations - should hopefully work better. lbt */
293 dump->magic = CMAGIC;
294 dump->start_code = 0;
295 dump->start_stack = rdusp() & ~(PAGE_SIZE - 1);
296 dump->u_tsize = ((unsigned long) current->mm->end_code) >> PAGE_SHIFT;
297 dump->u_dsize = ((unsigned long) (current->mm->brk +
298 (PAGE_SIZE-1))) >> PAGE_SHIFT;
299 dump->u_dsize -= dump->u_tsize;
302 if (dump->start_stack < TASK_SIZE)
303 dump->u_ssize = ((unsigned long) (TASK_SIZE - dump->start_stack)) >> PAGE_SHIFT;
305 dump->u_ar0 = (struct user_regs_struct *)((int)&dump->regs - (int)dump);
306 sw = ((struct switch_stack *)regs) - 1;
307 dump->regs.d1 = regs->d1;
308 dump->regs.d2 = regs->d2;
309 dump->regs.d3 = regs->d3;
310 dump->regs.d4 = regs->d4;
311 dump->regs.d5 = regs->d5;
312 dump->regs.d6 = sw->d6;
313 dump->regs.d7 = sw->d7;
314 dump->regs.a0 = regs->a0;
315 dump->regs.a1 = regs->a1;
316 dump->regs.a2 = regs->a2;
317 dump->regs.a3 = sw->a3;
318 dump->regs.a4 = sw->a4;
319 dump->regs.a5 = sw->a5;
320 dump->regs.a6 = sw->a6;
321 dump->regs.d0 = regs->d0;
322 dump->regs.orig_d0 = regs->orig_d0;
323 dump->regs.stkadj = regs->stkadj;
324 dump->regs.sr = regs->sr;
325 dump->regs.pc = regs->pc;
326 dump->regs.fmtvec = (regs->format << 12) | regs->vector;
327 /* dump floating point stuff */
328 dump->u_fpvalid = dump_fpu (regs, &dump->m68kfp);
332 * sys_execve() executes a new program.
334 asmlinkage int sys_execve(char *name, char **argv, char **envp)
338 struct pt_regs *regs = (struct pt_regs *) &name;
341 filename = getname(name);
342 error = PTR_ERR(filename);
343 if (IS_ERR(filename))
345 error = do_execve(filename, argv, envp, regs);
353 * These bracket the sleeping functions..
355 extern void scheduling_functions_start_here(void);
356 extern void scheduling_functions_end_here(void);
357 #define first_sched ((unsigned long) scheduling_functions_start_here)
358 #define last_sched ((unsigned long) scheduling_functions_end_here)
360 unsigned long get_wchan(struct task_struct *p)
362 unsigned long fp, pc;
363 unsigned long stack_page;
365 if (!p || p == current || p->state == TASK_RUNNING)
368 stack_page = (unsigned long)p;
369 fp = ((struct switch_stack *)p->thread.ksp)->a6;
371 if (fp < stack_page+sizeof(struct task_struct) ||
372 fp >= 8184+stack_page)
374 pc = ((unsigned long *)fp)[1];
375 /* FIXME: This depends on the order of these functions. */
376 if (pc < first_sched || pc >= last_sched)
378 fp = *(unsigned long *) fp;
379 } while (count++ < 16);