Skip to contentSkip to conversation

x86/HVM: fix various aspects of APIC emulation

PATCH Superseded Jan Beulich 2 participants 15 messages Sep 18, 2014 – Sep 24, 2014

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 cover x86/HVM: fix various aspects of APIC emulation

1: fix miscellaneous aspects of x2APIC emulation
2: fix ID handling of x2APIC emulation
3: a few type adjustments
4: don't silently accept bad vectors

---
v4: 2nd patch partly re-done (see patch for details), previously
separate 4th patch now integrated.
v3: 1st patch extended (see patch for details), new 3rd patch.
v2: Monolithic patch broken up. Generate #GP on invalid APIC base
MSR transitions.Correct ID and LDR after domain restore (if necessary); as stated previously the only compatibility problem this creates is when migrating a VM _to_ an unfixed (i.e. old) hypervisor, a scenario which supposedly isn't supported.

#1 lore raw

PATCH v4 1/4 x86/HVM: fix miscellaneous aspects of x2APIC emulation

- generate #GP on invalid APIC base MSR transitions
- fail reads from the EOI and self-IPI registers (which are write-only)
- handle self-IPI writes and the ICR2 half of ICR writes largely in hvm_x2apic_msr_write() and (for self-IPI only) vlapic_apicv_write()
- don't permit MMIO-based access in x2APIC mode
- filter writes to read-only registers in hvm_x2apic_msr_write(), allowing conditionals to be dropped from vlapic_reg_write()
- don't ignore upper half of MSR-based write to ESR being non-zero
- VMX's EXIT_REASON_APIC_WRITE must not result in #GP (this exit being trap-like, this exception would get raised on the wrong RIP)
- make hvm_x2apic_msr_read() produce X86EMUL_* return codes just like hvm_x2apic_msr_write() does (benign to the only caller)

---
v3: Also handle APIC_EOI in hvm_x2apic_msr_read() as pointed out by
Andrew. Filter MMIO-based accesses in vlapic_range() when in x2APIC mode. Move x2APIC special casing from vlapic_reg_write() to hvm_x2apic_msr_write(). Don't open-code vlapic_x2apic_mode().
v2: Split from main patch.

