From 20905ec95f308d1f342d72e3faf2d8558416c4bb Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Wed, 12 Aug 2026 20:54:27 -0400 Subject: [PATCH] driver: load driver file by alias only Signed-off-by: Michael Shaw --- drivers/ahci.c | 5 ++-- drivers/pci/pci.c | 6 +++- include/kernel/driver.h | 9 +++--- include/lib/hashchain.h | 5 ++++ kernel/driver.c | 63 +++++++++++++++++++++++------------------ lib/hashchain.c | 20 +++++++++---- 6 files changed, 69 insertions(+), 39 deletions(-) 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 index eff0108..77c0ad4 100644 --- a/include/lib/hashchain.h +++ b/include/lib/hashchain.h @@ -3,6 +3,7 @@ #define _ht_H_ #include +#include typedef struct ht_entry { char *key; @@ -17,6 +18,8 @@ typedef struct { ht_entry_t **buckets; size_t capacity; size_t size; + + mtx_t lock; } ht_t; ht_t *ht_create(size_t capacity); @@ -24,5 +27,7 @@ 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/hashchain.c b/lib/hashchain.c index f7b95a4..841bd2c 100644 --- a/lib/hashchain.c +++ b/lib/hashchain.c @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: GPL-2.0-only */ #include #include +#include #include #include #include @@ -61,21 +62,20 @@ int ht_insert(ht_t *ht, const char *key, void *value) ht_entry_t *entry = ht->buckets[idx]; while (entry) { - if (strcmp(entry->key, key) == 0) { - entry->ptr = value; - return 0; + if (!strcmp(entry->key, key)) { + return -EEXIST; } entry = entry->next; } entry = kzalloc(sizeof(ht_entry_t), ALLOC_KERN); if (!entry) - return -1; + return -ENOMEM; entry->key = strdup(key); if (!entry->key) { kfree(entry); - return -1; + return -ENOMEM; } entry->ptr = value; @@ -126,3 +126,13 @@ int ht_delete(ht_t *ht, const char *key) return -1; } + +void ht_lock(ht_t *ht) +{ + mtx_lock(&ht->lock); +} + +void ht_unlock(ht_t *ht) +{ + mtx_unlock(&ht->lock); +}