crash.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  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 != 0)
  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. ret = exclude_mem_range(cmem, crashk_low_res.start, crashk_low_res.end);
  282. if (ret)
  283. return ret;
  284. /* Exclude GART region */
  285. if (ced->gart_end) {
  286. ret = exclude_mem_range(cmem, ced->gart_start, ced->gart_end);
  287. if (ret)
  288. return ret;
  289. }
  290. return ret;
  291. }
  292. static int prepare_elf64_ram_headers_callback(u64 start, u64 end, void *arg)
  293. {
  294. struct crash_elf_data *ced = arg;
  295. Elf64_Ehdr *ehdr;
  296. Elf64_Phdr *phdr;
  297. unsigned long mstart, mend;
  298. struct kimage *image = ced->image;
  299. struct crash_mem *cmem;
  300. int ret, i;
  301. ehdr = ced->ehdr;
  302. /* Exclude unwanted mem ranges */
  303. ret = elf_header_exclude_ranges(ced, start, end);
  304. if (ret)
  305. return ret;
  306. /* Go through all the ranges in ced->mem.ranges[] and prepare phdr */
  307. cmem = &ced->mem;
  308. for (i = 0; i < cmem->nr_ranges; i++) {
  309. mstart = cmem->ranges[i].start;
  310. mend = cmem->ranges[i].end;
  311. phdr = ced->bufp;
  312. ced->bufp += sizeof(Elf64_Phdr);
  313. phdr->p_type = PT_LOAD;
  314. phdr->p_flags = PF_R|PF_W|PF_X;
  315. phdr->p_offset = mstart;
  316. /*
  317. * If a range matches backup region, adjust offset to backup
  318. * segment.
  319. */
  320. if (mstart == image->arch.backup_src_start &&
  321. (mend - mstart + 1) == image->arch.backup_src_sz)
  322. phdr->p_offset = image->arch.backup_load_addr;
  323. phdr->p_paddr = mstart;
  324. phdr->p_vaddr = (unsigned long long) __va(mstart);
  325. phdr->p_filesz = phdr->p_memsz = mend - mstart + 1;
  326. phdr->p_align = 0;
  327. ehdr->e_phnum++;
  328. 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",
  329. phdr, phdr->p_vaddr, phdr->p_paddr, phdr->p_filesz,
  330. ehdr->e_phnum, phdr->p_offset);
  331. }
  332. return ret;
  333. }
  334. static int prepare_elf64_headers(struct crash_elf_data *ced,
  335. void **addr, unsigned long *sz)
  336. {
  337. Elf64_Ehdr *ehdr;
  338. Elf64_Phdr *phdr;
  339. unsigned long nr_cpus = num_possible_cpus(), nr_phdr, elf_sz;
  340. unsigned char *buf, *bufp;
  341. unsigned int cpu;
  342. unsigned long long notes_addr;
  343. int ret;
  344. /* extra phdr for vmcoreinfo elf note */
  345. nr_phdr = nr_cpus + 1;
  346. nr_phdr += ced->max_nr_ranges;
  347. /*
  348. * kexec-tools creates an extra PT_LOAD phdr for kernel text mapping
  349. * area on x86_64 (ffffffff80000000 - ffffffffa0000000).
  350. * I think this is required by tools like gdb. So same physical
  351. * memory will be mapped in two elf headers. One will contain kernel
  352. * text virtual addresses and other will have __va(physical) addresses.
  353. */
  354. nr_phdr++;
  355. elf_sz = sizeof(Elf64_Ehdr) + nr_phdr * sizeof(Elf64_Phdr);
  356. elf_sz = ALIGN(elf_sz, ELF_CORE_HEADER_ALIGN);
  357. buf = vzalloc(elf_sz);
  358. if (!buf)
  359. return -ENOMEM;
  360. bufp = buf;
  361. ehdr = (Elf64_Ehdr *)bufp;
  362. bufp += sizeof(Elf64_Ehdr);
  363. memcpy(ehdr->e_ident, ELFMAG, SELFMAG);
  364. ehdr->e_ident[EI_CLASS] = ELFCLASS64;
  365. ehdr->e_ident[EI_DATA] = ELFDATA2LSB;
  366. ehdr->e_ident[EI_VERSION] = EV_CURRENT;
  367. ehdr->e_ident[EI_OSABI] = ELF_OSABI;
  368. memset(ehdr->e_ident + EI_PAD, 0, EI_NIDENT - EI_PAD);
  369. ehdr->e_type = ET_CORE;
  370. ehdr->e_machine = ELF_ARCH;
  371. ehdr->e_version = EV_CURRENT;
  372. ehdr->e_phoff = sizeof(Elf64_Ehdr);
  373. ehdr->e_ehsize = sizeof(Elf64_Ehdr);
  374. ehdr->e_phentsize = sizeof(Elf64_Phdr);
  375. /* Prepare one phdr of type PT_NOTE for each present cpu */
  376. for_each_present_cpu(cpu) {
  377. phdr = (Elf64_Phdr *)bufp;
  378. bufp += sizeof(Elf64_Phdr);
  379. phdr->p_type = PT_NOTE;
  380. notes_addr = per_cpu_ptr_to_phys(per_cpu_ptr(crash_notes, cpu));
  381. phdr->p_offset = phdr->p_paddr = notes_addr;
  382. phdr->p_filesz = phdr->p_memsz = sizeof(note_buf_t);
  383. (ehdr->e_phnum)++;
  384. }
  385. /* Prepare one PT_NOTE header for vmcoreinfo */
  386. phdr = (Elf64_Phdr *)bufp;
  387. bufp += sizeof(Elf64_Phdr);
  388. phdr->p_type = PT_NOTE;
  389. phdr->p_offset = phdr->p_paddr = paddr_vmcoreinfo_note();
  390. phdr->p_filesz = phdr->p_memsz = sizeof(vmcoreinfo_note);
  391. (ehdr->e_phnum)++;
  392. #ifdef CONFIG_X86_64
  393. /* Prepare PT_LOAD type program header for kernel text region */
  394. phdr = (Elf64_Phdr *)bufp;
  395. bufp += sizeof(Elf64_Phdr);
  396. phdr->p_type = PT_LOAD;
  397. phdr->p_flags = PF_R|PF_W|PF_X;
  398. phdr->p_vaddr = (Elf64_Addr)_text;
  399. phdr->p_filesz = phdr->p_memsz = _end - _text;
  400. phdr->p_offset = phdr->p_paddr = __pa_symbol(_text);
  401. (ehdr->e_phnum)++;
  402. #endif
  403. /* Prepare PT_LOAD headers for system ram chunks. */
  404. ced->ehdr = ehdr;
  405. ced->bufp = bufp;
  406. ret = walk_system_ram_res(0, -1, ced,
  407. prepare_elf64_ram_headers_callback);
  408. if (ret < 0)
  409. return ret;
  410. *addr = buf;
  411. *sz = elf_sz;
  412. return 0;
  413. }
  414. /* Prepare elf headers. Return addr and size */
  415. static int prepare_elf_headers(struct kimage *image, void **addr,
  416. unsigned long *sz)
  417. {
  418. struct crash_elf_data *ced;
  419. int ret;
  420. ced = kzalloc(sizeof(*ced), GFP_KERNEL);
  421. if (!ced)
  422. return -ENOMEM;
  423. fill_up_crash_elf_data(ced, image);
  424. /* By default prepare 64bit headers */
  425. ret = prepare_elf64_headers(ced, addr, sz);
  426. kfree(ced);
  427. return ret;
  428. }
  429. static int add_e820_entry(struct boot_params *params, struct e820entry *entry)
  430. {
  431. unsigned int nr_e820_entries;
  432. nr_e820_entries = params->e820_entries;
  433. if (nr_e820_entries >= E820MAX)
  434. return 1;
  435. memcpy(&params->e820_map[nr_e820_entries], entry,
  436. sizeof(struct e820entry));
  437. params->e820_entries++;
  438. return 0;
  439. }
  440. static int memmap_entry_callback(u64 start, u64 end, void *arg)
  441. {
  442. struct crash_memmap_data *cmd = arg;
  443. struct boot_params *params = cmd->params;
  444. struct e820entry ei;
  445. ei.addr = start;
  446. ei.size = end - start + 1;
  447. ei.type = cmd->type;
  448. add_e820_entry(params, &ei);
  449. return 0;
  450. }
  451. static int memmap_exclude_ranges(struct kimage *image, struct crash_mem *cmem,
  452. unsigned long long mstart,
  453. unsigned long long mend)
  454. {
  455. unsigned long start, end;
  456. int ret = 0;
  457. cmem->ranges[0].start = mstart;
  458. cmem->ranges[0].end = mend;
  459. cmem->nr_ranges = 1;
  460. /* Exclude Backup region */
  461. start = image->arch.backup_load_addr;
  462. end = start + image->arch.backup_src_sz - 1;
  463. ret = exclude_mem_range(cmem, start, end);
  464. if (ret)
  465. return ret;
  466. /* Exclude elf header region */
  467. start = image->arch.elf_load_addr;
  468. end = start + image->arch.elf_headers_sz - 1;
  469. return exclude_mem_range(cmem, start, end);
  470. }
  471. /* Prepare memory map for crash dump kernel */
  472. int crash_setup_memmap_entries(struct kimage *image, struct boot_params *params)
  473. {
  474. int i, ret = 0;
  475. unsigned long flags;
  476. struct e820entry ei;
  477. struct crash_memmap_data cmd;
  478. struct crash_mem *cmem;
  479. cmem = vzalloc(sizeof(struct crash_mem));
  480. if (!cmem)
  481. return -ENOMEM;
  482. memset(&cmd, 0, sizeof(struct crash_memmap_data));
  483. cmd.params = params;
  484. /* Add first 640K segment */
  485. ei.addr = image->arch.backup_src_start;
  486. ei.size = image->arch.backup_src_sz;
  487. ei.type = E820_RAM;
  488. add_e820_entry(params, &ei);
  489. /* Add ACPI tables */
  490. cmd.type = E820_ACPI;
  491. flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  492. walk_iomem_res("ACPI Tables", flags, 0, -1, &cmd,
  493. memmap_entry_callback);
  494. /* Add ACPI Non-volatile Storage */
  495. cmd.type = E820_NVS;
  496. walk_iomem_res("ACPI Non-volatile Storage", flags, 0, -1, &cmd,
  497. memmap_entry_callback);
  498. /* Add crashk_low_res region */
  499. if (crashk_low_res.end) {
  500. ei.addr = crashk_low_res.start;
  501. ei.size = crashk_low_res.end - crashk_low_res.start + 1;
  502. ei.type = E820_RAM;
  503. add_e820_entry(params, &ei);
  504. }
  505. /* Exclude some ranges from crashk_res and add rest to memmap */
  506. ret = memmap_exclude_ranges(image, cmem, crashk_res.start,
  507. crashk_res.end);
  508. if (ret)
  509. goto out;
  510. for (i = 0; i < cmem->nr_ranges; i++) {
  511. ei.size = cmem->ranges[i].end - cmem->ranges[i].start + 1;
  512. /* If entry is less than a page, skip it */
  513. if (ei.size < PAGE_SIZE)
  514. continue;
  515. ei.addr = cmem->ranges[i].start;
  516. ei.type = E820_RAM;
  517. add_e820_entry(params, &ei);
  518. }
  519. out:
  520. vfree(cmem);
  521. return ret;
  522. }
  523. static int determine_backup_region(u64 start, u64 end, void *arg)
  524. {
  525. struct kimage *image = arg;
  526. image->arch.backup_src_start = start;
  527. image->arch.backup_src_sz = end - start + 1;
  528. /* Expecting only one range for backup region */
  529. return 1;
  530. }
  531. int crash_load_segments(struct kimage *image)
  532. {
  533. unsigned long src_start, src_sz, elf_sz;
  534. void *elf_addr;
  535. int ret;
  536. /*
  537. * Determine and load a segment for backup area. First 640K RAM
  538. * region is backup source
  539. */
  540. ret = walk_system_ram_res(KEXEC_BACKUP_SRC_START, KEXEC_BACKUP_SRC_END,
  541. image, determine_backup_region);
  542. /* Zero or postive return values are ok */
  543. if (ret < 0)
  544. return ret;
  545. src_start = image->arch.backup_src_start;
  546. src_sz = image->arch.backup_src_sz;
  547. /* Add backup segment. */
  548. if (src_sz) {
  549. /*
  550. * Ideally there is no source for backup segment. This is
  551. * copied in purgatory after crash. Just add a zero filled
  552. * segment for now to make sure checksum logic works fine.
  553. */
  554. ret = kexec_add_buffer(image, (char *)&crash_zero_bytes,
  555. sizeof(crash_zero_bytes), src_sz,
  556. PAGE_SIZE, 0, -1, 0,
  557. &image->arch.backup_load_addr);
  558. if (ret)
  559. return ret;
  560. pr_debug("Loaded backup region at 0x%lx backup_start=0x%lx memsz=0x%lx\n",
  561. image->arch.backup_load_addr, src_start, src_sz);
  562. }
  563. /* Prepare elf headers and add a segment */
  564. ret = prepare_elf_headers(image, &elf_addr, &elf_sz);
  565. if (ret)
  566. return ret;
  567. image->arch.elf_headers = elf_addr;
  568. image->arch.elf_headers_sz = elf_sz;
  569. ret = kexec_add_buffer(image, (char *)elf_addr, elf_sz, elf_sz,
  570. ELF_CORE_HEADER_ALIGN, 0, -1, 0,
  571. &image->arch.elf_load_addr);
  572. if (ret) {
  573. vfree((void *)image->arch.elf_headers);
  574. return ret;
  575. }
  576. pr_debug("Loaded ELF headers at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
  577. image->arch.elf_load_addr, elf_sz, elf_sz);
  578. return ret;
  579. }
  580. #endif /* CONFIG_KEXEC_FILE */