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 <<<<<<< HEAD
:arch
/sparc64
/kernel
/unaligned
.c
12 #include <linux/jiffies.h>
13 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/sparc64
/kernel
/unaligned
.c
14 #include <linux/kernel.h>
15 #include <linux/sched.h>
17 #include <linux/module.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 */
32 load
, /* ld, ldd, ldh, ldsh */
33 store
, /* st, std, sth, stsh */
34 both
, /* Swap, ldstub, cas, ... */
41 static char *dirstrings
[] = {
42 "load", "store", "both", "fpload", "fpstore", "invalid"
46 static inline enum direction
decode_direction(unsigned int insn
)
48 unsigned long tmp
= (insn
>> 21) & 1;
53 switch ((insn
>>19)&0xf) {
62 /* 16 = double-word, 8 = extra-word, 4 = word, 2 = half-word */
63 static inline int decode_access_size(unsigned int insn
)
67 tmp
= ((insn
>> 19) & 0xf);
68 if (tmp
== 11 || tmp
== 14) /* ldx/stx */
74 return 16; /* ldd/std - Although it is actually 8 */
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...
91 static inline int decode_asi(unsigned int insn
, struct pt_regs
*regs
)
93 if (insn
& 0x800000) {
95 return (unsigned char)(regs
->tstate
>> 24); /* %asi */
97 return (unsigned char)(insn
>> 5); /* imm_asi */
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");
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
)
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]);
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]);
146 static unsigned long *fetch_reg_addr(unsigned int reg
, struct pt_regs
*regs
)
149 return ®s
->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];
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;
173 maybe_flush_windows(rs1
, 0, rd
, from_kernel
);
174 return (fetch_reg(rs1
, regs
) + sign_extend_imm13(insn
));
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
;
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
)) {
212 src_val
= swab16(src_val
);
215 src_val
= swab32(src_val
);
218 src_val
= swab64(src_val
);
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
;
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
);
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");
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
);
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
)
293 if (time_after(jiffies
, last_time
+ 5 * HZ
))
294 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/sparc64
/kernel
/unaligned
.c
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
);
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);
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);
333 unsigned long addr
, *reg_addr
;
336 addr
= compute_effective_address(regs
, insn
,
337 ((insn
>> 25) & 0x1f));
339 printk("KMNA: pc=%016lx [dir=%s addr=%016lx size=%d] "
341 regs
->tpc
, dirstrings
[dir
], addr
, size
,
342 regs
->u_regs
[UREG_RETPC
]);
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
;
365 val_in
= swab16(val_in
);
368 val_in
= swab32(val_in
);
371 val_in
= swab64(val_in
);
383 err
= do_int_store(((insn
>>25)&0x1f), size
,
384 (unsigned long *) addr
, regs
,
389 panic("Impossible kernel unaligned trap.");
393 kernel_mna_trap_fault(1);
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
)
407 int ret
, i
, rd
= ((insn
>> 25) & 0x1f);
408 int from_kernel
= (regs
->tstate
& TSTATE_PRIV
) != 0;
411 maybe_flush_windows(0, 0, rd
, from_kernel
);
412 value
= sign_extend_imm13(insn
);
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];
423 regs
->u_regs
[rd
] = ret
;
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]);
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]);
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
,
444 extern void sun4v_data_access_exception(struct pt_regs
*regs
,
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;
459 current_thread_info()->xfsr
[0] |= (6 << 14) /* invalid_fp_register */;
463 if (insn
& 0x200000) {
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];
481 /* Need to convert endians */
482 u64 tmp
= __swab64p(&first
);
484 first
= __swab64p(&second
);
489 if (tlb_type
== hypervisor
)
490 sun4v_data_access_exception(regs
, addr
, 0);
492 spitfire_data_access_exception(regs
, 0, addr
);
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);
502 spitfire_data_access_exception(regs
, 0, addr
);
506 /* LDF, LDDF, LDQF */
507 u32 data
[4] __attribute__ ((aligned(8)));
514 } else if (asi
> ASI_SNFL
) {
515 if (tlb_type
== hypervisor
)
516 sun4v_data_access_exception(regs
, addr
, 0);
518 spitfire_data_access_exception(regs
, 0, addr
);
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
++)
529 err
= get_user (data
[0], (u32 __user
*) addr
);
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);
538 spitfire_data_access_exception(regs
, 0, addr
);
541 if (asi
& 0x8) /* Little */ {
545 case 1: data
[0] = le32_to_cpup(data
+ 0); break;
546 default:*(u64
*)(data
+ 0) = le64_to_cpup((u64
*)(data
+ 0));
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
;
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
)) {
560 memset(f
->regs
, 0, 32*sizeof(u32
));
562 memset(f
->regs
+32, 0, 32*sizeof(u32
));
564 memcpy(f
->regs
+ freg
, data
, size
* 4);
565 current_thread_info()->fpsaved
[0] |= flag
;
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;
577 maybe_flush_windows(0, 0, rd
, from_kernel
);
578 reg
= fetch_reg_addr(rd
, regs
);
579 if (from_kernel
|| rd
< 16) {
581 if ((insn
& 0x780000) == 0x180000)
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);
588 put_user(0, (unsigned long __user
*) reg
);
589 if ((insn
& 0x780000) == 0x180000)
590 put_user(0, (unsigned long __user
*) reg
+ 1);
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
;
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
))
610 if (get_user(insn
, (u32 __user
*) pc
) != -EFAULT
) {
611 int asi
= decode_asi(insn
, regs
);
612 if ((asi
> ASI_SNFL
) ||
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;
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
)) {
634 memset(f
->regs
, 0, 32*sizeof(u32
));
636 memset(f
->regs
+32, 0, 32*sizeof(u32
));
638 *(u64
*)(f
->regs
+ freg
) = value
;
639 current_thread_info()->fpsaved
[0] |= flag
;
642 if (tlb_type
== hypervisor
)
643 sun4v_data_access_exception(regs
, sfar
, sfsr
);
645 spitfire_data_access_exception(regs
, sfsr
, sfar
);
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
;
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
))
666 if (get_user(insn
, (u32 __user
*) pc
) != -EFAULT
) {
667 int asi
= decode_asi(insn
, regs
);
668 freg
= ((insn
>> 25) & 0x1e) | ((insn
>> 20) & 0x20);
670 flag
= (freg
< 32) ? FPRS_DL
: FPRS_DU
;
671 if ((asi
> ASI_SNFL
) ||
674 save_and_clear_fpu();
675 if (current_thread_info()->fpsaved
[0] & flag
)
676 value
= *(u64
*)&f
->regs
[freg
];
682 value
= __swab64p(&value
); break;
685 if (put_user (value
>> 32, (u32 __user
*) sfar
) ||
686 __put_user ((u32
)value
, (u32 __user
*)(sfar
+ 4)))
690 if (tlb_type
== hypervisor
)
691 sun4v_data_access_exception(regs
, sfar
, sfsr
);
693 spitfire_data_access_exception(regs
, sfsr
, sfar
);