This repository has been archived by the owner on Sep 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqGdbARMPlugin.c
executable file
·214 lines (171 loc) · 4.89 KB
/
sqGdbARMPlugin.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
#define COG 1
#define FOR_COG_PLUGIN 1
#include "GdbARMPlugin.h"
//disassembler
#include <bfd.h>
#include <dis-asm.h>
#include <stdarg.h>
//emulator
#include <armdefs.h>
#include <armemu.h>
ARMul_State* lastCPU = NULL;
/* When compiling armulator, it generally sets NEED_UI_LOOP_HOOK, which
makes the main emulation step require this symbol to be set. */
extern int (*deprecated_ui_loop_hook) (int) = NULL;
// These two variables exist, in case there are library-functions which write to a stream.
// In that case, supply gdb_log_printf, which writes to those two variables.
#define LOGSIZE 4096
static char gdb_log[LOGSIZE+1];
static int gdblog_index = 0;
ulong minReadAddress, minWriteAddress;
void (*prevInterruptCheckChain)() = 0;
// used only for debugging purposes
void
print_state(ARMul_State* state)
{
printf("NextInstr: %i\ttheMemory: 0x%p\tNumInstrs: 0x%p\tPC: 0x%p\tmode: %i\tEndCondition: %i\tEmulate: %i\n",
state->NextInstr, state->MemDataPtr,
state->NumInstrs, state->Reg[15],
state->Mode, state->EndCondition,
state->Emulate);
}
void*
newCPU()
{
if(lastCPU == NULL) ARMul_EmulateInit();
lastCPU = ARMul_NewState();
return lastCPU;
}
int
resetCPU(void* cpu)
{
unsigned int i, j;
ARMul_State* state = (ARMul_State*) cpu;
// test whether the supplied instance is an ARMul type?
gdblog_index = 0;
// reset registers in all modes
for (i = 0; i < 16; i++)
{
state->Reg[i] = 0;
for (j = 0; j < 7; j++)
state->RegBank[j][i] = 0;
}
for (i = 0; i < 7; i++)
state->Spsr[i] = 0;
ARMul_Reset(state);
return 0;
}
int
runOnCPU(void *cpu, void *memory,
ulong byteSize, ulong minAddr, ulong minWriteMaxExecAddr, ARMword (*run)(ARMul_State*))
{
ARMul_State* state = (ARMul_State*) cpu;
lastCPU = state;
// test whether the supplied instance is an ARMul type?
state->MemDataPtr = (unsigned char*) memory;
state->MemSize = byteSize;
minReadAddress = minAddr;
minWriteAddress = minWriteMaxExecAddr;
gdblog_index = 0;
state->EndCondition = NoError;
state->NextInstr = RESUME;
state->Reg[15] = run(state);
// collect the PSR from their dedicated flags to have easy access from the image.
ARMul_SetCPSR(state, ARMul_GetCPSR(state));
if(state->EndCondition != NoError){
return state->EndCondition;
}
return gdblog_index == 0 ? 0 : SomethingLoggedError;
}
int
singleStepCPUInSizeMinAddressReadWrite(void *cpu, void *memory,
ulong byteSize, ulong minAddr, ulong minWriteMaxExecAddr)
{
return runOnCPU(cpu, memory, byteSize, minAddr, minWriteMaxExecAddr, ARMul_DoInstr);
}
int
runCPUInSizeMinAddressReadWrite(void *cpu, void *memory,
ulong byteSize, ulong minAddr, ulong minWriteMaxExecAddr)
{
return runOnCPU(cpu, memory, byteSize, minAddr, minWriteMaxExecAddr, ARMul_DoProg);
}
/*
* Currently a dummy for ARM Processor Alien.
*/
void
flushICacheFromTo(void *cpu, ulong saddr, ulong eaddr)
{
#if 0
# error not yet implemented
#endif
}
int
gdb_log_printf(void* stream, const char * format, ...)
{
va_list arg;
int n;
va_start(arg,format);
if(stream == NULL){
n = vsnprintf((char*) (&gdb_log) + gdblog_index, LOGSIZE-gdblog_index, format, arg);
gdblog_index = gdblog_index + n;
} else {
vfprintf(stream, format, arg);
}
return 0;
}
// disassemble one instruction at laddr relative to memory, but at most memory+byteSize
int
disassembleForAtInSize(void *cpu, ulong laddr,
void *memory, ulong byteSize)
{
gdblog_index = 0;
// ignore the cpu
disassemble_info* dis = (disassemble_info*) calloc(1, sizeof(disassemble_info));
init_disassemble_info ( dis, NULL, gdb_log_printf);
dis->arch = bfd_arch_arm;
dis->mach = bfd_mach_arm_unknown;
// sets some fields in the structure dis to architecture specific values
disassemble_init_for_target( dis );
dis->buffer_vma = 0;
dis->buffer = memory;
dis->buffer_length = byteSize;
//prepend the address
gdb_log_printf(NULL, "0x%p: ", laddr);
//other possible functions are listed in opcodes/dissassemble.c
unsigned int size = print_insn_little_arm((bfd_vma) laddr, dis);
free(dis);
// zero terminate the string
gdb_log[gdblog_index+1] = 0;
return size;
}
void
forceStopRunning()
{
lastCPU->Emulate = STOP;
}
int
errorAcorn(void) { return 0; }
char *
getlog(long *len)
{
*len = gdblog_index;
return gdb_log;
}
// adding a custom Software Interrupt to the ARMulator
unsigned __real_ARMul_OSHandleSWI(ARMul_State*, ARMword);
unsigned
__wrap_ARMul_OSHandleSWI (ARMul_State * state, ARMword number)
{
switch(number)
{
case 0x200000:
// This is the SWI number which is returned by our memory interface
// if there is an instruction fetch for an illegal address.
state->Emulate = STOP;
state->EndCondition = MemoryBoundsError;
// during execution, the pc points the next fetch address, which is 8 byte after the current instruction.
gdb_log_printf(NULL, "Illegal Instruction fetch address (%#p).", state->Reg[15]-8);
return TRUE;
}
return __real_ARMul_OSHandleSWI(state, number);
}