alignment.c 20 KB

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