VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/bootsectors/bs3-cpu-generated-1-template.c@ 66104

Last change on this file since 66104 was 66102, checked in by vboxsync, 8 years ago

bs3-cpu-generated-1: updates

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 44.6 KB
Line 
1/* $Id: bs3-cpu-generated-1-template.c 66102 2017-03-15 09:23:01Z vboxsync $ */
2/** @file
3 * BS3Kit - bs3-cpu-generated-1, C code template.
4 */
5
6/*
7 * Copyright (C) 2007-2017 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27#ifndef BS3_INSTANTIATING_CMN
28# error "BS3_INSTANTIATING_CMN not defined"
29#endif
30
31
32/*********************************************************************************************************************************
33* Header Files *
34*********************************************************************************************************************************/
35#include <iprt/asm.h>
36#include <iprt/asm-amd64-x86.h>
37
38#include "bs3-cpu-generated-1.h"
39
40
41/*********************************************************************************************************************************
42* Defined Constants And Macros *
43*********************************************************************************************************************************/
44#define P_CS X86_OP_PRF_CS
45#define P_SS X86_OP_PRF_SS
46#define P_DS X86_OP_PRF_DS
47#define P_ES X86_OP_PRF_ES
48#define P_FS X86_OP_PRF_FS
49#define P_GS X86_OP_PRF_GS
50#define P_OZ X86_OP_PRF_SIZE_OP
51#define P_AZ X86_OP_PRF_SIZE_ADDR
52#define P_LK X86_OP_PRF_LOCK
53#define P_RN X86_OP_PRF_REPNZ
54#define P_RZ X86_OP_PRF_REPZ
55
56#define REX_WRBX (X86_OP_REX_W | X86_OP_REX_R | X86_OP_REX_B | X86_OP_REX_X)
57#define REX_W___ (X86_OP_REX_W)
58#define REX_WR__ (X86_OP_REX_W | X86_OP_REX_R)
59#define REX_W_B_ (X86_OP_REX_W | X86_OP_REX_B)
60#define REX_W__X (X86_OP_REX_W | X86_OP_REX_X)
61#define REX_WRB_ (X86_OP_REX_W | X86_OP_REX_R | X86_OP_REX_B)
62#define REX_WR_X (X86_OP_REX_W | X86_OP_REX_R | X86_OP_REX_X)
63#define REX_W_BX (X86_OP_REX_W | X86_OP_REX_B | X86_OP_REX_X)
64#define REX__R__ (X86_OP_REX_R)
65#define REX__RB_ (X86_OP_REX_R | X86_OP_REX_B)
66#define REX__R_X (X86_OP_REX_R | X86_OP_REX_X)
67#define REX__RBX (X86_OP_REX_R | X86_OP_REX_B | X86_OP_REX_X)
68#define REX___B_ (X86_OP_REX_B)
69#define REX___BX (X86_OP_REX_B | X86_OP_REX_X)
70#define REX____X (X86_OP_REX_X)
71#define REX_____ (0x40)
72
73
74/** @def BS3CG1_DPRINTF
75 * Debug print macro.
76 */
77#if 1
78# define BS3CG1_DPRINTF(a_ArgList) Bs3TestPrintf a_ArgList
79#else
80# define BS3CG1_DPRINTF(a_ArgList) do { } while (0)
81#endif
82
83
84/*********************************************************************************************************************************
85* Structures and Typedefs *
86*********************************************************************************************************************************/
87/**
88 * The state.
89 */
90typedef struct BS3CG1STATE
91{
92 /** @name Instruction details (expanded from BS3CG1INSTR).
93 * @{ */
94 /** Pointer to the mnemonic string (not terminated) (g_achBs3Cg1Mnemonics). */
95 const char BS3_FAR *pchMnemonic;
96 /** Pointer to the test header. */
97 PCBS3CG1TESTHDR pTestHdr;
98 /** Pointer to the per operand flags (g_abBs3Cg1Operands). */
99 const uint8_t BS3_FAR *pabOperands;
100 /** Opcode bytes (g_abBs3Cg1Opcodes). */
101 const uint8_t BS3_FAR *pabOpcodes;
102 /** The current instruction number in the input array (for error reporting). */
103 uint32_t iInstr;
104
105 /** The instruction flags. */
106 uint32_t fFlags;
107 /** The encoding. */
108 BS3CG1ENC enmEncoding;
109#if ARCH_BITS == 16
110 uint16_t u16Padding0;
111#endif
112 /** Per operand flags. */
113 BS3CG1OP aenmOperands[4];
114 /** Opcode bytes. */
115 uint8_t abOpcodes[4];
116
117 /** The length of the mnemonic. */
118 uint8_t cchMnemonic;
119 /** Whether to advance the mnemonic pointer or not. */
120 uint8_t fAdvanceMnemonic;
121 /** The number of opcode bytes. */
122 uint8_t cbOpcodes;
123 /** Number of operands. */
124 uint8_t cOperands;
125 /** @} */
126
127 /** Operand size (16, 32, 64, or 0). */
128 uint8_t cBitsOp;
129 /** Target ring (0..3). */
130 uint8_t uCpl;
131
132 /** The current test number. */
133 uint8_t iTest;
134
135 /** Target mode (g_bBs3CurrentMode). */
136 uint8_t bMode;
137
138
139 /** @name Current encoded instruction.
140 * @{ */
141 /** The size of the current instruction that we're testing. */
142 uint8_t cbCurInstr;
143 /** The size the prefixes. */
144 uint8_t cbCurPrefix;
145 /** The offset into abCurInstr of the immediate. */
146 uint8_t offCurImm;
147 /** Buffer for assembling the current instruction. */
148 uint8_t abCurInstr[27];
149
150 /** Set if the encoding can't be tested in the same ring as this test code.
151 * This is used to deal with encodings modifying SP/ESP/RSP. */
152 bool fSameRingNotOkay;
153 /** The aOperands index of the modrm.reg operand (if applicable). */
154 uint8_t iRegOp;
155 /** The aOperands index of the modrm.rm operand (if applicable). */
156 uint8_t iRmOp;
157
158 /** Operands details. */
159 struct
160 {
161 uint8_t cbOp;
162 bool fMem;
163 /** The BS3CG1DST value for this field.
164 * Set to BS3CG1DST_INVALID if memory. */
165 uint8_t idxField;
166 } aOperands[4];
167 /** @} */
168
169 /** Page to put code in. When paging is enabled, the page before and after
170 * are marked not-present. */
171 uint8_t BS3_FAR *pbCodePg;
172 /** Page for placing data operands in. When paging is enabled, the page before
173 * and after are marked not-present. */
174 uint8_t BS3_FAR *pbDataPg;
175
176 /** The context we're working on. */
177 BS3REGCTX Ctx;
178 /** The trap context and frame. */
179 BS3TRAPFRAME TrapFrame;
180 /** Initial contexts, one for each ring. */
181 BS3REGCTX aInitialCtxs[4];
182
183 /** Memory operand scratch space. */
184 union
185 {
186 uint8_t ab[128];
187 uint16_t au16[128 / sizeof(uint16_t)];
188 uint32_t au32[128 / sizeof(uint32_t)];
189 uint64_t au64[128 / sizeof(uint64_t)];
190 } MemOp;
191} BS3CG1STATE;
192/** Pointer to the generated test state. */
193typedef BS3CG1STATE *PBS3CG1STATE;
194
195
196#define BS3CG1_PF_OZ UINT16_C(0x0001)
197#define BS3CG1_PF_AZ UINT16_C(0x0002)
198#define BS3CG1_PF_CS UINT16_C(0x0004)
199#define BS3CG1_PF_DS UINT16_C(0x0008)
200#define BS3CG1_PF_ES UINT16_C(0x0010)
201#define BS3CG1_PF_FS UINT16_C(0x0020)
202#define BS3CG1_PF_GS UINT16_C(0x0040)
203#define BS3CG1_PF_SS UINT16_C(0x0080)
204#define BS3CG1_PF_SEGS (BS3CG1_PF_CS | BS3CG1_PF_DS | BS3CG1_PF_ES | BS3CG1_PF_FS | BS3CG1_PF_GS | BS3CG1_PF_SS)
205#define BS3CG1_PF_MEM (BS3CG1_PF_SEGS | BS3CG1_PF_AZ)
206#define BS3CG1_PF_LK UINT16_C(0x0100)
207#define BS3CG1_PF_RN UINT16_C(0x0200)
208#define BS3CG1_PF_RZ UINT16_C(0x0400)
209#define BS3CG1_PF_W UINT16_C(0x0800) /**< REX.W */
210#define BS3CG1_PF_R UINT16_C(0x1000) /**< REX.R */
211#define BS3CG1_PF_B UINT16_C(0x2000) /**< REX.B */
212#define BS3CG1_PF_X UINT16_C(0x4000) /**< REX.X */
213
214
215/** Used in g_cbBs3Cg1DstFields to indicate that it's one of the 4 operands. */
216#define BS3CG1DSTSIZE_OPERAND UINT8_C(255)
217/** Used in g_cbBs3Cg1DstFields to indicate that the operand size determins
218 * the field size (2, 4, or 8). */
219#define BS3CG1DSTSIZE_OPERAND_SIZE_GRP UINT8_C(254)
220
221
222
223/*********************************************************************************************************************************
224* Global Variables *
225*********************************************************************************************************************************/
226/** Destination field sizes indexed by bBS3CG1DST.
227 * Zero means operand size sized. */
228static const uint8_t g_cbBs3Cg1DstFields[] =
229{
230 /* [BS3CG1DST_INVALID] = */ BS3CG1DSTSIZE_OPERAND,
231
232 /* [BS3CG1DST_OP1] = */ BS3CG1DSTSIZE_OPERAND,
233 /* [BS3CG1DST_OP2] = */ BS3CG1DSTSIZE_OPERAND,
234 /* [BS3CG1DST_OP3] = */ BS3CG1DSTSIZE_OPERAND,
235 /* [BS3CG1DST_OP4] = */ BS3CG1DSTSIZE_OPERAND,
236 /* [BS3CG1DST_EFL] = */ 4,
237 /* [BS3CG1DST_EFL_UNDEF]=*/ 4,
238
239 /* [BS3CG1DST_AL] = */ 1,
240 /* [BS3CG1DST_CL] = */ 1,
241 /* [BS3CG1DST_DL] = */ 1,
242 /* [BS3CG1DST_BL] = */ 1,
243 /* [BS3CG1DST_AH] = */ 1,
244 /* [BS3CG1DST_CH] = */ 1,
245 /* [BS3CG1DST_DH] = */ 1,
246 /* [BS3CG1DST_BH] = */ 1,
247 /* [BS3CG1DST_SPL] = */ 1,
248 /* [BS3CG1DST_BPL] = */ 1,
249 /* [BS3CG1DST_SIL] = */ 1,
250 /* [BS3CG1DST_DIL] = */ 1,
251 /* [BS3CG1DST_R8L] = */ 1,
252 /* [BS3CG1DST_R9L] = */ 1,
253 /* [BS3CG1DST_R10L] = */ 1,
254 /* [BS3CG1DST_R11L] = */ 1,
255 /* [BS3CG1DST_R12L] = */ 1,
256 /* [BS3CG1DST_R13L] = */ 1,
257 /* [BS3CG1DST_R14L] = */ 1,
258 /* [BS3CG1DST_R15L] = */ 1,
259
260 /* [BS3CG1DST_AX] = */ 2,
261 /* [BS3CG1DST_CX] = */ 2,
262 /* [BS3CG1DST_DX] = */ 2,
263 /* [BS3CG1DST_BX] = */ 2,
264 /* [BS3CG1DST_SP] = */ 2,
265 /* [BS3CG1DST_BP] = */ 2,
266 /* [BS3CG1DST_SI] = */ 2,
267 /* [BS3CG1DST_DI] = */ 2,
268 /* [BS3CG1DST_R8W] = */ 2,
269 /* [BS3CG1DST_R9W] = */ 2,
270 /* [BS3CG1DST_R10W] = */ 2,
271 /* [BS3CG1DST_R11W] = */ 2,
272 /* [BS3CG1DST_R12W] = */ 2,
273 /* [BS3CG1DST_R13W] = */ 2,
274 /* [BS3CG1DST_R14W] = */ 2,
275 /* [BS3CG1DST_R15W] = */ 2,
276
277 /* [BS3CG1DST_EAX] = */ 4,
278 /* [BS3CG1DST_ECX] = */ 4,
279 /* [BS3CG1DST_EDX] = */ 4,
280 /* [BS3CG1DST_EBX] = */ 4,
281 /* [BS3CG1DST_ESP] = */ 4,
282 /* [BS3CG1DST_EBP] = */ 4,
283 /* [BS3CG1DST_ESI] = */ 4,
284 /* [BS3CG1DST_EDI] = */ 4,
285 /* [BS3CG1DST_R8D] = */ 4,
286 /* [BS3CG1DST_R9D] = */ 4,
287 /* [BS3CG1DST_R10D] = */ 4,
288 /* [BS3CG1DST_R11D] = */ 4,
289 /* [BS3CG1DST_R12D] = */ 4,
290 /* [BS3CG1DST_R13D] = */ 4,
291 /* [BS3CG1DST_R14D] = */ 4,
292 /* [BS3CG1DST_R15D] = */ 4,
293
294 /* [BS3CG1DST_RAX] = */ 8,
295 /* [BS3CG1DST_RCX] = */ 8,
296 /* [BS3CG1DST_RDX] = */ 8,
297 /* [BS3CG1DST_RBX] = */ 8,
298 /* [BS3CG1DST_RSP] = */ 8,
299 /* [BS3CG1DST_RBP] = */ 8,
300 /* [BS3CG1DST_RSI] = */ 8,
301 /* [BS3CG1DST_RDI] = */ 8,
302 /* [BS3CG1DST_R8] = */ 8,
303 /* [BS3CG1DST_R9] = */ 8,
304 /* [BS3CG1DST_R10] = */ 8,
305 /* [BS3CG1DST_R11] = */ 8,
306 /* [BS3CG1DST_R12] = */ 8,
307 /* [BS3CG1DST_R13] = */ 8,
308 /* [BS3CG1DST_R14] = */ 8,
309 /* [BS3CG1DST_R15] = */ 8,
310
311 /* [BS3CG1DST_OZ_RAX] = */ BS3CG1DSTSIZE_OPERAND_SIZE_GRP,
312 /* [BS3CG1DST_OZ_RCX] = */ BS3CG1DSTSIZE_OPERAND_SIZE_GRP,
313 /* [BS3CG1DST_OZ_RDX] = */ BS3CG1DSTSIZE_OPERAND_SIZE_GRP,
314 /* [BS3CG1DST_OZ_RBX] = */ BS3CG1DSTSIZE_OPERAND_SIZE_GRP,
315 /* [BS3CG1DST_OZ_RSP] = */ BS3CG1DSTSIZE_OPERAND_SIZE_GRP,
316 /* [BS3CG1DST_OZ_RBP] = */ BS3CG1DSTSIZE_OPERAND_SIZE_GRP,
317 /* [BS3CG1DST_OZ_RSI] = */ BS3CG1DSTSIZE_OPERAND_SIZE_GRP,
318 /* [BS3CG1DST_OZ_RDI] = */ BS3CG1DSTSIZE_OPERAND_SIZE_GRP,
319 /* [BS3CG1DST_OZ_R8] = */ BS3CG1DSTSIZE_OPERAND_SIZE_GRP,
320 /* [BS3CG1DST_OZ_R9] = */ BS3CG1DSTSIZE_OPERAND_SIZE_GRP,
321 /* [BS3CG1DST_OZ_R10] = */ BS3CG1DSTSIZE_OPERAND_SIZE_GRP,
322 /* [BS3CG1DST_OZ_R11] = */ BS3CG1DSTSIZE_OPERAND_SIZE_GRP,
323 /* [BS3CG1DST_OZ_R12] = */ BS3CG1DSTSIZE_OPERAND_SIZE_GRP,
324 /* [BS3CG1DST_OZ_R13] = */ BS3CG1DSTSIZE_OPERAND_SIZE_GRP,
325 /* [BS3CG1DST_OZ_R14] = */ BS3CG1DSTSIZE_OPERAND_SIZE_GRP,
326 /* [BS3CG1DST_OZ_R15] = */ BS3CG1DSTSIZE_OPERAND_SIZE_GRP,
327};
328
329/** Destination field offset indexed by bBS3CG1DST.
330 * Zero means operand size sized. */
331static const unsigned g_offBs3Cg1DstFields[] =
332{
333 /* [BS3CG1DST_INVALID] = */ ~0U,
334 /* [BS3CG1DST_OP1] = */ ~0U,
335 /* [BS3CG1DST_OP2] = */ ~0U,
336 /* [BS3CG1DST_OP3] = */ ~0U,
337 /* [BS3CG1DST_OP4] = */ ~0U,
338 /* [BS3CG1DST_EFL] = */ RT_OFFSETOF(BS3REGCTX, rflags),
339 /* [BS3CG1DST_EFL_UNDEF]=*/ ~0, /* special field */
340
341 /* [BS3CG1DST_AL] = */ RT_OFFSETOF(BS3REGCTX, rax.u8),
342 /* [BS3CG1DST_CL] = */ RT_OFFSETOF(BS3REGCTX, rcx.u8),
343 /* [BS3CG1DST_DL] = */ RT_OFFSETOF(BS3REGCTX, rdx.u8),
344 /* [BS3CG1DST_BL] = */ RT_OFFSETOF(BS3REGCTX, rbx.u8),
345 /* [BS3CG1DST_AH] = */ RT_OFFSETOF(BS3REGCTX, rax.b.bHi),
346 /* [BS3CG1DST_CH] = */ RT_OFFSETOF(BS3REGCTX, rcx.b.bHi),
347 /* [BS3CG1DST_DH] = */ RT_OFFSETOF(BS3REGCTX, rdx.b.bHi),
348 /* [BS3CG1DST_BH] = */ RT_OFFSETOF(BS3REGCTX, rbx.b.bHi),
349 /* [BS3CG1DST_SPL] = */ RT_OFFSETOF(BS3REGCTX, rsp.u8),
350 /* [BS3CG1DST_BPL] = */ RT_OFFSETOF(BS3REGCTX, rbp.u8),
351 /* [BS3CG1DST_SIL] = */ RT_OFFSETOF(BS3REGCTX, rsi.u8),
352 /* [BS3CG1DST_DIL] = */ RT_OFFSETOF(BS3REGCTX, rdi.u8),
353 /* [BS3CG1DST_R8L] = */ RT_OFFSETOF(BS3REGCTX, r8.u8),
354 /* [BS3CG1DST_R9L] = */ RT_OFFSETOF(BS3REGCTX, r9.u8),
355 /* [BS3CG1DST_R10L] = */ RT_OFFSETOF(BS3REGCTX, r10.u8),
356 /* [BS3CG1DST_R11L] = */ RT_OFFSETOF(BS3REGCTX, r11.u8),
357 /* [BS3CG1DST_R12L] = */ RT_OFFSETOF(BS3REGCTX, r12.u8),
358 /* [BS3CG1DST_R13L] = */ RT_OFFSETOF(BS3REGCTX, r13.u8),
359 /* [BS3CG1DST_R14L] = */ RT_OFFSETOF(BS3REGCTX, r14.u8),
360 /* [BS3CG1DST_R15L] = */ RT_OFFSETOF(BS3REGCTX, r15.u8),
361
362 /* [BS3CG1DST_AX] = */ RT_OFFSETOF(BS3REGCTX, rax.u16),
363 /* [BS3CG1DST_CX] = */ RT_OFFSETOF(BS3REGCTX, rcx.u16),
364 /* [BS3CG1DST_DX] = */ RT_OFFSETOF(BS3REGCTX, rdx.u16),
365 /* [BS3CG1DST_BX] = */ RT_OFFSETOF(BS3REGCTX, rbx.u16),
366 /* [BS3CG1DST_SP] = */ RT_OFFSETOF(BS3REGCTX, rsp.u16),
367 /* [BS3CG1DST_BP] = */ RT_OFFSETOF(BS3REGCTX, rbp.u16),
368 /* [BS3CG1DST_SI] = */ RT_OFFSETOF(BS3REGCTX, rsi.u16),
369 /* [BS3CG1DST_DI] = */ RT_OFFSETOF(BS3REGCTX, rdi.u16),
370 /* [BS3CG1DST_R8W] = */ RT_OFFSETOF(BS3REGCTX, r8.u16),
371 /* [BS3CG1DST_R9W] = */ RT_OFFSETOF(BS3REGCTX, r9.u16),
372 /* [BS3CG1DST_R10W] = */ RT_OFFSETOF(BS3REGCTX, r10.u16),
373 /* [BS3CG1DST_R11W] = */ RT_OFFSETOF(BS3REGCTX, r11.u16),
374 /* [BS3CG1DST_R12W] = */ RT_OFFSETOF(BS3REGCTX, r12.u16),
375 /* [BS3CG1DST_R13W] = */ RT_OFFSETOF(BS3REGCTX, r13.u16),
376 /* [BS3CG1DST_R14W] = */ RT_OFFSETOF(BS3REGCTX, r14.u16),
377 /* [BS3CG1DST_R15W] = */ RT_OFFSETOF(BS3REGCTX, r15.u16),
378
379 /* [BS3CG1DST_EAX] = */ RT_OFFSETOF(BS3REGCTX, rax.u32),
380 /* [BS3CG1DST_ECX] = */ RT_OFFSETOF(BS3REGCTX, rcx.u32),
381 /* [BS3CG1DST_EDX] = */ RT_OFFSETOF(BS3REGCTX, rdx.u32),
382 /* [BS3CG1DST_EBX] = */ RT_OFFSETOF(BS3REGCTX, rbx.u32),
383 /* [BS3CG1DST_ESP] = */ RT_OFFSETOF(BS3REGCTX, rsp.u32),
384 /* [BS3CG1DST_EBP] = */ RT_OFFSETOF(BS3REGCTX, rbp.u32),
385 /* [BS3CG1DST_ESI] = */ RT_OFFSETOF(BS3REGCTX, rsi.u32),
386 /* [BS3CG1DST_EDI] = */ RT_OFFSETOF(BS3REGCTX, rdi.u32),
387 /* [BS3CG1DST_R8D] = */ RT_OFFSETOF(BS3REGCTX, r8.u32),
388 /* [BS3CG1DST_R9D] = */ RT_OFFSETOF(BS3REGCTX, r9.u32),
389 /* [BS3CG1DST_R10D] = */ RT_OFFSETOF(BS3REGCTX, r10.u32),
390 /* [BS3CG1DST_R11D] = */ RT_OFFSETOF(BS3REGCTX, r11.u32),
391 /* [BS3CG1DST_R12D] = */ RT_OFFSETOF(BS3REGCTX, r12.u32),
392 /* [BS3CG1DST_R13D] = */ RT_OFFSETOF(BS3REGCTX, r13.u32),
393 /* [BS3CG1DST_R14D] = */ RT_OFFSETOF(BS3REGCTX, r14.u32),
394 /* [BS3CG1DST_R15D] = */ RT_OFFSETOF(BS3REGCTX, r15.u32),
395
396 /* [BS3CG1DST_RAX] = */ RT_OFFSETOF(BS3REGCTX, rax.u64),
397 /* [BS3CG1DST_RCX] = */ RT_OFFSETOF(BS3REGCTX, rcx.u64),
398 /* [BS3CG1DST_RDX] = */ RT_OFFSETOF(BS3REGCTX, rdx.u64),
399 /* [BS3CG1DST_RBX] = */ RT_OFFSETOF(BS3REGCTX, rbx.u64),
400 /* [BS3CG1DST_RSP] = */ RT_OFFSETOF(BS3REGCTX, rsp.u64),
401 /* [BS3CG1DST_RBP] = */ RT_OFFSETOF(BS3REGCTX, rbp.u64),
402 /* [BS3CG1DST_RSI] = */ RT_OFFSETOF(BS3REGCTX, rsi.u64),
403 /* [BS3CG1DST_RDI] = */ RT_OFFSETOF(BS3REGCTX, rdi.u64),
404 /* [BS3CG1DST_R8] = */ RT_OFFSETOF(BS3REGCTX, r8.u64),
405 /* [BS3CG1DST_R9] = */ RT_OFFSETOF(BS3REGCTX, r9.u64),
406 /* [BS3CG1DST_R10] = */ RT_OFFSETOF(BS3REGCTX, r10.u64),
407 /* [BS3CG1DST_R11] = */ RT_OFFSETOF(BS3REGCTX, r11.u64),
408 /* [BS3CG1DST_R12] = */ RT_OFFSETOF(BS3REGCTX, r12.u64),
409 /* [BS3CG1DST_R13] = */ RT_OFFSETOF(BS3REGCTX, r13.u64),
410 /* [BS3CG1DST_R14] = */ RT_OFFSETOF(BS3REGCTX, r14.u64),
411 /* [BS3CG1DST_R15] = */ RT_OFFSETOF(BS3REGCTX, r15.u64),
412
413 /* [BS3CG1DST_OZ_RAX] = */ RT_OFFSETOF(BS3REGCTX, rax),
414 /* [BS3CG1DST_OZ_RCX] = */ RT_OFFSETOF(BS3REGCTX, rcx),
415 /* [BS3CG1DST_OZ_RDX] = */ RT_OFFSETOF(BS3REGCTX, rdx),
416 /* [BS3CG1DST_OZ_RBX] = */ RT_OFFSETOF(BS3REGCTX, rbx),
417 /* [BS3CG1DST_OZ_RSP] = */ RT_OFFSETOF(BS3REGCTX, rsp),
418 /* [BS3CG1DST_OZ_RBP] = */ RT_OFFSETOF(BS3REGCTX, rbp),
419 /* [BS3CG1DST_OZ_RSI] = */ RT_OFFSETOF(BS3REGCTX, rsi),
420 /* [BS3CG1DST_OZ_RDI] = */ RT_OFFSETOF(BS3REGCTX, rdi),
421 /* [BS3CG1DST_OZ_R8] = */ RT_OFFSETOF(BS3REGCTX, r8),
422 /* [BS3CG1DST_OZ_R9] = */ RT_OFFSETOF(BS3REGCTX, r9),
423 /* [BS3CG1DST_OZ_R10] = */ RT_OFFSETOF(BS3REGCTX, r10),
424 /* [BS3CG1DST_OZ_R11] = */ RT_OFFSETOF(BS3REGCTX, r11),
425 /* [BS3CG1DST_OZ_R12] = */ RT_OFFSETOF(BS3REGCTX, r12),
426 /* [BS3CG1DST_OZ_R13] = */ RT_OFFSETOF(BS3REGCTX, r13),
427 /* [BS3CG1DST_OZ_R14] = */ RT_OFFSETOF(BS3REGCTX, r14),
428 /* [BS3CG1DST_OZ_R15] = */ RT_OFFSETOF(BS3REGCTX, r15),
429};
430
431#if 0
432static const struct
433{
434 uint8_t cbPrefixes;
435 uint8_t abPrefixes[14];
436 uint16_t fEffective;
437} g_aPrefixVariations[] =
438{
439 { 0, { 0x00 }, BS3CG1_PF_NONE },
440
441 { 1, { P_OZ }, BS3CG1_PF_OZ },
442 { 1, { P_CS }, BS3CG1_PF_CS },
443 { 1, { P_DS }, BS3CG1_PF_DS },
444 { 1, { P_ES }, BS3CG1_PF_ES },
445 { 1, { P_FS }, BS3CG1_PF_FS },
446 { 1, { P_GS }, BS3CG1_PF_GS },
447 { 1, { P_SS }, BS3CG1_PF_SS },
448 { 1, { P_LK }, BS3CG1_PF_LK },
449
450 { 2, { P_CS, P_OZ, }, BS3CG1_PF_CS | BS3CFG1_PF_OZ },
451 { 2, { P_DS, P_OZ, }, BS3CG1_PF_DS | BS3CFG1_PF_OZ },
452 { 2, { P_ES, P_OZ, }, BS3CG1_PF_ES | BS3CFG1_PF_OZ },
453 { 2, { P_FS, P_OZ, }, BS3CG1_PF_FS | BS3CFG1_PF_OZ },
454 { 2, { P_GS, P_OZ, }, BS3CG1_PF_GS | BS3CFG1_PF_OZ },
455 { 2, { P_GS, P_OZ, }, BS3CG1_PF_SS | BS3CFG1_PF_OZ },
456 { 2, { P_SS, P_OZ, }, BS3CG1_PF_SS | BS3CFG1_PF_OZ },
457
458 { 2, { P_OZ, P_CS, }, BS3CG1_PF_CS | BS3CFG1_PF_OZ },
459 { 2, { P_OZ, P_DS, }, BS3CG1_PF_DS | BS3CFG1_PF_OZ },
460 { 2, { P_OZ, P_ES, }, BS3CG1_PF_ES | BS3CFG1_PF_OZ },
461 { 2, { P_OZ, P_FS, }, BS3CG1_PF_FS | BS3CFG1_PF_OZ },
462 { 2, { P_OZ, P_GS, }, BS3CG1_PF_GS | BS3CFG1_PF_OZ },
463 { 2, { P_OZ, P_GS, }, BS3CG1_PF_SS | BS3CFG1_PF_OZ },
464 { 2, { P_OZ, P_SS, }, BS3CG1_PF_SS | BS3CFG1_PF_OZ },
465};
466
467static const uint16_t g_afPfxKindToIgnoredFlags[BS3CGPFXKIND_END] =
468{
469 /* [BS3CGPFXKIND_INVALID] = */ UINT16_MAX,
470 /* [BS3CGPFXKIND_MODRM] = */ 0,
471 /* [BS3CGPFXKIND_MODRM_NO_OP_SIZES] = */ BS3CG1_PF_OZ | BS3CG1_PF_W,
472};
473
474#endif
475
476
477DECLINLINE(unsigned) Bs3Cg1InsertOpcodes(PBS3CG1STATE pThis, unsigned offDst)
478{
479 switch (pThis->cbOpcodes)
480 {
481 case 4: pThis->abCurInstr[offDst + 3] = pThis->abOpcodes[3];
482 case 3: pThis->abCurInstr[offDst + 2] = pThis->abOpcodes[2];
483 case 2: pThis->abCurInstr[offDst + 1] = pThis->abOpcodes[1];
484 case 1: pThis->abCurInstr[offDst] = pThis->abOpcodes[0];
485 return offDst + pThis->cbOpcodes;
486
487 default:
488 BS3_ASSERT(0);
489 return 0;
490 }
491}
492
493
494static unsigned Bs3Cg1EncodeNext(PBS3CG1STATE pThis, unsigned iEncoding)
495{
496 unsigned off;
497 switch (pThis->enmEncoding)
498 {
499 case BS3CG1ENC_MODRM_Eb_Gb:
500 /* Start by reg,reg encoding. */
501 if (iEncoding == 0)
502 {
503 off = Bs3Cg1InsertOpcodes(pThis, 0);
504 pThis->abCurInstr[off++] = X86_MODRM_MAKE(3, X86_GREG_xAX, X86_GREG_xCX);
505 pThis->cbCurInstr = off;
506 pThis->aOperands[pThis->iRegOp].idxField = BS3CG1DST_AL;
507 pThis->aOperands[pThis->iRmOp ].idxField = BS3CG1DST_CL;
508 iEncoding++;
509 }
510 break;
511
512 case BS3CG1ENC_MODRM_Ev_Gv:
513 case BS3CG1ENC_FIXED_AL_Ib:
514 case BS3CG1ENC_FIXED_rAX_Iz:
515 break;
516
517 case BS3CG1ENC_END:
518 case BS3CG1ENC_INVALID:
519 /* Impossible; to shut up gcc. */
520 break;
521 }
522
523
524 return iEncoding;
525}
526
527
528/**
529 * Prepares doing instruction encodings.
530 *
531 * This is in part specific to how the instruction is encoded, but generally it
532 * sets up basic operand values that doesn't change (much) when Bs3Cg1EncodeNext
533 * is called from within the loop.
534 *
535 * @returns Success indicator (true/false).
536 * @param pThis The state.
537 */
538static bool Bs3Cg1EncodePrep(PBS3CG1STATE pThis)
539{
540 pThis->iRmOp = RT_ELEMENTS(pThis->aOperands) - 1;
541 pThis->iRegOp = RT_ELEMENTS(pThis->aOperands) - 1;
542 pThis->fSameRingNotOkay = false;
543
544 switch (pThis->enmEncoding)
545 {
546 case BS3CG1ENC_MODRM_Eb_Gb:
547 pThis->iRmOp = 0;
548 pThis->iRegOp = 1;
549 pThis->aOperands[0].cbOp = 1;
550 pThis->aOperands[1].cbOp = 1;
551 pThis->aOperands[0].fMem = false;
552 pThis->aOperands[1].fMem = false;
553 break;
554
555 case BS3CG1ENC_MODRM_Ev_Gv:
556 pThis->iRmOp = 0;
557 pThis->iRegOp = 1;
558 pThis->aOperands[0].cbOp = 2;
559 pThis->aOperands[1].cbOp = 2;
560 pThis->aOperands[0].fMem = false;
561 pThis->aOperands[1].fMem = false;
562 break;
563
564 case BS3CG1ENC_FIXED_AL_Ib:
565 break;
566 case BS3CG1ENC_FIXED_rAX_Iz:
567 break;
568
569 default:
570 Bs3TestFailedF("Invalid enmEncoding for instruction #%u (%.*s): %d",
571 pThis->iInstr, pThis->cchMnemonic, pThis->pchMnemonic, pThis->enmEncoding);
572 return false;
573 }
574 return true;
575}
576
577
578/**
579 * Checks the preconditions for a test.
580 *
581 * @returns true if the test be executed, false if not.
582 * @param pThis The state.
583 * @param pHdr The test header.
584 */
585static bool Bs3Cg1RunSelector(PBS3CG1STATE pThis, PCBS3CG1TESTHDR pHdr)
586{
587
588 uint8_t const BS3_FAR *pbCode = (uint8_t const BS3_FAR *)(pHdr + 1);
589 unsigned cbLeft = pHdr->cbSelector;
590 while (cbLeft-- > 0)
591 {
592 switch (*pbCode++)
593 {
594#define CASE_PRED(a_Pred, a_Expr) \
595 case ((a_Pred) << BS3CG1SEL_OP_KIND_MASK) | BS3CG1SEL_OP_IS_TRUE: \
596 if (!(a_Expr)) return false; \
597 break; \
598 case ((a_Pred) << BS3CG1SEL_OP_KIND_MASK) | BS3CG1SEL_OP_IS_FALSE: \
599 if (a_Expr) return false; \
600 break
601 CASE_PRED(BS3CG1PRED_SIZE_O16, pThis->cBitsOp == 16);
602 CASE_PRED(BS3CG1PRED_SIZE_O32, pThis->cBitsOp == 32);
603 CASE_PRED(BS3CG1PRED_SIZE_O64, pThis->cBitsOp == 64);
604 CASE_PRED(BS3CG1PRED_RING_0, pThis->uCpl == 0);
605 CASE_PRED(BS3CG1PRED_RING_1, pThis->uCpl == 1);
606 CASE_PRED(BS3CG1PRED_RING_2, pThis->uCpl == 2);
607 CASE_PRED(BS3CG1PRED_RING_3, pThis->uCpl == 3);
608 CASE_PRED(BS3CG1PRED_RING_0_THRU_2, pThis->uCpl <= 2);
609 CASE_PRED(BS3CG1PRED_RING_1_THRU_3, pThis->uCpl >= 1);
610 CASE_PRED(BS3CG1PRED_CODE_64BIT, BS3_MODE_IS_64BIT_CODE(pThis->bMode));
611 CASE_PRED(BS3CG1PRED_CODE_32BIT, BS3_MODE_IS_32BIT_CODE(pThis->bMode));
612 CASE_PRED(BS3CG1PRED_CODE_16BIT, BS3_MODE_IS_16BIT_CODE(pThis->bMode));
613 CASE_PRED(BS3CG1PRED_MODE_REAL, BS3_MODE_IS_RM_SYS(pThis->bMode));
614 CASE_PRED(BS3CG1PRED_MODE_PROT, BS3_MODE_IS_PM_SYS(pThis->bMode));
615 CASE_PRED(BS3CG1PRED_MODE_LONG, BS3_MODE_IS_64BIT_SYS(pThis->bMode));
616 CASE_PRED(BS3CG1PRED_MODE_SMM, false);
617 CASE_PRED(BS3CG1PRED_MODE_VMX, false);
618 CASE_PRED(BS3CG1PRED_MODE_SVM, false);
619 CASE_PRED(BS3CG1PRED_PAGING_ON, BS3_MODE_IS_PAGED(pThis->bMode));
620 CASE_PRED(BS3CG1PRED_PAGING_OFF, !BS3_MODE_IS_PAGED(pThis->bMode));
621
622#undef CASE_PRED
623 default:
624 Bs3TestFailedF("Invalid selector opcode %#x!", pbCode[-1]);
625 return false;
626 }
627 }
628
629 return true;
630}
631
632
633/**
634 * Runs a context modifier program.
635 *
636 * @returns Success indicator (true/false).
637 * @param pThis The state.
638 * @param pCtx The context.
639 * @param pHdr The program header.
640 * @param off The program offset relative to the end of the header.
641 * @param cb The program size.
642 * @param pEflCtx The context to take undefined EFLAGS from. (This is NULL
643 * if we're processing a input context modifier program.)
644 */
645static bool Bs3Cg1RunContextModifier(PBS3CG1STATE pThis, PBS3REGCTX pCtx, PCBS3CG1TESTHDR pHdr, unsigned off, unsigned cb,
646 PCBS3REGCTX pEflCtx)
647{
648 uint8_t const BS3_FAR *pbCode = (uint8_t const BS3_FAR *)(pHdr + 1) + off;
649 int cbLeft = cb;
650 while (cbLeft-- > 0)
651 {
652 /*
653 * Decode the instruction.
654 */
655 uint8_t const bOpcode = *pbCode++;
656 unsigned cbValue;
657 unsigned cbDst;
658 BS3CG1DST idxField;
659
660 /* Expand the destiation field (can be escaped). */
661 switch (bOpcode & BS3CG1_CTXOP_DST_MASK)
662 {
663 case BS3CG1_CTXOP_OP1:
664 idxField = pThis->aOperands[0].idxField;
665 if (idxField == BS3CG1DST_INVALID)
666 idxField = BS3CG1DST_OP1;
667 break;
668
669 case BS3CG1_CTXOP_OP2:
670 idxField = pThis->aOperands[1].idxField;
671 if (idxField == BS3CG1DST_INVALID)
672 idxField = BS3CG1DST_OP2;
673 break;
674
675 case BS3CG1_CTXOP_EFL:
676 idxField = BS3CG1DST_EFL;
677 break;
678
679 case BS3CG1_CTXOP_DST_ESC:
680 if (cbLeft-- > 0)
681 {
682 idxField = (BS3CG1DST)*pbCode++;
683 if (idxField <= BS3CG1DST_OP4)
684 {
685 if (idxField > BS3CG1DST_INVALID)
686 {
687 uint8_t idxField2 = pThis->aOperands[idxField - BS3CG1DST_OP1].idxField;
688 if (idxField2 != BS3CG1DST_INVALID)
689 idxField = idxField2;
690 break;
691 }
692 }
693 else if (idxField < BS3CG1DST_END)
694 break;
695 Bs3TestFailedF("Malformed context instruction: idxField=%d", idxField);
696 return false;
697 }
698 /* fall thru */
699 default:
700 Bs3TestFailed("Malformed context instruction: Destination");
701 return false;
702 }
703
704
705 /* Expand value size (can be escaped). */
706 switch (bOpcode & BS3CG1_CTXOP_SIZE_MASK)
707 {
708 case BS3CG1_CTXOP_1_BYTE: cbValue = 1; break;
709 case BS3CG1_CTXOP_2_BYTES: cbValue = 2; break;
710 case BS3CG1_CTXOP_4_BYTES: cbValue = 4; break;
711 case BS3CG1_CTXOP_8_BYTES: cbValue = 8; break;
712 case BS3CG1_CTXOP_16_BYTES: cbValue = 16; break;
713 case BS3CG1_CTXOP_32_BYTES: cbValue = 32; break;
714 case BS3CG1_CTXOP_12_BYTES: cbValue = 12; break;
715 case BS3CG1_CTXOP_SIZE_ESC:
716 if (cbLeft-- > 0)
717 {
718 cbValue = *pbCode++;
719 if (cbValue)
720 break;
721 }
722 /* fall thru */
723 default:
724 Bs3TestFailed("Malformed context instruction: size");
725 return false;
726 }
727
728 /* Make sure there is enough instruction bytes for the value. */
729 if (cbValue <= cbLeft)
730 { /* likely */ }
731 else
732 {
733 Bs3TestFailedF("Malformed context instruction: %u bytes value, %u bytes left", cbValue, cbLeft);
734 return false;
735 }
736
737 /*
738 * Do value processing specific to the target field size.
739 */
740 cbDst = g_cbBs3Cg1DstFields[idxField];
741 if (cbDst == BS3CG1DSTSIZE_OPERAND)
742 cbDst = pThis->aOperands[idxField - BS3CG1DST_OP1].cbOp;
743 else if (cbDst == BS3CG1DSTSIZE_OPERAND_SIZE_GRP)
744 cbDst = pThis->cBitsOp / 8;
745 if (cbDst <= 8)
746 {
747 unsigned const offField = g_offBs3Cg1DstFields[idxField];
748 BS3PTRUNION PtrField;
749
750 /*
751 * Deal with fields up to 8-byte wide.
752 */
753 /* Get the value. */
754 uint64_t uValue;
755 if ((bOpcode & BS3CG1_CTXOP_SIGN_EXT))
756 switch (cbValue)
757 {
758 case 1: uValue = *(int8_t const BS3_FAR *)pbCode; break;
759 case 2: uValue = *(int16_t const BS3_FAR *)pbCode; break;
760 case 4: uValue = *(int32_t const BS3_FAR *)pbCode; break;
761 default:
762 if (cbValue >= 8)
763 {
764 uValue = *(uint64_t const BS3_FAR *)pbCode;
765 break;
766 }
767 Bs3TestFailedF("Malformed context instruction: %u bytes value (%u dst)", cbValue, cbDst);
768 return false;
769 }
770 else
771 switch (cbValue)
772 {
773 case 1: uValue = *(uint8_t const BS3_FAR *)pbCode; break;
774 case 2: uValue = *(uint16_t const BS3_FAR *)pbCode; break;
775 case 4: uValue = *(uint32_t const BS3_FAR *)pbCode; break;
776 default:
777 if (cbValue >= 8)
778 {
779 uValue = *(uint64_t const BS3_FAR *)pbCode;
780 break;
781 }
782 Bs3TestFailedF("Malformed context instruction: %u bytes value (%u dst)", cbValue, cbDst);
783 return false;
784 }
785
786 /* Find the field. */
787 if (offField < sizeof(BS3REGCTX))
788 PtrField.pu8 = (uint8_t BS3_FAR *)pCtx + offField;
789 /* Special field: Copying in undefined EFLAGS from the result context. */
790 else if (idxField == BS3CG1DST_EFL_UNDEF)
791 {
792 if (!pEflCtx || (bOpcode & BS3CG1_CTXOP_OPERATOR_MASK) != BS3CG1_CTXOP_ASSIGN)
793 {
794 Bs3TestFailed("Invalid BS3CG1DST_EFL_UNDEF usage");
795 return false;
796 }
797 PtrField.pu32 = &pCtx->rflags.u32;
798 uValue = (*PtrField.pu32 & ~(uint32_t)uValue) | (pEflCtx->rflags.u32 & (uint32_t)uValue);
799 }
800 //@todo else if (idxField <= BS3CG1DST_OP4)
801 //@todo {
802 //@todo
803 //@todo }
804 else
805 {
806 Bs3TestFailedF("Todo implement me: cbDst=%u idxField=%d offField=%#x", cbDst, idxField, offField);
807 return false;
808 }
809
810 /* Modify the field. */
811 switch (cbDst)
812 {
813 case 1:
814 switch (bOpcode & BS3CG1_CTXOP_OPERATOR_MASK)
815 {
816 case BS3CG1_CTXOP_ASSIGN: *PtrField.pu8 = (uint8_t)uValue; break;
817 case BS3CG1_CTXOP_OR: *PtrField.pu8 |= (uint8_t)uValue; break;
818 case BS3CG1_CTXOP_AND: *PtrField.pu8 &= (uint8_t)uValue; break;
819 case BS3CG1_CTXOP_AND_INV: *PtrField.pu8 &= ~(uint8_t)uValue; break;
820 }
821 break;
822
823 case 2:
824 switch (bOpcode & BS3CG1_CTXOP_OPERATOR_MASK)
825 {
826 case BS3CG1_CTXOP_ASSIGN: *PtrField.pu16 = (uint16_t)uValue; break;
827 case BS3CG1_CTXOP_OR: *PtrField.pu16 |= (uint16_t)uValue; break;
828 case BS3CG1_CTXOP_AND: *PtrField.pu16 &= (uint16_t)uValue; break;
829 case BS3CG1_CTXOP_AND_INV: *PtrField.pu16 &= ~(uint16_t)uValue; break;
830 }
831 break;
832
833 case 4:
834 switch (bOpcode & BS3CG1_CTXOP_OPERATOR_MASK)
835 {
836 case BS3CG1_CTXOP_ASSIGN: *PtrField.pu32 = (uint32_t)uValue; break;
837 case BS3CG1_CTXOP_OR: *PtrField.pu32 |= (uint32_t)uValue; break;
838 case BS3CG1_CTXOP_AND: *PtrField.pu32 &= (uint32_t)uValue; break;
839 case BS3CG1_CTXOP_AND_INV: *PtrField.pu32 &= ~(uint32_t)uValue; break;
840 }
841 break;
842
843 case 8:
844 switch (bOpcode & BS3CG1_CTXOP_OPERATOR_MASK)
845 {
846 case BS3CG1_CTXOP_ASSIGN: *PtrField.pu64 = (uint64_t)uValue; break;
847 case BS3CG1_CTXOP_OR: *PtrField.pu64 |= (uint64_t)uValue; break;
848 case BS3CG1_CTXOP_AND: *PtrField.pu64 &= (uint64_t)uValue; break;
849 case BS3CG1_CTXOP_AND_INV: *PtrField.pu64 &= ~(uint64_t)uValue; break;
850 }
851 break;
852
853 default:
854 Bs3TestFailedF("Malformed context instruction: cbDst=%u, expected 1, 2, 4, or 8", cbDst);
855 return false;
856 }
857 }
858 /*
859 * Deal with larger field (FPU, SSE, AVX, ...).
860 */
861 else
862 {
863 Bs3TestFailedF("TODO: Implement me: cbDst=%u idxField=%d", cbDst, idxField);
864 return false;
865 }
866
867 /*
868 * Advance to the next instruction.
869 */
870 pbCode += cbValue;
871 cbLeft -= cbValue;
872 }
873
874 return true;
875}
876
877
878
879BS3_DECL_FAR(uint8_t) BS3_CMN_NM(Bs3Cg1Worker)(uint8_t bMode)
880{
881 BS3CG1STATE This;
882 unsigned const iFirstRing = BS3_MODE_IS_V86(bMode) ? 3 : 0;
883 uint8_t const cRings = BS3_MODE_IS_RM_OR_V86(bMode) ? 1 : 4;
884 uint8_t iRing;
885 unsigned iInstr;
886 const char BS3_FAR * const pszMode = Bs3GetModeName(bMode);
887 BS3MEMKIND const enmMemKind = BS3_MODE_IS_RM_OR_V86(bMode) ? BS3MEMKIND_REAL
888 : BS3_MODE_IS_16BIT_CODE(bMode) ? BS3MEMKIND_TILED : BS3MEMKIND_FLAT32;
889
890 /*
891 * Initalize the state.
892 */
893 Bs3MemSet(&This, 0, sizeof(This));
894
895 This.bMode = bMode;
896 This.pchMnemonic = g_achBs3Cg1Mnemonics;
897 This.pabOperands = g_abBs3Cg1Operands;
898 This.pabOpcodes = g_abBs3Cg1Opcodes;
899 This.fAdvanceMnemonic = 1;
900
901 /* Allocate guarded exectuable and data memory. */
902 if (BS3_MODE_IS_PAGED(bMode))
903 {
904 This.pbCodePg = Bs3MemGuardedTestPageAlloc(enmMemKind);
905 if (!This.pbCodePg)
906 {
907 Bs3TestFailedF("First Bs3MemGuardedTestPageAlloc(%d) failed", enmMemKind);
908 return 0;
909 }
910 This.pbDataPg = Bs3MemGuardedTestPageAlloc(enmMemKind);
911 if (!This.pbDataPg)
912 {
913 Bs3MemGuardedTestPageFree(This.pbCodePg);
914 Bs3TestFailedF("Second Bs3MemGuardedTestPageAlloc(%d) failed", enmMemKind);
915 return 0;
916 }
917 }
918 else
919 {
920 This.pbCodePg = Bs3MemAlloc(enmMemKind, X86_PAGE_SIZE);
921 if (!This.pbCodePg)
922 {
923 Bs3TestFailedF("First Bs3MemAlloc(%d,Pg) failed", enmMemKind);
924 return 0;
925 }
926 This.pbDataPg = Bs3MemAlloc(enmMemKind, X86_PAGE_SIZE);
927 if (!This.pbDataPg)
928 {
929 Bs3MemFree(This.pbCodePg, X86_PAGE_SIZE);
930 Bs3TestFailedF("Second Bs3MemAlloc(%d,Pg) failed", enmMemKind);
931 return 0;
932 }
933 }
934
935 /* Create basic context for each target ring. In protected 16-bit code we need
936 set up code selectors that can access pbCodePg. ASSUMES 16-bit driver code! */
937 Bs3RegCtxSaveEx(&This.aInitialCtxs[iFirstRing], bMode, 512);
938 if (BS3_MODE_IS_16BIT_CODE(bMode) && !BS3_MODE_IS_RM_OR_V86(bMode))
939 {
940#if ARCH_BITS == 16
941 uintptr_t const uFlatCodePg = Bs3SelPtrToFlat(BS3_FP_MAKE(BS3_FP_SEG(This.pbCodePg), 0));
942#else
943 uintptr_t const uFlatCodePg = (uintptr_t)This.pbCodePg;
944#endif
945 BS3_ASSERT(ARCH_BITS == 16);
946 for (iRing = iFirstRing + 1; iRing < cRings; iRing++)
947 {
948 Bs3MemCpy(&This.aInitialCtxs[iRing], &This.aInitialCtxs[iFirstRing], sizeof(This.aInitialCtxs[iRing]));
949 Bs3RegCtxConvertToRingX(&This.aInitialCtxs[iRing], iRing);
950 }
951 for (iRing = iFirstRing; iRing < cRings; iRing++)
952 {
953 This.aInitialCtxs[iRing].cs = BS3_SEL_SPARE_00 + iRing * 8 + iRing;
954 Bs3SelSetup16BitCode(&Bs3GdteSpare00 + iRing, uFlatCodePg, iRing);
955 }
956 }
957 else
958 {
959 Bs3RegCtxSetRipCsFromCurPtr(&This.aInitialCtxs[iFirstRing], (FPFNBS3FAR)This.pbCodePg);
960 for (iRing = iFirstRing + 1; iRing < cRings; iRing++)
961 {
962 Bs3MemCpy(&This.aInitialCtxs[iRing], &This.aInitialCtxs[iFirstRing], sizeof(This.aInitialCtxs[iRing]));
963 Bs3RegCtxConvertToRingX(&This.aInitialCtxs[iRing], iRing);
964 }
965 }
966
967 /*
968 * Test the instructions.
969 */
970 for (iInstr = 0; iInstr < g_cBs3Cg1Instructions;
971 iInstr++,
972 This.pchMnemonic += This.fAdvanceMnemonic * This.cchMnemonic,
973 This.pabOperands += This.cOperands,
974 This.pabOpcodes += This.cbOpcodes)
975 {
976 unsigned iEncoding;
977 unsigned iEncodingNext;
978
979 /*
980 * Expand the instruction information into the state.
981 * Note! 16-bit will switch to a two level test header lookup once we exceed 64KB.
982 */
983 PCBS3CG1INSTR pInstr = &g_aBs3Cg1Instructions[iInstr];
984 This.iInstr = iInstr;
985 This.pTestHdr = (PCBS3CG1TESTHDR)&g_abBs3Cg1Tests[pInstr->offTests];
986 This.fFlags = pInstr->fFlags;
987 This.enmEncoding = (BS3CG1ENC)pInstr->enmEncoding;
988 This.cchMnemonic = pInstr->cchMnemonic;
989 if (This.fAdvanceMnemonic)
990 Bs3TestSubF("%.*s", This.cchMnemonic, This.pchMnemonic);
991 This.fAdvanceMnemonic = pInstr->fAdvanceMnemonic;
992 This.cOperands = pInstr->cOperands;
993 This.cbOpcodes = pInstr->cbOpcodes;
994 switch (This.cOperands)
995 {
996 case 3: This.aenmOperands[3] = (BS3CG1OP)This.pabOperands[3];
997 case 2: This.aenmOperands[2] = (BS3CG1OP)This.pabOperands[2];
998 case 1: This.aenmOperands[1] = (BS3CG1OP)This.pabOperands[1];
999 case 0: This.aenmOperands[0] = (BS3CG1OP)This.pabOperands[0];
1000 }
1001
1002 switch (This.cbOpcodes)
1003 {
1004 case 3: This.abOpcodes[3] = This.pabOpcodes[3];
1005 case 2: This.abOpcodes[2] = This.pabOpcodes[2];
1006 case 1: This.abOpcodes[1] = This.pabOpcodes[1];
1007 case 0: This.abOpcodes[0] = This.pabOpcodes[0];
1008 }
1009
1010 /*
1011 * Prep the operands and encoding handling.
1012 */
1013 if (!Bs3Cg1EncodePrep(&This))
1014 continue;
1015
1016 /*
1017 * Encode the instruction in various ways and check out the test values.
1018 */
1019 for (iEncoding = 0;; iEncoding = iEncodingNext)
1020 {
1021 /*
1022 * Encode the next instruction variation.
1023 */
1024 iEncodingNext = Bs3Cg1EncodeNext(&This, iEncoding);
1025 if (iEncodingNext <= iEncoding)
1026 break;
1027 BS3CG1_DPRINTF(("\ndbg: Encoding #%u: cbCurInst=%u %.*Rhxs\n", iEncoding, This.cbCurInstr, This.cbCurInstr, This.abCurInstr));
1028
1029 /*
1030 * Do the rings.
1031 */
1032 for (iRing = iFirstRing + This.fSameRingNotOkay; iRing < cRings; iRing++)
1033 {
1034 PCBS3CG1TESTHDR pHdr;
1035
1036 This.uCpl = iRing;
1037 BS3CG1_DPRINTF(("dbg: Ring %u\n", iRing));
1038
1039 /*
1040 * Do the tests one by one.
1041 */
1042 pHdr = This.pTestHdr;
1043 for (This.iTest = 0;; This.iTest++)
1044 {
1045 if (Bs3Cg1RunSelector(&This, pHdr))
1046 {
1047 /* Okay, set up the execution context. */
1048 uint8_t BS3_FAR *pbCode = &This.pbCodePg[BS3_MODE_IS_PAGED(bMode) ? This.cbCurInstr : 0];
1049
1050 Bs3MemCpy(&This.Ctx, &This.aInitialCtxs[iRing], sizeof(This.Ctx));
1051 if (BS3_MODE_IS_PAGED(bMode))
1052 pbCode = &This.pbCodePg[This.cbCurInstr];
1053 else
1054 {
1055 pbCode = This.pbCodePg;
1056 pbCode[This.cbCurInstr] = 0x0f; /* UD2 */
1057 pbCode[This.cbCurInstr + 1] = 0x0b;
1058 }
1059 Bs3MemCpy(pbCode, This.abCurInstr, This.cbCurInstr);
1060 This.Ctx.rip.u = BS3_FP_OFF(pbCode);
1061
1062 if (Bs3Cg1RunContextModifier(&This, &This.Ctx, pHdr, pHdr->cbSelector, pHdr->cbInput, NULL))
1063 {
1064 /* Run the instruction. */
1065 BS3CG1_DPRINTF(("dbg: Running test #%u\n", This.iTest));
1066 //Bs3RegCtxPrint(&This.Ctx);
1067 Bs3TrapSetJmpAndRestore(&This.Ctx, &This.TrapFrame);
1068 BS3CG1_DPRINTF(("dbg: bXcpt=%#x rip=%RX64 -> %RX64\n", This.TrapFrame.bXcpt, This.Ctx.rip.u, This.TrapFrame.Ctx.rip.u));
1069
1070 /* Check the control exception result first. */
1071 if ( This.TrapFrame.bXcpt == (BS3_MODE_IS_PAGED(bMode) ? X86_XCPT_PF : X86_XCPT_UD)
1072 && This.TrapFrame.Ctx.rip.u == This.Ctx.rip.u + This.cbCurInstr)
1073 {
1074 /* Apply output modifications and compare the contexts. */
1075 if (Bs3Cg1RunContextModifier(&This, &This.Ctx, pHdr,
1076 pHdr->cbSelector + pHdr->cbInput, pHdr->cbOutput,
1077 &This.TrapFrame.Ctx))
1078 {
1079 Bs3TestCheckRegCtxEx(&This.TrapFrame.Ctx, &This.Ctx, This.cbCurInstr, 0 /*cbSpAdjust*/,
1080 0 /*fExtraEfl*/, pszMode, iEncoding);
1081 }
1082 }
1083 else
1084 {
1085 Bs3TestFailedF("bXcpt=%#x expected %#x; rip=%RX64 expected %RX64 encoding: %.*Rhxs",
1086 This.TrapFrame.bXcpt, BS3_MODE_IS_PAGED(bMode) ? X86_XCPT_PF : X86_XCPT_UD,
1087 This.TrapFrame.Ctx.rip.u, This.Ctx.rip.u + This.cbCurInstr,
1088 This.cbCurInstr, This.abCurInstr);
1089 }
1090 }
1091 }
1092 else
1093 BS3CG1_DPRINTF(("dbg: Skipping #%u\n", This.iTest));
1094
1095 /* advance */
1096 if (pHdr->fLast)
1097 {
1098 BS3CG1_DPRINTF(("dbg: Last\n"));
1099 break;
1100 }
1101 pHdr = (PCBS3CG1TESTHDR)((uint8_t BS3_FAR *)(pHdr + 1) + pHdr->cbInput + pHdr->cbOutput + pHdr->cbSelector);
1102 }
1103 }
1104 }
1105
1106 }
1107
1108 /*
1109 * Clean up.
1110 */
1111 if (BS3_MODE_IS_PAGED(bMode))
1112 {
1113 Bs3MemGuardedTestPageFree(This.pbCodePg);
1114 Bs3MemGuardedTestPageFree(This.pbDataPg);
1115 }
1116 else
1117 {
1118 Bs3MemFree(This.pbCodePg, X86_PAGE_SIZE);
1119 Bs3MemFree(This.pbDataPg, X86_PAGE_SIZE);
1120 }
1121
1122 return 0;
1123}
1124
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette