Volume 2 System Programming (794096), страница 85
Текст из файла (страница 85)
3.13—July 2007AMD64 Technology13.2.2 Using BreakpointsA debug exception (#DB) occurs when an enabled-breakpoint condition is encountered duringprogram execution. The debug-handler must check the debug-status register (DR6), the conditionsenabled by the debug-control register (DR7), and the debug-control MSR (DebugCtlMSR), todetermine the #DB cause. The #DB exception corresponds to interrupt-vector 1. See “#DB—DebugException (Vector 1)” on page 209.Instruction breakpoints and general-detect conditions cause the #DB exception to occur before theinstruction is executed, while all other breakpoint and single-stepping conditions cause the #DBexception to occur after the instruction is executed. Table 13-2 on page 335 summarizes where the#DB exception occurs based on the breakpoint condition.Table 13-2.
Breakpoint Location by ConditionBreakpoint ConditionInstructionGeneral DetectBreakpoint LocationBefore Instruction is ExecutedData Write OnlyData Read or Data WriteAfter Instruction is Executed1I/O Read or I/O WriteSingle Step1After Instruction is ExecutedTask SwitchNote:1. Repeated operations (REP prefix) can breakpoint between iterations.Instruction breakpoints and general-detect conditions have a lower interrupt-priority than the otherbreakpoint and single-stepping conditions (see “Priorities” on page 224).
Data-breakpoint conditionson the previous instruction occur before an instruction-breakpoint condition on the next instruction.However, if instruction and data breakpoints can occur as a result of executing a single instruction, theinstruction breakpoint occurs first (before the instruction is executed), followed by the data breakpoint(after the instruction is executed).Instruction Breakpoints. Instruction breakpoints are set by loading a breakpoint-address register(DRn) with the desired instruction virtual-address, and then setting the corresponding DR7 fields asfollows:•••Ln or Gn is set to 1 to enable the breakpoint for either the local task or all tasks, respectively.R/Wn is set to 00b to specify that the contents of DRn are to be compared only with the virtualaddress of the next instruction to be executed.LENn must be set to 00b.When a #DB exception occurs due to an instruction breakpoint-address in DRn, the corresponding Bnfield in DR6 is set to 1 to indicate that a breakpoint condition occurred.
The breakpoint occurs beforethe instruction is executed, and the breakpoint-instruction address is pushed onto the debug-handlerDebug and Performance Resources335AMD64 Technology24593—Rev. 3.13—July 2007stack. If multiple instruction breakpoints are set, the debug handler can use the Bn field to identifywhich register caused the breakpoint.Returning from the debug handler causes the breakpoint instruction to be executed.
Before returningfrom the debug handler, the rFLAGS.RF bit should be set to 1 to prevent a reoccurrence of the #DBexception due to the instruction-breakpoint condition. The processor ignores instruction-breakpointconditions when rFLAGS.RF=1, until after the next instruction (in this case, the breakpointinstruction) is executed. After the next instruction is executed, the processor clears rFLAGS.RF to 0.Data Breakpoints. Data breakpoints are set by loading a breakpoint-address register (DRn) with thedesired data virtual-address, and then setting the corresponding DR7 fields as follows:•••Ln or Gn is set to 1 to enable the breakpoint for either the local task or all tasks, respectively.R/Wn is set to 01b to specify that the data virtual-address is compared with the contents of DRnonly during a memory-write.
Setting this field to 11b specifies that the comparison takes placeduring both memory reads and memory writes.LENn is set to 00b, 01b, 11b, or 10b to specify an address-match range of one, two, four, or eightbytes, respectively. Long mode must be active to set LENn to 10b.When a #DB exception occurs due to a data breakpoint address in DRn, the corresponding Bn field inDR6 is set to 1 to indicate that a breakpoint condition occurred. The breakpoint occurs after the dataaccess instruction is executed, which means that the original data is overwritten by the data-accessinstruction.
If the debug handler needs to report the previous data value, it must save that value beforesetting the breakpoint.Because the breakpoint occurs after the data-access instruction is executed, the address of theinstruction following the data-access instruction is pushed onto the debug-handler stack. Repeatedstring instructions, however, can trigger a breakpoint before all iterations of the repeat loop havecompleted. When this happens, the address of the string instruction is pushed onto the stack during a#DB exception if the repeat loop is not complete.
A subsequent IRET from the #DB handler returns tothe string instruction, causing the remaining iterations to be executed. Most implementations cannotreport breakpoints exactly for repeated string instructions, but instead report the breakpoint on aniteration later than the iteration where the breakpoint occurred.I/O Breakpoints. I/O breakpoints are set by loading a breakpoint-address register (DRn) with the I/O-port address to be trapped, and then setting the corresponding DR7 fields as follows:•••Ln or Gn is set to 1 to enable the breakpoint for either the local task or all tasks, respectively.R/Wn is set to 10b to specify that the I/O-port address is compared with the contents of DRn onlyduring execution of an I/O instruction. This encoding of R/Wn is valid only when debug extensionsare enabled (CR4.DE=1).LENn is set to 00b, 01b, or 11b to specify the breakpoint occurs on a byte, word, or doubleword I/Ooperation, respectively.The I/O-port address specified by the I/O instruction is zero extended by the processor to 64 bits beforecomparing it with the DRn registers.336Debug and Performance Resources24593—Rev.
3.13—July 2007AMD64 TechnologyWhen a #DB exception occurs due to an I/O breakpoint in DRn, the corresponding Bn field in DR6 isset to 1 to indicate that a breakpoint condition occurred. The breakpoint occurs after the instruction isexecuted, which means that the original data is overwritten by the breakpoint instruction. If the debughandler needs to report the previous data value, it must save that value before setting the breakpoint.Because the breakpoint occurs after the instruction is executed, the address of the instruction followingthe I/O instruction is pushed onto the debug-handler stack, in most cases. In the case of INS and OUTSinstructions that use the repeat prefix, however, the breakpoint occurs after the first iteration of therepeat loop.
When this happens, the I/O-instruction address can be pushed onto the stack during a #DBexception if the repeat loop is not complete. A subsequent return from the debug handler causes thenext I/O iteration to be executed. If the breakpoint condition is still set, the #DB exception reoccursafter that iteration is complete.Task-Switch Breakpoints. Breakpoints can be set in a task TSS to raise a #DB exception after a taskswitch. Software enables a task breakpoint by setting the T bit in the TSS to 1. When a task switchoccurs into a task with the T bit set, the processor completes loading the new task state.
Before the firstinstruction is executed, the #DB exception occurs, and the processor sets DR6.BT to 1, indicating thatthe #DB exception occurred as a result of task breakpoint.The processor does not clear the T bit in the TSS to 0 when the #DB exception occurs. Software mustexplicitly clear this bit to disable the task breakpoint. Software should never set the T-bit in the debughandler TSS if a separate task is used for #DB exception handling, otherwise the processor loops onthe debug handler.General-Detect Condition.
General-detect is a special debug-exception condition that occurs whensoftware running at any privilege level attempts to access any of the DRn registers while DR7.GD isset to 1. When a #DB exception occurs due to the general-detect condition, the processor clearsDR7.GD to 0 and sets DR6.BD to 1. Clearing DR7.GD to 0 allows the debug handler to access theDRn registers without causing infinite #DB exceptions.A debugger enables general detection to prevent other software from accessing and interfering with thedebug registers while they are in use by the debugger. The exception is taken before executing theMOV DRn instruction so that the DRn contents are not altered.13.2.3 Single SteppingSingle-step breakpoints are enabled by setting the rFLAGS.TF bit to 1.