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/config.h>
14 #include <linux/errno.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 
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>
34 
35 /*
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
39  * setup.
40  */
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);
45 
46 union task_union init_task_union
47 __attribute__((section("init_task"), aligned(KTHREAD_SIZE)))
48 	= { task: INIT_TASK(init_task_union.task) };
49 
50 asmlinkage void ret_from_fork(void);
51 
52 
53 /*
54  * The idle loop on an m68k..
55  */
default_idle(void)56 static void default_idle(void)
57 {
58 	while(1) {
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");
63 #else
64 			__asm__("stop #0x2000" : : : "cc");
65 #endif
66 		schedule();
67 		check_pgt_cache();
68 	}
69 }
70 
71 void (*idle)(void) = default_idle;
72 
73 /*
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)
78  */
cpu_idle(void)79 void cpu_idle(void)
80 {
81 	/* endless idle loop with no priority at all */
82 	init_idle();
83 	current->nice = 20;
84 	current->counter = -100;
85 	idle();
86 }
87 
machine_restart(char * __unused)88 void machine_restart(char * __unused)
89 {
90 	if (mach_reset)
91 		mach_reset();
92 	for (;;);
93 }
94 
machine_halt(void)95 void machine_halt(void)
96 {
97 	if (mach_halt)
98 		mach_halt();
99 	for (;;);
100 }
101 
machine_power_off(void)102 void machine_power_off(void)
103 {
104 	if (mach_power_off)
105 		mach_power_off();
106 	for (;;);
107 }
108 
show_regs(struct pt_regs * regs)109 void 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 
124 /*
125  * Create a kernel thread
126  */
arch_kernel_thread(int (* fn)(void *),void * arg,unsigned long flags)127 int arch_kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
128 {
129 	int pid;
130 	mm_segment_t fs;
131 
132 	fs = get_fs();
133 	set_fs (KERNEL_DS);
134 
135 	{
136 	register long retval __asm__ ("d0");
137 	register long clone_arg __asm__ ("d1") = flags | CLONE_VM;
138 
139 	retval = __NR_clone;
140 	__asm__ __volatile__
141 	  ("clrl %%d2\n\t"
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 */
150 	   "trap #0\n"
151 	   "1:"
152 	   : "+d" (retval)
153 	   : "i" (__NR_clone), "i" (__NR_exit),
154 	     "r" (arg), "a" (fn), "d" (clone_arg), "r" (current),
155 	     "i" (-KTHREAD_SIZE)
156 	   : "d2");
157 
158 	pid = retval;
159 	}
160 
161 	set_fs (fs);
162 	return pid;
163 }
164 
flush_thread(void)165 void flush_thread(void)
166 {
167 	unsigned long zero = 0;
168 	set_fs(USER_DS);
169 	current->thread.fs = __USER_DS;
170 	if (!FPU_IS_EMU)
171 		asm volatile (".chip 68k/68881\n\t"
172 			      "frestore %0@\n\t"
173 			      ".chip 68k" : : "a" (&zero));
174 }
175 
176 /*
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
180  * also copy_thread)
181  */
182 
m68k_fork(struct pt_regs * regs)183 asmlinkage int m68k_fork(struct pt_regs *regs)
184 {
185 	return do_fork(SIGCHLD, rdusp(), regs, 0);
186 }
187 
m68k_vfork(struct pt_regs * regs)188 asmlinkage int m68k_vfork(struct pt_regs *regs)
189 {
190 	return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, rdusp(), regs, 0);
191 }
192 
m68k_clone(struct pt_regs * regs)193 asmlinkage int m68k_clone(struct pt_regs *regs)
194 {
195 	unsigned long clone_flags;
196 	unsigned long newsp;
197 
198 	/* syscall2 puts clone_flags in d1 and usp in d2 */
199 	clone_flags = regs->d1;
200 	newsp = regs->d2;
201 	if (!newsp)
202 		newsp = rdusp();
203 	return do_fork(clone_flags, newsp, regs, 0);
204 }
205 
copy_thread(int nr,unsigned long clone_flags,unsigned long usp,unsigned long unused,struct task_struct * p,struct pt_regs * regs)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)
209 {
210 	struct pt_regs * childregs;
211 	struct switch_stack * childstack, *stack;
212 	unsigned long stack_offset, *retp;
213 
214 	stack_offset = KTHREAD_SIZE - sizeof(struct pt_regs);
215 	childregs = (struct pt_regs *) ((unsigned long) p + stack_offset);
216 
217 	*childregs = *regs;
218 	childregs->d0 = 0;
219 
220 	retp = ((unsigned long *) regs);
221 	stack = ((struct switch_stack *) retp) - 1;
222 
223 	childstack = ((struct switch_stack *) childregs) - 1;
224 	*childstack = *stack;
225 	childstack->retpc = (unsigned long)ret_from_fork;
226 
227 	p->thread.usp = usp;
228 	p->thread.ksp = (unsigned long)childstack;
229 	/*
230 	 * Must save the current SFC/DFC value, NOT the value when
231 	 * the parent was last descheduled - RGH  10-08-96
232 	 */
233 	p->thread.fs = get_fs().seg;
234 
235 	if (!FPU_IS_EMU) {
236 		/* Copy the current fpu state */
237 		asm volatile ("fsave %0" : : "m" (p->thread.fpstate[0]) : "memory");
238 
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])
243 				: "memory");
244 		/* Restore the state in case the fpu was busy */
245 		asm volatile ("frestore %0" : : "m" (p->thread.fpstate[0]));
246 	}
247 
248 	return 0;
249 }
250 
251 /* Fill in the fpu structure for a core dump.  */
252 
dump_fpu(struct pt_regs * regs,struct user_m68kfp_struct * fpu)253 int dump_fpu (struct pt_regs *regs, struct user_m68kfp_struct *fpu)
254 {
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 (!CPU_IS_060 ? !fpustate[0] : !fpustate[2])
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 	return 1;
283 }
284 
285 /*
286  * fill in the user structure for a core dump..
287  */
dump_thread(struct pt_regs * regs,struct user * dump)288 void dump_thread(struct pt_regs * regs, struct user * dump)
289 {
290 	struct switch_stack *sw;
291 
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;
300 	dump->u_ssize = 0;
301 
302 	if (dump->start_stack < TASK_SIZE)
303 		dump->u_ssize = ((unsigned long) (TASK_SIZE - dump->start_stack)) >> PAGE_SHIFT;
304 
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);
329 }
330 
331 /*
332  * sys_execve() executes a new program.
333  */
sys_execve(char * name,char ** argv,char ** envp)334 asmlinkage int sys_execve(char *name, char **argv, char **envp)
335 {
336 	int error;
337 	char * filename;
338 	struct pt_regs *regs = (struct pt_regs *) &name;
339 
340 	lock_kernel();
341 	filename = getname(name);
342 	error = PTR_ERR(filename);
343 	if (IS_ERR(filename))
344 		goto out;
345 	error = do_execve(filename, argv, envp, regs);
346 	putname(filename);
347 out:
348 	unlock_kernel();
349 	return error;
350 }
351 
352 /*
353  * These bracket the sleeping functions..
354  */
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)
359 
get_wchan(struct task_struct * p)360 unsigned long get_wchan(struct task_struct *p)
361 {
362 	unsigned long fp, pc;
363 	unsigned long stack_page;
364 	int count = 0;
365 	if (!p || p == current || p->state == TASK_RUNNING)
366 		return 0;
367 
368 	stack_page = (unsigned long)p;
369 	fp = ((struct switch_stack *)p->thread.ksp)->a6;
370 	do {
371 		if (fp < stack_page+sizeof(struct task_struct) ||
372 		    fp >= 8184+stack_page)
373 			return 0;
374 		pc = ((unsigned long *)fp)[1];
375 		/* FIXME: This depends on the order of these functions. */
376 		if (pc < first_sched || pc >= last_sched)
377 			return pc;
378 		fp = *(unsigned long *) fp;
379 	} while (count++ < 16);
380 	return 0;
381 }
382