From 74a58d6641734ff13242eb63c0688e955771b1f3 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Thu, 20 Apr 2023 16:06:49 -0400 Subject: [PATCH] mem: add basic slab allocator --- include/kernel/mem.h | 1 + include/kernel/slab.h | 20 +++++++++++ include/math.h | 12 +++++++ mem/{cr3.S => reg.S} | 0 mem/slab.c | 84 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 117 insertions(+) create mode 100644 include/kernel/slab.h rename mem/{cr3.S => reg.S} (100%) create mode 100644 mem/slab.c diff --git a/include/kernel/mem.h b/include/kernel/mem.h index c1749c2..960f263 100644 --- a/include/kernel/mem.h +++ b/include/kernel/mem.h @@ -93,6 +93,7 @@ uint64_t rsp_read(); void rsp_write(uint64_t rsp); void *buddy_alloc(size_t size); void buddy_free(void *paddr_hhdm); +void kmap(paddr_t paddr, uint64_t vaddr, size_t len, uint64_t attr); void mem_early_init(char *mem, size_t len); diff --git a/include/kernel/slab.h b/include/kernel/slab.h new file mode 100644 index 0000000..e5418c5 --- /dev/null +++ b/include/kernel/slab.h @@ -0,0 +1,20 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _SLAB_H_ +#define _SLAB_H_ + +#include +#include + +struct slab { + size_t size; /* size of each object */ + size_t num; /* number of objects in this slab */ + size_t free; /* number of free objects */ + uintptr_t *nextfree; + struct slab *next; +}; + +struct slab *slab_create(size_t size); +void *slab_alloc(struct slab *slab); +void slab_free(struct slab *slab, void *ptr); + +#endif /* _SLAB_H_ */ diff --git a/include/math.h b/include/math.h index 91d42bc..3694d99 100644 --- a/include/math.h +++ b/include/math.h @@ -11,4 +11,16 @@ void bitmap_set(char *bitmap, size_t n); void bitmap_clear(char *bitmap, size_t n); uint8_t bitmap_get(char *bitmap, size_t n); +#define MIN(a, b) ({ \ + typeof(a) _a = (a); \ + typeof(b) _b = (b); \ + _a < _b ? _a : _b; \ +}) + +#define MAX(a, b) ({ \ + typeof(a) _a = (a); \ + typeof(b) _b = (b); \ + _a > _b ? _a : _b; \ +}) + #endif /* _MATH_H_ */ diff --git a/mem/cr3.S b/mem/reg.S similarity index 100% rename from mem/cr3.S rename to mem/reg.S diff --git a/mem/slab.c b/mem/slab.c new file mode 100644 index 0000000..840055f --- /dev/null +++ b/mem/slab.c @@ -0,0 +1,84 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#include +#include +#include + +#define SLAB_MIN_COUNT 16 + +void slab_range(struct slab *slab, uintptr_t *o_start, uintptr_t *o_end) +{ + uintptr_t sslab = (uintptr_t)slab; + sslab = (sslab | 7) + 1; + + *o_start = sslab; + *o_end = sslab + (slab->size * slab->num); +} + +struct slab *slab_create(size_t size) +{ + if (size < sizeof(uintptr_t)) + return NULL; + + /* account for slab header and eight bytes of alignment */ + size_t est = npow2(size * SLAB_MIN_COUNT + sizeof(struct slab) + 8); + size_t asize = MAX(est, 0x1000ull); + + struct slab *ret = buddy_alloc(asize); + if (ret == NULL) + return NULL; + + /* slab area start position */ + size_t start = (sizeof(struct slab) | 7) + 1; + + ret->nextfree = (uintptr_t *)((uintptr_t)ret + start); + ret->size = size; + + uintptr_t *ptr = ret->nextfree; + while (ptr != NULL) { + if (ptr > (uintptr_t *)((uintptr_t)ret + asize - size)) { + *ptr = (uintptr_t)NULL; + ptr = NULL; + } else { + *ptr = (uintptr_t)ptr + size; + ptr = (uintptr_t *)*ptr; + } + } + + return ret; +} + +void *slab_alloc(struct slab *slab) +{ + if (slab->nextfree == NULL) { + if (slab->next == NULL) { + slab->next = slab_create(slab->size); + if (slab->next == NULL) + return NULL; + } + return slab_alloc(slab->next); + } + + slab->free--; + uintptr_t *ret = slab->nextfree; + slab->nextfree = (uintptr_t *)*slab->nextfree; + + return ret; +} + +void slab_free(struct slab *slab, void *ptr) +{ + if (slab == NULL) + return; + if (ptr == NULL) + return; + + uintptr_t low; + uintptr_t high; + slab_range(slab, &low, &high); + + if ((uintptr_t)ptr < low || (uintptr_t)ptr > high) + slab_free(slab->next, ptr); + + *(uintptr_t *)ptr = (uintptr_t)slab->nextfree; + slab->nextfree = ptr; +}