aboutsummaryrefslogtreecommitdiff
path: root/kernel/memory
diff options
context:
space:
mode:
authorXander <xander@biltopia.org>2023-07-21 21:36:54 +0200
committerXander <xander@biltopia.org>2023-07-21 21:36:54 +0200
commit1d25d9783127c181f79732eb5c20c680444af414 (patch)
tree5550f40b25af3e2722b5aaf6194f6df37ca9e092 /kernel/memory
parent9226c8bb581c363a552998663704c483f35f6aae (diff)
downloadats-os-1d25d9783127c181f79732eb5c20c680444af414.tar.xz
ats-os-1d25d9783127c181f79732eb5c20c680444af414.zip
Mainly refactoring
Diffstat (limited to 'kernel/memory')
-rw-r--r--kernel/memory/frame.dats57
-rw-r--r--kernel/memory/frame.sats4
2 files changed, 55 insertions, 6 deletions
diff --git a/kernel/memory/frame.dats b/kernel/memory/frame.dats
index 26ff412..8578e8f 100644
--- a/kernel/memory/frame.dats
+++ b/kernel/memory/frame.dats
@@ -2,6 +2,13 @@
#define ATS_DYNLOADFLAG 0
+macdef invalid_area = @{
+ base_addr = the_null_ptr,
+ length = 1,
+ type = 0u,
+ reserved = 0
+}
+
staload "./frame.sats"
staload "kernel/bootinfo/multiboot.sats"
@@ -12,19 +19,63 @@ implement containing_address(address) : frame_t =
counter = $UN.cast{size_t}(address) / i2sz(PAGE_SIZE)
}
-fn choose_next_area{l,k : agz}(pf : !frame_allocator_t@l, bf : !boot_info_t @ k | p : ptr l, b : ptr k) : void = let
+fn containing_area(area : memory_area_t) : frame_t = containing_address(ptr_add<uint8>(area.base_addr,area.length - i2sz(1)))
+//FIX: function doesn't handle no frames left
+fn choose_next_area{l,k : agz}(pf : !frame_allocator_t@l, bf : !boot_info_t @ k | p : ptr l, b : ptr k) : void = let
+ val length = get_memory_mappings_n(bf | b)
+ val next_free_frame = p->next_free_frame
+ fun loop {n,i : nat | i < n} {l : agz} .<n-i>. (bf : !boot_info_t @ l | b : ptr l, i : size_t i, n : size_t n) : memory_area_t = let
+ val entry = get_memory_mapping(bf | b ,i)
+ val fr = containing_area(entry)
+ in
+ if (entry.type = 1 && fr.num >= next_free_frame.num) then
+ entry
+ else if (i < n-1) then
+ loop (bf | b,succ(i),n)
+ else
+ entry //TODO: fix
+ end
in
+ if (length > 0) then //TODO: fix
+ p->current_area := loop(bf | b, i2sz(0), length);
+ if (p->current_area.length > 1) then
+ let
+ val start_frame = containing_address(p->current_area.base_addr)
+ in
+ if (p->next_free_frame.num < start_frame.num) then
+ p->next_free_frame := start_frame
+ end
end
+local
+
+var frame_allocator : frame_allocator_t
+
+in
+
+ implement frame_allocator_init() : allocptr = let
+ extern praxi __assert{l:addr} (ptr: ptr (l)): vtakeout0 (frame_allocator_t@l) // takeout proof UNSAFE:
+ prval (pf, fpf) = __assert (addr@frame_allocator)
+ val allocator = @{
+ next_free_frame = containing_address(the_null_ptr),
+ current_area = invalid_area
+ }
+ in
+
+ (pf, fpf | addr@frame_allocator)
+ end
+
+end
+
implement allocate_frame(pf , bf | p,b) : frame_t =
- if (p->current_area.length > 1) then let
+ if (p->current_area.type != 0u) then let
val area = p->current_area
val frame = @{ num = p->next_free_frame.num }
// last frame of current area
- val last_frame_area = containing_address(ptr_add<uint8>(area.base_addr,area.length - i2sz(1)))
+ val last_frame_area = containing_area(area)
in
if (frame.num > last_frame_area.num) then (
// all frames of current area are used, switch to next area
diff --git a/kernel/memory/frame.sats b/kernel/memory/frame.sats
index 441b85a..0d29298 100644
--- a/kernel/memory/frame.sats
+++ b/kernel/memory/frame.sats
@@ -9,7 +9,6 @@ typedef frame_t = @{
typedef frame_allocator_t = @{
next_free_frame = frame_t,
current_area = memory_area_t,
- num_areas = size_t,
kernel_start = frame_t,
kernel_end = frame_t,
multiboot_start = frame_t,
@@ -19,7 +18,6 @@ typedef frame_allocator_t = @{
vtypedef allocptr = [l : agz] (frame_allocator_t@l , frame_allocator_t@l -<lin,prf> void | ptr l)
fn containing_address {l : addr} (address : ptr l) : frame_t
-
fn allocate_frame {l : agz}{k : agz} (pf : !frame_allocator_t @ l >> frame_allocator_t @ l, bf : !boot_info_t @ k | p : ptr l, b : ptr k) : frame_t
-
fn deallocate_frame (p : !allocptr) : void
+fn frame_allocator_init() : allocptr