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