diff --git a/Makefile b/Makefile index f91fbeb..24c7b1d 100644 --- a/Makefile +++ b/Makefile @@ -138,11 +138,12 @@ modules_install: modules $(Q)echo -n > initramfs/modules/modules.alias $(Q)for ko in initramfs/modules/*.ko; do \ [ -f "$$ko" ] || continue; \ + modname=$$(basename "$$ko" .ko); \ readelf -p .modinfo "$$ko" 2>/dev/null | \ sed -n 's/^\s*\[.*\]\s*//p' | \ while IFS= read -r line; do \ case "$$line" in alias=*) \ - echo "$${line#alias=} $$ko" >> initramfs/modules/modules.alias ;; \ + echo "$${line#alias=} $$modname" >> initramfs/modules/modules.alias ;; \ esac; \ done; \ done diff --git a/drivers/ahci.c b/drivers/ahci.c index 058a111..da92459 100644 --- a/drivers/ahci.c +++ b/drivers/ahci.c @@ -392,11 +392,8 @@ static int ahci_init_device(struct pci_device *device) void ahci_init() { - list_t dev_list; - list_create(&dev_list); - driver_register("ahci", DRIVER_PCI, ahci_init_device, NULL, pci_driver_class(1, 6, 1)); - krdy("AHCI ready\n"); } DRIVER_ENTRY_PRIORITY(ahci_init, 2); +DRIVER_ALIAS("pci:010601"); diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c index 43a9d1c..1e4ab2e 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -76,6 +76,17 @@ size_t pci_find_device(u16 major, u16 minor, list_t *out) return n; } +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); + + if (driver && driver->init_device) { + pci_init_t f = driver->init_device; + f(pdev); + } +} + static void host_probe_devices(struct pci_host *host) { for (int bus = 0; bus < 256; bus++) { @@ -138,14 +149,7 @@ static void host_probe_devices(struct pci_host *host) break; } - /* call the driver */ - u32 driverclass = pci_driver_class(pdev->class_major, pdev->class_minor, pdev->prog_if); - struct driver *driver = driver_find(DRIVER_PCI, driverclass); - - if (driver && driver->init_device) { - pci_init_t f = driver->init_device; - f(pdev); - } + device_init(pdev); kdebug("PCI device: vendor=%04x, id=%x, major=%x, minor=%x\n", pdev->vendor, pdev->device_id, pdev->class_major, pdev->class_minor); diff --git a/include/kernel/driver.h b/include/kernel/driver.h index 7a38504..bafa244 100644 --- a/include/kernel/driver.h +++ b/include/kernel/driver.h @@ -11,6 +11,10 @@ #define DRIVER_ENTRY_PRIORITY(fn, pr) static void (*__initcall_##fn)(void) __attribute((used, section(".initcall." #pr))) = fn #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 + #define DRIVER_PCI 1 typedef int (*pci_init_t)(struct pci_device *dev); diff --git a/include/lib/string.h b/include/lib/string.h index 44b5b60..52238d4 100644 --- a/include/lib/string.h +++ b/include/lib/string.h @@ -15,6 +15,7 @@ void *memcpy_user(struct proc *proc, void *dest, const void *src, size_t num); int memcmp(const void *aa, const void *bb, size_t num); int strcmp(const char *a, const char *b); int strncmp(const char *a, const char *b, size_t n); +char *strchr(const char *s, int c); void *memset(void *str, int c, size_t n); char *strcpy(char *dest, const char *src); char *strncpy(char *dest, const char *src, size_t n); diff --git a/kernel/driver.c b/kernel/driver.c index 353401b..62b42c2 100644 --- a/kernel/driver.c +++ b/kernel/driver.c @@ -3,8 +3,11 @@ #include #include #include +#include static rbtree_t driver_registry; +static char *modules_alias_data; +static size_t modules_alias_len; list_t pending_drivers = {0}; @@ -56,12 +59,63 @@ struct driver *driver_register(const char *name, u32 type, void *init, void *dei return driver; } +static const char *alias_lookup(u32 type, u32 class) +{ + if (!modules_alias_data) { + dirent_t *dent; + if (vfs_open("/modules/modules.alias", &dent) < 0) + return NULL; + + modules_alias_len = dent->inode->file_size; + modules_alias_data = kalloc(modules_alias_len + 1, ALLOC_KERN); + if (!modules_alias_data) + return NULL; + + vfs_read(dent->inode, modules_alias_data, modules_alias_len, 0); + modules_alias_data[modules_alias_len] = '\0'; + } + + char want[64]; + if (type == DRIVER_PCI) + snprintf(want, sizeof(want), "pci:%06x ", class); + else + return NULL; + + size_t want_len = strlen(want); + char *p = modules_alias_data; + + while (p && p < modules_alias_data + modules_alias_len) { + if (strncmp(p, want, want_len) == 0) { + char *nl = strchr(p + want_len, '\n'); + size_t name_len = nl ? (size_t)(nl - p - want_len) : strlen(p + want_len); + char *modname = kalloc(name_len + 1, ALLOC_KERN); + if (modname) { + memcpy(modname, p + want_len, name_len); + modname[name_len] = '\0'; + } + return modname; + } + + char *nl = strchr(p, '\n'); + p = nl ? nl + 1 : NULL; + } + + return NULL; +} + struct driver *driver_find(u32 type, u32 class) { u64 key = (u64)type << 32; key |= class; rbnode_t *node = rbt_search(&driver_registry, key); + if (!node) { + const char *modname = alias_lookup(type, class); + if (modname && driver_load(modname) == 0) { + node = rbt_search(&driver_registry, key); + } + } + if (!node) return NULL;