1 | /* $Id: IEMN8veRecompiler.h 102847 2024-01-11 14:41:51Z 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)
|
---|
59 | AssertCompile(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 (10 * -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. */
|
---|
303 | typedef 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 | kIemNativeLabelType_ObsoleteTb,
|
---|
313 | kIemNativeLabelType_NeedCsLimChecking,
|
---|
314 | kIemNativeLabelType_CheckBranchMiss,
|
---|
315 | /* Labels with data, potentially multiple instances per TB: */
|
---|
316 | kIemNativeLabelType_FirstWithMultipleInstances,
|
---|
317 | kIemNativeLabelType_If = kIemNativeLabelType_FirstWithMultipleInstances,
|
---|
318 | kIemNativeLabelType_Else,
|
---|
319 | kIemNativeLabelType_Endif,
|
---|
320 | kIemNativeLabelType_CheckIrq,
|
---|
321 | kIemNativeLabelType_TlbLookup,
|
---|
322 | kIemNativeLabelType_TlbMiss,
|
---|
323 | kIemNativeLabelType_TlbDone,
|
---|
324 | kIemNativeLabelType_End
|
---|
325 | } IEMNATIVELABELTYPE;
|
---|
326 |
|
---|
327 | /** Native code generator label definition. */
|
---|
328 | typedef struct IEMNATIVELABEL
|
---|
329 | {
|
---|
330 | /** Code offset if defined, UINT32_MAX if it needs to be generated after/in
|
---|
331 | * the epilog. */
|
---|
332 | uint32_t off;
|
---|
333 | /** The type of label (IEMNATIVELABELTYPE). */
|
---|
334 | uint16_t enmType;
|
---|
335 | /** Additional label data, type specific. */
|
---|
336 | uint16_t uData;
|
---|
337 | } IEMNATIVELABEL;
|
---|
338 | /** Pointer to a label. */
|
---|
339 | typedef IEMNATIVELABEL *PIEMNATIVELABEL;
|
---|
340 |
|
---|
341 |
|
---|
342 | /** Native code generator fixup types. */
|
---|
343 | typedef enum
|
---|
344 | {
|
---|
345 | kIemNativeFixupType_Invalid = 0,
|
---|
346 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
347 | /** AMD64 fixup: PC relative 32-bit with addend in bData. */
|
---|
348 | kIemNativeFixupType_Rel32,
|
---|
349 | #elif defined(RT_ARCH_ARM64)
|
---|
350 | /** ARM64 fixup: PC relative offset at bits 25:0 (B, BL). */
|
---|
351 | kIemNativeFixupType_RelImm26At0,
|
---|
352 | /** ARM64 fixup: PC relative offset at bits 23:5 (CBZ, CBNZ, B.CC). */
|
---|
353 | kIemNativeFixupType_RelImm19At5,
|
---|
354 | /** ARM64 fixup: PC relative offset at bits 18:5 (TBZ, TBNZ). */
|
---|
355 | kIemNativeFixupType_RelImm14At5,
|
---|
356 | #endif
|
---|
357 | kIemNativeFixupType_End
|
---|
358 | } IEMNATIVEFIXUPTYPE;
|
---|
359 |
|
---|
360 | /** Native code generator fixup. */
|
---|
361 | typedef struct IEMNATIVEFIXUP
|
---|
362 | {
|
---|
363 | /** Code offset of the fixup location. */
|
---|
364 | uint32_t off;
|
---|
365 | /** The IEMNATIVELABEL this is a fixup for. */
|
---|
366 | uint16_t idxLabel;
|
---|
367 | /** The fixup type (IEMNATIVEFIXUPTYPE). */
|
---|
368 | uint8_t enmType;
|
---|
369 | /** Addend or other data. */
|
---|
370 | int8_t offAddend;
|
---|
371 | } IEMNATIVEFIXUP;
|
---|
372 | /** Pointer to a native code generator fixup. */
|
---|
373 | typedef IEMNATIVEFIXUP *PIEMNATIVEFIXUP;
|
---|
374 |
|
---|
375 |
|
---|
376 | /**
|
---|
377 | * Guest registers that can be shadowed in GPRs.
|
---|
378 | */
|
---|
379 | typedef enum IEMNATIVEGSTREG : uint8_t
|
---|
380 | {
|
---|
381 | kIemNativeGstReg_GprFirst = 0,
|
---|
382 | kIemNativeGstReg_GprLast = kIemNativeGstReg_GprFirst + 15,
|
---|
383 | kIemNativeGstReg_Pc,
|
---|
384 | kIemNativeGstReg_EFlags, /**< 32-bit, includes internal flags. */
|
---|
385 | kIemNativeGstReg_SegSelFirst,
|
---|
386 | kIemNativeGstReg_SegSelLast = kIemNativeGstReg_SegSelFirst + 5,
|
---|
387 | kIemNativeGstReg_SegBaseFirst,
|
---|
388 | kIemNativeGstReg_SegBaseLast = kIemNativeGstReg_SegBaseFirst + 5,
|
---|
389 | kIemNativeGstReg_SegLimitFirst,
|
---|
390 | kIemNativeGstReg_SegLimitLast = kIemNativeGstReg_SegLimitFirst + 5,
|
---|
391 | kIemNativeGstReg_SegAttribFirst,
|
---|
392 | kIemNativeGstReg_SegAttribLast = kIemNativeGstReg_SegAttribFirst + 5,
|
---|
393 | kIemNativeGstReg_End
|
---|
394 | } IEMNATIVEGSTREG;
|
---|
395 |
|
---|
396 | /** @name Helpers for converting register numbers to IEMNATIVEGSTREG values.
|
---|
397 | * @{ */
|
---|
398 | #define IEMNATIVEGSTREG_GPR(a_iGpr) ((IEMNATIVEGSTREG)(kIemNativeGstReg_GprFirst + (a_iGpr) ))
|
---|
399 | #define IEMNATIVEGSTREG_SEG_SEL(a_iSegReg) ((IEMNATIVEGSTREG)(kIemNativeGstReg_SegSelFirst + (a_iSegReg) ))
|
---|
400 | #define IEMNATIVEGSTREG_SEG_BASE(a_iSegReg) ((IEMNATIVEGSTREG)(kIemNativeGstReg_SegBaseFirst + (a_iSegReg) ))
|
---|
401 | #define IEMNATIVEGSTREG_SEG_LIMIT(a_iSegReg) ((IEMNATIVEGSTREG)(kIemNativeGstReg_SegLimitFirst + (a_iSegReg) ))
|
---|
402 | #define IEMNATIVEGSTREG_SEG_ATTRIB(a_iSegReg) ((IEMNATIVEGSTREG)(kIemNativeGstReg_SegAttribFirst + (a_iSegReg) ))
|
---|
403 | /** @} */
|
---|
404 |
|
---|
405 | /**
|
---|
406 | * Intended use statement for iemNativeRegAllocTmpForGuestReg().
|
---|
407 | */
|
---|
408 | typedef enum IEMNATIVEGSTREGUSE
|
---|
409 | {
|
---|
410 | /** The usage is read-only, the register holding the guest register
|
---|
411 | * shadow copy will not be modified by the caller. */
|
---|
412 | kIemNativeGstRegUse_ReadOnly = 0,
|
---|
413 | /** The caller will update the guest register (think: PC += cbInstr).
|
---|
414 | * The guest shadow copy will follow the returned register. */
|
---|
415 | kIemNativeGstRegUse_ForUpdate,
|
---|
416 | /** The call will put an entirely new value in the guest register, so
|
---|
417 | * if new register is allocate it will be returned uninitialized. */
|
---|
418 | kIemNativeGstRegUse_ForFullWrite,
|
---|
419 | /** The caller will use the guest register value as input in a calculation
|
---|
420 | * and the host register will be modified.
|
---|
421 | * This means that the returned host register will not be marked as a shadow
|
---|
422 | * copy of the guest register. */
|
---|
423 | kIemNativeGstRegUse_Calculation
|
---|
424 | } IEMNATIVEGSTREGUSE;
|
---|
425 |
|
---|
426 | /**
|
---|
427 | * Guest registers (classes) that can be referenced.
|
---|
428 | */
|
---|
429 | typedef enum IEMNATIVEGSTREGREF : uint8_t
|
---|
430 | {
|
---|
431 | kIemNativeGstRegRef_Invalid = 0,
|
---|
432 | kIemNativeGstRegRef_Gpr,
|
---|
433 | kIemNativeGstRegRef_GprHighByte, /**< AH, CH, DH, BH*/
|
---|
434 | kIemNativeGstRegRef_EFlags,
|
---|
435 | kIemNativeGstRegRef_MxCsr,
|
---|
436 | kIemNativeGstRegRef_FpuReg,
|
---|
437 | kIemNativeGstRegRef_MReg,
|
---|
438 | kIemNativeGstRegRef_XReg,
|
---|
439 | //kIemNativeGstRegRef_YReg, - doesn't work.
|
---|
440 | kIemNativeGstRegRef_End
|
---|
441 | } IEMNATIVEGSTREGREF;
|
---|
442 |
|
---|
443 |
|
---|
444 | /** Variable kinds. */
|
---|
445 | typedef enum IEMNATIVEVARKIND : uint8_t
|
---|
446 | {
|
---|
447 | /** Customary invalid zero value. */
|
---|
448 | kIemNativeVarKind_Invalid = 0,
|
---|
449 | /** This is either in a register or on the stack. */
|
---|
450 | kIemNativeVarKind_Stack,
|
---|
451 | /** Immediate value - loaded into register when needed, or can live on the
|
---|
452 | * stack if referenced (in theory). */
|
---|
453 | kIemNativeVarKind_Immediate,
|
---|
454 | /** Variable reference - loaded into register when needed, never stack. */
|
---|
455 | kIemNativeVarKind_VarRef,
|
---|
456 | /** Guest register reference - loaded into register when needed, never stack. */
|
---|
457 | kIemNativeVarKind_GstRegRef,
|
---|
458 | /** End of valid values. */
|
---|
459 | kIemNativeVarKind_End
|
---|
460 | } IEMNATIVEVARKIND;
|
---|
461 |
|
---|
462 |
|
---|
463 | /** Variable or argument. */
|
---|
464 | typedef struct IEMNATIVEVAR
|
---|
465 | {
|
---|
466 | /** The kind of variable. */
|
---|
467 | IEMNATIVEVARKIND enmKind;
|
---|
468 | /** The variable size in bytes. */
|
---|
469 | uint8_t cbVar;
|
---|
470 | /** The first stack slot (uint64_t), except for immediate and references
|
---|
471 | * where it usually is UINT8_MAX. This is allocated lazily, so if a variable
|
---|
472 | * has a stack slot it has been initialized and has a value. Unused variables
|
---|
473 | * has neither a stack slot nor a host register assignment. */
|
---|
474 | uint8_t idxStackSlot;
|
---|
475 | /** The host register allocated for the variable, UINT8_MAX if not. */
|
---|
476 | uint8_t idxReg;
|
---|
477 | /** The argument number if argument, UINT8_MAX if regular variable. */
|
---|
478 | uint8_t uArgNo;
|
---|
479 | /** If referenced, the index of the variable referencing this one, otherwise
|
---|
480 | * UINT8_MAX. A referenced variable must only be placed on the stack and
|
---|
481 | * must be either kIemNativeVarKind_Stack or kIemNativeVarKind_Immediate. */
|
---|
482 | uint8_t idxReferrerVar;
|
---|
483 | /** Guest register being shadowed here, kIemNativeGstReg_End(/UINT8_MAX) if not.
|
---|
484 | * @todo not sure what this really is for... */
|
---|
485 | IEMNATIVEGSTREG enmGstReg;
|
---|
486 | /** Set if the registered is currently used exclusively, false if the
|
---|
487 | * variable is idle and the register can be grabbed. */
|
---|
488 | bool fRegAcquired;
|
---|
489 |
|
---|
490 | union
|
---|
491 | {
|
---|
492 | /** kIemNativeVarKind_Immediate: The immediate value. */
|
---|
493 | uint64_t uValue;
|
---|
494 | /** kIemNativeVarKind_VarRef: The index of the variable being referenced. */
|
---|
495 | uint8_t idxRefVar;
|
---|
496 | /** kIemNativeVarKind_GstRegRef: The guest register being referrenced. */
|
---|
497 | struct
|
---|
498 | {
|
---|
499 | /** The class of register. */
|
---|
500 | IEMNATIVEGSTREGREF enmClass;
|
---|
501 | /** Index within the class. */
|
---|
502 | uint8_t idx;
|
---|
503 | } GstRegRef;
|
---|
504 | } u;
|
---|
505 | } IEMNATIVEVAR;
|
---|
506 |
|
---|
507 | /** What is being kept in a host register. */
|
---|
508 | typedef enum IEMNATIVEWHAT : uint8_t
|
---|
509 | {
|
---|
510 | /** The traditional invalid zero value. */
|
---|
511 | kIemNativeWhat_Invalid = 0,
|
---|
512 | /** Mapping a variable (IEMNATIVEHSTREG::idxVar). */
|
---|
513 | kIemNativeWhat_Var,
|
---|
514 | /** Temporary register, this is typically freed when a MC completes. */
|
---|
515 | kIemNativeWhat_Tmp,
|
---|
516 | /** Call argument w/o a variable mapping. This is free (via
|
---|
517 | * IEMNATIVE_CALL_VOLATILE_GREG_MASK) after the call is emitted. */
|
---|
518 | kIemNativeWhat_Arg,
|
---|
519 | /** Return status code.
|
---|
520 | * @todo not sure if we need this... */
|
---|
521 | kIemNativeWhat_rc,
|
---|
522 | /** The fixed pVCpu (PVMCPUCC) register.
|
---|
523 | * @todo consider offsetting this on amd64 to use negative offsets to access
|
---|
524 | * more members using 8-byte disp. */
|
---|
525 | kIemNativeWhat_pVCpuFixed,
|
---|
526 | /** The fixed pCtx (PCPUMCTX) register.
|
---|
527 | * @todo consider offsetting this on amd64 to use negative offsets to access
|
---|
528 | * more members using 8-byte disp. */
|
---|
529 | kIemNativeWhat_pCtxFixed,
|
---|
530 | /** Fixed temporary register. */
|
---|
531 | kIemNativeWhat_FixedTmp,
|
---|
532 | /** Register reserved by the CPU or OS architecture. */
|
---|
533 | kIemNativeWhat_FixedReserved,
|
---|
534 | /** End of valid values. */
|
---|
535 | kIemNativeWhat_End
|
---|
536 | } IEMNATIVEWHAT;
|
---|
537 |
|
---|
538 | /**
|
---|
539 | * Host general register entry.
|
---|
540 | *
|
---|
541 | * The actual allocation status is kept in IEMRECOMPILERSTATE::bmHstRegs.
|
---|
542 | *
|
---|
543 | * @todo Track immediate values in host registers similarlly to how we track the
|
---|
544 | * guest register shadow copies. For it to be real helpful, though,
|
---|
545 | * we probably need to know which will be reused and put them into
|
---|
546 | * non-volatile registers, otherwise it's going to be more or less
|
---|
547 | * restricted to an instruction or two.
|
---|
548 | */
|
---|
549 | typedef struct IEMNATIVEHSTREG
|
---|
550 | {
|
---|
551 | /** Set of guest registers this one shadows.
|
---|
552 | *
|
---|
553 | * Using a bitmap here so we can designate the same host register as a copy
|
---|
554 | * for more than one guest register. This is expected to be useful in
|
---|
555 | * situations where one value is copied to several registers in a sequence.
|
---|
556 | * If the mapping is 1:1, then we'd have to pick which side of a 'MOV SRC,DST'
|
---|
557 | * sequence we'd want to let this register follow to be a copy of and there
|
---|
558 | * will always be places where we'd be picking the wrong one.
|
---|
559 | */
|
---|
560 | uint64_t fGstRegShadows;
|
---|
561 | /** What is being kept in this register. */
|
---|
562 | IEMNATIVEWHAT enmWhat;
|
---|
563 | /** Variable index if holding a variable, otherwise UINT8_MAX. */
|
---|
564 | uint8_t idxVar;
|
---|
565 | /** Stack slot assigned by iemNativeVarSaveVolatileRegsPreHlpCall and freed
|
---|
566 | * by iemNativeVarRestoreVolatileRegsPostHlpCall. This is not valid outside
|
---|
567 | * that scope. */
|
---|
568 | uint8_t idxStackSlot;
|
---|
569 | /** Alignment padding. */
|
---|
570 | uint8_t abAlign[5];
|
---|
571 | } IEMNATIVEHSTREG;
|
---|
572 |
|
---|
573 |
|
---|
574 | /**
|
---|
575 | * Core state for the native recompiler, that is, things that needs careful
|
---|
576 | * handling when dealing with branches.
|
---|
577 | */
|
---|
578 | typedef struct IEMNATIVECORESTATE
|
---|
579 | {
|
---|
580 | /** Allocation bitmap for aHstRegs. */
|
---|
581 | uint32_t bmHstRegs;
|
---|
582 |
|
---|
583 | /** Bitmap marking which host register contains guest register shadow copies.
|
---|
584 | * This is used during register allocation to try preserve copies. */
|
---|
585 | uint32_t bmHstRegsWithGstShadow;
|
---|
586 | /** Bitmap marking valid entries in aidxGstRegShadows. */
|
---|
587 | uint64_t bmGstRegShadows;
|
---|
588 |
|
---|
589 | union
|
---|
590 | {
|
---|
591 | /** Index of variable arguments, UINT8_MAX if not valid. */
|
---|
592 | uint8_t aidxArgVars[8];
|
---|
593 | /** For more efficient resetting. */
|
---|
594 | uint64_t u64ArgVars;
|
---|
595 | };
|
---|
596 |
|
---|
597 | /** Allocation bitmap for the stack. */
|
---|
598 | uint32_t bmStack;
|
---|
599 | /** Allocation bitmap for aVars. */
|
---|
600 | uint32_t bmVars;
|
---|
601 |
|
---|
602 | /** Maps a guest register to a host GPR (index by IEMNATIVEGSTREG).
|
---|
603 | * Entries are only valid if the corresponding bit in bmGstRegShadows is set.
|
---|
604 | * (A shadow copy of a guest register can only be held in a one host register,
|
---|
605 | * there are no duplicate copies or ambiguities like that). */
|
---|
606 | uint8_t aidxGstRegShadows[kIemNativeGstReg_End];
|
---|
607 |
|
---|
608 | /** Host register allocation tracking. */
|
---|
609 | IEMNATIVEHSTREG aHstRegs[IEMNATIVE_HST_GREG_COUNT];
|
---|
610 |
|
---|
611 | /** Variables and arguments. */
|
---|
612 | IEMNATIVEVAR aVars[9];
|
---|
613 | } IEMNATIVECORESTATE;
|
---|
614 | /** Pointer to core state. */
|
---|
615 | typedef IEMNATIVECORESTATE *PIEMNATIVECORESTATE;
|
---|
616 | /** Pointer to const core state. */
|
---|
617 | typedef IEMNATIVECORESTATE const *PCIEMNATIVECORESTATE;
|
---|
618 |
|
---|
619 |
|
---|
620 | /**
|
---|
621 | * Conditional stack entry.
|
---|
622 | */
|
---|
623 | typedef struct IEMNATIVECOND
|
---|
624 | {
|
---|
625 | /** Set if we're in the "else" part, clear if we're in the "if" before it. */
|
---|
626 | bool fInElse;
|
---|
627 | /** The label for the IEM_MC_ELSE. */
|
---|
628 | uint32_t idxLabelElse;
|
---|
629 | /** The label for the IEM_MC_ENDIF. */
|
---|
630 | uint32_t idxLabelEndIf;
|
---|
631 | /** The initial state snapshot as the if-block starts executing. */
|
---|
632 | IEMNATIVECORESTATE InitialState;
|
---|
633 | /** The state snapshot at the end of the if-block. */
|
---|
634 | IEMNATIVECORESTATE IfFinalState;
|
---|
635 | } IEMNATIVECOND;
|
---|
636 | /** Pointer to a condition stack entry. */
|
---|
637 | typedef IEMNATIVECOND *PIEMNATIVECOND;
|
---|
638 |
|
---|
639 |
|
---|
640 | /**
|
---|
641 | * Native recompiler state.
|
---|
642 | */
|
---|
643 | typedef struct IEMRECOMPILERSTATE
|
---|
644 | {
|
---|
645 | /** Size of the buffer that pbNativeRecompileBufR3 points to in
|
---|
646 | * IEMNATIVEINSTR units. */
|
---|
647 | uint32_t cInstrBufAlloc;
|
---|
648 | #ifdef VBOX_STRICT
|
---|
649 | /** Strict: How far the last iemNativeInstrBufEnsure() checked. */
|
---|
650 | uint32_t offInstrBufChecked;
|
---|
651 | #else
|
---|
652 | uint32_t uPadding1; /* We don't keep track of the size here... */
|
---|
653 | #endif
|
---|
654 | /** Fixed temporary code buffer for native recompilation. */
|
---|
655 | PIEMNATIVEINSTR pInstrBuf;
|
---|
656 |
|
---|
657 | /** Bitmaps with the label types used. */
|
---|
658 | uint64_t bmLabelTypes;
|
---|
659 | /** Actual number of labels in paLabels. */
|
---|
660 | uint32_t cLabels;
|
---|
661 | /** Max number of entries allowed in paLabels before reallocating it. */
|
---|
662 | uint32_t cLabelsAlloc;
|
---|
663 | /** Labels defined while recompiling (referenced by fixups). */
|
---|
664 | PIEMNATIVELABEL paLabels;
|
---|
665 | /** Array with indexes of unique labels (uData always 0). */
|
---|
666 | uint32_t aidxUniqueLabels[kIemNativeLabelType_FirstWithMultipleInstances];
|
---|
667 |
|
---|
668 | /** Actual number of fixups paFixups. */
|
---|
669 | uint32_t cFixups;
|
---|
670 | /** Max number of entries allowed in paFixups before reallocating it. */
|
---|
671 | uint32_t cFixupsAlloc;
|
---|
672 | /** Buffer used by the recompiler for recording fixups when generating code. */
|
---|
673 | PIEMNATIVEFIXUP paFixups;
|
---|
674 |
|
---|
675 | #ifdef IEMNATIVE_WITH_TB_DEBUG_INFO
|
---|
676 | /** Number of debug info entries allocated for pDbgInfo. */
|
---|
677 | uint32_t cDbgInfoAlloc;
|
---|
678 | uint32_t uPadding;
|
---|
679 | /** Debug info. */
|
---|
680 | PIEMTBDBG pDbgInfo;
|
---|
681 | #endif
|
---|
682 |
|
---|
683 | /** The translation block being recompiled. */
|
---|
684 | PCIEMTB pTbOrg;
|
---|
685 | /** The VMCPU structure of the EMT. */
|
---|
686 | PVMCPUCC pVCpu;
|
---|
687 |
|
---|
688 | /** Condition sequence number (for generating unique labels). */
|
---|
689 | uint16_t uCondSeqNo;
|
---|
690 | /** Check IRQ seqeunce number (for generating unique labels). */
|
---|
691 | uint16_t uCheckIrqSeqNo;
|
---|
692 | /** TLB load sequence number (for generating unique labels). */
|
---|
693 | uint16_t uTlbSeqNo;
|
---|
694 | /** The current condition stack depth (aCondStack). */
|
---|
695 | uint8_t cCondDepth;
|
---|
696 |
|
---|
697 | /** The argument count + hidden regs from the IEM_MC_BEGIN statement. */
|
---|
698 | uint8_t cArgs;
|
---|
699 | /** The IEM_CIMPL_F_XXX flags from the IEM_MC_BEGIN statement. */
|
---|
700 | uint32_t fCImpl;
|
---|
701 | /** The IEM_MC_F_XXX flags from the IEM_MC_BEGIN statement. */
|
---|
702 | uint32_t fMc;
|
---|
703 | /** The expected IEMCPU::fExec value for the current call/instruction. */
|
---|
704 | uint32_t fExec;
|
---|
705 |
|
---|
706 | /** Core state requiring care with branches. */
|
---|
707 | IEMNATIVECORESTATE Core;
|
---|
708 |
|
---|
709 | /** The condition nesting stack. */
|
---|
710 | IEMNATIVECOND aCondStack[2];
|
---|
711 |
|
---|
712 | #ifndef IEM_WITH_THROW_CATCH
|
---|
713 | /** Pointer to the setjmp/longjmp buffer if we're not using C++ exceptions
|
---|
714 | * for recompilation error handling. */
|
---|
715 | jmp_buf JmpBuf;
|
---|
716 | #endif
|
---|
717 | } IEMRECOMPILERSTATE;
|
---|
718 | /** Pointer to a native recompiler state. */
|
---|
719 | typedef IEMRECOMPILERSTATE *PIEMRECOMPILERSTATE;
|
---|
720 |
|
---|
721 |
|
---|
722 | /** @def IEMNATIVE_TRY_SETJMP
|
---|
723 | * Wrapper around setjmp / try, hiding all the ugly differences.
|
---|
724 | *
|
---|
725 | * @note Use with extreme care as this is a fragile macro.
|
---|
726 | * @param a_pReNative The native recompile state.
|
---|
727 | * @param a_rcTarget The variable that should receive the status code in case
|
---|
728 | * of a longjmp/throw.
|
---|
729 | */
|
---|
730 | /** @def IEMNATIVE_CATCH_LONGJMP_BEGIN
|
---|
731 | * Start wrapper for catch / setjmp-else.
|
---|
732 | *
|
---|
733 | * This will set up a scope.
|
---|
734 | *
|
---|
735 | * @note Use with extreme care as this is a fragile macro.
|
---|
736 | * @param a_pReNative The native recompile state.
|
---|
737 | * @param a_rcTarget The variable that should receive the status code in case
|
---|
738 | * of a longjmp/throw.
|
---|
739 | */
|
---|
740 | /** @def IEMNATIVE_CATCH_LONGJMP_END
|
---|
741 | * End wrapper for catch / setjmp-else.
|
---|
742 | *
|
---|
743 | * This will close the scope set up by IEMNATIVE_CATCH_LONGJMP_BEGIN and clean
|
---|
744 | * up the state.
|
---|
745 | *
|
---|
746 | * @note Use with extreme care as this is a fragile macro.
|
---|
747 | * @param a_pReNative The native recompile state.
|
---|
748 | */
|
---|
749 | /** @def IEMNATIVE_DO_LONGJMP
|
---|
750 | *
|
---|
751 | * Wrapper around longjmp / throw.
|
---|
752 | *
|
---|
753 | * @param a_pReNative The native recompile state.
|
---|
754 | * @param a_rc The status code jump back with / throw.
|
---|
755 | */
|
---|
756 | #ifdef IEM_WITH_THROW_CATCH
|
---|
757 | # define IEMNATIVE_TRY_SETJMP(a_pReNative, a_rcTarget) \
|
---|
758 | a_rcTarget = VINF_SUCCESS; \
|
---|
759 | try
|
---|
760 | # define IEMNATIVE_CATCH_LONGJMP_BEGIN(a_pReNative, a_rcTarget) \
|
---|
761 | catch (int rcThrown) \
|
---|
762 | { \
|
---|
763 | a_rcTarget = rcThrown
|
---|
764 | # define IEMNATIVE_CATCH_LONGJMP_END(a_pReNative) \
|
---|
765 | } \
|
---|
766 | ((void)0)
|
---|
767 | # define IEMNATIVE_DO_LONGJMP(a_pReNative, a_rc) throw int(a_rc)
|
---|
768 | #else /* !IEM_WITH_THROW_CATCH */
|
---|
769 | # define IEMNATIVE_TRY_SETJMP(a_pReNative, a_rcTarget) \
|
---|
770 | if ((a_rcTarget = setjmp((a_pReNative)->JmpBuf)) == 0)
|
---|
771 | # define IEMNATIVE_CATCH_LONGJMP_BEGIN(a_pReNative, a_rcTarget) \
|
---|
772 | else \
|
---|
773 | { \
|
---|
774 | ((void)0)
|
---|
775 | # define IEMNATIVE_CATCH_LONGJMP_END(a_pReNative) \
|
---|
776 | }
|
---|
777 | # define IEMNATIVE_DO_LONGJMP(a_pReNative, a_rc) longjmp((a_pReNative)->JmpBuf, (a_rc))
|
---|
778 | #endif /* !IEM_WITH_THROW_CATCH */
|
---|
779 |
|
---|
780 |
|
---|
781 | /**
|
---|
782 | * Native recompiler worker for a threaded function.
|
---|
783 | *
|
---|
784 | * @returns New code buffer offset; throws VBox status code in case of a failure.
|
---|
785 | * @param pReNative The native recompiler state.
|
---|
786 | * @param off The current code buffer offset.
|
---|
787 | * @param pCallEntry The threaded call entry.
|
---|
788 | *
|
---|
789 | * @note This may throw/longjmp VBox status codes (int) to abort compilation, so no RT_NOEXCEPT!
|
---|
790 | */
|
---|
791 | typedef uint32_t (VBOXCALL FNIEMNATIVERECOMPFUNC)(PIEMRECOMPILERSTATE pReNative, uint32_t off, PCIEMTHRDEDCALLENTRY pCallEntry);
|
---|
792 | /** Pointer to a native recompiler worker for a threaded function. */
|
---|
793 | typedef FNIEMNATIVERECOMPFUNC *PFNIEMNATIVERECOMPFUNC;
|
---|
794 |
|
---|
795 | /** Defines a native recompiler worker for a threaded function.
|
---|
796 | * @see FNIEMNATIVERECOMPFUNC */
|
---|
797 | #define IEM_DECL_IEMNATIVERECOMPFUNC_DEF(a_Name) \
|
---|
798 | uint32_t VBOXCALL a_Name(PIEMRECOMPILERSTATE pReNative, uint32_t off, PCIEMTHRDEDCALLENTRY pCallEntry)
|
---|
799 |
|
---|
800 | /** Prototypes a native recompiler function for a threaded function.
|
---|
801 | * @see FNIEMNATIVERECOMPFUNC */
|
---|
802 | #define IEM_DECL_IEMNATIVERECOMPFUNC_PROTO(a_Name) FNIEMNATIVERECOMPFUNC a_Name
|
---|
803 |
|
---|
804 |
|
---|
805 | /** Define a native recompiler helper function, safe to call from the TB code. */
|
---|
806 | #define IEM_DECL_NATIVE_HLP_DEF(a_RetType, a_Name, a_ArgList) \
|
---|
807 | DECL_HIDDEN_THROW(a_RetType) VBOXCALL a_Name a_ArgList
|
---|
808 | /** Prototype a native recompiler helper function, safe to call from the TB code. */
|
---|
809 | #define IEM_DECL_NATIVE_HLP_PROTO(a_RetType, a_Name, a_ArgList) \
|
---|
810 | DECL_HIDDEN_THROW(a_RetType) VBOXCALL a_Name a_ArgList
|
---|
811 |
|
---|
812 |
|
---|
813 | DECL_HIDDEN_THROW(uint32_t) iemNativeLabelCreate(PIEMRECOMPILERSTATE pReNative, IEMNATIVELABELTYPE enmType,
|
---|
814 | uint32_t offWhere = UINT32_MAX, uint16_t uData = 0);
|
---|
815 | DECL_HIDDEN_THROW(void) iemNativeLabelDefine(PIEMRECOMPILERSTATE pReNative, uint32_t idxLabel, uint32_t offWhere);
|
---|
816 | DECL_HIDDEN_THROW(void) iemNativeAddFixup(PIEMRECOMPILERSTATE pReNative, uint32_t offWhere, uint32_t idxLabel,
|
---|
817 | IEMNATIVEFIXUPTYPE enmType, int8_t offAddend = 0);
|
---|
818 | DECL_HIDDEN_THROW(PIEMNATIVEINSTR) iemNativeInstrBufEnsureSlow(PIEMRECOMPILERSTATE pReNative, uint32_t off, uint32_t cInstrReq);
|
---|
819 |
|
---|
820 | DECL_HIDDEN_THROW(uint8_t) iemNativeRegAllocTmp(PIEMRECOMPILERSTATE pReNative, uint32_t *poff, bool fPreferVolatile = true);
|
---|
821 | DECL_HIDDEN_THROW(uint8_t) iemNativeRegAllocTmpEx(PIEMRECOMPILERSTATE pReNative, uint32_t *poff, uint32_t fRegMask,
|
---|
822 | bool fPreferVolatile = true);
|
---|
823 | DECL_HIDDEN_THROW(uint8_t) iemNativeRegAllocTmpImm(PIEMRECOMPILERSTATE pReNative, uint32_t *poff, uint64_t uImm,
|
---|
824 | bool fPreferVolatile = true);
|
---|
825 | DECL_HIDDEN_THROW(uint8_t) iemNativeRegAllocTmpForGuestReg(PIEMRECOMPILERSTATE pReNative, uint32_t *poff,
|
---|
826 | IEMNATIVEGSTREG enmGstReg,
|
---|
827 | IEMNATIVEGSTREGUSE enmIntendedUse = kIemNativeGstRegUse_ReadOnly,
|
---|
828 | bool fNoVolatileRegs = false);
|
---|
829 | DECL_HIDDEN_THROW(uint8_t) iemNativeRegAllocTmpForGuestRegIfAlreadyPresent(PIEMRECOMPILERSTATE pReNative, uint32_t *poff,
|
---|
830 | IEMNATIVEGSTREG enmGstReg);
|
---|
831 |
|
---|
832 | DECL_HIDDEN_THROW(uint8_t) iemNativeRegAllocVar(PIEMRECOMPILERSTATE pReNative, uint32_t *poff, uint8_t idxVar);
|
---|
833 | DECL_HIDDEN_THROW(uint32_t) iemNativeRegAllocArgs(PIEMRECOMPILERSTATE pReNative, uint32_t off, uint8_t cArgs);
|
---|
834 | DECL_HIDDEN_THROW(uint8_t) iemNativeRegAssignRc(PIEMRECOMPILERSTATE pReNative, uint8_t idxHstReg);
|
---|
835 | DECLHIDDEN(void) iemNativeRegFree(PIEMRECOMPILERSTATE pReNative, uint8_t idxHstReg) RT_NOEXCEPT;
|
---|
836 | DECLHIDDEN(void) iemNativeRegFreeTmp(PIEMRECOMPILERSTATE pReNative, uint8_t idxHstReg) RT_NOEXCEPT;
|
---|
837 | DECLHIDDEN(void) iemNativeRegFreeTmpImm(PIEMRECOMPILERSTATE pReNative, uint8_t idxHstReg) RT_NOEXCEPT;
|
---|
838 | DECLHIDDEN(void) iemNativeRegFreeVar(PIEMRECOMPILERSTATE pReNative, uint8_t idxHstReg, bool fFlushShadows) RT_NOEXCEPT;
|
---|
839 | DECLHIDDEN(void) iemNativeRegFreeAndFlushMask(PIEMRECOMPILERSTATE pReNative, uint32_t fHstRegMask) RT_NOEXCEPT;
|
---|
840 | DECL_HIDDEN_THROW(uint32_t) iemNativeRegFlushPendingWrites(PIEMRECOMPILERSTATE pReNative, uint32_t off);
|
---|
841 | DECL_HIDDEN_THROW(uint32_t) iemNativeRegMoveAndFreeAndFlushAtCall(PIEMRECOMPILERSTATE pReNative, uint32_t off, uint8_t cArgs,
|
---|
842 | uint32_t fKeepVars = 0);
|
---|
843 | DECLHIDDEN(void) iemNativeRegFlushGuestShadows(PIEMRECOMPILERSTATE pReNative, uint64_t fGstRegs) RT_NOEXCEPT;
|
---|
844 | DECLHIDDEN(void) iemNativeRegFlushGuestShadowsByHostMask(PIEMRECOMPILERSTATE pReNative, uint32_t fHstRegs) RT_NOEXCEPT;
|
---|
845 | DECL_HIDDEN_THROW(uint32_t) iemNativeRegRestoreGuestShadowsInVolatileRegs(PIEMRECOMPILERSTATE pReNative, uint32_t off,
|
---|
846 | uint32_t fHstRegsActiveShadows);
|
---|
847 |
|
---|
848 | DECL_HIDDEN_THROW(uint8_t) iemNativeVarGetStackSlot(PIEMRECOMPILERSTATE pReNative, uint8_t idxVar);
|
---|
849 | DECL_HIDDEN_THROW(uint8_t) iemNativeVarRegisterAcquire(PIEMRECOMPILERSTATE pReNative, uint8_t idxVar, uint32_t *poff,
|
---|
850 | bool fInitialized = false, uint8_t idxRegPref = UINT8_MAX);
|
---|
851 | DECL_HIDDEN_THROW(uint8_t) iemNativeVarRegisterAcquireForGuestReg(PIEMRECOMPILERSTATE pReNative, uint8_t idxVar,
|
---|
852 | IEMNATIVEGSTREG enmGstReg, uint32_t *poff);
|
---|
853 | DECL_HIDDEN_THROW(uint32_t) iemNativeVarSaveVolatileRegsPreHlpCall(PIEMRECOMPILERSTATE pReNative, uint32_t off,
|
---|
854 | uint32_t fHstRegsNotToSave);
|
---|
855 | DECL_HIDDEN_THROW(uint32_t) iemNativeVarRestoreVolatileRegsPostHlpCall(PIEMRECOMPILERSTATE pReNative, uint32_t off,
|
---|
856 | uint32_t fHstRegsNotToSave);
|
---|
857 |
|
---|
858 | DECL_HIDDEN_THROW(uint32_t) iemNativeEmitLoadGprWithGstShadowReg(PIEMRECOMPILERSTATE pReNative, uint32_t off,
|
---|
859 | uint8_t idxHstReg, IEMNATIVEGSTREG enmGstReg);
|
---|
860 | DECL_HIDDEN_THROW(uint32_t) iemNativeEmitCheckCallRetAndPassUp(PIEMRECOMPILERSTATE pReNative, uint32_t off, uint8_t idxInstr);
|
---|
861 | DECL_HIDDEN_THROW(uint32_t) iemNativeEmitCImplCall(PIEMRECOMPILERSTATE pReNative, uint32_t off, uint8_t idxInstr,
|
---|
862 | uint64_t fGstShwFlush, uintptr_t pfnCImpl, uint8_t cbInstr, uint8_t cAddParams,
|
---|
863 | uint64_t uParam0, uint64_t uParam1, uint64_t uParam2);
|
---|
864 | DECL_HIDDEN_THROW(uint32_t) iemNativeEmitThreadedCall(PIEMRECOMPILERSTATE pReNative, uint32_t off,
|
---|
865 | PCIEMTHRDEDCALLENTRY pCallEntry);
|
---|
866 |
|
---|
867 | IEM_DECL_IEMNATIVERECOMPFUNC_PROTO(iemNativeRecompFunc_BltIn_Nop);
|
---|
868 | IEM_DECL_IEMNATIVERECOMPFUNC_PROTO(iemNativeRecompFunc_BltIn_LogCpuState);
|
---|
869 | IEM_DECL_IEMNATIVERECOMPFUNC_PROTO(iemNativeRecompFunc_BltIn_DeferToCImpl0);
|
---|
870 | IEM_DECL_IEMNATIVERECOMPFUNC_PROTO(iemNativeRecompFunc_BltIn_CheckIrq);
|
---|
871 | IEM_DECL_IEMNATIVERECOMPFUNC_PROTO(iemNativeRecompFunc_BltIn_CheckMode);
|
---|
872 | IEM_DECL_IEMNATIVERECOMPFUNC_PROTO(iemNativeRecompFunc_BltIn_CheckCsLim);
|
---|
873 | IEM_DECL_IEMNATIVERECOMPFUNC_PROTO(iemNativeRecompFunc_BltIn_CheckCsLimAndOpcodes);
|
---|
874 | IEM_DECL_IEMNATIVERECOMPFUNC_PROTO(iemNativeRecompFunc_BltIn_CheckOpcodes);
|
---|
875 | IEM_DECL_IEMNATIVERECOMPFUNC_PROTO(iemNativeRecompFunc_BltIn_CheckOpcodesConsiderCsLim);
|
---|
876 | IEM_DECL_IEMNATIVERECOMPFUNC_PROTO(iemNativeRecompFunc_BltIn_CheckCsLimAndPcAndOpcodes);
|
---|
877 | IEM_DECL_IEMNATIVERECOMPFUNC_PROTO(iemNativeRecompFunc_BltIn_CheckPcAndOpcodes);
|
---|
878 | IEM_DECL_IEMNATIVERECOMPFUNC_PROTO(iemNativeRecompFunc_BltIn_CheckPcAndOpcodesConsiderCsLim);
|
---|
879 | IEM_DECL_IEMNATIVERECOMPFUNC_PROTO(iemNativeRecompFunc_BltIn_CheckCsLimAndOpcodesLoadingTlb);
|
---|
880 | IEM_DECL_IEMNATIVERECOMPFUNC_PROTO(iemNativeRecompFunc_BltIn_CheckOpcodesLoadingTlb);
|
---|
881 | IEM_DECL_IEMNATIVERECOMPFUNC_PROTO(iemNativeRecompFunc_BltIn_CheckOpcodesLoadingTlbConsiderCsLim);
|
---|
882 | IEM_DECL_IEMNATIVERECOMPFUNC_PROTO(iemNativeRecompFunc_BltIn_CheckCsLimAndOpcodesAcrossPageLoadingTlb);
|
---|
883 | IEM_DECL_IEMNATIVERECOMPFUNC_PROTO(iemNativeRecompFunc_BltIn_CheckOpcodesAcrossPageLoadingTlb);
|
---|
884 | IEM_DECL_IEMNATIVERECOMPFUNC_PROTO(iemNativeRecompFunc_BltIn_CheckOpcodesAcrossPageLoadingTlbConsiderCsLim);
|
---|
885 | IEM_DECL_IEMNATIVERECOMPFUNC_PROTO(iemNativeRecompFunc_BltIn_CheckCsLimAndOpcodesOnNextPageLoadingTlb);
|
---|
886 | IEM_DECL_IEMNATIVERECOMPFUNC_PROTO(iemNativeRecompFunc_BltIn_CheckOpcodesOnNextPageLoadingTlb);
|
---|
887 | IEM_DECL_IEMNATIVERECOMPFUNC_PROTO(iemNativeRecompFunc_BltIn_CheckOpcodesOnNextPageLoadingTlbConsiderCsLim);
|
---|
888 | IEM_DECL_IEMNATIVERECOMPFUNC_PROTO(iemNativeRecompFunc_BltIn_CheckCsLimAndOpcodesOnNewPageLoadingTlb);
|
---|
889 | IEM_DECL_IEMNATIVERECOMPFUNC_PROTO(iemNativeRecompFunc_BltIn_CheckOpcodesOnNewPageLoadingTlb);
|
---|
890 | IEM_DECL_IEMNATIVERECOMPFUNC_PROTO(iemNativeRecompFunc_BltIn_CheckOpcodesOnNewPageLoadingTlbConsiderCsLim);
|
---|
891 |
|
---|
892 | extern DECL_HIDDEN_DATA(const char * const) g_apszIemNativeHstRegNames[];
|
---|
893 |
|
---|
894 |
|
---|
895 | /**
|
---|
896 | * Ensures that there is sufficient space in the instruction output buffer.
|
---|
897 | *
|
---|
898 | * This will reallocate the buffer if needed and allowed.
|
---|
899 | *
|
---|
900 | * @note Always use IEMNATIVE_ASSERT_INSTR_BUF_ENSURE when done to check the
|
---|
901 | * allocation size.
|
---|
902 | *
|
---|
903 | * @returns Pointer to the instruction output buffer on success; throws VBox
|
---|
904 | * status code on failure, so no need to check it.
|
---|
905 | * @param pReNative The native recompile state.
|
---|
906 | * @param off Current instruction offset. Works safely for UINT32_MAX
|
---|
907 | * as well.
|
---|
908 | * @param cInstrReq Number of instruction about to be added. It's okay to
|
---|
909 | * overestimate this a bit.
|
---|
910 | */
|
---|
911 | DECL_FORCE_INLINE_THROW(PIEMNATIVEINSTR)
|
---|
912 | iemNativeInstrBufEnsure(PIEMRECOMPILERSTATE pReNative, uint32_t off, uint32_t cInstrReq)
|
---|
913 | {
|
---|
914 | uint64_t const offChecked = off + (uint64_t)cInstrReq; /** @todo may reconsider the need for UINT32_MAX safety... */
|
---|
915 | if (RT_LIKELY(offChecked <= pReNative->cInstrBufAlloc))
|
---|
916 | {
|
---|
917 | #ifdef VBOX_STRICT
|
---|
918 | pReNative->offInstrBufChecked = offChecked;
|
---|
919 | #endif
|
---|
920 | return pReNative->pInstrBuf;
|
---|
921 | }
|
---|
922 | return iemNativeInstrBufEnsureSlow(pReNative, off, cInstrReq);
|
---|
923 | }
|
---|
924 |
|
---|
925 | /**
|
---|
926 | * Checks that we didn't exceed the space requested in the last
|
---|
927 | * iemNativeInstrBufEnsure() call.
|
---|
928 | */
|
---|
929 | #define IEMNATIVE_ASSERT_INSTR_BUF_ENSURE(a_pReNative, a_off) \
|
---|
930 | AssertMsg((a_off) <= (a_pReNative)->offInstrBufChecked, \
|
---|
931 | ("off=%#x offInstrBufChecked=%#x\n", (a_off), (a_pReNative)->offInstrBufChecked))
|
---|
932 |
|
---|
933 | /**
|
---|
934 | * Checks that a variable index is valid.
|
---|
935 | */
|
---|
936 | #define IEMNATIVE_ASSERT_VAR_IDX(a_pReNative, a_idxVar) \
|
---|
937 | AssertMsg( (unsigned)(a_idxVar) < RT_ELEMENTS((a_pReNative)->Core.aVars) \
|
---|
938 | && ((a_pReNative)->Core.bmVars & RT_BIT_32(a_idxVar)), ("%s=%d\n", #a_idxVar, a_idxVar))
|
---|
939 |
|
---|
940 | /**
|
---|
941 | * Checks that a variable index is valid and that the variable is assigned the
|
---|
942 | * correct argument number.
|
---|
943 | * This also adds a RT_NOREF of a_idxVar.
|
---|
944 | */
|
---|
945 | #define IEMNATIVE_ASSERT_ARG_VAR_IDX(a_pReNative, a_idxVar, a_uArgNo) do { \
|
---|
946 | RT_NOREF_PV(a_idxVar); \
|
---|
947 | AssertMsg( (unsigned)(a_idxVar) < RT_ELEMENTS((a_pReNative)->Core.aVars) \
|
---|
948 | && ((a_pReNative)->Core.bmVars & RT_BIT_32(a_idxVar))\
|
---|
949 | && (a_pReNative)->Core.aVars[a_idxVar].uArgNo == (a_uArgNo) \
|
---|
950 | , ("%s=%d; uArgNo=%d, expected %u\n", #a_idxVar, a_idxVar, \
|
---|
951 | (a_pReNative)->Core.aVars[RT_MAX(a_idxVar, RT_ELEMENTS((a_pReNative)->Core.aVars)) - 1].uArgNo, a_uArgNo)); \
|
---|
952 | } while (0)
|
---|
953 |
|
---|
954 | /**
|
---|
955 | * Calculates the stack address of a variable as a [r]BP displacement value.
|
---|
956 | */
|
---|
957 | DECL_FORCE_INLINE(int32_t)
|
---|
958 | iemNativeStackCalcBpDisp(uint8_t idxStackSlot)
|
---|
959 | {
|
---|
960 | Assert(idxStackSlot < IEMNATIVE_FRAME_VAR_SLOTS);
|
---|
961 | return idxStackSlot * sizeof(uint64_t) + IEMNATIVE_FP_OFF_STACK_VARS;
|
---|
962 | }
|
---|
963 |
|
---|
964 |
|
---|
965 | /**
|
---|
966 | * Releases the variable's register.
|
---|
967 | *
|
---|
968 | * The register must have been previously acquired calling
|
---|
969 | * iemNativeVarRegisterAcquire(), iemNativeVarRegisterAcquireForGuestReg() or
|
---|
970 | * iemNativeVarRegisterSetAndAcquire().
|
---|
971 | */
|
---|
972 | DECL_INLINE_THROW(void) iemNativeVarRegisterRelease(PIEMRECOMPILERSTATE pReNative, uint8_t idxVar)
|
---|
973 | {
|
---|
974 | IEMNATIVE_ASSERT_VAR_IDX(pReNative, idxVar);
|
---|
975 | Assert(pReNative->Core.aVars[idxVar].fRegAcquired);
|
---|
976 | pReNative->Core.aVars[idxVar].fRegAcquired = false;
|
---|
977 | }
|
---|
978 |
|
---|
979 | /** @} */
|
---|
980 |
|
---|
981 | #endif /* !VMM_INCLUDED_SRC_include_IEMN8veRecompiler_h */
|
---|
982 |
|
---|