mem: improve slabbing to include flags

This commit is contained in:
Michael Shaw 2023-04-29 16:54:12 -04:00
commit e1108b3493
6 changed files with 30 additions and 14 deletions

View file

@ -5,10 +5,14 @@
#include <kernel/common.h> #include <kernel/common.h>
#include <kernel/mem.h> #include <kernel/mem.h>
#define SLAB_PAGE_ALIGN 0x01
struct slab { struct slab {
size_t size; /* size of each object */ size_t size; /* size of each object */
size_t num; /* number of objects in this slab */ size_t num; /* number of objects in this slab */
size_t free; /* number of free objects */ size_t free; /* number of free objects */
uint64_t flags;
size_t align;
uintptr_t *nextfree; uintptr_t *nextfree;
struct slab *next; struct slab *next;
struct slab *prev; struct slab *prev;
@ -21,7 +25,7 @@ struct kmap_entry {
struct kmap_entry *next; struct kmap_entry *next;
}; };
struct slab *slab_create(size_t size); struct slab *slab_create(size_t size, uint64_t flags);
void *slab_alloc(struct slab *slab); void *slab_alloc(struct slab *slab);
void slab_free(struct slab *slab, void *ptr); void slab_free(struct slab *slab, void *ptr);

View file

@ -62,12 +62,15 @@ void kmain(void)
char kmem[one_gb] ALIGN(0x1000); char kmem[one_gb] ALIGN(0x1000);
mem_early_init(kmem, one_gb); mem_early_init(kmem, one_gb);
kmalloc_init(); kmalloc_init();
_pic_init(); _pic_init();
printf("\n# "); void *kstack = buddy_alloc(KSTACK_SIZE);
void *kstack = kmalloc(KSTACK_SIZE);
uintptr_t ptr = (uintptr_t)kstack + KSTACK_SIZE - 8; uintptr_t ptr = (uintptr_t)kstack + KSTACK_SIZE - 8;
stack_init(ptr, ptr); stack_init(ptr, ptr);
} }
void kmain_post_stack_init()
{
printf("\n\n# ");
yield();
}

View file

@ -392,7 +392,7 @@ void mem_early_init(char *mem, size_t len)
/* copy the regions to the permanent regions table */ /* copy the regions to the permanent regions table */
memcpy(mem_regions, regions, sizeof(struct mem_region) * num_regions); memcpy(mem_regions, regions, sizeof(struct mem_region) * num_regions);
kmap_slab = slab_create(0x1000); kmap_slab = slab_create(0x1000, SLAB_PAGE_ALIGN);
/* initialize and populate the new permanent kernel page tables */ /* initialize and populate the new permanent kernel page tables */
alloc_page = kmap_alloc_page; alloc_page = kmap_alloc_page;

View file

@ -33,4 +33,6 @@ rbp_write:
stack_init: stack_init:
movq %rdi, %rsp movq %rdi, %rsp
movq %rsi, %rbp movq %rsi, %rbp
call yield __stack_init_no_escape:
call kmain_post_stack_init
jmp __stack_init_no_escape

View file

@ -5,6 +5,7 @@
#define SLAB_MIN_COUNT 256 #define SLAB_MIN_COUNT 256
#define SLAB_MIN_SIZE 0x10000ull /* 64 KiB */ #define SLAB_MIN_SIZE 0x10000ull /* 64 KiB */
#define SLAB_ALIGN 7
size_t slab_sizes[] = { 16, 32, 64, 128, 256, 512, 1024, 2048 }; size_t slab_sizes[] = { 16, 32, 64, 128, 256, 512, 1024, 2048 };
struct slab *slab_cache[ARRAY_SIZE(slab_sizes)]; struct slab *slab_cache[ARRAY_SIZE(slab_sizes)];
@ -15,13 +16,13 @@ struct kmap_entry *kmem_map = NULL;
static inline void slab_range(struct slab *slab, uintptr_t *o_start, uintptr_t *o_end) static inline void slab_range(struct slab *slab, uintptr_t *o_start, uintptr_t *o_end)
{ {
uintptr_t sslab = (uintptr_t)slab; uintptr_t sslab = (uintptr_t)slab;
sslab = (sslab | npow2(slab->size)) + 1; sslab = (sslab | slab->align) + 1;
*o_start = sslab; *o_start = sslab;
*o_end = sslab + (slab->size * slab->num); *o_end = sslab + (slab->size * slab->num);
} }
struct slab *slab_create(size_t size) struct slab *slab_create(size_t size, uint64_t flags)
{ {
if (size < sizeof(uintptr_t)) if (size < sizeof(uintptr_t))
return NULL; return NULL;
@ -39,13 +40,19 @@ struct slab *slab_create(size_t size)
uintptr_t aret = (uintptr_t)ret; uintptr_t aret = (uintptr_t)ret;
uintptr_t start = (uintptr_t)ret + sizeof(struct slab); uintptr_t start = (uintptr_t)ret + sizeof(struct slab);
start |= (npow2(size) - 1); if (flags & SLAB_PAGE_ALIGN)
ret->align = 0xFFF;
else
ret->align = SLAB_ALIGN;
start |= ret->align;
start += 1; start += 1;
ret->num = (((aret + asize) - start) / size); ret->num = (((aret + asize) - start) / size);
ret->size = size; ret->size = size;
ret->next = NULL; ret->next = NULL;
ret->prev = NULL; ret->prev = NULL;
ret->flags = flags;
ret->nextfree = (uintptr_t *)start; ret->nextfree = (uintptr_t *)start;
ret->free = ret->num; ret->free = ret->num;
@ -67,7 +74,7 @@ void *slab_alloc(struct slab *slab)
if (slab->nextfree == NULL) { if (slab->nextfree == NULL) {
if (slab->next == NULL) { if (slab->next == NULL) {
slab->next = slab_create(slab->size); slab->next = slab_create(slab->size, slab->flags);
if (slab->next == NULL) if (slab->next == NULL)
return NULL; return NULL;
slab->next->prev = slab; slab->next->prev = slab;
@ -120,8 +127,8 @@ void slab_free(struct slab *slab, void *ptr)
void kmalloc_init() void kmalloc_init()
{ {
for (size_t i = 0; i < ARRAY_SIZE(slab_sizes); i++) for (size_t i = 0; i < ARRAY_SIZE(slab_sizes); i++)
slab_cache[i] = slab_create(slab_sizes[i]); slab_cache[i] = slab_create(slab_sizes[i], 0);
kmalloc_slab = slab_create(sizeof(struct kmap_entry)); kmalloc_slab = slab_create(sizeof(struct kmap_entry), 0);
} }
void *kmalloc(size_t size) void *kmalloc(size_t size)