SlideShare a Scribd company logo
1 of 17
Introduction to 
Kernel & 
Device Drivers 
Raj Kumar Rampelli
Outline 
 What is kernel ? 
 What is a Device Driver ? 
 Types of Device Drivers 
 What is a Module ? 
 How to work with Module 
 Platform devices and its registration 
 How does probe() gets called ? 
 Role of Vold in volume mount 
 Allocation of memory in Kernel 
 References 
9/14/2014 
RajKumar Rampelli
What is Kernel ? 
 Kernel is a piece of code & core part of operating 
system and whose responsibilities are 
 Process management 
 Memory management 
 Device Input/output control: Device Drivers task 
 File system 
 Everything is a file in the linux including Device 
connected to system 
 Kernel code is written in C and assembly language 
 Kernel code always runs in Kernel Space 
9/14/2014 
RajKumar Rampelli
Kernel Space & User Space 
User space 
• User programs/applications, shell 
• No direct interaction with Hardware 
• Takes Kernel help for interacting with h/w 
Kernel space 
•Direct Interaction with Hardware 
•Device Drivers tasks 
Hardware 
9/14/2014 
RajKumar Rampelli
What is Device Driver ? 
 Piece of code that will be executed when suitable 
device is connected to the system 
 Each device has its own code in the operating system 
 This code is called “Device Driver” 
 Works for a specific h/w device 
 Device Drivers 
 Hides the details of how a device works 
 Bridge between user applications and Hardware 
 Mapping user application calls to specific h/w 
 Support Hot-plug feature 
 Drivers will be loaded when needed & will be unloaded 
when not needed 
 Also called as “Modules” 
9/14/2014 
RajKumar Rampelli
Types of Device Driver 
 Character device driver 
 Byte oriented data transfer 
 Block device driver 
 Block(512Byte) oriented data transfer 
 Network device driver 
 Packet data transfer 
9/14/2014 
RajKumar Rampelli
What is Module ? 
 Loadable Kernel object – contains related 
subroutines, data grouped together 
 Module executables are represented in kernel object 
format (.ko files) 
 It has only one entry function & one exit function 
 Entry function: called when loaded into kernel 
 Initialization function (like main() in C) 
 init_module(void) 
 Exit function: called when removed from kernel 
 cleanup_module(void) 
 Enables hot-plug feature 
 i.e. dynamic (runtime) loading/removal of drivers 
into/from kernel in response to device state 
9/14/2014 
RajKumar Rampelli
Working with Module 
 Insert/load a module into system kernel 
 insmod module_name.ko 
9/14/2014 
 Remove/unload a module from system kernel 
 rmmod module_name.ko 
 Insert a module along with 
dependent/related modules in one go. 
 modprobe module_name.ko 
 Detailed explanation on Module is available 
at 
http://practicepeople.blogspot.in/2013/06/ke 
rnel-programming-1-introduction-to.html 
RajKumar Rampelli
__init* macro() usage 
 Syntax: 
 __init function_name(); 
 __initdata variable_name; 
9/14/2014 
 Compiler stores the above function/variable in “init.text”/”init.data” 
section in vmlinux.ids file 
 Tells the compiler to free the memory (free_initmem()) once the 
task of function/variable associated with init macro is completed. 
 free_initmem(): memory clean up task 
 Memory optimization 
 Check Kernel log for “Freeing unused kernel memory: xxxk freed” 
 Where to use this macro: 
 Module ‘s Initialization function, since it is called/used only once 
when loading the module (or during kernel boot up) 
 More info can be found at 
http://practicepeople.blogspot.in/2013/07/kernel-programming-3- 
init-macros-usage.html 
RajKumar Rampelli
Device registration and 
initialization 
 Platform devices 
 Devices that are integrated into a given chip and 
therefore are always there 
 The platform-specific initialization code statically 
initializes such arrays of platform_devices and then 
registers them using platform_register() 
 Therefore there is no need for sophisticated 
probing. 
What is probing()[Discussed in the next slide] 
 Instead, the string contained in 
