VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/NEMR3Native-darwin-armv8.cpp@ 99735

Last change on this file since 99735 was 99735, checked in by vboxsync, 2 years ago

VMM/NEMR3Native-darwin-armv8: Set and clear the PPI interrupt of the vTimer if activated, bugref:10390

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 62.1 KB
Line 
1/* $Id: NEMR3Native-darwin-armv8.cpp 99735 2023-05-10 17:29:47Z vboxsync $ */
2/** @file
3 * NEM - Native execution manager, native ring-3 macOS backend using Hypervisor.framework, ARMv8 variant.
4 *
5 * Log group 2: Exit logging.
6 * Log group 3: Log context on exit.
7 * Log group 5: Ring-3 memory management
8 */
9
10/*
11 * Copyright (C) 2023 Oracle and/or its affiliates.
12 *
13 * This file is part of VirtualBox base platform packages, as
14 * available from https://www.virtualbox.org.
15 *
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation, in version 3 of the
19 * License.
20 *
21 * This program is distributed in the hope that it will be useful, but
22 * WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, see <https://www.gnu.org/licenses>.
28 *
29 * SPDX-License-Identifier: GPL-3.0-only
30 */
31
32
33/*********************************************************************************************************************************
34* Header Files *
35*********************************************************************************************************************************/
36#define LOG_GROUP LOG_GROUP_NEM
37#define VMCPU_INCL_CPUM_GST_CTX
38#define CPUM_WITH_NONCONST_HOST_FEATURES /* required for initializing parts of the g_CpumHostFeatures structure here. */
39#include <VBox/vmm/nem.h>
40#include <VBox/vmm/iem.h>
41#include <VBox/vmm/em.h>
42#include <VBox/vmm/gic.h>
43#include <VBox/vmm/pdm.h>
44#include <VBox/vmm/dbgftrace.h>
45#include <VBox/vmm/gcm.h>
46#include "NEMInternal.h"
47#include <VBox/vmm/vmcc.h>
48#include "dtrace/VBoxVMM.h"
49
50#include <iprt/armv8.h>
51#include <iprt/asm.h>
52#include <iprt/ldr.h>
53#include <iprt/mem.h>
54#include <iprt/path.h>
55#include <iprt/string.h>
56#include <iprt/system.h>
57#include <iprt/utf16.h>
58
59#include <mach/mach_time.h>
60#include <mach/kern_return.h>
61
62#include <Hypervisor/Hypervisor.h>
63
64
65/*********************************************************************************************************************************
66* Defined Constants And Macros *
67*********************************************************************************************************************************/
68
69
70/** @todo The vTimer PPI for the virt platform, make it configurable. */
71#define NEM_DARWIN_VTIMER_GIC_PPI_IRQ 11
72
73
74/*********************************************************************************************************************************
75* Structures and Typedefs *
76*********************************************************************************************************************************/
77
78
79/*********************************************************************************************************************************
80* Global Variables *
81*********************************************************************************************************************************/
82/** NEM_DARWIN_PAGE_STATE_XXX names. */
83NEM_TMPL_STATIC const char * const g_apszPageStates[4] = { "not-set", "unmapped", "readable", "writable" };
84/** The general registers. */
85static const struct
86{
87 hv_reg_t enmHvReg;
88 uint32_t fCpumExtrn;
89 uint32_t offCpumCtx;
90} s_aCpumRegs[] =
91{
92#define CPUM_GREG_EMIT_X0_X3(a_Idx) { HV_REG_X ## a_Idx, CPUMCTX_EXTRN_X ## a_Idx, RT_UOFFSETOF(CPUMCTX, aGRegs[a_Idx].x) }
93#define CPUM_GREG_EMIT_X4_X28(a_Idx) { HV_REG_X ## a_Idx, CPUMCTX_EXTRN_X4_X28, RT_UOFFSETOF(CPUMCTX, aGRegs[a_Idx].x) }
94 CPUM_GREG_EMIT_X0_X3(0),
95 CPUM_GREG_EMIT_X0_X3(1),
96 CPUM_GREG_EMIT_X0_X3(2),
97 CPUM_GREG_EMIT_X0_X3(3),
98 CPUM_GREG_EMIT_X4_X28(4),
99 CPUM_GREG_EMIT_X4_X28(5),
100 CPUM_GREG_EMIT_X4_X28(6),
101 CPUM_GREG_EMIT_X4_X28(7),
102 CPUM_GREG_EMIT_X4_X28(8),
103 CPUM_GREG_EMIT_X4_X28(9),
104 CPUM_GREG_EMIT_X4_X28(10),
105 CPUM_GREG_EMIT_X4_X28(11),
106 CPUM_GREG_EMIT_X4_X28(12),
107 CPUM_GREG_EMIT_X4_X28(13),
108 CPUM_GREG_EMIT_X4_X28(14),
109 CPUM_GREG_EMIT_X4_X28(15),
110 CPUM_GREG_EMIT_X4_X28(16),
111 CPUM_GREG_EMIT_X4_X28(17),
112 CPUM_GREG_EMIT_X4_X28(18),
113 CPUM_GREG_EMIT_X4_X28(19),
114 CPUM_GREG_EMIT_X4_X28(20),
115 CPUM_GREG_EMIT_X4_X28(21),
116 CPUM_GREG_EMIT_X4_X28(22),
117 CPUM_GREG_EMIT_X4_X28(23),
118 CPUM_GREG_EMIT_X4_X28(24),
119 CPUM_GREG_EMIT_X4_X28(25),
120 CPUM_GREG_EMIT_X4_X28(26),
121 CPUM_GREG_EMIT_X4_X28(27),
122 CPUM_GREG_EMIT_X4_X28(28),
123 { HV_REG_FP, CPUMCTX_EXTRN_FP, RT_UOFFSETOF(CPUMCTX, aGRegs[29].x) },
124 { HV_REG_LR, CPUMCTX_EXTRN_LR, RT_UOFFSETOF(CPUMCTX, aGRegs[30].x) },
125 { HV_REG_PC, CPUMCTX_EXTRN_PC, RT_UOFFSETOF(CPUMCTX, Pc.u64) },
126 { HV_REG_FPCR, CPUMCTX_EXTRN_FPCR, RT_UOFFSETOF(CPUMCTX, fpcr) },
127 { HV_REG_FPSR, CPUMCTX_EXTRN_FPSR, RT_UOFFSETOF(CPUMCTX, fpsr) }
128#undef CPUM_GREG_EMIT_X0_X3
129#undef CPUM_GREG_EMIT_X4_X28
130};
131/** SIMD/FP registers. */
132static const struct
133{
134 hv_simd_fp_reg_t enmHvReg;
135 uint32_t offCpumCtx;
136} s_aCpumFpRegs[] =
137{
138#define CPUM_VREG_EMIT(a_Idx) { HV_SIMD_FP_REG_Q ## a_Idx, RT_UOFFSETOF(CPUMCTX, aVRegs[a_Idx].v) }
139 CPUM_VREG_EMIT(0),
140 CPUM_VREG_EMIT(1),
141 CPUM_VREG_EMIT(2),
142 CPUM_VREG_EMIT(3),
143 CPUM_VREG_EMIT(4),
144 CPUM_VREG_EMIT(5),
145 CPUM_VREG_EMIT(6),
146 CPUM_VREG_EMIT(7),
147 CPUM_VREG_EMIT(8),
148 CPUM_VREG_EMIT(9),
149 CPUM_VREG_EMIT(10),
150 CPUM_VREG_EMIT(11),
151 CPUM_VREG_EMIT(12),
152 CPUM_VREG_EMIT(13),
153 CPUM_VREG_EMIT(14),
154 CPUM_VREG_EMIT(15),
155 CPUM_VREG_EMIT(16),
156 CPUM_VREG_EMIT(17),
157 CPUM_VREG_EMIT(18),
158 CPUM_VREG_EMIT(19),
159 CPUM_VREG_EMIT(20),
160 CPUM_VREG_EMIT(21),
161 CPUM_VREG_EMIT(22),
162 CPUM_VREG_EMIT(23),
163 CPUM_VREG_EMIT(24),
164 CPUM_VREG_EMIT(25),
165 CPUM_VREG_EMIT(26),
166 CPUM_VREG_EMIT(27),
167 CPUM_VREG_EMIT(28),
168 CPUM_VREG_EMIT(29),
169 CPUM_VREG_EMIT(30),
170 CPUM_VREG_EMIT(31)
171#undef CPUM_VREG_EMIT
172};
173/** System registers. */
174static const struct
175{
176 hv_sys_reg_t enmHvReg;
177 uint32_t fCpumExtrn;
178 uint32_t offCpumCtx;
179} s_aCpumSysRegs[] =
180{
181 { HV_SYS_REG_SP_EL0, CPUMCTX_EXTRN_SP, RT_UOFFSETOF(CPUMCTX, aSpReg[0].u64) },
182 { HV_SYS_REG_SP_EL1, CPUMCTX_EXTRN_SP, RT_UOFFSETOF(CPUMCTX, aSpReg[1].u64) },
183 { HV_SYS_REG_SPSR_EL1, CPUMCTX_EXTRN_SPSR, RT_UOFFSETOF(CPUMCTX, Spsr.u64) },
184 { HV_SYS_REG_ELR_EL1, CPUMCTX_EXTRN_ELR, RT_UOFFSETOF(CPUMCTX, Elr.u64) },
185 { HV_SYS_REG_SCTLR_EL1, CPUMCTX_EXTRN_SCTLR_TCR_TTBR, RT_UOFFSETOF(CPUMCTX, Sctlr.u64) },
186 { HV_SYS_REG_TCR_EL1, CPUMCTX_EXTRN_SCTLR_TCR_TTBR, RT_UOFFSETOF(CPUMCTX, Tcr.u64) },
187 { HV_SYS_REG_TTBR0_EL1, CPUMCTX_EXTRN_SCTLR_TCR_TTBR, RT_UOFFSETOF(CPUMCTX, Ttbr0.u64) },
188 { HV_SYS_REG_TTBR1_EL1, CPUMCTX_EXTRN_SCTLR_TCR_TTBR, RT_UOFFSETOF(CPUMCTX, Ttbr1.u64) },
189};
190
191
192/*********************************************************************************************************************************
193* Internal Functions *
194*********************************************************************************************************************************/
195
196
197/**
198 * Converts a HV return code to a VBox status code.
199 *
200 * @returns VBox status code.
201 * @param hrc The HV return code to convert.
202 */
203DECLINLINE(int) nemR3DarwinHvSts2Rc(hv_return_t hrc)
204{
205 if (hrc == HV_SUCCESS)
206 return VINF_SUCCESS;
207
208 switch (hrc)
209 {
210 case HV_ERROR: return VERR_INVALID_STATE;
211 case HV_BUSY: return VERR_RESOURCE_BUSY;
212 case HV_BAD_ARGUMENT: return VERR_INVALID_PARAMETER;
213 case HV_NO_RESOURCES: return VERR_OUT_OF_RESOURCES;
214 case HV_NO_DEVICE: return VERR_NOT_FOUND;
215 case HV_UNSUPPORTED: return VERR_NOT_SUPPORTED;
216 }
217
218 return VERR_IPE_UNEXPECTED_STATUS;
219}
220
221
222/**
223 * Returns a human readable string of the given exception class.
224 *
225 * @returns Pointer to the string matching the given EC.
226 * @param u32Ec The exception class to return the string for.
227 */
228static const char *nemR3DarwinEsrEl2EcStringify(uint32_t u32Ec)
229{
230 switch (u32Ec)
231 {
232#define ARMV8_EC_CASE(a_Ec) case a_Ec: return #a_Ec
233 ARMV8_EC_CASE(ARMV8_ESR_EL2_EC_UNKNOWN);
234 ARMV8_EC_CASE(ARMV8_ESR_EL2_EC_TRAPPED_WFX);
235 ARMV8_EC_CASE(ARMV8_ESR_EL2_EC_AARCH32_TRAPPED_MCR_MRC_COPROC_15);
236 ARMV8_EC_CASE(ARMV8_ESR_EL2_EC_AARCH32_TRAPPED_MCRR_MRRC_COPROC15);
237 ARMV8_EC_CASE(ARMV8_ESR_EL2_EC_AARCH32_TRAPPED_MCR_MRC_COPROC_14);
238 ARMV8_EC_CASE(ARMV8_ESR_EL2_EC_AARCH32_TRAPPED_LDC_STC);
239 ARMV8_EC_CASE(ARMV8_ESR_EL2_EC_AARCH32_TRAPPED_SME_SVE_NEON);
240 ARMV8_EC_CASE(ARMV8_ESR_EL2_EC_AARCH32_TRAPPED_VMRS);
241 ARMV8_EC_CASE(ARMV8_ESR_EL2_EC_AARCH32_TRAPPED_PA_INSN);
242 ARMV8_EC_CASE(ARMV8_ESR_EL2_EC_LS64_EXCEPTION);
243 ARMV8_EC_CASE(ARMV8_ESR_EL2_EC_AARCH32_TRAPPED_MRRC_COPROC14);
244 ARMV8_EC_CASE(ARMV8_ESR_EL2_EC_BTI_BRANCH_TARGET_EXCEPTION);
245 ARMV8_EC_CASE(ARMV8_ESR_EL2_ILLEGAL_EXECUTION_STATE);
246 ARMV8_EC_CASE(ARMV8_ESR_EL2_EC_AARCH32_SVC_INSN);
247 ARMV8_EC_CASE(ARMV8_ESR_EL2_EC_AARCH32_HVC_INSN);
248 ARMV8_EC_CASE(ARMV8_ESR_EL2_EC_AARCH32_SMC_INSN);
249 ARMV8_EC_CASE(ARMV8_ESR_EL2_EC_AARCH64_SVC_INSN);
250 ARMV8_EC_CASE(ARMV8_ESR_EL2_EC_AARCH64_HVC_INSN);
251 ARMV8_EC_CASE(ARMV8_ESR_EL2_EC_AARCH64_SMC_INSN);
252 ARMV8_EC_CASE(ARMV8_ESR_EL2_EC_AARCH64_TRAPPED_SYS_INSN);
253 ARMV8_EC_CASE(ARMV8_ESR_EL2_EC_SVE_TRAPPED);
254 ARMV8_EC_CASE(ARMV8_ESR_EL2_EC_PAUTH_NV_TRAPPED_ERET_ERETAA_ERETAB);
255 ARMV8_EC_CASE(ARMV8_ESR_EL2_EC_TME_TSTART_INSN_EXCEPTION);
256 ARMV8_EC_CASE(ARMV8_ESR_EL2_EC_FPAC_PA_INSN_FAILURE_EXCEPTION);
257 ARMV8_EC_CASE(ARMV8_ESR_EL2_EC_SME_TRAPPED_SME_ACCESS);
258 ARMV8_EC_CASE(ARMV8_ESR_EL2_EC_RME_GRANULE_PROT_CHECK_EXCEPTION);
259 ARMV8_EC_CASE(ARMV8_ESR_EL2_INSN_ABORT_FROM_LOWER_EL);
260 ARMV8_EC_CASE(ARMV8_ESR_EL2_INSN_ABORT_FROM_EL2);
261 ARMV8_EC_CASE(ARMV8_ESR_EL2_PC_ALIGNMENT_EXCEPTION);
262 ARMV8_EC_CASE(ARMV8_ESR_EL2_DATA_ABORT_FROM_LOWER_EL);
263 ARMV8_EC_CASE(ARMV8_ESR_EL2_DATA_ABORT_FROM_EL2);
264 ARMV8_EC_CASE(ARMV8_ESR_EL2_SP_ALIGNMENT_EXCEPTION);
265 ARMV8_EC_CASE(ARMV8_ESR_EL2_EC_MOPS_EXCEPTION);
266 ARMV8_EC_CASE(ARMV8_ESR_EL2_EC_AARCH32_TRAPPED_FP_EXCEPTION);
267 ARMV8_EC_CASE(ARMV8_ESR_EL2_EC_AARCH64_TRAPPED_FP_EXCEPTION);
268 ARMV8_EC_CASE(ARMV8_ESR_EL2_SERROR_INTERRUPT);
269 ARMV8_EC_CASE(ARMV8_ESR_EL2_BKPT_EXCEPTION_FROM_LOWER_EL);
270 ARMV8_EC_CASE(ARMV8_ESR_EL2_BKPT_EXCEPTION_FROM_EL2);
271 ARMV8_EC_CASE(ARMV8_ESR_EL2_SS_EXCEPTION_FROM_LOWER_EL);
272 ARMV8_EC_CASE(ARMV8_ESR_EL2_SS_EXCEPTION_FROM_EL2);
273 ARMV8_EC_CASE(ARMV8_ESR_EL2_WATCHPOINT_EXCEPTION_FROM_LOWER_EL);
274 ARMV8_EC_CASE(ARMV8_ESR_EL2_WATCHPOINT_EXCEPTION_FROM_EL2);
275 ARMV8_EC_CASE(ARMV8_ESR_EL2_EC_AARCH32_BKPT_INSN);
276 ARMV8_EC_CASE(ARMV8_ESR_EL2_EC_AARCH32_VEC_CATCH_EXCEPTION);
277 ARMV8_EC_CASE(ARMV8_ESR_EL2_EC_AARCH64_BRK_INSN);
278#undef ARMV8_EC_CASE
279 default:
280 break;
281 }
282
283 return "<INVALID>";
284}
285
286
287/**
288 * Resolves a NEM page state from the given protection flags.
289 *
290 * @returns NEM page state.
291 * @param fPageProt The page protection flags.
292 */
293DECLINLINE(uint8_t) nemR3DarwinPageStateFromProt(uint32_t fPageProt)
294{
295 switch (fPageProt)
296 {
297 case NEM_PAGE_PROT_NONE:
298 return NEM_DARWIN_PAGE_STATE_UNMAPPED;
299 case NEM_PAGE_PROT_READ | NEM_PAGE_PROT_EXECUTE:
300 return NEM_DARWIN_PAGE_STATE_RX;
301 case NEM_PAGE_PROT_READ | NEM_PAGE_PROT_WRITE:
302 return NEM_DARWIN_PAGE_STATE_RW;
303 case NEM_PAGE_PROT_READ | NEM_PAGE_PROT_WRITE | NEM_PAGE_PROT_EXECUTE:
304 return NEM_DARWIN_PAGE_STATE_RWX;
305 default:
306 break;
307 }
308
309 AssertLogRelMsgFailed(("Invalid combination of page protection flags %#x, can't map to page state!\n", fPageProt));
310 return NEM_DARWIN_PAGE_STATE_UNMAPPED;
311}
312
313
314/**
315 * Unmaps the given guest physical address range (page aligned).
316 *
317 * @returns VBox status code.
318 * @param pVM The cross context VM structure.
319 * @param GCPhys The guest physical address to start unmapping at.
320 * @param cb The size of the range to unmap in bytes.
321 * @param pu2State Where to store the new state of the unmappd page, optional.
322 */
323DECLINLINE(int) nemR3DarwinUnmap(PVM pVM, RTGCPHYS GCPhys, size_t cb, uint8_t *pu2State)
324{
325 if (*pu2State <= NEM_DARWIN_PAGE_STATE_UNMAPPED)
326 {
327 Log5(("nemR3DarwinUnmap: %RGp == unmapped\n", GCPhys));
328 *pu2State = NEM_DARWIN_PAGE_STATE_UNMAPPED;
329 return VINF_SUCCESS;
330 }
331
332 LogFlowFunc(("Unmapping %RGp LB %zu\n", GCPhys, cb));
333 hv_return_t hrc = hv_vm_unmap(GCPhys, cb);
334 if (RT_LIKELY(hrc == HV_SUCCESS))
335 {
336 STAM_REL_COUNTER_INC(&pVM->nem.s.StatUnmapPage);
337 if (pu2State)
338 *pu2State = NEM_DARWIN_PAGE_STATE_UNMAPPED;
339 Log5(("nemR3DarwinUnmap: %RGp => unmapped\n", GCPhys));
340 return VINF_SUCCESS;
341 }
342
343 STAM_REL_COUNTER_INC(&pVM->nem.s.StatUnmapPageFailed);
344 LogRel(("nemR3DarwinUnmap(%RGp): failed! hrc=%#x\n",
345 GCPhys, hrc));
346 return VERR_NEM_IPE_6;
347}
348
349
350/**
351 * Maps a given guest physical address range backed by the given memory with the given
352 * protection flags.
353 *
354 * @returns VBox status code.
355 * @param pVM The cross context VM structure.
356 * @param GCPhys The guest physical address to start mapping.
357 * @param pvRam The R3 pointer of the memory to back the range with.
358 * @param cb The size of the range, page aligned.
359 * @param fPageProt The page protection flags to use for this range, combination of NEM_PAGE_PROT_XXX
360 * @param pu2State Where to store the state for the new page, optional.
361 */
362DECLINLINE(int) nemR3DarwinMap(PVM pVM, RTGCPHYS GCPhys, const void *pvRam, size_t cb, uint32_t fPageProt, uint8_t *pu2State)
363{
364 LogFlowFunc(("Mapping %RGp LB %zu fProt=%#x\n", GCPhys, cb, fPageProt));
365
366 Assert(fPageProt != NEM_PAGE_PROT_NONE);
367 RT_NOREF(pVM);
368
369 hv_memory_flags_t fHvMemProt = 0;
370 if (fPageProt & NEM_PAGE_PROT_READ)
371 fHvMemProt |= HV_MEMORY_READ;
372 if (fPageProt & NEM_PAGE_PROT_WRITE)
373 fHvMemProt |= HV_MEMORY_WRITE;
374 if (fPageProt & NEM_PAGE_PROT_EXECUTE)
375 fHvMemProt |= HV_MEMORY_EXEC;
376
377 hv_return_t hrc = hv_vm_map((void *)pvRam, GCPhys, cb, fHvMemProt);
378 if (hrc == HV_SUCCESS)
379 {
380 if (pu2State)
381 *pu2State = nemR3DarwinPageStateFromProt(fPageProt);
382 return VINF_SUCCESS;
383 }
384
385 return nemR3DarwinHvSts2Rc(hrc);
386}
387
388#if 0 /* unused */
389DECLINLINE(int) nemR3DarwinProtectPage(PVM pVM, RTGCPHYS GCPhys, size_t cb, uint32_t fPageProt)
390{
391 hv_memory_flags_t fHvMemProt = 0;
392 if (fPageProt & NEM_PAGE_PROT_READ)
393 fHvMemProt |= HV_MEMORY_READ;
394 if (fPageProt & NEM_PAGE_PROT_WRITE)
395 fHvMemProt |= HV_MEMORY_WRITE;
396 if (fPageProt & NEM_PAGE_PROT_EXECUTE)
397 fHvMemProt |= HV_MEMORY_EXEC;
398
399 hv_return_t hrc;
400 if (pVM->nem.s.fCreatedAsid)
401 hrc = hv_vm_protect_space(pVM->nem.s.uVmAsid, GCPhys, cb, fHvMemProt);
402 else
403 hrc = hv_vm_protect(GCPhys, cb, fHvMemProt);
404
405 return nemR3DarwinHvSts2Rc(hrc);
406}
407#endif
408
409DECLINLINE(int) nemR3NativeGCPhys2R3PtrReadOnly(PVM pVM, RTGCPHYS GCPhys, const void **ppv)
410{
411 PGMPAGEMAPLOCK Lock;
412 int rc = PGMPhysGCPhys2CCPtrReadOnly(pVM, GCPhys, ppv, &Lock);
413 if (RT_SUCCESS(rc))
414 PGMPhysReleasePageMappingLock(pVM, &Lock);
415 return rc;
416}
417
418
419DECLINLINE(int) nemR3NativeGCPhys2R3PtrWriteable(PVM pVM, RTGCPHYS GCPhys, void **ppv)
420{
421 PGMPAGEMAPLOCK Lock;
422 int rc = PGMPhysGCPhys2CCPtr(pVM, GCPhys, ppv, &Lock);
423 if (RT_SUCCESS(rc))
424 PGMPhysReleasePageMappingLock(pVM, &Lock);
425 return rc;
426}
427
428
429#ifdef LOG_ENABLED
430/**
431 * Logs the current CPU state.
432 */
433static void nemR3DarwinLogState(PVMCC pVM, PVMCPUCC pVCpu)
434{
435 if (LogIs3Enabled())
436 {
437 char szRegs[4096];
438 DBGFR3RegPrintf(pVM->pUVM, pVCpu->idCpu, &szRegs[0], sizeof(szRegs),
439 "x0=%016VR{x0} x1=%016VR{x1} x2=%016VR{x2} x3=%016VR{x3}\n"
440 "x4=%016VR{x4} x5=%016VR{x5} x6=%016VR{x6} x7=%016VR{x7}\n"
441 "x8=%016VR{x8} x9=%016VR{x9} x10=%016VR{x10} x11=%016VR{x11}\n"
442 "x12=%016VR{x12} x13=%016VR{x13} x14=%016VR{x14} x15=%016VR{x15}\n"
443 "x16=%016VR{x16} x17=%016VR{x17} x18=%016VR{x18} x19=%016VR{x19}\n"
444 "x20=%016VR{x20} x21=%016VR{x21} x22=%016VR{x22} x23=%016VR{x23}\n"
445 "x24=%016VR{x24} x25=%016VR{x25} x26=%016VR{x26} x27=%016VR{x27}\n"
446 "x28=%016VR{x28} x29=%016VR{x29} x30=%016VR{x30}\n"
447 "pc=%016VR{pc} pstate=%016VR{pstate}\n"
448 "sp_el0=%016VR{sp_el0} sp_el1=%016VR{sp_el1} elr_el1=%016VR{elr_el1}\n"
449 "sctlr_el1=%016VR{sctlr_el1} tcr_el1=%016VR{tcr_el1}\n"
450 "ttbr0_el1=%016VR{ttbr0_el1} ttbr1_el1=%016VR{ttbr1_el1}\n"
451 );
452 char szInstr[256]; RT_ZERO(szInstr);
453#if 0
454 DBGFR3DisasInstrEx(pVM->pUVM, pVCpu->idCpu, 0, 0,
455 DBGF_DISAS_FLAGS_CURRENT_GUEST | DBGF_DISAS_FLAGS_DEFAULT_MODE,
456 szInstr, sizeof(szInstr), NULL);
457#endif
458 Log3(("%s%s\n", szRegs, szInstr));
459 }
460}
461#endif /* LOG_ENABLED */
462
463
464static int nemR3DarwinCopyStateFromHv(PVMCC pVM, PVMCPUCC pVCpu, uint64_t fWhat)
465{
466 RT_NOREF(pVM);
467 hv_return_t hrc = HV_SUCCESS;
468
469 if (fWhat & (CPUMCTX_EXTRN_GPRS_MASK | CPUMCTX_EXTRN_PC | CPUMCTX_EXTRN_FPCR | CPUMCTX_EXTRN_FPSR))
470 {
471 for (uint32_t i = 0; i < RT_ELEMENTS(s_aCpumRegs); i++)
472 {
473 if (s_aCpumRegs[i].fCpumExtrn & fWhat)
474 {
475 uint64_t *pu64 = (uint64_t *)((uint8_t *)&pVCpu->cpum.GstCtx + s_aCpumRegs[i].offCpumCtx);
476 hrc |= hv_vcpu_get_reg(pVCpu->nem.s.hVCpu, s_aCpumRegs[i].enmHvReg, pu64);
477 }
478 }
479 }
480
481 if ( hrc == HV_SUCCESS
482 && (fWhat & CPUMCTX_EXTRN_V0_V31))
483 {
484 /* SIMD/FP registers. */
485 for (uint32_t i = 0; i < RT_ELEMENTS(s_aCpumFpRegs); i++)
486 {
487 hv_simd_fp_uchar16_t *pu128 = (hv_simd_fp_uchar16_t *)((uint8_t *)&pVCpu->cpum.GstCtx + s_aCpumFpRegs[i].offCpumCtx);
488 hrc |= hv_vcpu_get_simd_fp_reg(pVCpu->nem.s.hVCpu, s_aCpumFpRegs[i].enmHvReg, pu128);
489 }
490 }
491
492 if ( hrc == HV_SUCCESS
493 && (fWhat & (CPUMCTX_EXTRN_SPSR | CPUMCTX_EXTRN_ELR | CPUMCTX_EXTRN_SP | CPUMCTX_EXTRN_SCTLR_TCR_TTBR)))
494 {
495 /* System registers. */
496 for (uint32_t i = 0; i < RT_ELEMENTS(s_aCpumSysRegs); i++)
497 {
498 if (s_aCpumSysRegs[i].fCpumExtrn & fWhat)
499 {
500 uint64_t *pu64 = (uint64_t *)((uint8_t *)&pVCpu->cpum.GstCtx + s_aCpumSysRegs[i].offCpumCtx);
501 hrc |= hv_vcpu_get_sys_reg(pVCpu->nem.s.hVCpu, s_aCpumSysRegs[i].enmHvReg, pu64);
502 }
503 }
504 }
505
506 if ( hrc == HV_SUCCESS
507 && (fWhat & CPUMCTX_EXTRN_PSTATE))
508 {
509 uint64_t u64Tmp;
510 hrc |= hv_vcpu_get_reg(pVCpu->nem.s.hVCpu, HV_REG_CPSR, &u64Tmp);
511 if (hrc == HV_SUCCESS)
512 pVCpu->cpum.GstCtx.fPState = (uint32_t)u64Tmp;
513 }
514
515 /* Almost done, just update extern flags. */
516 pVCpu->cpum.GstCtx.fExtrn &= ~fWhat;
517 if (!(pVCpu->cpum.GstCtx.fExtrn & CPUMCTX_EXTRN_ALL))
518 pVCpu->cpum.GstCtx.fExtrn = 0;
519
520 return nemR3DarwinHvSts2Rc(hrc);
521}
522
523
524/**
525 * Exports the guest state to HV for execution.
526 *
527 * @returns VBox status code.
528 * @param pVM The cross context VM structure.
529 * @param pVCpu The cross context virtual CPU structure of the
530 * calling EMT.
531 */
532static int nemR3DarwinExportGuestState(PVMCC pVM, PVMCPUCC pVCpu)
533{
534 RT_NOREF(pVM);
535 hv_return_t hrc = HV_SUCCESS;
536
537 if ( (pVCpu->cpum.GstCtx.fExtrn & (CPUMCTX_EXTRN_GPRS_MASK | CPUMCTX_EXTRN_PC | CPUMCTX_EXTRN_FPCR | CPUMCTX_EXTRN_FPSR))
538 != (CPUMCTX_EXTRN_GPRS_MASK | CPUMCTX_EXTRN_PC | CPUMCTX_EXTRN_FPCR | CPUMCTX_EXTRN_FPSR))
539 {
540 for (uint32_t i = 0; i < RT_ELEMENTS(s_aCpumRegs); i++)
541 {
542 if (!(s_aCpumRegs[i].fCpumExtrn & pVCpu->cpum.GstCtx.fExtrn))
543 {
544 uint64_t *pu64 = (uint64_t *)((uint8_t *)&pVCpu->cpum.GstCtx + s_aCpumRegs[i].offCpumCtx);
545 hrc |= hv_vcpu_set_reg(pVCpu->nem.s.hVCpu, s_aCpumRegs[i].enmHvReg, *pu64);
546 }
547 }
548 }
549
550 if ( hrc == HV_SUCCESS
551 && !(pVCpu->cpum.GstCtx.fExtrn & CPUMCTX_EXTRN_V0_V31))
552 {
553 /* SIMD/FP registers. */
554 for (uint32_t i = 0; i < RT_ELEMENTS(s_aCpumFpRegs); i++)
555 {
556 hv_simd_fp_uchar16_t *pu128 = (hv_simd_fp_uchar16_t *)((uint8_t *)&pVCpu->cpum.GstCtx + s_aCpumFpRegs[i].offCpumCtx);
557 hrc |= hv_vcpu_set_simd_fp_reg(pVCpu->nem.s.hVCpu, s_aCpumFpRegs[i].enmHvReg, *pu128);
558 }
559 }
560
561 if ( hrc == HV_SUCCESS
562 && (pVCpu->cpum.GstCtx.fExtrn & (CPUMCTX_EXTRN_SPSR | CPUMCTX_EXTRN_ELR | CPUMCTX_EXTRN_SP | CPUMCTX_EXTRN_SCTLR_TCR_TTBR))
563 != (CPUMCTX_EXTRN_SPSR | CPUMCTX_EXTRN_ELR | CPUMCTX_EXTRN_SP | CPUMCTX_EXTRN_SCTLR_TCR_TTBR))
564 {
565 /* System registers. */
566 for (uint32_t i = 0; i < RT_ELEMENTS(s_aCpumSysRegs); i++)
567 {
568 if (!(s_aCpumSysRegs[i].fCpumExtrn & pVCpu->cpum.GstCtx.fExtrn))
569 {
570 uint64_t *pu64 = (uint64_t *)((uint8_t *)&pVCpu->cpum.GstCtx + s_aCpumSysRegs[i].offCpumCtx);
571 hrc |= hv_vcpu_set_sys_reg(pVCpu->nem.s.hVCpu, s_aCpumSysRegs[i].enmHvReg, *pu64);
572 }
573 }
574 }
575
576 if ( hrc == HV_SUCCESS
577 && !(pVCpu->cpum.GstCtx.fExtrn & CPUMCTX_EXTRN_PSTATE))
578 hrc = hv_vcpu_set_reg(pVCpu->nem.s.hVCpu, HV_REG_CPSR, pVCpu->cpum.GstCtx.fPState);
579
580 pVCpu->cpum.GstCtx.fExtrn |= CPUMCTX_EXTRN_ALL | CPUMCTX_EXTRN_KEEPER_NEM;
581 return nemR3DarwinHvSts2Rc(hrc);
582}
583
584
585/**
586 * Try initialize the native API.
587 *
588 * This may only do part of the job, more can be done in
589 * nemR3NativeInitAfterCPUM() and nemR3NativeInitCompleted().
590 *
591 * @returns VBox status code.
592 * @param pVM The cross context VM structure.
593 * @param fFallback Whether we're in fallback mode or use-NEM mode. In
594 * the latter we'll fail if we cannot initialize.
595 * @param fForced Whether the HMForced flag is set and we should
596 * fail if we cannot initialize.
597 */
598int nemR3NativeInit(PVM pVM, bool fFallback, bool fForced)
599{
600 AssertReturn(!pVM->nem.s.fCreatedVm, VERR_WRONG_ORDER);
601
602 /*
603 * Some state init.
604 */
605 PCFGMNODE pCfgNem = CFGMR3GetChild(CFGMR3GetRoot(pVM), "NEM/");
606 RT_NOREF(pCfgNem);
607
608 /*
609 * Error state.
610 * The error message will be non-empty on failure and 'rc' will be set too.
611 */
612 RTERRINFOSTATIC ErrInfo;
613 PRTERRINFO pErrInfo = RTErrInfoInitStatic(&ErrInfo);
614
615 int rc = VINF_SUCCESS;
616 hv_return_t hrc = hv_vm_create(NULL);
617 if (hrc == HV_SUCCESS)
618 {
619 pVM->nem.s.fCreatedVm = true;
620 VM_SET_MAIN_EXECUTION_ENGINE(pVM, VM_EXEC_ENGINE_NATIVE_API);
621 Log(("NEM: Marked active!\n"));
622 PGMR3EnableNemMode(pVM);
623 }
624 else
625 rc = RTErrInfoSetF(pErrInfo, VERR_NEM_INIT_FAILED,
626 "hv_vm_create() failed: %#x", hrc);
627
628 /*
629 * We only fail if in forced mode, otherwise just log the complaint and return.
630 */
631 Assert(pVM->bMainExecutionEngine == VM_EXEC_ENGINE_NATIVE_API || RTErrInfoIsSet(pErrInfo));
632 if ( (fForced || !fFallback)
633 && pVM->bMainExecutionEngine != VM_EXEC_ENGINE_NATIVE_API)
634 return VMSetError(pVM, RT_SUCCESS_NP(rc) ? VERR_NEM_NOT_AVAILABLE : rc, RT_SRC_POS, "%s", pErrInfo->pszMsg);
635
636if (RTErrInfoIsSet(pErrInfo))
637 LogRel(("NEM: Not available: %s\n", pErrInfo->pszMsg));
638 return VINF_SUCCESS;
639}
640
641
642/**
643 * Worker to create the vCPU handle on the EMT running it later on (as required by HV).
644 *
645 * @returns VBox status code
646 * @param pVM The VM handle.
647 * @param pVCpu The vCPU handle.
648 * @param idCpu ID of the CPU to create.
649 */
650static DECLCALLBACK(int) nemR3DarwinNativeInitVCpuOnEmt(PVM pVM, PVMCPU pVCpu, VMCPUID idCpu)
651{
652 hv_return_t hrc = hv_vcpu_create(&pVCpu->nem.s.hVCpu, &pVCpu->nem.s.pHvExit, NULL);
653 if (hrc != HV_SUCCESS)
654 return VMSetError(pVM, VERR_NEM_VM_CREATE_FAILED, RT_SRC_POS,
655 "Call to hv_vcpu_create failed on vCPU %u: %#x (%Rrc)", idCpu, hrc, nemR3DarwinHvSts2Rc(hrc));
656
657 if (idCpu == 0)
658 {
659 /** @todo */
660 }
661
662 return VINF_SUCCESS;
663}
664
665
666/**
667 * Worker to destroy the vCPU handle on the EMT running it later on (as required by HV).
668 *
669 * @returns VBox status code
670 * @param pVCpu The vCPU handle.
671 */
672static DECLCALLBACK(int) nemR3DarwinNativeTermVCpuOnEmt(PVMCPU pVCpu)
673{
674 hv_return_t hrc = hv_vcpu_destroy(pVCpu->nem.s.hVCpu);
675 Assert(hrc == HV_SUCCESS); RT_NOREF(hrc);
676 return VINF_SUCCESS;
677}
678
679
680/**
681 * This is called after CPUMR3Init is done.
682 *
683 * @returns VBox status code.
684 * @param pVM The VM handle..
685 */
686int nemR3NativeInitAfterCPUM(PVM pVM)
687{
688 /*
689 * Validate sanity.
690 */
691 AssertReturn(!pVM->nem.s.fCreatedEmts, VERR_WRONG_ORDER);
692 AssertReturn(pVM->bMainExecutionEngine == VM_EXEC_ENGINE_NATIVE_API, VERR_WRONG_ORDER);
693
694 /*
695 * Setup the EMTs.
696 */
697 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
698 {
699 PVMCPU pVCpu = pVM->apCpusR3[idCpu];
700
701 int rc = VMR3ReqCallWait(pVM, idCpu, (PFNRT)nemR3DarwinNativeInitVCpuOnEmt, 3, pVM, pVCpu, idCpu);
702 if (RT_FAILURE(rc))
703 {
704 /* Rollback. */
705 while (idCpu--)
706 VMR3ReqCallWait(pVM, idCpu, (PFNRT)nemR3DarwinNativeTermVCpuOnEmt, 1, pVCpu);
707
708 return VMSetError(pVM, VERR_NEM_VM_CREATE_FAILED, RT_SRC_POS, "Call to hv_vcpu_create failed: %Rrc", rc);
709 }
710 }
711
712 pVM->nem.s.fCreatedEmts = true;
713 return VINF_SUCCESS;
714}
715
716
717int nemR3NativeInitCompleted(PVM pVM, VMINITCOMPLETED enmWhat)
718{
719 RT_NOREF(pVM, enmWhat);
720 return VINF_SUCCESS;
721}
722
723
724int nemR3NativeTerm(PVM pVM)
725{
726 /*
727 * Delete the VM.
728 */
729
730 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu--)
731 {
732 PVMCPU pVCpu = pVM->apCpusR3[idCpu];
733
734 /*
735 * Apple's documentation states that the vCPU should be destroyed
736 * on the thread running the vCPU but as all the other EMTs are gone
737 * at this point, destroying the VM would hang.
738 *
739 * We seem to be at luck here though as destroying apparently works
740 * from EMT(0) as well.
741 */
742 hv_return_t hrc = hv_vcpu_destroy(pVCpu->nem.s.hVCpu);
743 Assert(hrc == HV_SUCCESS); RT_NOREF(hrc);
744 }
745
746 pVM->nem.s.fCreatedEmts = false;
747 if (pVM->nem.s.fCreatedVm)
748 {
749 hv_return_t hrc = hv_vm_destroy();
750 if (hrc != HV_SUCCESS)
751 LogRel(("NEM: hv_vm_destroy() failed with %#x\n", hrc));
752
753 pVM->nem.s.fCreatedVm = false;
754 }
755 return VINF_SUCCESS;
756}
757
758
759/**
760 * VM reset notification.
761 *
762 * @param pVM The cross context VM structure.
763 */
764void nemR3NativeReset(PVM pVM)
765{
766 RT_NOREF(pVM);
767}
768
769
770/**
771 * Reset CPU due to INIT IPI or hot (un)plugging.
772 *
773 * @param pVCpu The cross context virtual CPU structure of the CPU being
774 * reset.
775 * @param fInitIpi Whether this is the INIT IPI or hot (un)plugging case.
776 */
777void nemR3NativeResetCpu(PVMCPU pVCpu, bool fInitIpi)
778{
779 RT_NOREF(pVCpu, fInitIpi);
780}
781
782
783/**
784 * Returns the byte size from the given access SAS value.
785 *
786 * @returns Number of bytes to transfer.
787 * @param uSas The SAS value to convert.
788 */
789DECLINLINE(size_t) nemR3DarwinGetByteCountFromSas(uint8_t uSas)
790{
791 switch (uSas)
792 {
793 case ARMV8_EC_ISS_DATA_ABRT_SAS_BYTE: return sizeof(uint8_t);
794 case ARMV8_EC_ISS_DATA_ABRT_SAS_HALFWORD: return sizeof(uint16_t);
795 case ARMV8_EC_ISS_DATA_ABRT_SAS_WORD: return sizeof(uint32_t);
796 case ARMV8_EC_ISS_DATA_ABRT_SAS_DWORD: return sizeof(uint64_t);
797 default:
798 AssertReleaseFailed();
799 }
800
801 return 0;
802}
803
804
805/**
806 * Sets the given general purpose register to the given value.
807 *
808 * @returns nothing.
809 * @param pVCpu The cross context virtual CPU structure of the
810 * calling EMT.
811 * @param uReg The register index.
812 * @param f64BitReg Flag whether to operate on a 64-bit or 32-bit register.
813 * @param fSignExtend Flag whether to sign extend the value.
814 * @param u64Val The value.
815 */
816DECLINLINE(void) nemR3DarwinSetGReg(PVMCPU pVCpu, uint8_t uReg, bool f64BitReg, bool fSignExtend, uint64_t u64Val)
817{
818 AssertReturnVoid(uReg < 31);
819
820 if (f64BitReg)
821 pVCpu->cpum.GstCtx.aGRegs[uReg].x = fSignExtend ? (int64_t)u64Val : u64Val;
822 else
823 pVCpu->cpum.GstCtx.aGRegs[uReg].w = fSignExtend ? (int32_t)u64Val : u64Val; /** @todo Does this clear the upper half on real hardware? */
824
825 /* Mark the register as not extern anymore. */
826 switch (uReg)
827 {
828 case 0:
829 pVCpu->cpum.GstCtx.fExtrn &= ~CPUMCTX_EXTRN_X0;
830 break;
831 case 1:
832 pVCpu->cpum.GstCtx.fExtrn &= ~CPUMCTX_EXTRN_X1;
833 break;
834 case 2:
835 pVCpu->cpum.GstCtx.fExtrn &= ~CPUMCTX_EXTRN_X2;
836 break;
837 case 3:
838 pVCpu->cpum.GstCtx.fExtrn &= ~CPUMCTX_EXTRN_X3;
839 break;
840 default:
841 AssertRelease(!(pVCpu->cpum.GstCtx.fExtrn & CPUMCTX_EXTRN_X4_X28));
842 /** @todo We need to import all missing registers in order to clear this flag (or just set it in HV from here). */
843 }
844}
845
846
847/**
848 * Gets the given general purpose register and returns the value.
849 *
850 * @returns Value from the given register.
851 * @param pVCpu The cross context virtual CPU structure of the
852 * calling EMT.
853 * @param uReg The register index.
854 */
855DECLINLINE(uint64_t) nemR3DarwinGetGReg(PVMCPU pVCpu, uint8_t uReg)
856{
857 AssertReturn(uReg <= ARMV8_AARCH64_REG_ZR, 0);
858
859 if (uReg == ARMV8_AARCH64_REG_ZR)
860 return 0;
861
862 /** @todo Import the register if extern. */
863 AssertRelease(!(pVCpu->cpum.GstCtx.fExtrn & CPUMCTX_EXTRN_GPRS_MASK));
864
865 return pVCpu->cpum.GstCtx.aGRegs[uReg].x;
866}
867
868
869/**
870 * Works on the data abort exception (which will be a MMIO access most of the time).
871 *
872 * @returns VBox strict status code.
873 * @param pVM The cross context VM structure.
874 * @param pVCpu The cross context virtual CPU structure of the
875 * calling EMT.
876 * @param uIss The instruction specific syndrome value.
877 * @param fInsn32Bit Flag whether the exception was caused by a 32-bit or 16-bit instruction.
878 * @param GCPtrDataAbrt The virtual GC address causing the data abort.
879 * @param GCPhysDataAbrt The physical GC address which caused the data abort.
880 */
881static VBOXSTRICTRC nemR3DarwinHandleExitExceptionDataAbort(PVM pVM, PVMCPU pVCpu, uint32_t uIss, bool fInsn32Bit,
882 RTGCPTR GCPtrDataAbrt, RTGCPHYS GCPhysDataAbrt)
883{
884 bool fIsv = RT_BOOL(uIss & ARMV8_EC_ISS_DATA_ABRT_ISV);
885 bool fL2Fault = RT_BOOL(uIss & ARMV8_EC_ISS_DATA_ABRT_S1PTW);
886 bool fWrite = RT_BOOL(uIss & ARMV8_EC_ISS_DATA_ABRT_WNR);
887 bool f64BitReg = RT_BOOL(uIss & ARMV8_EC_ISS_DATA_ABRT_SF);
888 bool fSignExtend = RT_BOOL(uIss & ARMV8_EC_ISS_DATA_ABRT_SSE);
889 uint8_t uReg = ARMV8_EC_ISS_DATA_ABRT_SRT_GET(uIss);
890 uint8_t uAcc = ARMV8_EC_ISS_DATA_ABRT_SAS_GET(uIss);
891 size_t cbAcc = nemR3DarwinGetByteCountFromSas(uAcc);
892 LogFlowFunc(("fIsv=%RTbool fL2Fault=%RTbool fWrite=%RTbool f64BitReg=%RTbool fSignExtend=%RTbool uReg=%u uAcc=%u GCPtrDataAbrt=%RGv GCPhysDataAbrt=%RGp\n",
893 fIsv, fL2Fault, fWrite, f64BitReg, fSignExtend, uReg, uAcc, GCPtrDataAbrt, GCPhysDataAbrt));
894
895 AssertReturn(fIsv, VERR_NOT_SUPPORTED); /** @todo Implement using IEM when this should occur. */
896
897 EMHistoryAddExit(pVCpu,
898 fWrite
899 ? EMEXIT_MAKE_FT(EMEXIT_F_KIND_EM, EMEXITTYPE_MMIO_WRITE)
900 : EMEXIT_MAKE_FT(EMEXIT_F_KIND_EM, EMEXITTYPE_MMIO_READ),
901 pVCpu->cpum.GstCtx.Pc.u64, ASMReadTSC());
902
903 VBOXSTRICTRC rcStrict = VINF_SUCCESS;
904 uint64_t u64Val = 0;
905 if (fWrite)
906 {
907 u64Val = nemR3DarwinGetGReg(pVCpu, uReg);
908 rcStrict = PGMPhysWrite(pVM, GCPhysDataAbrt, &u64Val, cbAcc, PGMACCESSORIGIN_HM);
909 Log4(("MmioExit/%u: %08RX64: WRITE %#x LB %u, %.*Rhxs -> rcStrict=%Rrc\n",
910 pVCpu->idCpu, pVCpu->cpum.GstCtx.Pc.u64, GCPhysDataAbrt, cbAcc, cbAcc,
911 &u64Val, VBOXSTRICTRC_VAL(rcStrict) ));
912 }
913 else
914 {
915 rcStrict = PGMPhysRead(pVM, GCPhysDataAbrt, &u64Val, cbAcc, PGMACCESSORIGIN_HM);
916 Log4(("MmioExit/%u: %08RX64: READ %#x LB %u -> %.*Rhxs rcStrict=%Rrc\n",
917 pVCpu->idCpu, pVCpu->cpum.GstCtx.Pc.u64, GCPhysDataAbrt, cbAcc, cbAcc,
918 &u64Val, VBOXSTRICTRC_VAL(rcStrict) ));
919 if (rcStrict == VINF_SUCCESS)
920 nemR3DarwinSetGReg(pVCpu, uReg, f64BitReg, fSignExtend, u64Val);
921 }
922
923 if (rcStrict == VINF_SUCCESS)
924 pVCpu->cpum.GstCtx.Pc.u64 += fInsn32Bit ? sizeof(uint32_t) : sizeof(uint16_t);
925
926 return rcStrict;
927}
928
929
930/**
931 * Works on the trapped MRS, MSR and system instruction exception.
932 *
933 * @returns VBox strict status code.
934 * @param pVM The cross context VM structure.
935 * @param pVCpu The cross context virtual CPU structure of the
936 * calling EMT.
937 * @param uIss The instruction specific syndrome value.
938 * @param fInsn32Bit Flag whether the exception was caused by a 32-bit or 16-bit instruction.
939 */
940static VBOXSTRICTRC nemR3DarwinHandleExitExceptionTrappedSysInsn(PVM pVM, PVMCPU pVCpu, uint32_t uIss, bool fInsn32Bit)
941{
942 bool fRead = ARMV8_EC_ISS_AARCH64_TRAPPED_SYS_INSN_DIRECTION_IS_READ(uIss);
943 uint8_t uCRm = ARMV8_EC_ISS_AARCH64_TRAPPED_SYS_INSN_CRM_GET(uIss);
944 uint8_t uReg = ARMV8_EC_ISS_AARCH64_TRAPPED_SYS_INSN_RT_GET(uIss);
945 uint8_t uCRn = ARMV8_EC_ISS_AARCH64_TRAPPED_SYS_INSN_CRN_GET(uIss);
946 uint8_t uOp1 = ARMV8_EC_ISS_AARCH64_TRAPPED_SYS_INSN_OP1_GET(uIss);
947 uint8_t uOp2 = ARMV8_EC_ISS_AARCH64_TRAPPED_SYS_INSN_OP2_GET(uIss);
948 uint8_t uOp0 = ARMV8_EC_ISS_AARCH64_TRAPPED_SYS_INSN_OP0_GET(uIss);
949 uint16_t idSysReg = ARMV8_AARCH64_SYSREG_ID_CREATE(uOp0, uOp1, uCRn, uCRm, uOp2);
950 LogFlowFunc(("fRead=%RTbool uCRm=%u uReg=%u uCRn=%u uOp1=%u uOp2=%u uOp0=%u idSysReg=%#x\n",
951 fRead, uCRm, uReg, uCRn, uOp1, uOp2, uOp0, idSysReg));
952
953 /** @todo EMEXITTYPE_MSR_READ/EMEXITTYPE_MSR_WRITE are misnomers. */
954 EMHistoryAddExit(pVCpu,
955 fRead
956 ? EMEXIT_MAKE_FT(EMEXIT_F_KIND_EM, EMEXITTYPE_MSR_READ)
957 : EMEXIT_MAKE_FT(EMEXIT_F_KIND_EM, EMEXITTYPE_MSR_WRITE),
958 pVCpu->cpum.GstCtx.Pc.u64, ASMReadTSC());
959
960 VBOXSTRICTRC rcStrict = VINF_SUCCESS;
961 uint64_t u64Val = 0;
962 if (fRead)
963 {
964 RT_NOREF(pVM);
965 rcStrict = CPUMQueryGuestSysReg(pVCpu, idSysReg, &u64Val);
966 Log4(("SysInsnExit/%u: %08RX64: READ %u:%u:%u:%u:%u -> %#RX64 rcStrict=%Rrc\n",
967 pVCpu->idCpu, pVCpu->cpum.GstCtx.Pc.u64, uOp0, uOp1, uCRn, uCRm, uOp2, u64Val,
968 VBOXSTRICTRC_VAL(rcStrict) ));
969 if (rcStrict == VINF_SUCCESS)
970 nemR3DarwinSetGReg(pVCpu, uReg, true /*f64BitReg*/, false /*fSignExtend*/, u64Val);
971 }
972 else
973 {
974 u64Val = nemR3DarwinGetGReg(pVCpu, uReg);
975 rcStrict = CPUMSetGuestSysReg(pVCpu, idSysReg, u64Val);
976 Log4(("SysInsnExit/%u: %08RX64: WRITE %u:%u:%u:%u:%u %#RX64 -> rcStrict=%Rrc\n",
977 pVCpu->idCpu, pVCpu->cpum.GstCtx.Pc.u64, uOp0, uOp1, uCRn, uCRm, uOp2, u64Val,
978 VBOXSTRICTRC_VAL(rcStrict) ));
979 }
980
981 if (rcStrict == VINF_SUCCESS)
982 pVCpu->cpum.GstCtx.Pc.u64 += fInsn32Bit ? sizeof(uint32_t) : sizeof(uint16_t);
983
984 return rcStrict;
985}
986
987
988/**
989 * Works on the trapped HVC instruction exception.
990 *
991 * @returns VBox strict status code.
992 * @param pVM The cross context VM structure.
993 * @param pVCpu The cross context virtual CPU structure of the
994 * calling EMT.
995 * @param uIss The instruction specific syndrome value.
996 */
997static VBOXSTRICTRC nemR3DarwinHandleExitExceptionTrappedHvcInsn(PVM pVM, PVMCPU pVCpu, uint32_t uIss)
998{
999 uint16_t u16Imm = ARMV8_EC_ISS_AARCH64_TRAPPED_HVC_INSN_IMM_GET(uIss);
1000 LogFlowFunc(("u16Imm=%#RX16\n", u16Imm));
1001
1002#if 0 /** @todo For later */
1003 EMHistoryAddExit(pVCpu,
1004 fRead
1005 ? EMEXIT_MAKE_FT(EMEXIT_F_KIND_EM, EMEXITTYPE_MSR_READ)
1006 : EMEXIT_MAKE_FT(EMEXIT_F_KIND_EM, EMEXITTYPE_MSR_WRITE),
1007 pVCpu->cpum.GstCtx.Pc.u64, ASMReadTSC());
1008#endif
1009
1010 RT_NOREF(pVM);
1011 VBOXSTRICTRC rcStrict = VINF_SUCCESS;
1012 /** @todo Raise exception to EL1 if PSCI not configured. */
1013 /** @todo Need a generic mechanism here to pass this to, GIM maybe?. Always return -1 for now (PSCI). */
1014 nemR3DarwinSetGReg(pVCpu, ARMV8_AARCH64_REG_X0, true /*f64BitReg*/, false /*fSignExtend*/, (uint64_t)-1);
1015
1016 return rcStrict;
1017}
1018
1019
1020/**
1021 * Handles an exception VM exit.
1022 *
1023 * @returns VBox strict status code.
1024 * @param pVM The cross context VM structure.
1025 * @param pVCpu The cross context virtual CPU structure of the
1026 * calling EMT.
1027 * @param pExit Pointer to the exit information.
1028 */
1029static VBOXSTRICTRC nemR3DarwinHandleExitException(PVM pVM, PVMCPU pVCpu, const hv_vcpu_exit_t *pExit)
1030{
1031 uint32_t uEc = ARMV8_ESR_EL2_EC_GET(pExit->exception.syndrome);
1032 uint32_t uIss = ARMV8_ESR_EL2_ISS_GET(pExit->exception.syndrome);
1033 bool fInsn32Bit = ARMV8_ESR_EL2_IL_IS_32BIT(pExit->exception.syndrome);
1034
1035 LogFlowFunc(("pVM=%p pVCpu=%p{.idCpu=%u} uEc=%u{%s} uIss=%#RX32 fInsn32Bit=%RTbool\n",
1036 pVM, pVCpu, pVCpu->idCpu, uEc, nemR3DarwinEsrEl2EcStringify(uEc), uIss, fInsn32Bit));
1037
1038 switch (uEc)
1039 {
1040 case ARMV8_ESR_EL2_DATA_ABORT_FROM_LOWER_EL:
1041 return nemR3DarwinHandleExitExceptionDataAbort(pVM, pVCpu, uIss, fInsn32Bit, pExit->exception.virtual_address,
1042 pExit->exception.physical_address);
1043 case ARMV8_ESR_EL2_EC_AARCH64_TRAPPED_SYS_INSN:
1044 return nemR3DarwinHandleExitExceptionTrappedSysInsn(pVM, pVCpu, uIss, fInsn32Bit);
1045 case ARMV8_ESR_EL2_EC_AARCH64_HVC_INSN:
1046 return nemR3DarwinHandleExitExceptionTrappedHvcInsn(pVM, pVCpu, uIss);
1047 case ARMV8_ESR_EL2_EC_TRAPPED_WFX:
1048 return VINF_EM_HALT;
1049 case ARMV8_ESR_EL2_EC_UNKNOWN:
1050 default:
1051 LogRel(("NEM/Darwin: Unknown Exception Class in syndrome: uEc=%u{%s} uIss=%#RX32 fInsn32Bit=%RTbool\n",
1052 uEc, nemR3DarwinEsrEl2EcStringify(uEc), uIss, fInsn32Bit));
1053 AssertReleaseFailed();
1054 return VERR_NOT_IMPLEMENTED;
1055 }
1056
1057 return VINF_SUCCESS;
1058}
1059
1060
1061/**
1062 * Handles an exit from hv_vcpu_run().
1063 *
1064 * @returns VBox strict status code.
1065 * @param pVM The cross context VM structure.
1066 * @param pVCpu The cross context virtual CPU structure of the
1067 * calling EMT.
1068 */
1069static VBOXSTRICTRC nemR3DarwinHandleExit(PVM pVM, PVMCPU pVCpu)
1070{
1071 int rc = nemR3DarwinCopyStateFromHv(pVM, pVCpu, CPUMCTX_EXTRN_ALL);
1072 if (RT_FAILURE(rc))
1073 return rc;
1074
1075#ifdef LOG_ENABLED
1076 if (LogIs3Enabled())
1077 nemR3DarwinLogState(pVM, pVCpu);
1078#endif
1079
1080 hv_vcpu_exit_t *pExit = pVCpu->nem.s.pHvExit;
1081 switch (pExit->reason)
1082 {
1083 case HV_EXIT_REASON_CANCELED:
1084 return VINF_EM_RAW_INTERRUPT;
1085 case HV_EXIT_REASON_EXCEPTION:
1086 return nemR3DarwinHandleExitException(pVM, pVCpu, pExit);
1087 case HV_EXIT_REASON_VTIMER_ACTIVATED:
1088 pVCpu->nem.s.fVTimerActivated = true;
1089 return GICPpiSet(pVCpu, NEM_DARWIN_VTIMER_GIC_PPI_IRQ, true /*fAsserted*/);
1090 default:
1091 AssertReleaseFailed();
1092 break;
1093 }
1094
1095 return VERR_INVALID_STATE;
1096}
1097
1098
1099/**
1100 * Runs the guest once until an exit occurs.
1101 *
1102 * @returns HV status code.
1103 * @param pVM The cross context VM structure.
1104 * @param pVCpu The cross context virtual CPU structure.
1105 */
1106static hv_return_t nemR3DarwinRunGuest(PVM pVM, PVMCPU pVCpu)
1107{
1108 TMNotifyStartOfExecution(pVM, pVCpu);
1109
1110 hv_return_t hrc = hv_vcpu_run(pVCpu->nem.s.hVCpu);
1111
1112 TMNotifyEndOfExecution(pVM, pVCpu, ASMReadTSC());
1113
1114 return hrc;
1115}
1116
1117
1118/**
1119 * Prepares the VM to run the guest.
1120 *
1121 * @returns Strict VBox status code.
1122 * @param pVM The cross context VM structure.
1123 * @param pVCpu The cross context virtual CPU structure.
1124 * @param fSingleStepping Flag whether we run in single stepping mode.
1125 */
1126static VBOXSTRICTRC nemR3DarwinPreRunGuest(PVM pVM, PVMCPU pVCpu, bool fSingleStepping)
1127{
1128#ifdef LOG_ENABLED
1129 bool fIrq = false;
1130 bool fFiq = false;
1131
1132 if (LogIs3Enabled())
1133 nemR3DarwinLogState(pVM, pVCpu);
1134#endif
1135
1136 /** @todo */ RT_NOREF(fSingleStepping);
1137 int rc = nemR3DarwinExportGuestState(pVM, pVCpu);
1138 AssertRCReturn(rc, rc);
1139
1140 /* Check whether the vTimer interrupt was handled by the guest and we can unmask the vTimer. */
1141 if (pVCpu->nem.s.fVTimerActivated)
1142 {
1143 /* Read the CNTV_CTL_EL0 register. */
1144 uint64_t u64CntvCtl = 0;
1145
1146 hv_return_t hrc = hv_vcpu_get_sys_reg(pVCpu->nem.s.hVCpu, HV_SYS_REG_CNTV_CTL_EL0, &u64CntvCtl);
1147 AssertRCReturn(hrc == HV_SUCCESS, VERR_NEM_IPE_9);
1148
1149 if ( (u64CntvCtl & (ARMV8_CNTV_CTL_EL0_AARCH64_ENABLE | ARMV8_CNTV_CTL_EL0_AARCH64_IMASK | ARMV8_CNTV_CTL_EL0_AARCH64_ISTATUS))
1150 != (ARMV8_CNTV_CTL_EL0_AARCH64_ENABLE | ARMV8_CNTV_CTL_EL0_AARCH64_ISTATUS))
1151 {
1152 /* Clear the interrupt. */
1153 GICPpiSet(pVCpu, NEM_DARWIN_VTIMER_GIC_PPI_IRQ, false /*fAsserted*/);
1154
1155 pVCpu->nem.s.fVTimerActivated = false;
1156 hrc = hv_vcpu_set_vtimer_mask(pVCpu->nem.s.hVCpu, false /*vtimer_is_masked*/);
1157 AssertReturn(hrc == HV_SUCCESS, VERR_NEM_IPE_9);
1158 }
1159 }
1160
1161 /* Set the pending interrupt state. */
1162 if (VMCPU_FF_IS_ANY_SET(pVCpu, VMCPU_FF_INTERRUPT_IRQ | VMCPU_FF_INTERRUPT_FIQ))
1163 {
1164 hv_return_t hrc = HV_SUCCESS;
1165
1166 if (VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_IRQ))
1167 {
1168 hrc = hv_vcpu_set_pending_interrupt(pVCpu->nem.s.hVCpu, HV_INTERRUPT_TYPE_IRQ, true);
1169 AssertReturn(hrc == HV_SUCCESS, VERR_NEM_IPE_9);
1170#ifdef LOG_ENABLED
1171 fIrq = true;
1172#endif
1173 }
1174
1175 if (VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_FIQ))
1176 {
1177 hrc = hv_vcpu_set_pending_interrupt(pVCpu->nem.s.hVCpu, HV_INTERRUPT_TYPE_FIQ, true);
1178 AssertReturn(hrc == HV_SUCCESS, VERR_NEM_IPE_9);
1179#ifdef LOG_ENABLED
1180 fFiq = true;
1181#endif
1182 }
1183 }
1184 else
1185 {
1186 hv_return_t hrc = hv_vcpu_set_pending_interrupt(pVCpu->nem.s.hVCpu, HV_INTERRUPT_TYPE_IRQ, false);
1187 AssertReturn(hrc == HV_SUCCESS, VERR_NEM_IPE_9);
1188
1189 hrc = hv_vcpu_set_pending_interrupt(pVCpu->nem.s.hVCpu, HV_INTERRUPT_TYPE_FIQ, false);
1190 AssertReturn(hrc == HV_SUCCESS, VERR_NEM_IPE_9);
1191 }
1192
1193 LogFlowFunc(("Running vCPU [%s,%s]\n", fIrq ? "I" : "nI", fFiq ? "F" : "nF"));
1194 pVCpu->nem.s.fEventPending = false;
1195 return VINF_SUCCESS;
1196}
1197
1198
1199/**
1200 * The normal runloop (no debugging features enabled).
1201 *
1202 * @returns Strict VBox status code.
1203 * @param pVM The cross context VM structure.
1204 * @param pVCpu The cross context virtual CPU structure.
1205 */
1206static VBOXSTRICTRC nemR3DarwinRunGuestNormal(PVM pVM, PVMCPU pVCpu)
1207{
1208 /*
1209 * The run loop.
1210 *
1211 * Current approach to state updating to use the sledgehammer and sync
1212 * everything every time. This will be optimized later.
1213 */
1214
1215 /*
1216 * Poll timers and run for a bit.
1217 */
1218 /** @todo See if we cannot optimize this TMTimerPollGIP by only redoing
1219 * the whole polling job when timers have changed... */
1220 uint64_t offDeltaIgnored;
1221 uint64_t const nsNextTimerEvt = TMTimerPollGIP(pVM, pVCpu, &offDeltaIgnored); NOREF(nsNextTimerEvt);
1222 VBOXSTRICTRC rcStrict = VINF_SUCCESS;
1223 for (unsigned iLoop = 0;; iLoop++)
1224 {
1225 rcStrict = nemR3DarwinPreRunGuest(pVM, pVCpu, false /* fSingleStepping */);
1226 if (rcStrict != VINF_SUCCESS)
1227 break;
1228
1229 hv_return_t hrc = nemR3DarwinRunGuest(pVM, pVCpu);
1230 if (hrc == HV_SUCCESS)
1231 {
1232 /*
1233 * Deal with the message.
1234 */
1235 rcStrict = nemR3DarwinHandleExit(pVM, pVCpu);
1236 if (rcStrict == VINF_SUCCESS)
1237 { /* hopefully likely */ }
1238 else
1239 {
1240 LogFlow(("NEM/%u: breaking: nemR3DarwinHandleExit -> %Rrc\n", pVCpu->idCpu, VBOXSTRICTRC_VAL(rcStrict) ));
1241 STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatBreakOnStatus);
1242 break;
1243 }
1244 }
1245 else
1246 {
1247 AssertLogRelMsgFailedReturn(("hv_vcpu_run()) failed for CPU #%u: %#x \n",
1248 pVCpu->idCpu, hrc), VERR_NEM_IPE_0);
1249 }
1250 } /* the run loop */
1251
1252 return rcStrict;
1253}
1254
1255
1256VBOXSTRICTRC nemR3NativeRunGC(PVM pVM, PVMCPU pVCpu)
1257{
1258#ifdef LOG_ENABLED
1259 if (LogIs3Enabled())
1260 nemR3DarwinLogState(pVM, pVCpu);
1261#endif
1262
1263 AssertReturn(NEMR3CanExecuteGuest(pVM, pVCpu), VERR_NEM_IPE_9);
1264
1265 /*
1266 * Try switch to NEM runloop state.
1267 */
1268 if (VMCPU_CMPXCHG_STATE(pVCpu, VMCPUSTATE_STARTED_EXEC_NEM, VMCPUSTATE_STARTED))
1269 { /* likely */ }
1270 else
1271 {
1272 VMCPU_CMPXCHG_STATE(pVCpu, VMCPUSTATE_STARTED_EXEC_NEM, VMCPUSTATE_STARTED_EXEC_NEM_CANCELED);
1273 LogFlow(("NEM/%u: returning immediately because canceled\n", pVCpu->idCpu));
1274 return VINF_SUCCESS;
1275 }
1276
1277 VBOXSTRICTRC rcStrict;
1278#if 0
1279 if ( !pVCpu->nem.s.fUseDebugLoop
1280 && !nemR3DarwinAnyExpensiveProbesEnabled()
1281 && !DBGFIsStepping(pVCpu)
1282 && !pVCpu->CTX_SUFF(pVM)->dbgf.ro.cEnabledInt3Breakpoints)
1283#endif
1284 rcStrict = nemR3DarwinRunGuestNormal(pVM, pVCpu);
1285#if 0
1286 else
1287 rcStrict = nemR3DarwinRunGuestDebug(pVM, pVCpu);
1288#endif
1289
1290 if (rcStrict == VINF_EM_RAW_TO_R3)
1291 rcStrict = VINF_SUCCESS;
1292
1293 /*
1294 * Convert any pending HM events back to TRPM due to premature exits.
1295 *
1296 * This is because execution may continue from IEM and we would need to inject
1297 * the event from there (hence place it back in TRPM).
1298 */
1299 if (pVCpu->nem.s.fEventPending)
1300 {
1301 /** @todo */
1302 }
1303
1304
1305 if (!VMCPU_CMPXCHG_STATE(pVCpu, VMCPUSTATE_STARTED, VMCPUSTATE_STARTED_EXEC_NEM))
1306 VMCPU_CMPXCHG_STATE(pVCpu, VMCPUSTATE_STARTED, VMCPUSTATE_STARTED_EXEC_NEM_CANCELED);
1307
1308 if (pVCpu->cpum.GstCtx.fExtrn & (CPUMCTX_EXTRN_ALL))
1309 {
1310 /* Try anticipate what we might need. */
1311 uint64_t fImport = NEM_DARWIN_CPUMCTX_EXTRN_MASK_FOR_IEM;
1312 if ( (rcStrict >= VINF_EM_FIRST && rcStrict <= VINF_EM_LAST)
1313 || RT_FAILURE(rcStrict))
1314 fImport = CPUMCTX_EXTRN_ALL;
1315 else if (VMCPU_FF_IS_ANY_SET(pVCpu, VMCPU_FF_INTERRUPT_IRQ | VMCPU_FF_INTERRUPT_FIQ
1316 | VMCPU_FF_INTERRUPT_NMI | VMCPU_FF_INTERRUPT_SMI))
1317 fImport |= IEM_CPUMCTX_EXTRN_XCPT_MASK;
1318
1319 if (pVCpu->cpum.GstCtx.fExtrn & fImport)
1320 {
1321 /* Only import what is external currently. */
1322 int rc2 = nemR3DarwinCopyStateFromHv(pVM, pVCpu, fImport);
1323 if (RT_SUCCESS(rc2))
1324 pVCpu->cpum.GstCtx.fExtrn &= ~fImport;
1325 else if (RT_SUCCESS(rcStrict))
1326 rcStrict = rc2;
1327 if (!(pVCpu->cpum.GstCtx.fExtrn & CPUMCTX_EXTRN_ALL))
1328 pVCpu->cpum.GstCtx.fExtrn = 0;
1329 STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatImportOnReturn);
1330 }
1331 else
1332 STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatImportOnReturnSkipped);
1333 }
1334 else
1335 {
1336 STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatImportOnReturnSkipped);
1337 pVCpu->cpum.GstCtx.fExtrn = 0;
1338 }
1339
1340 return rcStrict;
1341}
1342
1343
1344VMMR3_INT_DECL(bool) NEMR3CanExecuteGuest(PVM pVM, PVMCPU pVCpu)
1345{
1346 RT_NOREF(pVM, pVCpu);
1347 return true; /** @todo Are there any cases where we have to emulate? */
1348}
1349
1350
1351bool nemR3NativeSetSingleInstruction(PVM pVM, PVMCPU pVCpu, bool fEnable)
1352{
1353 VMCPU_ASSERT_EMT(pVCpu);
1354 bool fOld = pVCpu->nem.s.fSingleInstruction;
1355 pVCpu->nem.s.fSingleInstruction = fEnable;
1356 pVCpu->nem.s.fUseDebugLoop = fEnable || pVM->nem.s.fUseDebugLoop;
1357 return fOld;
1358}
1359
1360
1361void nemR3NativeNotifyFF(PVM pVM, PVMCPU pVCpu, uint32_t fFlags)
1362{
1363 LogFlowFunc(("pVM=%p pVCpu=%p fFlags=%#x\n", pVM, pVCpu, fFlags));
1364
1365 RT_NOREF(pVM, fFlags);
1366
1367 hv_return_t hrc = hv_vcpus_exit(&pVCpu->nem.s.hVCpu, 1);
1368 if (hrc != HV_SUCCESS)
1369 LogRel(("NEM: hv_vcpus_exit(%u, 1) failed with %#x\n", pVCpu->nem.s.hVCpu, hrc));
1370}
1371
1372
1373DECLHIDDEN(bool) nemR3NativeNotifyDebugEventChanged(PVM pVM, bool fUseDebugLoop)
1374{
1375 RT_NOREF(pVM, fUseDebugLoop);
1376 AssertReleaseFailed();
1377 return false;
1378}
1379
1380
1381DECLHIDDEN(bool) nemR3NativeNotifyDebugEventChangedPerCpu(PVM pVM, PVMCPU pVCpu, bool fUseDebugLoop)
1382{
1383 RT_NOREF(pVM, pVCpu, fUseDebugLoop);
1384 return fUseDebugLoop;
1385}
1386
1387
1388VMMR3_INT_DECL(int) NEMR3NotifyPhysRamRegister(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb, void *pvR3,
1389 uint8_t *pu2State, uint32_t *puNemRange)
1390{
1391 RT_NOREF(pVM, puNemRange);
1392
1393 Log5(("NEMR3NotifyPhysRamRegister: %RGp LB %RGp, pvR3=%p\n", GCPhys, cb, pvR3));
1394#if defined(VBOX_WITH_PGM_NEM_MODE)
1395 if (pvR3)
1396 {
1397 int rc = nemR3DarwinMap(pVM, GCPhys, pvR3, cb, NEM_PAGE_PROT_READ | NEM_PAGE_PROT_WRITE | NEM_PAGE_PROT_EXECUTE, pu2State);
1398 if (RT_FAILURE(rc))
1399 {
1400 LogRel(("NEMR3NotifyPhysRamRegister: GCPhys=%RGp LB %RGp pvR3=%p rc=%Rrc\n", GCPhys, cb, pvR3, rc));
1401 return VERR_NEM_MAP_PAGES_FAILED;
1402 }
1403 }
1404 return VINF_SUCCESS;
1405#else
1406 RT_NOREF(pVM, GCPhys, cb, pvR3);
1407 return VERR_NEM_MAP_PAGES_FAILED;
1408#endif
1409}
1410
1411
1412VMMR3_INT_DECL(bool) NEMR3IsMmio2DirtyPageTrackingSupported(PVM pVM)
1413{
1414 RT_NOREF(pVM);
1415 return false;
1416}
1417
1418
1419VMMR3_INT_DECL(int) NEMR3NotifyPhysMmioExMapEarly(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb, uint32_t fFlags,
1420 void *pvRam, void *pvMmio2, uint8_t *pu2State, uint32_t *puNemRange)
1421{
1422 RT_NOREF(pVM, puNemRange, pvRam, fFlags);
1423
1424 Log5(("NEMR3NotifyPhysMmioExMapEarly: %RGp LB %RGp fFlags=%#x pvRam=%p pvMmio2=%p pu2State=%p (%d)\n",
1425 GCPhys, cb, fFlags, pvRam, pvMmio2, pu2State, *pu2State));
1426
1427#if defined(VBOX_WITH_PGM_NEM_MODE)
1428 /*
1429 * Unmap the RAM we're replacing.
1430 */
1431 if (fFlags & NEM_NOTIFY_PHYS_MMIO_EX_F_REPLACE)
1432 {
1433 int rc = nemR3DarwinUnmap(pVM, GCPhys, cb, pu2State);
1434 if (RT_SUCCESS(rc))
1435 { /* likely */ }
1436 else if (pvMmio2)
1437 LogRel(("NEMR3NotifyPhysMmioExMapEarly: GCPhys=%RGp LB %RGp fFlags=%#x: Unmap -> rc=%Rc(ignored)\n",
1438 GCPhys, cb, fFlags, rc));
1439 else
1440 {
1441 LogRel(("NEMR3NotifyPhysMmioExMapEarly: GCPhys=%RGp LB %RGp fFlags=%#x: Unmap -> rc=%Rrc\n",
1442 GCPhys, cb, fFlags, rc));
1443 return VERR_NEM_UNMAP_PAGES_FAILED;
1444 }
1445 }
1446
1447 /*
1448 * Map MMIO2 if any.
1449 */
1450 if (pvMmio2)
1451 {
1452 Assert(fFlags & NEM_NOTIFY_PHYS_MMIO_EX_F_MMIO2);
1453 int rc = nemR3DarwinMap(pVM, GCPhys, pvMmio2, cb, NEM_PAGE_PROT_READ | NEM_PAGE_PROT_WRITE | NEM_PAGE_PROT_EXECUTE, pu2State);
1454 if (RT_FAILURE(rc))
1455 {
1456 LogRel(("NEMR3NotifyPhysMmioExMapEarly: GCPhys=%RGp LB %RGp fFlags=%#x pvMmio2=%p: Map -> rc=%Rrc\n",
1457 GCPhys, cb, fFlags, pvMmio2, rc));
1458 return VERR_NEM_MAP_PAGES_FAILED;
1459 }
1460 }
1461 else
1462 Assert(!(fFlags & NEM_NOTIFY_PHYS_MMIO_EX_F_MMIO2));
1463
1464#else
1465 RT_NOREF(pVM, GCPhys, cb, pvRam, pvMmio2);
1466 *pu2State = (fFlags & NEM_NOTIFY_PHYS_MMIO_EX_F_REPLACE) ? UINT8_MAX : NEM_DARWIN_PAGE_STATE_UNMAPPED;
1467#endif
1468 return VINF_SUCCESS;
1469}
1470
1471
1472VMMR3_INT_DECL(int) NEMR3NotifyPhysMmioExMapLate(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb, uint32_t fFlags,
1473 void *pvRam, void *pvMmio2, uint32_t *puNemRange)
1474{
1475 RT_NOREF(pVM, GCPhys, cb, fFlags, pvRam, pvMmio2, puNemRange);
1476 return VINF_SUCCESS;
1477}
1478
1479
1480VMMR3_INT_DECL(int) NEMR3NotifyPhysMmioExUnmap(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb, uint32_t fFlags, void *pvRam,
1481 void *pvMmio2, uint8_t *pu2State, uint32_t *puNemRange)
1482{
1483 RT_NOREF(pVM, puNemRange);
1484
1485 Log5(("NEMR3NotifyPhysMmioExUnmap: %RGp LB %RGp fFlags=%#x pvRam=%p pvMmio2=%p pu2State=%p uNemRange=%#x (%#x)\n",
1486 GCPhys, cb, fFlags, pvRam, pvMmio2, pu2State, puNemRange, *puNemRange));
1487
1488 int rc = VINF_SUCCESS;
1489#if defined(VBOX_WITH_PGM_NEM_MODE)
1490 /*
1491 * Unmap the MMIO2 pages.
1492 */
1493 /** @todo If we implement aliasing (MMIO2 page aliased into MMIO range),
1494 * we may have more stuff to unmap even in case of pure MMIO... */
1495 if (fFlags & NEM_NOTIFY_PHYS_MMIO_EX_F_MMIO2)
1496 {
1497 rc = nemR3DarwinUnmap(pVM, GCPhys, cb, pu2State);
1498 if (RT_FAILURE(rc))
1499 {
1500 LogRel2(("NEMR3NotifyPhysMmioExUnmap: GCPhys=%RGp LB %RGp fFlags=%#x: Unmap -> rc=%Rrc\n",
1501 GCPhys, cb, fFlags, rc));
1502 rc = VERR_NEM_UNMAP_PAGES_FAILED;
1503 }
1504 }
1505
1506 /* Ensure the page is masked as unmapped if relevant. */
1507 Assert(!pu2State || *pu2State == NEM_DARWIN_PAGE_STATE_UNMAPPED);
1508
1509 /*
1510 * Restore the RAM we replaced.
1511 */
1512 if (fFlags & NEM_NOTIFY_PHYS_MMIO_EX_F_REPLACE)
1513 {
1514 AssertPtr(pvRam);
1515 rc = nemR3DarwinMap(pVM, GCPhys, pvRam, cb, NEM_PAGE_PROT_READ | NEM_PAGE_PROT_WRITE | NEM_PAGE_PROT_EXECUTE, pu2State);
1516 if (RT_SUCCESS(rc))
1517 { /* likely */ }
1518 else
1519 {
1520 LogRel(("NEMR3NotifyPhysMmioExUnmap: GCPhys=%RGp LB %RGp pvMmio2=%p rc=%Rrc\n", GCPhys, cb, pvMmio2, rc));
1521 rc = VERR_NEM_MAP_PAGES_FAILED;
1522 }
1523 }
1524
1525 RT_NOREF(pvMmio2);
1526#else
1527 RT_NOREF(pVM, GCPhys, cb, fFlags, pvRam, pvMmio2, pu2State);
1528 if (pu2State)
1529 *pu2State = UINT8_MAX;
1530 rc = VERR_NEM_UNMAP_PAGES_FAILED;
1531#endif
1532 return rc;
1533}
1534
1535
1536VMMR3_INT_DECL(int) NEMR3PhysMmio2QueryAndResetDirtyBitmap(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb, uint32_t uNemRange,
1537 void *pvBitmap, size_t cbBitmap)
1538{
1539 RT_NOREF(pVM, GCPhys, cb, uNemRange, pvBitmap, cbBitmap);
1540 AssertReleaseFailed();
1541 return VERR_NOT_IMPLEMENTED;
1542}
1543
1544
1545VMMR3_INT_DECL(int) NEMR3NotifyPhysRomRegisterEarly(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb, void *pvPages, uint32_t fFlags,
1546 uint8_t *pu2State, uint32_t *puNemRange)
1547{
1548 RT_NOREF(pVM, GCPhys, cb, pvPages, fFlags, puNemRange);
1549
1550 Log5(("nemR3NativeNotifyPhysRomRegisterEarly: %RGp LB %RGp pvPages=%p fFlags=%#x\n", GCPhys, cb, pvPages, fFlags));
1551 *pu2State = UINT8_MAX;
1552 *puNemRange = 0;
1553 return VINF_SUCCESS;
1554}
1555
1556
1557VMMR3_INT_DECL(int) NEMR3NotifyPhysRomRegisterLate(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb, void *pvPages,
1558 uint32_t fFlags, uint8_t *pu2State, uint32_t *puNemRange)
1559{
1560 Log5(("nemR3NativeNotifyPhysRomRegisterLate: %RGp LB %RGp pvPages=%p fFlags=%#x pu2State=%p (%d) puNemRange=%p (%#x)\n",
1561 GCPhys, cb, pvPages, fFlags, pu2State, *pu2State, puNemRange, *puNemRange));
1562 *pu2State = UINT8_MAX;
1563
1564#if defined(VBOX_WITH_PGM_NEM_MODE)
1565 /*
1566 * (Re-)map readonly.
1567 */
1568 AssertPtrReturn(pvPages, VERR_INVALID_POINTER);
1569 int rc = nemR3DarwinMap(pVM, GCPhys, pvPages, cb, NEM_PAGE_PROT_READ | NEM_PAGE_PROT_EXECUTE, pu2State);
1570 if (RT_FAILURE(rc))
1571 {
1572 LogRel(("nemR3NativeNotifyPhysRomRegisterLate: GCPhys=%RGp LB %RGp pvPages=%p fFlags=%#x rc=%Rrc\n",
1573 GCPhys, cb, pvPages, fFlags, rc));
1574 return VERR_NEM_MAP_PAGES_FAILED;
1575 }
1576 RT_NOREF(fFlags, puNemRange);
1577 return VINF_SUCCESS;
1578#else
1579 RT_NOREF(pVM, GCPhys, cb, pvPages, fFlags, puNemRange);
1580 return VERR_NEM_MAP_PAGES_FAILED;
1581#endif
1582}
1583
1584
1585VMM_INT_DECL(void) NEMHCNotifyHandlerPhysicalDeregister(PVMCC pVM, PGMPHYSHANDLERKIND enmKind, RTGCPHYS GCPhys, RTGCPHYS cb,
1586 RTR3PTR pvMemR3, uint8_t *pu2State)
1587{
1588 RT_NOREF(pVM);
1589
1590 Log5(("NEMHCNotifyHandlerPhysicalDeregister: %RGp LB %RGp enmKind=%d pvMemR3=%p pu2State=%p (%d)\n",
1591 GCPhys, cb, enmKind, pvMemR3, pu2State, *pu2State));
1592
1593 *pu2State = UINT8_MAX;
1594#if defined(VBOX_WITH_PGM_NEM_MODE)
1595 if (pvMemR3)
1596 {
1597 int rc = nemR3DarwinMap(pVM, GCPhys, pvMemR3, cb, NEM_PAGE_PROT_READ | NEM_PAGE_PROT_WRITE | NEM_PAGE_PROT_EXECUTE, pu2State);
1598 AssertLogRelMsgRC(rc, ("NEMHCNotifyHandlerPhysicalDeregister: nemR3DarwinMap(,%p,%RGp,%RGp,) -> %Rrc\n",
1599 pvMemR3, GCPhys, cb, rc));
1600 }
1601 RT_NOREF(enmKind);
1602#else
1603 RT_NOREF(pVM, enmKind, GCPhys, cb, pvMemR3);
1604 AssertFailed();
1605#endif
1606}
1607
1608
1609VMMR3_INT_DECL(void) NEMR3NotifySetA20(PVMCPU pVCpu, bool fEnabled)
1610{
1611 Log(("NEMR3NotifySetA20: fEnabled=%RTbool\n", fEnabled));
1612 RT_NOREF(pVCpu, fEnabled);
1613}
1614
1615
1616void nemHCNativeNotifyHandlerPhysicalRegister(PVMCC pVM, PGMPHYSHANDLERKIND enmKind, RTGCPHYS GCPhys, RTGCPHYS cb)
1617{
1618 Log5(("nemHCNativeNotifyHandlerPhysicalRegister: %RGp LB %RGp enmKind=%d\n", GCPhys, cb, enmKind));
1619 NOREF(pVM); NOREF(enmKind); NOREF(GCPhys); NOREF(cb);
1620}
1621
1622
1623void nemHCNativeNotifyHandlerPhysicalModify(PVMCC pVM, PGMPHYSHANDLERKIND enmKind, RTGCPHYS GCPhysOld,
1624 RTGCPHYS GCPhysNew, RTGCPHYS cb, bool fRestoreAsRAM)
1625{
1626 Log5(("nemHCNativeNotifyHandlerPhysicalModify: %RGp LB %RGp -> %RGp enmKind=%d fRestoreAsRAM=%d\n",
1627 GCPhysOld, cb, GCPhysNew, enmKind, fRestoreAsRAM));
1628 NOREF(pVM); NOREF(enmKind); NOREF(GCPhysOld); NOREF(GCPhysNew); NOREF(cb); NOREF(fRestoreAsRAM);
1629}
1630
1631
1632int nemHCNativeNotifyPhysPageAllocated(PVMCC pVM, RTGCPHYS GCPhys, RTHCPHYS HCPhys, uint32_t fPageProt,
1633 PGMPAGETYPE enmType, uint8_t *pu2State)
1634{
1635 Log5(("nemHCNativeNotifyPhysPageAllocated: %RGp HCPhys=%RHp fPageProt=%#x enmType=%d *pu2State=%d\n",
1636 GCPhys, HCPhys, fPageProt, enmType, *pu2State));
1637 RT_NOREF(HCPhys, fPageProt, enmType);
1638
1639 return nemR3DarwinUnmap(pVM, GCPhys, GUEST_PAGE_SIZE, pu2State);
1640}
1641
1642
1643VMM_INT_DECL(void) NEMHCNotifyPhysPageProtChanged(PVMCC pVM, RTGCPHYS GCPhys, RTHCPHYS HCPhys, RTR3PTR pvR3, uint32_t fPageProt,
1644 PGMPAGETYPE enmType, uint8_t *pu2State)
1645{
1646 Log5(("NEMHCNotifyPhysPageProtChanged: %RGp HCPhys=%RHp fPageProt=%#x enmType=%d *pu2State=%d\n",
1647 GCPhys, HCPhys, fPageProt, enmType, *pu2State));
1648 RT_NOREF(HCPhys, pvR3, fPageProt, enmType)
1649
1650 nemR3DarwinUnmap(pVM, GCPhys, GUEST_PAGE_SIZE, pu2State);
1651}
1652
1653
1654VMM_INT_DECL(void) NEMHCNotifyPhysPageChanged(PVMCC pVM, RTGCPHYS GCPhys, RTHCPHYS HCPhysPrev, RTHCPHYS HCPhysNew,
1655 RTR3PTR pvNewR3, uint32_t fPageProt, PGMPAGETYPE enmType, uint8_t *pu2State)
1656{
1657 Log5(("NEMHCNotifyPhysPageChanged: %RGp HCPhys=%RHp->%RHp fPageProt=%#x enmType=%d *pu2State=%d\n",
1658 GCPhys, HCPhysPrev, HCPhysNew, fPageProt, enmType, *pu2State));
1659 RT_NOREF(HCPhysPrev, HCPhysNew, pvNewR3, fPageProt, enmType);
1660
1661 nemR3DarwinUnmap(pVM, GCPhys, GUEST_PAGE_SIZE, pu2State);
1662}
1663
1664
1665/**
1666 * Interface for importing state on demand (used by IEM).
1667 *
1668 * @returns VBox status code.
1669 * @param pVCpu The cross context CPU structure.
1670 * @param fWhat What to import, CPUMCTX_EXTRN_XXX.
1671 */
1672VMM_INT_DECL(int) NEMImportStateOnDemand(PVMCPUCC pVCpu, uint64_t fWhat)
1673{
1674 LogFlowFunc(("pVCpu=%p fWhat=%RX64\n", pVCpu, fWhat));
1675 STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatImportOnDemand);
1676
1677 return nemR3DarwinCopyStateFromHv(pVCpu->pVMR3, pVCpu, fWhat);
1678}
1679
1680
1681/**
1682 * Query the CPU tick counter and optionally the TSC_AUX MSR value.
1683 *
1684 * @returns VBox status code.
1685 * @param pVCpu The cross context CPU structure.
1686 * @param pcTicks Where to return the CPU tick count.
1687 * @param puAux Where to return the TSC_AUX register value.
1688 */
1689VMM_INT_DECL(int) NEMHCQueryCpuTick(PVMCPUCC pVCpu, uint64_t *pcTicks, uint32_t *puAux)
1690{
1691 LogFlowFunc(("pVCpu=%p pcTicks=%RX64 puAux=%RX32\n", pVCpu, pcTicks, puAux));
1692 STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatQueryCpuTick);
1693
1694 AssertReleaseFailed();
1695 return VERR_NOT_IMPLEMENTED;
1696}
1697
1698
1699/**
1700 * Resumes CPU clock (TSC) on all virtual CPUs.
1701 *
1702 * This is called by TM when the VM is started, restored, resumed or similar.
1703 *
1704 * @returns VBox status code.
1705 * @param pVM The cross context VM structure.
1706 * @param pVCpu The cross context CPU structure of the calling EMT.
1707 * @param uPausedTscValue The TSC value at the time of pausing.
1708 */
1709VMM_INT_DECL(int) NEMHCResumeCpuTickOnAll(PVMCC pVM, PVMCPUCC pVCpu, uint64_t uPausedTscValue)
1710{
1711 LogFlowFunc(("pVM=%p pVCpu=%p uPausedTscValue=%RX64\n", pVCpu, uPausedTscValue));
1712 VMCPU_ASSERT_EMT_RETURN(pVCpu, VERR_VM_THREAD_NOT_EMT);
1713 AssertReturn(VM_IS_NEM_ENABLED(pVM), VERR_NEM_IPE_9);
1714
1715 //AssertReleaseFailed();
1716 return VINF_SUCCESS;
1717}
1718
1719
1720/**
1721 * Returns features supported by the NEM backend.
1722 *
1723 * @returns Flags of features supported by the native NEM backend.
1724 * @param pVM The cross context VM structure.
1725 */
1726VMM_INT_DECL(uint32_t) NEMHCGetFeatures(PVMCC pVM)
1727{
1728 RT_NOREF(pVM);
1729 /*
1730 * Apple's Hypervisor.framework is not supported if the CPU doesn't support nested paging
1731 * and unrestricted guest execution support so we can safely return these flags here always.
1732 */
1733 return NEM_FEAT_F_NESTED_PAGING | NEM_FEAT_F_FULL_GST_EXEC | NEM_FEAT_F_XSAVE_XRSTOR;
1734}
1735
1736
1737/** @page pg_nem_darwin NEM/darwin - Native Execution Manager, macOS.
1738 *
1739 * @todo Add notes as the implementation progresses...
1740 */
1741
Note: See TracBrowser for help on using the repository browser.

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