alignment.c 25 KB

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