• Aucun résultat trouvé

Boot LoaderPartition TableWin BootloaderOS Bootloader

N/A
N/A
Protected

Academic year: 2022

Partager "Boot LoaderPartition TableWin BootloaderOS Bootloader"

Copied!
95
0
0

Texte intégral

(1)

Embedded Systems and Applications

Mail: Stephane.Lavirotte@unice.fr Web: http://stephane.lavirotte.com/

Université de Nice - Sophia Antipolis

(2)

Lecture Presentation

Embedded Systems and

Applications

(3)

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)

(4)

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

(5)

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

(6)

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

(7)

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

(8)

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

(9)

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

(10)

Architecture Overview

9 Layer Vision:

Source: Linux, Pilotes de Périphériques (3rdedition)

Hardware

(11)

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

(12)

Some reminders about Unix

Reminders

(13)

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

(14)

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)

(15)

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

(16)

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

(17)

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.

(18)

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

(19)

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

(20)

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

(21)

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

(22)

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

(23)

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

(24)

Since the boot...

« Boot » a System

(25)

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

ƒ ...

(26)

Boot Steps

(27)

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

(28)

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

(29)

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

(30)

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

(31)

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

(32)

/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/lilo

(33)

GRUB 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

(34)

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)

(35)

/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

(36)

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)

(37)

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

(38)

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)

(39)

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

(40)

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

(41)

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

(42)

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

(43)

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

(44)

... through the organization of the sources and

compile a kernel...

What is “Kernel” ?

(45)

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

(46)

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)

(47)

Different Kind of Kernels

2/2

(48)

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

(49)

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)

(50)

Illustration with

2.0 ≤

Linux Kernel

≤ 2.6

« A Penguin in the Engine »

(51)

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

(52)

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

(53)

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

(54)

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

(55)

Evolution of lines of code of the

Linux Kernel 2.6

(56)

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

(57)

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

(58)

Mapping Sources

9 Interactive map of Linux kernel source

– http://www.linuxdriver.co.il/kernel_map

(59)

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)

(60)

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

(61)

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)

(62)

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

(63)

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 !

(64)

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

(65)

... ending with the execution of the system.

A system that works

(66)

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

(67)

User Mode – Kernel Mode 2/5

Passing  Parameters

System  Call

write(...) _write

User Program

C Library

Kernel

(68)

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

(69)

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

(70)

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

(71)

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

x

and

y

on 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

(72)

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

(73)

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.

(74)

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

(75)

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)

(76)

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)

(77)

Facilitating Practical Lab…

but they are not virtual !

Hardware Virtualization

(78)

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

(79)

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

(80)

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

(81)

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

(82)

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

(83)

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

(84)

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

(85)

Free emulator and virtualizer

QEMU

(86)

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

(87)

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

(88)

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

(89)

Keys to success

Networks

(90)

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)

(91)

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

(92)

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

(93)

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

(94)

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

(95)

Conclusion

9 Now we are ready to start concrete Practical Lab !

Références

Documents relatifs

If your application has custom security configuration and you want all your actuator endpoints to be accessible without authentication, you need to explicitly

flCPPY. Power-cycling mode ~ust be terminated by inputting the APPUEIENTER key sequence, and will result in entry to ROM Customer monitor mode with the following

ALTO MESA BOOT LOADER - MBoot.asm LOADED ALMOST ANYWHERE AND EXECUTED R... READ header and

de Spring à Spring Boot - Spring Boot et la démarche DevOps (Orange internal) – Dependency-Check : détection de dépendances comportant des vulnérabilités connues – Fossology

This will cause all host file systems to sync as best they can, and core dumps to be taken from all nodes and the host.. If you wish to avoid taking core dumps, simply

failure can be accomplished with diagnostics and the installers (floppy or net). • Most important thing to remember is that you should not attempt data recovery until you are

issued in strict confidence and shall not, without the prior written permissiOn of Xerox Corpora- tion Rank Xerox,Ltd., be reproduced, copied or used for any purpose

NTLDR lit ensuite le contenu du fichier Boot.ini puis et affiche les options de démarrage, s'ils en existent plusieurs ou bien démarre le système d'exploitation par défaut.. Si