Handle process arguments

This commit is contained in:
2025-09-10 23:25:03 +02:00
parent 2f9f4d9397
commit dc3d80d707
13 changed files with 127 additions and 23 deletions

View File

@ -186,7 +186,7 @@ void intr_handleintr(IntrStackFrame *frame) {
if (frame->trapnum <= 31) { if (frame->trapnum <= 31) {
kprintf("ERROR %s, 0x%lX\n", exceptions[frame->trapnum], frame->errnum); kprintf("ERROR %s, 0x%lX\n", exceptions[frame->trapnum], frame->errnum);
intr_dumpframe(frame); intr_dumpframe(frame);
if (frame->trapnum == 14 && frame->errnum & 0x4) { if ((frame->trapnum == 14 && frame->errnum & 0x4) || frame->trapnum == 0x6) {
kprintf("killed pid %ld %s\n", PROCS.current->pid, PROCS.current->name); kprintf("killed pid %ld %s\n", PROCS.current->pid, PROCS.current->name);
proc_killself(); proc_killself();
proc_sched((void *)frame); proc_sched((void *)frame);

View File

@ -1,8 +1,5 @@
#include "regs.S" #include "regs.S"
siema:
jmp siema
.global hal_switchproc .global hal_switchproc
hal_switchproc: hal_switchproc:
mov %cr3, %rcx mov %cr3, %rcx

View File

@ -198,7 +198,6 @@ Proc *proc_nextready(void) {
} }
proc = proc->next; proc = proc->next;
} }
return proc;
} }
void proc_reaper(void) { void proc_reaper(void) {
@ -243,6 +242,14 @@ void proc_reaper(void) {
} }
pmm_free((uintptr_t)zombie->platformdata.cr3, 1); pmm_free((uintptr_t)zombie->platformdata.cr3, 1);
ProcArg *arg = zombie->procargs.list;
while (arg) {
dlfree(arg->string);
ProcArg *tmp = arg;
arg = arg->next;
dlfree(tmp);
}
} }
dlfree(zombie); dlfree(zombie);
} else { } else {
@ -251,19 +258,21 @@ void proc_reaper(void) {
} }
} }
extern void hal_zombiespin(void);
void proc_sched(void *cpustate) { void proc_sched(void *cpustate) {
hal_intr_disable(); hal_intr_disable();
sched_ticks++; sched_ticks++;
if (sched_ticks % PROC_REAPER_FREQ == 0) {
proc_reaper();
}
IntrStackFrame *frame = cpustate; IntrStackFrame *frame = cpustate;
PROCS.current->platformdata.trapframe = *frame; PROCS.current->platformdata.trapframe = *frame;
PROCS.current = proc_nextready(); PROCS.current = proc_nextready();
if (sched_ticks % PROC_REAPER_FREQ == 0) {
proc_reaper();
}
hal_switchproc(&PROCS.current->platformdata.trapframe, (void *)PROCS.current->platformdata.cr3); hal_switchproc(&PROCS.current->platformdata.trapframe, (void *)PROCS.current->platformdata.cr3);
} }

View File

@ -7,6 +7,7 @@
#include "bitmap/bitmap.h" #include "bitmap/bitmap.h"
#include "vfs/vfs.h" #include "vfs/vfs.h"
#include "ipc/pipe/pipe.h" #include "ipc/pipe/pipe.h"
#include "sysdefs/processctl.h"
#define PROC_NAME_MAX 0x100 #define PROC_NAME_MAX 0x100
@ -33,6 +34,11 @@ enum {
PROC_WAITING = 3, PROC_WAITING = 3,
}; };
typedef struct ProcArg {
struct ProcArg *next;
char string[PROC_ARG_MAX];
} ProcArg;
typedef struct Proc { typedef struct Proc {
struct Proc *next; struct Proc *next;
@ -53,6 +59,10 @@ typedef struct Proc {
IpcPipe *list; IpcPipe *list;
SpinLock spinlock; SpinLock spinlock;
} bcast_pipes; } bcast_pipes;
struct {
ProcArg *list;
size_t len;
} procargs;
uint64_t mman_map_base; uint64_t mman_map_base;
} Proc; } Proc;
@ -79,4 +89,12 @@ void proc_kill(Proc *proc);
for(;;); \ for(;;); \
} while(0) } while(0)
#define PROC_ARG(proc, str) \
do { \
ProcArg *__arg = dlmalloc(sizeof(*__arg)); \
hal_strcpy(__arg->string, (str)); \
LL_APPEND((proc)->procargs.list, __arg); \
(proc)->procargs.len++; \
} while(0)
#endif // PROC_PROC_H_ #endif // PROC_PROC_H_

View File

