Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 16

CS 635

Advanced Systems Programming


Spring 2003
Professor Allan B. Cruse
University of San Francisco
Instructor Contact Information
• Office: Harney Science Center – 212
• Hours: Mon-Wed 2:30pm-4:00pm
• Phone: (415) 422-6562
• Email: cruse@usfca.edu

• Webpage: nexus.cs.usfca.edu/~cruse/
Course Textbooks
Alessandro Rubini and Jonathan Corbet,
Linux Device Drivers (Second Edition), O’Reilly &
Associates, Incorporated (2001)

M. Beck et al, Linux Kernel Programming (Third Edition),


Addison-Wesley (2002)

Alessandro Rubini and Jonathan Corbet, & Greg Kroah-


Hartman
Linux Device Drivers (Thrid Edition) , O'Reilly &
Associates, Incorporated (2005)
Linux kernel modules
• Great mechanism for kernel ‘extensibility’
• Neat tool for studying how kernel works
• Kernel can be modified while it’s running
• Unnecessary to recompile and then reboot
• But inherently unsafe: bugs cause system
crashes!
‘Extensibility’
• Modern OS needs ability to evolve
• Will need to support new devices
• Will need to allow ‘bugs’ to be fixed
• Will need to permit performance gains

• Otherwise: suffer early obsolescence!


Two Extensibility Mechanisms

• ‘Open Source’ programming

• ‘Installable’ kernel modules


‘Superuser’ privileges
• Modifying a running kernel is ‘risky’

• Only authorized ‘system administrators’


are allowed to install kernel modules
A few ‘/proc’ examples
• $ cat /proc/version
• $ cat /proc/cpuinfo
• $ cat /proc/modules
• $ cat /proc/iomem
• $ cat /proc/self/maps
Module structure
• Two ‘module administration’ functions
plus
• Appropriate ‘module service’ functions
Required module functions ver 2.4
• int init_module( void );
• // gets called during module installation

• void cleanup_module( void );


• // gets called during module removal
Required module functions ver 2.6
• Does not mandate the use of “init_module”
and “cleanup_module” function names
• If those names are not used then
module_init() and module_exit() macros
must be used to indicate the init and
cleanup function respectively
– Example
• module_init(jiffies_init);
• module_exit(jiffies_exit);
How to compile a module
• gcc –c –O mod.c (ver 2.4)
• For compiling in kernel ver 2.6 Makefile
must contain the following entry
– obj-m := mod.o

• Compile using the following command


– make -C /lib/modules/`uname -r`/build M=`pwd` modules
• Creates a large number of files of which the final
kernel object file to be loaded is with .ko
extension
Using ‘insmod’ and ‘rmmod’

root# /sbin/insmod jiffies.ko

root# /sbin/rmmod jiffies


Using the ‘sudo’ command

user$ sudo /sbin/insmod jiffies.ko

user$ sudo /sbin/rmmod jiffies


jiffies
• unsigned long volatile jiffies;
• global kernel variable (used by scheduler)
• Initialized to zero when system reboots
• Gets incremented when timer interrupts
• So it counts ‘clock-ticks’ since cpu restart
• ‘tick-frequency’ is architecture dependent
• Must include “linux/jiffies.h” header file
jiffies overflow
• Won’t overflow for at least 16 months
• Linux recently modified to ‘fix’ overflow
• New declaration in ‘linux/sched.h’:
unsigned long long jiffies_64;
and a new instruction in ‘do_timer()’
(*(u64*)&jiffies_64)++;
which compiles to assembly language as
add $1, jiffies+0
adc $0, jiffies+4

You might also like