slab: replace struct slab with slab_t

This type should never be modified by the user, and only passed into the
functions in slab.c. Using a typedef to notifies the user semantically
of this fact
This commit is contained in:
Michael Shaw 2023-05-19 16:49:34 -04:00
commit 29a0435cf6
13 changed files with 196 additions and 46 deletions

View file

@ -16,7 +16,7 @@ define DEFAULT_VAR =
endef endef
# User controllable CFLAGS. # User controllable CFLAGS.
CFLAGS ?= -g -O2 -pipe -Wall -Wextra CFLAGS ?= -g -O0 -pipe -Wall -Wextra
# User controllable linker flags. We set none by default. # User controllable linker flags. We set none by default.
LDFLAGS ?= LDFLAGS ?=

View file

@ -145,7 +145,6 @@ static int cmdslot(hbaport_t *port)
bool ahci_access_sectors(struct sata_device *dev, paddr_t lba, uint16_t count, paddr_t buf, bool write) bool ahci_access_sectors(struct sata_device *dev, paddr_t lba, uint16_t count, paddr_t buf, bool write)
{ {
hbaport_t *port = &dev->abar->ports[dev->port]; hbaport_t *port = &dev->abar->ports[dev->port];
uint16_t ocount = count;
/* clear pending interrupt */ /* clear pending interrupt */
port->is = (uint32_t)-1; port->is = (uint32_t)-1;
@ -218,10 +217,6 @@ bool ahci_access_sectors(struct sata_device *dev, paddr_t lba, uint16_t count, p
return false; return false;
} }
/* disk read success */
#ifdef KDEBUG
kprintf(LOG_DEBUG "Read %d sectors from disk\n", ocount);
#endif
return true; return true;
} }

View file

@ -40,7 +40,17 @@ inline int block_read(struct block_device *bdev, void *buf, size_t offset, size_
return bdev->ops.read(bdev, buf, offset, size); return bdev->ops.read(bdev, buf, offset, size);
} }
inline int block_readp(struct block_part *part, void *buf, size_t offset, size_t size)
{
return part->bdev->ops.read(part->bdev, buf, part->lba_start + offset, size);
}
inline int block_write(struct block_device *bdev, void *buf, size_t offset, size_t size) inline int block_write(struct block_device *bdev, void *buf, size_t offset, size_t size)
{ {
return bdev->ops.write(bdev, buf, offset, size); return bdev->ops.write(bdev, buf, offset, size);
} }
inline int block_writep(struct block_part *part, void *buf, size_t offset, size_t size)
{
return part->bdev->ops.write(part->bdev, buf, part->lba_start + offset, size);
}

View file

@ -1,3 +1,57 @@
/* SPDX-License-Identifier: GPL-2.0-only */ /* SPDX-License-Identifier: GPL-2.0-only */
#include <kernel/block.h> #include <kernel/block.h>
#include <kernel/slab.h>
#include <fs/fat32.h> #include <fs/fat32.h>
struct fat32_fs *fat_init_part(struct block_part *part)
{
struct fat32_fs *ret = kmalloc(sizeof(struct fat32_fs), ALLOC_KERN);
if (!ret)
return NULL;
ret->part = part;
ret->dev = part->bdev;
char *buf = kmalloc(2048, ALLOC_KERN);
if (!buf)
goto free_ret;
if (block_readp(part, &ret->bpb, 0, 1)) {
kprintf(LOG_ERROR "Failed to read block\n");
goto free_buf;
}
if (ret->bpb.signature != FAT32_SIGNATURE) {
kprintf(LOG_ERROR "Invalid FAT32 signature: %x\n", ret->bpb.signature);
goto free_buf;
}
if (ret->bpb.bytes_per_sector != 512) {
kprintf(LOG_ERROR "Invalid FAT32 bytes per sector: %d\n", ret->bpb.bytes_per_sector);
goto free_buf;
}
if (block_readp(part, buf, ret->bpb.fsinfo_sector, 1)) {
kprintf(LOG_ERROR "Failed to read fsinfo block\n");
goto free_buf;
}
struct fat32_fsinfo *fsinfo = (struct fat32_fsinfo *)(buf);
ret->fsinfo = *fsinfo;
if (block_readp(part, buf + 512, ret->bpb.root_cluster, 1)) {
goto free_buf;
}
memcpy(&ret->root, buf + 512, sizeof(struct fat32_dirent));
kfree(buf);
return ret;
/* error conditions */
free_buf:
kfree(buf);
free_ret:
kfree(ret);
return NULL;
}

