alignment.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  1. /*
  2. * linux/arch/arm/mm/alignment.c
  3. *
  4. * Copyright (C) 1995 Linus Torvalds
  5. * Modifications for ARM processor (c) 1995-2001 Russell King
  6. * Thumb alignment fault fixups (c) 2004 MontaVista Software, Inc.
  7. * - Adapted from gdb/sim/arm/thumbemu.c -- Thumb instruction emulation.
  8. * Copyright (C) 1996, Cygnus Software Technologies Ltd.
  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. */
  14. #include <linux/moduleparam.h>
  15. #include <linux/compiler.h>
  16. #include <linux/kernel.h>
  17. #include <linux/errno.h>
  18. #include <linux/string.h>
  19. #include <linux/proc_fs.h>
  20. #include <linux/seq_file.h>
  21. #include <linux/init.h>
  22. #include <linux/sched.h>
  23. #include <linux/uaccess.h>
  24. #include <asm/cp15.h>
  25. #include <asm/system_info.h>
  26. #include <asm/unaligned.h>
  27. #include <asm/opcodes.h>
  28. #include "fault.h"
  29. #include "mm.h"
  30. /*
  31. * 32-bit misaligned trap handler (c) 1998 San Mehat (CCC) -July 1998
  32. * /proc/sys/debug/alignment, modified and integrated into
  33. * Linux 2.1 by Russell King
  34. *
  35. * Speed optimisations and better fault handling by Russell King.
  36. *
  37. * *** NOTE ***
  38. * This code is not portable to processors with late data abort handling.
  39. */
  40. #define CODING_BITS(i) (i & 0x0e000000)
  41. #define COND_BITS(i) (i & 0xf0000000)
  42. #define LDST_I_BIT(i) (i & (1 << 26)) /* Immediate constant */
  43. #define LDST_P_BIT(i) (i & (1 << 24)) /* Preindex */
  44. #define LDST_U_BIT(i) (i & (1 << 23)) /* Add offset */
  45. #define LDST_W_BIT(i) (i & (1 << 21)) /* Writeback */
  46. #define LDST_L_BIT(i) (i & (1 << 20)) /* Load */
  47. #define LDST_P_EQ_U(i) ((((i) ^ ((i) >> 1)) & (1 << 23)) == 0)
  48. #define LDSTHD_I_BIT(i) (i & (1 << 22)) /* double/half-word immed */
  49. #define LDM_S_BIT(i) (i & (1 << 22)) /* write CPSR from SPSR */
  50. #define RN_BITS(i) ((i >> 16) & 15) /* Rn */
  51. #define RD_BITS(i) ((i >> 12) & 15) /* Rd */
  52. #define RM_BITS(i) (i & 15) /* Rm */
  53. #define REGMASK_BITS(i) (i & 0xffff)
  54. #define OFFSET_BITS(i) (i & 0x0fff)
  55. #define IS_SHIFT(i) (i & 0x0ff0)
  56. #define SHIFT_BITS(i) ((i >> 7) & 0x1f)
  57. #define SHIFT_TYPE(i) (i & 0x60)
  58. #define SHIFT_LSL 0x00
  59. #define SHIFT_LSR 0x20
  60. #define SHIFT_ASR 0x40
  61. #define SHIFT_RORRRX 0x60
  62. #define BAD_INSTR 0xdeadc0de
  63. /* Thumb-2 32 bit format per ARMv7 DDI0406A A6.3, either f800h,e800h,f800h */
  64. #define IS_T32(hi16) \
  65. (((hi16) & 0xe000) == 0xe000 && ((hi16) & 0x1800))
  66. static unsigned long ai_user;
  67. static unsigned long ai_sys;
  68. static void *ai_sys_last_pc;
  69. static unsigned long ai_skipped;
  70. static unsigned long ai_half;
  71. static unsigned long ai_word;
  72. static unsigned long ai_dword;
  73. static unsigned long ai_multi;
  74. static int ai_usermode;
  75. static unsigned long cr_no_alignment;
  76. core_param(alignment, ai_usermode, int, 0600);
  77. #define UM_WARN (1 << 0)
  78. #define UM_FIXUP (1 << 1)
  79. #define UM_SIGNAL (1 << 2)
  80. /* Return true if and only if the ARMv6 unaligned access model is in use. */
  81. static bool cpu_is_v6_unaligned(void)
  82. {
  83. return cpu_architecture() >= CPU_ARCH_ARMv6 && get_cr() & CR_U;
  84. }
  85. static int safe_usermode(int new_usermode, bool warn)
  86. {
  87. /*
  88. * ARMv6 and later CPUs can perform unaligned accesses for
  89. * most single load and store instructions up to word size.
  90. * LDM, STM, LDRD and STRD still need to be handled.
  91. *
  92. * Ignoring the alignment fault is not an option on these
  93. * CPUs since we spin re-faulting the instruction without
  94. * making any progress.
  95. */
  96. if (cpu_is_v6_unaligned() && !(new_usermode & (UM_FIXUP | UM_SIGNAL))) {
  97. new_usermode |= UM_FIXUP;
  98. if (warn)
  99. pr_warn("alignment: ignoring faults is unsafe on this CPU. Defaulting to fixup mode.\n");
  100. }
  101. return new_usermode;
  102. }
  103. #ifdef CONFIG_PROC_FS
  104. static const char *usermode_action[] = {
  105. "ignored",
  106. "warn",
  107. "fixup",
  108. "fixup+warn",
  109. "signal",
  110. "signal+warn"
  111. };
  112. static int alignment_proc_show(struct seq_file *m, void *v)
  113. {
  114. seq_printf(m, "User:\t\t%lu\n", ai_user);
  115. seq_printf(m, "System:\t\t%lu (%pF)\n", ai_sys, ai_sys_last_pc);
  116. seq_printf(m, "Skipped:\t%lu\n", ai_skipped);
  117. seq_printf(m, "Half:\t\t%lu\n", ai_half);
  118. seq_printf(m, "Word:\t\t%lu\n", ai_word);
  119. if (cpu_architecture() >= CPU_ARCH_ARMv5TE)
  120. seq_printf(m, "DWord:\t\t%lu\n", ai_dword);
  121. seq_printf(m, "Multi:\t\t%lu\n", ai_multi);
  122. seq_printf(m, "User faults:\t%i (%s)\n", ai_usermode,
  123. usermode_action[ai_usermode]);
  124. return 0;
  125. }
  126. static int alignment_proc_open(struct inode *inode, struct file *file)
  127. {
  128. return single_open(file, alignment_proc_show, NULL);
  129. }
  130. static ssize_t alignment_proc_write(struct file *file, const char __user *buffer,
  131. size_t count, loff_t *pos)
  132. {
  133. char mode;
  134. if (count > 0) {
  135. if (get_user(mode, buffer))
  136. return -EFAULT;
  137. if (mode >= '0' && mode <= '5')
  138. ai_usermode = safe_usermode(mode - '0', true);
  139. }
  140. return count;
  141. }
  142. static const struct file_operations alignment_proc_fops = {
  143. .open = alignment_proc_open,
  144. .read = seq_read,
  145. .llseek = seq_lseek,
  146. .release = single_release,
  147. .write = alignment_proc_write,
  148. };
  149. #endif /* CONFIG_PROC_FS */
  150. union offset_union {
  151. unsigned long un;
  152. signed long sn;
  153. };
  154. #define TYPE_ERROR 0
  155. #define TYPE_FAULT 1
  156. #define TYPE_LDST 2
  157. #define TYPE_DONE 3
  158. #ifdef __ARMEB__
  159. #define BE 1
  160. #define FIRST_BYTE_16 "mov %1, %1, ror #8\n"
  161. #define FIRST_BYTE_32 "mov %1, %1, ror #24\n"
  162. #define NEXT_BYTE "ror #24"
  163. #else
  164. #define BE 0
  165. #define FIRST_BYTE_16
  166. #define FIRST_BYTE_32
  167. #define NEXT_BYTE "lsr #8"
  168. #endif
  169. #define __get8_unaligned_check(ins,val,addr,err) \
  170. __asm__( \
  171. ARM( "1: "ins" %1, [%2], #1\n" ) \
  172. THUMB( "1: "ins" %1, [%2]\n" ) \
  173. THUMB( " add %2, %2, #1\n" ) \
  174. "2:\n" \
  175. " .pushsection .text.fixup,\"ax\"\n" \
  176. " .align 2\n" \
  177. "3: mov %0, #1\n" \
  178. " b 2b\n" \
  179. " .popsection\n" \
  180. " .pushsection __ex_table,\"a\"\n" \
  181. " .align 3\n" \
  182. " .long 1b, 3b\n" \
  183. " .popsection\n" \
  184. : "=r" (err), "=&r" (val), "=r" (addr) \
  185. : "0" (err), "2" (addr))
  186. #define __get16_unaligned_check(ins,val,addr) \
  187. do { \
  188. unsigned int err = 0, v, a = addr; \
  189. __get8_unaligned_check(ins,v,a,err); \
  190. val = v << ((BE) ? 8 : 0); \
  191. __get8_unaligned_check(ins,v,a,err); \
  192. val |= v << ((BE) ? 0 : 8); \
  193. if (err) \
  194. goto fault; \
  195. } while (0)
  196. #define get16_unaligned_check(val,addr) \
  197. __get16_unaligned_check("ldrb",val,addr)
  198. #define get16t_unaligned_check(val,addr) \
  199. __get16_unaligned_check("ldrbt",val,addr)
  200. #define __get32_unaligned_check(ins,val,addr) \
  201. do { \
  202. unsigned int err = 0, v, a = addr; \
  203. __get8_unaligned_check(ins,v,a,err); \
  204. val = v << ((BE) ? 24 : 0); \
  205. __get8_unaligned_check(ins,v,a,err); \
  206. val |= v << ((BE) ? 16 : 8); \
  207. __get8_unaligned_check(ins,v,a,err); \
  208. val |= v << ((BE) ? 8 : 16); \
  209. __get8_unaligned_check(ins,v,a,err); \
  210. val |= v << ((BE) ? 0 : 24); \
  211. if (err) \
  212. goto fault; \
  213. } while (0)
  214. #define get32_unaligned_check(val,addr) \
  215. __get32_unaligned_check("ldrb",val,addr)
  216. #define get32t_unaligned_check(val,addr) \
  217. __get32_unaligned_check("ldrbt",val,addr)
  218. #define __put16_unaligned_check(ins,val,addr) \
  219. do { \
  220. unsigned int err = 0, v = val, a = addr; \
  221. __asm__( FIRST_BYTE_16 \
  222. ARM( "1: "ins" %1, [%2], #1\n" ) \
  223. THUMB( "1: "ins" %1, [%2]\n" ) \
  224. THUMB( " add %2, %2, #1\n" ) \
  225. " mov %1, %1, "NEXT_BYTE"\n" \
  226. "2: "ins" %1, [%2]\n" \
  227. "3:\n" \
  228. " .pushsection .text.fixup,\"ax\"\n" \
  229. " .align 2\n" \
  230. "4: mov %0, #1\n" \
  231. " b 3b\n" \
  232. " .popsection\n" \
  233. " .pushsection __ex_table,\"a\"\n" \
  234. " .align 3\n" \
  235. " .long 1b, 4b\n" \
  236. " .long 2b, 4b\n" \
  237. " .popsection\n" \
  238. : "=r" (err), "=&r" (v), "=&r" (a) \
  239. : "0" (err), "1" (v), "2" (a)); \
  240. if (err) \
  241. goto fault; \
  242. } while (0)
  243. #define put16_unaligned_check(val,addr) \
  244. __put16_unaligned_check("strb",val,addr)
  245. #define put16t_unaligned_check(val,addr) \
  246. __put16_unaligned_check("strbt",val,addr)
  247. #define __put32_unaligned_check(ins,val,addr) \
  248. do { \
  249. unsigned int err = 0, v = val, a = addr; \
  250. __asm__( FIRST_BYTE_32 \
  251. ARM( "1: "ins" %1, [%2], #1\n" ) \
  252. THUMB( "1: "ins" %1, [%2]\n" ) \
  253. THUMB( " add %2, %2, #1\n" ) \
  254. " mov %1, %1, "NEXT_BYTE"\n" \
  255. ARM( "2: "ins" %1, [%2], #1\n" ) \
  256. THUMB( "2: "ins" %1, [%2]\n" ) \
  257. THUMB( " add %2, %2, #1\n" ) \
  258. " mov %1, %1, "NEXT_BYTE"\n" \
  259. ARM( "3: "ins" %1, [%2], #1\n" ) \
  260. THUMB( "3: "ins" %1, [%2]\n" ) \
  261. THUMB( " add %2, %2, #1\n" ) \
  262. " mov %1, %1, "NEXT_BYTE"\n" \
  263. "4: "ins" %1, [%2]\n" \
  264. "5:\n" \
  265. " .pushsection .text.fixup,\"ax\"\n" \
  266. " .align 2\n" \
  267. "6: mov %0, #1\n" \
  268. " b 5b\n" \
  269. " .popsection\n" \
  270. " .pushsection __ex_table,\"a\"\n" \
  271. " .align 3\n" \
  272. " .long 1b, 6b\n" \
  273. " .long 2b, 6b\n" \
  274. " .long 3b, 6b\n" \
  275. " .long 4b, 6b\n" \
  276. " .popsection\n" \
  277. : "=r" (err), "=&r" (v), "=&r" (a) \
  278. : "0" (err), "1" (v), "2" (a)); \
  279. if (err) \
  280. goto fault; \
  281. } while (0)
  282. #define put32_unaligned_check(val,addr) \
  283. __put32_unaligned_check("strb", val, addr)
  284. #define put32t_unaligned_check(val,addr) \
  285. __put32_unaligned_check("strbt", val, addr)
  286. static void
  287. do_alignment_finish_ldst(unsigned long addr, unsigned long instr, struct pt_regs *regs, union offset_union offset)
  288. {
  289. if (!LDST_U_BIT(instr))
  290. offset.un = -offset.un;
  291. if (!LDST_P_BIT(instr))
  292. addr += offset.un;
  293. if (!LDST_P_BIT(instr) || LDST_W_BIT(instr))
  294. regs->uregs[RN_BITS(instr)] = addr;
  295. }
  296. static int
  297. do_alignment_ldrhstrh(unsigned long addr, unsigned long instr, struct pt_regs *regs)
  298. {
  299. unsigned int rd = RD_BITS(instr);
  300. ai_half += 1;
  301. if (user_mode(regs))
  302. goto user;
  303. if (LDST_L_BIT(instr)) {
  304. unsigned long val;
  305. get16_unaligned_check(val, addr);
  306. /* signed half-word? */
  307. if (instr & 0x40)
  308. val = (signed long)((signed short) val);
  309. regs->uregs[rd] = val;
  310. } else
  311. put16_unaligned_check(regs->uregs[rd], addr);
  312. return TYPE_LDST;
  313. user:
  314. if (LDST_L_BIT(instr)) {
  315. unsigned long val;
  316. unsigned int __ua_flags = uaccess_save_and_enable();
  317. get16t_unaligned_check(val, addr);
  318. uaccess_restore(__ua_flags);
  319. /* signed half-word? */
  320. if (instr & 0x40)
  321. val = (signed long)((signed short) val);
  322. regs->uregs[rd] = val;
  323. } else {
  324. unsigned int __ua_flags = uaccess_save_and_enable();
  325. put16t_unaligned_check(regs->uregs[rd], addr);
  326. uaccess_restore(__ua_flags);
  327. }
  328. return TYPE_LDST;
  329. fault:
  330. return TYPE_FAULT;
  331. }
  332. static int
  333. do_alignment_ldrdstrd(unsigned long addr, unsigned long instr,
  334. struct pt_regs *regs)
  335. {
  336. unsigned int rd = RD_BITS(instr);
  337. unsigned int rd2;
  338. int load;
  339. if ((instr & 0xfe000000) == 0xe8000000) {
  340. /* ARMv7 Thumb-2 32-bit LDRD/STRD */
  341. rd2 = (instr >> 8) & 0xf;
  342. load = !!(LDST_L_BIT(instr));
  343. } else if (((rd & 1) == 1) || (rd == 14))
  344. goto bad;
  345. else {
  346. load = ((instr & 0xf0) == 0xd0);
  347. rd2 = rd + 1;
  348. }
  349. ai_dword += 1;
  350. if (user_mode(regs))
  351. goto user;
  352. if (load) {
  353. unsigned long val;
  354. get32_unaligned_check(val, addr);
  355. regs->uregs[rd] = val;
  356. get32_unaligned_check(val, addr + 4);
  357. regs->uregs[rd2] = val;
  358. } else {
  359. put32_unaligned_check(regs->uregs[rd], addr);
  360. put32_unaligned_check(regs->uregs[rd2], addr + 4);
  361. }
  362. return TYPE_LDST;
  363. user:
  364. if (load) {
  365. unsigned long val, val2;
  366. unsigned int __ua_flags = uaccess_save_and_enable();
  367. get32t_unaligned_check(val, addr);
  368. get32t_unaligned_check(val2, addr + 4);
  369. uaccess_restore(__ua_flags);
  370. regs->uregs[rd] = val;
  371. regs->uregs[rd2] = val2;
  372. } else {
  373. unsigned int __ua_flags = uaccess_save_and_enable();
  374. put32t_unaligned_check(regs->uregs[rd], addr);
  375. put32t_unaligned_check(regs->uregs[rd2], addr + 4);
  376. uaccess_restore(__ua_flags);
  377. }
  378. return TYPE_LDST;
  379. bad:
  380. return TYPE_ERROR;
  381. fault:
  382. return TYPE_FAULT;
  383. }
  384. static int
  385. do_alignment_ldrstr(unsigned long addr, unsigned long instr, struct pt_regs *regs)
  386. {
  387. unsigned int rd = RD_BITS(instr);
  388. ai_word += 1;
  389. if ((!LDST_P_BIT(instr) && LDST_W_BIT(instr)) || user_mode(regs))
  390. goto trans;
  391. if (LDST_L_BIT(instr)) {
  392. unsigned int val;
  393. get32_unaligned_check(val, addr);
  394. regs->uregs[rd] = val;
  395. } else
  396. put32_unaligned_check(regs->uregs[rd], addr);
  397. return TYPE_LDST;
  398. trans:
  399. if (LDST_L_BIT(instr)) {
  400. unsigned int val;
  401. unsigned int __ua_flags = uaccess_save_and_enable();
  402. get32t_unaligned_check(val, addr);
  403. uaccess_restore(__ua_flags);
  404. regs->uregs[rd] = val;
  405. } else {
  406. unsigned int __ua_flags = uaccess_save_and_enable();
  407. put32t_unaligned_check(regs->uregs[rd], addr);
  408. uaccess_restore(__ua_flags);
  409. }
  410. return TYPE_LDST;
  411. fault:
  412. return TYPE_FAULT;
  413. }
  414. /*
  415. * LDM/STM alignment handler.
  416. *
  417. * There are 4 variants of this instruction:
  418. *
  419. * B = rn pointer before instruction, A = rn pointer after instruction
  420. * ------ increasing address ----->
  421. * | | r0 | r1 | ... | rx | |
  422. * PU = 01 B A
  423. * PU = 11 B A
  424. * PU = 00 A B
  425. * PU = 10 A B
  426. */
  427. static int
  428. do_alignment_ldmstm(unsigned long addr, unsigned long instr, struct pt_regs *regs)
  429. {
  430. unsigned int rd, rn, correction, nr_regs, regbits;
  431. unsigned long eaddr, newaddr;
  432. if (LDM_S_BIT(instr))
  433. goto bad;
  434. correction = 4; /* processor implementation defined */
  435. regs->ARM_pc += correction;
  436. ai_multi += 1;
  437. /* count the number of registers in the mask to be transferred */
  438. nr_regs = hweight16(REGMASK_BITS(instr)) * 4;
  439. rn = RN_BITS(instr);
  440. newaddr = eaddr = regs->uregs[rn];
  441. if (!LDST_U_BIT(instr))
  442. nr_regs = -nr_regs;
  443. newaddr += nr_regs;
  444. if (!LDST_U_BIT(instr))
  445. eaddr = newaddr;
  446. if (LDST_P_EQ_U(instr)) /* U = P */
  447. eaddr += 4;
  448. /*
  449. * For alignment faults on the ARM922T/ARM920T the MMU makes
  450. * the FSR (and hence addr) equal to the updated base address
  451. * of the multiple access rather than the restored value.
  452. * Switch this message off if we've got a ARM92[02], otherwise
  453. * [ls]dm alignment faults are noisy!
  454. */
  455. #if !(defined CONFIG_CPU_ARM922T) && !(defined CONFIG_CPU_ARM920T)
  456. /*
  457. * This is a "hint" - we already have eaddr worked out by the
  458. * processor for us.
  459. */
  460. if (addr != eaddr) {
  461. pr_err("LDMSTM: PC = %08lx, instr = %08lx, "
  462. "addr = %08lx, eaddr = %08lx\n",
  463. instruction_pointer(regs), instr, addr, eaddr);
  464. show_regs(regs);
  465. }
  466. #endif
  467. if (user_mode(regs)) {
  468. unsigned int __ua_flags = uaccess_save_and_enable();
  469. for (regbits = REGMASK_BITS(instr), rd = 0; regbits;
  470. regbits >>= 1, rd += 1)
  471. if (regbits & 1) {
  472. if (LDST_L_BIT(instr)) {
  473. unsigned int val;
  474. get32t_unaligned_check(val, eaddr);
  475. regs->uregs[rd] = val;
  476. } else
  477. put32t_unaligned_check(regs->uregs[rd], eaddr);
  478. eaddr += 4;
  479. }
  480. uaccess_restore(__ua_flags);
  481. } else {
  482. for (regbits = REGMASK_BITS(instr), rd = 0; regbits;
  483. regbits >>= 1, rd += 1)
  484. if (regbits & 1) {
  485. if (LDST_L_BIT(instr)) {
  486. unsigned int val;
  487. get32_unaligned_check(val, eaddr);
  488. regs->uregs[rd] = val;
  489. } else
  490. put32_unaligned_check(regs->uregs[rd], eaddr);
  491. eaddr += 4;
  492. }
  493. }
  494. if (LDST_W_BIT(instr))
  495. regs->uregs[rn] = newaddr;
  496. if (!LDST_L_BIT(instr) || !(REGMASK_BITS(instr) & (1 << 15)))
  497. regs->ARM_pc -= correction;
  498. return TYPE_DONE;
  499. fault:
  500. regs->ARM_pc -= correction;
  501. return TYPE_FAULT;
  502. bad:
  503. pr_err("Alignment trap: not handling ldm with s-bit set\n");
  504. return TYPE_ERROR;
  505. }
  506. /*
  507. * Convert Thumb ld/st instruction forms to equivalent ARM instructions so
  508. * we can reuse ARM userland alignment fault fixups for Thumb.
  509. *
  510. * This implementation was initially based on the algorithm found in
  511. * gdb/sim/arm/thumbemu.c. It is basically just a code reduction of same
  512. * to convert only Thumb ld/st instruction forms to equivalent ARM forms.
  513. *
  514. * NOTES:
  515. * 1. Comments below refer to ARM ARM DDI0100E Thumb Instruction sections.
  516. * 2. If for some reason we're passed an non-ld/st Thumb instruction to
  517. * decode, we return 0xdeadc0de. This should never happen under normal
  518. * circumstances but if it does, we've got other problems to deal with
  519. * elsewhere and we obviously can't fix those problems here.
  520. */
  521. static unsigned long
  522. thumb2arm(u16 tinstr)
  523. {
  524. u32 L = (tinstr & (1<<11)) >> 11;
  525. switch ((tinstr & 0xf800) >> 11) {
  526. /* 6.5.1 Format 1: */
  527. case 0x6000 >> 11: /* 7.1.52 STR(1) */
  528. case 0x6800 >> 11: /* 7.1.26 LDR(1) */
  529. case 0x7000 >> 11: /* 7.1.55 STRB(1) */
  530. case 0x7800 >> 11: /* 7.1.30 LDRB(1) */
  531. return 0xe5800000 |
  532. ((tinstr & (1<<12)) << (22-12)) | /* fixup */
  533. (L<<20) | /* L==1? */
  534. ((tinstr & (7<<0)) << (12-0)) | /* Rd */
  535. ((tinstr & (7<<3)) << (16-3)) | /* Rn */
  536. ((tinstr & (31<<6)) >> /* immed_5 */
  537. (6 - ((tinstr & (1<<12)) ? 0 : 2)));
  538. case 0x8000 >> 11: /* 7.1.57 STRH(1) */
  539. case 0x8800 >> 11: /* 7.1.32 LDRH(1) */
  540. return 0xe1c000b0 |
  541. (L<<20) | /* L==1? */
  542. ((tinstr & (7<<0)) << (12-0)) | /* Rd */
  543. ((tinstr & (7<<3)) << (16-3)) | /* Rn */
  544. ((tinstr & (7<<6)) >> (6-1)) | /* immed_5[2:0] */
  545. ((tinstr & (3<<9)) >> (9-8)); /* immed_5[4:3] */
  546. /* 6.5.1 Format 2: */
  547. case 0x5000 >> 11:
  548. case 0x5800 >> 11:
  549. {
  550. static const u32 subset[8] = {
  551. 0xe7800000, /* 7.1.53 STR(2) */
  552. 0xe18000b0, /* 7.1.58 STRH(2) */
  553. 0xe7c00000, /* 7.1.56 STRB(2) */
  554. 0xe19000d0, /* 7.1.34 LDRSB */
  555. 0xe7900000, /* 7.1.27 LDR(2) */
  556. 0xe19000b0, /* 7.1.33 LDRH(2) */
  557. 0xe7d00000, /* 7.1.31 LDRB(2) */
  558. 0xe19000f0 /* 7.1.35 LDRSH */
  559. };
  560. return subset[(tinstr & (7<<9)) >> 9] |
  561. ((tinstr & (7<<0)) << (12-0)) | /* Rd */
  562. ((tinstr & (7<<3)) << (16-3)) | /* Rn */
  563. ((tinstr & (7<<6)) >> (6-0)); /* Rm */
  564. }
  565. /* 6.5.1 Format 3: */
  566. case 0x4800 >> 11: /* 7.1.28 LDR(3) */
  567. /* NOTE: This case is not technically possible. We're
  568. * loading 32-bit memory data via PC relative
  569. * addressing mode. So we can and should eliminate
  570. * this case. But I'll leave it here for now.
  571. */
  572. return 0xe59f0000 |
  573. ((tinstr & (7<<8)) << (12-8)) | /* Rd */
  574. ((tinstr & 255) << (2-0)); /* immed_8 */
  575. /* 6.5.1 Format 4: */
  576. case 0x9000 >> 11: /* 7.1.54 STR(3) */
  577. case 0x9800 >> 11: /* 7.1.29 LDR(4) */
  578. return 0xe58d0000 |
  579. (L<<20) | /* L==1? */
  580. ((tinstr & (7<<8)) << (12-8)) | /* Rd */
  581. ((tinstr & 255) << 2); /* immed_8 */
  582. /* 6.6.1 Format 1: */
  583. case 0xc000 >> 11: /* 7.1.51 STMIA */
  584. case 0xc800 >> 11: /* 7.1.25 LDMIA */
  585. {
  586. u32 Rn = (tinstr & (7<<8)) >> 8;
  587. u32 W = ((L<<Rn) & (tinstr&255)) ? 0 : 1<<21;
  588. return 0xe8800000 | W | (L<<20) | (Rn<<16) |
  589. (tinstr&255);
  590. }
  591. /* 6.6.1 Format 2: */
  592. case 0xb000 >> 11: /* 7.1.48 PUSH */
  593. case 0xb800 >> 11: /* 7.1.47 POP */
  594. if ((tinstr & (3 << 9)) == 0x0400) {
  595. static const u32 subset[4] = {
  596. 0xe92d0000, /* STMDB sp!,{registers} */
  597. 0xe92d4000, /* STMDB sp!,{registers,lr} */
  598. 0xe8bd0000, /* LDMIA sp!,{registers} */
  599. 0xe8bd8000 /* LDMIA sp!,{registers,pc} */
  600. };
  601. return subset[(L<<1) | ((tinstr & (1<<8)) >> 8)] |
  602. (tinstr & 255); /* register_list */
  603. }
  604. /* Else fall through for illegal instruction case */
  605. default:
  606. return BAD_INSTR;
  607. }
  608. }
  609. /*
  610. * Convert Thumb-2 32 bit LDM, STM, LDRD, STRD to equivalent instruction
  611. * handlable by ARM alignment handler, also find the corresponding handler,
  612. * so that we can reuse ARM userland alignment fault fixups for Thumb.
  613. *
  614. * @pinstr: original Thumb-2 instruction; returns new handlable instruction
  615. * @regs: register context.
  616. * @poffset: return offset from faulted addr for later writeback
  617. *
  618. * NOTES:
  619. * 1. Comments below refer to ARMv7 DDI0406A Thumb Instruction sections.
  620. * 2. Register name Rt from ARMv7 is same as Rd from ARMv6 (Rd is Rt)
  621. */
  622. static void *
  623. do_alignment_t32_to_handler(unsigned long *pinstr, struct pt_regs *regs,
  624. union offset_union *poffset)
  625. {
  626. unsigned long instr = *pinstr;
  627. u16 tinst1 = (instr >> 16) & 0xffff;
  628. u16 tinst2 = instr & 0xffff;
  629. switch (tinst1 & 0xffe0) {
  630. /* A6.3.5 Load/Store multiple */
  631. case 0xe880: /* STM/STMIA/STMEA,LDM/LDMIA, PUSH/POP T2 */
  632. case 0xe8a0: /* ...above writeback version */
  633. case 0xe900: /* STMDB/STMFD, LDMDB/LDMEA */
  634. case 0xe920: /* ...above writeback version */
  635. /* no need offset decision since handler calculates it */
  636. return do_alignment_ldmstm;
  637. case 0xf840: /* POP/PUSH T3 (single register) */
  638. if (RN_BITS(instr) == 13 && (tinst2 & 0x09ff) == 0x0904) {
  639. u32 L = !!(LDST_L_BIT(instr));
  640. const u32 subset[2] = {
  641. 0xe92d0000, /* STMDB sp!,{registers} */
  642. 0xe8bd0000, /* LDMIA sp!,{registers} */
  643. };
  644. *pinstr = subset[L] | (1<<RD_BITS(instr));
  645. return do_alignment_ldmstm;
  646. }
  647. /* Else fall through for illegal instruction case */
  648. break;
  649. /* A6.3.6 Load/store double, STRD/LDRD(immed, lit, reg) */
  650. case 0xe860:
  651. case 0xe960:
  652. case 0xe8e0:
  653. case 0xe9e0:
  654. poffset->un = (tinst2 & 0xff) << 2;
  655. case 0xe940:
  656. case 0xe9c0:
  657. return do_alignment_ldrdstrd;
  658. /*
  659. * No need to handle load/store instructions up to word size
  660. * since ARMv6 and later CPUs can perform unaligned accesses.
  661. */
  662. default:
  663. break;
  664. }
  665. return NULL;
  666. }
  667. static int
  668. do_alignment(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
  669. {
  670. union offset_union uninitialized_var(offset);
  671. unsigned long instr = 0, instrptr;
  672. int (*handler)(unsigned long addr, unsigned long instr, struct pt_regs *regs);
  673. unsigned int type;
  674. unsigned int fault;
  675. u16 tinstr = 0;
  676. int isize = 4;
  677. int thumb2_32b = 0;
  678. if (interrupts_enabled(regs))
  679. local_irq_enable();
  680. instrptr = instruction_pointer(regs);
  681. if (thumb_mode(regs)) {
  682. u16 *ptr = (u16 *)(instrptr & ~1);
  683. fault = probe_kernel_address(ptr, tinstr);
  684. tinstr = __mem_to_opcode_thumb16(tinstr);
  685. if (!fault) {
  686. if (cpu_architecture() >= CPU_ARCH_ARMv7 &&
  687. IS_T32(tinstr)) {
  688. /* Thumb-2 32-bit */
  689. u16 tinst2 = 0;
  690. fault = probe_kernel_address(ptr + 1, tinst2);
  691. tinst2 = __mem_to_opcode_thumb16(tinst2);
  692. instr = __opcode_thumb32_compose(tinstr, tinst2);
  693. thumb2_32b = 1;
  694. } else {
  695. isize = 2;
  696. instr = thumb2arm(tinstr);
  697. }
  698. }
  699. } else {
  700. fault = probe_kernel_address((void *)instrptr, instr);
  701. instr = __mem_to_opcode_arm(instr);
  702. }
  703. if (fault) {
  704. type = TYPE_FAULT;
  705. goto bad_or_fault;
  706. }
  707. if (user_mode(regs))
  708. goto user;
  709. ai_sys += 1;
  710. ai_sys_last_pc = (void *)instruction_pointer(regs);
  711. fixup:
  712. regs->ARM_pc += isize;
  713. switch (CODING_BITS(instr)) {
  714. case 0x00000000: /* 3.13.4 load/store instruction extensions */
  715. if (LDSTHD_I_BIT(instr))
  716. offset.un = (instr & 0xf00) >> 4 | (instr & 15);
  717. else
  718. offset.un = regs->uregs[RM_BITS(instr)];
  719. if ((instr & 0x000000f0) == 0x000000b0 || /* LDRH, STRH */
  720. (instr & 0x001000f0) == 0x001000f0) /* LDRSH */
  721. handler = do_alignment_ldrhstrh;
  722. else if ((instr & 0x001000f0) == 0x000000d0 || /* LDRD */
  723. (instr & 0x001000f0) == 0x000000f0) /* STRD */
  724. handler = do_alignment_ldrdstrd;
  725. else if ((instr & 0x01f00ff0) == 0x01000090) /* SWP */
  726. goto swp;
  727. else
  728. goto bad;
  729. break;
  730. case 0x04000000: /* ldr or str immediate */
  731. if (COND_BITS(instr) == 0xf0000000) /* NEON VLDn, VSTn */
  732. goto bad;
  733. offset.un = OFFSET_BITS(instr);
  734. handler = do_alignment_ldrstr;
  735. break;
  736. case 0x06000000: /* ldr or str register */
  737. offset.un = regs->uregs[RM_BITS(instr)];
  738. if (IS_SHIFT(instr)) {
  739. unsigned int shiftval = SHIFT_BITS(instr);
  740. switch(SHIFT_TYPE(instr)) {
  741. case SHIFT_LSL:
  742. offset.un <<= shiftval;
  743. break;
  744. case SHIFT_LSR:
  745. offset.un >>= shiftval;
  746. break;
  747. case SHIFT_ASR:
  748. offset.sn >>= shiftval;
  749. break;
  750. case SHIFT_RORRRX:
  751. if (shiftval == 0) {
  752. offset.un >>= 1;
  753. if (regs->ARM_cpsr & PSR_C_BIT)
  754. offset.un |= 1 << 31;
  755. } else
  756. offset.un = offset.un >> shiftval |
  757. offset.un << (32 - shiftval);
  758. break;
  759. }
  760. }
  761. handler = do_alignment_ldrstr;
  762. break;
  763. case 0x08000000: /* ldm or stm, or thumb-2 32bit instruction */
  764. if (thumb2_32b) {
  765. offset.un = 0;
  766. handler = do_alignment_t32_to_handler(&instr, regs, &offset);
  767. } else {
  768. offset.un = 0;
  769. handler = do_alignment_ldmstm;
  770. }
  771. break;
  772. default:
  773. goto bad;
  774. }
  775. if (!handler)
  776. goto bad;
  777. type = handler(addr, instr, regs);
  778. if (type == TYPE_ERROR || type == TYPE_FAULT) {
  779. regs->ARM_pc -= isize;
  780. goto bad_or_fault;
  781. }
  782. if (type == TYPE_LDST)
  783. do_alignment_finish_ldst(addr, instr, regs, offset);
  784. return 0;
  785. bad_or_fault:
  786. if (type == TYPE_ERROR)
  787. goto bad;
  788. /*
  789. * We got a fault - fix it up, or die.
  790. */
  791. do_bad_area(addr, fsr, regs);
  792. return 0;
  793. swp:
  794. pr_err("Alignment trap: not handling swp instruction\n");
  795. bad:
  796. /*
  797. * Oops, we didn't handle the instruction.
  798. */
  799. pr_err("Alignment trap: not handling instruction "
  800. "%0*lx at [<%08lx>]\n",
  801. isize << 1,
  802. isize == 2 ? tinstr : instr, instrptr);
  803. ai_skipped += 1;
  804. return 1;
  805. user:
  806. ai_user += 1;
  807. if (ai_usermode & UM_WARN)
  808. printk("Alignment trap: %s (%d) PC=0x%08lx Instr=0x%0*lx "
  809. "Address=0x%08lx FSR 0x%03x\n", current->comm,
  810. task_pid_nr(current), instrptr,
  811. isize << 1,
  812. isize == 2 ? tinstr : instr,
  813. addr, fsr);
  814. if (ai_usermode & UM_FIXUP)
  815. goto fixup;
  816. if (ai_usermode & UM_SIGNAL) {
  817. siginfo_t si;
  818. si.si_signo = SIGBUS;
  819. si.si_errno = 0;
  820. si.si_code = BUS_ADRALN;
  821. si.si_addr = (void __user *)addr;
  822. force_sig_info(si.si_signo, &si, current);
  823. } else {
  824. /*
  825. * We're about to disable the alignment trap and return to
  826. * user space. But if an interrupt occurs before actually
  827. * reaching user space, then the IRQ vector entry code will
  828. * notice that we were still in kernel space and therefore
  829. * the alignment trap won't be re-enabled in that case as it
  830. * is presumed to be always on from kernel space.
  831. * Let's prevent that race by disabling interrupts here (they
  832. * are disabled on the way back to user space anyway in
  833. * entry-common.S) and disable the alignment trap only if
  834. * there is no work pending for this thread.
  835. */
  836. raw_local_irq_disable();
  837. if (!(current_thread_info()->flags & _TIF_WORK_MASK))
  838. set_cr(cr_no_alignment);
  839. }
  840. return 0;
  841. }
  842. static int __init noalign_setup(char *__unused)
  843. {
  844. set_cr(__clear_cr(CR_A));
  845. return 1;
  846. }
  847. __setup("noalign", noalign_setup);
  848. /*
  849. * This needs to be done after sysctl_init, otherwise sys/ will be
  850. * overwritten. Actually, this shouldn't be in sys/ at all since
  851. * it isn't a sysctl, and it doesn't contain sysctl information.
  852. * We now locate it in /proc/cpu/alignment instead.
  853. */
  854. static int __init alignment_init(void)
  855. {
  856. #ifdef CONFIG_PROC_FS
  857. struct proc_dir_entry *res;
  858. res = proc_create("cpu/alignment", S_IWUSR | S_IRUGO, NULL,
  859. &alignment_proc_fops);
  860. if (!res)
  861. return -ENOMEM;
  862. #endif
  863. if (cpu_is_v6_unaligned()) {
  864. set_cr(__clear_cr(CR_A));
  865. ai_usermode = safe_usermode(ai_usermode, false);
  866. }
  867. cr_no_alignment = get_cr() & ~CR_A;
  868. hook_fault_code(FAULT_CODE_ALIGNMENT, do_alignment, SIGBUS, BUS_ADRALN,
  869. "alignment exception");
  870. /*
  871. * ARMv6K and ARMv7 use fault status 3 (0b00011) as Access Flag section
  872. * fault, not as alignment error.
  873. *
  874. * TODO: handle ARMv6K properly. Runtime check for 'K' extension is
  875. * needed.
  876. */
  877. if (cpu_architecture() <= CPU_ARCH_ARMv6) {
  878. hook_fault_code(3, do_alignment, SIGBUS, BUS_ADRALN,
  879. "alignment exception");
  880. }
  881. return 0;
  882. }
  883. fs_initcall(alignment_init);