blob: 75c1408a664f51f9b00f6e936b131cf429a57b39 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $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
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/errno.h>
16#include <linux/module.h>
17#include <linux/sched.h>
18#include <linux/kernel.h>
19#include <linux/kallsyms.h>
20#include <linux/mm.h>
Alexey Dobriyan4e950f62007-07-30 02:36:13 +040021#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/stddef.h>
24#include <linux/ptrace.h>
25#include <linux/slab.h>
26#include <linux/user.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/reboot.h>
28#include <linux/delay.h>
29#include <linux/compat.h>
David S. Miller038cb012007-02-22 06:24:45 -080030#include <linux/tick.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/init.h>
David S. Millere02044092007-07-16 03:49:40 -070032#include <linux/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34#include <asm/oplib.h>
35#include <asm/uaccess.h>
36#include <asm/system.h>
37#include <asm/page.h>
38#include <asm/pgalloc.h>
39#include <asm/pgtable.h>
40#include <asm/processor.h>
41#include <asm/pstate.h>
42#include <asm/elf.h>
43#include <asm/fpumacro.h>
44#include <asm/head.h>
45#include <asm/cpudata.h>
David S. Miller74bf4312006-01-31 18:29:18 -080046#include <asm/mmu_context.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/unistd.h>
David S. Miller30c91d52006-02-21 16:55:23 -080048#include <asm/hypervisor.h>
David S. Miller22d6a1c2007-05-25 00:37:12 -070049#include <asm/sstate.h>
David S. Millerc3c25242008-02-19 20:39:18 -080050#include <asm/reboot.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52/* #define VERBOSE_SHOWREGS */
53
David S. Millere02044092007-07-16 03:49:40 -070054static void sparc64_yield(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
David S. Miller30c91d52006-02-21 16:55:23 -080056 if (tlb_type != hypervisor)
57 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
David S. Miller30c91d52006-02-21 16:55:23 -080059 clear_thread_flag(TIF_POLLING_NRFLAG);
60 smp_mb__after_clear_bit();
David S. Miller1bd0cd72006-02-21 15:41:01 -080061
David S. Millere02044092007-07-16 03:49:40 -070062 while (!need_resched() && !cpu_is_offline(cpu)) {
David S. Miller30c91d52006-02-21 16:55:23 -080063 unsigned long pstate;
David S. Miller1bd0cd72006-02-21 15:41:01 -080064
David S. Miller30c91d52006-02-21 16:55:23 -080065 /* Disable interrupts. */
66 __asm__ __volatile__(
67 "rdpr %%pstate, %0\n\t"
68 "andn %0, %1, %0\n\t"
69 "wrpr %0, %%g0, %%pstate"
70 : "=&r" (pstate)
71 : "i" (PSTATE_IE));
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
David S. Millere02044092007-07-16 03:49:40 -070073 if (!need_resched() && !cpu_is_offline(cpu))
David S. Miller30c91d52006-02-21 16:55:23 -080074 sun4v_cpu_yield();
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
David S. Miller30c91d52006-02-21 16:55:23 -080076 /* Re-enable interrupts. */
77 __asm__ __volatile__(
78 "rdpr %%pstate, %0\n\t"
79 "or %0, %1, %0\n\t"
80 "wrpr %0, %%g0, %%pstate"
81 : "=&r" (pstate)
82 : "i" (PSTATE_IE));
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 }
David S. Miller30c91d52006-02-21 16:55:23 -080084
85 set_thread_flag(TIF_POLLING_NRFLAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
87
David S. Miller30c91d52006-02-21 16:55:23 -080088/* The idle loop on sparc64. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070089void cpu_idle(void)
90{
David S. Millere02044092007-07-16 03:49:40 -070091 int cpu = smp_processor_id();
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 set_thread_flag(TIF_POLLING_NRFLAG);
Nick Piggin64c7c8f2005-11-08 21:39:04 -080094
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 while(1) {
David S. Miller038cb012007-02-22 06:24:45 -080096 tick_nohz_stop_sched_tick();
David S. Millere02044092007-07-16 03:49:40 -070097
98 while (!need_resched() && !cpu_is_offline(cpu))
99 sparc64_yield(cpu);
100
David S. Miller038cb012007-02-22 06:24:45 -0800101 tick_nohz_restart_sched_tick();
102
103 preempt_enable_no_resched();
David S. Millere02044092007-07-16 03:49:40 -0700104
105#ifdef CONFIG_HOTPLUG_CPU
106 if (cpu_is_offline(cpu))
107 cpu_play_dead();
108#endif
109
David S. Miller038cb012007-02-22 06:24:45 -0800110 schedule();
111 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 }
113}
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115extern char reboot_command [];
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117void machine_halt(void)
118{
David S. Miller22d6a1c2007-05-25 00:37:12 -0700119 sstate_halt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 prom_halt();
121 panic("Halt failed!");
122}
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124void machine_alt_power_off(void)
125{
David S. Miller22d6a1c2007-05-25 00:37:12 -0700126 sstate_poweroff();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 prom_halt_power_off();
128 panic("Power-off failed!");
129}
130
131void machine_restart(char * cmd)
132{
133 char *p;
134
David S. Miller22d6a1c2007-05-25 00:37:12 -0700135 sstate_reboot();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 p = strchr (reboot_command, '\n');
137 if (p) *p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 if (cmd)
139 prom_reboot(cmd);
140 if (*reboot_command)
141 prom_reboot(reboot_command);
142 prom_reboot("");
143 panic("Reboot failed!");
144}
145
David S. Miller959a85a2006-01-18 14:58:05 -0800146#ifdef CONFIG_COMPAT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147static void show_regwindow32(struct pt_regs *regs)
148{
149 struct reg_window32 __user *rw;
150 struct reg_window32 r_w;
151 mm_segment_t old_fs;
152
153 __asm__ __volatile__ ("flushw");
154 rw = compat_ptr((unsigned)regs->u_regs[14]);
155 old_fs = get_fs();
156 set_fs (USER_DS);
157 if (copy_from_user (&r_w, rw, sizeof(r_w))) {
158 set_fs (old_fs);
159 return;
160 }
161
162 set_fs (old_fs);
163 printk("l0: %08x l1: %08x l2: %08x l3: %08x "
164 "l4: %08x l5: %08x l6: %08x l7: %08x\n",
165 r_w.locals[0], r_w.locals[1], r_w.locals[2], r_w.locals[3],
166 r_w.locals[4], r_w.locals[5], r_w.locals[6], r_w.locals[7]);
167 printk("i0: %08x i1: %08x i2: %08x i3: %08x "
168 "i4: %08x i5: %08x i6: %08x i7: %08x\n",
169 r_w.ins[0], r_w.ins[1], r_w.ins[2], r_w.ins[3],
170 r_w.ins[4], r_w.ins[5], r_w.ins[6], r_w.ins[7]);
171}
David S. Miller959a85a2006-01-18 14:58:05 -0800172#else
173#define show_regwindow32(regs) do { } while (0)
174#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176static void show_regwindow(struct pt_regs *regs)
177{
178 struct reg_window __user *rw;
179 struct reg_window *rwk;
180 struct reg_window r_w;
181 mm_segment_t old_fs;
182
183 if ((regs->tstate & TSTATE_PRIV) || !(test_thread_flag(TIF_32BIT))) {
184 __asm__ __volatile__ ("flushw");
185 rw = (struct reg_window __user *)
186 (regs->u_regs[14] + STACK_BIAS);
187 rwk = (struct reg_window *)
188 (regs->u_regs[14] + STACK_BIAS);
189 if (!(regs->tstate & TSTATE_PRIV)) {
190 old_fs = get_fs();
191 set_fs (USER_DS);
192 if (copy_from_user (&r_w, rw, sizeof(r_w))) {
193 set_fs (old_fs);
194 return;
195 }
196 rwk = &r_w;
197 set_fs (old_fs);
198 }
199 } else {
200 show_regwindow32(regs);
201 return;
202 }
203 printk("l0: %016lx l1: %016lx l2: %016lx l3: %016lx\n",
204 rwk->locals[0], rwk->locals[1], rwk->locals[2], rwk->locals[3]);
205 printk("l4: %016lx l5: %016lx l6: %016lx l7: %016lx\n",
206 rwk->locals[4], rwk->locals[5], rwk->locals[6], rwk->locals[7]);
207 printk("i0: %016lx i1: %016lx i2: %016lx i3: %016lx\n",
208 rwk->ins[0], rwk->ins[1], rwk->ins[2], rwk->ins[3]);
209 printk("i4: %016lx i5: %016lx i6: %016lx i7: %016lx\n",
210 rwk->ins[4], rwk->ins[5], rwk->ins[6], rwk->ins[7]);
211 if (regs->tstate & TSTATE_PRIV)
212 print_symbol("I7: <%s>\n", rwk->ins[7]);
213}
214
215void show_stackframe(struct sparc_stackf *sf)
216{
217 unsigned long size;
218 unsigned long *stk;
219 int i;
220
221 printk("l0: %016lx l1: %016lx l2: %016lx l3: %016lx\n"
222 "l4: %016lx l5: %016lx l6: %016lx l7: %016lx\n",
223 sf->locals[0], sf->locals[1], sf->locals[2], sf->locals[3],
224 sf->locals[4], sf->locals[5], sf->locals[6], sf->locals[7]);
225 printk("i0: %016lx i1: %016lx i2: %016lx i3: %016lx\n"
226 "i4: %016lx i5: %016lx fp: %016lx ret_pc: %016lx\n",
227 sf->ins[0], sf->ins[1], sf->ins[2], sf->ins[3],
228 sf->ins[4], sf->ins[5], (unsigned long)sf->fp, sf->callers_pc);
229 printk("sp: %016lx x0: %016lx x1: %016lx x2: %016lx\n"
230 "x3: %016lx x4: %016lx x5: %016lx xx: %016lx\n",
231 (unsigned long)sf->structptr, sf->xargs[0], sf->xargs[1],
232 sf->xargs[2], sf->xargs[3], sf->xargs[4], sf->xargs[5],
233 sf->xxargs[0]);
234 size = ((unsigned long)sf->fp) - ((unsigned long)sf);
235 size -= STACKFRAME_SZ;
236 stk = (unsigned long *)((unsigned long)sf + STACKFRAME_SZ);
237 i = 0;
238 do {
239 printk("s%d: %016lx\n", i++, *stk++);
240 } while ((size -= sizeof(unsigned long)));
241}
242
243void show_stackframe32(struct sparc_stackf32 *sf)
244{
245 unsigned long size;
246 unsigned *stk;
247 int i;
248
249 printk("l0: %08x l1: %08x l2: %08x l3: %08x\n",
250 sf->locals[0], sf->locals[1], sf->locals[2], sf->locals[3]);
251 printk("l4: %08x l5: %08x l6: %08x l7: %08x\n",
252 sf->locals[4], sf->locals[5], sf->locals[6], sf->locals[7]);
253 printk("i0: %08x i1: %08x i2: %08x i3: %08x\n",
254 sf->ins[0], sf->ins[1], sf->ins[2], sf->ins[3]);
255 printk("i4: %08x i5: %08x fp: %08x ret_pc: %08x\n",
256 sf->ins[4], sf->ins[5], sf->fp, sf->callers_pc);
257 printk("sp: %08x x0: %08x x1: %08x x2: %08x\n"
258 "x3: %08x x4: %08x x5: %08x xx: %08x\n",
259 sf->structptr, sf->xargs[0], sf->xargs[1],
260 sf->xargs[2], sf->xargs[3], sf->xargs[4], sf->xargs[5],
261 sf->xxargs[0]);
262 size = ((unsigned long)sf->fp) - ((unsigned long)sf);
263 size -= STACKFRAME32_SZ;
264 stk = (unsigned *)((unsigned long)sf + STACKFRAME32_SZ);
265 i = 0;
266 do {
267 printk("s%d: %08x\n", i++, *stk++);
268 } while ((size -= sizeof(unsigned)));
269}
270
271#ifdef CONFIG_SMP
272static DEFINE_SPINLOCK(regdump_lock);
273#endif
274
275void __show_regs(struct pt_regs * regs)
276{
277#ifdef CONFIG_SMP
278 unsigned long flags;
279
280 /* Protect against xcall ipis which might lead to livelock on the lock */
281 __asm__ __volatile__("rdpr %%pstate, %0\n\t"
282 "wrpr %0, %1, %%pstate"
283 : "=r" (flags)
284 : "i" (PSTATE_IE));
285 spin_lock(&regdump_lock);
286#endif
287 printk("TSTATE: %016lx TPC: %016lx TNPC: %016lx Y: %08x %s\n", regs->tstate,
288 regs->tpc, regs->tnpc, regs->y, print_tainted());
289 print_symbol("TPC: <%s>\n", regs->tpc);
290 printk("g0: %016lx g1: %016lx g2: %016lx g3: %016lx\n",
291 regs->u_regs[0], regs->u_regs[1], regs->u_regs[2],
292 regs->u_regs[3]);
293 printk("g4: %016lx g5: %016lx g6: %016lx g7: %016lx\n",
294 regs->u_regs[4], regs->u_regs[5], regs->u_regs[6],
295 regs->u_regs[7]);
296 printk("o0: %016lx o1: %016lx o2: %016lx o3: %016lx\n",
297 regs->u_regs[8], regs->u_regs[9], regs->u_regs[10],
298 regs->u_regs[11]);
299 printk("o4: %016lx o5: %016lx sp: %016lx ret_pc: %016lx\n",
300 regs->u_regs[12], regs->u_regs[13], regs->u_regs[14],
301 regs->u_regs[15]);
302 print_symbol("RPC: <%s>\n", regs->u_regs[15]);
303 show_regwindow(regs);
304#ifdef CONFIG_SMP
305 spin_unlock(&regdump_lock);
306 __asm__ __volatile__("wrpr %0, 0, %%pstate"
307 : : "r" (flags));
308#endif
309}
310
311#ifdef VERBOSE_SHOWREGS
312static void idump_from_user (unsigned int *pc)
313{
314 int i;
315 int code;
316
317 if((((unsigned long) pc) & 3))
318 return;
319
320 pc -= 3;
321 for(i = -3; i < 6; i++) {
322 get_user(code, pc);
323 printk("%c%08x%c",i?' ':'<',code,i?' ':'>');
324 pc++;
325 }
326 printk("\n");
327}
328#endif
329
330void show_regs(struct pt_regs *regs)
331{
332#ifdef VERBOSE_SHOWREGS
333 extern long etrap, etraptl1;
334#endif
335 __show_regs(regs);
David S. Miller19a0d582006-02-17 01:17:21 -0800336#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337#ifdef CONFIG_SMP
338 {
339 extern void smp_report_regs(void);
340
341 smp_report_regs();
342 }
343#endif
David S. Miller19a0d582006-02-17 01:17:21 -0800344#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346#ifdef VERBOSE_SHOWREGS
347 if (regs->tpc >= &etrap && regs->tpc < &etraptl1 &&
348 regs->u_regs[14] >= (long)current - PAGE_SIZE &&
349 regs->u_regs[14] < (long)current + 6 * PAGE_SIZE) {
350 printk ("*********parent**********\n");
351 __show_regs((struct pt_regs *)(regs->u_regs[14] + PTREGS_OFF));
352 idump_from_user(((struct pt_regs *)(regs->u_regs[14] + PTREGS_OFF))->tpc);
353 printk ("*********endpar**********\n");
354 }
355#endif
356}
357
358void show_regs32(struct pt_regs32 *regs)
359{
360 printk("PSR: %08x PC: %08x NPC: %08x Y: %08x %s\n", regs->psr,
361 regs->pc, regs->npc, regs->y, print_tainted());
362 printk("g0: %08x g1: %08x g2: %08x g3: %08x ",
363 regs->u_regs[0], regs->u_regs[1], regs->u_regs[2],
364 regs->u_regs[3]);
365 printk("g4: %08x g5: %08x g6: %08x g7: %08x\n",
366 regs->u_regs[4], regs->u_regs[5], regs->u_regs[6],
367 regs->u_regs[7]);
368 printk("o0: %08x o1: %08x o2: %08x o3: %08x ",
369 regs->u_regs[8], regs->u_regs[9], regs->u_regs[10],
370 regs->u_regs[11]);
371 printk("o4: %08x o5: %08x sp: %08x ret_pc: %08x\n",
372 regs->u_regs[12], regs->u_regs[13], regs->u_regs[14],
373 regs->u_regs[15]);
374}
375
376unsigned long thread_saved_pc(struct task_struct *tsk)
377{
Al Virof3169642006-01-12 01:05:42 -0800378 struct thread_info *ti = task_thread_info(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 unsigned long ret = 0xdeadbeefUL;
380
381 if (ti && ti->ksp) {
382 unsigned long *sp;
383 sp = (unsigned long *)(ti->ksp + STACK_BIAS);
384 if (((unsigned long)sp & (sizeof(long) - 1)) == 0UL &&
385 sp[14]) {
386 unsigned long *fp;
387 fp = (unsigned long *)(sp[14] + STACK_BIAS);
388 if (((unsigned long)fp & (sizeof(long) - 1)) == 0UL)
389 ret = fp[15];
390 }
391 }
392 return ret;
393}
394
395/* Free current thread data structures etc.. */
396void exit_thread(void)
397{
398 struct thread_info *t = current_thread_info();
399
400 if (t->utraps) {
401 if (t->utraps[0] < 2)
402 kfree (t->utraps);
403 else
404 t->utraps[0]--;
405 }
406
407 if (test_and_clear_thread_flag(TIF_PERFCTR)) {
408 t->user_cntd0 = t->user_cntd1 = NULL;
409 t->pcr_reg = 0;
410 write_pcr(0);
411 }
412}
413
414void flush_thread(void)
415{
416 struct thread_info *t = current_thread_info();
David S. Miller74bf4312006-01-31 18:29:18 -0800417 struct mm_struct *mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Mathieu Desnoyersc0a79b22007-03-10 00:19:49 -0800419 if (test_ti_thread_flag(t, TIF_ABI_PENDING)) {
420 clear_ti_thread_flag(t, TIF_ABI_PENDING);
421 if (test_ti_thread_flag(t, TIF_32BIT))
422 clear_ti_thread_flag(t, TIF_32BIT);
423 else
424 set_ti_thread_flag(t, TIF_32BIT);
425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
David S. Miller74bf4312006-01-31 18:29:18 -0800427 mm = t->task->mm;
428 if (mm)
David S. Miller98c55842006-01-31 18:31:20 -0800429 tsb_context_switch(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 set_thread_wsaved(0);
432
433 /* Turn off performance counters if on. */
434 if (test_and_clear_thread_flag(TIF_PERFCTR)) {
435 t->user_cntd0 = t->user_cntd1 = NULL;
436 t->pcr_reg = 0;
437 write_pcr(0);
438 }
439
440 /* Clear FPU register state. */
441 t->fpsaved[0] = 0;
442
443 if (get_thread_current_ds() != ASI_AIUS)
444 set_fs(USER_DS);
445
446 /* Init new signal delivery disposition. */
447 clear_thread_flag(TIF_NEWSIGNALS);
448}
449
450/* It's a bit more tricky when 64-bit tasks are involved... */
451static unsigned long clone_stackframe(unsigned long csp, unsigned long psp)
452{
453 unsigned long fp, distance, rval;
454
455 if (!(test_thread_flag(TIF_32BIT))) {
456 csp += STACK_BIAS;
457 psp += STACK_BIAS;
458 __get_user(fp, &(((struct reg_window __user *)psp)->ins[6]));
459 fp += STACK_BIAS;
460 } else
461 __get_user(fp, &(((struct reg_window32 __user *)psp)->ins[6]));
462
463 /* Now 8-byte align the stack as this is mandatory in the
464 * Sparc ABI due to how register windows work. This hides
465 * the restriction from thread libraries etc. -DaveM
466 */
467 csp &= ~7UL;
468
469 distance = fp - psp;
470 rval = (csp - distance);
471 if (copy_in_user((void __user *) rval, (void __user *) psp, distance))
472 rval = 0;
473 else if (test_thread_flag(TIF_32BIT)) {
474 if (put_user(((u32)csp),
475 &(((struct reg_window32 __user *)rval)->ins[6])))
476 rval = 0;
477 } else {
478 if (put_user(((u64)csp - STACK_BIAS),
479 &(((struct reg_window __user *)rval)->ins[6])))
480 rval = 0;
481 else
482 rval = rval - STACK_BIAS;
483 }
484
485 return rval;
486}
487
488/* Standard stuff. */
489static inline void shift_window_buffer(int first_win, int last_win,
490 struct thread_info *t)
491{
492 int i;
493
494 for (i = first_win; i < last_win; i++) {
495 t->rwbuf_stkptrs[i] = t->rwbuf_stkptrs[i+1];
496 memcpy(&t->reg_window[i], &t->reg_window[i+1],
497 sizeof(struct reg_window));
498 }
499}
500
501void synchronize_user_stack(void)
502{
503 struct thread_info *t = current_thread_info();
504 unsigned long window;
505
506 flush_user_windows();
507 if ((window = get_thread_wsaved()) != 0) {
508 int winsize = sizeof(struct reg_window);
509 int bias = 0;
510
511 if (test_thread_flag(TIF_32BIT))
512 winsize = sizeof(struct reg_window32);
513 else
514 bias = STACK_BIAS;
515
516 window -= 1;
517 do {
518 unsigned long sp = (t->rwbuf_stkptrs[window] + bias);
519 struct reg_window *rwin = &t->reg_window[window];
520
521 if (!copy_to_user((char __user *)sp, rwin, winsize)) {
522 shift_window_buffer(window, get_thread_wsaved() - 1, t);
523 set_thread_wsaved(get_thread_wsaved() - 1);
524 }
525 } while (window--);
526 }
527}
528
David S. Miller314ef682006-02-04 00:10:01 -0800529static void stack_unaligned(unsigned long sp)
530{
531 siginfo_t info;
532
533 info.si_signo = SIGBUS;
534 info.si_errno = 0;
535 info.si_code = BUS_ADRALN;
536 info.si_addr = (void __user *) sp;
537 info.si_trapno = 0;
538 force_sig_info(SIGBUS, &info, current);
539}
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541void fault_in_user_windows(void)
542{
543 struct thread_info *t = current_thread_info();
544 unsigned long window;
545 int winsize = sizeof(struct reg_window);
546 int bias = 0;
547
548 if (test_thread_flag(TIF_32BIT))
549 winsize = sizeof(struct reg_window32);
550 else
551 bias = STACK_BIAS;
552
553 flush_user_windows();
554 window = get_thread_wsaved();
555
David S. Miller314ef682006-02-04 00:10:01 -0800556 if (likely(window != 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 window -= 1;
558 do {
559 unsigned long sp = (t->rwbuf_stkptrs[window] + bias);
560 struct reg_window *rwin = &t->reg_window[window];
561
David S. Miller314ef682006-02-04 00:10:01 -0800562 if (unlikely(sp & 0x7UL))
563 stack_unaligned(sp);
564
565 if (unlikely(copy_to_user((char __user *)sp,
566 rwin, winsize)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 goto barf;
568 } while (window--);
569 }
570 set_thread_wsaved(0);
571 return;
572
573barf:
574 set_thread_wsaved(window + 1);
575 do_exit(SIGILL);
576}
577
578asmlinkage long sparc_do_fork(unsigned long clone_flags,
579 unsigned long stack_start,
580 struct pt_regs *regs,
581 unsigned long stack_size)
582{
583 int __user *parent_tid_ptr, *child_tid_ptr;
584
585#ifdef CONFIG_COMPAT
586 if (test_thread_flag(TIF_32BIT)) {
587 parent_tid_ptr = compat_ptr(regs->u_regs[UREG_I2]);
588 child_tid_ptr = compat_ptr(regs->u_regs[UREG_I4]);
589 } else
590#endif
591 {
592 parent_tid_ptr = (int __user *) regs->u_regs[UREG_I2];
593 child_tid_ptr = (int __user *) regs->u_regs[UREG_I4];
594 }
595
596 return do_fork(clone_flags, stack_start,
597 regs, stack_size,
598 parent_tid_ptr, child_tid_ptr);
599}
600
601/* Copy a Sparc thread. The fork() return value conventions
602 * under SunOS are nothing short of bletcherous:
603 * Parent --> %o0 == childs pid, %o1 == 0
604 * Child --> %o0 == parents pid, %o1 == 1
605 */
606int copy_thread(int nr, unsigned long clone_flags, unsigned long sp,
607 unsigned long unused,
608 struct task_struct *p, struct pt_regs *regs)
609{
Al Viroee3eea12006-01-12 01:05:43 -0800610 struct thread_info *t = task_thread_info(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 char *child_trap_frame;
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 /* Calculate offset to stack_frame & pt_regs */
Al Viroee3eea12006-01-12 01:05:43 -0800614 child_trap_frame = task_stack_page(p) + (THREAD_SIZE - (TRACEREG_SZ+STACKFRAME_SZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 memcpy(child_trap_frame, (((struct sparc_stackf *)regs)-1), (TRACEREG_SZ+STACKFRAME_SZ));
616
617 t->flags = (t->flags & ~((0xffUL << TI_FLAG_CWP_SHIFT) | (0xffUL << TI_FLAG_CURRENT_DS_SHIFT))) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 (((regs->tstate + 1) & TSTATE_CWP) << TI_FLAG_CWP_SHIFT);
David S. Millerdb7d9a42005-07-24 19:36:26 -0700619 t->new_child = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 t->ksp = ((unsigned long) child_trap_frame) - STACK_BIAS;
621 t->kregs = (struct pt_regs *)(child_trap_frame+sizeof(struct sparc_stackf));
622 t->fpsaved[0] = 0;
623
624 if (regs->tstate & TSTATE_PRIV) {
625 /* Special case, if we are spawning a kernel thread from
626 * a userspace task (via KMOD, NFS, or similar) we must
627 * disable performance counters in the child because the
628 * address space and protection realm are changing.
629 */
630 if (t->flags & _TIF_PERFCTR) {
631 t->user_cntd0 = t->user_cntd1 = NULL;
632 t->pcr_reg = 0;
633 t->flags &= ~_TIF_PERFCTR;
634 }
635 t->kregs->u_regs[UREG_FP] = t->ksp;
636 t->flags |= ((long)ASI_P << TI_FLAG_CURRENT_DS_SHIFT);
637 flush_register_windows();
638 memcpy((void *)(t->ksp + STACK_BIAS),
639 (void *)(regs->u_regs[UREG_FP] + STACK_BIAS),
640 sizeof(struct sparc_stackf));
641 t->kregs->u_regs[UREG_G6] = (unsigned long) t;
642 t->kregs->u_regs[UREG_G4] = (unsigned long) t->task;
643 } else {
644 if (t->flags & _TIF_32BIT) {
645 sp &= 0x00000000ffffffffUL;
646 regs->u_regs[UREG_FP] &= 0x00000000ffffffffUL;
647 }
648 t->kregs->u_regs[UREG_FP] = sp;
649 t->flags |= ((long)ASI_AIUS << TI_FLAG_CURRENT_DS_SHIFT);
650 if (sp != regs->u_regs[UREG_FP]) {
651 unsigned long csp;
652
653 csp = clone_stackframe(sp, regs->u_regs[UREG_FP]);
654 if (!csp)
655 return -EFAULT;
656 t->kregs->u_regs[UREG_FP] = csp;
657 }
658 if (t->utraps)
659 t->utraps[0]++;
660 }
661
662 /* Set the return value for the child. */
663 t->kregs->u_regs[UREG_I0] = current->pid;
664 t->kregs->u_regs[UREG_I1] = 1;
665
666 /* Set the second return value for the parent. */
667 regs->u_regs[UREG_I1] = 0;
668
669 if (clone_flags & CLONE_SETTLS)
670 t->kregs->u_regs[UREG_G7] = regs->u_regs[UREG_I3];
671
672 return 0;
673}
674
675/*
676 * This is the mechanism for creating a new kernel thread.
677 *
678 * NOTE! Only a kernel-only process(ie the swapper or direct descendants
679 * who haven't done an "execve()") should use this: it will work within
680 * a system call from a "real" process, but the process memory space will
Simon Arlotte5dd42e2007-05-11 13:52:08 -0700681 * not be freed until both the parent and the child have exited.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 */
683pid_t kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
684{
685 long retval;
686
687 /* If the parent runs before fn(arg) is called by the child,
688 * the input registers of this function can be clobbered.
689 * So we stash 'fn' and 'arg' into global registers which
690 * will not be modified by the parent.
691 */
692 __asm__ __volatile__("mov %4, %%g2\n\t" /* Save FN into global */
693 "mov %5, %%g3\n\t" /* Save ARG into global */
694 "mov %1, %%g1\n\t" /* Clone syscall nr. */
695 "mov %2, %%o0\n\t" /* Clone flags. */
696 "mov 0, %%o1\n\t" /* usp arg == 0 */
697 "t 0x6d\n\t" /* Linux/Sparc clone(). */
698 "brz,a,pn %%o1, 1f\n\t" /* Parent, just return. */
699 " mov %%o0, %0\n\t"
700 "jmpl %%g2, %%o7\n\t" /* Call the function. */
701 " mov %%g3, %%o0\n\t" /* Set arg in delay. */
702 "mov %3, %%g1\n\t"
703 "t 0x6d\n\t" /* Linux/Sparc exit(). */
704 /* Notreached by child. */
705 "1:" :
706 "=r" (retval) :
707 "i" (__NR_clone), "r" (flags | CLONE_VM | CLONE_UNTRACED),
708 "i" (__NR_exit), "r" (fn), "r" (arg) :
709 "g1", "g2", "g3", "o0", "o1", "memory", "cc");
710 return retval;
711}
712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713typedef struct {
714 union {
715 unsigned int pr_regs[32];
716 unsigned long pr_dregs[16];
717 } pr_fr;
718 unsigned int __unused;
719 unsigned int pr_fsr;
720 unsigned char pr_qcnt;
721 unsigned char pr_q_entrysize;
722 unsigned char pr_en;
723 unsigned int pr_q[64];
724} elf_fpregset_t32;
725
726/*
727 * fill in the fpu structure for a core dump.
728 */
729int dump_fpu (struct pt_regs * regs, elf_fpregset_t * fpregs)
730{
731 unsigned long *kfpregs = current_thread_info()->fpregs;
732 unsigned long fprs = current_thread_info()->fpsaved[0];
733
734 if (test_thread_flag(TIF_32BIT)) {
735 elf_fpregset_t32 *fpregs32 = (elf_fpregset_t32 *)fpregs;
736
737 if (fprs & FPRS_DL)
738 memcpy(&fpregs32->pr_fr.pr_regs[0], kfpregs,
739 sizeof(unsigned int) * 32);
740 else
741 memset(&fpregs32->pr_fr.pr_regs[0], 0,
742 sizeof(unsigned int) * 32);
743 fpregs32->pr_qcnt = 0;
744 fpregs32->pr_q_entrysize = 8;
745 memset(&fpregs32->pr_q[0], 0,
746 (sizeof(unsigned int) * 64));
747 if (fprs & FPRS_FEF) {
748 fpregs32->pr_fsr = (unsigned int) current_thread_info()->xfsr[0];
749 fpregs32->pr_en = 1;
750 } else {
751 fpregs32->pr_fsr = 0;
752 fpregs32->pr_en = 0;
753 }
754 } else {
755 if(fprs & FPRS_DL)
756 memcpy(&fpregs->pr_regs[0], kfpregs,
757 sizeof(unsigned int) * 32);
758 else
759 memset(&fpregs->pr_regs[0], 0,
760 sizeof(unsigned int) * 32);
761 if(fprs & FPRS_DU)
762 memcpy(&fpregs->pr_regs[16], kfpregs+16,
763 sizeof(unsigned int) * 32);
764 else
765 memset(&fpregs->pr_regs[16], 0,
766 sizeof(unsigned int) * 32);
767 if(fprs & FPRS_FEF) {
768 fpregs->pr_fsr = current_thread_info()->xfsr[0];
769 fpregs->pr_gsr = current_thread_info()->gsr[0];
770 } else {
771 fpregs->pr_fsr = fpregs->pr_gsr = 0;
772 }
773 fpregs->pr_fprs = fprs;
774 }
775 return 1;
776}
777
778/*
779 * sparc_execve() executes a new program after the asm stub has set
780 * things up for us. This should basically do what I want it to.
781 */
782asmlinkage int sparc_execve(struct pt_regs *regs)
783{
784 int error, base = 0;
785 char *filename;
786
787 /* User register window flush is done by entry.S */
788
789 /* Check for indirect call. */
790 if (regs->u_regs[UREG_G1] == 0)
791 base = 1;
792
793 filename = getname((char __user *)regs->u_regs[base + UREG_I0]);
794 error = PTR_ERR(filename);
795 if (IS_ERR(filename))
796 goto out;
797 error = do_execve(filename,
798 (char __user * __user *)
799 regs->u_regs[base + UREG_I1],
800 (char __user * __user *)
801 regs->u_regs[base + UREG_I2], regs);
802 putname(filename);
803 if (!error) {
804 fprs_write(0);
805 current_thread_info()->xfsr[0] = 0;
806 current_thread_info()->fpsaved[0] = 0;
807 regs->tstate &= ~TSTATE_PEF;
808 task_lock(current);
809 current->ptrace &= ~PT_DTRACE;
810 task_unlock(current);
811 }
812out:
813 return error;
814}
815
816unsigned long get_wchan(struct task_struct *task)
817{
818 unsigned long pc, fp, bias = 0;
819 unsigned long thread_info_base;
820 struct reg_window *rw;
821 unsigned long ret = 0;
822 int count = 0;
823
824 if (!task || task == current ||
825 task->state == TASK_RUNNING)
826 goto out;
827
Al Viroee3eea12006-01-12 01:05:43 -0800828 thread_info_base = (unsigned long) task_stack_page(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 bias = STACK_BIAS;
Al Virof3169642006-01-12 01:05:42 -0800830 fp = task_thread_info(task)->ksp + bias;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832 do {
833 /* Bogus frame pointer? */
834 if (fp < (thread_info_base + sizeof(struct thread_info)) ||
835 fp >= (thread_info_base + THREAD_SIZE))
836 break;
837 rw = (struct reg_window *) fp;
838 pc = rw->ins[7];
839 if (!in_sched_functions(pc)) {
840 ret = pc;
841 goto out;
842 }
843 fp = rw->ins[6] + bias;
844 } while (++count < 16);
845
846out:
847 return ret;
848}