alignment.c 26 KB

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