Sie sind auf Seite 1von 146

Linux for Embedded Applications

July 2005

Darshak Vasavada
(darshak.vasavada@allgosystems.com)
AllGo Embedded Systems Pvt Ltd, Bangalore, India.

www.allgosystems.com
Linux for Embedded Systems
2005, Darshak Vasavada

GNU Free Documentation License

Permission is granted to copy, distribute and/or modify this


document under the terms of the GNU Free Documentation
License, Version 1.1 or any later version published by the Free
Software Foundation. A copy of this license can be found at:
http://www.fsf.org/copyleft/fdl.html

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 2
http://www.allgosystems.com
Contents
Perspective: where is Linux in the Embedded
World?
Sandbox: how to build and run a Linux system?
Embedded Linux kernel
Managing the CPU
Managing memory
File system
I/O handling: Linux device drivers
uClinux for processors without MMU
Real-time implementations

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 3
http://www.allgosystems.com
Time plan
Session 1: Introduction (1.5 hours)
Overview of the embedded system
Configure, build and run a Linux system
Sessions 2 & 3: Embedded Linux kernel: (3 hours)
Process subsystem
File subsystem & device drivers
Session 4: Variants of embedded Linux (1.5 hours)
uClinux
Real-time implementations

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 4
http://www.allgosystems.com
References
Links and materials used in making this presentation:
Linux for Embedded and Real-time applications; Doug Abbot (Newnes)
Understanding Linux Kernel: Bovet and Cesati (OReilly)
The Linux documentation project: http://www.tldp.org/
Tigran Aivazian, Linux kernel internals: http://www.faqs.org/docs/kernel_2_4/lki.html
Process management: http://www.linux.com/guides/lki-2.shtml
Memory management:
Paul Wilsons VM overview: http://home.earthlink.net/~jknapka/linux-mm/vm_paulwilson.html
Linux Memory management: http://linux-mm.org
Programming IPC: Dave Marshalls course notes: http://www.cs.cf.ac.uk/Dave/C/
A tour of Linux VFS: http://www.tldp.org/LDP/khg/HyperNews/get/fs/vfstour.html
Linux device drivers; Rubini and Corbet (OReilly)
online at http://www.oreilly.com/catalog/linuxdrive2/index.html

Google, mailing lists, wikis, notes, writeups,

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 5
http://www.allgosystems.com
Linux in Embedded World
PDAs
Cell phones
VoIP phones
Robots
Audio/video entertainment devices
Gateways, servers, WAPs
Smart cameras for industry automation

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 6
http://www.allgosystems.com
Typical system configuration
Requires
A few MB of RAM (typical 4-8 MB) *
A few hundreds of processor MHz *
Optional hard-disk
----
*: can do with much less
Provides:
Multi-tasking
Connectivity
GUI
Drivers
Applications
Standardization

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 7
http://www.allgosystems.com
Once upon a time ...
The goals of [this] system were
To provide a simultaneous computer access to
a large community
To supply ample computation power and data
storage
To allow users to share their data easily, if
desired.
The year was 1969.
The system being described was Multics.
The entities involved were AT&T, GE and MIT.
-- Maurice J Bach,
The Design of the Unix Operating System

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 8
http://www.allgosystems.com
Story of Unix
Failure of Multics
Space travel on PDP7 by Ken Thompson, Dennis Ritchie
The first commercial Unix system (text processing) on PDP-11
16 kbytes for the system
8 kbytes for the user programs
512 kbytes disk
Limit of 64k per file
Simultaneous development of C language
1973: first time, OS written in a high level language
Got ported to a range of systems
Became popular because of simple and consistent structure
and ease of understanding and programming

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 9
http://www.allgosystems.com
Story of Linux
Andrew Tannenbaum & Minix
Richard Stallman & FSF: GPL & programming environment
(gcc, bash, utilities)
Linus Torvald: the Linux kernel
The internet community and the loads of software
Applications
Processors
Drivers
Optimizations and improvements
Distributions
Documentation
Support

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 10
http://www.allgosystems.com
What is Linux?
Free?
Royalty free? Open source? Community support?
Kernel?
Multi-tasking? File system? Networking? Device drivers?
Programming environment?
gcc, gdb, libraries?
bash, vi, make?
A whole lot of development tools?
Connectivity?
TCP/IP? ping, ftp, telnet, http, browsers?
Applications?
GUI? Web? Multimedia?
All of these?!

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 11
http://www.allgosystems.com
Linux
To an end user
GUI, multimedia, connectivity
Free
To a programmer
A rich development environment
System calls: processes, file system, networking
To a Linux developer
CPU management
Memory management
File management
Device drivers

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 12
http://www.allgosystems.com
Users view
Applications
Free/open source applications
Browsers, mailers, graphics, multimedia,
Networking
How are these implemented?
Processes and threads
File system
File system mounted at /  root
Devices and hard-disks mounted at mount-points
Multi-user system; user access: r-w-x
Special user called root or the superuser
Has full accesses to the entire directory structure

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 13
http://www.allgosystems.com
A programmers view
Programming environment
Open source
Shell
make
Compiler tools: gcc & Co
Editors: vi, emacs,
Simple shell programming tools: sed, grep, find, cut,
Standard C libraries, X windows, GUI libraries
System calls
Simple and consistent structure
The operating system services available as system calls
Over 50 calls in the original Unix
About 300 system calls in Linux

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 14
http://www.allgosystems.com
System calls
The OS is available as a set of system calls in C
Files:
Accessing files: creat, open, close, read, write, lseek
Ownerships and permissions: chown, chmod
Devices: open, close, read, write, ioctl
Processes:
Create and terminate: fork, exec and exit
IPC: inter-process communication
Semaphores, shared memory
Message queues
Pipes
Signals

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 15
http://www.allgosystems.com
Internal view
Process subsystem
Process creation, termination, scheduling
Managing memory for processes
Inter-process communication
File subsystem
Character special
TTY, codec
Block special
HDD, CD
Device drivers
Hardware interface

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 16
http://www.allgosystems.com
Unix architecture ( source: maurice bach )
User programs

