diff --git a/include/std.h b/include/std.h index 80cef42..dd53044 100644 --- a/include/std.h +++ b/include/std.h @@ -31,4 +31,17 @@ int checkStringLength(const char *string); // Formatted output void myprintf(const char *format, ...); +// Debug helpers +#define PANIC(msg) panic(__FILE__, __LINE__, msg) +#define ASSERT(cond) do {\ + if (!(cond)) {\ + PANIC("assertion failed: " #cond);\ + }\ +} while (0) +#define TODO() PANIC("not implemented") +#define UNREACHABLE() PANIC("unreachable") +// don't use this directly, use the macros above +__attribute__ ((noreturn)) +void panic(const char *file, unsigned int line, const char *msg); + #endif // KARLOS_UTILITY_H diff --git a/src/kernel.c b/src/kernel.c index e532580..ee8fb68 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -82,7 +82,7 @@ void _start() { uint16_t bdf; if (pci_search(0x1AF4, 0x1001, &bdf) && pci_search(0x1AF4, 0x1042, &bdf)) { - panic("couldn't find virtio_blk device!"); + PANIC("couldn't find virtio_blk device!"); } myprintf("found virtio_blk device at bdf %u .", bdf); // read BARs @@ -92,26 +92,26 @@ void _start() { } /* - // check initrd - println(""); - uint64_t printed_in_line = 0; - for (uint64_t off = 0; off < bootboot.initrd_size; off++) { - uint8_t byte = ((uint8_t*)bootboot.initrd_ptr)[off]; + // check initrd + println(""); + uint64_t printed_in_line = 0; + for (uint64_t off = 0; off < bootboot.initrd_size; off++) { + uint8_t byte = ((uint8_t*)bootboot.initrd_ptr)[off]; - putc(TO_HEX((byte >> 4) & 0x0f)); - putc(TO_HEX(byte & 0x0f)); - putc(' '); + putc(TO_HEX((byte >> 4) & 0x0f)); + putc(TO_HEX(byte & 0x0f)); + putc(' '); - if (++printed_in_line % 32 == 0) { - printed_in_line = 0; - println(""); - } - } - println(""); - */ + if (++printed_in_line % 32 == 0) { + printed_in_line = 0; + println(""); + } + } + println(""); + */ // hang for now - panic("end of kernel"); + PANIC("end of kernel"); } /************************** diff --git a/src/pci.c b/src/pci.c index dc9a4b6..fc038ad 100644 --- a/src/pci.c +++ b/src/pci.c @@ -8,9 +8,7 @@ #define PCI_CONFIG_DATA_PORT 0xCFC uint16_t pci_bdf_make(uint8_t bus, uint8_t dev, uint8_t fun) { - if (dev >= 32 || fun >= 8) { - panic("pci_bdf_make"); - } + ASSERT(dev < 32 && fun < 8); return ((uint16_t)bus << 8) | ((uint16_t)dev << 3) | (uint16_t)fun; } @@ -34,9 +32,7 @@ void pci_config_write_u32(uint16_t bdf, uint8_t offset, uint32_t value) { } struct pci_bar_desc pci_bar_desc_read(uint16_t bdf, uint8_t barnum) { - if (barnum >= 6) { - panic("pci_config_read_bar"); - } + ASSERT(barnum < 6); // get basic information // https://en.wikipedia.org/wiki/PCI_configuration_space @@ -46,9 +42,7 @@ struct pci_bar_desc pci_bar_desc_read(uint16_t bdf, uint8_t barnum) { desc.type = bar & 0x1; if (desc.type == PCI_BAR_TYPE_MEM) { desc.locatable = (bar >> 1) & 0x3; - if (desc.locatable == 3) { - panic("pci_config_read_bar"); - } + ASSERT(desc.locatable != 3); desc.prefetchable = (bar >> 3) & 0x1; desc.address = bar & 0xfffffff0; } else { diff --git a/src/std.c b/src/std.c index 1cc11f5..6571fb0 100644 --- a/src/std.c +++ b/src/std.c @@ -527,3 +527,11 @@ void myprintf(const char *format, ...) { } va_end(args); } + +/* --- Debug helpers --- */ +__attribute__((noreturn)) void panic(const char *file, unsigned int line, const char *msg) { + myprintf("PANIC@%s:%d: %s\n", file, line, msg); + while (1) { + // loop forever + } +}