util: Update jni_gem5Op.c so it will compile again.
[gem5.git] / util / m5 / src / lua_gem5Op.c
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 <assert.h>
30 #include <lauxlib.h>
31 #include <lua.h>
32 #include <lualib.h>
33 #include <stdlib.h>
34
35 #include <gem5/m5ops.h>
36
37 #include "m5_mmap.h"
38
39 static int
40 do_arm(lua_State *L)
41 {
42 uint64_t address = lua_tointeger(L, 1);
43 m5_arm(address);
44 return 0;
45 }
46
47 static int
48 do_quiesce(lua_State *L)
49 {
50 m5_quiesce();
51 return 0;
52 }
53
54 static int
55 do_quiesce_ns(lua_State *L)
56 {
57 uint64_t ns = lua_tointeger(L, 1);
58 m5_quiesce_ns(ns);
59 return 0;
60 }
61
62 static int
63 do_quiesce_cycle(lua_State *L)
64 {
65 uint64_t cycles = lua_tointeger(L, 1);
66 m5_quiesce_cycle(cycles);
67 return 0;
68 }
69
70 static int
71 do_quiesce_time(lua_State *L)
72 {
73 uint64_t ns = m5_quiesce_time();
74 lua_pushinteger(L, ns);
75 return 1;
76 }
77
78 static int
79 do_rpns(lua_State *L)
80 {
81 uint64_t ns = m5_rpns();
82 lua_pushinteger(L, ns);
83 return 1;
84 }
85
86 static int
87 do_wake_cpu(lua_State *L)
88 {
89 uint64_t cpuid = lua_tointeger(L, 1);
90 m5_wake_cpu(cpuid);
91 return 0;
92 }
93
94 static int
95 do_exit(lua_State *L)
96 {
97 uint64_t ns_delay = lua_tointeger(L, 1);
98 m5_exit(ns_delay);
99 return 0;
100 }
101
102 static int
103 do_fail(lua_State *L)
104 {
105 uint64_t ns_delay = lua_tointeger(L, 1);
106 uint64_t code = lua_tointeger(L, 2);
107 m5_fail(ns_delay, code);
108 return 0;
109 }
110
111 static int
112 do_init_param(lua_State *L)
113 {
114 uint64_t key_str1 = lua_tointeger(L, 1);
115 uint64_t key_str2 = lua_tointeger(L, 2);
116 lua_pushinteger(L, m5_init_param(key_str1, key_str2));
117 return 1;
118 }
119
120 static int
121 do_checkpoint(lua_State *L)
122 {
123 uint64_t delay = lua_tointeger(L, 1);
124 uint64_t period = lua_tointeger(L, 2);
125 m5_checkpoint(delay, period);
126 return 0;
127 }
128
129 static int
130 do_reset_stats(lua_State *L)
131 {
132 uint64_t ns_delay = lua_tointeger(L, 1);
133 uint64_t ns_period = lua_tointeger(L, 2);
134 m5_reset_stats(ns_delay, ns_period);
135 return 0;
136 }
137
138 static int
139 do_dump_stats(lua_State *L)
140 {
141 uint64_t delay = lua_tointeger(L, 1);
142 uint64_t period = lua_tointeger(L, 2);
143 m5_dump_stats(delay, period);
144 return 0;
145 }
146
147 static int
148 do_dump_reset_stats(lua_State *L)
149 {
150 uint64_t delay = lua_tointeger(L, 1);
151 uint64_t period = lua_tointeger(L, 2);
152 m5_dump_reset_stats(delay, period);
153 return 0;
154 }
155
156 static int
157 do_read_file(lua_State *L)
158 {
159 uint64_t len = lua_tointeger(L, 1);
160 uint64_t offset = lua_tointeger(L, 2);
161 char *buf = malloc(len);
162 uint64_t readlen = m5_read_file(buf, len, offset);
163 lua_pushlstring(L, buf, readlen);
164 return 1;
165 }
166
167 static int
168 do_write_file(lua_State *L)
169 {
170 const char* buf = lua_tostring(L, 1);
171 uint64_t len = lua_tointeger(L, 2);
172 assert(len <= lua_strlen(L, 1));
173 uint64_t offset = lua_tointeger(L, 3);
174 const char *filename = lua_tostring(L, 4);
175 uint64_t w_len = m5_write_file((void *)buf, len, offset, filename);
176 lua_pushinteger(L, w_len);
177 return 1;
178 }
179
180 static int
181 do_debug_break(lua_State *L)
182 {
183 m5_debug_break();
184 return 0;
185 }
186
187 static int
188 do_switch_cpu(lua_State *L)
189 {
190 m5_switch_cpu();
191 return 0;
192 }
193
194 static int
195 do_dist_toggle_sync(lua_State *L)
196 {
197 m5_dist_toggle_sync();
198 return 0;
199 }
200
201 static int
202 do_add_symbol(lua_State *L)
203 {
204 uint64_t addr = lua_tointeger(L, 1);
205 char *string = (char*) lua_tostring(L, 2);
206 m5_add_symbol(addr, string);
207 return 0;
208 }
209
210 static int
211 do_loadsymbol(lua_State *L)
212 {
213 m5_load_symbol();
214 return 0;
215 }
216
217 static int
218 do_panic(lua_State *L)
219 {
220 m5_panic();
221 return 0;
222 }
223
224 static int
225 do_work_begin(lua_State *L)
226 {
227 uint64_t workid = lua_tointeger(L, 1);
228 uint64_t threadid = lua_tointeger(L, 2);
229 m5_work_begin(workid, threadid);
230 return 0;
231 }
232
233 static int
234 do_work_end(lua_State *L)
235 {
236 uint64_t workid = lua_tointeger(L, 1);
237 uint64_t threadid = lua_tointeger(L, 2);
238 m5_work_end(workid, threadid);
239 return 0;
240 }
241
242 int
243 luaopen_gem5OpLua(lua_State *L)
244 {
245 map_m5_mem();
246 #define ADD_FUNC(fname) do{ \
247 lua_pushcfunction(L, fname); \
248 lua_setfield(L, -2, #fname); \
249 }while (0)
250
251 lua_newtable(L);
252 ADD_FUNC(do_arm);
253 ADD_FUNC(do_quiesce);
254 ADD_FUNC(do_quiesce_ns);
255 ADD_FUNC(do_quiesce_cycle);
256 ADD_FUNC(do_quiesce_time);
257 ADD_FUNC(do_rpns);
258 ADD_FUNC(do_wake_cpu);
259 ADD_FUNC(do_exit);
260 ADD_FUNC(do_fail);
261 ADD_FUNC(do_init_param);
262 ADD_FUNC(do_checkpoint);
263 ADD_FUNC(do_reset_stats);
264 ADD_FUNC(do_dump_stats);
265 ADD_FUNC(do_dump_reset_stats);
266 ADD_FUNC(do_read_file);
267 ADD_FUNC(do_write_file);
268 ADD_FUNC(do_debug_break);
269 ADD_FUNC(do_switch_cpu);
270 ADD_FUNC(do_dist_toggle_sync);
271 ADD_FUNC(do_add_symbol);
272 ADD_FUNC(do_loadsymbol);
273 ADD_FUNC(do_panic);
274 ADD_FUNC(do_work_begin);
275 ADD_FUNC(do_work_end);
276 #undef ADD_FUNC
277 return 1;
278 }