1 intro to module programming(1)

Upload: anup-vishwakarma

Post on 03-Jun-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/12/2019 1 Intro to Module Programming(1)

    1/55

    Linux Device Drivers- Introduction & Basic Module Programming

    A Raghavendra Rao([email protected])

    C-DAC, Hyderabad

    mailto:[email protected]:[email protected]
  • 8/12/2019 1 Intro to Module Programming(1)

    2/55

    Agenda

    Kernels and Classifcations

    The Linux Kernel

    Linux Modules Linux Module Programming

    The Kernel !m"ol Ta"le

    Linux Boot Process

  • 8/12/2019 1 Intro to Module Programming(1)

    3/55

    Kernels and Classifcations

  • 8/12/2019 1 Intro to Module Programming(1)

    4/55

    #hat is a Kernel $$

    Core o% the 'erating !stem

    ervices (

    Process Management Memor! Management

    Device Management

    )iles!stem Management

    *et+or, Management

    ecurit!

  • 8/12/2019 1 Intro to Module Programming(1)

    5/55

    La!out o% the Kernel

  • 8/12/2019 1 Intro to Module Programming(1)

    6/55

    Kernel in action

    A''lication

    .ser

    !stem Li"rar!

    !stem Call

    .ser'ace

    Kernel

    /esources

    Kernel'ace

    /e0uest made

    "! the user

  • 8/12/2019 1 Intro to Module Programming(1)

    7/55

    Kernel in action

    A''lication

    .ser

    !stem Li"rar!

    !stem Call

    .ser'ace

    Kernel

    /esources

    Kernel'ace

    A''lication calls

    s!stem li"rar!routine

  • 8/12/2019 1 Intro to Module Programming(1)

    8/55

    Kernel in action

    A''lication

    .ser

    !stem Li"rar!

    !stem Call

    .ser'ace

    Kernel

    /esources

    Kernel'ace

    !stem li"rar! ma,es a s!stem call

  • 8/12/2019 1 Intro to Module Programming(1)

    9/55

    Kernel in action

    A''lication

    .ser

    !stem Li"rar!

    !stem Call

    .ser'ace

    Kernel

    /esources

    Kernel'ace

    !stem call

    Inter%ace tra'sthe ,ernel

  • 8/12/2019 1 Intro to Module Programming(1)

    10/55

    Kernel in action

    A''lication

    .ser

    !stem Li"rar!

    !stem Call

    .ser'ace

    Kernel

    /esources

    Kernel'ace

    Kernel access

    the resources

    as 'er re0uest

  • 8/12/2019 1 Intro to Module Programming(1)

    11/55

    Kernel in action

    A''lication

    .ser

    !stem Li"rar!

    !stem Call

    .ser'ace

    Kernel

    /esources

    Kernel'ace

    Kernel

    com'letes there0uest

  • 8/12/2019 1 Intro to Module Programming(1)

    12/55

    Kernel in action

    A''lication

    .ser

    !stem Li"rar!

    !stem Call

    .ser'ace

    Kernel

    /esources

    Kernel'ace

    /eturns the

    result

  • 8/12/2019 1 Intro to Module Programming(1)

    13/55

    Kernel in action

    A''lication

    .ser

    !stem Li"rar!

    !stem Call

    .ser'ace

    Kernel

    /esources

    Kernel'ace

    /eturns the result to s!stem li"rar!

  • 8/12/2019 1 Intro to Module Programming(1)

    14/55

    Kernel in action

    A''lication

    .ser

    !stem Li"rar!

    !stem Call

    .ser'ace

    Kernel

    /esources

    Kernel'ace

    /eturns the

    result to thea''lication

  • 8/12/2019 1 Intro to Module Programming(1)

    15/55

    Kernel in action

    A''lication

    .ser

    !stem Li"rar!

    !stem Call

    .ser'ace

    Kernel

    /esources

    Kernel'ace

    .ser re0uest

    'rocessed

  • 8/12/2019 1 Intro to Module Programming(1)

    16/55

    1xam'le

  • 8/12/2019 1 Intro to Module Programming(1)

    17/55

    T!'es o% Kernel Architectures

    Monolithic Kernel Micro,ernel

  • 8/12/2019 1 Intro to Module Programming(1)

    18/55

    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 ( )aster Processing

    Disadvantages (

    Crash insecure2 'orting in3exi"ilit!2 ,ernel si4e ex'losion 1xam'les (

    M-D2 .nix2 Linux2 etc

  • 8/12/2019 1 Intro to Module Programming(1)

    19/55

    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 ( Crash /esistant2 Porta"le2 maller i4e

    Disadvantages (

    lo+er Processing due to additional message 'assing 1xam'les (

    6*72 Minix 82 1/2 Ke!K2 #indo+s *T

  • 8/12/2019 1 Intro to Module Programming(1)

    20/55

    The Linux Kernel

    I d h L K l

  • 8/12/2019 1 Intro to Module Programming(1)

    21/55

    Introduction to the Linux Kernel

    tarted o9 in :;;: "! Linus Torvaldsas a

    sim'le terminal driver %or Intel

  • 8/12/2019 1 Intro to Module Programming(1)

    22/55

    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!ers2etc

  • 8/12/2019 1 Intro to Module Programming(1)

    23/55

    Linux Modules

    I t d ti t Li M d l

  • 8/12/2019 1 Intro to Module Programming(1)

    24/55

    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 i % th K l

  • 8/12/2019 1 Intro to Module Programming(1)

    25/55

    'lit vie+ o% the Kernel

    D i ll L d "l M d l

  • 8/12/2019 1 Intro to Module Programming(1)

    26/55

    D!namicall! Loada"le Modules

    /unning Linux Kernel

    vmlinux

    Module:,o

    insmod

    rmmod

    module:,o

    insmod

    rmmod

    Module:,o

    insmod

    rmmod

    moduleE,o

    insmod

    rmmod

  • 8/12/2019 1 Intro to Module Programming(1)

    27/55

    Linux Module Programming

    Introduction to Module Programming

  • 8/12/2019 1 Intro to Module Programming(1)

    28/55

    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 /evisited GG

  • 8/12/2019 1 Intro to Module Programming(1)

    29/55

    Fello #orld /evisitedGG

    5H Basic includes H5include Jlinux5inith

    include Jlinux5moduleh

    5H 'eci%! the license 5

    MD.L1LIC1*1?PLN

    5H 'eci%! the author H5

    MD.L1A.TF/CDACN

    Fello #orld /evisited GG

  • 8/12/2019 1 Intro to Module Programming(1)

    30/55

    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 /evisited GG

  • 8/12/2019 1 Intro to Module Programming(1)

    31/55

    /* 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)+

    Fello #orld /evisitedGG

    The Ma,efle

  • 8/12/2019 1 Intro to Module Programming(1)

    32/55

    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

  • 8/12/2019 1 Intro to Module Programming(1)

    33/55

    .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

  • 8/12/2019 1 Intro to Module Programming(1)

    34/55

    The ,ernel "uild s!stem

    3he root Makefilestarts 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

  • 8/12/2019 1 Intro to Module Programming(1)

    35/55

    Module Programs Qs .ser s'ace Programs

    1vent Driven 1xecution

    Concurrencies

    Li"raries egmentation )aults

    De"ugging tac, Limitations

    Module Parameters

  • 8/12/2019 1 Intro to Module Programming(1)

    36/55

    Module Parameters

    Parameters ma! "e 'assed to the modules during moduleinsertion

    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"letype( argument t!'e ( "ool2 int2 char'2 etc

    perm ( Permissions associated ( I/.?2 I#./

    .sage

  • 8/12/2019 1 Intro to Module Programming(1)

    37/55

    .sage

    5H Include the header H5 include Jlinux5module'aramh

    5H Declare the varia"les H5

    int n O :=Nchar 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

  • 8/12/2019 1 Intro to Module Programming(1)

    38/55

    The current'rocess

    The current 'rocess

  • 8/12/2019 1 Intro to Module Programming(1)

    39/55

    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

  • 8/12/2019 1 Intro to Module Programming(1)

    40/55

    The current 'rocess

    usera''

    Kernel code

    (process context)

    struct tas,struct Slong stateN

    void Hstac,N

    int 'rioN

    char HcommN'idt 'idN

    N

    current

  • 8/12/2019 1 Intro to Module Programming(1)

    41/55

    The Kernel !m"ol Ta"le

    The ,ernel s!m"ol ta"le

  • 8/12/2019 1 Intro to Module Programming(1)

    42/55

    !

    The ta"le contains the addresses o% glo"al ,ernelitems ( %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

  • 8/12/2019 1 Intro to Module Programming(1)

    43/55

    ' g

    Kernel

    .B Core

    .B Ke!"oard

    Driver

    .B Mouse

    Driver

    1x'orting s!m"ols

  • 8/12/2019 1 Intro to Module Programming(1)

    44/55

    ' g !

    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

  • 8/12/2019 1 Intro to Module Programming(1)

    45/55

    g

    5H Defne the %unction to "e ex'orted

    H5

    void ex'ort%unc

    S5H )unction "od! H5

    5H 1x'ort the %unction to the ,ernel

    s!m"ol ta"le H517P/TVMBLex'ort%uncN

    5H )unction declaration H5

    extern void ex'ort%uncN

    5H Call the ex'orted %unction H5void %uncuse

    S

    ex'ort%uncN

    1x'orting module ( ex'c Im'orting module ( im'c

    .sage cont

  • 8/12/2019 1 Intro to Module Programming(1)

    46/55

    g

    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% insmodto 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

  • 8/12/2019 1 Intro to Module Programming(1)

    47/55

    '

    mod'ro"e +or,s in similar +a! as that o% insmod2 "ut italso loads an! other modules that are re0uired "! the

    module !ou +ant to load

    I% !our module de'ends u'on three more modules2 !ouhave to do insmod three times 'lus once %or loading

    that module itsel%

    But +ith mod'ro"e2 single time is suWcient It solves thede'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$

  • 8/12/2019 1 Intro to Module Programming(1)

    48/55

    Mod'ro"e chec,s the de'endenc! o% modules andloads those modules to resolve the de'endenc!

    To ,no+ the de'endenc!2 mod'ro"e loo,s into

    modules.depfle

    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

  • 8/12/2019 1 Intro to Module Programming(1)

    49/55

    Linux Boot Process

    o%t+are Com'onents o% a runnning Linux Machine

  • 8/12/2019 1 Intro to Module Programming(1)

    50/55

    Bootloader

    Kernel

    /oot )iles!stem !stem Li"raries

    A''lications

    and much more

    o%t+are Com'onents o% a runnning Linux Machine

  • 8/12/2019 1 Intro to Module Programming(1)

    51/55

    Bootloader Kernel

    /oot )iles!stem !stem Li"raries

    A''lications

    and much more

    The Boot 'rocess

  • 8/12/2019 1 Intro to Module Programming(1)

    52/55

    -- PT

    -- Initialise the hard+are

    tage-: Bootloader -- MB/-- Load stage-E "ootloader

    BI

    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

    Po+er * 5 /eset

    The Boot 'rocess cont

  • 8/12/2019 1 Intro to Module Programming(1)

    53/55

    -- Tem'orar! )iles!stem

    -- Fel's in mounting the /)

    -- 'tional

    Mount the /)

    Initial /AM Dis, img

    Init 'rocess-- tart 5s"in5init

    -- )irst user-s'ace 'rocess

    A''lications

    /e%erences

  • 8/12/2019 1 Intro to Module Programming(1)

    54/55

    Xonathan Cor"et2 Alessandro /u"ini and ?reg Kroah-Fartman2Linux Device Drivers28rd 1dition2 @/eill!

    Pu"lications

    Linux "oot 'rocess (htt'(55+++i"mcom5develo'er+or,s5li"rar!5l-

    linux"oot5

    ++++i,i'ediacom

    +++googlecom

    http://www.wikipedia.com/http://www.wikipedia.com/
  • 8/12/2019 1 Intro to Module Programming(1)

    55/55

    Tank $ou )