libraries User mode

Kernel mode
system call interface

File Sub-system
IPC
Process
Control Sched
Char Block
Sub-system MM
Device drivers

hardware interface Software

hardware Hardware

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 17
http://www.allgosystems.com
Embedded developers view
How to build? Locate?
What is the foot print?
How does it manage CPU?
How does it manage memory?
How does it handle devices?
What is a process?
What is a thread?
What are scheduling mechanisms?
How does it multitask?
What are inter-task/inter-process communication mechanisms?
How does it handle interrupts?
What are interrupt latencies?
When does it disable interrupts?
Can it meet real-time requirements?
What file systems does it have?
How can I use networking?
What processors are supported?
What GUIs can be used with Linux?
What about the processors that do not have MMUs?
etc. etc.

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 18
http://www.allgosystems.com
Embedded developers view
Developing embedded systems
Configuration and build
Developing applications
Writing a driver
Kernel internals
Process subsystem
Processes, tasks, memory management, scheduler
File subsystem
File systems and drivers
Real-time performance
Process and task switch latencies
Interrupt latencies
Various approaches to improve real-time performance

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 19
http://www.allgosystems.com
Course Map
Introduction System call overview Conclusion


Introduction Process File

Linux Kernel Overview


Sub-system Sub-system
Embedded VM Mgt File system uClinux
Systems Mounting
Overview CPU
File
Processes
Linux Directory
Sandbox Threads
Links
Configure Sched Real-time
Drivers
Build system
IPC/ITC Modules
Run
shm que Drv struct
sem sig Interrupt

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 20
http://www.allgosystems.com
[ Embedded Systems Overview ]
Embedded systems overview
Where is Linux in the world of embedded software
systems?
1. Systems without an operating system
2. Systems with a micro-kernel (RTOS)
3. Systems with embedded operating system

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 22
http://www.allgosystems.com
1. while (1) systems
Simple system without any operating system
System components
Vector table
Start up
Interrupt handlers generating various events
While (1) loop handling these events

All linked together, placed in ROM


