vmcore.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  1. /*
  2. * fs/proc/vmcore.c Interface for accessing the crash
  3. * dump from the system's previous life.
  4. * Heavily borrowed from fs/proc/kcore.c
  5. * Created by: Hariprasad Nellitheertha (hari@in.ibm.com)
  6. * Copyright (C) IBM Corporation, 2004. All rights reserved
  7. *
  8. */
  9. #include <linux/mm.h>
  10. #include <linux/kcore.h>
  11. #include <linux/user.h>
  12. #include <linux/elf.h>
  13. #include <linux/elfcore.h>
  14. #include <linux/export.h>
  15. #include <linux/slab.h>
  16. #include <linux/highmem.h>
  17. #include <linux/printk.h>
  18. #include <linux/bootmem.h>
  19. #include <linux/init.h>
  20. #include <linux/crash_dump.h>
  21. #include <linux/list.h>
  22. #include <linux/vmalloc.h>
  23. #include <linux/pagemap.h>
  24. #include <asm/uaccess.h>
  25. #include <asm/io.h>
  26. #include "internal.h"
  27. /* List representing chunks of contiguous memory areas and their offsets in
  28. * vmcore file.
  29. */
  30. static LIST_HEAD(vmcore_list);
  31. /* Stores the pointer to the buffer containing kernel elf core headers. */
  32. static char *elfcorebuf;
  33. static size_t elfcorebuf_sz;
  34. static size_t elfcorebuf_sz_orig;
  35. static char *elfnotes_buf;
  36. static size_t elfnotes_sz;
  37. /* Total size of vmcore file. */
  38. static u64 vmcore_size;
  39. static struct proc_dir_entry *proc_vmcore;
  40. /*
  41. * Returns > 0 for RAM pages, 0 for non-RAM pages, < 0 on error
  42. * The called function has to take care of module refcounting.
  43. */
  44. static int (*oldmem_pfn_is_ram)(unsigned long pfn);
  45. int register_oldmem_pfn_is_ram(int (*fn)(unsigned long pfn))
  46. {
  47. if (oldmem_pfn_is_ram)
  48. return -EBUSY;
  49. oldmem_pfn_is_ram = fn;
  50. return 0;
  51. }
  52. EXPORT_SYMBOL_GPL(register_oldmem_pfn_is_ram);
  53. void unregister_oldmem_pfn_is_ram(void)
  54. {
  55. oldmem_pfn_is_ram = NULL;
  56. wmb();
  57. }
  58. EXPORT_SYMBOL_GPL(unregister_oldmem_pfn_is_ram);
  59. static int pfn_is_ram(unsigned long pfn)
  60. {
  61. int (*fn)(unsigned long pfn);
  62. /* pfn is ram unless fn() checks pagetype */
  63. int ret = 1;
  64. /*
  65. * Ask hypervisor if the pfn is really ram.
  66. * A ballooned page contains no data and reading from such a page
  67. * will cause high load in the hypervisor.
  68. */
  69. fn = oldmem_pfn_is_ram;
  70. if (fn)
  71. ret = fn(pfn);
  72. return ret;
  73. }
  74. /* Reads a page from the oldmem device from given offset. */
  75. static ssize_t read_from_oldmem(char *buf, size_t count,
  76. u64 *ppos, int userbuf)
  77. {
  78. unsigned long pfn, offset;
  79. size_t nr_bytes;
  80. ssize_t read = 0, tmp;
  81. if (!count)
  82. return 0;
  83. offset = (unsigned long)(*ppos % PAGE_SIZE);
  84. pfn = (unsigned long)(*ppos / PAGE_SIZE);
  85. do {
  86. if (count > (PAGE_SIZE - offset))
  87. nr_bytes = PAGE_SIZE - offset;
  88. else
  89. nr_bytes = count;
  90. /* If pfn is not ram, return zeros for sparse dump files */
  91. if (pfn_is_ram(pfn) == 0)
  92. memset(buf, 0, nr_bytes);
  93. else {
  94. tmp = copy_oldmem_page(pfn, buf, nr_bytes,
  95. offset, userbuf);
  96. if (tmp < 0)
  97. return tmp;
  98. }
  99. *ppos += nr_bytes;
  100. count -= nr_bytes;
  101. buf += nr_bytes;
  102. read += nr_bytes;
  103. ++pfn;
  104. offset = 0;
  105. } while (count);
  106. return read;
  107. }
  108. /*
  109. * Architectures may override this function to allocate ELF header in 2nd kernel
  110. */
  111. int __weak elfcorehdr_alloc(unsigned long long *addr, unsigned long long *size)
  112. {
  113. return 0;
  114. }
  115. /*
  116. * Architectures may override this function to free header
  117. */
  118. void __weak elfcorehdr_free(unsigned long long addr)
  119. {}
  120. /*
  121. * Architectures may override this function to read from ELF header
  122. */
  123. ssize_t __weak elfcorehdr_read(char *buf, size_t count, u64 *ppos)
  124. {
  125. return read_from_oldmem(buf, count, ppos, 0);
  126. }
  127. /*
  128. * Architectures may override this function to read from notes sections
  129. */
  130. ssize_t __weak elfcorehdr_read_notes(char *buf, size_t count, u64 *ppos)
  131. {
  132. return read_from_oldmem(buf, count, ppos, 0);
  133. }
  134. /*
  135. * Architectures may override this function to map oldmem
  136. */
  137. int __weak remap_oldmem_pfn_range(struct vm_area_struct *vma,
  138. unsigned long from, unsigned long pfn,
  139. unsigned long size, pgprot_t prot)
  140. {
  141. return remap_pfn_range(vma, from, pfn, size, prot);
  142. }
  143. /*
  144. * Copy to either kernel or user space
  145. */
  146. static int copy_to(void *target, void *src, size_t size, int userbuf)
  147. {
  148. if (userbuf) {
  149. if (copy_to_user((char __user *) target, src, size))
  150. return -EFAULT;
  151. } else {
  152. memcpy(target, src, size);
  153. }
  154. return 0;
  155. }
  156. /* Read from the ELF header and then the crash dump. On error, negative value is
  157. * returned otherwise number of bytes read are returned.
  158. */
  159. static ssize_t __read_vmcore(char *buffer, size_t buflen, loff_t *fpos,
  160. int userbuf)
  161. {
  162. ssize_t acc = 0, tmp;
  163. size_t tsz;
  164. u64 start;
  165. struct vmcore *m = NULL;
  166. if (buflen == 0 || *fpos >= vmcore_size)
  167. return 0;
  168. /* trim buflen to not go beyond EOF */
  169. if (buflen > vmcore_size - *fpos)
  170. buflen = vmcore_size - *fpos;
  171. /* Read ELF core header */
  172. if (*fpos < elfcorebuf_sz) {
  173. tsz = min(elfcorebuf_sz - (size_t)*fpos, buflen);
  174. if (copy_to(buffer, elfcorebuf + *fpos, tsz, userbuf))
  175. return -EFAULT;
  176. buflen -= tsz;
  177. *fpos += tsz;
  178. buffer += tsz;
  179. acc += tsz;
  180. /* leave now if filled buffer already */
  181. if (buflen == 0)
  182. return acc;
  183. }
  184. /* Read Elf note segment */
  185. if (*fpos < elfcorebuf_sz + elfnotes_sz) {
  186. void *kaddr;
  187. tsz = min(elfcorebuf_sz + elfnotes_sz - (size_t)*fpos, buflen);
  188. kaddr = elfnotes_buf + *fpos - elfcorebuf_sz;
  189. if (copy_to(buffer, kaddr, tsz, userbuf))
  190. return -EFAULT;
  191. buflen -= tsz;
  192. *fpos += tsz;
  193. buffer += tsz;
  194. acc += tsz;
  195. /* leave now if filled buffer already */
  196. if (buflen == 0)
  197. return acc;
  198. }
  199. list_for_each_entry(m, &vmcore_list, list) {
  200. if (*fpos < m->offset + m->size) {
  201. tsz = min_t(size_t, m->offset + m->size - *fpos, buflen);
  202. start = m->paddr + *fpos - m->offset;
  203. tmp = read_from_oldmem(buffer, tsz, &start, userbuf);
  204. if (tmp < 0)
  205. return tmp;
  206. buflen -= tsz;
  207. *fpos += tsz;
  208. buffer += tsz;
  209. acc += tsz;
  210. /* leave now if filled buffer already */
  211. if (buflen == 0)
  212. return acc;
  213. }
  214. }
  215. return acc;
  216. }
  217. static ssize_t read_vmcore(struct file *file, char __user *buffer,
  218. size_t buflen, loff_t *fpos)
  219. {
  220. return __read_vmcore((__force char *) buffer, buflen, fpos, 1);
  221. }
  222. /*
  223. * The vmcore fault handler uses the page cache and fills data using the
  224. * standard __vmcore_read() function.
  225. *
  226. * On s390 the fault handler is used for memory regions that can't be mapped
  227. * directly with remap_pfn_range().
  228. */
  229. static int mmap_vmcore_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  230. {
  231. #ifdef CONFIG_S390
  232. struct address_space *mapping = vma->vm_file->f_mapping;
  233. pgoff_t index = vmf->pgoff;
  234. struct page *page;
  235. loff_t offset;
  236. char *buf;
  237. int rc;
  238. page = find_or_create_page(mapping, index, GFP_KERNEL);
  239. if (!page)
  240. return VM_FAULT_OOM;
  241. if (!PageUptodate(page)) {
  242. offset = (loff_t) index << PAGE_CACHE_SHIFT;
  243. buf = __va((page_to_pfn(page) << PAGE_SHIFT));
  244. rc = __read_vmcore(buf, PAGE_SIZE, &offset, 0);
  245. if (rc < 0) {
  246. unlock_page(page);
  247. page_cache_release(page);
  248. return (rc == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
  249. }
  250. SetPageUptodate(page);
  251. }
  252. unlock_page(page);
  253. vmf->page = page;
  254. return 0;
  255. #else
  256. return VM_FAULT_SIGBUS;
  257. #endif
  258. }
  259. static const struct vm_operations_struct vmcore_mmap_ops = {
  260. .fault = mmap_vmcore_fault,
  261. };
  262. /**
  263. * alloc_elfnotes_buf - allocate buffer for ELF note segment in
  264. * vmalloc memory
  265. *
  266. * @notes_sz: size of buffer
  267. *
  268. * If CONFIG_MMU is defined, use vmalloc_user() to allow users to mmap
  269. * the buffer to user-space by means of remap_vmalloc_range().
  270. *
  271. * If CONFIG_MMU is not defined, use vzalloc() since mmap_vmcore() is
  272. * disabled and there's no need to allow users to mmap the buffer.
  273. */
  274. static inline char *alloc_elfnotes_buf(size_t notes_sz)
  275. {
  276. #ifdef CONFIG_MMU
  277. return vmalloc_user(notes_sz);
  278. #else
  279. return vzalloc(notes_sz);
  280. #endif
  281. }
  282. /*
  283. * Disable mmap_vmcore() if CONFIG_MMU is not defined. MMU is
  284. * essential for mmap_vmcore() in order to map physically
  285. * non-contiguous objects (ELF header, ELF note segment and memory
  286. * regions in the 1st kernel pointed to by PT_LOAD entries) into
  287. * virtually contiguous user-space in ELF layout.
  288. */
  289. #ifdef CONFIG_MMU
  290. /*
  291. * remap_oldmem_pfn_checked - do remap_oldmem_pfn_range replacing all pages
  292. * reported as not being ram with the zero page.
  293. *
  294. * @vma: vm_area_struct describing requested mapping
  295. * @from: start remapping from
  296. * @pfn: page frame number to start remapping to
  297. * @size: remapping size
  298. * @prot: protection bits
  299. *
  300. * Returns zero on success, -EAGAIN on failure.
  301. */
  302. static int remap_oldmem_pfn_checked(struct vm_area_struct *vma,
  303. unsigned long from, unsigned long pfn,
  304. unsigned long size, pgprot_t prot)
  305. {
  306. unsigned long map_size;
  307. unsigned long pos_start, pos_end, pos;
  308. unsigned long zeropage_pfn = my_zero_pfn(0);
  309. size_t len = 0;
  310. pos_start = pfn;
  311. pos_end = pfn + (size >> PAGE_SHIFT);
  312. for (pos = pos_start; pos < pos_end; ++pos) {
  313. if (!pfn_is_ram(pos)) {
  314. /*
  315. * We hit a page which is not ram. Remap the continuous
  316. * region between pos_start and pos-1 and replace
  317. * the non-ram page at pos with the zero page.
  318. */
  319. if (pos > pos_start) {
  320. /* Remap continuous region */
  321. map_size = (pos - pos_start) << PAGE_SHIFT;
  322. if (remap_oldmem_pfn_range(vma, from + len,
  323. pos_start, map_size,
  324. prot))
  325. goto fail;
  326. len += map_size;
  327. }
  328. /* Remap the zero page */
  329. if (remap_oldmem_pfn_range(vma, from + len,
  330. zeropage_pfn,
  331. PAGE_SIZE, prot))
  332. goto fail;
  333. len += PAGE_SIZE;
  334. pos_start = pos + 1;
  335. }
  336. }
  337. if (pos > pos_start) {
  338. /* Remap the rest */
  339. map_size = (pos - pos_start) << PAGE_SHIFT;
  340. if (remap_oldmem_pfn_range(vma, from + len, pos_start,
  341. map_size, prot))
  342. goto fail;
  343. }
  344. return 0;
  345. fail:
  346. do_munmap(vma->vm_mm, from, len);
  347. return -EAGAIN;
  348. }
  349. static int vmcore_remap_oldmem_pfn(struct vm_area_struct *vma,
  350. unsigned long from, unsigned long pfn,
  351. unsigned long size, pgprot_t prot)
  352. {
  353. /*
  354. * Check if oldmem_pfn_is_ram was registered to avoid
  355. * looping over all pages without a reason.
  356. */
  357. if (oldmem_pfn_is_ram)
  358. return remap_oldmem_pfn_checked(vma, from, pfn, size, prot);
  359. else
  360. return remap_oldmem_pfn_range(vma, from, pfn, size, prot);
  361. }
  362. static int mmap_vmcore(struct file *file, struct vm_area_struct *vma)
  363. {
  364. size_t size = vma->vm_end - vma->vm_start;
  365. u64 start, end, len, tsz;
  366. struct vmcore *m;
  367. start = (u64)vma->vm_pgoff << PAGE_SHIFT;
  368. end = start + size;
  369. if (size > vmcore_size || end > vmcore_size)
  370. return -EINVAL;
  371. if (vma->vm_flags & (VM_WRITE | VM_EXEC))
  372. return -EPERM;
  373. vma->vm_flags &= ~(VM_MAYWRITE | VM_MAYEXEC);
  374. vma->vm_flags |= VM_MIXEDMAP;
  375. vma->vm_ops = &vmcore_mmap_ops;
  376. len = 0;
  377. if (start < elfcorebuf_sz) {
  378. u64 pfn;
  379. tsz = min(elfcorebuf_sz - (size_t)start, size);
  380. pfn = __pa(elfcorebuf + start) >> PAGE_SHIFT;
  381. if (remap_pfn_range(vma, vma->vm_start, pfn, tsz,
  382. vma->vm_page_prot))
  383. return -EAGAIN;
  384. size -= tsz;
  385. start += tsz;
  386. len += tsz;
  387. if (size == 0)
  388. return 0;
  389. }
  390. if (start < elfcorebuf_sz + elfnotes_sz) {
  391. void *kaddr;
  392. tsz = min(elfcorebuf_sz + elfnotes_sz - (size_t)start, size);
  393. kaddr = elfnotes_buf + start - elfcorebuf_sz;
  394. if (remap_vmalloc_range_partial(vma, vma->vm_start + len,
  395. kaddr, tsz))
  396. goto fail;
  397. size -= tsz;
  398. start += tsz;
  399. len += tsz;
  400. if (size == 0)
  401. return 0;
  402. }
  403. list_for_each_entry(m, &vmcore_list, list) {
  404. if (start < m->offset + m->size) {
  405. u64 paddr = 0;
  406. tsz = min_t(size_t, m->offset + m->size - start, size);
  407. paddr = m->paddr + start - m->offset;
  408. if (vmcore_remap_oldmem_pfn(vma, vma->vm_start + len,
  409. paddr >> PAGE_SHIFT, tsz,
  410. vma->vm_page_prot))
  411. goto fail;
  412. size -= tsz;
  413. start += tsz;
  414. len += tsz;
  415. if (size == 0)
  416. return 0;
  417. }
  418. }
  419. return 0;
  420. fail:
  421. do_munmap(vma->vm_mm, vma->vm_start, len);
  422. return -EAGAIN;
  423. }
  424. #else
  425. static int mmap_vmcore(struct file *file, struct vm_area_struct *vma)
  426. {
  427. return -ENOSYS;
  428. }
  429. #endif
  430. static const struct file_operations proc_vmcore_operations = {
  431. .read = read_vmcore,
  432. .llseek = default_llseek,
  433. .mmap = mmap_vmcore,
  434. };
  435. static struct vmcore* __init get_new_element(void)
  436. {
  437. return kzalloc(sizeof(struct vmcore), GFP_KERNEL);
  438. }
  439. static u64 __init get_vmcore_size(size_t elfsz, size_t elfnotesegsz,
  440. struct list_head *vc_list)
  441. {
  442. u64 size;
  443. struct vmcore *m;
  444. size = elfsz + elfnotesegsz;
  445. list_for_each_entry(m, vc_list, list) {
  446. size += m->size;
  447. }
  448. return size;
  449. }
  450. /**
  451. * update_note_header_size_elf64 - update p_memsz member of each PT_NOTE entry
  452. *
  453. * @ehdr_ptr: ELF header
  454. *
  455. * This function updates p_memsz member of each PT_NOTE entry in the
  456. * program header table pointed to by @ehdr_ptr to real size of ELF
  457. * note segment.
  458. */
  459. static int __init update_note_header_size_elf64(const Elf64_Ehdr *ehdr_ptr)
  460. {
  461. int i, rc=0;
  462. Elf64_Phdr *phdr_ptr;
  463. Elf64_Nhdr *nhdr_ptr;
  464. phdr_ptr = (Elf64_Phdr *)(ehdr_ptr + 1);
  465. for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
  466. void *notes_section;
  467. u64 offset, max_sz, sz, real_sz = 0;
  468. if (phdr_ptr->p_type != PT_NOTE)
  469. continue;
  470. max_sz = phdr_ptr->p_memsz;
  471. offset = phdr_ptr->p_offset;
  472. notes_section = kmalloc(max_sz, GFP_KERNEL);
  473. if (!notes_section)
  474. return -ENOMEM;
  475. rc = elfcorehdr_read_notes(notes_section, max_sz, &offset);
  476. if (rc < 0) {
  477. kfree(notes_section);
  478. return rc;
  479. }
  480. nhdr_ptr = notes_section;
  481. while (nhdr_ptr->n_namesz != 0) {
  482. sz = sizeof(Elf64_Nhdr) +
  483. (((u64)nhdr_ptr->n_namesz + 3) & ~3) +
  484. (((u64)nhdr_ptr->n_descsz + 3) & ~3);
  485. if ((real_sz + sz) > max_sz) {
  486. pr_warn("Warning: Exceeded p_memsz, dropping PT_NOTE entry n_namesz=0x%x, n_descsz=0x%x\n",
  487. nhdr_ptr->n_namesz, nhdr_ptr->n_descsz);
  488. break;
  489. }
  490. real_sz += sz;
  491. nhdr_ptr = (Elf64_Nhdr*)((char*)nhdr_ptr + sz);
  492. }
  493. kfree(notes_section);
  494. phdr_ptr->p_memsz = real_sz;
  495. if (real_sz == 0) {
  496. pr_warn("Warning: Zero PT_NOTE entries found\n");
  497. }
  498. }
  499. return 0;
  500. }
  501. /**
  502. * get_note_number_and_size_elf64 - get the number of PT_NOTE program
  503. * headers and sum of real size of their ELF note segment headers and
  504. * data.
  505. *
  506. * @ehdr_ptr: ELF header
  507. * @nr_ptnote: buffer for the number of PT_NOTE program headers
  508. * @sz_ptnote: buffer for size of unique PT_NOTE program header
  509. *
  510. * This function is used to merge multiple PT_NOTE program headers
  511. * into a unique single one. The resulting unique entry will have
  512. * @sz_ptnote in its phdr->p_mem.
  513. *
  514. * It is assumed that program headers with PT_NOTE type pointed to by
  515. * @ehdr_ptr has already been updated by update_note_header_size_elf64
  516. * and each of PT_NOTE program headers has actual ELF note segment
  517. * size in its p_memsz member.
  518. */
  519. static int __init get_note_number_and_size_elf64(const Elf64_Ehdr *ehdr_ptr,
  520. int *nr_ptnote, u64 *sz_ptnote)
  521. {
  522. int i;
  523. Elf64_Phdr *phdr_ptr;
  524. *nr_ptnote = *sz_ptnote = 0;
  525. phdr_ptr = (Elf64_Phdr *)(ehdr_ptr + 1);
  526. for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
  527. if (phdr_ptr->p_type != PT_NOTE)
  528. continue;
  529. *nr_ptnote += 1;
  530. *sz_ptnote += phdr_ptr->p_memsz;
  531. }
  532. return 0;
  533. }
  534. /**
  535. * copy_notes_elf64 - copy ELF note segments in a given buffer
  536. *
  537. * @ehdr_ptr: ELF header
  538. * @notes_buf: buffer into which ELF note segments are copied
  539. *
  540. * This function is used to copy ELF note segment in the 1st kernel
  541. * into the buffer @notes_buf in the 2nd kernel. It is assumed that
  542. * size of the buffer @notes_buf is equal to or larger than sum of the
  543. * real ELF note segment headers and data.
  544. *
  545. * It is assumed that program headers with PT_NOTE type pointed to by
  546. * @ehdr_ptr has already been updated by update_note_header_size_elf64
  547. * and each of PT_NOTE program headers has actual ELF note segment
  548. * size in its p_memsz member.
  549. */
  550. static int __init copy_notes_elf64(const Elf64_Ehdr *ehdr_ptr, char *notes_buf)
  551. {
  552. int i, rc=0;
  553. Elf64_Phdr *phdr_ptr;
  554. phdr_ptr = (Elf64_Phdr*)(ehdr_ptr + 1);
  555. for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
  556. u64 offset;
  557. if (phdr_ptr->p_type != PT_NOTE)
  558. continue;
  559. offset = phdr_ptr->p_offset;
  560. rc = elfcorehdr_read_notes(notes_buf, phdr_ptr->p_memsz,
  561. &offset);
  562. if (rc < 0)
  563. return rc;
  564. notes_buf += phdr_ptr->p_memsz;
  565. }
  566. return 0;
  567. }
  568. /* Merges all the PT_NOTE headers into one. */
  569. static int __init merge_note_headers_elf64(char *elfptr, size_t *elfsz,
  570. char **notes_buf, size_t *notes_sz)
  571. {
  572. int i, nr_ptnote=0, rc=0;
  573. char *tmp;
  574. Elf64_Ehdr *ehdr_ptr;
  575. Elf64_Phdr phdr;
  576. u64 phdr_sz = 0, note_off;
  577. ehdr_ptr = (Elf64_Ehdr *)elfptr;
  578. rc = update_note_header_size_elf64(ehdr_ptr);
  579. if (rc < 0)
  580. return rc;
  581. rc = get_note_number_and_size_elf64(ehdr_ptr, &nr_ptnote, &phdr_sz);
  582. if (rc < 0)
  583. return rc;
  584. *notes_sz = roundup(phdr_sz, PAGE_SIZE);
  585. *notes_buf = alloc_elfnotes_buf(*notes_sz);
  586. if (!*notes_buf)
  587. return -ENOMEM;
  588. rc = copy_notes_elf64(ehdr_ptr, *notes_buf);
  589. if (rc < 0)
  590. return rc;
  591. /* Prepare merged PT_NOTE program header. */
  592. phdr.p_type = PT_NOTE;
  593. phdr.p_flags = 0;
  594. note_off = sizeof(Elf64_Ehdr) +
  595. (ehdr_ptr->e_phnum - nr_ptnote +1) * sizeof(Elf64_Phdr);
  596. phdr.p_offset = roundup(note_off, PAGE_SIZE);
  597. phdr.p_vaddr = phdr.p_paddr = 0;
  598. phdr.p_filesz = phdr.p_memsz = phdr_sz;
  599. phdr.p_align = 0;
  600. /* Add merged PT_NOTE program header*/
  601. tmp = elfptr + sizeof(Elf64_Ehdr);
  602. memcpy(tmp, &phdr, sizeof(phdr));
  603. tmp += sizeof(phdr);
  604. /* Remove unwanted PT_NOTE program headers. */
  605. i = (nr_ptnote - 1) * sizeof(Elf64_Phdr);
  606. *elfsz = *elfsz - i;
  607. memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf64_Ehdr)-sizeof(Elf64_Phdr)));
  608. memset(elfptr + *elfsz, 0, i);
  609. *elfsz = roundup(*elfsz, PAGE_SIZE);
  610. /* Modify e_phnum to reflect merged headers. */
  611. ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1;
  612. return 0;
  613. }
  614. /**
  615. * update_note_header_size_elf32 - update p_memsz member of each PT_NOTE entry
  616. *
  617. * @ehdr_ptr: ELF header
  618. *
  619. * This function updates p_memsz member of each PT_NOTE entry in the
  620. * program header table pointed to by @ehdr_ptr to real size of ELF
  621. * note segment.
  622. */
  623. static int __init update_note_header_size_elf32(const Elf32_Ehdr *ehdr_ptr)
  624. {
  625. int i, rc=0;
  626. Elf32_Phdr *phdr_ptr;
  627. Elf32_Nhdr *nhdr_ptr;
  628. phdr_ptr = (Elf32_Phdr *)(ehdr_ptr + 1);
  629. for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
  630. void *notes_section;
  631. u64 offset, max_sz, sz, real_sz = 0;
  632. if (phdr_ptr->p_type != PT_NOTE)
  633. continue;
  634. max_sz = phdr_ptr->p_memsz;
  635. offset = phdr_ptr->p_offset;
  636. notes_section = kmalloc(max_sz, GFP_KERNEL);
  637. if (!notes_section)
  638. return -ENOMEM;
  639. rc = elfcorehdr_read_notes(notes_section, max_sz, &offset);
  640. if (rc < 0) {
  641. kfree(notes_section);
  642. return rc;
  643. }
  644. nhdr_ptr = notes_section;
  645. while (nhdr_ptr->n_namesz != 0) {
  646. sz = sizeof(Elf32_Nhdr) +
  647. (((u64)nhdr_ptr->n_namesz + 3) & ~3) +
  648. (((u64)nhdr_ptr->n_descsz + 3) & ~3);
  649. if ((real_sz + sz) > max_sz) {
  650. pr_warn("Warning: Exceeded p_memsz, dropping PT_NOTE entry n_namesz=0x%x, n_descsz=0x%x\n",
  651. nhdr_ptr->n_namesz, nhdr_ptr->n_descsz);
  652. break;
  653. }
  654. real_sz += sz;
  655. nhdr_ptr = (Elf32_Nhdr*)((char*)nhdr_ptr + sz);
  656. }
  657. kfree(notes_section);
  658. phdr_ptr->p_memsz = real_sz;
  659. if (real_sz == 0) {
  660. pr_warn("Warning: Zero PT_NOTE entries found\n");
  661. }
  662. }
  663. return 0;
  664. }
  665. /**
  666. * get_note_number_and_size_elf32 - get the number of PT_NOTE program
  667. * headers and sum of real size of their ELF note segment headers and
  668. * data.
  669. *
  670. * @ehdr_ptr: ELF header
  671. * @nr_ptnote: buffer for the number of PT_NOTE program headers
  672. * @sz_ptnote: buffer for size of unique PT_NOTE program header
  673. *
  674. * This function is used to merge multiple PT_NOTE program headers
  675. * into a unique single one. The resulting unique entry will have
  676. * @sz_ptnote in its phdr->p_mem.
  677. *
  678. * It is assumed that program headers with PT_NOTE type pointed to by
  679. * @ehdr_ptr has already been updated by update_note_header_size_elf32
  680. * and each of PT_NOTE program headers has actual ELF note segment
  681. * size in its p_memsz member.
  682. */
  683. static int __init get_note_number_and_size_elf32(const Elf32_Ehdr *ehdr_ptr,
  684. int *nr_ptnote, u64 *sz_ptnote)
  685. {
  686. int i;
  687. Elf32_Phdr *phdr_ptr;
  688. *nr_ptnote = *sz_ptnote = 0;
  689. phdr_ptr = (Elf32_Phdr *)(ehdr_ptr + 1);
  690. for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
  691. if (phdr_ptr->p_type != PT_NOTE)
  692. continue;
  693. *nr_ptnote += 1;
  694. *sz_ptnote += phdr_ptr->p_memsz;
  695. }
  696. return 0;
  697. }
  698. /**
  699. * copy_notes_elf32 - copy ELF note segments in a given buffer
  700. *
  701. * @ehdr_ptr: ELF header
  702. * @notes_buf: buffer into which ELF note segments are copied
  703. *
  704. * This function is used to copy ELF note segment in the 1st kernel
  705. * into the buffer @notes_buf in the 2nd kernel. It is assumed that
  706. * size of the buffer @notes_buf is equal to or larger than sum of the
  707. * real ELF note segment headers and data.
  708. *
  709. * It is assumed that program headers with PT_NOTE type pointed to by
  710. * @ehdr_ptr has already been updated by update_note_header_size_elf32
  711. * and each of PT_NOTE program headers has actual ELF note segment
  712. * size in its p_memsz member.
  713. */
  714. static int __init copy_notes_elf32(const Elf32_Ehdr *ehdr_ptr, char *notes_buf)
  715. {
  716. int i, rc=0;
  717. Elf32_Phdr *phdr_ptr;
  718. phdr_ptr = (Elf32_Phdr*)(ehdr_ptr + 1);
  719. for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
  720. u64 offset;
  721. if (phdr_ptr->p_type != PT_NOTE)
  722. continue;
  723. offset = phdr_ptr->p_offset;
  724. rc = elfcorehdr_read_notes(notes_buf, phdr_ptr->p_memsz,
  725. &offset);
  726. if (rc < 0)
  727. return rc;
  728. notes_buf += phdr_ptr->p_memsz;
  729. }
  730. return 0;
  731. }
  732. /* Merges all the PT_NOTE headers into one. */
  733. static int __init merge_note_headers_elf32(char *elfptr, size_t *elfsz,
  734. char **notes_buf, size_t *notes_sz)
  735. {
  736. int i, nr_ptnote=0, rc=0;
  737. char *tmp;
  738. Elf32_Ehdr *ehdr_ptr;
  739. Elf32_Phdr phdr;
  740. u64 phdr_sz = 0, note_off;
  741. ehdr_ptr = (Elf32_Ehdr *)elfptr;
  742. rc = update_note_header_size_elf32(ehdr_ptr);
  743. if (rc < 0)
  744. return rc;
  745. rc = get_note_number_and_size_elf32(ehdr_ptr, &nr_ptnote, &phdr_sz);
  746. if (rc < 0)
  747. return rc;
  748. *notes_sz = roundup(phdr_sz, PAGE_SIZE);
  749. *notes_buf = alloc_elfnotes_buf(*notes_sz);
  750. if (!*notes_buf)
  751. return -ENOMEM;
  752. rc = copy_notes_elf32(ehdr_ptr, *notes_buf);
  753. if (rc < 0)
  754. return rc;
  755. /* Prepare merged PT_NOTE program header. */
  756. phdr.p_type = PT_NOTE;
  757. phdr.p_flags = 0;
  758. note_off = sizeof(Elf32_Ehdr) +
  759. (ehdr_ptr->e_phnum - nr_ptnote +1) * sizeof(Elf32_Phdr);
  760. phdr.p_offset = roundup(note_off, PAGE_SIZE);
  761. phdr.p_vaddr = phdr.p_paddr = 0;
  762. phdr.p_filesz = phdr.p_memsz = phdr_sz;
  763. phdr.p_align = 0;
  764. /* Add merged PT_NOTE program header*/
  765. tmp = elfptr + sizeof(Elf32_Ehdr);
  766. memcpy(tmp, &phdr, sizeof(phdr));
  767. tmp += sizeof(phdr);
  768. /* Remove unwanted PT_NOTE program headers. */
  769. i = (nr_ptnote - 1) * sizeof(Elf32_Phdr);
  770. *elfsz = *elfsz - i;
  771. memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf32_Ehdr)-sizeof(Elf32_Phdr)));
  772. memset(elfptr + *elfsz, 0, i);
  773. *elfsz = roundup(*elfsz, PAGE_SIZE);
  774. /* Modify e_phnum to reflect merged headers. */
  775. ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1;
  776. return 0;
  777. }
  778. /* Add memory chunks represented by program headers to vmcore list. Also update
  779. * the new offset fields of exported program headers. */
  780. static int __init process_ptload_program_headers_elf64(char *elfptr,
  781. size_t elfsz,
  782. size_t elfnotes_sz,
  783. struct list_head *vc_list)
  784. {
  785. int i;
  786. Elf64_Ehdr *ehdr_ptr;
  787. Elf64_Phdr *phdr_ptr;
  788. loff_t vmcore_off;
  789. struct vmcore *new;
  790. ehdr_ptr = (Elf64_Ehdr *)elfptr;
  791. phdr_ptr = (Elf64_Phdr*)(elfptr + sizeof(Elf64_Ehdr)); /* PT_NOTE hdr */
  792. /* Skip Elf header, program headers and Elf note segment. */
  793. vmcore_off = elfsz + elfnotes_sz;
  794. for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
  795. u64 paddr, start, end, size;
  796. if (phdr_ptr->p_type != PT_LOAD)
  797. continue;
  798. paddr = phdr_ptr->p_offset;
  799. start = rounddown(paddr, PAGE_SIZE);
  800. end = roundup(paddr + phdr_ptr->p_memsz, PAGE_SIZE);
  801. size = end - start;
  802. /* Add this contiguous chunk of memory to vmcore list.*/
  803. new = get_new_element();
  804. if (!new)
  805. return -ENOMEM;
  806. new->paddr = start;
  807. new->size = size;
  808. list_add_tail(&new->list, vc_list);
  809. /* Update the program header offset. */
  810. phdr_ptr->p_offset = vmcore_off + (paddr - start);
  811. vmcore_off = vmcore_off + size;
  812. }
  813. return 0;
  814. }
  815. static int __init process_ptload_program_headers_elf32(char *elfptr,
  816. size_t elfsz,
  817. size_t elfnotes_sz,
  818. struct list_head *vc_list)
  819. {
  820. int i;
  821. Elf32_Ehdr *ehdr_ptr;
  822. Elf32_Phdr *phdr_ptr;
  823. loff_t vmcore_off;
  824. struct vmcore *new;
  825. ehdr_ptr = (Elf32_Ehdr *)elfptr;
  826. phdr_ptr = (Elf32_Phdr*)(elfptr + sizeof(Elf32_Ehdr)); /* PT_NOTE hdr */
  827. /* Skip Elf header, program headers and Elf note segment. */
  828. vmcore_off = elfsz + elfnotes_sz;
  829. for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
  830. u64 paddr, start, end, size;
  831. if (phdr_ptr->p_type != PT_LOAD)
  832. continue;
  833. paddr = phdr_ptr->p_offset;
  834. start = rounddown(paddr, PAGE_SIZE);
  835. end = roundup(paddr + phdr_ptr->p_memsz, PAGE_SIZE);
  836. size = end - start;
  837. /* Add this contiguous chunk of memory to vmcore list.*/
  838. new = get_new_element();
  839. if (!new)
  840. return -ENOMEM;
  841. new->paddr = start;
  842. new->size = size;
  843. list_add_tail(&new->list, vc_list);
  844. /* Update the program header offset */
  845. phdr_ptr->p_offset = vmcore_off + (paddr - start);
  846. vmcore_off = vmcore_off + size;
  847. }
  848. return 0;
  849. }
  850. /* Sets offset fields of vmcore elements. */
  851. static void __init set_vmcore_list_offsets(size_t elfsz, size_t elfnotes_sz,
  852. struct list_head *vc_list)
  853. {
  854. loff_t vmcore_off;
  855. struct vmcore *m;
  856. /* Skip Elf header, program headers and Elf note segment. */
  857. vmcore_off = elfsz + elfnotes_sz;
  858. list_for_each_entry(m, vc_list, list) {
  859. m->offset = vmcore_off;
  860. vmcore_off += m->size;
  861. }
  862. }
  863. static void free_elfcorebuf(void)
  864. {
  865. free_pages((unsigned long)elfcorebuf, get_order(elfcorebuf_sz_orig));
  866. elfcorebuf = NULL;
  867. vfree(elfnotes_buf);
  868. elfnotes_buf = NULL;
  869. }
  870. static int __init parse_crash_elf64_headers(void)
  871. {
  872. int rc=0;
  873. Elf64_Ehdr ehdr;
  874. u64 addr;
  875. addr = elfcorehdr_addr;
  876. /* Read Elf header */
  877. rc = elfcorehdr_read((char *)&ehdr, sizeof(Elf64_Ehdr), &addr);
  878. if (rc < 0)
  879. return rc;
  880. /* Do some basic Verification. */
  881. if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
  882. (ehdr.e_type != ET_CORE) ||
  883. !vmcore_elf64_check_arch(&ehdr) ||
  884. ehdr.e_ident[EI_CLASS] != ELFCLASS64 ||
  885. ehdr.e_ident[EI_VERSION] != EV_CURRENT ||
  886. ehdr.e_version != EV_CURRENT ||
  887. ehdr.e_ehsize != sizeof(Elf64_Ehdr) ||
  888. ehdr.e_phentsize != sizeof(Elf64_Phdr) ||
  889. ehdr.e_phnum == 0) {
  890. pr_warn("Warning: Core image elf header is not sane\n");
  891. return -EINVAL;
  892. }
  893. /* Read in all elf headers. */
  894. elfcorebuf_sz_orig = sizeof(Elf64_Ehdr) +
  895. ehdr.e_phnum * sizeof(Elf64_Phdr);
  896. elfcorebuf_sz = elfcorebuf_sz_orig;
  897. elfcorebuf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  898. get_order(elfcorebuf_sz_orig));
  899. if (!elfcorebuf)
  900. return -ENOMEM;
  901. addr = elfcorehdr_addr;
  902. rc = elfcorehdr_read(elfcorebuf, elfcorebuf_sz_orig, &addr);
  903. if (rc < 0)
  904. goto fail;
  905. /* Merge all PT_NOTE headers into one. */
  906. rc = merge_note_headers_elf64(elfcorebuf, &elfcorebuf_sz,
  907. &elfnotes_buf, &elfnotes_sz);
  908. if (rc)
  909. goto fail;
  910. rc = process_ptload_program_headers_elf64(elfcorebuf, elfcorebuf_sz,
  911. elfnotes_sz, &vmcore_list);
  912. if (rc)
  913. goto fail;
  914. set_vmcore_list_offsets(elfcorebuf_sz, elfnotes_sz, &vmcore_list);
  915. return 0;
  916. fail:
  917. free_elfcorebuf();
  918. return rc;
  919. }
  920. static int __init parse_crash_elf32_headers(void)
  921. {
  922. int rc=0;
  923. Elf32_Ehdr ehdr;
  924. u64 addr;
  925. addr = elfcorehdr_addr;
  926. /* Read Elf header */
  927. rc = elfcorehdr_read((char *)&ehdr, sizeof(Elf32_Ehdr), &addr);
  928. if (rc < 0)
  929. return rc;
  930. /* Do some basic Verification. */
  931. if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
  932. (ehdr.e_type != ET_CORE) ||
  933. !elf_check_arch(&ehdr) ||
  934. ehdr.e_ident[EI_CLASS] != ELFCLASS32||
  935. ehdr.e_ident[EI_VERSION] != EV_CURRENT ||
  936. ehdr.e_version != EV_CURRENT ||
  937. ehdr.e_ehsize != sizeof(Elf32_Ehdr) ||
  938. ehdr.e_phentsize != sizeof(Elf32_Phdr) ||
  939. ehdr.e_phnum == 0) {
  940. pr_warn("Warning: Core image elf header is not sane\n");
  941. return -EINVAL;
  942. }
  943. /* Read in all elf headers. */
  944. elfcorebuf_sz_orig = sizeof(Elf32_Ehdr) + ehdr.e_phnum * sizeof(Elf32_Phdr);
  945. elfcorebuf_sz = elfcorebuf_sz_orig;
  946. elfcorebuf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  947. get_order(elfcorebuf_sz_orig));
  948. if (!elfcorebuf)
  949. return -ENOMEM;
  950. addr = elfcorehdr_addr;
  951. rc = elfcorehdr_read(elfcorebuf, elfcorebuf_sz_orig, &addr);
  952. if (rc < 0)
  953. goto fail;
  954. /* Merge all PT_NOTE headers into one. */
  955. rc = merge_note_headers_elf32(elfcorebuf, &elfcorebuf_sz,
  956. &elfnotes_buf, &elfnotes_sz);
  957. if (rc)
  958. goto fail;
  959. rc = process_ptload_program_headers_elf32(elfcorebuf, elfcorebuf_sz,
  960. elfnotes_sz, &vmcore_list);
  961. if (rc)
  962. goto fail;
  963. set_vmcore_list_offsets(elfcorebuf_sz, elfnotes_sz, &vmcore_list);
  964. return 0;
  965. fail:
  966. free_elfcorebuf();
  967. return rc;
  968. }
  969. static int __init parse_crash_elf_headers(void)
  970. {
  971. unsigned char e_ident[EI_NIDENT];
  972. u64 addr;
  973. int rc=0;
  974. addr = elfcorehdr_addr;
  975. rc = elfcorehdr_read(e_ident, EI_NIDENT, &addr);
  976. if (rc < 0)
  977. return rc;
  978. if (memcmp(e_ident, ELFMAG, SELFMAG) != 0) {
  979. pr_warn("Warning: Core image elf header not found\n");
  980. return -EINVAL;
  981. }
  982. if (e_ident[EI_CLASS] == ELFCLASS64) {
  983. rc = parse_crash_elf64_headers();
  984. if (rc)
  985. return rc;
  986. } else if (e_ident[EI_CLASS] == ELFCLASS32) {
  987. rc = parse_crash_elf32_headers();
  988. if (rc)
  989. return rc;
  990. } else {
  991. pr_warn("Warning: Core image elf header is not sane\n");
  992. return -EINVAL;
  993. }
  994. /* Determine vmcore size. */
  995. vmcore_size = get_vmcore_size(elfcorebuf_sz, elfnotes_sz,
  996. &vmcore_list);
  997. return 0;
  998. }
  999. /* Init function for vmcore module. */
  1000. static int __init vmcore_init(void)
  1001. {
  1002. int rc = 0;
  1003. /* Allow architectures to allocate ELF header in 2nd kernel */
  1004. rc = elfcorehdr_alloc(&elfcorehdr_addr, &elfcorehdr_size);
  1005. if (rc)
  1006. return rc;
  1007. /*
  1008. * If elfcorehdr= has been passed in cmdline or created in 2nd kernel,
  1009. * then capture the dump.
  1010. */
  1011. if (!(is_vmcore_usable()))
  1012. return rc;
  1013. rc = parse_crash_elf_headers();
  1014. if (rc) {
  1015. pr_warn("Kdump: vmcore not initialized\n");
  1016. return rc;
  1017. }
  1018. elfcorehdr_free(elfcorehdr_addr);
  1019. elfcorehdr_addr = ELFCORE_ADDR_ERR;
  1020. proc_vmcore = proc_create("vmcore", S_IRUSR, NULL, &proc_vmcore_operations);
  1021. if (proc_vmcore)
  1022. proc_vmcore->size = vmcore_size;
  1023. return 0;
  1024. }
  1025. fs_initcall(vmcore_init);
  1026. /* Cleanup function for vmcore module. */
  1027. void vmcore_cleanup(void)
  1028. {
  1029. struct list_head *pos, *next;
  1030. if (proc_vmcore) {
  1031. proc_remove(proc_vmcore);
  1032. proc_vmcore = NULL;
  1033. }
  1034. /* clear the vmcore list. */
  1035. list_for_each_safe(pos, next, &vmcore_list) {
  1036. struct vmcore *m;
  1037. m = list_entry(pos, struct vmcore, list);
  1038. list_del(&m->list);
  1039. kfree(m);
  1040. }
  1041. free_elfcorebuf();
  1042. }