hvf: use TCG emulation to handle data aborts
RFC PATCH 4/4 hw/arm/virt: enable VGA
In reply to Joelle van Dyne--- hw/arm/Kconfig | 1 + 1 file changed, 1 insertion(+)
hw/arm/Kconfig +1 -0
@@ -11,6 +11,7 @@ config ARM_VIRT imply TPM_TIS_I2C imply NVDIMM imply IOMMUFD + imply VIRTIO_VGA select ARM_GIC select ACPI select ARM_SMMUV3
Signature · 1 line
2.41.0
cover hvf: use TCG emulation to handle data aborts
When the VM exits with an data abort, we check the ISV field in the ESR and when ISV=1, that means the processor has filled the remaining fields with information needed to determine the access that caused the abort: address, access width, and the register operand. However, only a limited set of instructions which can cause a data abort is nice enough for the processor to decode this way. Many instructions such as LDP/STP and SIMD can cause an data abort with ISV=0 and for that the hypervisor needs to manually decode the instruction, find the operands, and emulate the access.
QEMU already ships with the ability to do this: TCG. However, TCG currently operates as a stand-alone accelerator. This patch set enables HVF to call into TCG when needed in order to perform a memory access that caused the abort.
One thing this enables is the ability to use virtio-vga with Windows for ARM64. Currently, graphics support for Windows is flakey because you must first boot with ramfb to get to the desktop where you can then install the virtio-gpu drivers and then start up with virtio-gpu. Even then, there is a known issue where Windows mistakingly thinks there are two monitors connected because the boot display does not share a framebuffer with the GPU. This results in sometimes a black screen when updating Windows.
Another issue is that the TPM driver uses LDP/STP to access the command buffer and so the QEMU device which maps registers as MMIO will not work.
There are a couple major issues with the patch as it currently stands. First of all, it is very slow. Because we do not track writes to code pages, to be safe we flush TLBs and TBs every time we switch to emulation mode. We also need to synchronize the register states between HVF and QEMU each time we enter and exit emulation mode. Since we enter/exit emulation mode for every instruction that causes the data abort, in the case of the VGA buffer being cleared in a loop, this means we need to enter-exit emulation mode to execute a single instruction for every pixel. Second, we don't support plugins at all. Lastly, some of the CPU state used by TCG is not properly synchronized with HVF and so subtle issues can occur. We may want to constrain the emulator to only run with a known allowlist of instructions we wish to handle in a data abort.
I think these issues can be worked around but I want to know if people think this approach is worth doing or if instead we should pursue alternatives such as a more basic instruction decoder which only supports a subset of instructions which are interesting for memory accesses.
Joelle van Dyne (4):
cpu-exec: support single-step without debug
cpu-target: support emulation from non-TCG accels
hvf: arm: emulate instruction when ISV=0 hw/arm/virt: enable VGA
include/exec/cpu-common.h | 1 + include/hw/core/cpu.h | 11 +++++ include/system/hvf_int.h | 2 +- target/arm/hvf_arm.h | 5 ++ target/arm/internals.h | 3 +- accel/hvf/hvf-accel-ops.c | 2 +- accel/tcg/cpu-exec.c | 35 +++++++++---- accel/tcg/plugin-gen.c | 4 ++ accel/tcg/tb-maint.c | 2 +- accel/tcg/tcg-accel-ops.c | 3 +- cpu-target.c | 20 +++++++- plugins/core.c | 12 +++++ system/physmem.c | 7 +-- target/arm/hvf/hvf.c | 100 ++++++++++++++++++++++++++++++++++++-- target/i386/hvf/hvf.c | 2 +- hw/arm/Kconfig | 1 + 16 files changed, 186 insertions(+), 24 deletions(-)
Signature · 1 line
2.41.0
RFC PATCH 2/4 cpu-target: support emulation from non-TCG accels
We create a toggle to allow TCG emulation to be used dynamically when running other accelerators. Tracking dirty code can be expensive so we need to flush the TLBs and TBs every time we toggle emulation mode. Plugin support is currently disabled when running in this mode.
--- include/hw/core/cpu.h | 10 ++++++++++ accel/tcg/plugin-gen.c | 4 ++++ accel/tcg/tb-maint.c | 2 +- accel/tcg/tcg-accel-ops.c | 3 ++- cpu-target.c | 13 +++++++++++++ plugins/core.c | 12 ++++++++++++ system/physmem.c | 5 +++-- 7 files changed, 45 insertions(+), 4 deletions(-)
include/hw/core/cpu.h +10 -0
@@ -569,6 +569,9 @@ struct CPUState {/* track IOMMUs whose translations we've cached in the TCG TLB */ GArray *iommu_notifiers; +/* doing emulation when not in TCG backend */ +bool emulation_enabled; +/* * MUST BE LAST in order to minimize the displacement to CPUArchState. */
@@ -1083,6 +1086,13 @@ void qemu_init_vcpu(CPUState *cpu);*/ void cpu_single_step (CPUState *cpu,int enabled); +/** +* cpu_emulate: +* @cpu: CPU to set to emulation mode +* @enabled: enable emulation mode +*/ +void cpu_emulate (CPUState *cpu,bool enabled); +/* Breakpoint/watchpoint flags */ #define BP_MEM_READ 0x01#define BP_MEM_WRITE 0x02
accel/tcg/plugin-gen.c +4 -0
@@ -388,6 +388,10 @@ bool plugin_gen_tb_start(CPUState *cpu, const DisasContextBase *db) {struct qemu_plugin_tb *ptb; +if (cpu->emulation_enabled) { +return false; + } +if (!test_bit (QEMU_PLUGIN_EV_VCPU_TB_TRANS , cpu->plugin_state->event_mask)) {return false;
accel/tcg/tb-maint.c +1 -1
@@ -791,7 +791,7 @@ done:void tb_flush (CPUState *cpu) { -if (tcg_enabled ()) { +if (tcg_enabled () ||unlikely (cpu->emulation_enabled)) {unsigned tb_flush_count =qatomic_read (&tb_ctx.tb_flush_count);if (cpu_in_serial_context (cpu)) {
accel/tcg/tcg-accel-ops.c +2 -1
@@ -32,6 +32,7 @@#include "qemu/main-loop.h" #include "qemu/guest-random.h" #include "qemu/timer.h" +#include "exec/cpu-common.h" #include "exec/exec-all.h" #include "exec/hwaddr.h" #include "exec/tb-flush.h"
@@ -74,7 +75,7 @@ void tcg_cpu_destroy(CPUState *cpu)int tcg_cpu_exec (CPUState *cpu) {int ret; -assert (tcg_enabled ()); +assert (tcg_enabled () || cpu->emulation_enabled);cpu_exec_start (cpu); ret =cpu_exec (cpu);cpu_exec_end (cpu);
cpu-target.c +13 -0
@@ -339,6 +339,19 @@ void cpu_single_step(CPUState *cpu, int enabled) } } +void cpu_emulate (CPUState *cpu,bool enabled) +{ +if (cpu->emulation_enabled != enabled) { + cpu->emulation_enabled = enabled; + +if (enabled) { +/* FIXME: track dirty code to improve performance */ +tb_flush (cpu); +tlb_flush (cpu); + } + } +} +void cpu_abort (CPUState *cpu,const char *fmt, ...) {va_list ap;
plugins/core.c +12 -0
@@ -55,6 +55,10 @@ struct qemu_plugin_ctx *plugin_id_to_ctx_locked(qemu_plugin_id_t id)static void plugin_cpu_update__async (CPUState *cpu,run_on_cpu_data data) { +if (cpu->emulation_enabled) { +return ; + } +bitmap_copy (cpu->plugin_state->event_mask, &data.host_ulong,QEMU_PLUGIN_EV_MAX );tcg_flush_jmp_cache (cpu);
@@ -499,6 +503,10 @@ qemu_plugin_vcpu_syscall(CPUState *cpu, int64_t num, uint64_t a1, uint64_t a2,struct qemu_plugin_cb *cb, *next;enum qemu_plugin_event ev =QEMU_PLUGIN_EV_VCPU_SYSCALL ; +if (cpu->emulation_enabled) { +return ; + } +if (!test_bit (ev, cpu->plugin_state->event_mask)) {return ; }
@@ -521,6 +529,10 @@ void qemu_plugin_vcpu_syscall_ret(CPUState *cpu, int64_t num, int64_t ret)struct qemu_plugin_cb *cb, *next;enum qemu_plugin_event ev =QEMU_PLUGIN_EV_VCPU_SYSCALL_RET ; +if (cpu->emulation_enabled) { +return ; + } +if (!test_bit (ev, cpu->plugin_state->event_mask)) {return ; }
system/physmem.c +3 -2
@@ -2696,7 +2696,9 @@ static void tcg_commit_cpu(CPUState *cpu, run_on_cpu_data data)CPUAddressSpace *cpuas = data.host_ptr; cpuas->memory_dispatch =address_space_to_dispatch (cpuas->as); -tlb_flush (cpu); +if (tcg_enabled () || cpu->emulation_enabled) { +tlb_flush (cpu); + } }static void tcg_commit (MemoryListener *listener)
@@ -2704,7 +2706,6 @@ static void tcg_commit(MemoryListener *listener)CPUAddressSpace *cpuas;CPUState *cpu; -assert (tcg_enabled ());/* since each CPU stores ram addresses in its TLB cache, we must reset the modified entries */ cpuas =container_of (listener, CPUAddressSpace, tcg_as_listener);
Signature · 1 line
2.41.0
RFC PATCH 3/4 hvf: arm: emulate instruction when ISV=0
In reply to Joelle van DyneOn a data abort, the processor will try to decode the faulting instruction so the hypervisor can emulate the read/write. However, it is not always able to do this and ISV=0 whenever the instruction is not decoded. This is the case for example if the faulting instruction is SIMD or a LDP/STP.
When this happens, we can use TCG to emulate the faulting instruction. This is needed if the processor uses one of these instructions to access memory that is currently unmapped such as with VGA VRAM.
--- include/system/hvf_int.h | 2 +- target/arm/hvf_arm.h | 5 ++ accel/hvf/hvf-accel-ops.c | 2 +- system/physmem.c | 2 +- target/arm/hvf/hvf.c | 100 ++++++++++++++++++++++++++++++++++++-- target/i386/hvf/hvf.c | 2 +- 6 files changed, 106 insertions(+), 7 deletions(-)
include/system/hvf_int.h +1 -1
@@ -64,7 +64,7 @@ void assert_hvf_ok_impl(hv_return_t ret, const char *file, unsigned int line, const char *exp);#define assert_hvf_ok (EX ) assert_hvf_ok_impl((EX), __FILE__, __LINE__, #EX)const char *hvf_return_string (hv_return_t ret); -int hvf_arch_init (void ); +int hvf_arch_init (MachineState *ms);hv_return_t hvf_arch_vm_create (MachineState *ms,uint32_t pa_range);int hvf_arch_init_vcpu (CPUState *cpu);void hvf_arch_vcpu_destroy (CPUState *cpu);
target/arm/hvf_arm.h +5 -0
@@ -41,4 +41,9 @@ static inline uint32_t hvf_arm_get_max_ipa_bit_size(void)#endif +/** +* hvf_arm_init_emulator() - initialize TCG emulator +*/ +void hvf_arm_init_emulator (int splitwx,unsigned max_cpus); +#endif
accel/hvf/hvf-accel-ops.c +1 -1
@@ -346,7 +346,7 @@ static int hvf_accel_init(MachineState *ms) hvf_state = s;memory_listener_register (&hvf_memory_listener, &address_space_memory); -return hvf_arch_init (); +return hvf_arch_init (ms); }static inline int hvf_gdbstub_sstep_flags (void )
system/physmem.c +1 -1
@@ -771,7 +771,7 @@ void cpu_address_space_init(CPUState *cpu, int asidx, newas = &cpu->cpu_ases[asidx]; newas->cpu = cpu; newas->as = as; -if (tcg_enabled ()) { +if (tcg_enabled () ||hvf_enabled ()) { newas->tcg_as_listener.log_global_after_sync = tcg_log_global_after_sync; newas->tcg_as_listener.commit = tcg_commit; newas->tcg_as_listener.name ="tcg" ;
target/arm/hvf/hvf.c +97 -3
154 lines not on this page · raw message
target/i386/hvf/hvf.c +1 -1
@@ -218,7 +218,7 @@ void hvf_kick_vcpu_thread(CPUState *cpu)hv_vcpu_interrupt (&cpu->accel->fd,1 ); } -int hvf_arch_init (void ) +int hvf_arch_init (MachineState *ms) {return 0 ; }
Signature · 1 line
2.41.0
RFC PATCH 1/4 cpu-exec: support single-step without debug
In reply to Joelle van DyneCurrently, single-stepping is tied to GDB debugging. This means that when EXCP_DEBUG is returned, a debug exception is triggered in many cases. We define a new EXCP_SINGLESTEP to differentiate the case where we want a single step to not be tied to a debug exception. We also define a new flag for cpu->singlestep_enabled called SSTEP_NODEBUG which is set when we want to use single-step for purposes other than debugging.
--- include/exec/cpu-common.h | 1 + include/hw/core/cpu.h | 1 + target/arm/internals.h | 3 ++- accel/tcg/cpu-exec.c | 35 +++++++++++++++++++++++++---------- cpu-target.c | 7 +++++-- 5 files changed, 34 insertions(+), 13 deletions(-)
include/exec/cpu-common.h +1 -0
@@ -22,6 +22,7 @@#define EXCP_HALTED 0x10003/* cpu is halted (waiting for external event) */ #define EXCP_YIELD 0x10004/* cpu wants to yield timeslice to another */ #define EXCP_ATOMIC 0x10005/* stop-the-world and emulate atomic */ +#define EXCP_SINGLESTEP 0x10006/* singlestep without debugging */ void cpu_exec_init_all (void );void cpu_exec_step_atomic (CPUState *cpu);
include/hw/core/cpu.h +1 -0
@@ -1072,6 +1072,7 @@ void qemu_init_vcpu(CPUState *cpu);#define SSTEP_ENABLE 0x1/* Enable simulated HW single stepping */ #define SSTEP_NOIRQ 0x2/* Do not use IRQ while single stepping */ #define SSTEP_NOTIMER 0x4/* Do not Timers while single stepping */ +#define SSTEP_NODEBUG 0x8/* Single-stepping is not for debugging */ /** * cpu_single_step:
target/arm/internals.h +2 -1
@@ -57,7 +57,8 @@ static inline bool excp_is_internal(int excp) || excp == EXCP_HALTED || excp ==EXCP_EXCEPTION_EXIT || excp ==EXCP_KERNEL_TRAP - || excp ==EXCP_SEMIHOST ; + || excp ==EXCP_SEMIHOST + || excp ==EXCP_SINGLESTEP ; }/*
accel/tcg/cpu-exec.c +25 -10
@@ -349,7 +349,7 @@ static bool check_for_breakpoints_slow(CPUState *cpu, vaddr pc,* so that one could (gdb) singlestep into the guest kernel's * architectural breakpoint handler. */ -if (cpu->singlestep_enabled) { +if (cpu->singlestep_enabled && !(cpu->singlestep_enabled &SSTEP_NODEBUG )) {return false; }
@@ -529,7 +529,11 @@ cpu_tb_exec(CPUState *cpu, TranslationBlock *itb, int *tb_exit)* is handled in cpu_handle_exception. */ if (unlikely (cpu->singlestep_enabled) && cpu->exception_index ==-1 ) { - cpu->exception_index =EXCP_DEBUG ; +if (!(cpu->singlestep_enabled &SSTEP_NODEBUG )) { + cpu->exception_index =EXCP_DEBUG ; + }else { + cpu->exception_index =EXCP_SINGLESTEP ; + }cpu_loop_exit (cpu); }
@@ -781,13 +785,20 @@ static inline bool cpu_handle_exception(CPUState *cpu, int *ret) cpu->exception_index =-1 ;if (unlikely (cpu->singlestep_enabled)) { -/* -* After processing the exception, ensure an EXCP_DEBUG is -* raised when single-stepping so that GDB doesn't miss the -* next instruction. -*/ - *ret =EXCP_DEBUG ; -cpu_handle_debug_exception (cpu); +if (!(cpu->singlestep_enabled &SSTEP_NODEBUG )) { +/* +* After processing the exception, ensure an EXCP_DEBUG is +* raised when single-stepping so that GDB doesn't miss the +* next instruction. +*/ + *ret =EXCP_DEBUG ; +cpu_handle_debug_exception (cpu); + }else { +/* +* In case of non-debug single step, just return +*/ + *ret =EXCP_SINGLESTEP ; + }return true; } }else if (!replay_has_interrupt ()) {
@@ -892,7 +903,11 @@ static inline bool cpu_handle_interrupt(CPUState *cpu,* next instruction. */ if (unlikely (cpu->singlestep_enabled)) { - cpu->exception_index =EXCP_DEBUG ; +if (!(cpu->singlestep_enabled &SSTEP_NODEBUG )) { + cpu->exception_index =EXCP_DEBUG ; + }else { + cpu->exception_index =EXCP_SINGLESTEP ; + }bql_unlock ();return true; }
cpu-target.c +5 -2
@@ -322,9 +322,12 @@ void list_cpus(void)CPU loop after each instruction */ void cpu_single_step (CPUState *cpu,int enabled) { -if (cpu->singlestep_enabled != enabled) { - cpu->singlestep_enabled = enabled; +int previous = cpu->singlestep_enabled; +bool prev_debug_en = previous && !(previous &SSTEP_NODEBUG ); +bool cur_debug_en = enabled && !(enabled &SSTEP_NODEBUG ); + cpu->singlestep_enabled = enabled; +if (prev_debug_en != cur_debug_en) {#if !defined(CONFIG_USER_ONLY )const AccelOpsClass *ops =cpus_get_accel ();if (ops->update_guest_debug) {
Signature · 1 line
2.41.0
Quoted 10 lines · Joelle van Dyne
When the VM exits with an data abort, we check the ISV field in the ESR and when ISV=1, that means the processor has filled the remaining fields with information needed to determine the access that caused the abort: address, access width, and the register operand. However, only a limited set of instructions which can cause a data abort is nice enough for the processor to decode this way. Many instructions such as LDP/STP and SIMD can cause an data abort with ISV=0 and for that the hypervisor needs to manually decode the instruction, find the operands, and emulate the access.
Quoted 3 lines
QEMU already ships with the ability to do this: TCG. However, TCG currently operates as a stand-alone accelerator. This patch set enables HVF to call into TCG when needed in order to perform a memory access that caused the abort.
So one problem with this is that it immediately puts all of TCG onto the security boundary with the VM. We don't claim any kind of security or can't-escape guarantees for TCG, and it's a lot of code, some of which is old and some of which wasn't written with security as a top-of-mind concern.
Our approach to these instructions with KVM on Arm is to say "don't do those in the guest to MMIO regions". Most sensible guest code doesn't do weird instruction forms for device accesses, and the performance is going to be bad anyway if you need to fully emulate them. (This includes in the past that Windows got fixed to not do this kind of insn to a device in at least one case.)
Quoted 7 lines
One thing this enables is the ability to use virtio-vga with Windows for ARM64. Currently, graphics support for Windows is flakey because you must first boot with ramfb to get to the desktop where you can then install the virtio-gpu drivers and then start up with virtio-gpu. Even then, there is a known issue where Windows mistakingly thinks there are two monitors connected because the boot display does not share a framebuffer with the GPU. This results in sometimes a black screen when updating Windows.
It's not really a good idea to use virtio-vga in an Arm VM, because it requires FEAT_S2FWB in the host CPU to make it work properly, and not every CPU has that, at least in the KVM world. So you need to use virtio-gpu anyhow.
thanks
-- PMM