73
Virtual Machines COMP9242 Advanced Operating Systems 2010/S2 Week 6 ©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

Embed Size (px)

Citation preview

Page 1: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

Virtual Machines

COMP9242 Advanced Operating Systems 2010/S2 Week 6

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Page 2: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

  You are free: •  to share — to copy, distribute and transmit the work

•  to remix — to adapt the work

  Under the following conditions: •  Attribution. You must attribute the work (but not in any way that suggests

that the author endorses you or your use of the work) as follows: “Courtesy of Gernot Heiser, UNSW”

  The complete license text can be found at http://creativecommons.org/licenses/by/3.0/legalcode

Copyright Notice

These slides are distributed under the Creative Commons Attribution 3.0 License

Page 3: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Virtual Machines

  “A virtual machine (VM) is an efficient, isolated duplicate of a real machine”   Duplicate: VM should behave identically to the real machine

•  Programs cannot distinguish between execution on real or virtual hardware •  Except for:

-  Fewer resources available (and potentially different between executions) -  Some timing differences (when dealing with devices)

  Isolated: Several VMs execute without interfering with each other   Efficient: VM should execute at a speed close to that of real hardware

•  Requires that most instruction are executed directly by real hardware

Page 4: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

Virtual Machines, Simulators and Emulators

Simulator: •  Provides functionally-accurate model of a machine •  May run on any hardware •  Is typically slow (1000-fold slowdown)

Emulator: •  Provides behavioural model of the hardware (and possibly software) •  Not fully accurate •  Reasonably fast (10-fold slowdown)

Virtual machine: •  Models machine accurately and efficiently •  Minimal slowdown •  Host and target must be (almost) the same ISA

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Page 5: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Types of Virtual Machines

  Contemporary use of the term VM is more general •  Buzzword-compliance makes simulators and emulators virtual machines •  Eg: “OS virtualization” is really emulation

  “Virtual machine” even if there is nor corresponding real machine •  E.g: Java virtual machine •  Can be viewed as virtualizing at the ABI level •  Also called process VM

  We only concern ourselves with virtualizing at the ISA level •  ISA = instruction-set architecture (hardware-software interface) •  Also called system VM •  Will later see subclasses of this

Page 6: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Virtual Machine Monitor (VMM), aka Hypervisor

  Program that runs on real hardware to implement the virtual machine   Controls resources

•  Partitions hardware •  Schedules guests

•  Performs world switch •  Mediates access to shared resources

-  e.g. console   Implications:

•  Hypervisor executes in privileged mode •  Guest software executes in unprivileged mode •  Privileged instructions in guest cause a trap into hypervisor •  Hypervisor interprets/emulates them •  Can have extra instructions for hypercalls

OS

Hardware

Guest OS

Hardware

Hypervisor

Page 7: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Why Virtual Machines?

  Historically used for easier sharing of expensive mainframes •  Run several (even different) OSes on same machine

•  called guest operating system •  Each on a subset of physical resources •  Can run single-user single-tasked OS

in time-sharing mode -  legacy support

  Gone out of fashion in 80’s •  Time-sharing OSes common-place •  Hardware too cheap to worry...

Hypervisor

RAM

VM1

Guest OS

Apps

Virt RAM

VM2

Guest OS

Apps

Virt RAM

Mem. region Mem. region

Page 8: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Why Virtual Machines?

  Renaissance in recent years for improved isolation   Server/desktop virtual machines

•  Improved QoS and security •  Uniform view of hardware •  Complete encapsulation

-  replication -  migration -  checkpointing -  debugging

•  Different concurrent OSes -  e.g.: Linux and Windows

•  Total mediation   Would be mostly unnecessary

•  if OSes were doing their job...

Hypervisor

RAM

VM1

Guest OS

Apps

Virt RAM

VM2

Guest OS

Apps

Virt RAM

Mem. region Mem. region

Page 9: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Virtualization Mechanics

  Traditional “trap and emulate” approach: •  guest attempts to access physical resource •  hardware raises exception (trap), invoking hypervisor's exception handler •  hypervisor emulates result, based on access to virtual resource

  Most instructions do not trap •  makes efficient virtualization possible •  requires that VM ISA is (almost) same as physical processor ISA

ld r0, curr_thrd ld r1, (r0,ASID)‏ mv CPU_ASID, r1 ld sp, (r1,kern_stk)‏

lda r1, vm_reg_ctxt ld r2, (r1,ofs_r0)‏ sto r2, (r1,ofs_ASID)‏

Guest

Exception

VMM

Page 10: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Virtualization Mechanics: Address Translation

Virt PT ptr

data

ld r0, adr Guest virtual address

Guest physical address

Physical address

PT ptr

Application (Virtual) guest page table

Hypervisor's guest memory map

Shadow page table Hypervisor mirrors guest's page table updates

Guest OS

Hypervisor

Page 11: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

VMM Types

Classic: as above Hosted: run on top of another operating system

•  e.g. VMware Player/Fusion Whole-system: Virtual hardware and operating system

