vmcore.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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 <asm/uaccess.h>
  23. #include <asm/io.h>
  24. #include "internal.h"
  25. /* List representing chunks of contiguous memory areas and their offsets in
  26. * vmcore file.
  27. */
  28. static LIST_HEAD(vmcore_list);
  29. /* Stores the pointer to the buffer containing kernel elf core headers. */
  30. static char *elfcorebuf;
  31. static size_t elfcorebuf_sz;
  32. static size_t elfcorebuf_sz_orig;
  33. /* Total size of vmcore file. */
  34. static u64 vmcore_size;
  35. static struct proc_dir_entry *proc_vmcore = NULL;
  36. /*
  37. * Returns > 0 for RAM pages, 0 for non-RAM pages, < 0 on error
  38. * The called function has to take care of module refcounting.
  39. */
  40. static int (*oldmem_pfn_is_ram)(unsigned long pfn);
  41. int register_oldmem_pfn_is_ram(int (*fn)(unsigned long pfn))
  42. {
  43. if (oldmem_pfn_is_ram)
  44. return -EBUSY;
  45. oldmem_pfn_is_ram = fn;
  46. return 0;
  47. }
  48. EXPORT_SYMBOL_GPL(register_oldmem_pfn_is_ram);
  49. void unregister_oldmem_pfn_is_ram(void)
  50. {
  51. oldmem_pfn_is_ram = NULL;
  52. wmb();
  53. }
  54. EXPORT_SYMBOL_GPL(unregister_oldmem_pfn_is_ram);
  55. static int pfn_is_ram(unsigned long pfn)
  56. {
  57. int (*fn)(unsigned long pfn);
  58. /* pfn is ram unless fn() checks pagetype */
  59. int ret = 1;
  60. /*
  61. * Ask hypervisor if the pfn is really ram.
  62. * A ballooned page contains no data and reading from such a page
  63. * will cause high load in the hypervisor.
  64. */
  65. fn = oldmem_pfn_is_ram;
  66. if (fn)
  67. ret = fn(pfn);
  68. return ret;
  69. }
  70. /* Reads a page from the oldmem device from given offset. */
  71. static ssize_t read_from_oldmem(char *buf, size_t count,
  72. u64 *ppos, int userbuf)
  73. {
  74. unsigned long pfn, offset;
  75. size_t nr_bytes;
  76. ssize_t read = 0, tmp;
  77. if (!count)
  78. return 0;
  79. offset = (unsigned long)(*ppos % PAGE_SIZE);
  80. pfn = (unsigned long)(*ppos / PAGE_SIZE);
  81. do {
  82. if (count > (PAGE_SIZE - offset))
  83. nr_bytes = PAGE_SIZE - offset;
  84. else
  85. nr_bytes = count;
  86. /* If pfn is not ram, return zeros for sparse dump files */
  87. if (pfn_is_ram(pfn) == 0)
  88. memset(buf, 0, nr_bytes);
  89. else {
  90. tmp = copy_oldmem_page(pfn, buf, nr_bytes,
  91. offset, userbuf);
  92. if (tmp < 0)
  93. return tmp;
  94. }
  95. *ppos += nr_bytes;
  96. count -= nr_bytes;
  97. buf += nr_bytes;
  98. read += nr_bytes;
  99. ++pfn;
  100. offset = 0;
  101. } while (count);
  102. return read;
  103. }
  104. /* Read from the ELF header and then the crash dump. On error, negative value is
  105. * returned otherwise number of bytes read are returned.
  106. */
  107. static ssize_t read_vmcore(struct file *file, char __user *buffer,
  108. size_t buflen, loff_t *fpos)
  109. {
  110. ssize_t acc = 0, tmp;
  111. size_t tsz;
  112. u64 start;
  113. struct vmcore *m = NULL;
  114. if (buflen == 0 || *fpos >= vmcore_size)
  115. return 0;
  116. /* trim buflen to not go beyond EOF */
  117. if (buflen > vmcore_size - *fpos)
  118. buflen = vmcore_size - *fpos;
  119. /* Read ELF core header */
  120. if (*fpos < elfcorebuf_sz) {
  121. tsz = elfcorebuf_sz - *fpos;
  122. if (buflen < tsz)
  123. tsz = buflen;
  124. if (copy_to_user(buffer, elfcorebuf + *fpos, tsz))
  125. return -EFAULT;
  126. buflen -= tsz;
  127. *fpos += tsz;
  128. buffer += tsz;
  129. acc += tsz;
  130. /* leave now if filled buffer already */
  131. if (buflen == 0)
  132. return acc;
  133. }
  134. list_for_each_entry(m, &vmcore_list, list) {
  135. if (*fpos < m->offset + m->size) {
  136. tsz = m->offset + m->size - *fpos;
  137. if (buflen < tsz)
  138. tsz = buflen;
  139. start = m->paddr + *fpos - m->offset;
  140. tmp = read_from_oldmem(buffer, tsz, &start, 1);
  141. if (tmp < 0)
  142. return tmp;
  143. buflen -= tsz;
  144. *fpos += tsz;
  145. buffer += tsz;
  146. acc += tsz;
  147. /* leave now if filled buffer already */
  148. if (buflen == 0)
  149. return acc;
  150. }
  151. }
  152. return acc;
  153. }
  154. static const struct file_operations proc_vmcore_operations = {
  155. .read = read_vmcore,
  156. .llseek = default_llseek,
  157. };
  158. static struct vmcore* __init get_new_element(void)
  159. {
  160. return kzalloc(sizeof(struct vmcore), GFP_KERNEL);
  161. }
  162. static u64 __init get_vmcore_size_elf64(char *elfptr, size_t elfsz)
  163. {
  164. int i;
  165. u64 size;
  166. Elf64_Ehdr *ehdr_ptr;
  167. Elf64_Phdr *phdr_ptr;
  168. ehdr_ptr = (Elf64_Ehdr *)elfptr;
  169. phdr_ptr = (Elf64_Phdr*)(elfptr + sizeof(Elf64_Ehdr));
  170. size = elfsz;
  171. for (i = 0; i < ehdr_ptr->e_phnum; i++) {
  172. size += phdr_ptr->p_memsz;
  173. phdr_ptr++;
  174. }
  175. return size;
  176. }
  177. static u64 __init get_vmcore_size_elf32(char *elfptr, size_t elfsz)
  178. {
  179. int i;
  180. u64 size;
  181. Elf32_Ehdr *ehdr_ptr;
  182. Elf32_Phdr *phdr_ptr;
  183. ehdr_ptr = (Elf32_Ehdr *)elfptr;
  184. phdr_ptr = (Elf32_Phdr*)(elfptr + sizeof(Elf32_Ehdr));
  185. size = elfsz;
  186. for (i = 0; i < ehdr_ptr->e_phnum; i++) {
  187. size += phdr_ptr->p_memsz;
  188. phdr_ptr++;
  189. }
  190. return size;
  191. }
  192. /* Merges all the PT_NOTE headers into one. */
  193. static int __init merge_note_headers_elf64(char *elfptr, size_t *elfsz,
  194. struct list_head *vc_list)
  195. {
  196. int i, nr_ptnote=0, rc=0;
  197. char *tmp;
  198. Elf64_Ehdr *ehdr_ptr;
  199. Elf64_Phdr phdr, *phdr_ptr;
  200. Elf64_Nhdr *nhdr_ptr;
  201. u64 phdr_sz = 0, note_off;
  202. ehdr_ptr = (Elf64_Ehdr *)elfptr;
  203. phdr_ptr = (Elf64_Phdr*)(elfptr + sizeof(Elf64_Ehdr));
  204. for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
  205. int j;
  206. void *notes_section;
  207. struct vmcore *new;
  208. u64 offset, max_sz, sz, real_sz = 0;
  209. if (phdr_ptr->p_type != PT_NOTE)
  210. continue;
  211. nr_ptnote++;
  212. max_sz = phdr_ptr->p_memsz;
  213. offset = phdr_ptr->p_offset;
  214. notes_section = kmalloc(max_sz, GFP_KERNEL);
  215. if (!notes_section)
  216. return -ENOMEM;
  217. rc = read_from_oldmem(notes_section, max_sz, &offset, 0);
  218. if (rc < 0) {
  219. kfree(notes_section);
  220. return rc;
  221. }
  222. nhdr_ptr = notes_section;
  223. for (j = 0; j < max_sz; j += sz) {
  224. if (nhdr_ptr->n_namesz == 0)
  225. break;
  226. sz = sizeof(Elf64_Nhdr) +
  227. ((nhdr_ptr->n_namesz + 3) & ~3) +
  228. ((nhdr_ptr->n_descsz + 3) & ~3);
  229. real_sz += sz;
  230. nhdr_ptr = (Elf64_Nhdr*)((char*)nhdr_ptr + sz);
  231. }
  232. /* Add this contiguous chunk of notes section to vmcore list.*/
  233. new = get_new_element();
  234. if (!new) {
  235. kfree(notes_section);
  236. return -ENOMEM;
  237. }
  238. new->paddr = phdr_ptr->p_offset;
  239. new->size = real_sz;
  240. list_add_tail(&new->list, vc_list);
  241. phdr_sz += real_sz;
  242. kfree(notes_section);
  243. }
  244. /* Prepare merged PT_NOTE program header. */
  245. phdr.p_type = PT_NOTE;
  246. phdr.p_flags = 0;
  247. note_off = sizeof(Elf64_Ehdr) +
  248. (ehdr_ptr->e_phnum - nr_ptnote +1) * sizeof(Elf64_Phdr);
  249. phdr.p_offset = note_off;
  250. phdr.p_vaddr = phdr.p_paddr = 0;
  251. phdr.p_filesz = phdr.p_memsz = phdr_sz;
  252. phdr.p_align = 0;
  253. /* Add merged PT_NOTE program header*/
  254. tmp = elfptr + sizeof(Elf64_Ehdr);
  255. memcpy(tmp, &phdr, sizeof(phdr));
  256. tmp += sizeof(phdr);
  257. /* Remove unwanted PT_NOTE program headers. */
  258. i = (nr_ptnote - 1) * sizeof(Elf64_Phdr);
  259. *elfsz = *elfsz - i;
  260. memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf64_Ehdr)-sizeof(Elf64_Phdr)));
  261. memset(elfptr + *elfsz, 0, i);
  262. *elfsz = roundup(*elfsz, PAGE_SIZE);
  263. /* Modify e_phnum to reflect merged headers. */
  264. ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1;
  265. return 0;
  266. }
  267. /* Merges all the PT_NOTE headers into one. */
  268. static int __init merge_note_headers_elf32(char *elfptr, size_t *elfsz,
  269. struct list_head *vc_list)
  270. {
  271. int i, nr_ptnote=0, rc=0;
  272. char *tmp;
  273. Elf32_Ehdr *ehdr_ptr;
  274. Elf32_Phdr phdr, *phdr_ptr;
  275. Elf32_Nhdr *nhdr_ptr;
  276. u64 phdr_sz = 0, note_off;
  277. ehdr_ptr = (Elf32_Ehdr *)elfptr;
  278. phdr_ptr = (Elf32_Phdr*)(elfptr + sizeof(Elf32_Ehdr));
  279. for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
  280. int j;
  281. void *notes_section;
  282. struct vmcore *new;
  283. u64 offset, max_sz, sz, real_sz = 0;
  284. if (phdr_ptr->p_type != PT_NOTE)
  285. continue;
  286. nr_ptnote++;
  287. max_sz = phdr_ptr->p_memsz;
  288. offset = phdr_ptr->p_offset;
  289. notes_section = kmalloc(max_sz, GFP_KERNEL);
  290. if (!notes_section)
  291. return -ENOMEM;
  292. rc = read_from_oldmem(notes_section, max_sz, &offset, 0);
  293. if (rc < 0) {
  294. kfree(notes_section);
  295. return rc;
  296. }
  297. nhdr_ptr = notes_section;
  298. for (j = 0; j < max_sz; j += sz) {
  299. if (nhdr_ptr->n_namesz == 0)
  300. break;
  301. sz = sizeof(Elf32_Nhdr) +
  302. ((nhdr_ptr->n_namesz + 3) & ~3) +
  303. ((nhdr_ptr->n_descsz + 3) & ~3);
  304. real_sz += sz;
  305. nhdr_ptr = (Elf32_Nhdr*)((char*)nhdr_ptr + sz);
  306. }
  307. /* Add this contiguous chunk of notes section to vmcore list.*/
  308. new = get_new_element();
  309. if (!new) {
  310. kfree(notes_section);
  311. return -ENOMEM;
  312. }
  313. new->paddr = phdr_ptr->p_offset;
  314. new->size = real_sz;
  315. list_add_tail(&new->list, vc_list);
  316. phdr_sz += real_sz;
  317. kfree(notes_section);
  318. }
  319. /* Prepare merged PT_NOTE program header. */
  320. phdr.p_type = PT_NOTE;
  321. phdr.p_flags = 0;
  322. note_off = sizeof(Elf32_Ehdr) +
  323. (ehdr_ptr->e_phnum - nr_ptnote +1) * sizeof(Elf32_Phdr);
  324. phdr.p_offset = note_off;
  325. phdr.p_vaddr = phdr.p_paddr = 0;
  326. phdr.p_filesz = phdr.p_memsz = phdr_sz;
  327. phdr.p_align = 0;
  328. /* Add merged PT_NOTE program header*/
  329. tmp = elfptr + sizeof(Elf32_Ehdr);
  330. memcpy(tmp, &phdr, sizeof(phdr));
  331. tmp += sizeof(phdr);
  332. /* Remove unwanted PT_NOTE program headers. */
  333. i = (nr_ptnote - 1) * sizeof(Elf32_Phdr);
  334. *elfsz = *elfsz - i;
  335. memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf32_Ehdr)-sizeof(Elf32_Phdr)));
  336. memset(elfptr + *elfsz, 0, i);
  337. *elfsz = roundup(*elfsz, PAGE_SIZE);
  338. /* Modify e_phnum to reflect merged headers. */
  339. ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1;
  340. return 0;
  341. }
  342. /* Add memory chunks represented by program headers to vmcore list. Also update
  343. * the new offset fields of exported program headers. */
  344. static int __init process_ptload_program_headers_elf64(char *elfptr,
  345. size_t elfsz,
  346. struct list_head *vc_list)
  347. {
  348. int i;
  349. Elf64_Ehdr *ehdr_ptr;
  350. Elf64_Phdr *phdr_ptr;
  351. loff_t vmcore_off;
  352. struct vmcore *new;
  353. ehdr_ptr = (Elf64_Ehdr *)elfptr;
  354. phdr_ptr = (Elf64_Phdr*)(elfptr + sizeof(Elf64_Ehdr)); /* PT_NOTE hdr */
  355. /* First program header is PT_NOTE header. */
  356. vmcore_off = elfsz +
  357. phdr_ptr->p_memsz; /* Note sections */
  358. for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
  359. if (phdr_ptr->p_type != PT_LOAD)
  360. continue;
  361. /* Add this contiguous chunk of memory to vmcore list.*/
  362. new = get_new_element();
  363. if (!new)
  364. return -ENOMEM;
  365. new->paddr = phdr_ptr->p_offset;
  366. new->size = phdr_ptr->p_memsz;
  367. list_add_tail(&new->list, vc_list);
  368. /* Update the program header offset. */
  369. phdr_ptr->p_offset = vmcore_off;
  370. vmcore_off = vmcore_off + phdr_ptr->p_memsz;
  371. }
  372. return 0;
  373. }
  374. static int __init process_ptload_program_headers_elf32(char *elfptr,
  375. size_t elfsz,
  376. struct list_head *vc_list)
  377. {
  378. int i;
  379. Elf32_Ehdr *ehdr_ptr;
  380. Elf32_Phdr *phdr_ptr;
  381. loff_t vmcore_off;
  382. struct vmcore *new;
  383. ehdr_ptr = (Elf32_Ehdr *)elfptr;
  384. phdr_ptr = (Elf32_Phdr*)(elfptr + sizeof(Elf32_Ehdr)); /* PT_NOTE hdr */
  385. /* First program header is PT_NOTE header. */
  386. vmcore_off = elfsz +
  387. phdr_ptr->p_memsz; /* Note sections */
  388. for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
  389. if (phdr_ptr->p_type != PT_LOAD)
  390. continue;
  391. /* Add this contiguous chunk of memory to vmcore list.*/
  392. new = get_new_element();
  393. if (!new)
  394. return -ENOMEM;
  395. new->paddr = phdr_ptr->p_offset;
  396. new->size = phdr_ptr->p_memsz;
  397. list_add_tail(&new->list, vc_list);
  398. /* Update the program header offset */
  399. phdr_ptr->p_offset = vmcore_off;
  400. vmcore_off = vmcore_off + phdr_ptr->p_memsz;
  401. }
  402. return 0;
  403. }
  404. /* Sets offset fields of vmcore elements. */
  405. static void __init set_vmcore_list_offsets(size_t elfsz,
  406. struct list_head *vc_list)
  407. {
  408. loff_t vmcore_off;
  409. struct vmcore *m;
  410. /* Skip Elf header and program headers. */
  411. vmcore_off = elfsz;
  412. list_for_each_entry(m, vc_list, list) {
  413. m->offset = vmcore_off;
  414. vmcore_off += m->size;
  415. }
  416. }
  417. static void free_elfcorebuf(void)
  418. {
  419. free_pages((unsigned long)elfcorebuf, get_order(elfcorebuf_sz_orig));
  420. elfcorebuf = NULL;
  421. }
  422. static int __init parse_crash_elf64_headers(void)
  423. {
  424. int rc=0;
  425. Elf64_Ehdr ehdr;
  426. u64 addr;
  427. addr = elfcorehdr_addr;
  428. /* Read Elf header */
  429. rc = read_from_oldmem((char*)&ehdr, sizeof(Elf64_Ehdr), &addr, 0);
  430. if (rc < 0)
  431. return rc;
  432. /* Do some basic Verification. */
  433. if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
  434. (ehdr.e_type != ET_CORE) ||
  435. !vmcore_elf64_check_arch(&ehdr) ||
  436. ehdr.e_ident[EI_CLASS] != ELFCLASS64 ||
  437. ehdr.e_ident[EI_VERSION] != EV_CURRENT ||
  438. ehdr.e_version != EV_CURRENT ||
  439. ehdr.e_ehsize != sizeof(Elf64_Ehdr) ||
  440. ehdr.e_phentsize != sizeof(Elf64_Phdr) ||
  441. ehdr.e_phnum == 0) {
  442. pr_warn("Warning: Core image elf header is not sane\n");
  443. return -EINVAL;
  444. }
  445. /* Read in all elf headers. */
  446. elfcorebuf_sz_orig = sizeof(Elf64_Ehdr) +
  447. ehdr.e_phnum * sizeof(Elf64_Phdr);
  448. elfcorebuf_sz = elfcorebuf_sz_orig;
  449. elfcorebuf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  450. get_order(elfcorebuf_sz_orig));
  451. if (!elfcorebuf)
  452. return -ENOMEM;
  453. addr = elfcorehdr_addr;
  454. rc = read_from_oldmem(elfcorebuf, elfcorebuf_sz_orig, &addr, 0);
  455. if (rc < 0)
  456. goto fail;
  457. /* Merge all PT_NOTE headers into one. */
  458. rc = merge_note_headers_elf64(elfcorebuf, &elfcorebuf_sz, &vmcore_list);
  459. if (rc)
  460. goto fail;
  461. rc = process_ptload_program_headers_elf64(elfcorebuf, elfcorebuf_sz,
  462. &vmcore_list);
  463. if (rc)
  464. goto fail;
  465. set_vmcore_list_offsets(elfcorebuf_sz, &vmcore_list);
  466. return 0;
  467. fail:
  468. free_elfcorebuf();
  469. return rc;
  470. }
  471. static int __init parse_crash_elf32_headers(void)
  472. {
  473. int rc=0;
  474. Elf32_Ehdr ehdr;
  475. u64 addr;
  476. addr = elfcorehdr_addr;
  477. /* Read Elf header */
  478. rc = read_from_oldmem((char*)&ehdr, sizeof(Elf32_Ehdr), &addr, 0);
  479. if (rc < 0)
  480. return rc;
  481. /* Do some basic Verification. */
  482. if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
  483. (ehdr.e_type != ET_CORE) ||
  484. !elf_check_arch(&ehdr) ||
  485. ehdr.e_ident[EI_CLASS] != ELFCLASS32||
  486. ehdr.e_ident[EI_VERSION] != EV_CURRENT ||
  487. ehdr.e_version != EV_CURRENT ||
  488. ehdr.e_ehsize != sizeof(Elf32_Ehdr) ||
  489. ehdr.e_phentsize != sizeof(Elf32_Phdr) ||
  490. ehdr.e_phnum == 0) {
  491. pr_warn("Warning: Core image elf header is not sane\n");
  492. return -EINVAL;
  493. }
  494. /* Read in all elf headers. */
  495. elfcorebuf_sz_orig = sizeof(Elf32_Ehdr) + ehdr.e_phnum * sizeof(Elf32_Phdr);
  496. elfcorebuf_sz = elfcorebuf_sz_orig;
  497. elfcorebuf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  498. get_order(elfcorebuf_sz_orig));
  499. if (!elfcorebuf)
  500. return -ENOMEM;
  501. addr = elfcorehdr_addr;
  502. rc = read_from_oldmem(elfcorebuf, elfcorebuf_sz_orig, &addr, 0);
  503. if (rc < 0)
  504. goto fail;
  505. /* Merge all PT_NOTE headers into one. */
  506. rc = merge_note_headers_elf32(elfcorebuf, &elfcorebuf_sz, &vmcore_list);
  507. if (rc)
  508. goto fail;
  509. rc = process_ptload_program_headers_elf32(elfcorebuf, elfcorebuf_sz,
  510. &vmcore_list);
  511. if (rc)
  512. goto fail;
  513. set_vmcore_list_offsets(elfcorebuf_sz, &vmcore_list);
  514. return 0;
  515. fail:
  516. free_elfcorebuf();
  517. return rc;
  518. }
  519. static int __init parse_crash_elf_headers(void)
  520. {
  521. unsigned char e_ident[EI_NIDENT];
  522. u64 addr;
  523. int rc=0;
  524. addr = elfcorehdr_addr;
  525. rc = read_from_oldmem(e_ident, EI_NIDENT, &addr, 0);
  526. if (rc < 0)
  527. return rc;
  528. if (memcmp(e_ident, ELFMAG, SELFMAG) != 0) {
  529. pr_warn("Warning: Core image elf header not found\n");
  530. return -EINVAL;
  531. }
  532. if (e_ident[EI_CLASS] == ELFCLASS64) {
  533. rc = parse_crash_elf64_headers();
  534. if (rc)
  535. return rc;
  536. /* Determine vmcore size. */
  537. vmcore_size = get_vmcore_size_elf64(elfcorebuf, elfcorebuf_sz);
  538. } else if (e_ident[EI_CLASS] == ELFCLASS32) {
  539. rc = parse_crash_elf32_headers();
  540. if (rc)
  541. return rc;
  542. /* Determine vmcore size. */
  543. vmcore_size = get_vmcore_size_elf32(elfcorebuf, elfcorebuf_sz);
  544. } else {
  545. pr_warn("Warning: Core image elf header is not sane\n");
  546. return -EINVAL;
  547. }
  548. return 0;
  549. }
  550. /* Init function for vmcore module. */
  551. static int __init vmcore_init(void)
  552. {
  553. int rc = 0;
  554. /* If elfcorehdr= has been passed in cmdline, then capture the dump.*/
  555. if (!(is_vmcore_usable()))
  556. return rc;
  557. rc = parse_crash_elf_headers();
  558. if (rc) {
  559. pr_warn("Kdump: vmcore not initialized\n");
  560. return rc;
  561. }
  562. proc_vmcore = proc_create("vmcore", S_IRUSR, NULL, &proc_vmcore_operations);
  563. if (proc_vmcore)
  564. proc_vmcore->size = vmcore_size;
  565. return 0;
  566. }
  567. module_init(vmcore_init)
  568. /* Cleanup function for vmcore module. */
  569. void vmcore_cleanup(void)
  570. {
  571. struct list_head *pos, *next;
  572. if (proc_vmcore) {
  573. proc_remove(proc_vmcore);
  574. proc_vmcore = NULL;
  575. }
  576. /* clear the vmcore list. */
  577. list_for_each_safe(pos, next, &vmcore_list) {
  578. struct vmcore *m;
  579. m = list_entry(pos, struct vmcore, list);
  580. list_del(&m->list);
  581. kfree(m);
  582. }
  583. free_elfcorebuf();
  584. }
  585. EXPORT_SYMBOL_GPL(vmcore_cleanup);