1 // SPDX-License-Identifier: GPL-2.0-only
3 * crash.c - kernel crash support code.
4 * Copyright (C) 2002-2004 Eric Biederman <ebiederm@xmission.com>
7 #include <linux/buildid.h>
8 #include <linux/init.h>
9 #include <linux/utsname.h>
10 #include <linux/vmalloc.h>
11 #include <linux/sizes.h>
12 #include <linux/kexec.h>
13 #include <linux/memory.h>
14 #include <linux/cpuhotplug.h>
15 #include <linux/memblock.h>
16 #include <linux/kexec.h>
17 #include <linux/kmemleak.h>
20 #include <asm/sections.h>
22 #include <crypto/sha1.h>
24 #include "kallsyms_internal.h"
25 #include "kexec_internal.h"
27 /* Location of the reserved area for the crash kernel */
28 struct resource crashk_res = {
29 .name = "Crash kernel",
32 .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
33 .desc = IORES_DESC_CRASH_KERNEL
35 struct resource crashk_low_res = {
36 .name = "Crash kernel",
39 .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
40 .desc = IORES_DESC_CRASH_KERNEL
44 * parsing the "crashkernel" commandline
46 * this code is intended to be called from architecture specific code
51 * This function parses command lines in the format
53 * crashkernel=ramsize-range:size[,...][@offset]
55 * The function returns 0 on success and -EINVAL on failure.
57 static int __init parse_crashkernel_mem(char *cmdline,
58 unsigned long long system_ram,
59 unsigned long long *crash_size,
60 unsigned long long *crash_base)
62 char *cur = cmdline, *tmp;
63 unsigned long long total_mem = system_ram;
66 * Firmware sometimes reserves some memory regions for its own use,
67 * so the system memory size is less than the actual physical memory
68 * size. Work around this by rounding up the total size to 128M,
69 * which is enough for most test cases.
71 total_mem = roundup(total_mem, SZ_128M);
73 /* for each entry of the comma-separated list */
75 unsigned long long start, end = ULLONG_MAX, size;
77 /* get the start of the range */
78 start = memparse(cur, &tmp);
80 pr_warn("crashkernel: Memory value expected\n");
85 pr_warn("crashkernel: '-' expected\n");
90 /* if no ':' is here, than we read the end */
92 end = memparse(cur, &tmp);
94 pr_warn("crashkernel: Memory value expected\n");
99 pr_warn("crashkernel: end <= start\n");
105 pr_warn("crashkernel: ':' expected\n");
110 size = memparse(cur, &tmp);
112 pr_warn("Memory value expected\n");
116 if (size >= total_mem) {
117 pr_warn("crashkernel: invalid size\n");
122 if (total_mem >= start && total_mem < end) {
126 } while (*cur++ == ',');
128 if (*crash_size > 0) {
129 while (*cur && *cur != ' ' && *cur != '@')
133 *crash_base = memparse(cur, &tmp);
135 pr_warn("Memory value expected after '@'\n");
140 pr_info("crashkernel size resulted in zero bytes\n");
146 * That function parses "simple" (old) crashkernel command lines like
148 * crashkernel=size[@offset]
150 * It returns 0 on success and -EINVAL on failure.
152 static int __init parse_crashkernel_simple(char *cmdline,
153 unsigned long long *crash_size,
154 unsigned long long *crash_base)
158 *crash_size = memparse(cmdline, &cur);
159 if (cmdline == cur) {
160 pr_warn("crashkernel: memory value expected\n");
165 *crash_base = memparse(cur+1, &cur);
166 else if (*cur != ' ' && *cur != '\0') {
167 pr_warn("crashkernel: unrecognized char: %c\n", *cur);
174 #define SUFFIX_HIGH 0
176 #define SUFFIX_NULL 2
177 static __initdata char *suffix_tbl[] = {
178 [SUFFIX_HIGH] = ",high",
179 [SUFFIX_LOW] = ",low",
180 [SUFFIX_NULL] = NULL,
184 * That function parses "suffix" crashkernel command lines like
186 * crashkernel=size,[high|low]
188 * It returns 0 on success and -EINVAL on failure.
190 static int __init parse_crashkernel_suffix(char *cmdline,
191 unsigned long long *crash_size,
196 *crash_size = memparse(cmdline, &cur);
197 if (cmdline == cur) {
198 pr_warn("crashkernel: memory value expected\n");
202 /* check with suffix */
203 if (strncmp(cur, suffix, strlen(suffix))) {
204 pr_warn("crashkernel: unrecognized char: %c\n", *cur);
207 cur += strlen(suffix);
208 if (*cur != ' ' && *cur != '\0') {
209 pr_warn("crashkernel: unrecognized char: %c\n", *cur);
216 static __init char *get_last_crashkernel(char *cmdline,
220 char *p = cmdline, *ck_cmdline = NULL;
222 /* find crashkernel and use the last one if there are more */
225 char *end_p = strchr(p, ' ');
229 end_p = p + strlen(p);
234 /* skip the one with any known suffix */
235 for (i = 0; suffix_tbl[i]; i++) {
236 q = end_p - strlen(suffix_tbl[i]);
237 if (!strncmp(q, suffix_tbl[i],
238 strlen(suffix_tbl[i])))
243 q = end_p - strlen(suffix);
244 if (!strncmp(q, suffix, strlen(suffix)))
248 p = strstr(p+1, name);
254 static int __init __parse_crashkernel(char *cmdline,
255 unsigned long long system_ram,
256 unsigned long long *crash_size,
257 unsigned long long *crash_base,
260 char *first_colon, *first_space;
262 char *name = "crashkernel=";
264 BUG_ON(!crash_size || !crash_base);
268 ck_cmdline = get_last_crashkernel(cmdline, name, suffix);
272 ck_cmdline += strlen(name);
275 return parse_crashkernel_suffix(ck_cmdline, crash_size,
278 * if the commandline contains a ':', then that's the extended
279 * syntax -- if not, it must be the classic syntax
281 first_colon = strchr(ck_cmdline, ':');
282 first_space = strchr(ck_cmdline, ' ');
283 if (first_colon && (!first_space || first_colon < first_space))
284 return parse_crashkernel_mem(ck_cmdline, system_ram,
285 crash_size, crash_base);
287 return parse_crashkernel_simple(ck_cmdline, crash_size, crash_base);
291 * That function is the entry point for command line parsing and should be
292 * called from the arch-specific code.
294 * If crashkernel=,high|low is supported on architecture, non-NULL values
295 * should be passed to parameters 'low_size' and 'high'.
297 int __init parse_crashkernel(char *cmdline,
298 unsigned long long system_ram,
299 unsigned long long *crash_size,
300 unsigned long long *crash_base,
301 unsigned long long *low_size,
306 /* crashkernel=X[@offset] */
307 ret = __parse_crashkernel(cmdline, system_ram, crash_size,
309 #ifdef CONFIG_ARCH_HAS_GENERIC_CRASHKERNEL_RESERVATION
311 * If non-NULL 'high' passed in and no normal crashkernel
312 * setting detected, try parsing crashkernel=,high|low.
314 if (high && ret == -ENOENT) {
315 ret = __parse_crashkernel(cmdline, 0, crash_size,
316 crash_base, suffix_tbl[SUFFIX_HIGH]);
317 if (ret || !*crash_size)
321 * crashkernel=Y,low can be specified or not, but invalid value
324 ret = __parse_crashkernel(cmdline, 0, low_size,
325 crash_base, suffix_tbl[SUFFIX_LOW]);
326 if (ret == -ENOENT) {
327 *low_size = DEFAULT_CRASH_KERNEL_LOW_SIZE;
343 * Add a dummy early_param handler to mark crashkernel= as a known command line
344 * parameter and suppress incorrect warnings in init/main.c.
346 static int __init parse_crashkernel_dummy(char *arg)
350 early_param("crashkernel", parse_crashkernel_dummy);
352 #ifdef CONFIG_ARCH_HAS_GENERIC_CRASHKERNEL_RESERVATION
353 static int __init reserve_crashkernel_low(unsigned long long low_size)
356 unsigned long long low_base;
358 low_base = memblock_phys_alloc_range(low_size, CRASH_ALIGN, 0, CRASH_ADDR_LOW_MAX);
360 pr_err("cannot allocate crashkernel low memory (size:0x%llx).\n", low_size);
364 pr_info("crashkernel low memory reserved: 0x%08llx - 0x%08llx (%lld MB)\n",
365 low_base, low_base + low_size, low_size >> 20);
367 crashk_low_res.start = low_base;
368 crashk_low_res.end = low_base + low_size - 1;
369 insert_resource(&iomem_resource, &crashk_low_res);
374 void __init reserve_crashkernel_generic(char *cmdline,
375 unsigned long long crash_size,
376 unsigned long long crash_base,
377 unsigned long long crash_low_size,
380 unsigned long long search_end = CRASH_ADDR_LOW_MAX, search_base = 0;
381 bool fixed_base = false;
383 /* User specifies base address explicitly. */
386 search_base = crash_base;
387 search_end = crash_base + crash_size;
389 search_base = CRASH_ADDR_LOW_MAX;
390 search_end = CRASH_ADDR_HIGH_MAX;
394 crash_base = memblock_phys_alloc_range(crash_size, CRASH_ALIGN,
395 search_base, search_end);
398 * For crashkernel=size[KMG]@offset[KMG], print out failure
399 * message if can't reserve the specified region.
402 pr_warn("crashkernel reservation failed - memory is in use.\n");
407 * For crashkernel=size[KMG], if the first attempt was for
408 * low memory, fall back to high memory, the minimum required
409 * low memory will be reserved later.
411 if (!high && search_end == CRASH_ADDR_LOW_MAX) {
412 search_end = CRASH_ADDR_HIGH_MAX;
413 search_base = CRASH_ADDR_LOW_MAX;
414 crash_low_size = DEFAULT_CRASH_KERNEL_LOW_SIZE;
419 * For crashkernel=size[KMG],high, if the first attempt was
420 * for high memory, fall back to low memory.
422 if (high && search_end == CRASH_ADDR_HIGH_MAX) {
423 search_end = CRASH_ADDR_LOW_MAX;
427 pr_warn("cannot allocate crashkernel (size:0x%llx)\n",
432 if ((crash_base >= CRASH_ADDR_LOW_MAX) &&
433 crash_low_size && reserve_crashkernel_low(crash_low_size)) {
434 memblock_phys_free(crash_base, crash_size);
438 pr_info("crashkernel reserved: 0x%016llx - 0x%016llx (%lld MB)\n",
439 crash_base, crash_base + crash_size, crash_size >> 20);
442 * The crashkernel memory will be removed from the kernel linear
443 * map. Inform kmemleak so that it won't try to access it.
445 kmemleak_ignore_phys(crash_base);
446 if (crashk_low_res.end)
447 kmemleak_ignore_phys(crashk_low_res.start);
449 crashk_res.start = crash_base;
450 crashk_res.end = crash_base + crash_size - 1;
453 static __init int insert_crashkernel_resources(void)
455 if (crashk_res.start < crashk_res.end)
456 insert_resource(&iomem_resource, &crashk_res);
458 if (crashk_low_res.start < crashk_low_res.end)
459 insert_resource(&iomem_resource, &crashk_low_res);
463 early_initcall(insert_crashkernel_resources);