vmcore.c 16 KB

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