vmcore.c 16 KB

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