platform_device.name is compared 
platform_driver.driver.name and a match is 
assumed if they are equal. 
9/14/2014 
RajKumar Rampelli
How probe() gets called ? 
Probe() is called when device is recognized by the platform 
Driver’s init function gives kernel a list of devices it is able to service, along 
with a pointer to a probe function. Kernel then calls the driver’s probe 
function one for each device. 
 probe function starts the per-device initialization: 
 initializing hardware, 
 allocating resources, and 
 registering the device with the kernel as a block device. 
 Example: Kernel/drivers/mmc/host/sdhic-tegra.c  sdhci_tegra_probe() 
 Host controller initialization at 
kernel/drivers/mmc/host/sdhci.c  sdhci_add_host() 
 Device initialization starts in 
kernel/drivers/mmc/core/core.c mmc_rescan() 
 Starts execution when Host detects the device. 
 mmc_attach_sdio()  sdio.c [core driver] 
 mmc_attach_sd()  sd.c [core driver] 
 mmc_attach_mmc()  mmc.c [core driver] 
9/14/2014 
RajKumar Rampelli
Vold (Volume Manager 
Daemon) 
 Listens to kernel Netlink socket events for volume status change 
 Interacts with MountService (Java layer) 
 Decision maker to mount any media/device 
 Receives volute state change notifications from Vold 
 Uses Unix domain (POSIX Local IPC) socket to communicate with 
Vold 
 Ex: Insert SD card on Android device 
 It stores volume information retrieved from vold.fstab file 
 This file describes what storage devices will be added into the 
system. Its format: 
<mount> <volume label> <mount point> <partition#> <sys fs path to 
the device> 
 Netlink Socket: Pass the information between Kernel and User 
space 
 Unix domain socket declare: $TOP/system/core/rootdir/init.rc 
socket vold stream 0660 root mount 
9/14/2014 
RajKumar Rampelli
9/14/2014 
Allocation of memory in Kernel 
Kmalloc() Kzalloc() Kcalloc() Vmalloc() 
Allocates 
contiguous physical 
blocks of memory in 
byte-sized chunks 
Allocates 
contiguous physical 
blocks of memory 
Allocates memory 
for an array 
same as kmalloc, 
except it allocates 
memory that is only 
virtually contiguous 
Memory is not set to 
zero 
Memory is set to zero Memory is set to zero underling physical 
memory can be 
discontiguous 
void * kmalloc (size_t 
size, int flags) 
void * kzalloc (size_t 
size, int flags) 
void * kcalloc (size_ 
t n, size_t size, 
unsigned int __noca 
st flags); 
void * vmalloc 
(unsigned long size) 
Depends on the flag 
used, it may sleep 
Depends on the flag 
used, it may sleep 
Depends on the flag 
used, it may sleep 
Can sleep, so don’t 
use it in interrupt 
context 
RajKumar Rampelli
9/14/2014 
Allocation of memory in Kernel 
Kmalloc() Kzalloc() Kcalloc() Vmalloc() 
Can allocate upto 
4MBytes 
Same as kmalloc NA to obtain very large 
regions of memory 
Simple and fast Simple, preferable 
and fast 
Simple and fast Slow compare to 
kmalloc 
Vmalloc(): Slower and not advisable if required memory is less. Because, 
1. Makes non-contiguous physical memory to continuous virtual memory 
2. Setting up the page table entries 
3. Pages obtained from vmalloc() must be mapped to their individual pages 
since physical memory is not contiguous 
4. Results in much greater TLB, performance is affected. 
Note: malloc() function also works in same manner. i.e. allocated memory in 
continuous in virtual address space of the processor, but there is no guarantee 
that they are contiguous in the physical address space (physical RAM) 
RajKumar Rampelli
Allocation of memory in Kernel 
 Flag: controls the behavior of memory allocation and divided into 3 groups 
 Action modifiers: How to allocate memory. Ex: can sleep or not 
 Zone modifiers: Where the request should be satisfied. (DMA buffers) 
 Types: types of allocation 
FLAG Type Action Modifier Zone Modifier 
GFP_KERNEL: use in process context, safe to sleep (__GFP_WAIT | 
__GFP_IO | 
__GFP_FS) 
GFP_ATOMIC (High priority & Does not sleep). Use in Interrupt 
handlers, bottom halves where kernel should not sleep 
__GFP_HIGH 
GFP_DMA __GFP_DMA 
__GFP_HIGH: The kernel can access emergency pools. 
__GFP_WAIT: The kernel can sleep 
__GFP_IO: The kernel can start disk I/O 
__GFP_FS: The kernel can start filesystem I/O 
__GFP_DMA: Allocate only DMA-capable memory 
9/14/2014 
RajKumar Rampelli
References 
 Linux Journal Article: 
