x86/vLAPIC: CMCI LVT handling
v2 cover x86/vLAPIC: CMCI LVT handling
1: add indirection to LVT handling
2: drop VLAPIC_VERSION
3: properly support the CMCI LVT
Jan
PATCH v2 1/3 x86/vLAPIC: add indirection to LVT handling
In preparation to add support for the CMCI LVT, which is discontiguous to the other LVTs, add a level of indirection. Rename the prior vlapic_lvt_mask[] while doing so (as subsequently a 2nd array will want adding, for use by guest_wrmsr_x2apic()).
---
The new name (lvt_valid[]) reflects its present contents. When re-based on top of "x86/hvm: vlapic: fix RO bits emulation in LVTx regs", the name wants to change to lvt_writable[] (or the 2nd array be added right away, with lvt_valid[] then used by guest_wrmsr_x2apic()). Alternatively the order of patches may want changing.
---
v2: Check incoming MAXLVT in lapic_check_regs().
xen/arch/x86/hvm/vlapic.c +43 -22
@@ -32,7 +32,16 @@#include <public/hvm/params.h> #define VLAPIC_VERSION 0x00050014 -#define VLAPIC_LVT_NUM 6 +#define LVT_BIAS (reg) (((reg) - APIC_LVTT) >> 4) + +#define LVTS \ + LVT(LVTT), LVT(LVTTHMR), LVT(LVTPC), LVT(LVT0), LVT(LVT1), LVT(LVTERR), + +static const unsigned int lvt_reg[] = { +#define LVT(which) APIC_ ## which + LVTS +#undef LVT +};#define LVT_MASK \ (APIC_LVT_MASKED | APIC_SEND_PENDING | APIC_VECTOR_MASK)
@@ -41,20 +50,21 @@ (LVT_MASK |APIC_DM_MASK |APIC_INPUT_POLARITY |\ APIC_LVT_REMOTE_IRR | APIC_LVT_LEVEL_TRIGGER) -static const unsigned int vlapic_lvt_mask[VLAPIC_LVT_NUM ] = +static const unsigned int lvt_valid[] = { -/* LVTT */ -LVT_MASK |APIC_TIMER_MODE_MASK , -/* LVTTHMR */ -LVT_MASK |APIC_DM_MASK , -/* LVTPC */ -LVT_MASK |APIC_DM_MASK , -/* LVT0-1 */ -LINT_MASK ,LINT_MASK , -/* LVTERR */ -LVT_MASK +#define LVTT_VALID (LVT_MASK | APIC_TIMER_MODE_MASK) +#define LVTTHMR_VALID (LVT_MASK | APIC_DM_MASK) +#define LVTPC_VALID (LVT_MASK | APIC_DM_MASK) +#define LVT0_VALID LINT_MASK +#define LVT1_VALID LINT_MASK +#define LVTERR_VALID LVT_MASK +#define LVT(which) [LVT_BIAS(APIC_ ## which)] = which ## _VALID + LVTS +#undef LVT }; +#undef LVTS + #define vlapic_lvtt_period(vlapic) \ ((vlapic_get_reg(vlapic, APIC_LVTT) & APIC_TIMER_MODE_MASK) \ == APIC_TIMER_MODE_PERIODIC)
@@ -827,16 +837,16 @@ void vlapic_reg_write(struct vcpu *v, unif ( !(val &APIC_SPIV_APIC_ENABLED ) ) { -int i; +unsigned int i, + nr =GET_APIC_MAXLVT (vlapic_get_reg (vlapic,APIC_LVR )) +1 ;uint32_t lvt_val; vlapic->hw.disabled |=VLAPIC_SW_DISABLED ; -for ( i =0 ; i <VLAPIC_LVT_NUM ; i++ ) +for ( i =0 ; i < nr; i++ ) { - lvt_val =vlapic_get_reg (vlapic,APIC_LVTT +0x10 * i); -vlapic_set_reg (vlapic,APIC_LVTT +0x10 * i, - lvt_val |APIC_LVT_MASKED ); + lvt_val =vlapic_get_reg (vlapic, lvt_reg[i]); +vlapic_set_reg (vlapic, lvt_reg[i], lvt_val |APIC_LVT_MASKED ); } }else
@@ -878,7 +888,7 @@ void vlapic_reg_write(struct vcpu *v, uncase APIC_LVTERR :/* LVT Error Reg */ if (vlapic_sw_disabled (vlapic) ) val |=APIC_LVT_MASKED ; - val &=array_access_nospec (vlapic_lvt_mask, (reg -APIC_LVTT ) >>4 ); + val &=array_access_nospec (lvt_valid,LVT_BIAS (reg));vlapic_set_reg (vlapic, reg, val);if ( reg ==APIC_LVT0 ) {
@@ -1424,7 +1434,7 @@ bool is_vlapic_lvtpc_enabled(struct vlap/* Reset the VLAPIC back to its init state. */ static void vlapic_do_init (struct vlapic *vlapic) { -int i; +unsigned int i, nr;if ( !has_vlapic (vlapic_vcpu (vlapic)->domain) )return ;
@@ -1452,8 +1462,9 @@ static void vlapic_do_init(struct vlapicvlapic_set_reg (vlapic,APIC_DFR ,0xffffffffU ); -for ( i =0 ; i <VLAPIC_LVT_NUM ; i++ ) -vlapic_set_reg (vlapic,APIC_LVTT +0x10 * i,APIC_LVT_MASKED ); + nr =GET_APIC_MAXLVT (vlapic_get_reg (vlapic,APIC_LVR )) +1 ; +for ( i =0 ; i < nr; i++ ) +vlapic_set_reg (vlapic, lvt_reg[i],APIC_LVT_MASKED );vlapic_set_reg (vlapic,APIC_SPIV ,0xff ); vlapic->hw.disabled |=VLAPIC_SW_DISABLED ;
@@ -1639,13 +1650,23 @@ static int cf_check lapic_check_regs(con {unsigned int vcpuid =hvm_load_instance (h);int rc; +const struct hvm_hw_lapic_regs *regs;if ( (rc =lapic_check_common (d, vcpuid)) )return rc; -if ( !hvm_get_entry (LAPIC_REGS , h) ) + regs =hvm_get_entry (LAPIC_REGS , h); +if ( !regs )return -ENODATA ; +/* Like vlapic_get_reg(), but without having a struct vlapic instance. */ +#define GET_REG (reg) (*(const uint32_t *)®s->data[reg]) + +if (GET_APIC_MAXLVT (GET_REG (APIC_LVR )) >=ARRAY_SIZE (lvt_reg) ) +return -ERANGE ; + +#undef GET_REG +return 0 ; }
PATCH v2 2/3 x86/vLAPIC: drop VLAPIC_VERSION
In reply to Jan BeulichIn preparation of making the value somewhat dynamic drop the constant. Replace its use in guest_wrmsr_x2apic() by actually fetching the LVR value.
---
v2: Use MASK_INSR() right here.
xen/arch/x86/hvm/vlapic.c +4 -3
@@ -31,7 +31,6 @@#include <public/hvm/ioreq.h> #include <public/hvm/params.h> -#define VLAPIC_VERSION 0x00050014#define LVT_BIAS (reg) (((reg) - APIC_LVTT) >> 4)#define LVTS \
@@ -1015,7 +1014,7 @@ int guest_wrmsr_x2apic(struct vcpu *v, ucase APIC_SPIV :if ( val & ~(APIC_VECTOR_MASK |APIC_SPIV_APIC_ENABLED |APIC_SPIV_FOCUS_DISABLED | - (VLAPIC_VERSION &APIC_LVR_DIRECTED_EOI + (vlapic_get_reg (vlapic,APIC_LVR ) &APIC_LVR_DIRECTED_EOI ?APIC_SPIV_DIRECTED_EOI :0 )) )return X86EMUL_EXCEPTION ;break ;
@@ -1439,7 +1438,9 @@ static void vlapic_do_init(struct vlapicif ( !has_vlapic (vlapic_vcpu (vlapic)->domain) )return ; -vlapic_set_reg (vlapic,APIC_LVR ,VLAPIC_VERSION ); +vlapic_set_reg (vlapic,APIC_LVR , +MASK_INSR (0x14 ,APIC_LVR_VERSION_MASK ) | +MASK_INSR (ARRAY_SIZE (lvt_reg) -1 ,APIC_LVR_MAXLVT_MASK ));for ( i =0 ; i <8 ; i++ ) {
xen/arch/x86/include/asm/apicdef.h +4 -1
@@ -15,7 +15,10 @@#define GET_xAPIC_ID (x) (((x)>>24)&0xFFu)#define SET_xAPIC_ID (x) (((x)<<24))#define APIC_LVR 0x30 -#define APIC_LVR_MASK 0xFF00FF +#define APIC_LVR_VERSION_MASK 0xff +#define APIC_LVR_MAXLVT_MASK 0xff0000 +#define APIC_LVR_MASK (APIC_LVR_VERSION_MASK | \ + APIC_LVR_MAXLVT_MASK)#define APIC_LVR_DIRECTED_EOI (1 << 24)#define GET_APIC_VERSION (x) ((x)&0xFF)#define GET_APIC_MAXLVT (x) (((x)>>16)&0xFF)
PATCH v2 3/3 x86/vLAPIC: properly support the CMCI LVT
In reply to Jan BeulichRather than unconditionally accepting reads and writes while discarding the value written, make accesses properly conditional upon CMCI being exposed via MCG_CAP, and arrange to actually retain the value written. Also reflect the extra LVT in LVR.
Note that this doesn't change the status quo of us never delivering any interrupt through this LVT.
---
The Fixes: tags are referencing where the explicit mentioning of APIC_CMCI in what are now guest_{rd,wr}msr_x2apic() was introduced; the mis-handling really pre-dates that, though.
In principle the later assignment to "nr" in vlapic_do_init() could now be dropped again. I wasn't quite sure though whether that's a good idea.
---
v2: Introduce vlapic_has_cmci(). Re-base over change to earlier patch.
xen/arch/x86/hvm/vlapic.c +38 -5
@@ -31,10 +31,19 @@#include <public/hvm/ioreq.h> #include <public/hvm/params.h> -#define LVT_BIAS (reg) (((reg) - APIC_LVTT) >> 4) +#include <../cpu/mcheck/x86_mca.h> /* MCG_CMCI_P */ + +#define LVT_BIAS (reg) (((reg) - APIC_CMCI) >> 4)#define LVTS \ - LVT(LVTT), LVT(LVTTHMR), LVT(LVTPC), LVT(LVT0), LVT(LVT1), LVT(LVTERR), + LVT(LVTT), LVT(LVTTHMR), LVT(LVTPC), LVT(LVT0), LVT(LVT1), LVT(LVTERR), \ + LVT(CMCI), + +enum { +#define LVT(which) VLAPIC_LVT_ ## which + LVTS +#undef LVT +};static const unsigned int lvt_reg[] = { #define LVT(which) APIC_ ## which
@@ -57,6 +66,7 @@ static const unsigned int lvt_valid[] =#define LVT0_VALID LINT_MASK#define LVT1_VALID LINT_MASK#define LVTERR_VALID LVT_MASK +#define CMCI_VALID (LVT_MASK | APIC_DM_MASK)#define LVT (which) [LVT_BIAS(APIC_ ## which)] = which ## _VALIDLVTS #undef LVT
@@ -78,6 +88,11 @@ static const unsigned int lvt_valid[] =static void vlapic_do_init (struct vlapic *vlapic); +static bool vlapic_has_cmci (const struct vlapic *vlapic) +{ +return GET_APIC_MAXLVT (vlapic_get_reg (vlapic,APIC_LVR )) >=VLAPIC_LVT_CMCI ; +} +static int vlapic_find_highest_vector (const void *bitmap) {const uint32_t *word = bitmap;
@@ -697,8 +712,17 @@ int guest_rdmsr_x2apic(const struct vcpureturn X86EMUL_EXCEPTION ; offset = reg <<4 ; -if ( offset ==APIC_ICR ) +switch ( offset ) + { +case APIC_ICR : high = (uint64_t )vlapic_read_aligned (vlapic,APIC_ICR2 ) <<32 ; +break ; + +case APIC_CMCI : +if ( !vlapic_has_cmci (vlapic) ) +return X86EMUL_EXCEPTION ; +break ; + } *val = high |vlapic_read_aligned (vlapic, offset);
@@ -868,6 +892,10 @@ void vlapic_reg_write(struct vcpu *v, unvlapic_set_reg (vlapic,APIC_ICR2 , val &0xff000000U );break ; +case APIC_CMCI :/* LVT CMCI */ +if ( !vlapic_has_cmci (vlapic) ) +break ; + fallthrough;case APIC_LVTT :/* LVT Timer Reg */ if (vlapic_lvtt_tdt (vlapic) != ((val &APIC_TIMER_MODE_MASK ) ==APIC_TIMER_MODE_TSC_DEADLINE ) )
@@ -1024,9 +1052,12 @@ int guest_wrmsr_x2apic(struct vcpu *v, ureturn X86EMUL_EXCEPTION ;break ; +case APIC_CMCI : +if ( !vlapic_has_cmci (vlapic) ) +return X86EMUL_EXCEPTION ; + fallthrough;case APIC_LVTTHMR :case APIC_LVTPC : -case APIC_CMCI :if ( val & ~(LVT_MASK |APIC_DM_MASK ) )return X86EMUL_EXCEPTION ;break ;
@@ -1438,9 +1469,11 @@ static void vlapic_do_init(struct vlapicif ( !has_vlapic (vlapic_vcpu (vlapic)->domain) )return ; + nr =ARRAY_SIZE (lvt_reg) - + !(vlapic_vcpu (vlapic)->arch.vmce.mcg_cap &MCG_CMCI_P );vlapic_set_reg (vlapic,APIC_LVR ,MASK_INSR (0x14 ,APIC_LVR_VERSION_MASK ) | -MASK_INSR (ARRAY_SIZE (lvt_reg) -1 ,APIC_LVR_MAXLVT_MASK )); +MASK_INSR (nr -1 ,APIC_LVR_MAXLVT_MASK ));for ( i =0 ; i <8 ; i++ ) {