summaryrefslogtreecommitdiff
path: root/kernel/application.cpp
diff options
context:
space:
mode:
authorBenji Dial <benji@benjidial.net>2024-01-15 15:44:20 -0500
committerBenji Dial <benji@benjidial.net>2024-01-15 15:44:20 -0500
commitc9a1266d219a83882735a3a8304f3824e0219cdb (patch)
treefea71a0b5b0de8f3a8962dc6ed417273f231e2a9 /kernel/application.cpp
parent7c6a18d77a81f232ad2e1d3a311bb21ea8f1f5b4 (diff)
downloadhilbert-os-c9a1266d219a83882735a3a8304f3824e0219cdb.tar.gz
redo application paging and system calls, rename mercury to hilbert
Diffstat (limited to 'kernel/application.cpp')
-rw-r--r--kernel/application.cpp216
1 files changed, 108 insertions, 108 deletions
diff --git a/kernel/application.cpp b/kernel/application.cpp
index 19b1dbc..ed8795b 100644
--- a/kernel/application.cpp
+++ b/kernel/application.cpp
@@ -1,83 +1,87 @@
-#include <mercury/kernel/application.hpp>
-#include <mercury/kernel/paging.hpp>
+#include <hilbert/kernel/application.hpp>
+#include <hilbert/kernel/paging.hpp>
//TODO - scheduling.
-namespace mercury::kernel::application {
+namespace hilbert::kernel::application {
- app_instance::app_instance() : state(app_state::paused) {
+ app_instance::app_instance()
+ : state(app_state::paused), framebuffer_vaddr(0) {
- framebuffer_vaddr = 0;
+ uint64_t p4_vaddr;
+ paging::map_new_kernel_page(p4_vaddr, p4_paddr);
+ p4 = (uint64_t *)p4_vaddr;
uint64_t p3_paddr;
- uint64_t p2_paddr;
-
- uint64_t p4_vaddr;
uint64_t p3_vaddr;
- uint64_t p2_vaddr;
-
- paging::map_new_kernel_page(p4_vaddr, p4_paddr);
paging::map_new_kernel_page(p3_vaddr, p3_paddr);
- paging::map_new_kernel_page(p2_vaddr, p2_paddr);
-
- p4 = (uint64_t *)p4_vaddr;
p3 = (uint64_t *)p3_vaddr;
- p2 = (uint64_t *)p2_vaddr;
for (int i = 1; i < 511; ++i)
p4[i] = 0;
+ p4[0] = paging::encode_pte(p3_paddr, true, true, true);
p4[511] = paging::kernel_p4e;
- p4[0] = paging::encode_pte(p3_paddr, true, true, true, false);
- for (int i = 1; i < 512; ++i)
+ for (int i = 0; i < 512; ++i) {
p3[i] = 0;
- p3[0] = paging::encode_pte(p2_paddr, true, true, true, false);
-
- for (int i = 0; i < 512; ++i)
- p2[i] = 0;
-
- p2es_to_free_on_exit = new bool[512];
+ p2s[i] = 0;
+ p1s[i] = 0;
+ p1es_to_free_on_exit[i] = 0;
+ }
}
- app_instance::~app_instance() {
+ void app_instance::map_page(uint64_t vaddr, uint64_t paddr,
+ bool write, bool execute, bool free_pram_on_exit
+ ) {
- for (int i = 1; i < 512; ++i)
- if (p2[i] != 0 && p2es_to_free_on_exit[i]) {
- uint64_t paddr = p2[i] & ~0x1fffffULL;
- paging::mark_pram_region_free(paddr, paddr + 0x200000);
+ uint64_t i = ((vaddr / 4096) / 512) / 512;
+ uint64_t j = ((vaddr / 4096) / 512) % 512;
+ uint64_t k = (vaddr / 4096) % 512;
+
+ if (p2s[i] == 0) {
+ uint64_t p2_paddr;
+ uint64_t p2_vaddr;
+ paging::map_new_kernel_page(p2_vaddr, p2_paddr);
+ p3[i] = paging::encode_pte(p2_paddr, true, true, true);
+ p2s[i] = (uint64_t *)p2_vaddr;
+ p1s[i] = new uint64_t *[512];
+ p1es_to_free_on_exit[i] = new bool *[512];
+ for (int u = 0; u < 512; ++u) {
+ p2s[i][u] = 0;
+ p1s[i][u] = 0;
+ p1es_to_free_on_exit[i][u] = 0;
}
+ }
- delete[] p2es_to_free_on_exit;
-
- uint64_t p2_paddr = p3[0] & ~0x1fffffULL;
- paging::unmap_kernel_page((uint64_t)p2);
- paging::mark_pram_region_free(p2_paddr, p2_paddr + 4096);
-
- uint64_t p3_paddr = p4[0] & ~0x1fffffULL;
- paging::unmap_kernel_page((uint64_t)p3);
- paging::mark_pram_region_free(p3_paddr, p3_paddr + 4096);
-
- paging::unmap_kernel_page((uint64_t)p4);
- paging::mark_pram_region_free(p4_paddr, p4_paddr + 4096);
+ if (p2s[i][j] == 0) {
+ uint64_t p1_paddr;
+ uint64_t p1_vaddr;
+ paging::map_new_kernel_page(p1_vaddr, p1_paddr);
+ p2s[i][j] = paging::encode_pte(p1_paddr, true, true, true);
+ p1s[i][j] = (uint64_t *)p1_vaddr;
+ p1es_to_free_on_exit[i][j] = new bool[512];
+ for (int u = 0; u < 512; ++u) {
+ p1s[i][j][u] = 0;
+ p1es_to_free_on_exit[i][j][u] = false;
+ }
+ }
- }
+ p1s[i][j][k] = paging::encode_pte(paddr, true, write, execute);
+ p1es_to_free_on_exit[i][j][k] = free_pram_on_exit;
- void app_instance::map_page(uint64_t vaddr, uint64_t paddr,
- bool write, bool execute, bool free_pram_on_exit
- ) {
- uint64_t i = vaddr / 0x200000;
- p2[i] = paging::encode_pte(paddr, true, write, execute, true);
- p2es_to_free_on_exit[i] = free_pram_on_exit;
}
uint64_t app_instance::get_free_vaddr_pages(uint64_t count) {
- uint64_t start = 1;
+ uint64_t start = 0x200000 / 4096;
uint64_t length = 0;
- while (start + length < 510) {
+ while (start + length <= 0x8000000000 / 4096) {
if (length == count)
- return start * 0x200000;
- if (p2[start + length] == 0)
+ return start * 4096;
+ int i = ((start + length) / 512) / 512;
+ int j = ((start + length) / 512) % 512;
+ int k = (start + length) % 512;
+ if (p1s[i] == 0 || p1s[i][j] == 0 || p1s[i][j][k] == 0)
++length;
else {
start += length + 1;
@@ -88,28 +92,15 @@ namespace mercury::kernel::application {
return 0;
}
- void app_instance::create_stack() {
- uint64_t stack_paddr = paging::take_2mib_pram_page();
- map_page(0x3fe00000, stack_paddr, true, false, true);
- for (int i = 0; i < 512; ++i) {
- uint64_t vaddr = paging::find_unmapped_vram_region(1);
- paging::map_kernel_page(stack_paddr + 512 * i, vaddr, true, false);
- for (int j = 0; j < 4096 / 8; ++j)
- *(uint64_t *)(vaddr + j * 8) = 0;
- paging::unmap_kernel_page(vaddr);
- }
- saved_regs.rsp = 0x40000000;
- }
-
- void app_instance::set_instruction_pointer(uint64_t vaddr) {
- saved_regs.rip = vaddr;
- }
-
uint64_t app_instance::count_mapped_vram_pages() {
uint64_t count = 0;
- for (int i = 1; i < 512; ++i)
- if (p2[i] != 0)
- ++count;
+ for (int i = 0; i < 512; ++i)
+ if (p1s[i] != 0)
+ for (int j = 0; j < 512; ++j)
+ if (p1s[i][j] != 0)
+ for (int k = 0; k < 512; ++k)
+ if (p1s[i][j][k] != 0)
+ ++count;
return count;
}
@@ -186,18 +177,19 @@ namespace mercury::kernel::application {
READ(entry_start + 40, 8, &vsize)
READ(entry_start + 4, 4, &flags)
- if (vaddr & 0x1fffff)
+ if (vaddr & 4095)
return create_app_result::app_corrupt;
if (file.dir_entry.length < foffset + fsize)
return create_app_result::app_corrupt;
if (fsize > vsize)
return create_app_result::app_corrupt;
- if (vaddr == 0)
+
+ if (vaddr < 0x200000)
return create_app_result::app_corrupt;
- uint64_t vpages = (vsize - 1) / 0x200000 + 1;
+ uint64_t vpages = (vsize - 1) / 4096 + 1;
- if (vaddr + vpages * 0x200000 > ((1 << 30) - (4 << 20)))
+ if (vaddr + vpages * 4096 > 0x8000000000)
return create_app_result::app_corrupt;
load_info info = {
@@ -217,47 +209,55 @@ namespace mercury::kernel::application {
for (unsigned i = 0; i < load_infos.count; ++i) {
const auto &info = load_infos.buffer[i];
for (uint64_t j = 0; j < info.vpages; ++j) {
- uint64_t paddr = paging::take_2mib_pram_page();
- out->map_page(info.vaddr + j * 0x200000, paddr,
+ uint64_t paddr = paging::take_pram_page();
+ out->map_page(info.vaddr + j * 4096, paddr,
info.writable, info.executable, true);
- for (int k = 0; k < 512; ++k) {
- uint64_t offset_in_segment = j * 0x200000 + k * 4096;
- uint64_t kvaddr = paging::find_unmapped_vram_region(1);
- paging::map_kernel_page(paddr + k * 4096, kvaddr, true, false);
- storage::fs_result result = storage::fs_result::success;
- if (info.fsize > offset_in_segment) {
- if (info.fsize >= offset_in_segment + 4096)
- result = file.read_file(
- info.foffset + offset_in_segment, 4096, (void *)kvaddr);
- else {
- int to_read = info.fsize - offset_in_segment;
- result = file.read_file(
- info.foffset + offset_in_segment, to_read, (void *)kvaddr);
- uint8_t *blank = (uint8_t *)(kvaddr + to_read);
- for (int i = 0; i < 4096 - to_read; ++i)
- blank[i] = 0;
- }
- }
+ uint64_t kvaddr = paging::find_unmapped_vram_region(1);
+ paging::map_kernel_page(paddr, kvaddr, true, false);
+ storage::fs_result result = storage::fs_result::success;
+ if (info.fsize > j * 4096) {
+ if (info.fsize >= j * 4096 + 4096)
+ result = file.read_file(
+ info.foffset + j * 4096, 4096, (void *)kvaddr);
else {
- uint8_t *blank = (uint8_t *)kvaddr;
- for (int i = 0; i < 4096; ++i)
+ int to_read = info.fsize - j * 4096;
+ result = file.read_file(
+ info.foffset + j * 4096, to_read, (void *)kvaddr);
+ uint8_t *blank = (uint8_t *)(kvaddr + to_read);
+ for (int i = 0; i < 4096 - to_read; ++i)
blank[i] = 0;
}
- paging::unmap_kernel_page(kvaddr);
- if (result == storage::fs_result::device_error) {
- delete out;
- return create_app_result::device_error;
- }
- if (result == storage::fs_result::fs_corrupt) {
- delete out;
- return create_app_result::fs_corrupt;
- }
+ }
+ else {
+ uint8_t *blank = (uint8_t *)kvaddr;
+ for (int i = 0; i < 4096; ++i)
+ blank[i] = 0;
+ }
+ paging::unmap_kernel_page(kvaddr);
+ if (result == storage::fs_result::device_error) {
+ delete out;
+ return create_app_result::device_error;
+ }
+ if (result == storage::fs_result::fs_corrupt) {
+ delete out;
+ return create_app_result::fs_corrupt;
}
}
}
- out->create_stack();
- out->set_instruction_pointer(entry_point);
+ for (uint64_t vaddr = 0x1000; vaddr < 0x1ff000; vaddr += 4096) {
+ uint64_t paddr = paging::take_pram_page();
+ uint64_t kvaddr = paging::find_unmapped_vram_region(1);
+ paging::map_kernel_page(paddr, kvaddr, true, false);
+ uint8_t *p = (uint8_t *)kvaddr;
+ for (int i = 0; i < 4096; ++i)
+ p[i] = 0;
+ paging::unmap_kernel_page(kvaddr);
+ out->map_page(vaddr, paddr, true, false, true);
+ }
+
+ out->saved_regs.rsp = 0x1ff000;
+ out->saved_regs.rip = entry_point;
return create_app_result::success;