310 likes | 523 Vues
Advanced Interfacing (OS, Driver, Kernel). Dr A Sahu Dept of Comp Sc & Engg . IIT Guwahati. Outline. Mid semester Examination Advance Peripheral interfacing Standard Interfacing using PCI, SCSI, USB OS, Device Driver Kernel Module Type of devices and drivers Recommended text.
E N D
Advanced Interfacing (OS, Driver, Kernel) Dr A Sahu Dept of Comp Sc & Engg. IIT Guwahati
Outline • Mid semester Examination • Advance Peripheral interfacing • Standard Interfacing using PCI, SCSI, USB • OS, Device Driver • Kernel Module • Type of devices and drivers • Recommended text
Mid Semester Exam • Most of the student have attempted all the questions • End semester exam questions will be from 2nd part only (Courses after mid-sem) • 20 Students Stats: Average is 24 out of 50 • Answer script of Mid-Semester will be shown 5th Oct 2010, Tuesday Class • We have to announce 1 week before • You will get 1 hour to check your paper for any discrepancy in evaluation • You can take back your answer script
Advance Peripheral interfacing • 8085/8086 based Interfacing • Understanding is Ok • Problem with Above • Advance Computer System • Standardization • PCI, SCSI, USB, • OS play important role in device interfacing • Kernel and Device driver • DMA, PIC, PIT • Controlling via Kernel Program (C program )
Linux Kernel Split View Linux Device Driver by JonhantanCorbet
Normal C/C++ programming 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”) application call ret standard “runtime” libraries
Normal C/C++ programming Many standard library functions perform services that require executing privileged instructions (which only the kernel can do) application call Operating System kernel ret syscall standard “runtime” libraries sysret user space kernel space
Linux Kernel Modules Linux allows us to write our own installable kernel modules and add them to a running system application module ret call call Operating System kernel ret syscall standard “runtime” libraries sysret user space kernel space
Requirements/Benefits • An LKM has to be written using “C” -- but can include “inline” assembly language • An LKM runs in kernel-space – so it can do anything that the CPU supports • So an LKM can – • directly control the peripheral devices • modify the kernel’s scheduling algorithms • examine the kernel’s hidden data-structures
I assume you know • Familiar with using Linux (or UNIX) • Able to write programs in C (or C++) • Basic of Make and Makefile • Able to use an assembler • Acquainted with x86 architecture • General-purpose registers (EAX, EBX, …) • Categories of instructions (MOV, ADD, …) • Ways to address memory (direct, indirect,…)
Typical C layout • Basic structure of a C program: • Comment-banner (showing title and abstract) • Preprocessor directives (e.g., for header-files) • Global data-declarations (if they are needed) • Required ‘main()’ function (as the entry-point) • Can invoke ‘printf()’ (for ‘formatted’ output) • Optionally may define some other functions
Example program in C #include<stdio.h>//Headerforprintf int main(){ printf(“\n Hello world\n”); return 0; }
OS ‘Extensibility’ • A modern OS needs the ability to evolve • Will need to support new devices • Will need to allow ‘bugs’ to be fixed • Will need to permit performance gains • Else OS may suffer early obsolescence!
Extensibility with Linux Two mechanisms for ‘extensibility’: • ‘Open Source’ development • ‘Loadable’ kernel modules (LKMs)
Loadable Kernel Modules • Convenient technique for OS ‘extensibility’ • Also allows us to study how kernel works • Kernel can be modified while it’s running • No need to recompile and then reboot • But inherently unsafe: any ‘bug’ can cause a system malfunction -- or complete crash!
‘Superuser’ privileges • Modifying a running kernel is ‘risky’ • Only authorized ‘system administrators’ are allowed to install kernel modules
‘insmod’ and ‘rmmod’ • We’re allowed to ‘install’ kernel objects: $ /sbin/insmod myLKM.ko • We’re allowed to ‘remove’ kernel objects: $ /sbin/rmmod myLKM • Anyone is allowed to ‘list’ kernel objects: $ /sbin/lsmod
Creating a new LKM • You can use any text-editor (e.g., ‘vi’ or ‘emacs’) to create source-code (in the C language) for a Linux kernel module (i.e., an LKM) • But a kernel module differs from a normal C application program (e.g., no ‘main()’ function) • A kernel module cannot call any of the familiar functions from the standard C runtime libraries • For any LKM, two entry-points are mandatory (one for ‘initialization’, and one for ‘cleanup’)
Normal LKM structure • Resembles normal layout of C programs but • Two ‘module administration’ functions [these are required] plus • Appropriate ‘module service’ functions [these are optional]
Other LKM differences • Module uses ‘printk()’ instead of ‘printf()’ • Includes the <linux/module.h> header-file • Specifies a legal software license (“GPL”) • Compilation requires a special ‘Makefile’ • Execution is “passive” (it’s a ‘side-effect’) • Module has no restriction on ‘privileges’
Required module functions • int init_module( void ); // this gets called during module installation • void cleanup_module( void ); // this gets called during module removal • A newer syntax allows memory-efficiency: 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 • Download ‘mmake.cpp’ from class website and compile it with ‘make’ (or alternatively use: $ g++ mmake.cpp –o mmake) • Download the ‘kello.c’ source-file from the website, and compile it using ‘mmake’ • Add the ‘kello.ko’ kernel-object to Linux using the Linux ‘/sbin/insmod’ command • Use ‘dmesg’ to view the kernel’s log-file • Remove ‘kello’ (with ‘/sbin/rmmodkello’)
Showing kernel messages • You can modify the ‘printk()’ text-string so its message will be sure to be displayed – -- it will be output to the graphical desktop • Here’s how you can do it: printk( “<0> Hello, everybody! \n” ); This log-level indicates a ‘kernel emergency’
Summary • Download mmake.cpp and kello.c • Compile mmake.cpp using ‘make’ • Then compile kello.c using ‘mmake’ • Install ‘kello.ko’ (and see printk-message) • Remove ‘kello’ (to see another message) • Modify the ‘printk()’ statements in kello.c • Recompile and reinstall to view new info
Recommended texts • Corbet J, Rubini, and Kroah-Hartman, Linux Device Drivers (3rd Ed), ‘O’Reilly, 2005 • George Pajari, Writing UNIX Device Drivers, Pearson Education India,2006 • Maurice J Back, The design of the Unix OS, Prentice Hall India ,2007 • Bovet and Cesati, Understanding the Linux Kernel (3rd Ed), ’ O’ Reilly (2006) • Soft copy of books available on course website