From 39c778d2f02248e3a9f8a280155cdea993253a6b Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Thu, 4 May 2023 02:02:17 -0400 Subject: [PATCH] add some asserts --- Makefile | 3 ++- dev/pci.c | 10 ++++++---- include/assert.h | 17 +++++++++++++++++ include/kernel/common.h | 3 +++ init/main.c | 4 ++-- lib/stdio.c | 1 + mem/buddy.c | 4 ++++ 7 files changed, 35 insertions(+), 7 deletions(-) create mode 100644 include/assert.h diff --git a/Makefile b/Makefile index a20392e..540eafa 100644 --- a/Makefile +++ b/Makefile @@ -16,7 +16,7 @@ define DEFAULT_VAR = endef # User controllable CFLAGS. -CFLAGS ?= -g -O2 -pipe -Wall -Wextra +CFLAGS ?= -g -O0 -pipe -Wall -Wextra # User controllable linker flags. We set none by default. LDFLAGS ?= @@ -30,6 +30,7 @@ override CFLAGS += \ -fno-lto \ -fno-pie \ -fno-pic \ + -DKDEBUG \ -m64 \ -march=x86-64 \ -mabi=sysv \ diff --git a/dev/pci.c b/dev/pci.c index dcab609..9f3c2b5 100644 --- a/dev/pci.c +++ b/dev/pci.c @@ -10,7 +10,7 @@ uint16_t pci_config_read_word(uint8_t bus, uint8_t slot, uint8_t func, uint8_t o uint32_t lfunc = func; uint16_t tmp = 0; - addr = (lbus << 16) | (lslot << 11) | (lfunc << 8) | (offset & 0xfc) | ((uint32_t)0x80000000); + addr = (lbus << 16) | (lslot << 11) | (lfunc << 8) | (offset & 0xfc) | 0x80000000; outl(PCIPM_CONFIG_ADDRESS, addr); tmp = (uint16_t)((inl(PCIPM_CONFIG_DATA) >> ((offset & 2) << 3)) & 0xFFFF); @@ -26,10 +26,12 @@ static void pci_print_bus(uint8_t bus) continue; uint8_t class = pci_config_read_word(bus, slot, 0, 0xA); - uint8_t subclass = (pci_config_read_word(bus, slot, 0, 0xA) >> 8); - uint8_t prog_if = (pci_config_read_word(bus, slot, 0, 0x8) >> 8); + if(class == 0xFF) + continue; - printf(LOG_INFO "PCI device: vendor:%xh class:%xh subclass:%xh prof_if:%xh\n", vendor_id, class, subclass, prog_if); + uint8_t subclass = (pci_config_read_word(bus, slot, 0, 0xA) >> 8); + + printf(LOG_INFO "PCI device [bus:%xh slot:%xh]: class=%xh sub=%xh\n", bus, slot, class, subclass); } } diff --git a/include/assert.h b/include/assert.h new file mode 100644 index 0000000..2c49fb2 --- /dev/null +++ b/include/assert.h @@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _ASSERT_H_ +#define _ASSERT_H_ + +#ifdef KDEBUG +#define assert(expr) \ + do { \ + if (!(expr)) { \ + printf(LOG_ERROR "assertion failed: %s, file %s, line %d\n", #expr, __FILE__, __LINE__); \ + yield(); \ + } \ + } while (0) +#else +#define assert(expr) ((void)0) +#endif + +#endif /* _ASSERT_H_ */ diff --git a/include/kernel/common.h b/include/kernel/common.h index ec613e1..2506e88 100644 --- a/include/kernel/common.h +++ b/include/kernel/common.h @@ -10,6 +10,7 @@ #include #include #include +#include #include @@ -20,6 +21,8 @@ #define MB (1024 * KB) #define GB (1024 * MB) +void yield(); + typedef uintptr_t paddr_t; #endif /* _COMMON_H_ */ diff --git a/init/main.c b/init/main.c index 79b6d14..b0428fb 100644 --- a/init/main.c +++ b/init/main.c @@ -65,6 +65,8 @@ void kmain(void) console_init(); acpi_init(); + pci_init(); + _pic_init(); void *kstack = buddy_alloc(KSTACK_SIZE); @@ -74,8 +76,6 @@ void kmain(void) void kmain_post_stack_init() { - pci_init(); - printf("\n\n# "); yield(); } diff --git a/lib/stdio.c b/lib/stdio.c index 4637607..564396f 100644 --- a/lib/stdio.c +++ b/lib/stdio.c @@ -102,6 +102,7 @@ printf_reverse: printf_out(buf[(n - 1) - j]); ret++; } + break; } } else { printf_out(c); diff --git a/mem/buddy.c b/mem/buddy.c index 8637c6e..7b2c640 100644 --- a/mem/buddy.c +++ b/mem/buddy.c @@ -219,6 +219,9 @@ void buddy_free(void *ptr) return; } } + + /* should never happen */ + assert(0); } static void *kmap_alloc_page() @@ -262,6 +265,7 @@ void kmap(paddr_t paddr, uint64_t vaddr, size_t len, uint64_t attr) if (pml4 == NULL) { pml4 = alloc_page(); + assert(pml4 != NULL); memset(pml4, 0, 0x1000); }