vmcore.c 17 KB

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