Possible addition of precise interpretive CPU emulation cores (MIPS R3000 emulator is included!)
RFC 1/1 Possible addition of precise interpretive CPU emulation cores (MIPS R3000 emulator is included!)
I'd like to discuss a possible addition of software interface for using third-party interpretive CPU emulation cores targeted
at accurate CPU emulation.
MIPS R3000 interpretive core is taken as an example, but the same technique could be used for other popular CPU architectures (e.g. ARM etc).
Please, express your opinions on this subject. Which is the best way to implement slow interpretive CPU emulation for specific aims?
Thank you in advance.
MOTIVATION
----------
* qemu works really fast due to crafty dynamic translation technology! But this great emulation speed is coupled with poor accuracy (e.g. MIPS tcg-based emulation lacks accurate cache emulation).
* adding another CPU model to qemu (even if there is already support for processors with same architecture in qemu) can be a big deal as it needs alteration of the tcg translator (may be this is the reason why we still have no MIPS R3000 support in qemu?).
So software interface for third-party CPU core emulator integration can be useful at least in two cases:
* if you want to use qemu for CPU test creation with additional information on CPU state after executing every instruction;
* if you want to quickly add support for one more "not-very-standard" CPU to qemu (or just integrate existing third-party CPU emulator).
You can find MIPS emulators reviews at http://www.linux-mips.org/wiki/Emulators.
POSSIBLE IMPLEMENTATIONS
------------------------
1. Hack of TCG
Just substitute 'cpu_exec(env)' call in cpus.c:tcg_cpu_exec() et voilà ... the work is done!
The only advantage of this implementation is a very short time for realization.
2. Expanding 'accelerators' to comprise interpreting emulation
qemu has a concept of 'accelerators'. There are four accelerators at the moment: 'tcg', 'xen', 'kvm' and 'qtest'. So we can add new 'precise' accelerator for MIPS CPU interpretive emulation. This implementation is much cleaner then 'hack of tcg' but it not so simple. There are some problems:
* current vl.c code makes adding one more accelerator painful:
you can't just add one more record to 'accel_list' but you
have to add some 'if' operators 'here-and-there'. See for example
this code from cpus.c:
void qemu_init_vcpu(CPUState *cpu)
{
cpu->nr_cores = smp_cores;
cpu->nr_threads = smp_threads;
cpu->stopped = true;
if (kvm_enabled()) {
qemu_kvm_start_vcpu(cpu);
} else if (tcg_enabled()) {
qemu_tcg_init_vcpu(cpu);
} else {
qemu_dummy_start_vcpu(cpu);
}
}
* we have to especially mark relation between CPUs and accel's,
e.g. add additional flags into 'mips_def_t' structure (mark which
processors have support for 'precise' accelerator and which
have only 'tcg' accelerator support):
$ qemu-system-mips \
-cpu 24Kc \
-machine malta,accel=precise
'24Kc' is not supported by 'precise' accel
* current 'struct CPUMIPSState' and related code is tcg-oriented.PROOF-OF-CONCEPT
----------------
This patch demonstrates partial "proof-of-concept": it hacks tcg.
It adds interpretive emulation code of ancient MIPS R3000 CPU to qemu. This R3000 emulation code is based on C++ (sic!) code of VMIPS R3000 emulator by Brian R. Gaeke (http://www.dgate.org/vmips/).
We will emulate well-known MIPS Malta board that uses R3000 as CPU instead of MIPS32. All peripherals are the same (ns16550, pci etc).
For demonstration we will use slightly modified for R3000 Barebox bootloader (http://www.barebox.org).
BUILD INSTRUCTION
-----------------
1. build barebox for R3000
TIP: you can skip this step: just use precompiled barebox image
https://github.com/frantony/qemu/raw/master.mips-precise-r3000.20140108/zbarebox-malta-r3000.bin
$ git clone https://github.com/frantony/barebox -b next.qemu-malta-r3000.20140108 $ cd barebox/ $ export ARCH=mips $ export CROSS_COMPILE=<your MIPS cross-compiler prefix> $ make qemu-malta_defconfig $ make $ cd ..
2. build qemu
$ git clone https://github.com/frantony/qemu -b master.mips-precise-r3000.20140108 $ cd qemu $ ./configure --target-list=mips-softmmu $ make
3. run qemu
$ ./mips-softmmu/qemu-system-mips \
-nodefaults \
-nographic \
-bios ../barebox/arch/mips/pbl/zbarebox.bin \
-serial stdio \
-monitor noneFUTURE WORK
-----------
1. add 'precise' accelerator for MIPS CPU interpretive emulation
Current proof-of-concept R3000 emulator is a dirty hack over tcg-based emulation.
2. make interpretive R3000 model more strict. Use linux kernel for model testing.
current R3000 VMIPS-based emulator is very simple:
* no TLB;
* no FPU (cp1);
* no cache;
* no exception support (and especially bus error);
* no interrupt controller.
--- cpus.c | 4 +- target-mips/Makefile.objs | 1 + target-mips/cpu.h | 204 +++-- target-mips/precise-cpu.c | 1821 +++++++++++++++++++++++++++++++++++++++ target-mips/precise-cpzeroreg.h | 211 +++++ target-mips/precise-excnames.h | 47 + target-mips/translate.c | 4 + 7 files changed, 2205 insertions(+), 87 deletions(-) create mode 100644 target-mips/precise-cpu.c create mode 100644 target-mips/precise-cpzeroreg.h create mode 100644 target-mips/precise-excnames.h
cpus.c +3 -1
@@ -1217,6 +1217,8 @@ int vm_stop_force_state(RunState state) } } +extern int precise_cpu_mips_exec (CPUArchState *env); +static int tcg_cpu_exec (CPUArchState *env) {int ret;
@@ -1252,7 +1254,7 @@ static int tcg_cpu_exec(CPUArchState *env) env->icount_decr.u16.low = decr; env->icount_extra = count; } - ret =cpu_exec (env); + ret =precise_cpu_mips_exec (env);#ifdef CONFIG_PROFILER qemu_time +=profile_getclock () - ti;#endif
target-mips/Makefile.objs +1 -0
@@ -1,3 +1,4 @@ obj-y += translate.o dsp_helper.o op_helper.o lmi_helper.o helper.o cpu.o obj-y += gdbstub.o +obj-y += precise-cpu.o obj-$(CONFIG_SOFTMMU) += machine.o
target-mips/cpu.h +118 -86
269 lines not on this page · raw message
target-mips/precise-cpu.c new file +1821 -0
1,821 lines not on this page · raw message
target-mips/precise-cpzeroreg.h new file +211 -0
211 lines not on this page · raw message
target-mips/precise-excnames.h new file +47 -0
47 lines not on this page · raw message
target-mips/translate.c +4 -0
@@ -15876,6 +15876,8 @@ void mips_tcg_init(void)#include "translate_init.c" +extern void precise_mips_cpu_init (struct CPUMIPSState *env); +MIPSCPU *cpu_mips_init (const char *cpu_model) {MIPSCPU *cpu;
@@ -15895,6 +15897,8 @@ MIPSCPU *cpu_mips_init(const char *cpu_model)fpu_init (env, def);mvp_init (env, def); +precise_mips_cpu_init (env); +object_property_set_bool (OBJECT (cpu), true,"realized" ,NULL );return cpu;
Signature · 1 line
1.8.5.2