42_KFS/src/memory/memory.c

54 lines
1.8 KiB
C
Raw Normal View History

#include "memory.h"
2024-10-18 08:45:37 -04:00
#include "debug.h"
2024-10-14 18:29:46 -04:00
#include "kprintf.h"
#include "utils.h"
2024-10-14 18:29:46 -04:00
#include <stdint.h>
2024-10-14 18:29:46 -04:00
uint32_t *page_directory = &boot_page_directory;
uint32_t page_table_default[1024] __attribute__((aligned(PAGE_SIZE)));
uint32_t multiboot_page_table[1024] __attribute__((aligned(PAGE_SIZE)));
uint32_t mem_size;
2024-11-15 07:35:48 -05:00
multiboot_memory_map_t *mmap_addr;
multiboot_uint32_t mmap_length;
2024-10-21 10:29:50 -04:00
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);
// 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) | INIT_FLAGS;
multiboot_info_t *mbd_virt =
(multiboot_info_t *)(GET_PAGE_ADDR(1023, 0) +
(uint32_t)mbd % PAGE_SIZE);
// 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;
2024-11-15 07:35:48 -05:00
mmap_addr = (multiboot_memory_map_t *)(GET_PAGE_ADDR(1023, mbd_size) +
(uint32_t)mbd_virt->mmap_addr %
PAGE_SIZE);
mmap_length = mbd_virt->mmap_length / sizeof(multiboot_memory_map_t);
}
void init_memory(multiboot_info_t *mbd, uint32_t magic)
{
2024-10-18 08:45:37 -04:00
assert(page_directory);
for (uint16_t i = 0; i < 0x300; i++)
2024-10-17 10:10:39 -04:00
page_directory[i] = 0x02;
init_page_table(page_table_default, 0);
2024-10-21 10:29:50 -04:00
page_directory[0] = ((uint32_t)page_table_default - HEAP_END) | 0x03;
init_multiboot(mbd, magic);
2024-10-17 10:10:39 -04:00
}