Linux for Embedded Systems

download Linux for Embedded Systems

of 146

Transcript of Linux for Embedded Systems

  • 7/30/2019 Linux for Embedded Systems

    1/146

    Linux for Embedded ApplicationsJuly 2005

    Darshak Vasavada([email protected])

    AllGo Embedded Systems Pvt Ltd, Bangalore, India.

    www.allgosystems.com

  • 7/30/2019 Linux for Embedded Systems

    2/146

    2

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://www.allgosystems.com

    GNU Free Documentation License

    Permission is granted to copy, distribute and/or modify thisdocument under the terms of the GNU Free DocumentationLicense, Version 1.1 or any later version published by the FreeSoftware Foundation. A copy of this license can be found at:http://www.fsf.org/copyleft/fdl.html

    Linux for Embedded Systems

    2005, Darshak Vasavada

  • 7/30/2019 Linux for Embedded Systems

    3/146

    3

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://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

  • 7/30/2019 Linux for Embedded Systems

    4/146

    4

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://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

  • 7/30/2019 Linux for Embedded Systems

    5/146

    5

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://www.allgosystems.com

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

  • 7/30/2019 Linux for Embedded Systems

    6/146

    6

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://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

  • 7/30/2019 Linux for Embedded Systems

    7/146

    7

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://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

  • 7/30/2019 Linux for Embedded Systems

    8/146

    8

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://www.allgosystems.com

    Once upon a time ... The goals of [this] system were

    To provide a simultaneous computer access toa large community

    To supply ample computation power and datastorage

    To allow users to share their data easily, ifdesired.

    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

  • 7/30/2019 Linux for Embedded Systems

    9/146

    9

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://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 structureand ease of understanding and programming

  • 7/30/2019 Linux for Embedded Systems

    10/146

    10

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://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

  • 7/30/2019 Linux for Embedded Systems

    11/146

    11

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://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?!

  • 7/30/2019 Linux for Embedded Systems

    12/146

    12

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://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

  • 7/30/2019 Linux for Embedded Systems

    13/146

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://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

  • 7/30/2019 Linux for Embedded Systems

    14/146

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://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

  • 7/30/2019 Linux for Embedded Systems

    15/146

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://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 communicationSemaphores, shared memory

    Message queues

    Pipes

    Signals

  • 7/30/2019 Linux for Embedded Systems

    16/146

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://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 specialHDD, CD

    Device drivers

    Hardware interface

  • 7/30/2019 Linux for Embedded Systems

    17/146

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://www.allgosystems.com

    Unix architecture (source: maurice bach)User programs

    User mode

    Kernel mode

    libraries

    system call interface

    File Sub-system

    ProcessControlSub-system

    IPC

    Sched

    MMChar Block

    Device drivers

    Software

    Hardware

    hardware interface

    hardware

  • 7/30/2019 Linux for Embedded Systems

    18/146

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://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.

  • 7/30/2019 Linux for Embedded Systems

    19/146

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://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 subsystemFile systems and drivers

    Real-time performance

    Process and task switch latencies

    Interrupt latencies

    Various approaches to improve real-time performance

  • 7/30/2019 Linux for Embedded Systems

    20/146

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://www.allgosystems.com

    Course Map

    Introduction

    L

    inuxKern

    elOverview

    Process

    Sub-system

    CPU

    Introduction

    Embedded

    Systems

    Overview

    LinuxSandbox

    Configure

    Build

    Run

    System call overview

    VM Mgt

    Processes

    Threads

    IPC/ITC

    shm

    sem

    que

    sig

    Sched

    File

    Sub-system

    File system

    File

    DirectoryLinks

    Mounting

    Drivers

    Modules

    Interrupt

    Drv struct

    Conclusion

    uClinux

    Real-time

    system

  • 7/30/2019 Linux for Embedded Systems

    21/146

    [ Embedded Systems Overview ]

  • 7/30/2019 Linux for Embedded Systems

    22/146

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://www.allgosystems.com

    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

  • 7/30/2019 Linux for Embedded Systems

    23/146

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://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

  • 7/30/2019 Linux for Embedded Systems

    24/146

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://www.allgosystems.com

    ISR3:Generate EVENT3.

    ISR2:

    Transmit data.

    If buffer empty

    Generate EVENT2.

    ISR1:

    Receive data.

    If buffer fullGenerate EVENT1.

    Examplesystem_init ();

    while (1) {

    switch (event) {

    case EVENT1:

    process_data1 ();

    break;

    case EVENT2:

    produce_data2 ();

    break;

    case EVENT3:

    run_state_machine ();

    break;

    /* etc. ... */

  • 7/30/2019 Linux for Embedded Systems

    25/146

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://www.allgosystems.com

    Memory map

    Flash mapRAM map

    Initialization 1

    Boot code

    RAM image

    Vector table Initialization 2

    while (1)

    {

    ...

    |

    ISRs

    Constants

    Stack

    Data

    Vector table

  • 7/30/2019 Linux for Embedded Systems

    26/146

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://www.allgosystems.com

    Variants

    Job queues, non-preemptive scheduler Can prioritize the system activities

    The longest activity determines the system

    response

    ISR1

    ISR2

    ISR3 Scheduler

    function1

    function2

    function3

  • 7/30/2019 Linux for Embedded Systems

    27/146

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://www.allgosystems.com

    2. Systems with RTOS Complex real-time requirements

    A single activity taking longer than the fastestresponse 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

  • 7/30/2019 Linux for Embedded Systems

    28/146

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://www.allgosystems.com

    RTOS Provides tasks for asynchronous execution

    Provides mechanisms for communication andsynchronization between the tasks

    Event flags Semaphores

    Data queues

    Lets the application handle interrupts and devicesdirectly

  • 7/30/2019 Linux for Embedded Systems

    29/146

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://www.allgosystems.com

    Example

    T1 T2

    T3 Tx ISR

    Rx ISR

    DMA

    Circular buffer

    Message queue

    Semaphore

    Double buffer

  • 7/30/2019 Linux for Embedded Systems

    30/146

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://www.allgosystems.com

    Cooperative multi-tasking

    high priority

    low priority

    idle

    interrupt

    t

    scheduler

    ISRs

    Priority preemptive scheduler

  • 7/30/2019 Linux for Embedded Systems

    31/146

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://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, flagsand message queues

    Sometimes application tasks even turn off interrupts or theoperating system scheduler

  • 7/30/2019 Linux for Embedded Systems

    32/146

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://www.allgosystems.com

    Building such a system

    Compiler/

    Assembler

    Object

    files

    Run-

    time

    libraryLinker

    RAM

    image

    Source

    files

    Debug-ger RAM

    RTOSROM

    prog

    Flash

    or any

    storageLinker

    CMD

    Boot

    loader

  • 7/30/2019 Linux for Embedded Systems

    33/146

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://www.allgosystems.com

    Memory map

    Flash map

    Initialization 1

    Boot code

    RAM image

    RAM map

    Initialization 2

    RTOScode & data

    ISRs

    Constants

    Data

    Task1 code

    Task2 code

    Task3 code

    Task1 stack

    Task2 stack

    Task3 stack

  • 7/30/2019 Linux for Embedded Systems

    34/146

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://www.allgosystems.com

    Embedded system with RTOS Huge variety of these

    Home brew, free, commercial 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)

    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)

  • 7/30/2019 Linux for Embedded Systems

    35/146

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://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 onembedded devices

  • 7/30/2019 Linux for Embedded Systems

    36/146

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://www.allgosystems.com

    Development model

    App

    System calls

    Drivers

    Hardware

    App AppT1 T2

    T3 ISR

    ISR

    D

  • 7/30/2019 Linux for Embedded Systems

    37/146

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://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

  • 7/30/2019 Linux for Embedded Systems

    38/146

    [email protected] 2005, Indian Institute of Science AllGo Embedded Systemshttp://www.allgosystems.com

    System memory map

    Flash map

    Boot code

    Compressed( kernel + file

    system )

    RAM map

    RAM file system

    Monolith kernel+ device drivers (code,

    data, stack)

    PAGE FRAME

    PAGE FRAMEPAGE FRAME

    PAGE FRAME

    PAGE FRAME

    PAGE FRAME

  • 7/30/2019 Linux for Embedded Systems

    39/146

    [email protected] 2005, Indian Institute of Science AllGo Embedded Systemshttp://www.allgosystems.com

    Boot loader Resides in ROM

    Downloads the operating system intothe 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 remotehost using ftp over network

  • 7/30/2019 Linux for Embedded Systems

    40/146

    [email protected] 2005, Indian Institute of Science AllGo Embedded Systemshttp://www.allgosystems.com

    Operating system (kernel) Modules that form the kernel

    Architecture specificcode

    File system Process subsystem

    Memory management

    Networking

    Drivers Sources compiled into a

    number of .o files

    All linked together along withthe applications to form thefinal image

    A li i

  • 7/30/2019 Linux for Embedded Systems

    41/146

    [email protected] 2005, Indian Institute of Science AllGo Embedded Systemshttp://www.allgosystems.com

    Applications Applications cross compiled

    with target specific libraries

    All applications in individualexecutables stored in thestandard Unix file system

    The file system goes on thesystem as ROM/RAMFS

    Li ki i h h k l

  • 7/30/2019 Linux for Embedded Systems

    42/146

    [email protected] 2005, Indian Institute of Science AllGo Embedded Systemshttp://www.allgosystems.com

    Linking apps with the kernel How are applications linked with the kernels?

    Applications are cross compiled andexecutables 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

    P tti it ll t th I

  • 7/30/2019 Linux for Embedded Systems

    43/146

    [email protected] 2005, Indian Institute of Science AllGo Embedded Systemshttp://www.allgosystems.com

    Putting it all together - I

    CrossCompile

    Boot

    ImageBoot code

    FlashProgrammer Flash

    Boot

    Code

    P tti ll t th II

  • 7/30/2019 Linux for Embedded Systems

    44/146

    [email protected] 2005, Indian Institute of Science AllGo Embedded Systemshttp://www.allgosystems.com

    Putting all together - II

    Application

    source code Boot code

    Kernel

    sources

    Kernel

    .o files

    Apps cross compiled

    and arranged in

    Unix dir structure

    Single file

    rootfs

    rootfs.o

    Kernel + rootfs

    uImage

    gzip

    vmlinux

    link

  • 7/30/2019 Linux for Embedded Systems

    45/146

    The development environment

  • 7/30/2019 Linux for Embedded Systems

    46/146

    [email protected] 2005, Indian Institute of Science AllGo Embedded Systemshttp://www.allgosystems.com

    The development environment

    Host

    Compiler toolsBuild OS sourcesApplication sources

    ImagesDebuggerTTY

    Target

    CPU+RAM+Flash

    Boot loaderKernel + apps

    (sometimes)development tools

    JTAG,UART orEthernet

    Course Map

  • 7/30/2019 Linux for Embedded Systems

    47/146

    [email protected] 2005, Indian Institute of Science AllGo Embedded Systemshttp://www.allgosystems.com

    Course Map

    Introduction

    LinuxKerne

    lOverview

    Process

    Sub-system

    CPU

    Introduction

    EmbeddedSystems

    Overview

    Linux

    Sandbox

    Configure

    Build

    Run

    System call overview

    VM Mgt

    Processes

    Threads

    IPC/ITC

    shmsem

    quesig

    Sched

    File

    Sub-system

    File system

    File

    Directory

    Links

    Mounting

    Drivers

    Modules

    Interrupt

    Drv struct

    Conclusion

    uClinux

    Real-time

    system

    Sandbox

  • 7/30/2019 Linux for Embedded Systems

    48/146

    [email protected] 2005, Indian Institute of Science AllGo Embedded Systemshttp://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

  • 7/30/2019 Linux for Embedded Systems

    49/146

    [ Overview of the Linux kernel ]

    What is an operating system?

  • 7/30/2019 Linux for Embedded Systems

    50/146

    [email protected] 2005, Indian Institute of Science AllGo Embedded Systemshttp://www.allgosystems.com

    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)

    How does the OS come into the picture?

  • 7/30/2019 Linux for Embedded Systems

    51/146

    [email protected] 2005, Indian Institute of Science AllGo Embedded Systemshttp://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);

    How OS comes into the picture

  • 7/30/2019 Linux for Embedded Systems

    52/146

    [email protected] 2005, Indian Institute of Science AllGo Embedded Systemshttp://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 memorymanagement

    Segmentation fault, divide-by-zero exceptionsfor error handling

    The kernel, therefore, is nothing more than a set ofhandlers in the vector table!

    Processor modes

  • 7/30/2019 Linux for Embedded Systems

    53/146

    [email protected] 2005, Indian Institute of Science AllGo Embedded Systemshttp://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)

    The trap doormain (){

    int i j;

  • 7/30/2019 Linux for Embedded Systems

    54/146

    [email protected] 2005, Indian Institute of Science AllGo Embedded Systemshttp://www.allgosystems.com

    The trap door int i, j;for (i = 0; ...

    j = ...

    printf (...

    return 0;

    }

    User mode

    _syscall (WRITE, )

    Copy ARGS

    trap

    Kernel modetrap handler:

    switch (ARG[0])

    case WRITE:

    write functionality

    case READ:

    read functionality

    case FORK:

    fork functionality

    ...

    write forkread

    hardware interface

    hardware

    write (1, )

    System calls

    Er excuse me!

  • 7/30/2019 Linux for Embedded Systems

    55/146

    55

    [email protected] 2005, Indian Institute of Science AllGo Embedded Systemshttp://www.allgosystems.com

    Er, excuse me!

    main (){

    int i, j;

    for (i = 0; ...j = ...

    printf (...

    return 0;

    }interrupt handler

    Handling device

    Transmitting data

    Receiving data

    Acknowledging interrupts

    External

    Interrupt

    Interrupts

    User mode

    Kernel mode

    Times up!

  • 7/30/2019 Linux for Embedded Systems

    56/146

    56

    [email protected] 2005, Indian Institute of Science AllGo Embedded Systemshttp://www.allgosystems.com

    Time s up!

    Task scheduling

    Scheduler

    main (){

    int i, j;

    for (i = 0; ...

    j = ...printf (...

    return 0;

    }timer handler

    TimerInterrupt

    User mode

    Kernelmode

    main (){

    FILE *fp;

    fp = fopen (...,

    fscanf (fp, ...

    return 0;

    }

    You, idiot!

  • 7/30/2019 Linux for Embedded Systems

    57/146

    57

    [email protected] 2005, Indian Institute of Science AllGo Embedded Systemshttp://www.allgosystems.com

    You, idiot!

    Segmentation violation

    Divide-by-zero

    Access violation

    main (){int i, *p;

    p = NULL;

    *p = 77;

    ...

    return 0;

    }

    Exception handler

    Print the error messageTerminate the process

    User mode

    Kernelmode

    Exception:segmentationviolation

    Operating system

  • 7/30/2019 Linux for Embedded Systems

    58/146

    58

    [email protected] 2005, Indian Institute of Science AllGo Embedded Systemshttp://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?

    Unix system calls

  • 7/30/2019 Linux for Embedded Systems

    59/146

    59

    [email protected] 2005, Indian Institute of Science AllGo Embedded Systemshttp://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

    Unix architecture (source: maurice bach)

  • 7/30/2019 Linux for Embedded Systems

    60/146

    60

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://www.allgosystems.com

    User programs

    User mode

    Kernel mode

    libraries

    system call interface

    File Sub-system

    ProcessControlSub-system

    IPC

    Sched

    MMChar Block

    Device drivers

    Software

    Hardware

    hardware interface

    hardware

    PROCESSSUB-SYSTEM

    FILESUB-SYSTEM

    Course Map

  • 7/30/2019 Linux for Embedded Systems

    61/146

    61

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://www.allgosystems.com

    p

    Introduction

    Li

    nuxKerne

    lOverview

    Process

    Sub-system

    CPU

    Introduction

    EmbeddedSystems

    Overview

    Linux

    Sandbox

    Configure

    Build

    Run

    System call overview

    VM Mgt

    Processes

    Threads

    IPC/ITC

    shmsem

    quesig

    Sched

    File

    Sub-system

    File system

    File

    Directory

    Links

    Mounting

    Drivers

    Modules

    Interrupt

    Drv struct

    Conclusion

    uClinux

    Real-time

    system

  • 7/30/2019 Linux for Embedded Systems

    62/146

    [ Process sub-system ]

    What is a process?

  • 7/30/2019 Linux for Embedded Systems

    63/146

    63

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://www.allgosystems.com

    p

    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 toitself

    Processes appear to be executingsimultaneously to the end user

    Virtualization of memory

    Each process thinks it has the entire addressspace to itself

    What forms an executable?

  • 7/30/2019 Linux for Embedded Systems

    64/146

    64

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://www.allgosystems.com

    Text:

    Instruction opcodes

    Data: Global and static variables

    Global and static constants

    (tables, strings)

    Headers, symbolic information etc.

    File header

    Symbol table

    Section header

    Section data

    Section header

    Section data

    Executable file

    Example

  • 7/30/2019 Linux for Embedded Systems

    65/146

    65

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://www.allgosystems.com

    p

    File header

    Symbol table

    main = 0x1000, printf = 0x1200

    global = 0x2000

    Section text

    start = 0x1000, length = 0x1000

    Opcodes for main, printf

    global

    Section data

    start = 0x2000, length = 0x4

    string hello, world

    Section data

    start = 0x2004, length = 0x40

    EXE#include

    int global;

    main ()

    {

    int local;

    global = 1;

    local = 0;

    printf (hello, world!\n);

    return 0;

    }

  • 7/30/2019 Linux for Embedded Systems

    66/146

    Executable: example

  • 7/30/2019 Linux for Embedded Systems

    67/146

    67

    [email protected]

    ASPICES 2005, Indian Institute of Science AllGo Embedded Systemshttp://www.allgosystems.com

    int global;

    main ()

    {

    int local;

    global = 1;

    local = 0;

    printf (hello, world!\n);

    return 0;

    }

  • 7/30/2019 Linux for Embedded Systems

    68/146

    Virtualization of memory

  • 7/30/2019 Linux for Embedded Systems

    69/146

    69

    [email protected]

    ASPICES 2005, Indian Institute of Science AllGo Embedded Systemshttp://www.allgosystems.com

    Q. Is the entire process required to be loaded into the memory?

    A. The process address space can be larger than the physicalmemory 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 addresseswith the help of the processor hardware (MMU)

    Use of physical memory

  • 7/30/2019 Linux for Embedded Systems

    70/146

    70

    [email protected]

    ASPICES 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://www.allgosystems.com

    MMU

    Process P1 virtualaddress space

    P1 data

    P1 text

    Physicalmemory

    data

    stack

    text

    P11 text

    P2 text

    P3 stack

    P2 data

    P2 text

    P2 data

    P2 stack

    KERNEL

    P1 stack

    Page table

    Use of physical memory

  • 7/30/2019 Linux for Embedded Systems

    71/146

    71

    [email protected]

    ASPICES 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://www.allgosystems.com

    MMU

    Process P2 virtualaddress space

    P1 data

    P1 text

    Physicalmemory

    data

    stack

    text

    P11 text

    P2 text

    P3 stack

    P2 text

    P2 text

    P1 data

    P2 stack

    KERNEL

    P1 stack

    Page table

    Virtual address space

  • 7/30/2019 Linux for Embedded Systems

    72/146

    72

    [email protected]

    ASPICES 2005, Indian Institute of Science

    AllGo Embedded Systems

    http://www.allgosystems.com

    Text

    Data

    Stack

    00000000

    FFFFFFFF

    0x100 add r0,r1

    0x101 st r0,(r3)

    0x102 jmp 0x110

    Text

    Data

    Stack

    0x100 mpy r7,r3

    0x101 add r7,r2

    0x102 sub r3,r0

    0x10000 11 22 33 44

    0x10004 55 66 77 88

    0x10008 99 aa bb cc

    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?

    P1 P2

    Example

  • 7/30/2019 Linux for Embedded Systems

    73/146

    73

    [email protected]

    ASPICES 2005, Indian Institute of Science

    AllGo Embedded Systems

    http://www.allgosystems.com

    #include #include

    int num;

    main (int argc, char *argv[])

    {

    num = atoi (argv[1]);

    while (1)

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

    return 0;}

    Advantages of virtual memory

  • 7/30/2019 Linux for Embedded Systems

    74/146

    74

    [email protected]

    ASPICES 2005, Indian Institute of Science

    AllGo Embedded Systems

    http://www.allgosystems.com

    The whole process does not have to be loaded inthe physical memory; only the parts which arerequired can be loaded.

    Multiple processes can simultaneously occupyparts of physical memory.

    Pages

  • 7/30/2019 Linux for Embedded Systems

    75/146

    75

    [email protected]

    ASPICES 2005, Indian Institute of Science

    AllGo Embedded Systems

    http://www.allgosystems.com

    Virtual page: a block of memory in the virtualaddress space

    Logical page: a block of memory that contains codeor data. More than one virtual pages can map to asingle logical page.

    Physical frame: a block of physical memory towhich a logical page is mapped.

    Pages

  • 7/30/2019 Linux for Embedded Systems

    76/146

    76

    [email protected]

    ASPICES 2005, Indian Institute of Science

    AllGo Embedded Systems

    http://www.allgosystems.com

    4k

    4k

    8k

    text

    data

    stack

    Proc

    A

    4k

    4k

    8k

    text

    data

    stack

    Proc

    B

    virtualpages logicalpagesphysicalframes

    regions

    How is a process created?

  • 7/30/2019 Linux for Embedded Systems

    77/146

    77

    [email protected]

    ASPICES 2005, Indian Institute of Science

    AllGo Embedded Systems

    http://www.allgosystems.com

    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 andgrand-children and great-grand children etc. of init

    Process related system calls

  • 7/30/2019 Linux for Embedded Systems

    78/146

    78

    [email protected]

    ASPICES 2005, Indian Institute of Science

    AllGo Embedded Systems

    http://www.allgosystems.com

    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

    Example

  • 7/30/2019 Linux for Embedded Systems

    79/146

    79

    [email protected]

    ASPICES 2005, Indian Institute of Science

    AllGo Embedded Systems

    http://www.allgosystems.com

    What happens when you run a command from ashell (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 copy of bash running

    exec (/bin/ls,

    ls runningbash waiting

    bash calls wait

    ls calls exit

    bash continues running

    bash comes out of wait

    fork example

  • 7/30/2019 Linux for Embedded Systems

    80/146

    80

    [email protected]

    ASPICES 2005, Indian Institute of Science

    AllGo Embedded Systems

    http://www.allgosystems.com

    if ((pid = fork()) == 0){

    while (1)

    printf (child\n);

    }

    else

    {

    while (1)

    printf (parent of %d\n, pid);

    }

    exec example

  • 7/30/2019 Linux for Embedded Systems

    81/146

    81

    [email protected]

    ASPICES 2005, Indian Institute of Science

    AllGo Embedded Systems

    http://www.allgosystems.com

    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?

    Context switch

  • 7/30/2019 Linux for Embedded Systems

    82/146

    82

    [email protected]

    ASPICES 2005, Indian Institute of Science

    AllGo Embedded Systems

    http://www.allgosystems.com

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

    Light weight processes

  • 7/30/2019 Linux for Embedded Systems

    83/146

    83

    [email protected]

    ASPICES 2005, Indian Institute of Science

    AllGo Embedded Systems

    http://www.allgosystems.com

    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

    Thread

  • 7/30/2019 Linux for Embedded Systems

    84/146

    84

    [email protected]

    ASPICES 2005, Indian Institute of Science

    AllGo Embedded Systems

    http://www.allgosystems.com

    A function running asynchronously Example usage

    int global;

    f1()

    {thread_create (f2, ...);

    global = 5;

    ...

    }f2()

    {

    ...

    global = 7;...

    f1

    thread create

    f1 continues

    running

    f2 starts

    running

    Options in light weight

  • 7/30/2019 Linux for Embedded Systems

    85/146

    85

    [email protected]

    ASPICES 2005, Indian Institute of Science

    AllGo Embedded Systems

    http://www.allgosystems.com

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

    System calls

  • 7/30/2019 Linux for Embedded Systems

    86/146

    86

    [email protected]

    ASPICES 2005, Indian Institute of Science

    AllGo Embedded Systems

    http://www.allgosystems.com

    pthreads: available as a library libpthread.a pthread_create

    pthread_join

    pthread_cancel Light weight process: clone

    Kernel threads: kernel_thread

    pthread example

  • 7/30/2019 Linux for Embedded Systems

    87/146

    87

    [email protected]

    ASPICES 2005, Indian Institute of Science

    AllGo Embedded Systems

    http://www.allgosystems.com

    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;

    }

  • 7/30/2019 Linux for Embedded Systems

    88/146

    Process and threads

  • 7/30/2019 Linux for Embedded Systems

    89/146

    89

    [email protected]

    ASPICES 2005, Indian Institute of Science

    AllGo Embedded Systems

    http://www.allgosystems.com

    Multi-level priority scheduling

  • 7/30/2019 Linux for Embedded Systems

    90/146

    90

    [email protected]

    ASPICES 2005, Indian Institute of Science

    AllGo Embedded Systems

    http://www.allgosystems.com

    priority

    levels

    Level 1

    Level 2

    Level 3

    Level N Zzz Zzz

    Zzz

    Zzz

    Scheduling - notes

    Multi level priority scheduling

  • 7/30/2019 Linux for Embedded Systems

    91/146

    91

    [email protected]

    ASPICES 2005, Indian Institute of Science

    AllGo Embedded Systems

    http://www.allgosystems.com

    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

    Process states

  • 7/30/2019 Linux for Embedded Systems

    92/146

    92

    [email protected]

    ASPICES 2005, Indian Institute of Science

    AllGo Embedded Systems

    http://www.allgosystems.com

    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

    IPC mechanisms

    SYS V IPC

  • 7/30/2019 Linux for Embedded Systems

    93/146

    93

    [email protected]

    ASPICES 2005, Indian Institute of Science

    AllGo Embedded Systems

    http://www.allgosystems.com

    SYS-V IPC Pipes

    Shared memory

    Semaphore Message queues

    Signals

    Threads IPC Semaphore

    Message queues

  • 7/30/2019 Linux for Embedded Systems

    94/146

    Pipe: typical usage

    int fd[2]; /* fd[0] for read fd[1] for write */

  • 7/30/2019 Linux for Embedded Systems

    95/146

    95

    [email protected]

    ASPICES 2005, Indian Institute of Science

    AllGo Embedded Systems

    http://www.allgosystems.com

    int fd[2]; /* fd[0] for read, fd[1] for write */

    pipe (fd); /* open pipes */

    if (fork()) {

    /* parent */

    write (fd[1], buf, num_bytes);

    } else {

    /* child */

    read (fd[0], buf, num_bytes);

    }

    Used in shell:

    cat foo | grep bar

    The shell creates the pipe.

    Also creates two processes:cat and grep

    And two processescommunicate through pipe.

    Shared memory

    Mechanism to share memory between two

  • 7/30/2019 Linux for Embedded Systems

    96/146

    96

    [email protected]

    ASPICES 2005, Indian Institute of Science

    AllGo Embedded Systems

    http://www.allgosystems.com

    Mechanism to share memory between twounrelated 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?

    Shared memory: example

    #define KEY 0xBABADADA

  • 7/30/2019 Linux for Embedded Systems

    97/146

    97

    [email protected]

    ASPICES 2005, Indian Institute of Science

    AllGo Embedded Systems

    http://www.allgosystems.com

    #

    process1()

    {

    int shmid;

    char *p_addr;

    shmid = shmget (key, SHM_SZ,

    IPC_CREAT | 0666);

    p_addr = shmat (shmid, NULL,0);

    strcpy (p_addr, hello!);

    }

    #define KEY 0xBABADADA

    process2()

    {

    int shmid;

    shmid = shmget (key, SHM_SZ,

    0666);

    p_addr = shmat (shmid, NULL,

    printf (%s\n, p_addr);

    }

    Semaphores

    A mechanism for synchronization & mutualexclusion

  • 7/30/2019 Linux for Embedded Systems

    98/146

    98

    [email protected]

    ASPICES 2005, Indian Institute of Science

    AllGo Embedded Systems

    http://www.allgosystems.com

    A mechanism for synchronization & mutualexclusion

    System calls:

    semget: get/create a semaphore

    semop: operate (post/wait) on a semaphore0 = 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

    Semaphore: synchronization

    #define KEY 0xBABADADA#

  • 7/30/2019 Linux for Embedded Systems

    99/146

    99

    [email protected]

    ASPICES 2005, Indian Institute of Science

    AllGo Embedded Systems

    http://www.allgosystems.com

    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);}

    Semaphore: mutual exclusion

    Two processes share common resource whichrequires entry regulation

  • 7/30/2019 Linux for Embedded Systems

    100/146

    100

    [email protected]

    ASPICES 2005, Indian Institute of Science

    AllGo Embedded Systems

    http://www.allgosystems.com

    Two processes share common resource whichrequires entry regulation

    Buffers, linked lists etc.

    Necessary because the process switch can occurat any time

    process_1 () process_2 ()

    { {

    sem_wait sem_wait

    Critical region Critical region

    sem_post sem_post} }

    Message queues

    Passing messages from one task to another

  • 7/30/2019 Linux for Embedded Systems

    101/146

    101

    [email protected]

    ASPICES 2005, Indian Institute of Science

    AllGo Embedded Systems

    http://www.allgosystems.com

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

    Message queue: example

    #define KEY 0xBABADADA#define KEY 0xBABADADA

  • 7/30/2019 Linux for Embedded Systems

    102/146

    102

    [email protected]

    ASPICES 2005, Indian Institute of Science

    AllGo Embedded Systems

    http://www.allgosystems.com

    process_producer()

    {

    char buf[MSGSZ];

    msqid = msgget (KEY, flags);

    /* write the message */

    strcpy (buf, hello!);

    msgsnd (msqid, buf, MSGSZ, flg);

    }

    #define KEY 0xBABADADA

    process_consumer()

    {

    char buf[MSGSZ];

    msqid = msgget (KEY, flags);

    msgrcv (msqid, buf, MSGSZ,

    printf (%s\n, buf);

    }

    Signals

    Asynchronous notification to a process about anevent

  • 7/30/2019 Linux for Embedded Systems

    103/146

    103

    [email protected]

    ASPICES 2005, Indian Institute of Science

    AllGo Embedded Systems

    http://www.allgosystems.com

    y pevent

    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

    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

    Example

    void sigint_handler (){

  • 7/30/2019 Linux for Embedded Systems

    104/146

    104

    [email protected]

    ASPICES 2005, Indian Institute of Science

    AllGo Embedded Systems

    http://www.allgosystems.com

    {

    signal (SIGINT, sigint_handler);

    printf (You pressed ^C!\n);

    }

    main ()

    {

    signal (SIGINT, sigint_handler);

    while (1)

    ;

    }

    Quiz: How will you stop the above program?

    Course Map

    Introduction System call overview Conclusion

  • 7/30/2019 Linux for Embedded Systems

    105/146

    105

    [email protected]

    ASPICES 2005, Indian Institute of Science

    AllGo Embedded Systems

    http://www.allgosystems.com

    Lin

    uxKernel

    Overview

    Process

    Sub-system

    CPU

    Introduction

    Embedded

    Systems

    Overview

    Linux

    Sandbox

    Configure

    Build

    Run

    VM Mgt

    Processes

    Threads

    IPC/ITC

    shm

    sem

    que

    sig

    Sched

    File

    Sub-system

    File system

    File

    Directory

    Links

    Mounting

    Drivers

    Modules

    Interrupt

    Drv struct

    uClinux

    Real-time

    system

  • 7/30/2019 Linux for Embedded Systems

    106/146

    [ file sub-system ]

    Unix file system

    Hierarchical file system Root directory

  • 7/30/2019 Linux for Embedded Systems

    107/146

    107

    [email protected]

    ASPICES 2005, Indian Institute of Science

    AllGo Embedded Systems

    http://www.allgosystems.com

    Root directory

    Directories with files

    A number of file systems mounted within /

    The file system to a user

    Multi-user file system Hierarchical directory structure starting from root

  • 7/30/2019 Linux for Embedded Systems

    108/146

    108

    [email protected]

    ASPICES 2005, Indian Institute of Science

    AllGo Embedded Systems

    http://www.allgosystems.com

    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

    The file system to a programmer

    System calls Accessing files: creat open read write close

  • 7/30/2019 Linux for Embedded Systems

    109/146

    109

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systems

    http://www.allgosystems.com

    Accessing files: creat, open, read, write, close

    Changing file attributes: chown, chmod

    Mounting, unmounting devices: mount, umount Changing root: chroot

    The virtual file system

    Any types of file systems and devices can bemounted seamlessly.

  • 7/30/2019 Linux for Embedded Systems

    110/146

    110

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systems

    http://www.allgosystems.com

    y

    The programmer does not have to worry about thefile system type.

    The programs written in the same way, with open,close, read, write etc. function calls.

    VFS implemented with function pointers. A new FShas to provide body of these functions.

    File system

    Boot block: contains the boot code Super block: information about the

    Boot block

  • 7/30/2019 Linux for Embedded Systems

    111/146

    111

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systems

    http://www.allgosystems.com

    Super block: information about thefile system

    Inode list: list of inodes

    (information about each file)

    Data blocks: disk blockscontaining data

    Super block

    Inode list

    Data blocks

    Files: internal view

    Inodes File owner

  • 7/30/2019 Linux for Embedded Systems

    112/146

    112

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systems

    http://www.allgosystems.com

    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

  • 7/30/2019 Linux for Embedded Systems

    113/146

    Super-block

    The structure containing the information about thefile system

  • 7/30/2019 Linux for Embedded Systems

    114/146

    114

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systems

    http://www.allgosystems.com

    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)

    Mounting a file system

    mount: device file system Mount table

  • 7/30/2019 Linux for Embedded Systems

    115/146

    115

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systems

    http://www.allgosystems.com

    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

    mount

    mount (/dev/disk1, /usr, 0); Check if super-user

  • 7/30/2019 Linux for Embedded Systems

    116/146

    116

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systems

    http://www.allgosystems.com

    p

    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 mountpoint

    Mounting the file system

    fstab File containing mount information

  • 7/30/2019 Linux for Embedded Systems

    117/146

    117

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systems

    http://www.allgosystems.com

    Device mount-point file-type args

    /dev/hda5 /windows/d ntfs ro 0 0

    Mount/umount commands to mount and unmountthe file systems

    # mount t ntfs /dev/hda5 /windows/s

    Automount: mount on use

    File system calls

    open: open a file for reading/writing close: close a file

  • 7/30/2019 Linux for Embedded Systems

    118/146

    118

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://www.allgosystems.com

    creat: create a file

    chown, chmod, chgrp: change permissions

    link, unlink: create/remove a link

    Hard and symbolic links Hard link: same inode but additional directory entry

    Example:

    $ ln file1 file2

  • 7/30/2019 Linux for Embedded Systems

    119/146

    119

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://www.allgosystems.com

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

    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?

    Example: busyboxmain (char *argv[], int argc) {

    if (argv[0] equals ls) do_ls ();

    if ( [0] l ) d ()

  • 7/30/2019 Linux for Embedded Systems

    120/146

    120

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://www.allgosystems.com

    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)

    Course Map

    Introduction

    ProcessIntrod ction

    System call overview

    File

    Conclusion

  • 7/30/2019 Linux for Embedded Systems

    121/146

    121

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://www.allgosystems.com

    LinuxKernelOverview

    Process

    Sub-system

    CPU

    Introduction

    Embedded

    Systems

    Overview

    Linux

    Sandbox

    Configure

    Build

    Run

    VM Mgt

    Processes

    Threads

    IPC/ITC

    shm

    sem

    que

    sig

    Sched

    File

    Sub-system

    File system

    File

    Directory

    Links

    Mounting

    Drivers

    Modules

    Interrupt

    Drv struct

    uClinux

    Real-time

    system

  • 7/30/2019 Linux for Embedded Systems

    122/146

    [ IO sub-system ]

    Loadable modules

    Module is a .o file that can be dynamically loadedand linked into the kernel

    Once loaded becomes a part of the kernel

  • 7/30/2019 Linux for Embedded Systems

    123/146

    123

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://www.allgosystems.com

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

    A simple module*#define MODULE

    #include

    int init module(void)

  • 7/30/2019 Linux for Embedded Systems

    124/146

    124

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://www.allgosystems.com

    int init_module(void)

    {

    printk("Hello, world\n");return 0;

    }

    void cleanup_module(void)

    {

    printk("Goodbye cruel world\n");

    }

    * For Linux v2.4.

    How to insert the module? (#)

    gcc c hello.c Creates hello.o

    i d h ll

  • 7/30/2019 Linux for Embedded Systems

    125/146

    125

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://www.allgosystems.com

    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

    Device

    Block device: hard disk, CDROM, SCSI Character device: TTY, I/O ports

    H fil ( /d / di )

  • 7/30/2019 Linux for Embedded Systems

    126/146

    126

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://www.allgosystems.com

    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

    Device usage: example#include

    main ()

    {

  • 7/30/2019 Linux for Embedded Systems

    127/146

    127

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://www.allgosystems.com

    {

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

    }}

    Device driver

    A set of functions to access a device init, read, write, open, release, ioctl,

    Runs as a part of the kernel

  • 7/30/2019 Linux for Embedded Systems

    128/146

    128

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://www.allgosystems.com

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

    Statically linked into the kernel (build time)

    Dynamically insterted as a module (run time)

    Registering a device

    Device functions are added into the devicetable

    Character device driver

    Driver functions (file operations, fops for short) init: one time device initialization

    open: first time device init allocate resources

  • 7/30/2019 Linux for Embedded Systems

    129/146

    129

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://www.allgosystems.com

    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

    cd_ioctlcd_writecd_readcd_closecd_open23

    ioctlwritereadreleaseopenNo

  • 7/30/2019 Linux for Embedded Systems

    130/146

    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

  • 7/30/2019 Linux for Embedded Systems

    131/146

    131

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://www.allgosystems.com

    2. These functions are statically linked with the kernelor 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 number4. Create the device node (file) in /dev

    mknod

    Creates a file in /dev, e.g. /dev/audio

  • 7/30/2019 Linux for Embedded Systems

    132/146

    Interrupt handling Request IRQ

    A module should request an IRQ

    request_irq (irq, handler, flags, );

  • 7/30/2019 Linux for Embedded Systems

    133/146

    133

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://www.allgosystems.com

    q _ q ( q, , g , );

    Free IRQ

    Free-up the IRQ Writing a handler

    Communication with the device

    Read/write into the device registersClear the interrupt pending register

    Communication with the driver functions

    Data through buffers

    Synchronization through kernel events

    Driver ISR synchronizationchar *ptr;

    dev_read (, char *buf, int count, )

    {

  • 7/30/2019 Linux for Embedded Systems

    134/146

    134

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://www.allgosystems.com

    ptr = buf; n = 0;

    while (n < count){

    wait_event ();

    n++;

    }

    return;

    }

    dev_irq

    {ch = READ_PORT();

    *ptr++ = ch; /* ** */

    wake_up ();

    return;

    }

    ** : ignores kernel/user space distinciton

    Tasklets Issues in ISRs

    Too long to be in the interrupt context

    (Would disable other interrupts/tasks)

  • 7/30/2019 Linux for Embedded Systems

    135/146

    135

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://www.allgosystems.com

    (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 theISR context

    Bottom half executed in kernel at a later point Tasklets

    DECLARE_TASKLET (name, function, data);

    Schedule from an ISR using tasklet_schedule().

    Tasklet examplevoid interrupt_handler (void)

    {

    /* do device related processing */

  • 7/30/2019 Linux for Embedded Systems

    136/146

    136

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://www.allgosystems.com

    ...

    /* schedule the tasklet */

    tasklet_schedule (tasklet_function);

    }

    void tasklet_function (void)

    {

    /* tasklet functionality */

    }

    Course Map

    Introduction

    Li

    Process

    Sub-systemIntroduction

    System call overview

    File

    Sub-system

    Conclusion

  • 7/30/2019 Linux for Embedded Systems

    137/146

    137

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://www.allgosystems.com

    inu

    xKernelO

    verview

    S y

    CPU

    Embedded

    SystemsOverview

    Linux

    Sandbox

    Configure

    Build

    Run

    VM Mgt

    Processes

    Threads

    IPC/ITC

    shm

    sem

    que

    sig

    Sched

    S y

    File system

    File

    Directory

    Links

    Mounting

    Drivers

    Modules

    Interrupt

    Drv struct

    uClinux

    Real-time

    system

  • 7/30/2019 Linux for Embedded Systems

    138/146

    [ uClinux and real-time Linux ]

  • 7/30/2019 Linux for Embedded Systems

    139/146

    Consequences of having no MMU

    Program loading

    Either the kernel has to fix-up the addresses

    Run-time overhead on the kernel

  • 7/30/2019 Linux for Embedded Systems

    140/146

    140

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://www.allgosystems.com

    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

    Consequences of having no MMU

    Process creation with vfork

    fork() would have to copy the whole process

    Instead, expects exec after fork()

  • 7/30/2019 Linux for Embedded Systems

    141/146

    141

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://www.allgosystems.com

    Parent waits until the child does exec()

    Until then, child shares everything, includingstack

    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

    Linux and real-time Real-time requirements

    Deterministic interrupt response

    Deterministic process response

  • 7/30/2019 Linux for Embedded Systems

    142/146

    142

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://www.allgosystems.com

    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

    Approaches Run the time-critical activities on a separate

    processor:

    Micro (OS, GUI, networking) with

    DSP ( i l i d f t i t t )

  • 7/30/2019 Linux for Embedded Systems

    143/146

    143

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://www.allgosystems.com

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

    Run Linux over a small RTOS

    Improve Linux scheduler

  • 7/30/2019 Linux for Embedded Systems

    144/146

    Improvements to kernelReal-time framework within the kernel (e.g. KURT)

    Real-time event handling modules

    RTmods (real-time modules): parts of applicationwhich require real time performance

  • 7/30/2019 Linux for Embedded Systems

    145/146

    145

    [email protected] 2005, Indian Institute of Science

    AllGo Embedded Systemshttp://www.allgosystems.com

    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 onfairness

  • 7/30/2019 Linux for Embedded Systems

    146/146

    (All is well that ends )

    Thanks!