From 9fbe23024c4e8638c7d0b1b560855ed315153032 Mon Sep 17 00:00:00 2001 From: kamkow1 Date: Tue, 28 Apr 2026 19:48:34 +0200 Subject: [PATCH] Implement passing commandline strings --- ce/ce.c | 4 ++++ include/syscall_defs.h | 1 + init/init.c | 2 +- kernel/amd64/proc.h | 2 ++ kernel/proc/procgroup.c | 9 +++++++++ kernel/proc/procgroup.h | 7 +++++-- kernel/syscall/syscall.c | 22 ++++++++++++++++++++-- libu/system.c | 6 +++++- libu/system.h | 5 ++++- 9 files changed, 51 insertions(+), 7 deletions(-) diff --git a/ce/ce.c b/ce/ce.c index 1dc5e8f..bed7a77 100644 --- a/ce/ce.c +++ b/ce/ce.c @@ -103,6 +103,10 @@ void app_main(void) { char printcmdsbuf[4]; char line[1024]; + const char* cmdline = get_cmdline(); + + debug_printf("cmdline=\"%s\"\n", cmdline); + if (env_get(process_get_pgid(), "printcmds", (void*)printcmdsbuf, sizeof(printcmdsbuf)) == ST_OK && strcmp(printcmdsbuf, "yes") == 0) { diff --git a/include/syscall_defs.h b/include/syscall_defs.h index 4333541..ad8078e 100644 --- a/include/syscall_defs.h +++ b/include/syscall_defs.h @@ -42,5 +42,6 @@ #define SYS_GET_VOLUME_INFO 39 #define SYS_VOLUME_DELETE 40 #define SYS_DATE_TIME 41 +#define SYS_GET_CMDLINE 42 #endif // _M_SYSCALL_DEFS_H diff --git a/init/init.c b/init/init.c index 2c55924..d392022 100644 --- a/init/init.c +++ b/init/init.c @@ -28,7 +28,7 @@ static void receiver(void* arg) { } static void run_ce_interactive(void) { - int ce_pid = exec("sys", "/ce"); + int ce_pid = exec("sys", "/ce", "-i"); ce_pgid = get_procgroup(ce_pid); process_spawn(&receiver, NULL); diff --git a/kernel/amd64/proc.h b/kernel/amd64/proc.h index 56a3494..fb789ac 100644 --- a/kernel/amd64/proc.h +++ b/kernel/amd64/proc.h @@ -12,6 +12,8 @@ /* proc_map () base address */ #define PROC_MAP_BASE 0x0000700000000000 +#define PROC_CMDLINE_BASE 0x0000100000000000 + /* Platform-dependent process data */ struct proc_platformdata { uint8_t fx_env[512] ALIGNED(16); diff --git a/kernel/proc/procgroup.c b/kernel/proc/procgroup.c index 0904f28..380af88 100644 --- a/kernel/proc/procgroup.c +++ b/kernel/proc/procgroup.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -132,6 +133,8 @@ bool procgroup_unmap(struct procgroup* procgroup, uintptr_t start_vaddr, size_t } struct procgroup* procgroup_create(void) { + struct limine_hhdm_response* hhdm = limine_hhdm_request.response; + struct procgroup* procgroup = malloc(sizeof(*procgroup)); if (procgroup == NULL) { return NULL; @@ -156,6 +159,10 @@ struct procgroup* procgroup_create(void) { procgroup->pd.cr3_paddr = mm_alloc_user_pd_phys(); procgroup->map_base = PROC_MAP_BASE; + procgroup->uvaddr_cmdline = procgroup_map(procgroup, PROC_CMDLINE_BASE, 1, + MM_PG_PRESENT | MM_PG_USER, &procgroup->paddr_cmdline); + memset((void*)((uintptr_t)hhdm->offset + procgroup->paddr_cmdline), 0, PROCGROUP_CMDLINE_MAX); + if (proc_create_resource_mail(procgroup) == NULL) { id_alloc_fini(&procgroup->rid_alloc); free(procgroup); @@ -214,6 +221,8 @@ static void procgroup_delete(struct procgroup* procgroup, struct reschedule_ctx* proc_env_cleanup(procgroup); + pmm_free(procgroup->paddr_cmdline, 1); + pmm_free(procgroup->pd.cr3_paddr, 1); free(procgroup->tls.tls_tmpl); diff --git a/kernel/proc/procgroup.h b/kernel/proc/procgroup.h index a2b1bff..beaa031 100644 --- a/kernel/proc/procgroup.h +++ b/kernel/proc/procgroup.h @@ -5,14 +5,15 @@ #include #include #include +#include #include #include #include #include #include -#define PROCGROUP_VFS_HANDLES_MAX 256 -#define PROCGROUP_RESOURCES_MAX 256 +#define PROCGROUP_RESOURCES_MAX 256 +#define PROCGROUP_CMDLINE_MAX PAGE_SIZE struct proc; struct reschedule_ctx; @@ -36,6 +37,8 @@ struct procgroup { uintptr_t map_base; struct procgroup_tls tls; struct proc_env env; + uintptr_t uvaddr_cmdline; + uintptr_t paddr_cmdline; }; struct procgroup* procgroup_create(void); diff --git a/kernel/syscall/syscall.c b/kernel/syscall/syscall.c index 2b0e993..7bc7b33 100644 --- a/kernel/syscall/syscall.c +++ b/kernel/syscall/syscall.c @@ -289,10 +289,11 @@ DEFINE_SYSCALL(sys_device_do) { return SYSRESULT(ret); } -/* int exec (char* volume, char* path) */ +/* int exec (char* volume, char* path, char* cmdline) */ DEFINE_SYSCALL(sys_exec) { uintptr_t uvaddr_volume = a1; uintptr_t uvaddr_path = a2; + uintptr_t uvaddr_cmdline = a3; struct limine_hhdm_response* hhdm = limine_hhdm_request.response; @@ -316,12 +317,21 @@ DEFINE_SYSCALL(sys_exec) { const char* volume = (const char*)((uintptr_t)hhdm->offset + out_paddr); + out_paddr = mm_v2p(&procgroup->pd, uvaddr_cmdline); + + char* cmdline = (out_paddr != 0) ? (char*)((uintptr_t)hhdm->offset + out_paddr) : NULL; + struct proc* new = proc_from_file(proc, volume, path, rctx); if (new == NULL) { return SYSRESULT(-ST_EXEC_ERROR); } + if (cmdline != NULL) { + void* cmdline_dest = (void*)((uintptr_t)hhdm->offset + new->procgroup->paddr_cmdline); + strncpy(cmdline_dest, cmdline, PAGE_SIZE); + } + int pid = new->pid; new->exec_pid = pid1; @@ -931,7 +941,14 @@ DEFINE_SYSCALL(sys_date_time) { date_time(dt); - return ST_OK; + return SYSRESULT(ST_OK); +} + +/* const char* get_cmdline(void) */ +DEFINE_SYSCALL(sys_get_cmdline) { + struct procgroup* procgroup = proc->procgroup; + + return SYSRESULT(procgroup->uvaddr_cmdline); } static syscall_handler_func_t handler_table[] = { @@ -976,6 +993,7 @@ static syscall_handler_func_t handler_table[] = { [SYS_GET_VOLUME_INFO] = &sys_get_volume_info, [SYS_VOLUME_DELETE] = &sys_volume_delete, [SYS_DATE_TIME] = &sys_date_time, + [SYS_GET_CMDLINE] = &sys_get_cmdline, }; syscall_handler_func_t syscall_find_handler(int syscall_num) { diff --git a/libu/system.c b/libu/system.c index 7ad9078..ce51114 100644 --- a/libu/system.c +++ b/libu/system.c @@ -39,7 +39,9 @@ int device_do(const char* device_key, int cmd, void* a1, void* a2, void* a3, voi return (int)do_syscall(SYS_DEVICE_DO, device_key, cmd, a1, a2, a3, a4); } -int exec(const char* volume, const char* path) { return (int)do_syscall(SYS_EXEC, volume, path); } +int exec(const char* volume, const char* path, const char* cmdline) { + return (int)do_syscall(SYS_EXEC, volume, path, cmdline); +} int volume_open(const char* volume) { return (int)do_syscall(SYS_VOLUME_OPEN, volume); } @@ -126,3 +128,5 @@ int get_volume_info(struct volume_info* infos, size_t count) { int volume_delete(const char* key) { return (int)do_syscall(SYS_VOLUME_DELETE, key); } int date_time(struct date_time* dt) { return (int)do_syscall(SYS_DATE_TIME, dt); } + +const char* get_cmdline(void) { return (const char*)do_syscall(SYS_GET_CMDLINE, 0); } diff --git a/libu/system.h b/libu/system.h index fef7b16..c2f5737 100644 --- a/libu/system.h +++ b/libu/system.h @@ -47,7 +47,7 @@ void* argument_ptr(void); int device_do(const char* device_key, int cmd, void* a1, void* a2, void* a3, void* a4); /* Run external ELF program */ -int exec(const char* volume, const char* path); +int exec(const char* volume, const char* path, const char* cmdline); /* Open a file */ int volume_open(const char* volume); @@ -133,4 +133,7 @@ int volume_delete(const char* key); /* Get date-time */ int date_time(struct date_time* dt); +/* Get commandline string */ +const char* get_cmdline(void); + #endif // _LIBMSL_M_SYSTEM_H