vmcore.c 16 KB

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