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