•  Really an emulation •  E.g. Virtual PC (for Macintosh)

Physically partitioned: allocate actual processors to each VM Logically partitioned: time-share processors between VMs Co-designed: hardware specifically designed for VMM

•  E.g. Transmeta Crusoe, IBM i-Series

Pseudo: no enforcement of partitioning •  Guests at same privilege level as hypervisor •  Really abuse of term “virtualization” •  e.g. products with “optional isolation”

Page 12: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Native vs. Hosted VMM

  Hosted VMM can run besides native apps

•  Sandbox untrusted apps •  Convenient for running

alternative OS on desktop

Native/Classic/ Bare-metal/Type-I

Hosted/Type-II

Guest OS

Hypervisor

Hardware

App Guest OS

Hypervisor

Host OS

Hardware

App

  Less efficient •  Twice number of mode switches •  Twice number of context switches •  Host not optimised for exception

forwarding

Page 13: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Requirements for Virtualization

Definitions:   Privileged instruction: executes in privileged mode, traps in user mode

•  Note: trap is required, NO-OP is insufficient!   Privileged state: determines resource allocation

•  Includes privilege mode, addressing context, exception vectors, …   Sensitive instruction: control-sensitive or behaviour-sensitive

•  control sensitive: changes privileged state •  behaviour sensitive: exposes privileged state

-  includes instructions which are NO-OPs in user but not privileged mode   Innocuous instruction: not sensitive

Note: •  Some instructions are inherently sensitive

-  e.g. TLB load •  Others are sensitive in some context

-  e.g. store to page table

Page 14: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Trap-and-Emulate Requirements

  An architecture is virtualizable if all sensitive instructions are privileged   Can then achieve accurate, efficient guest execution

•  by simply running guest binary on hypervisor   VMM controls resources   Virtualized execution is indistinguishable from native, except:

•  Resources more limited (running on smaller machine) •  Timing is different (if there is an observable time source)

  Recursively virtualizable machine: •  VMM can be built without any timing dependence

ld r0, curr_thrd ld r1, (r0,ASID)‏ mv CPU_ASID, r1 ld sp, (r1,kern_stk)‏

lda r1, vm_reg_ctxt ld r2, (r1,ofs_r0)‏ sto r2, (r1,ofs_ASID)‏

Guest

Exception

VMM

Page 15: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Virtualization Overheads

  VMM needs to maintain virtualized privileged machine state •  processor status •  addressing context •  device state...

  VMM needs to emulate privileged instructions •  translate between virtual and real privileged state •  e.g. guest ↔ real page tables

  Virtualization traps are be expensive on modern hardware •  can be 100s of cycles (x86)

  Some OS operations involve frequent traps •  STI/CLI for mutual exclusion •  frequent page table updates during fork()... •  MIPS KSEG address used for physical addressing in kernel

Page 16: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Unvirtualizable Architectures

  x86: lots of unvirtualizable features •  e.g. sensitive PUSH of PSW is not privileged •  segment and interrupt descriptor tables in virtual memory •  segment description expose privileged level

  Itanium: mostly virtualizable, but •  interrupt vector table in virtual memory •  THASH instruction exposes hardware page tables address

  MIPS: mostly virtualizable, but •  kernel registers k0, k1 (needed to save/restore state) user-accessible •  performance issue with virtualizing KSEG addresses

  ARM: mostly virtualizable, but •  some instructions undefined in user mode (banked registers, CPSR) •  PC is a GPR, exception return in MOVS to PC, doesn’t trap

  Most others have problems too   Modern trend is “hardware virtualization support” (later)

Page 17: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Impure Virtualization

  Used for two reasons: •  unvirtualizable architectures •  performance problems of virtualization

  Change the guest OS, replacing sensitive instructions •  by trapping code (hypercalls) •  by in-line emulation code

  Two standard approaches: •  binary translation: modifies binary •  para-virtualization: changes ISA

ld r0, curr_thrd ld r1, (r0,ASID)‏ mv CPU_ASID, r1 ld sp,(r1,kern_stk)‏

ld r0, curr_thrd ld r1,(r0,ASID)‏ trap ld sp,(r1,kern_stck)‏

ld r0, curr_thrd ld r1,(r0,ASID)‏ jmp fixup_15 ld sp,(r1,kern_stck)‏

Page 18: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Binary Translation

  Locate sensitive instructions in guest binary and replace on-the-fly by emulation code or hypercall

•  pioneered by VMware •  can also detect combinations of sensitive instructions and replace by single

emulation •  doesn’t require source, uses unmodified native binary

-  in this respect appears like pure virtualization! •  very tricky to get right (especially on x86!)

•  “heroic effort” [Orran Krieger, then IBM now VMware] •  needs to make some assumptions on sane behaviour of guest

Page 19: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Para-Virtualization

  New name, old technique •  Mach Unix server [Golub et al, 90], L4Linux [Härtig et al, 97],

Disco [Bugnion et al, 97] •  Name coined by Denali [Whitaker et al, 02], popularised by

Xen [Barham et al, 03]   Idea: manually port the guest OS to modified ISA

