Re-make: kernel heap (malloc, free) via free list alloc + add UEFI emulation doc

This commit is contained in:
xamidev
2024-09-04 21:49:26 +02:00
parent 3b39a0a1f4
commit 4d05e0d620
4 changed files with 79 additions and 35 deletions

View File

@@ -3,6 +3,8 @@
## Table of Contents ## Table of Contents
- Getting Started - Getting Started
- Debugging the kernel
- Emulated booting in UEFI mode
- Writing programs for BlankOS - Writing programs for BlankOS
- Changing the TTY font - Changing the TTY font
@@ -33,6 +35,14 @@ gdb kernel.elf
(gdb) target remote localhost:1234 (gdb) target remote localhost:1234
``` ```
## Emulated booting in UEFI mode (QEMU w/ OVMF)
Install the OVMF firmware package by doing `sudo pacman -S ovmf` or the equivalent for your distro. Then, you can emulate the OS as if it was ran using an UEFI machine:
```
sudo qemu-system-i386 -drive if=pflash,format=raw,readonly=on,file=/usr/share/OVMF/ia32/OVMF_CODE.fd -drive if=pflash,format=raw,file=/usr/share/OVMF/ia32/OVMF_VARS.fd -drive file=blankos.iso,format=raw -m 4098
```
## Writing programs for BlankOS ## Writing programs for BlankOS
Be warned, these are not actual programs in the sense you'd expect. These are indeed functions that are called from the shell, and embedded in the kernel ELF binary. Real programs apart from the kernel are not yet a thing here, but might be one day. Be warned, these are not actual programs in the sense you'd expect. These are indeed functions that are called from the shell, and embedded in the kernel ELF binary. Real programs apart from the kernel are not yet a thing here, but might be one day.

View File

@@ -5,42 +5,60 @@
#include "kheap.h" #include "kheap.h"
#include <stdint.h> #include <stdint.h>
#include "system.h"
extern uint32_t end; // Free list allocator
uint32_t placement_address = (uint32_t)&end;
uint32_t kmalloc_int(uint32_t sz, int align, uint32_t *phys) static uint8_t heap[HEAP_SIZE];
static block_t* free_list = NULL;
void init_alloc()
{ {
if (align == 1 && (placement_address & 0x00000FFF)) free_list = (block_t*)heap;
free_list->size = HEAP_SIZE-sizeof(block_t);
free_list->next = NULL;
}
void* malloc(size_t size)
{
block_t* prev = NULL;
block_t* curr = free_list;
while (curr != NULL)
{ {
placement_address &= 0xFFFFF000; if (curr->size >= size)
placement_address += 0x1000; {
if (curr->size > (size_t)(size + sizeof(block_t)))
{
block_t* new_block = (block_t*)((uint8_t*)curr + sizeof(block_t) + size);
new_block->size = curr->size - size - sizeof(block_t);
new_block->next = curr->next;
curr->size = size;
curr->next = new_block;
}
if (prev == NULL)
{
free_list = curr->next;
} else {
prev->next = curr->next;
}
return (void*)((uint8_t*)curr + sizeof(block_t));
}
prev = curr;
curr = curr->next;
} }
if (phys)
{ return NULL;
*phys = placement_address;
}
uint32_t tmp = placement_address;
placement_address += sz;
return tmp;
} }
uint32_t kmalloc_a(uint32_t sz) void free(void* ptr)
{ {
return kmalloc_int(sz, 1, 0); if (ptr == NULL) return;
}
uint32_t kmalloc_p(uint32_t sz, uint32_t *phys) block_t* block_to_free = (block_t*)((uint8_t*)ptr - sizeof(block_t));
{ block_to_free->next = free_list;
return kmalloc_int(sz, 0, phys); free_list = block_to_free;
}
uint32_t kmalloc_ap(uint32_t sz, uint32_t *phys)
{
return kmalloc_int(sz, 1, phys);
}
uint32_t kmalloc(uint32_t sz)
{
return kmalloc_int(sz, 0, 0);
} }

View File

@@ -7,10 +7,19 @@
#define KHEAP_H #define KHEAP_H
#include <stdint.h> #include <stdint.h>
#include "system.h"
uint32_t kmalloc_a(uint32_t sz); typedef struct block
uint32_t kmalloc_p(uint32_t sz, uint32_t *phys); {
uint32_t kmalloc_ap(uint32_t sz, uint32_t *phys); size_t size;
uint32_t kmalloc(uint32_t sz); struct block* next;
} block_t;
#define HEAP_SIZE 1024*1024 // 1MB malloc-able
void init_alloc();
void* malloc(size_t size);
void free(void* ptr);
#endif #endif

View File

@@ -13,6 +13,7 @@
#include "../drivers/framebuffer.h" #include "../drivers/framebuffer.h"
#include "kmain.h" #include "kmain.h"
#include "multiboot2.h" #include "multiboot2.h"
#include "kheap.h"
void kmain(multiboot2_info *mb_info) void kmain(multiboot2_info *mb_info)
{ {
@@ -66,14 +67,17 @@ void kmain(multiboot2_info *mb_info)
while ((uint8_t*) mmap < tags + mmap_tag->size) while ((uint8_t*) mmap < tags + mmap_tag->size)
{ {
if (mmap->addr != 0) if (mmap->addr != 0)
{ {
/*
printf("[debug] base addr=0x%x%x, length=0x%x%x, type=%u\n", printf("[debug] base addr=0x%x%x, length=0x%x%x, type=%u\n",
(uint32_t) (mmap->addr >> 32), (uint32_t) (mmap->addr >> 32),
(uint32_t) (mmap->addr & 0xFFFFFFFF), (uint32_t) (mmap->addr & 0xFFFFFFFF),
(uint32_t) (mmap->len >> 32), (uint32_t) (mmap->len >> 32),
(uint32_t) (mmap->len & 0xFFFFFFFF), (uint32_t) (mmap->len & 0xFFFFFFFF),
mmap->type); mmap->type);
*/
} }
mmap = (struct multiboot_mmap_entry*) ((uint8_t*)mmap + mmap_tag->entry_size); mmap = (struct multiboot_mmap_entry*) ((uint8_t*)mmap + mmap_tag->entry_size);
@@ -87,7 +91,10 @@ void kmain(multiboot2_info *mb_info)
irq_install(); irq_install();
__asm__ __volatile__("sti"); __asm__ __volatile__("sti");
//test_read_sector(); init_alloc();
void* ptr1 = malloc(256);
void* ptr2 = malloc(512);
free(ptr2);
timer_install(); timer_install();
keyboard_install(); keyboard_install();