forked from Bananymous/banan-os
Kernel: Kmalloc now has its memory statically allocated
We don't use the memory map given by bootloader since this automatically maps the memory to a available space.
This commit is contained in:
parent
9c0f3dd996
commit
2de64b592d
|
@ -47,6 +47,8 @@ MMU::MMU()
|
||||||
m_highest_paging_struct[i] = (uint64_t)page_directory | Flags::Present;
|
m_highest_paging_struct[i] = (uint64_t)page_directory | Flags::Present;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FIXME: We should just identity map until g_kernel_end
|
||||||
|
|
||||||
// create and identity map first 6 MiB
|
// create and identity map first 6 MiB
|
||||||
uint64_t* page_directory1 = (uint64_t*)(m_highest_paging_struct[0] & PAGE_MASK);
|
uint64_t* page_directory1 = (uint64_t*)(m_highest_paging_struct[0] & PAGE_MASK);
|
||||||
for (uint64_t i = 0; i < 3; i++)
|
for (uint64_t i = 0; i < 3; i++)
|
||||||
|
|
|
@ -28,9 +28,7 @@ g_boot_stack_bottom:
|
||||||
g_boot_stack_top:
|
g_boot_stack_top:
|
||||||
|
|
||||||
# 0 MiB -> 1 MiB: bootloader stuff
|
# 0 MiB -> 1 MiB: bootloader stuff
|
||||||
# 1 MiB -> 2 MiB: kernel
|
# 1 MiB -> : kernel
|
||||||
# 2 MiB -> 3 MiB: kmalloc
|
|
||||||
# 3 MiB -> 4 Mib: kmalloc_fixed
|
|
||||||
.align 32
|
.align 32
|
||||||
boot_page_directory_pointer_table:
|
boot_page_directory_pointer_table:
|
||||||
.skip 4 * 8
|
.skip 4 * 8
|
||||||
|
|
|
@ -35,7 +35,9 @@ static uint64_t* allocate_page_aligned_page()
|
||||||
|
|
||||||
MMU::MMU()
|
MMU::MMU()
|
||||||
{
|
{
|
||||||
// Identity map from 4 KiB -> 6 MiB
|
// FIXME: We should just identity map until g_kernel_end
|
||||||
|
|
||||||
|
// Identity map from 0 -> 6 MiB
|
||||||
m_highest_paging_struct = allocate_page_aligned_page();
|
m_highest_paging_struct = allocate_page_aligned_page();
|
||||||
|
|
||||||
uint64_t* pdpt = allocate_page_aligned_page();
|
uint64_t* pdpt = allocate_page_aligned_page();
|
||||||
|
|
|
@ -41,9 +41,7 @@
|
||||||
# we will identity map first 4 MiB
|
# we will identity map first 4 MiB
|
||||||
|
|
||||||
# 0 MiB -> 1 MiB: bootloader stuff
|
# 0 MiB -> 1 MiB: bootloader stuff
|
||||||
# 1 MiB -> 4 MiB: kernel
|
# 1 MiB -> : kernel
|
||||||
# 4 MiB -> 5 MiB: kmalloc
|
|
||||||
# 5 MiB -> 6 MiB: kmalloc_fixed
|
|
||||||
.align 4096
|
.align 4096
|
||||||
boot_pml4:
|
boot_pml4:
|
||||||
.skip 512 * 8
|
.skip 512 * 8
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
#include <kernel/CriticalScope.h>
|
#include <kernel/CriticalScope.h>
|
||||||
#include <kernel/kprint.h>
|
#include <kernel/kprint.h>
|
||||||
#include <kernel/Memory/kmalloc.h>
|
#include <kernel/Memory/kmalloc.h>
|
||||||
#include <kernel/multiboot.h>
|
|
||||||
|
|
||||||
#include <kernel/Thread.h>
|
#include <kernel/Thread.h>
|
||||||
|
|
||||||
|
@ -10,6 +9,8 @@
|
||||||
|
|
||||||
static constexpr size_t s_kmalloc_min_align = alignof(max_align_t);
|
static constexpr size_t s_kmalloc_min_align = alignof(max_align_t);
|
||||||
|
|
||||||
|
static uint8_t s_kmalloc_storage[2 * MB];
|
||||||
|
|
||||||
struct kmalloc_node
|
struct kmalloc_node
|
||||||
{
|
{
|
||||||
void set_align(ptrdiff_t align) { m_align = align; }
|
void set_align(ptrdiff_t align) { m_align = align; }
|
||||||
|
@ -60,9 +61,9 @@ static_assert(sizeof(kmalloc_node) == s_kmalloc_min_align);
|
||||||
|
|
||||||
struct kmalloc_info
|
struct kmalloc_info
|
||||||
{
|
{
|
||||||
static constexpr uintptr_t base = 0x00400000;
|
const uintptr_t base = (uintptr_t)s_kmalloc_storage;
|
||||||
static constexpr size_t size = 1 * MB;
|
const size_t size = sizeof(s_kmalloc_storage) / 2;
|
||||||
static constexpr uintptr_t end = base + size;
|
const uintptr_t end = base + size;
|
||||||
|
|
||||||
kmalloc_node* first() { return (kmalloc_node*)base; }
|
kmalloc_node* first() { return (kmalloc_node*)base; }
|
||||||
kmalloc_node* from_address(void* addr)
|
kmalloc_node* from_address(void* addr)
|
||||||
|
@ -91,11 +92,10 @@ struct kmalloc_fixed_info
|
||||||
{
|
{
|
||||||
using node = kmalloc_fixed_node<64>;
|
using node = kmalloc_fixed_node<64>;
|
||||||
|
|
||||||
static constexpr uintptr_t base = s_kmalloc_info.end;
|
const uintptr_t base = s_kmalloc_info.end;
|
||||||
static constexpr size_t size = 1 * MB;
|
const size_t size = (uintptr_t)s_kmalloc_storage + sizeof(s_kmalloc_storage) - base;
|
||||||
static constexpr uintptr_t end = base + size;
|
const uintptr_t end = base + size;
|
||||||
static constexpr size_t node_count = size / sizeof(node);
|
const size_t node_count = size / sizeof(node);
|
||||||
static_assert(node_count < (1 << 16));
|
|
||||||
|
|
||||||
node* free_list_head = NULL;
|
node* free_list_head = NULL;
|
||||||
node* used_list_head = NULL;
|
node* used_list_head = NULL;
|
||||||
|
@ -108,42 +108,9 @@ struct kmalloc_fixed_info
|
||||||
};
|
};
|
||||||
static kmalloc_fixed_info s_kmalloc_fixed_info;
|
static kmalloc_fixed_info s_kmalloc_fixed_info;
|
||||||
|
|
||||||
extern "C" uintptr_t g_kernel_end;
|
|
||||||
|
|
||||||
void kmalloc_initialize()
|
void kmalloc_initialize()
|
||||||
{
|
{
|
||||||
if (!(g_multiboot_info->flags & (1 << 6)))
|
dprintln("kmalloc {8H}->{8H}", (uintptr_t)s_kmalloc_storage, (uintptr_t)s_kmalloc_storage + sizeof(s_kmalloc_storage));
|
||||||
Kernel::panic("Kmalloc: Bootloader didn't provide a memory map");
|
|
||||||
|
|
||||||
if ((uintptr_t)&g_kernel_end > s_kmalloc_info.base)
|
|
||||||
Kernel::panic("Kmalloc: Kernel end ({}) is over kmalloc base ({})", &g_kernel_end, (void*)s_kmalloc_info.base);
|
|
||||||
|
|
||||||
// Validate kmalloc memory
|
|
||||||
bool valid = false;
|
|
||||||
for (size_t i = 0; i < g_multiboot_info->mmap_length;)
|
|
||||||
{
|
|
||||||
multiboot_memory_map_t* mmmt = (multiboot_memory_map_t*)(g_multiboot_info->mmap_addr + i);
|
|
||||||
|
|
||||||
if (mmmt->type == 1)
|
|
||||||
{
|
|
||||||
if (mmmt->base_addr <= s_kmalloc_info.base && s_kmalloc_fixed_info.end <= mmmt->base_addr + mmmt->length)
|
|
||||||
{
|
|
||||||
valid = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
i += mmmt->size + sizeof(uint32_t);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!valid)
|
|
||||||
{
|
|
||||||
size_t kmalloc_total_size = s_kmalloc_info.size + s_kmalloc_fixed_info.size;
|
|
||||||
Kernel::panic("Kmalloc: Could not find {}.{} MB of memory",
|
|
||||||
kmalloc_total_size / MB,
|
|
||||||
kmalloc_total_size % MB
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// initialize fixed size allocations
|
// initialize fixed size allocations
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue