vmcore.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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. const struct file_operations proc_vmcore_operations = {
  146. .read = read_vmcore,
  147. };
  148. static struct vmcore* __init get_new_element(void)
  149. {
  150. struct vmcore *p;
  151. p = kmalloc(sizeof(*p), GFP_KERNEL);
  152. if (p)
  153. memset(p, 0, sizeof(*p));
  154. return p;
  155. }
  156. static u64 __init get_vmcore_size_elf64(char *elfptr)
  157. {
  158. int i;
  159. u64 size;
  160. Elf64_Ehdr *ehdr_ptr;
  161. Elf64_Phdr *phdr_ptr;
  162. ehdr_ptr = (Elf64_Ehdr *)elfptr;
  163. phdr_ptr = (Elf64_Phdr*)(elfptr + sizeof(Elf64_Ehdr));
  164. size = sizeof(Elf64_Ehdr) + ((ehdr_ptr->e_phnum) * sizeof(Elf64_Phdr));
  165. for (i = 0; i < ehdr_ptr->e_phnum; i++) {
  166. size += phdr_ptr->p_memsz;
  167. phdr_ptr++;
  168. }
  169. return size;
  170. }
  171. static u64 __init get_vmcore_size_elf32(char *elfptr)
  172. {
  173. int i;
  174. u64 size;
  175. Elf32_Ehdr *ehdr_ptr;
  176. Elf32_Phdr *phdr_ptr;
  177. ehdr_ptr = (Elf32_Ehdr *)elfptr;
  178. phdr_ptr = (Elf32_Phdr*)(elfptr + sizeof(Elf32_Ehdr));
  179. size = sizeof(Elf32_Ehdr) + ((ehdr_ptr->e_phnum) * sizeof(Elf32_Phdr));
  180. for (i = 0; i < ehdr_ptr->e_phnum; i++) {
  181. size += phdr_ptr->p_memsz;
  182. phdr_ptr++;
  183. }
  184. return size;
  185. }
  186. /* Merges all the PT_NOTE headers into one. */
  187. static int __init merge_note_headers_elf64(char *elfptr, size_t *elfsz,
  188. struct list_head *vc_list)
  189. {
  190. int i, nr_ptnote=0, rc=0;
  191. char *tmp;
  192. Elf64_Ehdr *ehdr_ptr;
  193. Elf64_Phdr phdr, *phdr_ptr;
  194. Elf64_Nhdr *nhdr_ptr;
  195. u64 phdr_sz = 0, note_off;
  196. ehdr_ptr = (Elf64_Ehdr *)elfptr;
  197. phdr_ptr = (Elf64_Phdr*)(elfptr + sizeof(Elf64_Ehdr));
  198. for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
  199. int j;
  200. void *notes_section;
  201. struct vmcore *new;
  202. u64 offset, max_sz, sz, real_sz = 0;
  203. if (phdr_ptr->p_type != PT_NOTE)
  204. continue;
  205. nr_ptnote++;
  206. max_sz = phdr_ptr->p_memsz;
  207. offset = phdr_ptr->p_offset;
  208. notes_section = kmalloc(max_sz, GFP_KERNEL);
  209. if (!notes_section)
  210. return -ENOMEM;
  211. rc = read_from_oldmem(notes_section, max_sz, &offset, 0);
  212. if (rc < 0) {
  213. kfree(notes_section);
  214. return rc;
  215. }
  216. nhdr_ptr = notes_section;
  217. for (j = 0; j < max_sz; j += sz) {
  218. if (nhdr_ptr->n_namesz == 0)
  219. break;
  220. sz = sizeof(Elf64_Nhdr) +
  221. ((nhdr_ptr->n_namesz + 3) & ~3) +
  222. ((nhdr_ptr->n_descsz + 3) & ~3);
  223. real_sz += sz;
  224. nhdr_ptr = (Elf64_Nhdr*)((char*)nhdr_ptr + sz);
  225. }
  226. /* Add this contiguous chunk of notes section to vmcore list.*/
  227. new = get_new_element();
  228. if (!new) {
  229. kfree(notes_section);
  230. return -ENOMEM;
  231. }
  232. new->paddr = phdr_ptr->p_offset;
  233. new->size = real_sz;
  234. list_add_tail(&new->list, vc_list);
  235. phdr_sz += real_sz;
  236. kfree(notes_section);
  237. }
  238. /* Prepare merged PT_NOTE program header. */
  239. phdr.p_type = PT_NOTE;
  240. phdr.p_flags = 0;
  241. note_off = sizeof(Elf64_Ehdr) +
  242. (ehdr_ptr->e_phnum - nr_ptnote +1) * sizeof(Elf64_Phdr);
  243. phdr.p_offset = note_off;
  244. phdr.p_vaddr = phdr.p_paddr = 0;
  245. phdr.p_filesz = phdr.p_memsz = phdr_sz;
  246. phdr.p_align = 0;
  247. /* Add merged PT_NOTE program header*/
  248. tmp = elfptr + sizeof(Elf64_Ehdr);
  249. memcpy(tmp, &phdr, sizeof(phdr));
  250. tmp += sizeof(phdr);
  251. /* Remove unwanted PT_NOTE program headers. */
  252. i = (nr_ptnote - 1) * sizeof(Elf64_Phdr);
  253. *elfsz = *elfsz - i;
  254. memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf64_Ehdr)-sizeof(Elf64_Phdr)));
  255. /* Modify e_phnum to reflect merged headers. */
  256. ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1;
  257. return 0;
  258. }
  259. /* Merges all the PT_NOTE headers into one. */
  260. static int __init merge_note_headers_elf32(char *elfptr, size_t *elfsz,
  261. struct list_head *vc_list)
  262. {
  263. int i, nr_ptnote=0, rc=0;
  264. char *tmp;
  265. Elf32_Ehdr *ehdr_ptr;
  266. Elf32_Phdr phdr, *phdr_ptr;
  267. Elf32_Nhdr *nhdr_ptr;
  268. u64 phdr_sz = 0, note_off;
  269. ehdr_ptr = (Elf32_Ehdr *)elfptr;
  270. phdr_ptr = (Elf32_Phdr*)(elfptr + sizeof(Elf32_Ehdr));
  271. for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
  272. int j;
  273. void *notes_section;
  274. struct vmcore *new;
  275. u64 offset, max_sz, sz, real_sz = 0;
  276. if (phdr_ptr->p_type != PT_NOTE)
  277. continue;
  278. nr_ptnote++;
  279. max_sz = phdr_ptr->p_memsz;
  280. offset = phdr_ptr->p_offset;
  281. notes_section = kmalloc(max_sz, GFP_KERNEL);
  282. if (!notes_section)
  283. return -ENOMEM;
  284. rc = read_from_oldmem(notes_section, max_sz, &offset, 0);
  285. if (rc < 0) {
  286. kfree(notes_section);
  287. return rc;
  288. }
  289. nhdr_ptr = notes_section;
  290. for (j = 0; j < max_sz; j += sz) {
  291. if (nhdr_ptr->n_namesz == 0)
  292. break;
  293. sz = sizeof(Elf32_Nhdr) +
  294. ((nhdr_ptr->n_namesz + 3) & ~3) +
  295. ((nhdr_ptr->n_descsz + 3) & ~3);
  296. real_sz += sz;
  297. nhdr_ptr = (Elf32_Nhdr*)((char*)nhdr_ptr + sz);
  298. }
  299. /* Add this contiguous chunk of notes section to vmcore list.*/
  300. new = get_new_element();
  301. if (!new) {
  302. kfree(notes_section);
  303. return -ENOMEM;
  304. }
  305. new->paddr = phdr_ptr->p_offset;
  306. new->size = real_sz;
  307. list_add_tail(&new->list, vc_list);
  308. phdr_sz += real_sz;
  309. kfree(notes_section);
  310. }
  311. /* Prepare merged PT_NOTE program header. */
  312. phdr.p_type = PT_NOTE;
  313. phdr.p_flags = 0;
  314. note_off = sizeof(Elf32_Ehdr) +
  315. (ehdr_ptr->e_phnum - nr_ptnote +1) * sizeof(Elf32_Phdr);
  316. phdr.p_offset = note_off;
  317. phdr.p_vaddr = phdr.p_paddr = 0;
  318. phdr.p_filesz = phdr.p_memsz = phdr_sz;
  319. phdr.p_align = 0;
  320. /* Add merged PT_NOTE program header*/
  321. tmp = elfptr + sizeof(Elf32_Ehdr);
  322. memcpy(tmp, &phdr, sizeof(phdr));
  323. tmp += sizeof(phdr);
  324. /* Remove unwanted PT_NOTE program headers. */
  325. i = (nr_ptnote - 1) * sizeof(Elf32_Phdr);
  326. *elfsz = *elfsz - i;
  327. memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf32_Ehdr)-sizeof(Elf32_Phdr)));
  328. /* Modify e_phnum to reflect merged headers. */
  329. ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1;
  330. return 0;
  331. }
  332. /* Add memory chunks represented by program headers to vmcore list. Also update
  333. * the new offset fields of exported program headers. */
  334. static int __init process_ptload_program_headers_elf64(char *elfptr,
  335. size_t elfsz,
  336. struct list_head *vc_list)
  337. {
  338. int i;
  339. Elf64_Ehdr *ehdr_ptr;
  340. Elf64_Phdr *phdr_ptr;
  341. loff_t vmcore_off;
  342. struct vmcore *new;
  343. ehdr_ptr = (Elf64_Ehdr *)elfptr;
  344. phdr_ptr = (Elf64_Phdr*)(elfptr + sizeof(Elf64_Ehdr)); /* PT_NOTE hdr */
  345. /* First program header is PT_NOTE header. */
  346. vmcore_off = sizeof(Elf64_Ehdr) +
  347. (ehdr_ptr->e_phnum) * sizeof(Elf64_Phdr) +
  348. phdr_ptr->p_memsz; /* Note sections */
  349. for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
  350. if (phdr_ptr->p_type != PT_LOAD)
  351. continue;
  352. /* Add this contiguous chunk of memory to vmcore list.*/
  353. new = get_new_element();
  354. if (!new)
  355. return -ENOMEM;
  356. new->paddr = phdr_ptr->p_offset;
  357. new->size = phdr_ptr->p_memsz;
  358. list_add_tail(&new->list, vc_list);
  359. /* Update the program header offset. */
  360. phdr_ptr->p_offset = vmcore_off;
  361. vmcore_off = vmcore_off + phdr_ptr->p_memsz;
  362. }
  363. return 0;
  364. }
  365. static int __init process_ptload_program_headers_elf32(char *elfptr,
  366. size_t elfsz,
  367. struct list_head *vc_list)
  368. {
  369. int i;
  370. Elf32_Ehdr *ehdr_ptr;
  371. Elf32_Phdr *phdr_ptr;
  372. loff_t vmcore_off;
  373. struct vmcore *new;
  374. ehdr_ptr = (Elf32_Ehdr *)elfptr;
  375. phdr_ptr = (Elf32_Phdr*)(elfptr + sizeof(Elf32_Ehdr)); /* PT_NOTE hdr */
  376. /* First program header is PT_NOTE header. */
  377. vmcore_off = sizeof(Elf32_Ehdr) +
  378. (ehdr_ptr->e_phnum) * sizeof(Elf32_Phdr) +
  379. phdr_ptr->p_memsz; /* Note sections */
  380. for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
  381. if (phdr_ptr->p_type != PT_LOAD)
  382. continue;
  383. /* Add this contiguous chunk of memory to vmcore list.*/
  384. new = get_new_element();
  385. if (!new)
  386. return -ENOMEM;
  387. new->paddr = phdr_ptr->p_offset;
  388. new->size = phdr_ptr->p_memsz;
  389. list_add_tail(&new->list, vc_list);
  390. /* Update the program header offset */
  391. phdr_ptr->p_offset = vmcore_off;
  392. vmcore_off = vmcore_off + phdr_ptr->p_memsz;
  393. }
  394. return 0;
  395. }
  396. /* Sets offset fields of vmcore elements. */
  397. static void __init set_vmcore_list_offsets_elf64(char *elfptr,
  398. struct list_head *vc_list)
  399. {
  400. loff_t vmcore_off;
  401. Elf64_Ehdr *ehdr_ptr;
  402. struct vmcore *m;
  403. ehdr_ptr = (Elf64_Ehdr *)elfptr;
  404. /* Skip Elf header and program headers. */
  405. vmcore_off = sizeof(Elf64_Ehdr) +
  406. (ehdr_ptr->e_phnum) * sizeof(Elf64_Phdr);
  407. list_for_each_entry(m, vc_list, list) {
  408. m->offset = vmcore_off;
  409. vmcore_off += m->size;
  410. }
  411. }
  412. /* Sets offset fields of vmcore elements. */
  413. static void __init set_vmcore_list_offsets_elf32(char *elfptr,
  414. struct list_head *vc_list)
  415. {
  416. loff_t vmcore_off;
  417. Elf32_Ehdr *ehdr_ptr;
  418. struct vmcore *m;
  419. ehdr_ptr = (Elf32_Ehdr *)elfptr;
  420. /* Skip Elf header and program headers. */
  421. vmcore_off = sizeof(Elf32_Ehdr) +
  422. (ehdr_ptr->e_phnum) * sizeof(Elf32_Phdr);
  423. list_for_each_entry(m, vc_list, list) {
  424. m->offset = vmcore_off;
  425. vmcore_off += m->size;
  426. }
  427. }
  428. static int __init parse_crash_elf64_headers(void)
  429. {
  430. int rc=0;
  431. Elf64_Ehdr ehdr;
  432. u64 addr;
  433. addr = elfcorehdr_addr;
  434. /* Read Elf header */
  435. rc = read_from_oldmem((char*)&ehdr, sizeof(Elf64_Ehdr), &addr, 0);
  436. if (rc < 0)
  437. return rc;
  438. /* Do some basic Verification. */
  439. if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
  440. (ehdr.e_type != ET_CORE) ||
  441. !vmcore_elf_check_arch(&ehdr) ||
  442. ehdr.e_ident[EI_CLASS] != ELFCLASS64 ||
  443. ehdr.e_ident[EI_VERSION] != EV_CURRENT ||
  444. ehdr.e_version != EV_CURRENT ||
  445. ehdr.e_ehsize != sizeof(Elf64_Ehdr) ||
  446. ehdr.e_phentsize != sizeof(Elf64_Phdr) ||
  447. ehdr.e_phnum == 0) {
  448. printk(KERN_WARNING "Warning: Core image elf header is not"
  449. "sane\n");
  450. return -EINVAL;
  451. }
  452. /* Read in all elf headers. */
  453. elfcorebuf_sz = sizeof(Elf64_Ehdr) + ehdr.e_phnum * sizeof(Elf64_Phdr);
  454. elfcorebuf = kmalloc(elfcorebuf_sz, GFP_KERNEL);
  455. if (!elfcorebuf)
  456. return -ENOMEM;
  457. addr = elfcorehdr_addr;
  458. rc = read_from_oldmem(elfcorebuf, elfcorebuf_sz, &addr, 0);
  459. if (rc < 0) {
  460. kfree(elfcorebuf);
  461. return rc;
  462. }
  463. /* Merge all PT_NOTE headers into one. */
  464. rc = merge_note_headers_elf64(elfcorebuf, &elfcorebuf_sz, &vmcore_list);
  465. if (rc) {
  466. kfree(elfcorebuf);
  467. return rc;
  468. }
  469. rc = process_ptload_program_headers_elf64(elfcorebuf, elfcorebuf_sz,
  470. &vmcore_list);
  471. if (rc) {
  472. kfree(elfcorebuf);
  473. return rc;
  474. }
  475. set_vmcore_list_offsets_elf64(elfcorebuf, &vmcore_list);
  476. return 0;
  477. }
  478. static int __init parse_crash_elf32_headers(void)
  479. {
  480. int rc=0;
  481. Elf32_Ehdr ehdr;
  482. u64 addr;
  483. addr = elfcorehdr_addr;
  484. /* Read Elf header */
  485. rc = read_from_oldmem((char*)&ehdr, sizeof(Elf32_Ehdr), &addr, 0);
  486. if (rc < 0)
  487. return rc;
  488. /* Do some basic Verification. */
  489. if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
  490. (ehdr.e_type != ET_CORE) ||
  491. !elf_check_arch(&ehdr) ||
  492. ehdr.e_ident[EI_CLASS] != ELFCLASS32||
  493. ehdr.e_ident[EI_VERSION] != EV_CURRENT ||
  494. ehdr.e_version != EV_CURRENT ||
  495. ehdr.e_ehsize != sizeof(Elf32_Ehdr) ||
  496. ehdr.e_phentsize != sizeof(Elf32_Phdr) ||
  497. ehdr.e_phnum == 0) {
  498. printk(KERN_WARNING "Warning: Core image elf header is not"
  499. "sane\n");
  500. return -EINVAL;
  501. }
  502. /* Read in all elf headers. */
  503. elfcorebuf_sz = sizeof(Elf32_Ehdr) + ehdr.e_phnum * sizeof(Elf32_Phdr);
  504. elfcorebuf = kmalloc(elfcorebuf_sz, GFP_KERNEL);
  505. if (!elfcorebuf)
  506. return -ENOMEM;
  507. addr = elfcorehdr_addr;
  508. rc = read_from_oldmem(elfcorebuf, elfcorebuf_sz, &addr, 0);
  509. if (rc < 0) {
  510. kfree(elfcorebuf);
  511. return rc;
  512. }
  513. /* Merge all PT_NOTE headers into one. */
  514. rc = merge_note_headers_elf32(elfcorebuf, &elfcorebuf_sz, &vmcore_list);
  515. if (rc) {
  516. kfree(elfcorebuf);
  517. return rc;
  518. }
  519. rc = process_ptload_program_headers_elf32(elfcorebuf, elfcorebuf_sz,
  520. &vmcore_list);
  521. if (rc) {
  522. kfree(elfcorebuf);
  523. return rc;
  524. }
  525. set_vmcore_list_offsets_elf32(elfcorebuf, &vmcore_list);
  526. return 0;
  527. }
  528. static int __init parse_crash_elf_headers(void)
  529. {
  530. unsigned char e_ident[EI_NIDENT];
  531. u64 addr;
  532. int rc=0;
  533. addr = elfcorehdr_addr;
  534. rc = read_from_oldmem(e_ident, EI_NIDENT, &addr, 0);
  535. if (rc < 0)
  536. return rc;
  537. if (memcmp(e_ident, ELFMAG, SELFMAG) != 0) {
  538. printk(KERN_WARNING "Warning: Core image elf header"
  539. " not found\n");
  540. return -EINVAL;
  541. }
  542. if (e_ident[EI_CLASS] == ELFCLASS64) {
  543. rc = parse_crash_elf64_headers();
  544. if (rc)
  545. return rc;
  546. /* Determine vmcore size. */
  547. vmcore_size = get_vmcore_size_elf64(elfcorebuf);
  548. } else if (e_ident[EI_CLASS] == ELFCLASS32) {
  549. rc = parse_crash_elf32_headers();
  550. if (rc)
  551. return rc;
  552. /* Determine vmcore size. */
  553. vmcore_size = get_vmcore_size_elf32(elfcorebuf);
  554. } else {
  555. printk(KERN_WARNING "Warning: Core image elf header is not"
  556. " sane\n");
  557. return -EINVAL;
  558. }
  559. return 0;
  560. }
  561. /* Init function for vmcore module. */
  562. static int __init vmcore_init(void)
  563. {
  564. int rc = 0;
  565. /* If elfcorehdr= has been passed in cmdline, then capture the dump.*/
  566. if (!(elfcorehdr_addr < ELFCORE_ADDR_MAX))
  567. return rc;
  568. rc = parse_crash_elf_headers();
  569. if (rc) {
  570. printk(KERN_WARNING "Kdump: vmcore not initialized\n");
  571. return rc;
  572. }
  573. /* Initialize /proc/vmcore size if proc is already up. */
  574. if (proc_vmcore)
  575. proc_vmcore->size = vmcore_size;
  576. return 0;
  577. }
  578. module_init(vmcore_init)