#include "ipc.hpp" #include "stdio.h" #include "string.h" #include "abi/syscalls.h" #define PROCMGR_ENDPOINT 4 #define PROC_OP_LIST 1 #define PROC_OP_KILL 2 #define PROC_OP_SPAWN 3 #define PROC_OP_STAT 4 extern "C" void _start() { puts("[PROCMGR] Process & Server Manager Daemon Initialized (Ring 3)."); uint64_t last_client = 0; uint64_t reply_val0 = 0; uint64_t reply_val1 = 0; while (true) { uint64_t sender_tid = 0; uint64_t opcode = 0; uint64_t arg0 = 0; uint64_t arg1 = 0; sysret_t ret = sys_ipc_reply_recv_raw( PROCMGR_ENDPOINT, last_client, reply_val0, reply_val1, &sender_tid, &opcode, &arg0, &arg1 ); if (ret == 0 && sender_tid != 0) { last_client = sender_tid; if (opcode == PROC_OP_KILL) { // Terminate process with TID = arg0 register uint64_t rax __asm__("rax") = SYS_PROC_KILL; register uint64_t rdi __asm__("rdi") = arg0; __asm__ volatile("syscall" : "+r"(rax) : "r"(rdi) : "rcx", "r11", "memory"); reply_val0 = rax; // 0 = success, -1 = error reply_val1 = 0; } else if (opcode == PROC_OP_LIST) { // Query real thread count from kernel thread_info_t list[16]; register uint64_t rax __asm__("rax") = SYS_PROC_LIST; register uint64_t rdi __asm__("rdi") = (uint64_t)list; register uint64_t rsi __asm__("rsi") = 16; __asm__ volatile("syscall" : "+r"(rax) : "r"(rdi), "r"(rsi) : "rcx", "r11", "memory"); reply_val0 = (rax > 0) ? rax : 0; reply_val1 = 0; } else if (opcode == PROC_OP_SPAWN) { // Program name passed in arg0 (8 bytes) + arg1 (8 bytes) char prog[20] = {0}; memcpy(prog, &arg0, 8); memcpy(prog + 8, &arg1, 8); int64_t tid = sys_module_exec(prog); if (tid < 0 && !strstr(prog, ".")) { char opc_name[28]; snprintf(opc_name, sizeof(opc_name), "%s.opc", prog); tid = sys_module_exec(opc_name); if (tid < 0) { char elf_name[28]; snprintf(elf_name, sizeof(elf_name), "%s.elf", prog); tid = sys_module_exec(elf_name); } } reply_val0 = (uint64_t)tid; reply_val1 = 0; } else { reply_val0 = 0; reply_val1 = 0; } } else { last_client = 0; sys_yield(); } } }