alignment.c 26 KB

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