unaligned_32.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 ([email protected])
  7. * Copyright (C) 1996 Jakub Jelinek ([email protected])
  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 <linux/extable.h>
  18. #include <asm/setup.h>
  19. #include "kernel.h"
  20. enum direction {
  21. load, /* ld, ldd, ldh, ldsh */
  22. store, /* st, std, sth, stsh */
  23. both, /* Swap, ldstub, etc. */
  24. fpload,
  25. fpstore,
  26. invalid,
  27. };
  28. static inline enum direction decode_direction(unsigned int insn)
  29. {
  30. unsigned long tmp = (insn >> 21) & 1;
  31. if(!tmp)
  32. return load;
  33. else {
  34. if(((insn>>19)&0x3f) == 15)
  35. return both;
  36. else
  37. return store;
  38. }
  39. }
  40. /* 8 = double-word, 4 = word, 2 = half-word */
  41. static inline int decode_access_size(unsigned int insn)
  42. {
  43. insn = (insn >> 19) & 3;
  44. if(!insn)
  45. return 4;
  46. else if(insn == 3)
  47. return 8;
  48. else if(insn == 2)
  49. return 2;
  50. else {
  51. printk("Impossible unaligned trap. insn=%08x\n", insn);
  52. die_if_kernel("Byte sized unaligned access?!?!", current->thread.kregs);
  53. return 4; /* just to keep gcc happy. */
  54. }
  55. }
  56. /* 0x400000 = signed, 0 = unsigned */
  57. static inline int decode_signedness(unsigned int insn)
  58. {
  59. return (insn & 0x400000);
  60. }
  61. static inline void maybe_flush_windows(unsigned int rs1, unsigned int rs2,
  62. unsigned int rd)
  63. {
  64. if(rs2 >= 16 || rs1 >= 16 || rd >= 16) {
  65. /* Wheee... */
  66. __asm__ __volatile__("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. "save %sp, -0x40, %sp\n\t"
  73. "restore; restore; restore; restore;\n\t"
  74. "restore; restore; restore;\n\t");
  75. }
  76. }
  77. static inline int sign_extend_imm13(int imm)
  78. {
  79. return imm << 19 >> 19;
  80. }
  81. static inline unsigned long fetch_reg(unsigned int reg, struct pt_regs *regs)
  82. {
  83. struct reg_window32 *win;
  84. if(reg < 16)
  85. return (!reg ? 0 : regs->u_regs[reg]);
  86. /* Ho hum, the slightly complicated case. */
  87. win = (struct reg_window32 *) regs->u_regs[UREG_FP];
  88. return win->locals[reg - 16]; /* yes, I know what this does... */
  89. }
  90. static inline unsigned long safe_fetch_reg(unsigned int reg, struct pt_regs *regs)
  91. {
  92. struct reg_window32 __user *win;
  93. unsigned long ret;
  94. if (reg < 16)
  95. return (!reg ? 0 : regs->u_regs[reg]);
  96. /* Ho hum, the slightly complicated case. */
  97. win = (struct reg_window32 __user *) regs->u_regs[UREG_FP];
  98. if ((unsigned long)win & 3)
  99. return -1;
  100. if (get_user(ret, &win->locals[reg - 16]))
  101. return -1;
  102. return ret;
  103. }
  104. static inline unsigned long *fetch_reg_addr(unsigned int reg, struct pt_regs *regs)
  105. {
  106. struct reg_window32 *win;
  107. if(reg < 16)
  108. return &regs->u_regs[reg];
  109. win = (struct reg_window32 *) regs->u_regs[UREG_FP];
  110. return &win->locals[reg - 16];
  111. }
  112. static unsigned long compute_effective_address(struct pt_regs *regs,
  113. unsigned int insn)
  114. {
  115. unsigned int rs1 = (insn >> 14) & 0x1f;
  116. unsigned int rs2 = insn & 0x1f;
  117. unsigned int rd = (insn >> 25) & 0x1f;
  118. if(insn & 0x2000) {
  119. maybe_flush_windows(rs1, 0, rd);
  120. return (fetch_reg(rs1, regs) + sign_extend_imm13(insn));
  121. } else {
  122. maybe_flush_windows(rs1, rs2, rd);
  123. return (fetch_reg(rs1, regs) + fetch_reg(rs2, regs));
  124. }
  125. }
  126. unsigned long safe_compute_effective_address(struct pt_regs *regs,
  127. unsigned int insn)
  128. {
  129. unsigned int rs1 = (insn >> 14) & 0x1f;
  130. unsigned int rs2 = insn & 0x1f;
  131. unsigned int rd = (insn >> 25) & 0x1f;
  132. if(insn & 0x2000) {
  133. maybe_flush_windows(rs1, 0, rd);
  134. return (safe_fetch_reg(rs1, regs) + sign_extend_imm13(insn));
  135. } else {
  136. maybe_flush_windows(rs1, rs2, rd);
  137. return (safe_fetch_reg(rs1, regs) + safe_fetch_reg(rs2, regs));
  138. }
  139. }
  140. /* This is just to make gcc think panic does return... */
  141. static void unaligned_panic(char *str)
  142. {
  143. panic("%s", str);
  144. }
  145. /* una_asm.S */
  146. extern int do_int_load(unsigned long *dest_reg, int size,
  147. unsigned long *saddr, int is_signed);
  148. extern int __do_int_store(unsigned long *dst_addr, int size,
  149. unsigned long *src_val);
  150. static int do_int_store(int reg_num, int size, unsigned long *dst_addr,
  151. struct pt_regs *regs)
  152. {
  153. unsigned long zero[2] = { 0, 0 };
  154. unsigned long *src_val;
  155. if (reg_num)
  156. src_val = fetch_reg_addr(reg_num, regs);
  157. else {
  158. src_val = &zero[0];
  159. if (size == 8)
  160. zero[1] = fetch_reg(1, regs);
  161. }
  162. return __do_int_store(dst_addr, size, src_val);
  163. }
  164. extern void smp_capture(void);
  165. extern void smp_release(void);
  166. static inline void advance(struct pt_regs *regs)
  167. {
  168. regs->pc = regs->npc;
  169. regs->npc += 4;
  170. }
  171. static inline int floating_point_load_or_store_p(unsigned int insn)
  172. {
  173. return (insn >> 24) & 1;
  174. }
  175. static inline int ok_for_kernel(unsigned int insn)
  176. {
  177. return !floating_point_load_or_store_p(insn);
  178. }
  179. static void kernel_mna_trap_fault(struct pt_regs *regs, unsigned int insn)
  180. {
  181. const struct exception_table_entry *entry;
  182. entry = search_exception_tables(regs->pc);
  183. if (!entry) {
  184. unsigned long address = compute_effective_address(regs, insn);
  185. if(address < PAGE_SIZE) {
  186. printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference in mna handler");
  187. } else
  188. printk(KERN_ALERT "Unable to handle kernel paging request in mna handler");
  189. printk(KERN_ALERT " at virtual address %08lx\n",address);
  190. printk(KERN_ALERT "current->{mm,active_mm}->context = %08lx\n",
  191. (current->mm ? current->mm->context :
  192. current->active_mm->context));
  193. printk(KERN_ALERT "current->{mm,active_mm}->pgd = %08lx\n",
  194. (current->mm ? (unsigned long) current->mm->pgd :
  195. (unsigned long) current->active_mm->pgd));
  196. die_if_kernel("Oops", regs);
  197. /* Not reached */
  198. }
  199. regs->pc = entry->fixup;
  200. regs->npc = regs->pc + 4;
  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. asmlinkage void user_unaligned_trap(struct pt_regs *regs, unsigned int insn)
  236. {
  237. send_sig_fault(SIGBUS, BUS_ADRALN,
  238. (void __user *)safe_compute_effective_address(regs, insn),
  239. current);
  240. }