-
Notifications
You must be signed in to change notification settings - Fork 3
Kernel Modules
The kernel provides a robust and flexible design for kernel modules. They don't need any special build system and can be compiled & linked with existing software. In the underlying module-implementation, kernel modules are actually shared-object files which are dynamically linked with other kernel modules & the microkernel.
The modules each contains a DYNAMIC segment which contains a array of dynamic-tag entries. In the array of tags, the following are used during loading & linking -
1. DT_INIT - The microkernel calls a initialization function before the module starts running. If the DT_INIT tag is present, the corresponding function at the address given is executed. This is really useful if there are object-allocators, C++ constructors, or pointers to default objects which need to be loaded before the KModuleMain function starts execution. Also, it is very crucial for bundles of modules which are inter-dependent and assume that all other modules are initialization, disregarding the order in which are started.
2. DT_FINI - The microkernel will call this before the module memory is freed and unmapped. Very useful for printing finalization output & releasing system memory.
NOTE: - Other relevant tags are automatically produced by the linker (currently ld is the preferred linker for linux-based host machines).
The kernel modules must follow a certain pattern of linker scripts. The linker script for the **ModuleFramework (package com.silcos.mdfrwk.*) is given below -
OUTPUT_FORMAT("elf32-i386")
ENTRY(KModuleMain)
PHDRS {
KmTextSegment PT_LOAD FILEHDR;/* Text segment will be read-only, execute */
KmDataSegment PT_LOAD;/* Data segment will be read-write, no-execute (for security) */
KmDynamicSegment PT_DYNAMIC;
}
SECTIONS {
. = 0x00000000 + SIZEOF_HEADERS;
.text ALIGN(0x1000) : {
ctorsStart = .;
*(SORT(.ctors*))
ctorsEnd = .;
dtorsStart = .;
*(SORT(.dtors*))
dtorsEnd = .;
*(.text)
*(.text*)
*(.rdata)
*(.rodata)
} :KmTextSegment
.data ALIGN(0x1000):{
*(.data)
} :KmDataSegment
.bss ALIGN(0x1000):{
*(.COMMON)
*(.bss)
} :KmDataSegment
.dynamic ALIGN(0x1000):{
*(.dynamic)
} :KmDynamicSegment
/DISCARD/ :
{
*(.interp)
*(.gcc_except_table)
*(.eh_frame)
*(.note)
*(.comment)
}
}
Important Note - The DT_INIT tag may not be available on certain linkers. Then the module can declare a extern "C" void __init()
function. The module loader will automatically call it if the DT_INIT tag is not present.
KModuleMain is not required, if the module does not perform any functions on its own and only provides utilities to other kernel software.
The module MUST PROVIDE A PT_DYNAMIC
segment for loading the module successfully and must be a position-independent object file