VirtualBox

source: vbox/trunk/src/VBox/VMM/include/IEMN8veRecompiler.h@ 102447

Last change on this file since 102447 was 102436, checked in by vboxsync, 14 months ago

VMM/IEM: U8 mem map MCs. bugref:10371

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 38.4 KB
Line 
1/* $Id: IEMN8veRecompiler.h 102436 2023-12-02 22:09:36Z vboxsync $ */
2/** @file
3 * IEM - Interpreted Execution Manager - Native Recompiler Internals.
4 */
5
6/*
7 * Copyright (C) 2011-2023 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 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28#ifndef VMM_INCLUDED_SRC_include_IEMN8veRecompiler_h
29#define VMM_INCLUDED_SRC_include_IEMN8veRecompiler_h
30#ifndef RT_WITHOUT_PRAGMA_ONCE
31# pragma once
32#endif
33
34
35/** @defgroup grp_iem_n8ve_re Native Recompiler Internals.
36 * @ingroup grp_iem_int
37 * @{
38 */
39
40/** @def IEMNATIVE_WITH_TB_DEBUG_INFO
41 * Enables generating internal debug info for better TB disassembly dumping. */
42#if defined(DEBUG) || defined(DOXYGEN_RUNNING)
43# define IEMNATIVE_WITH_TB_DEBUG_INFO
44#endif
45
46/** Always count instructions for now. */
47#define IEMNATIVE_WITH_INSTRUCTION_COUNTING
48
49
50/** @name Stack Frame Layout
51 *
52 * @{ */
53/** The size of the area for stack variables and spills and stuff.
54 * @note This limit is duplicated in the python script(s). We add 0x40 for
55 * alignment padding. */
56#define IEMNATIVE_FRAME_VAR_SIZE (0xc0 + 0x40)
57/** Number of 64-bit variable slots (0x100 / 8 = 32. */
58#define IEMNATIVE_FRAME_VAR_SLOTS (IEMNATIVE_FRAME_VAR_SIZE / 8)
59AssertCompile(IEMNATIVE_FRAME_VAR_SLOTS == 32);
60
61#ifdef RT_ARCH_AMD64
62/** An stack alignment adjustment (between non-volatile register pushes and
63 * the stack variable area, so the latter better aligned). */
64# define IEMNATIVE_FRAME_ALIGN_SIZE 8
65
66/** Number of stack arguments slots for calls made from the frame. */
67# ifdef RT_OS_WINDOWS
68# define IEMNATIVE_FRAME_STACK_ARG_COUNT 4
69# else
70# define IEMNATIVE_FRAME_STACK_ARG_COUNT 2
71# endif
72/** Number of any shadow arguments (spill area) for calls we make. */
73# ifdef RT_OS_WINDOWS
74# define IEMNATIVE_FRAME_SHADOW_ARG_COUNT 4
75# else
76# define IEMNATIVE_FRAME_SHADOW_ARG_COUNT 0
77# endif
78
79/** Frame pointer (RBP) relative offset of the last push. */
80# ifdef RT_OS_WINDOWS
81# define IEMNATIVE_FP_OFF_LAST_PUSH (7 * -8)
82# else
83# define IEMNATIVE_FP_OFF_LAST_PUSH (5 * -8)
84# endif
85/** Frame pointer (RBP) relative offset of the stack variable area (the lowest
86 * address for it). */
87# define IEMNATIVE_FP_OFF_STACK_VARS (IEMNATIVE_FP_OFF_LAST_PUSH - IEMNATIVE_FRAME_ALIGN_SIZE - IEMNATIVE_FRAME_VAR_SIZE)
88/** Frame pointer (RBP) relative offset of the first stack argument for calls. */
89# define IEMNATIVE_FP_OFF_STACK_ARG0 (IEMNATIVE_FP_OFF_STACK_VARS - IEMNATIVE_FRAME_STACK_ARG_COUNT * 8)
90/** Frame pointer (RBP) relative offset of the second stack argument for calls. */
91# define IEMNATIVE_FP_OFF_STACK_ARG1 (IEMNATIVE_FP_OFF_STACK_ARG0 + 8)
92# ifdef RT_OS_WINDOWS
93/** Frame pointer (RBP) relative offset of the third stack argument for calls. */
94# define IEMNATIVE_FP_OFF_STACK_ARG2 (IEMNATIVE_FP_OFF_STACK_ARG0 + 16)
95/** Frame pointer (RBP) relative offset of the fourth stack argument for calls. */
96# define IEMNATIVE_FP_OFF_STACK_ARG3 (IEMNATIVE_FP_OFF_STACK_ARG0 + 24)
97# endif
98
99# ifdef RT_OS_WINDOWS
100/** Frame pointer (RBP) relative offset of the first incoming shadow argument. */
101# define IEMNATIVE_FP_OFF_IN_SHADOW_ARG0 (16)
102/** Frame pointer (RBP) relative offset of the second incoming shadow argument. */
103# define IEMNATIVE_FP_OFF_IN_SHADOW_ARG1 (24)
104/** Frame pointer (RBP) relative offset of the third incoming shadow argument. */
105# define IEMNATIVE_FP_OFF_IN_SHADOW_ARG2 (32)
106/** Frame pointer (RBP) relative offset of the fourth incoming shadow argument. */
107# define IEMNATIVE_FP_OFF_IN_SHADOW_ARG3 (40)
108# endif
109
110#elif RT_ARCH_ARM64
111/** No alignment padding needed for arm64. */
112# define IEMNATIVE_FRAME_ALIGN_SIZE 0
113/** No stack argument slots, got 8 registers for arguments will suffice. */
114# define IEMNATIVE_FRAME_STACK_ARG_COUNT 0
115/** There are no argument spill area. */
116# define IEMNATIVE_FRAME_SHADOW_ARG_COUNT 0
117
118/** Number of saved registers at the top of our stack frame.
119 * This includes the return address and old frame pointer, so x19 thru x30. */
120# define IEMNATIVE_FRAME_SAVE_REG_COUNT (12)
121/** The size of the save registered (IEMNATIVE_FRAME_SAVE_REG_COUNT). */
122# define IEMNATIVE_FRAME_SAVE_REG_SIZE (IEMNATIVE_FRAME_SAVE_REG_COUNT * 8)
123
124/** Frame pointer (BP) relative offset of the last push. */
125# define IEMNATIVE_FP_OFF_LAST_PUSH (7 * -8)
126
127/** Frame pointer (BP) relative offset of the stack variable area (the lowest
128 * address for it). */
129# define IEMNATIVE_FP_OFF_STACK_VARS (IEMNATIVE_FP_OFF_LAST_PUSH - IEMNATIVE_FRAME_ALIGN_SIZE - IEMNATIVE_FRAME_VAR_SIZE)
130
131#else
132# error "port me"
133#endif
134/** @} */
135
136
137/** @name Fixed Register Allocation(s)
138 * @{ */
139/** @def IEMNATIVE_REG_FIXED_PVMCPU
140 * The number of the register holding the pVCpu pointer. */
141/** @def IEMNATIVE_REG_FIXED_PCPUMCTX
142 * The number of the register holding the &pVCpu->cpum.GstCtx pointer.
143 * @note This not available on AMD64, only ARM64. */
144/** @def IEMNATIVE_REG_FIXED_TMP0
145 * Dedicated temporary register.
146 * @todo replace this by a register allocator and content tracker. */
147/** @def IEMNATIVE_REG_FIXED_MASK
148 * Mask GPRs with fixes assignments, either by us or dictated by the CPU/OS
149 * architecture. */
150#if defined(RT_ARCH_AMD64) && !defined(DOXYGEN_RUNNING)
151# define IEMNATIVE_REG_FIXED_PVMCPU X86_GREG_xBX
152# define IEMNATIVE_REG_FIXED_TMP0 X86_GREG_x11
153# define IEMNATIVE_REG_FIXED_MASK ( RT_BIT_32(IEMNATIVE_REG_FIXED_PVMCPU) \
154 | RT_BIT_32(IEMNATIVE_REG_FIXED_TMP0) \
155 | RT_BIT_32(X86_GREG_xSP) \
156 | RT_BIT_32(X86_GREG_xBP) )
157
158#elif defined(RT_ARCH_ARM64) || defined(DOXYGEN_RUNNING)
159# define IEMNATIVE_REG_FIXED_PVMCPU ARMV8_A64_REG_X28
160# define IEMNATIVE_REG_FIXED_PCPUMCTX ARMV8_A64_REG_X27
161# define IEMNATIVE_REG_FIXED_TMP0 ARMV8_A64_REG_X15
162# define IEMNATIVE_REG_FIXED_MASK ( RT_BIT_32(ARMV8_A64_REG_SP) \
163 | RT_BIT_32(ARMV8_A64_REG_LR) \
164 | RT_BIT_32(ARMV8_A64_REG_BP) \
165 | RT_BIT_32(IEMNATIVE_REG_FIXED_PVMCPU) \
166 | RT_BIT_32(IEMNATIVE_REG_FIXED_PCPUMCTX) \
167 | RT_BIT_32(ARMV8_A64_REG_X18) \
168 | RT_BIT_32(IEMNATIVE_REG_FIXED_TMP0) )
169
170#else
171# error "port me"
172#endif
173/** @} */
174
175/** @name Call related registers.
176 * @{ */
177/** @def IEMNATIVE_CALL_RET_GREG
178 * The return value register. */
179/** @def IEMNATIVE_CALL_ARG_GREG_COUNT
180 * Number of arguments in registers. */
181/** @def IEMNATIVE_CALL_ARG0_GREG
182 * The general purpose register carrying argument \#0. */
183/** @def IEMNATIVE_CALL_ARG1_GREG
184 * The general purpose register carrying argument \#1. */
185/** @def IEMNATIVE_CALL_ARG2_GREG
186 * The general purpose register carrying argument \#2. */
187/** @def IEMNATIVE_CALL_ARG3_GREG
188 * The general purpose register carrying argument \#3. */
189/** @def IEMNATIVE_CALL_VOLATILE_GREG_MASK
190 * Mask of registers the callee will not save and may trash. */
191#ifdef RT_ARCH_AMD64
192# define IEMNATIVE_CALL_RET_GREG X86_GREG_xAX
193
194# ifdef RT_OS_WINDOWS
195# define IEMNATIVE_CALL_ARG_GREG_COUNT 4
196# define IEMNATIVE_CALL_ARG0_GREG X86_GREG_xCX
197# define IEMNATIVE_CALL_ARG1_GREG X86_GREG_xDX
198# define IEMNATIVE_CALL_ARG2_GREG X86_GREG_x8
199# define IEMNATIVE_CALL_ARG3_GREG X86_GREG_x9
200# define IEMNATIVE_CALL_ARGS_GREG_MASK ( RT_BIT_32(IEMNATIVE_CALL_ARG0_GREG) \
201 | RT_BIT_32(IEMNATIVE_CALL_ARG1_GREG) \
202 | RT_BIT_32(IEMNATIVE_CALL_ARG2_GREG) \
203 | RT_BIT_32(IEMNATIVE_CALL_ARG3_GREG) )
204# define IEMNATIVE_CALL_VOLATILE_GREG_MASK ( RT_BIT_32(X86_GREG_xAX) \
205 | RT_BIT_32(X86_GREG_xCX) \
206 | RT_BIT_32(X86_GREG_xDX) \
207 | RT_BIT_32(X86_GREG_x8) \
208 | RT_BIT_32(X86_GREG_x9) \
209 | RT_BIT_32(X86_GREG_x10) \
210 | RT_BIT_32(X86_GREG_x11) )
211# else
212# define IEMNATIVE_CALL_ARG_GREG_COUNT 6
213# define IEMNATIVE_CALL_ARG0_GREG X86_GREG_xDI
214# define IEMNATIVE_CALL_ARG1_GREG X86_GREG_xSI
215# define IEMNATIVE_CALL_ARG2_GREG X86_GREG_xDX
216# define IEMNATIVE_CALL_ARG3_GREG X86_GREG_xCX
217# define IEMNATIVE_CALL_ARG4_GREG X86_GREG_x8
218# define IEMNATIVE_CALL_ARG5_GREG X86_GREG_x9
219# define IEMNATIVE_CALL_ARGS_GREG_MASK ( RT_BIT_32(IEMNATIVE_CALL_ARG0_GREG) \
220 | RT_BIT_32(IEMNATIVE_CALL_ARG1_GREG) \
221 | RT_BIT_32(IEMNATIVE_CALL_ARG2_GREG) \
222 | RT_BIT_32(IEMNATIVE_CALL_ARG3_GREG) \
223 | RT_BIT_32(IEMNATIVE_CALL_ARG4_GREG) \
224 | RT_BIT_32(IEMNATIVE_CALL_ARG5_GREG) )
225# define IEMNATIVE_CALL_VOLATILE_GREG_MASK ( RT_BIT_32(X86_GREG_xAX) \
226 | RT_BIT_32(X86_GREG_xCX) \
227 | RT_BIT_32(X86_GREG_xDX) \
228 | RT_BIT_32(X86_GREG_xDI) \
229 | RT_BIT_32(X86_GREG_xSI) \
230 | RT_BIT_32(X86_GREG_x8) \
231 | RT_BIT_32(X86_GREG_x9) \
232 | RT_BIT_32(X86_GREG_x10) \
233 | RT_BIT_32(X86_GREG_x11) )
234# endif
235
236#elif defined(RT_ARCH_ARM64)
237# define IEMNATIVE_CALL_RET_GREG ARMV8_A64_REG_X0
238# define IEMNATIVE_CALL_ARG_GREG_COUNT 8
239# define IEMNATIVE_CALL_ARG0_GREG ARMV8_A64_REG_X0
240# define IEMNATIVE_CALL_ARG1_GREG ARMV8_A64_REG_X1
241# define IEMNATIVE_CALL_ARG2_GREG ARMV8_A64_REG_X2
242# define IEMNATIVE_CALL_ARG3_GREG ARMV8_A64_REG_X3
243# define IEMNATIVE_CALL_ARG4_GREG ARMV8_A64_REG_X4
244# define IEMNATIVE_CALL_ARG5_GREG ARMV8_A64_REG_X5
245# define IEMNATIVE_CALL_ARG6_GREG ARMV8_A64_REG_X6
246# define IEMNATIVE_CALL_ARG7_GREG ARMV8_A64_REG_X7
247# define IEMNATIVE_CALL_ARGS_GREG_MASK ( RT_BIT_32(ARMV8_A64_REG_X0) \
248 | RT_BIT_32(ARMV8_A64_REG_X1) \
249 | RT_BIT_32(ARMV8_A64_REG_X2) \
250 | RT_BIT_32(ARMV8_A64_REG_X3) \
251 | RT_BIT_32(ARMV8_A64_REG_X4) \
252 | RT_BIT_32(ARMV8_A64_REG_X5) \
253 | RT_BIT_32(ARMV8_A64_REG_X6) \
254 | RT_BIT_32(ARMV8_A64_REG_X7) )
255# define IEMNATIVE_CALL_VOLATILE_GREG_MASK ( RT_BIT_32(ARMV8_A64_REG_X0) \
256 | RT_BIT_32(ARMV8_A64_REG_X1) \
257 | RT_BIT_32(ARMV8_A64_REG_X2) \
258 | RT_BIT_32(ARMV8_A64_REG_X3) \
259 | RT_BIT_32(ARMV8_A64_REG_X4) \
260 | RT_BIT_32(ARMV8_A64_REG_X5) \
261 | RT_BIT_32(ARMV8_A64_REG_X6) \
262 | RT_BIT_32(ARMV8_A64_REG_X7) \
263 | RT_BIT_32(ARMV8_A64_REG_X8) \
264 | RT_BIT_32(ARMV8_A64_REG_X9) \
265 | RT_BIT_32(ARMV8_A64_REG_X10) \
266 | RT_BIT_32(ARMV8_A64_REG_X11) \
267 | RT_BIT_32(ARMV8_A64_REG_X12) \
268 | RT_BIT_32(ARMV8_A64_REG_X13) \
269 | RT_BIT_32(ARMV8_A64_REG_X14) \
270 | RT_BIT_32(ARMV8_A64_REG_X15) \
271 | RT_BIT_32(ARMV8_A64_REG_X16) \
272 | RT_BIT_32(ARMV8_A64_REG_X17) )
273
274#endif
275
276/** This is the maximum argument count we'll ever be needing. */
277#if defined(RT_OS_WINDOWS) && defined(VBOXSTRICTRC_STRICT_ENABLED)
278# define IEMNATIVE_CALL_MAX_ARG_COUNT 8
279#else
280# define IEMNATIVE_CALL_MAX_ARG_COUNT 7
281#endif
282/** @} */
283
284
285/** @def IEMNATIVE_HST_GREG_COUNT
286 * Number of host general purpose registers we tracker. */
287/** @def IEMNATIVE_HST_GREG_MASK
288 * Mask corresponding to IEMNATIVE_HST_GREG_COUNT that can be applied to
289 * inverted register masks and such to get down to a correct set of regs. */
290#ifdef RT_ARCH_AMD64
291# define IEMNATIVE_HST_GREG_COUNT 16
292# define IEMNATIVE_HST_GREG_MASK UINT32_C(0xffff)
293
294#elif defined(RT_ARCH_ARM64)
295# define IEMNATIVE_HST_GREG_COUNT 32
296# define IEMNATIVE_HST_GREG_MASK UINT32_MAX
297#else
298# error "Port me!"
299#endif
300
301
302/** Native code generator label types. */
303typedef enum
304{
305 kIemNativeLabelType_Invalid = 0,
306 /* Labels w/o data, only once instance per TB: */
307 kIemNativeLabelType_Return,
308 kIemNativeLabelType_ReturnBreak,
309 kIemNativeLabelType_ReturnWithFlags,
310 kIemNativeLabelType_NonZeroRetOrPassUp,
311 kIemNativeLabelType_RaiseGp0,
312 /* Labels with data, potentially multiple instances per TB: */
313 kIemNativeLabelType_FirstWithMultipleInstances,
314 kIemNativeLabelType_If = kIemNativeLabelType_FirstWithMultipleInstances,
315 kIemNativeLabelType_Else,
316 kIemNativeLabelType_Endif,
317 kIemNativeLabelType_CheckIrq,
318 kIemNativeLabelType_TlbMiss,
319 kIemNativeLabelType_TlbDone,
320 kIemNativeLabelType_End
321} IEMNATIVELABELTYPE;
322
323/** Native code generator label definition. */
324typedef struct IEMNATIVELABEL
325{
326 /** Code offset if defined, UINT32_MAX if it needs to be generated after/in
327 * the epilog. */
328 uint32_t off;
329 /** The type of label (IEMNATIVELABELTYPE). */
330 uint16_t enmType;
331 /** Additional label data, type specific. */
332 uint16_t uData;
333} IEMNATIVELABEL;
334/** Pointer to a label. */
335typedef IEMNATIVELABEL *PIEMNATIVELABEL;
336
337
338/** Native code generator fixup types. */
339typedef enum
340{
341 kIemNativeFixupType_Invalid = 0,
342#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
343 /** AMD64 fixup: PC relative 32-bit with addend in bData. */
344 kIemNativeFixupType_Rel32,
345#elif defined(RT_ARCH_ARM64)
346 /** ARM64 fixup: PC relative offset at bits 25:0 (B, BL). */
347 kIemNativeFixupType_RelImm26At0,
348 /** ARM64 fixup: PC relative offset at bits 23:5 (CBZ, CBNZ, B.CC). */
349 kIemNativeFixupType_RelImm19At5,
350 /** ARM64 fixup: PC relative offset at bits 18:5 (TBZ, TBNZ). */
351 kIemNativeFixupType_RelImm14At5,
352#endif
353 kIemNativeFixupType_End
354} IEMNATIVEFIXUPTYPE;
355
356/** Native code generator fixup. */
357typedef struct IEMNATIVEFIXUP
358{
359 /** Code offset of the fixup location. */
360 uint32_t off;
361 /** The IEMNATIVELABEL this is a fixup for. */
362 uint16_t idxLabel;
363 /** The fixup type (IEMNATIVEFIXUPTYPE). */
364 uint8_t enmType;
365 /** Addend or other data. */
366 int8_t offAddend;
367} IEMNATIVEFIXUP;
368/** Pointer to a native code generator fixup. */
369typedef IEMNATIVEFIXUP *PIEMNATIVEFIXUP;
370
371
372/**
373 * Guest registers that can be shadowed in GPRs.
374 */
375typedef enum IEMNATIVEGSTREG : uint8_t
376{
377 kIemNativeGstReg_GprFirst = 0,
378 kIemNativeGstReg_GprLast = kIemNativeGstReg_GprFirst + 15,
379 kIemNativeGstReg_Pc,
380 kIemNativeGstReg_EFlags, /**< 32-bit, includes internal flags. */
381 kIemNativeGstReg_SegSelFirst,
382 kIemNativeGstReg_SegSelLast = kIemNativeGstReg_SegSelFirst + 5,
383 kIemNativeGstReg_SegBaseFirst,
384 kIemNativeGstReg_SegBaseLast = kIemNativeGstReg_SegBaseFirst + 5,
385 kIemNativeGstReg_SegLimitFirst,
386 kIemNativeGstReg_SegLimitLast = kIemNativeGstReg_SegLimitFirst + 5,
387 kIemNativeGstReg_End
388} IEMNATIVEGSTREG;
389
390/** @name Helpers for converting register numbers to IEMNATIVEGSTREG values.
391 * @{ */
392#define IEMNATIVEGSTREG_GPR(a_iGpr) ((IEMNATIVEGSTREG)(kIemNativeGstReg_GprFirst + (a_iGpr) ))
393#define IEMNATIVEGSTREG_SEG_SEL(a_iSegReg) ((IEMNATIVEGSTREG)(kIemNativeGstReg_SegSelFirst + (a_iSegReg) ))
394#define IEMNATIVEGSTREG_SEG_BASE(a_iSegReg) ((IEMNATIVEGSTREG)(kIemNativeGstReg_SegBaseFirst + (a_iSegReg) ))
395#define IEMNATIVEGSTREG_SEG_LIMIT(a_iSegReg) ((IEMNATIVEGSTREG)(kIemNativeGstReg_SegLimitFirst + (a_iSegReg) ))
396/** @} */
397
398/**
399 * Intended use statement for iemNativeRegAllocTmpForGuestReg().
400 */
401typedef enum IEMNATIVEGSTREGUSE
402{
403 /** The usage is read-only, the register holding the guest register
404 * shadow copy will not be modified by the caller. */
405 kIemNativeGstRegUse_ReadOnly = 0,
406 /** The caller will update the guest register (think: PC += cbInstr).
407 * The guest shadow copy will follow the returned register. */
408 kIemNativeGstRegUse_ForUpdate,
409 /** The call will put an entirely new value in the guest register, so
410 * if new register is allocate it will be returned uninitialized. */
411 kIemNativeGstRegUse_ForFullWrite,
412 /** The caller will use the guest register value as input in a calculation
413 * and the host register will be modified.
414 * This means that the returned host register will not be marked as a shadow
415 * copy of the guest register. */
416 kIemNativeGstRegUse_Calculation
417} IEMNATIVEGSTREGUSE;
418
419/**
420 * Guest registers (classes) that can be referenced.
421 */
422typedef enum IEMNATIVEGSTREGREF : uint8_t
423{
424 kIemNativeGstRegRef_Invalid = 0,
425 kIemNativeGstRegRef_Gpr,
426 kIemNativeGstRegRef_GprHighByte, /**< AH, CH, DH, BH*/
427 kIemNativeGstRegRef_EFlags,
428 kIemNativeGstRegRef_MxCsr,
429 kIemNativeGstRegRef_FpuReg,
430 kIemNativeGstRegRef_MReg,
431 kIemNativeGstRegRef_XReg,
432 //kIemNativeGstRegRef_YReg, - doesn't work.
433 kIemNativeGstRegRef_End
434} IEMNATIVEGSTREGREF;
435
436
437/** Variable kinds. */
438typedef enum IEMNATIVEVARKIND : uint8_t
439{
440 /** Customary invalid zero value. */
441 kIemNativeVarKind_Invalid = 0,
442 /** This is either in a register or on the stack. */
443 kIemNativeVarKind_Stack,
444 /** Immediate value - loaded into register when needed, or can live on the
445 * stack if referenced (in theory). */
446 kIemNativeVarKind_Immediate,
447 /** Variable reference - loaded into register when needed, never stack. */
448 kIemNativeVarKind_VarRef,
449 /** Guest register reference - loaded into register when needed, never stack. */
450 kIemNativeVarKind_GstRegRef,
451 /** End of valid values. */
452 kIemNativeVarKind_End
453} IEMNATIVEVARKIND;
454
455
456/** Variable or argument. */
457typedef struct IEMNATIVEVAR
458{
459 /** The kind of variable. */
460 IEMNATIVEVARKIND enmKind;
461 /** The variable size in bytes. */
462 uint8_t cbVar;
463 /** The first stack slot (uint64_t), except for immediate and references
464 * where it usually is UINT8_MAX. This is allocated lazily, so if a variable
465 * has a stack slot it has been initialized and has a value. Unused variables
466 * has neither a stack slot nor a host register assignment. */
467 uint8_t idxStackSlot;
468 /** The host register allocated for the variable, UINT8_MAX if not. */
469 uint8_t idxReg;
470 /** The argument number if argument, UINT8_MAX if regular variable. */
471 uint8_t uArgNo;
472 /** If referenced, the index of the variable referencing this one, otherwise
473 * UINT8_MAX. A referenced variable must only be placed on the stack and
474 * must be either kIemNativeVarKind_Stack or kIemNativeVarKind_Immediate. */
475 uint8_t idxReferrerVar;
476 /** Guest register being shadowed here, kIemNativeGstReg_End(/UINT8_MAX) if not.
477 * @todo not sure what this really is for... */
478 IEMNATIVEGSTREG enmGstReg;
479 uint8_t bAlign;
480
481 union
482 {
483 /** kIemNativeVarKind_Immediate: The immediate value. */
484 uint64_t uValue;
485 /** kIemNativeVarKind_VarRef: The index of the variable being referenced. */
486 uint8_t idxRefVar;
487 /** kIemNativeVarKind_GstRegRef: The guest register being referrenced. */
488 struct
489 {
490 /** The class of register. */
491 IEMNATIVEGSTREGREF enmClass;
492 /** Index within the class. */
493 uint8_t idx;
494 } GstRegRef;
495 } u;
496} IEMNATIVEVAR;
497
498/** What is being kept in a host register. */
499typedef enum IEMNATIVEWHAT : uint8_t
500{
501 /** The traditional invalid zero value. */
502 kIemNativeWhat_Invalid = 0,
503 /** Mapping a variable (IEMNATIVEHSTREG::idxVar). */
504 kIemNativeWhat_Var,
505 /** Temporary register, this is typically freed when a MC completes. */
506 kIemNativeWhat_Tmp,
507 /** Call argument w/o a variable mapping. This is free (via
508 * IEMNATIVE_CALL_VOLATILE_GREG_MASK) after the call is emitted. */
509 kIemNativeWhat_Arg,
510 /** Return status code.
511 * @todo not sure if we need this... */
512 kIemNativeWhat_rc,
513 /** The fixed pVCpu (PVMCPUCC) register.
514 * @todo consider offsetting this on amd64 to use negative offsets to access
515 * more members using 8-byte disp. */
516 kIemNativeWhat_pVCpuFixed,
517 /** The fixed pCtx (PCPUMCTX) register.
518 * @todo consider offsetting this on amd64 to use negative offsets to access
519 * more members using 8-byte disp. */
520 kIemNativeWhat_pCtxFixed,
521 /** Fixed temporary register. */
522 kIemNativeWhat_FixedTmp,
523 /** Register reserved by the CPU or OS architecture. */
524 kIemNativeWhat_FixedReserved,
525 /** End of valid values. */
526 kIemNativeWhat_End
527} IEMNATIVEWHAT;
528
529/**
530 * Host general register entry.
531 *
532 * The actual allocation status is kept in IEMRECOMPILERSTATE::bmHstRegs.
533 *
534 * @todo Track immediate values in host registers similarlly to how we track the
535 * guest register shadow copies. For it to be real helpful, though,
536 * we probably need to know which will be reused and put them into
537 * non-volatile registers, otherwise it's going to be more or less
538 * restricted to an instruction or two.
539 */
540typedef struct IEMNATIVEHSTREG
541{
542 /** Set of guest registers this one shadows.
543 *
544 * Using a bitmap here so we can designate the same host register as a copy
545 * for more than one guest register. This is expected to be useful in
546 * situations where one value is copied to several registers in a sequence.
547 * If the mapping is 1:1, then we'd have to pick which side of a 'MOV SRC,DST'
548 * sequence we'd want to let this register follow to be a copy of and there
549 * will always be places where we'd be picking the wrong one.
550 */
551 uint64_t fGstRegShadows;
552 /** What is being kept in this register. */
553 IEMNATIVEWHAT enmWhat;
554 /** Variable index if holding a variable, otherwise UINT8_MAX. */
555 uint8_t idxVar;
556 /** Alignment padding. */
557 uint8_t abAlign[6];
558} IEMNATIVEHSTREG;
559
560
561/**
562 * Core state for the native recompiler, that is, things that needs careful
563 * handling when dealing with branches.
564 */
565typedef struct IEMNATIVECORESTATE
566{
567 /** Allocation bitmap for aHstRegs. */
568 uint32_t bmHstRegs;
569
570 /** Bitmap marking which host register contains guest register shadow copies.
571 * This is used during register allocation to try preserve copies. */
572 uint32_t bmHstRegsWithGstShadow;
573 /** Bitmap marking valid entries in aidxGstRegShadows. */
574 uint64_t bmGstRegShadows;
575
576 union
577 {
578 /** Index of variable arguments, UINT8_MAX if not valid. */
579 uint8_t aidxArgVars[8];
580 /** For more efficient resetting. */
581 uint64_t u64ArgVars;
582 };
583
584 /** Allocation bitmap for the stack. */
585 uint32_t bmStack;
586 /** Allocation bitmap for aVars. */
587 uint32_t bmVars;
588
589 /** Maps a guest register to a host GPR (index by IEMNATIVEGSTREG).
590 * Entries are only valid if the corresponding bit in bmGstRegShadows is set.
591 * (A shadow copy of a guest register can only be held in a one host register,
592 * there are no duplicate copies or ambiguities like that). */
593 uint8_t aidxGstRegShadows[kIemNativeGstReg_End];
594
595 /** Host register allocation tracking. */
596 IEMNATIVEHSTREG aHstRegs[IEMNATIVE_HST_GREG_COUNT];
597
598 /** Variables and arguments. */
599 IEMNATIVEVAR aVars[9];
600} IEMNATIVECORESTATE;
601/** Pointer to core state. */
602typedef IEMNATIVECORESTATE *PIEMNATIVECORESTATE;
603/** Pointer to const core state. */
604typedef IEMNATIVECORESTATE const *PCIEMNATIVECORESTATE;
605
606
607/**
608 * Conditional stack entry.
609 */
610typedef struct IEMNATIVECOND
611{
612 /** Set if we're in the "else" part, clear if we're in the "if" before it. */
613 bool fInElse;
614 /** The label for the IEM_MC_ELSE. */
615 uint32_t idxLabelElse;
616 /** The label for the IEM_MC_ENDIF. */
617 uint32_t idxLabelEndIf;
618 /** The initial state snapshot as the if-block starts executing. */
619 IEMNATIVECORESTATE InitialState;
620 /** The state snapshot at the end of the if-block. */
621 IEMNATIVECORESTATE IfFinalState;
622} IEMNATIVECOND;
623/** Pointer to a condition stack entry. */
624typedef IEMNATIVECOND *PIEMNATIVECOND;
625
626
627/**
628 * Native recompiler state.
629 */
630typedef struct IEMRECOMPILERSTATE
631{
632 /** Size of the buffer that pbNativeRecompileBufR3 points to in
633 * IEMNATIVEINSTR units. */
634 uint32_t cInstrBufAlloc;
635#ifdef VBOX_STRICT
636 /** Strict: How far the last iemNativeInstrBufEnsure() checked. */
637 uint32_t offInstrBufChecked;
638#else
639 uint32_t uPadding1; /* We don't keep track of the size here... */
640#endif
641 /** Fixed temporary code buffer for native recompilation. */
642 PIEMNATIVEINSTR pInstrBuf;
643
644 /** Bitmaps with the label types used. */
645 uint64_t bmLabelTypes;
646 /** Actual number of labels in paLabels. */
647 uint32_t cLabels;
648 /** Max number of entries allowed in paLabels before reallocating it. */
649 uint32_t cLabelsAlloc;
650 /** Labels defined while recompiling (referenced by fixups). */
651 PIEMNATIVELABEL paLabels;
652 /** Array with indexes of unique labels (uData always 0). */
653 uint32_t aidxUniqueLabels[kIemNativeLabelType_FirstWithMultipleInstances];
654
655 /** Actual number of fixups paFixups. */
656 uint32_t cFixups;
657 /** Max number of entries allowed in paFixups before reallocating it. */
658 uint32_t cFixupsAlloc;
659 /** Buffer used by the recompiler for recording fixups when generating code. */
660 PIEMNATIVEFIXUP paFixups;
661
662#ifdef IEMNATIVE_WITH_TB_DEBUG_INFO
663 /** Number of debug info entries allocated for pDbgInfo. */
664 uint32_t cDbgInfoAlloc;
665 uint32_t uPadding;
666 /** Debug info. */
667 PIEMTBDBG pDbgInfo;
668#endif
669
670 /** The translation block being recompiled. */
671 PCIEMTB pTbOrg;
672
673 /** Condition sequence number (for generating unique labels). */
674 uint16_t uCondSeqNo;
675 /** Check IRQ seqeunce number (for generating unique labels). */
676 uint16_t uCheckIrqSeqNo;
677 /** TLB load sequence number (for generating unique labels). */
678 uint16_t uTlbSeqNo;
679 /** The current condition stack depth (aCondStack). */
680 uint8_t cCondDepth;
681
682 /** The argument count + hidden regs from the IEM_MC_BEGIN statement. */
683 uint8_t cArgs;
684 /** The IEM_CIMPL_F_XXX flags from the IEM_MC_BEGIN statement. */
685 uint32_t fCImpl;
686 /** The IEM_MC_F_XXX flags from the IEM_MC_BEGIN statement. */
687 uint32_t fMc;
688 /** The expected IEMCPU::fExec value for the current call/instruction. */
689 uint32_t fExec;
690
691 /** Core state requiring care with branches. */
692 IEMNATIVECORESTATE Core;
693
694 /** The condition nesting stack. */
695 IEMNATIVECOND aCondStack[2];
696
697#ifndef IEM_WITH_THROW_CATCH
698 /** Pointer to the setjmp/longjmp buffer if we're not using C++ exceptions
699 * for recompilation error handling. */
700 jmp_buf JmpBuf;
701#endif
702} IEMRECOMPILERSTATE;
703/** Pointer to a native recompiler state. */
704typedef IEMRECOMPILERSTATE *PIEMRECOMPILERSTATE;
705
706
707/** @def IEMNATIVE_TRY_SETJMP
708 * Wrapper around setjmp / try, hiding all the ugly differences.
709 *
710 * @note Use with extreme care as this is a fragile macro.
711 * @param a_pReNative The native recompile state.
712 * @param a_rcTarget The variable that should receive the status code in case
713 * of a longjmp/throw.
714 */
715/** @def IEMNATIVE_CATCH_LONGJMP_BEGIN
716 * Start wrapper for catch / setjmp-else.
717 *
718 * This will set up a scope.
719 *
720 * @note Use with extreme care as this is a fragile macro.
721 * @param a_pReNative The native recompile state.
722 * @param a_rcTarget The variable that should receive the status code in case
723 * of a longjmp/throw.
724 */
725/** @def IEMNATIVE_CATCH_LONGJMP_END
726 * End wrapper for catch / setjmp-else.
727 *
728 * This will close the scope set up by IEMNATIVE_CATCH_LONGJMP_BEGIN and clean
729 * up the state.
730 *
731 * @note Use with extreme care as this is a fragile macro.
732 * @param a_pReNative The native recompile state.
733 */
734/** @def IEMNATIVE_DO_LONGJMP
735 *
736 * Wrapper around longjmp / throw.
737 *
738 * @param a_pReNative The native recompile state.
739 * @param a_rc The status code jump back with / throw.
740 */
741#ifdef IEM_WITH_THROW_CATCH
742# define IEMNATIVE_TRY_SETJMP(a_pReNative, a_rcTarget) \
743 a_rcTarget = VINF_SUCCESS; \
744 try
745# define IEMNATIVE_CATCH_LONGJMP_BEGIN(a_pReNative, a_rcTarget) \
746 catch (int rcThrown) \
747 { \
748 a_rcTarget = rcThrown
749# define IEMNATIVE_CATCH_LONGJMP_END(a_pReNative) \
750 } \
751 ((void)0)
752# define IEMNATIVE_DO_LONGJMP(a_pReNative, a_rc) throw int(a_rc)
753#else /* !IEM_WITH_THROW_CATCH */
754# define IEMNATIVE_TRY_SETJMP(a_pReNative, a_rcTarget) \
755 if ((a_rcTarget = setjmp((a_pReNative)->JmpBuf)) == 0)
756# define IEMNATIVE_CATCH_LONGJMP_BEGIN(a_pReNative, a_rcTarget) \
757 else \
758 { \
759 ((void)0)
760# define IEMNATIVE_CATCH_LONGJMP_END(a_pReNative) \
761 }
762# define IEMNATIVE_DO_LONGJMP(a_pReNative, a_rc) longjmp((a_pReNative)->JmpBuf, (a_rc))
763#endif /* !IEM_WITH_THROW_CATCH */
764
765
766/**
767 * Native recompiler worker for a threaded function.
768 *
769 * @returns New code buffer offset; throws VBox status code in case of a failure.
770 * @param pReNative The native recompiler state.
771 * @param off The current code buffer offset.
772 * @param pCallEntry The threaded call entry.
773 *
774 * @note This may throw/longjmp VBox status codes (int) to abort compilation, so no RT_NOEXCEPT!
775 */
776typedef uint32_t (VBOXCALL FNIEMNATIVERECOMPFUNC)(PIEMRECOMPILERSTATE pReNative, uint32_t off, PCIEMTHRDEDCALLENTRY pCallEntry);
777/** Pointer to a native recompiler worker for a threaded function. */
778typedef FNIEMNATIVERECOMPFUNC *PFNIEMNATIVERECOMPFUNC;
779
780/** Defines a native recompiler worker for a threaded function.
781 * @see FNIEMNATIVERECOMPFUNC */
782#define IEM_DECL_IEMNATIVERECOMPFUNC_DEF(a_Name) \
783 uint32_t VBOXCALL a_Name(PIEMRECOMPILERSTATE pReNative, uint32_t off, PCIEMTHRDEDCALLENTRY pCallEntry)
784
785/** Prototypes a native recompiler function for a threaded function.
786 * @see FNIEMNATIVERECOMPFUNC */
787#define IEM_DECL_IEMNATIVERECOMPFUNC_PROTO(a_Name) FNIEMNATIVERECOMPFUNC a_Name
788
789
790/** Define a native recompiler helper function, safe to call from the TB code. */
791#define IEM_DECL_NATIVE_HLP_DEF(a_RetType, a_Name, a_ArgList) \
792 DECL_HIDDEN_THROW(a_RetType) VBOXCALL a_Name a_ArgList
793/** Prototype a native recompiler helper function, safe to call from the TB code. */
794#define IEM_DECL_NATIVE_HLP_PROTO(a_RetType, a_Name, a_ArgList) \
795 DECL_HIDDEN_THROW(a_RetType) VBOXCALL a_Name a_ArgList
796
797
798DECL_HIDDEN_THROW(uint32_t) iemNativeLabelCreate(PIEMRECOMPILERSTATE pReNative, IEMNATIVELABELTYPE enmType,
799 uint32_t offWhere = UINT32_MAX, uint16_t uData = 0);
800DECL_HIDDEN_THROW(void) iemNativeLabelDefine(PIEMRECOMPILERSTATE pReNative, uint32_t idxLabel, uint32_t offWhere);
801DECL_HIDDEN_THROW(void) iemNativeAddFixup(PIEMRECOMPILERSTATE pReNative, uint32_t offWhere, uint32_t idxLabel,
802 IEMNATIVEFIXUPTYPE enmType, int8_t offAddend = 0);
803DECL_HIDDEN_THROW(PIEMNATIVEINSTR) iemNativeInstrBufEnsureSlow(PIEMRECOMPILERSTATE pReNative, uint32_t off, uint32_t cInstrReq);
804
805DECL_HIDDEN_THROW(uint8_t) iemNativeRegAllocTmp(PIEMRECOMPILERSTATE pReNative, uint32_t *poff, bool fPreferVolatile = true);
806DECL_HIDDEN_THROW(uint8_t) iemNativeRegAllocTmpImm(PIEMRECOMPILERSTATE pReNative, uint32_t *poff, uint64_t uImm,
807 bool fPreferVolatile = true);
808DECL_HIDDEN_THROW(uint8_t) iemNativeRegAllocTmpForGuestReg(PIEMRECOMPILERSTATE pReNative, uint32_t *poff,
809 IEMNATIVEGSTREG enmGstReg, IEMNATIVEGSTREGUSE enmIntendedUse);
810DECL_HIDDEN_THROW(uint8_t) iemNativeRegAllocTmpForGuestRegIfAlreadyPresent(PIEMRECOMPILERSTATE pReNative, uint32_t *poff,
811 IEMNATIVEGSTREG enmGstReg);
812
813DECL_HIDDEN_THROW(uint8_t) iemNativeRegAllocVar(PIEMRECOMPILERSTATE pReNative, uint32_t *poff, uint8_t idxVar);
814DECL_HIDDEN_THROW(uint32_t) iemNativeRegAllocArgs(PIEMRECOMPILERSTATE pReNative, uint32_t off, uint8_t cArgs);
815DECL_HIDDEN_THROW(uint8_t) iemNativeRegAssignRc(PIEMRECOMPILERSTATE pReNative, uint8_t idxHstReg);
816DECLHIDDEN(void) iemNativeRegFree(PIEMRECOMPILERSTATE pReNative, uint8_t idxHstReg) RT_NOEXCEPT;
817DECLHIDDEN(void) iemNativeRegFreeTmp(PIEMRECOMPILERSTATE pReNative, uint8_t idxHstReg) RT_NOEXCEPT;
818DECLHIDDEN(void) iemNativeRegFreeTmpImm(PIEMRECOMPILERSTATE pReNative, uint8_t idxHstReg) RT_NOEXCEPT;
819DECLHIDDEN(void) iemNativeRegFreeVar(PIEMRECOMPILERSTATE pReNative, uint8_t idxHstReg, bool fFlushShadows) RT_NOEXCEPT;
820DECLHIDDEN(void) iemNativeRegFreeAndFlushMask(PIEMRECOMPILERSTATE pReNative, uint32_t fHstRegMask) RT_NOEXCEPT;
821DECL_HIDDEN_THROW(uint32_t) iemNativeRegFlushPendingWrites(PIEMRECOMPILERSTATE pReNative, uint32_t off);
822
823DECL_HIDDEN_THROW(uint8_t) iemNativeVarGetStackSlot(PIEMRECOMPILERSTATE pReNative, uint8_t idxVar);
824
825DECL_HIDDEN_THROW(uint32_t) iemNativeEmitLoadGprWithGstShadowReg(PIEMRECOMPILERSTATE pReNative, uint32_t off,
826 uint8_t idxHstReg, IEMNATIVEGSTREG enmGstReg);
827DECL_HIDDEN_THROW(uint32_t) iemNativeEmitCheckCallRetAndPassUp(PIEMRECOMPILERSTATE pReNative, uint32_t off, uint8_t idxInstr);
828
829extern DECL_HIDDEN_DATA(const char * const) g_apszIemNativeHstRegNames[];
830
831
832/**
833 * Ensures that there is sufficient space in the instruction output buffer.
834 *
835 * This will reallocate the buffer if needed and allowed.
836 *
837 * @note Always use IEMNATIVE_ASSERT_INSTR_BUF_ENSURE when done to check the
838 * allocation size.
839 *
840 * @returns Pointer to the instruction output buffer on success; throws VBox
841 * status code on failure, so no need to check it.
842 * @param pReNative The native recompile state.
843 * @param off Current instruction offset. Works safely for UINT32_MAX
844 * as well.
845 * @param cInstrReq Number of instruction about to be added. It's okay to
846 * overestimate this a bit.
847 */
848DECL_FORCE_INLINE_THROW(PIEMNATIVEINSTR)
849iemNativeInstrBufEnsure(PIEMRECOMPILERSTATE pReNative, uint32_t off, uint32_t cInstrReq)
850{
851 uint64_t const offChecked = off + (uint64_t)cInstrReq; /** @todo may reconsider the need for UINT32_MAX safety... */
852 if (RT_LIKELY(offChecked <= pReNative->cInstrBufAlloc))
853 {
854#ifdef VBOX_STRICT
855 pReNative->offInstrBufChecked = offChecked;
856#endif
857 return pReNative->pInstrBuf;
858 }
859 return iemNativeInstrBufEnsureSlow(pReNative, off, cInstrReq);
860}
861
862/**
863 * Checks that we didn't exceed the space requested in the last
864 * iemNativeInstrBufEnsure() call.
865 */
866#define IEMNATIVE_ASSERT_INSTR_BUF_ENSURE(a_pReNative, a_off) \
867 AssertMsg((a_off) <= (a_pReNative)->offInstrBufChecked, \
868 ("off=%#x offInstrBufChecked=%#x\n", (a_off), (a_pReNative)->offInstrBufChecked))
869
870/**
871 * Checks that a variable index is valid.
872 */
873#define IEMNATIVE_ASSERT_VAR_IDX(a_pReNative, a_idxVar) \
874 AssertMsg( (unsigned)(a_idxVar) < RT_ELEMENTS((a_pReNative)->Core.aVars) \
875 && ((a_pReNative)->Core.bmVars & RT_BIT_32(a_idxVar)), ("%s=%d\n", #a_idxVar, a_idxVar))
876
877/**
878 * Checks that a variable index is valid and that the variable is assigned the
879 * correct argument number.
880 * This also adds a RT_NOREF of a_idxVar.
881 */
882#define IEMNATIVE_ASSERT_ARG_VAR_IDX(a_pReNative, a_idxVar, a_uArgNo) do { \
883 RT_NOREF_PV(a_idxVar); \
884 AssertMsg( (unsigned)(a_idxVar) < RT_ELEMENTS((a_pReNative)->Core.aVars) \
885 && ((a_pReNative)->Core.bmVars & RT_BIT_32(a_idxVar))\
886 && (a_pReNative)->Core.aVars[a_idxVar].uArgNo == (a_uArgNo) \
887 , ("%s=%d; uArgNo=%d, expected %u\n", #a_idxVar, a_idxVar, \
888 (a_pReNative)->Core.aVars[RT_MAX(a_idxVar, RT_ELEMENTS((a_pReNative)->Core.aVars)) - 1].uArgNo, a_uArgNo)); \
889 } while (0)
890
891/**
892 * Calculates the stack address of a variable as a [r]BP displacement value.
893 */
894DECL_FORCE_INLINE(int32_t)
895iemNativeStackCalcBpDisp(uint8_t idxStackSlot)
896{
897 Assert(idxStackSlot < IEMNATIVE_FRAME_VAR_SLOTS);
898 return idxStackSlot * sizeof(uint64_t) + IEMNATIVE_FP_OFF_STACK_VARS;
899}
900
901/** @} */
902
903#endif /* !VMM_INCLUDED_SRC_include_IEMN8veRecompiler_h */
904
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