Skip to content

Commit

Permalink
kernel: Prettify logs
Browse files Browse the repository at this point in the history
  • Loading branch information
coditva committed Oct 11, 2018
1 parent 74afaa6 commit 15dffe9
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 30 deletions.
5 changes: 5 additions & 0 deletions kernel/boot/gdt.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <logger.h>
#include "boot/gdt.h"

gdt_entry_t gdt[GDT_SIZE];
Expand Down Expand Up @@ -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);

Expand All @@ -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);
}
22 changes: 15 additions & 7 deletions kernel/boot/multiboot.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
}
22 changes: 22 additions & 0 deletions kernel/include/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,34 @@
# define LOG_INFO 2
# define LOG_DEBUG 3

# define LOG_HRULE \
"------------------------------------------------------------------------\n"

# ifdef DEBUG
# include<kio.h>

# 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 */
8 changes: 8 additions & 0 deletions kernel/interrupt/interrupt.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <logger.h>
#include <types.h>
#include "interrupt/interrupt.h"
#include "io/portio/portio.h"

Expand All @@ -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];
Expand All @@ -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,
Expand Down Expand Up @@ -65,4 +71,6 @@ void idt_init(void)
write_port(PIC2_DATA, 0xff);

idt_load(&idt_pointer);

klog_status_ok(LOG_DEBUG);
}
15 changes: 14 additions & 1 deletion kernel/io/serial/serial.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <logger.h>
#include <io.h>
#include <stdarg.h>
#include "io/serial/serial.h"
Expand All @@ -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)
Expand All @@ -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') {
Expand Down
2 changes: 1 addition & 1 deletion kernel/io/serial/serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
16 changes: 1 addition & 15 deletions kernel/kernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -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$ ");
Expand Down
18 changes: 12 additions & 6 deletions kernel/mm/page_frame.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
Expand All @@ -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
}

Expand Down

0 comments on commit 15dffe9

Please sign in to comment.