kexec_file.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * kexec: kexec_file_load system call
  4. *
  5. * Copyright (C) 2014 Red Hat Inc.
  6. * Authors:
  7. * Vivek Goyal <[email protected]>
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/capability.h>
  11. #include <linux/mm.h>
  12. #include <linux/file.h>
  13. #include <linux/slab.h>
  14. #include <linux/kexec.h>
  15. #include <linux/memblock.h>
  16. #include <linux/mutex.h>
  17. #include <linux/list.h>
  18. #include <linux/fs.h>
  19. #include <linux/ima.h>
  20. #include <crypto/hash.h>
  21. #include <crypto/sha2.h>
  22. #include <linux/elf.h>
  23. #include <linux/elfcore.h>
  24. #include <linux/kernel.h>
  25. #include <linux/kernel_read_file.h>
  26. #include <linux/syscalls.h>
  27. #include <linux/vmalloc.h>
  28. #include "kexec_internal.h"
  29. #ifdef CONFIG_KEXEC_SIG
  30. static bool sig_enforce = IS_ENABLED(CONFIG_KEXEC_SIG_FORCE);
  31. void set_kexec_sig_enforced(void)
  32. {
  33. sig_enforce = true;
  34. }
  35. #endif
  36. static int kexec_calculate_store_digests(struct kimage *image);
  37. /* Maximum size in bytes for kernel/initrd files. */
  38. #define KEXEC_FILE_SIZE_MAX min_t(s64, 4LL << 30, SSIZE_MAX)
  39. /*
  40. * Currently this is the only default function that is exported as some
  41. * architectures need it to do additional handlings.
  42. * In the future, other default functions may be exported too if required.
  43. */
  44. int kexec_image_probe_default(struct kimage *image, void *buf,
  45. unsigned long buf_len)
  46. {
  47. const struct kexec_file_ops * const *fops;
  48. int ret = -ENOEXEC;
  49. for (fops = &kexec_file_loaders[0]; *fops && (*fops)->probe; ++fops) {
  50. ret = (*fops)->probe(buf, buf_len);
  51. if (!ret) {
  52. image->fops = *fops;
  53. return ret;
  54. }
  55. }
  56. return ret;
  57. }
  58. void *kexec_image_load_default(struct kimage *image)
  59. {
  60. if (!image->fops || !image->fops->load)
  61. return ERR_PTR(-ENOEXEC);
  62. return image->fops->load(image, image->kernel_buf,
  63. image->kernel_buf_len, image->initrd_buf,
  64. image->initrd_buf_len, image->cmdline_buf,
  65. image->cmdline_buf_len);
  66. }
  67. int kexec_image_post_load_cleanup_default(struct kimage *image)
  68. {
  69. if (!image->fops || !image->fops->cleanup)
  70. return 0;
  71. return image->fops->cleanup(image->image_loader_data);
  72. }
  73. /*
  74. * Free up memory used by kernel, initrd, and command line. This is temporary
  75. * memory allocation which is not needed any more after these buffers have
  76. * been loaded into separate segments and have been copied elsewhere.
  77. */
  78. void kimage_file_post_load_cleanup(struct kimage *image)
  79. {
  80. struct purgatory_info *pi = &image->purgatory_info;
  81. vfree(image->kernel_buf);
  82. image->kernel_buf = NULL;
  83. vfree(image->initrd_buf);
  84. image->initrd_buf = NULL;
  85. kfree(image->cmdline_buf);
  86. image->cmdline_buf = NULL;
  87. vfree(pi->purgatory_buf);
  88. pi->purgatory_buf = NULL;
  89. vfree(pi->sechdrs);
  90. pi->sechdrs = NULL;
  91. #ifdef CONFIG_IMA_KEXEC
  92. vfree(image->ima_buffer);
  93. image->ima_buffer = NULL;
  94. #endif /* CONFIG_IMA_KEXEC */
  95. /* See if architecture has anything to cleanup post load */
  96. arch_kimage_file_post_load_cleanup(image);
  97. /*
  98. * Above call should have called into bootloader to free up
  99. * any data stored in kimage->image_loader_data. It should
  100. * be ok now to free it up.
  101. */
  102. kfree(image->image_loader_data);
  103. image->image_loader_data = NULL;
  104. }
  105. #ifdef CONFIG_KEXEC_SIG
  106. #ifdef CONFIG_SIGNED_PE_FILE_VERIFICATION
  107. int kexec_kernel_verify_pe_sig(const char *kernel, unsigned long kernel_len)
  108. {
  109. int ret;
  110. ret = verify_pefile_signature(kernel, kernel_len,
  111. VERIFY_USE_SECONDARY_KEYRING,
  112. VERIFYING_KEXEC_PE_SIGNATURE);
  113. if (ret == -ENOKEY && IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING)) {
  114. ret = verify_pefile_signature(kernel, kernel_len,
  115. VERIFY_USE_PLATFORM_KEYRING,
  116. VERIFYING_KEXEC_PE_SIGNATURE);
  117. }
  118. return ret;
  119. }
  120. #endif
  121. static int kexec_image_verify_sig(struct kimage *image, void *buf,
  122. unsigned long buf_len)
  123. {
  124. if (!image->fops || !image->fops->verify_sig) {
  125. pr_debug("kernel loader does not support signature verification.\n");
  126. return -EKEYREJECTED;
  127. }
  128. return image->fops->verify_sig(buf, buf_len);
  129. }
  130. static int
  131. kimage_validate_signature(struct kimage *image)
  132. {
  133. int ret;
  134. ret = kexec_image_verify_sig(image, image->kernel_buf,
  135. image->kernel_buf_len);
  136. if (ret) {
  137. if (sig_enforce) {
  138. pr_notice("Enforced kernel signature verification failed (%d).\n", ret);
  139. return ret;
  140. }
  141. /*
  142. * If IMA is guaranteed to appraise a signature on the kexec
  143. * image, permit it even if the kernel is otherwise locked
  144. * down.
  145. */
  146. if (!ima_appraise_signature(READING_KEXEC_IMAGE) &&
  147. security_locked_down(LOCKDOWN_KEXEC))
  148. return -EPERM;
  149. pr_debug("kernel signature verification failed (%d).\n", ret);
  150. }
  151. return 0;
  152. }
  153. #endif
  154. /*
  155. * In file mode list of segments is prepared by kernel. Copy relevant
  156. * data from user space, do error checking, prepare segment list
  157. */
  158. static int
  159. kimage_file_prepare_segments(struct kimage *image, int kernel_fd, int initrd_fd,
  160. const char __user *cmdline_ptr,
  161. unsigned long cmdline_len, unsigned flags)
  162. {
  163. ssize_t ret;
  164. void *ldata;
  165. ret = kernel_read_file_from_fd(kernel_fd, 0, &image->kernel_buf,
  166. KEXEC_FILE_SIZE_MAX, NULL,
  167. READING_KEXEC_IMAGE);
  168. if (ret < 0)
  169. return ret;
  170. image->kernel_buf_len = ret;
  171. /* Call arch image probe handlers */
  172. ret = arch_kexec_kernel_image_probe(image, image->kernel_buf,
  173. image->kernel_buf_len);
  174. if (ret)
  175. goto out;
  176. #ifdef CONFIG_KEXEC_SIG
  177. ret = kimage_validate_signature(image);
  178. if (ret)
  179. goto out;
  180. #endif
  181. /* It is possible that there no initramfs is being loaded */
  182. if (!(flags & KEXEC_FILE_NO_INITRAMFS)) {
  183. ret = kernel_read_file_from_fd(initrd_fd, 0, &image->initrd_buf,
  184. KEXEC_FILE_SIZE_MAX, NULL,
  185. READING_KEXEC_INITRAMFS);
  186. if (ret < 0)
  187. goto out;
  188. image->initrd_buf_len = ret;
  189. ret = 0;
  190. }
  191. if (cmdline_len) {
  192. image->cmdline_buf = memdup_user(cmdline_ptr, cmdline_len);
  193. if (IS_ERR(image->cmdline_buf)) {
  194. ret = PTR_ERR(image->cmdline_buf);
  195. image->cmdline_buf = NULL;
  196. goto out;
  197. }
  198. image->cmdline_buf_len = cmdline_len;
  199. /* command line should be a string with last byte null */
  200. if (image->cmdline_buf[cmdline_len - 1] != '\0') {
  201. ret = -EINVAL;
  202. goto out;
  203. }
  204. ima_kexec_cmdline(kernel_fd, image->cmdline_buf,
  205. image->cmdline_buf_len - 1);
  206. }
  207. /* IMA needs to pass the measurement list to the next kernel. */
  208. ima_add_kexec_buffer(image);
  209. /* Call arch image load handlers */
  210. ldata = arch_kexec_kernel_image_load(image);
  211. if (IS_ERR(ldata)) {
  212. ret = PTR_ERR(ldata);
  213. goto out;
  214. }
  215. image->image_loader_data = ldata;
  216. out:
  217. /* In case of error, free up all allocated memory in this function */
  218. if (ret)
  219. kimage_file_post_load_cleanup(image);
  220. return ret;
  221. }
  222. static int
  223. kimage_file_alloc_init(struct kimage **rimage, int kernel_fd,
  224. int initrd_fd, const char __user *cmdline_ptr,
  225. unsigned long cmdline_len, unsigned long flags)
  226. {
  227. int ret;
  228. struct kimage *image;
  229. bool kexec_on_panic = flags & KEXEC_FILE_ON_CRASH;
  230. image = do_kimage_alloc_init();
  231. if (!image)
  232. return -ENOMEM;
  233. image->file_mode = 1;
  234. if (kexec_on_panic) {
  235. /* Enable special crash kernel control page alloc policy. */
  236. image->control_page = crashk_res.start;
  237. image->type = KEXEC_TYPE_CRASH;
  238. }
  239. ret = kimage_file_prepare_segments(image, kernel_fd, initrd_fd,
  240. cmdline_ptr, cmdline_len, flags);
  241. if (ret)
  242. goto out_free_image;
  243. ret = sanity_check_segment_list(image);
  244. if (ret)
  245. goto out_free_post_load_bufs;
  246. ret = -ENOMEM;
  247. image->control_code_page = kimage_alloc_control_pages(image,
  248. get_order(KEXEC_CONTROL_PAGE_SIZE));
  249. if (!image->control_code_page) {
  250. pr_err("Could not allocate control_code_buffer\n");
  251. goto out_free_post_load_bufs;
  252. }
  253. if (!kexec_on_panic) {
  254. image->swap_page = kimage_alloc_control_pages(image, 0);
  255. if (!image->swap_page) {
  256. pr_err("Could not allocate swap buffer\n");
  257. goto out_free_control_pages;
  258. }
  259. }
  260. *rimage = image;
  261. return 0;
  262. out_free_control_pages:
  263. kimage_free_page_list(&image->control_pages);
  264. out_free_post_load_bufs:
  265. kimage_file_post_load_cleanup(image);
  266. out_free_image:
  267. kfree(image);
  268. return ret;
  269. }
  270. SYSCALL_DEFINE5(kexec_file_load, int, kernel_fd, int, initrd_fd,
  271. unsigned long, cmdline_len, const char __user *, cmdline_ptr,
  272. unsigned long, flags)
  273. {
  274. int ret = 0, i;
  275. struct kimage **dest_image, *image;
  276. /* We only trust the superuser with rebooting the system. */
  277. if (!capable(CAP_SYS_BOOT) || kexec_load_disabled)
  278. return -EPERM;
  279. /* Make sure we have a legal set of flags */
  280. if (flags != (flags & KEXEC_FILE_FLAGS))
  281. return -EINVAL;
  282. image = NULL;
  283. if (!kexec_trylock())
  284. return -EBUSY;
  285. dest_image = &kexec_image;
  286. if (flags & KEXEC_FILE_ON_CRASH) {
  287. dest_image = &kexec_crash_image;
  288. if (kexec_crash_image)
  289. arch_kexec_unprotect_crashkres();
  290. }
  291. if (flags & KEXEC_FILE_UNLOAD)
  292. goto exchange;
  293. /*
  294. * In case of crash, new kernel gets loaded in reserved region. It is
  295. * same memory where old crash kernel might be loaded. Free any
  296. * current crash dump kernel before we corrupt it.
  297. */
  298. if (flags & KEXEC_FILE_ON_CRASH)
  299. kimage_free(xchg(&kexec_crash_image, NULL));
  300. ret = kimage_file_alloc_init(&image, kernel_fd, initrd_fd, cmdline_ptr,
  301. cmdline_len, flags);
  302. if (ret)
  303. goto out;
  304. ret = machine_kexec_prepare(image);
  305. if (ret)
  306. goto out;
  307. /*
  308. * Some architecture(like S390) may touch the crash memory before
  309. * machine_kexec_prepare(), we must copy vmcoreinfo data after it.
  310. */
  311. ret = kimage_crash_copy_vmcoreinfo(image);
  312. if (ret)
  313. goto out;
  314. ret = kexec_calculate_store_digests(image);
  315. if (ret)
  316. goto out;
  317. for (i = 0; i < image->nr_segments; i++) {
  318. struct kexec_segment *ksegment;
  319. ksegment = &image->segment[i];
  320. pr_debug("Loading segment %d: buf=0x%p bufsz=0x%zx mem=0x%lx memsz=0x%zx\n",
  321. i, ksegment->buf, ksegment->bufsz, ksegment->mem,
  322. ksegment->memsz);
  323. ret = kimage_load_segment(image, &image->segment[i]);
  324. if (ret)
  325. goto out;
  326. }
  327. kimage_terminate(image);
  328. ret = machine_kexec_post_load(image);
  329. if (ret)
  330. goto out;
  331. /*
  332. * Free up any temporary buffers allocated which are not needed
  333. * after image has been loaded
  334. */
  335. kimage_file_post_load_cleanup(image);
  336. exchange:
  337. image = xchg(dest_image, image);
  338. out:
  339. if ((flags & KEXEC_FILE_ON_CRASH) && kexec_crash_image)
  340. arch_kexec_protect_crashkres();
  341. kexec_unlock();
  342. kimage_free(image);
  343. return ret;
  344. }
  345. static int locate_mem_hole_top_down(unsigned long start, unsigned long end,
  346. struct kexec_buf *kbuf)
  347. {
  348. struct kimage *image = kbuf->image;
  349. unsigned long temp_start, temp_end;
  350. temp_end = min(end, kbuf->buf_max);
  351. temp_start = temp_end - kbuf->memsz;
  352. do {
  353. /* align down start */
  354. temp_start = temp_start & (~(kbuf->buf_align - 1));
  355. if (temp_start < start || temp_start < kbuf->buf_min)
  356. return 0;
  357. temp_end = temp_start + kbuf->memsz - 1;
  358. /*
  359. * Make sure this does not conflict with any of existing
  360. * segments
  361. */
  362. if (kimage_is_destination_range(image, temp_start, temp_end)) {
  363. temp_start = temp_start - PAGE_SIZE;
  364. continue;
  365. }
  366. /* We found a suitable memory range */
  367. break;
  368. } while (1);
  369. /* If we are here, we found a suitable memory range */
  370. kbuf->mem = temp_start;
  371. /* Success, stop navigating through remaining System RAM ranges */
  372. return 1;
  373. }
  374. static int locate_mem_hole_bottom_up(unsigned long start, unsigned long end,
  375. struct kexec_buf *kbuf)
  376. {
  377. struct kimage *image = kbuf->image;
  378. unsigned long temp_start, temp_end;
  379. temp_start = max(start, kbuf->buf_min);
  380. do {
  381. temp_start = ALIGN(temp_start, kbuf->buf_align);
  382. temp_end = temp_start + kbuf->memsz - 1;
  383. if (temp_end > end || temp_end > kbuf->buf_max)
  384. return 0;
  385. /*
  386. * Make sure this does not conflict with any of existing
  387. * segments
  388. */
  389. if (kimage_is_destination_range(image, temp_start, temp_end)) {
  390. temp_start = temp_start + PAGE_SIZE;
  391. continue;
  392. }
  393. /* We found a suitable memory range */
  394. break;
  395. } while (1);
  396. /* If we are here, we found a suitable memory range */
  397. kbuf->mem = temp_start;
  398. /* Success, stop navigating through remaining System RAM ranges */
  399. return 1;
  400. }
  401. static int locate_mem_hole_callback(struct resource *res, void *arg)
  402. {
  403. struct kexec_buf *kbuf = (struct kexec_buf *)arg;
  404. u64 start = res->start, end = res->end;
  405. unsigned long sz = end - start + 1;
  406. /* Returning 0 will take to next memory range */
  407. /* Don't use memory that will be detected and handled by a driver. */
  408. if (res->flags & IORESOURCE_SYSRAM_DRIVER_MANAGED)
  409. return 0;
  410. if (sz < kbuf->memsz)
  411. return 0;
  412. if (end < kbuf->buf_min || start > kbuf->buf_max)
  413. return 0;
  414. /*
  415. * Allocate memory top down with-in ram range. Otherwise bottom up
  416. * allocation.
  417. */
  418. if (kbuf->top_down)
  419. return locate_mem_hole_top_down(start, end, kbuf);
  420. return locate_mem_hole_bottom_up(start, end, kbuf);
  421. }
  422. #ifdef CONFIG_ARCH_KEEP_MEMBLOCK
  423. static int kexec_walk_memblock(struct kexec_buf *kbuf,
  424. int (*func)(struct resource *, void *))
  425. {
  426. int ret = 0;
  427. u64 i;
  428. phys_addr_t mstart, mend;
  429. struct resource res = { };
  430. if (kbuf->image->type == KEXEC_TYPE_CRASH)
  431. return func(&crashk_res, kbuf);
  432. /*
  433. * Using MEMBLOCK_NONE will properly skip MEMBLOCK_DRIVER_MANAGED. See
  434. * IORESOURCE_SYSRAM_DRIVER_MANAGED handling in
  435. * locate_mem_hole_callback().
  436. */
  437. if (kbuf->top_down) {
  438. for_each_free_mem_range_reverse(i, NUMA_NO_NODE, MEMBLOCK_NONE,
  439. &mstart, &mend, NULL) {
  440. /*
  441. * In memblock, end points to the first byte after the
  442. * range while in kexec, end points to the last byte
  443. * in the range.
  444. */
  445. res.start = mstart;
  446. res.end = mend - 1;
  447. ret = func(&res, kbuf);
  448. if (ret)
  449. break;
  450. }
  451. } else {
  452. for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE,
  453. &mstart, &mend, NULL) {
  454. /*
  455. * In memblock, end points to the first byte after the
  456. * range while in kexec, end points to the last byte
  457. * in the range.
  458. */
  459. res.start = mstart;
  460. res.end = mend - 1;
  461. ret = func(&res, kbuf);
  462. if (ret)
  463. break;
  464. }
  465. }
  466. return ret;
  467. }
  468. #else
  469. static int kexec_walk_memblock(struct kexec_buf *kbuf,
  470. int (*func)(struct resource *, void *))
  471. {
  472. return 0;
  473. }
  474. #endif
  475. /**
  476. * kexec_walk_resources - call func(data) on free memory regions
  477. * @kbuf: Context info for the search. Also passed to @func.
  478. * @func: Function to call for each memory region.
  479. *
  480. * Return: The memory walk will stop when func returns a non-zero value
  481. * and that value will be returned. If all free regions are visited without
  482. * func returning non-zero, then zero will be returned.
  483. */
  484. static int kexec_walk_resources(struct kexec_buf *kbuf,
  485. int (*func)(struct resource *, void *))
  486. {
  487. if (kbuf->image->type == KEXEC_TYPE_CRASH)
  488. return walk_iomem_res_desc(crashk_res.desc,
  489. IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY,
  490. crashk_res.start, crashk_res.end,
  491. kbuf, func);
  492. else
  493. return walk_system_ram_res(0, ULONG_MAX, kbuf, func);
  494. }
  495. /**
  496. * kexec_locate_mem_hole - find free memory for the purgatory or the next kernel
  497. * @kbuf: Parameters for the memory search.
  498. *
  499. * On success, kbuf->mem will have the start address of the memory region found.
  500. *
  501. * Return: 0 on success, negative errno on error.
  502. */
  503. int kexec_locate_mem_hole(struct kexec_buf *kbuf)
  504. {
  505. int ret;
  506. /* Arch knows where to place */
  507. if (kbuf->mem != KEXEC_BUF_MEM_UNKNOWN)
  508. return 0;
  509. if (!IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK))
  510. ret = kexec_walk_resources(kbuf, locate_mem_hole_callback);
  511. else
  512. ret = kexec_walk_memblock(kbuf, locate_mem_hole_callback);
  513. return ret == 1 ? 0 : -EADDRNOTAVAIL;
  514. }
  515. /**
  516. * kexec_add_buffer - place a buffer in a kexec segment
  517. * @kbuf: Buffer contents and memory parameters.
  518. *
  519. * This function assumes that kexec_mutex is held.
  520. * On successful return, @kbuf->mem will have the physical address of
  521. * the buffer in memory.
  522. *
  523. * Return: 0 on success, negative errno on error.
  524. */
  525. int kexec_add_buffer(struct kexec_buf *kbuf)
  526. {
  527. struct kexec_segment *ksegment;
  528. int ret;
  529. /* Currently adding segment this way is allowed only in file mode */
  530. if (!kbuf->image->file_mode)
  531. return -EINVAL;
  532. if (kbuf->image->nr_segments >= KEXEC_SEGMENT_MAX)
  533. return -EINVAL;
  534. /*
  535. * Make sure we are not trying to add buffer after allocating
  536. * control pages. All segments need to be placed first before
  537. * any control pages are allocated. As control page allocation
  538. * logic goes through list of segments to make sure there are
  539. * no destination overlaps.
  540. */
  541. if (!list_empty(&kbuf->image->control_pages)) {
  542. WARN_ON(1);
  543. return -EINVAL;
  544. }
  545. /* Ensure minimum alignment needed for segments. */
  546. kbuf->memsz = ALIGN(kbuf->memsz, PAGE_SIZE);
  547. kbuf->buf_align = max(kbuf->buf_align, PAGE_SIZE);
  548. /* Walk the RAM ranges and allocate a suitable range for the buffer */
  549. ret = arch_kexec_locate_mem_hole(kbuf);
  550. if (ret)
  551. return ret;
  552. /* Found a suitable memory range */
  553. ksegment = &kbuf->image->segment[kbuf->image->nr_segments];
  554. ksegment->kbuf = kbuf->buffer;
  555. ksegment->bufsz = kbuf->bufsz;
  556. ksegment->mem = kbuf->mem;
  557. ksegment->memsz = kbuf->memsz;
  558. kbuf->image->nr_segments++;
  559. return 0;
  560. }
  561. /* Calculate and store the digest of segments */
  562. static int kexec_calculate_store_digests(struct kimage *image)
  563. {
  564. struct crypto_shash *tfm;
  565. struct shash_desc *desc;
  566. int ret = 0, i, j, zero_buf_sz, sha_region_sz;
  567. size_t desc_size, nullsz;
  568. char *digest;
  569. void *zero_buf;
  570. struct kexec_sha_region *sha_regions;
  571. struct purgatory_info *pi = &image->purgatory_info;
  572. if (!IS_ENABLED(CONFIG_ARCH_HAS_KEXEC_PURGATORY))
  573. return 0;
  574. zero_buf = __va(page_to_pfn(ZERO_PAGE(0)) << PAGE_SHIFT);
  575. zero_buf_sz = PAGE_SIZE;
  576. tfm = crypto_alloc_shash("sha256", 0, 0);
  577. if (IS_ERR(tfm)) {
  578. ret = PTR_ERR(tfm);
  579. goto out;
  580. }
  581. desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
  582. desc = kzalloc(desc_size, GFP_KERNEL);
  583. if (!desc) {
  584. ret = -ENOMEM;
  585. goto out_free_tfm;
  586. }
  587. sha_region_sz = KEXEC_SEGMENT_MAX * sizeof(struct kexec_sha_region);
  588. sha_regions = vzalloc(sha_region_sz);
  589. if (!sha_regions) {
  590. ret = -ENOMEM;
  591. goto out_free_desc;
  592. }
  593. desc->tfm = tfm;
  594. ret = crypto_shash_init(desc);
  595. if (ret < 0)
  596. goto out_free_sha_regions;
  597. digest = kzalloc(SHA256_DIGEST_SIZE, GFP_KERNEL);
  598. if (!digest) {
  599. ret = -ENOMEM;
  600. goto out_free_sha_regions;
  601. }
  602. for (j = i = 0; i < image->nr_segments; i++) {
  603. struct kexec_segment *ksegment;
  604. ksegment = &image->segment[i];
  605. /*
  606. * Skip purgatory as it will be modified once we put digest
  607. * info in purgatory.
  608. */
  609. if (ksegment->kbuf == pi->purgatory_buf)
  610. continue;
  611. ret = crypto_shash_update(desc, ksegment->kbuf,
  612. ksegment->bufsz);
  613. if (ret)
  614. break;
  615. /*
  616. * Assume rest of the buffer is filled with zero and
  617. * update digest accordingly.
  618. */
  619. nullsz = ksegment->memsz - ksegment->bufsz;
  620. while (nullsz) {
  621. unsigned long bytes = nullsz;
  622. if (bytes > zero_buf_sz)
  623. bytes = zero_buf_sz;
  624. ret = crypto_shash_update(desc, zero_buf, bytes);
  625. if (ret)
  626. break;
  627. nullsz -= bytes;
  628. }
  629. if (ret)
  630. break;
  631. sha_regions[j].start = ksegment->mem;
  632. sha_regions[j].len = ksegment->memsz;
  633. j++;
  634. }
  635. if (!ret) {
  636. ret = crypto_shash_final(desc, digest);
  637. if (ret)
  638. goto out_free_digest;
  639. ret = kexec_purgatory_get_set_symbol(image, "purgatory_sha_regions",
  640. sha_regions, sha_region_sz, 0);
  641. if (ret)
  642. goto out_free_digest;
  643. ret = kexec_purgatory_get_set_symbol(image, "purgatory_sha256_digest",
  644. digest, SHA256_DIGEST_SIZE, 0);
  645. if (ret)
  646. goto out_free_digest;
  647. }
  648. out_free_digest:
  649. kfree(digest);
  650. out_free_sha_regions:
  651. vfree(sha_regions);
  652. out_free_desc:
  653. kfree(desc);
  654. out_free_tfm:
  655. kfree(tfm);
  656. out:
  657. return ret;
  658. }
  659. #ifdef CONFIG_ARCH_HAS_KEXEC_PURGATORY
  660. /*
  661. * kexec_purgatory_setup_kbuf - prepare buffer to load purgatory.
  662. * @pi: Purgatory to be loaded.
  663. * @kbuf: Buffer to setup.
  664. *
  665. * Allocates the memory needed for the buffer. Caller is responsible to free
  666. * the memory after use.
  667. *
  668. * Return: 0 on success, negative errno on error.
  669. */
  670. static int kexec_purgatory_setup_kbuf(struct purgatory_info *pi,
  671. struct kexec_buf *kbuf)
  672. {
  673. const Elf_Shdr *sechdrs;
  674. unsigned long bss_align;
  675. unsigned long bss_sz;
  676. unsigned long align;
  677. int i, ret;
  678. sechdrs = (void *)pi->ehdr + pi->ehdr->e_shoff;
  679. kbuf->buf_align = bss_align = 1;
  680. kbuf->bufsz = bss_sz = 0;
  681. for (i = 0; i < pi->ehdr->e_shnum; i++) {
  682. if (!(sechdrs[i].sh_flags & SHF_ALLOC))
  683. continue;
  684. align = sechdrs[i].sh_addralign;
  685. if (sechdrs[i].sh_type != SHT_NOBITS) {
  686. if (kbuf->buf_align < align)
  687. kbuf->buf_align = align;
  688. kbuf->bufsz = ALIGN(kbuf->bufsz, align);
  689. kbuf->bufsz += sechdrs[i].sh_size;
  690. } else {
  691. if (bss_align < align)
  692. bss_align = align;
  693. bss_sz = ALIGN(bss_sz, align);
  694. bss_sz += sechdrs[i].sh_size;
  695. }
  696. }
  697. kbuf->bufsz = ALIGN(kbuf->bufsz, bss_align);
  698. kbuf->memsz = kbuf->bufsz + bss_sz;
  699. if (kbuf->buf_align < bss_align)
  700. kbuf->buf_align = bss_align;
  701. kbuf->buffer = vzalloc(kbuf->bufsz);
  702. if (!kbuf->buffer)
  703. return -ENOMEM;
  704. pi->purgatory_buf = kbuf->buffer;
  705. ret = kexec_add_buffer(kbuf);
  706. if (ret)
  707. goto out;
  708. return 0;
  709. out:
  710. vfree(pi->purgatory_buf);
  711. pi->purgatory_buf = NULL;
  712. return ret;
  713. }
  714. /*
  715. * kexec_purgatory_setup_sechdrs - prepares the pi->sechdrs buffer.
  716. * @pi: Purgatory to be loaded.
  717. * @kbuf: Buffer prepared to store purgatory.
  718. *
  719. * Allocates the memory needed for the buffer. Caller is responsible to free
  720. * the memory after use.
  721. *
  722. * Return: 0 on success, negative errno on error.
  723. */
  724. static int kexec_purgatory_setup_sechdrs(struct purgatory_info *pi,
  725. struct kexec_buf *kbuf)
  726. {
  727. unsigned long bss_addr;
  728. unsigned long offset;
  729. Elf_Shdr *sechdrs;
  730. int i;
  731. /*
  732. * The section headers in kexec_purgatory are read-only. In order to
  733. * have them modifiable make a temporary copy.
  734. */
  735. sechdrs = vzalloc(array_size(sizeof(Elf_Shdr), pi->ehdr->e_shnum));
  736. if (!sechdrs)
  737. return -ENOMEM;
  738. memcpy(sechdrs, (void *)pi->ehdr + pi->ehdr->e_shoff,
  739. pi->ehdr->e_shnum * sizeof(Elf_Shdr));
  740. pi->sechdrs = sechdrs;
  741. offset = 0;
  742. bss_addr = kbuf->mem + kbuf->bufsz;
  743. kbuf->image->start = pi->ehdr->e_entry;
  744. for (i = 0; i < pi->ehdr->e_shnum; i++) {
  745. unsigned long align;
  746. void *src, *dst;
  747. if (!(sechdrs[i].sh_flags & SHF_ALLOC))
  748. continue;
  749. align = sechdrs[i].sh_addralign;
  750. if (sechdrs[i].sh_type == SHT_NOBITS) {
  751. bss_addr = ALIGN(bss_addr, align);
  752. sechdrs[i].sh_addr = bss_addr;
  753. bss_addr += sechdrs[i].sh_size;
  754. continue;
  755. }
  756. offset = ALIGN(offset, align);
  757. /*
  758. * Check if the segment contains the entry point, if so,
  759. * calculate the value of image->start based on it.
  760. * If the compiler has produced more than one .text section
  761. * (Eg: .text.hot), they are generally after the main .text
  762. * section, and they shall not be used to calculate
  763. * image->start. So do not re-calculate image->start if it
  764. * is not set to the initial value, and warn the user so they
  765. * have a chance to fix their purgatory's linker script.
  766. */
  767. if (sechdrs[i].sh_flags & SHF_EXECINSTR &&
  768. pi->ehdr->e_entry >= sechdrs[i].sh_addr &&
  769. pi->ehdr->e_entry < (sechdrs[i].sh_addr
  770. + sechdrs[i].sh_size) &&
  771. !WARN_ON(kbuf->image->start != pi->ehdr->e_entry)) {
  772. kbuf->image->start -= sechdrs[i].sh_addr;
  773. kbuf->image->start += kbuf->mem + offset;
  774. }
  775. src = (void *)pi->ehdr + sechdrs[i].sh_offset;
  776. dst = pi->purgatory_buf + offset;
  777. memcpy(dst, src, sechdrs[i].sh_size);
  778. sechdrs[i].sh_addr = kbuf->mem + offset;
  779. sechdrs[i].sh_offset = offset;
  780. offset += sechdrs[i].sh_size;
  781. }
  782. return 0;
  783. }
  784. static int kexec_apply_relocations(struct kimage *image)
  785. {
  786. int i, ret;
  787. struct purgatory_info *pi = &image->purgatory_info;
  788. const Elf_Shdr *sechdrs;
  789. sechdrs = (void *)pi->ehdr + pi->ehdr->e_shoff;
  790. for (i = 0; i < pi->ehdr->e_shnum; i++) {
  791. const Elf_Shdr *relsec;
  792. const Elf_Shdr *symtab;
  793. Elf_Shdr *section;
  794. relsec = sechdrs + i;
  795. if (relsec->sh_type != SHT_RELA &&
  796. relsec->sh_type != SHT_REL)
  797. continue;
  798. /*
  799. * For section of type SHT_RELA/SHT_REL,
  800. * ->sh_link contains section header index of associated
  801. * symbol table. And ->sh_info contains section header
  802. * index of section to which relocations apply.
  803. */
  804. if (relsec->sh_info >= pi->ehdr->e_shnum ||
  805. relsec->sh_link >= pi->ehdr->e_shnum)
  806. return -ENOEXEC;
  807. section = pi->sechdrs + relsec->sh_info;
  808. symtab = sechdrs + relsec->sh_link;
  809. if (!(section->sh_flags & SHF_ALLOC))
  810. continue;
  811. /*
  812. * symtab->sh_link contain section header index of associated
  813. * string table.
  814. */
  815. if (symtab->sh_link >= pi->ehdr->e_shnum)
  816. /* Invalid section number? */
  817. continue;
  818. /*
  819. * Respective architecture needs to provide support for applying
  820. * relocations of type SHT_RELA/SHT_REL.
  821. */
  822. if (relsec->sh_type == SHT_RELA)
  823. ret = arch_kexec_apply_relocations_add(pi, section,
  824. relsec, symtab);
  825. else if (relsec->sh_type == SHT_REL)
  826. ret = arch_kexec_apply_relocations(pi, section,
  827. relsec, symtab);
  828. if (ret)
  829. return ret;
  830. }
  831. return 0;
  832. }
  833. /*
  834. * kexec_load_purgatory - Load and relocate the purgatory object.
  835. * @image: Image to add the purgatory to.
  836. * @kbuf: Memory parameters to use.
  837. *
  838. * Allocates the memory needed for image->purgatory_info.sechdrs and
  839. * image->purgatory_info.purgatory_buf/kbuf->buffer. Caller is responsible
  840. * to free the memory after use.
  841. *
  842. * Return: 0 on success, negative errno on error.
  843. */
  844. int kexec_load_purgatory(struct kimage *image, struct kexec_buf *kbuf)
  845. {
  846. struct purgatory_info *pi = &image->purgatory_info;
  847. int ret;
  848. if (kexec_purgatory_size <= 0)
  849. return -EINVAL;
  850. pi->ehdr = (const Elf_Ehdr *)kexec_purgatory;
  851. ret = kexec_purgatory_setup_kbuf(pi, kbuf);
  852. if (ret)
  853. return ret;
  854. ret = kexec_purgatory_setup_sechdrs(pi, kbuf);
  855. if (ret)
  856. goto out_free_kbuf;
  857. ret = kexec_apply_relocations(image);
  858. if (ret)
  859. goto out;
  860. return 0;
  861. out:
  862. vfree(pi->sechdrs);
  863. pi->sechdrs = NULL;
  864. out_free_kbuf:
  865. vfree(pi->purgatory_buf);
  866. pi->purgatory_buf = NULL;
  867. return ret;
  868. }
  869. /*
  870. * kexec_purgatory_find_symbol - find a symbol in the purgatory
  871. * @pi: Purgatory to search in.
  872. * @name: Name of the symbol.
  873. *
  874. * Return: pointer to symbol in read-only symtab on success, NULL on error.
  875. */
  876. static const Elf_Sym *kexec_purgatory_find_symbol(struct purgatory_info *pi,
  877. const char *name)
  878. {
  879. const Elf_Shdr *sechdrs;
  880. const Elf_Ehdr *ehdr;
  881. const Elf_Sym *syms;
  882. const char *strtab;
  883. int i, k;
  884. if (!pi->ehdr)
  885. return NULL;
  886. ehdr = pi->ehdr;
  887. sechdrs = (void *)ehdr + ehdr->e_shoff;
  888. for (i = 0; i < ehdr->e_shnum; i++) {
  889. if (sechdrs[i].sh_type != SHT_SYMTAB)
  890. continue;
  891. if (sechdrs[i].sh_link >= ehdr->e_shnum)
  892. /* Invalid strtab section number */
  893. continue;
  894. strtab = (void *)ehdr + sechdrs[sechdrs[i].sh_link].sh_offset;
  895. syms = (void *)ehdr + sechdrs[i].sh_offset;
  896. /* Go through symbols for a match */
  897. for (k = 0; k < sechdrs[i].sh_size/sizeof(Elf_Sym); k++) {
  898. if (ELF_ST_BIND(syms[k].st_info) != STB_GLOBAL)
  899. continue;
  900. if (strcmp(strtab + syms[k].st_name, name) != 0)
  901. continue;
  902. if (syms[k].st_shndx == SHN_UNDEF ||
  903. syms[k].st_shndx >= ehdr->e_shnum) {
  904. pr_debug("Symbol: %s has bad section index %d.\n",
  905. name, syms[k].st_shndx);
  906. return NULL;
  907. }
  908. /* Found the symbol we are looking for */
  909. return &syms[k];
  910. }
  911. }
  912. return NULL;
  913. }
  914. void *kexec_purgatory_get_symbol_addr(struct kimage *image, const char *name)
  915. {
  916. struct purgatory_info *pi = &image->purgatory_info;
  917. const Elf_Sym *sym;
  918. Elf_Shdr *sechdr;
  919. sym = kexec_purgatory_find_symbol(pi, name);
  920. if (!sym)
  921. return ERR_PTR(-EINVAL);
  922. sechdr = &pi->sechdrs[sym->st_shndx];
  923. /*
  924. * Returns the address where symbol will finally be loaded after
  925. * kexec_load_segment()
  926. */
  927. return (void *)(sechdr->sh_addr + sym->st_value);
  928. }
  929. /*
  930. * Get or set value of a symbol. If "get_value" is true, symbol value is
  931. * returned in buf otherwise symbol value is set based on value in buf.
  932. */
  933. int kexec_purgatory_get_set_symbol(struct kimage *image, const char *name,
  934. void *buf, unsigned int size, bool get_value)
  935. {
  936. struct purgatory_info *pi = &image->purgatory_info;
  937. const Elf_Sym *sym;
  938. Elf_Shdr *sec;
  939. char *sym_buf;
  940. sym = kexec_purgatory_find_symbol(pi, name);
  941. if (!sym)
  942. return -EINVAL;
  943. if (sym->st_size != size) {
  944. pr_err("symbol %s size mismatch: expected %lu actual %u\n",
  945. name, (unsigned long)sym->st_size, size);
  946. return -EINVAL;
  947. }
  948. sec = pi->sechdrs + sym->st_shndx;
  949. if (sec->sh_type == SHT_NOBITS) {
  950. pr_err("symbol %s is in a bss section. Cannot %s\n", name,
  951. get_value ? "get" : "set");
  952. return -EINVAL;
  953. }
  954. sym_buf = (char *)pi->purgatory_buf + sec->sh_offset + sym->st_value;
  955. if (get_value)
  956. memcpy((void *)buf, sym_buf, size);
  957. else
  958. memcpy((void *)sym_buf, buf, size);
  959. return 0;
  960. }
  961. #endif /* CONFIG_ARCH_HAS_KEXEC_PURGATORY */
  962. int crash_exclude_mem_range(struct crash_mem *mem,
  963. unsigned long long mstart, unsigned long long mend)
  964. {
  965. int i, j;
  966. unsigned long long start, end, p_start, p_end;
  967. struct crash_mem_range temp_range = {0, 0};
  968. for (i = 0; i < mem->nr_ranges; i++) {
  969. start = mem->ranges[i].start;
  970. end = mem->ranges[i].end;
  971. p_start = mstart;
  972. p_end = mend;
  973. if (mstart > end || mend < start)
  974. continue;
  975. /* Truncate any area outside of range */
  976. if (mstart < start)
  977. p_start = start;
  978. if (mend > end)
  979. p_end = end;
  980. /* Found completely overlapping range */
  981. if (p_start == start && p_end == end) {
  982. mem->ranges[i].start = 0;
  983. mem->ranges[i].end = 0;
  984. if (i < mem->nr_ranges - 1) {
  985. /* Shift rest of the ranges to left */
  986. for (j = i; j < mem->nr_ranges - 1; j++) {
  987. mem->ranges[j].start =
  988. mem->ranges[j+1].start;
  989. mem->ranges[j].end =
  990. mem->ranges[j+1].end;
  991. }
  992. /*
  993. * Continue to check if there are another overlapping ranges
  994. * from the current position because of shifting the above
  995. * mem ranges.
  996. */
  997. i--;
  998. mem->nr_ranges--;
  999. continue;
  1000. }
  1001. mem->nr_ranges--;
  1002. return 0;
  1003. }
  1004. if (p_start > start && p_end < end) {
  1005. /* Split original range */
  1006. mem->ranges[i].end = p_start - 1;
  1007. temp_range.start = p_end + 1;
  1008. temp_range.end = end;
  1009. } else if (p_start != start)
  1010. mem->ranges[i].end = p_start - 1;
  1011. else
  1012. mem->ranges[i].start = p_end + 1;
  1013. break;
  1014. }
  1015. /* If a split happened, add the split to array */
  1016. if (!temp_range.end)
  1017. return 0;
  1018. /* Split happened */
  1019. if (i == mem->max_nr_ranges - 1)
  1020. return -ENOMEM;
  1021. /* Location where new range should go */
  1022. j = i + 1;
  1023. if (j < mem->nr_ranges) {
  1024. /* Move over all ranges one slot towards the end */
  1025. for (i = mem->nr_ranges - 1; i >= j; i--)
  1026. mem->ranges[i + 1] = mem->ranges[i];
  1027. }
  1028. mem->ranges[j].start = temp_range.start;
  1029. mem->ranges[j].end = temp_range.end;
  1030. mem->nr_ranges++;
  1031. return 0;
  1032. }
  1033. int crash_prepare_elf64_headers(struct crash_mem *mem, int need_kernel_map,
  1034. void **addr, unsigned long *sz)
  1035. {
  1036. Elf64_Ehdr *ehdr;
  1037. Elf64_Phdr *phdr;
  1038. unsigned long nr_cpus = num_possible_cpus(), nr_phdr, elf_sz;
  1039. unsigned char *buf;
  1040. unsigned int cpu, i;
  1041. unsigned long long notes_addr;
  1042. unsigned long mstart, mend;
  1043. /* extra phdr for vmcoreinfo ELF note */
  1044. nr_phdr = nr_cpus + 1;
  1045. nr_phdr += mem->nr_ranges;
  1046. /*
  1047. * kexec-tools creates an extra PT_LOAD phdr for kernel text mapping
  1048. * area (for example, ffffffff80000000 - ffffffffa0000000 on x86_64).
  1049. * I think this is required by tools like gdb. So same physical
  1050. * memory will be mapped in two ELF headers. One will contain kernel
  1051. * text virtual addresses and other will have __va(physical) addresses.
  1052. */
  1053. nr_phdr++;
  1054. elf_sz = sizeof(Elf64_Ehdr) + nr_phdr * sizeof(Elf64_Phdr);
  1055. elf_sz = ALIGN(elf_sz, ELF_CORE_HEADER_ALIGN);
  1056. buf = vzalloc(elf_sz);
  1057. if (!buf)
  1058. return -ENOMEM;
  1059. ehdr = (Elf64_Ehdr *)buf;
  1060. phdr = (Elf64_Phdr *)(ehdr + 1);
  1061. memcpy(ehdr->e_ident, ELFMAG, SELFMAG);
  1062. ehdr->e_ident[EI_CLASS] = ELFCLASS64;
  1063. ehdr->e_ident[EI_DATA] = ELFDATA2LSB;
  1064. ehdr->e_ident[EI_VERSION] = EV_CURRENT;
  1065. ehdr->e_ident[EI_OSABI] = ELF_OSABI;
  1066. memset(ehdr->e_ident + EI_PAD, 0, EI_NIDENT - EI_PAD);
  1067. ehdr->e_type = ET_CORE;
  1068. ehdr->e_machine = ELF_ARCH;
  1069. ehdr->e_version = EV_CURRENT;
  1070. ehdr->e_phoff = sizeof(Elf64_Ehdr);
  1071. ehdr->e_ehsize = sizeof(Elf64_Ehdr);
  1072. ehdr->e_phentsize = sizeof(Elf64_Phdr);
  1073. /* Prepare one phdr of type PT_NOTE for each present CPU */
  1074. for_each_present_cpu(cpu) {
  1075. phdr->p_type = PT_NOTE;
  1076. notes_addr = per_cpu_ptr_to_phys(per_cpu_ptr(crash_notes, cpu));
  1077. phdr->p_offset = phdr->p_paddr = notes_addr;
  1078. phdr->p_filesz = phdr->p_memsz = sizeof(note_buf_t);
  1079. (ehdr->e_phnum)++;
  1080. phdr++;
  1081. }
  1082. /* Prepare one PT_NOTE header for vmcoreinfo */
  1083. phdr->p_type = PT_NOTE;
  1084. phdr->p_offset = phdr->p_paddr = paddr_vmcoreinfo_note();
  1085. phdr->p_filesz = phdr->p_memsz = VMCOREINFO_NOTE_SIZE;
  1086. (ehdr->e_phnum)++;
  1087. phdr++;
  1088. /* Prepare PT_LOAD type program header for kernel text region */
  1089. if (need_kernel_map) {
  1090. phdr->p_type = PT_LOAD;
  1091. phdr->p_flags = PF_R|PF_W|PF_X;
  1092. phdr->p_vaddr = (unsigned long) _text;
  1093. phdr->p_filesz = phdr->p_memsz = _end - _text;
  1094. phdr->p_offset = phdr->p_paddr = __pa_symbol(_text);
  1095. ehdr->e_phnum++;
  1096. phdr++;
  1097. }
  1098. /* Go through all the ranges in mem->ranges[] and prepare phdr */
  1099. for (i = 0; i < mem->nr_ranges; i++) {
  1100. mstart = mem->ranges[i].start;
  1101. mend = mem->ranges[i].end;
  1102. phdr->p_type = PT_LOAD;
  1103. phdr->p_flags = PF_R|PF_W|PF_X;
  1104. phdr->p_offset = mstart;
  1105. phdr->p_paddr = mstart;
  1106. phdr->p_vaddr = (unsigned long) __va(mstart);
  1107. phdr->p_filesz = phdr->p_memsz = mend - mstart + 1;
  1108. phdr->p_align = 0;
  1109. ehdr->e_phnum++;
  1110. pr_debug("Crash PT_LOAD ELF header. phdr=%p vaddr=0x%llx, paddr=0x%llx, sz=0x%llx e_phnum=%d p_offset=0x%llx\n",
  1111. phdr, phdr->p_vaddr, phdr->p_paddr, phdr->p_filesz,
  1112. ehdr->e_phnum, phdr->p_offset);
  1113. phdr++;
  1114. }
  1115. *addr = buf;
  1116. *sz = elf_sz;
  1117. return 0;
  1118. }