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 38b631b95dSBaoquan He /* Location of the reserved area for the crash kernel */ 39b631b95dSBaoquan He struct resource crashk_res = { 40b631b95dSBaoquan He .name = "Crash kernel", 41b631b95dSBaoquan He .start = 0, 42b631b95dSBaoquan He .end = 0, 43b631b95dSBaoquan He .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM, 44b631b95dSBaoquan He .desc = IORES_DESC_CRASH_KERNEL 45b631b95dSBaoquan He }; 46b631b95dSBaoquan He struct resource crashk_low_res = { 47b631b95dSBaoquan He .name = "Crash kernel", 48b631b95dSBaoquan He .start = 0, 49b631b95dSBaoquan He .end = 0, 50b631b95dSBaoquan He .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM, 51b631b95dSBaoquan He .desc = IORES_DESC_CRASH_KERNEL 52b631b95dSBaoquan He }; 53b631b95dSBaoquan 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 35371d2bcecSPhilipp Rudo /* 35471d2bcecSPhilipp Rudo * Add a dummy early_param handler to mark crashkernel= as a known command line 35571d2bcecSPhilipp Rudo * parameter and suppress incorrect warnings in init/main.c. 35671d2bcecSPhilipp Rudo */ 35771d2bcecSPhilipp Rudo static int __init parse_crashkernel_dummy(char *arg) 35871d2bcecSPhilipp Rudo { 35971d2bcecSPhilipp Rudo return 0; 36071d2bcecSPhilipp Rudo } 36171d2bcecSPhilipp Rudo early_param("crashkernel", parse_crashkernel_dummy); 36271d2bcecSPhilipp Rudo 3630ab97169SBaoquan He #ifdef CONFIG_ARCH_HAS_GENERIC_CRASHKERNEL_RESERVATION 3640ab97169SBaoquan He static int __init reserve_crashkernel_low(unsigned long long low_size) 3650ab97169SBaoquan He { 3660ab97169SBaoquan He #ifdef CONFIG_64BIT 3670ab97169SBaoquan He unsigned long long low_base; 3680ab97169SBaoquan He 3690ab97169SBaoquan He low_base = memblock_phys_alloc_range(low_size, CRASH_ALIGN, 0, CRASH_ADDR_LOW_MAX); 3700ab97169SBaoquan He if (!low_base) { 3710ab97169SBaoquan He pr_err("cannot allocate crashkernel low memory (size:0x%llx).\n", low_size); 3720ab97169SBaoquan He return -ENOMEM; 3730ab97169SBaoquan He } 3740ab97169SBaoquan He 3750ab97169SBaoquan He pr_info("crashkernel low memory reserved: 0x%08llx - 0x%08llx (%lld MB)\n", 3760ab97169SBaoquan He low_base, low_base + low_size, low_size >> 20); 3770ab97169SBaoquan He 3780ab97169SBaoquan He crashk_low_res.start = low_base; 3790ab97169SBaoquan He crashk_low_res.end = low_base + low_size - 1; 3800ab97169SBaoquan He insert_resource(&iomem_resource, &crashk_low_res); 3810ab97169SBaoquan He #endif 3820ab97169SBaoquan He return 0; 3830ab97169SBaoquan He } 3840ab97169SBaoquan He 3850ab97169SBaoquan He void __init reserve_crashkernel_generic(char *cmdline, 3860ab97169SBaoquan He unsigned long long crash_size, 3870ab97169SBaoquan He unsigned long long crash_base, 3880ab97169SBaoquan He unsigned long long crash_low_size, 3890ab97169SBaoquan He bool high) 3900ab97169SBaoquan He { 3910ab97169SBaoquan He unsigned long long search_end = CRASH_ADDR_LOW_MAX, search_base = 0; 3920ab97169SBaoquan He bool fixed_base = false; 3930ab97169SBaoquan He 3940ab97169SBaoquan He /* User specifies base address explicitly. */ 3950ab97169SBaoquan He if (crash_base) { 3960ab97169SBaoquan He fixed_base = true; 3970ab97169SBaoquan He search_base = crash_base; 3980ab97169SBaoquan He search_end = crash_base + crash_size; 3990ab97169SBaoquan He } else if (high) { 4000ab97169SBaoquan He search_base = CRASH_ADDR_LOW_MAX; 4010ab97169SBaoquan He search_end = CRASH_ADDR_HIGH_MAX; 4020ab97169SBaoquan He } 4030ab97169SBaoquan He 4040ab97169SBaoquan He retry: 4050ab97169SBaoquan He crash_base = memblock_phys_alloc_range(crash_size, CRASH_ALIGN, 4060ab97169SBaoquan He search_base, search_end); 4070ab97169SBaoquan He if (!crash_base) { 4080ab97169SBaoquan He /* 4090ab97169SBaoquan He * For crashkernel=size[KMG]@offset[KMG], print out failure 4100ab97169SBaoquan He * message if can't reserve the specified region. 4110ab97169SBaoquan He */ 4120ab97169SBaoquan He if (fixed_base) { 4130ab97169SBaoquan He pr_warn("crashkernel reservation failed - memory is in use.\n"); 4140ab97169SBaoquan He return; 4150ab97169SBaoquan He } 4160ab97169SBaoquan He 4170ab97169SBaoquan He /* 4180ab97169SBaoquan He * For crashkernel=size[KMG], if the first attempt was for 4190ab97169SBaoquan He * low memory, fall back to high memory, the minimum required 4200ab97169SBaoquan He * low memory will be reserved later. 4210ab97169SBaoquan He */ 4220ab97169SBaoquan He if (!high && search_end == CRASH_ADDR_LOW_MAX) { 4230ab97169SBaoquan He search_end = CRASH_ADDR_HIGH_MAX; 4240ab97169SBaoquan He search_base = CRASH_ADDR_LOW_MAX; 4250ab97169SBaoquan He crash_low_size = DEFAULT_CRASH_KERNEL_LOW_SIZE; 4260ab97169SBaoquan He goto retry; 4270ab97169SBaoquan He } 4280ab97169SBaoquan He 4290ab97169SBaoquan He /* 4300ab97169SBaoquan He * For crashkernel=size[KMG],high, if the first attempt was 4310ab97169SBaoquan He * for high memory, fall back to low memory. 4320ab97169SBaoquan He */ 4330ab97169SBaoquan He if (high && search_end == CRASH_ADDR_HIGH_MAX) { 4340ab97169SBaoquan He search_end = CRASH_ADDR_LOW_MAX; 4350ab97169SBaoquan He search_base = 0; 4360ab97169SBaoquan He goto retry; 4370ab97169SBaoquan He } 4380ab97169SBaoquan He pr_warn("cannot allocate crashkernel (size:0x%llx)\n", 4390ab97169SBaoquan He crash_size); 4400ab97169SBaoquan He return; 4410ab97169SBaoquan He } 4420ab97169SBaoquan He 4431dd11e97SYuntao Wang if ((crash_base >= CRASH_ADDR_LOW_MAX) && 4440ab97169SBaoquan He crash_low_size && reserve_crashkernel_low(crash_low_size)) { 4450ab97169SBaoquan He memblock_phys_free(crash_base, crash_size); 4460ab97169SBaoquan He return; 4470ab97169SBaoquan He } 4480ab97169SBaoquan He 4490ab97169SBaoquan He pr_info("crashkernel reserved: 0x%016llx - 0x%016llx (%lld MB)\n", 4500ab97169SBaoquan He crash_base, crash_base + crash_size, crash_size >> 20); 4510ab97169SBaoquan He 4520ab97169SBaoquan He /* 4530ab97169SBaoquan He * The crashkernel memory will be removed from the kernel linear 4540ab97169SBaoquan He * map. Inform kmemleak so that it won't try to access it. 4550ab97169SBaoquan He */ 4560ab97169SBaoquan He kmemleak_ignore_phys(crash_base); 4570ab97169SBaoquan He if (crashk_low_res.end) 4580ab97169SBaoquan He kmemleak_ignore_phys(crashk_low_res.start); 4590ab97169SBaoquan He 4600ab97169SBaoquan He crashk_res.start = crash_base; 4610ab97169SBaoquan He crashk_res.end = crash_base + crash_size - 1; 4620ab97169SBaoquan He insert_resource(&iomem_resource, &crashk_res); 4630ab97169SBaoquan He } 4640ab97169SBaoquan He #endif 4650ab97169SBaoquan He 4666f991cc3SEric DeVolder int crash_prepare_elf64_headers(struct crash_mem *mem, int need_kernel_map, 4676f991cc3SEric DeVolder void **addr, unsigned long *sz) 4686f991cc3SEric DeVolder { 4696f991cc3SEric DeVolder Elf64_Ehdr *ehdr; 4706f991cc3SEric DeVolder Elf64_Phdr *phdr; 4716f991cc3SEric DeVolder unsigned long nr_cpus = num_possible_cpus(), nr_phdr, elf_sz; 4726f991cc3SEric DeVolder unsigned char *buf; 4736f991cc3SEric DeVolder unsigned int cpu, i; 4746f991cc3SEric DeVolder unsigned long long notes_addr; 4756f991cc3SEric DeVolder unsigned long mstart, mend; 4766f991cc3SEric DeVolder 4776f991cc3SEric DeVolder /* extra phdr for vmcoreinfo ELF note */ 4786f991cc3SEric DeVolder nr_phdr = nr_cpus + 1; 4796f991cc3SEric DeVolder nr_phdr += mem->nr_ranges; 4806f991cc3SEric DeVolder 4816f991cc3SEric DeVolder /* 4826f991cc3SEric DeVolder * kexec-tools creates an extra PT_LOAD phdr for kernel text mapping 4836f991cc3SEric DeVolder * area (for example, ffffffff80000000 - ffffffffa0000000 on x86_64). 4846f991cc3SEric DeVolder * I think this is required by tools like gdb. So same physical 4856f991cc3SEric DeVolder * memory will be mapped in two ELF headers. One will contain kernel 4866f991cc3SEric DeVolder * text virtual addresses and other will have __va(physical) addresses. 4876f991cc3SEric DeVolder */ 4886f991cc3SEric DeVolder 4896f991cc3SEric DeVolder nr_phdr++; 4906f991cc3SEric DeVolder elf_sz = sizeof(Elf64_Ehdr) + nr_phdr * sizeof(Elf64_Phdr); 4916f991cc3SEric DeVolder elf_sz = ALIGN(elf_sz, ELF_CORE_HEADER_ALIGN); 4926f991cc3SEric DeVolder 4936f991cc3SEric DeVolder buf = vzalloc(elf_sz); 4946f991cc3SEric DeVolder if (!buf) 4956f991cc3SEric DeVolder return -ENOMEM; 4966f991cc3SEric DeVolder 4976f991cc3SEric DeVolder ehdr = (Elf64_Ehdr *)buf; 4986f991cc3SEric DeVolder phdr = (Elf64_Phdr *)(ehdr + 1); 4996f991cc3SEric DeVolder memcpy(ehdr->e_ident, ELFMAG, SELFMAG); 5006f991cc3SEric DeVolder ehdr->e_ident[EI_CLASS] = ELFCLASS64; 5016f991cc3SEric DeVolder ehdr->e_ident[EI_DATA] = ELFDATA2LSB; 5026f991cc3SEric DeVolder ehdr->e_ident[EI_VERSION] = EV_CURRENT; 5036f991cc3SEric DeVolder ehdr->e_ident[EI_OSABI] = ELF_OSABI; 5046f991cc3SEric DeVolder memset(ehdr->e_ident + EI_PAD, 0, EI_NIDENT - EI_PAD); 5056f991cc3SEric DeVolder ehdr->e_type = ET_CORE; 5066f991cc3SEric DeVolder ehdr->e_machine = ELF_ARCH; 5076f991cc3SEric DeVolder ehdr->e_version = EV_CURRENT; 5086f991cc3SEric DeVolder ehdr->e_phoff = sizeof(Elf64_Ehdr); 5096f991cc3SEric DeVolder ehdr->e_ehsize = sizeof(Elf64_Ehdr); 5106f991cc3SEric DeVolder ehdr->e_phentsize = sizeof(Elf64_Phdr); 5116f991cc3SEric DeVolder 512a396d0f8SEric DeVolder /* Prepare one phdr of type PT_NOTE for each possible CPU */ 513a396d0f8SEric DeVolder for_each_possible_cpu(cpu) { 5146f991cc3SEric DeVolder phdr->p_type = PT_NOTE; 5156f991cc3SEric DeVolder notes_addr = per_cpu_ptr_to_phys(per_cpu_ptr(crash_notes, cpu)); 5166f991cc3SEric DeVolder phdr->p_offset = phdr->p_paddr = notes_addr; 5176f991cc3SEric DeVolder phdr->p_filesz = phdr->p_memsz = sizeof(note_buf_t); 5186f991cc3SEric DeVolder (ehdr->e_phnum)++; 5196f991cc3SEric DeVolder phdr++; 5206f991cc3SEric DeVolder } 5216f991cc3SEric DeVolder 5226f991cc3SEric DeVolder /* Prepare one PT_NOTE header for vmcoreinfo */ 5236f991cc3SEric DeVolder phdr->p_type = PT_NOTE; 5246f991cc3SEric DeVolder phdr->p_offset = phdr->p_paddr = paddr_vmcoreinfo_note(); 5256f991cc3SEric DeVolder phdr->p_filesz = phdr->p_memsz = VMCOREINFO_NOTE_SIZE; 5266f991cc3SEric DeVolder (ehdr->e_phnum)++; 5276f991cc3SEric DeVolder phdr++; 5286f991cc3SEric DeVolder 5296f991cc3SEric DeVolder /* Prepare PT_LOAD type program header for kernel text region */ 5306f991cc3SEric DeVolder if (need_kernel_map) { 5316f991cc3SEric DeVolder phdr->p_type = PT_LOAD; 5326f991cc3SEric DeVolder phdr->p_flags = PF_R|PF_W|PF_X; 5336f991cc3SEric DeVolder phdr->p_vaddr = (unsigned long) _text; 5346f991cc3SEric DeVolder phdr->p_filesz = phdr->p_memsz = _end - _text; 5356f991cc3SEric DeVolder phdr->p_offset = phdr->p_paddr = __pa_symbol(_text); 5366f991cc3SEric DeVolder ehdr->e_phnum++; 5376f991cc3SEric DeVolder phdr++; 5386f991cc3SEric DeVolder } 5396f991cc3SEric DeVolder 5406f991cc3SEric DeVolder /* Go through all the ranges in mem->ranges[] and prepare phdr */ 5416f991cc3SEric DeVolder for (i = 0; i < mem->nr_ranges; i++) { 5426f991cc3SEric DeVolder mstart = mem->ranges[i].start; 5436f991cc3SEric DeVolder mend = mem->ranges[i].end; 5446f991cc3SEric DeVolder 5456f991cc3SEric DeVolder phdr->p_type = PT_LOAD; 5466f991cc3SEric DeVolder phdr->p_flags = PF_R|PF_W|PF_X; 5476f991cc3SEric DeVolder phdr->p_offset = mstart; 5486f991cc3SEric DeVolder 5496f991cc3SEric DeVolder phdr->p_paddr = mstart; 5506f991cc3SEric DeVolder phdr->p_vaddr = (unsigned long) __va(mstart); 5516f991cc3SEric DeVolder phdr->p_filesz = phdr->p_memsz = mend - mstart + 1; 5526f991cc3SEric DeVolder phdr->p_align = 0; 5536f991cc3SEric DeVolder ehdr->e_phnum++; 5546f991cc3SEric 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", 5556f991cc3SEric DeVolder phdr, phdr->p_vaddr, phdr->p_paddr, phdr->p_filesz, 5566f991cc3SEric DeVolder ehdr->e_phnum, phdr->p_offset); 5576f991cc3SEric DeVolder phdr++; 5586f991cc3SEric DeVolder } 5596f991cc3SEric DeVolder 5606f991cc3SEric DeVolder *addr = buf; 5616f991cc3SEric DeVolder *sz = elf_sz; 5626f991cc3SEric DeVolder return 0; 5636f991cc3SEric DeVolder } 5646f991cc3SEric DeVolder 5656f991cc3SEric DeVolder int crash_exclude_mem_range(struct crash_mem *mem, 5666f991cc3SEric DeVolder unsigned long long mstart, unsigned long long mend) 5676f991cc3SEric DeVolder { 5686f991cc3SEric DeVolder int i, j; 5696f991cc3SEric DeVolder unsigned long long start, end, p_start, p_end; 5706f991cc3SEric DeVolder struct range temp_range = {0, 0}; 5716f991cc3SEric DeVolder 5726f991cc3SEric DeVolder for (i = 0; i < mem->nr_ranges; i++) { 5736f991cc3SEric DeVolder start = mem->ranges[i].start; 5746f991cc3SEric DeVolder end = mem->ranges[i].end; 5756f991cc3SEric DeVolder p_start = mstart; 5766f991cc3SEric DeVolder p_end = mend; 5776f991cc3SEric DeVolder 5786f991cc3SEric DeVolder if (mstart > end || mend < start) 5796f991cc3SEric DeVolder continue; 5806f991cc3SEric DeVolder 5816f991cc3SEric DeVolder /* Truncate any area outside of range */ 5826f991cc3SEric DeVolder if (mstart < start) 5836f991cc3SEric DeVolder p_start = start; 5846f991cc3SEric DeVolder if (mend > end) 5856f991cc3SEric DeVolder p_end = end; 5866f991cc3SEric DeVolder 5876f991cc3SEric DeVolder /* Found completely overlapping range */ 5886f991cc3SEric DeVolder if (p_start == start && p_end == end) { 5896f991cc3SEric DeVolder mem->ranges[i].start = 0; 5906f991cc3SEric DeVolder mem->ranges[i].end = 0; 5916f991cc3SEric DeVolder if (i < mem->nr_ranges - 1) { 5926f991cc3SEric DeVolder /* Shift rest of the ranges to left */ 5936f991cc3SEric DeVolder for (j = i; j < mem->nr_ranges - 1; j++) { 5946f991cc3SEric DeVolder mem->ranges[j].start = 5956f991cc3SEric DeVolder mem->ranges[j+1].start; 5966f991cc3SEric DeVolder mem->ranges[j].end = 5976f991cc3SEric DeVolder mem->ranges[j+1].end; 5986f991cc3SEric DeVolder } 5996f991cc3SEric DeVolder 6006f991cc3SEric DeVolder /* 6016f991cc3SEric DeVolder * Continue to check if there are another overlapping ranges 6026f991cc3SEric DeVolder * from the current position because of shifting the above 6036f991cc3SEric DeVolder * mem ranges. 6046f991cc3SEric DeVolder */ 6056f991cc3SEric DeVolder i--; 6066f991cc3SEric DeVolder mem->nr_ranges--; 6076f991cc3SEric DeVolder continue; 6086f991cc3SEric DeVolder } 6096f991cc3SEric DeVolder mem->nr_ranges--; 6106f991cc3SEric DeVolder return 0; 6116f991cc3SEric DeVolder } 6126f991cc3SEric DeVolder 6136f991cc3SEric DeVolder if (p_start > start && p_end < end) { 6146f991cc3SEric DeVolder /* Split original range */ 6156f991cc3SEric DeVolder mem->ranges[i].end = p_start - 1; 6166f991cc3SEric DeVolder temp_range.start = p_end + 1; 6176f991cc3SEric DeVolder temp_range.end = end; 6186f991cc3SEric DeVolder } else if (p_start != start) 6196f991cc3SEric DeVolder mem->ranges[i].end = p_start - 1; 6206f991cc3SEric DeVolder else 6216f991cc3SEric DeVolder mem->ranges[i].start = p_end + 1; 6226f991cc3SEric DeVolder break; 6236f991cc3SEric DeVolder } 6246f991cc3SEric DeVolder 6256f991cc3SEric DeVolder /* If a split happened, add the split to array */ 6266f991cc3SEric DeVolder if (!temp_range.end) 6276f991cc3SEric DeVolder return 0; 6286f991cc3SEric DeVolder 6296f991cc3SEric DeVolder /* Split happened */ 6306f991cc3SEric DeVolder if (i == mem->max_nr_ranges - 1) 6316f991cc3SEric DeVolder return -ENOMEM; 6326f991cc3SEric DeVolder 6336f991cc3SEric DeVolder /* Location where new range should go */ 6346f991cc3SEric DeVolder j = i + 1; 6356f991cc3SEric DeVolder if (j < mem->nr_ranges) { 6366f991cc3SEric DeVolder /* Move over all ranges one slot towards the end */ 6376f991cc3SEric DeVolder for (i = mem->nr_ranges - 1; i >= j; i--) 6386f991cc3SEric DeVolder mem->ranges[i + 1] = mem->ranges[i]; 6396f991cc3SEric DeVolder } 6406f991cc3SEric DeVolder 6416f991cc3SEric DeVolder mem->ranges[j].start = temp_range.start; 6426f991cc3SEric DeVolder mem->ranges[j].end = temp_range.end; 6436f991cc3SEric DeVolder mem->nr_ranges++; 6446f991cc3SEric DeVolder return 0; 6456f991cc3SEric DeVolder } 6466f991cc3SEric DeVolder 64751dbd925SHari Bathini Elf_Word *append_elf_note(Elf_Word *buf, char *name, unsigned int type, 648692f66f2SHari Bathini void *data, size_t data_len) 649692f66f2SHari Bathini { 65051dbd925SHari Bathini struct elf_note *note = (struct elf_note *)buf; 651692f66f2SHari Bathini 65251dbd925SHari Bathini note->n_namesz = strlen(name) + 1; 65351dbd925SHari Bathini note->n_descsz = data_len; 65451dbd925SHari Bathini note->n_type = type; 65551dbd925SHari Bathini buf += DIV_ROUND_UP(sizeof(*note), sizeof(Elf_Word)); 65651dbd925SHari Bathini memcpy(buf, name, note->n_namesz); 65751dbd925SHari Bathini buf += DIV_ROUND_UP(note->n_namesz, sizeof(Elf_Word)); 65851dbd925SHari Bathini memcpy(buf, data, data_len); 65951dbd925SHari Bathini buf += DIV_ROUND_UP(data_len, sizeof(Elf_Word)); 660692f66f2SHari Bathini 661692f66f2SHari Bathini return buf; 662692f66f2SHari Bathini } 663692f66f2SHari Bathini 66451dbd925SHari Bathini void final_note(Elf_Word *buf) 665692f66f2SHari Bathini { 66651dbd925SHari Bathini memset(buf, 0, sizeof(struct elf_note)); 667692f66f2SHari Bathini } 668692f66f2SHari Bathini 669692f66f2SHari Bathini static void update_vmcoreinfo_note(void) 670692f66f2SHari Bathini { 671692f66f2SHari Bathini u32 *buf = vmcoreinfo_note; 672692f66f2SHari Bathini 673692f66f2SHari Bathini if (!vmcoreinfo_size) 674692f66f2SHari Bathini return; 675692f66f2SHari Bathini buf = append_elf_note(buf, VMCOREINFO_NOTE_NAME, 0, vmcoreinfo_data, 676692f66f2SHari Bathini vmcoreinfo_size); 677692f66f2SHari Bathini final_note(buf); 678692f66f2SHari Bathini } 679692f66f2SHari Bathini 6801229384fSXunlei Pang void crash_update_vmcoreinfo_safecopy(void *ptr) 6811229384fSXunlei Pang { 6821229384fSXunlei Pang if (ptr) 6831229384fSXunlei Pang memcpy(ptr, vmcoreinfo_data, vmcoreinfo_size); 6841229384fSXunlei Pang 6851229384fSXunlei Pang vmcoreinfo_data_safecopy = ptr; 6861229384fSXunlei Pang } 6871229384fSXunlei Pang 688692f66f2SHari Bathini void crash_save_vmcoreinfo(void) 689692f66f2SHari Bathini { 690203e9e41SXunlei Pang if (!vmcoreinfo_note) 691203e9e41SXunlei Pang return; 692203e9e41SXunlei Pang 6931229384fSXunlei Pang /* Use the safe copy to generate vmcoreinfo note if have */ 6941229384fSXunlei Pang if (vmcoreinfo_data_safecopy) 6951229384fSXunlei Pang vmcoreinfo_data = vmcoreinfo_data_safecopy; 6961229384fSXunlei Pang 69791bc9aafSArnd Bergmann vmcoreinfo_append_str("CRASHTIME=%lld\n", ktime_get_real_seconds()); 698692f66f2SHari Bathini update_vmcoreinfo_note(); 699692f66f2SHari Bathini } 700692f66f2SHari Bathini 701692f66f2SHari Bathini void vmcoreinfo_append_str(const char *fmt, ...) 702692f66f2SHari Bathini { 703692f66f2SHari Bathini va_list args; 704692f66f2SHari Bathini char buf[0x50]; 705692f66f2SHari Bathini size_t r; 706692f66f2SHari Bathini 707692f66f2SHari Bathini va_start(args, fmt); 708692f66f2SHari Bathini r = vscnprintf(buf, sizeof(buf), fmt, args); 709692f66f2SHari Bathini va_end(args); 710692f66f2SHari Bathini 7115203f499SXunlei Pang r = min(r, (size_t)VMCOREINFO_BYTES - vmcoreinfo_size); 712692f66f2SHari Bathini 713692f66f2SHari Bathini memcpy(&vmcoreinfo_data[vmcoreinfo_size], buf, r); 714692f66f2SHari Bathini 715692f66f2SHari Bathini vmcoreinfo_size += r; 71608fc35f3SStephen Brennan 71708fc35f3SStephen Brennan WARN_ONCE(vmcoreinfo_size == VMCOREINFO_BYTES, 71808fc35f3SStephen Brennan "vmcoreinfo data exceeds allocated size, truncating"); 719692f66f2SHari Bathini } 720692f66f2SHari Bathini 721692f66f2SHari Bathini /* 722692f66f2SHari Bathini * provide an empty default implementation here -- architecture 723692f66f2SHari Bathini * code may override this 724692f66f2SHari Bathini */ 725692f66f2SHari Bathini void __weak arch_crash_save_vmcoreinfo(void) 726692f66f2SHari Bathini {} 727692f66f2SHari Bathini 728692f66f2SHari Bathini phys_addr_t __weak paddr_vmcoreinfo_note(void) 729692f66f2SHari Bathini { 730203e9e41SXunlei Pang return __pa(vmcoreinfo_note); 731692f66f2SHari Bathini } 73243d4cb47SMarc-André Lureau EXPORT_SYMBOL(paddr_vmcoreinfo_note); 733692f66f2SHari Bathini 734692f66f2SHari Bathini static int __init crash_save_vmcoreinfo_init(void) 735692f66f2SHari Bathini { 736203e9e41SXunlei Pang vmcoreinfo_data = (unsigned char *)get_zeroed_page(GFP_KERNEL); 737203e9e41SXunlei Pang if (!vmcoreinfo_data) { 738203e9e41SXunlei Pang pr_warn("Memory allocation for vmcoreinfo_data failed\n"); 739203e9e41SXunlei Pang return -ENOMEM; 740203e9e41SXunlei Pang } 741203e9e41SXunlei Pang 742203e9e41SXunlei Pang vmcoreinfo_note = alloc_pages_exact(VMCOREINFO_NOTE_SIZE, 743203e9e41SXunlei Pang GFP_KERNEL | __GFP_ZERO); 744203e9e41SXunlei Pang if (!vmcoreinfo_note) { 745203e9e41SXunlei Pang free_page((unsigned long)vmcoreinfo_data); 746203e9e41SXunlei Pang vmcoreinfo_data = NULL; 747203e9e41SXunlei Pang pr_warn("Memory allocation for vmcoreinfo_note failed\n"); 748203e9e41SXunlei Pang return -ENOMEM; 749203e9e41SXunlei Pang } 750203e9e41SXunlei Pang 751692f66f2SHari Bathini VMCOREINFO_OSRELEASE(init_uts_ns.name.release); 75244e8a5e9SStephen Boyd VMCOREINFO_BUILD_ID(); 753692f66f2SHari Bathini VMCOREINFO_PAGESIZE(PAGE_SIZE); 754692f66f2SHari Bathini 755692f66f2SHari Bathini VMCOREINFO_SYMBOL(init_uts_ns); 756ca4a9241SAlexander Egorenkov VMCOREINFO_OFFSET(uts_namespace, name); 757692f66f2SHari Bathini VMCOREINFO_SYMBOL(node_online_map); 758692f66f2SHari Bathini #ifdef CONFIG_MMU 759eff4345eSOmar Sandoval VMCOREINFO_SYMBOL_ARRAY(swapper_pg_dir); 760692f66f2SHari Bathini #endif 761692f66f2SHari Bathini VMCOREINFO_SYMBOL(_stext); 762692f66f2SHari Bathini VMCOREINFO_SYMBOL(vmap_area_list); 763692f66f2SHari Bathini 764a9ee6cf5SMike Rapoport #ifndef CONFIG_NUMA 765692f66f2SHari Bathini VMCOREINFO_SYMBOL(mem_map); 766692f66f2SHari Bathini VMCOREINFO_SYMBOL(contig_page_data); 767692f66f2SHari Bathini #endif 768692f66f2SHari Bathini #ifdef CONFIG_SPARSEMEM 769a0b12803SKirill A. Shutemov VMCOREINFO_SYMBOL_ARRAY(mem_section); 770692f66f2SHari Bathini VMCOREINFO_LENGTH(mem_section, NR_SECTION_ROOTS); 771692f66f2SHari Bathini VMCOREINFO_STRUCT_SIZE(mem_section); 772692f66f2SHari Bathini VMCOREINFO_OFFSET(mem_section, section_mem_map); 7734f5aecdfSPingfan Liu VMCOREINFO_NUMBER(SECTION_SIZE_BITS); 7741d50e5d0SBhupesh Sharma VMCOREINFO_NUMBER(MAX_PHYSMEM_BITS); 775692f66f2SHari Bathini #endif 776692f66f2SHari Bathini VMCOREINFO_STRUCT_SIZE(page); 777692f66f2SHari Bathini VMCOREINFO_STRUCT_SIZE(pglist_data); 778692f66f2SHari Bathini VMCOREINFO_STRUCT_SIZE(zone); 779692f66f2SHari Bathini VMCOREINFO_STRUCT_SIZE(free_area); 780692f66f2SHari Bathini VMCOREINFO_STRUCT_SIZE(list_head); 781692f66f2SHari Bathini VMCOREINFO_SIZE(nodemask_t); 782692f66f2SHari Bathini VMCOREINFO_OFFSET(page, flags); 783692f66f2SHari Bathini VMCOREINFO_OFFSET(page, _refcount); 784692f66f2SHari Bathini VMCOREINFO_OFFSET(page, mapping); 785692f66f2SHari Bathini VMCOREINFO_OFFSET(page, lru); 786692f66f2SHari Bathini VMCOREINFO_OFFSET(page, _mapcount); 787692f66f2SHari Bathini VMCOREINFO_OFFSET(page, private); 788692f66f2SHari Bathini VMCOREINFO_OFFSET(page, compound_head); 789692f66f2SHari Bathini VMCOREINFO_OFFSET(pglist_data, node_zones); 790692f66f2SHari Bathini VMCOREINFO_OFFSET(pglist_data, nr_zones); 79143b02ba9SMike Rapoport #ifdef CONFIG_FLATMEM 792692f66f2SHari Bathini VMCOREINFO_OFFSET(pglist_data, node_mem_map); 793692f66f2SHari Bathini #endif 794692f66f2SHari Bathini VMCOREINFO_OFFSET(pglist_data, node_start_pfn); 795692f66f2SHari Bathini VMCOREINFO_OFFSET(pglist_data, node_spanned_pages); 796692f66f2SHari Bathini VMCOREINFO_OFFSET(pglist_data, node_id); 797692f66f2SHari Bathini VMCOREINFO_OFFSET(zone, free_area); 798692f66f2SHari Bathini VMCOREINFO_OFFSET(zone, vm_stat); 799692f66f2SHari Bathini VMCOREINFO_OFFSET(zone, spanned_pages); 800692f66f2SHari Bathini VMCOREINFO_OFFSET(free_area, free_list); 801692f66f2SHari Bathini VMCOREINFO_OFFSET(list_head, next); 802692f66f2SHari Bathini VMCOREINFO_OFFSET(list_head, prev); 803692f66f2SHari Bathini VMCOREINFO_OFFSET(vmap_area, va_start); 804692f66f2SHari Bathini VMCOREINFO_OFFSET(vmap_area, list); 805*fd377218SKirill A. Shutemov VMCOREINFO_LENGTH(zone.free_area, NR_PAGE_ORDERS); 806692f66f2SHari Bathini log_buf_vmcoreinfo_setup(); 807692f66f2SHari Bathini VMCOREINFO_LENGTH(free_area.free_list, MIGRATE_TYPES); 808692f66f2SHari Bathini VMCOREINFO_NUMBER(NR_FREE_PAGES); 809692f66f2SHari Bathini VMCOREINFO_NUMBER(PG_lru); 810692f66f2SHari Bathini VMCOREINFO_NUMBER(PG_private); 811692f66f2SHari Bathini VMCOREINFO_NUMBER(PG_swapcache); 8121cbf29daSPetr Tesarik VMCOREINFO_NUMBER(PG_swapbacked); 813692f66f2SHari Bathini VMCOREINFO_NUMBER(PG_slab); 814692f66f2SHari Bathini #ifdef CONFIG_MEMORY_FAILURE 815692f66f2SHari Bathini VMCOREINFO_NUMBER(PG_hwpoison); 816692f66f2SHari Bathini #endif 817692f66f2SHari Bathini VMCOREINFO_NUMBER(PG_head_mask); 8186e292b9bSMatthew Wilcox #define PAGE_BUDDY_MAPCOUNT_VALUE (~PG_buddy) 819692f66f2SHari Bathini VMCOREINFO_NUMBER(PAGE_BUDDY_MAPCOUNT_VALUE); 820692f66f2SHari Bathini #ifdef CONFIG_HUGETLB_PAGE 8219c5ccf2dSMatthew Wilcox (Oracle) VMCOREINFO_NUMBER(PG_hugetlb); 822e04b742fSDavid Hildenbrand #define PAGE_OFFLINE_MAPCOUNT_VALUE (~PG_offline) 823e04b742fSDavid Hildenbrand VMCOREINFO_NUMBER(PAGE_OFFLINE_MAPCOUNT_VALUE); 824692f66f2SHari Bathini #endif 825692f66f2SHari Bathini 8265fd8fea9SStephen Brennan #ifdef CONFIG_KALLSYMS 8275fd8fea9SStephen Brennan VMCOREINFO_SYMBOL(kallsyms_names); 828f09bddbdSStephen Brennan VMCOREINFO_SYMBOL(kallsyms_num_syms); 8295fd8fea9SStephen Brennan VMCOREINFO_SYMBOL(kallsyms_token_table); 8305fd8fea9SStephen Brennan VMCOREINFO_SYMBOL(kallsyms_token_index); 8315fd8fea9SStephen Brennan #ifdef CONFIG_KALLSYMS_BASE_RELATIVE 8325fd8fea9SStephen Brennan VMCOREINFO_SYMBOL(kallsyms_offsets); 8335fd8fea9SStephen Brennan VMCOREINFO_SYMBOL(kallsyms_relative_base); 8345fd8fea9SStephen Brennan #else 8355fd8fea9SStephen Brennan VMCOREINFO_SYMBOL(kallsyms_addresses); 8365fd8fea9SStephen Brennan #endif /* CONFIG_KALLSYMS_BASE_RELATIVE */ 8375fd8fea9SStephen Brennan #endif /* CONFIG_KALLSYMS */ 8385fd8fea9SStephen Brennan 839692f66f2SHari Bathini arch_crash_save_vmcoreinfo(); 840692f66f2SHari Bathini update_vmcoreinfo_note(); 841692f66f2SHari Bathini 842692f66f2SHari Bathini return 0; 843692f66f2SHari Bathini } 844692f66f2SHari Bathini 845692f66f2SHari Bathini subsys_initcall(crash_save_vmcoreinfo_init); 8466f991cc3SEric DeVolder 8476f991cc3SEric DeVolder static int __init crash_notes_memory_init(void) 8486f991cc3SEric DeVolder { 8496f991cc3SEric DeVolder /* Allocate memory for saving cpu registers. */ 8506f991cc3SEric DeVolder size_t size, align; 8516f991cc3SEric DeVolder 8526f991cc3SEric DeVolder /* 8536f991cc3SEric DeVolder * crash_notes could be allocated across 2 vmalloc pages when percpu 8546f991cc3SEric DeVolder * is vmalloc based . vmalloc doesn't guarantee 2 continuous vmalloc 8556f991cc3SEric DeVolder * pages are also on 2 continuous physical pages. In this case the 8566f991cc3SEric DeVolder * 2nd part of crash_notes in 2nd page could be lost since only the 8576f991cc3SEric DeVolder * starting address and size of crash_notes are exported through sysfs. 8586f991cc3SEric DeVolder * Here round up the size of crash_notes to the nearest power of two 8596f991cc3SEric DeVolder * and pass it to __alloc_percpu as align value. This can make sure 8606f991cc3SEric DeVolder * crash_notes is allocated inside one physical page. 8616f991cc3SEric DeVolder */ 8626f991cc3SEric DeVolder size = sizeof(note_buf_t); 8636f991cc3SEric DeVolder align = min(roundup_pow_of_two(sizeof(note_buf_t)), PAGE_SIZE); 8646f991cc3SEric DeVolder 8656f991cc3SEric DeVolder /* 8666f991cc3SEric DeVolder * Break compile if size is bigger than PAGE_SIZE since crash_notes 8676f991cc3SEric DeVolder * definitely will be in 2 pages with that. 8686f991cc3SEric DeVolder */ 8696f991cc3SEric DeVolder BUILD_BUG_ON(size > PAGE_SIZE); 8706f991cc3SEric DeVolder 8716f991cc3SEric DeVolder crash_notes = __alloc_percpu(size, align); 8726f991cc3SEric DeVolder if (!crash_notes) { 8736f991cc3SEric DeVolder pr_warn("Memory allocation for saving cpu register states failed\n"); 8746f991cc3SEric DeVolder return -ENOMEM; 8756f991cc3SEric DeVolder } 8766f991cc3SEric DeVolder return 0; 8776f991cc3SEric DeVolder } 8786f991cc3SEric DeVolder subsys_initcall(crash_notes_memory_init); 87924726275SEric DeVolder 88024726275SEric DeVolder #ifdef CONFIG_CRASH_HOTPLUG 88124726275SEric DeVolder #undef pr_fmt 88224726275SEric DeVolder #define pr_fmt(fmt) "crash hp: " fmt 883a72bbec7SEric DeVolder 884a72bbec7SEric DeVolder /* 885e2a8f20dSBaoquan He * Different than kexec/kdump loading/unloading/jumping/shrinking which 886e2a8f20dSBaoquan He * usually rarely happen, there will be many crash hotplug events notified 887e2a8f20dSBaoquan He * during one short period, e.g one memory board is hot added and memory 888e2a8f20dSBaoquan He * regions are online. So mutex lock __crash_hotplug_lock is used to 889e2a8f20dSBaoquan He * serialize the crash hotplug handling specifically. 890e2a8f20dSBaoquan He */ 891e2a8f20dSBaoquan He DEFINE_MUTEX(__crash_hotplug_lock); 892e2a8f20dSBaoquan He #define crash_hotplug_lock() mutex_lock(&__crash_hotplug_lock) 893e2a8f20dSBaoquan He #define crash_hotplug_unlock() mutex_unlock(&__crash_hotplug_lock) 894e2a8f20dSBaoquan He 895e2a8f20dSBaoquan He /* 896a72bbec7SEric DeVolder * This routine utilized when the crash_hotplug sysfs node is read. 897a72bbec7SEric DeVolder * It reflects the kernel's ability/permission to update the crash 898a72bbec7SEric DeVolder * elfcorehdr directly. 899a72bbec7SEric DeVolder */ 900a72bbec7SEric DeVolder int crash_check_update_elfcorehdr(void) 901a72bbec7SEric DeVolder { 902a72bbec7SEric DeVolder int rc = 0; 903a72bbec7SEric DeVolder 904e2a8f20dSBaoquan He crash_hotplug_lock(); 905a72bbec7SEric DeVolder /* Obtain lock while reading crash information */ 906a72bbec7SEric DeVolder if (!kexec_trylock()) { 907a72bbec7SEric DeVolder pr_info("kexec_trylock() failed, elfcorehdr may be inaccurate\n"); 908e2a8f20dSBaoquan He crash_hotplug_unlock(); 909a72bbec7SEric DeVolder return 0; 910a72bbec7SEric DeVolder } 911a72bbec7SEric DeVolder if (kexec_crash_image) { 912a72bbec7SEric DeVolder if (kexec_crash_image->file_mode) 913a72bbec7SEric DeVolder rc = 1; 914a72bbec7SEric DeVolder else 915a72bbec7SEric DeVolder rc = kexec_crash_image->update_elfcorehdr; 916a72bbec7SEric DeVolder } 917a72bbec7SEric DeVolder /* Release lock now that update complete */ 918a72bbec7SEric DeVolder kexec_unlock(); 919e2a8f20dSBaoquan He crash_hotplug_unlock(); 920a72bbec7SEric DeVolder 921a72bbec7SEric DeVolder return rc; 922a72bbec7SEric DeVolder } 923a72bbec7SEric DeVolder 92424726275SEric DeVolder /* 92524726275SEric DeVolder * To accurately reflect hot un/plug changes of cpu and memory resources 92624726275SEric DeVolder * (including onling and offlining of those resources), the elfcorehdr 92724726275SEric DeVolder * (which is passed to the crash kernel via the elfcorehdr= parameter) 92824726275SEric DeVolder * must be updated with the new list of CPUs and memories. 92924726275SEric DeVolder * 93024726275SEric DeVolder * In order to make changes to elfcorehdr, two conditions are needed: 93124726275SEric DeVolder * First, the segment containing the elfcorehdr must be large enough 93224726275SEric DeVolder * to permit a growing number of resources; the elfcorehdr memory size 93324726275SEric DeVolder * is based on NR_CPUS_DEFAULT and CRASH_MAX_MEMORY_RANGES. 93424726275SEric DeVolder * Second, purgatory must explicitly exclude the elfcorehdr from the 93524726275SEric DeVolder * list of segments it checks (since the elfcorehdr changes and thus 93624726275SEric DeVolder * would require an update to purgatory itself to update the digest). 93724726275SEric DeVolder */ 93824726275SEric DeVolder static void crash_handle_hotplug_event(unsigned int hp_action, unsigned int cpu) 93924726275SEric DeVolder { 94024726275SEric DeVolder struct kimage *image; 94124726275SEric DeVolder 942e2a8f20dSBaoquan He crash_hotplug_lock(); 94324726275SEric DeVolder /* Obtain lock while changing crash information */ 94424726275SEric DeVolder if (!kexec_trylock()) { 94524726275SEric DeVolder pr_info("kexec_trylock() failed, elfcorehdr may be inaccurate\n"); 946e2a8f20dSBaoquan He crash_hotplug_unlock(); 94724726275SEric DeVolder return; 94824726275SEric DeVolder } 94924726275SEric DeVolder 95024726275SEric DeVolder /* Check kdump is not loaded */ 95124726275SEric DeVolder if (!kexec_crash_image) 95224726275SEric DeVolder goto out; 95324726275SEric DeVolder 95424726275SEric DeVolder image = kexec_crash_image; 95524726275SEric DeVolder 956a72bbec7SEric DeVolder /* Check that updating elfcorehdr is permitted */ 957a72bbec7SEric DeVolder if (!(image->file_mode || image->update_elfcorehdr)) 958a72bbec7SEric DeVolder goto out; 959a72bbec7SEric DeVolder 96024726275SEric DeVolder if (hp_action == KEXEC_CRASH_HP_ADD_CPU || 96124726275SEric DeVolder hp_action == KEXEC_CRASH_HP_REMOVE_CPU) 96224726275SEric DeVolder pr_debug("hp_action %u, cpu %u\n", hp_action, cpu); 96324726275SEric DeVolder else 96424726275SEric DeVolder pr_debug("hp_action %u\n", hp_action); 96524726275SEric DeVolder 96624726275SEric DeVolder /* 96724726275SEric DeVolder * The elfcorehdr_index is set to -1 when the struct kimage 96824726275SEric DeVolder * is allocated. Find the segment containing the elfcorehdr, 96924726275SEric DeVolder * if not already found. 97024726275SEric DeVolder */ 97124726275SEric DeVolder if (image->elfcorehdr_index < 0) { 97224726275SEric DeVolder unsigned long mem; 97324726275SEric DeVolder unsigned char *ptr; 97424726275SEric DeVolder unsigned int n; 97524726275SEric DeVolder 97624726275SEric DeVolder for (n = 0; n < image->nr_segments; n++) { 97724726275SEric DeVolder mem = image->segment[n].mem; 97824726275SEric DeVolder ptr = kmap_local_page(pfn_to_page(mem >> PAGE_SHIFT)); 97924726275SEric DeVolder if (ptr) { 98024726275SEric DeVolder /* The segment containing elfcorehdr */ 98124726275SEric DeVolder if (memcmp(ptr, ELFMAG, SELFMAG) == 0) 98224726275SEric DeVolder image->elfcorehdr_index = (int)n; 98324726275SEric DeVolder kunmap_local(ptr); 98424726275SEric DeVolder } 98524726275SEric DeVolder } 98624726275SEric DeVolder } 98724726275SEric DeVolder 98824726275SEric DeVolder if (image->elfcorehdr_index < 0) { 98924726275SEric DeVolder pr_err("unable to locate elfcorehdr segment"); 99024726275SEric DeVolder goto out; 99124726275SEric DeVolder } 99224726275SEric DeVolder 99324726275SEric DeVolder /* Needed in order for the segments to be updated */ 99424726275SEric DeVolder arch_kexec_unprotect_crashkres(); 99524726275SEric DeVolder 99624726275SEric DeVolder /* Differentiate between normal load and hotplug update */ 99724726275SEric DeVolder image->hp_action = hp_action; 99824726275SEric DeVolder 99924726275SEric DeVolder /* Now invoke arch-specific update handler */ 100024726275SEric DeVolder arch_crash_handle_hotplug_event(image); 100124726275SEric DeVolder 100224726275SEric DeVolder /* No longer handling a hotplug event */ 100324726275SEric DeVolder image->hp_action = KEXEC_CRASH_HP_NONE; 100424726275SEric DeVolder image->elfcorehdr_updated = true; 100524726275SEric DeVolder 100624726275SEric DeVolder /* Change back to read-only */ 100724726275SEric DeVolder arch_kexec_protect_crashkres(); 100824726275SEric DeVolder 100924726275SEric DeVolder /* Errors in the callback is not a reason to rollback state */ 101024726275SEric DeVolder out: 101124726275SEric DeVolder /* Release lock now that update complete */ 101224726275SEric DeVolder kexec_unlock(); 1013e2a8f20dSBaoquan He crash_hotplug_unlock(); 101424726275SEric DeVolder } 101524726275SEric DeVolder 101624726275SEric DeVolder static int crash_memhp_notifier(struct notifier_block *nb, unsigned long val, void *v) 101724726275SEric DeVolder { 101824726275SEric DeVolder switch (val) { 101924726275SEric DeVolder case MEM_ONLINE: 102024726275SEric DeVolder crash_handle_hotplug_event(KEXEC_CRASH_HP_ADD_MEMORY, 102124726275SEric DeVolder KEXEC_CRASH_HP_INVALID_CPU); 102224726275SEric DeVolder break; 102324726275SEric DeVolder 102424726275SEric DeVolder case MEM_OFFLINE: 102524726275SEric DeVolder crash_handle_hotplug_event(KEXEC_CRASH_HP_REMOVE_MEMORY, 102624726275SEric DeVolder KEXEC_CRASH_HP_INVALID_CPU); 102724726275SEric DeVolder break; 102824726275SEric DeVolder } 102924726275SEric DeVolder return NOTIFY_OK; 103024726275SEric DeVolder } 103124726275SEric DeVolder 103224726275SEric DeVolder static struct notifier_block crash_memhp_nb = { 103324726275SEric DeVolder .notifier_call = crash_memhp_notifier, 103424726275SEric DeVolder .priority = 0 103524726275SEric DeVolder }; 103624726275SEric DeVolder 103724726275SEric DeVolder static int crash_cpuhp_online(unsigned int cpu) 103824726275SEric DeVolder { 103924726275SEric DeVolder crash_handle_hotplug_event(KEXEC_CRASH_HP_ADD_CPU, cpu); 104024726275SEric DeVolder return 0; 104124726275SEric DeVolder } 104224726275SEric DeVolder 104324726275SEric DeVolder static int crash_cpuhp_offline(unsigned int cpu) 104424726275SEric DeVolder { 104524726275SEric DeVolder crash_handle_hotplug_event(KEXEC_CRASH_HP_REMOVE_CPU, cpu); 104624726275SEric DeVolder return 0; 104724726275SEric DeVolder } 104824726275SEric DeVolder 104924726275SEric DeVolder static int __init crash_hotplug_init(void) 105024726275SEric DeVolder { 105124726275SEric DeVolder int result = 0; 105224726275SEric DeVolder 105324726275SEric DeVolder if (IS_ENABLED(CONFIG_MEMORY_HOTPLUG)) 105424726275SEric DeVolder register_memory_notifier(&crash_memhp_nb); 105524726275SEric DeVolder 105624726275SEric DeVolder if (IS_ENABLED(CONFIG_HOTPLUG_CPU)) { 105724726275SEric DeVolder result = cpuhp_setup_state_nocalls(CPUHP_BP_PREPARE_DYN, 105824726275SEric DeVolder "crash/cpuhp", crash_cpuhp_online, crash_cpuhp_offline); 105924726275SEric DeVolder } 106024726275SEric DeVolder 106124726275SEric DeVolder return result; 106224726275SEric DeVolder } 106324726275SEric DeVolder 106424726275SEric DeVolder subsys_initcall(crash_hotplug_init); 106524726275SEric DeVolder #endif 1066