2 * Unaligned memory access handler
4 * Copyright (C) 2001 Randolph Chung <tausq@debian.org>
5 * Significantly tweaked by LaMont Jones <lamont@debian.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <linux/jiffies.h>
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/sched/signal.h>
27 #include <linux/sched/debug.h>
28 #include <linux/signal.h>
29 #include <linux/ratelimit.h>
30 #include <linux/uaccess.h>
31 #include <asm/hardirq.h>
32 #include <asm/traps.h>
34 /* #define DEBUG_UNALIGNED 1 */
36 #ifdef DEBUG_UNALIGNED
37 #define DPRINTF(fmt, args...) do { printk(KERN_DEBUG "%s:%d:%s ", __FILE__, __LINE__, __func__ ); printk(KERN_DEBUG fmt, ##args ); } while (0)
39 #define DPRINTF(fmt, args...)
48 #define FIXUP_BRANCH(lbl) \
49 "\tldil L%%" #lbl ", %%r1\n" \
50 "\tldo R%%" #lbl "(%%r1), %%r1\n" \
52 /* If you use FIXUP_BRANCH, then you must list this clobber */
53 #define FIXUP_BRANCH_CLOBBER "r1"
55 /* 1111 1100 0000 0000 0001 0011 1100 0000 */
56 #define OPCODE1(a,b,c) ((a)<<26|(b)<<12|(c)<<6)
57 #define OPCODE2(a,b) ((a)<<26|(b)<<1)
58 #define OPCODE3(a,b) ((a)<<26|(b)<<2)
59 #define OPCODE4(a) ((a)<<26)
60 #define OPCODE1_MASK OPCODE1(0x3f,1,0xf)
61 #define OPCODE2_MASK OPCODE2(0x3f,1)
62 #define OPCODE3_MASK OPCODE3(0x3f,1)
63 #define OPCODE4_MASK OPCODE4(0x3f)
65 /* skip LDB - never unaligned (index) */
66 #define OPCODE_LDH_I OPCODE1(0x03,0,0x1)
67 #define OPCODE_LDW_I OPCODE1(0x03,0,0x2)
68 #define OPCODE_LDD_I OPCODE1(0x03,0,0x3)
69 #define OPCODE_LDDA_I OPCODE1(0x03,0,0x4)
70 #define OPCODE_LDCD_I OPCODE1(0x03,0,0x5)
71 #define OPCODE_LDWA_I OPCODE1(0x03,0,0x6)
72 #define OPCODE_LDCW_I OPCODE1(0x03,0,0x7)
73 /* skip LDB - never unaligned (short) */
74 #define OPCODE_LDH_S OPCODE1(0x03,1,0x1)
75 #define OPCODE_LDW_S OPCODE1(0x03,1,0x2)
76 #define OPCODE_LDD_S OPCODE1(0x03,1,0x3)
77 #define OPCODE_LDDA_S OPCODE1(0x03,1,0x4)
78 #define OPCODE_LDCD_S OPCODE1(0x03,1,0x5)
79 #define OPCODE_LDWA_S OPCODE1(0x03,1,0x6)
80 #define OPCODE_LDCW_S OPCODE1(0x03,1,0x7)
81 /* skip STB - never unaligned */
82 #define OPCODE_STH OPCODE1(0x03,1,0x9)
83 #define OPCODE_STW OPCODE1(0x03,1,0xa)
84 #define OPCODE_STD OPCODE1(0x03,1,0xb)
85 /* skip STBY - never unaligned */
86 /* skip STDBY - never unaligned */
87 #define OPCODE_STWA OPCODE1(0x03,1,0xe)
88 #define OPCODE_STDA OPCODE1(0x03,1,0xf)
90 #define OPCODE_FLDWX OPCODE1(0x09,0,0x0)
91 #define OPCODE_FLDWXR OPCODE1(0x09,0,0x1)
92 #define OPCODE_FSTWX OPCODE1(0x09,0,0x8)
93 #define OPCODE_FSTWXR OPCODE1(0x09,0,0x9)
94 #define OPCODE_FLDWS OPCODE1(0x09,1,0x0)
95 #define OPCODE_FLDWSR OPCODE1(0x09,1,0x1)
96 #define OPCODE_FSTWS OPCODE1(0x09,1,0x8)
97 #define OPCODE_FSTWSR OPCODE1(0x09,1,0x9)
98 #define OPCODE_FLDDX OPCODE1(0x0b,0,0x0)
99 #define OPCODE_FSTDX OPCODE1(0x0b,0,0x8)
100 #define OPCODE_FLDDS OPCODE1(0x0b,1,0x0)
101 #define OPCODE_FSTDS OPCODE1(0x0b,1,0x8)
103 #define OPCODE_LDD_L OPCODE2(0x14,0)
104 #define OPCODE_FLDD_L OPCODE2(0x14,1)
105 #define OPCODE_STD_L OPCODE2(0x1c,0)
106 #define OPCODE_FSTD_L OPCODE2(0x1c,1)
108 #define OPCODE_LDW_M OPCODE3(0x17,1)
109 #define OPCODE_FLDW_L OPCODE3(0x17,0)
110 #define OPCODE_FSTW_L OPCODE3(0x1f,0)
111 #define OPCODE_STW_M OPCODE3(0x1f,1)
113 #define OPCODE_LDH_L OPCODE4(0x11)
114 #define OPCODE_LDW_L OPCODE4(0x12)
115 #define OPCODE_LDWM OPCODE4(0x13)
116 #define OPCODE_STH_L OPCODE4(0x19)
117 #define OPCODE_STW_L OPCODE4(0x1A)
118 #define OPCODE_STWM OPCODE4(0x1B)
120 #define MAJOR_OP(i) (((i)>>26)&0x3f)
121 #define R1(i) (((i)>>21)&0x1f)
122 #define R2(i) (((i)>>16)&0x1f)
123 #define R3(i) ((i)&0x1f)
124 #define FR3(i) ((((i)<<1)&0x1f)|(((i)>>6)&1))
125 #define IM(i,n) (((i)>>1&((1<<(n-1))-1))|((i)&1?((0-1L)<<(n-1)):0))
126 #define IM5_2(i) IM((i)>>16,5)
127 #define IM5_3(i) IM((i),5)
128 #define IM14(i) IM((i),14)
130 #define ERR_NOTHANDLED -1
131 #define ERR_PAGEFAULT -2
133 int unaligned_enabled __read_mostly
= 1;
135 static int emulate_ldh(struct pt_regs
*regs
, int toreg
)
137 unsigned long saddr
= regs
->ior
;
138 unsigned long val
= 0;
141 DPRINTF("load " RFMT
":" RFMT
" to r%d for 2 bytes\n",
142 regs
->isr
, regs
->ior
, toreg
);
144 __asm__
__volatile__ (
146 "1: ldbs 0(%%sr1,%3), %%r20\n"
147 "2: ldbs 1(%%sr1,%3), %0\n"
148 " depw %%r20, 23, 24, %0\n"
151 " .section .fixup,\"ax\"\n"
155 ASM_EXCEPTIONTABLE_ENTRY(1b
, 4b
)
156 ASM_EXCEPTIONTABLE_ENTRY(2b
, 4b
)
157 : "=r" (val
), "=r" (ret
)
158 : "0" (val
), "r" (saddr
), "r" (regs
->isr
)
159 : "r20", FIXUP_BRANCH_CLOBBER
);
161 DPRINTF("val = 0x" RFMT
"\n", val
);
164 regs
->gr
[toreg
] = val
;
169 static int emulate_ldw(struct pt_regs
*regs
, int toreg
, int flop
)
171 unsigned long saddr
= regs
->ior
;
172 unsigned long val
= 0;
175 DPRINTF("load " RFMT
":" RFMT
" to r%d for 4 bytes\n",
176 regs
->isr
, regs
->ior
, toreg
);
178 __asm__
__volatile__ (
179 " zdep %3,28,2,%%r19\n" /* r19=(ofs&3)*8 */
181 " depw %%r0,31,2,%3\n"
182 "1: ldw 0(%%sr1,%3),%0\n"
183 "2: ldw 4(%%sr1,%3),%%r20\n"
184 " subi 32,%%r19,%%r19\n"
186 " vshd %0,%%r20,%0\n"
189 " .section .fixup,\"ax\"\n"
193 ASM_EXCEPTIONTABLE_ENTRY(1b
, 4b
)
194 ASM_EXCEPTIONTABLE_ENTRY(2b
, 4b
)
195 : "=r" (val
), "=r" (ret
)
196 : "0" (val
), "r" (saddr
), "r" (regs
->isr
)
197 : "r19", "r20", FIXUP_BRANCH_CLOBBER
);
199 DPRINTF("val = 0x" RFMT
"\n", val
);
202 ((__u32
*)(regs
->fr
))[toreg
] = val
;
204 regs
->gr
[toreg
] = val
;
208 static int emulate_ldd(struct pt_regs
*regs
, int toreg
, int flop
)
210 unsigned long saddr
= regs
->ior
;
214 DPRINTF("load " RFMT
":" RFMT
" to r%d for 8 bytes\n",
215 regs
->isr
, regs
->ior
, toreg
);
222 __asm__
__volatile__ (
223 " depd,z %3,60,3,%%r19\n" /* r19=(ofs&7)*8 */
225 " depd %%r0,63,3,%3\n"
226 "1: ldd 0(%%sr1,%3),%0\n"
227 "2: ldd 8(%%sr1,%3),%%r20\n"
228 " subi 64,%%r19,%%r19\n"
230 " shrpd %0,%%r20,%%sar,%0\n"
233 " .section .fixup,\"ax\"\n"
237 ASM_EXCEPTIONTABLE_ENTRY(1b
,4b
)
238 ASM_EXCEPTIONTABLE_ENTRY(2b
,4b
)
239 : "=r" (val
), "=r" (ret
)
240 : "0" (val
), "r" (saddr
), "r" (regs
->isr
)
241 : "r19", "r20", FIXUP_BRANCH_CLOBBER
);
244 unsigned long valh
=0,vall
=0;
245 __asm__
__volatile__ (
246 " zdep %5,29,2,%%r19\n" /* r19=(ofs&3)*8 */
248 " dep %%r0,31,2,%5\n"
249 "1: ldw 0(%%sr1,%5),%0\n"
250 "2: ldw 4(%%sr1,%5),%1\n"
251 "3: ldw 8(%%sr1,%5),%%r20\n"
252 " subi 32,%%r19,%%r19\n"
255 " vshd %1,%%r20,%1\n"
258 " .section .fixup,\"ax\"\n"
262 ASM_EXCEPTIONTABLE_ENTRY(1b
,5b
)
263 ASM_EXCEPTIONTABLE_ENTRY(2b
,5b
)
264 ASM_EXCEPTIONTABLE_ENTRY(3b
,5b
)
265 : "=r" (valh
), "=r" (vall
), "=r" (ret
)
266 : "0" (valh
), "1" (vall
), "r" (saddr
), "r" (regs
->isr
)
267 : "r19", "r20", FIXUP_BRANCH_CLOBBER
);
268 val
=((__u64
)valh
<<32)|(__u64
)vall
;
272 DPRINTF("val = 0x%llx\n", val
);
275 regs
->fr
[toreg
] = val
;
277 regs
->gr
[toreg
] = val
;
282 static int emulate_sth(struct pt_regs
*regs
, int frreg
)
284 unsigned long val
= regs
->gr
[frreg
];
290 DPRINTF("store r%d (0x" RFMT
") to " RFMT
":" RFMT
" for 2 bytes\n", frreg
,
291 val
, regs
->isr
, regs
->ior
);
293 __asm__
__volatile__ (
295 " extrw,u %1, 23, 8, %%r19\n"
296 "1: stb %1, 1(%%sr1, %2)\n"
297 "2: stb %%r19, 0(%%sr1, %2)\n"
300 " .section .fixup,\"ax\"\n"
304 ASM_EXCEPTIONTABLE_ENTRY(1b
,4b
)
305 ASM_EXCEPTIONTABLE_ENTRY(2b
,4b
)
307 : "r" (val
), "r" (regs
->ior
), "r" (regs
->isr
)
308 : "r19", FIXUP_BRANCH_CLOBBER
);
313 static int emulate_stw(struct pt_regs
*regs
, int frreg
, int flop
)
319 val
= ((__u32
*)(regs
->fr
))[frreg
];
321 val
= regs
->gr
[frreg
];
325 DPRINTF("store r%d (0x" RFMT
") to " RFMT
":" RFMT
" for 4 bytes\n", frreg
,
326 val
, regs
->isr
, regs
->ior
);
329 __asm__
__volatile__ (
331 " zdep %2, 28, 2, %%r19\n"
332 " dep %%r0, 31, 2, %2\n"
334 " depwi,z -2, %%sar, 32, %%r19\n"
335 "1: ldw 0(%%sr1,%2),%%r20\n"
336 "2: ldw 4(%%sr1,%2),%%r21\n"
337 " vshd %%r0, %1, %%r22\n"
338 " vshd %1, %%r0, %%r1\n"
339 " and %%r20, %%r19, %%r20\n"
340 " andcm %%r21, %%r19, %%r21\n"
341 " or %%r22, %%r20, %%r20\n"
342 " or %%r1, %%r21, %%r21\n"
343 " stw %%r20,0(%%sr1,%2)\n"
344 " stw %%r21,4(%%sr1,%2)\n"
347 " .section .fixup,\"ax\"\n"
351 ASM_EXCEPTIONTABLE_ENTRY(1b
,4b
)
352 ASM_EXCEPTIONTABLE_ENTRY(2b
,4b
)
354 : "r" (val
), "r" (regs
->ior
), "r" (regs
->isr
)
355 : "r19", "r20", "r21", "r22", "r1", FIXUP_BRANCH_CLOBBER
);
359 static int emulate_std(struct pt_regs
*regs
, int frreg
, int flop
)
365 val
= regs
->fr
[frreg
];
367 val
= regs
->gr
[frreg
];
371 DPRINTF("store r%d (0x%016llx) to " RFMT
":" RFMT
" for 8 bytes\n", frreg
,
372 val
, regs
->isr
, regs
->ior
);
379 __asm__
__volatile__ (
381 " depd,z %2, 60, 3, %%r19\n"
382 " depd %%r0, 63, 3, %2\n"
384 " depdi,z -2, %%sar, 64, %%r19\n"
385 "1: ldd 0(%%sr1,%2),%%r20\n"
386 "2: ldd 8(%%sr1,%2),%%r21\n"
387 " shrpd %%r0, %1, %%sar, %%r22\n"
388 " shrpd %1, %%r0, %%sar, %%r1\n"
389 " and %%r20, %%r19, %%r20\n"
390 " andcm %%r21, %%r19, %%r21\n"
391 " or %%r22, %%r20, %%r20\n"
392 " or %%r1, %%r21, %%r21\n"
393 "3: std %%r20,0(%%sr1,%2)\n"
394 "4: std %%r21,8(%%sr1,%2)\n"
397 " .section .fixup,\"ax\"\n"
401 ASM_EXCEPTIONTABLE_ENTRY(1b
,6b
)
402 ASM_EXCEPTIONTABLE_ENTRY(2b
,6b
)
403 ASM_EXCEPTIONTABLE_ENTRY(3b
,6b
)
404 ASM_EXCEPTIONTABLE_ENTRY(4b
,6b
)
406 : "r" (val
), "r" (regs
->ior
), "r" (regs
->isr
)
407 : "r19", "r20", "r21", "r22", "r1", FIXUP_BRANCH_CLOBBER
);
410 unsigned long valh
=(val
>>32),vall
=(val
&0xffffffffl
);
411 __asm__
__volatile__ (
413 " zdep %2, 29, 2, %%r19\n"
414 " dep %%r0, 31, 2, %2\n"
416 " zvdepi -2, 32, %%r19\n"
417 "1: ldw 0(%%sr1,%3),%%r20\n"
418 "2: ldw 8(%%sr1,%3),%%r21\n"
419 " vshd %1, %2, %%r1\n"
420 " vshd %%r0, %1, %1\n"
421 " vshd %2, %%r0, %2\n"
422 " and %%r20, %%r19, %%r20\n"
423 " andcm %%r21, %%r19, %%r21\n"
424 " or %1, %%r20, %1\n"
425 " or %2, %%r21, %2\n"
426 "3: stw %1,0(%%sr1,%1)\n"
427 "4: stw %%r1,4(%%sr1,%3)\n"
428 "5: stw %2,8(%%sr1,%3)\n"
431 " .section .fixup,\"ax\"\n"
435 ASM_EXCEPTIONTABLE_ENTRY(1b
,7b
)
436 ASM_EXCEPTIONTABLE_ENTRY(2b
,7b
)
437 ASM_EXCEPTIONTABLE_ENTRY(3b
,7b
)
438 ASM_EXCEPTIONTABLE_ENTRY(4b
,7b
)
439 ASM_EXCEPTIONTABLE_ENTRY(5b
,7b
)
441 : "r" (valh
), "r" (vall
), "r" (regs
->ior
), "r" (regs
->isr
)
442 : "r19", "r20", "r21", "r1", FIXUP_BRANCH_CLOBBER
);
449 void handle_unaligned(struct pt_regs
*regs
)
451 static DEFINE_RATELIMIT_STATE(ratelimit
, 5 * HZ
, 5);
452 unsigned long newbase
= R1(regs
->iir
)?regs
->gr
[R1(regs
->iir
)]:0;
454 int ret
= ERR_NOTHANDLED
;
456 register int flop
=0; /* true if this is a flop */
458 __inc_irq_stat(irq_unaligned_count
);
460 /* log a message with pacing */
461 if (user_mode(regs
)) {
462 if (current
->thread
.flags
& PARISC_UAC_SIGBUS
) {
466 if (!(current
->thread
.flags
& PARISC_UAC_NOPRINT
) &&
467 __ratelimit(&ratelimit
)) {
469 sprintf(buf
, "%s(%d): unaligned access to 0x" RFMT
" at ip=0x" RFMT
"\n",
470 current
->comm
, task_pid_nr(current
), regs
->ior
, regs
->iaoq
[0]);
471 printk(KERN_WARNING
"%s", buf
);
472 #ifdef DEBUG_UNALIGNED
477 if (!unaligned_enabled
)
481 /* handle modification - OK, it's ugly, see the instruction manual */
482 switch (MAJOR_OP(regs
->iir
))
490 if (regs
->iir
&0x1000) /* short loads */
492 newbase
+= IM5_3(regs
->iir
);
494 newbase
+= IM5_2(regs
->iir
);
495 else if (regs
->iir
&0x2000) /* scaled indexed */
498 switch (regs
->iir
& OPCODE1_MASK
)
508 newbase
+= (R2(regs
->iir
)?regs
->gr
[R2(regs
->iir
)]:0)<<shift
;
509 } else /* simple indexed */
510 newbase
+= (R2(regs
->iir
)?regs
->gr
[R2(regs
->iir
)]:0);
516 newbase
+= IM14(regs
->iir
);
523 newbase
+= IM14(regs
->iir
&~0xe);
529 newbase
+= IM14(regs
->iir
&6);
536 newbase
+= IM14(regs
->iir
&~4);
541 /* TODO: make this cleaner... */
542 switch (regs
->iir
& OPCODE1_MASK
)
546 ret
= emulate_ldh(regs
, R3(regs
->iir
));
553 ret
= emulate_ldw(regs
, R3(regs
->iir
),0);
557 ret
= emulate_sth(regs
, R2(regs
->iir
));
562 ret
= emulate_stw(regs
, R2(regs
->iir
),0);
570 ret
= emulate_ldd(regs
, R3(regs
->iir
),0);
575 ret
= emulate_std(regs
, R2(regs
->iir
),0);
584 ret
= emulate_ldw(regs
,FR3(regs
->iir
),1);
590 ret
= emulate_ldd(regs
,R3(regs
->iir
),1);
598 ret
= emulate_stw(regs
,FR3(regs
->iir
),1);
604 ret
= emulate_std(regs
,R3(regs
->iir
),1);
611 ret
= ERR_NOTHANDLED
; /* "undefined", but lets kill them. */
615 switch (regs
->iir
& OPCODE2_MASK
)
619 ret
= emulate_ldd(regs
,R2(regs
->iir
),1);
623 ret
= emulate_std(regs
, R2(regs
->iir
),1);
626 ret
= emulate_ldd(regs
, R2(regs
->iir
),0);
629 ret
= emulate_std(regs
, R2(regs
->iir
),0);
633 switch (regs
->iir
& OPCODE3_MASK
)
637 ret
= emulate_ldw(regs
, R2(regs
->iir
),0);
640 ret
= emulate_ldw(regs
, R2(regs
->iir
),1);
645 ret
= emulate_stw(regs
, R2(regs
->iir
),1);
648 ret
= emulate_stw(regs
, R2(regs
->iir
),0);
651 switch (regs
->iir
& OPCODE4_MASK
)
654 ret
= emulate_ldh(regs
, R2(regs
->iir
));
658 ret
= emulate_ldw(regs
, R2(regs
->iir
),0);
661 ret
= emulate_sth(regs
, R2(regs
->iir
));
665 ret
= emulate_stw(regs
, R2(regs
->iir
),0);
669 if (ret
== 0 && modify
&& R1(regs
->iir
))
670 regs
->gr
[R1(regs
->iir
)] = newbase
;
673 if (ret
== ERR_NOTHANDLED
)
674 printk(KERN_CRIT
"Not-handled unaligned insn 0x%08lx\n", regs
->iir
);
676 DPRINTF("ret = %d\n", ret
);
681 * The unaligned handler failed.
682 * If we were called by __get_user() or __put_user() jump
683 * to it's exception fixup handler instead of crashing.
685 if (!user_mode(regs
) && fixup_exception(regs
))
688 printk(KERN_CRIT
"Unaligned handler failed, ret = %d\n", ret
);
689 die_if_kernel("Unaligned data reference", regs
, 28);
691 if (ret
== ERR_PAGEFAULT
)
693 si
.si_signo
= SIGSEGV
;
695 si
.si_code
= SEGV_MAPERR
;
696 si
.si_addr
= (void __user
*)regs
->ior
;
697 force_sig_info(SIGSEGV
, &si
, current
);
702 /* couldn't handle it ... */
703 si
.si_signo
= SIGBUS
;
705 si
.si_code
= BUS_ADRALN
;
706 si
.si_addr
= (void __user
*)regs
->ior
;
707 force_sig_info(SIGBUS
, &si
, current
);
713 /* else we handled it, let life go on. */
718 * NB: check_unaligned() is only used for PCXS processors right
719 * now, so we only check for PA1.1 encodings at this point.
723 check_unaligned(struct pt_regs
*regs
)
725 unsigned long align_mask
;
727 /* Get alignment mask */
730 switch (regs
->iir
& OPCODE1_MASK
) {
748 switch (regs
->iir
& OPCODE4_MASK
) {
763 return (int)(regs
->ior
& align_mask
);