xref: /openbmc/linux/arch/x86/kernel/crash.c (revision 8a10bc9d)
1 /*
2  * Architecture specific (i386/x86_64) functions for kexec based crash dumps.
3  *
4  * Created by: Hariprasad Nellitheertha (hari@in.ibm.com)
5  *
6  * Copyright (C) IBM Corporation, 2004. All rights reserved.
7  *
8  */
9 
10 #include <linux/types.h>
11 #include <linux/kernel.h>
12 #include <linux/smp.h>
13 #include <linux/reboot.h>
14 #include <linux/kexec.h>
15 #include <linux/delay.h>
16 #include <linux/elf.h>
17 #include <linux/elfcore.h>
18 #include <linux/module.h>
19 
20 #include <asm/processor.h>
21 #include <asm/hardirq.h>
22 #include <asm/nmi.h>
23 #include <asm/hw_irq.h>
24 #include <asm/apic.h>
25 #include <asm/hpet.h>
26 #include <linux/kdebug.h>
27 #include <asm/cpu.h>
28 #include <asm/reboot.h>
29 #include <asm/virtext.h>
30 
31 int in_crash_kexec;
32 
33 /*
34  * This is used to VMCLEAR all VMCSs loaded on the
35  * processor. And when loading kvm_intel module, the
36  * callback function pointer will be assigned.
37  *
38  * protected by rcu.
39  */
40 crash_vmclear_fn __rcu *crash_vmclear_loaded_vmcss = NULL;
41 EXPORT_SYMBOL_GPL(crash_vmclear_loaded_vmcss);
42 
43 static inline void cpu_crash_vmclear_loaded_vmcss(void)
44 {
45 	crash_vmclear_fn *do_vmclear_operation = NULL;
46 
47 	rcu_read_lock();
48 	do_vmclear_operation = rcu_dereference(crash_vmclear_loaded_vmcss);
49 	if (do_vmclear_operation)
50 		do_vmclear_operation();
51 	rcu_read_unlock();
52 }
53 
54 #if defined(CONFIG_SMP) && defined(CONFIG_X86_LOCAL_APIC)
55 
56 static void kdump_nmi_callback(int cpu, struct pt_regs *regs)
57 {
58 #ifdef CONFIG_X86_32
59 	struct pt_regs fixed_regs;
60 #endif
61 
62 #ifdef CONFIG_X86_32
63 	if (!user_mode_vm(regs)) {
64 		crash_fixup_ss_esp(&fixed_regs, regs);
65 		regs = &fixed_regs;
66 	}
67 #endif
68 	crash_save_cpu(regs, cpu);
69 
70 	/*
71 	 * VMCLEAR VMCSs loaded on all cpus if needed.
72 	 */
73 	cpu_crash_vmclear_loaded_vmcss();
74 
75 	/* Disable VMX or SVM if needed.
76 	 *
77 	 * We need to disable virtualization on all CPUs.
78 	 * Having VMX or SVM enabled on any CPU may break rebooting
79 	 * after the kdump kernel has finished its task.
80 	 */
81 	cpu_emergency_vmxoff();
82 	cpu_emergency_svm_disable();
83 
84 	disable_local_APIC();
85 }
86 
87 static void kdump_nmi_shootdown_cpus(void)
88 {
89 	in_crash_kexec = 1;
90 	nmi_shootdown_cpus(kdump_nmi_callback);
91 
92 	disable_local_APIC();
93 }
94 
95 #else
96 static void kdump_nmi_shootdown_cpus(void)
97 {
98 	/* There are no cpus to shootdown */
99 }
100 #endif
101 
102 void native_machine_crash_shutdown(struct pt_regs *regs)
103 {
104 	/* This function is only called after the system
105 	 * has panicked or is otherwise in a critical state.
106 	 * The minimum amount of code to allow a kexec'd kernel
107 	 * to run successfully needs to happen here.
108 	 *
109 	 * In practice this means shooting down the other cpus in
110 	 * an SMP system.
111 	 */
112 	/* The kernel is broken so disable interrupts */
113 	local_irq_disable();
114 
115 	kdump_nmi_shootdown_cpus();
116 
117 	/*
118 	 * VMCLEAR VMCSs loaded on this cpu if needed.
119 	 */
120 	cpu_crash_vmclear_loaded_vmcss();
121 
122 	/* Booting kdump kernel with VMX or SVM enabled won't work,
123 	 * because (among other limitations) we can't disable paging
124 	 * with the virt flags.
125 	 */
126 	cpu_emergency_vmxoff();
127 	cpu_emergency_svm_disable();
128 
129 #ifdef CONFIG_X86_IO_APIC
130 	/* Prevent crash_kexec() from deadlocking on ioapic_lock. */
131 	ioapic_zap_locks();
132 	disable_IO_APIC();
133 #endif
134 	lapic_shutdown();
135 #ifdef CONFIG_HPET_TIMER
136 	hpet_disable();
137 #endif
138 	crash_save_cpu(regs, safe_smp_processor_id());
139 }
140