From b97967ab660243b723f246db03c2e4a82e125f3f Mon Sep 17 00:00:00 2001 From: Kimplul Date: Sun, 7 Jul 2024 07:02:46 +0300 Subject: simplify assertions + No real point having multiple different levels of assertions, just say you assert something and be done with it --- src/dmem.c | 4 ++-- src/elf.c | 2 +- src/initrd.c | 4 ++-- src/irq.c | 2 +- src/orphanage.c | 4 ++-- src/pmem.c | 8 ++++++-- src/proc.c | 2 ++ src/regions.c | 4 ++-- src/tcb.c | 20 ++++++++++---------- src/vmem.c | 10 +++++----- 10 files changed, 33 insertions(+), 27 deletions(-) (limited to 'src') diff --git a/src/dmem.c b/src/dmem.c index b02b71b..91ddb02 100644 --- a/src/dmem.c +++ b/src/dmem.c @@ -48,7 +48,7 @@ static struct mem_region_root *__select_region(pm_t addr) vm_t alloc_devmem(struct tcb *t, pm_t start, size_t bytes, vmflags_t flags) { - hard_assert(t && is_proc(t), ERR_INVAL); + assert(t && is_proc(t)); struct mem_region_root *region = __select_region(start); if (!region) @@ -69,7 +69,7 @@ vm_t alloc_devmem(struct tcb *t, pm_t start, size_t bytes, vmflags_t flags) stat_t free_devmem(struct tcb *t, vm_t start) { - hard_assert(t && is_proc(t), ERR_INVAL); + assert(t && is_proc(t)); pm_t addr = 0; stat_vpage(t->proc.vmem, start, &addr, NULL, NULL); diff --git a/src/elf.c b/src/elf.c index 9add641..5c2f9bd 100644 --- a/src/elf.c +++ b/src/elf.c @@ -46,7 +46,7 @@ static uint8_t __elf_to_uvflags(uint8_t elf_flags) static void __map_exec(struct tcb *t, vm_t bin, uint8_t ei_c, vm_t phstart, size_t phnum, size_t phsize) { - hard_assert(t && is_proc(t), RETURN_VOID); + assert(t && is_proc(t)); /** \todo take alignment into consideration? */ /** \todo take overlapping memory regions into account, probably mostly diff --git a/src/initrd.c b/src/initrd.c index 242e373..489d6f5 100644 --- a/src/initrd.c +++ b/src/initrd.c @@ -120,7 +120,7 @@ pm_t get_initrdtop(const void *fdt) void *initrd_end_ptr = (void *)fdt_getprop(fdt, chosen_offset, "linux,initrd-end", &len); - catastrophic_assert(initrd_end_ptr); + assert(initrd_end_ptr); return (pm_t)fdt_load_int_ptr(len / 4, initrd_end_ptr); } @@ -132,7 +132,7 @@ pm_t get_initrdbase(const void *fdt) void *initrd_base_ptr = (void *)fdt_getprop(fdt, chosen_offset, "linux,initrd-start", &len); - catastrophic_assert(initrd_base_ptr); + assert(initrd_base_ptr); return (pm_t)fdt_load_int_ptr(len / 4, initrd_base_ptr); } diff --git a/src/irq.c b/src/irq.c index 73ae88d..bc9e2de 100644 --- a/src/irq.c +++ b/src/irq.c @@ -55,7 +55,7 @@ stat_t unregister_irq(struct tcb *t, irq_t id) void handle_irq() { irq_t id = get_irq(); - hard_assert(id < max_irq, RETURN_VOID); + assert(id < max_irq); id_t tid = irq_map[id]; diff --git a/src/orphanage.c b/src/orphanage.c index 9da6f8b..e613e8b 100644 --- a/src/orphanage.c +++ b/src/orphanage.c @@ -24,7 +24,7 @@ void orphanize(struct tcb *t) void unorphanize(struct tcb *t) { - catastrophic_assert(!is_rpc(t)); + assert(!is_rpc(t)); /* attach to init process */ struct tcb *init = get_tcb(1); @@ -43,7 +43,7 @@ void unorphanize(struct tcb *t) clear_bits(t->state, TCB_ORPHAN); - catastrophic_assert(init->callback); + assert(init->callback); set_args3(t, 0, SYS_USER_ORPHANED, t->tid); set_return(t, init->callback); t->callback = init->callback; diff --git a/src/pmem.c b/src/pmem.c index 5516d30..bed10a4 100644 --- a/src/pmem.c +++ b/src/pmem.c @@ -564,7 +564,7 @@ static size_t build_reserved_map(size_t exists, struct avoid_region avoid[64], pm_t size = (pm_t)fdt_load_reg_size(ci, rmem_reg, 0); avoid[exists++] = (struct avoid_region){(pm_t)__va(base), size}; - catastrophic_assert(exists < 64); + assert(exists < 64); } return exists; @@ -636,10 +636,14 @@ void init_pmem(void *fdt, uintptr_t load_addr) info("found initrd at [%lx - %lx]\n", initrd_base, initrd_base + initrd_size); + assert(is_aligned(initrd_base, BASE_PAGE_SIZE)); + pm_t fdt_base = (pm_t)fdt; pm_t fdt_size = fdt_totalsize(fdt); info("found fdt at [%lx - %lx]\n", fdt_base, fdt_base + fdt_size); + assert(is_aligned(fdt_base, BASE_PAGE_SIZE)); + /* find probably most suitable contiguous region of ram for our physical * ram map */ @@ -661,7 +665,7 @@ void init_pmem(void *fdt, uintptr_t load_addr) pm_t pmap_base = select_base(ram_base, ram_size, probe_size, avoid_count, avoid); - catastrophic_assert(pmap_base); + assert(pmap_base); info("choosing to place pmem map at %lx\n", pmap_base); size_t actual_size = populate_pmap(ram_base, ram_size, pmap_base); diff --git a/src/proc.c b/src/proc.c index 93e3aa5..3d2d5bd 100644 --- a/src/proc.c +++ b/src/proc.c @@ -18,6 +18,8 @@ #include +static vm_t entry; + stat_t prepare_proc(struct tcb *t, vm_t bin, vm_t interp) { vm_t entry = load_elf(t, bin, interp); diff --git a/src/regions.c b/src/regions.c index f098f4f..60cab23 100644 --- a/src/regions.c +++ b/src/regions.c @@ -664,7 +664,7 @@ stat_t clone_region(struct vmem *b, struct vmem *g, vm_t from, vm_t to, align_region(from, bytes, &from, &from_size); align_region(to, bytes, &to, &to_size); - catastrophic_assert(from_size == to_size); + assert(from_size == to_size); bytes = from_size; while (bytes) { @@ -694,7 +694,7 @@ stat_t copy_region(struct vmem *b, struct vmem *g, vm_t from, vm_t to, align_region(from, bytes, &from, &from_size); align_region(to, bytes, &to, &to_size); - catastrophic_assert(from_size == to_size); + assert(from_size == to_size); bytes = from_size; while (bytes) { diff --git a/src/tcb.c b/src/tcb.c index 6915676..ea00d3d 100644 --- a/src/tcb.c +++ b/src/tcb.c @@ -47,7 +47,7 @@ void init_tcbs() * something smaller but this is fine for now. */ tcbs = (struct tcb **)alloc_page(MM_O1); num_tids = order_size(MM_O1) / sizeof(struct tcb *); - catastrophic_assert(is_powerof2(num_tids)); + assert(is_powerof2(num_tids)); memset(tcbs, 0, order_size(MM_O1)); } @@ -186,7 +186,7 @@ static stat_t __copy_proc(struct tcb *p, struct tcb *n) struct tcb *create_proc(struct tcb *p) { - hard_assert(tcbs, 0); + assert(tcbs); /* create a new thread outside the current process */ struct tcb *n = create_thread(NULL); @@ -207,7 +207,7 @@ struct tcb *create_proc(struct tcb *p) */ static stat_t __destroy_thread_data(struct tcb *t) { - catastrophic_assert(t->refcount == 0); + assert(t->refcount == 0); /* remove ourselves from the thread pool */ /** @todo this should be at the top of the function, and be wrapped in @@ -231,8 +231,8 @@ static stat_t __destroy_thread_data(struct tcb *t) stat_t destroy_thread(struct tcb *t) { - hard_assert(tcbs, ERR_NOINIT); - hard_assert(!is_proc(t), ERR_INVAL); + assert(tcbs); + assert(!is_proc(t)); /* mark us as zombies */ set_bits(t->state, TCB_ZOMBIE); @@ -265,8 +265,8 @@ stat_t destroy_thread(struct tcb *t) stat_t destroy_proc(struct tcb *p) { - hard_assert(tcbs, ERR_NOINIT); - hard_assert(is_proc(p), ERR_INVAL); + assert(tcbs); + assert(is_proc(p)); /** @todo currently we don't care who else is in the address space when * we start freeing stuff, one fairly simple way to deal with this is to @@ -291,7 +291,7 @@ void reference_proc(struct tcb *p) if (!p) return; - hard_assert(is_proc(p), RETURN_VOID); + assert(is_proc(p)); p->refcount++; } @@ -300,7 +300,7 @@ void unreference_proc(struct tcb *p) if (!p) return; - hard_assert(is_proc(p), RETURN_VOID); + assert(is_proc(p)); p->refcount--; if (zombie(p) && p->refcount == 0) { dbg("thread %ld is completely destroyed\n", (long)p->tid); @@ -342,7 +342,7 @@ void use_tcb(struct tcb *t) struct tcb *get_tcb(id_t tid) { - hard_assert(tcbs, 0); + assert(tcbs); if (tid <= 0) return NULL; diff --git a/src/vmem.c b/src/vmem.c index 2b9ab59..27e1b2f 100644 --- a/src/vmem.c +++ b/src/vmem.c @@ -40,7 +40,7 @@ static stat_t __copy_mapped_region(struct tcb *d, struct tcb *s, size_t size = end - start; vm_t v = alloc_fixed_region(&d->uvmem.region, start, size, &size, m->flags); - catastrophic_assert(v == start); + assert(v == start); /* note that we use uvmem.vmem instead of proc.vmem, this is just to * make sure that zombies don't eat our brains */ @@ -77,7 +77,7 @@ static stat_t __copy_shared_region(struct tcb *d, struct mem_region *m) vm_t v = alloc_fixed_region(&d->uvmem.region, start, size, &size, m->flags); - catastrophic_assert(v == start); + assert(v == start); stat_t res = clone_region(d->uvmem.vmem, s->uvmem.vmem, start, v, size, m->flags); if (res == OK) @@ -240,7 +240,7 @@ stat_t copy_uvmem(struct tcb *d, struct tcb *s) vm_t alloc_uvmem(struct tcb *t, size_t size, vmflags_t flags) { /* t exists and is the process tcb of the current process */ - hard_assert(t && is_proc(t), ERR_INVAL); + assert(t && is_proc(t)); const vm_t v = alloc_region(&t->uvmem.region, size, &size, flags); if (map_region(t->proc.vmem, v, size, max_order(), flags)) { @@ -254,7 +254,7 @@ vm_t alloc_uvmem(struct tcb *t, size_t size, vmflags_t flags) vm_t alloc_fixed_uvmem(struct tcb *t, vm_t start, size_t size, vmflags_t flags) { - hard_assert(t && is_proc(t), ERR_INVAL); + assert(t && is_proc(t)); const vm_t v = alloc_fixed_region(&t->uvmem.region, start, size, &size, flags); @@ -282,7 +282,7 @@ vm_t map_fixed_uvmem(struct tcb *t, pm_t start, size_t size, vmflags_t flags) /* free_shared_uvmem shouldn't be needed, likely to work with free_uvmem */ vm_t alloc_shared_uvmem(struct tcb *s, size_t size, vmflags_t flags) { - hard_assert(s && is_proc(s), ERR_INVAL); + assert(s && is_proc(s)); const vm_t v = alloc_region(&s->uvmem.region, size, &size, MR_SHARED | flags); /* use base pages to make clone more likely to succeed */ -- cgit v1.3