90 likes | 251 Views
Computer Science & Engineering Department. The University of Connecticut. 191 Auditorium Road, Box U-155. Storrs, CT 06269-3155. Linux Loadable Module. A. Paul Heely Jr. and Mike Stack. What are Modules?. Linux Kernel Monolithic Originally all functions were built in at compile time
E N D
Computer Science & Engineering Department The University of Connecticut 191 Auditorium Road, Box U-155 Storrs, CT 06269-3155 Linux Loadable Module A. Paul Heely Jr. and Mike Stack
What are Modules? • Linux Kernel • Monolithic • Originally all functions were built in at compile time • Loadable Modules • Compile Object Files • Inserted and Removed from a Running Kernel • Run in the context of the Kernel • Contain any number of functions or types of data • Must contain an init_module() and cleanup_module function
Why Use Modules • Why Use Modules at All? • Reduce size of running kernel • Network Protocol Drivers (TCP/IP, IPX, AppleTalk, etc) can be loaded only when needed • Single hardware device can provide different functionality • Parallel Port can be used for printing, zip drives, and “higher” speed networking (PLIP) • These cannot co-exist at the same time • Modules allow these to co-exist at different times without loading a new kernel • New devices can be added dynamically • Rot-13 device
Loading Modules • 4 Steps In Loading a Module • Preparing the module • Allocating Kernel Memory • Copying the Module to the Kernel • Executing the Modules Init Function
Preparing Modules • Handled by the module loader insmod • Module File Opened and Read Piece by Piece • header • text segment • data segment • text relocation information • data relocation information • symbol table • string table • Symbol Table Stored in a Binary Tree to Speedup Symbol Resolution
Preparing Modules • External References Resolved • Kernel object or other modules • Symbol Table Updated • Text and Data Segments also Updated with Kernel address
Allocating Memory, Copying, Module Init • Kernel Memory Allocation • create_module(): Kernel function • Passed module name and size • Returns address in Kernel where module will reside • Copying Module to Kernel Space • init_module(): Kernel function • Updates all offsets in the modules object code to reflect actual kernel location • Copies object code from user space to the kernel • Module Initialization • Final step, Kernel invokes the modules init_module() function
Example Module • Rot-13 Encoding Device • New device added to running Linux System • Performs Rot-13 encoding/decoding on a character stream • Linux: The Choice of a GNU Generation becomes Yvahk: Gur Pubvpr bs TAH Trarengvba • New device can be opened, closed, and written • Handled the same as a “file” • Kernel maintains open count • Module cannot be removed while it is opened
Conclusions • Loadable Modules Provide Simple Method of modifying a running Kernel • multiple drivers can share same hardware resource • Modules can be used to reduce a kernels size • seldom used drivers unloaded until needed • Trade Off between kernel size and performance • Loadable modules reduce size but require time to load.