xen/arch/x86/hvm/hvm.c +2 -1
@@ -4499,7 +4499,8 @@ int hvm_msr_write_intercept(unsigned int
         break;
 
     case MSR_IA32_APICBASE:
-        vlapic_msr_set(vcpu_vlapic(v), msr_content);
+        if ( !vlapic_msr_set(vcpu_vlapic(v), msr_content) )
+            goto gp_fault;
         break;
 
     case MSR_IA32_TSC_DEADLINE:
xen/arch/x86/hvm/vlapic.c +61 -43

200 lines not on this page · raw message

xen/arch/x86/hvm/vmx/vmx.c +1 -2
@@ -3095,8 +3095,7 @@ void vmx_vmexit_handler(struct cpu_user_
         break;
 
     case EXIT_REASON_APIC_WRITE:
-        if ( vmx_handle_apic_write() )
-            hvm_inject_hw_exception(TRAP_gp_fault, 0);
+        vmx_handle_apic_write();
         break;
 
     case EXIT_REASON_ACCESS_GDTR_OR_IDTR:
xen/include/asm-x86/hvm/vlapic.h +1 -1
@@ -106,7 +106,7 @@ void vlapic_destroy(struct vcpu *v);
 
 void vlapic_reset(struct vlapic *vlapic);
 
-void vlapic_msr_set(struct vlapic *vlapic, uint64_t value);
+bool_t vlapic_msr_set(struct vlapic *vlapic, uint64_t value);
 void vlapic_tdt_msr_set(struct vlapic *vlapic, uint64_t value);
 uint64_t vlapic_tdt_msr_get(struct vlapic *vlapic);
 

x86/HVM: fix miscellaneous aspects of x2APIC emulation

- generate #GP on invalid APIC base MSR transitions
- fail reads from the EOI and self-IPI registers (which are write-only)
- handle self-IPI writes and the ICR2 half of ICR writes largely in hvm_x2apic_msr_write() and (for self-IPI only) vlapic_apicv_write()
- don't permit MMIO-based access in x2APIC mode
- filter writes to read-only registers in hvm_x2apic_msr_write(), allowing conditionals to be dropped from vlapic_reg_write()
- don't ignore upper half of MSR-based write to ESR being non-zero
- VMX's EXIT_REASON_APIC_WRITE must not result in #GP (this exit being trap-like, this exception would get raised on the wrong RIP)
- make hvm_x2apic_msr_read() produce X86EMUL_* return codes just like hvm_x2apic_msr_write() does (benign to the only caller)

---
v3: Also handle APIC_EOI in hvm_x2apic_msr_read() as pointed out by
Andrew. Filter MMIO-based accesses in vlapic_range() when in x2APIC mode. Move x2APIC special casing from vlapic_reg_write() to hvm_x2apic_msr_write(). Don't open-code vlapic_x2apic_mode().
v2: Split from main patch.

xen/arch/x86/hvm/hvm.c +2 -1
@@ -4499,7 +4499,8 @@ int hvm_msr_write_intercept(unsigned int
         break;
 
     case MSR_IA32_APICBASE:
-        vlapic_msr_set(vcpu_vlapic(v), msr_content);
+        if ( !vlapic_msr_set(vcpu_vlapic(v), msr_content) )
+            goto gp_fault;
         break;
 
     case MSR_IA32_TSC_DEADLINE:
xen/arch/x86/hvm/vlapic.c +61 -43

200 lines not on this page · raw message

xen/arch/x86/hvm/vmx/vmx.c +1 -2
@@ -3095,8 +3095,7 @@ void vmx_vmexit_handler(struct cpu_user_
         break;
 
     case EXIT_REASON_APIC_WRITE:
-        if ( vmx_handle_apic_write() )
-            hvm_inject_hw_exception(TRAP_gp_fault, 0);
+        vmx_handle_apic_write();
         break;
 
     case EXIT_REASON_ACCESS_GDTR_OR_IDTR:
xen/include/asm-x86/hvm/vlapic.h +1 -1
@@ -106,7 +106,7 @@ void vlapic_destroy(struct vcpu *v);
 
 void vlapic_reset(struct vlapic *vlapic);
 
-void vlapic_msr_set(struct vlapic *vlapic, uint64_t value);
+bool_t vlapic_msr_set(struct vlapic *vlapic, uint64_t value);
 void vlapic_tdt_msr_set(struct vlapic *vlapic, uint64_t value);
 uint64_t vlapic_tdt_msr_get(struct vlapic *vlapic);
 

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#2 lore raw

Re: [PATCH v4 1/4] x86/HVM: fix miscellaneous aspects of x2APIC emulation

Quoted 4 lines · Jan Beulich

int hvm_x2apic_msr_write(struct vcpu *v, unsigned int msr, uint64_t msr_content)

xen/arch/x86/hvm/vlapic.c +0 -0
@@ -891,16 +876,33 @@ int hvm_x2apic_msr_write(struct vcpu *v,
 
     switch ( offset )

xen/arch/x86/hvm/vlapic.c int hvm_x2apic_msr_write(struct vcpu *v, · line 886

Quoted 10 lines
xen/arch/x86/hvm/vlapic.c +8 -1
     {
-        int rc;
+    case APIC_TASKPRI:
+    case APIC_EOI:
+    case APIC_SPIV:
+    case APIC_CMCI:
+    case APIC_LVTT ... APIC_LVTERR:
+    case APIC_TMICT:
+    case APIC_TMCCT:
+    case APIC_TDCR:

Most (all?) of these MSRs have reserved bits, which should fail with a #GP(0) for attempts to set. vlapic_reg_read() masks most of the relevant bits, but doesn't appear to hit a misbehaving VM.

Quoted 11 lines
xen/arch/x86/hvm/vlapic.c +2 -5
+        break;
 
     case APIC_ICR:
-        rc = vlapic_reg_write(v, APIC_ICR2, (uint32_t)(msr_content >> 32));
-        if ( rc )
-            return rc;
+        vlapic_set_reg(vlapic, APIC_ICR2, msr_content >> 32);
         break;
 
-    case APIC_ICR2:
-        return X86EMUL_UNHANDLEABLE;

xen/arch/x86/hvm/vlapic.c int hvm_x2apic_msr_write(struct vcpu *v, · line 902

Quoted 10 lines
xen/arch/x86/hvm/vlapic.c +10 -0
+    case APIC_SELF_IPI:
+        offset = APIC_ICR;
+        msr_content = APIC_DEST_SELF | (uint8_t)msr_content;
+        break;
+
+    case APIC_ESR:
+        if ( msr_content )
+        {
+            printk(XENLOG_G_WARNING "%pv: non-zero (%lx) LAPIC ESR write\n",
+                   v, msr_content);

I know this is just moving an existing error message, but is it actually useful? ESR is no more special than the other registers with some/all bits reserved.

Quoted 7 lines
xen/arch/x86/hvm/vlapic.c +3 -0
+    default:
+            return X86EMUL_UNHANDLEABLE;
+        }
     }
 
     return vlapic_reg_write(v, offset, (uint32_t)msr_content);

xen/arch/x86/hvm/vlapic.c static int vlapic_range(struct vcpu *v, · line 918

Quoted 8 lines
xen/arch/x86/hvm/vlapic.c +4 -1
@@ -910,7 +912,10 @@ static int vlapic_range(struct vcpu *v, 
 {
     struct vlapic *vlapic = vcpu_vlapic(v);
     unsigned long offset  = addr - vlapic_base_address(vlapic);
-    return (!vlapic_hw_disabled(vlapic) && (offset < PAGE_SIZE));
+
+    return !vlapic_hw_disabled(vlapic) &&
+           !vlapic_x2apic_mode(vlapic) &&
+           (offset < PAGE_SIZE);

This check is too restrictive, at least on Intel. From SDM Vol 3 29.4.3.3

"As noted in Section 29.5, execution of WRMSR with ECX = 83FH (self-IPI MSR) can lead to an APIC-write VM exit
if the “virtual-interrupt delivery” VM-execution control is 1. The exit qualification for such an APIC-write VM exit is
3F0H."

So we can still end up wandering the vlapic MMIO codepaths even in x2apic mode.

~Andrew

#3 lore raw

Re: [PATCH v4 1/4] x86/HVM: fix miscellaneous aspects of x2APIC emulation

Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 22.09.14 at 15:14, <[email protected]> wrote:
Quoted 2 lines · Jan Beulich
(quoted diff) +0 -0
@@ -891,16 +876,33 @@ int hvm_x2apic_msr_write(struct vcpu *v,
 
Quoted 15 lines

xen/arch/x86/hvm/vlapic.c int hvm_x2apic_msr_write(struct vcpu *v, · line 886

Quoted 11 lines
xen/arch/x86/hvm/vlapic.c +8 -1
     switch ( offset )
     {
-        int rc;
+    case APIC_TASKPRI:
+    case APIC_EOI:
+    case APIC_SPIV:
+    case APIC_CMCI:
+    case APIC_LVTT ... APIC_LVTERR:
+    case APIC_TMICT:
+    case APIC_TMCCT:
+    case APIC_TDCR:

Most (all?) of these MSRs have reserved bits, which should fail with a #GP(0) for attempts to set. vlapic_reg_read() masks most of the relevant bits, but doesn't appear to hit a misbehaving VM.

Perhaps, but not in this patch.

Quoted 22 lines
Quoted 11 lines
xen/arch/x86/hvm/vlapic.c +2 -5
+        break;
 
     case APIC_ICR:
-        rc = vlapic_reg_write(v, APIC_ICR2, (uint32_t)(msr_content >> 32));
-        if ( rc )
-            return rc;
+        vlapic_set_reg(vlapic, APIC_ICR2, msr_content >> 32);
         break;
 
-    case APIC_ICR2:
-        return X86EMUL_UNHANDLEABLE;

xen/arch/x86/hvm/vlapic.c int hvm_x2apic_msr_write(struct vcpu *v, · line 902

Quoted 10 lines
xen/arch/x86/hvm/vlapic.c +10 -0
+    case APIC_SELF_IPI:
+        offset = APIC_ICR;
+        msr_content = APIC_DEST_SELF | (uint8_t)msr_content;
+        break;
+
+    case APIC_ESR:
+        if ( msr_content )
+        {
+            printk(XENLOG_G_WARNING "%pv: non-zero (%lx) LAPIC ESR write\n",
+                   v, msr_content);
Quoted 3 lines

I know this is just moving an existing error message, but is it actually useful? ESR is no more special than the other registers with some/all bits reserved.

It is different in that legacy APIC mode allows these to be written. But yes, I wondered about the usefulness of this message too.

Quoted 12 lines

xen/arch/x86/hvm/vlapic.c static int vlapic_range(struct vcpu *v, · line 918

Quoted 9 lines
xen/arch/x86/hvm/vlapic.c +4 -1
@@ -910,7 +912,10 @@ static int vlapic_range(struct vcpu *v, 
 {
     struct vlapic *vlapic = vcpu_vlapic(v);
     unsigned long offset  = addr - vlapic_base_address(vlapic);
-    return (!vlapic_hw_disabled(vlapic) && (offset < PAGE_SIZE));
+
+    return !vlapic_hw_disabled(vlapic) &&
+           !vlapic_x2apic_mode(vlapic) &&
+           (offset < PAGE_SIZE);

This check is too restrictive, at least on Intel. From SDM Vol 3 29.4.3.3

Quoted 8 lines

"As noted in Section 29.5, execution of WRMSR with ECX = 83FH (self-IPI MSR) can lead to an APIC-write VM exit
if the “virtual-interrupt delivery” VM-execution control is 1. The exit qualification for such an APIC-write VM exit is
3F0H."

So we can still end up wandering the vlapic MMIO codepaths even in x2apic mode.

Not exactly: These accesses would arrive at vlapic_apicv_write() (where APIC_SELF_IPI is being handled even for the x2APIC case) and hence won't go the normal MMIO path (including vlapic_range()).

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#4 lore raw

Re: [PATCH v4 1/4] x86/HVM: fix miscellaneous aspects of x2APIC emulation

Quoted 18 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 22.09.14 at 15:14, <[email protected]> wrote:
Quoted 2 lines · Jan Beulich
(quoted diff) +0 -0
@@ -891,16 +876,33 @@ int hvm_x2apic_msr_write(struct vcpu *v,
 
Quoted 14 lines

xen/arch/x86/hvm/vlapic.c int hvm_x2apic_msr_write(struct vcpu *v, · line 886

Quoted 11 lines
xen/arch/x86/hvm/vlapic.c +8 -1
     switch ( offset )
     {
-        int rc;
+    case APIC_TASKPRI:
+    case APIC_EOI:
+    case APIC_SPIV:
+    case APIC_CMCI:
+    case APIC_LVTT ... APIC_LVTERR:
+    case APIC_TMICT:
+    case APIC_TMCCT:
+    case APIC_TDCR:

Most (all?) of these MSRs have reserved bits, which should fail with a #GP(0) for attempts to set. vlapic_reg_read() masks most of the relevant bits, but doesn't appear to hit a misbehaving VM.

Quoted 1 line

Perhaps, but not in this patch.

Does it not count towards fixing misc aspects of x2APIC emulation?

Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
xen/arch/x86/hvm/vlapic.c +12 -5
+        break;
 
     case APIC_ICR:
-        rc = vlapic_reg_write(v, APIC_ICR2, (uint32_t)(msr_content >> 32));
-        if ( rc )
-            return rc;
+        vlapic_set_reg(vlapic, APIC_ICR2, msr_content >> 32);
         break;
 
-    case APIC_ICR2:
-        return X86EMUL_UNHANDLEABLE;
+    case APIC_SELF_IPI:
+        offset = APIC_ICR;
+        msr_content = APIC_DEST_SELF | (uint8_t)msr_content;
+        break;
+
+    case APIC_ESR:
+        if ( msr_content )
+        {
+            printk(XENLOG_G_WARNING "%pv: non-zero (%lx) LAPIC ESR write\n",
+                   v, msr_content);
Quoted 5 lines
Quoted 3 lines

I know this is just moving an existing error message, but is it actually useful? ESR is no more special than the other registers with some/all bits reserved.

It is different in that legacy APIC mode allows these to be written. But yes, I wondered about the usefulness of this message too.

Even for legacy APIC, the value written is discarded by hardware. I would just drop the message.

Quoted 18 lines
Quoted 18 lines

xen/arch/x86/hvm/vlapic.c static int vlapic_range(struct vcpu *v, · line 918

Quoted 10 lines
xen/arch/x86/hvm/vlapic.c +4 -1
@@ -910,7 +912,10 @@ static int vlapic_range(struct vcpu *v, 
 {
     struct vlapic *vlapic = vcpu_vlapic(v);
     unsigned long offset  = addr - vlapic_base_address(vlapic);
-    return (!vlapic_hw_disabled(vlapic) && (offset < PAGE_SIZE));
+
+    return !vlapic_hw_disabled(vlapic) &&
+           !vlapic_x2apic_mode(vlapic) &&
+           (offset < PAGE_SIZE);

This check is too restrictive, at least on Intel. From SDM Vol 3 29.4.3.3

"As noted in Section 29.5, execution of WRMSR with ECX = 83FH (self-IPI MSR) can lead to an APIC-write VM exit
if the “virtual-interrupt delivery” VM-execution control is 1. The exit qualification for such an APIC-write VM exit is
3F0H."

Quoted 5 lines
Quoted 2 lines

So we can still end up wandering the vlapic MMIO codepaths even in x2apic mode.

Not exactly: These accesses would arrive at vlapic_apicv_write() (where APIC_SELF_IPI is being handled even for the x2APIC case) and hence won't go the normal MMIO path (including vlapic_range()).

Ah yes, which bypasses the vlapic_range() test.

In which case, this change appears ok.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#5 lore raw

Re: [PATCH v4 1/4] x86/HVM: fix miscellaneous aspects of x2APIC emulation

Quoted 20 lines
Quoted 1 line
Quoted 1 line · On 23.09.14 at 18:56, <[email protected]> wrote:
Quoted 18 lines · Jan Beulich
Quoted 18 lines
Quoted 1 line
Quoted 1 line · On 22.09.14 at 15:14, <[email protected]> wrote:

xen/arch/x86/hvm/vlapic.c int hvm_x2apic_msr_write(struct vcpu *v, · line 886

Quoted 13 lines · Jan Beulich
xen/arch/x86/hvm/vlapic.c +8 -1
@@ -891,16 +876,33 @@ int hvm_x2apic_msr_write(struct vcpu *v,
 
     switch ( offset )
     {
-        int rc;
+    case APIC_TASKPRI:
+    case APIC_EOI:
+    case APIC_SPIV:
+    case APIC_CMCI:
+    case APIC_LVTT ... APIC_LVTERR:
+    case APIC_TMICT:
+    case APIC_TMCCT:
+    case APIC_TDCR:

Most (all?) of these MSRs have reserved bits, which should fail with a #GP(0) for attempts to set. vlapic_reg_read() masks most of the relevant bits, but doesn't appear to hit a misbehaving VM.

Quoted 3 lines
Quoted 1 line

Perhaps, but not in this patch.

Does it not count towards fixing misc aspects of x2APIC emulation?

Sure it does - I guess I should go and see by how much it grows the patch (I really don't want it to double its size because of this).

Jan

#6 lore raw

PATCH v4 2/4 x86/HVM: fix ID handling of x2APIC emulation

In reply to Jan Beulich

- properly change ID when switching into x2APIC mode (instead of mimicking necessary behavior in hvm_x2apic_msr_read())
- correctly (meaningfully) set LDR (so far it ended up being 1 on all vCPU-s)
- even if we don't support more than 128 vCPU-s in a HVM guest for now, we should properly handle IDs as 32-bit values (i.e. not ignore the top 24 bits)
- with that, properly do cluster ID and bit mask check in vlapic_match_logical_addr()
- slightly adjust other parameter types of vlapic_match_dest() and vlapic_lowest_prio() (and related local variable ones)

---
v4: Replace unpause-based approach with one latching most recently
loaded values.
v2: Some changes broken out to separate patch. Correct ID and
LDR after domain restore (if necessary); as stated previously the only compatibility problem this creates is when migrating a VM _to_ an unfixed (i.e. old) hypervisor, a scenario which supposedly isn't supported.

xen/arch/x86/hvm/vlapic.c +54 -26
@@ -173,18 +173,17 @@ uint32_t vlapic_set_ppr(struct vlapic *v
    return ppr;
 }
 
-static int vlapic_match_logical_addr(struct vlapic *vlapic, uint8_t mda)
+static int vlapic_match_logical_addr(struct vlapic *vlapic, uint32_t mda)
 {
     int result = 0;
-    uint32_t logical_id;
+    uint32_t logical_id = vlapic_get_reg(vlapic, APIC_LDR);
 
     if ( vlapic_x2apic_mode(vlapic) )
-    {
-        logical_id = vlapic_get_reg(vlapic, APIC_LDR);
-        return !!(logical_id & mda);
-    }
+        return ((logical_id >> 16) == (mda >> 16)) &&
+               (uint16_t)(logical_id & mda);
 
-    logical_id = GET_xAPIC_LOGICAL_ID(vlapic_get_reg(vlapic, APIC_LDR));
+    logical_id = GET_xAPIC_LOGICAL_ID(logical_id);
+    mda = (uint8_t)mda;
 
     switch ( vlapic_get_reg(vlapic, APIC_DFR) )
     {
@@ -207,8 +206,8 @@ static int vlapic_match_logical_addr(str
 }
 
 bool_t vlapic_match_dest(
-    struct vlapic *target, struct vlapic *source,
-    int short_hand, uint8_t dest, uint8_t dest_mode)
+    struct vlapic *target, const struct vlapic *source,
+    int short_hand, uint32_t dest, bool_t dest_mode)
 {
     HVM_DBG_LOG(DBG_LEVEL_VLAPIC, "target %p, source %p, dest %#x, "
                 "dest_mode %#x, short_hand %#x",
@@ -219,7 +218,8 @@ bool_t vlapic_match_dest(
     case APIC_DEST_NOSHORT:
         if ( dest_mode )
             return vlapic_match_logical_addr(target, dest);
-        return ((dest == 0xFF) || (dest == VLAPIC_ID(target)));
+        return (dest == _VLAPIC_ID(target, 0xffffffff)) ||
+               (dest == VLAPIC_ID(target));
 
     case APIC_DEST_SELF:
         return (target == source);
@@ -286,7 +286,7 @@ static void vlapic_init_sipi_action(unsi
     uint32_t icr = vcpu_vlapic(origin)->init_sipi.icr;
     uint32_t dest = vcpu_vlapic(origin)->init_sipi.dest;
     uint32_t short_hand = icr & APIC_SHORT_MASK;
-    uint32_t dest_mode  = !!(icr & APIC_DEST_MASK);
+    bool_t dest_mode  = !!(icr & APIC_DEST_MASK);
     struct vcpu *v;
 
     if ( icr == 0 )
@@ -352,8 +352,8 @@ static void vlapic_accept_irq(struct vcp
 }
 
 struct vlapic *vlapic_lowest_prio(
-    struct domain *d, struct vlapic *source,
-    int short_hand, uint8_t dest, uint8_t dest_mode)
+    struct domain *d, const struct vlapic *source,
+    int short_hand, uint32_t dest, bool_t dest_mode)
 {
     int old = d->arch.hvm_domain.irq.round_robin_prev_vcpu;
     uint32_t ppr, target_ppr = UINT_MAX;
@@ -434,13 +434,11 @@ void vlapic_ipi(
 {
     unsigned int dest;
     unsigned int short_hand = icr_low & APIC_SHORT_MASK;
-    unsigned int dest_mode  = !!(icr_low & APIC_DEST_MASK);
+    bool_t dest_mode = !!(icr_low & APIC_DEST_MASK);
 
     HVM_DBG_LOG(DBG_LEVEL_VLAPIC, "icr = 0x%08x:%08x", icr_high, icr_low);
 
-    dest = (vlapic_x2apic_mode(vlapic)
-            ? icr_high
-            : GET_xAPIC_DEST_FIELD(icr_high));
+    dest = _VLAPIC_ID(vlapic, icr_high);
 
     switch ( icr_low & APIC_MODE_MASK )
     {
@@ -619,10 +617,6 @@ int hvm_x2apic_msr_read(struct vcpu *v, 
     vlapic_read_aligned(vlapic, offset, &low);
     switch ( offset )
     {
-    case APIC_ID:
-        low = GET_xAPIC_ID(low);
-        break;
-
     case APIC_ICR:
         vlapic_read_aligned(vlapic, APIC_ICR2, &high);
         break;
@@ -656,6 +650,8 @@ static int vlapic_reg_write(struct vcpu 
     struct vlapic *vlapic = vcpu_vlapic(v);
     int rc = X86EMUL_OKAY;
 
+    memset(&vlapic->loaded, 0, sizeof(vlapic->loaded));
+
     switch ( offset )
     {
     case APIC_ID:
@@ -924,6 +920,15 @@ const struct hvm_mmio_handler vlapic_mmi
     .write_handler = vlapic_write
 };
 
+static void set_x2apic_id(struct vlapic *vlapic)
+{
+    u32 id = vlapic_vcpu(vlapic)->vcpu_id;
+    u32 ldr = ((id & ~0xf) << 12) | (1 << (id & 0xf));
+
+    vlapic_set_reg(vlapic, APIC_ID, id * 2);
+    vlapic_set_reg(vlapic, APIC_LDR, ldr);
+}
+
 bool_t vlapic_msr_set(struct vlapic *vlapic, uint64_t value)
 {
     if ( (vlapic->hw.apic_base_msr ^ value) & MSR_IA32_APICBASE_ENABLE )
@@ -949,13 +954,10 @@ bool_t vlapic_msr_set(struct vlapic *vla
         return 0;
 
     vlapic->hw.apic_base_msr = value;
+    memset(&vlapic->loaded, 0, sizeof(vlapic->loaded));
 
     if ( vlapic_x2apic_mode(vlapic) )
-    {
-        u32 id = vlapic_get_reg(vlapic, APIC_ID);
-        u32 ldr = ((id & ~0xf) << 16) | (1 << (id & 0xf));
-        vlapic_set_reg(vlapic, APIC_LDR, ldr);
-    }
+        set_x2apic_id(vlapic);
 
     vmx_vlapic_msr_changed(vlapic_vcpu(vlapic));
 
@@ -1227,6 +1229,22 @@ static int lapic_save_regs(struct domain
     return rc;
 }
 
+/*
+ * Following lapic_load_hidden()/lapic_load_regs() we may need to
+ * correct ID and LDR when they come from an old, broken hypervisor.
+ */
+static void lapic_load_fixup(struct vlapic *vlapic)
+{
+     uint32_t id = vlapic->loaded.id;
+
+     if ( vlapic_x2apic_mode(vlapic) &&
+          id && vlapic->loaded.ldr == 1 &&
+          /* Further checks are optional: ID != 0 contradicts LDR == 1. */
+          GET_xAPIC_ID(id) == vlapic_vcpu(vlapic)->vcpu_id * 2 &&
+          id == SET_xAPIC_ID(GET_xAPIC_ID(id)) )
+         set_x2apic_id(vlapic);
+}
+
 static int lapic_load_hidden(struct domain *d, hvm_domain_context_t *h)
 {
     uint16_t vcpuid;
@@ -1246,6 +1264,10 @@ static int lapic_load_hidden(struct doma
     if ( hvm_load_entry_zeroextend(LAPIC, h, &s->hw) != 0 ) 
         return -EINVAL;
 
+    s->loaded.hw = 1;
+    if ( s->loaded.regs )
+        lapic_load_fixup(s);
+
     if ( !(s->hw.apic_base_msr & MSR_IA32_APICBASE_ENABLE) &&
          unlikely(vlapic_x2apic_mode(s)) )
         return -EINVAL;
@@ -1274,6 +1296,12 @@ static int lapic_load_regs(struct domain
     if ( hvm_load_entry(LAPIC_REGS, h, s->regs) != 0 ) 
         return -EINVAL;
 
+    s->loaded.id = vlapic_get_reg(s, APIC_ID);
+    s->loaded.ldr = vlapic_get_reg(s, APIC_LDR);
+    s->loaded.regs = 1;
+    if ( s->loaded.hw )
+        lapic_load_fixup(s);
+
     if ( hvm_funcs.process_isr )
         hvm_funcs.process_isr(vlapic_find_highest_isr(s), v);
 
xen/include/asm-x86/hvm/vlapic.h +11 -6
@@ -30,8 +30,9 @@
 #define vlapic_vcpu(x)   (container_of((x), struct vcpu, arch.hvm_vcpu.vlapic))
 #define vlapic_domain(x) (vlapic_vcpu(x)->domain)
 
-#define VLAPIC_ID(vlapic)   \
-    (GET_xAPIC_ID(vlapic_get_reg((vlapic), APIC_ID)))
+#define _VLAPIC_ID(vlapic, id) (vlapic_x2apic_mode(vlapic) \
+                                ? (id) : GET_xAPIC_ID(id))
+#define VLAPIC_ID(vlapic) _VLAPIC_ID(vlapic, vlapic_get_reg(vlapic, APIC_ID))
 
 /*
  * APIC can be disabled in two ways:
@@ -70,6 +71,10 @@
 struct vlapic {
     struct hvm_hw_lapic      hw;
     struct hvm_hw_lapic_regs *regs;
+    struct {
+        bool_t               hw, regs;
+        uint32_t             id, ldr;
+    }                        loaded;
     struct periodic_time     pt;
     s_time_t                 timer_last_update;
     struct page_info         *regs_page;
@@ -123,11 +128,11 @@ void vlapic_ipi(struct vlapic *vlapic, u
 int vlapic_apicv_write(struct vcpu *v, unsigned int offset);
 
 struct vlapic *vlapic_lowest_prio(
-    struct domain *d, struct vlapic *source,
-    int short_hand, uint8_t dest, uint8_t dest_mode);
+    struct domain *d, const struct vlapic *source,
+    int short_hand, uint32_t dest, bool_t dest_mode);
 
 bool_t vlapic_match_dest(
-    struct vlapic *target, struct vlapic *source,
-    int short_hand, uint8_t dest, uint8_t dest_mode);
+    struct vlapic *target, const struct vlapic *source,
+    int short_hand, uint32_t dest, bool_t dest_mode);
 
 #endif /* __ASM_X86_HVM_VLAPIC_H__ */

x86/HVM: fix ID handling of x2APIC emulation

- properly change ID when switching into x2APIC mode (instead of mimicking necessary behavior in hvm_x2apic_msr_read())
- correctly (meaningfully) set LDR (so far it ended up being 1 on all vCPU-s)
- even if we don't support more than 128 vCPU-s in a HVM guest for now, we should properly handle IDs as 32-bit values (i.e. not ignore the top 24 bits)
- with that, properly do cluster ID and bit mask check in vlapic_match_logical_addr()
- slightly adjust other parameter types of vlapic_match_dest() and vlapic_lowest_prio() (and related local variable ones)

---
v4: Replace unpause-based approach with one latching most recently
loaded values.
v2: Some changes broken out to separate patch. Correct ID and
LDR after domain restore (if necessary); as stated previously the only compatibility problem this creates is when migrating a VM _to_ an unfixed (i.e. old) hypervisor, a scenario which supposedly isn't supported.

xen/arch/x86/hvm/vlapic.c +54 -26

168 lines not on this page · raw message

xen/include/asm-x86/hvm/vlapic.h +11 -6
@@ -30,8 +30,9 @@
 #define vlapic_vcpu(x)   (container_of((x), struct vcpu, arch.hvm_vcpu.vlapic))
 #define vlapic_domain(x) (vlapic_vcpu(x)->domain)
 
-#define VLAPIC_ID(vlapic)   \
-    (GET_xAPIC_ID(vlapic_get_reg((vlapic), APIC_ID)))
+#define _VLAPIC_ID(vlapic, id) (vlapic_x2apic_mode(vlapic) \
+                                ? (id) : GET_xAPIC_ID(id))
+#define VLAPIC_ID(vlapic) _VLAPIC_ID(vlapic, vlapic_get_reg(vlapic, APIC_ID))
 
 /*
  * APIC can be disabled in two ways:
@@ -70,6 +71,10 @@
 struct vlapic {
     struct hvm_hw_lapic      hw;
     struct hvm_hw_lapic_regs *regs;
+    struct {
+        bool_t               hw, regs;
+        uint32_t             id, ldr;
+    }                        loaded;
     struct periodic_time     pt;
     s_time_t                 timer_last_update;
     struct page_info         *regs_page;
@@ -123,11 +128,11 @@ void vlapic_ipi(struct vlapic *vlapic, u
 int vlapic_apicv_write(struct vcpu *v, unsigned int offset);
 
 struct vlapic *vlapic_lowest_prio(
-    struct domain *d, struct vlapic *source,
-    int short_hand, uint8_t dest, uint8_t dest_mode);
+    struct domain *d, const struct vlapic *source,
+    int short_hand, uint32_t dest, bool_t dest_mode);
 
 bool_t vlapic_match_dest(
-    struct vlapic *target, struct vlapic *source,
-    int short_hand, uint8_t dest, uint8_t dest_mode);
+    struct vlapic *target, const struct vlapic *source,
+    int short_hand, uint32_t dest, bool_t dest_mode);
 
 #endif /* __ASM_X86_HVM_VLAPIC_H__ */

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#7 lore raw

Re: [PATCH v4 2/4] x86/HVM: fix ID handling of x2APIC emulation

Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 18.09.14 at 16:44, <[email protected]> wrote:
xen/arch/x86/hvm/vlapic.c +4 -0
@@ -1227,6 +1229,22 @@ static int lapic_save_regs(struct domain
     return rc;
 }
 
+/*
+ * Following lapic_load_hidden()/lapic_load_regs() we may need to
+ * correct ID and LDR when they come from an old, broken hypervisor.
+ */

xen/arch/x86/hvm/vlapic.c static int lapic_save_regs(struct domain · line 1245

Quoted 10 lines
xen/arch/x86/hvm/vlapic.c +10 -0
+static void lapic_load_fixup(struct vlapic *vlapic)
+{
+     uint32_t id = vlapic->loaded.id;
+
+     if ( vlapic_x2apic_mode(vlapic) &&
+          id && vlapic->loaded.ldr == 1 &&
+          /* Further checks are optional: ID != 0 contradicts LDR == 1. */
+          GET_xAPIC_ID(id) == vlapic_vcpu(vlapic)->vcpu_id * 2 &&
+          id == SET_xAPIC_ID(GET_xAPIC_ID(id)) )
+         set_x2apic_id(vlapic);

+ else /* Undo an eventual earlier fixup. */
+ {
+ vlapic_set_reg(vlapic, APIC_ID, id);
+ vlapic_set_reg(vlapic, APIC_LDR, vlapic->loaded.ldr);
+ }

Review static int lapic_save_regs(struct domain · line 1246

Quoted 1 line
(quoted diff) +1 -0
+}

Jan

#8 lore raw

Re: [PATCH v4 2/4] x86/HVM: fix ID handling of x2APIC emulation

In reply to Jan Beulich
Quoted 50 lines · Jan Beulich

- properly change ID when switching into x2APIC mode (instead of mimicking necessary behavior in hvm_x2apic_msr_read())
- correctly (meaningfully) set LDR (so far it ended up being 1 on all vCPU-s)
- even if we don't support more than 128 vCPU-s in a HVM guest for now, we should properly handle IDs as 32-bit values (i.e. not ignore the top 24 bits)
- with that, properly do cluster ID and bit mask check in vlapic_match_logical_addr()
- slightly adjust other parameter types of vlapic_match_dest() and vlapic_lowest_prio() (and related local variable ones)

---
v4: Replace unpause-based approach with one latching most recently
loaded values.
v2: Some changes broken out to separate patch. Correct ID and
LDR after domain restore (if necessary); as stated previously the only compatibility problem this creates is when migrating a VM _to_ an unfixed (i.e. old) hypervisor, a scenario which supposedly isn't supported.

xen/arch/x86/hvm/vlapic.c +6 -7
@@ -173,18 +173,17 @@ uint32_t vlapic_set_ppr(struct vlapic *v
    return ppr;
 }
 
-static int vlapic_match_logical_addr(struct vlapic *vlapic, uint8_t mda)
+static int vlapic_match_logical_addr(struct vlapic *vlapic, uint32_t mda)
 {
     int result = 0;
-    uint32_t logical_id;
+    uint32_t logical_id = vlapic_get_reg(vlapic, APIC_LDR);
 
     if ( vlapic_x2apic_mode(vlapic) )
-    {
-        logical_id = vlapic_get_reg(vlapic, APIC_LDR);
-        return !!(logical_id & mda);
-    }
+        return ((logical_id >> 16) == (mda >> 16)) &&
+               (uint16_t)(logical_id & mda);
 
-    logical_id = GET_xAPIC_LOGICAL_ID(vlapic_get_reg(vlapic, APIC_LDR));
+    logical_id = GET_xAPIC_LOGICAL_ID(logical_id);
+    mda = (uint8_t)mda;
 
     switch ( vlapic_get_reg(vlapic, APIC_DFR) )
     {

xen/arch/x86/hvm/vlapic.c static int vlapic_match_logical_addr(str · line 209

Quoted 6 lines
xen/arch/x86/hvm/vlapic.c +1 -2
@@ -207,8 +206,8 @@ static int vlapic_match_logical_addr(str
 }
 
 bool_t vlapic_match_dest(
-    struct vlapic *target, struct vlapic *source,
-    int short_hand, uint8_t dest, uint8_t dest_mode)
+    struct vlapic *target, const struct vlapic *source,

This constification of source could move to patch 3 (if there are sufficient other changes to warrent a respin of the series). Here and elsewhere.

Quoted 15 lines
xen/arch/x86/hvm/vlapic.c +3 -1
+    int short_hand, uint32_t dest, bool_t dest_mode)
 {
     HVM_DBG_LOG(DBG_LEVEL_VLAPIC, "target %p, source %p, dest %#x, "
                 "dest_mode %#x, short_hand %#x",
@@ -219,7 +218,8 @@ bool_t vlapic_match_dest(
     case APIC_DEST_NOSHORT:
         if ( dest_mode )
             return vlapic_match_logical_addr(target, dest);
-        return ((dest == 0xFF) || (dest == VLAPIC_ID(target)));
+        return (dest == _VLAPIC_ID(target, 0xffffffff)) ||
+               (dest == VLAPIC_ID(target));
 
     case APIC_DEST_SELF:
         return (target == source);

xen/arch/x86/hvm/vlapic.c static void vlapic_init_sipi_action(unsi · line 289

Quoted 5 lines
xen/arch/x86/hvm/vlapic.c +1 -1
@@ -286,7 +286,7 @@ static void vlapic_init_sipi_action(unsi
     uint32_t icr = vcpu_vlapic(origin)->init_sipi.icr;
     uint32_t dest = vcpu_vlapic(origin)->init_sipi.dest;
     uint32_t short_hand = icr & APIC_SHORT_MASK;
-    uint32_t dest_mode  = !!(icr & APIC_DEST_MASK);
+    bool_t dest_mode  = !!(icr & APIC_DEST_MASK);

This could also move to patch 3.

Quoted 42 lines
xen/arch/x86/hvm/vlapic.c +4 -10
     struct vcpu *v;
 
     if ( icr == 0 )
@@ -352,8 +352,8 @@ static void vlapic_accept_irq(struct vcp
 }
 
 struct vlapic *vlapic_lowest_prio(
-    struct domain *d, struct vlapic *source,
-    int short_hand, uint8_t dest, uint8_t dest_mode)
+    struct domain *d, const struct vlapic *source,
+    int short_hand, uint32_t dest, bool_t dest_mode)
 {
     int old = d->arch.hvm_domain.irq.round_robin_prev_vcpu;
     uint32_t ppr, target_ppr = UINT_MAX;
@@ -434,13 +434,11 @@ void vlapic_ipi(
 {
     unsigned int dest;
     unsigned int short_hand = icr_low & APIC_SHORT_MASK;
-    unsigned int dest_mode  = !!(icr_low & APIC_DEST_MASK);
+    bool_t dest_mode = !!(icr_low & APIC_DEST_MASK);
 
     HVM_DBG_LOG(DBG_LEVEL_VLAPIC, "icr = 0x%08x:%08x", icr_high, icr_low);
 
-    dest = (vlapic_x2apic_mode(vlapic)
-            ? icr_high
-            : GET_xAPIC_DEST_FIELD(icr_high));
+    dest = _VLAPIC_ID(vlapic, icr_high);
 
     switch ( icr_low & APIC_MODE_MASK )
     {
@@ -619,10 +617,6 @@ int hvm_x2apic_msr_read(struct vcpu *v, 
     vlapic_read_aligned(vlapic, offset, &low);
     switch ( offset )
     {
-    case APIC_ID:
-        low = GET_xAPIC_ID(low);
-        break;
-
     case APIC_ICR:
         vlapic_read_aligned(vlapic, APIC_ICR2, &high);
         break;

xen/arch/x86/hvm/vlapic.c static int vlapic_reg_write(struct vcpu · line 653

Quoted 4 lines
xen/arch/x86/hvm/vlapic.c +1 -0
@@ -656,6 +650,8 @@ static int vlapic_reg_write(struct vcpu 
     struct vlapic *vlapic = vcpu_vlapic(v);
     int rc = X86EMUL_OKAY;
 
+    memset(&vlapic->loaded, 0, sizeof(vlapic->loaded));

This is slightly expensive to do for each register write. Why does this clobbering need to be here? Could it not be....

Quoted 21 lines
xen/arch/x86/hvm/vlapic.c +10 -0
+
     switch ( offset )
     {
     case APIC_ID:
@@ -924,6 +920,15 @@ const struct hvm_mmio_handler vlapic_mmi
     .write_handler = vlapic_write
 };
 
+static void set_x2apic_id(struct vlapic *vlapic)
+{
+    u32 id = vlapic_vcpu(vlapic)->vcpu_id;
+    u32 ldr = ((id & ~0xf) << 12) | (1 << (id & 0xf));
+
+    vlapic_set_reg(vlapic, APIC_ID, id * 2);
+    vlapic_set_reg(vlapic, APIC_LDR, ldr);
+}
+
 bool_t vlapic_msr_set(struct vlapic *vlapic, uint64_t value)
 {
     if ( (vlapic->hw.apic_base_msr ^ value) & MSR_IA32_APICBASE_ENABLE )

xen/arch/x86/hvm/vlapic.c bool_t vlapic_msr_set(struct vlapic *vla · line 957

Quoted 4 lines
xen/arch/x86/hvm/vlapic.c +1 -0
@@ -949,13 +954,10 @@ bool_t vlapic_msr_set(struct vlapic *vla
         return 0;
 
     vlapic->hw.apic_base_msr = value;
+    memset(&vlapic->loaded, 0, sizeof(vlapic->loaded));

(Similarly here...)

Quoted 19 lines
xen/arch/x86/hvm/vlapic.c +5 -5
     if ( vlapic_x2apic_mode(vlapic) )
-    {
-        u32 id = vlapic_get_reg(vlapic, APIC_ID);
-        u32 ldr = ((id & ~0xf) << 16) | (1 << (id & 0xf));
-        vlapic_set_reg(vlapic, APIC_LDR, ldr);
-    }
+        set_x2apic_id(vlapic);
 
     vmx_vlapic_msr_changed(vlapic_vcpu(vlapic));
 
@@ -1227,6 +1229,22 @@ static int lapic_save_regs(struct domain
     return rc;
 }
 
+/*
+ * Following lapic_load_hidden()/lapic_load_regs() we may need to
+ * correct ID and LDR when they come from an old, broken hypervisor.
+ */

xen/arch/x86/hvm/vlapic.c static int lapic_save_regs(struct domain · line 1245

Quoted 10 lines
xen/arch/x86/hvm/vlapic.c +10 -0
+static void lapic_load_fixup(struct vlapic *vlapic)
+{
+     uint32_t id = vlapic->loaded.id;
+
+     if ( vlapic_x2apic_mode(vlapic) &&
+          id && vlapic->loaded.ldr == 1 &&
+          /* Further checks are optional: ID != 0 contradicts LDR == 1. */
+          GET_xAPIC_ID(id) == vlapic_vcpu(vlapic)->vcpu_id * 2 &&
+          id == SET_xAPIC_ID(GET_xAPIC_ID(id)) )
+         set_x2apic_id(vlapic);

... in here, where we have positively identified whether fixup is needed or not.

~Andrew

Quoted 74 lines
xen/arch/x86/hvm/vlapic.c +12 -0
+}
+
 static int lapic_load_hidden(struct domain *d, hvm_domain_context_t *h)
 {
     uint16_t vcpuid;
@@ -1246,6 +1264,10 @@ static int lapic_load_hidden(struct doma
     if ( hvm_load_entry_zeroextend(LAPIC, h, &s->hw) != 0 ) 
         return -EINVAL;
 
+    s->loaded.hw = 1;
+    if ( s->loaded.regs )
+        lapic_load_fixup(s);
+
     if ( !(s->hw.apic_base_msr & MSR_IA32_APICBASE_ENABLE) &&
          unlikely(vlapic_x2apic_mode(s)) )
         return -EINVAL;
@@ -1274,6 +1296,12 @@ static int lapic_load_regs(struct domain
     if ( hvm_load_entry(LAPIC_REGS, h, s->regs) != 0 ) 
         return -EINVAL;
 
+    s->loaded.id = vlapic_get_reg(s, APIC_ID);
+    s->loaded.ldr = vlapic_get_reg(s, APIC_LDR);
+    s->loaded.regs = 1;
+    if ( s->loaded.hw )
+        lapic_load_fixup(s);
+
     if ( hvm_funcs.process_isr )
         hvm_funcs.process_isr(vlapic_find_highest_isr(s), v);
 
xen/include/asm-x86/hvm/vlapic.h +11 -6
@@ -30,8 +30,9 @@
 #define vlapic_vcpu(x)   (container_of((x), struct vcpu, arch.hvm_vcpu.vlapic))
 #define vlapic_domain(x) (vlapic_vcpu(x)->domain)
 
-#define VLAPIC_ID(vlapic)   \
-    (GET_xAPIC_ID(vlapic_get_reg((vlapic), APIC_ID)))
+#define _VLAPIC_ID(vlapic, id) (vlapic_x2apic_mode(vlapic) \
+                                ? (id) : GET_xAPIC_ID(id))
+#define VLAPIC_ID(vlapic) _VLAPIC_ID(vlapic, vlapic_get_reg(vlapic, APIC_ID))
 
 /*
  * APIC can be disabled in two ways:
@@ -70,6 +71,10 @@
 struct vlapic {
     struct hvm_hw_lapic      hw;
     struct hvm_hw_lapic_regs *regs;
+    struct {
+        bool_t               hw, regs;
+        uint32_t             id, ldr;
+    }                        loaded;
     struct periodic_time     pt;
     s_time_t                 timer_last_update;
     struct page_info         *regs_page;
@@ -123,11 +128,11 @@ void vlapic_ipi(struct vlapic *vlapic, u
 int vlapic_apicv_write(struct vcpu *v, unsigned int offset);
 
 struct vlapic *vlapic_lowest_prio(
-    struct domain *d, struct vlapic *source,
-    int short_hand, uint8_t dest, uint8_t dest_mode);
+    struct domain *d, const struct vlapic *source,
+    int short_hand, uint32_t dest, bool_t dest_mode);
 
 bool_t vlapic_match_dest(
-    struct vlapic *target, struct vlapic *source,
-    int short_hand, uint8_t dest, uint8_t dest_mode);
+    struct vlapic *target, const struct vlapic *source,
+    int short_hand, uint32_t dest, bool_t dest_mode);
 
 #endif /* __ASM_X86_HVM_VLAPIC_H__ */
Quoted 4 lines

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#9 lore raw

Re: [PATCH v4 2/4] x86/HVM: fix ID handling of x2APIC emulation

Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 22.09.14 at 16:30, <[email protected]> wrote:

xen/arch/x86/hvm/vlapic.c static int vlapic_reg_write(struct vcpu · line 653

Quoted 5 lines · Jan Beulich
xen/arch/x86/hvm/vlapic.c +1 -0
@@ -656,6 +650,8 @@ static int vlapic_reg_write(struct vcpu 
     struct vlapic *vlapic = vcpu_vlapic(v);
     int rc = X86EMUL_OKAY;
 
+    memset(&vlapic->loaded, 0, sizeof(vlapic->loaded));

This is slightly expensive to do for each register write. Why does this clobbering need to be here? Could it not be....

No, clearing this state can't be done in the load functions themselves (or else we wouldn't need the state in the first place. This, however, is more obvious with the follow-up addition I mailed in reply to the patch here, adding an "else" path to lapic_load_fixup(). That code wouldn't work as intended if we cleared the state early.

If writing 12 bytes to memory would really turn out expensive, we may end up having to limit the clearing to certain register writes, but I was really instead considering to also do the clearing on register reads.

Quoted 5 lines
Quoted 5 lines
xen/arch/x86/hvm/vlapic.c +5 -0
+static void lapic_load_fixup(struct vlapic *vlapic)
+{
+     uint32_t id = vlapic->loaded.id;
+
+     if ( vlapic_x2apic_mode(vlapic) &&
Quoted 8 lines

xen/arch/x86/hvm/vlapic.c static int lapic_save_regs(struct domain · line 1245

Quoted 5 lines
xen/arch/x86/hvm/vlapic.c +5 -0
+          id && vlapic->loaded.ldr == 1 &&
+          /* Further checks are optional: ID != 0 contradicts LDR == 1. */
+          GET_xAPIC_ID(id) == vlapic_vcpu(vlapic)->vcpu_id * 2 &&
+          id == SET_xAPIC_ID(GET_xAPIC_ID(id)) )
+         set_x2apic_id(vlapic);

... in here, where we have positively identified whether fixup is needed or not.

For full context, here's the full intended function again:

+static void lapic_load_fixup(struct vlapic *vlapic) +{
+ uint32_t id = vlapic->loaded.id;
+
+ if ( vlapic_x2apic_mode(vlapic) && id && vlapic->loaded.ldr == 1 &&

+         /* Further checks are optional: ID != 0 contradicts LDR == 1. */
+         GET_xAPIC_ID(id) == vlapic_vcpu(vlapic)->vcpu_id * 2 &&
+         id == SET_xAPIC_ID(GET_xAPIC_ID(id)) )

+ set_x2apic_id(vlapic);
+ else /* Undo an eventual earlier fixup. */
+ {
+ vlapic_set_reg(vlapic, APIC_ID, id);
+ vlapic_set_reg(vlapic, APIC_LDR, vlapic->loaded.ldr);
+ }
+}

Jan

#10 lore raw

Re: [PATCH v4 2/4] x86/HVM: fix ID handling of x2APIC emulation

Quoted 39 lines · Jan Beulich
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 22.09.14 at 16:30, <[email protected]> wrote:

xen/arch/x86/hvm/vlapic.c static int vlapic_reg_write(struct vcpu · line 653

Quoted 5 lines · Jan Beulich
xen/arch/x86/hvm/vlapic.c +1 -0
@@ -656,6 +650,8 @@ static int vlapic_reg_write(struct vcpu 
     struct vlapic *vlapic = vcpu_vlapic(v);
     int rc = X86EMUL_OKAY;
 
+    memset(&vlapic->loaded, 0, sizeof(vlapic->loaded));

This is slightly expensive to do for each register write. Why does this clobbering need to be here? Could it not be....

No, clearing this state can't be done in the load functions themselves (or else we wouldn't need the state in the first place. This, however, is more obvious with the follow-up addition I mailed in reply to the patch here, adding an "else" path to lapic_load_fixup(). That code wouldn't work as intended if we cleared the state early.

If writing 12 bytes to memory would really turn out expensive, we may end up having to limit the clearing to certain register writes, but I was really instead considering to also do the clearing on register reads.

Quoted 4 lines
Quoted 4 lines
xen/arch/x86/hvm/vlapic.c +4 -0
+static void lapic_load_fixup(struct vlapic *vlapic)
+{
+     uint32_t id = vlapic->loaded.id;
+
Quoted 8 lines

xen/arch/x86/hvm/vlapic.c static int lapic_save_regs(struct domain · line 1245

Quoted 6 lines
xen/arch/x86/hvm/vlapic.c +6 -0
+     if ( vlapic_x2apic_mode(vlapic) &&
+          id && vlapic->loaded.ldr == 1 &&
+          /* Further checks are optional: ID != 0 contradicts LDR == 1. */
+          GET_xAPIC_ID(id) == vlapic_vcpu(vlapic)->vcpu_id * 2 &&
+          id == SET_xAPIC_ID(GET_xAPIC_ID(id)) )
+         set_x2apic_id(vlapic);

... in here, where we have positively identified whether fixup is needed or not.

For full context, here's the full intended function again:

xen/arch/x86/hvm/vlapic.c +5 -0
+static void lapic_load_fixup(struct vlapic *vlapic)
+{
+    uint32_t id = vlapic->loaded.id;
+
+    if ( vlapic_x2apic_mode(vlapic) && id && vlapic->loaded.ldr == 1 &&

xen/arch/x86/hvm/vlapic.c static int lapic_save_regs(struct domain

Quoted 10 lines
xen/arch/x86/hvm/vlapic.c +10 -0
+         /* Further checks are optional: ID != 0 contradicts LDR == 1. */
+         GET_xAPIC_ID(id) == vlapic_vcpu(vlapic)->vcpu_id * 2 &&
+         id == SET_xAPIC_ID(GET_xAPIC_ID(id)) )
+        set_x2apic_id(vlapic);
+    else /* Undo an eventual earlier fixup. */
+    {
+        vlapic_set_reg(vlapic, APIC_ID, id);
+        vlapic_set_reg(vlapic, APIC_LDR, vlapic->loaded.ldr);
+    }
+}

How about dropping the optional checks, as "id && vlapic->loaded.ldr == 1" covers the broken hypervisor case?

The "id = vcpu_id * 2" is a broken assumption which I do need to fix as part of the cpuid infrastructure improvements, which would then break this check for a broken Xen.

Furthermore, vlapic_x2apic_mode(vlapic) contradicts the use of {GET,SET}_xAPIC_ID().

~Andrew

#11 lore raw

Re: [PATCH v4 2/4] x86/HVM: fix ID handling of x2APIC emulation

Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 24.09.14 at 12:42, <[email protected]> wrote:
Quoted 12 lines · Jan Beulich

For full context, here's the full intended function again:

xen/arch/x86/hvm/vlapic.c +10 -0
+static void lapic_load_fixup(struct vlapic *vlapic)
+{
+    uint32_t id = vlapic->loaded.id;
+
+    if ( vlapic_x2apic_mode(vlapic) && id && vlapic->loaded.ldr == 1 &&
+         /* Further checks are optional: ID != 0 contradicts LDR == 1. */
+         GET_xAPIC_ID(id) == vlapic_vcpu(vlapic)->vcpu_id * 2 &&
+         id == SET_xAPIC_ID(GET_xAPIC_ID(id)) )
+        set_x2apic_id(vlapic);
+    else /* Undo an eventual earlier fixup. */
Quoted 8 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+    {
+        vlapic_set_reg(vlapic, APIC_ID, id);
+        vlapic_set_reg(vlapic, APIC_LDR, vlapic->loaded.ldr);
+    }
+}

How about dropping the optional checks, as "id && vlapic->loaded.ldr == 1" covers the broken hypervisor case?

I'd like to keep them for a while - after all that's why I added the comment saying they're optional. The moment they start conflicting with something else, they could be dropped.

The alternative would be to make them WARN_ON()s inside the if().

Quoted 3 lines

The "id = vcpu_id * 2" is a broken assumption which I do need to fix as part of the cpuid infrastructure improvements, which would then break this check for a broken Xen.

For one it's not a broken assumption imo: The APIC ID gets set up this way. And then I don't see why altering the APIC ID setting would break this check here: If altering how the ID gets established would get backported, I'd surely expect the change to the ID handling here to also be.

Quoted 2 lines

Furthermore, vlapic_x2apic_mode(vlapic) contradicts the use of {GET,SET}_xAPIC_ID().

So it does, but intentionally. Remember - we're checking whether some fixup to what came in is necessary, and part of the brokenness was that the ID was left set in a legacy APIC manner.

Jan

#12 lore raw

Re: [PATCH v4 2/4] x86/HVM: fix ID handling of x2APIC emulation

Quoted 21 lines · Jan Beulich
Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 24.09.14 at 12:42, <[email protected]> wrote:
Quoted 11 lines · Jan Beulich

For full context, here's the full intended function again:

xen/arch/x86/hvm/vlapic.c +9 -0
+static void lapic_load_fixup(struct vlapic *vlapic)
+{
+    uint32_t id = vlapic->loaded.id;
+
+    if ( vlapic_x2apic_mode(vlapic) && id && vlapic->loaded.ldr == 1 &&
+         /* Further checks are optional: ID != 0 contradicts LDR == 1. */
+         GET_xAPIC_ID(id) == vlapic_vcpu(vlapic)->vcpu_id * 2 &&
+         id == SET_xAPIC_ID(GET_xAPIC_ID(id)) )
+        set_x2apic_id(vlapic);
Quoted 8 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+    else /* Undo an eventual earlier fixup. */
+    {
+        vlapic_set_reg(vlapic, APIC_ID, id);
+        vlapic_set_reg(vlapic, APIC_LDR, vlapic->loaded.ldr);
+    }
+}

How about dropping the optional checks, as "id && vlapic->loaded.ldr == 1" covers the broken hypervisor case?

Quoted 5 lines

I'd like to keep them for a while - after all that's why I added the comment saying they're optional. The moment they start conflicting with something else, they could be dropped.

The alternative would be to make them WARN_ON()s inside the if().

Making them WARN_ON()s would make it more obvious if underlying assumption/implementations change which subsequently invalidate the checks.

Quoted 6 lines
Quoted 4 lines

The "id = vcpu_id * 2" is a broken assumption which I do need to fix as part of the cpuid infrastructure improvements, which would then break this check for a broken Xen.

For one it's not a broken assumption imo: The APIC ID gets set up this way.

Sorry - it is one which is expected to change in future development work.

Quoted 4 lines

And then I don't see why altering the APIC ID setting would break this check here: If altering how the ID gets established would get backported, I'd surely expect the change to the ID handling here to also be.

This check gets applied equally to migrations from the same version of Xen as to those from older versions. In this case I suppose the "id && vlapic->loaded.ldr" will short circuit the vcpu_id*2 check, in makes it ok (assuming no backports).

Quoted 6 lines
Quoted 3 lines

Furthermore, vlapic_x2apic_mode(vlapic) contradicts the use of {GET,SET}_xAPIC_ID().

So it does, but intentionally. Remember - we're checking whether some fixup to what came in is necessary, and part of the brokenness was that the ID was left set in a legacy APIC manner.

Right, which again due to short circuiting ok given that we have never supported x2apic with ids greater than 255.

~Andrew

#13 lore raw

PATCH v4 3/4 x86/HVM: a few type adjustments

In reply to Jan Beulich

Constify a couple of pointer parameters, convert a boolean function return type to bool_t, and clean up a printk() being touched anyway.

xen/arch/x86/hvm/vlapic.c +6 -6
@@ -173,9 +173,9 @@ uint32_t vlapic_set_ppr(struct vlapic *v
    return ppr;
 }
 
-static int vlapic_match_logical_addr(struct vlapic *vlapic, uint32_t mda)
+static bool_t vlapic_match_logical_addr(const struct vlapic *vlapic, uint32_t mda)
 {
-    int result = 0;
+    bool_t result = 0;
     uint32_t logical_id = vlapic_get_reg(vlapic, APIC_LDR);
 
     if ( vlapic_x2apic_mode(vlapic) )
@@ -196,9 +196,9 @@ static int vlapic_match_logical_addr(str
             result = 1;
         break;
     default:
-        gdprintk(XENLOG_WARNING, "Bad DFR value for lapic of vcpu %d: %08x\n",
-                 vlapic_vcpu(vlapic)->vcpu_id,
-                 vlapic_get_reg(vlapic, APIC_DFR));
+        printk(XENLOG_G_WARNING "%pv: bad LAPIC DFR value %08x\n",
+               const_vlapic_vcpu(vlapic),
+               vlapic_get_reg(vlapic, APIC_DFR));
         break;
     }
 
@@ -206,7 +206,7 @@ static int vlapic_match_logical_addr(str
 }
 
 bool_t vlapic_match_dest(
-    struct vlapic *target, const struct vlapic *source,
+    const struct vlapic *target, const struct vlapic *source,
     int short_hand, uint32_t dest, bool_t dest_mode)
 {
     HVM_DBG_LOG(DBG_LEVEL_VLAPIC, "target %p, source %p, dest %#x, "
xen/include/asm-x86/hvm/vlapic.h +5 -2
@@ -28,6 +28,8 @@
 
 #define vcpu_vlapic(x)   (&(x)->arch.hvm_vcpu.vlapic)
 #define vlapic_vcpu(x)   (container_of((x), struct vcpu, arch.hvm_vcpu.vlapic))
+#define const_vlapic_vcpu(x) (container_of((x), const struct vcpu, \
+                              arch.hvm_vcpu.vlapic))
 #define vlapic_domain(x) (vlapic_vcpu(x)->domain)
 
 #define _VLAPIC_ID(vlapic, id) (vlapic_x2apic_mode(vlapic) \
@@ -88,7 +90,8 @@ struct vlapic {
 /* vlapic's frequence is 100 MHz */
 #define APIC_BUS_CYCLE_NS               10
 
-static inline uint32_t vlapic_get_reg(struct vlapic *vlapic, uint32_t reg)
+static inline uint32_t vlapic_get_reg(const struct vlapic *vlapic,
+                                      uint32_t reg)
 {
     return *((uint32_t *)(&vlapic->regs->data[reg]));
 }
@@ -132,7 +135,7 @@ struct vlapic *vlapic_lowest_prio(
     int short_hand, uint32_t dest, bool_t dest_mode);
 
 bool_t vlapic_match_dest(
-    struct vlapic *target, const struct vlapic *source,
+    const struct vlapic *target, const struct vlapic *source,
     int short_hand, uint32_t dest, bool_t dest_mode);
 
 #endif /* __ASM_X86_HVM_VLAPIC_H__ */

x86/vlapic: a few type adjustments

Constify a couple of pointer parameters, convert a boolean function return type to bool_t, and clean up a printk() being touched anyway.

xen/arch/x86/hvm/vlapic.c +6 -6
@@ -173,9 +173,9 @@ uint32_t vlapic_set_ppr(struct vlapic *v
    return ppr;
 }
 
-static int vlapic_match_logical_addr(struct vlapic *vlapic, uint32_t mda)
+static bool_t vlapic_match_logical_addr(const struct vlapic *vlapic, uint32_t mda)
 {
-    int result = 0;
+    bool_t result = 0;
     uint32_t logical_id = vlapic_get_reg(vlapic, APIC_LDR);
 
     if ( vlapic_x2apic_mode(vlapic) )
@@ -196,9 +196,9 @@ static int vlapic_match_logical_addr(str
             result = 1;
         break;
     default:
-        gdprintk(XENLOG_WARNING, "Bad DFR value for lapic of vcpu %d: %08x\n",
-                 vlapic_vcpu(vlapic)->vcpu_id,
-                 vlapic_get_reg(vlapic, APIC_DFR));
+        printk(XENLOG_G_WARNING "%pv: bad LAPIC DFR value %08x\n",
+               const_vlapic_vcpu(vlapic),
+               vlapic_get_reg(vlapic, APIC_DFR));
         break;
     }
 
@@ -206,7 +206,7 @@ static int vlapic_match_logical_addr(str
 }
 
 bool_t vlapic_match_dest(
-    struct vlapic *target, const struct vlapic *source,
+    const struct vlapic *target, const struct vlapic *source,
     int short_hand, uint32_t dest, bool_t dest_mode)
 {
     HVM_DBG_LOG(DBG_LEVEL_VLAPIC, "target %p, source %p, dest %#x, "
xen/include/asm-x86/hvm/vlapic.h +5 -2
@@ -28,6 +28,8 @@
 
 #define vcpu_vlapic(x)   (&(x)->arch.hvm_vcpu.vlapic)
 #define vlapic_vcpu(x)   (container_of((x), struct vcpu, arch.hvm_vcpu.vlapic))
+#define const_vlapic_vcpu(x) (container_of((x), const struct vcpu, \
+                              arch.hvm_vcpu.vlapic))
 #define vlapic_domain(x) (vlapic_vcpu(x)->domain)
 
 #define _VLAPIC_ID(vlapic, id) (vlapic_x2apic_mode(vlapic) \
@@ -88,7 +90,8 @@ struct vlapic {
 /* vlapic's frequence is 100 MHz */
 #define APIC_BUS_CYCLE_NS               10
 
-static inline uint32_t vlapic_get_reg(struct vlapic *vlapic, uint32_t reg)
+static inline uint32_t vlapic_get_reg(const struct vlapic *vlapic,
+                                      uint32_t reg)
 {
     return *((uint32_t *)(&vlapic->regs->data[reg]));
 }
@@ -132,7 +135,7 @@ struct vlapic *vlapic_lowest_prio(
     int short_hand, uint32_t dest, bool_t dest_mode);
 
 bool_t vlapic_match_dest(
-    struct vlapic *target, const struct vlapic *source,
+    const struct vlapic *target, const struct vlapic *source,
     int short_hand, uint32_t dest, bool_t dest_mode);
 
 #endif /* __ASM_X86_HVM_VLAPIC_H__ */

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#14 lore raw

PATCH v4 4/4 x86/vlapic: don't silently accept bad vectors

In reply to Jan Beulich

Vectors 0-15 are reserved, and a physical LAPIC - upon sending or receiving one - would generate an APIC error instead of doing the requested action. Make our emulation behave similarly.

xen/arch/x86/hvm/vlapic.c +37 -1
@@ -123,10 +123,34 @@ static int vlapic_find_highest_irr(struc
     return vlapic_find_highest_vector(&vlapic->regs->data[APIC_IRR]);
 }
 
+static void vlapic_error(struct vlapic *vlapic, unsigned int errmask)
+{
+    unsigned long flags;
+    uint32_t esr;
+
+    spin_lock_irqsave(&vlapic->esr_lock, flags);
+    esr = vlapic_get_reg(vlapic, APIC_ESR);
+    if ( (esr & errmask) != errmask )
+    {
+        uint32_t lvterr = vlapic_get_reg(vlapic, APIC_LVTERR);
+
+        vlapic_set_reg(vlapic, APIC_ESR, esr | errmask);
+        if ( !(lvterr & APIC_LVT_MASKED) )
+            vlapic_set_irq(vlapic, lvterr & APIC_VECTOR_MASK, 0);
+    }
+    spin_unlock_irqrestore(&vlapic->esr_lock, flags);
+}
+
 void vlapic_set_irq(struct vlapic *vlapic, uint8_t vec, uint8_t trig)
 {
     struct vcpu *target = vlapic_vcpu(vlapic);
 
+    if ( unlikely(vec < 16) )
+    {
+        vlapic_error(vlapic, APIC_ESR_RECVILL);
+        return;
+    }
+
     if ( trig )
         vlapic_set_vector(vec, &vlapic->regs->data[APIC_TMR]);
 
@@ -459,7 +483,12 @@ void vlapic_ipi(
         struct vlapic *target = vlapic_lowest_prio(
             vlapic_domain(vlapic), vlapic, short_hand, dest, dest_mode);
         if ( target != NULL )
-            vlapic_accept_irq(vlapic_vcpu(target), icr_low);
+        {
+            if ( likely((icr_low & APIC_VECTOR_MASK) >= 16) )
+                vlapic_accept_irq(vlapic_vcpu(target), icr_low);
+            else
+                vlapic_error(vlapic, APIC_ESR_SENDILL);
+        }
         break;
     }
 
@@ -467,6 +496,11 @@ void vlapic_ipi(
         struct vcpu *v;
         bool_t batch = is_multicast_dest(vlapic, short_hand, dest, dest_mode);
 
+        if ( unlikely((icr_low & APIC_VECTOR_MASK) < 16) )
+        {
+            vlapic_error(vlapic, APIC_ESR_SENDILL);
+            break;
+        }
         if ( batch )
             cpu_raise_softirq_batch_begin();
         for_each_vcpu ( vlapic_domain(vlapic), v )
@@ -1353,6 +1387,8 @@ int vlapic_init(struct vcpu *v)
     if ( v->vcpu_id == 0 )
         vlapic->hw.apic_base_msr |= MSR_IA32_APICBASE_BSP;
 
+    spin_lock_init(&vlapic->esr_lock);
+
     tasklet_init(&vlapic->init_sipi.tasklet,
                  vlapic_init_sipi_action,
                  (unsigned long)v);
xen/include/asm-x86/hvm/vlapic.h +1 -0
@@ -77,6 +77,7 @@ struct vlapic {
         bool_t               hw, regs;
         uint32_t             id, ldr;
     }                        loaded;
+    spinlock_t               esr_lock;
     struct periodic_time     pt;
     s_time_t                 timer_last_update;
     struct page_info         *regs_page;

x86/vlapic: don't silently accept bad vectors

Vectors 0-15 are reserved, and a physical LAPIC - upon sending or receiving one - would generate an APIC error instead of doing the requested action. Make our emulation behave similarly.

xen/arch/x86/hvm/vlapic.c +37 -1
@@ -123,10 +123,34 @@ static int vlapic_find_highest_irr(struc
     return vlapic_find_highest_vector(&vlapic->regs->data[APIC_IRR]);
 }
 
+static void vlapic_error(struct vlapic *vlapic, unsigned int errmask)
+{
+    unsigned long flags;
+    uint32_t esr;
+
+    spin_lock_irqsave(&vlapic->esr_lock, flags);
+    esr = vlapic_get_reg(vlapic, APIC_ESR);
+    if ( (esr & errmask) != errmask )
+    {
+        uint32_t lvterr = vlapic_get_reg(vlapic, APIC_LVTERR);
+
+        vlapic_set_reg(vlapic, APIC_ESR, esr | errmask);
+        if ( !(lvterr & APIC_LVT_MASKED) )
+            vlapic_set_irq(vlapic, lvterr & APIC_VECTOR_MASK, 0);
+    }
+    spin_unlock_irqrestore(&vlapic->esr_lock, flags);
+}
+
 void vlapic_set_irq(struct vlapic *vlapic, uint8_t vec, uint8_t trig)
 {
     struct vcpu *target = vlapic_vcpu(vlapic);
 
+    if ( unlikely(vec < 16) )
+    {
+        vlapic_error(vlapic, APIC_ESR_RECVILL);
+        return;
+    }
+
     if ( trig )
         vlapic_set_vector(vec, &vlapic->regs->data[APIC_TMR]);
 
@@ -459,7 +483,12 @@ void vlapic_ipi(
         struct vlapic *target = vlapic_lowest_prio(
             vlapic_domain(vlapic), vlapic, short_hand, dest, dest_mode);
         if ( target != NULL )
-            vlapic_accept_irq(vlapic_vcpu(target), icr_low);
+        {
+            if ( likely((icr_low & APIC_VECTOR_MASK) >= 16) )
+                vlapic_accept_irq(vlapic_vcpu(target), icr_low);
+            else
+                vlapic_error(vlapic, APIC_ESR_SENDILL);
+        }
         break;
     }
 
@@ -467,6 +496,11 @@ void vlapic_ipi(
         struct vcpu *v;
         bool_t batch = is_multicast_dest(vlapic, short_hand, dest, dest_mode);
 
+        if ( unlikely((icr_low & APIC_VECTOR_MASK) < 16) )
+        {
+            vlapic_error(vlapic, APIC_ESR_SENDILL);
+            break;
+        }
         if ( batch )
             cpu_raise_softirq_batch_begin();
         for_each_vcpu ( vlapic_domain(vlapic), v )
@@ -1353,6 +1387,8 @@ int vlapic_init(struct vcpu *v)
     if ( v->vcpu_id == 0 )
         vlapic->hw.apic_base_msr |= MSR_IA32_APICBASE_BSP;
 
+    spin_lock_init(&vlapic->esr_lock);
+
     tasklet_init(&vlapic->init_sipi.tasklet,
                  vlapic_init_sipi_action,
                  (unsigned long)v);
xen/include/asm-x86/hvm/vlapic.h +1 -0
@@ -77,6 +77,7 @@ struct vlapic {
         bool_t               hw, regs;
         uint32_t             id, ldr;
     }                        loaded;
+    spinlock_t               esr_lock;
     struct periodic_time     pt;
     s_time_t                 timer_last_update;
     struct page_info         *regs_page;

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel