From 475f77d30f2f4a7f4d3d32fe5ce843e5231cda50 Mon Sep 17 00:00:00 2001 From: kamkow1 Date: Sun, 21 Sep 2025 18:32:22 +0200 Subject: [PATCH] Rewrite LL_* (link list) macros --- kernel/dlmalloc/dlmalloc_port.inc | 2 +- kernel/hal/x86_64/intr.c | 2 +- kernel/proc/proc.c | 94 ++++++++++++++----------------- kernel/syscall/mman.c | 9 ++- kernel/syscall/processctl.c | 37 +++++------- kernel/util/util.h | 62 ++++++++++++++------ 6 files changed, 106 insertions(+), 100 deletions(-) diff --git a/kernel/dlmalloc/dlmalloc_port.inc b/kernel/dlmalloc/dlmalloc_port.inc index fe53949..2e4613a 100644 --- a/kernel/dlmalloc/dlmalloc_port.inc +++ b/kernel/dlmalloc/dlmalloc_port.inc @@ -68,6 +68,6 @@ void *sbrk(long inc) { uint64_t blocks = _DIV_ROUNDUP(inc, BITMAP_BLOCK_SIZE); uint8_t *virt = VIRT(pmm_alloc(blocks)); hal_memset(virt, 0, blocks * BITMAP_BLOCK_SIZE); - _last = (void *)(virt + (blocks * BITMAP_BLOCK_SIZE)); + _last = (void *)(virt + inc); return virt; } diff --git a/kernel/hal/x86_64/intr.c b/kernel/hal/x86_64/intr.c index a74acb5..640b541 100644 --- a/kernel/hal/x86_64/intr.c +++ b/kernel/hal/x86_64/intr.c @@ -200,7 +200,7 @@ void intr_handleintr(IntrStackFrame *frame) { kprintf("ERROR %s, 0x%lX\n", exceptions[frame->trapnum], frame->errnum); intr_dumpframe(frame); backtrace((BackTraceFrame *)frame->regs.rbp); - if (frame->errnum & 0x4) { + if (hal_vmm_current_cr3() != KERNEL_CR3) { kprintf("killed pid %ld %s\n", PROCS.current->pid, PROCS.current->name); proc_killself(); proc_sched((void *)frame); diff --git a/kernel/proc/proc.c b/kernel/proc/proc.c index 1385da8..44ecc46 100644 --- a/kernel/proc/proc.c +++ b/kernel/proc/proc.c @@ -211,58 +211,50 @@ Proc *proc_nextready(void) { void proc_reaper(void) { spinlock_acquire(&PROCS.spinlock); - Proc *head = PROCS.procs; - while (head) { - if (head->state == PROC_ZOMBIE) { - Proc *zombie = head; - head = head->next; - - LL_REMOVE(PROCS.procs, zombie); - - for (size_t i = 0; i < zombie->vobjcnt; i++) { - if (zombie->vobjs[i] != NULL) { - vfs_close(zombie->vobjs[i]); - zombie->vobjs[i] = NULL; - } - } - - for (size_t i = 0; i < PROC_PIPEHANDLES_MAX; i++) { - if (zombie->pipes[i] != NULL && zombie->pipes[i]->ownerpid == zombie->pid) { - dlfree(zombie->pipes[i]); - ipc_pipefree(zombie->pipes[i]); - zombie->pipes[i] = NULL; - } - } - - pmm_free((uintptr_t)(zombie->platformdata.kstack - PROC_STACKSIZE), PROC_STACKBLOCKS); - pmm_free((uintptr_t)(zombie->platformdata.pstack - PROC_STACKSIZE), PROC_STACKBLOCKS); - - if (!zombie->kern) { - VasRange *vashead = zombie->vas; - size_t i = 0; - while (vashead) { - VasRange *tmp = vashead; - vashead = vashead->next; - hal_vmm_unmap_range(zombie->platformdata.cr3, tmp->virtstart, tmp->physstart, tmp->size); - pmm_free((uintptr_t)tmp->physstart, tmp->size / HAL_PAGE_SIZE); - dlfree(tmp); - i++; - } - - 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 { - head = head->next; + Proc *head, *tmp; + LL_FOREACH_SAFE(PROCS.procs, head, tmp) { + if (head->state != PROC_ZOMBIE) { + continue; } + + Proc *zombie = head; + LL_REMOVE(PROCS.procs, zombie); + + for (size_t i = 0; i < zombie->vobjcnt; i++) { + if (zombie->vobjs[i] != NULL) { + vfs_close(zombie->vobjs[i]); + zombie->vobjs[i] = NULL; + } + } + + for (size_t i = 0; i < PROC_PIPEHANDLES_MAX; i++) { + if (zombie->pipes[i] != NULL && zombie->pipes[i]->ownerpid == zombie->pid) { + dlfree(zombie->pipes[i]); + ipc_pipefree(zombie->pipes[i]); + zombie->pipes[i] = NULL; + } + } + + pmm_free((uintptr_t)(zombie->platformdata.kstack - PROC_STACKSIZE), PROC_STACKBLOCKS); + pmm_free((uintptr_t)(zombie->platformdata.pstack - PROC_STACKSIZE), PROC_STACKBLOCKS); + + if (!zombie->kern) { + VasRange *vas, *vastmp; + LL_FOREACH_SAFE(zombie->vas, vas, vastmp) { + hal_vmm_unmap_range(zombie->platformdata.cr3, vas->virtstart, vas->physstart, vas->size); + pmm_free((uintptr_t)vas->physstart, vas->size / HAL_PAGE_SIZE); + dlfree(vas); + } + + pmm_free((uintptr_t)zombie->platformdata.cr3, 1); + + ProcArg *arg, *argtmp; + LL_FOREACH_SAFE(zombie->procargs.list, arg, argtmp) { + dlfree(arg->string); + dlfree(arg); + } + } + dlfree(zombie); } spinlock_release(&PROCS.spinlock); } diff --git a/kernel/syscall/mman.c b/kernel/syscall/mman.c index 4cc580c..4f5ad28 100644 --- a/kernel/syscall/mman.c +++ b/kernel/syscall/mman.c @@ -86,13 +86,12 @@ int32_t SYSCALL1(sys_mman_unmap, addr1) { uint8_t *virt = NULL; VasRange *tofree = NULL; - VasRange *range = proc->vas; - while (range) { - if (range->virtstart == addr) { - tofree = range; + VasRange *vas, *vastmp; + LL_FOREACH_SAFE(proc->vas, vas, vastmp) { + if (vas->virtstart == addr) { + tofree = vas; break; } - range = range->next; } if (tofree == NULL) { diff --git a/kernel/syscall/processctl.c b/kernel/syscall/processctl.c index 112af20..9d1e827 100644 --- a/kernel/syscall/processctl.c +++ b/kernel/syscall/processctl.c @@ -97,30 +97,23 @@ int32_t SYSCALL5(sys_processctl, pid1, cmd1, arg1, arg2, arg3) { goto done; } size_t len = arg2; - ProcArg *arg = proc->procargs.list; - size_t i = 0; - while (arg) { - if (i == len) { - break; - } + size_t i; + ProcArg *arg, *argtmp; + LL_FOREACH_SAFE_IDX_LIMIT(proc->procargs.list, arg, argtmp, i, len) { if (argbuf[i] == NULL) { ret = E_INVALIDARGUMENT; goto done; } hal_strcpy(argbuf[i], arg->string); - arg = arg->next; - i++; } + ret = E_OK; } break; case PCTL_PLS_SZ: { - size_t i = 0; + Proc *p, *ptmp; + size_t i; spinlock_acquire(&PROCS.spinlock); - Proc *p = PROCS.procs; - while (p) { - i++; - p = p->next; - } + LL_FOREACH_SAFE_IDX(PROCS.procs, p, ptmp, i); spinlock_release(&PROCS.spinlock); ret = i; } break; @@ -133,26 +126,22 @@ int32_t SYSCALL5(sys_processctl, pid1, cmd1, arg1, arg2, arg3) { goto done; } - size_t i = 0; + Proc *p, *ptmp; + size_t i; spinlock_acquire(&PROCS.spinlock); - Proc *p = PROCS.procs; - while (p) { + LL_FOREACH_SAFE_IDX(PROCS.procs, p, ptmp, i) { if (i == pidx) { stat->pid = p->pid; hal_strcpy(stat->name, p->name); stat->state = p->state; stat->kern = p->kern; - VasRange *range = p->vas; - while (range) { - stat->usemem += range->size; - range = range->next; + VasRange *vas, *vastmp; + LL_FOREACH_SAFE(p->vas, vas, vastmp) { + stat->usemem += vas->size; } break; } - - i++; - p = p->next; } spinlock_release(&PROCS.spinlock); ret = E_OK; diff --git a/kernel/util/util.h b/kernel/util/util.h index 30c1044..0623711 100644 --- a/kernel/util/util.h +++ b/kernel/util/util.h @@ -8,35 +8,45 @@ #define LL_APPEND(head, new) \ do { \ - if ((head) != NULL) { \ - typeof((head)) __tmp; \ - (new)->next = NULL; \ - __tmp = (head); \ - while (__tmp->next != NULL) { \ - __tmp = __tmp->next; \ + if ((new) != NULL) { \ + if ((head) != NULL) { \ + typeof((head)) __tmp; \ + (new)->next = NULL; \ + __tmp = (head); \ + while (__tmp->next != NULL) { \ + __tmp = __tmp->next; \ + } \ + __tmp->next = (new); \ + } else { \ + (new)->next = NULL; \ + (head) = (new); \ } \ - __tmp->next = (new); \ - } else { \ - (new)->next = NULL; \ - (head) = (new); \ } \ } while(0) #define LL_REMOVE(head, ele) \ do { \ - typeof((head)) __cur = (head); \ - typeof((head)) __prev = NULL; \ - while (__cur != (ele)) { \ - __prev = __cur; \ - __cur = __cur->next; \ - } \ - if (__prev != NULL) { \ - __prev->next = __cur->next; \ + if ((head) != NULL && (ele) != NULL) { \ + typeof((head)) __cur = (head); \ + typeof((head)) __prev = NULL; \ + while (__cur != NULL && __cur != (ele)) { \ + __prev = __cur; \ + __cur = __cur->next; \ + } \ + if (__cur == (ele)) { \ + if (__prev != NULL) { \ + __prev->next = __cur->next; \ + } else { \ + (head) = __cur->next; \ + } \ + (ele)->next = NULL; \ + } \ } \ } while(0) #define LL_FINDPROP(head, out, propname, propvalue) \ do { \ + (out) = NULL; \ typeof((head)) __tmp = (head); \ while (__tmp) { \ if (__tmp->propname == (propvalue)) { \ @@ -47,6 +57,22 @@ } \ } while(0) +#define LL_FOREACH_SAFE(head, var, tmp) \ + for (typeof(head) var = (head), tmp = (var ? var->next : NULL); \ + var != NULL; \ + var = tmp, tmp = (var ? var->next : NULL) \ + ) + +#define LL_FOREACH_SAFE_IDX(head, var, tmp, idx) \ + for (typeof(head) var = (head), tmp = (var ? var->next : NULL); \ + var != NULL && ((idx) = 0, 1); \ + var = tmp, tmp = (var ? var->next : NULL), (idx)++) + +#define LL_FOREACH_SAFE_IDX_LIMIT(head, var, tmp, idx, max) \ + for ((idx) = 0, var = (head), tmp = (var ? var->next : NULL); \ + var != NULL && (idx) < (max); \ + var = tmp, tmp = (var ? var->next : NULL), (idx)++) + char *util_get_filename(char *path); #endif // UTIL_UTIL_H_