VirtualBox

source: vbox/trunk/src/VBox/VMM/DBGFMem.cpp@ 19463

Last change on this file since 19463 was 19463, checked in by vboxsync, 16 years ago

dbgfsel.h,VMM,DBGC: Named the union containing the raw data to shut up gcc warnings.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 15.4 KB
Line 
1/* $Id: DBGFMem.cpp 19463 2009-05-06 20:30:57Z vboxsync $ */
2/** @file
3 * DBGF - Debugger Facility, Memory Methods.
4 */
5
6/*
7 * Copyright (C) 2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22
23/*******************************************************************************
24* Header Files *
25*******************************************************************************/
26#define LOG_GROUP LOG_GROUP_DBGF
27#include <VBox/dbgf.h>
28#include <VBox/pgm.h>
29#include <VBox/selm.h>
30#include <VBox/hwaccm.h>
31#include "DBGFInternal.h"
32#include <VBox/vm.h>
33#include <VBox/err.h>
34#include <VBox/log.h>
35#include <VBox/mm.h>
36
37
38
39/**
40 * Scan guest memory for an exact byte string.
41 *
42 * @returns VBox status code.
43 * @param pVM The VM handle.
44 * @param idCpu The ID of the CPU context to search in.
45 * @param pAddress Where to store the mixed address.
46 * @param pcbRange The number of bytes to scan. Passed as a pointer because
47 * it may be 64-bit.
48 * @param pabNeedle What to search for - exact search.
49 * @param cbNeedle Size of the search byte string.
50 * @param pHitAddress Where to put the address of the first hit.
51 */
52static DECLCALLBACK(int) dbgfR3MemScan(PVM pVM, VMCPUID idCpu, PCDBGFADDRESS pAddress, PCRTGCUINTPTR pcbRange,
53 const uint8_t *pabNeedle, size_t cbNeedle, PDBGFADDRESS pHitAddress)
54{
55 Assert(idCpu == VMMGetCpuId(pVM));
56
57 /*
58 * Validate the input we use, PGM does the rest.
59 */
60 RTGCUINTPTR cbRange = *pcbRange;
61 if (!DBGFR3AddrIsValid(pVM, pAddress))
62 return VERR_INVALID_POINTER;
63 if (!VALID_PTR(pHitAddress))
64 return VERR_INVALID_POINTER;
65 if (DBGFADDRESS_IS_HMA(pAddress))
66 return VERR_INVALID_POINTER;
67
68 /*
69 * Select DBGF worker by addressing mode.
70 */
71 int rc;
72 PVMCPU pVCpu = VMMGetCpuById(pVM, idCpu);
73 PGMMODE enmMode = PGMGetGuestMode(pVCpu);
74 if ( enmMode == PGMMODE_REAL
75 || enmMode == PGMMODE_PROTECTED
76 || DBGFADDRESS_IS_PHYS(pAddress)
77 )
78 {
79 RTGCPHYS PhysHit;
80 rc = PGMR3DbgScanPhysical(pVM, pAddress->FlatPtr, cbRange, pabNeedle, cbNeedle, &PhysHit);
81 if (RT_SUCCESS(rc))
82 DBGFR3AddrFromPhys(pVM, pHitAddress, PhysHit);
83 }
84 else
85 {
86#if GC_ARCH_BITS > 32
87 if ( ( pAddress->FlatPtr >= _4G
88 || pAddress->FlatPtr + cbRange > _4G)
89 && enmMode != PGMMODE_AMD64
90 && enmMode != PGMMODE_AMD64_NX)
91 return VERR_DBGF_MEM_NOT_FOUND;
92#endif
93 RTGCUINTPTR GCPtrHit;
94 rc = PGMR3DbgScanVirtual(pVM, pVCpu, pAddress->FlatPtr, cbRange, pabNeedle, cbNeedle, &GCPtrHit);
95 if (RT_SUCCESS(rc))
96 DBGFR3AddrFromFlat(pVM, pHitAddress, GCPtrHit);
97 }
98
99 return rc;
100}
101
102
103/**
104 * Scan guest memory for an exact byte string.
105 *
106 * @returns VBox status codes:
107 * @retval VINF_SUCCESS and *pGCPtrHit on success.
108 * @retval VERR_DBGF_MEM_NOT_FOUND if not found.
109 * @retval VERR_INVALID_POINTER if any of the pointer arguments are invalid.
110 * @retval VERR_INVALID_ARGUMENT if any other arguments are invalid.
111 *
112 * @param pVM The VM handle.
113 * @param idCpu The ID of the CPU context to search in.
114 * @param pAddress Where to store the mixed address.
115 * @param cbRange The number of bytes to scan.
116 * @param pabNeedle What to search for - exact search.
117 * @param cbNeedle Size of the search byte string.
118 * @param pHitAddress Where to put the address of the first hit.
119 *
120 * @thread Any thread.
121 */
122VMMR3DECL(int) DBGFR3MemScan(PVM pVM, VMCPUID idCpu, PCDBGFADDRESS pAddress, RTGCUINTPTR cbRange, const uint8_t *pabNeedle, size_t cbNeedle, PDBGFADDRESS pHitAddress)
123{
124 AssertReturn(idCpu < pVM->cCPUs, VERR_INVALID_PARAMETER);
125
126 PVMREQ pReq;
127 int rc = VMR3ReqCall(pVM, idCpu, &pReq, RT_INDEFINITE_WAIT,
128 (PFNRT)dbgfR3MemScan, 7, pVM, idCpu, pAddress, &cbRange, pabNeedle, cbNeedle, pHitAddress);
129 if (RT_SUCCESS(rc))
130 rc = pReq->iStatus;
131 VMR3ReqFree(pReq);
132
133 return rc;
134}
135
136
137/**
138 * Read guest memory.
139 *
140 * @returns VBox status code.
141 * @param pVM Pointer to the shared VM structure.
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.
145 */
146static DECLCALLBACK(int) dbgfR3MemRead(PVM pVM, VMCPUID idCpu, PCDBGFADDRESS pAddress, void *pvBuf, size_t cbRead)
147{
148 Assert(idCpu == VMMGetCpuId(pVM));
149
150 /*
151 * Validate the input we use, PGM does the rest.
152 */
153 if (!DBGFR3AddrIsValid(pVM, pAddress))
154 return VERR_INVALID_POINTER;
155 if (!VALID_PTR(pvBuf))
156 return VERR_INVALID_POINTER;
157
158 /*
159 * HMA is special
160 */
161 int rc;
162 if (DBGFADDRESS_IS_HMA(pAddress))
163 {
164 rc = VERR_INVALID_POINTER;
165 }
166 else
167 {
168 /*
169 * Select DBGF worker by addressing mode.
170 */
171 PVMCPU pVCpu = VMMGetCpuById(pVM, idCpu);
172 PGMMODE enmMode = PGMGetGuestMode(pVCpu);
173 if ( enmMode == PGMMODE_REAL
174 || enmMode == PGMMODE_PROTECTED
175 || DBGFADDRESS_IS_PHYS(pAddress) )
176 rc = PGMPhysSimpleReadGCPhys(pVM, pvBuf, pAddress->FlatPtr, cbRead);
177 else
178 {
179#if GC_ARCH_BITS > 32
180 if ( ( pAddress->FlatPtr >= _4G
181 || pAddress->FlatPtr + cbRead > _4G)
182 && enmMode != PGMMODE_AMD64
183 && enmMode != PGMMODE_AMD64_NX)
184 return VERR_PAGE_TABLE_NOT_PRESENT;
185#endif
186 rc = PGMPhysSimpleReadGCPtr(pVCpu, pvBuf, pAddress->FlatPtr, cbRead);
187 }
188 }
189 return rc;
190}
191
192
193/**
194 * Read guest memory.
195 *
196 * @returns VBox status code.
197 *
198 * @param pVM Pointer to the shared VM structure.
199 * @param idCpu The ID of the source CPU context (for the address).
200 * @param pAddress Where to start reading.
201 * @param pvBuf Where to store the data we've read.
202 * @param cbRead The number of bytes to read.
203 */
204VMMR3DECL(int) DBGFR3MemRead(PVM pVM, VMCPUID idCpu, PCDBGFADDRESS pAddress, void *pvBuf, size_t cbRead)
205{
206 AssertReturn(idCpu < pVM->cCPUs, VERR_INVALID_PARAMETER);
207
208 PVMREQ pReq;
209 int rc = VMR3ReqCallU(pVM->pUVM, idCpu, &pReq, RT_INDEFINITE_WAIT, 0,
210 (PFNRT)dbgfR3MemRead, 5, pVM, idCpu, pAddress, pvBuf, cbRead);
211 if (RT_SUCCESS(rc))
212 rc = pReq->iStatus;
213 VMR3ReqFree(pReq);
214
215 return rc;
216}
217
218
219/**
220 * Read a zero terminated string from guest memory.
221 *
222 * @returns VBox status code.
223 *
224 * @param pVM Pointer to the shared VM structure.
225 * @param idCpu The ID of the source CPU context (for the address).
226 * @param pAddress Where to start reading.
227 * @param pszBuf Where to store the string.
228 * @param cchBuf The size of the buffer.
229 */
230static DECLCALLBACK(int) dbgfR3MemReadString(PVM pVM, VMCPUID idCpu, PCDBGFADDRESS pAddress, char *pszBuf, size_t cchBuf)
231{
232 /*
233 * Validate the input we use, PGM does the rest.
234 */
235 if (!DBGFR3AddrIsValid(pVM, pAddress))
236 return VERR_INVALID_POINTER;
237 if (!VALID_PTR(pszBuf))
238 return VERR_INVALID_POINTER;
239
240 /*
241 * Let dbgfR3MemRead do the job.
242 */
243 int rc = dbgfR3MemRead(pVM, idCpu, pAddress, pszBuf, cchBuf);
244
245 /*
246 * Make sure the result is terminated and that overflow is signaled.
247 * This may look a bit reckless with the rc but, it should be fine.
248 */
249 if (!memchr(pszBuf, '\0', cchBuf))
250 {
251 pszBuf[cchBuf - 1] = '\0';
252 rc = VINF_BUFFER_OVERFLOW;
253 }
254 /*
255 * Handle partial reads (not perfect).
256 */
257 else if (RT_FAILURE(rc))
258 {
259 if (pszBuf[0])
260 rc = VINF_SUCCESS;
261 }
262
263 return rc;
264}
265
266
267/**
268 * Read a zero terminated string from guest memory.
269 *
270 * @returns VBox status code.
271 *
272 * @param pVM Pointer to the shared VM structure.
273 * @param idCpu The ID of the source CPU context (for the address).
274 * @param pAddress Where to start reading.
275 * @param pszBuf Where to store the string.
276 * @param cchBuf The size of the buffer.
277 */
278VMMR3DECL(int) DBGFR3MemReadString(PVM pVM, VMCPUID idCpu, PCDBGFADDRESS pAddress, char *pszBuf, size_t cchBuf)
279{
280 /*
281 * Validate and zero output.
282 */
283 if (!VALID_PTR(pszBuf))
284 return VERR_INVALID_POINTER;
285 if (cchBuf <= 0)
286 return VERR_INVALID_PARAMETER;
287 memset(pszBuf, 0, cchBuf);
288 AssertReturn(idCpu < pVM->cCPUs, VERR_INVALID_PARAMETER);
289
290 /*
291 * Pass it on to the EMT.
292 */
293 PVMREQ pReq;
294 int rc = VMR3ReqCallU(pVM->pUVM, idCpu, &pReq, RT_INDEFINITE_WAIT, 0,
295 (PFNRT)dbgfR3MemReadString, 5, pVM, idCpu, pAddress, pszBuf, cchBuf);
296 if (RT_SUCCESS(rc))
297 rc = pReq->iStatus;
298 VMR3ReqFree(pReq);
299
300 return rc;
301}
302
303
304/**
305 * Writes guest memory.
306 *
307 * @returns VBox status code.
308 *
309 * @param pVM Pointer to the shared VM structure.
310 * @param idCpu The ID of the target CPU context (for the address).
311 * @param pAddress Where to start writing.
312 * @param pvBuf The data to write.
313 * @param cbRead The number of bytes to write.
314 */
315static DECLCALLBACK(int) dbgfR3MemWrite(PVM pVM, VMCPUID idCpu, PCDBGFADDRESS pAddress, void const *pvBuf, size_t cbWrite)
316{
317 /*
318 * Validate the input we use, PGM does the rest.
319 */
320 if (!DBGFR3AddrIsValid(pVM, pAddress))
321 return VERR_INVALID_POINTER;
322 if (!VALID_PTR(pvBuf))
323 return VERR_INVALID_POINTER;
324
325 /*
326 * HMA is always special.
327 */
328 int rc;
329 if (DBGFADDRESS_IS_HMA(pAddress))
330 {
331 /** @todo write to HMA. */
332 rc = VERR_ACCESS_DENIED;
333 }
334 else
335 {
336 /*
337 * Select PGM function by addressing mode.
338 */
339 PVMCPU pVCpu = VMMGetCpuById(pVM, idCpu);
340 PGMMODE enmMode = PGMGetGuestMode(pVCpu);
341 if ( enmMode == PGMMODE_REAL
342 || enmMode == PGMMODE_PROTECTED
343 || DBGFADDRESS_IS_PHYS(pAddress) )
344 rc = PGMPhysSimpleWriteGCPhys(pVM, pAddress->FlatPtr, pvBuf, cbWrite);
345 else
346 {
347#if GC_ARCH_BITS > 32
348 if ( ( pAddress->FlatPtr >= _4G
349 || pAddress->FlatPtr + cbWrite > _4G)
350 && enmMode != PGMMODE_AMD64
351 && enmMode != PGMMODE_AMD64_NX)
352 return VERR_PAGE_TABLE_NOT_PRESENT;
353#endif
354 rc = PGMPhysSimpleWriteGCPtr(pVCpu, pAddress->FlatPtr, pvBuf, cbWrite);
355 }
356 }
357 return rc;
358}
359
360
361/**
362 * Read guest memory.
363 *
364 * @returns VBox status code.
365 *
366 * @param pVM Pointer to the shared VM structure.
367 * @param idCpu The ID of the target CPU context (for the address).
368 * @param pAddress Where to start writing.
369 * @param pvBuf The data to write.
370 * @param cbRead The number of bytes to write.
371 */
372VMMR3DECL(int) DBGFR3MemWrite(PVM pVM, VMCPUID idCpu, PCDBGFADDRESS pAddress, void const *pvBuf, size_t cbWrite)
373{
374 AssertReturn(idCpu < pVM->cCPUs, VERR_INVALID_PARAMETER);
375
376 PVMREQ pReq;
377 int rc = VMR3ReqCallU(pVM->pUVM, idCpu, &pReq, RT_INDEFINITE_WAIT, 0,
378 (PFNRT)dbgfR3MemWrite, 5, pVM, idCpu, pAddress, pvBuf, cbWrite);
379 if (RT_SUCCESS(rc))
380 rc = pReq->iStatus;
381 VMR3ReqFree(pReq);
382
383 return rc;
384}
385
386
387/**
388 * Worker for DBGFR3SelQueryInfo that calls into SELM.
389 */
390static DECLCALLBACK(int) dbgfR3SelQueryInfo(PVM pVM, VMCPUID idCpu, RTSEL Sel, uint32_t fFlags, PDBGFSELINFO pSelInfo)
391{
392 /*
393 * Make the query.
394 */
395 int rc;
396 if (!(fFlags & DBGFSELQI_FLAGS_DT_GUEST))
397 {
398 PVMCPU pVCpu = VMMGetCpuById(pVM, idCpu);
399 VMCPU_ASSERT_EMT(pVCpu);
400 rc = SELMR3GetSelectorInfo(pVM, pVCpu, Sel, pSelInfo);
401 }
402 else
403 {
404 if (HWACCMIsEnabled(pVM))
405 rc = VERR_INVALID_STATE;
406 else
407 rc = SELMR3GetShadowSelectorInfo(pVM, Sel, pSelInfo);
408 }
409 return rc;
410}
411
412
413/**
414 * Gets information about a selector.
415 *
416 * Intended for the debugger mostly and will prefer the guest
417 * descriptor tables over the shadow ones.
418 *
419 * @returns VBox status code, the following are the common ones.
420 * @retval VINF_SUCCESS on success.
421 * @retval VERR_INVALID_SELECTOR if the selector isn't fully inside the
422 * descriptor table.
423 * @retval VERR_SELECTOR_NOT_PRESENT if the LDT is invalid or not present. This
424 * is not returned if the selector itself isn't present, you have to
425 * check that for yourself (see DBGFSELINFO::fFlags).
426 * @retval VERR_PAGE_TABLE_NOT_PRESENT or VERR_PAGE_NOT_PRESENT if the
427 * pagetable or page backing the selector table wasn't present.
428 *
429 * @param pVM VM handle.
430 * @param idCpu The ID of the virtual CPU context.
431 * @param Sel The selector to get info about.
432 * @param fFlags Flags, see DBGFQSEL_FLAGS_*.
433 * @param pSelInfo Where to store the information. This will always be
434 * updated.
435 *
436 * @remarks This is a wrapper around SELMR3GetSelectorInfo and
437 * SELMR3GetShadowSelectorInfo.
438 */
439VMMR3DECL(int) DBGFR3SelQueryInfo(PVM pVM, VMCPUID idCpu, RTSEL Sel, uint32_t fFlags, PDBGFSELINFO pSelInfo)
440{
441 AssertReturn(idCpu < pVM->cCPUs, VERR_INVALID_PARAMETER);
442 AssertReturn(!(fFlags & ~(DBGFSELQI_FLAGS_DT_GUEST | DBGFSELQI_FLAGS_DT_SHADOW)), VERR_INVALID_PARAMETER);
443
444 /* Clear the return data here on this thread. */
445 memset(pSelInfo, 0, sizeof(*pSelInfo));
446
447 /*
448 * Dispatch the request to a worker running on the target CPU.
449 */
450 PVMREQ pReq;
451 int rc = VMR3ReqCallU(pVM->pUVM, idCpu, &pReq, RT_INDEFINITE_WAIT, 0,
452 (PFNRT)dbgfR3SelQueryInfo, 5, pVM, idCpu, Sel, fFlags, pSelInfo);
453 if (RT_SUCCESS(rc))
454 rc = pReq->iStatus;
455 VMR3ReqFree(pReq);
456
457 return rc;
458}
459
460
461/**
462 * Validates a CS selector.
463 *
464 * @returns VBox status code.
465 * @param pSelInfo Pointer to the selector information for the CS selector.
466 * @param SelCPL The selector defining the CPL (SS).
467 */
468VMMDECL(int) DBGFR3SelInfoValidateCS(PCDBGFSELINFO pSelInfo, RTSEL SelCPL)
469{
470 /*
471 * Check if present.
472 */
473 if (pSelInfo->u.Raw.Gen.u1Present)
474 {
475 /*
476 * Type check.
477 */
478 if ( pSelInfo->u.Raw.Gen.u1DescType == 1
479 && (pSelInfo->u.Raw.Gen.u4Type & X86_SEL_TYPE_CODE))
480 {
481 /*
482 * Check level.
483 */
484 unsigned uLevel = RT_MAX(SelCPL & X86_SEL_RPL, pSelInfo->Sel & X86_SEL_RPL);
485 if ( !(pSelInfo->u.Raw.Gen.u4Type & X86_SEL_TYPE_CONF)
486 ? uLevel <= pSelInfo->u.Raw.Gen.u2Dpl
487 : uLevel >= pSelInfo->u.Raw.Gen.u2Dpl /* hope I got this right now... */
488 )
489 return VINF_SUCCESS;
490 return VERR_INVALID_RPL;
491 }
492 return VERR_NOT_CODE_SELECTOR;
493 }
494 return VERR_SELECTOR_NOT_PRESENT;
495}
496
497
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