1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67#include <linux/types.h>
68#include <linux/sched.h>
69#include <linux/mm.h>
70#include <linux/perf_event.h>
71#include <asm/uaccess.h>
72
73#include "sfp-util_32.h"
74#include <math-emu/soft-fp.h>
75#include <math-emu/single.h>
76#include <math-emu/double.h>
77#include <math-emu/quad.h>
78
79#define FLOATFUNC(x) extern int x(void *,void *,void *)
80
81
82
83
84
85#define FSQRTQ 0x02b
86#define FADDQ 0x043
87#define FSUBQ 0x047
88#define FMULQ 0x04b
89#define FDIVQ 0x04f
90#define FDMULQ 0x06e
91#define FQTOS 0x0c7
92#define FQTOD 0x0cb
93#define FITOQ 0x0cc
94#define FSTOQ 0x0cd
95#define FDTOQ 0x0ce
96#define FQTOI 0x0d3
97#define FCMPQ 0x053
98#define FCMPEQ 0x057
99
100#define FSQRTS 0x029
101#define FSQRTD 0x02a
102#define FADDS 0x041
103#define FADDD 0x042
104#define FSUBS 0x045
105#define FSUBD 0x046
106#define FMULS 0x049
107#define FMULD 0x04a
108#define FDIVS 0x04d
109#define FDIVD 0x04e
110#define FSMULD 0x069
111#define FDTOS 0x0c6
112#define FSTOD 0x0c9
113#define FSTOI 0x0d1
114#define FDTOI 0x0d2
115#define FABSS 0x009
116#define FCMPS 0x051
117#define FCMPES 0x055
118#define FCMPD 0x052
119#define FCMPED 0x056
120#define FMOVS 0x001
121#define FNEGS 0x005
122#define FITOS 0x0c4
123#define FITOD 0x0c8
124
125#define FSR_TEM_SHIFT 23UL
126#define FSR_TEM_MASK (0x1fUL << FSR_TEM_SHIFT)
127#define FSR_AEXC_SHIFT 5UL
128#define FSR_AEXC_MASK (0x1fUL << FSR_AEXC_SHIFT)
129#define FSR_CEXC_SHIFT 0UL
130#define FSR_CEXC_MASK (0x1fUL << FSR_CEXC_SHIFT)
131
132static int do_one_mathemu(u32 insn, unsigned long *fsr, unsigned long *fregs);
133
134
135
136
137
138
139
140
141
142
143int do_mathemu(struct pt_regs *regs, struct task_struct *fpt)
144{
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163 int i;
164 int retcode = 0;
165 unsigned long insn;
166
167 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, 0, regs, 0);
168
169#ifdef DEBUG_MATHEMU
170 printk("In do_mathemu()... pc is %08lx\n", regs->pc);
171 printk("fpqdepth is %ld\n", fpt->thread.fpqdepth);
172 for (i = 0; i < fpt->thread.fpqdepth; i++)
173 printk("%d: %08lx at %08lx\n", i, fpt->thread.fpqueue[i].insn,
174 (unsigned long)fpt->thread.fpqueue[i].insn_addr);
175#endif
176
177 if (fpt->thread.fpqdepth == 0) {
178#ifdef DEBUG_MATHEMU
179 printk("precise trap at %08lx\n", regs->pc);
180#endif
181 if (!get_user(insn, (u32 __user *) regs->pc)) {
182 retcode = do_one_mathemu(insn, &fpt->thread.fsr, fpt->thread.float_regs);
183 if (retcode) {
184
185 regs->pc = regs->npc;
186 regs->npc += 4;
187 }
188 }
189 return retcode;
190 }
191
192
193 for (i = 0; i < fpt->thread.fpqdepth; i++) {
194 retcode = do_one_mathemu(fpt->thread.fpqueue[i].insn, &(fpt->thread.fsr), fpt->thread.float_regs);
195 if (!retcode)
196 break;
197 }
198
199 if (retcode)
200 fpt->thread.fsr &= ~(0x3000 | FSR_CEXC_MASK);
201 else
202 fpt->thread.fsr &= ~0x3000;
203 fpt->thread.fpqdepth = 0;
204
205 return retcode;
206}
207
208
209
210
211
212
213
214
215
216static inline int record_exception(unsigned long *pfsr, int eflag)
217{
218 unsigned long fsr = *pfsr;
219 int would_trap;
220
221
222 would_trap = (fsr & ((long)eflag << FSR_TEM_SHIFT)) != 0UL;
223
224
225 if (would_trap != 0) {
226 eflag &= ((fsr & FSR_TEM_MASK) >> FSR_TEM_SHIFT);
227 if ((eflag & (eflag - 1)) != 0) {
228 if (eflag & FP_EX_INVALID)
229 eflag = FP_EX_INVALID;
230 else if (eflag & FP_EX_OVERFLOW)
231 eflag = FP_EX_OVERFLOW;
232 else if (eflag & FP_EX_UNDERFLOW)
233 eflag = FP_EX_UNDERFLOW;
234 else if (eflag & FP_EX_DIVZERO)
235 eflag = FP_EX_DIVZERO;
236 else if (eflag & FP_EX_INEXACT)
237 eflag = FP_EX_INEXACT;
238 }
239 }
240
241
242
243
244
245
246
247 fsr &= ~(FSR_CEXC_MASK);
248 fsr |= ((long)eflag << FSR_CEXC_SHIFT);
249
250
251
252
253
254
255
256 if (would_trap == 0)
257 fsr |= ((long)eflag << FSR_AEXC_SHIFT);
258
259
260 if (would_trap != 0)
261 fsr |= (1UL << 14);
262
263 *pfsr = fsr;
264
265 return (would_trap ? 0 : 1);
266}
267
268typedef union {
269 u32 s;
270 u64 d;
271 u64 q[2];
272} *argp;
273
274static int do_one_mathemu(u32 insn, unsigned long *pfsr, unsigned long *fregs)
275{
276
277 int type = 0;
278
279
280
281#define TYPE(dummy, r, ru, b, bu, a, au) type = (au << 2) | (a << 0) | (bu << 5) | (b << 3) | (ru << 8) | (r << 6)
282 int freg;
283 argp rs1 = NULL, rs2 = NULL, rd = NULL;
284 FP_DECL_EX;
285 FP_DECL_S(SA); FP_DECL_S(SB); FP_DECL_S(SR);
286 FP_DECL_D(DA); FP_DECL_D(DB); FP_DECL_D(DR);
287 FP_DECL_Q(QA); FP_DECL_Q(QB); FP_DECL_Q(QR);
288 int IR;
289 long fsr;
290
291#ifdef DEBUG_MATHEMU
292 printk("In do_mathemu(), emulating %08lx\n", insn);
293#endif
294
295 if ((insn & 0xc1f80000) == 0x81a00000) {
296 switch ((insn >> 5) & 0x1ff) {
297 case FSQRTQ: TYPE(3,3,1,3,1,0,0); break;
298 case FADDQ:
299 case FSUBQ:
300 case FMULQ:
301 case FDIVQ: TYPE(3,3,1,3,1,3,1); break;
302 case FDMULQ: TYPE(3,3,1,2,1,2,1); break;
303 case FQTOS: TYPE(3,1,1,3,1,0,0); break;
304 case FQTOD: TYPE(3,2,1,3,1,0,0); break;
305 case FITOQ: TYPE(3,3,1,1,0,0,0); break;
306 case FSTOQ: TYPE(3,3,1,1,1,0,0); break;
307 case FDTOQ: TYPE(3,3,1,2,1,0,0); break;
308 case FQTOI: TYPE(3,1,0,3,1,0,0); break;
309 case FSQRTS: TYPE(2,1,1,1,1,0,0); break;
310 case FSQRTD: TYPE(2,2,1,2,1,0,0); break;
311 case FADDD:
312 case FSUBD:
313 case FMULD:
314 case FDIVD: TYPE(2,2,1,2,1,2,1); break;
315 case FADDS:
316 case FSUBS:
317 case FMULS:
318 case FDIVS: TYPE(2,1,1,1,1,1,1); break;
319 case FSMULD: TYPE(2,2,1,1,1,1,1); break;
320 case FDTOS: TYPE(2,1,1,2,1,0,0); break;
321 case FSTOD: TYPE(2,2,1,1,1,0,0); break;
322 case FSTOI: TYPE(2,1,0,1,1,0,0); break;
323 case FDTOI: TYPE(2,1,0,2,1,0,0); break;
324 case FITOS: TYPE(2,1,1,1,0,0,0); break;
325 case FITOD: TYPE(2,2,1,1,0,0,0); break;
326 case FMOVS:
327 case FABSS:
328 case FNEGS: TYPE(2,1,0,1,0,0,0); break;
329 }
330 } else if ((insn & 0xc1f80000) == 0x81a80000) {
331 switch ((insn >> 5) & 0x1ff) {
332 case FCMPS: TYPE(3,0,0,1,1,1,1); break;
333 case FCMPES: TYPE(3,0,0,1,1,1,1); break;
334 case FCMPD: TYPE(3,0,0,2,1,2,1); break;
335 case FCMPED: TYPE(3,0,0,2,1,2,1); break;
336 case FCMPQ: TYPE(3,0,0,3,1,3,1); break;
337 case FCMPEQ: TYPE(3,0,0,3,1,3,1); break;
338 }
339 }
340
341 if (!type) {
342#ifdef DEBUG_MATHEMU
343 printk("attempt to emulate unrecognised FPop!\n");
344#endif
345 return 0;
346 }
347
348
349 freg = (*pfsr >> 14) & 0xf;
350
351 *pfsr &= ~0x1c000;
352
353 freg = ((insn >> 14) & 0x1f);
354 switch (type & 0x3) {
355 case 3:
356 if (freg & 3) {
357
358 *pfsr |= (6 << 14);
359 return 0;
360 }
361
362 case 2:
363 if (freg & 1) {
364 *pfsr |= (6 << 14);
365 return 0;
366 }
367 }
368 rs1 = (argp)&fregs[freg];
369 switch (type & 0x7) {
370 case 7: FP_UNPACK_QP (QA, rs1); break;
371 case 6: FP_UNPACK_DP (DA, rs1); break;
372 case 5: FP_UNPACK_SP (SA, rs1); break;
373 }
374 freg = (insn & 0x1f);
375 switch ((type >> 3) & 0x3) {
376 case 3:
377 if (freg & 3) {
378
379 *pfsr |= (6 << 14);
380 return 0;
381 }
382
383 case 2:
384 if (freg & 1) {
385 *pfsr |= (6 << 14);
386 return 0;
387 }
388 }
389 rs2 = (argp)&fregs[freg];
390 switch ((type >> 3) & 0x7) {
391 case 7: FP_UNPACK_QP (QB, rs2); break;
392 case 6: FP_UNPACK_DP (DB, rs2); break;
393 case 5: FP_UNPACK_SP (SB, rs2); break;
394 }
395 freg = ((insn >> 25) & 0x1f);
396 switch ((type >> 6) & 0x3) {
397 case 0:
398 if (freg) {
399
400 *pfsr |= (6 << 14);
401 return 0;
402 }
403 break;
404 case 3:
405 if (freg & 3) {
406
407 *pfsr |= (6 << 14);
408 return 0;
409 }
410
411 case 2:
412 if (freg & 1) {
413 *pfsr |= (6 << 14);
414 return 0;
415 }
416
417 case 1:
418 rd = (void *)&fregs[freg];
419 break;
420 }
421#ifdef DEBUG_MATHEMU
422 printk("executing insn...\n");
423#endif
424
425 switch ((insn >> 5) & 0x1ff) {
426
427 case FADDS: FP_ADD_S (SR, SA, SB); break;
428 case FADDD: FP_ADD_D (DR, DA, DB); break;
429 case FADDQ: FP_ADD_Q (QR, QA, QB); break;
430
431 case FSUBS: FP_SUB_S (SR, SA, SB); break;
432 case FSUBD: FP_SUB_D (DR, DA, DB); break;
433 case FSUBQ: FP_SUB_Q (QR, QA, QB); break;
434
435 case FMULS: FP_MUL_S (SR, SA, SB); break;
436 case FSMULD: FP_CONV (D, S, 2, 1, DA, SA);
437 FP_CONV (D, S, 2, 1, DB, SB);
438 case FMULD: FP_MUL_D (DR, DA, DB); break;
439 case FDMULQ: FP_CONV (Q, D, 4, 2, QA, DA);
440 FP_CONV (Q, D, 4, 2, QB, DB);
441 case FMULQ: FP_MUL_Q (QR, QA, QB); break;
442
443 case FDIVS: FP_DIV_S (SR, SA, SB); break;
444 case FDIVD: FP_DIV_D (DR, DA, DB); break;
445 case FDIVQ: FP_DIV_Q (QR, QA, QB); break;
446
447 case FSQRTS: FP_SQRT_S (SR, SB); break;
448 case FSQRTD: FP_SQRT_D (DR, DB); break;
449 case FSQRTQ: FP_SQRT_Q (QR, QB); break;
450
451 case FMOVS: rd->s = rs2->s; break;
452 case FABSS: rd->s = rs2->s & 0x7fffffff; break;
453 case FNEGS: rd->s = rs2->s ^ 0x80000000; break;
454
455 case FSTOI: FP_TO_INT_S (IR, SB, 32, 1); break;
456 case FDTOI: FP_TO_INT_D (IR, DB, 32, 1); break;
457 case FQTOI: FP_TO_INT_Q (IR, QB, 32, 1); break;
458
459 case FITOS: IR = rs2->s; FP_FROM_INT_S (SR, IR, 32, int); break;
460 case FITOD: IR = rs2->s; FP_FROM_INT_D (DR, IR, 32, int); break;
461 case FITOQ: IR = rs2->s; FP_FROM_INT_Q (QR, IR, 32, int); break;
462
463 case FSTOD: FP_CONV (D, S, 2, 1, DR, SB); break;
464 case FSTOQ: FP_CONV (Q, S, 4, 1, QR, SB); break;
465 case FDTOQ: FP_CONV (Q, D, 4, 2, QR, DB); break;
466 case FDTOS: FP_CONV (S, D, 1, 2, SR, DB); break;
467 case FQTOS: FP_CONV (S, Q, 1, 4, SR, QB); break;
468 case FQTOD: FP_CONV (D, Q, 2, 4, DR, QB); break;
469
470 case FCMPS:
471 case FCMPES:
472 FP_CMP_S(IR, SB, SA, 3);
473 if (IR == 3 &&
474 (((insn >> 5) & 0x1ff) == FCMPES ||
475 FP_ISSIGNAN_S(SA) ||
476 FP_ISSIGNAN_S(SB)))
477 FP_SET_EXCEPTION (FP_EX_INVALID);
478 break;
479 case FCMPD:
480 case FCMPED:
481 FP_CMP_D(IR, DB, DA, 3);
482 if (IR == 3 &&
483 (((insn >> 5) & 0x1ff) == FCMPED ||
484 FP_ISSIGNAN_D(DA) ||
485 FP_ISSIGNAN_D(DB)))
486 FP_SET_EXCEPTION (FP_EX_INVALID);
487 break;
488 case FCMPQ:
489 case FCMPEQ:
490 FP_CMP_Q(IR, QB, QA, 3);
491 if (IR == 3 &&
492 (((insn >> 5) & 0x1ff) == FCMPEQ ||
493 FP_ISSIGNAN_Q(QA) ||
494 FP_ISSIGNAN_Q(QB)))
495 FP_SET_EXCEPTION (FP_EX_INVALID);
496 }
497 if (!FP_INHIBIT_RESULTS) {
498 switch ((type >> 6) & 0x7) {
499 case 0: fsr = *pfsr;
500 if (IR == -1) IR = 2;
501
502 fsr &= ~0xc00; fsr |= (IR << 10); break;
503 *pfsr = fsr;
504 break;
505 case 1: rd->s = IR; break;
506 case 5: FP_PACK_SP (rd, SR); break;
507 case 6: FP_PACK_DP (rd, DR); break;
508 case 7: FP_PACK_QP (rd, QR); break;
509 }
510 }
511 if (_fex == 0)
512 return 1;
513 return record_exception(pfsr, _fex);
514}
515