1 /* $Id: muldiv.c,v 1.5 1997/12/15 20:07:20 ecd Exp $
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)
9 #include <linux/kernel.h>
10 #include <linux/sched.h>
12 #include <asm/ptrace.h>
13 #include <asm/processor.h>
14 #include <asm/system.h>
15 #include <asm/uaccess.h>
17 /* #define DEBUG_MULDIV */
19 static inline int has_imm13(int insn)
21 return (insn & 0x2000);
24 static inline int is_foocc(int insn)
26 return (insn & 0x800000);
29 static inline int sign_extend_imm13(int imm)
31 return imm << 19 >> 19;
34 static inline void advance(struct pt_regs *regs)
40 static inline void maybe_flush_windows(unsigned int rs1, unsigned int rs2,
43 if(rs2 >= 16 || rs1 >= 16 || rd >= 16) {
45 __asm__ __volatile__("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 "save %sp, -0x40, %sp\n\t"
50 "save %sp, -0x40, %sp\n\t"
51 "save %sp, -0x40, %sp\n\t"
52 "restore; restore; restore; restore;\n\t"
53 "restore; restore; restore;\n\t");
57 #define fetch_reg(reg, regs) ({ \
58 struct reg_window *win; \
59 register unsigned long ret; \
61 if (!(reg)) ret = 0; \
62 else if((reg) < 16) { \
63 ret = regs->u_regs[(reg)]; \
65 /* Ho hum, the slightly complicated case. */ \
66 win = (struct reg_window *)regs->u_regs[UREG_FP]; \
67 if (get_user (ret, &win->locals[(reg) - 16])) return -1; \
73 store_reg(unsigned int result, unsigned int reg, struct pt_regs *regs)
75 struct reg_window *win;
80 regs->u_regs[reg] = result;
83 /* need to use put_user() in this case: */
84 win = (struct reg_window *)regs->u_regs[UREG_FP];
85 return (put_user(result, &win->locals[reg - 16]));
89 extern void handle_hw_divzero (struct pt_regs *regs, unsigned long pc,
90 unsigned long npc, unsigned long psr);
92 /* Should return 0 if mul/div emulation succeeded and SIGILL should not be issued */
93 int do_user_muldiv(struct pt_regs *regs, unsigned long pc)
97 unsigned int rs1, rs2, rdv;
99 if (!pc) return -1; /* This happens to often, I think */
100 if (get_user (insn, (unsigned int *)pc)) return -1;
101 if ((insn & 0xc1400000) != 0x80400000) return -1;
102 inst = ((insn >> 19) & 0xf);
103 if ((inst & 0xe) != 10 && (inst & 0xe) != 14) return -1;
104 /* Now we know we have to do something with umul, smul, udiv or sdiv */
105 rs1 = (insn >> 14) & 0x1f;
107 rdv = (insn >> 25) & 0x1f;
108 if(has_imm13(insn)) {
109 maybe_flush_windows(rs1, 0, rdv);
110 rs2 = sign_extend_imm13(insn);
112 maybe_flush_windows(rs1, rs2, rdv);
113 rs2 = fetch_reg(rs2, regs);
115 rs1 = fetch_reg(rs1, regs);
119 printk ("unsigned muldiv: 0x%x * 0x%x = ", rs1, rs2);
121 __asm__ __volatile__ ("\n\t"
127 : "=r" (rs1), "=r" (rs2)
129 : "o0", "o1", "o2", "o3", "o4", "o5", "o7", "cc");
131 printk ("0x%x%08x\n", rs2, rs1);
133 if (store_reg(rs1, rdv, regs))
139 printk ("signed muldiv: 0x%x * 0x%x = ", rs1, rs2);
141 __asm__ __volatile__ ("\n\t"
147 : "=r" (rs1), "=r" (rs2)
149 : "o0", "o1", "o2", "o3", "o4", "o5", "o7", "cc");
151 printk ("0x%x%08x\n", rs2, rs1);
153 if (store_reg(rs1, rdv, regs))
159 printk ("unsigned muldiv: 0x%x%08x / 0x%x = ", regs->y, rs1, rs2);
163 printk ("DIVISION BY ZERO\n");
165 handle_hw_divzero (regs, pc, regs->npc, regs->psr);
168 __asm__ __volatile__ ("\n\t"
176 : "=r" (rs1), "=r" (rs2)
178 : "o0", "o1", "o2", "o3", "o4", "o5", "o7",
179 "g1", "g2", "g3", "cc");
181 printk ("0x%x\n", rs1);
183 if (store_reg(rs1, rdv, regs))
188 printk ("signed muldiv: 0x%x%08x / 0x%x = ", regs->y, rs1, rs2);
192 printk ("DIVISION BY ZERO\n");
194 handle_hw_divzero (regs, pc, regs->npc, regs->psr);
197 __asm__ __volatile__ ("\n\t"
205 : "=r" (rs1), "=r" (rs2)
207 : "o0", "o1", "o2", "o3", "o4", "o5", "o7",
208 "g1", "g2", "g3", "cc");
210 printk ("0x%x\n", rs1);
212 if (store_reg(rs1, rdv, regs))
216 if (is_foocc (insn)) {
217 regs->psr &= ~PSR_ICC;
218 if ((inst & 0xe) == 14) {
220 if (rs2) regs->psr |= PSR_V;
222 if (!rs1) regs->psr |= PSR_Z;
223 if (((int)rs1) < 0) regs->psr |= PSR_N;
225 printk ("psr muldiv: %08x\n", regs->psr);