SlideShare a Scribd company logo
1 of 43
Download to read offline
Team Emertxe
Linux Device Drivers
An Introduction
Introduction
Familiarity Check
●
Good C & Programming Skills
●
Linux & the Filesytem
– Root, User Space Headers & Libraries
●
Files
– Regular, Special, Device
●
Toolchain
– gcc & friends
●
Make & Makefiles
●
Kernel Sources (Location & Building)
The Flow
● Introduction
– Linux kernel Ecosystem
– Kernel Souce Organization
– Command set and Files
– Writing the first driver (Module)
● Character Drivers
– Device files
– Device access from user space (End to End flow)
– Registering the driver
– File Operations and registration
– Data transfer between User and Kernel space
– ioctl
● Memory & Hardware
● Time & Timings
● USB Drivers
● Interrupt Handling
● Block Drivers
● PCI Drivers
● Debugging
The Flow...
●
Introduction
– Linux kernel Ecosystem
– Kernel Souce Organization
– Command set and Files
– Writing the first driver (Module)
●
Character Drivers
– Device files
– Device access from user space (End to End flow)
● Memory & Hardware
● Time & Timings
● USB Drivers
●
Interrupt Handling
●
Block Drivers
● PCI Drivers
● Debugging
Hands-On
●
Your First Driver
●
Character Drivers
– Null Driver
– Memory Driver
– UART Driver for Customized Hardware
●
USB Drivers
– USB Device Hot-plug-ability
– USB to Serial Hardware Driver
●
Filesystem Modules
– VFS Interfacing
– “Pseudo” File System with Memory Files
Linux Driver
Ecosystem
bash gvim X Server gcc firefox
`
`
Process
Management
ssh
Memory
Management
File Systems
Device
Control
Networking
Architecture
Dependent
Code
Character
Devices
Memory
Manager
Filesystem
Types
Block
Devices
Network
Subsystem
IF
Devices
Concurrency
MultiTasking
Virtual
Memory
Files & Dirs:
The VFS
Ttys &
Device Access
Connectivity
CPU Memory
Disks &
CDs
Consoles,
etc
Network
Interfaces
Kernel Source
Organization
Kernel Source
include
net
drivers
block
fs
mm
kernel
arch
char mtd/ide net pci ...usbserial
asm-<arch>linux
arm powerpc sparc x86 ...
The Locations &
Config Files
● Kernel Source Path: /usr/src/linux
● Std Modules Path:
– /lib/modules/<kernel version>/kernel/...
●
Module Configuration: /etc/modprobe.conf
● Kernel Windows:
– /proc
– /sys
●
System Logs: /var/log/messages
The Commands
●
lsmod
●
insmod
●
modprobe
●
rmmod
●
dmesg
●
objdump
●
nm
●
cat /proc/<file>
The Kernel's C
●
ctor & dtor
– init_module, cleanup_module
●
printf
– printk
● Libraries
– <kernel src>/kernel
●
Headers
– <kernel src>/include
The Init Code
static int __init mfd_init(void)
{
printk(KERN_INFO "mfd registered");
...
return 0;
}
module_init(mfd_init);
The Cleanup Code
static void __exit mfd_exit(void)
{
printk(KERN_INFO "mfd deregistered");
...
}
module_exit(mfd_exit);
Usage of printk
● <linux/kernel.h>
● Constant String for Log Level
– KERN_EMERG "<0>" /* system is unusable */
– KERN_ALERT "<1>" /* action must be taken immediately */
– KERN_CRIT "<2>" /* critical conditions */
– KERN_ERR "<3>" /* error conditions */
– KERN_WARNING "<4>" /* warning conditions */
– KERN_NOTICE "<5>" /* normal but significant condition */
– KERN_INFO "<6>" /* informational */
– KERN_DEBUG "<7>" /* debug-level messages */
●
printf like arguments
The Other Basics &
Ornaments
● Headers
– #include <linux/module.h>
– #include <linux/version.h>
– #include <linux/kernel.h>
● MODULE_LICENSE("GPL");
● MODULE_AUTHOR("Emertxe");
● MODULE_DESCRIPTION("First Device Driver");
Building the Module
● Our driver needs
– The Kernel Headers for Prototypes
– The Kernel Functions for Functionality
– The Kernel Build System & the Makefile for Building
●
Two options
– Building under Kernel Source Tree
● Put our driver under drivers folder
● Edit Kconfig(s) & Makefile to include our driver
– Create our own Makefile to do the right invocation
Our Makefile
ifneq (${KERNELRELEASE},)
obj-m += <module>.o
else
KERNEL_SOURCE := <kernel source directory path>
PWD := $(shell pwd)
default:
$(MAKE) -C ${KERNEL_SOURCE} SUBDIRS=$(PWD) modules
clean:
$(MAKE) -C ${KERNEL_SOURCE} SUBDIRS=$(PWD) clean
endif
Try Out your First Driver
Character Drivers
Major &
Minor Number
● ls -l /dev
● Major is to Driver; Minor is to Device
● <linux/types.h> (>= 2.6.0)
– dev_t: 12 & 20 bits for major & minor
● <linux/kdev_t.h>
– MAJOR(dev_t dev)
– MINOR(dev_t dev)
– MKDEV(int major, int minor)
Registering &
Unregistering
●
Registering the Device Driver
– int register_chrdev_region(dev_t first, unsigned int count,
char *name);
– int alloc_chrdev_region(dev_t *dev, unsigned int firstminor,
unsigned int cnt, char *name);
●
Unregistering the Device Driver
– void unregister_chrdev_region(dev_t first, unsigned int
count);
●
Header: <linux/fs.h>
The file operations
● #include <linux/fs.h>
● struct file_operations
– int (*open)(struct inode *, struct file *);
– int (*release)(struct inode *, struct file *);
– ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
– ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
– struct module owner = THIS_MODULE; / linux/module.h> */
– loff_t (*llseek)(struct file *, loff_t, int);
– int (*ioctl)(struct inode *, struct file *, unsigned int, unsigned long);
User level I/O
●
int open(const char *path, int oflag, ... )
●
int close(int fd);
●
ssize_t write(int fd, const void *buf, size_t nbyte)
●
ssize_t read(int fd, void *buf, size_t nbyte)
●
int ioctl(int d, int request, ...)
– The ioctl() function manipulates the underlying device
parameters of special files.
– The argument d must be an open file descriptor.
– The second argument is a device-dependent request code.
The file &
inode structures
●
struct file
– mode_t f_mode
– loff_t f_pos
– unsigned int f_flags
– struct file_operations *f_op
– void * private_data
●
struct inode
– unsigned int iminor(struct inode *);
– unsigned int imajor(struct inode *);
Registering the
file operations
●
#include <linux/cdev.h>
●
1st way initialization:
– struct cdev *my_cdev = cdev_alloc();
– my_cdev->owner = THIS_MODULE;
– my_cdev->ops = &my_fops;
● 2nd way initialization:
– struct cdev my_cdev;
– cdev_init(&my_cdev, &my_fops);
– my_cdev.owner = THIS_MODULE;
– my_cdev.ops = &my_fops;
Registering the
file operations...
● The Registration
– int cdev_add(struct cdev *cdev, dev_t num,
unsigned int count);
●
The Unregistration
– void cdev_del(struct cdev *cdev);
Registering/Unregistering
Old Way
● Registering the Device Driver
– int register_chrdev(undigned int major, const char *name,
struct file_operations *fops);
● Unregistering the Device Driver
– int unregister_chrdev(undigned int major, const char
*name);
The read flow
struct file
-------------------------
f_count
f_flags
f_mode
-------------------------
f_pos
-------------------------
...
...
ssize_t my_read(struct file *f, char __user *buf, size_t cnt, loff_t *off)
Buffer
(in the driver)
Buffer
(in the
application
or libc)
Kernel Space (Non-swappable) User Space (Swappable)
copy_to_user
The /dev/null
read & write
ssize_t my_read(struct file *f, char __user *buf, size_t cnt, loff_t
*off)
{
...
return read_cnt;
}
ssize_t my_write(struct file *f, char __user *buf, size_t cnt, loff_t
*off)
{
...
return wrote_cnt;
}
The mem device read
ssize_t my_read(struct file *f, char __user *buf, size_t cnt, loff_t
*off)
{
...
if (copy_to_user(buf, from, cnt) != 0)
{
return -EFAULT;
}
...
return read_cnt;
}
The mem device write
ssize_t my_write(struct file *f, char __user *buf, size_t cnt, loff_t
*off)
{
...
if (copy_from_user(to, buf, cnt) != 0)
{
return -EFAULT;
}
...
return wrote_cnt;
}
Dynamic Device Node
& Classes
● Class Operations
– struct class *class_create(struct module *owner, char
*name);
– void class_destroy(struct class *cl);
●
Device into & Out of Class
– struct class_device *device_create(struct class *cl, NULL,
dev_t devnum, NULL, const char *fmt, ...);
– void device_destroy(struct class *cl, dev_t devnum);
The I/O Control API
●
int (*ioctl)(struct inode *, struct file *, unsigned int cmd, unsigned long arg)
●
int (*unlocked_ioctl)(struct file *, unsigned int cmd, unsigned long arg)
●
Command
– <linux/ioctl.h> -> ... -> <asm-generic/ioctl.h>
– Macros
●
_IO, _IOR, _IOW, _IOWR
– Parameters
●
type (Magic character) [15:8]
●
number (index) [7:0]
●
size (param type) [29:16]
The I/O Control API
● Macro Usage
_IO(type, index)
[_IOR | _IOW | _IOWR](type, index,
datatype/size)
Module Parameters
●
<linux/moduleparam.h>
– Macros
● module_param(name, type, perm)
● module_param_array(name, type, num, perm)
● Perm (is a bitmask)
– 0
– S_IRUGO
– S_IWUSR | S_IRUGO
– Loading
● insmod driver.ko name=10
x86 Architecture
A
B
Memory Access
Physical Vs
Virtual Memory
●
The kernel Organizes Physical memory in to pages
– Page size Depends on Arch
● X86-based 4096 bytes
●
On 32-bit X86 system Kernel total Virtual address space
– Total 4GB (pointer size)
– Kernel Configuration Splits 4GB in to
● 3BG Virtual Sp for US
● 1GB Virtual Sp for Kernel
– 128MB KDS
– Virtual Address also called “Logical Address”
Memory Access from
Kernel Space
●
Virtual Address on Physical Address
– #include <linux/gfp.h>
●
unsigned long __get_free_pages(flags, order); etc
●
void free_pages(addr, order); etc
– #include <linux/slab.h>
●
void *kmalloc(size_t size, gfp_t flags);
– GFP_ATOMIC, GFP_KERNEL, GFP_DMA
● void kfree(void *obj);
– #include <linux/vmalloc.h>
● void *vmalloc(unsigned long size);
●
void vfree(void *addr);
Memory Access from
Kernel Space...
●
Virtual Address for Bus/IO Address
– #include <asm/io.h>
● void *ioremap(unsigned long offset, unsigned long size);
● void iounmap(void *addr);
● I/O Memory Access
– #include <asm/io.h>
● unsigned int ioread[8|16|32](void *addr);
● unsigned int iowrite[8|16|32](u[8|16|32] value, void *addr);
●
Barriers
– #include <linux/kernel.h>: void barrier(void);
– #include <asm/system.h>: void [r|w|]mb(void);
Hardware Access
I/O Accesses from
Kernel Space
● I/O Port Access
– #include <asm/io.h>
● unsigned in[b|w|l](unsigned port);
● void out[b|w|l](unsigned [char|short|int] value,
unsigned port);
Hands-On the Hardware

