From a0dd6ec6b1a70e750dc9e504a648e967f4c5119e Mon Sep 17 00:00:00 2001 From: Jesse Buhagiar Date: Tue, 17 May 2022 21:20:07 +1000 Subject: [PATCH] Kernel/USB: Add `driver_init` section At any one given time, there can be an abitrary number of USB drivers in the system. The way driver mapping works (i.e, a device is inserted, and a potentially matching driver is probed) requires us to have instantiated driver objects _before_ a device is inserted. This leaves us with a slight "chicken and egg" problem. We cannot call the probe function before the driver is initialised, but we need to know _what_ driver to initialise. This section is designed to store pointers to functions that are called during the last stage of the early `_init` sequence in the Kernel. The accompanying macro in `USBDriver` emits a symbol, based on the driver name, into this table that is then automatically called. This way, we enforce a "common" driver model; driver developers are not only required to write their driver and inherit from `USB::Driver`, but are also required to have a free floating init function that registers their driver with the USB Core. --- Kernel/Arch/aarch64/linker.ld | 7 +++++++ Kernel/Arch/riscv64/linker.ld | 7 +++++++ Kernel/Arch/x86_64/linker.ld | 7 +++++++ Kernel/Bus/USB/Drivers/USBDriver.h | 4 ++++ 4 files changed, 25 insertions(+) diff --git a/Kernel/Arch/aarch64/linker.ld b/Kernel/Arch/aarch64/linker.ld index 9b3fde122e..bf8ca0d751 100644 --- a/Kernel/Arch/aarch64/linker.ld +++ b/Kernel/Arch/aarch64/linker.ld @@ -31,6 +31,13 @@ SECTIONS *(.text*) } :text + .driver_init ALIGN(4K) : AT (ADDR(.driver_init)) + { + driver_init_table_start = .; + *(.driver_init) + driver_init_table_end = .; + } :text + .rodata ALIGN(4K) : AT (ADDR(.rodata) - KERNEL_MAPPING_BASE) { start_heap_ctors = .; diff --git a/Kernel/Arch/riscv64/linker.ld b/Kernel/Arch/riscv64/linker.ld index 63d759ba5a..4984be6a16 100644 --- a/Kernel/Arch/riscv64/linker.ld +++ b/Kernel/Arch/riscv64/linker.ld @@ -39,6 +39,13 @@ SECTIONS end_of_kernel_text = .; } :text + .driver_init ALIGN(4K) : AT (ADDR(.driver_init)) + { + driver_init_table_start = .; + *(.driver_init) + driver_init_table_end = .; + } :text + .rodata ALIGN(4K) : { start_heap_ctors = .; diff --git a/Kernel/Arch/x86_64/linker.ld b/Kernel/Arch/x86_64/linker.ld index 5776daaf2e..803d75b373 100644 --- a/Kernel/Arch/x86_64/linker.ld +++ b/Kernel/Arch/x86_64/linker.ld @@ -38,6 +38,13 @@ SECTIONS *(.text*) } :text + .driver_init ALIGN(4K) : AT (ADDR(.driver_init)) + { + driver_init_table_start = .; + *(.driver_init) + driver_init_table_end = .; + } :text + .unmap_after_init ALIGN(4K) : AT (ADDR(.unmap_after_init)) { start_of_unmap_after_init = .; diff --git a/Kernel/Bus/USB/Drivers/USBDriver.h b/Kernel/Bus/USB/Drivers/USBDriver.h index 858d795784..5f9ed3213a 100644 --- a/Kernel/Bus/USB/Drivers/USBDriver.h +++ b/Kernel/Bus/USB/Drivers/USBDriver.h @@ -12,6 +12,10 @@ namespace Kernel::USB { +using DriverInitFunction = void (*)(); +#define USB_DEVICE_DRIVER(driver_name) \ + DriverInitFunction driver_init_function_ptr_##driver_name __attribute__((section(".driver_init"), used)) = &driver_name::init + class Device; struct USBDeviceDescriptor; class USBInterface;