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) 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)
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 int init_module( void ); // gets called during module installation void cleanup_module( void ); // gets called during module removal
How to compile a module gcc –c –O mod.c
Using ‘insmod’ and ‘rmmod’ root# /sbin/insmod jiffies.o root# /sbin/rmmod jiffies
Using the ‘sudo’ command user$ sudo /sbin/insmod jiffies.o 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
jiffies overflow Won’t overflow for at least 16 months Linux recently modified to ‘fix’ overflow New declaration in ‘linux/sched.h’: unsigned long longjiffies_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