diff --git a/src/IPC/IPC_msg.c b/src/IPC/IPC_msg.c new file mode 100644 index 0000000..dff2676 --- /dev/null +++ b/src/IPC/IPC_msg.c @@ -0,0 +1,187 @@ +#include "IPC_msg.h" +#include +#include "../debug.h" +#include +#include + + + +static int syscalls_indexIPC_msg(lua_State* L) +{ + ksys_ipc_msg* r = luaL_checkudata(L, 1, syscalls_IPC_msg_metatable_name); + const char* index = luaL_checkstring(L, 2); + + if (strcmp(index, "size") == 0) + { + lua_pushinteger(L, r->datalen); + } + else if (strcmp(index, "pid") == 0) + { + lua_pushboolean(L, r->pid); + } + else + { + lua_pushnil(L); + } + + return 1; +} + +static int syscalls_newindexIPC_msg(lua_State* L) +{ + ksys_ipc_msg* r = luaL_checkudata(L, 1, syscalls_IPC_msg_metatable_name); + lua_Integer index_i = lua_isnumber(L, 2); + uint32_t val = luaL_checkinteger(L, 3); + + if (!index_i) + { + const char* index = luaL_checkstring(L, 2); + if (strcmp(index, "size") == 0) + { + r->datalen = val; + } + else if (strcmp(index, "pid") == 0) + { + r->pid = val; + } + else + { + luaL_error(L, "wrong index: %s", index); + } + } + else + { + if (index_i < r->data) + { + ((lua_Integer*)r + (2 * sizeof(unsigned)))[index_i] = val; + } + else + { + luaL_error(L, "out of range. size: %d index: %d", r->datalen, index_i); + } + } + + lua_pushboolean(L, true); + + return 1; +} + +static bool compare_ipc_msg(ksys_ipc_msg* a, ksys_ipc_msg* b) +{ + if (a->datalen == b->datalen) + { + return memcmp( + a + (2 * sizeof(unsigned)), + b + (2 * sizeof(unsigned)), + a->datalen + ); + } + else + { + return false; + } +} + +static int syscalls_eqIPC_msg(lua_State* L) +{ + lua_pushboolean( + L, + compare_ipc_msg( + luaL_checkudata(L, 1, syscalls_IPC_msg_metatable_name), + luaL_checkudata(L, 2, syscalls_IPC_msg_metatable_name) + ) + ); + + return 1; +} + +static int syscalls_IPC_msg_ReadByte(lua_State* L) +{ + lua_pushinteger( + L, + ((uint8_t*)luaL_checkudata(L, 1, syscalls_IPC_msg_metatable_name) + (2 * sizeof(unsigned)))[luaL_checkinteger(L, 2)] + ); + + return 1; +} + +static int syscalls_IPC_msg_ReadWord(lua_State* L) +{ + lua_pushinteger( + L, + ((uint16_t*)luaL_checkudata(L, 1, syscalls_IPC_msg_metatable_name) + (2 * sizeof(unsigned)))[luaL_checkinteger(L, 2)] + ); + + return 1; +} + +static int syscalls_IPC_msg_ReadDword(lua_State* L) +{ + lua_pushinteger( + L, + ((uint32_t*)luaL_checkudata(L, 1, syscalls_IPC_msg_metatable_name) + (2 * sizeof(unsigned)))[luaL_checkinteger(L, 2)] + ); + + return 1; +} + +static int syscalls_IPC_msg_ReadInteger(lua_State* L) +{ + lua_pushinteger( + L, + ((lua_Integer*)luaL_checkudata(L, 1, syscalls_IPC_msg_metatable_name) + (2 * sizeof(unsigned)))[luaL_checkinteger(L, 2)] + ); + + return 1; +} + +static const luaL_Reg syscalls_IPC_msg_m[] = { + {"__index", syscalls_indexIPC_msg}, + {"__newindex", syscalls_newindexIPC_msg}, + {"__eq", syscalls_eqIPC_msg}, + {"ReadByte", syscalls_IPC_msg_ReadByte}, + {"ReadWord", syscalls_IPC_msg_ReadWord}, + {"ReadDword", syscalls_IPC_msg_ReadDword}, + {"ReadInteger", syscalls_IPC_msg_ReadInteger}, + {NULL, NULL} +}; + +struct registers* syscalls_pushIPC_msg(lua_State* L, size_t dataLen) +{ + DEBUG_LINE("push IPC_msg entry"); + + ksys_ipc_msg* entry = lua_newuserdata(L, sizeof(ksys_ipc_msg) + dataLen); + + luaL_setmetatable(L, syscalls_IPC_msg_metatable_name); + + return entry; +} + +static int syscalls_newIPC_msg(lua_State* L) +{ + size_t dataLen = luaL_checkinteger(L, 1); + ksys_ipc_msg* r = syscalls_pushIPC_msg(L, dataLen); + r->datalen = dataLen; + + return 1; +} + +static const luaL_Reg syscalls_IPC_msg_lib[] = { + {"new", syscalls_newIPC_msg}, + {NULL, NULL} +}; + +void syscalls_register_IPC_msg(lua_State* L) +{ + DEBUG_LINE("register IPC_msg entry"); + + luaL_newlib(L, syscalls_IPC_msg_lib); + + lua_setfield(L, -2, syscalls_IPC_msg_name); + + + luaL_newmetatable(L, syscalls_IPC_msg_metatable_name); + luaL_setfuncs(L, syscalls_IPC_msg_m, 0); + + lua_pop(L, 1); +} diff --git a/src/IPC/IPC_msg.h b/src/IPC/IPC_msg.h new file mode 100644 index 0000000..0b0d954 --- /dev/null +++ b/src/IPC/IPC_msg.h @@ -0,0 +1,19 @@ +#ifndef _SYSCALLS_IPC_MSG_H_ +#define _SYSCALLS_IPC_MSG_H_ + +#include "../syscalls.h" +#include + +#define syscalls_IPC_msg_metatable_name "syscalls IPC_msg metatable" +#define syscalls_IPC_msg_name "IPC_msg" + +int syscalls_newSystemColors(lua_State* L); + +ksys_colors_table_t* syscalls_pushSystemColors(lua_State* L); + +/** + * Register SystemColors lib + */ +void syscalls_register_SystemColors(lua_State* L); + +#endif // _SYSCALLS_IPC_MSG_H_ diff --git a/src/IPC/ipc.c b/src/IPC/ipc.c new file mode 100644 index 0000000..0bec3f8 --- /dev/null +++ b/src/IPC/ipc.c @@ -0,0 +1,48 @@ +#include "ipc.h" +#include "IPC_msg.h" +#include + +inline static void define_ipc(ksys_ipc_buffer* buffer, size_t bufLen) +{ + asm_inline( + "int $0x40" + ::"a"(60), "b"(1), "c"(buffer), "d"(bufLen) + ); +} + +inline static enum SendIPCErrors send_ipc(int pid, ksys_ipc_msg* msg) +{ + enum SendIPCErrors ret; + + asm_inline( + "int $0x40" + : "=a"(ret) + : "a"(60), "b"(1), "c"(pid), "d"(msg), "S"(sizeof(ksys_ipc_msg) + msg->datalen) + ); + + return ret; +} + +int syscalls_DefineIPCBuffer(lua_State* L) +{ + uint32_t len = luaL_checkinteger(L, 1); + ksys_ipc_buffer* buffer; + + + define_ipc(buffer, len); + + return 1; +} + +int syscalls_SendIPCMessage(lua_State* L) +{ + lua_pushinteger( + L, + send_ipc( + lua_checkinteger(L, 1), + luaL_checkudata(L, 1, syscalls_IPC_msg_metatable_name) + ) + ); + + return 1; +} diff --git a/src/IPC/ipc.h b/src/IPC/ipc.h new file mode 100644 index 0000000..1c9e14b --- /dev/null +++ b/src/IPC/ipc.h @@ -0,0 +1,18 @@ +#ifndef __IPC_H__ +#define __IPC_H__ + +#include "../syscalls.h" + +enum SendIPCErrors +{ + Ok = 0, + BufferNotDefined = 1, + BufferLocked = 2, + BufferOverflow = 3, + PidNotExist = 4 +}; + +int syscalls_DefineIPCBuffer(lua_State* L); +int syscalls_SendIPCMessage(lua_State* L); + +#endif // __IPC_H__ \ No newline at end of file diff --git a/src/ipc.c b/src/ipc.c deleted file mode 100644 index 9315f27..0000000 --- a/src/ipc.c +++ /dev/null @@ -1,51 +0,0 @@ -# include - -enum SendIPCErrors -{ - Ok = 0, - BufferNotDefined = 1, - BufferLocked = 2, - BufferOverflow = 3, - PidNotExist = 4 -} - -inline static void define_ipc(ksys_ipc_buffer *buffer, size_t bufLen) -{ - asm_inline( - "int $0x40" - ::"a"(60), "b"(1), "c"(buffer), "d"(bufLen) - ); -} - -inline static enum SendIPCErrors send_ipc(int pid, ksys_ipc_msg *msg, size_t msgLen) -{ - enum SendIPCErrors ret; - - asm_inline( - "int $0x40" - : "=a"(ret), - : "a"(60), "b"(1), "c"(pid), "d"(msg), "S"(msgLen) - ) - - return ret; -} - -int syscalls_DefineIPCBuffer(lua_State *L) -{ - define_ipc(); - - return 0; -} - -int syscalls_SendIPCMessage(lua_State *L) -{ - lua_pushinteger( - L, - send_ipc( - lua_checkinteger(L, 1);, - - ) - ); - - return 1; -}