Skip to contentSkip to conversation

x86/emul: Emulate %cr8 accesses

PATCH In review Andrew Cooper 2 participants 2 messages Mar 17, 2025 – Mar 18, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/emul: Emulate %cr8 accesses

Petr reports:

(XEN) MMIO emulation failed (1): d12v1 64bit @ 0010:fffff8057ba7dfbf -> 45 0f 20 c2 ...

during introspection.

This is MOV %cr8, which is wired up for hvm_mov_{to,from}_cr(); the VMExit fastpaths, but not for the full emulation slowpaths. Wire %cr8 up in hvmemul_{read,write}_cr() too.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Petr Beneš <[email protected]>

Like the fastpaths, this depends on all HVM/PVH guests strictly getting an LAPIC, which is guaranteed by XSA-256. There's no such thing as a 64bit CPU without a Local APIC, so no such thing as %cr8 not being TPR.

---
 xen/arch/x86/hvm/emulate.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
xen/arch/x86/hvm/emulate.c +10 -0
index dbf6b5543adf..852240b29d74 100644
@@ -2285,6 +2285,11 @@ static int cf_check hvmemul_read_cr(
         *val = current->arch.hvm.guest_cr[reg];
         TRACE(TRC_HVM_CR_READ64, reg, *val, *val >> 32);
         return X86EMUL_OKAY;
+
+    case 8:
+        *val = (vlapic_get_reg(vcpu_vlapic(current), APIC_TASKPRI) & 0xf0) >> 4;
+        return X86EMUL_OKAY;
+
     default:
         break;
     }
@@ -2325,6 +2330,11 @@ static int cf_check hvmemul_write_cr(
         rc = hvm_set_cr4(val, true);
         break;
 
+    case 8:
+        vlapic_set_reg(vcpu_vlapic(current), APIC_TASKPRI, ((val & 0x0f) << 4));
+        rc = X86EMUL_OKAY;
+        break;
+
     default:
         rc = X86EMUL_UNHANDLEABLE;
         break;

base-commit: e7e0d485e993e97b1c816adcfc610e7c7258ce96

Signature · 1 line
2.39.5
#1 lore raw
Quoted 4 lines · Andrew Cooper

Petr reports:

(XEN) MMIO emulation failed (1): d12v1 64bit @ 0010:fffff8057ba7dfbf -> 45 0f 20 c2 ...

Quoted 8 lines

during introspection.

This is MOV %cr8, which is wired up for hvm_mov_{to,from}_cr(); the VMExit fastpaths, but not for the full emulation slowpaths. Wire %cr8 up in hvmemul_{read,write}_cr() too.

Like in a few other cases I guess there's no good Fixes: tag to use, as the omission must have been there basically forever.

xen/arch/x86/hvm/emulate.c static int cf_check hvmemul_read_cr( · line 2290

Quoted 9 lines
xen/arch/x86/hvm/emulate.c +3 -0
@@ -2285,6 +2285,11 @@ static int cf_check hvmemul_read_cr(
         *val = current->arch.hvm.guest_cr[reg];
         TRACE(TRC_HVM_CR_READ64, reg, *val, *val >> 32);
         return X86EMUL_OKAY;
+
+    case 8:
+        *val = (vlapic_get_reg(vcpu_vlapic(current), APIC_TASKPRI) & 0xf0) >> 4;

I think it would be nice to add a #define to apicdef.h that could be utilized to use MASK_EXTR() here (and MASK_INSR() below). Otherwise even without such a #define I'd like to ask that we use the two macros here.

xen/arch/x86/hvm/emulate.c static int cf_check hvmemul_write_cr( · line 2336

Quoted 13 lines
xen/arch/x86/hvm/emulate.c +6 -0
+        return X86EMUL_OKAY;
+
     default:
         break;
     }
@@ -2325,6 +2330,11 @@ static int cf_check hvmemul_write_cr(
         rc = hvm_set_cr4(val, true);
         break;
 
+    case 8:
+        vlapic_set_reg(vcpu_vlapic(current), APIC_TASKPRI, ((val & 0x0f) << 4));
+        rc = X86EMUL_OKAY;
+        break;

Both SDM and PM say #GP upon writes to reserved bits, without there being a place where it is said which bits are reserved. Don't we therefore need to treat bits 4 and up as must-be-zero, implying that all bits which have no meaning are reserved?

Jan