Handle process arguments
This commit is contained in:
@ -186,7 +186,7 @@ void intr_handleintr(IntrStackFrame *frame) {
|
||||
if (frame->trapnum <= 31) {
|
||||
kprintf("ERROR %s, 0x%lX\n", exceptions[frame->trapnum], frame->errnum);
|
||||
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);
|
||||
proc_killself();
|
||||
proc_sched((void *)frame);
|
||||
|
@ -1,8 +1,5 @@
|
||||
#include "regs.S"
|
||||
|
||||
siema:
|
||||
jmp siema
|
||||
|
||||
.global hal_switchproc
|
||||
hal_switchproc:
|
||||
mov %cr3, %rcx
|
||||
|
@ -198,7 +198,6 @@ Proc *proc_nextready(void) {
|
||||
}
|
||||
proc = proc->next;
|
||||
}
|
||||
return proc;
|
||||
}
|
||||
|
||||
void proc_reaper(void) {
|
||||
@ -243,6 +242,14 @@ void proc_reaper(void) {
|
||||
}
|
||||
|
||||
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);
|
||||
} else {
|
||||
@ -251,19 +258,21 @@ void proc_reaper(void) {
|
||||
}
|
||||
}
|
||||
|
||||
extern void hal_zombiespin(void);
|
||||
|
||||
void proc_sched(void *cpustate) {
|
||||
hal_intr_disable();
|
||||
sched_ticks++;
|
||||
|
||||
if (sched_ticks % PROC_REAPER_FREQ == 0) {
|
||||
proc_reaper();
|
||||
}
|
||||
|
||||
IntrStackFrame *frame = cpustate;
|
||||
|
||||
PROCS.current->platformdata.trapframe = *frame;
|
||||
|
||||
|
||||
PROCS.current = proc_nextready();
|
||||
|
||||
if (sched_ticks % PROC_REAPER_FREQ == 0) {
|
||||
proc_reaper();
|
||||
}
|
||||
|
||||
hal_switchproc(&PROCS.current->platformdata.trapframe, (void *)PROCS.current->platformdata.cr3);
|
||||
}
|
||||
|
@ -7,6 +7,7 @@
|
||||
#include "bitmap/bitmap.h"
|
||||
#include "vfs/vfs.h"
|
||||
#include "ipc/pipe/pipe.h"
|
||||
#include "sysdefs/processctl.h"
|
||||
|
||||
#define PROC_NAME_MAX 0x100
|
||||
|
||||
@ -33,6 +34,11 @@ enum {
|
||||
PROC_WAITING = 3,
|
||||
};
|
||||
|
||||
typedef struct ProcArg {
|
||||
struct ProcArg *next;
|
||||
char string[PROC_ARG_MAX];
|
||||
} ProcArg;
|
||||
|
||||
typedef struct Proc {
|
||||
struct Proc *next;
|
||||
|
||||
@ -53,6 +59,10 @@ typedef struct Proc {
|
||||
IpcPipe *list;
|
||||
SpinLock spinlock;
|
||||
} bcast_pipes;
|
||||
struct {
|
||||
ProcArg *list;
|
||||
size_t len;
|
||||
} procargs;
|
||||
|
||||
uint64_t mman_map_base;
|
||||
} Proc;
|
||||
@ -79,4 +89,12 @@ void proc_kill(Proc *proc);
|
||||
for(;;); \
|
||||
} 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_
|
||||
|
@ -8,11 +8,12 @@
|
||||
#include "vfs/vfs.h"
|
||||
#include "path/path.h"
|
||||
#include "kprintf.h"
|
||||
#include "dlmalloc/malloc.h"
|
||||
|
||||
#define PCTL_MP_MAX 0xff
|
||||
#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 cmd = cmd1;
|
||||
int32_t ret = E_OK;
|
||||
@ -58,6 +59,13 @@ int32_t SYSCALL3(sys_processctl, pid1, cmd1, arg1) {
|
||||
ret = E_NOMEMORY;
|
||||
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);
|
||||
ret = newproc->pid;
|
||||
} break;
|
||||
@ -70,6 +78,32 @@ int32_t SYSCALL3(sys_processctl, pid1, cmd1, arg1) {
|
||||
case PCTL_GETPID: {
|
||||
ret = proc->pid;
|
||||
} 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: {
|
||||
ret = E_INVALIDARGUMENT;
|
||||
} break;
|
||||
|
@ -4,6 +4,6 @@
|
||||
#include <stdint.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_
|
||||
|
@ -1,12 +1,16 @@
|
||||
#ifndef SHARE_HDRS_PROCESSCTL_H_
|
||||
#define SHARE_HDRS_PROCESSCTL_H_
|
||||
|
||||
#define PROC_ARG_MAX 0x400
|
||||
|
||||
enum {
|
||||
PCTL_KILL = 0,
|
||||
PCTL_SPAWN = 1,
|
||||
PCTL_POLLSTATE = 2,
|
||||
PCTL_RUN = 3,
|
||||
PCTL_GETPID = 4,
|
||||
PCTL_ARGLEN = 5,
|
||||
PCTL_ARGV = 6,
|
||||
};
|
||||
|
||||
#endif // SHARE_HDRS_PROCESSCTL_H_
|
||||
|
@ -1,5 +1,10 @@
|
||||
#include <stdint.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);
|
||||
|
||||
@ -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
|
||||
void _premain(void) {
|
||||
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();
|
||||
}
|
||||
|
||||
|
7
ulib/args.h
Normal file
7
ulib/args.h
Normal file
@ -0,0 +1,7 @@
|
||||
#ifndef ULIB_ARGS_H_
|
||||
#define ULIB_ARGS_H_
|
||||
|
||||
char **args(void);
|
||||
size_t argslen(void);
|
||||
|
||||
#endif // ULIB_ARGS_H_
|
@ -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);
|
||||
}
|
||||
|
||||
int32_t processctl(uint64_t pid, uint64_t cmd, uint64_t arg1) {
|
||||
return syscall(SYS_PROCESSCTL, pid, cmd, arg1, 0, 0, 0);
|
||||
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, arg2, arg3, 0);
|
||||
}
|
||||
|
||||
int32_t ipcpipe(uint64_t pid, uint64_t pipenum, uint64_t cmd, uint8_t *buffer, size_t len) {
|
||||
|
@ -6,7 +6,7 @@
|
||||
|
||||
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 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 mman_map(uint8_t *addr, size_t size, uint64_t prot, uint64_t flags, uint8_t **out);
|
||||
int32_t mman_unmap(uint8_t *addr);
|
||||
|
@ -30,10 +30,12 @@ void main(void) {
|
||||
|
||||
uprintf("Hello world using uprintf\n");
|
||||
|
||||
int32_t tb = processctl(-1, PCTL_SPAWN, (uint64_t)"base:/bin/tb");
|
||||
ipcpipe(tb, IPCPIPE_OUT, IPCPIPE_REPLACE, (uint8_t *)(uint64_t)processctl(-1, PCTL_GETPID, 0), IPCPIPE_OUT);
|
||||
processctl(tb, PCTL_RUN, 0);
|
||||
while(processctl(tb, PCTL_POLLSTATE, 0) != 2);
|
||||
const char *tbargs[] = { "-i" };
|
||||
int32_t tb = processctl(-1, PCTL_SPAWN, (uint64_t)"base:/bin/tb", (uint64_t)tbargs, 1);
|
||||
uint64_t selfpid = (uint64_t)processctl(-1, PCTL_GETPID, 0, 0, 0);
|
||||
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) {
|
||||
uprintf("failed to create 10th pipe\n");
|
||||
|
@ -1,11 +1,12 @@
|
||||
#include <uprintf.h>
|
||||
#include <dlmalloc/malloc.h>
|
||||
#include <args.h>
|
||||
|
||||
void main(void) {
|
||||
uprintf("Hello from tb!\n");
|
||||
|
||||
int *tmp = dlmalloc(sizeof(*tmp) * 1024);
|
||||
*tmp = 123456;
|
||||
uprintf("*tmp = %d\n", *tmp);
|
||||
dlfree(tmp);
|
||||
for (size_t i = 0; i < argslen(); i++) {
|
||||
uprintf("i = %d\n", i);
|
||||
uprintf("arg: %s\n", args()[i]);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user