vmcore.c 18 KB

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