xref: /linux/arch/sparc/kernel/process_32.c (revision d97b46a64674a267bc41c9e16132ee2a98c3347d)
1 /*  linux/arch/sparc/kernel/process.c
2  *
3  *  Copyright (C) 1995, 2008 David S. Miller (davem@davemloft.net)
4  *  Copyright (C) 1996 Eddie C. Dost   (ecd@skynet.be)
5  */
6 
7 /*
8  * This file handles the architecture-dependent parts of process handling..
9  */
10 
11 #include <stdarg.h>
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/stddef.h>
19 #include <linux/ptrace.h>
20 #include <linux/user.h>
21 #include <linux/smp.h>
22 #include <linux/reboot.h>
23 #include <linux/delay.h>
24 #include <linux/pm.h>
25 #include <linux/init.h>
26 #include <linux/slab.h>
27 
28 #include <asm/auxio.h>
29 #include <asm/oplib.h>
30 #include <asm/uaccess.h>
31 #include <asm/page.h>
32 #include <asm/pgalloc.h>
33 #include <asm/pgtable.h>
34 #include <asm/delay.h>
35 #include <asm/processor.h>
36 #include <asm/psr.h>
37 #include <asm/elf.h>
38 #include <asm/prom.h>
39 #include <asm/unistd.h>
40 #include <asm/setup.h>
41 
42 /*
43  * Power management idle function
44  * Set in pm platform drivers (apc.c and pmc.c)
45  */
46 void (*pm_idle)(void);
47 EXPORT_SYMBOL(pm_idle);
48 
49 /*
50  * Power-off handler instantiation for pm.h compliance
51  * This is done via auxio, but could be used as a fallback
52  * handler when auxio is not present-- unused for now...
53  */
54 void (*pm_power_off)(void) = machine_power_off;
55 EXPORT_SYMBOL(pm_power_off);
56 
57 /*
58  * sysctl - toggle power-off restriction for serial console
59  * systems in machine_power_off()
60  */
61 int scons_pwroff = 1;
62 
63 extern void fpsave(unsigned long *, unsigned long *, void *, unsigned long *);
64 
65 struct task_struct *last_task_used_math = NULL;
66 struct thread_info *current_set[NR_CPUS];
67 
68 #ifndef CONFIG_SMP
69 
70 /*
71  * the idle loop on a Sparc... ;)
72  */
73 void cpu_idle(void)
74 {
75 	/* endless idle loop with no priority at all */
76 	for (;;) {
77 		if (pm_idle) {
78 			while (!need_resched())
79 				(*pm_idle)();
80 		} else {
81 			while (!need_resched())
82 				cpu_relax();
83 		}
84 		schedule_preempt_disabled();
85 	}
86 }
87 
88 #else
89 
90 /* This is being executed in task 0 'user space'. */
91 void cpu_idle(void)
92 {
93         set_thread_flag(TIF_POLLING_NRFLAG);
94 	/* endless idle loop with no priority at all */
95 	while(1) {
96 #ifdef CONFIG_SPARC_LEON
97 		if (pm_idle) {
98 			while (!need_resched())
99 				(*pm_idle)();
100 		} else
101 #endif
102 		{
103 			while (!need_resched())
104 				cpu_relax();
105 		}
106 		schedule_preempt_disabled();
107 	}
108 }
109 
110 #endif
111 
112 /* XXX cli/sti -> local_irq_xxx here, check this works once SMP is fixed. */
113 void machine_halt(void)
114 {
115 	local_irq_enable();
116 	mdelay(8);
117 	local_irq_disable();
118 	prom_halt();
119 	panic("Halt failed!");
120 }
121 
122 void machine_restart(char * cmd)
123 {
124 	char *p;
125 
126 	local_irq_enable();
127 	mdelay(8);
128 	local_irq_disable();
129 
130 	p = strchr (reboot_command, '\n');
131 	if (p) *p = 0;
132 	if (cmd)
133 		prom_reboot(cmd);
134 	if (*reboot_command)
135 		prom_reboot(reboot_command);
136 	prom_feval ("reset");
137 	panic("Reboot failed!");
138 }
139 
140 void machine_power_off(void)
141 {
142 	if (auxio_power_register &&
143 	    (strcmp(of_console_device->type, "serial") || scons_pwroff))
144 		*auxio_power_register |= AUXIO_POWER_OFF;
145 	machine_halt();
146 }
147 
148 void show_regs(struct pt_regs *r)
149 {
150 	struct reg_window32 *rw = (struct reg_window32 *) r->u_regs[14];
151 
152         printk("PSR: %08lx PC: %08lx NPC: %08lx Y: %08lx    %s\n",
153 	       r->psr, r->pc, r->npc, r->y, print_tainted());
154 	printk("PC: <%pS>\n", (void *) r->pc);
155 	printk("%%G: %08lx %08lx  %08lx %08lx  %08lx %08lx  %08lx %08lx\n",
156 	       r->u_regs[0], r->u_regs[1], r->u_regs[2], r->u_regs[3],
157 	       r->u_regs[4], r->u_regs[5], r->u_regs[6], r->u_regs[7]);
158 	printk("%%O: %08lx %08lx  %08lx %08lx  %08lx %08lx  %08lx %08lx\n",
159 	       r->u_regs[8], r->u_regs[9], r->u_regs[10], r->u_regs[11],
160 	       r->u_regs[12], r->u_regs[13], r->u_regs[14], r->u_regs[15]);
161 	printk("RPC: <%pS>\n", (void *) r->u_regs[15]);
162 
163 	printk("%%L: %08lx %08lx  %08lx %08lx  %08lx %08lx  %08lx %08lx\n",
164 	       rw->locals[0], rw->locals[1], rw->locals[2], rw->locals[3],
165 	       rw->locals[4], rw->locals[5], rw->locals[6], rw->locals[7]);
166 	printk("%%I: %08lx %08lx  %08lx %08lx  %08lx %08lx  %08lx %08lx\n",
167 	       rw->ins[0], rw->ins[1], rw->ins[2], rw->ins[3],
168 	       rw->ins[4], rw->ins[5], rw->ins[6], rw->ins[7]);
169 }
170 
171 /*
172  * The show_stack is an external API which we do not use ourselves.
173  * The oops is printed in die_if_kernel.
174  */
175 void show_stack(struct task_struct *tsk, unsigned long *_ksp)
176 {
177 	unsigned long pc, fp;
178 	unsigned long task_base;
179 	struct reg_window32 *rw;
180 	int count = 0;
181 
182 	if (tsk != NULL)
183 		task_base = (unsigned long) task_stack_page(tsk);
184 	else
185 		task_base = (unsigned long) current_thread_info();
186 
187 	fp = (unsigned long) _ksp;
188 	do {
189 		/* Bogus frame pointer? */
190 		if (fp < (task_base + sizeof(struct thread_info)) ||
191 		    fp >= (task_base + (PAGE_SIZE << 1)))
192 			break;
193 		rw = (struct reg_window32 *) fp;
194 		pc = rw->ins[7];
195 		printk("[%08lx : ", pc);
196 		printk("%pS ] ", (void *) pc);
197 		fp = rw->ins[6];
198 	} while (++count < 16);
199 	printk("\n");
200 }
201 
202 void dump_stack(void)
203 {
204 	unsigned long *ksp;
205 
206 	__asm__ __volatile__("mov	%%fp, %0"
207 			     : "=r" (ksp));
208 	show_stack(current, ksp);
209 }
210 
211 EXPORT_SYMBOL(dump_stack);
212 
213 /*
214  * Note: sparc64 has a pretty intricated thread_saved_pc, check it out.
215  */
216 unsigned long thread_saved_pc(struct task_struct *tsk)
217 {
218 	return task_thread_info(tsk)->kpc;
219 }
220 
221 /*
222  * Free current thread data structures etc..
223  */
224 void exit_thread(void)
225 {
226 #ifndef CONFIG_SMP
227 	if(last_task_used_math == current) {
228 #else
229 	if (test_thread_flag(TIF_USEDFPU)) {
230 #endif
231 		/* Keep process from leaving FPU in a bogon state. */
232 		put_psr(get_psr() | PSR_EF);
233 		fpsave(&current->thread.float_regs[0], &current->thread.fsr,
234 		       &current->thread.fpqueue[0], &current->thread.fpqdepth);
235 #ifndef CONFIG_SMP
236 		last_task_used_math = NULL;
237 #else
238 		clear_thread_flag(TIF_USEDFPU);
239 #endif
240 	}
241 }
242 
243 void flush_thread(void)
244 {
245 	current_thread_info()->w_saved = 0;
246 
247 #ifndef CONFIG_SMP
248 	if(last_task_used_math == current) {
249 #else
250 	if (test_thread_flag(TIF_USEDFPU)) {
251 #endif
252 		/* Clean the fpu. */
253 		put_psr(get_psr() | PSR_EF);
254 		fpsave(&current->thread.float_regs[0], &current->thread.fsr,
255 		       &current->thread.fpqueue[0], &current->thread.fpqdepth);
256 #ifndef CONFIG_SMP
257 		last_task_used_math = NULL;
258 #else
259 		clear_thread_flag(TIF_USEDFPU);
260 #endif
261 	}
262 
263 	/* This task is no longer a kernel thread. */
264 	if (current->thread.flags & SPARC_FLAG_KTHREAD) {
265 		current->thread.flags &= ~SPARC_FLAG_KTHREAD;
266 
267 		/* We must fixup kregs as well. */
268 		/* XXX This was not fixed for ti for a while, worked. Unused? */
269 		current->thread.kregs = (struct pt_regs *)
270 		    (task_stack_page(current) + (THREAD_SIZE - TRACEREG_SZ));
271 	}
272 }
273 
274 static inline struct sparc_stackf __user *
275 clone_stackframe(struct sparc_stackf __user *dst,
276 		 struct sparc_stackf __user *src)
277 {
278 	unsigned long size, fp;
279 	struct sparc_stackf *tmp;
280 	struct sparc_stackf __user *sp;
281 
282 	if (get_user(tmp, &src->fp))
283 		return NULL;
284 
285 	fp = (unsigned long) tmp;
286 	size = (fp - ((unsigned long) src));
287 	fp = (unsigned long) dst;
288 	sp = (struct sparc_stackf __user *)(fp - size);
289 
290 	/* do_fork() grabs the parent semaphore, we must release it
291 	 * temporarily so we can build the child clone stack frame
292 	 * without deadlocking.
293 	 */
294 	if (__copy_user(sp, src, size))
295 		sp = NULL;
296 	else if (put_user(fp, &sp->fp))
297 		sp = NULL;
298 
299 	return sp;
300 }
301 
302 asmlinkage int sparc_do_fork(unsigned long clone_flags,
303                              unsigned long stack_start,
304                              struct pt_regs *regs,
305                              unsigned long stack_size)
306 {
307 	unsigned long parent_tid_ptr, child_tid_ptr;
308 	unsigned long orig_i1 = regs->u_regs[UREG_I1];
309 	long ret;
310 
311 	parent_tid_ptr = regs->u_regs[UREG_I2];
312 	child_tid_ptr = regs->u_regs[UREG_I4];
313 
314 	ret = do_fork(clone_flags, stack_start,
315 		      regs, stack_size,
316 		      (int __user *) parent_tid_ptr,
317 		      (int __user *) child_tid_ptr);
318 
319 	/* If we get an error and potentially restart the system
320 	 * call, we're screwed because copy_thread() clobbered
321 	 * the parent's %o1.  So detect that case and restore it
322 	 * here.
323 	 */
324 	if ((unsigned long)ret >= -ERESTART_RESTARTBLOCK)
325 		regs->u_regs[UREG_I1] = orig_i1;
326 
327 	return ret;
328 }
329 
330 /* Copy a Sparc thread.  The fork() return value conventions
331  * under SunOS are nothing short of bletcherous:
332  * Parent -->  %o0 == childs  pid, %o1 == 0
333  * Child  -->  %o0 == parents pid, %o1 == 1
334  *
335  * NOTE: We have a separate fork kpsr/kwim because
336  *       the parent could change these values between
337  *       sys_fork invocation and when we reach here
338  *       if the parent should sleep while trying to
339  *       allocate the task_struct and kernel stack in
340  *       do_fork().
341  * XXX See comment above sys_vfork in sparc64. todo.
342  */
343 extern void ret_from_fork(void);
344 
345 int copy_thread(unsigned long clone_flags, unsigned long sp,
346 		unsigned long unused,
347 		struct task_struct *p, struct pt_regs *regs)
348 {
349 	struct thread_info *ti = task_thread_info(p);
350 	struct pt_regs *childregs;
351 	char *new_stack;
352 
353 #ifndef CONFIG_SMP
354 	if(last_task_used_math == current) {
355 #else
356 	if (test_thread_flag(TIF_USEDFPU)) {
357 #endif
358 		put_psr(get_psr() | PSR_EF);
359 		fpsave(&p->thread.float_regs[0], &p->thread.fsr,
360 		       &p->thread.fpqueue[0], &p->thread.fpqdepth);
361 #ifdef CONFIG_SMP
362 		clear_thread_flag(TIF_USEDFPU);
363 #endif
364 	}
365 
366 	/*
367 	 *  p->thread_info         new_stack   childregs
368 	 *  !                      !           !             {if(PSR_PS) }
369 	 *  V                      V (stk.fr.) V  (pt_regs)  { (stk.fr.) }
370 	 *  +----- - - - - - ------+===========+============={+==========}+
371 	 */
372 	new_stack = task_stack_page(p) + THREAD_SIZE;
373 	if (regs->psr & PSR_PS)
374 		new_stack -= STACKFRAME_SZ;
375 	new_stack -= STACKFRAME_SZ + TRACEREG_SZ;
376 	memcpy(new_stack, (char *)regs - STACKFRAME_SZ, STACKFRAME_SZ + TRACEREG_SZ);
377 	childregs = (struct pt_regs *) (new_stack + STACKFRAME_SZ);
378 
379 	/*
380 	 * A new process must start with interrupts closed in 2.5,
381 	 * because this is how Mingo's scheduler works (see schedule_tail
382 	 * and finish_arch_switch). If we do not do it, a timer interrupt hits
383 	 * before we unlock, attempts to re-take the rq->lock, and then we die.
384 	 * Thus, kpsr|=PSR_PIL.
385 	 */
386 	ti->ksp = (unsigned long) new_stack;
387 	ti->kpc = (((unsigned long) ret_from_fork) - 0x8);
388 	ti->kpsr = current->thread.fork_kpsr | PSR_PIL;
389 	ti->kwim = current->thread.fork_kwim;
390 
391 	if(regs->psr & PSR_PS) {
392 		extern struct pt_regs fake_swapper_regs;
393 
394 		p->thread.kregs = &fake_swapper_regs;
395 		new_stack += STACKFRAME_SZ + TRACEREG_SZ;
396 		childregs->u_regs[UREG_FP] = (unsigned long) new_stack;
397 		p->thread.flags |= SPARC_FLAG_KTHREAD;
398 		p->thread.current_ds = KERNEL_DS;
399 		memcpy(new_stack, (void *)regs->u_regs[UREG_FP], STACKFRAME_SZ);
400 		childregs->u_regs[UREG_G6] = (unsigned long) ti;
401 	} else {
402 		p->thread.kregs = childregs;
403 		childregs->u_regs[UREG_FP] = sp;
404 		p->thread.flags &= ~SPARC_FLAG_KTHREAD;
405 		p->thread.current_ds = USER_DS;
406 
407 		if (sp != regs->u_regs[UREG_FP]) {
408 			struct sparc_stackf __user *childstack;
409 			struct sparc_stackf __user *parentstack;
410 
411 			/*
412 			 * This is a clone() call with supplied user stack.
413 			 * Set some valid stack frames to give to the child.
414 			 */
415 			childstack = (struct sparc_stackf __user *)
416 				(sp & ~0xfUL);
417 			parentstack = (struct sparc_stackf __user *)
418 				regs->u_regs[UREG_FP];
419 
420 #if 0
421 			printk("clone: parent stack:\n");
422 			show_stackframe(parentstack);
423 #endif
424 
425 			childstack = clone_stackframe(childstack, parentstack);
426 			if (!childstack)
427 				return -EFAULT;
428 
429 #if 0
430 			printk("clone: child stack:\n");
431 			show_stackframe(childstack);
432 #endif
433 
434 			childregs->u_regs[UREG_FP] = (unsigned long)childstack;
435 		}
436 	}
437 
438 #ifdef CONFIG_SMP
439 	/* FPU must be disabled on SMP. */
440 	childregs->psr &= ~PSR_EF;
441 #endif
442 
443 	/* Set the return value for the child. */
444 	childregs->u_regs[UREG_I0] = current->pid;
445 	childregs->u_regs[UREG_I1] = 1;
446 
447 	/* Set the return value for the parent. */
448 	regs->u_regs[UREG_I1] = 0;
449 
450 	if (clone_flags & CLONE_SETTLS)
451 		childregs->u_regs[UREG_G7] = regs->u_regs[UREG_I3];
452 
453 	return 0;
454 }
455 
456 /*
457  * fill in the fpu structure for a core dump.
458  */
459 int dump_fpu (struct pt_regs * regs, elf_fpregset_t * fpregs)
460 {
461 	if (used_math()) {
462 		memset(fpregs, 0, sizeof(*fpregs));
463 		fpregs->pr_q_entrysize = 8;
464 		return 1;
465 	}
466 #ifdef CONFIG_SMP
467 	if (test_thread_flag(TIF_USEDFPU)) {
468 		put_psr(get_psr() | PSR_EF);
469 		fpsave(&current->thread.float_regs[0], &current->thread.fsr,
470 		       &current->thread.fpqueue[0], &current->thread.fpqdepth);
471 		if (regs != NULL) {
472 			regs->psr &= ~(PSR_EF);
473 			clear_thread_flag(TIF_USEDFPU);
474 		}
475 	}
476 #else
477 	if (current == last_task_used_math) {
478 		put_psr(get_psr() | PSR_EF);
479 		fpsave(&current->thread.float_regs[0], &current->thread.fsr,
480 		       &current->thread.fpqueue[0], &current->thread.fpqdepth);
481 		if (regs != NULL) {
482 			regs->psr &= ~(PSR_EF);
483 			last_task_used_math = NULL;
484 		}
485 	}
486 #endif
487 	memcpy(&fpregs->pr_fr.pr_regs[0],
488 	       &current->thread.float_regs[0],
489 	       (sizeof(unsigned long) * 32));
490 	fpregs->pr_fsr = current->thread.fsr;
491 	fpregs->pr_qcnt = current->thread.fpqdepth;
492 	fpregs->pr_q_entrysize = 8;
493 	fpregs->pr_en = 1;
494 	if(fpregs->pr_qcnt != 0) {
495 		memcpy(&fpregs->pr_q[0],
496 		       &current->thread.fpqueue[0],
497 		       sizeof(struct fpq) * fpregs->pr_qcnt);
498 	}
499 	/* Zero out the rest. */
500 	memset(&fpregs->pr_q[fpregs->pr_qcnt], 0,
501 	       sizeof(struct fpq) * (32 - fpregs->pr_qcnt));
502 	return 1;
503 }
504 
505 /*
506  * sparc_execve() executes a new program after the asm stub has set
507  * things up for us.  This should basically do what I want it to.
508  */
509 asmlinkage int sparc_execve(struct pt_regs *regs)
510 {
511 	int error, base = 0;
512 	char *filename;
513 
514 	/* Check for indirect call. */
515 	if(regs->u_regs[UREG_G1] == 0)
516 		base = 1;
517 
518 	filename = getname((char __user *)regs->u_regs[base + UREG_I0]);
519 	error = PTR_ERR(filename);
520 	if(IS_ERR(filename))
521 		goto out;
522 	error = do_execve(filename,
523 			  (const char __user *const  __user *)
524 			  regs->u_regs[base + UREG_I1],
525 			  (const char __user *const  __user *)
526 			  regs->u_regs[base + UREG_I2],
527 			  regs);
528 	putname(filename);
529 out:
530 	return error;
531 }
532 
533 /*
534  * This is the mechanism for creating a new kernel thread.
535  *
536  * NOTE! Only a kernel-only process(ie the swapper or direct descendants
537  * who haven't done an "execve()") should use this: it will work within
538  * a system call from a "real" process, but the process memory space will
539  * not be freed until both the parent and the child have exited.
540  */
541 pid_t kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
542 {
543 	long retval;
544 
545 	__asm__ __volatile__("mov %4, %%g2\n\t"    /* Set aside fn ptr... */
546 			     "mov %5, %%g3\n\t"    /* and arg. */
547 			     "mov %1, %%g1\n\t"
548 			     "mov %2, %%o0\n\t"    /* Clone flags. */
549 			     "mov 0, %%o1\n\t"     /* usp arg == 0 */
550 			     "t 0x10\n\t"          /* Linux/Sparc clone(). */
551 			     "cmp %%o1, 0\n\t"
552 			     "be 1f\n\t"           /* The parent, just return. */
553 			     " nop\n\t"            /* Delay slot. */
554 			     "jmpl %%g2, %%o7\n\t" /* Call the function. */
555 			     " mov %%g3, %%o0\n\t" /* Get back the arg in delay. */
556 			     "mov %3, %%g1\n\t"
557 			     "t 0x10\n\t"          /* Linux/Sparc exit(). */
558 			     /* Notreached by child. */
559 			     "1: mov %%o0, %0\n\t" :
560 			     "=r" (retval) :
561 			     "i" (__NR_clone), "r" (flags | CLONE_VM | CLONE_UNTRACED),
562 			     "i" (__NR_exit),  "r" (fn), "r" (arg) :
563 			     "g1", "g2", "g3", "o0", "o1", "memory", "cc");
564 	return retval;
565 }
566 EXPORT_SYMBOL(kernel_thread);
567 
568 unsigned long get_wchan(struct task_struct *task)
569 {
570 	unsigned long pc, fp, bias = 0;
571 	unsigned long task_base = (unsigned long) task;
572         unsigned long ret = 0;
573 	struct reg_window32 *rw;
574 	int count = 0;
575 
576 	if (!task || task == current ||
577             task->state == TASK_RUNNING)
578 		goto out;
579 
580 	fp = task_thread_info(task)->ksp + bias;
581 	do {
582 		/* Bogus frame pointer? */
583 		if (fp < (task_base + sizeof(struct thread_info)) ||
584 		    fp >= (task_base + (2 * PAGE_SIZE)))
585 			break;
586 		rw = (struct reg_window32 *) fp;
587 		pc = rw->ins[7];
588 		if (!in_sched_functions(pc)) {
589 			ret = pc;
590 			goto out;
591 		}
592 		fp = rw->ins[6] + bias;
593 	} while (++count < 16);
594 
595 out:
596 	return ret;
597 }
598 
599