blob: f7fe6c3485954e9754c353a1c48c81b09c41b7fd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/module.h>
17#include <linux/errno.h>
18#include <linux/sched.h>
19#include <linux/kernel.h>
20#include <linux/mm.h>
21#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/stddef.h>
23#include <linux/unistd.h>
24#include <linux/ptrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/user.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/interrupt.h>
27#include <linux/reboot.h>
Greg Ungerer07ef3092007-08-24 09:26:06 +100028#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#include <asm/uaccess.h>
32#include <asm/system.h>
33#include <asm/traps.h>
34#include <asm/machdep.h>
35#include <asm/setup.h>
36#include <asm/pgtable.h>
37
38asmlinkage void ret_from_fork(void);
39
Greg Ungerer10c1f712006-02-08 09:19:17 +100040/*
41 * The following aren't currently used.
42 */
43void (*pm_idle)(void);
44EXPORT_SYMBOL(pm_idle);
45
46void (*pm_power_off)(void);
47EXPORT_SYMBOL(pm_power_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49/*
50 * The idle loop on an m68knommu..
51 */
Adrian Bunkcdb04522006-03-24 03:15:57 -080052static void default_idle(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
Greg Ungererb05a7202005-06-03 11:35:20 +100054 local_irq_disable();
55 while (!need_resched()) {
56 /* This stop will re-enable interrupts */
57 __asm__("stop #0x2000" : : : "cc");
58 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 }
Greg Ungererb05a7202005-06-03 11:35:20 +100060 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070061}
62
63void (*idle)(void) = default_idle;
64
65/*
66 * The idle thread. There's no useful work to be
67 * done, so just try to conserve power and have a
68 * low exit latency (ie sit in a loop waiting for
69 * somebody to say that they'd like to reschedule)
70 */
71void cpu_idle(void)
72{
73 /* endless idle loop with no priority at all */
Greg Ungererb05a7202005-06-03 11:35:20 +100074 while (1) {
75 idle();
Thomas Gleixnerbd2f5532011-03-21 12:33:18 +010076 schedule_preempt_disabled();
Greg Ungererb05a7202005-06-03 11:35:20 +100077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
80void machine_restart(char * __unused)
81{
82 if (mach_reset)
83 mach_reset();
84 for (;;);
85}
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087void machine_halt(void)
88{
89 if (mach_halt)
90 mach_halt();
91 for (;;);
92}
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094void machine_power_off(void)
95{
96 if (mach_power_off)
97 mach_power_off();
98 for (;;);
99}
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101void show_regs(struct pt_regs * regs)
102{
103 printk(KERN_NOTICE "\n");
104 printk(KERN_NOTICE "Format %02x Vector: %04x PC: %08lx Status: %04x %s\n",
105 regs->format, regs->vector, regs->pc, regs->sr, print_tainted());
106 printk(KERN_NOTICE "ORIG_D0: %08lx D0: %08lx A2: %08lx A1: %08lx\n",
107 regs->orig_d0, regs->d0, regs->a2, regs->a1);
108 printk(KERN_NOTICE "A0: %08lx D5: %08lx D4: %08lx\n",
109 regs->a0, regs->d5, regs->d4);
110 printk(KERN_NOTICE "D3: %08lx D2: %08lx D1: %08lx\n",
111 regs->d3, regs->d2, regs->d1);
112 if (!(regs->sr & PS_S))
113 printk(KERN_NOTICE "USP: %08lx\n", rdusp());
114}
115
116/*
117 * Create a kernel thread
118 */
119int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
120{
121 int retval;
122 long clone_arg = flags | CLONE_VM;
123 mm_segment_t fs;
124
125 fs = get_fs();
126 set_fs(KERNEL_DS);
127
128 __asm__ __volatile__ (
129 "movel %%sp, %%d2\n\t"
130 "movel %5, %%d1\n\t"
131 "movel %1, %%d0\n\t"
132 "trap #0\n\t"
133 "cmpl %%sp, %%d2\n\t"
134 "jeq 1f\n\t"
135 "movel %3, %%sp@-\n\t"
136 "jsr %4@\n\t"
137 "movel %2, %%d0\n\t"
138 "trap #0\n"
139 "1:\n\t"
140 "movel %%d0, %0\n"
141 : "=d" (retval)
142 : "i" (__NR_clone),
143 "i" (__NR_exit),
144 "a" (arg),
145 "a" (fn),
146 "a" (clone_arg)
147 : "cc", "%d0", "%d1", "%d2");
148
149 set_fs(fs);
150 return retval;
151}
Greg Ungererdcc4d722011-03-29 14:14:21 +1000152EXPORT_SYMBOL(kernel_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154void flush_thread(void)
155{
156#ifdef CONFIG_FPU
157 unsigned long zero = 0;
158#endif
Mathias Krauseb7de1102011-07-25 17:12:42 -0700159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 current->thread.fs = __USER_DS;
161#ifdef CONFIG_FPU
162 if (!FPU_IS_EMU)
163 asm volatile (".chip 68k/68881\n\t"
Andreas Schwab2a353502012-01-09 15:10:15 +0100164 "frestore %0\n\t"
165 ".chip 68k" : : "m" (zero));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166#endif
167}
168
169/*
170 * "m68k_fork()".. By the time we get here, the
171 * non-volatile registers have also been saved on the
172 * stack. We do some ugly pointer stuff here.. (see
173 * also copy_thread)
174 */
175
176asmlinkage int m68k_fork(struct pt_regs *regs)
177{
178 /* fork almost works, enough to trick you into looking elsewhere :-( */
179 return(-EINVAL);
180}
181
182asmlinkage int m68k_vfork(struct pt_regs *regs)
183{
184 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, rdusp(), regs, 0, NULL, NULL);
185}
186
187asmlinkage int m68k_clone(struct pt_regs *regs)
188{
189 unsigned long clone_flags;
190 unsigned long newsp;
191
192 /* syscall2 puts clone_flags in d1 and usp in d2 */
193 clone_flags = regs->d1;
194 newsp = regs->d2;
195 if (!newsp)
196 newsp = rdusp();
197 return do_fork(clone_flags, newsp, regs, 0, NULL, NULL);
198}
199
Alexey Dobriyan6f2c55b2009-04-02 16:56:59 -0700200int copy_thread(unsigned long clone_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 unsigned long usp, unsigned long topstk,
202 struct task_struct * p, struct pt_regs * regs)
203{
204 struct pt_regs * childregs;
205 struct switch_stack * childstack, *stack;
Al Viro513091b2006-01-12 01:05:54 -0800206 unsigned long *retp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Al Viro513091b2006-01-12 01:05:54 -0800208 childregs = (struct pt_regs *) (task_stack_page(p) + THREAD_SIZE) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 *childregs = *regs;
211 childregs->d0 = 0;
212
213 retp = ((unsigned long *) regs);
214 stack = ((struct switch_stack *) retp) - 1;
215
216 childstack = ((struct switch_stack *) childregs) - 1;
217 *childstack = *stack;
218 childstack->retpc = (unsigned long)ret_from_fork;
219
220 p->thread.usp = usp;
221 p->thread.ksp = (unsigned long)childstack;
Maxim Kuvyrkova58f7532009-12-06 10:08:14 -0800222
223 if (clone_flags & CLONE_SETTLS)
224 task_thread_info(p)->tp_value = regs->d5;
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 /*
227 * Must save the current SFC/DFC value, NOT the value when
228 * the parent was last descheduled - RGH 10-08-96
229 */
230 p->thread.fs = get_fs().seg;
231
232#ifdef CONFIG_FPU
233 if (!FPU_IS_EMU) {
234 /* Copy the current fpu state */
235 asm volatile ("fsave %0" : : "m" (p->thread.fpstate[0]) : "memory");
236
237 if (p->thread.fpstate[0])
238 asm volatile ("fmovemx %/fp0-%/fp7,%0\n\t"
239 "fmoveml %/fpiar/%/fpcr/%/fpsr,%1"
240 : : "m" (p->thread.fp[0]), "m" (p->thread.fpcntl[0])
241 : "memory");
242 /* Restore the state in case the fpu was busy */
243 asm volatile ("frestore %0" : : "m" (p->thread.fpstate[0]));
244 }
245#endif
246
247 return 0;
248}
249
250/* Fill in the fpu structure for a core dump. */
251
252int dump_fpu(struct pt_regs *regs, struct user_m68kfp_struct *fpu)
253{
254#ifdef CONFIG_FPU
255 char fpustate[216];
256
257 if (FPU_IS_EMU) {
258 int i;
259
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
264 */
265 for (i = 0; i < 24; i += 3)
266 fpu->fpregs[i] = ((fpu->fpregs[i] & 0xffff0000) << 15) |
267 ((fpu->fpregs[i] & 0x0000ffff) << 16);
268 return 1;
269 }
270
271 /* First dump the fpu context to avoid protocol violation. */
272 asm volatile ("fsave %0" :: "m" (fpustate[0]) : "memory");
273 if (!fpustate[0])
274 return 0;
275
276 asm volatile ("fmovem %/fpiar/%/fpcr/%/fpsr,%0"
277 :: "m" (fpu->fpcntl[0])
278 : "memory");
279 asm volatile ("fmovemx %/fp0-%/fp7,%0"
280 :: "m" (fpu->fpregs[0])
281 : "memory");
282#endif
283 return 1;
284}
Greg Ungerer67cc09e2011-03-29 14:03:26 +1000285EXPORT_SYMBOL(dump_fpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 * Generic dumping code. Used for panic and debug.
289 */
290void dump(struct pt_regs *fp)
291{
292 unsigned long *sp;
293 unsigned char *tp;
294 int i;
295
Joe Perchesad361c92009-07-06 13:05:40 -0700296 printk(KERN_EMERG "\nCURRENT PROCESS:\n\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 printk(KERN_EMERG "COMM=%s PID=%d\n", current->comm, current->pid);
298
299 if (current->mm) {
300 printk(KERN_EMERG "TEXT=%08x-%08x DATA=%08x-%08x BSS=%08x-%08x\n",
301 (int) current->mm->start_code,
302 (int) current->mm->end_code,
303 (int) current->mm->start_data,
304 (int) current->mm->end_data,
305 (int) current->mm->end_data,
306 (int) current->mm->brk);
Joe Perchesad361c92009-07-06 13:05:40 -0700307 printk(KERN_EMERG "USER-STACK=%08x KERNEL-STACK=%08x\n\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 (int) current->mm->start_stack,
309 (int)(((unsigned long) current) + THREAD_SIZE));
310 }
311
312 printk(KERN_EMERG "PC: %08lx\n", fp->pc);
313 printk(KERN_EMERG "SR: %08lx SP: %08lx\n", (long) fp->sr, (long) fp);
314 printk(KERN_EMERG "d0: %08lx d1: %08lx d2: %08lx d3: %08lx\n",
315 fp->d0, fp->d1, fp->d2, fp->d3);
316 printk(KERN_EMERG "d4: %08lx d5: %08lx a0: %08lx a1: %08lx\n",
317 fp->d4, fp->d5, fp->a0, fp->a1);
Kulikov Vasiliyc7484cf2010-07-14 22:01:13 +0400318 printk(KERN_EMERG "\nUSP: %08x TRAPFRAME: %p\n",
319 (unsigned int) rdusp(), fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Joe Perchesad361c92009-07-06 13:05:40 -0700321 printk(KERN_EMERG "\nCODE:");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 tp = ((unsigned char *) fp->pc) - 0x20;
323 for (sp = (unsigned long *) tp, i = 0; (i < 0x40); i += 4) {
324 if ((i % 0x10) == 0)
Kulikov Vasiliyc7484cf2010-07-14 22:01:13 +0400325 printk(KERN_EMERG "%p: ", tp + i);
Greg Ungerer19127e22006-12-04 17:26:55 +1000326 printk("%08x ", (int) *sp++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
Joe Perchesad361c92009-07-06 13:05:40 -0700328 printk(KERN_EMERG "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Greg Ungerer19127e22006-12-04 17:26:55 +1000330 printk(KERN_EMERG "KERNEL STACK:");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 tp = ((unsigned char *) fp) - 0x40;
332 for (sp = (unsigned long *) tp, i = 0; (i < 0xc0); i += 4) {
333 if ((i % 0x10) == 0)
Kulikov Vasiliyc7484cf2010-07-14 22:01:13 +0400334 printk(KERN_EMERG "%p: ", tp + i);
Greg Ungerer19127e22006-12-04 17:26:55 +1000335 printk("%08x ", (int) *sp++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
Joe Perchesad361c92009-07-06 13:05:40 -0700337 printk(KERN_EMERG "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Greg Ungerer19127e22006-12-04 17:26:55 +1000339 printk(KERN_EMERG "USER STACK:");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 tp = (unsigned char *) (rdusp() - 0x10);
341 for (sp = (unsigned long *) tp, i = 0; (i < 0x80); i += 4) {
342 if ((i % 0x10) == 0)
Kulikov Vasiliyc7484cf2010-07-14 22:01:13 +0400343 printk(KERN_EMERG "%p: ", tp + i);
Greg Ungerer19127e22006-12-04 17:26:55 +1000344 printk("%08x ", (int) *sp++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
Joe Perchesad361c92009-07-06 13:05:40 -0700346 printk(KERN_EMERG "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348
349/*
350 * sys_execve() executes a new program.
351 */
David Howellsd7627462010-08-17 23:52:56 +0100352asmlinkage int sys_execve(const char *name,
353 const char *const *argv,
354 const char *const *envp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
356 int error;
357 char * filename;
358 struct pt_regs *regs = (struct pt_regs *) &name;
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 filename = getname(name);
361 error = PTR_ERR(filename);
362 if (IS_ERR(filename))
John Kacureb7371d2009-10-12 23:37:28 +0200363 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 error = do_execve(filename, argv, envp, regs);
365 putname(filename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return error;
367}
368
369unsigned long get_wchan(struct task_struct *p)
370{
371 unsigned long fp, pc;
372 unsigned long stack_page;
373 int count = 0;
374 if (!p || p == current || p->state == TASK_RUNNING)
375 return 0;
376
377 stack_page = (unsigned long)p;
378 fp = ((struct switch_stack *)p->thread.ksp)->a6;
379 do {
380 if (fp < stack_page+sizeof(struct thread_info) ||
Greg Ungerer45a82f52007-07-15 23:38:31 -0700381 fp >= THREAD_SIZE-8+stack_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return 0;
383 pc = ((unsigned long *)fp)[1];
384 if (!in_sched_functions(pc))
385 return pc;
386 fp = *(unsigned long *) fp;
387 } while (count++ < 16);
388 return 0;
389}
390
391/*
392 * Return saved PC of a blocked thread.
393 */
394unsigned long thread_saved_pc(struct task_struct *tsk)
395{
396 struct switch_stack *sw = (struct switch_stack *)tsk->thread.ksp;
397
398 /* Check whether the thread is blocked in resume() */
399 if (in_sched_functions(sw->retpc))
400 return ((unsigned long *)sw->a6)[1];
401 else
402 return sw->retpc;
403}
404