You are on page 1of 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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems 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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems 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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems 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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems 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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems 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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems 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
AllGo Embedded Systems http://www.allgosystems.com
8

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems 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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

10

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

11

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

12

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

13

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

14

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

15

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

16

Unix architecture (
User programs

source: maurice bach

)
User mode Kernel mode

libraries

system call interface

File Sub-system Process Control Sub-system

IPC Sched MM

Char

Block

Device drivers hardware interface hardware


darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

Software Hardware
17

AllGo Embedded Systems 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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

18

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

19

Course Map
Introduction
Introduction Embedded Systems Overview Linux Sandbox Configure Build Run System call overview Process Sub-system VM Mgt CPU Processes Threads Sched IPC/ITC shm que sem sig File Sub-system File system Mounting File Directory Links Drivers Modules Drv struct Interrupt

Conclusion Linux Kernel Overview

uClinux

Real-time system

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

20

[ 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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

22

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

23

Example
system_init ();

ISR1: Receive data. If buffer full Generate EVENT1. ISR2: Transmit data. If buffer empty Generate EVENT2. ISR3: Generate EVENT3.

while (1) { switch (event) { case EVENT1: process_data1 (); break; case EVENT2: produce_data2 (); break; case EVENT3: run_state_machine (); break; /* etc. ... */

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

24

Memory map
Flash map
Vector table Initialization 1 Boot code RAM image ISRs Constants Data Stack

RAM map
Vector table Initialization 2 while (1) { ... |

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

25

Variants
ISR1 ISR2 ISR3 Scheduler function1 function2 function3

Job queues, non-preemptive scheduler Can prioritize the system activities The longest activity determines the system response

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

26

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

27

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

28

Example
Semaphore Circular buffer T1 T2

Rx ISR Message queue T3 Tx ISR

Double buffer

DMA

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

29

Cooperative multi-tasking
interrupt ISRs scheduler high priority low priority idle t

Priority preemptive scheduler

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

30

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

31

Building such a system


Source files Compiler/ Assembler Runtime library RTOS Linker CMD ROM prog Object files RAM image

Debugger

RAM

Linker

Boot loader Flash or any storage

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

32

Memory map
Flash map
Initialization 1 Boot code RAM image

RAM map
Initialization 2 RTOS code & data Task1 code Task2 code Task3 code ISRs Constants Data Task1 stack Task2 stack Task3 stack

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

33

Embedded system with RTOS


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

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

34

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

35

Development model

T1

T2

App

App

App

ISR T3 ISR

System calls

Drivers

Hardware

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

36

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

37

System memory map


RAM map
RAM file system

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

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

38

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

39

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
AllGo Embedded Systems http://www.allgosystems.com

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

40

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

41

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

42

Putting it all together - I


Boot Code

Cross Compile

Flash Programmer

Flash
Boot code

Boot Image

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

43

Putting all together - II


Single file rootfs rootfs.o
Apps cross compiled and arranged in Unix dir structure

Kernel .o files

Kernel sources

link vmlinux gzip

Application source code

uImage
Boot code

Kernel + rootfs

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

44

Putting it all together - III


RAM Flash
Boot code Compressed kernel + rootfs
Monolith kernel + device drivers (code, data, stack)

RAM file system (apps)


PAGE FRAME PAGE FRAME PAGE FRAME PAGE FRAME PAGE FRAME PAGE FRAME

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

45

The development environment


Host
JTAG, UART or Ethernet

Target CPU+RAM+Flash

Boot loader Kernel + apps (sometimes) development tools

Compiler tools Build OS sources Application sources Images Debugger TTY

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

46

Course Map
Introduction
Introduction Embedded Systems Overview System call overview Process Sub-system VM Mgt CPU Processes Threads Sched IPC/ITC shm que sem sig File Sub-system File system Mounting File Directory Links Drivers Modules Drv struct Interrupt

Conclusion Linux Kernel Overview

uClinux

Linux Sandbox Configure Build Run

Real-time system

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

47

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

48

[ 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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

50

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

51

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!
AllGo Embedded Systems http://www.allgosystems.com
52

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

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)
AllGo Embedded Systems http://www.allgosystems.com

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

53

The trap door


System calls

main (){ int i, j; for (i = 0; ... j = ... printf (... write (1, ) return 0;

} _syscall (WRITE, )

User mode
trap handler:

Copy ARGS trap

Kernel mode

switch (ARG[0]) case WRITE: write functionality case READ: read functionality case FORK: fork functionality ...

write

read

fork

hardware interface hardware


54

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

Er, excuse me!


Interrupts
main (){ int i, j; for (i = 0; ... j = ... printf (... return 0; } External Interrupt

User mode

interrupt handler Handling device Transmitting data Receiving data Acknowledging interrupts

Kernel mode

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

55

Times up!
Task scheduling
main (){ FILE *fp; fp = fopen (..., fscanf (fp, ... main (){ int i, j; for (i = 0; ... j = ... printf (... return 0; } Timer Interrupt } return 0;

User mode Kernel mode

timer handler

Scheduler

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

56

You, idiot!
Segmentation violation Divide-by-zero Access violation
Exception: segmentation violation
} main (){ int i, *p; p = NULL; *p = 77; ... return 0;

User mode Kernel mode Exception handler Print the error message Terminate the process

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

57

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

58

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

59

Unix architecture (
User programs

source: maurice bach

)
User mode Kernel mode

libraries

system call interface

File Sub-system

IPC Process PROCESS Sched Control SUB-SYSTEM Sub-system MM

FILE SUB-SYSTEM Char Block


Device drivers

hardware interface hardware


darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

Software Hardware
60

AllGo Embedded Systems http://www.allgosystems.com

Course Map
Introduction
Introduction Embedded Systems Overview System call overview Process Sub-system VM Mgt CPU Processes Threads Sched IPC/ITC shm que sem sig File Sub-system File system Mounting File Directory Links Drivers Modules Drv struct Interrupt

Conclusion Linux Kernel Overview

uClinux

Linux Sandbox Configure Build Run

Real-time system

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

61

[ 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
AllGo Embedded Systems http://www.allgosystems.com

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

63

What forms an executable?


Text: Instruction opcodes Data: Global and static variables Global and static constants (tables, strings) Headers, symbolic information etc. Executable file
File header Symbol table

Section header Section data

Section header Section data

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

64

Example
#include <stdio.h> int global; main () { int local; global = 1; local = 0; printf (hello, world!\n); return 0; }

EXE
File header Symbol table main = 0x1000, printf = 0x1200 global = 0x2000 Section text start = 0x1000, length = 0x1000 Opcodes for main, printf Section data start = 0x2000, length = 0x4 global Section data start = 0x2004, length = 0x40 string hello, world

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

65

What forms a process?


Process is a running instance of a program Text: Instructions opcodes loaded in the memory Data: Globals, statics loaded in the memory Heap Space for allocating run-time memory Stack Space for local variables, arguments etc.
Stack
FFFF...

virtual address space


0000...

Text

Data Heap

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

66

Executable: example
int global; main () { int local; global = 1; local = 0; printf (hello, world!\n); return 0; }

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

67

Example: continued
EXE
File header Symbol table main = 0x1000, printf = 0x1200 global = 0x2000 Section text start = 0x1000, length = 0x1000 Opcodes for main, printf Section data start = 0x2000, length = 0x4 global Section data start = 0x2004, length = 0x40 string hello, world

Process virtual address space


Text 0x1000: opcodes for main 0x1200: opcodes for printf Data global at 0x2000 hello world at 0x2004 Heap (created run-time)

Stack (created run-time) local at 0xfff8

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

68

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

69

Use of physical memory


Process P1 virtual address space MMU Physical memory
KERNEL P1 stack P2 text P3 stack P1 text P2 data P2 text P1 data P2 data P2 stack P11 text

text

data
Page table

stack

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

70

Use of physical memory


Process P2 virtual address space MMU Physical memory
KERNEL P1 stack P2 text P3 stack P1 text P2 text P2 text P1 data P1 data P2 stack P11 text

text

data
Page table

stack

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

71

Virtual address space


P1
0x100 0x101 0x102 add r0,r1 st r0,(r3) jmp 0x110 00000000

P2
Text Data
0x100 0x101 0x102 mpy r7,r3 add r7,r2 sub r3,r0

Text Data

0x10000 11 22 33 44 0x10004 55 66 77 88 0x10008 99 aa bb cc

Stack
FFFFFFFF

Stack

0x10000 1a 2b 23 47 0x10004 72 46 7d 48 0x10008 d9 ba ca 9c

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

72

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

73

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

74

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

75

Pages
virtual pages regions logical pages physical frames

Proc A

text data stack

4k 8k 4k

Proc B

text data stack

4k 8k 4k

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

76

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

77

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

78

Example
What happens when you run a command from a shell (or run through GUI by double clicking)? $ ls 1 f.c f.h f.o a.out $
bash running, reads command ls bash calls fork bash continues running bash calls wait bash waiting bash comes out of wait bash continues running

copy of bash running exec (/bin/ls, ls running ls calls exit

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

79

fork example
if ((pid = fork()) == 0) { while (1) printf (child\n); } else { while (1) printf (parent of %d\n, pid); }

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

80

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

81

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

82

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
AllGo Embedded Systems http://www.allgosystems.com

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

83

Thread
A function running asynchronously Example usage
int global; f1() f1 { thread_create (f2, ...); thread create global = 5; ... f1 continues } running f2() { ... global = 7; ...

f2 starts running

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

84

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, )
AllGo Embedded Systems http://www.allgosystems.com

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

85

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

86

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

87

Process address space

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

88

Process and threads

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

89

Multi-level priority scheduling


priority levels

Level N

Zzz

Zzz

Level 3 Level 2 Level 1

Zzz

Zzz

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

90

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

91

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
AllGo Embedded Systems http://www.allgosystems.com

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

92

IPC mechanisms
SYS-V IPC Pipes Shared memory Semaphore Message queues Signals Threads IPC Semaphore Message queues

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

93

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

P1

fd[1] fd[0]

fd[0] fd[1]

P2

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

94

Pipe: typical usage


int fd[2]; pipe (fd); /* fd[0] for read, fd[1] for write */ /* open pipes */

if (fork()) { Used in shell: /* parent */ write (fd[1], buf, num_bytes); cat foo | grep bar } else { /* child */ read (fd[0], buf, num_bytes); }
The shell creates the pipe. Also creates two processes: cat and grep And two processes communicate through pipe.

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

95

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

96

Shared memory: example


#define KEY 0xBABADADA process1() { int shmid; char *p_addr; shmid = shmget (key, SHM_SZ, IPC_CREAT | 0666); p_addr = shmat (shmid, NULL, 0);
} #define KEY 0xBABADADA process2() { int shmid; shmid = shmget (key, SHM_SZ, 0666); p_addr = shmat (shmid, NULL, printf (%s\n, p_addr);

strcpy (p_addr, hello!); }

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

97

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

98

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

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

99

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_2 () { sem_wait Critical region sem_post }
AllGo Embedded Systems http://www.allgosystems.com
100

process_1 () { sem_wait Critical region sem_post }


darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

101

Message queue: example


#define KEY 0xBABADADA #define KEY 0xBABADADA process_producer() { char buf[MSGSZ]; msqid = msgget (KEY, flags); /* write the message */ strcpy (buf, hello!); msgsnd (msqid, buf, MSGSZ, flg); } } process_consumer() { char buf[MSGSZ]; msqid = msgget (KEY, flags); msgrcv (msqid, buf, MSGSZ, printf (%s\n, buf);

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

102

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
AllGo Embedded Systems http://www.allgosystems.com
103

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

104

Course Map
Introduction
Introduction Embedded Systems Overview System call overview Process Sub-system VM Mgt CPU Processes Threads Sched IPC/ITC shm que sem sig File Sub-system File system Mounting File Directory Links Drivers Modules Drv struct Interrupt

Conclusion Linux Kernel Overview

uClinux

Linux Sandbox Configure Build Run

Real-time system

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

105

[ 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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

107

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
AllGo Embedded Systems http://www.allgosystems.com
108

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

109

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

110

File system
Boot block: contains the boot code Super block: information about the file system Inode list: list of inodes (information about each file) Data blocks: disk blocks containing data
Boot block Super block Inode list

Data blocks

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

111

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

112

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

113

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

114

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

115

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

116

Mounting the file system


fstab File containing mount information
Device /dev/hda5 mount-point /windows/d file-type ntfs args 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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

117

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

118

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

119

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

120

Course Map
Introduction
Introduction Embedded Systems Overview System call overview Process Sub-system VM Mgt CPU Processes Threads Sched IPC/ITC shm que sem sig File Sub-system File system Mounting Directory Links Drivers Modules Drv struct Interrupt

Conclusion Linux Kernel Overview

File

uClinux

Linux Sandbox Configure Build Run

Real-time system

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

121

[ 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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

123

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

124

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

125

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

126

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

127

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
AllGo Embedded Systems http://www.allgosystems.com
128

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

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 23 open release read write ioctl

cd_open cd_close cd_read cd_write cd_ioctl

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

129

Device driver structure


.c Or system init Or init struct fops Module.o

read write open


tty_read tty_write tty_open

kmalloc

Or system shutdown

Or uninit

From Understanding Linux Kernel, Bovet & Cesati, OReilly.


AllGo Embedded Systems http://www.allgosystems.com

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

130

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

131

Using a device driver


Same file system calls: open, read, write, close open (/dev/audio, O_RDONLY)
User mode Kernel mode
Device table

open (/dev/audio, O_RDONLY) FS Inode char special major dev no.


aud_open() { }

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

132

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
AllGo Embedded Systems http://www.allgosystems.com

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

133

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; }
** : ignores kernel/user space distinciton

return; }

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

134

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

135

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

136

Course Map
Introduction
Introduction Embedded Systems Overview System call overview Process Sub-system VM Mgt CPU Processes Threads Sched IPC/ITC shm que sem sig File Sub-system File system Mounting File Directory Links Drivers Modules Drv struct Interrupt

Conclusion Linux Kernel Overview

uClinux

Linux Sandbox Configure Build Run

Real-time system

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

137

[ 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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

139

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

140

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

141

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

142

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

143

RTLinux
Run Linux as a low priority task of a hard real-time kernel (www.fsmlabs.com)

darshak.vasavada@allgosystems.com ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

144

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 ASPICES 2005, Indian Institute of Science

AllGo Embedded Systems http://www.allgosystems.com

145

Thanks!
(All is well that ends )

You might also like