SlideShare a Scribd company logo
Note: The Copyrights of the referenced materials and photos go to their original authors. As a result, this slide is for personal reference only.
For the contents created in this document, the Copyright belongs to William W.-Y. Liang. © 2005-2016 All Rights Reserved.
NTU CSIE Open Source System Software
2016.03.22
An Introduction to the Linux Kernel
with Device Drivers
William W.-Y. Liang (梁文耀), Ph. D.
http://www.ntut.edu.tw/~wyliang
for 台大資工系開源系統軟體課程
hosted by Prof. Shih-Hao Hung
© 2016 William W.-Y. Liang, All Rights Reserved.
General-purpose Operating Systems
Characteristics
Development of applications can be logically separated from hardware
Complete software stacks and middleware can be created to support easy
development of complex and versatile applications.
Example:
Linux (with many Distributions), Android, Chrome OS, Firefox OS,
Brillo, uClinux, MacOS, iOS, Windows, etc.
Application development
System independent application development: General applications,
GUI/Window programming, Web programming
System dependent software development: System programming, Device
drivers, Protocol software
2 NTU OSSSP 2016: Linux Kernel
© 2016 William W.-Y. Liang, All Rights Reserved.
The Linux Operating System
Open source and free
Maintained by numerous volunteer experts and hackers
Robust, efficient, and reliable
Modular, configurable, and scalable
Tons of applications available
Suitable for application types from low-end embedded systems, mid-range consumer
products, personal computer, and up to the high performance supercomputers
3 NTU OSSSP 2016: Linux Kernel
https://en.wikipedia.org/wiki/Linux_kernel
© 2016 William W.-Y. Liang, All Rights Reserved.
Some References
Linux Kernel Wiki
https://en.wikipedia.org/wiki/Linux_kernel
Linux Kernel Source
git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git
Reference books for Beginners
Linux Device Driver, 3rd Edition, by Jonathan Corbet, Alessandro
Rubini and Greg Kroah-Hartman, O'Reilly (Also available online
https://lwn.net/Kernel/LDD3/)
Linux Kernel in a NutShell, by Greg Kroah-Hartman, O'Reilly (Also
available online http://www.kroah.com/lkn/)
Understanding the Linux Kernel, 3rd Edition, by Daniel P. Bovet, Marco
Cesati, O'Reilly
4 NTU OSSSP 2016: Linux Kernel
© 2016 William W.-Y. Liang, All Rights Reserved.
Chitchat about Linux and the Kernel
5 NTU OSSSP 2016: Linux Kernel
* Taiwanese slang :^)
© 2016 William W.-Y. Liang, All Rights Reserved.
A Systems View -- from Hardware to Software
Hardware
Software development environment
Tool chain and Library
Boot loader
OS Kernel / RTOS
Middleware*
Applications
6 NTU OSSSP 2016: Linux Kernel
* Example: In Android, the
middleware is called the
Application Framework.
Source: Qing Li and Caroline Yao, “real-time concepts for embedded systems”
Embedded System Development Environment Example
© 2016 William W.-Y. Liang, All Rights Reserved.
The Episode: System Booting
Source: Qing Li and Caroline Yao, “real-time concepts for embedded systems”
7 NTU OSSSP 2016: Linux Kernel
© 2016 William W.-Y. Liang, All Rights Reserved.
Boot Sequence Example
8 NTU OSSSP 2016: Linux Kernel
http://www.at91.com/linux4sam/bin/view/Linux4SAM/GettingStarted
© 2016 William W.-Y. Liang, All Rights Reserved.
Booting the Kernel – Before the C code
9 NTU OSSSP 2016: Linux Kernel
© 2016 William W.-Y. Liang, All Rights Reserved.
Booting the Kernel – Entering the C World
10 NTU OSSSP 2016: Linux Kernel
© 2016 William W.-Y. Liang, All Rights Reserved.
Prepare for Tracing the Kernel
Tools, always the most important thing
git, repo, vim, ctags, grep, find, etc.
Basics for understanding the Kernel
Data structures
11 NTU OSSSP 2016: Linux Kernel
© 2016 William W.-Y. Liang, All Rights Reserved.
Introduction to the Linux Kernel
12 NTU OSSSP 2016: Linux Kernel
Let’s try to understand the Linux
kernel by introducing the basics
for the device drivers and the
kernel features, and discussing
on some related issues.
© 2016 William W.-Y. Liang, All Rights Reserved.
Software View from User Program to Hardware
For a general purpose OS
such as Linux
User level (user space)
Coding
Compilation
Linking
Execution
Kernel (kernel space)
Program loading
System calls (API)
Device drivers
Hardware manipulation
Bare metal hardware
13 NTU OSSSP 2016: Linux Kernel
© 2016 William W.-Y. Liang, All Rights Reserved.
Device Drivers in Linux
Device drivers are usually treated as black boxes for
the application developers.
They resemble as a software layer
lying between the applications and
the actual devices.
14 NTU OSSSP 2016: Linux Kernel
http: //www.ni.com/tutorial/3789/en/
© 2016 William W.-Y. Liang, All Rights Reserved.
Device Drivers in Linux Kernel
15 NTU OSSSP 2016: Linux Kernel
ALESSANDRO RUBINI and JONATHAN CORBET, Linux Device Drivers, Second Edition
© 2016 William W.-Y. Liang, All Rights Reserved.
Major Classes of Devices for Linux
Character devices
A character (char) device is one that can be accessed as a stream
of bytes (just like a normal file).
Block devices
A block device is accessed in a unit of fixed-size block.
The device can be accessed randomly.
Network interfaces
Others: File Systems, Protocol Stack, USB, PCI, etc.
Refers to <kernel-source>/Documentation/devices.txt”
16 NTU OSSSP 2016: Linux Kernel
© 2016 William W.-Y. Liang, All Rights Reserved.
Device Files
File Operations vs. Device Accesses
Device Initialization vs. File Open
Device Accesses vs. File Read/Write
Device Control vs. File Control
Device Termination vs. File Close
UNIX/Linux systems implement device files to enable
users to employ familiar commands and functions such
as open, read, write, and close when working with
some peripherals.
17 NTU OSSSP 2016: Linux Kernel
int open( char *pathname, int flags, … );
int read( int fd, void *buf, size_t count );
int write( int fd, void *buf, size_t count );
int ioctl(struct inode *, struct file *, unsigned int, unsigned long );
int close( int fd );
© 2016 William W.-Y. Liang, All Rights Reserved.
How Device File and Driver Modules Work?
18 NTU OSSSP 2016: Linux Kernel
☼ Discussion: taking “Hello, World” as an example
© 2016 William W.-Y. Liang, All Rights Reserved.
Example: Applications using a Device
19 NTU OSSSP 2016: Linux Kernel
© 2016 William W.-Y. Liang, All Rights Reserved.
Device Number
Example
crw-rw-rw- 1 root root 1, 3 Mar 2 2015 null
crw-rw-rw- 1 root root 1, 5 Mar 2 2015 zero
crw------- 1 root root 4, 1 Mar 2 2015 tty1
crw-rw-rw- 1 root tty 4, 64 Mar 2 2015 ttys0
crw-rw---- 1 root uucp 4, 65 Mar 2 2015 ttyS1
crw------- 1 root root 10, 1 Mar 2 2015 psaux
crw-rw-rw- 1 root root 123, 0 Mar 2 2015 androint
A System Administrator may create device files with
the “mknod” command. For example,
mknod /dev/androint c 123 0
‘/dev/androint’ is the file’s pathname, ‘c’ indicates that it’s a
character-mode device, 123 is its (unique) ‘major number’,
and 0 is its ‘minor number’.
20 NTU OSSSP 2016: Linux Kernel
☼ Discussion: relationship between device files and drivers
© 2016 William W.-Y. Liang, All Rights Reserved.
Loadable Kernel Modules
A great mechanism for OS ‘extensibility’
No need to recompile and then reboot
A kernel module differs from a normal C program
A kernel module cannot call any of the familiar
functions from the standard C runtime libraries
A module will be run as part of the kernel
Any ‘bug’ can cause a system malfunction or a
complete crash!
21 NTU OSSSP 2016: Linux Kernel
☼ Discussion: GPL issues with the Linux kernel modules
© 2016 William W.-Y. Liang, All Rights Reserved.
Example
22 NTU OSSSP 2016: Linux Kernel
☼ Discussion: how multiple program entries work?
© 2016 William W.-Y. Liang, All Rights Reserved.
How to get a module to work in Kernel
23 NTU OSSSP 2016: Linux Kernel
ALESSANDRO RUBINI and JONATHAN CORBET, Linux Device Drivers, Second Edition
Driver Functions
© 2016 William W.-Y. Liang, All Rights Reserved.
Example: Device Registration
24 NTU OSSSP 2016: Linux Kernel
© 2016 William W.-Y. Liang, All Rights Reserved.
Driver Methods -- File Operations
The kernel uses the file_operations structure to access the
driver’s methods.
int (*open) (struct inode *, struct file *);
ssize_t (*read) (struct file *, char *, size_t, loff_t *);
ssize_t (*write) (struct file *, const char *, size_t, loff_t *);
int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned
long);
int (*release) (struct inode *, struct file *);
…
25 NTU OSSSP 2016: Linux Kernel
☼ Discussion: drivers vs. the object-orient concept
© 2016 William W.-Y. Liang, All Rights Reserved.
Kernel and User Space
26 NTU OSSSP 2016: Linux Kernel
☼ Discussion: address space for kernel and processes
© 2016 William W.-Y. Liang, All Rights Reserved.
Virtual to Physical Space
27 NTU OSSSP 2016: Linux Kernel
https://en.wikipedia.org/wiki/Operating_system
☼ Discussion: how the memory space is utilized?
ALESSANDRO RUBINI and JONATHAN CORBET, Linux Device Drivers, Second Edition
© 2016 William W.-Y. Liang, All Rights Reserved.
Data Transfer between User and Kernel Space
28 NTU OSSSP 2016: Linux Kernel
Read: kernel->user
Write: user->kernel
☼ Discussion: issues for cross space data accesses
ALESSANDRO RUBINI and JONATHAN CORBET, Linux Device Drivers, Second Edition ,
© 2016 William W.-Y. Liang, All Rights Reserved.
Example: User & Kernel Space Data Transfer
29 NTU OSSSP 2016: Linux Kernel
© 2016 William W.-Y. Liang, All Rights Reserved.
Kernel Memory Allocation
Small size allocation
kmalloc and kfree
dptr->data = kmalloc(qset * sizeof(char *), GFP_KERNEL);
memset(dptr->data, 0, qset * sizeof(char *));
Limitations
Large size allocation
vmalloc and vfree
Restrictions
Page allocation
Slab and memory pool
30 NTU OSSSP 2016: Linux Kernel
☼ Discussion: performance issues in memory management
© 2016 William W.-Y. Liang, All Rights Reserved.
Multi-tasking: the Central Part of the OS
Process states in OSes
Running: own CPU
Ready: To be scheduled,
in ready queue (run-queue)
Waiting:
Pending on I/O or Event
Delay itself for some duration
Request a resource but is not yet available
Linux Task States
Tasks: user-process, user-thread, kernel thread
States: TASK_RUNNING, TASK_INTERRUPTIBLE, etc.
Scheduling Policy
SCHED_OTHER, SCHED_FIFO, SCHED_RR, etc
31 NTU OSSSP 2016: Linux Kernel
☼ Discussion: scheduling and preemption issues
© 2016 William W.-Y. Liang, All Rights Reserved.
Concurrency Problems
Problems: Data inconsistency, Deadlock
Race Condition: Occurs when multiple tasks want to
access the shared resources, such as shared data or
hardware devices.
32 NTU OSSSP 2016: Linux Kernel
☼ Discussion: what’s the exact cause?
© 2016 William W.-Y. Liang, All Rights Reserved.
Thread Safe
Potential Causes of Thread-Unsafe Problems
Global variables
Static variables
Indirect accesses
Solutions
Reentrancy
Atomic operations
Thread-local storage
Mutual exclusion
33 NTU OSSSP 2016: Linux Kernel
Concurrency in Kernel and Device Drivers
 Driver code vs. Tasks
 Single Core vs. Multicore
 IRQ impact
 Soft-IRQ
