VirtualBox

source: vbox/trunk/src/VBox/VMM/PGMDbg.cpp@ 5667

Last change on this file since 5667 was 5667, checked in by vboxsync, 17 years ago

Debugger interface for searching memory. Fixed a const mixup.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 17.1 KB
Line 
1/* $Id: PGMDbg.cpp 5667 2007-11-11 04:28:47Z vboxsync $ */
2/** @file
3 * PGM - Page Manager and Monitor - Debugger & Debugging APIs.
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
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 as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18/*******************************************************************************
19* Header Files *
20*******************************************************************************/
21#define LOG_GROUP LOG_GROUP_PGM
22#include <VBox/pgm.h>
23#include <VBox/stam.h>
24#include "PGMInternal.h"
25#include <VBox/vm.h>
26#include <iprt/assert.h>
27#include <iprt/asm.h>
28#include <iprt/string.h>
29#include <VBox/log.h>
30#include <VBox/param.h>
31#include <VBox/err.h>
32
33/** The max needle size that we will bother searching for
34 * This must not be more than half a page! */
35#define MAX_NEEDLE_SIZE 256
36
37
38/**
39 * Converts a HC pointer to a GC physical address.
40 *
41 * Only for the debugger.
42 *
43 * @returns VBox status code.
44 * @retval VINF_SUCCESS on success, *pGCPhys is set.
45 * @retval VERR_INVALID_POINTER if the pointer is not within the GC physical memory.
46 *
47 * @param pVM The VM handle.
48 * @param HCPtr The HC pointer to convert.
49 * @param pGCPhys Where to store the GC physical address on success.
50 */
51PGMR3DECL(int) PGMR3DbgHCPtr2GCPhys(PVM pVM, RTHCPTR HCPtr, PRTGCPHYS pGCPhys)
52{
53#ifdef NEW_PHYS_CODE
54 *pGCPhys = NIL_RTGCPHYS;
55 return VERR_NOT_IMPLEMENTED;
56
57#else
58 for (PPGMRAMRANGE pRam = CTXSUFF(pVM->pgm.s.pRamRanges);
59 pRam;
60 pRam = CTXSUFF(pRam->pNext))
61 {
62 if (pRam->fFlags & MM_RAM_FLAGS_DYNAMIC_ALLOC)
63 {
64 for (unsigned iChunk = 0; iChunk < (pRam->cb >> PGM_DYNAMIC_CHUNK_SHIFT); iChunk++)
65 {
66 if (CTXSUFF(pRam->pavHCChunk)[iChunk])
67 {
68 RTHCUINTPTR off = (RTHCUINTPTR)HCPtr - (RTHCUINTPTR)CTXSUFF(pRam->pavHCChunk)[iChunk];
69 if (off < PGM_DYNAMIC_CHUNK_SIZE)
70 {
71 *pGCPhys = pRam->GCPhys + iChunk*PGM_DYNAMIC_CHUNK_SIZE + off;
72 return VINF_SUCCESS;
73 }
74 }
75 }
76 }
77 else if (pRam->pvHC)
78 {
79 RTHCUINTPTR off = (RTHCUINTPTR)HCPtr - (RTHCUINTPTR)pRam->pvHC;
80 if (off < pRam->cb)
81 {
82 *pGCPhys = pRam->GCPhys + off;
83 return VINF_SUCCESS;
84 }
85 }
86 }
87 return VERR_INVALID_POINTER;
88#endif
89}
90
91
92/**
93 * Converts a HC pointer to a GC physical address.
94 *
95 * @returns VBox status code.
96 * @retval VINF_SUCCESS on success, *pHCPhys is set.
97 * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid GC physical page but has no physical backing.
98 * @retval VERR_INVALID_POINTER if the pointer is not within the GC physical memory.
99 *
100 * @param pVM The VM handle.
101 * @param HCPtr The HC pointer to convert.
102 * @param pHCPhys Where to store the HC physical address on success.
103 */
104PGMR3DECL(int) PGMR3DbgHCPtr2HCPhys(PVM pVM, RTHCPTR HCPtr, PRTHCPHYS pHCPhys)
105{
106#ifdef NEW_PHYS_CODE
107 *pHCPhys = NIL_RTHCPHYS;
108 return VERR_NOT_IMPLEMENTED;
109
110#else
111 for (PPGMRAMRANGE pRam = CTXSUFF(pVM->pgm.s.pRamRanges);
112 pRam;
113 pRam = CTXSUFF(pRam->pNext))
114 {
115 if (pRam->fFlags & MM_RAM_FLAGS_DYNAMIC_ALLOC)
116 {
117 for (unsigned iChunk = 0; iChunk < (pRam->cb >> PGM_DYNAMIC_CHUNK_SHIFT); iChunk++)
118 {
119 if (CTXSUFF(pRam->pavHCChunk)[iChunk])
120 {
121 RTHCUINTPTR off = (RTHCUINTPTR)HCPtr - (RTHCUINTPTR)CTXSUFF(pRam->pavHCChunk)[iChunk];
122 if (off < PGM_DYNAMIC_CHUNK_SIZE)
123 {
124 PPGMPAGE pPage = &pRam->aPages[off >> PAGE_SHIFT];
125 if (PGM_PAGE_IS_RESERVED(pPage))
126 return VERR_PGM_PHYS_PAGE_RESERVED;
127 *pHCPhys = PGM_PAGE_GET_HCPHYS(pPage)
128 | (off & PAGE_OFFSET_MASK);
129 return VINF_SUCCESS;
130 }
131 }
132 }
133 }
134 else if (pRam->pvHC)
135 {
136 RTHCUINTPTR off = (RTHCUINTPTR)HCPtr - (RTHCUINTPTR)pRam->pvHC;
137 if (off < pRam->cb)
138 {
139 PPGMPAGE pPage = &pRam->aPages[off >> PAGE_SHIFT];
140 if (PGM_PAGE_IS_RESERVED(pPage))
141 return VERR_PGM_PHYS_PAGE_RESERVED;
142 *pHCPhys = PGM_PAGE_GET_HCPHYS(pPage)
143 | (off & PAGE_OFFSET_MASK);
144 return VINF_SUCCESS;
145 }
146 }
147 }
148 return VERR_INVALID_POINTER;
149#endif
150}
151
152
153/**
154 * Converts a HC physical address to a GC physical address.
155 *
156 * Only for the debugger.
157 *
158 * @returns VBox status code
159 * @retval VINF_SUCCESS on success, *pGCPhys is set.
160 * @retval VERR_INVALID_POINTER if the HC physical address is not within the GC physical memory.
161 *
162 * @param pVM The VM handle.
163 * @param HCPhys The HC physical address to convert.
164 * @param pGCPhys Where to store the GC physical address on success.
165 */
166PGMR3DECL(int) PGMR3DbgHCPhys2GCPhys(PVM pVM, RTHCPHYS HCPhys, PRTGCPHYS pGCPhys)
167{
168 /*
169 * Validate and adjust the input a bit.
170 */
171 if (HCPhys == NIL_RTHCPHYS)
172 return VERR_INVALID_POINTER;
173 unsigned off = HCPhys & PAGE_OFFSET_MASK;
174 HCPhys &= X86_PTE_PAE_PG_MASK;
175 if (HCPhys == 0)
176 return VERR_INVALID_POINTER;
177
178 for (PPGMRAMRANGE pRam = CTXSUFF(pVM->pgm.s.pRamRanges);
179 pRam;
180 pRam = CTXSUFF(pRam->pNext))
181 {
182 uint32_t iPage = pRam->cb >> PAGE_SHIFT;
183 while (iPage-- > 0)
184 if ( PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]) == HCPhys
185 && !PGM_PAGE_IS_RESERVED(&pRam->aPages[iPage]))
186 {
187 *pGCPhys = pRam->GCPhys + (iPage << PAGE_SHIFT) + off;
188 return VINF_SUCCESS;
189 }
190 }
191 return VERR_INVALID_POINTER;
192}
193
194
195/**
196 * Scans a page for a byte string, keeping track of potential
197 * cross page matches.
198 *
199 * @returns true and *poff on match.
200 * false on mismatch.
201 * @param pbPage Pointer to the current page.
202 * @param poff Input: The offset into the page.
203 * Output: The page offset of the match on success.
204 * @param cb The number of bytes to search, starting of *poff.
205 * @param pabNeedle The byte string to search for.
206 * @param cbNeedle The length of the byte string.
207 * @param pabPrev The buffer that keeps track of a partial match that we
208 * bring over from the previous page. This buffer must be
209 * at least cbNeedle - 1 big.
210 * @param pcbPrev Input: The number of partial matching bytes from the previous page.
211 * Output: The number of partial matching bytes from this page.
212 * Initialize to 0 before the first call to this function.
213 */
214static bool pgmR3DbgScanPage(const uint8_t *pbPage, int32_t *poff, uint32_t cb,
215 const uint8_t *pabNeedle, size_t cbNeedle,
216 uint8_t *pabPrev, size_t *pcbPrev)
217{
218 /*
219 * Try complete any partial match from the previous page.
220 */
221 if (*pcbPrev > 0)
222 {
223 size_t cbPrev = *pcbPrev;
224 Assert(!*poff);
225 Assert(cbPrev < cbNeedle);
226 if (!memcmp(pbPage, pabNeedle + cbPrev, cbNeedle - cbPrev))
227 {
228 if (cbNeedle - cbPrev > cb)
229 return false;
230 *poff = -(int32_t)cbPrev;
231 return true;
232 }
233
234 /* check out the remainder of the previous page. */
235 const uint8_t *pb = pabPrev;
236 while (cbPrev-- > 0)
237 {
238 pb = (const uint8_t *)memchr(pb + 1, *pabNeedle, cbPrev);
239 if (!pb)
240 break;
241 cbPrev = *pcbPrev - (pb - pabPrev);
242 if ( !memcmp(pb + 1, &pabNeedle[1], cbPrev - 1)
243 && !memcmp(pbPage, pabNeedle + cbPrev, cbNeedle - cbPrev))
244 {
245 if (cbNeedle - cbPrev > cb)
246 return false;
247 *poff = -(int32_t)cbPrev;
248 return true;
249 }
250 }
251
252 *pcbPrev = 0;
253 }
254
255 /*
256 * Match the body of the page.
257 */
258 const uint8_t *pb = pbPage + *poff;
259 const uint8_t *pbEnd = pb + cb;
260 for (;;)
261 {
262 pb = (const uint8_t *)memchr(pb, *pabNeedle, cb);
263 if (!pb)
264 break;
265 cb = pbEnd - pb;
266 if (cb <= cbNeedle)
267 {
268 /* match? */
269 if (!memcmp(pb + 1, &pabNeedle[1], cbNeedle - 1))
270 {
271 *poff = pb - pbPage;
272 return true;
273 }
274 }
275 else
276 {
277 /* paritial match at the end of the page? */
278 if (!memcmp(pb + 1, &pabNeedle[1], cb - 1))
279 {
280 /* We're copying one byte more that we really need here, but wtf. */
281 memcpy(pabPrev, pb, cb);
282 *pcbPrev = cb;
283 return false;
284 }
285 }
286
287 /* no match, skip a byte ahead. */
288 if (cb <= 1)
289 break;
290 pb++;
291 }
292
293 return false;
294}
295
296
297/**
298 * Scans guest physical memory for a byte string.
299 *
300 * @returns VBox status codes:
301 * @retval VINF_SUCCESS and *pGCPtrHit on success.
302 * @retval VERR_DBGF_MEM_NOT_FOUND if not found.
303 * @retval VERR_INVALID_POINTER if any of the pointer arguments are invalid.
304 * @retval VERR_INVALID_ARGUMENT if any other arguments are invalid.
305 *
306 * @param pVM Pointer to the shared VM structure.
307 * @param GCPhys Where to start searching.
308 * @param cbRange The number of bytes to search.
309 * @param pabNeedle The byte string to search for.
310 * @param cbNeedle The length of the byte string. Max 256 bytes.
311 * @param pGCPhysHit Where to store the address of the first occurence on success.
312 */
313PDMR3DECL(int) PGMR3DbgScanPhysical(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cbRange, const uint8_t *pabNeedle, size_t cbNeedle, PRTGCPHYS pGCPhysHit)
314{
315 /*
316 * Validate and adjust the input a bit.
317 */
318 if (!VALID_PTR(pGCPhysHit))
319 return VERR_INVALID_POINTER;
320 *pGCPhysHit = NIL_RTGCPHYS;
321
322 if ( !VALID_PTR(pabNeedle)
323 || GCPhys == NIL_RTGCPHYS)
324 return VERR_INVALID_POINTER;
325 if (!cbNeedle)
326 return VERR_INVALID_PARAMETER;
327 if (cbRange > MAX_NEEDLE_SIZE)
328 return VERR_INVALID_PARAMETER;
329
330 if (!cbRange)
331 return VERR_DBGF_MEM_NOT_FOUND;
332 if (GCPhys + cbNeedle - 1 < GCPhys)
333 return VERR_DBGF_MEM_NOT_FOUND;
334
335 const RTGCPHYS GCPhysLast = GCPhys + cbRange - 1 >= GCPhys
336 ? GCPhys + cbRange - 1
337 : ~(RTGCPHYS)0;
338
339 /*
340 * Search the memory - ignore MMIO and zero pages, also don't
341 * bother to match across ranges.
342 */
343 for (PPGMRAMRANGE pRam = CTXSUFF(pVM->pgm.s.pRamRanges);
344 pRam;
345 pRam = CTXSUFF(pRam->pNext))
346 {
347 /*
348 * If the search range starts prior to the current ram range record,
349 * adjust the search range and possibly conclude the search.
350 */
351 RTGCPHYS off;
352 if (GCPhys < pRam->GCPhys)
353 {
354 if (GCPhysLast < pRam->GCPhys)
355 break;
356 GCPhys = pRam->GCPhys;
357 off = 0;
358 }
359 else
360 off = GCPhys - pRam->GCPhys;
361 if (off < pRam->cb)
362 {
363 /*
364 * Iterate the relevant pages.
365 */
366 uint8_t abPrev[MAX_NEEDLE_SIZE];
367 size_t cbPrev = 0;
368 const uint32_t cPages = pRam->cb >> PAGE_SHIFT;
369 for (uint32_t iPage = off >> PAGE_SHIFT; iPage < cPages; iPage++)
370 {
371 PPGMPAGE pPage = &pRam->aPages[iPage];
372 if ( !PGM_PAGE_IS_ZERO(pPage)
373 && !PGM_PAGE_IS_MMIO(pPage))
374 {
375 void const *pvPage;
376 PGMPAGEMAPLOCK Lock;
377 int rc = PGMPhysGCPhys2CCPtrReadOnly(pVM, GCPhys & ~(RTGCPHYS)PAGE_OFFSET_MASK, &pvPage, &Lock);
378 if (RT_SUCCESS(rc))
379 {
380 int32_t offPage = (GCPhys & PAGE_OFFSET_MASK);
381 uint32_t cbSearch = (GCPhys ^ GCPhysLast) & ~(RTGCPHYS)PAGE_OFFSET_MASK
382 ? PAGE_SIZE - (uint32_t)offPage
383 : (GCPhysLast & PAGE_OFFSET_MASK) + 1 - (uint32_t)offPage;
384 bool fRc = pgmR3DbgScanPage((uint8_t const *)pvPage, &offPage, cbSearch,
385 pabNeedle, cbNeedle, &abPrev[0], &cbPrev);
386 PGMPhysReleasePageMappingLock(pVM, &Lock);
387 if (fRc)
388 {
389 *pGCPhysHit = (GCPhys & ~(RTGCPHYS)PAGE_OFFSET_MASK) + offPage;
390 return VINF_SUCCESS;
391 }
392 }
393 else
394 cbPrev = 0; /* ignore error. */
395 }
396 else
397 cbPrev = 0;
398
399 /* advance to the the next page. */
400 GCPhys |= PAGE_OFFSET_MASK;
401 if (GCPhys++ >= GCPhysLast)
402 return VERR_DBGF_MEM_NOT_FOUND;
403 }
404 }
405 }
406 return VERR_DBGF_MEM_NOT_FOUND;
407}
408
409
410/**
411 * Scans (guest) virtual memory for a byte string.
412 *
413 * @returns VBox status codes:
414 * @retval VINF_SUCCESS and *pGCPtrHit on success.
415 * @retval VERR_DBGF_MEM_NOT_FOUND if not found.
416 * @retval VERR_INVALID_POINTER if any of the pointer arguments are invalid.
417 * @retval VERR_INVALID_ARGUMENT if any other arguments are invalid.
418 *
419 * @param pVM Pointer to the shared VM structure.
420 * @param GCPtr Where to start searching.
421 * @param cbRange The number of bytes to search. Max 256 bytes.
422 * @param pabNeedle The byte string to search for.
423 * @param cbNeedle The length of the byte string.
424 * @param pGCPtrHit Where to store the address of the first occurence on success.
425 */
426PDMR3DECL(int) PGMR3DbgScanVirtual(PVM pVM, RTGCUINTPTR GCPtr, RTGCUINTPTR cbRange, const uint8_t *pabNeedle, size_t cbNeedle, PRTGCUINTPTR pGCPtrHit)
427{
428 /*
429 * Validate and adjust the input a bit.
430 */
431 if (!VALID_PTR(pGCPtrHit))
432 return VERR_INVALID_POINTER;
433 *pGCPtrHit = 0;
434
435 if (!VALID_PTR(pabNeedle))
436 return VERR_INVALID_POINTER;
437 if (!cbNeedle)
438 return VERR_INVALID_PARAMETER;
439 if (cbRange > MAX_NEEDLE_SIZE)
440 return VERR_INVALID_PARAMETER;
441
442 if (!cbRange)
443 return VERR_DBGF_MEM_NOT_FOUND;
444 if (GCPtr + cbNeedle - 1 < GCPtr)
445 return VERR_DBGF_MEM_NOT_FOUND;
446
447 /*
448 * Search the memory - ignore MMIO, zero and not-present pages.
449 */
450 uint8_t abPrev[MAX_NEEDLE_SIZE];
451 size_t cbPrev = 0;
452 const RTGCUINTPTR GCPtrLast = GCPtr + cbRange - 1 >= GCPtr
453 ? GCPtr + cbRange - 1
454 : ~(RTGCUINTPTR)0;
455 RTGCUINTPTR cPages = (((GCPtrLast - GCPtr) + (GCPtr & PAGE_OFFSET_MASK)) >> PAGE_SHIFT) + 1;
456 while (cPages-- > 0)
457 {
458 uint64_t fFlags;
459 RTGCPHYS GCPhys;
460 int rc = PGMGstGetPage(pVM, GCPtr, &fFlags, &GCPhys);
461 if (RT_SUCCESS(rc))
462 {
463 PPGMPAGE pPage = pgmPhysGetPage(&pVM->pgm.s, GCPhys);
464 if ( pPage
465 && !PGM_PAGE_IS_ZERO(pPage)
466 && !PGM_PAGE_IS_MMIO(pPage))
467 {
468 void const *pvPage;
469 PGMPAGEMAPLOCK Lock;
470 rc = PGMPhysGCPhys2CCPtrReadOnly(pVM, GCPtr & ~(RTGCUINTPTR)PAGE_OFFSET_MASK, &pvPage, &Lock);
471 if (RT_SUCCESS(rc))
472 {
473 int32_t offPage = (GCPtr & PAGE_OFFSET_MASK);
474 uint32_t cbSearch = cPages > 0
475 ? PAGE_SIZE - (uint32_t)offPage
476 : (GCPtrLast & PAGE_OFFSET_MASK) + 1 - (uint32_t)offPage;
477 bool fRc = pgmR3DbgScanPage((uint8_t const *)pvPage, &offPage, cbSearch,
478 pabNeedle, cbNeedle, &abPrev[0], &cbPrev);
479 PGMPhysReleasePageMappingLock(pVM, &Lock);
480 if (fRc)
481 {
482 *pGCPtrHit = (GCPtr & ~(RTGCUINTPTR)PAGE_OFFSET_MASK) + offPage;
483 return VINF_SUCCESS;
484 }
485 }
486 else
487 cbPrev = 0; /* ignore error. */
488 }
489 else
490 cbPrev = 0;
491 }
492 else
493 cbPrev = 0; /* ignore error. */
494 }
495 return VERR_DBGF_MEM_NOT_FOUND;
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