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