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 "m5op.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_readfile(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) 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_writefile(buf, len, offset, 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_dumpreset_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) 228 usage(); 229 230 const char *filename = argv[0]; 231 232 write_file(filename); 233} 234 235void 236do_exec_file(int argc, char *argv[]) 237{ 238 if (argc > 0) 239 usage(); 240 241 const char *destname = "/tmp/execfile"; 242 243 int fid = open(destname, O_WRONLY, 0777); 244 int len = read_file(fid); 245 close(fid); 246 if (len > 0) { 247 execl(destname, "execfile", NULL); 248 err(1, "execl failed!"); 249 } 250} 251 252void 253do_checkpoint(int argc, char *argv[]) 254{ 255 uint64_t ints[2]; 256 parse_int_args(argc, argv, ints, 2); 257 m5_checkpoint(ints[0], ints[1]); 258} 259 260void 261do_addsymbol(int argc, char *argv[]) 262{ 263 if (argc != 2) 264 usage(); 265 266 uint64_t addr = strtoul(argv[0], NULL, 0); 267 char *symbol = argv[1]; 268 m5_addsymbol(addr, symbol); 269} 270 271 272void 273do_loadsymbol(int argc, char *argv[]) 274{ 275 if (argc > 0) 276 usage(); 277 278 m5_loadsymbol(); 279} 280 281void 282do_initparam(int argc, char *argv[]) 283{ 284 if (argc > 1) 285 usage(); 286 287 uint64_t key_str[2]; 288 parse_str_args_to_regs(argc, argv, key_str, 2); 289 uint64_t val = m5_initparam(key_str[0], key_str[1]); 290 printf("%"PRIu64, val); 291} 292 293void 294do_sw99param(int argc, char *argv[]) 295{ 296 if (argc != 0) 297 usage(); 298 299 uint64_t param = m5_initparam(0, 0); 300 301 // run-time, rampup-time, rampdown-time, warmup-time, connections 302 printf("%"PRId64" %"PRId64" %"PRId64" %"PRId64" %"PRId64, 303 (param >> 48) & 0xfff, 304 (param >> 36) & 0xfff, (param >> 24) & 0xfff, 305 (param >> 12) & 0xfff, (param >> 0) & 0xfff); 306} 307 308#ifdef linux 309void 310do_pin(int argc, char *argv[]) 311{ 312 if (argc < 2) 313 usage(); 314 315 cpu_set_t mask; 316 CPU_ZERO(&mask); 317 318 const char *sep = ","; 319 char *target = strtok(argv[0], sep); 320 while (target) { 321 CPU_SET(atoi(target), &mask); 322 target = strtok(NULL, sep); 323 } 324 325 if (sched_setaffinity(0, sizeof(cpu_set_t), &mask) < 0) 326 err(1, "setaffinity"); 327 328 execvp(argv[1], &argv[1]); 329 err(1, "execvp failed!"); 330} 331#endif 332 333struct MainFunc 334{ 335 char *name; 336 void (*func)(int argc, char *argv[]); 337 char *usage; 338}; 339 340struct MainFunc mainfuncs[] = { 341 { "exit", do_exit, "[delay]" }, 342 { "fail", do_fail, "<code> [delay]" }, 343 { "resetstats", do_reset_stats, "[delay [period]]" }, 344 { "dumpstats", do_dump_stats, "[delay [period]]" }, 345 { "dumpresetstats", do_dump_reset_stats, "[delay [period]]" }, 346 { "readfile", do_read_file, "" }, 347 { "writefile", do_write_file, "<filename>" }, 348 { "execfile", do_exec_file, "" }, 349 { "checkpoint", do_checkpoint, "[delay [period]]" }, 350 { "addsymbol", do_addsymbol, "<address> <symbol>" }, 351 { "loadsymbol", do_loadsymbol, "" },
|