-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
423 lines (353 loc) · 11.6 KB
/
main.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/* main.c - main line routines */
/* SimpleScalar(TM) Tool Suite
* Copyright (C) 1994-2003 by Todd M. Austin, Ph.D. and SimpleScalar, LLC.
* All Rights Reserved.
*
* THIS IS A LEGAL DOCUMENT, BY USING SIMPLESCALAR,
* YOU ARE AGREEING TO THESE TERMS AND CONDITIONS.
*
* No portion of this work may be used by any commercial entity, or for any
* commercial purpose, without the prior, written permission of SimpleScalar,
* LLC ([email protected]). Nonprofit and noncommercial use is permitted
* as described below.
*
* 1. SimpleScalar is provided AS IS, with no warranty of any kind, express
* or implied. The user of the program accepts full responsibility for the
* application of the program and the use of any results.
*
* 2. Nonprofit and noncommercial use is encouraged. SimpleScalar may be
* downloaded, compiled, executed, copied, and modified solely for nonprofit,
* educational, noncommercial research, and noncommercial scholarship
* purposes provided that this notice in its entirety accompanies all copies.
* Copies of the modified software can be delivered to persons who use it
* solely for nonprofit, educational, noncommercial research, and
* noncommercial scholarship purposes provided that this notice in its
* entirety accompanies all copies.
*
* 3. ALL COMMERCIAL USE, AND ALL USE BY FOR PROFIT ENTITIES, IS EXPRESSLY
* PROHIBITED WITHOUT A LICENSE FROM SIMPLESCALAR, LLC ([email protected]).
*
* 4. No nonprofit user may place any restrictions on the use of this software,
* including as modified by the user, by any other authorized user.
*
* 5. Noncommercial and nonprofit users may distribute copies of SimpleScalar
* in compiled or executable form as set forth in Section 2, provided that
* either: (A) it is accompanied by the corresponding machine-readable source
* code, or (B) it is accompanied by a written offer, with no time limit, to
* give anyone a machine-readable copy of the corresponding source code in
* return for reimbursement of the cost of distribution. This written offer
* must permit verbatim duplication by anyone, or (C) it is distributed by
* someone who received only the executable form, and is accompanied by a
* copy of the written offer of source code.
*
* 6. SimpleScalar was developed by Todd M. Austin, Ph.D. The tool suite is
* currently maintained by SimpleScalar LLC ([email protected]). US Mail:
* 2395 Timbercrest Court, Ann Arbor, MI 48105.
*
* Copyright (C) 1994-2003 by Todd M. Austin, Ph.D. and SimpleScalar, LLC.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <setjmp.h>
#include <signal.h>
#include <sys/types.h>
#ifndef _MSC_VER
#include <unistd.h>
#include <sys/time.h>
#endif
#ifdef BFD_LOADER
#include <bfd.h>
#endif /* BFD_LOADER */
#include "host.h"
#include "misc.h"
#include "machine.h"
#include "endian.h"
#include "version.h"
#include "dlite.h"
#include "options.h"
#include "stats.h"
#include "loader.h"
#include "sim.h"
/* stats signal handler */
static void
signal_sim_stats(int sigtype)
{
sim_dump_stats = TRUE;
}
/* exit signal handler */
static void
signal_exit_now(int sigtype)
{
sim_exit_now = TRUE;
}
/* execution instruction counter */
counter_t sim_num_insn = 0;
#if 0 /* not portable... :-( */
/* total simulator (data) memory usage */
unsigned int sim_mem_usage = 0;
#endif
/* execution start/end times */
time_t sim_start_time;
time_t sim_end_time;
int sim_elapsed_time;
/* byte/word swapping required to execute target executable on this host */
int sim_swap_bytes;
int sim_swap_words;
/* exit when this becomes non-zero */
int sim_exit_now = FALSE;
/* longjmp here when simulation is completed */
jmp_buf sim_exit_buf;
/* set to non-zero when simulator should dump statistics */
int sim_dump_stats = FALSE;
/* options database */
struct opt_odb_t *sim_odb;
/* stats database */
struct stat_sdb_t *sim_sdb;
/* EIO interfaces */
char *sim_eio_fname = NULL;
char *sim_chkpt_fname = NULL;
FILE *sim_eio_fd = NULL;
/* redirected program/simulator output file names */
static char *sim_simout = NULL;
static char *sim_progout = NULL;
FILE *sim_progfd = NULL;
/* track first argument orphan, this is the program to execute */
static int exec_index = -1;
/* dump help information */
static int help_me;
/* random number generator seed */
static int rand_seed;
/* initialize and quit immediately */
static int init_quit;
#ifndef _MSC_VER
/* simulator scheduling priority */
static int nice_priority;
#endif
/* default simulator scheduling priority */
#define NICE_DEFAULT_VALUE 0
static int
orphan_fn(int i, int argc, char **argv)
{
exec_index = i;
return /* done */FALSE;
}
static void
banner(FILE *fd, int argc, char **argv)
{
char *s;
fprintf(fd,
"%s: SimpleScalar/%s Tool Set version %d.%d of %s.\n"
"Copyright (c) 1994-2003 by Todd M. Austin, Ph.D. and SimpleScalar, LLC.\n"
"All Rights Reserved. This version of SimpleScalar is licensed for academic\n"
"non-commercial use. No portion of this work may be used by any commercial\n"
"entity, or for any commercial purpose, without the prior written permission\n"
"of SimpleScalar, LLC ([email protected]).\n"
"\n",
((s = strrchr(argv[0], '/')) ? s+1 : argv[0]),
VER_TARGET, VER_MAJOR, VER_MINOR, VER_UPDATE);
}
static void
usage(FILE *fd, int argc, char **argv)
{
fprintf(fd, "Usage: %s {-options} executable {arguments}\n", argv[0]);
opt_print_help(sim_odb, fd);
}
static int running = FALSE;
/* print all simulator stats */
void
sim_print_stats(FILE *fd) /* output stream */
{
#if 0 /* not portable... :-( */
extern char etext, *sbrk(int);
#endif
if (!running)
return;
/* get stats time */
sim_end_time = time((time_t *)NULL);
sim_elapsed_time = MAX(sim_end_time - sim_start_time, 1);
#if 0 /* not portable... :-( */
/* compute simulator memory usage */
sim_mem_usage = (sbrk(0) - &etext) / 1024;
#endif
/* print simulation stats */
fprintf(fd, "\nsim: ** simulation statistics **\n");
stat_print_stats(sim_sdb, fd);
sim_aux_stats(fd);
fprintf(fd, "\n");
}
/* print stats, uninitialize simulator components, and exit w/ exitcode */
static void
exit_now(int exit_code)
{
/* print simulation stats */
sim_print_stats(stderr);
/* un-initialize the simulator */
sim_uninit();
/* all done! */
exit(exit_code);
}
int
main(int argc, char **argv, char **envp)
{
char *s;
int i, exit_code;
#ifndef _MSC_VER
/* catch SIGUSR1 and dump intermediate stats */
signal(SIGUSR1, signal_sim_stats);
/* catch SIGUSR2 and dump final stats and exit */
signal(SIGUSR2, signal_exit_now);
#endif /* _MSC_VER */
/* register an error handler */
fatal_hook(sim_print_stats);
/* set up a non-local exit point */
if ((exit_code = setjmp(sim_exit_buf)) != 0)
{
/* special handling as longjmp cannot pass 0 */
exit_now(exit_code-1);
}
/* register global options */
sim_odb = opt_new(orphan_fn);
opt_reg_flag(sim_odb, "-h", "print help message",
&help_me, /* default */FALSE, /* !print */FALSE, NULL);
opt_reg_flag(sim_odb, "-v", "verbose operation",
&verbose, /* default */FALSE, /* !print */FALSE, NULL);
#ifdef DEBUG
opt_reg_flag(sim_odb, "-d", "enable debug message",
&debugging, /* default */FALSE, /* !print */FALSE, NULL);
#endif /* DEBUG */
opt_reg_flag(sim_odb, "-i", "start in Dlite debugger",
&dlite_active, /* default */FALSE, /* !print */FALSE, NULL);
opt_reg_int(sim_odb, "-seed",
"random number generator seed (0 for timer seed)",
&rand_seed, /* default */1, /* print */TRUE, NULL);
opt_reg_flag(sim_odb, "-q", "initialize and terminate immediately",
&init_quit, /* default */FALSE, /* !print */FALSE, NULL);
opt_reg_string(sim_odb, "-chkpt", "restore EIO trace execution from <fname>",
&sim_chkpt_fname, /* default */NULL, /* !print */FALSE, NULL);
/* stdio redirection options */
opt_reg_string(sim_odb, "-redir:sim",
"redirect simulator output to file (non-interactive only)",
&sim_simout,
/* default */NULL, /* !print */FALSE, NULL);
opt_reg_string(sim_odb, "-redir:prog",
"redirect simulated program output to file",
&sim_progout, /* default */NULL, /* !print */FALSE, NULL);
#ifndef _MSC_VER
/* scheduling priority option */
opt_reg_int(sim_odb, "-nice",
"simulator scheduling priority", &nice_priority,
/* default */NICE_DEFAULT_VALUE, /* print */TRUE, NULL);
#endif
/* FIXME: add stats intervals and max insts... */
/* register all simulator-specific options */
sim_reg_options(sim_odb);
/* parse simulator options */
exec_index = -1;
opt_process_options(sim_odb, argc, argv);
/* redirect I/O? */
if (sim_simout != NULL)
{
/* send simulator non-interactive output (STDERR) to file SIM_SIMOUT */
fflush(stderr);
if (!freopen(sim_simout, "w", stderr))
fatal("unable to redirect simulator output to file `%s'", sim_simout);
}
if (sim_progout != NULL)
{
/* redirect simulated program output to file SIM_PROGOUT */
sim_progfd = fopen(sim_progout, "w");
if (!sim_progfd)
fatal("unable to redirect program output to file `%s'", sim_progout);
}
/* need at least two argv values to run */
if (argc < 2)
{
banner(stderr, argc, argv);
usage(stderr, argc, argv);
exit(1);
}
/* opening banner */
banner(stderr, argc, argv);
if (help_me)
{
/* print help message and exit */
usage(stderr, argc, argv);
exit(1);
}
/* seed the random number generator */
if (rand_seed == 0)
{
/* seed with the timer value, true random */
mysrand(time((time_t *)NULL));
}
else
{
/* seed with default or user-specified random number generator seed */
mysrand(rand_seed);
}
/* exec_index is set in orphan_fn() */
if (exec_index == -1)
{
/* executable was not found */
fprintf(stderr, "error: no executable specified\n");
usage(stderr, argc, argv);
exit(1);
}
/* else, exec_index points to simulated program arguments */
/* check simulator-specific options */
sim_check_options(sim_odb, argc, argv);
#ifndef _MSC_VER
/* set simulator scheduling priority */
if (nice(0) < nice_priority)
{
if (nice(nice_priority - nice(0)) < 0)
fatal("could not renice simulator process");
}
#endif
/* default architected value... */
sim_num_insn = 0;
#ifdef BFD_LOADER
/* initialize the bfd library */
bfd_init();
#endif /* BFD_LOADER */
/* initialize the instruction decoder */
md_init_decoder();
/* initialize all simulation modules */
sim_init();
/* initialize architected state */
sim_load_prog(argv[exec_index], argc-exec_index, argv+exec_index, envp);
/* register all simulator stats */
sim_sdb = stat_new();
sim_reg_stats(sim_sdb);
#if 0 /* not portable... :-( */
stat_reg_uint(sim_sdb, "sim_mem_usage",
"total simulator (data) memory usage",
&sim_mem_usage, sim_mem_usage, "%11dk");
#endif
/* record start of execution time, used in rate stats */
sim_start_time = time((time_t *)NULL);
/* emit the command line for later reuse */
fprintf(stderr, "sim: command line: ");
for (i=0; i < argc; i++)
fprintf(stderr, "%s ", argv[i]);
fprintf(stderr, "\n");
/* output simulation conditions */
s = ctime(&sim_start_time);
if (s[strlen(s)-1] == '\n')
s[strlen(s)-1] = '\0';
fprintf(stderr, "\nsim: simulation started @ %s, options follow:\n", s);
opt_print_options(sim_odb, stderr, /* short */TRUE, /* notes */TRUE);
sim_aux_config(stderr);
fprintf(stderr, "\n");
/* omit option dump time from rate stats */
sim_start_time = time((time_t *)NULL);
if (init_quit)
exit_now(0);
running = TRUE;
sim_main();
/* simulation finished early */
exit_now(0);
return 0;
}