vmcore.c 16 KB

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