VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMAll/DBGFAllBp.cpp@ 87030

Last change on this file since 87030 was 86751, checked in by vboxsync, 5 years ago

VMM/DBGF: Implement L2 binary search tree node removal and make the all context breakpoint management code compile in ring-3, completely untested though, bugref:9837 [build fix]

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 15.2 KB
Line 
1/* $Id: DBGFAllBp.cpp 86751 2020-10-28 21:12:52Z vboxsync $ */
2/** @file
3 * DBGF - Debugger Facility, All Context breakpoint management part.
4 */
5
6/*
7 * Copyright (C) 2006-2020 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/selm.h>
25#include <VBox/log.h>
26#include "DBGFInternal.h"
27#include <VBox/vmm/vmcc.h>
28#include <VBox/err.h>
29#include <iprt/assert.h>
30
31#ifdef VBOX_WITH_LOTS_OF_DBGF_BPS
32# include "DBGFInline.h"
33#endif
34
35
36#ifdef IN_RC
37# error "You lucky person have the pleasure to implement the raw mode part for this!"
38#endif
39
40
41/*********************************************************************************************************************************
42* Internal Functions *
43*********************************************************************************************************************************/
44
45#ifdef VBOX_WITH_LOTS_OF_DBGF_BPS
46/**
47 * Returns the internal breakpoint state for the given handle.
48 *
49 * @returns Pointer to the internal breakpoint state or NULL if the handle is invalid.
50 * @param pVM The ring-0 VM structure pointer.
51 * @param hBp The breakpoint handle to resolve.
52 * @param ppBpR0 Where to store the pointer to the ring-0 only part of the breakpoint
53 * on success, optional.
54 */
55# ifdef IN_RING0
56DECLINLINE(PDBGFBPINT) dbgfBpGetByHnd(PVMCC pVM, DBGFBP hBp, PDBGFBPINTR0 *ppBpR0)
57# else
58DECLINLINE(PDBGFBPINT) dbgfBpGetByHnd(PVMCC pVM, DBGFBP hBp)
59# endif
60{
61 uint32_t idChunk = DBGF_BP_HND_GET_CHUNK_ID(hBp);
62 uint32_t idxEntry = DBGF_BP_HND_GET_ENTRY(hBp);
63
64 AssertReturn(idChunk < DBGF_BP_CHUNK_COUNT, NULL);
65 AssertReturn(idxEntry < DBGF_BP_COUNT_PER_CHUNK, NULL);
66
67# ifdef IN_RING0
68 PDBGFBPCHUNKR0 pBpChunk = &pVM->dbgfr0.s.aBpChunks[idChunk];
69 AssertPtrReturn(pBpChunk->CTX_SUFF(paBpBaseShared), NULL);
70
71 if (ppBpR0)
72 *ppBpR0 = &pBpChunk->paBpBaseR0Only[idxEntry];
73 return &pBpChunk->CTX_SUFF(paBpBaseShared)[idxEntry];
74# elif defined(IN_RING3)
75 PUVM pUVM = pVM->pUVM;
76 PDBGFBPCHUNKR3 pBpChunk = &pUVM->dbgf.s.aBpChunks[idChunk];
77 AssertPtrReturn(pBpChunk->CTX_SUFF(pBpBase), NULL);
78
79 return &pBpChunk->CTX_SUFF(pBpBase)[idxEntry];
80# else
81# error "Unsupported host context"
82# endif
83}
84
85
86/**
87 * Returns the pointer to the L2 table entry from the given index.
88 *
89 * @returns Current context pointer to the L2 table entry or NULL if the provided index value is invalid.
90 * @param pVM The cross context VM structure.
91 * @param idxL2 The L2 table index to resolve.
92 *
93 * @note The content of the resolved L2 table entry is not validated!.
94 */
95DECLINLINE(PCDBGFBPL2ENTRY) dbgfBpL2GetByIdx(PVMCC pVM, uint32_t idxL2)
96{
97 uint32_t idChunk = DBGF_BP_L2_IDX_GET_CHUNK_ID(idxL2);
98 uint32_t idxEntry = DBGF_BP_L2_IDX_GET_ENTRY(idxL2);
99
100 AssertReturn(idChunk < DBGF_BP_L2_TBL_CHUNK_COUNT, NULL);
101 AssertReturn(idxEntry < DBGF_BP_L2_TBL_ENTRIES_PER_CHUNK, NULL);
102
103# ifdef IN_RING0
104 PDBGFBPL2TBLCHUNKR0 pL2Chunk = &pVM->dbgfr0.s.aBpL2TblChunks[idChunk];
105 AssertPtrReturn(pL2Chunk->CTX_SUFF(paBpL2TblBaseShared), NULL);
106
107 return &pL2Chunk->CTX_SUFF(paBpL2TblBaseShared)[idxEntry];
108# elif defined(IN_RING3)
109 PUVM pUVM = pVM->pUVM;
110 PDBGFBPL2TBLCHUNKR3 pL2Chunk = &pUVM->dbgf.s.aBpL2TblChunks[idChunk];
111 AssertPtrReturn(pL2Chunk->pbmAlloc, NULL);
112 AssertReturn(ASMBitTest(pL2Chunk->pbmAlloc, idxEntry), NULL);
113
114 return &pL2Chunk->CTX_SUFF(pL2Base)[idxEntry];
115# endif
116}
117
118
119/**
120 * Executes the actions associated with the given breakpoint.
121 *
122 * @returns VBox status code.
123 * @param pVM The cross context VM structure.
124 * @param pVCpu The cross context virtual CPU structure.
125 * @param pRegFrame Pointer to the register frame for the trap.
126 * @param hBp The breakpoint handle which hit.
127 * @param pBp The shared breakpoint state.
128 * @param pBpR0 The ring-0 only breakpoint state.
129 */
130# ifdef IN_RING0
131DECLINLINE(int) dbgfBpHit(PVMCC pVM, PVMCPUCC pVCpu, PCPUMCTXCORE pRegFrame,
132 DBGFBP hBp, PDBGFBPINT pBp, PDBGFBPINTR0 pBpR0)
133# else
134DECLINLINE(int) dbgfBpHit(PVMCC pVM, PVMCPUCC pVCpu, PCPUMCTXCORE pRegFrame,
135 DBGFBP hBp, PDBGFBPINT pBp)
136# endif
137{
138 uint64_t cHits = ASMAtomicIncU64(&pBp->Pub.cHits);
139 pVCpu->dbgf.s.hBpActive = hBp;
140
141 /** @todo Owner handling. */
142 RT_NOREF(pVM, pRegFrame);
143#ifdef IN_RING0
144 RT_NOREF(pBpR0);
145#endif
146
147 LogFlow(("dbgfBpHit: hit breakpoint %u at %04x:%RGv cHits=0x%RX64\n",
148 hBp, pRegFrame->cs.Sel, pRegFrame->rip, cHits));
149 return VINF_EM_DBG_BREAKPOINT;
150}
151
152
153/**
154 * Walks the L2 table starting at the given root index searching for the given key.
155 *
156 * @returns VBox status code.
157 * @param pVM The cross context VM structure.
158 * @param pVCpu The cross context virtual CPU structure.
159 * @param pRegFrame Pointer to the register frame for the trap.
160 * @param idxL2Root L2 table index of the table root.
161 * @param GCPtrKey The key to search for.
162 */
163static int dbgfBpL2Walk(PVMCC pVM, PVMCPUCC pVCpu, PCPUMCTXCORE pRegFrame,
164 uint32_t idxL2Root, RTGCUINTPTR GCPtrKey)
165{
166 /** @todo We don't use the depth right now but abort the walking after a fixed amount of levels. */
167 uint8_t iDepth = 32;
168 PCDBGFBPL2ENTRY pL2Entry = dbgfBpL2GetByIdx(pVM, idxL2Root);
169
170 while (RT_LIKELY( iDepth-- > 0
171 && pL2Entry))
172 {
173 /* Make a copy of the entry before verification. */
174 DBGFBPL2ENTRY L2Entry;
175 L2Entry.u64GCPtrKeyAndBpHnd1 = ASMAtomicReadU64((volatile uint64_t *)&pL2Entry->u64GCPtrKeyAndBpHnd1);
176 L2Entry.u64LeftRightIdxDepthBpHnd2 = ASMAtomicReadU64((volatile uint64_t *)&pL2Entry->u64LeftRightIdxDepthBpHnd2);
177
178 RTGCUINTPTR GCPtrL2Entry = DBGF_BP_L2_ENTRY_GET_GCPTR(L2Entry.u64GCPtrKeyAndBpHnd1);
179 if (GCPtrKey == GCPtrL2Entry)
180 {
181 DBGFBP hBp = DBGF_BP_L2_ENTRY_GET_BP_HND(L2Entry.u64GCPtrKeyAndBpHnd1, L2Entry.u64LeftRightIdxDepthBpHnd2);
182
183 /* Query the internal breakpoint state from the handle. */
184# ifdef IN_RING0
185 PDBGFBPINTR0 pBpR0 = NULL;
186 PDBGFBPINT pBp = dbgfBpGetByHnd(pVM, hBp, &pBpR0);
187# else
188 PDBGFBPINT pBp = dbgfBpGetByHnd(pVM, hBp);
189# endif
190 if ( pBp
191 && DBGF_BP_PUB_GET_TYPE(pBp->Pub.fFlagsAndType) == DBGFBPTYPE_INT3)
192 return dbgfBpHit(pVM, pVCpu, pRegFrame, hBp, pBp
193# ifdef IN_RING0
194 , pBpR0
195# endif
196 );
197
198 /* The entry got corrupted, just abort. */
199 return VERR_DBGF_BP_L2_LOOKUP_FAILED;
200 }
201
202 /* Not found, get to the next level. */
203 uint32_t idxL2Next = (GCPtrKey < GCPtrL2Entry)
204 ? DBGF_BP_L2_ENTRY_GET_IDX_LEFT(L2Entry.u64LeftRightIdxDepthBpHnd2)
205 : DBGF_BP_L2_ENTRY_GET_IDX_RIGHT(L2Entry.u64LeftRightIdxDepthBpHnd2);
206 /* It is genuine guest trap or we hit some assertion if we are at the end. */
207 if (idxL2Next == DBGF_BP_L2_ENTRY_IDX_END)
208 return VINF_EM_RAW_GUEST_TRAP;
209
210 pL2Entry = dbgfBpL2GetByIdx(pVM, idxL2Next);
211 }
212
213 return VERR_DBGF_BP_L2_LOOKUP_FAILED;
214}
215#endif /* !VBOX_WITH_LOTS_OF_DBGF_BPS */
216
217
218/**
219 * \#DB (Debug event) handler.
220 *
221 * @returns VBox status code.
222 * VINF_SUCCESS means we completely handled this trap,
223 * other codes are passed execution to host context.
224 *
225 * @param pVM The cross context VM structure.
226 * @param pVCpu The cross context virtual CPU structure.
227 * @param pRegFrame Pointer to the register frame for the trap.
228 * @param uDr6 The DR6 hypervisor register value.
229 * @param fAltStepping Alternative stepping indicator.
230 */
231VMM_INT_DECL(int) DBGFTrap01Handler(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame, RTGCUINTREG uDr6, bool fAltStepping)
232{
233 /** @todo Intel docs say that X86_DR6_BS has the highest priority... */
234 RT_NOREF(pRegFrame);
235
236 /*
237 * A breakpoint?
238 */
239 AssertCompile(X86_DR6_B0 == 1 && X86_DR6_B1 == 2 && X86_DR6_B2 == 4 && X86_DR6_B3 == 8);
240 if ( (uDr6 & (X86_DR6_B0 | X86_DR6_B1 | X86_DR6_B2 | X86_DR6_B3))
241 && pVM->dbgf.s.cEnabledHwBreakpoints > 0)
242 {
243 for (unsigned iBp = 0; iBp < RT_ELEMENTS(pVM->dbgf.s.aHwBreakpoints); iBp++)
244 {
245#ifndef VBOX_WITH_LOTS_OF_DBGF_BPS
246 if ( ((uint32_t)uDr6 & RT_BIT_32(iBp))
247 && pVM->dbgf.s.aHwBreakpoints[iBp].enmType == DBGFBPTYPE_REG)
248 {
249 pVCpu->dbgf.s.iActiveBp = pVM->dbgf.s.aHwBreakpoints[iBp].iBp;
250 pVCpu->dbgf.s.fSingleSteppingRaw = false;
251 LogFlow(("DBGFRZTrap03Handler: hit hw breakpoint %d at %04x:%RGv\n",
252 pVM->dbgf.s.aHwBreakpoints[iBp].iBp, pRegFrame->cs.Sel, pRegFrame->rip));
253
254 return VINF_EM_DBG_BREAKPOINT;
255 }
256#else
257 if ( ((uint32_t)uDr6 & RT_BIT_32(iBp))
258 && pVM->dbgf.s.aHwBreakpoints[iBp].hBp != NIL_DBGFBP)
259 {
260 pVCpu->dbgf.s.hBpActive = pVM->dbgf.s.aHwBreakpoints[iBp].hBp;
261 pVCpu->dbgf.s.fSingleSteppingRaw = false;
262 LogFlow(("DBGFRZTrap03Handler: hit hw breakpoint %x at %04x:%RGv\n",
263 pVM->dbgf.s.aHwBreakpoints[iBp].hBp, pRegFrame->cs.Sel, pRegFrame->rip));
264
265 return VINF_EM_DBG_BREAKPOINT;
266 }
267#endif
268 }
269 }
270
271 /*
272 * Single step?
273 * Are we single stepping or is it the guest?
274 */
275 if ( (uDr6 & X86_DR6_BS)
276 && (pVCpu->dbgf.s.fSingleSteppingRaw || fAltStepping))
277 {
278 pVCpu->dbgf.s.fSingleSteppingRaw = false;
279 LogFlow(("DBGFRZTrap01Handler: single step at %04x:%RGv\n", pRegFrame->cs.Sel, pRegFrame->rip));
280 return VINF_EM_DBG_STEPPED;
281 }
282
283 LogFlow(("DBGFRZTrap01Handler: guest debug event %#x at %04x:%RGv!\n", (uint32_t)uDr6, pRegFrame->cs.Sel, pRegFrame->rip));
284 return VINF_EM_RAW_GUEST_TRAP;
285}
286
287
288/**
289 * \#BP (Breakpoint) handler.
290 *
291 * @returns VBox status code.
292 * VINF_SUCCESS means we completely handled this trap,
293 * other codes are passed execution to host context.
294 *
295 * @param pVM The cross context VM structure.
296 * @param pVCpu The cross context virtual CPU structure.
297 * @param pRegFrame Pointer to the register frame for the trap.
298 */
299VMM_INT_DECL(int) DBGFTrap03Handler(PVMCC pVM, PVMCPUCC pVCpu, PCPUMCTXCORE pRegFrame)
300{
301#ifndef VBOX_WITH_LOTS_OF_DBGF_BPS
302 /*
303 * Get the trap address and look it up in the breakpoint table.
304 * Don't bother if we don't have any breakpoints.
305 */
306 unsigned cToSearch = pVM->dbgf.s.Int3.cToSearch;
307 if (cToSearch > 0)
308 {
309 RTGCPTR pPc;
310 int rc = SELMValidateAndConvertCSAddr(pVCpu, pRegFrame->eflags, pRegFrame->ss.Sel, pRegFrame->cs.Sel, &pRegFrame->cs,
311 pRegFrame->rip /* no -1 in R0 */,
312 &pPc);
313 AssertRCReturn(rc, rc);
314
315 unsigned iBp = pVM->dbgf.s.Int3.iStartSearch;
316 while (cToSearch-- > 0)
317 {
318 if ( pVM->dbgf.s.aBreakpoints[iBp].u.GCPtr == (RTGCUINTPTR)pPc
319 && pVM->dbgf.s.aBreakpoints[iBp].enmType == DBGFBPTYPE_INT3)
320 {
321 pVM->dbgf.s.aBreakpoints[iBp].cHits++;
322 pVCpu->dbgf.s.iActiveBp = pVM->dbgf.s.aBreakpoints[iBp].iBp;
323
324 LogFlow(("DBGFRZTrap03Handler: hit breakpoint %d at %RGv (%04x:%RGv) cHits=0x%RX64\n",
325 pVM->dbgf.s.aBreakpoints[iBp].iBp, pPc, pRegFrame->cs.Sel, pRegFrame->rip,
326 pVM->dbgf.s.aBreakpoints[iBp].cHits));
327 return VINF_EM_DBG_BREAKPOINT;
328 }
329 iBp++;
330 }
331 }
332#else
333# if defined(IN_RING0)
334 uint32_t volatile *paBpLocL1 = pVM->dbgfr0.s.CTX_SUFF(paBpLocL1);
335# elif defined(IN_RING3)
336 PUVM pUVM = pVM->pUVM;
337 uint32_t volatile *paBpLocL1 = pUVM->dbgf.s.CTX_SUFF(paBpLocL1);
338# else
339# error "Unsupported host context"
340# endif
341 if (paBpLocL1)
342 {
343 RTGCPTR GCPtrBp;
344 int rc = SELMValidateAndConvertCSAddr(pVCpu, pRegFrame->eflags, pRegFrame->ss.Sel, pRegFrame->cs.Sel, &pRegFrame->cs,
345 pRegFrame->rip /* no -1 in R0 */,
346 &GCPtrBp);
347 AssertRCReturn(rc, rc);
348
349 const uint16_t idxL1 = DBGF_BP_INT3_L1_IDX_EXTRACT_FROM_ADDR(GCPtrBp);
350 const uint32_t u32L1Entry = ASMAtomicReadU32(&paBpLocL1[idxL1]);
351
352 LogFlowFunc(("GCPtrBp=%RGv idxL1=%u u32L1Entry=%#x\n", GCPtrBp, idxL1, u32L1Entry));
353 rc = VINF_EM_RAW_GUEST_TRAP;
354 if (u32L1Entry != DBGF_BP_INT3_L1_ENTRY_TYPE_NULL)
355 {
356 uint8_t u8Type = DBGF_BP_INT3_L1_ENTRY_GET_TYPE(u32L1Entry);
357 if (u8Type == DBGF_BP_INT3_L1_ENTRY_TYPE_BP_HND)
358 {
359 DBGFBP hBp = DBGF_BP_INT3_L1_ENTRY_GET_BP_HND(u32L1Entry);
360
361 /* Query the internal breakpoint state from the handle. */
362#ifdef IN_RING0
363 PDBGFBPINTR0 pBpR0 = NULL;
364#endif
365 PDBGFBPINT pBp = dbgfBpGetByHnd(pVM, hBp
366#ifdef IN_RING0
367 , &pBpR0
368#endif
369 );
370 if ( pBp
371 && DBGF_BP_PUB_GET_TYPE(pBp->Pub.fFlagsAndType) == DBGFBPTYPE_INT3)
372 {
373 if (pBp->Pub.u.Int3.GCPtr == (RTGCUINTPTR)GCPtrBp)
374 rc = dbgfBpHit(pVM, pVCpu, pRegFrame, hBp, pBp
375#ifdef IN_RING0
376 , pBpR0
377#endif
378 );
379 /* else: Genuine guest trap. */
380 }
381 else /* Invalid breakpoint handle or not an int3 breakpoint. */
382 rc = VERR_DBGF_BP_L1_LOOKUP_FAILED;
383 }
384 else if (u8Type == DBGF_BP_INT3_L1_ENTRY_TYPE_L2_IDX)
385 rc = dbgfBpL2Walk(pVM, pVCpu, pRegFrame, DBGF_BP_INT3_L1_ENTRY_GET_L2_IDX(u32L1Entry),
386 DBGF_BP_INT3_L2_KEY_EXTRACT_FROM_ADDR((RTGCUINTPTR)GCPtrBp));
387 else /* Some invalid type. */
388 rc = VERR_DBGF_BP_L1_LOOKUP_FAILED;
389 }
390 /* else: Genuine guest trap. */
391
392 return rc;
393 }
394#endif /* !VBOX_WITH_LOTS_OF_DBGF_BPS */
395
396 return VINF_EM_RAW_GUEST_TRAP;
397}
398
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette