blob: b3d4760c975003cbb2c065d4a17a7c7b57a97ca0 [file] [log] [blame]
Greg Ungererfde39442012-02-09 14:18:46 +10001/*
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>
Greg Ungererfde39442012-02-09 14:18:46 +100030#include <asm/traps.h>
31#include <asm/machdep.h>
32#include <asm/setup.h>
33#include <asm/pgtable.h>
34
35
36asmlinkage void ret_from_fork(void);
Al Viro533e6902012-09-16 12:05:09 -040037asmlinkage void ret_from_kernel_thread(void);
Greg Ungererfde39442012-02-09 14:18:46 +100038
39
40/*
41 * Return saved PC from a blocked thread
42 */
43unsigned long thread_saved_pc(struct task_struct *tsk)
44{
45 struct switch_stack *sw = (struct switch_stack *)tsk->thread.ksp;
46 /* Check whether the thread is blocked in resume() */
47 if (in_sched_functions(sw->retpc))
48 return ((unsigned long *)sw->a6)[1];
49 else
50 return sw->retpc;
51}
52
53/*
54 * The idle loop on an m68k..
55 */
56static void default_idle(void)
57{
58 if (!need_resched())
59#if defined(MACH_ATARI_ONLY)
60 /* block out HSYNC on the atari (falcon) */
61 __asm__("stop #0x2200" : : : "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#else
Greg Ungererfde39442012-02-09 14:18:46 +100063 __asm__("stop #0x2000" : : : "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#endif
Greg Ungererfde39442012-02-09 14:18:46 +100065}
66
67void (*idle)(void) = default_idle;
68
69/*
70 * The idle thread. There's no useful work to be
71 * done, so just try to conserve power and have a
72 * low exit latency (ie sit in a loop waiting for
73 * somebody to say that they'd like to reschedule)
74 */
75void cpu_idle(void)
76{
77 /* endless idle loop with no priority at all */
78 while (1) {
79 while (!need_resched())
80 idle();
Linus Torvaldsb57cb722012-03-21 18:17:51 -070081 schedule_preempt_disabled();
Greg Ungererfde39442012-02-09 14:18:46 +100082 }
83}
84
85void machine_restart(char * __unused)
86{
87 if (mach_reset)
88 mach_reset();
89 for (;;);
90}
91
92void machine_halt(void)
93{
94 if (mach_halt)
95 mach_halt();
96 for (;;);
97}
98
99void machine_power_off(void)
100{
101 if (mach_power_off)
102 mach_power_off();
103 for (;;);
104}
105
106void (*pm_power_off)(void) = machine_power_off;
107EXPORT_SYMBOL(pm_power_off);
108
109void show_regs(struct pt_regs * regs)
110{
111 printk("\n");
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());
122}
123
Greg Ungererfde39442012-02-09 14:18:46 +1000124void flush_thread(void)
125{
126 current->thread.fs = __USER_DS;
127#ifdef CONFIG_FPU
128 if (!FPU_IS_EMU) {
129 unsigned long zero = 0;
130 asm volatile("frestore %0": :"m" (zero));
131 }
132#endif
133}
134
135/*
136 * "m68k_fork()".. By the time we get here, the
137 * non-volatile registers have also been saved on the
138 * stack. We do some ugly pointer stuff here.. (see
139 * also copy_thread)
140 */
141
142asmlinkage int m68k_fork(struct pt_regs *regs)
143{
144#ifdef CONFIG_MMU
145 return do_fork(SIGCHLD, rdusp(), regs, 0, NULL, NULL);
146#else
147 return -EINVAL;
148#endif
149}
150
151asmlinkage int m68k_vfork(struct pt_regs *regs)
152{
153 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, rdusp(), regs, 0,
154 NULL, NULL);
155}
156
157asmlinkage int m68k_clone(struct pt_regs *regs)
158{
159 unsigned long clone_flags;
160 unsigned long newsp;
161 int __user *parent_tidptr, *child_tidptr;
162
163 /* syscall2 puts clone_flags in d1 and usp in d2 */
164 clone_flags = regs->d1;
165 newsp = regs->d2;
166 parent_tidptr = (int __user *)regs->d3;
167 child_tidptr = (int __user *)regs->d4;
168 if (!newsp)
169 newsp = rdusp();
170 return do_fork(clone_flags, newsp, regs, 0,
171 parent_tidptr, child_tidptr);
172}
173
174int copy_thread(unsigned long clone_flags, unsigned long usp,
Al Viro533e6902012-09-16 12:05:09 -0400175 unsigned long arg,
Greg Ungererfde39442012-02-09 14:18:46 +1000176 struct task_struct * p, struct pt_regs * regs)
177{
178 struct pt_regs * childregs;
Al Viro533e6902012-09-16 12:05:09 -0400179 struct switch_stack *childstack;
Greg Ungererfde39442012-02-09 14:18:46 +1000180
181 childregs = (struct pt_regs *) (task_stack_page(p) + THREAD_SIZE) - 1;
Greg Ungererfde39442012-02-09 14:18:46 +1000182 childstack = ((struct switch_stack *) childregs) - 1;
Greg Ungererfde39442012-02-09 14:18:46 +1000183
184 p->thread.usp = usp;
185 p->thread.ksp = (unsigned long)childstack;
Al Viro533e6902012-09-16 12:05:09 -0400186 p->thread.esp0 = (unsigned long)childregs;
Greg Ungererfde39442012-02-09 14:18:46 +1000187
188 /*
189 * Must save the current SFC/DFC value, NOT the value when
190 * the parent was last descheduled - RGH 10-08-96
191 */
192 p->thread.fs = get_fs().seg;
193
Al Viro533e6902012-09-16 12:05:09 -0400194 if (unlikely(!regs)) {
195 /* kernel thread */
196 memset(childstack, 0,
197 sizeof(struct switch_stack) + sizeof(struct pt_regs));
198 childregs->sr = PS_S;
199 childstack->a3 = usp; /* function */
200 childstack->d7 = arg;
201 childstack->retpc = (unsigned long)ret_from_kernel_thread;
202 p->thread.usp = 0;
203 return 0;
204 }
205 *childregs = *regs;
206 childregs->d0 = 0;
207
208 *childstack = ((struct switch_stack *) regs)[-1];
209 childstack->retpc = (unsigned long)ret_from_fork;
210
211 if (clone_flags & CLONE_SETTLS)
212 task_thread_info(p)->tp_value = regs->d5;
213
Greg Ungererfde39442012-02-09 14:18:46 +1000214#ifdef CONFIG_FPU
215 if (!FPU_IS_EMU) {
216 /* Copy the current fpu state */
217 asm volatile ("fsave %0" : : "m" (p->thread.fpstate[0]) : "memory");
218
219 if (!CPU_IS_060 ? p->thread.fpstate[0] : p->thread.fpstate[2]) {
220 if (CPU_IS_COLDFIRE) {
221 asm volatile ("fmovemd %/fp0-%/fp7,%0\n\t"
222 "fmovel %/fpiar,%1\n\t"
223 "fmovel %/fpcr,%2\n\t"
224 "fmovel %/fpsr,%3"
225 :
226 : "m" (p->thread.fp[0]),
227 "m" (p->thread.fpcntl[0]),
228 "m" (p->thread.fpcntl[1]),
229 "m" (p->thread.fpcntl[2])
230 : "memory");
231 } else {
232 asm volatile ("fmovemx %/fp0-%/fp7,%0\n\t"
233 "fmoveml %/fpiar/%/fpcr/%/fpsr,%1"
234 :
235 : "m" (p->thread.fp[0]),
236 "m" (p->thread.fpcntl[0])
237 : "memory");
238 }
239 }
240
241 /* Restore the state in case the fpu was busy */
242 asm volatile ("frestore %0" : : "m" (p->thread.fpstate[0]));
243 }
244#endif /* CONFIG_FPU */
245
246 return 0;
247}
248
249/* Fill in the fpu structure for a core dump. */
250#ifdef CONFIG_FPU
251int dump_fpu (struct pt_regs *regs, struct user_m68kfp_struct *fpu)
252{
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 (!CPU_IS_060 ? !fpustate[0] : !fpustate[2])
272 return 0;
273
274 if (CPU_IS_COLDFIRE) {
275 asm volatile ("fmovel %/fpiar,%0\n\t"
276 "fmovel %/fpcr,%1\n\t"
277 "fmovel %/fpsr,%2\n\t"
278 "fmovemd %/fp0-%/fp7,%3"
279 :
280 : "m" (fpu->fpcntl[0]),
281 "m" (fpu->fpcntl[1]),
282 "m" (fpu->fpcntl[2]),
283 "m" (fpu->fpregs[0])
284 : "memory");
285 } else {
286 asm volatile ("fmovem %/fpiar/%/fpcr/%/fpsr,%0"
287 :
288 : "m" (fpu->fpcntl[0])
289 : "memory");
290 asm volatile ("fmovemx %/fp0-%/fp7,%0"
291 :
292 : "m" (fpu->fpregs[0])
293 : "memory");
294 }
295
296 return 1;
297}
298EXPORT_SYMBOL(dump_fpu);
299#endif /* CONFIG_FPU */
300
301/*
302 * sys_execve() executes a new program.
303 */
304asmlinkage int sys_execve(const char __user *name,
305 const char __user *const __user *argv,
306 const char __user *const __user *envp)
307{
308 int error;
309 char * filename;
310 struct pt_regs *regs = (struct pt_regs *) &name;
311
312 filename = getname(name);
313 error = PTR_ERR(filename);
314 if (IS_ERR(filename))
315 return error;
316 error = do_execve(filename, argv, envp, regs);
317 putname(filename);
318 return error;
319}
320
321unsigned long get_wchan(struct task_struct *p)
322{
323 unsigned long fp, pc;
324 unsigned long stack_page;
325 int count = 0;
326 if (!p || p == current || p->state == TASK_RUNNING)
327 return 0;
328
329 stack_page = (unsigned long)task_stack_page(p);
330 fp = ((struct switch_stack *)p->thread.ksp)->a6;
331 do {
332 if (fp < stack_page+sizeof(struct thread_info) ||
333 fp >= 8184+stack_page)
334 return 0;
335 pc = ((unsigned long *)fp)[1];
336 if (!in_sched_functions(pc))
337 return pc;
338 fp = *(unsigned long *) fp;
339 } while (count++ < 16);
340 return 0;
341}