•  Augment by explicit hypervisor calls (hypercalls) -  Use more high-level API to reduce the number of traps -  Remove un-virtualizable instructions -  Remove “messy” ISA features which complicate virtualization

•  Generally out-performs pure virtualization and binary-rewriting   Drawbacks:

•  Significant engineering effort •  Needs to be repeated for each guest-ISA-hypervisor combination •  Para-virtualized guest needs to be kept in sync with native guest •  Requires source

Guest

Hypervisor

Hardware

Page 20: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Virtualization Techniques

  Impure virtualization methods enable new optimisations •  due to the ability to control the ISA

  E.g. maintain some virtual machine state inside VMM: •  e.g. interrupt-enable bit (in virtual PSR) •  guest can update without (expensive) hypervisor invocation

-  requires changing guest's idea of where this bit lives •  hypervisor knows about VMM-local virtual state and can act accordingly

-  e.g. queue virtual interrupt until guest enables in virtual PSR

psid Trap 0

1

VPSR 0

0

PSR mov r1,#VPSR ldr r0,[r1] orr r0,r0,#VPSR_ID sto r0,[r1]

Page 21: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Virtualization Techniques

  E.g. lazy update of virtual machine state •  virtual state is kept inside hypervisor •  keep copy of virtual state inside VM •  allow temporary inconsistency between local copy and real VM state •  synchronise state on next forced hypervisor invocation

-  actual trap -  explicit hypercall when physical state must be updated

•  Example: add a mapping: -  guest enables FPU -  no need to invoke hypervisor at this point -  hypervsior syncs state on virtual kernel exit

Page 22: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Virtualization Techniques

Page table implementation options   Strict shadowing of virtual page table

•  write protect PTs ⇒ force trap into hypervisor on each update •  can combine multiple updates in single hypercall

-  e.g. during fork()   Lazy shadowing of virtual page table

•  identify synchronisation points •  possible due to TLB semantics

-  real PT updates only become effective once loaded into TLB -  explicit TLB loads and flushes are natural synchronisation points

•  PTs are big ⇒ need to tell hypervisor which part to sync   Expose real page tables (write-protected)

•  emulate updates •  guest must deal with PT reads differing from what was written

  Complex trade-offs •  Xen changed approach several times

Page 23: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Virtualization Techniques

Virtual memory tricks   Over-committing memory

•  like classical virtual memory •  sum of guest physical RAM > physical RAM

  Page sharing •  multiple VMs running the same guest have a lot of common pages

-  text segments, zeroed pages •  hypervisor detects pages with same content

-  keeps hash of every page •  uses copy-on-write to map those to a single copy

-  up to 60% memory savings [Waldspurger 02]   Memory reclamation using ballooning

•  load pseudo device driver into guest, colludes with hypervisor •  to reclaim memory, hypervisor instructs driver to request memory •  hypervisor can re-use memory hoarded by ballooning driver •  guest controls which memory it gives up

Guest Memory

Balloon

Guest Memory

Balloon

Guest Memory

Balloon

May page out

May page in

Page 24: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Device Virtualization Techniques

Full virtualization of device   Hypervisor contains real device driver   Native guest driver accesses device as usual

•  implies virtualizing all device registers etc •  trap on every device access

  Virtualizes at device interface   Hypervisor implements device-sharing policies   Drawbacks:

•  must re-implement/port all drivers in hypervisor -  unfeasible for contemporary hardware

•  very expensive (frequent traps) •  will not work on most devices

-  timing constraints violated Hypervisor Driver Driver

Processor

App

Guest OS

Native Driver

Native Driver

App

Page 25: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Device Virtualization Techniques

Virtual device drivers   Guest OS contains virtual drivers

•  forwards guest I/O requests to real driver via hypercalls •  very simple driver

  Need only support small number of different virtual devices •  e.g. one type of virtual NIC, one type of virtual disk

  Virtualizes at driver interface   Hypervisor implements device-sharing policies   Drawback:

•  must re-implement/port all drivers in hypervisor •  unfeasible for contemporary hardware •  ... unless a complete OS becomes

the hypervisor (KVM) Hypervisor Driver Driver

Processor

App

Guest OS

Virt Driver

Virt Driver

App

Page 26: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Device Virtualization Techniques

Driver inside host (for Type-II VMMs)   Guest OS contains virtual drivers   Hypervisor passes requests through to host   Fits Type-II model (VMM is just an app)

Processor

App

Guest OS

Virt Driver

Virt Driver

App

Hypervisor

Host OS Driver Driver

Page 27: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Device Virtualization Techniques

Device-driver OS   Special guest OS contains real drivers

•  Xen “Dom0 guest”   Hypervisor passes requests from virtual driver through to driver OS   Can re-use driver guest's native drivers unchanged   Drawbacks:

•  driver invocation requires full context switch

•  driver OS + all drivers becomes part of VMM

-  very large TCB   Can improve TCB by running each

driver in its own guest OS instance •  full encapsulation of drivers

[LeVasseur et al 04] Hypervisor

