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