QEMU internals Chad D. Kersey January 28, 2009 Chad D. Kersey - - PowerPoint PPT Presentation

qemu internals
SMART_READER_LITE
LIVE PREVIEW

QEMU internals Chad D. Kersey January 28, 2009 Chad D. Kersey - - PowerPoint PPT Presentation

The basics Dynamic translation Basic Block Chaining The codebase Acknowledgments Questions QEMU internals Chad D. Kersey January 28, 2009 Chad D. Kersey QEMU internals The basics Dynamic translation Basic Block Chaining The codebase


slide-1
SLIDE 1

The basics Dynamic translation Basic Block Chaining The codebase Acknowledgments Questions

QEMU internals

Chad D. Kersey January 28, 2009

Chad D. Kersey QEMU internals

slide-2
SLIDE 2

The basics Dynamic translation Basic Block Chaining The codebase Acknowledgments Questions

Where to get the source

svn co svn://svn.savannah.nongnu.org/qemu Make sure you have the latest sources if you’re reading along. A lot has changed since the previous release.

Chad D. Kersey QEMU internals

slide-3
SLIDE 3

The basics Dynamic translation Basic Block Chaining The codebase Acknowledgments Questions

Functional simulation

Simulate what a processor does, not how it does it. Needs separate model for timing analysis (if needed). Faster than “cycle-accurate” simulators. Good enough to use applications written for another CPU.

Chad D. Kersey QEMU internals

slide-4
SLIDE 4

The basics Dynamic translation Basic Block Chaining The codebase Acknowledgments Questions

QEMU system simulation

QEMU simulates VGA, serial, and ethernet. hw/* contain all of the supported boards. Includes rather complete PC, Nokia N-series, PCI ultrasparc. Various development boards in varying levels of completion.

Chad D. Kersey QEMU internals

slide-5
SLIDE 5

The basics Dynamic translation Basic Block Chaining The codebase Acknowledgments Questions

What dynamic translation isn’t

Interpreters execute instructions one at a time. Significant slowdown from constant overhead. Easier to write and debug than dynamic translators.

Static Code Data Flow Control Flow Guest Code

Chad D. Kersey QEMU internals

slide-6
SLIDE 6

The basics Dynamic translation Basic Block Chaining The codebase Acknowledgments Questions

What dynamic translation is

Dynamic translators convert code as needed. Try to spend most time executing in translation cache. Translate basic blocks as needed. Store translated blocks in code cache.

. . . Static Code Data Flow Control Flow Translation Cache . . . Generated Code Guest Code

Chad D. Kersey QEMU internals

slide-7
SLIDE 7

The basics Dynamic translation Basic Block Chaining The codebase Acknowledgments Questions

Getting into and out of the code cache

cpu exec() called each time around main loop. Program executes until an unchained block is encountered. Returns to cpu exec() through epilogue.

Epilogue Prologue . . . Pre−generated code Translation Cache . . . Code cpu_exec()

Chad D. Kersey QEMU internals

slide-8
SLIDE 8

The basics Dynamic translation Basic Block Chaining The codebase Acknowledgments Questions

Portable dynamic translation

gen_intermediate_code() Guest Code TCG Operations tcg_gen_code() Host Code

QEMU uses an intermediate form. Frontends are in target-*/ Backends are in tcg/*/ Selected with preprocessor evil.

Chad D. Kersey QEMU internals

slide-9
SLIDE 9

The basics Dynamic translation Basic Block Chaining The codebase Acknowledgments Questions

Portable dynamic translation: stage 1

gen_intermediate_code() Guest Code TCG Operations tcg_gen_code() Host Code

mov %esp,%ebp not %eax add %eax,%edx mov %edx,%eax xor $0x55555555,%eax push %ebp ret pop %ebp Chad D. Kersey QEMU internals

slide-10
SLIDE 10

The basics Dynamic translation Basic Block Chaining The codebase Acknowledgments Questions

Portable dynamic translation: stage 2

gen_intermediate_code() Guest Code TCG Operations tcg_gen_code() Host Code

ld_i32 tmp2,env,$0x10 qemu_ld32u tmp0,tmp2,$0xffffffff ld_i32 tmp4,env,$0x10 movi_i32 tmp14,$0x4 add_i32 tmp4,tmp4,tmp14 st_i32 tmp4,env,$0x10 st_i32 tmp0,env,$0x20 movi_i32 cc_op,$0x18 exit_tb $0x0

. . .

Chad D. Kersey QEMU internals

slide-11
SLIDE 11

The basics Dynamic translation Basic Block Chaining The codebase Acknowledgments Questions

Portable dynamic translation: stage 3

gen_intermediate_code() Guest Code TCG Operations tcg_gen_code() Host Code

mov 0x10(%ebp),%eax mov 0x10(%ebp),%edx mov (%ecx),%eax mov %eax,%ecx add $0x4,%edx mov %edx,0x10(%ebp) mov %eax,0x20(%ebp) mov $0x18,%eax mov %eax,0x30(%ebp) xor %eax,%eax jmp 0xba0db428 . . . /*This represents just the ret instruction!*/ Chad D. Kersey QEMU internals

