1ab8283efc4c63dae8320fc1e62cdb2fa4feddef
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / arch / sparc64 / kernel / process.c
1 /* $Id: process.c,v 1.131 2002/02/09 19:49:30 davem Exp $
2 * arch/sparc64/kernel/process.c
3 *
4 * Copyright (C) 1995, 1996 David S. Miller (davem@caip.rutgers.edu)
5 * Copyright (C) 1996 Eddie C. Dost (ecd@skynet.be)
6 * Copyright (C) 1997, 1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
7 */
8
9 /*
10 * This file handles the architecture-dependent parts of process handling..
11 */
12
13 #include <stdarg.h>
14
15 #include <linux/config.h>
16 #include <linux/errno.h>
17 #include <linux/module.h>
18 #include <linux/sched.h>
19 #include <linux/kernel.h>
20 #include <linux/kallsyms.h>
21 #include <linux/mm.h>
22 #include <linux/smp.h>
23 #include <linux/smp_lock.h>
24 #include <linux/stddef.h>
25 #include <linux/ptrace.h>
26 #include <linux/slab.h>
27 #include <linux/user.h>
28 #include <linux/a.out.h>
29 #include <linux/config.h>
30 #include <linux/reboot.h>
31 #include <linux/delay.h>
32 #include <linux/compat.h>
33 #include <linux/init.h>
34
35 #include <asm/oplib.h>
36 #include <asm/uaccess.h>
37 #include <asm/system.h>
38 #include <asm/page.h>
39 #include <asm/pgalloc.h>
40 #include <asm/pgtable.h>
41 #include <asm/processor.h>
42 #include <asm/pstate.h>
43 #include <asm/elf.h>
44 #include <asm/fpumacro.h>
45 #include <asm/head.h>
46 #include <asm/cpudata.h>
47 #include <asm/mmu_context.h>
48 #include <asm/unistd.h>
49
50 /* #define VERBOSE_SHOWREGS */
51
52 /*
53 * Nothing special yet...
54 */
55 void default_idle(void)
56 {
57 }
58
59
60
61 #ifndef CONFIG_SMP
62
63 /*
64 * the idle loop on a Sparc... ;)
65 */
66 void cpu_idle(void)
67 {
68 /* endless idle loop with no priority at all */
69 for (;;) {
70 /* If current->work.need_resched is zero we should really
71 * setup for a system wakup event and execute a shutdown
72 * instruction.
73 *
74 * But this requires writing back the contents of the
75 * L2 cache etc. so implement this later. -DaveM
76 */
77 while (!need_resched())
78 barrier();
79
80 preempt_enable_no_resched();
81 schedule();
82 preempt_disable();
83 check_pgt_cache();
84 }
85 }
86
87 #else
88
89 /*
90 * the idle loop on a UltraMultiPenguin...
91 *
92 * TIF_POLLING_NRFLAG is set because we do not sleep the cpu
93 * inside of the idler task, so an interrupt is not needed
94 * to get a clean fast response.
95 *
96 * XXX Reverify this assumption... -DaveM
97 *
98 * Addendum: We do want it to do something for the signal
99 * delivery case, we detect that by just seeing
100 * if we are trying to send this to an idler or not.
101 */
102 void cpu_idle(void)
103 {
104 cpuinfo_sparc *cpuinfo = &local_cpu_data();
105 set_thread_flag(TIF_POLLING_NRFLAG);
106
107 while(1) {
108 if (need_resched()) {
109 preempt_enable_no_resched();
110 schedule();
111 preempt_disable();
112 check_pgt_cache();
113 }
114 }
115 }
116
117 #endif
118
119 extern char reboot_command [];
120
121 extern void (*prom_palette)(int);
122 extern void (*prom_keyboard)(void);
123
124 void machine_halt(void)
125 {
126 if (!serial_console && prom_palette)
127 prom_palette (1);
128 if (prom_keyboard)
129 prom_keyboard();
130 prom_halt();
131 panic("Halt failed!");
132 }
133
134 void machine_alt_power_off(void)
135 {
136 if (!serial_console && prom_palette)
137 prom_palette(1);
138 if (prom_keyboard)
139 prom_keyboard();
140 prom_halt_power_off();
141 panic("Power-off failed!");
142 }
143
144 void machine_restart(char * cmd)
145 {
146 char *p;
147
148 p = strchr (reboot_command, '\n');
149 if (p) *p = 0;
150 if (!serial_console && prom_palette)
151 prom_palette (1);
152 if (prom_keyboard)
153 prom_keyboard();
154 if (cmd)
155 prom_reboot(cmd);
156 if (*reboot_command)
157 prom_reboot(reboot_command);
158 prom_reboot("");
159 panic("Reboot failed!");
160 }
161
162 #ifdef CONFIG_COMPAT
163 static void show_regwindow32(struct pt_regs *regs)
164 {
165 struct reg_window32 __user *rw;
166 struct reg_window32 r_w;
167 mm_segment_t old_fs;
168
169 __asm__ __volatile__ ("flushw");
170 rw = compat_ptr((unsigned)regs->u_regs[14]);
171 old_fs = get_fs();
172 set_fs (USER_DS);
173 if (copy_from_user (&r_w, rw, sizeof(r_w))) {
174 set_fs (old_fs);
175 return;
176 }
177
178 set_fs (old_fs);
179 printk("l0: %08x l1: %08x l2: %08x l3: %08x "
180 "l4: %08x l5: %08x l6: %08x l7: %08x\n",
181 r_w.locals[0], r_w.locals[1], r_w.locals[2], r_w.locals[3],
182 r_w.locals[4], r_w.locals[5], r_w.locals[6], r_w.locals[7]);
183 printk("i0: %08x i1: %08x i2: %08x i3: %08x "
184 "i4: %08x i5: %08x i6: %08x i7: %08x\n",
185 r_w.ins[0], r_w.ins[1], r_w.ins[2], r_w.ins[3],
186 r_w.ins[4], r_w.ins[5], r_w.ins[6], r_w.ins[7]);
187 }
188 #else
189 #define show_regwindow32(regs) do { } while (0)
190 #endif
191
192 static void show_regwindow(struct pt_regs *regs)
193 {
194 struct reg_window __user *rw;
195 struct reg_window *rwk;
196 struct reg_window r_w;
197 mm_segment_t old_fs;
198
199 if ((regs->tstate & TSTATE_PRIV) || !(test_thread_flag(TIF_32BIT))) {
200 __asm__ __volatile__ ("flushw");
201 rw = (struct reg_window __user *)
202 (regs->u_regs[14] + STACK_BIAS);
203 rwk = (struct reg_window *)
204 (regs->u_regs[14] + STACK_BIAS);
205 if (!(regs->tstate & TSTATE_PRIV)) {
206 old_fs = get_fs();
207 set_fs (USER_DS);
208 if (copy_from_user (&r_w, rw, sizeof(r_w))) {
209 set_fs (old_fs);
210 return;
211 }
212 rwk = &r_w;
213 set_fs (old_fs);
214 }
215 } else {
216 show_regwindow32(regs);
217 return;
218 }
219 printk("l0: %016lx l1: %016lx l2: %016lx l3: %016lx\n",
220 rwk->locals[0], rwk->locals[1], rwk->locals[2], rwk->locals[3]);
221 printk("l4: %016lx l5: %016lx l6: %016lx l7: %016lx\n",
222 rwk->locals[4], rwk->locals[5], rwk->locals[6], rwk->locals[7]);
223 printk("i0: %016lx i1: %016lx i2: %016lx i3: %016lx\n",
224 rwk->ins[0], rwk->ins[1], rwk->ins[2], rwk->ins[3]);
225 printk("i4: %016lx i5: %016lx i6: %016lx i7: %016lx\n",
226 rwk->ins[4], rwk->ins[5], rwk->ins[6], rwk->ins[7]);
227 if (regs->tstate & TSTATE_PRIV)
228 print_symbol("I7: <%s>\n", rwk->ins[7]);
229 }
230
231 void show_stackframe(struct sparc_stackf *sf)
232 {
233 unsigned long size;
234 unsigned long *stk;
235 int i;
236
237 printk("l0: %016lx l1: %016lx l2: %016lx l3: %016lx\n"
238 "l4: %016lx l5: %016lx l6: %016lx l7: %016lx\n",
239 sf->locals[0], sf->locals[1], sf->locals[2], sf->locals[3],
240 sf->locals[4], sf->locals[5], sf->locals[6], sf->locals[7]);
241 printk("i0: %016lx i1: %016lx i2: %016lx i3: %016lx\n"
242 "i4: %016lx i5: %016lx fp: %016lx ret_pc: %016lx\n",
243 sf->ins[0], sf->ins[1], sf->ins[2], sf->ins[3],
244 sf->ins[4], sf->ins[5], (unsigned long)sf->fp, sf->callers_pc);
245 printk("sp: %016lx x0: %016lx x1: %016lx x2: %016lx\n"
246 "x3: %016lx x4: %016lx x5: %016lx xx: %016lx\n",
247 (unsigned long)sf->structptr, sf->xargs[0], sf->xargs[1],
248 sf->xargs[2], sf->xargs[3], sf->xargs[4], sf->xargs[5],
249 sf->xxargs[0]);
250 size = ((unsigned long)sf->fp) - ((unsigned long)sf);
251 size -= STACKFRAME_SZ;
252 stk = (unsigned long *)((unsigned long)sf + STACKFRAME_SZ);
253 i = 0;
254 do {
255 printk("s%d: %016lx\n", i++, *stk++);
256 } while ((size -= sizeof(unsigned long)));
257 }
258
259 void show_stackframe32(struct sparc_stackf32 *sf)
260 {
261 unsigned long size;
262 unsigned *stk;
263 int i;
264
265 printk("l0: %08x l1: %08x l2: %08x l3: %08x\n",
266 sf->locals[0], sf->locals[1], sf->locals[2], sf->locals[3]);
267 printk("l4: %08x l5: %08x l6: %08x l7: %08x\n",
268 sf->locals[4], sf->locals[5], sf->locals[6], sf->locals[7]);
269 printk("i0: %08x i1: %08x i2: %08x i3: %08x\n",
270 sf->ins[0], sf->ins[1], sf->ins[2], sf->ins[3]);
271 printk("i4: %08x i5: %08x fp: %08x ret_pc: %08x\n",
272 sf->ins[4], sf->ins[5], sf->fp, sf->callers_pc);
273 printk("sp: %08x x0: %08x x1: %08x x2: %08x\n"
274 "x3: %08x x4: %08x x5: %08x xx: %08x\n",
275 sf->structptr, sf->xargs[0], sf->xargs[1],
276 sf->xargs[2], sf->xargs[3], sf->xargs[4], sf->xargs[5],
277 sf->xxargs[0]);
278 size = ((unsigned long)sf->fp) - ((unsigned long)sf);
279 size -= STACKFRAME32_SZ;
280 stk = (unsigned *)((unsigned long)sf + STACKFRAME32_SZ);
281 i = 0;
282 do {
283 printk("s%d: %08x\n", i++, *stk++);
284 } while ((size -= sizeof(unsigned)));
285 }
286
287 #ifdef CONFIG_SMP
288 static DEFINE_SPINLOCK(regdump_lock);
289 #endif
290
291 void __show_regs(struct pt_regs * regs)
292 {
293 #ifdef CONFIG_SMP
294 unsigned long flags;
295
296 /* Protect against xcall ipis which might lead to livelock on the lock */
297 __asm__ __volatile__("rdpr %%pstate, %0\n\t"
298 "wrpr %0, %1, %%pstate"
299 : "=r" (flags)
300 : "i" (PSTATE_IE));
301 spin_lock(&regdump_lock);
302 #endif
303 printk("TSTATE: %016lx TPC: %016lx TNPC: %016lx Y: %08x %s\n", regs->tstate,
304 regs->tpc, regs->tnpc, regs->y, print_tainted());
305 print_symbol("TPC: <%s>\n", regs->tpc);
306 printk("g0: %016lx g1: %016lx g2: %016lx g3: %016lx\n",
307 regs->u_regs[0], regs->u_regs[1], regs->u_regs[2],
308 regs->u_regs[3]);
309 printk("g4: %016lx g5: %016lx g6: %016lx g7: %016lx\n",
310 regs->u_regs[4], regs->u_regs[5], regs->u_regs[6],
311 regs->u_regs[7]);
312 printk("o0: %016lx o1: %016lx o2: %016lx o3: %016lx\n",
313 regs->u_regs[8], regs->u_regs[9], regs->u_regs[10],
314 regs->u_regs[11]);
315 printk("o4: %016lx o5: %016lx sp: %016lx ret_pc: %016lx\n",
316 regs->u_regs[12], regs->u_regs[13], regs->u_regs[14],
317 regs->u_regs[15]);
318 print_symbol("RPC: <%s>\n", regs->u_regs[15]);
319 show_regwindow(regs);
320 #ifdef CONFIG_SMP
321 spin_unlock(&regdump_lock);
322 __asm__ __volatile__("wrpr %0, 0, %%pstate"
323 : : "r" (flags));
324 #endif
325 }
326
327 #ifdef VERBOSE_SHOWREGS
328 static void idump_from_user (unsigned int *pc)
329 {
330 int i;
331 int code;
332
333 if((((unsigned long) pc) & 3))
334 return;
335
336 pc -= 3;
337 for(i = -3; i < 6; i++) {
338 get_user(code, pc);
339 printk("%c%08x%c",i?' ':'<',code,i?' ':'>');
340 pc++;
341 }
342 printk("\n");
343 }
344 #endif
345
346 void show_regs(struct pt_regs *regs)
347 {
348 #ifdef VERBOSE_SHOWREGS
349 extern long etrap, etraptl1;
350 #endif
351 __show_regs(regs);
352 #if 0
353 #ifdef CONFIG_SMP
354 {
355 extern void smp_report_regs(void);
356
357 smp_report_regs();
358 }
359 #endif
360 #endif
361
362 #ifdef VERBOSE_SHOWREGS
363 if (regs->tpc >= &etrap && regs->tpc < &etraptl1 &&
364 regs->u_regs[14] >= (long)current - PAGE_SIZE &&
365 regs->u_regs[14] < (long)current + 6 * PAGE_SIZE) {
366 printk ("*********parent**********\n");
367 __show_regs((struct pt_regs *)(regs->u_regs[14] + PTREGS_OFF));
368 idump_from_user(((struct pt_regs *)(regs->u_regs[14] + PTREGS_OFF))->tpc);
369 printk ("*********endpar**********\n");
370 }
371 #endif
372 }
373
374 void show_regs32(struct pt_regs32 *regs)
375 {
376 printk("PSR: %08x PC: %08x NPC: %08x Y: %08x %s\n", regs->psr,
377 regs->pc, regs->npc, regs->y, print_tainted());
378 printk("g0: %08x g1: %08x g2: %08x g3: %08x ",
379 regs->u_regs[0], regs->u_regs[1], regs->u_regs[2],
380 regs->u_regs[3]);
381 printk("g4: %08x g5: %08x g6: %08x g7: %08x\n",
382 regs->u_regs[4], regs->u_regs[5], regs->u_regs[6],
383 regs->u_regs[7]);
384 printk("o0: %08x o1: %08x o2: %08x o3: %08x ",
385 regs->u_regs[8], regs->u_regs[9], regs->u_regs[10],
386 regs->u_regs[11]);
387 printk("o4: %08x o5: %08x sp: %08x ret_pc: %08x\n",
388 regs->u_regs[12], regs->u_regs[13], regs->u_regs[14],
389 regs->u_regs[15]);
390 }
391
392 unsigned long thread_saved_pc(struct task_struct *tsk)
393 {
394 struct thread_info *ti = task_thread_info(tsk);
395 unsigned long ret = 0xdeadbeefUL;
396
397 if (ti && ti->ksp) {
398 unsigned long *sp;
399 sp = (unsigned long *)(ti->ksp + STACK_BIAS);
400 if (((unsigned long)sp & (sizeof(long) - 1)) == 0UL &&
401 sp[14]) {
402 unsigned long *fp;
403 fp = (unsigned long *)(sp[14] + STACK_BIAS);
404 if (((unsigned long)fp & (sizeof(long) - 1)) == 0UL)
405 ret = fp[15];
406 }
407 }
408 return ret;
409 }
410
411 /* Free current thread data structures etc.. */
412 void exit_thread(void)
413 {
414 struct thread_info *t = current_thread_info();
415
416 if (t->utraps) {
417 if (t->utraps[0] < 2)
418 kfree (t->utraps);
419 else
420 t->utraps[0]--;
421 }
422
423 if (test_and_clear_thread_flag(TIF_PERFCTR)) {
424 t->user_cntd0 = t->user_cntd1 = NULL;
425 t->pcr_reg = 0;
426 write_pcr(0);
427 }
428 }
429
430 void flush_thread(void)
431 {
432 struct thread_info *t = current_thread_info();
433 struct mm_struct *mm;
434
435 if (t->flags & _TIF_ABI_PENDING)
436 t->flags ^= (_TIF_ABI_PENDING | _TIF_32BIT);
437
438 mm = t->task->mm;
439 if (mm)
440 tsb_context_switch(mm);
441
442 set_thread_wsaved(0);
443
444 /* Turn off performance counters if on. */
445 if (test_and_clear_thread_flag(TIF_PERFCTR)) {
446 t->user_cntd0 = t->user_cntd1 = NULL;
447 t->pcr_reg = 0;
448 write_pcr(0);
449 }
450
451 /* Clear FPU register state. */
452 t->fpsaved[0] = 0;
453
454 if (get_thread_current_ds() != ASI_AIUS)
455 set_fs(USER_DS);
456
457 /* Init new signal delivery disposition. */
458 clear_thread_flag(TIF_NEWSIGNALS);
459 }
460
461 /* It's a bit more tricky when 64-bit tasks are involved... */
462 static unsigned long clone_stackframe(unsigned long csp, unsigned long psp)
463 {
464 unsigned long fp, distance, rval;
465
466 if (!(test_thread_flag(TIF_32BIT))) {
467 csp += STACK_BIAS;
468 psp += STACK_BIAS;
469 __get_user(fp, &(((struct reg_window __user *)psp)->ins[6]));
470 fp += STACK_BIAS;
471 } else
472 __get_user(fp, &(((struct reg_window32 __user *)psp)->ins[6]));
473
474 /* Now 8-byte align the stack as this is mandatory in the
475 * Sparc ABI due to how register windows work. This hides
476 * the restriction from thread libraries etc. -DaveM
477 */
478 csp &= ~7UL;
479
480 distance = fp - psp;
481 rval = (csp - distance);
482 if (copy_in_user((void __user *) rval, (void __user *) psp, distance))
483 rval = 0;
484 else if (test_thread_flag(TIF_32BIT)) {
485 if (put_user(((u32)csp),
486 &(((struct reg_window32 __user *)rval)->ins[6])))
487 rval = 0;
488 } else {
489 if (put_user(((u64)csp - STACK_BIAS),
490 &(((struct reg_window __user *)rval)->ins[6])))
491 rval = 0;
492 else
493 rval = rval - STACK_BIAS;
494 }
495
496 return rval;
497 }
498
499 /* Standard stuff. */
500 static inline void shift_window_buffer(int first_win, int last_win,
501 struct thread_info *t)
502 {
503 int i;
504
505 for (i = first_win; i < last_win; i++) {
506 t->rwbuf_stkptrs[i] = t->rwbuf_stkptrs[i+1];
507 memcpy(&t->reg_window[i], &t->reg_window[i+1],
508 sizeof(struct reg_window));
509 }
510 }
511
512 void synchronize_user_stack(void)
513 {
514 struct thread_info *t = current_thread_info();
515 unsigned long window;
516
517 flush_user_windows();
518 if ((window = get_thread_wsaved()) != 0) {
519 int winsize = sizeof(struct reg_window);
520 int bias = 0;
521
522 if (test_thread_flag(TIF_32BIT))
523 winsize = sizeof(struct reg_window32);
524 else
525 bias = STACK_BIAS;
526
527 window -= 1;
528 do {
529 unsigned long sp = (t->rwbuf_stkptrs[window] + bias);
530 struct reg_window *rwin = &t->reg_window[window];
531
532 if (!copy_to_user((char __user *)sp, rwin, winsize)) {
533 shift_window_buffer(window, get_thread_wsaved() - 1, t);
534 set_thread_wsaved(get_thread_wsaved() - 1);
535 }
536 } while (window--);
537 }
538 }
539
540 static void stack_unaligned(unsigned long sp)
541 {
542 siginfo_t info;
543
544 info.si_signo = SIGBUS;
545 info.si_errno = 0;
546 info.si_code = BUS_ADRALN;
547 info.si_addr = (void __user *) sp;
548 info.si_trapno = 0;
549 force_sig_info(SIGBUS, &info, current);
550 }
551
552 void fault_in_user_windows(void)
553 {
554 struct thread_info *t = current_thread_info();
555 unsigned long window;
556 int winsize = sizeof(struct reg_window);
557 int bias = 0;
558
559 if (test_thread_flag(TIF_32BIT))
560 winsize = sizeof(struct reg_window32);
561 else
562 bias = STACK_BIAS;
563
564 flush_user_windows();
565 window = get_thread_wsaved();
566
567 if (likely(window != 0)) {
568 window -= 1;
569 do {
570 unsigned long sp = (t->rwbuf_stkptrs[window] + bias);
571 struct reg_window *rwin = &t->reg_window[window];
572
573 if (unlikely(sp & 0x7UL))
574 stack_unaligned(sp);
575
576 if (unlikely(copy_to_user((char __user *)sp,
577 rwin, winsize)))
578 goto barf;
579 } while (window--);
580 }
581 set_thread_wsaved(0);
582 return;
583
584 barf:
585 set_thread_wsaved(window + 1);
586 do_exit(SIGILL);
587 }
588
589 asmlinkage long sparc_do_fork(unsigned long clone_flags,
590 unsigned long stack_start,
591 struct pt_regs *regs,
592 unsigned long stack_size)
593 {
594 int __user *parent_tid_ptr, *child_tid_ptr;
595
596 #ifdef CONFIG_COMPAT
597 if (test_thread_flag(TIF_32BIT)) {
598 parent_tid_ptr = compat_ptr(regs->u_regs[UREG_I2]);
599 child_tid_ptr = compat_ptr(regs->u_regs[UREG_I4]);
600 } else
601 #endif
602 {
603 parent_tid_ptr = (int __user *) regs->u_regs[UREG_I2];
604 child_tid_ptr = (int __user *) regs->u_regs[UREG_I4];
605 }
606
607 return do_fork(clone_flags, stack_start,
608 regs, stack_size,
609 parent_tid_ptr, child_tid_ptr);
610 }
611
612 /* Copy a Sparc thread. The fork() return value conventions
613 * under SunOS are nothing short of bletcherous:
614 * Parent --> %o0 == childs pid, %o1 == 0
615 * Child --> %o0 == parents pid, %o1 == 1
616 */
617 int copy_thread(int nr, unsigned long clone_flags, unsigned long sp,
618 unsigned long unused,
619 struct task_struct *p, struct pt_regs *regs)
620 {
621 struct thread_info *t = task_thread_info(p);
622 char *child_trap_frame;
623
624 /* Calculate offset to stack_frame & pt_regs */
625 child_trap_frame = task_stack_page(p) + (THREAD_SIZE - (TRACEREG_SZ+STACKFRAME_SZ));
626 memcpy(child_trap_frame, (((struct sparc_stackf *)regs)-1), (TRACEREG_SZ+STACKFRAME_SZ));
627
628 t->flags = (t->flags & ~((0xffUL << TI_FLAG_CWP_SHIFT) | (0xffUL << TI_FLAG_CURRENT_DS_SHIFT))) |
629 (((regs->tstate + 1) & TSTATE_CWP) << TI_FLAG_CWP_SHIFT);
630 t->new_child = 1;
631 t->ksp = ((unsigned long) child_trap_frame) - STACK_BIAS;
632 t->kregs = (struct pt_regs *)(child_trap_frame+sizeof(struct sparc_stackf));
633 t->fpsaved[0] = 0;
634
635 if (regs->tstate & TSTATE_PRIV) {
636 /* Special case, if we are spawning a kernel thread from
637 * a userspace task (via KMOD, NFS, or similar) we must
638 * disable performance counters in the child because the
639 * address space and protection realm are changing.
640 */
641 if (t->flags & _TIF_PERFCTR) {
642 t->user_cntd0 = t->user_cntd1 = NULL;
643 t->pcr_reg = 0;
644 t->flags &= ~_TIF_PERFCTR;
645 }
646 t->kregs->u_regs[UREG_FP] = t->ksp;
647 t->flags |= ((long)ASI_P << TI_FLAG_CURRENT_DS_SHIFT);
648 flush_register_windows();
649 memcpy((void *)(t->ksp + STACK_BIAS),
650 (void *)(regs->u_regs[UREG_FP] + STACK_BIAS),
651 sizeof(struct sparc_stackf));
652 t->kregs->u_regs[UREG_G6] = (unsigned long) t;
653 t->kregs->u_regs[UREG_G4] = (unsigned long) t->task;
654 } else {
655 if (t->flags & _TIF_32BIT) {
656 sp &= 0x00000000ffffffffUL;
657 regs->u_regs[UREG_FP] &= 0x00000000ffffffffUL;
658 }
659 t->kregs->u_regs[UREG_FP] = sp;
660 t->flags |= ((long)ASI_AIUS << TI_FLAG_CURRENT_DS_SHIFT);
661 if (sp != regs->u_regs[UREG_FP]) {
662 unsigned long csp;
663
664 csp = clone_stackframe(sp, regs->u_regs[UREG_FP]);
665 if (!csp)
666 return -EFAULT;
667 t->kregs->u_regs[UREG_FP] = csp;
668 }
669 if (t->utraps)
670 t->utraps[0]++;
671 }
672
673 /* Set the return value for the child. */
674 t->kregs->u_regs[UREG_I0] = current->pid;
675 t->kregs->u_regs[UREG_I1] = 1;
676
677 /* Set the second return value for the parent. */
678 regs->u_regs[UREG_I1] = 0;
679
680 if (clone_flags & CLONE_SETTLS)
681 t->kregs->u_regs[UREG_G7] = regs->u_regs[UREG_I3];
682
683 return 0;
684 }
685
686 /*
687 * This is the mechanism for creating a new kernel thread.
688 *
689 * NOTE! Only a kernel-only process(ie the swapper or direct descendants
690 * who haven't done an "execve()") should use this: it will work within
691 * a system call from a "real" process, but the process memory space will
692 * not be free'd until both the parent and the child have exited.
693 */
694 pid_t kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
695 {
696 long retval;
697
698 /* If the parent runs before fn(arg) is called by the child,
699 * the input registers of this function can be clobbered.
700 * So we stash 'fn' and 'arg' into global registers which
701 * will not be modified by the parent.
702 */
703 __asm__ __volatile__("mov %4, %%g2\n\t" /* Save FN into global */
704 "mov %5, %%g3\n\t" /* Save ARG into global */
705 "mov %1, %%g1\n\t" /* Clone syscall nr. */
706 "mov %2, %%o0\n\t" /* Clone flags. */
707 "mov 0, %%o1\n\t" /* usp arg == 0 */
708 "t 0x6d\n\t" /* Linux/Sparc clone(). */
709 "brz,a,pn %%o1, 1f\n\t" /* Parent, just return. */
710 " mov %%o0, %0\n\t"
711 "jmpl %%g2, %%o7\n\t" /* Call the function. */
712 " mov %%g3, %%o0\n\t" /* Set arg in delay. */
713 "mov %3, %%g1\n\t"
714 "t 0x6d\n\t" /* Linux/Sparc exit(). */
715 /* Notreached by child. */
716 "1:" :
717 "=r" (retval) :
718 "i" (__NR_clone), "r" (flags | CLONE_VM | CLONE_UNTRACED),
719 "i" (__NR_exit), "r" (fn), "r" (arg) :
720 "g1", "g2", "g3", "o0", "o1", "memory", "cc");
721 return retval;
722 }
723
724 /*
725 * fill in the user structure for a core dump..
726 */
727 void dump_thread(struct pt_regs * regs, struct user * dump)
728 {
729 /* Only should be used for SunOS and ancient a.out
730 * SparcLinux binaries... Not worth implementing.
731 */
732 memset(dump, 0, sizeof(struct user));
733 }
734
735 typedef struct {
736 union {
737 unsigned int pr_regs[32];
738 unsigned long pr_dregs[16];
739 } pr_fr;
740 unsigned int __unused;
741 unsigned int pr_fsr;
742 unsigned char pr_qcnt;
743 unsigned char pr_q_entrysize;
744 unsigned char pr_en;
745 unsigned int pr_q[64];
746 } elf_fpregset_t32;
747
748 /*
749 * fill in the fpu structure for a core dump.
750 */
751 int dump_fpu (struct pt_regs * regs, elf_fpregset_t * fpregs)
752 {
753 unsigned long *kfpregs = current_thread_info()->fpregs;
754 unsigned long fprs = current_thread_info()->fpsaved[0];
755
756 if (test_thread_flag(TIF_32BIT)) {
757 elf_fpregset_t32 *fpregs32 = (elf_fpregset_t32 *)fpregs;
758
759 if (fprs & FPRS_DL)
760 memcpy(&fpregs32->pr_fr.pr_regs[0], kfpregs,
761 sizeof(unsigned int) * 32);
762 else
763 memset(&fpregs32->pr_fr.pr_regs[0], 0,
764 sizeof(unsigned int) * 32);
765 fpregs32->pr_qcnt = 0;
766 fpregs32->pr_q_entrysize = 8;
767 memset(&fpregs32->pr_q[0], 0,
768 (sizeof(unsigned int) * 64));
769 if (fprs & FPRS_FEF) {
770 fpregs32->pr_fsr = (unsigned int) current_thread_info()->xfsr[0];
771 fpregs32->pr_en = 1;
772 } else {
773 fpregs32->pr_fsr = 0;
774 fpregs32->pr_en = 0;
775 }
776 } else {
777 if(fprs & FPRS_DL)
778 memcpy(&fpregs->pr_regs[0], kfpregs,
779 sizeof(unsigned int) * 32);
780 else
781 memset(&fpregs->pr_regs[0], 0,
782 sizeof(unsigned int) * 32);
783 if(fprs & FPRS_DU)
784 memcpy(&fpregs->pr_regs[16], kfpregs+16,
785 sizeof(unsigned int) * 32);
786 else
787 memset(&fpregs->pr_regs[16], 0,
788 sizeof(unsigned int) * 32);
789 if(fprs & FPRS_FEF) {
790 fpregs->pr_fsr = current_thread_info()->xfsr[0];
791 fpregs->pr_gsr = current_thread_info()->gsr[0];
792 } else {
793 fpregs->pr_fsr = fpregs->pr_gsr = 0;
794 }
795 fpregs->pr_fprs = fprs;
796 }
797 return 1;
798 }
799
800 /*
801 * sparc_execve() executes a new program after the asm stub has set
802 * things up for us. This should basically do what I want it to.
803 */
804 asmlinkage int sparc_execve(struct pt_regs *regs)
805 {
806 int error, base = 0;
807 char *filename;
808
809 /* User register window flush is done by entry.S */
810
811 /* Check for indirect call. */
812 if (regs->u_regs[UREG_G1] == 0)
813 base = 1;
814
815 filename = getname((char __user *)regs->u_regs[base + UREG_I0]);
816 error = PTR_ERR(filename);
817 if (IS_ERR(filename))
818 goto out;
819 error = do_execve(filename,
820 (char __user * __user *)
821 regs->u_regs[base + UREG_I1],
822 (char __user * __user *)
823 regs->u_regs[base + UREG_I2], regs);
824 putname(filename);
825 if (!error) {
826 fprs_write(0);
827 current_thread_info()->xfsr[0] = 0;
828 current_thread_info()->fpsaved[0] = 0;
829 regs->tstate &= ~TSTATE_PEF;
830 task_lock(current);
831 current->ptrace &= ~PT_DTRACE;
832 task_unlock(current);
833 }
834 out:
835 return error;
836 }
837
838 unsigned long get_wchan(struct task_struct *task)
839 {
840 unsigned long pc, fp, bias = 0;
841 unsigned long thread_info_base;
842 struct reg_window *rw;
843 unsigned long ret = 0;
844 int count = 0;
845
846 if (!task || task == current ||
847 task->state == TASK_RUNNING)
848 goto out;
849
850 thread_info_base = (unsigned long) task_stack_page(task);
851 bias = STACK_BIAS;
852 fp = task_thread_info(task)->ksp + bias;
853
854 do {
855 /* Bogus frame pointer? */
856 if (fp < (thread_info_base + sizeof(struct thread_info)) ||
857 fp >= (thread_info_base + THREAD_SIZE))
858 break;
859 rw = (struct reg_window *) fp;
860 pc = rw->ins[7];
861 if (!in_sched_functions(pc)) {
862 ret = pc;
863 goto out;
864 }
865 fp = rw->ins[6] + bias;
866 } while (++count < 16);
867
868 out:
869 return ret;
870 }