Processor

Guest OS

Virt Driver

App

Driver OS

Driver Driver

App

Guest OS

Virt Driver

Virt Driver

Page 28: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Device Virtualization Techniques

Native real driver in guest   Guest allowed to “own” device   Hypervisor not involved in I/O

•  native I/O performance!   In general insecure and thus infeasible   Possible for:

•  simple devices not doing DMA -  but sharing is an issue

•  with hardware support -  virtualization-friendly devices

•  e.g. IBM channel architecture -  IO-MMU

•  maps IO space to RAM •  under control of hypervisor •  e.g. Intel VT-d

Hypervisor

Processor

App

Guest OS

Native Driver

Native Driver

App

Page 29: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Hardware Virtualization Support

  Intel VT-x/VT-i: virtualization support for x86/Itanium •  Introduces new processor mode: VMX root mode for hypervisor •  In root mode, processor behaves like pre-VT x86 •  In non-root mode, all sensitive instructions trap to root mode (“VM exit”)

-  orthogonal to privilege rings, i.e. each mode has 4 ring levels -  very expensive traps (700+ cycles on Core processors)‏ -  not used by VMware for that reason [Adams & Agesen 06]

•  Supported by Xen for pure virtualization (as alternative to para-virtualization)‏ •  Used exclusively by KVM

-  KVM uses whole Linux system as hypervisor! -  Implemented by loadable driver that turns on root mode

•  VT-i (Itanium) also reduces virtual address-space size for non-root   Similar AMD (Pacifica), PowerPC   Other processor vendors working on similar feature

•  ARM TrustZone is partial solution (proper solution just announced, Aug’10)   Aim is virtualization of unmodified legacy OSes

Page 30: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Virtualization Performance Enhancements (VT-x)

  Hardware shadows some privileged state •  “guest state area” containing segment registers, PT pointer, interrupt mask etc •  swapped by hardware on VM entry/exit •  guest access to those does not cause VM exit •  reduce hypervisor traps

  Hypervisor-configurable register makes some VM exits optional •  allows delegating handling of some events to guest

-  e.g. interrupt, floating-point enable, I/O bitmaps -  selected exceptions, eg syscall exception

•  reduce hypervisor traps   Exception injection allows forcing certain exceptions on VM entry   Extended page tables (EPT) provide two-stage address translation

•  guest virtual → guest physical by guest's PT •  guest physical → physical by hypervisor's PT •  TLB refill walks both PTs in sequence

Page 31: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

I/O Virtualization Enhancements (VT-d)

  Introduce separate I/O address space   Mapped to physical address space by I/O MMU

•  under hypervisor control   Makes DMA safely virtualizable

•  device can only read/write RAM that is mapped into its I/O space   Useful not only for virtualization

•  safely encapsulated user-level drivers for DMA-capable devices

•  ideal for microkernels ☺   AMD IOMMU is essentially same   Similar features existed on high-end Alpha and HP boxes   ... and, of course, IBM channels since the '70s...

Page 32: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Halfway There: ARM TrustZone

  ARM TrustZone extensions introduce: •  new processor mode: monitor

-  similar to VT-x root mode -  banked registers (PC, LR) -  can run unmodified guest

OS binary in non-monitor kernel mode

•  new privileged instruction: SMI -  enters monitor mode

•  new processor status: secure •  partitioning of resources

-  memory and devices marked secure or insecure -  in secure mode, processor has access to all resources -  in insecure mode, processor has access to insecure resources only

•  monitor switches world (secure ↔ insecure) •  really only supports one virtual machine (guest in insecure mode)

-  need another hypervisor and para-virtualization for multiple guests

OS OS

App App App App App App

Hypervisor

Insecure World

Monitor

App App

OS

Secure World

Page 33: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Uses of Virtual Machines

  Multiple (identical) OSes on same platform •  the original raison d'être •  these days driven by server consolidation •  interesting variants of this:

-  different OSes (Linux + Windows) -  old version of same OS (Win2k for stuff broken under Vista) -  OS debugging (most likely uses Type-II VMM)

  Checkpoint-restart •  minimise lost work in case of crash •  useful for debugging, incl. going backwards in time

-  re-run from last checkpoint to crash, collect traces, invert trace from crash •  life system migration

-  load balancing, environment take-home   Ship application with complete OS

•  reduce dependency on environment •  “Java done right” ☺

  How about embedded systems?

Page 34: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Why Virtualization for Embedded Systems?

Use case 1: Mobile phone processor consolidation   High-end phones run high-level OS (Linux) on app processor

•  supports complex UI software   Base-band processing supported by real-time OS (RTOS)

Linux

Processor

UI Software

RTOS

Processor

Baseband Software

Hypervisor

Processor

Linux

UI Software

RTOS

Baseband Software

  Medium-range phone needs less grunt

•  can share processor •  two VMs on one

physical processor •  hardware cost

reduction

Page 35: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Why Virtualization for Embedded Systems?

Use case 1a: License separation   Linux desired for various reasons

