VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/NEMR3Native-linux-armv8.cpp@ 104336

Last change on this file since 104336 was 104336, checked in by vboxsync, 9 months ago

VMM/NEM: Skeleton of backend for linux.arm64 using KVM, able to execute the UEFI up to showing the logo (but not more), bugref:10391 [build fix]

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 74.4 KB
Line 
1/* $Id: NEMR3Native-linux-armv8.cpp 104336 2024-04-14 17:01:32Z vboxsync $ */
2/** @file
3 * NEM - Native execution manager, native ring-3 Linux backend arm64 version.
4 */
5
6/*
7 * Copyright (C) 2024 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28
29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
32#define LOG_GROUP LOG_GROUP_NEM
33#define VMCPU_INCL_CPUM_GST_CTX
34#include <VBox/vmm/nem.h>
35#include <VBox/vmm/iem.h>
36#include <VBox/vmm/em.h>
37#include <VBox/vmm/apic.h>
38#include <VBox/vmm/pdm.h>
39#include <VBox/vmm/trpm.h>
40#include "NEMInternal.h"
41#include <VBox/vmm/vmcc.h>
42
43#include <iprt/alloca.h>
44#include <iprt/string.h>
45#include <iprt/system.h>
46#include <iprt/armv8.h>
47
48#include <errno.h>
49#include <unistd.h>
50#include <sys/ioctl.h>
51#include <sys/fcntl.h>
52#include <sys/mman.h>
53#include <linux/kvm.h>
54
55/** @note This is an experiment right now and therefore is separate from the amd64 KVM NEM backend
56 * We'll see whether it would make sense to merge things later on when things have settled.
57 */
58
59/** Core register group. */
60#define KVM_ARM64_REG_CORE_GROUP UINT64_C(0x6030000000100000)
61/** System register group. */
62#define KVM_ARM64_REG_SYS_GROUP UINT64_C(0x6030000000130000)
63/** System register group. */
64#define KVM_ARM64_REG_SIMD_GROUP UINT64_C(0x6040000000100050)
65/** FP register group. */
66#define KVM_ARM64_REG_FP_GROUP UINT64_C(0x6020000000100000)
67
68#define KVM_ARM64_REG_CORE_CREATE(a_idReg) (KVM_ARM64_REG_CORE_GROUP | ((uint64_t)(a_idReg) & 0xffff))
69#define KVM_ARM64_REG_GPR(a_iGpr) KVM_ARM64_REG_CORE_CREATE((a_iGpr) << 1)
70#define KVM_ARM64_REG_SP_EL0 KVM_ARM64_REG_CORE_CREATE(0x3e)
71#define KVM_ARM64_REG_PC KVM_ARM64_REG_CORE_CREATE(0x40)
72#define KVM_ARM64_REG_PSTATE KVM_ARM64_REG_CORE_CREATE(0x42)
73#define KVM_ARM64_REG_SP_EL1 KVM_ARM64_REG_CORE_CREATE(0x44)
74#define KVM_ARM64_REG_ELR_EL1 KVM_ARM64_REG_CORE_CREATE(0x46)
75#define KVM_ARM64_REG_SPSR_EL1 KVM_ARM64_REG_CORE_CREATE(0x48)
76#define KVM_ARM64_REG_SPSR_ABT KVM_ARM64_REG_CORE_CREATE(0x4a)
77#define KVM_ARM64_REG_SPSR_UND KVM_ARM64_REG_CORE_CREATE(0x4c)
78#define KVM_ARM64_REG_SPSR_IRQ KVM_ARM64_REG_CORE_CREATE(0x4e)
79#define KVM_ARM64_REG_SPSR_FIQ KVM_ARM64_REG_CORE_CREATE(0x50)
80
81/** This maps to our IPRT representation of system register IDs, yay! */
82#define KVM_ARM64_REG_SYS_CREATE(a_idSysReg) (KVM_ARM64_REG_SYS_GROUP | ((uint64_t)(a_idSysReg) & 0xffff))
83
84#define KVM_ARM64_REG_SIMD_CREATE(a_iVecReg) (KVM_ARM64_REG_SIMD_GROUP | (((uint64_t)(a_idReg) << 2) & 0xffff))
85
86#define KVM_ARM64_REG_FP_CREATE(a_idReg) (KVM_ARM64_REG_FP_GROUP | ((uint64_t)(a_idReg) & 0xffff))
87#define KVM_ARM64_REG_FP_FPSR KVM_ARM64_REG_FP_CREATE(0xd4)
88#define KVM_ARM64_REG_FP_FPCR KVM_ARM64_REG_FP_CREATE(0xd5)
89
90
91/** ID registers. */
92static const struct
93{
94 uint64_t idKvmReg;
95 uint32_t offIdStruct;
96} s_aIdRegs[] =
97{
98 { KVM_ARM64_REG_SYS_CREATE(ARMV8_AARCH64_SYSREG_ID_AA64DFR0_EL1), RT_UOFFSETOF(CPUMIDREGS, u64RegIdAa64Dfr0El1) },
99 { KVM_ARM64_REG_SYS_CREATE(ARMV8_AARCH64_SYSREG_ID_AA64DFR1_EL1), RT_UOFFSETOF(CPUMIDREGS, u64RegIdAa64Dfr1El1) },
100 { KVM_ARM64_REG_SYS_CREATE(ARMV8_AARCH64_SYSREG_ID_AA64ISAR0_EL1), RT_UOFFSETOF(CPUMIDREGS, u64RegIdAa64Isar0El1) },
101 { KVM_ARM64_REG_SYS_CREATE(ARMV8_AARCH64_SYSREG_ID_AA64ISAR1_EL1), RT_UOFFSETOF(CPUMIDREGS, u64RegIdAa64Isar1El1) },
102 { KVM_ARM64_REG_SYS_CREATE(ARMV8_AARCH64_SYSREG_ID_AA64MMFR0_EL1), RT_UOFFSETOF(CPUMIDREGS, u64RegIdAa64Mmfr0El1) },
103 { KVM_ARM64_REG_SYS_CREATE(ARMV8_AARCH64_SYSREG_ID_AA64MMFR1_EL1), RT_UOFFSETOF(CPUMIDREGS, u64RegIdAa64Mmfr1El1) },
104 { KVM_ARM64_REG_SYS_CREATE(ARMV8_AARCH64_SYSREG_ID_AA64MMFR2_EL1), RT_UOFFSETOF(CPUMIDREGS, u64RegIdAa64Mmfr2El1) },
105 { KVM_ARM64_REG_SYS_CREATE(ARMV8_AARCH64_SYSREG_ID_AA64PFR0_EL1), RT_UOFFSETOF(CPUMIDREGS, u64RegIdAa64Pfr0El1) },
106 { KVM_ARM64_REG_SYS_CREATE(ARMV8_AARCH64_SYSREG_ID_AA64PFR1_EL1), RT_UOFFSETOF(CPUMIDREGS, u64RegIdAa64Pfr1El1) }
107};
108
109
110/**
111 * Worker for nemR3NativeInit that gets the hypervisor capabilities.
112 *
113 * @returns VBox status code.
114 * @param pVM The cross context VM structure.
115 * @param pErrInfo Where to always return error info.
116 */
117static int nemR3LnxInitCheckCapabilities(PVM pVM, PRTERRINFO pErrInfo)
118{
119 AssertReturn(pVM->nem.s.fdKvm != -1, RTErrInfoSet(pErrInfo, VERR_WRONG_ORDER, "Wrong initalization order"));
120
121 /*
122 * Capabilities.
123 */
124 static const struct
125 {
126 const char *pszName;
127 int iCap;
128 uint32_t offNem : 24;
129 uint32_t cbNem : 3;
130 uint32_t fReqNonZero : 1;
131 uint32_t uReserved : 4;
132 } s_aCaps[] =
133 {
134#define CAP_ENTRY__L(a_Define) { #a_Define, a_Define, UINT32_C(0x00ffffff), 0, 0, 0 }
135#define CAP_ENTRY__S(a_Define, a_Member) { #a_Define, a_Define, RT_UOFFSETOF(NEM, a_Member), RT_SIZEOFMEMB(NEM, a_Member), 0, 0 }
136#define CAP_ENTRY_MS(a_Define, a_Member) { #a_Define, a_Define, RT_UOFFSETOF(NEM, a_Member), RT_SIZEOFMEMB(NEM, a_Member), 1, 0 }
137#define CAP_ENTRY__U(a_Number) { "KVM_CAP_" #a_Number, a_Number, UINT32_C(0x00ffffff), 0, 0, 0 }
138#define CAP_ENTRY_ML(a_Number) { "KVM_CAP_" #a_Number, a_Number, UINT32_C(0x00ffffff), 0, 1, 0 }
139
140 CAP_ENTRY__L(KVM_CAP_IRQCHIP), /* 0 */
141 CAP_ENTRY__L(KVM_CAP_HLT),
142 CAP_ENTRY__L(KVM_CAP_MMU_SHADOW_CACHE_CONTROL),
143 CAP_ENTRY_ML(KVM_CAP_USER_MEMORY),
144 CAP_ENTRY__L(KVM_CAP_SET_TSS_ADDR),
145 CAP_ENTRY__U(5),
146 CAP_ENTRY__L(KVM_CAP_VAPIC),
147 CAP_ENTRY__L(KVM_CAP_EXT_CPUID),
148 CAP_ENTRY__L(KVM_CAP_CLOCKSOURCE),
149 CAP_ENTRY__L(KVM_CAP_NR_VCPUS),
150 CAP_ENTRY_MS(KVM_CAP_NR_MEMSLOTS, cMaxMemSlots), /* 10 */
151 CAP_ENTRY__L(KVM_CAP_PIT),
152 CAP_ENTRY__L(KVM_CAP_NOP_IO_DELAY),
153 CAP_ENTRY__L(KVM_CAP_PV_MMU),
154 CAP_ENTRY__L(KVM_CAP_MP_STATE),
155 CAP_ENTRY__L(KVM_CAP_COALESCED_MMIO),
156 CAP_ENTRY__L(KVM_CAP_SYNC_MMU),
157 CAP_ENTRY__U(17),
158 CAP_ENTRY__L(KVM_CAP_IOMMU),
159 CAP_ENTRY__U(19), /* Buggy KVM_CAP_JOIN_MEMORY_REGIONS? */
160 CAP_ENTRY__U(20), /* Mon-working KVM_CAP_DESTROY_MEMORY_REGION? */
161 CAP_ENTRY__L(KVM_CAP_DESTROY_MEMORY_REGION_WORKS), /* 21 */
162 CAP_ENTRY__L(KVM_CAP_USER_NMI),
163#ifdef __KVM_HAVE_GUEST_DEBUG
164 CAP_ENTRY__L(KVM_CAP_SET_GUEST_DEBUG),
165#endif
166#ifdef __KVM_HAVE_PIT
167 CAP_ENTRY__L(KVM_CAP_REINJECT_CONTROL),
168#endif
169 CAP_ENTRY__L(KVM_CAP_IRQ_ROUTING),
170 CAP_ENTRY__L(KVM_CAP_IRQ_INJECT_STATUS),
171 CAP_ENTRY__U(27),
172 CAP_ENTRY__U(28),
173 CAP_ENTRY__L(KVM_CAP_ASSIGN_DEV_IRQ),
174 CAP_ENTRY__L(KVM_CAP_JOIN_MEMORY_REGIONS_WORKS), /* 30 */
175#ifdef __KVM_HAVE_MCE
176 CAP_ENTRY__L(KVM_CAP_MCE),
177#endif
178 CAP_ENTRY__L(KVM_CAP_IRQFD),
179#ifdef __KVM_HAVE_PIT
180 CAP_ENTRY__L(KVM_CAP_PIT2),
181#endif
182 CAP_ENTRY__L(KVM_CAP_SET_BOOT_CPU_ID),
183#ifdef __KVM_HAVE_PIT_STATE2
184 CAP_ENTRY__L(KVM_CAP_PIT_STATE2),
185#endif
186 CAP_ENTRY__L(KVM_CAP_IOEVENTFD),
187 CAP_ENTRY__L(KVM_CAP_SET_IDENTITY_MAP_ADDR),
188#ifdef __KVM_HAVE_XEN_HVM
189 CAP_ENTRY__L(KVM_CAP_XEN_HVM),
190#endif
191 CAP_ENTRY__L(KVM_CAP_ADJUST_CLOCK),
192 CAP_ENTRY__L(KVM_CAP_INTERNAL_ERROR_DATA), /* 40 */
193#ifdef __KVM_HAVE_VCPU_EVENTS
194 CAP_ENTRY_ML(KVM_CAP_VCPU_EVENTS),
195#else
196 CAP_ENTRY_MU(41),
197#endif
198 CAP_ENTRY__L(KVM_CAP_S390_PSW),
199 CAP_ENTRY__L(KVM_CAP_PPC_SEGSTATE),
200 CAP_ENTRY__L(KVM_CAP_HYPERV),
201 CAP_ENTRY__L(KVM_CAP_HYPERV_VAPIC),
202 CAP_ENTRY__L(KVM_CAP_HYPERV_SPIN),
203 CAP_ENTRY__L(KVM_CAP_PCI_SEGMENT),
204 CAP_ENTRY__L(KVM_CAP_PPC_PAIRED_SINGLES),
205 CAP_ENTRY__L(KVM_CAP_INTR_SHADOW),
206#ifdef __KVM_HAVE_DEBUGREGS
207 CAP_ENTRY__L(KVM_CAP_DEBUGREGS), /* 50 */
208#endif
209 CAP_ENTRY__S(KVM_CAP_X86_ROBUST_SINGLESTEP, fRobustSingleStep),
210 CAP_ENTRY__L(KVM_CAP_PPC_OSI),
211 CAP_ENTRY__L(KVM_CAP_PPC_UNSET_IRQ),
212 CAP_ENTRY__L(KVM_CAP_ENABLE_CAP),
213 CAP_ENTRY__L(KVM_CAP_PPC_GET_PVINFO),
214 CAP_ENTRY__L(KVM_CAP_PPC_IRQ_LEVEL),
215 CAP_ENTRY__L(KVM_CAP_ASYNC_PF),
216 CAP_ENTRY__L(KVM_CAP_TSC_CONTROL), /* 60 */
217 CAP_ENTRY__L(KVM_CAP_GET_TSC_KHZ),
218 CAP_ENTRY__L(KVM_CAP_PPC_BOOKE_SREGS),
219 CAP_ENTRY__L(KVM_CAP_SPAPR_TCE),
220 CAP_ENTRY__L(KVM_CAP_PPC_SMT),
221 CAP_ENTRY__L(KVM_CAP_PPC_RMA),
222 CAP_ENTRY__L(KVM_CAP_MAX_VCPUS),
223 CAP_ENTRY__L(KVM_CAP_PPC_HIOR),
224 CAP_ENTRY__L(KVM_CAP_PPC_PAPR),
225 CAP_ENTRY__L(KVM_CAP_SW_TLB),
226 CAP_ENTRY__L(KVM_CAP_ONE_REG), /* 70 */
227 CAP_ENTRY__L(KVM_CAP_S390_GMAP),
228 CAP_ENTRY__L(KVM_CAP_TSC_DEADLINE_TIMER),
229 CAP_ENTRY__L(KVM_CAP_S390_UCONTROL),
230 CAP_ENTRY__L(KVM_CAP_SYNC_REGS),
231 CAP_ENTRY__L(KVM_CAP_PCI_2_3),
232 CAP_ENTRY__L(KVM_CAP_KVMCLOCK_CTRL),
233 CAP_ENTRY__L(KVM_CAP_SIGNAL_MSI),
234 CAP_ENTRY__L(KVM_CAP_PPC_GET_SMMU_INFO),
235 CAP_ENTRY__L(KVM_CAP_S390_COW),
236 CAP_ENTRY__L(KVM_CAP_PPC_ALLOC_HTAB), /* 80 */
237 CAP_ENTRY__L(KVM_CAP_READONLY_MEM),
238 CAP_ENTRY__L(KVM_CAP_IRQFD_RESAMPLE),
239 CAP_ENTRY__L(KVM_CAP_PPC_BOOKE_WATCHDOG),
240 CAP_ENTRY__L(KVM_CAP_PPC_HTAB_FD),
241 CAP_ENTRY__L(KVM_CAP_S390_CSS_SUPPORT),
242 CAP_ENTRY__L(KVM_CAP_PPC_EPR),
243 CAP_ENTRY__L(KVM_CAP_ARM_PSCI),
244 CAP_ENTRY__L(KVM_CAP_ARM_SET_DEVICE_ADDR),
245 CAP_ENTRY__L(KVM_CAP_DEVICE_CTRL),
246 CAP_ENTRY__L(KVM_CAP_IRQ_MPIC), /* 90 */
247 CAP_ENTRY__L(KVM_CAP_PPC_RTAS),
248 CAP_ENTRY__L(KVM_CAP_IRQ_XICS),
249 CAP_ENTRY__L(KVM_CAP_ARM_EL1_32BIT),
250 CAP_ENTRY__L(KVM_CAP_SPAPR_MULTITCE),
251 CAP_ENTRY__L(KVM_CAP_EXT_EMUL_CPUID),
252 CAP_ENTRY__L(KVM_CAP_HYPERV_TIME),
253 CAP_ENTRY__L(KVM_CAP_IOAPIC_POLARITY_IGNORED),
254 CAP_ENTRY__L(KVM_CAP_ENABLE_CAP_VM),
255 CAP_ENTRY__L(KVM_CAP_S390_IRQCHIP),
256 CAP_ENTRY__L(KVM_CAP_IOEVENTFD_NO_LENGTH), /* 100 */
257 CAP_ENTRY__L(KVM_CAP_VM_ATTRIBUTES),
258 CAP_ENTRY__L(KVM_CAP_ARM_PSCI_0_2),
259 CAP_ENTRY__L(KVM_CAP_PPC_FIXUP_HCALL),
260 CAP_ENTRY__L(KVM_CAP_PPC_ENABLE_HCALL),
261 CAP_ENTRY__L(KVM_CAP_CHECK_EXTENSION_VM),
262 CAP_ENTRY__L(KVM_CAP_S390_USER_SIGP),
263 CAP_ENTRY__L(KVM_CAP_S390_VECTOR_REGISTERS),
264 CAP_ENTRY__L(KVM_CAP_S390_MEM_OP),
265 CAP_ENTRY__L(KVM_CAP_S390_USER_STSI),
266 CAP_ENTRY__L(KVM_CAP_S390_SKEYS), /* 110 */
267 CAP_ENTRY__L(KVM_CAP_MIPS_FPU),
268 CAP_ENTRY__L(KVM_CAP_MIPS_MSA),
269 CAP_ENTRY__L(KVM_CAP_S390_INJECT_IRQ),
270 CAP_ENTRY__L(KVM_CAP_S390_IRQ_STATE),
271 CAP_ENTRY__L(KVM_CAP_PPC_HWRNG),
272 CAP_ENTRY__L(KVM_CAP_DISABLE_QUIRKS),
273 CAP_ENTRY__L(KVM_CAP_X86_SMM),
274 CAP_ENTRY__L(KVM_CAP_MULTI_ADDRESS_SPACE),
275 CAP_ENTRY__L(KVM_CAP_GUEST_DEBUG_HW_BPS),
276 CAP_ENTRY__L(KVM_CAP_GUEST_DEBUG_HW_WPS), /* 120 */
277 CAP_ENTRY__L(KVM_CAP_SPLIT_IRQCHIP),
278 CAP_ENTRY__L(KVM_CAP_IOEVENTFD_ANY_LENGTH),
279 CAP_ENTRY__L(KVM_CAP_HYPERV_SYNIC),
280 CAP_ENTRY__L(KVM_CAP_S390_RI),
281 CAP_ENTRY__L(KVM_CAP_SPAPR_TCE_64),
282 CAP_ENTRY__L(KVM_CAP_ARM_PMU_V3),
283 CAP_ENTRY__L(KVM_CAP_VCPU_ATTRIBUTES),
284 CAP_ENTRY__L(KVM_CAP_MAX_VCPU_ID),
285 CAP_ENTRY__L(KVM_CAP_X2APIC_API),
286 CAP_ENTRY__L(KVM_CAP_S390_USER_INSTR0), /* 130 */
287 CAP_ENTRY__L(KVM_CAP_MSI_DEVID),
288 CAP_ENTRY__L(KVM_CAP_PPC_HTM),
289 CAP_ENTRY__L(KVM_CAP_SPAPR_RESIZE_HPT),
290 CAP_ENTRY__L(KVM_CAP_PPC_MMU_RADIX),
291 CAP_ENTRY__L(KVM_CAP_PPC_MMU_HASH_V3),
292 CAP_ENTRY__L(KVM_CAP_IMMEDIATE_EXIT),
293 CAP_ENTRY__L(KVM_CAP_MIPS_VZ),
294 CAP_ENTRY__L(KVM_CAP_MIPS_TE),
295 CAP_ENTRY__L(KVM_CAP_MIPS_64BIT),
296 CAP_ENTRY__L(KVM_CAP_S390_GS), /* 140 */
297 CAP_ENTRY__L(KVM_CAP_S390_AIS),
298 CAP_ENTRY__L(KVM_CAP_SPAPR_TCE_VFIO),
299 CAP_ENTRY__L(KVM_CAP_X86_DISABLE_EXITS),
300 CAP_ENTRY__L(KVM_CAP_ARM_USER_IRQ),
301 CAP_ENTRY__L(KVM_CAP_S390_CMMA_MIGRATION),
302 CAP_ENTRY__L(KVM_CAP_PPC_FWNMI),
303 CAP_ENTRY__L(KVM_CAP_PPC_SMT_POSSIBLE),
304 CAP_ENTRY__L(KVM_CAP_HYPERV_SYNIC2),
305 CAP_ENTRY__L(KVM_CAP_HYPERV_VP_INDEX),
306 CAP_ENTRY__L(KVM_CAP_S390_AIS_MIGRATION), /* 150 */
307 CAP_ENTRY__L(KVM_CAP_PPC_GET_CPU_CHAR),
308 CAP_ENTRY__L(KVM_CAP_S390_BPB),
309 CAP_ENTRY__L(KVM_CAP_GET_MSR_FEATURES),
310 CAP_ENTRY__L(KVM_CAP_HYPERV_EVENTFD),
311 CAP_ENTRY__L(KVM_CAP_HYPERV_TLBFLUSH),
312 CAP_ENTRY__L(KVM_CAP_S390_HPAGE_1M),
313 CAP_ENTRY__L(KVM_CAP_NESTED_STATE),
314 CAP_ENTRY__L(KVM_CAP_ARM_INJECT_SERROR_ESR),
315 CAP_ENTRY__L(KVM_CAP_MSR_PLATFORM_INFO),
316 CAP_ENTRY__L(KVM_CAP_PPC_NESTED_HV), /* 160 */
317 CAP_ENTRY__L(KVM_CAP_HYPERV_SEND_IPI),
318 CAP_ENTRY__L(KVM_CAP_COALESCED_PIO),
319 CAP_ENTRY__L(KVM_CAP_HYPERV_ENLIGHTENED_VMCS),
320 CAP_ENTRY__L(KVM_CAP_EXCEPTION_PAYLOAD),
321 CAP_ENTRY_MS(KVM_CAP_ARM_VM_IPA_SIZE, cIpaBits),
322 CAP_ENTRY__L(KVM_CAP_MANUAL_DIRTY_LOG_PROTECT),
323 CAP_ENTRY__L(KVM_CAP_HYPERV_CPUID),
324 CAP_ENTRY__L(KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2),
325 CAP_ENTRY__L(KVM_CAP_PPC_IRQ_XIVE),
326 CAP_ENTRY__L(KVM_CAP_ARM_SVE), /* 170 */
327 CAP_ENTRY__L(KVM_CAP_ARM_PTRAUTH_ADDRESS),
328 CAP_ENTRY__L(KVM_CAP_ARM_PTRAUTH_GENERIC),
329 CAP_ENTRY__L(KVM_CAP_PMU_EVENT_FILTER),
330 CAP_ENTRY__L(KVM_CAP_ARM_IRQ_LINE_LAYOUT_2),
331 CAP_ENTRY__L(KVM_CAP_HYPERV_DIRECT_TLBFLUSH),
332 CAP_ENTRY__L(KVM_CAP_PPC_GUEST_DEBUG_SSTEP),
333 CAP_ENTRY__L(KVM_CAP_ARM_NISV_TO_USER),
334 CAP_ENTRY__L(KVM_CAP_ARM_INJECT_EXT_DABT),
335 CAP_ENTRY__L(KVM_CAP_S390_VCPU_RESETS),
336 CAP_ENTRY__L(KVM_CAP_S390_PROTECTED), /* 180 */
337 CAP_ENTRY__L(KVM_CAP_PPC_SECURE_GUEST),
338 CAP_ENTRY__L(KVM_CAP_HALT_POLL),
339 CAP_ENTRY__L(KVM_CAP_ASYNC_PF_INT),
340 CAP_ENTRY__L(KVM_CAP_LAST_CPU),
341 CAP_ENTRY__L(KVM_CAP_SMALLER_MAXPHYADDR),
342 CAP_ENTRY__L(KVM_CAP_S390_DIAG318),
343 CAP_ENTRY__L(KVM_CAP_STEAL_TIME),
344 CAP_ENTRY__L(KVM_CAP_X86_USER_SPACE_MSR), /* (since 5.10) */
345 CAP_ENTRY__L(KVM_CAP_X86_MSR_FILTER),
346 CAP_ENTRY__L(KVM_CAP_ENFORCE_PV_FEATURE_CPUID), /* 190 */
347 CAP_ENTRY__L(KVM_CAP_SYS_HYPERV_CPUID),
348 CAP_ENTRY__L(KVM_CAP_DIRTY_LOG_RING),
349 CAP_ENTRY__L(KVM_CAP_X86_BUS_LOCK_EXIT),
350 CAP_ENTRY__L(KVM_CAP_PPC_DAWR1),
351 CAP_ENTRY__L(KVM_CAP_SET_GUEST_DEBUG2),
352 CAP_ENTRY__L(KVM_CAP_SGX_ATTRIBUTE),
353 CAP_ENTRY__L(KVM_CAP_VM_COPY_ENC_CONTEXT_FROM),
354 CAP_ENTRY__L(KVM_CAP_PTP_KVM),
355 CAP_ENTRY__U(199),
356 CAP_ENTRY__U(200),
357 CAP_ENTRY__U(201),
358 CAP_ENTRY__U(202),
359 CAP_ENTRY__U(203),
360 CAP_ENTRY__U(204),
361 CAP_ENTRY__U(205),
362 CAP_ENTRY__U(206),
363 CAP_ENTRY__U(207),
364 CAP_ENTRY__U(208),
365 CAP_ENTRY__U(209),
366 CAP_ENTRY__U(210),
367 CAP_ENTRY__U(211),
368 CAP_ENTRY__U(212),
369 CAP_ENTRY__U(213),
370 CAP_ENTRY__U(214),
371 CAP_ENTRY__U(215),
372 CAP_ENTRY__U(216),
373 };
374
375 LogRel(("NEM: KVM capabilities (system):\n"));
376 int rcRet = VINF_SUCCESS;
377 for (unsigned i = 0; i < RT_ELEMENTS(s_aCaps); i++)
378 {
379 int rc = ioctl(pVM->nem.s.fdKvm, KVM_CHECK_EXTENSION, s_aCaps[i].iCap);
380 if (rc >= 10)
381 LogRel(("NEM: %36s: %#x (%d)\n", s_aCaps[i].pszName, rc, rc));
382 else if (rc >= 0)
383 LogRel(("NEM: %36s: %d\n", s_aCaps[i].pszName, rc));
384 else
385 LogRel(("NEM: %s failed: %d/%d\n", s_aCaps[i].pszName, rc, errno));
386 switch (s_aCaps[i].cbNem)
387 {
388 case 0:
389 break;
390 case 1:
391 {
392 uint8_t *puValue = (uint8_t *)&pVM->nem.padding[s_aCaps[i].offNem];
393 AssertReturn(s_aCaps[i].offNem <= sizeof(NEM) - sizeof(*puValue), VERR_NEM_IPE_0);
394 *puValue = (uint8_t)rc;
395 AssertLogRelMsg((int)*puValue == rc, ("%s: %#x\n", s_aCaps[i].pszName, rc));
396 break;
397 }
398 case 2:
399 {
400 uint16_t *puValue = (uint16_t *)&pVM->nem.padding[s_aCaps[i].offNem];
401 AssertReturn(s_aCaps[i].offNem <= sizeof(NEM) - sizeof(*puValue), VERR_NEM_IPE_0);
402 *puValue = (uint16_t)rc;
403 AssertLogRelMsg((int)*puValue == rc, ("%s: %#x\n", s_aCaps[i].pszName, rc));
404 break;
405 }
406 case 4:
407 {
408 uint32_t *puValue = (uint32_t *)&pVM->nem.padding[s_aCaps[i].offNem];
409 AssertReturn(s_aCaps[i].offNem <= sizeof(NEM) - sizeof(*puValue), VERR_NEM_IPE_0);
410 *puValue = (uint32_t)rc;
411 AssertLogRelMsg((int)*puValue == rc, ("%s: %#x\n", s_aCaps[i].pszName, rc));
412 break;
413 }
414 default:
415 rcRet = RTErrInfoSetF(pErrInfo, VERR_NEM_IPE_0, "s_aCaps[%u] is bad: cbNem=%#x - %s",
416 i, s_aCaps[i].pszName, s_aCaps[i].cbNem);
417 AssertFailedReturn(rcRet);
418 }
419
420 /*
421 * Is a require non-zero entry zero or failing?
422 */
423 if (s_aCaps[i].fReqNonZero && rc <= 0)
424 rcRet = RTERRINFO_LOG_REL_ADD_F(pErrInfo, VERR_NEM_MISSING_FEATURE,
425 "Required capability '%s' is missing!", s_aCaps[i].pszName);
426 }
427
428 /*
429 * Get per VCpu KVM_RUN MMAP area size.
430 */
431 int rc = ioctl(pVM->nem.s.fdKvm, KVM_GET_VCPU_MMAP_SIZE, 0UL);
432 if ((unsigned)rc < _64M)
433 {
434 pVM->nem.s.cbVCpuMmap = (uint32_t)rc;
435 LogRel(("NEM: %36s: %#x (%d)\n", "KVM_GET_VCPU_MMAP_SIZE", rc, rc));
436 }
437 else if (rc < 0)
438 rcRet = RTERRINFO_LOG_REL_ADD_F(pErrInfo, VERR_NEM_MISSING_FEATURE, "KVM_GET_VCPU_MMAP_SIZE failed: %d", errno);
439 else
440 rcRet = RTERRINFO_LOG_REL_ADD_F(pErrInfo, VERR_NEM_INIT_FAILED, "Odd KVM_GET_VCPU_MMAP_SIZE value: %#x (%d)", rc, rc);
441
442 /*
443 * Init the slot ID bitmap.
444 */
445 ASMBitSet(&pVM->nem.s.bmSlotIds[0], 0); /* don't use slot 0 */
446 if (pVM->nem.s.cMaxMemSlots < _32K)
447 ASMBitSetRange(&pVM->nem.s.bmSlotIds[0], pVM->nem.s.cMaxMemSlots, _32K);
448 ASMBitSet(&pVM->nem.s.bmSlotIds[0], _32K - 1); /* don't use the last slot */
449
450 return rcRet;
451}
452
453
454/**
455 * Queries and logs the supported register list from KVM.
456 *
457 * @returns VBox status code.
458 * @param fdVCpu The file descriptor number of vCPU 0.
459 */
460static int nemR3LnxLogRegList(int fdVCpu)
461{
462 struct KVM_REG_LIST
463 {
464 uint64_t cRegs;
465 uint64_t aRegs[1024];
466 } RegList; RT_ZERO(RegList);
467
468 RegList.cRegs = RT_ELEMENTS(RegList.aRegs);
469 int rcLnx = ioctl(fdVCpu, KVM_GET_REG_LIST, &RegList);
470 if (rcLnx != 0)
471 return RTErrConvertFromErrno(errno);
472
473 LogRel(("NEM: KVM vCPU registers:\n"));
474
475 for (uint32_t i = 0; i < RegList.cRegs; i++)
476 LogRel(("NEM: %36s: %#RX64\n", "Unknown" /** @todo */, RegList.aRegs[i]));
477
478 return VINF_SUCCESS;
479}
480
481
482DECL_FORCE_INLINE(int) nemR3LnxKvmSetQueryReg(PVMCPUCC pVCpu, bool fQuery, uint64_t idKvmReg, const uint64_t *pu64)
483{
484 struct kvm_one_reg Reg;
485 Reg.id = idKvmReg;
486 Reg.addr = (uintptr_t)pu64;
487
488 /*
489 * Who thought that this API was a good idea? Supporting to query/set just one register
490 * at a time is horribly inefficient.
491 */
492 int rcLnx = ioctl(pVCpu->nem.s.fdVCpu, fQuery ? KVM_GET_ONE_REG : KVM_SET_ONE_REG, &Reg);
493 if (!rcLnx)
494 return 0;
495
496 return RTErrConvertFromErrno(-rcLnx);
497}
498
499DECLINLINE(int) nemR3LnxKvmQueryReg(PVMCPUCC pVCpu, uint64_t idKvmReg, uint64_t *pu64)
500{
501 return nemR3LnxKvmSetQueryReg(pVCpu, true /*fQuery*/, idKvmReg, pu64);
502}
503
504
505DECLINLINE(int) nemR3LnxKvmSetReg(PVMCPUCC pVCpu, uint64_t idKvmReg, const uint64_t *pu64)
506{
507 return nemR3LnxKvmSetQueryReg(pVCpu, false /*fQuery*/, idKvmReg, pu64);
508}
509
510
511/**
512 * Does the early setup of a KVM VM.
513 *
514 * @returns VBox status code.
515 * @param pVM The cross context VM structure.
516 * @param pErrInfo Where to always return error info.
517 */
518static int nemR3LnxInitSetupVm(PVM pVM, PRTERRINFO pErrInfo)
519{
520 AssertReturn(pVM->nem.s.fdVm != -1, RTErrInfoSet(pErrInfo, VERR_WRONG_ORDER, "Wrong initalization order"));
521
522 /*
523 * Create the VCpus.
524 */
525 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
526 {
527 PVMCPU pVCpu = pVM->apCpusR3[idCpu];
528
529 /* Create it. */
530 pVCpu->nem.s.fdVCpu = ioctl(pVM->nem.s.fdVm, KVM_CREATE_VCPU, (unsigned long)idCpu);
531 if (pVCpu->nem.s.fdVCpu < 0)
532 return RTErrInfoSetF(pErrInfo, VERR_NEM_VM_CREATE_FAILED, "KVM_CREATE_VCPU failed for VCpu #%u: %d", idCpu, errno);
533
534 /* Map the KVM_RUN area. */
535 pVCpu->nem.s.pRun = (struct kvm_run *)mmap(NULL, pVM->nem.s.cbVCpuMmap, PROT_READ | PROT_WRITE, MAP_SHARED,
536 pVCpu->nem.s.fdVCpu, 0 /*offset*/);
537 if ((void *)pVCpu->nem.s.pRun == MAP_FAILED)
538 return RTErrInfoSetF(pErrInfo, VERR_NEM_VM_CREATE_FAILED, "mmap failed for VCpu #%u: %d", idCpu, errno);
539
540 /* Initialize the vCPU. */
541 struct kvm_vcpu_init VCpuInit; RT_ZERO(VCpuInit);
542 VCpuInit.target = KVM_ARM_TARGET_GENERIC_V8;
543 /** @todo Enable features. */
544 if (ioctl(pVCpu->nem.s.fdVCpu, KVM_ARM_VCPU_INIT, &VCpuInit) != 0)
545 return RTErrInfoSetF(pErrInfo, VERR_NEM_VM_CREATE_FAILED, "KVM_ARM_VCPU_INIT failed for VCpu #%u: %d", idCpu, errno);
546
547#if 0
548 uint32_t fFeatures = 0; /** @todo SVE */
549 if (ioctl(pVCpu->nem.s.fdVCpu, KVM_ARM_VCPU_FINALIZE, &fFeatures) != 0)
550 return RTErrInfoSetF(pErrInfo, VERR_NEM_VM_CREATE_FAILED, "KVM_ARM_VCPU_FINALIZE failed for VCpu #%u: %d", idCpu, errno);
551#endif
552
553 if (idCpu == 0)
554 {
555 /* Query the supported register list and log it. */
556 int rc = nemR3LnxLogRegList(pVCpu->nem.s.fdVCpu);
557 if (RT_FAILURE(rc))
558 return RTErrInfoSetF(pErrInfo, VERR_NEM_VM_CREATE_FAILED, "Querying the supported register list failed with %Rrc", rc);
559
560 /* Need to query the ID registers and populate CPUM. */
561 CPUMIDREGS IdRegs; RT_ZERO(IdRegs);
562 for (uint32_t i = 0; i < RT_ELEMENTS(s_aIdRegs); i++)
563 {
564 uint64_t *pu64 = (uint64_t *)((uint8_t *)&IdRegs + s_aIdRegs[i].offIdStruct);
565 rc = nemR3LnxKvmQueryReg(pVCpu, s_aIdRegs[i].idKvmReg, pu64);
566 if (RT_FAILURE(rc))
567 return VMSetError(pVM, VERR_NEM_VM_CREATE_FAILED, RT_SRC_POS,
568 "Querying register %#x failed: %Rrc", s_aIdRegs[i].idKvmReg, rc);
569 }
570
571 rc = CPUMR3PopulateFeaturesByIdRegisters(pVM, &IdRegs);
572 if (RT_FAILURE(rc))
573 return rc;
574 }
575 }
576 return VINF_SUCCESS;
577}
578
579
580/** @callback_method_impl{FNVMMEMTRENDEZVOUS} */
581static DECLCALLBACK(VBOXSTRICTRC) nemR3LnxFixThreadPoke(PVM pVM, PVMCPU pVCpu, void *pvUser)
582{
583 RT_NOREF(pVM, pvUser);
584 int rc = RTThreadControlPokeSignal(pVCpu->hThread, true /*fEnable*/);
585 AssertLogRelRC(rc);
586 return VINF_SUCCESS;
587}
588
589
590/**
591 * Try initialize the native API.
592 *
593 * This may only do part of the job, more can be done in
594 * nemR3NativeInitAfterCPUM() and nemR3NativeInitCompleted().
595 *
596 * @returns VBox status code.
597 * @param pVM The cross context VM structure.
598 * @param fFallback Whether we're in fallback mode or use-NEM mode. In
599 * the latter we'll fail if we cannot initialize.
600 * @param fForced Whether the HMForced flag is set and we should
601 * fail if we cannot initialize.
602 */
603int nemR3NativeInit(PVM pVM, bool fFallback, bool fForced)
604{
605 RT_NOREF(pVM, fFallback, fForced);
606 /*
607 * Some state init.
608 */
609 pVM->nem.s.fdKvm = -1;
610 pVM->nem.s.fdVm = -1;
611 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
612 {
613 PNEMCPU pNemCpu = &pVM->apCpusR3[idCpu]->nem.s;
614 pNemCpu->fdVCpu = -1;
615 }
616
617 /*
618 * Error state.
619 * The error message will be non-empty on failure and 'rc' will be set too.
620 */
621 RTERRINFOSTATIC ErrInfo;
622 PRTERRINFO pErrInfo = RTErrInfoInitStatic(&ErrInfo);
623
624 /*
625 * Open kvm subsystem so we can issue system ioctls.
626 */
627 int rc;
628 int fdKvm = open("/dev/kvm", O_RDWR | O_CLOEXEC);
629 if (fdKvm >= 0)
630 {
631 pVM->nem.s.fdKvm = fdKvm;
632
633 /*
634 * Check capabilities.
635 */
636 rc = nemR3LnxInitCheckCapabilities(pVM, pErrInfo);
637 if (RT_SUCCESS(rc))
638 {
639 /*
640 * Create an empty VM since it is recommended we check capabilities on
641 * the VM rather than the system descriptor.
642 */
643 int fdVm = ioctl(fdKvm, KVM_CREATE_VM, pVM->nem.s.cIpaBits);
644 if (fdVm >= 0)
645 {
646 pVM->nem.s.fdVm = fdVm;
647
648 /*
649 * Set up the VM (more on this later).
650 */
651 rc = nemR3LnxInitSetupVm(pVM, pErrInfo);
652 if (RT_SUCCESS(rc))
653 {
654 /*
655 * Set ourselves as the execution engine and make config adjustments.
656 */
657 VM_SET_MAIN_EXECUTION_ENGINE(pVM, VM_EXEC_ENGINE_NATIVE_API);
658 Log(("NEM: Marked active!\n"));
659 PGMR3EnableNemMode(pVM);
660
661 /*
662 * Register release statistics
663 */
664 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
665 {
666 PNEMCPU pNemCpu = &pVM->apCpusR3[idCpu]->nem.s;
667 STAMR3RegisterF(pVM, &pNemCpu->StatImportOnDemand, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of on-demand state imports", "/NEM/CPU%u/ImportOnDemand", idCpu);
668 STAMR3RegisterF(pVM, &pNemCpu->StatImportOnReturn, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of state imports on loop return", "/NEM/CPU%u/ImportOnReturn", idCpu);
669 STAMR3RegisterF(pVM, &pNemCpu->StatImportOnReturnSkipped, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of skipped state imports on loop return", "/NEM/CPU%u/ImportOnReturnSkipped", idCpu);
670 STAMR3RegisterF(pVM, &pNemCpu->StatImportPendingInterrupt, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of times an interrupt was pending when importing from KVM", "/NEM/CPU%u/ImportPendingInterrupt", idCpu);
671 STAMR3RegisterF(pVM, &pNemCpu->StatExportPendingInterrupt, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of times an interrupt was pending when exporting to KVM", "/NEM/CPU%u/ExportPendingInterrupt", idCpu);
672 STAMR3RegisterF(pVM, &pNemCpu->StatFlushExitOnReturn, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of times a KVM_EXIT_IO or KVM_EXIT_MMIO was flushed before returning to EM", "/NEM/CPU%u/FlushExitOnReturn", idCpu);
673 STAMR3RegisterF(pVM, &pNemCpu->StatFlushExitOnReturn1Loop, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of times a KVM_EXIT_IO or KVM_EXIT_MMIO was flushed before returning to EM", "/NEM/CPU%u/FlushExitOnReturn-01-loop", idCpu);
674 STAMR3RegisterF(pVM, &pNemCpu->StatFlushExitOnReturn2Loops, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of times a KVM_EXIT_IO or KVM_EXIT_MMIO was flushed before returning to EM", "/NEM/CPU%u/FlushExitOnReturn-02-loops", idCpu);
675 STAMR3RegisterF(pVM, &pNemCpu->StatFlushExitOnReturn3Loops, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of times a KVM_EXIT_IO or KVM_EXIT_MMIO was flushed before returning to EM", "/NEM/CPU%u/FlushExitOnReturn-03-loops", idCpu);
676 STAMR3RegisterF(pVM, &pNemCpu->StatFlushExitOnReturn4PlusLoops, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of times a KVM_EXIT_IO or KVM_EXIT_MMIO was flushed before returning to EM", "/NEM/CPU%u/FlushExitOnReturn-04-to-7-loops", idCpu);
677 STAMR3RegisterF(pVM, &pNemCpu->StatQueryCpuTick, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of TSC queries", "/NEM/CPU%u/QueryCpuTick", idCpu);
678 STAMR3RegisterF(pVM, &pNemCpu->StatExitTotal, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "All exits", "/NEM/CPU%u/Exit", idCpu);
679 STAMR3RegisterF(pVM, &pNemCpu->StatExitIo, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "KVM_EXIT_IO", "/NEM/CPU%u/Exit/Io", idCpu);
680 STAMR3RegisterF(pVM, &pNemCpu->StatExitMmio, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "KVM_EXIT_MMIO", "/NEM/CPU%u/Exit/Mmio", idCpu);
681 STAMR3RegisterF(pVM, &pNemCpu->StatExitIntr, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "KVM_EXIT_INTR", "/NEM/CPU%u/Exit/Intr", idCpu);
682 STAMR3RegisterF(pVM, &pNemCpu->StatExitHypercall, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "KVM_EXIT_HYPERCALL", "/NEM/CPU%u/Exit/Hypercall", idCpu);
683 STAMR3RegisterF(pVM, &pNemCpu->StatExitDebug, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "KVM_EXIT_DEBUG", "/NEM/CPU%u/Exit/Debug", idCpu);
684 STAMR3RegisterF(pVM, &pNemCpu->StatExitBusLock, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "KVM_EXIT_BUS_LOCK", "/NEM/CPU%u/Exit/BusLock", idCpu);
685 STAMR3RegisterF(pVM, &pNemCpu->StatExitInternalErrorEmulation, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "KVM_EXIT_INTERNAL_ERROR/EMULATION", "/NEM/CPU%u/Exit/InternalErrorEmulation", idCpu);
686 STAMR3RegisterF(pVM, &pNemCpu->StatExitInternalErrorFatal, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "KVM_EXIT_INTERNAL_ERROR/*", "/NEM/CPU%u/Exit/InternalErrorFatal", idCpu);
687 }
688
689 /*
690 * Success.
691 */
692 return VINF_SUCCESS;
693 }
694 close(fdVm);
695 pVM->nem.s.fdVm = -1;
696 }
697 else
698 rc = RTErrInfoSetF(pErrInfo, VERR_NEM_VM_CREATE_FAILED, "KVM_CREATE_VM failed: %u", errno);
699 }
700 close(fdKvm);
701 pVM->nem.s.fdKvm = -1;
702 }
703 else if (errno == EACCES)
704 rc = RTErrInfoSet(pErrInfo, VERR_ACCESS_DENIED, "Do not have access to open /dev/kvm for reading & writing.");
705 else if (errno == ENOENT)
706 rc = RTErrInfoSet(pErrInfo, VERR_NOT_SUPPORTED, "KVM is not availble (/dev/kvm does not exist)");
707 else
708 rc = RTErrInfoSetF(pErrInfo, RTErrConvertFromErrno(errno), "Failed to open '/dev/kvm': %u", errno);
709
710 /*
711 * We only fail if in forced mode, otherwise just log the complaint and return.
712 */
713 Assert(RTErrInfoIsSet(pErrInfo));
714 if ( (fForced || !fFallback)
715 && pVM->bMainExecutionEngine != VM_EXEC_ENGINE_NATIVE_API)
716 return VMSetError(pVM, RT_SUCCESS_NP(rc) ? VERR_NEM_NOT_AVAILABLE : rc, RT_SRC_POS, "%s", pErrInfo->pszMsg);
717 LogRel(("NEM: Not available: %s\n", pErrInfo->pszMsg));
718 return VINF_SUCCESS;
719}
720
721
722/**
723 * This is called after CPUMR3Init is done.
724 *
725 * @returns VBox status code.
726 * @param pVM The VM handle..
727 */
728int nemR3NativeInitAfterCPUM(PVM pVM)
729{
730 /*
731 * Validate sanity.
732 */
733 AssertReturn(pVM->nem.s.fdKvm >= 0, VERR_WRONG_ORDER);
734 AssertReturn(pVM->nem.s.fdVm >= 0, VERR_WRONG_ORDER);
735 AssertReturn(pVM->bMainExecutionEngine == VM_EXEC_ENGINE_NATIVE_API, VERR_WRONG_ORDER);
736
737 /** @todo */
738
739 return VINF_SUCCESS;
740}
741
742
743int nemR3NativeInitCompleted(PVM pVM, VMINITCOMPLETED enmWhat)
744{
745 /*
746 * Make RTThreadPoke work again (disabled for avoiding unnecessary
747 * critical section issues in ring-0).
748 */
749 if (enmWhat == VMINITCOMPLETED_RING3)
750 VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ALL_AT_ONCE, nemR3LnxFixThreadPoke, NULL);
751
752 return VINF_SUCCESS;
753}
754
755
756int nemR3NativeTerm(PVM pVM)
757{
758 /*
759 * Per-cpu data
760 */
761 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
762 {
763 PVMCPU pVCpu = pVM->apCpusR3[idCpu];
764
765 if (pVCpu->nem.s.fdVCpu != -1)
766 {
767 close(pVCpu->nem.s.fdVCpu);
768 pVCpu->nem.s.fdVCpu = -1;
769 }
770 if (pVCpu->nem.s.pRun)
771 {
772 munmap(pVCpu->nem.s.pRun, pVM->nem.s.cbVCpuMmap);
773 pVCpu->nem.s.pRun = NULL;
774 }
775 }
776
777 /*
778 * Global data.
779 */
780 if (pVM->nem.s.fdVm != -1)
781 {
782 close(pVM->nem.s.fdVm);
783 pVM->nem.s.fdVm = -1;
784 }
785
786 if (pVM->nem.s.fdKvm != -1)
787 {
788 close(pVM->nem.s.fdKvm);
789 pVM->nem.s.fdKvm = -1;
790 }
791 return VINF_SUCCESS;
792}
793
794
795/**
796 * VM reset notification.
797 *
798 * @param pVM The cross context VM structure.
799 */
800void nemR3NativeReset(PVM pVM)
801{
802 RT_NOREF(pVM);
803}
804
805
806/**
807 * Reset CPU due to INIT IPI or hot (un)plugging.
808 *
809 * @param pVCpu The cross context virtual CPU structure of the CPU being
810 * reset.
811 * @param fInitIpi Whether this is the INIT IPI or hot (un)plugging case.
812 */
813void nemR3NativeResetCpu(PVMCPU pVCpu, bool fInitIpi)
814{
815 RT_NOREF(pVCpu, fInitIpi);
816}
817
818
819/*********************************************************************************************************************************
820* Memory management *
821*********************************************************************************************************************************/
822
823
824/**
825 * Allocates a memory slot ID.
826 *
827 * @returns Slot ID on success, UINT16_MAX on failure.
828 */
829static uint16_t nemR3LnxMemSlotIdAlloc(PVM pVM)
830{
831 /* Use the hint first. */
832 uint16_t idHint = pVM->nem.s.idPrevSlot;
833 if (idHint < _32K - 1)
834 {
835 int32_t idx = ASMBitNextClear(&pVM->nem.s.bmSlotIds, _32K, idHint);
836 Assert(idx < _32K);
837 if (idx > 0 && !ASMAtomicBitTestAndSet(&pVM->nem.s.bmSlotIds, idx))
838 return pVM->nem.s.idPrevSlot = (uint16_t)idx;
839 }
840
841 /*
842 * Search the whole map from the start.
843 */
844 int32_t idx = ASMBitFirstClear(&pVM->nem.s.bmSlotIds, _32K);
845 Assert(idx < _32K);
846 if (idx > 0 && !ASMAtomicBitTestAndSet(&pVM->nem.s.bmSlotIds, idx))
847 return pVM->nem.s.idPrevSlot = (uint16_t)idx;
848
849 Assert(idx < 0 /*shouldn't trigger unless there is a race */);
850 return UINT16_MAX; /* caller is expected to assert. */
851}
852
853
854/**
855 * Frees a memory slot ID
856 */
857static void nemR3LnxMemSlotIdFree(PVM pVM, uint16_t idSlot)
858{
859 if (RT_LIKELY(idSlot < _32K && ASMAtomicBitTestAndClear(&pVM->nem.s.bmSlotIds, idSlot)))
860 { /*likely*/ }
861 else
862 AssertMsgFailed(("idSlot=%u (%#x)\n", idSlot, idSlot));
863}
864
865
866
867VMMR3_INT_DECL(int) NEMR3NotifyPhysRamRegister(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb, void *pvR3,
868 uint8_t *pu2State, uint32_t *puNemRange)
869{
870 uint16_t idSlot = nemR3LnxMemSlotIdAlloc(pVM);
871 AssertLogRelReturn(idSlot < _32K, VERR_NEM_MAP_PAGES_FAILED);
872
873 Log5(("NEMR3NotifyPhysRamRegister: %RGp LB %RGp, pvR3=%p pu2State=%p (%d) puNemRange=%p (%d) - idSlot=%#x\n",
874 GCPhys, cb, pvR3, pu2State, pu2State, puNemRange, *puNemRange, idSlot));
875
876 struct kvm_userspace_memory_region Region;
877 Region.slot = idSlot;
878 Region.flags = 0;
879 Region.guest_phys_addr = GCPhys;
880 Region.memory_size = cb;
881 Region.userspace_addr = (uintptr_t)pvR3;
882
883 int rc = ioctl(pVM->nem.s.fdVm, KVM_SET_USER_MEMORY_REGION, &Region);
884 if (rc == 0)
885 {
886 *pu2State = 0;
887 *puNemRange = idSlot;
888 return VINF_SUCCESS;
889 }
890
891 LogRel(("NEMR3NotifyPhysRamRegister: %RGp LB %RGp, pvR3=%p, idSlot=%#x failed: %u/%u\n", GCPhys, cb, pvR3, idSlot, rc, errno));
892 nemR3LnxMemSlotIdFree(pVM, idSlot);
893 return VERR_NEM_MAP_PAGES_FAILED;
894}
895
896
897VMMR3_INT_DECL(bool) NEMR3IsMmio2DirtyPageTrackingSupported(PVM pVM)
898{
899 RT_NOREF(pVM);
900 return true;
901}
902
903
904VMMR3_INT_DECL(int) NEMR3NotifyPhysMmioExMapEarly(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb, uint32_t fFlags,
905 void *pvRam, void *pvMmio2, uint8_t *pu2State, uint32_t *puNemRange)
906{
907 Log5(("NEMR3NotifyPhysMmioExMapEarly: %RGp LB %RGp fFlags=%#x pvRam=%p pvMmio2=%p pu2State=%p (%d) puNemRange=%p (%#x)\n",
908 GCPhys, cb, fFlags, pvRam, pvMmio2, pu2State, *pu2State, puNemRange, puNemRange ? *puNemRange : UINT32_MAX));
909 RT_NOREF(pvRam);
910
911 if (fFlags & NEM_NOTIFY_PHYS_MMIO_EX_F_REPLACE)
912 {
913 /** @todo implement splitting and whatnot of ranges if we want to be 100%
914 * conforming (just modify RAM registrations in MM.cpp to test). */
915 AssertLogRelMsgFailedReturn(("%RGp LB %RGp fFlags=%#x pvRam=%p pvMmio2=%p\n", GCPhys, cb, fFlags, pvRam, pvMmio2),
916 VERR_NEM_MAP_PAGES_FAILED);
917 }
918
919 /*
920 * Register MMIO2.
921 */
922 if (fFlags & NEM_NOTIFY_PHYS_MMIO_EX_F_MMIO2)
923 {
924 AssertReturn(pvMmio2, VERR_NEM_MAP_PAGES_FAILED);
925 AssertReturn(puNemRange, VERR_NEM_MAP_PAGES_FAILED);
926
927 uint16_t idSlot = nemR3LnxMemSlotIdAlloc(pVM);
928 AssertLogRelReturn(idSlot < _32K, VERR_NEM_MAP_PAGES_FAILED);
929
930 struct kvm_userspace_memory_region Region;
931 Region.slot = idSlot;
932 Region.flags = fFlags & NEM_NOTIFY_PHYS_MMIO_EX_F_TRACK_DIRTY_PAGES ? KVM_MEM_LOG_DIRTY_PAGES : 0;
933 Region.guest_phys_addr = GCPhys;
934 Region.memory_size = cb;
935 Region.userspace_addr = (uintptr_t)pvMmio2;
936
937 int rc = ioctl(pVM->nem.s.fdVm, KVM_SET_USER_MEMORY_REGION, &Region);
938 if (rc == 0)
939 {
940 *pu2State = 0;
941 *puNemRange = idSlot;
942 Log5(("NEMR3NotifyPhysMmioExMapEarly: %RGp LB %RGp fFlags=%#x pvMmio2=%p - idSlot=%#x\n",
943 GCPhys, cb, fFlags, pvMmio2, idSlot));
944 return VINF_SUCCESS;
945 }
946
947 nemR3LnxMemSlotIdFree(pVM, idSlot);
948 AssertLogRelMsgFailedReturn(("%RGp LB %RGp fFlags=%#x, pvMmio2=%p, idSlot=%#x failed: %u/%u\n",
949 GCPhys, cb, fFlags, pvMmio2, idSlot, errno, rc),
950 VERR_NEM_MAP_PAGES_FAILED);
951 }
952
953 /* MMIO, don't care. */
954 *pu2State = 0;
955 *puNemRange = UINT32_MAX;
956 return VINF_SUCCESS;
957}
958
959
960VMMR3_INT_DECL(int) NEMR3NotifyPhysMmioExMapLate(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb, uint32_t fFlags,
961 void *pvRam, void *pvMmio2, uint32_t *puNemRange)
962{
963 RT_NOREF(pVM, GCPhys, cb, fFlags, pvRam, pvMmio2, puNemRange);
964 return VINF_SUCCESS;
965}
966
967
968VMMR3_INT_DECL(int) NEMR3NotifyPhysMmioExUnmap(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb, uint32_t fFlags, void *pvRam,
969 void *pvMmio2, uint8_t *pu2State, uint32_t *puNemRange)
970{
971 Log5(("NEMR3NotifyPhysMmioExUnmap: %RGp LB %RGp fFlags=%#x pvRam=%p pvMmio2=%p pu2State=%p puNemRange=%p (%#x)\n",
972 GCPhys, cb, fFlags, pvRam, pvMmio2, pu2State, puNemRange, *puNemRange));
973 RT_NOREF(pVM, GCPhys, cb, fFlags, pvRam, pvMmio2, pu2State);
974
975 if (fFlags & NEM_NOTIFY_PHYS_MMIO_EX_F_REPLACE)
976 {
977 /** @todo implement splitting and whatnot of ranges if we want to be 100%
978 * conforming (just modify RAM registrations in MM.cpp to test). */
979 AssertLogRelMsgFailedReturn(("%RGp LB %RGp fFlags=%#x pvRam=%p pvMmio2=%p\n", GCPhys, cb, fFlags, pvRam, pvMmio2),
980 VERR_NEM_UNMAP_PAGES_FAILED);
981 }
982
983 if (fFlags & NEM_NOTIFY_PHYS_MMIO_EX_F_MMIO2)
984 {
985 uint32_t const idSlot = *puNemRange;
986 AssertReturn(idSlot > 0 && idSlot < _32K, VERR_NEM_IPE_4);
987 AssertReturn(ASMBitTest(pVM->nem.s.bmSlotIds, idSlot), VERR_NEM_IPE_4);
988
989 struct kvm_userspace_memory_region Region;
990 Region.slot = idSlot;
991 Region.flags = 0;
992 Region.guest_phys_addr = GCPhys;
993 Region.memory_size = 0; /* this deregisters it. */
994 Region.userspace_addr = (uintptr_t)pvMmio2;
995
996 int rc = ioctl(pVM->nem.s.fdVm, KVM_SET_USER_MEMORY_REGION, &Region);
997 if (rc == 0)
998 {
999 if (pu2State)
1000 *pu2State = 0;
1001 *puNemRange = UINT32_MAX;
1002 nemR3LnxMemSlotIdFree(pVM, idSlot);
1003 return VINF_SUCCESS;
1004 }
1005
1006 AssertLogRelMsgFailedReturn(("%RGp LB %RGp fFlags=%#x, pvMmio2=%p, idSlot=%#x failed: %u/%u\n",
1007 GCPhys, cb, fFlags, pvMmio2, idSlot, errno, rc),
1008 VERR_NEM_UNMAP_PAGES_FAILED);
1009 }
1010
1011 if (pu2State)
1012 *pu2State = UINT8_MAX;
1013 return VINF_SUCCESS;
1014}
1015
1016
1017VMMR3_INT_DECL(int) NEMR3PhysMmio2QueryAndResetDirtyBitmap(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb, uint32_t uNemRange,
1018 void *pvBitmap, size_t cbBitmap)
1019{
1020 AssertReturn(uNemRange > 0 && uNemRange < _32K, VERR_NEM_IPE_4);
1021 AssertReturn(ASMBitTest(pVM->nem.s.bmSlotIds, uNemRange), VERR_NEM_IPE_4);
1022
1023 RT_NOREF(GCPhys, cbBitmap);
1024
1025 struct kvm_dirty_log DirtyLog;
1026 DirtyLog.slot = uNemRange;
1027 DirtyLog.padding1 = 0;
1028 DirtyLog.dirty_bitmap = pvBitmap;
1029
1030 int rc = ioctl(pVM->nem.s.fdVm, KVM_GET_DIRTY_LOG, &DirtyLog);
1031 AssertLogRelMsgReturn(rc == 0, ("%RGp LB %RGp idSlot=%#x failed: %u/%u\n", GCPhys, cb, uNemRange, errno, rc),
1032 VERR_NEM_QUERY_DIRTY_BITMAP_FAILED);
1033
1034 return VINF_SUCCESS;
1035}
1036
1037
1038VMMR3_INT_DECL(int) NEMR3NotifyPhysRomRegisterEarly(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb, void *pvPages, uint32_t fFlags,
1039 uint8_t *pu2State, uint32_t *puNemRange)
1040{
1041 Log5(("NEMR3NotifyPhysRomRegisterEarly: %RGp LB %RGp pvPages=%p fFlags=%#x\n", GCPhys, cb, pvPages, fFlags));
1042 *pu2State = UINT8_MAX;
1043
1044 /* Don't support puttint ROM where there is already RAM. For
1045 now just shuffle the registrations till it works... */
1046 AssertLogRelMsgReturn(!(fFlags & NEM_NOTIFY_PHYS_ROM_F_REPLACE), ("%RGp LB %RGp fFlags=%#x\n", GCPhys, cb, fFlags),
1047 VERR_NEM_MAP_PAGES_FAILED);
1048
1049 /** @todo figure out how to do shadow ROMs. */
1050
1051 /*
1052 * We only allocate a slot number here in case we need to use it to
1053 * fend of physical handler fun.
1054 */
1055 uint16_t idSlot = nemR3LnxMemSlotIdAlloc(pVM);
1056 AssertLogRelReturn(idSlot < _32K, VERR_NEM_MAP_PAGES_FAILED);
1057
1058 *pu2State = 0;
1059 *puNemRange = idSlot;
1060 Log5(("NEMR3NotifyPhysRomRegisterEarly: %RGp LB %RGp fFlags=%#x pvPages=%p - idSlot=%#x\n",
1061 GCPhys, cb, fFlags, pvPages, idSlot));
1062 RT_NOREF(GCPhys, cb, fFlags, pvPages);
1063 return VINF_SUCCESS;
1064}
1065
1066
1067VMMR3_INT_DECL(int) NEMR3NotifyPhysRomRegisterLate(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb, void *pvPages,
1068 uint32_t fFlags, uint8_t *pu2State, uint32_t *puNemRange)
1069{
1070 Log5(("NEMR3NotifyPhysRomRegisterLate: %RGp LB %RGp pvPages=%p fFlags=%#x pu2State=%p (%d) puNemRange=%p (%#x)\n",
1071 GCPhys, cb, pvPages, fFlags, pu2State, *pu2State, puNemRange, *puNemRange));
1072
1073 AssertPtrReturn(pvPages, VERR_NEM_IPE_5);
1074
1075 uint32_t const idSlot = *puNemRange;
1076 AssertReturn(idSlot > 0 && idSlot < _32K, VERR_NEM_IPE_4);
1077 AssertReturn(ASMBitTest(pVM->nem.s.bmSlotIds, idSlot), VERR_NEM_IPE_4);
1078
1079 *pu2State = UINT8_MAX;
1080
1081 /*
1082 * Do the actual setting of the user pages here now that we've
1083 * got a valid pvPages (typically isn't available during the early
1084 * notification, unless we're replacing RAM).
1085 */
1086 struct kvm_userspace_memory_region Region;
1087 Region.slot = idSlot;
1088 Region.flags = 0;
1089 Region.guest_phys_addr = GCPhys;
1090 Region.memory_size = cb;
1091 Region.userspace_addr = (uintptr_t)pvPages;
1092
1093 int rc = ioctl(pVM->nem.s.fdVm, KVM_SET_USER_MEMORY_REGION, &Region);
1094 if (rc == 0)
1095 {
1096 *pu2State = 0;
1097 Log5(("NEMR3NotifyPhysRomRegisterEarly: %RGp LB %RGp fFlags=%#x pvPages=%p - idSlot=%#x\n",
1098 GCPhys, cb, fFlags, pvPages, idSlot));
1099 return VINF_SUCCESS;
1100 }
1101 AssertLogRelMsgFailedReturn(("%RGp LB %RGp fFlags=%#x, pvPages=%p, idSlot=%#x failed: %u/%u\n",
1102 GCPhys, cb, fFlags, pvPages, idSlot, errno, rc),
1103 VERR_NEM_MAP_PAGES_FAILED);
1104}
1105
1106
1107VMMR3_INT_DECL(void) NEMR3NotifySetA20(PVMCPU pVCpu, bool fEnabled)
1108{
1109 Log(("nemR3NativeNotifySetA20: fEnabled=%RTbool\n", fEnabled));
1110 Assert(VM_IS_NEM_ENABLED(pVCpu->CTX_SUFF(pVM)));
1111 RT_NOREF(pVCpu, fEnabled);
1112}
1113
1114
1115VMM_INT_DECL(void) NEMHCNotifyHandlerPhysicalDeregister(PVMCC pVM, PGMPHYSHANDLERKIND enmKind, RTGCPHYS GCPhys, RTGCPHYS cb,
1116 RTR3PTR pvMemR3, uint8_t *pu2State)
1117{
1118 Log5(("NEMHCNotifyHandlerPhysicalDeregister: %RGp LB %RGp enmKind=%d pvMemR3=%p pu2State=%p (%d)\n",
1119 GCPhys, cb, enmKind, pvMemR3, pu2State, *pu2State));
1120
1121 *pu2State = UINT8_MAX;
1122 RT_NOREF(pVM, enmKind, GCPhys, cb, pvMemR3);
1123}
1124
1125
1126void nemHCNativeNotifyHandlerPhysicalRegister(PVMCC pVM, PGMPHYSHANDLERKIND enmKind, RTGCPHYS GCPhys, RTGCPHYS cb)
1127{
1128 Log5(("nemHCNativeNotifyHandlerPhysicalRegister: %RGp LB %RGp enmKind=%d\n", GCPhys, cb, enmKind));
1129 RT_NOREF(pVM, enmKind, GCPhys, cb);
1130}
1131
1132
1133void nemHCNativeNotifyHandlerPhysicalModify(PVMCC pVM, PGMPHYSHANDLERKIND enmKind, RTGCPHYS GCPhysOld,
1134 RTGCPHYS GCPhysNew, RTGCPHYS cb, bool fRestoreAsRAM)
1135{
1136 Log5(("nemHCNativeNotifyHandlerPhysicalModify: %RGp LB %RGp -> %RGp enmKind=%d fRestoreAsRAM=%d\n",
1137 GCPhysOld, cb, GCPhysNew, enmKind, fRestoreAsRAM));
1138 RT_NOREF(pVM, enmKind, GCPhysOld, GCPhysNew, cb, fRestoreAsRAM);
1139}
1140
1141
1142int nemHCNativeNotifyPhysPageAllocated(PVMCC pVM, RTGCPHYS GCPhys, RTHCPHYS HCPhys, uint32_t fPageProt,
1143 PGMPAGETYPE enmType, uint8_t *pu2State)
1144{
1145 Log5(("nemHCNativeNotifyPhysPageAllocated: %RGp HCPhys=%RHp fPageProt=%#x enmType=%d *pu2State=%d\n",
1146 GCPhys, HCPhys, fPageProt, enmType, *pu2State));
1147 RT_NOREF(pVM, GCPhys, HCPhys, fPageProt, enmType, pu2State);
1148 return VINF_SUCCESS;
1149}
1150
1151
1152VMM_INT_DECL(void) NEMHCNotifyPhysPageProtChanged(PVMCC pVM, RTGCPHYS GCPhys, RTHCPHYS HCPhys, RTR3PTR pvR3, uint32_t fPageProt,
1153 PGMPAGETYPE enmType, uint8_t *pu2State)
1154{
1155 Log5(("NEMHCNotifyPhysPageProtChanged: %RGp HCPhys=%RHp fPageProt=%#x enmType=%d *pu2State=%d\n",
1156 GCPhys, HCPhys, fPageProt, enmType, *pu2State));
1157 Assert(VM_IS_NEM_ENABLED(pVM));
1158 RT_NOREF(pVM, GCPhys, HCPhys, pvR3, fPageProt, enmType, pu2State);
1159
1160}
1161
1162
1163VMM_INT_DECL(void) NEMHCNotifyPhysPageChanged(PVMCC pVM, RTGCPHYS GCPhys, RTHCPHYS HCPhysPrev, RTHCPHYS HCPhysNew,
1164 RTR3PTR pvNewR3, uint32_t fPageProt, PGMPAGETYPE enmType, uint8_t *pu2State)
1165{
1166 Log5(("nemHCNativeNotifyPhysPageChanged: %RGp HCPhys=%RHp->%RHp pvNewR3=%p fPageProt=%#x enmType=%d *pu2State=%d\n",
1167 GCPhys, HCPhysPrev, HCPhysNew, pvNewR3, fPageProt, enmType, *pu2State));
1168 Assert(VM_IS_NEM_ENABLED(pVM));
1169 RT_NOREF(pVM, GCPhys, HCPhysPrev, HCPhysNew, pvNewR3, fPageProt, enmType, pu2State);
1170}
1171
1172
1173/*********************************************************************************************************************************
1174* CPU State *
1175*********************************************************************************************************************************/
1176
1177/**
1178 * Worker that imports selected state from KVM.
1179 */
1180static int nemHCLnxImportState(PVMCPUCC pVCpu, uint64_t fWhat, PCPUMCTX pCtx, struct kvm_run *pRun)
1181{
1182 fWhat &= pVCpu->cpum.GstCtx.fExtrn;
1183 if (!fWhat)
1184 return VINF_SUCCESS;
1185
1186 RT_NOREF(pRun);
1187
1188 /** @todo */
1189
1190 /*
1191 * Update the external mask.
1192 */
1193 pCtx->fExtrn &= ~fWhat;
1194 pVCpu->cpum.GstCtx.fExtrn &= ~fWhat;
1195 if (!(pVCpu->cpum.GstCtx.fExtrn & CPUMCTX_EXTRN_ALL))
1196 pVCpu->cpum.GstCtx.fExtrn = 0;
1197
1198 return VINF_SUCCESS;
1199}
1200
1201
1202/**
1203 * Interface for importing state on demand (used by IEM).
1204 *
1205 * @returns VBox status code.
1206 * @param pVCpu The cross context CPU structure.
1207 * @param fWhat What to import, CPUMCTX_EXTRN_XXX.
1208 */
1209VMM_INT_DECL(int) NEMImportStateOnDemand(PVMCPUCC pVCpu, uint64_t fWhat)
1210{
1211 STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatImportOnDemand);
1212 return nemHCLnxImportState(pVCpu, fWhat, &pVCpu->cpum.GstCtx, pVCpu->nem.s.pRun);
1213}
1214
1215
1216/**
1217 * Exports state to KVM.
1218 */
1219static int nemHCLnxExportState(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx, struct kvm_run *pRun)
1220{
1221 uint64_t const fExtrn = ~pCtx->fExtrn & CPUMCTX_EXTRN_ALL;
1222 Assert((~fExtrn & CPUMCTX_EXTRN_ALL) != CPUMCTX_EXTRN_ALL);
1223
1224 RT_NOREF(pVM, pVCpu, pCtx, pRun);
1225 /** @todo */
1226
1227 /*
1228 * KVM now owns all the state.
1229 */
1230 pCtx->fExtrn = CPUMCTX_EXTRN_KEEPER_NEM | CPUMCTX_EXTRN_ALL;
1231 return VINF_SUCCESS;
1232}
1233
1234
1235/**
1236 * Query the CPU tick counter and optionally the TSC_AUX MSR value.
1237 *
1238 * @returns VBox status code.
1239 * @param pVCpu The cross context CPU structure.
1240 * @param pcTicks Where to return the CPU tick count.
1241 * @param puAux Where to return the TSC_AUX register value.
1242 */
1243VMM_INT_DECL(int) NEMHCQueryCpuTick(PVMCPUCC pVCpu, uint64_t *pcTicks, uint32_t *puAux)
1244{
1245 STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatQueryCpuTick);
1246 // KVM_GET_CLOCK?
1247 RT_NOREF(pVCpu, pcTicks, puAux);
1248 return VINF_SUCCESS;
1249}
1250
1251
1252/**
1253 * Resumes CPU clock (TSC) on all virtual CPUs.
1254 *
1255 * This is called by TM when the VM is started, restored, resumed or similar.
1256 *
1257 * @returns VBox status code.
1258 * @param pVM The cross context VM structure.
1259 * @param pVCpu The cross context CPU structure of the calling EMT.
1260 * @param uPausedTscValue The TSC value at the time of pausing.
1261 */
1262VMM_INT_DECL(int) NEMHCResumeCpuTickOnAll(PVMCC pVM, PVMCPUCC pVCpu, uint64_t uPausedTscValue)
1263{
1264 // KVM_SET_CLOCK?
1265 RT_NOREF(pVM, pVCpu, uPausedTscValue);
1266 return VINF_SUCCESS;
1267}
1268
1269
1270VMM_INT_DECL(uint32_t) NEMHCGetFeatures(PVMCC pVM)
1271{
1272 RT_NOREF(pVM);
1273 return NEM_FEAT_F_NESTED_PAGING
1274 | NEM_FEAT_F_FULL_GST_EXEC;
1275}
1276
1277
1278
1279/*********************************************************************************************************************************
1280* Execution *
1281*********************************************************************************************************************************/
1282
1283
1284VMMR3_INT_DECL(bool) NEMR3CanExecuteGuest(PVM pVM, PVMCPU pVCpu)
1285{
1286 RT_NOREF(pVM, pVCpu);
1287 Assert(VM_IS_NEM_ENABLED(pVM));
1288 return true;
1289}
1290
1291
1292bool nemR3NativeSetSingleInstruction(PVM pVM, PVMCPU pVCpu, bool fEnable)
1293{
1294 NOREF(pVM); NOREF(pVCpu); NOREF(fEnable);
1295 return false;
1296}
1297
1298
1299void nemR3NativeNotifyFF(PVM pVM, PVMCPU pVCpu, uint32_t fFlags)
1300{
1301 int rc = RTThreadPoke(pVCpu->hThread);
1302 LogFlow(("nemR3NativeNotifyFF: #%u -> %Rrc\n", pVCpu->idCpu, rc));
1303 AssertRC(rc);
1304 RT_NOREF(pVM, fFlags);
1305}
1306
1307
1308DECLHIDDEN(bool) nemR3NativeNotifyDebugEventChanged(PVM pVM, bool fUseDebugLoop)
1309{
1310 RT_NOREF(pVM, fUseDebugLoop);
1311 return false;
1312}
1313
1314
1315DECLHIDDEN(bool) nemR3NativeNotifyDebugEventChangedPerCpu(PVM pVM, PVMCPU pVCpu, bool fUseDebugLoop)
1316{
1317 RT_NOREF(pVM, pVCpu, fUseDebugLoop);
1318 return false;
1319}
1320
1321
1322/**
1323 * Deals with pending interrupt FFs prior to executing guest code.
1324 */
1325static VBOXSTRICTRC nemHCLnxHandleInterruptFF(PVM pVM, PVMCPU pVCpu, struct kvm_run *pRun)
1326{
1327 RT_NOREF_PV(pVM);
1328
1329 /*
1330 * Do not doing anything if TRPM has something pending already as we can
1331 * only inject one event per KVM_RUN call. This can only happend if we
1332 * can directly from the loop in EM, so the inhibit bits must be internal.
1333 */
1334 if (!TRPMHasTrap(pVCpu))
1335 { /* semi likely */ }
1336 else
1337 {
1338 Log8(("nemHCLnxHandleInterruptFF: TRPM has an pending event already\n"));
1339 return VINF_SUCCESS;
1340 }
1341
1342 RT_NOREF(pRun);
1343
1344 /*
1345 * In KVM the CPUMCTX_EXTRN_INHIBIT_INT and CPUMCTX_EXTRN_INHIBIT_NMI states
1346 * are tied together with interrupt and NMI delivery, so we must get and
1347 * synchronize these all in one go and set both CPUMCTX_EXTRN_INHIBIT_XXX flags.
1348 * If we don't we may lose the interrupt/NMI we marked pending here when the
1349 * state is exported again before execution.
1350 */
1351 struct kvm_vcpu_events KvmEvents = {0};
1352 int rcLnx = ioctl(pVCpu->nem.s.fdVCpu, KVM_GET_VCPU_EVENTS, &KvmEvents);
1353 AssertLogRelMsgReturn(rcLnx == 0, ("rcLnx=%d errno=%d\n", rcLnx, errno), VERR_NEM_IPE_5);
1354
1355 /* KVM will own the INT + NMI inhibit state soon: */
1356 pVCpu->cpum.GstCtx.fExtrn = (pVCpu->cpum.GstCtx.fExtrn & ~CPUMCTX_EXTRN_KEEPER_MASK)
1357 | CPUMCTX_EXTRN_KEEPER_NEM;
1358
1359 /*
1360 * Now, update the state.
1361 */
1362 /** @todo skip when possible... */
1363 rcLnx = ioctl(pVCpu->nem.s.fdVCpu, KVM_SET_VCPU_EVENTS, &KvmEvents);
1364 AssertLogRelMsgReturn(rcLnx == 0, ("rcLnx=%d errno=%d\n", rcLnx, errno), VERR_NEM_IPE_5);
1365
1366 return VINF_SUCCESS;
1367}
1368
1369
1370#if 0
1371/**
1372 * Handles KVM_EXIT_INTERNAL_ERROR.
1373 */
1374static VBOXSTRICTRC nemR3LnxHandleInternalError(PVMCPU pVCpu, struct kvm_run *pRun)
1375{
1376 Log(("NEM: KVM_EXIT_INTERNAL_ERROR! suberror=%#x (%d) ndata=%u data=%.*Rhxs\n", pRun->internal.suberror,
1377 pRun->internal.suberror, pRun->internal.ndata, sizeof(pRun->internal.data), &pRun->internal.data[0]));
1378
1379 /*
1380 * Deal with each suberror, returning if we don't want IEM to handle it.
1381 */
1382 switch (pRun->internal.suberror)
1383 {
1384 case KVM_INTERNAL_ERROR_EMULATION:
1385 {
1386 EMHistoryAddExit(pVCpu, EMEXIT_MAKE_FT(EMEXIT_F_KIND_NEM, NEMEXITTYPE_INTERNAL_ERROR_EMULATION),
1387 pRun->s.regs.regs.rip + pRun->s.regs.sregs.cs.base, ASMReadTSC());
1388 STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatExitInternalErrorEmulation);
1389 break;
1390 }
1391
1392 case KVM_INTERNAL_ERROR_SIMUL_EX:
1393 case KVM_INTERNAL_ERROR_DELIVERY_EV:
1394 case KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON:
1395 default:
1396 {
1397 EMHistoryAddExit(pVCpu, EMEXIT_MAKE_FT(EMEXIT_F_KIND_NEM, NEMEXITTYPE_INTERNAL_ERROR_FATAL),
1398 pRun->s.regs.regs.rip + pRun->s.regs.sregs.cs.base, ASMReadTSC());
1399 STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatExitInternalErrorFatal);
1400 const char *pszName;
1401 switch (pRun->internal.suberror)
1402 {
1403 case KVM_INTERNAL_ERROR_EMULATION: pszName = "KVM_INTERNAL_ERROR_EMULATION"; break;
1404 case KVM_INTERNAL_ERROR_SIMUL_EX: pszName = "KVM_INTERNAL_ERROR_SIMUL_EX"; break;
1405 case KVM_INTERNAL_ERROR_DELIVERY_EV: pszName = "KVM_INTERNAL_ERROR_DELIVERY_EV"; break;
1406 case KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON: pszName = "KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON"; break;
1407 default: pszName = "unknown"; break;
1408 }
1409 LogRel(("NEM: KVM_EXIT_INTERNAL_ERROR! suberror=%#x (%s) ndata=%u data=%.*Rhxs\n", pRun->internal.suberror, pszName,
1410 pRun->internal.ndata, sizeof(pRun->internal.data), &pRun->internal.data[0]));
1411 return VERR_NEM_IPE_0;
1412 }
1413 }
1414
1415 /*
1416 * Execute instruction in IEM and try get on with it.
1417 */
1418 Log2(("nemR3LnxHandleInternalError: Executing instruction at %04x:%08RX64 in IEM\n",
1419 pRun->s.regs.sregs.cs.selector, pRun->s.regs.regs.rip));
1420 VBOXSTRICTRC rcStrict = nemHCLnxImportState(pVCpu,
1421 IEM_CPUMCTX_EXTRN_MUST_MASK | CPUMCTX_EXTRN_INHIBIT_INT
1422 | CPUMCTX_EXTRN_INHIBIT_NMI,
1423 &pVCpu->cpum.GstCtx, pRun);
1424 if (RT_SUCCESS(rcStrict))
1425 rcStrict = IEMExecOne(pVCpu);
1426 return rcStrict;
1427}
1428#endif
1429
1430
1431/**
1432 * Handles KVM_EXIT_MMIO.
1433 */
1434static VBOXSTRICTRC nemHCLnxHandleExitMmio(PVMCC pVM, PVMCPUCC pVCpu, struct kvm_run *pRun)
1435{
1436 /*
1437 * Input validation.
1438 */
1439 Assert(pRun->mmio.len <= sizeof(pRun->mmio.data));
1440 Assert(pRun->mmio.is_write <= 1);
1441
1442#if 0
1443 /*
1444 * We cannot easily act on the exit history here, because the MMIO port
1445 * exit is stateful and the instruction will be completed in the next
1446 * KVM_RUN call. There seems no way to circumvent this.
1447 */
1448 EMHistoryAddExit(pVCpu,
1449 pRun->mmio.is_write
1450 ? EMEXIT_MAKE_FT(EMEXIT_F_KIND_EM, EMEXITTYPE_MMIO_WRITE)
1451 : EMEXIT_MAKE_FT(EMEXIT_F_KIND_EM, EMEXITTYPE_MMIO_READ),
1452 pRun->s.regs.regs.pc, ASMReadTSC());
1453#endif
1454
1455 /*
1456 * Do the requested job.
1457 */
1458 VBOXSTRICTRC rcStrict;
1459 if (pRun->mmio.is_write)
1460 {
1461 rcStrict = PGMPhysWrite(pVM, pRun->mmio.phys_addr, pRun->mmio.data, pRun->mmio.len, PGMACCESSORIGIN_HM);
1462 Log4(("MmioExit/%u:WRITE %#x LB %u, %.*Rhxs -> rcStrict=%Rrc\n",
1463 pVCpu->idCpu,
1464 pRun->mmio.phys_addr, pRun->mmio.len, pRun->mmio.len, pRun->mmio.data, VBOXSTRICTRC_VAL(rcStrict) ));
1465 }
1466 else
1467 {
1468 rcStrict = PGMPhysRead(pVM, pRun->mmio.phys_addr, pRun->mmio.data, pRun->mmio.len, PGMACCESSORIGIN_HM);
1469 Log4(("MmioExit/%u: READ %#x LB %u -> %.*Rhxs rcStrict=%Rrc\n",
1470 pVCpu->idCpu,
1471 pRun->mmio.phys_addr, pRun->mmio.len, pRun->mmio.len, pRun->mmio.data, VBOXSTRICTRC_VAL(rcStrict) ));
1472 }
1473 return rcStrict;
1474}
1475
1476
1477static VBOXSTRICTRC nemHCLnxHandleExit(PVMCC pVM, PVMCPUCC pVCpu, struct kvm_run *pRun, bool *pfStatefulExit)
1478{
1479 STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatExitTotal);
1480 RT_NOREF(pfStatefulExit);
1481 switch (pRun->exit_reason)
1482 {
1483 case KVM_EXIT_EXCEPTION:
1484 AssertFailed();
1485 break;
1486
1487 case KVM_EXIT_MMIO:
1488 STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatExitMmio);
1489 *pfStatefulExit = true;
1490 return nemHCLnxHandleExitMmio(pVM, pVCpu, pRun);
1491
1492 case KVM_EXIT_INTR: /* EINTR */
1493 //EMHistoryAddExit(pVCpu, EMEXIT_MAKE_FT(EMEXIT_F_KIND_NEM, NEMEXITTYPE_INTERRUPTED),
1494 // pRun->s.regs.regs.pc, ASMReadTSC());
1495 STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatExitIntr);
1496 Log5(("Intr/%u\n", pVCpu->idCpu));
1497 return VINF_SUCCESS;
1498
1499#if 0
1500 case KVM_EXIT_HYPERCALL:
1501 STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatExitHypercall);
1502 AssertFailed();
1503 break;
1504
1505 case KVM_EXIT_DEBUG:
1506 STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatExitDebug);
1507 AssertFailed();
1508 break;
1509
1510 case KVM_EXIT_SYSTEM_EVENT:
1511 AssertFailed();
1512 break;
1513 case KVM_EXIT_IOAPIC_EOI:
1514 AssertFailed();
1515 break;
1516 case KVM_EXIT_HYPERV:
1517 AssertFailed();
1518 break;
1519
1520 case KVM_EXIT_DIRTY_RING_FULL:
1521 AssertFailed();
1522 break;
1523 case KVM_EXIT_AP_RESET_HOLD:
1524 AssertFailed();
1525 break;
1526 case KVM_EXIT_X86_BUS_LOCK:
1527 STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatExitBusLock);
1528 AssertFailed();
1529 break;
1530
1531
1532 case KVM_EXIT_SHUTDOWN:
1533 AssertFailed();
1534 break;
1535
1536 case KVM_EXIT_FAIL_ENTRY:
1537 LogRel(("NEM: KVM_EXIT_FAIL_ENTRY! hardware_entry_failure_reason=%#x cpu=%#x\n",
1538 pRun->fail_entry.hardware_entry_failure_reason, pRun->fail_entry.cpu));
1539 EMHistoryAddExit(pVCpu, EMEXIT_MAKE_FT(EMEXIT_F_KIND_NEM, NEMEXITTYPE_FAILED_ENTRY),
1540 pRun->s.regs.regs.pc, ASMReadTSC());
1541 return VERR_NEM_IPE_1;
1542
1543 case KVM_EXIT_INTERNAL_ERROR:
1544 /* we're counting sub-reasons inside the function. */
1545 return nemR3LnxHandleInternalError(pVCpu, pRun);
1546#endif
1547
1548 /*
1549 * Foreign and unknowns.
1550 */
1551#if 0
1552 case KVM_EXIT_IO:
1553 AssertLogRelMsgFailedReturn(("KVM_EXIT_IO on VCpu #%u at %RX64!\n", pVCpu->idCpu, pRun->s.regs.pc), VERR_NEM_IPE_1);
1554 case KVM_EXIT_NMI:
1555 AssertLogRelMsgFailedReturn(("KVM_EXIT_NMI on VCpu #%u at %RX64!\n", pVCpu->idCpu, pRun->s.regs.pc), VERR_NEM_IPE_1);
1556 case KVM_EXIT_EPR:
1557 AssertLogRelMsgFailedReturn(("KVM_EXIT_EPR on VCpu #%u at %RX64!\n", pVCpu->idCpu, pRun->s.regs.pc), VERR_NEM_IPE_1);
1558 case KVM_EXIT_WATCHDOG:
1559 AssertLogRelMsgFailedReturn(("KVM_EXIT_WATCHDOG on VCpu #%u at %RX64!\n", pVCpu->idCpu, pRun->s.regs.pc), VERR_NEM_IPE_1);
1560 case KVM_EXIT_ARM_NISV:
1561 AssertLogRelMsgFailedReturn(("KVM_EXIT_ARM_NISV on VCpu #%u at %RX64!\n", pVCpu->idCpu, pRun->s.regs.pc), VERR_NEM_IPE_1);
1562 case KVM_EXIT_S390_STSI:
1563 AssertLogRelMsgFailedReturn(("KVM_EXIT_S390_STSI on VCpu #%u at %RX64!\n", pVCpu->idCpu, pRun->s.regs.pc), VERR_NEM_IPE_1);
1564 case KVM_EXIT_S390_TSCH:
1565 AssertLogRelMsgFailedReturn(("KVM_EXIT_S390_TSCH on VCpu #%u at %RX64!\n", pVCpu->idCpu, pRun->s.regs.pc), VERR_NEM_IPE_1);
1566 case KVM_EXIT_OSI:
1567 AssertLogRelMsgFailedReturn(("KVM_EXIT_OSI on VCpu #%u at %RX64!\n", pVCpu->idCpu, pRun->s.regs.pc), VERR_NEM_IPE_1);
1568 case KVM_EXIT_PAPR_HCALL:
1569 AssertLogRelMsgFailedReturn(("KVM_EXIT_PAPR_HCALL on VCpu #%u at %RX64!\n", pVCpu->idCpu, pRun->s.regs.pc), VERR_NEM_IPE_1);
1570 case KVM_EXIT_S390_UCONTROL:
1571 AssertLogRelMsgFailedReturn(("KVM_EXIT_S390_UCONTROL on VCpu #%u at %RX64!\n", pVCpu->idCpu, pRun->s.regs.pc), VERR_NEM_IPE_1);
1572 case KVM_EXIT_DCR:
1573 AssertLogRelMsgFailedReturn(("KVM_EXIT_DCR on VCpu #%u at %RX64!\n", pVCpu->idCpu, pRun->s.regs.pc), VERR_NEM_IPE_1);
1574 case KVM_EXIT_S390_SIEIC:
1575 AssertLogRelMsgFailedReturn(("KVM_EXIT_S390_SIEIC on VCpu #%u at %RX64!\n", pVCpu->idCpu, pRun->s.regs.pc), VERR_NEM_IPE_1);
1576 case KVM_EXIT_S390_RESET:
1577 AssertLogRelMsgFailedReturn(("KVM_EXIT_S390_RESET on VCpu #%u at %RX64!\n", pVCpu->idCpu, pRun->s.regs.pc), VERR_NEM_IPE_1);
1578 case KVM_EXIT_UNKNOWN:
1579 AssertLogRelMsgFailedReturn(("KVM_EXIT_UNKNOWN on VCpu #%u at %RX64!\n", pVCpu->idCpu, pRun->s.regs.pc), VERR_NEM_IPE_1);
1580 case KVM_EXIT_XEN:
1581 AssertLogRelMsgFailedReturn(("KVM_EXIT_XEN on VCpu #%u at %RX64!\n", pVCpu->idCpu, pRun->s.regs.pc), VERR_NEM_IPE_1);
1582#endif
1583 default:
1584 AssertLogRelMsgFailedReturn(("Unknown exit reason %u on VCpu #%u!\n", pRun->exit_reason, pVCpu->idCpu), VERR_NEM_IPE_1);
1585 }
1586 RT_NOREF(pVM, pVCpu);
1587 return VERR_NOT_IMPLEMENTED;
1588}
1589
1590
1591VBOXSTRICTRC nemR3NativeRunGC(PVM pVM, PVMCPU pVCpu)
1592{
1593 /*
1594 * Try switch to NEM runloop state.
1595 */
1596 if (VMCPU_CMPXCHG_STATE(pVCpu, VMCPUSTATE_STARTED_EXEC_NEM, VMCPUSTATE_STARTED))
1597 { /* likely */ }
1598 else
1599 {
1600 VMCPU_CMPXCHG_STATE(pVCpu, VMCPUSTATE_STARTED_EXEC_NEM, VMCPUSTATE_STARTED_EXEC_NEM_CANCELED);
1601 LogFlow(("NEM/%u: returning immediately because canceled\n", pVCpu->idCpu));
1602 return VINF_SUCCESS;
1603 }
1604
1605 /*
1606 * The run loop.
1607 */
1608 struct kvm_run * const pRun = pVCpu->nem.s.pRun;
1609 const bool fSingleStepping = DBGFIsStepping(pVCpu);
1610 VBOXSTRICTRC rcStrict = VINF_SUCCESS;
1611 bool fStatefulExit = false; /* For MMIO and IO exits. */
1612 for (unsigned iLoop = 0;; iLoop++)
1613 {
1614 /*
1615 * Pending interrupts or such? Need to check and deal with this prior
1616 * to the state syncing.
1617 */
1618 if (VMCPU_FF_IS_ANY_SET(pVCpu, VMCPU_FF_INTERRUPT_IRQ | VMCPU_FF_INTERRUPT_FIQ
1619 | VMCPU_FF_INTERRUPT_NMI))
1620 {
1621 /* Try inject interrupt. */
1622 rcStrict = nemHCLnxHandleInterruptFF(pVM, pVCpu, pRun);
1623 if (rcStrict == VINF_SUCCESS)
1624 { /* likely */ }
1625 else
1626 {
1627 LogFlow(("NEM/%u: breaking: nemHCLnxHandleInterruptFF -> %Rrc\n", pVCpu->idCpu, VBOXSTRICTRC_VAL(rcStrict) ));
1628 STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatBreakOnStatus);
1629 break;
1630 }
1631 }
1632
1633 /*
1634 * Ensure KVM has the whole state.
1635 */
1636 if ((pVCpu->cpum.GstCtx.fExtrn & CPUMCTX_EXTRN_ALL) != CPUMCTX_EXTRN_ALL)
1637 {
1638 int rc2 = nemHCLnxExportState(pVM, pVCpu, &pVCpu->cpum.GstCtx, pRun);
1639 AssertRCReturn(rc2, rc2);
1640 }
1641
1642 /*
1643 * Poll timers and run for a bit.
1644 *
1645 * With the VID approach (ring-0 or ring-3) we can specify a timeout here,
1646 * so we take the time of the next timer event and uses that as a deadline.
1647 * The rounding heuristics are "tuned" so that rhel5 (1K timer) will boot fine.
1648 */
1649 /** @todo See if we cannot optimize this TMTimerPollGIP by only redoing
1650 * the whole polling job when timers have changed... */
1651 uint64_t offDeltaIgnored;
1652 uint64_t const nsNextTimerEvt = TMTimerPollGIP(pVM, pVCpu, &offDeltaIgnored); NOREF(nsNextTimerEvt);
1653 if ( !VM_FF_IS_ANY_SET(pVM, VM_FF_EMT_RENDEZVOUS | VM_FF_TM_VIRTUAL_SYNC)
1654 && !VMCPU_FF_IS_ANY_SET(pVCpu, VMCPU_FF_HM_TO_R3_MASK))
1655 {
1656 if (VMCPU_CMPXCHG_STATE(pVCpu, VMCPUSTATE_STARTED_EXEC_NEM_WAIT, VMCPUSTATE_STARTED_EXEC_NEM))
1657 {
1658 //LogFlow(("NEM/%u: Entry @ %04x:%08RX64 IF=%d EFL=%#RX64 SS:RSP=%04x:%08RX64 cr0=%RX64\n",
1659 // pVCpu->idCpu, pRun->s.regs.sregs.cs.selector, pRun->s.regs.regs.rip,
1660 // !!(pRun->s.regs.regs.rflags & X86_EFL_IF), pRun->s.regs.regs.rflags,
1661 // pRun->s.regs.sregs.ss.selector, pRun->s.regs.regs.rsp, pRun->s.regs.sregs.cr0));
1662 TMNotifyStartOfExecution(pVM, pVCpu);
1663
1664 int rcLnx = ioctl(pVCpu->nem.s.fdVCpu, KVM_RUN, 0UL);
1665
1666 VMCPU_CMPXCHG_STATE(pVCpu, VMCPUSTATE_STARTED_EXEC_NEM, VMCPUSTATE_STARTED_EXEC_NEM_WAIT);
1667 TMNotifyEndOfExecution(pVM, pVCpu, ASMReadTSC());
1668
1669#if 0 //def LOG_ENABLED
1670 if (LogIsFlowEnabled())
1671 {
1672 struct kvm_mp_state MpState = {UINT32_MAX};
1673 ioctl(pVCpu->nem.s.fdVCpu, KVM_GET_MP_STATE, &MpState);
1674 LogFlow(("NEM/%u: Exit @ %04x:%08RX64 IF=%d EFL=%#RX64 CR8=%#x Reason=%#x IrqReady=%d Flags=%#x %#lx\n", pVCpu->idCpu,
1675 pRun->s.regs.sregs.cs.selector, pRun->s.regs.regs.rip, pRun->if_flag,
1676 pRun->s.regs.regs.rflags, pRun->s.regs.sregs.cr8, pRun->exit_reason,
1677 pRun->ready_for_interrupt_injection, pRun->flags, MpState.mp_state));
1678 }
1679#endif
1680 fStatefulExit = false;
1681 if (RT_LIKELY(rcLnx == 0 || errno == EINTR))
1682 {
1683 /*
1684 * Deal with the exit.
1685 */
1686 rcStrict = nemHCLnxHandleExit(pVM, pVCpu, pRun, &fStatefulExit);
1687 if (rcStrict == VINF_SUCCESS)
1688 { /* hopefully likely */ }
1689 else
1690 {
1691 LogFlow(("NEM/%u: breaking: nemHCLnxHandleExit -> %Rrc\n", pVCpu->idCpu, VBOXSTRICTRC_VAL(rcStrict) ));
1692 STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatBreakOnStatus);
1693 break;
1694 }
1695 }
1696 else
1697 {
1698 int rc2 = RTErrConvertFromErrno(errno);
1699 AssertLogRelMsgFailedReturn(("KVM_RUN failed: rcLnx=%d errno=%u rc=%Rrc\n", rcLnx, errno, rc2), rc2);
1700 }
1701
1702 /*
1703 * If no relevant FFs are pending, loop.
1704 */
1705 if ( !VM_FF_IS_ANY_SET( pVM, !fSingleStepping ? VM_FF_HP_R0_PRE_HM_MASK : VM_FF_HP_R0_PRE_HM_STEP_MASK)
1706 && !VMCPU_FF_IS_ANY_SET(pVCpu, !fSingleStepping ? VMCPU_FF_HP_R0_PRE_HM_MASK : VMCPU_FF_HP_R0_PRE_HM_STEP_MASK) )
1707 { /* likely */ }
1708 else
1709 {
1710
1711 /** @todo Try handle pending flags, not just return to EM loops. Take care
1712 * not to set important RCs here unless we've handled an exit. */
1713 LogFlow(("NEM/%u: breaking: pending FF (%#x / %#RX64)\n",
1714 pVCpu->idCpu, pVM->fGlobalForcedActions, (uint64_t)pVCpu->fLocalForcedActions));
1715 STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatBreakOnFFPost);
1716 break;
1717 }
1718 }
1719 else
1720 {
1721 LogFlow(("NEM/%u: breaking: canceled %d (pre exec)\n", pVCpu->idCpu, VMCPU_GET_STATE(pVCpu) ));
1722 STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatBreakOnCancel);
1723 break;
1724 }
1725 }
1726 else
1727 {
1728 LogFlow(("NEM/%u: breaking: pending FF (pre exec)\n", pVCpu->idCpu));
1729 STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatBreakOnFFPre);
1730 break;
1731 }
1732 } /* the run loop */
1733
1734
1735 /*
1736 * If the last exit was stateful, commit the state we provided before
1737 * returning to the EM loop so we have a consistent state and can safely
1738 * be rescheduled and whatnot. This may require us to make multiple runs
1739 * for larger MMIO and I/O operations. Sigh^3.
1740 *
1741 * Note! There is no 'ing way to reset the kernel side completion callback
1742 * for these stateful i/o exits. Very annoying interface.
1743 */
1744 /** @todo check how this works with string I/O and string MMIO. */
1745 if (fStatefulExit && RT_SUCCESS(rcStrict))
1746 {
1747 STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatFlushExitOnReturn);
1748 uint32_t const uOrgExit = pRun->exit_reason;
1749 for (uint32_t i = 0; ; i++)
1750 {
1751 pRun->immediate_exit = 1;
1752 int rcLnx = ioctl(pVCpu->nem.s.fdVCpu, KVM_RUN, 0UL);
1753 Log(("NEM/%u: Flushed stateful exit -> %d/%d exit_reason=%d\n", pVCpu->idCpu, rcLnx, errno, pRun->exit_reason));
1754 if (rcLnx == -1 && errno == EINTR)
1755 {
1756 switch (i)
1757 {
1758 case 0: STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatFlushExitOnReturn1Loop); break;
1759 case 1: STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatFlushExitOnReturn2Loops); break;
1760 case 2: STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatFlushExitOnReturn3Loops); break;
1761 default: STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatFlushExitOnReturn4PlusLoops); break;
1762 }
1763 break;
1764 }
1765 AssertLogRelMsgBreakStmt(rcLnx == 0 && pRun->exit_reason == uOrgExit,
1766 ("rcLnx=%d errno=%d exit_reason=%d uOrgExit=%d\n", rcLnx, errno, pRun->exit_reason, uOrgExit),
1767 rcStrict = VERR_NEM_IPE_6);
1768 VBOXSTRICTRC rcStrict2 = nemHCLnxHandleExit(pVM, pVCpu, pRun, &fStatefulExit);
1769 if (rcStrict2 == VINF_SUCCESS || rcStrict2 == rcStrict)
1770 { /* likely */ }
1771 else if (RT_FAILURE(rcStrict2))
1772 {
1773 rcStrict = rcStrict2;
1774 break;
1775 }
1776 else
1777 {
1778 AssertLogRelMsgBreakStmt(rcStrict == VINF_SUCCESS,
1779 ("rcStrict=%Rrc rcStrict2=%Rrc\n", VBOXSTRICTRC_VAL(rcStrict), VBOXSTRICTRC_VAL(rcStrict2)),
1780 rcStrict = VERR_NEM_IPE_7);
1781 rcStrict = rcStrict2;
1782 }
1783 }
1784 pRun->immediate_exit = 0;
1785 }
1786
1787 /*
1788 * If the CPU is running, make sure to stop it before we try sync back the
1789 * state and return to EM. We don't sync back the whole state if we can help it.
1790 */
1791 if (!VMCPU_CMPXCHG_STATE(pVCpu, VMCPUSTATE_STARTED, VMCPUSTATE_STARTED_EXEC_NEM))
1792 VMCPU_CMPXCHG_STATE(pVCpu, VMCPUSTATE_STARTED, VMCPUSTATE_STARTED_EXEC_NEM_CANCELED);
1793
1794 if (pVCpu->cpum.GstCtx.fExtrn & CPUMCTX_EXTRN_ALL)
1795 {
1796 /* Try anticipate what we might need. */
1797 uint64_t fImport = IEM_CPUMCTX_EXTRN_MUST_MASK /*?*/;
1798 if ( (rcStrict >= VINF_EM_FIRST && rcStrict <= VINF_EM_LAST)
1799 || RT_FAILURE(rcStrict))
1800 fImport = CPUMCTX_EXTRN_ALL;
1801 else if (VMCPU_FF_IS_ANY_SET(pVCpu, VMCPU_FF_INTERRUPT_IRQ | VMCPU_FF_INTERRUPT_FIQ))
1802 fImport |= IEM_CPUMCTX_EXTRN_XCPT_MASK;
1803
1804 if (pVCpu->cpum.GstCtx.fExtrn & fImport)
1805 {
1806 int rc2 = nemHCLnxImportState(pVCpu, fImport, &pVCpu->cpum.GstCtx, pRun);
1807 if (RT_SUCCESS(rc2))
1808 pVCpu->cpum.GstCtx.fExtrn &= ~fImport;
1809 else if (RT_SUCCESS(rcStrict))
1810 rcStrict = rc2;
1811 if (!(pVCpu->cpum.GstCtx.fExtrn & CPUMCTX_EXTRN_ALL))
1812 pVCpu->cpum.GstCtx.fExtrn = 0;
1813 STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatImportOnReturn);
1814 }
1815 else
1816 STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatImportOnReturnSkipped);
1817 }
1818 else
1819 {
1820 pVCpu->cpum.GstCtx.fExtrn = 0;
1821 STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatImportOnReturnSkipped);
1822 }
1823
1824 LogFlow(("NEM/%u: %08RX64 => %Rrc\n", pVCpu->idCpu, pVCpu->cpum.GstCtx.Pc, VBOXSTRICTRC_VAL(rcStrict) ));
1825 return rcStrict;
1826}
1827
1828
1829/** @page pg_nem_linux NEM/linux - Native Execution Manager, Linux.
1830 *
1831 * This is using KVM.
1832 *
1833 */
1834
Note: See TracBrowser for help on using the repository browser.

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