unaligned_32.c 9.2 KB

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