|
|
Line 22: |
Line 22: |
| * Implement a simple '''loadable module'''. | | * Implement a simple '''loadable module'''. |
| * Create a '''Makefile.''' | | * Create a '''Makefile.''' |
− | * Compile a '''module.''' | + | * '''Compile''' a '''module.''' |
| | | |
| |- | | |- |
Line 352: |
Line 352: |
| Press '''Enter''' | | Press '''Enter''' |
| | | |
− | Highlight '''modinfo'''
| |
| | | |
− | Highlight '''filename'''
| |
− |
| |
− | Highlight '''description'''
| |
− |
| |
− | Highlight '''author, license'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Let us see the details of a '''module'''.
| |
− |
| |
− | Type''' modinfo space simple_module dot ko.'''
| |
− |
| |
− | This '''command''' displays information about the '''Linux kernel module. '''
| |
− |
| |
− | The '''filename''' field contains an '''absolute path''' to the '''.ko kernel object file.'''
| |
− |
| |
− | This field displays a short description of a '''module.'''
| |
− |
| |
− | '''Author''' field shows author and '''License''' field shows the '''license''' of the '''module.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''srcversion'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| The '''srcversion field '''contains the source code version used to compile a''' module'''.
| |
− |
| |
− | It is calculated automatically at build time.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''depends'''
| |
− |
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| The''' depends''' field contains all '''modules''' on which this '''module '''depends.
| |
− |
| |
− | It is shown empty here as this '''module''' does not depend on any other '''module.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''retpoline'''
| |
− |
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| '''Retpoline''' field indicates that the '''module '''is '''compiled''' with '''retpoline '''method.
| |
− |
| |
− | '''Modules''' must also be compiled with a '''retpoline-aware compiler.'''
| |
− |
| |
− | Otherwise the '''kernel '''can be vulnerable.
| |
− |
| |
− | '''Retpoline technique''' protects the kernel from the hacker’s attacks.
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''name'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| The '''name''' field contains the '''name '''of a''' module.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''vermagic'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| This field shows the''' version magic''' number of a '''module.'''
| |
− |
| |
− | This '''version magic''' number is used while loading the '''kernel module.'''
| |
− |
| |
− | The '''version magic''' number of '''module''' and current '''kernel version '''should be the same.
| |
− |
| |
− | Otherwise it will fail to load in the current '''kernel. '''
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Type >> '''make clean'''
| |
− |
| |
− | Press''' Enter'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Type '''make space clean.'''
| |
− |
| |
− | This '''command''' removes all '''object files''' that are created after the '''compilation.'''
| |
− |
| |
− | '''Type ls.'''
| |
− |
| |
− | Now there are no '''object files.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"|
| |
− | | style="border:0.5pt solid #000000;padding:0.176cm;"| With this, we come to the end of this tutorial. Let us summarize.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Slide 6:
| |
− |
| |
− | Summary:
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| In this tutorial, we learned how to
| |
− |
| |
− | * Implement a '''simple loadable module.'''
| |
− | * Create a '''Makefile'''.
| |
− | * Compile a '''module.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Slide 7:
| |
− |
| |
− | Assignment:
| |
− |
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| As an assignment,
| |
− |
| |
− | # Open the''' simple_module.c file'''
| |
− | # Change the module description''' '''using''' MODULE_DESCRIPTION macro '''
| |
− | # Run the '''modinfo command'''
| |
− | # See the output of description.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Slide 8:
| |
− |
| |
− | About Spoken Tutorial Project:
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| The video at the following link summarizes the Spoken Tutorial project.
| |
− |
| |
− | Please download and watch it.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Slide 9:
| |
− |
| |
− | Spoken Tutorial workshops:
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| The''' Spoken Tutorial Project''' Team:
| |
− |
| |
− | * conducts workshops and
| |
− | * gives certificates.
| |
− |
| |
− | For more details, please write to us.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Slide 10:
| |
− |
| |
− | Forum questions:
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| * Do you have questions in THIS Spoken Tutorial?
| |
− | * Please visit this site
| |
− | * Choose the minute and second where you have the question.
| |
− | * Explain your question briefly.
| |
− | * The Spoken Tutorial project will ensure an answer
| |
− | * You will have to register to ask questions
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Slide 11:
| |
− |
| |
− | Forum for specific questions:
| |
− |
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| * The Spoken Tutorial forum is for specific questions on this tutorial.
| |
− | * Please do not post unrelated and general questions on them.
| |
− | * This will help reduce the clutter.
| |
− | * With less clutter, we can use these discussions as instructional material.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Slide 12:
| |
− |
| |
− | Forum for specific questions:
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| * Do you have general or technical questions on Embedded Linux Device Driver?
| |
− | * Please visit the FOSSEE forum and post your question.
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Slide 13:
| |
− |
| |
− | Acknowledgment:
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Spoken Tutorial project is funded by MHRD, Government of India.
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Slide 14:
| |
− |
| |
− | Thank you slide:
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| This tutorial has been contributed by FOSSEE and Spoken Tutorial Project, IIT Bombay.
| |
− |
| |
− | This is Mayuri Panchakshari signing off.
| |
− |
| |
− | Thanks for watching.
| |
− |
| |
− | |}
| |
− | .
| |
− |
| |
− | I have given the description as “'''First Linux kernel module”.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''static int init_func(void)'''
| |
− |
| |
− | Highlight '''return(0)'''
| |
− |
| |
− | Highlight '''module_init()'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| The '''init function '''will execute when a '''module '''is loaded into the '''kernel'''.
| |
− |
| |
− | So it is called as an '''initialization''' '''function''' of a '''module'''.
| |
− |
| |
− | If this '''function''' fails then the '''error '''will be returned.
| |
− |
| |
− | It will '''return''' 0 to the '''kernel''' on successful execution.
| |
− |
| |
− | Initialization function prints a simple message and return zero.
| |
− |
| |
− | The '''module init''' '''macro''' is used to register the '''init function.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight''' void exit_func (void)'''
| |
− |
| |
− | Highlight '''module_exit()'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| The''' exit function '''will execute when the '''module '''is unloaded from the '''kernel'''.
| |
− |
| |
− | It is mostly used to cleanup the '''resources''' acquired by the '''module'''
| |
− |
| |
− | The''' exit function''' will undo whatever the''' init function '''did.
| |
− |
| |
− | The '''module underscore exit macro is''' used to register the '''exit function. '''
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''printk'''
| |
− |
| |
− | Highlight '''KERN_INFO'''
| |
− |
| |
− | Press >> '''Ctrl + S'''
| |
− |
| |
− | Press '''Enter'''
| |
− |
| |
− | Click on '''close''' option.
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| In '''kernel '''programming, '''printk '''is used as a printing''' function.'''
| |
− |
| |
− | It is similar to '''printf function '''in '''C '''programming.
| |
− |
| |
− | Using this '''function,''' string data will get loaded in the '''kernel''' log '''buffer'''.
| |
− |
| |
− | '''printk''' has an optional prefix string, '''Loglevel '''such as''' KERN_INFO.'''
| |
− |
| |
− | '''Loglevel '''specifies the type of message being sent to the kernel message log.
| |
− |
| |
− | '''KERN underscore INFO''' is used for informational messages.
| |
− |
| |
− | For more log levels, go through the '''Additional reading material''' link of this tutorial.
| |
− |
| |
− | Press '''Ctrl + S''' keys to save the '''program '''.
| |
− |
| |
− | Close the '''file.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Switch back to the '''terminal'''.
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Switch back to the '''terminal'''.
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"|
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Let us compile the code.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Type >> '''gedit Makefile'''
| |
− |
| |
− | Highlight the makefile
| |
− |
| |
− | Press '''Enter'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| For that, we have to create a''' Makefile.'''
| |
− |
| |
− | Type '''gedit Makefile '''
| |
− |
| |
− | The '''Makefile''' is a special file containing '''shell '''commands.
| |
− |
| |
− | Press '''Enter.'''
| |
− |
| |
− | Create a '''Makefile '''where you have saved the '''simple_module dot c .'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Copy paste the makefile content
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Type the '''code '''as shown.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''obj-m'''
| |
− |
| |
− | Highlight '''simple_module.o'''
| |
− |
| |
− | Highlight '''all:'''
| |
− |
| |
− | Highlight '''clean:'''
| |
− |
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| The''' obj hyphen m''' variable tells the '''kernel Makefile '''that this '''module '''needs to be compiled.
| |
− |
| |
− | When you run the '''make''' '''all '''command''',''' then commands under '''all''' section will be executed.
| |
− |
| |
− | If you run the '''make clean '''command, it executes commands under the '''clean '''section.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''-C'''
| |
− |
| |
− | Highlight '''uname -r'''
| |
− | Highlight''' (PWD)'''
| |
− | Highlight '''M=$(PWD)'''
| |
− | Highlight''' modules'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Using '''hyphen C''', we are running '''make command '''inside the''' kernel source directory.'''
| |
− |
| |
− | As you know,''' uname hyphen r''' finds the current '''Linux kernel''' version of a '''system.'''
| |
− |
| |
− | '''PWD '''means the '''present working directory.'''
| |
− |
| |
− | This option tells the '''kernel''' '''makefile''' that the '''source''' '''code '''for the driver is in '''PWD'''.
| |
− |
| |
− | The word '''modules''' tell the '''kernel makefile''' to build the '''modules'''.
| |
− |
| |
− | But it will not build the complete''' kernel source code.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''clean'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| The word '''clean''' tells the '''kernel makefile '''to clean the generated '''object files.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Press >> '''Ctrl + S'''
| |
− |
| |
− | Click on the '''close'''.
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Save the '''file''' and close the '''editor'''.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Open the '''terminal'''
| |
− |
| |
− | Type >> '''make all'''
| |
− |
| |
− | Press '''Enter'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Switch back to the '''terminal'''.
| |
− |
| |
− | Let us compile the '''program''',
| |
− |
| |
− | Type '''make space all.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Type''' ls '''and''' '''Press''' Enter'''
| |
− |
| |
− | Highlight''' ls '''
| |
− |
| |
− | Show the output
| |
− |
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Now type''' ls.'''
| |
− |
| |
− | This command lists the '''files''' in the current '''directory.'''
| |
− |
| |
− | Here, you can see the number of '''files '''created in this '''directory'''.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight''' module.symvers'''
| |
− |
| |
− | Highlight''' mod.c '''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| The''' module.symvers''' file contains a list of all '''symbols'''
| |
− |
| |
− | These '''symbols '''are exported from the '''kernel '''build.
| |
− |
| |
− | The '''mod.c file '''contains information about the '''module'''.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight''' mod.o'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| '''Mod.o''' file is '''object file''' resulting from the compilation of the '''mod.c file.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight''' .o'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| This''' object file''' is created from the compilation of the '''module source file'''.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight''' modules.order'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| It will list out the order in which the compilation and creation of the '''.ko file '''takes place.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight''' .ko'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| This is the final '''kernel module''' binary that is loaded into the '''kernel.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Type >> '''clear'''
| |
− |
| |
− | Press''' Enter'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Clear the screen
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Type '''>> modinfo simple_module.ko'''
| |
− |
| |
− | Press '''Enter'''
| |
| | | |
| Highlight '''modinfo''' | | Highlight '''modinfo''' |
| | | |
− | Highlight '''filename'''
| |
− |
| |
− | Highlight '''description'''
| |
− |
| |
− | Highlight '''author, license'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Let us see the details of a '''module'''.
| |
− |
| |
− | Type''' modinfo space simple_module dot ko.'''
| |
− |
| |
− | This command displays information about the '''Linux kernel module. '''
| |
− |
| |
− | The '''filename''' field contains an absolute path to the .'''ko kernel''' '''object file.'''
| |
− |
| |
− | This field displays a short description of a '''module.'''
| |
− |
| |
− | Author field shows author and '''license '''field shows the '''license''' of the '''module.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''srcversion'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| The '''srcversion field '''contains the source code version used to compile a''' module'''.
| |
− |
| |
− | It is calculated automatically at build time.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''depends'''
| |
− |
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| The''' depends''' field contains all '''modules''' on which this '''module '''depends.
| |
− |
| |
− | It is shown empty here as this '''module '''does not depend on any other '''module.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''retpoline'''
| |
− |
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| '''Retpoline''' field indicates that the '''module '''is compiled with '''retpoline '''method.
| |
− |
| |
− | '''Modules''' must also be compiled with a '''retpoline-aware compiler.'''
| |
− |
| |
− | Otherwise the '''kernel '''can be vulnerable.
| |
− |
| |
− | '''Retpoline technique''' protects the kernel from the hacker’s attacks.
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''name'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| The '''name''' field contains the '''name '''of a''' module.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''vermagic'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| This field shows the''' version magic''' number of a '''module.'''
| |
− |
| |
− | This '''version magic''' number is used while loading the '''kernel module.'''
| |
− |
| |
− | The '''version magic''' number of '''module''' and current '''kernel version '''should be the same.
| |
− |
| |
− | Otherwise it will fail to load in the current '''kernel. '''
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Type >> '''make clean'''
| |
− |
| |
− | Press''' Enter'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Type '''make space clean.'''
| |
− |
| |
− | This '''command''' removes all '''object files''' that are created after the '''compilation.'''
| |
− |
| |
− | '''Type ls.'''
| |
− |
| |
− | Now there are no '''object files.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"|
| |
− | | style="border:0.5pt solid #000000;padding:0.176cm;"| With this, we come to the end of this tutorial. Let us summarize.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Slide 6:
| |
− |
| |
− | Summary:
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| In this tutorial, we learned how to
| |
− |
| |
− | * Implement a '''simple loadable module.'''
| |
− | * Create a '''Makefile'''.
| |
− | * Compile a '''module.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Slide 7:
| |
− |
| |
− | Assignment:
| |
− |
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| As an assignment,
| |
− |
| |
− | # Open the''' simple_module.c file'''
| |
− | # Change the module description''' '''using''' MODULE_DESCRIPTION macro '''
| |
− | # Run the '''modinfo''' command
| |
− | # See the output of description.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Slide 8:
| |
− |
| |
− | About Spoken Tutorial Project:
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| The video at the following link summarizes the Spoken Tutorial project.
| |
− |
| |
− | Please download and watch it.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Slide 9:
| |
− |
| |
− | Spoken Tutorial workshops:
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| The''' Spoken Tutorial Project''' Team:
| |
− |
| |
− | * conducts workshops and
| |
− | * gives certificates.
| |
− |
| |
− | For more details, please write to us.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Slide 10:
| |
− |
| |
− | Forum questions:
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| * Do you have questions in THIS Spoken Tutorial?
| |
− | * Please visit this site
| |
− | * Choose the minute and second where you have the question.
| |
− | * Explain your question briefly.
| |
− | * The Spoken Tutorial project will ensure an answer
| |
− | * You will have to register to ask questions
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Slide 11:
| |
− |
| |
− | Forum for specific questions:
| |
− |
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| * The Spoken Tutorial forum is for specific questions on this tutorial.
| |
− | * Please do not post unrelated and general questions on them.
| |
− | * This will help reduce the clutter.
| |
− | * With less clutter, we can use these discussions as instructional material.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Slide 12:
| |
− |
| |
− | Forum for specific questions:
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| * Do you have general or technical questions on Embedded Linux Device Driver?
| |
− | * Please visit the FOSSEE forum and post your question.
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Slide 13:
| |
− |
| |
− | Acknowledgment:
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Spoken Tutorial project is funded by MHRD, Government of India.
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Slide 14:
| |
− |
| |
− | Thank you slide:
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| This tutorial has been contributed by FOSSEE and Spoken Tutorial Project, IIT Bombay.
| |
− |
| |
− | This is Mayuri Panchakshari signing off.
| |
− |
| |
− | Thanks for watching.
| |
− |
| |
− | |}
| |
− | .
| |
− |
| |
− | I have given the description as “'''First Linux kernel module”.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''static int init_func(void)'''
| |
− |
| |
− | Highlight '''return(0)'''
| |
− |
| |
− | Highlight '''module_init()'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| The '''init function '''will execute when a '''module '''is loaded into the '''kernel'''.
| |
− |
| |
− | So it is called as an '''initialization''' '''function''' of a '''module'''.
| |
− |
| |
− | If this '''function''' fails then the '''error '''will be returned.
| |
− |
| |
− | It will '''return''' 0 to the '''kernel''' on successful '''execution'''.
| |
− |
| |
− | '''Initialization function''' prints a simple message and return zero.
| |
− |
| |
− | The '''module init''' '''macro''' is used to register the '''init function.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight''' void exit_func (void)'''
| |
− |
| |
− | Highlight '''module_exit()'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| The''' exit function '''will execute when the '''module '''is unloaded from the '''kernel'''.
| |
− |
| |
− | It is mostly used to cleanup the '''resources''' acquired by the '''module'''
| |
− |
| |
− | The''' exit function''' will undo whatever the''' init function '''did.
| |
− |
| |
− | The '''module underscore exit macro is''' used to register the '''exit function. '''
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''printk'''
| |
− |
| |
− | Highlight '''KERN_INFO'''
| |
− |
| |
− | Press >> '''Ctrl + S'''
| |
− |
| |
− | Press '''Enter'''
| |
− |
| |
− | Click on '''close''' option.
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| In '''kernel '''programming, '''printk '''is used as a printing''' function.'''
| |
− |
| |
− | It is similar to '''printf function '''in '''C '''programming.
| |
− |
| |
− | Using this '''function,''' string data will get loaded in the '''kernel''' log '''buffer'''.
| |
− |
| |
− | '''printk''' has an optional prefix string, '''Loglevel '''such as''' KERN_INFO.'''
| |
− |
| |
− | '''Loglevel '''specifies the type of message being sent to the kernel message log.
| |
− |
| |
− | '''KERN underscore INFO''' is used for informational messages.
| |
− |
| |
− | For more log levels, go through the '''Additional reading material''' link of this tutorial.
| |
− |
| |
− | Press '''Ctrl + S''' keys to save the '''program '''.
| |
− |
| |
− | Close the '''file.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Switch back to the '''terminal'''.
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Switch back to the '''terminal'''.
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"|
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Let us compile the code.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Type >> '''gedit Makefile'''
| |
− |
| |
− | Highlight the makefile
| |
− |
| |
− | Press '''Enter'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| For that, we have to create a''' Makefile.'''
| |
− |
| |
− | Type '''gedit Makefile '''
| |
− |
| |
− | The '''Makefile''' is a special file containing '''shell '''commands.
| |
− |
| |
− | Press '''Enter.'''
| |
− |
| |
− | Create a '''Makefile '''where you have saved the '''simple_module dot c .'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Copy paste the makefile content
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Type the '''code '''as shown.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''obj-m'''
| |
− |
| |
− | Highlight '''simple_module.o'''
| |
− |
| |
− | Highlight '''all:'''
| |
− |
| |
− | Highlight '''clean:'''
| |
− |
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| The''' obj hyphen m''' variable tells the '''kernel Makefile '''that this '''module '''needs to be compiled.
| |
− |
| |
− | When you run the '''make''' '''all '''command''',''' then commands under '''all''' section will be executed.
| |
− |
| |
− | If you run the '''make clean '''command, it executes commands under the '''clean '''section.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''-C'''
| |
− |
| |
− | Highlight '''uname -r'''
| |
− | Highlight''' (PWD)'''
| |
− | Highlight '''M=$(PWD)'''
| |
− | Highlight''' modules'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Using '''hyphen C''', we are running '''make command '''inside the''' kernel source directory.'''
| |
− |
| |
− | As you know,''' uname hyphen r''' finds the current '''Linux kernel''' version of a '''system.'''
| |
− |
| |
− | '''PWD '''means the '''present working directory.'''
| |
− |
| |
− | This option tells the '''kernel''' '''makefile''' that the '''source''' '''code '''for the driver is in '''PWD'''.
| |
− |
| |
− | The word '''modules''' tell the '''kernel makefile''' to build the '''modules'''.
| |
− |
| |
− | But it will not build the complete''' kernel source code.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''clean'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| The word '''clean''' tells the '''kernel makefile '''to clean the generated '''object files.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Press >> '''Ctrl + S'''
| |
− |
| |
− | Click on the '''close'''.
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Save the '''file''' and close the '''editor'''.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Open the '''terminal'''
| |
− |
| |
− | Type >> '''make all'''
| |
− |
| |
− | Press '''Enter'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Switch back to the '''terminal'''.
| |
− |
| |
− | Let us compile the '''program''',
| |
− |
| |
− | Type '''make space all.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Type''' ls '''and''' '''Press''' Enter'''
| |
− |
| |
− | Highlight''' ls '''
| |
− |
| |
− | Show the output
| |
− |
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Now type''' ls.'''
| |
− |
| |
− | This command lists the '''files''' in the current '''directory.'''
| |
− |
| |
− | Here, you can see the number of '''files '''created in this '''directory'''.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight''' module.symvers'''
| |
− |
| |
− | Highlight''' mod.c '''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| The''' module.symvers''' file contains a list of all '''symbols'''
| |
− |
| |
− | These '''symbols '''are exported from the '''kernel '''build.
| |
− |
| |
− | The '''mod.c file '''contains information about the '''module'''.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight''' mod.o'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| '''Mod.o''' file is '''object file''' resulting from the compilation of the '''mod.c file.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight''' .o'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| This''' object file''' is created from the compilation of the '''module source file'''.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight''' modules.order'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| It will list out the order in which the compilation and creation of the '''.ko file '''takes place.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight''' .ko'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| This is the final '''kernel module''' binary that is loaded into the '''kernel.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Type >> '''clear'''
| |
− |
| |
− | Press''' Enter'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Clear the screen
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Type '''>> modinfo simple_module.ko'''
| |
− |
| |
− | Press '''Enter'''
| |
− |
| |
− | Highlight '''modinfo'''
| |
| | | |
| Highlight '''filename''' | | Highlight '''filename''' |
| | | |
− | Highlight '''description'''
| |
− |
| |
− | Highlight '''author, license'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Let us see the details of a '''module'''.
| |
− |
| |
− | Type''' modinfo space simple_module dot ko.'''
| |
− |
| |
− | This command displays information about the '''Linux kernel module. '''
| |
− |
| |
− | The '''filename''' field contains an absolute path to the .'''ko kernel''' '''object file.'''
| |
− |
| |
− | This field displays a short description of a '''module.'''
| |
− |
| |
− | Author field shows author and '''license '''field shows the '''license''' of the '''module.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''srcversion'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| The '''srcversion field '''contains the source code version used to compile a''' module'''.
| |
− |
| |
− | It is calculated automatically at build time.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''depends'''
| |
− |
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| The''' depends''' field contains all '''modules''' on which this '''module '''depends.
| |
− |
| |
− | It is shown empty here as this '''module '''does not depend on any other '''module.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''retpoline'''
| |
− |
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| '''Retpoline''' field indicates that the '''module '''is compiled with '''retpoline '''method.
| |
− |
| |
− | '''Modules''' must also be compiled with a '''retpoline-aware compiler.'''
| |
− |
| |
− | Otherwise the '''kernel '''can be vulnerable.
| |
− |
| |
− | '''Retpoline technique''' protects the kernel from the hacker’s attacks.
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''name'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| The '''name''' field contains the '''name '''of a''' module.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''vermagic'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| This field shows the''' version magic''' number of a '''module.'''
| |
− |
| |
− | This '''version magic''' number is used while loading the '''kernel module.'''
| |
− |
| |
− | The '''version magic''' number of '''module''' and current '''kernel version '''should be the same.
| |
− |
| |
− | Otherwise it will fail to load in the current '''kernel. '''
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Type >> '''make clean'''
| |
− |
| |
− | Press''' Enter'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Type '''make space clean.'''
| |
− |
| |
− | This '''command''' removes all '''object files''' that are created after the '''compilation.'''
| |
− |
| |
− | '''Type ls.'''
| |
− |
| |
− | Now there are no '''object files.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"|
| |
− | | style="border:0.5pt solid #000000;padding:0.176cm;"| With this, we come to the end of this tutorial. Let us summarize.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Slide 6:
| |
− |
| |
− | Summary:
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| In this tutorial, we learned how to
| |
− |
| |
− | * Implement a '''simple loadable module.'''
| |
− | * Create a '''Makefile'''.
| |
− | * Compile a '''module.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Slide 7:
| |
− |
| |
− | Assignment:
| |
− |
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| As an assignment,
| |
− |
| |
− | # Open the''' simple_module.c file'''
| |
− | # Change the module description''' '''using''' MODULE_DESCRIPTION macro '''
| |
− | # Run the '''modinfo''' command
| |
− | # See the output of description.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Slide 8:
| |
− |
| |
− | About Spoken Tutorial Project:
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| The video at the following link summarizes the Spoken Tutorial project.
| |
− |
| |
− | Please download and watch it.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Slide 9:
| |
− |
| |
− | Spoken Tutorial workshops:
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| The''' Spoken Tutorial Project''' Team:
| |
− |
| |
− | * conducts workshops and
| |
− | * gives certificates.
| |
− |
| |
− | For more details, please write to us.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Slide 10:
| |
− |
| |
− | Forum questions:
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| * Do you have questions in THIS Spoken Tutorial?
| |
− | * Please visit this site
| |
− | * Choose the minute and second where you have the question.
| |
− | * Explain your question briefly.
| |
− | * The Spoken Tutorial project will ensure an answer
| |
− | * You will have to register to ask questions
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Slide 11:
| |
− |
| |
− | Forum for specific questions:
| |
− |
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| * The Spoken Tutorial forum is for specific questions on this tutorial.
| |
− | * Please do not post unrelated and general questions on them.
| |
− | * This will help reduce the clutter.
| |
− | * With less clutter, we can use these discussions as instructional material.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Slide 12:
| |
− |
| |
− | Forum for specific questions:
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| * Do you have general or technical questions on Embedded Linux Device Driver?
| |
− | * Please visit the FOSSEE forum and post your question.
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Slide 13:
| |
− |
| |
− | Acknowledgment:
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Spoken Tutorial project is funded by MHRD, Government of India.
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Slide 14:
| |
− |
| |
− | Thank you slide:
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| This tutorial has been contributed by FOSSEE and Spoken Tutorial Project, IIT Bombay.
| |
− |
| |
− | This is Mayuri Panchakshari signing off.
| |
− |
| |
− | Thanks for watching. .
| |
− |
| |
− | I have given the description as “'''First Linux kernel module”.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''static int init_func(void)'''
| |
− |
| |
− | Highlight '''return(0)'''
| |
− |
| |
− | Highlight '''module_init()'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| The '''init function '''will execute when a '''module '''is loaded into the '''kernel'''.
| |
− |
| |
− | So it is called as an '''initialization''' '''function''' of a '''module'''.
| |
− |
| |
− | If this '''function''' fails then the '''error '''will be '''returned'''.
| |
− |
| |
− | It will '''return''' 0 to the '''kernel''' on successful '''execution'''.
| |
− |
| |
− | '''Initialization function''' prints a simple message and '''return''' zero.
| |
− |
| |
− | The '''module init''' '''macro''' is used to register the '''init function.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight''' void exit_func (void)'''
| |
− |
| |
− | Highlight '''module_exit()'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| The''' exit function '''will execute when the '''module '''is unloaded from the '''kernel'''.
| |
− |
| |
− | It is mostly used to cleanup the '''resources''' acquired by the '''module'''
| |
− |
| |
− | The''' exit function''' will undo whatever the''' init function '''did.
| |
− |
| |
− | The '''module underscore exit macro is''' used to register the '''exit function. '''
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''printk'''
| |
− |
| |
− | Highlight '''KERN_INFO'''
| |
− |
| |
− | Press >> '''Ctrl + S'''
| |
− |
| |
− | Press '''Enter'''
| |
− |
| |
− | Click on '''close''' option.
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| In '''kernel '''programming, '''printk '''is used as a printing''' function.'''
| |
− |
| |
− | It is similar to '''printf function '''in '''C '''programming.
| |
− |
| |
− | Using this '''function,''' string data will get loaded in the '''kernel''' log '''buffer'''.
| |
− |
| |
− | '''printk''' has an optional prefix string, '''Loglevel '''such as''' KERN_INFO.'''
| |
− |
| |
− | '''Loglevel '''specifies the type of message being sent to the kernel message log.
| |
− |
| |
− | '''KERN underscore INFO''' is used for informational messages.
| |
− |
| |
− | For more log levels, go through the '''Additional reading material''' link of this tutorial.
| |
− |
| |
− | Press '''Ctrl + S''' keys to save the '''program '''.
| |
− |
| |
− | Close the '''file.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Switch back to the '''terminal'''.
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Switch back to the '''terminal'''.
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"|
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Let us compile the code.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Type >> '''gedit Makefile'''
| |
− |
| |
− | Highlight the makefile
| |
− |
| |
− | Press '''Enter'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| For that, we have to create a''' Makefile.'''
| |
− |
| |
− | Type '''gedit Makefile '''
| |
− |
| |
− | The '''Makefile''' is a special file containing '''shell '''commands.
| |
− |
| |
− | Press '''Enter.'''
| |
− |
| |
− | Create a '''Makefile '''where you have saved the '''simple_module dot c .'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Copy paste the makefile content
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Type the '''code '''as shown.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''obj-m'''
| |
− |
| |
− | Highlight '''simple_module.o'''
| |
− |
| |
− | Highlight '''all:'''
| |
− |
| |
− | Highlight '''clean:'''
| |
− |
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| The''' obj hyphen m''' variable tells the '''kernel Makefile '''that this '''module '''needs to be compiled.
| |
− |
| |
− | When you run the '''make''' '''all '''command''',''' then commands under '''all''' section will be executed.
| |
− |
| |
− | If you run the '''make clean '''command, it executes commands under the '''clean '''section.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''-C'''
| |
− |
| |
− | Highlight '''uname -r'''
| |
− | Highlight''' (PWD)'''
| |
− | Highlight '''M=$(PWD)'''
| |
− | Highlight''' modules'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Using '''hyphen C''', we are running '''make command '''inside the''' kernel source directory.'''
| |
− |
| |
− | As you know,''' uname hyphen r''' finds the current '''Linux kernel''' version of a '''system.'''
| |
− |
| |
− | '''PWD '''means the '''present working directory.'''
| |
− |
| |
− | This option tells the '''kernel''' '''makefile''' that the '''source''' '''code '''for the driver is in '''PWD'''.
| |
− |
| |
− | The word '''modules''' tell the '''kernel makefile''' to build the '''modules'''.
| |
− |
| |
− | But it will not build the complete''' kernel source code.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''clean'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| The word '''clean''' tells the '''kernel makefile '''to clean the generated '''object files.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Press >> '''Ctrl + S'''
| |
− |
| |
− | Click on the '''close'''.
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Save the '''file''' and close the '''editor'''.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Open the '''terminal'''
| |
− |
| |
− | Type >> '''make all'''
| |
− |
| |
− | Press '''Enter'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Switch back to the '''terminal'''.
| |
− |
| |
− | Let us compile the '''program''',
| |
− |
| |
− | Type '''make space all.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Type''' ls '''and''' '''Press''' Enter'''
| |
− |
| |
− | Highlight''' ls '''
| |
− |
| |
− | Show the output
| |
− |
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Now type''' ls.'''
| |
− |
| |
− | This command lists the '''files''' in the current '''directory.'''
| |
− |
| |
− | Here, you can see the number of '''files '''created in this '''directory'''.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight''' module.symvers'''
| |
− |
| |
− | Highlight''' mod.c '''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| The''' module.symvers''' file contains a list of all '''symbols'''
| |
− |
| |
− | These '''symbols '''are exported from the '''kernel '''build.
| |
− |
| |
− | The '''mod.c file '''contains information about the '''module'''.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight''' mod.o'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| '''Mod.o''' file is '''object file''' resulting from the compilation of the '''mod.c file.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight''' .o'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| This''' object file''' is created from the compilation of the '''module source file'''.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight''' modules.order'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| It will list out the order in which the compilation and creation of the '''.ko file '''takes place.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight''' .ko'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| This is the final '''kernel module''' binary that is loaded into the '''kernel.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Type >> '''clear'''
| |
− |
| |
− | Press''' Enter'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Clear the screen
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Type '''>> modinfo simple_module.ko'''
| |
− |
| |
− | Press '''Enter'''
| |
− |
| |
− | Highlight '''modinfo'''
| |
− |
| |
− | Highlight '''filename'''
| |
| | | |
| Highlight '''description''' | | Highlight '''description''' |
| | | |
− | Highlight '''author, license'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Let us see the details of a '''module'''.
| |
− |
| |
− | Type''' modinfo space simple_module dot ko.'''
| |
− |
| |
− | This command displays information about the '''Linux kernel module. '''
| |
− |
| |
− | The '''filename''' field contains an absolute path to the .'''ko kernel''' '''object file.'''
| |
− |
| |
− | This field displays a short description of a '''module.'''
| |
− |
| |
− | Author field shows author and '''license '''field shows the '''license''' of the '''module.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''srcversion'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| The '''srcversion field '''contains the source code version used to compile a''' module'''.
| |
− |
| |
− | It is calculated automatically at build time.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''depends'''
| |
− |
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| The''' depends''' field contains all '''modules''' on which this '''module '''depends.
| |
− |
| |
− | It is shown empty here as this '''module '''does not depend on any other '''module.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''retpoline'''
| |
− |
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| '''Retpoline''' field indicates that the '''module '''is compiled with '''retpoline '''method.
| |
− |
| |
− | '''Modules''' must also be compiled with a '''retpoline-aware compiler.'''
| |
− |
| |
− | Otherwise the '''kernel '''can be vulnerable.
| |
− |
| |
− | '''Retpoline technique''' protects the kernel from the hacker’s attacks.
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''name'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| The '''name''' field contains the '''name '''of a''' module.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''vermagic'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| This field shows the''' version magic''' number of a '''module.'''
| |
− |
| |
− | This '''version magic''' number is used while loading the '''kernel module.'''
| |
− |
| |
− | The '''version magic''' number of '''module''' and current '''kernel version '''should be the same.
| |
− |
| |
− | Otherwise it will fail to load in the current '''kernel. '''
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Type >> '''make clean'''
| |
− |
| |
− | Press''' Enter'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Type '''make space clean.'''
| |
− |
| |
− | This '''command''' removes all '''object files''' that are created after the '''compilation.'''
| |
− |
| |
− | '''Type ls.'''
| |
− |
| |
− | Now there are no '''object files.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"|
| |
− | | style="border:0.5pt solid #000000;padding:0.176cm;"| With this, we come to the end of this tutorial. Let us summarize.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Slide 6:
| |
− |
| |
− | Summary:
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| In this tutorial, we learned how to
| |
− |
| |
− | * Implement a '''simple loadable module.'''
| |
− | * Create a '''Makefile'''.
| |
− | * Compile a '''module.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Slide 7:
| |
− |
| |
− | Assignment:
| |
− |
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| As an assignment,
| |
− |
| |
− | # Open the''' simple_module.c file'''
| |
− | # Change the module description''' '''using''' MODULE_DESCRIPTION macro '''
| |
− | # Run the '''modinfo''' command
| |
− | # See the output of description.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Slide 8:
| |
− |
| |
− | About Spoken Tutorial Project:
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| The video at the following link summarizes the Spoken Tutorial project.
| |
− |
| |
− | Please download and watch it.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Slide 9:
| |
− |
| |
− | Spoken Tutorial workshops:
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| The''' Spoken Tutorial Project''' Team:
| |
− |
| |
− | * conducts workshops and
| |
− | * gives certificates.
| |
− |
| |
− | For more details, please write to us.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Slide 10:
| |
− |
| |
− | Forum questions:
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| * Do you have questions in THIS Spoken Tutorial?
| |
− | * Please visit this site
| |
− | * Choose the minute and second where you have the question.
| |
− | * Explain your question briefly.
| |
− | * The Spoken Tutorial project will ensure an answer
| |
− | * You will have to register to ask questions
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Slide 11:
| |
− |
| |
− | Forum for specific questions:
| |
− |
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| * The Spoken Tutorial forum is for specific questions on this tutorial.
| |
− | * Please do not post unrelated and general questions on them.
| |
− | * This will help reduce the clutter.
| |
− | * With less clutter, we can use these discussions as instructional material.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Slide 12:
| |
− |
| |
− | Forum for specific questions:
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| * Do you have general or technical questions on Embedded Linux Device Driver?
| |
− | * Please visit the FOSSEE forum and post your question.
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Slide 13:
| |
− |
| |
− | Acknowledgment:
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Spoken Tutorial project is funded by MHRD, Government of India.
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Slide 14:
| |
− |
| |
− | Thank you slide:
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| This tutorial has been contributed by FOSSEE and Spoken Tutorial Project, IIT Bombay.
| |
− |
| |
− | This is Mayuri Panchakshari signing off.
| |
− |
| |
− | Thanks for watching.
| |
− |
| |
− | |}
| |
− |
| |
− |
| |
− | |}
| |
− |
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''static int init_func(void)'''
| |
− |
| |
− | Highlight '''return(0)'''
| |
− |
| |
− | Highlight '''module_init()'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| The '''init function '''will execute when a '''module '''is loaded into the '''kernel'''.
| |
− |
| |
− | So it is called as an '''initialization''' '''function''' of a '''module'''.
| |
− |
| |
− | If this '''function''' fails then the '''error '''will be returned. .
| |
− |
| |
− | I have given the description as “'''First Linux kernel module”.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''static int init_func(void)'''
| |
− |
| |
− | Highlight '''return(0)'''
| |
− |
| |
− | Highlight '''module_init()'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| The '''init function '''will execute when a '''module '''is loaded into the '''kernel'''.
| |
− |
| |
− | So it is called as an '''initialization''' '''function''' of a '''module'''.
| |
− |
| |
− | If this '''function''' fails then the '''error '''will be '''returned'''.
| |
− |
| |
− | It will '''return''' 0 to the '''kernel''' on successful '''execution'''.
| |
− |
| |
− | Initialization function prints a simple message and return zero.
| |
− |
| |
− | The '''module init''' '''macro''' is used to register the '''init function.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight''' void exit_func (void)'''
| |
− |
| |
− | Highlight '''module_exit()'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| The''' exit function '''will execute when the '''module '''is unloaded from the '''kernel'''.
| |
− |
| |
− | It is mostly used to cleanup the '''resources''' acquired by the '''module'''
| |
− |
| |
− | The''' exit function''' will undo whatever the''' init function '''did.
| |
− |
| |
− | The '''module underscore exit macro is''' used to register the '''exit function. '''
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''printk'''
| |
− |
| |
− | Highlight '''KERN_INFO'''
| |
− |
| |
− | Press >> '''Ctrl + S'''
| |
− |
| |
− | Press '''Enter'''
| |
− |
| |
− | Click on '''close''' option.
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| In '''kernel '''programming, '''printk '''is used as a printing''' function.'''
| |
− |
| |
− | It is similar to '''printf function '''in '''C '''programming.
| |
− |
| |
− | Using this '''function,''' string data will get loaded in the '''kernel''' log '''buffer'''.
| |
− |
| |
− | '''printk''' has an optional prefix string, '''Loglevel '''such as''' KERN_INFO.'''
| |
− |
| |
− | '''Loglevel '''specifies the type of message being sent to the kernel message log.
| |
− |
| |
− | '''KERN underscore INFO''' is used for informational messages.
| |
− |
| |
− | For more log levels, go through the '''Additional reading material''' link of this tutorial.
| |
− |
| |
− | Press '''Ctrl + S''' keys to save the '''program '''.
| |
− |
| |
− | Close the '''file.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Switch back to the '''terminal'''.
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Switch back to the '''terminal'''.
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"|
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Let us compile the code.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Type >> '''gedit Makefile'''
| |
− |
| |
− | Highlight the makefile
| |
− |
| |
− | Press '''Enter'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| For that, we have to create a''' Makefile.'''
| |
− |
| |
− | Type '''gedit Makefile '''
| |
− |
| |
− | The '''Makefile''' is a special file containing '''shell '''commands.
| |
− |
| |
− | Press '''Enter.'''
| |
− |
| |
− | Create a '''Makefile '''where you have saved the '''simple_module dot c .'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Copy paste the makefile content
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Type the '''code '''as shown.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''obj-m'''
| |
− |
| |
− | Highlight '''simple_module.o'''
| |
− |
| |
− | Highlight '''all:'''
| |
− |
| |
− | Highlight '''clean:'''
| |
− |
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| The''' obj hyphen m''' variable tells the '''kernel Makefile '''that this '''module '''needs to be compiled.
| |
− |
| |
− | When you run the '''make''' '''all '''command''',''' then commands under '''all''' section will be executed.
| |
− |
| |
− | If you run the '''make clean '''command, it executes commands under the '''clean '''section.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''-C'''
| |
− |
| |
− | Highlight '''uname -r'''
| |
− | Highlight''' (PWD)'''
| |
− | Highlight '''M=$(PWD)'''
| |
− | Highlight''' modules'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Using '''hyphen C''', we are running '''make command '''inside the''' kernel source directory.'''
| |
− |
| |
− | As you know,''' uname hyphen r''' finds the current '''Linux kernel''' version of a '''system.'''
| |
− |
| |
− | '''PWD '''means the '''present working directory.'''
| |
− |
| |
− | This option tells the '''kernel''' '''makefile''' that the '''source''' '''code '''for the driver is in '''PWD'''.
| |
− |
| |
− | The word '''modules''' tell the '''kernel makefile''' to build the '''modules'''.
| |
− |
| |
− | But it will not build the complete''' kernel source code.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''clean'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| The word '''clean''' tells the '''kernel makefile '''to clean the generated '''object files.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Press >> '''Ctrl + S'''
| |
− |
| |
− | Click on the '''close'''.
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Save the '''file''' and close the '''editor'''.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Open the '''terminal'''
| |
− |
| |
− | Type >> '''make all'''
| |
− |
| |
− | Press '''Enter'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Switch back to the '''terminal'''.
| |
− |
| |
− | Let us compile the '''program''',
| |
− |
| |
− | Type '''make space all.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Type''' ls '''and''' '''Press''' Enter'''
| |
− |
| |
− | Highlight''' ls '''
| |
− |
| |
− | Show the output
| |
− |
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Now type''' ls.'''
| |
− |
| |
− | This command lists the '''files''' in the current '''directory.'''
| |
− |
| |
− | Here, you can see the number of '''files '''created in this '''directory'''.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight''' module.symvers'''
| |
− |
| |
− | Highlight''' mod.c '''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| The''' module.symvers''' file contains a list of all '''symbols'''
| |
− |
| |
− | These '''symbols '''are exported from the '''kernel '''build.
| |
− |
| |
− | The '''mod.c file '''contains information about the '''module'''.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight''' mod.o'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| '''Mod.o''' file is '''object file''' resulting from the compilation of the '''mod.c file.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight''' .o'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| This''' object file''' is created from the compilation of the '''module source file'''.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight''' modules.order'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| It will list out the order in which the compilation and creation of the '''.ko file '''takes place.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight''' .ko'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| This is the final '''kernel module''' binary that is loaded into the '''kernel.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Type >> '''clear'''
| |
− |
| |
− | Press''' Enter'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Clear the screen
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Type '''>> modinfo simple_module.ko'''
| |
− |
| |
− | Press '''Enter'''
| |
− |
| |
− | Highlight '''modinfo'''
| |
− |
| |
− | Highlight '''filename'''
| |
− |
| |
− | Highlight '''description'''
| |
| | | |
| Highlight '''author, license''' | | Highlight '''author, license''' |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Let us see the details of a '''module'''.
| |
| | | |
− | Type''' modinfo space simple_module dot ko.'''
| |
− |
| |
− | This command displays information about the '''Linux kernel module. '''
| |
− |
| |
− | The '''filename''' field contains an absolute path to the .'''ko kernel''' '''object file.'''
| |
− |
| |
− | This field displays a short description of a '''module.'''
| |
− |
| |
− | Author field shows author and '''license '''field shows the '''license''' of the '''module.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''srcversion'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| The '''srcversion field '''contains the source code version used to compile a''' module'''.
| |
− |
| |
− | It is calculated automatically at build time.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''depends'''
| |
− |
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| The''' depends''' field contains all '''modules''' on which this '''module '''depends.
| |
− |
| |
− | It is shown empty here as this '''module '''does not depend on any other '''module.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''retpoline'''
| |
− |
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| '''Retpoline''' field indicates that the '''module '''is compiled with '''retpoline '''method.
| |
− |
| |
− | '''Modules''' must also be compiled with a '''retpoline-aware compiler.'''
| |
− |
| |
− | Otherwise the '''kernel '''can be vulnerable.
| |
− |
| |
− | '''Retpoline technique''' protects the kernel from the hacker’s attacks.
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''name'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| The '''name''' field contains the '''name '''of a''' module.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''vermagic'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| This field shows the''' version magic''' number of a '''module.'''
| |
− |
| |
− | This '''version magic''' number is used while loading the '''kernel module.'''
| |
− |
| |
− | The '''version magic''' number of '''module''' and current '''kernel version '''should be the same.
| |
− |
| |
− | Otherwise it will fail to load in the current '''kernel. '''
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Type >> '''make clean'''
| |
− |
| |
− | Press''' Enter'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Type '''make space clean.'''
| |
− |
| |
− | This '''command''' removes all '''object files''' that are created after the '''compilation.'''
| |
− |
| |
− | '''Type ls.'''
| |
− |
| |
− | Now there are no '''object files.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"|
| |
− | | style="border:0.5pt solid #000000;padding:0.176cm;"| With this, we come to the end of this tutorial. Let us summarize.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Slide 6:
| |
− |
| |
− | Summary:
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| In this tutorial, we learned how to
| |
− |
| |
− | * Implement a '''simple loadable module.'''
| |
− | * Create a '''Makefile'''.
| |
− | * Compile a '''module.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Slide 7:
| |
− |
| |
− | Assignment:
| |
− |
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| As an assignment,
| |
− |
| |
− | # Open the''' simple_module.c file'''
| |
− | # Change the module description''' '''using''' MODULE_DESCRIPTION macro '''
| |
− | # Run the '''modinfo''' command
| |
− | # See the output of description.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Slide 8:
| |
− |
| |
− | About Spoken Tutorial Project:
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| The video at the following link summarizes the Spoken Tutorial project.
| |
− |
| |
− | Please download and watch it.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Slide 9:
| |
− |
| |
− | Spoken Tutorial workshops:
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| The''' Spoken Tutorial Project''' Team:
| |
− |
| |
− | * conducts workshops and
| |
− | * gives certificates.
| |
− |
| |
− | For more details, please write to us.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Slide 10:
| |
− |
| |
− | Forum questions:
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| * Do you have questions in THIS Spoken Tutorial?
| |
− | * Please visit this site
| |
− | * Choose the minute and second where you have the question.
| |
− | * Explain your question briefly.
| |
− | * The Spoken Tutorial project will ensure an answer
| |
− | * You will have to register to ask questions
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Slide 11:
| |
− |
| |
− | Forum for specific questions:
| |
− |
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| * The Spoken Tutorial forum is for specific questions on this tutorial.
| |
− | * Please do not post unrelated and general questions on them.
| |
− | * This will help reduce the clutter.
| |
− | * With less clutter, we can use these discussions as instructional material.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Slide 12:
| |
− |
| |
− | Forum for specific questions:
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| * Do you have general or technical questions on Embedded Linux Device Driver?
| |
− | * Please visit the FOSSEE forum and post your question.
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Slide 13:
| |
− |
| |
− | Acknowledgment:
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Spoken Tutorial project is funded by MHRD, Government of India.
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Slide 14:
| |
− |
| |
− | Thank you slide:
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| This tutorial has been contributed by FOSSEE and Spoken Tutorial Project, IIT Bombay.
| |
− |
| |
− | This is Mayuri Panchakshari signing off.
| |
− |
| |
− | Thanks for watching.
| |
− |
| |
− | |}
| |
− |
| |
− |
| |
− | It will '''return''' 0 to the '''kernel''' on successful execution.
| |
− |
| |
− | Initialization function prints a simple message and return zero.
| |
− |
| |
− | The '''module init''' '''macro''' is used to register the '''init function.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight''' void exit_func (void)'''
| |
− |
| |
− | Highlight '''module_exit()'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| The''' exit function '''will execute when the '''module '''is unloaded from the '''kernel'''.
| |
− |
| |
− | It is mostly used to cleanup the '''resources''' acquired by the '''module'''
| |
− |
| |
− | The''' exit function''' will undo whatever the''' init function '''did.
| |
− |
| |
− | The '''module underscore exit macro is''' used to register the '''exit function. '''
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''printk'''
| |
− |
| |
− | Highlight '''KERN_INFO'''
| |
− |
| |
− | Press >> '''Ctrl + S'''
| |
− |
| |
− | Press '''Enter'''
| |
− |
| |
− | Click on '''close''' option.
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| In '''kernel '''programming, '''printk '''is used as a printing''' function.'''
| |
− |
| |
− | It is similar to '''printf function '''in '''C '''programming.
| |
− |
| |
− | Using this '''function,''' string data will get loaded in the '''kernel''' log '''buffer'''.
| |
− |
| |
− | '''printk''' has an optional prefix string, '''Loglevel '''such as''' KERN_INFO.'''
| |
− |
| |
− | '''Loglevel '''specifies the type of message being sent to the kernel message log.
| |
− |
| |
− | '''KERN underscore INFO''' is used for informational messages.
| |
− |
| |
− | For more log levels, go through the '''Additional reading material''' link of this tutorial.
| |
− |
| |
− | Press '''Ctrl + S''' keys to save the '''program '''.
| |
− |
| |
− | Close the '''file.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Switch back to the '''terminal'''.
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Switch back to the '''terminal'''.
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"|
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Let us compile the code.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Type >> '''gedit Makefile'''
| |
− |
| |
− | Highlight the makefile
| |
− |
| |
− | Press '''Enter'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| For that, we have to create a''' Makefile.'''
| |
− |
| |
− | Type '''gedit Makefile '''
| |
− |
| |
− | The '''Makefile''' is a special file containing '''shell '''commands.
| |
− |
| |
− | Press '''Enter.'''
| |
− |
| |
− | Create a '''Makefile '''where you have saved the '''simple_module dot c .'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Copy paste the makefile content
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Type the '''code '''as shown.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''obj-m'''
| |
− |
| |
− | Highlight '''simple_module.o'''
| |
− |
| |
− | Highlight '''all:'''
| |
− |
| |
− | Highlight '''clean:'''
| |
− |
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| The''' obj hyphen m''' variable tells the '''kernel Makefile '''that this '''module '''needs to be compiled.
| |
− |
| |
− | When you run the '''make''' '''all '''command''',''' then commands under '''all''' section will be executed.
| |
− |
| |
− | If you run the '''make clean '''command, it executes commands under the '''clean '''section.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''-C'''
| |
− |
| |
− | Highlight '''uname -r'''
| |
− | Highlight''' (PWD)'''
| |
− | Highlight '''M=$(PWD)'''
| |
− | Highlight''' modules'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Using '''hyphen C''', we are running '''make command '''inside the''' kernel source directory.'''
| |
− |
| |
− | As you know,''' uname hyphen r''' finds the current '''Linux kernel''' version of a '''system.'''
| |
− |
| |
− | '''PWD '''means the '''present working directory.'''
| |
− |
| |
− | This option tells the '''kernel''' '''makefile''' that the '''source''' '''code '''for the driver is in '''PWD'''.
| |
− |
| |
− | The word '''modules''' tell the '''kernel makefile''' to build the '''modules'''.
| |
− |
| |
− | But it will not build the complete''' kernel source code.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''clean'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| The word '''clean''' tells the '''kernel makefile '''to clean the generated '''object files.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Press >> '''Ctrl + S'''
| |
− |
| |
− | Click on the '''close'''.
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Save the '''file''' and close the '''editor'''.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Open the '''terminal'''
| |
− |
| |
− | Type >> '''make all'''
| |
− |
| |
− | Press '''Enter'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Switch back to the '''terminal'''.
| |
− |
| |
− | Let us compile the '''program''',
| |
− |
| |
− | Type '''make space all.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Type''' ls '''and''' '''Press''' Enter'''
| |
− |
| |
− | Highlight''' ls '''
| |
− |
| |
− | Show the output
| |
− |
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Now type''' ls.'''
| |
− |
| |
− | This command lists the '''files''' in the current '''directory.'''
| |
− |
| |
− | Here, you can see the number of '''files '''created in this '''directory'''.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight''' module.symvers'''
| |
− |
| |
− | Highlight''' mod.c '''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| The''' module.symvers''' file contains a list of all '''symbols'''
| |
− |
| |
− | These '''symbols '''are exported from the '''kernel '''build.
| |
− |
| |
− | The '''mod.c file '''contains information about the '''module'''.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight''' mod.o'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| '''Mod.o''' file is '''object file''' resulting from the compilation of the '''mod.c file.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight''' .o'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| This''' object file''' is created from the compilation of the '''module source file'''.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight''' modules.order'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| It will list out the order in which the compilation and creation of the '''.ko file '''takes place.
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight''' .ko'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| This is the final '''kernel module''' binary that is loaded into the '''kernel.'''
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Type >> '''clear'''
| |
− |
| |
− | Press''' Enter'''
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Clear the screen
| |
− |
| |
− | |-
| |
− | | style="border:1pt solid #000000;padding:0.176cm;"| Type '''>> modinfo simple_module.ko'''
| |
− |
| |
− | Press '''Enter'''
| |
− |
| |
− | Highlight '''modinfo'''
| |
− |
| |
− | Highlight '''filename'''
| |
− |
| |
− | Highlight '''description'''
| |
− |
| |
− | Highlight '''author, license'''
| |
| | style="border:1pt solid #000000;padding:0.176cm;"| Let us see the details of a '''module'''. | | | style="border:1pt solid #000000;padding:0.176cm;"| Let us see the details of a '''module'''. |
| | | |
| Type''' modinfo space simple_module dot ko.''' | | Type''' modinfo space simple_module dot ko.''' |
| | | |
− | This command displays information about the '''Linux kernel module. ''' | + | This '''command''' displays information about the '''Linux kernel module. ''' |
| | | |
− | The '''filename''' field contains an absolute path to the .'''ko kernel''' '''object file.''' | + | The '''filename''' field contains an '''absolute path''' to the '''.ko kernel object file.''' |
| | | |
| This field displays a short description of a '''module.''' | | This field displays a short description of a '''module.''' |
| | | |
− | Author field shows author and '''license '''field shows the '''license''' of the '''module.''' | + | '''Author''' field shows author and '''License''' field shows the '''license''' of the '''module.''' |
| | | |
| |- | | |- |
Line 2,118: |
Line 388: |
| | style="border:1pt solid #000000;padding:0.176cm;"| The''' depends''' field contains all '''modules''' on which this '''module '''depends. | | | style="border:1pt solid #000000;padding:0.176cm;"| The''' depends''' field contains all '''modules''' on which this '''module '''depends. |
| | | |
− | It is shown empty here as this '''module '''does not depend on any other '''module.''' | + | It is shown empty here as this '''module''' does not depend on any other '''module.''' |
| | | |
| |- | | |- |
| | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''retpoline''' | | | style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''retpoline''' |
| | | |
− | | style="border:1pt solid #000000;padding:0.176cm;"| '''Retpoline''' field indicates that the '''module '''is compiled with '''retpoline '''method. | + | | style="border:1pt solid #000000;padding:0.176cm;"| '''Retpoline''' field indicates that the '''module '''is '''compiled''' with '''retpoline '''method. |
| | | |
| '''Modules''' must also be compiled with a '''retpoline-aware compiler.''' | | '''Modules''' must also be compiled with a '''retpoline-aware compiler.''' |
Line 2,151: |
Line 421: |
| This '''command''' removes all '''object files''' that are created after the '''compilation.''' | | This '''command''' removes all '''object files''' that are created after the '''compilation.''' |
| | | |
− | '''Type ls.''' | + | Type '''ls.''' |
| | | |
| Now there are no '''object files.''' | | Now there are no '''object files.''' |
Line 2,167: |
Line 437: |
| * Implement a '''simple loadable module.''' | | * Implement a '''simple loadable module.''' |
| * Create a '''Makefile'''. | | * Create a '''Makefile'''. |
− | * Compile a '''module.''' | + | * '''Compile''' a '''module.''' |
| | | |
| |- | | |- |
Line 2,178: |
Line 448: |
| # Open the''' simple_module.c file''' | | # Open the''' simple_module.c file''' |
| # Change the module description''' '''using''' MODULE_DESCRIPTION macro ''' | | # Change the module description''' '''using''' MODULE_DESCRIPTION macro ''' |
− | # Run the '''modinfo''' command | + | # '''Run''' the '''modinfo command''' |
| # See the output of description. | | # See the output of description. |
| | | |