• No results found

E6998 Virtual Machines. CPU Virtualization. Scott Devine

N/A
N/A
Protected

Academic year: 2021

Share "E6998 Virtual Machines. CPU Virtualization. Scott Devine"

Copied!
31
0
0

Loading.... (view fulltext now)

Full text

(1)

E6998 ‐ Virtual Machines Lecture 2

Lecture 2

CPU Virtualization

Scott Devine VMware, Inc.

VMware, Inc.

(2)

Outline

• CPU Background

l h

• Virtualization Techniques

– System ISA Virtualization

i i

– Instruction Interpretation – Trap and Emulate

Binary Translation

– Binary Translation

– Hybrid Models

(3)

Computer System Organization

CPU

Memory

MMU Controller

Local Bus

Interface

High‐Speed I/O Bus

NIC Controller Bridge Frame  Buffer

LAN LAN

Low‐Speed I/O Bus

USB CD‐ROM

(4)

CPU Organization

• Instruction Set Architecture (ISA)

D fi

Defines:

– the state visible to the programmer 

• registers and memory g y

– the instruction that operate on the state

• ISA typically divided into 2 parts yp y p

– User ISA

• Primarily for computation

– System ISA

• Primarily for system resource management

(5)

User ISA ‐ State

Special‐Purpose Registers Program Counter

Condition Codes

User Virtual  Memory

General‐Purpose Registers

Floating Point Registers

y Reg 0

Reg 1

FP 0 FP 1

Reg n‐1 FP n‐1

(6)

User ISA – Instructions

Integer

Typical Instruction Pipeline

Fetch Registers Issue Integer

Memory Decode

FP

Add S b

Load byte L d W d

Jump

J l

Add single M lt d bl Integer Memory Control Flow Floating Point

Sub And Compare

Load Word Store Multiple Push

Jump equal Call

Return

Mult. double Sqrt double

Instruction Groupings

(7)

System ISA

• Privilege Levels

• Control Registers

User

Control Registers

• Traps and Interrupts – Hardcoded Vectors

System

– Dispatch Table

• System Clock

• MMU

User

– Page Tables TLB

User Extension

Kernel Level 0

– TLB

• I/O Device Access

Level 0 Level 1 Level 2

(8)

Outline

• CPU Background

l h

• Virtualization Techniques

– System ISA Virtualization

i i

– Instruction Interpretation – Trap and Emulate

Binary Translation

– Binary Translation

– Hybrid Models

(9)

Virtualizing the System ISA

• Hardware needed by monitor

– Ex: monitor must control real hardware interrupts Ex: monitor must control real hardware interrupts

• Access to hardware would allow VM to  compromise isolation boundaries

compromise isolation boundaries

– Ex: access to MMU would allow VM to write any page

• So…

– All access to the virtual System ISA by the guest must be  emulated by the monitor in software.

S t t t k t i

– System state kept in memory.

– System instructions are implemented as functions in the 

monitor.

(10)

Example: CPUState

static struct {

uint32 GPR[16];

void CPU_CLI(void) {

uint32 GPR[16];

uint32 LR;

uint32 PC;

int IE;

int IRQ;

{

CPUState.IE = 0;

}

void CPU_STI(void)

} CPUState; {

CPUState.IE = 1;

}

G l f CPU i t li ti t h i

• Goal for CPU virtualization techniques

– Process normal instructions as fast as possible 

Forward privileged instructions to emulation routines

– Forward privileged instructions to emulation routines

(11)

Isomorphism

(S ) Guest

S i e(S

i

) S j

S ’ e’(S

i

’) S ’

V(S

i

) V(S

j

)

Host

S i

i

S j

Formally, virtualization involves the construction of  an isomorphism from guest state to host state

an isomorphism from guest state to host state.

(12)

Instruction Interpretation

• Emulate Fetch/Decode/Execute pipeline in  software

software

• Postives

– Easy to implement – Minimal complexity

• Negatives

– Slow!

(13)

Example: Virtualizing the Interrupt Flag w/ Instruction Interpreter

void CPU_Run(void) {

while (1) {

inst = Fetch(CPUState.PC);

void CPU_CLI(void) {

CPUState.IE = 0;

} CPUState.PC += 4;

switch (inst) { case ADD:

C S G [ d]

void CPU_STI(void) {

CPUState.IE = 1;

} CPUState.GPR[rd]

= GPR[rn] + GPR[rm];

break;

case CLI:

CPU CLI();

void CPU_Vector(int exc) {

CPUState.LR = CPUState.PC;

CPUState.PC = disTab[exc];

} CPU_CLI();

break;

case STI:

CPU_STI();

break;

}

}

if (CPUState.IRQ

&& CPUState.IE) { CPUState.IE = 0;

CPU_Vector(EXC_INT);

} } } }