☼ Discussion: performance issues for synchronizations
© 2016 William W.-Y. Liang, All Rights Reserved.
Synchronization Operations
Semaphore
down(), up(), etc.
Mutex
mutex_lock(), mutex_unlock(), etc.
Spin locks
spin_lock(), spin_unlock(), etc.
Atomic operations
atomic_set(), atomic_add(), etc
Bit operations
set_bit(), clear_bit(), etc.
34 NTU OSSSP 2016: Linux Kernel
☼ Discussion: semaphore vs. spinlock, usage & performance
© 2016 William W.-Y. Liang, All Rights Reserved.
Interrupt Handling
35 NTU OSSSP 2016: Linux Kernel
☼ Discussion: cost and performance issues
of IRQ handling and solutions
© 2016 William W.-Y. Liang, All Rights Reserved.
Task Blocking and Waiting Queue
Processes are usually blocked due to I/O waiting.
No data available for read, or no space
available for write or device busy
Waiting queues
Blocked task is put into a waiting queue
before it is woke up.
36 NTU OSSSP 2016: Linux Kernel Linux Device Drivers, Second Edition
☼ Discussion: how it works?
Linux Device Drivers, Second Edition
© 2016 William W.-Y. Liang, All Rights Reserved.
The Endless Journey on Discovering Linux
To be Continued…
This brief introductory lecture is just a beginning.
Still many interesting things out there
Take it as interesting adventure, by learning from the
wonderful open source world!
37 NTU OSSSP 2016: Linux Kernel
https://en.wikipedia.org/wiki/Linux_kernel#/media/File:Linux_kernel_map.png
Let’s keep the discussion next week
on the next topic – Android Framework.
Note: The Copyrights of the referenced materials and photos go to their original authors. As a result, this slide is for personal reference only.
For the contents created in this document, the Copyright belongs to William W.-Y. Liang. © 2005-2016 All Rights Reserved.
Q & A
william.wyliang@gmail.com
http://www.ntut.edu.tw/~wyliang
http://www.facebook.com/william.wyliang