•  familiar, high-level API •  large developer community •  free

  Other parts of system contain proprietary code   Manufacturer doesn't want to open-source   User VM to contain Linux + GPL

Hypervisor

Processor

Linux GPL

UI Software

RTOS

Baseband Software

Page 36: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Why Virtualization for Embedded Systems?

Use case 1b: Software-architecture abstraction   Support for product series

•  range of related products of varying capabilities   Same low-level software for high- and medium-end devices   Benefits:

•  time-to-market •  engineering cost

Hypervisor

Processor

Linux

UI Software

RTOS

Realtime Software Linux

Processor

UI Software

RTOS

Processor

Realtime Software

Hypervisor

Page 37: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Why Virtualization for Embedded Systems?

Use case 1c: Dynamic processor allocation   Allocate share of base-band processor to application OS

•  Provide extra CPU power during high-load periods (media play) •  Better processor utilisation ⇒ higher performance with lower-end hardware •  HW cost reduction •  Tricky to do

requires SMP guest etc…

Linux

Processor

UI Software

RTOS

Processor

Baseband Software

Hypervisor

Linux

Processor

UI Software

RTOS

Processor

Baseb. SW

Hypervisor

Page 38: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Why Virtualization for Embedded Systems?

Use case 2: Certification re-use   Phones need to be certified to comply with communication standards   Any change that (potentially) affects comms needs re-certification   UI part of system changes frequently

Hypervisor

Processor

Linux

UI Software

RTOS

Baseband Software

Linux

Processor

UI Software

RTOS

Processor

Baseband Software

Hypervisor

  Encapsulation of UI •  provided by VM •  avoids need for

costly re-certification

Page 39: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Why Virtualization for Embedded Systems?

Hypervisor

Processor

Linux

UI Software

RTOS

Baseband Software

Linux

Processor

UI Software

RTOS

Processor

Baseband Software

Hypervisor

Use case 2a: Open phone with user-configured OS   Give users control over the application environment

•  perfect match for Linux   Requires strong encapsulation of application environment

•  without undermining performance!

Page 40: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Why Virtualization for Embedded Systems?

Use case 2b: Phone with private and enterprise environment   Work phone environment integrated with enterprise IT system   Private phone environment contains sensitive personal data   Mutual distrust between the environments ⇒ strong isolation needed

Processor

Linux

Private phone env

RTOS

Processor

Baseband Software

Hypervisor

Linux

Enterprise phone env

Page 41: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Why Virtualization for Embedded Systems?

Use case 2c: Security   Protect against exploits   Modem software attacked by UI exploits

•  Compromised application OS could compromise RT side

•  Could have serious consequences -  e.g. jamming cellular network

  Virtualization protects •  Separate apps and system code into

different VMs

UI SW

Attack

Access SW

Hypervisor

Core

OS OS

libjpeg

Page 42: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Why Virtualization for Embedded Systems?

Use case 3: Mobile internet device (MID) with enterprise app   MID is open device, controlled by owner   Enterprise app is closed and controlled by enterprise IT department   Hypervisor provides isolation

Hypervisor

Processor

Linux

Apps

Special-purpose OS

Enterpr. App

Page 43: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Why Virtualization for Embedded Systems?

Use case 3a: Environment with minimal trusted computing base (TCB)   Minimise exposure of highly security-critical service to other code   Avoid even an OS, provide minimal trusted environment

•  need a minimal programming environment •  goes beyond capabilities of normal hypervisor •  requires basic OS functionality

Generalised Hypervisor

Processor

Linux

Apps

Critical code

Page 44: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Why Virtualization for Embedded Systems?

Use case 3b: Point-of-sale (POS) device   May be stand-alone or integrated with other device (eg phone)   Similar: secure voice/SMS on COTS phone   Financial services providers require strong isolation

•  dedicated processor for PIN/key entry   use dedicated virtual processor ⇒ HW cost reduction   Similar: secure

voice/SMS on COTS phone

•  Military use…

Processor Processor

Linux

Apps

PIN entry Generalised Hypervisor

Processor

Linux

Apps

PIN entry

Page 45: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Use case 4: DRM on open device   Device runs Linux as app OS, uses Linux-based media player   DRM must not rely on Linux   Need trustworthy code that

•  loads media content into on-chip RAM •  decrypts and decodes content •  allows Linux-based player to disply

  Need to protect data from guest OS

Generalised Hypervisor

Processor

HLOS

Apps

Codec

Crypto

Why Virtualization for Embedded Systems?

Page 46: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Why Virtualization for Embedded Systems?

Use case 4a: IP protection in set-top box   STB runs Linux for UI, but also contains highly valuable IP

•  highly-efficient, proprietary compression algorithm   Operates in hostile environment

•  reverse engineering of algorithms   Need highly-trustworthy code that

•  loads code from Flash into on-chip RAM •  decrypts code •  runs code protected from interference

Hypervisor

Processor

Linux

Apps Decom-pression

Crypto, Secure loader

Page 47: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Why Virtualization for Embedded Systems?

Use case 5: Automotive control and infotainment   Trend to processor consolidation in automotive industry

