Advanced Interfacing �(OS, Driver, Kernel)
Dr A Sahu
Dept of Comp Sc & Engg.
IIT Guwahati
Outline
Mid Semester Exam
Advance Peripheral interfacing
Linux Kernel Split View
Linux Device Driver by Jonhantan Corbet
Normal C/C++ programming
application
We would write most of this source-code
“app.cpp”
but we would call some library-functions
e.g., open(), read(), write(), malloc(), …
then our code would get ‘linked’ with
standard runtime libraries
(So this is an example of “code reuse”)
standard
“runtime”
libraries
call
ret
Normal C/C++ programming
application
standard
“runtime”
libraries
call
ret
user space
kernel space
Operating
System
kernel
syscall
sysret
Many standard library functions
perform services that require
executing privileged instructions
(which only the kernel can do)
Linux Kernel Modules
application
standard
“runtime”
libraries
call
ret
user space
kernel space
Operating System
kernel
syscall
sysret
module
Linux allows us to write our own
installable kernel modules
and add them to a running system
call
ret
Requirements/Benefits
I assume you know
Typical C layout
Example program in C
#include<stdio.h>//Headerfor printf
int main(){
printf(“\n Hello world\n”);
return 0;
}
OS ‘Extensibility’
Extensibility with Linux
Two mechanisms for ‘extensibility’:
Loadable Kernel Modules
‘Superuser’ privileges
are allowed to install kernel modules
‘insmod’ and ‘rmmod’
$ /sbin/insmod myLKM.ko
$ /sbin/rmmod myLKM
$ /sbin/lsmod
Creating a new LKM
Normal LKM structure
but
plus
Other LKM differences
Required module functions
// this gets called during module installation
// this gets called during module removal
module_init(my_init);
module_exit(my_exit);
Kernel module written in C
#include <linux/module.h> // for printk()
int init( void ){
printk( "\n Kello, everybody! \n\n" );
return 0;
}
void exit( void ){
printk( "\n Goodbye now... \n\n" );
}
MODULE_LICENSE("GPL");
module_init(init);
module_exit(exit);
Practice in lab
Showing kernel messages
printk( “<0> Hello, everybody! \n” );
This log-level indicates a ‘kernel emergency’
Summary
Recommended texts
Thanks