vmcore.c 16 KB

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