vmcore.c 16 KB

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