Difference between revisions of "Embedded-Linux-Device-Driver/C2/Simple-Loadable-Module/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with " {| style="border-spacing:0;" | style="border:1pt solid #000000;padding:0.176cm;"| '''Visual cue''' | style="border:1pt solid #000000;padding:0.176cm;"| '''Narration''' |- |...")
 
Line 73: Line 73:
  
 
What is a''' kernel module'''?
 
What is a''' kernel module'''?
| style="border:1pt solid #000000;padding:0.176cm;"|  
+
| style="border:1pt solid #000000;padding:0.176cm;"| What is a''' kernel module'''?
 
* Code that is added to the''' kernel '''at runtime is called a '''module.'''
 
* Code that is added to the''' kernel '''at runtime is called a '''module.'''
  
* It extends the functionality of the '''kernel '''without the need to reboot the system.
+
* It extends the '''functionality''' of the '''kernel '''without the need to '''reboot''' the '''system'''.
  
* It communicates with the base '''kernel''' to complete their functions.  
+
* It communicates with the base '''kernel''' to complete their '''functions'''.  
  
 
|-
 
|-
Line 87: Line 87:
 
| style="border:1pt solid #000000;padding:0.176cm;"| Point to the '''folder''' and''' file''' in '''desktop'''
 
| style="border:1pt solid #000000;padding:0.176cm;"| Point to the '''folder''' and''' file''' in '''desktop'''
 
| style="border:1pt solid #000000;padding:0.176cm;"| I have saved a file '''simple_module dot c''' in a folder '''DeviceDriver''' in '''Desktop'''.
 
| style="border:1pt solid #000000;padding:0.176cm;"| I have saved a file '''simple_module dot c''' in a folder '''DeviceDriver''' in '''Desktop'''.
Let us open this '''file.'''
 
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.176cm;"|  
+
| style="border:1pt solid #000000;padding:0.176cm;"| Slide
| style="border:1pt solid #000000;padding:0.176cm;"| Open the''' terminal''' by pressing '''Ctrl+Alt+T'''
+
| style="border:1pt solid #000000;padding:0.176cm;"|
 +
Please download the file '''simple_module dot c '''from the '''Code files''' link of this tutorial.
  
keys simultaneously.
+
Make use of this file while practising.  
  
Press the '''Enter''' key after every command.
+
Remember the file path.
 +
 
 +
Let us open this file.
 +
 
 +
|-
 +
| style="border:1pt solid #000000;padding:0.176cm;"|Press '''Alt+Ctrl+T''' keys
 +
| style="border:1pt solid #000000;padding:0.176cm;"| Open the''' terminal''' by pressing '''Alt+Ctrl+T''' keys simultaneously.
 +
 
 +
Press the '''Enter''' key after every '''command'''.
  
 
|-
 
|-
Line 101: Line 109:
  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| Type '''gedit simple_module dot c '''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| Type '''gedit simple_module dot c '''
 
Please download the file '''simple_module dot c '''from the code files link of this tutorial.
 
 
Make use of this '''file''' while practising.
 
 
Remember the''' file '''path.
 
  
 
|-
 
|-
Line 126: Line 128:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| Highlight'''<nowiki> <linux/kernel.h></nowiki>'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| Highlight'''<nowiki> <linux/kernel.h></nowiki>'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| The''' kernel dot h file''' contains functions such as '''printk.'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| The''' kernel dot h file''' contains '''functions''' such as '''printk.'''
  
 
|-
 
|-
Line 136: Line 138:
 
'''GPL''' means '''GNU Public License.'''
 
'''GPL''' means '''GNU Public License.'''
  
It is otherwise called as '''GNU, '''which''' '''is a free and open-source '''license.'''
+
It is otherwise called as '''GNU, '''which is a free and open-source license.
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''MODULE_AUTHOR'''
 
| style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''MODULE_AUTHOR'''
Line 144: Line 146:
 
| style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''MODULE_DESCRIPTION'''
 
| style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''MODULE_DESCRIPTION'''
  
| style="border:1pt solid #000000;padding:0.176cm;"| You can simply describe your '''module '''using this macro .
+
| style="border:1pt solid #000000;padding:0.176cm;"| You can simply describe your '''module '''using this '''macro'''.
  
 
I have given the description as “'''First Linux kernel module”.'''
 
I have given the description as “'''First Linux kernel module”.'''
Line 153: Line 155:
 
| style="border:1pt solid #000000;padding:0.176cm;"| The '''init function '''will execute when a '''module '''is loaded into the '''kernel'''.
 
| 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'''.
+
So it is called as an '''initialization function''' of a '''module'''.
  
 
If this '''function''' fails then the '''error '''will be returned.
 
If this '''function''' fails then the '''error '''will be returned.
Line 161: Line 163:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| It will return 0 to the '''kernel''' on successful execution.
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| It will return 0 to the '''kernel''' on successful execution.
  
Initialization function prints a simple message and return zero.  
+
'''Initialization function''' prints a simple message and return zero.  
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| Highlight '''module_init()'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| Highlight '''module_init()'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| The '''module init''' '''macro''' is used to register the '''init function.'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| The '''module init macro''' is used to register the '''init function.'''
  
 
|-
 
|-
Line 178: Line 180:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| Highlight '''module_exit()'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| Highlight '''module_exit()'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| The '''module underscore exit macro''' used to register the '''exit function. '''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| The '''module underscore exit macro''' is used to register the '''exit function. '''
  
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''printk'''
 
| style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''printk'''
  
| style="border:1pt solid #000000;padding:0.176cm;"| In '''kernel '''programming, '''printk '''is used as a printing''' function.'''
+
| 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.
+
It is similar to '''printf function '''in '''C programming'''.
  
Using this '''function,''' string data will get loaded in the '''kernel''' log '''buffer'''.
+
Using this '''function, string data''' will get loaded in the '''kernel log buffer'''.
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| Highlight '''KERN_INFO'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| Highlight '''KERN_INFO'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| '''printk''' has an optional prefix string, '''Loglevel '''such as''' KERN underscore INFO.'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| '''printk''' has an optional prefix '''string, Loglevel '''such as''' KERN underscore INFO.'''
  
'''Loglevel '''specifies the type of message being sent to the kernel message log.
+
'''Loglevel '''specifies the type of message being sent to the '''kernel message log'''.
  
 
'''KERN underscore INFO''' is used for informational messages.
 
'''KERN underscore INFO''' is used for informational messages.
  
For more log levels, go through the '''Additional reading material''' link of this tutorial.
+
For more '''log''' levels, go through the '''Additional reading material''' link of this tutorial.
  
 
|-
 
|-
Line 212: Line 214:
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.176cm;"|  
 
| 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;"| Let us '''compile''' the code.
  
 
|-
 
|-
Line 240: Line 242:
  
 
Highlight '''simple_module.o'''
 
Highlight '''simple_module.o'''
| style="border:1pt solid #000000;padding:0.176cm;"| The''' obj hyphen m''' variable tells the '''kernel Makefile '''that this '''module '''needs to be compiled.
+
| style="border:1pt solid #000000;padding:0.176cm;"| The''' obj hyphen m variable''' tells the '''kernel Makefile '''that this '''module '''needs to be '''compiled'''.
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| Highlight '''all:'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| Highlight '''all:'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| When you run the '''make''' '''all '''command''',''' then commands under '''all''' section will be executed.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| When you '''run''' the '''make all command''', then '''commands''' under '''all''' section will be executed.
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| Highlight '''clean:'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| Highlight '''clean:'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| If you run the '''make clean '''command, it executes commands under the '''clean '''section.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| If you '''run''' the '''make clean command''', it executes '''commands''' under the '''clean '''section.
  
 
|-
 
|-
Line 266: Line 268:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| '''PWD '''means the '''present working directory.'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| '''PWD '''means the '''present working directory.'''
  
This option tells the '''kernel''' '''makefile''' that the '''source''' '''code '''for the driver is in '''PWD'''.
+
This option tells the '''kernel makefile''' that the '''source code '''for the '''driver''' is in '''PWD'''.
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| Highlight''' modules'''  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| Highlight''' modules'''  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| The word '''modules''' tell the '''kernel makefile''' to build the '''modules'''.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| The word '''modules''' tell the '''kernel makefile''' to '''build''' the '''modules'''.
  
But it will not build the complete''' kernel source code.'''
+
But it will not '''build''' the complete''' kernel source code.'''
  
 
|-
 
|-
Line 290: Line 292:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| Type >> '''make all'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| Type >> '''make all'''
  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| Let us compile the '''program''',
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| Let us '''compile''' the '''program'''.
  
 
Type '''make space all.'''
 
Type '''make space all.'''
Line 303: Line 305:
 
| style="border:1pt solid #000000;padding:0.176cm;"| Now type''' ls.'''
 
| style="border:1pt solid #000000;padding:0.176cm;"| Now type''' ls.'''
  
This command lists the '''files''' in the current '''directory.'''
+
This '''command''' lists the files in the current '''directory.'''
  
Here, you can see the number of '''files '''created in this '''directory'''.
+
Here, you can see the number of files created in this '''directory'''.
  
 
|-
 
|-
Line 312: Line 314:
 
| style="border:1pt solid #000000;padding:0.176cm;"| The''' module.symvers''' file contains a list of all '''symbols'''
 
| 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.
+
These '''symbols '''are exported from the '''kernel build'''.
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| Highlight''' mod.c '''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| Highlight''' mod.c '''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| The '''mod.c file '''contains information about the '''module'''.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| 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;"| 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;"| '''Mod.o''' file is the '''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;"| Highlight''' .o'''
| style="border:1pt solid #000000;padding:0.176cm;"| This''' object file''' created from the compilation of the '''module source file'''.
+
| 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;"| 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;"| 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;"| 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;"| This is the final '''kernel module binary''' that is loaded into the '''kernel.'''
  
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.176cm;"| Type >> '''clear'''
 
| style="border:1pt solid #000000;padding:0.176cm;"| Type >> '''clear'''
 
 
 
| style="border:1pt solid #000000;padding:0.176cm;"| Clear the screen
 
| style="border:1pt solid #000000;padding:0.176cm;"| Clear the screen
  
Line 350: Line 350:
 
Type''' modinfo space simple_module dot ko.'''
 
Type''' modinfo space simple_module dot ko.'''
  
This command displays the information about the '''Linux kernel module. '''
+
This '''command''' displays the information about the '''Linux kernel module. '''
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| Highlight '''filename'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| Highlight '''filename'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| The '''filename''' field contains an absolute path to the .'''ko kernel''' '''object file.'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| The '''filename''' field contains an '''absolute path''' to the .'''ko kernel object''' file.
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| Highlight '''description'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| Highlight '''description'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| This field displays a short description of a '''module.'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| This '''field''' displays a short description of a '''module.'''
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| Highlight '''author, license'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| Highlight '''author, license'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| Author field shows author and '''license '''field shows the '''license''' of the '''module.'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| 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;"| Highlight '''srcversion'''
| style="border:1pt solid #000000;padding:0.176cm;"| The '''srcversion field '''contains the source code version used to compile a''' module'''.
+
| 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.
+
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;"| Highlight '''depends'''
  
| 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.'''
Line 380: Line 380:
 
| 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.'''
  
 
Otherwise the '''kernel '''can be vulnerable.
 
Otherwise the '''kernel '''can be vulnerable.
  
'''Retpoline technique''' protects the kernel from the hacker’s attacks.
+
'''Retpoline technique''' protects the '''kernel''' from hackers' attacks.
  
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''name'''
 
| 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;"| 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;"| Highlight '''vermagic'''
| style="border:1pt solid #000000;padding:0.176cm;"| This field shows the''' version magic''' number of a '''module.'''
+
| 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.'''
 
This '''version magic''' number is used while loading the '''kernel module.'''
Line 408: Line 408:
 
| style="border:1pt solid #000000;padding:0.176cm;"| Type '''make space clean.'''
 
| style="border:1pt solid #000000;padding:0.176cm;"| Type '''make space clean.'''
  
This '''command''' removes all '''object files''' that are created after the '''compilation.'''
+
This '''command''' removes all '''object''' files that are created after the '''compilation.'''
  
 
|-
 
|-
Line 414: Line 414:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| Type''' ls.'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| Type''' ls.'''
  
Now there are no '''object files.'''
+
Now there are no '''object''' files.
  
 
|-
 
|-
Line 445: Line 445:
  
 
# 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.
  
Line 461: Line 461:
  
 
Spoken Tutorial workshops:
 
Spoken Tutorial workshops:
| style="border:1pt solid #000000;padding:0.176cm;"| The''' Spoken Tutorial Project''' Team:
+
| style="border:1pt solid #000000;padding:0.176cm;"| The''' Spoken Tutorial Project''' Team conducts workshops and gives certificates.
 
+
* conducts workshops and
+
* gives certificates.
+
  
 
For more details, please write to us.
 
For more details, please write to us.
Line 472: Line 469:
  
 
Forum questions:
 
Forum questions:
| style="border:1pt solid #000000;padding:0.176cm;"| * Do you have questions in THIS Spoken Tutorial?
+
| style="border:1pt solid #000000;padding:0.176cm;"|  
 +
* Do you have questions in THIS Spoken Tutorial?
 
* Please visit this site
 
* Please visit this site
 
* Choose the minute and second where you have the question.
 
* Choose the minute and second where you have the question.
Line 484: Line 482:
 
Forum for specific questions:
 
Forum for specific questions:
  
| style="border:1pt solid #000000;padding:0.176cm;"| * The Spoken Tutorial forum is for specific questions on this tutorial.
+
| 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.
 
* Please do not post unrelated and general questions on them.
 
* This will help reduce the clutter.
 
* This will help reduce the clutter.
Line 493: Line 492:
  
 
Forum for specific questions:
 
Forum for specific questions:
| style="border:1pt solid #000000;padding:0.176cm;"| * Do you have general or technical questions on Embedded Linux Device Driver?
+
| 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.
 
* Please visit the FOSSEE forum and post your question.
  

Revision as of 19:08, 16 March 2020

Visual cue Narration
Slide 1:

Welcome slide:

Welcome to the spoken tutorial on Simple Loadable Module.
Slide 2:

Learning objectives:

  • Implement a simple loadable module.
  • Create the Makefile.
  • Compile the module.
In this tutorial, we will learn how to,
  • Implement a simple loadable module.
  • Create a Makefile.
  • Compile a module.
Slide 3:

System Requirements:

  • VirtualBox 5.2
  • Ubuntu Linux 18.04 LTS operating system.
  • Linux kernel version 5.0.0-31 generic.
  • gedit Text Editor.
To record this tutorial, I am using,
  • VirtualBox 5.2
  • Ubuntu Linux 18.04 LTS operating system.
  • Linux kernel version 5.0.0-31 generic
  • gedit Text Editor.
Slide 4:

Prerequisites:

To follow this tutorial, you should be familiar with:

  • C programming language and
  • basics of Linux kernel
To follow this tutorial, you should be familiar with:
  • C programming language and
  • basics of Linux kernel
Slide 5:

Gedit Installation:

  • To proceed further, make sure that gedit is installed on your system.
  • If not, to do the installation, follow the instructions in gedit series on this https://spoken-tutorial.org
gedit Installation:
  • To proceed further, make sure that gedit is installed on your system.
  • If not, to do the installation, follow the instructions in gedit series on this website.
Slide 6:

What is a kernel module?

What is a kernel module?
  • Code that is added to the kernel at runtime is called a module.
  • It extends the functionality of the kernel without the need to reboot the system.
  • It communicates with the base kernel to complete their functions.
Narration only I’ll start with a simple example to explain how to create a module.
Point to the folder and file in desktop I have saved a file simple_module dot c in a folder DeviceDriver in Desktop.
Slide

Please download the file simple_module dot c from the Code files link of this tutorial.

Make use of this file while practising.

Remember the file path.

Let us open this file.

Press Alt+Ctrl+T keys Open the terminal by pressing Alt+Ctrl+T keys simultaneously.

Press the Enter key after every command.

Type gedit simple_module dot c and press Enter. Type gedit simple_module dot c
Let us understand the code now.
Show the program

Highlight <linux/init.h>

At the start of the program, there are some header files.

Init dot h file used to start the init process.

Highlight <linux/module.h> The module dot h file contains functions related to the modules.
Highlight <linux/kernel.h> The kernel dot h file contains functions such as printk.
Highlight MODULE_LICENSE(“GPL”)

Highlight GPL

Next line of the code is about GPL.

GPL means GNU Public License.

It is otherwise called as GNU, which is a free and open-source license.

Highlight MODULE_AUTHOR You can give the author name for your module using this macro as shown here.
Highlight MODULE_DESCRIPTION You can simply describe your module using this macro.

I have given the description as “First Linux kernel module”.

Highlight static int init_func(void) 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.

Highlight return(0) It will return 0 to the kernel on successful execution.

Initialization function prints a simple message and return zero.

Highlight module_init() The module init macro is used to register the init function.
Highlight void exit_func (void) 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.

Highlight module_exit() The module underscore exit macro is used to register the exit function.
Highlight printk 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.

Highlight KERN_INFO printk has an optional prefix string, Loglevel such as KERN underscore 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

Click on close option.

Press Ctrl + S keys to save the program .

Close the file.

Switch back to the terminal. Switch back to the terminal.
Let us compile the code.
Type >> gedit Makefile

Highlight the makefile

Press Enter

For that, we have to create a Makefile.

Type gedit Makefile

The Makefile is a special file containing shell commands.

Press Enter.

narration Create a Makefile where you have saved the simple_module dot c.
Copy paste the makefile content Type the code as shown.
Highlight obj-m

Highlight simple_module.o

The obj hyphen m variable tells the kernel Makefile that this module needs to be compiled.
Highlight all: When you run the make all command, then commands under all section will be executed.
Highlight clean: If you run the make clean command, it executes commands under the clean section.
Highlight -C Using hyphen C, we are running make command inside the kernel source directory.
Highlight uname -r As you know, uname hyphen r finds the current Linux kernel version of a system.
Highlight (PWD)

Highlight M=$(PWD)

PWD means the present working directory.

This option tells the kernel makefile that the source code for the driver is in PWD.

Highlight modules The word modules tell the kernel makefile to build the modules.

But it will not build the complete kernel source code.

Highlight clean The word clean tells the kernel makefile to clean the generated object files.
Press >> Ctrl + S

Click on the close.

Save the file and close the editor.
Open the terminal Switch back to the terminal.
Type >> make all Let us compile the program.

Type make space all.

Type ls and Press Enter

Highlight ls

Show the output

Now type ls.

This command lists the files in the current directory.

Here, you can see the number of files created in this directory.

Highlight module.symvers The module.symvers file contains a list of all symbols

These symbols are exported from the kernel build.

Highlight mod.c The mod.c file contains information about the module.
Highlight mod.o Mod.o file is the object file resulting from the compilation of the mod.c file.
Highlight .o This object file is created from the compilation of the module source file.
Highlight modules.order It will list out the order in which the compilation and creation of the .ko file takes place.
Highlight .ko This is the final kernel module binary that is loaded into the kernel.
Type >> clear Clear the screen
Type >> modinfo simple_module.ko

Press Enter

Highlight modinfo

Let us see the details of a module.

Type modinfo space simple_module dot ko.

This command displays the information about the Linux kernel module.

Highlight filename The filename field contains an absolute path to the .ko kernel object file.
Highlight description This field displays a short description of a module.
Highlight author, license Author field shows author and license field shows the license of the module.
Highlight srcversion The srcversion field contains the source code version used to compile a module.

It is calculated automatically at build time.

Highlight depends 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.

Highlight retpoline 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 hackers' attacks.

Highlight name The name field contains the name of a module.
Highlight vermagic 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.

Type >> make clean

Press Enter

Type make space clean.

This command removes all object files that are created after the compilation.

Type >> ls Type ls.

Now there are no object files.

Type >> clear

Press Enter.

Clear the screen.
Type >> exit

Press Enter

Close the terminal.
Slide 6:

Summary:

In this tutorial, we learned how to
  • Implement a simple loadable module.
  • Create a Makefile.
  • Compile a module.
Slide 7:

Assignment:

As an assignment,
  1. Open the simple_module.c file
  2. Change the module description using MODULE_DESCRIPTION macro
  3. Run the modinfo command
  4. See the output of description.
Slide 8:

About Spoken Tutorial Project:

The video at the following link summarizes the Spoken Tutorial project.

Please download and watch it.

Slide 9:

Spoken Tutorial workshops:

The Spoken Tutorial Project Team conducts workshops and gives certificates.

For more details, please write to us.

Slide 10:

Forum questions:

  • 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
Slide 11:

Forum for specific questions:

  • 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.
Slide 12:

Forum for specific questions:

  • Do you have general or technical questions on Embedded Linux Device Driver?
  • Please visit the FOSSEE forum and post your question.
Slide 13:

Acknowledgment:

Spoken Tutorial project is funded by MHRD, Government of India.
Slide 14:

Thank you slide:

This tutorial has been contributed by FOSSEE and Spoken Tutorial Project, IIT Bombay.

This is Mayuri Panchakshari signing off.

Thanks for watching.

Contributors and Content Editors

Nancyvarkey, Nirmala Venkat