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