crash.c 17 KB

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