Linux 2.6.26-rc5
[linux-2.6/openmoko-kernel/knife-kernel.git] / arch / sparc / kernel / muldiv.c
blobe352239e72c8f7423a7a70110f52bd8136ca5a2a
1 /*
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>
14 #include <linux/mm.h>
15 #include <asm/ptrace.h>
16 #include <asm/processor.h>
17 #include <asm/system.h>
18 #include <asm/uaccess.h>
20 /* #define DEBUG_MULDIV */
22 static inline int has_imm13(int insn)
24 return (insn & 0x2000);
27 static inline int is_foocc(int insn)
29 return (insn & 0x800000);
32 static inline int sign_extend_imm13(int imm)
34 return imm << 19 >> 19;
37 static inline void advance(struct pt_regs *regs)
39 regs->pc = regs->npc;
40 regs->npc += 4;
43 static inline void maybe_flush_windows(unsigned int rs1, unsigned int rs2,
44 unsigned int rd)
46 if(rs2 >= 16 || rs1 >= 16 || rd >= 16) {
47 /* Wheee... */
48 __asm__ __volatile__("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 "save %sp, -0x40, %sp\n\t"
53 "save %sp, -0x40, %sp\n\t"
54 "save %sp, -0x40, %sp\n\t"
55 "restore; restore; restore; restore;\n\t"
56 "restore; restore; restore;\n\t");
60 #define fetch_reg(reg, regs) ({ \
61 struct reg_window __user *win; \
62 register unsigned long ret; \
64 if (!(reg)) ret = 0; \
65 else if ((reg) < 16) { \
66 ret = regs->u_regs[(reg)]; \
67 } else { \
68 /* Ho hum, the slightly complicated case. */ \
69 win = (struct reg_window __user *)regs->u_regs[UREG_FP];\
70 if (get_user (ret, &win->locals[(reg) - 16])) return -1;\
71 } \
72 ret; \
75 static inline int
76 store_reg(unsigned int result, unsigned int reg, struct pt_regs *regs)
78 struct reg_window __user *win;
80 if (!reg)
81 return 0;
82 if (reg < 16) {
83 regs->u_regs[reg] = result;
84 return 0;
85 } else {
86 /* need to use put_user() in this case: */
87 win = (struct reg_window __user *) regs->u_regs[UREG_FP];
88 return (put_user(result, &win->locals[reg - 16]));
92 extern void handle_hw_divzero (struct pt_regs *regs, unsigned long pc,
93 unsigned long npc, unsigned long psr);
95 /* Should return 0 if mul/div emulation succeeded and SIGILL should
96 * not be issued.
98 int do_user_muldiv(struct pt_regs *regs, unsigned long pc)
100 unsigned int insn;
101 int inst;
102 unsigned int rs1, rs2, rdv;
104 if (!pc)
105 return -1; /* This happens to often, I think */
106 if (get_user (insn, (unsigned int __user *)pc))
107 return -1;
108 if ((insn & 0xc1400000) != 0x80400000)
109 return -1;
110 inst = ((insn >> 19) & 0xf);
111 if ((inst & 0xe) != 10 && (inst & 0xe) != 14)
112 return -1;
114 /* Now we know we have to do something with umul, smul, udiv or sdiv */
115 rs1 = (insn >> 14) & 0x1f;
116 rs2 = insn & 0x1f;
117 rdv = (insn >> 25) & 0x1f;
118 if (has_imm13(insn)) {
119 maybe_flush_windows(rs1, 0, rdv);
120 rs2 = sign_extend_imm13(insn);
121 } else {
122 maybe_flush_windows(rs1, rs2, rdv);
123 rs2 = fetch_reg(rs2, regs);
125 rs1 = fetch_reg(rs1, regs);
126 switch (inst) {
127 case 10: /* umul */
128 #ifdef DEBUG_MULDIV
129 printk ("unsigned muldiv: 0x%x * 0x%x = ", rs1, rs2);
130 #endif
131 __asm__ __volatile__ ("\n\t"
132 "mov %0, %%o0\n\t"
133 "call .umul\n\t"
134 " mov %1, %%o1\n\t"
135 "mov %%o0, %0\n\t"
136 "mov %%o1, %1\n\t"
137 : "=r" (rs1), "=r" (rs2)
138 : "0" (rs1), "1" (rs2)
139 : "o0", "o1", "o2", "o3", "o4", "o5", "o7", "cc");
140 #ifdef DEBUG_MULDIV
141 printk ("0x%x%08x\n", rs2, rs1);
142 #endif
143 if (store_reg(rs1, rdv, regs))
144 return -1;
145 regs->y = rs2;
146 break;
147 case 11: /* smul */
148 #ifdef DEBUG_MULDIV
149 printk ("signed muldiv: 0x%x * 0x%x = ", rs1, rs2);
150 #endif
151 __asm__ __volatile__ ("\n\t"
152 "mov %0, %%o0\n\t"
153 "call .mul\n\t"
154 " mov %1, %%o1\n\t"
155 "mov %%o0, %0\n\t"
156 "mov %%o1, %1\n\t"
157 : "=r" (rs1), "=r" (rs2)
158 : "0" (rs1), "1" (rs2)
159 : "o0", "o1", "o2", "o3", "o4", "o5", "o7", "cc");
160 #ifdef DEBUG_MULDIV
161 printk ("0x%x%08x\n", rs2, rs1);
162 #endif
163 if (store_reg(rs1, rdv, regs))
164 return -1;
165 regs->y = rs2;
166 break;
167 case 14: /* udiv */
168 #ifdef DEBUG_MULDIV
169 printk ("unsigned muldiv: 0x%x%08x / 0x%x = ", regs->y, rs1, rs2);
170 #endif
171 if (!rs2) {
172 #ifdef DEBUG_MULDIV
173 printk ("DIVISION BY ZERO\n");
174 #endif
175 handle_hw_divzero (regs, pc, regs->npc, regs->psr);
176 return 0;
178 __asm__ __volatile__ ("\n\t"
179 "mov %2, %%o0\n\t"
180 "mov %0, %%o1\n\t"
181 "mov %%g0, %%o2\n\t"
182 "call __udivdi3\n\t"
183 " mov %1, %%o3\n\t"
184 "mov %%o1, %0\n\t"
185 "mov %%o0, %1\n\t"
186 : "=r" (rs1), "=r" (rs2)
187 : "r" (regs->y), "0" (rs1), "1" (rs2)
188 : "o0", "o1", "o2", "o3", "o4", "o5", "o7",
189 "g1", "g2", "g3", "cc");
190 #ifdef DEBUG_MULDIV
191 printk ("0x%x\n", rs1);
192 #endif
193 if (store_reg(rs1, rdv, regs))
194 return -1;
195 break;
196 case 15: /* sdiv */
197 #ifdef DEBUG_MULDIV
198 printk ("signed muldiv: 0x%x%08x / 0x%x = ", regs->y, rs1, rs2);
199 #endif
200 if (!rs2) {
201 #ifdef DEBUG_MULDIV
202 printk ("DIVISION BY ZERO\n");
203 #endif
204 handle_hw_divzero (regs, pc, regs->npc, regs->psr);
205 return 0;
207 __asm__ __volatile__ ("\n\t"
208 "mov %2, %%o0\n\t"
209 "mov %0, %%o1\n\t"
210 "mov %%g0, %%o2\n\t"
211 "call __divdi3\n\t"
212 " mov %1, %%o3\n\t"
213 "mov %%o1, %0\n\t"
214 "mov %%o0, %1\n\t"
215 : "=r" (rs1), "=r" (rs2)
216 : "r" (regs->y), "0" (rs1), "1" (rs2)
217 : "o0", "o1", "o2", "o3", "o4", "o5", "o7",
218 "g1", "g2", "g3", "cc");
219 #ifdef DEBUG_MULDIV
220 printk ("0x%x\n", rs1);
221 #endif
222 if (store_reg(rs1, rdv, regs))
223 return -1;
224 break;
226 if (is_foocc (insn)) {
227 regs->psr &= ~PSR_ICC;
228 if ((inst & 0xe) == 14) {
229 /* ?div */
230 if (rs2) regs->psr |= PSR_V;
232 if (!rs1) regs->psr |= PSR_Z;
233 if (((int)rs1) < 0) regs->psr |= PSR_N;
234 #ifdef DEBUG_MULDIV
235 printk ("psr muldiv: %08x\n", regs->psr);
236 #endif
238 advance(regs);
239 return 0;