(14)

Trap and Emulate

Guest OS + Applications e d U nprivileg e

Page Fault

Undef Instr

vIRQ

U

vIRQ

MMU Emulation

CPU Emulation

I/O

Emulation vileg e d

Virtual Machine Monitor

Emulation Emulation Emulation

Pri v

(15)

“Strictly Virtualizable”

A processor or mode of a processor is strictly  virtualizable if when executed in a lesser virtualizable if, when executed in a lesser  privileged mode:

ll h l d

• all instructions that access privileged state  trap

• all instructions either trap or execute  identically

• …

(16)

Issues with Trap and Emulate

• Not all architectures support it b h h

• Trap costs may be high

• Monitor uses a privilege level

– Need to virtualize the protection levels

(17)

Binary Translator

Guest  Code Code

Translator

Translation  Callouts

TC CPU 

Emulation

Cache Callouts

Index Emulation

Routines

(18)

Basic Blocks

Guest Code

vPC

mov ebx, eax

cli

and ebx, ~0xfff

Straight‐line code

B i Bl k

mov ebx, cr3 sti

ret

Control flow

Basic Block

(19)

Binary Translation

Guest Code Translation Cache

vPC

mov ebx, eax

cli

and ebx, ~0xfff

mov ebx, eax call HANDLE_CLI and ebx, ~0xfff

start

mov ebx, cr3 sti

ret

mov [CO_ARG], ebx call HANDLE_CR3 call HANDLE_STI jmp HANDLE_RET

(20)

Binary Translation

Guest Code Translation Cache

vPC

mov ebx, eax

cli

and ebx, ~0xfff

mov ebx, eax mov [CPU_IE], 0 and ebx, ~0xfff

start

mov ebx, cr3 sti

ret

mov [CO_ARG], ebx call HANDLE_CR3 mov [CPU_IE], 1 test [CPU_IRQ], 1 jne

call HANDLE_INTS j

jmp HANDLE_RET

(21)

Basic Binary Translator

void BT_Run(void) {

CPUState.PC = _start;

void *BTTranslate(uint32 pc) {

void *start = TCTop;

BT_Continue();

}

void BT_Continue(void) {

void *tcpc;

uint32 TCPC = pc;

while (1) {

inst = Fetch(TCPC);

TCPC += 4;

void *tcpc;

tcpc = BTFindBB(CPUState.PC);

if (!tcpc) {

tcpc = BTTranslate(CPUState.PC);

if (IsPrivileged(inst)) { EmitCallout();

} else if (IsControlFlow(inst)) { EmitEndBB();

break;

p ( );

}

RestoreRegsAndJump(tcpc);

}

; } else {

/* ident translation */

EmitInst(inst);

} }

return start;

}

(22)

Basic Binary Translator – Part 2

void BT_CalloutSTI(BTSavedRegs regs) {

CPUState.PC = BTFindPC(regs.tcpc);

CPUState.GPR[] = regs.GPR[];

CPU_STI();

CPUState.PC += 4;

if (CPUState.IRQ

&& CPUState.IE) { CPUVector();

BT_Continue();

/* NOT REACHED */

/ _ /

}

return;

}

(23)

Controlling Control Flow

Guest Code Translation Cache

vEPC

test eax, 1 jeq

