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)
10 #include <linux/kernel.h>
11 #include <linux/sched.h>
13 #include <linux/module.h>
15 #include <asm/ptrace.h>
16 #include <asm/pstate.h>
17 #include <asm/processor.h>
18 #include <asm/system.h>
19 #include <asm/uaccess.h>
20 #include <linux/smp.h>
21 #include <linux/smp_lock.h>
22 #include <linux/bitops.h>
23 #include <asm/fpumacro.h>
25 /* #define DEBUG_MNA */
28 load, /* ld, ldd, ldh, ldsh */
29 store, /* st, std, sth, stsh */
30 both, /* Swap, ldstub, cas, ... */
37 static char *dirstrings[] = {
38 "load", "store", "both", "fpload", "fpstore", "invalid"
42 static inline enum direction decode_direction(unsigned int insn)
44 unsigned long tmp = (insn >> 21) & 1;
49 switch ((insn>>19)&0xf) {
58 /* 16 = double-word, 8 = extra-word, 4 = word, 2 = half-word */
59 static inline int decode_access_size(unsigned int insn)
63 tmp = ((insn >> 19) & 0xf);
64 if (tmp == 11 || tmp == 14) /* ldx/stx */
70 return 16; /* ldd/std - Although it is actually 8 */
74 printk("Impossible unaligned trap. insn=%08x\n", insn);
75 die_if_kernel("Byte sized unaligned access?!?!", current_thread_info()->kregs);
77 /* GCC should never warn that control reaches the end
78 * of this function without returning a value because
79 * die_if_kernel() is marked with attribute 'noreturn'.
80 * Alas, some versions do...
87 static inline int decode_asi(unsigned int insn, struct pt_regs *regs)
89 if (insn & 0x800000) {
91 return (unsigned char)(regs->tstate >> 24); /* %asi */
93 return (unsigned char)(insn >> 5); /* imm_asi */
98 /* 0x400000 = signed, 0 = unsigned */
99 static inline int decode_signedness(unsigned int insn)
101 return (insn & 0x400000);
104 static inline void maybe_flush_windows(unsigned int rs1, unsigned int rs2,
105 unsigned int rd, int from_kernel)
107 if (rs2 >= 16 || rs1 >= 16 || rd >= 16) {
108 if (from_kernel != 0)
109 __asm__ __volatile__("flushw");
115 static inline long sign_extend_imm13(long imm)
117 return imm << 51 >> 51;
120 static unsigned long fetch_reg(unsigned int reg, struct pt_regs *regs)
125 return (!reg ? 0 : regs->u_regs[reg]);
126 if (regs->tstate & TSTATE_PRIV) {
127 struct reg_window *win;
128 win = (struct reg_window *)(regs->u_regs[UREG_FP] + STACK_BIAS);
129 value = win->locals[reg - 16];
130 } else if (test_thread_flag(TIF_32BIT)) {
131 struct reg_window32 __user *win32;
132 win32 = (struct reg_window32 __user *)((unsigned long)((u32)regs->u_regs[UREG_FP]));
133 get_user(value, &win32->locals[reg - 16]);
135 struct reg_window __user *win;
136 win = (struct reg_window __user *)(regs->u_regs[UREG_FP] + STACK_BIAS);
137 get_user(value, &win->locals[reg - 16]);
142 static unsigned long *fetch_reg_addr(unsigned int reg, struct pt_regs *regs)
145 return ®s->u_regs[reg];
146 if (regs->tstate & TSTATE_PRIV) {
147 struct reg_window *win;
148 win = (struct reg_window *)(regs->u_regs[UREG_FP] + STACK_BIAS);
149 return &win->locals[reg - 16];
150 } else if (test_thread_flag(TIF_32BIT)) {
151 struct reg_window32 *win32;
152 win32 = (struct reg_window32 *)((unsigned long)((u32)regs->u_regs[UREG_FP]));
153 return (unsigned long *)&win32->locals[reg - 16];
155 struct reg_window *win;
156 win = (struct reg_window *)(regs->u_regs[UREG_FP] + STACK_BIAS);
157 return &win->locals[reg - 16];
161 unsigned long compute_effective_address(struct pt_regs *regs,
162 unsigned int insn, unsigned int rd)
164 unsigned int rs1 = (insn >> 14) & 0x1f;
165 unsigned int rs2 = insn & 0x1f;
166 int from_kernel = (regs->tstate & TSTATE_PRIV) != 0;
169 maybe_flush_windows(rs1, 0, rd, from_kernel);
170 return (fetch_reg(rs1, regs) + sign_extend_imm13(insn));
172 maybe_flush_windows(rs1, rs2, rd, from_kernel);
173 return (fetch_reg(rs1, regs) + fetch_reg(rs2, regs));
177 /* This is just to make gcc think die_if_kernel does return... */
178 static void __attribute_used__ unaligned_panic(char *str, struct pt_regs *regs)
180 die_if_kernel(str, regs);
183 extern void do_int_load(unsigned long *dest_reg, int size,
184 unsigned long *saddr, int is_signed, int asi);
186 extern void __do_int_store(unsigned long *dst_addr, int size,
187 unsigned long src_val, int asi);
189 static inline void do_int_store(int reg_num, int size, unsigned long *dst_addr,
190 struct pt_regs *regs, int asi, int orig_asi)
192 unsigned long zero = 0;
193 unsigned long *src_val_p = &zero;
194 unsigned long src_val;
198 zero = (((long)(reg_num ?
199 (unsigned)fetch_reg(reg_num, regs) : 0)) << 32) |
200 (unsigned)fetch_reg(reg_num + 1, regs);
201 } else if (reg_num) {
202 src_val_p = fetch_reg_addr(reg_num, regs);
204 src_val = *src_val_p;
205 if (unlikely(asi != orig_asi)) {
208 src_val = swab16(src_val);
211 src_val = swab32(src_val);
214 src_val = swab64(src_val);
222 __do_int_store(dst_addr, size, src_val, asi);
225 static inline void advance(struct pt_regs *regs)
227 regs->tpc = regs->tnpc;
229 if (test_thread_flag(TIF_32BIT)) {
230 regs->tpc &= 0xffffffff;
231 regs->tnpc &= 0xffffffff;
235 static inline int floating_point_load_or_store_p(unsigned int insn)
237 return (insn >> 24) & 1;
240 static inline int ok_for_kernel(unsigned int insn)
242 return !floating_point_load_or_store_p(insn);
245 void kernel_mna_trap_fault(void)
247 struct pt_regs *regs = current_thread_info()->kern_una_regs;
248 unsigned int insn = current_thread_info()->kern_una_insn;
249 unsigned long g2 = regs->u_regs[UREG_G2];
250 unsigned long fixup = search_extables_range(regs->tpc, &g2);
253 unsigned long address;
255 address = compute_effective_address(regs, insn,
256 ((insn >> 25) & 0x1f));
257 if (address < PAGE_SIZE) {
258 printk(KERN_ALERT "Unable to handle kernel NULL "
259 "pointer dereference in mna handler");
261 printk(KERN_ALERT "Unable to handle kernel paging "
262 "request in mna handler");
263 printk(KERN_ALERT " at virtual address %016lx\n",address);
264 printk(KERN_ALERT "current->{active_,}mm->context = %016lx\n",
265 (current->mm ? CTX_HWBITS(current->mm->context) :
266 CTX_HWBITS(current->active_mm->context)));
267 printk(KERN_ALERT "current->{active_,}mm->pgd = %016lx\n",
268 (current->mm ? (unsigned long) current->mm->pgd :
269 (unsigned long) current->active_mm->pgd));
270 die_if_kernel("Oops", regs);
274 regs->tnpc = regs->tpc + 4;
275 regs->u_regs [UREG_G2] = g2;
277 regs->tstate &= ~TSTATE_ASI;
278 regs->tstate |= (ASI_AIUS << 24UL);
281 asmlinkage void kernel_unaligned_trap(struct pt_regs *regs, unsigned int insn, unsigned long sfar, unsigned long sfsr)
283 enum direction dir = decode_direction(insn);
284 int size = decode_access_size(insn);
286 current_thread_info()->kern_una_regs = regs;
287 current_thread_info()->kern_una_insn = insn;
289 if (!ok_for_kernel(insn) || dir == both) {
290 printk("Unsupported unaligned load/store trap for kernel "
291 "at <%016lx>.\n", regs->tpc);
292 unaligned_panic("Kernel does fpu/atomic "
293 "unaligned load/store.", regs);
295 kernel_mna_trap_fault();
300 addr = compute_effective_address(regs, insn,
301 ((insn >> 25) & 0x1f));
303 printk("KMNA: pc=%016lx [dir=%s addr=%016lx size=%d] "
305 regs->tpc, dirstrings[dir], addr, size,
306 regs->u_regs[UREG_RETPC]);
308 orig_asi = asi = decode_asi(insn, regs);
322 do_int_load(fetch_reg_addr(((insn>>25)&0x1f), regs),
323 size, (unsigned long *) addr,
324 decode_signedness(insn), asi);
325 if (unlikely(asi != orig_asi)) {
326 unsigned long val_in = *(unsigned long *) addr;
329 val_in = swab16(val_in);
332 val_in = swab32(val_in);
335 val_in = swab64(val_in);
342 *(unsigned long *) addr = val_in;
347 do_int_store(((insn>>25)&0x1f), size,
348 (unsigned long *) addr, regs,
353 panic("Impossible kernel unaligned trap.");
360 static char popc_helper[] = {
361 0, 1, 1, 2, 1, 2, 2, 3,
362 1, 2, 2, 3, 2, 3, 3, 4,
365 int handle_popc(u32 insn, struct pt_regs *regs)
368 int ret, i, rd = ((insn >> 25) & 0x1f);
369 int from_kernel = (regs->tstate & TSTATE_PRIV) != 0;
372 maybe_flush_windows(0, 0, rd, from_kernel);
373 value = sign_extend_imm13(insn);
375 maybe_flush_windows(0, insn & 0x1f, rd, from_kernel);
376 value = fetch_reg(insn & 0x1f, regs);
378 for (ret = 0, i = 0; i < 16; i++) {
379 ret += popc_helper[value & 0xf];
384 regs->u_regs[rd] = ret;
386 if (test_thread_flag(TIF_32BIT)) {
387 struct reg_window32 __user *win32;
388 win32 = (struct reg_window32 __user *)((unsigned long)((u32)regs->u_regs[UREG_FP]));
389 put_user(ret, &win32->locals[rd - 16]);
391 struct reg_window __user *win;
392 win = (struct reg_window __user *)(regs->u_regs[UREG_FP] + STACK_BIAS);
393 put_user(ret, &win->locals[rd - 16]);
400 extern void do_fpother(struct pt_regs *regs);
401 extern void do_privact(struct pt_regs *regs);
402 extern void spitfire_data_access_exception(struct pt_regs *regs,
406 int handle_ldf_stq(u32 insn, struct pt_regs *regs)
408 unsigned long addr = compute_effective_address(regs, insn, 0);
409 int freg = ((insn >> 25) & 0x1e) | ((insn >> 20) & 0x20);
410 struct fpustate *f = FPUSTATE;
411 int asi = decode_asi(insn, regs);
412 int flag = (freg < 32) ? FPRS_DL : FPRS_DU;
414 save_and_clear_fpu();
415 current_thread_info()->xfsr[0] &= ~0x1c000;
417 current_thread_info()->xfsr[0] |= (6 << 14) /* invalid_fp_register */;
421 if (insn & 0x200000) {
423 u64 first = 0, second = 0;
425 if (current_thread_info()->fpsaved[0] & flag) {
426 first = *(u64 *)&f->regs[freg];
427 second = *(u64 *)&f->regs[freg+2];
439 /* Need to convert endians */
440 u64 tmp = __swab64p(&first);
442 first = __swab64p(&second);
447 spitfire_data_access_exception(regs, 0, addr);
450 if (put_user (first >> 32, (u32 __user *)addr) ||
451 __put_user ((u32)first, (u32 __user *)(addr + 4)) ||
452 __put_user (second >> 32, (u32 __user *)(addr + 8)) ||
453 __put_user ((u32)second, (u32 __user *)(addr + 12))) {
454 spitfire_data_access_exception(regs, 0, addr);
458 /* LDF, LDDF, LDQF */
459 u32 data[4] __attribute__ ((aligned(8)));
466 } else if (asi > ASI_SNFL) {
467 spitfire_data_access_exception(regs, 0, addr);
470 switch (insn & 0x180000) {
471 case 0x000000: size = 1; break;
472 case 0x100000: size = 4; break;
473 default: size = 2; break;
475 for (i = 0; i < size; i++)
478 err = get_user (data[0], (u32 __user *) addr);
480 for (i = 1; i < size; i++)
481 err |= __get_user (data[i], (u32 __user *)(addr + 4*i));
483 if (err && !(asi & 0x2 /* NF */)) {
484 spitfire_data_access_exception(regs, 0, addr);
487 if (asi & 0x8) /* Little */ {
491 case 1: data[0] = le32_to_cpup(data + 0); break;
492 default:*(u64 *)(data + 0) = le64_to_cpup((u64 *)(data + 0));
494 case 4: tmp = le64_to_cpup((u64 *)(data + 0));
495 *(u64 *)(data + 0) = le64_to_cpup((u64 *)(data + 2));
496 *(u64 *)(data + 2) = tmp;
500 if (!(current_thread_info()->fpsaved[0] & FPRS_FEF)) {
501 current_thread_info()->fpsaved[0] = FPRS_FEF;
502 current_thread_info()->gsr[0] = 0;
504 if (!(current_thread_info()->fpsaved[0] & flag)) {
506 memset(f->regs, 0, 32*sizeof(u32));
508 memset(f->regs+32, 0, 32*sizeof(u32));
510 memcpy(f->regs + freg, data, size * 4);
511 current_thread_info()->fpsaved[0] |= flag;
517 void handle_ld_nf(u32 insn, struct pt_regs *regs)
519 int rd = ((insn >> 25) & 0x1f);
520 int from_kernel = (regs->tstate & TSTATE_PRIV) != 0;
523 maybe_flush_windows(0, 0, rd, from_kernel);
524 reg = fetch_reg_addr(rd, regs);
525 if (from_kernel || rd < 16) {
527 if ((insn & 0x780000) == 0x180000)
529 } else if (test_thread_flag(TIF_32BIT)) {
530 put_user(0, (int __user *) reg);
531 if ((insn & 0x780000) == 0x180000)
532 put_user(0, ((int __user *) reg) + 1);
534 put_user(0, (unsigned long __user *) reg);
535 if ((insn & 0x780000) == 0x180000)
536 put_user(0, (unsigned long __user *) reg + 1);
541 void handle_lddfmna(struct pt_regs *regs, unsigned long sfar, unsigned long sfsr)
543 unsigned long pc = regs->tpc;
544 unsigned long tstate = regs->tstate;
550 struct fpustate *f = FPUSTATE;
552 if (tstate & TSTATE_PRIV)
553 die_if_kernel("lddfmna from kernel", regs);
554 if (test_thread_flag(TIF_32BIT))
556 if (get_user(insn, (u32 __user *) pc) != -EFAULT) {
558 if ((asi > ASI_SNFL) ||
561 if (get_user(first, (u32 __user *)sfar) ||
562 get_user(second, (u32 __user *)(sfar + 4))) {
563 if (asi & 0x2) /* NF */ {
564 first = 0; second = 0;
568 save_and_clear_fpu();
569 freg = ((insn >> 25) & 0x1e) | ((insn >> 20) & 0x20);
570 value = (((u64)first) << 32) | second;
571 if (asi & 0x8) /* Little */
572 value = __swab64p(&value);
573 flag = (freg < 32) ? FPRS_DL : FPRS_DU;
574 if (!(current_thread_info()->fpsaved[0] & FPRS_FEF)) {
575 current_thread_info()->fpsaved[0] = FPRS_FEF;
576 current_thread_info()->gsr[0] = 0;
578 if (!(current_thread_info()->fpsaved[0] & flag)) {
580 memset(f->regs, 0, 32*sizeof(u32));
582 memset(f->regs+32, 0, 32*sizeof(u32));
584 *(u64 *)(f->regs + freg) = value;
585 current_thread_info()->fpsaved[0] |= flag;
587 daex: spitfire_data_access_exception(regs, sfsr, sfar);
594 void handle_stdfmna(struct pt_regs *regs, unsigned long sfar, unsigned long sfsr)
596 unsigned long pc = regs->tpc;
597 unsigned long tstate = regs->tstate;
602 struct fpustate *f = FPUSTATE;
604 if (tstate & TSTATE_PRIV)
605 die_if_kernel("stdfmna from kernel", regs);
606 if (test_thread_flag(TIF_32BIT))
608 if (get_user(insn, (u32 __user *) pc) != -EFAULT) {
609 freg = ((insn >> 25) & 0x1e) | ((insn >> 20) & 0x20);
612 flag = (freg < 32) ? FPRS_DL : FPRS_DU;
613 if ((asi > ASI_SNFL) ||
616 save_and_clear_fpu();
617 if (current_thread_info()->fpsaved[0] & flag)
618 value = *(u64 *)&f->regs[freg];
624 value = __swab64p(&value); break;
627 if (put_user (value >> 32, (u32 __user *) sfar) ||
628 __put_user ((u32)value, (u32 __user *)(sfar + 4)))
631 daex: spitfire_data_access_exception(regs, sfsr, sfar);