diff --git a/headers/memory.h b/headers/memory.h index 4968a94..0ed46d0 100644 --- a/headers/memory.h +++ b/headers/memory.h @@ -1,5 +1,7 @@ #pragma once +#include "multiboot.h" + #include #include @@ -17,16 +19,16 @@ #define PT_START 256 #define GET_PAGE_ADDR(pd_index, pt_index) \ - (((pd_index * 1024) + pt_index) * 4096) + ((((uint32_t)pd_index * 1024) + (uint32_t)pt_index) * 4096) extern uint32_t _kernel_end; extern uint32_t boot_page_directory; extern uint32_t *page_directory; extern uint32_t page_table_default[1024]; -extern uint64_t mem_size; +extern uint32_t mem_size; uint32_t *virt_to_phys(uint32_t *virt_addr); -void init_memory(void); +void init_memory(multiboot_info_t *mbd, uint32_t magic); void *alloc_frames(size_t size); int free_frames(void *frame_ptr, size_t size); void *alloc_pages(size_t size); diff --git a/src/boot.s b/src/boot.s index 8215bf4..3eeb36c 100644 --- a/src/boot.s +++ b/src/boot.s @@ -34,7 +34,6 @@ boot_page_table1: # The kernel entry point. .section .multiboot.text, "a" .global _start -.global end_mem .type _start, @function _start: # Physical address of boot_page_table1. @@ -90,10 +89,6 @@ _start: movl $(boot_page_directory - 0xC0000000), %ecx movl %ecx, %cr3 - jmp set_mem_size - -end_mem: - # Enable paging and the write-protect bit. movl %cr0, %ecx orl $0x80000000, %ecx @@ -118,6 +113,9 @@ end_mem: # Set up the stack. mov $stack_top, %esp + push %eax + push %ebx + # Enter the high-level kernel. call kernel_main diff --git a/src/kernel.c b/src/kernel.c index a11fb10..fdcbf2c 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -55,14 +55,13 @@ static void set_mem_size(multiboot_info_t *mbd, uint32_t magic) } } -void kernel_main(void) +void kernel_main(multiboot_info_t *mbd, uint32_t magic) { terminal_initialize(); init_gdt(); init_idt(); - init_memory(); + init_memory(mbd, magic); load_drivers(); - /* set_mem_size(mbd, magic); */ kprintf(KERN_ALERT "I see no way to confuse an array of 256 seg:off pairs with a " "complex 8*unknown quantity -byte descriptor table. -- Troy " diff --git a/src/memory/memory.c b/src/memory/memory.c index 4e79a69..ed3ef61 100644 --- a/src/memory/memory.c +++ b/src/memory/memory.c @@ -1,18 +1,59 @@ #include "memory.h" #include "debug.h" #include "kprintf.h" +#include "utils.h" #include uint32_t *page_directory = &boot_page_directory; -uint32_t page_table_default[1024] __attribute__((aligned(4096))); -uint64_t mem_size; +uint32_t page_table_default[1024] __attribute__((aligned(PAGE_SIZE))); +uint32_t multiboot_page_table[1024] __attribute__((aligned(PAGE_SIZE))); +uint32_t mem_size; +multiboot_uint32_t *mmap_addr; +multiboot_uint32_t mmap_length; -void init_memory(void) +static void init_multiboot(multiboot_info_t *mbd, uint32_t magic) +{ + if (magic != MULTIBOOT_BOOTLOADER_MAGIC) + kpanic("invalid magic number! (git good skill issue)"); + + init_page_table(multiboot_page_table, 0); + page_directory[1023] = + ((uint32_t)multiboot_page_table - HEAP_END) | 0x03; + const size_t mbd_size = CEIL( + (uint32_t)mbd % PAGE_SIZE + sizeof(multiboot_info_t), PAGE_SIZE); + + kprintf("cramptes0\n"); + // Index multiboot_info_t struct + for (uint32_t i = 0; i < mbd_size; i++) + multiboot_page_table[i] = + ((uint32_t)mbd + PAGE_SIZE * i) & PAGE_MASK; + multiboot_info_t *mbd_virt = + (multiboot_info_t *)(GET_PAGE_ADDR(1023, 0) + + (uint32_t)mbd % PAGE_SIZE); + + kprintf("cramptes1\n"); + PRINT_PTR(mbd_virt); + /* PRINT_UINT(mbd_virt->mmap_length); */ + // Index mbd->mmap_addr pointers + for (uint32_t i = 0; i < mbd_virt->mmap_length; i++) + multiboot_page_table[i + mbd_size] = + ((mbd_virt->mmap_addr + i * PAGE_SIZE) & PAGE_MASK) | + INIT_FLAGS; + kprintf("cramptes2\n"); + mmap_addr = + (multiboot_uint32_t *)(GET_PAGE_ADDR(1023, mbd_size) + + (uint32_t)mbd_virt->mmap_addr % PAGE_SIZE); + kprintf("cramptes3\n"); + mmap_length = mbd_virt->mmap_length; +} + +void init_memory(multiboot_info_t *mbd, uint32_t magic) { assert(page_directory); for (uint16_t i = 0; i < 0x300; i++) page_directory[i] = 0x02; init_page_table(page_table_default, 0); page_directory[0] = ((uint32_t)page_table_default - HEAP_END) | 0x03; + init_multiboot(mbd, magic); } diff --git a/src/set_mem_size.s b/src/set_mem_size.s deleted file mode 100644 index 06a5d18..0000000 --- a/src/set_mem_size.s +++ /dev/null @@ -1,28 +0,0 @@ -.intel_syntax noprefix - -.section .text - .global set_mem_size - - set_mem_size: - jmp end_mem - xor ebx, ebx // i = 0 -.L1: - // loop condition - cmp ebx, [eax + 44] // mbd->mmap_length - jge end_mem - - // declare mmmt - mov ecx, [eax + 48] // mbd->mmap_addr - add ecx, ebx // + i - - // check if the mmmt->type is available (1) - mov edx, [ecx + 20] - cmp edx, 1 - jne .L2 - - // add the size of the block - mov edx, [ecx + 0] // mmmt->size - add mem_size, edx -.L2: - add ebx, 24 // i += sizeof(multiboot_memory_map_t) - jmp .L1