add ebx, 18

test eax, 1 jeq

call END_BB

start

mov ecx, [ebx]

mov [ecx], eax

call END_BB

ret

(24)

Controlling Control Flow

Guest Code Translation Cache

vEPC

test eax, 1 jeq

add ebx, 18

test eax, 1 jeq

call END_BB mov ecx, [ebx]

mov [ecx], eax

call END_BB

ret

add ebx, 18 mov ecx, [ebx]

find next

mov [ecx], eax call HANDLE_RET

eax == 0

(25)

Controlling Control Flow

Guest Code Translation Cache

vEPC

test eax, 1 jeq

add ebx, 18

test eax, 1 jeq

jmp mov ecx, [ebx]

mov [ecx], eax

call END_BB

ret

add ebx, 18 mov ecx, [ebx]

mov [ecx], eax call HANDLE_RET

eax == 0

(26)

Controlling Control Flow

Guest Code Translation Cache

test eax, 1 jeq

add ebx, 18

test eax, 1 jeq

jmp

vEPC

mov ecx, [ebx]

mov [ecx], eax

call END_BB

ret

add ebx, 18

mov ecx, [ebx]

find

mov [ecx], eax call HANDLE_RET

next

mov [ecx], eax ll

eax == 1

call HANDLE_RET

(27)

Controlling Control Flow

Guest Code Translation Cache

test eax, 1 jeq

add ebx, 18

test eax, 1 jeq

jmp

vEPC

mov ecx, [ebx]

mov [ecx], eax

jmp

ret

add ebx, 18 mov ecx, [ebx]

mov [ecx], eax call HANDLE_RET mov [ecx], eax

ll

eax == 1

call HANDLE_RET

(28)

Issues with Binary Translation

• Translation cache index data structure h

• PC Synchronization on interrupts

• Self‐modifying code

– Notified on writes to translated guest code

(29)

Other Uses for Binary Translation

• Cross ISA translators

Di it l FX!32 – Digital FX!32

• Optimizing translators

– H.P. Dynamo

• High level language byte code translators

– Java

– .NET/CLI

(30)

Hybrid Approach

DirectExec OK?

Direct Execution Jump to Guest PC

Yes Trap

OK? Jump to Guest PC

Handle Priv.

Instruction

No Execute Callout

In TC TC 

Validate

No Callout

• Binary Translation for the Kernel

• Direct Execution (Trap‐and‐emulate) for the User Direct Execution (Trap and emulate) for the User

• U.S. Patent 6,397,242

(31)

Homework 1 Binary Patching Binary Patching

Binary Patching for profiling and code coverage

– Process Virtualization

– Patching code be compiled into program

• Follow execution by patching control flow instructions

• Patch instructions in‐place Patch instructions in place

– No need for translation or copying

• Instruction decoding need only determine

– Length of instruction – Length of instruction – Control flow points

• Callouts call through asm linkage

Saves EFLAGS

– Saves EFLAGS

– Saves registers

– Calls C code

References

Related documents

Nowdays, induction machines have become the prime source of electromechanical energy co n version in modern society, from miniature drives to the large industrial

(The author is a tax senior with Punongbayan & Araullo’s tax advisory and compliance division. P&A is a leading audit, tax and an advisory service firm and is the

With respect to forensics, there are several po- tential indicators (due to the nature of rolling code schemes) that the remote control may have been cloned: If the vehicle does

Another key difference between women in the CFP ® Professional Community and women outside is their perception of the ethical nature of the financial planning profession:

Honeypots are typically virtual machines, designed to emulate real machines, feigning or creating the appearance of running full services and applications, with

Originally created in a dictionary and phonetic transcription allows linguists as a relatively small, the syllable into a phonemic status of the transcriptions of a narrow is

With that said, it’s nothing about the building, it’s kind of small for my…if we are going to get into this, why aren’t we finding a location, building a building ourselves

Choice of fund (Choice) gives eligible employees the right to choose the fund they want their employer Superannuation Guarantee (SG) contributions paid into, rather than requiring