diff --git a/arch/Makefile b/arch/Makefile index 917b99c..8c2e73e 100644 --- a/arch/Makefile +++ b/arch/Makefile @@ -7,8 +7,9 @@ OBJS = start.s.o OBJS += init.c.o OBJS += main.c.o -OBJS += logger.c.o console.c.o +OBJS += logger.c.o console.c.o kprintf.c.o OBJS += multiboot.c.o +OBJS += memory.c.o OBJS += protected.c.o protected.asm.cpp.o OBJS += exception.c.o exception.asm.cpp.o diff --git a/arch/init.c b/arch/init.c index fa6d20a..5c0a4b3 100644 --- a/arch/init.c +++ b/arch/init.c @@ -1,9 +1,17 @@ -#include "multiboot.h" #include "console.h" +#include "logger.h" +#include "multiboot.h" +#include "memory.h" void init(struct KernelMQ_Multiboot_Info multiboot_info) { console_initialize(); + logger_info("Memory info:"); + + print_memory_info(); + + logger_info("Multiboot info:"); + print_multiboot_info(multiboot_info); } diff --git a/arch/kprintf.c b/arch/kprintf.c new file mode 100644 index 0000000..de8c5ff --- /dev/null +++ b/arch/kprintf.c @@ -0,0 +1,60 @@ +#include "kprintf.h" + +#include "console.h" +#include "util.h" + +void kprintf(const char *format, ...) +{ + console_setcolor(VGA_COLOR_LIGHT_GREY); + + char **arg = (char **) &format; + int c; + char buf[20]; + arg++; + + while ((c = *format++) != 0) + { + if (c != '%') { + console_putc(c); + } + else { + char *p, *p2; + int pad0 = 0, pad = 0; + c = *format++; + if (c == '0') + { + pad0 = 1; + c = *format++; + } + if (c >= '0' && c <= '9') + { + pad = c - '0'; + c = *format++; + } + switch (c) + { + case 'd': + case 'u': + case 'x': + itoa (buf, c, *((int *) arg++)); + p = buf; + goto string; + break; + case 's': + p = *arg++; + if (! p) + p = "(null)"; +string: + for (p2 = p; *p2; p2++); + for (; p2 < p + pad; p2++) + console_putc(pad0 ? '0' : ' '); + while (*p) + console_putc(*p++); + break; + default: + console_putc(*((int *) arg++)); + break; + } + } + } +} diff --git a/arch/kprintf.h b/arch/kprintf.h new file mode 100644 index 0000000..f09f875 --- /dev/null +++ b/arch/kprintf.h @@ -0,0 +1,6 @@ +#ifndef KERNELMQ_INCLUDED_KPRINTF +#define KERNELMQ_INCLUDED_KPRINTF 1 + +void kprintf(const char *format, ...); + +#endif diff --git a/arch/linker.ld b/arch/linker.ld index d325c0d..681e3fb 100644 --- a/arch/linker.ld +++ b/arch/linker.ld @@ -1,6 +1,11 @@ OUTPUT_ARCH("i386") ENTRY(_start) +_memory_offset = 0xC0000000; /* 3 GB */ + +_memory_phys_base = 4M; +_memory_virt_base = (_memory_phys_base + _memory_offset); + SECTIONS { . = 1M; diff --git a/arch/memory.c b/arch/memory.c new file mode 100644 index 0000000..7d863a9 --- /dev/null +++ b/arch/memory.c @@ -0,0 +1,17 @@ +#include "memory.h" + +#include "kprintf.h" + +// Defined in linker script +extern char _memory_offset; +extern char _memory_phys_base; +extern char _memory_virt_base; + +const unsigned int memory_offset = (unsigned int)&_memory_offset; +const unsigned int memory_phys_base = (unsigned int)&_memory_phys_base; +const unsigned int memory_virt_base = (unsigned int)&_memory_virt_base; + +void print_memory_info() +{ + kprintf("0x%x + 0x%x = 0x%x\n", memory_phys_base, memory_offset, memory_virt_base); +} diff --git a/arch/memory.h b/arch/memory.h new file mode 100644 index 0000000..e67e426 --- /dev/null +++ b/arch/memory.h @@ -0,0 +1,6 @@ +#ifndef KERNELMQ_INCLUDED_MEMORY +#define KERNELMQ_INCLUDED_MEMORY 1 + +void print_memory_info(); + +#endif diff --git a/arch/multiboot.c b/arch/multiboot.c index 71fde71..4e6f8d3 100644 --- a/arch/multiboot.c +++ b/arch/multiboot.c @@ -1,7 +1,6 @@ #include "multiboot.h" -#include "console.h" -#include "util.h" +#include "kprintf.h" #define MULTIBOOT_1_MAGIC 0x2BADB002 #define MULTIBOOT_2_MAGIC 0x36d76289 @@ -67,9 +66,6 @@ struct multiboot_tag_mmap struct multiboot_mmap_entry entries[0]; }; -static void itoa(char *buf, int base, int d); -static void printf(const char *format, ...); - static void print_multiboot_tag(const struct multiboot_tag *tag); static void print_multiboot_tag_cmdline (const struct multiboot_tag_string *tag); @@ -80,17 +76,17 @@ static void print_multiboot_tag_mmap (const struct multiboot_tag_mmap void print_multiboot_info(struct KernelMQ_Multiboot_Info info) { if (info.magic == MULTIBOOT_1_MAGIC) { - printf("Old Multiboot specification does not support modules."); + kprintf("Old Multiboot specification does not support modules."); return; } if (info.magic != MULTIBOOT_2_MAGIC) { - printf("No Multiboot-compliant bootloader is not supported."); + kprintf("No Multiboot-compliant bootloader is not supported."); return; } if (info.addr & 7) { - printf("Unaligned Multiboot information address: 0x%x\n", info.addr); + kprintf("Unaligned Multiboot information address: 0x%x\n", info.addr); return; } @@ -127,29 +123,29 @@ void print_multiboot_tag(const struct multiboot_tag *const tag) void print_multiboot_tag_cmdline(const struct multiboot_tag_string *const tag) { - printf("Kernel command line: %s\n", tag->string); + kprintf("Kernel command line: %s\n", tag->string); } void print_multiboot_tag_module(const struct multiboot_tag_module *const tag) { - printf("Module at 0x%x-0x%x, command line: %s\n", tag->mod_start, tag->mod_end, tag->cmdline); + kprintf("Module at 0x%x-0x%x, command line: %s\n", tag->mod_start, tag->mod_end, tag->cmdline); } void print_multiboot_tag_basic_meminfo(const struct multiboot_tag_basic_meminfo *const tag) { - printf("mem_lower = %uKB, mem_upper = %uKB\n", tag->mem_lower, tag->mem_upper); + kprintf("mem_lower = %uKB, mem_upper = %uKB\n", tag->mem_lower, tag->mem_upper); } void print_multiboot_tag_mmap(const struct multiboot_tag_mmap *const tag) { - printf("Memory map:\n"); + kprintf("Memory map:\n"); for ( const multiboot_memory_map_t *mmap = tag->entries; (unsigned char*)mmap < (unsigned char*)tag + tag->size; mmap = (multiboot_memory_map_t*)((unsigned long) mmap + tag->entry_size) ) { - printf( + kprintf( " base_addr = 0x%x%x, length = 0x%x%x, type = 0x%x\n", (unsigned)(mmap->addr >> 32), (unsigned)(mmap->addr & 0xffffffff), @@ -159,56 +155,3 @@ void print_multiboot_tag_mmap(const struct multiboot_tag_mmap *const tag) ); } } - -void printf(const char *format, ...) -{ - char **arg = (char **) &format; - int c; - char buf[20]; - arg++; - while ((c = *format++) != 0) - { - if (c != '%') - console_putc(c); - else - { - char *p, *p2; - int pad0 = 0, pad = 0; - c = *format++; - if (c == '0') - { - pad0 = 1; - c = *format++; - } - if (c >= '0' && c <= '9') - { - pad = c - '0'; - c = *format++; - } - switch (c) - { - case 'd': - case 'u': - case 'x': - itoa (buf, c, *((int *) arg++)); - p = buf; - goto string; - break; - case 's': - p = *arg++; - if (! p) - p = "(null)"; -string: - for (p2 = p; *p2; p2++); - for (; p2 < p + pad; p2++) - console_putc(pad0 ? '0' : ' '); - while (*p) - console_putc(*p++); - break; - default: - console_putc(*((int *) arg++)); - break; - } - } - } -}