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

The Linux Block Layer - Built for Fast Storage
The Linux Block Layer - Built for Fast StorageThe Linux Block Layer - Built for Fast Storage
The Linux Block Layer - Built for Fast StorageKernel TLV
 
Part 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module ProgrammingPart 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module ProgrammingTushar B Kute
 
Page cache in Linux kernel
Page cache in Linux kernelPage cache in Linux kernel
Page cache in Linux kernelAdrian Huang
 
Fast boot
Fast bootFast boot
Fast bootSZ Lin
 
U boot porting guide for SoC
U boot porting guide for SoCU boot porting guide for SoC
U boot porting guide for SoCMacpaul Lin
 
Linux MMAP & Ioremap introduction
Linux MMAP & Ioremap introductionLinux MMAP & Ioremap introduction
Linux MMAP & Ioremap introductionGene Chang
 
Process Address Space: The way to create virtual address (page table) of user...
Process Address Space: The way to create virtual address (page table) of user...Process Address Space: The way to create virtual address (page table) of user...
Process Address Space: The way to create virtual address (page table) of user...Adrian Huang
 
Linux Internals - Kernel/Core
Linux Internals - Kernel/CoreLinux Internals - Kernel/Core
Linux Internals - Kernel/CoreShay Cohen
 
Understanding a kernel oops and a kernel panic
Understanding a kernel oops and a kernel panicUnderstanding a kernel oops and a kernel panic
Understanding a kernel oops and a kernel panicJoseph Lu
 
Uboot startup sequence
Uboot startup sequenceUboot startup sequence
Uboot startup sequenceHoucheng Lin
 
Introduction to char device driver
Introduction to char device driverIntroduction to char device driver
Introduction to char device driverVandana Salve
 

What's hot (20)

Embedded Android : System Development - Part II (Linux device drivers)
Embedded Android : System Development - Part II (Linux device drivers)Embedded Android : System Development - Part II (Linux device drivers)
Embedded Android : System Development - Part II (Linux device drivers)
 
Embedded Android : System Development - Part III
Embedded Android : System Development - Part IIIEmbedded Android : System Development - Part III
Embedded Android : System Development - Part III
 
The Linux Block Layer - Built for Fast Storage
The Linux Block Layer - Built for Fast StorageThe Linux Block Layer - Built for Fast Storage
The Linux Block Layer - Built for Fast Storage
 
Embedded Android : System Development - Part III (Audio / Video HAL)
Embedded Android : System Development - Part III (Audio / Video HAL)Embedded Android : System Development - Part III (Audio / Video HAL)
Embedded Android : System Development - Part III (Audio / Video HAL)
 
Part 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module ProgrammingPart 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module Programming
 
Embedded Android : System Development - Part I
Embedded Android : System Development - Part IEmbedded Android : System Development - Part I
Embedded Android : System Development - Part I
 
Introduction to Modern U-Boot
Introduction to Modern U-BootIntroduction to Modern U-Boot
Introduction to Modern U-Boot
 
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
 
Page cache in Linux kernel
Page cache in Linux kernelPage cache in Linux kernel
Page cache in Linux kernel
 
Fast boot
Fast bootFast boot
Fast boot
 
U boot porting guide for SoC
U boot porting guide for SoCU boot porting guide for SoC
U boot porting guide for SoC
 
Linux MMAP & Ioremap introduction
Linux MMAP & Ioremap introductionLinux MMAP & Ioremap introduction
Linux MMAP & Ioremap introduction
 
Process Address Space: The way to create virtual address (page table) of user...
Process Address Space: The way to create virtual address (page table) of user...Process Address Space: The way to create virtual address (page table) of user...
Process Address Space: The way to create virtual address (page table) of user...
 
Linux Internals - Kernel/Core
Linux Internals - Kernel/CoreLinux Internals - Kernel/Core
Linux Internals - Kernel/Core
 
Understanding a kernel oops and a kernel panic
Understanding a kernel oops and a kernel panicUnderstanding a kernel oops and a kernel panic
Understanding a kernel oops and a kernel panic
 
