2. Story Time
• Emulation V.S. Virtualization
• Why we love emulator ?
• QEMU
• ISA translation of QEMU
• Guest Insn. Intermediate Representation Host Insn.
• Code Block Translation
• Translation Block Cache
• Translation Block Chaining
• Helper Feature of QEMU
2
3. Emulation V.S. Virtualization
• Both can be used to host VM (a.k.a hypervisor)
• Virtualization
• Share the underlying hardware as disjoin set for each VM instance.
• Host ISA is the same as Guest ISA.
• Guest operations can be directly dispatched to hardware
• Fast
• Emulation
• Everything of Guest ISA are realized by software.
• Register, Memory, I/O
• Host ISA can be differ from Guest ISA.
• Guest operations are translated into operations to the emulated devices
• Slow
3
4. Why we LOVE Emulator ?
• Everything is implemented by software!
• Everything can be customized on demands!
• Welcome to the code-tracing hell….
• Popular emulator
• QEMU
• Bochs
• QEMU is preferred in our use due to its better
performance.
• We will give more details later.
4
5. QEMU
• QEMU, a Fast and Portable Dynamic Translator
• http://static.usenix.org/event/usenix05/tech/freenix/full_pap
ers/bellard/bellard.pdf
• Supporting numerous ISA emulation
• i386
• x86_64
• arm
• mips
• ppc
• Etc.
• QEMU is also the client of Linux KVM (virtualization).
• Herein we focus only on its emulation functionality.
5
22. Translation Between Different ISA
of QEMU
• QEMU adopts an abstraction layer between the
translation.
• Tiny Code Generator (TCG), an intermediate
representation (IR) code.
Guest Host
TCG
22
23. Translation Between Different ISA
of QEMU
• QEMU adopts an abstraction layer between the
translation.
• Tiny Code Generator (TCG), an intermediate
representation (IR) code.
Guest Host
TCG
23
24. Translation Between Different ISA
of QEMU
• QEMU adopts an abstraction layer between the
translation.
• Tiny Code Generator (TCG), an intermediate
representation (IR) code.
Guest Host
TCG
24
25. Translation Between Different ISA
of QEMU
• QEMU adopts an abstraction layer between the
translation.
• Tiny Code Generator (TCG), an intermediate
representation (IR) code.
Guest Host
TCG
25
29. Code Block-based Translation
• First thought of code translation
• Interpret each encountered Guest instruction and
execute the translated code in Host (Bochs’ way).
• Recall the emulation example in page 7.
• QEMU use code block-based translation instead of
one-by-one interpretation.
• Performance improvement
29
30. What is Code Block
• Code Block/Basic Block (also called Translation Block in QEMU)
• A collection of instructions that can be SEQUENTIALLY executed.
• Each block is ended with a control-flow transfer instruction.
30
33. Translation Block Cache
• Since executing code doesn’t change often, why
don’t we stop translating the code previously
translated ?
33
34. Translation Block Cache
• Since executing code doesn’t change often, why
don’t we stop translating the code previously
translated ?
• YES! QEMU caches the translation block and index
it with the Guest physical address where the code
resides in.
34
36. Translation Block Cache
• Cache space is limited.
• Policy for cache replacement upon full cache is
required.
36
37. Translation Block Cache
• Cache space is limited.
• Policy for cache replacement upon full cache is
required.
37
38. • Assume TB1, TB2, and TB3 are all cached and going
to be sequentially executed.
• Six control flow transfer.
Translation Block Chaining
38
QEMU
TB 1
TB 2
TB 3
Find TB1 in cache & Exec
Return
Find TB2 in cache & Exec
Return
Find TB3 in cache & Exec
Return
Time
39. • When TB1, TB2, and TB3 are executed sequentially
in most case …
• Four control flow transfer. Faster
Translation Block Chaining
39
QEMU
TB 1
TB 2
TB 3
Find TB1 in cache & Exec
Return
Find TB2 in cache & Exec
Return
Find TB3 in cache & Exec
Return
Time
40. • What if the end of a TB is a conditional branch ?
( e.g. JCC group of x86 )
• Each TB has two slots for chaining
Translation Block Chaining
40
TB 1
TB 2 TB 3
True
Chain
False
Chain
42. Helper Feature of QEMU
• Helper makes the TB execution be transferred
immediately to C-function Host code.
• Advantage
• Ease of the burden of coding on complex code
translation
• Interception during TB execution
• Disadvantage
• Overhead caused by transmitting the QEMU state from
“executing translated Guest code” to “executing Host
code”
42
43. Example of Helper Use
43
qemu/helper.c
void helper_div( arg1, arg2 )
{
// Do the division job &
// Update the emulated Guest CPU/Memory
}
Guest Code TCG IR Host Code
• x86_64 TCG x86_64
44. Example of Helper Use
44
qemu/helper.c
void helper_div( arg1, arg2 )
{
// Do the division job &
// Update the emulated Guest CPU/Memory
}
Guest Code TCG IR Host Code
• x86_64 TCG x86_64
compile
helper_div:
0x7f776ce80440: push rbp
0x7f776ce80441: mov rbp, rsp
…
45. Example of Helper Use
45
qemu/helper.c
void helper_div( arg1, arg2 )
{
// Do the division job &
// Update the emulated Guest CPU/Memory
}
Guest Code TCG IR Host Code
div ecx
• x86_64 TCG x86_64
compile
helper_div:
0x7f776ce80440: push rbp
0x7f776ce80441: mov rbp, rsp
…
46. Example of Helper Use
46
qemu/helper.c
void helper_div( arg1, arg2 )
{
// Do the division job &
// Update the emulated Guest CPU/Memory
}
Guest Code TCG IR Host Code
div ecx
mov_i64 tmp0,rcx
movi_i64 tmp3,$0xf0544
st_i64 tmp3,env,$0x80
call divl_EAX,$0x0,$0,env,tmp0
• x86_64 TCG x86_64
compile
helper_div:
0x7f776ce80440: push rbp
0x7f776ce80441: mov rbp, rsp
…
Translation by calling
gen_helper_div
47. Example of Helper Use
47
qemu/helper.c
void helper_div( arg1, arg2 )
{
// Do the division job &
// Update the emulated Guest CPU/Memory
}
Guest Code TCG IR Host Code
div ecx
mov_i64 tmp0,rcx
movi_i64 tmp3,$0xf0544
st_i64 tmp3,env,$0x80
call divl_EAX,$0x0,$0,env,tmp0
movq $0xf0544,0x80(%r14)
mov %r14,%rdi
mov $0xa,%esi
callq 0x7f776ce80440
• x86_64 TCG x86_64
compile
helper_div:
0x7f776ce80440: push rbp
0x7f776ce80441: mov rbp, rsp
…
Translation by calling
gen_helper_div
Generate Host Code for div ecx emulation
48. Example of Helper Use
48
qemu/helper.c
void helper_div( arg1, arg2 )
{
// Do the division job &
// Update the emulated Guest CPU/Memory
}
Guest Code TCG IR Host Code
div ecx
mov_i64 tmp0,rcx
movi_i64 tmp3,$0xf0544
st_i64 tmp3,env,$0x80
call divl_EAX,$0x0,$0,env,tmp0
movq $0xf0544,0x80(%r14)
mov %r14,%rdi
mov $0xa,%esi
callq 0x7f776ce80440
• x86_64 TCG x86_64
compile
helper_div:
0x7f776ce80440: push rbp
0x7f776ce80441: mov rbp, rsp
…
Translation by calling
gen_helper_div
Generate Host Code for div ecx emulation
49. Example of Helper Use
49
qemu/helper.c
void helper_div( arg1, arg2 )
{
// Do the division job &
// Update the emulated Guest CPU/Memory
}
Guest Code TCG IR Host Code
div ecx
mov_i64 tmp0,rcx
movi_i64 tmp3,$0xf0544
st_i64 tmp3,env,$0x80
call divl_EAX,$0x0,$0,env,tmp0
movq $0xf0544,0x80(%r14)
mov %r14,%rdi
mov $0xa,%esi
callq 0x7f776ce80440
• x86_64 TCG x86_64
compile
helper_div:
0x7f776ce80440: push rbp
0x7f776ce80441: mov rbp, rsp
…
Translation by calling
gen_helper_div
Generate Host Code for div ecx emulation
50. When does QEMU Use Helper ?
• Translating an instruction which results in complex
and numerous TCG IR generation.
• e.g. div of x86
• Interception to the execution of translated
instruction is required. (like hook)
• e.g. jcc of x86
• … (might be more cases. I haven’t fully comprehended)
50
51. More QEMU-related System of
DSNSLab
• SecMap
• Two-layer Disk Forensics
• MrKIP
• VMaware Detector
• Cloudebug
• ProbeBuilder
• Android Taint
51