compat_alignment.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. // based on arch/arm/mm/alignment.c
  3. #include <linux/compiler.h>
  4. #include <linux/errno.h>
  5. #include <linux/kernel.h>
  6. #include <linux/init.h>
  7. #include <linux/perf_event.h>
  8. #include <linux/uaccess.h>
  9. #include <asm/exception.h>
  10. #include <asm/ptrace.h>
  11. #include <asm/traps.h>
  12. /*
  13. * 32-bit misaligned trap handler (c) 1998 San Mehat (CCC) -July 1998
  14. *
  15. * Speed optimisations and better fault handling by Russell King.
  16. */
  17. #define CODING_BITS(i) (i & 0x0e000000)
  18. #define LDST_P_BIT(i) (i & (1 << 24)) /* Preindex */
  19. #define LDST_U_BIT(i) (i & (1 << 23)) /* Add offset */
  20. #define LDST_W_BIT(i) (i & (1 << 21)) /* Writeback */
  21. #define LDST_L_BIT(i) (i & (1 << 20)) /* Load */
  22. #define LDST_P_EQ_U(i) ((((i) ^ ((i) >> 1)) & (1 << 23)) == 0)
  23. #define LDSTHD_I_BIT(i) (i & (1 << 22)) /* double/half-word immed */
  24. #define RN_BITS(i) ((i >> 16) & 15) /* Rn */
  25. #define RD_BITS(i) ((i >> 12) & 15) /* Rd */
  26. #define RM_BITS(i) (i & 15) /* Rm */
  27. #define REGMASK_BITS(i) (i & 0xffff)
  28. #define BAD_INSTR 0xdeadc0de
  29. /* Thumb-2 32 bit format per ARMv7 DDI0406A A6.3, either f800h,e800h,f800h */
  30. #define IS_T32(hi16) \
  31. (((hi16) & 0xe000) == 0xe000 && ((hi16) & 0x1800))
  32. union offset_union {
  33. unsigned long un;
  34. signed long sn;
  35. };
  36. #define TYPE_ERROR 0
  37. #define TYPE_FAULT 1
  38. #define TYPE_LDST 2
  39. #define TYPE_DONE 3
  40. static void
  41. do_alignment_finish_ldst(unsigned long addr, u32 instr, struct pt_regs *regs,
  42. union offset_union offset)
  43. {
  44. if (!LDST_U_BIT(instr))
  45. offset.un = -offset.un;
  46. if (!LDST_P_BIT(instr))
  47. addr += offset.un;
  48. if (!LDST_P_BIT(instr) || LDST_W_BIT(instr))
  49. regs->regs[RN_BITS(instr)] = addr;
  50. }
  51. static int
  52. do_alignment_ldrdstrd(unsigned long addr, u32 instr, struct pt_regs *regs)
  53. {
  54. unsigned int rd = RD_BITS(instr);
  55. unsigned int rd2;
  56. int load;
  57. if ((instr & 0xfe000000) == 0xe8000000) {
  58. /* ARMv7 Thumb-2 32-bit LDRD/STRD */
  59. rd2 = (instr >> 8) & 0xf;
  60. load = !!(LDST_L_BIT(instr));
  61. } else if (((rd & 1) == 1) || (rd == 14)) {
  62. return TYPE_ERROR;
  63. } else {
  64. load = ((instr & 0xf0) == 0xd0);
  65. rd2 = rd + 1;
  66. }
  67. if (load) {
  68. unsigned int val, val2;
  69. if (get_user(val, (u32 __user *)addr) ||
  70. get_user(val2, (u32 __user *)(addr + 4)))
  71. return TYPE_FAULT;
  72. regs->regs[rd] = val;
  73. regs->regs[rd2] = val2;
  74. } else {
  75. if (put_user(regs->regs[rd], (u32 __user *)addr) ||
  76. put_user(regs->regs[rd2], (u32 __user *)(addr + 4)))
  77. return TYPE_FAULT;
  78. }
  79. return TYPE_LDST;
  80. }
  81. /*
  82. * LDM/STM alignment handler.
  83. *
  84. * There are 4 variants of this instruction:
  85. *
  86. * B = rn pointer before instruction, A = rn pointer after instruction
  87. * ------ increasing address ----->
  88. * | | r0 | r1 | ... | rx | |
  89. * PU = 01 B A
  90. * PU = 11 B A
  91. * PU = 00 A B
  92. * PU = 10 A B
  93. */
  94. static int
  95. do_alignment_ldmstm(unsigned long addr, u32 instr, struct pt_regs *regs)
  96. {
  97. unsigned int rd, rn, nr_regs, regbits;
  98. unsigned long eaddr, newaddr;
  99. unsigned int val;
  100. /* count the number of registers in the mask to be transferred */
  101. nr_regs = hweight16(REGMASK_BITS(instr)) * 4;
  102. rn = RN_BITS(instr);
  103. newaddr = eaddr = regs->regs[rn];
  104. if (!LDST_U_BIT(instr))
  105. nr_regs = -nr_regs;
  106. newaddr += nr_regs;
  107. if (!LDST_U_BIT(instr))
  108. eaddr = newaddr;
  109. if (LDST_P_EQ_U(instr)) /* U = P */
  110. eaddr += 4;
  111. for (regbits = REGMASK_BITS(instr), rd = 0; regbits;
  112. regbits >>= 1, rd += 1)
  113. if (regbits & 1) {
  114. if (LDST_L_BIT(instr)) {
  115. if (get_user(val, (u32 __user *)eaddr))
  116. return TYPE_FAULT;
  117. if (rd < 15)
  118. regs->regs[rd] = val;
  119. else
  120. regs->pc = val;
  121. } else {
  122. /*
  123. * The PC register has a bias of +8 in ARM mode
  124. * and +4 in Thumb mode. This means that a read
  125. * of the value of PC should account for this.
  126. * Since Thumb does not permit STM instructions
  127. * to refer to PC, just add 8 here.
  128. */
  129. val = (rd < 15) ? regs->regs[rd] : regs->pc + 8;
  130. if (put_user(val, (u32 __user *)eaddr))
  131. return TYPE_FAULT;
  132. }
  133. eaddr += 4;
  134. }
  135. if (LDST_W_BIT(instr))
  136. regs->regs[rn] = newaddr;
  137. return TYPE_DONE;
  138. }
  139. /*
  140. * Convert Thumb multi-word load/store instruction forms to equivalent ARM
  141. * instructions so we can reuse ARM userland alignment fault fixups for Thumb.
  142. *
  143. * This implementation was initially based on the algorithm found in
  144. * gdb/sim/arm/thumbemu.c. It is basically just a code reduction of same
  145. * to convert only Thumb ld/st instruction forms to equivalent ARM forms.
  146. *
  147. * NOTES:
  148. * 1. Comments below refer to ARM ARM DDI0100E Thumb Instruction sections.
  149. * 2. If for some reason we're passed an non-ld/st Thumb instruction to
  150. * decode, we return 0xdeadc0de. This should never happen under normal
  151. * circumstances but if it does, we've got other problems to deal with
  152. * elsewhere and we obviously can't fix those problems here.
  153. */
  154. static unsigned long thumb2arm(u16 tinstr)
  155. {
  156. u32 L = (tinstr & (1<<11)) >> 11;
  157. switch ((tinstr & 0xf800) >> 11) {
  158. /* 6.6.1 Format 1: */
  159. case 0xc000 >> 11: /* 7.1.51 STMIA */
  160. case 0xc800 >> 11: /* 7.1.25 LDMIA */
  161. {
  162. u32 Rn = (tinstr & (7<<8)) >> 8;
  163. u32 W = ((L<<Rn) & (tinstr&255)) ? 0 : 1<<21;
  164. return 0xe8800000 | W | (L<<20) | (Rn<<16) |
  165. (tinstr&255);
  166. }
  167. /* 6.6.1 Format 2: */
  168. case 0xb000 >> 11: /* 7.1.48 PUSH */
  169. case 0xb800 >> 11: /* 7.1.47 POP */
  170. if ((tinstr & (3 << 9)) == 0x0400) {
  171. static const u32 subset[4] = {
  172. 0xe92d0000, /* STMDB sp!,{registers} */
  173. 0xe92d4000, /* STMDB sp!,{registers,lr} */
  174. 0xe8bd0000, /* LDMIA sp!,{registers} */
  175. 0xe8bd8000 /* LDMIA sp!,{registers,pc} */
  176. };
  177. return subset[(L<<1) | ((tinstr & (1<<8)) >> 8)] |
  178. (tinstr & 255); /* register_list */
  179. }
  180. fallthrough; /* for illegal instruction case */
  181. default:
  182. return BAD_INSTR;
  183. }
  184. }
  185. /*
  186. * Convert Thumb-2 32 bit LDM, STM, LDRD, STRD to equivalent instruction
  187. * handlable by ARM alignment handler, also find the corresponding handler,
  188. * so that we can reuse ARM userland alignment fault fixups for Thumb.
  189. *
  190. * @pinstr: original Thumb-2 instruction; returns new handlable instruction
  191. * @regs: register context.
  192. * @poffset: return offset from faulted addr for later writeback
  193. *
  194. * NOTES:
  195. * 1. Comments below refer to ARMv7 DDI0406A Thumb Instruction sections.
  196. * 2. Register name Rt from ARMv7 is same as Rd from ARMv6 (Rd is Rt)
  197. */
  198. static void *
  199. do_alignment_t32_to_handler(u32 *pinstr, struct pt_regs *regs,
  200. union offset_union *poffset)
  201. {
  202. u32 instr = *pinstr;
  203. u16 tinst1 = (instr >> 16) & 0xffff;
  204. u16 tinst2 = instr & 0xffff;
  205. switch (tinst1 & 0xffe0) {
  206. /* A6.3.5 Load/Store multiple */
  207. case 0xe880: /* STM/STMIA/STMEA,LDM/LDMIA, PUSH/POP T2 */
  208. case 0xe8a0: /* ...above writeback version */
  209. case 0xe900: /* STMDB/STMFD, LDMDB/LDMEA */
  210. case 0xe920: /* ...above writeback version */
  211. /* no need offset decision since handler calculates it */
  212. return do_alignment_ldmstm;
  213. case 0xf840: /* POP/PUSH T3 (single register) */
  214. if (RN_BITS(instr) == 13 && (tinst2 & 0x09ff) == 0x0904) {
  215. u32 L = !!(LDST_L_BIT(instr));
  216. const u32 subset[2] = {
  217. 0xe92d0000, /* STMDB sp!,{registers} */
  218. 0xe8bd0000, /* LDMIA sp!,{registers} */
  219. };
  220. *pinstr = subset[L] | (1<<RD_BITS(instr));
  221. return do_alignment_ldmstm;
  222. }
  223. /* Else fall through for illegal instruction case */
  224. break;
  225. /* A6.3.6 Load/store double, STRD/LDRD(immed, lit, reg) */
  226. case 0xe860:
  227. case 0xe960:
  228. case 0xe8e0:
  229. case 0xe9e0:
  230. poffset->un = (tinst2 & 0xff) << 2;
  231. fallthrough;
  232. case 0xe940:
  233. case 0xe9c0:
  234. return do_alignment_ldrdstrd;
  235. /*
  236. * No need to handle load/store instructions up to word size
  237. * since ARMv6 and later CPUs can perform unaligned accesses.
  238. */
  239. default:
  240. break;
  241. }
  242. return NULL;
  243. }
  244. static int alignment_get_arm(struct pt_regs *regs, __le32 __user *ip, u32 *inst)
  245. {
  246. __le32 instr = 0;
  247. int fault;
  248. fault = get_user(instr, ip);
  249. if (fault)
  250. return fault;
  251. *inst = __le32_to_cpu(instr);
  252. return 0;
  253. }
  254. static int alignment_get_thumb(struct pt_regs *regs, __le16 __user *ip, u16 *inst)
  255. {
  256. __le16 instr = 0;
  257. int fault;
  258. fault = get_user(instr, ip);
  259. if (fault)
  260. return fault;
  261. *inst = __le16_to_cpu(instr);
  262. return 0;
  263. }
  264. int do_compat_alignment_fixup(unsigned long addr, struct pt_regs *regs)
  265. {
  266. union offset_union offset;
  267. unsigned long instrptr;
  268. int (*handler)(unsigned long addr, u32 instr, struct pt_regs *regs);
  269. unsigned int type;
  270. u32 instr = 0;
  271. int isize = 4;
  272. int thumb2_32b = 0;
  273. instrptr = instruction_pointer(regs);
  274. if (compat_thumb_mode(regs)) {
  275. __le16 __user *ptr = (__le16 __user *)(instrptr & ~1);
  276. u16 tinstr, tinst2;
  277. if (alignment_get_thumb(regs, ptr, &tinstr))
  278. return 1;
  279. if (IS_T32(tinstr)) { /* Thumb-2 32-bit */
  280. if (alignment_get_thumb(regs, ptr + 1, &tinst2))
  281. return 1;
  282. instr = ((u32)tinstr << 16) | tinst2;
  283. thumb2_32b = 1;
  284. } else {
  285. isize = 2;
  286. instr = thumb2arm(tinstr);
  287. }
  288. } else {
  289. if (alignment_get_arm(regs, (__le32 __user *)instrptr, &instr))
  290. return 1;
  291. }
  292. switch (CODING_BITS(instr)) {
  293. case 0x00000000: /* 3.13.4 load/store instruction extensions */
  294. if (LDSTHD_I_BIT(instr))
  295. offset.un = (instr & 0xf00) >> 4 | (instr & 15);
  296. else
  297. offset.un = regs->regs[RM_BITS(instr)];
  298. if ((instr & 0x001000f0) == 0x000000d0 || /* LDRD */
  299. (instr & 0x001000f0) == 0x000000f0) /* STRD */
  300. handler = do_alignment_ldrdstrd;
  301. else
  302. return 1;
  303. break;
  304. case 0x08000000: /* ldm or stm, or thumb-2 32bit instruction */
  305. if (thumb2_32b) {
  306. offset.un = 0;
  307. handler = do_alignment_t32_to_handler(&instr, regs, &offset);
  308. } else {
  309. offset.un = 0;
  310. handler = do_alignment_ldmstm;
  311. }
  312. break;
  313. default:
  314. return 1;
  315. }
  316. type = handler(addr, instr, regs);
  317. if (type == TYPE_ERROR || type == TYPE_FAULT)
  318. return 1;
  319. if (type == TYPE_LDST)
  320. do_alignment_finish_ldst(addr, instr, regs, offset);
  321. perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS, 1, regs, regs->pc);
  322. arm64_skip_faulting_instruction(regs, isize);
  323. return 0;
  324. }