summaryrefslogtreecommitdiff
path: root/src/kernel/libs/page.c
diff options
context:
space:
mode:
authorBrett Weiland <brett_weiland@bpcspace.com>2021-03-24 15:36:54 -0500
committerBrett Weiland <brett_weiland@bpcspace.com>2021-03-24 15:38:09 -0500
commit14b109ea24dc5cb1db948de57a2a44c80ef4622e (patch)
treee397997d9c8d9c1b50a5a5235d38c6b0e8f24b5b /src/kernel/libs/page.c
parent13000d6f44d66da795e92e4b2ddc37ce879bf275 (diff)
modified: README.md
modified: compiler/create_crosscompiler.sh deleted: notes modified: src/.gdb_history deleted: src/amd64_vol2.pdf modified: src/bootloader/bios_functions/bios_disk.asm modified: src/bootloader/bios_functions/print.asm modified: src/bootloader/bootloader.asm modified: src/bootloader/cpu_check.asm modified: src/bootloader/enter_kernel.asm deleted: src/bootloader/enter_kernel_backup modified: src/bootloader/gdt.asm deleted: src/bootloader/multicore.asm deleted: src/bootloader/notes new file: src/debug/gdbinit.gdb deleted: src/indigo_os modified: src/kernel/include/libc.h modified: src/kernel/include/paging.h modified: src/kernel/include/video.h modified: src/kernel/kernel.c modified: src/kernel/libs/acpi.c modified: src/kernel/libs/drivers/serial.c modified: src/kernel/libs/drivers/video.c modified: src/kernel/libs/libc.c modified: src/kernel/libs/page.c modified: src/link.ld modified: src/makefile new file: tools/README.md modified: tools/page/page.py
Diffstat (limited to 'src/kernel/libs/page.c')
-rw-r--r--src/kernel/libs/page.c108
1 files changed, 79 insertions, 29 deletions
diff --git a/src/kernel/libs/page.c b/src/kernel/libs/page.c
index 88d0c2f..6f1dd7c 100644
--- a/src/kernel/libs/page.c
+++ b/src/kernel/libs/page.c
@@ -1,14 +1,7 @@
#include <printf.h>
#include <paging.h>
#include <stdint.h>
-
-// for now, this will always remain the same.
-// If this ever isn't the case, we'll add paramaters to the paging functions.
-// We will also never need to mess with many bits,
-// as we are not implimenting security for a bare metal fractal generator.
-// Also, is this really the cleanest way to do things?
-
-//after we get paging working, we can probably remove these structs and replace them with plain old uint_16ts.
+#include <libc.h>
void debug_print_memory() {
struct memory_table *memtable = (struct memory_table *)&_meminfo_loc;
@@ -22,33 +15,89 @@ void debug_print_memory() {
}
+//uses buddy system allocation
+void init_memory() {
+ struct memory_table *memtable = (struct memory_table *)&_meminfo_loc;
+ struct phys_map *map = (struct phys_map*)0x200000;
+ uintptr_t onpage = 0x200000;
+ unsigned int i, x, buddy_size, buddy_bitsize, prev_buddy_bsize;
+ uint64_t *buddy_ptr;
+
+ map_page((void*)0x200000, (void*)0x200000, PAGE_SIZE_2M);
+ void *next_page = (void *)onpage + 0x200000;
+ // at this point, we are declaring our header and kernel itself as free (so don't forget to fix that!)
+
+ for(i = 0; memtable[i].length > 0; i++) {
+ if((memtable[i].type == MEM_AVAILABLE) && (memtable[i].ACPI & 1)) {
+
+ map->chunk_start = (void*)memtable[i].base;
+ map->chunk_size = memtable[i].length;
+ buddy_ptr = (void*)&map->buddies;
+
+
+ for(x = 0; x < 8; x++) {
+
+ buddy_bitsize = memtable[i].length / (0x1000 * (1 << x));
+ buddy_size = ceil(buddy_bitsize / (float)8);
+
+ if((void *)buddy_ptr + buddy_size >= next_page) {
+ map_page(next_page, next_page, PAGE_SIZE_2M);
+ next_page += 0x200000;
+ }
+
+ bzero(buddy_ptr, buddy_size); //meant to be /8?
+
+ if((buddy_bitsize * 2) != prev_buddy_bsize) {
+ buddy_ptr[-1] |= (1 << ((prev_buddy_bsize % 8) - 1));
+ }
+
+ buddy_ptr += buddy_size;
+
+ prev_buddy_bsize = buddy_bitsize;
+
+ }
+ map->map_size = buddy_ptr - (uint64_t*)map;
+ map = (struct phys_map *)map + map->map_size;
+
+ if((void *)map + 24 >= next_page) {
+ map_page(next_page, next_page, PAGE_SIZE_2M);
+ next_page += 0x200000;
+ }
+ }
+ }
+}
+
+//TODO this function was deleted due to it being wrong.
+//I'll create it once I have the physical paging prerequisite set up.
+void create_pagetable_stage2(uint64_t free_mem) {
+}
+
+
/**
- * You can critisise the quality of this function all you want, it's messy due to planning mistakes
- * and I'm planning on sanatising it.
- * BUT, don't critisise it _just_ for using goto, that's bullshit. See the following links.
- *
- * https://www.kernel.org/doc/html/v4.17/process/coding-style.html "goto" section
- * https://koblents.com/Ches/Links/Month-Mar-2013/20-Using-Goto-in-Linux-Kernel-Code/
+ * BIG TODO:
+ * Paging turned out to be simpler then I thought. I've temporarily fixed the code, but needs to be rewritten/simplified.
+ * Let's get rid of those nasty GOTOs if we can.
+ * Also, once we get physical memory allocator up and running, impliment that in this function.
**/
-bool map_page(uintptr_t virtual_addr, uintptr_t physical_addr, uint8_t size) {
- printf("map page called\n");
- if((virtual_addr % (1 << size)) || (physical_addr % (1 << size))) {
+bool map_page(void *virtual_addr, void *physical_addr, uint8_t size) {
+ //printf("map page called\n");
+ uintptr_t va_ptr = (uintptr_t)virtual_addr;
+ uintptr_t pa_ptr = (uintptr_t)physical_addr;
+ if((va_ptr % (1 << size)) || (pa_ptr % (1 << size))) {
return 0;
}
page_table *table = (page_table *)PAGEMAP_LOCATION;
- int pte_i = (virtual_addr >> 12) & 0x1ff;
- int pde_i = (virtual_addr >> 21) & 0x1ff;
- int pdpe_i = (virtual_addr >> 30) & 0x1ff;
- int pml4e_i = (virtual_addr >> 39) & 0x1ff;
- //TODO remove this debugging info
- printf("Virtual offsets:\npte:\t\t%i\npde:\t\t%i\npdpe:\t\t%i\npml4e\t\t%i\n", pte_i, pde_i, pdpe_i, pml4e_i);
+ int pte_i = (va_ptr >> 12) & 0x1ff;
+ int pde_i = (va_ptr >> 21) & 0x1ff;
+ int pdpe_i = (va_ptr >> 30) & 0x1ff;
+ int pml4e_i = (va_ptr >> 39) & 0x1ff;
if(table->pml4e[pml4e_i].present) {
if(table->pml4e[pml4e_i].base_ptr != (uintptr_t)&table->pdpe[pdpe_i] >> 12) goto error;
if(table->pdpe[pdpe_i].present) {
if(size == PAGE_SIZE_1G) {
- if(table->pdpe[pdpe_i].base_ptr == (uintptr_t)physical_addr >> 30 & 0x1ff)
+ if(table->pdpe[pdpe_i].base_ptr == (uintptr_t)pa_ptr >> 30 & 0x1ff)
return true;
goto error;
}
@@ -56,13 +105,13 @@ bool map_page(uintptr_t virtual_addr, uintptr_t physical_addr, uint8_t size) {
if(table->pde[pde_i].present) {
if(size == PAGE_SIZE_2M) {
- if(table->pde[pde_i].base_ptr == (uintptr_t)physical_addr >> 21 & 0x1ff)
+ if(table->pde[pde_i].base_ptr == (uintptr_t)pa_ptr >> 21 & 0x1ff)
return true;
goto error;
}
if(table->pde[pde_i].base_ptr != (uintptr_t)&table->pte[pte_i] >> 12) goto error;
if(table->pte[pte_i].present) {
- if(table->pte[pte_i].base_ptr != ((physical_addr >> 12) & 0x1ff)) goto error;
+ if(table->pte[pte_i].base_ptr != ((pa_ptr >> 12) & 0x1ff)) goto error;
return true;
}
else goto mod_page_pte;
@@ -77,9 +126,10 @@ bool map_page(uintptr_t virtual_addr, uintptr_t physical_addr, uint8_t size) {
table->pml4e[pml4e_i].present = 1;
mod_page_pdpe:
table->pdpe[pdpe_i].read_write = 1;
+ //TODO you just found out things are a lot more simple then you thought!
if(size == PAGE_SIZE_1G) {
table->pdpe[pdpe_i].size = 1;
- table->pdpe[pdpe_i].base_ptr = physical_addr >> 30;
+ table->pdpe[pdpe_i].base_ptr = pa_ptr >> 12;
table->pdpe[pdpe_i].present = 1;
return true;
}
@@ -89,14 +139,14 @@ mod_page_pde:
table->pde[pde_i].read_write = 1;
if(size == PAGE_SIZE_2M) {
table->pde[pde_i].size = 1;
- table->pde[pde_i].base_ptr = physical_addr >> 21;
+ table->pde[pde_i].base_ptr = pa_ptr >> 12;
table->pde[pde_i].present = 1;
return true;
}
table->pde[pde_i].base_ptr = (uintptr_t)&table->pte[pte_i] >> 12;
table->pde[pde_i].present = 1;
mod_page_pte:
- table->pte[pte_i].base_ptr = (physical_addr >> 12);
+ table->pte[pte_i].base_ptr = pa_ptr >> 12;
table->pte[pte_i].read_write = 1;
table->pte[pte_i].present = 1;
return true;