CS 152 Computer Architecture and Engineering Lecture 22 Virtual Machines Krste Asanovic Electrical Engineering and Computer Sciences University of California Berkeley http www eecs berkeley edu krste http inst eecs berkeley edu cs152 Outline Types of Virtual Machine User level System level Techniques for implementing all or parts of a nonnative ISA on a host machine Interpreter Static binary translation Dynamic binary translation Hardware emulation 4 30 2009 CS152 Spring 09 2 What is a Virtual Machine VM Broadest definition includes all emulation methods that provide a standard software interface such as the Java VM User Virtual Machines Operating System Virtual Machines provide a complete system level environment at binary ISA Here assume ISAs always match the native hardware ISA E g IBM VM 370 VMware ESX Server and Xen Present illusion that VM users have entire computer to themselves including a copy of OS Single computer runs multiple VMs and can support a multiple different OSes On conventional platform single OS owns all HW resources With a VM multiple OSes all share HW resources Underlying HW platform is called the host and its resources used to run guest VMs user and or system 4 30 2009 CS152 Spring 09 3 Software Applications How is a software application encoded What are you getting when you buy a software application What machines will it work on Who do you blame if it doesn t work i e what contract s were violated 4 30 2009 CS152 Spring 09 4 User Virtual Machine ISA Environment ISA alone not sufficient to write useful programs need I O Direct access to memory mapped I O via load store instructions problematic time shared systems portability Operating system responsible for I O sharing devices and managing security hiding different types of hardware e g EIDE vs SCSI disks ISA communicates with operating system through some standard mechanism i e syscall instructions example convention to open file addi r1 r0 27 27 is code for file open addu r2 r0 rfname r2 points to filename string syscall cause trap into OS On return from syscall r1 holds file descriptor 4 30 2009 CS152 Spring 09 5 Application Binary Interface ABI Programs are usually distributed in a binary format that encodes the program text instructions and initial values of some data segments Virtual machine specifications include what state is available at process creation which instructions are available the ISA what system calls are possible I O or the environment The ABI is a specification of the binary format used to encode programs for a virtual machine Operating system implements the virtual machine at process startup OS reads the binary program creates an environment for it then begins to execute the code handling traps for I O calls emulation etc 4 30 2009 CS152 Spring 09 6 OS Can Support Multiple User VMs Virtual machine features change over time with new versions of operating system new ISA instructions added new types of I O are added e g asynchronous file I O Common to provide backwards compatibility so old binaries run on new OS SunOS 5 System V Release 4 Unix Solaris can run binaries compiled for SunOS4 BSD style Unix Windows 98 runs MS DOS programs If ABI needs instructions not supported by native hardware OS can provide in software 4 30 2009 CS152 Spring 09 7 ISA Implementations Partly in Software Often good idea to implement part of ISA in software Expensive but rarely used instructions can cause trap to OS emulation routine e g decimal arithmetic instructions in MicroVax implementation of VAX ISA Infrequent but difficult operand values can cause trap e g IEEE floating point denormals cause traps in almost all floating point unit implementations Old machine can trap unused opcodes allows binaries for new ISA to run on old hardware e g Sun SPARC v8 added integer multiply instructions older v7 CPUs trap and emulate 4 30 2009 CS152 Spring 09 8 Supporting Non Native ISAs Run programs for one ISA on hardware with different ISA Software Interpreter OS software interprets instructions at run time E g OS 9 for PowerPC Macs had interpreter for 68000 code Binary Translation convert at install and or load time IBM AS 400 to modified PowerPC cores DEC tools for VAX MIPS Alpha Dynamic Translation non native ISA to native ISA at run time Sun s HotSpot Java JIT just in time compiler Transmeta Crusoe x86 VLIW code morphing OS X for Intel Macs has binary translator for PowerPC Run time Hardware Emulation IBM 360 had IBM 1401 emulator in microcode Intel Itanium converts x86 to native VLIW two software visible ISAs ARM cores support 32 bit ARM 16 bit Thumb and JVM three softwarevisible ISAs 4 30 2009 CS152 Spring 09 9 Software Interpreter Fetch and decode one instruction at a time in software Interpreter Stack Guest Stack Executable on Disk Guest ISA Data Guest ISA Code 4 30 2009 Guest ISA Data Load into interpreter process memory Guest ISA Code Interpreter Data Interpreter Code CS152 Spring 09 Memory image of guest VM lives in host interpreter data memory fetch decode loop while stop inst Code PC PC 4 execute inst 10 Software Interpreter Easy to code small code footprint Slow approximately 100x slower than native execution for RISC ISA hosted on RISC ISA Problem is time taken to decode instructions 4 30 2009 fetch instruction from memory switch tables to decode opcodes extract register specifiers using bit shifts access register file data structure execute operation return to main fetch loop CS152 Spring 09 11 Binary Translation Each guest ISA instruction translates into some set of host or native ISA instructions Instead of dynamically fetching and decoding instructions at run time translate entire binary program and save result as new native ISA executable Removes interpretive fetch decode overhead Can do compiler optimizations on translated code to improve performance register allocation for values flowing between guest ISA instructions native instruction scheduling to improve performance remove unreachable code inline assembly procedures 4 30 2009 CS152 Spring 09 12 Binary Translation Take 1 Executable on Disk Executable on Disk Guest ISA Data Guest ISA Code 4 30 2009 Data unchanged Translate to native ISA code Guest ISA Data Native Data Native translation might need extra data workspace Native ISA Code CS152 Spring 09 13 Binary Translation Problems Branch and Jump targets guest code j L1 L1 lw r1 r4 jr r1 native code j translation native jump at end of block jumps to native translation of lw lw translation
View Full Document
Unlocking...