-
Notifications
You must be signed in to change notification settings - Fork 0
/
hwbreak.html
69 lines (69 loc) · 6.45 KB
/
hwbreak.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
<title>Notes on Hardware Breakpoints and Watchpoints</title>
<style type="text/css">
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
</style>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<header id="title-block-header">
<h1 class="title">Notes on Hardware Breakpoints and Watchpoints</h1>
</header>
<h1 id="amd64">AMD64</h1>
<p>Hardware breakpoints on amd64 are fairly well documented, they are controlled by registers DR0 through DR7.</p>
<p>Register DR7 is a control register, that determines which hardware breakpoints/watchpoints are enabled and how they work. DR6 is a status register will tell you which breakpoint/watchpoint was triggered. Registers DR0 through DR5 contain the address of each breakpoint or watchpoint, DR4 and DR5 are reserved for the kernel.</p>
<p>Precise documentation on the layout of DR7 and DR6 can be found in “IA-32 Architectures Software Developer’s Manual, Vol. 3B, section 17.2”.</p>
<h2 id="enabling-a-breakpoint-or-watchpoint">Enabling a breakpoint or watchpoint</h2>
<ul>
<li>Write the address in one of DR0, DR1, DR2 or DR3 that isn’t being used</li>
<li>Write the four bits representing length and mode for the breakpoint or watchpoint to DR7</li>
<li>Write the enable bit for the breakpoint or watchpoint to DR7</li>
</ul>
<h2 id="checking-if-a-breakpoint-was-triggered">Checking if a breakpoint was triggered</h2>
<ul>
<li>Check the relative bit in DR6, it is the responsibility of the debugger to clear this bit before restarting</li>
</ul>
<h2 id="linux-interface">Linux Interface</h2>
<p>On linux the debug registers are stored in the user struct which can be read and written using PTRACE_PEEKUSER and PTRACE_POKEUSER. To determine the offsets for the debug registers you can either use the definition of the user struct, in source/arch/x86/kernel/ptrace.c or just use the constant 848:</p>
<pre><code>ptrace(PEEK_USER, pid, (void *)848, &v);</code></pre>
<p>reads the value of DR0 into v0.</p>
<p>Unlike on windows all 8 registers are present in the user struct, reading DR4 and DR5 (which are reserved for kernel use) will always get you 0x0. Trying to write DR4 and DR5 will result in an error (EIO).</p>
<p>A watchpoint or hardware breakpoint hit is reported with a SIGTRAP, checking the value of DR6 will determine if it was a watchpoint or hardware breakpoint as well as determine which one it was.</p>
<h2 id="windows-interface">Windows Interface</h2>
<p>DR0 through DR3, DR6 and DR7 are fields in the CONTEXT struct that can be read with GetThreadContext and written with SetThreadContext.</p>
<h1 id="arm64">ARM64</h1>
<p>Arm64 is a far bit more complicated than amd64 as well as being overall less documented, especially when it comes to the interfaces provided by the linux kernel.</p>
<p>On arm64 there can be a maximum of 16 watchpoints and 16 hardware breakpoints, however specific implementations of arm64 can (and often will) offer fewer.</p>
<p>All of the following can be referenced in “ARM - Architecture Reference Manual Armv8, for A-profile architectures”, the number in parenthesis will be the section number.</p>
<p>The first register of interest is ID_AA64DFR0_EL1 (AArch64 Debug Feature Register 0) which will tell you how many watchpoint and hardware breakpoints exist in your particular CPU (D13.2.59).</p>
<p>Each breakpoint is described by a pair of registers: * DBGBCRn_EL1 (where n = 0 .. 16) is the breakpoint control register (D13.3.2) * DBGBVRn_EL1 (where n = 0 .. 16) is the breakpoint value register, which will contain the address of the instruction at which the breakpoint should trigger. (D13.3.3)</p>
<p>The same goes for watchpoints which are described by</p>
<ul>
<li>DBGWCRn_EL1 (where n = 0 .. 16) which determines if the watchpoint is enabled, if it is a read or write breakpoint and the length of the data (D13.3.11)</li>
<li>DBGWVRn_EL1 (where n = 0 .. 16) which contains the address of the watchpoint (D13.3.10)</li>
</ul>
<p>When a breakpoint or watchpoint is hit the address that caused the breakpoint to trigger is saved in FAR_EL1 the Fault Address Register, which should be checked to determine which breakpoint was triggered.</p>
<p>Because of certain hardware limitations watchpoint hits are imprecise, fortunately details about this are handled by the kernel.</p>
<h2 id="linux-interface-1">Linux Interface</h2>
<p>The debug registers are accessed through the PTRACE_GETREGSET/PTRACE_SETREGSET interface, when reading register set types NT_ARM_HW_WATCH and NT_ARM_HW_BREAK.</p>
<p>The layout of the register set is described by the structure user_hwdebug_state defined in arch/arm64/include/uapi/asm/ptrace.h, and is as follows:</p>
<ul>
<li>1byte: number of watchpoints for NT_ARM_HW_WATCH or breakpoints for NT_ARM_HW_BREAK</li>
<li>1byte: debug architecture version (the 4 least significant bits of ID_AA64DFR0_EL1)</li>
<li>6bytes: padding</li>
</ul>
<p>then for each watchpoint (or breakpoint) a pair of 64bit words follow, the first one being the “value” register (DBGBVRn_EL1 or DBGWVRn_EL1) and the second one being the control register (DBGBCRn_EL1 or DBGWCRn_EL1).</p>
<p>Note that for the control register of watchpoints (DBGWCRn_EL1) only the fields BAS, LSC, PAC and E are considered, which greatly reduces the features of arm64 which you are able to use.</p>
<p>When reading with PTRACE_GETREGSET you can just read all 16 registers, even if the CPU defines fewer. The registers that don’t exist will simply be always zeroed.</p>
<p>However when writing with PTRACE_SETREGSET you can only write registers that exist, even if you simply pass 0x0 for the ones that don’t. Trying to write non-existent registers will return the slightly confusing ENOSPC.</p>
<p>When a breakpoint or watchpoint is triggered a SIGTRAP will be returned to the debugger by waitpid. To determine if the cause of the SIGTRAP was actually a watchpoint use PTRACE_GETSIGINFO to retrieve the siginfo_t struct for each thread that received SIGTRAP and check that the Code is 0x4 (TRAP_HWBKPT), the addr field of siginfo_t will contain the address that caused the watchpoint hit (i.e. the address that was saved in FAR_EL1 by the CPU).</p>
</body>
</html>