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