alignment.c 21 KB

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