Skip to contentSkip to conversation

TCG flow vs dyngen

Qemu-devel Stefano Bonifazi 2 participants 7 messages Dec 10, 2010 – Dec 14, 2010

lore
Share & tools
lore
#0 lore raw

Hi all!
From the technical documentation
(http://www.usenix.org/publications/library/proceedings/usenix05/tech/freenix/bellard.html) I read:

Quoted 1 line

The first step is to split each target CPU instruction into fewer simpler instructions called /micro operations/. Each micro operation is implemented by a small piece of C code. This small C source code is compiled by GCC to an object file. The micro operations are chosen so that their number is much smaller (typically a few hundreds) than all the combinations of instructions and operands of the target CPU. The translation from target CPU instructions to micro operations is done entirely with hand coded code. A compile time tool called dyngen uses the object file containing the micro operations as input to generate a dynamic code generator. This dynamic code generator is invoked at runtime to generate a complete host function which concatenates several micro operations.

instead from wikipedia(http://en.wikipedia.org/wiki/QEMU) and other sources I read:

Quoted 1 line

The Tiny Code Generator (TCG) aims to remove the shortcoming of relying on a particular version of GCC <http://en.wikipedia.org/wiki/GNU_Compiler_Collection> or any compiler, instead incorporating the compiler (code generator) into other tasks performed by QEMU in run-time. The whole translation task thus consists of two parts: blocks of target code (/TBs/) being rewritten in *TCG ops* - a kind of machine-independent intermediate notation, and subsequently this notation being compiled for the host's architecture by TCG. Optional optimisation passes are performed between them.

- So, I think that the technical documentation is now obsolete, isn't it?

- The "old way" used much offline (compile time) work compiling the micro operations into host machine code, while if I understand well, TCG does everything in run-time(please correct me if I am wrong!).. so I wonder, how can it be as fast as the previous method (or even faster)?

- If I understand well, TGC runtime flow is the following:
- TCG takes the target binary, and splits it into target blocks
- if the TB is not cached, TGC translates it (or better the target
instructions it is composed by) into TCG micro ops,
- TGC compiles TGC uops into host object code,
- TGC caches the TB,
- TGC tries to chain the block with others,
- TGC copies the TB into the execution buffer
- TGC runs it
Am I right? Please correct me, whether I am wrong, as I wanna use that flow scheme for trying to understand the code.. Thank you very much in advance!
Stefano B.

#1 lore raw
Quoted 30 lines · Stefano Bonifazi

Hi all!
 From the technical documentation
(http://www.usenix.org/publications/library/proceedings/usenix05/tech/freenix/bellard.html)
I read:

The first step is to split each target CPU instruction into fewer simpler instructions called micro operations. Each micro operation is implemented by a small piece of C code. This small C source code is compiled by GCC to an object file. The micro operations are chosen so that their number is much smaller (typically a few hundreds) than all the combinations of instructions and operands of the target CPU. The translation from target CPU instructions to micro operations is done entirely with hand coded code.

A compile time tool called dyngen uses the object file containing the micro operations as input to generate a dynamic code generator. This dynamic code generator is invoked at runtime to generate a complete host function which concatenates several micro operations.

instead from wikipedia(http://en.wikipedia.org/wiki/QEMU) and other sources I read:

The Tiny Code Generator (TCG) aims to remove the shortcoming of relying on a particular version of GCC or any compiler, instead incorporating the compiler (code generator) into other tasks performed by QEMU in run-time. The whole translation task thus consists of two parts: blocks of target code (TBs) being rewritten in TCG ops - a kind of machine-independent intermediate notation, and subsequently this notation being compiled for the host's architecture by TCG. Optional optimisation passes are performed between them.

Quoted 1 line

- So, I think that the technical documentation is now obsolete, isn't it?

At least we shouldn't link to that paper anymore. There's also documentation generated from qemu-tech.texi that should be up to date.

Quoted 4 lines

- The "old way" used much offline (compile time) work compiling the micro operations into host machine code, while if I understand well, TCG does everything in run-time(please correct me if I am wrong!).. so I wonder, how can it be as fast as the previous method (or even faster)?

The dyngen way was to extract machine instructions for each micro-op from an object file (op.o) compiled by GCC during QEMU build. TCG instead generates the instructions directly. Since the whole host register set is available for the micro-ops (in contrast to fixed T0/T1/T2 used by dyngen), TCG should outperform dyngen in some cases. In other cases, GCC may have used some optimization when generating the op which would be too complex to implement by the TCG generator so the dyngen op may have been more optimal.

The old way was not portable to GCC 4.x series. Now it might be even possible to replace GCC extensions with something else and use other compilers.

Quoted 4 lines

- If I understand well, TGC runtime flow is the following:
    - TCG takes the target binary, and splits it into target blocks
    - if the TB is not cached, TGC translates it (or better the target instructions it is composed by) into TCG micro ops,

The above is not the job of TCG (which is host specific), but the target specific translators (target-*/translate.c).

Quoted 1 line

    - TGC compiles TGC uops into host object code,

OK.

Quoted 2 lines

    - TGC caches the TB,
    - TGC tries to chain the block with others,

The above is part of the CPU execution loop (cpu-exec.c), TCG is not involved anymore.

Quoted 1 line

    - TGC copies the TB into the execution buffer

There is no copying.

Quoted 3 lines

    - TGC runs it
Am I right? Please correct me, whether I am wrong, as I wanna use that flow scheme for trying to understand the code..

Otherwise right.

#2 lore raw

Thank you very very much! I'd take months for understanding everything myself from the source code! :)

Quoted 7 lines · Blue Swirl
Quoted 3 lines · Stefano Bonifazi

[..]

- So, I think that the technical documentation is now obsolete, isn't it?

At least we shouldn't link to that paper anymore. There's also documentation generated from qemu-tech.texi that should be up to date.

Do you mean this:
http://www.weilnetz.de/qemu-tech.html
?

Quoted 6 lines
Quoted 4 lines

- If I understand well, TCG runtime flow is the following:
- TCG takes the target binary, and splits it into target blocks
- if the TB is not cached, TCG translates it (or better the target
instructions it is composed by) into TCG micro ops,

The above is not the job of TCG (which is host specific), but the target specific translators (target-*/translate.c).

Ok, then considering QEMU flow instead of simply TCG, do those steps take place in the order I considered?

Quoted 4 lines
Quoted 2 lines

- TCG caches the TB,
- TCG tries to chain the block with others,

The above is part of the CPU execution loop (cpu-exec.c), TCG is not involved anymore.

Ok! Thank you, now I have a clearer idea of where different operations are implemented.. but again considering the whole QEMU flow, are the steps I reported executed in the order I put them?

Quoted 2 lines
Quoted 1 line

- TCG copies the TB into the execution buffer

There is no copying.

Does that mean TCG produces the host object code directly into the emulator's memory for it to fetch? Or does TCG make the emulator even execute that object code as soon as it is produced? But, if the object code is consumed on the fly, it means there is no cashing of it, is it there?
What is actually cached? Only target blocks? Their translation into TCG uops? Host binary code generated by TCG?

Again many many thanks!!!
Stefano B.

#3 lore raw
Quoted 10 lines · Stefano Bonifazi

Thank you very very much! I'd take months for understanding everything myself from the source code! :)

Quoted 6 lines · Blue Swirl
Quoted 3 lines · Stefano Bonifazi

[..]

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

- So, I think that the technical documentation is now obsolete, isn't it?

At least we shouldn't link to that paper anymore. There's also documentation generated from qemu-tech.texi that should be up to date.

Do you mean this:
http://www.weilnetz.de/qemu-tech.html
?

Yes.

Quoted 10 lines
Quoted 7 lines
Quoted 4 lines

- If I understand well, TCG runtime flow is the following:
    - TCG takes the target binary, and splits it into target blocks
    - if the TB is not cached, TCG translates it (or better the target instructions it is composed by) into TCG micro ops,

The above is not the job of TCG (which is host specific), but the target specific translators (target-*/translate.c).

Ok, then considering QEMU flow instead of simply TCG, do those steps take place in the order I considered?

Yes, that's about it.

Quoted 14 lines
Quoted 5 lines
Quoted 2 lines

    - TCG caches the TB,
    - TCG tries to chain the block with others,

The above is part of the CPU execution loop (cpu-exec.c), TCG is not involved anymore.

Ok! Thank you, now I have a clearer idea of where different operations are implemented.. but again considering the whole QEMU flow, are the steps I reported executed in the order I put them?

Quoted 4 lines
Quoted 2 lines

    - TCG copies the TB into the execution buffer

There is no copying.

Quoted 7 lines

Does that mean TCG produces the host object code directly into the emulator's memory for it to fetch? Or does TCG make the emulator even execute that object code as soon as it is produced?
But, if the object code is consumed on the fly, it means there is no cashing of it, is it there?
What is actually cached? Only target blocks? Their translation into TCG uops? Host binary code generated by TCG?

There's a large buffer for generated code, allocated in exec.c. This is filled with host code by TCG, when full it is flushed. The CPU execution loop generates new TBs when needed, otherwise the old code can be executed.

TCG also uses intermediate ops but those are used only once during translation.

#4 lore raw

-----Original Message-----
From: Blue Swirl [mailto:[email protected]]
Sent: sabato 11 dicembre 2010 14:12
To: Stefano Bonifazi
Cc: [email protected]
Subject: Re: [Qemu-devel] TCG flow vs dyngen

Quoted 3 lines

There's a large buffer for generated code, allocated in exec.c. This is filled with host code by TCG, when full it is flushed. The CPU execution loop >generates new TBs when needed, otherwise the old code can be executed.

TCG also uses intermediate ops but those are used only once during translation.

So if I understand well the flow is the following:

1) the CPU execution loop at runtime takes a new TB from the target code
2) I guess some hash function is computed on this TB for getting a key for searching into the buffer of generated code that probably should store the binary as a map key-binary
3) if the search is successful the binary is given to the translator(how? You said no copy involved) and we return to point 1) otherwise:
4) the target specific translator generates TCG uops from the TB
5) TCG uses uops for generating host binary code
6) this new binary code is cached by TGC if there is enough storage place

