Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

DrASahu

Dept of Comp Sc & Engg. DeptofCompSc&Engg.


IITGuwahati
MidsemesterExamination
AdvancePeripheralinterfacing
Standard Interfacing using PCI, SCSI, USB StandardInterfacingusingPCI,SCSI,USB
OS,DeviceDriver
K l M d l KernelModule
Typeofdevicesanddrivers
Recommendedtext
Mostofthestudenthaveattemptedallthe
questions questions
Endsemesterexamquestionswillbefrom2
nd
part
only(Coursesaftermidsem) y ( )
20StudentsStats:Averageis24outof50
AnswerscriptofMidSemesterwillbeshown5
th
Oct p
2010,TuesdayClass
Wehavetoannounce1weekbefore
Youwillget1hourtocheckyourpaperforany
discrepancyinevaluation
You can take back your answer script Youcantakebackyouranswerscript
8085/8086basedInterfacing
Understanding is Ok UnderstandingisOk
ProblemwithAbove
Advance Computer System AdvanceComputerSystem
Standardization
PCI,SCSI,USB, , , ,
OSplayimportantroleindeviceinterfacing
Kernel and Device driver KernelandDevicedriver
DMA,PIC,PIT
ControllingviaKernelProgram(Cprogram) g g ( p g )
LinuxKernelSplitView p
LinuxDeviceDriverbyJonhantan Corbet
application
Wewouldwritemost ofthissourcecode
app.cpp
butwewouldcallsome libraryfunctions
e.g., open(), read(), write(), malloc(),
call
e.g.,open(),read(),write(),malloc(),
thenourcodewouldgetlinkedwith
standard
runtime
ret
g
standardruntimelibraries
(Sothisisanexampleofcodereuse)
libraries
Manystandardlibraryfunctions
performservicesthatrequire
application
executingprivileged instructions
(whichonlythekernelcando)
call
O i
standard
runtime
ret
Operating
System
kernel
syscall
libraries
kernel
sysret
userspace kernelspace
Linuxallowsustowriteourown
installablekernelmodules
application
module
andaddthemtoarunningsystem
pp
call
ret
module
call
ret
standard
runtime
ret
OperatingSystem
kernel
syscall
libraries
sysret
userspace kernelspace
An LKM has to be written using C but can AnLKMhastobewrittenusing C butcan
includeinlineassemblylanguage
An LKM runs in kernel space so it can do AnLKMrunsinkernelspace soitcando
anythingthattheCPUsupports
S LKM SoanLKMcan
directlycontroltheperipheraldevices
modifythekernelsschedulingalgorithms
examinethekernelshiddendatastructures
Familiar with using Linux (or UNIX) FamiliarwithusingLinux(orUNIX)
AbletowriteprogramsinC(orC++)
i f k d k fil BasicofMakeandMakefile
Abletouseanassembler
Acquaintedwithx86architecture
Generalpurposeregisters(EAX,EBX,) Ge e a pu pose eg s e s ( , , )
Categoriesofinstructions(MOV,ADD,)
Ways to address memory (direct indirect ) Waystoaddressmemory(direct,indirect,)
Basic structure of a C program: BasicstructureofaCprogram:
Commentbanner(showingtitleandabstract)
Preprocessor directives (e g for header files) Preprocessordirectives(e.g.,forheaderfiles)
Globaldatadeclarations(iftheyareneeded)
R i d i () f ti ( th t i t) Requiredmain()function(astheentrypoint)
Caninvokeprintf()(forformattedoutput)
O i ll d fi h f i Optionallymaydefinesomeotherfunctions
#include<st di o. h>/ / Header f or pr i nt f #include<st di o. h>/ / Header f or pr i nt f
i t i ( ) { int mai n( ) {
pr i nt f ( \ n Hel l o wor l d\ n) ;
return 0;
}}
A modern OS needs the ability to evolve AmodernOSneedstheabilitytoevolve
Willneedtosupportnewdevices
Will need to allow bugs to be fixed Willneedtoallow bugs tobefixed
Willneedtopermitperformancegains
ElseOSmaysufferearlyobsolescence!
Two mechanisms for extensibility: Twomechanismsfor extensibility :
O S d l OpenSourcedevelopment
Loadablekernelmodules(LKMs)
Convenient technique for OS extensibility ConvenienttechniqueforOS extensibility
Alsoallowsustostudyhowkernelworks
l b difi d hil i i Kernelcanbemodifiedwhileitsrunning
Noneedtorecompileandthenreboot
Butinherentlyunsafe:anybugcancausea
systemmalfunction orcompletecrash! y p
Modifying a running kernel is risky Modifyingarunningkernelis risky
O l h i d d i i Onlyauthorizedsystemadministrators
areallowedtoinstallkernelmodules
Were allowed to install kernel objects: We reallowedto install kernelobjects:
$/sbin/insmodmyLKM.ko
Wereallowedtoremovekernelobjects:
$ / bi / d LKM $/sbin/rmmodmyLKM
Anyoneisallowedtolistkernelobjects:
$/sbin/lsmod
You can use any texteditor (e.g., vi or emacs) to Youcanuseanytext editor(e.g., vi or emacs )to
createsourcecode(intheClanguage)foraLinux
kernelmodule(i.e.,anLKM)
ButakernelmodulediffersfromanormalC
applicationprogram(e.g.,nomain()function)
Akernelmodulecannotcallanyofthefamiliar
functionsfromthestandardCruntimelibraries
ForanyLKM,twoentrypointsaremandatory(one
forinitialization,andoneforcleanup)
Resembles normal layout of C programs ResemblesnormallayoutofCprograms
but
d l d i i i f i Twomoduleadministrationfunctions
[thesearerequired]
plus
Appropriatemoduleservicefunctions pp p
[theseareoptional]
Module uses printk() instead of printf() Moduleuses printk() insteadof printf()
Includesthe<linux/module.h>headerfile
S ifi l l f li (G ) Specifiesalegalsoftwarelicense(GPL)
CompilationrequiresaspecialMakefile
Executionispassive(itsasideeffect)
Module has no restriction on privileges Modulehasnorestrictionon privileges
int init module( void ); intinit_module(void);
//thisgetscalledduringmoduleinstallation
voidcleanup_module(void);
// this gets called during module removal //thisgetscalledduringmoduleremoval
A newer syntax allows memory efficiency: Anewersyntaxallowsmemoryefficiency:
module_init(my_init);
module exit(my exit); module_exit(my_exit);
#include <l i nux/ modul e. h> / / f or pr i nt k( )
int i ni t ( voi d ) {
pr i nt k( " \ n Kel l o, ever ybody! \ n\ n" ) ; p t ( \ e o, e e ybody! \ \ ) ;
return 0;
}
voi d exi t ( voi d ) {
pr i nt k( " \ n Goodbye now. . . \ n\ n" ) ; p y
}
MODULE_LI CENSE( " GPL" ) ;
modul e i ni t ( i ni t ) ; modul e_i ni t ( i ni t ) ;
modul e_exi t ( exi t ) ;
Download mmake.cpp from class website Download mmake.cpp fromclasswebsite
andcompileitwithmake(oralternatively
use: $g++mmake.cppommake )
Downloadthekello.csourcefilefromthe
website,andcompileitusingmmake
Addthekello.kokernelobjecttoLinuxusing
theLinux/sbin/insmodcommand
Usedmesgtoviewthekernelslogfile
Remove kello (with /sbin/rmmod kello) Remove kello (with /sbin/rmmod kello )
You can modify the printk() textstring so its Youcanmodifythe printk() text stringsoits
messagewillbesuretobedisplayed itwill
be output to the graphical desktop beoutputtothegraphicaldesktop
H h d i Hereshowyoucandoit:
printk(<0>Hello,everybody!\n);
Thisloglevelindicatesakernelemergency
Download mmake cpp and kello c Downloadmmake.cppandkello.c
Compilemmake.cpp usingmake
h il k ll i k Thencompilekello.c usingmmake
Installkello.ko(andseeprintkmessage)
Removekello(toseeanothermessage)
Modify the printk() statements in kello.c Modifythe printk() statementsinkello.c
Recompileandreinstalltoviewnewinfo
Corbet J, Rubini, and KroahHartman, Linux Corbet J,Rubini,andKroah Hartman,Linux
DeviceDrivers(3
rd
Ed),OReilly,2005
George Pajari, Writing UNIX Device Drivers, GeorgePajari,WritingUNIXDeviceDrivers,
PearsonEducationIndia,2006
Maurice J Back, The design of the Unix OS, MauriceJBack,ThedesignoftheUnixOS,
PrenticeHallIndia,2007
Bovet and Cesati, Understanding the Linux BovetandCesati,UnderstandingtheLinux
Kernel(3
rd
Ed),OReilly(2006)
Softcopyofbooksavailableoncoursewebsite py

You might also like