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