blob: 84f7316792a72a29efccdd8999333bac768a53d8 [file] [log] [blame]
Thomas Gleixner40b0b3f2019-06-03 07:44:46 +02001// SPDX-License-Identifier: GPL-2.0-only
Dave Younga43cac02015-09-09 15:38:51 -07002/*
3 * kexec: kexec_file_load system call
4 *
5 * Copyright (C) 2014 Red Hat Inc.
6 * Authors:
7 * Vivek Goyal <vgoyal@redhat.com>
Dave Younga43cac02015-09-09 15:38:51 -07008 */
9
Minfei Huangde90a6b2015-11-06 16:32:45 -080010#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
Dave Younga43cac02015-09-09 15:38:51 -070012#include <linux/capability.h>
13#include <linux/mm.h>
14#include <linux/file.h>
15#include <linux/slab.h>
16#include <linux/kexec.h>
AKASHI Takahiro735c2f92018-11-15 14:52:43 +090017#include <linux/memblock.h>
Dave Younga43cac02015-09-09 15:38:51 -070018#include <linux/mutex.h>
19#include <linux/list.h>
Mimi Zoharb804def2016-01-14 20:59:14 -050020#include <linux/fs.h>
Mimi Zohar7b8589c2016-12-19 16:22:48 -080021#include <linux/ima.h>
Dave Younga43cac02015-09-09 15:38:51 -070022#include <crypto/hash.h>
23#include <crypto/sha.h>
AKASHI Takahirobabac4a2018-04-13 15:36:06 -070024#include <linux/elf.h>
25#include <linux/elfcore.h>
26#include <linux/kernel.h>
Scott Brandenb89999d02020-10-02 10:38:15 -070027#include <linux/kernel_read_file.h>
Dave Younga43cac02015-09-09 15:38:51 -070028#include <linux/syscalls.h>
29#include <linux/vmalloc.h>
30#include "kexec_internal.h"
31
Dave Younga43cac02015-09-09 15:38:51 -070032static int kexec_calculate_store_digests(struct kimage *image);
33
AKASHI Takahiro9ec4ece2018-04-13 15:35:49 -070034/*
35 * Currently this is the only default function that is exported as some
36 * architectures need it to do additional handlings.
37 * In the future, other default functions may be exported too if required.
38 */
39int kexec_image_probe_default(struct kimage *image, void *buf,
40 unsigned long buf_len)
41{
42 const struct kexec_file_ops * const *fops;
43 int ret = -ENOEXEC;
44
45 for (fops = &kexec_file_loaders[0]; *fops && (*fops)->probe; ++fops) {
46 ret = (*fops)->probe(buf, buf_len);
47 if (!ret) {
48 image->fops = *fops;
49 return ret;
50 }
51 }
52
53 return ret;
54}
55
Dave Younga43cac02015-09-09 15:38:51 -070056/* Architectures can provide this probe function */
57int __weak arch_kexec_kernel_image_probe(struct kimage *image, void *buf,
58 unsigned long buf_len)
59{
AKASHI Takahiro9ec4ece2018-04-13 15:35:49 -070060 return kexec_image_probe_default(image, buf, buf_len);
61}
62
63static void *kexec_image_load_default(struct kimage *image)
64{
65 if (!image->fops || !image->fops->load)
66 return ERR_PTR(-ENOEXEC);
67
68 return image->fops->load(image, image->kernel_buf,
69 image->kernel_buf_len, image->initrd_buf,
70 image->initrd_buf_len, image->cmdline_buf,
71 image->cmdline_buf_len);
Dave Younga43cac02015-09-09 15:38:51 -070072}
73
74void * __weak arch_kexec_kernel_image_load(struct kimage *image)
75{
AKASHI Takahiro9ec4ece2018-04-13 15:35:49 -070076 return kexec_image_load_default(image);
77}
78
AKASHI Takahiro92a98a22018-11-15 14:52:41 +090079int kexec_image_post_load_cleanup_default(struct kimage *image)
AKASHI Takahiro9ec4ece2018-04-13 15:35:49 -070080{
81 if (!image->fops || !image->fops->cleanup)
82 return 0;
83
84 return image->fops->cleanup(image->image_loader_data);
Dave Younga43cac02015-09-09 15:38:51 -070085}
86
87int __weak arch_kimage_file_post_load_cleanup(struct kimage *image)
88{
AKASHI Takahiro9ec4ece2018-04-13 15:35:49 -070089 return kexec_image_post_load_cleanup_default(image);
Dave Younga43cac02015-09-09 15:38:51 -070090}
91
Jiri Bohac99d5cadf2019-08-19 17:17:44 -070092#ifdef CONFIG_KEXEC_SIG
AKASHI Takahiro9ec4ece2018-04-13 15:35:49 -070093static int kexec_image_verify_sig_default(struct kimage *image, void *buf,
94 unsigned long buf_len)
95{
96 if (!image->fops || !image->fops->verify_sig) {
97 pr_debug("kernel loader does not support signature verification.\n");
98 return -EKEYREJECTED;
99 }
100
101 return image->fops->verify_sig(buf, buf_len);
102}
103
Dave Younga43cac02015-09-09 15:38:51 -0700104int __weak arch_kexec_kernel_verify_sig(struct kimage *image, void *buf,
105 unsigned long buf_len)
106{
AKASHI Takahiro9ec4ece2018-04-13 15:35:49 -0700107 return kexec_image_verify_sig_default(image, buf, buf_len);
Dave Younga43cac02015-09-09 15:38:51 -0700108}
Xunlei Pang978e30c2016-01-20 15:00:36 -0800109#endif
Dave Younga43cac02015-09-09 15:38:51 -0700110
Philipp Rudo8aec3952018-04-13 15:36:24 -0700111/*
112 * arch_kexec_apply_relocations_add - apply relocations of type RELA
113 * @pi: Purgatory to be relocated.
114 * @section: Section relocations applying to.
115 * @relsec: Section containing RELAs.
116 * @symtab: Corresponding symtab.
117 *
118 * Return: 0 on success, negative errno on error.
119 */
Dave Younga43cac02015-09-09 15:38:51 -0700120int __weak
Philipp Rudo8aec3952018-04-13 15:36:24 -0700121arch_kexec_apply_relocations_add(struct purgatory_info *pi, Elf_Shdr *section,
122 const Elf_Shdr *relsec, const Elf_Shdr *symtab)
Dave Younga43cac02015-09-09 15:38:51 -0700123{
124 pr_err("RELA relocation unsupported.\n");
125 return -ENOEXEC;
126}
127
Philipp Rudo8aec3952018-04-13 15:36:24 -0700128/*
129 * arch_kexec_apply_relocations - apply relocations of type REL
130 * @pi: Purgatory to be relocated.
131 * @section: Section relocations applying to.
132 * @relsec: Section containing RELs.
133 * @symtab: Corresponding symtab.
134 *
135 * Return: 0 on success, negative errno on error.
136 */
Dave Younga43cac02015-09-09 15:38:51 -0700137int __weak
Philipp Rudo8aec3952018-04-13 15:36:24 -0700138arch_kexec_apply_relocations(struct purgatory_info *pi, Elf_Shdr *section,
139 const Elf_Shdr *relsec, const Elf_Shdr *symtab)
Dave Younga43cac02015-09-09 15:38:51 -0700140{
141 pr_err("REL relocation unsupported.\n");
142 return -ENOEXEC;
143}
144
145/*
146 * Free up memory used by kernel, initrd, and command line. This is temporary
147 * memory allocation which is not needed any more after these buffers have
148 * been loaded into separate segments and have been copied elsewhere.
149 */
150void kimage_file_post_load_cleanup(struct kimage *image)
151{
152 struct purgatory_info *pi = &image->purgatory_info;
153
154 vfree(image->kernel_buf);
155 image->kernel_buf = NULL;
156
157 vfree(image->initrd_buf);
158 image->initrd_buf = NULL;
159
160 kfree(image->cmdline_buf);
161 image->cmdline_buf = NULL;
162
163 vfree(pi->purgatory_buf);
164 pi->purgatory_buf = NULL;
165
166 vfree(pi->sechdrs);
167 pi->sechdrs = NULL;
168
169 /* See if architecture has anything to cleanup post load */
170 arch_kimage_file_post_load_cleanup(image);
171
172 /*
173 * Above call should have called into bootloader to free up
174 * any data stored in kimage->image_loader_data. It should
175 * be ok now to free it up.
176 */
177 kfree(image->image_loader_data);
178 image->image_loader_data = NULL;
179}
180
Jiri Bohac99d5cadf2019-08-19 17:17:44 -0700181#ifdef CONFIG_KEXEC_SIG
182static int
183kimage_validate_signature(struct kimage *image)
184{
Jiri Bohac99d5cadf2019-08-19 17:17:44 -0700185 int ret;
186
187 ret = arch_kexec_kernel_verify_sig(image, image->kernel_buf,
188 image->kernel_buf_len);
Lianbo Jiangfd7af712020-06-25 20:29:27 -0700189 if (ret) {
Jiri Bohac99d5cadf2019-08-19 17:17:44 -0700190
Jiri Bohac99d5cadf2019-08-19 17:17:44 -0700191 if (IS_ENABLED(CONFIG_KEXEC_SIG_FORCE)) {
Lianbo Jiangfd7af712020-06-25 20:29:27 -0700192 pr_notice("Enforced kernel signature verification failed (%d).\n", ret);
Jiri Bohac99d5cadf2019-08-19 17:17:44 -0700193 return ret;
194 }
195
Lianbo Jiangfd7af712020-06-25 20:29:27 -0700196 /*
197 * If IMA is guaranteed to appraise a signature on the kexec
Matthew Garrett29d3c1c2019-08-19 17:18:01 -0700198 * image, permit it even if the kernel is otherwise locked
199 * down.
200 */
201 if (!ima_appraise_signature(READING_KEXEC_IMAGE) &&
202 security_locked_down(LOCKDOWN_KEXEC))
203 return -EPERM;
204
Lianbo Jiangfd7af712020-06-25 20:29:27 -0700205 pr_debug("kernel signature verification failed (%d).\n", ret);
Jiri Bohac99d5cadf2019-08-19 17:17:44 -0700206 }
207
Lianbo Jiangfd7af712020-06-25 20:29:27 -0700208 return 0;
Jiri Bohac99d5cadf2019-08-19 17:17:44 -0700209}
210#endif
211
Dave Younga43cac02015-09-09 15:38:51 -0700212/*
213 * In file mode list of segments is prepared by kernel. Copy relevant
214 * data from user space, do error checking, prepare segment list
215 */
216static int
217kimage_file_prepare_segments(struct kimage *image, int kernel_fd, int initrd_fd,
218 const char __user *cmdline_ptr,
219 unsigned long cmdline_len, unsigned flags)
220{
Jiri Bohac99d5cadf2019-08-19 17:17:44 -0700221 int ret;
Dave Younga43cac02015-09-09 15:38:51 -0700222 void *ldata;
223
Kees Cook0fa8e082020-10-02 10:38:25 -0700224 ret = kernel_read_file_from_fd(kernel_fd, 0, &image->kernel_buf,
Kees Cook88535282020-10-02 10:38:19 -0700225 INT_MAX, NULL, READING_KEXEC_IMAGE);
Kees Cookf7a4f682020-10-02 10:38:17 -0700226 if (ret < 0)
Dave Younga43cac02015-09-09 15:38:51 -0700227 return ret;
Kees Cookf7a4f682020-10-02 10:38:17 -0700228 image->kernel_buf_len = ret;
Dave Younga43cac02015-09-09 15:38:51 -0700229
230 /* Call arch image probe handlers */
231 ret = arch_kexec_kernel_image_probe(image, image->kernel_buf,
232 image->kernel_buf_len);
Dave Younga43cac02015-09-09 15:38:51 -0700233 if (ret)
234 goto out;
235
Jiri Bohac99d5cadf2019-08-19 17:17:44 -0700236#ifdef CONFIG_KEXEC_SIG
237 ret = kimage_validate_signature(image);
238
239 if (ret)
Dave Younga43cac02015-09-09 15:38:51 -0700240 goto out;
Dave Younga43cac02015-09-09 15:38:51 -0700241#endif
242 /* It is possible that there no initramfs is being loaded */
243 if (!(flags & KEXEC_FILE_NO_INITRAMFS)) {
Kees Cook0fa8e082020-10-02 10:38:25 -0700244 ret = kernel_read_file_from_fd(initrd_fd, 0, &image->initrd_buf,
Kees Cook88535282020-10-02 10:38:19 -0700245 INT_MAX, NULL,
Mimi Zoharb804def2016-01-14 20:59:14 -0500246 READING_KEXEC_INITRAMFS);
Kees Cookf7a4f682020-10-02 10:38:17 -0700247 if (ret < 0)
Dave Younga43cac02015-09-09 15:38:51 -0700248 goto out;
Kees Cookf7a4f682020-10-02 10:38:17 -0700249 image->initrd_buf_len = ret;
250 ret = 0;
Dave Younga43cac02015-09-09 15:38:51 -0700251 }
252
253 if (cmdline_len) {
Al Viroa9bd8df2017-05-13 18:39:01 -0400254 image->cmdline_buf = memdup_user(cmdline_ptr, cmdline_len);
255 if (IS_ERR(image->cmdline_buf)) {
256 ret = PTR_ERR(image->cmdline_buf);
257 image->cmdline_buf = NULL;
Dave Younga43cac02015-09-09 15:38:51 -0700258 goto out;
259 }
260
261 image->cmdline_buf_len = cmdline_len;
262
263 /* command line should be a string with last byte null */
264 if (image->cmdline_buf[cmdline_len - 1] != '\0') {
265 ret = -EINVAL;
266 goto out;
267 }
Prakhar Srivastava6a31fcd2019-06-23 23:23:31 -0700268
Tyler Hicks48341772020-07-09 01:19:11 -0500269 ima_kexec_cmdline(kernel_fd, image->cmdline_buf,
Prakhar Srivastava6a31fcd2019-06-23 23:23:31 -0700270 image->cmdline_buf_len - 1);
Dave Younga43cac02015-09-09 15:38:51 -0700271 }
272
Prakhar Srivastava6a31fcd2019-06-23 23:23:31 -0700273 /* IMA needs to pass the measurement list to the next kernel. */
274 ima_add_kexec_buffer(image);
275
Dave Younga43cac02015-09-09 15:38:51 -0700276 /* Call arch image load handlers */
277 ldata = arch_kexec_kernel_image_load(image);
278
279 if (IS_ERR(ldata)) {
280 ret = PTR_ERR(ldata);
281 goto out;
282 }
283
284 image->image_loader_data = ldata;
285out:
286 /* In case of error, free up all allocated memory in this function */
287 if (ret)
288 kimage_file_post_load_cleanup(image);
289 return ret;
290}
291
292static int
293kimage_file_alloc_init(struct kimage **rimage, int kernel_fd,
294 int initrd_fd, const char __user *cmdline_ptr,
295 unsigned long cmdline_len, unsigned long flags)
296{
297 int ret;
298 struct kimage *image;
299 bool kexec_on_panic = flags & KEXEC_FILE_ON_CRASH;
300
301 image = do_kimage_alloc_init();
302 if (!image)
303 return -ENOMEM;
304
305 image->file_mode = 1;
306
307 if (kexec_on_panic) {
308 /* Enable special crash kernel control page alloc policy. */
309 image->control_page = crashk_res.start;
310 image->type = KEXEC_TYPE_CRASH;
311 }
312
313 ret = kimage_file_prepare_segments(image, kernel_fd, initrd_fd,
314 cmdline_ptr, cmdline_len, flags);
315 if (ret)
316 goto out_free_image;
317
318 ret = sanity_check_segment_list(image);
319 if (ret)
320 goto out_free_post_load_bufs;
321
322 ret = -ENOMEM;
323 image->control_code_page = kimage_alloc_control_pages(image,
324 get_order(KEXEC_CONTROL_PAGE_SIZE));
325 if (!image->control_code_page) {
326 pr_err("Could not allocate control_code_buffer\n");
327 goto out_free_post_load_bufs;
328 }
329
330 if (!kexec_on_panic) {
331 image->swap_page = kimage_alloc_control_pages(image, 0);
332 if (!image->swap_page) {
333 pr_err("Could not allocate swap buffer\n");
334 goto out_free_control_pages;
335 }
336 }
337
338 *rimage = image;
339 return 0;
340out_free_control_pages:
341 kimage_free_page_list(&image->control_pages);
342out_free_post_load_bufs:
343 kimage_file_post_load_cleanup(image);
344out_free_image:
345 kfree(image);
346 return ret;
347}
348
349SYSCALL_DEFINE5(kexec_file_load, int, kernel_fd, int, initrd_fd,
350 unsigned long, cmdline_len, const char __user *, cmdline_ptr,
351 unsigned long, flags)
352{
353 int ret = 0, i;
354 struct kimage **dest_image, *image;
355
356 /* We only trust the superuser with rebooting the system. */
357 if (!capable(CAP_SYS_BOOT) || kexec_load_disabled)
358 return -EPERM;
359
360 /* Make sure we have a legal set of flags */
361 if (flags != (flags & KEXEC_FILE_FLAGS))
362 return -EINVAL;
363
364 image = NULL;
365
366 if (!mutex_trylock(&kexec_mutex))
367 return -EBUSY;
368
369 dest_image = &kexec_image;
Xunlei Pang9b492cf2016-05-23 16:24:10 -0700370 if (flags & KEXEC_FILE_ON_CRASH) {
Dave Younga43cac02015-09-09 15:38:51 -0700371 dest_image = &kexec_crash_image;
Xunlei Pang9b492cf2016-05-23 16:24:10 -0700372 if (kexec_crash_image)
373 arch_kexec_unprotect_crashkres();
374 }
Dave Younga43cac02015-09-09 15:38:51 -0700375
376 if (flags & KEXEC_FILE_UNLOAD)
377 goto exchange;
378
379 /*
380 * In case of crash, new kernel gets loaded in reserved region. It is
381 * same memory where old crash kernel might be loaded. Free any
382 * current crash dump kernel before we corrupt it.
383 */
384 if (flags & KEXEC_FILE_ON_CRASH)
385 kimage_free(xchg(&kexec_crash_image, NULL));
386
387 ret = kimage_file_alloc_init(&image, kernel_fd, initrd_fd, cmdline_ptr,
388 cmdline_len, flags);
389 if (ret)
390 goto out;
391
392 ret = machine_kexec_prepare(image);
393 if (ret)
394 goto out;
395
Xunlei Pang12293842017-07-12 14:33:21 -0700396 /*
397 * Some architecture(like S390) may touch the crash memory before
398 * machine_kexec_prepare(), we must copy vmcoreinfo data after it.
399 */
400 ret = kimage_crash_copy_vmcoreinfo(image);
401 if (ret)
402 goto out;
403
Dave Younga43cac02015-09-09 15:38:51 -0700404 ret = kexec_calculate_store_digests(image);
405 if (ret)
406 goto out;
407
408 for (i = 0; i < image->nr_segments; i++) {
409 struct kexec_segment *ksegment;
410
411 ksegment = &image->segment[i];
412 pr_debug("Loading segment %d: buf=0x%p bufsz=0x%zx mem=0x%lx memsz=0x%zx\n",
413 i, ksegment->buf, ksegment->bufsz, ksegment->mem,
414 ksegment->memsz);
415
416 ret = kimage_load_segment(image, &image->segment[i]);
417 if (ret)
418 goto out;
419 }
420
421 kimage_terminate(image);
422
Pavel Tatashinde68e4d2019-12-04 10:59:15 -0500423 ret = machine_kexec_post_load(image);
424 if (ret)
425 goto out;
426
Dave Younga43cac02015-09-09 15:38:51 -0700427 /*
428 * Free up any temporary buffers allocated which are not needed
429 * after image has been loaded
430 */
431 kimage_file_post_load_cleanup(image);
432exchange:
433 image = xchg(dest_image, image);
434out:
Xunlei Pang9b492cf2016-05-23 16:24:10 -0700435 if ((flags & KEXEC_FILE_ON_CRASH) && kexec_crash_image)
436 arch_kexec_protect_crashkres();
437
Dave Younga43cac02015-09-09 15:38:51 -0700438 mutex_unlock(&kexec_mutex);
439 kimage_free(image);
440 return ret;
441}
442
443static int locate_mem_hole_top_down(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_end = min(end, kbuf->buf_max);
450 temp_start = temp_end - kbuf->memsz;
451
452 do {
453 /* align down start */
454 temp_start = temp_start & (~(kbuf->buf_align - 1));
455
456 if (temp_start < start || temp_start < kbuf->buf_min)
457 return 0;
458
459 temp_end = temp_start + kbuf->memsz - 1;
460
461 /*
462 * Make sure this does not conflict with any of existing
463 * segments
464 */
465 if (kimage_is_destination_range(image, temp_start, temp_end)) {
466 temp_start = temp_start - PAGE_SIZE;
467 continue;
468 }
469
470 /* We found a suitable memory range */
471 break;
472 } while (1);
473
474 /* If we are here, we found a suitable memory range */
475 kbuf->mem = temp_start;
476
477 /* Success, stop navigating through remaining System RAM ranges */
478 return 1;
479}
480
481static int locate_mem_hole_bottom_up(unsigned long start, unsigned long end,
482 struct kexec_buf *kbuf)
483{
484 struct kimage *image = kbuf->image;
485 unsigned long temp_start, temp_end;
486
487 temp_start = max(start, kbuf->buf_min);
488
489 do {
490 temp_start = ALIGN(temp_start, kbuf->buf_align);
491 temp_end = temp_start + kbuf->memsz - 1;
492
493 if (temp_end > end || temp_end > kbuf->buf_max)
494 return 0;
495 /*
496 * Make sure this does not conflict with any of existing
497 * segments
498 */
499 if (kimage_is_destination_range(image, temp_start, temp_end)) {
500 temp_start = temp_start + PAGE_SIZE;
501 continue;
502 }
503
504 /* We found a suitable memory range */
505 break;
506 } while (1);
507
508 /* If we are here, we found a suitable memory range */
509 kbuf->mem = temp_start;
510
511 /* Success, stop navigating through remaining System RAM ranges */
512 return 1;
513}
514
Tom Lendacky1d2e7332017-10-20 09:30:51 -0500515static int locate_mem_hole_callback(struct resource *res, void *arg)
Dave Younga43cac02015-09-09 15:38:51 -0700516{
517 struct kexec_buf *kbuf = (struct kexec_buf *)arg;
Tom Lendacky1d2e7332017-10-20 09:30:51 -0500518 u64 start = res->start, end = res->end;
Dave Younga43cac02015-09-09 15:38:51 -0700519 unsigned long sz = end - start + 1;
520
521 /* Returning 0 will take to next memory range */
David Hildenbrand3fe4f492020-06-04 16:48:44 -0700522
523 /* Don't use memory that will be detected and handled by a driver. */
524 if (res->flags & IORESOURCE_MEM_DRIVER_MANAGED)
525 return 0;
526
Dave Younga43cac02015-09-09 15:38:51 -0700527 if (sz < kbuf->memsz)
528 return 0;
529
530 if (end < kbuf->buf_min || start > kbuf->buf_max)
531 return 0;
532
533 /*
534 * Allocate memory top down with-in ram range. Otherwise bottom up
535 * allocation.
536 */
537 if (kbuf->top_down)
538 return locate_mem_hole_top_down(start, end, kbuf);
539 return locate_mem_hole_bottom_up(start, end, kbuf);
540}
541
Mike Rapoport350e88b2019-05-13 17:22:59 -0700542#ifdef CONFIG_ARCH_KEEP_MEMBLOCK
AKASHI Takahiro735c2f92018-11-15 14:52:43 +0900543static int kexec_walk_memblock(struct kexec_buf *kbuf,
544 int (*func)(struct resource *, void *))
545{
546 int ret = 0;
547 u64 i;
548 phys_addr_t mstart, mend;
549 struct resource res = { };
550
AKASHI Takahiro497e1852018-11-15 14:52:44 +0900551 if (kbuf->image->type == KEXEC_TYPE_CRASH)
552 return func(&crashk_res, kbuf);
553
AKASHI Takahiro735c2f92018-11-15 14:52:43 +0900554 if (kbuf->top_down) {
AKASHI Takahiro497e1852018-11-15 14:52:44 +0900555 for_each_free_mem_range_reverse(i, NUMA_NO_NODE, MEMBLOCK_NONE,
AKASHI Takahiro735c2f92018-11-15 14:52:43 +0900556 &mstart, &mend, NULL) {
557 /*
558 * In memblock, end points to the first byte after the
559 * range while in kexec, end points to the last byte
560 * in the range.
561 */
562 res.start = mstart;
563 res.end = mend - 1;
564 ret = func(&res, kbuf);
565 if (ret)
566 break;
567 }
568 } else {
AKASHI Takahiro497e1852018-11-15 14:52:44 +0900569 for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE,
570 &mstart, &mend, NULL) {
AKASHI Takahiro735c2f92018-11-15 14:52:43 +0900571 /*
572 * In memblock, end points to the first byte after the
573 * range while in kexec, end points to the last byte
574 * in the range.
575 */
576 res.start = mstart;
577 res.end = mend - 1;
578 ret = func(&res, kbuf);
579 if (ret)
580 break;
581 }
582 }
583
584 return ret;
585}
Mike Rapoport350e88b2019-05-13 17:22:59 -0700586#else
587static int kexec_walk_memblock(struct kexec_buf *kbuf,
588 int (*func)(struct resource *, void *))
589{
590 return 0;
591}
AKASHI Takahiro735c2f92018-11-15 14:52:43 +0900592#endif
593
Thiago Jung Bauermann60fe3912016-11-29 23:45:47 +1100594/**
AKASHI Takahiro735c2f92018-11-15 14:52:43 +0900595 * kexec_walk_resources - call func(data) on free memory regions
Thiago Jung Bauermann60fe3912016-11-29 23:45:47 +1100596 * @kbuf: Context info for the search. Also passed to @func.
597 * @func: Function to call for each memory region.
598 *
599 * Return: The memory walk will stop when func returns a non-zero value
600 * and that value will be returned. If all free regions are visited without
601 * func returning non-zero, then zero will be returned.
602 */
AKASHI Takahiro735c2f92018-11-15 14:52:43 +0900603static int kexec_walk_resources(struct kexec_buf *kbuf,
604 int (*func)(struct resource *, void *))
Thiago Jung Bauermann60fe3912016-11-29 23:45:47 +1100605{
606 if (kbuf->image->type == KEXEC_TYPE_CRASH)
607 return walk_iomem_res_desc(crashk_res.desc,
608 IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY,
609 crashk_res.start, crashk_res.end,
610 kbuf, func);
611 else
612 return walk_system_ram_res(0, ULONG_MAX, kbuf, func);
613}
614
Thiago Jung Bauermannec2b9bf2016-11-29 23:45:48 +1100615/**
Thiago Jung Bauermanne2e806f2016-11-29 23:45:49 +1100616 * kexec_locate_mem_hole - find free memory for the purgatory or the next kernel
617 * @kbuf: Parameters for the memory search.
618 *
619 * On success, kbuf->mem will have the start address of the memory region found.
620 *
621 * Return: 0 on success, negative errno on error.
622 */
623int kexec_locate_mem_hole(struct kexec_buf *kbuf)
624{
625 int ret;
626
AKASHI Takahirob6664ba2018-11-15 14:52:42 +0900627 /* Arch knows where to place */
628 if (kbuf->mem != KEXEC_BUF_MEM_UNKNOWN)
629 return 0;
630
Mike Rapoport350e88b2019-05-13 17:22:59 -0700631 if (!IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK))
AKASHI Takahiro735c2f92018-11-15 14:52:43 +0900632 ret = kexec_walk_resources(kbuf, locate_mem_hole_callback);
633 else
634 ret = kexec_walk_memblock(kbuf, locate_mem_hole_callback);
Thiago Jung Bauermanne2e806f2016-11-29 23:45:49 +1100635
636 return ret == 1 ? 0 : -EADDRNOTAVAIL;
637}
638
639/**
Hari Bathinif891f192020-07-29 17:09:19 +0530640 * arch_kexec_locate_mem_hole - Find free memory to place the segments.
641 * @kbuf: Parameters for the memory search.
642 *
643 * On success, kbuf->mem will have the start address of the memory region found.
644 *
645 * Return: 0 on success, negative errno on error.
646 */
647int __weak arch_kexec_locate_mem_hole(struct kexec_buf *kbuf)
648{
649 return kexec_locate_mem_hole(kbuf);
650}
651
652/**
Thiago Jung Bauermannec2b9bf2016-11-29 23:45:48 +1100653 * kexec_add_buffer - place a buffer in a kexec segment
654 * @kbuf: Buffer contents and memory parameters.
655 *
656 * This function assumes that kexec_mutex is held.
657 * On successful return, @kbuf->mem will have the physical address of
658 * the buffer in memory.
659 *
660 * Return: 0 on success, negative errno on error.
Dave Younga43cac02015-09-09 15:38:51 -0700661 */
Thiago Jung Bauermannec2b9bf2016-11-29 23:45:48 +1100662int kexec_add_buffer(struct kexec_buf *kbuf)
Dave Younga43cac02015-09-09 15:38:51 -0700663{
Dave Younga43cac02015-09-09 15:38:51 -0700664 struct kexec_segment *ksegment;
Dave Younga43cac02015-09-09 15:38:51 -0700665 int ret;
666
667 /* Currently adding segment this way is allowed only in file mode */
Thiago Jung Bauermannec2b9bf2016-11-29 23:45:48 +1100668 if (!kbuf->image->file_mode)
Dave Younga43cac02015-09-09 15:38:51 -0700669 return -EINVAL;
670
Thiago Jung Bauermannec2b9bf2016-11-29 23:45:48 +1100671 if (kbuf->image->nr_segments >= KEXEC_SEGMENT_MAX)
Dave Younga43cac02015-09-09 15:38:51 -0700672 return -EINVAL;
673
674 /*
675 * Make sure we are not trying to add buffer after allocating
676 * control pages. All segments need to be placed first before
677 * any control pages are allocated. As control page allocation
678 * logic goes through list of segments to make sure there are
679 * no destination overlaps.
680 */
Thiago Jung Bauermannec2b9bf2016-11-29 23:45:48 +1100681 if (!list_empty(&kbuf->image->control_pages)) {
Dave Younga43cac02015-09-09 15:38:51 -0700682 WARN_ON(1);
683 return -EINVAL;
684 }
685
Thiago Jung Bauermannec2b9bf2016-11-29 23:45:48 +1100686 /* Ensure minimum alignment needed for segments. */
687 kbuf->memsz = ALIGN(kbuf->memsz, PAGE_SIZE);
688 kbuf->buf_align = max(kbuf->buf_align, PAGE_SIZE);
Dave Younga43cac02015-09-09 15:38:51 -0700689
690 /* Walk the RAM ranges and allocate a suitable range for the buffer */
Hari Bathinif891f192020-07-29 17:09:19 +0530691 ret = arch_kexec_locate_mem_hole(kbuf);
Thiago Jung Bauermanne2e806f2016-11-29 23:45:49 +1100692 if (ret)
693 return ret;
Dave Younga43cac02015-09-09 15:38:51 -0700694
695 /* Found a suitable memory range */
Thiago Jung Bauermannec2b9bf2016-11-29 23:45:48 +1100696 ksegment = &kbuf->image->segment[kbuf->image->nr_segments];
Dave Younga43cac02015-09-09 15:38:51 -0700697 ksegment->kbuf = kbuf->buffer;
698 ksegment->bufsz = kbuf->bufsz;
699 ksegment->mem = kbuf->mem;
700 ksegment->memsz = kbuf->memsz;
Thiago Jung Bauermannec2b9bf2016-11-29 23:45:48 +1100701 kbuf->image->nr_segments++;
Dave Younga43cac02015-09-09 15:38:51 -0700702 return 0;
703}
704
705/* Calculate and store the digest of segments */
706static int kexec_calculate_store_digests(struct kimage *image)
707{
708 struct crypto_shash *tfm;
709 struct shash_desc *desc;
710 int ret = 0, i, j, zero_buf_sz, sha_region_sz;
711 size_t desc_size, nullsz;
712 char *digest;
713 void *zero_buf;
714 struct kexec_sha_region *sha_regions;
715 struct purgatory_info *pi = &image->purgatory_info;
716
AKASHI Takahirob799a092018-04-13 15:35:45 -0700717 if (!IS_ENABLED(CONFIG_ARCH_HAS_KEXEC_PURGATORY))
718 return 0;
719
Dave Younga43cac02015-09-09 15:38:51 -0700720 zero_buf = __va(page_to_pfn(ZERO_PAGE(0)) << PAGE_SHIFT);
721 zero_buf_sz = PAGE_SIZE;
722
723 tfm = crypto_alloc_shash("sha256", 0, 0);
724 if (IS_ERR(tfm)) {
725 ret = PTR_ERR(tfm);
726 goto out;
727 }
728
729 desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
730 desc = kzalloc(desc_size, GFP_KERNEL);
731 if (!desc) {
732 ret = -ENOMEM;
733 goto out_free_tfm;
734 }
735
736 sha_region_sz = KEXEC_SEGMENT_MAX * sizeof(struct kexec_sha_region);
737 sha_regions = vzalloc(sha_region_sz);
738 if (!sha_regions)
739 goto out_free_desc;
740
741 desc->tfm = tfm;
Dave Younga43cac02015-09-09 15:38:51 -0700742
743 ret = crypto_shash_init(desc);
744 if (ret < 0)
745 goto out_free_sha_regions;
746
747 digest = kzalloc(SHA256_DIGEST_SIZE, GFP_KERNEL);
748 if (!digest) {
749 ret = -ENOMEM;
750 goto out_free_sha_regions;
751 }
752
753 for (j = i = 0; i < image->nr_segments; i++) {
754 struct kexec_segment *ksegment;
755
756 ksegment = &image->segment[i];
757 /*
758 * Skip purgatory as it will be modified once we put digest
759 * info in purgatory.
760 */
761 if (ksegment->kbuf == pi->purgatory_buf)
762 continue;
763
764 ret = crypto_shash_update(desc, ksegment->kbuf,
765 ksegment->bufsz);
766 if (ret)
767 break;
768
769 /*
770 * Assume rest of the buffer is filled with zero and
771 * update digest accordingly.
772 */
773 nullsz = ksegment->memsz - ksegment->bufsz;
774 while (nullsz) {
775 unsigned long bytes = nullsz;
776
777 if (bytes > zero_buf_sz)
778 bytes = zero_buf_sz;
779 ret = crypto_shash_update(desc, zero_buf, bytes);
780 if (ret)
781 break;
782 nullsz -= bytes;
783 }
784
785 if (ret)
786 break;
787
788 sha_regions[j].start = ksegment->mem;
789 sha_regions[j].len = ksegment->memsz;
790 j++;
791 }
792
793 if (!ret) {
794 ret = crypto_shash_final(desc, digest);
795 if (ret)
796 goto out_free_digest;
Thomas Gleixner40c50c12017-03-10 13:17:18 +0100797 ret = kexec_purgatory_get_set_symbol(image, "purgatory_sha_regions",
798 sha_regions, sha_region_sz, 0);
Dave Younga43cac02015-09-09 15:38:51 -0700799 if (ret)
800 goto out_free_digest;
801
Thomas Gleixner40c50c12017-03-10 13:17:18 +0100802 ret = kexec_purgatory_get_set_symbol(image, "purgatory_sha256_digest",
803 digest, SHA256_DIGEST_SIZE, 0);
Dave Younga43cac02015-09-09 15:38:51 -0700804 if (ret)
805 goto out_free_digest;
806 }
807
808out_free_digest:
809 kfree(digest);
810out_free_sha_regions:
811 vfree(sha_regions);
812out_free_desc:
813 kfree(desc);
814out_free_tfm:
815 kfree(tfm);
816out:
817 return ret;
818}
819
AKASHI Takahirob799a092018-04-13 15:35:45 -0700820#ifdef CONFIG_ARCH_HAS_KEXEC_PURGATORY
Philipp Rudo93045702018-04-13 15:36:28 -0700821/*
822 * kexec_purgatory_setup_kbuf - prepare buffer to load purgatory.
823 * @pi: Purgatory to be loaded.
824 * @kbuf: Buffer to setup.
825 *
826 * Allocates the memory needed for the buffer. Caller is responsible to free
827 * the memory after use.
828 *
829 * Return: 0 on success, negative errno on error.
830 */
831static int kexec_purgatory_setup_kbuf(struct purgatory_info *pi,
832 struct kexec_buf *kbuf)
Dave Younga43cac02015-09-09 15:38:51 -0700833{
Philipp Rudo93045702018-04-13 15:36:28 -0700834 const Elf_Shdr *sechdrs;
835 unsigned long bss_align;
836 unsigned long bss_sz;
837 unsigned long align;
838 int i, ret;
Dave Younga43cac02015-09-09 15:38:51 -0700839
Philipp Rudo93045702018-04-13 15:36:28 -0700840 sechdrs = (void *)pi->ehdr + pi->ehdr->e_shoff;
Philipp Rudo3be3f612018-04-13 15:36:43 -0700841 kbuf->buf_align = bss_align = 1;
842 kbuf->bufsz = bss_sz = 0;
Dave Younga43cac02015-09-09 15:38:51 -0700843
Philipp Rudo93045702018-04-13 15:36:28 -0700844 for (i = 0; i < pi->ehdr->e_shnum; i++) {
845 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
846 continue;
847
848 align = sechdrs[i].sh_addralign;
849 if (sechdrs[i].sh_type != SHT_NOBITS) {
850 if (kbuf->buf_align < align)
851 kbuf->buf_align = align;
852 kbuf->bufsz = ALIGN(kbuf->bufsz, align);
853 kbuf->bufsz += sechdrs[i].sh_size;
854 } else {
855 if (bss_align < align)
856 bss_align = align;
857 bss_sz = ALIGN(bss_sz, align);
858 bss_sz += sechdrs[i].sh_size;
859 }
860 }
861 kbuf->bufsz = ALIGN(kbuf->bufsz, bss_align);
862 kbuf->memsz = kbuf->bufsz + bss_sz;
863 if (kbuf->buf_align < bss_align)
864 kbuf->buf_align = bss_align;
865
866 kbuf->buffer = vzalloc(kbuf->bufsz);
867 if (!kbuf->buffer)
868 return -ENOMEM;
869 pi->purgatory_buf = kbuf->buffer;
870
871 ret = kexec_add_buffer(kbuf);
872 if (ret)
873 goto out;
Philipp Rudo93045702018-04-13 15:36:28 -0700874
875 return 0;
876out:
877 vfree(pi->purgatory_buf);
878 pi->purgatory_buf = NULL;
879 return ret;
880}
881
882/*
883 * kexec_purgatory_setup_sechdrs - prepares the pi->sechdrs buffer.
884 * @pi: Purgatory to be loaded.
885 * @kbuf: Buffer prepared to store purgatory.
886 *
887 * Allocates the memory needed for the buffer. Caller is responsible to free
888 * the memory after use.
889 *
890 * Return: 0 on success, negative errno on error.
891 */
892static int kexec_purgatory_setup_sechdrs(struct purgatory_info *pi,
893 struct kexec_buf *kbuf)
894{
Philipp Rudo93045702018-04-13 15:36:28 -0700895 unsigned long bss_addr;
896 unsigned long offset;
Philipp Rudo93045702018-04-13 15:36:28 -0700897 Elf_Shdr *sechdrs;
Philipp Rudo93045702018-04-13 15:36:28 -0700898 int i;
899
Philipp Rudo8da0b722018-04-13 15:36:39 -0700900 /*
901 * The section headers in kexec_purgatory are read-only. In order to
902 * have them modifiable make a temporary copy.
903 */
Kees Cookfad953c2018-06-12 14:27:37 -0700904 sechdrs = vzalloc(array_size(sizeof(Elf_Shdr), pi->ehdr->e_shnum));
Dave Younga43cac02015-09-09 15:38:51 -0700905 if (!sechdrs)
906 return -ENOMEM;
Philipp Rudo93045702018-04-13 15:36:28 -0700907 memcpy(sechdrs, (void *)pi->ehdr + pi->ehdr->e_shoff,
908 pi->ehdr->e_shnum * sizeof(Elf_Shdr));
909 pi->sechdrs = sechdrs;
Dave Younga43cac02015-09-09 15:38:51 -0700910
Philipp Rudo620f6972018-04-13 15:36:35 -0700911 offset = 0;
912 bss_addr = kbuf->mem + kbuf->bufsz;
Philipp Rudof1b1cca2018-04-13 15:36:32 -0700913 kbuf->image->start = pi->ehdr->e_entry;
Dave Younga43cac02015-09-09 15:38:51 -0700914
915 for (i = 0; i < pi->ehdr->e_shnum; i++) {
Philipp Rudo93045702018-04-13 15:36:28 -0700916 unsigned long align;
Philipp Rudo620f6972018-04-13 15:36:35 -0700917 void *src, *dst;
Philipp Rudo93045702018-04-13 15:36:28 -0700918
Dave Younga43cac02015-09-09 15:38:51 -0700919 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
920 continue;
921
922 align = sechdrs[i].sh_addralign;
Philipp Rudof1b1cca2018-04-13 15:36:32 -0700923 if (sechdrs[i].sh_type == SHT_NOBITS) {
Dave Younga43cac02015-09-09 15:38:51 -0700924 bss_addr = ALIGN(bss_addr, align);
925 sechdrs[i].sh_addr = bss_addr;
926 bss_addr += sechdrs[i].sh_size;
Philipp Rudof1b1cca2018-04-13 15:36:32 -0700927 continue;
Dave Younga43cac02015-09-09 15:38:51 -0700928 }
Dave Younga43cac02015-09-09 15:38:51 -0700929
Philipp Rudo620f6972018-04-13 15:36:35 -0700930 offset = ALIGN(offset, align);
Philipp Rudof1b1cca2018-04-13 15:36:32 -0700931 if (sechdrs[i].sh_flags & SHF_EXECINSTR &&
932 pi->ehdr->e_entry >= sechdrs[i].sh_addr &&
933 pi->ehdr->e_entry < (sechdrs[i].sh_addr
934 + sechdrs[i].sh_size)) {
935 kbuf->image->start -= sechdrs[i].sh_addr;
Philipp Rudo620f6972018-04-13 15:36:35 -0700936 kbuf->image->start += kbuf->mem + offset;
Philipp Rudof1b1cca2018-04-13 15:36:32 -0700937 }
938
Philipp Rudo8da0b722018-04-13 15:36:39 -0700939 src = (void *)pi->ehdr + sechdrs[i].sh_offset;
Philipp Rudo620f6972018-04-13 15:36:35 -0700940 dst = pi->purgatory_buf + offset;
941 memcpy(dst, src, sechdrs[i].sh_size);
942
943 sechdrs[i].sh_addr = kbuf->mem + offset;
Philipp Rudo8da0b722018-04-13 15:36:39 -0700944 sechdrs[i].sh_offset = offset;
Philipp Rudo620f6972018-04-13 15:36:35 -0700945 offset += sechdrs[i].sh_size;
Philipp Rudof1b1cca2018-04-13 15:36:32 -0700946 }
Dave Younga43cac02015-09-09 15:38:51 -0700947
Philipp Rudo93045702018-04-13 15:36:28 -0700948 return 0;
Dave Younga43cac02015-09-09 15:38:51 -0700949}
950
951static int kexec_apply_relocations(struct kimage *image)
952{
953 int i, ret;
954 struct purgatory_info *pi = &image->purgatory_info;
Philipp Rudo8aec3952018-04-13 15:36:24 -0700955 const Elf_Shdr *sechdrs;
Dave Younga43cac02015-09-09 15:38:51 -0700956
Philipp Rudo8aec3952018-04-13 15:36:24 -0700957 sechdrs = (void *)pi->ehdr + pi->ehdr->e_shoff;
958
Dave Younga43cac02015-09-09 15:38:51 -0700959 for (i = 0; i < pi->ehdr->e_shnum; i++) {
Philipp Rudo8aec3952018-04-13 15:36:24 -0700960 const Elf_Shdr *relsec;
961 const Elf_Shdr *symtab;
962 Elf_Shdr *section;
Dave Younga43cac02015-09-09 15:38:51 -0700963
Philipp Rudo8aec3952018-04-13 15:36:24 -0700964 relsec = sechdrs + i;
965
966 if (relsec->sh_type != SHT_RELA &&
967 relsec->sh_type != SHT_REL)
Dave Younga43cac02015-09-09 15:38:51 -0700968 continue;
969
970 /*
971 * For section of type SHT_RELA/SHT_REL,
972 * ->sh_link contains section header index of associated
973 * symbol table. And ->sh_info contains section header
974 * index of section to which relocations apply.
975 */
Philipp Rudo8aec3952018-04-13 15:36:24 -0700976 if (relsec->sh_info >= pi->ehdr->e_shnum ||
977 relsec->sh_link >= pi->ehdr->e_shnum)
Dave Younga43cac02015-09-09 15:38:51 -0700978 return -ENOEXEC;
979
Philipp Rudo8aec3952018-04-13 15:36:24 -0700980 section = pi->sechdrs + relsec->sh_info;
981 symtab = sechdrs + relsec->sh_link;
Dave Younga43cac02015-09-09 15:38:51 -0700982
983 if (!(section->sh_flags & SHF_ALLOC))
984 continue;
985
986 /*
987 * symtab->sh_link contain section header index of associated
988 * string table.
989 */
990 if (symtab->sh_link >= pi->ehdr->e_shnum)
991 /* Invalid section number? */
992 continue;
993
994 /*
995 * Respective architecture needs to provide support for applying
996 * relocations of type SHT_RELA/SHT_REL.
997 */
Philipp Rudo8aec3952018-04-13 15:36:24 -0700998 if (relsec->sh_type == SHT_RELA)
999 ret = arch_kexec_apply_relocations_add(pi, section,
1000 relsec, symtab);
1001 else if (relsec->sh_type == SHT_REL)
1002 ret = arch_kexec_apply_relocations(pi, section,
1003 relsec, symtab);
Dave Younga43cac02015-09-09 15:38:51 -07001004 if (ret)
1005 return ret;
1006 }
1007
1008 return 0;
1009}
1010
Philipp Rudo3be3f612018-04-13 15:36:43 -07001011/*
1012 * kexec_load_purgatory - Load and relocate the purgatory object.
1013 * @image: Image to add the purgatory to.
1014 * @kbuf: Memory parameters to use.
1015 *
1016 * Allocates the memory needed for image->purgatory_info.sechdrs and
1017 * image->purgatory_info.purgatory_buf/kbuf->buffer. Caller is responsible
1018 * to free the memory after use.
1019 *
1020 * Return: 0 on success, negative errno on error.
1021 */
1022int kexec_load_purgatory(struct kimage *image, struct kexec_buf *kbuf)
Dave Younga43cac02015-09-09 15:38:51 -07001023{
1024 struct purgatory_info *pi = &image->purgatory_info;
1025 int ret;
1026
1027 if (kexec_purgatory_size <= 0)
1028 return -EINVAL;
1029
Philipp Rudo65c225d2018-04-13 15:36:17 -07001030 pi->ehdr = (const Elf_Ehdr *)kexec_purgatory;
Dave Younga43cac02015-09-09 15:38:51 -07001031
Philipp Rudo3be3f612018-04-13 15:36:43 -07001032 ret = kexec_purgatory_setup_kbuf(pi, kbuf);
Dave Younga43cac02015-09-09 15:38:51 -07001033 if (ret)
1034 return ret;
1035
Philipp Rudo3be3f612018-04-13 15:36:43 -07001036 ret = kexec_purgatory_setup_sechdrs(pi, kbuf);
Philipp Rudo93045702018-04-13 15:36:28 -07001037 if (ret)
1038 goto out_free_kbuf;
1039
Dave Younga43cac02015-09-09 15:38:51 -07001040 ret = kexec_apply_relocations(image);
1041 if (ret)
1042 goto out;
1043
Dave Younga43cac02015-09-09 15:38:51 -07001044 return 0;
1045out:
1046 vfree(pi->sechdrs);
Thiago Jung Bauermann070c43e2016-09-01 16:14:44 -07001047 pi->sechdrs = NULL;
Philipp Rudo93045702018-04-13 15:36:28 -07001048out_free_kbuf:
Dave Younga43cac02015-09-09 15:38:51 -07001049 vfree(pi->purgatory_buf);
Thiago Jung Bauermann070c43e2016-09-01 16:14:44 -07001050 pi->purgatory_buf = NULL;
Dave Younga43cac02015-09-09 15:38:51 -07001051 return ret;
1052}
1053
Philipp Rudo961d9212018-04-13 15:36:21 -07001054/*
1055 * kexec_purgatory_find_symbol - find a symbol in the purgatory
1056 * @pi: Purgatory to search in.
1057 * @name: Name of the symbol.
1058 *
1059 * Return: pointer to symbol in read-only symtab on success, NULL on error.
1060 */
1061static const Elf_Sym *kexec_purgatory_find_symbol(struct purgatory_info *pi,
1062 const char *name)
Dave Younga43cac02015-09-09 15:38:51 -07001063{
Philipp Rudo961d9212018-04-13 15:36:21 -07001064 const Elf_Shdr *sechdrs;
Philipp Rudo65c225d2018-04-13 15:36:17 -07001065 const Elf_Ehdr *ehdr;
Philipp Rudo961d9212018-04-13 15:36:21 -07001066 const Elf_Sym *syms;
Dave Younga43cac02015-09-09 15:38:51 -07001067 const char *strtab;
Philipp Rudo961d9212018-04-13 15:36:21 -07001068 int i, k;
Dave Younga43cac02015-09-09 15:38:51 -07001069
Philipp Rudo961d9212018-04-13 15:36:21 -07001070 if (!pi->ehdr)
Dave Younga43cac02015-09-09 15:38:51 -07001071 return NULL;
1072
Dave Younga43cac02015-09-09 15:38:51 -07001073 ehdr = pi->ehdr;
Philipp Rudo961d9212018-04-13 15:36:21 -07001074 sechdrs = (void *)ehdr + ehdr->e_shoff;
Dave Younga43cac02015-09-09 15:38:51 -07001075
1076 for (i = 0; i < ehdr->e_shnum; i++) {
1077 if (sechdrs[i].sh_type != SHT_SYMTAB)
1078 continue;
1079
1080 if (sechdrs[i].sh_link >= ehdr->e_shnum)
1081 /* Invalid strtab section number */
1082 continue;
Philipp Rudo961d9212018-04-13 15:36:21 -07001083 strtab = (void *)ehdr + sechdrs[sechdrs[i].sh_link].sh_offset;
1084 syms = (void *)ehdr + sechdrs[i].sh_offset;
Dave Younga43cac02015-09-09 15:38:51 -07001085
1086 /* Go through symbols for a match */
1087 for (k = 0; k < sechdrs[i].sh_size/sizeof(Elf_Sym); k++) {
1088 if (ELF_ST_BIND(syms[k].st_info) != STB_GLOBAL)
1089 continue;
1090
1091 if (strcmp(strtab + syms[k].st_name, name) != 0)
1092 continue;
1093
1094 if (syms[k].st_shndx == SHN_UNDEF ||
1095 syms[k].st_shndx >= ehdr->e_shnum) {
1096 pr_debug("Symbol: %s has bad section index %d.\n",
1097 name, syms[k].st_shndx);
1098 return NULL;
1099 }
1100
1101 /* Found the symbol we are looking for */
1102 return &syms[k];
1103 }
1104 }
1105
1106 return NULL;
1107}
1108
1109void *kexec_purgatory_get_symbol_addr(struct kimage *image, const char *name)
1110{
1111 struct purgatory_info *pi = &image->purgatory_info;
Philipp Rudo961d9212018-04-13 15:36:21 -07001112 const Elf_Sym *sym;
Dave Younga43cac02015-09-09 15:38:51 -07001113 Elf_Shdr *sechdr;
1114
1115 sym = kexec_purgatory_find_symbol(pi, name);
1116 if (!sym)
1117 return ERR_PTR(-EINVAL);
1118
1119 sechdr = &pi->sechdrs[sym->st_shndx];
1120
1121 /*
1122 * Returns the address where symbol will finally be loaded after
1123 * kexec_load_segment()
1124 */
1125 return (void *)(sechdr->sh_addr + sym->st_value);
1126}
1127
1128/*
1129 * Get or set value of a symbol. If "get_value" is true, symbol value is
1130 * returned in buf otherwise symbol value is set based on value in buf.
1131 */
1132int kexec_purgatory_get_set_symbol(struct kimage *image, const char *name,
1133 void *buf, unsigned int size, bool get_value)
1134{
Dave Younga43cac02015-09-09 15:38:51 -07001135 struct purgatory_info *pi = &image->purgatory_info;
Philipp Rudo961d9212018-04-13 15:36:21 -07001136 const Elf_Sym *sym;
1137 Elf_Shdr *sec;
Dave Younga43cac02015-09-09 15:38:51 -07001138 char *sym_buf;
1139
1140 sym = kexec_purgatory_find_symbol(pi, name);
1141 if (!sym)
1142 return -EINVAL;
1143
1144 if (sym->st_size != size) {
1145 pr_err("symbol %s size mismatch: expected %lu actual %u\n",
1146 name, (unsigned long)sym->st_size, size);
1147 return -EINVAL;
1148 }
1149
Philipp Rudo961d9212018-04-13 15:36:21 -07001150 sec = pi->sechdrs + sym->st_shndx;
Dave Younga43cac02015-09-09 15:38:51 -07001151
Philipp Rudo961d9212018-04-13 15:36:21 -07001152 if (sec->sh_type == SHT_NOBITS) {
Dave Younga43cac02015-09-09 15:38:51 -07001153 pr_err("symbol %s is in a bss section. Cannot %s\n", name,
1154 get_value ? "get" : "set");
1155 return -EINVAL;
1156 }
1157
Philipp Rudo8da0b722018-04-13 15:36:39 -07001158 sym_buf = (char *)pi->purgatory_buf + sec->sh_offset + sym->st_value;
Dave Younga43cac02015-09-09 15:38:51 -07001159
1160 if (get_value)
1161 memcpy((void *)buf, sym_buf, size);
1162 else
1163 memcpy((void *)sym_buf, buf, size);
1164
1165 return 0;
1166}
AKASHI Takahirob799a092018-04-13 15:35:45 -07001167#endif /* CONFIG_ARCH_HAS_KEXEC_PURGATORY */
AKASHI Takahirobabac4a2018-04-13 15:36:06 -07001168
1169int crash_exclude_mem_range(struct crash_mem *mem,
1170 unsigned long long mstart, unsigned long long mend)
1171{
1172 int i, j;
Lianbo Jianga2e9a95d2020-08-04 12:49:32 +08001173 unsigned long long start, end, p_start, p_end;
AKASHI Takahirobabac4a2018-04-13 15:36:06 -07001174 struct crash_mem_range temp_range = {0, 0};
1175
1176 for (i = 0; i < mem->nr_ranges; i++) {
1177 start = mem->ranges[i].start;
1178 end = mem->ranges[i].end;
Lianbo Jianga2e9a95d2020-08-04 12:49:32 +08001179 p_start = mstart;
1180 p_end = mend;
AKASHI Takahirobabac4a2018-04-13 15:36:06 -07001181
1182 if (mstart > end || mend < start)
1183 continue;
1184
1185 /* Truncate any area outside of range */
1186 if (mstart < start)
Lianbo Jianga2e9a95d2020-08-04 12:49:32 +08001187 p_start = start;
AKASHI Takahirobabac4a2018-04-13 15:36:06 -07001188 if (mend > end)
Lianbo Jianga2e9a95d2020-08-04 12:49:32 +08001189 p_end = end;
AKASHI Takahirobabac4a2018-04-13 15:36:06 -07001190
1191 /* Found completely overlapping range */
Lianbo Jianga2e9a95d2020-08-04 12:49:32 +08001192 if (p_start == start && p_end == end) {
AKASHI Takahirobabac4a2018-04-13 15:36:06 -07001193 mem->ranges[i].start = 0;
1194 mem->ranges[i].end = 0;
1195 if (i < mem->nr_ranges - 1) {
1196 /* Shift rest of the ranges to left */
1197 for (j = i; j < mem->nr_ranges - 1; j++) {
1198 mem->ranges[j].start =
1199 mem->ranges[j+1].start;
1200 mem->ranges[j].end =
1201 mem->ranges[j+1].end;
1202 }
Lianbo Jianga2e9a95d2020-08-04 12:49:32 +08001203
1204 /*
1205 * Continue to check if there are another overlapping ranges
1206 * from the current position because of shifting the above
1207 * mem ranges.
1208 */
1209 i--;
1210 mem->nr_ranges--;
1211 continue;
AKASHI Takahirobabac4a2018-04-13 15:36:06 -07001212 }
1213 mem->nr_ranges--;
1214 return 0;
1215 }
1216
Lianbo Jianga2e9a95d2020-08-04 12:49:32 +08001217 if (p_start > start && p_end < end) {
AKASHI Takahirobabac4a2018-04-13 15:36:06 -07001218 /* Split original range */
Lianbo Jianga2e9a95d2020-08-04 12:49:32 +08001219 mem->ranges[i].end = p_start - 1;
1220 temp_range.start = p_end + 1;
AKASHI Takahirobabac4a2018-04-13 15:36:06 -07001221 temp_range.end = end;
Lianbo Jianga2e9a95d2020-08-04 12:49:32 +08001222 } else if (p_start != start)
1223 mem->ranges[i].end = p_start - 1;
AKASHI Takahirobabac4a2018-04-13 15:36:06 -07001224 else
Lianbo Jianga2e9a95d2020-08-04 12:49:32 +08001225 mem->ranges[i].start = p_end + 1;
AKASHI Takahirobabac4a2018-04-13 15:36:06 -07001226 break;
1227 }
1228
1229 /* If a split happened, add the split to array */
1230 if (!temp_range.end)
1231 return 0;
1232
1233 /* Split happened */
1234 if (i == mem->max_nr_ranges - 1)
1235 return -ENOMEM;
1236
1237 /* Location where new range should go */
1238 j = i + 1;
1239 if (j < mem->nr_ranges) {
1240 /* Move over all ranges one slot towards the end */
1241 for (i = mem->nr_ranges - 1; i >= j; i--)
1242 mem->ranges[i + 1] = mem->ranges[i];
1243 }
1244
1245 mem->ranges[j].start = temp_range.start;
1246 mem->ranges[j].end = temp_range.end;
1247 mem->nr_ranges++;
1248 return 0;
1249}
1250
1251int crash_prepare_elf64_headers(struct crash_mem *mem, int kernel_map,
1252 void **addr, unsigned long *sz)
1253{
1254 Elf64_Ehdr *ehdr;
1255 Elf64_Phdr *phdr;
1256 unsigned long nr_cpus = num_possible_cpus(), nr_phdr, elf_sz;
1257 unsigned char *buf;
1258 unsigned int cpu, i;
1259 unsigned long long notes_addr;
1260 unsigned long mstart, mend;
1261
Lianbo Jiang475f63a2020-08-04 12:49:33 +08001262 /* extra phdr for vmcoreinfo ELF note */
AKASHI Takahirobabac4a2018-04-13 15:36:06 -07001263 nr_phdr = nr_cpus + 1;
1264 nr_phdr += mem->nr_ranges;
1265
1266 /*
1267 * kexec-tools creates an extra PT_LOAD phdr for kernel text mapping
1268 * area (for example, ffffffff80000000 - ffffffffa0000000 on x86_64).
1269 * I think this is required by tools like gdb. So same physical
Lianbo Jiang475f63a2020-08-04 12:49:33 +08001270 * memory will be mapped in two ELF headers. One will contain kernel
AKASHI Takahirobabac4a2018-04-13 15:36:06 -07001271 * text virtual addresses and other will have __va(physical) addresses.
1272 */
1273
1274 nr_phdr++;
1275 elf_sz = sizeof(Elf64_Ehdr) + nr_phdr * sizeof(Elf64_Phdr);
1276 elf_sz = ALIGN(elf_sz, ELF_CORE_HEADER_ALIGN);
1277
1278 buf = vzalloc(elf_sz);
1279 if (!buf)
1280 return -ENOMEM;
1281
1282 ehdr = (Elf64_Ehdr *)buf;
1283 phdr = (Elf64_Phdr *)(ehdr + 1);
1284 memcpy(ehdr->e_ident, ELFMAG, SELFMAG);
1285 ehdr->e_ident[EI_CLASS] = ELFCLASS64;
1286 ehdr->e_ident[EI_DATA] = ELFDATA2LSB;
1287 ehdr->e_ident[EI_VERSION] = EV_CURRENT;
1288 ehdr->e_ident[EI_OSABI] = ELF_OSABI;
1289 memset(ehdr->e_ident + EI_PAD, 0, EI_NIDENT - EI_PAD);
1290 ehdr->e_type = ET_CORE;
1291 ehdr->e_machine = ELF_ARCH;
1292 ehdr->e_version = EV_CURRENT;
1293 ehdr->e_phoff = sizeof(Elf64_Ehdr);
1294 ehdr->e_ehsize = sizeof(Elf64_Ehdr);
1295 ehdr->e_phentsize = sizeof(Elf64_Phdr);
1296
Lianbo Jianga2e9a95d2020-08-04 12:49:32 +08001297 /* Prepare one phdr of type PT_NOTE for each present CPU */
AKASHI Takahirobabac4a2018-04-13 15:36:06 -07001298 for_each_present_cpu(cpu) {
1299 phdr->p_type = PT_NOTE;
1300 notes_addr = per_cpu_ptr_to_phys(per_cpu_ptr(crash_notes, cpu));
1301 phdr->p_offset = phdr->p_paddr = notes_addr;
1302 phdr->p_filesz = phdr->p_memsz = sizeof(note_buf_t);
1303 (ehdr->e_phnum)++;
1304 phdr++;
1305 }
1306
1307 /* Prepare one PT_NOTE header for vmcoreinfo */
1308 phdr->p_type = PT_NOTE;
1309 phdr->p_offset = phdr->p_paddr = paddr_vmcoreinfo_note();
1310 phdr->p_filesz = phdr->p_memsz = VMCOREINFO_NOTE_SIZE;
1311 (ehdr->e_phnum)++;
1312 phdr++;
1313
1314 /* Prepare PT_LOAD type program header for kernel text region */
1315 if (kernel_map) {
1316 phdr->p_type = PT_LOAD;
1317 phdr->p_flags = PF_R|PF_W|PF_X;
Helge Dellerf973cce2019-10-21 22:38:29 +02001318 phdr->p_vaddr = (unsigned long) _text;
AKASHI Takahirobabac4a2018-04-13 15:36:06 -07001319 phdr->p_filesz = phdr->p_memsz = _end - _text;
1320 phdr->p_offset = phdr->p_paddr = __pa_symbol(_text);
1321 ehdr->e_phnum++;
1322 phdr++;
1323 }
1324
1325 /* Go through all the ranges in mem->ranges[] and prepare phdr */
1326 for (i = 0; i < mem->nr_ranges; i++) {
1327 mstart = mem->ranges[i].start;
1328 mend = mem->ranges[i].end;
1329
1330 phdr->p_type = PT_LOAD;
1331 phdr->p_flags = PF_R|PF_W|PF_X;
1332 phdr->p_offset = mstart;
1333
1334 phdr->p_paddr = mstart;
Helge Dellerf973cce2019-10-21 22:38:29 +02001335 phdr->p_vaddr = (unsigned long) __va(mstart);
AKASHI Takahirobabac4a2018-04-13 15:36:06 -07001336 phdr->p_filesz = phdr->p_memsz = mend - mstart + 1;
1337 phdr->p_align = 0;
1338 ehdr->e_phnum++;
Lianbo Jiang475f63a2020-08-04 12:49:33 +08001339 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",
AKASHI Takahirobabac4a2018-04-13 15:36:06 -07001340 phdr, phdr->p_vaddr, phdr->p_paddr, phdr->p_filesz,
1341 ehdr->e_phnum, phdr->p_offset);
Lianbo Jiang475f63a2020-08-04 12:49:33 +08001342 phdr++;
AKASHI Takahirobabac4a2018-04-13 15:36:06 -07001343 }
1344
1345 *addr = buf;
1346 *sz = elf_sz;
1347 return 0;
1348}