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 <ebiederm@xmission.com> 5692f66f2SHari Bathini */ 6692f66f2SHari Bathini 744e8a5e9SStephen Boyd #include <linux/buildid.h> 8692f66f2SHari Bathini #include <linux/crash_core.h> 971d2bcecSPhilipp Rudo #include <linux/init.h> 10692f66f2SHari Bathini #include <linux/utsname.h> 11692f66f2SHari Bathini #include <linux/vmalloc.h> 1246d36b1bSTao Liu #include <linux/sizes.h> 136f991cc3SEric DeVolder #include <linux/kexec.h> 1424726275SEric DeVolder #include <linux/memory.h> 1524726275SEric DeVolder #include <linux/cpuhotplug.h> 16692f66f2SHari Bathini 17692f66f2SHari Bathini #include <asm/page.h> 18692f66f2SHari Bathini #include <asm/sections.h> 19692f66f2SHari Bathini 20a24d22b2SEric Biggers #include <crypto/sha1.h> 210935288cSVijay Balakrishna 225fd8fea9SStephen Brennan #include "kallsyms_internal.h" 2324726275SEric DeVolder #include "kexec_internal.h" 245fd8fea9SStephen Brennan 256f991cc3SEric DeVolder /* Per cpu memory for storing cpu states in case of system crash. */ 266f991cc3SEric DeVolder note_buf_t __percpu *crash_notes; 276f991cc3SEric DeVolder 28692f66f2SHari Bathini /* vmcoreinfo stuff */ 2923c85094SOmar Sandoval unsigned char *vmcoreinfo_data; 3023c85094SOmar Sandoval size_t vmcoreinfo_size; 31203e9e41SXunlei Pang u32 *vmcoreinfo_note; 32692f66f2SHari Bathini 331229384fSXunlei Pang /* trusted vmcoreinfo, e.g. we can make a copy in the crash memory */ 341229384fSXunlei Pang static unsigned char *vmcoreinfo_data_safecopy; 351229384fSXunlei Pang 36692f66f2SHari Bathini /* 37692f66f2SHari Bathini * parsing the "crashkernel" commandline 38692f66f2SHari Bathini * 39692f66f2SHari Bathini * this code is intended to be called from architecture specific code 40692f66f2SHari Bathini */ 41692f66f2SHari Bathini 42692f66f2SHari Bathini 43692f66f2SHari Bathini /* 44692f66f2SHari Bathini * This function parses command lines in the format 45692f66f2SHari Bathini * 46692f66f2SHari Bathini * crashkernel=ramsize-range:size[,...][@offset] 47692f66f2SHari Bathini * 48692f66f2SHari Bathini * The function returns 0 on success and -EINVAL on failure. 49692f66f2SHari Bathini */ 50692f66f2SHari Bathini static int __init parse_crashkernel_mem(char *cmdline, 51692f66f2SHari Bathini unsigned long long system_ram, 52692f66f2SHari Bathini unsigned long long *crash_size, 53692f66f2SHari Bathini unsigned long long *crash_base) 54692f66f2SHari Bathini { 55692f66f2SHari Bathini char *cur = cmdline, *tmp; 5646d36b1bSTao Liu unsigned long long total_mem = system_ram; 5746d36b1bSTao Liu 5846d36b1bSTao Liu /* 5946d36b1bSTao Liu * Firmware sometimes reserves some memory regions for its own use, 6046d36b1bSTao Liu * so the system memory size is less than the actual physical memory 6146d36b1bSTao Liu * size. Work around this by rounding up the total size to 128M, 6246d36b1bSTao Liu * which is enough for most test cases. 6346d36b1bSTao Liu */ 6446d36b1bSTao Liu total_mem = roundup(total_mem, SZ_128M); 65692f66f2SHari Bathini 66692f66f2SHari Bathini /* for each entry of the comma-separated list */ 67692f66f2SHari Bathini do { 68692f66f2SHari Bathini unsigned long long start, end = ULLONG_MAX, size; 69692f66f2SHari Bathini 70692f66f2SHari Bathini /* get the start of the range */ 71692f66f2SHari Bathini start = memparse(cur, &tmp); 72692f66f2SHari Bathini if (cur == tmp) { 73692f66f2SHari Bathini pr_warn("crashkernel: Memory value expected\n"); 74692f66f2SHari Bathini return -EINVAL; 75692f66f2SHari Bathini } 76692f66f2SHari Bathini cur = tmp; 77692f66f2SHari Bathini if (*cur != '-') { 78692f66f2SHari Bathini pr_warn("crashkernel: '-' expected\n"); 79692f66f2SHari Bathini return -EINVAL; 80692f66f2SHari Bathini } 81692f66f2SHari Bathini cur++; 82692f66f2SHari Bathini 83692f66f2SHari Bathini /* if no ':' is here, than we read the end */ 84692f66f2SHari Bathini if (*cur != ':') { 85692f66f2SHari Bathini end = memparse(cur, &tmp); 86692f66f2SHari Bathini if (cur == tmp) { 87692f66f2SHari Bathini pr_warn("crashkernel: Memory value expected\n"); 88692f66f2SHari Bathini return -EINVAL; 89692f66f2SHari Bathini } 90692f66f2SHari Bathini cur = tmp; 91692f66f2SHari Bathini if (end <= start) { 92692f66f2SHari Bathini pr_warn("crashkernel: end <= start\n"); 93692f66f2SHari Bathini return -EINVAL; 94692f66f2SHari Bathini } 95692f66f2SHari Bathini } 96692f66f2SHari Bathini 97692f66f2SHari Bathini if (*cur != ':') { 98692f66f2SHari Bathini pr_warn("crashkernel: ':' expected\n"); 99692f66f2SHari Bathini return -EINVAL; 100692f66f2SHari Bathini } 101692f66f2SHari Bathini cur++; 102692f66f2SHari Bathini 103692f66f2SHari Bathini size = memparse(cur, &tmp); 104692f66f2SHari Bathini if (cur == tmp) { 105692f66f2SHari Bathini pr_warn("Memory value expected\n"); 106692f66f2SHari Bathini return -EINVAL; 107692f66f2SHari Bathini } 108692f66f2SHari Bathini cur = tmp; 10946d36b1bSTao Liu if (size >= total_mem) { 110692f66f2SHari Bathini pr_warn("crashkernel: invalid size\n"); 111692f66f2SHari Bathini return -EINVAL; 112692f66f2SHari Bathini } 113692f66f2SHari Bathini 114692f66f2SHari Bathini /* match ? */ 11546d36b1bSTao Liu if (total_mem >= start && total_mem < end) { 116692f66f2SHari Bathini *crash_size = size; 117692f66f2SHari Bathini break; 118692f66f2SHari Bathini } 119692f66f2SHari Bathini } while (*cur++ == ','); 120692f66f2SHari Bathini 121692f66f2SHari Bathini if (*crash_size > 0) { 122692f66f2SHari Bathini while (*cur && *cur != ' ' && *cur != '@') 123692f66f2SHari Bathini cur++; 124692f66f2SHari Bathini if (*cur == '@') { 125692f66f2SHari Bathini cur++; 126692f66f2SHari Bathini *crash_base = memparse(cur, &tmp); 127692f66f2SHari Bathini if (cur == tmp) { 128692f66f2SHari Bathini pr_warn("Memory value expected after '@'\n"); 129692f66f2SHari Bathini return -EINVAL; 130692f66f2SHari Bathini } 131692f66f2SHari Bathini } 132de40ccefSDave Young } else 133de40ccefSDave Young pr_info("crashkernel size resulted in zero bytes\n"); 134692f66f2SHari Bathini 135692f66f2SHari Bathini return 0; 136692f66f2SHari Bathini } 137692f66f2SHari Bathini 138692f66f2SHari Bathini /* 139692f66f2SHari Bathini * That function parses "simple" (old) crashkernel command lines like 140692f66f2SHari Bathini * 141692f66f2SHari Bathini * crashkernel=size[@offset] 142692f66f2SHari Bathini * 143692f66f2SHari Bathini * It returns 0 on success and -EINVAL on failure. 144692f66f2SHari Bathini */ 145692f66f2SHari Bathini static int __init parse_crashkernel_simple(char *cmdline, 146692f66f2SHari Bathini unsigned long long *crash_size, 147692f66f2SHari Bathini unsigned long long *crash_base) 148692f66f2SHari Bathini { 149692f66f2SHari Bathini char *cur = cmdline; 150692f66f2SHari Bathini 151692f66f2SHari Bathini *crash_size = memparse(cmdline, &cur); 152692f66f2SHari Bathini if (cmdline == cur) { 153692f66f2SHari Bathini pr_warn("crashkernel: memory value expected\n"); 154692f66f2SHari Bathini return -EINVAL; 155692f66f2SHari Bathini } 156692f66f2SHari Bathini 157692f66f2SHari Bathini if (*cur == '@') 158692f66f2SHari Bathini *crash_base = memparse(cur+1, &cur); 159692f66f2SHari Bathini else if (*cur != ' ' && *cur != '\0') { 160692f66f2SHari Bathini pr_warn("crashkernel: unrecognized char: %c\n", *cur); 161692f66f2SHari Bathini return -EINVAL; 162692f66f2SHari Bathini } 163692f66f2SHari Bathini 164692f66f2SHari Bathini return 0; 165692f66f2SHari Bathini } 166692f66f2SHari Bathini 167692f66f2SHari Bathini #define SUFFIX_HIGH 0 168692f66f2SHari Bathini #define SUFFIX_LOW 1 169692f66f2SHari Bathini #define SUFFIX_NULL 2 170692f66f2SHari Bathini static __initdata char *suffix_tbl[] = { 171692f66f2SHari Bathini [SUFFIX_HIGH] = ",high", 172692f66f2SHari Bathini [SUFFIX_LOW] = ",low", 173692f66f2SHari Bathini [SUFFIX_NULL] = NULL, 174692f66f2SHari Bathini }; 175692f66f2SHari Bathini 176692f66f2SHari Bathini /* 177692f66f2SHari Bathini * That function parses "suffix" crashkernel command lines like 178692f66f2SHari Bathini * 179692f66f2SHari Bathini * crashkernel=size,[high|low] 180692f66f2SHari Bathini * 181692f66f2SHari Bathini * It returns 0 on success and -EINVAL on failure. 182692f66f2SHari Bathini */ 183692f66f2SHari Bathini static int __init parse_crashkernel_suffix(char *cmdline, 184692f66f2SHari Bathini unsigned long long *crash_size, 185692f66f2SHari Bathini const char *suffix) 186692f66f2SHari Bathini { 187692f66f2SHari Bathini char *cur = cmdline; 188692f66f2SHari Bathini 189692f66f2SHari Bathini *crash_size = memparse(cmdline, &cur); 190692f66f2SHari Bathini if (cmdline == cur) { 191692f66f2SHari Bathini pr_warn("crashkernel: memory value expected\n"); 192692f66f2SHari Bathini return -EINVAL; 193692f66f2SHari Bathini } 194692f66f2SHari Bathini 195692f66f2SHari Bathini /* check with suffix */ 196692f66f2SHari Bathini if (strncmp(cur, suffix, strlen(suffix))) { 197692f66f2SHari Bathini pr_warn("crashkernel: unrecognized char: %c\n", *cur); 198692f66f2SHari Bathini return -EINVAL; 199692f66f2SHari Bathini } 200692f66f2SHari Bathini cur += strlen(suffix); 201692f66f2SHari Bathini if (*cur != ' ' && *cur != '\0') { 202692f66f2SHari Bathini pr_warn("crashkernel: unrecognized char: %c\n", *cur); 203692f66f2SHari Bathini return -EINVAL; 204692f66f2SHari Bathini } 205692f66f2SHari Bathini 206692f66f2SHari Bathini return 0; 207692f66f2SHari Bathini } 208692f66f2SHari Bathini 209692f66f2SHari Bathini static __init char *get_last_crashkernel(char *cmdline, 210692f66f2SHari Bathini const char *name, 211692f66f2SHari Bathini const char *suffix) 212692f66f2SHari Bathini { 213692f66f2SHari Bathini char *p = cmdline, *ck_cmdline = NULL; 214692f66f2SHari Bathini 215692f66f2SHari Bathini /* find crashkernel and use the last one if there are more */ 216692f66f2SHari Bathini p = strstr(p, name); 217692f66f2SHari Bathini while (p) { 218692f66f2SHari Bathini char *end_p = strchr(p, ' '); 219692f66f2SHari Bathini char *q; 220692f66f2SHari Bathini 221692f66f2SHari Bathini if (!end_p) 222692f66f2SHari Bathini end_p = p + strlen(p); 223692f66f2SHari Bathini 224692f66f2SHari Bathini if (!suffix) { 225692f66f2SHari Bathini int i; 226692f66f2SHari Bathini 227692f66f2SHari Bathini /* skip the one with any known suffix */ 228692f66f2SHari Bathini for (i = 0; suffix_tbl[i]; i++) { 229692f66f2SHari Bathini q = end_p - strlen(suffix_tbl[i]); 230692f66f2SHari Bathini if (!strncmp(q, suffix_tbl[i], 231692f66f2SHari Bathini strlen(suffix_tbl[i]))) 232692f66f2SHari Bathini goto next; 233692f66f2SHari Bathini } 234692f66f2SHari Bathini ck_cmdline = p; 235692f66f2SHari Bathini } else { 236692f66f2SHari Bathini q = end_p - strlen(suffix); 237692f66f2SHari Bathini if (!strncmp(q, suffix, strlen(suffix))) 238692f66f2SHari Bathini ck_cmdline = p; 239692f66f2SHari Bathini } 240692f66f2SHari Bathini next: 241692f66f2SHari Bathini p = strstr(p+1, name); 242692f66f2SHari Bathini } 243692f66f2SHari Bathini 244692f66f2SHari Bathini return ck_cmdline; 245692f66f2SHari Bathini } 246692f66f2SHari Bathini 247692f66f2SHari Bathini static int __init __parse_crashkernel(char *cmdline, 248692f66f2SHari Bathini unsigned long long system_ram, 249692f66f2SHari Bathini unsigned long long *crash_size, 250692f66f2SHari Bathini unsigned long long *crash_base, 251692f66f2SHari Bathini const char *name, 252692f66f2SHari Bathini const char *suffix) 253692f66f2SHari Bathini { 254692f66f2SHari Bathini char *first_colon, *first_space; 255692f66f2SHari Bathini char *ck_cmdline; 256692f66f2SHari Bathini 257692f66f2SHari Bathini BUG_ON(!crash_size || !crash_base); 258692f66f2SHari Bathini *crash_size = 0; 259692f66f2SHari Bathini *crash_base = 0; 260692f66f2SHari Bathini 261692f66f2SHari Bathini ck_cmdline = get_last_crashkernel(cmdline, name, suffix); 262692f66f2SHari Bathini if (!ck_cmdline) 2632e5920bbSZhen Lei return -ENOENT; 264692f66f2SHari Bathini 265692f66f2SHari Bathini ck_cmdline += strlen(name); 266692f66f2SHari Bathini 267692f66f2SHari Bathini if (suffix) 268692f66f2SHari Bathini return parse_crashkernel_suffix(ck_cmdline, crash_size, 269692f66f2SHari Bathini suffix); 270692f66f2SHari Bathini /* 271692f66f2SHari Bathini * if the commandline contains a ':', then that's the extended 272692f66f2SHari Bathini * syntax -- if not, it must be the classic syntax 273692f66f2SHari Bathini */ 274692f66f2SHari Bathini first_colon = strchr(ck_cmdline, ':'); 275692f66f2SHari Bathini first_space = strchr(ck_cmdline, ' '); 276692f66f2SHari Bathini if (first_colon && (!first_space || first_colon < first_space)) 277692f66f2SHari Bathini return parse_crashkernel_mem(ck_cmdline, system_ram, 278692f66f2SHari Bathini crash_size, crash_base); 279692f66f2SHari Bathini 280692f66f2SHari Bathini return parse_crashkernel_simple(ck_cmdline, crash_size, crash_base); 281692f66f2SHari Bathini } 282692f66f2SHari Bathini 283692f66f2SHari Bathini /* 284692f66f2SHari Bathini * That function is the entry point for command line parsing and should be 285692f66f2SHari Bathini * called from the arch-specific code. 286692f66f2SHari Bathini */ 287692f66f2SHari Bathini int __init parse_crashkernel(char *cmdline, 288692f66f2SHari Bathini unsigned long long system_ram, 289692f66f2SHari Bathini unsigned long long *crash_size, 290692f66f2SHari Bathini unsigned long long *crash_base) 291692f66f2SHari Bathini { 292692f66f2SHari Bathini return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base, 293692f66f2SHari Bathini "crashkernel=", NULL); 294692f66f2SHari Bathini } 295692f66f2SHari Bathini 296692f66f2SHari Bathini int __init parse_crashkernel_high(char *cmdline, 297692f66f2SHari Bathini unsigned long long system_ram, 298692f66f2SHari Bathini unsigned long long *crash_size, 299692f66f2SHari Bathini unsigned long long *crash_base) 300692f66f2SHari Bathini { 301692f66f2SHari Bathini return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base, 302692f66f2SHari Bathini "crashkernel=", suffix_tbl[SUFFIX_HIGH]); 303692f66f2SHari Bathini } 304692f66f2SHari Bathini 305692f66f2SHari Bathini int __init parse_crashkernel_low(char *cmdline, 306692f66f2SHari Bathini unsigned long long system_ram, 307692f66f2SHari Bathini unsigned long long *crash_size, 308692f66f2SHari Bathini unsigned long long *crash_base) 309692f66f2SHari Bathini { 310692f66f2SHari Bathini return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base, 311692f66f2SHari Bathini "crashkernel=", suffix_tbl[SUFFIX_LOW]); 312692f66f2SHari Bathini } 313692f66f2SHari Bathini 31471d2bcecSPhilipp Rudo /* 31571d2bcecSPhilipp Rudo * Add a dummy early_param handler to mark crashkernel= as a known command line 31671d2bcecSPhilipp Rudo * parameter and suppress incorrect warnings in init/main.c. 31771d2bcecSPhilipp Rudo */ 31871d2bcecSPhilipp Rudo static int __init parse_crashkernel_dummy(char *arg) 31971d2bcecSPhilipp Rudo { 32071d2bcecSPhilipp Rudo return 0; 32171d2bcecSPhilipp Rudo } 32271d2bcecSPhilipp Rudo early_param("crashkernel", parse_crashkernel_dummy); 32371d2bcecSPhilipp Rudo 3246f991cc3SEric DeVolder int crash_prepare_elf64_headers(struct crash_mem *mem, int need_kernel_map, 3256f991cc3SEric DeVolder void **addr, unsigned long *sz) 3266f991cc3SEric DeVolder { 3276f991cc3SEric DeVolder Elf64_Ehdr *ehdr; 3286f991cc3SEric DeVolder Elf64_Phdr *phdr; 3296f991cc3SEric DeVolder unsigned long nr_cpus = num_possible_cpus(), nr_phdr, elf_sz; 3306f991cc3SEric DeVolder unsigned char *buf; 3316f991cc3SEric DeVolder unsigned int cpu, i; 3326f991cc3SEric DeVolder unsigned long long notes_addr; 3336f991cc3SEric DeVolder unsigned long mstart, mend; 3346f991cc3SEric DeVolder 3356f991cc3SEric DeVolder /* extra phdr for vmcoreinfo ELF note */ 3366f991cc3SEric DeVolder nr_phdr = nr_cpus + 1; 3376f991cc3SEric DeVolder nr_phdr += mem->nr_ranges; 3386f991cc3SEric DeVolder 3396f991cc3SEric DeVolder /* 3406f991cc3SEric DeVolder * kexec-tools creates an extra PT_LOAD phdr for kernel text mapping 3416f991cc3SEric DeVolder * area (for example, ffffffff80000000 - ffffffffa0000000 on x86_64). 3426f991cc3SEric DeVolder * I think this is required by tools like gdb. So same physical 3436f991cc3SEric DeVolder * memory will be mapped in two ELF headers. One will contain kernel 3446f991cc3SEric DeVolder * text virtual addresses and other will have __va(physical) addresses. 3456f991cc3SEric DeVolder */ 3466f991cc3SEric DeVolder 3476f991cc3SEric DeVolder nr_phdr++; 3486f991cc3SEric DeVolder elf_sz = sizeof(Elf64_Ehdr) + nr_phdr * sizeof(Elf64_Phdr); 3496f991cc3SEric DeVolder elf_sz = ALIGN(elf_sz, ELF_CORE_HEADER_ALIGN); 3506f991cc3SEric DeVolder 3516f991cc3SEric DeVolder buf = vzalloc(elf_sz); 3526f991cc3SEric DeVolder if (!buf) 3536f991cc3SEric DeVolder return -ENOMEM; 3546f991cc3SEric DeVolder 3556f991cc3SEric DeVolder ehdr = (Elf64_Ehdr *)buf; 3566f991cc3SEric DeVolder phdr = (Elf64_Phdr *)(ehdr + 1); 3576f991cc3SEric DeVolder memcpy(ehdr->e_ident, ELFMAG, SELFMAG); 3586f991cc3SEric DeVolder ehdr->e_ident[EI_CLASS] = ELFCLASS64; 3596f991cc3SEric DeVolder ehdr->e_ident[EI_DATA] = ELFDATA2LSB; 3606f991cc3SEric DeVolder ehdr->e_ident[EI_VERSION] = EV_CURRENT; 3616f991cc3SEric DeVolder ehdr->e_ident[EI_OSABI] = ELF_OSABI; 3626f991cc3SEric DeVolder memset(ehdr->e_ident + EI_PAD, 0, EI_NIDENT - EI_PAD); 3636f991cc3SEric DeVolder ehdr->e_type = ET_CORE; 3646f991cc3SEric DeVolder ehdr->e_machine = ELF_ARCH; 3656f991cc3SEric DeVolder ehdr->e_version = EV_CURRENT; 3666f991cc3SEric DeVolder ehdr->e_phoff = sizeof(Elf64_Ehdr); 3676f991cc3SEric DeVolder ehdr->e_ehsize = sizeof(Elf64_Ehdr); 3686f991cc3SEric DeVolder ehdr->e_phentsize = sizeof(Elf64_Phdr); 3696f991cc3SEric DeVolder 3706f991cc3SEric DeVolder /* Prepare one phdr of type PT_NOTE for each present CPU */ 3716f991cc3SEric DeVolder for_each_present_cpu(cpu) { 3726f991cc3SEric DeVolder phdr->p_type = PT_NOTE; 3736f991cc3SEric DeVolder notes_addr = per_cpu_ptr_to_phys(per_cpu_ptr(crash_notes, cpu)); 3746f991cc3SEric DeVolder phdr->p_offset = phdr->p_paddr = notes_addr; 3756f991cc3SEric DeVolder phdr->p_filesz = phdr->p_memsz = sizeof(note_buf_t); 3766f991cc3SEric DeVolder (ehdr->e_phnum)++; 3776f991cc3SEric DeVolder phdr++; 3786f991cc3SEric DeVolder } 3796f991cc3SEric DeVolder 3806f991cc3SEric DeVolder /* Prepare one PT_NOTE header for vmcoreinfo */ 3816f991cc3SEric DeVolder phdr->p_type = PT_NOTE; 3826f991cc3SEric DeVolder phdr->p_offset = phdr->p_paddr = paddr_vmcoreinfo_note(); 3836f991cc3SEric DeVolder phdr->p_filesz = phdr->p_memsz = VMCOREINFO_NOTE_SIZE; 3846f991cc3SEric DeVolder (ehdr->e_phnum)++; 3856f991cc3SEric DeVolder phdr++; 3866f991cc3SEric DeVolder 3876f991cc3SEric DeVolder /* Prepare PT_LOAD type program header for kernel text region */ 3886f991cc3SEric DeVolder if (need_kernel_map) { 3896f991cc3SEric DeVolder phdr->p_type = PT_LOAD; 3906f991cc3SEric DeVolder phdr->p_flags = PF_R|PF_W|PF_X; 3916f991cc3SEric DeVolder phdr->p_vaddr = (unsigned long) _text; 3926f991cc3SEric DeVolder phdr->p_filesz = phdr->p_memsz = _end - _text; 3936f991cc3SEric DeVolder phdr->p_offset = phdr->p_paddr = __pa_symbol(_text); 3946f991cc3SEric DeVolder ehdr->e_phnum++; 3956f991cc3SEric DeVolder phdr++; 3966f991cc3SEric DeVolder } 3976f991cc3SEric DeVolder 3986f991cc3SEric DeVolder /* Go through all the ranges in mem->ranges[] and prepare phdr */ 3996f991cc3SEric DeVolder for (i = 0; i < mem->nr_ranges; i++) { 4006f991cc3SEric DeVolder mstart = mem->ranges[i].start; 4016f991cc3SEric DeVolder mend = mem->ranges[i].end; 4026f991cc3SEric DeVolder 4036f991cc3SEric DeVolder phdr->p_type = PT_LOAD; 4046f991cc3SEric DeVolder phdr->p_flags = PF_R|PF_W|PF_X; 4056f991cc3SEric DeVolder phdr->p_offset = mstart; 4066f991cc3SEric DeVolder 4076f991cc3SEric DeVolder phdr->p_paddr = mstart; 4086f991cc3SEric DeVolder phdr->p_vaddr = (unsigned long) __va(mstart); 4096f991cc3SEric DeVolder phdr->p_filesz = phdr->p_memsz = mend - mstart + 1; 4106f991cc3SEric DeVolder phdr->p_align = 0; 4116f991cc3SEric DeVolder ehdr->e_phnum++; 4126f991cc3SEric 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", 4136f991cc3SEric DeVolder phdr, phdr->p_vaddr, phdr->p_paddr, phdr->p_filesz, 4146f991cc3SEric DeVolder ehdr->e_phnum, phdr->p_offset); 4156f991cc3SEric DeVolder phdr++; 4166f991cc3SEric DeVolder } 4176f991cc3SEric DeVolder 4186f991cc3SEric DeVolder *addr = buf; 4196f991cc3SEric DeVolder *sz = elf_sz; 4206f991cc3SEric DeVolder return 0; 4216f991cc3SEric DeVolder } 4226f991cc3SEric DeVolder 4236f991cc3SEric DeVolder int crash_exclude_mem_range(struct crash_mem *mem, 4246f991cc3SEric DeVolder unsigned long long mstart, unsigned long long mend) 4256f991cc3SEric DeVolder { 4266f991cc3SEric DeVolder int i, j; 4276f991cc3SEric DeVolder unsigned long long start, end, p_start, p_end; 4286f991cc3SEric DeVolder struct range temp_range = {0, 0}; 4296f991cc3SEric DeVolder 4306f991cc3SEric DeVolder for (i = 0; i < mem->nr_ranges; i++) { 4316f991cc3SEric DeVolder start = mem->ranges[i].start; 4326f991cc3SEric DeVolder end = mem->ranges[i].end; 4336f991cc3SEric DeVolder p_start = mstart; 4346f991cc3SEric DeVolder p_end = mend; 4356f991cc3SEric DeVolder 4366f991cc3SEric DeVolder if (mstart > end || mend < start) 4376f991cc3SEric DeVolder continue; 4386f991cc3SEric DeVolder 4396f991cc3SEric DeVolder /* Truncate any area outside of range */ 4406f991cc3SEric DeVolder if (mstart < start) 4416f991cc3SEric DeVolder p_start = start; 4426f991cc3SEric DeVolder if (mend > end) 4436f991cc3SEric DeVolder p_end = end; 4446f991cc3SEric DeVolder 4456f991cc3SEric DeVolder /* Found completely overlapping range */ 4466f991cc3SEric DeVolder if (p_start == start && p_end == end) { 4476f991cc3SEric DeVolder mem->ranges[i].start = 0; 4486f991cc3SEric DeVolder mem->ranges[i].end = 0; 4496f991cc3SEric DeVolder if (i < mem->nr_ranges - 1) { 4506f991cc3SEric DeVolder /* Shift rest of the ranges to left */ 4516f991cc3SEric DeVolder for (j = i; j < mem->nr_ranges - 1; j++) { 4526f991cc3SEric DeVolder mem->ranges[j].start = 4536f991cc3SEric DeVolder mem->ranges[j+1].start; 4546f991cc3SEric DeVolder mem->ranges[j].end = 4556f991cc3SEric DeVolder mem->ranges[j+1].end; 4566f991cc3SEric DeVolder } 4576f991cc3SEric DeVolder 4586f991cc3SEric DeVolder /* 4596f991cc3SEric DeVolder * Continue to check if there are another overlapping ranges 4606f991cc3SEric DeVolder * from the current position because of shifting the above 4616f991cc3SEric DeVolder * mem ranges. 4626f991cc3SEric DeVolder */ 4636f991cc3SEric DeVolder i--; 4646f991cc3SEric DeVolder mem->nr_ranges--; 4656f991cc3SEric DeVolder continue; 4666f991cc3SEric DeVolder } 4676f991cc3SEric DeVolder mem->nr_ranges--; 4686f991cc3SEric DeVolder return 0; 4696f991cc3SEric DeVolder } 4706f991cc3SEric DeVolder 4716f991cc3SEric DeVolder if (p_start > start && p_end < end) { 4726f991cc3SEric DeVolder /* Split original range */ 4736f991cc3SEric DeVolder mem->ranges[i].end = p_start - 1; 4746f991cc3SEric DeVolder temp_range.start = p_end + 1; 4756f991cc3SEric DeVolder temp_range.end = end; 4766f991cc3SEric DeVolder } else if (p_start != start) 4776f991cc3SEric DeVolder mem->ranges[i].end = p_start - 1; 4786f991cc3SEric DeVolder else 4796f991cc3SEric DeVolder mem->ranges[i].start = p_end + 1; 4806f991cc3SEric DeVolder break; 4816f991cc3SEric DeVolder } 4826f991cc3SEric DeVolder 4836f991cc3SEric DeVolder /* If a split happened, add the split to array */ 4846f991cc3SEric DeVolder if (!temp_range.end) 4856f991cc3SEric DeVolder return 0; 4866f991cc3SEric DeVolder 4876f991cc3SEric DeVolder /* Split happened */ 4886f991cc3SEric DeVolder if (i == mem->max_nr_ranges - 1) 4896f991cc3SEric DeVolder return -ENOMEM; 4906f991cc3SEric DeVolder 4916f991cc3SEric DeVolder /* Location where new range should go */ 4926f991cc3SEric DeVolder j = i + 1; 4936f991cc3SEric DeVolder if (j < mem->nr_ranges) { 4946f991cc3SEric DeVolder /* Move over all ranges one slot towards the end */ 4956f991cc3SEric DeVolder for (i = mem->nr_ranges - 1; i >= j; i--) 4966f991cc3SEric DeVolder mem->ranges[i + 1] = mem->ranges[i]; 4976f991cc3SEric DeVolder } 4986f991cc3SEric DeVolder 4996f991cc3SEric DeVolder mem->ranges[j].start = temp_range.start; 5006f991cc3SEric DeVolder mem->ranges[j].end = temp_range.end; 5016f991cc3SEric DeVolder mem->nr_ranges++; 5026f991cc3SEric DeVolder return 0; 5036f991cc3SEric DeVolder } 5046f991cc3SEric DeVolder 50551dbd925SHari Bathini Elf_Word *append_elf_note(Elf_Word *buf, char *name, unsigned int type, 506692f66f2SHari Bathini void *data, size_t data_len) 507692f66f2SHari Bathini { 50851dbd925SHari Bathini struct elf_note *note = (struct elf_note *)buf; 509692f66f2SHari Bathini 51051dbd925SHari Bathini note->n_namesz = strlen(name) + 1; 51151dbd925SHari Bathini note->n_descsz = data_len; 51251dbd925SHari Bathini note->n_type = type; 51351dbd925SHari Bathini buf += DIV_ROUND_UP(sizeof(*note), sizeof(Elf_Word)); 51451dbd925SHari Bathini memcpy(buf, name, note->n_namesz); 51551dbd925SHari Bathini buf += DIV_ROUND_UP(note->n_namesz, sizeof(Elf_Word)); 51651dbd925SHari Bathini memcpy(buf, data, data_len); 51751dbd925SHari Bathini buf += DIV_ROUND_UP(data_len, sizeof(Elf_Word)); 518692f66f2SHari Bathini 519692f66f2SHari Bathini return buf; 520692f66f2SHari Bathini } 521692f66f2SHari Bathini 52251dbd925SHari Bathini void final_note(Elf_Word *buf) 523692f66f2SHari Bathini { 52451dbd925SHari Bathini memset(buf, 0, sizeof(struct elf_note)); 525692f66f2SHari Bathini } 526692f66f2SHari Bathini 527692f66f2SHari Bathini static void update_vmcoreinfo_note(void) 528692f66f2SHari Bathini { 529692f66f2SHari Bathini u32 *buf = vmcoreinfo_note; 530692f66f2SHari Bathini 531692f66f2SHari Bathini if (!vmcoreinfo_size) 532692f66f2SHari Bathini return; 533692f66f2SHari Bathini buf = append_elf_note(buf, VMCOREINFO_NOTE_NAME, 0, vmcoreinfo_data, 534692f66f2SHari Bathini vmcoreinfo_size); 535692f66f2SHari Bathini final_note(buf); 536692f66f2SHari Bathini } 537692f66f2SHari Bathini 5381229384fSXunlei Pang void crash_update_vmcoreinfo_safecopy(void *ptr) 5391229384fSXunlei Pang { 5401229384fSXunlei Pang if (ptr) 5411229384fSXunlei Pang memcpy(ptr, vmcoreinfo_data, vmcoreinfo_size); 5421229384fSXunlei Pang 5431229384fSXunlei Pang vmcoreinfo_data_safecopy = ptr; 5441229384fSXunlei Pang } 5451229384fSXunlei Pang 546692f66f2SHari Bathini void crash_save_vmcoreinfo(void) 547692f66f2SHari Bathini { 548203e9e41SXunlei Pang if (!vmcoreinfo_note) 549203e9e41SXunlei Pang return; 550203e9e41SXunlei Pang 5511229384fSXunlei Pang /* Use the safe copy to generate vmcoreinfo note if have */ 5521229384fSXunlei Pang if (vmcoreinfo_data_safecopy) 5531229384fSXunlei Pang vmcoreinfo_data = vmcoreinfo_data_safecopy; 5541229384fSXunlei Pang 55591bc9aafSArnd Bergmann vmcoreinfo_append_str("CRASHTIME=%lld\n", ktime_get_real_seconds()); 556692f66f2SHari Bathini update_vmcoreinfo_note(); 557692f66f2SHari Bathini } 558692f66f2SHari Bathini 559692f66f2SHari Bathini void vmcoreinfo_append_str(const char *fmt, ...) 560692f66f2SHari Bathini { 561692f66f2SHari Bathini va_list args; 562692f66f2SHari Bathini char buf[0x50]; 563692f66f2SHari Bathini size_t r; 564692f66f2SHari Bathini 565692f66f2SHari Bathini va_start(args, fmt); 566692f66f2SHari Bathini r = vscnprintf(buf, sizeof(buf), fmt, args); 567692f66f2SHari Bathini va_end(args); 568692f66f2SHari Bathini 5695203f499SXunlei Pang r = min(r, (size_t)VMCOREINFO_BYTES - vmcoreinfo_size); 570692f66f2SHari Bathini 571692f66f2SHari Bathini memcpy(&vmcoreinfo_data[vmcoreinfo_size], buf, r); 572692f66f2SHari Bathini 573692f66f2SHari Bathini vmcoreinfo_size += r; 57408fc35f3SStephen Brennan 57508fc35f3SStephen Brennan WARN_ONCE(vmcoreinfo_size == VMCOREINFO_BYTES, 57608fc35f3SStephen Brennan "vmcoreinfo data exceeds allocated size, truncating"); 577692f66f2SHari Bathini } 578692f66f2SHari Bathini 579692f66f2SHari Bathini /* 580692f66f2SHari Bathini * provide an empty default implementation here -- architecture 581692f66f2SHari Bathini * code may override this 582692f66f2SHari Bathini */ 583692f66f2SHari Bathini void __weak arch_crash_save_vmcoreinfo(void) 584692f66f2SHari Bathini {} 585692f66f2SHari Bathini 586692f66f2SHari Bathini phys_addr_t __weak paddr_vmcoreinfo_note(void) 587692f66f2SHari Bathini { 588203e9e41SXunlei Pang return __pa(vmcoreinfo_note); 589692f66f2SHari Bathini } 59043d4cb47SMarc-André Lureau EXPORT_SYMBOL(paddr_vmcoreinfo_note); 591692f66f2SHari Bathini 592692f66f2SHari Bathini static int __init crash_save_vmcoreinfo_init(void) 593692f66f2SHari Bathini { 594203e9e41SXunlei Pang vmcoreinfo_data = (unsigned char *)get_zeroed_page(GFP_KERNEL); 595203e9e41SXunlei Pang if (!vmcoreinfo_data) { 596203e9e41SXunlei Pang pr_warn("Memory allocation for vmcoreinfo_data failed\n"); 597203e9e41SXunlei Pang return -ENOMEM; 598203e9e41SXunlei Pang } 599203e9e41SXunlei Pang 600203e9e41SXunlei Pang vmcoreinfo_note = alloc_pages_exact(VMCOREINFO_NOTE_SIZE, 601203e9e41SXunlei Pang GFP_KERNEL | __GFP_ZERO); 602203e9e41SXunlei Pang if (!vmcoreinfo_note) { 603203e9e41SXunlei Pang free_page((unsigned long)vmcoreinfo_data); 604203e9e41SXunlei Pang vmcoreinfo_data = NULL; 605203e9e41SXunlei Pang pr_warn("Memory allocation for vmcoreinfo_note failed\n"); 606203e9e41SXunlei Pang return -ENOMEM; 607203e9e41SXunlei Pang } 608203e9e41SXunlei Pang 609692f66f2SHari Bathini VMCOREINFO_OSRELEASE(init_uts_ns.name.release); 61044e8a5e9SStephen Boyd VMCOREINFO_BUILD_ID(); 611692f66f2SHari Bathini VMCOREINFO_PAGESIZE(PAGE_SIZE); 612692f66f2SHari Bathini 613692f66f2SHari Bathini VMCOREINFO_SYMBOL(init_uts_ns); 614ca4a9241SAlexander Egorenkov VMCOREINFO_OFFSET(uts_namespace, name); 615692f66f2SHari Bathini VMCOREINFO_SYMBOL(node_online_map); 616692f66f2SHari Bathini #ifdef CONFIG_MMU 617eff4345eSOmar Sandoval VMCOREINFO_SYMBOL_ARRAY(swapper_pg_dir); 618692f66f2SHari Bathini #endif 619692f66f2SHari Bathini VMCOREINFO_SYMBOL(_stext); 620692f66f2SHari Bathini VMCOREINFO_SYMBOL(vmap_area_list); 621692f66f2SHari Bathini 622a9ee6cf5SMike Rapoport #ifndef CONFIG_NUMA 623692f66f2SHari Bathini VMCOREINFO_SYMBOL(mem_map); 624692f66f2SHari Bathini VMCOREINFO_SYMBOL(contig_page_data); 625692f66f2SHari Bathini #endif 626692f66f2SHari Bathini #ifdef CONFIG_SPARSEMEM 627a0b12803SKirill A. Shutemov VMCOREINFO_SYMBOL_ARRAY(mem_section); 628692f66f2SHari Bathini VMCOREINFO_LENGTH(mem_section, NR_SECTION_ROOTS); 629692f66f2SHari Bathini VMCOREINFO_STRUCT_SIZE(mem_section); 630692f66f2SHari Bathini VMCOREINFO_OFFSET(mem_section, section_mem_map); 6314f5aecdfSPingfan Liu VMCOREINFO_NUMBER(SECTION_SIZE_BITS); 6321d50e5d0SBhupesh Sharma VMCOREINFO_NUMBER(MAX_PHYSMEM_BITS); 633692f66f2SHari Bathini #endif 634692f66f2SHari Bathini VMCOREINFO_STRUCT_SIZE(page); 635692f66f2SHari Bathini VMCOREINFO_STRUCT_SIZE(pglist_data); 636692f66f2SHari Bathini VMCOREINFO_STRUCT_SIZE(zone); 637692f66f2SHari Bathini VMCOREINFO_STRUCT_SIZE(free_area); 638692f66f2SHari Bathini VMCOREINFO_STRUCT_SIZE(list_head); 639692f66f2SHari Bathini VMCOREINFO_SIZE(nodemask_t); 640692f66f2SHari Bathini VMCOREINFO_OFFSET(page, flags); 641692f66f2SHari Bathini VMCOREINFO_OFFSET(page, _refcount); 642692f66f2SHari Bathini VMCOREINFO_OFFSET(page, mapping); 643692f66f2SHari Bathini VMCOREINFO_OFFSET(page, lru); 644692f66f2SHari Bathini VMCOREINFO_OFFSET(page, _mapcount); 645692f66f2SHari Bathini VMCOREINFO_OFFSET(page, private); 6461c5509beSMatthew Wilcox (Oracle) VMCOREINFO_OFFSET(folio, _folio_dtor); 6471c5509beSMatthew Wilcox (Oracle) VMCOREINFO_OFFSET(folio, _folio_order); 648692f66f2SHari Bathini VMCOREINFO_OFFSET(page, compound_head); 649692f66f2SHari Bathini VMCOREINFO_OFFSET(pglist_data, node_zones); 650692f66f2SHari Bathini VMCOREINFO_OFFSET(pglist_data, nr_zones); 65143b02ba9SMike Rapoport #ifdef CONFIG_FLATMEM 652692f66f2SHari Bathini VMCOREINFO_OFFSET(pglist_data, node_mem_map); 653692f66f2SHari Bathini #endif 654692f66f2SHari Bathini VMCOREINFO_OFFSET(pglist_data, node_start_pfn); 655692f66f2SHari Bathini VMCOREINFO_OFFSET(pglist_data, node_spanned_pages); 656692f66f2SHari Bathini VMCOREINFO_OFFSET(pglist_data, node_id); 657692f66f2SHari Bathini VMCOREINFO_OFFSET(zone, free_area); 658692f66f2SHari Bathini VMCOREINFO_OFFSET(zone, vm_stat); 659692f66f2SHari Bathini VMCOREINFO_OFFSET(zone, spanned_pages); 660692f66f2SHari Bathini VMCOREINFO_OFFSET(free_area, free_list); 661692f66f2SHari Bathini VMCOREINFO_OFFSET(list_head, next); 662692f66f2SHari Bathini VMCOREINFO_OFFSET(list_head, prev); 663692f66f2SHari Bathini VMCOREINFO_OFFSET(vmap_area, va_start); 664692f66f2SHari Bathini VMCOREINFO_OFFSET(vmap_area, list); 66523baf831SKirill A. Shutemov VMCOREINFO_LENGTH(zone.free_area, MAX_ORDER + 1); 666692f66f2SHari Bathini log_buf_vmcoreinfo_setup(); 667692f66f2SHari Bathini VMCOREINFO_LENGTH(free_area.free_list, MIGRATE_TYPES); 668692f66f2SHari Bathini VMCOREINFO_NUMBER(NR_FREE_PAGES); 669692f66f2SHari Bathini VMCOREINFO_NUMBER(PG_lru); 670692f66f2SHari Bathini VMCOREINFO_NUMBER(PG_private); 671692f66f2SHari Bathini VMCOREINFO_NUMBER(PG_swapcache); 6721cbf29daSPetr Tesarik VMCOREINFO_NUMBER(PG_swapbacked); 673692f66f2SHari Bathini VMCOREINFO_NUMBER(PG_slab); 674692f66f2SHari Bathini #ifdef CONFIG_MEMORY_FAILURE 675692f66f2SHari Bathini VMCOREINFO_NUMBER(PG_hwpoison); 676692f66f2SHari Bathini #endif 677692f66f2SHari Bathini VMCOREINFO_NUMBER(PG_head_mask); 6786e292b9bSMatthew Wilcox #define PAGE_BUDDY_MAPCOUNT_VALUE (~PG_buddy) 679692f66f2SHari Bathini VMCOREINFO_NUMBER(PAGE_BUDDY_MAPCOUNT_VALUE); 680692f66f2SHari Bathini #ifdef CONFIG_HUGETLB_PAGE 681692f66f2SHari Bathini VMCOREINFO_NUMBER(HUGETLB_PAGE_DTOR); 682e04b742fSDavid Hildenbrand #define PAGE_OFFLINE_MAPCOUNT_VALUE (~PG_offline) 683e04b742fSDavid Hildenbrand VMCOREINFO_NUMBER(PAGE_OFFLINE_MAPCOUNT_VALUE); 684692f66f2SHari Bathini #endif 685692f66f2SHari Bathini 6865fd8fea9SStephen Brennan #ifdef CONFIG_KALLSYMS 6875fd8fea9SStephen Brennan VMCOREINFO_SYMBOL(kallsyms_names); 688f09bddbdSStephen Brennan VMCOREINFO_SYMBOL(kallsyms_num_syms); 6895fd8fea9SStephen Brennan VMCOREINFO_SYMBOL(kallsyms_token_table); 6905fd8fea9SStephen Brennan VMCOREINFO_SYMBOL(kallsyms_token_index); 6915fd8fea9SStephen Brennan #ifdef CONFIG_KALLSYMS_BASE_RELATIVE 6925fd8fea9SStephen Brennan VMCOREINFO_SYMBOL(kallsyms_offsets); 6935fd8fea9SStephen Brennan VMCOREINFO_SYMBOL(kallsyms_relative_base); 6945fd8fea9SStephen Brennan #else 6955fd8fea9SStephen Brennan VMCOREINFO_SYMBOL(kallsyms_addresses); 6965fd8fea9SStephen Brennan #endif /* CONFIG_KALLSYMS_BASE_RELATIVE */ 6975fd8fea9SStephen Brennan #endif /* CONFIG_KALLSYMS */ 6985fd8fea9SStephen Brennan 699692f66f2SHari Bathini arch_crash_save_vmcoreinfo(); 700692f66f2SHari Bathini update_vmcoreinfo_note(); 701692f66f2SHari Bathini 702692f66f2SHari Bathini return 0; 703692f66f2SHari Bathini } 704692f66f2SHari Bathini 705692f66f2SHari Bathini subsys_initcall(crash_save_vmcoreinfo_init); 7066f991cc3SEric DeVolder 7076f991cc3SEric DeVolder static int __init crash_notes_memory_init(void) 7086f991cc3SEric DeVolder { 7096f991cc3SEric DeVolder /* Allocate memory for saving cpu registers. */ 7106f991cc3SEric DeVolder size_t size, align; 7116f991cc3SEric DeVolder 7126f991cc3SEric DeVolder /* 7136f991cc3SEric DeVolder * crash_notes could be allocated across 2 vmalloc pages when percpu 7146f991cc3SEric DeVolder * is vmalloc based . vmalloc doesn't guarantee 2 continuous vmalloc 7156f991cc3SEric DeVolder * pages are also on 2 continuous physical pages. In this case the 7166f991cc3SEric DeVolder * 2nd part of crash_notes in 2nd page could be lost since only the 7176f991cc3SEric DeVolder * starting address and size of crash_notes are exported through sysfs. 7186f991cc3SEric DeVolder * Here round up the size of crash_notes to the nearest power of two 7196f991cc3SEric DeVolder * and pass it to __alloc_percpu as align value. This can make sure 7206f991cc3SEric DeVolder * crash_notes is allocated inside one physical page. 7216f991cc3SEric DeVolder */ 7226f991cc3SEric DeVolder size = sizeof(note_buf_t); 7236f991cc3SEric DeVolder align = min(roundup_pow_of_two(sizeof(note_buf_t)), PAGE_SIZE); 7246f991cc3SEric DeVolder 7256f991cc3SEric DeVolder /* 7266f991cc3SEric DeVolder * Break compile if size is bigger than PAGE_SIZE since crash_notes 7276f991cc3SEric DeVolder * definitely will be in 2 pages with that. 7286f991cc3SEric DeVolder */ 7296f991cc3SEric DeVolder BUILD_BUG_ON(size > PAGE_SIZE); 7306f991cc3SEric DeVolder 7316f991cc3SEric DeVolder crash_notes = __alloc_percpu(size, align); 7326f991cc3SEric DeVolder if (!crash_notes) { 7336f991cc3SEric DeVolder pr_warn("Memory allocation for saving cpu register states failed\n"); 7346f991cc3SEric DeVolder return -ENOMEM; 7356f991cc3SEric DeVolder } 7366f991cc3SEric DeVolder return 0; 7376f991cc3SEric DeVolder } 7386f991cc3SEric DeVolder subsys_initcall(crash_notes_memory_init); 73924726275SEric DeVolder 74024726275SEric DeVolder #ifdef CONFIG_CRASH_HOTPLUG 74124726275SEric DeVolder #undef pr_fmt 74224726275SEric DeVolder #define pr_fmt(fmt) "crash hp: " fmt 743*a72bbec7SEric DeVolder 744*a72bbec7SEric DeVolder /* 745*a72bbec7SEric DeVolder * This routine utilized when the crash_hotplug sysfs node is read. 746*a72bbec7SEric DeVolder * It reflects the kernel's ability/permission to update the crash 747*a72bbec7SEric DeVolder * elfcorehdr directly. 748*a72bbec7SEric DeVolder */ 749*a72bbec7SEric DeVolder int crash_check_update_elfcorehdr(void) 750*a72bbec7SEric DeVolder { 751*a72bbec7SEric DeVolder int rc = 0; 752*a72bbec7SEric DeVolder 753*a72bbec7SEric DeVolder /* Obtain lock while reading crash information */ 754*a72bbec7SEric DeVolder if (!kexec_trylock()) { 755*a72bbec7SEric DeVolder pr_info("kexec_trylock() failed, elfcorehdr may be inaccurate\n"); 756*a72bbec7SEric DeVolder return 0; 757*a72bbec7SEric DeVolder } 758*a72bbec7SEric DeVolder if (kexec_crash_image) { 759*a72bbec7SEric DeVolder if (kexec_crash_image->file_mode) 760*a72bbec7SEric DeVolder rc = 1; 761*a72bbec7SEric DeVolder else 762*a72bbec7SEric DeVolder rc = kexec_crash_image->update_elfcorehdr; 763*a72bbec7SEric DeVolder } 764*a72bbec7SEric DeVolder /* Release lock now that update complete */ 765*a72bbec7SEric DeVolder kexec_unlock(); 766*a72bbec7SEric DeVolder 767*a72bbec7SEric DeVolder return rc; 768*a72bbec7SEric DeVolder } 769*a72bbec7SEric DeVolder 77024726275SEric DeVolder /* 77124726275SEric DeVolder * To accurately reflect hot un/plug changes of cpu and memory resources 77224726275SEric DeVolder * (including onling and offlining of those resources), the elfcorehdr 77324726275SEric DeVolder * (which is passed to the crash kernel via the elfcorehdr= parameter) 77424726275SEric DeVolder * must be updated with the new list of CPUs and memories. 77524726275SEric DeVolder * 77624726275SEric DeVolder * In order to make changes to elfcorehdr, two conditions are needed: 77724726275SEric DeVolder * First, the segment containing the elfcorehdr must be large enough 77824726275SEric DeVolder * to permit a growing number of resources; the elfcorehdr memory size 77924726275SEric DeVolder * is based on NR_CPUS_DEFAULT and CRASH_MAX_MEMORY_RANGES. 78024726275SEric DeVolder * Second, purgatory must explicitly exclude the elfcorehdr from the 78124726275SEric DeVolder * list of segments it checks (since the elfcorehdr changes and thus 78224726275SEric DeVolder * would require an update to purgatory itself to update the digest). 78324726275SEric DeVolder */ 78424726275SEric DeVolder static void crash_handle_hotplug_event(unsigned int hp_action, unsigned int cpu) 78524726275SEric DeVolder { 78624726275SEric DeVolder struct kimage *image; 78724726275SEric DeVolder 78824726275SEric DeVolder /* Obtain lock while changing crash information */ 78924726275SEric DeVolder if (!kexec_trylock()) { 79024726275SEric DeVolder pr_info("kexec_trylock() failed, elfcorehdr may be inaccurate\n"); 79124726275SEric DeVolder return; 79224726275SEric DeVolder } 79324726275SEric DeVolder 79424726275SEric DeVolder /* Check kdump is not loaded */ 79524726275SEric DeVolder if (!kexec_crash_image) 79624726275SEric DeVolder goto out; 79724726275SEric DeVolder 79824726275SEric DeVolder image = kexec_crash_image; 79924726275SEric DeVolder 800*a72bbec7SEric DeVolder /* Check that updating elfcorehdr is permitted */ 801*a72bbec7SEric DeVolder if (!(image->file_mode || image->update_elfcorehdr)) 802*a72bbec7SEric DeVolder goto out; 803*a72bbec7SEric DeVolder 80424726275SEric DeVolder if (hp_action == KEXEC_CRASH_HP_ADD_CPU || 80524726275SEric DeVolder hp_action == KEXEC_CRASH_HP_REMOVE_CPU) 80624726275SEric DeVolder pr_debug("hp_action %u, cpu %u\n", hp_action, cpu); 80724726275SEric DeVolder else 80824726275SEric DeVolder pr_debug("hp_action %u\n", hp_action); 80924726275SEric DeVolder 81024726275SEric DeVolder /* 81124726275SEric DeVolder * The elfcorehdr_index is set to -1 when the struct kimage 81224726275SEric DeVolder * is allocated. Find the segment containing the elfcorehdr, 81324726275SEric DeVolder * if not already found. 81424726275SEric DeVolder */ 81524726275SEric DeVolder if (image->elfcorehdr_index < 0) { 81624726275SEric DeVolder unsigned long mem; 81724726275SEric DeVolder unsigned char *ptr; 81824726275SEric DeVolder unsigned int n; 81924726275SEric DeVolder 82024726275SEric DeVolder for (n = 0; n < image->nr_segments; n++) { 82124726275SEric DeVolder mem = image->segment[n].mem; 82224726275SEric DeVolder ptr = kmap_local_page(pfn_to_page(mem >> PAGE_SHIFT)); 82324726275SEric DeVolder if (ptr) { 82424726275SEric DeVolder /* The segment containing elfcorehdr */ 82524726275SEric DeVolder if (memcmp(ptr, ELFMAG, SELFMAG) == 0) 82624726275SEric DeVolder image->elfcorehdr_index = (int)n; 82724726275SEric DeVolder kunmap_local(ptr); 82824726275SEric DeVolder } 82924726275SEric DeVolder } 83024726275SEric DeVolder } 83124726275SEric DeVolder 83224726275SEric DeVolder if (image->elfcorehdr_index < 0) { 83324726275SEric DeVolder pr_err("unable to locate elfcorehdr segment"); 83424726275SEric DeVolder goto out; 83524726275SEric DeVolder } 83624726275SEric DeVolder 83724726275SEric DeVolder /* Needed in order for the segments to be updated */ 83824726275SEric DeVolder arch_kexec_unprotect_crashkres(); 83924726275SEric DeVolder 84024726275SEric DeVolder /* Differentiate between normal load and hotplug update */ 84124726275SEric DeVolder image->hp_action = hp_action; 84224726275SEric DeVolder 84324726275SEric DeVolder /* Now invoke arch-specific update handler */ 84424726275SEric DeVolder arch_crash_handle_hotplug_event(image); 84524726275SEric DeVolder 84624726275SEric DeVolder /* No longer handling a hotplug event */ 84724726275SEric DeVolder image->hp_action = KEXEC_CRASH_HP_NONE; 84824726275SEric DeVolder image->elfcorehdr_updated = true; 84924726275SEric DeVolder 85024726275SEric DeVolder /* Change back to read-only */ 85124726275SEric DeVolder arch_kexec_protect_crashkres(); 85224726275SEric DeVolder 85324726275SEric DeVolder /* Errors in the callback is not a reason to rollback state */ 85424726275SEric DeVolder out: 85524726275SEric DeVolder /* Release lock now that update complete */ 85624726275SEric DeVolder kexec_unlock(); 85724726275SEric DeVolder } 85824726275SEric DeVolder 85924726275SEric DeVolder static int crash_memhp_notifier(struct notifier_block *nb, unsigned long val, void *v) 86024726275SEric DeVolder { 86124726275SEric DeVolder switch (val) { 86224726275SEric DeVolder case MEM_ONLINE: 86324726275SEric DeVolder crash_handle_hotplug_event(KEXEC_CRASH_HP_ADD_MEMORY, 86424726275SEric DeVolder KEXEC_CRASH_HP_INVALID_CPU); 86524726275SEric DeVolder break; 86624726275SEric DeVolder 86724726275SEric DeVolder case MEM_OFFLINE: 86824726275SEric DeVolder crash_handle_hotplug_event(KEXEC_CRASH_HP_REMOVE_MEMORY, 86924726275SEric DeVolder KEXEC_CRASH_HP_INVALID_CPU); 87024726275SEric DeVolder break; 87124726275SEric DeVolder } 87224726275SEric DeVolder return NOTIFY_OK; 87324726275SEric DeVolder } 87424726275SEric DeVolder 87524726275SEric DeVolder static struct notifier_block crash_memhp_nb = { 87624726275SEric DeVolder .notifier_call = crash_memhp_notifier, 87724726275SEric DeVolder .priority = 0 87824726275SEric DeVolder }; 87924726275SEric DeVolder 88024726275SEric DeVolder static int crash_cpuhp_online(unsigned int cpu) 88124726275SEric DeVolder { 88224726275SEric DeVolder crash_handle_hotplug_event(KEXEC_CRASH_HP_ADD_CPU, cpu); 88324726275SEric DeVolder return 0; 88424726275SEric DeVolder } 88524726275SEric DeVolder 88624726275SEric DeVolder static int crash_cpuhp_offline(unsigned int cpu) 88724726275SEric DeVolder { 88824726275SEric DeVolder crash_handle_hotplug_event(KEXEC_CRASH_HP_REMOVE_CPU, cpu); 88924726275SEric DeVolder return 0; 89024726275SEric DeVolder } 89124726275SEric DeVolder 89224726275SEric DeVolder static int __init crash_hotplug_init(void) 89324726275SEric DeVolder { 89424726275SEric DeVolder int result = 0; 89524726275SEric DeVolder 89624726275SEric DeVolder if (IS_ENABLED(CONFIG_MEMORY_HOTPLUG)) 89724726275SEric DeVolder register_memory_notifier(&crash_memhp_nb); 89824726275SEric DeVolder 89924726275SEric DeVolder if (IS_ENABLED(CONFIG_HOTPLUG_CPU)) { 90024726275SEric DeVolder result = cpuhp_setup_state_nocalls(CPUHP_BP_PREPARE_DYN, 90124726275SEric DeVolder "crash/cpuhp", crash_cpuhp_online, crash_cpuhp_offline); 90224726275SEric DeVolder } 90324726275SEric DeVolder 90424726275SEric DeVolder return result; 90524726275SEric DeVolder } 90624726275SEric DeVolder 90724726275SEric DeVolder subsys_initcall(crash_hotplug_init); 90824726275SEric DeVolder #endif 909