More Related Content

What's hot

Linux System Programming - File I/O
Linux System Programming - File I/O Linux System Programming - File I/O
Linux System Programming - File I/O
YourHelper1
 
Linux device drivers
Linux device driversLinux device drivers
Linux device drivers
Abhishek Sagar
 
Uboot startup sequence
Uboot startup sequenceUboot startup sequence
Uboot startup sequenceHoucheng Lin
 
Linux Initialization Process (2)
Linux Initialization Process (2)Linux Initialization Process (2)
Linux Initialization Process (2)
shimosawa
 
U Boot or Universal Bootloader
U Boot or Universal BootloaderU Boot or Universal Bootloader
U Boot or Universal Bootloader
Satpal Parmar
 
Linux Initialization Process (1)
Linux Initialization Process (1)Linux Initialization Process (1)
Linux Initialization Process (1)
shimosawa
 
Linux boot process
Linux boot processLinux boot process
Linux boot process
Archana Chandrasekharan
 
Bootstrap process of u boot (NDS32 RISC CPU)
Bootstrap process of u boot (NDS32 RISC CPU)Bootstrap process of u boot (NDS32 RISC CPU)
Bootstrap process of u boot (NDS32 RISC CPU)
Macpaul Lin
 
U-Boot presentation 2013
U-Boot presentation  2013U-Boot presentation  2013
U-Boot presentation 2013
Wave Digitech
 
Linux device drivers
Linux device drivers Linux device drivers
U-Boot Porting on New Hardware
U-Boot Porting on New HardwareU-Boot Porting on New Hardware
U-Boot Porting on New Hardware
RuggedBoardGroup
 
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
Emertxe Information Technologies Pvt Ltd
 
Linux Kernel Image
Linux Kernel ImageLinux Kernel Image
Linux Kernel Image艾鍗科技
 
Character drivers
Character driversCharacter drivers
Character drivers
pradeep_tewani
 
Linux MMAP & Ioremap introduction
Linux MMAP & Ioremap introductionLinux MMAP & Ioremap introduction
Linux MMAP & Ioremap introduction
Gene Chang
 
Lesson 2 Understanding Linux File System
Lesson 2 Understanding Linux File SystemLesson 2 Understanding Linux File System
Lesson 2 Understanding Linux File System
Sadia Bashir
 
Linux booting process!!
Linux booting process!!Linux booting process!!
Linux booting process!!
sourav verma
 
Bootloaders
BootloadersBootloaders
Bootloaders
Anil Kumar Pugalia
 

What's hot (20)

Linux System Programming - File I/O
Linux System Programming - File I/O Linux System Programming - File I/O
Linux System Programming - File I/O
 
Linux device drivers
Linux device driversLinux device drivers
Linux device drivers
 
Uboot startup sequence
Uboot startup sequenceUboot startup sequence
Uboot startup sequence
 
Linux Initialization Process (2)
Linux Initialization Process (2)Linux Initialization Process (2)
Linux Initialization Process (2)
 
U Boot or Universal Bootloader
U Boot or Universal BootloaderU Boot or Universal Bootloader
U Boot or Universal Bootloader
 
