Skip to content

Commit ec9ce0d

Browse files
Vivek GoyalLinus Torvalds
authored andcommitted
[PATCH] kdump: x86_64 save cpu registers upon crash
- Saving the cpu registers of all cpus before booting in to the crash kernel. - crash_setup_regs will save the registers of the cpu on which panic has occured. One of the concerns ppc64 folks raised is that after capturing the register states, one should not pop the current call frame and push new one. Hence it has been inlined. More call frames later get pushed on to stack (machine_crash_shutdown() and machine_kexec()), but one will not want to backtrace those. - Not very sure about the CFI annotations. With this patch I am getting decent backtrace with gdb. Assuming, compiler has generated enough debugging information for crash_kexec(). Coding crash_setup_regs() in pure assembly makes it tricky because then it can not be inlined and we don't want to return back after capturing register states we don't want to pop this call frame. - Saving the non-panicing cpus registers will be done in the NMI handler while shooting down them in machine_crash_shutdown. - Introducing CRASH_DUMP option in Kconfig for x86_64. Signed-off-by: Murali M Chakravarthy <muralim@in.ibm.com> Signed-off-by: Vivek Goyal <vgoyal@in.ibm.com> Cc: Andi Kleen <ak@muc.de> Cc: "Eric W. Biederman" <ebiederm@xmission.com> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
1 parent cffe632 commit ec9ce0d

3 files changed

Lines changed: 113 additions & 0 deletions

File tree

‎arch/x86_64/Kconfig‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,13 @@ config KEXEC
427427
support. As of this writing the exact hardware interface is
428428
strongly in flux, so no good recommendation can be made.
429429

430+
config CRASH_DUMP
431+
bool "kernel crash dumps (EXPERIMENTAL)"
432+
depends on EMBEDDED
433+
depends on EXPERIMENTAL
434+
help
435+
Generate crash dump after being started by kexec.
436+
430437
config SECCOMP
431438
bool "Enable seccomp to safely compute untrusted bytecode"
432439
depends on PROC_FS

‎arch/x86_64/kernel/crash.c‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@
1111
#include <linux/types.h>
1212
#include <linux/kernel.h>
1313
#include <linux/smp.h>
14+
#include <linux/irq.h>
1415
#include <linux/reboot.h>
1516
#include <linux/kexec.h>
1617
#include <linux/delay.h>
18+
#include <linux/elf.h>
19+
#include <linux/elfcore.h>
1720

1821
#include <asm/processor.h>
1922
#include <asm/hardirq.h>
@@ -24,6 +27,71 @@
2427
/* This keeps a track of which one is crashing cpu. */
2528
static int crashing_cpu;
2629

30+
static u32 *append_elf_note(u32 *buf, char *name, unsigned type,
31+
void *data, size_t data_len)
32+
{
33+
struct elf_note note;
34+
35+
note.n_namesz = strlen(name) + 1;
36+
note.n_descsz = data_len;
37+
note.n_type = type;
38+
memcpy(buf, &note, sizeof(note));
39+
buf += (sizeof(note) +3)/4;
40+
memcpy(buf, name, note.n_namesz);
41+
buf += (note.n_namesz + 3)/4;
42+
memcpy(buf, data, note.n_descsz);
43+
buf += (note.n_descsz + 3)/4;
44+
45+
return buf;
46+
}
47+
48+
static void final_note(u32 *buf)
49+
{
50+
struct elf_note note;
51+
52+
note.n_namesz = 0;
53+
note.n_descsz = 0;
54+
note.n_type = 0;
55+
memcpy(buf, &note, sizeof(note));
56+
}
57+
58+
static void crash_save_this_cpu(struct pt_regs *regs, int cpu)
59+
{
60+
struct elf_prstatus prstatus;
61+
u32 *buf;
62+
63+
if ((cpu < 0) || (cpu >= NR_CPUS))
64+
return;
65+
66+
/* Using ELF notes here is opportunistic.
67+
* I need a well defined structure format
68+
* for the data I pass, and I need tags
69+
* on the data to indicate what information I have
70+
* squirrelled away. ELF notes happen to provide
71+
* all of that that no need to invent something new.
72+
*/
73+
74+
buf = (u32*)per_cpu_ptr(crash_notes, cpu);
75+
76+
if (!buf)
77+
return;
78+
79+
memset(&prstatus, 0, sizeof(prstatus));
80+
prstatus.pr_pid = current->pid;
81+
elf_core_copy_regs(&prstatus.pr_reg, regs);
82+
buf = append_elf_note(buf, "CORE", NT_PRSTATUS, &prstatus,
83+
sizeof(prstatus));
84+
final_note(buf);
85+
}
86+
87+
static void crash_save_self(struct pt_regs *regs)
88+
{
89+
int cpu;
90+
91+
cpu = smp_processor_id();
92+
crash_save_this_cpu(regs, cpu);
93+
}
94+
2795
#ifdef CONFIG_SMP
2896
static atomic_t waiting_for_crash_ipi;
2997

