crash.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. /*
  2. * Architecture specific (i386/x86_64) functions for kexec based crash dumps.
  3. *
  4. * Created by: Hariprasad Nellitheertha (hari@in.ibm.com)
  5. *
  6. * Copyright (C) IBM Corporation, 2004. All rights reserved.
  7. * Copyright (C) Red Hat Inc., 2014. All rights reserved.
  8. * Authors:
  9. * Vivek Goyal <vgoyal@redhat.com>
  10. *
  11. */
  12. #define pr_fmt(fmt) "kexec: " fmt
  13. #include <linux/types.h>
  14. #include <linux/kernel.h>
  15. #include <linux/smp.h>
  16. #include <linux/reboot.h>
  17. #include <linux/kexec.h>
  18. #include <linux/delay.h>
  19. #include <linux/elf.h>
  20. #include <linux/elfcore.h>
  21. #include <linux/module.h>
  22. #include <linux/slab.h>
  23. #include <linux/vmalloc.h>
  24. #include <asm/processor.h>
  25. #include <asm/hardirq.h>
  26. #include <asm/nmi.h>
  27. #include <asm/hw_irq.h>
  28. #include <asm/apic.h>
  29. #include <asm/io_apic.h>
  30. #include <asm/hpet.h>
  31. #include <linux/kdebug.h>
  32. #include <asm/cpu.h>
  33. #include <asm/reboot.h>
  34. #include <asm/virtext.h>
  35. /* Alignment required for elf header segment */
  36. #define ELF_CORE_HEADER_ALIGN 4096
  37. /* This primarily represents number of split ranges due to exclusion */
  38. #define CRASH_MAX_RANGES 16
  39. struct crash_mem_range {
  40. u64 start, end;
  41. };
  42. struct crash_mem {
  43. unsigned int nr_ranges;
  44. struct crash_mem_range ranges[CRASH_MAX_RANGES];
  45. };
  46. /* Misc data about ram ranges needed to prepare elf headers */
  47. struct crash_elf_data {
  48. struct kimage *image;
  49. /*
  50. * Total number of ram ranges we have after various adjustments for
  51. * GART, crash reserved region etc.
  52. */
  53. unsigned int max_nr_ranges;
  54. unsigned long gart_start, gart_end;
  55. /* Pointer to elf header */
  56. void *ehdr;
  57. /* Pointer to next phdr */
  58. void *bufp;
  59. struct crash_mem mem;
  60. };
  61. /* Used while preparing memory map entries for second kernel */
  62. struct crash_memmap_data {
  63. struct boot_params *params;
  64. /* Type of memory */
  65. unsigned int type;
  66. };
  67. int in_crash_kexec;
  68. /*
  69. * This is used to VMCLEAR all VMCSs loaded on the
  70. * processor. And when loading kvm_intel module, the
  71. * callback function pointer will be assigned.
  72. *
  73. * protected by rcu.
  74. */
  75. crash_vmclear_fn __rcu *crash_vmclear_loaded_vmcss = NULL;
  76. EXPORT_SYMBOL_GPL(crash_vmclear_loaded_vmcss);
  77. unsigned long crash_zero_bytes;
  78. static inline void cpu_crash_vmclear_loaded_vmcss(void)
  79. {
  80. crash_vmclear_fn *do_vmclear_operation = NULL;
  81. rcu_read_lock();
  82. do_vmclear_operation = rcu_dereference(crash_vmclear_loaded_vmcss);
  83. if (do_vmclear_operation)
  84. do_vmclear_operation();
  85. rcu_read_unlock();
  86. }
  87. #if defined(CONFIG_SMP) && defined(CONFIG_X86_LOCAL_APIC)
  88. static void kdump_nmi_callback(int cpu, struct pt_regs *regs)
  89. {
  90. #ifdef CONFIG_X86_32
  91. struct pt_regs fixed_regs;
  92. if (!user_mode(regs)) {
  93. crash_fixup_ss_esp(&fixed_regs, regs);
  94. regs = &fixed_regs;
  95. }
  96. #endif
  97. crash_save_cpu(regs, cpu);
  98. /*
  99. * VMCLEAR VMCSs loaded on all cpus if needed.
  100. */
  101. cpu_crash_vmclear_loaded_vmcss();
  102. /* Disable VMX or SVM if needed.
  103. *
  104. * We need to disable virtualization on all CPUs.
  105. * Having VMX or SVM enabled on any CPU may break rebooting
  106. * after the kdump kernel has finished its task.
  107. */
  108. cpu_emergency_vmxoff();
  109. cpu_emergency_svm_disable();
  110. disable_local_APIC();
  111. }
  112. static void kdump_nmi_shootdown_cpus(void)
  113. {
  114. in_crash_kexec = 1;
  115. nmi_shootdown_cpus(kdump_nmi_callback);
  116. disable_local_APIC();
  117. }
  118. #else
  119. static void kdump_nmi_shootdown_cpus(void)
  120. {
  121. /* There are no cpus to shootdown */
  122. }
  123. #endif
  124. void native_machine_crash_shutdown(struct pt_regs *regs)
  125. {
  126. /* This function is only called after the system
  127. * has panicked or is otherwise in a critical state.
  128. * The minimum amount of code to allow a kexec'd kernel
  129. * to run successfully needs to happen here.
  130. *
  131. * In practice this means shooting down the other cpus in
  132. * an SMP system.
  133. */
  134. /* The kernel is broken so disable interrupts */
  135. local_irq_disable();
  136. kdump_nmi_shootdown_cpus();
  137. /*
  138. * VMCLEAR VMCSs loaded on this cpu if needed.
  139. */
  140. cpu_crash_vmclear_loaded_vmcss();
  141. /* Booting kdump kernel with VMX or SVM enabled won't work,
  142. * because (among other limitations) we can't disable paging
  143. * with the virt flags.
  144. */
  145. cpu_emergency_vmxoff();
  146. cpu_emergency_svm_disable();
  147. #ifdef CONFIG_X86_IO_APIC
  148. /* Prevent crash_kexec() from deadlocking on ioapic_lock. */
  149. ioapic_zap_locks();
  150. disable_IO_APIC();
  151. #endif
  152. lapic_shutdown();
  153. #ifdef CONFIG_HPET_TIMER
  154. hpet_disable();
  155. #endif
  156. crash_save_cpu(regs, safe_smp_processor_id());
  157. }
  158. #ifdef CONFIG_KEXEC_FILE
  159. static int get_nr_ram_ranges_callback(u64 start, u64 end, void *arg)
  160. {
  161. unsigned int *nr_ranges = arg;
  162. (*nr_ranges)++;
  163. return 0;
  164. }
  165. static int get_gart_ranges_callback(u64 start, u64 end, void *arg)
  166. {
  167. struct crash_elf_data *ced = arg;
  168. ced->gart_start = start;
  169. ced->gart_end = end;
  170. /* Not expecting more than 1 gart aperture */
  171. return 1;
  172. }
  173. /* Gather all the required information to prepare elf headers for ram regions */
  174. static void fill_up_crash_elf_data(struct crash_elf_data *ced,
  175. struct kimage *image)
  176. {
  177. unsigned int nr_ranges = 0;
  178. ced->image = image;
  179. walk_system_ram_res(0, -1, &nr_ranges,
  180. get_nr_ram_ranges_callback);
  181. ced->max_nr_ranges = nr_ranges;
  182. /*
  183. * We don't create ELF headers for GART aperture as an attempt
  184. * to dump this memory in second kernel leads to hang/crash.
  185. * If gart aperture is present, one needs to exclude that region
  186. * and that could lead to need of extra phdr.
  187. */
  188. walk_iomem_res("GART", IORESOURCE_MEM, 0, -1,
  189. ced, get_gart_ranges_callback);
  190. /*
  191. * If we have gart region, excluding that could potentially split
  192. * a memory range, resulting in extra header. Account for that.
  193. */
  194. if (ced->gart_end)
  195. ced->max_nr_ranges++;
  196. /* Exclusion of crash region could split memory ranges */
  197. ced->max_nr_ranges++;
  198. /* If crashk_low_res is not 0, another range split possible */
  199. if (crashk_low_res.end)
  200. ced->max_nr_ranges++;
  201. }
  202. static int exclude_mem_range(struct crash_mem *mem,
  203. unsigned long long mstart, unsigned long long mend)
  204. {
  205. int i, j;
  206. unsigned long long start, end;
  207. struct crash_mem_range temp_range = {0, 0};
  208. for (i = 0; i < mem->nr_ranges; i++) {
  209. start = mem->ranges[i].start;
  210. end = mem->ranges[i].end;
  211. if (mstart > end || mend < start)
  212. continue;
  213. /* Truncate any area outside of range */
  214. if (mstart < start)
  215. mstart = start;
  216. if (mend > end)
  217. mend = end;
  218. /* Found completely overlapping range */
  219. if (mstart == start && mend == end) {
  220. mem->ranges[i].start = 0;
  221. mem->ranges[i].end = 0;
  222. if (i < mem->nr_ranges - 1) {
  223. /* Shift rest of the ranges to left */
  224. for (j = i; j < mem->nr_ranges - 1; j++) {
  225. mem->ranges[j].start =
  226. mem->ranges[j+1].start;
  227. mem->ranges[j].end =
  228. mem->ranges[j+1].end;
  229. }
  230. }
  231. mem->nr_ranges--;
  232. return 0;
  233. }
  234. if (mstart > start && mend < end) {
  235. /* Split original range */
  236. mem->ranges[i].end = mstart - 1;
  237. temp_range.start = mend + 1;
  238. temp_range.end = end;
  239. } else if (mstart != start)
  240. mem->ranges[i].end = mstart - 1;
  241. else
  242. mem->ranges[i].start = mend + 1;
  243. break;
  244. }
  245. /* If a split happend, add the split to array */
  246. if (!temp_range.end)
  247. return 0;
  248. /* Split happened */
  249. if (i == CRASH_MAX_RANGES - 1) {
  250. pr_err("Too many crash ranges after split\n");
  251. return -ENOMEM;
  252. }
  253. /* Location where new range should go */
  254. j = i + 1;
  255. if (j < mem->nr_ranges) {
  256. /* Move over all ranges one slot towards the end */
  257. for (i = mem->nr_ranges - 1; i >= j; i--)
  258. mem->ranges[i + 1] = mem->ranges[i];
  259. }
  260. mem->ranges[j].start = temp_range.start;
  261. mem->ranges[j].end = temp_range.end;
  262. mem->nr_ranges++;
  263. return 0;
  264. }
  265. /*
  266. * Look for any unwanted ranges between mstart, mend and remove them. This
  267. * might lead to split and split ranges are put in ced->mem.ranges[] array
  268. */
  269. static int elf_header_exclude_ranges(struct crash_elf_data *ced,
  270. unsigned long long mstart, unsigned long long mend)
  271. {
  272. struct crash_mem *cmem = &ced->mem;
  273. int ret = 0;
  274. memset(cmem->ranges, 0, sizeof(cmem->ranges));
  275. cmem->ranges[0].start = mstart;
  276. cmem->ranges[0].end = mend;
  277. cmem->nr_ranges = 1;
  278. /* Exclude crashkernel region */
  279. ret = exclude_mem_range(cmem, crashk_res.start, crashk_res.end);
  280. if (ret)
  281. return ret;
  282. if (crashk_low_res.end) {
  283. ret = exclude_mem_range(cmem, crashk_low_res.start, crashk_low_res.end);
  284. if (ret)
  285. return ret;
  286. }
  287. /* Exclude GART region */
  288. if (ced->gart_end) {
  289. ret = exclude_mem_range(cmem, ced->gart_start, ced->gart_end);
  290. if (ret)
  291. return ret;
  292. }
  293. return ret;
  294. }
  295. static int prepare_elf64_ram_headers_callback(u64 start, u64 end, void *arg)
  296. {
  297. struct crash_elf_data *ced = arg;
  298. Elf64_Ehdr *ehdr;
  299. Elf64_Phdr *phdr;
  300. unsigned long mstart, mend;
  301. struct kimage *image = ced->image;
  302. struct crash_mem *cmem;
  303. int ret, i;
  304. ehdr = ced->ehdr;
  305. /* Exclude unwanted mem ranges */
  306. ret = elf_header_exclude_ranges(ced, start, end);
  307. if (ret)
  308. return ret;
  309. /* Go through all the ranges in ced->mem.ranges[] and prepare phdr */
  310. cmem = &ced->mem;
  311. for (i = 0; i < cmem->nr_ranges; i++) {
  312. mstart = cmem->ranges[i].start;
  313. mend = cmem->ranges[i].end;
  314. phdr = ced->bufp;
  315. ced->bufp += sizeof(Elf64_Phdr);
  316. phdr->p_type = PT_LOAD;
  317. phdr->p_flags = PF_R|PF_W|PF_X;
  318. phdr->p_offset = mstart;
  319. /*
  320. * If a range matches backup region, adjust offset to backup
  321. * segment.
  322. */
  323. if (mstart == image->arch.backup_src_start &&
  324. (mend - mstart + 1) == image->arch.backup_src_sz)
  325. phdr->p_offset = image->arch.backup_load_addr;
  326. phdr->p_paddr = mstart;
  327. phdr->p_vaddr = (unsigned long long) __va(mstart);
  328. phdr->p_filesz = phdr->p_memsz = mend - mstart + 1;
  329. phdr->p_align = 0;
  330. ehdr->e_phnum++;
  331. 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",
  332. phdr, phdr->p_vaddr, phdr->p_paddr, phdr->p_filesz,
  333. ehdr->e_phnum, phdr->p_offset);
  334. }
  335. return ret;
  336. }
  337. static int prepare_elf64_headers(struct crash_elf_data *ced,
  338. void **addr, unsigned long *sz)
  339. {
  340. Elf64_Ehdr *ehdr;
  341. Elf64_Phdr *phdr;
  342. unsigned long nr_cpus = num_possible_cpus(), nr_phdr, elf_sz;
  343. unsigned char *buf, *bufp;
  344. unsigned int cpu;
  345. unsigned long long notes_addr;
  346. int ret;
  347. /* extra phdr for vmcoreinfo elf note */
  348. nr_phdr = nr_cpus + 1;
  349. nr_phdr += ced->max_nr_ranges;
  350. /*
  351. * kexec-tools creates an extra PT_LOAD phdr for kernel text mapping
  352. * area on x86_64 (ffffffff80000000 - ffffffffa0000000).
  353. * I think this is required by tools like gdb. So same physical
  354. * memory will be mapped in two elf headers. One will contain kernel
  355. * text virtual addresses and other will have __va(physical) addresses.
  356. */
  357. nr_phdr++;
  358. elf_sz = sizeof(Elf64_Ehdr) + nr_phdr * sizeof(Elf64_Phdr);
  359. elf_sz = ALIGN(elf_sz, ELF_CORE_HEADER_ALIGN);
  360. buf = vzalloc(elf_sz);
  361. if (!buf)
  362. return -ENOMEM;
  363. bufp = buf;
  364. ehdr = (Elf64_Ehdr *)bufp;
  365. bufp += sizeof(Elf64_Ehdr);
  366. memcpy(ehdr->e_ident, ELFMAG, SELFMAG);
  367. ehdr->e_ident[EI_CLASS] = ELFCLASS64;
  368. ehdr->e_ident[EI_DATA] = ELFDATA2LSB;
  369. ehdr->e_ident[EI_VERSION] = EV_CURRENT;
  370. ehdr->e_ident[EI_OSABI] = ELF_OSABI;
  371. memset(ehdr->e_ident + EI_PAD, 0, EI_NIDENT - EI_PAD);
  372. ehdr->e_type = ET_CORE;
  373. ehdr->e_machine = ELF_ARCH;
  374. ehdr->e_version = EV_CURRENT;
  375. ehdr->e_phoff = sizeof(Elf64_Ehdr);
  376. ehdr->e_ehsize = sizeof(Elf64_Ehdr);
  377. ehdr->e_phentsize = sizeof(Elf64_Phdr);
  378. /* Prepare one phdr of type PT_NOTE for each present cpu */
  379. for_each_present_cpu(cpu) {
  380. phdr = (Elf64_Phdr *)bufp;
  381. bufp += sizeof(Elf64_Phdr);
  382. phdr->p_type = PT_NOTE;
  383. notes_addr = per_cpu_ptr_to_phys(per_cpu_ptr(crash_notes, cpu));
  384. phdr->p_offset = phdr->p_paddr = notes_addr;
  385. phdr->p_filesz = phdr->p_memsz = sizeof(note_buf_t);
  386. (ehdr->e_phnum)++;
  387. }
  388. /* Prepare one PT_NOTE header for vmcoreinfo */
  389. phdr = (Elf64_Phdr *)bufp;
  390. bufp += sizeof(Elf64_Phdr);
  391. phdr->p_type = PT_NOTE;
  392. phdr->p_offset = phdr->p_paddr = paddr_vmcoreinfo_note();
  393. phdr->p_filesz = phdr->p_memsz = sizeof(vmcoreinfo_note);
  394. (ehdr->e_phnum)++;
  395. #ifdef CONFIG_X86_64
  396. /* Prepare PT_LOAD type program header for kernel text region */
  397. phdr = (Elf64_Phdr *)bufp;
  398. bufp += sizeof(Elf64_Phdr);
  399. phdr->p_type = PT_LOAD;
  400. phdr->p_flags = PF_R|PF_W|PF_X;
  401. phdr->p_vaddr = (Elf64_Addr)_text;
  402. phdr->p_filesz = phdr->p_memsz = _end - _text;
  403. phdr->p_offset = phdr->p_paddr = __pa_symbol(_text);
  404. (ehdr->e_phnum)++;
  405. #endif
  406. /* Prepare PT_LOAD headers for system ram chunks. */
  407. ced->ehdr = ehdr;
  408. ced->bufp = bufp;
  409. ret = walk_system_ram_res(0, -1, ced,
  410. prepare_elf64_ram_headers_callback);
  411. if (ret < 0)
  412. return ret;
  413. *addr = buf;
  414. *sz = elf_sz;
  415. return 0;
  416. }
  417. /* Prepare elf headers. Return addr and size */
  418. static int prepare_elf_headers(struct kimage *image, void **addr,
  419. unsigned long *sz)
  420. {
  421. struct crash_elf_data *ced;
  422. int ret;
  423. ced = kzalloc(sizeof(*ced), GFP_KERNEL);
  424. if (!ced)
  425. return -ENOMEM;
  426. fill_up_crash_elf_data(ced, image);
  427. /* By default prepare 64bit headers */
  428. ret = prepare_elf64_headers(ced, addr, sz);
  429. kfree(ced);
  430. return ret;
  431. }
  432. static int add_e820_entry(struct boot_params *params, struct e820entry *entry)
  433. {
  434. unsigned int nr_e820_entries;
  435. nr_e820_entries = params->e820_entries;
  436. if (nr_e820_entries >= E820MAX)
  437. return 1;
  438. memcpy(&params->e820_map[nr_e820_entries], entry,
  439. sizeof(struct e820entry));
  440. params->e820_entries++;
  441. return 0;
  442. }
  443. static int memmap_entry_callback(u64 start, u64 end, void *arg)
  444. {
  445. struct crash_memmap_data *cmd = arg;
  446. struct boot_params *params = cmd->params;
  447. struct e820entry ei;
  448. ei.addr = start;
  449. ei.size = end - start + 1;
  450. ei.type = cmd->type;
  451. add_e820_entry(params, &ei);
  452. return 0;
  453. }
  454. static int memmap_exclude_ranges(struct kimage *image, struct crash_mem *cmem,
  455. unsigned long long mstart,
  456. unsigned long long mend)
  457. {
  458. unsigned long start, end;
  459. int ret = 0;
  460. cmem->ranges[0].start = mstart;
  461. cmem->ranges[0].end = mend;
  462. cmem->nr_ranges = 1;
  463. /* Exclude Backup region */
  464. start = image->arch.backup_load_addr;
  465. end = start + image->arch.backup_src_sz - 1;
  466. ret = exclude_mem_range(cmem, start, end);
  467. if (ret)
  468. return ret;
  469. /* Exclude elf header region */
  470. start = image->arch.elf_load_addr;
  471. end = start + image->arch.elf_headers_sz - 1;
  472. return exclude_mem_range(cmem, start, end);
  473. }
  474. /* Prepare memory map for crash dump kernel */
  475. int crash_setup_memmap_entries(struct kimage *image, struct boot_params *params)
  476. {
  477. int i, ret = 0;
  478. unsigned long flags;
  479. struct e820entry ei;
  480. struct crash_memmap_data cmd;
  481. struct crash_mem *cmem;
  482. cmem = vzalloc(sizeof(struct crash_mem));
  483. if (!cmem)
  484. return -ENOMEM;
  485. memset(&cmd, 0, sizeof(struct crash_memmap_data));
  486. cmd.params = params;
  487. /* Add first 640K segment */
  488. ei.addr = image->arch.backup_src_start;
  489. ei.size = image->arch.backup_src_sz;
  490. ei.type = E820_RAM;
  491. add_e820_entry(params, &ei);
  492. /* Add ACPI tables */
  493. cmd.type = E820_ACPI;
  494. flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  495. walk_iomem_res("ACPI Tables", flags, 0, -1, &cmd,
  496. memmap_entry_callback);
  497. /* Add ACPI Non-volatile Storage */
  498. cmd.type = E820_NVS;
  499. walk_iomem_res("ACPI Non-volatile Storage", flags, 0, -1, &cmd,
  500. memmap_entry_callback);
  501. /* Add crashk_low_res region */
  502. if (crashk_low_res.end) {
  503. ei.addr = crashk_low_res.start;
  504. ei.size = crashk_low_res.end - crashk_low_res.start + 1;
  505. ei.type = E820_RAM;
  506. add_e820_entry(params, &ei);
  507. }
  508. /* Exclude some ranges from crashk_res and add rest to memmap */
  509. ret = memmap_exclude_ranges(image, cmem, crashk_res.start,
  510. crashk_res.end);
  511. if (ret)
  512. goto out;
  513. for (i = 0; i < cmem->nr_ranges; i++) {
  514. ei.size = cmem->ranges[i].end - cmem->ranges[i].start + 1;
  515. /* If entry is less than a page, skip it */
  516. if (ei.size < PAGE_SIZE)
  517. continue;
  518. ei.addr = cmem->ranges[i].start;
  519. ei.type = E820_RAM;
  520. add_e820_entry(params, &ei);
  521. }
  522. out:
  523. vfree(cmem);
  524. return ret;
  525. }
  526. static int determine_backup_region(u64 start, u64 end, void *arg)
  527. {
  528. struct kimage *image = arg;
  529. image->arch.backup_src_start = start;
  530. image->arch.backup_src_sz = end - start + 1;
  531. /* Expecting only one range for backup region */
  532. return 1;
  533. }
  534. int crash_load_segments(struct kimage *image)
  535. {
  536. unsigned long src_start, src_sz, elf_sz;
  537. void *elf_addr;
  538. int ret;
  539. /*
  540. * Determine and load a segment for backup area. First 640K RAM
  541. * region is backup source
  542. */
  543. ret = walk_system_ram_res(KEXEC_BACKUP_SRC_START, KEXEC_BACKUP_SRC_END,
  544. image, determine_backup_region);
  545. /* Zero or postive return values are ok */
  546. if (ret < 0)
  547. return ret;
  548. src_start = image->arch.backup_src_start;
  549. src_sz = image->arch.backup_src_sz;
  550. /* Add backup segment. */
  551. if (src_sz) {
  552. /*
  553. * Ideally there is no source for backup segment. This is
  554. * copied in purgatory after crash. Just add a zero filled
  555. * segment for now to make sure checksum logic works fine.
  556. */
  557. ret = kexec_add_buffer(image, (char *)&crash_zero_bytes,
  558. sizeof(crash_zero_bytes), src_sz,
  559. PAGE_SIZE, 0, -1, 0,
  560. &image->arch.backup_load_addr);
  561. if (ret)
  562. return ret;
  563. pr_debug("Loaded backup region at 0x%lx backup_start=0x%lx memsz=0x%lx\n",
  564. image->arch.backup_load_addr, src_start, src_sz);
  565. }
  566. /* Prepare elf headers and add a segment */
  567. ret = prepare_elf_headers(image, &elf_addr, &elf_sz);
  568. if (ret)
  569. return ret;
  570. image->arch.elf_headers = elf_addr;
  571. image->arch.elf_headers_sz = elf_sz;
  572. ret = kexec_add_buffer(image, (char *)elf_addr, elf_sz, elf_sz,
  573. ELF_CORE_HEADER_ALIGN, 0, -1, 0,
  574. &image->arch.elf_load_addr);
  575. if (ret) {
  576. vfree((void *)image->arch.elf_headers);
  577. return ret;
  578. }
  579. pr_debug("Loaded ELF headers at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
  580. image->arch.elf_load_addr, elf_sz, elf_sz);
  581. return ret;
  582. }
  583. #endif /* CONFIG_KEXEC_FILE */