Embedded Systems and Applications
Mail: Stephane.Lavirotte@unice.fr Web: http://stephane.lavirotte.com/
Université de Nice - Sophia Antipolis
Lecture Presentation
Embedded Systems and
Applications
Lecture Objectives and Organization
9 Purpose:
– Deepening the knowledge on Operating Systems – Have an “inside” system vision
– Studying the different components of an OS
9 Focus on testing
– Illustration and case studies on Linux
9 Applied to Embedded Systems
– And to embedded Software also
9 Organization
– Lecture (1/3)
– Practical Lab (2/3)
Embedded Systems
9 Systems and Critical Systems
– Business critical Î time-to-market
Phone, audio, TV, DVD, games, … – Mission critical Î highest quality
Trajectory et altitude, imagery, communications, … – Life critical Î validation et certification
Pacemakers, insulin pump, surgeon robot, … – Safety critical Î validation et certification
Piloting, brake, electronic distribution, …
9 Different kind of constraints
– Everything can be seen as critical
– We will not process validation in this lecture
Design and Validation of Critical Systems
Embedded Systems and Applications Lectures
9 Operating System Architecture
– Architecture and Introduction
– Configuring Linux and System Calls
– Modules: adding new functionalities to kernel – Device drivers
9 Embedded Systems (“High Level”)
– Building an embedded system – Optimizing an embedded system
Bibliography
9 Books
– D. Bovet and M. Cesati, Understanding the Linux Kernel (2ème édition), O'Reilly 2002.
– J. Corbet , A. Rubini and G. Kroah-Hartman, Linux Device Drivers 3rd edition, O'Reilly 2001. http://lwn.net/Kernel/LDD3/
9 Online Resources
– http://www.tldp.org/guides.html,
Gerard Beekmans, Linux From Scratch, Nov 2005.
Peter Jay Salzman, Michael Burian, Ori Pomerantz, The Linux Kernel Module Programming Guide, Kernel 2.4 et 2.6, Jul 2004.
Tigran Aivazian, Linux Kernel 2.4 Internals, Aug 2002.
David A. Rusling, The Linux Kernel, Jan 1998.
Michael K. Johnson, The Linux Kernel Hackers' Guide, Aug 1998.
– http://www.linuxhq.com/lkprogram.html 9 Courses and Tutorials
– Embedded Linux: http://free-electrons.com/
9 Kernel Documentation
– /usr/src/linux/Documentation/…
– And “may the Sources be with you !!!” (grep, (e|c)tags, …
Acknowledgement
9 This lecture was mde with the following sources :
– Cours et exercices « Systèmes d’Exploitation Avancés »
Erick Gallesio and Jean-Paul Rigault (1999-2004)
Stéphane Lavirotte and Jean-Yves Tigli (2004-2008) – Cours et exercices
Michael Opdenacker – Free Electrons (2004-2008) http://free-electrons.com/
9 Thanks to:
– Erick Gallesio
– Fabrice Huet, Olivier – Michael Opdenacker – Jean-Paul Rigault – Jean-Yves Tigli
Introduction and Architecture of an Operating System
Mail: Stephane.Lavirotte@unice.fr Web: http://stephane.lavirotte.com/
Université de Nice - Sophia Antipolis
Stéphane Lavirotte
An OS: What for?
9 Features of an Operating System
– Serving requests for processes
System Calls: Read, Write, Open, …
– Handle exceptions caused by physical processes
Rerouting: Division by 0, stack overflow, … – Manage hardware interrupts
Interrupts : keyboard / mouse, network, … – Provide a set of specific services
Ensure maintenance tasks system
– Memory paging management – Swap, and caches management – File systems management
– …
Architecture Overview
9 Layer Vision:
Source: Linux, Pilotes de Périphériques (3rdedition)
Hardware
Architecture Overview
9 A contemporary vision
Common Services
VFS Interface
Scheduler Streams
Pilotes Block
Exec.
Virtual Memory
EXT3
NFS VFAT
System Processes Real Time Harddrive
driver
Tape Driver Mapping
files Mapping
devices.
elf a.out
Some reminders about Unix
Reminders
Root File System
9 /bin : Basic programs for all users 9 /boot: Image files for kernel boot 9 /dev: Special files to access devices
9 /etc: Configuration files for systems and applications 9 /home: Users’ directory of the machine
9 /lib: Shared libraries for system startup
9 /mnt: Mount points for temporary partitions
9 /proc: Virtual files; reflect the operation of the system 9 /root: Account for the super user
9 /sys: « Ditto » to /proc starting from 2.6 9 /tmp: Temporary files
9 /usr: Secondary hierarchy for applications 9 /var: Variable files (log, cache, …)
Files
9 Everything is files
– Data Storage
– Access devices via /dev
/dev/hdaX, /dev/hdbX, … : IDE hard drive
/dev/sdaX, /dev/sdbX, … : SCSI hard drive, USB, …
/dev/fdX, /dev/fdX, … : floppy disk
/dev/videoX : graphics card graphique, TV tuner card, …
/dev/i2cX : access to components connected I2C bus
/dev/null, /dev/zero : destruction or generation of zero
9 Unless the network access
– /sbin/ifconfig ethX
– Configuration via /etc/network/interfaces or via ifconfig
9 Information about devices connected on the bus
– lspci, lsusb, lspcmcia, … (also useful: lsinput)
Memory Management 1/3
9 Unix relies on virtual memory
9 Processes do not have direct references to physical addresses
– Each process has its virtual address space
9 The virtual addresses are translated into physical addresses
– The MMU circuit operates to convert each reference
Using a set of records that describe the conversion tables of the current process
A conversion table is associated with each process – When changing context (= current process)
load the MMU registers with new addresses tables
Memory Management 2/3
9 Using virtual memory with 2 other techniques
– Pagination
The memory is divided into pieces
The allocation is done by a whole number of pages
Few external fragmentation (free space cut into small pieces)
Internal fragmentation (to many allocations)
Requires a hardware MMU – Segmentation
Used to isolate pieces of memory
They are not accessible by the running process
Use of hardware registers
If not respected: segmentation fault
9 These two techniques allow a memory protection
– A process can not read/write memory of another
Memory Management 3/3
9 Distinction between the memory used by the kernel and the one used by the processes
9 Kernel Space
– kernel memory area – never put in swap
9 User Space
– memory area for execution of user programs
– to access the kernel space program user must go through system calls.
Processes Memory
9 The memory of a process is divided into 3 zones
– Stack: automatically grow – Data : explicit grow
– Text : fixed
9 Used by most language
– In particular, the C language
So used inside the kernel
– Some exceptions : Fortran77, ...
9 Each process has (at least) one
– It helps to remember the call parameters of functions
Allows recursive calls
– When the function execution ends
Withdrawal call parameters
Stacking of the return value
Text Data Stack
Installing New Software
9 Methode “for the brave”
– Get package .tar.gz
– With luck, configured with automake
– ./configure ; make ; make install
9 Package distribution
– Major formats :
.deb (Debian, Ubuntu, ...)
.rpm (RedHat, Mandriva, ...)
9 Intelligent installation
– Tools for major distributions
.deb : dpkg, dselect, aptitude, …
.rpm : rpm
Packages Management
9 Despite dependencies, conflicts may exist 9 Dependency information often inadequate
– RPM: simply claim without specifying where to look
9 Solution : packages repository
– Website consisting of many packages
Consistent with each
Regular updates – Update command
apt(Debian)
yum/up2date (RedHat)
synaptic: works with the 2 formats
– Lauch : synaptic
Some useful Utilities
9 wget
– Get files from Internet: wget url
9 gpg
– Verify file integrity: gpg --verify file.sign file.bz2
9 patch
– Apply a patch to sources : patch –pn < file.diff
9 mount
– Attach a new partition: mount [–t type] device
9 make
– Compile sources: make target
– Time optimization: make –j 4 (faster even on a single proc.)
9 dmesg
– Access to kernel messages: dmesg
Have a Look to Messages !
9 When it does not work, let’s have a look to logs !!
– Files are stored in /var/log
9 As root, launch
– tail -f /var/log/messages
9 Syslogd Daemon
– File /etc/syslog.conf
Lines formed of couples Selector / Action
– Simple selector : facility.priority
• Notice: priority means level >= priority
• Ex : mail.notice messages on the mail for all levels of emergency to record (except info and debug)
– Extended selector
• facility1,facility2.priority
• facility.=priority; facility.!=priority … – Action :
• /path/to/a/log/file, @machine.domaine,
• user1,user2, * : wall(1) (all those connected) ...
Syslog Configuration
9 All critical messages, except the kernel ones in a dedicated file
.=crit;kern.none /var/adm/critical
9 All kernel messages to a dedicated file
kern.* /var/adm/kernel
9 All kernel messages are sent to another machine and to the console
kern.crit @finlandia kern.crit /dev/console
9 All the kernel messages (from info to critical) to a dedicated file
kern.info;kern.!err /var/adm/kern.info
9 Messages of priority mail info to a tty, the other to a file
mail.=info /dev/tty12
mail.*;mail.!=info /var/adm/mail
Since the boot...
« Boot » a System
Steps to Loading System
9 Loading several successive programs
– Load the kernel is complicated and can not be achieved in a single operation
Too large to lodge in ROM
The program loading kernel itself is a program that is loaded with a program loader!
– Loading the kernel is customizable
Floppy, CR-ROM or hard drive only
Floppy or CD-ROM then hard drive
Network
...
Boot Steps
The Boot Sequence : BIOS
9 Each processor initializes
– Self-test
– Multi-pro: possible election of a CPU leader
9 CPU leader executes instruction at address 0xfffffff0 9 Instruction at 0xfffffff0
– Jump to the start of the BIOS program(Basic Input /Output System)
9 BIOS: POST (Power On Self Test)
9 BIOS: Selecting a boot device
The Boot Sequence : MBR
9 BIOS: Load MBR (Master Boot Record) from the boot device
– 1st sector (512 bytes) of the device
9 BIOS: inspect MBR:
– Verifications (magic number, partition table) – Search the boot sector
– Beware the crash if multiple OS on the machine
9 BIOS: load the boot sector
– Load the beginning of the Linux loader program (Lilo, Grub) or the loader of another OS
Boot Loader Partition Table
Windows
Win Bootloader
OS 2 Linux
Partition 1 (hda1 ou hd0,0) Partition 2 Partition 3 MBR
OS Bootloader Boot Sector
The Boot Sequence: LILO
9 Either
– The boot sector program end the LILO loading (MBR LILO) – The boot sector starts loading the loader another OS that will
load the first sector of LILO
1st LILO sector installed on the second boot sector (at the beginning of the Linux partition)
We are the in the same situation, LILO finishes loading
9 LILO: loads the kernel and starts execution
– Possible options to configure the kernel loading
Will see this later
Kernel Loader : LILO or GRUB
9 LILO (LInux LOader)
– Historical loader
– Ad hoc tool for Linux (targets: Linux or Other) – A few annoying limitations
First sector should on a cylinder < 1024 – Not really used anymore
9 GRUB (GRand Unified Bootloader)
– The GNU/FSF generic loader
– Natively support multiple OS (Linux, *BSD, ...) – Many improvements (« micro shell », ...)
Installation de LILO
9 Configuration File /etc/lilo.conf
– Common section
boot = <device> : installation on MBR or secondary boot
delay = <n> : choice delay (10th of seconds)
vga = <mode> : display mode at startup – Linux section
image = <kernel_file> : file containing kernel
label = <label> : choice name in the LILO menu
root = <dev_file> : root partition
read-only = first mount root read-only – Other OS section
other = <dev_file> : Other OS partition
label = <etiquette> : choice name in the LILO menu
/etc/lilo.conf
# Common section boot=/dev/hda delay=40
default=linux vga=normal
# Linux section
image=/boot/vmlinuz-2.4.20-8 label=linux
root=/dev/hda3 read-only
image=/boot/vmlinuz-2.4.27 label=linux2
root=/dev/hda3
# Other OS section OS other=/dev/hda1
label=XP
Must validate modifications :
run the command
/sbin/liloGRUB Installation 1/2
9 Configuration file : /boot/grub/grub.conf
– Similarity with LILO
Common section, Entry 1, Entry 2, Chained entry (like other) – Major differences
Identifying disks and partitions
No need to validate the changes as with lilo
9 Identifying disks and partitions
– Disk : hdn avec n = 0, 1, 2, ...
– Uniq numberingfor IDE and SCSI
Numbering in the detection order – Partition : (hdn, x) with x = 0, 1, 2, ...
Primary partitions : 0 to 3
Logical partitions : from 4
GRUB Installation 2/2
9 Initial installation
– Sopy 'stage' file on the 'boot-device'
mount /dev/hda3 /mnt
mkdir -p /mnt/boot/grub
cd /usr/share/grub/i386-pc
cp stage[12] *stage1_5 /mnt/boot/grub – Installation
umount /mnt
Manually with grub
– grub> root (hd0, 2) – grub> setup (hd0) – grub> quit
“Auto-magically” with grub
– grub-install (RedHat)
/boot/grub/grub.conf
# Common section timeout 10
default 0
# First entry
title Red Hat Linux root (hd0,0)
kernel =/boot/vmlinuz-2.4.20-8 ro root=LABEL=/
initrd /boot/initrd-2.4.20-8.img
# Second entry title Windows XP
root(hd1,1) make-active chainloader+1
Boot Loader for Embedded Systems
9 Uboot: Universal Bootloader.
– The most used on arm architecture – http://uboot.sourceforge.net/
– Supported hardware: arm, ppc, mips, x86
9 RedBoot: Bootloader based on eCos (RedHat)
– http://sources.redhat.com/redboot/
– Supported hardware: x86, arm, ppc, mips, sh, m68k...
9 LAB: Linux As Bootloader (Handhelds.org)
– Integrated in the Linux kernel distributed by Handhelds.org – See:
http://handhelds.org/moin/moin.cgi/Linux26ToolsAndSources – Supported hardware: arm (experimental)
Settings to Kernel Boot
9 The parameters are passed by the bootloader 9 List of kernel parameters in the file
– /usr/src/linux/Documentation/kernel-parameters.txt
9 Parameters example:
– hdc=ide-scsi : defines that IDE drive hdc must be treated as a SCSI device (for example: cdrecord on a burner)
– init : gives the executable to run after mounting root – root : defines the root partition to mount
– ro(rw) : defines the root partition must be mounted read (or read-write)
– vga : defines the video mode to configure
Boot Sequence:
Kernel Loading and Starting
9 The kernel is usually compressed!
– The 1st instructions = uncompression program
Decompress the rest of the kernel, then starts the execution
9 The kernel begins its execution
– Kernel initializations, detects devices, ...
– Mount root read-only
– Run the first process /sbin/init (process with 1)
9 The init process
– Read file /etc/inittab
– Execute rc scripts (Run Control)
Boot Sequence : init
9 The init process is in a state (level) of execution : runlevel
9 Under Linux RedHat
– 0 : Halt
– 1 : Single User mode
– 2 : Multi-User mode, without NFS – 3 : Full Multi-User mode
– 4 : Unsued
– 5 : Full Multi-User mode, with X – 6 : Reboot
9 It is described in /etc/inittab
Boot Sequence : Changing runlevel
9 When changing init level, tasks are triggered :
– Depending on the rules defined in /etc/inittab – Depending on the Run Control scripts defined in
/etc/rc.d/rcn.d
SXXyyyyy: when going in a level
KXXyyyyy: when going out a level – Example
/etc/rc.d/rc3.d/S20random :
– Initialize the random number generator
/etc/rc.d/rc3.d/K35smb :
– Stop the samba service
Boot Sequence: /etc/inittab 1/3
9 Describe the operating rules of init 9 Contains entries of the form:
– id:level:action:process
– id : identifiant (1- 4 caractères)
– level: runlevel to which the rules apply – action : action
– process : process to excute
Boot Sequence: /etc/inittab 2/3
9 Possible actions:
– respawn : re-run a process
– once : process started once the level of performance achieved – wait : ditto once + init waits until the end of the process
– boot : executed during boot (runlevel is ignored) – bootwait : ditto boot + pending terminaison
– off: do nothing
– ondemande : when the corresponding level is reached – initdefault : indicates the runlevel default
– sysinit : executed during the boot, but before boot and bootwait – powerwait, powerfail, powerokwait, powerfailnow : power
incident...
– ctrlaltdel : action to be executed when init gets SIGINT(via ctrl+alt+del)
– kbrequest : when a combination is performed at the keyboard
Boot Sequence: /etc/inittab 3/3
9 Example /etc/inittab :
– id:5:initdefault:
– l0:0:wait:/etc/rc.d/rc 0 – l1:1:wait:/etc/rc.d/rc 1 – ...
– ca:ctrlaltdel:/sbin/shutdown -t3 -r now – ...
– 1:2345:respawn:/sbin/mingetty tty1 – ...
– x:5:respawn:/etc/X11/prefdm -nodeamon
... through the organization of the sources and
compile a kernel...
What is “Kernel” ?
A Kernel : What is it ?
9 Above all it is a program :
– Resides on disk
For example /vmlinuz or /boot/vmlinuz
– Loads during the boot sequence (boot loader)
9 But a special program:
– Runs closest of hardware (just above the BIOS) – Privileged access to hardware
– Implements the abstraction process
But this is not one itself
His role with regard to processes :
– Manage the process (creation, arbitration, ...) – Provide services (access to hardware, signals, ...)
Different Kind of Kernels 1/2
9 Monolithic Kernel
– Provides all services (single program, modular or not) – Everything runs in kernel mode
– Ex. non-modular: DOS, Windows 9x, MacOS <9, … – Ex. modular: Linux, OS/2, AIX, Irix, …
9 Micro Kernel
– Provides minimal services
process management, memory and IPCs – Other services are provided by users progs – Ex.: Mac OS X (Mach), GNU/Hurd (Hurd)
9 Hybrid Kernel
– Combining the best of both worlds ? (Windows NT) 9 http://en.wikipedia.org/wiki/Kernel_(computing)
Different Kind of Kernels
2/2
Comparison of Kernel Types
9 Monolithic Kernel
– Easier to write
– Less elegant than micro kernel – More efficient
9 Micro Kernel
– Very interesting theory, difficult in practice – More resistant to bugs (thus safer)
9 Hybrid Kernel
– Combining the best of both worlds?
9 Fights between Monolithic and Micro
– Tanenbaum vs Torwald
Kernel vs Operating System
9 Do not confuse:
– Kernel et
– Operating System
9 « Linux » Example
– The Kernel
Linux
– The Operating System
GNU-Linux
C Library (GNU C Lib)
Applications Kernel
(Linux)
Illustration with
2.0 ≤
Linux Kernel
≤ 2.6« A Penguin in the Engine »
History
9 1991: Creation of Linux from scratch by Linus
Thorwald in 6 months (student at the University of Helsinki)
9 1991: Linux distribution on the Internet by the author.
Programmers worldwide contribute
9 1992: Linux is distributed under GNU GPL license 9 1994: Linux 1.0 Released
9 1994: Creation of the Red Hat Society 9 1995:
9 GNU / Linux on servers spread
9 2001: IBM invests $1 billion in Linux
9 2002: Linux adoption in many industry sectors
Kernel Numbering
9 Linux versions are numbered: x.y.z
9 Stable release (every 2 or 3 years or more)
– Identified by an even middle number – Examples: 1.0, 2.0, 2.2, 2.4
9 Development branch to integrate new functionalities and major changes
– Identified by an odd middle number – Examples: 2.1, 2.3, 2.5
9 Minor releases once in while:
– Examples: 2.0.40, 2.2.26, 2.4.36, 2.6.26
9 Patches release:
– identifies the patch level of this version (optional) – Examples: 2.6.26-8
History of Kernel Versions
9 Kernel Version
– 1991 - 1996: 0 et 1 – 1996 - 2004: 2.0 – 1999 - 2004: 2.2 – 2001 - …: 2.4
– 2004 - …: 2.6
Kernel Versions
9 Linux 2.4
–
Mature and Validated
–
Developments stopped
–
Missing some modules
–
Always an acceptable choice
–
Kernel code readable
9 Linux 2.6
– Supported by the community
– Mature and exhaustive
– New functionalities – Increased
performance
– Kernel code less
readable
Evolution of lines of code of the
Linux Kernel 2.6
Supported Hardware Architectures
9 Look in the directory
arch/9 Requirements: 32-bit processors, with or without MMU 9 32 bits architecture
– alpha, arm, arm26, cris, h8300, m32r, m68k, m68knommu, mips, parisc, powerpc, ppc, s390, sh, sparc, um, v850, x86, xtensa
9 64 bits architecture
– Is64, sh64, sparc64, x86_64
9 See the documentation sources for more details
Organisation of Kernel Sources
/usr/src/linux
|-- Documentation
|-- arch
| `-- alpha, arm, x86, x86_64, m68k, mips, ppc, sparc, ...
|-- drivers
| `-- block, bluetooth, cdrom, char, ieee1394, net, pci, scsi, ...
|-- fs
| `-- autofs, ext2, ext3, fat, isofs, msdos, nfs, proc, vfat, ...
|-- include
| |-- asm -> asm-i386
| |-- asm-generic
| |-- asm-i386
| |-- linux
| | `-- modules
| |-- net
| |-- pcmcia
| |-- scsi
| `-- video
|-- init
|-- ipc
|-- kernel
|-- lib
|-- mm
|-- net
Mapping Sources
9 Interactive map of Linux kernel source
– http://www.linuxdriver.co.il/kernel_map
Development activities on the Linux Kernel
Number and affiliation of developers (over 100 lines per month) More than 300 organizations working to help Linux
A good 1 / 3 are not businesses (compared to OpenOffice)
Kernel Compiling ( ≤ 2.4)
9 Cleaning sources:
– make clean : clean compiled source files
– make mrproper: ditto clean + deletes the config file
9 Building Configuration:
– make config, make menuconfig , make xconfig, make allnoconfig
9 Building kernel and modules
– make dep : clean and update the dependencies
– make zImage : build a compressed kernel (ou bzImage)
– New kernel located in /usr/src/linux/arch/i386/boot/bzImage – make modules : build modules
– make modules_install: install in /lib/modules/x.y.z
– make install : install the kernel in / boot and edit lilo
Kernel Compiling (2.6)
9 Cleaning sources :
– make clean : clean compiled source files
– make mrproper: ditto clean + deletes the config file
9 Building Configuration :
– make allnoconfig: interesting for embedded provides a minimum configuration of the kernel
approximately 550kb in bzImage
Includes required options as and when required
– make config, make menuconfig ou make xconfig: to modify an existing configuration
9 Building kernel and modules
– make : compiles all (kernel and modules)
Final Kernel Installation
9 The simplest solution (automatic configuration loader in general):
– make modules_install ; make install
9 Or manually install the kernel :
– cd /usr/src/linux-x.y.z-e – cp System.map /boot
– cp arch/i386/boot/bzImage /boot/vmlinuz-x.y.z-e – make modules_install
Copy the modules in /lib/modules/x.y.z-e – Reconfigure the kernel loader accordingly
Create links to /vmlinuz et /initrd
Why should compile a kernel?
9 For a standard PC (in terms of hardware), no real interest
9 Regular updating of the kernel version
– Update security followed in the major distributions
9 So in what cases is it necessary to compile the kernel
– Activating a particular device driver not provided by default (specific equipment)
– Enable a feature that is not in the main branch of the kernel
Adding support graphical boot
Adding real-time support (applying a patch)
Adding a specific driver (DVB-T TV tuner card, ...)
– Make an optimized kernel in the case of embedded systems
Of course !
Cross Compiling
Example
9 If you have a cross compiling toolchain located in
/usr/local/arm/3.3.2/
9 You just have to add it to your PATH :
– export PATH=/usr/local/arm/3.3.2/bin:$PATH
– See the file Documentation / Changes for details on minimum versions supported tools
9 Compiling for a Target (ARM)
– make ARCH=arm CROSS_COMPILE=armlinux – make modules_install
9 Install
– Copy arch/<platform>/boot/zImage on the target – Copy /lib/modules/<version> on the target
– You can customize arch/<arch>/boot/install.sh so that make
install do it
... ending with the execution of the system.
A system that works
User Mode – Kernel Mode 1/5
9 A system call causes the transition from user mode processes in kernel mode
9 User Mode: « standard » mode
– Execution mode for user programs
– Only the allocated memory areas are accessible – Some processor instructions are prohibited
– Direct access to devices prohibited
9 Kernel Mode: privileged mode
– User program execution core – All memory is addressable
– All processor instructions are allowed – Direct access to devices
User Mode – Kernel Mode 2/5
Passing Parameters
System Call
write(...) _write
User Program
C Library
Kernel
User Mode – Kernel Mode 3/5
For Linux-i386, the transition from user mode to kernel mode is implemented as follows :
9 Placement of settings in the "good" records 9 Software interrupt 0x80
– Passage of the process in kernel mode
– Running the function system-call (cf arch/i386/kernel/entry.S)
Recovering the system call number in eax register and arguments in the following registers (ebx, ecx, ...)
Performing the function located in sys_call_table[eax]
– Return from function system_call – Process return un user mode
9 Program continuation
User Mode – Kernel Mode 4/5
The driver development is usually done in kernel mode, but can also build a device driver in user mode
9Advantages of User Mode:
– Complete access to C library !!!
– If the driver crashes, just kill him (otherwise the machine crash) – Possibility of correction with the standard debug tools. But the
debug kernel is complicated
– A user mode driver can be swapped if not used
User Mode – Kernel Mode 5/5
9 However, a user mode driver poses a number of problems :
– Interruptions are not accessible
– Input/Output ports are just accessible through system call reserved for the super user (ioperm and/or iopl)
– Direct access to memory is possible only through
/dev/mem (for example using mmappant), but this file is accessible only by the super user
– The response times are longer because the data transfers require a change of context (or even to return the driver swap!)
Declare new Linux System Call
We want to implement the system call
addition(int x, int y)
which displays the result of the addition of
xand
yon the console.
Each system call has a name defined in the table
sys_call_table
(cf
arch/x86/kernel/syscall_table_XX.S)
So you have to had a new system call to table
sys_call_table:
ENTRY(sys_call_table)
.long sys_restart_syscall /* 0 - old "setup()" system call */
.long sys_exit) .long sys_fork
.long sys_read /* 3 */
...
.long sys_addition /* Added new system call entry N° NUM*/
Add a uniq number
arch/x86/include/unistd_XX.h ( <asm/unistd.h>)#define __NR_addition NUM
Implement a Linux System Call
9 Writes the function that implements the system call (cf
kernel/sys.c)
SYSCALL_DEFINE2(addition, int, x, int, y) {
printk(" <1>Addition %d %d ==> %d\n ", x, y, x+y);
return 0;
}
9 Notice:
–
SYSCALL_DEFINE2 is a macro to define syscall with 2 arguments Different macros for 1, 2, 3, … arguments
–
printk is the same that printf from C library (same conventions but without floating)– Messages posted on the console and file /var/log/messages
Test your Linux System Call (until 2.6.18)
9 Definition of the system call in C program (library)
#include <linux/unistd.h> /* pour _syscall2 */
#include <asm/unistd.h>
_syscall2(int, sys_addition, int, x, int, y); /* macro 2 args */
9 What is generated by the macro:
int sys_addition(int x, int y) {
long __res;
__asm__ volatile ("int $0x80" :"=a" (__res) :"0" (XXX),
"b" ((long)(x)),
"c" ((long)(y)));
if (__res >= 0) return (int) __res;
errno = -__res;
return -1;
}
1 Code version 2.0. Starting with 2.2 it's more complicated to check the validity of the return code.
Test your Linux System Call (until Linux 2.6.18)
9 C program to test the new system call
#include <asm/unistd.h> /* inclus the declared system calls */
#include <sys/syscall.h>
#include <errno.h>
#include <stdio.h>
void main() {
printf(“return call code: %d\n",
syscall(__NR_addition, 1, 2);
}
9 To compile
– Specify where are your modified include files
gcc –o test_syscall test_syscall.c –I...
Address Space User vs. Kernel 1/2
9 Distinction between the memory used by the kernel and used by the processes
9The kernel provides some functions/macros utilities (cf
<asm/uaccess.h>)
9int access_ok(int type, unsigned long addr, unsigned long size);
– addr: address in the address space of the calling process – type ∈ {VERIFY_READ, VERIFY_WRITE}
9int get_user(lvalue, address);
– to read in the address space of the caller (0 if OK-EFAULT otherwise).
– the type of pointer "address" determines the size to transfer
9int put_user(expression, address);
– to write in the address space of the calling – same conventions as get_user
9__get_user(lvalue, address); et __put_user(expression, address);
– "unsecure“ version (ie do not test with access_ok)
Address Space User vs. Kernel 2/2
9 int copy_to_user(unsigner long to, unsigned long from, unsigned long size);
– to copy a memory area from address space to kernel address space of the process
– return value = number of bytes not transferred
9 int copy_from_user(unsigned long to, unsigned long from, unsigned long size);
– to copy a memory area from address space to kernel address space of the process
– same conventions
9 int __copy_to_user(...)
et
__copy_from_user(...)– "unsecure“ version (ie do not test with access_ok)
Facilitating Practical Lab…
but they are not virtual !
Hardware Virtualization
What is Hardware Virtualization?
9 Goal
– Running multiple systems on the same machine
9 Notice
– Hardware and or Software Abstraction Layer
– Host operating system (host) installed directly on hardware – Operating systems (or applications, or application set),
"virtualized (s)" or "guest (s)" (guest)
– Partitioning, insulation and/or sharing of physical resources and/or logic
– Images can be manipulated: starting, stopping, freezing,
cloning, backup and restore, backup context, migration from one physical machine to another
– Virtual Network: Network software purely internal to the host machine between the host and/or guests
Several Softwares
9 QEMU
– Emulator: platforms x86, PPC, Sparc
9 KVM
– modified version of QEMU using virtualization instructions of AMD and Intel processors (Intel VT or AMD-V)
9 VMware
– proprietor, emulator: platform x86
9 Microsoft VirtualPC et VirtualServer
– propretor, emulator: plateform x86
9 VirtualBox
– Emulator: platform x86
Different Kind of Virtualization 1/2
9 Isolation
9 Isolating the performance of apps in execution contexts 9 Performance but not really
virtualization
9 Ex: chroot, jail, Linux-VServer
9 Kernel in User Space
9 Kernel running in user space as a classical application
9 Independence from the host system does not exist
9 Ex: User Mode Linux, coLinux
9 Machine Virtuelle
9 Software that emulates and / or virtualizes the hardware for the guest OS
9 Insulates the OS, but cost in term of performances
9 Ex: Qemu, VirtualPC, VirtualBox, VMware
Different Kind of Virtualization 2/2
9 Emulator
– Allows the execution of a program for a device X on device Y – An instruction is executed by a routine that simulates the PC – Slow but universal
– Emulators Ex.: PSX, DS, Néo-Géo, … mais aussi Plex86, QEMU
9 Virtualization
– Compile instructions during their 1st run (Virtual PC)
– If possible, the code is directly executed on the CPU, otherwise re-write dynamically
Allows execution to 80% speed (Vmware, kqemu, VirtualBox)
9 Para-Virtualisation ou Hyperviseur
– The guest system is “aware” of the underlying system – Optimal performances (near the one of the host system) – Ex: Xen, UML, Hypervisor, …
Performances 1/2
9 Performance comparison
Native Linux (L) Xen / Linux (X)
VMWare WorkStation 3.2 (V) User Mode Linux (U)
Source: http://www.cl.cam.ac.uk/research/srg/netos/xen/performance.html
Performances 2/2
VirtualBox – Qemu – Vmware (FreshDiagnose)
VirtualBox – Qemu (hardinfo)
0%
10%
20%
30%
40%
50%
60%
70%
80%
90%
100%
DhryStone ALU (MDIPS)
WhetStone FPU (MWIPS)
Affectation d'entiers
Affectation de réels
Division d'entiers Division de réels
Vitesse d'écriture (MB/s) Vitesse de lecture (MB/s)
0%
10%
20%
30%
40%
50%
60%
70%
80%
90%
100%
CPU Zlib
CPU Fibonacci
CPU MD5
CPU SHA1 CPU Blowfish
FPU Raytracing
VMware
9 Provides a virtual environment
– Processor, Memoiry, Bus, …
– Peripherals : Nertwork card, Video card, …
9 Host Machine (physical computer)
– Machine running the VMware program
9 Guest Machine (virtual machine)
– Machine running inside the VMware process
9 Communications
– Using TCP / IP to communicate
– The machine uses the guest host machine as gateway – Network set up automatically
– 3 Modes: NAT, Bridge, Hôte
Free emulator and virtualizer
QEMU
QEMU
9 Open Source Processor Emulator
– Operating Modes
User Emulation: allows you to start a Linux program compiled for one CPU on another
System Emulation: emulate a complete system (CPU, peripherals, ...) to install an operating system
Virtualization module (kqemu) increases performance – Created by Fabrice Bellard : http://bellard.org/qemu/
9 Runs under (host system)
– x86, x86-64, PPC, Sparc32, ARM, MIPS, Sparc64, ia64, m68k
9 QEMU emulates (guest systems)
– x86, x86-64, ARM, Sparc32, PPC, MIPS, m68k, Sparc64, PPC64
9 QEMU User emulation (guest system)
– x86, x86-64, ARM, Sparc32, PPC, MIPS, m68k, Sparc64, PPC64
Starting with QEMU
9 Installing an OS
– Creating a disk image file : qemu-img
qemu-img create [-b base_image] [–f format] filename size(M|G)
qemu-img create –f qcow2 disk1.img 32M
– Start the emulation system : qemu
qemu [–L bios-dir] [–m RAMsize] [–hda filename]
Some usefull options:
– File Systems : (-fda –fdb –hda … -hdd –cdrom) filename
– Peripherals: -soundhw c,… –usb [name] –net (nic|user|tap|socket) …
– Linux: -kernel bzImage –append cmdline –initrd file
– Debug/Expert: -kernel-kqemu –no-acpi –vnc display
– Other: -boot [a|c|d|n] -localtime –full-screen –nographic –smp n
– Emulation of a different system : qemu-system-xxx
xxx = arm, mips, mipsel, ppc, sparc, x86_64
Same options
Installing an OS
9 Creating a disk image file
qemu-img create -f qcow2 hda.img 8G
9 Installing from CDROM
qemu -L pc-bios -m 256 -hda hda.img -boot d \ -cdrom c:\\debian-503-i386-businesscard.iso
9 Using guest installed system
qemu -L pc-bios -m 256 -hda hda.img -boot c
Keys to success
Networks
Host and Guest:
Several Configurations
9 Host Only
– The virtual machine has only access to the host on a virtual private network
– From a LAN point of view, there is no new machine – Uses DHCP from host machine
9 NAT
– The virtual machine has access to the LAN through the host machine by routing type of NAT (Network Address Translation) – From a LAN point of view, there is no new machine
– Requires a connected and operational LAN on the host machine – Uses DHCP from host machine
9 Bridged
– The virtual machine has direct access to the LAN
– From a LAN point of view, there is a new machine with its own IP address
– Requires a connected and operational LAN on the host machine – Uses DHCP from LAN (if present)
Host Only, NAT, Bridged
n o
p
Host Machine Guest Machine eth0
tap0
192.168.2.1 192.168.2.2
Host Machine Guest Machine eth0
tap0
192.168.2.1 192.168.2.2
(bridge) eth0
157.159.x.x
Host Machine Gest Machine
157.159.x.x 157.159.x.y
Windows TAP Configuration
9 Configuring the host machine (Windows)
– Creating a tap interface
Installing OpenVPN (http://openvpn.net/)
Verify the interface configuration: openvpn --show-adapters
Rename the created interface into: openvpn-tap
Share your Internet connection with openvpn-tap
– Give address192.168.0.1 to interface: openvpn-tap
Be careful:
Do not make these changes
during use
Configuring Network of the Guest Machine
9 Connecting the guest machine network interface to
openvpn-tap
from host machine
– qemu … -net nic,model=ne2k_pci –net tap,ifname=openvpn-tap
9 Configuring the network interface eth0
– Utilisation du dhcp (/etc/network/interface)
allow-hotplug eth0
iface eth0 inet dhcp
– or ifconfig eth0 192.168.0.x netmask 255.255.255.0
9 Configuring the routing table
– route add default gw 192.168.0.1
9 Configuring the DNS (
/etc/resolv.conf)
– nameserver 192.168.0.1
9 Network Test
– ping 192.168.0.1 – ping www.unice.fr
Exchange files via the network
9 Configuring the host machine (Windows)
– Share a folder in read/write mode
Right click on folder and select property
9 Configuring the guest machine (
Debian GNU/Linux)
– Install the smbfs package (if not already done)
Workgroup / Domain configuration
– Mounting the shared folder on the file system
mount –t smbfs –o username=name //host/directory /mnt