]>
Commit | Line | Data |
---|---|---|
5526b7e4 | 1 | /* arch/sparc64/kernel/process.c |
1da177e4 | 2 | * |
93dae5b7 | 3 | * Copyright (C) 1995, 1996, 2008 David S. Miller (davem@davemloft.net) |
1da177e4 LT |
4 | * Copyright (C) 1996 Eddie C. Dost (ecd@skynet.be) |
5 | * Copyright (C) 1997, 1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz) | |
6 | */ | |
7 | ||
8 | /* | |
9 | * This file handles the architecture-dependent parts of process handling.. | |
10 | */ | |
11 | ||
12 | #include <stdarg.h> | |
13 | ||
1da177e4 | 14 | #include <linux/errno.h> |
066bcaca | 15 | #include <linux/export.h> |
1da177e4 | 16 | #include <linux/sched.h> |
b17b0153 | 17 | #include <linux/sched/debug.h> |
1da177e4 | 18 | #include <linux/kernel.h> |
1da177e4 | 19 | #include <linux/mm.h> |
4e950f6f | 20 | #include <linux/fs.h> |
1da177e4 | 21 | #include <linux/smp.h> |
1da177e4 LT |
22 | #include <linux/stddef.h> |
23 | #include <linux/ptrace.h> | |
24 | #include <linux/slab.h> | |
25 | #include <linux/user.h> | |
1da177e4 LT |
26 | #include <linux/delay.h> |
27 | #include <linux/compat.h> | |
038cb01e | 28 | #include <linux/tick.h> |
1da177e4 | 29 | #include <linux/init.h> |
e0204409 | 30 | #include <linux/cpu.h> |
916ca14a | 31 | #include <linux/perf_event.h> |
b1422401 | 32 | #include <linux/elfcore.h> |
93dae5b7 | 33 | #include <linux/sysrq.h> |
802c64b3 | 34 | #include <linux/nmi.h> |
812cb83a | 35 | #include <linux/context_tracking.h> |
1da177e4 | 36 | |
7c0f6ba6 | 37 | #include <linux/uaccess.h> |
1da177e4 LT |
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> | |
74bf4312 | 47 | #include <asm/mmu_context.h> |
1da177e4 | 48 | #include <asm/unistd.h> |
30c91d57 | 49 | #include <asm/hypervisor.h> |
b1422401 | 50 | #include <asm/syscalls.h> |
93dae5b7 DM |
51 | #include <asm/irq_regs.h> |
52 | #include <asm/smp.h> | |
916ca14a | 53 | #include <asm/pcr.h> |
1da177e4 | 54 | |
4f70f7a9 DM |
55 | #include "kstack.h" |
56 | ||
87fa05ae SR |
57 | /* Idle loop support on sparc64. */ |
58 | void arch_cpu_idle(void) | |
1da177e4 | 59 | { |
802c64b3 DM |
60 | if (tlb_type != hypervisor) { |
61 | touch_nmi_watchdog(); | |
cb3042d6 | 62 | local_irq_enable(); |
87fa05ae | 63 | } else { |
30c91d57 DM |
64 | unsigned long pstate; |
65 | ||
cb3042d6 DM |
66 | local_irq_enable(); |
67 | ||
87fa05ae SR |
68 | /* The sun4v sleeping code requires that we have PSTATE.IE cleared over |
69 | * the cpu sleep hypervisor call. | |
70 | */ | |
30c91d57 DM |
71 | __asm__ __volatile__( |
72 | "rdpr %%pstate, %0\n\t" | |
73 | "andn %0, %1, %0\n\t" | |
74 | "wrpr %0, %%g0, %%pstate" | |
75 | : "=&r" (pstate) | |
76 | : "i" (PSTATE_IE)); | |
77 | ||
87fa05ae | 78 | if (!need_resched() && !cpu_is_offline(smp_processor_id())) |
30c91d57 DM |
79 | sun4v_cpu_yield(); |
80 | ||
81 | /* Re-enable interrupts. */ | |
82 | __asm__ __volatile__( | |
83 | "rdpr %%pstate, %0\n\t" | |
84 | "or %0, %1, %0\n\t" | |
85 | "wrpr %0, %%g0, %%pstate" | |
86 | : "=&r" (pstate) | |
87 | : "i" (PSTATE_IE)); | |
1da177e4 | 88 | } |
30c91d57 | 89 | } |
1da177e4 | 90 | |
e0204409 | 91 | #ifdef CONFIG_HOTPLUG_CPU |
d1584504 | 92 | void arch_cpu_idle_dead(void) |
87fa05ae SR |
93 | { |
94 | sched_preempt_enable_no_resched(); | |
95 | cpu_play_dead(); | |
1da177e4 | 96 | } |
87fa05ae | 97 | #endif |
1da177e4 | 98 | |
959a85ad | 99 | #ifdef CONFIG_COMPAT |
1da177e4 LT |
100 | static void show_regwindow32(struct pt_regs *regs) |
101 | { | |
102 | struct reg_window32 __user *rw; | |
103 | struct reg_window32 r_w; | |
104 | mm_segment_t old_fs; | |
105 | ||
106 | __asm__ __volatile__ ("flushw"); | |
9ef595d8 | 107 | rw = compat_ptr((unsigned int)regs->u_regs[14]); |
1da177e4 LT |
108 | old_fs = get_fs(); |
109 | set_fs (USER_DS); | |
110 | if (copy_from_user (&r_w, rw, sizeof(r_w))) { | |
111 | set_fs (old_fs); | |
112 | return; | |
113 | } | |
114 | ||
115 | set_fs (old_fs); | |
116 | printk("l0: %08x l1: %08x l2: %08x l3: %08x " | |
117 | "l4: %08x l5: %08x l6: %08x l7: %08x\n", | |
118 | r_w.locals[0], r_w.locals[1], r_w.locals[2], r_w.locals[3], | |
119 | r_w.locals[4], r_w.locals[5], r_w.locals[6], r_w.locals[7]); | |
120 | printk("i0: %08x i1: %08x i2: %08x i3: %08x " | |
121 | "i4: %08x i5: %08x i6: %08x i7: %08x\n", | |
122 | r_w.ins[0], r_w.ins[1], r_w.ins[2], r_w.ins[3], | |
123 | r_w.ins[4], r_w.ins[5], r_w.ins[6], r_w.ins[7]); | |
124 | } | |
959a85ad DM |
125 | #else |
126 | #define show_regwindow32(regs) do { } while (0) | |
127 | #endif | |
1da177e4 LT |
128 | |
129 | static void show_regwindow(struct pt_regs *regs) | |
130 | { | |
131 | struct reg_window __user *rw; | |
132 | struct reg_window *rwk; | |
133 | struct reg_window r_w; | |
134 | mm_segment_t old_fs; | |
135 | ||
136 | if ((regs->tstate & TSTATE_PRIV) || !(test_thread_flag(TIF_32BIT))) { | |
137 | __asm__ __volatile__ ("flushw"); | |
138 | rw = (struct reg_window __user *) | |
139 | (regs->u_regs[14] + STACK_BIAS); | |
140 | rwk = (struct reg_window *) | |
141 | (regs->u_regs[14] + STACK_BIAS); | |
142 | if (!(regs->tstate & TSTATE_PRIV)) { | |
143 | old_fs = get_fs(); | |
144 | set_fs (USER_DS); | |
145 | if (copy_from_user (&r_w, rw, sizeof(r_w))) { | |
146 | set_fs (old_fs); | |
147 | return; | |
148 | } | |
149 | rwk = &r_w; | |
150 | set_fs (old_fs); | |
151 | } | |
152 | } else { | |
153 | show_regwindow32(regs); | |
154 | return; | |
155 | } | |
156 | printk("l0: %016lx l1: %016lx l2: %016lx l3: %016lx\n", | |
157 | rwk->locals[0], rwk->locals[1], rwk->locals[2], rwk->locals[3]); | |
158 | printk("l4: %016lx l5: %016lx l6: %016lx l7: %016lx\n", | |
159 | rwk->locals[4], rwk->locals[5], rwk->locals[6], rwk->locals[7]); | |
160 | printk("i0: %016lx i1: %016lx i2: %016lx i3: %016lx\n", | |
161 | rwk->ins[0], rwk->ins[1], rwk->ins[2], rwk->ins[3]); | |
162 | printk("i4: %016lx i5: %016lx i6: %016lx i7: %016lx\n", | |
163 | rwk->ins[4], rwk->ins[5], rwk->ins[6], rwk->ins[7]); | |
164 | if (regs->tstate & TSTATE_PRIV) | |
4fe3ebec | 165 | printk("I7: <%pS>\n", (void *) rwk->ins[7]); |
1da177e4 LT |
166 | } |
167 | ||
dbf3e950 | 168 | void show_regs(struct pt_regs *regs) |
1da177e4 | 169 | { |
a43cb95d TH |
170 | show_regs_print_info(KERN_DEFAULT); |
171 | ||
1da177e4 LT |
172 | printk("TSTATE: %016lx TPC: %016lx TNPC: %016lx Y: %08x %s\n", regs->tstate, |
173 | regs->tpc, regs->tnpc, regs->y, print_tainted()); | |
4fe3ebec | 174 | printk("TPC: <%pS>\n", (void *) regs->tpc); |
1da177e4 LT |
175 | printk("g0: %016lx g1: %016lx g2: %016lx g3: %016lx\n", |
176 | regs->u_regs[0], regs->u_regs[1], regs->u_regs[2], | |
177 | regs->u_regs[3]); | |
178 | printk("g4: %016lx g5: %016lx g6: %016lx g7: %016lx\n", | |
179 | regs->u_regs[4], regs->u_regs[5], regs->u_regs[6], | |
180 | regs->u_regs[7]); | |
181 | printk("o0: %016lx o1: %016lx o2: %016lx o3: %016lx\n", | |
182 | regs->u_regs[8], regs->u_regs[9], regs->u_regs[10], | |
183 | regs->u_regs[11]); | |
184 | printk("o4: %016lx o5: %016lx sp: %016lx ret_pc: %016lx\n", | |
185 | regs->u_regs[12], regs->u_regs[13], regs->u_regs[14], | |
186 | regs->u_regs[15]); | |
4fe3ebec | 187 | printk("RPC: <%pS>\n", (void *) regs->u_regs[15]); |
1da177e4 | 188 | show_regwindow(regs); |
87e8f0e3 | 189 | show_stack(current, (unsigned long *) regs->u_regs[UREG_FP]); |
1da177e4 LT |
190 | } |
191 | ||
916ca14a DM |
192 | union global_cpu_snapshot global_cpu_snapshot[NR_CPUS]; |
193 | static DEFINE_SPINLOCK(global_cpu_snapshot_lock); | |
93dae5b7 DM |
194 | |
195 | static void __global_reg_self(struct thread_info *tp, struct pt_regs *regs, | |
196 | int this_cpu) | |
197 | { | |
916ca14a DM |
198 | struct global_reg_snapshot *rp; |
199 | ||
93dae5b7 DM |
200 | flushw_all(); |
201 | ||
916ca14a DM |
202 | rp = &global_cpu_snapshot[this_cpu].reg; |
203 | ||
204 | rp->tstate = regs->tstate; | |
205 | rp->tpc = regs->tpc; | |
206 | rp->tnpc = regs->tnpc; | |
207 | rp->o7 = regs->u_regs[UREG_I7]; | |
93dae5b7 DM |
208 | |
209 | if (regs->tstate & TSTATE_PRIV) { | |
210 | struct reg_window *rw; | |
211 | ||
212 | rw = (struct reg_window *) | |
213 | (regs->u_regs[UREG_FP] + STACK_BIAS); | |
4f70f7a9 | 214 | if (kstack_valid(tp, (unsigned long) rw)) { |
916ca14a | 215 | rp->i7 = rw->ins[7]; |
5afe2738 DM |
216 | rw = (struct reg_window *) |
217 | (rw->ins[6] + STACK_BIAS); | |
4f70f7a9 | 218 | if (kstack_valid(tp, (unsigned long) rw)) |
916ca14a | 219 | rp->rpc = rw->ins[7]; |
5afe2738 DM |
220 | } |
221 | } else { | |
916ca14a DM |
222 | rp->i7 = 0; |
223 | rp->rpc = 0; | |
5afe2738 | 224 | } |
916ca14a | 225 | rp->thread = tp; |
93dae5b7 DM |
226 | } |
227 | ||
228 | /* In order to avoid hangs we do not try to synchronize with the | |
229 | * global register dump client cpus. The last store they make is to | |
230 | * the thread pointer, so do a short poll waiting for that to become | |
231 | * non-NULL. | |
232 | */ | |
233 | static void __global_reg_poll(struct global_reg_snapshot *gp) | |
234 | { | |
235 | int limit = 0; | |
236 | ||
237 | while (!gp->thread && ++limit < 100) { | |
238 | barrier(); | |
239 | udelay(1); | |
240 | } | |
241 | } | |
242 | ||
9a01c3ed | 243 | void arch_trigger_cpumask_backtrace(const cpumask_t *mask, bool exclude_self) |
93dae5b7 DM |
244 | { |
245 | struct thread_info *tp = current_thread_info(); | |
246 | struct pt_regs *regs = get_irq_regs(); | |
93dae5b7 DM |
247 | unsigned long flags; |
248 | int this_cpu, cpu; | |
249 | ||
250 | if (!regs) | |
251 | regs = tp->kregs; | |
252 | ||
916ca14a | 253 | spin_lock_irqsave(&global_cpu_snapshot_lock, flags); |
93dae5b7 | 254 | |
93dae5b7 DM |
255 | this_cpu = raw_smp_processor_id(); |
256 | ||
f3aca3d0 AT |
257 | memset(global_cpu_snapshot, 0, sizeof(global_cpu_snapshot)); |
258 | ||
9a01c3ed | 259 | if (cpumask_test_cpu(this_cpu, mask) && !exclude_self) |
f3aca3d0 | 260 | __global_reg_self(tp, regs, this_cpu); |
93dae5b7 DM |
261 | |
262 | smp_fetch_global_regs(); | |
263 | ||
9a01c3ed | 264 | for_each_cpu(cpu, mask) { |
f3aca3d0 AT |
265 | struct global_reg_snapshot *gp; |
266 | ||
9a01c3ed | 267 | if (exclude_self && cpu == this_cpu) |
f3aca3d0 AT |
268 | continue; |
269 | ||
270 | gp = &global_cpu_snapshot[cpu].reg; | |
93dae5b7 DM |
271 | |
272 | __global_reg_poll(gp); | |
273 | ||
274 | tp = gp->thread; | |
275 | printk("%c CPU[%3d]: TSTATE[%016lx] TPC[%016lx] TNPC[%016lx] TASK[%s:%d]\n", | |
276 | (cpu == this_cpu ? '*' : ' '), cpu, | |
277 | gp->tstate, gp->tpc, gp->tnpc, | |
278 | ((tp && tp->task) ? tp->task->comm : "NULL"), | |
279 | ((tp && tp->task) ? tp->task->pid : -1)); | |
4fe3ebec | 280 | |
93dae5b7 | 281 | if (gp->tstate & TSTATE_PRIV) { |
5afe2738 | 282 | printk(" TPC[%pS] O7[%pS] I7[%pS] RPC[%pS]\n", |
4fe3ebec DM |
283 | (void *) gp->tpc, |
284 | (void *) gp->o7, | |
5afe2738 DM |
285 | (void *) gp->i7, |
286 | (void *) gp->rpc); | |
4fe3ebec | 287 | } else { |
5afe2738 DM |
288 | printk(" TPC[%lx] O7[%lx] I7[%lx] RPC[%lx]\n", |
289 | gp->tpc, gp->o7, gp->i7, gp->rpc); | |
93dae5b7 | 290 | } |
31aaa98c DA |
291 | |
292 | touch_nmi_watchdog(); | |
93dae5b7 DM |
293 | } |
294 | ||
916ca14a | 295 | memset(global_cpu_snapshot, 0, sizeof(global_cpu_snapshot)); |
93dae5b7 | 296 | |
916ca14a | 297 | spin_unlock_irqrestore(&global_cpu_snapshot_lock, flags); |
93dae5b7 DM |
298 | } |
299 | ||
09ee167c DM |
300 | #ifdef CONFIG_MAGIC_SYSRQ |
301 | ||
1495cc9d | 302 | static void sysrq_handle_globreg(int key) |
09ee167c | 303 | { |
9a01c3ed | 304 | trigger_all_cpu_backtrace(); |
09ee167c DM |
305 | } |
306 | ||
93dae5b7 DM |
307 | static struct sysrq_key_op sparc_globalreg_op = { |
308 | .handler = sysrq_handle_globreg, | |
5452c7f8 | 309 | .help_msg = "global-regs(y)", |
93dae5b7 DM |
310 | .action_msg = "Show Global CPU Regs", |
311 | }; | |
312 | ||
916ca14a | 313 | static void __global_pmu_self(int this_cpu) |
93dae5b7 | 314 | { |
916ca14a DM |
315 | struct global_pmu_snapshot *pp; |
316 | int i, num; | |
317 | ||
8bccf5b3 DM |
318 | if (!pcr_ops) |
319 | return; | |
320 | ||
916ca14a DM |
321 | pp = &global_cpu_snapshot[this_cpu].pmu; |
322 | ||
323 | num = 1; | |
324 | if (tlb_type == hypervisor && | |
325 | sun4v_chip_type >= SUN4V_CHIP_NIAGARA4) | |
326 | num = 4; | |
327 | ||
328 | for (i = 0; i < num; i++) { | |
329 | pp->pcr[i] = pcr_ops->read_pcr(i); | |
330 | pp->pic[i] = pcr_ops->read_pic(i); | |
331 | } | |
93dae5b7 DM |
332 | } |
333 | ||
916ca14a DM |
334 | static void __global_pmu_poll(struct global_pmu_snapshot *pp) |
335 | { | |
336 | int limit = 0; | |
337 | ||
338 | while (!pp->pcr[0] && ++limit < 100) { | |
339 | barrier(); | |
340 | udelay(1); | |
341 | } | |
342 | } | |
343 | ||
344 | static void pmu_snapshot_all_cpus(void) | |
93dae5b7 | 345 | { |
916ca14a DM |
346 | unsigned long flags; |
347 | int this_cpu, cpu; | |
348 | ||
349 | spin_lock_irqsave(&global_cpu_snapshot_lock, flags); | |
350 | ||
351 | memset(global_cpu_snapshot, 0, sizeof(global_cpu_snapshot)); | |
352 | ||
353 | this_cpu = raw_smp_processor_id(); | |
354 | ||
355 | __global_pmu_self(this_cpu); | |
356 | ||
357 | smp_fetch_global_pmu(); | |
358 | ||
359 | for_each_online_cpu(cpu) { | |
360 | struct global_pmu_snapshot *pp = &global_cpu_snapshot[cpu].pmu; | |
361 | ||
362 | __global_pmu_poll(pp); | |
363 | ||
364 | printk("%c CPU[%3d]: PCR[%08lx:%08lx:%08lx:%08lx] PIC[%08lx:%08lx:%08lx:%08lx]\n", | |
365 | (cpu == this_cpu ? '*' : ' '), cpu, | |
366 | pp->pcr[0], pp->pcr[1], pp->pcr[2], pp->pcr[3], | |
367 | pp->pic[0], pp->pic[1], pp->pic[2], pp->pic[3]); | |
31aaa98c DA |
368 | |
369 | touch_nmi_watchdog(); | |
916ca14a DM |
370 | } |
371 | ||
372 | memset(global_cpu_snapshot, 0, sizeof(global_cpu_snapshot)); | |
373 | ||
374 | spin_unlock_irqrestore(&global_cpu_snapshot_lock, flags); | |
375 | } | |
376 | ||
377 | static void sysrq_handle_globpmu(int key) | |
378 | { | |
379 | pmu_snapshot_all_cpus(); | |
380 | } | |
381 | ||
382 | static struct sysrq_key_op sparc_globalpmu_op = { | |
383 | .handler = sysrq_handle_globpmu, | |
5452c7f8 | 384 | .help_msg = "global-pmu(x)", |
916ca14a DM |
385 | .action_msg = "Show Global PMU Regs", |
386 | }; | |
387 | ||
388 | static int __init sparc_sysrq_init(void) | |
389 | { | |
390 | int ret = register_sysrq_key('y', &sparc_globalreg_op); | |
391 | ||
392 | if (!ret) | |
393 | ret = register_sysrq_key('x', &sparc_globalpmu_op); | |
394 | return ret; | |
93dae5b7 DM |
395 | } |
396 | ||
916ca14a | 397 | core_initcall(sparc_sysrq_init); |
93dae5b7 DM |
398 | |
399 | #endif | |
400 | ||
1da177e4 LT |
401 | unsigned long thread_saved_pc(struct task_struct *tsk) |
402 | { | |
f3169641 | 403 | struct thread_info *ti = task_thread_info(tsk); |
1da177e4 LT |
404 | unsigned long ret = 0xdeadbeefUL; |
405 | ||
406 | if (ti && ti->ksp) { | |
407 | unsigned long *sp; | |
408 | sp = (unsigned long *)(ti->ksp + STACK_BIAS); | |
409 | if (((unsigned long)sp & (sizeof(long) - 1)) == 0UL && | |
410 | sp[14]) { | |
411 | unsigned long *fp; | |
412 | fp = (unsigned long *)(sp[14] + STACK_BIAS); | |
413 | if (((unsigned long)fp & (sizeof(long) - 1)) == 0UL) | |
414 | ret = fp[15]; | |
415 | } | |
416 | } | |
417 | return ret; | |
418 | } | |
419 | ||
420 | /* Free current thread data structures etc.. */ | |
e6464694 | 421 | void exit_thread(struct task_struct *tsk) |
1da177e4 | 422 | { |
e6464694 | 423 | struct thread_info *t = task_thread_info(tsk); |
1da177e4 LT |
424 | |
425 | if (t->utraps) { | |
426 | if (t->utraps[0] < 2) | |
427 | kfree (t->utraps); | |
428 | else | |
429 | t->utraps[0]--; | |
430 | } | |
1da177e4 LT |
431 | } |
432 | ||
433 | void flush_thread(void) | |
434 | { | |
435 | struct thread_info *t = current_thread_info(); | |
74bf4312 | 436 | struct mm_struct *mm; |
1da177e4 | 437 | |
74bf4312 DM |
438 | mm = t->task->mm; |
439 | if (mm) | |
98c5584c | 440 | tsb_context_switch(mm); |
1da177e4 | 441 | |
1da177e4 LT |
442 | set_thread_wsaved(0); |
443 | ||
1da177e4 LT |
444 | /* Clear FPU register state. */ |
445 | t->fpsaved[0] = 0; | |
1da177e4 LT |
446 | } |
447 | ||
448 | /* It's a bit more tricky when 64-bit tasks are involved... */ | |
449 | static unsigned long clone_stackframe(unsigned long csp, unsigned long psp) | |
450 | { | |
517ffce4 | 451 | bool stack_64bit = test_thread_64bit_stack(psp); |
1da177e4 LT |
452 | unsigned long fp, distance, rval; |
453 | ||
517ffce4 | 454 | if (stack_64bit) { |
1da177e4 LT |
455 | csp += STACK_BIAS; |
456 | psp += STACK_BIAS; | |
457 | __get_user(fp, &(((struct reg_window __user *)psp)->ins[6])); | |
458 | fp += STACK_BIAS; | |
517ffce4 DM |
459 | if (test_thread_flag(TIF_32BIT)) |
460 | fp &= 0xffffffff; | |
1da177e4 LT |
461 | } else |
462 | __get_user(fp, &(((struct reg_window32 __user *)psp)->ins[6])); | |
463 | ||
f036d9f3 DM |
464 | /* Now align the stack as this is mandatory in the Sparc ABI |
465 | * due to how register windows work. This hides the | |
466 | * restriction from thread libraries etc. | |
1da177e4 | 467 | */ |
f036d9f3 | 468 | csp &= ~15UL; |
1da177e4 LT |
469 | |
470 | distance = fp - psp; | |
471 | rval = (csp - distance); | |
472 | if (copy_in_user((void __user *) rval, (void __user *) psp, distance)) | |
473 | rval = 0; | |
517ffce4 | 474 | else if (!stack_64bit) { |
1da177e4 LT |
475 | if (put_user(((u32)csp), |
476 | &(((struct reg_window32 __user *)rval)->ins[6]))) | |
477 | rval = 0; | |
478 | } else { | |
479 | if (put_user(((u64)csp - STACK_BIAS), | |
480 | &(((struct reg_window __user *)rval)->ins[6]))) | |
481 | rval = 0; | |
482 | else | |
483 | rval = rval - STACK_BIAS; | |
484 | } | |
485 | ||
486 | return rval; | |
487 | } | |
488 | ||
489 | /* Standard stuff. */ | |
490 | static inline void shift_window_buffer(int first_win, int last_win, | |
491 | struct thread_info *t) | |
492 | { | |
493 | int i; | |
494 | ||
495 | for (i = first_win; i < last_win; i++) { | |
496 | t->rwbuf_stkptrs[i] = t->rwbuf_stkptrs[i+1]; | |
497 | memcpy(&t->reg_window[i], &t->reg_window[i+1], | |
498 | sizeof(struct reg_window)); | |
499 | } | |
500 | } | |
501 | ||
502 | void synchronize_user_stack(void) | |
503 | { | |
504 | struct thread_info *t = current_thread_info(); | |
505 | unsigned long window; | |
506 | ||
507 | flush_user_windows(); | |
508 | if ((window = get_thread_wsaved()) != 0) { | |
1da177e4 LT |
509 | window -= 1; |
510 | do { | |
1da177e4 | 511 | struct reg_window *rwin = &t->reg_window[window]; |
517ffce4 DM |
512 | int winsize = sizeof(struct reg_window); |
513 | unsigned long sp; | |
514 | ||
515 | sp = t->rwbuf_stkptrs[window]; | |
516 | ||
517 | if (test_thread_64bit_stack(sp)) | |
518 | sp += STACK_BIAS; | |
519 | else | |
520 | winsize = sizeof(struct reg_window32); | |
1da177e4 LT |
521 | |
522 | if (!copy_to_user((char __user *)sp, rwin, winsize)) { | |
523 | shift_window_buffer(window, get_thread_wsaved() - 1, t); | |
524 | set_thread_wsaved(get_thread_wsaved() - 1); | |
525 | } | |
526 | } while (window--); | |
527 | } | |
528 | } | |
529 | ||
314ef685 DM |
530 | static void stack_unaligned(unsigned long sp) |
531 | { | |
532 | siginfo_t info; | |
533 | ||
534 | info.si_signo = SIGBUS; | |
535 | info.si_errno = 0; | |
536 | info.si_code = BUS_ADRALN; | |
537 | info.si_addr = (void __user *) sp; | |
538 | info.si_trapno = 0; | |
539 | force_sig_info(SIGBUS, &info, current); | |
540 | } | |
541 | ||
1da177e4 LT |
542 | void fault_in_user_windows(void) |
543 | { | |
544 | struct thread_info *t = current_thread_info(); | |
545 | unsigned long window; | |
1da177e4 LT |
546 | |
547 | flush_user_windows(); | |
548 | window = get_thread_wsaved(); | |
549 | ||
314ef685 | 550 | if (likely(window != 0)) { |
1da177e4 LT |
551 | window -= 1; |
552 | do { | |
1da177e4 | 553 | struct reg_window *rwin = &t->reg_window[window]; |
517ffce4 DM |
554 | int winsize = sizeof(struct reg_window); |
555 | unsigned long sp; | |
556 | ||
557 | sp = t->rwbuf_stkptrs[window]; | |
558 | ||
559 | if (test_thread_64bit_stack(sp)) | |
560 | sp += STACK_BIAS; | |
561 | else | |
562 | winsize = sizeof(struct reg_window32); | |
1da177e4 | 563 | |
314ef685 DM |
564 | if (unlikely(sp & 0x7UL)) |
565 | stack_unaligned(sp); | |
566 | ||
567 | if (unlikely(copy_to_user((char __user *)sp, | |
568 | rwin, winsize))) | |
1da177e4 LT |
569 | goto barf; |
570 | } while (window--); | |
571 | } | |
572 | set_thread_wsaved(0); | |
573 | return; | |
574 | ||
575 | barf: | |
576 | set_thread_wsaved(window + 1); | |
812cb83a | 577 | user_exit(); |
1da177e4 LT |
578 | do_exit(SIGILL); |
579 | } | |
580 | ||
581 | asmlinkage long sparc_do_fork(unsigned long clone_flags, | |
582 | unsigned long stack_start, | |
583 | struct pt_regs *regs, | |
584 | unsigned long stack_size) | |
585 | { | |
586 | int __user *parent_tid_ptr, *child_tid_ptr; | |
1e38c126 DM |
587 | unsigned long orig_i1 = regs->u_regs[UREG_I1]; |
588 | long ret; | |
1da177e4 LT |
589 | |
590 | #ifdef CONFIG_COMPAT | |
591 | if (test_thread_flag(TIF_32BIT)) { | |
592 | parent_tid_ptr = compat_ptr(regs->u_regs[UREG_I2]); | |
593 | child_tid_ptr = compat_ptr(regs->u_regs[UREG_I4]); | |
594 | } else | |
595 | #endif | |
596 | { | |
597 | parent_tid_ptr = (int __user *) regs->u_regs[UREG_I2]; | |
598 | child_tid_ptr = (int __user *) regs->u_regs[UREG_I4]; | |
599 | } | |
600 | ||
e80d6661 | 601 | ret = do_fork(clone_flags, stack_start, stack_size, |
1e38c126 DM |
602 | parent_tid_ptr, child_tid_ptr); |
603 | ||
604 | /* If we get an error and potentially restart the system | |
605 | * call, we're screwed because copy_thread() clobbered | |
606 | * the parent's %o1. So detect that case and restore it | |
607 | * here. | |
608 | */ | |
609 | if ((unsigned long)ret >= -ERESTART_RESTARTBLOCK) | |
610 | regs->u_regs[UREG_I1] = orig_i1; | |
611 | ||
612 | return ret; | |
1da177e4 LT |
613 | } |
614 | ||
615 | /* Copy a Sparc thread. The fork() return value conventions | |
616 | * under SunOS are nothing short of bletcherous: | |
617 | * Parent --> %o0 == childs pid, %o1 == 0 | |
618 | * Child --> %o0 == parents pid, %o1 == 1 | |
619 | */ | |
6f2c55b8 | 620 | int copy_thread(unsigned long clone_flags, unsigned long sp, |
afa86fc4 | 621 | unsigned long arg, struct task_struct *p) |
1da177e4 | 622 | { |
ee3eea16 | 623 | struct thread_info *t = task_thread_info(p); |
afa86fc4 | 624 | struct pt_regs *regs = current_pt_regs(); |
a051bc5b DM |
625 | struct sparc_stackf *parent_sf; |
626 | unsigned long child_stack_sz; | |
1da177e4 | 627 | char *child_trap_frame; |
1da177e4 | 628 | |
a051bc5b | 629 | /* Calculate offset to stack_frame & pt_regs */ |
1918c7f5 | 630 | child_stack_sz = (STACKFRAME_SZ + TRACEREG_SZ); |
a051bc5b DM |
631 | child_trap_frame = (task_stack_page(p) + |
632 | (THREAD_SIZE - child_stack_sz)); | |
a051bc5b | 633 | |
db7d9a4e | 634 | t->new_child = 1; |
1da177e4 | 635 | t->ksp = ((unsigned long) child_trap_frame) - STACK_BIAS; |
a051bc5b DM |
636 | t->kregs = (struct pt_regs *) (child_trap_frame + |
637 | sizeof(struct sparc_stackf)); | |
1da177e4 LT |
638 | t->fpsaved[0] = 0; |
639 | ||
1918c7f5 AV |
640 | if (unlikely(p->flags & PF_KTHREAD)) { |
641 | memset(child_trap_frame, 0, child_stack_sz); | |
642 | __thread_flag_byte_ptr(t)[TI_FLAG_BYTE_CWP] = | |
643 | (current_pt_regs()->tstate + 1) & TSTATE_CWP; | |
dff933da | 644 | t->current_ds = ASI_P; |
1918c7f5 AV |
645 | t->kregs->u_regs[UREG_G1] = sp; /* function */ |
646 | t->kregs->u_regs[UREG_G2] = arg; | |
647 | return 0; | |
1da177e4 LT |
648 | } |
649 | ||
1918c7f5 AV |
650 | parent_sf = ((struct sparc_stackf *) regs) - 1; |
651 | memcpy(child_trap_frame, parent_sf, child_stack_sz); | |
652 | if (t->flags & _TIF_32BIT) { | |
653 | sp &= 0x00000000ffffffffUL; | |
654 | regs->u_regs[UREG_FP] &= 0x00000000ffffffffUL; | |
655 | } | |
656 | t->kregs->u_regs[UREG_FP] = sp; | |
657 | __thread_flag_byte_ptr(t)[TI_FLAG_BYTE_CWP] = | |
658 | (regs->tstate + 1) & TSTATE_CWP; | |
659 | t->current_ds = ASI_AIUS; | |
660 | if (sp != regs->u_regs[UREG_FP]) { | |
661 | unsigned long csp; | |
662 | ||
663 | csp = clone_stackframe(sp, regs->u_regs[UREG_FP]); | |
664 | if (!csp) | |
665 | return -EFAULT; | |
666 | t->kregs->u_regs[UREG_FP] = csp; | |
667 | } | |
668 | if (t->utraps) | |
669 | t->utraps[0]++; | |
670 | ||
1da177e4 LT |
671 | /* Set the return value for the child. */ |
672 | t->kregs->u_regs[UREG_I0] = current->pid; | |
673 | t->kregs->u_regs[UREG_I1] = 1; | |
674 | ||
675 | /* Set the second return value for the parent. */ | |
676 | regs->u_regs[UREG_I1] = 0; | |
677 | ||
678 | if (clone_flags & CLONE_SETTLS) | |
679 | t->kregs->u_regs[UREG_G7] = regs->u_regs[UREG_I3]; | |
680 | ||
681 | return 0; | |
682 | } | |
683 | ||
1da177e4 LT |
684 | typedef struct { |
685 | union { | |
686 | unsigned int pr_regs[32]; | |
687 | unsigned long pr_dregs[16]; | |
688 | } pr_fr; | |
689 | unsigned int __unused; | |
690 | unsigned int pr_fsr; | |
691 | unsigned char pr_qcnt; | |
692 | unsigned char pr_q_entrysize; | |
693 | unsigned char pr_en; | |
694 | unsigned int pr_q[64]; | |
695 | } elf_fpregset_t32; | |
696 | ||
697 | /* | |
698 | * fill in the fpu structure for a core dump. | |
699 | */ | |
700 | int dump_fpu (struct pt_regs * regs, elf_fpregset_t * fpregs) | |
701 | { | |
702 | unsigned long *kfpregs = current_thread_info()->fpregs; | |
703 | unsigned long fprs = current_thread_info()->fpsaved[0]; | |
704 | ||
705 | if (test_thread_flag(TIF_32BIT)) { | |
706 | elf_fpregset_t32 *fpregs32 = (elf_fpregset_t32 *)fpregs; | |
707 | ||
708 | if (fprs & FPRS_DL) | |
709 | memcpy(&fpregs32->pr_fr.pr_regs[0], kfpregs, | |
710 | sizeof(unsigned int) * 32); | |
711 | else | |
712 | memset(&fpregs32->pr_fr.pr_regs[0], 0, | |
713 | sizeof(unsigned int) * 32); | |
714 | fpregs32->pr_qcnt = 0; | |
715 | fpregs32->pr_q_entrysize = 8; | |
716 | memset(&fpregs32->pr_q[0], 0, | |
717 | (sizeof(unsigned int) * 64)); | |
718 | if (fprs & FPRS_FEF) { | |
719 | fpregs32->pr_fsr = (unsigned int) current_thread_info()->xfsr[0]; | |
720 | fpregs32->pr_en = 1; | |
721 | } else { | |
722 | fpregs32->pr_fsr = 0; | |
723 | fpregs32->pr_en = 0; | |
724 | } | |
725 | } else { | |
726 | if(fprs & FPRS_DL) | |
727 | memcpy(&fpregs->pr_regs[0], kfpregs, | |
728 | sizeof(unsigned int) * 32); | |
729 | else | |
730 | memset(&fpregs->pr_regs[0], 0, | |
731 | sizeof(unsigned int) * 32); | |
732 | if(fprs & FPRS_DU) | |
733 | memcpy(&fpregs->pr_regs[16], kfpregs+16, | |
734 | sizeof(unsigned int) * 32); | |
735 | else | |
736 | memset(&fpregs->pr_regs[16], 0, | |
737 | sizeof(unsigned int) * 32); | |
738 | if(fprs & FPRS_FEF) { | |
739 | fpregs->pr_fsr = current_thread_info()->xfsr[0]; | |
740 | fpregs->pr_gsr = current_thread_info()->gsr[0]; | |
741 | } else { | |
742 | fpregs->pr_fsr = fpregs->pr_gsr = 0; | |
743 | } | |
744 | fpregs->pr_fprs = fprs; | |
745 | } | |
746 | return 1; | |
747 | } | |
917c3660 | 748 | EXPORT_SYMBOL(dump_fpu); |
1da177e4 | 749 | |
1da177e4 LT |
750 | unsigned long get_wchan(struct task_struct *task) |
751 | { | |
752 | unsigned long pc, fp, bias = 0; | |
4f70f7a9 | 753 | struct thread_info *tp; |
1da177e4 LT |
754 | struct reg_window *rw; |
755 | unsigned long ret = 0; | |
756 | int count = 0; | |
757 | ||
758 | if (!task || task == current || | |
759 | task->state == TASK_RUNNING) | |
760 | goto out; | |
761 | ||
4f70f7a9 | 762 | tp = task_thread_info(task); |
1da177e4 | 763 | bias = STACK_BIAS; |
f3169641 | 764 | fp = task_thread_info(task)->ksp + bias; |
1da177e4 LT |
765 | |
766 | do { | |
4f70f7a9 | 767 | if (!kstack_valid(tp, fp)) |
1da177e4 LT |
768 | break; |
769 | rw = (struct reg_window *) fp; | |
770 | pc = rw->ins[7]; | |
771 | if (!in_sched_functions(pc)) { | |
772 | ret = pc; | |
773 | goto out; | |
774 | } | |
775 | fp = rw->ins[6] + bias; | |
776 | } while (++count < 16); | |
777 | ||
778 | out: | |
779 | return ret; | |
780 | } |