alignment.c 23 KB

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