•  top-end cars have > 100 CPUs! •  cost, complexity and space pressures to reduce by an order of magnitude •  AUTOSAR OS standard addressing this for control/convenience function

  Increasing importance of Infotainment •  driver information and entertainment

function •  not addressed by AUTOSAR

Hypervisor

Processor

Linux

UI Software

AUTOSAR

Automotive apps

  Increasing overlap of infotainment and control/convenience

•  eg park-distance control using infotainment display

•  benefits from being located on same CPU

Page 48: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Why Virtualization for Embedded Systems?

Future use case: multicore resource management (esp. power)   Hypervsior is virtualization layer that allows turning off idle resources

Hypervisor

Guest 1

VCPU VCPU VCPU VCPU

Guest 2

CPU CPU CPU CPU

Hypervisor

Guest 1

VCPU VCPU VCPU VCPU

Guest 2

CPU CPU CPU CPU

Hypervisor

Guest 1

VCPU VCPU VCPU VCPU

Guest 2

CPU CPU CPU CPU

Page 49: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Homogenous vs heterogenous guests

Linux guest

Hypervisor

Processor

Apps

Linux guest

Apps

Windows guest

Apps

  Enterprise: many similar guests •  hypervisor size irrelevant •  VMs scheduled round-robin

  Embedded: 1 HLOS + 1 RTOS •  hypervisor resource-constrained •  interrupt latencies matter

Hypervisor

Processor

Linux

UI Software

RTOS

Baseband Software

Enterprise vs Embedded Systems VMs

Page 50: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Enterprise   Independent services   Emphasis on isolation   Inter-VM communication is

secondary •  performance secondary

  VMs connected to Internet (and thus to each other)

Embedded   Integrated system   Cooperation with protection   Inter-VM communication is critically

important •  performance crucial

  VMs are subsystems accessing shared (but restricted) resources

Core Difference: Isolation vs Cooperation

Page 51: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Hypervisor Driver Driver

Processor

App

Guest

Virt Driver

Virt Driver

Trusted

App

  Drivers in hypervisor •  need to port all drivers •  huge TCB

  Drivers in privileged guest OS •  can leverage guest's driver support •  need to trust driver OS •  still huge TCB!

Hypervisor

Processor

Guest

Virt Driver

App

Dom0

Driver Driver

App

Devices in enterprise-style virtual machines   Hypervisor owns all devices

Enterprise vs Embedded Systems VMs

Page 52: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

  Use isolated drivers •  protected from other drivers •  protected from guest OSes

Hypervisor

Processor

Guest

Virt Driver

Virt Driver

App

Guest

Guest Driver

App

Native Driver

Devices in embedded virtual machines   Some devices owned by particular VM   Some devices shared   Some devices too sensitive to trust any guest   Driver OS too resource hungry

Enterprise vs Embedded Systems VMs

Page 53: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Enterprise   Round-robin scheduling of VMs   Guest OS schedules its apps

Embedded   Global view of scheduling   Schedule threads, not VMs

Task

Task

Task

VM

Task

... Task

Task

Task

VM

Task

Task

Task

Task

VM

Task

...

VM

Task

Task

VM

Task

Task

Prio1

Prio2

Prio3

Prio4

VM

Task

Task

Task

  Similar for energy management: •  energy is a global resource •  optimal per-VM energy policies are not globally optimal

Isolation vs Cooperation: Scheduling

Page 54: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Operator Stake

Modern embedded systems are multi-user devices!   Eg a phone has three classes of “users”: •  the network operator(s)

•  assets: cellular network

Buffer

Media Player

UI etc

HLOS Crypto

Codec

Priv. Data

Buffer RTOS

Comms Stack

Operator Stake

Buffer

Media Player UI etc

HLOS

Crypto

Codec

Priv. Data

Buffer

RTOS

Comms Stack

Inter-VM Communication Control

Page 55: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Media Provider Stake

Modern embedded systems are multi-user devices!   Eg a phone has three classes of “users”: •  the network operator(s)

•  assets: cellular network •  content providers

•  media content

Operator Stake

Buffer

Media Player UI etc

HLOS

Crypto

Codec

Priv. Data

Buffer

RTOS

Comms Stack

Buffer

Media Player

UI etc

HLOS Crypto

Codec

Priv. Data

Buffer RTOS

Comms Stack

Operator Stake

Media Provider Stake Buffer

Media Player UI etc

HLOS

Crypto Codec

Priv. Data

Buffer RTOS

Comms Stack

Operator Stake

Inter-VM Communication Control

Page 56: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Owner Stake

Modern embedded systems are multi-user devices!   Eg a phone has three classes of “users”: •  the network operator(s)

•  assets: cellular network •  content providers

•  media content •  the owner of the physical device

•  assets: private data, access keys

Operator Stake

Buffer

Media Player UI etc

HLOS

Crypto

Codec

Priv. Data

Buffer

RTOS

Comms Stack

Media Provider Stake Buffer

Media Player UI etc

HLOS

Crypto Codec

Priv. Data

