summaryrefslogtreecommitdiffstats
path: root/mem
diff options
context:
space:
mode:
authorJon Santmyer <jon@jonsantmyer.com>2024-03-15 13:16:02 -0400
committerJon Santmyer <jon@jonsantmyer.com>2024-03-15 13:16:02 -0400
commitdd5d9e1d48396cbc226ff14fe557a55613c91fcb (patch)
treeb0ec9f54b80f26777ac08b723b42c1c64c5a2bda /mem
parentf46ab8ca2050ee77edf6e6b979875426bdaf29dc (diff)
downloadjove-kernel-dd5d9e1d48396cbc226ff14fe557a55613c91fcb.tar.gz
jove-kernel-dd5d9e1d48396cbc226ff14fe557a55613c91fcb.tar.bz2
jove-kernel-dd5d9e1d48396cbc226ff14fe557a55613c91fcb.zip
better buddy memory allocator
Diffstat (limited to 'mem')
-rw-r--r--mem/buddymap.c151
-rw-r--r--mem/buddymap.h21
-rw-r--r--mem/memory.c10
-rw-r--r--mem/memory.h12
-rw-r--r--mem/phys.c13
-rw-r--r--mem/zone.c97
-rw-r--r--mem/zone.h52
7 files changed, 170 insertions, 186 deletions
diff --git a/mem/buddymap.c b/mem/buddymap.c
deleted file mode 100644
index 5165876..0000000
--- a/mem/buddymap.c
+++ /dev/null
@@ -1,151 +0,0 @@
-#include "buddymap.h"
-#include "lib/string.h"
-#include "boot/boot.h"
-#include "io/log.h"
-#include <stdbool.h>
-
-#define ENTRY_BITS (sizeof(uintmax_t) * 8)
-#define ENTRY_SIZE (ENTRY_BITS * PAGESIZE)
-#define ENTRY_COUNT (MEMMAP_BUDDY_LIMIT / ENTRY_SIZE)
-#define BUDDY_LAYERS 4
-
-uintmax_t s_buddy_l0[ENTRY_COUNT];
-uintmax_t s_buddy_l1[ENTRY_COUNT << 1];
-uintmax_t s_buddy_l2[ENTRY_COUNT << 2];
-uintmax_t s_buddy_l3[ENTRY_COUNT << 3];
-uintmax_t *s_buddies[BUDDY_LAYERS] = {
- s_buddy_l0,
- s_buddy_l1,
- s_buddy_l2,
- s_buddy_l3
-};
-size_t s_buddies_lastfree[BUDDY_LAYERS] = { 1, 1, 1, 1 };
-
-static bool
-s_buddy_test(size_t l, size_t i)
-{
- return (s_buddies[l][i / (ENTRY_BITS)] & (1ULL << (i % (ENTRY_BITS)))) > 0;
-}
-
-static void
-s_buddy_set(size_t l, size_t i)
-{
- size_t j = i << l;
- size_t w = 1 << l;
- for(int layer = 0; layer < BUDDY_LAYERS; layer++)
- {
- if(w == 0) w = 1;
- for(size_t bit = 0; bit < w; bit++) {
- size_t entry = (j + bit) / ENTRY_BITS;
- s_buddies[layer][entry] |= (1ULL << (j + bit % ENTRY_BITS));
- }
- j >>= 1;
- w >>= 1;
- }
-}
-
-static void
-s_buddy_unset(size_t l, size_t i)
-{
- size_t j = i << l;
- size_t w = 1 << l;
- bool free_upper = false;
- for(int layer = 0; layer < BUDDY_LAYERS; layer++)
- {
- if(w == 0) {
- size_t lower = (j << 1) % ENTRY_BITS;
- size_t other = (lower + 1);
- size_t entry = (j << 1) / ENTRY_BITS;
- if((s_buddies[layer-1][entry] & (1ULL << lower)) > 0 &&
- (s_buddies[layer-1][entry] & (1ULL << other)) > 0)
- s_buddies[layer][entry >> 1] &= ~(1ULL << (j % ENTRY_BITS));
- }
-
- for(size_t bit = 0; bit < w; bit++) {
- size_t entry = j / ENTRY_BITS;
- s_buddies[layer][entry] |= (1ULL << bit);
- }
- j >>= 1;
- w >>= 1;
- }
-}
-
-void
-mem_buddy_set_range(uintptr_t base, size_t length)
-{
- for(int l = 0; l < BUDDY_LAYERS; l++) {
- size_t bits = (length / PAGESIZE) >> l;
- size_t biti = (base / PAGESIZE) >> l;
-
- if(bits == 0) bits = 1;
- for(size_t i = 0; i < bits; i++) {
- size_t entry = (biti + i) / ENTRY_BITS;
- s_buddies[l][entry] |= 1ULL << ((biti + i) % ENTRY_BITS);
- }
- }
-}
-
-void
-mem_buddy_free_range(uintptr_t base, size_t length)
-{
- for(int l = 0; l < BUDDY_LAYERS; l++) {
- size_t bits = (length / PAGESIZE) >> l;
- size_t biti = (base / PAGESIZE) >> l;
- size_t bitbase = (biti * PAGESIZE) << l;
-
- if(bits == 0) continue;
- for(size_t i = 0; i < bits; i++, bitbase += (PAGESIZE << l)) {
- if(bitbase < base) continue;
- size_t entry = (biti + i) / ENTRY_BITS;
- s_buddies[l][entry] &= ~(1ULL << ((biti+ i) % ENTRY_BITS));
- }
- }
-}
-
-
-uintptr_t
-mem_buddy_takefree(size_t l)
-{
- uintmax_t *layer = s_buddies[l];
- size_t lastfree = s_buddies_lastfree[l];
- if(s_buddy_test(l, lastfree)) lastfree = 0;
- size_t entries = ENTRY_COUNT >> l;
- for(size_t i = lastfree / ENTRY_BITS; i < entries; i++) {
- uintmax_t entry = layer[i];
- if(entry == (uintmax_t)-1LL) continue;
- for(size_t j = 0; j < ENTRY_BITS; j++) {
- if((entry & (1ULL << j)) == 0) {
- size_t bit = (i * ENTRY_BITS) + j;
- s_buddies_lastfree[l] = bit + 1;
- s_buddy_set(l, bit);
- return bit * (PAGESIZE << l);
- }
- }
- }
- return 0;
-}
-
-void
-mem_buddy_setup()
-{
- memset(s_buddy_l0, 0xFF, sizeof(s_buddy_l0));
- memset(s_buddy_l1, 0xFF, sizeof(s_buddy_l1));
- memset(s_buddy_l2, 0xFF, sizeof(s_buddy_l2));
- memset(s_buddy_l3, 0xFF, sizeof(s_buddy_l3));
-
- for(int i = 0; i < boot_memorymap.count; i++) {
- struct MemoryMapEntry *entry = &boot_memorymap.entries[i];
- klogf("%2i\t%#016X -> %#016X (%i)\n",
- i, entry->base, entry->base + entry->length, entry->usable);
- if(entry->base > MEMMAP_BUDDY_LIMIT) continue;
- size_t length = entry->length;
- if(entry->base + length > MEMMAP_BUDDY_LIMIT) length = MEMMAP_BUDDY_LIMIT - (entry->base + length);
- if(entry->usable)
- mem_buddy_free_range(entry->base, entry->length);
- }
-
- s_buddies[0][0] |= 1;
- s_buddies[1][0] |= 1;
- s_buddies[2][0] |= 1;
- s_buddies[3][0] |= 1;
-}
diff --git a/mem/buddymap.h b/mem/buddymap.h
deleted file mode 100644
index 2f4f5dc..0000000
--- a/mem/buddymap.h
+++ /dev/null
@@ -1,21 +0,0 @@
-#ifndef JOVE_MEMORY_BUDDYMAP_H
-#define JOVE_MEMORY_BUDDYMAP_H 1
-
-#include "memory.h"
-#include <stdint.h>
-#include <stddef.h>
-
-#define MEMMAP_BUDDY_LIMIT (4 * GiB)
-
-void mem_buddy_set_range(uintptr_t base, size_t length);
-void mem_buddy_free_range(uintptr_t base, size_t length);
-uintptr_t mem_buddy_takefree(size_t layer);
-
-#define mem_buddy_takefree_4k() mem_buddy_takefree(0)
-#define mem_buddy_takefree_8k() mem_buddy_takefree(1)
-#define mem_buddy_takefree_16k() mem_buddy_takefree(2)
-#define mem_buddy_takefree_32k() mem_buddy_takefree(3)
-
-void mem_buddy_setup(void);
-
-#endif
diff --git a/mem/memory.c b/mem/memory.c
new file mode 100644
index 0000000..26bbbd8
--- /dev/null
+++ b/mem/memory.c
@@ -0,0 +1,10 @@
+#include "memory.h"
+#include "zone.h"
+
+void
+mem_setup(void)
+{
+ mem_zone_setup_standard();
+ mem_paging_setup();
+ mem_slab_setup();
+}
diff --git a/mem/memory.h b/mem/memory.h
index 3956852..41323ed 100644
--- a/mem/memory.h
+++ b/mem/memory.h
@@ -32,12 +32,12 @@ bool mem_check_ptr(const void *ptr);
* @param user flag to mark page as user accessable*/
void mem_ensure_range(uintptr_t from, uintptr_t to, bool rw, bool user);
-/**Make sure the range indicated is available in memory for specified pd
+/** Make sure the range indicated is available in memory for specified pd
* If necessary, allocate new pages using the passed flags
* @param pd pointer to page directory to edit
- * @param from start of the range.
- * @param to end of the range.
- * @param rw flag to mark page is writeable.
+ * @param from start of the range
+ * @param to end of the range
+ * @param rw flag to mark page is writeable
* @param user flag to mark page as user accessable*/
void mem_ensure_range_for(void *pd, uintptr_t from, uintptr_t to, bool rw, bool user);
@@ -52,7 +52,9 @@ void mem_free(void *ptr);
/*Physical*/
-physptr_t mem_phys_take4k(void);
+physptr_t mem_phys_alloc(size_t pages);
void mem_phys_reserve(physptr_t start, size_t len);
+void mem_setup(void);
+
#endif
diff --git a/mem/phys.c b/mem/phys.c
index e56a4d6..00f3531 100644
--- a/mem/phys.c
+++ b/mem/phys.c
@@ -1,14 +1,9 @@
#include "memory.h"
-#include "buddymap.h"
+#include "zone.h"
physptr_t
-mem_phys_take4k(void)
+mem_phys_alloc(size_t pages)
{
- return mem_buddy_takefree_4k();
-}
-
-void
-mem_phys_reserve(physptr_t start, size_t len)
-{
- mem_buddy_set_range(start, len);
+ physptr_t ptr = mem_zone_alloc(MEM_ZONE_STANDARD, pages);
+ return ptr;
}
diff --git a/mem/zone.c b/mem/zone.c
new file mode 100644
index 0000000..489383a
--- /dev/null
+++ b/mem/zone.c
@@ -0,0 +1,97 @@
+#include "zone.h"
+#include "memory.h"
+#include "boot/boot.h"
+#include "lib/string.h"
+#include "io/log.h"
+
+#define MEM_ZONE_STANDARD_PAGES (MEM_ZONE_STANDARD_LIMIT >> 12)
+
+static uintmax_t
+ s_zone_standard_freemap_blocks_flat[BUDDY_BLOCKS_FOR(MEM_ZONE_STANDARD_PAGES)];
+static uintmax_t*
+ s_zone_standard_freemap_blocks[MEM_BUDDY_ORDERS];
+
+static struct PhysicalMemoryZone s_zones[MEM_ZONE_COUNT] =
+{
+ {
+ .name = "Standard",
+ .base = MEM_ZONE_STANDARD_BASE,
+ .limit = MEM_ZONE_STANDARD_LIMIT,
+ .npages_total = MEM_ZONE_STANDARD_PAGES,
+ .npages_free = 0,
+ .freemap = {
+ .orders = MEM_BUDDY_ORDERS,
+ .bits = MEM_ZONE_STANDARD_PAGES,
+ .free = 0,
+ .blocks = s_zone_standard_freemap_blocks
+ }
+ },
+ {
+ .name = "Higher",
+ .base = MEM_ZONE_HIGHER_BASE,
+ .limit = -1,
+ .freemap = {
+ .orders = MEM_BUDDY_ORDERS
+ }
+ }
+};
+
+void
+mem_zone_resv(size_t zone, uintptr_t base, uintptr_t limit)
+{
+ size_t base_off = base % PAGESIZE;
+ size_t limit_off = limit % PAGESIZE;
+
+ if(base_off > 0) base += (PAGESIZE - base_off);
+ limit -= limit_off;
+
+ buddy_mark_range(&s_zones[zone].freemap, base >> 12, limit >> 12);
+}
+
+void
+mem_zone_free(size_t zone, uintptr_t base, uintptr_t limit)
+{
+ size_t base_off = base % PAGESIZE;
+ size_t limit_off = limit % PAGESIZE;
+
+ if(base_off > 0) base += (PAGESIZE - base_off);
+ limit -= limit_off;
+
+ size_t npages = (limit - base) >> 12;
+ s_zones[zone].npages_free += npages;
+ buddy_free_range(&s_zones[zone].freemap, base >> 12, limit >> 12);
+}
+
+uintptr_t
+mem_zone_alloc(size_t zone, size_t pages)
+{
+ struct PhysicalMemoryZone *pmz = &s_zones[zone];
+ intmax_t pagei = buddy_alloc(&pmz->freemap, pages);
+ if(pagei < 0) return 0;
+
+ return (((uintmax_t)pagei) << 12) + pmz->base;
+}
+
+void
+mem_zone_setup_standard(void)
+{
+ struct PhysicalMemoryZone *standard_zone = &s_zones[MEM_ZONE_STANDARD];
+ uintmax_t *map_block_layer_base = s_zone_standard_freemap_blocks_flat;
+ for(size_t i = 0; i < MEM_BUDDY_ORDERS; i++) {
+ size_t layer_entries = (standard_zone->freemap.bits / BUDDY_BLOCK_BITS) >> i;
+ standard_zone->freemap.blocks[i] = map_block_layer_base;
+ memset(map_block_layer_base, 0xFF, layer_entries * sizeof(uintmax_t));
+ map_block_layer_base = &map_block_layer_base[layer_entries];
+ }
+
+ for(int i = 0; i < boot_memorymap.count; i++) {
+ struct MemoryMapEntry *entry = &boot_memorymap.entries[i];
+ klogf("%2i\t%#016X -> %#016X (%i)\n",
+ i, entry->base, entry->base + entry->length, entry->usable);
+ if(entry->base > MEM_ZONE_STANDARD_LIMIT) continue;
+ size_t limit = entry->base + entry->length;
+ if(limit > MEM_ZONE_STANDARD_LIMIT) limit = MEM_ZONE_STANDARD_LIMIT;
+ if(entry->usable)
+ mem_zone_free(MEM_ZONE_STANDARD, entry->base, limit);
+ }
+}
diff --git a/mem/zone.h b/mem/zone.h
new file mode 100644
index 0000000..7e863bf
--- /dev/null
+++ b/mem/zone.h
@@ -0,0 +1,52 @@
+#ifndef JOVE_MEM_ZONE_H
+#define JOVE_MEM_ZONE_H 1
+
+#include <stdint.h>
+#include "lib/buddymap.h"
+
+enum {
+ MEM_ZONE_STANDARD = 0, /* First GiB of physical memory. */
+ MEM_ZONE_HIGHER,
+ MEM_ZONE_COUNT
+};
+
+#define MEM_ZONE_STANDARD_BASE 0
+#define MEM_ZONE_STANDARD_LIMIT (1 * GiB)
+#define MEM_ZONE_HIGHER_BASE MEM_ZONE_STANDARD_LIMIT
+
+#define MEM_BUDDY_ORDERS 12
+struct PhysicalMemoryZone
+{
+ const char *name;
+
+ uintptr_t base;
+ uintptr_t limit;
+
+ size_t npages_total;
+ size_t npages_free;
+
+ struct BuddyMap freemap;
+};
+
+/** Using a given zone, reserve a range of physical addresses
+ * @param zone identifier of zone to modify
+ * @param base starting address to reserve
+ * @param limit ending address to reserve*/
+void mem_zone_resv(size_t zone, uintptr_t base, uintptr_t limit);
+
+/** Using a given zone, free a range of physical addresses
+ * @param zone identifier of zone to modify
+ * @param base starting address to free
+ * @param limit ending address to free*/
+void mem_zone_free(size_t zone, uintptr_t base, uintptr_t limit);
+
+/** Allocate a number of pages from the given zone
+ * @param zone identifier of the zone to modify
+ * @param pages number of pages to allocate
+ * @return physical memory address of allocation
+ * zero if allocation failed*/
+uintptr_t mem_zone_alloc(size_t zone, size_t pages);
+
+void mem_zone_setup_standard(void);
+
+#endif