vmcore.c 17 KB

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