util: Add a "writefile" unit test to the m5 utility.
[gem5.git] / util / m5 / src / lua_gem5Op.cc
1 /* Copyright (c) 2017 Hanhwi Jang
2 * All rights reserved.
3
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met: redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer;
8 * redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution;
11 * neither the name of the copyright holders nor the names of its
12 * contributors may be used to endorse or promote products derived from
13 * this software without specific prior written permission.
14
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28
29 #include <lauxlib.h>
30 #include <lua.h>
31 #include <lualib.h>
32
33 #include <cassert>
34 #include <cstdlib>
35
36 #include <gem5/m5ops.h>
37
38 #include "m5_mmap.h"
39
40 static int
41 do_arm(lua_State *L)
42 {
43 uint64_t address = lua_tointeger(L, 1);
44 m5_arm(address);
45 return 0;
46 }
47
48 static int
49 do_quiesce(lua_State *L)
50 {
51 m5_quiesce();
52 return 0;
53 }
54
55 static int
56 do_quiesce_ns(lua_State *L)
57 {
58 uint64_t ns = lua_tointeger(L, 1);
59 m5_quiesce_ns(ns);
60 return 0;
61 }
62
63 static int
64 do_quiesce_cycle(lua_State *L)
65 {
66 uint64_t cycles = lua_tointeger(L, 1);
67 m5_quiesce_cycle(cycles);
68 return 0;
69 }
70
71 static int
72 do_quiesce_time(lua_State *L)
73 {
74 uint64_t ns = m5_quiesce_time();
75 lua_pushinteger(L, ns);
76 return 1;
77 }
78
79 static int
80 do_rpns(lua_State *L)
81 {
82 uint64_t ns = m5_rpns();
83 lua_pushinteger(L, ns);
84 return 1;
85 }
86
87 static int
88 do_wake_cpu(lua_State *L)
89 {
90 uint64_t cpuid = lua_tointeger(L, 1);
91 m5_wake_cpu(cpuid);
92 return 0;
93 }
94
95 static int
96 do_exit(lua_State *L)
97 {
98 uint64_t ns_delay = lua_tointeger(L, 1);
99 m5_exit(ns_delay);
100 return 0;
101 }
102
103 static int
104 do_fail(lua_State *L)
105 {
106 uint64_t ns_delay = lua_tointeger(L, 1);
107 uint64_t code = lua_tointeger(L, 2);
108 m5_fail(ns_delay, code);
109 return 0;
110 }
111
112 static int
113 do_init_param(lua_State *L)
114 {
115 uint64_t key_str1 = lua_tointeger(L, 1);
116 uint64_t key_str2 = lua_tointeger(L, 2);
117 lua_pushinteger(L, m5_init_param(key_str1, key_str2));
118 return 1;
119 }
120
121 static int
122 do_checkpoint(lua_State *L)
123 {
124 uint64_t delay = lua_tointeger(L, 1);
125 uint64_t period = lua_tointeger(L, 2);
126 m5_checkpoint(delay, period);
127 return 0;
128 }
129
130 static int
131 do_reset_stats(lua_State *L)
132 {
133 uint64_t ns_delay = lua_tointeger(L, 1);
134 uint64_t ns_period = lua_tointeger(L, 2);
135 m5_reset_stats(ns_delay, ns_period);
136 return 0;
137 }
138
139 static int
140 do_dump_stats(lua_State *L)
141 {
142 uint64_t delay = lua_tointeger(L, 1);
143 uint64_t period = lua_tointeger(L, 2);
144 m5_dump_stats(delay, period);
145 return 0;
146 }
147
148 static int
149 do_dump_reset_stats(lua_State *L)
150 {
151 uint64_t delay = lua_tointeger(L, 1);
152 uint64_t period = lua_tointeger(L, 2);
153 m5_dump_reset_stats(delay, period);
154 return 0;
155 }
156
157 static int
158 do_read_file(lua_State *L)
159 {
160 uint64_t len = lua_tointeger(L, 1);
161 uint64_t offset = lua_tointeger(L, 2);
162 char *buf = (char *)malloc(len);
163 uint64_t readlen = m5_read_file(buf, len, offset);
164 lua_pushlstring(L, buf, readlen);
165 return 1;
166 }
167
168 static int
169 do_write_file(lua_State *L)
170 {
171 const char* buf = lua_tostring(L, 1);
172 uint64_t len = lua_tointeger(L, 2);
173 assert(len <= lua_strlen(L, 1));
174 uint64_t offset = lua_tointeger(L, 3);
175 const char *filename = lua_tostring(L, 4);
176 uint64_t w_len = m5_write_file((void *)buf, len, offset, filename);
177 lua_pushinteger(L, w_len);
178 return 1;
179 }
180
181 static int
182 do_debug_break(lua_State *L)
183 {
184 m5_debug_break();
185 return 0;
186 }
187
188 static int
189 do_switch_cpu(lua_State *L)
190 {
191 m5_switch_cpu();
192 return 0;
193 }
194
195 static int
196 do_dist_toggle_sync(lua_State *L)
197 {
198 m5_dist_toggle_sync();
199 return 0;
200 }
201
202 static int
203 do_add_symbol(lua_State *L)
204 {
205 uint64_t addr = lua_tointeger(L, 1);
206 char *string = (char*) lua_tostring(L, 2);
207 m5_add_symbol(addr, string);
208 return 0;
209 }
210
211 static int
212 do_loadsymbol(lua_State *L)
213 {
214 m5_load_symbol();
215 return 0;
216 }
217
218 static int
219 do_panic(lua_State *L)
220 {
221 m5_panic();
222 return 0;
223 }
224
225 static int
226 do_work_begin(lua_State *L)
227 {
228 uint64_t workid = lua_tointeger(L, 1);
229 uint64_t threadid = lua_tointeger(L, 2);
230 m5_work_begin(workid, threadid);
231 return 0;
232 }
233
234 static int
235 do_work_end(lua_State *L)
236 {
237 uint64_t workid = lua_tointeger(L, 1);
238 uint64_t threadid = lua_tointeger(L, 2);
239 m5_work_end(workid, threadid);
240 return 0;
241 }
242
243 extern "C"
244 {
245
246 int luaopen_gem5OpLua(lua_State *);
247
248 }
249
250 int
251 luaopen_gem5OpLua(lua_State *L)
252 {
253 map_m5_mem();
254 #define ADD_FUNC(fname) do{ \
255 lua_pushcfunction(L, fname); \
256 lua_setfield(L, -2, #fname); \
257 }while (0)
258
259 lua_newtable(L);
260 ADD_FUNC(do_arm);
261 ADD_FUNC(do_quiesce);
262 ADD_FUNC(do_quiesce_ns);
263 ADD_FUNC(do_quiesce_cycle);
264 ADD_FUNC(do_quiesce_time);
265 ADD_FUNC(do_rpns);
266 ADD_FUNC(do_wake_cpu);
267 ADD_FUNC(do_exit);
268 ADD_FUNC(do_fail);
269 ADD_FUNC(do_init_param);
270 ADD_FUNC(do_checkpoint);
271 ADD_FUNC(do_reset_stats);
272 ADD_FUNC(do_dump_stats);
273 ADD_FUNC(do_dump_reset_stats);
274 ADD_FUNC(do_read_file);
275 ADD_FUNC(do_write_file);
276 ADD_FUNC(do_debug_break);
277 ADD_FUNC(do_switch_cpu);
278 ADD_FUNC(do_dist_toggle_sync);
279 ADD_FUNC(do_add_symbol);
280 ADD_FUNC(do_loadsymbol);
281 ADD_FUNC(do_panic);
282 ADD_FUNC(do_work_begin);
283 ADD_FUNC(do_work_end);
284 #undef ADD_FUNC
285 return 1;
286 }