diff --git a/arch/x86/extables.c b/arch/x86/extables.c index 69d4c61d..e1327dea 100644 --- a/arch/x86/extables.c +++ b/arch/x86/extables.c @@ -29,7 +29,7 @@ void init_extables(void) { for (extable_entry_t *cur = __start_extables; cur < __stop_extables; ++cur) { if (!cur->fixup && !cur->cb) - panic("extable entry #%d for addr 0x%lx lacks fixup and callback!\n", - cur - __start_extables, cur->fault_addr); + warning("extable entry #%d for addr 0x%lx lacks fixup and callback!", + cur - __start_extables, cur->fault_addr); } } diff --git a/arch/x86/traps.c b/arch/x86/traps.c index 30b54c54..a44d2609 100644 --- a/arch/x86/traps.c +++ b/arch/x86/traps.c @@ -315,7 +315,7 @@ void do_exception(cpu_regs_t *regs) { x86_ex_decode_error_code(ec_str, sizeof(ec_str), regs->exc.vector, regs->exc.error_code); snprintf(panic_str, sizeof(panic_str), - "#%s %sat IP: 0x%02x:0x%016lx SP: 0x%02x:0x%016lx\n", + "#%s %sat IP: 0x%02x:0x%016lx SP: 0x%02x:0x%016lx", exception_names[regs->exc.vector], ec_str, regs->exc.cs, regs->exc._ASM_IP, regs->exc.ss, regs->exc._ASM_SP); diff --git a/common/acpi.c b/common/acpi.c index d4f19de7..6f487655 100644 --- a/common/acpi.c +++ b/common/acpi.c @@ -87,7 +87,8 @@ static inline bool validate_rsdp(rsdp_rev1_t *ptr) { size = sizeof(rsdp_rev2_t); break; default: - panic("Unknown ACPI revision: %u\n", ptr->rev); + warning("Unknown ACPI revision: %u", ptr->rev); + return false; } if (get_checksum(ptr, size) != 0x0) @@ -141,7 +142,7 @@ static unsigned acpi_table_map_pages(paddr_t pa, size_t len) { for (unsigned i = 0; i < num_pages; i++, mfn++) { if (mfn_invalid(mfn)) { - panic("ACPI table at %p of length %lx has invalid MFN: %lx\n", _ptr(pa), len, + panic("ACPI table at %p of length %lx has invalid MFN: %lx", _ptr(pa), len, mfn); } @@ -366,7 +367,8 @@ static int process_madt_entries(void) { break; } default: - panic("Unknown ACPI MADT entry type: %u\n", entry->type); + warning("Unknown ACPI MADT entry type: %u", entry->type); + break; } } @@ -681,7 +683,7 @@ static void madt_parser(ACPI_SUBTABLE_HEADER *entry, void *arg) { break; } default: - panic("ACPI [MADT]: Unsupported subtable entry type: %x\n", entry->Type); + warning("ACPI [MADT]: Unsupported subtable entry type: %x", entry->Type); break; } } diff --git a/common/cmdline.c b/common/cmdline.c index 9732ad96..f4e02d8c 100644 --- a/common/cmdline.c +++ b/common/cmdline.c @@ -130,7 +130,7 @@ void __text_init cmdline_parse(const char *cmdline) { !!parse_bool(!strcmp(optval, optkey) ? "1" : optval); break; default: - panic("Unkown cmdline type detected..."); + warning("Unkown cmdline type \"%u\" for %s", param->type, param->name); break; } } diff --git a/common/console.c b/common/console.c index 05aa898b..48da57db 100644 --- a/common/console.c +++ b/common/console.c @@ -52,7 +52,7 @@ void vprintk(const char *fmt, va_list args) { rc = vsnprintf(buf, sizeof(buf), fmt, args); if (rc > (int) sizeof(buf)) - panic("vprintk() buffer overflow\n"); + panic("vprintk() buffer overflow"); for (i = 0; i < num_console_callbacks; i++) { void *arg = console_callbacks[i].arg; @@ -125,15 +125,28 @@ void register_console_callback(console_callback_t cb, void *arg) { void __noreturn panic(const char *fmt, ...) { va_list args; - printk("******************************\n"); - printk("CPU[%u] PANIC: ", smp_processor_id()); + printk("************** PANIC **************\n"); + printk("CPU[%u]: ", smp_processor_id()); va_start(args, fmt); vprintk(fmt, args); va_end(args); - printk("******************************\n"); + printk("\n***********************************\n"); while (1) halt(); } + +void warning(const char *fmt, ...) { + va_list args; + + printk("************* WARNING *************\n"); + printk("CPU[%u]: ", smp_processor_id()); + + va_start(args, fmt); + vprintk(fmt, args); + va_end(args); + + printk("\n***********************************\n"); +} diff --git a/common/sched.c b/common/sched.c index 9bcc5b1e..66060bf2 100644 --- a/common/sched.c +++ b/common/sched.c @@ -112,7 +112,7 @@ static int prepare_task(task_t *task, const char *name, task_func_t func, void * if (!task) return -EINVAL; - BUG_ON(get_task_state(task) > TASK_STATE_READY); + ASSERT(get_task_state(task) <= TASK_STATE_READY); task->name = name; task->func = func; @@ -179,7 +179,7 @@ int schedule_task(task_t *task, cpu_t *cpu) { return -EEXIST; } - BUG_ON(get_task_state(task) != TASK_STATE_READY); + ASSERT(get_task_state(task) == TASK_STATE_READY); printk("CPU[%u]: Scheduling task %s[%u] (%s)\n", cpu->id, task->name, task->id, task_repeat_string(task->repeat)); diff --git a/drivers/acpi/acpica/osl.c b/drivers/acpi/acpica/osl.c index b80bbdd5..a959c06f 100644 --- a/drivers/acpi/acpica/osl.c +++ b/drivers/acpi/acpica/osl.c @@ -85,7 +85,8 @@ ACPI_STATUS AcpiOsSignal(UINT32 Function, void *Info) { printk("ACPI: Received ACPI_SIGNAL_BREAKPOINT: %s", bp_msg ?: ""); } break; default: - BUG(); + warning("ACPI: Unsupported ACPI signal: %u", Function); + break; } return AE_OK; diff --git a/drivers/fb/fb.c b/drivers/fb/fb.c index b43912f4..8912adb3 100644 --- a/drivers/fb/fb.c +++ b/drivers/fb/fb.c @@ -93,10 +93,6 @@ void init_framebuffer(const struct multiboot2_tag_framebuffer *fb) { banner_size = (size_t) width * (LOGO_HEIGHT + FONT_SIZE); switch (bpp) { - case 0 ... 7: - printk("FB: Unsupported framebuffer BPP: %u\n", bpp); - has_fb = false; - return; case 8: put_pixel = put_pixel8; break; @@ -117,7 +113,9 @@ void init_framebuffer(const struct multiboot2_tag_framebuffer *fb) { banner_size *= 4; break; default: - BUG(); + warning("FB: Unsupported framebuffer BPP: %u", bpp); + has_fb = false; + return; } line_width = pitch * FONT_SIZE; diff --git a/drivers/serial.c b/drivers/serial.c index 165d0a34..2564f416 100644 --- a/drivers/serial.c +++ b/drivers/serial.c @@ -51,7 +51,8 @@ static inline const char *com_name(com_idx_t com) { case COM4: return "COM4"; default: - BUG(); + warning("Invalid COM port %u\n", com); + return NULL; } } diff --git a/include/console.h b/include/console.h index 5181bd17..04e87a18 100644 --- a/include/console.h +++ b/include/console.h @@ -52,5 +52,6 @@ extern void fb_console_write(void *arg, const char *buf, size_t len); extern void register_console_callback(console_callback_t func, void *arg); extern void panic(const char *fmt, ...); +extern void warning(const char *fmt, ...); #endif /* KTF_CONSOLE_H */ diff --git a/include/lib.h b/include/lib.h index 21e0a87e..bce6f233 100644 --- a/include/lib.h +++ b/include/lib.h @@ -350,7 +350,7 @@ static inline void sysret(void) { #define BUG() \ do { \ - panic("BUG in %s() at line %u\n", __func__, __LINE__); \ + panic("BUG in %s() at line %u", __func__, __LINE__); \ } while (true) #define BUG_ON(cond) \ do { \ @@ -358,10 +358,20 @@ static inline void sysret(void) { BUG(); \ } while (0) +#define WARN(msg) \ + do { \ + warning("Warning in %s() at line %u: %s", __func__, __LINE__, (msg)); \ + } while (0) +#define WARN_ON(cond, msg) \ + do { \ + if ((cond)) \ + WARN((msg)); \ + } while (0) + #define ASSERT(cond) \ do { \ if (!(cond)) \ - panic("%s: Assert at %d failed: %s\n", __func__, __LINE__, STR((cond))); \ + panic("%s: Assert at %d failed: %s", __func__, __LINE__, STR((cond))); \ } while (0) /* I/O Ports handling */ diff --git a/mm/pmm.c b/mm/pmm.c index 26bf1eb6..0a18cd0f 100644 --- a/mm/pmm.c +++ b/mm/pmm.c @@ -83,7 +83,7 @@ static frames_array_t *new_frames_array(void) { array = get_free_page(GFP_KERNEL); if (!array) - panic("PMM: Unable to allocate new page for frame array\n"); + panic("PMM: Unable to allocate new page for frame array"); dprintk("%s: allocated new frames array: %p\n", __func__, array); @@ -109,15 +109,11 @@ static bool is_frames_array_free(const frames_array_t *array) { if (array->meta.free_count < ARRAY_SIZE(array->frames)) return false; else if (array->meta.free_count > ARRAY_SIZE(array->frames)) - panic("PMM: incorrect number of free slots: %d in array: %p\n", + panic("PMM: incorrect number of free slots: %d in array: %p", array->meta.free_count, array); - for (unsigned i = 0; i < ARRAY_SIZE(array->frames); i++) { - const frame_t *frame = &array->frames[i]; - - if (!is_frame_free(frame)) - panic("PMM: found occupied slot in an empty array: %p\n", array); - } + for (unsigned i = 0; i < ARRAY_SIZE(array->frames); i++) + ASSERT(is_frame_free(&array->frames[i])); return true; } @@ -171,7 +167,7 @@ static inline frame_t *take_frame(frame_t *frame, frames_array_t *array) { } static inline frame_t *put_frames_array_entry(frame_t *frame, frames_array_t *array) { - BUG_ON(is_frame_free(frame)); + ASSERT(!is_frame_free(frame)); if (!array) array = find_frames_array(frame); @@ -190,7 +186,7 @@ static inline frame_t *get_frames_array_entry(void) { frames_array_t *array = get_frames_array(); if (!array) - panic("PMM: Unable to get a free array of frames' metadata\n"); + panic("PMM: Unable to get a free array of frames' metadata"); for (unsigned i = 0; i < ARRAY_SIZE(array->frames); i++) { frame_t *frame = &array->frames[i]; @@ -203,7 +199,7 @@ static inline frame_t *get_frames_array_entry(void) { } static inline void destroy_frame(frame_t *frame) { - BUG_ON(is_frame_used(frame)); + ASSERT(!is_frame_used(frame)); if (frame) { list_unlink(&frame->list); @@ -260,7 +256,7 @@ static unsigned find_first_avail_region(void) { return i; } - panic("PMM: Cannot obtain first available physical memory address range\n"); + panic("PMM: Cannot obtain first available physical memory address range"); UNREACHABLE(); } @@ -313,8 +309,8 @@ static size_t process_memory_range(unsigned index, unsigned first_avail_region) } if (cur != end) { - panic( - "PMM range processing failed: start=0x%016lx end=0x%016lx current=0x%016lx\n", + warning( + "PMM range processing failed: start=0x%016lx end=0x%016lx current=0x%016lx", start, end, cur); } @@ -343,12 +339,12 @@ static inline void check_early_frames(unsigned first_avail_region) { addr_range_t range; if (get_avail_memory_range(first_avail_region, &range) < 0) - panic("PMM: Cannot obtain first available physical memory address range\n"); + panic("PMM: Cannot obtain first available physical memory address range"); early_frames_cnt = (MB(EARLY_VIRT_MEM) - get_region_free_start(range.start)) / PAGE_SIZE; if (frames_count[PAGE_ORDER_4K] < early_frames_cnt) { - panic("Not enough early frames: %u missing\n", + panic("Not enough early frames: %u missing", early_frames_cnt - frames_count[PAGE_ORDER_4K]); } } @@ -395,8 +391,7 @@ static inline frame_t *reserve_frame(frame_t *frame) { } static inline bool return_frame(frame_t *frame) { - if (!is_frame_used(frame)) - panic("PMM: trying to return unused frame: %p\n", frame); + ASSERT(is_frame_used(frame)); if (--frame->refcount == 0) { list_unlink(&frame->list); @@ -458,7 +453,7 @@ frame_t *get_free_frames_cond(free_frames_cond_t cb) { } static inline void relink_frame_to_order(frame_t *frame, unsigned int new_order) { - BUG_ON(new_order > MAX_PAGE_ORDER); + ASSERT(new_order <= MAX_PAGE_ORDER); list_unlink(&frame->list); frames_count[frame->order]--; @@ -548,13 +543,16 @@ void put_free_frames(mfn_t mfn, unsigned int order) { spin_lock(&lock); frame = find_mfn_frame(busy_frames, mfn, order); - if (!frame) - panic("PMM: unable to find frame: %lx, order: %u among busy frames\n", mfn, - order); + if (!frame) { + warning("PMM: unable to find frame: %lx, order: %u among busy frames", mfn, + order); + goto unlock; + } if (return_frame(frame)) merge_frames(frame); +unlock: spin_unlock(&lock); } diff --git a/mm/regions.c b/mm/regions.c index 83c57ea8..4dbbf356 100644 --- a/mm/regions.c +++ b/mm/regions.c @@ -85,7 +85,7 @@ addr_range_t get_memory_range(paddr_t pa) { memset(&r, 0, sizeof(r)); if (mbi_get_memory_range(pa, &r) < 0) /* FIXME: e820_lower_memory_bound() */ - panic("Unable to get memory range for: 0x%016lx\n", pa); + panic("Unable to get memory range for: 0x%016lx", pa); return r; } diff --git a/mm/slab.c b/mm/slab.c index 37e45f75..e85925ea 100644 --- a/mm/slab.c +++ b/mm/slab.c @@ -320,7 +320,7 @@ static void ktf_free(void *ptr) { } } - panic("Attempted to free %p and couldn't find it\n", ptr); + panic("Attempted to free %p and couldn't find it", ptr); /* If we reached here, something terribly went wrong */ UNREACHABLE(); } diff --git a/mm/vmm.c b/mm/vmm.c index fedcd89c..b40fe62c 100644 --- a/mm/vmm.c +++ b/mm/vmm.c @@ -36,7 +36,7 @@ void *get_free_pages(unsigned int order, gfp_flags_t flags) { mfn_t mfn; if (!boot_flags.virt) - panic("Unable to use %s() before final page tables are set\n", __func__); + panic("Unable to use %s() before final page tables are set", __func__); if (!frame) return NULL; diff --git a/smp/mptables.c b/smp/mptables.c index fdfb3679..1d44ad8e 100644 --- a/smp/mptables.c +++ b/smp/mptables.c @@ -110,8 +110,10 @@ static mpc_hdr_t *get_mpc_addr(const mpf_t *mpf_ptr) { mpc_hdr_t *mpc_ptr; mpc_ptr = paddr_to_virt_kern(mpf_ptr->mpc_base); - if (!validate_mpc(mpc_ptr)) - panic("Incorrect MP Configuration Table found!\n"); + if (!validate_mpc(mpc_ptr)) { + warning("Incorrect MP Configuration Table found!"); + return NULL; + } return mpc_ptr; } @@ -284,7 +286,8 @@ static void process_mpc_entries(mpc_hdr_t *mpc_ptr) { break; } default: - panic("Unknown MP Configuration Table entry type: %x\n", *entry_ptr); + warning("Unknown MP Configuration Table entry type: %x", *entry_ptr); + break; } } } @@ -307,6 +310,9 @@ int init_mptables(void) { } mpc_ptr = get_mpc_addr(mpf_ptr); + if (!mpc_ptr) + return -EINVAL; + if (opt_debug) dump_mpc_hdr(mpc_ptr); process_mpc_entries(mpc_ptr);