vmcore.c 40 KB

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