vmcore.c 40 KB

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