vmcore.c 17 KB

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