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
2 changes: 1 addition & 1 deletion cfg/config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set(CMAKE_C_STANDARD_REQUIRED ON)

add_compile_options(
-m32
-march=i386
-march=i486
-O2
-g
-fno-builtin
Expand Down
2 changes: 1 addition & 1 deletion kernel/core/panic.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ struct stackframe {
uint32_t eip;
};

void panic(const char *s) {
_Noreturn void panic(const char *s) {
uint32_t ebp, esp;
__asm__ volatile("mov %%ebp, %0\n"
"mov %%esp, %1\n"
Expand Down
2 changes: 1 addition & 1 deletion kernel/include/core/panic.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#ifndef PANIC_H
#define PANIC_H

void panic(const char *);
_Noreturn void panic(const char *);

#endif
10 changes: 4 additions & 6 deletions kernel/include/memory/map.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#ifndef MEMORY_MAP_H
#define MEMORY_MAP_H

#define MEMORY_MAP_MAX_SIZE 64

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

#define MEMORY_MAP_MAX_SIZE 64

typedef enum { MEMORY_FREE, MEMORY_BUSY } memory_t;

typedef struct {
Expand All @@ -19,11 +19,9 @@ void *get_kernel_end();

void mem_map_init();

void *mem_alloc_size(uint64_t size);

void *mem_alloc(uint64_t *size);
uintptr_t mem_alloc_size(uint64_t size);

void *mem_alloc_largest(uint64_t *size);
uintptr_t mem_alloc(uint64_t *size);

bool mem_has_free();

Expand Down
4 changes: 2 additions & 2 deletions kernel/include/memory/pfa.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

void pfa_init();

uint32_t pf_alloc();
uintptr_t pf_alloc();

void pf_free(uint32_t idx);
void pf_free(uintptr_t addr);

#endif
31 changes: 31 additions & 0 deletions kernel/include/memory/virtual/layout.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef MEMORY_VIRTUAL_LAYOUT_H
#define MEMORY_VIRTUAL_LAYOUT_H

#define MEM_VIRT_LAYOUT_NULL_PAGE_START 0x00000000
#define MEM_VIRT_LAYOUT_NULL_PAGE_END 0x00000FFF

#define MEM_VIRT_LAYOUT_USERLAND_START 0x00001000
#define MEM_VIRT_LAYOUT_USERLAND_END 0xBFFFFFFF

#define MEM_VIRT_LAYOUT_KERNEL_LOW_START 0xC0000000
#define MEM_VIRT_LAYOUT_KERNEL_LOW_END 0xC03FFFFF

#define MEM_VIRT_LAYOUT_KERNEL_HEAP_START 0xC0400000
#define MEM_VIRT_LAYOUT_KERNEL_HEAP_END 0xC13FFFFF

#define MEM_VIRT_LAYOUT_KERNEL_STACK_GUARD_START 0xC1400000
#define MEM_VIRT_LAYOUT_KERNEL_STACK_GUARD_END 0xC1400FFF

#define MEM_VIRT_LAYOUT_KERNEL_STACK_START 0xC1401000
#define MEM_VIRT_LAYOUT_KERNEL_STACK_END 0xC1402FF0

#define MEM_VIRT_LAYOUT_RESERVED_START 0xC1403000
#define MEM_VIRT_LAYOUT_RESERVED_END 0xFFBFFFFF

#define MEM_VIRT_LAYOUT_PAGE_MAPPING_START 0xFFC00000
#define MEM_VIRT_LAYOUT_PAGE_MAPPING_END 0xFFFFFFFF

// It is defined like this to prevent stack corruption
#define mem_virt_layout_setup_stack() __asm__ volatile("mov %0, %%esp" : : "r"(MEM_VIRT_LAYOUT_KERNEL_STACK_END));

#endif
2 changes: 1 addition & 1 deletion kernel/include/memory/virtual/paging.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef PAGING_H
#define PAGING_H

#define PAGING_FIRST_4MIB_MAPPING_ADDR 0xC0000000
void *paging_addr_phys_to_virt(uintptr_t phys);

void paging_init();

Expand Down
72 changes: 40 additions & 32 deletions kernel/init/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,45 @@
#include <memory/map.h>
#include <memory/map/e820.h>
#include <memory/pfa.h>
#include <memory/virtual/layout.h>
#include <memory/virtual/paging.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>

int main(void) {
_Noreturn void _main() {
{
printf("\x1b[33m");
printf("PFA test:\n");
uintptr_t ptrs[100];

printf("Allocating...\n");
for (size_t i = 0; i < sizeof(ptrs) / sizeof(*ptrs); i++) {
ptrs[i] = pf_alloc();
}

printf("Freeing...\n");
for (size_t i = 0; i < sizeof(ptrs) / sizeof(*ptrs); i++) {
pf_free(ptrs[i]);
}

printf("\x1b[0m");
}

printf("Initializing keyboard...\n");
keyboard_init();

printf("\nWelcome to PRosBSD v.%s!\n\n", VERSION_STRING);
printf("\033[34m * Source Code: \033[0mhttps://github.com/prosdev-org/PRosBSD\n\n");

printf("\033[1;32m~$\033[0m ");

for (;;) {
putchar(getchar());
}
}

int main() {
vga_tty_clear();

printf("Initializing GDT...\n");
Expand All @@ -28,7 +61,8 @@ int main(void) {
printf("Memory map provided by BIOS:\n");
for (size_t i = 0; i < size; i++) {
printf("%d) ", i);
printf("0x%x-0x%x ", (uint32_t) e820_map[i].address, (uint32_t) (e820_map[i].address + e820_map[i].length - 1));
printf("0x%x-0x%x ", (uint32_t) e820_map[i].address,
(uint32_t) (e820_map[i].address + e820_map[i].length - 1));
printf("Type: 0x%x, ", e820_map[i].type);
printf("ACPI3 attr: 0x%x", e820_map[i].acpi3_attributes);
printf("\n");
Expand All @@ -48,7 +82,8 @@ int main(void) {
printf("Memory map:\n");
for (size_t i = 0; i < size; i++) {
printf("%d) ", i);
printf("0x%x-0x%x ", (uint32_t) memory_map[i].base, (uint32_t) (memory_map[i].base + memory_map[i].length - 1));
printf("0x%x-0x%x ", (uint32_t) memory_map[i].base,
(uint32_t) (memory_map[i].base + memory_map[i].length - 1));
switch (memory_map[i].type) {
case MEMORY_FREE:
printf(" FREE");
Expand All @@ -64,33 +99,6 @@ int main(void) {
printf("Initializing Paging...\n");
paging_init();

{
printf("\x1b[33m");
printf("PFA test:\n");
uint32_t ptrs[100];

printf("Allocating...\n");
for (size_t i = 0; i < 100; i++) {
ptrs[i] = pf_alloc();
}

printf("Freeing...\n");
for (size_t i = 0; i < 100; i++) {
pf_free(ptrs[i]);
}

printf("\x1b[0m");
}

printf("Initializing keyboard...\n");
keyboard_init();

printf("\nWelcome to PRosBSD v.%s!\n\n", VERSION_STRING);
printf("\033[34m * Source Code: \033[0mhttps://github.com/prosdev-org/PRosBSD\n\n");

printf("\033[1;32m~$\033[0m ");

for (;;) {
putchar(getchar());
}
mem_virt_layout_setup_stack();
_main();
}
22 changes: 11 additions & 11 deletions kernel/memory/map.c
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#include <core/panic.h>
#include <memory/map.h>
#include <memory/map/e820.h>
#include <memory/virtual/paging.h>
#include <memory/virtual/layout.h>
#include <stdint.h>
#include <string.h>

extern uint32_t __kernel_end;

static void *get_kernel_physical_end() {
return (void *) ((size_t) &__kernel_end - PAGING_FIRST_4MIB_MAPPING_ADDR);
return (void *) ((uintptr_t) &__kernel_end - MEM_VIRT_LAYOUT_KERNEL_LOW_START);
}

void *get_kernel_end() {
Expand All @@ -30,7 +30,7 @@ static void append(const memory_block_t *memory_block) {
insert(memory_block, map_size);
}

static void delete(size_t idx) {
static void delete(const size_t idx) {
memmove(&map[idx], &map[idx + 1], (--map_size - idx) * sizeof(memory_block_t));
}

Expand Down Expand Up @@ -78,32 +78,32 @@ void mem_map_init() {
delete (0);
}

static void *mem_alloc_common(const size_t *target) {
static uintptr_t mem_alloc_common(const size_t *target) {
map[*target].type = MEMORY_BUSY;
return (void *) (size_t) map[*target].base;
return (uintptr_t) map[*target].base;
}

void *mem_alloc_size(const uint64_t size) {
uintptr_t mem_alloc_size(const uint64_t size) {
size_t target = 0;
while (map[target].type != MEMORY_FREE || map[target].length < size) // first fit
target++;

void *ptr = mem_alloc_common(&target);
const uintptr_t base = mem_alloc_common(&target);

if (split(map[target].base + size, target))
map[target + 1].type = MEMORY_FREE;

return ptr;
return base;
}

void *mem_alloc(uint64_t *size) {
uintptr_t mem_alloc(uint64_t *size) {
size_t target = 0;
while (map[target].type != MEMORY_FREE)
target++;

void *ptr = mem_alloc_common(&target);
const uintptr_t base = mem_alloc_common(&target);
*size = map[target].length;
return ptr;
return base;
}

bool mem_has_free() {
Expand Down
21 changes: 15 additions & 6 deletions kernel/memory/pfa.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,22 @@ static size_t map_size = 0;
static uint32_t *bitmap; // 0 - free, 1 - busy
static uint32_t last_alloc = 0;

static uintptr_t idx_to_addr(const uint32_t idx) {
return (uintptr_t) idx << 12;
}

static uint32_t addr_to_idx(const uintptr_t addr) {
return (uint32_t) addr >> 12;
}

void pfa_init() {
bitmap = mem_alloc_size(BITMAP_SIZE * sizeof(uint32_t)) + PAGING_FIRST_4MIB_MAPPING_ADDR;
bitmap = paging_addr_phys_to_virt(mem_alloc_size(BITMAP_SIZE * sizeof(uint32_t)));
for (size_t i = 0; i < BITMAP_SIZE; i++)
bitmap[i] = 0;

while (mem_has_free()) {
uint64_t length;
uint64_t base = (size_t) mem_alloc(&length);
uint64_t base = mem_alloc(&length);

if (base > 0xFFFFFFFF)
break;
Expand Down Expand Up @@ -89,20 +97,21 @@ static uint32_t pf_alloc_general(const uint32_t start, const uint32_t end, bool
return 0;
}

uint32_t pf_alloc() {
uintptr_t pf_alloc() {
bool found;
uint32_t idx = pf_alloc_general(last_alloc, BITMAP_SIZE * sizeof(uint32_t), &found);
if (found)
return idx;
return idx_to_addr(idx);

idx = pf_alloc_general(0, last_alloc, &found);
if (found)
return idx;
return idx_to_addr(idx);

panic("PFA: Ran out of page frames");
}

void pf_free(const uint32_t idx) {
void pf_free(const uintptr_t addr) {
const uint32_t idx = addr_to_idx(addr);
if (!is_usable(idx << 12))
panic("PFA: Attempt to free reserved memory");

Expand Down
Loading