vmcore.c 16 KB

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