Buffer RTOS

Comms Stack

Operator Stake

Buffer

Media Player

UI etc

HLOS Crypto

Codec

Priv. Data

Buffer RTOS

Comms Stack

Media Provider Stake

Operator Stake

Owner Stake

Buffer

Media Player UI etc

HLOS

Crypto Codec

Priv. Data

Buffer

RTOS

Comms Stack

Media Provider Stake

Operator Stake

Inter-VM Communication Control

Page 57: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Modern embedded systems are multi-user devices!   Eg a phone has three classes of “users”: •  the network operator(s)

•  assets: cellular network •  content providers

•  media content •  the owner of the physical device

•  assets: private data, access keys

Operator Stake

Buffer

Media Player UI etc

HLOS

Crypto

Codec

Priv. Data

Buffer

RTOS

Comms Stack

Media Provider Stake Buffer

Media Player UI etc

HLOS

Crypto Codec

Priv. Data

Buffer RTOS

Comms Stack

Operator Stake

Owner Stake

Buffer

Media Player UI etc

HLOS

Crypto Codec

Priv. Data

Buffer

RTOS

Comms Stack

Media Provider Stake

Operator Stake

  They are mutually distrusting •  need to protect integrity and confidentiality against

internal exploits •  need control over information flow

•  strict control over who has access to what •  strict control over communication channels

Inter-VM Communication Control

Page 58: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

  Different “users” are mutually distrusting   Need strong protection / information-flow control between them

Owner Stake

Buffer

Media Player UI etc

HLOS

Crypto Codec

Priv. Data

Buffer

RTOS

Comms Stack

Media Provider Stake

Operator Stake

  Isolation boundaries ≠ VM boundaries •  some are much smaller than VMs

-  individual buffers, programs •  some contain VMs •  some overlap VMs

  Need to define information flow between isolation domains

Inter-VM Communication Control

Linux

Microkernel

Processor

App App

App

""

"

Secure HyperCellTM

boundary

Page 59: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

High Safety/Reliability Requirements

  Software complexity is mushrooming in embedded systems too •  millions of lines of code

  Some have very high safety or reliability requirements   Need divide-and-conquer approach to software reliability

•  Highly componentised systems to enable fault tolerance

Comp

OKL4

OK Linux

App

App

App

Real Time App

Comp Comp Comp

Comp Comms Library

Object Mgr

Comp Loader

TCP/IP User Interface

File System

Network Display Flash

Page 60: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Componentization for Security — MILS

  MILS architecture: multiple independent levels of security   Approach to making security verification of complex systems tractable   Separation kernel provides strong security isolation between subsystems   High-grade verification requires small components

Separation Kernel

Processor

Domain

Isolation boundary

Domain Domain

Page 61: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Embedded Systems Requirements

  Sliding scale of isolation from individual program to VM running full-blown OS •  isolation domains, information-flow control

  Global scheduling and power management •  no strict VM-hypervisor hierarchy •  increased hypervisor-guest interaction

  High degree of sharing is essential and performance-critical •  high bandwidth, low latency communication, subject to security policies

  Real-time response •  fast and predictable switches to device driver / RT stack

  High safety/security requirements •  need to maintain minimal TCB •  need to support componentized software architecture / MILS

Virtualization in embedded systems is good, but different from enterprise •  requires more than just a hypervisor, also needs general OS functionality •  perfect match for good microkernel, such as OKL4...

Page 62: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

Hypervisors vs Microkernels

>  Both contain all code executing at highest privilege level •  Although hypervisor may contain user-mode code as well

>  Both need to abstract hardware resources •  Hypervisor: abstraction closely models hardware •  Microkernel: abstraction designed to support wide range of systems

>  What must be abstracted? •  Memory •  CPU •  I/O •  Communication

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Page 63: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

What’s the difference?

Resource Hypervisor Microkernel

Memory Virtual MMU (vMMU) Address space

CPU Virtual CPU (vCPU) Thread or scheduler activation

I/O •  Simplified virtual device •  Driver in hypervisor •  Virtual IRQ (vIRQ)

•  IPC interface to user-mode driver •  Interrupt IPC

Communication Virtual NIC, with driver and network stack

High-performance message-passing IPC

Just page tables in disguise

Just kernel-

scheduled activities

Modelled on HW, Re-uses SW

Minimal overhead,

Custom API

Real Difference?

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Page 64: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

Closer Look at I/O and Communication

>  Communication is critical for I/O •  Microkernel IPC is highly optimised •  Hypervisor inter-VM communication is frequently a bottleneck

Apps Apps Apps

Microkernel

Server Device Driver

IPC Hypervisor

Device Driver VM

OS

Device Driver

VM1

OS

Virtual Driver

Apps Apps Apps

Virtual NW

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Page 65: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

Hypervisors vs Microkernels: Summary

>  Fundamentally, both provide similar abstractions >  Optimised for different use cases

•  Hypervisor designed for virtual machines API is hardware-like to ease guest ports

•  Microkernel designed for multi-server systems seems to provide more OS-like abstractions

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Page 66: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

