]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/sparc/kernel/process_32.c
sched/headers: Prepare for new header dependencies before moving code to <linux/sched...
[mirror_ubuntu-bionic-kernel.git] / arch / sparc / kernel / process_32.c
CommitLineData
64d329ee 1/* linux/arch/sparc/kernel/process.c
1da177e4 2 *
4fe3ebec 3 * Copyright (C) 1995, 2008 David S. Miller (davem@davemloft.net)
1da177e4
LT
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
8bdd5a1b 13#include <linux/elfcore.h>
1da177e4
LT
14#include <linux/errno.h>
15#include <linux/module.h>
16#include <linux/sched.h>
b17b0153 17#include <linux/sched/debug.h>
1da177e4 18#include <linux/kernel.h>
1da177e4
LT
19#include <linux/mm.h>
20#include <linux/stddef.h>
21#include <linux/ptrace.h>
1da177e4 22#include <linux/user.h>
1da177e4 23#include <linux/smp.h>
1da177e4
LT
24#include <linux/reboot.h>
25#include <linux/delay.h>
26#include <linux/pm.h>
5a0e3ad6 27#include <linux/slab.h>
8bdd5a1b 28#include <linux/cpu.h>
1da177e4
LT
29
30#include <asm/auxio.h>
31#include <asm/oplib.h>
7c0f6ba6 32#include <linux/uaccess.h>
1da177e4
LT
33#include <asm/page.h>
34#include <asm/pgalloc.h>
35#include <asm/pgtable.h>
36#include <asm/delay.h>
37#include <asm/processor.h>
38#include <asm/psr.h>
39#include <asm/elf.h>
c73fcc84 40#include <asm/prom.h>
1da177e4 41#include <asm/unistd.h>
d550bbd4 42#include <asm/setup.h>
1da177e4 43
8bdd5a1b
SR
44#include "kernel.h"
45
1da177e4
LT
46/*
47 * Power management idle function
48 * Set in pm platform drivers (apc.c and pmc.c)
49 */
d472ba84 50void (*sparc_idle)(void);
1da177e4
LT
51
52/*
53 * Power-off handler instantiation for pm.h compliance
54 * This is done via auxio, but could be used as a fallback
55 * handler when auxio is not present-- unused for now...
56 */
304cd3ef 57void (*pm_power_off)(void) = machine_power_off;
89eb1693 58EXPORT_SYMBOL(pm_power_off);
1da177e4
LT
59
60/*
61 * sysctl - toggle power-off restriction for serial console
62 * systems in machine_power_off()
63 */
64int scons_pwroff = 1;
65
66extern void fpsave(unsigned long *, unsigned long *, void *, unsigned long *);
67
68struct task_struct *last_task_used_math = NULL;
69struct thread_info *current_set[NR_CPUS];
70
87fa05ae
SR
71/* Idle loop support. */
72void arch_cpu_idle(void)
1da177e4 73{
87fa05ae
SR
74 if (sparc_idle)
75 (*sparc_idle)();
76 local_irq_enable();
1da177e4
LT
77}
78
1da177e4
LT
79/* XXX cli/sti -> local_irq_xxx here, check this works once SMP is fixed. */
80void machine_halt(void)
81{
82 local_irq_enable();
83 mdelay(8);
84 local_irq_disable();
1da177e4
LT
85 prom_halt();
86 panic("Halt failed!");
87}
88
1da177e4
LT
89void machine_restart(char * cmd)
90{
91 char *p;
92
93 local_irq_enable();
94 mdelay(8);
95 local_irq_disable();
96
97 p = strchr (reboot_command, '\n');
98 if (p) *p = 0;
1da177e4
LT
99 if (cmd)
100 prom_reboot(cmd);
101 if (*reboot_command)
102 prom_reboot(reboot_command);
103 prom_feval ("reset");
104 panic("Reboot failed!");
105}
106
1da177e4
LT
107void machine_power_off(void)
108{
c73fcc84 109 if (auxio_power_register &&
d2aca8f9
SR
110 (strcmp(of_console_device->type, "serial") || scons_pwroff)) {
111 u8 power_register = sbus_readb(auxio_power_register);
112 power_register |= AUXIO_POWER_OFF;
113 sbus_writeb(power_register, auxio_power_register);
114 }
115
1da177e4
LT
116 machine_halt();
117}
118
1da177e4
LT
119void show_regs(struct pt_regs *r)
120{
4d7b92ad 121 struct reg_window32 *rw = (struct reg_window32 *) r->u_regs[14];
1da177e4 122
a43cb95d
TH
123 show_regs_print_info(KERN_DEFAULT);
124
1da177e4
LT
125 printk("PSR: %08lx PC: %08lx NPC: %08lx Y: %08lx %s\n",
126 r->psr, r->pc, r->npc, r->y, print_tainted());
4fe3ebec 127 printk("PC: <%pS>\n", (void *) r->pc);
1da177e4
LT
128 printk("%%G: %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
129 r->u_regs[0], r->u_regs[1], r->u_regs[2], r->u_regs[3],
130 r->u_regs[4], r->u_regs[5], r->u_regs[6], r->u_regs[7]);
131 printk("%%O: %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
132 r->u_regs[8], r->u_regs[9], r->u_regs[10], r->u_regs[11],
133 r->u_regs[12], r->u_regs[13], r->u_regs[14], r->u_regs[15]);
4fe3ebec 134 printk("RPC: <%pS>\n", (void *) r->u_regs[15]);
1da177e4
LT
135
136 printk("%%L: %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
137 rw->locals[0], rw->locals[1], rw->locals[2], rw->locals[3],
138 rw->locals[4], rw->locals[5], rw->locals[6], rw->locals[7]);
139 printk("%%I: %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
140 rw->ins[0], rw->ins[1], rw->ins[2], rw->ins[3],
141 rw->ins[4], rw->ins[5], rw->ins[6], rw->ins[7]);
142}
143
144/*
145 * The show_stack is an external API which we do not use ourselves.
146 * The oops is printed in die_if_kernel.
147 */
148void show_stack(struct task_struct *tsk, unsigned long *_ksp)
149{
150 unsigned long pc, fp;
151 unsigned long task_base;
4d7b92ad 152 struct reg_window32 *rw;
1da177e4
LT
153 int count = 0;
154
89e3f23d
TH
155 if (!tsk)
156 tsk = current;
1da177e4 157
89e3f23d
TH
158 if (tsk == current && !_ksp)
159 __asm__ __volatile__("mov %%fp, %0" : "=r" (_ksp));
160
161 task_base = (unsigned long) task_stack_page(tsk);
1da177e4
LT
162 fp = (unsigned long) _ksp;
163 do {
164 /* Bogus frame pointer? */
165 if (fp < (task_base + sizeof(struct thread_info)) ||
166 fp >= (task_base + (PAGE_SIZE << 1)))
167 break;
4d7b92ad 168 rw = (struct reg_window32 *) fp;
1da177e4
LT
169 pc = rw->ins[7];
170 printk("[%08lx : ", pc);
4fe3ebec 171 printk("%pS ] ", (void *) pc);
1da177e4
LT
172 fp = rw->ins[6];
173 } while (++count < 16);
174 printk("\n");
175}
176
177/*
178 * Note: sparc64 has a pretty intricated thread_saved_pc, check it out.
179 */
180unsigned long thread_saved_pc(struct task_struct *tsk)
181{
d562ef6a 182 return task_thread_info(tsk)->kpc;
1da177e4
LT
183}
184
185/*
186 * Free current thread data structures etc..
187 */
e6464694 188void exit_thread(struct task_struct *tsk)
1da177e4
LT
189{
190#ifndef CONFIG_SMP
e6464694 191 if (last_task_used_math == tsk) {
1da177e4 192#else
e6464694 193 if (test_ti_thread_flag(task_thread_info(tsk), TIF_USEDFPU)) {
1da177e4
LT
194#endif
195 /* Keep process from leaving FPU in a bogon state. */
196 put_psr(get_psr() | PSR_EF);
e6464694
JS
197 fpsave(&tsk->thread.float_regs[0], &tsk->thread.fsr,
198 &tsk->thread.fpqueue[0], &tsk->thread.fpqdepth);
1da177e4
LT
199#ifndef CONFIG_SMP
200 last_task_used_math = NULL;
201#else
e6464694 202 clear_ti_thread_flag(task_thread_info(tsk), TIF_USEDFPU);
1da177e4
LT
203#endif
204 }
205}
206
207void flush_thread(void)
208{
209 current_thread_info()->w_saved = 0;
210
1da177e4
LT
211#ifndef CONFIG_SMP
212 if(last_task_used_math == current) {
213#else
54f565ea 214 if (test_thread_flag(TIF_USEDFPU)) {
1da177e4
LT
215#endif
216 /* Clean the fpu. */
217 put_psr(get_psr() | PSR_EF);
218 fpsave(&current->thread.float_regs[0], &current->thread.fsr,
219 &current->thread.fpqueue[0], &current->thread.fpqdepth);
220#ifndef CONFIG_SMP
221 last_task_used_math = NULL;
222#else
54f565ea 223 clear_thread_flag(TIF_USEDFPU);
1da177e4
LT
224#endif
225 }
226
ddd53bf1 227 /* This task is no longer a kernel thread. */
1da177e4
LT
228 if (current->thread.flags & SPARC_FLAG_KTHREAD) {
229 current->thread.flags &= ~SPARC_FLAG_KTHREAD;
230
231 /* We must fixup kregs as well. */
232 /* XXX This was not fixed for ti for a while, worked. Unused? */
233 current->thread.kregs = (struct pt_regs *)
36483c6b 234 (task_stack_page(current) + (THREAD_SIZE - TRACEREG_SZ));
1da177e4
LT
235 }
236}
237
64d329ee 238static inline struct sparc_stackf __user *
1da177e4
LT
239clone_stackframe(struct sparc_stackf __user *dst,
240 struct sparc_stackf __user *src)
241{
242 unsigned long size, fp;
243 struct sparc_stackf *tmp;
244 struct sparc_stackf __user *sp;
245
246 if (get_user(tmp, &src->fp))
247 return NULL;
248
249 fp = (unsigned long) tmp;
250 size = (fp - ((unsigned long) src));
251 fp = (unsigned long) dst;
252 sp = (struct sparc_stackf __user *)(fp - size);
253
254 /* do_fork() grabs the parent semaphore, we must release it
255 * temporarily so we can build the child clone stack frame
256 * without deadlocking.
257 */
258 if (__copy_user(sp, src, size))
259 sp = NULL;
260 else if (put_user(fp, &sp->fp))
261 sp = NULL;
262
263 return sp;
264}
265
266asmlinkage int sparc_do_fork(unsigned long clone_flags,
267 unsigned long stack_start,
268 struct pt_regs *regs,
269 unsigned long stack_size)
270{
271 unsigned long parent_tid_ptr, child_tid_ptr;
1e38c126
DM
272 unsigned long orig_i1 = regs->u_regs[UREG_I1];
273 long ret;
1da177e4
LT
274
275 parent_tid_ptr = regs->u_regs[UREG_I2];
276 child_tid_ptr = regs->u_regs[UREG_I4];
277
e80d6661 278 ret = do_fork(clone_flags, stack_start, stack_size,
1e38c126
DM
279 (int __user *) parent_tid_ptr,
280 (int __user *) child_tid_ptr);
281
282 /* If we get an error and potentially restart the system
283 * call, we're screwed because copy_thread() clobbered
284 * the parent's %o1. So detect that case and restore it
285 * here.
286 */
287 if ((unsigned long)ret >= -ERESTART_RESTARTBLOCK)
288 regs->u_regs[UREG_I1] = orig_i1;
289
290 return ret;
1da177e4
LT
291}
292
293/* Copy a Sparc thread. The fork() return value conventions
294 * under SunOS are nothing short of bletcherous:
295 * Parent --> %o0 == childs pid, %o1 == 0
296 * Child --> %o0 == parents pid, %o1 == 1
297 *
298 * NOTE: We have a separate fork kpsr/kwim because
299 * the parent could change these values between
300 * sys_fork invocation and when we reach here
301 * if the parent should sleep while trying to
302 * allocate the task_struct and kernel stack in
303 * do_fork().
304 * XXX See comment above sys_vfork in sparc64. todo.
305 */
306extern void ret_from_fork(void);
c78e0643 307extern void ret_from_kernel_thread(void);
1da177e4 308
6f2c55b8 309int copy_thread(unsigned long clone_flags, unsigned long sp,
afa86fc4 310 unsigned long arg, struct task_struct *p)
1da177e4 311{
36483c6b 312 struct thread_info *ti = task_thread_info(p);
afa86fc4 313 struct pt_regs *childregs, *regs = current_pt_regs();
1da177e4
LT
314 char *new_stack;
315
316#ifndef CONFIG_SMP
317 if(last_task_used_math == current) {
318#else
54f565ea 319 if (test_thread_flag(TIF_USEDFPU)) {
1da177e4
LT
320#endif
321 put_psr(get_psr() | PSR_EF);
322 fpsave(&p->thread.float_regs[0], &p->thread.fsr,
323 &p->thread.fpqueue[0], &p->thread.fpqdepth);
1da177e4
LT
324 }
325
326 /*
c78e0643
AV
327 * p->thread_info new_stack childregs stack bottom
328 * ! ! ! !
329 * V V (stk.fr.) V (pt_regs) V
330 * +----- - - - - - ------+===========+=============+
1da177e4 331 */
36483c6b 332 new_stack = task_stack_page(p) + THREAD_SIZE;
1da177e4 333 new_stack -= STACKFRAME_SZ + TRACEREG_SZ;
1da177e4
LT
334 childregs = (struct pt_regs *) (new_stack + STACKFRAME_SZ);
335
336 /*
bef033a3
IM
337 * A new process must start with interrupts disabled, see schedule_tail()
338 * and finish_task_switch(). (If we do not do it and if a timer interrupt
339 * hits before we unlock and attempts to take the rq->lock, we deadlock.)
340 *
341 * Thus, kpsr |= PSR_PIL.
1da177e4
LT
342 */
343 ti->ksp = (unsigned long) new_stack;
c78e0643
AV
344 p->thread.kregs = childregs;
345
346 if (unlikely(p->flags & PF_KTHREAD)) {
347 extern int nwindows;
348 unsigned long psr;
349 memset(new_stack, 0, STACKFRAME_SZ + TRACEREG_SZ);
350 p->thread.flags |= SPARC_FLAG_KTHREAD;
351 p->thread.current_ds = KERNEL_DS;
352 ti->kpc = (((unsigned long) ret_from_kernel_thread) - 0x8);
353 childregs->u_regs[UREG_G1] = sp; /* function */
354 childregs->u_regs[UREG_G2] = arg;
355 psr = childregs->psr = get_psr();
356 ti->kpsr = psr | PSR_PIL;
357 ti->kwim = 1 << (((psr & PSR_CWP) + 1) % nwindows);
358 return 0;
359 }
360 memcpy(new_stack, (char *)regs - STACKFRAME_SZ, STACKFRAME_SZ + TRACEREG_SZ);
361 childregs->u_regs[UREG_FP] = sp;
362 p->thread.flags &= ~SPARC_FLAG_KTHREAD;
363 p->thread.current_ds = USER_DS;
1da177e4
LT
364 ti->kpc = (((unsigned long) ret_from_fork) - 0x8);
365 ti->kpsr = current->thread.fork_kpsr | PSR_PIL;
366 ti->kwim = current->thread.fork_kwim;
367
c78e0643
AV
368 if (sp != regs->u_regs[UREG_FP]) {
369 struct sparc_stackf __user *childstack;
370 struct sparc_stackf __user *parentstack;
1da177e4 371
c78e0643
AV
372 /*
373 * This is a clone() call with supplied user stack.
374 * Set some valid stack frames to give to the child.
375 */
376 childstack = (struct sparc_stackf __user *)
377 (sp & ~0xfUL);
378 parentstack = (struct sparc_stackf __user *)
379 regs->u_regs[UREG_FP];
1da177e4
LT
380
381#if 0
c78e0643
AV
382 printk("clone: parent stack:\n");
383 show_stackframe(parentstack);
1da177e4
LT
384#endif
385
c78e0643
AV
386 childstack = clone_stackframe(childstack, parentstack);
387 if (!childstack)
388 return -EFAULT;
1da177e4
LT
389
390#if 0
c78e0643
AV
391 printk("clone: child stack:\n");
392 show_stackframe(childstack);
1da177e4
LT
393#endif
394
c78e0643 395 childregs->u_regs[UREG_FP] = (unsigned long)childstack;
1da177e4
LT
396 }
397
398#ifdef CONFIG_SMP
399 /* FPU must be disabled on SMP. */
400 childregs->psr &= ~PSR_EF;
427f23cb 401 clear_tsk_thread_flag(p, TIF_USEDFPU);
1da177e4
LT
402#endif
403
404 /* Set the return value for the child. */
405 childregs->u_regs[UREG_I0] = current->pid;
406 childregs->u_regs[UREG_I1] = 1;
407
408 /* Set the return value for the parent. */
409 regs->u_regs[UREG_I1] = 0;
410
411 if (clone_flags & CLONE_SETTLS)
412 childregs->u_regs[UREG_G7] = regs->u_regs[UREG_I3];
413
414 return 0;
415}
416
1da177e4
LT
417/*
418 * fill in the fpu structure for a core dump.
419 */
420int dump_fpu (struct pt_regs * regs, elf_fpregset_t * fpregs)
421{
422 if (used_math()) {
423 memset(fpregs, 0, sizeof(*fpregs));
424 fpregs->pr_q_entrysize = 8;
425 return 1;
426 }
427#ifdef CONFIG_SMP
54f565ea 428 if (test_thread_flag(TIF_USEDFPU)) {
1da177e4
LT
429 put_psr(get_psr() | PSR_EF);
430 fpsave(&current->thread.float_regs[0], &current->thread.fsr,
431 &current->thread.fpqueue[0], &current->thread.fpqdepth);
432 if (regs != NULL) {
433 regs->psr &= ~(PSR_EF);
54f565ea 434 clear_thread_flag(TIF_USEDFPU);
1da177e4
LT
435 }
436 }
437#else
438 if (current == last_task_used_math) {
439 put_psr(get_psr() | PSR_EF);
440 fpsave(&current->thread.float_regs[0], &current->thread.fsr,
441 &current->thread.fpqueue[0], &current->thread.fpqdepth);
442 if (regs != NULL) {
443 regs->psr &= ~(PSR_EF);
444 last_task_used_math = NULL;
445 }
446 }
447#endif
448 memcpy(&fpregs->pr_fr.pr_regs[0],
449 &current->thread.float_regs[0],
450 (sizeof(unsigned long) * 32));
451 fpregs->pr_fsr = current->thread.fsr;
452 fpregs->pr_qcnt = current->thread.fpqdepth;
453 fpregs->pr_q_entrysize = 8;
454 fpregs->pr_en = 1;
455 if(fpregs->pr_qcnt != 0) {
456 memcpy(&fpregs->pr_q[0],
457 &current->thread.fpqueue[0],
458 sizeof(struct fpq) * fpregs->pr_qcnt);
459 }
460 /* Zero out the rest. */
461 memset(&fpregs->pr_q[fpregs->pr_qcnt], 0,
462 sizeof(struct fpq) * (32 - fpregs->pr_qcnt));
463 return 1;
464}
465
1da177e4
LT
466unsigned long get_wchan(struct task_struct *task)
467{
468 unsigned long pc, fp, bias = 0;
469 unsigned long task_base = (unsigned long) task;
470 unsigned long ret = 0;
4d7b92ad 471 struct reg_window32 *rw;
1da177e4
LT
472 int count = 0;
473
474 if (!task || task == current ||
475 task->state == TASK_RUNNING)
476 goto out;
477
d562ef6a 478 fp = task_thread_info(task)->ksp + bias;
1da177e4
LT
479 do {
480 /* Bogus frame pointer? */
481 if (fp < (task_base + sizeof(struct thread_info)) ||
482 fp >= (task_base + (2 * PAGE_SIZE)))
483 break;
4d7b92ad 484 rw = (struct reg_window32 *) fp;
1da177e4
LT
485 pc = rw->ins[7];
486 if (!in_sched_functions(pc)) {
487 ret = pc;
488 goto out;
489 }
490 fp = rw->ins[6] + bias;
491 } while (++count < 16);
492
493out:
494 return ret;
495}
496