Compare commits

..

4 Commits

Author SHA1 Message Date
0fb63b4695 pctl Add kill command 2025-09-29 21:43:28 +02:00
46e52c8d48 tb %PID builtin command 2025-09-29 21:37:48 +02:00
4e8afae5fb all procs are user procs 2025-09-29 21:32:07 +02:00
20a89502c0 Remove kernel procs 2025-09-29 21:27:46 +02:00
16 changed files with 62 additions and 195 deletions

View File

@ -53,8 +53,6 @@ SRCFILES += $(call GRABSRC, \
fs/portlfs \ fs/portlfs \
baseimg \ baseimg \
proc \ proc \
proc/kproc \
proc/serialproc \
hal \ hal \
hal/$(ARCH) \ hal/$(ARCH) \
std \ std \

View File

@ -1,16 +0,0 @@
#include <stdint.h>
#include "proc/proc.h"
#include "hal/hal.h"
#include "ipc/pipe/pipe.h"
#include "kprintf.h"
Proc *KPROC;
void kproc_init(Proc *proc) {
KPROC = proc;
}
void kproc_fn(void) {
for (;;) {
}
}

View File

@ -1,11 +0,0 @@
#ifndef PROC_KPROC_KPROC_H_
#define PROC_KPROC_KPROC_H_
#include "proc/proc.h"
extern Proc *KPROC;
void kproc_fn(void);
void kproc_init(Proc *proc);
#endif // PROC_KPROC_KPROC_H_

View File

@ -13,8 +13,6 @@
#include "vfs/vfs.h" #include "vfs/vfs.h"
#include "bootinfo/bootinfo.h" #include "bootinfo/bootinfo.h"
#include "ipc/pipe/pipe.h" #include "ipc/pipe/pipe.h"
#include "kproc/kproc.h"
#include "serialproc/serialproc.h"
#include "sysdefs/processctl.h" #include "sysdefs/processctl.h"
#define PROC_REAPER_FREQ 30 #define PROC_REAPER_FREQ 30
@ -75,41 +73,6 @@ ElfAuxval proc_load_elf_segs(Proc *proc, uint8_t *data) {
return aux; return aux;
} }
Proc *proc_spawnkern(void (*ent)(void), char *name) {
if (pids >= PROC_MAX) {
return NULL;
}
Proc *proc = dlmalloc(sizeof(*proc));
if (proc == NULL) {
return NULL;
}
hal_memset(proc, 0, sizeof(*proc));
hal_memcpy(proc->name, name, PROC_NAME_MAX);
proc->kern = true;
uint8_t *pstackp = (uint8_t *)pmm_alloc(PROC_STACKBLOCKS) + PROC_STACKSIZE;
uint8_t *kstackp = (uint8_t *)pmm_alloc(PROC_STACKBLOCKS) + PROC_STACKSIZE;
proc->platformdata.kstack = kstackp;
proc->platformdata.pstack = pstackp;
hal_memset(&proc->platformdata.trapframe, 0, sizeof(proc->platformdata.trapframe));
proc->platformdata.trapframe.ss = 0x10;
proc->platformdata.trapframe.rsp = (uint64_t)VIRT(pstackp);
proc->platformdata.trapframe.rflags = 0x202;
proc->platformdata.trapframe.cs = 0x08;
proc->platformdata.trapframe.rip = (uint64_t)ent;
proc->platformdata.cr3 = KERNEL_CR3;
proc->state = PROC_EMBRYO;
proc->pid = pids++;
spinlock_init(&proc->bcast_pipes.spinlock);
spinlock_init(&proc->pipes_spinlock);
return proc;
}
Proc *proc_spawnuser(char *mountpoint, char *path) { Proc *proc_spawnuser(char *mountpoint, char *path) {
VfsObj *vobj = vfs_open(mountpoint, path, VFS_FLAG_READ); VfsObj *vobj = vfs_open(mountpoint, path, VFS_FLAG_READ);
if (vobj == NULL) { if (vobj == NULL) {
@ -237,21 +200,19 @@ void proc_reaper(void) {
pmm_free((uintptr_t)(zombie->platformdata.kstack - PROC_STACKSIZE), PROC_STACKBLOCKS); pmm_free((uintptr_t)(zombie->platformdata.kstack - PROC_STACKSIZE), PROC_STACKBLOCKS);
pmm_free((uintptr_t)(zombie->platformdata.pstack - PROC_STACKSIZE), PROC_STACKBLOCKS); pmm_free((uintptr_t)(zombie->platformdata.pstack - PROC_STACKSIZE), PROC_STACKBLOCKS);
if (!zombie->kern) { VasRange *vas, *vastmp;
VasRange *vas, *vastmp; LL_FOREACH_SAFE(zombie->vas, vas, vastmp) {
LL_FOREACH_SAFE(zombie->vas, vas, vastmp) { hal_vmm_unmap_range(zombie->platformdata.cr3, vas->virtstart, vas->physstart, vas->size);
hal_vmm_unmap_range(zombie->platformdata.cr3, vas->virtstart, vas->physstart, vas->size); pmm_free((uintptr_t)vas->physstart, vas->size / HAL_PAGE_SIZE);
pmm_free((uintptr_t)vas->physstart, vas->size / HAL_PAGE_SIZE); dlfree(vas);
dlfree(vas); }
}
pmm_free((uintptr_t)zombie->platformdata.cr3, 1); pmm_free((uintptr_t)zombie->platformdata.cr3, 1);
ProcArg *arg, *argtmp; ProcArg *arg, *argtmp;
LL_FOREACH_SAFE(zombie->procargs.list, arg, argtmp) { LL_FOREACH_SAFE(zombie->procargs.list, arg, argtmp) {
dlfree(arg->string); dlfree(arg->string);
dlfree(arg); dlfree(arg);
}
} }
dlfree(zombie); dlfree(zombie);
} }
@ -287,22 +248,6 @@ void proc_killself(void) {
spinlock_release(&PROCS.spinlock); spinlock_release(&PROCS.spinlock);
} }
void proc_status(void) {
static const char *statuses[] = {"ready", "running", "zombie", "waiting"};
for (;;) {
spinlock_acquire(&PROCS.spinlock);
Proc *head = PROCS.procs;
while (head) {
kprintf("%s %s %s\n", head->kern ? "kern" : "user", statuses[head->state], head->name);
head = head->next;
}
kprintf("\n\n");
spinlock_release(&PROCS.spinlock);
hal_wait(3 * 1000);
}
}
void proc_init(void) { void proc_init(void) {
spinlock_init(&PROCS.spinlock); spinlock_init(&PROCS.spinlock);
PROCS.procs = NULL; PROCS.procs = NULL;

View File

@ -54,8 +54,6 @@ typedef struct Proc {
uint8_t state; uint8_t state;
VasRange *vas; VasRange *vas;
bool kern;
VfsObj *vobjs[PROC_VFSHANDLES_MAX]; VfsObj *vobjs[PROC_VFSHANDLES_MAX];
uint64_t vobjcnt; uint64_t vobjcnt;
@ -88,7 +86,6 @@ extern Procs PROCS;
void proc_init(void); void proc_init(void);
void proc_register(Proc *proc); void proc_register(Proc *proc);
Proc *proc_spawnkern(void (*ent)(void), char *name);
Proc *proc_spawnuser(char *mountpoint, char *path); Proc *proc_spawnuser(char *mountpoint, char *path);
void proc_sched(void *cpustate); void proc_sched(void *cpustate);
void proc_killself(void); void proc_killself(void);

View File

@ -1,79 +0,0 @@
#include <stdint.h>
#include <stddef.h>
#include "proc/proc.h"
#include "ipc/pipe/pipe.h"
#include "hal/hal.h"
#include "dlmalloc/malloc.h"
#define SERIAL_PORT 0x3f8
Proc *SERIALPROC;
void serialproc_init(Proc *proc) {
SERIALPROC = proc;
SERIALPROC->pipes[0] = dlmalloc(sizeof(IpcPipe));
ipc_pipeinit(SERIALPROC->pipes[0], SERIALPROC->pid);
SERIALPROC->pipes[1] = dlmalloc(sizeof(IpcPipe));
ipc_pipeinit(SERIALPROC->pipes[1], SERIALPROC->pid);
io_out8(SERIAL_PORT + 1, 0x00);
io_out8(SERIAL_PORT + 3, 0x80);
io_out8(SERIAL_PORT + 0, 0x03);
io_out8(SERIAL_PORT + 1, 0x00);
io_out8(SERIAL_PORT + 3, 0x03);
io_out8(SERIAL_PORT + 2, 0xc7);
io_out8(SERIAL_PORT + 4, 0x0b);
io_out8(SERIAL_PORT + 4, 0x1e);
io_out8(SERIAL_PORT + 0, 0xae);
if (io_in8(SERIAL_PORT + 0) != 0xae) {
return;
}
io_out8(SERIAL_PORT + 4, 0x0f);
}
int serialproc_received(void) {
return io_in8(SERIAL_PORT + 5) & 1;
}
uint8_t serialproc_read(void) {
while (serialproc_received() == 0);
return io_in8(SERIAL_PORT);
}
int serialproc_trans_empty(void) {
return io_in8(SERIAL_PORT + 5) & 0x20;
}
void serialproc_write(uint8_t value) {
while (!serialproc_trans_empty());
io_out8(SERIAL_PORT, value);
}
void serialproc_fn(void) {
char buf[0x100];
for (;;) {
hal_memset(buf, 0, sizeof(buf));
spinlock_acquire(&SERIALPROC->pipes_spinlock);
IpcPipe *inpipe = SERIALPROC->pipes[1];
spinlock_release(&SERIALPROC->pipes_spinlock);
int32_t read = ipc_piperead(inpipe, (uint8_t *)buf, sizeof(buf));
if (read > 0) {
for (size_t i = 0; i < sizeof(buf); i++) {
serialproc_write(buf[i]);
}
}
if (serialproc_received() == 0) {
continue;
}
uint8_t inchar = io_in8(SERIAL_PORT);
spinlock_acquire(&SERIALPROC->pipes_spinlock);
IpcPipe *outpipe = SERIALPROC->pipes[0];
spinlock_release(&SERIALPROC->pipes_spinlock);
ipc_pipewrite(outpipe, &inchar, sizeof(inchar));
}
}

View File

@ -1,11 +0,0 @@
#ifndef PROC_SERIALPROC_SERIALPROC_H_
#define PROC_SERIALPROC_SERIALPROC_H_
#include "proc/proc.h"
extern Proc *SERIALPROC;
void serialproc_init(Proc *proc);
void serialproc_fn(void);
#endif // PROC_SERIALPROC_SERIALPROC_H_

View File

@ -139,7 +139,6 @@ int32_t SYSCALL5(sys_processctl, pid1, cmd1, arg1, arg2, arg3) {
stat->pid = p->pid; stat->pid = p->pid;
hal_strcpy(stat->name, p->name); hal_strcpy(stat->name, p->name);
stat->state = p->state; stat->state = p->state;
stat->kern = p->kern;
VasRange *vas, *vastmp; VasRange *vas, *vastmp;
LL_FOREACH_SAFE(p->vas, vas, vastmp) { LL_FOREACH_SAFE(p->vas, vas, vastmp) {

View File

@ -20,7 +20,8 @@ typedef struct {
char name[0x100]; char name[0x100];
uint8_t state; uint8_t state;
size_t usemem; size_t usemem;
bool kern;
} ProcStat; } ProcStat;
typedef uint64_t PID_t;
#endif // SHARE_HDRS_PROCESSCTL_H_ #endif // SHARE_HDRS_PROCESSCTL_H_

28
user/pctl/kill.c Normal file
View File

@ -0,0 +1,28 @@
#include <stddef.h>
#include <stdint.h>
#include <ulib.h>
#include "kill.h"
#include "macros.h"
struct {
int32_t pid;
} PCTL_KILL_CONFIG = {
.pid = -1,
};
static Arg ARGS[] = {
ARG("-pid", ARG_INT, &PCTL_KILL_CONFIG.pid),
ARG_END(),
};
void pctl_kill(void) {
int32_t ret;
if ((ret = parse_args(SUBCMD_ARGS(), SUBCMD_ARGSLEN(), ARGS)) < 0) {
uprintf("pctl kill: Could not parse args: %d\n", ret);
return;
}
if (PCTL_KILL_CONFIG.pid != -1) {
processctl(PCTL_KILL_CONFIG.pid, PCTL_KILL, 0, 0, 0);
}
}

6
user/pctl/kill.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef PCTL_KILL_H_
#define PCTL_KILL_H_
void pctl_kill(void);
#endif // PCTL_KILL_H_

View File

@ -64,7 +64,7 @@ void pctl_ls(void) {
char *namebuf = umalloc(34); char *namebuf = umalloc(34);
char *membuf = umalloc(20); char *membuf = umalloc(20);
uprintf("%-30s %s %-6s %-15s %-8s\n", "NAME", "PID", "TYPE", "MEMORY", "STATE"); uprintf("%-30s %s %-15s %-8s\n", "NAME", "PID", "MEMORY", "STATE");
for (size_t i = 0; i < procslen; i++) { for (size_t i = 0; i < procslen; i++) {
ProcStat stat; ZERO(&stat); ProcStat stat; ZERO(&stat);
@ -86,10 +86,9 @@ void pctl_ls(void) {
namebuf[31] = namebuf[32] = namebuf[33] = '.'; namebuf[31] = namebuf[32] = namebuf[33] = '.';
// Too big format string causes a stack overflow. This is an issue with printf ;( // Too big format string causes a stack overflow. This is an issue with printf ;(
uprintf("%-30s %-3lu %-6s %-15s ", uprintf("%-30s %-3lu %-15s ",
namebuf, namebuf,
stat.pid, stat.pid,
stat.kern ? "KERNEL" : "USER",
human_size(stat.usemem, membuf, 20) human_size(stat.usemem, membuf, 20)
); );
uprintf("%-8s", states[stat.state]); uprintf("%-8s", states[stat.state]);

View File

@ -2,6 +2,7 @@
#include <stdint.h> #include <stdint.h>
#include <ulib.h> #include <ulib.h>
#include "ls.h" #include "ls.h"
#include "kill.h"
void main(void) { void main(void) {
if (argslen() == 0) { if (argslen() == 0) {
@ -12,6 +13,8 @@ void main(void) {
if (string_strcmp(cmd, "ls") == 0) { if (string_strcmp(cmd, "ls") == 0) {
pctl_ls(); pctl_ls();
} else if (string_strcmp(cmd, "kill") == 0) {
pctl_kill();
} else { } else {
uprintf("pctl: unknown command %s\n", cmd); uprintf("pctl: unknown command %s\n", cmd);
} }

View File

@ -5,7 +5,7 @@
#include "runtime.h" #include "runtime.h"
#include "macros.h" #include "macros.h"
extern uint64_t PID; extern PID_t PID;
static InterpResult RES; static InterpResult RES;

View File

@ -6,7 +6,7 @@
#define LINEBUF_MAX 1024 #define LINEBUF_MAX 1024
uint64_t PID; PID_t PID;
struct { struct {
char *modestr; char *modestr;

View File

@ -4,6 +4,8 @@
#include "runtime.h" #include "runtime.h"
#include "interp.h" #include "interp.h"
extern PID_t PID;
RtCmd *RTCMDS = NULL; RtCmd *RTCMDS = NULL;
RtAlias *RTALIASES = NULL; RtAlias *RTALIASES = NULL;
@ -45,7 +47,13 @@ bool rt_mkalias(Token *tks) {
return true; return true;
} }
bool rt_PID(Token *tks) {
uprintf("%lu\n", PID);
return true;
}
void rt_init(void) { void rt_init(void) {
RTCMD("%print", &rt_print); RTCMD("%print", &rt_print);
RTCMD("%mkalias", &rt_mkalias); RTCMD("%mkalias", &rt_mkalias);
RTCMD("%PID", &rt_PID);
} }