PatchworkOS  966e257
A non-POSIX operating system.
Loading...
Searching...
No Matches
module.c File Reference
#include <kernel/fs/dentry.h>
#include <kernel/fs/sysfs.h>
#include <kernel/module/module.h>
#include <kernel/fs/vfs.h>
#include <kernel/init/boot_info.h>
#include <kernel/log/log.h>
#include <kernel/log/panic.h>
#include <kernel/mem/vmm.h>
#include <kernel/module/symbol.h>
#include <kernel/proc/process.h>
#include <kernel/sched/sched.h>
#include <kernel/sync/lock.h>
#include <kernel/utils/map.h>
#include <kernel/version.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/elf.h>
#include <sys/io.h>
#include <sys/list.h>
Include dependency graph for module.c:

Go to the source code of this file.

Data Structures

struct  module_file_t
 
struct  module_load_ctx_t
 

Macros

#define MODULE_SYMBOL_ALLOWED(type, binding, name)
 

Functions

static void * module_resolve_symbol_callback (const char *name, void *private)
 
static module_tmodule_new (module_info_t *info)
 
static void module_free (module_t *module)
 
static uint64_t module_call_load_event (module_t *module)
 
static void module_call_unload_event (module_t *module)
 
static module_tmodule_find_by_name (const char *name)
 
static module_tmodule_find_provider (symbol_group_id_t groupId)
 
static module_device_tmodule_device_new (const char *type, const char *name)
 
static void module_device_free (module_device_t *device)
 
static module_device_tmodule_device_get (const char *name)
 
static module_device_handler_tmodule_handler_add (module_t *module, module_device_t *device)
 
static void module_handler_remove (module_device_handler_t *handler)
 
static uint64_t module_string_copy (const char *str, char *out, size_t outSize)
 Copy a string up to either a null-terminator or a semicolon into the output buffer.
 
static module_info_tmodule_info_parse (const char *moduleInfo)
 
static uint64_t module_file_read (module_file_t *outFile, const path_t *dirPath, process_t *process, const char *filename)
 
static void module_file_deinit (module_file_t *file)
 
static uint64_t module_cache_symbols_add (module_file_t *file, const char *path)
 
static uint64_t module_cache_device_types_add (module_file_t *file, const char *path)
 
static module_cached_symbol_tmodule_cache_lookup_symbol (const char *name)
 
static module_cached_device_tmodule_cache_lookup_device_type (const char *type)
 
static void module_cache_clear (void)
 
static uint64_t module_cache_build (void)
 
static void module_gc_mark_reachable (module_t *module)
 
static void module_gc_sweep_unreachable (module_t *module, list_t *unreachables)
 
static void module_gc_collect (void)
 
void module_init_fake_kernel_module (void)
 Initialize a fake module representing the kernel itself.
 
static bool module_info_supports_device (const module_info_t *info, const char *type)
 
static uint64_t module_load_and_relocate_elf (module_t *module, Elf64_File *elf, module_load_ctx_t *ctx)
 
static uint64_t module_load_dependency (module_load_ctx_t *ctx, const char *symbolName)
 
static module_tmodule_get_or_load (const char *filename, file_t *dir, const char *type)
 
uint64_t module_device_attach (const char *type, const char *name, module_load_flags_t flags)
 Notify the module system of a device being attached.
 
void module_device_detach (const char *name)
 Notify the module system of a device being detached.
 
bool module_device_types_contains (const char *deviceTypes, const char *type)
 Check if a list of device types contains a specific device type.
 

Variables

static module_info_t fakeKernelModuleInfo
 
static list_t modulesList = LIST_CREATE(modulesList)
 
static map_t modulesMap = MAP_CREATE()
 Key = module name, value = module_t*.
 
static map_t providerMap
 Key = symbol_group_id_t, value = module_t*. Used to find which module provides which symbols.
 
static map_t deviceMap = MAP_CREATE()
 Key = device name, value = module_device_t*.
 
static map_t symbolCache = MAP_CREATE()
 Key = symbol name, value = module_cached_symbol_t*.
 
static map_t deviceCache = MAP_CREATE()
 Key = device type, value = module_cached_device_t*.
 
static bool cacheValid = false
 
static mutex_t lock = MUTEX_CREATE(lock)
 

Macro Definition Documentation

◆ MODULE_SYMBOL_ALLOWED

