This repository has been archived by the owner on Jun 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
arm_simulator.c
225 lines (205 loc) · 7.29 KB
/
arm_simulator.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
/*
Armator - simulateur de jeu d'instruction ARMv5T � but p�dagogique
Copyright (C) 2011 Guillaume Huard
Ce programme est libre, vous pouvez le redistribuer et/ou le modifier selon les
termes de la Licence Publique G�n�rale GNU publi�e par la Free Software
Foundation (version 2 ou bien toute autre version ult�rieure choisie par vous).
Ce programme est distribu� car potentiellement utile, mais SANS AUCUNE
GARANTIE, ni explicite ni implicite, y compris les garanties de
commercialisation ou d'adaptation dans un but sp�cifique. Reportez-vous � la
Licence Publique G�n�rale GNU pour plus de d�tails.
Vous devez avoir re�u une copie de la Licence Publique G�n�rale GNU en m�me
temps que ce programme ; si ce n'est pas le cas, �crivez � la Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,
�tats-Unis.
Contact: [email protected]
B�timent IMAG
700 avenue centrale, domaine universitaire
38401 Saint Martin d'H�res
*/
#include <sys/socket.h>
#include <pthread.h>
#include <getopt.h>
#include "csapp.h"
#include "scanner.h"
#include "arm.h"
#include "memory.h"
#include "gdb_protocol.h"
#include "trace.h"
#include "debug.h"
struct shared_data
{
registers reg;
memory mem;
arm_core arm;
pthread_mutex_t lock;
in_port_t gdb_port, irq_port;
};
struct server_data
{
int socket;
unsigned short port;
};
static struct server_data create_server(in_port_t port)
{
struct sockaddr_in addr;
socklen_t addr_length;
struct server_data result;
int option_value = 1;
result.socket = Socket(PF_INET, SOCK_STREAM, 0);
Setsockopt(result.socket, SOL_SOCKET, SO_REUSEADDR, &option_value, sizeof(option_value));
bzero(&addr, sizeof(addr));
addr.sin_port = htons(port);
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
Bind(result.socket, (struct sockaddr *)&addr, sizeof(addr));
addr_length = sizeof(addr);
getsockname(result.socket, (struct sockaddr *)&addr, &addr_length);
result.port = ntohs(addr.sin_port);
Listen(result.socket, 1);
return result;
}
static void *gdb_listener(void *arg)
{
struct shared_data *shared = (struct shared_data *)arg;
struct sockaddr_in peer;
int connection;
socklen_t peer_length;
struct server_data server;
server = create_server(shared->gdb_port);
fprintf(stderr, "Listening to gdb connection on port %d\n", server.port);
peer_length = sizeof(peer);
connection = Accept(server.socket, (struct sockaddr *)&peer, &peer_length);
gdb_scanner(shared->arm, shared->reg, shared->mem, connection, connection, &shared->lock);
shutdown(connection, SHUT_RDWR);
close(server.socket);
pthread_exit(NULL);
}
static void *irq_listener(void *arg)
{
struct shared_data *shared = (struct shared_data *)arg;
struct sockaddr_in peer;
int connection;
socklen_t peer_length;
struct server_data server;
unsigned char irq;
server = create_server(shared->irq_port);
fprintf(stderr, "Listening to irq connections on port %d\n", server.port);
while (1)
{
peer_length = sizeof(peer);
connection = Accept(server.socket, (struct sockaddr *)&peer, &peer_length);
while (Read(connection, &irq, 1) > 0)
{
pthread_mutex_lock(&shared->lock);
arm_exception(shared->arm, irq);
pthread_mutex_unlock(&shared->lock);
}
shutdown(connection, SHUT_RDWR);
}
close(server.socket);
pthread_exit(NULL);
}
void usage(char *name)
{
fprintf(stderr, "Usage:\n"
"%s [ --help ] [ --gdb-port port ] [ --irq-port port ] "
"[ --trace-file file ] [ --trace-registers ] [ --trace-memory ] "
"[ --trace-state ] [ --trace-position ] [ --debug filename ]\n\n"
"Start an ARMv5 instruction set simulator that acts as a gdb server "
"and can receive interrupts. It is possible to specify on which ports "
"the simulator listen to gdb client or irq sending program "
"connections. Trace options have the following behavior:\n"
"- trace file: file into which trace information is stored (default is"
" stdout)\n"
"- trace registers: outputs informations about each access to"
" registers\n"
"- trace memory: outputs informations about each access to memory\n"
"- trace state: outputs the processor state after each instruction\n"
"- trace position: for each traced access, outputs the file and line"
" at which the access has been performed\n"
"The debug switch enable selective reporting of debug messages on a "
"per source file basis\n",
name);
}
int main(int argc, char *argv[])
{
struct shared_data shared;
pthread_t gdb_thread;
pthread_t irq_thread;
void *result;
int opt;
FILE *trace_file;
struct option longopts[] = {
{"gdb-port", required_argument, NULL, 'g'},
{"irq-port", required_argument, NULL, 'i'},
{"trace-file", required_argument, NULL, 't'},
{"trace-registers", no_argument, NULL, 'r'},
{"trace-memory", no_argument, NULL, 'm'},
{"trace-state", required_argument, NULL, 's'},
{"trace-position", no_argument, NULL, 'p'},
{"help", no_argument, NULL, 'h'},
{"debug", required_argument, NULL, 'd'},
{NULL, 0, NULL, 0}};
shared.gdb_port = 0;
shared.irq_port = 0;
trace_file = stdout;
while ((opt = getopt_long(argc, argv, "g:i:ht:rmspd:", longopts, NULL)) != -1)
{
switch (opt)
{
case 'g':
shared.gdb_port = atoi(optarg);
break;
case 'i':
shared.irq_port = atoi(optarg);
break;
case 'h':
usage(argv[0]);
exit(0);
case 't':
trace_file = fopen(optarg, "w");
if (trace_file == NULL)
{
perror("Trace file");
exit(1);
}
break;
case 'r':
trace_add(REGISTERS);
break;
case 'm':
trace_add(MEMORY);
break;
case 's':
trace_add(arm_get_mode_number(optarg));
break;
case 'p':
trace_add(POSITION);
break;
case 'd':
add_debug_to(optarg);
break;
default:
fprintf(stderr, "Unrecognized option %c\n", opt);
usage(argv[0]);
exit(1);
}
}
gdb_init();
arm_init();
set_trace_file(trace_file);
// We allocate 32K for the memory, and we have to make sure that this is coherent with
// the examples, see in particular the linker_script.
shared.mem = memory_create(0x8000);
shared.reg = registers_create();
shared.arm = arm_create(shared.reg, shared.mem);
pthread_mutex_init(&shared.lock, NULL);
pthread_create(&gdb_thread, NULL, gdb_listener, &shared);
pthread_create(&irq_thread, NULL, irq_listener, &shared);
pthread_join(gdb_thread, &result);
arm_destroy(shared.arm);
registers_destroy(shared.reg);
memory_destroy(shared.mem);
return 0;
}