Audio Drivers
Audio DriversAudio Drivers
Audio Drivers
 
Uboot startup sequence
Uboot startup sequenceUboot startup sequence
Uboot startup sequence
 
Linux Audio Drivers. ALSA
Linux Audio Drivers. ALSALinux Audio Drivers. ALSA
Linux Audio Drivers. ALSA
 
Linux device drivers
Linux device drivers Linux device drivers
Linux device drivers
 
Introduction to char device driver
Introduction to char device driverIntroduction to char device driver
Introduction to char device driver
 

Viewers also liked

Learn python – for beginners
Learn python – for beginnersLearn python – for beginners
Learn python – for beginnersRajKumar Rampelli
 
System Booting Process overview
System Booting Process overviewSystem Booting Process overview
System Booting Process overviewRajKumar Rampelli
 
Network security and cryptography
Network security and cryptographyNetwork security and cryptography
Network security and cryptographyRajKumar Rampelli
 
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)RajKumar Rampelli
 
Learn python - for beginners - part-2
Learn python - for beginners - part-2Learn python - for beginners - part-2
Learn python - for beginners - part-2RajKumar Rampelli
 

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 vorlesungdns -
 
Linux Kernel Development
Linux Kernel DevelopmentLinux Kernel Development
Linux Kernel DevelopmentPriyank Kapadia
 
Linuxdd[1]
Linuxdd[1]Linuxdd[1]
Linuxdd[1]mcganesh
 
Lecture 5 Kernel Development
Lecture 5 Kernel DevelopmentLecture 5 Kernel Development
Lecture 5 Kernel DevelopmentMohammed Farrag
 
Parallel and Distributed Computing Chapter 8
Parallel and Distributed Computing Chapter 8Parallel and Distributed Computing Chapter 8
Parallel and Distributed Computing Chapter 8AbdullahMunir32
 
brief intro to Linux device drivers
brief intro to Linux device driversbrief intro to Linux device drivers
brief intro to Linux device driversAlexandre Moreno
 
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.pdfPRATIKSINHA7304
 
Linux Kernel Programming
Linux Kernel ProgrammingLinux Kernel Programming
Linux Kernel ProgrammingNalin Sharma
 
Linux Device Driver v3 [Chapter 2]
Linux Device Driver v3 [Chapter 2]Linux Device Driver v3 [Chapter 2]
Linux Device Driver v3 [Chapter 2]Anupam Datta
 
Linux Module Programming
Linux Module ProgrammingLinux Module Programming
Linux Module ProgrammingAmir Payberah
 
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 KernelAlexey Smirnov
 
Docker Security Paradigm
Docker Security ParadigmDocker Security Paradigm
Docker Security ParadigmAnis LARGUEM
 

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
 

More from RajKumar Rampelli

Writing Character driver (loadable module) in linux
Writing Character driver (loadable module) in linuxWriting Character driver (loadable module) in linux
Writing Character driver (loadable module) in linuxRajKumar Rampelli
 
Introduction to Python - Running Notes
Introduction to Python - Running NotesIntroduction to Python - Running Notes
Introduction to Python - Running NotesRajKumar Rampelli
 

More from RajKumar Rampelli (6)

Writing Character driver (loadable module) in linux
Writing Character driver (loadable module) in linuxWriting Character driver (loadable module) in linux
Writing Character driver (loadable module) in linux
 
Introduction to Python - Running Notes
Introduction to Python - Running NotesIntroduction to Python - Running Notes
Introduction to Python - Running Notes
 
Sql injection attack
Sql injection attackSql injection attack
Sql injection attack
 
Turing awards seminar
Turing awards seminarTuring awards seminar
Turing awards seminar
 
Higher education importance
Higher education importanceHigher education importance
Higher education importance
 
C compilation process
C compilation processC compilation process
C compilation process
 

Recently uploaded

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 

Recently uploaded (20)

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 

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.