alignment.c 24 KB

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