crash.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 ([email protected])
  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 <[email protected]>
  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/intel_pt.h>
  38. #include <asm/crash.h>
  39. #include <asm/cmdline.h>
  40. /* Used while preparing memory map entries for second kernel */
  41. struct crash_memmap_data {
  42. struct boot_params *params;
  43. /* Type of memory */
  44. unsigned int type;
  45. };
  46. #if defined(CONFIG_SMP) && defined(CONFIG_X86_LOCAL_APIC)
  47. static void kdump_nmi_callback(int cpu, struct pt_regs *regs)
  48. {
  49. crash_save_cpu(regs, cpu);
  50. /*
  51. * Disable Intel PT to stop its logging
  52. */
  53. cpu_emergency_stop_pt();
  54. disable_local_APIC();
  55. }
  56. void kdump_nmi_shootdown_cpus(void)
  57. {
  58. nmi_shootdown_cpus(kdump_nmi_callback);
  59. disable_local_APIC();
  60. }
  61. /* Override the weak function in kernel/panic.c */
  62. void crash_smp_send_stop(void)
  63. {
  64. static int cpus_stopped;
  65. if (cpus_stopped)
  66. return;
  67. if (smp_ops.crash_stop_other_cpus)
  68. smp_ops.crash_stop_other_cpus();
  69. else
  70. smp_send_stop();
  71. cpus_stopped = 1;
  72. }
  73. #else
  74. void crash_smp_send_stop(void)
  75. {
  76. /* There are no cpus to shootdown */
  77. }
  78. #endif
  79. void native_machine_crash_shutdown(struct pt_regs *regs)
  80. {
  81. /* This function is only called after the system
  82. * has panicked or is otherwise in a critical state.
  83. * The minimum amount of code to allow a kexec'd kernel
  84. * to run successfully needs to happen here.
  85. *
  86. * In practice this means shooting down the other cpus in
  87. * an SMP system.
  88. */
  89. /* The kernel is broken so disable interrupts */
  90. local_irq_disable();
  91. crash_smp_send_stop();
  92. cpu_emergency_disable_virtualization();
  93. /*
  94. * Disable Intel PT to stop its logging
  95. */
  96. cpu_emergency_stop_pt();
  97. #ifdef CONFIG_X86_IO_APIC
  98. /* Prevent crash_kexec() from deadlocking on ioapic_lock. */
  99. ioapic_zap_locks();
  100. clear_IO_APIC();
  101. #endif
  102. lapic_shutdown();
  103. restore_boot_irq_mode();
  104. #ifdef CONFIG_HPET_TIMER
  105. hpet_disable();
  106. #endif
  107. crash_save_cpu(regs, safe_smp_processor_id());
  108. }
  109. #ifdef CONFIG_KEXEC_FILE
  110. static int get_nr_ram_ranges_callback(struct resource *res, void *arg)
  111. {
  112. unsigned int *nr_ranges = arg;
  113. (*nr_ranges)++;
  114. return 0;
  115. }
  116. /* Gather all the required information to prepare elf headers for ram regions */
  117. static struct crash_mem *fill_up_crash_elf_data(void)
  118. {
  119. unsigned int nr_ranges = 0;
  120. struct crash_mem *cmem;
  121. walk_system_ram_res(0, -1, &nr_ranges, get_nr_ram_ranges_callback);
  122. if (!nr_ranges)
  123. return NULL;
  124. /*
  125. * Exclusion of crash region and/or crashk_low_res may cause
  126. * another range split. So add extra two slots here.
  127. */
  128. nr_ranges += 2;
  129. cmem = vzalloc(struct_size(cmem, ranges, nr_ranges));
  130. if (!cmem)
  131. return NULL;
  132. cmem->max_nr_ranges = nr_ranges;
  133. cmem->nr_ranges = 0;
  134. return cmem;
  135. }
  136. /*
  137. * Look for any unwanted ranges between mstart, mend and remove them. This
  138. * might lead to split and split ranges are put in cmem->ranges[] array
  139. */
  140. static int elf_header_exclude_ranges(struct crash_mem *cmem)
  141. {
  142. int ret = 0;
  143. /* Exclude the low 1M because it is always reserved */
  144. ret = crash_exclude_mem_range(cmem, 0, (1<<20)-1);
  145. if (ret)
  146. return ret;
  147. /* Exclude crashkernel region */
  148. ret = crash_exclude_mem_range(cmem, crashk_res.start, crashk_res.end);
  149. if (ret)
  150. return ret;
  151. if (crashk_low_res.end)
  152. ret = crash_exclude_mem_range(cmem, crashk_low_res.start,
  153. crashk_low_res.end);
  154. return ret;
  155. }
  156. static int prepare_elf64_ram_headers_callback(struct resource *res, void *arg)
  157. {
  158. struct crash_mem *cmem = arg;
  159. cmem->ranges[cmem->nr_ranges].start = res->start;
  160. cmem->ranges[cmem->nr_ranges].end = res->end;
  161. cmem->nr_ranges++;
  162. return 0;
  163. }
  164. /* Prepare elf headers. Return addr and size */
  165. static int prepare_elf_headers(struct kimage *image, void **addr,
  166. unsigned long *sz)
  167. {
  168. struct crash_mem *cmem;
  169. int ret;
  170. cmem = fill_up_crash_elf_data();
  171. if (!cmem)
  172. return -ENOMEM;
  173. ret = walk_system_ram_res(0, -1, cmem, prepare_elf64_ram_headers_callback);
  174. if (ret)
  175. goto out;
  176. /* Exclude unwanted mem ranges */
  177. ret = elf_header_exclude_ranges(cmem);
  178. if (ret)
  179. goto out;
  180. /* By default prepare 64bit headers */
  181. ret = crash_prepare_elf64_headers(cmem, IS_ENABLED(CONFIG_X86_64), addr, sz);
  182. out:
  183. vfree(cmem);
  184. return ret;
  185. }
  186. static int add_e820_entry(struct boot_params *params, struct e820_entry *entry)
  187. {
  188. unsigned int nr_e820_entries;
  189. nr_e820_entries = params->e820_entries;
  190. if (nr_e820_entries >= E820_MAX_ENTRIES_ZEROPAGE)
  191. return 1;
  192. memcpy(&params->e820_table[nr_e820_entries], entry, sizeof(struct e820_entry));
  193. params->e820_entries++;
  194. return 0;
  195. }
  196. static int memmap_entry_callback(struct resource *res, void *arg)
  197. {
  198. struct crash_memmap_data *cmd = arg;
  199. struct boot_params *params = cmd->params;
  200. struct e820_entry ei;
  201. ei.addr = res->start;
  202. ei.size = resource_size(res);
  203. ei.type = cmd->type;
  204. add_e820_entry(params, &ei);
  205. return 0;
  206. }
  207. static int memmap_exclude_ranges(struct kimage *image, struct crash_mem *cmem,
  208. unsigned long long mstart,
  209. unsigned long long mend)
  210. {
  211. unsigned long start, end;
  212. cmem->ranges[0].start = mstart;
  213. cmem->ranges[0].end = mend;
  214. cmem->nr_ranges = 1;
  215. /* Exclude elf header region */
  216. start = image->elf_load_addr;
  217. end = start + image->elf_headers_sz - 1;
  218. return crash_exclude_mem_range(cmem, start, end);
  219. }
  220. /* Prepare memory map for crash dump kernel */
  221. int crash_setup_memmap_entries(struct kimage *image, struct boot_params *params)
  222. {
  223. int i, ret = 0;
  224. unsigned long flags;
  225. struct e820_entry ei;
  226. struct crash_memmap_data cmd;
  227. struct crash_mem *cmem;
  228. cmem = vzalloc(struct_size(cmem, ranges, 1));
  229. if (!cmem)
  230. return -ENOMEM;
  231. memset(&cmd, 0, sizeof(struct crash_memmap_data));
  232. cmd.params = params;
  233. /* Add the low 1M */
  234. cmd.type = E820_TYPE_RAM;
  235. flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
  236. walk_iomem_res_desc(IORES_DESC_NONE, flags, 0, (1<<20)-1, &cmd,
  237. memmap_entry_callback);
  238. /* Add ACPI tables */
  239. cmd.type = E820_TYPE_ACPI;
  240. flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  241. walk_iomem_res_desc(IORES_DESC_ACPI_TABLES, flags, 0, -1, &cmd,
  242. memmap_entry_callback);
  243. /* Add ACPI Non-volatile Storage */
  244. cmd.type = E820_TYPE_NVS;
  245. walk_iomem_res_desc(IORES_DESC_ACPI_NV_STORAGE, flags, 0, -1, &cmd,
  246. memmap_entry_callback);
  247. /* Add e820 reserved ranges */
  248. cmd.type = E820_TYPE_RESERVED;
  249. flags = IORESOURCE_MEM;
  250. walk_iomem_res_desc(IORES_DESC_RESERVED, flags, 0, -1, &cmd,
  251. memmap_entry_callback);
  252. /* Add crashk_low_res region */
  253. if (crashk_low_res.end) {
  254. ei.addr = crashk_low_res.start;
  255. ei.size = resource_size(&crashk_low_res);
  256. ei.type = E820_TYPE_RAM;
  257. add_e820_entry(params, &ei);
  258. }
  259. /* Exclude some ranges from crashk_res and add rest to memmap */
  260. ret = memmap_exclude_ranges(image, cmem, crashk_res.start, crashk_res.end);
  261. if (ret)
  262. goto out;
  263. for (i = 0; i < cmem->nr_ranges; i++) {
  264. ei.size = cmem->ranges[i].end - cmem->ranges[i].start + 1;
  265. /* If entry is less than a page, skip it */
  266. if (ei.size < PAGE_SIZE)
  267. continue;
  268. ei.addr = cmem->ranges[i].start;
  269. ei.type = E820_TYPE_RAM;
  270. add_e820_entry(params, &ei);
  271. }
  272. out:
  273. vfree(cmem);
  274. return ret;
  275. }
  276. int crash_load_segments(struct kimage *image)
  277. {
  278. int ret;
  279. struct kexec_buf kbuf = { .image = image, .buf_min = 0,
  280. .buf_max = ULONG_MAX, .top_down = false };
  281. /* Prepare elf headers and add a segment */
  282. ret = prepare_elf_headers(image, &kbuf.buffer, &kbuf.bufsz);
  283. if (ret)
  284. return ret;
  285. image->elf_headers = kbuf.buffer;
  286. image->elf_headers_sz = kbuf.bufsz;
  287. kbuf.memsz = kbuf.bufsz;
  288. kbuf.buf_align = ELF_CORE_HEADER_ALIGN;
  289. kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
  290. ret = kexec_add_buffer(&kbuf);
  291. if (ret)
  292. return ret;
  293. image->elf_load_addr = kbuf.mem;
  294. pr_debug("Loaded ELF headers at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
  295. image->elf_load_addr, kbuf.bufsz, kbuf.memsz);
  296. return ret;
  297. }
  298. #endif /* CONFIG_KEXEC_FILE */