* [PATCH v2 01/14] target/i386/hvf: introduce x86_emul_ops
2025-03-07 19:55 [PATCH v2 00/14] Factor out HVF's instruction emulator Wei Liu
@ 2025-03-07 19:55 ` Wei Liu
2025-03-07 19:55 ` [PATCH v2 02/14] target/i386/hvf: remove HVF specific calls from x86_decode.c Wei Liu
` (13 subsequent siblings)
14 siblings, 0 replies; 16+ messages in thread
From: Wei Liu @ 2025-03-07 19:55 UTC (permalink / raw)
To: qemu-devel
Cc: wei.liu, dirty, rbolshakov, phil, jinankjain, liuwe, muislam,
ziqiaozhou, mukeshrathor, magnuskulke, prapal, jpiotrowski, deviv,
Wei Liu
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.
Signed-off-by: Wei Liu <liuwe@linux.microsoft.com>
---
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(-)
diff --git a/target/i386/hvf/hvf.c b/target/i386/hvf/hvf.c
index 9ba0e04ac756..03456ffbc705 100644
--- a/target/i386/hvf/hvf.c
+++ b/target/i386/hvf/hvf.c
@@ -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) {
diff --git a/target/i386/hvf/x86_emu.c b/target/i386/hvf/x86_emu.c
index ebba80a36b50..c15b5a7ca850 100644
--- a/target/i386/hvf/x86_emu.c
+++ b/target/i386/hvf/x86_emu.c
@@ -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();
}
diff --git a/target/i386/hvf/x86_emu.h b/target/i386/hvf/x86_emu.h
index bc0fc72c761b..1422d06ea184 100644
--- a/target/i386/hvf/x86_emu.h
+++ b/target/i386/hvf/x86_emu.h
@@ -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);
--
2.47.2
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH v2 02/14] target/i386/hvf: remove HVF specific calls from x86_decode.c
2025-03-07 19:55 [PATCH v2 00/14] Factor out HVF's instruction emulator Wei Liu
2025-03-07 19:55 ` [PATCH v2 01/14] target/i386/hvf: introduce x86_emul_ops Wei Liu
@ 2025-03-07 19:55 ` Wei Liu
2025-03-07 19:55 ` [PATCH v2 03/14] target/i386/hvf: provide and use handle_io in emul_ops Wei Liu
` (12 subsequent siblings)
14 siblings, 0 replies; 16+ messages in thread
From: Wei Liu @ 2025-03-07 19:55 UTC (permalink / raw)
To: qemu-devel
Cc: wei.liu, dirty, rbolshakov, phil, jinankjain, liuwe, muislam,
ziqiaozhou, mukeshrathor, magnuskulke, prapal, jpiotrowski, deviv,
Wei Liu
Use the newly defined emul_ops. This allows the module to be reused
by other accelerator in the future.
No functional change intended.
Signed-off-by: Wei Liu <liuwe@linux.microsoft.com>
---
target/i386/hvf/x86_decode.c | 19 ++++++-------------
1 file changed, 6 insertions(+), 13 deletions(-)
diff --git a/target/i386/hvf/x86_decode.c b/target/i386/hvf/x86_decode.c
index 5fea2dd3cc03..728e15963817 100644
--- a/target/i386/hvf/x86_decode.c
+++ b/target/i386/hvf/x86_decode.c
@@ -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) {
--
2.47.2
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH v2 03/14] target/i386/hvf: provide and use handle_io in emul_ops
2025-03-07 19:55 [PATCH v2 00/14] Factor out HVF's instruction emulator Wei Liu
2025-03-07 19:55 ` [PATCH v2 01/14] target/i386/hvf: introduce x86_emul_ops Wei Liu
2025-03-07 19:55 ` [PATCH v2 02/14] target/i386/hvf: remove HVF specific calls from x86_decode.c Wei Liu
@ 2025-03-07 19:55 ` Wei Liu
2025-03-07 19:55 ` [PATCH v2 04/14] target/i386: rename hvf_mmio_buf to emu_mmio_buf Wei Liu
` (11 subsequent siblings)
14 siblings, 0 replies; 16+ messages in thread
From: Wei Liu @ 2025-03-07 19:55 UTC (permalink / raw)
To: qemu-devel
Cc: wei.liu, dirty, rbolshakov, phil, jinankjain, liuwe, muislam,
ziqiaozhou, mukeshrathor, magnuskulke, prapal, jpiotrowski, deviv,
Wei Liu
This drops the calls to hvf_handle_io from x86_emu.c.
Signed-off-by: Wei Liu <liuwe@linux.microsoft.com>
---
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(-)
diff --git a/target/i386/hvf/hvf.c b/target/i386/hvf/hvf.c
index 03456ffbc705..7da03f9c0811 100644
--- a/target/i386/hvf/hvf.c
+++ b/target/i386/hvf/hvf.c
@@ -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)
diff --git a/target/i386/hvf/x86_emu.c b/target/i386/hvf/x86_emu.c
index c15b5a7ca850..7b01ccde5d3e 100644
--- a/target/i386/hvf/x86_emu.c
+++ b/target/i386/hvf/x86_emu.c
@@ -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);
}
diff --git a/target/i386/hvf/x86_emu.h b/target/i386/hvf/x86_emu.h
index 1422d06ea184..40cc786694e1 100644
--- a/target/i386/hvf/x86_emu.h
+++ b/target/i386/hvf/x86_emu.h
@@ -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;
--
2.47.2
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH v2 04/14] target/i386: rename hvf_mmio_buf to emu_mmio_buf
2025-03-07 19:55 [PATCH v2 00/14] Factor out HVF's instruction emulator Wei Liu
` (2 preceding siblings ...)
2025-03-07 19:55 ` [PATCH v2 03/14] target/i386/hvf: provide and use handle_io in emul_ops Wei Liu
@ 2025-03-07 19:55 ` Wei Liu
2025-03-07 19:55 ` [PATCH v2 05/14] target/i386/hvf: use emul_ops->read_mem in x86_emu.c Wei Liu
` (10 subsequent siblings)
14 siblings, 0 replies; 16+ messages in thread
From: Wei Liu @ 2025-03-07 19:55 UTC (permalink / raw)
To: qemu-devel
Cc: wei.liu, dirty, rbolshakov, phil, jinankjain, liuwe, muislam,
ziqiaozhou, mukeshrathor, magnuskulke, prapal, jpiotrowski, deviv,
Wei Liu
We 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.
Signed-off-by: Wei Liu <liuwe@linux.microsoft.com>
---
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(-)
diff --git a/target/i386/cpu.h b/target/i386/cpu.h
index 7882b63b9b61..cf2bd0e9ada9 100644
--- a/target/i386/cpu.h
+++ b/target/i386/cpu.h
@@ -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;
diff --git a/target/i386/hvf/hvf.c b/target/i386/hvf/hvf.c
index 7da03f9c0811..1cecb765952b 100644
--- a/target/i386/hvf/hvf.c
+++ b/target/i386/hvf/hvf.c
@@ -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);
diff --git a/target/i386/hvf/x86_emu.c b/target/i386/hvf/x86_emu.c
index 7b01ccde5d3e..e59a73e00d5c 100644
--- a/target/i386/hvf/x86_emu.c
+++ b/target/i386/hvf/x86_emu.c
@@ -184,8 +184,8 @@ void write_val_ext(CPUX86State *env, target_ulong ptr, target_ulong val, int siz
uint8_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);
--
2.47.2
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH v2 05/14] target/i386/hvf: use emul_ops->read_mem in x86_emu.c
2025-03-07 19:55 [PATCH v2 00/14] Factor out HVF's instruction emulator Wei Liu
` (3 preceding siblings ...)
2025-03-07 19:55 ` [PATCH v2 04/14] target/i386: rename hvf_mmio_buf to emu_mmio_buf Wei Liu
@ 2025-03-07 19:55 ` Wei Liu
2025-03-07 19:55 ` [PATCH v2 06/14] taret/i386/hvf: provide and use write_mem in emul_ops Wei Liu
` (9 subsequent siblings)
14 siblings, 0 replies; 16+ messages in thread
From: Wei Liu @ 2025-03-07 19:55 UTC (permalink / raw)
To: qemu-devel
Cc: wei.liu, dirty, rbolshakov, phil, jinankjain, liuwe, muislam,
ziqiaozhou, mukeshrathor, magnuskulke, prapal, jpiotrowski, deviv,
Wei Liu
No functional change.
Signed-off-by: Wei Liu <liuwe@linux.microsoft.com>
---
target/i386/hvf/x86_emu.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/target/i386/hvf/x86_emu.c b/target/i386/hvf/x86_emu.c
index e59a73e00d5c..7b816b5a1dab 100644
--- a/target/i386/hvf/x86_emu.c
+++ b/target/i386/hvf/x86_emu.c
@@ -184,7 +184,7 @@ void write_val_ext(CPUX86State *env, target_ulong ptr, target_ulong val, int siz
uint8_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);
--
2.47.2
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH v2 06/14] taret/i386/hvf: provide and use write_mem in emul_ops
2025-03-07 19:55 [PATCH v2 00/14] Factor out HVF's instruction emulator Wei Liu
` (4 preceding siblings ...)
2025-03-07 19:55 ` [PATCH v2 05/14] target/i386/hvf: use emul_ops->read_mem in x86_emu.c Wei Liu
@ 2025-03-07 19:55 ` Wei Liu
2025-03-07 19:55 ` [PATCH v2 07/14] target/i386/hvf: provide and use simulate_{wrmsr, rdmsr} " Wei Liu
` (8 subsequent siblings)
14 siblings, 0 replies; 16+ messages in thread
From: Wei Liu @ 2025-03-07 19:55 UTC (permalink / raw)
To: qemu-devel
Cc: wei.liu, dirty, rbolshakov, phil, jinankjain, liuwe, muislam,
ziqiaozhou, mukeshrathor, magnuskulke, prapal, jpiotrowski, deviv,
Wei Liu
Signed-off-by: Wei Liu <liuwe@linux.microsoft.com>
---
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(-)
diff --git a/target/i386/hvf/hvf.c b/target/i386/hvf/hvf.c
index 1cecb765952b..e4f48a79fb7c 100644
--- a/target/i386/hvf/hvf.c
+++ b/target/i386/hvf/hvf.c
@@ -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,
};
diff --git a/target/i386/hvf/x86_emu.c b/target/i386/hvf/x86_emu.c
index 7b816b5a1dab..3ff41c35d89a 100644
--- a/target/i386/hvf/x86_emu.c
+++ b/target/i386/hvf/x86_emu.c
@@ -179,7 +179,7 @@ void write_val_ext(CPUX86State *env, target_ulong ptr, target_ulong val, int siz
write_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);
}
diff --git a/target/i386/hvf/x86_emu.h b/target/i386/hvf/x86_emu.h
index 40cc786694e1..107c1f1ac866 100644
--- a/target/i386/hvf/x86_emu.h
+++ b/target/i386/hvf/x86_emu.h
@@ -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,
--
2.47.2
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH v2 07/14] target/i386/hvf: provide and use simulate_{wrmsr, rdmsr} in emul_ops
2025-03-07 19:55 [PATCH v2 00/14] Factor out HVF's instruction emulator Wei Liu
` (5 preceding siblings ...)
2025-03-07 19:55 ` [PATCH v2 06/14] taret/i386/hvf: provide and use write_mem in emul_ops Wei Liu
@ 2025-03-07 19:55 ` Wei Liu
2025-03-07 19:55 ` [PATCH v2 08/14] target/i386: rename lazy flags field and its type Wei Liu
` (7 subsequent siblings)
14 siblings, 0 replies; 16+ messages in thread
From: Wei Liu @ 2025-03-07 19:55 UTC (permalink / raw)
To: qemu-devel
Cc: wei.liu, dirty, rbolshakov, phil, jinankjain, liuwe, muislam,
ziqiaozhou, mukeshrathor, magnuskulke, prapal, jpiotrowski, deviv,
Wei Liu
Change the first argument's type to be CPUState to match other hooks.
Signed-off-by: Wei Liu <liuwe@linux.microsoft.com>
---
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(-)
diff --git a/target/i386/hvf/hvf-i386.h b/target/i386/hvf/hvf-i386.h
index 044ad236ae80..8c42ae6b0130 100644
--- a/target/i386/hvf/hvf-i386.h
+++ b/target/i386/hvf/hvf-i386.h
@@ -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);
diff --git a/target/i386/hvf/hvf.c b/target/i386/hvf/hvf.c
index e4f48a79fb7c..8c31d2e0cf72 100644
--- a/target/i386/hvf/hvf.c
+++ b/target/i386/hvf/hvf.c
@@ -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);
diff --git a/target/i386/hvf/x86_emu.c b/target/i386/hvf/x86_emu.c
index 3ff41c35d89a..aec7a8a3fa85 100644
--- a/target/i386/hvf/x86_emu.c
+++ b/target/i386/hvf/x86_emu.c
@@ -672,13 +672,13 @@ void x86_emul_raise_exception(CPUX86State *env, int exception_index, int error_c
static 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;
}
diff --git a/target/i386/hvf/x86_emu.h b/target/i386/hvf/x86_emu.h
index 107c1f1ac866..555b567e2c7d 100644
--- a/target/i386/hvf/x86_emu.h
+++ b/target/i386/hvf/x86_emu.h
@@ -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;
--
2.47.2
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH v2 08/14] target/i386: rename lazy flags field and its type
2025-03-07 19:55 [PATCH v2 00/14] Factor out HVF's instruction emulator Wei Liu
` (6 preceding siblings ...)
2025-03-07 19:55 ` [PATCH v2 07/14] target/i386/hvf: provide and use simulate_{wrmsr, rdmsr} " Wei Liu
@ 2025-03-07 19:55 ` Wei Liu
2025-03-07 19:55 ` [PATCH v2 09/14] target/i386/hvf: drop unused headers Wei Liu
` (6 subsequent siblings)
14 siblings, 0 replies; 16+ messages in thread
From: Wei Liu @ 2025-03-07 19:55 UTC (permalink / raw)
To: qemu-devel
Cc: wei.liu, dirty, rbolshakov, phil, jinankjain, liuwe, muislam,
ziqiaozhou, mukeshrathor, magnuskulke, prapal, jpiotrowski, deviv,
Wei Liu
The same structure and code can be used by other accelerators. Drop
the hvf prefix in the type and field name.
No functional change.
Signed-off-by: Wei Liu <liuwe@linux.microsoft.com>
---
target/i386/cpu.h | 6 ++--
target/i386/hvf/x86_flags.c | 56 ++++++++++++++++++-------------------
2 files changed, 31 insertions(+), 31 deletions(-)
diff --git a/target/i386/cpu.h b/target/i386/cpu.h
index cf2bd0e9ada9..04ade00abb3d 100644
--- a/target/i386/cpu.h
+++ b/target/i386/cpu.h
@@ -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
diff --git a/target/i386/hvf/x86_flags.c b/target/i386/hvf/x86_flags.c
index 03d6de5efc3e..3c02c9c5632e 100644
--- a/target/i386/hvf/x86_flags.c
+++ b/target/i386/hvf/x86_flags.c
@@ -62,7 +62,7 @@
#define SET_FLAGS_OSZAPC_SIZE(size, lf_carries, lf_result) { \
target_ulong temp = ((lf_carries) & (LF_MASK_AF)) | \
(((lf_carries) >> (size - 2)) << LF_BIT_PO); \
- env->hvf_lflags.result = (target_ulong)(int##size##_t)(lf_result); \
+ env->lflags.result = (target_ulong)(int##size##_t)(lf_result); \
if ((size) == 32) { \
temp = ((lf_carries) & ~(LF_MASK_PDB | LF_MASK_SD)); \
} else if ((size) == 16) { \
@@ -72,7 +72,7 @@
} else { \
VM_PANIC("unimplemented"); \
} \
- env->hvf_lflags.auxbits = (target_ulong)(uint32_t)temp; \
+ env->lflags.auxbits = (target_ulong)(uint32_t)temp; \
}
/* carries, result */
@@ -99,10 +99,10 @@
} else { \
VM_PANIC("unimplemented"); \
} \
- env->hvf_lflags.result = (target_ulong)(int##size##_t)(lf_result); \
- target_ulong delta_c = (env->hvf_lflags.auxbits ^ temp) & LF_MASK_CF; \
+ env->lflags.result = (target_ulong)(int##size##_t)(lf_result); \
+ target_ulong delta_c = (env->lflags.auxbits ^ temp) & LF_MASK_CF; \
delta_c ^= (delta_c >> 1); \
- env->hvf_lflags.auxbits = (target_ulong)(uint32_t)(temp ^ delta_c); \
+ env->lflags.auxbits = (target_ulong)(uint32_t)(temp ^ delta_c); \
}
/* carries, result */
@@ -116,8 +116,8 @@
void SET_FLAGS_OxxxxC(CPUX86State *env, uint32_t new_of, uint32_t new_cf)
{
uint32_t temp_po = new_of ^ new_cf;
- env->hvf_lflags.auxbits &= ~(LF_MASK_PO | LF_MASK_CF);
- env->hvf_lflags.auxbits |= (temp_po << LF_BIT_PO) | (new_cf << LF_BIT_CF);
+ env->lflags.auxbits &= ~(LF_MASK_PO | LF_MASK_CF);
+ env->lflags.auxbits |= (temp_po << LF_BIT_PO) | (new_cf << LF_BIT_CF);
}
void SET_FLAGS_OSZAPC_SUB32(CPUX86State *env, uint32_t v1, uint32_t v2,
@@ -213,27 +213,27 @@ void SET_FLAGS_OSZAPC_LOGIC8(CPUX86State *env, uint8_t v1, uint8_t v2,
bool get_PF(CPUX86State *env)
{
- uint32_t temp = (255 & env->hvf_lflags.result);
- temp = temp ^ (255 & (env->hvf_lflags.auxbits >> LF_BIT_PDB));
+ uint32_t temp = (255 & env->lflags.result);
+ temp = temp ^ (255 & (env->lflags.auxbits >> LF_BIT_PDB));
temp = (temp ^ (temp >> 4)) & 0x0F;
return (0x9669U >> temp) & 1;
}
void set_PF(CPUX86State *env, bool val)
{
- uint32_t temp = (255 & env->hvf_lflags.result) ^ (!val);
- env->hvf_lflags.auxbits &= ~(LF_MASK_PDB);
- env->hvf_lflags.auxbits |= (temp << LF_BIT_PDB);
+ uint32_t temp = (255 & env->lflags.result) ^ (!val);
+ env->lflags.auxbits &= ~(LF_MASK_PDB);
+ env->lflags.auxbits |= (temp << LF_BIT_PDB);
}
bool get_OF(CPUX86State *env)
{
- return ((env->hvf_lflags.auxbits + (1U << LF_BIT_PO)) >> LF_BIT_CF) & 1;
+ return ((env->lflags.auxbits + (1U << LF_BIT_PO)) >> LF_BIT_CF) & 1;
}
bool get_CF(CPUX86State *env)
{
- return (env->hvf_lflags.auxbits >> LF_BIT_CF) & 1;
+ return (env->lflags.auxbits >> LF_BIT_CF) & 1;
}
void set_OF(CPUX86State *env, bool val)
@@ -250,45 +250,45 @@ void set_CF(CPUX86State *env, bool val)
bool get_AF(CPUX86State *env)
{
- return (env->hvf_lflags.auxbits >> LF_BIT_AF) & 1;
+ return (env->lflags.auxbits >> LF_BIT_AF) & 1;
}
void set_AF(CPUX86State *env, bool val)
{
- env->hvf_lflags.auxbits &= ~(LF_MASK_AF);
- env->hvf_lflags.auxbits |= val << LF_BIT_AF;
+ env->lflags.auxbits &= ~(LF_MASK_AF);
+ env->lflags.auxbits |= val << LF_BIT_AF;
}
bool get_ZF(CPUX86State *env)
{
- return !env->hvf_lflags.result;
+ return !env->lflags.result;
}
void set_ZF(CPUX86State *env, bool val)
{
if (val) {
- env->hvf_lflags.auxbits ^=
- (((env->hvf_lflags.result >> LF_SIGN_BIT) & 1) << LF_BIT_SD);
+ env->lflags.auxbits ^=
+ (((env->lflags.result >> LF_SIGN_BIT) & 1) << LF_BIT_SD);
/* merge the parity bits into the Parity Delta Byte */
- uint32_t temp_pdb = (255 & env->hvf_lflags.result);
- env->hvf_lflags.auxbits ^= (temp_pdb << LF_BIT_PDB);
+ uint32_t temp_pdb = (255 & env->lflags.result);
+ env->lflags.auxbits ^= (temp_pdb << LF_BIT_PDB);
/* now zero the .result value */
- env->hvf_lflags.result = 0;
+ env->lflags.result = 0;
} else {
- env->hvf_lflags.result |= (1 << 8);
+ env->lflags.result |= (1 << 8);
}
}
bool get_SF(CPUX86State *env)
{
- return ((env->hvf_lflags.result >> LF_SIGN_BIT) ^
- (env->hvf_lflags.auxbits >> LF_BIT_SD)) & 1;
+ return ((env->lflags.result >> LF_SIGN_BIT) ^
+ (env->lflags.auxbits >> LF_BIT_SD)) & 1;
}
void set_SF(CPUX86State *env, bool val)
{
bool temp_sf = get_SF(env);
- env->hvf_lflags.auxbits ^= (temp_sf ^ val) << LF_BIT_SD;
+ env->lflags.auxbits ^= (temp_sf ^ val) << LF_BIT_SD;
}
void lflags_to_rflags(CPUX86State *env)
@@ -303,7 +303,7 @@ void lflags_to_rflags(CPUX86State *env)
void rflags_to_lflags(CPUX86State *env)
{
- env->hvf_lflags.auxbits = env->hvf_lflags.result = 0;
+ env->lflags.auxbits = env->lflags.result = 0;
set_OF(env, env->eflags & CC_O);
set_SF(env, env->eflags & CC_S);
set_ZF(env, env->eflags & CC_Z);
--
2.47.2
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH v2 09/14] target/i386/hvf: drop unused headers
2025-03-07 19:55 [PATCH v2 00/14] Factor out HVF's instruction emulator Wei Liu
` (7 preceding siblings ...)
2025-03-07 19:55 ` [PATCH v2 08/14] target/i386: rename lazy flags field and its type Wei Liu
@ 2025-03-07 19:55 ` Wei Liu
2025-03-07 19:55 ` [PATCH v2 10/14] target/i386/hvf: rename some include guards Wei Liu
` (5 subsequent siblings)
14 siblings, 0 replies; 16+ messages in thread
From: Wei Liu @ 2025-03-07 19:55 UTC (permalink / raw)
To: qemu-devel
Cc: wei.liu, dirty, rbolshakov, phil, jinankjain, liuwe, muislam,
ziqiaozhou, mukeshrathor, magnuskulke, prapal, jpiotrowski, deviv,
Wei Liu
Signed-off-by: Wei Liu <liuwe@linux.microsoft.com>
---
target/i386/hvf/x86_decode.c | 3 ---
target/i386/hvf/x86_emu.c | 4 ----
2 files changed, 7 deletions(-)
diff --git a/target/i386/hvf/x86_decode.c b/target/i386/hvf/x86_decode.c
index 728e15963817..ddd7b60bcfe1 100644
--- a/target/i386/hvf/x86_decode.c
+++ b/target/i386/hvf/x86_decode.c
@@ -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
diff --git a/target/i386/hvf/x86_emu.c b/target/i386/hvf/x86_emu.c
index aec7a8a3fa85..26a4876aac09 100644
--- a/target/i386/hvf/x86_emu.c
+++ b/target/i386/hvf/x86_emu.c
@@ -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) \
{ \
--
2.47.2
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH v2 10/14] target/i386/hvf: rename some include guards
2025-03-07 19:55 [PATCH v2 00/14] Factor out HVF's instruction emulator Wei Liu
` (8 preceding siblings ...)
2025-03-07 19:55 ` [PATCH v2 09/14] target/i386/hvf: drop unused headers Wei Liu
@ 2025-03-07 19:55 ` Wei Liu
2025-03-07 19:55 ` [PATCH v2 11/14] target/i386: add a directory for x86 instruction emulator Wei Liu
` (4 subsequent siblings)
14 siblings, 0 replies; 16+ messages in thread
From: Wei Liu @ 2025-03-07 19:55 UTC (permalink / raw)
To: qemu-devel
Cc: wei.liu, dirty, rbolshakov, phil, jinankjain, liuwe, muislam,
ziqiaozhou, mukeshrathor, magnuskulke, prapal, jpiotrowski, deviv,
Wei Liu
These headers will be moved out to its own component.
Signed-off-by: Wei Liu <liuwe@linux.microsoft.com>
---
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(-)
diff --git a/target/i386/hvf/x86.h b/target/i386/hvf/x86.h
index 063cd0b83ec9..73edccfba006 100644
--- a/target/i386/hvf/x86.h
+++ b/target/i386/hvf/x86.h
@@ -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 {
diff --git a/target/i386/hvf/x86_decode.h b/target/i386/hvf/x86_decode.h
index a2d7a2a27b68..930d965164a4 100644
--- a/target/i386/hvf/x86_decode.h
+++ b/target/i386/hvf/x86_decode.h
@@ -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"
diff --git a/target/i386/hvf/x86_flags.h b/target/i386/hvf/x86_flags.h
index 75c2a7feab53..6c175007b571 100644
--- a/target/i386/hvf/x86_flags.h
+++ b/target/i386/hvf/x86_flags.h
@@ -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 */
--
2.47.2
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH v2 11/14] target/i386: add a directory for x86 instruction emulator
2025-03-07 19:55 [PATCH v2 00/14] Factor out HVF's instruction emulator Wei Liu
` (9 preceding siblings ...)
2025-03-07 19:55 ` [PATCH v2 10/14] target/i386/hvf: rename some include guards Wei Liu
@ 2025-03-07 19:55 ` Wei Liu
2025-03-07 19:55 ` [PATCH v2 12/14] target/i386/emulate: add a panic.h Wei Liu
` (3 subsequent siblings)
14 siblings, 0 replies; 16+ messages in thread
From: Wei Liu @ 2025-03-07 19:55 UTC (permalink / raw)
To: qemu-devel
Cc: wei.liu, dirty, rbolshakov, phil, jinankjain, liuwe, muislam,
ziqiaozhou, mukeshrathor, magnuskulke, prapal, jpiotrowski, deviv,
Wei Liu
Signed-off-by: Wei Liu <liuwe@linux.microsoft.com>
---
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
diff --git a/target/i386/emulate/meson.build b/target/i386/emulate/meson.build
new file mode 100644
index 000000000000..e69de29bb2d1
diff --git a/target/i386/meson.build b/target/i386/meson.build
index 2e9c472f49d3..c1aacea61356 100644
--- a/target/i386/meson.build
+++ b/target/i386/meson.build
@@ -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}
--
2.47.2
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH v2 12/14] target/i386/emulate: add a panic.h
2025-03-07 19:55 [PATCH v2 00/14] Factor out HVF's instruction emulator Wei Liu
` (10 preceding siblings ...)
2025-03-07 19:55 ` [PATCH v2 11/14] target/i386: add a directory for x86 instruction emulator Wei Liu
@ 2025-03-07 19:55 ` Wei Liu
2025-03-07 19:55 ` [PATCH v2 13/14] target/i386: move x86 instruction emulator out of hvf Wei Liu
` (2 subsequent siblings)
14 siblings, 0 replies; 16+ messages in thread
From: Wei Liu @ 2025-03-07 19:55 UTC (permalink / raw)
To: qemu-devel
Cc: wei.liu, dirty, rbolshakov, phil, jinankjain, liuwe, muislam,
ziqiaozhou, mukeshrathor, magnuskulke, prapal, jpiotrowski, deviv,
Wei Liu
The macros will be used by the instruction emulator. The code is the same as
the one under hvf.
Signed-off-by: Wei Liu <liuwe@linux.microsoft.com>
---
target/i386/emulate/panic.h | 45 +++++++++++++++++++++++++++++++++++++
1 file changed, 45 insertions(+)
create mode 100644 target/i386/emulate/panic.h
diff --git a/target/i386/emulate/panic.h b/target/i386/emulate/panic.h
new file mode 100644
index 000000000000..71c24874ba03
--- /dev/null
+++ b/target/i386/emulate/panic.h
@@ -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
--
2.47.2
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH v2 13/14] target/i386: move x86 instruction emulator out of hvf
2025-03-07 19:55 [PATCH v2 00/14] Factor out HVF's instruction emulator Wei Liu
` (11 preceding siblings ...)
2025-03-07 19:55 ` [PATCH v2 12/14] target/i386/emulate: add a panic.h Wei Liu
@ 2025-03-07 19:55 ` Wei Liu
2025-03-07 19:55 ` [PATCH v2 14/14] MAINTAINERS: add an entry for the x86 instruction emulator Wei Liu
2025-03-14 19:01 ` [PATCH v2 00/14] Factor out HVF's " Wei Liu
14 siblings, 0 replies; 16+ messages in thread
From: Wei Liu @ 2025-03-07 19:55 UTC (permalink / raw)
To: qemu-devel
Cc: wei.liu, dirty, rbolshakov, phil, jinankjain, liuwe, muislam,
ziqiaozhou, mukeshrathor, magnuskulke, prapal, jpiotrowski, deviv,
Wei Liu
Move x86_decode, x86_emu, x86_flags and some headers to the new location.
Fix up all the inclusion sites in hvf.
Signed-off-by: Wei Liu <liuwe@linux.microsoft.com>
---
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%)
diff --git a/target/i386/emulate/meson.build b/target/i386/emulate/meson.build
index e69de29bb2d1..4edd4f462fc7 100644
--- a/target/i386/emulate/meson.build
+++ b/target/i386/emulate/meson.build
@@ -0,0 +1,5 @@
+i386_system_ss.add(when: [hvf, 'CONFIG_HVF'], if_true: files(
+ 'x86_decode.c',
+ 'x86_emu.c',
+ 'x86_flags.c',
+))
diff --git a/target/i386/hvf/x86.h b/target/i386/emulate/x86.h
similarity index 100%
rename from target/i386/hvf/x86.h
rename to target/i386/emulate/x86.h
diff --git a/target/i386/hvf/x86_decode.c b/target/i386/emulate/x86_decode.c
similarity index 100%
rename from target/i386/hvf/x86_decode.c
rename to target/i386/emulate/x86_decode.c
diff --git a/target/i386/hvf/x86_decode.h b/target/i386/emulate/x86_decode.h
similarity index 100%
rename from target/i386/hvf/x86_decode.h
rename to target/i386/emulate/x86_decode.h
diff --git a/target/i386/hvf/x86_emu.c b/target/i386/emulate/x86_emu.c
similarity index 100%
rename from target/i386/hvf/x86_emu.c
rename to target/i386/emulate/x86_emu.c
diff --git a/target/i386/hvf/x86_emu.h b/target/i386/emulate/x86_emu.h
similarity index 100%
rename from target/i386/hvf/x86_emu.h
rename to target/i386/emulate/x86_emu.h
diff --git a/target/i386/hvf/x86_flags.c b/target/i386/emulate/x86_flags.c
similarity index 100%
rename from target/i386/hvf/x86_flags.c
rename to target/i386/emulate/x86_flags.c
diff --git a/target/i386/hvf/x86_flags.h b/target/i386/emulate/x86_flags.h
similarity index 100%
rename from target/i386/hvf/x86_flags.h
rename to target/i386/emulate/x86_flags.h
diff --git a/target/i386/hvf/hvf.c b/target/i386/hvf/hvf.c
index 8c31d2e0cf72..23ebf2550aca 100644
--- a/target/i386/hvf/hvf.c
+++ b/target/i386/hvf/hvf.c
@@ -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"
diff --git a/target/i386/hvf/meson.build b/target/i386/hvf/meson.build
index 05c3c8cf18b5..519d190f0e6b 100644
--- a/target/i386/hvf/meson.build
+++ b/target/i386/hvf/meson.build
@@ -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',
diff --git a/target/i386/hvf/vmx.h b/target/i386/hvf/vmx.h
index 80ce26279bf0..3c56afc9d3a1 100644
--- a/target/i386/hvf/vmx.h
+++ b/target/i386/hvf/vmx.h
@@ -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"
diff --git a/target/i386/hvf/x86.c b/target/i386/hvf/x86.c
index a0ede138865e..5c75ec9a0079 100644
--- a/target/i386/hvf/x86.c
+++ b/target/i386/hvf/x86.c
@@ -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"
diff --git a/target/i386/hvf/x86_cpuid.c b/target/i386/hvf/x86_cpuid.c
index ae836f65cc92..fa131b18c6d1 100644
--- a/target/i386/hvf/x86_cpuid.c
+++ b/target/i386/hvf/x86_cpuid.c
@@ -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"
diff --git a/target/i386/hvf/x86_descr.h b/target/i386/hvf/x86_descr.h
index ce5de9834973..24af4946cd46 100644
--- a/target/i386/hvf/x86_descr.h
+++ b/target/i386/hvf/x86_descr.h
@@ -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;
diff --git a/target/i386/hvf/x86_mmu.c b/target/i386/hvf/x86_mmu.c
index 579d0c3a4cc5..afc5c17d5d5c 100644
--- a/target/i386/hvf/x86_mmu.c
+++ b/target/i386/hvf/x86_mmu.c
@@ -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"
diff --git a/target/i386/hvf/x86_task.c b/target/i386/hvf/x86_task.c
index 161217991fc0..bdf8b51ae670 100644
--- a/target/i386/hvf/x86_task.c
+++ b/target/i386/hvf/x86_task.c
@@ -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"
diff --git a/target/i386/hvf/x86hvf.c b/target/i386/hvf/x86hvf.c
index 531a340b37c9..2057314892a9 100644
--- a/target/i386/hvf/x86hvf.c
+++ b/target/i386/hvf/x86hvf.c
@@ -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"
--
2.47.2
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH v2 14/14] MAINTAINERS: add an entry for the x86 instruction emulator
2025-03-07 19:55 [PATCH v2 00/14] Factor out HVF's instruction emulator Wei Liu
` (12 preceding siblings ...)
2025-03-07 19:55 ` [PATCH v2 13/14] target/i386: move x86 instruction emulator out of hvf Wei Liu
@ 2025-03-07 19:55 ` Wei Liu
2025-03-14 19:01 ` [PATCH v2 00/14] Factor out HVF's " Wei Liu
14 siblings, 0 replies; 16+ messages in thread
From: Wei Liu @ 2025-03-07 19:55 UTC (permalink / raw)
To: qemu-devel
Cc: wei.liu, dirty, rbolshakov, phil, jinankjain, liuwe, muislam,
ziqiaozhou, mukeshrathor, magnuskulke, prapal, jpiotrowski, deviv,
Wei Liu
Add myself as a reviewer.
Signed-off-by: Wei Liu <liuwe@linux.microsoft.com>
---
v2: new
---
MAINTAINERS | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/MAINTAINERS b/MAINTAINERS
index 5df6020ed545..02c763e9d14f 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -533,6 +533,14 @@ S: Supported
F: target/i386/whpx/
F: include/system/whpx.h
+X86 Instruction Emulator
+M: Cameron Esfahani <dirty@apple.com>
+M: Roman Bolshakov <rbolshakov@ddn.com>
+R: Phil Dennis-Jordan <phil@philjordan.eu>
+R: Wei Liu <wei.liu@kernel.org>
+S: Maintained
+F: target/i386/emulate/
+
Guest CPU Cores (Xen)
---------------------
X86 Xen CPUs
--
2.47.2
^ permalink raw reply related [flat|nested] 16+ messages in thread* Re: [PATCH v2 00/14] Factor out HVF's instruction emulator
2025-03-07 19:55 [PATCH v2 00/14] Factor out HVF's instruction emulator Wei Liu
` (13 preceding siblings ...)
2025-03-07 19:55 ` [PATCH v2 14/14] MAINTAINERS: add an entry for the x86 instruction emulator Wei Liu
@ 2025-03-14 19:01 ` Wei Liu
14 siblings, 0 replies; 16+ messages in thread
From: Wei Liu @ 2025-03-14 19:01 UTC (permalink / raw)
To: qemu-devel
Cc: wei.liu, dirty, rbolshakov, phil, jinankjain, liuwe, muislam,
ziqiaozhou, mukeshrathor, magnuskulke, prapal, jpiotrowski, deviv,
Wei Liu
On Fri, Mar 07, 2025 at 11:55:11AM -0800, Wei Liu wrote:
> 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
HVF maintainers, Ping?
Thanks,
Wei.
^ permalink raw reply [flat|nested] 16+ messages in thread