git: dbb4487124af - main - bhyve: Preserve upper RAX bits for byte and word input

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

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

commit dbb4487124afb2fff5ba81176c5a8a224f8fa627
Author:     Hayzam Sherif <hayzam@FreeBSD.org>
AuthorDate: 2026-11-27 07:17:06 +0000
Commit:     Hayzam Sherif <hayzam@FreeBSD.org>
CommitDate: 2026-11-27 07:17:06 +0000

    bhyve: Preserve upper RAX bits for byte and word input
    
    Scalar IN merges the input into a 32-bit EAX temporary and writes that
    value to RAX.  This clears bits 63:32 for byte and word inputs, which
    must preserve the bits outside AL or AX.
    
    Use vie_update_register() to update the accumulator at the operand
    width.  This preserves the remaining bits for byte and word inputs and
    retains zero-extension for dword inputs.
    
    Reviewed by:    markj
    Approved by:    markj
    Sponsored by:   Alchemilla Ventures Private Limited
    Differential Revision:  https://reviews.freebsd.org/D61498
---
 sys/amd64/vmm/vmm_ioport.c   | 6 ++----
 usr.sbin/bhyve/amd64/inout.c | 6 ++----
 2 files changed, 4 insertions(+), 8 deletions(-)

diff --git a/sys/amd64/vmm/vmm_ioport.c b/sys/amd64/vmm/vmm_ioport.c
index 65710c8de717..60f3f1860e9d 100644
--- a/sys/amd64/vmm/vmm_ioport.c
+++ b/sys/amd64/vmm/vmm_ioport.c
@@ -134,10 +134,8 @@ emulate_inout_port(struct vcpu *vcpu, struct vm_exit *vmexit, bool *retu)
 	}
 
 	if (vmexit->u.inout.in) {
-		vmexit->u.inout.eax &= ~mask;
-		vmexit->u.inout.eax |= val & mask;
-		error = vm_set_register(vcpu, VM_REG_GUEST_RAX,
-		    vmexit->u.inout.eax);
+		error = vie_update_register(vcpu, VM_REG_GUEST_RAX,
+		    val, vmexit->u.inout.bytes);
 		KASSERT(error == 0, ("emulate_ioport: error %d setting guest "
 		    "rax register", error));
 	}
diff --git a/usr.sbin/bhyve/amd64/inout.c b/usr.sbin/bhyve/amd64/inout.c
index 64f36093ba91..6cc90c36156a 100644
--- a/usr.sbin/bhyve/amd64/inout.c
+++ b/usr.sbin/bhyve/amd64/inout.c
@@ -227,10 +227,8 @@ emulate_inout(struct vmctx *ctx, struct vcpu *vcpu, struct vm_exit *vmexit)
 		val = eax & vie_size2mask(bytes);
 		retval = handler(ctx, in, port, bytes, &val, arg);
 		if (retval == 0 && in) {
-			eax &= ~vie_size2mask(bytes);
-			eax |= val & vie_size2mask(bytes);
-			error = vm_set_register(vcpu, VM_REG_GUEST_RAX,
-			    eax);
+			error = vie_update_register(vcpu, VM_REG_GUEST_RAX,
+			    val, bytes);
 			if (error != 0)
 				vm_check_destroyed(vcpu);
 			assert(error == 0);