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

Kernel Recipes 2018 - Overview of SD/eMMC, their high speed modes and Linux s...
Kernel Recipes 2018 - Overview of SD/eMMC, their high speed modes and Linux s...Kernel Recipes 2018 - Overview of SD/eMMC, their high speed modes and Linux s...
Kernel Recipes 2018 - Overview of SD/eMMC, their high speed modes and Linux s...Anne Nicolas
 
Android booting sequece and setup and debugging
Android booting sequece and setup and debuggingAndroid booting sequece and setup and debugging
Android booting sequece and setup and debuggingUtkarsh Mankad
 
Introduction Linux Device Drivers
Introduction Linux Device DriversIntroduction Linux Device Drivers
Introduction Linux Device DriversNEEVEE Technologies
 
System Booting Process overview
System Booting Process overviewSystem Booting Process overview
System Booting Process overviewRajKumar Rampelli
 
Q4.11: Introduction to eMMC
Q4.11: Introduction to eMMCQ4.11: Introduction to eMMC
Q4.11: Introduction to eMMCLinaro
 
Android Storage - Vold
Android Storage - VoldAndroid Storage - Vold
Android Storage - VoldWilliam Lee
 
Uboot startup sequence
Uboot startup sequenceUboot startup sequence
Uboot startup sequenceHoucheng Lin
 
U-boot and Android Verified Boot 2.0
U-boot and Android Verified Boot 2.0U-boot and Android Verified Boot 2.0
U-boot and Android Verified Boot 2.0GlobalLogic Ukraine
 
U-Boot presentation 2013
U-Boot presentation  2013U-Boot presentation  2013
U-Boot presentation 2013Wave Digitech
 
Using and Customizing the Android Framework / part 4 of Embedded Android Work...
Using and Customizing the Android Framework / part 4 of Embedded Android Work...Using and Customizing the Android Framework / part 4 of Embedded Android Work...
Using and Customizing the Android Framework / part 4 of Embedded Android Work...Opersys inc.
 
Q4.11: Next Gen Mobile Storage – UFS
Q4.11: Next Gen Mobile Storage – UFSQ4.11: Next Gen Mobile Storage – UFS
Q4.11: Next Gen Mobile Storage – UFSLinaro
 
Kernel Recipes 2015: Linux Kernel IO subsystem - How it works and how can I s...
Kernel Recipes 2015: Linux Kernel IO subsystem - How it works and how can I s...Kernel Recipes 2015: Linux Kernel IO subsystem - How it works and how can I s...
Kernel Recipes 2015: Linux Kernel IO subsystem - How it works and how can I s...Anne Nicolas
 
U boot porting guide for SoC
U boot porting guide for SoCU boot porting guide for SoC
U boot porting guide for SoCMacpaul Lin
 
Learning AOSP - Android Linux Device Driver
Learning AOSP - Android Linux Device DriverLearning AOSP - Android Linux Device Driver
Learning AOSP - Android Linux Device DriverNanik Tolaram
 
U Boot or Universal Bootloader
U Boot or Universal BootloaderU Boot or Universal Bootloader
U Boot or Universal BootloaderSatpal Parmar
 
Native Android Userspace part of the Embedded Android Workshop at Linaro Conn...
Native Android Userspace part of the Embedded Android Workshop at Linaro Conn...Native Android Userspace part of the Embedded Android Workshop at Linaro Conn...
Native Android Userspace part of the Embedded Android Workshop at Linaro Conn...Opersys inc.
 

What's hot (20)

Kernel Recipes 2018 - Overview of SD/eMMC, their high speed modes and Linux s...
Kernel Recipes 2018 - Overview of SD/eMMC, their high speed modes and Linux s...Kernel Recipes 2018 - Overview of SD/eMMC, their high speed modes and Linux s...
Kernel Recipes 2018 - Overview of SD/eMMC, their high speed modes and Linux s...
 
Linux I2C
Linux I2CLinux I2C
Linux I2C
 
Embedded Android : System Development - Part II (HAL)
Embedded Android : System Development - Part II (HAL)Embedded Android : System Development - Part II (HAL)
Embedded Android : System Development - Part II (HAL)
 
Android booting sequece and setup and debugging
Android booting sequece and setup and debuggingAndroid booting sequece and setup and debugging
Android booting sequece and setup and debugging
 
Introduction Linux Device Drivers
Introduction Linux Device DriversIntroduction Linux Device Drivers
Introduction Linux Device Drivers
 
System Booting Process overview
System Booting Process overviewSystem Booting Process overview
System Booting Process overview
 
Q4.11: Introduction to eMMC
Q4.11: Introduction to eMMCQ4.11: Introduction to eMMC
Q4.11: Introduction to eMMC
 
Android Storage - Vold
Android Storage - VoldAndroid Storage - Vold
Android Storage - Vold
 
Uboot startup sequence
Uboot startup sequenceUboot startup sequence
Uboot startup sequence
 
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)
 
U-boot and Android Verified Boot 2.0
U-boot and Android Verified Boot 2.0U-boot and Android Verified Boot 2.0
U-boot and Android Verified Boot 2.0
 
U-Boot presentation 2013
U-Boot presentation  2013U-Boot presentation  2013
U-Boot presentation 2013
 
Using and Customizing the Android Framework / part 4 of Embedded Android Work...
Using and Customizing the Android Framework / part 4 of Embedded Android Work...Using and Customizing the Android Framework / part 4 of Embedded Android Work...
Using and Customizing the Android Framework / part 4 of Embedded Android Work...
 
Q4.11: Next Gen Mobile Storage – UFS
Q4.11: Next Gen Mobile Storage – UFSQ4.11: Next Gen Mobile Storage – UFS
Q4.11: Next Gen Mobile Storage – UFS
 
Embedded Android : System Development - Part IV
Embedded Android : System Development - Part IVEmbedded Android : System Development - Part IV
Embedded Android : System Development - Part IV
 
Kernel Recipes 2015: Linux Kernel IO subsystem - How it works and how can I s...
Kernel Recipes 2015: Linux Kernel IO subsystem - How it works and how can I s...Kernel Recipes 2015: Linux Kernel IO subsystem - How it works and how can I s...
Kernel Recipes 2015: Linux Kernel IO subsystem - How it works and how can I s...
 
U boot porting guide for SoC
U boot porting guide for SoCU boot porting guide for SoC
U boot porting guide for SoC
 
Learning AOSP - Android Linux Device Driver
Learning AOSP - Android Linux Device DriverLearning AOSP - Android Linux Device Driver
Learning AOSP - Android Linux Device Driver
 
U Boot or Universal Bootloader
U Boot or Universal BootloaderU Boot or Universal Bootloader
U Boot or Universal Bootloader
 
Native Android Userspace part of the Embedded Android Workshop at Linaro Conn...
Native Android Userspace part of the Embedded Android Workshop at Linaro Conn...Native Android Userspace part of the Embedded Android Workshop at Linaro Conn...
Native Android Userspace part of the Embedded Android Workshop at Linaro Conn...
 

Viewers also liked

Learn python – for beginners
Learn python – for beginnersLearn python – for beginners
Learn python – for beginnersRajKumar 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 (7)

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
 
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
 

Recently uploaded

Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 

Recently uploaded (20)

Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 

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.