@ -8,11 +8,12 @@
#include "vfs/vfs.h" #include "vfs/vfs.h"
#include "path/path.h" #include "path/path.h"
#include "kprintf.h" #include "kprintf.h"
#include "dlmalloc/malloc.h"
#define PCTL_MP_MAX 0xff #define PCTL_MP_MAX 0xff
#define PCTL_PATH_MAX VFS_PATH_MAX #define PCTL_PATH_MAX VFS_PATH_MAX
int32_t SYSCALL3(sys_processctl, pid1, cmd1, arg1) { int32_t SYSCALL5(sys_processctl, pid1, cmd1, arg1, arg2, arg3) {
uint64_t pid = pid1; uint64_t pid = pid1;
uint64_t cmd = cmd1; uint64_t cmd = cmd1;
int32_t ret = E_OK; int32_t ret = E_OK;
@ -58,6 +59,13 @@ int32_t SYSCALL3(sys_processctl, pid1, cmd1, arg1) {
ret = E_NOMEMORY; ret = E_NOMEMORY;
goto done; goto done;
} }
size_t argslen = arg3;
char **args = (char **)arg2;
for (size_t i = 0; i < argslen; i++) {
PROC_ARG(newproc, args[i]);
}
proc_register(newproc); proc_register(newproc);
ret = newproc->pid; ret = newproc->pid;
} break; } break;
@ -70,6 +78,32 @@ int32_t SYSCALL3(sys_processctl, pid1, cmd1, arg1) {
case PCTL_GETPID: { case PCTL_GETPID: {
ret = proc->pid; ret = proc->pid;
} break; } break;
case PCTL_ARGLEN: {
ret = proc->procargs.len;
} break;
case PCTL_ARGV: {
char **argbuf = (char **)arg1;
if (argbuf == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
size_t len = arg2;
ProcArg *arg = proc->procargs.list;
size_t i = 0;
while (arg) {
if (i == len) {
break;
}
if (argbuf[i] == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
hal_strcpy(argbuf[i], arg->string);
arg = arg->next;
i++;
}
ret = E_OK;
} break;
default: { default: {
ret = E_INVALIDARGUMENT; ret = E_INVALIDARGUMENT;
} break; } break;

View File

@ -4,6 +4,6 @@
#include <stdint.h> #include <stdint.h>
#include "syscall.h" #include "syscall.h"
int32_t SYSCALL3(sys_processctl, pid1, cmd1, arg1); int32_t SYSCALL5(sys_processctl, pid1, cmd1, arg1, arg2, arg3);
#endif // SYSCALL_PROCESSCTL_H_ #endif // SYSCALL_PROCESSCTL_H_

View File

@ -1,12 +1,16 @@
#ifndef SHARE_HDRS_PROCESSCTL_H_ #ifndef SHARE_HDRS_PROCESSCTL_H_
#define SHARE_HDRS_PROCESSCTL_H_ #define SHARE_HDRS_PROCESSCTL_H_
#define PROC_ARG_MAX 0x400
enum { enum {
PCTL_KILL = 0, PCTL_KILL = 0,
PCTL_SPAWN = 1, PCTL_SPAWN = 1,
PCTL_POLLSTATE = 2, PCTL_POLLSTATE = 2,
PCTL_RUN = 3, PCTL_RUN = 3,
PCTL_GETPID = 4, PCTL_GETPID = 4,
PCTL_ARGLEN = 5,
PCTL_ARGV = 6,
}; };
#endif // SHARE_HDRS_PROCESSCTL_H_ #endif // SHARE_HDRS_PROCESSCTL_H_

View File

@ -1,5 +1,10 @@
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include <system/system.h>
#include <sysdefs/processctl.h>
#include <errors.h>
#include <dlmalloc/malloc.h>
#include <uprintf.h>
extern void main(void); extern void main(void);
@ -14,9 +19,36 @@ void bss_clear(void) {
} }
} }
static char **_args;
static size_t _argslen;
char **args(void) {
return _args;
}
size_t argslen(void) {
return _argslen;
}
// ulib initialization goes here // ulib initialization goes here
void _premain(void) { void _premain(void) {
bss_clear(); bss_clear();
_argslen = processctl(-1, PCTL_ARGLEN, 0, 0, 0);
_args = dlmalloc(_argslen * sizeof(*_args));
if (_args == NULL) {
return;
}
for (size_t i = 0; i < _argslen; i++) {
_args[i] = dlmalloc(PROC_ARG_MAX);
if (_args[i] == NULL) {
return;
}
}
if (processctl(-1, PCTL_ARGV, (uint64_t)_args, _argslen, 0) != E_OK) {
return;
}
main(); main();
} }

