slab: upgrade kmalloc to allow DMA regions

these DMA regions are only meant to comply with certain AHCI
requirements, and are not yet general purpose
This commit is contained in:
Michael Shaw 2023-05-08 01:14:11 -04:00
commit 8544efe1ab
7 changed files with 89 additions and 36 deletions

View file

@ -143,7 +143,7 @@ bool ahci_access_sectors(struct sata_device *dev, paddr_t lba, uint16_t count, p
memset(cmdtbl, 0, sizeof(struct hba_cmd_tbl) + (cmdheader->prdt_len - 1) * sizeof(struct hba_prdt_entry)); memset(cmdtbl, 0, sizeof(struct hba_cmd_tbl) + (cmdheader->prdt_len - 1) * sizeof(struct hba_prdt_entry));
int i; int i;
for (i = 0; i < cmdheader->prdt_len - 1; i++) { for (i = 0; i < cmdheader->prdt_len; i++) {
cmdtbl->prdt_entry[i].data_base = buf; cmdtbl->prdt_entry[i].data_base = buf;
cmdtbl->prdt_entry[i].byte_count = 8 * 1024 - 1; cmdtbl->prdt_entry[i].byte_count = 8 * 1024 - 1;
cmdtbl->prdt_entry[i].interrupt = 1; cmdtbl->prdt_entry[i].interrupt = 1;
@ -151,10 +151,6 @@ bool ahci_access_sectors(struct sata_device *dev, paddr_t lba, uint16_t count, p
count -= 16; count -= 16;
} }
cmdtbl->prdt_entry[i].data_base = buf;
cmdtbl->prdt_entry[i].byte_count = (count << 9) - 1;
cmdtbl->prdt_entry[i].interrupt = 1;
struct fis_reg_h2d *cmdfis = (struct fis_reg_h2d *)(&cmdtbl->cfis); struct fis_reg_h2d *cmdfis = (struct fis_reg_h2d *)(&cmdtbl->cfis);
cmdfis->fis_type = FIS_REG_H2D; cmdfis->fis_type = FIS_REG_H2D;
cmdfis->cmd_mode = 1; cmdfis->cmd_mode = 1;

View file

@ -99,7 +99,7 @@ struct pci_device *pci_find_device(uint8_t class, uint8_t subclass)
void pci_init() void pci_init()
{ {
pci_devices = kzalloc(sizeof(struct pci_device) * MAX_PCI_DEVICES); pci_devices = kzalloc(sizeof(struct pci_device) * MAX_PCI_DEVICES, ALLOC_KERN);
pci_device_count = 0; pci_device_count = 0;
for (int bus = 0; bus < 256; bus++) for (int bus = 0; bus < 256; bus++)

View file

@ -17,6 +17,7 @@
#define ALIGN(_a) __attribute__((aligned(_a))) #define ALIGN(_a) __attribute__((aligned(_a)))
struct page { /* I would rather refer to this as a struct */ struct page { /* I would rather refer to this as a struct */
union { union {
struct { struct {
@ -81,6 +82,7 @@ extern size_t max_regions;
extern struct page *pml4; extern struct page *pml4;
extern paddr_t kcr3; extern paddr_t kcr3;
extern struct page kdefault_attrs;
uint64_t cr3_read(); uint64_t cr3_read();
void cr3_write(uint64_t cr3); void cr3_write(uint64_t cr3);

View file

@ -6,9 +6,14 @@
#include <kernel/mem.h> #include <kernel/mem.h>
#define SLAB_PAGE_ALIGN 0x01 #define SLAB_PAGE_ALIGN 0x01
#define SLAB_DMA_64K 0x02
#define ALLOC_KERN 0x01
#define ALLOC_DMA 0x02
struct slab { struct slab {
size_t size; /* size of each object */ size_t size; /* size of each object */
size_t tsize; /* total size of this slab */
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; uint64_t flags;
@ -30,8 +35,8 @@ void *slab_alloc(struct slab *slab);
void slab_free(struct slab *slab, void *ptr); void slab_free(struct slab *slab, void *ptr);
void kmalloc_init(); void kmalloc_init();
void *kmalloc(size_t size); void *kmalloc(size_t size, uint64_t flags);
void *kzalloc(size_t size); void *kzalloc(size_t size, uint64_t flags);
void kfree(void *ptr); void kfree(void *ptr);
#endif /* _SLAB_H_ */ #endif /* _SLAB_H_ */

View file

@ -73,10 +73,10 @@ void kmain(void)
struct limine_file *kfile = (struct limine_file *)((paddr_t)resp->kernel_file | hhdm_start); struct limine_file *kfile = (struct limine_file *)((paddr_t)resp->kernel_file | hhdm_start);
kcmdline = kzalloc(strlen(kfile->cmdline) + 1); kcmdline = kzalloc(strlen(kfile->cmdline) + 1, ALLOC_KERN);
strcpy(kcmdline, kfile->cmdline); strcpy(kcmdline, kfile->cmdline);
kpath = kzalloc(strlen(kfile->path) + 1); kpath = kzalloc(strlen(kfile->path) + 1, ALLOC_KERN);
strcpy(kpath, kfile->path); strcpy(kpath, kfile->path);
kroot_disk = kfile->gpt_disk_uuid; kroot_disk = kfile->gpt_disk_uuid;

View file

@ -21,6 +21,8 @@ struct page *pml4;
struct mem_region *mem_regions; struct mem_region *mem_regions;
size_t num_regions; size_t num_regions;
struct page kdefault_attrs;
paddr_t kcr3 = 0; paddr_t kcr3 = 0;
static void *(*alloc_page)(void); static void *(*alloc_page)(void);
@ -368,6 +370,8 @@ void mem_early_init(char *mem, size_t len)
attrs.rw = 1; attrs.rw = 1;
attrs.xd = 0; attrs.xd = 0;
kdefault_attrs.val = attrs.val;
/* These page tables are initialized on the kernel stack /* These page tables are initialized on the kernel stack
* *
* These will be used to initialize the new memory regions we have * These will be used to initialize the new memory regions we have

View file

@ -9,6 +9,8 @@
static size_t slab_sizes[] = { 16, 32, 64, 128, 256, 512, 1024, 2048, 4096 }; static size_t slab_sizes[] = { 16, 32, 64, 128, 256, 512, 1024, 2048, 4096 };
static struct slab *slab_cache[ARRAY_SIZE(slab_sizes)]; static struct slab *slab_cache[ARRAY_SIZE(slab_sizes)];
static size_t dma_sizes[] = { 0x1000, 0x10000, 0x100000, 0x1000000 };
static struct slab *slab_cache_dma[ARRAY_SIZE(slab_sizes)];
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)
{ {
@ -37,16 +39,23 @@ struct slab *slab_create(size_t size, uint64_t flags)
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);
if (flags & SLAB_PAGE_ALIGN) if (flags & SLAB_PAGE_ALIGN) {
ret->align = 0xFFF; ret->align = 0xFFF;
else } else if (flags & SLAB_DMA_64K) {
ret->align = 0xFFFF;
struct page attrs = kdefault_attrs;
attrs.pcd = 1;
kmap((paddr_t)(ret) & (~hhdm_start), (paddr_t)ret, asize, attrs.val);
} else {
ret->align = SLAB_ALIGN; ret->align = SLAB_ALIGN;
}
start |= ret->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->tsize = asize;
ret->next = NULL; ret->next = NULL;
ret->prev = NULL; ret->prev = NULL;
ret->flags = flags; ret->flags = flags;
@ -117,6 +126,12 @@ void slab_free(struct slab *slab, void *ptr)
if (next != NULL) if (next != NULL)
next->prev = prev; next->prev = prev;
if (slab->flags & SLAB_DMA_64K) {
struct page attrs = kdefault_attrs;
attrs.pcd = 0;
kmap((paddr_t)slab & (~hhdm_start), (paddr_t)slab, slab->tsize, attrs.val);
}
buddy_free(slab); buddy_free(slab);
} }
} }
@ -125,36 +140,47 @@ 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], 0); slab_cache[i] = slab_create(slab_sizes[i], 0);
for (size_t i = 0; i < ARRAY_SIZE(dma_sizes); i++)
slab_cache_dma[i] = slab_create(dma_sizes[i], SLAB_DMA_64K);
} }
void *kmalloc(size_t size) static void *kmalloc_table(size_t size, size_t *sizes, size_t nsizes, struct slab **cache, bool fb_disable)
{ {
void *ret = NULL;
struct slab *slab = NULL; struct slab *slab = NULL;
if (size > sizes[nsizes - 1] && !fb_disable) {
if (size > slab_sizes[ARRAY_SIZE(slab_sizes) - 1]) { return buddy_alloc(npow2(size));
ret = buddy_alloc(npow2(size));
} else { } else {
for (size_t i = 0; i < ARRAY_SIZE(slab_sizes); i++) { for (size_t i = 0; i < nsizes; i++) {
if (size <= slab_sizes[i]) { if (size <= sizes[i]) {
size = slab_sizes[i]; size = sizes[i];
slab = slab_cache[i]; slab = cache[i];
ret = slab_alloc(slab); return slab_alloc(slab);
break;
} }
} }
} }
return NULL;
}
void *kmalloc(size_t size, uint64_t flags)
{
void *ret = NULL;
if (flags & ALLOC_DMA) {
ret = kmalloc_table(size, dma_sizes, ARRAY_SIZE(dma_sizes), slab_cache_dma, true);
} else {
ret = kmalloc_table(size, slab_sizes, ARRAY_SIZE(slab_sizes), slab_cache, false);
}
if (ret == NULL) if (ret == NULL)
return NULL; return NULL;
return ret; return ret;
} }
void *kzalloc(size_t size) void *kzalloc(size_t size, uint64_t flags)
{ {
void *ret = kmalloc(size); void *ret = kmalloc(size, flags);
if (ret == NULL) if (ret == NULL)
return NULL; return NULL;
@ -162,25 +188,45 @@ void *kzalloc(size_t size)
return ret; return ret;
} }
static bool kfree_helper(void *ptr, struct slab *slab)
{
while (slab != NULL) {
uintptr_t low = 0;
uintptr_t high = 0;
slab_range(slab, &low, &high);
if ((uintptr_t)ptr >= low && (uintptr_t)ptr <= high) {
slab_free(slab, ptr);
return true;
}
slab = slab->next;
}
return false;
}
void kfree(void *ptr) void kfree(void *ptr)
{ {
if (ptr == NULL) if (ptr == NULL)
return; return;
for (size_t i = 0; i < ARRAY_SIZE(slab_sizes); i++) { size_t nslabsizes = ARRAY_SIZE(slab_sizes);
struct slab *slab = slab_cache[i]; size_t ndmasizes = ARRAY_SIZE(dma_sizes);
while (slab != NULL) { size_t max = MAX(nslabsizes, ndmasizes);
uintptr_t low = 0; for (size_t i = 0; i < max; i++) {
uintptr_t high = 0; if (i < nslabsizes) {
struct slab *slab = slab_cache[i];
slab_range(slab_cache[i], &low, &high); if (kfree_helper(ptr, slab))
if ((uintptr_t)ptr >= low && (uintptr_t)ptr <= high) { return;
slab_free(slab_cache[i], ptr); }
if (i < ndmasizes) {
struct slab *slab = slab_cache_dma[i];
if (kfree_helper(ptr, slab))
return; return;
}
slab = slab->next;
} }
} }
/* if we get here, it's a buddy allocation */
buddy_free(ptr); buddy_free(ptr);
} }