crash.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Architecture specific (i386/x86_64) functions for kexec based crash dumps.
  4. *
  5. * Created by: Hariprasad Nellitheertha (hari@in.ibm.com)
  6. *
  7. * Copyright (C) IBM Corporation, 2004. All rights reserved.
  8. * Copyright (C) Red Hat Inc., 2014. All rights reserved.
  9. * Authors:
  10. * Vivek Goyal <vgoyal@redhat.com>
  11. *
  12. */
  13. #define pr_fmt(fmt) "kexec: " fmt
  14. #include <linux/types.h>
  15. #include <linux/kernel.h>
  16. #include <linux/smp.h>
  17. #include <linux/reboot.h>
  18. #include <linux/kexec.h>
  19. #include <linux/delay.h>
  20. #include <linux/elf.h>
  21. #include <linux/elfcore.h>
  22. #include <linux/export.h>
  23. #include <linux/slab.h>
  24. #include <linux/vmalloc.h>
  25. #include <linux/memblock.h>
  26. #include <asm/bootparam.h>
  27. #include <asm/processor.h>
  28. #include <asm/hardirq.h>
  29. #include <asm/nmi.h>
  30. #include <asm/hw_irq.h>
  31. #include <asm/apic.h>
  32. #include <asm/e820/types.h>
  33. #include <asm/io_apic.h>
  34. #include <asm/hpet.h>
  35. #include <linux/kdebug.h>
  36. #include <asm/cpu.h>
  37. #include <asm/reboot.h>
  38. #include <asm/intel_pt.h>
  39. #include <asm/crash.h>
  40. #include <asm/cmdline.h>
  41. #include <asm/sev.h>
  42. /* Used while preparing memory map entries for second kernel */
  43. struct crash_memmap_data {
  44. struct boot_params *params;
  45. /* Type of memory */
  46. unsigned int type;
  47. };
  48. #if defined(CONFIG_SMP) && defined(CONFIG_X86_LOCAL_APIC)
  49. static void kdump_nmi_callback(int cpu, struct pt_regs *regs)
  50. {
  51. crash_save_cpu(regs, cpu);
  52. /*
  53. * Disable Intel PT to stop its logging
  54. */
  55. cpu_emergency_stop_pt();
  56. kdump_sev_callback();
  57. disable_local_APIC();
  58. }
  59. void kdump_nmi_shootdown_cpus(void)
  60. {
  61. nmi_shootdown_cpus(kdump_nmi_callback);
  62. disable_local_APIC();
  63. }
  64. /* Override the weak function in kernel/panic.c */
  65. void crash_smp_send_stop(void)
  66. {
  67. static int cpus_stopped;
  68. if (cpus_stopped)
  69. return;
  70. if (smp_ops.crash_stop_other_cpus)
  71. smp_ops.crash_stop_other_cpus();
  72. else
  73. smp_send_stop();
  74. cpus_stopped = 1;
  75. }
  76. #else
  77. void crash_smp_send_stop(void)
  78. {
  79. /* There are no cpus to shootdown */
  80. }
  81. #endif
  82. void native_machine_crash_shutdown(struct pt_regs *regs)
  83. {
  84. /* This function is only called after the system
  85. * has panicked or is otherwise in a critical state.
  86. * The minimum amount of code to allow a kexec'd kernel
  87. * to run successfully needs to happen here.
  88. *
  89. * In practice this means shooting down the other cpus in
  90. * an SMP system.
  91. */
  92. /* The kernel is broken so disable interrupts */
  93. local_irq_disable();
  94. crash_smp_send_stop();
  95. cpu_emergency_disable_virtualization();
  96. /*
  97. * Disable Intel PT to stop its logging
  98. */
  99. cpu_emergency_stop_pt();
  100. #ifdef CONFIG_X86_IO_APIC
  101. /* Prevent crash_kexec() from deadlocking on ioapic_lock. */
  102. ioapic_zap_locks();
  103. clear_IO_APIC();
  104. #endif
  105. lapic_shutdown();
  106. restore_boot_irq_mode();
  107. #ifdef CONFIG_HPET_TIMER
  108. hpet_disable();
  109. #endif
  110. /*
  111. * Non-crash kexec calls enc_kexec_begin() while scheduling is still
  112. * active. This allows the callback to wait until all in-flight
  113. * shared<->private conversions are complete. In a crash scenario,
  114. * enc_kexec_begin() gets called after all but one CPU have been shut
  115. * down and interrupts have been disabled. This allows the callback to
  116. * detect a race with the conversion and report it.
  117. */
  118. x86_platform.guest.enc_kexec_begin();
  119. x86_platform.guest.enc_kexec_finish();
  120. crash_save_cpu(regs, safe_smp_processor_id());
  121. }
  122. #if defined(CONFIG_KEXEC_FILE) || defined(CONFIG_CRASH_HOTPLUG)
  123. static int get_nr_ram_ranges_callback(struct resource *res, void *arg)
  124. {
  125. unsigned int *nr_ranges = arg;
  126. (*nr_ranges)++;
  127. return 0;
  128. }
  129. /* Gather all the required information to prepare elf headers for ram regions */
  130. static struct crash_mem *fill_up_crash_elf_data(void)
  131. {
  132. unsigned int nr_ranges = 0;
  133. struct crash_mem *cmem;
  134. walk_system_ram_res(0, -1, &nr_ranges, get_nr_ram_ranges_callback);
  135. if (!nr_ranges)
  136. return NULL;
  137. /*
  138. * Exclusion of crash region and/or crashk_low_res may cause
  139. * another range split. So add extra two slots here.
  140. */
  141. nr_ranges += 2;
  142. cmem = vzalloc(struct_size(cmem, ranges, nr_ranges));
  143. if (!cmem)
  144. return NULL;
  145. cmem->max_nr_ranges = nr_ranges;
  146. cmem->nr_ranges = 0;
  147. return cmem;
  148. }
  149. /*
  150. * Look for any unwanted ranges between mstart, mend and remove them. This
  151. * might lead to split and split ranges are put in cmem->ranges[] array
  152. */
  153. static int elf_header_exclude_ranges(struct crash_mem *cmem)
  154. {
  155. int ret = 0;
  156. /* Exclude the low 1M because it is always reserved */
  157. ret = crash_exclude_mem_range(cmem, 0, SZ_1M - 1);
  158. if (ret)
  159. return ret;
  160. /* Exclude crashkernel region */
  161. ret = crash_exclude_mem_range(cmem, crashk_res.start, crashk_res.end);
  162. if (ret)
  163. return ret;
  164. if (crashk_low_res.end)
  165. ret = crash_exclude_mem_range(cmem, crashk_low_res.start,
  166. crashk_low_res.end);
  167. return ret;
  168. }
  169. static int prepare_elf64_ram_headers_callback(struct resource *res, void *arg)
  170. {
  171. struct crash_mem *cmem = arg;
  172. cmem->ranges[cmem->nr_ranges].start = res->start;
  173. cmem->ranges[cmem->nr_ranges].end = res->end;
  174. cmem->nr_ranges++;
  175. return 0;
  176. }
  177. /* Prepare elf headers. Return addr and size */
  178. static int prepare_elf_headers(void **addr, unsigned long *sz,
  179. unsigned long *nr_mem_ranges)
  180. {
  181. struct crash_mem *cmem;
  182. int ret;
  183. cmem = fill_up_crash_elf_data();
  184. if (!cmem)
  185. return -ENOMEM;
  186. ret = walk_system_ram_res(0, -1, cmem, prepare_elf64_ram_headers_callback);
  187. if (ret)
  188. goto out;
  189. /* Exclude unwanted mem ranges */
  190. ret = elf_header_exclude_ranges(cmem);
  191. if (ret)
  192. goto out;
  193. /* Return the computed number of memory ranges, for hotplug usage */
  194. *nr_mem_ranges = cmem->nr_ranges;
  195. /* By default prepare 64bit headers */
  196. ret = crash_prepare_elf64_headers(cmem, IS_ENABLED(CONFIG_X86_64), addr, sz);
  197. out:
  198. vfree(cmem);
  199. return ret;
  200. }
  201. #endif
  202. #ifdef CONFIG_KEXEC_FILE
  203. static int add_e820_entry(struct boot_params *params, struct e820_entry *entry)
  204. {
  205. unsigned int nr_e820_entries;
  206. nr_e820_entries = params->e820_entries;
  207. if (nr_e820_entries >= E820_MAX_ENTRIES_ZEROPAGE)
  208. return 1;
  209. memcpy(&params->e820_table[nr_e820_entries], entry, sizeof(struct e820_entry));
  210. params->e820_entries++;
  211. return 0;
  212. }
  213. static int memmap_entry_callback(struct resource *res, void *arg)
  214. {
  215. struct crash_memmap_data *cmd = arg;
  216. struct boot_params *params = cmd->params;
  217. struct e820_entry ei;
  218. ei.addr = res->start;
  219. ei.size = resource_size(res);
  220. ei.type = cmd->type;
  221. add_e820_entry(params, &ei);
  222. return 0;
  223. }
  224. static int memmap_exclude_ranges(struct kimage *image, struct crash_mem *cmem,
  225. unsigned long long mstart,
  226. unsigned long long mend)
  227. {
  228. unsigned long start, end;
  229. cmem->ranges[0].start = mstart;
  230. cmem->ranges[0].end = mend;
  231. cmem->nr_ranges = 1;
  232. /* Exclude elf header region */
  233. start = image->elf_load_addr;
  234. end = start + image->elf_headers_sz - 1;
  235. return crash_exclude_mem_range(cmem, start, end);
  236. }
  237. /* Prepare memory map for crash dump kernel */
  238. int crash_setup_memmap_entries(struct kimage *image, struct boot_params *params)
  239. {
  240. int i, ret = 0;
  241. unsigned long flags;
  242. struct e820_entry ei;
  243. struct crash_memmap_data cmd;
  244. struct crash_mem *cmem;
  245. cmem = vzalloc(struct_size(cmem, ranges, 1));
  246. if (!cmem)
  247. return -ENOMEM;
  248. memset(&cmd, 0, sizeof(struct crash_memmap_data));
  249. cmd.params = params;
  250. /* Add the low 1M */
  251. cmd.type = E820_TYPE_RAM;
  252. flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
  253. walk_iomem_res_desc(IORES_DESC_NONE, flags, 0, (1<<20)-1, &cmd,
  254. memmap_entry_callback);
  255. /* Add ACPI tables */
  256. cmd.type = E820_TYPE_ACPI;
  257. flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  258. walk_iomem_res_desc(IORES_DESC_ACPI_TABLES, flags, 0, -1, &cmd,
  259. memmap_entry_callback);
  260. /* Add ACPI Non-volatile Storage */
  261. cmd.type = E820_TYPE_NVS;
  262. walk_iomem_res_desc(IORES_DESC_ACPI_NV_STORAGE, flags, 0, -1, &cmd,
  263. memmap_entry_callback);
  264. /* Add e820 reserved ranges */
  265. cmd.type = E820_TYPE_RESERVED;
  266. flags = IORESOURCE_MEM;
  267. walk_iomem_res_desc(IORES_DESC_RESERVED, flags, 0, -1, &cmd,
  268. memmap_entry_callback);
  269. /* Add crashk_low_res region */
  270. if (crashk_low_res.end) {
  271. ei.addr = crashk_low_res.start;
  272. ei.size = resource_size(&crashk_low_res);
  273. ei.type = E820_TYPE_RAM;
  274. add_e820_entry(params, &ei);
  275. }
  276. /* Exclude some ranges from crashk_res and add rest to memmap */
  277. ret = memmap_exclude_ranges(image, cmem, crashk_res.start, crashk_res.end);
  278. if (ret)
  279. goto out;
  280. for (i = 0; i < cmem->nr_ranges; i++) {
  281. ei.size = cmem->ranges[i].end - cmem->ranges[i].start + 1;
  282. /* If entry is less than a page, skip it */
  283. if (ei.size < PAGE_SIZE)
  284. continue;
  285. ei.addr = cmem->ranges[i].start;
  286. ei.type = E820_TYPE_RAM;
  287. add_e820_entry(params, &ei);
  288. }
  289. out:
  290. vfree(cmem);
  291. return ret;
  292. }
  293. int crash_load_segments(struct kimage *image)
  294. {
  295. int ret;
  296. unsigned long pnum = 0;
  297. struct kexec_buf kbuf = { .image = image, .buf_min = 0,
  298. .buf_max = ULONG_MAX, .top_down = false };
  299. /* Prepare elf headers and add a segment */
  300. ret = prepare_elf_headers(&kbuf.buffer, &kbuf.bufsz, &pnum);
  301. if (ret)
  302. return ret;
  303. image->elf_headers = kbuf.buffer;
  304. image->elf_headers_sz = kbuf.bufsz;
  305. kbuf.memsz = kbuf.bufsz;
  306. #ifdef CONFIG_CRASH_HOTPLUG
  307. /*
  308. * The elfcorehdr segment size accounts for VMCOREINFO, kernel_map,
  309. * maximum CPUs and maximum memory ranges.
  310. */
  311. if (IS_ENABLED(CONFIG_MEMORY_HOTPLUG))
  312. pnum = 2 + CONFIG_NR_CPUS_DEFAULT + CONFIG_CRASH_MAX_MEMORY_RANGES;
  313. else
  314. pnum += 2 + CONFIG_NR_CPUS_DEFAULT;
  315. if (pnum < (unsigned long)PN_XNUM) {
  316. kbuf.memsz = pnum * sizeof(Elf64_Phdr);
  317. kbuf.memsz += sizeof(Elf64_Ehdr);
  318. image->elfcorehdr_index = image->nr_segments;
  319. /* Mark as usable to crash kernel, else crash kernel fails on boot */
  320. image->elf_headers_sz = kbuf.memsz;
  321. } else {
  322. pr_err("number of Phdrs %lu exceeds max\n", pnum);
  323. }
  324. #endif
  325. kbuf.buf_align = ELF_CORE_HEADER_ALIGN;
  326. kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
  327. ret = kexec_add_buffer(&kbuf);
  328. if (ret)
  329. return ret;
  330. image->elf_load_addr = kbuf.mem;
  331. kexec_dprintk("Loaded ELF headers at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
  332. image->elf_load_addr, kbuf.bufsz, kbuf.memsz);
  333. return ret;
  334. }
  335. #endif /* CONFIG_KEXEC_FILE */
  336. #ifdef CONFIG_CRASH_HOTPLUG
  337. #undef pr_fmt
  338. #define pr_fmt(fmt) "crash hp: " fmt
  339. int arch_crash_hotplug_support(struct kimage *image, unsigned long kexec_flags)
  340. {
  341. #ifdef CONFIG_KEXEC_FILE
  342. if (image->file_mode)
  343. return 1;
  344. #endif
  345. /*
  346. * Initially, crash hotplug support for kexec_load was added
  347. * with the KEXEC_UPDATE_ELFCOREHDR flag. Later, this
  348. * functionality was expanded to accommodate multiple kexec
  349. * segment updates, leading to the introduction of the
  350. * KEXEC_CRASH_HOTPLUG_SUPPORT kexec flag bit. Consequently,
  351. * when the kexec tool sends either of these flags, it indicates
  352. * that the required kexec segment (elfcorehdr) is excluded from
  353. * the SHA calculation.
  354. */
  355. return (kexec_flags & KEXEC_UPDATE_ELFCOREHDR ||
  356. kexec_flags & KEXEC_CRASH_HOTPLUG_SUPPORT);
  357. }
  358. unsigned int arch_crash_get_elfcorehdr_size(void)
  359. {
  360. unsigned int sz;
  361. /* kernel_map, VMCOREINFO and maximum CPUs */
  362. sz = 2 + CONFIG_NR_CPUS_DEFAULT;
  363. if (IS_ENABLED(CONFIG_MEMORY_HOTPLUG))
  364. sz += CONFIG_CRASH_MAX_MEMORY_RANGES;
  365. sz *= sizeof(Elf64_Phdr);
  366. return sz;
  367. }
  368. /**
  369. * arch_crash_handle_hotplug_event() - Handle hotplug elfcorehdr changes
  370. * @image: a pointer to kexec_crash_image
  371. * @arg: struct memory_notify handler for memory hotplug case and
  372. * NULL for CPU hotplug case.
  373. *
  374. * Prepare the new elfcorehdr and replace the existing elfcorehdr.
  375. */
  376. void arch_crash_handle_hotplug_event(struct kimage *image, void *arg)
  377. {
  378. void *elfbuf = NULL, *old_elfcorehdr;
  379. unsigned long nr_mem_ranges;
  380. unsigned long mem, memsz;
  381. unsigned long elfsz = 0;
  382. /*
  383. * As crash_prepare_elf64_headers() has already described all
  384. * possible CPUs, there is no need to update the elfcorehdr
  385. * for additional CPU changes.
  386. */
  387. if ((image->file_mode || image->elfcorehdr_updated) &&
  388. ((image->hp_action == KEXEC_CRASH_HP_ADD_CPU) ||
  389. (image->hp_action == KEXEC_CRASH_HP_REMOVE_CPU)))
  390. return;
  391. /*
  392. * Create the new elfcorehdr reflecting the changes to CPU and/or
  393. * memory resources.
  394. */
  395. if (prepare_elf_headers(&elfbuf, &elfsz, &nr_mem_ranges)) {
  396. pr_err("unable to create new elfcorehdr");
  397. goto out;
  398. }
  399. /*
  400. * Obtain address and size of the elfcorehdr segment, and
  401. * check it against the new elfcorehdr buffer.
  402. */
  403. mem = image->segment[image->elfcorehdr_index].mem;
  404. memsz = image->segment[image->elfcorehdr_index].memsz;
  405. if (elfsz > memsz) {
  406. pr_err("update elfcorehdr elfsz %lu > memsz %lu",
  407. elfsz, memsz);
  408. goto out;
  409. }
  410. /*
  411. * Copy new elfcorehdr over the old elfcorehdr at destination.
  412. */
  413. old_elfcorehdr = kmap_local_page(pfn_to_page(mem >> PAGE_SHIFT));
  414. if (!old_elfcorehdr) {
  415. pr_err("mapping elfcorehdr segment failed\n");
  416. goto out;
  417. }
  418. /*
  419. * Temporarily invalidate the crash image while the
  420. * elfcorehdr is updated.
  421. */
  422. xchg(&kexec_crash_image, NULL);
  423. memcpy_flushcache(old_elfcorehdr, elfbuf, elfsz);
  424. xchg(&kexec_crash_image, image);
  425. kunmap_local(old_elfcorehdr);
  426. pr_debug("updated elfcorehdr\n");
  427. out:
  428. vfree(elfbuf);
  429. }
  430. #endif