m5.c (9457:a4739b6f799d) m5.c (9898:2935441b0870)
1/*
2 * Copyright (c) 2011 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Copyright (c) 2003-2005 The Regents of The University of Michigan
15 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 * Authors: Nathan Binkert
41 */
42
43#ifdef linux
44#define _GNU_SOURCE
45#include <sched.h>
46#endif
47
48#include <err.h>
49#include <fcntl.h>
50#include <inttypes.h>
51#include <stdio.h>
52#include <stdlib.h>
53#include <string.h>
1/*
2 * Copyright (c) 2011 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Copyright (c) 2003-2005 The Regents of The University of Michigan
15 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 * Authors: Nathan Binkert
41 */
42
43#ifdef linux
44#define _GNU_SOURCE
45#include <sched.h>
46#endif
47
48#include <err.h>
49#include <fcntl.h>
50#include <inttypes.h>
51#include <stdio.h>
52#include <stdlib.h>
53#include <string.h>
54#include <sys/mman.h>
55#include <sys/stat.h>
56#include <sys/types.h>
54#include <unistd.h>
55
56#include "m5op.h"
57
57#include <unistd.h>
58
59#include "m5op.h"
60
61void *m5_mem = NULL;
62
58char *progname;
59char *command = "unspecified";
60void usage();
61
62void
63parse_int_args(int argc, char *argv[], uint64_t ints[], int len)
64{
65 if (argc > len)
66 usage();
67
68// On 32 bit platforms we need to use strtoull to do the conversion
69#ifdef __LP64__
70#define strto64 strtoul
71#else
72#define strto64 strtoull
73#endif
74 int i;
75 for (i = 0; i < len; ++i)
76 ints[i] = (i < argc) ? strto64(argv[i], NULL, 0) : 0;
77
78#undef strto64
79}
80
81int
82read_file(int dest_fid)
83{
84 char buf[256*1024];
85 int offset = 0;
86 int len;
87
88 // Touch all buffer pages to ensure they are mapped in the
89 // page table. This is required in the case of X86_FS, where
90 // Linux does demand paging.
91 memset(buf, 0, sizeof(buf));
92
93 while ((len = m5_readfile(buf, sizeof(buf), offset)) > 0) {
94 write(dest_fid, buf, len);
95 offset += len;
96 }
97}
98
99int
100write_file(const char *filename)
101{
102 fprintf(stderr, "opening %s\n", filename);
103 int src_fid = open(filename, O_RDONLY);
104
105 if (src_fid < 0) {
106 fprintf(stderr, "error opening %s\n", filename);
107 return;
108 }
109
110 char buf[256*1024];
111 int offset = 0;
112 int len;
113 int bytes = 0;
114
115 memset(buf, 0, sizeof(buf));
116
117 while ((len = read(src_fid, buf, sizeof(buf))) > 0) {
118 bytes += m5_writefile(buf, len, offset, filename);
119 offset += len;
120 }
121 fprintf(stderr, "written %d bytes\n", bytes);
122
123 close(src_fid);
124}
125
126void
127do_exit(int argc, char *argv[])
128{
129 if (argc > 1)
130 usage();
131
132 uint64_t ints[1];
133 parse_int_args(argc, argv, ints, 1);
134 m5_exit(ints[0]);
135}
136
137void
138do_fail(int argc, char *argv[])
139{
140 if (argc < 1 || argc > 2)
141 usage();
142
143 m5_fail((argc > 1) ? strtoul(argv[1], NULL, 0) : 0, strtoul(argv[0], NULL, 0));
144}
145
146void
147do_reset_stats(int argc, char *argv[])
148{
149 uint64_t ints[2];
150 parse_int_args(argc, argv, ints, 2);
151 m5_reset_stats(ints[0], ints[1]);
152}
153
154void
155do_dump_stats(int argc, char *argv[])
156{
157 uint64_t ints[2];
158 parse_int_args(argc, argv, ints, 2);
159 m5_dump_stats(ints[0], ints[1]);
160}
161
162void
163do_dump_reset_stats(int argc, char *argv[])
164{
165 uint64_t ints[2];
166 parse_int_args(argc, argv, ints, 2);
167 m5_dumpreset_stats(ints[0], ints[1]);
168}
169
170void
171do_read_file(int argc, char *argv[])
172{
173 if (argc > 0)
174 usage();
175
176 read_file(STDOUT_FILENO);
177}
178
179void
180do_write_file(int argc, char *argv[])
181{
182 if (argc != 1)
183 usage();
184
185 const char *filename = argv[0];
186
187 write_file(filename);
188}
189
190void
191do_exec_file(int argc, char *argv[])
192{
193 if (argc > 0)
194 usage();
195
196 const char *destname = "/tmp/execfile";
197
198 int fid = open(destname, O_WRONLY, 0777);
199 int len = read_file(fid);
200 close(fid);
201 if (len > 0) {
202 execl(destname, "execfile", NULL);
203 err(1, "execl failed!");
204 }
205}
206
207void
208do_checkpoint(int argc, char *argv[])
209{
210 uint64_t ints[2];
211 parse_int_args(argc, argv, ints, 2);
212 m5_checkpoint(ints[0], ints[1]);
213}
214
215void
216do_load_symbol(int argc, char *argv[])
217{
218 if (argc != 2)
219 usage();
220
221 uint64_t addr = strtoul(argv[0], NULL, 0);
222 char *symbol = argv[1];
223 m5_loadsymbol(addr, symbol);
224}
225
226void
227do_initparam(int argc, char *argv[])
228{
229 if (argc != 0)
230 usage();
231
232 uint64_t val = m5_initparam();
233 printf("%"PRIu64, val);
234}
235
236void
237do_sw99param(int argc, char *argv[])
238{
239 if (argc != 0)
240 usage();
241
242 uint64_t param = m5_initparam();
243
244 // run-time, rampup-time, rampdown-time, warmup-time, connections
245 printf("%"PRId64" %"PRId64" %"PRId64" %"PRId64" %"PRId64,
246 (param >> 48) & 0xfff,
247 (param >> 36) & 0xfff, (param >> 24) & 0xfff,
248 (param >> 12) & 0xfff, (param >> 0) & 0xfff);
249}
250
251#ifdef linux
252void
253do_pin(int argc, char *argv[])
254{
255 if (argc < 2)
256 usage();
257
258 cpu_set_t mask;
259 CPU_ZERO(&mask);
260
261 const char *sep = ",";
262 char *target = strtok(argv[0], sep);
263 while (target) {
264 CPU_SET(atoi(target), &mask);
265 target = strtok(NULL, sep);
266 }
267
268 if (sched_setaffinity(0, sizeof(cpu_set_t), &mask) < 0)
269 err(1, "setaffinity");
270
271 execvp(argv[1], &argv[1]);
272 err(1, "execvp failed!");
273}
274#endif
275
276struct MainFunc
277{
278 char *name;
279 void (*func)(int argc, char *argv[]);
280 char *usage;
281};
282
283struct MainFunc mainfuncs[] = {
284 { "exit", do_exit, "[delay]" },
285 { "fail", do_fail, "<code> [delay]" },
286 { "resetstats", do_reset_stats, "[delay [period]]" },
287 { "dumpstats", do_dump_stats, "[delay [period]]" },
288 { "dumpresetstats", do_dump_reset_stats, "[delay [period]]" },
289 { "readfile", do_read_file, "" },
290 { "writefile", do_write_file, "<filename>" },
291 { "execfile", do_exec_file, "" },
292 { "checkpoint", do_checkpoint, "[delay [period]]" },
293 { "loadsymbol", do_load_symbol, "<address> <symbol>" },
294 { "initparam", do_initparam, "" },
295 { "sw99param", do_sw99param, "" },
296#ifdef linux
297 { "pin", do_pin, "<cpu> <program> [args ...]" }
298#endif
299};
300int numfuncs = sizeof(mainfuncs) / sizeof(mainfuncs[0]);
301
302void
303usage()
304{
305 int i;
306
307 for (i = 0; i < numfuncs; ++i) {
308 char *header = i ? "" : "usage:";
309 fprintf(stderr, "%-6s %s %s %s\n",
310 header, progname, mainfuncs[i].name, mainfuncs[i].usage);
311 }
312 fprintf(stderr, "\n");
313 fprintf(stderr, "All times in nanoseconds!\n");
314
315 exit(1);
316}
317
63char *progname;
64char *command = "unspecified";
65void usage();
66
67void
68parse_int_args(int argc, char *argv[], uint64_t ints[], int len)
69{
70 if (argc > len)
71 usage();
72
73// On 32 bit platforms we need to use strtoull to do the conversion
74#ifdef __LP64__
75#define strto64 strtoul
76#else
77#define strto64 strtoull
78#endif
79 int i;
80 for (i = 0; i < len; ++i)
81 ints[i] = (i < argc) ? strto64(argv[i], NULL, 0) : 0;
82
83#undef strto64
84}
85
86int
87read_file(int dest_fid)
88{
89 char buf[256*1024];
90 int offset = 0;
91 int len;
92
93 // Touch all buffer pages to ensure they are mapped in the
94 // page table. This is required in the case of X86_FS, where
95 // Linux does demand paging.
96 memset(buf, 0, sizeof(buf));
97
98 while ((len = m5_readfile(buf, sizeof(buf), offset)) > 0) {
99 write(dest_fid, buf, len);
100 offset += len;
101 }
102}
103
104int
105write_file(const char *filename)
106{
107 fprintf(stderr, "opening %s\n", filename);
108 int src_fid = open(filename, O_RDONLY);
109
110 if (src_fid < 0) {
111 fprintf(stderr, "error opening %s\n", filename);
112 return;
113 }
114
115 char buf[256*1024];
116 int offset = 0;
117 int len;
118 int bytes = 0;
119
120 memset(buf, 0, sizeof(buf));
121
122 while ((len = read(src_fid, buf, sizeof(buf))) > 0) {
123 bytes += m5_writefile(buf, len, offset, filename);
124 offset += len;
125 }
126 fprintf(stderr, "written %d bytes\n", bytes);
127
128 close(src_fid);
129}
130
131void
132do_exit(int argc, char *argv[])
133{
134 if (argc > 1)
135 usage();
136
137 uint64_t ints[1];
138 parse_int_args(argc, argv, ints, 1);
139 m5_exit(ints[0]);
140}
141
142void
143do_fail(int argc, char *argv[])
144{
145 if (argc < 1 || argc > 2)
146 usage();
147
148 m5_fail((argc > 1) ? strtoul(argv[1], NULL, 0) : 0, strtoul(argv[0], NULL, 0));
149}
150
151void
152do_reset_stats(int argc, char *argv[])
153{
154 uint64_t ints[2];
155 parse_int_args(argc, argv, ints, 2);
156 m5_reset_stats(ints[0], ints[1]);
157}
158
159void
160do_dump_stats(int argc, char *argv[])
161{
162 uint64_t ints[2];
163 parse_int_args(argc, argv, ints, 2);
164 m5_dump_stats(ints[0], ints[1]);
165}
166
167void
168do_dump_reset_stats(int argc, char *argv[])
169{
170 uint64_t ints[2];
171 parse_int_args(argc, argv, ints, 2);
172 m5_dumpreset_stats(ints[0], ints[1]);
173}
174
175void
176do_read_file(int argc, char *argv[])
177{
178 if (argc > 0)
179 usage();
180
181 read_file(STDOUT_FILENO);
182}
183
184void
185do_write_file(int argc, char *argv[])
186{
187 if (argc != 1)
188 usage();
189
190 const char *filename = argv[0];
191
192 write_file(filename);
193}
194
195void
196do_exec_file(int argc, char *argv[])
197{
198 if (argc > 0)
199 usage();
200
201 const char *destname = "/tmp/execfile";
202
203 int fid = open(destname, O_WRONLY, 0777);
204 int len = read_file(fid);
205 close(fid);
206 if (len > 0) {
207 execl(destname, "execfile", NULL);
208 err(1, "execl failed!");
209 }
210}
211
212void
213do_checkpoint(int argc, char *argv[])
214{
215 uint64_t ints[2];
216 parse_int_args(argc, argv, ints, 2);
217 m5_checkpoint(ints[0], ints[1]);
218}
219
220void
221do_load_symbol(int argc, char *argv[])
222{
223 if (argc != 2)
224 usage();
225
226 uint64_t addr = strtoul(argv[0], NULL, 0);
227 char *symbol = argv[1];
228 m5_loadsymbol(addr, symbol);
229}
230
231void
232do_initparam(int argc, char *argv[])
233{
234 if (argc != 0)
235 usage();
236
237 uint64_t val = m5_initparam();
238 printf("%"PRIu64, val);
239}
240
241void
242do_sw99param(int argc, char *argv[])
243{
244 if (argc != 0)
245 usage();
246
247 uint64_t param = m5_initparam();
248
249 // run-time, rampup-time, rampdown-time, warmup-time, connections
250 printf("%"PRId64" %"PRId64" %"PRId64" %"PRId64" %"PRId64,
251 (param >> 48) & 0xfff,
252 (param >> 36) & 0xfff, (param >> 24) & 0xfff,
253 (param >> 12) & 0xfff, (param >> 0) & 0xfff);
254}
255
256#ifdef linux
257void
258do_pin(int argc, char *argv[])
259{
260 if (argc < 2)
261 usage();
262
263 cpu_set_t mask;
264 CPU_ZERO(&mask);
265
266 const char *sep = ",";
267 char *target = strtok(argv[0], sep);
268 while (target) {
269 CPU_SET(atoi(target), &mask);
270 target = strtok(NULL, sep);
271 }
272
273 if (sched_setaffinity(0, sizeof(cpu_set_t), &mask) < 0)
274 err(1, "setaffinity");
275
276 execvp(argv[1], &argv[1]);
277 err(1, "execvp failed!");
278}
279#endif
280
281struct MainFunc
282{
283 char *name;
284 void (*func)(int argc, char *argv[]);
285 char *usage;
286};
287
288struct MainFunc mainfuncs[] = {
289 { "exit", do_exit, "[delay]" },
290 { "fail", do_fail, "<code> [delay]" },
291 { "resetstats", do_reset_stats, "[delay [period]]" },
292 { "dumpstats", do_dump_stats, "[delay [period]]" },
293 { "dumpresetstats", do_dump_reset_stats, "[delay [period]]" },
294 { "readfile", do_read_file, "" },
295 { "writefile", do_write_file, "<filename>" },
296 { "execfile", do_exec_file, "" },
297 { "checkpoint", do_checkpoint, "[delay [period]]" },
298 { "loadsymbol", do_load_symbol, "<address> <symbol>" },
299 { "initparam", do_initparam, "" },
300 { "sw99param", do_sw99param, "" },
301#ifdef linux
302 { "pin", do_pin, "<cpu> <program> [args ...]" }
303#endif
304};
305int numfuncs = sizeof(mainfuncs) / sizeof(mainfuncs[0]);
306
307void
308usage()
309{
310 int i;
311
312 for (i = 0; i < numfuncs; ++i) {
313 char *header = i ? "" : "usage:";
314 fprintf(stderr, "%-6s %s %s %s\n",
315 header, progname, mainfuncs[i].name, mainfuncs[i].usage);
316 }
317 fprintf(stderr, "\n");
318 fprintf(stderr, "All times in nanoseconds!\n");
319
320 exit(1);
321}
322
323static void
324map_m5_mem()
325{
326#ifdef M5OP_ADDR
327 int fd;
328
329 fd = open("/dev/mem", O_RDWR | O_SYNC);
330 if (fd == -1) {
331 perror("Can't open /dev/mem");
332 exit(1);
333 }
334
335 m5_mem = mmap(NULL, 0x10000, PROT_READ | PROT_WRITE, MAP_SHARED, fd, M5OP_ADDR);
336 if (!m5_mem) {
337 perror("Can't mmap /dev/mem");
338 exit(1);
339 }
340#endif
341}
342
318int
319main(int argc, char *argv[])
320{
321 progname = argv[0];
322 if (argc < 2)
323 usage(1);
324
343int
344main(int argc, char *argv[])
345{
346 progname = argv[0];
347 if (argc < 2)
348 usage(1);
349
350 map_m5_mem();
351
325 command = argv[1];
326
327 argv += 2;
328 argc -= 2;
329
330 int i;
331 for (i = 0; i < numfuncs; ++i) {
332 if (strcmp(command, mainfuncs[i].name) != 0)
333 continue;
334
335 mainfuncs[i].func(argc, argv);
336 exit(0);
337 }
338
339 usage(1);
340}
352 command = argv[1];
353
354 argv += 2;
355 argc -= 2;
356
357 int i;
358 for (i = 0; i < numfuncs; ++i) {
359 if (strcmp(command, mainfuncs[i].name) != 0)
360 continue;
361
362 mainfuncs[i].func(argc, argv);
363 exit(0);
364 }
365
366 usage(1);
367}