aboutsummaryrefslogtreecommitdiff
path: root/arch_x86/gdbstub_sys.h
blob: 18878d9c8fa4648fc021c83cd1ff3d585cc2664b (plain) (blame)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 * Copyright (c) 2016-2019 Matt Borgerson
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#ifndef _GDBSTUB_SYS_H_
#define _GDBSTUB_SYS_H_

/* Define the size_t type */
#define DBG_DEFINE_SIZET 1

/* Define required standard integer types (e.g. uint16_t) */
#define DBG_DEFINE_STDINT 1

/*****************************************************************************
 * Types
 ****************************************************************************/

#if DBG_DEFINE_STDINT
typedef unsigned char  uint8_t;
typedef unsigned short uint16_t;
typedef unsigned long  uint32_t;
#endif

#if DBG_DEFINE_SIZET
typedef unsigned int size_t;
#endif

typedef unsigned int address;
typedef unsigned int reg;

#pragma pack(1)
struct dbg_interrupt_state {
	uint32_t ss;
	uint32_t gs;
	uint32_t fs;
	uint32_t es;
	uint32_t ds;
	uint32_t edi;
	uint32_t esi;
	uint32_t ebp;
	uint32_t esp;
	uint32_t ebx;
	uint32_t edx;
	uint32_t ecx;
	uint32_t eax;
	uint32_t vector;
	uint32_t error_code;
	uint32_t eip;
	uint32_t cs;
	uint32_t eflags;
};
#pragma pack()

#pragma pack(1)
struct dbg_idtr
{
	uint16_t len;
	uint32_t offset;
};
#pragma pack()

#pragma pack(1)
struct dbg_idt_gate
{
	uint16_t offset_low;
	uint16_t segment;
	uint16_t flags;
	uint16_t offset_high;
};
#pragma pack()

enum DBG_REGISTER {
	DBG_CPU_I386_REG_EAX       = 0,
	DBG_CPU_I386_REG_ECX       = 1,
	DBG_CPU_I386_REG_EDX       = 2,
	DBG_CPU_I386_REG_EBX       = 3,
	DBG_CPU_I386_REG_ESP       = 4,
	DBG_CPU_I386_REG_EBP       = 5,
	DBG_CPU_I386_REG_ESI       = 6,
	DBG_CPU_I386_REG_EDI       = 7,
	DBG_CPU_I386_REG_PC        = 8,
	DBG_CPU_I386_REG_PS        = 9,
	DBG_CPU_I386_REG_CS        = 10,
	DBG_CPU_I386_REG_SS        = 11,
	DBG_CPU_I386_REG_DS        = 12,
	DBG_CPU_I386_REG_ES        = 13,
	DBG_CPU_I386_REG_FS        = 14,
	DBG_CPU_I386_REG_GS        = 15,
	DBG_CPU_NUM_REGISTERS = 16
};

struct dbg_state {
	int signum;
	reg registers[DBG_CPU_NUM_REGISTERS];
};

/*****************************************************************************
 * Const Data
 ****************************************************************************/

extern void const * const dbg_int_handlers[];

/*****************************************************************************
 * Prototypes
 ****************************************************************************/

int dbg_hook_idt(uint8_t vector, const void *function);
int dbg_init_gates(void);
int dbg_init_idt(void);
int dbg_load_idt(struct dbg_idtr *idtr);
int dbg_store_idt(struct dbg_idtr *idtr);
uint32_t dbg_get_cs(void);
void dbg_int_handler(struct dbg_interrupt_state *istate);
void dbg_interrupt(struct dbg_interrupt_state *istate);
void dbg_start(void);
void dbg_io_write_8(uint16_t port, uint8_t val);
uint8_t dbg_io_read_8(uint16_t port);
void *dbg_sys_memset(void *ptr, int data, size_t len);

#endif