alignment.c 24 KB

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