View file

@ -3,6 +3,8 @@
#include <kernel/gpt.h> #include <kernel/gpt.h>
#include <kernel/slab.h> #include <kernel/slab.h>
#include <fs/fat32.h>
#define GPT_HEADER_SIGNATURE 0x5452415020494645ull /* "EFI PART" */ #define GPT_HEADER_SIGNATURE 0x5452415020494645ull /* "EFI PART" */
void block_gpt_init(struct block_device *dev) void block_gpt_init(struct block_device *dev)
@ -32,26 +34,36 @@ void block_gpt_init(struct block_device *dev)
goto free_entries; goto free_entries;
} }
if (block_read(dev, entries, header->lba_first, 1)) { if (block_read(dev, entries, header->lba_table, 1)) {
kprintf(LOG_ERROR "Failed to read GPT entries for device %s\n", dev->name); kprintf(LOG_ERROR "Failed to read GPT entries for device %s\n", dev->name);
goto free_entries; goto free_entries;
} }
dev->gpt_header = header; dev->partitions = kmalloc(sizeof(struct block_part) * header->entry_count, ALLOC_KERN);
dev->gpt_entries = entries;
dev->gpt_entries_count = header->entry_count; size_t used_count = 0;
for(size_t i = 0; i < header->entry_count; i++) {
if (entries[i].type_guid[1] == 0)
continue;
used_count++;
struct block_part *part = &dev->partitions[i];
part->bdev = dev;
part->lba_start = entries[i].lba_first;
part->num_blocks = entries[i].lba_last - entries[i].lba_first + 1;
fat_init_part(part);
}
dev->partition_count = used_count;
#ifdef KDEBUG #ifdef KDEBUG
kprintf(LOG_DEBUG "GPT header for device %s loaded\n", dev->name); kprintf(LOG_DEBUG "GPT header for device %s loaded\n", dev->name);
#endif #endif
return; /* Success */
free_entries: free_entries:
kfree(entries); kfree(entries);
free_header: free_header:
kfree(header); kfree(header);
dev->gpt_entries = NULL;
dev->gpt_header = NULL;
dev->gpt_entries_count = 0;
} }

View file

@ -5,4 +5,75 @@
#include <kernel/common.h> #include <kernel/common.h>
#include <kernel/block.h> #include <kernel/block.h>
#define FAT32_SIGNATURE 0x29ull
struct fat32_bpb {
uint8_t jmp[3];
uint64_t oem_id;
uint16_t bytes_per_sector;
uint8_t sectors_per_cluster;
uint16_t reserved_sectors;
uint8_t fat_count;
uint16_t root_entries;
uint16_t total_sectors;
uint8_t media_type;
uint16_t fat_sectors;
uint16_t sectors_per_track;
uint16_t heads;
uint32_t hidden_sectors;
uint32_t total_sectors_large;
/* FAT32 */
uint32_t fat_sectors_large;
uint16_t flags;
uint16_t version;
uint32_t root_cluster;
uint16_t fsinfo_sector;
uint16_t backup_sector;
uint8_t reserved[12];
uint8_t drive_number;
uint8_t reserved2;
uint8_t signature;
uint32_t volume_id;
uint8_t volume_label[11];
uint8_t system_id[8];
uint8_t boot_code[420];
uint16_t boot_signature;
} PACKED;
struct fat32_fsinfo {
uint32_t signature1;
uint8_t reserved1[480];
uint32_t signature2;
uint32_t free_cluster_count;
uint32_t next_free_cluster;
uint8_t reserved2[12];
uint32_t signature3;
} PACKED;
struct fat32_dirent {
uint8_t name[8];
uint8_t ext[3];
uint8_t attr;
uint8_t reserved;
uint8_t ctime_ms;
uint16_t ctime_time;
uint16_t ctime_date;
uint16_t atime_date;
uint16_t cluster_high;
uint16_t mtime_time;
uint16_t mtime_date;
uint16_t cluster_low;
uint32_t size;
} PACKED;
struct fat32_fs {
struct block_device *dev;
struct block_part *part;
struct fat32_bpb bpb;
struct fat32_fsinfo fsinfo;
struct fat32_dirent root;
};
#endif /* _FAT32_H_ */ #endif /* _FAT32_H_ */

View file