More Related Content

What's hot

Android device driver structure introduction
Android device driver structure introductionAndroid device driver structure introduction
Android device driver structure introductionWilliam Liang
 
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.
 
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.
 
Learning AOSP - Android Booting Process
Learning AOSP - Android Booting ProcessLearning AOSP - Android Booting Process
Learning AOSP - Android Booting ProcessNanik Tolaram
 
Learning AOSP - Android Linux Device Driver
Learning AOSP - Android Linux Device DriverLearning AOSP - Android Linux Device Driver
Learning AOSP - Android Linux Device DriverNanik Tolaram
 
Android's HIDL: Treble in the HAL
Android's HIDL: Treble in the HALAndroid's HIDL: Treble in the HAL
Android's HIDL: Treble in the HALOpersys inc.
 
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 imagesChris Simmonds
 
"Learning AOSP" - Android Hardware Abstraction Layer (HAL)
"Learning AOSP" - Android Hardware Abstraction Layer (HAL)"Learning AOSP" - Android Hardware Abstraction Layer (HAL)
"Learning AOSP" - Android Hardware Abstraction Layer (HAL)Nanik Tolaram
 
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
 
Understanding the Android System Server
Understanding the Android System ServerUnderstanding the Android System Server
Understanding the Android System ServerOpersys inc.
 
