1 | /* $Id: HMSVMR0.cpp 46365 2013-06-03 15:02:41Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * HM SVM (AMD-V) - Host Context Ring-0.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2013 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | /*******************************************************************************
|
---|
19 | * Header Files *
|
---|
20 | *******************************************************************************/
|
---|
21 |
|
---|
22 | #ifdef DEBUG_ramshankar
|
---|
23 | # define HMSVM_ALWAYS_TRAP_ALL_XCPTS
|
---|
24 | # define HMSVM_ALWAYS_TRAP_PF
|
---|
25 | #endif
|
---|
26 |
|
---|
27 |
|
---|
28 | /*******************************************************************************
|
---|
29 | * Defined Constants And Macros *
|
---|
30 | *******************************************************************************/
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * MSR-bitmap read permissions.
|
---|
34 | */
|
---|
35 | typedef enum SVMMSREXITREAD
|
---|
36 | {
|
---|
37 | /** Reading this MSR causes a VM-exit. */
|
---|
38 | SVMMSREXIT_INTERCEPT_READ = 0xb,
|
---|
39 | /** Reading this MSR does not cause a VM-exit. */
|
---|
40 | SVMMSREXIT_PASSTHRU_READ
|
---|
41 | } VMXMSREXITREAD;
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * MSR-bitmap write permissions.
|
---|
45 | */
|
---|
46 | typedef enum SVMMSREXITWRITE
|
---|
47 | {
|
---|
48 | /** Writing to this MSR causes a VM-exit. */
|
---|
49 | SVMMSREXIT_INTERCEPT_WRITE = 0xd,
|
---|
50 | /** Writing to this MSR does not cause a VM-exit. */
|
---|
51 | SVMMSREXIT_PASSTHRU_WRITE
|
---|
52 | } VMXMSREXITWRITE;
|
---|
53 |
|
---|
54 |
|
---|
55 | /*******************************************************************************
|
---|
56 | * Internal Functions *
|
---|
57 | *******************************************************************************/
|
---|
58 | static void hmR0SvmSetMSRPermission(PVMCPU pVCpu, unsigned uMsr, SVMMSREXITREAD enmRead, SVMMSREXITWRITE enmWrite);
|
---|
59 |
|
---|
60 |
|
---|
61 | /*******************************************************************************
|
---|
62 | * Global Variables *
|
---|
63 | *******************************************************************************/
|
---|
64 | /** Ring-0 memory object for the IO bitmap. */
|
---|
65 | RTR0MEMOBJ g_hMemObjIOBitmap = NIL_RTR0MEMOBJ;
|
---|
66 | /** Physical address of the IO bitmap. */
|
---|
67 | RTHCPHYS g_HCPhysIOBitmap = 0;
|
---|
68 | /** Virtual address of the IO bitmap. */
|
---|
69 | R0PTRTYPE(void *) g_pvIOBitmap = NULL;
|
---|
70 |
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Sets up and activates AMD-V on the current CPU.
|
---|
74 | *
|
---|
75 | * @returns VBox status code.
|
---|
76 | * @param pCpu Pointer to the CPU info struct.
|
---|
77 | * @param pVM Pointer to the VM (can be NULL after a resume!).
|
---|
78 | * @param pvCpuPage Pointer to the global CPU page.
|
---|
79 | * @param HCPhysCpuPage Physical address of the global CPU page.
|
---|
80 | */
|
---|
81 | VMMR0DECL(int) SVMR0EnableCpu(PHMGLOBLCPUINFO pCpu, PVM pVM, void *pvCpuPage, RTHCPHYS HCPhysCpuPage, bool fEnabledByHost)
|
---|
82 | {
|
---|
83 | AssertReturn(!fEnabledByHost, VERR_INVALID_PARAMETER);
|
---|
84 | AssertReturn( HCPhysCpuPage
|
---|
85 | && HCPhysCpuPage != NIL_RTHCPHYS, VERR_INVALID_PARAMETER);
|
---|
86 | AssertReturn(pvCpuPage, VERR_INVALID_PARAMETER);
|
---|
87 |
|
---|
88 | /*
|
---|
89 | * We must turn on AMD-V and setup the host state physical address, as those MSRs are per CPU.
|
---|
90 | */
|
---|
91 | uint64_t u64HostEfer = ASMRdMsr(MSR_K6_EFER);
|
---|
92 | if (u64HostEfer & MSR_K6_EFER_SVME)
|
---|
93 | {
|
---|
94 | /* If the VBOX_HWVIRTEX_IGNORE_SVM_IN_USE is active, then we blindly use AMD-V. */
|
---|
95 | if ( pVM
|
---|
96 | && pVM->hm.s.svm.fIgnoreInUseError)
|
---|
97 | {
|
---|
98 | pCpu->fIgnoreAMDVInUseError = true;
|
---|
99 | }
|
---|
100 |
|
---|
101 | if (!pCpu->fIgnoreAMDVInUseError)
|
---|
102 | return VERR_SVM_IN_USE;
|
---|
103 | }
|
---|
104 |
|
---|
105 | /* Turn on AMD-V in the EFER MSR. */
|
---|
106 | ASMWrMsr(MSR_K6_EFER, u64HostEfer | MSR_K6_EFER_SVME);
|
---|
107 |
|
---|
108 | /* Write the physical page address where the CPU will store the host state while executing the VM. */
|
---|
109 | ASMWrMsr(MSR_K8_VM_HSAVE_PA, HCPhysCpuPage);
|
---|
110 |
|
---|
111 | /*
|
---|
112 | * Theoretically, other hypervisors may have used ASIDs, ideally we should flush all non-zero ASIDs
|
---|
113 | * when enabling SVM. AMD doesn't have an SVM instruction to flush all ASIDs (flushing is done
|
---|
114 | * upon VMRUN). Therefore, just set the fFlushAsidBeforeUse flag which instructs hmR0SvmSetupTLB()
|
---|
115 | * to flush the TLB with before using a new ASID.
|
---|
116 | */
|
---|
117 | pCpu->fFlushAsidBeforeUse = true;
|
---|
118 |
|
---|
119 | /*
|
---|
120 | * Ensure each VCPU scheduled on this CPU gets a new VPID on resume. See @bugref{6255}.
|
---|
121 | */
|
---|
122 | ++pCpu->cTlbFlushes;
|
---|
123 |
|
---|
124 | return VINF_SUCCESS;
|
---|
125 | }
|
---|
126 |
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * Deactivates AMD-V on the current CPU.
|
---|
130 | *
|
---|
131 | * @returns VBox status code.
|
---|
132 | * @param pCpu Pointer to the CPU info struct.
|
---|
133 | * @param pvCpuPage Pointer to the global CPU page.
|
---|
134 | * @param HCPhysCpuPage Physical address of the global CPU page.
|
---|
135 | */
|
---|
136 | VMMR0DECL(int) SVMR0DisableCpu(PHMGLOBLCPUINFO pCpu, void *pvCpuPage, RTHCPHYS HCPhysCpuPage)
|
---|
137 | {
|
---|
138 | AssertReturn( HCPhysCpuPage
|
---|
139 | && HCPhysCpuPage != NIL_RTHCPHYS, VERR_INVALID_PARAMETER);
|
---|
140 | AssertReturn(pvCpuPage, VERR_INVALID_PARAMETER);
|
---|
141 | NOREF(pCpu);
|
---|
142 |
|
---|
143 | /* Turn off AMD-V in the EFER MSR if AMD-V is active. */
|
---|
144 | uint64_t u64HostEfer = ASMRdMsr(MSR_K6_EFER);
|
---|
145 | if (u64HostEfer & MSR_K6_EFER_SVME)
|
---|
146 | {
|
---|
147 | ASMWrMsr(MSR_K6_EFER, u64HostEfer & ~MSR_K6_EFER_SVME);
|
---|
148 |
|
---|
149 | /* Invalidate host state physical address. */
|
---|
150 | ASMWrMsr(MSR_K8_VM_HSAVE_PA, 0);
|
---|
151 | }
|
---|
152 |
|
---|
153 | return VINF_SUCCESS;
|
---|
154 | }
|
---|
155 |
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * Does global AMD-V initialization (called during module initialization).
|
---|
159 | *
|
---|
160 | * @returns VBox status code.
|
---|
161 | */
|
---|
162 | VMMR0DECL(int) SVMR0GlobalInit(void)
|
---|
163 | {
|
---|
164 | /*
|
---|
165 | * Allocate 12 KB for the IO bitmap. Since this is non-optional and we always intercept all IO accesses, it's done
|
---|
166 | * once globally here instead of per-VM.
|
---|
167 | */
|
---|
168 | int rc = RTR0MemObjAllocCont(&g_hMemObjIOBitmap, 3 << PAGE_SHIFT, false /* fExecutable */);
|
---|
169 | if (RT_FAILURE(rc))
|
---|
170 | return rc;
|
---|
171 |
|
---|
172 | g_pvIOBitmap = RTR0MemObjAddress(g_hMemObjIOBitmap);
|
---|
173 | g_HCPhysIOBitmap = RTR0MemObjGetPagePhysAddr(g_hMemObjIOBitmap, 0 /* iPage */);
|
---|
174 |
|
---|
175 | /* Set all bits to intercept all IO accesses. */
|
---|
176 | ASMMemFill32(pVM->hm.s.svm.pvIOBitmap, 3 << PAGE_SHIFT, UINT32_C(0xffffffff));
|
---|
177 | }
|
---|
178 |
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * Does global VT-x termination (called during module termination).
|
---|
182 | */
|
---|
183 | VMMR0DECL(void) SVMR0GlobalTerm(void)
|
---|
184 | {
|
---|
185 | if (g_hMemObjIOBitmap != NIL_RTR0MEMOBJ)
|
---|
186 | {
|
---|
187 | RTR0MemObjFree(pVM->hm.s.svm.hMemObjIOBitmap, false /* fFreeMappings */);
|
---|
188 | g_pvIOBitmap = NULL;
|
---|
189 | g_HCPhysIOBitmap = 0;
|
---|
190 | g_hMemObjIOBitmap = NIL_RTR0MEMOBJ;
|
---|
191 | }
|
---|
192 | }
|
---|
193 |
|
---|
194 |
|
---|
195 | /**
|
---|
196 | * Frees any allocated per-VCPU structures for a VM.
|
---|
197 | *
|
---|
198 | * @param pVM Pointer to the VM.
|
---|
199 | */
|
---|
200 | DECLINLINE(void) hmR0SvmFreeStructs(PVM pVM)
|
---|
201 | {
|
---|
202 | for (uint32_t i = 0; i < pVM->cCpus; i++)
|
---|
203 | {
|
---|
204 | PVMCPU pVCpu = &pVM->aCpus[i];
|
---|
205 |
|
---|
206 | if (pVCpu->hm.s.svm.hMemObjVmcbHost != NIL_RTR0MEMOBJ)
|
---|
207 | {
|
---|
208 | RTR0MemObjFree(pVCpu->hm.s.svm.hMemObjVmcbHost, false);
|
---|
209 | pVCpu->hm.s.svm.pvVmcbHost = 0;
|
---|
210 | pVCpu->hm.s.svm.HCPhysVmcbHost = 0;
|
---|
211 | pVCpu->hm.s.svm.hMemObjVmcbHost = NIL_RTR0MEMOBJ;
|
---|
212 | }
|
---|
213 |
|
---|
214 | if (pVCpu->hm.s.svm.hMemObjVmcb != NIL_RTR0MEMOBJ)
|
---|
215 | {
|
---|
216 | RTR0MemObjFree(pVCpu->hm.s.svm.hMemObjVmcb, false);
|
---|
217 | pVCpu->hm.s.svm.pvVmcb = 0;
|
---|
218 | pVCpu->hm.s.svm.HCPhysVmcb = 0;
|
---|
219 | pVCpu->hm.s.svm.hMemObjVmcb = NIL_RTR0MEMOBJ;
|
---|
220 | }
|
---|
221 |
|
---|
222 | if (pVCpu->hm.s.svm.hMemObjMsrBitmap != NIL_RTR0MEMOBJ)
|
---|
223 | {
|
---|
224 | RTR0MemObjFree(pVCpu->hm.s.svm.hMemObjMsrBitmap, false);
|
---|
225 | pVCpu->hm.s.svm.pvMsrBitmap = 0;
|
---|
226 | pVCpu->hm.s.svm.HCPhysMsrBitmap = 0;
|
---|
227 | pVCpu->hm.s.svm.hMemObjMsrBitmap = NIL_RTR0MEMOBJ;
|
---|
228 | }
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 |
|
---|
233 | /**
|
---|
234 | * Does per-VM AMD-V initialization.
|
---|
235 | *
|
---|
236 | * @returns VBox status code.
|
---|
237 | * @param pVM Pointer to the VM.
|
---|
238 | */
|
---|
239 | VMMR0DECL(int) SVMR0InitVM(PVM pVM)
|
---|
240 | {
|
---|
241 | int rc = VERR_INTERNAL_ERROR_5;
|
---|
242 |
|
---|
243 | /* Check for an AMD CPU erratum which requires us to flush the TLB before every world-switch. */
|
---|
244 | uint32_t u32Family;
|
---|
245 | uint32_t u32Model;
|
---|
246 | uint32_t u32Stepping;
|
---|
247 | if (HMAmdIsSubjectToErratum170(&u32Family, &u32Model, &u32Stepping))
|
---|
248 | {
|
---|
249 | Log(("SVMR0InitVM: AMD cpu with erratum 170 family %#x model %#x stepping %#x\n", u32Family, u32Model, u32Stepping));
|
---|
250 | pVM->hm.s.svm.fAlwaysFlushTLB = true;
|
---|
251 | }
|
---|
252 |
|
---|
253 | /* Initialize the memory objects up-front so we can cleanup on allocation failures properly. */
|
---|
254 | for (uint32_t i = 0; i < pVM->cCpus; i++)
|
---|
255 | {
|
---|
256 | PVMCPU pVCpu = &pVM->aCpus[i];
|
---|
257 | pVCpu->hm.s.svm.hMemObjVmcbHost = NIL_RTR0MEMOBJ;
|
---|
258 | pVCpu->hm.s.svm.hMemObjVmcb = NIL_RTR0MEMOBJ;
|
---|
259 | pVCpu->hm.s.svm.hMemObjMsrBitmap = NIL_RTR0MEMOBJ;
|
---|
260 | }
|
---|
261 |
|
---|
262 | /* Allocate a VMCB for each VCPU. */
|
---|
263 | for (uint32_t i = 0; i < pVM->cCpus; i++)
|
---|
264 | {
|
---|
265 | /* Allocate one page for the host context */
|
---|
266 | rc = RTR0MemObjAllocCont(&pVCpu->hm.s.svm.hMemObjVmcbHost, 1 << PAGE_SHIFT, false /* fExecutable */);
|
---|
267 | if (RT_FAILURE(rc))
|
---|
268 | goto failure_cleanup;
|
---|
269 |
|
---|
270 | pVCpu->hm.s.svm.pvVmcbHost = RTR0MemObjAddress(pVCpu->hm.s.svm.hMemObjVmcbHost);
|
---|
271 | pVCpu->hm.s.svm.HCPhysVmcbHost = RTR0MemObjGetPagePhysAddr(pVCpu->hm.s.svm.hMemObjVmcbHost, 0 /* iPage */);
|
---|
272 | Assert(pVCpu->hm.s.svm.HCPhysVmcbHost < _4G);
|
---|
273 | ASMMemZeroPage(pVCpu->hm.s.svm.pvVmcbHost);
|
---|
274 |
|
---|
275 | /* Allocate one page for the VM control block (VMCB). */
|
---|
276 | rc = RTR0MemObjAllocCont(&pVCpu->hm.s.svm.hMemObjVmcb, 1 << PAGE_SHIFT, false /* fExecutable */);
|
---|
277 | if (RT_FAILURE(rc))
|
---|
278 | goto failure_cleanup;
|
---|
279 |
|
---|
280 | pVCpu->hm.s.svm.pvVmcb = RTR0MemObjAddress(pVCpu->hm.s.svm.hMemObjVmcb);
|
---|
281 | pVCpu->hm.s.svm.HCPhysVmcb = RTR0MemObjGetPagePhysAddr(pVCpu->hm.s.svm.hMemObjVmcb, 0 /* iPage */);
|
---|
282 | Assert(pVCpu->hm.s.svm.HCPhysVmcb < _4G);
|
---|
283 | ASMMemZeroPage(pVCpu->hm.s.svm.pvVmcb);
|
---|
284 |
|
---|
285 | /* Allocate 8 KB for the MSR bitmap (doesn't seem to be a way to convince SVM not to use it) */
|
---|
286 | rc = RTR0MemObjAllocCont(&pVCpu->hm.s.svm.hMemObjMsrBitmap, 2 << PAGE_SHIFT, false /* fExecutable */);
|
---|
287 | if (RT_FAILURE(rc))
|
---|
288 | failure_cleanup;
|
---|
289 |
|
---|
290 | pVCpu->hm.s.svm.pvMsrBitmap = RTR0MemObjAddress(pVCpu->hm.s.svm.hMemObjMsrBitmap);
|
---|
291 | pVCpu->hm.s.svm.HCPhysMsrBitmap = RTR0MemObjGetPagePhysAddr(pVCpu->hm.s.svm.hMemObjMsrBitmap, 0 /* iPage */);
|
---|
292 | /* Set all bits to intercept all MSR accesses. */
|
---|
293 | ASMMemFill32(pVCpu->hm.s.svm.pvMsrBitmap, 2 << PAGE_SHIFT, 0xffffffff);
|
---|
294 | }
|
---|
295 |
|
---|
296 | return VINF_SUCCESS;
|
---|
297 |
|
---|
298 | failure_cleanup:
|
---|
299 | hmR0SvmFreeVMStructs(pVM);
|
---|
300 | return rc;
|
---|
301 | }
|
---|
302 |
|
---|
303 |
|
---|
304 | /**
|
---|
305 | * Does per-VM AMD-V termination.
|
---|
306 | *
|
---|
307 | * @returns VBox status code.
|
---|
308 | * @param pVM Pointer to the VM.
|
---|
309 | */
|
---|
310 | VMMR0DECL(int) SVMR0TermVM(PVM pVM)
|
---|
311 | {
|
---|
312 | hmR0SvmFreeVMStructs(pVM);
|
---|
313 | return VINF_SUCCESS;
|
---|
314 | }
|
---|
315 |
|
---|
316 |
|
---|
317 | /**
|
---|
318 | * Sets up AMD-V for the specified VM.
|
---|
319 | * This function is only called once per-VM during initalization.
|
---|
320 | *
|
---|
321 | * @returns VBox status code.
|
---|
322 | * @param pVM Pointer to the VM.
|
---|
323 | */
|
---|
324 | VMMR0DECL(int) SVMR0SetupVM(PVM pVM)
|
---|
325 | {
|
---|
326 | int rc = VINF_SUCCESS;
|
---|
327 |
|
---|
328 | AssertReturn(pVM, VERR_INVALID_PARAMETER);
|
---|
329 | Assert(pVM->hm.s.svm.fSupported);
|
---|
330 |
|
---|
331 | for (VMCPUID i = 0; i < pVM->cCpus; i++)
|
---|
332 | {
|
---|
333 | PVMCPU pVCpu = &pVM->aCpus[i];
|
---|
334 | PSVMVMCB pVmcb = (PSVMVMCB)pVM->aCpus[i].hm.s.svm.pvVmcb;
|
---|
335 |
|
---|
336 | AssertMsgReturn(pVmcb, ("Invalid pVmcb\n"), VERR_SVM_INVALID_PVMCB);
|
---|
337 |
|
---|
338 | /* Trap exceptions unconditionally (debug purposes). */
|
---|
339 | #ifdef HMSVM_ALWAYS_TRAP_PF
|
---|
340 | pVmcb->ctrl.u32InterceptException |= RT_BIT(X86_XCPT_PF);
|
---|
341 | #endif
|
---|
342 | #ifdef HMSVM_ALWAYS_TRAP_ALL_XCPTS
|
---|
343 | pVmcb->ctrl.u32InterceptException |= RT_BIT(X86_XCPT_BP)
|
---|
344 | | RT_BIT(X86_XCPT_DB)
|
---|
345 | | RT_BIT(X86_XCPT_DE)
|
---|
346 | | RT_BIT(X86_XCPT_NM)
|
---|
347 | | RT_BIT(X86_XCPT_UD)
|
---|
348 | | RT_BIT(X86_XCPT_NP)
|
---|
349 | | RT_BIT(X86_XCPT_SS)
|
---|
350 | | RT_BIT(X86_XCPT_GP)
|
---|
351 | | RT_BIT(X86_XCPT_PF)
|
---|
352 | | RT_BIT(X86_XCPT_MF);
|
---|
353 | #endif
|
---|
354 |
|
---|
355 | /* Set up unconditional intercepts and conditions. */
|
---|
356 | pVmcb->ctrl.u32InterceptCtrl1 = SVM_CTRL1_INTERCEPT_INTR /* External interrupt causes a VM-exit. */
|
---|
357 | | SVM_CTRL1_INTERCEPT_VINTR /* When guest enabled interrupts cause a VM-exit. */
|
---|
358 | | SVM_CTRL1_INTERCEPT_NMI /* Non-Maskable Interrupts causes a VM-exit. */
|
---|
359 | | SVM_CTRL1_INTERCEPT_SMI /* System Management Interrupt cause a VM-exit. */
|
---|
360 | | SVM_CTRL1_INTERCEPT_INIT /* INIT signal causes a VM-exit. */
|
---|
361 | | SVM_CTRL1_INTERCEPT_RDPMC /* RDPMC causes a VM-exit. */
|
---|
362 | | SVM_CTRL1_INTERCEPT_CPUID /* CPUID causes a VM-exit. */
|
---|
363 | | SVM_CTRL1_INTERCEPT_RSM /* RSM causes a VM-exit. */
|
---|
364 | | SVM_CTRL1_INTERCEPT_HLT /* HLT causes a VM-exit. */
|
---|
365 | | SVM_CTRL1_INTERCEPT_INOUT_BITMAP /* Use the IOPM to cause IOIO VM-exits. */
|
---|
366 | | SVM_CTRL1_INTERCEPT_MSR_SHADOW /* MSR access not covered by MSRPM causes a VM-exit.*/
|
---|
367 | | SVM_CTRL1_INTERCEPT_INVLPGA /* INVLPGA causes a VM-exit. */
|
---|
368 | | SVM_CTRL1_INTERCEPT_SHUTDOWN /* Shutdown events causes a VM-exit. */
|
---|
369 | | SVM_CTRL1_INTERCEPT_FERR_FREEZE; /* Intercept "freezing" during legacy FPU handling. */
|
---|
370 |
|
---|
371 | pVmcb->ctrl.u32InterceptCtrl2 = SVM_CTRL2_INTERCEPT_VMRUN /* VMRUN causes a VM-exit. */
|
---|
372 | | SVM_CTRL2_INTERCEPT_VMMCALL /* VMMCALL causes a VM-exit. */
|
---|
373 | | SVM_CTRL2_INTERCEPT_VMLOAD /* VMLOAD causes a VM-exit. */
|
---|
374 | | SVM_CTRL2_INTERCEPT_VMSAVE /* VMSAVE causes a VM-exit. */
|
---|
375 | | SVM_CTRL2_INTERCEPT_STGI /* STGI causes a VM-exit. */
|
---|
376 | | SVM_CTRL2_INTERCEPT_CLGI /* CLGI causes a VM-exit. */
|
---|
377 | | SVM_CTRL2_INTERCEPT_SKINIT /* SKINIT causes a VM-exit. */
|
---|
378 | | SVM_CTRL2_INTERCEPT_WBINVD /* WBINVD causes a VM-exit. */
|
---|
379 | | SVM_CTRL2_INTERCEPT_MONITOR /* MONITOR causes a VM-exit. */
|
---|
380 | | SVM_CTRL2_INTERCEPT_MWAIT_UNCOND; /* MWAIT causes a VM-exit. */
|
---|
381 |
|
---|
382 | /* CR0, CR4 reads must be intercepted, our shadow values are not necessarily the same as the guest's. */
|
---|
383 | pVmcb->ctrl.u16InterceptRdCRx = RT_BIT(0) | RT_BIT(4);
|
---|
384 |
|
---|
385 | /* CR0, CR4 writes must be intercepted for obvious reasons. */
|
---|
386 | pVmcb->ctrl.u16InterceptWrCRx = RT_BIT(0) | RT_BIT(4);
|
---|
387 |
|
---|
388 | /* Intercept all DRx reads and writes by default. Changed later on. */
|
---|
389 | pVmcb->ctrl.u16InterceptRdDRx = 0xffff;
|
---|
390 | pVmcb->ctrl.u16InterceptWrDRx = 0xffff;
|
---|
391 |
|
---|
392 | /* Virtualize masking of INTR interrupts. (reads/writes from/to CR8 go to the V_TPR register) */
|
---|
393 | pVmcb->ctrl.IntCtrl.n.u1VIrqMasking = 1;
|
---|
394 |
|
---|
395 | /* Ignore the priority in the TPR; just deliver it to the guest when we tell it to. */
|
---|
396 | pVmcb->ctrl.IntCtrl.n.u1IgnoreTPR = 1;
|
---|
397 |
|
---|
398 | /* Set IO and MSR bitmap permission bitmap physical addresses. */
|
---|
399 | pVmcb->ctrl.u64IOPMPhysAddr = g_HCPhysIOBitmap;
|
---|
400 | pVmcb->ctrl.u64MSRPMPhysAddr = pVCpu->hm.s.svm.HCPhysMsrBitmap;
|
---|
401 |
|
---|
402 | /* No LBR virtualization. */
|
---|
403 | pVmcb->ctrl.u64LBRVirt = 0;
|
---|
404 |
|
---|
405 | /* The ASID must start at 1; the host uses 0. */
|
---|
406 | pVmcb->ctrl.TLBCtrl.n.u32ASID = 1;
|
---|
407 |
|
---|
408 | /*
|
---|
409 | * Setup the PAT MSR (applicable for Nested Paging only).
|
---|
410 | * The default value should be 0x0007040600070406ULL, but we want to treat all guest memory as WB,
|
---|
411 | * so choose type 6 for all PAT slots.
|
---|
412 | */
|
---|
413 | pVmcb->guest.u64GPAT = UINT64_C(0x0006060606060606);
|
---|
414 |
|
---|
415 | /* Without Nested Paging, we need additionally intercepts. */
|
---|
416 | if (!pVM->hm.s.fNestedPaging)
|
---|
417 | {
|
---|
418 | /* CR3 reads/writes must be intercepted; our shadow values differ from the guest values. */
|
---|
419 | pVmcb->ctrl.u16InterceptRdCRx |= RT_BIT(3);
|
---|
420 | pVmcb->ctrl.u16InterceptWrCRx |= RT_BIT(3);
|
---|
421 |
|
---|
422 | /* Intercept INVLPG and task switches (may change CR3, EFLAGS, LDT). */
|
---|
423 | pVmcb->ctrl.u32InterceptCtrl1 |= SVM_CTRL1_INTERCEPT_INVLPG
|
---|
424 | | SVM_CTRL1_INTERCEPT_TASK_SWITCH;
|
---|
425 |
|
---|
426 | /* Page faults must be intercepted to implement shadow paging. */
|
---|
427 | pVmcb->ctrl.u32InterceptException |= RT_BIT(X86_XCPT_PF);
|
---|
428 | }
|
---|
429 |
|
---|
430 | /*
|
---|
431 | * The following MSRs are saved/restored automatically during the world-switch.
|
---|
432 | * Don't intercept guest read/write accesses to these MSRs.
|
---|
433 | */
|
---|
434 | hmR0SvmSetMSRPermission(pVCpu, MSR_K8_LSTAR, SVMMSREXIT_PASSTHRU_READ, SVMMSREXIT_PASSTHRU_WRITE);
|
---|
435 | hmR0SvmSetMSRPermission(pVCpu, MSR_K8_CSTAR, SVMMSREXIT_PASSTHRU_READ, SVMMSREXIT_PASSTHRU_WRITE);
|
---|
436 | hmR0SvmSetMSRPermission(pVCpu, MSR_K6_STAR, SVMMSREXIT_PASSTHRU_READ, SVMMSREXIT_PASSTHRU_WRITE);
|
---|
437 | hmR0SvmSetMSRPermission(pVCpu, MSR_K8_SF_MASK, SVMMSREXIT_PASSTHRU_READ, SVMMSREXIT_PASSTHRU_WRITE);
|
---|
438 | hmR0SvmSetMSRPermission(pVCpu, MSR_K8_FS_BASE, SVMMSREXIT_PASSTHRU_READ, SVMMSREXIT_PASSTHRU_WRITE);
|
---|
439 | hmR0SvmSetMSRPermission(pVCpu, MSR_K8_GS_BASE, SVMMSREXIT_PASSTHRU_READ, SVMMSREXIT_PASSTHRU_WRITE);
|
---|
440 | hmR0SvmSetMSRPermission(pVCpu, MSR_K8_KERNEL_GS_BASE, SVMMSREXIT_PASSTHRU_READ, SVMMSREXIT_PASSTHRU_WRITE);
|
---|
441 | hmR0SvmSetMSRPermission(pVCpu, MSR_IA32_SYSENTER_CS, SVMMSREXIT_PASSTHRU_READ, SVMMSREXIT_PASSTHRU_WRITE);
|
---|
442 | hmR0SvmSetMSRPermission(pVCpu, MSR_IA32_SYSENTER_ESP, SVMMSREXIT_PASSTHRU_READ, SVMMSREXIT_PASSTHRU_WRITE);
|
---|
443 | hmR0SvmSetMSRPermission(pVCpu, MSR_IA32_SYSENTER_EIP, SVMMSREXIT_PASSTHRU_READ, SVMMSREXIT_PASSTHRU_WRITE);
|
---|
444 | }
|
---|
445 |
|
---|
446 | return rc;
|
---|
447 | }
|
---|
448 |
|
---|
449 |
|
---|
450 | /**
|
---|
451 | * Sets the permission bits for the specified MSR.
|
---|
452 | *
|
---|
453 | * @param pVCpu Pointer to the VMCPU.
|
---|
454 | * @param uMsr The MSR.
|
---|
455 | * @param fRead Whether reading is allowed.
|
---|
456 | * @param fWrite Whether writing is allowed.
|
---|
457 | */
|
---|
458 | static void hmR0SvmSetMSRPermission(PVMCPU pVCpu, uint32_t uMsr, SVMMSREXITREAD enmRead, SVMMSREXITWRITE enmWrite)
|
---|
459 | {
|
---|
460 | unsigned ulBit;
|
---|
461 | uint8_t *pbMsrBitmap = (uint8_t *)pVCpu->hm.s.svm.pvMsrBitmap;
|
---|
462 |
|
---|
463 | /*
|
---|
464 | * Layout:
|
---|
465 | * Byte offset MSR range
|
---|
466 | * 0x000 - 0x7ff 0x00000000 - 0x00001fff
|
---|
467 | * 0x800 - 0xfff 0xc0000000 - 0xc0001fff
|
---|
468 | * 0x1000 - 0x17ff 0xc0010000 - 0xc0011fff
|
---|
469 | * 0x1800 - 0x1fff Reserved
|
---|
470 | */
|
---|
471 | if (uMsr <= 0x00001FFF)
|
---|
472 | {
|
---|
473 | /* Pentium-compatible MSRs */
|
---|
474 | ulBit = uMsr * 2;
|
---|
475 | }
|
---|
476 | else if ( uMsr >= 0xC0000000
|
---|
477 | && uMsr <= 0xC0001FFF)
|
---|
478 | {
|
---|
479 | /* AMD Sixth Generation x86 Processor MSRs and SYSCALL */
|
---|
480 | ulBit = (uMsr - 0xC0000000) * 2;
|
---|
481 | pbMsrBitmap += 0x800;
|
---|
482 | }
|
---|
483 | else if ( uMsr >= 0xC0010000
|
---|
484 | && uMsr <= 0xC0011FFF)
|
---|
485 | {
|
---|
486 | /* AMD Seventh and Eighth Generation Processor MSRs */
|
---|
487 | ulBit = (uMsr - 0xC0001000) * 2;
|
---|
488 | pbMsrBitmap += 0x1000;
|
---|
489 | }
|
---|
490 | else
|
---|
491 | {
|
---|
492 | AssertFailed();
|
---|
493 | return;
|
---|
494 | }
|
---|
495 |
|
---|
496 | Assert(ulBit < 0x3fff /* 16 * 1024 - 1 */);
|
---|
497 | if (enmRead == SVMMSREXIT_INTERCEPT_READ)
|
---|
498 | ASMBitSet(pbMsrBitmap, ulBit);
|
---|
499 | else
|
---|
500 | ASMBitClear(pbMsrBitmap, ulBit);
|
---|
501 |
|
---|
502 | if (enmWrite == SVMMSREXIT_INTERCEPT_WRITE)
|
---|
503 | ASMBitSet(pbMsrBitmap, ulBit + 1);
|
---|
504 | else
|
---|
505 | ASMBitClear(pbMsrBitmap, ulBit + 1);
|
---|
506 | }
|
---|
507 |
|
---|