vmcore.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121
  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 = NULL;
  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. static int mmap_vmcore(struct file *file, struct vm_area_struct *vma)
  291. {
  292. size_t size = vma->vm_end - vma->vm_start;
  293. u64 start, end, len, tsz;
  294. struct vmcore *m;
  295. start = (u64)vma->vm_pgoff << PAGE_SHIFT;
  296. end = start + size;
  297. if (size > vmcore_size || end > vmcore_size)
  298. return -EINVAL;
  299. if (vma->vm_flags & (VM_WRITE | VM_EXEC))
  300. return -EPERM;
  301. vma->vm_flags &= ~(VM_MAYWRITE | VM_MAYEXEC);
  302. vma->vm_flags |= VM_MIXEDMAP;
  303. vma->vm_ops = &vmcore_mmap_ops;
  304. len = 0;
  305. if (start < elfcorebuf_sz) {
  306. u64 pfn;
  307. tsz = min(elfcorebuf_sz - (size_t)start, size);
  308. pfn = __pa(elfcorebuf + start) >> PAGE_SHIFT;
  309. if (remap_pfn_range(vma, vma->vm_start, pfn, tsz,
  310. vma->vm_page_prot))
  311. return -EAGAIN;
  312. size -= tsz;
  313. start += tsz;
  314. len += tsz;
  315. if (size == 0)
  316. return 0;
  317. }
  318. if (start < elfcorebuf_sz + elfnotes_sz) {
  319. void *kaddr;
  320. tsz = min(elfcorebuf_sz + elfnotes_sz - (size_t)start, size);
  321. kaddr = elfnotes_buf + start - elfcorebuf_sz;
  322. if (remap_vmalloc_range_partial(vma, vma->vm_start + len,
  323. kaddr, tsz))
  324. goto fail;
  325. size -= tsz;
  326. start += tsz;
  327. len += tsz;
  328. if (size == 0)
  329. return 0;
  330. }
  331. list_for_each_entry(m, &vmcore_list, list) {
  332. if (start < m->offset + m->size) {
  333. u64 paddr = 0;
  334. tsz = min_t(size_t, m->offset + m->size - start, size);
  335. paddr = m->paddr + start - m->offset;
  336. if (remap_oldmem_pfn_range(vma, vma->vm_start + len,
  337. paddr >> PAGE_SHIFT, tsz,
  338. vma->vm_page_prot))
  339. goto fail;
  340. size -= tsz;
  341. start += tsz;
  342. len += tsz;
  343. if (size == 0)
  344. return 0;
  345. }
  346. }
  347. return 0;
  348. fail:
  349. do_munmap(vma->vm_mm, vma->vm_start, len);
  350. return -EAGAIN;
  351. }
  352. #else
  353. static int mmap_vmcore(struct file *file, struct vm_area_struct *vma)
  354. {
  355. return -ENOSYS;
  356. }
  357. #endif
  358. static const struct file_operations proc_vmcore_operations = {
  359. .read = read_vmcore,
  360. .llseek = default_llseek,
  361. .mmap = mmap_vmcore,
  362. };
  363. static struct vmcore* __init get_new_element(void)
  364. {
  365. return kzalloc(sizeof(struct vmcore), GFP_KERNEL);
  366. }
  367. static u64 __init get_vmcore_size(size_t elfsz, size_t elfnotesegsz,
  368. struct list_head *vc_list)
  369. {
  370. u64 size;
  371. struct vmcore *m;
  372. size = elfsz + elfnotesegsz;
  373. list_for_each_entry(m, vc_list, list) {
  374. size += m->size;
  375. }
  376. return size;
  377. }
  378. /**
  379. * update_note_header_size_elf64 - update p_memsz member of each PT_NOTE entry
  380. *
  381. * @ehdr_ptr: ELF header
  382. *
  383. * This function updates p_memsz member of each PT_NOTE entry in the
  384. * program header table pointed to by @ehdr_ptr to real size of ELF
  385. * note segment.
  386. */
  387. static int __init update_note_header_size_elf64(const Elf64_Ehdr *ehdr_ptr)
  388. {
  389. int i, rc=0;
  390. Elf64_Phdr *phdr_ptr;
  391. Elf64_Nhdr *nhdr_ptr;
  392. phdr_ptr = (Elf64_Phdr *)(ehdr_ptr + 1);
  393. for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
  394. void *notes_section;
  395. u64 offset, max_sz, sz, real_sz = 0;
  396. if (phdr_ptr->p_type != PT_NOTE)
  397. continue;
  398. max_sz = phdr_ptr->p_memsz;
  399. offset = phdr_ptr->p_offset;
  400. notes_section = kmalloc(max_sz, GFP_KERNEL);
  401. if (!notes_section)
  402. return -ENOMEM;
  403. rc = elfcorehdr_read_notes(notes_section, max_sz, &offset);
  404. if (rc < 0) {
  405. kfree(notes_section);
  406. return rc;
  407. }
  408. nhdr_ptr = notes_section;
  409. while (nhdr_ptr->n_namesz != 0) {
  410. sz = sizeof(Elf64_Nhdr) +
  411. ((nhdr_ptr->n_namesz + 3) & ~3) +
  412. ((nhdr_ptr->n_descsz + 3) & ~3);
  413. if ((real_sz + sz) > max_sz) {
  414. pr_warn("Warning: Exceeded p_memsz, dropping PT_NOTE entry n_namesz=0x%x, n_descsz=0x%x\n",
  415. nhdr_ptr->n_namesz, nhdr_ptr->n_descsz);
  416. break;
  417. }
  418. real_sz += sz;
  419. nhdr_ptr = (Elf64_Nhdr*)((char*)nhdr_ptr + sz);
  420. }
  421. kfree(notes_section);
  422. phdr_ptr->p_memsz = real_sz;
  423. if (real_sz == 0) {
  424. pr_warn("Warning: Zero PT_NOTE entries found\n");
  425. return -EINVAL;
  426. }
  427. }
  428. return 0;
  429. }
  430. /**
  431. * get_note_number_and_size_elf64 - get the number of PT_NOTE program
  432. * headers and sum of real size of their ELF note segment headers and
  433. * data.
  434. *
  435. * @ehdr_ptr: ELF header
  436. * @nr_ptnote: buffer for the number of PT_NOTE program headers
  437. * @sz_ptnote: buffer for size of unique PT_NOTE program header
  438. *
  439. * This function is used to merge multiple PT_NOTE program headers
  440. * into a unique single one. The resulting unique entry will have
  441. * @sz_ptnote in its phdr->p_mem.
  442. *
  443. * It is assumed that program headers with PT_NOTE type pointed to by
  444. * @ehdr_ptr has already been updated by update_note_header_size_elf64
  445. * and each of PT_NOTE program headers has actual ELF note segment
  446. * size in its p_memsz member.
  447. */
  448. static int __init get_note_number_and_size_elf64(const Elf64_Ehdr *ehdr_ptr,
  449. int *nr_ptnote, u64 *sz_ptnote)
  450. {
  451. int i;
  452. Elf64_Phdr *phdr_ptr;
  453. *nr_ptnote = *sz_ptnote = 0;
  454. phdr_ptr = (Elf64_Phdr *)(ehdr_ptr + 1);
  455. for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
  456. if (phdr_ptr->p_type != PT_NOTE)
  457. continue;
  458. *nr_ptnote += 1;
  459. *sz_ptnote += phdr_ptr->p_memsz;
  460. }
  461. return 0;
  462. }
  463. /**
  464. * copy_notes_elf64 - copy ELF note segments in a given buffer
  465. *
  466. * @ehdr_ptr: ELF header
  467. * @notes_buf: buffer into which ELF note segments are copied
  468. *
  469. * This function is used to copy ELF note segment in the 1st kernel
  470. * into the buffer @notes_buf in the 2nd kernel. It is assumed that
  471. * size of the buffer @notes_buf is equal to or larger than sum of the
  472. * real ELF note segment headers and data.
  473. *
  474. * It is assumed that program headers with PT_NOTE type pointed to by
  475. * @ehdr_ptr has already been updated by update_note_header_size_elf64
  476. * and each of PT_NOTE program headers has actual ELF note segment
  477. * size in its p_memsz member.
  478. */
  479. static int __init copy_notes_elf64(const Elf64_Ehdr *ehdr_ptr, char *notes_buf)
  480. {
  481. int i, rc=0;
  482. Elf64_Phdr *phdr_ptr;
  483. phdr_ptr = (Elf64_Phdr*)(ehdr_ptr + 1);
  484. for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
  485. u64 offset;
  486. if (phdr_ptr->p_type != PT_NOTE)
  487. continue;
  488. offset = phdr_ptr->p_offset;
  489. rc = elfcorehdr_read_notes(notes_buf, phdr_ptr->p_memsz,
  490. &offset);
  491. if (rc < 0)
  492. return rc;
  493. notes_buf += phdr_ptr->p_memsz;
  494. }
  495. return 0;
  496. }
  497. /* Merges all the PT_NOTE headers into one. */
  498. static int __init merge_note_headers_elf64(char *elfptr, size_t *elfsz,
  499. char **notes_buf, size_t *notes_sz)
  500. {
  501. int i, nr_ptnote=0, rc=0;
  502. char *tmp;
  503. Elf64_Ehdr *ehdr_ptr;
  504. Elf64_Phdr phdr;
  505. u64 phdr_sz = 0, note_off;
  506. ehdr_ptr = (Elf64_Ehdr *)elfptr;
  507. rc = update_note_header_size_elf64(ehdr_ptr);
  508. if (rc < 0)
  509. return rc;
  510. rc = get_note_number_and_size_elf64(ehdr_ptr, &nr_ptnote, &phdr_sz);
  511. if (rc < 0)
  512. return rc;
  513. *notes_sz = roundup(phdr_sz, PAGE_SIZE);
  514. *notes_buf = alloc_elfnotes_buf(*notes_sz);
  515. if (!*notes_buf)
  516. return -ENOMEM;
  517. rc = copy_notes_elf64(ehdr_ptr, *notes_buf);
  518. if (rc < 0)
  519. return rc;
  520. /* Prepare merged PT_NOTE program header. */
  521. phdr.p_type = PT_NOTE;
  522. phdr.p_flags = 0;
  523. note_off = sizeof(Elf64_Ehdr) +
  524. (ehdr_ptr->e_phnum - nr_ptnote +1) * sizeof(Elf64_Phdr);
  525. phdr.p_offset = roundup(note_off, PAGE_SIZE);
  526. phdr.p_vaddr = phdr.p_paddr = 0;
  527. phdr.p_filesz = phdr.p_memsz = phdr_sz;
  528. phdr.p_align = 0;
  529. /* Add merged PT_NOTE program header*/
  530. tmp = elfptr + sizeof(Elf64_Ehdr);
  531. memcpy(tmp, &phdr, sizeof(phdr));
  532. tmp += sizeof(phdr);
  533. /* Remove unwanted PT_NOTE program headers. */
  534. i = (nr_ptnote - 1) * sizeof(Elf64_Phdr);
  535. *elfsz = *elfsz - i;
  536. memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf64_Ehdr)-sizeof(Elf64_Phdr)));
  537. memset(elfptr + *elfsz, 0, i);
  538. *elfsz = roundup(*elfsz, PAGE_SIZE);
  539. /* Modify e_phnum to reflect merged headers. */
  540. ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1;
  541. return 0;
  542. }
  543. /**
  544. * update_note_header_size_elf32 - update p_memsz member of each PT_NOTE entry
  545. *
  546. * @ehdr_ptr: ELF header
  547. *
  548. * This function updates p_memsz member of each PT_NOTE entry in the
  549. * program header table pointed to by @ehdr_ptr to real size of ELF
  550. * note segment.
  551. */
  552. static int __init update_note_header_size_elf32(const Elf32_Ehdr *ehdr_ptr)
  553. {
  554. int i, rc=0;
  555. Elf32_Phdr *phdr_ptr;
  556. Elf32_Nhdr *nhdr_ptr;
  557. phdr_ptr = (Elf32_Phdr *)(ehdr_ptr + 1);
  558. for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
  559. void *notes_section;
  560. u64 offset, max_sz, sz, real_sz = 0;
  561. if (phdr_ptr->p_type != PT_NOTE)
  562. continue;
  563. max_sz = phdr_ptr->p_memsz;
  564. offset = phdr_ptr->p_offset;
  565. notes_section = kmalloc(max_sz, GFP_KERNEL);
  566. if (!notes_section)
  567. return -ENOMEM;
  568. rc = elfcorehdr_read_notes(notes_section, max_sz, &offset);
  569. if (rc < 0) {
  570. kfree(notes_section);
  571. return rc;
  572. }
  573. nhdr_ptr = notes_section;
  574. while (nhdr_ptr->n_namesz != 0) {
  575. sz = sizeof(Elf32_Nhdr) +
  576. ((nhdr_ptr->n_namesz + 3) & ~3) +
  577. ((nhdr_ptr->n_descsz + 3) & ~3);
  578. if ((real_sz + sz) > max_sz) {
  579. pr_warn("Warning: Exceeded p_memsz, dropping PT_NOTE entry n_namesz=0x%x, n_descsz=0x%x\n",
  580. nhdr_ptr->n_namesz, nhdr_ptr->n_descsz);
  581. break;
  582. }
  583. real_sz += sz;
  584. nhdr_ptr = (Elf32_Nhdr*)((char*)nhdr_ptr + sz);
  585. }
  586. kfree(notes_section);
  587. phdr_ptr->p_memsz = real_sz;
  588. if (real_sz == 0) {
  589. pr_warn("Warning: Zero PT_NOTE entries found\n");
  590. return -EINVAL;
  591. }
  592. }
  593. return 0;
  594. }
  595. /**
  596. * get_note_number_and_size_elf32 - get the number of PT_NOTE program
  597. * headers and sum of real size of their ELF note segment headers and
  598. * data.
  599. *
  600. * @ehdr_ptr: ELF header
  601. * @nr_ptnote: buffer for the number of PT_NOTE program headers
  602. * @sz_ptnote: buffer for size of unique PT_NOTE program header
  603. *
  604. * This function is used to merge multiple PT_NOTE program headers
  605. * into a unique single one. The resulting unique entry will have
  606. * @sz_ptnote in its phdr->p_mem.
  607. *
  608. * It is assumed that program headers with PT_NOTE type pointed to by
  609. * @ehdr_ptr has already been updated by update_note_header_size_elf32
  610. * and each of PT_NOTE program headers has actual ELF note segment
  611. * size in its p_memsz member.
  612. */
  613. static int __init get_note_number_and_size_elf32(const Elf32_Ehdr *ehdr_ptr,
  614. int *nr_ptnote, u64 *sz_ptnote)
  615. {
  616. int i;
  617. Elf32_Phdr *phdr_ptr;
  618. *nr_ptnote = *sz_ptnote = 0;
  619. phdr_ptr = (Elf32_Phdr *)(ehdr_ptr + 1);
  620. for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
  621. if (phdr_ptr->p_type != PT_NOTE)
  622. continue;
  623. *nr_ptnote += 1;
  624. *sz_ptnote += phdr_ptr->p_memsz;
  625. }
  626. return 0;
  627. }
  628. /**
  629. * copy_notes_elf32 - copy ELF note segments in a given buffer
  630. *
  631. * @ehdr_ptr: ELF header
  632. * @notes_buf: buffer into which ELF note segments are copied
  633. *
  634. * This function is used to copy ELF note segment in the 1st kernel
  635. * into the buffer @notes_buf in the 2nd kernel. It is assumed that
  636. * size of the buffer @notes_buf is equal to or larger than sum of the
  637. * real ELF note segment headers and data.
  638. *
  639. * It is assumed that program headers with PT_NOTE type pointed to by
  640. * @ehdr_ptr has already been updated by update_note_header_size_elf32
  641. * and each of PT_NOTE program headers has actual ELF note segment
  642. * size in its p_memsz member.
  643. */
  644. static int __init copy_notes_elf32(const Elf32_Ehdr *ehdr_ptr, char *notes_buf)
  645. {
  646. int i, rc=0;
  647. Elf32_Phdr *phdr_ptr;
  648. phdr_ptr = (Elf32_Phdr*)(ehdr_ptr + 1);
  649. for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
  650. u64 offset;
  651. if (phdr_ptr->p_type != PT_NOTE)
  652. continue;
  653. offset = phdr_ptr->p_offset;
  654. rc = elfcorehdr_read_notes(notes_buf, phdr_ptr->p_memsz,
  655. &offset);
  656. if (rc < 0)
  657. return rc;
  658. notes_buf += phdr_ptr->p_memsz;
  659. }
  660. return 0;
  661. }
  662. /* Merges all the PT_NOTE headers into one. */
  663. static int __init merge_note_headers_elf32(char *elfptr, size_t *elfsz,
  664. char **notes_buf, size_t *notes_sz)
  665. {
  666. int i, nr_ptnote=0, rc=0;
  667. char *tmp;
  668. Elf32_Ehdr *ehdr_ptr;
  669. Elf32_Phdr phdr;
  670. u64 phdr_sz = 0, note_off;
  671. ehdr_ptr = (Elf32_Ehdr *)elfptr;
  672. rc = update_note_header_size_elf32(ehdr_ptr);
  673. if (rc < 0)
  674. return rc;
  675. rc = get_note_number_and_size_elf32(ehdr_ptr, &nr_ptnote, &phdr_sz);
  676. if (rc < 0)
  677. return rc;
  678. *notes_sz = roundup(phdr_sz, PAGE_SIZE);
  679. *notes_buf = alloc_elfnotes_buf(*notes_sz);
  680. if (!*notes_buf)
  681. return -ENOMEM;
  682. rc = copy_notes_elf32(ehdr_ptr, *notes_buf);
  683. if (rc < 0)
  684. return rc;
  685. /* Prepare merged PT_NOTE program header. */
  686. phdr.p_type = PT_NOTE;
  687. phdr.p_flags = 0;
  688. note_off = sizeof(Elf32_Ehdr) +
  689. (ehdr_ptr->e_phnum - nr_ptnote +1) * sizeof(Elf32_Phdr);
  690. phdr.p_offset = roundup(note_off, PAGE_SIZE);
  691. phdr.p_vaddr = phdr.p_paddr = 0;
  692. phdr.p_filesz = phdr.p_memsz = phdr_sz;
  693. phdr.p_align = 0;
  694. /* Add merged PT_NOTE program header*/
  695. tmp = elfptr + sizeof(Elf32_Ehdr);
  696. memcpy(tmp, &phdr, sizeof(phdr));
  697. tmp += sizeof(phdr);
  698. /* Remove unwanted PT_NOTE program headers. */
  699. i = (nr_ptnote - 1) * sizeof(Elf32_Phdr);
  700. *elfsz = *elfsz - i;
  701. memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf32_Ehdr)-sizeof(Elf32_Phdr)));
  702. memset(elfptr + *elfsz, 0, i);
  703. *elfsz = roundup(*elfsz, PAGE_SIZE);
  704. /* Modify e_phnum to reflect merged headers. */
  705. ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1;
  706. return 0;
  707. }
  708. /* Add memory chunks represented by program headers to vmcore list. Also update
  709. * the new offset fields of exported program headers. */
  710. static int __init process_ptload_program_headers_elf64(char *elfptr,
  711. size_t elfsz,
  712. size_t elfnotes_sz,
  713. struct list_head *vc_list)
  714. {
  715. int i;
  716. Elf64_Ehdr *ehdr_ptr;
  717. Elf64_Phdr *phdr_ptr;
  718. loff_t vmcore_off;
  719. struct vmcore *new;
  720. ehdr_ptr = (Elf64_Ehdr *)elfptr;
  721. phdr_ptr = (Elf64_Phdr*)(elfptr + sizeof(Elf64_Ehdr)); /* PT_NOTE hdr */
  722. /* Skip Elf header, program headers and Elf note segment. */
  723. vmcore_off = elfsz + elfnotes_sz;
  724. for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
  725. u64 paddr, start, end, size;
  726. if (phdr_ptr->p_type != PT_LOAD)
  727. continue;
  728. paddr = phdr_ptr->p_offset;
  729. start = rounddown(paddr, PAGE_SIZE);
  730. end = roundup(paddr + phdr_ptr->p_memsz, PAGE_SIZE);
  731. size = end - start;
  732. /* Add this contiguous chunk of memory to vmcore list.*/
  733. new = get_new_element();
  734. if (!new)
  735. return -ENOMEM;
  736. new->paddr = start;
  737. new->size = size;
  738. list_add_tail(&new->list, vc_list);
  739. /* Update the program header offset. */
  740. phdr_ptr->p_offset = vmcore_off + (paddr - start);
  741. vmcore_off = vmcore_off + size;
  742. }
  743. return 0;
  744. }
  745. static int __init process_ptload_program_headers_elf32(char *elfptr,
  746. size_t elfsz,
  747. size_t elfnotes_sz,
  748. struct list_head *vc_list)
  749. {
  750. int i;
  751. Elf32_Ehdr *ehdr_ptr;
  752. Elf32_Phdr *phdr_ptr;
  753. loff_t vmcore_off;
  754. struct vmcore *new;
  755. ehdr_ptr = (Elf32_Ehdr *)elfptr;
  756. phdr_ptr = (Elf32_Phdr*)(elfptr + sizeof(Elf32_Ehdr)); /* PT_NOTE hdr */
  757. /* Skip Elf header, program headers and Elf note segment. */
  758. vmcore_off = elfsz + elfnotes_sz;
  759. for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
  760. u64 paddr, start, end, size;
  761. if (phdr_ptr->p_type != PT_LOAD)
  762. continue;
  763. paddr = phdr_ptr->p_offset;
  764. start = rounddown(paddr, PAGE_SIZE);
  765. end = roundup(paddr + phdr_ptr->p_memsz, PAGE_SIZE);
  766. size = end - start;
  767. /* Add this contiguous chunk of memory to vmcore list.*/
  768. new = get_new_element();
  769. if (!new)
  770. return -ENOMEM;
  771. new->paddr = start;
  772. new->size = size;
  773. list_add_tail(&new->list, vc_list);
  774. /* Update the program header offset */
  775. phdr_ptr->p_offset = vmcore_off + (paddr - start);
  776. vmcore_off = vmcore_off + size;
  777. }
  778. return 0;
  779. }
  780. /* Sets offset fields of vmcore elements. */
  781. static void __init set_vmcore_list_offsets(size_t elfsz, size_t elfnotes_sz,
  782. struct list_head *vc_list)
  783. {
  784. loff_t vmcore_off;
  785. struct vmcore *m;
  786. /* Skip Elf header, program headers and Elf note segment. */
  787. vmcore_off = elfsz + elfnotes_sz;
  788. list_for_each_entry(m, vc_list, list) {
  789. m->offset = vmcore_off;
  790. vmcore_off += m->size;
  791. }
  792. }
  793. static void free_elfcorebuf(void)
  794. {
  795. free_pages((unsigned long)elfcorebuf, get_order(elfcorebuf_sz_orig));
  796. elfcorebuf = NULL;
  797. vfree(elfnotes_buf);
  798. elfnotes_buf = NULL;
  799. }
  800. static int __init parse_crash_elf64_headers(void)
  801. {
  802. int rc=0;
  803. Elf64_Ehdr ehdr;
  804. u64 addr;
  805. addr = elfcorehdr_addr;
  806. /* Read Elf header */
  807. rc = elfcorehdr_read((char *)&ehdr, sizeof(Elf64_Ehdr), &addr);
  808. if (rc < 0)
  809. return rc;
  810. /* Do some basic Verification. */
  811. if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
  812. (ehdr.e_type != ET_CORE) ||
  813. !vmcore_elf64_check_arch(&ehdr) ||
  814. ehdr.e_ident[EI_CLASS] != ELFCLASS64 ||
  815. ehdr.e_ident[EI_VERSION] != EV_CURRENT ||
  816. ehdr.e_version != EV_CURRENT ||
  817. ehdr.e_ehsize != sizeof(Elf64_Ehdr) ||
  818. ehdr.e_phentsize != sizeof(Elf64_Phdr) ||
  819. ehdr.e_phnum == 0) {
  820. pr_warn("Warning: Core image elf header is not sane\n");
  821. return -EINVAL;
  822. }
  823. /* Read in all elf headers. */
  824. elfcorebuf_sz_orig = sizeof(Elf64_Ehdr) +
  825. ehdr.e_phnum * sizeof(Elf64_Phdr);
  826. elfcorebuf_sz = elfcorebuf_sz_orig;
  827. elfcorebuf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  828. get_order(elfcorebuf_sz_orig));
  829. if (!elfcorebuf)
  830. return -ENOMEM;
  831. addr = elfcorehdr_addr;
  832. rc = elfcorehdr_read(elfcorebuf, elfcorebuf_sz_orig, &addr);
  833. if (rc < 0)
  834. goto fail;
  835. /* Merge all PT_NOTE headers into one. */
  836. rc = merge_note_headers_elf64(elfcorebuf, &elfcorebuf_sz,
  837. &elfnotes_buf, &elfnotes_sz);
  838. if (rc)
  839. goto fail;
  840. rc = process_ptload_program_headers_elf64(elfcorebuf, elfcorebuf_sz,
  841. elfnotes_sz, &vmcore_list);
  842. if (rc)
  843. goto fail;
  844. set_vmcore_list_offsets(elfcorebuf_sz, elfnotes_sz, &vmcore_list);
  845. return 0;
  846. fail:
  847. free_elfcorebuf();
  848. return rc;
  849. }
  850. static int __init parse_crash_elf32_headers(void)
  851. {
  852. int rc=0;
  853. Elf32_Ehdr ehdr;
  854. u64 addr;
  855. addr = elfcorehdr_addr;
  856. /* Read Elf header */
  857. rc = elfcorehdr_read((char *)&ehdr, sizeof(Elf32_Ehdr), &addr);
  858. if (rc < 0)
  859. return rc;
  860. /* Do some basic Verification. */
  861. if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
  862. (ehdr.e_type != ET_CORE) ||
  863. !elf_check_arch(&ehdr) ||
  864. ehdr.e_ident[EI_CLASS] != ELFCLASS32||
  865. ehdr.e_ident[EI_VERSION] != EV_CURRENT ||
  866. ehdr.e_version != EV_CURRENT ||
  867. ehdr.e_ehsize != sizeof(Elf32_Ehdr) ||
  868. ehdr.e_phentsize != sizeof(Elf32_Phdr) ||
  869. ehdr.e_phnum == 0) {
  870. pr_warn("Warning: Core image elf header is not sane\n");
  871. return -EINVAL;
  872. }
  873. /* Read in all elf headers. */
  874. elfcorebuf_sz_orig = sizeof(Elf32_Ehdr) + ehdr.e_phnum * sizeof(Elf32_Phdr);
  875. elfcorebuf_sz = elfcorebuf_sz_orig;
  876. elfcorebuf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  877. get_order(elfcorebuf_sz_orig));
  878. if (!elfcorebuf)
  879. return -ENOMEM;
  880. addr = elfcorehdr_addr;
  881. rc = elfcorehdr_read(elfcorebuf, elfcorebuf_sz_orig, &addr);
  882. if (rc < 0)
  883. goto fail;
  884. /* Merge all PT_NOTE headers into one. */
  885. rc = merge_note_headers_elf32(elfcorebuf, &elfcorebuf_sz,
  886. &elfnotes_buf, &elfnotes_sz);
  887. if (rc)
  888. goto fail;
  889. rc = process_ptload_program_headers_elf32(elfcorebuf, elfcorebuf_sz,
  890. elfnotes_sz, &vmcore_list);
  891. if (rc)
  892. goto fail;
  893. set_vmcore_list_offsets(elfcorebuf_sz, elfnotes_sz, &vmcore_list);
  894. return 0;
  895. fail:
  896. free_elfcorebuf();
  897. return rc;
  898. }
  899. static int __init parse_crash_elf_headers(void)
  900. {
  901. unsigned char e_ident[EI_NIDENT];
  902. u64 addr;
  903. int rc=0;
  904. addr = elfcorehdr_addr;
  905. rc = elfcorehdr_read(e_ident, EI_NIDENT, &addr);
  906. if (rc < 0)
  907. return rc;
  908. if (memcmp(e_ident, ELFMAG, SELFMAG) != 0) {
  909. pr_warn("Warning: Core image elf header not found\n");
  910. return -EINVAL;
  911. }
  912. if (e_ident[EI_CLASS] == ELFCLASS64) {
  913. rc = parse_crash_elf64_headers();
  914. if (rc)
  915. return rc;
  916. } else if (e_ident[EI_CLASS] == ELFCLASS32) {
  917. rc = parse_crash_elf32_headers();
  918. if (rc)
  919. return rc;
  920. } else {
  921. pr_warn("Warning: Core image elf header is not sane\n");
  922. return -EINVAL;
  923. }
  924. /* Determine vmcore size. */
  925. vmcore_size = get_vmcore_size(elfcorebuf_sz, elfnotes_sz,
  926. &vmcore_list);
  927. return 0;
  928. }
  929. /* Init function for vmcore module. */
  930. static int __init vmcore_init(void)
  931. {
  932. int rc = 0;
  933. /* Allow architectures to allocate ELF header in 2nd kernel */
  934. rc = elfcorehdr_alloc(&elfcorehdr_addr, &elfcorehdr_size);
  935. if (rc)
  936. return rc;
  937. /*
  938. * If elfcorehdr= has been passed in cmdline or created in 2nd kernel,
  939. * then capture the dump.
  940. */
  941. if (!(is_vmcore_usable()))
  942. return rc;
  943. rc = parse_crash_elf_headers();
  944. if (rc) {
  945. pr_warn("Kdump: vmcore not initialized\n");
  946. return rc;
  947. }
  948. elfcorehdr_free(elfcorehdr_addr);
  949. elfcorehdr_addr = ELFCORE_ADDR_ERR;
  950. proc_vmcore = proc_create("vmcore", S_IRUSR, NULL, &proc_vmcore_operations);
  951. if (proc_vmcore)
  952. proc_vmcore->size = vmcore_size;
  953. return 0;
  954. }
  955. fs_initcall(vmcore_init);
  956. /* Cleanup function for vmcore module. */
  957. void vmcore_cleanup(void)
  958. {
  959. struct list_head *pos, *next;
  960. if (proc_vmcore) {
  961. proc_remove(proc_vmcore);
  962. proc_vmcore = NULL;
  963. }
  964. /* clear the vmcore list. */
  965. list_for_each_safe(pos, next, &vmcore_list) {
  966. struct vmcore *m;
  967. m = list_entry(pos, struct vmcore, list);
  968. list_del(&m->list);
  969. kfree(m);
  970. }
  971. free_elfcorebuf();
  972. }
  973. EXPORT_SYMBOL_GPL(vmcore_cleanup);