m5.c revision 9949
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 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 uint64_t ints[2] = {0,0}; 149 parse_int_args(argc, argv, ints, argc); 150 m5_fail(ints[1], ints[0]); 151} 152 153void 154do_reset_stats(int argc, char *argv[]) 155{ 156 uint64_t ints[2]; 157 parse_int_args(argc, argv, ints, 2); 158 m5_reset_stats(ints[0], ints[1]); 159} 160 161void 162do_dump_stats(int argc, char *argv[]) 163{ 164 uint64_t ints[2]; 165 parse_int_args(argc, argv, ints, 2); 166 m5_dump_stats(ints[0], ints[1]); 167} 168 169void 170do_dump_reset_stats(int argc, char *argv[]) 171{ 172 uint64_t ints[2]; 173 parse_int_args(argc, argv, ints, 2); 174 m5_dumpreset_stats(ints[0], ints[1]); 175} 176 177void 178do_read_file(int argc, char *argv[]) 179{ 180 if (argc > 0) 181 usage(); 182 183 read_file(STDOUT_FILENO); 184} 185 186void 187do_write_file(int argc, char *argv[]) 188{ 189 if (argc != 1) 190 usage(); 191 192 const char *filename = argv[0]; 193 194 write_file(filename); 195} 196 197void 198do_exec_file(int argc, char *argv[]) 199{ 200 if (argc > 0) 201 usage(); 202 203 const char *destname = "/tmp/execfile"; 204 205 int fid = open(destname, O_WRONLY, 0777); 206 int len = read_file(fid); 207 close(fid); 208 if (len > 0) { 209 execl(destname, "execfile", NULL); 210 err(1, "execl failed!"); 211 } 212} 213 214void 215do_checkpoint(int argc, char *argv[]) 216{ 217 uint64_t ints[2]; 218 parse_int_args(argc, argv, ints, 2); 219 m5_checkpoint(ints[0], ints[1]); 220} 221 222void 223do_load_symbol(int argc, char *argv[]) 224{ 225 if (argc != 2) 226 usage(); 227 228 uint64_t addr = strtoul(argv[0], NULL, 0); 229 char *symbol = argv[1]; 230 m5_loadsymbol(addr, symbol); 231} 232 233void 234do_initparam(int argc, char *argv[]) 235{ 236 if (argc != 0) 237 usage(); 238 239 uint64_t val = m5_initparam(); 240 printf("%"PRIu64, val); 241} 242 243void 244do_sw99param(int argc, char *argv[]) 245{ 246 if (argc != 0) 247 usage(); 248 249 uint64_t param = m5_initparam(); 250 251 // run-time, rampup-time, rampdown-time, warmup-time, connections 252 printf("%"PRId64" %"PRId64" %"PRId64" %"PRId64" %"PRId64, 253 (param >> 48) & 0xfff, 254 (param >> 36) & 0xfff, (param >> 24) & 0xfff, 255 (param >> 12) & 0xfff, (param >> 0) & 0xfff); 256} 257 258#ifdef linux 259void 260do_pin(int argc, char *argv[]) 261{ 262 if (argc < 2) 263 usage(); 264 265 cpu_set_t mask; 266 CPU_ZERO(&mask); 267 268 const char *sep = ","; 269 char *target = strtok(argv[0], sep); 270 while (target) { 271 CPU_SET(atoi(target), &mask); 272 target = strtok(NULL, sep); 273 } 274 275 if (sched_setaffinity(0, sizeof(cpu_set_t), &mask) < 0) 276 err(1, "setaffinity"); 277 278 execvp(argv[1], &argv[1]); 279 err(1, "execvp failed!"); 280} 281#endif 282 283struct MainFunc 284{ 285 char *name; 286 void (*func)(int argc, char *argv[]); 287 char *usage; 288}; 289 290struct MainFunc mainfuncs[] = { 291 { "exit", do_exit, "[delay]" }, 292 { "fail", do_fail, "<code> [delay]" }, 293 { "resetstats", do_reset_stats, "[delay [period]]" }, 294 { "dumpstats", do_dump_stats, "[delay [period]]" }, 295 { "dumpresetstats", do_dump_reset_stats, "[delay [period]]" }, 296 { "readfile", do_read_file, "" }, 297 { "writefile", do_write_file, "<filename>" }, 298 { "execfile", do_exec_file, "" }, 299 { "checkpoint", do_checkpoint, "[delay [period]]" }, 300 { "loadsymbol", do_load_symbol, "<address> <symbol>" }, 301 { "initparam", do_initparam, "" }, 302 { "sw99param", do_sw99param, "" }, 303#ifdef linux 304 { "pin", do_pin, "<cpu> <program> [args ...]" } 305#endif 306}; 307int numfuncs = sizeof(mainfuncs) / sizeof(mainfuncs[0]); 308 309void 310usage() 311{ 312 int i; 313 314 for (i = 0; i < numfuncs; ++i) { 315 char *header = i ? "" : "usage:"; 316 fprintf(stderr, "%-6s %s %s %s\n", 317 header, progname, mainfuncs[i].name, mainfuncs[i].usage); 318 } 319 fprintf(stderr, "\n"); 320 fprintf(stderr, "All times in nanoseconds!\n"); 321 322 exit(1); 323} 324 325static void 326map_m5_mem() 327{ 328#ifdef M5OP_ADDR 329 int fd; 330 331 fd = open("/dev/mem", O_RDWR | O_SYNC); 332 if (fd == -1) { 333 perror("Can't open /dev/mem"); 334 exit(1); 335 } 336 337 m5_mem = mmap(NULL, 0x10000, PROT_READ | PROT_WRITE, MAP_SHARED, fd, M5OP_ADDR); 338 if (!m5_mem) { 339 perror("Can't mmap /dev/mem"); 340 exit(1); 341 } 342#endif 343} 344 345int 346main(int argc, char *argv[]) 347{ 348 progname = argv[0]; 349 if (argc < 2) 350 usage(1); 351 352 map_m5_mem(); 353 354 command = argv[1]; 355 356 argv += 2; 357 argc -= 2; 358 359 int i; 360 for (i = 0; i < numfuncs; ++i) { 361 if (strcmp(command, mainfuncs[i].name) != 0) 362 continue; 363 364 mainfuncs[i].func(argc, argv); 365 exit(0); 366 } 367 368 usage(1); 369} 370