Android Internals at Linaro Connect Asia 2013
Android Internals at Linaro Connect Asia 2013Android Internals at Linaro Connect Asia 2013
Android Internals at Linaro Connect Asia 2013Opersys inc.
 
Quickboot on i.MX6
Quickboot on i.MX6Quickboot on i.MX6
Quickboot on i.MX6Gary Bisson
 
Android media framework overview
Android media framework overviewAndroid media framework overview
Android media framework overviewJerrin George
 

What's hot (20)

Android device driver structure introduction
Android device driver structure introductionAndroid device driver structure introduction
Android device driver structure introduction
 
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...
 
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...
 
Automotive android
Automotive androidAutomotive android
Automotive android
 
Learning AOSP - Android Booting Process
Learning AOSP - Android Booting ProcessLearning AOSP - Android Booting Process
Learning AOSP - Android Booting Process
 
Learning AOSP - Android Linux Device Driver
Learning AOSP - Android Linux Device DriverLearning AOSP - Android Linux Device Driver
Learning AOSP - Android Linux Device Driver
 
Android Internals
Android InternalsAndroid Internals
Android Internals
 
Android's HIDL: Treble in the HAL
Android's HIDL: Treble in the HALAndroid's HIDL: Treble in the HAL
Android's HIDL: Treble in the HAL
 
