@samitouri / QOSamiQemu / commits / 7e44b76644

hvf: i386: save/restore CR0/2/3

For symmetry, save/restore the same set of registers even when not needed. CR2 save/restore needed as page faults injected to the guest imply modifying CR2. Signed-off-by: Mohamed Mediouni <mohamed@unpredictable.fr> Link: https://lore.kernel.org/r/20260223233950.96076-21-mohamed@unpredictable.fr Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

Mohamed Mediouni committed Feb 24, 2026 at 00:39 UTC 7e44b76644273065003abd7d87547c38a5dd64e3
1 file changed +15
target/i386/hvf/hvf.c
+15
@@ -477,7 +477,19 @@ static void hvf_load_crs(CPUState *cs)
477
478 env->cr[0] = rvmcs(cpu->accel->fd, VMCS_GUEST_CR0);
479 env->cr[3] = rvmcs(cpu->accel->fd, VMCS_GUEST_CR3);
480 + env->cr[2] = rreg(cpu->accel->fd, HV_X86_CR2);
481 }
482 +
483 +static void hvf_save_crs(CPUState *cs)
484 +{
485 + X86CPU *x86_cpu = X86_CPU(cpu);
486 + CPUX86State *env = &x86_cpu->env;
487 +
488 + wvmcs(cpu->accel->fd, VMCS_GUEST_CR0, env->cr[0]);
489 + wvmcs(cpu->accel->fd, VMCS_GUEST_CR3, env->cr[3]);
490 + wreg(cs->accel->fd, HV_X86_CR2, env->cr[2]);
491 +}
492 +
493 void hvf_load_regs(CPUState *cs)
494 {
495 X86CPU *cpu = X86_CPU(cs);
@@ -794,6 +806,7 @@ static int hvf_handle_vmexit(CPUState *cpu)
806 decode_instruction(env, &decode);
807 exec_instruction(env, &decode);
808 hvf_store_regs(cpu);
809 + hvf_save_crs(cpu);
810 break;
811 }
812 break;
@@ -837,6 +850,7 @@ static int hvf_handle_vmexit(CPUState *cpu)
850 assert(ins_len == decode.len);
851 exec_instruction(env, &decode);
852 hvf_store_regs(cpu);
853 + hvf_save_crs(cpu);
854
855 break;
856 }
@@ -942,6 +956,7 @@ static int hvf_handle_vmexit(CPUState *cpu)
956 decode_instruction(env, &decode);
957 exec_instruction(env, &decode);
958 hvf_store_regs(cpu);
959 + hvf_save_crs(cpu);
960 break;
961 }
962 case EXIT_REASON_TPR: {