]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - lib/libc/sparc64/sys/__sparc_utrap_emul.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / lib / libc / sparc64 / sys / __sparc_utrap_emul.c
1 /*-
2  * Copyright (c) 2001 by Thomas Moestl <tmm@FreeBSD.org>.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
18  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
23  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28
29 #include <sys/types.h>
30 #include <machine/cpufunc.h>
31 #include <machine/frame.h>
32 #include <machine/instr.h>
33
34 #include <signal.h>
35
36 #include "__sparc_utrap_private.h"
37 #include "fpu_reg.h"
38
39 int
40 __emul_insn(struct utrapframe *uf)
41 {
42         u_long reg, res;
43         u_long *addr;
44         u_int insn;
45         int sig;
46         int rd;
47         int i;
48
49         sig = 0;
50         insn = *(u_int *)uf->uf_pc;
51         flushw();
52         switch (IF_OP(insn)) {
53         case IOP_MISC:
54                 switch (IF_F3_OP3(insn)) {
55                 case INS2_POPC:
56                         if (IF_F3_RS1(insn) != 0) {
57                                 sig = SIGILL;
58                                 break;
59                         }
60                         reg = __emul_f3_op2(uf, insn);
61                         for (i = 0; i < 64; i++)
62                                 res += (reg >> i) & 1;
63                         __emul_store_reg(uf, IF_F3_RD(insn), res);
64                         break;
65                 default:
66                         sig = SIGILL;
67                         break;
68                 }
69                 break;
70         case IOP_LDST:
71                 switch (IF_F3_OP3(insn)) {
72                 case INS3_LDQF:
73                         rd = INSFPdq_RN(IF_F3_RD(insn));
74                         addr = (u_long *)__emul_f3_memop_addr(uf, insn);
75                         __fpu_setreg64(rd, addr[0]);
76                         __fpu_setreg64(rd + 2, addr[1]);
77                         break;
78                 case INS3_STQF:
79                         rd = INSFPdq_RN(IF_F3_RD(insn));
80                         addr = (u_long *)__emul_f3_memop_addr(uf, insn);
81                         addr[0] = __fpu_getreg64(rd);
82                         addr[1] = __fpu_getreg64(rd + 2);
83                         break;
84                 default:
85                         sig = SIGILL;
86                         break;
87                 }
88                 break;
89         default:
90                 sig = SIGILL;
91                 break;
92         }
93         return (sig);
94 }
95
96 u_long
97 __emul_fetch_reg(struct utrapframe *uf, int reg)
98 {
99         struct frame *frm;
100
101         if (reg == IREG_G0)
102                 return (0);
103         else if (reg < IREG_O0) /* global */
104                 return (uf->uf_global[reg]);
105         else if (reg < IREG_L0) /* out */
106                 return (uf->uf_out[reg - IREG_O0]);
107         else {                  /* local, in */
108                 /*
109                  * The in registers are immediately after the locals in
110                  * the frame.
111                  */
112                 frm = (struct frame *)(uf->uf_out[6] + SPOFF);
113                 return (frm->fr_local[reg - IREG_L0]);
114         }
115 }
116
117 void
118 __emul_store_reg(struct utrapframe *uf, int reg, u_long val)
119 {
120         struct frame *frm;
121
122         if (reg == IREG_G0)
123                 return;
124         if (reg < IREG_O0)      /* global */
125                 uf->uf_global[reg] = val;
126         else if (reg < IREG_L0) /* out */
127                 uf->uf_out[reg - IREG_O0] = val;
128         else {
129                 /*
130                  * The in registers are immediately after the locals in
131                  * the frame.
132                  */
133                 frm = (struct frame *)(uf->uf_out[6] + SPOFF);
134                 frm->fr_local[reg - IREG_L0] = val;
135         }
136 }
137
138 u_long
139 __emul_f3_op2(struct utrapframe *uf, u_int insn)
140 {
141
142         if (IF_F3_I(insn) != 0)
143                 return (IF_SIMM(insn, 13));
144         else
145                 return (__emul_fetch_reg(uf, IF_F3_RS2(insn)));
146 }
147
148 u_long
149 __emul_f3_memop_addr(struct utrapframe *uf, u_int insn)
150 {
151         u_long addr;
152
153         addr = __emul_f3_op2(uf, insn) + __emul_fetch_reg(uf, IF_F3_RS1(insn));
154         return (addr);
155 }