crash.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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/processor.h>
  27. #include <asm/hardirq.h>
  28. #include <asm/nmi.h>
  29. #include <asm/hw_irq.h>
  30. #include <asm/apic.h>
  31. #include <asm/e820/types.h>
  32. #include <asm/io_apic.h>
  33. #include <asm/hpet.h>
  34. #include <linux/kdebug.h>
  35. #include <asm/cpu.h>
  36. #include <asm/reboot.h>
  37. #include <asm/virtext.h>
  38. #include <asm/intel_pt.h>
  39. #include <asm/crash.h>
  40. #include <asm/cmdline.h>
  41. /* Used while preparing memory map entries for second kernel */
  42. struct crash_memmap_data {
  43. struct boot_params *params;
  44. /* Type of memory */
  45. unsigned int type;
  46. };
  47. /*
  48. * This is used to VMCLEAR all VMCSs loaded on the
  49. * processor. And when loading kvm_intel module, the
  50. * callback function pointer will be assigned.
  51. *
  52. * protected by rcu.
  53. */
  54. crash_vmclear_fn __rcu *crash_vmclear_loaded_vmcss = NULL;
  55. EXPORT_SYMBOL_GPL(crash_vmclear_loaded_vmcss);
  56. static inline void cpu_crash_vmclear_loaded_vmcss(void)
  57. {
  58. crash_vmclear_fn *do_vmclear_operation = NULL;
  59. rcu_read_lock();
  60. do_vmclear_operation = rcu_dereference(crash_vmclear_loaded_vmcss);
  61. if (do_vmclear_operation)
  62. do_vmclear_operation();
  63. rcu_read_unlock();
  64. }
  65. /*
  66. * When the crashkernel option is specified, only use the low
  67. * 1M for the real mode trampoline.
  68. */
  69. void __init crash_reserve_low_1M(void)
  70. {
  71. if (cmdline_find_option(boot_command_line, "crashkernel", NULL, 0) < 0)
  72. return;
  73. memblock_reserve(0, 1<<20);
  74. pr_info("Reserving the low 1M of memory for crashkernel\n");
  75. }
  76. #if defined(CONFIG_SMP) && defined(CONFIG_X86_LOCAL_APIC)
  77. static void kdump_nmi_callback(int cpu, struct pt_regs *regs)
  78. {
  79. crash_save_cpu(regs, cpu);
  80. /*
  81. * VMCLEAR VMCSs loaded on all cpus if needed.
  82. */
  83. cpu_crash_vmclear_loaded_vmcss();
  84. /* Disable VMX or SVM if needed.
  85. *
  86. * We need to disable virtualization on all CPUs.
  87. * Having VMX or SVM enabled on any CPU may break rebooting
  88. * after the kdump kernel has finished its task.
  89. */
  90. cpu_emergency_vmxoff();
  91. cpu_emergency_svm_disable();
  92. /*
  93. * Disable Intel PT to stop its logging
  94. */
  95. cpu_emergency_stop_pt();
  96. disable_local_APIC();
  97. }
  98. void kdump_nmi_shootdown_cpus(void)
  99. {
  100. nmi_shootdown_cpus(kdump_nmi_callback);
  101. disable_local_APIC();
  102. }
  103. /* Override the weak function in kernel/panic.c */
  104. void crash_smp_send_stop(void)
  105. {
  106. static int cpus_stopped;
  107. if (cpus_stopped)
  108. return;
  109. if (smp_ops.crash_stop_other_cpus)
  110. smp_ops.crash_stop_other_cpus();
  111. else
  112. smp_send_stop();
  113. cpus_stopped = 1;
  114. }
  115. #else
  116. void crash_smp_send_stop(void)
  117. {
  118. /* There are no cpus to shootdown */
  119. }
  120. #endif
  121. void native_machine_crash_shutdown(struct pt_regs *regs)
  122. {
  123. /* This function is only called after the system
  124. * has panicked or is otherwise in a critical state.
  125. * The minimum amount of code to allow a kexec'd kernel
  126. * to run successfully needs to happen here.
  127. *
  128. * In practice this means shooting down the other cpus in
  129. * an SMP system.
  130. */
  131. /* The kernel is broken so disable interrupts */
  132. local_irq_disable();
  133. crash_smp_send_stop();
  134. /*
  135. * VMCLEAR VMCSs loaded on this cpu if needed.
  136. */
  137. cpu_crash_vmclear_loaded_vmcss();
  138. /* Booting kdump kernel with VMX or SVM enabled won't work,
  139. * because (among other limitations) we can't disable paging
  140. * with the virt flags.
  141. */
  142. cpu_emergency_vmxoff();
  143. cpu_emergency_svm_disable();
  144. /*
  145. * Disable Intel PT to stop its logging
  146. */
  147. cpu_emergency_stop_pt();
  148. #ifdef CONFIG_X86_IO_APIC
  149. /* Prevent crash_kexec() from deadlocking on ioapic_lock. */
  150. ioapic_zap_locks();
  151. clear_IO_APIC();
  152. #endif
  153. lapic_shutdown();
  154. restore_boot_irq_mode();
  155. #ifdef CONFIG_HPET_TIMER
  156. hpet_disable();
  157. #endif
  158. crash_save_cpu(regs, safe_smp_processor_id());
  159. }
  160. #ifdef CONFIG_KEXEC_FILE
  161. static int get_nr_ram_ranges_callback(struct resource *res, void *arg)
  162. {
  163. unsigned int *nr_ranges = arg;
  164. (*nr_ranges)++;
  165. return 0;
  166. }
  167. /* Gather all the required information to prepare elf headers for ram regions */
  168. static struct crash_mem *fill_up_crash_elf_data(void)
  169. {
  170. unsigned int nr_ranges = 0;
  171. struct crash_mem *cmem;
  172. walk_system_ram_res(0, -1, &nr_ranges, get_nr_ram_ranges_callback);
  173. if (!nr_ranges)
  174. return NULL;
  175. /*
  176. * Exclusion of crash region and/or crashk_low_res may cause
  177. * another range split. So add extra two slots here.
  178. */
  179. nr_ranges += 2;
  180. cmem = vzalloc(struct_size(cmem, ranges, nr_ranges));
  181. if (!cmem)
  182. return NULL;
  183. cmem->max_nr_ranges = nr_ranges;
  184. cmem->nr_ranges = 0;
  185. return cmem;
  186. }
  187. /*
  188. * Look for any unwanted ranges between mstart, mend and remove them. This
  189. * might lead to split and split ranges are put in cmem->ranges[] array
  190. */
  191. static int elf_header_exclude_ranges(struct crash_mem *cmem)
  192. {
  193. int ret = 0;
  194. /* Exclude the low 1M because it is always reserved */
  195. ret = crash_exclude_mem_range(cmem, 0, (1<<20)-1);
  196. if (ret)
  197. return ret;
  198. /* Exclude crashkernel region */
  199. ret = crash_exclude_mem_range(cmem, crashk_res.start, crashk_res.end);
  200. if (ret)
  201. return ret;
  202. if (crashk_low_res.end)
  203. ret = crash_exclude_mem_range(cmem, crashk_low_res.start,
  204. crashk_low_res.end);
  205. return ret;
  206. }
  207. static int prepare_elf64_ram_headers_callback(struct resource *res, void *arg)
  208. {
  209. struct crash_mem *cmem = arg;
  210. cmem->ranges[cmem->nr_ranges].start = res->start;
  211. cmem->ranges[cmem->nr_ranges].end = res->end;
  212. cmem->nr_ranges++;
  213. return 0;
  214. }
  215. /* Prepare elf headers. Return addr and size */
  216. static int prepare_elf_headers(struct kimage *image, void **addr,
  217. unsigned long *sz)
  218. {
  219. struct crash_mem *cmem;
  220. int ret;
  221. cmem = fill_up_crash_elf_data();
  222. if (!cmem)
  223. return -ENOMEM;
  224. ret = walk_system_ram_res(0, -1, cmem, prepare_elf64_ram_headers_callback);
  225. if (ret)
  226. goto out;
  227. /* Exclude unwanted mem ranges */
  228. ret = elf_header_exclude_ranges(cmem);
  229. if (ret)
  230. goto out;
  231. /* By default prepare 64bit headers */
  232. ret = crash_prepare_elf64_headers(cmem, IS_ENABLED(CONFIG_X86_64), addr, sz);
  233. out:
  234. vfree(cmem);
  235. return ret;
  236. }
  237. static int add_e820_entry(struct boot_params *params, struct e820_entry *entry)
  238. {
  239. unsigned int nr_e820_entries;
  240. nr_e820_entries = params->e820_entries;
  241. if (nr_e820_entries >= E820_MAX_ENTRIES_ZEROPAGE)
  242. return 1;
  243. memcpy(&params->e820_table[nr_e820_entries], entry, sizeof(struct e820_entry));
  244. params->e820_entries++;
  245. return 0;
  246. }
  247. static int memmap_entry_callback(struct resource *res, void *arg)
  248. {
  249. struct crash_memmap_data *cmd = arg;
  250. struct boot_params *params = cmd->params;
  251. struct e820_entry ei;
  252. ei.addr = res->start;
  253. ei.size = resource_size(res);
  254. ei.type = cmd->type;
  255. add_e820_entry(params, &ei);
  256. return 0;
  257. }
  258. static int memmap_exclude_ranges(struct kimage *image, struct crash_mem *cmem,
  259. unsigned long long mstart,
  260. unsigned long long mend)
  261. {
  262. unsigned long start, end;
  263. cmem->ranges[0].start = mstart;
  264. cmem->ranges[0].end = mend;
  265. cmem->nr_ranges = 1;
  266. /* Exclude elf header region */
  267. start = image->arch.elf_load_addr;
  268. end = start + image->arch.elf_headers_sz - 1;
  269. return crash_exclude_mem_range(cmem, start, end);
  270. }
  271. /* Prepare memory map for crash dump kernel */
  272. int crash_setup_memmap_entries(struct kimage *image, struct boot_params *params)
  273. {
  274. int i, ret = 0;
  275. unsigned long flags;
  276. struct e820_entry ei;
  277. struct crash_memmap_data cmd;
  278. struct crash_mem *cmem;
  279. cmem = vzalloc(struct_size(cmem, ranges, 1));
  280. if (!cmem)
  281. return -ENOMEM;
  282. memset(&cmd, 0, sizeof(struct crash_memmap_data));
  283. cmd.params = params;
  284. /* Add the low 1M */
  285. cmd.type = E820_TYPE_RAM;
  286. flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
  287. walk_iomem_res_desc(IORES_DESC_NONE, flags, 0, (1<<20)-1, &cmd,
  288. memmap_entry_callback);
  289. /* Add ACPI tables */
  290. cmd.type = E820_TYPE_ACPI;
  291. flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  292. walk_iomem_res_desc(IORES_DESC_ACPI_TABLES, flags, 0, -1, &cmd,
  293. memmap_entry_callback);
  294. /* Add ACPI Non-volatile Storage */
  295. cmd.type = E820_TYPE_NVS;
  296. walk_iomem_res_desc(IORES_DESC_ACPI_NV_STORAGE, flags, 0, -1, &cmd,
  297. memmap_entry_callback);
  298. /* Add e820 reserved ranges */
  299. cmd.type = E820_TYPE_RESERVED;
  300. flags = IORESOURCE_MEM;
  301. walk_iomem_res_desc(IORES_DESC_RESERVED, flags, 0, -1, &cmd,
  302. memmap_entry_callback);
  303. /* Add crashk_low_res region */
  304. if (crashk_low_res.end) {
  305. ei.addr = crashk_low_res.start;
  306. ei.size = resource_size(&crashk_low_res);
  307. ei.type = E820_TYPE_RAM;
  308. add_e820_entry(params, &ei);
  309. }
  310. /* Exclude some ranges from crashk_res and add rest to memmap */
  311. ret = memmap_exclude_ranges(image, cmem, crashk_res.start, crashk_res.end);
  312. if (ret)
  313. goto out;
  314. for (i = 0; i < cmem->nr_ranges; i++) {
  315. ei.size = cmem->ranges[i].end - cmem->ranges[i].start + 1;
  316. /* If entry is less than a page, skip it */
  317. if (ei.size < PAGE_SIZE)
  318. continue;
  319. ei.addr = cmem->ranges[i].start;
  320. ei.type = E820_TYPE_RAM;
  321. add_e820_entry(params, &ei);
  322. }
  323. out:
  324. vfree(cmem);
  325. return ret;
  326. }
  327. int crash_load_segments(struct kimage *image)
  328. {
  329. int ret;
  330. struct kexec_buf kbuf = { .image = image, .buf_min = 0,
  331. .buf_max = ULONG_MAX, .top_down = false };
  332. /* Prepare elf headers and add a segment */
  333. ret = prepare_elf_headers(image, &kbuf.buffer, &kbuf.bufsz);
  334. if (ret)
  335. return ret;
  336. image->arch.elf_headers = kbuf.buffer;
  337. image->arch.elf_headers_sz = kbuf.bufsz;
  338. kbuf.memsz = kbuf.bufsz;
  339. kbuf.buf_align = ELF_CORE_HEADER_ALIGN;
  340. kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
  341. ret = kexec_add_buffer(&kbuf);
  342. if (ret) {
  343. vfree((void *)image->arch.elf_headers);
  344. return ret;
  345. }
  346. image->arch.elf_load_addr = kbuf.mem;
  347. pr_debug("Loaded ELF headers at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
  348. image->arch.elf_load_addr, kbuf.bufsz, kbuf.bufsz);
  349. return ret;
  350. }
  351. #endif /* CONFIG_KEXEC_FILE */