debug macros in std

This commit is contained in:
uosfz 2025-02-20 19:48:15 +01:00
parent df294bc125
commit dbcd40f3d7
Signed by: uosfz
SSH key fingerprint: SHA256:FlktuluyhTQg3jHZNLKwxOOC5hbfrUXM0tz3IA3lGJo
4 changed files with 41 additions and 26 deletions

View file

@ -31,4 +31,17 @@ int checkStringLength(const char *string);
// Formatted output // Formatted output
void myprintf(const char *format, ...); 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 #endif // KARLOS_UTILITY_H

View file

@ -82,7 +82,7 @@ void _start() {
uint16_t bdf; uint16_t bdf;
if (pci_search(0x1AF4, 0x1001, &bdf) && pci_search(0x1AF4, 0x1042, &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); myprintf("found virtio_blk device at bdf %u .", bdf);
// read BARs // read BARs
@ -92,26 +92,26 @@ void _start() {
} }
/* /*
// check initrd // check initrd
println(""); println("");
uint64_t printed_in_line = 0; uint64_t printed_in_line = 0;
for (uint64_t off = 0; off < bootboot.initrd_size; off++) { for (uint64_t off = 0; off < bootboot.initrd_size; off++) {
uint8_t byte = ((uint8_t*)bootboot.initrd_ptr)[off]; uint8_t byte = ((uint8_t*)bootboot.initrd_ptr)[off];
putc(TO_HEX((byte >> 4) & 0x0f)); putc(TO_HEX((byte >> 4) & 0x0f));
putc(TO_HEX(byte & 0x0f)); putc(TO_HEX(byte & 0x0f));
putc(' '); putc(' ');
if (++printed_in_line % 32 == 0) { if (++printed_in_line % 32 == 0) {
printed_in_line = 0; printed_in_line = 0;
println(""); println("");
} }
} }
println(""); println("");
*/ */
// hang for now // hang for now
panic("end of kernel"); PANIC("end of kernel");
} }
/************************** /**************************

View file

@ -8,9 +8,7 @@
#define PCI_CONFIG_DATA_PORT 0xCFC #define PCI_CONFIG_DATA_PORT 0xCFC
uint16_t pci_bdf_make(uint8_t bus, uint8_t dev, uint8_t fun) { uint16_t pci_bdf_make(uint8_t bus, uint8_t dev, uint8_t fun) {
if (dev >= 32 || fun >= 8) { ASSERT(dev < 32 && fun < 8);
panic("pci_bdf_make");
}
return ((uint16_t)bus << 8) | ((uint16_t)dev << 3) | (uint16_t)fun; 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) { struct pci_bar_desc pci_bar_desc_read(uint16_t bdf, uint8_t barnum) {
if (barnum >= 6) { ASSERT(barnum < 6);
panic("pci_config_read_bar");
}
// get basic information // get basic information
// https://en.wikipedia.org/wiki/PCI_configuration_space // 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; desc.type = bar & 0x1;
if (desc.type == PCI_BAR_TYPE_MEM) { if (desc.type == PCI_BAR_TYPE_MEM) {
desc.locatable = (bar >> 1) & 0x3; desc.locatable = (bar >> 1) & 0x3;
if (desc.locatable == 3) { ASSERT(desc.locatable != 3);
panic("pci_config_read_bar");
}
desc.prefetchable = (bar >> 3) & 0x1; desc.prefetchable = (bar >> 3) & 0x1;
desc.address = bar & 0xfffffff0; desc.address = bar & 0xfffffff0;
} else { } else {

View file

@ -527,3 +527,11 @@ void myprintf(const char *format, ...) {
} }
va_end(args); 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
}
}