git: 3a5512da84c0 - main - amd64/vmm: Restore vmxctx registers through vmxctx_setreg()

From: Hayzam Sherif <hayzam_at_FreeBSD.org>
Date: Fri, 27 Nov 2026 08:03:58 UTC
The branch main has been updated by hayzam:

URL: https://cgit.FreeBSD.org/src/commit/?id=3a5512da84c037f4bdc2a45ce94d0be702f113bc

commit 3a5512da84c037f4bdc2a45ce94d0be702f113bc
Author:     Hayzam Sherif <hayzam@FreeBSD.org>
AuthorDate: 2026-11-27 07:57:13 +0000
Commit:     Hayzam Sherif <hayzam@FreeBSD.org>
CommitDate: 2026-11-27 07:57:13 +0000

    amd64/vmm: Restore vmxctx registers through vmxctx_setreg()
    
    vmx_vcpu_snapshot() copies the guest registers kept in the vCPU context
    straight between the context and the snapshot buffer, while
    VM_SET_REGISTER stores them through vmxctx_setreg().  A check added to
    vmxctx_setreg() would thus not apply to values restored from a
    snapshot.
    
    Add vmxctx_snapshot_reg(), modelled on vmcs_snapshot_reg(), and use it
    for every register in the vCPU context, so that both paths store values
    the same way.  The snapshot format does not change.
    
    No functional change intended.
    
    Reviewed by:    markj
    Approved by:    markj
    Sponsored by:   Alchemilla Ventures Private Limited
    Differential Revision:  https://reviews.freebsd.org/D61953
---
 sys/amd64/vmm/intel/vmx.c | 70 ++++++++++++++++++++++++++++++-----------------
 1 file changed, 45 insertions(+), 25 deletions(-)

diff --git a/sys/amd64/vmm/intel/vmx.c b/sys/amd64/vmm/intel/vmx.c
index b3d016e7b967..c16e0ae33fb9 100644
--- a/sys/amd64/vmm/intel/vmx.c
+++ b/sys/amd64/vmm/intel/vmx.c
@@ -4165,6 +4165,40 @@ vmx_vlapic_cleanup(struct vlapic *vlapic)
 }
 
 #ifdef BHYVE_SNAPSHOT
+/* Registers kept in the vCPU context, in the order they are snapshotted. */
+static const int vmxctx_snapshot_regs[] = {
+	VM_REG_GUEST_RDI, VM_REG_GUEST_RSI, VM_REG_GUEST_RDX, VM_REG_GUEST_RCX,
+	VM_REG_GUEST_R8, VM_REG_GUEST_R9, VM_REG_GUEST_RAX, VM_REG_GUEST_RBX,
+	VM_REG_GUEST_RBP, VM_REG_GUEST_R10, VM_REG_GUEST_R11, VM_REG_GUEST_R12,
+	VM_REG_GUEST_R13, VM_REG_GUEST_R14, VM_REG_GUEST_R15, VM_REG_GUEST_CR2,
+	VM_REG_GUEST_DR0, VM_REG_GUEST_DR1, VM_REG_GUEST_DR2, VM_REG_GUEST_DR3,
+	VM_REG_GUEST_DR6,
+};
+
+static int
+vmxctx_snapshot_reg(struct vmxctx *vmxctx, int reg,
+    struct vm_snapshot_meta *meta)
+{
+	uint64_t val;
+	int ret;
+
+	if (meta->op == VM_SNAPSHOT_SAVE) {
+		ret = vmxctx_getreg(vmxctx, reg, &val);
+		if (ret != 0)
+			goto done;
+
+		SNAPSHOT_VAR_OR_LEAVE(val, meta, ret, done);
+	} else if (meta->op == VM_SNAPSHOT_RESTORE) {
+		SNAPSHOT_VAR_OR_LEAVE(val, meta, ret, done);
+
+		ret = vmxctx_setreg(vmxctx, reg, val);
+	} else
+		ret = EINVAL;
+
+done:
+	return (ret);
+}
+
 static int
 vmx_vcpu_snapshot(void *vcpui, struct vm_snapshot_meta *meta)
 {
@@ -4172,9 +4206,9 @@ vmx_vcpu_snapshot(void *vcpui, struct vm_snapshot_meta *meta)
 	struct vmx *vmx;
 	struct vmx_vcpu *vcpu;
 	struct vmxctx *vmxctx;
-	uint64_t dr6;
 	uint64_t guest_msrs[GUEST_MSR_NUM];
 	int err, run, hostcpu;
+	u_int i;
 
 	err = 0;
 	vcpu = vcpui;
@@ -4258,30 +4292,16 @@ vmx_vcpu_snapshot(void *vcpui, struct vm_snapshot_meta *meta)
 	    sizeof(vcpu->mtrr), meta, err, done);
 
 	vmxctx = &vcpu->ctx;
