vmcore.c 41 KB

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