diff --git a/drivers/ahci.c b/drivers/ahci.c index da92459..3185427 100644 --- a/drivers/ahci.c +++ b/drivers/ahci.c @@ -19,6 +19,7 @@ #include #define SPIN_MAX 2000 +#define AHCI_ALIAS "pci:010601" static uint8_t sata_device_count = 0; static uint8_t cd_device_count = 0; @@ -392,8 +393,8 @@ static int ahci_init_device(struct pci_device *device) void ahci_init() { - driver_register("ahci", DRIVER_PCI, ahci_init_device, NULL, pci_driver_class(1, 6, 1)); + driver_register("ahci", DRIVER_PCI, ahci_init_device, NULL, AHCI_ALIAS); krdy("AHCI ready\n"); } DRIVER_ENTRY_PRIORITY(ahci_init, 2); -DRIVER_ALIAS("pci:010601"); +DRIVER_ALIAS(AHCI_ALIAS); diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c index 1e4ab2e..7bb7712 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -79,7 +79,11 @@ size_t pci_find_device(u16 major, u16 minor, list_t *out) static void device_init(struct pci_device *pdev) { u32 driverclass = pci_driver_class(pdev->class_major, pdev->class_minor, pdev->prog_if); - struct driver *driver = driver_find(DRIVER_PCI, driverclass); + char alias[11]; + + snprintf(alias, 11, "pci:%06x", driverclass); + + struct driver *driver = driver_find(alias); if (driver && driver->init_device) { pci_init_t f = driver->init_device; diff --git a/include/kernel/driver.h b/include/kernel/driver.h index bafa244..65d4656 100644 --- a/include/kernel/driver.h +++ b/include/kernel/driver.h @@ -12,8 +12,7 @@ #define DRIVER_EXIT(fn) static void (*__exitcall_##fn)(void) __attribute((used, section(".exitcall"))) = fn #define DRIVER_ALIAS(_alias) \ - static const char __driver_alias_##__LINE__[] \ - __attribute__((used, section(".modinfo"), aligned(1))) = "alias=" _alias + static const char __driver_alias_##__LINE__[] __attribute__((used, section(".modinfo"), aligned(1))) = "alias=" _alias #define DRIVER_PCI 1 @@ -33,11 +32,13 @@ struct driver { void *base; size_t size; + char alias[64]; + u32 n_users; }; -struct driver *driver_register(const char *name, u32 type, void *init, void *deinit, u32 class); -struct driver *driver_find(u32 type, u32 class); +struct driver *driver_register(const char *name, u32 type, void *init, void *deinit, const char *alias); +struct driver *driver_find(const char *alias); int driver_load(const char *name); int driver_unload(const char *name); diff --git a/include/lib/hashchain.h b/include/lib/hashchain.h new file mode 100644 index 0000000..77c0ad4 --- /dev/null +++ b/include/lib/hashchain.h @@ -0,0 +1,33 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _ht_H_ +#define _ht_H_ + +#include +#include + +typedef struct ht_entry { + char *key; + union { + void *ptr; + u64 val; + }; + struct ht_entry *next; +} ht_entry_t; + +typedef struct { + ht_entry_t **buckets; + size_t capacity; + size_t size; + + mtx_t lock; +} ht_t; + +ht_t *ht_create(size_t capacity); +void ht_destroy(ht_t *ht); +int ht_insert(ht_t *ht, const char *key, void *value); +void *ht_lookup(ht_t *ht, const char *key); +int ht_delete(ht_t *ht, const char *key); +void ht_lock(ht_t *ht); +void ht_unlock(ht_t *ht); + +#endif /* _ht_H_ */ diff --git a/kernel/driver.c b/kernel/driver.c index 62b42c2..a7c31fd 100644 --- a/kernel/driver.c +++ b/kernel/driver.c @@ -2,17 +2,33 @@ #include #include #include -#include +#include #include -static rbtree_t driver_registry; +#define DRIVER_HT_SIZE 128 + +static ht_t *driver_registry; static char *modules_alias_data; static size_t modules_alias_len; list_t pending_drivers = {0}; -struct driver *driver_register(const char *name, u32 type, void *init, void *deinit, u32 class) +static void driver_registry_init() { + driver_registry = ht_create(DRIVER_HT_SIZE); +} + +struct driver *driver_register(const char *name, u32 type, void *init, void *deinit, const char *alias) +{ + if (!alias) + return NULL; + + if (!driver_registry) { + driver_registry_init(); + if (!driver_registry) + return NULL; + } + if (!pending_drivers.head && !pending_drivers.tail) list_create(&pending_drivers); @@ -46,20 +62,16 @@ struct driver *driver_register(const char *name, u32 type, void *init, void *dei driver->init_device = init; driver->deinit_device = deinit; - u64 key = (u64)type << 32; - key |= class; + strncpy(driver->alias, alias, sizeof(driver->alias) - 1); - rbnode_t *node = rbt_insert(&driver_registry, key); - if (!node) { - kfree(driver); - return NULL; - } - node->ptr = driver; + ht_lock(driver_registry); + ht_insert(driver_registry, driver->alias, driver); + ht_unlock(driver_registry); return driver; } -static const char *alias_lookup(u32 type, u32 class) +static const char *alias_lookup(const char *alias) { if (!modules_alias_data) { dirent_t *dent; @@ -76,10 +88,7 @@ static const char *alias_lookup(u32 type, u32 class) } char want[64]; - if (type == DRIVER_PCI) - snprintf(want, sizeof(want), "pci:%06x ", class); - else - return NULL; + snprintf(want, sizeof(want), "%s ", alias); size_t want_len = strlen(want); char *p = modules_alias_data; @@ -103,21 +112,21 @@ static const char *alias_lookup(u32 type, u32 class) return NULL; } -struct driver *driver_find(u32 type, u32 class) +struct driver *driver_find(const char *alias) { - u64 key = (u64)type << 32; - key |= class; + if (!driver_registry) { + driver_registry_init(); + if (!driver_registry) + return NULL; + } - rbnode_t *node = rbt_search(&driver_registry, key); - if (!node) { - const char *modname = alias_lookup(type, class); + struct driver *ret = ht_lookup(driver_registry, alias); + if (!ret) { + const char *modname = alias_lookup(alias); if (modname && driver_load(modname) == 0) { - node = rbt_search(&driver_registry, key); + ret = ht_lookup(driver_registry, alias); } } - if (!node) - return NULL; - - return node->ptr; + return ret; } diff --git a/lib/Makefile b/lib/Makefile index a238d4e..c19dae5 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -8,3 +8,4 @@ obj-y += string.o obj-y += list.o obj-y += sem.o obj-y += rwlock.o +obj-y += hashchain.o diff --git a/lib/hashchain.c b/lib/hashchain.c new file mode 100644 index 0000000..841bd2c --- /dev/null +++ b/lib/hashchain.c @@ -0,0 +1,138 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#include +#include +#include +#include +#include +#include + +#define ht_MIN 32 + +static u64 hash_str(const char *str) +{ + u64 hash = 5381; + for (const char *p = str; *p; p++) + hash = ((hash << 5) + hash) + (u64)*p; + return hash; +} + +ht_t *ht_create(size_t capacity) +{ + size_t cap = npow2(capacity); + if (cap < ht_MIN) + cap = ht_MIN; + ht_t *ht = kzalloc(sizeof(ht_t), ALLOC_KERN); + if (!ht) + return NULL; + + ht->buckets = kzalloc(sizeof(ht_entry_t *) * cap, ALLOC_KERN); + if (!ht->buckets) { + kfree(ht); + return NULL; + } + + ht->capacity = cap; + ht->size = 0; + return ht; +} + +void ht_destroy(ht_t *ht) +{ + if (!ht) + return; + + for (size_t i = 0; i < ht->capacity; i++) { + ht_entry_t *entry = ht->buckets[i]; + while (entry) { + ht_entry_t *next = entry->next; + kfree(entry->key); + kfree(entry); + entry = next; + } + } + + kfree(ht->buckets); + kfree(ht); +} + +int ht_insert(ht_t *ht, const char *key, void *value) +{ + u64 hash = hash_str(key); + size_t idx = hash & (ht->capacity - 1); + + ht_entry_t *entry = ht->buckets[idx]; + while (entry) { + if (!strcmp(entry->key, key)) { + return -EEXIST; + } + entry = entry->next; + } + + entry = kzalloc(sizeof(ht_entry_t), ALLOC_KERN); + if (!entry) + return -ENOMEM; + + entry->key = strdup(key); + if (!entry->key) { + kfree(entry); + return -ENOMEM; + } + + entry->ptr = value; + entry->next = ht->buckets[idx]; + ht->buckets[idx] = entry; + ht->size++; + return 0; +} + +void *ht_lookup(ht_t *ht, const char *key) +{ + u64 hash = hash_str(key); + size_t idx = hash & (ht->capacity - 1); + + ht_entry_t *entry = ht->buckets[idx]; + while (entry) { + if (strcmp(entry->key, key) == 0) + return entry->ptr; + entry = entry->next; + } + + return NULL; +} + +int ht_delete(ht_t *ht, const char *key) +{ + u64 hash = hash_str(key); + size_t idx = hash & (ht->capacity - 1); + + ht_entry_t *entry = ht->buckets[idx]; + ht_entry_t *prev = NULL; + + while (entry) { + if (strcmp(entry->key, key) == 0) { + if (prev) + prev->next = entry->next; + else + ht->buckets[idx] = entry->next; + + kfree(entry->key); + kfree(entry); + ht->size--; + return 0; + } + prev = entry; + entry = entry->next; + } + + return -1; +} + +void ht_lock(ht_t *ht) +{ + mtx_lock(&ht->lock); +} + +void ht_unlock(ht_t *ht) +{ + mtx_unlock(&ht->lock); +}