Is that all correct?

Where does the execution of host binary take place in the previous list of events? Between point 5) and 6) ? After 6) ? In what QEMU source code file/function does the final execution of host binary take place?

In the previous list of events, when does the translator try to chain the current TB with previous ones? Before TCG generates the binary in order to feed it with "linked micro code"?

Thank you very very much! :)
Stefano B.

#5 lore raw
Quoted 10 lines · Stefano Bonifazi

-----Original Message-----
From: Blue Swirl [mailto:[email protected]]
Sent: sabato 11 dicembre 2010 14:12
To: Stefano Bonifazi
Cc: [email protected]
Subject: Re: [Qemu-devel] TCG flow vs dyngen

Quoted 1 line

There's a large buffer for generated code, allocated in exec.c. This is filled with host code by TCG, when full it is flushed. The CPU execution loop >generates new TBs when needed, otherwise the old code can be executed.

Quoted 7 lines
Quoted 1 line

TCG also uses intermediate ops but those are used only once during translation.

So if I understand well the flow is the following:

1) the CPU execution loop at runtime takes a new TB from the target code
2) I guess some hash function is computed on this TB for getting a key for searching into the buffer of generated code that probably should store the binary as a map key-binary
3) if the search is successful the binary is given to the translator(how? You said no copy involved) and we return to point 1) otherwise:

