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)
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 
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>
19 
20 /* #define DEBUG_MULDIV */
21 
has_imm13(int insn)22 static inline int has_imm13(int insn)
23 {
24 	return (insn & 0x2000);
25 }
26 
is_foocc(int insn)27 static inline int is_foocc(int insn)
28 {
29 	return (insn & 0x800000);
30 }
31 
sign_extend_imm13(int imm)32 static inline int sign_extend_imm13(int imm)
33 {
34 	return imm << 19 >> 19;
35 }
36 
advance(struct pt_regs * regs)37 static inline void advance(struct pt_regs *regs)
38 {
39 	regs->pc   = regs->npc;
40 	regs->npc += 4;
41 }
42 
maybe_flush_windows(unsigned int rs1,unsigned int rs2,unsigned int rd)43 static inline void maybe_flush_windows(unsigned int rs1, unsigned int rs2,
44 				       unsigned int rd)
45 {
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");
57 	}
58 }
59 
60 #define fetch_reg(reg, regs) ({							\
61 	struct reg_window *win;							\
62 	register unsigned long ret;						\
63 										\
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 *)regs->u_regs[UREG_FP];		\
70 		if (get_user (ret, &win->locals[(reg) - 16])) return -1;	\
71 	}									\
72 	ret;									\
73 })
74 
75 static inline int
store_reg(unsigned int result,unsigned int reg,struct pt_regs * regs)76 store_reg(unsigned int result, unsigned int reg, struct pt_regs *regs)
77 {
78 	struct reg_window *win;
79 
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 *)regs->u_regs[UREG_FP];
88 		return (put_user(result, &win->locals[reg - 16]));
89 	}
90 }
91 
92 extern void handle_hw_divzero (struct pt_regs *regs, unsigned long pc,
93 			       unsigned long npc, unsigned long psr);
94 
95 /* Should return 0 if mul/div emulation succeeded and SIGILL should not be issued */
do_user_muldiv(struct pt_regs * regs,unsigned long pc)96 int do_user_muldiv(struct pt_regs *regs, unsigned long pc)
97 {
98 	unsigned int insn;
99 	int inst;
100 	unsigned int rs1, rs2, rdv;
101 
102 	if (!pc) return -1; /* This happens to often, I think */
103 	if (get_user (insn, (unsigned int *)pc)) return -1;
104 	if ((insn & 0xc1400000) != 0x80400000) return -1;
105 	inst = ((insn >> 19) & 0xf);
106 	if ((inst & 0xe) != 10 && (inst & 0xe) != 14) return -1;
107 	/* Now we know we have to do something with umul, smul, udiv or sdiv */
108 	rs1 = (insn >> 14) & 0x1f;
109 	rs2 = insn & 0x1f;
110 	rdv = (insn >> 25) & 0x1f;
111 	if(has_imm13(insn)) {
112 		maybe_flush_windows(rs1, 0, rdv);
113 		rs2 = sign_extend_imm13(insn);
114 	} else {
115 		maybe_flush_windows(rs1, rs2, rdv);
116 		rs2 = fetch_reg(rs2, regs);
117 	}
118 	rs1 = fetch_reg(rs1, regs);
119 	switch (inst) {
120 	case 10: /* umul */
121 #ifdef DEBUG_MULDIV
122 		printk ("unsigned muldiv: 0x%x * 0x%x = ", rs1, rs2);
123 #endif
124 		__asm__ __volatile__ ("\n\t"
125 			"mov	%0, %%o0\n\t"
126 			"call	.umul\n\t"
127 			" mov	%1, %%o1\n\t"
128 			"mov	%%o0, %0\n\t"
129 			"mov	%%o1, %1\n\t"
130 			: "=r" (rs1), "=r" (rs2)
131 		        : "0" (rs1), "1" (rs2)
132 			: "o0", "o1", "o2", "o3", "o4", "o5", "o7", "cc");
133 #ifdef DEBUG_MULDIV
134 		printk ("0x%x%08x\n", rs2, rs1);
135 #endif
136 		if (store_reg(rs1, rdv, regs))
137 			return -1;
138 		regs->y = rs2;
139 		break;
140 	case 11: /* smul */
141 #ifdef DEBUG_MULDIV
142 		printk ("signed muldiv: 0x%x * 0x%x = ", rs1, rs2);
143 #endif
144 		__asm__ __volatile__ ("\n\t"
145 			"mov	%0, %%o0\n\t"
146 			"call	.mul\n\t"
147 			" mov	%1, %%o1\n\t"
148 			"mov	%%o0, %0\n\t"
149 			"mov	%%o1, %1\n\t"
150 			: "=r" (rs1), "=r" (rs2)
151 		        : "0" (rs1), "1" (rs2)
152 			: "o0", "o1", "o2", "o3", "o4", "o5", "o7", "cc");
153 #ifdef DEBUG_MULDIV
154 		printk ("0x%x%08x\n", rs2, rs1);
155 #endif
156 		if (store_reg(rs1, rdv, regs))
157 			return -1;
158 		regs->y = rs2;
159 		break;
160 	case 14: /* udiv */
161 #ifdef DEBUG_MULDIV
162 		printk ("unsigned muldiv: 0x%x%08x / 0x%x = ", regs->y, rs1, rs2);
163 #endif
164 		if (!rs2) {
165 #ifdef DEBUG_MULDIV
166 			printk ("DIVISION BY ZERO\n");
167 #endif
168 			handle_hw_divzero (regs, pc, regs->npc, regs->psr);
169 			return 0;
170 		}
171 		__asm__ __volatile__ ("\n\t"
172 			"mov	%2, %%o0\n\t"
173 			"mov	%0, %%o1\n\t"
174 			"mov	%%g0, %%o2\n\t"
175 			"call	__udivdi3\n\t"
176 			" mov	%1, %%o3\n\t"
177 			"mov	%%o1, %0\n\t"
178 			"mov	%%o0, %1\n\t"
179 			: "=r" (rs1), "=r" (rs2)
180 			: "r" (regs->y), "0" (rs1), "1" (rs2)
181 			: "o0", "o1", "o2", "o3", "o4", "o5", "o7",
182 			  "g1", "g2", "g3", "cc");
183 #ifdef DEBUG_MULDIV
184 		printk ("0x%x\n", rs1);
185 #endif
186 		if (store_reg(rs1, rdv, regs))
187 			return -1;
188 		break;
189 	case 15: /* sdiv */
190 #ifdef DEBUG_MULDIV
191 		printk ("signed muldiv: 0x%x%08x / 0x%x = ", regs->y, rs1, rs2);
192 #endif
193 		if (!rs2) {
194 #ifdef DEBUG_MULDIV
195 			printk ("DIVISION BY ZERO\n");
196 #endif
197 			handle_hw_divzero (regs, pc, regs->npc, regs->psr);
198 			return 0;
199 		}
200 		__asm__ __volatile__ ("\n\t"
201 			"mov	%2, %%o0\n\t"
202 			"mov	%0, %%o1\n\t"
203 			"mov	%%g0, %%o2\n\t"
204 			"call	__divdi3\n\t"
205 			" mov	%1, %%o3\n\t"
206 			"mov	%%o1, %0\n\t"
207 			"mov	%%o0, %1\n\t"
208 			: "=r" (rs1), "=r" (rs2)
209 			: "r" (regs->y), "0" (rs1), "1" (rs2)
210 			: "o0", "o1", "o2", "o3", "o4", "o5", "o7",
211 			  "g1", "g2", "g3", "cc");
212 #ifdef DEBUG_MULDIV
213 		printk ("0x%x\n", rs1);
214 #endif
215 		if (store_reg(rs1, rdv, regs))
216 			return -1;
217 		break;
218 	}
219 	if (is_foocc (insn)) {
220 		regs->psr &= ~PSR_ICC;
221 		if ((inst & 0xe) == 14) {
222 			/* ?div */
223 			if (rs2) regs->psr |= PSR_V;
224 		}
225 		if (!rs1) regs->psr |= PSR_Z;
226 		if (((int)rs1) < 0) regs->psr |= PSR_N;
227 #ifdef DEBUG_MULDIV
228 		printk ("psr muldiv: %08x\n", regs->psr);
229 #endif
230 	}
231 	advance(regs);
232 	return 0;
233 }
234