util: Convert the m5 utility to C++.
[gem5.git] / util / m5 / src / commands.cc
1 /*
2 * Copyright (c) 2003-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <err.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32
33 #include <cinttypes>
34 #include <cstdio>
35 #include <cstdlib>
36 #include <cstring>
37
38 #include "args.hh"
39 #include "commands.hh"
40 #include "usage.hh"
41
42 static int
43 read_file(DispatchTable *dt, int dest_fid)
44 {
45 uint8_t buf[256*1024];
46 int offset = 0;
47 int len, ret;
48
49 // Touch all buffer pages to ensure they are mapped in the
50 // page table. This is required in the case of X86_FS, where
51 // Linux does demand paging.
52 memset(buf, 0, sizeof(buf));
53
54 while ((len = (*dt->m5_read_file)(buf, sizeof(buf), offset)) > 0) {
55 uint8_t *base = buf;
56 offset += len;
57 do {
58 ret = write(dest_fid, base, len);
59 if (ret < 0) {
60 perror("Failed to write file");
61 exit(2);
62 } else if (ret == 0) {
63 fprintf(stderr, "Failed to write file: "
64 "Unhandled short write\n");
65 exit(2);
66 }
67
68 base += ret;
69 len -= ret;
70 } while (len);
71 }
72
73 return offset;
74 }
75
76 static void
77 write_file(DispatchTable *dt, const char *filename, const char *host_filename)
78 {
79 fprintf(stderr, "opening %s\n", filename);
80 int src_fid = open(filename, O_RDONLY);
81
82 if (src_fid < 0) {
83 fprintf(stderr, "error opening %s\n", filename);
84 return;
85 }
86
87 char buf[256*1024];
88 int offset = 0;
89 int len;
90 int bytes = 0;
91
92 memset(buf, 0, sizeof(buf));
93
94 while ((len = read(src_fid, buf, sizeof(buf))) > 0) {
95 bytes += (*dt->m5_write_file)(buf, len, offset, host_filename);
96 offset += len;
97 }
98 fprintf(stderr, "written %d bytes\n", bytes);
99
100 close(src_fid);
101 }
102
103 static void
104 do_exit(DispatchTable *dt, Args *args)
105 {
106 if (args->argc > 1)
107 usage();
108
109 uint64_t ints[1];
110 if (!parse_int_args(args, ints, 1))
111 usage();
112 (*dt->m5_exit)(ints[0]);
113 }
114
115 static void
116 do_fail(DispatchTable *dt, Args *args)
117 {
118 if (args->argc < 1 || args->argc > 2)
119 usage();
120
121 uint64_t ints[2] = { 0, 0 };
122 if (!parse_int_args(args, ints, args->argc))
123 usage();
124 (*dt->m5_fail)(ints[1], ints[0]);
125 }
126
127 static void
128 do_reset_stats(DispatchTable *dt, Args *args)
129 {
130 uint64_t ints[2];
131 if (!parse_int_args(args, ints, 2))
132 usage();
133 (*dt->m5_reset_stats)(ints[0], ints[1]);
134 }
135
136 static void
137 do_dump_stats(DispatchTable *dt, Args *args)
138 {
139 uint64_t ints[2];
140 if (!parse_int_args(args, ints, 2))
141 usage();
142 (*dt->m5_dump_stats)(ints[0], ints[1]);
143 }
144
145 static void
146 do_dump_reset_stats(DispatchTable *dt, Args *args)
147 {
148 uint64_t ints[2];
149 if (!parse_int_args(args, ints, 2))
150 usage();
151 (*dt->m5_dump_reset_stats)(ints[0], ints[1]);
152 }
153
154 static void
155 do_read_file(DispatchTable *dt, Args *args)
156 {
157 if (args->argc > 0)
158 usage();
159
160 read_file(dt, STDOUT_FILENO);
161 }
162
163 static void
164 do_write_file(DispatchTable *dt, Args *args)
165 {
166 if (args->argc != 1 && args->argc != 2)
167 usage();
168
169 const char *filename = pop_arg(args);
170 const char *host_filename = pop_arg(args);
171 if (!host_filename)
172 host_filename = filename;
173
174 write_file(dt, filename, host_filename);
175 }
176
177 static void
178 do_checkpoint(DispatchTable *dt, Args *args)
179 {
180 uint64_t ints[2];
181 if (!parse_int_args(args, ints, 2))
182 usage();
183 (*dt->m5_checkpoint)(ints[0], ints[1]);
184 }
185
186 static void
187 do_addsymbol(DispatchTable *dt, Args *args)
188 {
189 if (args->argc != 2)
190 usage();
191
192 uint64_t addr = strtoul(pop_arg(args), NULL, 0);
193 const char *symbol = pop_arg(args);
194 (*dt->m5_add_symbol)(addr, symbol);
195 }
196
197
198 static void
199 do_loadsymbol(DispatchTable *dt, Args *args)
200 {
201 if (args->argc > 0)
202 usage();
203
204 (*dt->m5_load_symbol)();
205 }
206
207 static void
208 do_initparam(DispatchTable *dt, Args *args)
209 {
210 if (args->argc > 1)
211 usage();
212
213 uint64_t key_str[2];
214 if (!pack_arg_into_regs(args, key_str, 2))
215 usage();
216 uint64_t val = (*dt->m5_init_param)(key_str[0], key_str[1]);
217 std::cout << val;
218 }
219
220 CommandInfo command_table[] = {
221 { "addsymbol", do_addsymbol, "<address> <symbol> // Adds a "
222 "symbol with address \"address\" "
223 "to gem5's symbol table" },
224 { "checkpoint", do_checkpoint, "[delay [period]] // After "
225 "delay (default 0) take a "
226 "checkpoint, and then optionally "
227 "every period after" },
228 { "dumpresetstats", do_dump_reset_stats, "[delay [period]] // After "
229 "delay (default 0) dump and "
230 "reset the stats, and then "
231 "optionally every period after" },
232 { "dumpstats", do_dump_stats, "[delay [period]] // After "
233 "delay (default 0) dump the "
234 "stats, and then optionally "
235 "every period after" },
236 { "exit", do_exit, "[delay] // Exit after delay, "
237 "or immediately" },
238 { "fail", do_fail, "<code> [delay] // Exit with "
239 "failure code code after delay, "
240 "or immediately" },
241 { "initparam", do_initparam, "[key] // optional key may be at "
242 "most 16 characters long" },
243 { "loadsymbol", do_loadsymbol, "load a preselected symbol file "
244 "into gem5's symbol table" },
245 { "readfile", do_read_file, "read a preselected file from "
246 "the host and write it to "
247 "stdout" },
248 { "resetstats", do_reset_stats, "[delay [period]] // After "
249 "delay (default 0) reset the "
250 "stats, and then optionally "
251 "every period after" },
252 { "writefile", do_write_file, "<filename> [host filename] // "
253 "Write a file to the host, "
254 "optionally with a different "
255 "name" },
256 };
257
258 int num_commands = sizeof(command_table) / sizeof(CommandInfo);