crash.c 17 KB

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