http://www.linuxjournal.com/article/6930 
 Third Edition of Linux Device Drivers, by 
Jonathan Corbet 
 Vold topics in slideshare.net 
9/14/2014 
RajKumar Rampelli
THANK YOU  
Have a look at 
My PPTs: http://www.slideshare.net/rampalliraj/ 
My Blog: http://practicepeople.blogspot.in/ 
9/14/2014 
RajKumar Rampelli

More Related Content

What's hot

Linux Kernel Image
Linux Kernel ImageLinux Kernel Image
Linux Kernel Image
艾鍗科技
 
U boot porting guide for SoC
U boot porting guide for SoCU boot porting guide for SoC
U boot porting guide for SoC
Macpaul Lin
 

What's hot (20)

Introduction to Modern U-Boot
Introduction to Modern U-BootIntroduction to Modern U-Boot
Introduction to Modern U-Boot
 
Linux device drivers
Linux device driversLinux device drivers
Linux device drivers
 
OSSNA 2017 Performance Analysis Superpowers with Linux BPF
OSSNA 2017 Performance Analysis Superpowers with Linux BPFOSSNA 2017 Performance Analysis Superpowers with Linux BPF
OSSNA 2017 Performance Analysis Superpowers with Linux BPF
 
Linux Kernel Image
Linux Kernel ImageLinux Kernel Image
Linux Kernel Image
 
linux device driver
linux device driverlinux device driver
linux device driver
 
Booting Android: bootloaders, fastboot and boot images
Booting Android: bootloaders, fastboot and boot imagesBooting Android: bootloaders, fastboot and boot images
Booting Android: bootloaders, fastboot and boot images
 
Android Booting Sequence
Android Booting SequenceAndroid Booting Sequence
Android Booting Sequence
 
ACPI Debugging from Linux Kernel
ACPI Debugging from Linux KernelACPI Debugging from Linux Kernel
ACPI Debugging from Linux Kernel
 
I2C Drivers
I2C DriversI2C Drivers
I2C Drivers
 
U boot porting guide for SoC
U boot porting guide for SoCU boot porting guide for SoC
U boot porting guide for SoC
 
Embedded Android : System Development - Part IV
Embedded Android : System Development - Part IVEmbedded Android : System Development - Part IV
Embedded Android : System Development - Part IV
 
Linux I2C
Linux I2CLinux I2C
Linux I2C
 
Embedded Linux Kernel - Build your custom kernel
Embedded Linux Kernel - Build your custom kernelEmbedded Linux Kernel - Build your custom kernel
Embedded Linux Kernel - Build your custom kernel
 
Linux Initialization Process (2)
Linux Initialization Process (2)Linux Initialization Process (2)
Linux Initialization Process (2)
 
Linux dma engine
Linux dma engineLinux dma engine
Linux dma engine
 
Embedded linux network device driver development
Embedded linux network device driver developmentEmbedded linux network device driver development
Embedded linux network device driver development
 
Making Linux do Hard Real-time
Making Linux do Hard Real-timeMaking Linux do Hard Real-time
Making Linux do Hard Real-time
 
Linux-Internals-and-Networking
Linux-Internals-and-NetworkingLinux-Internals-and-Networking
Linux-Internals-and-Networking
 
Linux Kernel MMC Storage driver Overview
Linux Kernel MMC Storage driver OverviewLinux Kernel MMC Storage driver Overview
Linux Kernel MMC Storage driver Overview
 
Decompressed vmlinux: linux kernel initialization from page table configurati...
Decompressed vmlinux: linux kernel initialization from page table configurati...Decompressed vmlinux: linux kernel initialization from page table configurati...
Decompressed vmlinux: linux kernel initialization from page table configurati...
 

Viewers also liked

Viewers also liked (8)

Learn python – for beginners
Learn python – for beginnersLearn python – for beginners
Learn python – for beginners
 
Linux GIT commands
Linux GIT commandsLinux GIT commands
Linux GIT commands
 
System Booting Process overview
System Booting Process overviewSystem Booting Process overview
System Booting Process overview
 
Linux Kernel I/O Schedulers
Linux Kernel I/O SchedulersLinux Kernel I/O Schedulers
Linux Kernel I/O Schedulers
 
