muldiv.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * muldiv.c: Hardware multiply/division illegal instruction trap
  3. * for sun4c/sun4 (which do not have those instructions)
  4. *
  5. * Copyright (C) 1996 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
  6. * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
  7. *
  8. * 2004-12-25 Krzysztof Helt (krzysztof.h1@wp.pl)
  9. * - fixed registers constrains in inline assembly declarations
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/sched.h>
  13. #include <linux/mm.h>
  14. #include <asm/ptrace.h>
  15. #include <asm/processor.h>
  16. #include <asm/system.h>
  17. #include <asm/uaccess.h>
  18. #include "kernel.h"
  19. /* #define DEBUG_MULDIV */
  20. static inline int has_imm13(int insn)
  21. {
  22. return (insn & 0x2000);
  23. }
  24. static inline int is_foocc(int insn)
  25. {
  26. return (insn & 0x800000);
  27. }
  28. static inline int sign_extend_imm13(int imm)
  29. {
  30. return imm << 19 >> 19;
  31. }
  32. static inline void advance(struct pt_regs *regs)
  33. {
  34. regs->pc = regs->npc;
  35. regs->npc += 4;
  36. }
  37. static inline void maybe_flush_windows(unsigned int rs1, unsigned int rs2,
  38. unsigned int rd)
  39. {
  40. if(rs2 >= 16 || rs1 >= 16 || rd >= 16) {
  41. /* Wheee... */
  42. __asm__ __volatile__("save %sp, -0x40, %sp\n\t"
  43. "save %sp, -0x40, %sp\n\t"
  44. "save %sp, -0x40, %sp\n\t"
  45. "save %sp, -0x40, %sp\n\t"
  46. "save %sp, -0x40, %sp\n\t"
  47. "save %sp, -0x40, %sp\n\t"
  48. "save %sp, -0x40, %sp\n\t"
  49. "restore; restore; restore; restore;\n\t"
  50. "restore; restore; restore;\n\t");
  51. }
  52. }
  53. #define fetch_reg(reg, regs) ({ \
  54. struct reg_window32 __user *win; \
  55. register unsigned long ret; \
  56. \
  57. if (!(reg)) ret = 0; \
  58. else if ((reg) < 16) { \
  59. ret = regs->u_regs[(reg)]; \
  60. } else { \
  61. /* Ho hum, the slightly complicated case. */ \
  62. win = (struct reg_window32 __user *)regs->u_regs[UREG_FP];\
  63. if (get_user (ret, &win->locals[(reg) - 16])) return -1;\
  64. } \
  65. ret; \
  66. })
  67. static inline int
  68. store_reg(unsigned int result, unsigned int reg, struct pt_regs *regs)
  69. {
  70. struct reg_window32 __user *win;
  71. if (!reg)
  72. return 0;
  73. if (reg < 16) {
  74. regs->u_regs[reg] = result;
  75. return 0;
  76. } else {
  77. /* need to use put_user() in this case: */
  78. win = (struct reg_window32 __user *) regs->u_regs[UREG_FP];
  79. return (put_user(result, &win->locals[reg - 16]));
  80. }
  81. }
  82. /* Should return 0 if mul/div emulation succeeded and SIGILL should
  83. * not be issued.
  84. */
  85. int do_user_muldiv(struct pt_regs *regs, unsigned long pc)
  86. {
  87. unsigned int insn;
  88. int inst;
  89. unsigned int rs1, rs2, rdv;
  90. if (!pc)
  91. return -1; /* This happens to often, I think */
  92. if (get_user (insn, (unsigned int __user *)pc))
  93. return -1;
  94. if ((insn & 0xc1400000) != 0x80400000)
  95. return -1;
  96. inst = ((insn >> 19) & 0xf);
  97. if ((inst & 0xe) != 10 && (inst & 0xe) != 14)
  98. return -1;
  99. /* Now we know we have to do something with umul, smul, udiv or sdiv */
  100. rs1 = (insn >> 14) & 0x1f;
  101. rs2 = insn & 0x1f;
  102. rdv = (insn >> 25) & 0x1f;
  103. if (has_imm13(insn)) {
  104. maybe_flush_windows(rs1, 0, rdv);
  105. rs2 = sign_extend_imm13(insn);
  106. } else {
  107. maybe_flush_windows(rs1, rs2, rdv);
  108. rs2 = fetch_reg(rs2, regs);
  109. }
  110. rs1 = fetch_reg(rs1, regs);
  111. switch (inst) {
  112. case 10: /* umul */
  113. #ifdef DEBUG_MULDIV
  114. printk ("unsigned muldiv: 0x%x * 0x%x = ", rs1, rs2);
  115. #endif
  116. __asm__ __volatile__ ("\n\t"
  117. "mov %0, %%o0\n\t"
  118. "call .umul\n\t"
  119. " mov %1, %%o1\n\t"
  120. "mov %%o0, %0\n\t"
  121. "mov %%o1, %1\n\t"
  122. : "=r" (rs1), "=r" (rs2)
  123. : "0" (rs1), "1" (rs2)
  124. : "o0", "o1", "o2", "o3", "o4", "o5", "o7", "cc");
  125. #ifdef DEBUG_MULDIV
  126. printk ("0x%x%08x\n", rs2, rs1);
  127. #endif
  128. if (store_reg(rs1, rdv, regs))
  129. return -1;
  130. regs->y = rs2;
  131. break;
  132. case 11: /* smul */
  133. #ifdef DEBUG_MULDIV
  134. printk ("signed muldiv: 0x%x * 0x%x = ", rs1, rs2);
  135. #endif
  136. __asm__ __volatile__ ("\n\t"
  137. "mov %0, %%o0\n\t"
  138. "call .mul\n\t"
  139. " mov %1, %%o1\n\t"
  140. "mov %%o0, %0\n\t"
  141. "mov %%o1, %1\n\t"
  142. : "=r" (rs1), "=r" (rs2)
  143. : "0" (rs1), "1" (rs2)
  144. : "o0", "o1", "o2", "o3", "o4", "o5", "o7", "cc");
  145. #ifdef DEBUG_MULDIV
  146. printk ("0x%x%08x\n", rs2, rs1);
  147. #endif
  148. if (store_reg(rs1, rdv, regs))
  149. return -1;
  150. regs->y = rs2;
  151. break;
  152. case 14: /* udiv */
  153. #ifdef DEBUG_MULDIV
  154. printk ("unsigned muldiv: 0x%x%08x / 0x%x = ", regs->y, rs1, rs2);
  155. #endif
  156. if (!rs2) {
  157. #ifdef DEBUG_MULDIV
  158. printk ("DIVISION BY ZERO\n");
  159. #endif
  160. handle_hw_divzero (regs, pc, regs->npc, regs->psr);
  161. return 0;
  162. }
  163. __asm__ __volatile__ ("\n\t"
  164. "mov %2, %%o0\n\t"
  165. "mov %0, %%o1\n\t"
  166. "mov %%g0, %%o2\n\t"
  167. "call __udivdi3\n\t"
  168. " mov %1, %%o3\n\t"
  169. "mov %%o1, %0\n\t"
  170. "mov %%o0, %1\n\t"
  171. : "=r" (rs1), "=r" (rs2)
  172. : "r" (regs->y), "0" (rs1), "1" (rs2)
  173. : "o0", "o1", "o2", "o3", "o4", "o5", "o7",
  174. "g1", "g2", "g3", "cc");
  175. #ifdef DEBUG_MULDIV
  176. printk ("0x%x\n", rs1);
  177. #endif
  178. if (store_reg(rs1, rdv, regs))
  179. return -1;
  180. break;
  181. case 15: /* sdiv */
  182. #ifdef DEBUG_MULDIV
  183. printk ("signed muldiv: 0x%x%08x / 0x%x = ", regs->y, rs1, rs2);
  184. #endif
  185. if (!rs2) {
  186. #ifdef DEBUG_MULDIV
  187. printk ("DIVISION BY ZERO\n");
  188. #endif
  189. handle_hw_divzero (regs, pc, regs->npc, regs->psr);
  190. return 0;
  191. }
  192. __asm__ __volatile__ ("\n\t"
  193. "mov %2, %%o0\n\t"
  194. "mov %0, %%o1\n\t"
  195. "mov %%g0, %%o2\n\t"
  196. "call __divdi3\n\t"
  197. " mov %1, %%o3\n\t"
  198. "mov %%o1, %0\n\t"
  199. "mov %%o0, %1\n\t"
  200. : "=r" (rs1), "=r" (rs2)
  201. : "r" (regs->y), "0" (rs1), "1" (rs2)
  202. : "o0", "o1", "o2", "o3", "o4", "o5", "o7",
  203. "g1", "g2", "g3", "cc");
  204. #ifdef DEBUG_MULDIV
  205. printk ("0x%x\n", rs1);
  206. #endif
  207. if (store_reg(rs1, rdv, regs))
  208. return -1;
  209. break;
  210. }
  211. if (is_foocc (insn)) {
  212. regs->psr &= ~PSR_ICC;
  213. if ((inst & 0xe) == 14) {
  214. /* ?div */
  215. if (rs2) regs->psr |= PSR_V;
  216. }
  217. if (!rs1) regs->psr |= PSR_Z;
  218. if (((int)rs1) < 0) regs->psr |= PSR_N;
  219. #ifdef DEBUG_MULDIV
  220. printk ("psr muldiv: %08x\n", regs->psr);
  221. #endif
  222. }
  223. advance(regs);
  224. return 0;
  225. }