Skip to content

Commit 1bcca86

Browse files
tangyoulingchenhuacai
authored andcommitted
LoongArch: Add crash dump support for kexec_file
Enabling crash dump (kdump) includes: - Prepare contents of ELF header of a core dump file, /proc/vmcore, using crash_prepare_elf64_headers(). - Add "mem=size@start" parameter to the command line and pass it to the capture kernel. Limit the runtime memory area of the captured kernel to avoid disrupting the production kernel's runtime state. - Add "elfcorehdr=size@start" parameter to the cmdline. The basic usage for kdump (add the cmdline parameter crashkernel=512M to grub.cfg for production kernel): 1) Load capture kernel image (vmlinux.efi or vmlinux can both be used): # kexec -s -p vmlinuz.efi --initrd=initrd.img --reuse-cmdline 2) Do something to crash, like: # echo c > /proc/sysrq-trigger Signed-off-by: Youling Tang <tangyouling@kylinos.cn> Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
1 parent fc9c112 commit 1bcca86

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

arch/loongarch/kernel/machine_kexec_file.c

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,81 @@ static void cmdline_add_initrd(struct kimage *image, unsigned long *cmdline_tmpl
5555
*cmdline_tmplen += initrd_strlen;
5656
}
5757

58+
#ifdef CONFIG_CRASH_DUMP
59+
60+
static int prepare_elf_headers(void **addr, unsigned long *sz)
61+
{
62+
int ret, nr_ranges;
63+
uint64_t i;
64+
phys_addr_t start, end;
65+
struct crash_mem *cmem;
66+
67+
nr_ranges = 2; /* for exclusion of crashkernel region */
68+
for_each_mem_range(i, &start, &end)
69+
nr_ranges++;
70+
71+
cmem = kmalloc(struct_size(cmem, ranges, nr_ranges), GFP_KERNEL);
72+
if (!cmem)
73+
return -ENOMEM;
74+
75+
cmem->max_nr_ranges = nr_ranges;
76+
cmem->nr_ranges = 0;
77+
for_each_mem_range(i, &start, &end) {
78+
cmem->ranges[cmem->nr_ranges].start = start;
79+
cmem->ranges[cmem->nr_ranges].end = end - 1;
80+
cmem->nr_ranges++;
81+
}
82+
83+
/* Exclude crashkernel region */
84+
ret = crash_exclude_mem_range(cmem, crashk_res.start, crashk_res.end);
85+
if (ret < 0)
86+
goto out;
87+
88+
if (crashk_low_res.end) {
89+
ret = crash_exclude_mem_range(cmem, crashk_low_res.start, crashk_low_res.end);
90+
if (ret < 0)
91+
goto out;
92+
}
93+
94+
ret = crash_prepare_elf64_headers(cmem, true, addr, sz);
95+
96+
out:
97+
kfree(cmem);
98+
return ret;
99+
}
100+
101+
/*
102+
* Add the "mem=size@start" command line parameter to command line, indicating the
103+
* memory region the new kernel can use to boot into.
104+
*/
105+
static void cmdline_add_mem(unsigned long *cmdline_tmplen, char *modified_cmdline)
106+
{
107+
int mem_strlen = 0;
108+
109+
mem_strlen = sprintf(modified_cmdline + (*cmdline_tmplen), "mem=0x%llx@0x%llx ",
110+
crashk_res.end - crashk_res.start + 1, crashk_res.start);
111+
*cmdline_tmplen += mem_strlen;
112+
113+
if (crashk_low_res.end) {
114+
mem_strlen = sprintf(modified_cmdline + (*cmdline_tmplen), "mem=0x%llx@0x%llx ",
115+
crashk_low_res.end - crashk_low_res.start + 1, crashk_low_res.start);
116+
*cmdline_tmplen += mem_strlen;
117+
}
118+
}
119+
120+
/* Add the "elfcorehdr=size@start" command line parameter to command line. */
121+
static void cmdline_add_elfcorehdr(struct kimage *image, unsigned long *cmdline_tmplen,
122+
char *modified_cmdline, unsigned long elfcorehdr_sz)
123+
{
124+
int elfcorehdr_strlen = 0;
125+
126+
elfcorehdr_strlen = sprintf(modified_cmdline + (*cmdline_tmplen), "elfcorehdr=0x%lx@0x%lx ",
127+
elfcorehdr_sz, image->elf_load_addr);
128+
*cmdline_tmplen += elfcorehdr_strlen;
129+
}
130+
131+
#endif
132+
58133
/*
59134
* Try to add the initrd to the image. If it is not possible to find valid
60135
* locations, this function will undo changes to the image and return non zero.
@@ -82,6 +157,46 @@ int load_other_segments(struct kimage *image,
82157
/* Ensure it's null terminated */
83158
modified_cmdline[COMMAND_LINE_SIZE - 1] = '\0';
84159

160+
#ifdef CONFIG_CRASH_DUMP
161+
/* Load elf core header */
162+
if (image->type == KEXEC_TYPE_CRASH) {
163+
void *headers;
164+
unsigned long headers_sz;
165+
166+
ret = prepare_elf_headers(&headers, &headers_sz);
167+
if (ret < 0) {
168+
pr_err("Preparing elf core header failed\n");
169+
goto out_err;
170+
}
171+
172+
kbuf.buffer = headers;
173+
kbuf.bufsz = headers_sz;
174+
kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
175+
kbuf.memsz = headers_sz;
176+
kbuf.buf_align = SZ_64K; /* largest supported page size */
177+
kbuf.buf_max = ULONG_MAX;
178+
kbuf.top_down = true;
179+
180+
ret = kexec_add_buffer(&kbuf);
181+
if (ret < 0) {
182+
vfree(headers);
183+
goto out_err;
184+
}
185+
image->elf_headers = headers;
186+
image->elf_load_addr = kbuf.mem;
187+
image->elf_headers_sz = headers_sz;
188+
189+
kexec_dprintk("Loaded elf core header at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
190+
image->elf_load_addr, kbuf.bufsz, kbuf.memsz);
191+
192+
/* Add the mem=size@start parameter to the command line */
193+
cmdline_add_mem(&cmdline_tmplen, modified_cmdline);
194+
195+
/* Add the elfcorehdr=size@start parameter to the command line */
196+
cmdline_add_elfcorehdr(image, &cmdline_tmplen, modified_cmdline, headers_sz);
197+
}
198+
#endif
199+
85200
/* Load initrd */
86201
if (initrd) {
87202
kbuf.buffer = initrd;

0 commit comments

Comments
 (0)