-
Notifications
You must be signed in to change notification settings - Fork 0
/
eio.c
executable file
·271 lines (232 loc) · 8.21 KB
/
eio.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
/* eio.c - external interfaces to external I/O files */
/* 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 (info@simplescalar.com). 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 (info@simplescalar.com).
*
* 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 (info@simplescalar.com). US Mail:
* 2395 Timbercrest Court, Ann Arbor, MI 48105.
*
* Copyright (C) 1994-2003 by Todd M. Austin, Ph.D. and SimpleScalar, LLC.
*/
#include<iostream>
#include<fstream>
#include<cstdio>
#include<cstdlib>
#ifdef _MSC_VER
#include <io.h>
#else /* !_MSC_VER */
#include <unistd.h>
#endif
#include "host.h"
#include "misc.h"
#include "machine.h"
#include "regs.h"
#include "memory.h"
#include "loader.h"
#include "syscall.h"
#include "sim.h"
#include "endian.h"
#include "eio.h"
#ifdef _MSC_VER
#define write _write
#endif
#define EIO_FILE_HEADER "/* This is a SimpleScalar EIO file - DO NOT MOVE OR EDIT THIS LINE! */\n"
//EIO transaction count, i.e., number of last transaction completed
counter_t eio_trans_icnt = -1;
int eio_open(std::string fname, std::ifstream & in)
{
int target_big_endian = (endian_host_byte_order() == endian_big);
in.open(fname.c_str());
if(!in.is_open())
{
fatal("unable to open EIO file `%s'", fname.c_str());
}
std::string format_header("/* file_format: 2, file_version: 3, big_endian: 0 */");
std::string format_header_4("/* file_format: 2, file_version: 4, big_endian: 0 */");
std::string start_header("/* ** start checkpoint @ -1... */");
std::string buf;
getline(in,buf);
buf += '\n';
if(buf!=EIO_FILE_HEADER)
{
std::cerr << "Failed reading EIO file header, read: " << buf << std::endl;
std::cerr << "Should have read: " << EIO_FILE_HEADER << std::endl;
exit(-1);
}
getline(in,buf);
getline(in,buf);
if(buf!=format_header && buf!=format_header_4)
{
std::cerr << "Failed reading format header, read: " << buf << std::endl;
std::cerr << "Should have read: " << format_header << std::endl;
std::cerr << "or: " << format_header_4 << std::endl;
exit(-1);
}
int file_format, file_version, big_endian;
char c_buf;
in >> c_buf >> file_format >> c_buf >> file_version >> c_buf >> big_endian;
getline(in,buf);
if(!in.good())
{
fatal("could not read EIO file header");
}
getline(in,buf);
getline(in,buf);
if(buf!=start_header)
{
std::cerr << "Failed reading start header, read: " << buf << std::endl;
std::cerr << "Should have read: " << start_header << std::endl;
exit(-1);
}
getline(in,buf);
if(file_format != MD_EIO_FILE_FORMAT)
{
fatal("EIO file `%s' has incompatible format", fname.c_str());
}
if((file_version != EIO_FILE_VERSION) && (file_version != EIO_FILE_VERSION_NEW))
{
fatal("EIO file `%s' has incompatible version", fname.c_str());
}
if(!!big_endian != !!target_big_endian)
{
warn("endian of `%s' does not match host", fname.c_str());
warn("running with experimental cross-endian execution support");
warn("****************************************");
warn("**>> please check results carefully <<**");
warn("****************************************");
}
return file_version;
}
//returns non-zero if file FNAME has a valid EIO header
int eio_valid(const char *fname)
{
char buf[512];
//open possible EIO file
FILE *fd = gzopen(fname, "r");
if(!fd)
{
return FALSE;
}
//read and check EIO file header
if(!fgets(buf, 512, fd))
return FALSE;
//all done, close up file
gzclose(fd);
//check the header
if(strcmp(buf, EIO_FILE_HEADER))
{
return FALSE;
}
//else, has a valid header, go with it...
return TRUE;
}
void eio_close(FILE *fd)
{
gzclose(fd);
}
//check point current architected state to stream FD, returns EIO transaction count (an EIO file pointer)
counter_t eio_write_chkpt(regs_t *regs, //regs to dump
mem_t *mem, //memory to dump
std::string filename, //filename to write to
counter_t & sim_num_insn) //Reference to number of instructions executed
{
std::ofstream outfile(filename.c_str());
if(!outfile.is_open())
{
warn("Could not open %s for eio checkpoint creation",filename.c_str());
return -1;
}
//These could be const, maybe not file_version which we will allow to be 4.
int file_format(2), file_version(4), big_endian(0);
eio_trans_icnt = -1;
outfile << "/* This is a SimpleScalar EIO file - DO NOT MOVE OR EDIT THIS LINE! */\n\n";
outfile << "/* file_format: " << file_format << ", file_version: " << file_version << ", big_endian: " << big_endian << " */\n";
outfile << "(" << file_format << ", " << file_version << ", " << big_endian << ")\n\n";
outfile << "/* ** start checkpoint @ " << eio_trans_icnt << "... */\n\n";
outfile << "/* EIO file pointer: -1... */\n";
//Needed to be -1 in old case, doesn't anymore, but retaining for now.
outfile << (unsigned long long)(-1);
outfile << "\n\n/* Translation Table */" << std::endl;
outfile << contexts[regs->context_id].file_table << std::endl;
outfile << (*regs);
outfile << mem;
outfile << "/* ** end checkpoint @ " << eio_trans_icnt << "... */\n\n";
outfile.close();
return eio_trans_icnt;
}
//read check point of architected state from stream FD, returns EIO transaction count (an EIO file pointer)
counter_t eio_read_chkpt(regs_t *regs, //regs to dump
mem_t *mem, //memory to dump
std::ifstream & in,
counter_t & sim_num_insn, //Reference to number of instructions executed
int version)
{
std::string eio_pointer_header("/* EIO file pointer: -1... */");
std::string buf;
std::getline(in,buf);
if(buf!=eio_pointer_header)
{
std::cerr << "Failed reading EIO file pointer, read: " << buf << std::endl;
std::cerr << "Wanted to read: " << eio_pointer_header << std::endl;
exit(-1);
}
unsigned long long trans_icnt;
in >> trans_icnt;
if(trans_icnt!=(unsigned long long)-1)
{
std::cerr << "Failed reading trans_icnt, read: " << trans_icnt << "\nWanted to read: " << (unsigned long long)(-1) << std::endl;
exit(-1);
}
std::getline(in,buf);
std::getline(in,buf);
if(version == EIO_FILE_VERSION_NEW)
{
std::getline(in,buf);
assert(buf=="/* Translation Table */");
in >> contexts[num_contexts].file_table;
std::getline(in,buf);
std::getline(in,buf);
}
in >> contexts[num_contexts].regs;
in >> contexts[num_contexts].mem;
in.close();
return trans_icnt;
}