vmcore.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551
  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/kcore.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 <linux/mutex.h>
  23. #include <linux/vmalloc.h>
  24. #include <linux/pagemap.h>
  25. #include <linux/uaccess.h>
  26. #include <asm/io.h>
  27. #include "internal.h"
  28. /* List representing chunks of contiguous memory areas and their offsets in
  29. * vmcore file.
  30. */
  31. static LIST_HEAD(vmcore_list);
  32. /* Stores the pointer to the buffer containing kernel elf core headers. */
  33. static char *elfcorebuf;
  34. static size_t elfcorebuf_sz;
  35. static size_t elfcorebuf_sz_orig;
  36. static char *elfnotes_buf;
  37. static size_t elfnotes_sz;
  38. /* Size of all notes minus the device dump notes */
  39. static size_t elfnotes_orig_sz;
  40. /* Total size of vmcore file. */
  41. static u64 vmcore_size;
  42. static struct proc_dir_entry *proc_vmcore;
  43. #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP
  44. /* Device Dump list and mutex to synchronize access to list */
  45. static LIST_HEAD(vmcoredd_list);
  46. static DEFINE_MUTEX(vmcoredd_mutex);
  47. #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */
  48. /* Device Dump Size */
  49. static size_t vmcoredd_orig_sz;
  50. /*
  51. * Returns > 0 for RAM pages, 0 for non-RAM pages, < 0 on error
  52. * The called function has to take care of module refcounting.
  53. */
  54. static int (*oldmem_pfn_is_ram)(unsigned long pfn);
  55. int register_oldmem_pfn_is_ram(int (*fn)(unsigned long pfn))
  56. {
  57. if (oldmem_pfn_is_ram)
  58. return -EBUSY;
  59. oldmem_pfn_is_ram = fn;
  60. return 0;
  61. }
  62. EXPORT_SYMBOL_GPL(register_oldmem_pfn_is_ram);
  63. void unregister_oldmem_pfn_is_ram(void)
  64. {
  65. oldmem_pfn_is_ram = NULL;
  66. wmb();
  67. }
  68. EXPORT_SYMBOL_GPL(unregister_oldmem_pfn_is_ram);
  69. static int pfn_is_ram(unsigned long pfn)
  70. {
  71. int (*fn)(unsigned long pfn);
  72. /* pfn is ram unless fn() checks pagetype */
  73. int ret = 1;
  74. /*
  75. * Ask hypervisor if the pfn is really ram.
  76. * A ballooned page contains no data and reading from such a page
  77. * will cause high load in the hypervisor.
  78. */
  79. fn = oldmem_pfn_is_ram;
  80. if (fn)
  81. ret = fn(pfn);
  82. return ret;
  83. }
  84. /* Reads a page from the oldmem device from given offset. */
  85. static ssize_t read_from_oldmem(char *buf, size_t count,
  86. u64 *ppos, int userbuf)
  87. {
  88. unsigned long pfn, offset;
  89. size_t nr_bytes;
  90. ssize_t read = 0, tmp;
  91. if (!count)
  92. return 0;
  93. offset = (unsigned long)(*ppos % PAGE_SIZE);
  94. pfn = (unsigned long)(*ppos / PAGE_SIZE);
  95. do {
  96. if (count > (PAGE_SIZE - offset))
  97. nr_bytes = PAGE_SIZE - offset;
  98. else
  99. nr_bytes = count;
  100. /* If pfn is not ram, return zeros for sparse dump files */
  101. if (pfn_is_ram(pfn) == 0)
  102. memset(buf, 0, nr_bytes);
  103. else {
  104. tmp = copy_oldmem_page(pfn, buf, nr_bytes,
  105. offset, userbuf);
  106. if (tmp < 0)
  107. return tmp;
  108. }
  109. *ppos += nr_bytes;
  110. count -= nr_bytes;
  111. buf += nr_bytes;
  112. read += nr_bytes;
  113. ++pfn;
  114. offset = 0;
  115. } while (count);
  116. return read;
  117. }
  118. /*
  119. * Architectures may override this function to allocate ELF header in 2nd kernel
  120. */
  121. int __weak elfcorehdr_alloc(unsigned long long *addr, unsigned long long *size)
  122. {
  123. return 0;
  124. }
  125. /*
  126. * Architectures may override this function to free header
  127. */
  128. void __weak elfcorehdr_free(unsigned long long addr)
  129. {}
  130. /*
  131. * Architectures may override this function to read from ELF header
  132. */
  133. ssize_t __weak elfcorehdr_read(char *buf, size_t count, u64 *ppos)
  134. {
  135. return read_from_oldmem(buf, count, ppos, 0);
  136. }
  137. /*
  138. * Architectures may override this function to read from notes sections
  139. */
  140. ssize_t __weak elfcorehdr_read_notes(char *buf, size_t count, u64 *ppos)
  141. {
  142. return read_from_oldmem(buf, count, ppos, 0);
  143. }
  144. /*
  145. * Architectures may override this function to map oldmem
  146. */
  147. int __weak remap_oldmem_pfn_range(struct vm_area_struct *vma,
  148. unsigned long from, unsigned long pfn,
  149. unsigned long size, pgprot_t prot)
  150. {
  151. return remap_pfn_range(vma, from, pfn, size, prot);
  152. }
  153. /*
  154. * Copy to either kernel or user space
  155. */
  156. static int copy_to(void *target, void *src, size_t size, int userbuf)
  157. {
  158. if (userbuf) {
  159. if (copy_to_user((char __user *) target, src, size))
  160. return -EFAULT;
  161. } else {
  162. memcpy(target, src, size);
  163. }
  164. return 0;
  165. }
  166. #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP
  167. static int vmcoredd_copy_dumps(void *dst, u64 start, size_t size, int userbuf)
  168. {
  169. struct vmcoredd_node *dump;
  170. u64 offset = 0;
  171. int ret = 0;
  172. size_t tsz;
  173. char *buf;
  174. mutex_lock(&vmcoredd_mutex);
  175. list_for_each_entry(dump, &vmcoredd_list, list) {
  176. if (start < offset + dump->size) {
  177. tsz = min(offset + (u64)dump->size - start, (u64)size);
  178. buf = dump->buf + start - offset;
  179. if (copy_to(dst, buf, tsz, userbuf)) {
  180. ret = -EFAULT;
  181. goto out_unlock;
  182. }
  183. size -= tsz;
  184. start += tsz;
  185. dst += tsz;
  186. /* Leave now if buffer filled already */
  187. if (!size)
  188. goto out_unlock;
  189. }
  190. offset += dump->size;
  191. }
  192. out_unlock:
  193. mutex_unlock(&vmcoredd_mutex);
  194. return ret;
  195. }
  196. static int vmcoredd_mmap_dumps(struct vm_area_struct *vma, unsigned long dst,
  197. u64 start, size_t size)
  198. {
  199. struct vmcoredd_node *dump;
  200. u64 offset = 0;
  201. int ret = 0;
  202. size_t tsz;
  203. char *buf;
  204. mutex_lock(&vmcoredd_mutex);
  205. list_for_each_entry(dump, &vmcoredd_list, list) {
  206. if (start < offset + dump->size) {
  207. tsz = min(offset + (u64)dump->size - start, (u64)size);
  208. buf = dump->buf + start - offset;
  209. if (remap_vmalloc_range_partial(vma, dst, buf, tsz)) {
  210. ret = -EFAULT;
  211. goto out_unlock;
  212. }
  213. size -= tsz;
  214. start += tsz;
  215. dst += tsz;
  216. /* Leave now if buffer filled already */
  217. if (!size)
  218. goto out_unlock;
  219. }
  220. offset += dump->size;
  221. }
  222. out_unlock:
  223. mutex_unlock(&vmcoredd_mutex);
  224. return ret;
  225. }
  226. #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */
  227. /* Read from the ELF header and then the crash dump. On error, negative value is
  228. * returned otherwise number of bytes read are returned.
  229. */
  230. static ssize_t __read_vmcore(char *buffer, size_t buflen, loff_t *fpos,
  231. int userbuf)
  232. {
  233. ssize_t acc = 0, tmp;
  234. size_t tsz;
  235. u64 start;
  236. struct vmcore *m = NULL;
  237. if (buflen == 0 || *fpos >= vmcore_size)
  238. return 0;
  239. /* trim buflen to not go beyond EOF */
  240. if (buflen > vmcore_size - *fpos)
  241. buflen = vmcore_size - *fpos;
  242. /* Read ELF core header */
  243. if (*fpos < elfcorebuf_sz) {
  244. tsz = min(elfcorebuf_sz - (size_t)*fpos, buflen);
  245. if (copy_to(buffer, elfcorebuf + *fpos, tsz, userbuf))
  246. return -EFAULT;
  247. buflen -= tsz;
  248. *fpos += tsz;
  249. buffer += tsz;
  250. acc += tsz;
  251. /* leave now if filled buffer already */
  252. if (buflen == 0)
  253. return acc;
  254. }
  255. /* Read Elf note segment */
  256. if (*fpos < elfcorebuf_sz + elfnotes_sz) {
  257. void *kaddr;
  258. /* We add device dumps before other elf notes because the
  259. * other elf notes may not fill the elf notes buffer
  260. * completely and we will end up with zero-filled data
  261. * between the elf notes and the device dumps. Tools will
  262. * then try to decode this zero-filled data as valid notes
  263. * and we don't want that. Hence, adding device dumps before
  264. * the other elf notes ensure that zero-filled data can be
  265. * avoided.
  266. */
  267. #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP
  268. /* Read device dumps */
  269. if (*fpos < elfcorebuf_sz + vmcoredd_orig_sz) {
  270. tsz = min(elfcorebuf_sz + vmcoredd_orig_sz -
  271. (size_t)*fpos, buflen);
  272. start = *fpos - elfcorebuf_sz;
  273. if (vmcoredd_copy_dumps(buffer, start, tsz, userbuf))
  274. return -EFAULT;
  275. buflen -= tsz;
  276. *fpos += tsz;
  277. buffer += tsz;
  278. acc += tsz;
  279. /* leave now if filled buffer already */
  280. if (!buflen)
  281. return acc;
  282. }
  283. #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */
  284. /* Read remaining elf notes */
  285. tsz = min(elfcorebuf_sz + elfnotes_sz - (size_t)*fpos, buflen);
  286. kaddr = elfnotes_buf + *fpos - elfcorebuf_sz - vmcoredd_orig_sz;
  287. if (copy_to(buffer, kaddr, tsz, userbuf))
  288. return -EFAULT;
  289. buflen -= tsz;
  290. *fpos += tsz;
  291. buffer += tsz;
  292. acc += tsz;
  293. /* leave now if filled buffer already */
  294. if (buflen == 0)
  295. return acc;
  296. }
  297. list_for_each_entry(m, &vmcore_list, list) {
  298. if (*fpos < m->offset + m->size) {
  299. tsz = (size_t)min_t(unsigned long long,
  300. m->offset + m->size - *fpos,
  301. buflen);
  302. start = m->paddr + *fpos - m->offset;
  303. tmp = read_from_oldmem(buffer, tsz, &start, userbuf);
  304. if (tmp < 0)
  305. return tmp;
  306. buflen -= tsz;
  307. *fpos += tsz;
  308. buffer += tsz;
  309. acc += tsz;
  310. /* leave now if filled buffer already */
  311. if (buflen == 0)
  312. return acc;
  313. }
  314. }
  315. return acc;
  316. }
  317. static ssize_t read_vmcore(struct file *file, char __user *buffer,
  318. size_t buflen, loff_t *fpos)
  319. {
  320. return __read_vmcore((__force char *) buffer, buflen, fpos, 1);
  321. }
  322. /*
  323. * The vmcore fault handler uses the page cache and fills data using the
  324. * standard __vmcore_read() function.
  325. *
  326. * On s390 the fault handler is used for memory regions that can't be mapped
  327. * directly with remap_pfn_range().
  328. */
  329. static int mmap_vmcore_fault(struct vm_fault *vmf)
  330. {
  331. #ifdef CONFIG_S390
  332. struct address_space *mapping = vmf->vma->vm_file->f_mapping;
  333. pgoff_t index = vmf->pgoff;
  334. struct page *page;
  335. loff_t offset;
  336. char *buf;
  337. int rc;
  338. page = find_or_create_page(mapping, index, GFP_KERNEL);
  339. if (!page)
  340. return VM_FAULT_OOM;
  341. if (!PageUptodate(page)) {
  342. offset = (loff_t) index << PAGE_SHIFT;
  343. buf = __va((page_to_pfn(page) << PAGE_SHIFT));
  344. rc = __read_vmcore(buf, PAGE_SIZE, &offset, 0);
  345. if (rc < 0) {
  346. unlock_page(page);
  347. put_page(page);
  348. return (rc == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
  349. }
  350. SetPageUptodate(page);
  351. }
  352. unlock_page(page);
  353. vmf->page = page;
  354. return 0;
  355. #else
  356. return VM_FAULT_SIGBUS;
  357. #endif
  358. }
  359. static const struct vm_operations_struct vmcore_mmap_ops = {
  360. .fault = mmap_vmcore_fault,
  361. };
  362. /**
  363. * vmcore_alloc_buf - allocate buffer in vmalloc memory
  364. * @sizez: size of buffer
  365. *
  366. * If CONFIG_MMU is defined, use vmalloc_user() to allow users to mmap
  367. * the buffer to user-space by means of remap_vmalloc_range().
  368. *
  369. * If CONFIG_MMU is not defined, use vzalloc() since mmap_vmcore() is
  370. * disabled and there's no need to allow users to mmap the buffer.
  371. */
  372. static inline char *vmcore_alloc_buf(size_t size)
  373. {
  374. #ifdef CONFIG_MMU
  375. return vmalloc_user(size);
  376. #else
  377. return vzalloc(size);
  378. #endif
  379. }
  380. /*
  381. * Disable mmap_vmcore() if CONFIG_MMU is not defined. MMU is
  382. * essential for mmap_vmcore() in order to map physically
  383. * non-contiguous objects (ELF header, ELF note segment and memory
  384. * regions in the 1st kernel pointed to by PT_LOAD entries) into
  385. * virtually contiguous user-space in ELF layout.
  386. */
  387. #ifdef CONFIG_MMU
  388. /*
  389. * remap_oldmem_pfn_checked - do remap_oldmem_pfn_range replacing all pages
  390. * reported as not being ram with the zero page.
  391. *
  392. * @vma: vm_area_struct describing requested mapping
  393. * @from: start remapping from
  394. * @pfn: page frame number to start remapping to
  395. * @size: remapping size
  396. * @prot: protection bits
  397. *
  398. * Returns zero on success, -EAGAIN on failure.
  399. */
  400. static int remap_oldmem_pfn_checked(struct vm_area_struct *vma,
  401. unsigned long from, unsigned long pfn,
  402. unsigned long size, pgprot_t prot)
  403. {
  404. unsigned long map_size;
  405. unsigned long pos_start, pos_end, pos;
  406. unsigned long zeropage_pfn = my_zero_pfn(0);
  407. size_t len = 0;
  408. pos_start = pfn;
  409. pos_end = pfn + (size >> PAGE_SHIFT);
  410. for (pos = pos_start; pos < pos_end; ++pos) {
  411. if (!pfn_is_ram(pos)) {
  412. /*
  413. * We hit a page which is not ram. Remap the continuous
  414. * region between pos_start and pos-1 and replace
  415. * the non-ram page at pos with the zero page.
  416. */
  417. if (pos > pos_start) {
  418. /* Remap continuous region */
  419. map_size = (pos - pos_start) << PAGE_SHIFT;
  420. if (remap_oldmem_pfn_range(vma, from + len,
  421. pos_start, map_size,
  422. prot))
  423. goto fail;
  424. len += map_size;
  425. }
  426. /* Remap the zero page */
  427. if (remap_oldmem_pfn_range(vma, from + len,
  428. zeropage_pfn,
  429. PAGE_SIZE, prot))
  430. goto fail;
  431. len += PAGE_SIZE;
  432. pos_start = pos + 1;
  433. }
  434. }
  435. if (pos > pos_start) {
  436. /* Remap the rest */
  437. map_size = (pos - pos_start) << PAGE_SHIFT;
  438. if (remap_oldmem_pfn_range(vma, from + len, pos_start,
  439. map_size, prot))
  440. goto fail;
  441. }
  442. return 0;
  443. fail:
  444. do_munmap(vma->vm_mm, from, len, NULL);
  445. return -EAGAIN;
  446. }
  447. static int vmcore_remap_oldmem_pfn(struct vm_area_struct *vma,
  448. unsigned long from, unsigned long pfn,
  449. unsigned long size, pgprot_t prot)
  450. {
  451. /*
  452. * Check if oldmem_pfn_is_ram was registered to avoid
  453. * looping over all pages without a reason.
  454. */
  455. if (oldmem_pfn_is_ram)
  456. return remap_oldmem_pfn_checked(vma, from, pfn, size, prot);
  457. else
  458. return remap_oldmem_pfn_range(vma, from, pfn, size, prot);
  459. }
  460. static int mmap_vmcore(struct file *file, struct vm_area_struct *vma)
  461. {
  462. size_t size = vma->vm_end - vma->vm_start;
  463. u64 start, end, len, tsz;
  464. struct vmcore *m;
  465. start = (u64)vma->vm_pgoff << PAGE_SHIFT;
  466. end = start + size;
  467. if (size > vmcore_size || end > vmcore_size)
  468. return -EINVAL;
  469. if (vma->vm_flags & (VM_WRITE | VM_EXEC))
  470. return -EPERM;
  471. vma->vm_flags &= ~(VM_MAYWRITE | VM_MAYEXEC);
  472. vma->vm_flags |= VM_MIXEDMAP;
  473. vma->vm_ops = &vmcore_mmap_ops;
  474. len = 0;
  475. if (start < elfcorebuf_sz) {
  476. u64 pfn;
  477. tsz = min(elfcorebuf_sz - (size_t)start, size);
  478. pfn = __pa(elfcorebuf + start) >> PAGE_SHIFT;
  479. if (remap_pfn_range(vma, vma->vm_start, pfn, tsz,
  480. vma->vm_page_prot))
  481. return -EAGAIN;
  482. size -= tsz;
  483. start += tsz;
  484. len += tsz;
  485. if (size == 0)
  486. return 0;
  487. }
  488. if (start < elfcorebuf_sz + elfnotes_sz) {
  489. void *kaddr;
  490. /* We add device dumps before other elf notes because the
  491. * other elf notes may not fill the elf notes buffer
  492. * completely and we will end up with zero-filled data
  493. * between the elf notes and the device dumps. Tools will
  494. * then try to decode this zero-filled data as valid notes
  495. * and we don't want that. Hence, adding device dumps before
  496. * the other elf notes ensure that zero-filled data can be
  497. * avoided. This also ensures that the device dumps and
  498. * other elf notes can be properly mmaped at page aligned
  499. * address.
  500. */
  501. #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP
  502. /* Read device dumps */
  503. if (start < elfcorebuf_sz + vmcoredd_orig_sz) {
  504. u64 start_off;
  505. tsz = min(elfcorebuf_sz + vmcoredd_orig_sz -
  506. (size_t)start, size);
  507. start_off = start - elfcorebuf_sz;
  508. if (vmcoredd_mmap_dumps(vma, vma->vm_start + len,
  509. start_off, tsz))
  510. goto fail;
  511. size -= tsz;
  512. start += tsz;
  513. len += tsz;
  514. /* leave now if filled buffer already */
  515. if (!size)
  516. return 0;
  517. }
  518. #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */
  519. /* Read remaining elf notes */
  520. tsz = min(elfcorebuf_sz + elfnotes_sz - (size_t)start, size);
  521. kaddr = elfnotes_buf + start - elfcorebuf_sz - vmcoredd_orig_sz;
  522. if (remap_vmalloc_range_partial(vma, vma->vm_start + len,
  523. kaddr, tsz))
  524. goto fail;
  525. size -= tsz;
  526. start += tsz;
  527. len += tsz;
  528. if (size == 0)
  529. return 0;
  530. }
  531. list_for_each_entry(m, &vmcore_list, list) {
  532. if (start < m->offset + m->size) {
  533. u64 paddr = 0;
  534. tsz = (size_t)min_t(unsigned long long,
  535. m->offset + m->size - start, size);
  536. paddr = m->paddr + start - m->offset;
  537. if (vmcore_remap_oldmem_pfn(vma, vma->vm_start + len,
  538. paddr >> PAGE_SHIFT, tsz,
  539. vma->vm_page_prot))
  540. goto fail;
  541. size -= tsz;
  542. start += tsz;
  543. len += tsz;
  544. if (size == 0)
  545. return 0;
  546. }
  547. }
  548. return 0;
  549. fail:
  550. do_munmap(vma->vm_mm, vma->vm_start, len, NULL);
  551. return -EAGAIN;
  552. }
  553. #else
  554. static int mmap_vmcore(struct file *file, struct vm_area_struct *vma)
  555. {
  556. return -ENOSYS;
  557. }
  558. #endif
  559. static const struct file_operations proc_vmcore_operations = {
  560. .read = read_vmcore,
  561. .llseek = default_llseek,
  562. .mmap = mmap_vmcore,
  563. };
  564. static struct vmcore* __init get_new_element(void)
  565. {
  566. return kzalloc(sizeof(struct vmcore), GFP_KERNEL);
  567. }
  568. static u64 get_vmcore_size(size_t elfsz, size_t elfnotesegsz,
  569. struct list_head *vc_list)
  570. {
  571. u64 size;
  572. struct vmcore *m;
  573. size = elfsz + elfnotesegsz;
  574. list_for_each_entry(m, vc_list, list) {
  575. size += m->size;
  576. }
  577. return size;
  578. }
  579. /**
  580. * update_note_header_size_elf64 - update p_memsz member of each PT_NOTE entry
  581. *
  582. * @ehdr_ptr: ELF header
  583. *
  584. * This function updates p_memsz member of each PT_NOTE entry in the
  585. * program header table pointed to by @ehdr_ptr to real size of ELF
  586. * note segment.
  587. */
  588. static int __init update_note_header_size_elf64(const Elf64_Ehdr *ehdr_ptr)
  589. {
  590. int i, rc=0;
  591. Elf64_Phdr *phdr_ptr;
  592. Elf64_Nhdr *nhdr_ptr;
  593. phdr_ptr = (Elf64_Phdr *)(ehdr_ptr + 1);
  594. for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
  595. void *notes_section;
  596. u64 offset, max_sz, sz, real_sz = 0;
  597. if (phdr_ptr->p_type != PT_NOTE)
  598. continue;
  599. max_sz = phdr_ptr->p_memsz;
  600. offset = phdr_ptr->p_offset;
  601. notes_section = kmalloc(max_sz, GFP_KERNEL);
  602. if (!notes_section)
  603. return -ENOMEM;
  604. rc = elfcorehdr_read_notes(notes_section, max_sz, &offset);
  605. if (rc < 0) {
  606. kfree(notes_section);
  607. return rc;
  608. }
  609. nhdr_ptr = notes_section;
  610. while (nhdr_ptr->n_namesz != 0) {
  611. sz = sizeof(Elf64_Nhdr) +
  612. (((u64)nhdr_ptr->n_namesz + 3) & ~3) +
  613. (((u64)nhdr_ptr->n_descsz + 3) & ~3);
  614. if ((real_sz + sz) > max_sz) {
  615. pr_warn("Warning: Exceeded p_memsz, dropping PT_NOTE entry n_namesz=0x%x, n_descsz=0x%x\n",
  616. nhdr_ptr->n_namesz, nhdr_ptr->n_descsz);
  617. break;
  618. }
  619. real_sz += sz;
  620. nhdr_ptr = (Elf64_Nhdr*)((char*)nhdr_ptr + sz);
  621. }
  622. kfree(notes_section);
  623. phdr_ptr->p_memsz = real_sz;
  624. if (real_sz == 0) {
  625. pr_warn("Warning: Zero PT_NOTE entries found\n");
  626. }
  627. }
  628. return 0;
  629. }
  630. /**
  631. * get_note_number_and_size_elf64 - get the number of PT_NOTE program
  632. * headers and sum of real size of their ELF note segment headers and
  633. * data.
  634. *
  635. * @ehdr_ptr: ELF header
  636. * @nr_ptnote: buffer for the number of PT_NOTE program headers
  637. * @sz_ptnote: buffer for size of unique PT_NOTE program header
  638. *
  639. * This function is used to merge multiple PT_NOTE program headers
  640. * into a unique single one. The resulting unique entry will have
  641. * @sz_ptnote in its phdr->p_mem.
  642. *
  643. * It is assumed that program headers with PT_NOTE type pointed to by
  644. * @ehdr_ptr has already been updated by update_note_header_size_elf64
  645. * and each of PT_NOTE program headers has actual ELF note segment
  646. * size in its p_memsz member.
  647. */
  648. static int __init get_note_number_and_size_elf64(const Elf64_Ehdr *ehdr_ptr,
  649. int *nr_ptnote, u64 *sz_ptnote)
  650. {
  651. int i;
  652. Elf64_Phdr *phdr_ptr;
  653. *nr_ptnote = *sz_ptnote = 0;
  654. phdr_ptr = (Elf64_Phdr *)(ehdr_ptr + 1);
  655. for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
  656. if (phdr_ptr->p_type != PT_NOTE)
  657. continue;
  658. *nr_ptnote += 1;
  659. *sz_ptnote += phdr_ptr->p_memsz;
  660. }
  661. return 0;
  662. }
  663. /**
  664. * copy_notes_elf64 - copy ELF note segments in a given buffer
  665. *
  666. * @ehdr_ptr: ELF header
  667. * @notes_buf: buffer into which ELF note segments are copied
  668. *
  669. * This function is used to copy ELF note segment in the 1st kernel
  670. * into the buffer @notes_buf in the 2nd kernel. It is assumed that
  671. * size of the buffer @notes_buf is equal to or larger than sum of the
  672. * real ELF note segment headers and data.
  673. *
  674. * It is assumed that program headers with PT_NOTE type pointed to by
  675. * @ehdr_ptr has already been updated by update_note_header_size_elf64
  676. * and each of PT_NOTE program headers has actual ELF note segment
  677. * size in its p_memsz member.
  678. */
  679. static int __init copy_notes_elf64(const Elf64_Ehdr *ehdr_ptr, char *notes_buf)
  680. {
  681. int i, rc=0;
  682. Elf64_Phdr *phdr_ptr;
  683. phdr_ptr = (Elf64_Phdr*)(ehdr_ptr + 1);
  684. for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
  685. u64 offset;
  686. if (phdr_ptr->p_type != PT_NOTE)
  687. continue;
  688. offset = phdr_ptr->p_offset;
  689. rc = elfcorehdr_read_notes(notes_buf, phdr_ptr->p_memsz,
  690. &offset);
  691. if (rc < 0)
  692. return rc;
  693. notes_buf += phdr_ptr->p_memsz;
  694. }
  695. return 0;
  696. }
  697. /* Merges all the PT_NOTE headers into one. */
  698. static int __init merge_note_headers_elf64(char *elfptr, size_t *elfsz,
  699. char **notes_buf, size_t *notes_sz)
  700. {
  701. int i, nr_ptnote=0, rc=0;
  702. char *tmp;
  703. Elf64_Ehdr *ehdr_ptr;
  704. Elf64_Phdr phdr;
  705. u64 phdr_sz = 0, note_off;
  706. ehdr_ptr = (Elf64_Ehdr *)elfptr;
  707. rc = update_note_header_size_elf64(ehdr_ptr);
  708. if (rc < 0)
  709. return rc;
  710. rc = get_note_number_and_size_elf64(ehdr_ptr, &nr_ptnote, &phdr_sz);
  711. if (rc < 0)
  712. return rc;
  713. *notes_sz = roundup(phdr_sz, PAGE_SIZE);
  714. *notes_buf = vmcore_alloc_buf(*notes_sz);
  715. if (!*notes_buf)
  716. return -ENOMEM;
  717. rc = copy_notes_elf64(ehdr_ptr, *notes_buf);
  718. if (rc < 0)
  719. return rc;
  720. /* Prepare merged PT_NOTE program header. */
  721. phdr.p_type = PT_NOTE;
  722. phdr.p_flags = 0;
  723. note_off = sizeof(Elf64_Ehdr) +
  724. (ehdr_ptr->e_phnum - nr_ptnote +1) * sizeof(Elf64_Phdr);
  725. phdr.p_offset = roundup(note_off, PAGE_SIZE);
  726. phdr.p_vaddr = phdr.p_paddr = 0;
  727. phdr.p_filesz = phdr.p_memsz = phdr_sz;
  728. phdr.p_align = 0;
  729. /* Add merged PT_NOTE program header*/
  730. tmp = elfptr + sizeof(Elf64_Ehdr);
  731. memcpy(tmp, &phdr, sizeof(phdr));
  732. tmp += sizeof(phdr);
  733. /* Remove unwanted PT_NOTE program headers. */
  734. i = (nr_ptnote - 1) * sizeof(Elf64_Phdr);
  735. *elfsz = *elfsz - i;
  736. memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf64_Ehdr)-sizeof(Elf64_Phdr)));
  737. memset(elfptr + *elfsz, 0, i);
  738. *elfsz = roundup(*elfsz, PAGE_SIZE);
  739. /* Modify e_phnum to reflect merged headers. */
  740. ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1;
  741. /* Store the size of all notes. We need this to update the note
  742. * header when the device dumps will be added.
  743. */
  744. elfnotes_orig_sz = phdr.p_memsz;
  745. return 0;
  746. }
  747. /**
  748. * update_note_header_size_elf32 - update p_memsz member of each PT_NOTE entry
  749. *
  750. * @ehdr_ptr: ELF header
  751. *
  752. * This function updates p_memsz member of each PT_NOTE entry in the
  753. * program header table pointed to by @ehdr_ptr to real size of ELF
  754. * note segment.
  755. */
  756. static int __init update_note_header_size_elf32(const Elf32_Ehdr *ehdr_ptr)
  757. {
  758. int i, rc=0;
  759. Elf32_Phdr *phdr_ptr;
  760. Elf32_Nhdr *nhdr_ptr;
  761. phdr_ptr = (Elf32_Phdr *)(ehdr_ptr + 1);
  762. for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
  763. void *notes_section;
  764. u64 offset, max_sz, sz, real_sz = 0;
  765. if (phdr_ptr->p_type != PT_NOTE)
  766. continue;
  767. max_sz = phdr_ptr->p_memsz;
  768. offset = phdr_ptr->p_offset;
  769. notes_section = kmalloc(max_sz, GFP_KERNEL);
  770. if (!notes_section)
  771. return -ENOMEM;
  772. rc = elfcorehdr_read_notes(notes_section, max_sz, &offset);
  773. if (rc < 0) {
  774. kfree(notes_section);
  775. return rc;
  776. }
  777. nhdr_ptr = notes_section;
  778. while (nhdr_ptr->n_namesz != 0) {
  779. sz = sizeof(Elf32_Nhdr) +
  780. (((u64)nhdr_ptr->n_namesz + 3) & ~3) +
  781. (((u64)nhdr_ptr->n_descsz + 3) & ~3);
  782. if ((real_sz + sz) > max_sz) {
  783. pr_warn("Warning: Exceeded p_memsz, dropping PT_NOTE entry n_namesz=0x%x, n_descsz=0x%x\n",
  784. nhdr_ptr->n_namesz, nhdr_ptr->n_descsz);
  785. break;
  786. }
  787. real_sz += sz;
  788. nhdr_ptr = (Elf32_Nhdr*)((char*)nhdr_ptr + sz);
  789. }
  790. kfree(notes_section);
  791. phdr_ptr->p_memsz = real_sz;
  792. if (real_sz == 0) {
  793. pr_warn("Warning: Zero PT_NOTE entries found\n");
  794. }
  795. }
  796. return 0;
  797. }
  798. /**
  799. * get_note_number_and_size_elf32 - get the number of PT_NOTE program
  800. * headers and sum of real size of their ELF note segment headers and
  801. * data.
  802. *
  803. * @ehdr_ptr: ELF header
  804. * @nr_ptnote: buffer for the number of PT_NOTE program headers
  805. * @sz_ptnote: buffer for size of unique PT_NOTE program header
  806. *
  807. * This function is used to merge multiple PT_NOTE program headers
  808. * into a unique single one. The resulting unique entry will have
  809. * @sz_ptnote in its phdr->p_mem.
  810. *
  811. * It is assumed that program headers with PT_NOTE type pointed to by
  812. * @ehdr_ptr has already been updated by update_note_header_size_elf32
  813. * and each of PT_NOTE program headers has actual ELF note segment
  814. * size in its p_memsz member.
  815. */
  816. static int __init get_note_number_and_size_elf32(const Elf32_Ehdr *ehdr_ptr,
  817. int *nr_ptnote, u64 *sz_ptnote)
  818. {
  819. int i;
  820. Elf32_Phdr *phdr_ptr;
  821. *nr_ptnote = *sz_ptnote = 0;
  822. phdr_ptr = (Elf32_Phdr *)(ehdr_ptr + 1);
  823. for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
  824. if (phdr_ptr->p_type != PT_NOTE)
  825. continue;
  826. *nr_ptnote += 1;
  827. *sz_ptnote += phdr_ptr->p_memsz;
  828. }
  829. return 0;
  830. }
  831. /**
  832. * copy_notes_elf32 - copy ELF note segments in a given buffer
  833. *
  834. * @ehdr_ptr: ELF header
  835. * @notes_buf: buffer into which ELF note segments are copied
  836. *
  837. * This function is used to copy ELF note segment in the 1st kernel
  838. * into the buffer @notes_buf in the 2nd kernel. It is assumed that
  839. * size of the buffer @notes_buf is equal to or larger than sum of the
  840. * real ELF note segment headers and data.
  841. *
  842. * It is assumed that program headers with PT_NOTE type pointed to by
  843. * @ehdr_ptr has already been updated by update_note_header_size_elf32
  844. * and each of PT_NOTE program headers has actual ELF note segment
  845. * size in its p_memsz member.
  846. */
  847. static int __init copy_notes_elf32(const Elf32_Ehdr *ehdr_ptr, char *notes_buf)
  848. {
  849. int i, rc=0;
  850. Elf32_Phdr *phdr_ptr;
  851. phdr_ptr = (Elf32_Phdr*)(ehdr_ptr + 1);
  852. for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
  853. u64 offset;
  854. if (phdr_ptr->p_type != PT_NOTE)
  855. continue;
  856. offset = phdr_ptr->p_offset;
  857. rc = elfcorehdr_read_notes(notes_buf, phdr_ptr->p_memsz,
  858. &offset);
  859. if (rc < 0)
  860. return rc;
  861. notes_buf += phdr_ptr->p_memsz;
  862. }
  863. return 0;
  864. }
  865. /* Merges all the PT_NOTE headers into one. */
  866. static int __init merge_note_headers_elf32(char *elfptr, size_t *elfsz,
  867. char **notes_buf, size_t *notes_sz)
  868. {
  869. int i, nr_ptnote=0, rc=0;
  870. char *tmp;
  871. Elf32_Ehdr *ehdr_ptr;
  872. Elf32_Phdr phdr;
  873. u64 phdr_sz = 0, note_off;
  874. ehdr_ptr = (Elf32_Ehdr *)elfptr;
  875. rc = update_note_header_size_elf32(ehdr_ptr);
  876. if (rc < 0)
  877. return rc;
  878. rc = get_note_number_and_size_elf32(ehdr_ptr, &nr_ptnote, &phdr_sz);
  879. if (rc < 0)
  880. return rc;
  881. *notes_sz = roundup(phdr_sz, PAGE_SIZE);
  882. *notes_buf = vmcore_alloc_buf(*notes_sz);
  883. if (!*notes_buf)
  884. return -ENOMEM;
  885. rc = copy_notes_elf32(ehdr_ptr, *notes_buf);
  886. if (rc < 0)
  887. return rc;
  888. /* Prepare merged PT_NOTE program header. */
  889. phdr.p_type = PT_NOTE;
  890. phdr.p_flags = 0;
  891. note_off = sizeof(Elf32_Ehdr) +
  892. (ehdr_ptr->e_phnum - nr_ptnote +1) * sizeof(Elf32_Phdr);
  893. phdr.p_offset = roundup(note_off, PAGE_SIZE);
  894. phdr.p_vaddr = phdr.p_paddr = 0;
  895. phdr.p_filesz = phdr.p_memsz = phdr_sz;
  896. phdr.p_align = 0;
  897. /* Add merged PT_NOTE program header*/
  898. tmp = elfptr + sizeof(Elf32_Ehdr);
  899. memcpy(tmp, &phdr, sizeof(phdr));
  900. tmp += sizeof(phdr);
  901. /* Remove unwanted PT_NOTE program headers. */
  902. i = (nr_ptnote - 1) * sizeof(Elf32_Phdr);
  903. *elfsz = *elfsz - i;
  904. memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf32_Ehdr)-sizeof(Elf32_Phdr)));
  905. memset(elfptr + *elfsz, 0, i);
  906. *elfsz = roundup(*elfsz, PAGE_SIZE);
  907. /* Modify e_phnum to reflect merged headers. */
  908. ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1;
  909. /* Store the size of all notes. We need this to update the note
  910. * header when the device dumps will be added.
  911. */
  912. elfnotes_orig_sz = phdr.p_memsz;
  913. return 0;
  914. }
  915. /* Add memory chunks represented by program headers to vmcore list. Also update
  916. * the new offset fields of exported program headers. */
  917. static int __init process_ptload_program_headers_elf64(char *elfptr,
  918. size_t elfsz,
  919. size_t elfnotes_sz,
  920. struct list_head *vc_list)
  921. {
  922. int i;
  923. Elf64_Ehdr *ehdr_ptr;
  924. Elf64_Phdr *phdr_ptr;
  925. loff_t vmcore_off;
  926. struct vmcore *new;
  927. ehdr_ptr = (Elf64_Ehdr *)elfptr;
  928. phdr_ptr = (Elf64_Phdr*)(elfptr + sizeof(Elf64_Ehdr)); /* PT_NOTE hdr */
  929. /* Skip Elf header, program headers and Elf note segment. */
  930. vmcore_off = elfsz + elfnotes_sz;
  931. for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
  932. u64 paddr, start, end, size;
  933. if (phdr_ptr->p_type != PT_LOAD)
  934. continue;
  935. paddr = phdr_ptr->p_offset;
  936. start = rounddown(paddr, PAGE_SIZE);
  937. end = roundup(paddr + phdr_ptr->p_memsz, PAGE_SIZE);
  938. size = end - start;
  939. /* Add this contiguous chunk of memory to vmcore list.*/
  940. new = get_new_element();
  941. if (!new)
  942. return -ENOMEM;
  943. new->paddr = start;
  944. new->size = size;
  945. list_add_tail(&new->list, vc_list);
  946. /* Update the program header offset. */
  947. phdr_ptr->p_offset = vmcore_off + (paddr - start);
  948. vmcore_off = vmcore_off + size;
  949. }
  950. return 0;
  951. }
  952. static int __init process_ptload_program_headers_elf32(char *elfptr,
  953. size_t elfsz,
  954. size_t elfnotes_sz,
  955. struct list_head *vc_list)
  956. {
  957. int i;
  958. Elf32_Ehdr *ehdr_ptr;
  959. Elf32_Phdr *phdr_ptr;
  960. loff_t vmcore_off;
  961. struct vmcore *new;
  962. ehdr_ptr = (Elf32_Ehdr *)elfptr;
  963. phdr_ptr = (Elf32_Phdr*)(elfptr + sizeof(Elf32_Ehdr)); /* PT_NOTE hdr */
  964. /* Skip Elf header, program headers and Elf note segment. */
  965. vmcore_off = elfsz + elfnotes_sz;
  966. for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
  967. u64 paddr, start, end, size;
  968. if (phdr_ptr->p_type != PT_LOAD)
  969. continue;
  970. paddr = phdr_ptr->p_offset;
  971. start = rounddown(paddr, PAGE_SIZE);
  972. end = roundup(paddr + phdr_ptr->p_memsz, PAGE_SIZE);
  973. size = end - start;
  974. /* Add this contiguous chunk of memory to vmcore list.*/
  975. new = get_new_element();
  976. if (!new)
  977. return -ENOMEM;
  978. new->paddr = start;
  979. new->size = size;
  980. list_add_tail(&new->list, vc_list);
  981. /* Update the program header offset */
  982. phdr_ptr->p_offset = vmcore_off + (paddr - start);
  983. vmcore_off = vmcore_off + size;
  984. }
  985. return 0;
  986. }
  987. /* Sets offset fields of vmcore elements. */
  988. static void set_vmcore_list_offsets(size_t elfsz, size_t elfnotes_sz,
  989. struct list_head *vc_list)
  990. {
  991. loff_t vmcore_off;
  992. struct vmcore *m;
  993. /* Skip Elf header, program headers and Elf note segment. */
  994. vmcore_off = elfsz + elfnotes_sz;
  995. list_for_each_entry(m, vc_list, list) {
  996. m->offset = vmcore_off;
  997. vmcore_off += m->size;
  998. }
  999. }
  1000. static void free_elfcorebuf(void)
  1001. {
  1002. free_pages((unsigned long)elfcorebuf, get_order(elfcorebuf_sz_orig));
  1003. elfcorebuf = NULL;
  1004. vfree(elfnotes_buf);
  1005. elfnotes_buf = NULL;
  1006. }
  1007. static int __init parse_crash_elf64_headers(void)
  1008. {
  1009. int rc=0;
  1010. Elf64_Ehdr ehdr;
  1011. u64 addr;
  1012. addr = elfcorehdr_addr;
  1013. /* Read Elf header */
  1014. rc = elfcorehdr_read((char *)&ehdr, sizeof(Elf64_Ehdr), &addr);
  1015. if (rc < 0)
  1016. return rc;
  1017. /* Do some basic Verification. */
  1018. if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
  1019. (ehdr.e_type != ET_CORE) ||
  1020. !vmcore_elf64_check_arch(&ehdr) ||
  1021. ehdr.e_ident[EI_CLASS] != ELFCLASS64 ||
  1022. ehdr.e_ident[EI_VERSION] != EV_CURRENT ||
  1023. ehdr.e_version != EV_CURRENT ||
  1024. ehdr.e_ehsize != sizeof(Elf64_Ehdr) ||
  1025. ehdr.e_phentsize != sizeof(Elf64_Phdr) ||
  1026. ehdr.e_phnum == 0) {
  1027. pr_warn("Warning: Core image elf header is not sane\n");
  1028. return -EINVAL;
  1029. }
  1030. /* Read in all elf headers. */
  1031. elfcorebuf_sz_orig = sizeof(Elf64_Ehdr) +
  1032. ehdr.e_phnum * sizeof(Elf64_Phdr);
  1033. elfcorebuf_sz = elfcorebuf_sz_orig;
  1034. elfcorebuf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  1035. get_order(elfcorebuf_sz_orig));
  1036. if (!elfcorebuf)
  1037. return -ENOMEM;
  1038. addr = elfcorehdr_addr;
  1039. rc = elfcorehdr_read(elfcorebuf, elfcorebuf_sz_orig, &addr);
  1040. if (rc < 0)
  1041. goto fail;
  1042. /* Merge all PT_NOTE headers into one. */
  1043. rc = merge_note_headers_elf64(elfcorebuf, &elfcorebuf_sz,
  1044. &elfnotes_buf, &elfnotes_sz);
  1045. if (rc)
  1046. goto fail;
  1047. rc = process_ptload_program_headers_elf64(elfcorebuf, elfcorebuf_sz,
  1048. elfnotes_sz, &vmcore_list);
  1049. if (rc)
  1050. goto fail;
  1051. set_vmcore_list_offsets(elfcorebuf_sz, elfnotes_sz, &vmcore_list);
  1052. return 0;
  1053. fail:
  1054. free_elfcorebuf();
  1055. return rc;
  1056. }
  1057. static int __init parse_crash_elf32_headers(void)
  1058. {
  1059. int rc=0;
  1060. Elf32_Ehdr ehdr;
  1061. u64 addr;
  1062. addr = elfcorehdr_addr;
  1063. /* Read Elf header */
  1064. rc = elfcorehdr_read((char *)&ehdr, sizeof(Elf32_Ehdr), &addr);
  1065. if (rc < 0)
  1066. return rc;
  1067. /* Do some basic Verification. */
  1068. if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
  1069. (ehdr.e_type != ET_CORE) ||
  1070. !vmcore_elf32_check_arch(&ehdr) ||
  1071. ehdr.e_ident[EI_CLASS] != ELFCLASS32||
  1072. ehdr.e_ident[EI_VERSION] != EV_CURRENT ||
  1073. ehdr.e_version != EV_CURRENT ||
  1074. ehdr.e_ehsize != sizeof(Elf32_Ehdr) ||
  1075. ehdr.e_phentsize != sizeof(Elf32_Phdr) ||
  1076. ehdr.e_phnum == 0) {
  1077. pr_warn("Warning: Core image elf header is not sane\n");
  1078. return -EINVAL;
  1079. }
  1080. /* Read in all elf headers. */
  1081. elfcorebuf_sz_orig = sizeof(Elf32_Ehdr) + ehdr.e_phnum * sizeof(Elf32_Phdr);
  1082. elfcorebuf_sz = elfcorebuf_sz_orig;
  1083. elfcorebuf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  1084. get_order(elfcorebuf_sz_orig));
  1085. if (!elfcorebuf)
  1086. return -ENOMEM;
  1087. addr = elfcorehdr_addr;
  1088. rc = elfcorehdr_read(elfcorebuf, elfcorebuf_sz_orig, &addr);
  1089. if (rc < 0)
  1090. goto fail;
  1091. /* Merge all PT_NOTE headers into one. */
  1092. rc = merge_note_headers_elf32(elfcorebuf, &elfcorebuf_sz,
  1093. &elfnotes_buf, &elfnotes_sz);
  1094. if (rc)
  1095. goto fail;
  1096. rc = process_ptload_program_headers_elf32(elfcorebuf, elfcorebuf_sz,
  1097. elfnotes_sz, &vmcore_list);
  1098. if (rc)
  1099. goto fail;
  1100. set_vmcore_list_offsets(elfcorebuf_sz, elfnotes_sz, &vmcore_list);
  1101. return 0;
  1102. fail:
  1103. free_elfcorebuf();
  1104. return rc;
  1105. }
  1106. static int __init parse_crash_elf_headers(void)
  1107. {
  1108. unsigned char e_ident[EI_NIDENT];
  1109. u64 addr;
  1110. int rc=0;
  1111. addr = elfcorehdr_addr;
  1112. rc = elfcorehdr_read(e_ident, EI_NIDENT, &addr);
  1113. if (rc < 0)
  1114. return rc;
  1115. if (memcmp(e_ident, ELFMAG, SELFMAG) != 0) {
  1116. pr_warn("Warning: Core image elf header not found\n");
  1117. return -EINVAL;
  1118. }
  1119. if (e_ident[EI_CLASS] == ELFCLASS64) {
  1120. rc = parse_crash_elf64_headers();
  1121. if (rc)
  1122. return rc;
  1123. } else if (e_ident[EI_CLASS] == ELFCLASS32) {
  1124. rc = parse_crash_elf32_headers();
  1125. if (rc)
  1126. return rc;
  1127. } else {
  1128. pr_warn("Warning: Core image elf header is not sane\n");
  1129. return -EINVAL;
  1130. }
  1131. /* Determine vmcore size. */
  1132. vmcore_size = get_vmcore_size(elfcorebuf_sz, elfnotes_sz,
  1133. &vmcore_list);
  1134. return 0;
  1135. }
  1136. #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP
  1137. /**
  1138. * vmcoredd_write_header - Write vmcore device dump header at the
  1139. * beginning of the dump's buffer.
  1140. * @buf: Output buffer where the note is written
  1141. * @data: Dump info
  1142. * @size: Size of the dump
  1143. *
  1144. * Fills beginning of the dump's buffer with vmcore device dump header.
  1145. */
  1146. static void vmcoredd_write_header(void *buf, struct vmcoredd_data *data,
  1147. u32 size)
  1148. {
  1149. struct vmcoredd_header *vdd_hdr = (struct vmcoredd_header *)buf;
  1150. vdd_hdr->n_namesz = sizeof(vdd_hdr->name);
  1151. vdd_hdr->n_descsz = size + sizeof(vdd_hdr->dump_name);
  1152. vdd_hdr->n_type = NT_VMCOREDD;
  1153. strncpy((char *)vdd_hdr->name, VMCOREDD_NOTE_NAME,
  1154. sizeof(vdd_hdr->name));
  1155. memcpy(vdd_hdr->dump_name, data->dump_name, sizeof(vdd_hdr->dump_name));
  1156. }
  1157. /**
  1158. * vmcoredd_update_program_headers - Update all Elf program headers
  1159. * @elfptr: Pointer to elf header
  1160. * @elfnotesz: Size of elf notes aligned to page size
  1161. * @vmcoreddsz: Size of device dumps to be added to elf note header
  1162. *
  1163. * Determine type of Elf header (Elf64 or Elf32) and update the elf note size.
  1164. * Also update the offsets of all the program headers after the elf note header.
  1165. */
  1166. static void vmcoredd_update_program_headers(char *elfptr, size_t elfnotesz,
  1167. size_t vmcoreddsz)
  1168. {
  1169. unsigned char *e_ident = (unsigned char *)elfptr;
  1170. u64 start, end, size;
  1171. loff_t vmcore_off;
  1172. u32 i;
  1173. vmcore_off = elfcorebuf_sz + elfnotesz;
  1174. if (e_ident[EI_CLASS] == ELFCLASS64) {
  1175. Elf64_Ehdr *ehdr = (Elf64_Ehdr *)elfptr;
  1176. Elf64_Phdr *phdr = (Elf64_Phdr *)(elfptr + sizeof(Elf64_Ehdr));
  1177. /* Update all program headers */
  1178. for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
  1179. if (phdr->p_type == PT_NOTE) {
  1180. /* Update note size */
  1181. phdr->p_memsz = elfnotes_orig_sz + vmcoreddsz;
  1182. phdr->p_filesz = phdr->p_memsz;
  1183. continue;
  1184. }
  1185. start = rounddown(phdr->p_offset, PAGE_SIZE);
  1186. end = roundup(phdr->p_offset + phdr->p_memsz,
  1187. PAGE_SIZE);
  1188. size = end - start;
  1189. phdr->p_offset = vmcore_off + (phdr->p_offset - start);
  1190. vmcore_off += size;
  1191. }
  1192. } else {
  1193. Elf32_Ehdr *ehdr = (Elf32_Ehdr *)elfptr;
  1194. Elf32_Phdr *phdr = (Elf32_Phdr *)(elfptr + sizeof(Elf32_Ehdr));
  1195. /* Update all program headers */
  1196. for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
  1197. if (phdr->p_type == PT_NOTE) {
  1198. /* Update note size */
  1199. phdr->p_memsz = elfnotes_orig_sz + vmcoreddsz;
  1200. phdr->p_filesz = phdr->p_memsz;
  1201. continue;
  1202. }
  1203. start = rounddown(phdr->p_offset, PAGE_SIZE);
  1204. end = roundup(phdr->p_offset + phdr->p_memsz,
  1205. PAGE_SIZE);
  1206. size = end - start;
  1207. phdr->p_offset = vmcore_off + (phdr->p_offset - start);
  1208. vmcore_off += size;
  1209. }
  1210. }
  1211. }
  1212. /**
  1213. * vmcoredd_update_size - Update the total size of the device dumps and update
  1214. * Elf header
  1215. * @dump_size: Size of the current device dump to be added to total size
  1216. *
  1217. * Update the total size of all the device dumps and update the Elf program
  1218. * headers. Calculate the new offsets for the vmcore list and update the
  1219. * total vmcore size.
  1220. */
  1221. static void vmcoredd_update_size(size_t dump_size)
  1222. {
  1223. vmcoredd_orig_sz += dump_size;
  1224. elfnotes_sz = roundup(elfnotes_orig_sz, PAGE_SIZE) + vmcoredd_orig_sz;
  1225. vmcoredd_update_program_headers(elfcorebuf, elfnotes_sz,
  1226. vmcoredd_orig_sz);
  1227. /* Update vmcore list offsets */
  1228. set_vmcore_list_offsets(elfcorebuf_sz, elfnotes_sz, &vmcore_list);
  1229. vmcore_size = get_vmcore_size(elfcorebuf_sz, elfnotes_sz,
  1230. &vmcore_list);
  1231. proc_vmcore->size = vmcore_size;
  1232. }
  1233. /**
  1234. * vmcore_add_device_dump - Add a buffer containing device dump to vmcore
  1235. * @data: dump info.
  1236. *
  1237. * Allocate a buffer and invoke the calling driver's dump collect routine.
  1238. * Write Elf note at the beginning of the buffer to indicate vmcore device
  1239. * dump and add the dump to global list.
  1240. */
  1241. int vmcore_add_device_dump(struct vmcoredd_data *data)
  1242. {
  1243. struct vmcoredd_node *dump;
  1244. void *buf = NULL;
  1245. size_t data_size;
  1246. int ret;
  1247. if (!data || !strlen(data->dump_name) ||
  1248. !data->vmcoredd_callback || !data->size)
  1249. return -EINVAL;
  1250. dump = vzalloc(sizeof(*dump));
  1251. if (!dump) {
  1252. ret = -ENOMEM;
  1253. goto out_err;
  1254. }
  1255. /* Keep size of the buffer page aligned so that it can be mmaped */
  1256. data_size = roundup(sizeof(struct vmcoredd_header) + data->size,
  1257. PAGE_SIZE);
  1258. /* Allocate buffer for driver's to write their dumps */
  1259. buf = vmcore_alloc_buf(data_size);
  1260. if (!buf) {
  1261. ret = -ENOMEM;
  1262. goto out_err;
  1263. }
  1264. vmcoredd_write_header(buf, data, data_size -
  1265. sizeof(struct vmcoredd_header));
  1266. /* Invoke the driver's dump collection routing */
  1267. ret = data->vmcoredd_callback(data, buf +
  1268. sizeof(struct vmcoredd_header));
  1269. if (ret)
  1270. goto out_err;
  1271. dump->buf = buf;
  1272. dump->size = data_size;
  1273. /* Add the dump to driver sysfs list */
  1274. mutex_lock(&vmcoredd_mutex);
  1275. list_add_tail(&dump->list, &vmcoredd_list);
  1276. mutex_unlock(&vmcoredd_mutex);
  1277. vmcoredd_update_size(data_size);
  1278. return 0;
  1279. out_err:
  1280. if (buf)
  1281. vfree(buf);
  1282. if (dump)
  1283. vfree(dump);
  1284. return ret;
  1285. }
  1286. EXPORT_SYMBOL(vmcore_add_device_dump);
  1287. #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */
  1288. /* Free all dumps in vmcore device dump list */
  1289. static void vmcore_free_device_dumps(void)
  1290. {
  1291. #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP
  1292. mutex_lock(&vmcoredd_mutex);
  1293. while (!list_empty(&vmcoredd_list)) {
  1294. struct vmcoredd_node *dump;
  1295. dump = list_first_entry(&vmcoredd_list, struct vmcoredd_node,
  1296. list);
  1297. list_del(&dump->list);
  1298. vfree(dump->buf);
  1299. vfree(dump);
  1300. }
  1301. mutex_unlock(&vmcoredd_mutex);
  1302. #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */
  1303. }
  1304. /* Init function for vmcore module. */
  1305. static int __init vmcore_init(void)
  1306. {
  1307. int rc = 0;
  1308. /* Allow architectures to allocate ELF header in 2nd kernel */
  1309. rc = elfcorehdr_alloc(&elfcorehdr_addr, &elfcorehdr_size);
  1310. if (rc)
  1311. return rc;
  1312. /*
  1313. * If elfcorehdr= has been passed in cmdline or created in 2nd kernel,
  1314. * then capture the dump.
  1315. */
  1316. if (!(is_vmcore_usable()))
  1317. return rc;
  1318. rc = parse_crash_elf_headers();
  1319. if (rc) {
  1320. pr_warn("Kdump: vmcore not initialized\n");
  1321. return rc;
  1322. }
  1323. elfcorehdr_free(elfcorehdr_addr);
  1324. elfcorehdr_addr = ELFCORE_ADDR_ERR;
  1325. proc_vmcore = proc_create("vmcore", S_IRUSR, NULL, &proc_vmcore_operations);
  1326. if (proc_vmcore)
  1327. proc_vmcore->size = vmcore_size;
  1328. return 0;
  1329. }
  1330. fs_initcall(vmcore_init);
  1331. /* Cleanup function for vmcore module. */
  1332. void vmcore_cleanup(void)
  1333. {
  1334. if (proc_vmcore) {
  1335. proc_remove(proc_vmcore);
  1336. proc_vmcore = NULL;
  1337. }
  1338. /* clear the vmcore list. */
  1339. while (!list_empty(&vmcore_list)) {
  1340. struct vmcore *m;
  1341. m = list_first_entry(&vmcore_list, struct vmcore, list);
  1342. list_del(&m->list);
  1343. kfree(m);
  1344. }
  1345. free_elfcorebuf();
  1346. /* clear vmcore device dump list */
  1347. vmcore_free_device_dumps();
  1348. }