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.h>
27 #include <linux/signal.h>
28 #include <asm/uaccess.h>
30 /* #define DEBUG_UNALIGNED 1 */
32 #ifdef DEBUG_UNALIGNED
33 #define DPRINTF(fmt, args...) do { printk(KERN_DEBUG "%s:%d:%s ", __FILE__, __LINE__, __FUNCTION__ ); printk(KERN_DEBUG fmt, ##args ); } while (0)
35 #define DPRINTF(fmt, args...)
44 #define FIXUP_BRANCH(lbl) \
45 "\tldil L%%" #lbl ", %%r1\n" \
46 "\tldo R%%" #lbl "(%%r1), %%r1\n" \
48 /* If you use FIXUP_BRANCH, then you must list this clobber */
49 #define FIXUP_BRANCH_CLOBBER "r1"
51 /* 1111 1100 0000 0000 0001 0011 1100 0000 */
52 #define OPCODE1(a,b,c) ((a)<<26|(b)<<12|(c)<<6)
53 #define OPCODE2(a,b) ((a)<<26|(b)<<1)
54 #define OPCODE3(a,b) ((a)<<26|(b)<<2)
55 #define OPCODE4(a) ((a)<<26)
56 #define OPCODE1_MASK OPCODE1(0x3f,1,0xf)
57 #define OPCODE2_MASK OPCODE2(0x3f,1)
58 #define OPCODE3_MASK OPCODE3(0x3f,1)
59 #define OPCODE4_MASK OPCODE4(0x3f)
61 /* skip LDB - never unaligned (index) */
62 #define OPCODE_LDH_I OPCODE1(0x03,0,0x1)
63 #define OPCODE_LDW_I OPCODE1(0x03,0,0x2)
64 #define OPCODE_LDD_I OPCODE1(0x03,0,0x3)
65 #define OPCODE_LDDA_I OPCODE1(0x03,0,0x4)
66 #define OPCODE_LDCD_I OPCODE1(0x03,0,0x5)
67 #define OPCODE_LDWA_I OPCODE1(0x03,0,0x6)
68 #define OPCODE_LDCW_I OPCODE1(0x03,0,0x7)
69 /* skip LDB - never unaligned (short) */
70 #define OPCODE_LDH_S OPCODE1(0x03,1,0x1)
71 #define OPCODE_LDW_S OPCODE1(0x03,1,0x2)
72 #define OPCODE_LDD_S OPCODE1(0x03,1,0x3)
73 #define OPCODE_LDDA_S OPCODE1(0x03,1,0x4)
74 #define OPCODE_LDCD_S OPCODE1(0x03,1,0x5)
75 #define OPCODE_LDWA_S OPCODE1(0x03,1,0x6)
76 #define OPCODE_LDCW_S OPCODE1(0x03,1,0x7)
77 /* skip STB - never unaligned */
78 #define OPCODE_STH OPCODE1(0x03,1,0x9)
79 #define OPCODE_STW OPCODE1(0x03,1,0xa)
80 #define OPCODE_STD OPCODE1(0x03,1,0xb)
81 /* skip STBY - never unaligned */
82 /* skip STDBY - never unaligned */
83 #define OPCODE_STWA OPCODE1(0x03,1,0xe)
84 #define OPCODE_STDA OPCODE1(0x03,1,0xf)
86 #define OPCODE_FLDWX OPCODE1(0x09,0,0x0)
87 #define OPCODE_FLDWXR OPCODE1(0x09,0,0x1)
88 #define OPCODE_FSTWX OPCODE1(0x09,0,0x8)
89 #define OPCODE_FSTWXR OPCODE1(0x09,0,0x9)
90 #define OPCODE_FLDWS OPCODE1(0x09,1,0x0)
91 #define OPCODE_FLDWSR OPCODE1(0x09,1,0x1)
92 #define OPCODE_FSTWS OPCODE1(0x09,1,0x8)
93 #define OPCODE_FSTWSR OPCODE1(0x09,1,0x9)
94 #define OPCODE_FLDDX OPCODE1(0x0b,0,0x0)
95 #define OPCODE_FSTDX OPCODE1(0x0b,0,0x8)
96 #define OPCODE_FLDDS OPCODE1(0x0b,1,0x0)
97 #define OPCODE_FSTDS OPCODE1(0x0b,1,0x8)
99 #define OPCODE_LDD_L OPCODE2(0x14,0)
100 #define OPCODE_FLDD_L OPCODE2(0x14,1)
101 #define OPCODE_STD_L OPCODE2(0x1c,0)
102 #define OPCODE_FSTD_L OPCODE2(0x1c,1)
104 #define OPCODE_LDW_M OPCODE3(0x17,1)
105 #define OPCODE_FLDW_L OPCODE3(0x17,0)
106 #define OPCODE_FSTW_L OPCODE3(0x1f,0)
107 #define OPCODE_STW_M OPCODE3(0x1f,1)
109 #define OPCODE_LDH_L OPCODE4(0x11)
110 #define OPCODE_LDW_L OPCODE4(0x12)
111 #define OPCODE_LDWM OPCODE4(0x13)
112 #define OPCODE_STH_L OPCODE4(0x19)
113 #define OPCODE_STW_L OPCODE4(0x1A)
114 #define OPCODE_STWM OPCODE4(0x1B)
116 #define MAJOR_OP(i) (((i)>>26)&0x3f)
117 #define R1(i) (((i)>>21)&0x1f)
118 #define R2(i) (((i)>>16)&0x1f)
119 #define R3(i) ((i)&0x1f)
120 #define FR3(i) ((((i)<<1)&0x1f)|(((i)>>6)&1))
121 #define IM(i,n) (((i)>>1&((1<<(n-1))-1))|((i)&1?((0-1L)<<(n-1)):0))
122 #define IM5_2(i) IM((i)>>16,5)
123 #define IM5_3(i) IM((i),5)
124 #define IM14(i) IM((i),14)
126 #define ERR_NOTHANDLED -1
127 #define ERR_PAGEFAULT -2
129 int unaligned_enabled __read_mostly = 1;
131 void die_if_kernel (char *str, struct pt_regs *regs, long err);
133 static int emulate_ldh(struct pt_regs *regs, int toreg)
135 unsigned long saddr = regs->ior;
136 unsigned long val = 0;
139 DPRINTF("load " RFMT ":" RFMT " to r%d for 2 bytes\n",
140 regs->isr, regs->ior, toreg);
142 __asm__ __volatile__ (
144 "1: ldbs 0(%%sr1,%3), %%r20\n"
145 "2: ldbs 1(%%sr1,%3), %0\n"
146 " depw %%r20, 23, 24, %0\n"
149 " .section .fixup,\"ax\"\n"
153 ASM_EXCEPTIONTABLE_ENTRY(1b, 4b)
154 ASM_EXCEPTIONTABLE_ENTRY(2b, 4b)
155 : "=r" (val), "=r" (ret)
156 : "0" (val), "r" (saddr), "r" (regs->isr)
157 : "r20", FIXUP_BRANCH_CLOBBER );
159 DPRINTF("val = 0x" RFMT "\n", val);
162 regs->gr[toreg] = val;
167 static int emulate_ldw(struct pt_regs *regs, int toreg, int flop)
169 unsigned long saddr = regs->ior;
170 unsigned long val = 0;
173 DPRINTF("load " RFMT ":" RFMT " to r%d for 4 bytes\n",
174 regs->isr, regs->ior, toreg);
176 __asm__ __volatile__ (
177 " zdep %3,28,2,%%r19\n" /* r19=(ofs&3)*8 */
179 " depw %%r0,31,2,%3\n"
180 "1: ldw 0(%%sr1,%3),%0\n"
181 "2: ldw 4(%%sr1,%3),%%r20\n"
182 " subi 32,%%r19,%%r19\n"
184 " vshd %0,%%r20,%0\n"
187 " .section .fixup,\"ax\"\n"
191 ASM_EXCEPTIONTABLE_ENTRY(1b, 4b)
192 ASM_EXCEPTIONTABLE_ENTRY(2b, 4b)
193 : "=r" (val), "=r" (ret)
194 : "0" (val), "r" (saddr), "r" (regs->isr)
195 : "r19", "r20", FIXUP_BRANCH_CLOBBER );
197 DPRINTF("val = 0x" RFMT "\n", val);
200 ((__u32*)(regs->fr))[toreg] = val;
202 regs->gr[toreg] = val;
206 static int emulate_ldd(struct pt_regs *regs, int toreg, int flop)
208 unsigned long saddr = regs->ior;
212 DPRINTF("load " RFMT ":" RFMT " to r%d for 8 bytes\n",
213 regs->isr, regs->ior, toreg);
220 __asm__ __volatile__ (
221 " depd,z %3,60,3,%%r19\n" /* r19=(ofs&7)*8 */
223 " depd %%r0,63,3,%3\n"
224 "1: ldd 0(%%sr1,%3),%0\n"
225 "2: ldd 8(%%sr1,%3),%%r20\n"
226 " subi 64,%%r19,%%r19\n"
228 " shrpd %0,%%r20,%%sar,%0\n"
231 " .section .fixup,\"ax\"\n"
235 ASM_EXCEPTIONTABLE_ENTRY(1b,4b)
236 ASM_EXCEPTIONTABLE_ENTRY(2b,4b)
237 : "=r" (val), "=r" (ret)
238 : "0" (val), "r" (saddr), "r" (regs->isr)
239 : "r19", "r20", FIXUP_BRANCH_CLOBBER );
242 unsigned long valh=0,vall=0;
243 __asm__ __volatile__ (
244 " zdep %5,29,2,%%r19\n" /* r19=(ofs&3)*8 */
246 " dep %%r0,31,2,%5\n"
247 "1: ldw 0(%%sr1,%5),%0\n"
248 "2: ldw 4(%%sr1,%5),%1\n"
249 "3: ldw 8(%%sr1,%5),%%r20\n"
250 " subi 32,%%r19,%%r19\n"
253 " vshd %1,%%r20,%1\n"
256 " .section .fixup,\"ax\"\n"
260 ASM_EXCEPTIONTABLE_ENTRY(1b,5b)
261 ASM_EXCEPTIONTABLE_ENTRY(2b,5b)
262 ASM_EXCEPTIONTABLE_ENTRY(3b,5b)
263 : "=r" (valh), "=r" (vall), "=r" (ret)
264 : "0" (valh), "1" (vall), "r" (saddr), "r" (regs->isr)
265 : "r19", "r20", FIXUP_BRANCH_CLOBBER );
266 val=((__u64)valh<<32)|(__u64)vall;
270 DPRINTF("val = 0x%llx\n", val);
273 regs->fr[toreg] = val;
275 regs->gr[toreg] = val;
280 static int emulate_sth(struct pt_regs *regs, int frreg)
282 unsigned long val = regs->gr[frreg];
288 DPRINTF("store r%d (0x" RFMT ") to " RFMT ":" RFMT " for 2 bytes\n", frreg,
289 val, regs->isr, regs->ior);
291 __asm__ __volatile__ (
293 " extrw,u %1, 23, 8, %%r19\n"
294 "1: stb %1, 1(%%sr1, %2)\n"
295 "2: stb %%r19, 0(%%sr1, %2)\n"
298 " .section .fixup,\"ax\"\n"
302 ASM_EXCEPTIONTABLE_ENTRY(1b,4b)
303 ASM_EXCEPTIONTABLE_ENTRY(2b,4b)
305 : "r" (val), "r" (regs->ior), "r" (regs->isr)
306 : "r19", FIXUP_BRANCH_CLOBBER );
311 static int emulate_stw(struct pt_regs *regs, int frreg, int flop)
317 val = ((__u32*)(regs->fr))[frreg];
319 val = regs->gr[frreg];
323 DPRINTF("store r%d (0x" RFMT ") to " RFMT ":" RFMT " for 4 bytes\n", frreg,
324 val, regs->isr, regs->ior);
327 __asm__ __volatile__ (
329 " zdep %2, 28, 2, %%r19\n"
330 " dep %%r0, 31, 2, %2\n"
332 " depwi,z -2, %%sar, 32, %%r19\n"
333 "1: ldw 0(%%sr1,%2),%%r20\n"
334 "2: ldw 4(%%sr1,%2),%%r21\n"
335 " vshd %%r0, %1, %%r22\n"
336 " vshd %1, %%r0, %%r1\n"
337 " and %%r20, %%r19, %%r20\n"
338 " andcm %%r21, %%r19, %%r21\n"
339 " or %%r22, %%r20, %%r20\n"
340 " or %%r1, %%r21, %%r21\n"
341 " stw %%r20,0(%%sr1,%2)\n"
342 " stw %%r21,4(%%sr1,%2)\n"
345 " .section .fixup,\"ax\"\n"
349 ASM_EXCEPTIONTABLE_ENTRY(1b,4b)
350 ASM_EXCEPTIONTABLE_ENTRY(2b,4b)
352 : "r" (val), "r" (regs->ior), "r" (regs->isr)
353 : "r19", "r20", "r21", "r22", "r1", FIXUP_BRANCH_CLOBBER );
357 static int emulate_std(struct pt_regs *regs, int frreg, int flop)
363 val = regs->fr[frreg];
365 val = regs->gr[frreg];
369 DPRINTF("store r%d (0x%016llx) to " RFMT ":" RFMT " for 8 bytes\n", frreg,
370 val, regs->isr, regs->ior);
377 __asm__ __volatile__ (
379 " depd,z %2, 60, 3, %%r19\n"
380 " depd %%r0, 63, 3, %2\n"
382 " depdi,z -2, %%sar, 64, %%r19\n"
383 "1: ldd 0(%%sr1,%2),%%r20\n"
384 "2: ldd 8(%%sr1,%2),%%r21\n"
385 " shrpd %%r0, %1, %%sar, %%r22\n"
386 " shrpd %1, %%r0, %%sar, %%r1\n"
387 " and %%r20, %%r19, %%r20\n"
388 " andcm %%r21, %%r19, %%r21\n"
389 " or %%r22, %%r20, %%r20\n"
390 " or %%r1, %%r21, %%r21\n"
391 "3: std %%r20,0(%%sr1,%2)\n"
392 "4: std %%r21,8(%%sr1,%2)\n"
395 " .section .fixup,\"ax\"\n"
399 ASM_EXCEPTIONTABLE_ENTRY(1b,6b)
400 ASM_EXCEPTIONTABLE_ENTRY(2b,6b)
401 ASM_EXCEPTIONTABLE_ENTRY(3b,6b)
402 ASM_EXCEPTIONTABLE_ENTRY(4b,6b)
404 : "r" (val), "r" (regs->ior), "r" (regs->isr)
405 : "r19", "r20", "r21", "r22", "r1", FIXUP_BRANCH_CLOBBER );
408 unsigned long valh=(val>>32),vall=(val&0xffffffffl);
409 __asm__ __volatile__ (
411 " zdep %2, 29, 2, %%r19\n"
412 " dep %%r0, 31, 2, %2\n"
414 " zvdepi -2, 32, %%r19\n"
415 "1: ldw 0(%%sr1,%3),%%r20\n"
416 "2: ldw 8(%%sr1,%3),%%r21\n"
417 " vshd %1, %2, %%r1\n"
418 " vshd %%r0, %1, %1\n"
419 " vshd %2, %%r0, %2\n"
420 " and %%r20, %%r19, %%r20\n"
421 " andcm %%r21, %%r19, %%r21\n"
422 " or %1, %%r20, %1\n"
423 " or %2, %%r21, %2\n"
424 "3: stw %1,0(%%sr1,%1)\n"
425 "4: stw %%r1,4(%%sr1,%3)\n"
426 "5: stw %2,8(%%sr1,%3)\n"
429 " .section .fixup,\"ax\"\n"
433 ASM_EXCEPTIONTABLE_ENTRY(1b,7b)
434 ASM_EXCEPTIONTABLE_ENTRY(2b,7b)
435 ASM_EXCEPTIONTABLE_ENTRY(3b,7b)
436 ASM_EXCEPTIONTABLE_ENTRY(4b,7b)
437 ASM_EXCEPTIONTABLE_ENTRY(5b,7b)
439 : "r" (valh), "r" (vall), "r" (regs->ior), "r" (regs->isr)
440 : "r19", "r20", "r21", "r1", FIXUP_BRANCH_CLOBBER );
447 void handle_unaligned(struct pt_regs *regs)
449 static unsigned long unaligned_count = 0;
450 static unsigned long last_time = 0;
451 unsigned long newbase = R1(regs->iir)?regs->gr[R1(regs->iir)]:0;
453 int ret = ERR_NOTHANDLED;
455 register int flop=0; /* true if this is a flop */
457 /* log a message with pacing */
458 if (user_mode(regs)) {
459 if (current->thread.flags & PARISC_UAC_SIGBUS) {
463 if (unaligned_count > 5 && jiffies - last_time > 5*HZ) {
468 if (!(current->thread.flags & PARISC_UAC_NOPRINT)
469 && ++unaligned_count < 5) {
471 sprintf(buf, "%s(%d): unaligned access to 0x" RFMT " at ip=0x" RFMT "\n",
472 current->comm, current->pid, regs->ior, regs->iaoq[0]);
473 printk(KERN_WARNING "%s", buf);
474 #ifdef DEBUG_UNALIGNED
479 if (!unaligned_enabled)
483 /* handle modification - OK, it's ugly, see the instruction manual */
484 switch (MAJOR_OP(regs->iir))
492 if (regs->iir&0x1000) /* short loads */
494 newbase += IM5_3(regs->iir);
496 newbase += IM5_2(regs->iir);
497 else if (regs->iir&0x2000) /* scaled indexed */
500 switch (regs->iir & OPCODE1_MASK)
510 newbase += (R2(regs->iir)?regs->gr[R2(regs->iir)]:0)<<shift;
511 } else /* simple indexed */
512 newbase += (R2(regs->iir)?regs->gr[R2(regs->iir)]:0);
518 newbase += IM14(regs->iir);
525 newbase += IM14(regs->iir&~0xe);
531 newbase += IM14(regs->iir&6);
538 newbase += IM14(regs->iir&~4);
543 /* TODO: make this cleaner... */
544 switch (regs->iir & OPCODE1_MASK)
548 ret = emulate_ldh(regs, R3(regs->iir));
555 ret = emulate_ldw(regs, R3(regs->iir),0);
559 ret = emulate_sth(regs, R2(regs->iir));
564 ret = emulate_stw(regs, R2(regs->iir),0);
572 ret = emulate_ldd(regs, R3(regs->iir),0);
577 ret = emulate_std(regs, R2(regs->iir),0);
586 ret = emulate_ldw(regs,FR3(regs->iir),1);
592 ret = emulate_ldd(regs,R3(regs->iir),1);
600 ret = emulate_stw(regs,FR3(regs->iir),1);
606 ret = emulate_std(regs,R3(regs->iir),1);
613 ret = ERR_NOTHANDLED; /* "undefined", but lets kill them. */
617 switch (regs->iir & OPCODE2_MASK)
621 ret = emulate_ldd(regs,R2(regs->iir),1);
625 ret = emulate_std(regs, R2(regs->iir),1);
630 ret = emulate_ldd(regs, R2(regs->iir),0);
633 ret = emulate_std(regs, R2(regs->iir),0);
638 switch (regs->iir & OPCODE3_MASK)
642 ret = emulate_ldw(regs, R2(regs->iir),0);
645 ret = emulate_ldw(regs, R2(regs->iir),1);
650 ret = emulate_stw(regs, R2(regs->iir),1);
653 ret = emulate_stw(regs, R2(regs->iir),0);
656 switch (regs->iir & OPCODE4_MASK)
659 ret = emulate_ldh(regs, R2(regs->iir));
663 ret = emulate_ldw(regs, R2(regs->iir),0);
666 ret = emulate_sth(regs, R2(regs->iir));
670 ret = emulate_stw(regs, R2(regs->iir),0);
674 if (modify && R1(regs->iir))
675 regs->gr[R1(regs->iir)] = newbase;
678 if (ret == ERR_NOTHANDLED)
679 printk(KERN_CRIT "Not-handled unaligned insn 0x%08lx\n", regs->iir);
681 DPRINTF("ret = %d\n", ret);
685 printk(KERN_CRIT "Unaligned handler failed, ret = %d\n", ret);
686 die_if_kernel("Unaligned data reference", regs, 28);
688 if (ret == ERR_PAGEFAULT)
690 si.si_signo = SIGSEGV;
692 si.si_code = SEGV_MAPERR;
693 si.si_addr = (void __user *)regs->ior;
694 force_sig_info(SIGSEGV, &si, current);
699 /* couldn't handle it ... */
700 si.si_signo = SIGBUS;
702 si.si_code = BUS_ADRALN;
703 si.si_addr = (void __user *)regs->ior;
704 force_sig_info(SIGBUS, &si, current);
710 /* else we handled it, let life go on. */
715 * NB: check_unaligned() is only used for PCXS processors right
716 * now, so we only check for PA1.1 encodings at this point.
720 check_unaligned(struct pt_regs *regs)
722 unsigned long align_mask;
724 /* Get alignment mask */
727 switch (regs->iir & OPCODE1_MASK) {
745 switch (regs->iir & OPCODE4_MASK) {
760 return (int)(regs->ior & align_mask);