#define MODULE_SYMBOL_ALLOWED (   type,
  binding,
  name 
)
Value:
(((type) == STT_OBJECT || (type) == STT_FUNC) && ((binding) == STB_GLOBAL) && \
#define MODULE_RESERVED_PREFIX
Reserved prefix for module global symbols.
Definition module.h:209
@ MODULE_RESERVED_PREFIX_LENGTH
Definition module.h:217
@ STB_GLOBAL
Global symbol, visible to all object files being combined.
Definition elf.h:567
@ STT_FUNC
Symbol is a code object.
Definition elf.h:591
@ STT_OBJECT
Symbol is a data object.
Definition elf.h:590
_PUBLIC int strncmp(const char *s1, const char *s2, size_t n)
Definition strncmp.c:3

Definition at line 51 of file module.c.

Function Documentation

◆ module_resolve_symbol_callback()

static void * module_resolve_symbol_callback ( const char *  name,
void *  private 
)
static

Definition at line 899 of file module.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ module_new()

static module_t * module_new ( module_info_t info)
static

Definition at line 55 of file module.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ module_free()

static void module_free ( module_t module)
static

Definition at line 109 of file module.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ module_call_load_event()

static uint64_t module_call_load_event ( module_t module)
static

Definition at line 129 of file module.c.

Here is the caller graph for this function:

◆ module_call_unload_event()

static void module_call_unload_event ( module_t module)
static

Definition at line 144 of file module.c.

Here is the caller graph for this function:

◆ module_find_by_name()

static module_t * module_find_by_name ( const char *  name)
static

Definition at line 157 of file module.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ module_find_provider()

static module_t * module_find_provider ( symbol_group_id_t  groupId)
static

Definition at line 163 of file module.c.

Here is the call graph for this function:

◆ module_device_new()

static module_device_t * module_device_new ( const char *  type,
const char *  name 
)
static

Definition at line 169 of file module.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ module_device_free()

static void module_device_free ( module_device_t device)
static

Definition at line 191 of file module.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ module_device_get()

static module_device_t * module_device_get ( const char *  name)
static

Definition at line 199 of file module.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ module_handler_add()

static module_device_handler_t * module_handler_add ( module_t module,
module_device_t device 
)
inlinestatic

Definition at line 205 of file module.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ module_handler_remove()

static void module_handler_remove ( module_device_handler_t handler)
inlinestatic

Definition at line 235 of file module.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ module_string_copy()

static uint64_t module_string_copy ( const char *  str,
char *  out,
size_t  outSize 
)
inlinestatic

Copy a string up to either a null-terminator or a semicolon into the output buffer.

Definition at line 252 of file module.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ module_info_parse()

static module_info_t * module_info_parse ( const char *  moduleInfo)
inlinestatic

Definition at line 268 of file module.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ module_file_read()

static uint64_t module_file_read ( module_file_t outFile,
const path_t dirPath,
process_t process,
const char *  filename 
)
static

Definition at line 371 of file module.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ module_file_deinit()

static void module_file_deinit ( module_file_t file)
static

Definition at line 429 of file module.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ module_cache_symbols_add()

static uint64_t module_cache_symbols_add ( module_file_t file,
const char *  path 
)
static

Definition at line 435 of file module.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ module_cache_device_types_add()

static uint64_t module_cache_device_types_add ( module_file_t file,
const char *  path 
)
static

Definition at line 487 of file module.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ module_cache_lookup_symbol()

static module_cached_symbol_t * module_cache_lookup_symbol ( const char *  name)
static

Definition at line 534 of file module.c.

Here is the call graph for this function:

◆ module_cache_lookup_device_type()

static module_cached_device_t * module_cache_lookup_device_type ( const char *  type)
static

Definition at line 540 of file module.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ module_cache_clear()

static void module_cache_clear ( void  )
static

Definition at line 546 of file module.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ module_cache_build()

static uint64_t module_cache_build ( void  )
static

Definition at line 582 of file module.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ module_gc_mark_reachable()

static void module_gc_mark_reachable ( module_t module)
static

Definition at line 655 of file module.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ module_gc_sweep_unreachable()

static void module_gc_sweep_unreachable ( module_t module,
list_t unreachables 
)
static

Definition at line 671 of file module.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ module_gc_collect()

static void module_gc_collect ( void  )
static

Definition at line 688 of file module.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ module_info_supports_device()

static bool module_info_supports_device ( const module_info_t info,
const char *  type 
)
static

Definition at line 758 of file module.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ module_load_and_relocate_elf()

static uint64_t module_load_and_relocate_elf ( module_t module,
Elf64_File elf,
module_load_ctx_t ctx 
)
static

Definition at line 794 of file module.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ module_load_dependency()

static uint64_t module_load_dependency ( module_load_ctx_t ctx,
const char *  symbolName 
)
static

Definition at line 853 of file module.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ module_get_or_load()

static module_t * module_get_or_load ( const char *  filename,
file_t dir,
const char *  type 
)
static

Definition at line 952 of file module.c.

Here is the call graph for this function:

Variable Documentation

◆ fakeKernelModuleInfo

module_info_t fakeKernelModuleInfo
static
Initial value:
= {
.name = "kernel",
.author = "The PatchworkOS Authors",
.description = "The PatchworkOS Kernel",
.version = OS_VERSION,
.license = "MIT",
.osVersion = OS_VERSION,
.deviceTypes = "",
}

Definition at line 26 of file module.c.

◆ modulesList

list_t modulesList = LIST_CREATE(modulesList)
static

Definition at line 36 of file module.c.

◆ modulesMap

map_t modulesMap = MAP_CREATE()
static

Key = module name, value = module_t*.

Definition at line 37 of file module.c.

◆ providerMap

map_t providerMap
static
Initial value:
=
#define MAP_CREATE()
Create a map initializer.
Definition map.h:160

Key = symbol_group_id_t, value = module_t*. Used to find which module provides which symbols.

Definition at line 38 of file module.c.

◆ deviceMap

map_t deviceMap = MAP_CREATE()
static

Key = device name, value = module_device_t*.

Definition at line 41 of file module.c.

◆ symbolCache

map_t symbolCache = MAP_CREATE()
static

Key = symbol name, value = module_cached_symbol_t*.

Definition at line 43 of file module.c.

◆ deviceCache

map_t deviceCache = MAP_CREATE()
static

Key = device type, value = module_cached_device_t*.

Definition at line 44 of file module.c.

◆ cacheValid

bool cacheValid = false
static

Definition at line 45 of file module.c.

◆ lock

mutex_t lock = MUTEX_CREATE(lock)
static

Definition at line 47 of file module.c.