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