-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpu32.c
261 lines (197 loc) · 5.61 KB
/
cpu32.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
249
250
251
252
253
254
255
256
257
258
259
260
/*
Copyright (c) 2001 by William A. Gatliff
All rights reserved. [email protected]
See the file COPYING for details.
This file is provided "as-is", and without any express or implied
warranties, including, without limitation, the implied warranties of
merchantability and fitness for a particular purpose.
The author welcomes feedback regarding this file.
Original CPU32/MC68332 port of gdbstubs contributed by Scott Sumner,
[email protected]. Thanks, Scott!
Made non-MC68332 specific and generally revised Oct 2001 by Bill
Gatliff, [email protected].
The basic CPU32 stub handling code. Uses TRACE bits for instruction
stepping. Does not yet do much with bus error handling, but it will
report a bus error as such.
The exception handler always assumes that there is a twelve word
exception stack frame present. This isn't a problem, because the
stack pointer is always unwound to remove whatever exception frame
is present before any other processing begins. A format==0000 stack
frame is constructed when returning from exceptions.
A copy of the exception stack frame is stored at the end of
register_file.
$Id: cpu32.c,v 1.3 2002/03/20 02:26:01 bgat Exp $
*/
#include "gdb.h"
#include "cpu32.h"
#define SR_T1 0x8000
#define SR_T0 0x4000
typedef enum {
D0, D1, D2, D3, D4, D5, D6, D7,
A0, A1, A2, A3, A4, A5, A6, A7,
FP=A6, SP=A7, SR, PC } register_id_E;
typedef struct {
/* these are the basic registers that gdb understands; the ones we
don't send gdb will assume contain zeros */
long d[8];
long a[8];
short sr;
long pc;
/* internal register/stack management stuff; not part of the
register file proper, but this is a good place for it anyway.
gdb never sees these values */
short fv;
long faulted_addr;
long dbuf;
long current_pc;
short itcr;
short special;
} register_file_S;
static register_file_S register_file;
/* Retrieves a register value from register_file. returns the size of
the register, in bytes, or zero if an invalid id is specified,
which *will* happen---gdb.c uses this functionality to tell how
many registers we actually have. */
int gdb_peek_register_file (int id, long *val)
{
int retval = sizeof(long);
switch (id)
{
case D0: case D1: case D2: case D3:
case D4: case D5: case D6: case D7:
*val = register_file.d[id - D0];
break;
case A0: case A1: case A2: case A3:
case A4: case A5: case A6: case A7:
*val = register_file.a[id - A0];
break;
case SR:
/* sr isn't really 32 bits, but gdb thinks it is */
*val = register_file.sr;
break;
case PC:
*val = register_file.pc;
break;
default: retval = 0;
}
return retval;
}
/* Stuffs a register value into register_file. Returns the size of
the register, in bytes, or zero if an invalid id is specified */
int gdb_poke_register_file (int id, long val)
{
int retval = sizeof(long);
switch (id)
{
case D0: case D1: case D2: case D3:
case D4: case D5: case D6: case D7:
register_file.d[id - D0] = val;
break;
case A0: case A1: case A2: case A3:
case A4: case A5: case A6: case A7:
register_file.a[id - A0] = val;
break;
case SR:
register_file.sr = (short)val;
break;
case PC:
register_file.pc = val;
break;
default: retval = 0;
}
return retval;
}
void gdb_step (long addr)
{
register_file.sr |= SR_T1;
gdb_continue(addr);
}
void gdb_continue (long addr)
{
if (addr) register_file.pc = addr;
gdb_return_from_exception();
}
void gdb_monitor_onentry (void){}
void gdb_monitor_onexit (void){}
void gdb_cpu32_cleanup_stack (void)
{
int sigval;
/* undo damage caused by exception stack frame */
switch (register_file.fv >> 12)
{
default:
case 0:
register_file.a[7] += 4 * sizeof(short);
break;
case 2:
register_file.a[7] += 6 * sizeof(short);
break;
case 0xc:
register_file.a[7] += 12 * sizeof(short);
break;
}
/* turn of instruction tracing */
register_file.sr &= ~(SR_T1 | SR_T0);
/* if gdb sets a breakpoint using trap #1, we have to back up in
order to restart the instruction replaced by the breakpoint */
if(register_file.fv == 0x0084)
register_file.pc -= 2;
switch(register_file.fv & 0xfff)
{
/* translate the exception number into a gdb signal number. */
/* TODO: this has not been thoroughly tested. */
case 2: sigval = GDB_SIGBUS; break;
case 3: sigval = GDB_SIGSEGV; break;
case 4: sigval = GDB_SIGILL; break;
case 5: sigval = GDB_SIGFPE; break;
case 8: sigval = GDB_SIGILL; break;
case 6: case 9: case 7:
default: sigval = GDB_SIGTRAP; break;
}
gdb_handle_exception(sigval);
}
__asm__("
.global gdb_interrupt_handler
gdb_interrupt_handler:
/* disable interrupts */
ori.w #0x700,%sr
gdb_save_state:
/* populate register file */
movem.l %d0-%d7/%a0-%a7,register_file
/* save the exception stack frame */
moveq.l #12,%d0
move.l %a7,%a0
lea register_file+64,%a1
save_exception_frame:
move.w (%a0)+,(%a1)+
dbf %d0,save_exception_frame
bra gdb_cpu32_cleanup_stack
nop
.global gdb_return_from_exception
gdb_return_from_exception:
/* always return via a format==0000 frame */
move.l (register_file+60),%a7
moveq.l #0,%d0
move.w %d0,-(%a7)
lea register_file+70,%a0
move.w -(%a0),-(%a7)
move.w -(%a0),-(%a7)
move.w -(%a0),-(%a7)
movem.l register_file,%d0-%d7/%a0-%a6
rte
nop
.global gdb_kill
gdb_kill:
move.l (0),%a7
move.l (4),%a0
jmp (%a0)
.global gdb_detach
gdb_detach:
move.l (0),%a7
move.l (4),%a0
jmp (%a0)
/* cpu32's don't have caches, do they? */
.global gdb_flush_cache
gdb_flush_cache:
rts
");