From 407b7298d75cfbb2d81b35558015e73715581baf Mon Sep 17 00:00:00 2001 From: Yuki Okumura Date: Thu, 26 Oct 2023 22:17:24 +0900 Subject: [PATCH] Create user process --- _hostwasm/hostwasm_main.c | 19 +++++++++++++ _hostwasm/runner/runner.cpp | 55 ++++++++++++++++++++++++++++++++++++- 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/_hostwasm/hostwasm_main.c b/_hostwasm/hostwasm_main.c index 574240043d2394..371bc42b567e7e 100644 --- a/_hostwasm/hostwasm_main.c +++ b/_hostwasm/hostwasm_main.c @@ -107,6 +107,25 @@ copy_init_thread_info(void){ memcpy(dummy_page, &init_thread_info, 0x220); } +long wasmlinux_create_process_ctx(void); +long wasmlinux_create_thread_ctx(void); +void wasmlinux_set_ctx(long ctx); + +uint32_t __attribute__((export_name ("taskmgmt"))) +taskmgmt(uint32_t op, uint32_t arg){ + switch(op){ + case 1: + return wasmlinux_create_process_ctx(); + case 2: + return wasmlinux_create_thread_ctx(); + case 3: + wasmlinux_set_ctx(arg); + return 0; + default: + return 0; + } +} + uint32_t __attribute__((export_name ("syscall"))) syscall(uint32_t no, uint32_t nargs, uint32_t* in){ return lkl_syscall(no, nargs, (long*)in); diff --git a/_hostwasm/runner/runner.cpp b/_hostwasm/runner/runner.cpp index d158bee9f40250..4116040eba959e 100644 --- a/_hostwasm/runner/runner.cpp +++ b/_hostwasm/runner/runner.cpp @@ -19,6 +19,21 @@ runsyscall32(uint32_t no, uint32_t nargs, uint32_t in){ return w2c_kernel_syscall(my_linux, no, nargs, in); } +static uint32_t +newtask_process(void){ + return w2c_kernel_taskmgmt(my_linux, 1, 0); +} + +static uint32_t +newtask_thread(void){ + return w2c_kernel_taskmgmt(my_linux, 2, 0); +} + +static void +newtask_apply(uint32_t ctx){ + w2c_kernel_taskmgmt(my_linux, 3, ctx); +} + std::mutex instancemtx; static void newinstance(){ @@ -524,6 +539,12 @@ debugwrite(uint32_t fd, const char* data, size_t len){ buf[2] = len; res = runsyscall32(64 /* __NR_write */, 3, ptr0); printf("write res = %d\n", res); + pool_free(buf); +} + +static uint32_t +debuggetpid(void){ + return runsyscall32(172 /* __NR_getpid */, 0, 0); } static int kfd_stdout; @@ -795,6 +816,34 @@ w2c_env_nccc_call64(struct w2c_env* env, u32 inptr, u32 outptr){ } } +static void +thr_user(uint32_t procctx){ + /* Allocate linux context */ + newinstance(); + prepare_newthread(); + + /* Assign process ctx */ + newtask_apply(procctx); + /* Sleep */ + for(;;){ + std::this_thread::sleep_for(std::chrono::seconds(1)); + debugwrite(kfd_stdout, "out\n", 4); + printf("(user) pid = %d\n", debuggetpid()); + } +} + +static void +spawn_user(void){ + uint32_t procctx; + std::thread* thr; + /* fork */ + procctx = newtask_process(); + printf("procctx = %d\n", procctx); + + thr = new std::thread(thr_user, procctx); + thr->detach(); +} + int main(int ac, char** av){ int i; @@ -841,10 +890,14 @@ main(int ac, char** av){ /* Create debug I/O thread */ spawn_debugiothread(); + printf("(init) pid = %d\n", debuggetpid()); + + /* Create user program */ + spawn_user(); + /* Sleep */ for(;;){ std::this_thread::sleep_for(std::chrono::seconds(1)); - debugwrite(kfd_stdout, "out\n", 4); } return 0;