git: 8e7f9aa97b3e - main - vmm: Emulate the byte forms of OR and AND with an immediate

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Sun, 08 Nov 2026 16:18:31 UTC
The branch main has been updated by markj:

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

commit 8e7f9aa97b3e886afc2c445bb51c4d781a106a1e
Author:     Wanpeng Qian <wanpengqian@gmail.com>
AuthorDate: 2026-11-08 16:05:04 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2026-11-08 16:18:14 +0000

    vmm: Emulate the byte forms of OR and AND with an immediate
    
    Opcode 0x80 (group 1: r/m8, imm8) is in the decode table and
    emulate_cmp() handles 80 /7, but emulate_or() and emulate_and() only
    accept 0x81 and 0x83.  An "or byte ptr [mem], imm8" or "and byte ptr
    [mem], imm8" on emulated memory therefore fails with EINVAL and bhyve
    stops the guest with "Failed to emulate instruction sequence
    [ 80 ... ]".  Seen with macOS: VoodooHDA starts the HDA controller's
    CORB with "or byte [rax+0x4c], 2" (CORBCTL |= CORBRUN), and the guest
    died at the first sound.
    
    Accept 0x80 in both, with a one byte operand as emulate_cmp() does.
    The file is shared with bhyve(8), so the fix covers the in-kernel
    devices (HPET, local APIC without APICv, I/O APIC) and the user space
    device models alike.
    
    Signed-off-by: Wanpeng Qian <wanpengqian@gmail.com>
    
    Reviewed by:    markj
    MFC after:      1 week
    Sponsored by:   keelos.dev
    Differential Revision:  https://reviews.freebsd.org/D60997
---
 sys/amd64/vmm/vmm_instruction_emul.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/sys/amd64/vmm/vmm_instruction_emul.c b/sys/amd64/vmm/vmm_instruction_emul.c
index 963da132c36b..b588b3618764 100644
--- a/sys/amd64/vmm/vmm_instruction_emul.c
+++ b/sys/amd64/vmm/vmm_instruction_emul.c
@@ -1040,12 +1040,16 @@ emulate_and(struct vcpu *vcpu, uint64_t gpa, struct vie *vie,
 		result = val1 & val2;
 		error = vie_update_register(vcpu, reg, result, size);
 		break;
+	case 0x80:
 	case 0x81:
 	case 0x83:
 		/*
 		 * AND mem (ModRM:r/m) with immediate and store the
 		 * result in mem.
 		 *
+		 * 80 /4		and r/m8, imm8
+		 * REX + 80 /4		and r/m8, imm8
+		 *
 		 * 81 /4		and r/m16, imm16
 		 * 81 /4		and r/m32, imm32
 		 * REX.W + 81 /4	and r/m64, imm32 sign-extended to 64
@@ -1054,6 +1058,8 @@ emulate_and(struct vcpu *vcpu, uint64_t gpa, struct vie *vie,
 		 * 83 /4		and r/m32, imm8 sign-extended to 32
 		 * REX.W + 83/4		and r/m64, imm8 sign-extended to 64
 		 */
+		if (vie->op.op_byte == 0x80)
+			size = 1;
 
 		/* get the first operand */
                 error = memread(vcpu, gpa, &val1, size, arg);
@@ -1128,12 +1134,16 @@ emulate_or(struct vcpu *vcpu, uint64_t gpa, struct vie *vie,
 		result = val1 | val2;
 		error = vie_update_register(vcpu, reg, result, size);
 		break;
+	case 0x80:
 	case 0x81:
 	case 0x83:
 		/*
 		 * OR mem (ModRM:r/m) with immediate and store the
 		 * result in mem.
 		 *
+		 * 80 /1		or r/m8, imm8
+		 * REX + 80 /1		or r/m8, imm8
+		 *
 		 * 81 /1		or r/m16, imm16
 		 * 81 /1		or r/m32, imm32
 		 * REX.W + 81 /1	or r/m64, imm32 sign-extended to 64
@@ -1142,6 +1152,8 @@ emulate_or(struct vcpu *vcpu, uint64_t gpa, struct vie *vie,
 		 * 83 /1		or r/m32, imm8 sign-extended to 32
 		 * REX.W + 83/1		or r/m64, imm8 sign-extended to 64
 		 */
+		if (vie->op.op_byte == 0x80)
+			size = 1;
 
 		/* get the first operand */
                 error = memread(vcpu, gpa, &val1, size, arg);