Skip to contentSkip to conversation

How does env_to_regs and regs_to_env works?

Qemu-devel 曹莹 2 participants 2 messages May 31, 2010 – Jun 2, 2010

lore
Share & tools
lore

曹莹

author
#0 lore raw

Now, I’m working with Qemu source study, but I have some difficulties. Could someday give me some help or advice.

At the beginning of cpu_exec, there is a function call env_to_regs, but I cannot understand the behavior of env_to_regs. In my understanding, env_to_regs stores environment context pointer(pointer that points to X86CPUState) and other critical Qemu defined variable to real host register, that is to say function env_to_regs accomplishes the task register mapping for translating optimization. That is my understanding, but I really understand the codes in env_to_regs and regs_to_env. Codes in env_to_regs are like this:

#ifdef reg_EAX

EAX = env->regs[R_EAX];

…………

I cannot find where the micro reg_EAX is defined, actually, I insert one line: printf(“reg_EAX is defined!\n”) after #ifdef reg_EAX into Qemu source, compile it and run Qemu, there is no output information, so I think, env_to_regs is actually a void function (For other micro define, situations are the same). But if this is true, how did Qemu’s register mapping accomplished?

If I suppose that reg_EAX has already defined somewhere in Qemu source, but how does EAX = env->regs[R_EAX] works? EAX is also a micro definition like this #define EAX (env->regs[R_EAX]). Then the code becomes like this: env->regs[R_EAX]= env->regs[R_EAX]. Left side and right side of “=” is the same . So how does regs_to_env and regs_to_env work? How to understand the functionality of the two functions, Could anyone give me some advice. I will own my thankfulness for all of you. Thank you very much.

#1 lore raw
Quoted 5 lines · 曹莹

Codes in env_to_regs are like this:

#ifdef reg_EAX

EAX = env->regs[R_EAX];

[...]

Quoted 1 line

I cannot find where the micro reg_EAX is defined

[...]

Quoted 2 lines

I think, env_to_regs is actually a void function
But if this is true, how did Qemu’s register mapping accomplished?

For i386 and x86_64, only AREG[0123] were ever defined. The introduction of TCG on 2008-02-01 broke support for all hosts except i386 and x86_64.

Commit edea5f0 (2008-05-10) then removed the definitions of reg_EAX, etc. for target-i386.

Commit 6635f6f (2009-03-08) then removed the definitions of AREG3 to AREG11 for any hosts that defined them.

Commit 49a945a (2010-01-19) then removed env_to_regs() and regs_to_env().

So in other words, env_to_regs() and regs_to_env() haven't done anything at all for quite a long time.

TCG allocates host registers dynamically (i.e. at translation time), so host registers are no longer allocated statically (as they were with dyngen under combinations of hosts and targets with these definitions). TCG_AREG0 is still needed for the 'env' pointer itself, though.

It is TCG's job to make sure that the generated code saves values in the CPUState struct where appropriate, i.e. at the end of any 'basic block', or where host registers might get clobbered, or where exceptions might need to be raised. This is handled by calls to the save_globals() function in tcg.c.

Cheers,

Signature · 1 line
Stuart Brady