vmcore.c 16 KB

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