2 * linux/arch/arm/mm/alignment.c
4 * Copyright (C) 1995 Linus Torvalds
5 * Modifications for ARM processor (c) 1995-2001 Russell King
6 * Thumb alignment fault fixups (c) 2004 MontaVista Software, Inc.
7 * - Adapted from gdb/sim/arm/thumbemu.c -- Thumb instruction emulation.
8 * Copyright (C) 1996, Cygnus Software Technologies Ltd.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
14 #include <linux/compiler.h>
15 #include <linux/kernel.h>
16 #include <linux/errno.h>
17 #include <linux/string.h>
18 #include <linux/proc_fs.h>
19 #include <linux/init.h>
20 #include <linux/uaccess.h>
22 #include <asm/unaligned.h>
27 * 32-bit misaligned trap handler (c) 1998 San Mehat (CCC) -July 1998
28 * /proc/sys/debug/alignment, modified and integrated into
29 * Linux 2.1 by Russell King
31 * Speed optimisations and better fault handling by Russell King.
34 * This code is not portable to processors with late data abort handling.
36 #define CODING_BITS(i) (i & 0x0e000000)
38 #define LDST_I_BIT(i) (i & (1 << 26)) /* Immediate constant */
39 #define LDST_P_BIT(i) (i & (1 << 24)) /* Preindex */
40 #define LDST_U_BIT(i) (i & (1 << 23)) /* Add offset */
41 #define LDST_W_BIT(i) (i & (1 << 21)) /* Writeback */
42 #define LDST_L_BIT(i) (i & (1 << 20)) /* Load */
44 #define LDST_P_EQ_U(i) ((((i) ^ ((i) >> 1)) & (1 << 23)) == 0)
46 #define LDSTHD_I_BIT(i) (i & (1 << 22)) /* double/half-word immed */
47 #define LDM_S_BIT(i) (i & (1 << 22)) /* write CPSR from SPSR */
49 #define RN_BITS(i) ((i >> 16) & 15) /* Rn */
50 #define RD_BITS(i) ((i >> 12) & 15) /* Rd */
51 #define RM_BITS(i) (i & 15) /* Rm */
53 #define REGMASK_BITS(i) (i & 0xffff)
54 #define OFFSET_BITS(i) (i & 0x0fff)
56 #define IS_SHIFT(i) (i & 0x0ff0)
57 #define SHIFT_BITS(i) ((i >> 7) & 0x1f)
58 #define SHIFT_TYPE(i) (i & 0x60)
59 #define SHIFT_LSL 0x00
60 #define SHIFT_LSR 0x20
61 #define SHIFT_ASR 0x40
62 #define SHIFT_RORRRX 0x60
64 static unsigned long ai_user;
65 static unsigned long ai_sys;
66 static unsigned long ai_skipped;
67 static unsigned long ai_half;
68 static unsigned long ai_word;
69 static unsigned long ai_dword;
70 static unsigned long ai_multi;
71 static int ai_usermode;
73 #define UM_WARN (1 << 0)
74 #define UM_FIXUP (1 << 1)
75 #define UM_SIGNAL (1 << 2)
78 static const char *usermode_action[] = {
88 proc_alignment_read(char *page, char **start, off_t off, int count, int *eof,
94 p += sprintf(p, "User:\t\t%lu\n", ai_user);
95 p += sprintf(p, "System:\t\t%lu\n", ai_sys);
96 p += sprintf(p, "Skipped:\t%lu\n", ai_skipped);
97 p += sprintf(p, "Half:\t\t%lu\n", ai_half);
98 p += sprintf(p, "Word:\t\t%lu\n", ai_word);
99 if (cpu_architecture() >= CPU_ARCH_ARMv5TE)
100 p += sprintf(p, "DWord:\t\t%lu\n", ai_dword);
101 p += sprintf(p, "Multi:\t\t%lu\n", ai_multi);
102 p += sprintf(p, "User faults:\t%i (%s)\n", ai_usermode,
103 usermode_action[ai_usermode]);
105 len = (p - page) - off;
109 *eof = (len <= count) ? 1 : 0;
115 static int proc_alignment_write(struct file *file, const char __user *buffer,
116 unsigned long count, void *data)
121 if (get_user(mode, buffer))
123 if (mode >= '0' && mode <= '5')
124 ai_usermode = mode - '0';
129 #endif /* CONFIG_PROC_FS */
143 #define FIRST_BYTE_16 "mov %1, %1, ror #8\n"
144 #define FIRST_BYTE_32 "mov %1, %1, ror #24\n"
145 #define NEXT_BYTE "ror #24"
148 #define FIRST_BYTE_16
149 #define FIRST_BYTE_32
150 #define NEXT_BYTE "lsr #8"
153 #define __get8_unaligned_check(ins,val,addr,err) \
155 "1: "ins" %1, [%2], #1\n" \
157 " .section .fixup,\"ax\"\n" \
162 " .section __ex_table,\"a\"\n" \
166 : "=r" (err), "=&r" (val), "=r" (addr) \
167 : "0" (err), "2" (addr))
169 #define __get16_unaligned_check(ins,val,addr) \
171 unsigned int err = 0, v, a = addr; \
172 __get8_unaligned_check(ins,v,a,err); \
173 val = v << ((BE) ? 8 : 0); \
174 __get8_unaligned_check(ins,v,a,err); \
175 val |= v << ((BE) ? 0 : 8); \
180 #define get16_unaligned_check(val,addr) \
181 __get16_unaligned_check("ldrb",val,addr)
183 #define get16t_unaligned_check(val,addr) \
184 __get16_unaligned_check("ldrbt",val,addr)
186 #define __get32_unaligned_check(ins,val,addr) \
188 unsigned int err = 0, v, a = addr; \
189 __get8_unaligned_check(ins,v,a,err); \
190 val = v << ((BE) ? 24 : 0); \
191 __get8_unaligned_check(ins,v,a,err); \
192 val |= v << ((BE) ? 16 : 8); \
193 __get8_unaligned_check(ins,v,a,err); \
194 val |= v << ((BE) ? 8 : 16); \
195 __get8_unaligned_check(ins,v,a,err); \
196 val |= v << ((BE) ? 0 : 24); \
201 #define get32_unaligned_check(val,addr) \
202 __get32_unaligned_check("ldrb",val,addr)
204 #define get32t_unaligned_check(val,addr) \
205 __get32_unaligned_check("ldrbt",val,addr)
207 #define __put16_unaligned_check(ins,val,addr) \
209 unsigned int err = 0, v = val, a = addr; \
210 __asm__( FIRST_BYTE_16 \
211 "1: "ins" %1, [%2], #1\n" \
212 " mov %1, %1, "NEXT_BYTE"\n" \
213 "2: "ins" %1, [%2]\n" \
215 " .section .fixup,\"ax\"\n" \
220 " .section __ex_table,\"a\"\n" \
225 : "=r" (err), "=&r" (v), "=&r" (a) \
226 : "0" (err), "1" (v), "2" (a)); \
231 #define put16_unaligned_check(val,addr) \
232 __put16_unaligned_check("strb",val,addr)
234 #define put16t_unaligned_check(val,addr) \
235 __put16_unaligned_check("strbt",val,addr)
237 #define __put32_unaligned_check(ins,val,addr) \
239 unsigned int err = 0, v = val, a = addr; \
240 __asm__( FIRST_BYTE_32 \
241 "1: "ins" %1, [%2], #1\n" \
242 " mov %1, %1, "NEXT_BYTE"\n" \
243 "2: "ins" %1, [%2], #1\n" \
244 " mov %1, %1, "NEXT_BYTE"\n" \
245 "3: "ins" %1, [%2], #1\n" \
246 " mov %1, %1, "NEXT_BYTE"\n" \
247 "4: "ins" %1, [%2]\n" \
249 " .section .fixup,\"ax\"\n" \
254 " .section __ex_table,\"a\"\n" \
261 : "=r" (err), "=&r" (v), "=&r" (a) \
262 : "0" (err), "1" (v), "2" (a)); \
267 #define put32_unaligned_check(val,addr) \
268 __put32_unaligned_check("strb", val, addr)
270 #define put32t_unaligned_check(val,addr) \
271 __put32_unaligned_check("strbt", val, addr)
274 do_alignment_finish_ldst(unsigned long addr, unsigned long instr, struct pt_regs *regs, union offset_union offset)
276 if (!LDST_U_BIT(instr))
277 offset.un = -offset.un;
279 if (!LDST_P_BIT(instr))
282 if (!LDST_P_BIT(instr) || LDST_W_BIT(instr))
283 regs->uregs[RN_BITS(instr)] = addr;
287 do_alignment_ldrhstrh(unsigned long addr, unsigned long instr, struct pt_regs *regs)
289 unsigned int rd = RD_BITS(instr);
296 if (LDST_L_BIT(instr)) {
298 get16_unaligned_check(val, addr);
300 /* signed half-word? */
302 val = (signed long)((signed short) val);
304 regs->uregs[rd] = val;
306 put16_unaligned_check(regs->uregs[rd], addr);
311 if (LDST_L_BIT(instr)) {
313 get16t_unaligned_check(val, addr);
315 /* signed half-word? */
317 val = (signed long)((signed short) val);
319 regs->uregs[rd] = val;
321 put16t_unaligned_check(regs->uregs[rd], addr);
330 do_alignment_ldrdstrd(unsigned long addr, unsigned long instr,
331 struct pt_regs *regs)
333 unsigned int rd = RD_BITS(instr);
335 if (((rd & 1) == 1) || (rd == 14))
343 if ((instr & 0xf0) == 0xd0) {
345 get32_unaligned_check(val, addr);
346 regs->uregs[rd] = val;
347 get32_unaligned_check(val, addr + 4);
348 regs->uregs[rd + 1] = val;
350 put32_unaligned_check(regs->uregs[rd], addr);
351 put32_unaligned_check(regs->uregs[rd + 1], addr + 4);
357 if ((instr & 0xf0) == 0xd0) {
359 get32t_unaligned_check(val, addr);
360 regs->uregs[rd] = val;
361 get32t_unaligned_check(val, addr + 4);
362 regs->uregs[rd + 1] = val;
364 put32t_unaligned_check(regs->uregs[rd], addr);
365 put32t_unaligned_check(regs->uregs[rd + 1], addr + 4);
376 do_alignment_ldrstr(unsigned long addr, unsigned long instr, struct pt_regs *regs)
378 unsigned int rd = RD_BITS(instr);
382 if ((!LDST_P_BIT(instr) && LDST_W_BIT(instr)) || user_mode(regs))
385 if (LDST_L_BIT(instr)) {
387 get32_unaligned_check(val, addr);
388 regs->uregs[rd] = val;
390 put32_unaligned_check(regs->uregs[rd], addr);
394 if (LDST_L_BIT(instr)) {
396 get32t_unaligned_check(val, addr);
397 regs->uregs[rd] = val;
399 put32t_unaligned_check(regs->uregs[rd], addr);
407 * LDM/STM alignment handler.
409 * There are 4 variants of this instruction:
411 * B = rn pointer before instruction, A = rn pointer after instruction
412 * ------ increasing address ----->
413 * | | r0 | r1 | ... | rx | |
420 do_alignment_ldmstm(unsigned long addr, unsigned long instr, struct pt_regs *regs)
422 unsigned int rd, rn, correction, nr_regs, regbits;
423 unsigned long eaddr, newaddr;
425 if (LDM_S_BIT(instr))
428 correction = 4; /* processor implementation defined */
429 regs->ARM_pc += correction;
433 /* count the number of registers in the mask to be transferred */
434 nr_regs = hweight16(REGMASK_BITS(instr)) * 4;
437 newaddr = eaddr = regs->uregs[rn];
439 if (!LDST_U_BIT(instr))
442 if (!LDST_U_BIT(instr))
445 if (LDST_P_EQ_U(instr)) /* U = P */
449 * For alignment faults on the ARM922T/ARM920T the MMU makes
450 * the FSR (and hence addr) equal to the updated base address
451 * of the multiple access rather than the restored value.
452 * Switch this message off if we've got a ARM92[02], otherwise
453 * [ls]dm alignment faults are noisy!
455 #if !(defined CONFIG_CPU_ARM922T) && !(defined CONFIG_CPU_ARM920T)
457 * This is a "hint" - we already have eaddr worked out by the
461 printk(KERN_ERR "LDMSTM: PC = %08lx, instr = %08lx, "
462 "addr = %08lx, eaddr = %08lx\n",
463 instruction_pointer(regs), instr, addr, eaddr);
468 if (user_mode(regs)) {
469 for (regbits = REGMASK_BITS(instr), rd = 0; regbits;
470 regbits >>= 1, rd += 1)
472 if (LDST_L_BIT(instr)) {
474 get32t_unaligned_check(val, eaddr);
475 regs->uregs[rd] = val;
477 put32t_unaligned_check(regs->uregs[rd], eaddr);
481 for (regbits = REGMASK_BITS(instr), rd = 0; regbits;
482 regbits >>= 1, rd += 1)
484 if (LDST_L_BIT(instr)) {
486 get32_unaligned_check(val, eaddr);
487 regs->uregs[rd] = val;
489 put32_unaligned_check(regs->uregs[rd], eaddr);
494 if (LDST_W_BIT(instr))
495 regs->uregs[rn] = newaddr;
496 if (!LDST_L_BIT(instr) || !(REGMASK_BITS(instr) & (1 << 15)))
497 regs->ARM_pc -= correction;
501 regs->ARM_pc -= correction;
505 printk(KERN_ERR "Alignment trap: not handling ldm with s-bit set\n");
510 * Convert Thumb ld/st instruction forms to equivalent ARM instructions so
511 * we can reuse ARM userland alignment fault fixups for Thumb.
513 * This implementation was initially based on the algorithm found in
514 * gdb/sim/arm/thumbemu.c. It is basically just a code reduction of same
515 * to convert only Thumb ld/st instruction forms to equivalent ARM forms.
518 * 1. Comments below refer to ARM ARM DDI0100E Thumb Instruction sections.
519 * 2. If for some reason we're passed an non-ld/st Thumb instruction to
520 * decode, we return 0xdeadc0de. This should never happen under normal
521 * circumstances but if it does, we've got other problems to deal with
522 * elsewhere and we obviously can't fix those problems here.
526 thumb2arm(u16 tinstr)
528 u32 L = (tinstr & (1<<11)) >> 11;
530 switch ((tinstr & 0xf800) >> 11) {
531 /* 6.5.1 Format 1: */
532 case 0x6000 >> 11: /* 7.1.52 STR(1) */
533 case 0x6800 >> 11: /* 7.1.26 LDR(1) */
534 case 0x7000 >> 11: /* 7.1.55 STRB(1) */
535 case 0x7800 >> 11: /* 7.1.30 LDRB(1) */
537 ((tinstr & (1<<12)) << (22-12)) | /* fixup */
538 (L<<20) | /* L==1? */
539 ((tinstr & (7<<0)) << (12-0)) | /* Rd */
540 ((tinstr & (7<<3)) << (16-3)) | /* Rn */
541 ((tinstr & (31<<6)) >> /* immed_5 */
542 (6 - ((tinstr & (1<<12)) ? 0 : 2)));
543 case 0x8000 >> 11: /* 7.1.57 STRH(1) */
544 case 0x8800 >> 11: /* 7.1.32 LDRH(1) */
546 (L<<20) | /* L==1? */
547 ((tinstr & (7<<0)) << (12-0)) | /* Rd */
548 ((tinstr & (7<<3)) << (16-3)) | /* Rn */
549 ((tinstr & (7<<6)) >> (6-1)) | /* immed_5[2:0] */
550 ((tinstr & (3<<9)) >> (9-8)); /* immed_5[4:3] */
552 /* 6.5.1 Format 2: */
556 static const u32 subset[8] = {
557 0xe7800000, /* 7.1.53 STR(2) */
558 0xe18000b0, /* 7.1.58 STRH(2) */
559 0xe7c00000, /* 7.1.56 STRB(2) */
560 0xe19000d0, /* 7.1.34 LDRSB */
561 0xe7900000, /* 7.1.27 LDR(2) */
562 0xe19000b0, /* 7.1.33 LDRH(2) */
563 0xe7d00000, /* 7.1.31 LDRB(2) */
564 0xe19000f0 /* 7.1.35 LDRSH */
566 return subset[(tinstr & (7<<9)) >> 9] |
567 ((tinstr & (7<<0)) << (12-0)) | /* Rd */
568 ((tinstr & (7<<3)) << (16-3)) | /* Rn */
569 ((tinstr & (7<<6)) >> (6-0)); /* Rm */
572 /* 6.5.1 Format 3: */
573 case 0x4800 >> 11: /* 7.1.28 LDR(3) */
574 /* NOTE: This case is not technically possible. We're
575 * loading 32-bit memory data via PC relative
576 * addressing mode. So we can and should eliminate
577 * this case. But I'll leave it here for now.
580 ((tinstr & (7<<8)) << (12-8)) | /* Rd */
581 ((tinstr & 255) << (2-0)); /* immed_8 */
583 /* 6.5.1 Format 4: */
584 case 0x9000 >> 11: /* 7.1.54 STR(3) */
585 case 0x9800 >> 11: /* 7.1.29 LDR(4) */
587 (L<<20) | /* L==1? */
588 ((tinstr & (7<<8)) << (12-8)) | /* Rd */
589 ((tinstr & 255) << 2); /* immed_8 */
591 /* 6.6.1 Format 1: */
592 case 0xc000 >> 11: /* 7.1.51 STMIA */
593 case 0xc800 >> 11: /* 7.1.25 LDMIA */
595 u32 Rn = (tinstr & (7<<8)) >> 8;
596 u32 W = ((L<<Rn) & (tinstr&255)) ? 0 : 1<<21;
598 return 0xe8800000 | W | (L<<20) | (Rn<<16) |
602 /* 6.6.1 Format 2: */
603 case 0xb000 >> 11: /* 7.1.48 PUSH */
604 case 0xb800 >> 11: /* 7.1.47 POP */
605 if ((tinstr & (3 << 9)) == 0x0400) {
606 static const u32 subset[4] = {
607 0xe92d0000, /* STMDB sp!,{registers} */
608 0xe92d4000, /* STMDB sp!,{registers,lr} */
609 0xe8bd0000, /* LDMIA sp!,{registers} */
610 0xe8bd8000 /* LDMIA sp!,{registers,pc} */
612 return subset[(L<<1) | ((tinstr & (1<<8)) >> 8)] |
613 (tinstr & 255); /* register_list */
615 /* Else fall through for illegal instruction case */
623 do_alignment(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
625 union offset_union offset;
626 unsigned long instr = 0, instrptr;
627 int (*handler)(unsigned long addr, unsigned long instr, struct pt_regs *regs);
633 instrptr = instruction_pointer(regs);
637 if (thumb_mode(regs)) {
638 fault = __get_user(tinstr, (u16 *)(instrptr & ~1));
640 instr = thumb2arm(tinstr);
642 fault = __get_user(instr, (u32 *)instrptr);
657 regs->ARM_pc += thumb_mode(regs) ? 2 : 4;
659 switch (CODING_BITS(instr)) {
660 case 0x00000000: /* 3.13.4 load/store instruction extensions */
661 if (LDSTHD_I_BIT(instr))
662 offset.un = (instr & 0xf00) >> 4 | (instr & 15);
664 offset.un = regs->uregs[RM_BITS(instr)];
666 if ((instr & 0x000000f0) == 0x000000b0 || /* LDRH, STRH */
667 (instr & 0x001000f0) == 0x001000f0) /* LDRSH */
668 handler = do_alignment_ldrhstrh;
669 else if ((instr & 0x001000f0) == 0x000000d0 || /* LDRD */
670 (instr & 0x001000f0) == 0x000000f0) /* STRD */
671 handler = do_alignment_ldrdstrd;
672 else if ((instr & 0x01f00ff0) == 0x01000090) /* SWP */
678 case 0x04000000: /* ldr or str immediate */
679 offset.un = OFFSET_BITS(instr);
680 handler = do_alignment_ldrstr;
683 case 0x06000000: /* ldr or str register */
684 offset.un = regs->uregs[RM_BITS(instr)];
686 if (IS_SHIFT(instr)) {
687 unsigned int shiftval = SHIFT_BITS(instr);
689 switch(SHIFT_TYPE(instr)) {
691 offset.un <<= shiftval;
695 offset.un >>= shiftval;
699 offset.sn >>= shiftval;
705 if (regs->ARM_cpsr & PSR_C_BIT)
706 offset.un |= 1 << 31;
708 offset.un = offset.un >> shiftval |
709 offset.un << (32 - shiftval);
713 handler = do_alignment_ldrstr;
716 case 0x08000000: /* ldm or stm */
717 handler = do_alignment_ldmstm;
724 type = handler(addr, instr, regs);
726 if (type == TYPE_ERROR || type == TYPE_FAULT)
729 if (type == TYPE_LDST)
730 do_alignment_finish_ldst(addr, instr, regs, offset);
735 if (type == TYPE_ERROR)
737 regs->ARM_pc -= thumb_mode(regs) ? 2 : 4;
739 * We got a fault - fix it up, or die.
741 do_bad_area(addr, fsr, regs);
745 printk(KERN_ERR "Alignment trap: not handling swp instruction\n");
749 * Oops, we didn't handle the instruction.
751 printk(KERN_ERR "Alignment trap: not handling instruction "
752 "%0*lx at [<%08lx>]\n",
753 thumb_mode(regs) ? 4 : 8,
754 thumb_mode(regs) ? tinstr : instr, instrptr);
761 if (ai_usermode & UM_WARN)
762 printk("Alignment trap: %s (%d) PC=0x%08lx Instr=0x%0*lx "
763 "Address=0x%08lx FSR 0x%03x\n", current->comm,
764 task_pid_nr(current), instrptr,
765 thumb_mode(regs) ? 4 : 8,
766 thumb_mode(regs) ? tinstr : instr,
769 if (ai_usermode & UM_FIXUP)
772 if (ai_usermode & UM_SIGNAL)
773 force_sig(SIGBUS, current);
775 set_cr(cr_no_alignment);
781 * This needs to be done after sysctl_init, otherwise sys/ will be
782 * overwritten. Actually, this shouldn't be in sys/ at all since
783 * it isn't a sysctl, and it doesn't contain sysctl information.
784 * We now locate it in /proc/cpu/alignment instead.
786 static int __init alignment_init(void)
788 #ifdef CONFIG_PROC_FS
789 struct proc_dir_entry *res;
791 res = proc_mkdir("cpu", NULL);
795 res = create_proc_entry("alignment", S_IWUSR | S_IRUGO, res);
799 res->read_proc = proc_alignment_read;
800 res->write_proc = proc_alignment_write;
804 * ARMv6 and later CPUs can perform unaligned accesses for
805 * most single load and store instructions up to word size.
806 * LDM, STM, LDRD and STRD still need to be handled.
808 * Ignoring the alignment fault is not an option on these
809 * CPUs since we spin re-faulting the instruction without
810 * making any progress.
812 if (cpu_architecture() >= CPU_ARCH_ARMv6 && (cr_alignment & CR_U)) {
813 cr_alignment &= ~CR_A;
814 cr_no_alignment &= ~CR_A;
815 set_cr(cr_alignment);
816 ai_usermode = UM_FIXUP;
819 hook_fault_code(1, do_alignment, SIGILL, "alignment exception");
820 hook_fault_code(3, do_alignment, SIGILL, "alignment exception");
825 fs_initcall(alignment_init);