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/kernel.h>
24 #include <linux/module.h>
25 #include <asm/uaccess.h>
27 /* #define DEBUG_UNALIGNED 1 */
29 #ifdef DEBUG_UNALIGNED
30 #define DPRINTF(fmt, args...) do { printk(KERN_DEBUG "%s:%d:%s ", __FILE__, __LINE__, __FUNCTION__ ); printk(KERN_DEBUG fmt, ##args ); } while (0)
32 #define DPRINTF(fmt, args...)
41 #define FIXUP_BRANCH(lbl) \
42 "\tldil L%%" #lbl ", %%r1\n" \
43 "\tldo R%%" #lbl "(%%r1), %%r1\n" \
45 /* If you use FIXUP_BRANCH, then you must list this clobber */
46 #define FIXUP_BRANCH_CLOBBER "r1"
48 /* 1111 1100 0000 0000 0001 0011 1100 0000 */
49 #define OPCODE1(a,b,c) ((a)<<26|(b)<<12|(c)<<6)
50 #define OPCODE2(a,b) ((a)<<26|(b)<<1)
51 #define OPCODE3(a,b) ((a)<<26|(b)<<2)
52 #define OPCODE4(a) ((a)<<26)
53 #define OPCODE1_MASK OPCODE1(0x3f,1,0xf)
54 #define OPCODE2_MASK OPCODE2(0x3f,1)
55 #define OPCODE3_MASK OPCODE3(0x3f,1)
56 #define OPCODE4_MASK OPCODE4(0x3f)
58 /* skip LDB - never unaligned (index) */
59 #define OPCODE_LDH_I OPCODE1(0x03,0,0x1)
60 #define OPCODE_LDW_I OPCODE1(0x03,0,0x2)
61 #define OPCODE_LDD_I OPCODE1(0x03,0,0x3)
62 #define OPCODE_LDDA_I OPCODE1(0x03,0,0x4)
63 #define OPCODE_LDCD_I OPCODE1(0x03,0,0x5)
64 #define OPCODE_LDWA_I OPCODE1(0x03,0,0x6)
65 #define OPCODE_LDCW_I OPCODE1(0x03,0,0x7)
66 /* skip LDB - never unaligned (short) */
67 #define OPCODE_LDH_S OPCODE1(0x03,1,0x1)
68 #define OPCODE_LDW_S OPCODE1(0x03,1,0x2)
69 #define OPCODE_LDD_S OPCODE1(0x03,1,0x3)
70 #define OPCODE_LDDA_S OPCODE1(0x03,1,0x4)
71 #define OPCODE_LDCD_S OPCODE1(0x03,1,0x5)
72 #define OPCODE_LDWA_S OPCODE1(0x03,1,0x6)
73 #define OPCODE_LDCW_S OPCODE1(0x03,1,0x7)
74 /* skip STB - never unaligned */
75 #define OPCODE_STH OPCODE1(0x03,1,0x9)
76 #define OPCODE_STW OPCODE1(0x03,1,0xa)
77 #define OPCODE_STD OPCODE1(0x03,1,0xb)
78 /* skip STBY - never unaligned */
79 /* skip STDBY - never unaligned */
80 #define OPCODE_STWA OPCODE1(0x03,1,0xe)
81 #define OPCODE_STDA OPCODE1(0x03,1,0xf)
83 #define OPCODE_FLDWX OPCODE1(0x09,0,0x0)
84 #define OPCODE_FLDWXR OPCODE1(0x09,0,0x1)
85 #define OPCODE_FSTWX OPCODE1(0x09,0,0x8)
86 #define OPCODE_FSTWXR OPCODE1(0x09,0,0x9)
87 #define OPCODE_FLDWS OPCODE1(0x09,1,0x0)
88 #define OPCODE_FLDWSR OPCODE1(0x09,1,0x1)
89 #define OPCODE_FSTWS OPCODE1(0x09,1,0x8)
90 #define OPCODE_FSTWSR OPCODE1(0x09,1,0x9)
91 #define OPCODE_FLDDX OPCODE1(0x0b,0,0x0)
92 #define OPCODE_FSTDX OPCODE1(0x0b,0,0x8)
93 #define OPCODE_FLDDS OPCODE1(0x0b,1,0x0)
94 #define OPCODE_FSTDS OPCODE1(0x0b,1,0x8)
96 #define OPCODE_LDD_L OPCODE2(0x14,0)
97 #define OPCODE_FLDD_L OPCODE2(0x14,1)
98 #define OPCODE_STD_L OPCODE2(0x1c,0)
99 #define OPCODE_FSTD_L OPCODE2(0x1c,1)
101 #define OPCODE_LDW_M OPCODE3(0x17,1)
102 #define OPCODE_FLDW_L OPCODE3(0x17,0)
103 #define OPCODE_FSTW_L OPCODE3(0x1f,0)
104 #define OPCODE_STW_M OPCODE3(0x1f,1)
106 #define OPCODE_LDH_L OPCODE4(0x11)
107 #define OPCODE_LDW_L OPCODE4(0x12)
108 #define OPCODE_LDWM OPCODE4(0x13)
109 #define OPCODE_STH_L OPCODE4(0x19)
110 #define OPCODE_STW_L OPCODE4(0x1A)
111 #define OPCODE_STWM OPCODE4(0x1B)
113 #define MAJOR_OP(i) (((i)>>26)&0x3f)
114 #define R1(i) (((i)>>21)&0x1f)
115 #define R2(i) (((i)>>16)&0x1f)
116 #define R3(i) ((i)&0x1f)
117 #define FR3(i) ((((i)<<1)&0x1f)|(((i)>>6)&1))
118 #define IM(i,n) (((i)>>1&((1<<(n-1))-1))|((i)&1?((0-1L)<<(n-1)):0))
119 #define IM5_2(i) IM((i)>>16,5)
120 #define IM5_3(i) IM((i),5)
121 #define IM14(i) IM((i),14)
123 #define ERR_NOTHANDLED -1
124 #define ERR_PAGEFAULT -2
126 int unaligned_enabled __read_mostly
= 1;
128 void die_if_kernel (char *str
, struct pt_regs
*regs
, long err
);
130 static int emulate_ldh(struct pt_regs
*regs
, int toreg
)
132 unsigned long saddr
= regs
->ior
;
133 unsigned long val
= 0;
136 DPRINTF("load " RFMT
":" RFMT
" to r%d for 2 bytes\n",
137 regs
->isr
, regs
->ior
, toreg
);
139 __asm__
__volatile__ (
141 "1: ldbs 0(%%sr1,%3), %%r20\n"
142 "2: ldbs 1(%%sr1,%3), %0\n"
143 " depw %%r20, 23, 24, %0\n"
146 " .section .fixup,\"ax\"\n"
150 " .section __ex_table,\"aw\"\n"
159 : "=r" (val
), "=r" (ret
)
160 : "0" (val
), "r" (saddr
), "r" (regs
->isr
)
161 : "r20", FIXUP_BRANCH_CLOBBER
);
163 DPRINTF("val = 0x" RFMT
"\n", val
);
166 regs
->gr
[toreg
] = val
;
171 static int emulate_ldw(struct pt_regs
*regs
, int toreg
, int flop
)
173 unsigned long saddr
= regs
->ior
;
174 unsigned long val
= 0;
177 DPRINTF("load " RFMT
":" RFMT
" to r%d for 4 bytes\n",
178 regs
->isr
, regs
->ior
, toreg
);
180 __asm__
__volatile__ (
181 " zdep %3,28,2,%%r19\n" /* r19=(ofs&3)*8 */
183 " depw %%r0,31,2,%3\n"
184 "1: ldw 0(%%sr1,%3),%0\n"
185 "2: ldw 4(%%sr1,%3),%%r20\n"
186 " subi 32,%%r19,%%r19\n"
188 " vshd %0,%%r20,%0\n"
191 " .section .fixup,\"ax\"\n"
195 " .section __ex_table,\"aw\"\n"
204 : "=r" (val
), "=r" (ret
)
205 : "0" (val
), "r" (saddr
), "r" (regs
->isr
)
206 : "r19", "r20", FIXUP_BRANCH_CLOBBER
);
208 DPRINTF("val = 0x" RFMT
"\n", val
);
211 ((__u32
*)(regs
->fr
))[toreg
] = val
;
213 regs
->gr
[toreg
] = val
;
217 static int emulate_ldd(struct pt_regs
*regs
, int toreg
, int flop
)
219 unsigned long saddr
= regs
->ior
;
223 DPRINTF("load " RFMT
":" RFMT
" to r%d for 8 bytes\n",
224 regs
->isr
, regs
->ior
, toreg
);
231 __asm__
__volatile__ (
232 " depd,z %3,60,3,%%r19\n" /* r19=(ofs&7)*8 */
234 " depd %%r0,63,3,%3\n"
235 "1: ldd 0(%%sr1,%3),%0\n"
236 "2: ldd 8(%%sr1,%3),%%r20\n"
237 " subi 64,%%r19,%%r19\n"
239 " shrpd %0,%%r20,%%sar,%0\n"
242 " .section .fixup,\"ax\"\n"
246 " .section __ex_table,\"aw\"\n"
255 : "=r" (val
), "=r" (ret
)
256 : "0" (val
), "r" (saddr
), "r" (regs
->isr
)
257 : "r19", "r20", FIXUP_BRANCH_CLOBBER
);
260 unsigned long valh
=0,vall
=0;
261 __asm__
__volatile__ (
262 " zdep %5,29,2,%%r19\n" /* r19=(ofs&3)*8 */
264 " dep %%r0,31,2,%5\n"
265 "1: ldw 0(%%sr1,%5),%0\n"
266 "2: ldw 4(%%sr1,%5),%1\n"
267 "3: ldw 8(%%sr1,%5),%%r20\n"
268 " subi 32,%%r19,%%r19\n"
271 " vshd %1,%%r20,%1\n"
274 " .section .fixup,\"ax\"\n"
278 " .section __ex_table,\"aw\"\n"
289 : "=r" (valh
), "=r" (vall
), "=r" (ret
)
290 : "0" (valh
), "1" (vall
), "r" (saddr
), "r" (regs
->isr
)
291 : "r19", "r20", FIXUP_BRANCH_CLOBBER
);
292 val
=((__u64
)valh
<<32)|(__u64
)vall
;
296 DPRINTF("val = 0x%llx\n", val
);
299 regs
->fr
[toreg
] = val
;
301 regs
->gr
[toreg
] = val
;
306 static int emulate_sth(struct pt_regs
*regs
, int frreg
)
308 unsigned long val
= regs
->gr
[frreg
];
314 DPRINTF("store r%d (0x" RFMT
") to " RFMT
":" RFMT
" for 2 bytes\n", frreg
,
315 val
, regs
->isr
, regs
->ior
);
317 __asm__
__volatile__ (
319 " extrw,u %1, 23, 8, %%r19\n"
320 "1: stb %1, 1(%%sr1, %2)\n"
321 "2: stb %%r19, 0(%%sr1, %2)\n"
324 " .section .fixup,\"ax\"\n"
328 " .section __ex_table,\"aw\"\n"
338 : "r" (val
), "r" (regs
->ior
), "r" (regs
->isr
)
339 : "r19", FIXUP_BRANCH_CLOBBER
);
344 static int emulate_stw(struct pt_regs
*regs
, int frreg
, int flop
)
350 val
= ((__u32
*)(regs
->fr
))[frreg
];
352 val
= regs
->gr
[frreg
];
356 DPRINTF("store r%d (0x" RFMT
") to " RFMT
":" RFMT
" for 4 bytes\n", frreg
,
357 val
, regs
->isr
, regs
->ior
);
360 __asm__
__volatile__ (
362 " zdep %2, 28, 2, %%r19\n"
363 " dep %%r0, 31, 2, %2\n"
365 " depwi,z -2, %%sar, 32, %%r19\n"
366 "1: ldw 0(%%sr1,%2),%%r20\n"
367 "2: ldw 4(%%sr1,%2),%%r21\n"
368 " vshd %%r0, %1, %%r22\n"
369 " vshd %1, %%r0, %%r1\n"
370 " and %%r20, %%r19, %%r20\n"
371 " andcm %%r21, %%r19, %%r21\n"
372 " or %%r22, %%r20, %%r20\n"
373 " or %%r1, %%r21, %%r21\n"
374 " stw %%r20,0(%%sr1,%2)\n"
375 " stw %%r21,4(%%sr1,%2)\n"
378 " .section .fixup,\"ax\"\n"
382 " .section __ex_table,\"aw\"\n"
392 : "r" (val
), "r" (regs
->ior
), "r" (regs
->isr
)
393 : "r19", "r20", "r21", "r22", "r1", FIXUP_BRANCH_CLOBBER
);
397 static int emulate_std(struct pt_regs
*regs
, int frreg
, int flop
)
403 val
= regs
->fr
[frreg
];
405 val
= regs
->gr
[frreg
];
409 DPRINTF("store r%d (0x%016llx) to " RFMT
":" RFMT
" for 8 bytes\n", frreg
,
410 val
, regs
->isr
, regs
->ior
);
417 __asm__
__volatile__ (
419 " depd,z %2, 60, 3, %%r19\n"
420 " depd %%r0, 63, 3, %2\n"
422 " depdi,z -2, %%sar, 64, %%r19\n"
423 "1: ldd 0(%%sr1,%2),%%r20\n"
424 "2: ldd 8(%%sr1,%2),%%r21\n"
425 " shrpd %%r0, %1, %%sar, %%r22\n"
426 " shrpd %1, %%r0, %%sar, %%r1\n"
427 " and %%r20, %%r19, %%r20\n"
428 " andcm %%r21, %%r19, %%r21\n"
429 " or %%r22, %%r20, %%r20\n"
430 " or %%r1, %%r21, %%r21\n"
431 "3: std %%r20,0(%%sr1,%2)\n"
432 "4: std %%r21,8(%%sr1,%2)\n"
435 " .section .fixup,\"ax\"\n"
439 " .section __ex_table,\"aw\"\n"
453 : "r" (val
), "r" (regs
->ior
), "r" (regs
->isr
)
454 : "r19", "r20", "r21", "r22", "r1", FIXUP_BRANCH_CLOBBER
);
457 unsigned long valh
=(val
>>32),vall
=(val
&0xffffffffl
);
458 __asm__
__volatile__ (
460 " zdep %2, 29, 2, %%r19\n"
461 " dep %%r0, 31, 2, %2\n"
463 " zvdepi -2, 32, %%r19\n"
464 "1: ldw 0(%%sr1,%3),%%r20\n"
465 "2: ldw 8(%%sr1,%3),%%r21\n"
466 " vshd %1, %2, %%r1\n"
467 " vshd %%r0, %1, %1\n"
468 " vshd %2, %%r0, %2\n"
469 " and %%r20, %%r19, %%r20\n"
470 " andcm %%r21, %%r19, %%r21\n"
471 " or %1, %%r20, %1\n"
472 " or %2, %%r21, %2\n"
473 "3: stw %1,0(%%sr1,%1)\n"
474 "4: stw %%r1,4(%%sr1,%3)\n"
475 "5: stw %2,8(%%sr1,%3)\n"
478 " .section .fixup,\"ax\"\n"
482 " .section __ex_table,\"aw\"\n"
498 : "r" (valh
), "r" (vall
), "r" (regs
->ior
), "r" (regs
->isr
)
499 : "r19", "r20", "r21", "r1", FIXUP_BRANCH_CLOBBER
);
506 void handle_unaligned(struct pt_regs
*regs
)
508 static unsigned long unaligned_count
= 0;
509 static unsigned long last_time
= 0;
510 unsigned long newbase
= R1(regs
->iir
)?regs
->gr
[R1(regs
->iir
)]:0;
512 int ret
= ERR_NOTHANDLED
;
514 register int flop
=0; /* true if this is a flop */
516 /* log a message with pacing */
517 if (user_mode(regs
)) {
518 if (current
->thread
.flags
& PARISC_UAC_SIGBUS
) {
522 if (unaligned_count
> 5 && jiffies
- last_time
> 5*HZ
) {
527 if (!(current
->thread
.flags
& PARISC_UAC_NOPRINT
)
528 && ++unaligned_count
< 5) {
530 sprintf(buf
, "%s(%d): unaligned access to 0x" RFMT
" at ip=0x" RFMT
"\n",
531 current
->comm
, current
->pid
, regs
->ior
, regs
->iaoq
[0]);
532 printk(KERN_WARNING
"%s", buf
);
533 #ifdef DEBUG_UNALIGNED
538 if (!unaligned_enabled
)
542 /* handle modification - OK, it's ugly, see the instruction manual */
543 switch (MAJOR_OP(regs
->iir
))
551 if (regs
->iir
&0x1000) /* short loads */
553 newbase
+= IM5_3(regs
->iir
);
555 newbase
+= IM5_2(regs
->iir
);
556 else if (regs
->iir
&0x2000) /* scaled indexed */
559 switch (regs
->iir
& OPCODE1_MASK
)
569 newbase
+= (R2(regs
->iir
)?regs
->gr
[R2(regs
->iir
)]:0)<<shift
;
570 } else /* simple indexed */
571 newbase
+= (R2(regs
->iir
)?regs
->gr
[R2(regs
->iir
)]:0);
577 newbase
+= IM14(regs
->iir
);
584 newbase
+= IM14(regs
->iir
&~0xe);
590 newbase
+= IM14(regs
->iir
&6);
597 newbase
+= IM14(regs
->iir
&~4);
602 /* TODO: make this cleaner... */
603 switch (regs
->iir
& OPCODE1_MASK
)
607 ret
= emulate_ldh(regs
, R3(regs
->iir
));
614 ret
= emulate_ldw(regs
, R3(regs
->iir
),0);
618 ret
= emulate_sth(regs
, R2(regs
->iir
));
623 ret
= emulate_stw(regs
, R2(regs
->iir
),0);
631 ret
= emulate_ldd(regs
, R3(regs
->iir
),0);
636 ret
= emulate_std(regs
, R2(regs
->iir
),0);
645 ret
= emulate_ldw(regs
,FR3(regs
->iir
),1);
651 ret
= emulate_ldd(regs
,R3(regs
->iir
),1);
659 ret
= emulate_stw(regs
,FR3(regs
->iir
),1);
665 ret
= emulate_std(regs
,R3(regs
->iir
),1);
672 ret
= ERR_NOTHANDLED
; /* "undefined", but lets kill them. */
676 switch (regs
->iir
& OPCODE2_MASK
)
680 ret
= emulate_ldd(regs
,R2(regs
->iir
),1);
684 ret
= emulate_std(regs
, R2(regs
->iir
),1);
689 ret
= emulate_ldd(regs
, R2(regs
->iir
),0);
692 ret
= emulate_std(regs
, R2(regs
->iir
),0);
697 switch (regs
->iir
& OPCODE3_MASK
)
701 ret
= emulate_ldw(regs
, R2(regs
->iir
),0);
704 ret
= emulate_ldw(regs
, R2(regs
->iir
),1);
709 ret
= emulate_stw(regs
, R2(regs
->iir
),1);
712 ret
= emulate_stw(regs
, R2(regs
->iir
),0);
715 switch (regs
->iir
& OPCODE4_MASK
)
718 ret
= emulate_ldh(regs
, R2(regs
->iir
));
722 ret
= emulate_ldw(regs
, R2(regs
->iir
),0);
725 ret
= emulate_sth(regs
, R2(regs
->iir
));
729 ret
= emulate_stw(regs
, R2(regs
->iir
),0);
733 if (modify
&& R1(regs
->iir
))
734 regs
->gr
[R1(regs
->iir
)] = newbase
;
737 if (ret
== ERR_NOTHANDLED
)
738 printk(KERN_CRIT
"Not-handled unaligned insn 0x%08lx\n", regs
->iir
);
740 DPRINTF("ret = %d\n", ret
);
744 printk(KERN_CRIT
"Unaligned handler failed, ret = %d\n", ret
);
745 die_if_kernel("Unaligned data reference", regs
, 28);
747 if (ret
== ERR_PAGEFAULT
)
749 si
.si_signo
= SIGSEGV
;
751 si
.si_code
= SEGV_MAPERR
;
752 si
.si_addr
= (void __user
*)regs
->ior
;
753 force_sig_info(SIGSEGV
, &si
, current
);
758 /* couldn't handle it ... */
759 si
.si_signo
= SIGBUS
;
761 si
.si_code
= BUS_ADRALN
;
762 si
.si_addr
= (void __user
*)regs
->ior
;
763 force_sig_info(SIGBUS
, &si
, current
);
769 /* else we handled it, let life go on. */
774 * NB: check_unaligned() is only used for PCXS processors right
775 * now, so we only check for PA1.1 encodings at this point.
779 check_unaligned(struct pt_regs
*regs
)
781 unsigned long align_mask
;
783 /* Get alignment mask */
786 switch (regs
->iir
& OPCODE1_MASK
) {
804 switch (regs
->iir
& OPCODE4_MASK
) {
819 return (int)(regs
->ior
& align_mask
);