2 * muldiv.c: Hardware multiply/division illegal instruction trap
3 * for sun4c/sun4 (which do not have those instructions)
5 * Copyright (C) 1996 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
6 * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
8 * 2004-12-25 Krzysztof Helt (krzysztof.h1@wp.pl)
9 * - fixed registers constrains in inline assembly declarations
12 #include <linux/kernel.h>
13 #include <linux/sched.h>
15 #include <asm/ptrace.h>
16 #include <asm/processor.h>
17 #include <asm/uaccess.h>
21 /* #define DEBUG_MULDIV */
23 static inline int has_imm13(int insn)
25 return (insn & 0x2000);
28 static inline int is_foocc(int insn)
30 return (insn & 0x800000);
33 static inline int sign_extend_imm13(int imm)
35 return imm << 19 >> 19;
38 static inline void advance(struct pt_regs *regs)
44 static inline void maybe_flush_windows(unsigned int rs1, unsigned int rs2,
47 if(rs2 >= 16 || rs1 >= 16 || rd >= 16) {
49 __asm__ __volatile__("save %sp, -0x40, %sp\n\t"
50 "save %sp, -0x40, %sp\n\t"
51 "save %sp, -0x40, %sp\n\t"
52 "save %sp, -0x40, %sp\n\t"
53 "save %sp, -0x40, %sp\n\t"
54 "save %sp, -0x40, %sp\n\t"
55 "save %sp, -0x40, %sp\n\t"
56 "restore; restore; restore; restore;\n\t"
57 "restore; restore; restore;\n\t");
61 #define fetch_reg(reg, regs) ({ \
62 struct reg_window32 __user *win; \
63 register unsigned long ret; \
65 if (!(reg)) ret = 0; \
66 else if ((reg) < 16) { \
67 ret = regs->u_regs[(reg)]; \
69 /* Ho hum, the slightly complicated case. */ \
70 win = (struct reg_window32 __user *)regs->u_regs[UREG_FP];\
71 if (get_user (ret, &win->locals[(reg) - 16])) return -1;\
77 store_reg(unsigned int result, unsigned int reg, struct pt_regs *regs)
79 struct reg_window32 __user *win;
84 regs->u_regs[reg] = result;
87 /* need to use put_user() in this case: */
88 win = (struct reg_window32 __user *) regs->u_regs[UREG_FP];
89 return (put_user(result, &win->locals[reg - 16]));
93 /* Should return 0 if mul/div emulation succeeded and SIGILL should
96 int do_user_muldiv(struct pt_regs *regs, unsigned long pc)
100 unsigned int rs1, rs2, rdv;
103 return -1; /* This happens to often, I think */
104 if (get_user (insn, (unsigned int __user *)pc))
106 if ((insn & 0xc1400000) != 0x80400000)
108 inst = ((insn >> 19) & 0xf);
109 if ((inst & 0xe) != 10 && (inst & 0xe) != 14)
112 /* Now we know we have to do something with umul, smul, udiv or sdiv */
113 rs1 = (insn >> 14) & 0x1f;
115 rdv = (insn >> 25) & 0x1f;
116 if (has_imm13(insn)) {
117 maybe_flush_windows(rs1, 0, rdv);
118 rs2 = sign_extend_imm13(insn);
120 maybe_flush_windows(rs1, rs2, rdv);
121 rs2 = fetch_reg(rs2, regs);
123 rs1 = fetch_reg(rs1, regs);
127 printk ("unsigned muldiv: 0x%x * 0x%x = ", rs1, rs2);
129 __asm__ __volatile__ ("\n\t"
135 : "=r" (rs1), "=r" (rs2)
136 : "0" (rs1), "1" (rs2)
137 : "o0", "o1", "o2", "o3", "o4", "o5", "o7", "cc");
139 printk ("0x%x%08x\n", rs2, rs1);
141 if (store_reg(rs1, rdv, regs))
147 printk ("signed muldiv: 0x%x * 0x%x = ", rs1, rs2);
149 __asm__ __volatile__ ("\n\t"
155 : "=r" (rs1), "=r" (rs2)
156 : "0" (rs1), "1" (rs2)
157 : "o0", "o1", "o2", "o3", "o4", "o5", "o7", "cc");
159 printk ("0x%x%08x\n", rs2, rs1);
161 if (store_reg(rs1, rdv, regs))
167 printk ("unsigned muldiv: 0x%x%08x / 0x%x = ", regs->y, rs1, rs2);
171 printk ("DIVISION BY ZERO\n");
173 handle_hw_divzero (regs, pc, regs->npc, regs->psr);
176 __asm__ __volatile__ ("\n\t"
184 : "=r" (rs1), "=r" (rs2)
185 : "r" (regs->y), "0" (rs1), "1" (rs2)
186 : "o0", "o1", "o2", "o3", "o4", "o5", "o7",
187 "g1", "g2", "g3", "cc");
189 printk ("0x%x\n", rs1);
191 if (store_reg(rs1, rdv, regs))
196 printk ("signed muldiv: 0x%x%08x / 0x%x = ", regs->y, rs1, rs2);
200 printk ("DIVISION BY ZERO\n");
202 handle_hw_divzero (regs, pc, regs->npc, regs->psr);
205 __asm__ __volatile__ ("\n\t"
213 : "=r" (rs1), "=r" (rs2)
214 : "r" (regs->y), "0" (rs1), "1" (rs2)
215 : "o0", "o1", "o2", "o3", "o4", "o5", "o7",
216 "g1", "g2", "g3", "cc");
218 printk ("0x%x\n", rs1);
220 if (store_reg(rs1, rdv, regs))
224 if (is_foocc (insn)) {
225 regs->psr &= ~PSR_ICC;
226 if ((inst & 0xe) == 14) {
228 if (rs2) regs->psr |= PSR_V;
230 if (!rs1) regs->psr |= PSR_Z;
231 if (((int)rs1) < 0) regs->psr |= PSR_N;
233 printk ("psr muldiv: %08x\n", regs->psr);