1414#include <kernel.h>
1515#include <stdbool.h>
1616
17+ /**
18+ * @brief module ABI version the kernel expects
19+ *
20+ * @note If we change the ABI at all, we must bump this by 1.
21+ * It is used in the names of the init/exit functions of each module,
22+ * so an incompatible module is outright rejected before it can wreak
23+ * havoc in the kernel space.
24+ */
25+ #define KMOD_ABI 100
26+
1727/* ELF identification */
1828#define EI_NIDENT 16 /**< Size of e_ident[] array */
1929
@@ -127,8 +137,29 @@ typedef struct elf_rela {
127137typedef bool (* module_init_fn )(void ); /**< Prototype for module initialiser */
128138typedef void (* module_exit_fn )(void ); /**< Prototype for module finaliser */
129139
140+ /**
141+ * @brief Set this prefix on all module_init/module_exit functions in modules
142+ */
130143#define EXPORTED __attribute__((visibility("default")))
131144
145+ /**
146+ * @brief helpers to form symbol names
147+ */
148+ #define MOD_CAT2 (a ,b ) a##b
149+ #define MOD_CAT (a ,b ) MOD_CAT2(a,b)
150+ #define MOD_INIT_SYM (ver ) MOD_CAT(mod_init_v, ver)
151+ #define MOD_EXIT_SYM (ver ) MOD_CAT(mod_exit_v, ver)
152+
153+ /* stringification */
154+ #define STR2 (x ) #x
155+ #define STR (x ) STR2(x)
156+
157+ /**
158+ * @brief canonical names as strings
159+ */
160+ #define MOD_INIT_NAME_STR "mod_init_v" STR(KMOD_ABI)
161+ #define MOD_EXIT_NAME_STR "mod_exit_v" STR(KMOD_ABI)
162+
132163/**
133164 * @brief Describes a loaded and relocated module image
134165 *
@@ -137,8 +168,9 @@ typedef void (*module_exit_fn)(void); /**< Prototype for module finaliser */
137168 * lookup, and resolves conventional entry points named mod_init and mod_exit
138169 */
139170typedef struct module {
140- char name [ 64 ] ; /**< Optional friendly name, not set by the loader */
171+ const char * name ; /**< Module name */
141172 uint8_t * base ; /**< Base of contiguous allocation holding SHF_ALLOC sections */
173+ void * raw_bits ; /**< For symbol resolution by other modules */
142174 size_t size ; /**< Total size of the contiguous allocation */
143175 const elf_sym * symtab ; /**< Pointer to the module's SHT_SYMTAB within the file buffer */
144176 size_t sym_count ; /**< Number of entries in symtab */
@@ -156,9 +188,9 @@ typedef struct module {
156188 * @brief Load and relocate a module from an in-memory ELF64 ET_REL buffer, then call its initialiser
157189 *
158190 * Validates the ELF header, allocates a single contiguous region for all SHF_ALLOC sections,
159- * copies/zeros contents with correct alignment, loads symtab/strtab, applies all SHT_RELA
160- * relocations, resolves undefined globals against the kernel dynamic symbol table
161- * (kernel must keep .dynsym/.dynstr), resolves mod_init/mod_exit, then calls mod_init
191+ * copies/zeros contents with correct alignment, applies all SHT_RELA relocations, resolves
192+ * undefined globals against the kernel dynamic symbol table (from kernel.sym),
193+ * resolves versioned mod_init/mod_exit, then calls mod_init
162194 *
163195 * @param file pointer to the ELF bytes
164196 * @param len size of the ELF buffer in bytes
@@ -178,7 +210,7 @@ bool module_load_from_memory(const void *file, size_t len, module *out);
178210 * @param m module descriptor previously returned by module_load_from_memory
179211 * @return true on success, false if @p m is NULL or teardown fails
180212 */
181- bool module_unload (module * m );
213+ bool module_internal_unload (module * m );
182214
183215/**
184216 * @brief Resolve a kernel global by name via the kernel dynamic symbol table
@@ -297,3 +329,34 @@ uintptr_t module_resolve_symbol_addr(const module *m, const elf_sym *s);
297329 * externals should appear as R_X86_64_64 when modules are built with -mcmodel=large -fno-pic -fno-plt
298330 */
299331bool module_apply_relocations (const uint8_t * file , const elf_shdr * sh , size_t shnum , module * m );
332+
333+ /**
334+ * @brief Initialise the module registry and loader state
335+ *
336+ * Creates the in-kernel hashmap used to track loaded modules keyed by name
337+ * Must be called once before any other module operations
338+ */
339+ void init_modules (void );
340+
341+ /**
342+ * @brief Load and start a module by name
343+ *
344+ * Constructs the path /system/modules/<name>.ko, reads the file, relocates it,
345+ * resolves externals against the kernel symbol index, and calls mod_init
346+ * Returns false if the module is already loaded or on any failure
347+ *
348+ * @param name NUL-terminated module base name without extension or path
349+ * @return true on success, false on error
350+ */
351+ bool load_module (const char * name );
352+
353+ /**
354+ * @brief Stop and unload a loaded module by name
355+ *
356+ * Locates the module in the registry, calls mod_exit if present, frees all
357+ * allocations associated with the image, and removes the entry from the registry
358+ *
359+ * @param name NUL-terminated module base name
360+ * @return true on success, false if not found or unload failed
361+ */
362+ bool unload_module (const char * name );
0 commit comments