Network security and cryptography
Network security and cryptographyNetwork security and cryptography
Network security and cryptography
 
Tasklet vs work queues (Deferrable functions in linux)
Tasklet vs work queues (Deferrable functions in linux)Tasklet vs work queues (Deferrable functions in linux)
Tasklet vs work queues (Deferrable functions in linux)
 
Learn python - for beginners - part-2
Learn python - for beginners - part-2Learn python - for beginners - part-2
Learn python - for beginners - part-2
 
Linux watchdog timer
Linux watchdog timerLinux watchdog timer
Linux watchdog timer
 

Similar to Introduction to Kernel and Device Drivers

Linux kernel driver tutorial vorlesung
Linux kernel driver tutorial vorlesungLinux kernel driver tutorial vorlesung
Linux kernel driver tutorial vorlesung
dns -
 
Linuxdd[1]
Linuxdd[1]Linuxdd[1]
Linuxdd[1]
mcganesh
 
unixlinux - kernelexplain yield in user spaceexplain yield in k.pdf
unixlinux - kernelexplain yield in user spaceexplain yield in k.pdfunixlinux - kernelexplain yield in user spaceexplain yield in k.pdf
unixlinux - kernelexplain yield in user spaceexplain yield in k.pdf
PRATIKSINHA7304
 
Linux Kernel Programming
Linux Kernel ProgrammingLinux Kernel Programming
Linux Kernel Programming
Nalin Sharma
 
DUSK - Develop at Userland Install into Kernel
DUSK - Develop at Userland Install into KernelDUSK - Develop at Userland Install into Kernel
DUSK - Develop at Userland Install into Kernel
Alexey Smirnov
 

Similar to Introduction to Kernel and Device Drivers (20)

Linux kernel driver tutorial vorlesung
Linux kernel driver tutorial vorlesungLinux kernel driver tutorial vorlesung
Linux kernel driver tutorial vorlesung
 
Linux Kernel Development
Linux Kernel DevelopmentLinux Kernel Development
Linux Kernel Development
 
Linuxdd[1]
Linuxdd[1]Linuxdd[1]
Linuxdd[1]
 
Basic Linux Internals
Basic Linux InternalsBasic Linux Internals
Basic Linux Internals
 
Lecture 5 Kernel Development
Lecture 5 Kernel DevelopmentLecture 5 Kernel Development
Lecture 5 Kernel Development
 
Parallel and Distributed Computing Chapter 8
Parallel and Distributed Computing Chapter 8Parallel and Distributed Computing Chapter 8
Parallel and Distributed Computing Chapter 8
 
brief intro to Linux device drivers
brief intro to Linux device driversbrief intro to Linux device drivers
brief intro to Linux device drivers
 
unixlinux - kernelexplain yield in user spaceexplain yield in k.pdf
unixlinux - kernelexplain yield in user spaceexplain yield in k.pdfunixlinux - kernelexplain yield in user spaceexplain yield in k.pdf
unixlinux - kernelexplain yield in user spaceexplain yield in k.pdf
 
Linux Kernel Programming
Linux Kernel ProgrammingLinux Kernel Programming
Linux Kernel Programming
 
Linux scheduler
Linux schedulerLinux scheduler
Linux scheduler
 
Operating system ppt
Operating system pptOperating system ppt
Operating system ppt
 
Operating system ppt
Operating system pptOperating system ppt
Operating system ppt
 
Operating system ppt
Operating system pptOperating system ppt
Operating system ppt
 
Operating system ppt
Operating system pptOperating system ppt
Operating system ppt
 
Linux Device Driver v3 [Chapter 2]
Linux Device Driver v3 [Chapter 2]Linux Device Driver v3 [Chapter 2]
Linux Device Driver v3 [Chapter 2]
 
Linux Module Programming
Linux Module ProgrammingLinux Module Programming
Linux Module Programming
 
DUSK - Develop at Userland Install into Kernel
DUSK - Develop at Userland Install into KernelDUSK - Develop at Userland Install into Kernel
DUSK - Develop at Userland Install into Kernel
 
Nachos 2
Nachos 2Nachos 2
Nachos 2
 
Docker Security Paradigm
Docker Security ParadigmDocker Security Paradigm
Docker Security Paradigm
 
Studienarb linux kernel-dev
Studienarb linux kernel-devStudienarb linux kernel-dev
Studienarb linux kernel-dev
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 

