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