crash_dump.c 17 KB

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