Skip to content

Commit 7aa1c8f

Browse files
committed
MIPS: kdump: Add support
[ralf@linux-mips.org: Original patch by Maxim Uvarov <muvarov@gmail.com> with plenty of further shining, polishing, debugging and testing by me.] Signed-off-by: Maxim Uvarov <muvarov@gmail.com> Cc: linux-mips@linux-mips.org Cc: kexec@lists.infradead.org Cc: horms@verge.net.au Patchwork: https://patchwork.linux-mips.org/patch/1025/ Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
1 parent 98cdee0 commit 7aa1c8f

11 files changed

Lines changed: 396 additions & 9 deletions

File tree

‎arch/mips/Kconfig‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2379,6 +2379,29 @@ config KEXEC
23792379
support. As of this writing the exact hardware interface is
23802380
strongly in flux, so no good recommendation can be made.
23812381

2382+
config CRASH_DUMP
2383+
bool "Kernel crash dumps"
2384+
help
2385+
Generate crash dump after being started by kexec.
2386+
This should be normally only set in special crash dump kernels
2387+
which are loaded in the main kernel with kexec-tools into
2388+
a specially reserved region and then later executed after
2389+
a crash by kdump/kexec. The crash dump kernel must be compiled
2390+
to a memory address not used by the main kernel or firmware using
2391+
PHYSICAL_START.
2392+
2393+
config PHYSICAL_START
2394+
hex "Physical address where the kernel is loaded"
2395+
default "0xffffffff84000000" if 64BIT
2396+
default "0x84000000" if 32BIT
2397+
depends on CRASH_DUMP
2398+
help
2399+
This gives the CKSEG0 or KSEG0 address where the kernel is loaded.
2400+
If you plan to use kernel for capturing the crash dump change
2401+
this value to start of the reserved region (the "X" value as
2402+
specified in the "crashkernel=YM@XM" command line boot parameter
2403+
passed to the panic-ed kernel).
2404+
23822405
config SECCOMP
23832406
bool "Enable seccomp to safely compute untrusted bytecode"
23842407
depends on PROC_FS

‎arch/mips/include/asm/kexec.h‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,43 @@
99
#ifndef _MIPS_KEXEC
1010
# define _MIPS_KEXEC
1111

12+
#include <asm/stacktrace.h>
13+
1214
/* Maximum physical address we can use pages from */
1315
#define KEXEC_SOURCE_MEMORY_LIMIT (0x20000000)
1416
/* Maximum address we can reach in physical address mode */
1517
#define KEXEC_DESTINATION_MEMORY_LIMIT (0x20000000)
1618
/* Maximum address we can use for the control code buffer */
1719
#define KEXEC_CONTROL_MEMORY_LIMIT (0x20000000)
18-
19-
#define KEXEC_CONTROL_PAGE_SIZE 4096
20+
/* Reserve 3*4096 bytes for board-specific info */
21+
#define KEXEC_CONTROL_PAGE_SIZE (4096 + 3*4096)
2022

2123
/* The native architecture */
2224
#define KEXEC_ARCH KEXEC_ARCH_MIPS
25+
#define MAX_NOTE_BYTES 1024
2326

2427
static inline void crash_setup_regs(struct pt_regs *newregs,
2528
struct pt_regs *oldregs)
2629
{
27-
/* Dummy implementation for now */
30+
if (oldregs)
31+
memcpy(newregs, oldregs, sizeof(*newregs));
32+
else
33+
prepare_frametrace(newregs);
2834
}
2935

36+
#ifdef CONFIG_KEXEC
37+
struct kimage;
38+
extern unsigned long kexec_args[4];
39+
extern int (*_machine_kexec_prepare)(struct kimage *);
40+
extern void (*_machine_kexec_shutdown)(void);
41+
extern void (*_machine_crash_shutdown)(struct pt_regs *regs);
42+
extern void default_machine_crash_shutdown(struct pt_regs *regs);
43+
#ifdef CONFIG_SMP
44+
extern const unsigned char kexec_smp_wait[];
45+
extern unsigned long secondary_kexec_args[4];
46+
extern void (*relocated_kexec_smp_wait) (void *);
47+
extern atomic_t kexec_ready_to_reboot;
48+
#endif
49+
#endif
50+
3051
#endif /* !_MIPS_KEXEC */

‎arch/mips/include/asm/smp.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ extern int __cpu_logical_map[NR_CPUS];
4040
#define SMP_CALL_FUNCTION 0x2
4141
/* Octeon - Tell another core to flush its icache */
4242
#define SMP_ICACHE_FLUSH 0x4
43+
/* Used by kexec crashdump to save all cpu's state */
44+
#define SMP_DUMP 0x8
4345

4446
extern volatile cpumask_t cpu_callin_map;
4547

@@ -91,4 +93,8 @@ static inline void arch_send_call_function_ipi_mask(const struct cpumask *mask)
9193
mp_ops->send_ipi_mask(mask, SMP_CALL_FUNCTION);
9294
}
9395

