1 / 14

Operating Systems Lab. (#3)

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.

Download Presentation

Operating Systems Lab. (#3)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Operating Systems Lab.(#3) University of Tehran – ECE Dept. Fall 2005 Reza Shokri shokri@ece.ut.ac.ir

  2. Outline • Kernel Modules • Writing a Module • Compiling the Module • Linking a Modules • Proc File System Operating Systems Lab - UT ECE Dept Fall 2005

  3. 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

  4. 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

  5. Makefile Operating Systems Lab - UT ECE Dept Fall 2005

  6. 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

  7. 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

  8. 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

  9. How to install and remove root# /sbin/insmod hello.o root# /sbin/rmmod hello Operating Systems Lab - UT ECE Dept Fall 2005

  10. Linking a module to the kernel (from Rubini’s book) Operating Systems Lab - UT ECE Dept Fall 2005

  11. Two interfaces • Use /proc file system • Use device drive interface Operating Systems Lab - UT ECE Dept Fall 2005

  12. 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

  13. 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

  14. 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

More Related