advanced interfacing (os, driver, kernel)

27
Advanced Interfacing (OS, Driver, Kernel) Dr A Sahu Dept of Comp Sc & Engg. IIT Guwahati

Upload: amethyst-cabrera

Post on 02-Jan-2016

39 views

Category:

Documents


1 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Advanced Interfacing  (OS, Driver, Kernel)

Advanced Interfacing (OS, Driver, Kernel)

Dr A SahuDept of Comp Sc & Engg.

IIT Guwahati

Page 2: Advanced Interfacing  (OS, Driver, Kernel)

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

Page 3: Advanced Interfacing  (OS, Driver, Kernel)

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

Page 4: Advanced Interfacing  (OS, Driver, Kernel)

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 )

Page 5: Advanced Interfacing  (OS, Driver, Kernel)

Linux Kernel Split View

Linux Device Driver by Jonhantan Corbet

Page 6: Advanced Interfacing  (OS, Driver, Kernel)

Normal C/C++ programming

applicationWe 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

Page 7: Advanced Interfacing  (OS, Driver, Kernel)

Normal C/C++ programming

application

standard“runtime”libraries

call

ret

user space kernel space

Operating Systemkernel

syscall

sysret

Many standard library functions perform services that require executing privileged instructions (which only the kernel can do)

Page 8: Advanced Interfacing  (OS, Driver, Kernel)

Linux Kernel Modules

application

standard“runtime”libraries

callret

user space kernel space

Operating Systemkernel

syscall

sysret

module

Linux allows us to write our own installable kernel modulesand add them to a running system

callret

Page 9: Advanced Interfacing  (OS, Driver, Kernel)

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

Page 10: Advanced Interfacing  (OS, Driver, Kernel)

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,…)

Page 11: Advanced Interfacing  (OS, Driver, Kernel)

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

Page 12: Advanced Interfacing  (OS, Driver, Kernel)

Example program in C

#include<stdio.h>//Headerfor printf

int main(){printf(“\n Hello world\n”);return 0;

}

Page 13: Advanced Interfacing  (OS, Driver, Kernel)

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!

Page 14: Advanced Interfacing  (OS, Driver, Kernel)

Extensibility with Linux

Two mechanisms for ‘extensibility’:

• ‘Open Source’ development

• ‘Loadable’ kernel modules (LKMs)

Page 15: Advanced Interfacing  (OS, Driver, Kernel)

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!

Page 16: Advanced Interfacing  (OS, Driver, Kernel)

‘Superuser’ privileges

• Modifying a running kernel is ‘risky’

• Only authorized ‘system administrators’are allowed to install kernel modules

Page 17: Advanced Interfacing  (OS, Driver, Kernel)

‘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

Page 18: Advanced Interfacing  (OS, Driver, Kernel)

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’)

Page 19: Advanced Interfacing  (OS, Driver, Kernel)

Normal LKM structure

• Resembles normal layout of C programsbut

• Two ‘module administration’ functions [these are required]

plus• Appropriate ‘module service’ functions

[these are optional]

Page 20: Advanced Interfacing  (OS, Driver, Kernel)

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’

Page 21: Advanced Interfacing  (OS, Driver, Kernel)

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);

Page 22: Advanced Interfacing  (OS, Driver, Kernel)

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);

Page 23: Advanced Interfacing  (OS, Driver, Kernel)

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/rmmod kello’)

Page 24: Advanced Interfacing  (OS, Driver, Kernel)

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’

Page 25: Advanced Interfacing  (OS, Driver, Kernel)

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

Page 26: Advanced Interfacing  (OS, Driver, Kernel)

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

Page 27: Advanced Interfacing  (OS, Driver, Kernel)

Thanks