[23] | 1 | /* $Id: DBGFMem.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
|
---|
[1] | 2 | /** @file
|
---|
[12677] | 3 | * DBGF - Debugger Facility, Memory Methods.
|
---|
[1] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[82968] | 7 | * Copyright (C) 2007-2020 Oracle Corporation
|
---|
[1] | 8 | *
|
---|
[6000] | 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.
|
---|
[1] | 16 | */
|
---|
| 17 |
|
---|
| 18 |
|
---|
[57358] | 19 | /*********************************************************************************************************************************
|
---|
| 20 | * Header Files *
|
---|
| 21 | *********************************************************************************************************************************/
|
---|
[1] | 22 | #define LOG_GROUP LOG_GROUP_DBGF
|
---|
[35346] | 23 | #include <VBox/vmm/dbgf.h>
|
---|
| 24 | #include <VBox/vmm/pgm.h>
|
---|
| 25 | #include <VBox/vmm/selm.h>
|
---|
[43387] | 26 | #include <VBox/vmm/hm.h>
|
---|
[1] | 27 | #include "DBGFInternal.h"
|
---|
[35346] | 28 | #include <VBox/vmm/vm.h>
|
---|
[44399] | 29 | #include <VBox/vmm/uvm.h>
|
---|
[1] | 30 | #include <VBox/err.h>
|
---|
| 31 | #include <VBox/log.h>
|
---|
[35346] | 32 | #include <VBox/vmm/mm.h>
|
---|
[1] | 33 |
|
---|
| 34 |
|
---|
| 35 |
|
---|
| 36 | /**
|
---|
[5667] | 37 | * Scan guest memory for an exact byte string.
|
---|
[1] | 38 | *
|
---|
| 39 | * @returns VBox status code.
|
---|
[44399] | 40 | * @param pUVM The user mode VM handle.
|
---|
[19286] | 41 | * @param idCpu The ID of the CPU context to search in.
|
---|
[1] | 42 | * @param pAddress Where to store the mixed address.
|
---|
[25647] | 43 | * @param puAlign The alignment restriction imposed on the search result.
|
---|
[19286] | 44 | * @param pcbRange The number of bytes to scan. Passed as a pointer because
|
---|
[15997] | 45 | * it may be 64-bit.
|
---|
[5667] | 46 | * @param pabNeedle What to search for - exact search.
|
---|
| 47 | * @param cbNeedle Size of the search byte string.
|
---|
| 48 | * @param pHitAddress Where to put the address of the first hit.
|
---|
[1] | 49 | */
|
---|
[44399] | 50 | static DECLCALLBACK(int) dbgfR3MemScan(PUVM pUVM, VMCPUID idCpu, PCDBGFADDRESS pAddress, PCRTGCUINTPTR pcbRange,
|
---|
| 51 | RTGCUINTPTR *puAlign, const uint8_t *pabNeedle, size_t cbNeedle, PDBGFADDRESS pHitAddress)
|
---|
[1] | 52 | {
|
---|
[44399] | 53 | PVM pVM = pUVM->pVM;
|
---|
| 54 | VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
|
---|
[19286] | 55 | Assert(idCpu == VMMGetCpuId(pVM));
|
---|
[18927] | 56 |
|
---|
[5667] | 57 | /*
|
---|
| 58 | * Validate the input we use, PGM does the rest.
|
---|
| 59 | */
|
---|
[15997] | 60 | RTGCUINTPTR cbRange = *pcbRange;
|
---|
[44399] | 61 | if (!DBGFR3AddrIsValid(pUVM, pAddress))
|
---|
[5667] | 62 | return VERR_INVALID_POINTER;
|
---|
| 63 | if (!VALID_PTR(pHitAddress))
|
---|
| 64 | return VERR_INVALID_POINTER;
|
---|
| 65 |
|
---|
| 66 | /*
|
---|
| 67 | * Select DBGF worker by addressing mode.
|
---|
| 68 | */
|
---|
[19286] | 69 | int rc;
|
---|
[19293] | 70 | PVMCPU pVCpu = VMMGetCpuById(pVM, idCpu);
|
---|
[18927] | 71 | PGMMODE enmMode = PGMGetGuestMode(pVCpu);
|
---|
[5667] | 72 | if ( enmMode == PGMMODE_REAL
|
---|
| 73 | || enmMode == PGMMODE_PROTECTED
|
---|
| 74 | || DBGFADDRESS_IS_PHYS(pAddress)
|
---|
| 75 | )
|
---|
[1] | 76 | {
|
---|
[24061] | 77 | RTGCPHYS GCPhysAlign = *puAlign;
|
---|
| 78 | if (GCPhysAlign != *puAlign)
|
---|
| 79 | return VERR_OUT_OF_RANGE;
|
---|
[5667] | 80 | RTGCPHYS PhysHit;
|
---|
[24061] | 81 | rc = PGMR3DbgScanPhysical(pVM, pAddress->FlatPtr, cbRange, GCPhysAlign, pabNeedle, cbNeedle, &PhysHit);
|
---|
[5667] | 82 | if (RT_SUCCESS(rc))
|
---|
[44399] | 83 | DBGFR3AddrFromPhys(pUVM, pHitAddress, PhysHit);
|
---|
[1] | 84 | }
|
---|
| 85 | else
|
---|
| 86 | {
|
---|
[13609] | 87 | #if GC_ARCH_BITS > 32
|
---|
[13685] | 88 | if ( ( pAddress->FlatPtr >= _4G
|
---|
[13502] | 89 | || pAddress->FlatPtr + cbRange > _4G)
|
---|
[13685] | 90 | && enmMode != PGMMODE_AMD64
|
---|
[13502] | 91 | && enmMode != PGMMODE_AMD64_NX)
|
---|
| 92 | return VERR_DBGF_MEM_NOT_FOUND;
|
---|
[13685] | 93 | #endif
|
---|
[5667] | 94 | RTGCUINTPTR GCPtrHit;
|
---|
[24061] | 95 | rc = PGMR3DbgScanVirtual(pVM, pVCpu, pAddress->FlatPtr, cbRange, *puAlign, pabNeedle, cbNeedle, &GCPtrHit);
|
---|
[5667] | 96 | if (RT_SUCCESS(rc))
|
---|
[44399] | 97 | DBGFR3AddrFromFlat(pUVM, pHitAddress, GCPtrHit);
|
---|
[1] | 98 | }
|
---|
| 99 |
|
---|
[5667] | 100 | return rc;
|
---|
[1] | 101 | }
|
---|
| 102 |
|
---|
| 103 |
|
---|
| 104 | /**
|
---|
[5667] | 105 | * Scan guest memory for an exact byte string.
|
---|
[1] | 106 | *
|
---|
[5731] | 107 | * @returns VBox status codes:
|
---|
| 108 | * @retval VINF_SUCCESS and *pGCPtrHit on success.
|
---|
| 109 | * @retval VERR_DBGF_MEM_NOT_FOUND if not found.
|
---|
| 110 | * @retval VERR_INVALID_POINTER if any of the pointer arguments are invalid.
|
---|
| 111 | * @retval VERR_INVALID_ARGUMENT if any other arguments are invalid.
|
---|
| 112 | *
|
---|
[44399] | 113 | * @param pUVM The user mode VM handle.
|
---|
[19286] | 114 | * @param idCpu The ID of the CPU context to search in.
|
---|
[1] | 115 | * @param pAddress Where to store the mixed address.
|
---|
[5667] | 116 | * @param cbRange The number of bytes to scan.
|
---|
[24061] | 117 | * @param uAlign The alignment restriction imposed on the result.
|
---|
| 118 | * Usually set to 1.
|
---|
| 119 | * @param pvNeedle What to search for - exact search.
|
---|
[5667] | 120 | * @param cbNeedle Size of the search byte string.
|
---|
| 121 | * @param pHitAddress Where to put the address of the first hit.
|
---|
| 122 | *
|
---|
| 123 | * @thread Any thread.
|
---|
[1] | 124 | */
|
---|
[44399] | 125 | VMMR3DECL(int) DBGFR3MemScan(PUVM pUVM, VMCPUID idCpu, PCDBGFADDRESS pAddress, RTGCUINTPTR cbRange, RTGCUINTPTR uAlign,
|
---|
[24061] | 126 | const void *pvNeedle, size_t cbNeedle, PDBGFADDRESS pHitAddress)
|
---|
[1] | 127 | {
|
---|
[44399] | 128 | UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
|
---|
| 129 | AssertReturn(idCpu < pUVM->cCpus, VERR_INVALID_CPU_ID);
|
---|
| 130 | return VMR3ReqPriorityCallWaitU(pUVM, idCpu, (PFNRT)dbgfR3MemScan, 8,
|
---|
| 131 | pUVM, idCpu, pAddress, &cbRange, &uAlign, pvNeedle, cbNeedle, pHitAddress);
|
---|
[19286] | 132 |
|
---|
[1] | 133 | }
|
---|
| 134 |
|
---|
| 135 |
|
---|
[8800] | 136 | /**
|
---|
| 137 | * Read guest memory.
|
---|
| 138 | *
|
---|
| 139 | * @returns VBox status code.
|
---|
[44399] | 140 | * @param pUVM The user mode VM handle.
|
---|
[58126] | 141 | * @param idCpu The ID of the CPU context to read memory from.
|
---|
[44399] | 142 | * @param pAddress Where to start reading.
|
---|
| 143 | * @param pvBuf Where to store the data we've read.
|
---|
| 144 | * @param cbRead The number of bytes to read.
|
---|
[8800] | 145 | */
|
---|
[44399] | 146 | static DECLCALLBACK(int) dbgfR3MemRead(PUVM pUVM, VMCPUID idCpu, PCDBGFADDRESS pAddress, void *pvBuf, size_t cbRead)
|
---|
[8800] | 147 | {
|
---|
[44399] | 148 | PVM pVM = pUVM->pVM;
|
---|
| 149 | VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
|
---|
[19286] | 150 | Assert(idCpu == VMMGetCpuId(pVM));
|
---|
[18927] | 151 |
|
---|
[8800] | 152 | /*
|
---|
| 153 | * Validate the input we use, PGM does the rest.
|
---|
| 154 | */
|
---|
[44399] | 155 | if (!DBGFR3AddrIsValid(pUVM, pAddress))
|
---|
[8800] | 156 | return VERR_INVALID_POINTER;
|
---|
| 157 | if (!VALID_PTR(pvBuf))
|
---|
| 158 | return VERR_INVALID_POINTER;
|
---|
| 159 |
|
---|
| 160 | /*
|
---|
[80014] | 161 | * Select PGM worker by addressing mode.
|
---|
[8800] | 162 | */
|
---|
| 163 | int rc;
|
---|
[80014] | 164 | PVMCPU pVCpu = VMMGetCpuById(pVM, idCpu);
|
---|
| 165 | PGMMODE enmMode = PGMGetGuestMode(pVCpu);
|
---|
| 166 | if ( enmMode == PGMMODE_REAL
|
---|
| 167 | || enmMode == PGMMODE_PROTECTED
|
---|
| 168 | || DBGFADDRESS_IS_PHYS(pAddress) )
|
---|
| 169 | rc = PGMPhysSimpleReadGCPhys(pVM, pvBuf, pAddress->FlatPtr, cbRead);
|
---|
[8800] | 170 | else
|
---|
[13502] | 171 | {
|
---|
[13609] | 172 | #if GC_ARCH_BITS > 32
|
---|
[80014] | 173 | if ( ( pAddress->FlatPtr >= _4G
|
---|
| 174 | || pAddress->FlatPtr + cbRead > _4G)
|
---|
| 175 | && enmMode != PGMMODE_AMD64
|
---|
| 176 | && enmMode != PGMMODE_AMD64_NX)
|
---|
| 177 | return VERR_PAGE_TABLE_NOT_PRESENT;
|
---|
[13685] | 178 | #endif
|
---|
[80014] | 179 | rc = PGMPhysSimpleReadGCPtr(pVCpu, pvBuf, pAddress->FlatPtr, cbRead);
|
---|
[13502] | 180 | }
|
---|
[8800] | 181 | return rc;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 |
|
---|
| 185 | /**
|
---|
| 186 | * Read guest memory.
|
---|
| 187 | *
|
---|
| 188 | * @returns VBox status code.
|
---|
[19286] | 189 | *
|
---|
[44399] | 190 | * @param pUVM The user mode VM handle.
|
---|
| 191 | * @param idCpu The ID of the source CPU context (for the address).
|
---|
| 192 | * @param pAddress Where to start reading.
|
---|
| 193 | * @param pvBuf Where to store the data we've read.
|
---|
| 194 | * @param cbRead The number of bytes to read.
|
---|
[8800] | 195 | */
|
---|
[44399] | 196 | VMMR3DECL(int) DBGFR3MemRead(PUVM pUVM, VMCPUID idCpu, PCDBGFADDRESS pAddress, void *pvBuf, size_t cbRead)
|
---|
[8800] | 197 | {
|
---|
[44399] | 198 | UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
|
---|
| 199 | AssertReturn(idCpu < pUVM->cCpus, VERR_INVALID_CPU_ID);
|
---|
| 200 |
|
---|
[19575] | 201 | if ((pAddress->fFlags & DBGFADDRESS_FLAGS_TYPE_MASK) == DBGFADDRESS_FLAGS_RING0)
|
---|
| 202 | {
|
---|
[19576] | 203 | AssertCompile(sizeof(RTHCUINTPTR) <= sizeof(pAddress->FlatPtr));
|
---|
[44399] | 204 | VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, VERR_INVALID_VM_HANDLE);
|
---|
| 205 | return VMMR3ReadR0Stack(pUVM->pVM, idCpu, (RTHCUINTPTR)pAddress->FlatPtr, pvBuf, cbRead);
|
---|
[19575] | 206 | }
|
---|
[44399] | 207 | return VMR3ReqPriorityCallWaitU(pUVM, idCpu, (PFNRT)dbgfR3MemRead, 5, pUVM, idCpu, pAddress, pvBuf, cbRead);
|
---|
[8800] | 208 | }
|
---|
[8819] | 209 |
|
---|
| 210 |
|
---|
| 211 | /**
|
---|
| 212 | * Read a zero terminated string from guest memory.
|
---|
| 213 | *
|
---|
| 214 | * @returns VBox status code.
|
---|
[19286] | 215 | *
|
---|
[44399] | 216 | * @param pUVM The user mode VM handle.
|
---|
| 217 | * @param idCpu The ID of the source CPU context (for the address).
|
---|
| 218 | * @param pAddress Where to start reading.
|
---|
| 219 | * @param pszBuf Where to store the string.
|
---|
| 220 | * @param cchBuf The size of the buffer.
|
---|
[8819] | 221 | */
|
---|
[44399] | 222 | static DECLCALLBACK(int) dbgfR3MemReadString(PUVM pUVM, VMCPUID idCpu, PCDBGFADDRESS pAddress, char *pszBuf, size_t cchBuf)
|
---|
[8819] | 223 | {
|
---|
| 224 | /*
|
---|
| 225 | * Validate the input we use, PGM does the rest.
|
---|
| 226 | */
|
---|
[44399] | 227 | if (!DBGFR3AddrIsValid(pUVM, pAddress))
|
---|
[8819] | 228 | return VERR_INVALID_POINTER;
|
---|
| 229 | if (!VALID_PTR(pszBuf))
|
---|
| 230 | return VERR_INVALID_POINTER;
|
---|
| 231 |
|
---|
| 232 | /*
|
---|
[19286] | 233 | * Let dbgfR3MemRead do the job.
|
---|
[8819] | 234 | */
|
---|
[44399] | 235 | int rc = dbgfR3MemRead(pUVM, idCpu, pAddress, pszBuf, cchBuf);
|
---|
[8819] | 236 |
|
---|
| 237 | /*
|
---|
| 238 | * Make sure the result is terminated and that overflow is signaled.
|
---|
[19286] | 239 | * This may look a bit reckless with the rc but, it should be fine.
|
---|
[8819] | 240 | */
|
---|
[30320] | 241 | if (!RTStrEnd(pszBuf, cchBuf))
|
---|
[8819] | 242 | {
|
---|
| 243 | pszBuf[cchBuf - 1] = '\0';
|
---|
| 244 | rc = VINF_BUFFER_OVERFLOW;
|
---|
| 245 | }
|
---|
| 246 | /*
|
---|
| 247 | * Handle partial reads (not perfect).
|
---|
| 248 | */
|
---|
| 249 | else if (RT_FAILURE(rc))
|
---|
| 250 | {
|
---|
| 251 | if (pszBuf[0])
|
---|
| 252 | rc = VINF_SUCCESS;
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 | return rc;
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 |
|
---|
| 259 | /**
|
---|
| 260 | * Read a zero terminated string from guest memory.
|
---|
| 261 | *
|
---|
| 262 | * @returns VBox status code.
|
---|
[19286] | 263 | *
|
---|
[44399] | 264 | * @param pUVM The user mode VM handle.
|
---|
| 265 | * @param idCpu The ID of the source CPU context (for the address).
|
---|
| 266 | * @param pAddress Where to start reading.
|
---|
| 267 | * @param pszBuf Where to store the string.
|
---|
| 268 | * @param cchBuf The size of the buffer.
|
---|
[8819] | 269 | */
|
---|
[44399] | 270 | VMMR3DECL(int) DBGFR3MemReadString(PUVM pUVM, VMCPUID idCpu, PCDBGFADDRESS pAddress, char *pszBuf, size_t cchBuf)
|
---|
[8819] | 271 | {
|
---|
| 272 | /*
|
---|
| 273 | * Validate and zero output.
|
---|
| 274 | */
|
---|
| 275 | if (!VALID_PTR(pszBuf))
|
---|
| 276 | return VERR_INVALID_POINTER;
|
---|
| 277 | if (cchBuf <= 0)
|
---|
| 278 | return VERR_INVALID_PARAMETER;
|
---|
| 279 | memset(pszBuf, 0, cchBuf);
|
---|
[44399] | 280 | UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
|
---|
| 281 | AssertReturn(idCpu < pUVM->cCpus, VERR_INVALID_CPU_ID);
|
---|
[8819] | 282 |
|
---|
| 283 | /*
|
---|
| 284 | * Pass it on to the EMT.
|
---|
| 285 | */
|
---|
[44399] | 286 | return VMR3ReqPriorityCallWaitU(pUVM, idCpu, (PFNRT)dbgfR3MemReadString, 5, pUVM, idCpu, pAddress, pszBuf, cchBuf);
|
---|
[8819] | 287 | }
|
---|
| 288 |
|
---|
[18927] | 289 |
|
---|
| 290 | /**
|
---|
[19286] | 291 | * Writes guest memory.
|
---|
[18927] | 292 | *
|
---|
[19286] | 293 | * @returns VBox status code.
|
---|
[18927] | 294 | *
|
---|
[44399] | 295 | * @param pUVM The user mode VM handle.
|
---|
| 296 | * @param idCpu The ID of the target CPU context (for the address).
|
---|
| 297 | * @param pAddress Where to start writing.
|
---|
| 298 | * @param pvBuf The data to write.
|
---|
| 299 | * @param cbWrite The number of bytes to write.
|
---|
[18927] | 300 | */
|
---|
[44399] | 301 | static DECLCALLBACK(int) dbgfR3MemWrite(PUVM pUVM, VMCPUID idCpu, PCDBGFADDRESS pAddress, void const *pvBuf, size_t cbWrite)
|
---|
[18927] | 302 | {
|
---|
[19286] | 303 | /*
|
---|
| 304 | * Validate the input we use, PGM does the rest.
|
---|
| 305 | */
|
---|
[44399] | 306 | if (!DBGFR3AddrIsValid(pUVM, pAddress))
|
---|
[19286] | 307 | return VERR_INVALID_POINTER;
|
---|
| 308 | if (!VALID_PTR(pvBuf))
|
---|
| 309 | return VERR_INVALID_POINTER;
|
---|
[44399] | 310 | PVM pVM = pUVM->pVM;
|
---|
| 311 | VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
|
---|
[19286] | 312 |
|
---|
| 313 | /*
|
---|
[80014] | 314 | * Select PGM function by addressing mode.
|
---|
[19286] | 315 | */
|
---|
| 316 | int rc;
|
---|
[80014] | 317 | PVMCPU pVCpu = VMMGetCpuById(pVM, idCpu);
|
---|
| 318 | PGMMODE enmMode = PGMGetGuestMode(pVCpu);
|
---|
| 319 | if ( enmMode == PGMMODE_REAL
|
---|
| 320 | || enmMode == PGMMODE_PROTECTED
|
---|
| 321 | || DBGFADDRESS_IS_PHYS(pAddress) )
|
---|
| 322 | rc = PGMPhysSimpleWriteGCPhys(pVM, pAddress->FlatPtr, pvBuf, cbWrite);
|
---|
[19286] | 323 | else
|
---|
| 324 | {
|
---|
| 325 | #if GC_ARCH_BITS > 32
|
---|
[80014] | 326 | if ( ( pAddress->FlatPtr >= _4G
|
---|
| 327 | || pAddress->FlatPtr + cbWrite > _4G)
|
---|
| 328 | && enmMode != PGMMODE_AMD64
|
---|
| 329 | && enmMode != PGMMODE_AMD64_NX)
|
---|
| 330 | return VERR_PAGE_TABLE_NOT_PRESENT;
|
---|
[19286] | 331 | #endif
|
---|
[80014] | 332 | rc = PGMPhysSimpleWriteGCPtr(pVCpu, pAddress->FlatPtr, pvBuf, cbWrite);
|
---|
[19286] | 333 | }
|
---|
| 334 | return rc;
|
---|
[18927] | 335 | }
|
---|
| 336 |
|
---|
| 337 |
|
---|
| 338 | /**
|
---|
[19286] | 339 | * Read guest memory.
|
---|
[18927] | 340 | *
|
---|
[19286] | 341 | * @returns VBox status code.
|
---|
[18927] | 342 | *
|
---|
[44399] | 343 | * @param pUVM The user mode VM handle.
|
---|
| 344 | * @param idCpu The ID of the target CPU context (for the address).
|
---|
| 345 | * @param pAddress Where to start writing.
|
---|
| 346 | * @param pvBuf The data to write.
|
---|
[58126] | 347 | * @param cbWrite The number of bytes to write.
|
---|
[18927] | 348 | */
|
---|
[44399] | 349 | VMMR3DECL(int) DBGFR3MemWrite(PUVM pUVM, VMCPUID idCpu, PCDBGFADDRESS pAddress, void const *pvBuf, size_t cbWrite)
|
---|
[18927] | 350 | {
|
---|
[44399] | 351 | UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
|
---|
| 352 | AssertReturn(idCpu < pUVM->cCpus, VERR_INVALID_CPU_ID);
|
---|
| 353 | return VMR3ReqPriorityCallWaitU(pUVM, idCpu, (PFNRT)dbgfR3MemWrite, 5, pUVM, idCpu, pAddress, pvBuf, cbWrite);
|
---|
[18927] | 354 | }
|
---|
[19286] | 355 |
|
---|
| 356 |
|
---|
[19334] | 357 | /**
|
---|
| 358 | * Worker for DBGFR3SelQueryInfo that calls into SELM.
|
---|
| 359 | */
|
---|
[44399] | 360 | static DECLCALLBACK(int) dbgfR3SelQueryInfo(PUVM pUVM, VMCPUID idCpu, RTSEL Sel, uint32_t fFlags, PDBGFSELINFO pSelInfo)
|
---|
[19334] | 361 | {
|
---|
[44399] | 362 | PVM pVM = pUVM->pVM;
|
---|
| 363 | VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
|
---|
| 364 |
|
---|
[19334] | 365 | /*
|
---|
| 366 | * Make the query.
|
---|
| 367 | */
|
---|
[80014] | 368 | PVMCPU pVCpu = VMMGetCpuById(pVM, idCpu);
|
---|
| 369 | VMCPU_ASSERT_EMT(pVCpu);
|
---|
| 370 | int rc = SELMR3GetSelectorInfo(pVCpu, Sel, pSelInfo);
|
---|
| 371 |
|
---|
| 372 | /*
|
---|
| 373 | * 64-bit mode HACKS for making data and stack selectors wide open when
|
---|
| 374 | * queried. This is voodoo magic.
|
---|
| 375 | */
|
---|
| 376 | if (fFlags & DBGFSELQI_FLAGS_DT_ADJ_64BIT_MODE)
|
---|
[19334] | 377 | {
|
---|
[80014] | 378 | /* Expand 64-bit data and stack selectors. The check is a bit bogus... */
|
---|
| 379 | if ( RT_SUCCESS(rc)
|
---|
| 380 | && (pSelInfo->fFlags & ( DBGFSELINFO_FLAGS_LONG_MODE | DBGFSELINFO_FLAGS_REAL_MODE | DBGFSELINFO_FLAGS_PROT_MODE
|
---|
| 381 | | DBGFSELINFO_FLAGS_GATE | DBGFSELINFO_FLAGS_HYPER
|
---|
| 382 | | DBGFSELINFO_FLAGS_INVALID | DBGFSELINFO_FLAGS_NOT_PRESENT))
|
---|
| 383 | == DBGFSELINFO_FLAGS_LONG_MODE
|
---|
| 384 | && pSelInfo->cbLimit != ~(RTGCPTR)0
|
---|
| 385 | && CPUMIsGuestIn64BitCode(pVCpu) )
|
---|
[22105] | 386 | {
|
---|
[80014] | 387 | pSelInfo->GCPtrBase = 0;
|
---|
| 388 | pSelInfo->cbLimit = ~(RTGCPTR)0;
|
---|
[22105] | 389 | }
|
---|
[80014] | 390 | else if ( Sel == 0
|
---|
| 391 | && CPUMIsGuestIn64BitCode(pVCpu))
|
---|
| 392 | {
|
---|
| 393 | pSelInfo->GCPtrBase = 0;
|
---|
| 394 | pSelInfo->cbLimit = ~(RTGCPTR)0;
|
---|
| 395 | pSelInfo->Sel = 0;
|
---|
| 396 | pSelInfo->SelGate = 0;
|
---|
| 397 | pSelInfo->fFlags = DBGFSELINFO_FLAGS_LONG_MODE;
|
---|
| 398 | pSelInfo->u.Raw64.Gen.u1Present = 1;
|
---|
| 399 | pSelInfo->u.Raw64.Gen.u1Long = 1;
|
---|
| 400 | pSelInfo->u.Raw64.Gen.u1DescType = 1;
|
---|
| 401 | rc = VINF_SUCCESS;
|
---|
| 402 | }
|
---|
[19334] | 403 | }
|
---|
| 404 | return rc;
|
---|
| 405 | }
|
---|
[19286] | 406 |
|
---|
[19334] | 407 |
|
---|
| 408 | /**
|
---|
| 409 | * Gets information about a selector.
|
---|
| 410 | *
|
---|
| 411 | * Intended for the debugger mostly and will prefer the guest
|
---|
| 412 | * descriptor tables over the shadow ones.
|
---|
| 413 | *
|
---|
| 414 | * @returns VBox status code, the following are the common ones.
|
---|
| 415 | * @retval VINF_SUCCESS on success.
|
---|
| 416 | * @retval VERR_INVALID_SELECTOR if the selector isn't fully inside the
|
---|
| 417 | * descriptor table.
|
---|
[19336] | 418 | * @retval VERR_SELECTOR_NOT_PRESENT if the LDT is invalid or not present. This
|
---|
| 419 | * is not returned if the selector itself isn't present, you have to
|
---|
| 420 | * check that for yourself (see DBGFSELINFO::fFlags).
|
---|
[19334] | 421 | * @retval VERR_PAGE_TABLE_NOT_PRESENT or VERR_PAGE_NOT_PRESENT if the
|
---|
| 422 | * pagetable or page backing the selector table wasn't present.
|
---|
| 423 | *
|
---|
[44399] | 424 | * @param pUVM The user mode VM handle.
|
---|
[19334] | 425 | * @param idCpu The ID of the virtual CPU context.
|
---|
| 426 | * @param Sel The selector to get info about.
|
---|
| 427 | * @param fFlags Flags, see DBGFQSEL_FLAGS_*.
|
---|
| 428 | * @param pSelInfo Where to store the information. This will always be
|
---|
| 429 | * updated.
|
---|
| 430 | *
|
---|
| 431 | * @remarks This is a wrapper around SELMR3GetSelectorInfo and
|
---|
| 432 | * SELMR3GetShadowSelectorInfo.
|
---|
| 433 | */
|
---|
[44399] | 434 | VMMR3DECL(int) DBGFR3SelQueryInfo(PUVM pUVM, VMCPUID idCpu, RTSEL Sel, uint32_t fFlags, PDBGFSELINFO pSelInfo)
|
---|
[19334] | 435 | {
|
---|
[44399] | 436 | UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
|
---|
| 437 | AssertReturn(idCpu < pUVM->cCpus, VERR_INVALID_CPU_ID);
|
---|
[80014] | 438 | AssertReturn(!(fFlags & ~(DBGFSELQI_FLAGS_DT_GUEST | DBGFSELQI_FLAGS_DT_ADJ_64BIT_MODE)), VERR_INVALID_PARAMETER);
|
---|
[19334] | 439 |
|
---|
| 440 | /* Clear the return data here on this thread. */
|
---|
| 441 | memset(pSelInfo, 0, sizeof(*pSelInfo));
|
---|
| 442 |
|
---|
| 443 | /*
|
---|
| 444 | * Dispatch the request to a worker running on the target CPU.
|
---|
| 445 | */
|
---|
[44399] | 446 | return VMR3ReqPriorityCallWaitU(pUVM, idCpu, (PFNRT)dbgfR3SelQueryInfo, 5, pUVM, idCpu, Sel, fFlags, pSelInfo);
|
---|
[19334] | 447 | }
|
---|
| 448 |
|
---|
| 449 |
|
---|
| 450 | /**
|
---|
| 451 | * Validates a CS selector.
|
---|
| 452 | *
|
---|
| 453 | * @returns VBox status code.
|
---|
| 454 | * @param pSelInfo Pointer to the selector information for the CS selector.
|
---|
| 455 | * @param SelCPL The selector defining the CPL (SS).
|
---|
| 456 | */
|
---|
| 457 | VMMDECL(int) DBGFR3SelInfoValidateCS(PCDBGFSELINFO pSelInfo, RTSEL SelCPL)
|
---|
| 458 | {
|
---|
| 459 | /*
|
---|
| 460 | * Check if present.
|
---|
| 461 | */
|
---|
[19463] | 462 | if (pSelInfo->u.Raw.Gen.u1Present)
|
---|
[19334] | 463 | {
|
---|
| 464 | /*
|
---|
| 465 | * Type check.
|
---|
| 466 | */
|
---|
[19463] | 467 | if ( pSelInfo->u.Raw.Gen.u1DescType == 1
|
---|
| 468 | && (pSelInfo->u.Raw.Gen.u4Type & X86_SEL_TYPE_CODE))
|
---|
[19334] | 469 | {
|
---|
| 470 | /*
|
---|
| 471 | * Check level.
|
---|
| 472 | */
|
---|
| 473 | unsigned uLevel = RT_MAX(SelCPL & X86_SEL_RPL, pSelInfo->Sel & X86_SEL_RPL);
|
---|
[19463] | 474 | if ( !(pSelInfo->u.Raw.Gen.u4Type & X86_SEL_TYPE_CONF)
|
---|
| 475 | ? uLevel <= pSelInfo->u.Raw.Gen.u2Dpl
|
---|
| 476 | : uLevel >= pSelInfo->u.Raw.Gen.u2Dpl /* hope I got this right now... */
|
---|
[19334] | 477 | )
|
---|
| 478 | return VINF_SUCCESS;
|
---|
| 479 | return VERR_INVALID_RPL;
|
---|
| 480 | }
|
---|
| 481 | return VERR_NOT_CODE_SELECTOR;
|
---|
| 482 | }
|
---|
| 483 | return VERR_SELECTOR_NOT_PRESENT;
|
---|
| 484 | }
|
---|
| 485 |
|
---|
[31966] | 486 |
|
---|
| 487 | /**
|
---|
[33540] | 488 | * Converts a PGM paging mode to a set of DBGFPGDMP_XXX flags.
|
---|
[31966] | 489 | *
|
---|
| 490 | * @returns Flags. UINT32_MAX if the mode is invalid (asserted).
|
---|
| 491 | * @param enmMode The mode.
|
---|
| 492 | */
|
---|
| 493 | static uint32_t dbgfR3PagingDumpModeToFlags(PGMMODE enmMode)
|
---|
| 494 | {
|
---|
| 495 | switch (enmMode)
|
---|
| 496 | {
|
---|
| 497 | case PGMMODE_32_BIT:
|
---|
| 498 | return DBGFPGDMP_FLAGS_PSE;
|
---|
| 499 | case PGMMODE_PAE:
|
---|
| 500 | return DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE;
|
---|
| 501 | case PGMMODE_PAE_NX:
|
---|
| 502 | return DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE | DBGFPGDMP_FLAGS_NXE;
|
---|
| 503 | case PGMMODE_AMD64:
|
---|
| 504 | return DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE | DBGFPGDMP_FLAGS_LME;
|
---|
| 505 | case PGMMODE_AMD64_NX:
|
---|
| 506 | return DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE | DBGFPGDMP_FLAGS_LME | DBGFPGDMP_FLAGS_NXE;
|
---|
[73246] | 507 | case PGMMODE_NESTED_32BIT:
|
---|
[73346] | 508 | return DBGFPGDMP_FLAGS_NP | DBGFPGDMP_FLAGS_PSE;
|
---|
[73246] | 509 | case PGMMODE_NESTED_PAE:
|
---|
[73346] | 510 | return DBGFPGDMP_FLAGS_NP | DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE | DBGFPGDMP_FLAGS_NXE;
|
---|
[73246] | 511 | case PGMMODE_NESTED_AMD64:
|
---|
[73346] | 512 | return DBGFPGDMP_FLAGS_NP | DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE | DBGFPGDMP_FLAGS_LME | DBGFPGDMP_FLAGS_NXE;
|
---|
[31966] | 513 | case PGMMODE_EPT:
|
---|
| 514 | return DBGFPGDMP_FLAGS_EPT;
|
---|
[73346] | 515 | case PGMMODE_NONE:
|
---|
| 516 | return 0;
|
---|
[31966] | 517 | default:
|
---|
| 518 | AssertFailedReturn(UINT32_MAX);
|
---|
| 519 | }
|
---|
| 520 | }
|
---|
| 521 |
|
---|
| 522 |
|
---|
| 523 | /**
|
---|
| 524 | * EMT worker for DBGFR3PagingDumpEx.
|
---|
| 525 | *
|
---|
| 526 | * @returns VBox status code.
|
---|
[44399] | 527 | * @param pUVM The shared VM handle.
|
---|
[31966] | 528 | * @param idCpu The current CPU ID.
|
---|
| 529 | * @param fFlags The flags, DBGFPGDMP_FLAGS_XXX. Valid.
|
---|
| 530 | * @param pcr3 The CR3 to use (unless we're getting the current
|
---|
| 531 | * state, see @a fFlags).
|
---|
| 532 | * @param pu64FirstAddr The first address.
|
---|
| 533 | * @param pu64LastAddr The last address.
|
---|
| 534 | * @param cMaxDepth The depth.
|
---|
| 535 | * @param pHlp The output callbacks.
|
---|
| 536 | */
|
---|
[44399] | 537 | static DECLCALLBACK(int) dbgfR3PagingDumpEx(PUVM pUVM, VMCPUID idCpu, uint32_t fFlags, uint64_t *pcr3,
|
---|
[31966] | 538 | uint64_t *pu64FirstAddr, uint64_t *pu64LastAddr,
|
---|
| 539 | uint32_t cMaxDepth, PCDBGFINFOHLP pHlp)
|
---|
| 540 | {
|
---|
| 541 | /*
|
---|
| 542 | * Implement dumping both context by means of recursion.
|
---|
| 543 | */
|
---|
| 544 | if ((fFlags & (DBGFPGDMP_FLAGS_GUEST | DBGFPGDMP_FLAGS_SHADOW)) == (DBGFPGDMP_FLAGS_GUEST | DBGFPGDMP_FLAGS_SHADOW))
|
---|
| 545 | {
|
---|
[44399] | 546 | int rc1 = dbgfR3PagingDumpEx(pUVM, idCpu, fFlags & ~DBGFPGDMP_FLAGS_GUEST,
|
---|
[31966] | 547 | pcr3, pu64FirstAddr, pu64LastAddr, cMaxDepth, pHlp);
|
---|
[44399] | 548 | int rc2 = dbgfR3PagingDumpEx(pUVM, idCpu, fFlags & ~DBGFPGDMP_FLAGS_SHADOW,
|
---|
[31966] | 549 | pcr3, pu64FirstAddr, pu64LastAddr, cMaxDepth, pHlp);
|
---|
| 550 | return RT_FAILURE(rc1) ? rc1 : rc2;
|
---|
| 551 | }
|
---|
| 552 |
|
---|
[44399] | 553 | PVM pVM = pUVM->pVM;
|
---|
| 554 | VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
|
---|
| 555 |
|
---|
[31966] | 556 | /*
|
---|
| 557 | * Get the current CR3/mode if required.
|
---|
| 558 | */
|
---|
| 559 | uint64_t cr3 = *pcr3;
|
---|
| 560 | if (fFlags & (DBGFPGDMP_FLAGS_CURRENT_CR3 | DBGFPGDMP_FLAGS_CURRENT_MODE))
|
---|
| 561 | {
|
---|
[80191] | 562 | PVMCPU pVCpu = pVM->apCpusR3[idCpu];
|
---|
[31966] | 563 | if (fFlags & DBGFPGDMP_FLAGS_SHADOW)
|
---|
| 564 | {
|
---|
[73346] | 565 | if (PGMGetShadowMode(pVCpu) == PGMMODE_NONE)
|
---|
| 566 | {
|
---|
| 567 | pHlp->pfnPrintf(pHlp, "Shadow paging mode is 'none' (NEM)\n");
|
---|
| 568 | return VINF_SUCCESS;
|
---|
| 569 | }
|
---|
| 570 |
|
---|
[31966] | 571 | if (fFlags & DBGFPGDMP_FLAGS_CURRENT_CR3)
|
---|
| 572 | cr3 = PGMGetHyperCR3(pVCpu);
|
---|
| 573 | if (fFlags & DBGFPGDMP_FLAGS_CURRENT_MODE)
|
---|
| 574 | fFlags |= dbgfR3PagingDumpModeToFlags(PGMGetShadowMode(pVCpu));
|
---|
| 575 | }
|
---|
| 576 | else
|
---|
| 577 | {
|
---|
| 578 | if (fFlags & DBGFPGDMP_FLAGS_CURRENT_CR3)
|
---|
| 579 | cr3 = CPUMGetGuestCR3(pVCpu);
|
---|
| 580 | if (fFlags & DBGFPGDMP_FLAGS_CURRENT_MODE)
|
---|
| 581 | {
|
---|
| 582 | AssertCompile(DBGFPGDMP_FLAGS_PSE == X86_CR4_PSE); AssertCompile(DBGFPGDMP_FLAGS_PAE == X86_CR4_PAE);
|
---|
| 583 | fFlags |= CPUMGetGuestCR4(pVCpu) & (X86_CR4_PSE | X86_CR4_PAE);
|
---|
| 584 | AssertCompile(DBGFPGDMP_FLAGS_LME == MSR_K6_EFER_LME); AssertCompile(DBGFPGDMP_FLAGS_NXE == MSR_K6_EFER_NXE);
|
---|
[31987] | 585 | fFlags |= CPUMGetGuestEFER(pVCpu) & (MSR_K6_EFER_LME | MSR_K6_EFER_NXE);
|
---|
[31966] | 586 | }
|
---|
| 587 | }
|
---|
| 588 | }
|
---|
| 589 | fFlags &= ~(DBGFPGDMP_FLAGS_CURRENT_MODE | DBGFPGDMP_FLAGS_CURRENT_CR3);
|
---|
| 590 |
|
---|
| 591 | /*
|
---|
| 592 | * Call PGM to do the real work.
|
---|
| 593 | */
|
---|
| 594 | int rc;
|
---|
| 595 | if (fFlags & DBGFPGDMP_FLAGS_SHADOW)
|
---|
[31989] | 596 | rc = PGMR3DumpHierarchyShw(pVM, cr3, fFlags, *pu64FirstAddr, *pu64LastAddr, cMaxDepth, pHlp);
|
---|
[31966] | 597 | else
|
---|
[31989] | 598 | rc = PGMR3DumpHierarchyGst(pVM, cr3, fFlags, *pu64FirstAddr, *pu64LastAddr, cMaxDepth, pHlp);
|
---|
[31966] | 599 | return rc;
|
---|
| 600 | }
|
---|
| 601 |
|
---|
| 602 |
|
---|
| 603 | /**
|
---|
| 604 | * Dump paging structures.
|
---|
| 605 | *
|
---|
| 606 | * This API can be used to dump both guest and shadow structures.
|
---|
| 607 | *
|
---|
| 608 | * @returns VBox status code.
|
---|
[44399] | 609 | * @param pUVM The user mode VM handle.
|
---|
[31966] | 610 | * @param idCpu The current CPU ID.
|
---|
| 611 | * @param fFlags The flags, DBGFPGDMP_FLAGS_XXX.
|
---|
| 612 | * @param cr3 The CR3 to use (unless we're getting the current
|
---|
| 613 | * state, see @a fFlags).
|
---|
| 614 | * @param u64FirstAddr The address to start dumping at.
|
---|
| 615 | * @param u64LastAddr The address to end dumping after.
|
---|
| 616 | * @param cMaxDepth The depth.
|
---|
| 617 | * @param pHlp The output callbacks. Defaults to the debug log if
|
---|
| 618 | * NULL.
|
---|
| 619 | */
|
---|
[44399] | 620 | VMMDECL(int) DBGFR3PagingDumpEx(PUVM pUVM, VMCPUID idCpu, uint32_t fFlags, uint64_t cr3, uint64_t u64FirstAddr,
|
---|
[31966] | 621 | uint64_t u64LastAddr, uint32_t cMaxDepth, PCDBGFINFOHLP pHlp)
|
---|
| 622 | {
|
---|
| 623 | /*
|
---|
| 624 | * Input validation.
|
---|
| 625 | */
|
---|
[44399] | 626 | UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
|
---|
| 627 | AssertReturn(idCpu < pUVM->cCpus, VERR_INVALID_CPU_ID);
|
---|
[31966] | 628 | AssertReturn(!(fFlags & ~DBGFPGDMP_FLAGS_VALID_MASK), VERR_INVALID_PARAMETER);
|
---|
| 629 | AssertReturn(fFlags & (DBGFPGDMP_FLAGS_SHADOW | DBGFPGDMP_FLAGS_GUEST), VERR_INVALID_PARAMETER);
|
---|
| 630 | AssertReturn((fFlags & DBGFPGDMP_FLAGS_CURRENT_MODE) || !(fFlags & DBGFPGDMP_FLAGS_MODE_MASK), VERR_INVALID_PARAMETER);
|
---|
| 631 | AssertReturn( !(fFlags & DBGFPGDMP_FLAGS_EPT)
|
---|
| 632 | || !(fFlags & (DBGFPGDMP_FLAGS_LME | DBGFPGDMP_FLAGS_PAE | DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_NXE))
|
---|
| 633 | , VERR_INVALID_PARAMETER);
|
---|
| 634 | AssertPtrReturn(pHlp, VERR_INVALID_POINTER);
|
---|
| 635 | AssertReturn(cMaxDepth, VERR_INVALID_PARAMETER);
|
---|
| 636 |
|
---|
| 637 | /*
|
---|
| 638 | * Forward the request to the target CPU.
|
---|
| 639 | */
|
---|
[44399] | 640 | return VMR3ReqPriorityCallWaitU(pUVM, idCpu, (PFNRT)dbgfR3PagingDumpEx, 8,
|
---|
| 641 | pUVM, idCpu, fFlags, &cr3, &u64FirstAddr, &u64LastAddr, cMaxDepth, pHlp);
|
---|
[31966] | 642 | }
|
---|
| 643 |
|
---|