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