@samitouri / QOSamiQemu / commits / 9fdece1224

whpx: move whpx_get_reg/whpx_set_reg to generic code

These will be used in the next commit on the x86_64 backend too. Also move flush_cpu_state as it's used by get_reg/set_reg and the arm64 code. Signed-off-by: Mohamed Mediouni <mohamed@unpredictable.fr> Reviewed-by: Bernhard Beschow <shentey@gmail.com> Link: https://lore.kernel.org/r/20260223233950.96076-10-mohamed@unpredictable.fr Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

Mohamed Mediouni committed Feb 24, 2026 at 00:39 UTC 9fdece1224e9d2348c4f25cf0fb89a907fe35734
3 files changed +39 -36
accel/whpx/whpx-common.c
+35
@@ -46,6 +46,41 @@ static HMODULE hWinHvEmulation;
46 struct whpx_state whpx_global;
47 struct WHPDispatch whp_dispatch;
48
49 +void whpx_flush_cpu_state(CPUState *cpu)
50 +{
51 + if (cpu->vcpu_dirty) {
52 + whpx_set_registers(cpu, WHPX_SET_RUNTIME_STATE);
53 + cpu->vcpu_dirty = false;
54 + }
55 +}
56 +
57 +void whpx_get_reg(CPUState *cpu, WHV_REGISTER_NAME reg, WHV_REGISTER_VALUE* val)
58 +{
59 + struct whpx_state *whpx = &whpx_global;
60 + HRESULT hr;
61 +
62 + whpx_flush_cpu_state(cpu);
63 +
64 + hr = whp_dispatch.WHvGetVirtualProcessorRegisters(whpx->partition, cpu->cpu_index,
65 + &reg, 1, val);
66 +
67 + if (FAILED(hr)) {
68 + error_report("WHPX: Failed to get register %08x, hr=%08lx", reg, hr);
69 + }
70 +}
71 +
72 +void whpx_set_reg(CPUState *cpu, WHV_REGISTER_NAME reg, WHV_REGISTER_VALUE val)
73 +{
74 + struct whpx_state *whpx = &whpx_global;
75 + HRESULT hr;
76 + hr = whp_dispatch.WHvSetVirtualProcessorRegisters(whpx->partition, cpu->cpu_index,
77 + &reg, 1, &val);
78 +
79 + if (FAILED(hr)) {
80 + error_report("WHPX: Failed to set register %08x, hr=%08lx", reg, hr);
81 + }
82 +}
83 +
84 /* Tries to find a breakpoint at the specified address. */
85 struct whpx_breakpoint *whpx_lookup_breakpoint_by_addr(uint64_t address)
86 {
include/system/whpx-common.h
+3
@@ -20,6 +20,9 @@ int whpx_first_vcpu_starting(CPUState *cpu);
20 int whpx_last_vcpu_stopping(CPUState *cpu);
21 void whpx_memory_init(void);
22 struct whpx_breakpoint *whpx_lookup_breakpoint_by_addr(uint64_t address);
23 +void whpx_flush_cpu_state(CPUState *cpu);
24 +void whpx_get_reg(CPUState *cpu, WHV_REGISTER_NAME reg, WHV_REGISTER_VALUE* val);
25 +void whpx_set_reg(CPUState *cpu, WHV_REGISTER_NAME reg, WHV_REGISTER_VALUE val);
26
27 /* On x64: same as WHvX64ExceptionTypeDebugTrapOrFault */
28 #define WHPX_INTERCEPT_DEBUG_TRAPS 1
target/arm/whpx/whpx-all.c
+1 -36
@@ -273,14 +273,6 @@ static struct whpx_sreg_match whpx_sreg_match[] = {
273 { WHvArm64RegisterSpEl1, ENCODE_AA64_CP_REG(4, 1, 3, 4, 0) },
274 };
275
276 -static void flush_cpu_state(CPUState *cpu)
277 -{
278 - if (cpu->vcpu_dirty) {
279 - whpx_set_registers(cpu, WHPX_SET_RUNTIME_STATE);
280 - cpu->vcpu_dirty = false;
281 - }
282 -}
283 -
276 HRESULT whpx_set_exception_exit_bitmap(UINT64 exceptions)
277 {
278 if (exceptions != 0) {
@@ -313,33 +305,6 @@ void whpx_arch_destroy_vcpu(CPUState *cpu)
305 /* currently empty on Arm */
306 }
307
316 -static void whpx_get_reg(CPUState *cpu, WHV_REGISTER_NAME reg, WHV_REGISTER_VALUE* val)
317 -{
318 - struct whpx_state *whpx = &whpx_global;
319 - HRESULT hr;
320 -
321 - flush_cpu_state(cpu);
322 -
323 - hr = whp_dispatch.WHvGetVirtualProcessorRegisters(whpx->partition, cpu->cpu_index,
324 - &reg, 1, val);
325 -
326 - if (FAILED(hr)) {
327 - error_report("WHPX: Failed to get register %08x, hr=%08lx", reg, hr);
328 - }
329 -}
330 -
331 -static void whpx_set_reg(CPUState *cpu, WHV_REGISTER_NAME reg, WHV_REGISTER_VALUE val)
332 -{
333 - struct whpx_state *whpx = &whpx_global;
334 - HRESULT hr;
335 - hr = whp_dispatch.WHvSetVirtualProcessorRegisters(whpx->partition, cpu->cpu_index,
336 - &reg, 1, &val);
337 -
338 - if (FAILED(hr)) {
339 - error_report("WHPX: Failed to set register %08x, hr=%08lx", reg, hr);
340 - }
341 -}
342 -
308 static void whpx_get_global_reg(WHV_REGISTER_NAME reg, WHV_REGISTER_VALUE *val)
309 {
310 struct whpx_state *whpx = &whpx_global;
@@ -526,7 +491,7 @@ int whpx_vcpu_run(CPUState *cpu)
491 if (advance_pc) {
492 WHV_REGISTER_VALUE pc;
493
529 - flush_cpu_state(cpu);
494 + whpx_flush_cpu_state(cpu);
495 pc.Reg64 = vcpu->exit_ctx.MemoryAccess.Header.Pc + 4;
496 whpx_set_reg(cpu, WHvArm64RegisterPc, pc);
497 }