VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMAll/DBGFAll.cpp@ 97698

Last change on this file since 97698 was 97693, checked in by vboxsync, 2 years ago

VMM/DBGF: Added a DBGFBpCheckInstruction function for checking for instruction breakpoint. bugref:9898

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 18.4 KB
Line 
1/* $Id: DBGFAll.cpp 97693 2022-11-28 22:03:17Z vboxsync $ */
2/** @file
3 * DBGF - Debugger Facility, All Context Code.
4 */
5
6/*
7 * Copyright (C) 2006-2022 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28
29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
32#define LOG_GROUP LOG_GROUP_DBGF
33#define VMCPU_INCL_CPUM_GST_CTX
34#include <VBox/vmm/dbgf.h>
35#include "DBGFInternal.h"
36#include <VBox/vmm/cpum.h>
37#include <VBox/vmm/vmcc.h>
38#include <VBox/err.h>
39#include <iprt/assert.h>
40#include <iprt/asm.h>
41#include <iprt/stdarg.h>
42
43
44/*
45 * Check the read-only VM members.
46 */
47AssertCompileMembersSameSizeAndOffset(VM, dbgf.s.bmSoftIntBreakpoints, VM, dbgf.ro.bmSoftIntBreakpoints);
48AssertCompileMembersSameSizeAndOffset(VM, dbgf.s.bmHardIntBreakpoints, VM, dbgf.ro.bmHardIntBreakpoints);
49AssertCompileMembersSameSizeAndOffset(VM, dbgf.s.bmSelectedEvents, VM, dbgf.ro.bmSelectedEvents);
50AssertCompileMembersSameSizeAndOffset(VM, dbgf.s.cHardIntBreakpoints, VM, dbgf.ro.cHardIntBreakpoints);
51AssertCompileMembersSameSizeAndOffset(VM, dbgf.s.cSoftIntBreakpoints, VM, dbgf.ro.cSoftIntBreakpoints);
52AssertCompileMembersSameSizeAndOffset(VM, dbgf.s.cSelectedEvents, VM, dbgf.ro.cSelectedEvents);
53
54
55/**
56 * Gets the hardware breakpoint configuration as DR7.
57 *
58 * @returns DR7 from the DBGF point of view.
59 * @param pVM The cross context VM structure.
60 */
61VMM_INT_DECL(RTGCUINTREG) DBGFBpGetDR7(PVM pVM)
62{
63 RTGCUINTREG uDr7 = X86_DR7_GD | X86_DR7_GE | X86_DR7_LE | X86_DR7_RA1_MASK;
64 for (uint32_t i = 0; i < RT_ELEMENTS(pVM->dbgf.s.aHwBreakpoints); i++)
65 {
66 if ( pVM->dbgf.s.aHwBreakpoints[i].fEnabled
67 && pVM->dbgf.s.aHwBreakpoints[i].hBp != NIL_DBGFBP)
68 {
69 static const uint8_t s_au8Sizes[8] =
70 {
71 X86_DR7_LEN_BYTE, X86_DR7_LEN_BYTE, X86_DR7_LEN_WORD, X86_DR7_LEN_BYTE,
72 X86_DR7_LEN_DWORD,X86_DR7_LEN_BYTE, X86_DR7_LEN_BYTE, X86_DR7_LEN_QWORD
73 };
74 uDr7 |= X86_DR7_G(i)
75 | X86_DR7_RW(i, pVM->dbgf.s.aHwBreakpoints[i].fType)
76 | X86_DR7_LEN(i, s_au8Sizes[pVM->dbgf.s.aHwBreakpoints[i].cb]);
77 }
78 }
79 return uDr7;
80}
81
82
83/**
84 * Gets the address of the hardware breakpoint number 0.
85 *
86 * @returns DR0 from the DBGF point of view.
87 * @param pVM The cross context VM structure.
88 */
89VMM_INT_DECL(RTGCUINTREG) DBGFBpGetDR0(PVM pVM)
90{
91 return pVM->dbgf.s.aHwBreakpoints[0].GCPtr;
92}
93
94
95/**
96 * Gets the address of the hardware breakpoint number 1.
97 *
98 * @returns DR1 from the DBGF point of view.
99 * @param pVM The cross context VM structure.
100 */
101VMM_INT_DECL(RTGCUINTREG) DBGFBpGetDR1(PVM pVM)
102{
103 return pVM->dbgf.s.aHwBreakpoints[1].GCPtr;
104}
105
106
107/**
108 * Gets the address of the hardware breakpoint number 2.
109 *
110 * @returns DR2 from the DBGF point of view.
111 * @param pVM The cross context VM structure.
112 */
113VMM_INT_DECL(RTGCUINTREG) DBGFBpGetDR2(PVM pVM)
114{
115 return pVM->dbgf.s.aHwBreakpoints[2].GCPtr;
116}
117
118
119/**
120 * Gets the address of the hardware breakpoint number 3.
121 *
122 * @returns DR3 from the DBGF point of view.
123 * @param pVM The cross context VM structure.
124 */
125VMM_INT_DECL(RTGCUINTREG) DBGFBpGetDR3(PVM pVM)
126{
127 return pVM->dbgf.s.aHwBreakpoints[3].GCPtr;
128}
129
130
131/**
132 * Checks if any of the hardware breakpoints are armed.
133 *
134 * @returns true if armed, false if not.
135 * @param pVM The cross context VM structure.
136 * @remarks Don't call this from CPUMRecalcHyperDRx!
137 */
138VMM_INT_DECL(bool) DBGFBpIsHwArmed(PVM pVM)
139{
140 return pVM->dbgf.s.cEnabledHwBreakpoints > 0;
141}
142
143
144/**
145 * Checks if any of the hardware I/O breakpoints are armed.
146 *
147 * @returns true if armed, false if not.
148 * @param pVM The cross context VM structure.
149 * @remarks Don't call this from CPUMRecalcHyperDRx!
150 */
151VMM_INT_DECL(bool) DBGFBpIsHwIoArmed(PVM pVM)
152{
153 return pVM->dbgf.s.cEnabledHwIoBreakpoints > 0;
154}
155
156
157/**
158 * Checks if any INT3 breakpoints are armed.
159 *
160 * @returns true if armed, false if not.
161 * @param pVM The cross context VM structure.
162 * @remarks Don't call this from CPUMRecalcHyperDRx!
163 */
164VMM_INT_DECL(bool) DBGFBpIsInt3Armed(PVM pVM)
165{
166 /** @todo There was a todo here and returning false when I (bird) removed
167 * VBOX_WITH_LOTS_OF_DBGF_BPS, so this might not be correct. */
168 return pVM->dbgf.s.cEnabledInt3Breakpoints > 0;
169}
170
171
172/**
173 * Checks instruction boundrary for guest or hypervisor hardware breakpoints.
174 *
175 * @returns Strict VBox status code. May return DRx register import errors in
176 * addition to the ones detailed.
177 * @retval VINF_SUCCESS no breakpoint.
178 * @retval VINF_EM_DBG_BREAKPOINT hypervisor breakpoint triggered.
179 * @retval VINF_EM_RAW_GUEST_TRAP caller must trigger \#DB trap, DR6 and DR7
180 * have been updated appropriately.
181 *
182 * @param pVM The cross context VM structure.
183 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
184 * @param GCPtrPC The unsegmented PC address.
185 */
186VMM_INT_DECL(VBOXSTRICTRC) DBGFBpCheckInstruction(PVMCC pVM, PVMCPUCC pVCpu, RTGCPTR GCPtrPC)
187{
188 CPUM_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_DR7);
189
190 /*
191 * Check hyper breakpoints first as the VMM debugger has priority over
192 * the guest.
193 */
194 if (pVM->dbgf.s.cEnabledHwIoBreakpoints > 0)
195 for (unsigned iBp = 0; iBp < RT_ELEMENTS(pVM->dbgf.s.aHwBreakpoints); iBp++)
196 {
197 if ( pVM->dbgf.s.aHwBreakpoints[iBp].GCPtr != GCPtrPC
198 || pVM->dbgf.s.aHwBreakpoints[iBp].fType != X86_DR7_RW_EO
199 || pVM->dbgf.s.aHwBreakpoints[iBp].cb != 1
200 || !pVM->dbgf.s.aHwBreakpoints[iBp].fEnabled
201 || pVM->dbgf.s.aHwBreakpoints[iBp].hBp == NIL_DBGFBP)
202 { /*likely*/ }
203 else
204 {
205 /* (See also DBGFRZTrap01Handler.) */
206 pVCpu->dbgf.s.hBpActive = pVM->dbgf.s.aHwBreakpoints[iBp].hBp;
207 pVCpu->dbgf.s.fSingleSteppingRaw = false;
208
209 LogFlow(("DBGFBpCheckInstruction: hit hw breakpoint %u at %04x:%RGv (%RGv)\n",
210 iBp, pVCpu->cpum.GstCtx.cs.Sel, pVCpu->cpum.GstCtx.rip, GCPtrPC));
211 return VINF_EM_DBG_BREAKPOINT;
212 }
213 }
214
215 /*
216 * Check the guest.
217 */
218 uint32_t const fDr7 = (uint32_t)pVCpu->cpum.GstCtx.dr[7];
219 if (X86_DR7_ANY_EO_ENABLED(fDr7))
220 {
221 /*
222 * The CPU (10980XE & 6700K at least) will set the DR6.BPx bits for any
223 * DRx that matches the current PC and is configured as an execution
224 * breakpoint (RWx=EO, LENx=1byte). They don't have to be enabled,
225 * however one that is enabled must match for the #DB to be raised and
226 * DR6 to be modified, of course.
227 */
228 CPUM_IMPORT_EXTRN_RET(pVCpu, CPUMCTX_EXTRN_DR0_DR3);
229 uint32_t fMatched = 0;
230 uint32_t fEnabled = 0;
231 for (unsigned iBp = 0, iBpMask = 1; iBp < 4; iBp++, iBpMask <<= 1)
232 if (X86_DR7_IS_EO_CFG(fDr7, iBp))
233 {
234 if (fDr7 & X86_DR7_L_G(iBp))
235 fEnabled |= iBpMask;
236 if (pVCpu->cpum.GstCtx.dr[iBp] == GCPtrPC)
237 fMatched |= iBpMask;
238 }
239 if (!(fEnabled & fMatched))
240 { /*likely*/ }
241 else if (fEnabled & fMatched)
242 {
243 /*
244 * Update DR6 and DR7.
245 *
246 * See "AMD64 Architecture Programmer's Manual Volume 2", chapter
247 * 13.1.1.3 for details on DR6 bits. The basics is that the B0..B3
248 * bits are always cleared while the others must be cleared by software.
249 *
250 * The following sub chapters says the GD bit is always cleared when
251 * generating a #DB so the handler can safely access the debug registers.
252 */
253 CPUM_IMPORT_EXTRN_RET(pVCpu, CPUMCTX_EXTRN_DR6);
254 pVCpu->cpum.GstCtx.dr[6] &= ~X86_DR6_B_MASK;
255 pVCpu->cpum.GstCtx.dr[6] |= fMatched; /* All matched */
256 pVCpu->cpum.GstCtx.dr[7] &= ~X86_DR7_GD;
257 LogFlow(("DBGFBpCheckInstruction: hit hw breakpoints %#x at %04x:%RGv (%RGv)\n",
258 fMatched, pVCpu->cpum.GstCtx.cs.Sel, pVCpu->cpum.GstCtx.rip, GCPtrPC));
259 return VINF_EM_RAW_GUEST_TRAP;
260 }
261 }
262 return VINF_SUCCESS;
263}
264
265
266/**
267 * Checks I/O access for guest or hypervisor hardware breakpoints.
268 *
269 * @returns Strict VBox status code
270 * @retval VINF_SUCCESS no breakpoint.
271 * @retval VINF_EM_DBG_BREAKPOINT hypervisor breakpoint triggered.
272 * @retval VINF_EM_RAW_GUEST_TRAP guest breakpoint triggered, DR6 and DR7 have
273 * been updated appropriately.
274 *
275 * @param pVM The cross context VM structure.
276 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
277 * @param pCtx The CPU context for the calling EMT.
278 * @param uIoPort The I/O port being accessed.
279 * @param cbValue The size/width of the access, in bytes.
280 */
281VMM_INT_DECL(VBOXSTRICTRC) DBGFBpCheckIo(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx, RTIOPORT uIoPort, uint8_t cbValue)
282{
283 uint32_t const uIoPortFirst = uIoPort;
284 uint32_t const uIoPortLast = uIoPortFirst + cbValue - 1;
285
286 /*
287 * Check hyper breakpoints first as the VMM debugger has priority over
288 * the guest.
289 */
290 if (pVM->dbgf.s.cEnabledHwIoBreakpoints > 0)
291 {
292 for (unsigned iBp = 0; iBp < RT_ELEMENTS(pVM->dbgf.s.aHwBreakpoints); iBp++)
293 {
294 if ( pVM->dbgf.s.aHwBreakpoints[iBp].fType == X86_DR7_RW_IO
295 && pVM->dbgf.s.aHwBreakpoints[iBp].fEnabled
296 && pVM->dbgf.s.aHwBreakpoints[iBp].hBp != NIL_DBGFBP)
297 {
298 uint8_t cbReg = pVM->dbgf.s.aHwBreakpoints[iBp].cb; Assert(RT_IS_POWER_OF_TWO(cbReg));
299 uint64_t uDrXFirst = pVM->dbgf.s.aHwBreakpoints[iBp].GCPtr & ~(uint64_t)(cbReg - 1);
300 uint64_t uDrXLast = uDrXFirst + cbReg - 1;
301 if (uDrXFirst <= uIoPortLast && uDrXLast >= uIoPortFirst)
302 {
303 /* (See also DBGFRZTrap01Handler.) */
304 pVCpu->dbgf.s.hBpActive = pVM->dbgf.s.aHwBreakpoints[iBp].hBp;
305 pVCpu->dbgf.s.fSingleSteppingRaw = false;
306
307 LogFlow(("DBGFBpCheckIo: hit hw breakpoint %d at %04x:%RGv (iop %#x)\n",
308 iBp, pCtx->cs.Sel, pCtx->rip, uIoPort));
309 return VINF_EM_DBG_BREAKPOINT;
310 }
311 }
312 }
313 }
314
315 /*
316 * Check the guest.
317 */
318 uint32_t const uDr7 = pCtx->dr[7];
319 if ( (uDr7 & X86_DR7_ENABLED_MASK)
320 && X86_DR7_ANY_RW_IO(uDr7)
321 && (pCtx->cr4 & X86_CR4_DE) )
322 {
323 for (unsigned iBp = 0; iBp < 4; iBp++)
324 {
325 if ( (uDr7 & X86_DR7_L_G(iBp))
326 && X86_DR7_GET_RW(uDr7, iBp) == X86_DR7_RW_IO)
327 {
328 /* ASSUME the breakpoint and the I/O width qualifier uses the same encoding (1 2 x 4). */
329 static uint8_t const s_abInvAlign[4] = { 0, 1, 7, 3 };
330 uint8_t cbInvAlign = s_abInvAlign[X86_DR7_GET_LEN(uDr7, iBp)];
331 uint64_t uDrXFirst = pCtx->dr[iBp] & ~(uint64_t)cbInvAlign;
332 uint64_t uDrXLast = uDrXFirst + cbInvAlign;
333
334 if (uDrXFirst <= uIoPortLast && uDrXLast >= uIoPortFirst)
335 {
336 /*
337 * Update DR6 and DR7.
338 *
339 * See "AMD64 Architecture Programmer's Manual Volume 2",
340 * chapter 13.1.1.3 for details on DR6 bits. The basics is
341 * that the B0..B3 bits are always cleared while the others
342 * must be cleared by software.
343 *
344 * The following sub chapters says the GD bit is always
345 * cleared when generating a #DB so the handler can safely
346 * access the debug registers.
347 */
348 pCtx->dr[6] &= ~X86_DR6_B_MASK;
349 pCtx->dr[6] |= X86_DR6_B(iBp);
350 pCtx->dr[7] &= ~X86_DR7_GD;
351 LogFlow(("DBGFBpCheckIo: hit hw breakpoint %d at %04x:%RGv (iop %#x)\n",
352 iBp, pCtx->cs.Sel, pCtx->rip, uIoPort));
353 return VINF_EM_RAW_GUEST_TRAP;
354 }
355 }
356 }
357 }
358 return VINF_SUCCESS;
359}
360
361
362/**
363 * Returns the single stepping state for a virtual CPU.
364 *
365 * @returns stepping (true) or not (false).
366 *
367 * @param pVCpu The cross context virtual CPU structure.
368 */
369VMM_INT_DECL(bool) DBGFIsStepping(PVMCPU pVCpu)
370{
371 return pVCpu->dbgf.s.fSingleSteppingRaw;
372}
373
374
375/**
376 * Checks if the specified generic event is enabled or not.
377 *
378 * @returns true / false.
379 * @param pVM The cross context VM structure.
380 * @param enmEvent The generic event being raised.
381 * @param uEventArg The argument of that event.
382 */
383DECLINLINE(bool) dbgfEventIsGenericWithArgEnabled(PVM pVM, DBGFEVENTTYPE enmEvent, uint64_t uEventArg)
384{
385 if (DBGF_IS_EVENT_ENABLED(pVM, enmEvent))
386 {
387 switch (enmEvent)
388 {
389 case DBGFEVENT_INTERRUPT_HARDWARE:
390 AssertReturn(uEventArg < 256, false);
391 return ASMBitTest(pVM->dbgf.s.bmHardIntBreakpoints, (uint32_t)uEventArg);
392
393 case DBGFEVENT_INTERRUPT_SOFTWARE:
394 AssertReturn(uEventArg < 256, false);
395 return ASMBitTest(pVM->dbgf.s.bmSoftIntBreakpoints, (uint32_t)uEventArg);
396
397 default:
398 return true;
399
400 }
401 }
402 return false;
403}
404
405
406/**
407 * Raises a generic debug event if enabled and not being ignored.
408 *
409 * @returns Strict VBox status code.
410 * @retval VINF_EM_DBG_EVENT if the event was raised and the caller should
411 * return ASAP to the debugger (via EM). We set VMCPU_FF_DBGF so, it
412 * is okay not to pass this along in some situations.
413 * @retval VINF_SUCCESS if the event was disabled or ignored.
414 *
415 * @param pVM The cross context VM structure.
416 * @param pVCpu The cross context virtual CPU structure.
417 * @param enmEvent The generic event being raised.
418 * @param enmCtx The context in which this event is being raised.
419 * @param cArgs Number of arguments (0 - 6).
420 * @param ... Event arguments.
421 *
422 * @thread EMT(pVCpu)
423 */
424VMM_INT_DECL(VBOXSTRICTRC) DBGFEventGenericWithArgs(PVM pVM, PVMCPU pVCpu, DBGFEVENTTYPE enmEvent, DBGFEVENTCTX enmCtx,
425 unsigned cArgs, ...)
426{
427 Assert(cArgs < RT_ELEMENTS(pVCpu->dbgf.s.aEvents[0].Event.u.Generic.auArgs));
428
429 /*
430 * Is it enabled.
431 */
432 va_list va;
433 va_start(va, cArgs);
434 uint64_t uEventArg0 = cArgs ? va_arg(va, uint64_t) : 0;
435 if (dbgfEventIsGenericWithArgEnabled(pVM, enmEvent, uEventArg0))
436 {
437 /*
438 * Any events on the stack. Should the incoming event be ignored?
439 */
440 uint64_t const rip = CPUMGetGuestRIP(pVCpu);
441 uint32_t i = pVCpu->dbgf.s.cEvents;
442 if (i > 0)
443 {
444 while (i-- > 0)
445 {
446 if ( pVCpu->dbgf.s.aEvents[i].Event.enmType == enmEvent
447 && pVCpu->dbgf.s.aEvents[i].enmState == DBGFEVENTSTATE_IGNORE
448 && pVCpu->dbgf.s.aEvents[i].rip == rip)
449 {
450 pVCpu->dbgf.s.aEvents[i].enmState = DBGFEVENTSTATE_RESTORABLE;
451 va_end(va);
452 return VINF_SUCCESS;
453 }
454 Assert(pVCpu->dbgf.s.aEvents[i].enmState != DBGFEVENTSTATE_CURRENT);
455 }
456
457 /*
458 * Trim the event stack.
459 */
460 i = pVCpu->dbgf.s.cEvents;
461 while (i-- > 0)
462 {
463 if ( pVCpu->dbgf.s.aEvents[i].rip == rip
464 && ( pVCpu->dbgf.s.aEvents[i].enmState == DBGFEVENTSTATE_RESTORABLE
465 || pVCpu->dbgf.s.aEvents[i].enmState == DBGFEVENTSTATE_IGNORE) )
466 pVCpu->dbgf.s.aEvents[i].enmState = DBGFEVENTSTATE_IGNORE;
467 else
468 {
469 if (i + 1 != pVCpu->dbgf.s.cEvents)
470 memmove(&pVCpu->dbgf.s.aEvents[i], &pVCpu->dbgf.s.aEvents[i + 1],
471 (pVCpu->dbgf.s.cEvents - i) * sizeof(pVCpu->dbgf.s.aEvents));
472 pVCpu->dbgf.s.cEvents--;
473 }
474 }
475
476 i = pVCpu->dbgf.s.cEvents;
477 AssertStmt(i < RT_ELEMENTS(pVCpu->dbgf.s.aEvents), i = RT_ELEMENTS(pVCpu->dbgf.s.aEvents) - 1);
478 }
479
480 /*
481 * Push the event.
482 */
483 pVCpu->dbgf.s.aEvents[i].enmState = DBGFEVENTSTATE_CURRENT;
484 pVCpu->dbgf.s.aEvents[i].rip = rip;
485 pVCpu->dbgf.s.aEvents[i].Event.enmType = enmEvent;
486 pVCpu->dbgf.s.aEvents[i].Event.enmCtx = enmCtx;
487 pVCpu->dbgf.s.aEvents[i].Event.u.Generic.cArgs = cArgs;
488 pVCpu->dbgf.s.aEvents[i].Event.u.Generic.auArgs[0] = uEventArg0;
489 if (cArgs > 1)
490 {
491 AssertStmt(cArgs < RT_ELEMENTS(pVCpu->dbgf.s.aEvents[i].Event.u.Generic.auArgs),
492 cArgs = RT_ELEMENTS(pVCpu->dbgf.s.aEvents[i].Event.u.Generic.auArgs));
493 for (unsigned iArg = 1; iArg < cArgs; iArg++)
494 pVCpu->dbgf.s.aEvents[i].Event.u.Generic.auArgs[iArg] = va_arg(va, uint64_t);
495 }
496 pVCpu->dbgf.s.cEvents = i + 1;
497
498 VMCPU_FF_SET(pVCpu, VMCPU_FF_DBGF);
499 va_end(va);
500 return VINF_EM_DBG_EVENT;
501 }
502
503 va_end(va);
504 return VINF_SUCCESS;
505}
506
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