vmcore.c 17 KB

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