slide-12
SLIDE 12

The basics Dynamic translation Basic Block Chaining The codebase Acknowledgments Questions

Basic block chaining

Returning from code cache is slow. Solution: jump directly between basic blocks! Make space for a jump, follow by a return to the epilogue. Every time a block returns, try to chain it.

Epilogue Prologue TB TB TB TB Pre−generated code Translation Cache cpu_exec()

Chad D. Kersey QEMU internals

slide-13
SLIDE 13

The basics Dynamic translation Basic Block Chaining The codebase Acknowledgments Questions

Basic block chaining: step 1

Epilogue Prologue TB Pre−generated code Translation Cache TB TB TB cpu_exec()

Chad D. Kersey QEMU internals

slide-14
SLIDE 14

The basics Dynamic translation Basic Block Chaining The codebase Acknowledgments Questions

Basic block chaining: step 2

Epilogue Prologue TB TB Pre−generated code Translation Cache TB TB cpu_exec()

Chad D. Kersey QEMU internals

slide-15
SLIDE 15

The basics Dynamic translation Basic Block Chaining The codebase Acknowledgments Questions

Basic block chaining: step 3

Epilogue Prologue TB TB TB Pre−generated code Translation Cache TB cpu_exec()

Chad D. Kersey QEMU internals

slide-16
SLIDE 16

The basics Dynamic translation Basic Block Chaining The codebase Acknowledgments Questions

Basic block chaining: step 4

Epilogue Prologue TB TB TB Pre−generated code cpu_exec() Translation Cache TB

Chad D. Kersey QEMU internals

slide-17
SLIDE 17

The basics Dynamic translation Basic Block Chaining The codebase Acknowledgments Questions

Basic block chaining: step 5

Epilogue Prologue TB TB TB TB Pre−generated code Translation Cache cpu_exec()

Chad D. Kersey QEMU internals

slide-18
SLIDE 18

The basics Dynamic translation Basic Block Chaining The codebase Acknowledgments Questions

Unchain on interrupt

Now how do we interrupt the processor? Have another thread unchain the blocks.

Epilogue Prologue TB TB TB TB Pre−generated code Translation Cache cpu_exec() cpu_interrupt()

Chad D. Kersey QEMU internals

slide-19
SLIDE 19

The basics Dynamic translation Basic Block Chaining The codebase Acknowledgments Questions

Code organization

TranslationBlock structure in translate-all.h Translation cache is code gen buffer in exec.c cpu-exec() in cpu-exec.c orchestrates translation and block chaining. target-*/translate.c: guest ISA specific code. tcg-*/*/: host ISA specific code. linux-user/*: Linux usermode specific code. vl.c: Main loop for system emulation. hw/*: Hardware, including video, audio, and boards.

Chad D. Kersey QEMU internals

slide-20
SLIDE 20

The basics Dynamic translation Basic Block Chaining The codebase Acknowledgments Questions

Ways to have fun

Add extra instructions to an ISA. Generate execution traces to drive timing models. Try to integrate timing models. Retarget frontend or backend. Improve optimization, say, by retaining chaining across interrupts.

Chad D. Kersey QEMU internals

slide-21
SLIDE 21

The basics Dynamic translation Basic Block Chaining The codebase Acknowledgments Questions

Acknowledgments

QEMU by Fabrice Bellard: www.bellard.org/ Current qemu-internals: http://bellard.org/qemu/qemu-tech.html Some graphics in these slides part of work funded by DOE grant.

Chad D. Kersey QEMU internals

slide-22
SLIDE 22

The basics Dynamic translation Basic Block Chaining The codebase Acknowledgments Questions

Questions?

?

Chad D. Kersey QEMU internals