vmcore.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  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 <asm/uaccess.h>
  24. #include <asm/io.h>
  25. #include "internal.h"
  26. /* List representing chunks of contiguous memory areas and their offsets in
  27. * vmcore file.
  28. */
  29. static LIST_HEAD(vmcore_list);
  30. /* Stores the pointer to the buffer containing kernel elf core headers. */
  31. static char *elfcorebuf;
  32. static size_t elfcorebuf_sz;
  33. static size_t elfcorebuf_sz_orig;
  34. static char *elfnotes_buf;
  35. static size_t elfnotes_sz;
  36. /* Total size of vmcore file. */
  37. static u64 vmcore_size;
  38. static struct proc_dir_entry *proc_vmcore = NULL;
  39. /*
  40. * Returns > 0 for RAM pages, 0 for non-RAM pages, < 0 on error
  41. * The called function has to take care of module refcounting.
  42. */
  43. static int (*oldmem_pfn_is_ram)(unsigned long pfn);
  44. int register_oldmem_pfn_is_ram(int (*fn)(unsigned long pfn))
  45. {
  46. if (oldmem_pfn_is_ram)
  47. return -EBUSY;
  48. oldmem_pfn_is_ram = fn;
  49. return 0;
  50. }
  51. EXPORT_SYMBOL_GPL(register_oldmem_pfn_is_ram);
  52. void unregister_oldmem_pfn_is_ram(void)
  53. {
  54. oldmem_pfn_is_ram = NULL;
  55. wmb();
  56. }
  57. EXPORT_SYMBOL_GPL(unregister_oldmem_pfn_is_ram);
  58. static int pfn_is_ram(unsigned long pfn)
  59. {
  60. int (*fn)(unsigned long pfn);
  61. /* pfn is ram unless fn() checks pagetype */
  62. int ret = 1;
  63. /*
  64. * Ask hypervisor if the pfn is really ram.
  65. * A ballooned page contains no data and reading from such a page
  66. * will cause high load in the hypervisor.
  67. */
  68. fn = oldmem_pfn_is_ram;
  69. if (fn)
  70. ret = fn(pfn);
  71. return ret;
  72. }
  73. /* Reads a page from the oldmem device from given offset. */
  74. static ssize_t read_from_oldmem(char *buf, size_t count,
  75. u64 *ppos, int userbuf)
  76. {
  77. unsigned long pfn, offset;
  78. size_t nr_bytes;
  79. ssize_t read = 0, tmp;
  80. if (!count)
  81. return 0;
  82. offset = (unsigned long)(*ppos % PAGE_SIZE);
  83. pfn = (unsigned long)(*ppos / PAGE_SIZE);
  84. do {
  85. if (count > (PAGE_SIZE - offset))
  86. nr_bytes = PAGE_SIZE - offset;
  87. else
  88. nr_bytes = count;
  89. /* If pfn is not ram, return zeros for sparse dump files */
  90. if (pfn_is_ram(pfn) == 0)
  91. memset(buf, 0, nr_bytes);
  92. else {
  93. tmp = copy_oldmem_page(pfn, buf, nr_bytes,
  94. offset, userbuf);
  95. if (tmp < 0)
  96. return tmp;
  97. }
  98. *ppos += nr_bytes;
  99. count -= nr_bytes;
  100. buf += nr_bytes;
  101. read += nr_bytes;
  102. ++pfn;
  103. offset = 0;
  104. } while (count);
  105. return read;
  106. }
  107. /* Read from the ELF header and then the crash dump. On error, negative value is
  108. * returned otherwise number of bytes read are returned.
  109. */
  110. static ssize_t read_vmcore(struct file *file, char __user *buffer,
  111. size_t buflen, loff_t *fpos)
  112. {
  113. ssize_t acc = 0, tmp;
  114. size_t tsz;
  115. u64 start;
  116. struct vmcore *m = NULL;
  117. if (buflen == 0 || *fpos >= vmcore_size)
  118. return 0;
  119. /* trim buflen to not go beyond EOF */
  120. if (buflen > vmcore_size - *fpos)
  121. buflen = vmcore_size - *fpos;
  122. /* Read ELF core header */
  123. if (*fpos < elfcorebuf_sz) {
  124. tsz = min(elfcorebuf_sz - (size_t)*fpos, buflen);
  125. if (copy_to_user(buffer, elfcorebuf + *fpos, tsz))
  126. return -EFAULT;
  127. buflen -= tsz;
  128. *fpos += tsz;
  129. buffer += tsz;
  130. acc += tsz;
  131. /* leave now if filled buffer already */
  132. if (buflen == 0)
  133. return acc;
  134. }
  135. /* Read Elf note segment */
  136. if (*fpos < elfcorebuf_sz + elfnotes_sz) {
  137. void *kaddr;
  138. tsz = min(elfcorebuf_sz + elfnotes_sz - (size_t)*fpos, buflen);
  139. kaddr = elfnotes_buf + *fpos - elfcorebuf_sz;
  140. if (copy_to_user(buffer, kaddr, tsz))
  141. return -EFAULT;
  142. buflen -= tsz;
  143. *fpos += tsz;
  144. buffer += tsz;
  145. acc += tsz;
  146. /* leave now if filled buffer already */
  147. if (buflen == 0)
  148. return acc;
  149. }
  150. list_for_each_entry(m, &vmcore_list, list) {
  151. if (*fpos < m->offset + m->size) {
  152. tsz = min_t(size_t, m->offset + m->size - *fpos, buflen);
  153. start = m->paddr + *fpos - m->offset;
  154. tmp = read_from_oldmem(buffer, tsz, &start, 1);
  155. if (tmp < 0)
  156. return tmp;
  157. buflen -= tsz;
  158. *fpos += tsz;
  159. buffer += tsz;
  160. acc += tsz;
  161. /* leave now if filled buffer already */
  162. if (buflen == 0)
  163. return acc;
  164. }
  165. }
  166. return acc;
  167. }
  168. /**
  169. * alloc_elfnotes_buf - allocate buffer for ELF note segment in
  170. * vmalloc memory
  171. *
  172. * @notes_sz: size of buffer
  173. *
  174. * If CONFIG_MMU is defined, use vmalloc_user() to allow users to mmap
  175. * the buffer to user-space by means of remap_vmalloc_range().
  176. *
  177. * If CONFIG_MMU is not defined, use vzalloc() since mmap_vmcore() is
  178. * disabled and there's no need to allow users to mmap the buffer.
  179. */
  180. static inline char *alloc_elfnotes_buf(size_t notes_sz)
  181. {
  182. #ifdef CONFIG_MMU
  183. return vmalloc_user(notes_sz);
  184. #else
  185. return vzalloc(notes_sz);
  186. #endif
  187. }
  188. /*
  189. * Disable mmap_vmcore() if CONFIG_MMU is not defined. MMU is
  190. * essential for mmap_vmcore() in order to map physically
  191. * non-contiguous objects (ELF header, ELF note segment and memory
  192. * regions in the 1st kernel pointed to by PT_LOAD entries) into
  193. * virtually contiguous user-space in ELF layout.
  194. */
  195. #if defined(CONFIG_MMU) && !defined(CONFIG_S390)
  196. static int mmap_vmcore(struct file *file, struct vm_area_struct *vma)
  197. {
  198. size_t size = vma->vm_end - vma->vm_start;
  199. u64 start, end, len, tsz;
  200. struct vmcore *m;
  201. start = (u64)vma->vm_pgoff << PAGE_SHIFT;
  202. end = start + size;
  203. if (size > vmcore_size || end > vmcore_size)
  204. return -EINVAL;
  205. if (vma->vm_flags & (VM_WRITE | VM_EXEC))
  206. return -EPERM;
  207. vma->vm_flags &= ~(VM_MAYWRITE | VM_MAYEXEC);
  208. vma->vm_flags |= VM_MIXEDMAP;
  209. len = 0;
  210. if (start < elfcorebuf_sz) {
  211. u64 pfn;
  212. tsz = min(elfcorebuf_sz - (size_t)start, size);
  213. pfn = __pa(elfcorebuf + start) >> PAGE_SHIFT;
  214. if (remap_pfn_range(vma, vma->vm_start, pfn, tsz,
  215. vma->vm_page_prot))
  216. return -EAGAIN;
  217. size -= tsz;
  218. start += tsz;
  219. len += tsz;
  220. if (size == 0)
  221. return 0;
  222. }
  223. if (start < elfcorebuf_sz + elfnotes_sz) {
  224. void *kaddr;
  225. tsz = min(elfcorebuf_sz + elfnotes_sz - (size_t)start, size);
  226. kaddr = elfnotes_buf + start - elfcorebuf_sz;
  227. if (remap_vmalloc_range_partial(vma, vma->vm_start + len,
  228. kaddr, tsz))
  229. goto fail;
  230. size -= tsz;
  231. start += tsz;
  232. len += tsz;
  233. if (size == 0)
  234. return 0;
  235. }
  236. list_for_each_entry(m, &vmcore_list, list) {
  237. if (start < m->offset + m->size) {
  238. u64 paddr = 0;
  239. tsz = min_t(size_t, m->offset + m->size - start, size);
  240. paddr = m->paddr + start - m->offset;
  241. if (remap_pfn_range(vma, vma->vm_start + len,
  242. paddr >> PAGE_SHIFT, tsz,
  243. vma->vm_page_prot))
  244. goto fail;
  245. size -= tsz;
  246. start += tsz;
  247. len += tsz;
  248. if (size == 0)
  249. return 0;
  250. }
  251. }
  252. return 0;
  253. fail:
  254. do_munmap(vma->vm_mm, vma->vm_start, len);
  255. return -EAGAIN;
  256. }
  257. #else
  258. static int mmap_vmcore(struct file *file, struct vm_area_struct *vma)
  259. {
  260. return -ENOSYS;
  261. }
  262. #endif
  263. static const struct file_operations proc_vmcore_operations = {
  264. .read = read_vmcore,
  265. .llseek = default_llseek,
  266. .mmap = mmap_vmcore,
  267. };
  268. static struct vmcore* __init get_new_element(void)
  269. {
  270. return kzalloc(sizeof(struct vmcore), GFP_KERNEL);
  271. }
  272. static u64 __init get_vmcore_size(size_t elfsz, size_t elfnotesegsz,
  273. struct list_head *vc_list)
  274. {
  275. u64 size;
  276. struct vmcore *m;
  277. size = elfsz + elfnotesegsz;
  278. list_for_each_entry(m, vc_list, list) {
  279. size += m->size;
  280. }
  281. return size;
  282. }
  283. /**
  284. * update_note_header_size_elf64 - update p_memsz member of each PT_NOTE entry
  285. *
  286. * @ehdr_ptr: ELF header
  287. *
  288. * This function updates p_memsz member of each PT_NOTE entry in the
  289. * program header table pointed to by @ehdr_ptr to real size of ELF
  290. * note segment.
  291. */
  292. static int __init update_note_header_size_elf64(const Elf64_Ehdr *ehdr_ptr)
  293. {
  294. int i, rc=0;
  295. Elf64_Phdr *phdr_ptr;
  296. Elf64_Nhdr *nhdr_ptr;
  297. phdr_ptr = (Elf64_Phdr *)(ehdr_ptr + 1);
  298. for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
  299. void *notes_section;
  300. u64 offset, max_sz, sz, real_sz = 0;
  301. if (phdr_ptr->p_type != PT_NOTE)
  302. continue;
  303. max_sz = phdr_ptr->p_memsz;
  304. offset = phdr_ptr->p_offset;
  305. notes_section = kmalloc(max_sz, GFP_KERNEL);
  306. if (!notes_section)
  307. return -ENOMEM;
  308. rc = read_from_oldmem(notes_section, max_sz, &offset, 0);
  309. if (rc < 0) {
  310. kfree(notes_section);
  311. return rc;
  312. }
  313. nhdr_ptr = notes_section;
  314. while (real_sz < max_sz) {
  315. if (nhdr_ptr->n_namesz == 0)
  316. break;
  317. sz = sizeof(Elf64_Nhdr) +
  318. ((nhdr_ptr->n_namesz + 3) & ~3) +
  319. ((nhdr_ptr->n_descsz + 3) & ~3);
  320. real_sz += sz;
  321. nhdr_ptr = (Elf64_Nhdr*)((char*)nhdr_ptr + sz);
  322. }
  323. kfree(notes_section);
  324. phdr_ptr->p_memsz = real_sz;
  325. }
  326. return 0;
  327. }
  328. /**
  329. * get_note_number_and_size_elf64 - get the number of PT_NOTE program
  330. * headers and sum of real size of their ELF note segment headers and
  331. * data.
  332. *
  333. * @ehdr_ptr: ELF header
  334. * @nr_ptnote: buffer for the number of PT_NOTE program headers
  335. * @sz_ptnote: buffer for size of unique PT_NOTE program header
  336. *
  337. * This function is used to merge multiple PT_NOTE program headers
  338. * into a unique single one. The resulting unique entry will have
  339. * @sz_ptnote in its phdr->p_mem.
  340. *
  341. * It is assumed that program headers with PT_NOTE type pointed to by
  342. * @ehdr_ptr has already been updated by update_note_header_size_elf64
  343. * and each of PT_NOTE program headers has actual ELF note segment
  344. * size in its p_memsz member.
  345. */
  346. static int __init get_note_number_and_size_elf64(const Elf64_Ehdr *ehdr_ptr,
  347. int *nr_ptnote, u64 *sz_ptnote)
  348. {
  349. int i;
  350. Elf64_Phdr *phdr_ptr;
  351. *nr_ptnote = *sz_ptnote = 0;
  352. phdr_ptr = (Elf64_Phdr *)(ehdr_ptr + 1);
  353. for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
  354. if (phdr_ptr->p_type != PT_NOTE)
  355. continue;
  356. *nr_ptnote += 1;
  357. *sz_ptnote += phdr_ptr->p_memsz;
  358. }
  359. return 0;
  360. }
  361. /**
  362. * copy_notes_elf64 - copy ELF note segments in a given buffer
  363. *
  364. * @ehdr_ptr: ELF header
  365. * @notes_buf: buffer into which ELF note segments are copied
  366. *
  367. * This function is used to copy ELF note segment in the 1st kernel
  368. * into the buffer @notes_buf in the 2nd kernel. It is assumed that
  369. * size of the buffer @notes_buf is equal to or larger than sum of the
  370. * real ELF note segment headers and data.
  371. *
  372. * It is assumed that program headers with PT_NOTE type pointed to by
  373. * @ehdr_ptr has already been updated by update_note_header_size_elf64
  374. * and each of PT_NOTE program headers has actual ELF note segment
  375. * size in its p_memsz member.
  376. */
  377. static int __init copy_notes_elf64(const Elf64_Ehdr *ehdr_ptr, char *notes_buf)
  378. {
  379. int i, rc=0;
  380. Elf64_Phdr *phdr_ptr;
  381. phdr_ptr = (Elf64_Phdr*)(ehdr_ptr + 1);
  382. for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
  383. u64 offset;
  384. if (phdr_ptr->p_type != PT_NOTE)
  385. continue;
  386. offset = phdr_ptr->p_offset;
  387. rc = read_from_oldmem(notes_buf, phdr_ptr->p_memsz, &offset, 0);
  388. if (rc < 0)
  389. return rc;
  390. notes_buf += phdr_ptr->p_memsz;
  391. }
  392. return 0;
  393. }
  394. /* Merges all the PT_NOTE headers into one. */
  395. static int __init merge_note_headers_elf64(char *elfptr, size_t *elfsz,
  396. char **notes_buf, size_t *notes_sz)
  397. {
  398. int i, nr_ptnote=0, rc=0;
  399. char *tmp;
  400. Elf64_Ehdr *ehdr_ptr;
  401. Elf64_Phdr phdr;
  402. u64 phdr_sz = 0, note_off;
  403. ehdr_ptr = (Elf64_Ehdr *)elfptr;
  404. rc = update_note_header_size_elf64(ehdr_ptr);
  405. if (rc < 0)
  406. return rc;
  407. rc = get_note_number_and_size_elf64(ehdr_ptr, &nr_ptnote, &phdr_sz);
  408. if (rc < 0)
  409. return rc;
  410. *notes_sz = roundup(phdr_sz, PAGE_SIZE);
  411. *notes_buf = alloc_elfnotes_buf(*notes_sz);
  412. if (!*notes_buf)
  413. return -ENOMEM;
  414. rc = copy_notes_elf64(ehdr_ptr, *notes_buf);
  415. if (rc < 0)
  416. return rc;
  417. /* Prepare merged PT_NOTE program header. */
  418. phdr.p_type = PT_NOTE;
  419. phdr.p_flags = 0;
  420. note_off = sizeof(Elf64_Ehdr) +
  421. (ehdr_ptr->e_phnum - nr_ptnote +1) * sizeof(Elf64_Phdr);
  422. phdr.p_offset = roundup(note_off, PAGE_SIZE);
  423. phdr.p_vaddr = phdr.p_paddr = 0;
  424. phdr.p_filesz = phdr.p_memsz = phdr_sz;
  425. phdr.p_align = 0;
  426. /* Add merged PT_NOTE program header*/
  427. tmp = elfptr + sizeof(Elf64_Ehdr);
  428. memcpy(tmp, &phdr, sizeof(phdr));
  429. tmp += sizeof(phdr);
  430. /* Remove unwanted PT_NOTE program headers. */
  431. i = (nr_ptnote - 1) * sizeof(Elf64_Phdr);
  432. *elfsz = *elfsz - i;
  433. memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf64_Ehdr)-sizeof(Elf64_Phdr)));
  434. memset(elfptr + *elfsz, 0, i);
  435. *elfsz = roundup(*elfsz, PAGE_SIZE);
  436. /* Modify e_phnum to reflect merged headers. */
  437. ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1;
  438. return 0;
  439. }
  440. /**
  441. * update_note_header_size_elf32 - update p_memsz member of each PT_NOTE entry
  442. *
  443. * @ehdr_ptr: ELF header
  444. *
  445. * This function updates p_memsz member of each PT_NOTE entry in the
  446. * program header table pointed to by @ehdr_ptr to real size of ELF
  447. * note segment.
  448. */
  449. static int __init update_note_header_size_elf32(const Elf32_Ehdr *ehdr_ptr)
  450. {
  451. int i, rc=0;
  452. Elf32_Phdr *phdr_ptr;
  453. Elf32_Nhdr *nhdr_ptr;
  454. phdr_ptr = (Elf32_Phdr *)(ehdr_ptr + 1);
  455. for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
  456. void *notes_section;
  457. u64 offset, max_sz, sz, real_sz = 0;
  458. if (phdr_ptr->p_type != PT_NOTE)
  459. continue;
  460. max_sz = phdr_ptr->p_memsz;
  461. offset = phdr_ptr->p_offset;
  462. notes_section = kmalloc(max_sz, GFP_KERNEL);
  463. if (!notes_section)
  464. return -ENOMEM;
  465. rc = read_from_oldmem(notes_section, max_sz, &offset, 0);
  466. if (rc < 0) {
  467. kfree(notes_section);
  468. return rc;
  469. }
  470. nhdr_ptr = notes_section;
  471. while (real_sz < max_sz) {
  472. if (nhdr_ptr->n_namesz == 0)
  473. break;
  474. sz = sizeof(Elf32_Nhdr) +
  475. ((nhdr_ptr->n_namesz + 3) & ~3) +
  476. ((nhdr_ptr->n_descsz + 3) & ~3);
  477. real_sz += sz;
  478. nhdr_ptr = (Elf32_Nhdr*)((char*)nhdr_ptr + sz);
  479. }
  480. kfree(notes_section);
  481. phdr_ptr->p_memsz = real_sz;
  482. }
  483. return 0;
  484. }
  485. /**
  486. * get_note_number_and_size_elf32 - get the number of PT_NOTE program
  487. * headers and sum of real size of their ELF note segment headers and
  488. * data.
  489. *
  490. * @ehdr_ptr: ELF header
  491. * @nr_ptnote: buffer for the number of PT_NOTE program headers
  492. * @sz_ptnote: buffer for size of unique PT_NOTE program header
  493. *
  494. * This function is used to merge multiple PT_NOTE program headers
  495. * into a unique single one. The resulting unique entry will have
  496. * @sz_ptnote in its phdr->p_mem.
  497. *
  498. * It is assumed that program headers with PT_NOTE type pointed to by
  499. * @ehdr_ptr has already been updated by update_note_header_size_elf32
  500. * and each of PT_NOTE program headers has actual ELF note segment
  501. * size in its p_memsz member.
  502. */
  503. static int __init get_note_number_and_size_elf32(const Elf32_Ehdr *ehdr_ptr,
  504. int *nr_ptnote, u64 *sz_ptnote)
  505. {
  506. int i;
  507. Elf32_Phdr *phdr_ptr;
  508. *nr_ptnote = *sz_ptnote = 0;
  509. phdr_ptr = (Elf32_Phdr *)(ehdr_ptr + 1);
  510. for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
  511. if (phdr_ptr->p_type != PT_NOTE)
  512. continue;
  513. *nr_ptnote += 1;
  514. *sz_ptnote += phdr_ptr->p_memsz;
  515. }
  516. return 0;
  517. }
  518. /**
  519. * copy_notes_elf32 - copy ELF note segments in a given buffer
  520. *
  521. * @ehdr_ptr: ELF header
  522. * @notes_buf: buffer into which ELF note segments are copied
  523. *
  524. * This function is used to copy ELF note segment in the 1st kernel
  525. * into the buffer @notes_buf in the 2nd kernel. It is assumed that
  526. * size of the buffer @notes_buf is equal to or larger than sum of the
  527. * real ELF note segment headers and data.
  528. *
  529. * It is assumed that program headers with PT_NOTE type pointed to by
  530. * @ehdr_ptr has already been updated by update_note_header_size_elf32
  531. * and each of PT_NOTE program headers has actual ELF note segment
  532. * size in its p_memsz member.
  533. */
  534. static int __init copy_notes_elf32(const Elf32_Ehdr *ehdr_ptr, char *notes_buf)
  535. {
  536. int i, rc=0;
  537. Elf32_Phdr *phdr_ptr;
  538. phdr_ptr = (Elf32_Phdr*)(ehdr_ptr + 1);
  539. for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
  540. u64 offset;
  541. if (phdr_ptr->p_type != PT_NOTE)
  542. continue;
  543. offset = phdr_ptr->p_offset;
  544. rc = read_from_oldmem(notes_buf, phdr_ptr->p_memsz, &offset, 0);
  545. if (rc < 0)
  546. return rc;
  547. notes_buf += phdr_ptr->p_memsz;
  548. }
  549. return 0;
  550. }
  551. /* Merges all the PT_NOTE headers into one. */
  552. static int __init merge_note_headers_elf32(char *elfptr, size_t *elfsz,
  553. char **notes_buf, size_t *notes_sz)
  554. {
  555. int i, nr_ptnote=0, rc=0;
  556. char *tmp;
  557. Elf32_Ehdr *ehdr_ptr;
  558. Elf32_Phdr phdr;
  559. u64 phdr_sz = 0, note_off;
  560. ehdr_ptr = (Elf32_Ehdr *)elfptr;
  561. rc = update_note_header_size_elf32(ehdr_ptr);
  562. if (rc < 0)
  563. return rc;
  564. rc = get_note_number_and_size_elf32(ehdr_ptr, &nr_ptnote, &phdr_sz);
  565. if (rc < 0)
  566. return rc;
  567. *notes_sz = roundup(phdr_sz, PAGE_SIZE);
  568. *notes_buf = alloc_elfnotes_buf(*notes_sz);
  569. if (!*notes_buf)
  570. return -ENOMEM;
  571. rc = copy_notes_elf32(ehdr_ptr, *notes_buf);
  572. if (rc < 0)
  573. return rc;
  574. /* Prepare merged PT_NOTE program header. */
  575. phdr.p_type = PT_NOTE;
  576. phdr.p_flags = 0;
  577. note_off = sizeof(Elf32_Ehdr) +
  578. (ehdr_ptr->e_phnum - nr_ptnote +1) * sizeof(Elf32_Phdr);
  579. phdr.p_offset = roundup(note_off, PAGE_SIZE);
  580. phdr.p_vaddr = phdr.p_paddr = 0;
  581. phdr.p_filesz = phdr.p_memsz = phdr_sz;
  582. phdr.p_align = 0;
  583. /* Add merged PT_NOTE program header*/
  584. tmp = elfptr + sizeof(Elf32_Ehdr);
  585. memcpy(tmp, &phdr, sizeof(phdr));
  586. tmp += sizeof(phdr);
  587. /* Remove unwanted PT_NOTE program headers. */
  588. i = (nr_ptnote - 1) * sizeof(Elf32_Phdr);
  589. *elfsz = *elfsz - i;
  590. memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf32_Ehdr)-sizeof(Elf32_Phdr)));
  591. memset(elfptr + *elfsz, 0, i);
  592. *elfsz = roundup(*elfsz, PAGE_SIZE);
  593. /* Modify e_phnum to reflect merged headers. */
  594. ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1;
  595. return 0;
  596. }
  597. /* Add memory chunks represented by program headers to vmcore list. Also update
  598. * the new offset fields of exported program headers. */
  599. static int __init process_ptload_program_headers_elf64(char *elfptr,
  600. size_t elfsz,
  601. size_t elfnotes_sz,
  602. struct list_head *vc_list)
  603. {
  604. int i;
  605. Elf64_Ehdr *ehdr_ptr;
  606. Elf64_Phdr *phdr_ptr;
  607. loff_t vmcore_off;
  608. struct vmcore *new;
  609. ehdr_ptr = (Elf64_Ehdr *)elfptr;
  610. phdr_ptr = (Elf64_Phdr*)(elfptr + sizeof(Elf64_Ehdr)); /* PT_NOTE hdr */
  611. /* Skip Elf header, program headers and Elf note segment. */
  612. vmcore_off = elfsz + elfnotes_sz;
  613. for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
  614. u64 paddr, start, end, size;
  615. if (phdr_ptr->p_type != PT_LOAD)
  616. continue;
  617. paddr = phdr_ptr->p_offset;
  618. start = rounddown(paddr, PAGE_SIZE);
  619. end = roundup(paddr + phdr_ptr->p_memsz, PAGE_SIZE);
  620. size = end - start;
  621. /* Add this contiguous chunk of memory to vmcore list.*/
  622. new = get_new_element();
  623. if (!new)
  624. return -ENOMEM;
  625. new->paddr = start;
  626. new->size = size;
  627. list_add_tail(&new->list, vc_list);
  628. /* Update the program header offset. */
  629. phdr_ptr->p_offset = vmcore_off + (paddr - start);
  630. vmcore_off = vmcore_off + size;
  631. }
  632. return 0;
  633. }
  634. static int __init process_ptload_program_headers_elf32(char *elfptr,
  635. size_t elfsz,
  636. size_t elfnotes_sz,
  637. struct list_head *vc_list)
  638. {
  639. int i;
  640. Elf32_Ehdr *ehdr_ptr;
  641. Elf32_Phdr *phdr_ptr;
  642. loff_t vmcore_off;
  643. struct vmcore *new;
  644. ehdr_ptr = (Elf32_Ehdr *)elfptr;
  645. phdr_ptr = (Elf32_Phdr*)(elfptr + sizeof(Elf32_Ehdr)); /* PT_NOTE hdr */
  646. /* Skip Elf header, program headers and Elf note segment. */
  647. vmcore_off = elfsz + elfnotes_sz;
  648. for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
  649. u64 paddr, start, end, size;
  650. if (phdr_ptr->p_type != PT_LOAD)
  651. continue;
  652. paddr = phdr_ptr->p_offset;
  653. start = rounddown(paddr, PAGE_SIZE);
  654. end = roundup(paddr + phdr_ptr->p_memsz, PAGE_SIZE);
  655. size = end - start;
  656. /* Add this contiguous chunk of memory to vmcore list.*/
  657. new = get_new_element();
  658. if (!new)
  659. return -ENOMEM;
  660. new->paddr = start;
  661. new->size = size;
  662. list_add_tail(&new->list, vc_list);
  663. /* Update the program header offset */
  664. phdr_ptr->p_offset = vmcore_off + (paddr - start);
  665. vmcore_off = vmcore_off + size;
  666. }
  667. return 0;
  668. }
  669. /* Sets offset fields of vmcore elements. */
  670. static void __init set_vmcore_list_offsets(size_t elfsz, size_t elfnotes_sz,
  671. struct list_head *vc_list)
  672. {
  673. loff_t vmcore_off;
  674. struct vmcore *m;
  675. /* Skip Elf header, program headers and Elf note segment. */
  676. vmcore_off = elfsz + elfnotes_sz;
  677. list_for_each_entry(m, vc_list, list) {
  678. m->offset = vmcore_off;
  679. vmcore_off += m->size;
  680. }
  681. }
  682. static void free_elfcorebuf(void)
  683. {
  684. free_pages((unsigned long)elfcorebuf, get_order(elfcorebuf_sz_orig));
  685. elfcorebuf = NULL;
  686. vfree(elfnotes_buf);
  687. elfnotes_buf = NULL;
  688. }
  689. static int __init parse_crash_elf64_headers(void)
  690. {
  691. int rc=0;
  692. Elf64_Ehdr ehdr;
  693. u64 addr;
  694. addr = elfcorehdr_addr;
  695. /* Read Elf header */
  696. rc = read_from_oldmem((char*)&ehdr, sizeof(Elf64_Ehdr), &addr, 0);
  697. if (rc < 0)
  698. return rc;
  699. /* Do some basic Verification. */
  700. if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
  701. (ehdr.e_type != ET_CORE) ||
  702. !vmcore_elf64_check_arch(&ehdr) ||
  703. ehdr.e_ident[EI_CLASS] != ELFCLASS64 ||
  704. ehdr.e_ident[EI_VERSION] != EV_CURRENT ||
  705. ehdr.e_version != EV_CURRENT ||
  706. ehdr.e_ehsize != sizeof(Elf64_Ehdr) ||
  707. ehdr.e_phentsize != sizeof(Elf64_Phdr) ||
  708. ehdr.e_phnum == 0) {
  709. pr_warn("Warning: Core image elf header is not sane\n");
  710. return -EINVAL;
  711. }
  712. /* Read in all elf headers. */
  713. elfcorebuf_sz_orig = sizeof(Elf64_Ehdr) +
  714. ehdr.e_phnum * sizeof(Elf64_Phdr);
  715. elfcorebuf_sz = elfcorebuf_sz_orig;
  716. elfcorebuf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  717. get_order(elfcorebuf_sz_orig));
  718. if (!elfcorebuf)
  719. return -ENOMEM;
  720. addr = elfcorehdr_addr;
  721. rc = read_from_oldmem(elfcorebuf, elfcorebuf_sz_orig, &addr, 0);
  722. if (rc < 0)
  723. goto fail;
  724. /* Merge all PT_NOTE headers into one. */
  725. rc = merge_note_headers_elf64(elfcorebuf, &elfcorebuf_sz,
  726. &elfnotes_buf, &elfnotes_sz);
  727. if (rc)
  728. goto fail;
  729. rc = process_ptload_program_headers_elf64(elfcorebuf, elfcorebuf_sz,
  730. elfnotes_sz, &vmcore_list);
  731. if (rc)
  732. goto fail;
  733. set_vmcore_list_offsets(elfcorebuf_sz, elfnotes_sz, &vmcore_list);
  734. return 0;
  735. fail:
  736. free_elfcorebuf();
  737. return rc;
  738. }
  739. static int __init parse_crash_elf32_headers(void)
  740. {
  741. int rc=0;
  742. Elf32_Ehdr ehdr;
  743. u64 addr;
  744. addr = elfcorehdr_addr;
  745. /* Read Elf header */
  746. rc = read_from_oldmem((char*)&ehdr, sizeof(Elf32_Ehdr), &addr, 0);
  747. if (rc < 0)
  748. return rc;
  749. /* Do some basic Verification. */
  750. if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
  751. (ehdr.e_type != ET_CORE) ||
  752. !elf_check_arch(&ehdr) ||
  753. ehdr.e_ident[EI_CLASS] != ELFCLASS32||
  754. ehdr.e_ident[EI_VERSION] != EV_CURRENT ||
  755. ehdr.e_version != EV_CURRENT ||
  756. ehdr.e_ehsize != sizeof(Elf32_Ehdr) ||
  757. ehdr.e_phentsize != sizeof(Elf32_Phdr) ||
  758. ehdr.e_phnum == 0) {
  759. pr_warn("Warning: Core image elf header is not sane\n");
  760. return -EINVAL;
  761. }
  762. /* Read in all elf headers. */
  763. elfcorebuf_sz_orig = sizeof(Elf32_Ehdr) + ehdr.e_phnum * sizeof(Elf32_Phdr);
  764. elfcorebuf_sz = elfcorebuf_sz_orig;
  765. elfcorebuf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  766. get_order(elfcorebuf_sz_orig));
  767. if (!elfcorebuf)
  768. return -ENOMEM;
  769. addr = elfcorehdr_addr;
  770. rc = read_from_oldmem(elfcorebuf, elfcorebuf_sz_orig, &addr, 0);
  771. if (rc < 0)
  772. goto fail;
  773. /* Merge all PT_NOTE headers into one. */
  774. rc = merge_note_headers_elf32(elfcorebuf, &elfcorebuf_sz,
  775. &elfnotes_buf, &elfnotes_sz);
  776. if (rc)
  777. goto fail;
  778. rc = process_ptload_program_headers_elf32(elfcorebuf, elfcorebuf_sz,
  779. elfnotes_sz, &vmcore_list);
  780. if (rc)
  781. goto fail;
  782. set_vmcore_list_offsets(elfcorebuf_sz, elfnotes_sz, &vmcore_list);
  783. return 0;
  784. fail:
  785. free_elfcorebuf();
  786. return rc;
  787. }
  788. static int __init parse_crash_elf_headers(void)
  789. {
  790. unsigned char e_ident[EI_NIDENT];
  791. u64 addr;
  792. int rc=0;
  793. addr = elfcorehdr_addr;
  794. rc = read_from_oldmem(e_ident, EI_NIDENT, &addr, 0);
  795. if (rc < 0)
  796. return rc;
  797. if (memcmp(e_ident, ELFMAG, SELFMAG) != 0) {
  798. pr_warn("Warning: Core image elf header not found\n");
  799. return -EINVAL;
  800. }
  801. if (e_ident[EI_CLASS] == ELFCLASS64) {
  802. rc = parse_crash_elf64_headers();
  803. if (rc)
  804. return rc;
  805. } else if (e_ident[EI_CLASS] == ELFCLASS32) {
  806. rc = parse_crash_elf32_headers();
  807. if (rc)
  808. return rc;
  809. } else {
  810. pr_warn("Warning: Core image elf header is not sane\n");
  811. return -EINVAL;
  812. }
  813. /* Determine vmcore size. */
  814. vmcore_size = get_vmcore_size(elfcorebuf_sz, elfnotes_sz,
  815. &vmcore_list);
  816. return 0;
  817. }
  818. /* Init function for vmcore module. */
  819. static int __init vmcore_init(void)
  820. {
  821. int rc = 0;
  822. /* If elfcorehdr= has been passed in cmdline, then capture the dump.*/
  823. if (!(is_vmcore_usable()))
  824. return rc;
  825. rc = parse_crash_elf_headers();
  826. if (rc) {
  827. pr_warn("Kdump: vmcore not initialized\n");
  828. return rc;
  829. }
  830. proc_vmcore = proc_create("vmcore", S_IRUSR, NULL, &proc_vmcore_operations);
  831. if (proc_vmcore)
  832. proc_vmcore->size = vmcore_size;
  833. return 0;
  834. }
  835. module_init(vmcore_init)
  836. /* Cleanup function for vmcore module. */
  837. void vmcore_cleanup(void)
  838. {
  839. struct list_head *pos, *next;
  840. if (proc_vmcore) {
  841. proc_remove(proc_vmcore);
  842. proc_vmcore = NULL;
  843. }
  844. /* clear the vmcore list. */
  845. list_for_each_safe(pos, next, &vmcore_list) {
  846. struct vmcore *m;
  847. m = list_entry(pos, struct vmcore, list);
  848. list_del(&m->list);
  849. kfree(m);
  850. }
  851. free_elfcorebuf();
  852. }
  853. EXPORT_SYMBOL_GPL(vmcore_cleanup);