Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ $(OS_BIN): $(BOOT_ASM) $(KERNEL_ASM_SRCS) $(KERNEL_C_SRCS) $(LINKER_SCRIPT)
i686-elf-gcc $(CFLAGS) \
-T linker.ld \
-o build/kernel.elf \
build/kernel-linked.o && \
build/kernel-linked.o ; \
\
echo "== Creating raw kernel binary =="; \
i686-elf-objcopy -O binary \
build/kernel.elf \
bin/kernel.bin && \
bin/kernel.bin ; \
\
KERNEL_SIZE=$$(stat -c%s bin/kernel.bin); \
KERNEL_SECTORS=$$(( (KERNEL_SIZE + 511) / 512 )); \
Expand Down
8 changes: 6 additions & 2 deletions includes/mm/heap.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@ typedef struct {
heap_block_t *head;
} heap_t;

extern heap_t *current_heap;

int8_t init_heap(pde_t *directory, heap_t *heap);

void *gmalloc(heap_t *heap, size_t size);
int8_t gfree(heap_t *heap, void *ptr);

// void *kmalloc(size_t size);
// void kfree(void *ptr);
void *malloc(size_t size);
int8_t free(void *ptr);

bool is_allocated(const void *ptr);

#endif
1 change: 1 addition & 0 deletions includes/mm/vmm.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ pte_t *vmm_create_page_table();
int8_t vmm_map_page(pde_t *directory, uint32_t virtual_addr, uint32_t physical_addr,
uint32_t flags);
int8_t vmm_allocate(uint32_t virtual_addr);
int8_t vmm_allocate_range(uint32_t start, uint32_t end);

int8_t vmm_unmap_page(pde_t *directory, uint32_t virtual_addr);

Expand Down
33 changes: 16 additions & 17 deletions src/arch/x86/mm/heap/heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@

#include "panic.h"

#include "stdio.h"
heap_t *current_heap;

int8_t init_heap(pde_t *directory, heap_t *heap) {
printf("cc\n");
if (directory == NULL) {
print("null");
return -1;
}

Expand All @@ -21,19 +19,8 @@ int8_t init_heap(pde_t *directory, heap_t *heap) {
if (end > K_HEAP_MAX)
return -1;

printf("cc\n");

print("\ninit heap\n");
// Map the initial heap pages.
for (uintptr_t addr = start; addr < end; addr += PAGE_SIZE) {
print("a ");
if (vmm_allocate(addr) != 0) {
print("\n not a");
return -1;
}
}

printf("ff\n");
if (vmm_allocate_range((uint32_t)start, (uint32_t)end) != 0)
return -1;

// Initialize heap metadata.
heap->start = start;
Expand Down Expand Up @@ -115,4 +102,16 @@ int8_t gfree(heap_t *heap, void *ptr) {
block->is_free = true;

return 0;
}
}

void *malloc(size_t size) { return gmalloc(current_heap, size); }

int8_t free(void *ptr) { return gfree(current_heap, ptr); }

bool is_allocated(const void *ptr) {
if (ptr == NULL)
return false;

const heap_block_t *block = (const heap_block_t *)ptr - 1;
return !block->is_free;
}
41 changes: 41 additions & 0 deletions src/arch/x86/mm/virtual/vmm.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,47 @@ int8_t vmm_allocate(uint32_t virtual_addr) {
return 0;
}

int8_t vmm_allocate_range(uint32_t start, uint32_t end) {
if (current_directory == NULL || start >= end || (start & (PAGE_SIZE - 1)) != 0 ||
(end & (PAGE_SIZE - 1)) != 0)
return -1;

uint32_t page_count = (end - start) / PAGE_SIZE;

/*
* Check the complete range before allocating frames so a failed request
* cannot leave a partially mapped virtual range.
*/
for (uint32_t i = 0; i < page_count; i++) {
vaddr_t va;
va.raw = start + (i * PAGE_SIZE);

if (vmm_is_mapped(current_directory, va))
return -1;
}

uintptr_t physical_start = pmm_alloc_n_frame((int)page_count);

if (physical_start == 0)
return -1;

for (uint32_t i = 0; i < page_count; i++) {
uint32_t virtual_addr = start + (i * PAGE_SIZE);
uint32_t physical_addr = (uint32_t)(physical_start + (i * PAGE_SIZE));

if (vmm_map_page(current_directory, virtual_addr, physical_addr,
VMM_PRESENT | VMM_WRITABLE) != 0) {
for (uint32_t rollback = 0; rollback < i; rollback++)
vmm_unmap_page(current_directory, start + (rollback * PAGE_SIZE));

pmm_free_n_frame(physical_start, (int)page_count);
return -1;
}
}

return 0;
}

int8_t vmm_unmap_page(pde_t *directory, uint32_t virtual_addr) {
if (directory == NULL)
return -1;
Expand Down
36 changes: 6 additions & 30 deletions src/kernel/kernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,42 +69,18 @@ void kernel_main(boot_info_t *boot_info) {
pmm_reserve((uintptr_t)&kernel_start_addr, (uintptr_t)&kernel_end_addr);
KERNEL_INIT_P("Reserved kernel memory");

// uintptr_t ptr = pmm_alloc_n_frame(100);
// printf("%x\n", ptr);

// bool c = pmm_check_mm(ptr);

// printf("%b\n", c);

// tty_t tty0;
// tty_init(&tty0);

KERNEL_INIT("Virtual Memory & Paging ", vmm_init);

vmm_allocate(0x3e000);
// vmm_map_page(current_directory,0x3e000,0x3e000,VMM_PRESENT | VMM_WRITABLE);

static volatile int *a = (volatile int *)0x3e000;

*a = 5;
int b = *a;
printf("a %d", b);

// int8_t err = init_heap(current_directory, &k_heap);
// if (err == -1)
// panic("heap init error");

// KERNEL_INIT_P("Kernal Heap");

// void *b = gmalloc(&k_heap, sizeof(int) * 5);
// b = 5;
current_heap = &k_heap;
init_heap(current_directory, current_heap);

// static volatile int *a = (volatile int *)0xfffffff;
KERNEL_INIT_P("Kernal Heap");

// *a = 4;
tty_t tty0;
tty_init(&tty0);

while (1) {
// tty_process_events(&tty0);
tty_process_events(&tty0);
}

print("[\033[02:00mready\033[0m] Kernel initialized successfully!\n");
Expand Down
Loading