vmcore.c 45 KB

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