7
ulib/args.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef ULIB_ARGS_H_
#define ULIB_ARGS_H_
char **args(void);
size_t argslen(void);
#endif // ULIB_ARGS_H_

View File

@ -11,8 +11,8 @@ int32_t ioctl(uint64_t ioh, uint64_t cmd, uint64_t arg1, uint64_t arg2, uint64_t
return syscall(SYS_IOCTL, ioh, cmd, arg1, arg2, arg3, 0); return syscall(SYS_IOCTL, ioh, cmd, arg1, arg2, arg3, 0);
} }
int32_t processctl(uint64_t pid, uint64_t cmd, uint64_t arg1) { int32_t processctl(uint64_t pid, uint64_t cmd, uint64_t arg1, uint64_t arg2, uint64_t arg3) {
return syscall(SYS_PROCESSCTL, pid, cmd, arg1, 0, 0, 0); return syscall(SYS_PROCESSCTL, pid, cmd, arg1, arg2, arg3, 0);
} }
int32_t ipcpipe(uint64_t pid, uint64_t pipenum, uint64_t cmd, uint8_t *buffer, size_t len) { int32_t ipcpipe(uint64_t pid, uint64_t pipenum, uint64_t cmd, uint8_t *buffer, size_t len) {

View File

@ -6,7 +6,7 @@
void debugprint(const char *string); void debugprint(const char *string);
int32_t ioctl(uint64_t ioh, uint64_t cmd, uint64_t arg1, uint64_t arg2, uint64_t arg3); int32_t ioctl(uint64_t ioh, uint64_t cmd, uint64_t arg1, uint64_t arg2, uint64_t arg3);
int32_t processctl(uint64_t pid, uint64_t cmd, uint64_t arg1); int32_t processctl(uint64_t pid, uint64_t cmd, uint64_t arg1, uint64_t arg2, uint64_t arg3);
int32_t ipcpipe(uint64_t pid, uint64_t pipenum, uint64_t cmd, uint8_t *buffer, size_t len); int32_t ipcpipe(uint64_t pid, uint64_t pipenum, uint64_t cmd, uint8_t *buffer, size_t len);
int32_t mman_map(uint8_t *addr, size_t size, uint64_t prot, uint64_t flags, uint8_t **out); int32_t mman_map(uint8_t *addr, size_t size, uint64_t prot, uint64_t flags, uint8_t **out);
int32_t mman_unmap(uint8_t *addr); int32_t mman_unmap(uint8_t *addr);

View File

@ -30,10 +30,12 @@ void main(void) {
uprintf("Hello world using uprintf\n"); uprintf("Hello world using uprintf\n");
int32_t tb = processctl(-1, PCTL_SPAWN, (uint64_t)"base:/bin/tb"); const char *tbargs[] = { "-i" };
ipcpipe(tb, IPCPIPE_OUT, IPCPIPE_REPLACE, (uint8_t *)(uint64_t)processctl(-1, PCTL_GETPID, 0), IPCPIPE_OUT); int32_t tb = processctl(-1, PCTL_SPAWN, (uint64_t)"base:/bin/tb", (uint64_t)tbargs, 1);
processctl(tb, PCTL_RUN, 0); uint64_t selfpid = (uint64_t)processctl(-1, PCTL_GETPID, 0, 0, 0);
while(processctl(tb, PCTL_POLLSTATE, 0) != 2); ipcpipe(tb, IPCPIPE_OUT, IPCPIPE_REPLACE, (uint8_t *)selfpid, IPCPIPE_OUT);
processctl(tb, PCTL_RUN, 0, 0, 0);
while(processctl(tb, PCTL_POLLSTATE, 0, 0, 0) != 2);
if (ipcpipe(IPCPIPE_SELFPID, 10, IPCPIPE_MAKE, NULL, 0) < 0) { if (ipcpipe(IPCPIPE_SELFPID, 10, IPCPIPE_MAKE, NULL, 0) < 0) {
uprintf("failed to create 10th pipe\n"); uprintf("failed to create 10th pipe\n");

View File

@ -1,11 +1,12 @@
#include <uprintf.h> #include <uprintf.h>
#include <dlmalloc/malloc.h> #include <dlmalloc/malloc.h>
#include <args.h>
void main(void) { void main(void) {
uprintf("Hello from tb!\n"); uprintf("Hello from tb!\n");
int *tmp = dlmalloc(sizeof(*tmp) * 1024); for (size_t i = 0; i < argslen(); i++) {
*tmp = 123456; uprintf("i = %d\n", i);
uprintf("*tmp = %d\n", *tmp); uprintf("arg: %s\n", args()[i]);
dlfree(tmp); }
} }