VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/DBGFAddr.cpp@ 58611

Last change on this file since 58611 was 58126, checked in by vboxsync, 9 years ago

VMM: Fixed almost all the Doxygen warnings.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 16.6 KB
Line 
1/* $Id: DBGFAddr.cpp 58126 2015-10-08 20:59:48Z vboxsync $ */
2/** @file
3 * DBGF - Debugger Facility, Mixed Address Methods.
4 */
5
6/*
7 * Copyright (C) 2006-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
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_DBGF
23#include <VBox/vmm/dbgf.h>
24#include <VBox/vmm/pgm.h>
25#include <VBox/vmm/selm.h>
26#include <VBox/vmm/mm.h>
27#include <VBox/vmm/hm.h>
28#include "DBGFInternal.h"
29#include <VBox/vmm/vm.h>
30#include <VBox/vmm/uvm.h>
31
32#include <VBox/param.h>
33#include <VBox/err.h>
34#include <VBox/log.h>
35#include "internal/pgm.h"
36
37
38
39/**
40 * Checks if an address is in the HMA or not.
41 *
42 * @retval true if it's inside the HMA.
43 * @retval flase if it's not inside the HMA.
44 *
45 * @param pUVM The user mode VM handle.
46 * @param FlatPtr The address in question.
47 */
48DECLINLINE(bool) dbgfR3IsHMA(PUVM pUVM, RTGCUINTPTR FlatPtr)
49{
50 return !HMIsEnabled(pUVM->pVM)
51 && MMHyperIsInsideArea(pUVM->pVM, FlatPtr);
52}
53
54
55/**
56 * Common worker for DBGFR3AddrFromSelOff and DBGFR3AddrFromSelInfoOff.
57 */
58static int dbgfR3AddrFromSelInfoOffWorker(PDBGFADDRESS pAddress, PCDBGFSELINFO pSelInfo, RTUINTPTR off)
59{
60 if (pSelInfo->fFlags & (DBGFSELINFO_FLAGS_INVALID | DBGFSELINFO_FLAGS_NOT_PRESENT))
61 return pSelInfo->fFlags & DBGFSELINFO_FLAGS_NOT_PRESENT
62 ? VERR_SELECTOR_NOT_PRESENT
63 : VERR_INVALID_SELECTOR;
64
65 /** @todo This all goes voodoo in long mode. */
66 /* check limit. */
67 if (DBGFSelInfoIsExpandDown(pSelInfo))
68 {
69 if ( !pSelInfo->u.Raw.Gen.u1Granularity
70 && off > UINT32_C(0xffff))
71 return VERR_OUT_OF_SELECTOR_BOUNDS;
72 if (off <= pSelInfo->cbLimit)
73 return VERR_OUT_OF_SELECTOR_BOUNDS;
74 }
75 else if (off > pSelInfo->cbLimit)
76 return VERR_OUT_OF_SELECTOR_BOUNDS;
77
78 pAddress->FlatPtr = pSelInfo->GCPtrBase + off;
79
80 /** @todo fix all these selector tests! */
81 if ( !pSelInfo->GCPtrBase
82 && pSelInfo->u.Raw.Gen.u1Granularity
83 && pSelInfo->u.Raw.Gen.u1DefBig)
84 pAddress->fFlags = DBGFADDRESS_FLAGS_FLAT;
85 else if (pSelInfo->cbLimit <= UINT32_C(0xffff))
86 pAddress->fFlags = DBGFADDRESS_FLAGS_FAR16;
87 else if (pSelInfo->cbLimit <= UINT32_C(0xffffffff))
88 pAddress->fFlags = DBGFADDRESS_FLAGS_FAR32;
89 else
90 pAddress->fFlags = DBGFADDRESS_FLAGS_FAR64;
91
92 return VINF_SUCCESS;
93}
94
95
96/**
97 * Creates a mixed address from a Sel:off pair.
98 *
99 * @returns VBox status code.
100 * @param pUVM The user mode VM handle.
101 * @param idCpu The CPU ID.
102 * @param pAddress Where to store the mixed address.
103 * @param Sel The selector part.
104 * @param off The offset part.
105 */
106VMMR3DECL(int) DBGFR3AddrFromSelOff(PUVM pUVM, VMCPUID idCpu, PDBGFADDRESS pAddress, RTSEL Sel, RTUINTPTR off)
107{
108 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
109 VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, VERR_INVALID_VM_HANDLE);
110 AssertReturn(idCpu < pUVM->cCpus, VERR_INVALID_PARAMETER);
111
112 pAddress->Sel = Sel;
113 pAddress->off = off;
114 if (Sel != DBGF_SEL_FLAT)
115 {
116 DBGFSELINFO SelInfo;
117 int rc = DBGFR3SelQueryInfo(pUVM, idCpu, Sel, DBGFSELQI_FLAGS_DT_GUEST | DBGFSELQI_FLAGS_DT_ADJ_64BIT_MODE, &SelInfo);
118 if (RT_FAILURE(rc) && !HMIsEnabled(pUVM->pVM))
119 rc = DBGFR3SelQueryInfo(pUVM, idCpu, Sel, DBGFSELQI_FLAGS_DT_SHADOW, &SelInfo);
120 if (RT_FAILURE(rc))
121 return rc;
122 rc = dbgfR3AddrFromSelInfoOffWorker(pAddress, &SelInfo, off);
123 if (RT_FAILURE(rc))
124 return rc;
125 if ( (SelInfo.fFlags & DBGFSELINFO_FLAGS_HYPER)
126 || dbgfR3IsHMA(pUVM, pAddress->FlatPtr))
127 pAddress->fFlags |= DBGFADDRESS_FLAGS_HMA;
128 }
129 else
130 {
131 pAddress->FlatPtr = off;
132 pAddress->fFlags = DBGFADDRESS_FLAGS_FLAT;
133 if (dbgfR3IsHMA(pUVM, pAddress->FlatPtr))
134 pAddress->fFlags |= DBGFADDRESS_FLAGS_HMA;
135 }
136 pAddress->fFlags |= DBGFADDRESS_FLAGS_VALID;
137
138 return VINF_SUCCESS;
139}
140
141
142/**
143 * Creates a mixed address from selector info and an offset into the segment
144 * described by it.
145 *
146 * @returns VBox status code.
147 * @param pUVM The user mode VM handle.
148 * @param pAddress Where to store the mixed address.
149 * @param pSelInfo The selector info.
150 * @param off The offset part.
151 */
152VMMR3DECL(int) DBGFR3AddrFromSelInfoOff(PUVM pUVM, PDBGFADDRESS pAddress, PCDBGFSELINFO pSelInfo, RTUINTPTR off)
153{
154 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
155 VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, VERR_INVALID_VM_HANDLE);
156
157 pAddress->Sel = pSelInfo->Sel;
158 pAddress->off = off;
159 int rc = dbgfR3AddrFromSelInfoOffWorker(pAddress, pSelInfo, off);
160 if (RT_FAILURE(rc))
161 return rc;
162
163 pAddress->fFlags |= DBGFADDRESS_FLAGS_VALID;
164 if (dbgfR3IsHMA(pUVM, pAddress->FlatPtr))
165 pAddress->fFlags |= DBGFADDRESS_FLAGS_HMA;
166
167 return VINF_SUCCESS;
168}
169
170
171/**
172 * Creates a mixed address from a flat address.
173 *
174 * @returns pAddress.
175 * @param pUVM The user mode VM handle.
176 * @param pAddress Where to store the mixed address.
177 * @param FlatPtr The flat pointer.
178 */
179VMMR3DECL(PDBGFADDRESS) DBGFR3AddrFromFlat(PUVM pUVM, PDBGFADDRESS pAddress, RTGCUINTPTR FlatPtr)
180{
181 UVM_ASSERT_VALID_EXT_RETURN(pUVM, NULL);
182 VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, NULL);
183 pAddress->Sel = DBGF_SEL_FLAT;
184 pAddress->off = FlatPtr;
185 pAddress->FlatPtr = FlatPtr;
186 pAddress->fFlags = DBGFADDRESS_FLAGS_FLAT | DBGFADDRESS_FLAGS_VALID;
187 if (dbgfR3IsHMA(pUVM, pAddress->FlatPtr))
188 pAddress->fFlags |= DBGFADDRESS_FLAGS_HMA;
189 return pAddress;
190}
191
192
193/**
194 * Creates a mixed address from a guest physical address.
195 *
196 * @returns pAddress.
197 * @param pUVM The user mode VM handle.
198 * @param pAddress Where to store the mixed address.
199 * @param PhysAddr The guest physical address.
200 */
201VMMR3DECL(PDBGFADDRESS) DBGFR3AddrFromPhys(PUVM pUVM, PDBGFADDRESS pAddress, RTGCPHYS PhysAddr)
202{
203 UVM_ASSERT_VALID_EXT_RETURN(pUVM, NULL);
204 pAddress->Sel = DBGF_SEL_FLAT;
205 pAddress->off = PhysAddr;
206 pAddress->FlatPtr = PhysAddr;
207 pAddress->fFlags = DBGFADDRESS_FLAGS_PHYS | DBGFADDRESS_FLAGS_VALID;
208 return pAddress;
209}
210
211
212/**
213 * Checks if the specified address is valid (checks the structure pointer too).
214 *
215 * @returns true if valid.
216 * @returns false if invalid.
217 * @param pUVM The user mode VM handle.
218 * @param pAddress The address to validate.
219 */
220VMMR3DECL(bool) DBGFR3AddrIsValid(PUVM pUVM, PCDBGFADDRESS pAddress)
221{
222 UVM_ASSERT_VALID_EXT_RETURN(pUVM, false);
223 if (!VALID_PTR(pAddress))
224 return false;
225 if (!DBGFADDRESS_IS_VALID(pAddress))
226 return false;
227 /* more? */
228 return true;
229}
230
231
232/**
233 * Called on the EMT for the VCpu.
234 *
235 * @returns VBox status code.
236 * @param pVCpu The cross context virtual CPU structure.
237 * @param pAddress The address.
238 * @param pGCPhys Where to return the physical address.
239 */
240static DECLCALLBACK(int) dbgfR3AddrToPhysOnVCpu(PVMCPU pVCpu, PDBGFADDRESS pAddress, PRTGCPHYS pGCPhys)
241{
242 VMCPU_ASSERT_EMT(pVCpu);
243 /* This is just a wrapper because we cannot pass FlatPtr thru VMR3ReqCall directly. */
244 return PGMGstGetPage(pVCpu, pAddress->FlatPtr, NULL, pGCPhys);
245}
246
247
248/**
249 * Converts an address to a guest physical address.
250 *
251 * @returns VBox status code.
252 * @retval VINF_SUCCESS
253 * @retval VERR_INVALID_PARAMETER if the address is invalid.
254 * @retval VERR_INVALID_STATE if the VM is being terminated or if the virtual
255 * CPU handle is invalid.
256 * @retval VERR_NOT_SUPPORTED is the type of address cannot be converted.
257 * @retval VERR_PAGE_NOT_PRESENT
258 * @retval VERR_PAGE_TABLE_NOT_PRESENT
259 * @retval VERR_PAGE_DIRECTORY_PTR_NOT_PRESENT
260 * @retval VERR_PAGE_MAP_LEVEL4_NOT_PRESENT
261 *
262 * @param pUVM The user mode VM handle.
263 * @param idCpu The ID of the CPU context to convert virtual
264 * addresses.
265 * @param pAddress The address.
266 * @param pGCPhys Where to return the physical address.
267 */
268VMMR3DECL(int) DBGFR3AddrToPhys(PUVM pUVM, VMCPUID idCpu, PDBGFADDRESS pAddress, PRTGCPHYS pGCPhys)
269{
270 /*
271 * Parameter validation.
272 */
273 AssertPtr(pGCPhys);
274 *pGCPhys = NIL_RTGCPHYS;
275 AssertPtr(pAddress);
276 AssertReturn(DBGFADDRESS_IS_VALID(pAddress), VERR_INVALID_PARAMETER);
277 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_STATE);
278 PVM pVM = pUVM->pVM;
279 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
280 AssertReturn(idCpu < pUVM->cCpus, VERR_INVALID_PARAMETER);
281
282 /*
283 * Convert by address type.
284 */
285 int rc;
286 if (pAddress->fFlags & DBGFADDRESS_FLAGS_HMA)
287 rc = VERR_NOT_SUPPORTED;
288 else if (pAddress->fFlags & DBGFADDRESS_FLAGS_PHYS)
289 {
290 *pGCPhys = pAddress->FlatPtr;
291 rc = VINF_SUCCESS;
292 }
293 else
294 {
295 PVMCPU pVCpu = VMMGetCpuById(pVM, idCpu);
296 if (VMCPU_IS_EMT(pVCpu))
297 rc = dbgfR3AddrToPhysOnVCpu(pVCpu, pAddress, pGCPhys);
298 else
299 rc = VMR3ReqPriorityCallWaitU(pUVM, pVCpu->idCpu,
300 (PFNRT)dbgfR3AddrToPhysOnVCpu, 3, pVCpu, pAddress, pGCPhys);
301 }
302 return rc;
303}
304
305
306/**
307 * Converts an address to a host physical address.
308 *
309 * @returns VBox status code.
310 * @retval VINF_SUCCESS
311 * @retval VERR_INVALID_PARAMETER if the address is invalid.
312 * @retval VERR_INVALID_STATE if the VM is being terminated or if the virtual
313 * CPU handle is invalid.
314 * @retval VERR_NOT_SUPPORTED is the type of address cannot be converted.
315 * @retval VERR_PAGE_NOT_PRESENT
316 * @retval VERR_PAGE_TABLE_NOT_PRESENT
317 * @retval VERR_PAGE_DIRECTORY_PTR_NOT_PRESENT
318 * @retval VERR_PAGE_MAP_LEVEL4_NOT_PRESENT
319 * @retval VERR_PGM_PHYS_PAGE_RESERVED
320 * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS
321 *
322 * @param pUVM The user mode VM handle.
323 * @param idCpu The ID of the CPU context to convert virtual
324 * addresses.
325 * @param pAddress The address.
326 * @param pHCPhys Where to return the physical address.
327 */
328VMMR3DECL(int) DBGFR3AddrToHostPhys(PUVM pUVM, VMCPUID idCpu, PDBGFADDRESS pAddress, PRTHCPHYS pHCPhys)
329{
330 /*
331 * Parameter validation.
332 */
333 AssertPtr(pHCPhys);
334 *pHCPhys = NIL_RTHCPHYS;
335 AssertPtr(pAddress);
336 AssertReturn(DBGFADDRESS_IS_VALID(pAddress), VERR_INVALID_PARAMETER);
337 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_STATE);
338 PVM pVM = pUVM->pVM;
339 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
340 AssertReturn(idCpu < pUVM->cCpus, VERR_INVALID_PARAMETER);
341
342 /*
343 * Convert it if we can.
344 */
345 int rc;
346 if (pAddress->fFlags & DBGFADDRESS_FLAGS_HMA)
347 rc = VERR_NOT_SUPPORTED; /** @todo implement this */
348 else
349 {
350 RTGCPHYS GCPhys;
351 rc = DBGFR3AddrToPhys(pUVM, idCpu, pAddress, &GCPhys);
352 if (RT_SUCCESS(rc))
353 rc = PGMPhysGCPhys2HCPhys(pVM, pAddress->FlatPtr, pHCPhys);
354 }
355 return rc;
356}
357
358
359/**
360 * Called on the EMT for the VCpu.
361 *
362 * @returns VBox status code.
363 *
364 * @param pUVM The user mode VM handle.
365 * @param idCpu The ID of the CPU context.
366 * @param pAddress The address.
367 * @param fReadOnly Whether returning a read-only page is fine or not.
368 * @param ppvR3Ptr Where to return the address.
369 */
370static DECLCALLBACK(int) dbgfR3AddrToVolatileR3PtrOnVCpu(PUVM pUVM, VMCPUID idCpu, PDBGFADDRESS pAddress, bool fReadOnly,
371 void **ppvR3Ptr)
372{
373 PVM pVM = pUVM->pVM;
374 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
375 Assert(idCpu == VMMGetCpuId(pVM));
376
377 int rc;
378 if (pAddress->fFlags & DBGFADDRESS_FLAGS_HMA)
379 {
380 rc = VERR_NOT_SUPPORTED; /** @todo create some dedicated errors for this stuff. */
381 /** @todo this may assert, create a debug version of this which doesn't. */
382 if ( !HMIsEnabled(pVM)
383 && MMHyperIsInsideArea(pVM, pAddress->FlatPtr))
384 {
385 void *pv = MMHyperRCToCC(pVM, (RTRCPTR)pAddress->FlatPtr);
386 if (pv)
387 {
388 *ppvR3Ptr = pv;
389 rc = VINF_SUCCESS;
390 }
391 }
392 }
393 else
394 {
395 /*
396 * This is a tad ugly, but it gets the job done.
397 */
398 PGMPAGEMAPLOCK Lock;
399 if (pAddress->fFlags & DBGFADDRESS_FLAGS_PHYS)
400 {
401 if (fReadOnly)
402 rc = PGMPhysGCPhys2CCPtrReadOnly(pVM, pAddress->FlatPtr, (void const **)ppvR3Ptr, &Lock);
403 else
404 rc = PGMPhysGCPhys2CCPtr(pVM, pAddress->FlatPtr, ppvR3Ptr, &Lock);
405 }
406 else
407 {
408 PVMCPU pVCpu = VMMGetCpuById(pVM, idCpu);
409 if (fReadOnly)
410 rc = PGMPhysGCPtr2CCPtrReadOnly(pVCpu, pAddress->FlatPtr, (void const **)ppvR3Ptr, &Lock);
411 else
412 rc = PGMPhysGCPtr2CCPtr(pVCpu, pAddress->FlatPtr, ppvR3Ptr, &Lock);
413 }
414 if (RT_SUCCESS(rc))
415 PGMPhysReleasePageMappingLock(pVM, &Lock);
416 }
417 return rc;
418}
419
420
421
422
423/**
424 * Converts an address to a volatile host virtual address.
425 *
426 * @returns VBox status code.
427 * @retval VINF_SUCCESS
428 * @retval VERR_INVALID_PARAMETER if the address is invalid.
429 * @retval VERR_INVALID_STATE if the VM is being terminated or if the virtual
430 * CPU handle is invalid.
431 * @retval VERR_NOT_SUPPORTED is the type of address cannot be converted.
432 * @retval VERR_PAGE_NOT_PRESENT
433 * @retval VERR_PAGE_TABLE_NOT_PRESENT
434 * @retval VERR_PAGE_DIRECTORY_PTR_NOT_PRESENT
435 * @retval VERR_PAGE_MAP_LEVEL4_NOT_PRESENT
436 * @retval VERR_PGM_PHYS_PAGE_RESERVED
437 * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS
438 *
439 * @param pUVM The user mode VM handle.
440 * @param idCpu The ID of the CPU context to convert virtual
441 * addresses.
442 * @param pAddress The address.
443 * @param fReadOnly Whether returning a read-only page is fine or not.
444 * If set to thru the page may have to be made writable
445 * before we return.
446 * @param ppvR3Ptr Where to return the address.
447 */
448VMMR3DECL(int) DBGFR3AddrToVolatileR3Ptr(PUVM pUVM, VMCPUID idCpu, PDBGFADDRESS pAddress, bool fReadOnly, void **ppvR3Ptr)
449{
450 /*
451 * Parameter validation.
452 */
453 AssertPtr(ppvR3Ptr);
454 *ppvR3Ptr = NULL;
455 AssertPtr(pAddress);
456 AssertReturn(DBGFADDRESS_IS_VALID(pAddress), VERR_INVALID_PARAMETER);
457 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_STATE);
458 AssertReturn(idCpu < pUVM->cCpus, VERR_INVALID_PARAMETER);
459
460 /*
461 * Convert it.
462 */
463 return VMR3ReqPriorityCallWaitU(pUVM, idCpu, (PFNRT)dbgfR3AddrToVolatileR3PtrOnVCpu, 5,
464 pUVM, idCpu, pAddress, fReadOnly, ppvR3Ptr);
465}
466
467
468/**
469 * Adds an offset to an address.
470 *
471 * @returns pAddress.
472 *
473 * @param pAddress The address.
474 * @param uAddend How much to add.
475 *
476 * @remarks No address space or segment limit checks are performed,
477 */
478VMMR3DECL(PDBGFADDRESS) DBGFR3AddrAdd(PDBGFADDRESS pAddress, RTGCUINTPTR uAddend)
479{
480 /*
481 * Parameter validation.
482 */
483 AssertPtrReturn(pAddress, NULL);
484 AssertReturn(DBGFADDRESS_IS_VALID(pAddress), NULL);
485
486 /*
487 * Add the stuff.
488 */
489 pAddress->off += uAddend;
490 pAddress->FlatPtr += uAddend;
491
492 return pAddress;
493}
494
495
496/**
497 * Subtracts an offset from an address.
498 *
499 * @returns VINF_SUCCESS on success.
500 *
501 * @param pAddress The address.
502 * @param uSubtrahend How much to subtract.
503 *
504 * @remarks No address space or segment limit checks are performed,
505 */
506VMMR3DECL(PDBGFADDRESS) DBGFR3AddrSub(PDBGFADDRESS pAddress, RTGCUINTPTR uSubtrahend)
507{
508 /*
509 * Parameter validation.
510 */
511 AssertPtrReturn(pAddress, NULL);
512 AssertReturn(DBGFADDRESS_IS_VALID(pAddress), NULL);
513
514 /*
515 * Add the stuff.
516 */
517 pAddress->off -= uSubtrahend;
518 pAddress->FlatPtr -= uSubtrahend;
519
520 return pAddress;
521}
522
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