Someone mentioned valgrind.  It happens that I was looking at the source
code of valgrind yesterday, so I'll share the little bit that I was able
to pick up.

The purpose of Valgrind is memory debugging of user level code.  The
implementation that the author chose was a 100% simulated CPU which keeps
track of which bits in memory are valid and which aren't.  When the
simulated CPU makes system calls, valgrind routes it to the real kernel
though.

For each basic block of code that it encounters, it first translates the
x86 instructions into RISC-like micro-ops.  Then it adds the
memory-checking code, using a few special micro-ops that set and check the
valid bits, and does some optimization.  Finally, the micro-ops are mapped
back into native x86 instructions and executed.  All basic blocks that
execute get translated; the ones that are used frequently get stored so
that translation isn't needed the next time, using a least-recently-used
scheme of some kind.  It's quite impressive, IMHO.

The micro-ops technique is basically used so that it can insert the
memory-checking code into a binary at run time.  This expands the code
into multiple instructions per simulated x86 instruction, whereas plex86
should be able to run one x86 instruction per simulated instruction (in
user code at least).

Valgrind's strategy is good for what it tries to do.  It sort of
recompiles the binary on the fly, with additional instructions inserted to
check for illegal memory use.

Micro-ops might be useful to Bochs, which runs on many different
platforms.  In theory, you could translate basic blocks into
platform-independent micro-ops, then use an x86/Sparc/PowerPC/etc backend
to compile micro-ops into native code for the target platform.  (It's not
clear to me if it would be worth the trouble, or not.)

But for plex86 I think it's clear that these transformations would hurt
performance rather than help it.  You're already working with the best
Intel CPU simulator you will find--a real Intel CPU.

Regards,
Bryce

P.S. Please forgive me for writing something on topic. :)


Reply via email to