alignment.c 26 KB

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