aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarvin Borner2019-10-25 23:26:29 +0200
committerMarvin Borner2019-10-25 23:26:29 +0200
commit5a4db78897063e5b09a676b02fec3ba4a2965921 (patch)
treeba9e1bf1ebc31ba3b541fd3936655c7a70e8ef61
parentd97071a061df629fa8ee7a29cc7c0b23ac304506 (diff)
Rewrite of paging and alloc functions
Still not working though :/
-rw-r--r--Makefile4
-rw-r--r--src/kernel/boot.asm15
-rw-r--r--src/kernel/graphics/vesa.c65
-rw-r--r--src/kernel/graphics/vesa.h11
-rw-r--r--src/kernel/interact.asm1
-rw-r--r--src/kernel/kernel.c2
-rw-r--r--src/kernel/lib/alloc.c446
-rw-r--r--src/kernel/lib/alloc.h22
-rw-r--r--src/kernel/paging/kheap.c295
-rw-r--r--src/kernel/paging/kheap.h108
-rw-r--r--src/kernel/paging/ordered_array.c66
-rw-r--r--src/kernel/paging/ordered_array.h66
-rw-r--r--src/kernel/paging/paging.c274
-rw-r--r--src/kernel/paging/paging.h120
14 files changed, 683 insertions, 812 deletions
diff --git a/Makefile b/Makefile
index 3606e2a..f843795 100644
--- a/Makefile
+++ b/Makefile
@@ -23,11 +23,11 @@ build: clean
stripped=$$(echo "$${line}" | sed -r 's/\//_/g'); \
stripped=$${stripped#??????}; \
stripped=$${stripped%%?}o; \
- i686-elf-gcc -c ./"$${line}" -o ./build/"$${stripped}" -std=gnu99 -ffreestanding -O2 -Wall -Wextra -Wno-unused-parameter || exit; \
+ i686-elf-gcc -c ./"$${line}" -o ./build/"$${stripped}" -std=gnu99 -ffreestanding -O3 -Wall -Wextra -Wno-unused-parameter || exit; \
done <./build/tmp; \
rm ./build/tmp; \
- i686-elf-gcc -T ./src/kernel/linker.ld -o ./build/melvix.bin -ffreestanding -O2 -nostdlib ./build/*.o -lgcc || exit; \
+ i686-elf-gcc -T ./src/kernel/linker.ld -o ./build/melvix.bin -std=gnu99 -ffreestanding -O2 -nostdlib ./build/*.o || exit; \
# Testing
if grub-file --is-x86-multiboot ./build/melvix.bin; then \
diff --git a/src/kernel/boot.asm b/src/kernel/boot.asm
index b628018..e3d469c 100644
--- a/src/kernel/boot.asm
+++ b/src/kernel/boot.asm
@@ -41,21 +41,6 @@ stublet:
call kernel_main
jmp $
-[global copy_page_physical]
-copy_page_physical:
- push ebx
- pushf
- cli
- mov ebx, [esp+12]
- mov ecx, [esp+16]
-
- ; Disable paging
- mov edx, cr0
- and edx, 0x7fffffff
- mov cr0, edx
-
- mov edx, 0x400
-
%include "src/kernel/gdt/gdt.asm"
%include "src/kernel/interrupts/idt.asm"
diff --git a/src/kernel/graphics/vesa.c b/src/kernel/graphics/vesa.c
index 8f7a609..22228de 100644
--- a/src/kernel/graphics/vesa.c
+++ b/src/kernel/graphics/vesa.c
@@ -2,20 +2,18 @@
#include "font.h"
#include "../io/io.h"
#include "../lib/lib.h"
-#include "../paging/kheap.h"
#include "../paging/paging.h"
#include "../system.h"
-
-extern page_directory_t *kernel_directory;
+#include "../lib/alloc.h"
void switch_to_vga() {
serial_write("Force switch to VGA!\n");
vesa_available = 0;
regs16_t regs;
regs.ax = 0x0003;
- disable_paging();
+ paging_disable();
int32(0x10, &regs);
- enable_paging();
+ paging_enable();
uint16_t *terminal_buffer = (uint16_t *) 0xB8000;
char *error = "This computer has no supported video drivers!";
@@ -32,9 +30,9 @@ struct edid_data get_edid() {
regs.bx = 0x01; // BL
regs.es = get_segment(edid);
regs.di = get_offset(edid);
- disable_paging();
+ paging_disable();
int32(0x10, &regs);
- enable_paging();
+ paging_enable();
kfree(edid);
@@ -47,9 +45,9 @@ void vbe_set_mode(unsigned short mode) {
regs.ax = 0x4F02;
regs.bx = mode;
regs.bx |= 0x4000;
- disable_paging();
+ paging_disable();
int32(0x10, &regs);
- enable_paging();
+ paging_enable();
if (regs.ax != 0x004F) switch_to_vga();
else vesa_available = 1;
@@ -63,11 +61,11 @@ uint16_t *vbe_get_modes() {
regs16_t regs;
regs.ax = 0x4F00;
- regs.es = get_segment(info_address);
- regs.di = get_offset(info_address);
- disable_paging();
+ regs.es = 0;
+ regs.di = 0x7E00;
+ paging_disable();
int32(0x10, &regs);
- enable_paging();
+ paging_enable();
struct vbe_info *info = (struct vbe_info *) info_address;
@@ -77,28 +75,33 @@ uint16_t *vbe_get_modes() {
}
// Get number of modes
- uint16_t *mode_ptr = get_ptr(info->video_modes);
+ uint16_t *mode_ptr = (uint16_t *) info->video_modes;
int number_modes = 1;
for (uint16_t *p = mode_ptr; *p != 0xFFFF; p++) number_modes++;
- uint16_t *video_modes = (uint16_t *) kmalloc(sizeof(uint16_t) * number_modes);
+ uint16_t *video_modes = kmalloc(sizeof(uint16_t) * number_modes);
for (int i = 0; i < number_modes; i++)
- video_modes[i] = mode_ptr[i];
+ video_modes[i] = mode_ptr[i]; // THIS FAILS
+
+ for (int i = 0; i < 47; i++) {
+ serial_write_hex(video_modes[i]);
+ serial_write("\n");
+ }
return video_modes;
}
struct vbe_mode_info *vbe_get_mode_info(uint16_t mode) {
- struct vbe_mode_info_all *mode_info = (struct vbe_mode_info_all *) 0x7E00;
-
regs16_t regs;
regs.ax = 0x4F01;
regs.cx = mode;
- regs.es = get_segment(mode_info);
- regs.di = get_offset(mode_info);
- disable_paging();
+ regs.es = 0;
+ regs.di = 0x7E00;
+ paging_disable();
int32(0x10, &regs);
- enable_paging();
+ paging_enable();
+
+ struct vbe_mode_info_all *mode_info = (struct vbe_mode_info_all *) 0x7E00;
struct vbe_mode_info *mode_info_final = (struct vbe_mode_info *) kmalloc(sizeof(struct vbe_mode_info));
mode_info_final->attributes = mode_info->attributes;
@@ -115,10 +118,17 @@ struct vbe_mode_info *vbe_get_mode_info(uint16_t mode) {
void set_optimal_resolution() {
uint16_t *video_modes = vbe_get_modes();
+ serial_write("\n\n");
+ for (int i = 0; i < 47; i++) {
+ serial_write_hex(video_modes[i]);
+ serial_write("\n");
+ }
+
uint16_t highest = 0;
for (uint16_t *mode = video_modes; *mode != 0xFFFF; mode++) {
struct vbe_mode_info *mode_info = vbe_get_mode_info(*mode);
+ serial_write_dec(mode_info->width);
if ((mode_info->attributes & 0x90) != 0x90 || !mode_info->success ||
(mode_info->memory_model != 4 && mode_info->memory_model != 6))
@@ -156,6 +166,7 @@ void set_optimal_resolution() {
vbe_pitch = mode_info->pitch;
vbe_bpp = mode_info->bpp >> 3;
fb = (unsigned char *) mode_info->framebuffer;
+ kfree(mode_info);
}
serial_write("Using mode: (");
@@ -168,13 +179,11 @@ void set_optimal_resolution() {
serial_write_dec(vbe_bpp << 3);
serial_write("\n");
- uint32_t fb_size = vbe_width * vbe_height * vbe_bpp;
- for (uint32_t z = 0; z <= fb_size; z += 4096)
- alloc_frame(get_page((uint32_t) fb + z, 1, kernel_directory), 1, 1);
+ uint32_t fb_psize = vbe_width * vbe_height * vbe_bpp;
+ for (uint32_t z = 0; z < fb_psize; z += 4096)
+ paging_map((uint32_t) fb + z, (uint32_t) fb + z, PT_PRESENT | PT_RW | PT_USED);
vbe_set_mode(highest);
-
- disable_paging();
}
uint16_t terminal_x = 1;
@@ -204,7 +213,7 @@ void vesa_draw_rectangle(int x1, int y1, int x2, int y2, int color) {
char green = (color >> 8) & 255;
char red = (color >> 16) & 255;
int pos1 = x1 * vbe_bpp + y1 * vbe_pitch;
- char *draw = &fb[pos1];
+ char *draw = (char *) &fb[pos1];
for (i = 0; i <= y2 - y1; i++) {
for (j = 0; j <= x2 - x1; j++) {
draw[vbe_bpp * j] = blue;
diff --git a/src/kernel/graphics/vesa.h b/src/kernel/graphics/vesa.h
index 0a8bd60..9ebc435 100644
--- a/src/kernel/graphics/vesa.h
+++ b/src/kernel/graphics/vesa.h
@@ -39,12 +39,12 @@ struct edid_data {
*/
struct vbe_info {
char signature[4];
- uint32_t version;
- struct far_ptr oem;
+ uint16_t version;
+ uint32_t oem;
uint32_t capabilities;
- struct far_ptr video_modes;
- uint32_t video_memory;
- uint32_t software_rev;
+ uint32_t video_modes;
+ uint16_t video_memory;
+ uint16_t software_rev;
uint32_t vendor;
uint32_t product_name;
uint32_t product_rev;
@@ -115,7 +115,6 @@ struct vbe_mode_info {
*/
struct edid_data get_edid();
-
/**
* Forces switch to VGA, displays an error and halts the CPU
*/
diff --git a/src/kernel/interact.asm b/src/kernel/interact.asm
index 42b8a6b..d24c79b 100644
--- a/src/kernel/interact.asm
+++ b/src/kernel/interact.asm
@@ -29,7 +29,6 @@ endstruc
%define DATA16 GDTENTRY(4) ; 0x20
%define STACK16 (INT32_BASE - regs16_t_size)
-
section .text
int32: use32 ; by Napalm
_int32:
diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c
index e064f4f..494be1f 100644
--- a/src/kernel/kernel.c
+++ b/src/kernel/kernel.c
@@ -9,8 +9,8 @@
void init() {
timer_install();
gdt_install();
+ paging_install();
keyboard_install();
- initialise_paging();
idt_install();
isrs_install();
irq_install();
diff --git a/src/kernel/lib/alloc.c b/src/kernel/lib/alloc.c
new file mode 100644
index 0000000..2545d28
--- /dev/null
+++ b/src/kernel/lib/alloc.c
@@ -0,0 +1,446 @@
+#include <stddef.h>
+#include <stdint.h>
+#include "alloc.h"
+#include "../paging/paging.h"
+
+int liballoc_lock() {
+ asm volatile ("cli");
+ return 0;
+}
+
+int liballoc_unlock() {
+ asm volatile ("sti");
+ return 0;
+}
+
+void *liballoc_alloc(size_t p) {
+ uint32_t ptr = paging_alloc_pages((uint32_t) p);
+ return (void *) ptr;
+}
+
+int liballoc_free(void *ptr, size_t p) {
+ paging_set_free((uint32_t) ptr, (uint32_t) p);
+ return 0;
+}
+
+#define ALIGNMENT 16ul
+#define ALIGN_TYPE char
+#define ALIGN_INFO sizeof(ALIGN_TYPE) * 16
+#define USE_CASE1
+#define USE_CASE2
+#define USE_CASE3
+#define USE_CASE4
+#define USE_CASE5
+
+#define ALIGN(ptr) \
+ if ( ALIGNMENT > 1 ) { \
+ uintptr_t diff; \
+ ptr = (void*) ((uintptr_t) ptr + ALIGN_INFO); \
+ diff = (uintptr_t) ptr & (ALIGNMENT - 1); \
+ if (diff != 0) { \
+ diff = ALIGNMENT - diff; \
+ ptr = (void*) ((uintptr_t) ptr + diff); \
+ } \
+ *((ALIGN_TYPE*) ((uintptr_t) ptr - ALIGN_INFO)) = diff + ALIGN_INFO; \
+ }
+
+#define UNALIGN(ptr) \
+ if (ALIGNMENT > 1) { \
+ uintptr_t diff = *((ALIGN_TYPE*) ((uintptr_t) ptr - ALIGN_INFO)); \
+ if (diff < (ALIGNMENT + ALIGN_INFO)) { \
+ ptr = (void*) ((uintptr_t) ptr - diff); \
+ } \
+ }
+
+#define LIBALLOC_MAGIC 0xc001c0de
+#define LIBALLOC_DEAD 0xdeaddead
+
+struct liballoc_major {
+ struct liballoc_major *prev;
+ struct liballoc_major *next;
+ unsigned int pages;
+ unsigned int size;
+ unsigned int usage;
+ struct liballoc_minor *first;
+};
+
+struct liballoc_minor {
+ struct liballoc_minor *prev;
+ struct liballoc_minor *next;
+ struct liballoc_major *block;
+ unsigned int magic;
+ unsigned int size;
+ unsigned int req_size;
+};
+
+static struct liballoc_major *l_memRoot = NULL;
+static struct liballoc_major *l_bestBet = NULL;
+
+static unsigned int l_pageSize = 4096;
+static unsigned int l_pageCount = 16;
+static unsigned long long l_allocated = 0;
+static unsigned long long l_inuse = 0;
+
+static long long l_warningCount = 0;
+static long long l_errorCount = 0;
+static long long l_possibleOverruns = 0;
+
+static void *liballoc_memset(void *s, int c, size_t n) {
+ unsigned int i;
+ for (i = 0; i < n; i++)
+ ((char *) s)[i] = c;
+
+ return s;
+}
+
+static void *liballoc_memcpy(void *s1, const void *s2, size_t n) {
+ char *cdest;
+ char *csrc;
+ unsigned int *ldest = (unsigned int *) s1;
+ unsigned int *lsrc = (unsigned int *) s2;
+
+ while (n >= sizeof(unsigned int)) {
+ *ldest++ = *lsrc++;
+ n -= sizeof(unsigned int);
+ }
+
+ cdest = (char *) ldest;
+ csrc = (char *) lsrc;
+
+ while (n > 0) {
+ *cdest++ = *csrc++;
+ n -= 1;
+ }
+ return s1;
+}
+
+static struct liballoc_major *allocate_new_page(unsigned int size) {
+ unsigned int st;
+ struct liballoc_major *maj;
+
+ st = size + sizeof(struct liballoc_major);
+ st += sizeof(struct liballoc_minor);
+
+ if ((st % l_pageSize) == 0)
+ st = st / (l_pageSize);
+ else
+ st = st / (l_pageSize) + 1;
+
+ if (st < l_pageCount) st = l_pageCount;
+
+ maj = (struct liballoc_major *) liballoc_alloc(st);
+
+ if (maj == NULL) {
+ l_warningCount += 1;
+ return NULL;
+ }
+
+ maj->prev = NULL;
+ maj->next = NULL;
+ maj->pages = st;
+ maj->size = st * l_pageSize;
+ maj->usage = sizeof(struct liballoc_major);
+ maj->first = NULL;
+
+ l_allocated += maj->size;
+
+ return maj;
+}
+
+void *PREFIX(malloc)(size_t req_size) {
+ int startedBet = 0;
+ unsigned long long bestSize = 0;
+ void *p = NULL;
+ uintptr_t diff;
+ struct liballoc_major *maj;
+ struct liballoc_minor *min;
+ struct liballoc_minor *new_min;
+ unsigned long size = req_size;
+
+ if (ALIGNMENT > 1) {
+ size += ALIGNMENT + ALIGN_INFO;
+ }
+
+ liballoc_lock();
+
+ if (size == 0) {
+ l_warningCount += 1;
+ liballoc_unlock();
+ return PREFIX(malloc)(1);
+ }
+
+ if (l_memRoot == NULL) {
+ l_memRoot = allocate_new_page(size);
+ if (l_memRoot == NULL) {
+ liballoc_unlock();
+ return NULL;
+ }
+ }
+
+ maj = l_memRoot;
+ startedBet = 0;
+
+ if (l_bestBet != NULL) {
+ bestSize = l_bestBet->size - l_bestBet->usage;
+
+ if (bestSize > (size + sizeof(struct liballoc_minor))) {
+ maj = l_bestBet;
+ startedBet = 1;
+ }
+ }
+
+ while (maj != NULL) {
+ diff = maj->size - maj->usage;
+ if (bestSize < diff) {
+ l_bestBet = maj;
+ bestSize = diff;
+ }
+
+#ifdef USE_CASE1
+ if (diff < (size + sizeof(struct liballoc_minor))) {
+ if (maj->next != NULL) {
+ maj = maj->next;
+ continue;
+ }
+
+ if (startedBet == 1) {
+ maj = l_memRoot;
+ startedBet = 0;
+ continue;
+ }
+
+ maj->next = allocate_new_page(size);
+ if (maj->next == NULL) break;
+ maj->next->prev = maj;
+ maj = maj->next;
+ }
+#endif
+
+#ifdef USE_CASE2
+ if (maj->first == NULL) {
+ maj->first = (struct liballoc_minor *) ((uintptr_t) maj + sizeof(struct liballoc_major));
+
+ maj->first->magic = LIBALLOC_MAGIC;
+ maj->first->prev = NULL;
+ maj->first->next = NULL;
+ maj->first->block = maj;
+ maj->first->size = size;
+ maj->first->req_size = req_size;
+ maj->usage += size + sizeof(struct liballoc_minor);
+ l_inuse += size;
+ p = (void *) ((uintptr_t) (maj->first) + sizeof(struct liballoc_minor));
+ ALIGN(p);
+ liballoc_unlock();
+ return p;
+ }
+#endif
+
+#ifdef USE_CASE3
+ diff = (uintptr_t) (maj->first);
+ diff -= (uintptr_t) maj;
+ diff -= sizeof(struct liballoc_major);
+
+ if (diff >= (size + sizeof(struct liballoc_minor))) {
+ maj->first->prev = (struct liballoc_minor *) ((uintptr_t) maj + sizeof(struct liballoc_major));
+ maj->first->prev->next = maj->first;
+ maj->first = maj->first->prev;
+ maj->first->magic = LIBALLOC_MAGIC;
+ maj->first->prev = NULL;
+ maj->first->block = maj;
+ maj->first->size = size;
+ maj->first->req_size = req_size;
+ maj->usage += size + sizeof(struct liballoc_minor);
+ l_inuse += size;
+ p = (void *) ((uintptr_t) (maj->first) + sizeof(struct liballoc_minor));
+ ALIGN(p);
+ liballoc_unlock();
+ return p;
+ }
+#endif
+
+#ifdef USE_CASE4
+ min = maj->first;
+
+ while (min != NULL) {
+ if (min->next == NULL) {
+ diff = (uintptr_t) (maj) + maj->size;
+ diff -= (uintptr_t) min;
+ diff -= sizeof(struct liballoc_minor);
+ diff -= min->size;
+ if (diff >= (size + sizeof(struct liballoc_minor))) {
+ min->next = (struct liballoc_minor *) ((uintptr_t) min + sizeof(struct liballoc_minor) + min->size);
+ min->next->prev = min;
+ min = min->next;
+ min->next = NULL;
+ min->magic = LIBALLOC_MAGIC;
+ min->block = maj;
+ min->size = size;
+ min->req_size = req_size;
+ maj->usage += size + sizeof(struct liballoc_minor);
+ l_inuse += size;
+ p = (void *) ((uintptr_t) min + sizeof(struct liballoc_minor));
+ ALIGN(p);
+ liballoc_unlock();
+ return p;
+ }
+ }
+
+ if (min->next != NULL) {
+ diff = (uintptr_t) (min->next);
+ diff -= (uintptr_t) min;
+ diff -= sizeof(struct liballoc_minor);
+ diff -= min->size;
+
+ if (diff >= (size + sizeof(struct liballoc_minor))) {
+ new_min = (struct liballoc_minor *) ((uintptr_t) min + sizeof(struct liballoc_minor) + min->size);
+ new_min->magic = LIBALLOC_MAGIC;
+ new_min->next = min->next;
+ new_min->prev = min;
+ new_min->size = size;
+ new_min->req_size = req_size;
+ new_min->block = maj;
+ min->next->prev = new_min;
+ min->next = new_min;
+ maj->usage += size + sizeof(struct liballoc_minor);
+ l_inuse += size;
+ p = (void *) ((uintptr_t) new_min + sizeof(struct liballoc_minor));
+ ALIGN(p);
+ liballoc_unlock();
+ return p;
+ }
+ }
+
+ min = min->next;
+ }
+#endif
+
+#ifdef USE_CASE5
+ if (maj->next == NULL) {
+ if (startedBet == 1) {
+ maj = l_memRoot;
+ startedBet = 0;
+ continue;
+ }
+ maj->next = allocate_new_page(size);
+ if (maj->next == NULL) break;
+ maj->next->prev = maj;
+ }
+#endif
+ maj = maj->next;
+ }
+
+ liballoc_unlock();
+
+ return NULL;
+}
+
+void PREFIX(free)(void *ptr) {
+ struct liballoc_minor *min;
+ struct liballoc_major *maj;
+
+ if (ptr == NULL) {
+ l_warningCount += 1;
+ return;
+ }
+
+ UNALIGN(ptr);
+ liballoc_lock();
+
+ min = (struct liballoc_minor *) ((uintptr_t) ptr - sizeof(struct liballoc_minor));
+
+ if (min->magic != LIBALLOC_MAGIC) {
+ l_errorCount += 1;
+
+ if (((min->magic & 0xFFFFFF) == (LIBALLOC_MAGIC & 0xFFFFFF)) ||
+ ((min->magic & 0xFFFF) == (LIBALLOC_MAGIC & 0xFFFF)) ||
+ ((min->magic & 0xFF) == (LIBALLOC_MAGIC & 0xFF))) {
+ l_possibleOverruns += 1;
+ }
+
+ liballoc_unlock();
+ return;
+ }
+
+ maj = min->block;
+ l_inuse -= min->size;
+ maj->usage -= (min->size + sizeof(struct liballoc_minor));
+ min->magic = LIBALLOC_DEAD;
+
+ if (min->next != NULL) min->next->prev = min->prev;
+ if (min->prev != NULL) min->prev->next = min->next;
+ if (min->prev == NULL) maj->first = min->next;
+ if (maj->first == NULL) {
+ if (l_memRoot == maj) l_memRoot = maj->next;
+ if (l_bestBet == maj) l_bestBet = NULL;
+ if (maj->prev != NULL) maj->prev->next = maj->next;
+ if (maj->next != NULL) maj->next->prev = maj->prev;
+ l_allocated -= maj->size;
+ liballoc_free(maj, maj->pages);
+ } else {
+ if (l_bestBet != NULL) {
+ int bestSize = l_bestBet->size - l_bestBet->usage;
+ int majSize = maj->size - maj->usage;
+ if (majSize > bestSize) l_bestBet = maj;
+ }
+ }
+ liballoc_unlock();
+}
+
+void *PREFIX(calloc)(size_t nobj, size_t size) {
+ int real_size;
+ void *p;
+
+ real_size = nobj * size;
+
+ p = PREFIX(malloc)(real_size);
+
+ liballoc_memset(p, 0, real_size);
+
+ return p;
+}
+
+void *PREFIX(realloc)(void *p, size_t size) {
+ void *ptr;
+ struct liballoc_minor *min;
+ unsigned int real_size;
+
+ if (size == 0) {
+ PREFIX(free)(p);
+ return NULL;
+ }
+
+ if (p == NULL) return PREFIX(malloc)(size);
+
+ ptr = p;
+ UNALIGN(ptr);
+ liballoc_lock();
+ min = (struct liballoc_minor *) ((uintptr_t) ptr - sizeof(struct liballoc_minor));
+
+ if (min->magic != LIBALLOC_MAGIC) {
+ l_errorCount += 1;
+ if (((min->magic & 0xFFFFFF) == (LIBALLOC_MAGIC & 0xFFFFFF)) ||
+ ((min->magic & 0xFFFF) == (LIBALLOC_MAGIC & 0xFFFF)) ||
+ ((min->magic & 0xFF) == (LIBALLOC_MAGIC & 0xFF))) {
+ l_possibleOverruns += 1;
+ }
+
+ liballoc_unlock();
+ return NULL;
+ }
+
+ real_size = min->req_size;
+
+ if (real_size >= size) {
+ min->req_size = size;
+ liballoc_unlock();
+ return p;
+ }
+
+ liballoc_unlock();
+
+ ptr = PREFIX(malloc)(size);
+ liballoc_memcpy(ptr, p, real_size);
+ PREFIX(free)(p);
+
+ return ptr;
+}
diff --git a/src/kernel/lib/alloc.h b/src/kernel/lib/alloc.h
new file mode 100644
index 0000000..5658da7
--- /dev/null
+++ b/src/kernel/lib/alloc.h
@@ -0,0 +1,22 @@
+#ifndef MELVIX_ALLOC_H
+#define MELVIX_ALLOC_H
+
+#define PREFIX(func) k ## func
+
+int liballoc_lock();
+
+int liballoc_unlock();
+
+void *liballoc_alloc(size_t);
+
+int liballoc_free(void *, size_t);
+
+void *PREFIX(malloc)(size_t);
+
+void *PREFIX(realloc)(void *, size_t);
+
+void *PREFIX(calloc)(size_t, size_t);
+
+void PREFIX(free)(void *);
+
+#endif
diff --git a/src/kernel/paging/kheap.c b/src/kernel/paging/kheap.c
deleted file mode 100644
index 9feae77..0000000
--- a/src/kernel/paging/kheap.c
+++ /dev/null
@@ -1,295 +0,0 @@
-#include <stdint.h>
-#include "kheap.h"
-#include "paging.h"
-#include "ordered_array.h"
-#include "../system.h"
-
-extern uint32_t end;
-extern page_directory_t *kernel_directory;
-uint32_t placement_address = (uint32_t) &end;
-heap_t *kheap = 0;
-
-uint32_t kmalloc_int(uint32_t sz, int align, uint32_t *phys) {
- if (kheap != 0) {
- void *addr = alloc(sz, (unsigned char) align, kheap);
- if (phys != 0) {
- page_t *page = get_page((uint32_t) addr, 0, kernel_directory);
- *phys = page->frame * 0x1000 + ((uint32_t) addr & 0xFFF);
- }
- return (uint32_t) addr;
- } else {
- if (align == 1 && (placement_address & 0xFFFFF000)) {
- placement_address &= 0xFFFFF000;
- placement_address += 0x1000;
- }
- if (phys)
- *phys = placement_address;
- uint32_t tmp = placement_address;
- placement_address += sz;
- return tmp;
- }
-}
-
-void kfree(void *p) {
- free(p, kheap);
-}
-
-uint32_t kmalloc_a(uint32_t sz) {
- return kmalloc_int(sz, 1, 0);
-}
-
-uint32_t kmalloc_p(uint32_t sz, uint32_t *phys) {
- return kmalloc_int(sz, 0, phys);
-}
-
-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);
-}
-
-static void expand(uint32_t new_size, heap_t *heap) {
- assert(new_size > heap->end_address - heap->start_address);
-
- if ((new_size & 0xFFFFF000) != 0) {
- new_size &= 0xFFFFF000;
- new_size += 0x1000;
- }
-
- assert(heap->start_address + new_size <= heap->max_address);
-
- uint32_t old_size = heap->end_address - heap->start_address;
- uint32_t i = old_size;
- while (i < new_size) {
- alloc_frame(get_page(heap->start_address + i, 1, kernel_directory),
- (heap->supervisor) ? 1 : 0, (heap->readonly) ? 0 : 1);
- i += 0x1000;
- }
- heap->end_address = heap->start_address + new_size;
-}
-
-static uint32_t contract(uint32_t new_size, heap_t *heap) {
- assert(new_size < heap->end_address - heap->start_address);
-
- if (new_size & 0x1000) {
- new_size &= 0x1000;
- new_size += 0x1000;
- }
-
- if (new_size < HEAP_MIN_SIZE)
- new_size = HEAP_MIN_SIZE;
-
- uint32_t old_size = heap->end_address - heap->start_address;
- uint32_t i = old_size - 0x1000;
- while (new_size < i) {
- free_frame(get_page(heap->start_address + i, 0, kernel_directory));
- i -= 0x1000;
- }
- heap->end_address = heap->start_address + new_size;
- return new_size;
-}
-
-static int find_smallest_hole(uint32_t size, unsigned char page_align, heap_t *heap) {
- uint32_t iterator = 0;
- while (iterator < heap->index.size) {
- header_t *header = (header_t *) lookup_ordered_array(iterator, &heap->index);
- if (page_align > 0) {
- uint32_t location = (uint32_t) header;
- int offset = 0;
- if (((location + sizeof(header_t)) & 0xFFFFF000) != 0)
- offset = 0x1000 - (location + sizeof(header_t)) % 0x1000;
- int hole_size = (int) header->size - offset;
-
- if (hole_size >= (int) size)
- break;
- } else if (header->size >= size)
- break;
- iterator++;
- }
-
- if (iterator == heap->index.size)
- return -1;
- else
- return iterator;
-}
-
-static char header_t_less_than(void *a, void *b) {
- return (((header_t *) a)->size < ((header_t *) b)->size) ? 1 : 0;
-}
-
-heap_t *create_heap(uint32_t start, uint32_t end_addr, uint32_t max, unsigned char supervisor, unsigned char readonly) {
- heap_t *heap = (heap_t *) kmalloc(sizeof(heap_t));
- assert(start % 0x1000 == 0);
- assert(end_addr % 0x1000 == 0);
- heap->index = place_ordered_array((void *) start, HEAP_INDEX_SIZE, &header_t_less_than);
-
- start += sizeof(type_t) * HEAP_INDEX_SIZE;
- if ((start & 0xFFFFF000) != 0) {
- start &= 0xFFFFF000;
- start += 0x1000;
- }
-
- heap->start_address = start;
- heap->end_address = end_addr;
- heap->max_address = max;
- heap->supervisor = supervisor;
- heap->readonly = readonly;
-
- header_t *hole = (header_t *) start;
- hole->size = end_addr - start;
- hole->magic = HEAP_MAGIC;
- hole->is_hole = 1;
- insert_ordered_array((void *) hole, &heap->index);
- return heap;
-}
-
-void *alloc(uint32_t size, unsigned char page_align, heap_t *heap) {
- uint32_t new_size = size + sizeof(header_t) + sizeof(footer_t);
-
- int iterator = find_smallest_hole(new_size, page_align, heap);
- if (iterator == -1) {
- uint32_t old_length = heap->end_address - heap->start_address;
- uint32_t old_end_address = heap->end_address;
- expand(old_length + new_size, heap);
- uint32_t new_length = heap->end_address - heap->start_address;
- iterator = 0;
-
- uint32_t idx = -1;
- uint32_t value = 0x0;
- while (iterator < (int) heap->index.size) {
- uint32_t tmp = (uint32_t) lookup_ordered_array(iterator, &heap->index);
- if (tmp > value) {
- value = tmp;
- idx = iterator;
- }
- iterator++;
- }
-
- if ((int) idx == -1) {
- header_t *header = (header_t *) old_end_address;
- header->magic = HEAP_MAGIC;
- header->size = new_length - old_length;
- header->is_hole = 1;
- footer_t *footer = (footer_t *) (old_end_address + header->size - sizeof(footer_t));
- footer->magic = HEAP_MAGIC;
- footer->header = header;
- insert_ordered_array((void *) header, &heap->index);
- } else {
- header_t *header = lookup_ordered_array(idx, &heap->index);
- header->size += new_length - old_length;
- footer_t *footer = (footer_t *) ((uint32_t) header + header->size - sizeof(footer_t));
- footer->header = header;
- footer->magic = HEAP_MAGIC;
- }
- return alloc(size, page_align, heap);
- }
-
- header_t *orig_hole_header = (header_t *) lookup_ordered_array(iterator, &heap->index);
- uint32_t orig_hole_pos = (uint32_t) orig_hole_header;
- uint32_t orig_hole_size = orig_hole_header->size;
-
- if (orig_hole_size - new_size < sizeof(header_t) + sizeof(footer_t)) {
- size += orig_hole_size - new_size;
- new_size = orig_hole_size;
- }
-
- if (page_align && orig_hole_pos & 0xFFFFF000) {
- uint32_t new_location = orig_hole_pos + 0x1000 - (orig_hole_pos & 0xFFF) - sizeof(header_t);
- header_t *hole_header = (header_t *) orig_hole_pos;
- hole_header->size = 0x1000 - (orig_hole_pos & 0xFFF) - sizeof(header_t);
- hole_header->magic = HEAP_MAGIC;
- hole_header->is_hole = 1;
- footer_t *hole_footer = (footer_t *) ((uint32_t) new_location - sizeof(footer_t));
- hole_footer->magic = HEAP_MAGIC;
- hole_footer->header = hole_header;
- orig_hole_pos = new_location;
- orig_hole_size = orig_hole_size - hole_header->size;
- } else {
- remove_ordered_array(iterator, &heap->index);
- }
-
- header_t *block_header = (header_t *) orig_hole_pos;
- block_header->magic = HEAP_MAGIC;
- block_header->is_hole = 0;
- block_header->size = new_size;
- footer_t *block_footer = (footer_t *) (orig_hole_pos + sizeof(header_t) + size);
- block_footer->magic = HEAP_MAGIC;
- block_footer->header = block_header;
-
- if (orig_hole_size - new_size > 0) {
- header_t *hole_header = (header_t *) (orig_hole_pos + sizeof(header_t) + size + sizeof(footer_t));
- hole_header->magic = HEAP_MAGIC;
- hole_header->is_hole = 1;
- hole_header->size = orig_hole_size - new_size;
- footer_t *hole_footer = (footer_t *) ((uint32_t) hole_header + orig_hole_size - new_size - sizeof(footer_t));
- if ((uint32_t) hole_footer < heap->end_address) {
- hole_footer->magic = HEAP_MAGIC;
- hole_footer->header = hole_header;
- }
- insert_ordered_array((void *) hole_header, &heap->index);
- }
- return (void *) ((uint32_t) block_header + sizeof(header_t));
-}
-
-void free(void *p, heap_t *heap) {
- if (p == 0)
- return;
-
- header_t *header = (header_t *) ((uint32_t) p - sizeof(header_t));
- footer_t *footer = (footer_t *) ((uint32_t) header + header->size - sizeof(footer_t));
-
- assert(header->magic == HEAP_MAGIC);
- assert(footer->magic == HEAP_MAGIC);
-
- header->is_hole = 1;
- char do_add = 1;
-
- footer_t *test_footer = (footer_t *) ((uint32_t) header - sizeof(footer_t));
- if (test_footer->magic == HEAP_MAGIC &&
- test_footer->header->is_hole == 1) {
- uint32_t cache_size = header->size;
- header = test_footer->header;
- footer->header = header;
- header->size += cache_size;
- do_add = 0;
- }
-
- header_t *test_header = (header_t *) ((uint32_t) footer + sizeof(footer_t));
- if (test_header->magic == HEAP_MAGIC &&
- test_header->is_hole) {
- header->size += test_header->size;
- test_footer = (footer_t *) ((uint32_t) test_header + test_header->size - sizeof(footer_t));
- footer = test_footer;
- uint32_t iterator = 0;
- while ((iterator < heap->index.size) && (lookup_ordered_array(iterator, &heap->index) != (void *) test_header))
- iterator++;
-
- assert(iterator < heap->index.size);
- remove_ordered_array(iterator, &heap->index);
- }
-
- if ((uint32_t) footer + sizeof(footer_t) == heap->end_address) {
- uint32_t old_length = heap->end_address - heap->start_address;
- uint32_t new_length = contract((uint32_t) header - heap->start_address, heap);
-
- if (header->size - (old_length - new_length) > 0) {
- header->size -= old_length - new_length;
- footer = (footer_t *) ((uint32_t) header + header->size - sizeof(footer_t));
- footer->magic = HEAP_MAGIC;
- footer->header = header;
- } else {
- uint32_t iterator = 0;
- while ((iterator < heap->index.size) &&
- (lookup_ordered_array(iterator, &heap->index) != (void *) test_header))
- iterator++;
-
- if (iterator < heap->index.size)
- remove_ordered_array(iterator, &heap->index);
- }
- }
-
- if (do_add == 1)
- insert_ordered_array((void *) header, &heap->index);
-}
diff --git a/src/kernel/paging/kheap.h b/src/kernel/paging/kheap.h
deleted file mode 100644
index a2254be..0000000
--- a/src/kernel/paging/kheap.h
+++ /dev/null
@@ -1,108 +0,0 @@
-#ifndef MELVIX_KHEAP_H
-#define MELVIX_KHEAP_H
-
-#include "ordered_array.h"
-
-#define KHEAP_START 0xC0000000
-#define KHEAP_INITIAL_SIZE 0x100000
-
-#define HEAP_INDEX_SIZE 0x20000
-#define HEAP_MAGIC 0x03A93A90
-#define HEAP_MIN_SIZE 0x70000
-
-/**
- * Size information of holes/blocks
- */
-typedef struct {
- uint32_t magic;
- unsigned char is_hole; // 1 if hole
- uint32_t size;
-} header_t;
-
-typedef struct {
- uint32_t magic;
- header_t *header;
-} footer_t;
-
-typedef struct {
- ordered_array_t index;
- uint32_t start_address;
- uint32_t end_address;
- uint32_t max_address;
- unsigned char supervisor;
- unsigned char readonly;
-} heap_t;
-
-/**
- * Create a new heap
- * @param start
- * @param end
- * @param max
- * @param supervisor
- * @param readonly
- * @return The heap pointer
- */
-heap_t *create_heap(uint32_t start, uint32_t end, uint32_t max, unsigned char supervisor, unsigned char readonly);
-
-/**
- * Allocate a region of memory
- * @param size The size of the memory
- * @param page_align Start the block on a page boundary
- * @param heap The Heap pointer
- * @return
- */
-void *alloc(uint32_t size, unsigned char page_align, heap_t *heap);
-
-/**
- * Release an allocated block
- * @param p The block
- * @param heap The heap
- */
-void free(void *p, heap_t *heap);
-
-/**
- * Release an allocated block using kheap
- * @param p The block
- */
-void kfree(void *p);
-
-/**
- * Allocate a chunk of memory
- * @param sz The size of the memory
- * @param align Start the block on a page boundary
- * @param phys Location of the memory if not 0
- * @return The memory address
- */
-uint32_t kmalloc_int(uint32_t sz, int align, uint32_t *phys);
-
-/**
- * Allocate a page-aligned chunk of memory
- * @param sz The size of the memory
- * @return The memory address
- */
-uint32_t kmalloc_a(uint32_t sz);
-
-/**
- * Allocate a chunk of memory in a physical address
- * @param sz The size of the memory
- * @param phys The physical address
- * @return The memory address
- */
-uint32_t kmalloc_p(uint32_t sz, uint32_t *phys);
-
-/**
- * Allocate a page-aligned chunk of memory in a physical address
- * @param sz The size of the memory
- * @param phys The physical address
- * @return The memory address
- */
-uint32_t kmalloc_ap(uint32_t sz, uint32_t *phys);
-
-/**
- * Allocate a chunk of memory (non page-aligned and no physical address)
- * @param sz The size of the memory
- * @return The memory address
- */
-uint32_t kmalloc(uint32_t sz);
-
-#endif
diff --git a/src/kernel/paging/ordered_array.c b/src/kernel/paging/ordered_array.c
deleted file mode 100644
index 688ff8f..0000000
--- a/src/kernel/paging/ordered_array.c
+++ /dev/null
@@ -1,66 +0,0 @@
-#include <stdint.h>
-#include "ordered_array.h"
-#include "kheap.h"
-#include "../lib/lib.h"
-#include "../system.h"
-
-char standard_lessthan_predicate(type_t a, type_t b) {
- return (a < b) ? 1 : 0;
-}
-
-ordered_array_t create_ordered_array(uint32_t max_size, lessthan_predicate_t less_than) {
- ordered_array_t to_ret;
- to_ret.array = (void *) kmalloc(max_size * sizeof(type_t));
- memory_set(to_ret.array, 0, max_size * sizeof(type_t));
- to_ret.size = 0;
- to_ret.max_size = max_size;
- to_ret.less_than = less_than;
- return to_ret;
-}
-
-ordered_array_t place_ordered_array(void *addr, uint32_t max_size, lessthan_predicate_t less_than) {
- ordered_array_t to_ret;
- to_ret.array = (type_t *) addr;
- memory_set(to_ret.array, 0, max_size * sizeof(type_t));
- to_ret.size = 0;
- to_ret.max_size = max_size;
- to_ret.less_than = less_than;
- return to_ret;
-}
-
-void destroy_ordered_array(ordered_array_t *array) {
- kfree(array->array);
-}
-
-void insert_ordered_array(type_t item, ordered_array_t *array) {
- assert((int) array->less_than);
- uint32_t iterator = 0;
- while (iterator < array->size && array->less_than(array->array[iterator], item))
- iterator++;
- if (iterator == array->size)
- array->array[array->size++] = item;
- else {
- type_t tmp = array->array[iterator];
- array->array[iterator] = item;
- while (iterator < array->size) {
- iterator++;
- type_t tmp2 = array->array[iterator];
- array->array[iterator] = tmp;
- tmp = tmp2;
- }
- array->size++;
- }
-}
-
-type_t lookup_ordered_array(uint32_t i, ordered_array_t *array) {
- assert(i < array->size);
- return array->array[i];
-}
-
-void remove_ordered_array(uint32_t i, ordered_array_t *array) {
- while (i < array->size) {
- array->array[i] = array->array[i + 1];
- i++;
- }
- array->size--;
-}
diff --git a/src/kernel/paging/ordered_array.h b/src/kernel/paging/ordered_array.h
deleted file mode 100644
index 223ea14..0000000
--- a/src/kernel/paging/ordered_array.h
+++ /dev/null
@@ -1,66 +0,0 @@
-#ifndef MELVIX_ORDERED_ARRAY_H
-#define MELVIX_ORDERED_ARRAY_H
-
-#include <stdint.h>
-
-/**
- * The array can store anything - so a void pointer is used
- */
-typedef void *type_t;
-
-typedef char (*lessthan_predicate_t)(type_t, type_t);
-
-typedef struct {
- type_t *array;
- uint32_t size;
- uint32_t max_size;
- lessthan_predicate_t less_than;
-} ordered_array_t;
-
-/**
- * A standard less-than predicate
- * @param a The first parameter
- * @param b The second parameter
- * @return Non-zero if the first parameter is bigger than the second
- */
-char standard_lessthan_predicate(type_t a, type_t b);
-
-/**
- * Create an ordered array
- * @param max_size Maximal size
- * @param less_than The less-than predicate
- * @return The newly created array
- */
-ordered_array_t create_ordered_array(uint32_t max_size, lessthan_predicate_t less_than);
-
-ordered_array_t place_ordered_array(void *addr, uint32_t max_size, lessthan_predicate_t less_than);
-
-/**
- * Destroy an ordered array
- * @param array The ordered array
- */
-void destroy_ordered_array(ordered_array_t *array);
-
-/**
- * Add an item into the array
- * @param item The item
- * @param array The array
- */
-void insert_ordered_array(type_t item, ordered_array_t *array);
-
-/**
- * Lookup the item at a specific index
- * @param i The index
- * @param array The array
- * @return
- */
-type_t lookup_ordered_array(uint32_t i, ordered_array_t *array);
-
-/**
- * Delete an item at an index from an array
- * @param i The index
- * @param array The array
- */
-void remove_ordered_array(uint32_t i, ordered_array_t *array);
-
-#endif
diff --git a/src/kernel/paging/paging.c b/src/kernel/paging/paging.c
index 0b7428e..f1dc340 100644
--- a/src/kernel/paging/paging.c
+++ b/src/kernel/paging/paging.c
@@ -1,187 +1,149 @@
+#include <stdint.h>
#include "paging.h"
-#include "kheap.h"
#include "../system.h"
-#include "../lib/lib.h"
-#include "../io/io.h"
-
-page_directory_t *kernel_directory = 0;
-page_directory_t *current_directory = 0;
-uint32_t *frames;
-uint32_t nframes;
-
-extern uint32_t placement_address;
-extern heap_t *kheap;
-
-#define INDEX_FROM_BIT(a) (a/(8*4))
-#define OFFSET_FROM_BIT(a) (a%(8*4))
-
-static void set_frame(uint32_t frame_addr) {
- uint32_t frame = frame_addr / 0x1000;
- uint32_t idx = INDEX_FROM_BIT(frame);
- uint32_t off = OFFSET_FROM_BIT(frame);
- frames[idx] |= (0x1 << off);
-}
-
-static void clear_frame(uint32_t frame_addr) {
- uint32_t frame = frame_addr / 0x1000;
- uint32_t idx = INDEX_FROM_BIT(frame);
- uint32_t off = OFFSET_FROM_BIT(frame);
- frames[idx] &= ~(0x1 << off);
-}
-
-/*static uint32_t test_frame(uint32_t frame_addr) {
- uint32_t frame = frame_addr / 0x1000;
- uint32_t idx = INDEX_FROM_BIT(frame);
- uint32_t off = OFFSET_FROM_BIT(frame);
- return (frames[idx] & (0x1 << off));
-}*/
-
-static uint32_t first_frame() {
- uint32_t i, j;
- for (i = 0; i < INDEX_FROM_BIT(nframes); i++) {
- if (frames[i] != 0xFFFFFFFF) {
- for (j = 0; j < 32; j++) {
- uint32_t toTest = 0x1 << j;
- if (!(frames[i] & toTest)) {
- return i * 4 * 8 + j;
- }
- }
+
+extern void *end;
+
+uint32_t page_directory[1024] __attribute__((aligned(4096)));
+uint32_t page_tables[1024][1024] __attribute__((aligned(4096)));
+
+void paging_install() {
+ for (uint32_t i = 0; i < 1024; i++) {
+ for (uint32_t j = 0; j < 1024; j++) {
+ page_tables[i][j] = ((j * 0x1000) + (i * 0x400000)) | PT_RW;
}
}
- return -1;
-}
-
-void alloc_frame(page_t *page, int is_kernel, int is_writeable) {
- if (page->frame != 0) {
- return;
- } else {
- uint32_t idx = first_frame();
- if (idx == (uint32_t) -1) panic("No free frames!");
- set_frame(idx * 0x1000);
- page->present = 1;
- page->rw = (is_writeable == 1) ? 1 : 0;
- page->user = (is_kernel == 1) ? 0 : 1;
- page->frame = idx;
- }
-}
-void free_frame(page_t *page) {
- uint32_t frame;
- if (!(frame = page->frame)) {
- return;
- } else {
- clear_frame(frame);
- page->frame = 0x0;
+ for (uint32_t i = 0; i < 1024; i++) {
+ page_directory[i] = ((uint32_t) page_tables[i]) | PD_RW | PD_PRESENT;
}
-}
-
-void initialise_paging() {
- uint32_t mem_end_page = 0x1000000;
- nframes = mem_end_page / 0x1000;
- frames = (uint32_t *) kmalloc(INDEX_FROM_BIT(nframes));
- memory_set(frames, 0, INDEX_FROM_BIT(nframes));
- kernel_directory = (page_directory_t *) kmalloc_a(sizeof(page_directory_t));
- memory_set(kernel_directory, 0, sizeof(page_directory_t));
- kernel_directory->physicalAddr = (uint32_t) kernel_directory->tablesPhysical;
+ // TODO: Calculate max memory
+ paging_set_present(0, 0x1000000);
- unsigned int i = 0;
- for (i = KHEAP_START; i < KHEAP_START + KHEAP_INITIAL_SIZE; i += 0x1000)
- get_page(i, 1, kernel_directory);
+ paging_set_used(0, ((uint32_t) end >> 12) + 1);
- i = 0;
- while (i < 0x400000) {
- alloc_frame(get_page(i, 1, kernel_directory), 0, 0);
- i += 0x1000;
- }
-
- for (i = KHEAP_START; i < KHEAP_START + KHEAP_INITIAL_SIZE; i += 0x1000)
- alloc_frame(get_page(i, 1, kernel_directory), 0, 0);
+ paging_enable();
+}
- switch_page_directory(kernel_directory);
- kheap = create_heap(KHEAP_START, KHEAP_START + KHEAP_INITIAL_SIZE, 0xCFFFF000, 0, 0);
- current_directory = clone_directory(kernel_directory);
- switch_page_directory(current_directory);
- serial_write("Paging init successful!\n");
+int paging_enabled() {
+ uint32_t cr0;
+ asm volatile("mov %%cr0, %0": "=r"(cr0));
+ return (cr0 | 0x80000000) == cr0;
}
-void disable_paging() {
+void paging_disable() {
uint32_t cr0;
asm volatile("mov %%cr0, %0": "=r"(cr0));
cr0 &= 0x7fffffff;
asm volatile("mov %0, %%cr0"::"r"(cr0));
}
-void enable_paging() {
- switch_page_directory(kernel_directory);
- switch_page_directory(current_directory);
-}
-
-void switch_page_directory(page_directory_t *dir) {
- current_directory = dir;
- asm volatile("mov %0, %%cr3"::"r"(dir->physicalAddr));
+void paging_enable() {
+ asm volatile("mov %0, %%cr3"::"r"(page_directory));
uint32_t cr0;
asm volatile("mov %%cr0, %0": "=r"(cr0));
cr0 |= 0x80000000;
asm volatile("mov %0, %%cr0"::"r"(cr0));
}
-page_t *get_page(uint32_t address, int make, page_directory_t *dir) {
- address /= 0x1000;
- uint32_t table_idx = address / 1024;
-
- if (dir->tables[table_idx]) {
- return &dir->tables[table_idx]->pages[address % 1024];
- } else if (make) {
- uint32_t tmp;
- dir->tables[table_idx] = (page_table_t *) kmalloc_ap(sizeof(page_table_t), &tmp);
- memory_set(dir->tables[table_idx], 0, 0x1000);
- dir->tablesPhysical[table_idx] = tmp | 0x7;
- return &dir->tables[table_idx]->pages[address % 1024];
- } else {
- return 0;
+inline void invlpg(uint32_t addr) {
+ asm volatile("invlpg (%0)"::"r" (addr) : "memory");
+}
+
+void paging_map(uint32_t phy, uint32_t virt, uint16_t flags) {
+ uint32_t pdi = virt >> 22;
+ uint32_t pti = virt >> 12 & 0x03FF;
+ page_tables[pdi][pti] = phy | flags;
+ invlpg(virt);
+}
+
+uint32_t paging_get_physical_addr(uint32_t virt) {
+ uint32_t pdi = virt >> 22;
+ uint32_t pti = (virt >> 12) & 0x03FF;
+ return page_tables[pdi][pti] & 0xFFFFF000;
+}
+
+uint16_t paging_get_flags(uint32_t virt) {
+ uint32_t pdi = virt >> 22;
+ uint32_t pti = (virt >> 12) & 0x03FF;
+ return page_tables[pdi][pti] & 0xFFF;
+}
+
+void paging_set_flag_up(uint32_t virt, uint32_t count, uint32_t flag) {
+ uint32_t page_n = virt / 4096;
+ for (uint32_t i = page_n; i < page_n + count; i++) {
+ page_tables[i / 1024][i % 1024] |= flag;
+ invlpg(i * 4096);
+ }
+}
+
+void paging_set_flag_down(uint32_t virt, uint32_t count, uint32_t flag) {
+ uint32_t page_n = virt / 4096;
+ for (uint32_t i = page_n; i < page_n + count; i++) {
+ page_tables[i / 1024][i % 1024] &= ~flag;
+ invlpg(i * 4096);
+ }
+}
+
+void paging_set_present(uint32_t virt, uint32_t count) {
+ paging_set_flag_up(virt, count, PT_PRESENT);
+}
+
+void paging_set_absent(uint32_t virt, uint32_t count) {
+ paging_set_flag_down(virt, count, PT_PRESENT);
+}
+
+void paging_set_used(uint32_t virt, uint32_t count) {
+ paging_set_flag_up(virt, count, PT_USED);
+}
+
+void paging_set_free(uint32_t virt, uint32_t count) {
+ paging_set_flag_down(virt, count, PT_USED);
+}
+
+void paging_set_user(uint32_t virt, uint32_t count) {
+ uint32_t page_n = virt / 4096;
+ for (uint32_t i = page_n; i < page_n + count; i += 1024) {
+ page_directory[i / 1024] |= PD_ALL_PRIV;
}
+ paging_set_flag_up(virt, count, PT_ALL_PRIV);
}
-static page_table_t *clone_table(page_table_t *src, uint32_t *physAddr) {
- page_table_t *table = (page_table_t *) kmalloc_ap(sizeof(page_table_t), physAddr);
- memory_set(table, 0, sizeof(page_directory_t));
-
- int i;
- for (i = 0; i < 1024; i++) {
- if (!src->pages[i].frame)
- continue;
- alloc_frame(&table->pages[i], 0, 0);
- if (src->pages[i].present) table->pages[i].present = 1;
- if (src->pages[i].rw) table->pages[i].rw = 1;
- if (src->pages[i].user) table->pages[i].user = 1;
- if (src->pages[i].accessed)table->pages[i].accessed = 1;
- if (src->pages[i].dirty) table->pages[i].dirty = 1;
- extern void copy_page_physical(int a, int b);
- copy_page_physical(src->pages[i].frame * 0x1000, table->pages[i].frame * 0x1000);
+uint32_t paging_find_pages(uint32_t count) {
+ uint32_t continous = 0;
+ uint32_t startDir = 0;
+ uint32_t startPage = 0;
+ for (uint32_t i = 0; i < 1024; i++) {
+ for (uint32_t j = 0; j < 1024; j++) {
+ if (!(page_tables[i][j] & PT_PRESENT) || (page_tables[i][j] & PT_USED)) {
+ continous = 0;
+ startDir = i;
+ startPage = j + 1;
+ } else {
+ if (++continous == count)
+ return (startDir * 0x400000) + (startPage * 0x1000);
+ }
+ }
}
- return table;
-}
-
-page_directory_t *clone_directory(page_directory_t *src) {
- uint32_t phys;
- page_directory_t *dir = (page_directory_t *) kmalloc_ap(sizeof(page_directory_t), &phys);
- memory_set(dir, 0, sizeof(page_directory_t));
- uint32_t offset = (uint32_t) dir->tablesPhysical - (uint32_t) dir;
- dir->physicalAddr = phys + offset;
-
- for (int i = 0; i < 1024; i++) {
- if (!src->tables[i])
- continue;
-
- if (kernel_directory->tables[i] == src->tables[i]) {
- dir->tables[i] = src->tables[i];
- dir->tablesPhysical[i] = src->tablesPhysical[i];
- } else {
- uint32_t phys;
- dir->tables[i] = clone_table(src->tables[i], &phys);
- dir->tablesPhysical[i] = phys | 0x07;
+
+ panic("Out of memory!");
+ return 0;
+}
+
+uint32_t paging_alloc_pages(uint32_t count) {
+ uint32_t ptr = paging_find_pages(count);
+ paging_set_used(ptr, count);
+ return ptr;
+}
+
+uint32_t paging_get_used_pages() {
+ uint32_t n = 0;
+ for (uint32_t i = 0; i < 1024; i++) {
+ for (uint32_t j = 0; j < 1024; j++) {
+ uint8_t flags = page_tables[i][j] & PT_USED;
+ if (flags == 1) n++;
}
}
- return dir;
+ return n;
}
diff --git a/src/kernel/paging/paging.h b/src/kernel/paging/paging.h
index 681b84e..85ef120 100644
--- a/src/kernel/paging/paging.h
+++ b/src/kernel/paging/paging.h
@@ -2,73 +2,57 @@
#define MELVIX_PAGING_H
#include <stdint.h>
-#include "../interrupts/interrupts.h"
-
-typedef struct page {
- uint32_t present : 1; // Page present in memory
- uint32_t rw : 1; // Read-only if clear, readwrite if set
- uint32_t user : 1; // Supervisor level only if clear
- uint32_t accessed : 1; // Has the page been accessed since last refresh?
- uint32_t dirty : 1; // Has the page been written to since last refresh?
- uint32_t unused : 7; // Amalgamation of unused and reserved bits
- uint32_t frame : 20; // Frame address (shifted right 12 bits)
-} page_t;
-
-typedef struct page_table {
- page_t pages[1024];
-} page_table_t;
-
-typedef struct page_directory {
- page_table_t *tables[1024];
- uint32_t tablesPhysical[1024];
- uint32_t physicalAddr;
-} page_directory_t;
-
-/**
- * Initialize the environment and enable paging
- */
-void initialise_paging();
-
-/**
- * Enable paging bit in CR0 (without initializing)
- */
-void enable_paging();
-
-/**
- * Disable paging bit in CR0
- */
-void disable_paging();
-
-/**
- * Load the page directory into the CR3 register
- * @param new The page directory
- */
-void switch_page_directory(page_directory_t *new);
-
-/**
- * Get a specific page pointer
- * @param address The page address
- * @param make If 1 create the page first
- * @param dir The page directory
- * @return The page pointer
- */
-page_t *get_page(uint32_t address, int make, page_directory_t *dir);
-
-/**
- * Page fault handler
- * @param r The IRQ registers
- */
-void page_fault(struct regs *r);
-
-void alloc_frame(page_t *page, int is_kernel, int is_writeable);
-
-void free_frame(page_t *page);
-
-/**
- * Copy/clone a page directory
- * @param src The page directory
- * @return A new page directory pointer
- */
-page_directory_t *clone_directory(page_directory_t *src);
+
+#define PD_PRESENT 1 << 0
+#define PD_RW 1 << 1
+#define PD_ALL_PRIV 1 << 2
+#define PD_WRITETHR 1 << 3
+#define PD_CACHE_D 1 << 4
+#define PD_ACCESSED 1 << 5
+#define PD_4M_PAGE 1 << 7
+
+#define PT_PRESENT 1 << 0
+#define PT_RW 1 << 1
+#define PT_ALL_PRIV 1 << 2
+#define PT_WRITETHR 1 << 3
+#define PT_CACHE_D 1 << 4
+#define PT_ACCESSED 1 << 5
+#define PT_DIRTY 1 << 6
+#define PT_GLOBAL 1 << 8
+#define PT_USED 1 << 9
+
+void paging_install();
+
+void paging_enable();
+
+void paging_disable();
+
+void paging_map(uint32_t phy, uint32_t virt, uint16_t flags);
+
+uint32_t paging_get_phys(uint32_t virt);
+
+uint16_t paging_get_flags(uint32_t virt);
+
+void paging_set_flags(uint32_t virt, uint32_t count, uint16_t flags);
+
+void paging_set_flag_up(uint32_t virt, uint32_t count, uint32_t flag);
+
+void paging_set_flag_down(uint32_t virt, uint32_t count, uint32_t flag);
+
+void paging_set_present(uint32_t virt, uint32_t count);
+
+void paging_set_absent(uint32_t virt, uint32_t count);
+
+void paging_set_used(uint32_t virt, uint32_t count);
+
+void paging_set_free(uint32_t virt, uint32_t count);
+
+void paging_set_user(uint32_t virt, uint32_t count);
+
+uint32_t paging_find_pages(uint32_t count);
+
+uint32_t paging_alloc_pages(uint32_t count);
+
+uint32_t paging_get_used_pages();
#endif