1-3) Please see tb_find_fast() and its caller in cpu-exec.c. Only pointer passing is involved.

Quoted 5 lines

4) the target specific translator generates TCG uops from the TB
5) TCG uses uops for generating  host binary code
6) this new binary code is cached by TGC if there is enough storage place

Is that all correct?

4-5) OK.
6) If there is no space, all previously generated code is thrown away.

Quoted 5 lines

Where does the execution of host binary take place in the previous list of events?  Between point 5) and 6) ? After 6) ? In what QEMU source code file/function does the final execution of host binary take place?

In the previous list of events, when does the translator try to chain the current TB with previous ones?  Before TCG generates the binary in order to feed it with "linked micro code"?

All of this happens in cpu-exec.c:581 to 618.

#6 lore raw
Quoted 7 lines · Blue Swirl
Quoted 4 lines · Stefano Bonifazi

Where does the execution of host binary take place in the previous list of events? Between point 5) and 6) ? After 6) ? In what QEMU source code file/function does the final execution of host binary take place?

In the previous list of events, when does the translator try to chain the current TB with previous ones? Before TCG generates the binary in order to feed it with "linked micro code"?

All of this happens in cpu-exec.c:581 to 618.

Hi!
Thank you very much! Knowing exactly where I should check, in a so big project helped me very much!! Anyway after having spent more than 2 days on that code I still can't understand how it works the real execution:

in cpu-exec.c : cpu_exec_nocache i find:

Quoted 2 lines

/* execute the generated code */

    next_tb = tcg_qemu_tb_exec(tb->tc_ptr);

and in cpu-exec.c : cpu_exec

Quoted 3 lines

/* execute the generated code */

                    next_tb = tcg_qemu_tb_exec(tc_ptr);

so I thought tcg_qemu_tb_exec "function" should do the work of executing the translated binary in the host. But then I found out it is just a define in tcg.h:

Quoted 1 line

#define tcg_qemu_tb_exec(tb_ptr) ((long REGPARM (*)(void *))code_gen_prologue)(tb_ptr)

and again in exec.c

Quoted 1 line

uint8_t code_gen_prologue[1024] code_gen_section;

Maybe I have some problems with that C syntax, but I really don't understand what happens there.. how the execution happens!

Maybe I am too stuck to my idea of a common emulator "fetch -> decode -> execute" where an addition would be implemented simply as "env->regC = env->regA +env->regB" ... where this C instruction would be compiled offline into host machine binary by host compiler.. so the emulator would be a monolith block of host code just with branches for the different opcodes that would come from the target binary loaded at runtime.. Here instead with QEMU/TCG I understood that at runtime the target binary is translated into host binary (somehow) .. but then.. how can this new host binary be run? Shall the host code at runtime do some sort of (assembly speaking) branch jump to an area of memory with new host binary instructions .. and then jump back to the old process binary code? If so, can you explain me how this happens in those lines of code?
I am just a student.. unluckily at university they just tell you that a cpu follows some sort of "fetch ->decode->execute" flow .. but then you open QEMU.. and wow there is a huge gap for understanding it, and no books where to study it! ;)
Please help me understanding it :)
Thank you very very much in advance!
Stefano B.