diff --git a/Makefile b/Makefile index 7a7e0f2..247497d 100644 --- a/Makefile +++ b/Makefile @@ -51,7 +51,9 @@ clean: ## Sources -src/syscalls.o: src/syscalls.c src/syscalls.h src/systemColors.h src/ARP_entry.h src/scancodes.h src/sockets/socket_lua.h src/graphic.h src/version/library_version.h +IPC_H = src/IPC/IPC_msg.h src/IPC/IPC_buffer.h src/IPC/ipc.h + +src/syscalls.o: src/syscalls.c src/syscalls.h src/systemColors.h src/ARP_entry.h src/scancodes.h src/sockets/socket_lua.h src/graphic.h src/version/library_version.h $(IPC_H) src/ARP_entry.o: src/ARP_entry.c src/ARP_entry.h src/debug.h src/systemColors.o: src/systemColors.c src/systemColors.h src/debug.h src/sockets/socket.o: src/sockets/socket.c src/sockets/socket.h @@ -61,7 +63,7 @@ src/graphic.o: src/graphic.c src/graphic.h src/debug/debug.o: src/debug/debug.c src/debug/debug.h src/debug/registers.h src/IPC/ipc.o: src/IPC/ipc.c src/IPC/ipc.h src/IPC/IPC_msg.h src/IPC/IPC_msg.o: src/IPC/IPC_msg.c src/IPC/IPC_msg.h -src/IPC/IPC_buffer.o: src/IPC/IPC_buffer.c src/IPC/IPC_buffer.h +src/IPC/IPC_buffer.o: src/IPC/IPC_buffer.c src/IPC/IPC_buffer.h src/IPC/IPC_msg.h src/debug/registers.o: src/debug/registers.c src/debug/registers.h src/syscalls.h src/debug.h src/version/coreversion.o: src/version/coreversion.c src/version/coreversion.h src/version/version_type.o: src/version/version_type.c src/version/version_type.h src/debug.h diff --git a/src/IPC/IPC_buffer.c b/src/IPC/IPC_buffer.c index 92203e0..164ebff 100644 --- a/src/IPC/IPC_buffer.c +++ b/src/IPC/IPC_buffer.c @@ -1,4 +1,5 @@ #include "IPC_buffer.h" +#include "IPC_msg.h" #include #include "../debug.h" #include @@ -20,7 +21,7 @@ static int syscalls_indexIPC_buffer(lua_State* L) } else if (strcmp(index, "size") == 0) { - lua_pushnumber(L, r->size); + lua_pushinteger(L, r->size); } else { @@ -86,7 +87,7 @@ static int syscalls_eqIPC_buffer(lua_State* L) static int syscalls_IPC_buffer_lock(lua_State* L) { - ksys_ipc_buffer* buffer = luaL_checkudata(L, 1, syscalls_IPC_buffer_metatable_name); + struct IPC_buffer* buffer = luaL_checkudata(L, 1, syscalls_IPC_buffer_metatable_name); buffer->lock = 1; @@ -95,7 +96,7 @@ static int syscalls_IPC_buffer_lock(lua_State* L) static int syscalls_IPC_buffer_unlock(lua_State* L) { - ksys_ipc_buffer* buffer = luaL_checkudata(L, 1, syscalls_IPC_buffer_metatable_name); + struct IPC_buffer* buffer = luaL_checkudata(L, 1, syscalls_IPC_buffer_metatable_name); buffer->lock = 0; @@ -106,9 +107,9 @@ static int syscalls_IPC_buffer_unlock(lua_State* L) * @brief Получить сообщение под номером * @param buffer * @param i - * @return указатель на сообщение, если такого сообщения ещё нет, то возвращает + * @return указатель на сообщение, если такого сообщения ещё нет, то возвращает 0 */ -static ksys_ipc_msg* IPC_buffer_get_message(struct IPC_buffer* buffer, int i) +static ksys_ipc_msg* IPC_buffer_get_message(ksys_ipc_buffer* buffer, int i) { ksys_ipc_msg* j = (ksys_ipc_msg*)(buffer + 8); unsigned diff = 0; @@ -125,7 +126,7 @@ static ksys_ipc_msg* IPC_buffer_get_message(struct IPC_buffer* buffer, int i) return j; } -static ksys_ipc_msg* IPC_buffer_get_last_message(struct IPC_buffer* buffer) +static ksys_ipc_msg* IPC_buffer_get_last_message(ksys_ipc_buffer* buffer) { ksys_ipc_msg* j = (ksys_ipc_msg*)(buffer + 8); unsigned diff = 0; @@ -142,13 +143,14 @@ static ksys_ipc_msg* IPC_buffer_get_last_message(struct IPC_buffer* buffer) static int syscalls_IPC_buffer_get_message(lua_State* L) { struct IPC_buffer* buffer = luaL_checkudata(L, 1, syscalls_IPC_buffer_metatable_name); - unsigned i = luaL_checkinteger(L, 1); + unsigned i = luaL_checkinteger(L, 2); - ksys_ipc_msg* msg = IPC_buffer_get_message(buffer, i); + ksys_ipc_msg* msg = IPC_buffer_get_message(IPC_buffer_to_ksys_ipc_buffer(buffer), i); - if (msg) + if (msg != 0) { lua_pushlightuserdata(L, msg); + luaL_setmetatable(L, syscalls_IPC_msg_metatable_name); } else { @@ -162,7 +164,17 @@ static int syscalls_IPC_buffer_get_last_message(lua_State* L) { struct IPC_buffer* buffer = luaL_checkudata(L, 1, syscalls_IPC_buffer_metatable_name); - lua_pushlightuserdata(L, IPC_buffer_get_last_message(buffer)); + ksys_ipc_msg* msg = IPC_buffer_get_last_message(IPC_buffer_to_ksys_ipc_buffer(buffer)); + + if ((unsigned)msg != (unsigned)buffer) + { + lua_pushlightuserdata(L, msg); + luaL_setmetatable(L, syscalls_IPC_msg_metatable_name); + } + else + { + lua_pushnil(L); + } return 1; } @@ -178,14 +190,9 @@ static int syscalls_IPC_buffer_reset(lua_State* L) } static const luaL_Reg syscalls_IPC_buffer_m[] = { - {"__index", syscalls_indexIPC_buffer}, {"__newindex", syscalls_newindexIPC_buffer}, {"__eq", syscalls_eqIPC_buffer}, - {"Lock", syscalls_IPC_buffer_lock}, - {"Unlock", syscalls_IPC_buffer_unlock}, - {"GetMessage", syscalls_IPC_buffer_get_message}, - {"GetLastMessage", syscalls_IPC_buffer_get_last_message}, - {"Reset", syscalls_IPC_buffer_reset}, + {"__index", syscalls_indexIPC_buffer}, {NULL, NULL} }; @@ -213,6 +220,11 @@ static int syscalls_newIPC_buffer(lua_State* L) static const luaL_Reg syscalls_IPC_buffer_lib[] = { {"new", syscalls_newIPC_buffer}, + {"Lock", syscalls_IPC_buffer_lock}, + {"Unlock", syscalls_IPC_buffer_unlock}, + {"GetMessage", syscalls_IPC_buffer_get_message}, + {"GetLastMessage", syscalls_IPC_buffer_get_last_message}, + {"Reset", syscalls_IPC_buffer_reset}, {NULL, NULL} }; diff --git a/src/IPC/IPC_buffer.h b/src/IPC/IPC_buffer.h index 1686011..6693603 100644 --- a/src/IPC/IPC_buffer.h +++ b/src/IPC/IPC_buffer.h @@ -9,8 +9,13 @@ struct IPC_buffer ksys_ipc_buffer; }; -#define syscalls_IPC_buffer_metatable_name "syscalls IPCbuffer metatable" -#define syscalls_IPC_buffer_name "IPCbuffer" +static inline ksys_ipc_buffer* IPC_buffer_to_ksys_ipc_buffer(struct IPC_buffer* buffer) +{ + return (ksys_ipc_buffer*)(buffer + (sizeof(struct IPC_buffer) - sizeof(ksys_ipc_buffer))); +} + +#define syscalls_IPC_buffer_metatable_name "syscalls IPC_buffer metatable" +#define syscalls_IPC_buffer_name "IPC_buffer" struct IPC_buffer* syscalls_pushIPC_buffer(lua_State* L, size_t dataLen); diff --git a/src/IPC/IPC_msg.c b/src/IPC/IPC_msg.c index 3772326..22e003f 100644 --- a/src/IPC/IPC_msg.c +++ b/src/IPC/IPC_msg.c @@ -2,8 +2,7 @@ #include #include "../debug.h" #include -#include - +#include static int syscalls_indexIPC_msg(lua_State* L) @@ -137,20 +136,27 @@ static int syscalls_IPC_msg_ReadInteger(lua_State* L) static int syscalls_IPC_msg_tostring(lua_State* L) { ksys_ipc_msg* msg = luaL_checkudata(L, 1, syscalls_IPC_msg_metatable_name); - lua_pushstring(L, &msg->data); + + char* buff = malloc(msg->datalen + 24 + 1); + + sprintf(buff, "pid: %d, len: %d, text: ", msg->pid, msg->datalen); + + memcpy(buff + 24, &msg->data, msg->datalen); + + buff[msg->datalen + 24] = '\0'; + + lua_pushstring(L, buff); + + free(buff); return 1; } static const luaL_Reg syscalls_IPC_msg_m[] = { - {"__index", syscalls_indexIPC_msg}, {"__newindex", syscalls_newindexIPC_msg}, {"__eq", syscalls_eqIPC_msg}, {"__tostring", syscalls_IPC_msg_tostring}, - {"ReadByte", syscalls_IPC_msg_ReadByte}, - {"ReadWord", syscalls_IPC_msg_ReadWord}, - {"ReadDword", syscalls_IPC_msg_ReadDword}, - {"ReadInteger", syscalls_IPC_msg_ReadInteger}, + {"__index", syscalls_indexIPC_msg}, {NULL, NULL} }; @@ -170,17 +176,22 @@ ksys_ipc_msg* syscalls_pushIPC_msg(lua_State* L, size_t dataLen) static int syscalls_newIPC_msg(lua_State* L) { size_t dataLen = luaL_checkinteger(L, 1); + unsigned pid = luaL_checkinteger(L, 2); ksys_ipc_msg* msg = syscalls_pushIPC_msg(L, dataLen); + msg->pid = pid; + return 1; } static int syscalls_fromStringIPC_msg(lua_State* L) { const char* text = luaL_checkstring(L, 1); + unsigned pid = luaL_checkinteger(L, 2); size_t len = strlen(text); ksys_ipc_msg* r = syscalls_pushIPC_msg(L, len); + r->pid = pid; memcpy(&r->data, text, len); @@ -191,6 +202,10 @@ static int syscalls_fromStringIPC_msg(lua_State* L) static const luaL_Reg syscalls_IPC_msg_lib[] = { {"new", syscalls_newIPC_msg}, {"fromString", syscalls_fromStringIPC_msg}, + {"ReadByte", syscalls_IPC_msg_ReadByte}, + {"ReadWord", syscalls_IPC_msg_ReadWord}, + {"ReadDword", syscalls_IPC_msg_ReadDword}, + {"ReadInteger", syscalls_IPC_msg_ReadInteger}, {NULL, NULL} }; diff --git a/src/IPC/ipc.c b/src/IPC/ipc.c index a3a2728..11c76fe 100644 --- a/src/IPC/ipc.c +++ b/src/IPC/ipc.c @@ -3,22 +3,22 @@ #include "IPC_buffer.h" #include -inline static void define_ipc(struct IPC_buffer* buffer, size_t bufLen) +inline static void define_ipc(ksys_ipc_buffer* buffer, size_t bufLen) { asm_inline( "int $0x40" - ::"a"(60), "b"(1), "c"(buffer + (sizeof(struct IPC_buffer) - sizeof(ksys_ipc_msg))), "d"(bufLen) + ::"a"(60), "b"(1), "c"(buffer), "d"(bufLen) ); } -inline static enum SendIPCErrors send_ipc(int pid, ksys_ipc_msg* msg) +inline static enum SendIPCErrors send_ipc(int pid, void* msg, size_t len) { enum SendIPCErrors ret; asm_inline( "int $0x40" : "=a"(ret) - : "a"(60), "b"(2), "c"(pid), "d"(msg), "S"((sizeof(ksys_ipc_msg)) + msg->datalen) + : "a"(60), "b"(2), "c"(pid), "d"(msg), "S"(len) ); return ret; @@ -27,20 +27,23 @@ inline static enum SendIPCErrors send_ipc(int pid, ksys_ipc_msg* msg) int syscalls_DefineIPCBuffer(lua_State* L) { uint32_t len = luaL_checkinteger(L, 1); - ksys_ipc_buffer* buffer = syscalls_pushIPC_buffer(L, len); + struct IPC_buffer* buffer = syscalls_pushIPC_buffer(L, len); - define_ipc(buffer, len); + define_ipc(IPC_buffer_to_ksys_ipc_buffer(buffer), len); return 1; } int syscalls_SendIPCMessage(lua_State* L) { + ksys_ipc_msg* msg = luaL_checkudata(L, 1, syscalls_IPC_msg_metatable_name); + lua_pushinteger( L, send_ipc( - luaL_checkinteger(L, 1), - luaL_checkudata(L, 2, syscalls_IPC_msg_metatable_name) + msg->pid, + msg + 8, + msg->datalen ) ); diff --git a/src/syscalls.c b/src/syscalls.c index f495896..720261b 100644 --- a/src/syscalls.c +++ b/src/syscalls.c @@ -1139,7 +1139,7 @@ typedef enum SYSCALLS_PROTOCOLS ARP = 5 } SYSCALLS_PROTOCOLS; -inline int syscalls_ReadPacketSend(lua_State* L, SYSCALLS_PROTOCOLS protocol) +static inline int syscalls_ReadPacketSend(lua_State* L, SYSCALLS_PROTOCOLS protocol) { uint32_t eax; uint8_t device = luaL_checkinteger(L, 1); @@ -1154,7 +1154,7 @@ inline int syscalls_ReadPacketSend(lua_State* L, SYSCALLS_PROTOCOLS protocol) return 1; } -inline int syscalls_ReadPacketReceive(lua_State* L, SYSCALLS_PROTOCOLS protocol) +static inline int syscalls_ReadPacketReceive(lua_State* L, SYSCALLS_PROTOCOLS protocol) { uint32_t eax; uint8_t device = luaL_checkinteger(L, 1); diff --git a/tests/ipc_get.lua b/tests/ipc_get.lua index 82e8521..ba3cb74 100644 --- a/tests/ipc_get.lua +++ b/tests/ipc_get.lua @@ -4,23 +4,32 @@ local syscalls = require("syscalls") +-- Enable IPC event only syscalls.SetEventMask(1 << (syscalls.Event.IPC - 1)) +-- write pid to /tmp0/1/lua_test_ipc_pid + local pid = syscalls.ThreadInfo().PID local f = io.open("/tmp0/1/lua_test_ipc_pid", "w") if f then - f:write(pid) + f:write(pid); f:close() end +------------ + local buffer = syscalls.DefineIPCBuffer(4096) +syscalls.IPC_buffer.Unlock(buffer) while true do + print("buffer:", buffer.used .. '/' .. buffer.size, "Locked:" .. tostring(buffer.lock)) if syscalls.WaitEvent() == syscalls.Event.IPC then - print(buffer:GetLastMessage()) + syscalls.IPC_buffer.Lock(buffer) + print("message:", syscalls.IPC_buffer.GetLastMessage(buffer)) + syscalls.IPC_buffer.Unlock(buffer) end end diff --git a/tests/ipc_send.lua b/tests/ipc_send.lua index c6fd08e..2bd7187 100644 --- a/tests/ipc_send.lua +++ b/tests/ipc_send.lua @@ -15,6 +15,7 @@ end if pid then while true do - print(syscalls.SendIPCMessage(pid, syscalls.IPC_msg.fromString("Test aboba"))) + local msg = syscalls.IPC_msg.fromString("Test aboba", pid) + print("Send:", msg, "State:", syscalls.SendIPCMessage(msg)) end end