Android 10
Android 10Android 10
Android 10
 
BusyBox for Embedded Linux
BusyBox for Embedded LinuxBusyBox for Embedded Linux
BusyBox for Embedded Linux
 
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 Internals
Android InternalsAndroid Internals
Android Internals
 
"Learning AOSP" - Android Hardware Abstraction Layer (HAL)
"Learning AOSP" - Android Hardware Abstraction Layer (HAL)"Learning AOSP" - Android Hardware Abstraction Layer (HAL)
"Learning AOSP" - Android Hardware Abstraction Layer (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
 
Understanding the Android System Server
Understanding the Android System ServerUnderstanding the Android System Server
Understanding the Android System Server
 
Android Internals at Linaro Connect Asia 2013
Android Internals at Linaro Connect Asia 2013Android Internals at Linaro Connect Asia 2013
Android Internals at Linaro Connect Asia 2013
 
Quickboot on i.MX6
Quickboot on i.MX6Quickboot on i.MX6
Quickboot on i.MX6
 
Low Level View of Android System Architecture
Low Level View of Android System ArchitectureLow Level View of Android System Architecture
Low Level View of Android System Architecture
 
Android media framework overview
Android media framework overviewAndroid media framework overview
Android media framework overview
 
Android Audio System
Android Audio SystemAndroid Audio System
Android Audio System
 

Viewers also liked

Viewers also liked (20)

Embedded Linux on ARM
Embedded Linux on ARMEmbedded Linux on ARM
Embedded Linux on ARM
 
Linux device drivers
Linux device drivers Linux device drivers
Linux device drivers
 
Communication Protocols (UART, SPI,I2C)
Communication Protocols (UART, SPI,I2C)Communication Protocols (UART, SPI,I2C)
Communication Protocols (UART, SPI,I2C)
 
Linux systems - Getting started with setting up and embedded platform
Linux systems - Getting started with setting up and embedded platformLinux systems - Getting started with setting up and embedded platform
Linux systems - Getting started with setting up and embedded platform
 
Embedded C
Embedded CEmbedded C
Embedded C
 
File systems for Embedded Linux
File systems for Embedded LinuxFile systems for Embedded Linux
File systems for Embedded Linux
 
Introduction to Embedded Systems
Introduction to Embedded Systems Introduction to Embedded Systems
Introduction to Embedded Systems
 
Data Structures & Algorithm design using C
Data Structures & Algorithm design using C Data Structures & Algorithm design using C
Data Structures & Algorithm design using C
 
Embedded C - Optimization techniques
Embedded C - Optimization techniquesEmbedded C - Optimization techniques
Embedded C - Optimization techniques
 
Emertxe : Linux training portfolio
Emertxe : Linux training portfolioEmertxe : Linux training portfolio
Emertxe : Linux training portfolio
 
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
 
Interview preparation workshop
Interview preparation workshopInterview preparation workshop
Interview preparation workshop
 
Linux Internals - Part I
Linux Internals - Part ILinux Internals - Part I
Linux Internals - Part I
 
Embedded Linux - Building toolchain
Embedded Linux - Building toolchainEmbedded Linux - Building toolchain
Embedded Linux - Building toolchain
 
Linux Internals - Part III
Linux Internals - Part IIILinux Internals - Part III
Linux Internals - Part III
 
Emertxe : Training portfolio
Emertxe : Training portfolioEmertxe : Training portfolio
Emertxe : Training portfolio
 
Resume Preparation - Workshop
Resume Preparation - WorkshopResume Preparation - Workshop
Resume Preparation - Workshop
 
Linux Internals - Part II
Linux Internals - Part IILinux Internals - Part II
Linux Internals - Part II
 
A practical guide to buildroot
A practical guide to buildrootA practical guide to buildroot
A practical guide to buildroot
 
Getting started with BeagleBone Black - Embedded Linux
Getting started with BeagleBone Black - Embedded LinuxGetting started with BeagleBone Black - Embedded Linux
Getting started with BeagleBone Black - Embedded Linux
 

Similar to Embedded Android : System Development - Part II (Linux device drivers)

Android for Embedded Linux Developers
Android for Embedded Linux DevelopersAndroid for Embedded Linux Developers
Android for Embedded Linux DevelopersOpersys inc.
 
Hunting and Exploiting Bugs in Kernel Drivers - DefCamp 2012
Hunting and Exploiting Bugs in Kernel Drivers - DefCamp 2012Hunting and Exploiting Bugs in Kernel Drivers - DefCamp 2012
Hunting and Exploiting Bugs in Kernel Drivers - DefCamp 2012DefCamp
 
Char Drivers And Debugging Techniques
Char Drivers And Debugging TechniquesChar Drivers And Debugging Techniques
Char Drivers And Debugging TechniquesYourHelper1
 
Automotive Grade Linux and systemd
Automotive Grade Linux and systemdAutomotive Grade Linux and systemd
Automotive Grade Linux and systemdAlison Chaiken
 
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 char device driver
Introduction to char device driverIntroduction to char device driver
Introduction to char device driverVandana Salve
 
SELinux Kernel Internals and Architecture - FOSS.IN/2005
SELinux Kernel Internals and Architecture - FOSS.IN/2005SELinux Kernel Internals and Architecture - FOSS.IN/2005
SELinux Kernel Internals and Architecture - FOSS.IN/2005James Morris
 
Basis Linux (aan de hand van LPIC-1)
Basis Linux (aan de hand van LPIC-1)Basis Linux (aan de hand van LPIC-1)
Basis Linux (aan de hand van LPIC-1)Peter Martin
 
Lec 10-linux-review
Lec 10-linux-reviewLec 10-linux-review
Lec 10-linux-reviewabinaya m
 
Grub and dracut ii
Grub and dracut iiGrub and dracut ii
Grub and dracut iiplarsen67
 
Leveraging Android's Linux Heritage at AnDevCon IV
Leveraging Android's Linux Heritage at AnDevCon IVLeveraging Android's Linux Heritage at AnDevCon IV
Leveraging Android's Linux Heritage at AnDevCon IVOpersys inc.
 
Open CL For Speedup Workshop
Open CL For Speedup WorkshopOpen CL For Speedup Workshop
Open CL For Speedup WorkshopOfer Rosenberg
 
Linux kernel modules
Linux kernel modulesLinux kernel modules
Linux kernel modulesEddy Reyes
 
Linux Char Device Driver
Linux Char Device DriverLinux Char Device Driver
Linux Char Device DriverGary Yeh
 
Windows内核技术介绍
Windows内核技术介绍Windows内核技术介绍
Windows内核技术介绍jeffz
 

Similar to Embedded Android : System Development - Part II (Linux device drivers) (20)

Android for Embedded Linux Developers
Android for Embedded Linux DevelopersAndroid for Embedded Linux Developers
Android for Embedded Linux Developers
 
Hunting and Exploiting Bugs in Kernel Drivers - DefCamp 2012
Hunting and Exploiting Bugs in Kernel Drivers - DefCamp 2012Hunting and Exploiting Bugs in Kernel Drivers - DefCamp 2012
Hunting and Exploiting Bugs in Kernel Drivers - DefCamp 2012
 
Char Drivers And Debugging Techniques
Char Drivers And Debugging TechniquesChar Drivers And Debugging Techniques
Char Drivers And Debugging Techniques
 
Character drivers
Character driversCharacter drivers
Character drivers
 
Automotive Grade Linux and systemd
Automotive Grade Linux and systemdAutomotive Grade Linux and systemd
Automotive Grade Linux and systemd
 
Linux kernel modules
Linux kernel modulesLinux kernel modules
Linux kernel modules
 
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 char device driver
Introduction to char device driverIntroduction to char device driver
Introduction to char device driver
 
SELinux Kernel Internals and Architecture - FOSS.IN/2005
SELinux Kernel Internals and Architecture - FOSS.IN/2005SELinux Kernel Internals and Architecture - FOSS.IN/2005
SELinux Kernel Internals and Architecture - FOSS.IN/2005
 
Driver_linux
Driver_linuxDriver_linux
Driver_linux
 
Basis Linux (aan de hand van LPIC-1)
Basis Linux (aan de hand van LPIC-1)Basis Linux (aan de hand van LPIC-1)
Basis Linux (aan de hand van LPIC-1)
 
An Introduction To Linux
An Introduction To LinuxAn Introduction To Linux
An Introduction To Linux
 
Lec 10-linux-review
Lec 10-linux-reviewLec 10-linux-review
Lec 10-linux-review
 
Understanding The Boot Process
Understanding The Boot ProcessUnderstanding The Boot Process
Understanding The Boot Process
 
Grub and dracut ii
Grub and dracut iiGrub and dracut ii
Grub and dracut ii
 
Leveraging Android's Linux Heritage at AnDevCon IV
Leveraging Android's Linux Heritage at AnDevCon IVLeveraging Android's Linux Heritage at AnDevCon IV
Leveraging Android's Linux Heritage at AnDevCon IV
 
Open CL For Speedup Workshop
Open CL For Speedup WorkshopOpen CL For Speedup Workshop
Open CL For Speedup Workshop
 
Linux kernel modules
Linux kernel modulesLinux kernel modules
Linux kernel modules
 
Linux Char Device Driver
Linux Char Device DriverLinux Char Device Driver
Linux Char Device Driver
 
Windows内核技术介绍
Windows内核技术介绍Windows内核技术介绍
Windows内核技术介绍
 

More from Emertxe Information Technologies Pvt Ltd

More from Emertxe Information Technologies Pvt Ltd (20)

premium post (1).pdf
premium post (1).pdfpremium post (1).pdf
premium post (1).pdf
 
Career Transition (1).pdf
Career Transition (1).pdfCareer Transition (1).pdf
Career Transition (1).pdf
 
10_isxdigit.pdf
10_isxdigit.pdf10_isxdigit.pdf
10_isxdigit.pdf
 
01_student_record.pdf
01_student_record.pdf01_student_record.pdf
01_student_record.pdf
 
02_swap.pdf
02_swap.pdf02_swap.pdf
02_swap.pdf
 
01_sizeof.pdf
01_sizeof.pdf01_sizeof.pdf
01_sizeof.pdf
 
07_product_matrix.pdf
07_product_matrix.pdf07_product_matrix.pdf
07_product_matrix.pdf
 
06_sort_names.pdf
06_sort_names.pdf06_sort_names.pdf
06_sort_names.pdf
 
05_fragments.pdf
05_fragments.pdf05_fragments.pdf
05_fragments.pdf
 
04_magic_square.pdf
04_magic_square.pdf04_magic_square.pdf
04_magic_square.pdf
 
03_endianess.pdf
03_endianess.pdf03_endianess.pdf
03_endianess.pdf
 
02_variance.pdf
02_variance.pdf02_variance.pdf
02_variance.pdf
 
01_memory_manager.pdf
01_memory_manager.pdf01_memory_manager.pdf
01_memory_manager.pdf
 
09_nrps.pdf
09_nrps.pdf09_nrps.pdf
09_nrps.pdf
 
11_pangram.pdf
11_pangram.pdf11_pangram.pdf
11_pangram.pdf
 
10_combinations.pdf
10_combinations.pdf10_combinations.pdf
10_combinations.pdf
 
08_squeeze.pdf
08_squeeze.pdf08_squeeze.pdf
08_squeeze.pdf
 
07_strtok.pdf
07_strtok.pdf07_strtok.pdf
07_strtok.pdf
 
06_reverserec.pdf
06_reverserec.pdf06_reverserec.pdf
06_reverserec.pdf
 
05_reverseiter.pdf
05_reverseiter.pdf05_reverseiter.pdf
05_reverseiter.pdf
 

Recently uploaded

Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
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
 
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
 
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
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
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
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
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
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
[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
 

Recently uploaded (20)

Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
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...
 
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
 
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
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
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
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
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...
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
[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
 

Embedded Android : System Development - Part II (Linux device drivers)

  • 1. Team Emertxe Linux Device Drivers An Introduction
  • 3. Familiarity Check ● Good C & Programming Skills ● Linux & the Filesytem – Root, User Space Headers & Libraries ● Files – Regular, Special, Device ● Toolchain – gcc & friends ● Make & Makefiles ● Kernel Sources (Location & Building)
  • 4. The Flow ● Introduction – Linux kernel Ecosystem – Kernel Souce Organization – Command set and Files – Writing the first driver (Module) ● Character Drivers – Device files – Device access from user space (End to End flow) – Registering the driver – File Operations and registration – Data transfer between User and Kernel space – ioctl ● Memory & Hardware ● Time & Timings ● USB Drivers ● Interrupt Handling ● Block Drivers ● PCI Drivers ● Debugging
  • 5. The Flow... ● Introduction – Linux kernel Ecosystem – Kernel Souce Organization – Command set and Files – Writing the first driver (Module) ● Character Drivers – Device files – Device access from user space (End to End flow) ● Memory & Hardware ● Time & Timings ● USB Drivers ● Interrupt Handling ● Block Drivers ● PCI Drivers ● Debugging
  • 6. Hands-On ● Your First Driver ● Character Drivers – Null Driver – Memory Driver – UART Driver for Customized Hardware ● USB Drivers – USB Device Hot-plug-ability – USB to Serial Hardware Driver ● Filesystem Modules – VFS Interfacing – “Pseudo” File System with Memory Files
  • 7. Linux Driver Ecosystem bash gvim X Server gcc firefox ` ` Process Management ssh Memory Management File Systems Device Control Networking Architecture Dependent Code Character Devices Memory Manager Filesystem Types Block Devices Network Subsystem IF Devices Concurrency MultiTasking Virtual Memory Files & Dirs: The VFS Ttys & Device Access Connectivity CPU Memory Disks & CDs Consoles, etc Network Interfaces
  • 8. Kernel Source Organization Kernel Source include net drivers block fs mm kernel arch char mtd/ide net pci ...usbserial asm-<arch>linux arm powerpc sparc x86 ...
  • 9. The Locations & Config Files ● Kernel Source Path: /usr/src/linux ● Std Modules Path: – /lib/modules/<kernel version>/kernel/... ● Module Configuration: /etc/modprobe.conf ● Kernel Windows: – /proc – /sys ● System Logs: /var/log/messages
  • 11. The Kernel's C ● ctor & dtor – init_module, cleanup_module ● printf – printk ● Libraries – <kernel src>/kernel ● Headers – <kernel src>/include
  • 12. The Init Code static int __init mfd_init(void) { printk(KERN_INFO "mfd registered"); ... return 0; } module_init(mfd_init);
  • 13. The Cleanup Code static void __exit mfd_exit(void) { printk(KERN_INFO "mfd deregistered"); ... } module_exit(mfd_exit);
  • 14. Usage of printk ● <linux/kernel.h> ● Constant String for Log Level – KERN_EMERG "<0>" /* system is unusable */ – KERN_ALERT "<1>" /* action must be taken immediately */ – KERN_CRIT "<2>" /* critical conditions */ – KERN_ERR "<3>" /* error conditions */ – KERN_WARNING "<4>" /* warning conditions */ – KERN_NOTICE "<5>" /* normal but significant condition */ – KERN_INFO "<6>" /* informational */ – KERN_DEBUG "<7>" /* debug-level messages */ ● printf like arguments
  • 15. The Other Basics & Ornaments ● Headers – #include <linux/module.h> – #include <linux/version.h> – #include <linux/kernel.h> ● MODULE_LICENSE("GPL"); ● MODULE_AUTHOR("Emertxe"); ● MODULE_DESCRIPTION("First Device Driver");
  • 16. Building the Module ● Our driver needs – The Kernel Headers for Prototypes – The Kernel Functions for Functionality – The Kernel Build System & the Makefile for Building ● Two options – Building under Kernel Source Tree ● Put our driver under drivers folder ● Edit Kconfig(s) & Makefile to include our driver – Create our own Makefile to do the right invocation
  • 17. Our Makefile ifneq (${KERNELRELEASE},) obj-m += <module>.o else KERNEL_SOURCE := <kernel source directory path> PWD := $(shell pwd) default: $(MAKE) -C ${KERNEL_SOURCE} SUBDIRS=$(PWD) modules clean: $(MAKE) -C ${KERNEL_SOURCE} SUBDIRS=$(PWD) clean endif
  • 18. Try Out your First Driver
  • 20. Major & Minor Number ● ls -l /dev ● Major is to Driver; Minor is to Device ● <linux/types.h> (>= 2.6.0) – dev_t: 12 & 20 bits for major & minor ● <linux/kdev_t.h> – MAJOR(dev_t dev) – MINOR(dev_t dev) – MKDEV(int major, int minor)
  • 21. Registering & Unregistering ● Registering the Device Driver – int register_chrdev_region(dev_t first, unsigned int count, char *name); – int alloc_chrdev_region(dev_t *dev, unsigned int firstminor, unsigned int cnt, char *name); ● Unregistering the Device Driver – void unregister_chrdev_region(dev_t first, unsigned int count); ● Header: <linux/fs.h>
  • 22. The file operations ● #include <linux/fs.h> ● struct file_operations – int (*open)(struct inode *, struct file *); – int (*release)(struct inode *, struct file *); – ssize_t (*read)(struct file *, char __user *, size_t, loff_t *); – ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *); – struct module owner = THIS_MODULE; / linux/module.h> */ – loff_t (*llseek)(struct file *, loff_t, int); – int (*ioctl)(struct inode *, struct file *, unsigned int, unsigned long);
  • 23. User level I/O ● int open(const char *path, int oflag, ... ) ● int close(int fd); ● ssize_t write(int fd, const void *buf, size_t nbyte) ● ssize_t read(int fd, void *buf, size_t nbyte) ● int ioctl(int d, int request, ...) – The ioctl() function manipulates the underlying device parameters of special files. – The argument d must be an open file descriptor. – The second argument is a device-dependent request code.
  • 24. The file & inode structures ● struct file – mode_t f_mode – loff_t f_pos – unsigned int f_flags – struct file_operations *f_op – void * private_data ● struct inode – unsigned int iminor(struct inode *); – unsigned int imajor(struct inode *);
  • 25. Registering the file operations ● #include <linux/cdev.h> ● 1st way initialization: – struct cdev *my_cdev = cdev_alloc(); – my_cdev->owner = THIS_MODULE; – my_cdev->ops = &my_fops; ● 2nd way initialization: – struct cdev my_cdev; – cdev_init(&my_cdev, &my_fops); – my_cdev.owner = THIS_MODULE; – my_cdev.ops = &my_fops;
  • 26. Registering the file operations... ● The Registration – int cdev_add(struct cdev *cdev, dev_t num, unsigned int count); ● The Unregistration – void cdev_del(struct cdev *cdev);
  • 27. Registering/Unregistering Old Way ● Registering the Device Driver – int register_chrdev(undigned int major, const char *name, struct file_operations *fops); ● Unregistering the Device Driver – int unregister_chrdev(undigned int major, const char *name);
  • 28. The read flow struct file ------------------------- f_count f_flags f_mode ------------------------- f_pos ------------------------- ... ... ssize_t my_read(struct file *f, char __user *buf, size_t cnt, loff_t *off) Buffer (in the driver) Buffer (in the application or libc) Kernel Space (Non-swappable) User Space (Swappable) copy_to_user
  • 29. The /dev/null read & write ssize_t my_read(struct file *f, char __user *buf, size_t cnt, loff_t *off) { ... return read_cnt; } ssize_t my_write(struct file *f, char __user *buf, size_t cnt, loff_t *off) { ... return wrote_cnt; }
  • 30. The mem device read ssize_t my_read(struct file *f, char __user *buf, size_t cnt, loff_t *off) { ... if (copy_to_user(buf, from, cnt) != 0) { return -EFAULT; } ... return read_cnt; }
  • 31. The mem device write ssize_t my_write(struct file *f, char __user *buf, size_t cnt, loff_t *off) { ... if (copy_from_user(to, buf, cnt) != 0) { return -EFAULT; } ... return wrote_cnt; }
  • 32. Dynamic Device Node & Classes ● Class Operations – struct class *class_create(struct module *owner, char *name); – void class_destroy(struct class *cl); ● Device into & Out of Class – struct class_device *device_create(struct class *cl, NULL, dev_t devnum, NULL, const char *fmt, ...); – void device_destroy(struct class *cl, dev_t devnum);
  • 33. The I/O Control API ● int (*ioctl)(struct inode *, struct file *, unsigned int cmd, unsigned long arg) ● int (*unlocked_ioctl)(struct file *, unsigned int cmd, unsigned long arg) ● Command – <linux/ioctl.h> -> ... -> <asm-generic/ioctl.h> – Macros ● _IO, _IOR, _IOW, _IOWR – Parameters ● type (Magic character) [15:8] ● number (index) [7:0] ● size (param type) [29:16]
  • 34. The I/O Control API ● Macro Usage _IO(type, index) [_IOR | _IOW | _IOWR](type, index, datatype/size)
  • 35. Module Parameters ● <linux/moduleparam.h> – Macros ● module_param(name, type, perm) ● module_param_array(name, type, num, perm) ● Perm (is a bitmask) – 0 – S_IRUGO – S_IWUSR | S_IRUGO – Loading ● insmod driver.ko name=10
  • 38. Physical Vs Virtual Memory ● The kernel Organizes Physical memory in to pages – Page size Depends on Arch ● X86-based 4096 bytes ● On 32-bit X86 system Kernel total Virtual address space – Total 4GB (pointer size) – Kernel Configuration Splits 4GB in to ● 3BG Virtual Sp for US ● 1GB Virtual Sp for Kernel – 128MB KDS – Virtual Address also called “Logical Address”
  • 39. Memory Access from Kernel Space ● Virtual Address on Physical Address – #include <linux/gfp.h> ● unsigned long __get_free_pages(flags, order); etc ● void free_pages(addr, order); etc – #include <linux/slab.h> ● void *kmalloc(size_t size, gfp_t flags); – GFP_ATOMIC, GFP_KERNEL, GFP_DMA ● void kfree(void *obj); – #include <linux/vmalloc.h> ● void *vmalloc(unsigned long size); ● void vfree(void *addr);
  • 40. Memory Access from Kernel Space... ● Virtual Address for Bus/IO Address – #include <asm/io.h> ● void *ioremap(unsigned long offset, unsigned long size); ● void iounmap(void *addr); ● I/O Memory Access – #include <asm/io.h> ● unsigned int ioread[8|16|32](void *addr); ● unsigned int iowrite[8|16|32](u[8|16|32] value, void *addr); ● Barriers – #include <linux/kernel.h>: void barrier(void); – #include <asm/system.h>: void [r|w|]mb(void);
  • 42. I/O Accesses from Kernel Space ● I/O Port Access – #include <asm/io.h> ● unsigned in[b|w|l](unsigned port); ● void out[b|w|l](unsigned [char|short|int] value, unsigned port);