Linux Initialization Process (1)
Linux Initialization Process (1)Linux Initialization Process (1)
Linux Initialization Process (1)
 
Linux boot process
Linux boot processLinux boot process
Linux boot process
 
Bootstrap process of u boot (NDS32 RISC CPU)
Bootstrap process of u boot (NDS32 RISC CPU)Bootstrap process of u boot (NDS32 RISC CPU)
Bootstrap process of u boot (NDS32 RISC CPU)
 
U-Boot presentation 2013
U-Boot presentation  2013U-Boot presentation  2013
U-Boot presentation 2013
 
Linux device drivers
Linux device drivers Linux device drivers
Linux device drivers
 
U-Boot Porting on New Hardware
U-Boot Porting on New HardwareU-Boot Porting on New Hardware
U-Boot Porting on New Hardware
 
Block Drivers
Block DriversBlock Drivers
Block Drivers
 
Embedded Linux Kernel - Build your custom kernel
Embedded Linux Kernel - Build your custom kernelEmbedded Linux Kernel - Build your custom kernel
Embedded Linux Kernel - Build your custom kernel
 
Linux Kernel Image
Linux Kernel ImageLinux Kernel Image
Linux Kernel Image
 
Character Drivers
Character DriversCharacter Drivers
Character Drivers
 
Character drivers
Character driversCharacter drivers
Character drivers
 
Linux MMAP & Ioremap introduction
Linux MMAP & Ioremap introductionLinux MMAP & Ioremap introduction
Linux MMAP & Ioremap introduction
 
Lesson 2 Understanding Linux File System
Lesson 2 Understanding Linux File SystemLesson 2 Understanding Linux File System
Lesson 2 Understanding Linux File System
 
Linux booting process!!
Linux booting process!!Linux booting process!!
Linux booting process!!
 
Bootloaders
BootloadersBootloaders
Bootloaders
 

Similar to An introduction to the linux kernel and device drivers (NTU CSIE 2016.03)

Android device driver structure introduction
Android device driver structure introductionAndroid device driver structure introduction
Android device driver structure introduction
William Liang
 
Comparison of Windows and Linux Operating Systems in Advanced Features
Comparison of Windows and Linux Operating Systems in Advanced FeaturesComparison of Windows and Linux Operating Systems in Advanced Features
Comparison of Windows and Linux Operating Systems in Advanced Features
IJERA Editor
 
Linux
Linux Linux
Linux
Teja Babu
 
Linux Introduction
Linux IntroductionLinux Introduction
Linux Introduction
Ankita Tiwari
 
Group project linux helix
Group project linux helixGroup project linux helix
Group project linux helix
Jeff Carroll
 
linux system and network administrations
linux system and network administrationslinux system and network administrations
linux system and network administrations
haile468688
 
Linux introduction (eng)
Linux introduction (eng)Linux introduction (eng)
Linux introduction (eng)
Anatoliy Okhotnikov
 
TDC2016SP - Trilha Linux Embarcado
TDC2016SP - Trilha Linux EmbarcadoTDC2016SP - Trilha Linux Embarcado
TDC2016SP - Trilha Linux Embarcado
tdc-globalcode
 
Linux technology
Linux technologyLinux technology
Linux technology
Prakash Poudel
 
Presentation on linux
Presentation on linuxPresentation on linux
Presentation on linux
Veeral Bhateja
 
UNIT I LINUX.docx
UNIT I LINUX.docxUNIT I LINUX.docx
UNIT I LINUX.docx
BhuvanaR13
 
An Introduction to the Android Framework -- a core architecture view from app...
An Introduction to the Android Framework -- a core architecture view from app...An Introduction to the Android Framework -- a core architecture view from app...
An Introduction to the Android Framework -- a core architecture view from app...
William Liang
 
I Am Linux-Introductory Module on Linux
I Am Linux-Introductory Module on LinuxI Am Linux-Introductory Module on Linux
I Am Linux-Introductory Module on Linux
Sagar Kumar
 
3CS LSP UNIT 1-1.pdf
3CS LSP UNIT 1-1.pdf3CS LSP UNIT 1-1.pdf
3CS LSP UNIT 1-1.pdf
DeepakKumar783815
 
Introduction to System Programming
Introduction to System ProgrammingIntroduction to System Programming
Introduction to System Programming
Sayed Chhattan Shah
 
OS Lab: Introduction to Linux
OS Lab: Introduction to LinuxOS Lab: Introduction to Linux
OS Lab: Introduction to Linux
Motaz Saad
 
Deft v7
Deft v7Deft v7
Deft v7
TGodfrey
 
Linux nic training_intro_14_dec_09
Linux nic training_intro_14_dec_09Linux nic training_intro_14_dec_09
Linux nic training_intro_14_dec_09Aravindan Arun
 

Similar to An introduction to the linux kernel and device drivers (NTU CSIE 2016.03) (20)

Android device driver structure introduction
Android device driver structure introductionAndroid device driver structure introduction
Android device driver structure introduction
 
Comparison of Windows and Linux Operating Systems in Advanced Features
Comparison of Windows and Linux Operating Systems in Advanced FeaturesComparison of Windows and Linux Operating Systems in Advanced Features
Comparison of Windows and Linux Operating Systems in Advanced Features
 
Linux
Linux Linux
Linux
 
Linux Introduction
Linux IntroductionLinux Introduction
Linux Introduction
 
Group project linux helix
Group project linux helixGroup project linux helix
Group project linux helix
 
linux system and network administrations
linux system and network administrationslinux system and network administrations
linux system and network administrations
 
Before begining linux
Before begining linuxBefore begining linux
Before begining linux
 
Linux introduction (eng)
Linux introduction (eng)Linux introduction (eng)
Linux introduction (eng)
 
TDC2016SP - Trilha Linux Embarcado
TDC2016SP - Trilha Linux EmbarcadoTDC2016SP - Trilha Linux Embarcado
TDC2016SP - Trilha Linux Embarcado
 
