unaligned_32.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * unaligned.c: Unaligned load/store trap handling with special
  4. * cases for the kernel to do them more quickly.
  5. *
  6. * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
  7. * Copyright (C) 1996 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/sched/signal.h>
  11. #include <linux/mm.h>
  12. #include <asm/ptrace.h>
  13. #include <asm/processor.h>
  14. #include <linux/uaccess.h>
  15. #include <linux/smp.h>
  16. #include <linux/perf_event.h>
  17. #include <asm/setup.h>
  18. #include "kernel.h"
  19. enum direction {
  20. load, /* ld, ldd, ldh, ldsh */
  21. store, /* st, std, sth, stsh */
  22. both, /* Swap, ldstub, etc. */
  23. fpload,
  24. fpstore,
  25. invalid,
  26. };
  27. static inline enum direction decode_direction(unsigned int insn)
  28. {
  29. unsigned long tmp = (insn >> 21) & 1;
  30. if(!tmp)
  31. return load;
  32. else {
  33. if(((insn>>19)&0x3f) == 15)
  34. return both;
  35. else
  36. return store;
  37. }
  38. }
  39. /* 8 = double-word, 4 = word, 2 = half-word */
  40. static inline int decode_access_size(unsigned int insn)
  41. {
  42. insn = (insn >> 19) & 3;
  43. if(!insn)
  44. return 4;
  45. else if(insn == 3)
  46. return 8;
  47. else if(insn == 2)
  48. return 2;
  49. else {
  50. printk("Impossible unaligned trap. insn=%08x\n", insn);
  51. die_if_kernel("Byte sized unaligned access?!?!", current->thread.kregs);
  52. return 4; /* just to keep gcc happy. */
  53. }
  54. }
  55. /* 0x400000 = signed, 0 = unsigned */
  56. static inline int decode_signedness(unsigned int insn)
  57. {
  58. return (insn & 0x400000);
  59. }
  60. static inline void maybe_flush_windows(unsigned int rs1, unsigned int rs2,
  61. unsigned int rd)
  62. {
  63. if(rs2 >= 16 || rs1 >= 16 || rd >= 16) {
  64. /* Wheee... */
  65. __asm__ __volatile__("save %sp, -0x40, %sp\n\t"
  66. "save %sp, -0x40, %sp\n\t"
  67. "save %sp, -0x40, %sp\n\t"
  68. "save %sp, -0x40, %sp\n\t"
  69. "save %sp, -0x40, %sp\n\t"
  70. "save %sp, -0x40, %sp\n\t"
  71. "save %sp, -0x40, %sp\n\t"
  72. "restore; restore; restore; restore;\n\t"
  73. "restore; restore; restore;\n\t");
  74. }
  75. }
  76. static inline int sign_extend_imm13(int imm)
  77. {
  78. return imm << 19 >> 19;
  79. }
  80. static inline unsigned long fetch_reg(unsigned int reg, struct pt_regs *regs)
  81. {
  82. struct reg_window32 *win;
  83. if(reg < 16)
  84. return (!reg ? 0 : regs->u_regs[reg]);
  85. /* Ho hum, the slightly complicated case. */
  86. win = (struct reg_window32 *) regs->u_regs[UREG_FP];
  87. return win->locals[reg - 16]; /* yes, I know what this does... */
  88. }
  89. static inline unsigned long safe_fetch_reg(unsigned int reg, struct pt_regs *regs)
  90. {
  91. struct reg_window32 __user *win;
  92. unsigned long ret;
  93. if (reg < 16)
  94. return (!reg ? 0 : regs->u_regs[reg]);
  95. /* Ho hum, the slightly complicated case. */
  96. win = (struct reg_window32 __user *) regs->u_regs[UREG_FP];
  97. if ((unsigned long)win & 3)
  98. return -1;
  99. if (get_user(ret, &win->locals[reg - 16]))
  100. return -1;
  101. return ret;
  102. }
  103. static inline unsigned long *fetch_reg_addr(unsigned int reg, struct pt_regs *regs)
  104. {
  105. struct reg_window32 *win;
  106. if(reg < 16)
  107. return &regs->u_regs[reg];
  108. win = (struct reg_window32 *) regs->u_regs[UREG_FP];
  109. return &win->locals[reg - 16];
  110. }
  111. static unsigned long compute_effective_address(struct pt_regs *regs,
  112. unsigned int insn)
  113. {
  114. unsigned int rs1 = (insn >> 14) & 0x1f;
  115. unsigned int rs2 = insn & 0x1f;
  116. unsigned int rd = (insn >> 25) & 0x1f;
  117. if(insn & 0x2000) {
  118. maybe_flush_windows(rs1, 0, rd);
  119. return (fetch_reg(rs1, regs) + sign_extend_imm13(insn));
  120. } else {
  121. maybe_flush_windows(rs1, rs2, rd);
  122. return (fetch_reg(rs1, regs) + fetch_reg(rs2, regs));
  123. }
  124. }
  125. unsigned long safe_compute_effective_address(struct pt_regs *regs,
  126. unsigned int insn)
  127. {
  128. unsigned int rs1 = (insn >> 14) & 0x1f;
  129. unsigned int rs2 = insn & 0x1f;
  130. unsigned int rd = (insn >> 25) & 0x1f;
  131. if(insn & 0x2000) {
  132. maybe_flush_windows(rs1, 0, rd);
  133. return (safe_fetch_reg(rs1, regs) + sign_extend_imm13(insn));
  134. } else {
  135. maybe_flush_windows(rs1, rs2, rd);
  136. return (safe_fetch_reg(rs1, regs) + safe_fetch_reg(rs2, regs));
  137. }
  138. }
  139. /* This is just to make gcc think panic does return... */
  140. static void unaligned_panic(char *str)
  141. {
  142. panic("%s", str);
  143. }
  144. /* una_asm.S */
  145. extern int do_int_load(unsigned long *dest_reg, int size,
  146. unsigned long *saddr, int is_signed);
  147. extern int __do_int_store(unsigned long *dst_addr, int size,
  148. unsigned long *src_val);
  149. static int do_int_store(int reg_num, int size, unsigned long *dst_addr,
  150. struct pt_regs *regs)
  151. {
  152. unsigned long zero[2] = { 0, 0 };
  153. unsigned long *src_val;
  154. if (reg_num)
  155. src_val = fetch_reg_addr(reg_num, regs);
  156. else {
  157. src_val = &zero[0];
  158. if (size == 8)
  159. zero[1] = fetch_reg(1, regs);
  160. }
  161. return __do_int_store(dst_addr, size, src_val);
  162. }
  163. extern void smp_capture(void);
  164. extern void smp_release(void);
  165. static inline void advance(struct pt_regs *regs)
  166. {
  167. regs->pc = regs->npc;
  168. regs->npc += 4;
  169. }
  170. static inline int floating_point_load_or_store_p(unsigned int insn)
  171. {
  172. return (insn >> 24) & 1;
  173. }
  174. static inline int ok_for_kernel(unsigned int insn)
  175. {
  176. return !floating_point_load_or_store_p(insn);
  177. }
  178. static void kernel_mna_trap_fault(struct pt_regs *regs, unsigned int insn)
  179. {
  180. unsigned long g2 = regs->u_regs [UREG_G2];
  181. unsigned long fixup = search_extables_range(regs->pc, &g2);
  182. if (!fixup) {
  183. unsigned long address = compute_effective_address(regs, insn);
  184. if(address < PAGE_SIZE) {
  185. printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference in mna handler");
  186. } else
  187. printk(KERN_ALERT "Unable to handle kernel paging request in mna handler");
  188. printk(KERN_ALERT " at virtual address %08lx\n",address);
  189. printk(KERN_ALERT "current->{mm,active_mm}->context = %08lx\n",
  190. (current->mm ? current->mm->context :
  191. current->active_mm->context));
  192. printk(KERN_ALERT "current->{mm,active_mm}->pgd = %08lx\n",
  193. (current->mm ? (unsigned long) current->mm->pgd :
  194. (unsigned long) current->active_mm->pgd));
  195. die_if_kernel("Oops", regs);
  196. /* Not reached */
  197. }
  198. regs->pc = fixup;
  199. regs->npc = regs->pc + 4;
  200. regs->u_regs [UREG_G2] = g2;
  201. }
  202. asmlinkage void kernel_unaligned_trap(struct pt_regs *regs, unsigned int insn)
  203. {
  204. enum direction dir = decode_direction(insn);
  205. int size = decode_access_size(insn);
  206. if(!ok_for_kernel(insn) || dir == both) {
  207. printk("Unsupported unaligned load/store trap for kernel at <%08lx>.\n",
  208. regs->pc);
  209. unaligned_panic("Wheee. Kernel does fpu/atomic unaligned load/store.");
  210. } else {
  211. unsigned long addr = compute_effective_address(regs, insn);
  212. int err;
  213. perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS, 1, regs, addr);
  214. switch (dir) {
  215. case load:
  216. err = do_int_load(fetch_reg_addr(((insn>>25)&0x1f),
  217. regs),
  218. size, (unsigned long *) addr,
  219. decode_signedness(insn));
  220. break;
  221. case store:
  222. err = do_int_store(((insn>>25)&0x1f), size,
  223. (unsigned long *) addr, regs);
  224. break;
  225. default:
  226. panic("Impossible kernel unaligned trap.");
  227. /* Not reached... */
  228. }
  229. if (err)
  230. kernel_mna_trap_fault(regs, insn);
  231. else
  232. advance(regs);
  233. }
  234. }
  235. static inline int ok_for_user(struct pt_regs *regs, unsigned int insn,
  236. enum direction dir)
  237. {
  238. unsigned int reg;
  239. int size = ((insn >> 19) & 3) == 3 ? 8 : 4;
  240. if ((regs->pc | regs->npc) & 3)
  241. return 0;
  242. /* Must access_ok() in all the necessary places. */
  243. #define WINREG_ADDR(regnum) \
  244. ((void __user *)(((unsigned long *)regs->u_regs[UREG_FP])+(regnum)))
  245. reg = (insn >> 25) & 0x1f;
  246. if (reg >= 16) {
  247. if (!access_ok(WINREG_ADDR(reg - 16), size))
  248. return -EFAULT;
  249. }
  250. reg = (insn >> 14) & 0x1f;
  251. if (reg >= 16) {
  252. if (!access_ok(WINREG_ADDR(reg - 16), size))
  253. return -EFAULT;
  254. }
  255. if (!(insn & 0x2000)) {
  256. reg = (insn & 0x1f);
  257. if (reg >= 16) {
  258. if (!access_ok(WINREG_ADDR(reg - 16), size))
  259. return -EFAULT;
  260. }
  261. }
  262. #undef WINREG_ADDR
  263. return 0;
  264. }
  265. static void user_mna_trap_fault(struct pt_regs *regs, unsigned int insn)
  266. {
  267. send_sig_fault(SIGBUS, BUS_ADRALN,
  268. (void __user *)safe_compute_effective_address(regs, insn),
  269. 0, current);
  270. }
  271. asmlinkage void user_unaligned_trap(struct pt_regs *regs, unsigned int insn)
  272. {
  273. enum direction dir;
  274. if(!(current->thread.flags & SPARC_FLAG_UNALIGNED) ||
  275. (((insn >> 30) & 3) != 3))
  276. goto kill_user;
  277. dir = decode_direction(insn);
  278. if(!ok_for_user(regs, insn, dir)) {
  279. goto kill_user;
  280. } else {
  281. int err, size = decode_access_size(insn);
  282. unsigned long addr;
  283. if(floating_point_load_or_store_p(insn)) {
  284. printk("User FPU load/store unaligned unsupported.\n");
  285. goto kill_user;
  286. }
  287. addr = compute_effective_address(regs, insn);
  288. perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS, 1, regs, addr);
  289. switch(dir) {
  290. case load:
  291. err = do_int_load(fetch_reg_addr(((insn>>25)&0x1f),
  292. regs),
  293. size, (unsigned long *) addr,
  294. decode_signedness(insn));
  295. break;
  296. case store:
  297. err = do_int_store(((insn>>25)&0x1f), size,
  298. (unsigned long *) addr, regs);
  299. break;
  300. case both:
  301. /*
  302. * This was supported in 2.4. However, we question
  303. * the value of SWAP instruction across word boundaries.
  304. */
  305. printk("Unaligned SWAP unsupported.\n");
  306. err = -EFAULT;
  307. break;
  308. default:
  309. unaligned_panic("Impossible user unaligned trap.");
  310. goto out;
  311. }
  312. if (err)
  313. goto kill_user;
  314. else
  315. advance(regs);
  316. goto out;
  317. }
  318. kill_user:
  319. user_mna_trap_fault(regs, insn);
  320. out:
  321. ;
  322. }