@@ -38,6 +106,7 @@ static int crash_nmi_callback(struct pt_regs *regs, int cpu)
38106
return 1;
39107
local_irq_disable();
40108

109+
crash_save_this_cpu(regs, cpu);
41110
disable_local_APIC();
42111
atomic_dec(&waiting_for_crash_ipi);
43112
/* Assume hlt works */
@@ -113,4 +182,5 @@ void machine_crash_shutdown(struct pt_regs *regs)
113182
disable_IO_APIC();
114183
#endif
115184

185+
crash_save_self(regs);
116186
}

‎include/asm-x86_64/kexec.h‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <asm/page.h>
55
#include <asm/proto.h>
6+
#include <asm/ptrace.h>
67

78
/*
89
* KEXEC_SOURCE_MEMORY_LIMIT maximum page get_free_page can return.
@@ -27,4 +28,39 @@
2728

2829
#define MAX_NOTE_BYTES 1024
2930

31+
/*
32+
* Saving the registers of the cpu on which panic occured in
33+
* crash_kexec to save a valid sp. The registers of other cpus
34+
* will be saved in machine_crash_shutdown while shooting down them.
35+
*/
36+
37+
static inline void crash_setup_regs(struct pt_regs *newregs,
38+
struct pt_regs *oldregs)
39+
{
40+
if (oldregs)
41+
memcpy(newregs, oldregs, sizeof(*newregs));
42+
else {
43+
__asm__ __volatile__("movq %%rbx,%0" : "=m"(newregs->rbx));
44+
__asm__ __volatile__("movq %%rcx,%0" : "=m"(newregs->rcx));
45+
__asm__ __volatile__("movq %%rdx,%0" : "=m"(newregs->rdx));
46+
__asm__ __volatile__("movq %%rsi,%0" : "=m"(newregs->rsi));
47+
__asm__ __volatile__("movq %%rdi,%0" : "=m"(newregs->rdi));
48+
__asm__ __volatile__("movq %%rbp,%0" : "=m"(newregs->rbp));
49+
__asm__ __volatile__("movq %%rax,%0" : "=m"(newregs->rax));
50+
__asm__ __volatile__("movq %%rsp,%0" : "=m"(newregs->rsp));
51+
__asm__ __volatile__("movq %%r8,%0" : "=m"(newregs->r8));
52+
__asm__ __volatile__("movq %%r9,%0" : "=m"(newregs->r9));
53+
__asm__ __volatile__("movq %%r10,%0" : "=m"(newregs->r10));
54+
__asm__ __volatile__("movq %%r11,%0" : "=m"(newregs->r11));
55+
__asm__ __volatile__("movq %%r12,%0" : "=m"(newregs->r12));
56+
__asm__ __volatile__("movq %%r13,%0" : "=m"(newregs->r13));
57+
__asm__ __volatile__("movq %%r14,%0" : "=m"(newregs->r14));
58+
__asm__ __volatile__("movq %%r15,%0" : "=m"(newregs->r15));
59+
__asm__ __volatile__("movl %%ss, %%eax;" :"=a"(newregs->ss));
60+
__asm__ __volatile__("movl %%cs, %%eax;" :"=a"(newregs->cs));
61+
__asm__ __volatile__("pushfq; popq %0" :"=m"(newregs->eflags));
62+
63+
newregs->rip = (unsigned long)current_text_addr();
64+
}
65+
}
3066
#endif /* _X86_64_KEXEC_H */

0 commit comments

Comments
 (0)