@ -12,6 +12,13 @@ struct block_device_ops {
int (*write)(struct block_device *bdev, void *buf, size_t offset, size_t size); int (*write)(struct block_device *bdev, void *buf, size_t offset, size_t size);
}; };
struct block_part {
char *name;
struct block_device *bdev;
size_t lba_start;
size_t num_blocks;
};
struct block_device { struct block_device {
char *name; char *name;
struct block_device_ops ops; struct block_device_ops ops;
@ -21,14 +28,15 @@ struct block_device {
size_t block_count; size_t block_count;
size_t block_size; size_t block_size;
struct gpt_header *gpt_header; struct block_part *partitions;
struct gpt_entry *gpt_entries; size_t partition_count;
size_t gpt_entries_count;
}; };
struct block_device *block_register(char *name, struct block_device_ops *ops, void *data, size_t block_count, size_t block_size); struct block_device *block_register(char *name, struct block_device_ops *ops, void *data, size_t block_count, size_t block_size);
int block_read(struct block_device *bdev, void *buf, size_t offset, size_t size); int block_read(struct block_device *bdev, void *buf, size_t offset, size_t size);
int block_readp(struct block_part *part, void *buf, size_t offset, size_t size);
int block_write(struct block_device *bdev, void *buf, size_t offset, size_t size); int block_write(struct block_device *bdev, void *buf, size_t offset, size_t size);
int block_writep(struct block_part *part, void *buf, size_t offset, size_t size);
extern struct block_device *block_devices[]; extern struct block_device *block_devices[];
extern size_t block_devices_count; extern size_t block_devices_count;

View file

@ -38,12 +38,12 @@ struct gpt_header {
/* GPT LBA 2 */ /* GPT LBA 2 */
struct gpt_entry { struct gpt_entry {
uint8_t guid[16]; uint64_t type_guid[2];
uint8_t guid_parent[16]; uint64_t unique_guid[2];
uint64_t lba_first; uint64_t lba_first;
uint64_t lba_last; uint64_t lba_last;
uint8_t attr[8]; uint64_t flags;
char name[72]; uint8_t name[72];
} PACKED; } PACKED;
void block_gpt_init(struct block_device *dev); void block_gpt_init(struct block_device *dev);

View file

@ -11,7 +11,7 @@
#define ALLOC_KERN 0x01 #define ALLOC_KERN 0x01
#define ALLOC_DMA 0x02 #define ALLOC_DMA 0x02
struct slab { typedef 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 tsize; /* total size of this slab */
size_t num; /* number of objects in this slab */ size_t num; /* number of objects in this slab */
@ -19,20 +19,20 @@ struct slab {
uint64_t flags; uint64_t flags;
size_t align; size_t align;
uintptr_t *nextfree; uintptr_t *nextfree;
struct slab *next; struct _slab *next;
struct slab *prev; struct _slab *prev;
}; } slab_t;
struct kmap_entry { struct kmap_entry {
uintptr_t *ptr; uintptr_t *ptr;
size_t size; size_t size;
struct slab *slab; slab_t *slab;
struct kmap_entry *next; struct kmap_entry *next;
}; };
struct slab *slab_create(size_t size, size_t cache_size, uint64_t flags); slab_t *slab_create(size_t size, size_t cache_size, uint64_t flags);
void *slab_alloc(struct slab *slab); void *slab_alloc(slab_t *slab);
void slab_free(struct slab *slab, void *ptr); void slab_free(slab_t *slab, void *ptr);
void kmalloc_init(); void kmalloc_init();
void *kmalloc(size_t size, uint64_t flags); void *kmalloc(size_t size, uint64_t flags);

View file

@ -4,7 +4,7 @@
#include <kernel/slab.h> #include <kernel/slab.h>
#include <kernel/rbtree.h> #include <kernel/rbtree.h>
static struct slab *rbt_slab; static slab_t *rbt_slab;
void rbt_slab_init() void rbt_slab_init()
{ {

View file

@ -134,7 +134,7 @@ int snprintf(char *str, const char *restrict fmt, size_t size, ...)
int kprintf(const char *restrict fmt, ...) int kprintf(const char *restrict fmt, ...)
{ {
char buf[1024] = { 0 }; char buf[2048] = { 0 };
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
int ret = vsnprintf(buf, fmt, 1024, args); int ret = vsnprintf(buf, fmt, 1024, args);

View file

@ -27,7 +27,7 @@ struct page kdefault_attrs;
paddr_t kcr3 = 0; paddr_t kcr3 = 0;
static void *(*alloc_page)(void); static void *(*alloc_page)(void);
static struct slab *kmap_slab; static slab_t *kmap_slab;
static void buddy_bitmap_set(char *bitmap, size_t depth, size_t n, int val) static void buddy_bitmap_set(char *bitmap, size_t depth, size_t n, int val)
{ {

View file

@ -7,13 +7,13 @@
#define SLAB_ALIGN 7 #define SLAB_ALIGN 7
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 slab_t *slab_cache[ARRAY_SIZE(slab_sizes)];
static size_t dma_sizes[] = { 0x1000, 0x10000, 0x100000 }; static size_t dma_sizes[] = { 0x1000, 0x10000, 0x100000 };
static struct slab *slab_cache_dma[ARRAY_SIZE(slab_sizes)]; static slab_t *slab_cache_dma[ARRAY_SIZE(slab_sizes)];
static struct rbtree kmalloc_tree = { NULL }; static struct rbtree kmalloc_tree = { NULL };
static inline void slab_range(struct slab *slab, uintptr_t *o_start, uintptr_t *o_end) static inline void slab_range(slab_t *slab, uintptr_t *o_start, uintptr_t *o_end)
{ {
uintptr_t sslab = (uintptr_t)slab; uintptr_t sslab = (uintptr_t)slab;
sslab = (sslab | slab->align) + 1; sslab = (sslab | slab->align) + 1;
@ -22,12 +22,12 @@ static inline void slab_range(struct slab *slab, uintptr_t *o_start, uintptr_t *
*o_end = sslab + (slab->size * slab->num); *o_end = sslab + (slab->size * slab->num);
} }
struct slab *slab_create(size_t size, size_t cache_size, uint64_t flags) slab_t *slab_create(size_t size, size_t cache_size, uint64_t flags)
{ {
if (size < sizeof(uintptr_t)) if (size < sizeof(uintptr_t))
return NULL; return NULL;
struct slab *ret = buddy_alloc(cache_size); slab_t *ret = buddy_alloc(cache_size);
if (ret == NULL) if (ret == NULL)
return NULL; return NULL;
@ -36,7 +36,7 @@ struct slab *slab_create(size_t size, size_t cache_size, uint64_t flags)
/* slab area start position */ /* slab area start position */
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(slab_t);
if (flags & SLAB_PAGE_ALIGN) { if (flags & SLAB_PAGE_ALIGN) {
ret->align = 0xFFF; ret->align = 0xFFF;
@ -72,7 +72,7 @@ struct slab *slab_create(size_t size, size_t cache_size, uint64_t flags)
return ret; return ret;
} }
void *slab_alloc(struct slab *slab) void *slab_alloc(slab_t *slab)
{ {
if (slab == NULL) if (slab == NULL)
return NULL; return NULL;
@ -94,7 +94,7 @@ void *slab_alloc(struct slab *slab)
return ret; return ret;
} }
void slab_free(struct slab *slab, void *ptr) void slab_free(slab_t *slab, void *ptr)
{ {
if (slab == NULL) if (slab == NULL)
return; return;
@ -118,8 +118,8 @@ void slab_free(struct slab *slab, void *ptr)
/* delete the slab if it's empty and not a root slab */ /* delete the slab if it's empty and not a root slab */
if (slab->free == slab->num && slab->prev != NULL) { if (slab->free == slab->num && slab->prev != NULL) {
struct slab *next = slab->next; slab_t *next = slab->next;
struct slab *prev = slab->prev; slab_t *prev = slab->prev;
prev->next = next; prev->next = next;
if (next != NULL) if (next != NULL)
@ -143,9 +143,9 @@ void kmalloc_init()
slab_cache_dma[i] = slab_create(dma_sizes[i], 8 * MB, SLAB_DMA_64K); slab_cache_dma[i] = slab_create(dma_sizes[i], 8 * MB, SLAB_DMA_64K);
} }
static void *kmalloc_table(size_t size, size_t *sizes, size_t nsizes, struct slab **cache, bool fb_disable) static void *kmalloc_table(size_t size, size_t *sizes, size_t nsizes, slab_t **cache, bool fb_disable)
{ {
struct slab *slab = NULL; slab_t *slab = NULL;
void *ret = NULL; void *ret = NULL;
if (size > sizes[nsizes - 1] && !fb_disable) { if (size > sizes[nsizes - 1] && !fb_disable) {
ret = buddy_alloc(npow2(size)); ret = buddy_alloc(npow2(size));
@ -210,7 +210,7 @@ void kfree(void *ptr)
return; return;
} }
struct slab *slab = (struct slab *)n->value; slab_t *slab = (slab_t *)n->value;
if (slab != NULL) if (slab != NULL)
slab_free(slab, ptr); slab_free(slab, ptr);
else else