200 likes | 390 Views
Operating Systems Lab. (#3). University of Tehran – ECE Dept. Fall 2005 Reza Shokri shokri@ece.ut.ac.ir. Outline. Kernel Modules Writing a Module Compiling the Module Linking a Modules Proc File System. Kernel Modules.
E N D
Operating Systems Lab.(#3) University of Tehran – ECE Dept. Fall 2005 Reza Shokri shokri@ece.ut.ac.ir
Outline • Kernel Modules • Writing a Module • Compiling the Module • Linking a Modules • Proc File System Operating Systems Lab - UT ECE Dept Fall 2005
Kernel Modules • Sections of kernel code that can be compiled, loaded, and unloaded independent of the rest of the kernel. • A kernel module may typically implement a device driver, a file system, or a networking protocol. • The module interface allows third parties to write and distribute, on their own terms, device drivers or file systems . • Kernel modules allow a Linux system to be set up with a standard, minimal kernel, without any extra device drivers built in. • Module management. • Driver registration. Operating Systems Lab - UT ECE Dept Fall 2005
A minimal module-template #include <linux/module.h> int init_module( void ) { // code here gets called during module installation } void cleanup_module( void ) { // code here gets called during module removal } MODULE_LICENSE(“GPL”); Operating Systems Lab - UT ECE Dept Fall 2005
Makefile Operating Systems Lab - UT ECE Dept Fall 2005
How to compile a module • You could directly invoke the C-compiler: $ gcc –c –O –D__KERNEL__ -DMODULE –I/lib/modules/`uname –r`/build/include mod.c • -c:Modules are not independent executable • Does not perform the linking step • -Wall: to turn on compiler warning • -D__KERNEL__: We are in kernel mode • -DMODULE: a module is being compiled • OR: you can use the ‘make’ utility: $ make mod.o Operating Systems Lab - UT ECE Dept Fall 2005
The ‘printk()’ function • Kernel modules cannot call any functions in the C runtime library (e.g., ‘printf()’) • But similar kernel versions of important functions are provided (e.g., ‘printk()’) • Syntax and semantics are slightly different (e.g., priority and message-destination) • Capabilities may be somewhat restricted (e.g., printk() can’t show floating-point) Operating Systems Lab - UT ECE Dept Fall 2005
Simple module example #include <linux/module.h> int init_module( void ) { printk( “<1>Hello, world!\n” ); return 0; // SUCCESS } void cleanup_module( void ) { printk( “<1>Goodbye everyone\n” ); } MODULE_LICENSE(“GPL”); Operating Systems Lab - UT ECE Dept Fall 2005
How to install and remove root# /sbin/insmod hello.o root# /sbin/rmmod hello Operating Systems Lab - UT ECE Dept Fall 2005
Linking a module to the kernel (from Rubini’s book) Operating Systems Lab - UT ECE Dept Fall 2005
Two interfaces • Use /proc file system • Use device drive interface Operating Systems Lab - UT ECE Dept Fall 2005
Use /proc Interface • The module is • registered with the create_proc_entry() and • unregistered with the remove_proc_entry() functions. • struct proc_dir_entry *create_proc_entry(char *name, mode_t mode, struct proc_dir_entry *parent); • The name is the name of the file to be created • mode is its mode • parent is a pointer to the parent directory. If parent is NULL, the new entry is added in the root /proc directory. Operating Systems Lab - UT ECE Dept Fall 2005
read_proc • int read_proc(char *sys_buffer, char **my_buffer, off_t file_pos, int count, int *eof, void *data); • The sys_buffer argument points to a buffer allocated by the system. • The file_pos argument specifies the offset of the read into the proc file • the count specifies the maximum number of bytes that can be saved in the buffer. • If these fields are used properly, the eof field can be used to indicate writing the end-of-file by writing 1 into *eof Operating Systems Lab - UT ECE Dept Fall 2005
Ruuning the Module • gcc -Wall -I/usr/src/linux/include -D__KERNEL__ -DMODULE -c mymodule.c • cat /proc/modules • Cat/proc/my_module • insmod mymodule.o • rmsmod mymodule.o Operating Systems Lab - UT ECE Dept Fall 2005