5 #include <linux/kernel.h>
6 #include <linux/errno.h>
8 #include <linux/perf_event.h>
10 #include <asm/ptrace.h>
13 #include <asm/uaccess.h>
14 #include <asm/cacheflush.h>
19 #define FPACK16_OPF 0x03b
22 #define FPACK32_OPF 0x03a
25 #define FPACKFIX_OPF 0x03d
28 #define FEXPAND_OPF 0x04d
31 #define FPMERGE_OPF 0x04b
34 #define FMUL8x16_OPF 0x031
37 #define FMUL8x16AU_OPF 0x033
40 #define FMUL8x16AL_OPF 0x035
43 #define FMUL8SUx16_OPF 0x036
46 #define FMUL8ULx16_OPF 0x037
49 #define FMULD8SUx16_OPF 0x038
52 #define FMULD8ULx16_OPF 0x039
55 #define FCMPGT16_OPF 0x028
58 #define FCMPGT32_OPF 0x02c
61 #define FCMPLE16_OPF 0x020
64 #define FCMPLE32_OPF 0x024
67 #define FCMPNE16_OPF 0x022
70 #define FCMPNE32_OPF 0x026
73 #define FCMPEQ16_OPF 0x02a
76 #define FCMPEQ32_OPF 0x02e
79 #define EDGE8_OPF 0x000
82 #define EDGE8N_OPF 0x001
85 #define EDGE8L_OPF 0x002
88 #define EDGE8LN_OPF 0x003
91 #define EDGE16_OPF 0x004
94 #define EDGE16N_OPF 0x005
97 #define EDGE16L_OPF 0x006
100 #define EDGE16LN_OPF 0x007
103 #define EDGE32_OPF 0x008
106 #define EDGE32N_OPF 0x009
109 #define EDGE32L_OPF 0x00a
112 #define EDGE32LN_OPF 0x00b
115 #define PDIST_OPF 0x03e
118 #define ARRAY8_OPF 0x010
121 #define ARRAY16_OPF 0x012
124 #define ARRAY32_OPF 0x014
127 #define BMASK_OPF 0x019
130 #define BSHUFFLE_OPF 0x04c
132 #define VIS_OPF_SHIFT 5
133 #define VIS_OPF_MASK (0x1ff << VIS_OPF_SHIFT)
135 #define RS1(INSN) (((INSN) >> 14) & 0x1f)
136 #define RS2(INSN) (((INSN) >> 0) & 0x1f)
137 #define RD(INSN) (((INSN) >> 25) & 0x1f)
139 static inline void maybe_flush_windows(
unsigned int rs1,
unsigned int rs2,
140 unsigned int rd,
int from_kernel)
142 if (rs2 >= 16 || rs1 >= 16 || rd >= 16) {
143 if (from_kernel != 0)
144 __asm__ __volatile__(
"flushw");
150 static unsigned long fetch_reg(
unsigned int reg,
struct pt_regs *
regs)
155 return (!reg ? 0 : regs->
u_regs[reg]);
160 struct reg_window *
win;
162 value = win->locals[reg - 16];
163 }
else if (!test_thread_64bit_stack(fp)) {
170 get_user(value, &win->locals[reg - 16]);
175 static inline unsigned long __user *__fetch_reg_addr_user(
unsigned int reg,
183 if (!test_thread_64bit_stack(fp)) {
186 return (
unsigned long __user *)&win32->
locals[reg - 16];
190 return &win->locals[reg - 16];
194 static inline unsigned long *__fetch_reg_addr_kern(
unsigned int reg,
203 static void store_reg(
struct pt_regs *regs,
unsigned long val,
unsigned long rd)
206 unsigned long *rd_kern = __fetch_reg_addr_kern(rd, regs);
210 unsigned long __user *rd_user = __fetch_reg_addr_user(rd, regs);
219 static inline unsigned long fpd_regval(
struct fpustate *
f,
220 unsigned int insn_regnum)
222 insn_regnum = (((insn_regnum & 1) << 5) |
223 (insn_regnum & 0x1e));
225 return *(
unsigned long *) &f->
regs[insn_regnum];
228 static inline unsigned long *fpd_regaddr(
struct fpustate *
f,
229 unsigned int insn_regnum)
231 insn_regnum = (((insn_regnum & 1) << 5) |
232 (insn_regnum & 0x1e));
234 return (
unsigned long *) &f->
regs[insn_regnum];
237 static inline unsigned int fps_regval(
struct fpustate *f,
238 unsigned int insn_regnum)
240 return f->
regs[insn_regnum];
243 static inline unsigned int *fps_regaddr(
struct fpustate *f,
244 unsigned int insn_regnum)
246 return &f->
regs[insn_regnum];
252 static struct edge_tab edge8_tab[8] = {
262 static struct edge_tab edge8_tab_l[8] = {
272 static struct edge_tab edge16_tab[4] = {
278 static struct edge_tab edge16_tab_l[4] = {
284 static struct edge_tab edge32_tab[2] = {
288 static struct edge_tab edge32_tab_l[2] = {
293 static void edge(
struct pt_regs *regs,
unsigned int insn,
unsigned int opf)
295 unsigned long orig_rs1, rs1, orig_rs2, rs2, rd_val;
298 maybe_flush_windows(
RS1(insn),
RS2(insn),
RD(insn), 0);
299 orig_rs1 = rs1 = fetch_reg(
RS1(insn), regs);
300 orig_rs2 = rs2 = fetch_reg(
RS2(insn), regs);
302 if (test_thread_flag(TIF_32BIT)) {
303 rs1 = rs1 & 0xffffffff;
304 rs2 = rs2 & 0xffffffff;
310 left = edge8_tab[rs1 & 0x7].
left;
311 right = edge8_tab[rs2 & 0x7].
right;
315 left = edge8_tab_l[rs1 & 0x7].
left;
316 right = edge8_tab_l[rs2 & 0x7].
right;
321 left = edge16_tab[(rs1 >> 1) & 0x3].left;
322 right = edge16_tab[(rs2 >> 1) & 0x3].right;
327 left = edge16_tab_l[(rs1 >> 1) & 0x3].left;
328 right = edge16_tab_l[(rs2 >> 1) & 0x3].right;
333 left = edge32_tab[(rs1 >> 2) & 0x1].left;
334 right = edge32_tab[(rs2 >> 2) & 0x1].right;
339 left = edge32_tab_l[(rs1 >> 2) & 0x1].left;
340 right = edge32_tab_l[(rs2 >> 2) & 0x1].right;
344 if ((rs1 & ~0x7UL) == (rs2 & ~0x7
UL))
345 rd_val = right &
left;
349 store_reg(regs, rd_val,
RD(insn));
358 unsigned long ccr, tstate;
360 __asm__ __volatile__(
"subcc %1, %2, %%g0\n\t"
363 :
"r" (orig_rs1),
"r" (orig_rs2)
366 regs->tstate = tstate | (ccr << 32
UL);
371 static void array(
struct pt_regs *regs,
unsigned int insn,
unsigned int opf)
373 unsigned long rs1, rs2, rd_val;
374 unsigned int bits, bits_mask;
376 maybe_flush_windows(
RS1(insn),
RS2(insn),
RD(insn), 0);
377 rs1 = fetch_reg(
RS1(insn), regs);
378 rs2 = fetch_reg(
RS2(insn), regs);
380 bits = (rs2 > 5 ? 5 : rs2);
383 rd_val = ((((rs1 >> 11) & 0x3) << 0) |
384 (((rs1 >> 33) & 0x3) << 2) |
385 (((rs1 >> 55) & 0x1) << 4) |
386 (((rs1 >> 13) & 0xf) << 5) |
387 (((rs1 >> 35) & 0xf) << 9) |
388 (((rs1 >> 56) & 0xf) << 13) |
389 (((rs1 >> 17) & bits_mask) << 17) |
390 (((rs1 >> 39) & bits_mask) << (17 + bits)) |
391 (((rs1 >> 60) & 0xf) << (17 + (2*bits))));
402 store_reg(regs, rd_val,
RD(insn));
405 static void bmask(
struct pt_regs *regs,
unsigned int insn)
407 unsigned long rs1, rs2, rd_val, gsr;
409 maybe_flush_windows(
RS1(insn),
RS2(insn),
RD(insn), 0);
410 rs1 = fetch_reg(
RS1(insn), regs);
411 rs2 = fetch_reg(
RS2(insn), regs);
414 store_reg(regs, rd_val,
RD(insn));
417 gsr |= rd_val << 32
UL;
421 static void bshuffle(
struct pt_regs *regs,
unsigned int insn)
424 unsigned long rs1, rs2, rd_val;
429 rs1 = fpd_regval(f,
RS1(insn));
430 rs2 = fpd_regval(f,
RS2(insn));
433 for (i = 0; i < 8; i++) {
434 unsigned long which = (bmask >> (i * 4)) & 0xf;
438 byte = (rs1 >> (which * 8)) & 0xff;
440 byte = (rs2 >> ((which-8)*8)) & 0xff;
441 rd_val |= (byte << (i * 8));
444 *fpd_regaddr(f,
RD(insn)) = rd_val;
447 static void pdist(
struct pt_regs *regs,
unsigned int insn)
450 unsigned long rs1, rs2, *
rd, rd_val;
453 rs1 = fpd_regval(f,
RS1(insn));
454 rs2 = fpd_regval(f,
RS2(insn));
455 rd = fpd_regaddr(f,
RD(insn));
459 for (i = 0; i < 8; i++) {
462 s1 = (rs1 >> (56 - (i * 8))) & 0xff;
463 s2 = (rs2 >> (56 - (i * 8))) & 0xff;
476 static void pformat(
struct pt_regs *regs,
unsigned int insn,
unsigned int opf)
479 unsigned long rs1, rs2, gsr, scale, rd_val;
482 scale = (gsr >> 3) & (opf ==
FPACK16_OPF ? 0xf : 0x1f);
487 rs2 = fpd_regval(f,
RS2(insn));
489 for (byte = 0; byte < 4; byte++) {
492 int scaled = src << scale;
493 int from_fixed = scaled >> 7;
495 val = ((from_fixed < 0) ?
500 rd_val |= (val << (8 *
byte));
502 *fps_regaddr(f,
RD(insn)) = rd_val;
509 rs1 = fpd_regval(f,
RS1(insn));
510 rs2 = fpd_regval(f,
RS2(insn));
511 rd_val = (rs1 << 8) & ~(0x000000ff000000ffUL);
512 for (word = 0; word < 2; word++) {
514 s32 src = (rs2 >> (word * 32
UL));
515 s64 scaled = src << scale;
516 s64 from_fixed = scaled >> 23;
518 val = ((from_fixed < 0) ?
523 rd_val |= (val << (32 *
word));
525 *fpd_regaddr(f,
RD(insn)) = rd_val;
532 rs2 = fpd_regval(f,
RS2(insn));
535 for (word = 0; word < 2; word++) {
537 s32 src = (rs2 >> (word * 32
UL));
538 s64 scaled = src << scale;
539 s64 from_fixed = scaled >> 16;
541 val = ((from_fixed < -32768) ?
543 (from_fixed > 32767) ?
546 rd_val |= ((val & 0xffff) << (word * 16));
548 *fps_regaddr(f,
RD(insn)) = rd_val;
555 rs2 = fps_regval(f,
RS2(insn));
558 for (byte = 0; byte < 4; byte++) {
560 u8 src = (rs2 >> (byte * 8)) & 0xff;
564 rd_val |= (val << (byte * 16));
566 *fpd_regaddr(f,
RD(insn)) = rd_val;
571 rs1 = fps_regval(f,
RS1(insn));
572 rs2 = fps_regval(f,
RS2(insn));
574 rd_val = (((rs2 & 0x000000ff) << 0) |
575 ((rs1 & 0x000000ff) << 8) |
576 ((rs2 & 0x0000ff00) << 8) |
577 ((rs1 & 0x0000ff00) << 16) |
578 ((rs2 & 0x00ff0000) << 16) |
579 ((rs1 & 0x00ff0000) << 24) |
580 ((rs2 & 0xff000000) << 24) |
581 ((rs1 & 0xff000000) << 32));
582 *fpd_regaddr(f,
RD(insn)) = rd_val;
588 static void pmul(
struct pt_regs *regs,
unsigned int insn,
unsigned int opf)
591 unsigned long rs1, rs2, rd_val;
597 rs1 = fps_regval(f,
RS1(insn));
598 rs2 = fpd_regval(f,
RS2(insn));
601 for (byte = 0; byte < 4; byte++) {
602 u16 src1 = (rs1 >> (byte * 8)) & 0x00ff;
603 s16 src2 = (rs2 >> (byte * 16)) & 0xffff;
604 u32 prod = src1 * src2;
605 u16 scaled = ((prod & 0x00ffff00) >> 8);
610 rd_val |= ((scaled & 0xffff
UL) << (byte * 16
UL));
613 *fpd_regaddr(f,
RD(insn)) = rd_val;
622 rs1 = fps_regval(f,
RS1(insn));
623 rs2 = fps_regval(f,
RS2(insn));
627 for (byte = 0; byte < 4; byte++) {
628 u16 src1 = (rs1 >> (byte * 8)) & 0x00ff;
629 u32 prod = src1 * src2;
630 u16 scaled = ((prod & 0x00ffff00) >> 8);
635 rd_val |= ((scaled & 0xffff
UL) << (byte * 16
UL));
638 *fpd_regaddr(f,
RD(insn)) = rd_val;
644 unsigned long byte, ushift;
646 rs1 = fpd_regval(f,
RS1(insn));
647 rs2 = fpd_regval(f,
RS2(insn));
651 for (byte = 0; byte < 4; byte++) {
657 src1 = ((rs1 >> ((16 *
byte) + ushift)) & 0x00ff);
658 src2 = ((rs2 >> (16 *
byte)) & 0xffff);
660 scaled = ((prod & 0x00ffff00) >> 8);
665 rd_val |= ((scaled & 0xffff
UL) << (byte * 16
UL));
668 *fpd_regaddr(f,
RD(insn)) = rd_val;
674 unsigned long byte, ushift;
676 rs1 = fps_regval(f,
RS1(insn));
677 rs2 = fps_regval(f,
RS2(insn));
681 for (byte = 0; byte < 2; byte++) {
687 src1 = ((rs1 >> ((16 *
byte) + ushift)) & 0x00ff);
688 src2 = ((rs2 >> (16 *
byte)) & 0xffff);
690 scaled = ((prod & 0x00ffff00) >> 8);
695 rd_val |= ((scaled & 0xffff
UL) <<
696 ((byte * 32
UL) + 7
UL));
698 *fpd_regaddr(f,
RD(insn)) = rd_val;
704 static void pcmp(
struct pt_regs *regs,
unsigned int insn,
unsigned int opf)
707 unsigned long rs1, rs2, rd_val,
i;
709 rs1 = fpd_regval(f,
RS1(insn));
710 rs2 = fpd_regval(f,
RS2(insn));
716 for (i = 0; i < 4; i++) {
717 s16 a = (rs1 >> (i * 16)) & 0xffff;
718 s16 b = (rs2 >> (i * 16)) & 0xffff;
726 for (i = 0; i < 2; i++) {
727 s32 a = (rs1 >> (i * 32)) & 0xffffffff;
728 s32 b = (rs2 >> (i * 32)) & 0xffffffff;
736 for (i = 0; i < 4; i++) {
737 s16 a = (rs1 >> (i * 16)) & 0xffff;
738 s16 b = (rs2 >> (i * 16)) & 0xffff;
746 for (i = 0; i < 2; i++) {
747 s32 a = (rs1 >> (i * 32)) & 0xffffffff;
748 s32 b = (rs2 >> (i * 32)) & 0xffffffff;
756 for (i = 0; i < 4; i++) {
757 s16 a = (rs1 >> (i * 16)) & 0xffff;
758 s16 b = (rs2 >> (i * 16)) & 0xffff;
766 for (i = 0; i < 2; i++) {
767 s32 a = (rs1 >> (i * 32)) & 0xffffffff;
768 s32 b = (rs2 >> (i * 32)) & 0xffffffff;
776 for (i = 0; i < 4; i++) {
777 s16 a = (rs1 >> (i * 16)) & 0xffff;
778 s16 b = (rs2 >> (i * 16)) & 0xffff;
786 for (i = 0; i < 2; i++) {
787 s32 a = (rs1 >> (i * 32)) & 0xffffffff;
788 s32 b = (rs2 >> (i * 32)) & 0xffffffff;
796 maybe_flush_windows(0, 0,
RD(insn), 0);
797 store_reg(regs, rd_val,
RD(insn));
805 unsigned long pc = regs->tpc;
812 if (test_thread_flag(TIF_32BIT))
818 save_and_clear_fpu();
831 pformat(regs, insn, opf);
842 pmul(regs, insn, opf);
854 pcmp(regs, insn, opf);
870 edge(regs, insn, opf);
882 array(regs, insn, opf);
891 bshuffle(regs, insn);
895 regs->tpc = regs->tnpc;