-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgdbstub_sys.c
248 lines (202 loc) · 6.38 KB
/
gdbstub_sys.c
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
* 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.
*/
#include <string.h>
#include <libdragon.h>
#include "gdbstub.h"
#ifdef __STRICT_ANSI__
#define asm __asm__
#endif
#define NUM_IDT_ENTRIES 32
/*****************************************************************************
* BSS Data
****************************************************************************/
static struct dbg_state dbg_state;
extern volatile uint64_t __baseRegAddr;
static reg_block_t *regs = (reg_block_t*)&__baseRegAddr;
static struct {
uintptr_t addr;
uint32_t value;
} orig_op_codes[2];
/*****************************************************************************
* Misc. Functions
****************************************************************************/
static void db_step_set(uint32_t index, uintptr_t addr, uint32_t value)
{
orig_op_codes[index].addr = addr;
orig_op_codes[index].value = value;
*(uint32_t*)addr = value;
data_cache_index_writeback_invalidate((void*)addr, 4);
inst_cache_hit_invalidate((void*)addr, 4);
}
void *dbg_sys_memset(void *ptr, int data, size_t len)
{
char *p = ptr;
while (len--) {
*p++ = (char)data;
}
return ptr;
}
/*****************************************************************************
* Interrupt Management Functions
****************************************************************************/
void dbg_int_handler_libdragon(exception_t *exception)
{
uint32_t badvaddr;
asm volatile ("mfc0 %0, $8\nnop\nnop" : "=r"(badvaddr));
dbg_state.signum = 5;
for(int i = 0; i < 32; i++)
{
dbg_state.registers[i] = 0xFFFFFFFF00000000 | exception->regs->gpr[i];
}
dbg_state.registers[32] = 0xFFFFFFFF00000000 | exception->regs->sr;
dbg_state.registers[33] = 0xFFFFFFFF00000000 | exception->regs->lo;
dbg_state.registers[34] = 0xFFFFFFFF00000000 | exception->regs->hi;
dbg_state.registers[35] = 0xFFFFFFFF00000000 | badvaddr;
dbg_state.registers[36] = 0xFFFFFFFF00000000 | exception->regs->cr;
dbg_state.registers[37] = 0xFFFFFFFF00000000 | exception->regs->epc;
for(int i = 0; i < 32; i++)
{
dbg_state.registers[i+38] = exception->regs->fpr[i];
}
dbg_state.registers[73] = 0;
for(int i = 0; i < sizeof(orig_op_codes)/sizeof(*orig_op_codes); i++) {
if(orig_op_codes[i].addr != 0) {
uint32_t *addr = (uint32_t*)orig_op_codes[i].addr;
*addr = orig_op_codes[i].value;
data_cache_index_writeback_invalidate((void*)addr, 4);
inst_cache_hit_invalidate((void*)addr, 4);
orig_op_codes[i].addr = 0;
}
}
dbg_main(&dbg_state);
for(int i = 0; i < 32; i++)
{
regs->gpr[i] = dbg_state.registers[i];
}
regs->sr = dbg_state.registers[32];
regs->lo = dbg_state.registers[33];
regs->hi = dbg_state.registers[34];
regs->cr = dbg_state.registers[36];
regs->epc = dbg_state.registers[37];
asm volatile("move $t0, %0\nnop\nmtc0 $t0, $30\nnop\nmtc0 $t0, $14\nnop\nnop\n" :: "r"(dbg_state.registers[37]) : "%t0");
}
void dbg_init_exception(void)
{
register_exception_handler(dbg_int_handler_libdragon);
}
/*****************************************************************************
* Generic serial interface
****************************************************************************/
int dbg_serial_getc(void)
{
/* Wait for data */
return *((volatile uint32_t *) (0xA0000000 | 0x18000000));
}
int dbg_serial_putchar(int ch)
{
*((volatile uint32_t *) (0xA0000000 | 0x18000000)) = ch;
return ch;
}
/*****************************************************************************
* Debugging System Functions
****************************************************************************/
/*
* Write one character to the debugging stream.
*/
int dbg_sys_putchar(int ch)
{
return dbg_serial_putchar(ch);
}
/*
* Read one character from the debugging stream.
*/
int dbg_sys_getc(void)
{
return dbg_serial_getc() & 0xff;
}
/*
* Read one byte from memory.
*/
int dbg_sys_mem_readb(address addr, char *val)
{
*val = *(volatile char *)addr;
return 0;
}
/*
* Write one byte to memory.
*/
int dbg_sys_mem_writeb(address addr, char val)
{
*(volatile char *)addr = val;
return 0;
}
/*
* Continue program execution.
*/
int dbg_sys_continue(void)
{
dbg_state.registers[DBG_REG_PC] += 4;
return 0;
}
/*
* Single step the next instruction.
*/
int dbg_sys_step(void)
{
intptr_t pc = (intptr_t)dbg_state.registers[DBG_REG_PC];
uint32_t insn = *(uint32_t*)pc;
intptr_t tnext, fnext;
tnext = pc + 4;
fnext = -1;
if((insn & 0xF8000000) == 0x08000000) { /* J/JAL */
tnext = (tnext & 0xFFFFFFFFF0000000) | ((insn & 0x03FFFFFF) << 2);
} else if(0
|| ((insn & 0xFC0C0000) == 0x04000000) /* B{LT/GE}Z[AL][L] */
|| ((insn & 0xF0000000) == 0x10000000) /* B{EQ/NE/LE/GT} */
|| ((insn & 0xF3E00000) == 0x41000000) /* BCz{T/F}[L] */
|| ((insn & 0xF0000000) == 0x50000000) /* B{EQ/NE/LE/GT}L */
) {
fnext = tnext + 4; /* next of branch-delay */
tnext = tnext + ((intptr_t)(int16_t)insn << 2);
} else if((insn & 0xFC00003E) == 0x00000008) { /* JR/JALR */
uint32_t reg = (insn >> 21) & 0x1F;
tnext = *((int64_t*)((uint32_t)dbg_state.registers[reg]));
}
if(fnext != -1) {
db_step_set(1, fnext, 0x0000000D);
}
db_step_set(0, tnext, 0x0000000D);
dbg_state.registers[DBG_REG_PC] += 4;
return 0;
}
/*
* Debugger init function.
*
* Installs the exception handler to enable debugging.
*/
void dbg_start(void)
{
/* Initalize interrupt handler */
dbg_init_exception();
/* Interrupt to start debugging. */
asm volatile ("break");
}