aboutsummaryrefslogtreecommitdiff
path: root/kernel/memory
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/memory')
-rw-r--r--kernel/memory/frame.dats57
-rw-r--r--kernel/memory/frame.sats25
2 files changed, 82 insertions, 0 deletions
diff --git a/kernel/memory/frame.dats b/kernel/memory/frame.dats
new file mode 100644
index 0000000..26ff412
--- /dev/null
+++ b/kernel/memory/frame.dats
@@ -0,0 +1,57 @@
+#include "kernel/prelude/kernel_prelude.hats"
+
+#define ATS_DYNLOADFLAG 0
+
+staload "./frame.sats"
+staload "kernel/bootinfo/multiboot.sats"
+
+staload UN = "prelude/SATS/unsafe.sats"
+
+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
+
+in
+
+end
+
+implement allocate_frame(pf , bf | p,b) : frame_t =
+ if (p->current_area.length > 1) 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)))
+ in
+ if (frame.num > last_frame_area.num) then (
+ // all frames of current area are used, switch to next area
+ choose_next_area(pf, bf | p,b);
+ allocate_frame(pf,bf | p,b)
+ ) else if (frame.num > p->kernel_start.num && frame.num <= p->kernel_end.num) then (
+ // frame is used by kernel
+ p->next_free_frame := @{num = p->kernel_end.num + 1};
+ allocate_frame(pf,bf | p,b)
+ ) else if (frame.num > p->multiboot_start.num && frame.num <= p->multiboot_end.num) then (
+ // frame is used by multiboot info structure
+ p->next_free_frame := @{num = p->multiboot_end.num + 1};
+ allocate_frame(pf,bf | p,b)
+ ) else (
+ // frame is unused, increment `next_free_frame` and return it
+ p->next_free_frame.num := succ(p->next_free_frame.num);
+ frame
+ );
+ end
+ else
+ @{num = i2sz(0)} // No free frames left
+
+
+
+implement deallocate_frame(p) : void = let
+
+in
+
+end
+
diff --git a/kernel/memory/frame.sats b/kernel/memory/frame.sats
new file mode 100644
index 0000000..441b85a
--- /dev/null
+++ b/kernel/memory/frame.sats
@@ -0,0 +1,25 @@
+#define PAGE_SIZE 4096
+
+staload "kernel/bootinfo/multiboot.sats"
+
+typedef frame_t = @{
+ num = size_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,
+ multiboot_end = frame_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