kexec: disable iommu and x2apic before jumping into the kdump kernel
PATCH 1/1 kexec: disable iommu and x2apic before jumping into the kdump kernel
This is a massive hack but is better than the code currently in place. Some functions have been duped into a crash_* version to separate them from the regular shutdown path, where disabling these subsystems is irrelevant.
xen/arch/x86/apic.c +36 -0
@@ -344,6 +344,42 @@ } } +/* Customised version of disable_local_APIC which is only called on the kexec crash path. +* It is a brutal hack and only here untill the APIC code is overhauled. We NEED to swtich +* out of x2apic mode whatever the case as the kdump kernel cant cope due to the MMIO <-> MSR +* swap invovled with x2apic mode. +*/ +void crash_disable_local_APIC (void ) +{ +uint64_t msr_content; +clear_local_APIC (); + +apic_write_around (APIC_SPIV , +apic_read (APIC_SPIV ) & ~APIC_SPIV_APIC_ENABLED ); + +rdmsrl (MSR_IA32_APICBASE , msr_content); + +/* This is the only way to exit x2apic mode. Trying to disable x2apic mode while staying +* xapic mode will result in a protection fault which leads to a general protection fault +* as this is on the fault handler codepath. +*/ + msr_content &= ~(MSR_IA32_APICBASE_ENABLE |MSR_IA32_APICBASE_EXTD ); +wrmsrl (MSR_IA32_APICBASE , msr_content); + +/* In most cases, we should leave the lapic in xapic mode as this is the default state on +* CPU reset. However, some bioses specifically disable xapic mode for hilarity sake so +* we should follow suit so the kdump kernel does not get confused about mismatching ACPI +* tables. +*/ +if ( ! enabled_via_apicbase) { +/* If we did not manually enable xapic mode, I.E. the bios left it enabled, +* lets follow suit and leave it enabled for the kdump kernel. +*/ +wrmsrl (MSR_IA32_APICBASE , msr_content |MSR_IA32_APICBASE_ENABLE ); + } +} + +/* * This is to verify that we're looking at a real local APIC. * Check these against your board if the CPUs aren't getting
xen/arch/x86/crash.c +32 -2
@@ -27,6 +27,8 @@#include <asm/hvm/support.h> #include <asm/apic.h> #include <asm/io_apic.h> +#include <xen/iommu.h> +#include <asm/hvm/iommu.h> static atomic_t waiting_for_crash_ipi;static unsigned int crashing_cpu;
@@ -43,7 +45,12 @@kexec_crash_save_cpu (); -__stop_this_cpu (); +crash_disable_local_APIC (); +hvm_cpu_down (); +/* Clear FPU, zapping any pending exceptions. Needed for warm reset with +* some BIOSes.*/ +clts (); + asmvolatile ("fninit" );atomic_dec (&waiting_for_crash_ipi);
@@ -77,7 +84,15 @@ msecs--; } -__stop_this_cpu (); +crash_disable_local_APIC (); +hvm_cpu_down (); +/* Clear FPU, zapping any pending exceptions. Needed for warm reset with +* some BIOSes.*/ +clts (); + asmvolatile ("fninit" ); + + x2apic_enabled =0 ; +disable_IO_APIC ();local_irq_enable ();
@@ -86,9 +101,24 @@void machine_crash_shutdown (void ) {crash_xen_info_t *info; +const struct iommu_ops * ops;nmi_shootdown_cpus (); +local_irq_disable (); + +/* Yes i know this is hacky but it is the easiest solution. I should add an iommu_ops +* function called crash() or so which just disables the iommu 'fun' without saving state +*/ + ops =iommu_get_ops (); + ops->suspend (); + +/* Yes i know this is from driver/passthrough/vtd/ but it appears to be architecture +* independant, and also bears little/no relation to x2apic. Needs cleaning up +*/ +iommu_disable_x2apic_IR (); +local_irq_enable (); + info =kexec_crash_save_info (); info->xen_phys_start = xen_phys_start; info->dom0_pfn_to_mfn_frame_list_list =
xen/arch/x86/hpet.c +28 -0
@@ -670,6 +670,34 @@smp_send_event_check_mask (&cpu_online_map); } +void crash_hpet_disable_legacy_broadcast (void ) +{ +u32 cfg; + +if ( !hpet_events || !(hpet_events->flags &HPET_EVT_LEGACY ) ) +return ; + +/* By this point in the crash handler, we are the only CPU still going, +* so the lock is irrelevant +*/ +/* spin_lock_irqsave(&hpet_events->lock, flags); */ + + hpet_events->flags |=HPET_EVT_DISABLE ; + +/* disable HPET T0 */ + cfg =hpet_read32 (HPET_Tn_CFG (0 )); + cfg &= ~HPET_TN_ENABLE ; +hpet_write32 (cfg,HPET_Tn_CFG (0 )); + +/* Stop HPET legacy interrupts */ + cfg =hpet_read32 (HPET_CFG ); + cfg &= ~HPET_CFG_LEGACY ; +hpet_write32 (cfg,HPET_CFG ); + +/* spin_unlock_irqrestore(&hpet_events->lock, flags); */ +} + +void hpet_broadcast_enter (void ) {unsigned int cpu =smp_processor_id ();
xen/arch/x86/machine_kexec.c +1 -1
@@ -97,7 +97,7 @@ };if (hpet_broadcast_is_available () ) -hpet_disable_legacy_broadcast (); +crash_hpet_disable_legacy_broadcast ();/* * compat_machine_kexec() returns to idle pagetables, which requires us
xen/include/asm-x86/apic.h +1 -0
@@ -182,6 +182,7 @@extern void connect_bsp_APIC (void );extern void disconnect_bsp_APIC (int virt_wire_setup);extern void disable_local_APIC (void ); +extern void crash_disable_local_APIC (void );extern int verify_local_APIC (void );extern void cache_APIC_registers (void );extern void sync_Arb_IDs (void );
xen/include/asm-x86/hpet.h +1 -0
@@ -73,5 +73,6 @@void hpet_broadcast_exit (void );int hpet_broadcast_is_available (void );void hpet_disable_legacy_broadcast (void ); +void crash_hpet_disable_legacy_broadcast (void );#endif /* __X86_HPET_H__ */
Quoted 5 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper
This is a massive hack but is better than the code currently in place. Some functions have been duped into a crash_* version to separate them from the regular shutdown path, where disabling these subsystems is irrelevant.
Hmm, I'd rather not see this go in (in this form). A few comment below, but even with them addressed I'm not going to be in favor of it.
Also did you compare all of this with the way Linux deals with the same situation?
xen/arch/x86/apic.c line 350
Quoted 13 lines
Signed-off-by Andrew Cooper <[email protected]>diff -r
0c446850d85e-refa733ebd00dxen/arch/x86/apic.cxen/arch/x86/apic.c +4
-0@@ -344,6 +344,42 @@ } } +/* Customised version of disable_local_APIC which is only called on the kexec crash path. +* It is a brutal hack and only here untill the APIC code is overhauled. We NEED to swtich +* out of x2apic mode whatever the case as the kdump kernel cant cope due to the MMIO <-> MSR +* swap invovled with x2apic mode.
Unless the system booted in x2apic mode. Otherwise you'll confuse the secondary kernel even more.
Quoted 64 lines
xen/arch/x86/apic.c +32
-0+*/ +void crash_disable_local_APIC (void ) +{ +uint64_t msr_content; +clear_local_APIC (); + +apic_write_around (APIC_SPIV , +apic_read (APIC_SPIV ) & ~APIC_SPIV_APIC_ENABLED ); + +rdmsrl (MSR_IA32_APICBASE , msr_content); + +/* This is the only way to exit x2apic mode. Trying to disable x2apic mode while staying +* xapic mode will result in a protection fault which leads to a general protection fault +* as this is on the fault handler codepath. +*/ + msr_content &= ~(MSR_IA32_APICBASE_ENABLE |MSR_IA32_APICBASE_EXTD ); +wrmsrl (MSR_IA32_APICBASE , msr_content); + +/* In most cases, we should leave the lapic in xapic mode as this is the default state on +* CPU reset. However, some bioses specifically disable xapic mode for hilarity sake so +* we should follow suit so the kdump kernel does not get confused about mismatching ACPI +* tables. +*/ +if ( ! enabled_via_apicbase) { +/* If we did not manually enable xapic mode, I.E. the bios left it enabled, +* lets follow suit and leave it enabled for the kdump kernel. +*/ +wrmsrl (MSR_IA32_APICBASE , msr_content |MSR_IA32_APICBASE_ENABLE ); + } +} + +/* * This is to verify that we're looking at a real local APIC. * Check these against your board if the CPUs aren't getting diff -r
0c446850d85e-refa733ebd00dxen/arch/x86/crash.cxen/arch/x86/crash.c +8
-1@@ -27,6 +27,8 @@#include <asm/hvm/support.h> #include <asm/apic.h> #include <asm/io_apic.h> +#include <xen/iommu.h> +#include <asm/hvm/iommu.h> static atomic_t waiting_for_crash_ipi;static unsigned int crashing_cpu;@@ -43,7 +45,12 @@kexec_crash_save_cpu (); -__stop_this_cpu (); +crash_disable_local_APIC (); +hvm_cpu_down (); +/* Clear FPU, zapping any pending exceptions. Needed for warm reset with +* some BIOSes.*/ +clts (); + asmvolatile ("fninit" );atomic_dec (&waiting_for_crash_ipi);@@ -77,7 +84,15 @@ msecs--; }
xen/arch/x86/crash.c line 94
Quoted 10 lines
xen/arch/x86/crash.c +8
-1-__stop_this_cpu (); +crash_disable_local_APIC (); +hvm_cpu_down (); +/* Clear FPU, zapping any pending exceptions. Needed for warm reset with +* some BIOSes.*/ +clts (); + asmvolatile ("fninit" ); + + x2apic_enabled =0 ;
Why? And if indeed needed here, why not above? (Your patch wasn't generated with -p, so it's a little difficult to understand what functions you modify here.)
xen/arch/x86/crash.c line 108
Quoted 13 lines
xen/arch/x86/crash.c +3
-0+disable_IO_APIC ();local_irq_enable ();@@ -86,9 +101,24 @@void machine_crash_shutdown (void ) {crash_xen_info_t *info; +const struct iommu_ops * ops;nmi_shootdown_cpus (); +local_irq_disable ();
local_irq_save()
xen/arch/x86/crash.c line 113
Quoted 5 lines
xen/arch/x86/crash.c +5
-0+ +/* Yes i know this is hacky but it is the easiest solution. I should add an iommu_ops +* function called crash() or so which just disables the iommu 'fun' without saving state +*/ + ops =iommu_get_ops ();
if ( ops )
xen/arch/x86/crash.c line 120
Quoted 7 lines
xen/arch/x86/crash.c +7
-0+ ops->suspend (); + +/* Yes i know this is from driver/passthrough/vtd/ but it appears to be architecture +* independant, and also bears little/no relation to x2apic. Needs cleaning up +*/ +iommu_disable_x2apic_IR (); +local_irq_enable ();
local_irq_restore() - you absolutely want to avoid turning on interrupts if they weren't on entry to the function.
Quoted 27 lines
xen/arch/x86/crash.c +1
-0+ info =kexec_crash_save_info (); info->xen_phys_start = xen_phys_start; info->dom0_pfn_to_mfn_frame_list_list =diff -r
0c446850d85e-refa733ebd00dxen/arch/x86/hpet.cxen/arch/x86/hpet.c +16
-0@@ -670,6 +670,34 @@smp_send_event_check_mask (&cpu_online_map); } +void crash_hpet_disable_legacy_broadcast (void ) +{ +u32 cfg; + +if ( !hpet_events || !(hpet_events->flags &HPET_EVT_LEGACY ) ) +return ; + +/* By this point in the crash handler, we are the only CPU still going, +* so the lock is irrelevant +*/ +/* spin_lock_irqsave(&hpet_events->lock, flags); */ + + hpet_events->flags |=HPET_EVT_DISABLE ; + +/* disable HPET T0 */ + cfg =hpet_read32 (HPET_Tn_CFG (0 ));
xen/arch/x86/hpet.c line 698
Quoted 10 lines
xen/arch/x86/hpet.c +10
-0+ cfg &= ~HPET_TN_ENABLE ; +hpet_write32 (cfg,HPET_Tn_CFG (0 )); + +/* Stop HPET legacy interrupts */ + cfg =hpet_read32 (HPET_CFG ); + cfg &= ~HPET_CFG_LEGACY ; +hpet_write32 (cfg,HPET_CFG ); + +/* spin_unlock_irqrestore(&hpet_events->lock, flags); */ +}
This looks to be the same except for the skipping of the locks. Calls for adding a parameter to the original function.
Jan
Quoted 43 lines
xen/arch/x86/hpet.c +2
-0+ +void hpet_broadcast_enter (void ) {unsigned int cpu =smp_processor_id ();diff -r
0c446850d85e-refa733ebd00dxen/arch/x86/machine_kexec.cxen/arch/x86/machine_kexec.c +1
-1@@ -97,7 +97,7 @@ };if (hpet_broadcast_is_available () ) -hpet_disable_legacy_broadcast (); +crash_hpet_disable_legacy_broadcast ();/* * compat_machine_kexec() returns to idle pagetables, which requires us diff -r
0c446850d85e-refa733ebd00dxen/include/asm-x86/apic.hxen/include/asm-x86/apic.h +1
-0@@ -182,6 +182,7 @@extern void connect_bsp_APIC (void );extern void disconnect_bsp_APIC (int virt_wire_setup);extern void disable_local_APIC (void ); +extern void crash_disable_local_APIC (void );extern int verify_local_APIC (void );extern void cache_APIC_registers (void );extern void sync_Arb_IDs (void );diff -r
0c446850d85e-refa733ebd00dxen/include/asm-x86/hpet.hxen/include/asm-x86/hpet.h +1
-0@@ -73,5 +73,6 @@void hpet_broadcast_exit (void );int hpet_broadcast_is_available (void );void hpet_disable_legacy_broadcast (void ); +void crash_hpet_disable_legacy_broadcast (void );#endif /* __X86_HPET_H__ */ _______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
Quoted 2 lines · Jan Beulich
(Your patch wasn't generated with -p, so it's a little difficult to understand what functions you modify here.)
You can add the following to your ~/.hgrc:
[diff]
showfunc = True
To make that the default.
Ian.
Quoted 10 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper
This is a massive hack but is better than the code currently in place. Some functions have been duped into a crash_* version to separate them from the regular shutdown path, where disabling these subsystems is irrelevant.
Hmm, I'd rather not see this go in (in this form). A few comment below, but even with them addressed I'm not going to be in favor of it.
Also did you compare all of this with the way Linux deals with the same situation?
Linux 2.6.32 which I was comparing against successfully reverts the lapic to boot state, but has no clue about iommu so will never enable it in the first place. There is currently not enough information in apic.c to remember the boot state - the hack for xapic boot state is poor to say the least. I will rejig this to properly record boot state in all cases for local apics.
Quoted 104 lines
xen/arch/x86/apic.c line 350
Quoted 13 lines
Signed-off-by Andrew Cooper<[email protected]>diff -r
0c446850d85e-refa733ebd00dxen/arch/x86/apic.cxen/arch/x86/apic.c +4
-0@@ -344,6 +344,42 @@ } } +/* Customised version of disable_local_APIC which is only called on the kexec crash path. +* It is a brutal hack and only here untill the APIC code is overhauled. We NEED to swtich +* out of x2apic mode whatever the case as the kdump kernel cant cope due to the MMIO<-> MSR +* swap invovled with x2apic mode. Unless the system booted in x2apic mode. Otherwise you'll confuse the secondary kernel even more.
Quoted 64 lines
xen/arch/x86/apic.c +32
-0+*/ +void crash_disable_local_APIC (void ) +{ +uint64_t msr_content; +clear_local_APIC (); + +apic_write_around (APIC_SPIV , +apic_read (APIC_SPIV )& ~APIC_SPIV_APIC_ENABLED ); + +rdmsrl (MSR_IA32_APICBASE , msr_content); + +/* This is the only way to exit x2apic mode. Trying to disable x2apic mode while staying +* xapic mode will result in a protection fault which leads to a general protection fault +* as this is on the fault handler codepath. +*/ + msr_content&= ~(MSR_IA32_APICBASE_ENABLE |MSR_IA32_APICBASE_EXTD ); +wrmsrl (MSR_IA32_APICBASE , msr_content); + +/* In most cases, we should leave the lapic in xapic mode as this is the default state on +* CPU reset. However, some bioses specifically disable xapic mode for hilarity sake so +* we should follow suit so the kdump kernel does not get confused about mismatching ACPI +* tables. +*/ +if ( ! enabled_via_apicbase) { +/* If we did not manually enable xapic mode, I.E. the bios left it enabled, +* lets follow suit and leave it enabled for the kdump kernel. +*/ +wrmsrl (MSR_IA32_APICBASE , msr_content |MSR_IA32_APICBASE_ENABLE ); + } +} + +/* * This is to verify that we're looking at a real local APIC. * Check these against your board if the CPUs aren't getting diff -r
0c446850d85e-refa733ebd00dxen/arch/x86/crash.cxen/arch/x86/crash.c +8
-1@@ -27,6 +27,8 @@#include <asm/hvm/support.h> #include <asm/apic.h> #include <asm/io_apic.h> +#include <xen/iommu.h> +#include <asm/hvm/iommu.h> static atomic_t waiting_for_crash_ipi;static unsigned int crashing_cpu;@@ -43,7 +45,12 @@kexec_crash_save_cpu (); -__stop_this_cpu (); +crash_disable_local_APIC (); +hvm_cpu_down (); +/* Clear FPU, zapping any pending exceptions. Needed for warm reset with +* some BIOSes.*/ +clts (); + asmvolatile ("fninit" );atomic_dec (&waiting_for_crash_ipi);@@ -77,7 +84,15 @@ msecs--; }xen/arch/x86/crash.c line 94
Quoted 10 lines
xen/arch/x86/crash.c +8
-1-__stop_this_cpu (); +crash_disable_local_APIC (); +hvm_cpu_down (); +/* Clear FPU, zapping any pending exceptions. Needed for warm reset with +* some BIOSes.*/ +clts (); + asmvolatile ("fninit" ); + + x2apic_enabled =0 ;Why? And if indeed needed here, why not above? (Your patch wasn't generated with -p, so it's a little difficult to understand what functions you modify here.)
Quoted 10 lines
xen/arch/x86/crash.c +2
-0+disable_IO_APIC ();local_irq_enable ();@@ -86,9 +101,24 @@void machine_crash_shutdown (void ) {crash_xen_info_t *info; +const struct iommu_ops * ops;
Quoted 4 lines
xen/arch/x86/crash.c line 108
Quoted 3 lines
xen/arch/x86/crash.c +1
-0nmi_shootdown_cpus (); +local_irq_disable ();
local_irq_save()
Fair enough in the general case but in this case it really doesnt matter. Having said that, there are other areas on the crash/reboot path which need updating with this logic
Quoted 57 lines
xen/arch/x86/crash.c line 113
Quoted 5 lines
xen/arch/x86/crash.c +5
-0+ +/* Yes i know this is hacky but it is the easiest solution. I should add an iommu_ops +* function called crash() or so which just disables the iommu 'fun' without saving state +*/ + ops =iommu_get_ops ();if ( ops )
xen/arch/x86/crash.c line 120
Quoted 7 lines
xen/arch/x86/crash.c +7
-0+ ops->suspend (); + +/* Yes i know this is from driver/passthrough/vtd/ but it appears to be architecture +* independant, and also bears little/no relation to x2apic. Needs cleaning up +*/ +iommu_disable_x2apic_IR (); +local_irq_enable ();
local_irq_restore()- you absolutely want to avoid turning on interrupts if they weren't on entry to the function.Quoted 27 lines
xen/arch/x86/crash.c +1
-0+ info =kexec_crash_save_info (); info->xen_phys_start = xen_phys_start; info->dom0_pfn_to_mfn_frame_list_list =diff -r
0c446850d85e-refa733ebd00dxen/arch/x86/hpet.cxen/arch/x86/hpet.c +16
-0@@ -670,6 +670,34 @@smp_send_event_check_mask (&cpu_online_map); } +void crash_hpet_disable_legacy_broadcast (void ) +{ +u32 cfg; + +if ( !hpet_events || !(hpet_events->flags&HPET_EVT_LEGACY ) ) +return ; + +/* By this point in the crash handler, we are the only CPU still going, +* so the lock is irrelevant +*/ +/* spin_lock_irqsave(&hpet_events->lock, flags); */ + + hpet_events->flags |=HPET_EVT_DISABLE ; + +/* disable HPET T0 */ + cfg =hpet_read32 (HPET_Tn_CFG (0 ));xen/arch/x86/hpet.c line 698
Quoted 10 lines
xen/arch/x86/hpet.c +10
-0+ cfg&= ~HPET_TN_ENABLE ; +hpet_write32 (cfg,HPET_Tn_CFG (0 )); + +/* Stop HPET legacy interrupts */ + cfg =hpet_read32 (HPET_CFG ); + cfg&= ~HPET_CFG_LEGACY ; +hpet_write32 (cfg,HPET_CFG ); + +/* spin_unlock_irqrestore(&hpet_events->lock, flags); */ +}This looks to be the same except for the skipping of the locks. Calls for adding a parameter to the original function.
Quoted 1 line
Jan
This function skips the IPI to all other processors, because at this point in the code, all other processors have been shot down. I will comment that detail
Quoted 44 lines
Quoted 43 lines
xen/arch/x86/hpet.c +2
-0+ +void hpet_broadcast_enter (void ) {unsigned int cpu =smp_processor_id ();diff -r
0c446850d85e-refa733ebd00dxen/arch/x86/machine_kexec.cxen/arch/x86/machine_kexec.c +1
-1@@ -97,7 +97,7 @@ };if (hpet_broadcast_is_available () ) -hpet_disable_legacy_broadcast (); +crash_hpet_disable_legacy_broadcast ();/* * compat_machine_kexec() returns to idle pagetables, which requires us diff -r
0c446850d85e-refa733ebd00dxen/include/asm-x86/apic.hxen/include/asm-x86/apic.h +1
-0@@ -182,6 +182,7 @@extern void connect_bsp_APIC (void );extern void disconnect_bsp_APIC (int virt_wire_setup);extern void disable_local_APIC (void ); +extern void crash_disable_local_APIC (void );extern int verify_local_APIC (void );extern void cache_APIC_registers (void );extern void sync_Arb_IDs (void );diff -r
0c446850d85e-refa733ebd00dxen/include/asm-x86/hpet.hxen/include/asm-x86/hpet.h +1
-0@@ -73,5 +73,6 @@void hpet_broadcast_exit (void );int hpet_broadcast_is_available (void );void hpet_disable_legacy_broadcast (void ); +void crash_hpet_disable_legacy_broadcast (void );#endif /* __X86_HPET_H__ */ _______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
Signature · 2 lines
Andrew Cooper - Dom0 Kernel Engineer, Citrix XenServer T: +44 (0)1223 225 900, http://www.citrix.com