Static Functions in Linux Device Driver

Embed Size (px)

Citation preview

  • 8/15/2019 Static Functions in Linux Device Driver

    1/4

    Static functions in Linux device driver?Is there a reason why most function definition in device driver in linux code is definedas static? Is there a reason for this?

    I was told this is for scoping and to prevent namespace pollution, could anyone

    explain it in detail why static definition is used in this context?

     ANS:-unctions declared static are not visi!le outside the translation unit they are

    defined in "a translation unit is !asically a #c file$# If a function does not need to !e called

    from outside the file, then it should !e made static so as to not pollute the glo!al

    namespace# %his ma&es conflicts !etween names that are the same are less li&ely to

    happen# 'xported sym!ols are usually indentified with some sort of su!system tag,

    which further reduces scope for conflict#

    (ften, pointers to these functions end up in structs, so they are actually called fromoutside the file they are defined in, !ut not !y their function name#

    porting linux on !are !oard

    )esterday I faced with an interview # In interview  he as&ed me for steps to porting Linux

    on new board# *y answer was,

    +# cross-compile u-!oot for !oard architecture#

    # cross-compile &ernel, with selecting driver for peripherals connected to !oard#

    # cross-compile filesystem, and port all on !oard#.ut Interviewer  is not happy with this answer#

     ANS:-

    /orting steps -

    • Install a cross-development environment#

    • Setup the !oard and ensure that the serial port is wor&ing so we can print data

    through the serial port#

    • 0ownload and install the Linux &ernel, most of the porting wor& will !e done at

    this level#

    •  Add !oard specific code into the &ernel tree#

    • .uild a &ernel image to run on the !oard

    • %est that early &ernel print& is wor&ing

    • 1et the real print& wor&ing with the serial console#

    • or a new !oard, a new !oard-specific directory should !e added as well as

    support for interrupt handling, &ernel timer services and mapping for memory areas#

    • 'thernet drivers are usually the next drivers to focus on as they ena!le setup of

    NS root file system to get access to user utilities and applications#

    http://stackoverflow.com/questions/348984/static-functions-in-linux-device-driverhttp://stackoverflow.com/questions/9378379/porting-linux-on-bare-boardhttp://stackoverflow.com/questions/9378379/porting-linux-on-bare-boardhttp://stackoverflow.com/questions/348984/static-functions-in-linux-device-driver

  • 8/15/2019 Static Functions in Linux Device Driver

    2/4

    • ilesystem can !e provided in different forms which are listed on Linuxilesystem

    2ow to port a linux driver , which is compiled in #3

    &ernel ,without compiling in other new version of &ernel

    %han&s to every one,

    %his is the 4uestion as&ed in one of the interview i faced#

    I have a Linux device driver which was compiled in Linux &ernel version #3#I wouldli&e to port the same driver in a Linux /5 which has &ernel #6 without compiling in

    new versions#

    Is it possi!le ? If it is possi!le please let me &now how# If it is not possi!le please letme &now why not ?

    now it7s not possi!le:

    • usually, a 8driver8 is a !inary &ernel-module

    •   porting  will involve code-changes to the &ernel module# if you change the code,

    you need to compile it, in order to get a !inary#

    • since &ernel modules run in &ernel space, it is crucial that they are ro!ust# since

    parts of the &ernel-A/I change every now and then, trying to use a module compiled

    for &ernel-6 with another &ernel-), might either not load !ecause of missing sym!ols

    "if you are luc&y$ or lead to a &ernel panic !ecause semantics have changed#

    • !tw, all this is not really related to 2.6.x vs 3.y, !ut holds true for any &ernel

    version

    !ut then: of course in theory it is possi!le to 8write8 a &ernel-module as !inary code inyour favourite hex-editor, without resorting to compilers and such# this would allow you to

    8port8 a driver from one &ernel to another without recompilation# i guess this is not for

    humans though##

    No you cannot port module which is compiled for one version to other version#

    %he reason is as follows

    Modules are strongly tied to the data structures and function prototypes dened in a

    particular kernel version; the interface seen by a module can change signicantly from

    one kernel version to the next. This is especially true of development kernels of course

    http://stackoverflow.com/questions/17830427/how-to-port-a-linux-driver-which-is-compiled-in-2-6-kernel-without-compilinghttp://stackoverflow.com/questions/17830427/how-to-port-a-linux-driver-which-is-compiled-in-2-6-kernel-without-compilinghttp://stackoverflow.com/questions/17830427/how-to-port-a-linux-driver-which-is-compiled-in-2-6-kernel-without-compilinghttp://stackoverflow.com/questions/17830427/how-to-port-a-linux-driver-which-is-compiled-in-2-6-kernel-without-compiling

  • 8/15/2019 Static Functions in Linux Device Driver

    3/4

     The kernel does not !ust assume that a given module has been built against the proper

    kernel version. "ne of the steps in the build process is to link your module against a le

    #called vermagic.o$ from the current kernel tree; this ob!ect contains a fair amount of

    information about the kernel the module %as built for including the target kernel version

    compiler version and the settings of a number of important conguration variables.

    &hen an attempt is made to load a module this information can be tested for

    compatibility %ith the running kernel. 'f things don(t match

    the module is not loaded9 instead, you see something li&e:

    ) insmod hello.ko

    *rror inserting +.,hello.ko+- / 'nvalid module format

     A loo& in the system log file "varlogmessages or whatever your system is configured to

    use$ will reveal the specific pro!lem that caused the module to fail to load#

    ;ernel interfaces often change !etween releases# If you are writing a module that is

    intended to wor& with multiple versions of the &ernel "especially if it must wor& acrossmahat are coding conventions for using floating-point in

    Linux device drivers?

    Short answer: ;ernel code can use floating point if this use is surrounded

    !y kernel0fpu0begin#$kernel0fpu0end#$# %hese function handle saving and restoring the

    fpu context# Also, they call preempt0disable#$preempt0enable#$, which means no

    sleeping, page faults etc# in the code !etween those functions# 1oogle the function

    names for more information#

    If I understand correctly, whenever a ;* is running, it is using a hardware context "or

    hardware thread or register set -- whatever you want to call it$ that has !een preempted

    from some application thread#

    No, a &ernel module can run in user context as well "eg# when userspace calls syscallson a device provided !y the ;*$# It has, however, no relation to the float issue#

    If you write your ;* in c, the compiler will correctly insure that the general-purpose

    registers are properly saved and restored "much as in an application$, !ut that doesn7t

    automatically happen with floating-point registers#

    %hat is not !ecause of the compiler, !ut !ecause of the &ernel context-switching code#

    *emory usage of a &ernel module

    http://stackoverflow.com/questions/431081/what-are-coding-conventions-for-using-floating-point-in-linux-device-drivershttp://stackoverflow.com/questions/431081/what-are-coding-conventions-for-using-floating-point-in-linux-device-drivershttp://stackoverflow.com/questions/662526/memory-usage-of-a-kernel-modulehttp://stackoverflow.com/questions/431081/what-are-coding-conventions-for-using-floating-point-in-linux-device-drivershttp://stackoverflow.com/questions/431081/what-are-coding-conventions-for-using-floating-point-in-linux-device-drivershttp://stackoverflow.com/questions/662526/memory-usage-of-a-kernel-module

  • 8/15/2019 Static Functions in Linux Device Driver

    4/4

    >hile trying to estimate the amount of memory consumed !y a &ernel module "usually

    device drivers$,I tried using the size utility which gave the sie of the static memory

    areas of the #&o " #!ss, #data, #text etc$# So I was expecting the sum of these values to !e

    exactly e4ual to the output given !y the lsmod command immediately after inserting the

    module#

    No dynamic memory allocation"&malloc or vmalloc$ is performed in the init"$ function to

    ensure that it isn7t causing the difference#So why is there a mismatch?

    5uriously the mismatch was found to !e a fixed amount most of the time@@

    %he command outputs are listed !elow

    sie chardev#&o

    text data bss dec hex lename/12 /424/6 /42636 fa21c chardev.ko

    lsmod

    Module 5ie 7sed by Tainted- 8chardev /42944 4 :ive 4xc44d444

    *ay !e the functions used !y the module are counted into the module sie ? %ry

    cat ,proc,kallsyms < grep module0name

    %he difference !etween the two sie is B# %ext C data C B D +B# *ay !e this is

    some &ind of granularity pro!lem ? I don7t &now how the sie is calculated inside the&ernel###

    2owever, &ernel code and data are allocated using dynamic memory# And &malloc usespre-allocated !loc& of memory, so it is 4uite li&ely that there is some rounding up whencode and data sections are allocated#

    %ry to increment the sie of the data sections and see if the lsmod reported sie change