96+
#if defined(CONFIG_KEXEC)
97+
extern void (*dump_ipi_function_ptr)(void *);
98+
void dump_send_ipi(void (*dump_ipi_callback)(void *));
99+
#endif
94100
#endif /* __ASM_SMP_H */

‎arch/mips/kernel/Makefile‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ obj-$(CONFIG_I8253) += i8253.o
8080

8181
obj-$(CONFIG_GPIO_TXX9) += gpio_txx9.o
8282

83-
obj-$(CONFIG_KEXEC) += machine_kexec.o relocate_kernel.o
83+
obj-$(CONFIG_KEXEC) += machine_kexec.o relocate_kernel.o crash.o
84+
obj-$(CONFIG_CRASH_DUMP) += crash_dump.o
8485
obj-$(CONFIG_EARLY_PRINTK) += early_printk.o
8586
obj-$(CONFIG_SPINLOCK_TEST) += spinlock_test.o
8687
obj-$(CONFIG_MIPS_MACHINE) += mips_machine.o

‎arch/mips/kernel/crash.c‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include <linux/kernel.h>
2+
#include <linux/smp.h>
3+
#include <linux/reboot.h>
4+
#include <linux/kexec.h>
5+
#include <linux/bootmem.h>
6+
#include <linux/crash_dump.h>
7+
#include <linux/delay.h>
8+
#include <linux/init.h>
9+
#include <linux/irq.h>
10+
#include <linux/types.h>
11+
#include <linux/sched.h>
12+
13+
/* This keeps a track of which one is crashing cpu. */
14+
static int crashing_cpu = -1;
15+
static cpumask_t cpus_in_crash = CPU_MASK_NONE;
16+
17+
#ifdef CONFIG_SMP
18+
static void crash_shutdown_secondary(void *ignore)
19+
{
20+
struct pt_regs *regs;
21+
int cpu = smp_processor_id();
22+
23+
regs = task_pt_regs(current);
24+
25+
if (!cpu_online(cpu))
26+
return;
27+
28+
local_irq_disable();
29+
if (!cpu_isset(cpu, cpus_in_crash))
30+
crash_save_cpu(regs, cpu);
31+
cpu_set(cpu, cpus_in_crash);
32+
33+
while (!atomic_read(&kexec_ready_to_reboot))
34+
cpu_relax();
35+
relocated_kexec_smp_wait(NULL);
36+
/* NOTREACHED */
37+
}
38+
39+
static void crash_kexec_prepare_cpus(void)
40+
{
41+
unsigned int msecs;
42+
43+
unsigned int ncpus = num_online_cpus() - 1;/* Excluding the panic cpu */
44+
45+
dump_send_ipi(crash_shutdown_secondary);
46+
smp_wmb();
47+
48+
/*
49+
* The crash CPU sends an IPI and wait for other CPUs to
50+
* respond. Delay of at least 10 seconds.
51+
*/
52+
pr_emerg("Sending IPI to other cpus...\n");
53+
msecs = 10000;
54+
while ((cpus_weight(cpus_in_crash) < ncpus) && (--msecs > 0)) {
55+
cpu_relax();
56+
mdelay(1);
57+
}
58+
}
59+
60+
#else /* !defined(CONFIG_SMP) */
61+
static void crash_kexec_prepare_cpus(void) {}
62+
#endif /* !defined(CONFIG_SMP) */
63+
64+
void default_machine_crash_shutdown(struct pt_regs *regs)
65+
{
66+
local_irq_disable();
67+
crashing_cpu = smp_processor_id();
68+
crash_save_cpu(regs, crashing_cpu);
69+
crash_kexec_prepare_cpus();
70+
cpu_set(crashing_cpu, cpus_in_crash);
71+
}

