1 From eee16330c9de9adf7880cce9f1d32e13f89706bb Mon Sep 17 00:00:00 2001
2 From: Wu Zhangjin <wuzhangjin@gmail.com>
3 Date: Tue, 11 Jan 2011 13:16:47 +0000
4 Subject: MIPS: Add crash and kdump support
6 From: http://patchwork.linux-mips.org/patch/1025/
10 Please find here MIPS crash and kdump patches.
11 This is patch set of 3 patches:
12 1. generic MIPS changes (kernel);
13 2. MIPS Cavium Octeon board kexec/kdump code (kernel);
14 3. Kexec user space MIPS changes.
16 Patches were tested on the latest linux-mips@ git kernel and the latest
17 kexec-tools git on Cavium Octeon 50xx board.
19 I also made the same code working on RMI XLR/XLS boards for both
20 mips32 and mips64 kernels.
26 [ Zhangjin: Several trivial building failure has been fixed.
28 Note: the 2nd patch can not be cleanly applied, but may be a good
29 reference for the other board development:
31 + MIPS Cavium Octeon board kexec,kdump support
32 http://patchwork.linux-mips.org/patch/1026/
34 And the 3rd patch has already been merged into the mainline kexec-tools:
36 + some kexec MIPS improvements
37 http://patchwork.linux-mips.org/patch/1027/
39 kexec-tools is available here:
41 + http://horms.net/projects/kexec/
42 git://git.kernel.org/pub/scm/utils/kernel/kexec/kexec-tools.git
44 Signed-off-by: Wu Zhangjin <wuzhangjin@gmail.com>
46 (limited to 'arch/mips/kernel')
48 --- a/arch/mips/kernel/Makefile
49 +++ b/arch/mips/kernel/Makefile
50 @@ -95,7 +95,8 @@ obj-$(CONFIG_I8253) += i8253.o
52 obj-$(CONFIG_GPIO_TXX9) += gpio_txx9.o
54 -obj-$(CONFIG_KEXEC) += machine_kexec.o relocate_kernel.o
55 +obj-$(CONFIG_KEXEC) += machine_kexec.o relocate_kernel.o crash.o
56 +obj-$(CONFIG_CRASH_DUMP) += crash_dump.o
57 obj-$(CONFIG_EARLY_PRINTK) += early_printk.o
58 obj-$(CONFIG_SPINLOCK_TEST) += spinlock_test.o
59 obj-$(CONFIG_MIPS_MACHINE) += mips_machine.o
61 +++ b/arch/mips/kernel/crash.c
63 +#include <linux/kernel.h>
64 +#include <linux/smp.h>
65 +#include <linux/reboot.h>
66 +#include <linux/kexec.h>
67 +#include <linux/bootmem.h>
68 +#include <linux/crash_dump.h>
69 +#include <linux/delay.h>
70 +#include <linux/init.h>
71 +#include <linux/irq.h>
72 +#include <linux/types.h>
73 +#include <linux/sched.h>
75 +#ifdef CONFIG_CRASH_DUMP
76 +unsigned long long elfcorehdr_addr = ELFCORE_ADDR_MAX;
79 +/* This keeps a track of which one is crashing cpu. */
80 +int crashing_cpu = -1;
81 +static cpumask_t cpus_in_crash = CPU_MASK_NONE;
84 +void crash_shutdown_secondary(void *ignore)
86 + struct pt_regs *regs;
87 + int cpu = smp_processor_id();
89 + regs = task_pt_regs(current);
91 + if (!cpu_online(cpu))
94 + local_irq_disable();
95 + if (!cpu_isset(cpu, cpus_in_crash))
96 + crash_save_cpu(regs, cpu);
97 + cpu_set(cpu, cpus_in_crash);
99 + while (!atomic_read(&kexec_ready_to_reboot))
101 + relocated_kexec_smp_wait(NULL);
105 +static void crash_kexec_prepare_cpus(void)
107 + unsigned int msecs;
109 + unsigned int ncpus = num_online_cpus() - 1;/* Excluding the panic cpu */
111 + dump_send_ipi(crash_shutdown_secondary);
115 + * The crash CPU sends an IPI and wait for other CPUs to
116 + * respond. Delay of at least 10 seconds.
118 + printk(KERN_EMERG "Sending IPI to other cpus...\n");
120 + while ((cpus_weight(cpus_in_crash) < ncpus) && (--msecs > 0)) {
127 +static void crash_kexec_prepare_cpus(void) {}
130 +void default_machine_crash_shutdown(struct pt_regs *regs)
132 + local_irq_disable();
133 + crashing_cpu = smp_processor_id();
134 + crash_save_cpu(regs, crashing_cpu);
135 + crash_kexec_prepare_cpus();
136 + cpu_set(crashing_cpu, cpus_in_crash);
139 +++ b/arch/mips/kernel/crash_dump.c
141 +#include <linux/highmem.h>
142 +#include <linux/bootmem.h>
143 +#include <linux/crash_dump.h>
144 +#include <asm/uaccess.h>
146 +#ifdef CONFIG_PROC_VMCORE
147 +static int __init parse_elfcorehdr(char *p)
150 + elfcorehdr_addr = memparse(p, &p);
153 +__setup("elfcorehdr=", parse_elfcorehdr);
156 +static int __init parse_savemaxmem(char *p)
159 + saved_max_pfn = (memparse(p, &p) >> PAGE_SHIFT) - 1;
163 +__setup("savemaxmem=", parse_savemaxmem);
166 +static void *kdump_buf_page;
169 + * copy_oldmem_page - copy one page from "oldmem"
170 + * @pfn: page frame number to be copied
171 + * @buf: target memory address for the copy; this can be in kernel address
172 + * space or user address space (see @userbuf)
173 + * @csize: number of bytes to copy
174 + * @offset: offset in bytes into the page (based on pfn) to begin the copy
175 + * @userbuf: if set, @buf is in user address space, use copy_to_user(),
176 + * otherwise @buf is in kernel address space, use memcpy().
178 + * Copy a page from "oldmem". For this page, there is no pte mapped
179 + * in the current kernel.
181 + * Calling copy_to_user() in atomic context is not desirable. Hence first
182 + * copying the data to a pre-allocated kernel page and then copying to user
183 + * space in non-atomic context.
185 +ssize_t copy_oldmem_page(unsigned long pfn, char *buf,
186 + size_t csize, unsigned long offset, int userbuf)
193 + vaddr = kmap_atomic_pfn(pfn, KM_PTE0);
196 + memcpy(buf, (vaddr + offset), csize);
197 + kunmap_atomic(vaddr, KM_PTE0);
199 + if (!kdump_buf_page) {
200 + printk(KERN_WARNING "Kdump: Kdump buffer page not"
204 + copy_page(kdump_buf_page, vaddr);
205 + kunmap_atomic(vaddr, KM_PTE0);
206 + if (copy_to_user(buf, (kdump_buf_page + offset), csize))
213 +static int __init kdump_buf_page_init(void)
217 + kdump_buf_page = kmalloc(PAGE_SIZE, GFP_KERNEL);
218 + if (!kdump_buf_page) {
219 + printk(KERN_WARNING "Kdump: Failed to allocate kdump buffer"
226 +arch_initcall(kdump_buf_page_init);
227 --- a/arch/mips/kernel/machine_kexec.c
228 +++ b/arch/mips/kernel/machine_kexec.c
229 @@ -19,9 +19,19 @@ extern const size_t relocate_new_kernel_
230 extern unsigned long kexec_start_address;
231 extern unsigned long kexec_indirection_page;
233 +int (*_machine_kexec_prepare)(struct kimage *) = NULL;
234 +void (*_machine_kexec_shutdown)(void) = NULL;
235 +void (*_machine_crash_shutdown)(struct pt_regs *regs) = NULL;
237 +void (*relocated_kexec_smp_wait) (void *);
238 +atomic_t kexec_ready_to_reboot = ATOMIC_INIT(0);
242 machine_kexec_prepare(struct kimage *kimage)
244 + if (_machine_kexec_prepare)
245 + return _machine_kexec_prepare(kimage);
249 @@ -33,11 +43,17 @@ machine_kexec_cleanup(struct kimage *kim
251 machine_shutdown(void)
253 + if (_machine_kexec_shutdown)
254 + _machine_kexec_shutdown();
258 machine_crash_shutdown(struct pt_regs *regs)
260 + if (_machine_crash_shutdown)
261 + _machine_crash_shutdown(regs);
263 + default_machine_crash_shutdown(regs);
266 typedef void (*noretfun_t)(void) __attribute__((noreturn));
267 @@ -52,7 +68,9 @@ machine_kexec(struct kimage *image)
269 (unsigned long)page_address(image->control_code_page);
271 - kexec_start_address = (unsigned long) phys_to_virt(image->start);
272 + kexec_start_address =
273 + (unsigned long) phys_to_virt(image->start);
275 kexec_indirection_page =
276 (unsigned long) phys_to_virt(image->head & PAGE_MASK);
278 @@ -63,7 +81,7 @@ machine_kexec(struct kimage *image)
279 * The generic kexec code builds a page list with physical
280 * addresses. they are directly accessible through KSEG0 (or
281 * CKSEG0 or XPHYS if on 64bit system), hence the
282 - * pys_to_virt() call.
283 + * phys_to_virt() call.
285 for (ptr = &image->head; (entry = *ptr) && !(entry &IND_DONE);
286 ptr = (entry & IND_INDIRECTION) ?
287 @@ -81,5 +99,13 @@ machine_kexec(struct kimage *image)
288 printk("Will call new kernel at %08lx\n", image->start);
292 + /* All secondary cpus now may jump to kexec_wait cycle */
293 + relocated_kexec_smp_wait = reboot_code_buffer +
294 + (void *)(kexec_smp_wait - relocate_new_kernel);
296 + atomic_set(&kexec_ready_to_reboot, 1);
298 ((noretfun_t) reboot_code_buffer)();
301 --- a/arch/mips/kernel/relocate_kernel.S
302 +++ b/arch/mips/kernel/relocate_kernel.S
304 #include <asm/addrspace.h>
306 LEAF(relocate_new_kernel)
312 PTR_L s0, kexec_indirection_page
313 PTR_L s1, kexec_start_address
315 @@ -26,7 +31,6 @@ process_entry:
318 and s4, s2, ~0x1 /* store destination addr in s4 */
323 @@ -60,23 +64,100 @@ copy_word:
328 + /* kexec_flag reset is signal to other CPUs what kernel
329 + was moved to it's location. Note - we need relocated address
335 + PTR_LA t0,kexec_flag
342 /* jump to kexec_start_address */
344 END(relocate_new_kernel)
346 -kexec_start_address:
347 - EXPORT(kexec_start_address)
350 + * Other CPUs should wait until code is relocated and
351 + * then start at entry (?) point.
353 +LEAF(kexec_smp_wait)
358 + PTR_L s1, kexec_start_address
360 + /* Non-relocated address works for args and kexec_start_address ( old
361 + * kernel is not overwritten). But we need relocated address of
368 + PTR_LA t0,kexec_flag
377 + END(kexec_smp_wait)
381 + /* all PTR's must be aligned to 8 byte in 64-bit mode */
385 +/* All parameters to new kernel are passed in registers a0-a3.
386 + * kexec_args[0..3] are uses to prepare register values.
394 + .size kexec_args,PTRSIZE*4
398 + * Secondary CPUs may have different kernel parameters in
399 + * their registers a0-a3. secondary_kexec_args[0..3] are used
400 + * to prepare register values.
402 +EXPORT(secondary_kexec_args)
407 + .size secondary_kexec_args,PTRSIZE*4
413 +EXPORT(kexec_start_address)
415 .size kexec_start_address, PTRSIZE
417 -kexec_indirection_page:
418 - EXPORT(kexec_indirection_page)
419 +EXPORT(kexec_indirection_page)
421 .size kexec_indirection_page, PTRSIZE
423 relocate_new_kernel_end:
425 -relocate_new_kernel_size:
426 - EXPORT(relocate_new_kernel_size)
427 +EXPORT(relocate_new_kernel_size)
428 PTR relocate_new_kernel_end - relocate_new_kernel
429 .size relocate_new_kernel_size, PTRSIZE
430 --- a/arch/mips/kernel/setup.c
431 +++ b/arch/mips/kernel/setup.c
433 #include <linux/console.h>
434 #include <linux/pfn.h>
435 #include <linux/debugfs.h>
436 +#include <linux/kexec.h>
438 #include <asm/addrspace.h>
439 #include <asm/bootinfo.h>
440 @@ -488,12 +489,62 @@ static void __init arch_mem_init(char **
445 + if (crashk_res.start != crashk_res.end)
446 + reserve_bootmem(crashk_res.start,
447 + crashk_res.end - crashk_res.start + 1,
452 plat_swiotlb_setup();
457 +static inline unsigned long long get_total_mem(void)
459 + unsigned long long total;
460 + total = max_pfn - min_low_pfn;
461 + return total << PAGE_SHIFT;
464 +static void __init mips_parse_crashkernel(void)
466 + unsigned long long total_mem;
467 + unsigned long long crash_size, crash_base;
470 + total_mem = get_total_mem();
471 + ret = parse_crashkernel(boot_command_line, total_mem,
472 + &crash_size, &crash_base);
473 + if (ret != 0 || crash_size <= 0)
476 + crashk_res.start = crash_base;
477 + crashk_res.end = crash_base + crash_size - 1;
479 +static void __init request_crashkernel(struct resource *res)
483 + ret = request_resource(res, &crashk_res);
485 + printk(KERN_INFO "Reserving %ldMB of memory at %ldMB "
486 + "for crashkernel\n",
487 + (unsigned long)((crashk_res.end -
488 + crashk_res.start + 1) >> 20),
489 + (unsigned long)(crashk_res.start >> 20));
492 +static void __init mips_parse_crashkernel(void)
495 +static void __init request_crashkernel(struct resource *res)
500 static void __init resource_init(void)
503 @@ -509,6 +560,8 @@ static void __init resource_init(void)
505 * Request address space for all standard RAM.
507 + mips_parse_crashkernel();
509 for (i = 0; i < boot_mem_map.nr_map; i++) {
510 struct resource *res;
511 unsigned long start, end;
512 @@ -544,6 +597,7 @@ static void __init resource_init(void)
514 request_resource(res, &code_resource);
515 request_resource(res, &data_resource);
516 + request_crashkernel(res);
520 --- a/arch/mips/kernel/smp.c
521 +++ b/arch/mips/kernel/smp.c
522 @@ -433,3 +433,21 @@ void flush_tlb_one(unsigned long vaddr)
524 EXPORT_SYMBOL(flush_tlb_page);
525 EXPORT_SYMBOL(flush_tlb_one);
527 +#if defined(CONFIG_KEXEC)
528 +void (*dump_ipi_function_ptr)(void *) = NULL;
529 +void dump_send_ipi(void (*dump_ipi_callback)(void *))
532 + int cpu = smp_processor_id();
534 + dump_ipi_function_ptr = dump_ipi_callback;
536 + for_each_online_cpu(i)
538 + core_send_ipi(i, SMP_DUMP);
541 +EXPORT_SYMBOL(dump_send_ipi);
544 --- a/arch/mips/include/asm/kexec.h
545 +++ b/arch/mips/include/asm/kexec.h
550 +#include <asm/stacktrace.h>
552 +extern unsigned long long elfcorehdr_addr;
554 /* Maximum physical address we can use pages from */
555 #define KEXEC_SOURCE_MEMORY_LIMIT (0x20000000)
556 /* Maximum address we can reach in physical address mode */
557 #define KEXEC_DESTINATION_MEMORY_LIMIT (0x20000000)
558 /* Maximum address we can use for the control code buffer */
559 #define KEXEC_CONTROL_MEMORY_LIMIT (0x20000000)
561 -#define KEXEC_CONTROL_PAGE_SIZE 4096
562 +/* Reserve 3*4096 bytes for board-specific info */
563 +#define KEXEC_CONTROL_PAGE_SIZE (4096 + 3*4096)
565 /* The native architecture */
566 #define KEXEC_ARCH KEXEC_ARCH_MIPS
567 +#define MAX_NOTE_BYTES 1024
569 static inline void crash_setup_regs(struct pt_regs *newregs,
570 - struct pt_regs *oldregs)
571 + struct pt_regs *oldregs)
573 - /* Dummy implementation for now */
575 + memcpy(newregs, oldregs, sizeof(*newregs));
577 + prepare_frametrace(newregs);
582 +extern unsigned long kexec_args[4];
583 +extern int (*_machine_kexec_prepare)(struct kimage *);
584 +extern void (*_machine_kexec_shutdown)(void);
585 +extern void (*_machine_crash_shutdown)(struct pt_regs *regs);
586 +extern void default_machine_crash_shutdown(struct pt_regs *regs);
588 +extern const unsigned char kexec_smp_wait[];
589 +extern unsigned long secondary_kexec_args[4];
590 +extern void (*relocated_kexec_smp_wait) (void *);
591 +extern atomic_t kexec_ready_to_reboot;
595 #endif /* !_MIPS_KEXEC */
596 --- a/arch/mips/include/asm/smp.h
597 +++ b/arch/mips/include/asm/smp.h
598 @@ -40,6 +40,8 @@ extern int __cpu_logical_map[NR_CPUS];
599 #define SMP_CALL_FUNCTION 0x2
600 /* Octeon - Tell another core to flush its icache */
601 #define SMP_ICACHE_FLUSH 0x4
602 +/* Used by kexec crashdump to save all cpu's state */
603 +#define SMP_DUMP 0x8
605 extern volatile cpumask_t cpu_callin_map;
607 @@ -91,4 +93,9 @@ static inline void arch_send_call_functi
608 mp_ops->send_ipi_mask(mask, SMP_CALL_FUNCTION);
611 +extern void core_send_ipi(int cpu, unsigned int action);
612 +#if defined(CONFIG_KEXEC)
613 +extern void (*dump_ipi_function_ptr)(void *);
614 +void dump_send_ipi(void (*dump_ipi_callback)(void *));
616 #endif /* __ASM_SMP_H */