dbregs.h revision 1.4.12.2 1 1.4.12.2 jdolecek /* $NetBSD: dbregs.h,v 1.4.12.2 2017/12/03 11:36:50 jdolecek Exp $ */
2 1.4.12.2 jdolecek
3 1.4.12.2 jdolecek /*-
4 1.4.12.2 jdolecek * Copyright (c) 2016 The NetBSD Foundation, Inc.
5 1.4.12.2 jdolecek * All rights reserved.
6 1.4.12.2 jdolecek *
7 1.4.12.2 jdolecek * Redistribution and use in source and binary forms, with or without
8 1.4.12.2 jdolecek * modification, are permitted provided that the following conditions
9 1.4.12.2 jdolecek * are met:
10 1.4.12.2 jdolecek * 1. Redistributions of source code must retain the above copyright
11 1.4.12.2 jdolecek * notice, this list of conditions and the following disclaimer.
12 1.4.12.2 jdolecek * 2. Redistributions in binary form must reproduce the above copyright
13 1.4.12.2 jdolecek * notice, this list of conditions and the following disclaimer in the
14 1.4.12.2 jdolecek * documentation and/or other materials provided with the distribution.
15 1.4.12.2 jdolecek *
16 1.4.12.2 jdolecek * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.4.12.2 jdolecek * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.4.12.2 jdolecek * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.4.12.2 jdolecek * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.4.12.2 jdolecek * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.4.12.2 jdolecek * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.4.12.2 jdolecek * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.4.12.2 jdolecek * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.4.12.2 jdolecek * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.4.12.2 jdolecek * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.4.12.2 jdolecek * POSSIBILITY OF SUCH DAMAGE.
27 1.4.12.2 jdolecek */
28 1.4.12.2 jdolecek
29 1.4.12.2 jdolecek
30 1.4.12.2 jdolecek #ifndef _X86_DBREGS_H_
31 1.4.12.2 jdolecek #define _X86_DBREGS_H_
32 1.4.12.2 jdolecek
33 1.4.12.2 jdolecek #include <sys/param.h>
34 1.4.12.2 jdolecek #include <sys/types.h>
35 1.4.12.2 jdolecek #include <machine/reg.h>
36 1.4.12.2 jdolecek
37 1.4.12.2 jdolecek /*
38 1.4.12.2 jdolecek * CPU Debug Status Register (DR6)
39 1.4.12.2 jdolecek *
40 1.4.12.2 jdolecek * Reserved bits: 4-12 and on x86_64 32-64
41 1.4.12.2 jdolecek */
42 1.4.12.2 jdolecek #define X86_DR6_DR0_BREAKPOINT_CONDITION_DETECTED __BIT(0)
43 1.4.12.2 jdolecek #define X86_DR6_DR1_BREAKPOINT_CONDITION_DETECTED __BIT(1)
44 1.4.12.2 jdolecek #define X86_DR6_DR2_BREAKPOINT_CONDITION_DETECTED __BIT(2)
45 1.4.12.2 jdolecek #define X86_DR6_DR3_BREAKPOINT_CONDITION_DETECTED __BIT(3)
46 1.4.12.2 jdolecek #define X86_DR6_DEBUG_REGISTER_ACCESS_DETECTED __BIT(13)
47 1.4.12.2 jdolecek #define X86_DR6_SINGLE_STEP __BIT(14)
48 1.4.12.2 jdolecek #define X86_DR6_TASK_SWITCH __BIT(15)
49 1.4.12.2 jdolecek
50 1.4.12.2 jdolecek /*
51 1.4.12.2 jdolecek * CPU Debug Control Register (DR7)
52 1.4.12.2 jdolecek *
53 1.4.12.2 jdolecek * LOCAL_EXACT_BREAKPOINT and GLOBAL_EXACT_BREAKPOINT are no longer used since
54 1.4.12.2 jdolecek * the P6 processor family - portable code should set these bits
55 1.4.12.2 jdolecek * unconditionally in oder to get exact breakpoints
56 1.4.12.2 jdolecek *
57 1.4.12.2 jdolecek * Reserved bits: 10, 12, 14-15 and on x86_64 32-64
58 1.4.12.2 jdolecek */
59 1.4.12.2 jdolecek #define X86_DR7_LOCAL_DR0_BREAKPOINT __BIT(0)
60 1.4.12.2 jdolecek #define X86_DR7_GLOBAL_DR0_BREAKPOINT __BIT(1)
61 1.4.12.2 jdolecek #define X86_DR7_LOCAL_DR1_BREAKPOINT __BIT(2)
62 1.4.12.2 jdolecek #define X86_DR7_GLOBAL_DR1_BREAKPOINT __BIT(3)
63 1.4.12.2 jdolecek #define X86_DR7_LOCAL_DR2_BREAKPOINT __BIT(4)
64 1.4.12.2 jdolecek #define X86_DR7_GLOBAL_DR2_BREAKPOINT __BIT(5)
65 1.4.12.2 jdolecek #define X86_DR7_LOCAL_DR3_BREAKPOINT __BIT(6)
66 1.4.12.2 jdolecek #define X86_DR7_GLOBAL_DR3_BREAKPOINT __BIT(7)
67 1.4.12.2 jdolecek #define X86_DR7_LOCAL_EXACT_BREAKPOINT __BIT(8)
68 1.4.12.2 jdolecek #define X86_DR7_GLOBAL_EXACT_BREAKPOINT __BIT(9)
69 1.4.12.2 jdolecek #define X86_DR7_RESTRICTED_TRANSACTIONAL_MEMORY __BIT(11)
70 1.4.12.2 jdolecek #define X86_DR7_GENERAL_DETECT_ENABLE __BIT(13)
71 1.4.12.2 jdolecek
72 1.4.12.2 jdolecek #define X86_DR7_DR0_CONDITION_MASK __BITS(16, 17)
73 1.4.12.2 jdolecek #define X86_DR7_DR0_LENGTH_MASK __BITS(18, 19)
74 1.4.12.2 jdolecek #define X86_DR7_DR1_CONDITION_MASK __BITS(20, 21)
75 1.4.12.2 jdolecek #define X86_DR7_DR1_LENGTH_MASK __BITS(22, 23)
76 1.4.12.2 jdolecek #define X86_DR7_DR2_CONDITION_MASK __BITS(24, 25)
77 1.4.12.2 jdolecek #define X86_DR7_DR2_LENGTH_MASK __BITS(26, 27)
78 1.4.12.2 jdolecek #define X86_DR7_DR3_CONDITION_MASK __BITS(28, 29)
79 1.4.12.2 jdolecek #define X86_DR7_DR3_LENGTH_MASK __BITS(30, 31)
80 1.4.12.2 jdolecek
81 1.4.12.2 jdolecek /*
82 1.4.12.2 jdolecek * X86_DR7_CONDITION_IO_READWRITE is currently unused
83 1.4.12.2 jdolecek * it requires DE (debug extension) flag in control register CR4 set
84 1.4.12.2 jdolecek * not all CPUs support it
85 1.4.12.2 jdolecek */
86 1.4.12.2 jdolecek enum x86_dr7_condition {
87 1.4.12.2 jdolecek X86_DR7_CONDITION_EXECUTION = 0x0,
88 1.4.12.2 jdolecek X86_DR7_CONDITION_DATA_WRITE = 0x1,
89 1.4.12.2 jdolecek X86_DR7_CONDITION_IO_READWRITE = 0x2,
90 1.4.12.2 jdolecek X86_DR7_CONDITION_DATA_READWRITE = 0x3
91 1.4.12.2 jdolecek };
92 1.4.12.2 jdolecek
93 1.4.12.2 jdolecek /*
94 1.4.12.2 jdolecek * 0x2 is currently unimplemented - it reflects 8 bytes on modern CPUs
95 1.4.12.2 jdolecek */
96 1.4.12.2 jdolecek enum x86_dr7_length {
97 1.4.12.2 jdolecek X86_DR7_LENGTH_BYTE = 0x0,
98 1.4.12.2 jdolecek X86_DR7_LENGTH_TWOBYTES = 0x1,
99 1.4.12.2 jdolecek /* 0x2 undefined */
100 1.4.12.2 jdolecek X86_DR7_LENGTH_FOURBYTES = 0x3
101 1.4.12.2 jdolecek };
102 1.4.12.2 jdolecek
103 1.4.12.2 jdolecek /*
104 1.4.12.2 jdolecek * The number of available watchpoint/breakpoint registers available since
105 1.4.12.2 jdolecek * Intel 80386. New CPUs (x86_64) ship with up to 16 Debug Registers but they
106 1.4.12.2 jdolecek * still offer the same number of watchpoints/breakpoints.
107 1.4.12.2 jdolecek */
108 1.4.12.2 jdolecek #define X86_DBREGS 4
109 1.4.12.2 jdolecek
110 1.4.12.2 jdolecek /*
111 1.4.12.2 jdolecek * Store the initial Debug Register state of CPU
112 1.4.12.2 jdolecek * This copy will be used to initialize new debug register state
113 1.4.12.2 jdolecek */
114 1.4.12.2 jdolecek void x86_dbregs_setup_initdbstate(void);
115 1.4.12.2 jdolecek
116 1.4.12.2 jdolecek /*
117 1.4.12.2 jdolecek * Reset CPU Debug Registers - to be used after returning to user context
118 1.4.12.2 jdolecek */
119 1.4.12.2 jdolecek void x86_dbregs_clear(struct lwp *l);
120 1.4.12.2 jdolecek
121 1.4.12.2 jdolecek /*
122 1.4.12.2 jdolecek * Retrieve Debug Registers from LWP's PCB and save in regs
123 1.4.12.2 jdolecek * In case of empty register set, initialize it
124 1.4.12.2 jdolecek */
125 1.4.12.2 jdolecek void x86_dbregs_read(struct lwp *l, struct dbreg *regs);
126 1.4.12.2 jdolecek
127 1.4.12.2 jdolecek /*
128 1.4.12.2 jdolecek * Set CPU Debug Registers - to be used before entering user-land context
129 1.4.12.2 jdolecek */
130 1.4.12.2 jdolecek void x86_dbregs_set(struct lwp *l);
131 1.4.12.2 jdolecek
132 1.4.12.2 jdolecek /*
133 1.4.12.2 jdolecek * Store DR6 in LWP - to be used in trap function
134 1.4.12.2 jdolecek */
135 1.4.12.2 jdolecek void x86_dbregs_store_dr6(struct lwp *l);
136 1.4.12.2 jdolecek
137 1.4.12.2 jdolecek /*
138 1.4.12.2 jdolecek * Check if trap is triggered from user-land if so return nonzero value
139 1.4.12.2 jdolecek */
140 1.4.12.2 jdolecek int x86_dbregs_user_trap(void);
141 1.4.12.2 jdolecek
142 1.4.12.2 jdolecek /*
143 1.4.12.2 jdolecek * Check if trap is triggered from user-land if so return nonzero value
144 1.4.12.2 jdolecek */
145 1.4.12.2 jdolecek int x86_dbregs_validate(const struct dbreg *regs);
146 1.4.12.2 jdolecek
147 1.4.12.2 jdolecek /*
148 1.4.12.2 jdolecek * Write new Debug Registers from regs into LWP's PCB
149 1.4.12.2 jdolecek */
150 1.4.12.2 jdolecek void x86_dbregs_write(struct lwp *l, const struct dbreg *regs);
151 1.4.12.2 jdolecek
152 1.4.12.2 jdolecek #endif /* !_X86_DBREGS_H_ */
153