alignment.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/arch/arm/mm/alignment.c
  4. *
  5. * Copyright (C) 1995 Linus Torvalds
  6. * Modifications for ARM processor (c) 1995-2001 Russell King
  7. * Thumb alignment fault fixups (c) 2004 MontaVista Software, Inc.
  8. * - Adapted from gdb/sim/arm/thumbemu.c -- Thumb instruction emulation.
  9. * Copyright (C) 1996, Cygnus Software Technologies Ltd.
  10. */
  11. #include <linux/moduleparam.h>
  12. #include <linux/compiler.h>
  13. #include <linux/kernel.h>
  14. #include <linux/sched/debug.h>
  15. #include <linux/errno.h>
  16. #include <linux/string.h>
  17. #include <linux/proc_fs.h>
  18. #include <linux/seq_file.h>
  19. #include <linux/init.h>
  20. #include <linux/sched/signal.h>
  21. #include <linux/uaccess.h>
  22. #include <asm/cp15.h>
  23. #include <asm/system_info.h>
  24. #include <asm/unaligned.h>
  25. #include <asm/opcodes.h>
  26. #include "fault.h"
  27. #include "mm.h"
  28. /*
  29. * 32-bit misaligned trap handler (c) 1998 San Mehat (CCC) -July 1998
  30. * /proc/sys/debug/alignment, modified and integrated into
  31. * Linux 2.1 by Russell King
  32. *
  33. * Speed optimisations and better fault handling by Russell King.
  34. *
  35. * *** NOTE ***
  36. * This code is not portable to processors with late data abort handling.
  37. */
  38. #define CODING_BITS(i) (i & 0x0e000000)
  39. #define COND_BITS(i) (i & 0xf0000000)
  40. #define LDST_I_BIT(i) (i & (1 << 26)) /* Immediate constant */
  41. #define LDST_P_BIT(i) (i & (1 << 24)) /* Preindex */
  42. #define LDST_U_BIT(i) (i & (1 << 23)) /* Add offset */
  43. #define LDST_W_BIT(i) (i & (1 << 21)) /* Writeback */
  44. #define LDST_L_BIT(i) (i & (1 << 20)) /* Load */
  45. #define LDST_P_EQ_U(i) ((((i) ^ ((i) >> 1)) & (1 << 23)) == 0)
  46. #define LDSTHD_I_BIT(i) (i & (1 << 22)) /* double/half-word immed */
  47. #define LDM_S_BIT(i) (i & (1 << 22)) /* write CPSR from SPSR */
  48. #define RN_BITS(i) ((i >> 16) & 15) /* Rn */
  49. #define RD_BITS(i) ((i >> 12) & 15) /* Rd */
  50. #define RM_BITS(i) (i & 15) /* Rm */
  51. #define REGMASK_BITS(i) (i & 0xffff)
  52. #define OFFSET_BITS(i) (i & 0x0fff)
  53. #define IS_SHIFT(i) (i & 0x0ff0)
  54. #define SHIFT_BITS(i) ((i >> 7) & 0x1f)
  55. #define SHIFT_TYPE(i) (i & 0x60)
  56. #define SHIFT_LSL 0x00
  57. #define SHIFT_LSR 0x20
  58. #define SHIFT_ASR 0x40
  59. #define SHIFT_RORRRX 0x60
  60. #define BAD_INSTR 0xdeadc0de
  61. /* Thumb-2 32 bit format per ARMv7 DDI0406A A6.3, either f800h,e800h,f800h */
  62. #define IS_T32(hi16) \
  63. (((hi16) & 0xe000) == 0xe000 && ((hi16) & 0x1800))
  64. static unsigned long ai_user;
  65. static unsigned long ai_sys;
  66. static void *ai_sys_last_pc;
  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. pr_warn("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 (%pS)\n", ai_sys, ai_sys_last_pc);
  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 proc_ops alignment_proc_ops = {
  141. .proc_open = alignment_proc_open,
  142. .proc_read = seq_read,
  143. .proc_lseek = seq_lseek,
  144. .proc_release = single_release,
  145. .proc_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 .text.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 .text.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 .text.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, u32 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, u32 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. unsigned int __ua_flags = uaccess_save_and_enable();
  315. get16t_unaligned_check(val, addr);
  316. uaccess_restore(__ua_flags);
  317. /* signed half-word? */
  318. if (instr & 0x40)
  319. val = (signed long)((signed short) val);
  320. regs->uregs[rd] = val;
  321. } else {
  322. unsigned int __ua_flags = uaccess_save_and_enable();
  323. put16t_unaligned_check(regs->uregs[rd], addr);
  324. uaccess_restore(__ua_flags);
  325. }
  326. return TYPE_LDST;
  327. fault:
  328. return TYPE_FAULT;
  329. }
  330. static int
  331. do_alignment_ldrdstrd(unsigned long addr, u32 instr, struct pt_regs *regs)
  332. {
  333. unsigned int rd = RD_BITS(instr);
  334. unsigned int rd2;
  335. int load;
  336. if ((instr & 0xfe000000) == 0xe8000000) {
  337. /* ARMv7 Thumb-2 32-bit LDRD/STRD */
  338. rd2 = (instr >> 8) & 0xf;
  339. load = !!(LDST_L_BIT(instr));
  340. } else if (((rd & 1) == 1) || (rd == 14))
  341. goto bad;
  342. else {
  343. load = ((instr & 0xf0) == 0xd0);
  344. rd2 = rd + 1;
  345. }
  346. ai_dword += 1;
  347. if (user_mode(regs))
  348. goto user;
  349. if (load) {
  350. unsigned long val;
  351. get32_unaligned_check(val, addr);
  352. regs->uregs[rd] = val;
  353. get32_unaligned_check(val, addr + 4);
  354. regs->uregs[rd2] = val;
  355. } else {
  356. put32_unaligned_check(regs->uregs[rd], addr);
  357. put32_unaligned_check(regs->uregs[rd2], addr + 4);
  358. }
  359. return TYPE_LDST;
  360. user:
  361. if (load) {
  362. unsigned long val, val2;
  363. unsigned int __ua_flags = uaccess_save_and_enable();
  364. get32t_unaligned_check(val, addr);
  365. get32t_unaligned_check(val2, addr + 4);
  366. uaccess_restore(__ua_flags);
  367. regs->uregs[rd] = val;
  368. regs->uregs[rd2] = val2;
  369. } else {
  370. unsigned int __ua_flags = uaccess_save_and_enable();
  371. put32t_unaligned_check(regs->uregs[rd], addr);
  372. put32t_unaligned_check(regs->uregs[rd2], addr + 4);
  373. uaccess_restore(__ua_flags);
  374. }
  375. return TYPE_LDST;
  376. bad:
  377. return TYPE_ERROR;
  378. fault:
  379. return TYPE_FAULT;
  380. }
  381. static int
  382. do_alignment_ldrstr(unsigned long addr, u32 instr, struct pt_regs *regs)
  383. {
  384. unsigned int rd = RD_BITS(instr);
  385. ai_word += 1;
  386. if ((!LDST_P_BIT(instr) && LDST_W_BIT(instr)) || user_mode(regs))
  387. goto trans;
  388. if (LDST_L_BIT(instr)) {
  389. unsigned int val;
  390. get32_unaligned_check(val, addr);
  391. regs->uregs[rd] = val;
  392. } else
  393. put32_unaligned_check(regs->uregs[rd], addr);
  394. return TYPE_LDST;
  395. trans:
  396. if (LDST_L_BIT(instr)) {
  397. unsigned int val;
  398. unsigned int __ua_flags = uaccess_save_and_enable();
  399. get32t_unaligned_check(val, addr);
  400. uaccess_restore(__ua_flags);
  401. regs->uregs[rd] = val;
  402. } else {
  403. unsigned int __ua_flags = uaccess_save_and_enable();
  404. put32t_unaligned_check(regs->uregs[rd], addr);
  405. uaccess_restore(__ua_flags);
  406. }
  407. return TYPE_LDST;
  408. fault:
  409. return TYPE_FAULT;
  410. }
  411. /*
  412. * LDM/STM alignment handler.
  413. *
  414. * There are 4 variants of this instruction:
  415. *
  416. * B = rn pointer before instruction, A = rn pointer after instruction
  417. * ------ increasing address ----->
  418. * | | r0 | r1 | ... | rx | |
  419. * PU = 01 B A
  420. * PU = 11 B A
  421. * PU = 00 A B
  422. * PU = 10 A B
  423. */
  424. static int
  425. do_alignment_ldmstm(unsigned long addr, u32 instr, struct pt_regs *regs)
  426. {
  427. unsigned int rd, rn, correction, nr_regs, regbits;
  428. unsigned long eaddr, newaddr;
  429. if (LDM_S_BIT(instr))
  430. goto bad;
  431. correction = 4; /* processor implementation defined */
  432. regs->ARM_pc += correction;
  433. ai_multi += 1;
  434. /* count the number of registers in the mask to be transferred */
  435. nr_regs = hweight16(REGMASK_BITS(instr)) * 4;
  436. rn = RN_BITS(instr);
  437. newaddr = eaddr = regs->uregs[rn];
  438. if (!LDST_U_BIT(instr))
  439. nr_regs = -nr_regs;
  440. newaddr += nr_regs;
  441. if (!LDST_U_BIT(instr))
  442. eaddr = newaddr;
  443. if (LDST_P_EQ_U(instr)) /* U = P */
  444. eaddr += 4;
  445. /*
  446. * For alignment faults on the ARM922T/ARM920T the MMU makes
  447. * the FSR (and hence addr) equal to the updated base address
  448. * of the multiple access rather than the restored value.
  449. * Switch this message off if we've got a ARM92[02], otherwise
  450. * [ls]dm alignment faults are noisy!
  451. */
  452. #if !(defined CONFIG_CPU_ARM922T) && !(defined CONFIG_CPU_ARM920T)
  453. /*
  454. * This is a "hint" - we already have eaddr worked out by the
  455. * processor for us.
  456. */
  457. if (addr != eaddr) {
  458. pr_err("LDMSTM: PC = %08lx, instr = %08x, "
  459. "addr = %08lx, eaddr = %08lx\n",
  460. instruction_pointer(regs), instr, addr, eaddr);
  461. show_regs(regs);
  462. }
  463. #endif
  464. if (user_mode(regs)) {
  465. unsigned int __ua_flags = uaccess_save_and_enable();
  466. for (regbits = REGMASK_BITS(instr), rd = 0; regbits;
  467. regbits >>= 1, rd += 1)
  468. if (regbits & 1) {
  469. if (LDST_L_BIT(instr)) {
  470. unsigned int val;
  471. get32t_unaligned_check(val, eaddr);
  472. regs->uregs[rd] = val;
  473. } else
  474. put32t_unaligned_check(regs->uregs[rd], eaddr);
  475. eaddr += 4;
  476. }
  477. uaccess_restore(__ua_flags);
  478. } else {
  479. for (regbits = REGMASK_BITS(instr), rd = 0; regbits;
  480. regbits >>= 1, rd += 1)
  481. if (regbits & 1) {
  482. if (LDST_L_BIT(instr)) {
  483. unsigned int val;
  484. get32_unaligned_check(val, eaddr);
  485. regs->uregs[rd] = val;
  486. } else
  487. put32_unaligned_check(regs->uregs[rd], eaddr);
  488. eaddr += 4;
  489. }
  490. }
  491. if (LDST_W_BIT(instr))
  492. regs->uregs[rn] = newaddr;
  493. if (!LDST_L_BIT(instr) || !(REGMASK_BITS(instr) & (1 << 15)))
  494. regs->ARM_pc -= correction;
  495. return TYPE_DONE;
  496. fault:
  497. regs->ARM_pc -= correction;
  498. return TYPE_FAULT;
  499. bad:
  500. pr_err("Alignment trap: not handling ldm with s-bit set\n");
  501. return TYPE_ERROR;
  502. }
  503. /*
  504. * Convert Thumb ld/st instruction forms to equivalent ARM instructions so
  505. * we can reuse ARM userland alignment fault fixups for Thumb.
  506. *
  507. * This implementation was initially based on the algorithm found in
  508. * gdb/sim/arm/thumbemu.c. It is basically just a code reduction of same
  509. * to convert only Thumb ld/st instruction forms to equivalent ARM forms.
  510. *
  511. * NOTES:
  512. * 1. Comments below refer to ARM ARM DDI0100E Thumb Instruction sections.
  513. * 2. If for some reason we're passed an non-ld/st Thumb instruction to
  514. * decode, we return 0xdeadc0de. This should never happen under normal
  515. * circumstances but if it does, we've got other problems to deal with
  516. * elsewhere and we obviously can't fix those problems here.
  517. */
  518. static unsigned long
  519. thumb2arm(u16 tinstr)
  520. {
  521. u32 L = (tinstr & (1<<11)) >> 11;
  522. switch ((tinstr & 0xf800) >> 11) {
  523. /* 6.5.1 Format 1: */
  524. case 0x6000 >> 11: /* 7.1.52 STR(1) */
  525. case 0x6800 >> 11: /* 7.1.26 LDR(1) */
  526. case 0x7000 >> 11: /* 7.1.55 STRB(1) */
  527. case 0x7800 >> 11: /* 7.1.30 LDRB(1) */
  528. return 0xe5800000 |
  529. ((tinstr & (1<<12)) << (22-12)) | /* fixup */
  530. (L<<20) | /* L==1? */
  531. ((tinstr & (7<<0)) << (12-0)) | /* Rd */
  532. ((tinstr & (7<<3)) << (16-3)) | /* Rn */
  533. ((tinstr & (31<<6)) >> /* immed_5 */
  534. (6 - ((tinstr & (1<<12)) ? 0 : 2)));
  535. case 0x8000 >> 11: /* 7.1.57 STRH(1) */
  536. case 0x8800 >> 11: /* 7.1.32 LDRH(1) */
  537. return 0xe1c000b0 |
  538. (L<<20) | /* L==1? */
  539. ((tinstr & (7<<0)) << (12-0)) | /* Rd */
  540. ((tinstr & (7<<3)) << (16-3)) | /* Rn */
  541. ((tinstr & (7<<6)) >> (6-1)) | /* immed_5[2:0] */
  542. ((tinstr & (3<<9)) >> (9-8)); /* immed_5[4:3] */
  543. /* 6.5.1 Format 2: */
  544. case 0x5000 >> 11:
  545. case 0x5800 >> 11:
  546. {
  547. static const u32 subset[8] = {
  548. 0xe7800000, /* 7.1.53 STR(2) */
  549. 0xe18000b0, /* 7.1.58 STRH(2) */
  550. 0xe7c00000, /* 7.1.56 STRB(2) */
  551. 0xe19000d0, /* 7.1.34 LDRSB */
  552. 0xe7900000, /* 7.1.27 LDR(2) */
  553. 0xe19000b0, /* 7.1.33 LDRH(2) */
  554. 0xe7d00000, /* 7.1.31 LDRB(2) */
  555. 0xe19000f0 /* 7.1.35 LDRSH */
  556. };
  557. return subset[(tinstr & (7<<9)) >> 9] |
  558. ((tinstr & (7<<0)) << (12-0)) | /* Rd */
  559. ((tinstr & (7<<3)) << (16-3)) | /* Rn */
  560. ((tinstr & (7<<6)) >> (6-0)); /* Rm */
  561. }
  562. /* 6.5.1 Format 3: */
  563. case 0x4800 >> 11: /* 7.1.28 LDR(3) */
  564. /* NOTE: This case is not technically possible. We're
  565. * loading 32-bit memory data via PC relative
  566. * addressing mode. So we can and should eliminate
  567. * this case. But I'll leave it here for now.
  568. */
  569. return 0xe59f0000 |
  570. ((tinstr & (7<<8)) << (12-8)) | /* Rd */
  571. ((tinstr & 255) << (2-0)); /* immed_8 */
  572. /* 6.5.1 Format 4: */
  573. case 0x9000 >> 11: /* 7.1.54 STR(3) */
  574. case 0x9800 >> 11: /* 7.1.29 LDR(4) */
  575. return 0xe58d0000 |
  576. (L<<20) | /* L==1? */
  577. ((tinstr & (7<<8)) << (12-8)) | /* Rd */
  578. ((tinstr & 255) << 2); /* immed_8 */
  579. /* 6.6.1 Format 1: */
  580. case 0xc000 >> 11: /* 7.1.51 STMIA */
  581. case 0xc800 >> 11: /* 7.1.25 LDMIA */
  582. {
  583. u32 Rn = (tinstr & (7<<8)) >> 8;
  584. u32 W = ((L<<Rn) & (tinstr&255)) ? 0 : 1<<21;
  585. return 0xe8800000 | W | (L<<20) | (Rn<<16) |
  586. (tinstr&255);
  587. }
  588. /* 6.6.1 Format 2: */
  589. case 0xb000 >> 11: /* 7.1.48 PUSH */
  590. case 0xb800 >> 11: /* 7.1.47 POP */
  591. if ((tinstr & (3 << 9)) == 0x0400) {
  592. static const u32 subset[4] = {
  593. 0xe92d0000, /* STMDB sp!,{registers} */
  594. 0xe92d4000, /* STMDB sp!,{registers,lr} */
  595. 0xe8bd0000, /* LDMIA sp!,{registers} */
  596. 0xe8bd8000 /* LDMIA sp!,{registers,pc} */
  597. };
  598. return subset[(L<<1) | ((tinstr & (1<<8)) >> 8)] |
  599. (tinstr & 255); /* register_list */
  600. }
  601. fallthrough; /* for illegal instruction case */
  602. default:
  603. return BAD_INSTR;
  604. }
  605. }
  606. /*
  607. * Convert Thumb-2 32 bit LDM, STM, LDRD, STRD to equivalent instruction
  608. * handlable by ARM alignment handler, also find the corresponding handler,
  609. * so that we can reuse ARM userland alignment fault fixups for Thumb.
  610. *
  611. * @pinstr: original Thumb-2 instruction; returns new handlable instruction
  612. * @regs: register context.
  613. * @poffset: return offset from faulted addr for later writeback
  614. *
  615. * NOTES:
  616. * 1. Comments below refer to ARMv7 DDI0406A Thumb Instruction sections.
  617. * 2. Register name Rt from ARMv7 is same as Rd from ARMv6 (Rd is Rt)
  618. */
  619. static void *
  620. do_alignment_t32_to_handler(u32 *pinstr, struct pt_regs *regs,
  621. union offset_union *poffset)
  622. {
  623. u32 instr = *pinstr;
  624. u16 tinst1 = (instr >> 16) & 0xffff;
  625. u16 tinst2 = instr & 0xffff;
  626. switch (tinst1 & 0xffe0) {
  627. /* A6.3.5 Load/Store multiple */
  628. case 0xe880: /* STM/STMIA/STMEA,LDM/LDMIA, PUSH/POP T2 */
  629. case 0xe8a0: /* ...above writeback version */
  630. case 0xe900: /* STMDB/STMFD, LDMDB/LDMEA */
  631. case 0xe920: /* ...above writeback version */
  632. /* no need offset decision since handler calculates it */
  633. return do_alignment_ldmstm;
  634. case 0xf840: /* POP/PUSH T3 (single register) */
  635. if (RN_BITS(instr) == 13 && (tinst2 & 0x09ff) == 0x0904) {
  636. u32 L = !!(LDST_L_BIT(instr));
  637. const u32 subset[2] = {
  638. 0xe92d0000, /* STMDB sp!,{registers} */
  639. 0xe8bd0000, /* LDMIA sp!,{registers} */
  640. };
  641. *pinstr = subset[L] | (1<<RD_BITS(instr));
  642. return do_alignment_ldmstm;
  643. }
  644. /* Else fall through for illegal instruction case */
  645. break;
  646. /* A6.3.6 Load/store double, STRD/LDRD(immed, lit, reg) */
  647. case 0xe860:
  648. case 0xe960:
  649. case 0xe8e0:
  650. case 0xe9e0:
  651. poffset->un = (tinst2 & 0xff) << 2;
  652. fallthrough;
  653. case 0xe940:
  654. case 0xe9c0:
  655. return do_alignment_ldrdstrd;
  656. /*
  657. * No need to handle load/store instructions up to word size
  658. * since ARMv6 and later CPUs can perform unaligned accesses.
  659. */
  660. default:
  661. break;
  662. }
  663. return NULL;
  664. }
  665. static int alignment_get_arm(struct pt_regs *regs, u32 *ip, u32 *inst)
  666. {
  667. u32 instr = 0;
  668. int fault;
  669. if (user_mode(regs))
  670. fault = get_user(instr, ip);
  671. else
  672. fault = get_kernel_nofault(instr, ip);
  673. *inst = __mem_to_opcode_arm(instr);
  674. return fault;
  675. }
  676. static int alignment_get_thumb(struct pt_regs *regs, u16 *ip, u16 *inst)
  677. {
  678. u16 instr = 0;
  679. int fault;
  680. if (user_mode(regs))
  681. fault = get_user(instr, ip);
  682. else
  683. fault = get_kernel_nofault(instr, ip);
  684. *inst = __mem_to_opcode_thumb16(instr);
  685. return fault;
  686. }
  687. static int
  688. do_alignment(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
  689. {
  690. union offset_union offset;
  691. unsigned long instrptr;
  692. int (*handler)(unsigned long addr, u32 instr, struct pt_regs *regs);
  693. unsigned int type;
  694. u32 instr = 0;
  695. u16 tinstr = 0;
  696. int isize = 4;
  697. int thumb2_32b = 0;
  698. int fault;
  699. if (interrupts_enabled(regs))
  700. local_irq_enable();
  701. instrptr = instruction_pointer(regs);
  702. if (thumb_mode(regs)) {
  703. u16 *ptr = (u16 *)(instrptr & ~1);
  704. fault = alignment_get_thumb(regs, ptr, &tinstr);
  705. if (!fault) {
  706. if (cpu_architecture() >= CPU_ARCH_ARMv7 &&
  707. IS_T32(tinstr)) {
  708. /* Thumb-2 32-bit */
  709. u16 tinst2;
  710. fault = alignment_get_thumb(regs, ptr + 1, &tinst2);
  711. instr = __opcode_thumb32_compose(tinstr, tinst2);
  712. thumb2_32b = 1;
  713. } else {
  714. isize = 2;
  715. instr = thumb2arm(tinstr);
  716. }
  717. }
  718. } else {
  719. fault = alignment_get_arm(regs, (void *)instrptr, &instr);
  720. }
  721. if (fault) {
  722. type = TYPE_FAULT;
  723. goto bad_or_fault;
  724. }
  725. if (user_mode(regs))
  726. goto user;
  727. ai_sys += 1;
  728. ai_sys_last_pc = (void *)instruction_pointer(regs);
  729. fixup:
  730. regs->ARM_pc += isize;
  731. switch (CODING_BITS(instr)) {
  732. case 0x00000000: /* 3.13.4 load/store instruction extensions */
  733. if (LDSTHD_I_BIT(instr))
  734. offset.un = (instr & 0xf00) >> 4 | (instr & 15);
  735. else
  736. offset.un = regs->uregs[RM_BITS(instr)];
  737. if ((instr & 0x000000f0) == 0x000000b0 || /* LDRH, STRH */
  738. (instr & 0x001000f0) == 0x001000f0) /* LDRSH */
  739. handler = do_alignment_ldrhstrh;
  740. else if ((instr & 0x001000f0) == 0x000000d0 || /* LDRD */
  741. (instr & 0x001000f0) == 0x000000f0) /* STRD */
  742. handler = do_alignment_ldrdstrd;
  743. else if ((instr & 0x01f00ff0) == 0x01000090) /* SWP */
  744. goto swp;
  745. else
  746. goto bad;
  747. break;
  748. case 0x04000000: /* ldr or str immediate */
  749. if (COND_BITS(instr) == 0xf0000000) /* NEON VLDn, VSTn */
  750. goto bad;
  751. offset.un = OFFSET_BITS(instr);
  752. handler = do_alignment_ldrstr;
  753. break;
  754. case 0x06000000: /* ldr or str register */
  755. offset.un = regs->uregs[RM_BITS(instr)];
  756. if (IS_SHIFT(instr)) {
  757. unsigned int shiftval = SHIFT_BITS(instr);
  758. switch(SHIFT_TYPE(instr)) {
  759. case SHIFT_LSL:
  760. offset.un <<= shiftval;
  761. break;
  762. case SHIFT_LSR:
  763. offset.un >>= shiftval;
  764. break;
  765. case SHIFT_ASR:
  766. offset.sn >>= shiftval;
  767. break;
  768. case SHIFT_RORRRX:
  769. if (shiftval == 0) {
  770. offset.un >>= 1;
  771. if (regs->ARM_cpsr & PSR_C_BIT)
  772. offset.un |= 1 << 31;
  773. } else
  774. offset.un = offset.un >> shiftval |
  775. offset.un << (32 - shiftval);
  776. break;
  777. }
  778. }
  779. handler = do_alignment_ldrstr;
  780. break;
  781. case 0x08000000: /* ldm or stm, or thumb-2 32bit instruction */
  782. if (thumb2_32b) {
  783. offset.un = 0;
  784. handler = do_alignment_t32_to_handler(&instr, regs, &offset);
  785. } else {
  786. offset.un = 0;
  787. handler = do_alignment_ldmstm;
  788. }
  789. break;
  790. default:
  791. goto bad;
  792. }
  793. if (!handler)
  794. goto bad;
  795. type = handler(addr, instr, regs);
  796. if (type == TYPE_ERROR || type == TYPE_FAULT) {
  797. regs->ARM_pc -= isize;
  798. goto bad_or_fault;
  799. }
  800. if (type == TYPE_LDST)
  801. do_alignment_finish_ldst(addr, instr, regs, offset);
  802. if (thumb_mode(regs))
  803. regs->ARM_cpsr = it_advance(regs->ARM_cpsr);
  804. return 0;
  805. bad_or_fault:
  806. if (type == TYPE_ERROR)
  807. goto bad;
  808. /*
  809. * We got a fault - fix it up, or die.
  810. */
  811. do_bad_area(addr, fsr, regs);
  812. return 0;
  813. swp:
  814. pr_err("Alignment trap: not handling swp instruction\n");
  815. bad:
  816. /*
  817. * Oops, we didn't handle the instruction.
  818. */
  819. pr_err("Alignment trap: not handling instruction "
  820. "%0*x at [<%08lx>]\n",
  821. isize << 1,
  822. isize == 2 ? tinstr : instr, instrptr);
  823. ai_skipped += 1;
  824. return 1;
  825. user:
  826. ai_user += 1;
  827. if (ai_usermode & UM_WARN)
  828. printk("Alignment trap: %s (%d) PC=0x%08lx Instr=0x%0*x "
  829. "Address=0x%08lx FSR 0x%03x\n", current->comm,
  830. task_pid_nr(current), instrptr,
  831. isize << 1,
  832. isize == 2 ? tinstr : instr,
  833. addr, fsr);
  834. if (ai_usermode & UM_FIXUP)
  835. goto fixup;
  836. if (ai_usermode & UM_SIGNAL) {
  837. force_sig_fault(SIGBUS, BUS_ADRALN, (void __user *)addr);
  838. } else {
  839. /*
  840. * We're about to disable the alignment trap and return to
  841. * user space. But if an interrupt occurs before actually
  842. * reaching user space, then the IRQ vector entry code will
  843. * notice that we were still in kernel space and therefore
  844. * the alignment trap won't be re-enabled in that case as it
  845. * is presumed to be always on from kernel space.
  846. * Let's prevent that race by disabling interrupts here (they
  847. * are disabled on the way back to user space anyway in
  848. * entry-common.S) and disable the alignment trap only if
  849. * there is no work pending for this thread.
  850. */
  851. raw_local_irq_disable();
  852. if (!(read_thread_flags() & _TIF_WORK_MASK))
  853. set_cr(cr_no_alignment);
  854. }
  855. return 0;
  856. }
  857. static int __init noalign_setup(char *__unused)
  858. {
  859. set_cr(__clear_cr(CR_A));
  860. return 1;
  861. }
  862. __setup("noalign", noalign_setup);
  863. /*
  864. * This needs to be done after sysctl_init_bases(), otherwise sys/ will be
  865. * overwritten. Actually, this shouldn't be in sys/ at all since
  866. * it isn't a sysctl, and it doesn't contain sysctl information.
  867. * We now locate it in /proc/cpu/alignment instead.
  868. */
  869. static int __init alignment_init(void)
  870. {
  871. #ifdef CONFIG_PROC_FS
  872. struct proc_dir_entry *res;
  873. res = proc_create("cpu/alignment", S_IWUSR | S_IRUGO, NULL,
  874. &alignment_proc_ops);
  875. if (!res)
  876. return -ENOMEM;
  877. #endif
  878. if (cpu_is_v6_unaligned()) {
  879. set_cr(__clear_cr(CR_A));
  880. ai_usermode = safe_usermode(ai_usermode, false);
  881. }
  882. cr_no_alignment = get_cr() & ~CR_A;
  883. hook_fault_code(FAULT_CODE_ALIGNMENT, do_alignment, SIGBUS, BUS_ADRALN,
  884. "alignment exception");
  885. /*
  886. * ARMv6K and ARMv7 use fault status 3 (0b00011) as Access Flag section
  887. * fault, not as alignment error.
  888. *
  889. * TODO: handle ARMv6K properly. Runtime check for 'K' extension is
  890. * needed.
  891. */
  892. if (cpu_architecture() <= CPU_ARCH_ARMv6) {
  893. hook_fault_code(3, do_alignment, SIGBUS, BUS_ADRALN,
  894. "alignment exception");
  895. }
  896. return 0;
  897. }
  898. fs_initcall(alignment_init);