Linux technology
Linux technologyLinux technology
Linux technology
 
Group 3
Group 3Group 3
Group 3
 
Presentation on linux
Presentation on linuxPresentation on linux
Presentation on linux
 
UNIT I LINUX.docx
UNIT I LINUX.docxUNIT I LINUX.docx
UNIT I LINUX.docx
 
An Introduction to the Android Framework -- a core architecture view from app...
An Introduction to the Android Framework -- a core architecture view from app...An Introduction to the Android Framework -- a core architecture view from app...
An Introduction to the Android Framework -- a core architecture view from app...
 
I Am Linux-Introductory Module on Linux
I Am Linux-Introductory Module on LinuxI Am Linux-Introductory Module on Linux
I Am Linux-Introductory Module on Linux
 
3CS LSP UNIT 1-1.pdf
3CS LSP UNIT 1-1.pdf3CS LSP UNIT 1-1.pdf
3CS LSP UNIT 1-1.pdf
 
Introduction to System Programming
Introduction to System ProgrammingIntroduction to System Programming
Introduction to System Programming
 
OS Lab: Introduction to Linux
OS Lab: Introduction to LinuxOS Lab: Introduction to Linux
OS Lab: Introduction to Linux
 
Deft v7
Deft v7Deft v7
Deft v7
 
Linux nic training_intro_14_dec_09
Linux nic training_intro_14_dec_09Linux nic training_intro_14_dec_09
Linux nic training_intro_14_dec_09
 

More from William Liang

Internet of energy
Internet of energyInternet of energy
Internet of energy
William Liang
 
Edge Computing for the Industry
Edge Computing for the IndustryEdge Computing for the Industry
Edge Computing for the Industry
William Liang
 
From Embedded to IoT and From Cloud to Edge & AIoT -- A computer technology t...
From Embedded to IoT and From Cloud to Edge & AIoT -- A computer technology t...From Embedded to IoT and From Cloud to Edge & AIoT -- A computer technology t...
From Embedded to IoT and From Cloud to Edge & AIoT -- A computer technology t...
William Liang
 
The development trends of smart applications and open source system software ...
The development trends of smart applications and open source system software ...The development trends of smart applications and open source system software ...
The development trends of smart applications and open source system software ...
William Liang
 
The key issues for teaching or learning Android and Linux Kernel
The key issues for teaching or learning Android and Linux KernelThe key issues for teaching or learning Android and Linux Kernel
The key issues for teaching or learning Android and Linux Kernel
William Liang
 
Data-centric IoT (NTU CSIE 2016.12)
Data-centric IoT (NTU CSIE 2016.12)Data-centric IoT (NTU CSIE 2016.12)
Data-centric IoT (NTU CSIE 2016.12)
William Liang
 
行動終端、物聯網與雲端平台在高速移動網路下的整合趨勢 (for MOPCON 2016)
行動終端、物聯網與雲端平台在高速移動網路下的整合趨勢 (for MOPCON 2016)行動終端、物聯網與雲端平台在高速移動網路下的整合趨勢 (for MOPCON 2016)
行動終端、物聯網與雲端平台在高速移動網路下的整合趨勢 (for MOPCON 2016)
William Liang
 
