1 | /* $Id: GIM.cpp 61771 2016-06-20 16:13:26Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * GIM - Guest Interface Manager.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2014-2015 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 | /** @page pg_gim GIM - The Guest Interface Manager
|
---|
19 | *
|
---|
20 | * The Guest Interface Manager abstracts an interface provider through which
|
---|
21 | * guests may interact with the hypervisor.
|
---|
22 | *
|
---|
23 | * @see grp_gim
|
---|
24 | *
|
---|
25 | *
|
---|
26 | * @section sec_gim_provider Providers
|
---|
27 | *
|
---|
28 | * A GIM provider implements a particular hypervisor interface such as Microsoft
|
---|
29 | * Hyper-V, Linux KVM and so on. It hooks into various components in the VMM to
|
---|
30 | * ease the guest in running under a recognized, virtualized environment.
|
---|
31 | *
|
---|
32 | * The GIM provider configured for the VM needs to be recognized by the guest OS
|
---|
33 | * in order to make use of features supported by the interface. Since it
|
---|
34 | * requires co-operation from the guest OS, a GIM provider may also referred to
|
---|
35 | * as a paravirtualization interface.
|
---|
36 | *
|
---|
37 | * One of the goals of having a paravirtualized interface is for enabling guests
|
---|
38 | * to be more accurate and efficient when operating in a virtualized
|
---|
39 | * environment. For instance, a guest OS which interfaces to VirtualBox through
|
---|
40 | * a GIM provider may rely on the provider for supplying the correct TSC
|
---|
41 | * frequency of the host processor. The guest can then avoid caliberating the
|
---|
42 | * TSC itself, resulting in higher accuracy and better performance.
|
---|
43 | *
|
---|
44 | * At most, only one GIM provider can be active for a running VM and cannot be
|
---|
45 | * changed during the lifetime of the VM.
|
---|
46 | */
|
---|
47 |
|
---|
48 |
|
---|
49 | /*********************************************************************************************************************************
|
---|
50 | * Header Files *
|
---|
51 | *********************************************************************************************************************************/
|
---|
52 | #define LOG_GROUP LOG_GROUP_GIM
|
---|
53 | #include <VBox/vmm/gim.h>
|
---|
54 | #include <VBox/vmm/hm.h>
|
---|
55 | #include <VBox/vmm/ssm.h>
|
---|
56 | #include <VBox/vmm/pdmdev.h>
|
---|
57 | #include "GIMInternal.h"
|
---|
58 | #include <VBox/vmm/vm.h>
|
---|
59 |
|
---|
60 | #include <VBox/log.h>
|
---|
61 |
|
---|
62 | #include <iprt/err.h>
|
---|
63 | #include <iprt/semaphore.h>
|
---|
64 | #include <iprt/string.h>
|
---|
65 |
|
---|
66 | /* Include all GIM providers. */
|
---|
67 | #include "GIMMinimalInternal.h"
|
---|
68 | #include "GIMHvInternal.h"
|
---|
69 | #include "GIMKvmInternal.h"
|
---|
70 |
|
---|
71 |
|
---|
72 | /*********************************************************************************************************************************
|
---|
73 | * Internal Functions *
|
---|
74 | *********************************************************************************************************************************/
|
---|
75 | static FNSSMINTSAVEEXEC gimR3Save;
|
---|
76 | static FNSSMINTLOADEXEC gimR3Load;
|
---|
77 | static FNPGMPHYSHANDLER gimR3Mmio2WriteHandler;
|
---|
78 |
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Initializes the GIM.
|
---|
82 | *
|
---|
83 | * @returns VBox status code.
|
---|
84 | * @param pVM The cross context VM structure.
|
---|
85 | */
|
---|
86 | VMMR3_INT_DECL(int) GIMR3Init(PVM pVM)
|
---|
87 | {
|
---|
88 | LogFlow(("GIMR3Init\n"));
|
---|
89 |
|
---|
90 | /*
|
---|
91 | * Assert alignment and sizes.
|
---|
92 | */
|
---|
93 | AssertCompile(sizeof(pVM->gim.s) <= sizeof(pVM->gim.padding));
|
---|
94 | AssertCompile(sizeof(pVM->aCpus[0].gim.s) <= sizeof(pVM->aCpus[0].gim.padding));
|
---|
95 |
|
---|
96 |
|
---|
97 | /*
|
---|
98 | * Initialize members.
|
---|
99 | */
|
---|
100 | pVM->gim.s.hSemiReadOnlyMmio2Handler = NIL_PGMPHYSHANDLERTYPE;
|
---|
101 |
|
---|
102 | /*
|
---|
103 | * Register the saved state data unit.
|
---|
104 | */
|
---|
105 | int rc = SSMR3RegisterInternal(pVM, "GIM", 0 /* uInstance */, GIM_SAVED_STATE_VERSION, sizeof(GIM),
|
---|
106 | NULL /* pfnLivePrep */, NULL /* pfnLiveExec */, NULL /* pfnLiveVote*/,
|
---|
107 | NULL /* pfnSavePrep */, gimR3Save, NULL /* pfnSaveDone */,
|
---|
108 | NULL /* pfnLoadPrep */, gimR3Load, NULL /* pfnLoadDone */);
|
---|
109 | if (RT_FAILURE(rc))
|
---|
110 | return rc;
|
---|
111 |
|
---|
112 | /*
|
---|
113 | * Read configuration.
|
---|
114 | */
|
---|
115 | PCFGMNODE pCfgNode = CFGMR3GetChild(CFGMR3GetRoot(pVM), "GIM/");
|
---|
116 |
|
---|
117 | /*
|
---|
118 | * Validate the GIM settings.
|
---|
119 | */
|
---|
120 | rc = CFGMR3ValidateConfig(pCfgNode, "/GIM/", /* pszNode */
|
---|
121 | "Provider" /* pszValidValues */
|
---|
122 | "|Version",
|
---|
123 | "HyperV", /* pszValidNodes */
|
---|
124 | "GIM", /* pszWho */
|
---|
125 | 0); /* uInstance */
|
---|
126 | if (RT_FAILURE(rc))
|
---|
127 | return rc;
|
---|
128 |
|
---|
129 | /** @cfgm{/GIM/Provider, string}
|
---|
130 | * The name of the GIM provider. The default is "none". */
|
---|
131 | char szProvider[64];
|
---|
132 | rc = CFGMR3QueryStringDef(pCfgNode, "Provider", szProvider, sizeof(szProvider), "None");
|
---|
133 | AssertLogRelRCReturn(rc, rc);
|
---|
134 |
|
---|
135 | /** @cfgm{/GIM/Version, uint32_t}
|
---|
136 | * The interface version. The default is 0, which means "provide the most
|
---|
137 | * up-to-date implementation". */
|
---|
138 | uint32_t uVersion;
|
---|
139 | rc = CFGMR3QueryU32Def(pCfgNode, "Version", &uVersion, 0 /* default */);
|
---|
140 | AssertLogRelRCReturn(rc, rc);
|
---|
141 |
|
---|
142 | /*
|
---|
143 | * Setup the GIM provider for this VM.
|
---|
144 | */
|
---|
145 | LogRel(("GIM: Using provider '%s' (Implementation version: %u)\n", szProvider, uVersion));
|
---|
146 | if (!RTStrCmp(szProvider, "None"))
|
---|
147 | pVM->gim.s.enmProviderId = GIMPROVIDERID_NONE;
|
---|
148 | else
|
---|
149 | {
|
---|
150 | pVM->gim.s.u32Version = uVersion;
|
---|
151 | /** @todo r=bird: Because u32Version is saved, it should be translated to the
|
---|
152 | * 'most up-to-date implementation' version number when 0. Otherwise,
|
---|
153 | * we'll have abiguities when loading the state of older VMs. */
|
---|
154 | if (!RTStrCmp(szProvider, "Minimal"))
|
---|
155 | {
|
---|
156 | pVM->gim.s.enmProviderId = GIMPROVIDERID_MINIMAL;
|
---|
157 | rc = gimR3MinimalInit(pVM);
|
---|
158 | }
|
---|
159 | else if (!RTStrCmp(szProvider, "HyperV"))
|
---|
160 | {
|
---|
161 | pVM->gim.s.enmProviderId = GIMPROVIDERID_HYPERV;
|
---|
162 | rc = gimR3HvInit(pVM, pCfgNode);
|
---|
163 | }
|
---|
164 | else if (!RTStrCmp(szProvider, "KVM"))
|
---|
165 | {
|
---|
166 | pVM->gim.s.enmProviderId = GIMPROVIDERID_KVM;
|
---|
167 | rc = gimR3KvmInit(pVM);
|
---|
168 | }
|
---|
169 | else
|
---|
170 | rc = VMR3SetError(pVM->pUVM, VERR_GIM_INVALID_PROVIDER, RT_SRC_POS, "Provider '%s' unknown.", szProvider);
|
---|
171 | }
|
---|
172 |
|
---|
173 | /*
|
---|
174 | * Statistics.
|
---|
175 | */
|
---|
176 | STAM_REL_REG_USED(pVM, &pVM->gim.s.StatDbgXmit, STAMTYPE_COUNTER, "/GIM/Debug/Transmit", STAMUNIT_OCCURENCES, "Debug packets sent.");
|
---|
177 | STAM_REL_REG_USED(pVM, &pVM->gim.s.StatDbgXmitBytes, STAMTYPE_COUNTER, "/GIM/Debug/TransmitBytes", STAMUNIT_OCCURENCES, "Debug bytes sent.");
|
---|
178 | STAM_REL_REG_USED(pVM, &pVM->gim.s.StatDbgRecv, STAMTYPE_COUNTER, "/GIM/Debug/Receive", STAMUNIT_OCCURENCES, "Debug packets received.");
|
---|
179 | STAM_REL_REG_USED(pVM, &pVM->gim.s.StatDbgRecvBytes, STAMTYPE_COUNTER, "/GIM/Debug/ReceiveBytes", STAMUNIT_OCCURENCES, "Debug bytes received.");
|
---|
180 |
|
---|
181 | STAM_REL_REG_USED(pVM, &pVM->gim.s.StatHypercalls, STAMTYPE_COUNTER, "/GIM/Hypercalls", STAMUNIT_OCCURENCES, "Number of hypercalls initiated.");
|
---|
182 | return rc;
|
---|
183 | }
|
---|
184 |
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * Initializes the remaining bits of the GIM provider.
|
---|
188 | *
|
---|
189 | * This is called after initializing HM and most other VMM components.
|
---|
190 | *
|
---|
191 | * @returns VBox status code.
|
---|
192 | * @param pVM The cross context VM structure.
|
---|
193 | * @thread EMT(0)
|
---|
194 | */
|
---|
195 | VMMR3_INT_DECL(int) GIMR3InitCompleted(PVM pVM)
|
---|
196 | {
|
---|
197 | switch (pVM->gim.s.enmProviderId)
|
---|
198 | {
|
---|
199 | case GIMPROVIDERID_MINIMAL:
|
---|
200 | return gimR3MinimalInitCompleted(pVM);
|
---|
201 |
|
---|
202 | case GIMPROVIDERID_HYPERV:
|
---|
203 | return gimR3HvInitCompleted(pVM);
|
---|
204 |
|
---|
205 | case GIMPROVIDERID_KVM:
|
---|
206 | return gimR3KvmInitCompleted(pVM);
|
---|
207 |
|
---|
208 | default:
|
---|
209 | break;
|
---|
210 | }
|
---|
211 |
|
---|
212 | if (!TMR3CpuTickIsFixedRateMonotonic(pVM, true /* fWithParavirtEnabled */))
|
---|
213 | LogRel(("GIM: Warning!!! Host TSC is unstable. The guest may behave unpredictably with a paravirtualized clock.\n"));
|
---|
214 |
|
---|
215 | return VINF_SUCCESS;
|
---|
216 | }
|
---|
217 |
|
---|
218 |
|
---|
219 | /**
|
---|
220 | * @callback_method_impl{FNSSMINTSAVEEXEC}
|
---|
221 | */
|
---|
222 | static DECLCALLBACK(int) gimR3Save(PVM pVM, PSSMHANDLE pSSM)
|
---|
223 | {
|
---|
224 | AssertReturn(pVM, VERR_INVALID_PARAMETER);
|
---|
225 | AssertReturn(pSSM, VERR_SSM_INVALID_STATE);
|
---|
226 |
|
---|
227 | /** @todo Save per-CPU data. */
|
---|
228 | int rc = VINF_SUCCESS;
|
---|
229 | #if 0
|
---|
230 | SSMR3PutU32(pSSM, pVM->cCpus);
|
---|
231 | for (VMCPUID i = 0; i < pVM->cCpus; i++)
|
---|
232 | {
|
---|
233 | rc = SSMR3PutXYZ(pSSM, pVM->aCpus[i].gim.s.XYZ);
|
---|
234 | }
|
---|
235 | #endif
|
---|
236 |
|
---|
237 | /*
|
---|
238 | * Save per-VM data.
|
---|
239 | */
|
---|
240 | SSMR3PutU32(pSSM, pVM->gim.s.enmProviderId);
|
---|
241 | SSMR3PutU32(pSSM, pVM->gim.s.u32Version);
|
---|
242 |
|
---|
243 | /*
|
---|
244 | * Save provider-specific data.
|
---|
245 | */
|
---|
246 | switch (pVM->gim.s.enmProviderId)
|
---|
247 | {
|
---|
248 | case GIMPROVIDERID_HYPERV:
|
---|
249 | rc = gimR3HvSave(pVM, pSSM);
|
---|
250 | AssertRCReturn(rc, rc);
|
---|
251 | break;
|
---|
252 |
|
---|
253 | case GIMPROVIDERID_KVM:
|
---|
254 | rc = gimR3KvmSave(pVM, pSSM);
|
---|
255 | AssertRCReturn(rc, rc);
|
---|
256 | break;
|
---|
257 |
|
---|
258 | default:
|
---|
259 | break;
|
---|
260 | }
|
---|
261 |
|
---|
262 | return rc;
|
---|
263 | }
|
---|
264 |
|
---|
265 |
|
---|
266 | /**
|
---|
267 | * @callback_method_impl{FNSSMINTLOADEXEC}
|
---|
268 | */
|
---|
269 | static DECLCALLBACK(int) gimR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
|
---|
270 | {
|
---|
271 | if (uPass != SSM_PASS_FINAL)
|
---|
272 | return VINF_SUCCESS;
|
---|
273 | if (uVersion != GIM_SAVED_STATE_VERSION)
|
---|
274 | return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
|
---|
275 |
|
---|
276 | /** @todo Load per-CPU data. */
|
---|
277 | int rc;
|
---|
278 | #if 0
|
---|
279 | for (VMCPUID i = 0; i < pVM->cCpus; i++)
|
---|
280 | {
|
---|
281 | rc = SSMR3PutXYZ(pSSM, pVM->aCpus[i].gim.s.XYZ);
|
---|
282 | }
|
---|
283 | #endif
|
---|
284 |
|
---|
285 | /*
|
---|
286 | * Load per-VM data.
|
---|
287 | */
|
---|
288 | uint32_t uProviderId;
|
---|
289 | uint32_t uProviderVersion;
|
---|
290 |
|
---|
291 | rc = SSMR3GetU32(pSSM, &uProviderId); AssertRCReturn(rc, rc);
|
---|
292 | rc = SSMR3GetU32(pSSM, &uProviderVersion); AssertRCReturn(rc, rc);
|
---|
293 |
|
---|
294 | if ((GIMPROVIDERID)uProviderId != pVM->gim.s.enmProviderId)
|
---|
295 | return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Saved GIM provider %u differs from the configured one (%u)."),
|
---|
296 | uProviderId, pVM->gim.s.enmProviderId);
|
---|
297 | #if 0 /** @todo r=bird: Figure out what you mean to do here with the version. */
|
---|
298 | if (uProviderVersion != pVM->gim.s.u32Version)
|
---|
299 | return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Saved GIM provider version %u differs from the configured one (%u)."),
|
---|
300 | uProviderVersion, pVM->gim.s.u32Version);
|
---|
301 | #else
|
---|
302 | pVM->gim.s.u32Version = uProviderVersion;
|
---|
303 | #endif
|
---|
304 |
|
---|
305 | /*
|
---|
306 | * Load provider-specific data.
|
---|
307 | */
|
---|
308 | switch (pVM->gim.s.enmProviderId)
|
---|
309 | {
|
---|
310 | case GIMPROVIDERID_HYPERV:
|
---|
311 | rc = gimR3HvLoad(pVM, pSSM, uVersion);
|
---|
312 | AssertRCReturn(rc, rc);
|
---|
313 | break;
|
---|
314 |
|
---|
315 | case GIMPROVIDERID_KVM:
|
---|
316 | rc = gimR3KvmLoad(pVM, pSSM, uVersion);
|
---|
317 | AssertRCReturn(rc, rc);
|
---|
318 | break;
|
---|
319 |
|
---|
320 | default:
|
---|
321 | break;
|
---|
322 | }
|
---|
323 |
|
---|
324 | return VINF_SUCCESS;
|
---|
325 | }
|
---|
326 |
|
---|
327 |
|
---|
328 | /**
|
---|
329 | * Terminates the GIM.
|
---|
330 | *
|
---|
331 | * Termination means cleaning up and freeing all resources,
|
---|
332 | * the VM itself is, at this point, powered off or suspended.
|
---|
333 | *
|
---|
334 | * @returns VBox status code.
|
---|
335 | * @param pVM The cross context VM structure.
|
---|
336 | */
|
---|
337 | VMMR3_INT_DECL(int) GIMR3Term(PVM pVM)
|
---|
338 | {
|
---|
339 | switch (pVM->gim.s.enmProviderId)
|
---|
340 | {
|
---|
341 | case GIMPROVIDERID_HYPERV:
|
---|
342 | return gimR3HvTerm(pVM);
|
---|
343 |
|
---|
344 | case GIMPROVIDERID_KVM:
|
---|
345 | return gimR3KvmTerm(pVM);
|
---|
346 |
|
---|
347 | default:
|
---|
348 | break;
|
---|
349 | }
|
---|
350 | return VINF_SUCCESS;
|
---|
351 | }
|
---|
352 |
|
---|
353 |
|
---|
354 | /**
|
---|
355 | * The VM is being reset.
|
---|
356 | *
|
---|
357 | * For the GIM component this means unmapping and unregistering MMIO2 regions
|
---|
358 | * and other provider-specific resets.
|
---|
359 | *
|
---|
360 | * @returns VBox status code.
|
---|
361 | * @param pVM The cross context VM structure.
|
---|
362 | */
|
---|
363 | VMMR3_INT_DECL(void) GIMR3Reset(PVM pVM)
|
---|
364 | {
|
---|
365 | switch (pVM->gim.s.enmProviderId)
|
---|
366 | {
|
---|
367 | case GIMPROVIDERID_HYPERV:
|
---|
368 | return gimR3HvReset(pVM);
|
---|
369 |
|
---|
370 | case GIMPROVIDERID_KVM:
|
---|
371 | return gimR3KvmReset(pVM);
|
---|
372 |
|
---|
373 | default:
|
---|
374 | break;
|
---|
375 | }
|
---|
376 | }
|
---|
377 |
|
---|
378 |
|
---|
379 | /**
|
---|
380 | * Registers the GIM device with VMM.
|
---|
381 | *
|
---|
382 | * @param pVM The cross context VM structure.
|
---|
383 | * @param pDevIns Pointer to the GIM device instance.
|
---|
384 | * @param pDbg Pointer to the GIM device debug structure, can be
|
---|
385 | * NULL.
|
---|
386 | */
|
---|
387 | VMMR3DECL(void) GIMR3GimDeviceRegister(PVM pVM, PPDMDEVINS pDevIns, PGIMDEBUG pDbg)
|
---|
388 | {
|
---|
389 | pVM->gim.s.pDevInsR3 = pDevIns;
|
---|
390 | pVM->gim.s.pDbgR3 = pDbg;
|
---|
391 | }
|
---|
392 |
|
---|
393 |
|
---|
394 | /**
|
---|
395 | * Gets debug setup specified by the provider.
|
---|
396 | *
|
---|
397 | * @returns VBox status code.
|
---|
398 | * @param pVM The cross context VM structure.
|
---|
399 | * @param pDbgSetup Where to store the debug setup details.
|
---|
400 | */
|
---|
401 | VMMR3DECL(int) GIMR3GetDebugSetup(PVM pVM, PGIMDEBUGSETUP pDbgSetup)
|
---|
402 | {
|
---|
403 | AssertReturn(pVM, VERR_INVALID_PARAMETER);
|
---|
404 | AssertReturn(pDbgSetup, VERR_INVALID_PARAMETER);
|
---|
405 |
|
---|
406 | switch (pVM->gim.s.enmProviderId)
|
---|
407 | {
|
---|
408 | case GIMPROVIDERID_HYPERV:
|
---|
409 | return gimR3HvGetDebugSetup(pVM, pDbgSetup);
|
---|
410 | default:
|
---|
411 | break;
|
---|
412 | }
|
---|
413 | return VERR_GIM_NO_DEBUG_CONNECTION;
|
---|
414 | }
|
---|
415 |
|
---|
416 |
|
---|
417 | /**
|
---|
418 | * Read data from a host debug session.
|
---|
419 | *
|
---|
420 | * @returns VBox status code.
|
---|
421 | *
|
---|
422 | * @param pVM The cross context VM structure.
|
---|
423 | * @param pvRead The read buffer.
|
---|
424 | * @param pcbRead The size of the read buffer as well as where to store
|
---|
425 | * the number of bytes read.
|
---|
426 | * @param pfnReadComplete Callback when the buffer has been read and
|
---|
427 | * before signaling reading of the next buffer.
|
---|
428 | * Optional, can be NULL.
|
---|
429 | * @thread EMT.
|
---|
430 | */
|
---|
431 | VMMR3_INT_DECL(int) gimR3DebugRead(PVM pVM, void *pvRead, size_t *pcbRead, PFNGIMDEBUGBUFREADCOMPLETED pfnReadComplete)
|
---|
432 | {
|
---|
433 | PGIMDEBUG pDbg = pVM->gim.s.pDbgR3;
|
---|
434 | if (pDbg)
|
---|
435 | {
|
---|
436 | if (ASMAtomicReadBool(&pDbg->fDbgRecvBufRead) == true)
|
---|
437 | {
|
---|
438 | STAM_REL_COUNTER_INC(&pVM->gim.s.StatDbgRecv);
|
---|
439 | STAM_REL_COUNTER_ADD(&pVM->gim.s.StatDbgRecvBytes, pDbg->cbDbgRecvBufRead);
|
---|
440 |
|
---|
441 | memcpy(pvRead, pDbg->pvDbgRecvBuf, pDbg->cbDbgRecvBufRead);
|
---|
442 | *pcbRead = pDbg->cbDbgRecvBufRead;
|
---|
443 | if (pfnReadComplete)
|
---|
444 | pfnReadComplete(pVM);
|
---|
445 | RTSemEventMultiSignal(pDbg->hDbgRecvThreadSem);
|
---|
446 | ASMAtomicWriteBool(&pDbg->fDbgRecvBufRead, false);
|
---|
447 | return VINF_SUCCESS;
|
---|
448 | }
|
---|
449 | else
|
---|
450 | *pcbRead = 0;
|
---|
451 | return VERR_NO_DATA;
|
---|
452 | }
|
---|
453 | return VERR_GIM_NO_DEBUG_CONNECTION;
|
---|
454 | }
|
---|
455 |
|
---|
456 |
|
---|
457 | /**
|
---|
458 | * Write data to a host debug session.
|
---|
459 | *
|
---|
460 | * @returns VBox status code.
|
---|
461 | *
|
---|
462 | * @param pVM The cross context VM structure.
|
---|
463 | * @param pvWrite The write buffer.
|
---|
464 | * @param pcbWrite The size of the write buffer as well as where to store
|
---|
465 | * the number of bytes written.
|
---|
466 | * @thread EMT.
|
---|
467 | */
|
---|
468 | VMMR3_INT_DECL(int) gimR3DebugWrite(PVM pVM, void *pvWrite, size_t *pcbWrite)
|
---|
469 | {
|
---|
470 | PGIMDEBUG pDbg = pVM->gim.s.pDbgR3;
|
---|
471 | if (pDbg)
|
---|
472 | {
|
---|
473 | PPDMISTREAM pDbgStream = pDbg->pDbgDrvStream;
|
---|
474 | if (pDbgStream)
|
---|
475 | {
|
---|
476 | size_t cbWrite = *pcbWrite;
|
---|
477 | int rc = pDbgStream->pfnWrite(pDbgStream, pvWrite, pcbWrite);
|
---|
478 | if ( RT_SUCCESS(rc)
|
---|
479 | && *pcbWrite == cbWrite)
|
---|
480 | {
|
---|
481 | STAM_REL_COUNTER_INC(&pVM->gim.s.StatDbgXmit);
|
---|
482 | STAM_REL_COUNTER_ADD(&pVM->gim.s.StatDbgXmitBytes, *pcbWrite);
|
---|
483 | }
|
---|
484 | return rc;
|
---|
485 | }
|
---|
486 | }
|
---|
487 | return VERR_GIM_NO_DEBUG_CONNECTION;
|
---|
488 | }
|
---|
489 |
|
---|
490 |
|
---|
491 | /**
|
---|
492 | * Returns the array of MMIO2 regions that are expected to be registered and
|
---|
493 | * later mapped into the guest-physical address space for the GIM provider
|
---|
494 | * configured for the VM.
|
---|
495 | *
|
---|
496 | * @returns Pointer to an array of GIM MMIO2 regions, may return NULL.
|
---|
497 | * @param pVM The cross context VM structure.
|
---|
498 | * @param pcRegions Where to store the number of items in the array.
|
---|
499 | *
|
---|
500 | * @remarks The caller does not own and therefore must -NOT- try to free the
|
---|
501 | * returned pointer.
|
---|
502 | */
|
---|
503 | VMMR3DECL(PGIMMMIO2REGION) GIMR3GetMmio2Regions(PVM pVM, uint32_t *pcRegions)
|
---|
504 | {
|
---|
505 | Assert(pVM);
|
---|
506 | Assert(pcRegions);
|
---|
507 |
|
---|
508 | *pcRegions = 0;
|
---|
509 | switch (pVM->gim.s.enmProviderId)
|
---|
510 | {
|
---|
511 | case GIMPROVIDERID_HYPERV:
|
---|
512 | return gimR3HvGetMmio2Regions(pVM, pcRegions);
|
---|
513 |
|
---|
514 | default:
|
---|
515 | break;
|
---|
516 | }
|
---|
517 |
|
---|
518 | return NULL;
|
---|
519 | }
|
---|
520 |
|
---|
521 |
|
---|
522 | /**
|
---|
523 | * @callback_method_impl{FNPGMPHYSHANDLER,
|
---|
524 | * Write access handler for mapped MMIO2 pages. Currently ignores writes.}
|
---|
525 | *
|
---|
526 | * @todo In the future we might want to let the GIM provider decide what the
|
---|
527 | * handler should do (like throwing \#GP faults).
|
---|
528 | */
|
---|
529 | static DECLCALLBACK(VBOXSTRICTRC) gimR3Mmio2WriteHandler(PVM pVM, PVMCPU pVCpu, RTGCPHYS GCPhys, void *pvPhys, void *pvBuf,
|
---|
530 | size_t cbBuf, PGMACCESSTYPE enmAccessType, PGMACCESSORIGIN enmOrigin,
|
---|
531 | void *pvUser)
|
---|
532 | {
|
---|
533 | /*
|
---|
534 | * Ignore writes to the mapped MMIO2 page.
|
---|
535 | */
|
---|
536 | Assert(enmAccessType == PGMACCESSTYPE_WRITE);
|
---|
537 | return VINF_SUCCESS; /** @todo Hyper-V says we should \#GP(0) fault for writes to the Hypercall and TSC page. */
|
---|
538 | }
|
---|
539 |
|
---|
540 |
|
---|
541 | #if 0
|
---|
542 | /**
|
---|
543 | * Unmaps a registered MMIO2 region in the guest address space and removes any
|
---|
544 | * access handlers for it.
|
---|
545 | *
|
---|
546 | * @returns VBox status code.
|
---|
547 | * @param pVM The cross context VM structure.
|
---|
548 | * @param pRegion Pointer to the GIM MMIO2 region.
|
---|
549 | */
|
---|
550 | VMMR3_INT_DECL(int) gimR3Mmio2Unmap(PVM pVM, PGIMMMIO2REGION pRegion)
|
---|
551 | {
|
---|
552 | AssertPtr(pVM);
|
---|
553 | AssertPtr(pRegion);
|
---|
554 |
|
---|
555 | PPDMDEVINS pDevIns = pVM->gim.s.pDevInsR3;
|
---|
556 | AssertPtr(pDevIns);
|
---|
557 | if (pRegion->fMapped)
|
---|
558 | {
|
---|
559 | int rc = PGMHandlerPhysicalDeregister(pVM, pRegion->GCPhysPage);
|
---|
560 | AssertRC(rc);
|
---|
561 |
|
---|
562 | rc = PDMDevHlpMMIO2Unmap(pDevIns, pRegion->iRegion, pRegion->GCPhysPage);
|
---|
563 | if (RT_SUCCESS(rc))
|
---|
564 | {
|
---|
565 | pRegion->fMapped = false;
|
---|
566 | pRegion->GCPhysPage = NIL_RTGCPHYS;
|
---|
567 | }
|
---|
568 | }
|
---|
569 | return VINF_SUCCESS;
|
---|
570 | }
|
---|
571 |
|
---|
572 |
|
---|
573 | /**
|
---|
574 | * Maps a registered MMIO2 region in the guest address space.
|
---|
575 | *
|
---|
576 | * The region will be made read-only and writes from the guest will be ignored.
|
---|
577 | *
|
---|
578 | * @returns VBox status code.
|
---|
579 | * @param pVM The cross context VM structure.
|
---|
580 | * @param pRegion Pointer to the GIM MMIO2 region.
|
---|
581 | * @param GCPhysRegion Where in the guest address space to map the region.
|
---|
582 | */
|
---|
583 | VMMR3_INT_DECL(int) GIMR3Mmio2Map(PVM pVM, PGIMMMIO2REGION pRegion, RTGCPHYS GCPhysRegion)
|
---|
584 | {
|
---|
585 | PPDMDEVINS pDevIns = pVM->gim.s.pDevInsR3;
|
---|
586 | AssertPtr(pDevIns);
|
---|
587 |
|
---|
588 | /* The guest-physical address must be page-aligned. */
|
---|
589 | if (GCPhysRegion & PAGE_OFFSET_MASK)
|
---|
590 | {
|
---|
591 | LogFunc(("%s: %#RGp not paging aligned\n", pRegion->szDescription, GCPhysRegion));
|
---|
592 | return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
|
---|
593 | }
|
---|
594 |
|
---|
595 | /* Allow only normal pages to be overlaid using our MMIO2 pages (disallow MMIO, ROM, reserved pages). */
|
---|
596 | /** @todo Hyper-V doesn't seem to be very strict about this, may be relax
|
---|
597 | * later if some guest really requires it. */
|
---|
598 | if (!PGMPhysIsGCPhysNormal(pVM, GCPhysRegion))
|
---|
599 | {
|
---|
600 | LogFunc(("%s: %#RGp is not normal memory\n", pRegion->szDescription, GCPhysRegion));
|
---|
601 | return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
|
---|
602 | }
|
---|
603 |
|
---|
604 | if (!pRegion->fRegistered)
|
---|
605 | {
|
---|
606 | LogFunc(("%s: Region has not been registered.\n", pRegion->szDescription));
|
---|
607 | return VERR_GIM_IPE_1;
|
---|
608 | }
|
---|
609 |
|
---|
610 | /*
|
---|
611 | * Map the MMIO2 region over the specified guest-physical address.
|
---|
612 | */
|
---|
613 | int rc = PDMDevHlpMMIO2Map(pDevIns, pRegion->iRegion, GCPhysRegion);
|
---|
614 | if (RT_SUCCESS(rc))
|
---|
615 | {
|
---|
616 | /*
|
---|
617 | * Install access-handlers for the mapped page to prevent (ignore) writes to it
|
---|
618 | * from the guest.
|
---|
619 | */
|
---|
620 | if (pVM->gim.s.hSemiReadOnlyMmio2Handler == NIL_PGMPHYSHANDLERTYPE)
|
---|
621 | rc = PGMR3HandlerPhysicalTypeRegister(pVM, PGMPHYSHANDLERKIND_WRITE,
|
---|
622 | gimR3Mmio2WriteHandler,
|
---|
623 | NULL /* pszModR0 */, NULL /* pszHandlerR0 */, NULL /* pszPfHandlerR0 */,
|
---|
624 | NULL /* pszModRC */, NULL /* pszHandlerRC */, NULL /* pszPfHandlerRC */,
|
---|
625 | "GIM read-only MMIO2 handler",
|
---|
626 | &pVM->gim.s.hSemiReadOnlyMmio2Handler);
|
---|
627 | if (RT_SUCCESS(rc))
|
---|
628 | {
|
---|
629 | rc = PGMHandlerPhysicalRegister(pVM, GCPhysRegion, GCPhysRegion + (pRegion->cbRegion - 1),
|
---|
630 | pVM->gim.s.hSemiReadOnlyMmio2Handler,
|
---|
631 | NULL /* pvUserR3 */, NIL_RTR0PTR /* pvUserR0 */, NIL_RTRCPTR /* pvUserRC */,
|
---|
632 | pRegion->szDescription);
|
---|
633 | if (RT_SUCCESS(rc))
|
---|
634 | {
|
---|
635 | pRegion->fMapped = true;
|
---|
636 | pRegion->GCPhysPage = GCPhysRegion;
|
---|
637 | return rc;
|
---|
638 | }
|
---|
639 | }
|
---|
640 |
|
---|
641 | PDMDevHlpMMIO2Unmap(pDevIns, pRegion->iRegion, GCPhysRegion);
|
---|
642 | }
|
---|
643 |
|
---|
644 | return rc;
|
---|
645 | }
|
---|
646 |
|
---|
647 |
|
---|
648 | /**
|
---|
649 | * Registers the physical handler for the registered and mapped MMIO2 region.
|
---|
650 | *
|
---|
651 | * @returns VBox status code.
|
---|
652 | * @param pVM The cross context VM structure.
|
---|
653 | * @param pRegion Pointer to the GIM MMIO2 region.
|
---|
654 | */
|
---|
655 | VMMR3_INT_DECL(int) gimR3Mmio2HandlerPhysicalRegister(PVM pVM, PGIMMMIO2REGION pRegion)
|
---|
656 | {
|
---|
657 | AssertPtr(pRegion);
|
---|
658 | AssertReturn(pRegion->fRegistered, VERR_GIM_IPE_2);
|
---|
659 | AssertReturn(pRegion->fMapped, VERR_GIM_IPE_3);
|
---|
660 |
|
---|
661 | return PGMR3HandlerPhysicalRegister(pVM,
|
---|
662 | PGMPHYSHANDLERKIND_WRITE,
|
---|
663 | pRegion->GCPhysPage, pRegion->GCPhysPage + (pRegion->cbRegion - 1),
|
---|
664 | gimR3Mmio2WriteHandler, NULL /* pvUserR3 */,
|
---|
665 | NULL /* pszModR0 */, NULL /* pszHandlerR0 */, NIL_RTR0PTR /* pvUserR0 */,
|
---|
666 | NULL /* pszModRC */, NULL /* pszHandlerRC */, NIL_RTRCPTR /* pvUserRC */,
|
---|
667 | pRegion->szDescription);
|
---|
668 | }
|
---|
669 |
|
---|
670 |
|
---|
671 | /**
|
---|
672 | * Deregisters the physical handler for the MMIO2 region.
|
---|
673 | *
|
---|
674 | * @returns VBox status code.
|
---|
675 | * @param pVM The cross context VM structure.
|
---|
676 | * @param pRegion Pointer to the GIM MMIO2 region.
|
---|
677 | */
|
---|
678 | VMMR3_INT_DECL(int) gimR3Mmio2HandlerPhysicalDeregister(PVM pVM, PGIMMMIO2REGION pRegion)
|
---|
679 | {
|
---|
680 | return PGMHandlerPhysicalDeregister(pVM, pRegion->GCPhysPage);
|
---|
681 | }
|
---|
682 | #endif
|
---|
683 |
|
---|