m5.c revision 11372:a5c8f7deb099
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>
57#include <unistd.h>
58
59#include "m5op.h"
60
61void *m5_mem = NULL;
62
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
86void
87parse_str_args_to_regs(int argc, char *argv[], uint64_t regs[], int len)
88{
89    if (argc > 1 || (argc > 0 && strlen(argv[0]) > len * sizeof(uint64_t)))
90        usage();
91
92    int i;
93    for (i = 0; i < len; i++)
94        regs[i] = 0;
95
96    if (argc == 0)
97        return;
98
99    int n;
100    for (n = 0, i = 0; i < len && n < strlen(argv[0]); n++) {
101        *((char *)(&regs[i]) + (n % 8)) = argv[0][n];
102        if ((n % 8) == 7)
103            i++;
104    }
105}
106
107int
108read_file(int dest_fid)
109{
110    char buf[256*1024];
111    int offset = 0;
112    int len;
113
114    // Touch all buffer pages to ensure they are mapped in the
115    // page table. This is required in the case of X86_FS, where
116    // Linux does demand paging.
117    memset(buf, 0, sizeof(buf));
118
119    while ((len = m5_readfile(buf, sizeof(buf), offset)) > 0) {
120        write(dest_fid, buf, len);
121        offset += len;
122    }
123}
124
125int
126write_file(const char *filename)
127{
128    fprintf(stderr, "opening %s\n", filename);
129    int src_fid = open(filename, O_RDONLY);
130
131    if (src_fid < 0) {
132        fprintf(stderr, "error opening %s\n", filename);
133        return;
134    }
135
136    char buf[256*1024];
137    int offset = 0;
138    int len;
139    int bytes = 0;
140
141    memset(buf, 0, sizeof(buf));
142
143    while ((len = read(src_fid, buf, sizeof(buf))) > 0) {
144        bytes += m5_writefile(buf, len, offset, filename);
145        offset += len;
146    }
147    fprintf(stderr, "written %d bytes\n", bytes);
148
149    close(src_fid);
150}
151
152void
153do_exit(int argc, char *argv[])
154{
155    if (argc > 1)
156        usage();
157
158    uint64_t ints[1];
159    parse_int_args(argc, argv, ints, 1);
160    m5_exit(ints[0]);
161}
162
163void
164do_fail(int argc, char *argv[])
165{
166    if (argc < 1 || argc > 2)
167        usage();
168
169    uint64_t ints[2] = {0,0};
170    parse_int_args(argc, argv, ints, argc);
171    m5_fail(ints[1], ints[0]);
172}
173
174void
175do_reset_stats(int argc, char *argv[])
176{
177    uint64_t ints[2];
178    parse_int_args(argc, argv, ints, 2);
179    m5_reset_stats(ints[0], ints[1]);
180}
181
182void
183do_dump_stats(int argc, char *argv[])
184{
185    uint64_t ints[2];
186    parse_int_args(argc, argv, ints, 2);
187    m5_dump_stats(ints[0], ints[1]);
188}
189
190void
191do_dump_reset_stats(int argc, char *argv[])
192{
193    uint64_t ints[2];
194    parse_int_args(argc, argv, ints, 2);
195    m5_dumpreset_stats(ints[0], ints[1]);
196}
197
198void
199do_read_file(int argc, char *argv[])
200{
201    if (argc > 0)
202        usage();
203
204    read_file(STDOUT_FILENO);
205}
206
207void
208do_write_file(int argc, char *argv[])
209{
210    if (argc != 1)
211        usage();
212
213    const char *filename = argv[0];
214
215    write_file(filename);
216}
217
218void
219do_exec_file(int argc, char *argv[])
220{
221    if (argc > 0)
222        usage();
223
224    const char *destname = "/tmp/execfile";
225
226    int fid = open(destname, O_WRONLY, 0777);
227    int len = read_file(fid);
228    close(fid);
229    if (len > 0) {
230        execl(destname, "execfile", NULL);
231        err(1, "execl failed!");
232    }
233}
234
235void
236do_checkpoint(int argc, char *argv[])
237{
238    uint64_t ints[2];
239    parse_int_args(argc, argv, ints, 2);
240    m5_checkpoint(ints[0], ints[1]);
241}
242
243void
244do_load_symbol(int argc, char *argv[])
245{
246    if (argc != 2)
247        usage();
248
249    uint64_t addr = strtoul(argv[0], NULL, 0);
250    char *symbol = argv[1];
251    m5_loadsymbol(addr, symbol);
252}
253
254void
255do_initparam(int argc, char *argv[])
256{
257    if (argc > 1)
258        usage();
259
260    uint64_t key_str[2];
261    parse_str_args_to_regs(argc, argv, key_str, 2);
262    uint64_t val = m5_initparam(key_str[0], key_str[1]);
263    printf("%"PRIu64, val);
264}
265
266void
267do_sw99param(int argc, char *argv[])
268{
269    if (argc != 0)
270        usage();
271
272    uint64_t param = m5_initparam(0, 0);
273
274    // run-time, rampup-time, rampdown-time, warmup-time, connections
275    printf("%"PRId64" %"PRId64" %"PRId64" %"PRId64" %"PRId64,
276           (param >> 48) & 0xfff,
277           (param >> 36) & 0xfff, (param >> 24) & 0xfff,
278           (param >> 12) & 0xfff, (param >> 0) & 0xfff);
279}
280
281#ifdef linux
282void
283do_pin(int argc, char *argv[])
284{
285    if (argc < 2)
286        usage();
287
288    cpu_set_t mask;
289    CPU_ZERO(&mask);
290
291    const char *sep = ",";
292    char *target = strtok(argv[0], sep);
293    while (target) {
294        CPU_SET(atoi(target), &mask);
295        target = strtok(NULL, sep);
296    }
297
298    if (sched_setaffinity(0, sizeof(cpu_set_t), &mask) < 0)
299        err(1, "setaffinity");
300
301    execvp(argv[1], &argv[1]);
302    err(1, "execvp failed!");
303}
304#endif
305
306struct MainFunc
307{
308    char *name;
309    void (*func)(int argc, char *argv[]);
310    char *usage;
311};
312
313struct MainFunc mainfuncs[] = {
314    { "exit",           do_exit,             "[delay]" },
315    { "fail",           do_fail,             "<code> [delay]" },
316    { "resetstats",     do_reset_stats,      "[delay [period]]" },
317    { "dumpstats",      do_dump_stats,       "[delay [period]]" },
318    { "dumpresetstats", do_dump_reset_stats, "[delay [period]]" },
319    { "readfile",       do_read_file,        "" },
320    { "writefile",      do_write_file,       "<filename>" },
321    { "execfile",       do_exec_file,        "" },
322    { "checkpoint",     do_checkpoint,       "[delay [period]]" },
323    { "loadsymbol",     do_load_symbol,      "<address> <symbol>" },
324    { "initparam",      do_initparam,        "[key] // key must be shorter than 16 chars" },
325    { "sw99param",      do_sw99param,        "" },
326#ifdef linux
327    { "pin",            do_pin,              "<cpu> <program> [args ...]" }
328#endif
329};
330int numfuncs = sizeof(mainfuncs) / sizeof(mainfuncs[0]);
331
332void
333usage()
334{
335    int i;
336
337    for (i = 0; i < numfuncs; ++i) {
338        char *header = i ? "" : "usage:";
339        fprintf(stderr, "%-6s %s %s %s\n",
340                header, progname, mainfuncs[i].name, mainfuncs[i].usage);
341    }
342    fprintf(stderr, "\n");
343    fprintf(stderr, "All times in nanoseconds!\n");
344
345    exit(1);
346}
347
348static void
349map_m5_mem()
350{
351#ifdef M5OP_ADDR
352    int fd;
353
354    fd = open("/dev/mem", O_RDWR | O_SYNC);
355    if (fd == -1) {
356        perror("Can't open /dev/mem");
357        exit(1);
358    }
359
360    m5_mem = mmap(NULL, 0x10000, PROT_READ | PROT_WRITE, MAP_SHARED, fd, M5OP_ADDR);
361    if (!m5_mem) {
362        perror("Can't mmap /dev/mem");
363        exit(1);
364    }
365#endif
366}
367
368int
369main(int argc, char *argv[])
370{
371    progname = argv[0];
372    if (argc < 2)
373        usage(1);
374
375    map_m5_mem();
376
377    command = argv[1];
378
379    argv += 2;
380    argc -= 2;
381
382    int i;
383    for (i = 0; i < numfuncs; ++i) {
384        if (strcmp(command, mainfuncs[i].name) != 0)
385            continue;
386
387        mainfuncs[i].func(argc, argv);
388        exit(0);
389    }
390
391    usage(1);
392}
393