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
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

View file

@ -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
@ -111,7 +111,7 @@ void _start() {
*/
// hang for now
panic("end of kernel");
PANIC("end of kernel");
}
/**************************

View file

@ -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 {

View file

@ -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
}
}