1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * linux/arch/arm/mm/alignment.c
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * Copyright (C) 1995 Linus Torvalds
61da177e4SLinus Torvalds * Modifications for ARM processor (c) 1995-2001 Russell King
76cbdc8c5SSimon Arlott * Thumb alignment fault fixups (c) 2004 MontaVista Software, Inc.
81da177e4SLinus Torvalds * - Adapted from gdb/sim/arm/thumbemu.c -- Thumb instruction emulation.
91da177e4SLinus Torvalds * Copyright (C) 1996, Cygnus Software Technologies Ltd.
101da177e4SLinus Torvalds */
11d944d549SRussell King #include <linux/moduleparam.h>
121da177e4SLinus Torvalds #include <linux/compiler.h>
131da177e4SLinus Torvalds #include <linux/kernel.h>
14b17b0153SIngo Molnar #include <linux/sched/debug.h>
151da177e4SLinus Torvalds #include <linux/errno.h>
161da177e4SLinus Torvalds #include <linux/string.h>
171da177e4SLinus Torvalds #include <linux/proc_fs.h>
18b7072c63SAlexey Dobriyan #include <linux/seq_file.h>
191da177e4SLinus Torvalds #include <linux/init.h>
203f07c014SIngo Molnar #include <linux/sched/signal.h>
2133fa9b13SRussell King #include <linux/uaccess.h>
221da177e4SLinus Torvalds
2315d07dc9SRussell King #include <asm/cp15.h>
249f97da78SDavid Howells #include <asm/system_info.h>
251da177e4SLinus Torvalds #include <asm/unaligned.h>
268592edf0SBen Dooks #include <asm/opcodes.h>
271da177e4SLinus Torvalds
281da177e4SLinus Torvalds #include "fault.h"
29b4b20ad8SRussell King #include "mm.h"
301da177e4SLinus Torvalds
311da177e4SLinus Torvalds /*
321da177e4SLinus Torvalds * 32-bit misaligned trap handler (c) 1998 San Mehat (CCC) -July 1998
331da177e4SLinus Torvalds * /proc/sys/debug/alignment, modified and integrated into
341da177e4SLinus Torvalds * Linux 2.1 by Russell King
351da177e4SLinus Torvalds *
361da177e4SLinus Torvalds * Speed optimisations and better fault handling by Russell King.
371da177e4SLinus Torvalds *
381da177e4SLinus Torvalds * *** NOTE ***
391da177e4SLinus Torvalds * This code is not portable to processors with late data abort handling.
401da177e4SLinus Torvalds */
411da177e4SLinus Torvalds #define CODING_BITS(i) (i & 0x0e000000)
425ca918e5SRobin Murphy #define COND_BITS(i) (i & 0xf0000000)
431da177e4SLinus Torvalds
441da177e4SLinus Torvalds #define LDST_I_BIT(i) (i & (1 << 26)) /* Immediate constant */
451da177e4SLinus Torvalds #define LDST_P_BIT(i) (i & (1 << 24)) /* Preindex */
461da177e4SLinus Torvalds #define LDST_U_BIT(i) (i & (1 << 23)) /* Add offset */
471da177e4SLinus Torvalds #define LDST_W_BIT(i) (i & (1 << 21)) /* Writeback */
481da177e4SLinus Torvalds #define LDST_L_BIT(i) (i & (1 << 20)) /* Load */
491da177e4SLinus Torvalds
501da177e4SLinus Torvalds #define LDST_P_EQ_U(i) ((((i) ^ ((i) >> 1)) & (1 << 23)) == 0)
511da177e4SLinus Torvalds
52f21ee2d4SSteve Longerbeam #define LDSTHD_I_BIT(i) (i & (1 << 22)) /* double/half-word immed */
531da177e4SLinus Torvalds #define LDM_S_BIT(i) (i & (1 << 22)) /* write CPSR from SPSR */
541da177e4SLinus Torvalds
551da177e4SLinus Torvalds #define RN_BITS(i) ((i >> 16) & 15) /* Rn */
561da177e4SLinus Torvalds #define RD_BITS(i) ((i >> 12) & 15) /* Rd */
571da177e4SLinus Torvalds #define RM_BITS(i) (i & 15) /* Rm */
581da177e4SLinus Torvalds
591da177e4SLinus Torvalds #define REGMASK_BITS(i) (i & 0xffff)
601da177e4SLinus Torvalds #define OFFSET_BITS(i) (i & 0x0fff)
611da177e4SLinus Torvalds
621da177e4SLinus Torvalds #define IS_SHIFT(i) (i & 0x0ff0)
631da177e4SLinus Torvalds #define SHIFT_BITS(i) ((i >> 7) & 0x1f)
641da177e4SLinus Torvalds #define SHIFT_TYPE(i) (i & 0x60)
651da177e4SLinus Torvalds #define SHIFT_LSL 0x00
661da177e4SLinus Torvalds #define SHIFT_LSR 0x20
671da177e4SLinus Torvalds #define SHIFT_ASR 0x40
681da177e4SLinus Torvalds #define SHIFT_RORRRX 0x60
691da177e4SLinus Torvalds
70c2860d43SGeorge G. Davis #define BAD_INSTR 0xdeadc0de
71c2860d43SGeorge G. Davis
72c2860d43SGeorge G. Davis /* Thumb-2 32 bit format per ARMv7 DDI0406A A6.3, either f800h,e800h,f800h */
73c2860d43SGeorge G. Davis #define IS_T32(hi16) \
74c2860d43SGeorge G. Davis (((hi16) & 0xe000) == 0xe000 && ((hi16) & 0x1800))
75c2860d43SGeorge G. Davis
761da177e4SLinus Torvalds static unsigned long ai_user;
771da177e4SLinus Torvalds static unsigned long ai_sys;
781e7e3211SRussell King static void *ai_sys_last_pc;
791da177e4SLinus Torvalds static unsigned long ai_skipped;
801da177e4SLinus Torvalds static unsigned long ai_half;
811da177e4SLinus Torvalds static unsigned long ai_word;
82f21ee2d4SSteve Longerbeam static unsigned long ai_dword;
831da177e4SLinus Torvalds static unsigned long ai_multi;
841da177e4SLinus Torvalds static int ai_usermode;
850aeb3408SRussell King static unsigned long cr_no_alignment;
861da177e4SLinus Torvalds
87d944d549SRussell King core_param(alignment, ai_usermode, int, 0600);
88d944d549SRussell King
89baa745a3SRussell King #define UM_WARN (1 << 0)
90baa745a3SRussell King #define UM_FIXUP (1 << 1)
91baa745a3SRussell King #define UM_SIGNAL (1 << 2)
92baa745a3SRussell King
93088c01f1SDave Martin /* Return true if and only if the ARMv6 unaligned access model is in use. */
cpu_is_v6_unaligned(void)94088c01f1SDave Martin static bool cpu_is_v6_unaligned(void)
95088c01f1SDave Martin {
964585eaffSRussell King return cpu_architecture() >= CPU_ARCH_ARMv6 && get_cr() & CR_U;
97088c01f1SDave Martin }
98088c01f1SDave Martin
safe_usermode(int new_usermode,bool warn)99088c01f1SDave Martin static int safe_usermode(int new_usermode, bool warn)
100088c01f1SDave Martin {
101088c01f1SDave Martin /*
102088c01f1SDave Martin * ARMv6 and later CPUs can perform unaligned accesses for
103088c01f1SDave Martin * most single load and store instructions up to word size.
104088c01f1SDave Martin * LDM, STM, LDRD and STRD still need to be handled.
105088c01f1SDave Martin *
106088c01f1SDave Martin * Ignoring the alignment fault is not an option on these
107088c01f1SDave Martin * CPUs since we spin re-faulting the instruction without
108088c01f1SDave Martin * making any progress.
109088c01f1SDave Martin */
110088c01f1SDave Martin if (cpu_is_v6_unaligned() && !(new_usermode & (UM_FIXUP | UM_SIGNAL))) {
111088c01f1SDave Martin new_usermode |= UM_FIXUP;
112088c01f1SDave Martin
113088c01f1SDave Martin if (warn)
1144ed89f22SRussell King pr_warn("alignment: ignoring faults is unsafe on this CPU. Defaulting to fixup mode.\n");
115088c01f1SDave Martin }
116088c01f1SDave Martin
117088c01f1SDave Martin return new_usermode;
118088c01f1SDave Martin }
119088c01f1SDave Martin
120ffc660c5SArnd Bergmann #ifdef CONFIG_PROC_FS
121ffc660c5SArnd Bergmann static const char *usermode_action[] = {
122ffc660c5SArnd Bergmann "ignored",
123ffc660c5SArnd Bergmann "warn",
124ffc660c5SArnd Bergmann "fixup",
125ffc660c5SArnd Bergmann "fixup+warn",
126ffc660c5SArnd Bergmann "signal",
127ffc660c5SArnd Bergmann "signal+warn"
128ffc660c5SArnd Bergmann };
129ffc660c5SArnd Bergmann
alignment_proc_show(struct seq_file * m,void * v)130b7072c63SAlexey Dobriyan static int alignment_proc_show(struct seq_file *m, void *v)
1311da177e4SLinus Torvalds {
132b7072c63SAlexey Dobriyan seq_printf(m, "User:\t\t%lu\n", ai_user);
133d75f773cSSakari Ailus seq_printf(m, "System:\t\t%lu (%pS)\n", ai_sys, ai_sys_last_pc);
134b7072c63SAlexey Dobriyan seq_printf(m, "Skipped:\t%lu\n", ai_skipped);
135b7072c63SAlexey Dobriyan seq_printf(m, "Half:\t\t%lu\n", ai_half);
136b7072c63SAlexey Dobriyan seq_printf(m, "Word:\t\t%lu\n", ai_word);
137f21ee2d4SSteve Longerbeam if (cpu_architecture() >= CPU_ARCH_ARMv5TE)
138b7072c63SAlexey Dobriyan seq_printf(m, "DWord:\t\t%lu\n", ai_dword);
139b7072c63SAlexey Dobriyan seq_printf(m, "Multi:\t\t%lu\n", ai_multi);
140b7072c63SAlexey Dobriyan seq_printf(m, "User faults:\t%i (%s)\n", ai_usermode,
1411da177e4SLinus Torvalds usermode_action[ai_usermode]);
1421da177e4SLinus Torvalds
143b7072c63SAlexey Dobriyan return 0;
1441da177e4SLinus Torvalds }
1451da177e4SLinus Torvalds
alignment_proc_open(struct inode * inode,struct file * file)146b7072c63SAlexey Dobriyan static int alignment_proc_open(struct inode *inode, struct file *file)
147b7072c63SAlexey Dobriyan {
148b7072c63SAlexey Dobriyan return single_open(file, alignment_proc_show, NULL);
149b7072c63SAlexey Dobriyan }
150b7072c63SAlexey Dobriyan
alignment_proc_write(struct file * file,const char __user * buffer,size_t count,loff_t * pos)151b7072c63SAlexey Dobriyan static ssize_t alignment_proc_write(struct file *file, const char __user *buffer,
152b7072c63SAlexey Dobriyan size_t count, loff_t *pos)
1531da177e4SLinus Torvalds {
1541da177e4SLinus Torvalds char mode;
1551da177e4SLinus Torvalds
1561da177e4SLinus Torvalds if (count > 0) {
1571da177e4SLinus Torvalds if (get_user(mode, buffer))
1581da177e4SLinus Torvalds return -EFAULT;
1591da177e4SLinus Torvalds if (mode >= '0' && mode <= '5')
160088c01f1SDave Martin ai_usermode = safe_usermode(mode - '0', true);
1611da177e4SLinus Torvalds }
1621da177e4SLinus Torvalds return count;
1631da177e4SLinus Torvalds }
1641da177e4SLinus Torvalds
16597a32539SAlexey Dobriyan static const struct proc_ops alignment_proc_ops = {
16697a32539SAlexey Dobriyan .proc_open = alignment_proc_open,
16797a32539SAlexey Dobriyan .proc_read = seq_read,
16897a32539SAlexey Dobriyan .proc_lseek = seq_lseek,
16997a32539SAlexey Dobriyan .proc_release = single_release,
17097a32539SAlexey Dobriyan .proc_write = alignment_proc_write,
171b7072c63SAlexey Dobriyan };
1721da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
1731da177e4SLinus Torvalds
1741da177e4SLinus Torvalds union offset_union {
1751da177e4SLinus Torvalds unsigned long un;
1761da177e4SLinus Torvalds signed long sn;
1771da177e4SLinus Torvalds };
1781da177e4SLinus Torvalds
1791da177e4SLinus Torvalds #define TYPE_ERROR 0
1801da177e4SLinus Torvalds #define TYPE_FAULT 1
1811da177e4SLinus Torvalds #define TYPE_LDST 2
1821da177e4SLinus Torvalds #define TYPE_DONE 3
1831da177e4SLinus Torvalds
1841da177e4SLinus Torvalds #ifdef __ARMEB__
1851da177e4SLinus Torvalds #define BE 1
1861da177e4SLinus Torvalds #define FIRST_BYTE_16 "mov %1, %1, ror #8\n"
1871da177e4SLinus Torvalds #define FIRST_BYTE_32 "mov %1, %1, ror #24\n"
1881da177e4SLinus Torvalds #define NEXT_BYTE "ror #24"
1891da177e4SLinus Torvalds #else
1901da177e4SLinus Torvalds #define BE 0
1911da177e4SLinus Torvalds #define FIRST_BYTE_16
1921da177e4SLinus Torvalds #define FIRST_BYTE_32
1931da177e4SLinus Torvalds #define NEXT_BYTE "lsr #8"
1941da177e4SLinus Torvalds #endif
1951da177e4SLinus Torvalds
1961da177e4SLinus Torvalds #define __get8_unaligned_check(ins,val,addr,err) \
1971da177e4SLinus Torvalds __asm__( \
198347c8b70SCatalin Marinas ARM( "1: "ins" %1, [%2], #1\n" ) \
199347c8b70SCatalin Marinas THUMB( "1: "ins" %1, [%2]\n" ) \
200347c8b70SCatalin Marinas THUMB( " add %2, %2, #1\n" ) \
2011da177e4SLinus Torvalds "2:\n" \
202c4a84ae3SArd Biesheuvel " .pushsection .text.fixup,\"ax\"\n" \
2031da177e4SLinus Torvalds " .align 2\n" \
2041da177e4SLinus Torvalds "3: mov %0, #1\n" \
2051da177e4SLinus Torvalds " b 2b\n" \
2064260415fSRussell King " .popsection\n" \
2074260415fSRussell King " .pushsection __ex_table,\"a\"\n" \
2081da177e4SLinus Torvalds " .align 3\n" \
2091da177e4SLinus Torvalds " .long 1b, 3b\n" \
2104260415fSRussell King " .popsection\n" \
2111da177e4SLinus Torvalds : "=r" (err), "=&r" (val), "=r" (addr) \
2121da177e4SLinus Torvalds : "0" (err), "2" (addr))
2131da177e4SLinus Torvalds
2141da177e4SLinus Torvalds #define __get16_unaligned_check(ins,val,addr) \
2151da177e4SLinus Torvalds do { \
2161da177e4SLinus Torvalds unsigned int err = 0, v, a = addr; \
2171da177e4SLinus Torvalds __get8_unaligned_check(ins,v,a,err); \
2181da177e4SLinus Torvalds val = v << ((BE) ? 8 : 0); \
2191da177e4SLinus Torvalds __get8_unaligned_check(ins,v,a,err); \
2201da177e4SLinus Torvalds val |= v << ((BE) ? 0 : 8); \
2211da177e4SLinus Torvalds if (err) \
2221da177e4SLinus Torvalds goto fault; \
2231da177e4SLinus Torvalds } while (0)
2241da177e4SLinus Torvalds
2251da177e4SLinus Torvalds #define get16_unaligned_check(val,addr) \
2261da177e4SLinus Torvalds __get16_unaligned_check("ldrb",val,addr)
2271da177e4SLinus Torvalds
2281da177e4SLinus Torvalds #define get16t_unaligned_check(val,addr) \
2291da177e4SLinus Torvalds __get16_unaligned_check("ldrbt",val,addr)
2301da177e4SLinus Torvalds
2311da177e4SLinus Torvalds #define __get32_unaligned_check(ins,val,addr) \
2321da177e4SLinus Torvalds do { \
2331da177e4SLinus Torvalds unsigned int err = 0, v, a = addr; \
2341da177e4SLinus Torvalds __get8_unaligned_check(ins,v,a,err); \
2351da177e4SLinus Torvalds val = v << ((BE) ? 24 : 0); \
2361da177e4SLinus Torvalds __get8_unaligned_check(ins,v,a,err); \
2371da177e4SLinus Torvalds val |= v << ((BE) ? 16 : 8); \
2381da177e4SLinus Torvalds __get8_unaligned_check(ins,v,a,err); \
2391da177e4SLinus Torvalds val |= v << ((BE) ? 8 : 16); \
2401da177e4SLinus Torvalds __get8_unaligned_check(ins,v,a,err); \
2411da177e4SLinus Torvalds val |= v << ((BE) ? 0 : 24); \
2421da177e4SLinus Torvalds if (err) \
2431da177e4SLinus Torvalds goto fault; \
2441da177e4SLinus Torvalds } while (0)
2451da177e4SLinus Torvalds
2461da177e4SLinus Torvalds #define get32_unaligned_check(val,addr) \
2471da177e4SLinus Torvalds __get32_unaligned_check("ldrb",val,addr)
2481da177e4SLinus Torvalds
2491da177e4SLinus Torvalds #define get32t_unaligned_check(val,addr) \
2501da177e4SLinus Torvalds __get32_unaligned_check("ldrbt",val,addr)
2511da177e4SLinus Torvalds
2521da177e4SLinus Torvalds #define __put16_unaligned_check(ins,val,addr) \
2531da177e4SLinus Torvalds do { \
2541da177e4SLinus Torvalds unsigned int err = 0, v = val, a = addr; \
2551da177e4SLinus Torvalds __asm__( FIRST_BYTE_16 \
256347c8b70SCatalin Marinas ARM( "1: "ins" %1, [%2], #1\n" ) \
257347c8b70SCatalin Marinas THUMB( "1: "ins" %1, [%2]\n" ) \
258347c8b70SCatalin Marinas THUMB( " add %2, %2, #1\n" ) \
2591da177e4SLinus Torvalds " mov %1, %1, "NEXT_BYTE"\n" \
2601da177e4SLinus Torvalds "2: "ins" %1, [%2]\n" \
2611da177e4SLinus Torvalds "3:\n" \
262c4a84ae3SArd Biesheuvel " .pushsection .text.fixup,\"ax\"\n" \
2631da177e4SLinus Torvalds " .align 2\n" \
2641da177e4SLinus Torvalds "4: mov %0, #1\n" \
2651da177e4SLinus Torvalds " b 3b\n" \
2664260415fSRussell King " .popsection\n" \
2674260415fSRussell King " .pushsection __ex_table,\"a\"\n" \
2681da177e4SLinus Torvalds " .align 3\n" \
2691da177e4SLinus Torvalds " .long 1b, 4b\n" \
2701da177e4SLinus Torvalds " .long 2b, 4b\n" \
2714260415fSRussell King " .popsection\n" \
2721da177e4SLinus Torvalds : "=r" (err), "=&r" (v), "=&r" (a) \
2731da177e4SLinus Torvalds : "0" (err), "1" (v), "2" (a)); \
2741da177e4SLinus Torvalds if (err) \
2751da177e4SLinus Torvalds goto fault; \
2761da177e4SLinus Torvalds } while (0)
2771da177e4SLinus Torvalds
2781da177e4SLinus Torvalds #define put16_unaligned_check(val,addr) \
2791da177e4SLinus Torvalds __put16_unaligned_check("strb",val,addr)
2801da177e4SLinus Torvalds
2811da177e4SLinus Torvalds #define put16t_unaligned_check(val,addr) \
2821da177e4SLinus Torvalds __put16_unaligned_check("strbt",val,addr)
2831da177e4SLinus Torvalds
2841da177e4SLinus Torvalds #define __put32_unaligned_check(ins,val,addr) \
2851da177e4SLinus Torvalds do { \
2861da177e4SLinus Torvalds unsigned int err = 0, v = val, a = addr; \
2871da177e4SLinus Torvalds __asm__( FIRST_BYTE_32 \
288347c8b70SCatalin Marinas ARM( "1: "ins" %1, [%2], #1\n" ) \
289347c8b70SCatalin Marinas THUMB( "1: "ins" %1, [%2]\n" ) \
290347c8b70SCatalin Marinas THUMB( " add %2, %2, #1\n" ) \
2911da177e4SLinus Torvalds " mov %1, %1, "NEXT_BYTE"\n" \
292347c8b70SCatalin Marinas ARM( "2: "ins" %1, [%2], #1\n" ) \
293347c8b70SCatalin Marinas THUMB( "2: "ins" %1, [%2]\n" ) \
294347c8b70SCatalin Marinas THUMB( " add %2, %2, #1\n" ) \
2951da177e4SLinus Torvalds " mov %1, %1, "NEXT_BYTE"\n" \
296347c8b70SCatalin Marinas ARM( "3: "ins" %1, [%2], #1\n" ) \
297347c8b70SCatalin Marinas THUMB( "3: "ins" %1, [%2]\n" ) \
298347c8b70SCatalin Marinas THUMB( " add %2, %2, #1\n" ) \
2991da177e4SLinus Torvalds " mov %1, %1, "NEXT_BYTE"\n" \
3001da177e4SLinus Torvalds "4: "ins" %1, [%2]\n" \
3011da177e4SLinus Torvalds "5:\n" \
302c4a84ae3SArd Biesheuvel " .pushsection .text.fixup,\"ax\"\n" \
3031da177e4SLinus Torvalds " .align 2\n" \
3041da177e4SLinus Torvalds "6: mov %0, #1\n" \
3051da177e4SLinus Torvalds " b 5b\n" \
3064260415fSRussell King " .popsection\n" \
3074260415fSRussell King " .pushsection __ex_table,\"a\"\n" \
3081da177e4SLinus Torvalds " .align 3\n" \
3091da177e4SLinus Torvalds " .long 1b, 6b\n" \
3101da177e4SLinus Torvalds " .long 2b, 6b\n" \
3111da177e4SLinus Torvalds " .long 3b, 6b\n" \
3121da177e4SLinus Torvalds " .long 4b, 6b\n" \
3134260415fSRussell King " .popsection\n" \
3141da177e4SLinus Torvalds : "=r" (err), "=&r" (v), "=&r" (a) \
3151da177e4SLinus Torvalds : "0" (err), "1" (v), "2" (a)); \
3161da177e4SLinus Torvalds if (err) \
3171da177e4SLinus Torvalds goto fault; \
3181da177e4SLinus Torvalds } while (0)
3191da177e4SLinus Torvalds
3201da177e4SLinus Torvalds #define put32_unaligned_check(val,addr) \
3211da177e4SLinus Torvalds __put32_unaligned_check("strb", val, addr)
3221da177e4SLinus Torvalds
3231da177e4SLinus Torvalds #define put32t_unaligned_check(val,addr) \
3241da177e4SLinus Torvalds __put32_unaligned_check("strbt", val, addr)
3251da177e4SLinus Torvalds
3261da177e4SLinus Torvalds static void
do_alignment_finish_ldst(unsigned long addr,u32 instr,struct pt_regs * regs,union offset_union offset)3271bb9fb0aSRussell King do_alignment_finish_ldst(unsigned long addr, u32 instr, struct pt_regs *regs, union offset_union offset)
3281da177e4SLinus Torvalds {
3291da177e4SLinus Torvalds if (!LDST_U_BIT(instr))
3301da177e4SLinus Torvalds offset.un = -offset.un;
3311da177e4SLinus Torvalds
3321da177e4SLinus Torvalds if (!LDST_P_BIT(instr))
3331da177e4SLinus Torvalds addr += offset.un;
3341da177e4SLinus Torvalds
3351da177e4SLinus Torvalds if (!LDST_P_BIT(instr) || LDST_W_BIT(instr))
3361da177e4SLinus Torvalds regs->uregs[RN_BITS(instr)] = addr;
3371da177e4SLinus Torvalds }
3381da177e4SLinus Torvalds
3391da177e4SLinus Torvalds static int
do_alignment_ldrhstrh(unsigned long addr,u32 instr,struct pt_regs * regs)3401bb9fb0aSRussell King do_alignment_ldrhstrh(unsigned long addr, u32 instr, struct pt_regs *regs)
3411da177e4SLinus Torvalds {
3421da177e4SLinus Torvalds unsigned int rd = RD_BITS(instr);
3431da177e4SLinus Torvalds
3441da177e4SLinus Torvalds ai_half += 1;
3451da177e4SLinus Torvalds
3461da177e4SLinus Torvalds if (user_mode(regs))
3471da177e4SLinus Torvalds goto user;
3481da177e4SLinus Torvalds
3491da177e4SLinus Torvalds if (LDST_L_BIT(instr)) {
3501da177e4SLinus Torvalds unsigned long val;
3511da177e4SLinus Torvalds get16_unaligned_check(val, addr);
3521da177e4SLinus Torvalds
3531da177e4SLinus Torvalds /* signed half-word? */
3541da177e4SLinus Torvalds if (instr & 0x40)
3551da177e4SLinus Torvalds val = (signed long)((signed short) val);
3561da177e4SLinus Torvalds
3571da177e4SLinus Torvalds regs->uregs[rd] = val;
3581da177e4SLinus Torvalds } else
3591da177e4SLinus Torvalds put16_unaligned_check(regs->uregs[rd], addr);
3601da177e4SLinus Torvalds
3611da177e4SLinus Torvalds return TYPE_LDST;
3621da177e4SLinus Torvalds
3631da177e4SLinus Torvalds user:
3641da177e4SLinus Torvalds if (LDST_L_BIT(instr)) {
3651da177e4SLinus Torvalds unsigned long val;
366274e91b8SRussell King unsigned int __ua_flags = uaccess_save_and_enable();
367274e91b8SRussell King
3681da177e4SLinus Torvalds get16t_unaligned_check(val, addr);
369274e91b8SRussell King uaccess_restore(__ua_flags);
3701da177e4SLinus Torvalds
3711da177e4SLinus Torvalds /* signed half-word? */
3721da177e4SLinus Torvalds if (instr & 0x40)
3731da177e4SLinus Torvalds val = (signed long)((signed short) val);
3741da177e4SLinus Torvalds
3751da177e4SLinus Torvalds regs->uregs[rd] = val;
376274e91b8SRussell King } else {
377274e91b8SRussell King unsigned int __ua_flags = uaccess_save_and_enable();
3781da177e4SLinus Torvalds put16t_unaligned_check(regs->uregs[rd], addr);
379274e91b8SRussell King uaccess_restore(__ua_flags);
380274e91b8SRussell King }
3811da177e4SLinus Torvalds
3821da177e4SLinus Torvalds return TYPE_LDST;
3831da177e4SLinus Torvalds
384f21ee2d4SSteve Longerbeam fault:
385f21ee2d4SSteve Longerbeam return TYPE_FAULT;
386f21ee2d4SSteve Longerbeam }
387f21ee2d4SSteve Longerbeam
388f21ee2d4SSteve Longerbeam static int
do_alignment_ldrdstrd(unsigned long addr,u32 instr,struct pt_regs * regs)3891bb9fb0aSRussell King do_alignment_ldrdstrd(unsigned long addr, u32 instr, struct pt_regs *regs)
390f21ee2d4SSteve Longerbeam {
391f21ee2d4SSteve Longerbeam unsigned int rd = RD_BITS(instr);
392c2860d43SGeorge G. Davis unsigned int rd2;
393c2860d43SGeorge G. Davis int load;
394f21ee2d4SSteve Longerbeam
395c2860d43SGeorge G. Davis if ((instr & 0xfe000000) == 0xe8000000) {
396c2860d43SGeorge G. Davis /* ARMv7 Thumb-2 32-bit LDRD/STRD */
397c2860d43SGeorge G. Davis rd2 = (instr >> 8) & 0xf;
398c2860d43SGeorge G. Davis load = !!(LDST_L_BIT(instr));
399c2860d43SGeorge G. Davis } else if (((rd & 1) == 1) || (rd == 14))
40019da83f6SGeorge G. Davis goto bad;
401c2860d43SGeorge G. Davis else {
402c2860d43SGeorge G. Davis load = ((instr & 0xf0) == 0xd0);
403c2860d43SGeorge G. Davis rd2 = rd + 1;
404c2860d43SGeorge G. Davis }
40519da83f6SGeorge G. Davis
406f21ee2d4SSteve Longerbeam ai_dword += 1;
407f21ee2d4SSteve Longerbeam
408f21ee2d4SSteve Longerbeam if (user_mode(regs))
409f21ee2d4SSteve Longerbeam goto user;
410f21ee2d4SSteve Longerbeam
411c2860d43SGeorge G. Davis if (load) {
412f21ee2d4SSteve Longerbeam unsigned long val;
413f21ee2d4SSteve Longerbeam get32_unaligned_check(val, addr);
414f21ee2d4SSteve Longerbeam regs->uregs[rd] = val;
415f21ee2d4SSteve Longerbeam get32_unaligned_check(val, addr + 4);
416c2860d43SGeorge G. Davis regs->uregs[rd2] = val;
417f21ee2d4SSteve Longerbeam } else {
418f21ee2d4SSteve Longerbeam put32_unaligned_check(regs->uregs[rd], addr);
419c2860d43SGeorge G. Davis put32_unaligned_check(regs->uregs[rd2], addr + 4);
420f21ee2d4SSteve Longerbeam }
421f21ee2d4SSteve Longerbeam
422f21ee2d4SSteve Longerbeam return TYPE_LDST;
423f21ee2d4SSteve Longerbeam
424f21ee2d4SSteve Longerbeam user:
425c2860d43SGeorge G. Davis if (load) {
426274e91b8SRussell King unsigned long val, val2;
427274e91b8SRussell King unsigned int __ua_flags = uaccess_save_and_enable();
428274e91b8SRussell King
429f21ee2d4SSteve Longerbeam get32t_unaligned_check(val, addr);
430274e91b8SRussell King get32t_unaligned_check(val2, addr + 4);
431274e91b8SRussell King
432274e91b8SRussell King uaccess_restore(__ua_flags);
433274e91b8SRussell King
434f21ee2d4SSteve Longerbeam regs->uregs[rd] = val;
435274e91b8SRussell King regs->uregs[rd2] = val2;
436f21ee2d4SSteve Longerbeam } else {
437274e91b8SRussell King unsigned int __ua_flags = uaccess_save_and_enable();
438f21ee2d4SSteve Longerbeam put32t_unaligned_check(regs->uregs[rd], addr);
439c2860d43SGeorge G. Davis put32t_unaligned_check(regs->uregs[rd2], addr + 4);
440274e91b8SRussell King uaccess_restore(__ua_flags);
441f21ee2d4SSteve Longerbeam }
442f21ee2d4SSteve Longerbeam
443f21ee2d4SSteve Longerbeam return TYPE_LDST;
44419da83f6SGeorge G. Davis bad:
44519da83f6SGeorge G. Davis return TYPE_ERROR;
4461da177e4SLinus Torvalds fault:
4471da177e4SLinus Torvalds return TYPE_FAULT;
4481da177e4SLinus Torvalds }
4491da177e4SLinus Torvalds
4501da177e4SLinus Torvalds static int
do_alignment_ldrstr(unsigned long addr,u32 instr,struct pt_regs * regs)4511bb9fb0aSRussell King do_alignment_ldrstr(unsigned long addr, u32 instr, struct pt_regs *regs)
4521da177e4SLinus Torvalds {
4531da177e4SLinus Torvalds unsigned int rd = RD_BITS(instr);
4541da177e4SLinus Torvalds
4551da177e4SLinus Torvalds ai_word += 1;
4561da177e4SLinus Torvalds
4571da177e4SLinus Torvalds if ((!LDST_P_BIT(instr) && LDST_W_BIT(instr)) || user_mode(regs))
4581da177e4SLinus Torvalds goto trans;
4591da177e4SLinus Torvalds
4601da177e4SLinus Torvalds if (LDST_L_BIT(instr)) {
4611da177e4SLinus Torvalds unsigned int val;
4621da177e4SLinus Torvalds get32_unaligned_check(val, addr);
4631da177e4SLinus Torvalds regs->uregs[rd] = val;
4641da177e4SLinus Torvalds } else
4651da177e4SLinus Torvalds put32_unaligned_check(regs->uregs[rd], addr);
4661da177e4SLinus Torvalds return TYPE_LDST;
4671da177e4SLinus Torvalds
4681da177e4SLinus Torvalds trans:
4691da177e4SLinus Torvalds if (LDST_L_BIT(instr)) {
4701da177e4SLinus Torvalds unsigned int val;
471274e91b8SRussell King unsigned int __ua_flags = uaccess_save_and_enable();
4721da177e4SLinus Torvalds get32t_unaligned_check(val, addr);
473274e91b8SRussell King uaccess_restore(__ua_flags);
4741da177e4SLinus Torvalds regs->uregs[rd] = val;
475274e91b8SRussell King } else {
476274e91b8SRussell King unsigned int __ua_flags = uaccess_save_and_enable();
4771da177e4SLinus Torvalds put32t_unaligned_check(regs->uregs[rd], addr);
478274e91b8SRussell King uaccess_restore(__ua_flags);
479274e91b8SRussell King }
4801da177e4SLinus Torvalds return TYPE_LDST;
4811da177e4SLinus Torvalds
4821da177e4SLinus Torvalds fault:
4831da177e4SLinus Torvalds return TYPE_FAULT;
4841da177e4SLinus Torvalds }
4851da177e4SLinus Torvalds
4861da177e4SLinus Torvalds /*
4871da177e4SLinus Torvalds * LDM/STM alignment handler.
4881da177e4SLinus Torvalds *
4891da177e4SLinus Torvalds * There are 4 variants of this instruction:
4901da177e4SLinus Torvalds *
4911da177e4SLinus Torvalds * B = rn pointer before instruction, A = rn pointer after instruction
4921da177e4SLinus Torvalds * ------ increasing address ----->
4931da177e4SLinus Torvalds * | | r0 | r1 | ... | rx | |
4941da177e4SLinus Torvalds * PU = 01 B A
4951da177e4SLinus Torvalds * PU = 11 B A
4961da177e4SLinus Torvalds * PU = 00 A B
4971da177e4SLinus Torvalds * PU = 10 A B
4981da177e4SLinus Torvalds */
4991da177e4SLinus Torvalds static int
do_alignment_ldmstm(unsigned long addr,u32 instr,struct pt_regs * regs)5001bb9fb0aSRussell King do_alignment_ldmstm(unsigned long addr, u32 instr, struct pt_regs *regs)
5011da177e4SLinus Torvalds {
5021da177e4SLinus Torvalds unsigned int rd, rn, correction, nr_regs, regbits;
5031da177e4SLinus Torvalds unsigned long eaddr, newaddr;
5041da177e4SLinus Torvalds
5051da177e4SLinus Torvalds if (LDM_S_BIT(instr))
5061da177e4SLinus Torvalds goto bad;
5071da177e4SLinus Torvalds
5081da177e4SLinus Torvalds correction = 4; /* processor implementation defined */
5091da177e4SLinus Torvalds regs->ARM_pc += correction;
5101da177e4SLinus Torvalds
5111da177e4SLinus Torvalds ai_multi += 1;
5121da177e4SLinus Torvalds
5131da177e4SLinus Torvalds /* count the number of registers in the mask to be transferred */
5141da177e4SLinus Torvalds nr_regs = hweight16(REGMASK_BITS(instr)) * 4;
5151da177e4SLinus Torvalds
5161da177e4SLinus Torvalds rn = RN_BITS(instr);
5171da177e4SLinus Torvalds newaddr = eaddr = regs->uregs[rn];
5181da177e4SLinus Torvalds
5191da177e4SLinus Torvalds if (!LDST_U_BIT(instr))
5201da177e4SLinus Torvalds nr_regs = -nr_regs;
5211da177e4SLinus Torvalds newaddr += nr_regs;
5221da177e4SLinus Torvalds if (!LDST_U_BIT(instr))
5231da177e4SLinus Torvalds eaddr = newaddr;
5241da177e4SLinus Torvalds
5251da177e4SLinus Torvalds if (LDST_P_EQ_U(instr)) /* U = P */
5261da177e4SLinus Torvalds eaddr += 4;
5271da177e4SLinus Torvalds
5281da177e4SLinus Torvalds /*
5291da177e4SLinus Torvalds * For alignment faults on the ARM922T/ARM920T the MMU makes
5301da177e4SLinus Torvalds * the FSR (and hence addr) equal to the updated base address
5311da177e4SLinus Torvalds * of the multiple access rather than the restored value.
5321da177e4SLinus Torvalds * Switch this message off if we've got a ARM92[02], otherwise
5331da177e4SLinus Torvalds * [ls]dm alignment faults are noisy!
5341da177e4SLinus Torvalds */
5351da177e4SLinus Torvalds #if !(defined CONFIG_CPU_ARM922T) && !(defined CONFIG_CPU_ARM920T)
5361da177e4SLinus Torvalds /*
5371da177e4SLinus Torvalds * This is a "hint" - we already have eaddr worked out by the
5381da177e4SLinus Torvalds * processor for us.
5391da177e4SLinus Torvalds */
5401da177e4SLinus Torvalds if (addr != eaddr) {
5411bb9fb0aSRussell King pr_err("LDMSTM: PC = %08lx, instr = %08x, "
5421da177e4SLinus Torvalds "addr = %08lx, eaddr = %08lx\n",
5431da177e4SLinus Torvalds instruction_pointer(regs), instr, addr, eaddr);
5441da177e4SLinus Torvalds show_regs(regs);
5451da177e4SLinus Torvalds }
5461da177e4SLinus Torvalds #endif
5471da177e4SLinus Torvalds
5481da177e4SLinus Torvalds if (user_mode(regs)) {
549274e91b8SRussell King unsigned int __ua_flags = uaccess_save_and_enable();
5501da177e4SLinus Torvalds for (regbits = REGMASK_BITS(instr), rd = 0; regbits;
5511da177e4SLinus Torvalds regbits >>= 1, rd += 1)
5521da177e4SLinus Torvalds if (regbits & 1) {
5531da177e4SLinus Torvalds if (LDST_L_BIT(instr)) {
5541da177e4SLinus Torvalds unsigned int val;
5551da177e4SLinus Torvalds get32t_unaligned_check(val, eaddr);
5561da177e4SLinus Torvalds regs->uregs[rd] = val;
5571da177e4SLinus Torvalds } else
5581da177e4SLinus Torvalds put32t_unaligned_check(regs->uregs[rd], eaddr);
5591da177e4SLinus Torvalds eaddr += 4;
5601da177e4SLinus Torvalds }
561274e91b8SRussell King uaccess_restore(__ua_flags);
5621da177e4SLinus Torvalds } else {
5631da177e4SLinus Torvalds for (regbits = REGMASK_BITS(instr), rd = 0; regbits;
5641da177e4SLinus Torvalds regbits >>= 1, rd += 1)
5651da177e4SLinus Torvalds if (regbits & 1) {
5661da177e4SLinus Torvalds if (LDST_L_BIT(instr)) {
5671da177e4SLinus Torvalds unsigned int val;
5681da177e4SLinus Torvalds get32_unaligned_check(val, eaddr);
5691da177e4SLinus Torvalds regs->uregs[rd] = val;
5701da177e4SLinus Torvalds } else
5711da177e4SLinus Torvalds put32_unaligned_check(regs->uregs[rd], eaddr);
5721da177e4SLinus Torvalds eaddr += 4;
5731da177e4SLinus Torvalds }
5741da177e4SLinus Torvalds }
5751da177e4SLinus Torvalds
5761da177e4SLinus Torvalds if (LDST_W_BIT(instr))
5771da177e4SLinus Torvalds regs->uregs[rn] = newaddr;
5781da177e4SLinus Torvalds if (!LDST_L_BIT(instr) || !(REGMASK_BITS(instr) & (1 << 15)))
5791da177e4SLinus Torvalds regs->ARM_pc -= correction;
5801da177e4SLinus Torvalds return TYPE_DONE;
5811da177e4SLinus Torvalds
5821da177e4SLinus Torvalds fault:
5831da177e4SLinus Torvalds regs->ARM_pc -= correction;
5841da177e4SLinus Torvalds return TYPE_FAULT;
5851da177e4SLinus Torvalds
5861da177e4SLinus Torvalds bad:
5874ed89f22SRussell King pr_err("Alignment trap: not handling ldm with s-bit set\n");
5881da177e4SLinus Torvalds return TYPE_ERROR;
5891da177e4SLinus Torvalds }
5901da177e4SLinus Torvalds
5911da177e4SLinus Torvalds /*
5921da177e4SLinus Torvalds * Convert Thumb ld/st instruction forms to equivalent ARM instructions so
5931da177e4SLinus Torvalds * we can reuse ARM userland alignment fault fixups for Thumb.
5941da177e4SLinus Torvalds *
5951da177e4SLinus Torvalds * This implementation was initially based on the algorithm found in
5961da177e4SLinus Torvalds * gdb/sim/arm/thumbemu.c. It is basically just a code reduction of same
5971da177e4SLinus Torvalds * to convert only Thumb ld/st instruction forms to equivalent ARM forms.
5981da177e4SLinus Torvalds *
5991da177e4SLinus Torvalds * NOTES:
6001da177e4SLinus Torvalds * 1. Comments below refer to ARM ARM DDI0100E Thumb Instruction sections.
6011da177e4SLinus Torvalds * 2. If for some reason we're passed an non-ld/st Thumb instruction to
6021da177e4SLinus Torvalds * decode, we return 0xdeadc0de. This should never happen under normal
6031da177e4SLinus Torvalds * circumstances but if it does, we've got other problems to deal with
6041da177e4SLinus Torvalds * elsewhere and we obviously can't fix those problems here.
6051da177e4SLinus Torvalds */
6061da177e4SLinus Torvalds
6071da177e4SLinus Torvalds static unsigned long
thumb2arm(u16 tinstr)6081da177e4SLinus Torvalds thumb2arm(u16 tinstr)
6091da177e4SLinus Torvalds {
6101da177e4SLinus Torvalds u32 L = (tinstr & (1<<11)) >> 11;
6111da177e4SLinus Torvalds
6121da177e4SLinus Torvalds switch ((tinstr & 0xf800) >> 11) {
6131da177e4SLinus Torvalds /* 6.5.1 Format 1: */
6141da177e4SLinus Torvalds case 0x6000 >> 11: /* 7.1.52 STR(1) */
6151da177e4SLinus Torvalds case 0x6800 >> 11: /* 7.1.26 LDR(1) */
6161da177e4SLinus Torvalds case 0x7000 >> 11: /* 7.1.55 STRB(1) */
6171da177e4SLinus Torvalds case 0x7800 >> 11: /* 7.1.30 LDRB(1) */
6181da177e4SLinus Torvalds return 0xe5800000 |
6191da177e4SLinus Torvalds ((tinstr & (1<<12)) << (22-12)) | /* fixup */
6201da177e4SLinus Torvalds (L<<20) | /* L==1? */
6211da177e4SLinus Torvalds ((tinstr & (7<<0)) << (12-0)) | /* Rd */
6221da177e4SLinus Torvalds ((tinstr & (7<<3)) << (16-3)) | /* Rn */
6231da177e4SLinus Torvalds ((tinstr & (31<<6)) >> /* immed_5 */
6241da177e4SLinus Torvalds (6 - ((tinstr & (1<<12)) ? 0 : 2)));
6251da177e4SLinus Torvalds case 0x8000 >> 11: /* 7.1.57 STRH(1) */
6261da177e4SLinus Torvalds case 0x8800 >> 11: /* 7.1.32 LDRH(1) */
6271da177e4SLinus Torvalds return 0xe1c000b0 |
6281da177e4SLinus Torvalds (L<<20) | /* L==1? */
6291da177e4SLinus Torvalds ((tinstr & (7<<0)) << (12-0)) | /* Rd */
6301da177e4SLinus Torvalds ((tinstr & (7<<3)) << (16-3)) | /* Rn */
6311da177e4SLinus Torvalds ((tinstr & (7<<6)) >> (6-1)) | /* immed_5[2:0] */
6321da177e4SLinus Torvalds ((tinstr & (3<<9)) >> (9-8)); /* immed_5[4:3] */
6331da177e4SLinus Torvalds
6341da177e4SLinus Torvalds /* 6.5.1 Format 2: */
6351da177e4SLinus Torvalds case 0x5000 >> 11:
6361da177e4SLinus Torvalds case 0x5800 >> 11:
6371da177e4SLinus Torvalds {
6381da177e4SLinus Torvalds static const u32 subset[8] = {
6391da177e4SLinus Torvalds 0xe7800000, /* 7.1.53 STR(2) */
6401da177e4SLinus Torvalds 0xe18000b0, /* 7.1.58 STRH(2) */
6411da177e4SLinus Torvalds 0xe7c00000, /* 7.1.56 STRB(2) */
6421da177e4SLinus Torvalds 0xe19000d0, /* 7.1.34 LDRSB */
6431da177e4SLinus Torvalds 0xe7900000, /* 7.1.27 LDR(2) */
6441da177e4SLinus Torvalds 0xe19000b0, /* 7.1.33 LDRH(2) */
6451da177e4SLinus Torvalds 0xe7d00000, /* 7.1.31 LDRB(2) */
6461da177e4SLinus Torvalds 0xe19000f0 /* 7.1.35 LDRSH */
6471da177e4SLinus Torvalds };
6481da177e4SLinus Torvalds return subset[(tinstr & (7<<9)) >> 9] |
6491da177e4SLinus Torvalds ((tinstr & (7<<0)) << (12-0)) | /* Rd */
6501da177e4SLinus Torvalds ((tinstr & (7<<3)) << (16-3)) | /* Rn */
6511da177e4SLinus Torvalds ((tinstr & (7<<6)) >> (6-0)); /* Rm */
6521da177e4SLinus Torvalds }
6531da177e4SLinus Torvalds
6541da177e4SLinus Torvalds /* 6.5.1 Format 3: */
6551da177e4SLinus Torvalds case 0x4800 >> 11: /* 7.1.28 LDR(3) */
6561da177e4SLinus Torvalds /* NOTE: This case is not technically possible. We're
6571da177e4SLinus Torvalds * loading 32-bit memory data via PC relative
6581da177e4SLinus Torvalds * addressing mode. So we can and should eliminate
6591da177e4SLinus Torvalds * this case. But I'll leave it here for now.
6601da177e4SLinus Torvalds */
6611da177e4SLinus Torvalds return 0xe59f0000 |
6621da177e4SLinus Torvalds ((tinstr & (7<<8)) << (12-8)) | /* Rd */
6631da177e4SLinus Torvalds ((tinstr & 255) << (2-0)); /* immed_8 */
6641da177e4SLinus Torvalds
6651da177e4SLinus Torvalds /* 6.5.1 Format 4: */
6661da177e4SLinus Torvalds case 0x9000 >> 11: /* 7.1.54 STR(3) */
6671da177e4SLinus Torvalds case 0x9800 >> 11: /* 7.1.29 LDR(4) */
6681da177e4SLinus Torvalds return 0xe58d0000 |
6691da177e4SLinus Torvalds (L<<20) | /* L==1? */
6701da177e4SLinus Torvalds ((tinstr & (7<<8)) << (12-8)) | /* Rd */
6711da177e4SLinus Torvalds ((tinstr & 255) << 2); /* immed_8 */
6721da177e4SLinus Torvalds
6731da177e4SLinus Torvalds /* 6.6.1 Format 1: */
6741da177e4SLinus Torvalds case 0xc000 >> 11: /* 7.1.51 STMIA */
6751da177e4SLinus Torvalds case 0xc800 >> 11: /* 7.1.25 LDMIA */
6761da177e4SLinus Torvalds {
6771da177e4SLinus Torvalds u32 Rn = (tinstr & (7<<8)) >> 8;
6781da177e4SLinus Torvalds u32 W = ((L<<Rn) & (tinstr&255)) ? 0 : 1<<21;
6791da177e4SLinus Torvalds
6801da177e4SLinus Torvalds return 0xe8800000 | W | (L<<20) | (Rn<<16) |
6811da177e4SLinus Torvalds (tinstr&255);
6821da177e4SLinus Torvalds }
6831da177e4SLinus Torvalds
6841da177e4SLinus Torvalds /* 6.6.1 Format 2: */
6851da177e4SLinus Torvalds case 0xb000 >> 11: /* 7.1.48 PUSH */
6861da177e4SLinus Torvalds case 0xb800 >> 11: /* 7.1.47 POP */
6871da177e4SLinus Torvalds if ((tinstr & (3 << 9)) == 0x0400) {
6881da177e4SLinus Torvalds static const u32 subset[4] = {
6891da177e4SLinus Torvalds 0xe92d0000, /* STMDB sp!,{registers} */
6901da177e4SLinus Torvalds 0xe92d4000, /* STMDB sp!,{registers,lr} */
6911da177e4SLinus Torvalds 0xe8bd0000, /* LDMIA sp!,{registers} */
6921da177e4SLinus Torvalds 0xe8bd8000 /* LDMIA sp!,{registers,pc} */
6931da177e4SLinus Torvalds };
6941da177e4SLinus Torvalds return subset[(L<<1) | ((tinstr & (1<<8)) >> 8)] |
6951da177e4SLinus Torvalds (tinstr & 255); /* register_list */
6961da177e4SLinus Torvalds }
697df561f66SGustavo A. R. Silva fallthrough; /* for illegal instruction case */
6981da177e4SLinus Torvalds
6991da177e4SLinus Torvalds default:
700c2860d43SGeorge G. Davis return BAD_INSTR;
7011da177e4SLinus Torvalds }
7021da177e4SLinus Torvalds }
7031da177e4SLinus Torvalds
704c2860d43SGeorge G. Davis /*
705c2860d43SGeorge G. Davis * Convert Thumb-2 32 bit LDM, STM, LDRD, STRD to equivalent instruction
706c2860d43SGeorge G. Davis * handlable by ARM alignment handler, also find the corresponding handler,
707c2860d43SGeorge G. Davis * so that we can reuse ARM userland alignment fault fixups for Thumb.
708c2860d43SGeorge G. Davis *
709c2860d43SGeorge G. Davis * @pinstr: original Thumb-2 instruction; returns new handlable instruction
710c2860d43SGeorge G. Davis * @regs: register context.
711c2860d43SGeorge G. Davis * @poffset: return offset from faulted addr for later writeback
712c2860d43SGeorge G. Davis *
713c2860d43SGeorge G. Davis * NOTES:
714c2860d43SGeorge G. Davis * 1. Comments below refer to ARMv7 DDI0406A Thumb Instruction sections.
715c2860d43SGeorge G. Davis * 2. Register name Rt from ARMv7 is same as Rd from ARMv6 (Rd is Rt)
716c2860d43SGeorge G. Davis */
717c2860d43SGeorge G. Davis static void *
do_alignment_t32_to_handler(u32 * pinstr,struct pt_regs * regs,union offset_union * poffset)7181bb9fb0aSRussell King do_alignment_t32_to_handler(u32 *pinstr, struct pt_regs *regs,
719c2860d43SGeorge G. Davis union offset_union *poffset)
720c2860d43SGeorge G. Davis {
7211bb9fb0aSRussell King u32 instr = *pinstr;
722c2860d43SGeorge G. Davis u16 tinst1 = (instr >> 16) & 0xffff;
723c2860d43SGeorge G. Davis u16 tinst2 = instr & 0xffff;
724c2860d43SGeorge G. Davis
725c2860d43SGeorge G. Davis switch (tinst1 & 0xffe0) {
726c2860d43SGeorge G. Davis /* A6.3.5 Load/Store multiple */
727c2860d43SGeorge G. Davis case 0xe880: /* STM/STMIA/STMEA,LDM/LDMIA, PUSH/POP T2 */
728c2860d43SGeorge G. Davis case 0xe8a0: /* ...above writeback version */
729c2860d43SGeorge G. Davis case 0xe900: /* STMDB/STMFD, LDMDB/LDMEA */
730c2860d43SGeorge G. Davis case 0xe920: /* ...above writeback version */
731c2860d43SGeorge G. Davis /* no need offset decision since handler calculates it */
732c2860d43SGeorge G. Davis return do_alignment_ldmstm;
733c2860d43SGeorge G. Davis
734c2860d43SGeorge G. Davis case 0xf840: /* POP/PUSH T3 (single register) */
735c2860d43SGeorge G. Davis if (RN_BITS(instr) == 13 && (tinst2 & 0x09ff) == 0x0904) {
736c2860d43SGeorge G. Davis u32 L = !!(LDST_L_BIT(instr));
737c2860d43SGeorge G. Davis const u32 subset[2] = {
738c2860d43SGeorge G. Davis 0xe92d0000, /* STMDB sp!,{registers} */
739c2860d43SGeorge G. Davis 0xe8bd0000, /* LDMIA sp!,{registers} */
740c2860d43SGeorge G. Davis };
741c2860d43SGeorge G. Davis *pinstr = subset[L] | (1<<RD_BITS(instr));
742c2860d43SGeorge G. Davis return do_alignment_ldmstm;
743c2860d43SGeorge G. Davis }
744c2860d43SGeorge G. Davis /* Else fall through for illegal instruction case */
745c2860d43SGeorge G. Davis break;
746c2860d43SGeorge G. Davis
747c2860d43SGeorge G. Davis /* A6.3.6 Load/store double, STRD/LDRD(immed, lit, reg) */
748c2860d43SGeorge G. Davis case 0xe860:
749c2860d43SGeorge G. Davis case 0xe960:
750c2860d43SGeorge G. Davis case 0xe8e0:
751c2860d43SGeorge G. Davis case 0xe9e0:
752c2860d43SGeorge G. Davis poffset->un = (tinst2 & 0xff) << 2;
753df561f66SGustavo A. R. Silva fallthrough;
754e7c0c9f6SGustavo A. R. Silva
755c2860d43SGeorge G. Davis case 0xe940:
756c2860d43SGeorge G. Davis case 0xe9c0:
757c2860d43SGeorge G. Davis return do_alignment_ldrdstrd;
758c2860d43SGeorge G. Davis
759c2860d43SGeorge G. Davis /*
760c2860d43SGeorge G. Davis * No need to handle load/store instructions up to word size
761c2860d43SGeorge G. Davis * since ARMv6 and later CPUs can perform unaligned accesses.
762c2860d43SGeorge G. Davis */
763c2860d43SGeorge G. Davis default:
764c2860d43SGeorge G. Davis break;
765c2860d43SGeorge G. Davis }
766c2860d43SGeorge G. Davis return NULL;
767c2860d43SGeorge G. Davis }
768c2860d43SGeorge G. Davis
alignment_get_arm(struct pt_regs * regs,u32 * ip,u32 * inst)7691bb9fb0aSRussell King static int alignment_get_arm(struct pt_regs *regs, u32 *ip, u32 *inst)
77067e15fa5SRussell King {
77167e15fa5SRussell King u32 instr = 0;
77267e15fa5SRussell King int fault;
77367e15fa5SRussell King
77467e15fa5SRussell King if (user_mode(regs))
77567e15fa5SRussell King fault = get_user(instr, ip);
77667e15fa5SRussell King else
77725f12ae4SChristoph Hellwig fault = get_kernel_nofault(instr, ip);
77867e15fa5SRussell King
77967e15fa5SRussell King *inst = __mem_to_opcode_arm(instr);
78067e15fa5SRussell King
78167e15fa5SRussell King return fault;
78267e15fa5SRussell King }
78367e15fa5SRussell King
alignment_get_thumb(struct pt_regs * regs,u16 * ip,u16 * inst)78467e15fa5SRussell King static int alignment_get_thumb(struct pt_regs *regs, u16 *ip, u16 *inst)
78567e15fa5SRussell King {
78667e15fa5SRussell King u16 instr = 0;
78767e15fa5SRussell King int fault;
78867e15fa5SRussell King
78967e15fa5SRussell King if (user_mode(regs))
79067e15fa5SRussell King fault = get_user(instr, ip);
79167e15fa5SRussell King else
79225f12ae4SChristoph Hellwig fault = get_kernel_nofault(instr, ip);
79367e15fa5SRussell King
79467e15fa5SRussell King *inst = __mem_to_opcode_thumb16(instr);
79567e15fa5SRussell King
79667e15fa5SRussell King return fault;
79767e15fa5SRussell King }
79867e15fa5SRussell King
7991da177e4SLinus Torvalds static int
do_alignment(unsigned long addr,unsigned int fsr,struct pt_regs * regs)8001da177e4SLinus Torvalds do_alignment(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
8011da177e4SLinus Torvalds {
8023f649ab7SKees Cook union offset_union offset;
8031bb9fb0aSRussell King unsigned long instrptr;
8041bb9fb0aSRussell King int (*handler)(unsigned long addr, u32 instr, struct pt_regs *regs);
8051da177e4SLinus Torvalds unsigned int type;
8061bb9fb0aSRussell King u32 instr = 0;
8071da177e4SLinus Torvalds u16 tinstr = 0;
808c2860d43SGeorge G. Davis int isize = 4;
809c2860d43SGeorge G. Davis int thumb2_32b = 0;
81067e15fa5SRussell King int fault;
8111da177e4SLinus Torvalds
81202fe2845SRussell King if (interrupts_enabled(regs))
81302fe2845SRussell King local_irq_enable();
81402fe2845SRussell King
8151da177e4SLinus Torvalds instrptr = instruction_pointer(regs);
8161da177e4SLinus Torvalds
817f8343685SYoann Padioleau if (thumb_mode(regs)) {
818b255188fSRussell King u16 *ptr = (u16 *)(instrptr & ~1);
81967e15fa5SRussell King
82067e15fa5SRussell King fault = alignment_get_thumb(regs, ptr, &tinstr);
821c2860d43SGeorge G. Davis if (!fault) {
822c2860d43SGeorge G. Davis if (cpu_architecture() >= CPU_ARCH_ARMv7 &&
823c2860d43SGeorge G. Davis IS_T32(tinstr)) {
824c2860d43SGeorge G. Davis /* Thumb-2 32-bit */
82567e15fa5SRussell King u16 tinst2;
82667e15fa5SRussell King fault = alignment_get_thumb(regs, ptr + 1, &tinst2);
8278592edf0SBen Dooks instr = __opcode_thumb32_compose(tinstr, tinst2);
828c2860d43SGeorge G. Davis thumb2_32b = 1;
829c2860d43SGeorge G. Davis } else {
830c2860d43SGeorge G. Davis isize = 2;
8311da177e4SLinus Torvalds instr = thumb2arm(tinstr);
832c2860d43SGeorge G. Davis }
833c2860d43SGeorge G. Davis }
8348592edf0SBen Dooks } else {
83567e15fa5SRussell King fault = alignment_get_arm(regs, (void *)instrptr, &instr);
8368592edf0SBen Dooks }
8371da177e4SLinus Torvalds
8381da177e4SLinus Torvalds if (fault) {
8391da177e4SLinus Torvalds type = TYPE_FAULT;
8401da177e4SLinus Torvalds goto bad_or_fault;
8411da177e4SLinus Torvalds }
8421da177e4SLinus Torvalds
8431da177e4SLinus Torvalds if (user_mode(regs))
8441da177e4SLinus Torvalds goto user;
8451da177e4SLinus Torvalds
8461da177e4SLinus Torvalds ai_sys += 1;
8471e7e3211SRussell King ai_sys_last_pc = (void *)instruction_pointer(regs);
8481da177e4SLinus Torvalds
8491da177e4SLinus Torvalds fixup:
8501da177e4SLinus Torvalds
851c2860d43SGeorge G. Davis regs->ARM_pc += isize;
8521da177e4SLinus Torvalds
8531da177e4SLinus Torvalds switch (CODING_BITS(instr)) {
854f21ee2d4SSteve Longerbeam case 0x00000000: /* 3.13.4 load/store instruction extensions */
855f21ee2d4SSteve Longerbeam if (LDSTHD_I_BIT(instr))
8561da177e4SLinus Torvalds offset.un = (instr & 0xf00) >> 4 | (instr & 15);
8571da177e4SLinus Torvalds else
8581da177e4SLinus Torvalds offset.un = regs->uregs[RM_BITS(instr)];
859f21ee2d4SSteve Longerbeam
860f21ee2d4SSteve Longerbeam if ((instr & 0x000000f0) == 0x000000b0 || /* LDRH, STRH */
861f21ee2d4SSteve Longerbeam (instr & 0x001000f0) == 0x001000f0) /* LDRSH */
8621da177e4SLinus Torvalds handler = do_alignment_ldrhstrh;
863f21ee2d4SSteve Longerbeam else if ((instr & 0x001000f0) == 0x000000d0 || /* LDRD */
864f21ee2d4SSteve Longerbeam (instr & 0x001000f0) == 0x000000f0) /* STRD */
865f21ee2d4SSteve Longerbeam handler = do_alignment_ldrdstrd;
86619da83f6SGeorge G. Davis else if ((instr & 0x01f00ff0) == 0x01000090) /* SWP */
86719da83f6SGeorge G. Davis goto swp;
868f21ee2d4SSteve Longerbeam else
869f21ee2d4SSteve Longerbeam goto bad;
8701da177e4SLinus Torvalds break;
8711da177e4SLinus Torvalds
8721da177e4SLinus Torvalds case 0x04000000: /* ldr or str immediate */
8735ca918e5SRobin Murphy if (COND_BITS(instr) == 0xf0000000) /* NEON VLDn, VSTn */
8745ca918e5SRobin Murphy goto bad;
8751da177e4SLinus Torvalds offset.un = OFFSET_BITS(instr);
8761da177e4SLinus Torvalds handler = do_alignment_ldrstr;
8771da177e4SLinus Torvalds break;
8781da177e4SLinus Torvalds
8791da177e4SLinus Torvalds case 0x06000000: /* ldr or str register */
8801da177e4SLinus Torvalds offset.un = regs->uregs[RM_BITS(instr)];
8811da177e4SLinus Torvalds
8821da177e4SLinus Torvalds if (IS_SHIFT(instr)) {
8831da177e4SLinus Torvalds unsigned int shiftval = SHIFT_BITS(instr);
8841da177e4SLinus Torvalds
8851da177e4SLinus Torvalds switch(SHIFT_TYPE(instr)) {
8861da177e4SLinus Torvalds case SHIFT_LSL:
8871da177e4SLinus Torvalds offset.un <<= shiftval;
8881da177e4SLinus Torvalds break;
8891da177e4SLinus Torvalds
8901da177e4SLinus Torvalds case SHIFT_LSR:
8911da177e4SLinus Torvalds offset.un >>= shiftval;
8921da177e4SLinus Torvalds break;
8931da177e4SLinus Torvalds
8941da177e4SLinus Torvalds case SHIFT_ASR:
8951da177e4SLinus Torvalds offset.sn >>= shiftval;
8961da177e4SLinus Torvalds break;
8971da177e4SLinus Torvalds
8981da177e4SLinus Torvalds case SHIFT_RORRRX:
8991da177e4SLinus Torvalds if (shiftval == 0) {
9001da177e4SLinus Torvalds offset.un >>= 1;
9011da177e4SLinus Torvalds if (regs->ARM_cpsr & PSR_C_BIT)
9021da177e4SLinus Torvalds offset.un |= 1 << 31;
9031da177e4SLinus Torvalds } else
9041da177e4SLinus Torvalds offset.un = offset.un >> shiftval |
9051da177e4SLinus Torvalds offset.un << (32 - shiftval);
9061da177e4SLinus Torvalds break;
9071da177e4SLinus Torvalds }
9081da177e4SLinus Torvalds }
9091da177e4SLinus Torvalds handler = do_alignment_ldrstr;
9101da177e4SLinus Torvalds break;
9111da177e4SLinus Torvalds
912c2860d43SGeorge G. Davis case 0x08000000: /* ldm or stm, or thumb-2 32bit instruction */
913a761cebfSRussell King if (thumb2_32b) {
914a761cebfSRussell King offset.un = 0;
915c2860d43SGeorge G. Davis handler = do_alignment_t32_to_handler(&instr, regs, &offset);
91631d2a638SArnd Bergmann } else {
91731d2a638SArnd Bergmann offset.un = 0;
9181da177e4SLinus Torvalds handler = do_alignment_ldmstm;
91931d2a638SArnd Bergmann }
9201da177e4SLinus Torvalds break;
9211da177e4SLinus Torvalds
9221da177e4SLinus Torvalds default:
9231da177e4SLinus Torvalds goto bad;
9241da177e4SLinus Torvalds }
9251da177e4SLinus Torvalds
926c2860d43SGeorge G. Davis if (!handler)
927c2860d43SGeorge G. Davis goto bad;
9281da177e4SLinus Torvalds type = handler(addr, instr, regs);
9291da177e4SLinus Torvalds
930c2860d43SGeorge G. Davis if (type == TYPE_ERROR || type == TYPE_FAULT) {
931c2860d43SGeorge G. Davis regs->ARM_pc -= isize;
9321da177e4SLinus Torvalds goto bad_or_fault;
933c2860d43SGeorge G. Davis }
9341da177e4SLinus Torvalds
9351da177e4SLinus Torvalds if (type == TYPE_LDST)
9361da177e4SLinus Torvalds do_alignment_finish_ldst(addr, instr, regs, offset);
9371da177e4SLinus Torvalds
938*e5c46fdeSArd Biesheuvel if (thumb_mode(regs))
939*e5c46fdeSArd Biesheuvel regs->ARM_cpsr = it_advance(regs->ARM_cpsr);
940*e5c46fdeSArd Biesheuvel
9411da177e4SLinus Torvalds return 0;
9421da177e4SLinus Torvalds
9431da177e4SLinus Torvalds bad_or_fault:
9441da177e4SLinus Torvalds if (type == TYPE_ERROR)
9451da177e4SLinus Torvalds goto bad;
9461da177e4SLinus Torvalds /*
9471da177e4SLinus Torvalds * We got a fault - fix it up, or die.
9481da177e4SLinus Torvalds */
949e5beac37SRussell King do_bad_area(addr, fsr, regs);
9501da177e4SLinus Torvalds return 0;
9511da177e4SLinus Torvalds
95219da83f6SGeorge G. Davis swp:
9534ed89f22SRussell King pr_err("Alignment trap: not handling swp instruction\n");
95419da83f6SGeorge G. Davis
9551da177e4SLinus Torvalds bad:
9561da177e4SLinus Torvalds /*
9571da177e4SLinus Torvalds * Oops, we didn't handle the instruction.
9581da177e4SLinus Torvalds */
9594ed89f22SRussell King pr_err("Alignment trap: not handling instruction "
9601bb9fb0aSRussell King "%0*x at [<%08lx>]\n",
961c2860d43SGeorge G. Davis isize << 1,
962c2860d43SGeorge G. Davis isize == 2 ? tinstr : instr, instrptr);
9631da177e4SLinus Torvalds ai_skipped += 1;
9641da177e4SLinus Torvalds return 1;
9651da177e4SLinus Torvalds
9661da177e4SLinus Torvalds user:
9671da177e4SLinus Torvalds ai_user += 1;
9681da177e4SLinus Torvalds
969baa745a3SRussell King if (ai_usermode & UM_WARN)
9701bb9fb0aSRussell King printk("Alignment trap: %s (%d) PC=0x%08lx Instr=0x%0*x "
9711da177e4SLinus Torvalds "Address=0x%08lx FSR 0x%03x\n", current->comm,
97219c5870cSAlexey Dobriyan task_pid_nr(current), instrptr,
973c2860d43SGeorge G. Davis isize << 1,
974c2860d43SGeorge G. Davis isize == 2 ? tinstr : instr,
9751da177e4SLinus Torvalds addr, fsr);
9761da177e4SLinus Torvalds
977baa745a3SRussell King if (ai_usermode & UM_FIXUP)
9781da177e4SLinus Torvalds goto fixup;
9791da177e4SLinus Torvalds
9802102a65eSDave Martin if (ai_usermode & UM_SIGNAL) {
9812e1661d2SEric W. Biederman force_sig_fault(SIGBUS, BUS_ADRALN, (void __user *)addr);
9822102a65eSDave Martin } else {
9832f27bf83SNicolas Pitre /*
9842f27bf83SNicolas Pitre * We're about to disable the alignment trap and return to
9852f27bf83SNicolas Pitre * user space. But if an interrupt occurs before actually
9862f27bf83SNicolas Pitre * reaching user space, then the IRQ vector entry code will
9872f27bf83SNicolas Pitre * notice that we were still in kernel space and therefore
9882f27bf83SNicolas Pitre * the alignment trap won't be re-enabled in that case as it
9892f27bf83SNicolas Pitre * is presumed to be always on from kernel space.
9902f27bf83SNicolas Pitre * Let's prevent that race by disabling interrupts here (they
9912f27bf83SNicolas Pitre * are disabled on the way back to user space anyway in
9922f27bf83SNicolas Pitre * entry-common.S) and disable the alignment trap only if
9932f27bf83SNicolas Pitre * there is no work pending for this thread.
9942f27bf83SNicolas Pitre */
9952f27bf83SNicolas Pitre raw_local_irq_disable();
996050e22bfSMark Rutland if (!(read_thread_flags() & _TIF_WORK_MASK))
9971da177e4SLinus Torvalds set_cr(cr_no_alignment);
9982f27bf83SNicolas Pitre }
9991da177e4SLinus Torvalds
10001da177e4SLinus Torvalds return 0;
10011da177e4SLinus Torvalds }
10021da177e4SLinus Torvalds
noalign_setup(char * __unused)1003175352a5SRussell King static int __init noalign_setup(char *__unused)
1004175352a5SRussell King {
1005175352a5SRussell King set_cr(__clear_cr(CR_A));
1006175352a5SRussell King return 1;
1007175352a5SRussell King }
1008175352a5SRussell King __setup("noalign", noalign_setup);
1009175352a5SRussell King
10101da177e4SLinus Torvalds /*
1011d8c0418aSLuis Chamberlain * This needs to be done after sysctl_init_bases(), otherwise sys/ will be
10121da177e4SLinus Torvalds * overwritten. Actually, this shouldn't be in sys/ at all since
10131da177e4SLinus Torvalds * it isn't a sysctl, and it doesn't contain sysctl information.
10141da177e4SLinus Torvalds * We now locate it in /proc/cpu/alignment instead.
10151da177e4SLinus Torvalds */
alignment_init(void)10161da177e4SLinus Torvalds static int __init alignment_init(void)
10171da177e4SLinus Torvalds {
10181da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
10191da177e4SLinus Torvalds struct proc_dir_entry *res;
10201da177e4SLinus Torvalds
1021b7072c63SAlexey Dobriyan res = proc_create("cpu/alignment", S_IWUSR | S_IRUGO, NULL,
102297a32539SAlexey Dobriyan &alignment_proc_ops);
10231da177e4SLinus Torvalds if (!res)
10241da177e4SLinus Torvalds return -ENOMEM;
10251da177e4SLinus Torvalds #endif
10261da177e4SLinus Torvalds
1027088c01f1SDave Martin if (cpu_is_v6_unaligned()) {
1028b4b20ad8SRussell King set_cr(__clear_cr(CR_A));
1029088c01f1SDave Martin ai_usermode = safe_usermode(ai_usermode, false);
1030baa745a3SRussell King }
1031baa745a3SRussell King
10320aeb3408SRussell King cr_no_alignment = get_cr() & ~CR_A;
10330aeb3408SRussell King
1034f7b8156dSCatalin Marinas hook_fault_code(FAULT_CODE_ALIGNMENT, do_alignment, SIGBUS, BUS_ADRALN,
10356338a6aaSKirill A. Shutemov "alignment exception");
1036b8ab5397SKirill A. Shutemov
1037b8ab5397SKirill A. Shutemov /*
1038b8ab5397SKirill A. Shutemov * ARMv6K and ARMv7 use fault status 3 (0b00011) as Access Flag section
1039b8ab5397SKirill A. Shutemov * fault, not as alignment error.
1040b8ab5397SKirill A. Shutemov *
1041b8ab5397SKirill A. Shutemov * TODO: handle ARMv6K properly. Runtime check for 'K' extension is
1042b8ab5397SKirill A. Shutemov * needed.
1043b8ab5397SKirill A. Shutemov */
1044b8ab5397SKirill A. Shutemov if (cpu_architecture() <= CPU_ARCH_ARMv6) {
10456338a6aaSKirill A. Shutemov hook_fault_code(3, do_alignment, SIGBUS, BUS_ADRALN,
10466338a6aaSKirill A. Shutemov "alignment exception");
1047b8ab5397SKirill A. Shutemov }
10481da177e4SLinus Torvalds
10491da177e4SLinus Torvalds return 0;
10501da177e4SLinus Torvalds }
10511da177e4SLinus Torvalds
10521da177e4SLinus Torvalds fs_initcall(alignment_init);
1053