From 15dffe951796a31bcffeadbbb6170e1619de2620 Mon Sep 17 00:00:00 2001 From: Utkarsh Maheshwari Date: Thu, 11 Oct 2018 19:44:04 +0530 Subject: [PATCH] kernel: Prettify logs --- kernel/boot/gdt.c | 5 +++++ kernel/boot/multiboot.c | 22 +++++++++++++++------- kernel/include/logger.h | 22 ++++++++++++++++++++++ kernel/interrupt/interrupt.c | 8 ++++++++ kernel/io/serial/serial.c | 15 ++++++++++++++- kernel/io/serial/serial.h | 2 +- kernel/kernel.c | 16 +--------------- kernel/mm/page_frame.c | 18 ++++++++++++------ 8 files changed, 78 insertions(+), 30 deletions(-) diff --git a/kernel/boot/gdt.c b/kernel/boot/gdt.c index c481572..dd0d9b2 100644 --- a/kernel/boot/gdt.c +++ b/kernel/boot/gdt.c @@ -1,3 +1,4 @@ +#include #include "boot/gdt.h" gdt_entry_t gdt[GDT_SIZE]; @@ -25,6 +26,8 @@ void gdt_set_entry(uint32_t offset, uint32_t base, uint32_t limit, void gdt_init(void) { + klog_status_init(LOG_DEBUG, "GDT"); + /* first entry is NULL entry */ gdt_set_entry(0, 0, 0, 0); @@ -42,4 +45,6 @@ void gdt_init(void) gdt_load(gdt, GDT_SIZE * sizeof(gdt_entry_t) - 1); reload_segments(); + + klog_status_ok(LOG_DEBUG); } diff --git a/kernel/boot/multiboot.c b/kernel/boot/multiboot.c index 44fb5a0..34f6b41 100644 --- a/kernel/boot/multiboot.c +++ b/kernel/boot/multiboot.c @@ -4,30 +4,34 @@ static inline void multiboot_dump_mmap(multiboot_info_t *info) { +#ifdef DEBUG if (info->flags & MULTIBOOT_FLAG_MEM_MAP) { klog(LOG_DEBUG, "mem map len : 0x%x\n", info->mmap_len); klog(LOG_DEBUG, "mem map addr : 0x%x\n", info->mmap_addr); int i = 0; + klog(LOG_DEBUG, LOG_HRULE); klog(LOG_DEBUG, " | size | base | length | type\n"); - klog(LOG_DEBUG, "------------------------------------------------------------------\n"); + klog(LOG_DEBUG, LOG_HRULE); FOREACH_MEMORY_MAP(mmap, info) { klog(LOG_DEBUG, " %d | 0x%x | 0x%x %x | 0x%x %x | %d\n", i, mmap->size, mmap->base_addr_high, mmap->base_addr_low, mmap->len_high, mmap->len_low, mmap->type); i++; } + klog(LOG_DEBUG, LOG_HRULE); } else { klog(LOG_WARN, "memory map not set\n"); } +#endif } -void multiboot_dump_info(multiboot_info_t *info) +inline void multiboot_dump_info(multiboot_info_t *info) { -#ifndef DEBUG - return; -#endif - klog(LOG_DEBUG, "------------ MULTIBOOT DUMP ------------\n"); +#ifdef DEBUG + klog(LOG_DEBUG, LOG_HRULE); + klog(LOG_DEBUG, "MULTIBOOT DUMP START\n"); + klog(LOG_DEBUG, LOG_HRULE); klog(LOG_DEBUG, "multiboot header: 0x%x\n", info); klog(LOG_DEBUG, "flags : 0x%x\n", info->flags); klog(LOG_DEBUG, "mem lower : 0x%x\n", info->mem_lower); @@ -39,5 +43,9 @@ void multiboot_dump_info(multiboot_info_t *info) multiboot_dump_mmap(info); klog(LOG_DEBUG, "config table : 0x%x\n", info->config_table); klog(LOG_DEBUG, "bootloader name : %s\n", info->bootloader_name); - klog(LOG_DEBUG, "----------------------------------------\n"); + klog(LOG_DEBUG, LOG_HRULE); + klog(LOG_DEBUG, "MULTIBOOT DUMP END\n"); + klog(LOG_DEBUG, LOG_HRULE); + klog(LOG_DEBUG, "\n"); +#endif } diff --git a/kernel/include/logger.h b/kernel/include/logger.h index 478f536..07ab127 100644 --- a/kernel/include/logger.h +++ b/kernel/include/logger.h @@ -8,12 +8,34 @@ # define LOG_INFO 2 # define LOG_DEBUG 3 +# define LOG_HRULE \ + "------------------------------------------------------------------------\n" + # ifdef DEBUG # include + # define klog(LOG_LEVEL, FORMAT, ...) \ keprintf(LOG_LEVEL, FORMAT, ##__VA_ARGS__) + +# define klog_status_init(LOG_LEVEL, INIT_STRING) \ + keprintf(LOG_LEVEL, "Initializing %s...", INIT_STRING) + +# ifdef DEBUG_TO_SERIAL +# define STATUS_FORMAT_STRING "%s\n" +# else +# define STATUS_FORMAT_STRING "\n%c%c%c%c%c%c%s", 8, 8, 8, 8, 8, 8 +# endif +# define klog_status_ok(LOG_LEVEL) \ + keprintf(LOG_LEVEL, STATUS_FORMAT_STRING, " [OK]"); + +# define klog_status_fail(LOG_LEVEL) \ + keprintf(LOG_LEVEL, STATUS_FORMAT_STRING, "[FAIL]"); + # else # define klog(...) +# define klog_status_init(...) +# define klog_status_ok(...) +# define klog_status_fail(...) # endif /* end of DEBUG */ #endif /* end of include guard: DEBUG_H_IEXU6FAG */ diff --git a/kernel/interrupt/interrupt.c b/kernel/interrupt/interrupt.c index 47affaa..1cc5265 100644 --- a/kernel/interrupt/interrupt.c +++ b/kernel/interrupt/interrupt.c @@ -1,3 +1,5 @@ +#include +#include #include "interrupt/interrupt.h" #include "io/portio/portio.h" @@ -18,8 +20,10 @@ void isr_set_double_fault(void) void isr_init_keyboard(void) { + klog_status_init(LOG_DEBUG, "keyboard"); /* TODO: This will disable all other interrupts; prevent that */ write_port(0x21, 0xfd); + klog_status_ok(LOG_DEBUG); } idt_t idt[IDT_SIZE]; @@ -36,6 +40,8 @@ void idt_set_gate(int offset, uint32_t base, uint16_t selector, void idt_init(void) { + klog_status_init(LOG_DEBUG, "IDT"); + uint32_t idt_address = (uint32_t)&idt; idt_ptr_t idt_pointer = { .limit = (sizeof(idt_t) * IDT_SIZE) - 1, @@ -65,4 +71,6 @@ void idt_init(void) write_port(PIC2_DATA, 0xff); idt_load(&idt_pointer); + + klog_status_ok(LOG_DEBUG); } diff --git a/kernel/io/serial/serial.c b/kernel/io/serial/serial.c index 629c0e1..ef80a6a 100644 --- a/kernel/io/serial/serial.c +++ b/kernel/io/serial/serial.c @@ -1,3 +1,4 @@ +#include #include #include #include "io/serial/serial.h" @@ -9,7 +10,7 @@ #define MAX_BUFFER_SIZE 1024 static char buffer[MAX_BUFFER_SIZE]; -void serial_init(int port) +static inline void serial_init_port(int port) { write_port(port + 1, 0x00); // Disable all interrupts write_port(port + 3, 0x80); // Enable DLAB (set baud rate divisor) @@ -20,6 +21,18 @@ void serial_init(int port) write_port(port + 4, 0x0B); // IRQs enabled, RTS/DSR set } +void serial_init() +{ + klog_status_init(LOG_DEBUG, "serial ports"); + + serial_init_port(SERIAL_PORT1); + serial_init_port(SERIAL_PORT2); + serial_init_port(SERIAL_PORT3); + serial_init_port(SERIAL_PORT4); + + klog_status_ok(LOG_DEBUG); +} + void serial_write(int port, char *data) { while(*data != '\0') { diff --git a/kernel/io/serial/serial.h b/kernel/io/serial/serial.h index 8de03a7..909eb93 100644 --- a/kernel/io/serial/serial.h +++ b/kernel/io/serial/serial.h @@ -6,7 +6,7 @@ #define SERIAL_PORT3 0x3e8 #define SERIAL_PORT4 0x2e8 -void serial_init (int port); +void serial_init (); void serial_write (int port, char *data); int serial_read (int port); diff --git a/kernel/kernel.c b/kernel/kernel.c index 60a5648..b137178 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -16,26 +16,12 @@ extern void kmain(multiboot_info_t *multiboot_info, uint32_t multiboot_magic) page_frame_init(multiboot_info); page_frame_dump_map(); - klog(LOG_DEBUG, "\nInitializing Serial ports... "); - serial_init(SERIAL_PORT1); - serial_init(SERIAL_PORT2); - serial_init(SERIAL_PORT3); - serial_init(SERIAL_PORT4); - klog(LOG_DEBUG, "OK"); - - klog(LOG_DEBUG, "\nInitializing GDT... "); + serial_init(); gdt_init(); - klog(LOG_DEBUG, "OK"); - - klog(LOG_DEBUG, "\nInitializing IDT... "); idt_init(); - klog(LOG_DEBUG, "OK"); - - klog(LOG_DEBUG, "\nInitializing ISRs... "); isr_set_keyboard(); isr_set_double_fault(); isr_init_keyboard(); - klog(LOG_DEBUG, "OK"); klog(LOG_DEBUG, "\nInitialization complete.\n"); kprintf("\n$ "); diff --git a/kernel/mm/page_frame.c b/kernel/mm/page_frame.c index c42bad5..e259c3b 100644 --- a/kernel/mm/page_frame.c +++ b/kernel/mm/page_frame.c @@ -68,10 +68,6 @@ int page_frame_init(multiboot_info_t *multiboot_info) num_frames_bitset_frames = NUM_OF_A_PER_B(num_frames_bitset_lines, BITSET_LINES_PER_FRAME); - klog(LOG_DEBUG, "Number of frames: %d\n", num_frames); - klog(LOG_DEBUG, "Number of bitset lines: %d\nNumber of bitset pages: %d\n", - num_frames_bitset_lines, num_frames_bitset_frames); - /* mark all free pages */ FOREACH_MEMORY_MAP(mmap, multiboot_info) { if (mmap->type == MULTIBOOT_MEM_TYPE_FREE) { @@ -96,10 +92,17 @@ void page_frame_dump_map(void) { #ifdef DEBUG uint32_t *frame = frames_bitset; - klog(LOG_DEBUG, "frames_bitset addr: 0x%x\n", frame); + klog(LOG_DEBUG, LOG_HRULE); + klog(LOG_DEBUG, "PAGE FRAMES MEMORY DUMP START\n"); + klog(LOG_DEBUG, LOG_HRULE); + klog(LOG_DEBUG, "Number of frames: %d\n", num_frames); + klog(LOG_DEBUG, "Number of bitset lines: %d\n", num_frames_bitset_lines); + klog(LOG_DEBUG, "Number of bitset pages: %d\n", num_frames_bitset_frames); + klog(LOG_DEBUG, "Frames bitmap at 0x%x\n", frame); for (int j = 0; j < num_frames_bitset_lines; j++) { - klog(LOG_DEBUG, "\n%x: ", FRAME_NUM_TO_PAGE_ADDR(j * FRAMES_PER_BITSET)); + klog(LOG_DEBUG, "\n%x:", FRAME_NUM_TO_PAGE_ADDR(j * FRAMES_PER_BITSET)); for (int k = 0; k < FRAMES_PER_BITSET; k++) { + (k % 4) || klog(LOG_DEBUG, " "); if (frame[j] & (0x1 << k)) { /* free */ klog(LOG_DEBUG, "."); } else { @@ -108,6 +111,9 @@ void page_frame_dump_map(void) } } klog(LOG_DEBUG, "\n"); + klog(LOG_DEBUG, LOG_HRULE); + klog(LOG_DEBUG, "PAGE FRAMES MEMORY DUMP END\n"); + klog(LOG_DEBUG, LOG_HRULE); #endif }