Hypervisors vs Microkernels: Drawbacks

Microkernels: >  Not ideal for virtualization

•  API not very effective L4 virtualization performance

close to hypervisor effort much higher

•  Virtualization needed for legacy

>  L4 model uses kernel-scheduled threads for more than exploiting parallelism

•  Kernel imposes policy •  Alternatives exist, eg. K42 uses

scheduler activations

Hypervisors: >  Communication is Achilles heel

•  More important than expected •  Critical for I/O

•  Plenty attempts to improve in Xen

>  Most hypervisors have big TCBs •  Infeasible to achieve high

assurance of security/safety •  In contrast, microkernel

implementations can be proved correct

Could we get the best of both?

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Page 67: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

Best of Both Worlds: The OKL4 Microvisor

>  Microvisor = microkernel hypervisor >  Microkernel with a hypervisor API:

•  vCPU: scheduled by microvisor on real CPU Multiple vCPUs per VM for

running on multiple cores •  vMMU: looks like a large TLB •  vIRQ: virtualize interrupts and

provide asynchronous inter-VM signals

>  Plus asynchronous channel abstraction •  for high-bandwidth, low-latency communication

>  Small TCB: ≈ 10 kLOC >  Suitable for multi-server systems

Microvisor

VM1

OS

Virtual Driver

Apps Apps Apps

Channel

Device Driver Device

Driver Device Driver

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Page 68: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

But Liedtke Said: “IPC Shall Be Synchronous!”

>  Early microkernels had poorly-performing semi-synchronous IPC •  Asynchronous send, synchronous receive •  Requires message buffering in kernel •  Each message is copied twice!

Kernel

Buffer

send (dest,buf)

Buffer

receive (*,buf)

Buffer

Copy Copy

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Page 69: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

But Liedtke Said: “IPC Shall Be Synchronous!”

>  Early microkernels had poorly-performing semi-synchronous IPC >  Liedtke designed L4 IPC fully synchronously to avoid such overheads

•  Rendezvous model: each operation blocks until partner is ready •  No buffering, message copied directly from sender to receiver •  Requires kernel-scheduled threads to avoid blocking whole app!

Kernel

send (dest,buf)

Buffer

receive (*,buf)

Buffer Copy

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Page 70: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

Hypervisors vs Microkernels: Summary

>  Fundamentally, both provide similar abstractions >  Optimised for different use cases

•  Hypervisor designed for virtual machines API is hardware-like to ease guest ports

•  Microkernel designed for multi-server systems seems to provide more OS-like abstractions

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Page 71: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

OKL4 Microvisor Communication is Asynchronous

>  In our experience, real-world embedded systems require asynchronous IPC •  Maps more naturally to hardware events •  In the past, we added asynchronous notification (effectively signals) to L4 IPC

>  The OKL4 Microvisor only provides fully asynchronous communication: •  vIRQs as asynchronous signals •  Channels for asynchronous data transfer

Still single-copy, no kernel buffers! Synchronised eg. by sending vIRQ

Kernel

Deposit data in channel buffer Buffer

read (channel, data)

Copy

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Page 72: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

Benchmark Native Virtualized Overhead null syscall 0.6 µs 0.96 µs 0.36 µs 180 cy 60 % read 1.14 µs 1.31 µs 0.17 µs 85 cy 15 % stat 4.73 µs 5.05 µs 0.32 µs 160 cy 7 % fstat 1.58 µs 2.24 µs 0.66 µs 330 cy 42 % open/close 9.12 µs 8.23 µs -0.89 µs -445 cy -10 % select(10) 2.62 µs 2.98 µs 0.36 µs 180 cy 14 % sig handler 1.77 µs 2.05 µs 0.28 µs 140 cy 16 % pipe latency 41.56 µs 54.45 µs 12.89 µs 6.4 kcy 31 % UNIX socket 52.76 µs 80.90 µs 28.14 µs 14 kcy 53 % fork 1,106 µs 1,190 µs 84 µs 42 kcy 8 % fork+execve 4,710 µs 4,933 µs 223 µs 112 kcy 5 % system 7,583 µs 7,796 µs 213 µs 107 kcy 3 %

Performance: lmbench

Beagle Board (500 MHz ARMv7) ©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License

Page 73: Virtual Machines - Computer Science and Engineeringcs9242/10/lectures/06-virt.pdf · Virtual Machines, Simulators and Emulators Simulator: • Provides functionally-accurate model

Performance: netperf on Loopback Device

Type Benchmark Native Virtualized Overhead TCP Throughput 651 [Mib/s] 630 [Mib/s] 3 %

CPU load 99 [%] 99 0 % Cost 12.5 [µ/KiB] 12.9 [µs/KiB] 3 %

UDP Throughput 537 [Mib/s] 516 [Mib/s] 4 % CPU load 99 99 0 % Cost 15.2 15.8 [µs/KiB] 4 %

Beagle Board (500 MHz ARMv7)

©2010 Gernot Heiser UNSW/NICTA. Distributed under Creative Commons Attribution License