x86/hvm: Disallow moving the APIC MMIO window
PATCH 1/1 x86/hvm: Disallow moving the APIC MMIO window
See the code comment for a full discussion, but in short: guests which currently run under Xen don't move the window, because moving it has never worked properly. Implementing support for moving the window is never going to work architecturally unless we switch to per-vcpu P2Ms (which seems very unlikely), and would still be a substantial quantity of work for a feature which is unused in practice.
Take the opportunity to rename vlapic_msr_set() to be consistent with the other MSR handling functions, and return X86EMUL_* constants. Move the guest_{rd,wr}msr_x2apic() declarations into vlapic.h which is a more appropriate place for them to live.
---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
--- xen/arch/x86/hvm/hvm.c | 4 +-- xen/arch/x86/hvm/vlapic.c | 60 ++++++++++++++++++++++++++++++++++++---- xen/include/asm-x86/hvm/hvm.h | 3 -- xen/include/asm-x86/hvm/vlapic.h | 5 +++- 4 files changed, 59 insertions(+), 13 deletions(-)
xen/arch/x86/hvm/hvm.c +1 -3
@@ -3565,9 +3565,7 @@ int hvm_msr_write_intercept(unsigned int msr, uint64_t msr_content,break ;case MSR_APIC_BASE : -if ( !vlapic_msr_set (vcpu_vlapic (v), msr_content) ) - goto gp_fault; -break ; +return guest_wrmsr_apic_base (v, msr_content);case MSR_IA32_TSC_DEADLINE :vlapic_tdt_msr_set (vcpu_vlapic (v), msr_content);
xen/arch/x86/hvm/vlapic.c +54 -6
83 lines not on this page · raw message
xen/include/asm-x86/hvm/hvm.h +0 -3
@@ -333,9 +333,6 @@ void hvm_toggle_singlestep(struct vcpu *v);int hvm_hap_nested_page_fault (paddr_t gpa,unsigned long gla,struct npfec npfec); -int guest_rdmsr_x2apic (const struct vcpu *v,uint32_t msr,uint64_t *val); -int guest_wrmsr_x2apic (struct vcpu *v,uint32_t msr,uint64_t val); -/* Check CR4/EFER values */ const char *hvm_efer_valid (const struct vcpu *v,uint64_t value,signed int cr0_pg);
xen/include/asm-x86/hvm/vlapic.h +4 -1
@@ -123,7 +123,10 @@ void vlapic_destroy(struct vcpu *v);void vlapic_reset (struct vlapic *vlapic); -bool_t vlapic_msr_set (struct vlapic *vlapic,uint64_t value); +int guest_wrmsr_apic_base (struct vcpu *v,uint64_t val); +int guest_rdmsr_x2apic (const struct vcpu *v,uint32_t msr,uint64_t *val); +int guest_wrmsr_x2apic (struct vcpu *v,uint32_t msr,uint64_t val); +void vlapic_tdt_msr_set (struct vlapic *vlapic,uint64_t value);uint64_t vlapic_tdt_msr_get (struct vlapic *vlapic);
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 84 lines · Andrew Cooper
See the code comment for a full discussion, but in short: guests which currently run under Xen don't move the window, because moving it has never worked properly. Implementing support for moving the window is never going to work architecturally unless we switch to per-vcpu P2Ms (which seems very unlikely), and would still be a substantial quantity of work for a feature which is unused in practice.
Take the opportunity to rename
vlapic_msr_set()to be consistent with the other MSR handling functions, and return X86EMUL_* constants. Move the guest_{rd,wr}msr_x2apic() declarations intovlapic.hwhich is a more appropriate place for them to live.Signed-off-by Andrew Cooper <[email protected]>---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>--- xen/arch/x86/hvm/hvm.c | 4 +-- xen/arch/x86/hvm/vlapic.c | 60 ++++++++++++++++++++++++++++++++++++---- xen/include/asm-x86/hvm/hvm.h | 3 -- xen/include/asm-x86/hvm/vlapic.h | 5 +++- 4 files changed, 59 insertions(+), 13 deletions(-)xen/arch/x86/hvm/hvm.c +1
-3@@ -3565,9 +3565,7 @@ int hvm_msr_write_intercept(unsigned int msr, uint64_t msr_content,break ;case MSR_APIC_BASE : -if ( !vlapic_msr_set (vcpu_vlapic (v), msr_content) ) - goto gp_fault; -break ; +return guest_wrmsr_apic_base (v, msr_content);case MSR_IA32_TSC_DEADLINE :vlapic_tdt_msr_set (vcpu_vlapic (v), msr_content);xen/arch/x86/hvm/vlapic.c +33
-3@@ -1072,15 +1072,63 @@ static void set_x2apic_id(struct vlapic *vlapic)vlapic_set_reg (vlapic,APIC_LDR , ldr); } -bool_t vlapic_msr_set (struct vlapic *vlapic,uint64_t value) +int guest_wrmsr_apic_base (struct vcpu *v,uint64_t value) { -if ( !has_vlapic (vlapic_domain (vlapic)) ) -return 0 ; +struct vlapic *vlapic =vcpu_vlapic (v); + +if ( !has_vlapic (v->domain) ) +return X86EMUL_EXCEPTION ; + +/* +* Architecturally speaking, we should allow a guest to move the xAPIC +* MMIO window (within reason - not even hardware allows arbitrary +* positions). However, virtualising the behaviour for multi-vcpu guests +* is problematic. +* +* The ability to move the MMIO window was introduced with the Pentium Pro +* processor, to deconflict the window with other MMIO in the system. The +* need to move the MMIO window was obsoleted by the Netburst architecture +* which reserved the space in physical address space for MSIs. +* +* As such, it appears to be a rarely used feature before the turn of the +* millennium, and entirely unused after. +* +* Xen uses a per-domain P2M, but MSR_APIC_BASE is per-vcpu. In +* principle, we could emulate the MMIO windows being in different +* locations by ensuring that all windows are unmapped in the P2M and trap +* for emulation. Xen has never had code to modify the P2M in response to +* APIC_BASE updates, so guests which actually try this are likely to end +* up without a working APIC. +* +* Things are more complicated with hardware APIC acceleration, where Xen +* has to map a sink-page into the P2M for APIC accesses to be recognised +* and accelerated by microcode. Again, this could in principle be +* emulated, but the visible result in the guest would be multiple working +* APIC MMIO windows. Moving the APIC window has never caused the +* sink-page to move in the P2M, meaning that on all modern hardware, the
xen/arch/x86/hvm/vlapic.c static void set_x2apic_id(struct vlapic *vlapic) · line 1118
Quoted 10 lines
xen/arch/x86/hvm/vlapic.c +10
-0+* APIC definitely ceases working if the guest tries to move the window. +* +* As such, when the APIC is configured in xAPIC mode, require the MMIO +* window to be in its default location. We don't expect any guests which +* currently run on Xen to be impacted by this restriction, and the #GP +* fault will be far more obvious to debug than a malfunctioning MMIO +* window. +*/ + if ( ((value & (APIC_BASE_EXTD | APIC_BASE_ENABLE)) == APIC_BASE_ENABLE) && + ((value & ~(APIC_BASE_BSP | APIC_BASE_EXTD | APIC_BASE_ENABLE)) !=
You could use MSR_IA32_APICBASE_BASE here AFAICT.
xen/arch/x86/hvm/vlapic.c static void set_x2apic_id(struct vlapic *vlapic) · line 1123
Quoted 5 lines
xen/arch/x86/hvm/vlapic.c +5
-0+ APIC_DEFAULT_PHYS_BASE) ) + { +dprintk (XENLOG_INFO , +"%pv attempted to move the APIC MMIO window to 0x%08" PRIx64"\n" , + v, value);
I think you should use PAGE_MASK, or else the message is misleading because you are actually printing the MSR value, which apart from the address also contains flags in the low 12 bits.
Thanks, Roger.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 88 lines · Roger Pau Monné
Quoted 87 lines · Andrew Cooper
See the code comment for a full discussion, but in short: guests which currently run under Xen don't move the window, because moving it has never worked properly. Implementing support for moving the window is never going to work architecturally unless we switch to per-vcpu P2Ms (which seems very unlikely), and would still be a substantial quantity of work for a feature which is unused in practice.
Take the opportunity to rename
vlapic_msr_set()to be consistent with the other MSR handling functions, and return X86EMUL_* constants. Move the guest_{rd,wr}msr_x2apic() declarations intovlapic.hwhich is a more appropriate place for them to live.Signed-off-by Andrew Cooper <[email protected]>---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>--- xen/arch/x86/hvm/hvm.c | 4 +-- xen/arch/x86/hvm/vlapic.c | 60 ++++++++++++++++++++++++++++++++++++---- xen/include/asm-x86/hvm/hvm.h | 3 -- xen/include/asm-x86/hvm/vlapic.h | 5 +++- 4 files changed, 59 insertions(+), 13 deletions(-)xen/arch/x86/hvm/hvm.c +1
-3@@ -3565,9 +3565,7 @@ int hvm_msr_write_intercept(unsigned int msr, uint64_t msr_content,break ;case MSR_APIC_BASE : -if ( !vlapic_msr_set (vcpu_vlapic (v), msr_content) ) - goto gp_fault; -break ; +return guest_wrmsr_apic_base (v, msr_content);case MSR_IA32_TSC_DEADLINE :vlapic_tdt_msr_set (vcpu_vlapic (v), msr_content);xen/arch/x86/hvm/vlapic.c +36
-3@@ -1072,15 +1072,63 @@ static void set_x2apic_id(struct vlapic *vlapic)vlapic_set_reg (vlapic,APIC_LDR , ldr); } -bool_t vlapic_msr_set (struct vlapic *vlapic,uint64_t value) +int guest_wrmsr_apic_base (struct vcpu *v,uint64_t value) { -if ( !has_vlapic (vlapic_domain (vlapic)) ) -return 0 ; +struct vlapic *vlapic =vcpu_vlapic (v); + +if ( !has_vlapic (v->domain) ) +return X86EMUL_EXCEPTION ; + +/* +* Architecturally speaking, we should allow a guest to move the xAPIC +* MMIO window (within reason - not even hardware allows arbitrary +* positions). However, virtualising the behaviour for multi-vcpu guests +* is problematic. +* +* The ability to move the MMIO window was introduced with the Pentium Pro +* processor, to deconflict the window with other MMIO in the system. The +* need to move the MMIO window was obsoleted by the Netburst architecture +* which reserved the space in physical address space for MSIs. +* +* As such, it appears to be a rarely used feature before the turn of the +* millennium, and entirely unused after. +* +* Xen uses a per-domain P2M, but MSR_APIC_BASE is per-vcpu. In +* principle, we could emulate the MMIO windows being in different +* locations by ensuring that all windows are unmapped in the P2M and trap +* for emulation. Xen has never had code to modify the P2M in response to +* APIC_BASE updates, so guests which actually try this are likely to end +* up without a working APIC. +* +* Things are more complicated with hardware APIC acceleration, where Xen +* has to map a sink-page into the P2M for APIC accesses to be recognised +* and accelerated by microcode. Again, this could in principle be +* emulated, but the visible result in the guest would be multiple working +* APIC MMIO windows. Moving the APIC window has never caused the +* sink-page to move in the P2M, meaning that on all modern hardware, the +* APIC definitely ceases working if the guest tries to move the window. +* +* As such, when the APIC is configured in xAPIC mode, require the MMIO
Quoted 8 lines
xen/arch/x86/hvm/vlapic.c static void set_x2apic_id(struct vlapic *vlapic) · line 1118
Quoted 7 lines
xen/arch/x86/hvm/vlapic.c +7
-0+* window to be in its default location. We don't expect any guests which +* currently run on Xen to be impacted by this restriction, and the #GP +* fault will be far more obvious to debug than a malfunctioning MMIO +* window. +*/ + if ( ((value & (APIC_BASE_EXTD | APIC_BASE_ENABLE)) == APIC_BASE_ENABLE) && + ((value & ~(APIC_BASE_BSP | APIC_BASE_EXTD | APIC_BASE_ENABLE)) !=You could use
MSR_IA32_APICBASE_BASEhere AFAICT.
That no longer exists in staging, but either way, that would be buggy. I suppose I should split the actually-reserved bit checking out into an earlier check. Nothing at the moment prevents the guest from setting the reserved bits.
Quoted 9 lines
xen/arch/x86/hvm/vlapic.c static void set_x2apic_id(struct vlapic *vlapic) · line 1123
Quoted 6 lines
xen/arch/x86/hvm/vlapic.c +5
-0+ APIC_DEFAULT_PHYS_BASE) ) + { +dprintk (XENLOG_INFO , +"%pv attempted to move the APIC MMIO window to 0x%08" PRIx64"\n" , + v, value);I think you should use
PAGE_MASK, or else the message is misleading because you are actually printing the MSR value, which apart from the address also contains flags in the low 12 bits.
Printing the lower metadata is important for diagnostics, even if only to double check the logic leading up to the printk(). (Also, I note it should be XENLOG_G_INFO)
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 98 lines · Andrew Cooper
Quoted 88 lines · Roger Pau Monné
Quoted 87 lines · Andrew Cooper
See the code comment for a full discussion, but in short: guests which currently run under Xen don't move the window, because moving it has never worked properly. Implementing support for moving the window is never going to work architecturally unless we switch to per-vcpu P2Ms (which seems very unlikely), and would still be a substantial quantity of work for a feature which is unused in practice.
Take the opportunity to rename
vlapic_msr_set()to be consistent with the other MSR handling functions, and return X86EMUL_* constants. Move the guest_{rd,wr}msr_x2apic() declarations intovlapic.hwhich is a more appropriate place for them to live.Signed-off-by Andrew Cooper <[email protected]>---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>--- xen/arch/x86/hvm/hvm.c | 4 +-- xen/arch/x86/hvm/vlapic.c | 60 ++++++++++++++++++++++++++++++++++++---- xen/include/asm-x86/hvm/hvm.h | 3 -- xen/include/asm-x86/hvm/vlapic.h | 5 +++- 4 files changed, 59 insertions(+), 13 deletions(-)xen/arch/x86/hvm/hvm.c +1
-3@@ -3565,9 +3565,7 @@ int hvm_msr_write_intercept(unsigned int msr, uint64_t msr_content,break ;case MSR_APIC_BASE : -if ( !vlapic_msr_set (vcpu_vlapic (v), msr_content) ) - goto gp_fault; -break ; +return guest_wrmsr_apic_base (v, msr_content);case MSR_IA32_TSC_DEADLINE :vlapic_tdt_msr_set (vcpu_vlapic (v), msr_content);xen/arch/x86/hvm/vlapic.c +36
-3@@ -1072,15 +1072,63 @@ static void set_x2apic_id(struct vlapic *vlapic)vlapic_set_reg (vlapic,APIC_LDR , ldr); } -bool_t vlapic_msr_set (struct vlapic *vlapic,uint64_t value) +int guest_wrmsr_apic_base (struct vcpu *v,uint64_t value) { -if ( !has_vlapic (vlapic_domain (vlapic)) ) -return 0 ; +struct vlapic *vlapic =vcpu_vlapic (v); + +if ( !has_vlapic (v->domain) ) +return X86EMUL_EXCEPTION ; + +/* +* Architecturally speaking, we should allow a guest to move the xAPIC +* MMIO window (within reason - not even hardware allows arbitrary +* positions). However, virtualising the behaviour for multi-vcpu guests +* is problematic. +* +* The ability to move the MMIO window was introduced with the Pentium Pro +* processor, to deconflict the window with other MMIO in the system. The +* need to move the MMIO window was obsoleted by the Netburst architecture +* which reserved the space in physical address space for MSIs. +* +* As such, it appears to be a rarely used feature before the turn of the +* millennium, and entirely unused after. +* +* Xen uses a per-domain P2M, but MSR_APIC_BASE is per-vcpu. In +* principle, we could emulate the MMIO windows being in different +* locations by ensuring that all windows are unmapped in the P2M and trap +* for emulation. Xen has never had code to modify the P2M in response to +* APIC_BASE updates, so guests which actually try this are likely to end +* up without a working APIC. +* +* Things are more complicated with hardware APIC acceleration, where Xen +* has to map a sink-page into the P2M for APIC accesses to be recognised +* and accelerated by microcode. Again, this could in principle be +* emulated, but the visible result in the guest would be multiple working +* APIC MMIO windows. Moving the APIC window has never caused the +* sink-page to move in the P2M, meaning that on all modern hardware, the +* APIC definitely ceases working if the guest tries to move the window. +* +* As such, when the APIC is configured in xAPIC mode, require the MMIO Quoted 8 lines
xen/arch/x86/hvm/vlapic.c static void set_x2apic_id(struct vlapic *vlapic) · line 1118
Quoted 7 lines
xen/arch/x86/hvm/vlapic.c +7
-0+* window to be in its default location. We don't expect any guests which +* currently run on Xen to be impacted by this restriction, and the #GP +* fault will be far more obvious to debug than a malfunctioning MMIO +* window. +*/ + if ( ((value & (APIC_BASE_EXTD | APIC_BASE_ENABLE)) == APIC_BASE_ENABLE) && + ((value & ~(APIC_BASE_BSP | APIC_BASE_EXTD | APIC_BASE_ENABLE)) !=You could use
MSR_IA32_APICBASE_BASEhere AFAICT.
Quoted 4 lines
That no longer exists in staging, but either way, that would be buggy. I suppose I should split the actually-reserved bit checking out into an earlier check. Nothing at the moment prevents the guest from setting the reserved bits.
Oh, I was looking at an old version of the tree, that's APIC_BASE_ADDR_MASK now.
Moving the reserved bit checking vs the MMIO window movement would be better.
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
xen/arch/x86/hvm/vlapic.c +5
-0+ APIC_DEFAULT_PHYS_BASE) ) + { +dprintk (XENLOG_INFO , +"%pv attempted to move the APIC MMIO window to 0x%08" PRIx64"\n" , + v, value);
Quoted 7 lines
Quoted 3 lines
I think you should use
PAGE_MASK, or else the message is misleading because you are actually printing the MSR value, which apart from the address also contains flags in the low 12 bits.Printing the lower metadata is important for diagnostics, even if only to double check the logic leading up to the printk(). (Also, I note it should be
XENLOG_G_INFO)
I agree, but I think the message is kind of misleading because you are not printing an address but the attempted MSR write value, that was the point I was trying to make with my comment. I would reword to "attempted to move the APIC MMIO window, MSR write: ..." or something similar.
Thanks, Roger.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH v2 1/1 x86/hvm: Disallow moving the APIC MMIO window
In reply to Andrew CooperSee the code comment for a full discussion, but in short: guests which currently run under Xen don't move the window, because moving it has never worked properly. Implementing support for moving the window is never going to work architecturally unless we switch to per-vcpu P2Ms (which seems very unlikely), and would still be a substantial quantity of work for a feature which is unused in practice.
Take the opportunity to rename vlapic_msr_set() to be consistent with the other MSR handling functions, and return X86EMUL_* constants. Add logic to check for reserved bits, including refusing x2APIC mode if it has not been offered to the guest. Move the guest_{rd,wr}msr_x2apic() declarations into vlapic.h which is a more appropriate place for them to live.
---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
v2:
* Split reserved bit checking out into a separate conditional
* Adjust printk wording
--- xen/arch/x86/hvm/hvm.c | 4 +-- xen/arch/x86/hvm/vlapic.c | 65 ++++++++++++++++++++++++++++++++++++---- xen/include/asm-x86/hvm/hvm.h | 3 -- xen/include/asm-x86/hvm/vlapic.h | 5 +++- 4 files changed, 64 insertions(+), 13 deletions(-)
xen/arch/x86/hvm/hvm.c +1 -3
@@ -3565,9 +3565,7 @@ int hvm_msr_write_intercept(unsigned int msr, uint64_t msr_content,break ;case MSR_APIC_BASE : -if ( !vlapic_msr_set (vcpu_vlapic (v), msr_content) ) - goto gp_fault; -break ; +return guest_wrmsr_apic_base (v, msr_content);case MSR_IA32_TSC_DEADLINE :vlapic_tdt_msr_set (vcpu_vlapic (v), msr_content);
xen/arch/x86/hvm/vlapic.c +59 -6
88 lines not on this page · raw message
xen/include/asm-x86/hvm/hvm.h +0 -3
@@ -333,9 +333,6 @@ void hvm_toggle_singlestep(struct vcpu *v);int hvm_hap_nested_page_fault (paddr_t gpa,unsigned long gla,struct npfec npfec); -int guest_rdmsr_x2apic (const struct vcpu *v,uint32_t msr,uint64_t *val); -int guest_wrmsr_x2apic (struct vcpu *v,uint32_t msr,uint64_t val); -/* Check CR4/EFER values */ const char *hvm_efer_valid (const struct vcpu *v,uint64_t value,signed int cr0_pg);
xen/include/asm-x86/hvm/vlapic.h +4 -1
@@ -123,7 +123,10 @@ void vlapic_destroy(struct vcpu *v);void vlapic_reset (struct vlapic *vlapic); -bool_t vlapic_msr_set (struct vlapic *vlapic,uint64_t value); +int guest_wrmsr_apic_base (struct vcpu *v,uint64_t val); +int guest_rdmsr_x2apic (const struct vcpu *v,uint32_t msr,uint64_t *val); +int guest_wrmsr_x2apic (struct vcpu *v,uint32_t msr,uint64_t val); +void vlapic_tdt_msr_set (struct vlapic *vlapic,uint64_t value);uint64_t vlapic_tdt_msr_get (struct vlapic *vlapic);
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 7 lines · Andrew Cooper
See the code comment for a full discussion, but in short: guests which currently run under Xen don't move the window, because moving it has never worked properly. Implementing support for moving the window is never going to work architecturally unless we switch to per-vcpu P2Ms (which seems very unlikely), and would still be a substantial quantity of work for a feature which is unused in practice.
Quoted 7 lines
Take the opportunity to rename
vlapic_msr_set()to be consistent with the other MSR handling functions, and return X86EMUL_* constants. Add logic to check for reserved bits, including refusing x2APIC mode if it has not been offered to the guest. Move the guest_{rd,wr}msr_x2apic() declarations intovlapic.hwhich is a more appropriate place for them to live.Signed-off-by Andrew Cooper <[email protected]>
Thanks.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 10.12.18 at 18:04, <[email protected]> wrote:
See the code comment for a full discussion, but in short: guests which currently run under Xen don't move the window, because moving it has never worked properly. Implementing support for moving the window is never going to work architecturally unless we switch to per-vcpu P2Ms (which seems very unlikely), and would still be a substantial quantity of work for a feature which is unused in practice.
Quoted 7 lines
Take the opportunity to rename
vlapic_msr_set()to be consistent with the other MSR handling functions, and return X86EMUL_* constants. Add logic to check for reserved bits, including refusing x2APIC mode if it has not been offered to the guest. Move the guest_{rd,wr}msr_x2apic() declarations intovlapic.hwhich is a more appropriate place for them to live.Signed-off-by Andrew Cooper <[email protected]>
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel