VirtualBox

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

Last change on this file since 60309 was 59073, checked in by vboxsync, 9 years ago

VMM: VINF_EM_DBG_EVENT and DBGFEventGenericWithArg implementation.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 13.2 KB
Line 
1/* $Id: DBGFAll.cpp 59073 2015-12-10 12:48:03Z vboxsync $ */
2/** @file
3 * DBGF - Debugger Facility, All Context Code.
4 */
5
6/*
7 * Copyright (C) 2006-2015 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 "DBGFInternal.h"
25#include <VBox/vmm/vm.h>
26#include <VBox/err.h>
27#include <iprt/assert.h>
28#include <iprt/asm.h>
29
30
31/*
32 * Check the read-only VM members.
33 */
34AssertCompileMembersSameSizeAndOffset(VM, dbgf.s.bmSoftIntBreakpoints, VM, dbgf.ro.bmSoftIntBreakpoints);
35AssertCompileMembersSameSizeAndOffset(VM, dbgf.s.bmHardIntBreakpoints, VM, dbgf.ro.bmHardIntBreakpoints);
36AssertCompileMembersSameSizeAndOffset(VM, dbgf.s.bmSelectedEvents, VM, dbgf.ro.bmSelectedEvents);
37AssertCompileMembersSameSizeAndOffset(VM, dbgf.s.cHardIntBreakpoints, VM, dbgf.ro.cHardIntBreakpoints);
38AssertCompileMembersSameSizeAndOffset(VM, dbgf.s.cSoftIntBreakpoints, VM, dbgf.ro.cSoftIntBreakpoints);
39AssertCompileMembersSameSizeAndOffset(VM, dbgf.s.cSelectedEvents, VM, dbgf.ro.cSelectedEvents);
40
41
42/**
43 * Gets the hardware breakpoint configuration as DR7.
44 *
45 * @returns DR7 from the DBGF point of view.
46 * @param pVM The cross context VM structure.
47 */
48VMM_INT_DECL(RTGCUINTREG) DBGFBpGetDR7(PVM pVM)
49{
50 RTGCUINTREG uDr7 = X86_DR7_GD | X86_DR7_GE | X86_DR7_LE | X86_DR7_RA1_MASK;
51 PDBGFBP pBp = &pVM->dbgf.s.aHwBreakpoints[0];
52 unsigned cLeft = RT_ELEMENTS(pVM->dbgf.s.aHwBreakpoints);
53 while (cLeft-- > 0)
54 {
55 if ( pBp->enmType == DBGFBPTYPE_REG
56 && pBp->fEnabled)
57 {
58 static const uint8_t s_au8Sizes[8] =
59 {
60 X86_DR7_LEN_BYTE, X86_DR7_LEN_BYTE, X86_DR7_LEN_WORD, X86_DR7_LEN_BYTE,
61 X86_DR7_LEN_DWORD,X86_DR7_LEN_BYTE, X86_DR7_LEN_BYTE, X86_DR7_LEN_QWORD
62 };
63 uDr7 |= X86_DR7_G(pBp->u.Reg.iReg)
64 | X86_DR7_RW(pBp->u.Reg.iReg, pBp->u.Reg.fType)
65 | X86_DR7_LEN(pBp->u.Reg.iReg, s_au8Sizes[pBp->u.Reg.cb]);
66 }
67 pBp++;
68 }
69 return uDr7;
70}
71
72
73/**
74 * Gets the address of the hardware breakpoint number 0.
75 *
76 * @returns DR0 from the DBGF point of view.
77 * @param pVM The cross context VM structure.
78 */
79VMM_INT_DECL(RTGCUINTREG) DBGFBpGetDR0(PVM pVM)
80{
81 PCDBGFBP pBp = &pVM->dbgf.s.aHwBreakpoints[0];
82 Assert(pBp->u.Reg.iReg == 0);
83 return pBp->u.Reg.GCPtr;
84}
85
86
87/**
88 * Gets the address of the hardware breakpoint number 1.
89 *
90 * @returns DR1 from the DBGF point of view.
91 * @param pVM The cross context VM structure.
92 */
93VMM_INT_DECL(RTGCUINTREG) DBGFBpGetDR1(PVM pVM)
94{
95 PCDBGFBP pBp = &pVM->dbgf.s.aHwBreakpoints[1];
96 Assert(pBp->u.Reg.iReg == 1);
97 return pBp->u.Reg.GCPtr;
98}
99
100
101/**
102 * Gets the address of the hardware breakpoint number 2.
103 *
104 * @returns DR2 from the DBGF point of view.
105 * @param pVM The cross context VM structure.
106 */
107VMM_INT_DECL(RTGCUINTREG) DBGFBpGetDR2(PVM pVM)
108{
109 PCDBGFBP pBp = &pVM->dbgf.s.aHwBreakpoints[2];
110 Assert(pBp->u.Reg.iReg == 2);
111 return pBp->u.Reg.GCPtr;
112}
113
114
115/**
116 * Gets the address of the hardware breakpoint number 3.
117 *
118 * @returns DR3 from the DBGF point of view.
119 * @param pVM The cross context VM structure.
120 */
121VMM_INT_DECL(RTGCUINTREG) DBGFBpGetDR3(PVM pVM)
122{
123 PCDBGFBP pBp = &pVM->dbgf.s.aHwBreakpoints[3];
124 Assert(pBp->u.Reg.iReg == 3);
125 return pBp->u.Reg.GCPtr;
126}
127
128
129/**
130 * Checks if any of the hardware breakpoints are armed.
131 *
132 * @returns true if armed, false if not.
133 * @param pVM The cross context VM structure.
134 * @remarks Don't call this from CPUMRecalcHyperDRx!
135 */
136VMM_INT_DECL(bool) DBGFBpIsHwArmed(PVM pVM)
137{
138 return pVM->dbgf.s.cEnabledHwBreakpoints > 0;
139}
140
141
142/**
143 * Checks if any of the hardware I/O breakpoints are armed.
144 *
145 * @returns true if armed, false if not.
146 * @param pVM The cross context VM structure.
147 * @remarks Don't call this from CPUMRecalcHyperDRx!
148 */
149VMM_INT_DECL(bool) DBGFBpIsHwIoArmed(PVM pVM)
150{
151 return pVM->dbgf.s.cEnabledHwIoBreakpoints > 0;
152}
153
154
155/**
156 * Checks I/O access for guest or hypervisor breakpoints.
157 *
158 * @returns Strict VBox status code
159 * @retval VINF_SUCCESS no breakpoint.
160 * @retval VINF_EM_DBG_BREAKPOINT hypervisor breakpoint triggered.
161 * @retval VINF_EM_RAW_GUEST_TRAP guest breakpoint triggered, DR6 and DR7 have
162 * been updated appropriately.
163 *
164 * @param pVM The cross context VM structure.
165 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
166 * @param pCtx The CPU context for the calling EMT.
167 * @param uIoPort The I/O port being accessed.
168 * @param cbValue The size/width of the access, in bytes.
169 */
170VMM_INT_DECL(VBOXSTRICTRC) DBGFBpCheckIo(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx, RTIOPORT uIoPort, uint8_t cbValue)
171{
172 uint32_t const uIoPortFirst = uIoPort;
173 uint32_t const uIoPortLast = uIoPortFirst + cbValue - 1;
174
175
176 /*
177 * Check hyper breakpoints first as the VMM debugger has priority over
178 * the guest.
179 */
180 if (pVM->dbgf.s.cEnabledHwIoBreakpoints > 0)
181 {
182 for (unsigned iBp = 0; iBp < RT_ELEMENTS(pVM->dbgf.s.aHwBreakpoints); iBp++)
183 {
184 if ( pVM->dbgf.s.aHwBreakpoints[iBp].u.Reg.fType == X86_DR7_RW_IO
185 && pVM->dbgf.s.aHwBreakpoints[iBp].fEnabled
186 && pVM->dbgf.s.aHwBreakpoints[iBp].enmType == DBGFBPTYPE_REG )
187 {
188 uint8_t cbReg = pVM->dbgf.s.aHwBreakpoints[iBp].u.Reg.cb; Assert(RT_IS_POWER_OF_TWO(cbReg));
189 uint64_t uDrXFirst = pVM->dbgf.s.aHwBreakpoints[iBp].u.Reg.GCPtr & ~(uint64_t)(cbReg - 1);
190 uint64_t uDrXLast = uDrXFirst + cbReg - 1;
191 if (uDrXFirst <= uIoPortLast && uDrXLast >= uIoPortFirst)
192 {
193 /* (See also DBGFRZTrap01Handler.) */
194 pVCpu->dbgf.s.iActiveBp = pVM->dbgf.s.aHwBreakpoints[iBp].iBp;
195 pVCpu->dbgf.s.fSingleSteppingRaw = false;
196
197 LogFlow(("DBGFBpCheckIo: hit hw breakpoint %d at %04x:%RGv (iop %#x)\n",
198 pVM->dbgf.s.aHwBreakpoints[iBp].iBp, pCtx->cs.Sel, pCtx->rip, uIoPort));
199 return VINF_EM_DBG_BREAKPOINT;
200 }
201 }
202 }
203 }
204
205 /*
206 * Check the guest.
207 */
208 uint32_t const uDr7 = pCtx->dr[7];
209 if ( (uDr7 & X86_DR7_ENABLED_MASK)
210 && X86_DR7_ANY_RW_IO(uDr7)
211 && (pCtx->cr4 & X86_CR4_DE) )
212 {
213 for (unsigned iBp = 0; iBp < 4; iBp++)
214 {
215 if ( (uDr7 & X86_DR7_L_G(iBp))
216 && X86_DR7_GET_RW(uDr7, iBp) == X86_DR7_RW_IO)
217 {
218 /* ASSUME the breakpoint and the I/O width qualifier uses the same encoding (1 2 x 4). */
219 static uint8_t const s_abInvAlign[4] = { 0, 1, 7, 3 };
220 uint8_t cbInvAlign = s_abInvAlign[X86_DR7_GET_LEN(uDr7, iBp)];
221 uint64_t uDrXFirst = pCtx->dr[iBp] & ~(uint64_t)cbInvAlign;
222 uint64_t uDrXLast = uDrXFirst + cbInvAlign;
223
224 if (uDrXFirst <= uIoPortLast && uDrXLast >= uIoPortFirst)
225 {
226 /*
227 * Update DR6 and DR7.
228 *
229 * See "AMD64 Architecture Programmer's Manual Volume 2",
230 * chapter 13.1.1.3 for details on DR6 bits. The basics is
231 * that the B0..B3 bits are always cleared while the others
232 * must be cleared by software.
233 *
234 * The following sub chapters says the GD bit is always
235 * cleared when generating a #DB so the handler can safely
236 * access the debug registers.
237 */
238 pCtx->dr[6] &= ~X86_DR6_B_MASK;
239 pCtx->dr[6] |= X86_DR6_B(iBp);
240 pCtx->dr[7] &= ~X86_DR7_GD;
241 LogFlow(("DBGFBpCheckIo: hit hw breakpoint %d at %04x:%RGv (iop %#x)\n",
242 pVM->dbgf.s.aHwBreakpoints[iBp].iBp, pCtx->cs.Sel, pCtx->rip, uIoPort));
243 return VINF_EM_RAW_GUEST_TRAP;
244 }
245 }
246 }
247 }
248 return VINF_SUCCESS;
249}
250
251
252/**
253 * Returns the single stepping state for a virtual CPU.
254 *
255 * @returns stepping (true) or not (false).
256 *
257 * @param pVCpu The cross context virtual CPU structure.
258 */
259VMM_INT_DECL(bool) DBGFIsStepping(PVMCPU pVCpu)
260{
261 return pVCpu->dbgf.s.fSingleSteppingRaw;
262}
263
264
265/**
266 * Checks if the specified generic event is enabled or not.
267 *
268 * @returns true / false.
269 * @param pVM The cross context VM structure.
270 * @param enmEvent The generic event being raised.
271 * @param uEventArg The argument of that event.
272 */
273DECLINLINE(bool) dbgfEventIsGenericWithArgEnabled(PVM pVM, DBGFEVENTTYPE enmEvent, uint64_t uEventArg)
274{
275 if (DBGF_IS_EVENT_ENABLED(pVM, enmEvent))
276 {
277 switch (enmEvent)
278 {
279 case DBGFEVENT_INTERRUPT_HARDWARE:
280 AssertReturn(uEventArg < 256, false);
281 return ASMBitTest(pVM->dbgf.s.bmHardIntBreakpoints, (uint32_t)uEventArg);
282
283 case DBGFEVENT_INTERRUPT_SOFTWARE:
284 AssertReturn(uEventArg < 256, false);
285 return ASMBitTest(pVM->dbgf.s.bmSoftIntBreakpoints, (uint32_t)uEventArg);
286
287 default:
288 return true;
289
290 }
291 }
292 return false;
293}
294
295
296/**
297 * Raises a generic debug event if enabled and not being ignored.
298 *
299 * @returns Strict VBox status code.
300 * @retval VINF_EM_DBG_EVENT if the event was raised and the caller should
301 * return ASAP to the debugger (via EM).
302 * @retval VINF_SUCCESS if the event was disabled or ignored.
303 *
304 * @param pVM The cross context VM structure.
305 * @param pVCpu The cross context virtual CPU structure.
306 * @param enmEvent The generic event being raised.
307 * @param uEventArg The argument of that event.
308 * @param enmCtx The context in which this event is being raised.
309 *
310 * @thread EMT(pVCpu)
311 */
312VMM_INT_DECL(VBOXSTRICTRC) DBGFEventGenericWithArg(PVM pVM, PVMCPU pVCpu, DBGFEVENTTYPE enmEvent, uint64_t uEventArg,
313 DBGFEVENTCTX enmCtx)
314{
315 /*
316 * Is it enabled.
317 */
318 if (dbgfEventIsGenericWithArgEnabled(pVM, enmEvent, uEventArg))
319 {
320 /*
321 * Any events on the stack. Should the incoming event be ignored?
322 */
323 uint64_t const rip = CPUMGetGuestRIP(pVCpu);
324 uint32_t i = pVCpu->dbgf.s.cEvents;
325 if (i > 0)
326 {
327 while (i-- > 0)
328 {
329 if ( pVCpu->dbgf.s.aEvents[i].Event.enmType == enmEvent
330 && pVCpu->dbgf.s.aEvents[i].enmState == DBGFEVENTSTATE_IGNORE
331 && pVCpu->dbgf.s.aEvents[i].rip == rip)
332 {
333 pVCpu->dbgf.s.aEvents[i].enmState = DBGFEVENTSTATE_RESTORABLE;
334 return VINF_SUCCESS;
335 }
336 Assert(pVCpu->dbgf.s.aEvents[i].enmState != DBGFEVENTSTATE_CURRENT);
337 }
338
339 /*
340 * Trim the event stack.
341 */
342 i = pVCpu->dbgf.s.cEvents;
343 while (i-- > 0)
344 {
345 if ( pVCpu->dbgf.s.aEvents[i].rip == rip
346 && ( pVCpu->dbgf.s.aEvents[i].enmState == DBGFEVENTSTATE_RESTORABLE
347 || pVCpu->dbgf.s.aEvents[i].enmState == DBGFEVENTSTATE_IGNORE) )
348 pVCpu->dbgf.s.aEvents[i].enmState = DBGFEVENTSTATE_IGNORE;
349 else
350 {
351 if (i + 1 != pVCpu->dbgf.s.cEvents)
352 memmove(&pVCpu->dbgf.s.aEvents[i], &pVCpu->dbgf.s.aEvents[i + 1],
353 (pVCpu->dbgf.s.cEvents - i) * sizeof(pVCpu->dbgf.s.aEvents));
354 pVCpu->dbgf.s.cEvents--;
355 }
356 }
357
358 i = pVCpu->dbgf.s.cEvents;
359 AssertStmt(i < RT_ELEMENTS(pVCpu->dbgf.s.aEvents), i = RT_ELEMENTS(pVCpu->dbgf.s.aEvents) - 1);
360 }
361
362 /*
363 * Push the event.
364 */
365 pVCpu->dbgf.s.aEvents[i].enmState = DBGFEVENTSTATE_CURRENT;
366 pVCpu->dbgf.s.aEvents[i].rip = rip;
367 pVCpu->dbgf.s.aEvents[i].Event.enmType = enmEvent;
368 pVCpu->dbgf.s.aEvents[i].Event.enmCtx = enmCtx;
369 pVCpu->dbgf.s.aEvents[i].Event.u.Generic.uArg = uEventArg;
370 pVCpu->dbgf.s.cEvents = i + 1;
371
372 return VINF_EM_DBG_EVENT;
373 }
374
375 return VINF_SUCCESS;
376}
377
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