forked from xamidev/pepperOS
Fix braces + init_paging args
This commit is contained in:
@@ -21,8 +21,7 @@
|
||||
#define USER_CODE_SEGMENT 0x18
|
||||
#define USER_DATA_SEGMENT 0x20
|
||||
|
||||
struct GDTR
|
||||
{
|
||||
struct GDTR {
|
||||
uint16_t limit;
|
||||
uint64_t address;
|
||||
} __attribute__((packed));
|
||||
|
||||
@@ -33,11 +33,9 @@ void kheap_init()
|
||||
uintptr_t current_addr = kheap_start;
|
||||
|
||||
// Map/alloc enough pages for heap (KHEAP_SIZE)
|
||||
for (size_t i=0; i<heap_pages; i++)
|
||||
{
|
||||
for (size_t i=0; i<heap_pages; i++) {
|
||||
uintptr_t phys = pmm_alloc();
|
||||
if (phys == 0)
|
||||
{
|
||||
if (phys == 0) {
|
||||
panic(NULL, "Not enough memory available to initialize kernel heap.");
|
||||
}
|
||||
|
||||
@@ -63,15 +61,11 @@ void* kmalloc(size_t size)
|
||||
|
||||
struct heap_block_t* curr = head;
|
||||
|
||||
while (curr)
|
||||
{
|
||||
while (curr) {
|
||||
// Is block free and big enough for us?
|
||||
if (curr->free && curr->size >= size)
|
||||
{
|
||||
if (curr->free && curr->size >= size) {
|
||||
// We split the block if it is big enough
|
||||
if (curr->size >= size + sizeof(struct heap_block_t) + 16)
|
||||
{
|
||||
//struct heap_block_t* new_block = (struct heap_block_t*)((uintptr_t)curr + sizeof(struct heap_block_t) + size);
|
||||
if (curr->size >= size + sizeof(struct heap_block_t) + 16) {
|
||||
struct heap_block_t* split = (struct heap_block_t*)((uintptr_t)curr + sizeof(struct heap_block_t) + size);
|
||||
|
||||
split->size = curr->size - size - sizeof(struct heap_block_t);
|
||||
@@ -109,10 +103,8 @@ void kfree(void* ptr)
|
||||
|
||||
// merge adjacent free blocks (coalescing)
|
||||
struct heap_block_t* curr = head;
|
||||
while (curr && curr->next)
|
||||
{
|
||||
if (curr->free && curr->next->free)
|
||||
{
|
||||
while (curr && curr->next) {
|
||||
if (curr->free && curr->next->free) {
|
||||
curr->size += sizeof(*curr) + curr->next->size;
|
||||
curr->next = curr->next->next;
|
||||
continue;
|
||||
|
||||
@@ -16,8 +16,7 @@
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
struct heap_block_t
|
||||
{
|
||||
struct heap_block_t {
|
||||
size_t size;
|
||||
bool free; // 1byte
|
||||
uint8_t reserved[7]; // (7+1 = 8 bytes)
|
||||
|
||||
@@ -21,8 +21,7 @@ void* memcpy(void* restrict dest, const void* restrict src, size_t n)
|
||||
uint8_t* restrict pdest = (uint8_t* restrict)dest;
|
||||
const uint8_t* restrict psrc = (const uint8_t* restrict)src;
|
||||
|
||||
for (size_t i=0; i<n; i++)
|
||||
{
|
||||
for (size_t i=0; i<n; i++) {
|
||||
pdest[i] = psrc[i];
|
||||
}
|
||||
|
||||
@@ -33,8 +32,7 @@ void* memset(void* s, int c, size_t n)
|
||||
{
|
||||
uint8_t* p = (uint8_t*)s;
|
||||
|
||||
for (size_t i=0; i<n; i++)
|
||||
{
|
||||
for (size_t i=0; i<n; i++) {
|
||||
p[i] = (uint8_t)c;
|
||||
}
|
||||
|
||||
@@ -46,16 +44,12 @@ void* memmove(void *dest, const void* src, size_t n)
|
||||
uint8_t* pdest = (uint8_t*)dest;
|
||||
const uint8_t* psrc = (uint8_t*)src;
|
||||
|
||||
if (src > dest)
|
||||
{
|
||||
for (size_t i=0; i<n; i++)
|
||||
{
|
||||
if (src > dest) {
|
||||
for (size_t i=0; i<n; i++) {
|
||||
pdest[i] = psrc[i];
|
||||
}
|
||||
} else if (src < dest)
|
||||
{
|
||||
for (size_t i=n; i>0; i--)
|
||||
{
|
||||
} else if (src < dest) {
|
||||
for (size_t i=n; i>0; i--) {
|
||||
pdest[i-1] = psrc[i-1];
|
||||
}
|
||||
}
|
||||
@@ -67,10 +61,8 @@ int memcmp(const void* s1, const void* s2, size_t n)
|
||||
const uint8_t* p1 = (const uint8_t*)s1;
|
||||
const uint8_t* p2 = (const uint8_t*)s2;
|
||||
|
||||
for (size_t i=0; i<n; i++)
|
||||
{
|
||||
if (p1[i] != p2[i])
|
||||
{
|
||||
for (size_t i=0; i<n; i++) {
|
||||
if (p1[i] != p2[i]) {
|
||||
return p1[i] < p2[i] ? -1 : 1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,8 +39,7 @@ static uint64_t* alloc_page_table()
|
||||
{
|
||||
uint64_t* virt = (uint64_t*)PHYS_TO_VIRT(pmm_alloc());
|
||||
|
||||
for (size_t i=0; i<512; i++)
|
||||
{
|
||||
for (size_t i=0; i<512; i++) {
|
||||
virt[i] = 0;
|
||||
}
|
||||
return virt;
|
||||
@@ -70,32 +69,26 @@ void paging_map_page(uint64_t* root_table, uint64_t virt, uint64_t phys, uint64_
|
||||
// PML4
|
||||
// If the entry at index is not present, allocate enough space for it
|
||||
// then populate the entry with correct addr + flags
|
||||
if (!(root_table[pml4_i] & PTE_PRESENT))
|
||||
{
|
||||
if (!(root_table[pml4_i] & PTE_PRESENT)) {
|
||||
pdpt = alloc_page_table();
|
||||
root_table[pml4_i] = VIRT_TO_PHYS(pdpt) | PTE_PRESENT | PTE_WRITABLE;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
pdpt = (uint64_t *)PHYS_TO_VIRT(root_table[pml4_i] & PTE_ADDR_MASK);
|
||||
}
|
||||
|
||||
// PDPT: same here
|
||||
if (!(pdpt[pdpt_i] & PTE_PRESENT))
|
||||
{
|
||||
if (!(pdpt[pdpt_i] & PTE_PRESENT)) {
|
||||
pd = alloc_page_table();
|
||||
pdpt[pdpt_i] = VIRT_TO_PHYS(pd) | PTE_PRESENT | PTE_WRITABLE;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
pd = (uint64_t *)PHYS_TO_VIRT(pdpt[pdpt_i] & PTE_ADDR_MASK);
|
||||
}
|
||||
|
||||
// PD: and here
|
||||
if (!(pd[pd_i] & PTE_PRESENT))
|
||||
{
|
||||
if (!(pd[pd_i] & PTE_PRESENT)) {
|
||||
pt = alloc_page_table();
|
||||
pd[pd_i] = VIRT_TO_PHYS(pt) | PTE_PRESENT | PTE_WRITABLE;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
pt = (uint64_t *)PHYS_TO_VIRT(pd[pd_i] & PTE_ADDR_MASK);
|
||||
}
|
||||
|
||||
@@ -109,9 +102,7 @@ void paging_map_page(uint64_t* root_table, uint64_t virt, uint64_t phys, uint64_
|
||||
uint64_t kernel_phys_base;
|
||||
uint64_t kernel_virt_base;
|
||||
|
||||
extern struct boot_context boot_ctx;
|
||||
|
||||
void paging_init()
|
||||
void paging_init(struct boot_context boot_ctx)
|
||||
{
|
||||
// We should map the kernel, GDT, IDT, stack, framebuffer.
|
||||
// Optionally we could map ACPI tables (we can find them in the Limine memmap)
|
||||
@@ -129,31 +120,25 @@ void paging_init()
|
||||
|
||||
// Find max physical address from limine memmap
|
||||
uint64_t max_phys = 0;
|
||||
for (uint64_t i=0; i<boot_ctx.mmap->entry_count; i++)
|
||||
{
|
||||
for (uint64_t i=0; i<boot_ctx.mmap->entry_count; i++) {
|
||||
struct limine_memmap_entry* entry = boot_ctx.mmap->entries[i];
|
||||
if (entry->length == 0)
|
||||
{
|
||||
if (entry->length == 0) {
|
||||
continue;
|
||||
}
|
||||
uint64_t top = entry->base + entry->length;
|
||||
if (top > max_phys)
|
||||
{
|
||||
if (top > max_phys) {
|
||||
max_phys = top;
|
||||
}
|
||||
}
|
||||
|
||||
// 4GB
|
||||
if (max_phys > 0x100000000)
|
||||
{
|
||||
DEBUG("WARNING: max_phys capped to 4GB (0x100000000) (from max_phys=%p)", max_phys);
|
||||
max_phys = 0x100000000;
|
||||
if (max_phys > PAGING_MAX_PHYS) {
|
||||
DEBUG("WARNING: max_phys capped to 4GB (%x) (from max_phys=%p)", PAGING_MAX_PHYS, max_phys);
|
||||
max_phys = PAGING_MAX_PHYS;
|
||||
}
|
||||
|
||||
// HHDM map up to max_phys or 4GB, whichever is smaller, using given offset
|
||||
for (uint64_t i=0; i<max_phys; i += PAGE_SIZE)
|
||||
{
|
||||
//paging_kmap_page(i+hhdm_off, i, PTE_WRITABLE);
|
||||
// HHDM map up to max_phys or PAGING_MAX_PHYS, whichever is smaller, using given offset
|
||||
for (uint64_t i=0; i<max_phys; i += PAGE_SIZE) {
|
||||
paging_map_page(kernel_pml4, i+hhdm_off, i, PTE_WRITABLE | PTE_PRESENT);
|
||||
page_count++;
|
||||
}
|
||||
@@ -163,9 +148,7 @@ void paging_init()
|
||||
// SOME DAY when we want a safer kernel we should map .text as Read/Exec
|
||||
// .rodata as Read and .data as Read/Write
|
||||
// For now who gives a shit, let's RWX all kernel
|
||||
for (uint64_t i = 0; i < KERNEL_SIZE; i += PAGE_SIZE)
|
||||
{
|
||||
//paging_kmap_page(kernel_virt_base+i, kernel_phys_base+i, PTE_WRITABLE);
|
||||
for (uint64_t i = 0; i < KERNEL_SIZE; i += PAGE_SIZE) {
|
||||
paging_map_page(kernel_pml4, kernel_virt_base+i, kernel_phys_base+i, PTE_WRITABLE);
|
||||
page_count++;
|
||||
}
|
||||
@@ -178,9 +161,7 @@ void paging_init()
|
||||
uint64_t fb_pages = (fb_size + PAGE_SIZE-1)/PAGE_SIZE;
|
||||
|
||||
// Map the framebuffer (with cache-disable & write-through)
|
||||
for (uint64_t i=0; i<fb_pages; i++)
|
||||
{
|
||||
//paging_kmap_page(fb_virt+i*PAGE_SIZE, fb_phys+i*PAGE_SIZE, PTE_WRITABLE | PTE_PCD | PTE_PWT);
|
||||
for (uint64_t i=0; i<fb_pages; i++) {
|
||||
paging_map_page(kernel_pml4, fb_virt+i*PAGE_SIZE, fb_phys+i*PAGE_SIZE, PTE_WRITABLE | PTE_PCD | PTE_PWT);
|
||||
page_count++;
|
||||
}
|
||||
|
||||
@@ -12,8 +12,9 @@
|
||||
#include <stdint.h>
|
||||
#include <limine.h>
|
||||
#include "mem/heap/kheap.h"
|
||||
#include <kernel.h>
|
||||
|
||||
void paging_init();
|
||||
void paging_init(struct boot_context boot_ctx);
|
||||
void paging_map_page(uint64_t* root_table, uint64_t virt, uint64_t phys, uint64_t flags);
|
||||
|
||||
// To swap root page tables
|
||||
|
||||
@@ -38,12 +38,10 @@ static void pmm_find_biggest_usable_region(struct limine_memmap_response* memmap
|
||||
uint64_t offset = hhdm->offset;
|
||||
|
||||
DEBUG("Usable Memory:");
|
||||
for (size_t i=0; i<memmap->entry_count; i++)
|
||||
{
|
||||
for (size_t i=0; i<memmap->entry_count; i++) {
|
||||
struct limine_memmap_entry* entry = memmap->entries[i];
|
||||
|
||||
if (entry->type == LIMINE_MEMMAP_USABLE)
|
||||
{
|
||||
if (entry->type == LIMINE_MEMMAP_USABLE) {
|
||||
DEBUG("0x%p-0x%p mapped at 0x%p-0x%p", entry->base, entry->base+entry->length,
|
||||
entry->base+offset, entry->base+entry->length+offset);
|
||||
if (entry->length > length_max)
|
||||
@@ -66,8 +64,7 @@ static uintptr_t g_freelist = 0;
|
||||
|
||||
uintptr_t pmm_alloc()
|
||||
{
|
||||
if (!g_freelist)
|
||||
{
|
||||
if (!g_freelist) {
|
||||
panic(NULL, "PMM is out of memory!");
|
||||
}
|
||||
uintptr_t addr = g_freelist;
|
||||
@@ -89,19 +86,17 @@ static void pmm_init_freelist()
|
||||
uint64_t end = ALIGN_DOWN(biggest_entry->base + biggest_entry->length, PAGE_SIZE);
|
||||
|
||||
uint64_t page_count=0;
|
||||
for (uint64_t addr = base; addr < end; addr += PAGE_SIZE)
|
||||
{
|
||||
for (uint64_t addr = base; addr < end; addr += PAGE_SIZE) {
|
||||
pmm_free(addr);
|
||||
//DEBUG("page %u lives at phys 0x%p (virt 0x%p)", page_count, addr, PHYS_TO_VIRT(addr));
|
||||
page_count++;
|
||||
}
|
||||
DEBUG("%u frames in freelist, available for use (%u bytes)", page_count, page_count*PAGE_SIZE);
|
||||
}
|
||||
|
||||
void pmm_init(struct limine_memmap_response* memmap, struct limine_hhdm_response* hhdm)
|
||||
void pmm_init(struct boot_context boot_ctx)
|
||||
{
|
||||
hhdm_off = hhdm->offset;
|
||||
pmm_find_biggest_usable_region(memmap, hhdm);
|
||||
hhdm_off = boot_ctx.hhdm->offset;
|
||||
pmm_find_biggest_usable_region(boot_ctx.mmap, boot_ctx.hhdm);
|
||||
|
||||
// Now we have biggest USABLE region,
|
||||
// so to populate the free list we just iterate through it
|
||||
|
||||
@@ -8,8 +8,9 @@
|
||||
#define PAGING_PMM_H
|
||||
|
||||
#include <limine.h>
|
||||
#include <kernel.h>
|
||||
|
||||
void pmm_init(struct limine_memmap_response* memmap, struct limine_hhdm_response* hhdm);
|
||||
void pmm_init(struct boot_context boot_ctx);
|
||||
void pmm_free(uintptr_t addr);
|
||||
uintptr_t pmm_alloc();
|
||||
|
||||
|
||||
@@ -16,8 +16,7 @@ Flags here aren't x86 flags, they are platform-agnostic
|
||||
kernel-defined flags.
|
||||
*/
|
||||
|
||||
struct vm_object
|
||||
{
|
||||
struct vm_object {
|
||||
uintptr_t base;
|
||||
size_t length;
|
||||
size_t flags;
|
||||
|
||||
Reference in New Issue
Block a user