Execute from ROM/RAM
Sequential activities, one after the other

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 23
http://www.allgosystems.com
Example
system_init ();
ISR1:
Receive data. while (1) {
switch (event) {
If buffer full
case EVENT1:
Generate EVENT1. process_data1 ();
break;
ISR2:
Transmit data. case EVENT2:
produce_data2 ();
break;
If buffer empty
Generate EVENT2. case EVENT3:
run_state_machine ();
ISR3: break;
Generate EVENT3.
/* etc. ... */

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 24
http://www.allgosystems.com
Memory map
RAM map
Flash map
Vector table

Vector table Initialization 2

Initialization 1 while (1)


{
Boot code ...
|
RAM image
ISRs

Constants

Data

Stack

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 25
http://www.allgosystems.com
Variants
ISR1
function1
ISR2

Scheduler function2
ISR3

function3

Job queues, non-preemptive scheduler


Can prioritize the system activities
The longest activity determines the system
response

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 26
http://www.allgosystems.com
2. Systems with RTOS
Complex real-time requirements
A single activity taking longer than the fastest
response time
Division of timings and functionality
Divide asynchronous activities into tasks
Provide priorities to tasks according to timing
requirements
Let the operating system take care of
scheduling

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 27
http://www.allgosystems.com
RTOS
Provides tasks for asynchronous execution
Provides mechanisms for communication and
synchronization between the tasks
Event flags
Semaphores
Data queues
Lets the application handle interrupts and devices
directly

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 28
http://www.allgosystems.com
Example
Semaphore
Circular buffer

T1 T2

Rx ISR
Message queue
T3 Tx ISR

DMA
Double buffer

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 29
http://www.allgosystems.com
Cooperative multi-tasking
interrupt

ISRs

scheduler

high priority

low priority

idle
t

Priority preemptive scheduler

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 30
http://www.allgosystems.com
Performance matters!
Cooperative multi-tasking
Fairness is not an issue
Tasks and ISRs call OS as and when required
A single task can potentially hog-up the system
Protection (often) is not an issue
Tasks are designed together
Potential to corrupt each-others space
Application specific driver model
ISRs and devices directly handled by the application code
Tasks communicate with ISRs with double buffers, flags
and message queues
Sometimes application tasks even turn off interrupts or the
operating system scheduler

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 31
http://www.allgosystems.com
Building such a system
Source
Object
files
files RAM
Compiler/ image Debug-
Assembler ger RAM
Run-
time Boot
Linker
library loader

RTOS Flash
ROM or any
Linker prog storage
CMD

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 32
http://www.allgosystems.com
Memory map
RAM map
Flash map
Initialization 2

Initialization 1 RTOS
code & data
Boot code Task1 code
Task2 code
RAM image Task3 code
ISRs

Constants

Data
Task1 stack
Task2 stack
Task3 stack

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 33
http://www.allgosystems.com
Embedded system with RTOS
Huge variety of these
Home brew, free, commercial
AMX, KwikNet, KwikPeg (from KADAK Products Ltd.) PSX (from JMI Software Systems, Inc.)
C EXECUTIVE(from JMI Software Systems, Inc.) QNX Neutrino (from QNX Software Systems Ltd.)
CMX-RTX (from CMX Systems, Inc.) QNX4 (from QNX Software Systems Ltd.)
DeltaOS (from CoreTek Systems, Inc.) REDICE-Linux(from REDSonic, Inc.)
eCos(from Red Hat, Inc.) RTLinux (from Finite State Machine Labs, Inc.)
embOS (from SEGGER Microcontroller Systeme GmbH) RTX 5.0 (from VenturCom)
eRTOS (from JK microsystems, Inc.) Portos (from Rabih Chrabieh)
ETS(from VenturCom) smx (Micro Digital, Inc) (from Micro Digital, In.)
EYRX (from Eyring Corporation) SuperTask! (from U S Software)
INTEGRITY (from Green Hills Software, Inc.) ThreadX (from Express Logic, Inc.)
INtime real time extension to Windows (from TenAsys Treck AMX (from Elmic Systems USA, Inc.)
Corporation) Treck MicroC/OS-II (from Elmic Systems USA, Inc.)
IRIX (from SGI) TronTask! (from U S Software)
iRMX(from TenAsys Corporation) TTPos: (from TTTech Computertechnik AG)
Jbed (from esmertec, inc.) Virtuoso (from Eonic Systems)
LynxOS (from LynuxWorks) VxWorks 5.4 (from Wind River)
MQX(from Precise Software Technologies Inc) SCORE, DACS and TADS(from DDC-I)
Nucleus PLUS (AcceleratedTechnology, ESD Mentor Graphics) Nimble - the SoC RTOS(from Eddy Solutions)
On Time RTOS-32 (from On Time Informatik GmbH) Nucleus (from Accelerated Technology)
OS-9 (from Microware Systems Corporation) Fusion RTOS (from DSP OS, Inc.)
OSE (from OSE Systems ) FreeRTOS (from Richard Barry)
PDOS (from Eyring Corporation) VelOSity (Green Hills)

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 34
http://www.allgosystems.com
3. Ports of desktop OS
Resources becoming powerful and inexpensive
CPU/memory/hard disks
Increasing functionalities
Networking/GUI/file systems
Run downloadable applications
Pressure on product cycle time
Trimmed down ports of desktop systems on
embedded devices

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 35
http://www.allgosystems.com
Development model

T1 T2 App App App

System calls
ISR

T3 ISR Drivers

D Hardware

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 36
http://www.allgosystems.com
What constitutes a Linux system?
Boot code
Initializes the system
Loads the operational code into memory
From ROM, network etc.
Kernel modules
A huge number of .o files
Process subsystem, file subsystem, device drivers etc.
Applications
Specific applications (networking, multimedia, GUI)
Standard applications: ls, rm, cp, telnet, ftp etc. etc.
Zipped together in a single file
Linked with the kernel module

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 37
http://www.allgosystems.com
System memory map
RAM map

RAM file system


Flash map
Monolith kernel
+ device drivers (code,
Boot code data, stack)
Compressed PAGE FRAME
( kernel + file
system ) PAGE FRAME
PAGE FRAME
PAGE FRAME
PAGE FRAME
PAGE FRAME

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 38
http://www.allgosystems.com
Boot loader
Resides in ROM
Downloads the operating system into
the system memory
Can download over UART or Ethernet
Can flash the OS and boot from flash
Contains initialization routines,
drivers, file systems, network stack
Can load a new version into flash
Can boot the system from a remote
host using ftp over network

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 39
http://www.allgosystems.com
Operating system (kernel)
Modules that form the kernel
Architecture specific
code
File system
Process subsystem
Memory management
Networking
Drivers
Sources compiled into a
number of .o files
All linked together along with
the applications to form the
final image

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 40
http://www.allgosystems.com
Applications
Applications cross compiled
with target specific libraries

All applications in individual


executables stored in the
standard Unix file system

The file system goes on the


system as ROM/RAMFS

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 41
http://www.allgosystems.com
Linking apps with the kernel
How are applications linked with the kernels?
Applications are cross compiled and
executables are stored in a Unix-like
directory structure
The application directory structure
converted into a single file (similar to tar)
The single file is converted into an object file
(similar to a C array)
The object file is linked with the rest of the
kernel object files to form the final image

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 42
http://www.allgosystems.com
Putting it all together - I

Boot
Code Cross Flash Flash
Compile Programmer
Boot Boot code
Image

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 43
http://www.allgosystems.com
Putting all together - II
Single file
rootfs Kernel
.o files Kernel
rootfs.o sources
Apps cross compiled
and arranged in link
Unix dir structure
vmlinux
gzip
Application uImage
source code Boot code

Kernel + rootfs

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 44
http://www.allgosystems.com
Putting it all together - III
RAM

Monolith kernel
Flash + device drivers (code,
data, stack)

Boot code RAM file system (apps)

Compressed PAGE FRAME


kernel + rootfs PAGE FRAME
PAGE FRAME
PAGE FRAME
PAGE FRAME
PAGE FRAME

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 45
http://www.allgosystems.com
The development environment

Target
Host CPU+RAM+Flash
JTAG,
UART or
Ethernet
Boot loader
Kernel + apps

(sometimes)
Compiler tools
development tools
Build
 OS sources
 Application sources
Images
Debugger
TTY

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 46
http://www.allgosystems.com
Course Map
Introduction System call overview Conclusion


Introduction Process File

Linux Kernel Overview


Sub-system Sub-system
Embedded VM Mgt File system uClinux

Systems
Overview CPU
Mounting
File
Processes
Linux Directory
Sandbox Threads
Links
Configure Sched Real-time
Drivers
Build system
IPC/ITC Modules
Run
shm que Drv struct
sem sig Interrupt

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 47
http://www.allgosystems.com
Sandbox
Look at the programming environment
Load the boot loader: U-Boot
Configure and build the Linux system
Write an application
Burn the Linux system on the flash
Boot a Linux system from the flash
Look at the memory map; find out the foot-print

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 48
http://www.allgosystems.com
[ Overview of the Linux kernel ]
What is an operating system?
An entity that manages resources
What resources?
CPU
Process and threads
Memory
Memory allocation
Virtual memory management
IO
File system
Device drivers
Block devices (e.g. disk driver)
Char devices (e.g. UART)

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 50
http://www.allgosystems.com
How does the OS come into the picture?
Examples

printf (Hello, world!\n);

p = (int *) malloc (n * sizeof (int));

fd = open (/dev/audio, O_RDONLY);

while (1);

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 51
http://www.allgosystems.com
How OS comes into the picture
When an application uses its service (system calls)
read, write, open, fork
See System Call and OS Structures
System call implemented through trap
On an exception or an interrupt or an exception
I/O interrupts: such as from a serial port
Timer interrupt for task scheduling
Page fault interrupt for virtual memory
management
Segmentation fault, divide-by-zero exceptions
for error handling
The kernel, therefore, is nothing more than a set of
handlers in the vector table!

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 52
http://www.allgosystems.com
Processor modes
Supervisor mode
Also known as kernel mode
Has full control to all the processor resources
User mode
Does not have access to
IO
MMU
Vector table
Certain registers (e.g. processor mode)
Privileged instructions
Whenever attempted any of the above ...
Generates an exception (access violation)

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 53
http://www.allgosystems.com
main (){
The trap door int i, j;

for (i = 0; ...
System calls j = ...
printf (...
write (1, )
return 0;
}
_syscall (WRITE, )

User mode Copy ARGS


trap

trap handler:
Kernel mode switch (ARG[0])
case WRITE: write read fork
write functionality
case READ:
read functionality
case FORK: hardware interface
fork functionality
... hardware

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 54
http://www.allgosystems.com
Er, excuse me!
Interrupts
main (){
int i, j;
External
for (i = 0; ... Interrupt
j = ... User mode
printf (...

return 0;
}
interrupt handler Kernel mode
Handling device
Transmitting data
Receiving data
Acknowledging interrupts

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 55
http://www.allgosystems.com
Times up!
Task scheduling main (){
FILE *fp;

fp = fopen (...,
fscanf (fp, ...


main (){
int i, j; return 0;
Timer }
for (i = 0; ... Interrupt
j = ... User mode
printf (...

return 0; Kernel
timer handler
}
mode

Scheduler

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 56
http://www.allgosystems.com
You, idiot!
main (){
Segmentation violation int i, *p;
Divide-by-zero
p = NULL;
Access violation *p = 77;

Exception: ...
segmentation
violation return 0;
}
User mode

Kernel Exception handler


mode
Print the error message
Terminate the process

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 57
http://www.allgosystems.com
Operating system
is a set of exception handlers
traps for system calls
software interrupts for exception handling
hardware interrupts for device handling
timer interrupts for scheduling

A quick quiz: what happens when


fopen?
exit?
malloc?

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 58
http://www.allgosystems.com
Unix system calls
Process subsystem
Processes: fork, clone, exec
Memory: sbrk, mmap
File subsystem
File system: mount, umount
Files: open, close, read, write
Drivers: handled as special files

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 59
http://www.allgosystems.com
Unix architecture ( source: maurice bach )
User programs

libraries User mode

Kernel mode
system call interface

File Sub-system
IPC
FILE ProcessPROCESS
Control Sched
SUB-SYSTEM
Char Block SUB-SYSTEM
Sub-system MM
Device drivers

hardware interface Software

hardware Hardware

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 60
http://www.allgosystems.com
Course Map
Introduction
System call overview Conclusion


Introduction Process File

Linux Kernel Overview


Sub-system Sub-system
Embedded VM Mgt File system uClinux

Systems
Overview CPU
Mounting
File
Processes
Linux Directory
Sandbox Threads
Links


Configure
Build
Sched
Drivers Real-time
system
IPC/ITC Modules
Run
shm que Drv struct
sem sig Interrupt

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 61
http://www.allgosystems.com
[ Process sub-system ]
What is a process?
A program in execution
An application in action, a file with life
A mechanism to share the CPU and memory
amongst multiple applications
Virtualization of CPU
Each process thinks it has the entire CPU to
itself
Processes appear to be executing
simultaneously to the end user
Virtualization of memory
Each process thinks it has the entire address
space to itself

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 63
http://www.allgosystems.com
What forms an executable?
Text: Executable file
Instruction opcodes
File header

Symbol table
Data:
Global and static variables
Section header
Global and static constants
(tables, strings) Section data

Section header
Headers, symbolic information etc.
Section data

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 64
http://www.allgosystems.com
Example
#include <stdio.h> EXE
File header

int global; Symbol table


main = 0x1000, printf = 0x1200
global = 0x2000
main () Section text
{ start = 0x1000, length = 0x1000
int local; Opcodes for main, printf

Section data
global = 1; start = 0x2000, length = 0x4
local = 0; global
printf (hello, world!\n); Section data
return 0; start = 0x2004, length = 0x40

} string hello, world

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 65
http://www.allgosystems.com
What forms a process?
Process is a running instance of a program
virtual
address
Text: space
Instructions opcodes loaded in the memory 0000...
Text

Data: Data
Globals, statics loaded in the memory
Heap

Heap
Space for allocating run-time memory

Stack Stack

Space for local variables, arguments etc. FFFF...

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 66
http://www.allgosystems.com
Executable: example
int global;

main ()
{
int local;

global = 1;
local = 0;
printf (hello, world!\n);
return 0;
}

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 67
http://www.allgosystems.com
Example: continued
EXE
File header Process virtual address space
Symbol table
main = 0x1000, printf = 0x1200
Text
global = 0x2000
0x1000: opcodes for main
Section text 0x1200: opcodes for printf
start = 0x1000, length = 0x1000
Data
Opcodes for main, printf global at 0x2000
hello world at 0x2004
Section data
start = 0x2000, length = 0x4 Heap (created run-time)
global
Section data
start = 0x2004, length = 0x40
Stack (created run-time)
string hello, world local at 0xfff8

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 68
http://www.allgosystems.com
Virtualization of memory
Q. Is the entire process required to be loaded into the memory?

A. The process address space can be larger than the physical


memory available.
B. Multiple processes may share the same addresses

Therefore, virtualization of memory.

 Divide the process address space into a number of pages


 Divide the physical memory into a number of frames
 Load the pages into frames as and when required
 Perform the translation from virtual to physical addresses
with the help of the processor hardware (MMU)

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 69
http://www.allgosystems.com
Use of physical memory
Process P1 virtual Physical
address space memory
MMU KERNEL

text P1 stack
P2 text
P3 stack
P1 text
P2 data
P2 text
data P1 data
P2 data
P2 stack
Page table
P11 text

stack

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 70
http://www.allgosystems.com
Use of physical memory
Process P2 virtual Physical
address space memory
MMU KERNEL

text P1 stack
P2 text
P3 stack
P1 text
P2 text
P2 text
data P1 data
P1 data
P2 stack
Page table
P11 text

stack

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 71
http://www.allgosystems.com
Virtual address space
P1 00000000 P2
Text Text 0x100 mpy r7,r3
0x100 add r0,r1
0x101 st r0,(r3) 0x101 add r7,r2
0x102 jmp 0x110 Data Data 0x102 sub r3,r0

0x10000 1a 2b 23 47
0x10000 11 22 33 44 0x10004 72 46 7d 48
0x10004 55 66 77 88 Stack Stack
0x10008 d9 ba ca 9c
0x10008 99 aa bb cc FFFFFFFF

Quiz:
I am debugging P1. I place a break-point and when the break-point hits:
1. I examine location 0x10003, what contents should I see?
2. If I see the list at location 0x101, what instruction should I find?
3. How can I list the opcodes of process2?

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 72
http://www.allgosystems.com
Example
#include <stdio.h>
#include <stdlib.h>

int num;

main (int argc, char *argv[])


{
num = atoi (argv[1]);

while (1)
printf (*(%p) = %d\n, &num, num);

return 0;
}

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 73
http://www.allgosystems.com
Advantages of virtual memory
The whole process does not have to be loaded in
the physical memory; only the parts which are
required can be loaded.

Multiple processes can simultaneously occupy


parts of physical memory.

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 74
http://www.allgosystems.com
Pages
Virtual page: a block of memory in the virtual
address space

Logical page: a block of memory that contains code


or data. More than one virtual pages can map to a
single logical page.

Physical frame: a block of physical memory to


which a logical page is mapped.

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 75
http://www.allgosystems.com
Pages
virtual logical physical
pages pages frames

regions

Proc text 4k
A data 8k
stack 4k

Proc text 4k
B data 8k
stack 4k

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 76
http://www.allgosystems.com
How is a process created?
Created by fork() system call
Every process other than pid=0 process is created
by fork
Pid=0: special process that becomes swapper
Pid=1: init mother of all the processes
All the remaining processes are children and
grand-children and great-grand children etc. of init

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 77
http://www.allgosystems.com
Process related system calls
fork : create a child process
vfork : a variant of fork
wait : wait for the child process to get over
exit : terminate a process
exec : load an executable
clone : create a light-weight process

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 78
http://www.allgosystems.com
Example
What happens when you run a command from a
shell (or run through GUI by double clicking)?

$ ls 1 bash running,
reads command ls
f.c
f.h bash calls fork
bash continues running copy of bash running
f.o
exec (/bin/ls,
a.out bash calls wait
ls running
$ bash waiting
bash comes out of wait ls calls exit
bash continues running

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 79
http://www.allgosystems.com
fork example
if ((pid = fork()) == 0)
{
while (1)
printf (child\n);
}
else
{
while (1)
printf (parent of %d\n, pid);
}

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 80
http://www.allgosystems.com
exec example
main (int argc, char *argv[], char *envp[])
{
char *argv[] = {/home/dsv/bin/myprog, NULL};

if (execve (/home/dsv/bin/myprog, argv, envp) == -1)


printf (error in exec.\n);
else
printf (exec successful.\n);
}

Quiz:
1. When will the above code print exec successful?

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 81
http://www.allgosystems.com
Context switch
How to switch from a process to another process?
Save the context of the old process
Bring in the context of the new process
What forms the context of a process?
Virtualization of the CPU
Registers including PC and SP
Virtualization of memory
Page tables

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 82
http://www.allgosystems.com
Light weight processes
Full context switch is an expensive operation
Especially the memory virtualization process
Solutions: light weight processes
Performs only the CPU context switch
Multiple threads within a process
Share text and data
Have their own stacks
Reduced context
Processor registers
Stack pointer
Significantly fast context switch

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 83
http://www.allgosystems.com
Thread
A function running asynchronously
Example usage
int global;
f1()
{ f1
thread_create (f2, ...);
global = 5; thread create
... f2 starts
f1 continues running
}
running
f2()
{
...
global = 7;
...

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 84
http://www.allgosystems.com
Options in light weight
processes
pthreads library
Multi-threading at application level
By application code
Light weight processes
Multi-threading at application level
Scheduling by kernel
Created by the function clone()
Kernel threads
Multi-threading inside operating system level
Scheduling by kernel
Separate communication primitives (sem, que, )

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 85
http://www.allgosystems.com
System calls
pthreads: available as a library libpthread.a
pthread_create
pthread_join
pthread_cancel
Light weight process: clone
Kernel threads: kernel_thread

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 86
http://www.allgosystems.com
pthread example
main ()
{
pthread_t thid;
pthread_create (&thid, NULL, newthread, 0);
for (j = 0; j < 10; j++)
printf (I am the old thread.\n);
pthread_join (&stat);
}

void *newthread (void *arg)


{
for (j = 0; j < 10; j++)
printf (I am the new thread.\n);
return NULL;
}

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 87
http://www.allgosystems.com
Process address space

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 88
http://www.allgosystems.com
Process and threads

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 89
http://www.allgosystems.com
Multi-level priority scheduling
priority
levels

Level N Zzz Zzz

Level 3 Zzz

Level 2

Level 1 Zzz

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 90
http://www.allgosystems.com
Scheduling - notes
Multi-level priority scheduling
Static priorities: 1 to 99
Never changed by scheduler
Useful for real-time implementation
Badly written code can hog the system resources
Dynamic priorities
Assigned by the scheduler
A process can only lower its priority by nice() call
Scheduling policy
SCHED_RR
SCHED_FIFO
SCHED_OTHER (conventional time-shared scheduler)
A process run by root can choose the application policy

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 91
http://www.allgosystems.com
Process states

Executing: currently running


Ready: can run, but not scheduled
Suspended: waiting for input/output
Stopped: by a signal (such as single stepping, suspended)
Zombie: terminated; waiting for parents to read status

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 92
http://www.allgosystems.com
IPC mechanisms
SYS-V IPC
Pipes
Shared memory
Semaphore
Message queues
Signals
Threads IPC
Semaphore
Message queues

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 93
http://www.allgosystems.com
Pipes
Useful for communication between parent-child or
siblings
Pipe call opens two files: one for reading, one for
writing.
The writer process writes into fd[1], reader process
reads from fd[0].

fd[1] fd[0]
P1 P2
fd[0] fd[1]

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 94
http://www.allgosystems.com
Pipe: typical usage
int fd[2]; /* fd[0] for read, fd[1] for write */

pipe (fd); /* open pipes */

if (fork()) {
Used in shell:
/* parent */
write (fd[1], buf, num_bytes); cat foo | grep bar

} else { The shell creates the pipe.

Also creates two processes:


/* child */ cat and grep
read (fd[0], buf, num_bytes);
} And two processes
communicate through pipe.

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 95
http://www.allgosystems.com
Shared memory
Mechanism to share memory between two
unrelated processes
System calls
shmget: get a shared memory
shmat: attach the shared memory to a process
shmdt: detach from the process
Sharing memory between two threads
How?

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 96
http://www.allgosystems.com
Shared memory: example
#define KEY 0xBABADADA
#define KEY 0xBABADADA
process1()
{ process2()
int shmid; {
char *p_addr; int shmid;

shmid = shmget (key, SHM_SZ,


shmid = shmget (key, SHM_SZ,
0666);
IPC_CREAT | 0666);
p_addr = shmat (shmid, NULL,
p_addr = shmat (shmid, NULL,
0); printf (%s\n, p_addr);
}

strcpy (p_addr, hello!);


}

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 97
http://www.allgosystems.com
Semaphores
A mechanism for synchronization & mutual
exclusion
System calls:
semget: get/create a semaphore
semop: operate (post/wait) on a semaphore
0 = wait
1 = post
POSIX semaphores
Light weight compared to SYS-V semaphores
sem_open: get/create a semaphore
sem_wait: wait till a semaphore is post
sem_post: post a semaphore

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 98
http://www.allgosystems.com
Semaphore: synchronization
#define KEY 0xBABADADA
process_producer() #define KEY 0xBABADADA
{
process_consumer()
semid = semget (key, nsems, flg);
{
semid = semget (key, ...
/* write into shared memory */
strcpy (p_addr, hello!); sops[0].sem_op = 0; /* wait
semop (semid, sops, 0);
sops[0].sem_op = 1; /* post */
semop (semid, sops, 0); printf (%s\n, p_addr);
} }

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 99
http://www.allgosystems.com
Semaphore: mutual exclusion
Two processes share common resource which
requires entry regulation
Buffers, linked lists etc.
Necessary because the process switch can occur
at any time

process_1 () process_2 ()
{ {
sem_wait sem_wait
Critical region Critical region
sem_post sem_post
} }

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 100
http://www.allgosystems.com
Message queues
Passing messages from one task to another
System calls:
msgget: create/get a message queue
msgsnd/msgrcv: send/receive a message

Posix message queues


mq_open: create/get a message queue
mq_send: send a message to the queue
mq_receive: receive a message from the queue

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 101
http://www.allgosystems.com
Message queue: example
#define KEY 0xBABADADA
#define KEY 0xBABADADA
process_producer()
process_consumer()
{
{
char buf[MSGSZ];
char buf[MSGSZ];
msqid = msgget (KEY, flags);
msqid = msgget (KEY, flags);
/* write the message */
strcpy (buf, hello!); msgrcv (msqid, buf, MSGSZ,

msgsnd (msqid, buf, MSGSZ, flg); printf (%s\n, buf);


} }

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 102
http://www.allgosystems.com
Signals
Asynchronous notification to a process about an
event
Similar to an interrupt at a user level
System calls
kill(): send a signal to a process (misnomer )
signal(): specify the signal handler (similar to
plugging an ISR in the vector table)
Some examples of signals <sys/signal.h>
SIGINT: when you try to stop a program by ^C
SIGSEGV: segmentation violation
SIGQUIT: kill 9 (can not be ignored or handled)
SIGALRM: alarm expired (started by alarm())
SIGUSER: user defined signal

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 103
http://www.allgosystems.com
Example
void sigint_handler ()
{
signal (SIGINT, sigint_handler);
printf (You pressed ^C!\n);
}

main ()
{
signal (SIGINT, sigint_handler);
while (1)
;
}

Quiz: How will you stop the above program?

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 104
http://www.allgosystems.com
Course Map
Introduction
System call overview Conclusion


Introduction Process File

Linux Kernel Overview


Sub-system Sub-system
Embedded VM Mgt File system uClinux

Systems
Overview CPU
Mounting
File
Processes
Linux Directory
Sandbox
Threads
Links


Configure
Build
Sched
Drivers Real-time
system
IPC/ITC Modules
Run
shm que Drv struct
sem sig Interrupt

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 105
http://www.allgosystems.com
[ file sub-system ]
Unix file system
Hierarchical file system
Root directory
Directories with files
A number of file systems mounted within /

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 107
http://www.allgosystems.com
The file system to a user
Multi-user file system
Hierarchical directory structure starting from root
(/)
Allows mounting a file system at a mount point
Mounting a network device: e.g. remote PC
Mounting a hardware device: e.g. thumb drive
Mounting heterogeneous file systems: ext, FAT,

root, the super-user, has all permissions to all
files
Access rights (rwx)
rwx.rwx.rwx : user.group.others

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 108
http://www.allgosystems.com
The file system to a programmer
System calls
Accessing files: creat, open, read, write, close
Changing file attributes: chown, chmod
Mounting, unmounting devices: mount, umount
Changing root: chroot

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 109
http://www.allgosystems.com
The virtual file system
Any types of file systems and devices can be
mounted seamlessly.
The programmer does not have to worry about the
file system type.
The programs written in the same way, with open,
close, read, write etc. function calls.
VFS implemented with function pointers. A new FS
has to provide body of these functions.

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 110
http://www.allgosystems.com
File system
Boot block: contains the boot code Boot block
Super block: information about the
file system Super block
Inode list: list of inodes
(information about each file) Inode list
Data blocks: disk blocks
containing data
Data blocks

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 111
http://www.allgosystems.com
Files: internal view
Inodes
File owner
File type
Access permissions
Access times (accessed, modified)
Number of links to the file
File size
TOC of disk blocks
In-core inode table
Run-time copy of inodes in the memory
Lock and the process waiting to be unlocked
Device number of the file system containing the file
Reference count: number of active instances

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 112
http://www.allgosystems.com
Structure of a file
File made up of a number of disk blocks
A table-of-contents of the disk blocks in the inode
Direct blocks, single, double and triple indirect
blocks

Directory
Directory is a file too
Contains information about the files
Inode number
File name

Q: How can you see the contents of a directory?

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 113
http://www.allgosystems.com
Super-block
The structure containing the information about the
file system
Size of the file system
Free blocks (size, list of free blocks, index of
the next free block)
Free inodes (number of free inodes, list of free
inodes, index of the next free inode)

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 114
http://www.allgosystems.com
Mounting a file system
mount: device  file system
Mount table
Device number
Pointer to the super block
Pointer to the root inode
Pointer to the inode of the mount-point
mount and umount system calls

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 115
http://www.allgosystems.com
mount
mount (/dev/disk1, /usr, 0);
Check if super-user
Add an entry in the mount table
Open the device
Read superblock into the buffer cache
Read root inode
Mark the mounted-on directory as the mount
point

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 116
http://www.allgosystems.com
Mounting the file system
fstab
File containing mount information
Device mount-point file-type args
/dev/hda5 /windows/d ntfs ro 0 0
Mount/umount commands to mount and unmount
the file systems
# mount t ntfs /dev/hda5 /windows/s
Automount: mount on use

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 117
http://www.allgosystems.com
File system calls
open: open a file for reading/writing
close: close a file
creat: create a file
chown, chmod, chgrp: change permissions
link, unlink: create/remove a link

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 118
http://www.allgosystems.com
Hard and symbolic links
Hard link: same inode but additional directory entry
Example:
$ ln file1 file2
$ ls i file1 file2
(will show the same inode number for file1 & file2)
Soft (symbolic) link:
One more inode is created that points to the original
inode
Equivalent to short-cut folder
Will show different inode numbers in above example

Quiz: what happens to the link if the original file is deleted:


In case of a hard link?
In case of a soft link?

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 119
http://www.allgosystems.com
Example: busybox
main (char *argv[], int argc) {
if (argv[0] equals ls) do_ls ();
if (argv[0] equals cp) do_cp ();
...

$ cc bb.c o bb
$ ln s bb ls
$ ln s bb cp
$ ls
(runs ls)
$ cp f1 f2
(runs cp)

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 120
http://www.allgosystems.com
Course Map
Introduction
System call overview Conclusion


Introduction Process File

Linux Kernel Overview


Sub-system Sub-system
Embedded VM Mgt File system uClinux

Systems
Overview CPU
Mounting
File
Processes
Linux Directory
Sandbox
Threads
Links


Configure
Build
Sched
Drivers Real-time
system
IPC/ITC Modules
Run
shm que Drv struct
sem sig Interrupt

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 121
http://www.allgosystems.com
[ IO sub-system ]
Loadable modules
Module is a .o file that can be dynamically loaded
and linked into the kernel
Once loaded, becomes a part of the kernel
Modules commands:
lsmod: display all the currently loaded modules
modinfo: display module information
Insmod/rmmod: insert/remove a single module
modprobe: load module plus any dependencies
Modules are used for dynamically adding device to
the kernel

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 123
http://www.allgosystems.com
A simple module*
#define MODULE
#include <linux/module.h>
int init_module(void)
{
printk("<1>Hello, world\n");
return 0;
}

void cleanup_module(void)
{
printk("<1>Goodbye cruel world\n");
}
* For Linux v2.4.
darshak.vasavada@allgosystems.com AllGo Embedded Systems
ASPICES 2005, Indian Institute of Science 124
http://www.allgosystems.com
How to insert the module? (#)
gcc c hello.c
Creates hello.o
insmod hello.o
Adds hello.o to the kernel.
Also executes init_module function
Typically used to register a device driver
rmmod hello.o
Removes hello.o from the kernel
Also executes cleanup_module function
Typically used to unregister a device driver

#: different in 2.6

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 125
http://www.allgosystems.com
Device
Block device: hard disk, CDROM, SCSI
Character device: TTY, I/O ports
Has a file name (e.g. /dev/audio)
Has an inode (type = block/char, permissions)
System calls: open, read, write, close, ioctl
Major device number
Indicates the device type: for example, tty
Minor device number
Indicates an instance of the specified device
For example: tty01, tty02, tty03

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 126
http://www.allgosystems.com
Device usage: example
#include <stdio.h>

main ()
{
int fd;

fd = open (/dev/audio, O_RDONLY);


while (1)
{
/* Read a buffer from a file */
...

/* Playback on the device. */


write (fd, buf, BUFFER_SIZE);
}
}

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 127
http://www.allgosystems.com
Device driver
A set of functions to access a device
init, read, write, open, release, ioctl,
Runs as a part of the kernel
Can access all the peripherals & vector table
Runs in real mode, no virtual memory
How do these functions become a part of the
kernel?
Statically linked into the kernel (build time)
Dynamically insterted as a module (run time)
Registering a device
Device functions are added into the device
table

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 128
http://www.allgosystems.com
Character device driver
Driver functions (file operations, fops for short)
init: one time device initialization
open: first time device init, allocate resources
read: receive data from the device
write: transmit from the device
release(close): freeup resources, disable device
ioctl: change the device parameters
Character device switch table
No open release read write ioctl

23 cd_open cd_close cd_read cd_write cd_ioctl

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 129
http://www.allgosystems.com
Device driver structure
.c  Module.o

Or system init Or init

read struct fops


write
open
tty_read
tty_write
tty_open kmalloc

Or system shutdown Or uninit

From Understanding Linux Kernel, Bovet & Cesati, OReilly.

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 130
http://www.allgosystems.com
Implementing a device driver
1. Implement device file operations:
init, read, write, open, release, ioctl, ISR
2. These functions are statically linked with the kernel
or dynamically added to the kernel with insmod.
3. Register these functions with the kernel:
register_chrdev (major, device,
 Kernel creates an entry in the device table and
allocates a major device number
4. Create the device node (file) in /dev
mknod
 Creates a file in /dev, e.g. /dev/audio

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 131
http://www.allgosystems.com
Using a device driver
Same file system calls: open, read, write, close

open (/dev/audio, O_RDONLY)


User mode

Device table
Kernel mode

open (/dev/audio, O_RDONLY)

FS major dev no. aud_open()


Inode {
char special
}

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 132
http://www.allgosystems.com
Interrupt handling
Request IRQ
A module should request an IRQ
request_irq (irq, handler, flags, );
Free IRQ
Free-up the IRQ
Writing a handler
Communication with the device
Read/write into the device registers
Clear the interrupt pending register
Communication with the driver functions
Data through buffers
Synchronization through kernel events

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 133
http://www.allgosystems.com
Driver ISR synchronization
char *ptr;
dev_read (, char *buf, int count, )
{
ptr = buf; n = 0; dev_irq
while (n < count) {
{ ch = READ_PORT();
wait_event (); *ptr++ = ch; /* ** */
n++;
} wake_up ();

return; return;
} }
** : ignores kernel/user space distinciton

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 134
http://www.allgosystems.com
Tasklets
Issues in ISRs
Too long to be in the interrupt context
(Would disable other interrupts/tasks)
Important enough not to be delayed
Top half and bottom half
Top half to execute the time-critical code in the
ISR context
Bottom half executed in kernel at a later point
Tasklets
DECLARE_TASKLET (name, function, data);
Schedule from an ISR using tasklet_schedule().

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 135
http://www.allgosystems.com
Tasklet example
void interrupt_handler (void)
{
/* do device related processing */
...

/* schedule the tasklet */


tasklet_schedule (tasklet_function);
}

void tasklet_function (void)


{
/* tasklet functionality */
}

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 136
http://www.allgosystems.com
Course Map
Introduction
System call overview Conclusion


Introduction Process File

Linux Kernel Overview


Sub-system Sub-system
Embedded VM Mgt File system uClinux

Systems
Overview CPU
Mounting
File
Linux
Processes
Directory
Sandbox
Threads
Links


Configure
Build
Sched
Drivers Real-time
system
IPC/ITC Modules
Run
shm que Drv struct
sem sig Interrupt

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 137
http://www.allgosystems.com
[ uClinux and real-time Linux ]
uClinux
Linux for the processors that do not have MMU
No virtual memory management support
Processes can see each others memory
Processes can see kernel/driver memory
No page tables
Context switch faster
Drivers and applications share the same place
Faster communication
Appropriate for DSPs and micro-controllers
Included in the main-stream linux from 2.6 onwards

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 139
http://www.allgosystems.com
Consequences of having no MMU
Program loading
Either the kernel has to fix-up the addresses
Run-time overhead on the kernel
Can not move-around the pages
Use this with ELF files
Or, the code has to be position independent
Program and data accesses with relative to PC
Significant memory and MHz overheads
Use this with new file type called flat

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 140
http://www.allgosystems.com
Consequences of having no MMU
Process creation with vfork
fork() would have to copy the whole process
Instead, expects exec after fork()
Parent waits until the child does exec()
Until then, child shares everything, including
stack
Dynamic memory allocation
Can not have autogrow stack
Fixed sized or compile time stacks (4k)
Can not use brk (dynamic memory allocation)
Can corrupt memory across processes

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 141
http://www.allgosystems.com
Linux and real-time
Real-time requirements
Deterministic interrupt response
Deterministic process response
Factors affecting the real-time performance
Kernel disabling interrupts for long duration
Context switch timings vary with load
User mode to/from kernel mode switch timings

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 142
http://www.allgosystems.com
Approaches
Run the time-critical activities on a separate
processor:
Micro (OS, GUI, networking) with
DSP (signal processing and fast interrupts)
Use faster clock or more powerful processor
May not gurantee a real-time
Use light weight processes
Use kernel threads and direct communication with
the devices
Run Linux over a small RTOS
Improve Linux scheduler

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 143
http://www.allgosystems.com
RTLinux
Run Linux as a low priority task of a hard real-time
kernel (www.fsmlabs.com)

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 144
http://www.allgosystems.com
Improvements to kernel
Real-time framework within the kernel (e.g. KURT)
Real-time event handling modules
RTmods (real-time modules): parts of application
which require real-time performance
RTmods run under kernel context
Allows user apps to schedule events at a resolution
of 10 us.
Scheduler improvements
Increase preemption points
Simpler preemptive scheduler with less focus on
fairness

darshak.vasavada@allgosystems.com AllGo Embedded Systems


ASPICES 2005, Indian Institute of Science 145
http://www.allgosystems.com
Thanks!

(All is well that ends )

Das könnte Ihnen auch gefallen