Linux Device Drivers - Introduction & Basic Module Programming A Raghavendra Rao Rao (
[email protected] [email protected])) C-DAC, Hyderabad
Agenda ●
Kernels and Classifcations
●
The Linux Kernel
●
Linux Modules
●
Linux Module Programming
●
The Kernel !m"ol Ta"le
●
Linux Boot Process
Agenda ●
Kernels and Classifcations
●
The Linux Kernel
●
Linux Modules
●
Linux Module Programming
●
The Kernel !m"ol Ta"le
●
Linux Boot Process
Kernels and Classifcations
#hat is a Kernel $$ ●
Core o% the 'erating !stem
●
ervices ( –
Process Management
–
Memor! Management
–
Device Management
–
)iles!stem Management
–
*et+or, Management
–
ecurit!
La!out o% the Kernel Kernel
Kernel in action .ser 'ace
Kernel 'ace
.ser
!stem Call
A''lication
Kernel
!stem Li"rar!
/esources
/e0uest made "! the user
Kernel in action .ser 'ace
Kernel 'ace
.ser
!stem Call
A''lication
Kernel
!stem Li"rar!
/esources
A''lication calls s!stem li"rar! routine
Kernel in action .ser 'ace
Kernel 'ace
.ser
!stem Call
A''lication
Kernel
!stem Li"rar!
/esources
!stem li"rar! ma,es a s!stem call
Kernel in action .ser 'ace .ser
Kernel 'ace !stem Call !stem call Inter%ace tra's the ,ernel
A''lication
Kernel
!stem Li"rar!
/esources
Kernel in action .ser 'ace
Kernel 'ace
.ser
!stem Call
A''lication
Kernel Kernel access the resources as 'er re0uest
!stem Li"rar!
/esources
Kernel in action .ser 'ace
Kernel 'ace
.ser
!stem Call
A''lication
Kernel
!stem Li"rar!
/esources
Kernel com'letes the re0uest
Kernel in action .ser 'ace .ser
Kernel 'ace !stem Call /eturns the result
A''lication
Kernel
!stem Li"rar!
/esources
Kernel in action .ser 'ace
Kernel 'ace
.ser
!stem Call
A''lication
Kernel
!stem Li"rar!
/esources
/eturns the result to s!stem li"rar!
Kernel in action .ser 'ace
Kernel 'ace
.ser
!stem Call
A''lication
Kernel
!stem Li"rar!
/esources
/eturns the result to the a''lication
Kernel in action .ser 'ace
Kernel 'ace
.ser
!stem Call
A''lication
Kernel
!stem Li"rar!
/esources
.ser re0uest 'rocessed
1xam'le
T!'es o% Kernel Architectures ●
Monolithic Kernel
●
Micro,ernel
Monolithic Kernel ●
●
All the 'arts o% a ,ernel li,e the cheduler2 )ile !stem2 Memor! Management2 *et+or,ing tac,s2 Device Drivers2 etc2 are maintained as a single unit +ithin the ,ernel Advantages ( –
●
Disadvantages ( –
●
)aster Processing Crash insecure2 'orting in3exi"ilit!2 ,ernel si4e ex'losion
1xam'les ( –
M-D2 .nix2 Linux2 etc
Micro,ernel ●
●
nl! the ver! im'ortant 'arts li,e IPC2 "asic scheduler2 "asic memor! handling2 "asic I5 'rimitives etc2 are 'ut into the ,ernel thers are maintained as server 'rocesses in user s'ace Advantages ( –
●
Disadvantages ( –
●
Crash /esistant2 Porta"le2 maller i4e lo+er Processing due to additional message 'assing
1xam'les ( –
6*72 Minix 82 1/2 Ke!K2 #indo+s *T
The Linux Kernel
Introduction to the Linux Kernel ●
●
●
●
tarted o9 in :;;: "! Linus Torvalds as a sim'le terminal driver %or Intel <=8<> Distri"uted under the ?*. ?eneral Pu"lic License as a )ree and 'en ource o%t+are Programming language ( C & Assem"l! The Linux Kernel can "e @%reel!@ do+nloaded %rom www.kernel.org
Com'lete Linux !stem ( Linux Distri"utions ●
Bootloader ( ?/.B2 LIL2 etc
●
Linux Kernel
●
!stem Li"raries ( gli"c2 'threads2 IPC2 etc
●
A''lications ( +e" "ro+sers2 games2 editors2 music 'la!ers2 etc
Linux Modules
Introduction to Linux Modules ●
●
●
1ach 'iece o% code that can "e added to the ,ernel at runtime is called a module. 1ach module is made u' o% o"ect code not lin,ed into a com'lete executa"le that can "e d!namicall! lin,ed to the running ,ernel The Linux ,ernel o9ers su''ort %or 0uite a %e+ di9erent t!'es or classes o% modules2 including2 "ut not limited to2 device drivers.
'lit vie+ o% the Kernel
D!namicall! Loada"le Modules
insmod
Module:,o module:,o rmmod insmod
Module:,o moduleE,o rmmod
/unning Linux Kernel vmlinux
Linux Module Programming
Introduction to Module Programming ●
●
●
Module initialisation ( –
A module al+a!s "egins +ith the %unction s'ecifed +ithin module_init().
–
It is the entr! 'oint o% the module
–
It tells the ,ernel +hat %unctionalit! does the module 'rovides
Bod! o% the module ( –
Per%orms the core %unctions o% the module
–
Calls are executed "! user-s'ace or s!stem hard+are
Module cleanu' ( –
A module al+a!s ends +ith the %unction s'ecifed +ithin module_exit().
–
1xit 'oint o% the module
–
It undoes +hatever initiali4ation %unction 'er%ormed
Fello #orld /evisitedGG 5H Basic includes H5 include Jlinux5inith include Jlinux5moduleh 5H 'eci%! the license 5 MD.L1LIC1*1?PLN 5H 'eci%! the author H5 MD.L1A.TF/CDACN
Fello #orld /evisitedGG /* Initialisation !nction */ static int ""init hello"init(void) # $rint%(&Hello 'orld..n)+ ret!rn + /* ention the init !nction to the %ernel */ od!le"init(hello"init)+
Fello #orld /evisitedGG /* Clean!$ !nction */ static void ""e0it hello"e0it(void) # $rint%(&1ye or no2..n)+ /* ention the e0it !nction to the %ernel */ od!le"e0it(hello"e0it)+
The Ma,efle 'eci%! the out'ut fle o"-m (O helloo etu' the varia"les K1/*/C O 5li"5modules5shell uname -r5"uild P#D O shell '+d ma,e target %or com'iling the module modules ( ma,e -C K1/*/C MOP#D modules ma,e target %or cleaning u' the s'ace clean ( ma,e -C K1/*/C MOP#D clean
.tilities ●
ma,e modules ( Com'iles the module 'rogram
●
ma,e clean ( Clears the "inaries
●
insmod ( Loads the module
●
mod'ro"e ( Loads the module a%ter loading all the de'endenc! modules
●
rmmod ( .nload the module
●
lsmod ( list all the loaded ,ernel modules 5'roc5modules
●
modin%o ( ?et the in%ormation a"out the module
●
de'mod ( /e"uild the module de'endenc! index
The ,ernel "uild s!stem ●
●
3he root Makefile starts loo%ing or the so!rce ile in the location s$eciied by 'M=PATH' o$tion. 4nce the b!ild is co$lete, the ollo2ing iles are generated 5 –
modulenamec ( ource fle o% the module
–
modulenameo ( Com'iled %orm o% the source fle
–
–
–
–
–
modulenamemodc ( Contains the in%ormation a"out the module version in%ormation2 sections2 de'endencies etc modulenamemodo ( Com'iled %orm o% modulenamemodc modulename,o ( modulenameo & modulenamemodo are lin,ed to 'roduce this fnal com'iled version o% the module Modules!mvers ( holds the in%ormation o% the ex'orted s!m"ols modulesorder ( lists out the order in +hich the com'ilation and creation o% ,o ta,es
Module Programs Qs .ser-s'ace Programs ●
1vent Driven 1xecution
●
Concurrencies
●
Li"raries
●
egmentation )aults
●
De"ugging
●
tac, Limitations
Module Parameters ●
●
●
Parameters ma! "e 'assed to the modules during module insertion The main 'ur'ose is %or ex'licit customisation o% the driver environment The macro module_param(name, type,perm) is used to initiali4e the 'arameters –
name ( *ame o% the varia"le
–
type ( argument t!'e ( "ool2 int2 char'2 etc
–
perm ( Permissions associated ( I/.?2 I#./
.sage 5H Include the header H5 include Jlinux5module'aramh 5H Declare the varia"les H5 int n O :=N char str O FelloN 5H Declare the varia"les as module 'arameters H5 module'aramn2 int2 I/.?N module'aramstr2 char'2 I/.?N 5H Load the module as H5 insmod 5modulename,o nOER strOFai
The current 'rocess
The current 'rocess ●
●
●
●
The ,ernel code might execute in one o% these contexts ( –
Interru't Context
–
Process Context
–
Kernel threads
A glo"al 'ointer2 current 'oints to the relevant 'rocess +hen executing in 'rocess context The macro 'oints to the structure o% t!'e @struct tas,struct@ Jlinux5schedh +hich re'resents the 'rocess control "loc, o% linux 'rocesses The macro is invalid +hen the code is executing in interru't context or as a ,ernel thread
The current 'rocess usera''
struct tas,struct S long stateN void Hstac,N int 'rioN char HcommN 'idt 'idN N
Kernel code (process context) current
The Kernel !m"ol Ta"le
The ,ernel s!m"ol ta"le ●
●
●
●
The ta"le contains the addresses o% glo"al ,ernel items ( %unctions and varia"les2 that are needed to im'lement modulari4ed drivers The s!m"ol ta"le can "e read "! ( cat 5'roc5,alls!ms #hen a module is loaded2 an! s!m"ol exported "! the module "ecomes 'art o% the ,ernel s!m"ol ta"le *e+ modules can use the s!m"ols ex'orted "! other modules This conce't is termed as 'module stacking'.
1xam'le ( Module tac,ing Kernel .B Core .B Ke!"oard Driver
.B Mouse Driver
1x'orting s!m"ols ●
●
●
●
Modules can ex'ort services to other modules using ( EX!"T_#$%&!L(symol_name) macro !m"ols must "e ex'orted in the glo"al 'art o% the moduleUs fle2 ie2 outside o% an! %unction !m"ols are ex'orted onl! +hen the module is loaded The ex'orted s!m"ol is stored in a s'ecial 'art o% the module@s executa"le that is used used "! the ,ernel at load time to fnd the varia"les ex'orted "! the module
.sage 1x'orting module ( ex'c 5H Defne the %unction to "e ex'orted H5 void ex'ort%unc S 5H )unction "od! H5 5H 1x'ort the %unction to the ,ernel s!m"ol ta"le H5 17P/TVMBLex'ort%uncN
Im'orting module ( im'c 5H )unction declaration H5 extern void ex'ort%uncN 5H Call the ex'orted %unction H5 void %uncuse S ex'ort%uncN
.sage cont ●
●
●
●
Modifcations in the Ma,efle ( Com'ile the modules together o"-m (O ex'o im'o Install the modules ( ma,e modulesinstall Chec, the 'ath ( 5li"5modules5uname -r5extra .se modproe, instead o% insmod to load im',o mod'ro"e im'
●
The module ex',o +ill "e loaded 'rior to im',o
●
Chec, the out'ut o% lsmod.
insmod Qs mod'ro"e ●
●
●
●
mod'ro"e +or,s in similar +a! as that o% insmod2 "ut it also loads an! other modules that are re0uired "! the module !ou +ant to load I% !our module de'ends u'on three more modules2 !ou have to do insmod three times 'lus once %or loading that module itsel% But +ith mod'ro"e2 single time is suWcient It solves the de'endenc! and loads the modules +hich are re0uired Fo+ever2 mod'ro"e searches %or modules in standard directories2 so i% !our module is in directories other than
Fo+ mod'ro"e +or,s$ ●
●
Mod'ro"e chec,s the de'endenc! o% modules and loads those modules to resolve the de'endenc! To ,no+ the de'endenc!2 mod'ro"e loo,s into modules.dep fle –
●
●
Location o% mod'ro"ede' is 5li"5modules5uname -r5
This fle is u'dated "! de'mod de'mod onl! chec,s into some standard locations %or the module o"ect fles
Linux Boot Process
o%t+are Com'onents o% a runnning Linux Machine
●
Bootloader
●
Kernel
●
/oot )iles!stem –
!stem Li"raries
–
A''lications
–
and much more
o%t+are Com'onents o% a runnning Linux Machine
●
Bootloader
●
Kernel
●
/oot )iles!stem –
!stem Li"raries
–
A''lications
–
and much more
The Boot 'rocess Po+er * 5 /eset BI
-- PT -- Initialise the hard+are
tage-: Bootloader
-- MB/ -- Load stage-E "ootloader
tage-E Bootloader
-- ?/.B2 LIL2 etc -- Load the ,ernel image "4Image
Kernel
-- Decom'ress the image -- Per%orm the initialisations ( stac,s2 'age ta"les2 etc
The Boot 'rocess cont Initial /AM Dis, img
-- Tem'orar! )iles!stem -- Fel's in mounting the /) -- 'tional
Mount the /) Init 'rocess A''lications
-- tart 5s"in5init -- )irst user-s'ace 'rocess
/e%erences ●
●
Xonathan Cor"et2 Alessandro /u"ini and ?reg KroahFartman2Linux Device Drivers28rd 1dition2 @/eill! Pu"lications Linux "oot 'rocess ( htt'(55+++i"mcom5develo'er+or,s5li"rar!5llinux"oot5
●
++++i,i'ediacom
●
+++googlecom