xref: /haiku/headers/private/kernel/arch/x86/arch_user_debugger.h (revision f2b4344867e97c3f4e742a1b4a15e6879644601a)
1 /*
2  * Copyright 2005-2011, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef _KERNEL_ARCH_X86_USER_DEBUGGER_H
6 #define _KERNEL_ARCH_X86_USER_DEBUGGER_H
7 
8 
9 #define ARCH_INIT_USER_DEBUG x86_init_user_debug
10 
11 // number of breakpoints the CPU supports
12 // Actually it supports 4, but DR3 is used to hold the Thread*.
13 #define X86_BREAKPOINT_COUNT	3
14 
15 // debug status register DR6
16 enum {
17 	X86_DR6_B0			= 0,	// breakpoint condition detected
18 	X86_DR6_B1			= 1,	//
19 	X86_DR6_B2			= 2,	//
20 	X86_DR6_B3			= 3,	//
21 	X86_DR6_BD			= 13,	// debug register access detected
22 	X86_DR6_BS			= 14,	// single step
23 	X86_DR6_BT			= 15,	// task switch
24 
25 	X86_DR6_BREAKPOINT_MASK	= (1 << X86_DR6_B0) | (1 << X86_DR6_B1)
26 								| (1 << X86_DR6_B2) | (1 << X86_DR6_B3),
27 };
28 
29 // debug control register DR7 layout:
30 // 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
31 // LEN3  R/W3  LEN2  R/W2  LEN1  R/W1  LEN0  R/W0
32 //
33 // 15 14 13 12 11 10 9  8  7  6  5  4  3  2  1  0
34 // 0  0  GD 0  0  1  GE LE G3 L3 G2 L2 G1 L1 G0 L0
35 //
36 enum {
37 	X86_DR7_L0			= 0,	// local/global breakpoints enable
38 	X86_DR7_G0			= 1,	//
39 	X86_DR7_L1			= 2,	//
40 	X86_DR7_G1			= 3,	//
41 	X86_DR7_L2			= 4,	//
42 	X86_DR7_G2			= 5,	//
43 	X86_DR7_L3			= 6,	//
44 	X86_DR7_G3			= 7,	//
45 	X86_DR7_LE			= 8,	// local/global exact breakpoint
46 	X86_DR7_GE			= 9,	//
47 	X86_DR7_GD			= 13,	// general detect enable: disallows debug
48 								// register access
49 	X86_DR7_RW0_LSB		= 16,	// breakpoints type and len
50 	X86_DR7_LEN0_LSB	= 18,	//
51 	X86_DR7_RW1_LSB		= 20,	//
52 	X86_DR7_LEN1_LSB	= 22,	//
53 	X86_DR7_RW2_LSB		= 24,	//
54 	X86_DR7_LEN2_LSB	= 26,	//
55 	X86_DR7_RW3_LSB		= 28,	//
56 	X86_DR7_LEN3_LSB	= 30,	//
57 
58 	X86_BREAKPOINTS_DISABLED_DR7
59 		= (1 << 10) | (1 << X86_DR7_GE) | (1 << X86_DR7_LE),
60 		// all breakpoints disabled
61 };
62 
63 // the EFLAGS flags we need
64 enum {
65 	X86_EFLAGS_CF	= 0,		// carry flag
66 	X86_EFLAGS_PF	= 2,		// parity flag
67 	X86_EFLAGS_AF	= 4,		// auxiliary carry flag (adjust flag)
68 	X86_EFLAGS_ZF	= 6,		// zero flag
69 	X86_EFLAGS_SF	= 7,		// sign flag
70 	X86_EFLAGS_TF	= 8,		// trap flag (single stepping)
71 	X86_EFLAGS_DF	= 10,		// direction flag
72 	X86_EFLAGS_OF	= 11,		// overflow flag
73 	X86_EFLAGS_RF	= 16,		// resume flag (skips instruction breakpoint)
74 
75 	X86_EFLAGS_USER_SETTABLE_FLAGS
76 		= (1 << X86_EFLAGS_CF) | (1 << X86_EFLAGS_PF) | (1 << X86_EFLAGS_AF)
77 			| (1 << X86_EFLAGS_ZF) | (1 << X86_EFLAGS_SF) | (1 << X86_EFLAGS_DF)
78 			| (1 << X86_EFLAGS_OF),
79 };
80 
81 // x86 breakpoint types
82 enum {
83 	X86_INSTRUCTION_BREAKPOINT		= 0x0,
84 	X86_DATA_WRITE_BREAKPOINT		= 0x1,
85 	X86_IO_READ_WRITE_BREAKPOINT	= 0x2,		// >= 586
86 	X86_DATA_READ_WRITE_BREAKPOINT	= 0x3,
87 };
88 
89 // x86 breakpoint lengths
90 enum {
91 	X86_BREAKPOINT_LENGTH_1	= 0x0,
92 	X86_BREAKPOINT_LENGTH_2	= 0x1,
93 	X86_BREAKPOINT_LENGTH_4	= 0x3,
94 };
95 
96 struct arch_breakpoint {
97 	void	*address;	// NULL, if deactivated
98 	uint32	type;		// one of the architecture types above
99 	uint32	length;		// one of the length values above
100 };
101 
102 struct arch_team_debug_info {
103 	struct arch_breakpoint	breakpoints[X86_BREAKPOINT_COUNT];
104 
105 	uint32					dr7;	// debug control register DR7
106 };
107 
108 struct arch_thread_debug_info {
109 	uint32	flags;
110 };
111 
112 // The software breakpoint instruction (int3).
113 extern const uint8 kX86SoftwareBreakpoint[1];
114 
115 #ifdef __cplusplus
116 extern "C" {
117 #endif
118 
119 struct iframe;
120 
121 extern void x86_init_user_debug_at_kernel_exit(struct iframe *frame);
122 extern void x86_exit_user_debug_at_kernel_entry();
123 
124 extern void x86_handle_debug_exception(struct iframe *frame);
125 extern void x86_handle_breakpoint_exception(struct iframe *frame);
126 
127 extern void x86_init_user_debug();
128 
129 #ifdef __cplusplus
130 }
131 #endif
132 
133 // Feature macros we're supposed to define.
134 #define DEBUG_MAX_BREAKPOINTS				X86_BREAKPOINT_COUNT
135 #define DEBUG_MAX_WATCHPOINTS				X86_BREAKPOINT_COUNT
136 #define DEBUG_SOFTWARE_BREAKPOINT			kX86SoftwareBreakpoint
137 #define DEBUG_SOFTWARE_BREAKPOINT_SIZE		1
138 #define DEBUG_SHARED_BREAK_AND_WATCHPOINTS	1
139 
140 
141 #endif	// _KERNEL_ARCH_X86_USER_DEBUGGER_H
142