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