140b0b3f8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2692f66f2SHari Bathini /* 3692f66f2SHari Bathini * crash.c - kernel crash support code. 4692f66f2SHari Bathini * Copyright (C) 2002-2004 Eric Biederman <[email protected]> 5692f66f2SHari Bathini */ 6692f66f2SHari Bathini 744e8a5e9SStephen Boyd #include <linux/buildid.h> 871d2bcecSPhilipp Rudo #include <linux/init.h> 9692f66f2SHari Bathini #include <linux/utsname.h> 10692f66f2SHari Bathini #include <linux/vmalloc.h> 1146d36b1bSTao Liu #include <linux/sizes.h> 126f991cc3SEric DeVolder #include <linux/kexec.h> 1324726275SEric DeVolder #include <linux/memory.h> 1424726275SEric DeVolder #include <linux/cpuhotplug.h> 150ab97169SBaoquan He #include <linux/memblock.h> 160ab97169SBaoquan He #include <linux/kexec.h> 170ab97169SBaoquan He #include <linux/kmemleak.h> 18692f66f2SHari Bathini 19692f66f2SHari Bathini #include <asm/page.h> 20692f66f2SHari Bathini #include <asm/sections.h> 21692f66f2SHari Bathini 22a24d22b2SEric Biggers #include <crypto/sha1.h> 230935288cSVijay Balakrishna 245fd8fea9SStephen Brennan #include "kallsyms_internal.h" 2524726275SEric DeVolder #include "kexec_internal.h" 265fd8fea9SStephen Brennan 276f991cc3SEric DeVolder /* Per cpu memory for storing cpu states in case of system crash. */ 286f991cc3SEric DeVolder note_buf_t __percpu *crash_notes; 29692f66f2SHari Bathini 30692f66f2SHari Bathini /* vmcoreinfo stuff */ 3123c85094SOmar Sandoval unsigned char *vmcoreinfo_data; 3223c85094SOmar Sandoval size_t vmcoreinfo_size; 33203e9e41SXunlei Pang u32 *vmcoreinfo_note; 34692f66f2SHari Bathini 351229384fSXunlei Pang /* trusted vmcoreinfo, e.g. we can make a copy in the crash memory */ 361229384fSXunlei Pang static unsigned char *vmcoreinfo_data_safecopy; 371229384fSXunlei Pang 38*b631b95dSBaoquan He /* Location of the reserved area for the crash kernel */ 39*b631b95dSBaoquan He struct resource crashk_res = { 40*b631b95dSBaoquan He .name = "Crash kernel", 41*b631b95dSBaoquan He .start = 0, 42*b631b95dSBaoquan He .end = 0, 43*b631b95dSBaoquan He .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM, 44*b631b95dSBaoquan He .desc = IORES_DESC_CRASH_KERNEL 45*b631b95dSBaoquan He }; 46*b631b95dSBaoquan He struct resource crashk_low_res = { 47*b631b95dSBaoquan He .name = "Crash kernel", 48*b631b95dSBaoquan He .start = 0, 49*b631b95dSBaoquan He .end = 0, 50*b631b95dSBaoquan He .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM, 51*b631b95dSBaoquan He .desc = IORES_DESC_CRASH_KERNEL 52*b631b95dSBaoquan He }; 53*b631b95dSBaoquan He 54692f66f2SHari Bathini /* 55692f66f2SHari Bathini * parsing the "crashkernel" commandline 56692f66f2SHari Bathini * 57692f66f2SHari Bathini * this code is intended to be called from architecture specific code 58692f66f2SHari Bathini */ 59692f66f2SHari Bathini 60692f66f2SHari Bathini 61692f66f2SHari Bathini /* 62692f66f2SHari Bathini * This function parses command lines in the format 63692f66f2SHari Bathini * 64692f66f2SHari Bathini * crashkernel=ramsize-range:size[,...][@offset] 65692f66f2SHari Bathini * 66692f66f2SHari Bathini * The function returns 0 on success and -EINVAL on failure. 67692f66f2SHari Bathini */ 68692f66f2SHari Bathini static int __init parse_crashkernel_mem(char *cmdline, 69692f66f2SHari Bathini unsigned long long system_ram, 70692f66f2SHari Bathini unsigned long long *crash_size, 71692f66f2SHari Bathini unsigned long long *crash_base) 72692f66f2SHari Bathini { 73692f66f2SHari Bathini char *cur = cmdline, *tmp; 7446d36b1bSTao Liu unsigned long long total_mem = system_ram; 7546d36b1bSTao Liu 7646d36b1bSTao Liu /* 7746d36b1bSTao Liu * Firmware sometimes reserves some memory regions for its own use, 7846d36b1bSTao Liu * so the system memory size is less than the actual physical memory 7946d36b1bSTao Liu * size. Work around this by rounding up the total size to 128M, 8046d36b1bSTao Liu * which is enough for most test cases. 8146d36b1bSTao Liu */ 8246d36b1bSTao Liu total_mem = roundup(total_mem, SZ_128M); 83692f66f2SHari Bathini 84692f66f2SHari Bathini /* for each entry of the comma-separated list */ 85692f66f2SHari Bathini do { 86692f66f2SHari Bathini unsigned long long start, end = ULLONG_MAX, size; 87692f66f2SHari Bathini 88692f66f2SHari Bathini /* get the start of the range */ 89692f66f2SHari Bathini start = memparse(cur, &tmp); 90692f66f2SHari Bathini if (cur == tmp) { 91692f66f2SHari Bathini pr_warn("crashkernel: Memory value expected\n"); 92692f66f2SHari Bathini return -EINVAL; 93692f66f2SHari Bathini } 94692f66f2SHari Bathini cur = tmp; 95692f66f2SHari Bathini if (*cur != '-') { 96692f66f2SHari Bathini pr_warn("crashkernel: '-' expected\n"); 97692f66f2SHari Bathini return -EINVAL; 98692f66f2SHari Bathini } 99692f66f2SHari Bathini cur++; 100692f66f2SHari Bathini 101692f66f2SHari Bathini /* if no ':' is here, than we read the end */ 102692f66f2SHari Bathini if (*cur != ':') { 103692f66f2SHari Bathini end = memparse(cur, &tmp); 104692f66f2SHari Bathini if (cur == tmp) { 105692f66f2SHari Bathini pr_warn("crashkernel: Memory value expected\n"); 106692f66f2SHari Bathini return -EINVAL; 107692f66f2SHari Bathini } 108692f66f2SHari Bathini cur = tmp; 109692f66f2SHari Bathini if (end <= start) { 110692f66f2SHari Bathini pr_warn("crashkernel: end <= start\n"); 111692f66f2SHari Bathini return -EINVAL; 112692f66f2SHari Bathini } 113692f66f2SHari Bathini } 114692f66f2SHari Bathini 115692f66f2SHari Bathini if (*cur != ':') { 116692f66f2SHari Bathini pr_warn("crashkernel: ':' expected\n"); 117692f66f2SHari Bathini return -EINVAL; 118692f66f2SHari Bathini } 119692f66f2SHari Bathini cur++; 120692f66f2SHari Bathini 121692f66f2SHari Bathini size = memparse(cur, &tmp); 122692f66f2SHari Bathini if (cur == tmp) { 123692f66f2SHari Bathini pr_warn("Memory value expected\n"); 124692f66f2SHari Bathini return -EINVAL; 125692f66f2SHari Bathini } 126692f66f2SHari Bathini cur = tmp; 12746d36b1bSTao Liu if (size >= total_mem) { 128692f66f2SHari Bathini pr_warn("crashkernel: invalid size\n"); 129692f66f2SHari Bathini return -EINVAL; 130692f66f2SHari Bathini } 131692f66f2SHari Bathini 132692f66f2SHari Bathini /* match ? */ 13346d36b1bSTao Liu if (total_mem >= start && total_mem < end) { 134692f66f2SHari Bathini *crash_size = size; 135692f66f2SHari Bathini break; 136692f66f2SHari Bathini } 137692f66f2SHari Bathini } while (*cur++ == ','); 138692f66f2SHari Bathini 139692f66f2SHari Bathini if (*crash_size > 0) { 140692f66f2SHari Bathini while (*cur && *cur != ' ' && *cur != '@') 141692f66f2SHari Bathini cur++; 142692f66f2SHari Bathini if (*cur == '@') { 143692f66f2SHari Bathini cur++; 144692f66f2SHari Bathini *crash_base = memparse(cur, &tmp); 145692f66f2SHari Bathini if (cur == tmp) { 146692f66f2SHari Bathini pr_warn("Memory value expected after '@'\n"); 147692f66f2SHari Bathini return -EINVAL; 148692f66f2SHari Bathini } 149692f66f2SHari Bathini } 150de40ccefSDave Young } else 151de40ccefSDave Young pr_info("crashkernel size resulted in zero bytes\n"); 152692f66f2SHari Bathini 153692f66f2SHari Bathini return 0; 154692f66f2SHari Bathini } 155692f66f2SHari Bathini 156692f66f2SHari Bathini /* 157692f66f2SHari Bathini * That function parses "simple" (old) crashkernel command lines like 158692f66f2SHari Bathini * 159692f66f2SHari Bathini * crashkernel=size[@offset] 160692f66f2SHari Bathini * 161692f66f2SHari Bathini * It returns 0 on success and -EINVAL on failure. 162692f66f2SHari Bathini */ 163692f66f2SHari Bathini static int __init parse_crashkernel_simple(char *cmdline, 164692f66f2SHari Bathini unsigned long long *crash_size, 165692f66f2SHari Bathini unsigned long long *crash_base) 166692f66f2SHari Bathini { 167692f66f2SHari Bathini char *cur = cmdline; 168692f66f2SHari Bathini 169692f66f2SHari Bathini *crash_size = memparse(cmdline, &cur); 170692f66f2SHari Bathini if (cmdline == cur) { 171692f66f2SHari Bathini pr_warn("crashkernel: memory value expected\n"); 172692f66f2SHari Bathini return -EINVAL; 173692f66f2SHari Bathini } 174692f66f2SHari Bathini 175692f66f2SHari Bathini if (*cur == '@') 176692f66f2SHari Bathini *crash_base = memparse(cur+1, &cur); 177692f66f2SHari Bathini else if (*cur != ' ' && *cur != '\0') { 178692f66f2SHari Bathini pr_warn("crashkernel: unrecognized char: %c\n", *cur); 179692f66f2SHari Bathini return -EINVAL; 180692f66f2SHari Bathini } 181692f66f2SHari Bathini 182692f66f2SHari Bathini return 0; 183692f66f2SHari Bathini } 184692f66f2SHari Bathini 185692f66f2SHari Bathini #define SUFFIX_HIGH 0 186692f66f2SHari Bathini #define SUFFIX_LOW 1 187692f66f2SHari Bathini #define SUFFIX_NULL 2 188692f66f2SHari Bathini static __initdata char *suffix_tbl[] = { 189692f66f2SHari Bathini [SUFFIX_HIGH] = ",high", 190692f66f2SHari Bathini [SUFFIX_LOW] = ",low", 191692f66f2SHari Bathini [SUFFIX_NULL] = NULL, 192692f66f2SHari Bathini }; 193692f66f2SHari Bathini 194692f66f2SHari Bathini /* 195692f66f2SHari Bathini * That function parses "suffix" crashkernel command lines like 196692f66f2SHari Bathini * 197692f66f2SHari Bathini * crashkernel=size,[high|low] 198692f66f2SHari Bathini * 199692f66f2SHari Bathini * It returns 0 on success and -EINVAL on failure. 200692f66f2SHari Bathini */ 201692f66f2SHari Bathini static int __init parse_crashkernel_suffix(char *cmdline, 202692f66f2SHari Bathini unsigned long long *crash_size, 203692f66f2SHari Bathini const char *suffix) 204692f66f2SHari Bathini { 205692f66f2SHari Bathini char *cur = cmdline; 206692f66f2SHari Bathini 207692f66f2SHari Bathini *crash_size = memparse(cmdline, &cur); 208692f66f2SHari Bathini if (cmdline == cur) { 209692f66f2SHari Bathini pr_warn("crashkernel: memory value expected\n"); 210692f66f2SHari Bathini return -EINVAL; 211692f66f2SHari Bathini } 212692f66f2SHari Bathini 213692f66f2SHari Bathini /* check with suffix */ 214692f66f2SHari Bathini if (strncmp(cur, suffix, strlen(suffix))) { 215692f66f2SHari Bathini pr_warn("crashkernel: unrecognized char: %c\n", *cur); 216692f66f2SHari Bathini return -EINVAL; 217692f66f2SHari Bathini } 218692f66f2SHari Bathini cur += strlen(suffix); 219692f66f2SHari Bathini if (*cur != ' ' && *cur != '\0') { 220692f66f2SHari Bathini pr_warn("crashkernel: unrecognized char: %c\n", *cur); 221692f66f2SHari Bathini return -EINVAL; 222692f66f2SHari Bathini } 223692f66f2SHari Bathini 224692f66f2SHari Bathini return 0; 225692f66f2SHari Bathini } 226692f66f2SHari Bathini 227692f66f2SHari Bathini static __init char *get_last_crashkernel(char *cmdline, 228692f66f2SHari Bathini const char *name, 229692f66f2SHari Bathini const char *suffix) 230692f66f2SHari Bathini { 231692f66f2SHari Bathini char *p = cmdline, *ck_cmdline = NULL; 232692f66f2SHari Bathini 233692f66f2SHari Bathini /* find crashkernel and use the last one if there are more */ 234692f66f2SHari Bathini p = strstr(p, name); 235692f66f2SHari Bathini while (p) { 236692f66f2SHari Bathini char *end_p = strchr(p, ' '); 237692f66f2SHari Bathini char *q; 238692f66f2SHari Bathini 239692f66f2SHari Bathini if (!end_p) 240692f66f2SHari Bathini end_p = p + strlen(p); 241692f66f2SHari Bathini 242692f66f2SHari Bathini if (!suffix) { 243692f66f2SHari Bathini int i; 244692f66f2SHari Bathini 245692f66f2SHari Bathini /* skip the one with any known suffix */ 246692f66f2SHari Bathini for (i = 0; suffix_tbl[i]; i++) { 247692f66f2SHari Bathini q = end_p - strlen(suffix_tbl[i]); 248692f66f2SHari Bathini if (!strncmp(q, suffix_tbl[i], 249692f66f2SHari Bathini strlen(suffix_tbl[i]))) 250692f66f2SHari Bathini goto next; 251692f66f2SHari Bathini } 252692f66f2SHari Bathini ck_cmdline = p; 253692f66f2SHari Bathini } else { 254692f66f2SHari Bathini q = end_p - strlen(suffix); 255692f66f2SHari Bathini if (!strncmp(q, suffix, strlen(suffix))) 256692f66f2SHari Bathini ck_cmdline = p; 257692f66f2SHari Bathini } 258692f66f2SHari Bathini next: 259692f66f2SHari Bathini p = strstr(p+1, name); 260692f66f2SHari Bathini } 261692f66f2SHari Bathini 262692f66f2SHari Bathini return ck_cmdline; 263692f66f2SHari Bathini } 264692f66f2SHari Bathini 265692f66f2SHari Bathini static int __init __parse_crashkernel(char *cmdline, 266692f66f2SHari Bathini unsigned long long system_ram, 267692f66f2SHari Bathini unsigned long long *crash_size, 268692f66f2SHari Bathini unsigned long long *crash_base, 269692f66f2SHari Bathini const char *suffix) 270692f66f2SHari Bathini { 271692f66f2SHari Bathini char *first_colon, *first_space; 272692f66f2SHari Bathini char *ck_cmdline; 273a6304272SBaoquan He char *name = "crashkernel="; 274692f66f2SHari Bathini 275692f66f2SHari Bathini BUG_ON(!crash_size || !crash_base); 276692f66f2SHari Bathini *crash_size = 0; 277692f66f2SHari Bathini *crash_base = 0; 278692f66f2SHari Bathini 279692f66f2SHari Bathini ck_cmdline = get_last_crashkernel(cmdline, name, suffix); 280692f66f2SHari Bathini if (!ck_cmdline) 2812e5920bbSZhen Lei return -ENOENT; 282692f66f2SHari Bathini 283692f66f2SHari Bathini ck_cmdline += strlen(name); 284692f66f2SHari Bathini 285692f66f2SHari Bathini if (suffix) 286692f66f2SHari Bathini return parse_crashkernel_suffix(ck_cmdline, crash_size, 287692f66f2SHari Bathini suffix); 288692f66f2SHari Bathini /* 289692f66f2SHari Bathini * if the commandline contains a ':', then that's the extended 290692f66f2SHari Bathini * syntax -- if not, it must be the classic syntax 291692f66f2SHari Bathini */ 292692f66f2SHari Bathini first_colon = strchr(ck_cmdline, ':'); 293692f66f2SHari Bathini first_space = strchr(ck_cmdline, ' '); 294692f66f2SHari Bathini if (first_colon && (!first_space || first_colon < first_space)) 295692f66f2SHari Bathini return parse_crashkernel_mem(ck_cmdline, system_ram, 296692f66f2SHari Bathini crash_size, crash_base); 297692f66f2SHari Bathini 298692f66f2SHari Bathini return parse_crashkernel_simple(ck_cmdline, crash_size, crash_base); 299692f66f2SHari Bathini } 300692f66f2SHari Bathini 301692f66f2SHari Bathini /* 302692f66f2SHari Bathini * That function is the entry point for command line parsing and should be 303692f66f2SHari Bathini * called from the arch-specific code. 30470916e9cSBaoquan He * 30570916e9cSBaoquan He * If crashkernel=,high|low is supported on architecture, non-NULL values 30670916e9cSBaoquan He * should be passed to parameters 'low_size' and 'high'. 307692f66f2SHari Bathini */ 308692f66f2SHari Bathini int __init parse_crashkernel(char *cmdline, 309692f66f2SHari Bathini unsigned long long system_ram, 310692f66f2SHari Bathini unsigned long long *crash_size, 311a9e1a3d8SBaoquan He unsigned long long *crash_base, 312a9e1a3d8SBaoquan He unsigned long long *low_size, 313a9e1a3d8SBaoquan He bool *high) 314692f66f2SHari Bathini { 315a9e1a3d8SBaoquan He int ret; 316a9e1a3d8SBaoquan He 317a9e1a3d8SBaoquan He /* crashkernel=X[@offset] */ 318a9e1a3d8SBaoquan He ret = __parse_crashkernel(cmdline, system_ram, crash_size, 319a9e1a3d8SBaoquan He crash_base, NULL); 32070916e9cSBaoquan He #ifdef CONFIG_ARCH_HAS_GENERIC_CRASHKERNEL_RESERVATION 32170916e9cSBaoquan He /* 32270916e9cSBaoquan He * If non-NULL 'high' passed in and no normal crashkernel 32370916e9cSBaoquan He * setting detected, try parsing crashkernel=,high|low. 32470916e9cSBaoquan He */ 32570916e9cSBaoquan He if (high && ret == -ENOENT) { 32670916e9cSBaoquan He ret = __parse_crashkernel(cmdline, 0, crash_size, 32770916e9cSBaoquan He crash_base, suffix_tbl[SUFFIX_HIGH]); 32870916e9cSBaoquan He if (ret || !*crash_size) 32970916e9cSBaoquan He return -EINVAL; 330a9e1a3d8SBaoquan He 33170916e9cSBaoquan He /* 33270916e9cSBaoquan He * crashkernel=Y,low can be specified or not, but invalid value 33370916e9cSBaoquan He * is not allowed. 33470916e9cSBaoquan He */ 33570916e9cSBaoquan He ret = __parse_crashkernel(cmdline, 0, low_size, 33670916e9cSBaoquan He crash_base, suffix_tbl[SUFFIX_LOW]); 33770916e9cSBaoquan He if (ret == -ENOENT) { 33870916e9cSBaoquan He *low_size = DEFAULT_CRASH_KERNEL_LOW_SIZE; 33970916e9cSBaoquan He ret = 0; 34070916e9cSBaoquan He } else if (ret) { 34170916e9cSBaoquan He return ret; 34270916e9cSBaoquan He } 34370916e9cSBaoquan He 34470916e9cSBaoquan He *high = true; 34570916e9cSBaoquan He } 34670916e9cSBaoquan He #endif 34770916e9cSBaoquan He if (!*crash_size) 34870916e9cSBaoquan He ret = -EINVAL; 34970916e9cSBaoquan He 35070916e9cSBaoquan He return ret; 351692f66f2SHari Bathini } 352692f66f2SHari Bathini 353692f66f2SHari Bathini int __init parse_crashkernel_high(char *cmdline, 354692f66f2SHari Bathini unsigned long long system_ram, 355692f66f2SHari Bathini unsigned long long *crash_size, 356692f66f2SHari Bathini unsigned long long *crash_base) 357692f66f2SHari Bathini { 358692f66f2SHari Bathini return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base, 359a6304272SBaoquan He suffix_tbl[SUFFIX_HIGH]); 360692f66f2SHari Bathini } 361692f66f2SHari Bathini 362692f66f2SHari Bathini int __init parse_crashkernel_low(char *cmdline, 363692f66f2SHari Bathini unsigned long long system_ram, 364692f66f2SHari Bathini unsigned long long *crash_size, 365692f66f2SHari Bathini unsigned long long *crash_base) 366692f66f2SHari Bathini { 367692f66f2SHari Bathini return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base, 368a6304272SBaoquan He suffix_tbl[SUFFIX_LOW]); 369692f66f2SHari Bathini } 370692f66f2SHari Bathini 37171d2bcecSPhilipp Rudo /* 37271d2bcecSPhilipp Rudo * Add a dummy early_param handler to mark crashkernel= as a known command line 37371d2bcecSPhilipp Rudo * parameter and suppress incorrect warnings in init/main.c. 37471d2bcecSPhilipp Rudo */ 37571d2bcecSPhilipp Rudo static int __init parse_crashkernel_dummy(char *arg) 37671d2bcecSPhilipp Rudo { 37771d2bcecSPhilipp Rudo return 0; 37871d2bcecSPhilipp Rudo } 37971d2bcecSPhilipp Rudo early_param("crashkernel", parse_crashkernel_dummy); 38071d2bcecSPhilipp Rudo 3810ab97169SBaoquan He #ifdef CONFIG_ARCH_HAS_GENERIC_CRASHKERNEL_RESERVATION 3820ab97169SBaoquan He static int __init reserve_crashkernel_low(unsigned long long low_size) 3830ab97169SBaoquan He { 3840ab97169SBaoquan He #ifdef CONFIG_64BIT 3850ab97169SBaoquan He unsigned long long low_base; 3860ab97169SBaoquan He 3870ab97169SBaoquan He low_base = memblock_phys_alloc_range(low_size, CRASH_ALIGN, 0, CRASH_ADDR_LOW_MAX); 3880ab97169SBaoquan He if (!low_base) { 3890ab97169SBaoquan He pr_err("cannot allocate crashkernel low memory (size:0x%llx).\n", low_size); 3900ab97169SBaoquan He return -ENOMEM; 3910ab97169SBaoquan He } 3920ab97169SBaoquan He 3930ab97169SBaoquan He pr_info("crashkernel low memory reserved: 0x%08llx - 0x%08llx (%lld MB)\n", 3940ab97169SBaoquan He low_base, low_base + low_size, low_size >> 20); 3950ab97169SBaoquan He 3960ab97169SBaoquan He crashk_low_res.start = low_base; 3970ab97169SBaoquan He crashk_low_res.end = low_base + low_size - 1; 3980ab97169SBaoquan He insert_resource(&iomem_resource, &crashk_low_res); 3990ab97169SBaoquan He #endif 4000ab97169SBaoquan He return 0; 4010ab97169SBaoquan He } 4020ab97169SBaoquan He 4030ab97169SBaoquan He void __init reserve_crashkernel_generic(char *cmdline, 4040ab97169SBaoquan He unsigned long long crash_size, 4050ab97169SBaoquan He unsigned long long crash_base, 4060ab97169SBaoquan He unsigned long long crash_low_size, 4070ab97169SBaoquan He bool high) 4080ab97169SBaoquan He { 4090ab97169SBaoquan He unsigned long long search_end = CRASH_ADDR_LOW_MAX, search_base = 0; 4100ab97169SBaoquan He bool fixed_base = false; 4110ab97169SBaoquan He 4120ab97169SBaoquan He /* User specifies base address explicitly. */ 4130ab97169SBaoquan He if (crash_base) { 4140ab97169SBaoquan He fixed_base = true; 4150ab97169SBaoquan He search_base = crash_base; 4160ab97169SBaoquan He search_end = crash_base + crash_size; 4170ab97169SBaoquan He } else if (high) { 4180ab97169SBaoquan He search_base = CRASH_ADDR_LOW_MAX; 4190ab97169SBaoquan He search_end = CRASH_ADDR_HIGH_MAX; 4200ab97169SBaoquan He } 4210ab97169SBaoquan He 4220ab97169SBaoquan He retry: 4230ab97169SBaoquan He crash_base = memblock_phys_alloc_range(crash_size, CRASH_ALIGN, 4240ab97169SBaoquan He search_base, search_end); 4250ab97169SBaoquan He if (!crash_base) { 4260ab97169SBaoquan He /* 4270ab97169SBaoquan He * For crashkernel=size[KMG]@offset[KMG], print out failure 4280ab97169SBaoquan He * message if can't reserve the specified region. 4290ab97169SBaoquan He */ 4300ab97169SBaoquan He if (fixed_base) { 4310ab97169SBaoquan He pr_warn("crashkernel reservation failed - memory is in use.\n"); 4320ab97169SBaoquan He return; 4330ab97169SBaoquan He } 4340ab97169SBaoquan He 4350ab97169SBaoquan He /* 4360ab97169SBaoquan He * For crashkernel=size[KMG], if the first attempt was for 4370ab97169SBaoquan He * low memory, fall back to high memory, the minimum required 4380ab97169SBaoquan He * low memory will be reserved later. 4390ab97169SBaoquan He */ 4400ab97169SBaoquan He if (!high && search_end == CRASH_ADDR_LOW_MAX) { 4410ab97169SBaoquan He search_end = CRASH_ADDR_HIGH_MAX; 4420ab97169SBaoquan He search_base = CRASH_ADDR_LOW_MAX; 4430ab97169SBaoquan He crash_low_size = DEFAULT_CRASH_KERNEL_LOW_SIZE; 4440ab97169SBaoquan He goto retry; 4450ab97169SBaoquan He } 4460ab97169SBaoquan He 4470ab97169SBaoquan He /* 4480ab97169SBaoquan He * For crashkernel=size[KMG],high, if the first attempt was 4490ab97169SBaoquan He * for high memory, fall back to low memory. 4500ab97169SBaoquan He */ 4510ab97169SBaoquan He if (high && search_end == CRASH_ADDR_HIGH_MAX) { 4520ab97169SBaoquan He search_end = CRASH_ADDR_LOW_MAX; 4530ab97169SBaoquan He search_base = 0; 4540ab97169SBaoquan He goto retry; 4550ab97169SBaoquan He } 4560ab97169SBaoquan He pr_warn("cannot allocate crashkernel (size:0x%llx)\n", 4570ab97169SBaoquan He crash_size); 4580ab97169SBaoquan He return; 4590ab97169SBaoquan He } 4600ab97169SBaoquan He 4610ab97169SBaoquan He if ((crash_base > CRASH_ADDR_LOW_MAX) && 4620ab97169SBaoquan He crash_low_size && reserve_crashkernel_low(crash_low_size)) { 4630ab97169SBaoquan He memblock_phys_free(crash_base, crash_size); 4640ab97169SBaoquan He return; 4650ab97169SBaoquan He } 4660ab97169SBaoquan He 4670ab97169SBaoquan He pr_info("crashkernel reserved: 0x%016llx - 0x%016llx (%lld MB)\n", 4680ab97169SBaoquan He crash_base, crash_base + crash_size, crash_size >> 20); 4690ab97169SBaoquan He 4700ab97169SBaoquan He /* 4710ab97169SBaoquan He * The crashkernel memory will be removed from the kernel linear 4720ab97169SBaoquan He * map. Inform kmemleak so that it won't try to access it. 4730ab97169SBaoquan He */ 4740ab97169SBaoquan He kmemleak_ignore_phys(crash_base); 4750ab97169SBaoquan He if (crashk_low_res.end) 4760ab97169SBaoquan He kmemleak_ignore_phys(crashk_low_res.start); 4770ab97169SBaoquan He 4780ab97169SBaoquan He crashk_res.start = crash_base; 4790ab97169SBaoquan He crashk_res.end = crash_base + crash_size - 1; 4800ab97169SBaoquan He insert_resource(&iomem_resource, &crashk_res); 4810ab97169SBaoquan He } 4820ab97169SBaoquan He #endif 4830ab97169SBaoquan He 4846f991cc3SEric DeVolder int crash_prepare_elf64_headers(struct crash_mem *mem, int need_kernel_map, 4856f991cc3SEric DeVolder void **addr, unsigned long *sz) 4866f991cc3SEric DeVolder { 4876f991cc3SEric DeVolder Elf64_Ehdr *ehdr; 4886f991cc3SEric DeVolder Elf64_Phdr *phdr; 4896f991cc3SEric DeVolder unsigned long nr_cpus = num_possible_cpus(), nr_phdr, elf_sz; 4906f991cc3SEric DeVolder unsigned char *buf; 4916f991cc3SEric DeVolder unsigned int cpu, i; 4926f991cc3SEric DeVolder unsigned long long notes_addr; 4936f991cc3SEric DeVolder unsigned long mstart, mend; 4946f991cc3SEric DeVolder 4956f991cc3SEric DeVolder /* extra phdr for vmcoreinfo ELF note */ 4966f991cc3SEric DeVolder nr_phdr = nr_cpus + 1; 4976f991cc3SEric DeVolder nr_phdr += mem->nr_ranges; 4986f991cc3SEric DeVolder 4996f991cc3SEric DeVolder /* 5006f991cc3SEric DeVolder * kexec-tools creates an extra PT_LOAD phdr for kernel text mapping 5016f991cc3SEric DeVolder * area (for example, ffffffff80000000 - ffffffffa0000000 on x86_64). 5026f991cc3SEric DeVolder * I think this is required by tools like gdb. So same physical 5036f991cc3SEric DeVolder * memory will be mapped in two ELF headers. One will contain kernel 5046f991cc3SEric DeVolder * text virtual addresses and other will have __va(physical) addresses. 5056f991cc3SEric DeVolder */ 5066f991cc3SEric DeVolder 5076f991cc3SEric DeVolder nr_phdr++; 5086f991cc3SEric DeVolder elf_sz = sizeof(Elf64_Ehdr) + nr_phdr * sizeof(Elf64_Phdr); 5096f991cc3SEric DeVolder elf_sz = ALIGN(elf_sz, ELF_CORE_HEADER_ALIGN); 5106f991cc3SEric DeVolder 5116f991cc3SEric DeVolder buf = vzalloc(elf_sz); 5126f991cc3SEric DeVolder if (!buf) 5136f991cc3SEric DeVolder return -ENOMEM; 5146f991cc3SEric DeVolder 5156f991cc3SEric DeVolder ehdr = (Elf64_Ehdr *)buf; 5166f991cc3SEric DeVolder phdr = (Elf64_Phdr *)(ehdr + 1); 5176f991cc3SEric DeVolder memcpy(ehdr->e_ident, ELFMAG, SELFMAG); 5186f991cc3SEric DeVolder ehdr->e_ident[EI_CLASS] = ELFCLASS64; 5196f991cc3SEric DeVolder ehdr->e_ident[EI_DATA] = ELFDATA2LSB; 5206f991cc3SEric DeVolder ehdr->e_ident[EI_VERSION] = EV_CURRENT; 5216f991cc3SEric DeVolder ehdr->e_ident[EI_OSABI] = ELF_OSABI; 5226f991cc3SEric DeVolder memset(ehdr->e_ident + EI_PAD, 0, EI_NIDENT - EI_PAD); 5236f991cc3SEric DeVolder ehdr->e_type = ET_CORE; 5246f991cc3SEric DeVolder ehdr->e_machine = ELF_ARCH; 5256f991cc3SEric DeVolder ehdr->e_version = EV_CURRENT; 5266f991cc3SEric DeVolder ehdr->e_phoff = sizeof(Elf64_Ehdr); 5276f991cc3SEric DeVolder ehdr->e_ehsize = sizeof(Elf64_Ehdr); 5286f991cc3SEric DeVolder ehdr->e_phentsize = sizeof(Elf64_Phdr); 5296f991cc3SEric DeVolder 530a396d0f8SEric DeVolder /* Prepare one phdr of type PT_NOTE for each possible CPU */ 531a396d0f8SEric DeVolder for_each_possible_cpu(cpu) { 5326f991cc3SEric DeVolder phdr->p_type = PT_NOTE; 5336f991cc3SEric DeVolder notes_addr = per_cpu_ptr_to_phys(per_cpu_ptr(crash_notes, cpu)); 5346f991cc3SEric DeVolder phdr->p_offset = phdr->p_paddr = notes_addr; 5356f991cc3SEric DeVolder phdr->p_filesz = phdr->p_memsz = sizeof(note_buf_t); 5366f991cc3SEric DeVolder (ehdr->e_phnum)++; 5376f991cc3SEric DeVolder phdr++; 5386f991cc3SEric DeVolder } 5396f991cc3SEric DeVolder 5406f991cc3SEric DeVolder /* Prepare one PT_NOTE header for vmcoreinfo */ 5416f991cc3SEric DeVolder phdr->p_type = PT_NOTE; 5426f991cc3SEric DeVolder phdr->p_offset = phdr->p_paddr = paddr_vmcoreinfo_note(); 5436f991cc3SEric DeVolder phdr->p_filesz = phdr->p_memsz = VMCOREINFO_NOTE_SIZE; 5446f991cc3SEric DeVolder (ehdr->e_phnum)++; 5456f991cc3SEric DeVolder phdr++; 5466f991cc3SEric DeVolder 5476f991cc3SEric DeVolder /* Prepare PT_LOAD type program header for kernel text region */ 5486f991cc3SEric DeVolder if (need_kernel_map) { 5496f991cc3SEric DeVolder phdr->p_type = PT_LOAD; 5506f991cc3SEric DeVolder phdr->p_flags = PF_R|PF_W|PF_X; 5516f991cc3SEric DeVolder phdr->p_vaddr = (unsigned long) _text; 5526f991cc3SEric DeVolder phdr->p_filesz = phdr->p_memsz = _end - _text; 5536f991cc3SEric DeVolder phdr->p_offset = phdr->p_paddr = __pa_symbol(_text); 5546f991cc3SEric DeVolder ehdr->e_phnum++; 5556f991cc3SEric DeVolder phdr++; 5566f991cc3SEric DeVolder } 5576f991cc3SEric DeVolder 5586f991cc3SEric DeVolder /* Go through all the ranges in mem->ranges[] and prepare phdr */ 5596f991cc3SEric DeVolder for (i = 0; i < mem->nr_ranges; i++) { 5606f991cc3SEric DeVolder mstart = mem->ranges[i].start; 5616f991cc3SEric DeVolder mend = mem->ranges[i].end; 5626f991cc3SEric DeVolder 5636f991cc3SEric DeVolder phdr->p_type = PT_LOAD; 5646f991cc3SEric DeVolder phdr->p_flags = PF_R|PF_W|PF_X; 5656f991cc3SEric DeVolder phdr->p_offset = mstart; 5666f991cc3SEric DeVolder 5676f991cc3SEric DeVolder phdr->p_paddr = mstart; 5686f991cc3SEric DeVolder phdr->p_vaddr = (unsigned long) __va(mstart); 5696f991cc3SEric DeVolder phdr->p_filesz = phdr->p_memsz = mend - mstart + 1; 5706f991cc3SEric DeVolder phdr->p_align = 0; 5716f991cc3SEric DeVolder ehdr->e_phnum++; 5726f991cc3SEric DeVolder pr_debug("Crash PT_LOAD ELF header. phdr=%p vaddr=0x%llx, paddr=0x%llx, sz=0x%llx e_phnum=%d p_offset=0x%llx\n", 5736f991cc3SEric DeVolder phdr, phdr->p_vaddr, phdr->p_paddr, phdr->p_filesz, 5746f991cc3SEric DeVolder ehdr->e_phnum, phdr->p_offset); 5756f991cc3SEric DeVolder phdr++; 5766f991cc3SEric DeVolder } 5776f991cc3SEric DeVolder 5786f991cc3SEric DeVolder *addr = buf; 5796f991cc3SEric DeVolder *sz = elf_sz; 5806f991cc3SEric DeVolder return 0; 5816f991cc3SEric DeVolder } 5826f991cc3SEric DeVolder 5836f991cc3SEric DeVolder int crash_exclude_mem_range(struct crash_mem *mem, 5846f991cc3SEric DeVolder unsigned long long mstart, unsigned long long mend) 5856f991cc3SEric DeVolder { 5866f991cc3SEric DeVolder int i, j; 5876f991cc3SEric DeVolder unsigned long long start, end, p_start, p_end; 5886f991cc3SEric DeVolder struct range temp_range = {0, 0}; 5896f991cc3SEric DeVolder 5906f991cc3SEric DeVolder for (i = 0; i < mem->nr_ranges; i++) { 5916f991cc3SEric DeVolder start = mem->ranges[i].start; 5926f991cc3SEric DeVolder end = mem->ranges[i].end; 5936f991cc3SEric DeVolder p_start = mstart; 5946f991cc3SEric DeVolder p_end = mend; 5956f991cc3SEric DeVolder 5966f991cc3SEric DeVolder if (mstart > end || mend < start) 5976f991cc3SEric DeVolder continue; 5986f991cc3SEric DeVolder 5996f991cc3SEric DeVolder /* Truncate any area outside of range */ 6006f991cc3SEric DeVolder if (mstart < start) 6016f991cc3SEric DeVolder p_start = start; 6026f991cc3SEric DeVolder if (mend > end) 6036f991cc3SEric DeVolder p_end = end; 6046f991cc3SEric DeVolder 6056f991cc3SEric DeVolder /* Found completely overlapping range */ 6066f991cc3SEric DeVolder if (p_start == start && p_end == end) { 6076f991cc3SEric DeVolder mem->ranges[i].start = 0; 6086f991cc3SEric DeVolder mem->ranges[i].end = 0; 6096f991cc3SEric DeVolder if (i < mem->nr_ranges - 1) { 6106f991cc3SEric DeVolder /* Shift rest of the ranges to left */ 6116f991cc3SEric DeVolder for (j = i; j < mem->nr_ranges - 1; j++) { 6126f991cc3SEric DeVolder mem->ranges[j].start = 6136f991cc3SEric DeVolder mem->ranges[j+1].start; 6146f991cc3SEric DeVolder mem->ranges[j].end = 6156f991cc3SEric DeVolder mem->ranges[j+1].end; 6166f991cc3SEric DeVolder } 6176f991cc3SEric DeVolder 6186f991cc3SEric DeVolder /* 6196f991cc3SEric DeVolder * Continue to check if there are another overlapping ranges 6206f991cc3SEric DeVolder * from the current position because of shifting the above 6216f991cc3SEric DeVolder * mem ranges. 6226f991cc3SEric DeVolder */ 6236f991cc3SEric DeVolder i--; 6246f991cc3SEric DeVolder mem->nr_ranges--; 6256f991cc3SEric DeVolder continue; 6266f991cc3SEric DeVolder } 6276f991cc3SEric DeVolder mem->nr_ranges--; 6286f991cc3SEric DeVolder return 0; 6296f991cc3SEric DeVolder } 6306f991cc3SEric DeVolder 6316f991cc3SEric DeVolder if (p_start > start && p_end < end) { 6326f991cc3SEric DeVolder /* Split original range */ 6336f991cc3SEric DeVolder mem->ranges[i].end = p_start - 1; 6346f991cc3SEric DeVolder temp_range.start = p_end + 1; 6356f991cc3SEric DeVolder temp_range.end = end; 6366f991cc3SEric DeVolder } else if (p_start != start) 6376f991cc3SEric DeVolder mem->ranges[i].end = p_start - 1; 6386f991cc3SEric DeVolder else 6396f991cc3SEric DeVolder mem->ranges[i].start = p_end + 1; 6406f991cc3SEric DeVolder break; 6416f991cc3SEric DeVolder } 6426f991cc3SEric DeVolder 6436f991cc3SEric DeVolder /* If a split happened, add the split to array */ 6446f991cc3SEric DeVolder if (!temp_range.end) 6456f991cc3SEric DeVolder return 0; 6466f991cc3SEric DeVolder 6476f991cc3SEric DeVolder /* Split happened */ 6486f991cc3SEric DeVolder if (i == mem->max_nr_ranges - 1) 6496f991cc3SEric DeVolder return -ENOMEM; 6506f991cc3SEric DeVolder 6516f991cc3SEric DeVolder /* Location where new range should go */ 6526f991cc3SEric DeVolder j = i + 1; 6536f991cc3SEric DeVolder if (j < mem->nr_ranges) { 6546f991cc3SEric DeVolder /* Move over all ranges one slot towards the end */ 6556f991cc3SEric DeVolder for (i = mem->nr_ranges - 1; i >= j; i--) 6566f991cc3SEric DeVolder mem->ranges[i + 1] = mem->ranges[i]; 6576f991cc3SEric DeVolder } 6586f991cc3SEric DeVolder 6596f991cc3SEric DeVolder mem->ranges[j].start = temp_range.start; 6606f991cc3SEric DeVolder mem->ranges[j].end = temp_range.end; 6616f991cc3SEric DeVolder mem->nr_ranges++; 6626f991cc3SEric DeVolder return 0; 6636f991cc3SEric DeVolder } 6646f991cc3SEric DeVolder 66551dbd925SHari Bathini Elf_Word *append_elf_note(Elf_Word *buf, char *name, unsigned int type, 666692f66f2SHari Bathini void *data, size_t data_len) 667692f66f2SHari Bathini { 66851dbd925SHari Bathini struct elf_note *note = (struct elf_note *)buf; 669692f66f2SHari Bathini 67051dbd925SHari Bathini note->n_namesz = strlen(name) + 1; 67151dbd925SHari Bathini note->n_descsz = data_len; 67251dbd925SHari Bathini note->n_type = type; 67351dbd925SHari Bathini buf += DIV_ROUND_UP(sizeof(*note), sizeof(Elf_Word)); 67451dbd925SHari Bathini memcpy(buf, name, note->n_namesz); 67551dbd925SHari Bathini buf += DIV_ROUND_UP(note->n_namesz, sizeof(Elf_Word)); 67651dbd925SHari Bathini memcpy(buf, data, data_len); 67751dbd925SHari Bathini buf += DIV_ROUND_UP(data_len, sizeof(Elf_Word)); 678692f66f2SHari Bathini 679692f66f2SHari Bathini return buf; 680692f66f2SHari Bathini } 681692f66f2SHari Bathini 68251dbd925SHari Bathini void final_note(Elf_Word *buf) 683692f66f2SHari Bathini { 68451dbd925SHari Bathini memset(buf, 0, sizeof(struct elf_note)); 685692f66f2SHari Bathini } 686692f66f2SHari Bathini 687692f66f2SHari Bathini static void update_vmcoreinfo_note(void) 688692f66f2SHari Bathini { 689692f66f2SHari Bathini u32 *buf = vmcoreinfo_note; 690692f66f2SHari Bathini 691692f66f2SHari Bathini if (!vmcoreinfo_size) 692692f66f2SHari Bathini return; 693692f66f2SHari Bathini buf = append_elf_note(buf, VMCOREINFO_NOTE_NAME, 0, vmcoreinfo_data, 694692f66f2SHari Bathini vmcoreinfo_size); 695692f66f2SHari Bathini final_note(buf); 696692f66f2SHari Bathini } 697692f66f2SHari Bathini 6981229384fSXunlei Pang void crash_update_vmcoreinfo_safecopy(void *ptr) 6991229384fSXunlei Pang { 7001229384fSXunlei Pang if (ptr) 7011229384fSXunlei Pang memcpy(ptr, vmcoreinfo_data, vmcoreinfo_size); 7021229384fSXunlei Pang 7031229384fSXunlei Pang vmcoreinfo_data_safecopy = ptr; 7041229384fSXunlei Pang } 7051229384fSXunlei Pang 706692f66f2SHari Bathini void crash_save_vmcoreinfo(void) 707692f66f2SHari Bathini { 708203e9e41SXunlei Pang if (!vmcoreinfo_note) 709203e9e41SXunlei Pang return; 710203e9e41SXunlei Pang 7111229384fSXunlei Pang /* Use the safe copy to generate vmcoreinfo note if have */ 7121229384fSXunlei Pang if (vmcoreinfo_data_safecopy) 7131229384fSXunlei Pang vmcoreinfo_data = vmcoreinfo_data_safecopy; 7141229384fSXunlei Pang 71591bc9aafSArnd Bergmann vmcoreinfo_append_str("CRASHTIME=%lld\n", ktime_get_real_seconds()); 716692f66f2SHari Bathini update_vmcoreinfo_note(); 717692f66f2SHari Bathini } 718692f66f2SHari Bathini 719692f66f2SHari Bathini void vmcoreinfo_append_str(const char *fmt, ...) 720692f66f2SHari Bathini { 721692f66f2SHari Bathini va_list args; 722692f66f2SHari Bathini char buf[0x50]; 723692f66f2SHari Bathini size_t r; 724692f66f2SHari Bathini 725692f66f2SHari Bathini va_start(args, fmt); 726692f66f2SHari Bathini r = vscnprintf(buf, sizeof(buf), fmt, args); 727692f66f2SHari Bathini va_end(args); 728692f66f2SHari Bathini 7295203f499SXunlei Pang r = min(r, (size_t)VMCOREINFO_BYTES - vmcoreinfo_size); 730692f66f2SHari Bathini 731692f66f2SHari Bathini memcpy(&vmcoreinfo_data[vmcoreinfo_size], buf, r); 732692f66f2SHari Bathini 733692f66f2SHari Bathini vmcoreinfo_size += r; 73408fc35f3SStephen Brennan 73508fc35f3SStephen Brennan WARN_ONCE(vmcoreinfo_size == VMCOREINFO_BYTES, 73608fc35f3SStephen Brennan "vmcoreinfo data exceeds allocated size, truncating"); 737692f66f2SHari Bathini } 738692f66f2SHari Bathini 739692f66f2SHari Bathini /* 740692f66f2SHari Bathini * provide an empty default implementation here -- architecture 741692f66f2SHari Bathini * code may override this 742692f66f2SHari Bathini */ 743692f66f2SHari Bathini void __weak arch_crash_save_vmcoreinfo(void) 744692f66f2SHari Bathini {} 745692f66f2SHari Bathini 746692f66f2SHari Bathini phys_addr_t __weak paddr_vmcoreinfo_note(void) 747692f66f2SHari Bathini { 748203e9e41SXunlei Pang return __pa(vmcoreinfo_note); 749692f66f2SHari Bathini } 75043d4cb47SMarc-André Lureau EXPORT_SYMBOL(paddr_vmcoreinfo_note); 751692f66f2SHari Bathini 752692f66f2SHari Bathini static int __init crash_save_vmcoreinfo_init(void) 753692f66f2SHari Bathini { 754203e9e41SXunlei Pang vmcoreinfo_data = (unsigned char *)get_zeroed_page(GFP_KERNEL); 755203e9e41SXunlei Pang if (!vmcoreinfo_data) { 756203e9e41SXunlei Pang pr_warn("Memory allocation for vmcoreinfo_data failed\n"); 757203e9e41SXunlei Pang return -ENOMEM; 758203e9e41SXunlei Pang } 759203e9e41SXunlei Pang 760203e9e41SXunlei Pang vmcoreinfo_note = alloc_pages_exact(VMCOREINFO_NOTE_SIZE, 761203e9e41SXunlei Pang GFP_KERNEL | __GFP_ZERO); 762203e9e41SXunlei Pang if (!vmcoreinfo_note) { 763203e9e41SXunlei Pang free_page((unsigned long)vmcoreinfo_data); 764203e9e41SXunlei Pang vmcoreinfo_data = NULL; 765203e9e41SXunlei Pang pr_warn("Memory allocation for vmcoreinfo_note failed\n"); 766203e9e41SXunlei Pang return -ENOMEM; 767203e9e41SXunlei Pang } 768203e9e41SXunlei Pang 769692f66f2SHari Bathini VMCOREINFO_OSRELEASE(init_uts_ns.name.release); 77044e8a5e9SStephen Boyd VMCOREINFO_BUILD_ID(); 771692f66f2SHari Bathini VMCOREINFO_PAGESIZE(PAGE_SIZE); 772692f66f2SHari Bathini 773692f66f2SHari Bathini VMCOREINFO_SYMBOL(init_uts_ns); 774ca4a9241SAlexander Egorenkov VMCOREINFO_OFFSET(uts_namespace, name); 775692f66f2SHari Bathini VMCOREINFO_SYMBOL(node_online_map); 776692f66f2SHari Bathini #ifdef CONFIG_MMU 777eff4345eSOmar Sandoval VMCOREINFO_SYMBOL_ARRAY(swapper_pg_dir); 778692f66f2SHari Bathini #endif 779692f66f2SHari Bathini VMCOREINFO_SYMBOL(_stext); 780692f66f2SHari Bathini VMCOREINFO_SYMBOL(vmap_area_list); 781692f66f2SHari Bathini 782a9ee6cf5SMike Rapoport #ifndef CONFIG_NUMA 783692f66f2SHari Bathini VMCOREINFO_SYMBOL(mem_map); 784692f66f2SHari Bathini VMCOREINFO_SYMBOL(contig_page_data); 785692f66f2SHari Bathini #endif 786692f66f2SHari Bathini #ifdef CONFIG_SPARSEMEM 787a0b12803SKirill A. Shutemov VMCOREINFO_SYMBOL_ARRAY(mem_section); 788692f66f2SHari Bathini VMCOREINFO_LENGTH(mem_section, NR_SECTION_ROOTS); 789692f66f2SHari Bathini VMCOREINFO_STRUCT_SIZE(mem_section); 790692f66f2SHari Bathini VMCOREINFO_OFFSET(mem_section, section_mem_map); 7914f5aecdfSPingfan Liu VMCOREINFO_NUMBER(SECTION_SIZE_BITS); 7921d50e5d0SBhupesh Sharma VMCOREINFO_NUMBER(MAX_PHYSMEM_BITS); 793692f66f2SHari Bathini #endif 794692f66f2SHari Bathini VMCOREINFO_STRUCT_SIZE(page); 795692f66f2SHari Bathini VMCOREINFO_STRUCT_SIZE(pglist_data); 796692f66f2SHari Bathini VMCOREINFO_STRUCT_SIZE(zone); 797692f66f2SHari Bathini VMCOREINFO_STRUCT_SIZE(free_area); 798692f66f2SHari Bathini VMCOREINFO_STRUCT_SIZE(list_head); 799692f66f2SHari Bathini VMCOREINFO_SIZE(nodemask_t); 800692f66f2SHari Bathini VMCOREINFO_OFFSET(page, flags); 801692f66f2SHari Bathini VMCOREINFO_OFFSET(page, _refcount); 802692f66f2SHari Bathini VMCOREINFO_OFFSET(page, mapping); 803692f66f2SHari Bathini VMCOREINFO_OFFSET(page, lru); 804692f66f2SHari Bathini VMCOREINFO_OFFSET(page, _mapcount); 805692f66f2SHari Bathini VMCOREINFO_OFFSET(page, private); 806692f66f2SHari Bathini VMCOREINFO_OFFSET(page, compound_head); 807692f66f2SHari Bathini VMCOREINFO_OFFSET(pglist_data, node_zones); 808692f66f2SHari Bathini VMCOREINFO_OFFSET(pglist_data, nr_zones); 80943b02ba9SMike Rapoport #ifdef CONFIG_FLATMEM 810692f66f2SHari Bathini VMCOREINFO_OFFSET(pglist_data, node_mem_map); 811692f66f2SHari Bathini #endif 812692f66f2SHari Bathini VMCOREINFO_OFFSET(pglist_data, node_start_pfn); 813692f66f2SHari Bathini VMCOREINFO_OFFSET(pglist_data, node_spanned_pages); 814692f66f2SHari Bathini VMCOREINFO_OFFSET(pglist_data, node_id); 815692f66f2SHari Bathini VMCOREINFO_OFFSET(zone, free_area); 816692f66f2SHari Bathini VMCOREINFO_OFFSET(zone, vm_stat); 817692f66f2SHari Bathini VMCOREINFO_OFFSET(zone, spanned_pages); 818692f66f2SHari Bathini VMCOREINFO_OFFSET(free_area, free_list); 819692f66f2SHari Bathini VMCOREINFO_OFFSET(list_head, next); 820692f66f2SHari Bathini VMCOREINFO_OFFSET(list_head, prev); 821692f66f2SHari Bathini VMCOREINFO_OFFSET(vmap_area, va_start); 822692f66f2SHari Bathini VMCOREINFO_OFFSET(vmap_area, list); 82323baf831SKirill A. Shutemov VMCOREINFO_LENGTH(zone.free_area, MAX_ORDER + 1); 824692f66f2SHari Bathini log_buf_vmcoreinfo_setup(); 825692f66f2SHari Bathini VMCOREINFO_LENGTH(free_area.free_list, MIGRATE_TYPES); 826692f66f2SHari Bathini VMCOREINFO_NUMBER(NR_FREE_PAGES); 827692f66f2SHari Bathini VMCOREINFO_NUMBER(PG_lru); 828692f66f2SHari Bathini VMCOREINFO_NUMBER(PG_private); 829692f66f2SHari Bathini VMCOREINFO_NUMBER(PG_swapcache); 8301cbf29daSPetr Tesarik VMCOREINFO_NUMBER(PG_swapbacked); 831692f66f2SHari Bathini VMCOREINFO_NUMBER(PG_slab); 832692f66f2SHari Bathini #ifdef CONFIG_MEMORY_FAILURE 833692f66f2SHari Bathini VMCOREINFO_NUMBER(PG_hwpoison); 834692f66f2SHari Bathini #endif 835692f66f2SHari Bathini VMCOREINFO_NUMBER(PG_head_mask); 8366e292b9bSMatthew Wilcox #define PAGE_BUDDY_MAPCOUNT_VALUE (~PG_buddy) 837692f66f2SHari Bathini VMCOREINFO_NUMBER(PAGE_BUDDY_MAPCOUNT_VALUE); 838692f66f2SHari Bathini #ifdef CONFIG_HUGETLB_PAGE 8399c5ccf2dSMatthew Wilcox (Oracle) VMCOREINFO_NUMBER(PG_hugetlb); 840e04b742fSDavid Hildenbrand #define PAGE_OFFLINE_MAPCOUNT_VALUE (~PG_offline) 841e04b742fSDavid Hildenbrand VMCOREINFO_NUMBER(PAGE_OFFLINE_MAPCOUNT_VALUE); 842692f66f2SHari Bathini #endif 843692f66f2SHari Bathini 8445fd8fea9SStephen Brennan #ifdef CONFIG_KALLSYMS 8455fd8fea9SStephen Brennan VMCOREINFO_SYMBOL(kallsyms_names); 846f09bddbdSStephen Brennan VMCOREINFO_SYMBOL(kallsyms_num_syms); 8475fd8fea9SStephen Brennan VMCOREINFO_SYMBOL(kallsyms_token_table); 8485fd8fea9SStephen Brennan VMCOREINFO_SYMBOL(kallsyms_token_index); 8495fd8fea9SStephen Brennan #ifdef CONFIG_KALLSYMS_BASE_RELATIVE 8505fd8fea9SStephen Brennan VMCOREINFO_SYMBOL(kallsyms_offsets); 8515fd8fea9SStephen Brennan VMCOREINFO_SYMBOL(kallsyms_relative_base); 8525fd8fea9SStephen Brennan #else 8535fd8fea9SStephen Brennan VMCOREINFO_SYMBOL(kallsyms_addresses); 8545fd8fea9SStephen Brennan #endif /* CONFIG_KALLSYMS_BASE_RELATIVE */ 8555fd8fea9SStephen Brennan #endif /* CONFIG_KALLSYMS */ 8565fd8fea9SStephen Brennan 857692f66f2SHari Bathini arch_crash_save_vmcoreinfo(); 858692f66f2SHari Bathini update_vmcoreinfo_note(); 859692f66f2SHari Bathini 860692f66f2SHari Bathini return 0; 861692f66f2SHari Bathini } 862692f66f2SHari Bathini 863692f66f2SHari Bathini subsys_initcall(crash_save_vmcoreinfo_init); 8646f991cc3SEric DeVolder 8656f991cc3SEric DeVolder static int __init crash_notes_memory_init(void) 8666f991cc3SEric DeVolder { 8676f991cc3SEric DeVolder /* Allocate memory for saving cpu registers. */ 8686f991cc3SEric DeVolder size_t size, align; 8696f991cc3SEric DeVolder 8706f991cc3SEric DeVolder /* 8716f991cc3SEric DeVolder * crash_notes could be allocated across 2 vmalloc pages when percpu 8726f991cc3SEric DeVolder * is vmalloc based . vmalloc doesn't guarantee 2 continuous vmalloc 8736f991cc3SEric DeVolder * pages are also on 2 continuous physical pages. In this case the 8746f991cc3SEric DeVolder * 2nd part of crash_notes in 2nd page could be lost since only the 8756f991cc3SEric DeVolder * starting address and size of crash_notes are exported through sysfs. 8766f991cc3SEric DeVolder * Here round up the size of crash_notes to the nearest power of two 8776f991cc3SEric DeVolder * and pass it to __alloc_percpu as align value. This can make sure 8786f991cc3SEric DeVolder * crash_notes is allocated inside one physical page. 8796f991cc3SEric DeVolder */ 8806f991cc3SEric DeVolder size = sizeof(note_buf_t); 8816f991cc3SEric DeVolder align = min(roundup_pow_of_two(sizeof(note_buf_t)), PAGE_SIZE); 8826f991cc3SEric DeVolder 8836f991cc3SEric DeVolder /* 8846f991cc3SEric DeVolder * Break compile if size is bigger than PAGE_SIZE since crash_notes 8856f991cc3SEric DeVolder * definitely will be in 2 pages with that. 8866f991cc3SEric DeVolder */ 8876f991cc3SEric DeVolder BUILD_BUG_ON(size > PAGE_SIZE); 8886f991cc3SEric DeVolder 8896f991cc3SEric DeVolder crash_notes = __alloc_percpu(size, align); 8906f991cc3SEric DeVolder if (!crash_notes) { 8916f991cc3SEric DeVolder pr_warn("Memory allocation for saving cpu register states failed\n"); 8926f991cc3SEric DeVolder return -ENOMEM; 8936f991cc3SEric DeVolder } 8946f991cc3SEric DeVolder return 0; 8956f991cc3SEric DeVolder } 8966f991cc3SEric DeVolder subsys_initcall(crash_notes_memory_init); 89724726275SEric DeVolder 89824726275SEric DeVolder #ifdef CONFIG_CRASH_HOTPLUG 89924726275SEric DeVolder #undef pr_fmt 90024726275SEric DeVolder #define pr_fmt(fmt) "crash hp: " fmt 901a72bbec7SEric DeVolder 902a72bbec7SEric DeVolder /* 903e2a8f20dSBaoquan He * Different than kexec/kdump loading/unloading/jumping/shrinking which 904e2a8f20dSBaoquan He * usually rarely happen, there will be many crash hotplug events notified 905e2a8f20dSBaoquan He * during one short period, e.g one memory board is hot added and memory 906e2a8f20dSBaoquan He * regions are online. So mutex lock __crash_hotplug_lock is used to 907e2a8f20dSBaoquan He * serialize the crash hotplug handling specifically. 908e2a8f20dSBaoquan He */ 909e2a8f20dSBaoquan He DEFINE_MUTEX(__crash_hotplug_lock); 910e2a8f20dSBaoquan He #define crash_hotplug_lock() mutex_lock(&__crash_hotplug_lock) 911e2a8f20dSBaoquan He #define crash_hotplug_unlock() mutex_unlock(&__crash_hotplug_lock) 912e2a8f20dSBaoquan He 913e2a8f20dSBaoquan He /* 914a72bbec7SEric DeVolder * This routine utilized when the crash_hotplug sysfs node is read. 915a72bbec7SEric DeVolder * It reflects the kernel's ability/permission to update the crash 916a72bbec7SEric DeVolder * elfcorehdr directly. 917a72bbec7SEric DeVolder */ 918a72bbec7SEric DeVolder int crash_check_update_elfcorehdr(void) 919a72bbec7SEric DeVolder { 920a72bbec7SEric DeVolder int rc = 0; 921a72bbec7SEric DeVolder 922e2a8f20dSBaoquan He crash_hotplug_lock(); 923a72bbec7SEric DeVolder /* Obtain lock while reading crash information */ 924a72bbec7SEric DeVolder if (!kexec_trylock()) { 925a72bbec7SEric DeVolder pr_info("kexec_trylock() failed, elfcorehdr may be inaccurate\n"); 926e2a8f20dSBaoquan He crash_hotplug_unlock(); 927a72bbec7SEric DeVolder return 0; 928a72bbec7SEric DeVolder } 929a72bbec7SEric DeVolder if (kexec_crash_image) { 930a72bbec7SEric DeVolder if (kexec_crash_image->file_mode) 931a72bbec7SEric DeVolder rc = 1; 932a72bbec7SEric DeVolder else 933a72bbec7SEric DeVolder rc = kexec_crash_image->update_elfcorehdr; 934a72bbec7SEric DeVolder } 935a72bbec7SEric DeVolder /* Release lock now that update complete */ 936a72bbec7SEric DeVolder kexec_unlock(); 937e2a8f20dSBaoquan He crash_hotplug_unlock(); 938a72bbec7SEric DeVolder 939a72bbec7SEric DeVolder return rc; 940a72bbec7SEric DeVolder } 941a72bbec7SEric DeVolder 94224726275SEric DeVolder /* 94324726275SEric DeVolder * To accurately reflect hot un/plug changes of cpu and memory resources 94424726275SEric DeVolder * (including onling and offlining of those resources), the elfcorehdr 94524726275SEric DeVolder * (which is passed to the crash kernel via the elfcorehdr= parameter) 94624726275SEric DeVolder * must be updated with the new list of CPUs and memories. 94724726275SEric DeVolder * 94824726275SEric DeVolder * In order to make changes to elfcorehdr, two conditions are needed: 94924726275SEric DeVolder * First, the segment containing the elfcorehdr must be large enough 95024726275SEric DeVolder * to permit a growing number of resources; the elfcorehdr memory size 95124726275SEric DeVolder * is based on NR_CPUS_DEFAULT and CRASH_MAX_MEMORY_RANGES. 95224726275SEric DeVolder * Second, purgatory must explicitly exclude the elfcorehdr from the 95324726275SEric DeVolder * list of segments it checks (since the elfcorehdr changes and thus 95424726275SEric DeVolder * would require an update to purgatory itself to update the digest). 95524726275SEric DeVolder */ 95624726275SEric DeVolder static void crash_handle_hotplug_event(unsigned int hp_action, unsigned int cpu) 95724726275SEric DeVolder { 95824726275SEric DeVolder struct kimage *image; 95924726275SEric DeVolder 960e2a8f20dSBaoquan He crash_hotplug_lock(); 96124726275SEric DeVolder /* Obtain lock while changing crash information */ 96224726275SEric DeVolder if (!kexec_trylock()) { 96324726275SEric DeVolder pr_info("kexec_trylock() failed, elfcorehdr may be inaccurate\n"); 964e2a8f20dSBaoquan He crash_hotplug_unlock(); 96524726275SEric DeVolder return; 96624726275SEric DeVolder } 96724726275SEric DeVolder 96824726275SEric DeVolder /* Check kdump is not loaded */ 96924726275SEric DeVolder if (!kexec_crash_image) 97024726275SEric DeVolder goto out; 97124726275SEric DeVolder 97224726275SEric DeVolder image = kexec_crash_image; 97324726275SEric DeVolder 974a72bbec7SEric DeVolder /* Check that updating elfcorehdr is permitted */ 975a72bbec7SEric DeVolder if (!(image->file_mode || image->update_elfcorehdr)) 976a72bbec7SEric DeVolder goto out; 977a72bbec7SEric DeVolder 97824726275SEric DeVolder if (hp_action == KEXEC_CRASH_HP_ADD_CPU || 97924726275SEric DeVolder hp_action == KEXEC_CRASH_HP_REMOVE_CPU) 98024726275SEric DeVolder pr_debug("hp_action %u, cpu %u\n", hp_action, cpu); 98124726275SEric DeVolder else 98224726275SEric DeVolder pr_debug("hp_action %u\n", hp_action); 98324726275SEric DeVolder 98424726275SEric DeVolder /* 98524726275SEric DeVolder * The elfcorehdr_index is set to -1 when the struct kimage 98624726275SEric DeVolder * is allocated. Find the segment containing the elfcorehdr, 98724726275SEric DeVolder * if not already found. 98824726275SEric DeVolder */ 98924726275SEric DeVolder if (image->elfcorehdr_index < 0) { 99024726275SEric DeVolder unsigned long mem; 99124726275SEric DeVolder unsigned char *ptr; 99224726275SEric DeVolder unsigned int n; 99324726275SEric DeVolder 99424726275SEric DeVolder for (n = 0; n < image->nr_segments; n++) { 99524726275SEric DeVolder mem = image->segment[n].mem; 99624726275SEric DeVolder ptr = kmap_local_page(pfn_to_page(mem >> PAGE_SHIFT)); 99724726275SEric DeVolder if (ptr) { 99824726275SEric DeVolder /* The segment containing elfcorehdr */ 99924726275SEric DeVolder if (memcmp(ptr, ELFMAG, SELFMAG) == 0) 100024726275SEric DeVolder image->elfcorehdr_index = (int)n; 100124726275SEric DeVolder kunmap_local(ptr); 100224726275SEric DeVolder } 100324726275SEric DeVolder } 100424726275SEric DeVolder } 100524726275SEric DeVolder 100624726275SEric DeVolder if (image->elfcorehdr_index < 0) { 100724726275SEric DeVolder pr_err("unable to locate elfcorehdr segment"); 100824726275SEric DeVolder goto out; 100924726275SEric DeVolder } 101024726275SEric DeVolder 101124726275SEric DeVolder /* Needed in order for the segments to be updated */ 101224726275SEric DeVolder arch_kexec_unprotect_crashkres(); 101324726275SEric DeVolder 101424726275SEric DeVolder /* Differentiate between normal load and hotplug update */ 101524726275SEric DeVolder image->hp_action = hp_action; 101624726275SEric DeVolder 101724726275SEric DeVolder /* Now invoke arch-specific update handler */ 101824726275SEric DeVolder arch_crash_handle_hotplug_event(image); 101924726275SEric DeVolder 102024726275SEric DeVolder /* No longer handling a hotplug event */ 102124726275SEric DeVolder image->hp_action = KEXEC_CRASH_HP_NONE; 102224726275SEric DeVolder image->elfcorehdr_updated = true; 102324726275SEric DeVolder 102424726275SEric DeVolder /* Change back to read-only */ 102524726275SEric DeVolder arch_kexec_protect_crashkres(); 102624726275SEric DeVolder 102724726275SEric DeVolder /* Errors in the callback is not a reason to rollback state */ 102824726275SEric DeVolder out: 102924726275SEric DeVolder /* Release lock now that update complete */ 103024726275SEric DeVolder kexec_unlock(); 1031e2a8f20dSBaoquan He crash_hotplug_unlock(); 103224726275SEric DeVolder } 103324726275SEric DeVolder 103424726275SEric DeVolder static int crash_memhp_notifier(struct notifier_block *nb, unsigned long val, void *v) 103524726275SEric DeVolder { 103624726275SEric DeVolder switch (val) { 103724726275SEric DeVolder case MEM_ONLINE: 103824726275SEric DeVolder crash_handle_hotplug_event(KEXEC_CRASH_HP_ADD_MEMORY, 103924726275SEric DeVolder KEXEC_CRASH_HP_INVALID_CPU); 104024726275SEric DeVolder break; 104124726275SEric DeVolder 104224726275SEric DeVolder case MEM_OFFLINE: 104324726275SEric DeVolder crash_handle_hotplug_event(KEXEC_CRASH_HP_REMOVE_MEMORY, 104424726275SEric DeVolder KEXEC_CRASH_HP_INVALID_CPU); 104524726275SEric DeVolder break; 104624726275SEric DeVolder } 104724726275SEric DeVolder return NOTIFY_OK; 104824726275SEric DeVolder } 104924726275SEric DeVolder 105024726275SEric DeVolder static struct notifier_block crash_memhp_nb = { 105124726275SEric DeVolder .notifier_call = crash_memhp_notifier, 105224726275SEric DeVolder .priority = 0 105324726275SEric DeVolder }; 105424726275SEric DeVolder 105524726275SEric DeVolder static int crash_cpuhp_online(unsigned int cpu) 105624726275SEric DeVolder { 105724726275SEric DeVolder crash_handle_hotplug_event(KEXEC_CRASH_HP_ADD_CPU, cpu); 105824726275SEric DeVolder return 0; 105924726275SEric DeVolder } 106024726275SEric DeVolder 106124726275SEric DeVolder static int crash_cpuhp_offline(unsigned int cpu) 106224726275SEric DeVolder { 106324726275SEric DeVolder crash_handle_hotplug_event(KEXEC_CRASH_HP_REMOVE_CPU, cpu); 106424726275SEric DeVolder return 0; 106524726275SEric DeVolder } 106624726275SEric DeVolder 106724726275SEric DeVolder static int __init crash_hotplug_init(void) 106824726275SEric DeVolder { 106924726275SEric DeVolder int result = 0; 107024726275SEric DeVolder 107124726275SEric DeVolder if (IS_ENABLED(CONFIG_MEMORY_HOTPLUG)) 107224726275SEric DeVolder register_memory_notifier(&crash_memhp_nb); 107324726275SEric DeVolder 107424726275SEric DeVolder if (IS_ENABLED(CONFIG_HOTPLUG_CPU)) { 107524726275SEric DeVolder result = cpuhp_setup_state_nocalls(CPUHP_BP_PREPARE_DYN, 107624726275SEric DeVolder "crash/cpuhp", crash_cpuhp_online, crash_cpuhp_offline); 107724726275SEric DeVolder } 107824726275SEric DeVolder 107924726275SEric DeVolder return result; 108024726275SEric DeVolder } 108124726275SEric DeVolder 108224726275SEric DeVolder subsys_initcall(crash_hotplug_init); 108324726275SEric DeVolder #endif 1084