vmcore.c 16 KB

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