blob: e376883ab35bf32849dd6633e45338410bfa192b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/arm/mm/alignment.c
3 *
4 * Copyright (C) 1995 Linus Torvalds
5 * Modifications for ARM processor (c) 1995-2001 Russell King
Simon Arlott6cbdc8c2007-05-11 20:40:30 +01006 * Thumb alignment fault fixups (c) 2004 MontaVista Software, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * - Adapted from gdb/sim/arm/thumbemu.c -- Thumb instruction emulation.
8 * Copyright (C) 1996, Cygnus Software Technologies Ltd.
9 *
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.
13 */
Russell Kingd944d542010-02-20 16:13:29 +000014#include <linux/moduleparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/compiler.h>
16#include <linux/kernel.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +010017#include <linux/sched/debug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/errno.h>
19#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/proc_fs.h>
Alexey Dobriyanb7072c62010-05-02 12:40:35 +030021#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/init.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010023#include <linux/sched/signal.h>
Russell King33fa9b12008-09-06 11:35:55 +010024#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Russell King15d07dc2012-03-28 18:30:01 +010026#include <asm/cp15.h>
David Howells9f97da72012-03-28 18:30:01 +010027#include <asm/system_info.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/unaligned.h>
Ben Dooks8592edf2013-07-18 21:10:56 +010029#include <asm/opcodes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#include "fault.h"
Russell Kingb4b20ad82014-04-13 18:57:29 +010032#include "mm.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34/*
35 * 32-bit misaligned trap handler (c) 1998 San Mehat (CCC) -July 1998
36 * /proc/sys/debug/alignment, modified and integrated into
37 * Linux 2.1 by Russell King
38 *
39 * Speed optimisations and better fault handling by Russell King.
40 *
41 * *** NOTE ***
42 * This code is not portable to processors with late data abort handling.
43 */
44#define CODING_BITS(i) (i & 0x0e000000)
Robin Murphy5ca918e2014-09-25 11:56:19 +010045#define COND_BITS(i) (i & 0xf0000000)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#define LDST_I_BIT(i) (i & (1 << 26)) /* Immediate constant */
48#define LDST_P_BIT(i) (i & (1 << 24)) /* Preindex */
49#define LDST_U_BIT(i) (i & (1 << 23)) /* Add offset */
50#define LDST_W_BIT(i) (i & (1 << 21)) /* Writeback */
51#define LDST_L_BIT(i) (i & (1 << 20)) /* Load */
52
53#define LDST_P_EQ_U(i) ((((i) ^ ((i) >> 1)) & (1 << 23)) == 0)
54
Steve Longerbeamf21ee2d2005-08-31 21:22:20 +010055#define LDSTHD_I_BIT(i) (i & (1 << 22)) /* double/half-word immed */
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#define LDM_S_BIT(i) (i & (1 << 22)) /* write CPSR from SPSR */
57
58#define RN_BITS(i) ((i >> 16) & 15) /* Rn */
59#define RD_BITS(i) ((i >> 12) & 15) /* Rd */
60#define RM_BITS(i) (i & 15) /* Rm */
61
62#define REGMASK_BITS(i) (i & 0xffff)
63#define OFFSET_BITS(i) (i & 0x0fff)
64
65#define IS_SHIFT(i) (i & 0x0ff0)
66#define SHIFT_BITS(i) ((i >> 7) & 0x1f)
67#define SHIFT_TYPE(i) (i & 0x60)
68#define SHIFT_LSL 0x00
69#define SHIFT_LSR 0x20
70#define SHIFT_ASR 0x40
71#define SHIFT_RORRRX 0x60
72
George G. Davisc2860d42009-06-04 17:16:04 +010073#define BAD_INSTR 0xdeadc0de
74
75/* Thumb-2 32 bit format per ARMv7 DDI0406A A6.3, either f800h,e800h,f800h */
76#define IS_T32(hi16) \
77 (((hi16) & 0xe000) == 0xe000 && ((hi16) & 0x1800))
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static unsigned long ai_user;
80static unsigned long ai_sys;
Russell King1e7e3212014-07-04 14:32:43 +010081static void *ai_sys_last_pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082static unsigned long ai_skipped;
83static unsigned long ai_half;
84static unsigned long ai_word;
Steve Longerbeamf21ee2d2005-08-31 21:22:20 +010085static unsigned long ai_dword;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086static unsigned long ai_multi;
87static int ai_usermode;
Russell King0aeb3402014-04-13 19:43:26 +010088static unsigned long cr_no_alignment;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Russell Kingd944d542010-02-20 16:13:29 +000090core_param(alignment, ai_usermode, int, 0600);
91
Russell Kingbaa745a2008-12-07 09:44:55 +000092#define UM_WARN (1 << 0)
93#define UM_FIXUP (1 << 1)
94#define UM_SIGNAL (1 << 2)
95
Dave Martin088c01f2011-07-28 14:28:52 +010096/* Return true if and only if the ARMv6 unaligned access model is in use. */
97static bool cpu_is_v6_unaligned(void)
98{
Russell King4585eaf2014-04-13 18:47:34 +010099 return cpu_architecture() >= CPU_ARCH_ARMv6 && get_cr() & CR_U;
Dave Martin088c01f2011-07-28 14:28:52 +0100100}
101
102static int safe_usermode(int new_usermode, bool warn)
103{
104 /*
105 * ARMv6 and later CPUs can perform unaligned accesses for
106 * most single load and store instructions up to word size.
107 * LDM, STM, LDRD and STRD still need to be handled.
108 *
109 * Ignoring the alignment fault is not an option on these
110 * CPUs since we spin re-faulting the instruction without
111 * making any progress.
112 */
113 if (cpu_is_v6_unaligned() && !(new_usermode & (UM_FIXUP | UM_SIGNAL))) {
114 new_usermode |= UM_FIXUP;
115
116 if (warn)
Russell King4ed89f22014-10-28 11:26:42 +0000117 pr_warn("alignment: ignoring faults is unsafe on this CPU. Defaulting to fixup mode.\n");
Dave Martin088c01f2011-07-28 14:28:52 +0100118 }
119
120 return new_usermode;
121}
122
Arnd Bergmannffc660c2011-08-27 20:01:07 +0200123#ifdef CONFIG_PROC_FS
124static const char *usermode_action[] = {
125 "ignored",
126 "warn",
127 "fixup",
128 "fixup+warn",
129 "signal",
130 "signal+warn"
131};
132
Alexey Dobriyanb7072c62010-05-02 12:40:35 +0300133static int alignment_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Alexey Dobriyanb7072c62010-05-02 12:40:35 +0300135 seq_printf(m, "User:\t\t%lu\n", ai_user);
Sakari Ailusd75f7732019-03-25 21:32:28 +0200136 seq_printf(m, "System:\t\t%lu (%pS)\n", ai_sys, ai_sys_last_pc);
Alexey Dobriyanb7072c62010-05-02 12:40:35 +0300137 seq_printf(m, "Skipped:\t%lu\n", ai_skipped);
138 seq_printf(m, "Half:\t\t%lu\n", ai_half);
139 seq_printf(m, "Word:\t\t%lu\n", ai_word);
Steve Longerbeamf21ee2d2005-08-31 21:22:20 +0100140 if (cpu_architecture() >= CPU_ARCH_ARMv5TE)
Alexey Dobriyanb7072c62010-05-02 12:40:35 +0300141 seq_printf(m, "DWord:\t\t%lu\n", ai_dword);
142 seq_printf(m, "Multi:\t\t%lu\n", ai_multi);
143 seq_printf(m, "User faults:\t%i (%s)\n", ai_usermode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 usermode_action[ai_usermode]);
145
Alexey Dobriyanb7072c62010-05-02 12:40:35 +0300146 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
Alexey Dobriyanb7072c62010-05-02 12:40:35 +0300149static int alignment_proc_open(struct inode *inode, struct file *file)
150{
151 return single_open(file, alignment_proc_show, NULL);
152}
153
154static ssize_t alignment_proc_write(struct file *file, const char __user *buffer,
155 size_t count, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 char mode;
158
159 if (count > 0) {
160 if (get_user(mode, buffer))
161 return -EFAULT;
162 if (mode >= '0' && mode <= '5')
Dave Martin088c01f2011-07-28 14:28:52 +0100163 ai_usermode = safe_usermode(mode - '0', true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 }
165 return count;
166}
167
Alexey Dobriyanb7072c62010-05-02 12:40:35 +0300168static const struct file_operations alignment_proc_fops = {
169 .open = alignment_proc_open,
170 .read = seq_read,
171 .llseek = seq_lseek,
172 .release = single_release,
173 .write = alignment_proc_write,
174};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175#endif /* CONFIG_PROC_FS */
176
177union offset_union {
178 unsigned long un;
179 signed long sn;
180};
181
182#define TYPE_ERROR 0
183#define TYPE_FAULT 1
184#define TYPE_LDST 2
185#define TYPE_DONE 3
186
187#ifdef __ARMEB__
188#define BE 1
189#define FIRST_BYTE_16 "mov %1, %1, ror #8\n"
190#define FIRST_BYTE_32 "mov %1, %1, ror #24\n"
191#define NEXT_BYTE "ror #24"
192#else
193#define BE 0
194#define FIRST_BYTE_16
195#define FIRST_BYTE_32
196#define NEXT_BYTE "lsr #8"
197#endif
198
199#define __get8_unaligned_check(ins,val,addr,err) \
200 __asm__( \
Catalin Marinas347c8b72009-07-24 12:32:56 +0100201 ARM( "1: "ins" %1, [%2], #1\n" ) \
202 THUMB( "1: "ins" %1, [%2]\n" ) \
203 THUMB( " add %2, %2, #1\n" ) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 "2:\n" \
Ard Biesheuvelc4a84ae2015-03-24 10:41:09 +0100205 " .pushsection .text.fixup,\"ax\"\n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 " .align 2\n" \
207 "3: mov %0, #1\n" \
208 " b 2b\n" \
Russell King42604152010-04-19 10:15:03 +0100209 " .popsection\n" \
210 " .pushsection __ex_table,\"a\"\n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 " .align 3\n" \
212 " .long 1b, 3b\n" \
Russell King42604152010-04-19 10:15:03 +0100213 " .popsection\n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 : "=r" (err), "=&r" (val), "=r" (addr) \
215 : "0" (err), "2" (addr))
216
217#define __get16_unaligned_check(ins,val,addr) \
218 do { \
219 unsigned int err = 0, v, a = addr; \
220 __get8_unaligned_check(ins,v,a,err); \
221 val = v << ((BE) ? 8 : 0); \
222 __get8_unaligned_check(ins,v,a,err); \
223 val |= v << ((BE) ? 0 : 8); \
224 if (err) \
225 goto fault; \
226 } while (0)
227
228#define get16_unaligned_check(val,addr) \
229 __get16_unaligned_check("ldrb",val,addr)
230
231#define get16t_unaligned_check(val,addr) \
232 __get16_unaligned_check("ldrbt",val,addr)
233
234#define __get32_unaligned_check(ins,val,addr) \
235 do { \
236 unsigned int err = 0, v, a = addr; \
237 __get8_unaligned_check(ins,v,a,err); \
238 val = v << ((BE) ? 24 : 0); \
239 __get8_unaligned_check(ins,v,a,err); \
240 val |= v << ((BE) ? 16 : 8); \
241 __get8_unaligned_check(ins,v,a,err); \
242 val |= v << ((BE) ? 8 : 16); \
243 __get8_unaligned_check(ins,v,a,err); \
244 val |= v << ((BE) ? 0 : 24); \
245 if (err) \
246 goto fault; \
247 } while (0)
248
249#define get32_unaligned_check(val,addr) \
250 __get32_unaligned_check("ldrb",val,addr)
251
252#define get32t_unaligned_check(val,addr) \
253 __get32_unaligned_check("ldrbt",val,addr)
254
255#define __put16_unaligned_check(ins,val,addr) \
256 do { \
257 unsigned int err = 0, v = val, a = addr; \
258 __asm__( FIRST_BYTE_16 \
Catalin Marinas347c8b72009-07-24 12:32:56 +0100259 ARM( "1: "ins" %1, [%2], #1\n" ) \
260 THUMB( "1: "ins" %1, [%2]\n" ) \
261 THUMB( " add %2, %2, #1\n" ) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 " mov %1, %1, "NEXT_BYTE"\n" \
263 "2: "ins" %1, [%2]\n" \
264 "3:\n" \
Ard Biesheuvelc4a84ae2015-03-24 10:41:09 +0100265 " .pushsection .text.fixup,\"ax\"\n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 " .align 2\n" \
267 "4: mov %0, #1\n" \
268 " b 3b\n" \
Russell King42604152010-04-19 10:15:03 +0100269 " .popsection\n" \
270 " .pushsection __ex_table,\"a\"\n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 " .align 3\n" \
272 " .long 1b, 4b\n" \
273 " .long 2b, 4b\n" \
Russell King42604152010-04-19 10:15:03 +0100274 " .popsection\n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 : "=r" (err), "=&r" (v), "=&r" (a) \
276 : "0" (err), "1" (v), "2" (a)); \
277 if (err) \
278 goto fault; \
279 } while (0)
280
281#define put16_unaligned_check(val,addr) \
282 __put16_unaligned_check("strb",val,addr)
283
284#define put16t_unaligned_check(val,addr) \
285 __put16_unaligned_check("strbt",val,addr)
286
287#define __put32_unaligned_check(ins,val,addr) \
288 do { \
289 unsigned int err = 0, v = val, a = addr; \
290 __asm__( FIRST_BYTE_32 \
Catalin Marinas347c8b72009-07-24 12:32:56 +0100291 ARM( "1: "ins" %1, [%2], #1\n" ) \
292 THUMB( "1: "ins" %1, [%2]\n" ) \
293 THUMB( " add %2, %2, #1\n" ) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 " mov %1, %1, "NEXT_BYTE"\n" \
Catalin Marinas347c8b72009-07-24 12:32:56 +0100295 ARM( "2: "ins" %1, [%2], #1\n" ) \
296 THUMB( "2: "ins" %1, [%2]\n" ) \
297 THUMB( " add %2, %2, #1\n" ) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 " mov %1, %1, "NEXT_BYTE"\n" \
Catalin Marinas347c8b72009-07-24 12:32:56 +0100299 ARM( "3: "ins" %1, [%2], #1\n" ) \
300 THUMB( "3: "ins" %1, [%2]\n" ) \
301 THUMB( " add %2, %2, #1\n" ) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 " mov %1, %1, "NEXT_BYTE"\n" \
303 "4: "ins" %1, [%2]\n" \
304 "5:\n" \
Ard Biesheuvelc4a84ae2015-03-24 10:41:09 +0100305 " .pushsection .text.fixup,\"ax\"\n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 " .align 2\n" \
307 "6: mov %0, #1\n" \
308 " b 5b\n" \
Russell King42604152010-04-19 10:15:03 +0100309 " .popsection\n" \
310 " .pushsection __ex_table,\"a\"\n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 " .align 3\n" \
312 " .long 1b, 6b\n" \
313 " .long 2b, 6b\n" \
314 " .long 3b, 6b\n" \
315 " .long 4b, 6b\n" \
Russell King42604152010-04-19 10:15:03 +0100316 " .popsection\n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 : "=r" (err), "=&r" (v), "=&r" (a) \
318 : "0" (err), "1" (v), "2" (a)); \
319 if (err) \
320 goto fault; \
321 } while (0)
322
George G. Davis737d0bb2005-10-12 19:58:10 +0100323#define put32_unaligned_check(val,addr) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 __put32_unaligned_check("strb", val, addr)
325
326#define put32t_unaligned_check(val,addr) \
327 __put32_unaligned_check("strbt", val, addr)
328
329static void
330do_alignment_finish_ldst(unsigned long addr, unsigned long instr, struct pt_regs *regs, union offset_union offset)
331{
332 if (!LDST_U_BIT(instr))
333 offset.un = -offset.un;
334
335 if (!LDST_P_BIT(instr))
336 addr += offset.un;
337
338 if (!LDST_P_BIT(instr) || LDST_W_BIT(instr))
339 regs->uregs[RN_BITS(instr)] = addr;
340}
341
342static int
343do_alignment_ldrhstrh(unsigned long addr, unsigned long instr, struct pt_regs *regs)
344{
345 unsigned int rd = RD_BITS(instr);
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 ai_half += 1;
348
349 if (user_mode(regs))
350 goto user;
351
352 if (LDST_L_BIT(instr)) {
353 unsigned long val;
354 get16_unaligned_check(val, addr);
355
356 /* signed half-word? */
357 if (instr & 0x40)
358 val = (signed long)((signed short) val);
359
360 regs->uregs[rd] = val;
361 } else
362 put16_unaligned_check(regs->uregs[rd], addr);
363
364 return TYPE_LDST;
365
366 user:
George G. Davis737d0bb2005-10-12 19:58:10 +0100367 if (LDST_L_BIT(instr)) {
368 unsigned long val;
Russell King274e91b2015-09-23 11:06:30 +0100369 unsigned int __ua_flags = uaccess_save_and_enable();
370
George G. Davis737d0bb2005-10-12 19:58:10 +0100371 get16t_unaligned_check(val, addr);
Russell King274e91b2015-09-23 11:06:30 +0100372 uaccess_restore(__ua_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
George G. Davis737d0bb2005-10-12 19:58:10 +0100374 /* signed half-word? */
375 if (instr & 0x40)
376 val = (signed long)((signed short) val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
George G. Davis737d0bb2005-10-12 19:58:10 +0100378 regs->uregs[rd] = val;
Russell King274e91b2015-09-23 11:06:30 +0100379 } else {
380 unsigned int __ua_flags = uaccess_save_and_enable();
George G. Davis737d0bb2005-10-12 19:58:10 +0100381 put16t_unaligned_check(regs->uregs[rd], addr);
Russell King274e91b2015-09-23 11:06:30 +0100382 uaccess_restore(__ua_flags);
383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
George G. Davis737d0bb2005-10-12 19:58:10 +0100385 return TYPE_LDST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Steve Longerbeamf21ee2d2005-08-31 21:22:20 +0100387 fault:
388 return TYPE_FAULT;
389}
390
391static int
392do_alignment_ldrdstrd(unsigned long addr, unsigned long instr,
393 struct pt_regs *regs)
394{
395 unsigned int rd = RD_BITS(instr);
George G. Davisc2860d42009-06-04 17:16:04 +0100396 unsigned int rd2;
397 int load;
Steve Longerbeamf21ee2d2005-08-31 21:22:20 +0100398
George G. Davisc2860d42009-06-04 17:16:04 +0100399 if ((instr & 0xfe000000) == 0xe8000000) {
400 /* ARMv7 Thumb-2 32-bit LDRD/STRD */
401 rd2 = (instr >> 8) & 0xf;
402 load = !!(LDST_L_BIT(instr));
403 } else if (((rd & 1) == 1) || (rd == 14))
George G. Davis19da83f2005-10-10 10:17:44 +0100404 goto bad;
George G. Davisc2860d42009-06-04 17:16:04 +0100405 else {
406 load = ((instr & 0xf0) == 0xd0);
407 rd2 = rd + 1;
408 }
George G. Davis19da83f2005-10-10 10:17:44 +0100409
Steve Longerbeamf21ee2d2005-08-31 21:22:20 +0100410 ai_dword += 1;
411
412 if (user_mode(regs))
413 goto user;
414
George G. Davisc2860d42009-06-04 17:16:04 +0100415 if (load) {
Steve Longerbeamf21ee2d2005-08-31 21:22:20 +0100416 unsigned long val;
417 get32_unaligned_check(val, addr);
418 regs->uregs[rd] = val;
George G. Davis737d0bb2005-10-12 19:58:10 +0100419 get32_unaligned_check(val, addr + 4);
George G. Davisc2860d42009-06-04 17:16:04 +0100420 regs->uregs[rd2] = val;
Steve Longerbeamf21ee2d2005-08-31 21:22:20 +0100421 } else {
422 put32_unaligned_check(regs->uregs[rd], addr);
George G. Davisc2860d42009-06-04 17:16:04 +0100423 put32_unaligned_check(regs->uregs[rd2], addr + 4);
Steve Longerbeamf21ee2d2005-08-31 21:22:20 +0100424 }
425
426 return TYPE_LDST;
427
428 user:
George G. Davisc2860d42009-06-04 17:16:04 +0100429 if (load) {
Russell King274e91b2015-09-23 11:06:30 +0100430 unsigned long val, val2;
431 unsigned int __ua_flags = uaccess_save_and_enable();
432
Steve Longerbeamf21ee2d2005-08-31 21:22:20 +0100433 get32t_unaligned_check(val, addr);
Russell King274e91b2015-09-23 11:06:30 +0100434 get32t_unaligned_check(val2, addr + 4);
435
436 uaccess_restore(__ua_flags);
437
Steve Longerbeamf21ee2d2005-08-31 21:22:20 +0100438 regs->uregs[rd] = val;
Russell King274e91b2015-09-23 11:06:30 +0100439 regs->uregs[rd2] = val2;
Steve Longerbeamf21ee2d2005-08-31 21:22:20 +0100440 } else {
Russell King274e91b2015-09-23 11:06:30 +0100441 unsigned int __ua_flags = uaccess_save_and_enable();
Steve Longerbeamf21ee2d2005-08-31 21:22:20 +0100442 put32t_unaligned_check(regs->uregs[rd], addr);
George G. Davisc2860d42009-06-04 17:16:04 +0100443 put32t_unaligned_check(regs->uregs[rd2], addr + 4);
Russell King274e91b2015-09-23 11:06:30 +0100444 uaccess_restore(__ua_flags);
Steve Longerbeamf21ee2d2005-08-31 21:22:20 +0100445 }
446
447 return TYPE_LDST;
George G. Davis19da83f2005-10-10 10:17:44 +0100448 bad:
449 return TYPE_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 fault:
451 return TYPE_FAULT;
452}
453
454static int
455do_alignment_ldrstr(unsigned long addr, unsigned long instr, struct pt_regs *regs)
456{
457 unsigned int rd = RD_BITS(instr);
458
459 ai_word += 1;
460
461 if ((!LDST_P_BIT(instr) && LDST_W_BIT(instr)) || user_mode(regs))
462 goto trans;
463
464 if (LDST_L_BIT(instr)) {
465 unsigned int val;
466 get32_unaligned_check(val, addr);
467 regs->uregs[rd] = val;
468 } else
469 put32_unaligned_check(regs->uregs[rd], addr);
470 return TYPE_LDST;
471
472 trans:
473 if (LDST_L_BIT(instr)) {
474 unsigned int val;
Russell King274e91b2015-09-23 11:06:30 +0100475 unsigned int __ua_flags = uaccess_save_and_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 get32t_unaligned_check(val, addr);
Russell King274e91b2015-09-23 11:06:30 +0100477 uaccess_restore(__ua_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 regs->uregs[rd] = val;
Russell King274e91b2015-09-23 11:06:30 +0100479 } else {
480 unsigned int __ua_flags = uaccess_save_and_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 put32t_unaligned_check(regs->uregs[rd], addr);
Russell King274e91b2015-09-23 11:06:30 +0100482 uaccess_restore(__ua_flags);
483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return TYPE_LDST;
485
486 fault:
487 return TYPE_FAULT;
488}
489
490/*
491 * LDM/STM alignment handler.
492 *
493 * There are 4 variants of this instruction:
494 *
495 * B = rn pointer before instruction, A = rn pointer after instruction
496 * ------ increasing address ----->
497 * | | r0 | r1 | ... | rx | |
498 * PU = 01 B A
499 * PU = 11 B A
500 * PU = 00 A B
501 * PU = 10 A B
502 */
503static int
504do_alignment_ldmstm(unsigned long addr, unsigned long instr, struct pt_regs *regs)
505{
506 unsigned int rd, rn, correction, nr_regs, regbits;
507 unsigned long eaddr, newaddr;
508
509 if (LDM_S_BIT(instr))
510 goto bad;
511
512 correction = 4; /* processor implementation defined */
513 regs->ARM_pc += correction;
514
515 ai_multi += 1;
516
517 /* count the number of registers in the mask to be transferred */
518 nr_regs = hweight16(REGMASK_BITS(instr)) * 4;
519
520 rn = RN_BITS(instr);
521 newaddr = eaddr = regs->uregs[rn];
522
523 if (!LDST_U_BIT(instr))
524 nr_regs = -nr_regs;
525 newaddr += nr_regs;
526 if (!LDST_U_BIT(instr))
527 eaddr = newaddr;
528
529 if (LDST_P_EQ_U(instr)) /* U = P */
530 eaddr += 4;
531
George G. Davis737d0bb2005-10-12 19:58:10 +0100532 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 * For alignment faults on the ARM922T/ARM920T the MMU makes
534 * the FSR (and hence addr) equal to the updated base address
535 * of the multiple access rather than the restored value.
536 * Switch this message off if we've got a ARM92[02], otherwise
537 * [ls]dm alignment faults are noisy!
538 */
539#if !(defined CONFIG_CPU_ARM922T) && !(defined CONFIG_CPU_ARM920T)
540 /*
541 * This is a "hint" - we already have eaddr worked out by the
542 * processor for us.
543 */
544 if (addr != eaddr) {
Russell King4ed89f22014-10-28 11:26:42 +0000545 pr_err("LDMSTM: PC = %08lx, instr = %08lx, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 "addr = %08lx, eaddr = %08lx\n",
547 instruction_pointer(regs), instr, addr, eaddr);
548 show_regs(regs);
549 }
550#endif
551
552 if (user_mode(regs)) {
Russell King274e91b2015-09-23 11:06:30 +0100553 unsigned int __ua_flags = uaccess_save_and_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 for (regbits = REGMASK_BITS(instr), rd = 0; regbits;
555 regbits >>= 1, rd += 1)
556 if (regbits & 1) {
557 if (LDST_L_BIT(instr)) {
558 unsigned int val;
559 get32t_unaligned_check(val, eaddr);
560 regs->uregs[rd] = val;
561 } else
562 put32t_unaligned_check(regs->uregs[rd], eaddr);
563 eaddr += 4;
564 }
Russell King274e91b2015-09-23 11:06:30 +0100565 uaccess_restore(__ua_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 } else {
567 for (regbits = REGMASK_BITS(instr), rd = 0; regbits;
568 regbits >>= 1, rd += 1)
569 if (regbits & 1) {
570 if (LDST_L_BIT(instr)) {
571 unsigned int val;
572 get32_unaligned_check(val, eaddr);
573 regs->uregs[rd] = val;
574 } else
575 put32_unaligned_check(regs->uregs[rd], eaddr);
576 eaddr += 4;
577 }
578 }
579
580 if (LDST_W_BIT(instr))
581 regs->uregs[rn] = newaddr;
582 if (!LDST_L_BIT(instr) || !(REGMASK_BITS(instr) & (1 << 15)))
583 regs->ARM_pc -= correction;
584 return TYPE_DONE;
585
586fault:
587 regs->ARM_pc -= correction;
588 return TYPE_FAULT;
589
590bad:
Russell King4ed89f22014-10-28 11:26:42 +0000591 pr_err("Alignment trap: not handling ldm with s-bit set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 return TYPE_ERROR;
593}
594
595/*
596 * Convert Thumb ld/st instruction forms to equivalent ARM instructions so
597 * we can reuse ARM userland alignment fault fixups for Thumb.
598 *
599 * This implementation was initially based on the algorithm found in
600 * gdb/sim/arm/thumbemu.c. It is basically just a code reduction of same
601 * to convert only Thumb ld/st instruction forms to equivalent ARM forms.
602 *
603 * NOTES:
604 * 1. Comments below refer to ARM ARM DDI0100E Thumb Instruction sections.
605 * 2. If for some reason we're passed an non-ld/st Thumb instruction to
606 * decode, we return 0xdeadc0de. This should never happen under normal
607 * circumstances but if it does, we've got other problems to deal with
608 * elsewhere and we obviously can't fix those problems here.
609 */
610
611static unsigned long
612thumb2arm(u16 tinstr)
613{
614 u32 L = (tinstr & (1<<11)) >> 11;
615
616 switch ((tinstr & 0xf800) >> 11) {
617 /* 6.5.1 Format 1: */
618 case 0x6000 >> 11: /* 7.1.52 STR(1) */
619 case 0x6800 >> 11: /* 7.1.26 LDR(1) */
620 case 0x7000 >> 11: /* 7.1.55 STRB(1) */
621 case 0x7800 >> 11: /* 7.1.30 LDRB(1) */
622 return 0xe5800000 |
623 ((tinstr & (1<<12)) << (22-12)) | /* fixup */
624 (L<<20) | /* L==1? */
625 ((tinstr & (7<<0)) << (12-0)) | /* Rd */
626 ((tinstr & (7<<3)) << (16-3)) | /* Rn */
627 ((tinstr & (31<<6)) >> /* immed_5 */
628 (6 - ((tinstr & (1<<12)) ? 0 : 2)));
629 case 0x8000 >> 11: /* 7.1.57 STRH(1) */
630 case 0x8800 >> 11: /* 7.1.32 LDRH(1) */
631 return 0xe1c000b0 |
632 (L<<20) | /* L==1? */
633 ((tinstr & (7<<0)) << (12-0)) | /* Rd */
634 ((tinstr & (7<<3)) << (16-3)) | /* Rn */
635 ((tinstr & (7<<6)) >> (6-1)) | /* immed_5[2:0] */
636 ((tinstr & (3<<9)) >> (9-8)); /* immed_5[4:3] */
637
638 /* 6.5.1 Format 2: */
639 case 0x5000 >> 11:
640 case 0x5800 >> 11:
641 {
642 static const u32 subset[8] = {
643 0xe7800000, /* 7.1.53 STR(2) */
644 0xe18000b0, /* 7.1.58 STRH(2) */
645 0xe7c00000, /* 7.1.56 STRB(2) */
646 0xe19000d0, /* 7.1.34 LDRSB */
647 0xe7900000, /* 7.1.27 LDR(2) */
648 0xe19000b0, /* 7.1.33 LDRH(2) */
649 0xe7d00000, /* 7.1.31 LDRB(2) */
650 0xe19000f0 /* 7.1.35 LDRSH */
651 };
652 return subset[(tinstr & (7<<9)) >> 9] |
653 ((tinstr & (7<<0)) << (12-0)) | /* Rd */
654 ((tinstr & (7<<3)) << (16-3)) | /* Rn */
655 ((tinstr & (7<<6)) >> (6-0)); /* Rm */
656 }
657
658 /* 6.5.1 Format 3: */
659 case 0x4800 >> 11: /* 7.1.28 LDR(3) */
660 /* NOTE: This case is not technically possible. We're
George G. Davis737d0bb2005-10-12 19:58:10 +0100661 * loading 32-bit memory data via PC relative
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 * addressing mode. So we can and should eliminate
663 * this case. But I'll leave it here for now.
664 */
665 return 0xe59f0000 |
666 ((tinstr & (7<<8)) << (12-8)) | /* Rd */
667 ((tinstr & 255) << (2-0)); /* immed_8 */
668
669 /* 6.5.1 Format 4: */
670 case 0x9000 >> 11: /* 7.1.54 STR(3) */
671 case 0x9800 >> 11: /* 7.1.29 LDR(4) */
672 return 0xe58d0000 |
673 (L<<20) | /* L==1? */
674 ((tinstr & (7<<8)) << (12-8)) | /* Rd */
675 ((tinstr & 255) << 2); /* immed_8 */
676
677 /* 6.6.1 Format 1: */
678 case 0xc000 >> 11: /* 7.1.51 STMIA */
679 case 0xc800 >> 11: /* 7.1.25 LDMIA */
680 {
681 u32 Rn = (tinstr & (7<<8)) >> 8;
682 u32 W = ((L<<Rn) & (tinstr&255)) ? 0 : 1<<21;
683
684 return 0xe8800000 | W | (L<<20) | (Rn<<16) |
685 (tinstr&255);
686 }
687
688 /* 6.6.1 Format 2: */
689 case 0xb000 >> 11: /* 7.1.48 PUSH */
690 case 0xb800 >> 11: /* 7.1.47 POP */
691 if ((tinstr & (3 << 9)) == 0x0400) {
692 static const u32 subset[4] = {
693 0xe92d0000, /* STMDB sp!,{registers} */
694 0xe92d4000, /* STMDB sp!,{registers,lr} */
695 0xe8bd0000, /* LDMIA sp!,{registers} */
696 0xe8bd8000 /* LDMIA sp!,{registers,pc} */
697 };
698 return subset[(L<<1) | ((tinstr & (1<<8)) >> 8)] |
699 (tinstr & 255); /* register_list */
700 }
701 /* Else fall through for illegal instruction case */
702
703 default:
George G. Davisc2860d42009-06-04 17:16:04 +0100704 return BAD_INSTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
706}
707
George G. Davisc2860d42009-06-04 17:16:04 +0100708/*
709 * Convert Thumb-2 32 bit LDM, STM, LDRD, STRD to equivalent instruction
710 * handlable by ARM alignment handler, also find the corresponding handler,
711 * so that we can reuse ARM userland alignment fault fixups for Thumb.
712 *
713 * @pinstr: original Thumb-2 instruction; returns new handlable instruction
714 * @regs: register context.
715 * @poffset: return offset from faulted addr for later writeback
716 *
717 * NOTES:
718 * 1. Comments below refer to ARMv7 DDI0406A Thumb Instruction sections.
719 * 2. Register name Rt from ARMv7 is same as Rd from ARMv6 (Rd is Rt)
720 */
721static void *
722do_alignment_t32_to_handler(unsigned long *pinstr, struct pt_regs *regs,
723 union offset_union *poffset)
724{
725 unsigned long instr = *pinstr;
726 u16 tinst1 = (instr >> 16) & 0xffff;
727 u16 tinst2 = instr & 0xffff;
George G. Davisc2860d42009-06-04 17:16:04 +0100728
729 switch (tinst1 & 0xffe0) {
730 /* A6.3.5 Load/Store multiple */
731 case 0xe880: /* STM/STMIA/STMEA,LDM/LDMIA, PUSH/POP T2 */
732 case 0xe8a0: /* ...above writeback version */
733 case 0xe900: /* STMDB/STMFD, LDMDB/LDMEA */
734 case 0xe920: /* ...above writeback version */
735 /* no need offset decision since handler calculates it */
736 return do_alignment_ldmstm;
737
738 case 0xf840: /* POP/PUSH T3 (single register) */
739 if (RN_BITS(instr) == 13 && (tinst2 & 0x09ff) == 0x0904) {
740 u32 L = !!(LDST_L_BIT(instr));
741 const u32 subset[2] = {
742 0xe92d0000, /* STMDB sp!,{registers} */
743 0xe8bd0000, /* LDMIA sp!,{registers} */
744 };
745 *pinstr = subset[L] | (1<<RD_BITS(instr));
746 return do_alignment_ldmstm;
747 }
748 /* Else fall through for illegal instruction case */
749 break;
750
751 /* A6.3.6 Load/store double, STRD/LDRD(immed, lit, reg) */
752 case 0xe860:
753 case 0xe960:
754 case 0xe8e0:
755 case 0xe9e0:
756 poffset->un = (tinst2 & 0xff) << 2;
757 case 0xe940:
758 case 0xe9c0:
759 return do_alignment_ldrdstrd;
760
761 /*
762 * No need to handle load/store instructions up to word size
763 * since ARMv6 and later CPUs can perform unaligned accesses.
764 */
765 default:
766 break;
767 }
768 return NULL;
769}
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771static int
772do_alignment(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
773{
viresh kumar6404f0b2012-10-31 10:40:42 +0100774 union offset_union uninitialized_var(offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 unsigned long instr = 0, instrptr;
776 int (*handler)(unsigned long addr, unsigned long instr, struct pt_regs *regs);
777 unsigned int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 unsigned int fault;
779 u16 tinstr = 0;
George G. Davisc2860d42009-06-04 17:16:04 +0100780 int isize = 4;
781 int thumb2_32b = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Russell King02fe2842011-06-25 11:44:06 +0100783 if (interrupts_enabled(regs))
784 local_irq_enable();
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 instrptr = instruction_pointer(regs);
787
Yoann Padioleauf8343682007-06-01 00:46:36 -0700788 if (thumb_mode(regs)) {
Russell Kingb2551882013-02-25 16:10:42 +0000789 u16 *ptr = (u16 *)(instrptr & ~1);
790 fault = probe_kernel_address(ptr, tinstr);
Ben Dooks8592edf2013-07-18 21:10:56 +0100791 tinstr = __mem_to_opcode_thumb16(tinstr);
George G. Davisc2860d42009-06-04 17:16:04 +0100792 if (!fault) {
793 if (cpu_architecture() >= CPU_ARCH_ARMv7 &&
794 IS_T32(tinstr)) {
795 /* Thumb-2 32-bit */
796 u16 tinst2 = 0;
Russell Kingb2551882013-02-25 16:10:42 +0000797 fault = probe_kernel_address(ptr + 1, tinst2);
Ben Dooks8592edf2013-07-18 21:10:56 +0100798 tinst2 = __mem_to_opcode_thumb16(tinst2);
799 instr = __opcode_thumb32_compose(tinstr, tinst2);
George G. Davisc2860d42009-06-04 17:16:04 +0100800 thumb2_32b = 1;
801 } else {
802 isize = 2;
803 instr = thumb2arm(tinstr);
804 }
805 }
Ben Dooks8592edf2013-07-18 21:10:56 +0100806 } else {
Andrew Morton0ab32b62015-11-05 18:46:03 -0800807 fault = probe_kernel_address((void *)instrptr, instr);
Ben Dooks8592edf2013-07-18 21:10:56 +0100808 instr = __mem_to_opcode_arm(instr);
809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811 if (fault) {
812 type = TYPE_FAULT;
George G. Davis737d0bb2005-10-12 19:58:10 +0100813 goto bad_or_fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 }
815
816 if (user_mode(regs))
817 goto user;
818
819 ai_sys += 1;
Russell King1e7e3212014-07-04 14:32:43 +0100820 ai_sys_last_pc = (void *)instruction_pointer(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 fixup:
823
George G. Davisc2860d42009-06-04 17:16:04 +0100824 regs->ARM_pc += isize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 switch (CODING_BITS(instr)) {
Steve Longerbeamf21ee2d2005-08-31 21:22:20 +0100827 case 0x00000000: /* 3.13.4 load/store instruction extensions */
828 if (LDSTHD_I_BIT(instr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 offset.un = (instr & 0xf00) >> 4 | (instr & 15);
830 else
831 offset.un = regs->uregs[RM_BITS(instr)];
Steve Longerbeamf21ee2d2005-08-31 21:22:20 +0100832
833 if ((instr & 0x000000f0) == 0x000000b0 || /* LDRH, STRH */
834 (instr & 0x001000f0) == 0x001000f0) /* LDRSH */
835 handler = do_alignment_ldrhstrh;
836 else if ((instr & 0x001000f0) == 0x000000d0 || /* LDRD */
837 (instr & 0x001000f0) == 0x000000f0) /* STRD */
838 handler = do_alignment_ldrdstrd;
George G. Davis19da83f2005-10-10 10:17:44 +0100839 else if ((instr & 0x01f00ff0) == 0x01000090) /* SWP */
840 goto swp;
Steve Longerbeamf21ee2d2005-08-31 21:22:20 +0100841 else
842 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 break;
844
845 case 0x04000000: /* ldr or str immediate */
Robin Murphy5ca918e2014-09-25 11:56:19 +0100846 if (COND_BITS(instr) == 0xf0000000) /* NEON VLDn, VSTn */
847 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 offset.un = OFFSET_BITS(instr);
849 handler = do_alignment_ldrstr;
850 break;
851
852 case 0x06000000: /* ldr or str register */
853 offset.un = regs->uregs[RM_BITS(instr)];
854
855 if (IS_SHIFT(instr)) {
856 unsigned int shiftval = SHIFT_BITS(instr);
857
858 switch(SHIFT_TYPE(instr)) {
859 case SHIFT_LSL:
860 offset.un <<= shiftval;
861 break;
862
863 case SHIFT_LSR:
864 offset.un >>= shiftval;
865 break;
866
867 case SHIFT_ASR:
868 offset.sn >>= shiftval;
869 break;
870
871 case SHIFT_RORRRX:
872 if (shiftval == 0) {
873 offset.un >>= 1;
874 if (regs->ARM_cpsr & PSR_C_BIT)
875 offset.un |= 1 << 31;
876 } else
877 offset.un = offset.un >> shiftval |
878 offset.un << (32 - shiftval);
879 break;
880 }
881 }
882 handler = do_alignment_ldrstr;
883 break;
884
George G. Davisc2860d42009-06-04 17:16:04 +0100885 case 0x08000000: /* ldm or stm, or thumb-2 32bit instruction */
Russell Kinga761ceb2012-09-10 11:50:45 +0100886 if (thumb2_32b) {
887 offset.un = 0;
George G. Davisc2860d42009-06-04 17:16:04 +0100888 handler = do_alignment_t32_to_handler(&instr, regs, &offset);
Arnd Bergmann31d2a632012-10-07 20:35:18 +0000889 } else {
890 offset.un = 0;
George G. Davisc2860d42009-06-04 17:16:04 +0100891 handler = do_alignment_ldmstm;
Arnd Bergmann31d2a632012-10-07 20:35:18 +0000892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 break;
894
895 default:
896 goto bad;
897 }
898
George G. Davisc2860d42009-06-04 17:16:04 +0100899 if (!handler)
900 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 type = handler(addr, instr, regs);
902
George G. Davisc2860d42009-06-04 17:16:04 +0100903 if (type == TYPE_ERROR || type == TYPE_FAULT) {
904 regs->ARM_pc -= isize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 goto bad_or_fault;
George G. Davisc2860d42009-06-04 17:16:04 +0100906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
908 if (type == TYPE_LDST)
909 do_alignment_finish_ldst(addr, instr, regs, offset);
910
911 return 0;
912
913 bad_or_fault:
914 if (type == TYPE_ERROR)
915 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 /*
917 * We got a fault - fix it up, or die.
918 */
Russell Kinge5beac32006-09-27 16:13:48 +0100919 do_bad_area(addr, fsr, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 return 0;
921
George G. Davis19da83f2005-10-10 10:17:44 +0100922 swp:
Russell King4ed89f22014-10-28 11:26:42 +0000923 pr_err("Alignment trap: not handling swp instruction\n");
George G. Davis19da83f2005-10-10 10:17:44 +0100924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 bad:
926 /*
927 * Oops, we didn't handle the instruction.
928 */
Russell King4ed89f22014-10-28 11:26:42 +0000929 pr_err("Alignment trap: not handling instruction "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 "%0*lx at [<%08lx>]\n",
George G. Davisc2860d42009-06-04 17:16:04 +0100931 isize << 1,
932 isize == 2 ? tinstr : instr, instrptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 ai_skipped += 1;
934 return 1;
935
936 user:
937 ai_user += 1;
938
Russell Kingbaa745a2008-12-07 09:44:55 +0000939 if (ai_usermode & UM_WARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 printk("Alignment trap: %s (%d) PC=0x%08lx Instr=0x%0*lx "
941 "Address=0x%08lx FSR 0x%03x\n", current->comm,
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700942 task_pid_nr(current), instrptr,
George G. Davisc2860d42009-06-04 17:16:04 +0100943 isize << 1,
944 isize == 2 ? tinstr : instr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 addr, fsr);
946
Russell Kingbaa745a2008-12-07 09:44:55 +0000947 if (ai_usermode & UM_FIXUP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 goto fixup;
949
Dave Martin2102a652011-07-28 14:29:40 +0100950 if (ai_usermode & UM_SIGNAL) {
Eric W. Biederman3ee6a442018-06-18 14:50:26 -0500951 force_sig_fault(SIGBUS, BUS_ADRALN, (void __user *)addr, current);
Dave Martin2102a652011-07-28 14:29:40 +0100952 } else {
Nicolas Pitre2f27bf82010-09-20 04:10:43 +0100953 /*
954 * We're about to disable the alignment trap and return to
955 * user space. But if an interrupt occurs before actually
956 * reaching user space, then the IRQ vector entry code will
957 * notice that we were still in kernel space and therefore
958 * the alignment trap won't be re-enabled in that case as it
959 * is presumed to be always on from kernel space.
960 * Let's prevent that race by disabling interrupts here (they
961 * are disabled on the way back to user space anyway in
962 * entry-common.S) and disable the alignment trap only if
963 * there is no work pending for this thread.
964 */
965 raw_local_irq_disable();
966 if (!(current_thread_info()->flags & _TIF_WORK_MASK))
967 set_cr(cr_no_alignment);
968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
970 return 0;
971}
972
Russell King175352a2014-04-13 19:00:17 +0100973static int __init noalign_setup(char *__unused)
974{
975 set_cr(__clear_cr(CR_A));
976 return 1;
977}
978__setup("noalign", noalign_setup);
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980/*
981 * This needs to be done after sysctl_init, otherwise sys/ will be
982 * overwritten. Actually, this shouldn't be in sys/ at all since
983 * it isn't a sysctl, and it doesn't contain sysctl information.
984 * We now locate it in /proc/cpu/alignment instead.
985 */
986static int __init alignment_init(void)
987{
988#ifdef CONFIG_PROC_FS
989 struct proc_dir_entry *res;
990
Alexey Dobriyanb7072c62010-05-02 12:40:35 +0300991 res = proc_create("cpu/alignment", S_IWUSR | S_IRUGO, NULL,
992 &alignment_proc_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 if (!res)
994 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995#endif
996
Dave Martin088c01f2011-07-28 14:28:52 +0100997 if (cpu_is_v6_unaligned()) {
Russell Kingb4b20ad82014-04-13 18:57:29 +0100998 set_cr(__clear_cr(CR_A));
Dave Martin088c01f2011-07-28 14:28:52 +0100999 ai_usermode = safe_usermode(ai_usermode, false);
Russell Kingbaa745a2008-12-07 09:44:55 +00001000 }
1001
Russell King0aeb3402014-04-13 19:43:26 +01001002 cr_no_alignment = get_cr() & ~CR_A;
1003
Catalin Marinasf7b81562011-11-22 17:30:31 +00001004 hook_fault_code(FAULT_CODE_ALIGNMENT, do_alignment, SIGBUS, BUS_ADRALN,
Kirill A. Shutemov6338a6a2010-07-22 13:18:19 +01001005 "alignment exception");
Kirill A. Shutemovb8ab5392010-07-26 11:20:41 +01001006
1007 /*
1008 * ARMv6K and ARMv7 use fault status 3 (0b00011) as Access Flag section
1009 * fault, not as alignment error.
1010 *
1011 * TODO: handle ARMv6K properly. Runtime check for 'K' extension is
1012 * needed.
1013 */
1014 if (cpu_architecture() <= CPU_ARCH_ARMv6) {
1015 hook_fault_code(3, do_alignment, SIGBUS, BUS_ADRALN,
1016 "alignment exception");
1017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 return 0;
1020}
1021
1022fs_initcall(alignment_init);