Factor out HVF's instruction emulator
v2 cover Factor out HVF's instruction emulator
Hi,
Microsoft's Linux Systems Group developed a Linux driver for the Microsoft Hypervisor (MSHV for short). The driver is being upstreamed. The first supported VMM is Cloud Hypervisor. We want to add QEMU as the second supported VMM.
The plan is to write an mshv accelerator in QEMU. The accelerator is still in the works.
MSHV doesn't emulate instructions. VMMs are supposed to bring their own instruction emulator. The path we've chosen is to reuse what's already in QEMU. The instruction emulator in HVF looks good for what we need.
This patch series makes the instruction emulator in HVF a common component for the i386 target. It removes HVF specific code by using a set of hooks. The new incoming MSHV accelerator will implement the hooks, and where necessary, enhance the emulator and / or add new hooks.
The patches have been lightly tested by running a Linux VM on an Intel-based Mac.
Thanks,
Wei.
Changes in v2:
1. Address comments from Paolo on variable and directory names.
2. Rebase and drop the already applied patches.
3. Add a new entry in MAINTAINERS.
Wei Liu (14):
target/i386/hvf: introduce x86_emul_ops
target/i386/hvf: remove HVF specific calls from x86_decode.c target/i386/hvf: provide and use handle_io in emul_ops target/i386: rename hvf_mmio_buf to emu_mmio_buf target/i386/hvf: use emul_ops->read_mem in x86_emu.c taret/i386/hvf: provide and use write_mem in emul_ops target/i386/hvf: provide and use simulate_{wrmsr,rdmsr} in emul_ops target/i386: rename lazy flags field and its type target/i386/hvf: drop unused headers
target/i386/hvf: rename some include guards
target/i386: add a directory for x86 instruction emulator target/i386/emulate: add a panic.h
target/i386: move x86 instruction emulator out of hvf
MAINTAINERS: add an entry for the x86 instruction emulator
MAINTAINERS | 8 +++
target/i386/cpu.h | 8 +--
target/i386/emulate/meson.build | 5 ++
target/i386/emulate/panic.h | 45 ++++++++++++++++
target/i386/{hvf => emulate}/x86.h | 4 +-
target/i386/{hvf => emulate}/x86_decode.c | 22 +++-----
target/i386/{hvf => emulate}/x86_decode.h | 4 +-
target/i386/{hvf => emulate}/x86_emu.c | 62 +++++++++++------------
target/i386/{hvf => emulate}/x86_emu.h | 15 +++++-
target/i386/{hvf => emulate}/x86_flags.c | 56 ++++++++++----------
target/i386/{hvf => emulate}/x86_flags.h | 6 +--
target/i386/hvf/hvf-i386.h | 4 +-
target/i386/hvf/hvf.c | 57 +++++++++++++++------
target/i386/hvf/meson.build | 3 --
target/i386/hvf/vmx.h | 2 +-
target/i386/hvf/x86.c | 4 +-
target/i386/hvf/x86_cpuid.c | 2 +-
target/i386/hvf/x86_descr.h | 2 +-
target/i386/hvf/x86_mmu.c | 2 +-
target/i386/hvf/x86_task.c | 6 +--
target/i386/hvf/x86hvf.c | 2 +-
target/i386/meson.build | 1 +
22 files changed, 203 insertions(+), 117 deletions(-)
create mode 100644 target/i386/emulate/meson.build
create mode 100644 target/i386/emulate/panic.h
rename target/i386/{hvf => emulate}/x86.h (99%)
rename target/i386/{hvf => emulate}/x86_decode.c (99%)
rename target/i386/{hvf => emulate}/x86_decode.h (99%)
rename target/i386/{hvf => emulate}/x86_emu.c (95%)
rename target/i386/{hvf => emulate}/x86_emu.h (75%)
rename target/i386/{hvf => emulate}/x86_flags.c (83%)
rename target/i386/{hvf => emulate}/x86_flags.h (97%)Signature · 1 line
2.47.2
PATCH v2 1/14 target/i386/hvf: introduce x86_emul_ops
This will be used to remove HVF specific code from the instruction emulator.
For now we only introduce two hooks for x86_decode.c. More hooks will be added when the code is refactored.
The emulator initialization function now takes in a pointer to the ops structure.
--- target/i386/hvf/hvf.c | 20 +++++++++++++++++++- target/i386/hvf/x86_emu.c | 5 ++++- target/i386/hvf/x86_emu.h | 10 +++++++++- 3 files changed, 32 insertions(+), 3 deletions(-)
target/i386/hvf/hvf.c +19 -1
@@ -229,6 +229,24 @@ hv_return_t hvf_arch_vm_create(MachineState *ms, uint32_t pa_range)return hv_vm_create (HV_VM_DEFAULT ); } +static void hvf_read_segment_descriptor (CPUState *s,struct x86_segment_descriptor *desc, +X86Seg seg) +{ +struct vmx_segment vmx_segment; +vmx_read_segment_descriptor (s, &vmx_segment, seg); +vmx_segment_to_x86_descriptor (s, &vmx_segment, desc); +} + +static void hvf_read_mem (CPUState *cpu,void *data,target_ulong gva,int bytes) +{ +vmx_read_mem (cpu, data, gva, bytes); +} + +static const struct x86_emul_ops hvf_x86_emul_ops = { + .read_mem = hvf_read_mem, + .read_segment_descriptor = hvf_read_segment_descriptor, +}; +int hvf_arch_init_vcpu (CPUState *cpu) {X86CPU *x86cpu =X86_CPU (cpu);
@@ -237,7 +255,7 @@ int hvf_arch_init_vcpu(CPUState *cpu)int r;uint64_t reqCap; -init_emu (); +init_emu (&hvf_x86_emul_ops);init_decoder ();if (hvf_state->hvf_caps ==NULL ) {
target/i386/hvf/x86_emu.c +4 -1
@@ -1231,6 +1231,8 @@ static struct cmd_handler {static struct cmd_handler _cmd_handler[X86_DECODE_CMD_LAST ]; +const struct x86_emul_ops *emul_ops; +static void init_cmd_handler (void ) {int i;
@@ -1253,7 +1255,8 @@ bool exec_instruction(CPUX86State *env, struct x86_decode *ins)return true; } -void init_emu (void ) +void init_emu (const struct x86_emul_ops *o) { + emul_ops = o;init_cmd_handler (); }
target/i386/hvf/x86_emu.h +9 -1
@@ -23,7 +23,15 @@#include "x86_decode.h" #include "cpu.h" -void init_emu (void ); +struct x86_emul_ops { +void (*read_mem)(CPUState *cpu,void *data,target_ulong addr,int bytes); +void (*read_segment_descriptor)(CPUState *cpu,struct x86_segment_descriptor *desc, +enum X86Seg seg); +}; + +extern const struct x86_emul_ops *emul_ops; + +void init_emu (const struct x86_emul_ops *ops);bool exec_instruction (CPUX86State *env,struct x86_decode *ins);void x86_emul_raise_exception (CPUX86State *env,int exception_index,int error_code);
Signature · 1 line
2.47.2
PATCH v2 2/14 target/i386/hvf: remove HVF specific calls from x86_decode.c
In reply to Wei LiuUse the newly defined emul_ops. This allows the module to be reused by other accelerator in the future.
No functional change intended.
--- target/i386/hvf/x86_decode.c | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-)
target/i386/hvf/x86_decode.c +6 -13
@@ -21,6 +21,7 @@#include "panic.h" #include "x86_decode.h" #include "vmx.h" +#include "x86_emu.h" #include "x86_mmu.h" #include "x86_descr.h"
@@ -74,7 +75,7 @@ static inline uint64_t decode_bytes(CPUX86State *env, struct x86_decode *decode,break ; }target_ulong va =linear_rip (env_cpu (env), env->eip) + decode->len; -vmx_read_mem (env_cpu (env), &val, va, size); + emul_ops->read_mem (env_cpu (env), &val, va, size); decode->len += size;return val;
@@ -1893,16 +1894,6 @@ static void decode_prefix(CPUX86State *env, struct x86_decode *decode) } } -static struct x86_segment_descriptor get_cs_descriptor (CPUState *s) -{ -struct vmx_segment vmx_cs; -x86_segment_descriptor cs; -vmx_read_segment_descriptor (s, &vmx_cs,R_CS ); -vmx_segment_to_x86_descriptor (s, &vmx_cs, &cs); - -return cs; -} -void set_addressing_size (CPUX86State *env,struct x86_decode *decode) { decode->addressing_size =-1 ;
@@ -1914,7 +1905,8 @@ void set_addressing_size(CPUX86State *env, struct x86_decode *decode) } }else if (!x86_is_long_mode (env_cpu (env))) {/* protected */ -x86_segment_descriptor cs =get_cs_descriptor (env_cpu (env)); +x86_segment_descriptor cs; + emul_ops->read_segment_descriptor (env_cpu (env), &cs,R_CS );/* check db */ if (cs.db) {if (decode->addr_size_override) {
@@ -1950,7 +1942,8 @@ void set_operand_size(CPUX86State *env, struct x86_decode *decode) } }else if (!x86_is_long_mode (env_cpu (env))) {/* protected */ -x86_segment_descriptor cs =get_cs_descriptor (env_cpu (env)); +x86_segment_descriptor cs; + emul_ops->read_segment_descriptor (env_cpu (env), &cs,R_CS );/* check db */ if (cs.db) {if (decode->op_size_override) {
Signature · 1 line
2.47.2
PATCH v2 3/14 target/i386/hvf: provide and use handle_io in emul_ops
In reply to Wei LiuThis drops the calls to hvf_handle_io from x86_emu.c.
--- target/i386/hvf/hvf.c | 1 + target/i386/hvf/x86_emu.c | 29 +++++++++++++++-------------- target/i386/hvf/x86_emu.h | 2 ++ 3 files changed, 18 insertions(+), 14 deletions(-)
target/i386/hvf/hvf.c +1 -0
@@ -245,6 +245,7 @@ static void hvf_read_mem(CPUState *cpu, void *data, target_ulong gva, int bytes)static const struct x86_emul_ops hvf_x86_emul_ops = { .read_mem = hvf_read_mem, .read_segment_descriptor = hvf_read_segment_descriptor, + .handle_io = hvf_handle_io, };int hvf_arch_init_vcpu (CPUState *cpu)
target/i386/hvf/x86_emu.c +15 -14
@@ -396,18 +396,18 @@ static void exec_out(CPUX86State *env, struct x86_decode *decode) {switch (decode->opcode[0 ]) {case 0xe6 : -hvf_handle_io (env_cpu (env), decode->op[0 ].val, &AL (env),1 ,1 ,1 ); + emul_ops->handle_io (env_cpu (env), decode->op[0 ].val, &AL (env),1 ,1 ,1 );break ;case 0xe7 : -hvf_handle_io (env_cpu (env), decode->op[0 ].val, &RAX (env),1 , - decode->operand_size,1 ); + emul_ops->handle_io (env_cpu (env), decode->op[0 ].val, &RAX (env),1 , + decode->operand_size,1 );break ;case 0xee : -hvf_handle_io (env_cpu (env),DX (env), &AL (env),1 ,1 ,1 ); + emul_ops->handle_io (env_cpu (env),DX (env), &AL (env),1 ,1 ,1 );break ;case 0xef : -hvf_handle_io (env_cpu (env),DX (env), &RAX (env),1 , - decode->operand_size,1 ); + emul_ops->handle_io (env_cpu (env),DX (env), &RAX (env),1 , + decode->operand_size,1 );break ;default :VM_PANIC ("Bad out opcode\n" );
@@ -421,10 +421,10 @@ static void exec_in(CPUX86State *env, struct x86_decode *decode)target_ulong val =0 ;switch (decode->opcode[0 ]) {case 0xe4 : -hvf_handle_io (env_cpu (env), decode->op[0 ].val, &AL (env),0 ,1 ,1 ); + emul_ops->handle_io (env_cpu (env), decode->op[0 ].val, &AL (env),0 ,1 ,1 );break ;case 0xe5 : -hvf_handle_io (env_cpu (env), decode->op[0 ].val, &val,0 , + emul_ops->handle_io (env_cpu (env), decode->op[0 ].val, &val,0 , decode->operand_size,1 );if (decode->operand_size ==2 ) {AX (env) = val;
@@ -433,10 +433,11 @@ static void exec_in(CPUX86State *env, struct x86_decode *decode) }break ;case 0xec : -hvf_handle_io (env_cpu (env),DX (env), &AL (env),0 ,1 ,1 ); + emul_ops->handle_io (env_cpu (env),DX (env), &AL (env),0 ,1 ,1 );break ;case 0xed : -hvf_handle_io (env_cpu (env),DX (env), &val,0 , decode->operand_size,1 ); + emul_ops->handle_io (env_cpu (env),DX (env), &val,0 , + decode->operand_size,1 );if (decode->operand_size ==2 ) {AX (env) = val; }else {
@@ -486,8 +487,8 @@ static void exec_ins_single(CPUX86State *env, struct x86_decode *decode)target_ulong addr =linear_addr_size (env_cpu (env),RDI (env), decode->addressing_size,R_ES ); -hvf_handle_io (env_cpu (env),DX (env), env->hvf_mmio_buf,0 , - decode->operand_size,1 ); + emul_ops->handle_io (env_cpu (env),DX (env), env->hvf_mmio_buf,0 , + decode->operand_size,1 );vmx_write_mem (env_cpu (env), addr, env->hvf_mmio_buf, decode->operand_size);
@@ -511,8 +512,8 @@ static void exec_outs_single(CPUX86State *env, struct x86_decode *decode)vmx_read_mem (env_cpu (env), env->hvf_mmio_buf, addr, decode->operand_size); -hvf_handle_io (env_cpu (env),DX (env), env->hvf_mmio_buf,1 , - decode->operand_size,1 ); + emul_ops->handle_io (env_cpu (env),DX (env), env->hvf_mmio_buf,1 , + decode->operand_size,1 );string_increment_reg (env,R_ESI , decode); }
target/i386/hvf/x86_emu.h +2 -0
@@ -27,6 +27,8 @@ struct x86_emul_ops {void (*read_mem)(CPUState *cpu,void *data,target_ulong addr,int bytes);void (*read_segment_descriptor)(CPUState *cpu,struct x86_segment_descriptor *desc,enum X86Seg seg); +void (*handle_io)(CPUState *cpu,uint16_t port,void *data,int direction, +int size,int count); };extern const struct x86_emul_ops *emul_ops;
Signature · 1 line
2.47.2
PATCH v2 4/14 target/i386: rename hvf_mmio_buf to emu_mmio_buf
In reply to Wei LiuWe want to refactor HVF's instruction emulator to a common component. Renaming hvf_mmio_buf removes the association between HVF and the instruction emulator.
The definition of the field is still guarded by CONFIG_HVF for now, since it is the only user.
No functional change.
---
v2: mmio_buf -> emu_mmio_buf per suggestion from Paolo
--- target/i386/cpu.h | 2 +- target/i386/hvf/hvf.c | 4 ++-- target/i386/hvf/x86_emu.c | 12 ++++++------ 3 files changed, 9 insertions(+), 9 deletions(-)
target/i386/cpu.h +1 -1
@@ -2109,7 +2109,7 @@ typedef struct CPUArchState {#endif #if defined(CONFIG_HVF )HVFX86LazyFlags hvf_lflags; -void *hvf_mmio_buf; +void *emu_mmio_buf;#endif uint64_t mcg_cap;
target/i386/hvf/hvf.c +2 -2
@@ -168,7 +168,7 @@ void hvf_arch_vcpu_destroy(CPUState *cpu)X86CPU *x86_cpu =X86_CPU (cpu);CPUX86State *env = &x86_cpu->env; -g_free (env->hvf_mmio_buf); +g_free (env->emu_mmio_buf); }static void init_tsc_freq (CPUX86State *env)
@@ -262,7 +262,7 @@ int hvf_arch_init_vcpu(CPUState *cpu)if (hvf_state->hvf_caps ==NULL ) { hvf_state->hvf_caps = g_new0(struct hvf_vcpu_caps, 1); } - env->hvf_mmio_buf =g_new (char,4096 ); + env->emu_mmio_buf =g_new (char,4096 );if (x86cpu->vmware_cpuid_freq) {init_tsc_freq (env);
target/i386/hvf/x86_emu.c +6 -6
@@ -184,8 +184,8 @@ void write_val_ext(CPUX86State *env, target_ulong ptr, target_ulong val, int sizuint8_t *read_mmio (CPUX86State *env,target_ulong ptr,int bytes) { -vmx_read_mem (env_cpu (env), env->hvf_mmio_buf, ptr, bytes); -return env->hvf_mmio_buf; +vmx_read_mem (env_cpu (env), env->emu_mmio_buf, ptr, bytes); +return env->emu_mmio_buf; }
@@ -487,9 +487,9 @@ static void exec_ins_single(CPUX86State *env, struct x86_decode *decode)target_ulong addr =linear_addr_size (env_cpu (env),RDI (env), decode->addressing_size,R_ES ); - emul_ops->handle_io (env_cpu (env),DX (env), env->hvf_mmio_buf,0 , + emul_ops->handle_io (env_cpu (env),DX (env), env->emu_mmio_buf,0 , decode->operand_size,1 ); -vmx_write_mem (env_cpu (env), addr, env->hvf_mmio_buf, +vmx_write_mem (env_cpu (env), addr, env->emu_mmio_buf, decode->operand_size);string_increment_reg (env,R_EDI , decode);
@@ -510,9 +510,9 @@ static void exec_outs_single(CPUX86State *env, struct x86_decode *decode) {target_ulong addr =decode_linear_addr (env, decode,RSI (env),R_DS ); -vmx_read_mem (env_cpu (env), env->hvf_mmio_buf, addr, +vmx_read_mem (env_cpu (env), env->emu_mmio_buf, addr, decode->operand_size); - emul_ops->handle_io (env_cpu (env),DX (env), env->hvf_mmio_buf,1 , + emul_ops->handle_io (env_cpu (env),DX (env), env->emu_mmio_buf,1 , decode->operand_size,1 );string_increment_reg (env,R_ESI , decode);
Signature · 1 line
2.47.2
PATCH v2 5/14 target/i386/hvf: use emul_ops->read_mem in x86_emu.c
In reply to Wei LiuNo functional change.
--- target/i386/hvf/x86_emu.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
target/i386/hvf/x86_emu.c +5 -5
@@ -184,7 +184,7 @@ void write_val_ext(CPUX86State *env, target_ulong ptr, target_ulong val, int sizuint8_t *read_mmio (CPUX86State *env,target_ulong ptr,int bytes) { -vmx_read_mem (env_cpu (env), env->emu_mmio_buf, ptr, bytes); + emul_ops->read_mem (env_cpu (env), env->emu_mmio_buf, ptr, bytes);return env->emu_mmio_buf; }
@@ -510,8 +510,8 @@ static void exec_outs_single(CPUX86State *env, struct x86_decode *decode) {target_ulong addr =decode_linear_addr (env, decode,RSI (env),R_DS ); -vmx_read_mem (env_cpu (env), env->emu_mmio_buf, addr, - decode->operand_size); + emul_ops->read_mem (env_cpu (env), env->emu_mmio_buf, addr, + decode->operand_size); emul_ops->handle_io (env_cpu (env),DX (env), env->emu_mmio_buf,1 , decode->operand_size,1 );
@@ -620,7 +620,7 @@ static void exec_scas_single(CPUX86State *env, struct x86_decode *decode) addr =linear_addr_size (env_cpu (env),RDI (env), decode->addressing_size,R_ES ); decode->op[1 ].type =X86_VAR_IMMEDIATE ; -vmx_read_mem (env_cpu (env), &decode->op[1 ].val, addr, decode->operand_size); + emul_ops->read_mem (env_cpu (env), &decode->op[1 ].val, addr, decode->operand_size); EXEC_2OP_FLAGS_CMD(env, decode, -, SET_FLAGS_OSZAPC_SUB, false);string_increment_reg (env,R_EDI , decode);
@@ -645,7 +645,7 @@ static void exec_lods_single(CPUX86State *env, struct x86_decode *decode)target_ulong val =0 ; addr =decode_linear_addr (env, decode,RSI (env),R_DS ); -vmx_read_mem (env_cpu (env), &val, addr, decode->operand_size); + emul_ops->read_mem (env_cpu (env), &val, addr, decode->operand_size);write_reg (env,R_EAX , val, decode->operand_size);string_increment_reg (env,R_ESI , decode);
Signature · 1 line
2.47.2
PATCH v2 6/14 taret/i386/hvf: provide and use write_mem in emul_ops
In reply to Wei Liu--- target/i386/hvf/hvf.c | 6 ++++++ target/i386/hvf/x86_emu.c | 8 ++++---- target/i386/hvf/x86_emu.h | 1 + 3 files changed, 11 insertions(+), 4 deletions(-)
target/i386/hvf/hvf.c +6 -0
@@ -242,8 +242,14 @@ static void hvf_read_mem(CPUState *cpu, void *data, target_ulong gva, int bytes)vmx_read_mem (cpu, data, gva, bytes); } +static void hvf_write_mem (CPUState *cpu,void *data,target_ulong gva,int bytes) +{ +vmx_write_mem (cpu, gva, data, bytes); +} +static const struct x86_emul_ops hvf_x86_emul_ops = { .read_mem = hvf_read_mem, + .write_mem = hvf_write_mem, .read_segment_descriptor = hvf_read_segment_descriptor, .handle_io = hvf_handle_io, };
target/i386/hvf/x86_emu.c +4 -4
@@ -179,7 +179,7 @@ void write_val_ext(CPUX86State *env, target_ulong ptr, target_ulong val, int sizwrite_val_to_reg (ptr, val, size);return ; } -vmx_write_mem (env_cpu (env), ptr, &val, size); + emul_ops->write_mem (env_cpu (env), &val, ptr, size); }uint8_t *read_mmio (CPUX86State *env,target_ulong ptr,int bytes)
@@ -489,8 +489,8 @@ static void exec_ins_single(CPUX86State *env, struct x86_decode *decode) emul_ops->handle_io (env_cpu (env),DX (env), env->emu_mmio_buf,0 , decode->operand_size,1 ); -vmx_write_mem (env_cpu (env), addr, env->emu_mmio_buf, - decode->operand_size); + emul_ops->write_mem (env_cpu (env), env->emu_mmio_buf, addr, + decode->operand_size);string_increment_reg (env,R_EDI , decode); }
@@ -596,7 +596,7 @@ static void exec_stos_single(CPUX86State *env, struct x86_decode *decode) addr =linear_addr_size (env_cpu (env),RDI (env), decode->addressing_size,R_ES ); val =read_reg (env,R_EAX , decode->operand_size); -vmx_write_mem (env_cpu (env), addr, &val, decode->operand_size); + emul_ops->write_mem (env_cpu (env), &val, addr, decode->operand_size);string_increment_reg (env,R_EDI , decode); }
target/i386/hvf/x86_emu.h +1 -0
@@ -25,6 +25,7 @@struct x86_emul_ops {void (*read_mem)(CPUState *cpu,void *data,target_ulong addr,int bytes); +void (*write_mem)(CPUState *cpu,void *data,target_ulong addr,int bytes);void (*read_segment_descriptor)(CPUState *cpu,struct x86_segment_descriptor *desc,enum X86Seg seg); void (*handle_io)(CPUState *cpu, uint16_t port, void *data, int direction,
Signature · 1 line
2.47.2
PATCH v2 7/14 target/i386/hvf: provide and use simulate_{wrmsr, rdmsr} in emul_ops
In reply to Wei LiuChange the first argument's type to be CPUState to match other hooks.
--- target/i386/hvf/hvf-i386.h | 4 ++-- target/i386/hvf/hvf.c | 18 ++++++++++-------- target/i386/hvf/x86_emu.c | 4 ++-- target/i386/hvf/x86_emu.h | 2 ++ 4 files changed, 16 insertions(+), 12 deletions(-)
target/i386/hvf/hvf-i386.h +2 -2
@@ -19,8 +19,8 @@uint32_t hvf_get_supported_cpuid (uint32_t func,uint32_t idx,int reg);void hvf_handle_io (CPUState *,uint16_t ,void *,int ,int ,int ); -void hvf_simulate_rdmsr (CPUX86State *env); -void hvf_simulate_wrmsr (CPUX86State *env); +void hvf_simulate_rdmsr (CPUState *cpu); +void hvf_simulate_wrmsr (CPUState *cpu);/* Host specific functions */ int hvf_inject_interrupt (CPUArchState *env,int vector);
target/i386/hvf/hvf.c +10 -8
@@ -252,6 +252,8 @@ static const struct x86_emul_ops hvf_x86_emul_ops = { .write_mem = hvf_write_mem, .read_segment_descriptor = hvf_read_segment_descriptor, .handle_io = hvf_handle_io, + .simulate_rdmsr = hvf_simulate_rdmsr, + .simulate_wrmsr = hvf_simulate_wrmsr, };int hvf_arch_init_vcpu (CPUState *cpu)
@@ -506,10 +508,10 @@ void hvf_store_regs(CPUState *cs)macvm_set_rip (cs, env->eip); } -void hvf_simulate_rdmsr (CPUX86State *env) +void hvf_simulate_rdmsr (CPUState *cs) { -X86CPU *cpu =env_archcpu (env); -CPUState *cs =env_cpu (env); +X86CPU *cpu =X86_CPU (cs); +CPUX86State *env = &cpu->env;uint32_t msr =ECX (env);uint64_t val =0 ;
@@ -611,10 +613,10 @@ void hvf_simulate_rdmsr(CPUX86State *env)RDX (env) = (uint32_t )(val >>32 ); } -void hvf_simulate_wrmsr (CPUX86State *env) +void hvf_simulate_wrmsr (CPUState *cs) { -X86CPU *cpu =env_archcpu (env); -CPUState *cs =env_cpu (env); +X86CPU *cpu =X86_CPU (cs); +CPUX86State *env = &cpu->env;uint32_t msr =ECX (env);uint64_t data = ((uint64_t )EDX (env) <<32 ) |EAX (env);
@@ -900,9 +902,9 @@ int hvf_vcpu_exec(CPUState *cpu) {hvf_load_regs (cpu);if (exit_reason ==EXIT_REASON_RDMSR ) { -hvf_simulate_rdmsr (env); +hvf_simulate_rdmsr (cpu); }else { -hvf_simulate_wrmsr (env); +hvf_simulate_wrmsr (cpu); } env->eip += ins_len;hvf_store_regs (cpu);
target/i386/hvf/x86_emu.c +2 -2
@@ -672,13 +672,13 @@ void x86_emul_raise_exception(CPUX86State *env, int exception_index, int error_cstatic void exec_rdmsr (CPUX86State *env,struct x86_decode *decode) { -hvf_simulate_rdmsr (env); + emul_ops->simulate_rdmsr (env_cpu (env)); env->eip += decode->len; }static void exec_wrmsr (CPUX86State *env,struct x86_decode *decode) { -hvf_simulate_wrmsr (env); + emul_ops->simulate_wrmsr (env_cpu (env)); env->eip += decode->len; }
target/i386/hvf/x86_emu.h +2 -0
@@ -30,6 +30,8 @@ struct x86_emul_ops { enum X86Seg seg);void (*handle_io)(CPUState *cpu,uint16_t port,void *data,int direction,int size,int count); +void (*simulate_rdmsr)(CPUState *cs); +void (*simulate_wrmsr)(CPUState *cs); };extern const struct x86_emul_ops *emul_ops;
Signature · 1 line
2.47.2
PATCH v2 8/14 target/i386: rename lazy flags field and its type
In reply to Wei LiuThe same structure and code can be used by other accelerators. Drop the hvf prefix in the type and field name.
No functional change.
--- target/i386/cpu.h | 6 ++-- target/i386/hvf/x86_flags.c | 56 ++++++++++++++++++------------------- 2 files changed, 31 insertions(+), 31 deletions(-)
target/i386/cpu.h +3 -3
@@ -1811,10 +1811,10 @@ typedef struct CPUCaches {CPUCacheInfo *l3_cache; } CPUCaches; -typedef struct HVFX86LazyFlags { +typedef struct X86LazyFlags {target_ulong result;target_ulong auxbits; -}HVFX86LazyFlags ; +}X86LazyFlags ; typedef struct CPUArchState { /* standard registers */
@@ -2108,7 +2108,7 @@ typedef struct CPUArchState {QemuMutex xen_timers_lock;#endif #if defined(CONFIG_HVF ) -HVFX86LazyFlags hvf_lflags; +X86LazyFlags lflags;void *emu_mmio_buf;#endif
target/i386/hvf/x86_flags.c +28 -28
139 lines not on this page · raw message
Signature · 1 line
2.47.2
PATCH v2 9/14 target/i386/hvf: drop unused headers
In reply to Wei Liu--- target/i386/hvf/x86_decode.c | 3 --- target/i386/hvf/x86_emu.c | 4 ---- 2 files changed, 7 deletions(-)
target/i386/hvf/x86_decode.c +0 -3
@@ -20,10 +20,7 @@#include "panic.h" #include "x86_decode.h" -#include "vmx.h" #include "x86_emu.h" -#include "x86_mmu.h" -#include "x86_descr.h" #define OPCODE_ESCAPE 0xf
target/i386/hvf/x86_emu.c +0 -4
@@ -40,11 +40,7 @@#include "x86_decode.h" #include "x86.h" #include "x86_emu.h" -#include "x86_mmu.h" #include "x86_flags.h" -#include "vmcs.h" -#include "vmx.h" -#include "hvf-i386.h" #define EXEC_2OP_FLAGS_CMD (env, decode, cmd,FLAGS_FUNC , save_res) \ { \
Signature · 1 line
2.47.2
PATCH v2 10/14 target/i386/hvf: rename some include guards
In reply to Wei LiuThese headers will be moved out to its own component.
--- target/i386/hvf/x86.h | 4 ++-- target/i386/hvf/x86_decode.h | 4 ++-- target/i386/hvf/x86_flags.h | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-)
target/i386/hvf/x86.h +2 -2
@@ -16,8 +16,8 @@* License along with this program; if not, see <http://www.gnu.org/licenses/>. */ -#ifndef HVF_X86_H -#define HVF_X86_H +#ifndef X86_EMU_DEFS_H +#define X86_EMU_DEFS_H typedef struct x86_register { union {
target/i386/hvf/x86_decode.h +2 -2
@@ -15,8 +15,8 @@* License along with this program; if not, see <http://www.gnu.org/licenses/>. */ -#ifndef HVF_X86_DECODE_H -#define HVF_X86_DECODE_H +#ifndef X86_EMU_DECODE_H +#define X86_EMU_DECODE_H #include "cpu.h" #include "x86.h"
target/i386/hvf/x86_flags.h +3 -3
@@ -21,8 +21,8 @@* x86 eflags functions */ -#ifndef X86_FLAGS_H -#define X86_FLAGS_H +#ifndef X86_EMU_FLAGS_H +#define X86_EMU_FLAGS_H #include "cpu.h" void lflags_to_rflags (CPUX86State *env);
@@ -78,4 +78,4 @@ void SET_FLAGS_OSZAPC_LOGIC16(CPUX86State *env, uint16_t v1, uint16_t v2,void SET_FLAGS_OSZAPC_LOGIC8 (CPUX86State *env,uint8_t v1,uint8_t v2,uint8_t diff); -#endif /* X86_FLAGS_H */ +#endif /* X86_EMU_FLAGS_H */
Signature · 1 line
2.47.2
PATCH v2 11/14 target/i386: add a directory for x86 instruction emulator
In reply to Wei Liu---
v2: name the directory emulate
--- target/i386/emulate/meson.build | 0 target/i386/meson.build | 1 + 2 files changed, 1 insertion(+) create mode 100644 target/i386/emulate/meson.build
target/i386/emulate/meson.build new file +0 -0
target/i386/meson.build +1 -0
@@ -31,6 +31,7 @@ subdir('whpx') subdir('nvmm') subdir('hvf') subdir('tcg') +subdir('emulate') target_arch += {'i386': i386_ss} target_system_arch += {'i386': i386_system_ss}
Signature · 1 line
2.47.2
PATCH v2 12/14 target/i386/emulate: add a panic.h
In reply to Wei LiuThe macros will be used by the instruction emulator. The code is the same as the one under hvf.
--- target/i386/emulate/panic.h | 45 +++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 target/i386/emulate/panic.h
target/i386/emulate/panic.h new file +45 -0
@@ -0,0 +1,45 @@ +/* +* Copyright (C) 2016 Veertu Inc, +* Copyright (C) 2017 Google Inc, +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU Lesser General Public +* License as published by the Free Software Foundation; either +* version 2.1 of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public +* License along with this program; if not, see <http://www.gnu.org/licenses/>. +*/ +#ifndef X86_EMU_PANIC_H +#define X86_EMU_PANIC_H + +#define VM_PANIC (x) {\ + printf("%s\n", x); \ + abort(); \ +} + +#define VM_PANIC_ON (x) {\ + if (x) { \ + printf("%s\n", #x); \ + abort(); \ + } \ +} + +#define VM_PANIC_EX (...) {\ + printf(__VA_ARGS__); \ + abort(); \ +} + +#define VM_PANIC_ON_EX (x, ...) {\ + if (x) { \ + printf(__VA_ARGS__); \ + abort(); \ + } \ +} + +#endif
Signature · 1 line
2.47.2
PATCH v2 13/14 target/i386: move x86 instruction emulator out of hvf
In reply to Wei LiuMove x86_decode, x86_emu, x86_flags and some headers to the new location. Fix up all the inclusion sites in hvf.
---
target/i386/emulate/meson.build | 5 +++++
target/i386/{hvf => emulate}/x86.h | 0
target/i386/{hvf => emulate}/x86_decode.c | 0
target/i386/{hvf => emulate}/x86_decode.h | 0
target/i386/{hvf => emulate}/x86_emu.c | 0
target/i386/{hvf => emulate}/x86_emu.h | 0
target/i386/{hvf => emulate}/x86_flags.c | 0
target/i386/{hvf => emulate}/x86_flags.h | 0
target/i386/hvf/hvf.c | 8 ++++----
target/i386/hvf/meson.build | 3 ---
target/i386/hvf/vmx.h | 2 +-
target/i386/hvf/x86.c | 4 ++--
target/i386/hvf/x86_cpuid.c | 2 +-
target/i386/hvf/x86_descr.h | 2 +-
target/i386/hvf/x86_mmu.c | 2 +-
target/i386/hvf/x86_task.c | 6 +++---
target/i386/hvf/x86hvf.c | 2 +-
17 files changed, 19 insertions(+), 17 deletions(-)
rename target/i386/{hvf => emulate}/x86.h (100%)
rename target/i386/{hvf => emulate}/x86_decode.c (100%)
rename target/i386/{hvf => emulate}/x86_decode.h (100%)
rename target/i386/{hvf => emulate}/x86_emu.c (100%)
rename target/i386/{hvf => emulate}/x86_emu.h (100%)
rename target/i386/{hvf => emulate}/x86_flags.c (100%)
rename target/i386/{hvf => emulate}/x86_flags.h (100%)target/i386/emulate/meson.build +5 -0
@@ -0,0 +1,5 @@ +i386_system_ss.add(when: [hvf, 'CONFIG_HVF'], if_true: files( + 'x86_decode.c', + 'x86_emu.c', + 'x86_flags.c', +))
target/i386/emulate/x86.h renamed from target/i386/hvf/x86.h +0 -0
target/i386/emulate/x86_decode.c renamed from target/i386/hvf/x86_decode.c +0 -0
target/i386/emulate/x86_decode.h renamed from target/i386/hvf/x86_decode.h +0 -0
target/i386/emulate/x86_emu.c renamed from target/i386/hvf/x86_emu.c +0 -0
target/i386/emulate/x86_emu.h renamed from target/i386/hvf/x86_emu.h +0 -0
target/i386/emulate/x86_flags.c renamed from target/i386/hvf/x86_flags.c +0 -0
target/i386/emulate/x86_flags.h renamed from target/i386/hvf/x86_flags.h +0 -0
target/i386/hvf/hvf.c +4 -4
@@ -59,12 +59,12 @@#include "hvf-i386.h" #include "vmcs.h" #include "vmx.h" -#include "x86.h" +#include "emulate/x86.h" #include "x86_descr.h" -#include "x86_flags.h" +#include "emulate/x86_flags.h" #include "x86_mmu.h" -#include "x86_decode.h" -#include "x86_emu.h" +#include "emulate/x86_decode.h" +#include "emulate/x86_emu.h" #include "x86_task.h" #include "x86hvf.h"
target/i386/hvf/meson.build +0 -3
@@ -2,10 +2,7 @@ i386_system_ss.add(when: [hvf, 'CONFIG_HVF'], if_true: files( 'hvf.c', 'x86.c', 'x86_cpuid.c', - 'x86_decode.c', 'x86_descr.c', - 'x86_emu.c', - 'x86_flags.c', 'x86_mmu.c', 'x86_task.c', 'x86hvf.c',
target/i386/hvf/vmx.h +1 -1
@@ -29,7 +29,7 @@#include <Hypervisor/hv_vmx.h> #include "vmcs.h" #include "cpu.h" -#include "x86.h" +#include "emulate/x86.h" #include "system/hvf.h" #include "system/hvf_int.h"
target/i386/hvf/x86.c +2 -2
@@ -19,8 +19,8 @@#include "qemu/osdep.h" #include "cpu.h" -#include "x86_decode.h" -#include "x86_emu.h" +#include "emulate/x86_decode.h" +#include "emulate/x86_emu.h" #include "vmcs.h" #include "vmx.h" #include "x86_mmu.h"
target/i386/hvf/x86_cpuid.c +1 -1
@@ -24,7 +24,7 @@#include "qemu/cpuid.h" #include "host/cpuinfo.h" #include "cpu.h" -#include "x86.h" +#include "emulate/x86.h" #include "vmx.h" #include "system/hvf.h" #include "hvf-i386.h"
target/i386/hvf/x86_descr.h +1 -1
@@ -19,7 +19,7 @@ #ifndef HVF_X86_DESCR_H #define HVF_X86_DESCR_H -#include "x86.h" +#include "emulate/x86.h" typedef struct vmx_segment { uint16_t sel;
target/i386/hvf/x86_mmu.c +1 -1
@@ -19,7 +19,7 @@#include "qemu/osdep.h" #include "panic.h" #include "cpu.h" -#include "x86.h" +#include "emulate/x86.h" #include "x86_mmu.h" #include "vmcs.h" #include "vmx.h"
target/i386/hvf/x86_task.c +3 -3
@@ -14,11 +14,11 @@#include "hvf-i386.h" #include "vmcs.h" #include "vmx.h" -#include "x86.h" +#include "emulate/x86.h" #include "x86_descr.h" #include "x86_mmu.h" -#include "x86_decode.h" -#include "x86_emu.h" +#include "emulate/x86_decode.h" +#include "emulate/x86_emu.h" #include "x86_task.h" #include "x86hvf.h"
target/i386/hvf/x86hvf.c +1 -1
@@ -24,7 +24,7 @@#include "vmcs.h" #include "cpu.h" #include "x86_descr.h" -#include "x86_decode.h" +#include "emulate/x86_decode.h" #include "system/hw_accel.h" #include "hw/i386/apic_internal.h"
Signature · 1 line
2.47.2
PATCH v2 14/14 MAINTAINERS: add an entry for the x86 instruction emulator
In reply to Wei LiuAdd myself as a reviewer.
---
v2: new
--- MAINTAINERS | 8 ++++++++ 1 file changed, 8 insertions(+)
MAINTAINERS +8 -0
@@ -533,6 +533,14 @@ S: Supported F: target/i386/whpx/ F: include/system/whpx.h +X86 Instruction Emulator +M: Cameron Esfahani <[email protected]> +M: Roman Bolshakov <[email protected]> +R: Phil Dennis-Jordan <[email protected]> +R: Wei Liu <[email protected]> +S: Maintained +F: target/i386/emulate/ + Guest CPU Cores (Xen) --------------------- X86 Xen CPUs
Signature · 1 line
2.47.2
Quoted 30 lines · Wei Liu
Hi,
Microsoft's Linux Systems Group developed a Linux driver for the Microsoft Hypervisor (MSHV for short). The driver is being upstreamed. The first supported VMM is Cloud Hypervisor. We want to add QEMU as the second supported VMM.
The plan is to write an mshv accelerator in QEMU. The accelerator is still in the works.
MSHV doesn't emulate instructions. VMMs are supposed to bring their own instruction emulator. The path we've chosen is to reuse what's already in QEMU. The instruction emulator in HVF looks good for what we need.
This patch series makes the instruction emulator in HVF a common component for the i386 target. It removes HVF specific code by using a set of hooks. The new incoming MSHV accelerator will implement the hooks, and where necessary, enhance the emulator and / or add new hooks.
The patches have been lightly tested by running a Linux VM on an Intel-based Mac.
Thanks,
Wei.Changes in v2:
1. Address comments from Paolo on variable and directory names.
2. Rebase and drop the already applied patches.
3. Add a new entry inMAINTAINERS.
Quoted 15 lines
Wei Liu (14):
target/i386/hvf: introducex86_emul_ops
target/i386/hvf: remove HVF specific calls fromx86_decode.ctarget/i386/hvf: provide and usehandle_ioinemul_opstarget/i386: renamehvf_mmio_buftoemu_mmio_buftarget/i386/hvf: useemul_ops->read_meminx86_emu.ctaret/i386/hvf: provide and usewrite_meminemul_opstarget/i386/hvf: provide and use simulate_{wrmsr,rdmsr} inemul_opstarget/i386: rename lazy flags field and its type target/i386/hvf: drop unused headers
target/i386/hvf: rename some include guards
target/i386: add a directory for x86 instruction emulator target/i386/emulate: add apanic.h
target/i386: move x86 instruction emulator out of hvf
MAINTAINERS: add an entry for the x86 instruction emulator
HVF maintainers, Ping?
Thanks,
Wei.