Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / arch / sparc64 / kernel / unaligned.c
blob7ab4a990dc76d43acc3298aba4d25ed38946299e
1 /* $Id: unaligned.c,v 1.24 2002/02/09 19:49:31 davem Exp $
2 * unaligned.c: Unaligned load/store trap handling with special
3 * cases for the kernel to do them more quickly.
5 * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
6 * Copyright (C) 1996,1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
7 */
10 <<<<<<< HEAD:arch/sparc64/kernel/unaligned.c
11 =======
12 #include <linux/jiffies.h>
13 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/sparc64/kernel/unaligned.c
14 #include <linux/kernel.h>
15 #include <linux/sched.h>
16 #include <linux/mm.h>
17 #include <linux/module.h>
18 #include <asm/asi.h>
19 #include <asm/ptrace.h>
20 #include <asm/pstate.h>
21 #include <asm/processor.h>
22 #include <asm/system.h>
23 #include <asm/uaccess.h>
24 #include <linux/smp.h>
25 #include <linux/bitops.h>
26 #include <linux/kallsyms.h>
27 #include <asm/fpumacro.h>
29 /* #define DEBUG_MNA */
31 enum direction {
32 load, /* ld, ldd, ldh, ldsh */
33 store, /* st, std, sth, stsh */
34 both, /* Swap, ldstub, cas, ... */
35 fpld,
36 fpst,
37 invalid,
40 #ifdef DEBUG_MNA
41 static char *dirstrings[] = {
42 "load", "store", "both", "fpload", "fpstore", "invalid"
44 #endif
46 static inline enum direction decode_direction(unsigned int insn)
48 unsigned long tmp = (insn >> 21) & 1;
50 if (!tmp)
51 return load;
52 else {
53 switch ((insn>>19)&0xf) {
54 case 15: /* swap* */
55 return both;
56 default:
57 return store;
62 /* 16 = double-word, 8 = extra-word, 4 = word, 2 = half-word */
63 static inline int decode_access_size(unsigned int insn)
65 unsigned int tmp;
67 tmp = ((insn >> 19) & 0xf);
68 if (tmp == 11 || tmp == 14) /* ldx/stx */
69 return 8;
70 tmp &= 3;
71 if (!tmp)
72 return 4;
73 else if (tmp == 3)
74 return 16; /* ldd/std - Although it is actually 8 */
75 else if (tmp == 2)
76 return 2;
77 else {
78 printk("Impossible unaligned trap. insn=%08x\n", insn);
79 die_if_kernel("Byte sized unaligned access?!?!", current_thread_info()->kregs);
81 /* GCC should never warn that control reaches the end
82 * of this function without returning a value because
83 * die_if_kernel() is marked with attribute 'noreturn'.
84 * Alas, some versions do...
87 return 0;
91 static inline int decode_asi(unsigned int insn, struct pt_regs *regs)
93 if (insn & 0x800000) {
94 if (insn & 0x2000)
95 return (unsigned char)(regs->tstate >> 24); /* %asi */
96 else
97 return (unsigned char)(insn >> 5); /* imm_asi */
98 } else
99 return ASI_P;
102 /* 0x400000 = signed, 0 = unsigned */
103 static inline int decode_signedness(unsigned int insn)
105 return (insn & 0x400000);
108 static inline void maybe_flush_windows(unsigned int rs1, unsigned int rs2,
109 unsigned int rd, int from_kernel)
111 if (rs2 >= 16 || rs1 >= 16 || rd >= 16) {
112 if (from_kernel != 0)
113 __asm__ __volatile__("flushw");
114 else
115 flushw_user();
119 static inline long sign_extend_imm13(long imm)
121 return imm << 51 >> 51;
124 static unsigned long fetch_reg(unsigned int reg, struct pt_regs *regs)
126 unsigned long value;
128 if (reg < 16)
129 return (!reg ? 0 : regs->u_regs[reg]);
130 if (regs->tstate & TSTATE_PRIV) {
131 struct reg_window *win;
132 win = (struct reg_window *)(regs->u_regs[UREG_FP] + STACK_BIAS);
133 value = win->locals[reg - 16];
134 } else if (test_thread_flag(TIF_32BIT)) {
135 struct reg_window32 __user *win32;
136 win32 = (struct reg_window32 __user *)((unsigned long)((u32)regs->u_regs[UREG_FP]));
137 get_user(value, &win32->locals[reg - 16]);
138 } else {
139 struct reg_window __user *win;
140 win = (struct reg_window __user *)(regs->u_regs[UREG_FP] + STACK_BIAS);
141 get_user(value, &win->locals[reg - 16]);
143 return value;
146 static unsigned long *fetch_reg_addr(unsigned int reg, struct pt_regs *regs)
148 if (reg < 16)
149 return &regs->u_regs[reg];
150 if (regs->tstate & TSTATE_PRIV) {
151 struct reg_window *win;
152 win = (struct reg_window *)(regs->u_regs[UREG_FP] + STACK_BIAS);
153 return &win->locals[reg - 16];
154 } else if (test_thread_flag(TIF_32BIT)) {
155 struct reg_window32 *win32;
156 win32 = (struct reg_window32 *)((unsigned long)((u32)regs->u_regs[UREG_FP]));
157 return (unsigned long *)&win32->locals[reg - 16];
158 } else {
159 struct reg_window *win;
160 win = (struct reg_window *)(regs->u_regs[UREG_FP] + STACK_BIAS);
161 return &win->locals[reg - 16];
165 unsigned long compute_effective_address(struct pt_regs *regs,
166 unsigned int insn, unsigned int rd)
168 unsigned int rs1 = (insn >> 14) & 0x1f;
169 unsigned int rs2 = insn & 0x1f;
170 int from_kernel = (regs->tstate & TSTATE_PRIV) != 0;
172 if (insn & 0x2000) {
173 maybe_flush_windows(rs1, 0, rd, from_kernel);
174 return (fetch_reg(rs1, regs) + sign_extend_imm13(insn));
175 } else {
176 maybe_flush_windows(rs1, rs2, rd, from_kernel);
177 return (fetch_reg(rs1, regs) + fetch_reg(rs2, regs));
181 /* This is just to make gcc think die_if_kernel does return... */
182 static void __used unaligned_panic(char *str, struct pt_regs *regs)
184 die_if_kernel(str, regs);
187 extern int do_int_load(unsigned long *dest_reg, int size,
188 unsigned long *saddr, int is_signed, int asi);
190 extern int __do_int_store(unsigned long *dst_addr, int size,
191 unsigned long src_val, int asi);
193 static inline int do_int_store(int reg_num, int size, unsigned long *dst_addr,
194 struct pt_regs *regs, int asi, int orig_asi)
196 unsigned long zero = 0;
197 unsigned long *src_val_p = &zero;
198 unsigned long src_val;
200 if (size == 16) {
201 size = 8;
202 zero = (((long)(reg_num ?
203 (unsigned)fetch_reg(reg_num, regs) : 0)) << 32) |
204 (unsigned)fetch_reg(reg_num + 1, regs);
205 } else if (reg_num) {
206 src_val_p = fetch_reg_addr(reg_num, regs);
208 src_val = *src_val_p;
209 if (unlikely(asi != orig_asi)) {
210 switch (size) {
211 case 2:
212 src_val = swab16(src_val);
213 break;
214 case 4:
215 src_val = swab32(src_val);
216 break;
217 case 8:
218 src_val = swab64(src_val);
219 break;
220 case 16:
221 default:
222 BUG();
223 break;
226 return __do_int_store(dst_addr, size, src_val, asi);
229 static inline void advance(struct pt_regs *regs)
231 regs->tpc = regs->tnpc;
232 regs->tnpc += 4;
233 if (test_thread_flag(TIF_32BIT)) {
234 regs->tpc &= 0xffffffff;
235 regs->tnpc &= 0xffffffff;
239 static inline int floating_point_load_or_store_p(unsigned int insn)
241 return (insn >> 24) & 1;
244 static inline int ok_for_kernel(unsigned int insn)
246 return !floating_point_load_or_store_p(insn);
249 static void kernel_mna_trap_fault(int fixup_tstate_asi)
251 struct pt_regs *regs = current_thread_info()->kern_una_regs;
252 unsigned int insn = current_thread_info()->kern_una_insn;
253 const struct exception_table_entry *entry;
255 entry = search_exception_tables(regs->tpc);
256 if (!entry) {
257 unsigned long address;
259 address = compute_effective_address(regs, insn,
260 ((insn >> 25) & 0x1f));
261 if (address < PAGE_SIZE) {
262 printk(KERN_ALERT "Unable to handle kernel NULL "
263 "pointer dereference in mna handler");
264 } else
265 printk(KERN_ALERT "Unable to handle kernel paging "
266 "request in mna handler");
267 printk(KERN_ALERT " at virtual address %016lx\n",address);
268 printk(KERN_ALERT "current->{active_,}mm->context = %016lx\n",
269 (current->mm ? CTX_HWBITS(current->mm->context) :
270 CTX_HWBITS(current->active_mm->context)));
271 printk(KERN_ALERT "current->{active_,}mm->pgd = %016lx\n",
272 (current->mm ? (unsigned long) current->mm->pgd :
273 (unsigned long) current->active_mm->pgd));
274 die_if_kernel("Oops", regs);
275 /* Not reached */
277 regs->tpc = entry->fixup;
278 regs->tnpc = regs->tpc + 4;
280 if (fixup_tstate_asi) {
281 regs->tstate &= ~TSTATE_ASI;
282 regs->tstate |= (ASI_AIUS << 24UL);
286 static void log_unaligned(struct pt_regs *regs)
288 static unsigned long count, last_time;
290 <<<<<<< HEAD:arch/sparc64/kernel/unaligned.c
291 if (jiffies - last_time > 5 * HZ)
292 =======
293 if (time_after(jiffies, last_time + 5 * HZ))
294 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/sparc64/kernel/unaligned.c
295 count = 0;
296 if (count < 5) {
297 last_time = jiffies;
298 count++;
299 printk("Kernel unaligned access at TPC[%lx] ", regs->tpc);
300 print_symbol("%s\n", regs->tpc);
304 asmlinkage void kernel_unaligned_trap(struct pt_regs *regs, unsigned int insn)
306 enum direction dir = decode_direction(insn);
307 int size = decode_access_size(insn);
308 int orig_asi, asi;
310 current_thread_info()->kern_una_regs = regs;
311 current_thread_info()->kern_una_insn = insn;
313 orig_asi = asi = decode_asi(insn, regs);
315 /* If this is a {get,put}_user() on an unaligned userspace pointer,
316 * just signal a fault and do not log the event.
318 if (asi == ASI_AIUS) {
319 kernel_mna_trap_fault(0);
320 return;
323 log_unaligned(regs);
325 if (!ok_for_kernel(insn) || dir == both) {
326 printk("Unsupported unaligned load/store trap for kernel "
327 "at <%016lx>.\n", regs->tpc);
328 unaligned_panic("Kernel does fpu/atomic "
329 "unaligned load/store.", regs);
331 kernel_mna_trap_fault(0);
332 } else {
333 unsigned long addr, *reg_addr;
334 int err;
336 addr = compute_effective_address(regs, insn,
337 ((insn >> 25) & 0x1f));
338 #ifdef DEBUG_MNA
339 printk("KMNA: pc=%016lx [dir=%s addr=%016lx size=%d] "
340 "retpc[%016lx]\n",
341 regs->tpc, dirstrings[dir], addr, size,
342 regs->u_regs[UREG_RETPC]);
343 #endif
344 switch (asi) {
345 case ASI_NL:
346 case ASI_AIUPL:
347 case ASI_AIUSL:
348 case ASI_PL:
349 case ASI_SL:
350 case ASI_PNFL:
351 case ASI_SNFL:
352 asi &= ~0x08;
353 break;
355 switch (dir) {
356 case load:
357 reg_addr = fetch_reg_addr(((insn>>25)&0x1f), regs);
358 err = do_int_load(reg_addr, size,
359 (unsigned long *) addr,
360 decode_signedness(insn), asi);
361 if (likely(!err) && unlikely(asi != orig_asi)) {
362 unsigned long val_in = *reg_addr;
363 switch (size) {
364 case 2:
365 val_in = swab16(val_in);
366 break;
367 case 4:
368 val_in = swab32(val_in);
369 break;
370 case 8:
371 val_in = swab64(val_in);
372 break;
373 case 16:
374 default:
375 BUG();
376 break;
378 *reg_addr = val_in;
380 break;
382 case store:
383 err = do_int_store(((insn>>25)&0x1f), size,
384 (unsigned long *) addr, regs,
385 asi, orig_asi);
386 break;
388 default:
389 panic("Impossible kernel unaligned trap.");
390 /* Not reached... */
392 if (unlikely(err))
393 kernel_mna_trap_fault(1);
394 else
395 advance(regs);
399 static char popc_helper[] = {
400 0, 1, 1, 2, 1, 2, 2, 3,
401 1, 2, 2, 3, 2, 3, 3, 4,
404 int handle_popc(u32 insn, struct pt_regs *regs)
406 u64 value;
407 int ret, i, rd = ((insn >> 25) & 0x1f);
408 int from_kernel = (regs->tstate & TSTATE_PRIV) != 0;
410 if (insn & 0x2000) {
411 maybe_flush_windows(0, 0, rd, from_kernel);
412 value = sign_extend_imm13(insn);
413 } else {
414 maybe_flush_windows(0, insn & 0x1f, rd, from_kernel);
415 value = fetch_reg(insn & 0x1f, regs);
417 for (ret = 0, i = 0; i < 16; i++) {
418 ret += popc_helper[value & 0xf];
419 value >>= 4;
421 if (rd < 16) {
422 if (rd)
423 regs->u_regs[rd] = ret;
424 } else {
425 if (test_thread_flag(TIF_32BIT)) {
426 struct reg_window32 __user *win32;
427 win32 = (struct reg_window32 __user *)((unsigned long)((u32)regs->u_regs[UREG_FP]));
428 put_user(ret, &win32->locals[rd - 16]);
429 } else {
430 struct reg_window __user *win;
431 win = (struct reg_window __user *)(regs->u_regs[UREG_FP] + STACK_BIAS);
432 put_user(ret, &win->locals[rd - 16]);
435 advance(regs);
436 return 1;
439 extern void do_fpother(struct pt_regs *regs);
440 extern void do_privact(struct pt_regs *regs);
441 extern void spitfire_data_access_exception(struct pt_regs *regs,
442 unsigned long sfsr,
443 unsigned long sfar);
444 extern void sun4v_data_access_exception(struct pt_regs *regs,
445 unsigned long addr,
446 unsigned long type_ctx);
448 int handle_ldf_stq(u32 insn, struct pt_regs *regs)
450 unsigned long addr = compute_effective_address(regs, insn, 0);
451 int freg = ((insn >> 25) & 0x1e) | ((insn >> 20) & 0x20);
452 struct fpustate *f = FPUSTATE;
453 int asi = decode_asi(insn, regs);
454 int flag = (freg < 32) ? FPRS_DL : FPRS_DU;
456 save_and_clear_fpu();
457 current_thread_info()->xfsr[0] &= ~0x1c000;
458 if (freg & 3) {
459 current_thread_info()->xfsr[0] |= (6 << 14) /* invalid_fp_register */;
460 do_fpother(regs);
461 return 0;
463 if (insn & 0x200000) {
464 /* STQ */
465 u64 first = 0, second = 0;
467 if (current_thread_info()->fpsaved[0] & flag) {
468 first = *(u64 *)&f->regs[freg];
469 second = *(u64 *)&f->regs[freg+2];
471 if (asi < 0x80) {
472 do_privact(regs);
473 return 1;
475 switch (asi) {
476 case ASI_P:
477 case ASI_S: break;
478 case ASI_PL:
479 case ASI_SL:
481 /* Need to convert endians */
482 u64 tmp = __swab64p(&first);
484 first = __swab64p(&second);
485 second = tmp;
486 break;
488 default:
489 if (tlb_type == hypervisor)
490 sun4v_data_access_exception(regs, addr, 0);
491 else
492 spitfire_data_access_exception(regs, 0, addr);
493 return 1;
495 if (put_user (first >> 32, (u32 __user *)addr) ||
496 __put_user ((u32)first, (u32 __user *)(addr + 4)) ||
497 __put_user (second >> 32, (u32 __user *)(addr + 8)) ||
498 __put_user ((u32)second, (u32 __user *)(addr + 12))) {
499 if (tlb_type == hypervisor)
500 sun4v_data_access_exception(regs, addr, 0);
501 else
502 spitfire_data_access_exception(regs, 0, addr);
503 return 1;
505 } else {
506 /* LDF, LDDF, LDQF */
507 u32 data[4] __attribute__ ((aligned(8)));
508 int size, i;
509 int err;
511 if (asi < 0x80) {
512 do_privact(regs);
513 return 1;
514 } else if (asi > ASI_SNFL) {
515 if (tlb_type == hypervisor)
516 sun4v_data_access_exception(regs, addr, 0);
517 else
518 spitfire_data_access_exception(regs, 0, addr);
519 return 1;
521 switch (insn & 0x180000) {
522 case 0x000000: size = 1; break;
523 case 0x100000: size = 4; break;
524 default: size = 2; break;
526 for (i = 0; i < size; i++)
527 data[i] = 0;
529 err = get_user (data[0], (u32 __user *) addr);
530 if (!err) {
531 for (i = 1; i < size; i++)
532 err |= __get_user (data[i], (u32 __user *)(addr + 4*i));
534 if (err && !(asi & 0x2 /* NF */)) {
535 if (tlb_type == hypervisor)
536 sun4v_data_access_exception(regs, addr, 0);
537 else
538 spitfire_data_access_exception(regs, 0, addr);
539 return 1;
541 if (asi & 0x8) /* Little */ {
542 u64 tmp;
544 switch (size) {
545 case 1: data[0] = le32_to_cpup(data + 0); break;
546 default:*(u64 *)(data + 0) = le64_to_cpup((u64 *)(data + 0));
547 break;
548 case 4: tmp = le64_to_cpup((u64 *)(data + 0));
549 *(u64 *)(data + 0) = le64_to_cpup((u64 *)(data + 2));
550 *(u64 *)(data + 2) = tmp;
551 break;
554 if (!(current_thread_info()->fpsaved[0] & FPRS_FEF)) {
555 current_thread_info()->fpsaved[0] = FPRS_FEF;
556 current_thread_info()->gsr[0] = 0;
558 if (!(current_thread_info()->fpsaved[0] & flag)) {
559 if (freg < 32)
560 memset(f->regs, 0, 32*sizeof(u32));
561 else
562 memset(f->regs+32, 0, 32*sizeof(u32));
564 memcpy(f->regs + freg, data, size * 4);
565 current_thread_info()->fpsaved[0] |= flag;
567 advance(regs);
568 return 1;
571 void handle_ld_nf(u32 insn, struct pt_regs *regs)
573 int rd = ((insn >> 25) & 0x1f);
574 int from_kernel = (regs->tstate & TSTATE_PRIV) != 0;
575 unsigned long *reg;
577 maybe_flush_windows(0, 0, rd, from_kernel);
578 reg = fetch_reg_addr(rd, regs);
579 if (from_kernel || rd < 16) {
580 reg[0] = 0;
581 if ((insn & 0x780000) == 0x180000)
582 reg[1] = 0;
583 } else if (test_thread_flag(TIF_32BIT)) {
584 put_user(0, (int __user *) reg);
585 if ((insn & 0x780000) == 0x180000)
586 put_user(0, ((int __user *) reg) + 1);
587 } else {
588 put_user(0, (unsigned long __user *) reg);
589 if ((insn & 0x780000) == 0x180000)
590 put_user(0, (unsigned long __user *) reg + 1);
592 advance(regs);
595 void handle_lddfmna(struct pt_regs *regs, unsigned long sfar, unsigned long sfsr)
597 unsigned long pc = regs->tpc;
598 unsigned long tstate = regs->tstate;
599 u32 insn;
600 u32 first, second;
601 u64 value;
602 u8 freg;
603 int flag;
604 struct fpustate *f = FPUSTATE;
606 if (tstate & TSTATE_PRIV)
607 die_if_kernel("lddfmna from kernel", regs);
608 if (test_thread_flag(TIF_32BIT))
609 pc = (u32)pc;
610 if (get_user(insn, (u32 __user *) pc) != -EFAULT) {
611 int asi = decode_asi(insn, regs);
612 if ((asi > ASI_SNFL) ||
613 (asi < ASI_P))
614 goto daex;
615 if (get_user(first, (u32 __user *)sfar) ||
616 get_user(second, (u32 __user *)(sfar + 4))) {
617 if (asi & 0x2) /* NF */ {
618 first = 0; second = 0;
619 } else
620 goto daex;
622 save_and_clear_fpu();
623 freg = ((insn >> 25) & 0x1e) | ((insn >> 20) & 0x20);
624 value = (((u64)first) << 32) | second;
625 if (asi & 0x8) /* Little */
626 value = __swab64p(&value);
627 flag = (freg < 32) ? FPRS_DL : FPRS_DU;
628 if (!(current_thread_info()->fpsaved[0] & FPRS_FEF)) {
629 current_thread_info()->fpsaved[0] = FPRS_FEF;
630 current_thread_info()->gsr[0] = 0;
632 if (!(current_thread_info()->fpsaved[0] & flag)) {
633 if (freg < 32)
634 memset(f->regs, 0, 32*sizeof(u32));
635 else
636 memset(f->regs+32, 0, 32*sizeof(u32));
638 *(u64 *)(f->regs + freg) = value;
639 current_thread_info()->fpsaved[0] |= flag;
640 } else {
641 daex:
642 if (tlb_type == hypervisor)
643 sun4v_data_access_exception(regs, sfar, sfsr);
644 else
645 spitfire_data_access_exception(regs, sfsr, sfar);
646 return;
648 advance(regs);
649 return;
652 void handle_stdfmna(struct pt_regs *regs, unsigned long sfar, unsigned long sfsr)
654 unsigned long pc = regs->tpc;
655 unsigned long tstate = regs->tstate;
656 u32 insn;
657 u64 value;
658 u8 freg;
659 int flag;
660 struct fpustate *f = FPUSTATE;
662 if (tstate & TSTATE_PRIV)
663 die_if_kernel("stdfmna from kernel", regs);
664 if (test_thread_flag(TIF_32BIT))
665 pc = (u32)pc;
666 if (get_user(insn, (u32 __user *) pc) != -EFAULT) {
667 int asi = decode_asi(insn, regs);
668 freg = ((insn >> 25) & 0x1e) | ((insn >> 20) & 0x20);
669 value = 0;
670 flag = (freg < 32) ? FPRS_DL : FPRS_DU;
671 if ((asi > ASI_SNFL) ||
672 (asi < ASI_P))
673 goto daex;
674 save_and_clear_fpu();
675 if (current_thread_info()->fpsaved[0] & flag)
676 value = *(u64 *)&f->regs[freg];
677 switch (asi) {
678 case ASI_P:
679 case ASI_S: break;
680 case ASI_PL:
681 case ASI_SL:
682 value = __swab64p(&value); break;
683 default: goto daex;
685 if (put_user (value >> 32, (u32 __user *) sfar) ||
686 __put_user ((u32)value, (u32 __user *)(sfar + 4)))
687 goto daex;
688 } else {
689 daex:
690 if (tlb_type == hypervisor)
691 sun4v_data_access_exception(regs, sfar, sfsr);
692 else
693 spitfire_data_access_exception(regs, sfsr, sfar);
694 return;
696 advance(regs);
697 return;