高速移動網路新時代 - 雲端與物聯網發展新趨勢 (An Integration Trend of Terminal Devices, IoT and C...
高速移動網路新時代  - 雲端與物聯網發展新趨勢 (An Integration Trend of Terminal Devices, IoT and C...高速移動網路新時代  - 雲端與物聯網發展新趨勢 (An Integration Trend of Terminal Devices, IoT and C...
高速移動網路新時代 - 雲端與物聯網發展新趨勢 (An Integration Trend of Terminal Devices, IoT and C...
William Liang
 
作業系統與硬體元件的驅動軟體開發法則 (Operating Systems and Software Design Principles for Har...
作業系統與硬體元件的驅動軟體開發法則 (Operating Systems and Software Design Principles for Har...作業系統與硬體元件的驅動軟體開發法則 (Operating Systems and Software Design Principles for Har...
作業系統與硬體元件的驅動軟體開發法則 (Operating Systems and Software Design Principles for Har...
William Liang
 
智慧應用與物聯網發展趨勢 (A Development Trend of Smart Applications and IoT)
智慧應用與物聯網發展趨勢 (A Development Trend of Smart Applications and IoT)智慧應用與物聯網發展趨勢 (A Development Trend of Smart Applications and IoT)
智慧應用與物聯網發展趨勢 (A Development Trend of Smart Applications and IoT)
William Liang
 
嵌入式智慧應用開源軟硬整合新趨勢 (Open Source Software and Hardware Integration Trend for Emb...
嵌入式智慧應用開源軟硬整合新趨勢 (Open Source Software and Hardware Integration Trend for Emb...嵌入式智慧應用開源軟硬整合新趨勢 (Open Source Software and Hardware Integration Trend for Emb...
嵌入式智慧應用開源軟硬整合新趨勢 (Open Source Software and Hardware Integration Trend for Emb...
William Liang
 

More from William Liang (11)

Internet of energy
Internet of energyInternet of energy
Internet of energy
 
Edge Computing for the Industry
Edge Computing for the IndustryEdge Computing for the Industry
Edge Computing for the Industry
 
From Embedded to IoT and From Cloud to Edge & AIoT -- A computer technology t...
From Embedded to IoT and From Cloud to Edge & AIoT -- A computer technology t...From Embedded to IoT and From Cloud to Edge & AIoT -- A computer technology t...
From Embedded to IoT and From Cloud to Edge & AIoT -- A computer technology t...
 
The development trends of smart applications and open source system software ...
The development trends of smart applications and open source system software ...The development trends of smart applications and open source system software ...
The development trends of smart applications and open source system software ...
 
The key issues for teaching or learning Android and Linux Kernel
The key issues for teaching or learning Android and Linux KernelThe key issues for teaching or learning Android and Linux Kernel
The key issues for teaching or learning Android and Linux Kernel
 
Data-centric IoT (NTU CSIE 2016.12)
Data-centric IoT (NTU CSIE 2016.12)Data-centric IoT (NTU CSIE 2016.12)
Data-centric IoT (NTU CSIE 2016.12)
 
行動終端、物聯網與雲端平台在高速移動網路下的整合趨勢 (for MOPCON 2016)
行動終端、物聯網與雲端平台在高速移動網路下的整合趨勢 (for MOPCON 2016)行動終端、物聯網與雲端平台在高速移動網路下的整合趨勢 (for MOPCON 2016)
行動終端、物聯網與雲端平台在高速移動網路下的整合趨勢 (for MOPCON 2016)
 
高速移動網路新時代 - 雲端與物聯網發展新趨勢 (An Integration Trend of Terminal Devices, IoT and C...
高速移動網路新時代  - 雲端與物聯網發展新趨勢 (An Integration Trend of Terminal Devices, IoT and C...高速移動網路新時代  - 雲端與物聯網發展新趨勢 (An Integration Trend of Terminal Devices, IoT and C...
高速移動網路新時代 - 雲端與物聯網發展新趨勢 (An Integration Trend of Terminal Devices, IoT and C...
 
作業系統與硬體元件的驅動軟體開發法則 (Operating Systems and Software Design Principles for Har...
作業系統與硬體元件的驅動軟體開發法則 (Operating Systems and Software Design Principles for Har...作業系統與硬體元件的驅動軟體開發法則 (Operating Systems and Software Design Principles for Har...
作業系統與硬體元件的驅動軟體開發法則 (Operating Systems and Software Design Principles for Har...
 
智慧應用與物聯網發展趨勢 (A Development Trend of Smart Applications and IoT)
智慧應用與物聯網發展趨勢 (A Development Trend of Smart Applications and IoT)智慧應用與物聯網發展趨勢 (A Development Trend of Smart Applications and IoT)
智慧應用與物聯網發展趨勢 (A Development Trend of Smart Applications and IoT)
 
嵌入式智慧應用開源軟硬整合新趨勢 (Open Source Software and Hardware Integration Trend for Emb...
嵌入式智慧應用開源軟硬整合新趨勢 (Open Source Software and Hardware Integration Trend for Emb...嵌入式智慧應用開源軟硬整合新趨勢 (Open Source Software and Hardware Integration Trend for Emb...
嵌入式智慧應用開源軟硬整合新趨勢 (Open Source Software and Hardware Integration Trend for Emb...
 

Recently uploaded

OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
takuyayamamoto1800
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
Matt Welsh
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
Tier1 app
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdfEnhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Jay Das
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Anthony Dahanne
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Globus
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Globus
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
AMB-Review
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
Juraj Vysvader
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
Globus
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Mind IT Systems
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
WSO2
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Globus
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
Ortus Solutions, Corp
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
Ortus Solutions, Corp
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Shahin Sheidaei
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
Georgi Kodinov
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
Philip Schwarz
 

Recently uploaded (20)

OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdfEnhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
 

An introduction to the linux kernel and device drivers (NTU CSIE 2016.03)

  • 1. Note: The Copyrights of the referenced materials and photos go to their original authors. As a result, this slide is for personal reference only. For the contents created in this document, the Copyright belongs to William W.-Y. Liang. © 2005-2016 All Rights Reserved. NTU CSIE Open Source System Software 2016.03.22 An Introduction to the Linux Kernel with Device Drivers William W.-Y. Liang (梁文耀), Ph. D. http://www.ntut.edu.tw/~wyliang for 台大資工系開源系統軟體課程 hosted by Prof. Shih-Hao Hung
  • 2. © 2016 William W.-Y. Liang, All Rights Reserved. General-purpose Operating Systems Characteristics Development of applications can be logically separated from hardware Complete software stacks and middleware can be created to support easy development of complex and versatile applications. Example: Linux (with many Distributions), Android, Chrome OS, Firefox OS, Brillo, uClinux, MacOS, iOS, Windows, etc. Application development System independent application development: General applications, GUI/Window programming, Web programming System dependent software development: System programming, Device drivers, Protocol software 2 NTU OSSSP 2016: Linux Kernel
  • 3. © 2016 William W.-Y. Liang, All Rights Reserved. The Linux Operating System Open source and free Maintained by numerous volunteer experts and hackers Robust, efficient, and reliable Modular, configurable, and scalable Tons of applications available Suitable for application types from low-end embedded systems, mid-range consumer products, personal computer, and up to the high performance supercomputers 3 NTU OSSSP 2016: Linux Kernel https://en.wikipedia.org/wiki/Linux_kernel
  • 4. © 2016 William W.-Y. Liang, All Rights Reserved. Some References Linux Kernel Wiki https://en.wikipedia.org/wiki/Linux_kernel Linux Kernel Source git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git Reference books for Beginners Linux Device Driver, 3rd Edition, by Jonathan Corbet, Alessandro Rubini and Greg Kroah-Hartman, O'Reilly (Also available online https://lwn.net/Kernel/LDD3/) Linux Kernel in a NutShell, by Greg Kroah-Hartman, O'Reilly (Also available online http://www.kroah.com/lkn/) Understanding the Linux Kernel, 3rd Edition, by Daniel P. Bovet, Marco Cesati, O'Reilly 4 NTU OSSSP 2016: Linux Kernel
  • 5. © 2016 William W.-Y. Liang, All Rights Reserved. Chitchat about Linux and the Kernel 5 NTU OSSSP 2016: Linux Kernel * Taiwanese slang :^)
  • 6. © 2016 William W.-Y. Liang, All Rights Reserved. A Systems View -- from Hardware to Software Hardware Software development environment Tool chain and Library Boot loader OS Kernel / RTOS Middleware* Applications 6 NTU OSSSP 2016: Linux Kernel * Example: In Android, the middleware is called the Application Framework. Source: Qing Li and Caroline Yao, “real-time concepts for embedded systems” Embedded System Development Environment Example
  • 7. © 2016 William W.-Y. Liang, All Rights Reserved. The Episode: System Booting Source: Qing Li and Caroline Yao, “real-time concepts for embedded systems” 7 NTU OSSSP 2016: Linux Kernel
  • 8. © 2016 William W.-Y. Liang, All Rights Reserved. Boot Sequence Example 8 NTU OSSSP 2016: Linux Kernel http://www.at91.com/linux4sam/bin/view/Linux4SAM/GettingStarted
  • 9. © 2016 William W.-Y. Liang, All Rights Reserved. Booting the Kernel – Before the C code 9 NTU OSSSP 2016: Linux Kernel
  • 10. © 2016 William W.-Y. Liang, All Rights Reserved. Booting the Kernel – Entering the C World 10 NTU OSSSP 2016: Linux Kernel
  • 11. © 2016 William W.-Y. Liang, All Rights Reserved. Prepare for Tracing the Kernel Tools, always the most important thing git, repo, vim, ctags, grep, find, etc. Basics for understanding the Kernel Data structures 11 NTU OSSSP 2016: Linux Kernel
  • 12. © 2016 William W.-Y. Liang, All Rights Reserved. Introduction to the Linux Kernel 12 NTU OSSSP 2016: Linux Kernel Let’s try to understand the Linux kernel by introducing the basics for the device drivers and the kernel features, and discussing on some related issues.
  • 13. © 2016 William W.-Y. Liang, All Rights Reserved. Software View from User Program to Hardware For a general purpose OS such as Linux User level (user space) Coding Compilation Linking Execution Kernel (kernel space) Program loading System calls (API) Device drivers Hardware manipulation Bare metal hardware 13 NTU OSSSP 2016: Linux Kernel
  • 14. © 2016 William W.-Y. Liang, All Rights Reserved. Device Drivers in Linux Device drivers are usually treated as black boxes for the application developers. They resemble as a software layer lying between the applications and the actual devices. 14 NTU OSSSP 2016: Linux Kernel http: //www.ni.com/tutorial/3789/en/
  • 15. © 2016 William W.-Y. Liang, All Rights Reserved. Device Drivers in Linux Kernel 15 NTU OSSSP 2016: Linux Kernel ALESSANDRO RUBINI and JONATHAN CORBET, Linux Device Drivers, Second Edition
  • 16. © 2016 William W.-Y. Liang, All Rights Reserved. Major Classes of Devices for Linux Character devices A character (char) device is one that can be accessed as a stream of bytes (just like a normal file). Block devices A block device is accessed in a unit of fixed-size block. The device can be accessed randomly. Network interfaces Others: File Systems, Protocol Stack, USB, PCI, etc. Refers to <kernel-source>/Documentation/devices.txt” 16 NTU OSSSP 2016: Linux Kernel
  • 17. © 2016 William W.-Y. Liang, All Rights Reserved. Device Files File Operations vs. Device Accesses Device Initialization vs. File Open Device Accesses vs. File Read/Write Device Control vs. File Control Device Termination vs. File Close UNIX/Linux systems implement device files to enable users to employ familiar commands and functions such as open, read, write, and close when working with some peripherals. 17 NTU OSSSP 2016: Linux Kernel int open( char *pathname, int flags, … ); int read( int fd, void *buf, size_t count ); int write( int fd, void *buf, size_t count ); int ioctl(struct inode *, struct file *, unsigned int, unsigned long ); int close( int fd );
  • 18. © 2016 William W.-Y. Liang, All Rights Reserved. How Device File and Driver Modules Work? 18 NTU OSSSP 2016: Linux Kernel ☼ Discussion: taking “Hello, World” as an example
  • 19. © 2016 William W.-Y. Liang, All Rights Reserved. Example: Applications using a Device 19 NTU OSSSP 2016: Linux Kernel
  • 20. © 2016 William W.-Y. Liang, All Rights Reserved. Device Number Example crw-rw-rw- 1 root root 1, 3 Mar 2 2015 null crw-rw-rw- 1 root root 1, 5 Mar 2 2015 zero crw------- 1 root root 4, 1 Mar 2 2015 tty1 crw-rw-rw- 1 root tty 4, 64 Mar 2 2015 ttys0 crw-rw---- 1 root uucp 4, 65 Mar 2 2015 ttyS1 crw------- 1 root root 10, 1 Mar 2 2015 psaux crw-rw-rw- 1 root root 123, 0 Mar 2 2015 androint A System Administrator may create device files with the “mknod” command. For example, mknod /dev/androint c 123 0 ‘/dev/androint’ is the file’s pathname, ‘c’ indicates that it’s a character-mode device, 123 is its (unique) ‘major number’, and 0 is its ‘minor number’. 20 NTU OSSSP 2016: Linux Kernel ☼ Discussion: relationship between device files and drivers
  • 21. © 2016 William W.-Y. Liang, All Rights Reserved. Loadable Kernel Modules A great mechanism for OS ‘extensibility’ No need to recompile and then reboot A kernel module differs from a normal C program A kernel module cannot call any of the familiar functions from the standard C runtime libraries A module will be run as part of the kernel Any ‘bug’ can cause a system malfunction or a complete crash! 21 NTU OSSSP 2016: Linux Kernel ☼ Discussion: GPL issues with the Linux kernel modules
  • 22. © 2016 William W.-Y. Liang, All Rights Reserved. Example 22 NTU OSSSP 2016: Linux Kernel ☼ Discussion: how multiple program entries work?
  • 23. © 2016 William W.-Y. Liang, All Rights Reserved. How to get a module to work in Kernel 23 NTU OSSSP 2016: Linux Kernel ALESSANDRO RUBINI and JONATHAN CORBET, Linux Device Drivers, Second Edition Driver Functions
  • 24. © 2016 William W.-Y. Liang, All Rights Reserved. Example: Device Registration 24 NTU OSSSP 2016: Linux Kernel
  • 25. © 2016 William W.-Y. Liang, All Rights Reserved. Driver Methods -- File Operations The kernel uses the file_operations structure to access the driver’s methods. int (*open) (struct inode *, struct file *); ssize_t (*read) (struct file *, char *, size_t, loff_t *); ssize_t (*write) (struct file *, const char *, size_t, loff_t *); int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long); int (*release) (struct inode *, struct file *); … 25 NTU OSSSP 2016: Linux Kernel ☼ Discussion: drivers vs. the object-orient concept
  • 26. © 2016 William W.-Y. Liang, All Rights Reserved. Kernel and User Space 26 NTU OSSSP 2016: Linux Kernel ☼ Discussion: address space for kernel and processes
  • 27. © 2016 William W.-Y. Liang, All Rights Reserved. Virtual to Physical Space 27 NTU OSSSP 2016: Linux Kernel https://en.wikipedia.org/wiki/Operating_system ☼ Discussion: how the memory space is utilized? ALESSANDRO RUBINI and JONATHAN CORBET, Linux Device Drivers, Second Edition
  • 28. © 2016 William W.-Y. Liang, All Rights Reserved. Data Transfer between User and Kernel Space 28 NTU OSSSP 2016: Linux Kernel Read: kernel->user Write: user->kernel ☼ Discussion: issues for cross space data accesses ALESSANDRO RUBINI and JONATHAN CORBET, Linux Device Drivers, Second Edition ,
  • 29. © 2016 William W.-Y. Liang, All Rights Reserved. Example: User & Kernel Space Data Transfer 29 NTU OSSSP 2016: Linux Kernel
  • 30. © 2016 William W.-Y. Liang, All Rights Reserved. Kernel Memory Allocation Small size allocation kmalloc and kfree dptr->data = kmalloc(qset * sizeof(char *), GFP_KERNEL); memset(dptr->data, 0, qset * sizeof(char *)); Limitations Large size allocation vmalloc and vfree Restrictions Page allocation Slab and memory pool 30 NTU OSSSP 2016: Linux Kernel ☼ Discussion: performance issues in memory management
  • 31. © 2016 William W.-Y. Liang, All Rights Reserved. Multi-tasking: the Central Part of the OS Process states in OSes Running: own CPU Ready: To be scheduled, in ready queue (run-queue) Waiting: Pending on I/O or Event Delay itself for some duration Request a resource but is not yet available Linux Task States Tasks: user-process, user-thread, kernel thread States: TASK_RUNNING, TASK_INTERRUPTIBLE, etc. Scheduling Policy SCHED_OTHER, SCHED_FIFO, SCHED_RR, etc 31 NTU OSSSP 2016: Linux Kernel ☼ Discussion: scheduling and preemption issues
  • 32. © 2016 William W.-Y. Liang, All Rights Reserved. Concurrency Problems Problems: Data inconsistency, Deadlock Race Condition: Occurs when multiple tasks want to access the shared resources, such as shared data or hardware devices. 32 NTU OSSSP 2016: Linux Kernel ☼ Discussion: what’s the exact cause?
  • 33. © 2016 William W.-Y. Liang, All Rights Reserved. Thread Safe Potential Causes of Thread-Unsafe Problems Global variables Static variables Indirect accesses Solutions Reentrancy Atomic operations Thread-local storage Mutual exclusion 33 NTU OSSSP 2016: Linux Kernel Concurrency in Kernel and Device Drivers  Driver code vs. Tasks  Single Core vs. Multicore  IRQ impact  Soft-IRQ ☼ Discussion: performance issues for synchronizations
  • 34. © 2016 William W.-Y. Liang, All Rights Reserved. Synchronization Operations Semaphore down(), up(), etc. Mutex mutex_lock(), mutex_unlock(), etc. Spin locks spin_lock(), spin_unlock(), etc. Atomic operations atomic_set(), atomic_add(), etc Bit operations set_bit(), clear_bit(), etc. 34 NTU OSSSP 2016: Linux Kernel ☼ Discussion: semaphore vs. spinlock, usage & performance
  • 35. © 2016 William W.-Y. Liang, All Rights Reserved. Interrupt Handling 35 NTU OSSSP 2016: Linux Kernel ☼ Discussion: cost and performance issues of IRQ handling and solutions
  • 36. © 2016 William W.-Y. Liang, All Rights Reserved. Task Blocking and Waiting Queue Processes are usually blocked due to I/O waiting. No data available for read, or no space available for write or device busy Waiting queues Blocked task is put into a waiting queue before it is woke up. 36 NTU OSSSP 2016: Linux Kernel Linux Device Drivers, Second Edition ☼ Discussion: how it works? Linux Device Drivers, Second Edition
  • 37. © 2016 William W.-Y. Liang, All Rights Reserved. The Endless Journey on Discovering Linux To be Continued… This brief introductory lecture is just a beginning. Still many interesting things out there Take it as interesting adventure, by learning from the wonderful open source world! 37 NTU OSSSP 2016: Linux Kernel https://en.wikipedia.org/wiki/Linux_kernel#/media/File:Linux_kernel_map.png Let’s keep the discussion next week on the next topic – Android Framework.
  • 38. Note: The Copyrights of the referenced materials and photos go to their original authors. As a result, this slide is for personal reference only. For the contents created in this document, the Copyright belongs to William W.-Y. Liang. © 2005-2016 All Rights Reserved. Q & A william.wyliang@gmail.com http://www.ntut.edu.tw/~wyliang http://www.facebook.com/william.wyliang