alignment.c 19 KB

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