2 * unaligned.c: Unaligned load/store trap handling with special
3 * cases for the kernel to do them more quickly.
5 * Copyright (C) 1996,2008 David S. Miller (davem@davemloft.net)
6 * Copyright (C) 1996,1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
10 #include <linux/jiffies.h>
11 #include <linux/kernel.h>
12 #include <linux/sched.h>
14 #include <linux/module.h>
16 #include <asm/ptrace.h>
17 #include <asm/pstate.h>
18 #include <asm/processor.h>
19 #include <asm/uaccess.h>
20 #include <linux/smp.h>
21 #include <linux/bitops.h>
22 #include <linux/perf_event.h>
23 #include <linux/ratelimit.h>
24 #include <asm/fpumacro.h>
25 #include <asm/cacheflush.h>
28 load
, /* ld, ldd, ldh, ldsh */
29 store
, /* st, std, sth, stsh */
30 both
, /* Swap, ldstub, cas, ... */
36 static inline enum direction
decode_direction(unsigned int insn
)
38 unsigned long tmp
= (insn
>> 21) & 1;
43 switch ((insn
>>19)&0xf) {
52 /* 16 = double-word, 8 = extra-word, 4 = word, 2 = half-word */
53 static inline int decode_access_size(struct pt_regs
*regs
, unsigned int insn
)
57 tmp
= ((insn
>> 19) & 0xf);
58 if (tmp
== 11 || tmp
== 14) /* ldx/stx */
64 return 16; /* ldd/std - Although it is actually 8 */
68 printk("Impossible unaligned trap. insn=%08x\n", insn
);
69 die_if_kernel("Byte sized unaligned access?!?!", regs
);
71 /* GCC should never warn that control reaches the end
72 * of this function without returning a value because
73 * die_if_kernel() is marked with attribute 'noreturn'.
74 * Alas, some versions do...
81 static inline int decode_asi(unsigned int insn
, struct pt_regs
*regs
)
83 if (insn
& 0x800000) {
85 return (unsigned char)(regs
->tstate
>> 24); /* %asi */
87 return (unsigned char)(insn
>> 5); /* imm_asi */
92 /* 0x400000 = signed, 0 = unsigned */
93 static inline int decode_signedness(unsigned int insn
)
95 return (insn
& 0x400000);
98 static inline void maybe_flush_windows(unsigned int rs1
, unsigned int rs2
,
99 unsigned int rd
, int from_kernel
)
101 if (rs2
>= 16 || rs1
>= 16 || rd
>= 16) {
102 if (from_kernel
!= 0)
103 __asm__
__volatile__("flushw");
109 static inline long sign_extend_imm13(long imm
)
111 return imm
<< 51 >> 51;
114 static unsigned long fetch_reg(unsigned int reg
, struct pt_regs
*regs
)
116 unsigned long value
, fp
;
119 return (!reg
? 0 : regs
->u_regs
[reg
]);
121 fp
= regs
->u_regs
[UREG_FP
];
123 if (regs
->tstate
& TSTATE_PRIV
) {
124 struct reg_window
*win
;
125 win
= (struct reg_window
*)(fp
+ STACK_BIAS
);
126 value
= win
->locals
[reg
- 16];
127 } else if (!test_thread_64bit_stack(fp
)) {
128 struct reg_window32 __user
*win32
;
129 win32
= (struct reg_window32 __user
*)((unsigned long)((u32
)fp
));
130 get_user(value
, &win32
->locals
[reg
- 16]);
132 struct reg_window __user
*win
;
133 win
= (struct reg_window __user
*)(fp
+ STACK_BIAS
);
134 get_user(value
, &win
->locals
[reg
- 16]);
139 static unsigned long *fetch_reg_addr(unsigned int reg
, struct pt_regs
*regs
)
144 return ®s
->u_regs
[reg
];
146 fp
= regs
->u_regs
[UREG_FP
];
148 if (regs
->tstate
& TSTATE_PRIV
) {
149 struct reg_window
*win
;
150 win
= (struct reg_window
*)(fp
+ STACK_BIAS
);
151 return &win
->locals
[reg
- 16];
152 } else if (!test_thread_64bit_stack(fp
)) {
153 struct reg_window32
*win32
;
154 win32
= (struct reg_window32
*)((unsigned long)((u32
)fp
));
155 return (unsigned long *)&win32
->locals
[reg
- 16];
157 struct reg_window
*win
;
158 win
= (struct reg_window
*)(fp
+ STACK_BIAS
);
159 return &win
->locals
[reg
- 16];
163 unsigned long compute_effective_address(struct pt_regs
*regs
,
164 unsigned int insn
, unsigned int rd
)
166 unsigned int rs1
= (insn
>> 14) & 0x1f;
167 unsigned int rs2
= insn
& 0x1f;
168 int from_kernel
= (regs
->tstate
& TSTATE_PRIV
) != 0;
171 maybe_flush_windows(rs1
, 0, rd
, from_kernel
);
172 return (fetch_reg(rs1
, regs
) + sign_extend_imm13(insn
));
174 maybe_flush_windows(rs1
, rs2
, rd
, from_kernel
);
175 return (fetch_reg(rs1
, regs
) + fetch_reg(rs2
, regs
));
179 /* This is just to make gcc think die_if_kernel does return... */
180 static void __used
unaligned_panic(char *str
, struct pt_regs
*regs
)
182 die_if_kernel(str
, regs
);
185 extern int do_int_load(unsigned long *dest_reg
, int size
,
186 unsigned long *saddr
, int is_signed
, int asi
);
188 extern int __do_int_store(unsigned long *dst_addr
, int size
,
189 unsigned long src_val
, int asi
);
191 static inline int do_int_store(int reg_num
, int size
, unsigned long *dst_addr
,
192 struct pt_regs
*regs
, int asi
, int orig_asi
)
194 unsigned long zero
= 0;
195 unsigned long *src_val_p
= &zero
;
196 unsigned long src_val
;
200 zero
= (((long)(reg_num
?
201 (unsigned)fetch_reg(reg_num
, regs
) : 0)) << 32) |
202 (unsigned)fetch_reg(reg_num
+ 1, regs
);
203 } else if (reg_num
) {
204 src_val_p
= fetch_reg_addr(reg_num
, regs
);
206 src_val
= *src_val_p
;
207 if (unlikely(asi
!= orig_asi
)) {
210 src_val
= swab16(src_val
);
213 src_val
= swab32(src_val
);
216 src_val
= swab64(src_val
);
224 return __do_int_store(dst_addr
, size
, src_val
, asi
);
227 static inline void advance(struct pt_regs
*regs
)
229 regs
->tpc
= regs
->tnpc
;
231 if (test_thread_flag(TIF_32BIT
)) {
232 regs
->tpc
&= 0xffffffff;
233 regs
->tnpc
&= 0xffffffff;
237 static inline int floating_point_load_or_store_p(unsigned int insn
)
239 return (insn
>> 24) & 1;
242 static inline int ok_for_kernel(unsigned int insn
)
244 return !floating_point_load_or_store_p(insn
);
247 static void kernel_mna_trap_fault(int fixup_tstate_asi
)
249 struct pt_regs
*regs
= current_thread_info()->kern_una_regs
;
250 unsigned int insn
= current_thread_info()->kern_una_insn
;
251 const struct exception_table_entry
*entry
;
253 entry
= search_exception_tables(regs
->tpc
);
255 unsigned long address
;
257 address
= compute_effective_address(regs
, insn
,
258 ((insn
>> 25) & 0x1f));
259 if (address
< PAGE_SIZE
) {
260 printk(KERN_ALERT
"Unable to handle kernel NULL "
261 "pointer dereference in mna handler");
263 printk(KERN_ALERT
"Unable to handle kernel paging "
264 "request in mna handler");
265 printk(KERN_ALERT
" at virtual address %016lx\n",address
);
266 printk(KERN_ALERT
"current->{active_,}mm->context = %016lx\n",
267 (current
->mm
? CTX_HWBITS(current
->mm
->context
) :
268 CTX_HWBITS(current
->active_mm
->context
)));
269 printk(KERN_ALERT
"current->{active_,}mm->pgd = %016lx\n",
270 (current
->mm
? (unsigned long) current
->mm
->pgd
:
271 (unsigned long) current
->active_mm
->pgd
));
272 die_if_kernel("Oops", regs
);
275 regs
->tpc
= entry
->fixup
;
276 regs
->tnpc
= regs
->tpc
+ 4;
278 if (fixup_tstate_asi
) {
279 regs
->tstate
&= ~TSTATE_ASI
;
280 regs
->tstate
|= (ASI_AIUS
<< 24UL);
284 static void log_unaligned(struct pt_regs
*regs
)
286 static DEFINE_RATELIMIT_STATE(ratelimit
, 5 * HZ
, 5);
288 if (__ratelimit(&ratelimit
)) {
289 printk("Kernel unaligned access at TPC[%lx] %pS\n",
290 regs
->tpc
, (void *) regs
->tpc
);
294 asmlinkage
void kernel_unaligned_trap(struct pt_regs
*regs
, unsigned int insn
)
296 enum direction dir
= decode_direction(insn
);
297 int size
= decode_access_size(regs
, insn
);
300 current_thread_info()->kern_una_regs
= regs
;
301 current_thread_info()->kern_una_insn
= insn
;
303 orig_asi
= asi
= decode_asi(insn
, regs
);
305 /* If this is a {get,put}_user() on an unaligned userspace pointer,
306 * just signal a fault and do not log the event.
308 if (asi
== ASI_AIUS
) {
309 kernel_mna_trap_fault(0);
315 if (!ok_for_kernel(insn
) || dir
== both
) {
316 printk("Unsupported unaligned load/store trap for kernel "
317 "at <%016lx>.\n", regs
->tpc
);
318 unaligned_panic("Kernel does fpu/atomic "
319 "unaligned load/store.", regs
);
321 kernel_mna_trap_fault(0);
323 unsigned long addr
, *reg_addr
;
326 addr
= compute_effective_address(regs
, insn
,
327 ((insn
>> 25) & 0x1f));
328 perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS
, 1, regs
, addr
);
342 reg_addr
= fetch_reg_addr(((insn
>>25)&0x1f), regs
);
343 err
= do_int_load(reg_addr
, size
,
344 (unsigned long *) addr
,
345 decode_signedness(insn
), asi
);
346 if (likely(!err
) && unlikely(asi
!= orig_asi
)) {
347 unsigned long val_in
= *reg_addr
;
350 val_in
= swab16(val_in
);
353 val_in
= swab32(val_in
);
356 val_in
= swab64(val_in
);
368 err
= do_int_store(((insn
>>25)&0x1f), size
,
369 (unsigned long *) addr
, regs
,
374 panic("Impossible kernel unaligned trap.");
378 kernel_mna_trap_fault(1);
384 int handle_popc(u32 insn
, struct pt_regs
*regs
)
386 int from_kernel
= (regs
->tstate
& TSTATE_PRIV
) != 0;
387 int ret
, rd
= ((insn
>> 25) & 0x1f);
390 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS
, 1, regs
, 0);
392 maybe_flush_windows(0, 0, rd
, from_kernel
);
393 value
= sign_extend_imm13(insn
);
395 maybe_flush_windows(0, insn
& 0x1f, rd
, from_kernel
);
396 value
= fetch_reg(insn
& 0x1f, regs
);
398 ret
= hweight64(value
);
401 regs
->u_regs
[rd
] = ret
;
403 unsigned long fp
= regs
->u_regs
[UREG_FP
];
405 if (!test_thread_64bit_stack(fp
)) {
406 struct reg_window32 __user
*win32
;
407 win32
= (struct reg_window32 __user
*)((unsigned long)((u32
)fp
));
408 put_user(ret
, &win32
->locals
[rd
- 16]);
410 struct reg_window __user
*win
;
411 win
= (struct reg_window __user
*)(fp
+ STACK_BIAS
);
412 put_user(ret
, &win
->locals
[rd
- 16]);
419 extern void do_fpother(struct pt_regs
*regs
);
420 extern void do_privact(struct pt_regs
*regs
);
421 extern void spitfire_data_access_exception(struct pt_regs
*regs
,
424 extern void sun4v_data_access_exception(struct pt_regs
*regs
,
426 unsigned long type_ctx
);
428 int handle_ldf_stq(u32 insn
, struct pt_regs
*regs
)
430 unsigned long addr
= compute_effective_address(regs
, insn
, 0);
431 int freg
= ((insn
>> 25) & 0x1e) | ((insn
>> 20) & 0x20);
432 struct fpustate
*f
= FPUSTATE
;
433 int asi
= decode_asi(insn
, regs
);
434 int flag
= (freg
< 32) ? FPRS_DL
: FPRS_DU
;
436 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS
, 1, regs
, 0);
438 save_and_clear_fpu();
439 current_thread_info()->xfsr
[0] &= ~0x1c000;
441 current_thread_info()->xfsr
[0] |= (6 << 14) /* invalid_fp_register */;
445 if (insn
& 0x200000) {
447 u64 first
= 0, second
= 0;
449 if (current_thread_info()->fpsaved
[0] & flag
) {
450 first
= *(u64
*)&f
->regs
[freg
];
451 second
= *(u64
*)&f
->regs
[freg
+2];
463 /* Need to convert endians */
464 u64 tmp
= __swab64p(&first
);
466 first
= __swab64p(&second
);
471 if (tlb_type
== hypervisor
)
472 sun4v_data_access_exception(regs
, addr
, 0);
474 spitfire_data_access_exception(regs
, 0, addr
);
477 if (put_user (first
>> 32, (u32 __user
*)addr
) ||
478 __put_user ((u32
)first
, (u32 __user
*)(addr
+ 4)) ||
479 __put_user (second
>> 32, (u32 __user
*)(addr
+ 8)) ||
480 __put_user ((u32
)second
, (u32 __user
*)(addr
+ 12))) {
481 if (tlb_type
== hypervisor
)
482 sun4v_data_access_exception(regs
, addr
, 0);
484 spitfire_data_access_exception(regs
, 0, addr
);
488 /* LDF, LDDF, LDQF */
489 u32 data
[4] __attribute__ ((aligned(8)));
496 } else if (asi
> ASI_SNFL
) {
497 if (tlb_type
== hypervisor
)
498 sun4v_data_access_exception(regs
, addr
, 0);
500 spitfire_data_access_exception(regs
, 0, addr
);
503 switch (insn
& 0x180000) {
504 case 0x000000: size
= 1; break;
505 case 0x100000: size
= 4; break;
506 default: size
= 2; break;
508 for (i
= 0; i
< size
; i
++)
511 err
= get_user (data
[0], (u32 __user
*) addr
);
513 for (i
= 1; i
< size
; i
++)
514 err
|= __get_user (data
[i
], (u32 __user
*)(addr
+ 4*i
));
516 if (err
&& !(asi
& 0x2 /* NF */)) {
517 if (tlb_type
== hypervisor
)
518 sun4v_data_access_exception(regs
, addr
, 0);
520 spitfire_data_access_exception(regs
, 0, addr
);
523 if (asi
& 0x8) /* Little */ {
527 case 1: data
[0] = le32_to_cpup(data
+ 0); break;
528 default:*(u64
*)(data
+ 0) = le64_to_cpup((u64
*)(data
+ 0));
530 case 4: tmp
= le64_to_cpup((u64
*)(data
+ 0));
531 *(u64
*)(data
+ 0) = le64_to_cpup((u64
*)(data
+ 2));
532 *(u64
*)(data
+ 2) = tmp
;
536 if (!(current_thread_info()->fpsaved
[0] & FPRS_FEF
)) {
537 current_thread_info()->fpsaved
[0] = FPRS_FEF
;
538 current_thread_info()->gsr
[0] = 0;
540 if (!(current_thread_info()->fpsaved
[0] & flag
)) {
542 memset(f
->regs
, 0, 32*sizeof(u32
));
544 memset(f
->regs
+32, 0, 32*sizeof(u32
));
546 memcpy(f
->regs
+ freg
, data
, size
* 4);
547 current_thread_info()->fpsaved
[0] |= flag
;
553 void handle_ld_nf(u32 insn
, struct pt_regs
*regs
)
555 int rd
= ((insn
>> 25) & 0x1f);
556 int from_kernel
= (regs
->tstate
& TSTATE_PRIV
) != 0;
559 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS
, 1, regs
, 0);
561 maybe_flush_windows(0, 0, rd
, from_kernel
);
562 reg
= fetch_reg_addr(rd
, regs
);
563 if (from_kernel
|| rd
< 16) {
565 if ((insn
& 0x780000) == 0x180000)
567 } else if (!test_thread_64bit_stack(regs
->u_regs
[UREG_FP
])) {
568 put_user(0, (int __user
*) reg
);
569 if ((insn
& 0x780000) == 0x180000)
570 put_user(0, ((int __user
*) reg
) + 1);
572 put_user(0, (unsigned long __user
*) reg
);
573 if ((insn
& 0x780000) == 0x180000)
574 put_user(0, (unsigned long __user
*) reg
+ 1);
579 void handle_lddfmna(struct pt_regs
*regs
, unsigned long sfar
, unsigned long sfsr
)
581 unsigned long pc
= regs
->tpc
;
582 unsigned long tstate
= regs
->tstate
;
587 struct fpustate
*f
= FPUSTATE
;
589 if (tstate
& TSTATE_PRIV
)
590 die_if_kernel("lddfmna from kernel", regs
);
591 perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS
, 1, regs
, sfar
);
592 if (test_thread_flag(TIF_32BIT
))
594 if (get_user(insn
, (u32 __user
*) pc
) != -EFAULT
) {
595 int asi
= decode_asi(insn
, regs
);
599 if ((asi
> ASI_SNFL
) ||
603 err
= get_user(first
, (u32 __user
*)sfar
);
605 err
= get_user(second
, (u32 __user
*)(sfar
+ 4));
611 save_and_clear_fpu();
612 freg
= ((insn
>> 25) & 0x1e) | ((insn
>> 20) & 0x20);
613 value
= (((u64
)first
) << 32) | second
;
614 if (asi
& 0x8) /* Little */
615 value
= __swab64p(&value
);
616 flag
= (freg
< 32) ? FPRS_DL
: FPRS_DU
;
617 if (!(current_thread_info()->fpsaved
[0] & FPRS_FEF
)) {
618 current_thread_info()->fpsaved
[0] = FPRS_FEF
;
619 current_thread_info()->gsr
[0] = 0;
621 if (!(current_thread_info()->fpsaved
[0] & flag
)) {
623 memset(f
->regs
, 0, 32*sizeof(u32
));
625 memset(f
->regs
+32, 0, 32*sizeof(u32
));
627 *(u64
*)(f
->regs
+ freg
) = value
;
628 current_thread_info()->fpsaved
[0] |= flag
;
631 if (tlb_type
== hypervisor
)
632 sun4v_data_access_exception(regs
, sfar
, sfsr
);
634 spitfire_data_access_exception(regs
, sfsr
, sfar
);
640 void handle_stdfmna(struct pt_regs
*regs
, unsigned long sfar
, unsigned long sfsr
)
642 unsigned long pc
= regs
->tpc
;
643 unsigned long tstate
= regs
->tstate
;
648 struct fpustate
*f
= FPUSTATE
;
650 if (tstate
& TSTATE_PRIV
)
651 die_if_kernel("stdfmna from kernel", regs
);
652 perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS
, 1, regs
, sfar
);
653 if (test_thread_flag(TIF_32BIT
))
655 if (get_user(insn
, (u32 __user
*) pc
) != -EFAULT
) {
656 int asi
= decode_asi(insn
, regs
);
657 freg
= ((insn
>> 25) & 0x1e) | ((insn
>> 20) & 0x20);
659 flag
= (freg
< 32) ? FPRS_DL
: FPRS_DU
;
660 if ((asi
> ASI_SNFL
) ||
663 save_and_clear_fpu();
664 if (current_thread_info()->fpsaved
[0] & flag
)
665 value
= *(u64
*)&f
->regs
[freg
];
671 value
= __swab64p(&value
); break;
674 if (put_user (value
>> 32, (u32 __user
*) sfar
) ||
675 __put_user ((u32
)value
, (u32 __user
*)(sfar
+ 4)))
679 if (tlb_type
== hypervisor
)
680 sun4v_data_access_exception(regs
, sfar
, sfsr
);
682 spitfire_data_access_exception(regs
, sfsr
, sfar
);