Recently uploaded (20)

Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 

Introduction to Kernel and Device Drivers

  • 1. Introduction to Kernel & Device Drivers Raj Kumar Rampelli
  • 2. Outline  What is kernel ?  What is a Device Driver ?  Types of Device Drivers  What is a Module ?  How to work with Module  Platform devices and its registration  How does probe() gets called ?  Role of Vold in volume mount  Allocation of memory in Kernel  References 9/14/2014 RajKumar Rampelli
  • 3. What is Kernel ?  Kernel is a piece of code & core part of operating system and whose responsibilities are  Process management  Memory management  Device Input/output control: Device Drivers task  File system  Everything is a file in the linux including Device connected to system  Kernel code is written in C and assembly language  Kernel code always runs in Kernel Space 9/14/2014 RajKumar Rampelli
  • 4. Kernel Space & User Space User space • User programs/applications, shell • No direct interaction with Hardware • Takes Kernel help for interacting with h/w Kernel space •Direct Interaction with Hardware •Device Drivers tasks Hardware 9/14/2014 RajKumar Rampelli
  • 5. What is Device Driver ?  Piece of code that will be executed when suitable device is connected to the system  Each device has its own code in the operating system  This code is called “Device Driver”  Works for a specific h/w device  Device Drivers  Hides the details of how a device works  Bridge between user applications and Hardware  Mapping user application calls to specific h/w  Support Hot-plug feature  Drivers will be loaded when needed & will be unloaded when not needed  Also called as “Modules” 9/14/2014 RajKumar Rampelli
  • 6. Types of Device Driver  Character device driver  Byte oriented data transfer  Block device driver  Block(512Byte) oriented data transfer  Network device driver  Packet data transfer 9/14/2014 RajKumar Rampelli
  • 7. What is Module ?  Loadable Kernel object – contains related subroutines, data grouped together  Module executables are represented in kernel object format (.ko files)  It has only one entry function & one exit function  Entry function: called when loaded into kernel  Initialization function (like main() in C)  init_module(void)  Exit function: called when removed from kernel  cleanup_module(void)  Enables hot-plug feature  i.e. dynamic (runtime) loading/removal of drivers into/from kernel in response to device state 9/14/2014 RajKumar Rampelli
  • 8. Working with Module  Insert/load a module into system kernel  insmod module_name.ko 9/14/2014  Remove/unload a module from system kernel  rmmod module_name.ko  Insert a module along with dependent/related modules in one go.  modprobe module_name.ko  Detailed explanation on Module is available at http://practicepeople.blogspot.in/2013/06/ke rnel-programming-1-introduction-to.html RajKumar Rampelli
  • 9. __init* macro() usage  Syntax:  __init function_name();  __initdata variable_name; 9/14/2014  Compiler stores the above function/variable in “init.text”/”init.data” section in vmlinux.ids file  Tells the compiler to free the memory (free_initmem()) once the task of function/variable associated with init macro is completed.  free_initmem(): memory clean up task  Memory optimization  Check Kernel log for “Freeing unused kernel memory: xxxk freed”  Where to use this macro:  Module ‘s Initialization function, since it is called/used only once when loading the module (or during kernel boot up)  More info can be found at http://practicepeople.blogspot.in/2013/07/kernel-programming-3- init-macros-usage.html RajKumar Rampelli
  • 10. Device registration and initialization  Platform devices  Devices that are integrated into a given chip and therefore are always there  The platform-specific initialization code statically initializes such arrays of platform_devices and then registers them using platform_register()  Therefore there is no need for sophisticated probing. What is probing()[Discussed in the next slide]  Instead, the string contained in platform_device.name is compared platform_driver.driver.name and a match is assumed if they are equal. 9/14/2014 RajKumar Rampelli
  • 11. How probe() gets called ? Probe() is called when device is recognized by the platform Driver’s init function gives kernel a list of devices it is able to service, along with a pointer to a probe function. Kernel then calls the driver’s probe function one for each device.  probe function starts the per-device initialization:  initializing hardware,  allocating resources, and  registering the device with the kernel as a block device.  Example: Kernel/drivers/mmc/host/sdhic-tegra.c  sdhci_tegra_probe()  Host controller initialization at kernel/drivers/mmc/host/sdhci.c  sdhci_add_host()  Device initialization starts in kernel/drivers/mmc/core/core.c mmc_rescan()  Starts execution when Host detects the device.  mmc_attach_sdio()  sdio.c [core driver]  mmc_attach_sd()  sd.c [core driver]  mmc_attach_mmc()  mmc.c [core driver] 9/14/2014 RajKumar Rampelli
  • 12. Vold (Volume Manager Daemon)  Listens to kernel Netlink socket events for volume status change  Interacts with MountService (Java layer)  Decision maker to mount any media/device  Receives volute state change notifications from Vold  Uses Unix domain (POSIX Local IPC) socket to communicate with Vold  Ex: Insert SD card on Android device  It stores volume information retrieved from vold.fstab file  This file describes what storage devices will be added into the system. Its format: <mount> <volume label> <mount point> <partition#> <sys fs path to the device>  Netlink Socket: Pass the information between Kernel and User space  Unix domain socket declare: $TOP/system/core/rootdir/init.rc socket vold stream 0660 root mount 9/14/2014 RajKumar Rampelli
  • 13. 9/14/2014 Allocation of memory in Kernel Kmalloc() Kzalloc() Kcalloc() Vmalloc() Allocates contiguous physical blocks of memory in byte-sized chunks Allocates contiguous physical blocks of memory Allocates memory for an array same as kmalloc, except it allocates memory that is only virtually contiguous Memory is not set to zero Memory is set to zero Memory is set to zero underling physical memory can be discontiguous void * kmalloc (size_t size, int flags) void * kzalloc (size_t size, int flags) void * kcalloc (size_ t n, size_t size, unsigned int __noca st flags); void * vmalloc (unsigned long size) Depends on the flag used, it may sleep Depends on the flag used, it may sleep Depends on the flag used, it may sleep Can sleep, so don’t use it in interrupt context RajKumar Rampelli
  • 14. 9/14/2014 Allocation of memory in Kernel Kmalloc() Kzalloc() Kcalloc() Vmalloc() Can allocate upto 4MBytes Same as kmalloc NA to obtain very large regions of memory Simple and fast Simple, preferable and fast Simple and fast Slow compare to kmalloc Vmalloc(): Slower and not advisable if required memory is less. Because, 1. Makes non-contiguous physical memory to continuous virtual memory 2. Setting up the page table entries 3. Pages obtained from vmalloc() must be mapped to their individual pages since physical memory is not contiguous 4. Results in much greater TLB, performance is affected. Note: malloc() function also works in same manner. i.e. allocated memory in continuous in virtual address space of the processor, but there is no guarantee that they are contiguous in the physical address space (physical RAM) RajKumar Rampelli
  • 15. Allocation of memory in Kernel  Flag: controls the behavior of memory allocation and divided into 3 groups  Action modifiers: How to allocate memory. Ex: can sleep or not  Zone modifiers: Where the request should be satisfied. (DMA buffers)  Types: types of allocation FLAG Type Action Modifier Zone Modifier GFP_KERNEL: use in process context, safe to sleep (__GFP_WAIT | __GFP_IO | __GFP_FS) GFP_ATOMIC (High priority & Does not sleep). Use in Interrupt handlers, bottom halves where kernel should not sleep __GFP_HIGH GFP_DMA __GFP_DMA __GFP_HIGH: The kernel can access emergency pools. __GFP_WAIT: The kernel can sleep __GFP_IO: The kernel can start disk I/O __GFP_FS: The kernel can start filesystem I/O __GFP_DMA: Allocate only DMA-capable memory 9/14/2014 RajKumar Rampelli
  • 16. References  Linux Journal Article: http://www.linuxjournal.com/article/6930  Third Edition of Linux Device Drivers, by Jonathan Corbet  Vold topics in slideshare.net 9/14/2014 RajKumar Rampelli
  • 17. THANK YOU  Have a look at My PPTs: http://www.slideshare.net/rampalliraj/ My Blog: http://practicepeople.blogspot.in/ 9/14/2014 RajKumar Rampelli

Editor's Notes

  1. Ls /usr/src/linux/drivers
  2. GFP_ATOMIC: The allocation is high-priority and does not sleep. This is the flag to use in interrupt handlers, bottom halves and other situations where you cannot sleep. GFP_KERNEL This is a normal allocation and might block. This is the flag to use in process context code when it is safe to sleep.