‎arch/mips/kernel/crash_dump.c‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include <linux/highmem.h>
2+
#include <linux/bootmem.h>
3+
#include <linux/crash_dump.h>
4+
#include <asm/uaccess.h>
5+
6+
unsigned long long elfcorehdr_addr = ELFCORE_ADDR_MAX;
7+
8+
static int __init parse_savemaxmem(char *p)
9+
{
10+
if (p)
11+
saved_max_pfn = (memparse(p, &p) >> PAGE_SHIFT) - 1;
12+
13+
return 1;
14+
}
15+
__setup("savemaxmem=", parse_savemaxmem);
16+
17+
18+
static void *kdump_buf_page;
19+
20+
/**
21+
* copy_oldmem_page - copy one page from "oldmem"
22+
* @pfn: page frame number to be copied
23+
* @buf: target memory address for the copy; this can be in kernel address
24+
* space or user address space (see @userbuf)
25+
* @csize: number of bytes to copy
26+
* @offset: offset in bytes into the page (based on pfn) to begin the copy
27+
* @userbuf: if set, @buf is in user address space, use copy_to_user(),
28+
* otherwise @buf is in kernel address space, use memcpy().
29+
*
30+
* Copy a page from "oldmem". For this page, there is no pte mapped
31+
* in the current kernel.
32+
*
33+
* Calling copy_to_user() in atomic context is not desirable. Hence first
34+
* copying the data to a pre-allocated kernel page and then copying to user
35+
* space in non-atomic context.
36+
*/
37+
ssize_t copy_oldmem_page(unsigned long pfn, char *buf,
38+
size_t csize, unsigned long offset, int userbuf)
39+
{
40+
void *vaddr;
41+
42+
if (!csize)
43+
return 0;
44+
45+
vaddr = kmap_atomic_pfn(pfn);
46+
47+
if (!userbuf) {
48+
memcpy(buf, (vaddr + offset), csize);
49+
kunmap_atomic(vaddr);
50+
} else {
51+
if (!kdump_buf_page) {
52+
pr_warning("Kdump: Kdump buffer page not allocated\n");
53+
54+
return -EFAULT;
55+
}
56+
copy_page(kdump_buf_page, vaddr);
57+
kunmap_atomic(vaddr);
58+
if (copy_to_user(buf, (kdump_buf_page + offset), csize))
59+
return -EFAULT;
60+
}
61+
62+
return csize;
63+
}
64+
65+
static int __init kdump_buf_page_init(void)
66+
{
67+
int ret = 0;
68+
69+
kdump_buf_page = kmalloc(PAGE_SIZE, GFP_KERNEL);
70+
if (!kdump_buf_page) {
71+
pr_warning("Kdump: Failed to allocate kdump buffer page\n");
72+
ret = -ENOMEM;
73+
}
74+
75+
return ret;
76+
}
77+
arch_initcall(kdump_buf_page_init);

‎arch/mips/kernel/machine_kexec.c‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* This source code is licensed under the GNU General Public License,
66
* Version 2. See the file COPYING for more details.
77
*/
8-
8+
#include <linux/compiler.h>
99
#include <linux/kexec.h>
1010
#include <linux/mm.h>
1111
#include <linux/delay.h>
@@ -19,9 +19,19 @@ extern const size_t relocate_new_kernel_size;
1919
extern unsigned long kexec_start_address;
2020
extern unsigned long kexec_indirection_page;
2121

22+
int (*_machine_kexec_prepare)(struct kimage *) = NULL;
23+
void (*_machine_kexec_shutdown)(void) = NULL;
24+
void (*_machine_crash_shutdown)(struct pt_regs *regs) = NULL;
25+
#ifdef CONFIG_SMP
26+
void (*relocated_kexec_smp_wait) (void *);
27+
atomic_t kexec_ready_to_reboot = ATOMIC_INIT(0);
28+
#endif
29+
2230
int
2331
machine_kexec_prepare(struct kimage *kimage)
2432
{
33+
if (_machine_kexec_prepare)
34+
return _machine_kexec_prepare(kimage);
2535
return 0;
2636
}
2737

@@ -33,14 +43,20 @@ machine_kexec_cleanup(struct kimage *kimage)
3343
void
3444
machine_shutdown(void)
3545
{
46+
if (_machine_kexec_shutdown)
47+
_machine_kexec_shutdown();
3648
}
3749

3850
void
3951
machine_crash_shutdown(struct pt_regs *regs)
4052
{
53+
if (_machine_crash_shutdown)
54+
_machine_crash_shutdown(regs);
55+
else
56+
default_machine_crash_shutdown(regs);
4157
}
4258

43-
typedef void (*noretfun_t)(void) __attribute__((noreturn));
59+
typedef void (*noretfun_t)(void) __noreturn;
4460

4561
void
4662
machine_kexec(struct kimage *image)
@@ -52,7 +68,9 @@ machine_kexec(struct kimage *image)
5268
reboot_code_buffer =
5369
(unsigned long)page_address(image->control_code_page);
5470

55-
kexec_start_address = image->start;
71+
kexec_start_address =
72+
(unsigned long) phys_to_virt(image->start);
73+
5674
kexec_indirection_page =
5775
(unsigned long) phys_to_virt(image->head & PAGE_MASK);
5876

@@ -63,7 +81,7 @@ machine_kexec(struct kimage *image)
6381
* The generic kexec code builds a page list with physical
6482
* addresses. they are directly accessible through KSEG0 (or
6583
* CKSEG0 or XPHYS if on 64bit system), hence the
66-
* pys_to_virt() call.
84+
* phys_to_virt() call.
6785
*/
6886
for (ptr = &image->head; (entry = *ptr) && !(entry &IND_DONE);
6987
ptr = (entry & IND_INDIRECTION) ?
@@ -81,5 +99,12 @@ machine_kexec(struct kimage *image)
8199
printk("Will call new kernel at %08lx\n", image->start);
82100
printk("Bye ...\n");
83101
__flush_cache_all();
102+
#ifdef CONFIG_SMP
103+
/* All secondary cpus now may jump to kexec_wait cycle */
104+
relocated_kexec_smp_wait = reboot_code_buffer +
105+
(void *)(kexec_smp_wait - relocate_new_kernel);
106+
smp_wmb();
107+
atomic_set(&kexec_ready_to_reboot, 1);
108+
#endif
84109
((noretfun_t) reboot_code_buffer)();
85110
}

0 commit comments

Comments
 (0)