crash_dump.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * S390 kdump implementation
  4. *
  5. * Copyright IBM Corp. 2011
  6. * Author(s): Michael Holzheu <[email protected]>
  7. */
  8. #include <linux/crash_dump.h>
  9. #include <asm/lowcore.h>
  10. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/mm.h>
  13. #include <linux/gfp.h>
  14. #include <linux/slab.h>
  15. #include <linux/memblock.h>
  16. #include <linux/elf.h>
  17. #include <linux/uio.h>
  18. #include <asm/asm-offsets.h>
  19. #include <asm/os_info.h>
  20. #include <asm/elf.h>
  21. #include <asm/ipl.h>
  22. #include <asm/sclp.h>
  23. #include <asm/maccess.h>
  24. #define PTR_ADD(x, y) (((char *) (x)) + ((unsigned long) (y)))
  25. #define PTR_SUB(x, y) (((char *) (x)) - ((unsigned long) (y)))
  26. #define PTR_DIFF(x, y) ((unsigned long)(((char *) (x)) - ((unsigned long) (y))))
  27. static struct memblock_region oldmem_region;
  28. static struct memblock_type oldmem_type = {
  29. .cnt = 1,
  30. .max = 1,
  31. .total_size = 0,
  32. .regions = &oldmem_region,
  33. .name = "oldmem",
  34. };
  35. struct save_area {
  36. struct list_head list;
  37. u64 psw[2];
  38. u64 ctrs[16];
  39. u64 gprs[16];
  40. u32 acrs[16];
  41. u64 fprs[16];
  42. u32 fpc;
  43. u32 prefix;
  44. u32 todpreg;
  45. u64 timer;
  46. u64 todcmp;
  47. u64 vxrs_low[16];
  48. __vector128 vxrs_high[16];
  49. };
  50. static LIST_HEAD(dump_save_areas);
  51. /*
  52. * Allocate a save area
  53. */
  54. struct save_area * __init save_area_alloc(bool is_boot_cpu)
  55. {
  56. struct save_area *sa;
  57. sa = memblock_alloc(sizeof(*sa), 8);
  58. if (!sa)
  59. return NULL;
  60. if (is_boot_cpu)
  61. list_add(&sa->list, &dump_save_areas);
  62. else
  63. list_add_tail(&sa->list, &dump_save_areas);
  64. return sa;
  65. }
  66. /*
  67. * Return the address of the save area for the boot CPU
  68. */
  69. struct save_area * __init save_area_boot_cpu(void)
  70. {
  71. return list_first_entry_or_null(&dump_save_areas, struct save_area, list);
  72. }
  73. /*
  74. * Copy CPU registers into the save area
  75. */
  76. void __init save_area_add_regs(struct save_area *sa, void *regs)
  77. {
  78. struct lowcore *lc;
  79. lc = (struct lowcore *)(regs - __LC_FPREGS_SAVE_AREA);
  80. memcpy(&sa->psw, &lc->psw_save_area, sizeof(sa->psw));
  81. memcpy(&sa->ctrs, &lc->cregs_save_area, sizeof(sa->ctrs));
  82. memcpy(&sa->gprs, &lc->gpregs_save_area, sizeof(sa->gprs));
  83. memcpy(&sa->acrs, &lc->access_regs_save_area, sizeof(sa->acrs));
  84. memcpy(&sa->fprs, &lc->floating_pt_save_area, sizeof(sa->fprs));
  85. memcpy(&sa->fpc, &lc->fpt_creg_save_area, sizeof(sa->fpc));
  86. memcpy(&sa->prefix, &lc->prefixreg_save_area, sizeof(sa->prefix));
  87. memcpy(&sa->todpreg, &lc->tod_progreg_save_area, sizeof(sa->todpreg));
  88. memcpy(&sa->timer, &lc->cpu_timer_save_area, sizeof(sa->timer));
  89. memcpy(&sa->todcmp, &lc->clock_comp_save_area, sizeof(sa->todcmp));
  90. }
  91. /*
  92. * Copy vector registers into the save area
  93. */
  94. void __init save_area_add_vxrs(struct save_area *sa, __vector128 *vxrs)
  95. {
  96. int i;
  97. /* Copy lower halves of vector registers 0-15 */
  98. for (i = 0; i < 16; i++)
  99. memcpy(&sa->vxrs_low[i], &vxrs[i].u[2], 8);
  100. /* Copy vector registers 16-31 */
  101. memcpy(sa->vxrs_high, vxrs + 16, 16 * sizeof(__vector128));
  102. }
  103. static size_t copy_oldmem_iter(struct iov_iter *iter, unsigned long src, size_t count)
  104. {
  105. size_t len, copied, res = 0;
  106. while (count) {
  107. if (!oldmem_data.start && src < sclp.hsa_size) {
  108. /* Copy from zfcp/nvme dump HSA area */
  109. len = min(count, sclp.hsa_size - src);
  110. copied = memcpy_hsa_iter(iter, src, len);
  111. } else {
  112. /* Check for swapped kdump oldmem areas */
  113. if (oldmem_data.start && src - oldmem_data.start < oldmem_data.size) {
  114. src -= oldmem_data.start;
  115. len = min(count, oldmem_data.size - src);
  116. } else if (oldmem_data.start && src < oldmem_data.size) {
  117. len = min(count, oldmem_data.size - src);
  118. src += oldmem_data.start;
  119. } else {
  120. len = count;
  121. }
  122. copied = memcpy_real_iter(iter, src, len);
  123. }
  124. count -= copied;
  125. src += copied;
  126. res += copied;
  127. if (copied < len)
  128. break;
  129. }
  130. return res;
  131. }
  132. int copy_oldmem_kernel(void *dst, unsigned long src, size_t count)
  133. {
  134. struct iov_iter iter;
  135. struct kvec kvec;
  136. kvec.iov_base = dst;
  137. kvec.iov_len = count;
  138. iov_iter_kvec(&iter, ITER_DEST, &kvec, 1, count);
  139. if (copy_oldmem_iter(&iter, src, count) < count)
  140. return -EFAULT;
  141. return 0;
  142. }
  143. /*
  144. * Copy one page from "oldmem"
  145. */
  146. ssize_t copy_oldmem_page(struct iov_iter *iter, unsigned long pfn, size_t csize,
  147. unsigned long offset)
  148. {
  149. unsigned long src;
  150. src = pfn_to_phys(pfn) + offset;
  151. return copy_oldmem_iter(iter, src, csize);
  152. }
  153. /*
  154. * Remap "oldmem" for kdump
  155. *
  156. * For the kdump reserved memory this functions performs a swap operation:
  157. * [0 - OLDMEM_SIZE] is mapped to [OLDMEM_BASE - OLDMEM_BASE + OLDMEM_SIZE]
  158. */
  159. static int remap_oldmem_pfn_range_kdump(struct vm_area_struct *vma,
  160. unsigned long from, unsigned long pfn,
  161. unsigned long size, pgprot_t prot)
  162. {
  163. unsigned long size_old;
  164. int rc;
  165. if (pfn < oldmem_data.size >> PAGE_SHIFT) {
  166. size_old = min(size, oldmem_data.size - (pfn << PAGE_SHIFT));
  167. rc = remap_pfn_range(vma, from,
  168. pfn + (oldmem_data.start >> PAGE_SHIFT),
  169. size_old, prot);
  170. if (rc || size == size_old)
  171. return rc;
  172. size -= size_old;
  173. from += size_old;
  174. pfn += size_old >> PAGE_SHIFT;
  175. }
  176. return remap_pfn_range(vma, from, pfn, size, prot);
  177. }
  178. /*
  179. * Remap "oldmem" for zfcp/nvme dump
  180. *
  181. * We only map available memory above HSA size. Memory below HSA size
  182. * is read on demand using the copy_oldmem_page() function.
  183. */
  184. static int remap_oldmem_pfn_range_zfcpdump(struct vm_area_struct *vma,
  185. unsigned long from,
  186. unsigned long pfn,
  187. unsigned long size, pgprot_t prot)
  188. {
  189. unsigned long hsa_end = sclp.hsa_size;
  190. unsigned long size_hsa;
  191. if (pfn < hsa_end >> PAGE_SHIFT) {
  192. size_hsa = min(size, hsa_end - (pfn << PAGE_SHIFT));
  193. if (size == size_hsa)
  194. return 0;
  195. size -= size_hsa;
  196. from += size_hsa;
  197. pfn += size_hsa >> PAGE_SHIFT;
  198. }
  199. return remap_pfn_range(vma, from, pfn, size, prot);
  200. }
  201. /*
  202. * Remap "oldmem" for kdump or zfcp/nvme dump
  203. */
  204. int remap_oldmem_pfn_range(struct vm_area_struct *vma, unsigned long from,
  205. unsigned long pfn, unsigned long size, pgprot_t prot)
  206. {
  207. if (oldmem_data.start)
  208. return remap_oldmem_pfn_range_kdump(vma, from, pfn, size, prot);
  209. else
  210. return remap_oldmem_pfn_range_zfcpdump(vma, from, pfn, size,
  211. prot);
  212. }
  213. static const char *nt_name(Elf64_Word type)
  214. {
  215. const char *name = "LINUX";
  216. if (type == NT_PRPSINFO || type == NT_PRSTATUS || type == NT_PRFPREG)
  217. name = KEXEC_CORE_NOTE_NAME;
  218. return name;
  219. }
  220. /*
  221. * Initialize ELF note
  222. */
  223. static void *nt_init_name(void *buf, Elf64_Word type, void *desc, int d_len,
  224. const char *name)
  225. {
  226. Elf64_Nhdr *note;
  227. u64 len;
  228. note = (Elf64_Nhdr *)buf;
  229. note->n_namesz = strlen(name) + 1;
  230. note->n_descsz = d_len;
  231. note->n_type = type;
  232. len = sizeof(Elf64_Nhdr);
  233. memcpy(buf + len, name, note->n_namesz);
  234. len = roundup(len + note->n_namesz, 4);
  235. memcpy(buf + len, desc, note->n_descsz);
  236. len = roundup(len + note->n_descsz, 4);
  237. return PTR_ADD(buf, len);
  238. }
  239. static inline void *nt_init(void *buf, Elf64_Word type, void *desc, int d_len)
  240. {
  241. return nt_init_name(buf, type, desc, d_len, nt_name(type));
  242. }
  243. /*
  244. * Calculate the size of ELF note
  245. */
  246. static size_t nt_size_name(int d_len, const char *name)
  247. {
  248. size_t size;
  249. size = sizeof(Elf64_Nhdr);
  250. size += roundup(strlen(name) + 1, 4);
  251. size += roundup(d_len, 4);
  252. return size;
  253. }
  254. static inline size_t nt_size(Elf64_Word type, int d_len)
  255. {
  256. return nt_size_name(d_len, nt_name(type));
  257. }
  258. /*
  259. * Fill ELF notes for one CPU with save area registers
  260. */
  261. static void *fill_cpu_elf_notes(void *ptr, int cpu, struct save_area *sa)
  262. {
  263. struct elf_prstatus nt_prstatus;
  264. elf_fpregset_t nt_fpregset;
  265. /* Prepare prstatus note */
  266. memset(&nt_prstatus, 0, sizeof(nt_prstatus));
  267. memcpy(&nt_prstatus.pr_reg.gprs, sa->gprs, sizeof(sa->gprs));
  268. memcpy(&nt_prstatus.pr_reg.psw, sa->psw, sizeof(sa->psw));
  269. memcpy(&nt_prstatus.pr_reg.acrs, sa->acrs, sizeof(sa->acrs));
  270. nt_prstatus.common.pr_pid = cpu;
  271. /* Prepare fpregset (floating point) note */
  272. memset(&nt_fpregset, 0, sizeof(nt_fpregset));
  273. memcpy(&nt_fpregset.fpc, &sa->fpc, sizeof(sa->fpc));
  274. memcpy(&nt_fpregset.fprs, &sa->fprs, sizeof(sa->fprs));
  275. /* Create ELF notes for the CPU */
  276. ptr = nt_init(ptr, NT_PRSTATUS, &nt_prstatus, sizeof(nt_prstatus));
  277. ptr = nt_init(ptr, NT_PRFPREG, &nt_fpregset, sizeof(nt_fpregset));
  278. ptr = nt_init(ptr, NT_S390_TIMER, &sa->timer, sizeof(sa->timer));
  279. ptr = nt_init(ptr, NT_S390_TODCMP, &sa->todcmp, sizeof(sa->todcmp));
  280. ptr = nt_init(ptr, NT_S390_TODPREG, &sa->todpreg, sizeof(sa->todpreg));
  281. ptr = nt_init(ptr, NT_S390_CTRS, &sa->ctrs, sizeof(sa->ctrs));
  282. ptr = nt_init(ptr, NT_S390_PREFIX, &sa->prefix, sizeof(sa->prefix));
  283. if (MACHINE_HAS_VX) {
  284. ptr = nt_init(ptr, NT_S390_VXRS_HIGH,
  285. &sa->vxrs_high, sizeof(sa->vxrs_high));
  286. ptr = nt_init(ptr, NT_S390_VXRS_LOW,
  287. &sa->vxrs_low, sizeof(sa->vxrs_low));
  288. }
  289. return ptr;
  290. }
  291. /*
  292. * Calculate size of ELF notes per cpu
  293. */
  294. static size_t get_cpu_elf_notes_size(void)
  295. {
  296. struct save_area *sa = NULL;
  297. size_t size;
  298. size = nt_size(NT_PRSTATUS, sizeof(struct elf_prstatus));
  299. size += nt_size(NT_PRFPREG, sizeof(elf_fpregset_t));
  300. size += nt_size(NT_S390_TIMER, sizeof(sa->timer));
  301. size += nt_size(NT_S390_TODCMP, sizeof(sa->todcmp));
  302. size += nt_size(NT_S390_TODPREG, sizeof(sa->todpreg));
  303. size += nt_size(NT_S390_CTRS, sizeof(sa->ctrs));
  304. size += nt_size(NT_S390_PREFIX, sizeof(sa->prefix));
  305. if (MACHINE_HAS_VX) {
  306. size += nt_size(NT_S390_VXRS_HIGH, sizeof(sa->vxrs_high));
  307. size += nt_size(NT_S390_VXRS_LOW, sizeof(sa->vxrs_low));
  308. }
  309. return size;
  310. }
  311. /*
  312. * Initialize prpsinfo note (new kernel)
  313. */
  314. static void *nt_prpsinfo(void *ptr)
  315. {
  316. struct elf_prpsinfo prpsinfo;
  317. memset(&prpsinfo, 0, sizeof(prpsinfo));
  318. prpsinfo.pr_sname = 'R';
  319. strcpy(prpsinfo.pr_fname, "vmlinux");
  320. return nt_init(ptr, NT_PRPSINFO, &prpsinfo, sizeof(prpsinfo));
  321. }
  322. /*
  323. * Get vmcoreinfo using lowcore->vmcore_info (new kernel)
  324. */
  325. static void *get_vmcoreinfo_old(unsigned long *size)
  326. {
  327. char nt_name[11], *vmcoreinfo;
  328. unsigned long addr;
  329. Elf64_Nhdr note;
  330. if (copy_oldmem_kernel(&addr, __LC_VMCORE_INFO, sizeof(addr)))
  331. return NULL;
  332. memset(nt_name, 0, sizeof(nt_name));
  333. if (copy_oldmem_kernel(&note, addr, sizeof(note)))
  334. return NULL;
  335. if (copy_oldmem_kernel(nt_name, addr + sizeof(note),
  336. sizeof(nt_name) - 1))
  337. return NULL;
  338. if (strcmp(nt_name, VMCOREINFO_NOTE_NAME) != 0)
  339. return NULL;
  340. vmcoreinfo = kzalloc(note.n_descsz, GFP_KERNEL);
  341. if (!vmcoreinfo)
  342. return NULL;
  343. if (copy_oldmem_kernel(vmcoreinfo, addr + 24, note.n_descsz)) {
  344. kfree(vmcoreinfo);
  345. return NULL;
  346. }
  347. *size = note.n_descsz;
  348. return vmcoreinfo;
  349. }
  350. /*
  351. * Initialize vmcoreinfo note (new kernel)
  352. */
  353. static void *nt_vmcoreinfo(void *ptr)
  354. {
  355. const char *name = VMCOREINFO_NOTE_NAME;
  356. unsigned long size;
  357. void *vmcoreinfo;
  358. vmcoreinfo = os_info_old_entry(OS_INFO_VMCOREINFO, &size);
  359. if (vmcoreinfo)
  360. return nt_init_name(ptr, 0, vmcoreinfo, size, name);
  361. vmcoreinfo = get_vmcoreinfo_old(&size);
  362. if (!vmcoreinfo)
  363. return ptr;
  364. ptr = nt_init_name(ptr, 0, vmcoreinfo, size, name);
  365. kfree(vmcoreinfo);
  366. return ptr;
  367. }
  368. static size_t nt_vmcoreinfo_size(void)
  369. {
  370. const char *name = VMCOREINFO_NOTE_NAME;
  371. unsigned long size;
  372. void *vmcoreinfo;
  373. vmcoreinfo = os_info_old_entry(OS_INFO_VMCOREINFO, &size);
  374. if (vmcoreinfo)
  375. return nt_size_name(size, name);
  376. vmcoreinfo = get_vmcoreinfo_old(&size);
  377. if (!vmcoreinfo)
  378. return 0;
  379. kfree(vmcoreinfo);
  380. return nt_size_name(size, name);
  381. }
  382. /*
  383. * Initialize final note (needed for /proc/vmcore code)
  384. */
  385. static void *nt_final(void *ptr)
  386. {
  387. Elf64_Nhdr *note;
  388. note = (Elf64_Nhdr *) ptr;
  389. note->n_namesz = 0;
  390. note->n_descsz = 0;
  391. note->n_type = 0;
  392. return PTR_ADD(ptr, sizeof(Elf64_Nhdr));
  393. }
  394. /*
  395. * Initialize ELF header (new kernel)
  396. */
  397. static void *ehdr_init(Elf64_Ehdr *ehdr, int mem_chunk_cnt)
  398. {
  399. memset(ehdr, 0, sizeof(*ehdr));
  400. memcpy(ehdr->e_ident, ELFMAG, SELFMAG);
  401. ehdr->e_ident[EI_CLASS] = ELFCLASS64;
  402. ehdr->e_ident[EI_DATA] = ELFDATA2MSB;
  403. ehdr->e_ident[EI_VERSION] = EV_CURRENT;
  404. memset(ehdr->e_ident + EI_PAD, 0, EI_NIDENT - EI_PAD);
  405. ehdr->e_type = ET_CORE;
  406. ehdr->e_machine = EM_S390;
  407. ehdr->e_version = EV_CURRENT;
  408. ehdr->e_phoff = sizeof(Elf64_Ehdr);
  409. ehdr->e_ehsize = sizeof(Elf64_Ehdr);
  410. ehdr->e_phentsize = sizeof(Elf64_Phdr);
  411. ehdr->e_phnum = mem_chunk_cnt + 1;
  412. return ehdr + 1;
  413. }
  414. /*
  415. * Return CPU count for ELF header (new kernel)
  416. */
  417. static int get_cpu_cnt(void)
  418. {
  419. struct save_area *sa;
  420. int cpus = 0;
  421. list_for_each_entry(sa, &dump_save_areas, list)
  422. if (sa->prefix != 0)
  423. cpus++;
  424. return cpus;
  425. }
  426. /*
  427. * Return memory chunk count for ELF header (new kernel)
  428. */
  429. static int get_mem_chunk_cnt(void)
  430. {
  431. int cnt = 0;
  432. u64 idx;
  433. for_each_physmem_range(idx, &oldmem_type, NULL, NULL)
  434. cnt++;
  435. return cnt;
  436. }
  437. /*
  438. * Initialize ELF loads (new kernel)
  439. */
  440. static void loads_init(Elf64_Phdr *phdr, u64 loads_offset)
  441. {
  442. phys_addr_t start, end;
  443. u64 idx;
  444. for_each_physmem_range(idx, &oldmem_type, &start, &end) {
  445. phdr->p_filesz = end - start;
  446. phdr->p_type = PT_LOAD;
  447. phdr->p_offset = start;
  448. phdr->p_vaddr = start;
  449. phdr->p_paddr = start;
  450. phdr->p_memsz = end - start;
  451. phdr->p_flags = PF_R | PF_W | PF_X;
  452. phdr->p_align = PAGE_SIZE;
  453. phdr++;
  454. }
  455. }
  456. /*
  457. * Initialize notes (new kernel)
  458. */
  459. static void *notes_init(Elf64_Phdr *phdr, void *ptr, u64 notes_offset)
  460. {
  461. struct save_area *sa;
  462. void *ptr_start = ptr;
  463. int cpu;
  464. ptr = nt_prpsinfo(ptr);
  465. cpu = 1;
  466. list_for_each_entry(sa, &dump_save_areas, list)
  467. if (sa->prefix != 0)
  468. ptr = fill_cpu_elf_notes(ptr, cpu++, sa);
  469. ptr = nt_vmcoreinfo(ptr);
  470. ptr = nt_final(ptr);
  471. memset(phdr, 0, sizeof(*phdr));
  472. phdr->p_type = PT_NOTE;
  473. phdr->p_offset = notes_offset;
  474. phdr->p_filesz = (unsigned long) PTR_SUB(ptr, ptr_start);
  475. phdr->p_memsz = phdr->p_filesz;
  476. return ptr;
  477. }
  478. static size_t get_elfcorehdr_size(int mem_chunk_cnt)
  479. {
  480. size_t size;
  481. size = sizeof(Elf64_Ehdr);
  482. /* PT_NOTES */
  483. size += sizeof(Elf64_Phdr);
  484. /* nt_prpsinfo */
  485. size += nt_size(NT_PRPSINFO, sizeof(struct elf_prpsinfo));
  486. /* regsets */
  487. size += get_cpu_cnt() * get_cpu_elf_notes_size();
  488. /* nt_vmcoreinfo */
  489. size += nt_vmcoreinfo_size();
  490. /* nt_final */
  491. size += sizeof(Elf64_Nhdr);
  492. /* PT_LOADS */
  493. size += mem_chunk_cnt * sizeof(Elf64_Phdr);
  494. return size;
  495. }
  496. /*
  497. * Create ELF core header (new kernel)
  498. */
  499. int elfcorehdr_alloc(unsigned long long *addr, unsigned long long *size)
  500. {
  501. Elf64_Phdr *phdr_notes, *phdr_loads;
  502. int mem_chunk_cnt;
  503. void *ptr, *hdr;
  504. u32 alloc_size;
  505. u64 hdr_off;
  506. /* If we are not in kdump or zfcp/nvme dump mode return */
  507. if (!oldmem_data.start && !is_ipl_type_dump())
  508. return 0;
  509. /* If we cannot get HSA size for zfcp/nvme dump return error */
  510. if (is_ipl_type_dump() && !sclp.hsa_size)
  511. return -ENODEV;
  512. /* For kdump, exclude previous crashkernel memory */
  513. if (oldmem_data.start) {
  514. oldmem_region.base = oldmem_data.start;
  515. oldmem_region.size = oldmem_data.size;
  516. oldmem_type.total_size = oldmem_data.size;
  517. }
  518. mem_chunk_cnt = get_mem_chunk_cnt();
  519. alloc_size = get_elfcorehdr_size(mem_chunk_cnt);
  520. hdr = kzalloc(alloc_size, GFP_KERNEL);
  521. /* Without elfcorehdr /proc/vmcore cannot be created. Thus creating
  522. * a dump with this crash kernel will fail. Panic now to allow other
  523. * dump mechanisms to take over.
  524. */
  525. if (!hdr)
  526. panic("s390 kdump allocating elfcorehdr failed");
  527. /* Init elf header */
  528. ptr = ehdr_init(hdr, mem_chunk_cnt);
  529. /* Init program headers */
  530. phdr_notes = ptr;
  531. ptr = PTR_ADD(ptr, sizeof(Elf64_Phdr));
  532. phdr_loads = ptr;
  533. ptr = PTR_ADD(ptr, sizeof(Elf64_Phdr) * mem_chunk_cnt);
  534. /* Init notes */
  535. hdr_off = PTR_DIFF(ptr, hdr);
  536. ptr = notes_init(phdr_notes, ptr, ((unsigned long) hdr) + hdr_off);
  537. /* Init loads */
  538. hdr_off = PTR_DIFF(ptr, hdr);
  539. loads_init(phdr_loads, hdr_off);
  540. *addr = (unsigned long long) hdr;
  541. *size = (unsigned long long) hdr_off;
  542. BUG_ON(elfcorehdr_size > alloc_size);
  543. return 0;
  544. }
  545. /*
  546. * Free ELF core header (new kernel)
  547. */
  548. void elfcorehdr_free(unsigned long long addr)
  549. {
  550. kfree((void *)(unsigned long)addr);
  551. }
  552. /*
  553. * Read from ELF header
  554. */
  555. ssize_t elfcorehdr_read(char *buf, size_t count, u64 *ppos)
  556. {
  557. void *src = (void *)(unsigned long)*ppos;
  558. memcpy(buf, src, count);
  559. *ppos += count;
  560. return count;
  561. }
  562. /*
  563. * Read from ELF notes data
  564. */
  565. ssize_t elfcorehdr_read_notes(char *buf, size_t count, u64 *ppos)
  566. {
  567. void *src = (void *)(unsigned long)*ppos;
  568. memcpy(buf, src, count);
  569. *ppos += count;
  570. return count;
  571. }