From a042169845662723f404be2e0a00a303451e19fb Mon Sep 17 00:00:00 2001 From: Maxime Bloch Date: Tue, 28 Jan 2020 22:25:37 +0100 Subject: [PATCH] Add print function, add more memory prints at startup --- kernel/kernel.c | 89 ++++++++++++++++++++----------------------------- kernel/memory.c | 65 +++++++++++++++--------------------- 2 files changed, 63 insertions(+), 91 deletions(-) diff --git a/kernel/kernel.c b/kernel/kernel.c index 21003f9..2d1e2a0 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -2,7 +2,7 @@ #if defined(__linux__) #error "You are not using a cross-compiler, you will most certainly run into trouble" #endif - + /* This tutorial will only work for the 32-bit ix86 targets. */ #if !defined(__i386__) #error "This kernel needs to be compiled with a ix86-elf compiler" @@ -16,76 +16,59 @@ #include "memory.c" #include "interrupts.c" #include "shell.c" +#include "util/printer.c" static inline bool are_interrupts_enabled() { unsigned long flags; asm volatile ( "pushf\n\t" "pop %0" - : "=g"(flags) ); + : "=g"(flags)); return flags & (1 << 9); } -void kernel_main(void) -{ - /* Initialize terminal interface */ - terminal_initialize(); +void kernel_main(void) { + /* Initialize terminal interface */ + terminal_initialize(); + + terminal_putchar('H'); + terminal_putchar('e'); + terminal_putchar('l'); + terminal_putchar('l'); + terminal_putchar('o'); - terminal_putchar('H'); - terminal_putchar('e'); - terminal_putchar('l'); - terminal_putchar('l'); - terminal_putchar('o'); - terminal_setcolor(vga_entry_color(VGA_COLOR_GREEN, VGA_COLOR_BLACK)); - terminal_writestring(" kernel"); + print(" kernel"); terminal_setcolor(vga_entry_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK)); - terminal_writestring(" World!\n"); - terminal_writestring("Newlines!\n"); + print(" World!\n"); + print("Newlines!\n"); - char* memory_str = alloc(sizeof(char) * 7); - for (int i = 0; i < 6; i++) { - memory_str[i] = "Memory"[i]; - } - memory_str[6] = 0; + char *memory_str = alloc(sizeof(char) * 7); + for (int i = 0; i < 6; i++) { + memory_str[i] = "Memory"[i]; + } + memory_str[6] = 0; - char* management_str = alloc(sizeof(char) * 13); - for (int i = 0; i < 13; i++) { - management_str[i] = " management!\n"[i]; - } - management_str[13] = 0; + char *management_str = alloc(sizeof(char) * 13); + for (int i = 0; i < 13; i++) { + management_str[i] = " management!\n"[i]; + } + management_str[13] = 0; + print(memory_str); + print(management_str); - terminal_writestring(memory_str); - terminal_writestring(management_str); - - terminal_writestring("Mem after freeing!\n"); + print("Mem after freeing\n"); free(memory_str); print_memory(); - free(management_str); + free(management_str); print_memory(); - char* memory_after_free = alloc(sizeof(char) * 12); - for (int i = 0; i < 13; i++) { - memory_after_free[i] = " Some text\n"[i]; + + print((are_interrupts_enabled()) ? "Interrupts!\n" : "No interrupts :(\n"); + + interrupt_init(); + + for (;;) { + shell_step(); } - memory_after_free[13] = 0; - - terminal_writestring(memory_after_free); - - terminal_writestring("Memory after new allocation!\n"); - alloc(1000); - print_memory(); - - terminal_writestring("Free again\n"); - free(memory_after_free); - print_memory(); - - - terminal_writestring((are_interrupts_enabled())? "Interrupts!\n": "No interrupts :(\n"); - - interrupt_init(); - - for(;;) { - shell_step(); - } } \ No newline at end of file diff --git a/kernel/memory.c b/kernel/memory.c index 80b1ef3..9ac1aaf 100644 --- a/kernel/memory.c +++ b/kernel/memory.c @@ -5,6 +5,7 @@ #include #include "terminal.c" +#include "util/printer.c" #define MEMORY_START 0x200000 //#define MEMORY_END 0x300000 @@ -71,45 +72,33 @@ void print_memory() { page_tag *curr_page = start; int i = 0; while (curr_page != NULL) { - terminal_writeint(i, 10); - terminal_write(": [", 3); - terminal_writeint((int) curr_page, 16); - terminal_write(" ", 1); - terminal_writeint(sizeof(page_tag), 10); - terminal_write("] [", 3); - terminal_writeint((int) (curr_page + 1), 16); - terminal_write(" ", 1); - terminal_writeint(curr_page->size, 10); - terminal_writestring("]\n"); - -// printf("%d: [%p (%zu)] [%p (%zu)]\n", -// i, -// curr_page, sizeof(page_tag), -// curr_page + 1, curr_page->size); - - - void *empty_start = (void *) curr_page + sizeof(page_tag) + curr_page->size; - if (empty_start + sizeof(page_tag) < (void *) curr_page->next) { - terminal_writestring("empty_page ("); - terminal_writeint(sizeof(page_tag), 10); - terminal_writestring(", "); - terminal_writeint( - (void *) curr_page->next - - (empty_start + sizeof(page_tag)), 10); - terminal_writestring(")\n"); -// printf("empty_page (%ld, %ld)\n", -// sizeof(page_tag), -// (void *) curr_page->next - -// (empty_start + sizeof(page_tag))); - } else if (empty_start < (void *) curr_page->next) { - terminal_writestring("not enough room ("); - terminal_writeint((void *) curr_page->next - empty_start, 10); - terminal_writestring(")\n"); -// printf("not enough room (%ld)\n", -// (void *) curr_page->next - empty_start); - } + print("%d: [%x (%d)] [%x (%d)]\n", + i, + curr_page, sizeof(page_tag), + (curr_page + 1), curr_page->size); +// +// void *empty_start = (void *) curr_page + sizeof(page_tag) + curr_page->size; +// if (empty_start + sizeof(page_tag) < (void *) curr_page->next) { +// terminal_writestring("_: empty_page ("); +// terminal_writeint(sizeof(page_tag), 10); +// terminal_writestring(", "); +// terminal_writeint( +// (void *) curr_page->next - +// (empty_start + sizeof(page_tag)), 10); +// terminal_writestring(")\n"); +//// printf("empty_page (%ld, %ld)\n", +//// sizeof(page_tag), +//// (void *) curr_page->next - +//// (empty_start + sizeof(page_tag))); +// } else if (empty_start < (void *) curr_page->next) { +// terminal_writestring("_: not enough room ("); +// terminal_writeint((void *) curr_page->next - empty_start, 10); +// terminal_writestring(")\n"); +//// printf("not enough room (%ld)\n", +//// (void *) curr_page->next - empty_start); +// } curr_page = curr_page->next; - i++; + i += 1; } terminal_write("------------------\n\n", 20); }