1 | /* $Id: cidet-instr-1.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * CPU Instruction Decoding & Execution Tests - First bunch of instructions.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2014-2024 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 |
|
---|
39 | /*********************************************************************************************************************************
|
---|
40 | * Header Files *
|
---|
41 | *********************************************************************************************************************************/
|
---|
42 | #include "cidet.h"
|
---|
43 | #include <VBox/err.h>
|
---|
44 |
|
---|
45 |
|
---|
46 | /*********************************************************************************************************************************
|
---|
47 | * Defined Constants And Macros *
|
---|
48 | *********************************************************************************************************************************/
|
---|
49 | /*
|
---|
50 | * Shorter defines for the EFLAGS to save table space.
|
---|
51 | */
|
---|
52 | #undef CF
|
---|
53 | #undef PF
|
---|
54 | #undef AF
|
---|
55 | #undef ZF
|
---|
56 | #undef SF
|
---|
57 | #undef OF
|
---|
58 |
|
---|
59 | #define CF X86_EFL_CF
|
---|
60 | #define PF X86_EFL_PF
|
---|
61 | #define AF X86_EFL_AF
|
---|
62 | #define ZF X86_EFL_ZF
|
---|
63 | #define SF X86_EFL_SF
|
---|
64 | #define OF X86_EFL_OF
|
---|
65 |
|
---|
66 |
|
---|
67 | /*********************************************************************************************************************************
|
---|
68 | * Structures and Typedefs *
|
---|
69 | *********************************************************************************************************************************/
|
---|
70 | typedef struct CIDET2IN1OUTWITHFLAGSU8ENTRY
|
---|
71 | {
|
---|
72 | uint8_t uIn1;
|
---|
73 | uint8_t uIn2;
|
---|
74 | uint16_t fEFlagsIn;
|
---|
75 | uint8_t uOut;
|
---|
76 | uint16_t fEFlagsOut;
|
---|
77 | } CIDET2IN1OUTWITHFLAGSU8ENTRY;
|
---|
78 | typedef CIDET2IN1OUTWITHFLAGSU8ENTRY const *PCCIDET2IN1OUTWITHFLAGSU8ENTRY;
|
---|
79 |
|
---|
80 | typedef struct CIDET2IN1OUTWITHFLAGSU16ENTRY
|
---|
81 | {
|
---|
82 | uint16_t uIn1;
|
---|
83 | uint16_t uIn2;
|
---|
84 | uint16_t fEFlagsIn;
|
---|
85 | uint16_t uOut;
|
---|
86 | uint16_t fEFlagsOut;
|
---|
87 | } CIDET2IN1OUTWITHFLAGSU16ENTRY;
|
---|
88 | typedef CIDET2IN1OUTWITHFLAGSU16ENTRY const *PCCIDET2IN1OUTWITHFLAGSU16ENTRY;
|
---|
89 |
|
---|
90 | typedef struct CIDET2IN1OUTWITHFLAGSU32ENTRY
|
---|
91 | {
|
---|
92 | uint32_t uIn1;
|
---|
93 | uint32_t uIn2;
|
---|
94 | uint16_t fEFlagsIn;
|
---|
95 | uint32_t uOut;
|
---|
96 | uint16_t fEFlagsOut;
|
---|
97 | } CIDET2IN1OUTWITHFLAGSU32ENTRY;
|
---|
98 | typedef CIDET2IN1OUTWITHFLAGSU32ENTRY const *PCCIDET2IN1OUTWITHFLAGSU32ENTRY;
|
---|
99 |
|
---|
100 | typedef struct CIDET2IN1OUTWITHFLAGSU64ENTRY
|
---|
101 | {
|
---|
102 | uint64_t uIn1;
|
---|
103 | uint64_t uIn2;
|
---|
104 | uint16_t fEFlagsIn;
|
---|
105 | uint64_t uOut;
|
---|
106 | uint16_t fEFlagsOut;
|
---|
107 | } CIDET2IN1OUTWITHFLAGSU64ENTRY;
|
---|
108 | typedef CIDET2IN1OUTWITHFLAGSU64ENTRY const *PCCIDET2IN1OUTWITHFLAGSU64ENTRY;
|
---|
109 |
|
---|
110 | typedef struct CIDET2IN1OUTWITHFLAGS
|
---|
111 | {
|
---|
112 | PCCIDET2IN1OUTWITHFLAGSU8ENTRY pa8Entries;
|
---|
113 | PCCIDET2IN1OUTWITHFLAGSU16ENTRY pa16Entries;
|
---|
114 | PCCIDET2IN1OUTWITHFLAGSU32ENTRY pa32Entries;
|
---|
115 | PCCIDET2IN1OUTWITHFLAGSU64ENTRY pa64Entries;
|
---|
116 | uint16_t c8Entries;
|
---|
117 | uint16_t c16Entries;
|
---|
118 | uint16_t c32Entries;
|
---|
119 | uint16_t c64Entries;
|
---|
120 | uint32_t fRelevantEFlags;
|
---|
121 | } CIDET2IN1OUTWITHFLAGS;
|
---|
122 |
|
---|
123 | #define CIDET2IN1OUTWITHFLAGS_INITIALIZER(a_fRelevantEFlags) \
|
---|
124 | { \
|
---|
125 | &s_a8Results[0], &s_a16Results[0], &s_a32Results[0], &s_a64Results[0], \
|
---|
126 | RT_ELEMENTS(s_a8Results), RT_ELEMENTS(s_a16Results), RT_ELEMENTS(s_a32Results), RT_ELEMENTS(s_a64Results), \
|
---|
127 | (a_fRelevantEFlags) \
|
---|
128 | }
|
---|
129 |
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Generic worker for a FNCIDETSETUPINOUT function with two GPR/MEM registers,
|
---|
133 | * storing result in the first and flags.
|
---|
134 | *
|
---|
135 | * @returns See FNCIDETSETUPINOUT.
|
---|
136 | * @param pThis The core CIDET state structure. The InCtx
|
---|
137 | * and ExpectedCtx members will be modified.
|
---|
138 | * @param fInvalid When set, get the next invalid operands that will
|
---|
139 | * cause exceptions/faults.
|
---|
140 | * @param pResults The result collection.
|
---|
141 | */
|
---|
142 | static int CidetGenericIn2Out1WithFlags(PCIDETCORE pThis, bool fInvalid, CIDET2IN1OUTWITHFLAGS const *pResults)
|
---|
143 | {
|
---|
144 | int rc;
|
---|
145 |
|
---|
146 | Assert(pThis->idxMrmRegOp < 2);
|
---|
147 | Assert(pThis->idxMrmRmOp < 2);
|
---|
148 | Assert(pThis->idxMrmRmOp != pThis->idxMrmRegOp);
|
---|
149 | AssertCompile(RT_ELEMENTS(pThis->aiInOut) >= 4);
|
---|
150 |
|
---|
151 | if (!fInvalid)
|
---|
152 | {
|
---|
153 | if ( !pThis->fHasRegCollisionDirect
|
---|
154 | && !pThis->fHasRegCollisionMem)
|
---|
155 | {
|
---|
156 | pThis->InCtx.rfl &= ~(uint64_t)pResults->fRelevantEFlags;
|
---|
157 | pThis->ExpectedCtx.rfl &= ~(uint64_t)pResults->fRelevantEFlags;
|
---|
158 | switch (pThis->aOperands[0].cb)
|
---|
159 | {
|
---|
160 | case 1:
|
---|
161 | {
|
---|
162 | uint16_t idx = ++pThis->aiInOut[0] % pResults->c8Entries;
|
---|
163 | PCCIDET2IN1OUTWITHFLAGSU8ENTRY pEntry = &pResults->pa8Entries[idx];
|
---|
164 | rc = idx ? VINF_SUCCESS : VINF_EOF;
|
---|
165 |
|
---|
166 | *pThis->aOperands[0].In.pu8 = pEntry->uIn1;
|
---|
167 | *pThis->aOperands[1].In.pu8 = pEntry->uIn2;
|
---|
168 | pThis->InCtx.rfl |= pEntry->fEFlagsIn;
|
---|
169 |
|
---|
170 | *pThis->aOperands[0].Expected.pu8 = pEntry->uOut;
|
---|
171 | *pThis->aOperands[1].Expected.pu8 = pEntry->uIn2;
|
---|
172 | pThis->ExpectedCtx.rfl |= pEntry->fEFlagsOut;
|
---|
173 | break;
|
---|
174 | }
|
---|
175 |
|
---|
176 | case 2:
|
---|
177 | {
|
---|
178 | uint16_t idx = ++pThis->aiInOut[1] % pResults->c16Entries;
|
---|
179 | PCCIDET2IN1OUTWITHFLAGSU16ENTRY pEntry = &pResults->pa16Entries[idx];
|
---|
180 | rc = idx ? VINF_SUCCESS : VINF_EOF;
|
---|
181 |
|
---|
182 | *pThis->aOperands[0].In.pu16 = pEntry->uIn1;
|
---|
183 | *pThis->aOperands[1].In.pu16 = pEntry->uIn2;
|
---|
184 | pThis->InCtx.rfl |= pEntry->fEFlagsIn;
|
---|
185 |
|
---|
186 | *pThis->aOperands[0].Expected.pu16 = pEntry->uOut;
|
---|
187 | *pThis->aOperands[1].Expected.pu16 = pEntry->uIn2;
|
---|
188 | pThis->ExpectedCtx.rfl |= pEntry->fEFlagsOut;
|
---|
189 | break;
|
---|
190 | }
|
---|
191 |
|
---|
192 | case 4:
|
---|
193 | {
|
---|
194 | uint16_t idx = ++pThis->aiInOut[2] % pResults->c32Entries;
|
---|
195 | PCCIDET2IN1OUTWITHFLAGSU32ENTRY pEntry = &pResults->pa32Entries[idx];
|
---|
196 | rc = idx ? VINF_SUCCESS : VINF_EOF;
|
---|
197 |
|
---|
198 | *pThis->aOperands[0].In.pu32 = pEntry->uIn1;
|
---|
199 | *pThis->aOperands[1].In.pu32 = pEntry->uIn2;
|
---|
200 | pThis->InCtx.rfl |= pEntry->fEFlagsIn;
|
---|
201 |
|
---|
202 | *pThis->aOperands[0].Expected.pu32 = pEntry->uOut;
|
---|
203 | if (!pThis->aOperands[0].fIsMem)
|
---|
204 | pThis->aOperands[0].Expected.pu32[1] = 0;
|
---|
205 | *pThis->aOperands[1].Expected.pu32 = pEntry->uIn2;
|
---|
206 | pThis->ExpectedCtx.rfl |= pEntry->fEFlagsOut;
|
---|
207 | break;
|
---|
208 | }
|
---|
209 |
|
---|
210 | case 8:
|
---|
211 | {
|
---|
212 | uint16_t idx = ++pThis->aiInOut[3] % pResults->c64Entries;
|
---|
213 | PCCIDET2IN1OUTWITHFLAGSU64ENTRY pEntry = &pResults->pa64Entries[idx];
|
---|
214 | rc = idx ? VINF_SUCCESS : VINF_EOF;
|
---|
215 |
|
---|
216 | *pThis->aOperands[0].In.pu64 = pEntry->uIn1;
|
---|
217 | *pThis->aOperands[1].In.pu64 = pEntry->uIn2;
|
---|
218 | pThis->InCtx.rfl |= pEntry->fEFlagsIn;
|
---|
219 |
|
---|
220 | *pThis->aOperands[0].Expected.pu64 = pEntry->uOut;
|
---|
221 | *pThis->aOperands[1].Expected.pu64 = pEntry->uIn2;
|
---|
222 | pThis->ExpectedCtx.rfl |= pEntry->fEFlagsOut;
|
---|
223 | break;
|
---|
224 | }
|
---|
225 |
|
---|
226 | default:
|
---|
227 | AssertFailed();
|
---|
228 | rc = VERR_INTERNAL_ERROR_3;
|
---|
229 | }
|
---|
230 | }
|
---|
231 | else
|
---|
232 | rc = VERR_NOT_SUPPORTED;
|
---|
233 | }
|
---|
234 | else
|
---|
235 | rc = VERR_NO_DATA;
|
---|
236 | return rc;
|
---|
237 | }
|
---|
238 |
|
---|
239 |
|
---|
240 | static DECLCALLBACK(int) cidetInOutAdd(PCIDETCORE pThis, bool fInvalid)
|
---|
241 | {
|
---|
242 | static const CIDET2IN1OUTWITHFLAGSU8ENTRY s_a8Results[] =
|
---|
243 | {
|
---|
244 | { UINT8_C(0x00), UINT8_C(0x00), 0, UINT8_C(0x00), ZF | PF },
|
---|
245 | { UINT8_C(0xff), UINT8_C(0x01), 0, UINT8_C(0x00), CF | ZF | AF | PF },
|
---|
246 | { UINT8_C(0x7f), UINT8_C(0x80), 0, UINT8_C(0xff), SF | PF },
|
---|
247 | { UINT8_C(0x01), UINT8_C(0x01), 0, UINT8_C(0x02), 0 },
|
---|
248 | };
|
---|
249 | static const CIDET2IN1OUTWITHFLAGSU16ENTRY s_a16Results[] =
|
---|
250 | {
|
---|
251 | { UINT16_C(0x0000), UINT16_C(0x0000), 0, UINT16_C(0x0000), ZF | PF },
|
---|
252 | { UINT16_C(0xfefd), UINT16_C(0x0103), 0, UINT16_C(0x0000), CF | ZF | AF | PF },
|
---|
253 | { UINT16_C(0x8e7d), UINT16_C(0x7182), 0, UINT16_C(0xffff), SF | PF },
|
---|
254 | { UINT16_C(0x0001), UINT16_C(0x0001), 0, UINT16_C(0x0002), 0 },
|
---|
255 | };
|
---|
256 | static const CIDET2IN1OUTWITHFLAGSU32ENTRY s_a32Results[] =
|
---|
257 | {
|
---|
258 | { UINT32_C(0x00000000), UINT32_C(0x00000000), 0, UINT32_C(0x00000000), ZF | PF },
|
---|
259 | { UINT32_C(0xfefdfcfb), UINT32_C(0x01020305), 0, UINT32_C(0x00000000), CF | ZF | AF | PF },
|
---|
260 | { UINT32_C(0x8efdfcfb), UINT32_C(0x71020304), 0, UINT32_C(0xffffffff), SF | PF },
|
---|
261 | { UINT32_C(0x00000001), UINT32_C(0x00000001), 0, UINT32_C(0x00000002), 0 },
|
---|
262 | };
|
---|
263 | static const CIDET2IN1OUTWITHFLAGSU64ENTRY s_a64Results[] =
|
---|
264 | {
|
---|
265 | { UINT64_C(0x0000000000000000), UINT64_C(0x0000000000000000), 0, UINT64_C(0x0000000000000000), ZF | PF },
|
---|
266 | { UINT64_C(0xfefdfcfbfaf9f8f7), UINT64_C(0x0102030405060709), 0, UINT64_C(0x0000000000000000), CF | ZF | AF | PF },
|
---|
267 | { UINT64_C(0x7efdfcfbfaf9f8f7), UINT64_C(0x8102030405060708), 0, UINT64_C(0xffffffffffffffff), SF | PF },
|
---|
268 | { UINT64_C(0x0000000000000001), UINT64_C(0x0000000000000001), 0, UINT64_C(0x0000000000000002), 0 },
|
---|
269 | };
|
---|
270 | static const CIDET2IN1OUTWITHFLAGS s_Results = CIDET2IN1OUTWITHFLAGS_INITIALIZER(CF | PF | AF | SF | OF);
|
---|
271 | return CidetGenericIn2Out1WithFlags(pThis, fInvalid, &s_Results);
|
---|
272 | }
|
---|
273 |
|
---|
274 |
|
---|
275 | /** First bunch of instructions. */
|
---|
276 | const CIDETINSTR g_aCidetInstructions1[] =
|
---|
277 | {
|
---|
278 | #if 1
|
---|
279 | {
|
---|
280 | "add Eb,Gb", cidetInOutAdd, 1, {0x00, 0, 0}, 0, 2,
|
---|
281 | { CIDET_OF_K_GPR | CIDET_OF_Z_BYTE | CIDET_OF_M_RM | CIDET_OF_A_RW,
|
---|
282 | CIDET_OF_K_GPR | CIDET_OF_Z_BYTE | CIDET_OF_M_REG | CIDET_OF_A_R,
|
---|
283 | 0, 0 }, CIDET_IF_MODRM
|
---|
284 | },
|
---|
285 | #endif
|
---|
286 | #if 1
|
---|
287 | {
|
---|
288 | "add Ev,Gv", cidetInOutAdd, 1, {0x01, 0, 0}, 0, 2,
|
---|
289 | { CIDET_OF_K_GPR | CIDET_OF_Z_VAR_WDQ | CIDET_OF_M_RM | CIDET_OF_A_RW,
|
---|
290 | CIDET_OF_K_GPR | CIDET_OF_Z_VAR_WDQ | CIDET_OF_M_REG | CIDET_OF_A_R,
|
---|
291 | 0, 0 }, CIDET_IF_MODRM
|
---|
292 | },
|
---|
293 | #endif
|
---|
294 | };
|
---|
295 | /** Number of instruction in the g_aInstructions1 array. */
|
---|
296 | const uint32_t g_cCidetInstructions1 = RT_ELEMENTS(g_aCidetInstructions1);
|
---|
297 |
|
---|