unaligned_32.c 9.2 KB

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