alignment.c 26 KB

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