-	SNAPSHOT_VAR_OR_LEAVE(vmxctx->guest_rdi, meta, err, done);
-	SNAPSHOT_VAR_OR_LEAVE(vmxctx->guest_rsi, meta, err, done);
-	SNAPSHOT_VAR_OR_LEAVE(vmxctx->guest_rdx, meta, err, done);
-	SNAPSHOT_VAR_OR_LEAVE(vmxctx->guest_rcx, meta, err, done);
-	SNAPSHOT_VAR_OR_LEAVE(vmxctx->guest_r8, meta, err, done);
-	SNAPSHOT_VAR_OR_LEAVE(vmxctx->guest_r9, meta, err, done);
-	SNAPSHOT_VAR_OR_LEAVE(vmxctx->guest_rax, meta, err, done);
-	SNAPSHOT_VAR_OR_LEAVE(vmxctx->guest_rbx, meta, err, done);
-	SNAPSHOT_VAR_OR_LEAVE(vmxctx->guest_rbp, meta, err, done);
-	SNAPSHOT_VAR_OR_LEAVE(vmxctx->guest_r10, meta, err, done);
-	SNAPSHOT_VAR_OR_LEAVE(vmxctx->guest_r11, meta, err, done);
-	SNAPSHOT_VAR_OR_LEAVE(vmxctx->guest_r12, meta, err, done);
-	SNAPSHOT_VAR_OR_LEAVE(vmxctx->guest_r13, meta, err, done);
-	SNAPSHOT_VAR_OR_LEAVE(vmxctx->guest_r14, meta, err, done);
-	SNAPSHOT_VAR_OR_LEAVE(vmxctx->guest_r15, meta, err, done);
-	SNAPSHOT_VAR_OR_LEAVE(vmxctx->guest_cr2, meta, err, done);
-	SNAPSHOT_VAR_OR_LEAVE(vmxctx->guest_dr0, meta, err, done);
-	SNAPSHOT_VAR_OR_LEAVE(vmxctx->guest_dr1, meta, err, done);
-	SNAPSHOT_VAR_OR_LEAVE(vmxctx->guest_dr2, meta, err, done);
-	SNAPSHOT_VAR_OR_LEAVE(vmxctx->guest_dr3, meta, err, done);
-	dr6 = vmxctx->guest_dr6;
-	SNAPSHOT_VAR_OR_LEAVE(dr6, meta, err, done);
-	if (meta->op == VM_SNAPSHOT_RESTORE)
-		err = vmxctx_setreg(vmxctx, VM_REG_GUEST_DR6, dr6);
+	/*
+	 * Go through vmxctx_setreg() on restore, so that the registers are
+	 * stored the same way as by VM_SET_REGISTER.
+	 */
+	for (i = 0; i < nitems(vmxctx_snapshot_regs); i++) {
+		err = vmxctx_snapshot_reg(vmxctx, vmxctx_snapshot_regs[i],
+		    meta);
+		if (err != 0)
+			goto done;
+	}
 
 done:
 	return (err);