Re-make: kernel heap (malloc, free) via free list alloc + add UEFI emulation doc
This commit is contained in:
@@ -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.
|
||||||
|
|||||||
@@ -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);
|
||||||
placement_address &= 0xFFFFF000;
|
free_list->next = NULL;
|
||||||
placement_address += 0x1000;
|
|
||||||
}
|
|
||||||
if (phys)
|
|
||||||
{
|
|
||||||
*phys = placement_address;
|
|
||||||
}
|
|
||||||
uint32_t tmp = placement_address;
|
|
||||||
placement_address += sz;
|
|
||||||
return tmp;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t kmalloc_a(uint32_t sz)
|
void* malloc(size_t size)
|
||||||
{
|
{
|
||||||
return kmalloc_int(sz, 1, 0);
|
block_t* prev = NULL;
|
||||||
|
block_t* curr = free_list;
|
||||||
|
|
||||||
|
while (curr != NULL)
|
||||||
|
{
|
||||||
|
if (curr->size >= size)
|
||||||
|
{
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t kmalloc_p(uint32_t sz, uint32_t *phys)
|
if (prev == NULL)
|
||||||
{
|
{
|
||||||
return kmalloc_int(sz, 0, phys);
|
free_list = curr->next;
|
||||||
|
} else {
|
||||||
|
prev->next = curr->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t kmalloc_ap(uint32_t sz, uint32_t *phys)
|
return (void*)((uint8_t*)curr + sizeof(block_t));
|
||||||
{
|
|
||||||
return kmalloc_int(sz, 1, phys);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t kmalloc(uint32_t sz)
|
prev = curr;
|
||||||
{
|
curr = curr->next;
|
||||||
return kmalloc_int(sz, 0, 0);
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void free(void* ptr)
|
||||||
|
{
|
||||||
|
if (ptr == NULL) return;
|
||||||
|
|
||||||
|
block_t* block_to_free = (block_t*)((uint8_t*)ptr - sizeof(block_t));
|
||||||
|
block_to_free->next = free_list;
|
||||||
|
free_list = block_to_free;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -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();
|
||||||
|
|||||||
Reference in New Issue
Block a user