Linux 4.18.10
[linux/fpc-iii.git] / arch / x86 / boot / compressed / kaslr.c
blobb87a7582853dd34a91b8d007a9a146574a4ca674
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * kaslr.c
5 * This contains the routines needed to generate a reasonable level of
6 * entropy to choose a randomized kernel base address offset in support
7 * of Kernel Address Space Layout Randomization (KASLR). Additionally
8 * handles walking the physical memory maps (and tracking memory regions
9 * to avoid) in order to select a physical memory location that can
10 * contain the entire properly aligned running kernel image.
15 * isspace() in linux/ctype.h is expected by next_args() to filter
16 * out "space/lf/tab". While boot/ctype.h conflicts with linux/ctype.h,
17 * since isdigit() is implemented in both of them. Hence disable it
18 * here.
20 #define BOOT_CTYPE_H
23 * _ctype[] in lib/ctype.c is needed by isspace() of linux/ctype.h.
24 * While both lib/ctype.c and lib/cmdline.c will bring EXPORT_SYMBOL
25 * which is meaningless and will cause compiling error in some cases.
26 * So do not include linux/export.h and define EXPORT_SYMBOL(sym)
27 * as empty.
29 #define _LINUX_EXPORT_H
30 #define EXPORT_SYMBOL(sym)
32 #include "misc.h"
33 #include "error.h"
34 #include "../string.h"
36 #include <generated/compile.h>
37 #include <linux/module.h>
38 #include <linux/uts.h>
39 #include <linux/utsname.h>
40 #include <linux/ctype.h>
41 #include <linux/efi.h>
42 #include <generated/utsrelease.h>
43 #include <asm/efi.h>
45 /* Macros used by the included decompressor code below. */
46 #define STATIC
47 #include <linux/decompress/mm.h>
49 #ifdef CONFIG_X86_5LEVEL
50 unsigned int __pgtable_l5_enabled;
51 unsigned int pgdir_shift __ro_after_init = 39;
52 unsigned int ptrs_per_p4d __ro_after_init = 1;
53 #endif
55 extern unsigned long get_cmd_line_ptr(void);
57 /* Used by PAGE_KERN* macros: */
58 pteval_t __default_kernel_pte_mask __read_mostly = ~0;
60 /* Simplified build-specific string for starting entropy. */
61 static const char build_str[] = UTS_RELEASE " (" LINUX_COMPILE_BY "@"
62 LINUX_COMPILE_HOST ") (" LINUX_COMPILER ") " UTS_VERSION;
64 static unsigned long rotate_xor(unsigned long hash, const void *area,
65 size_t size)
67 size_t i;
68 unsigned long *ptr = (unsigned long *)area;
70 for (i = 0; i < size / sizeof(hash); i++) {
71 /* Rotate by odd number of bits and XOR. */
72 hash = (hash << ((sizeof(hash) * 8) - 7)) | (hash >> 7);
73 hash ^= ptr[i];
76 return hash;
79 /* Attempt to create a simple but unpredictable starting entropy. */
80 static unsigned long get_boot_seed(void)
82 unsigned long hash = 0;
84 hash = rotate_xor(hash, build_str, sizeof(build_str));
85 hash = rotate_xor(hash, boot_params, sizeof(*boot_params));
87 return hash;
90 #define KASLR_COMPRESSED_BOOT
91 #include "../../lib/kaslr.c"
93 struct mem_vector {
94 unsigned long long start;
95 unsigned long long size;
98 /* Only supporting at most 4 unusable memmap regions with kaslr */
99 #define MAX_MEMMAP_REGIONS 4
101 static bool memmap_too_large;
104 /* Store memory limit specified by "mem=nn[KMG]" or "memmap=nn[KMG]" */
105 unsigned long long mem_limit = ULLONG_MAX;
108 enum mem_avoid_index {
109 MEM_AVOID_ZO_RANGE = 0,
110 MEM_AVOID_INITRD,
111 MEM_AVOID_CMDLINE,
112 MEM_AVOID_BOOTPARAMS,
113 MEM_AVOID_MEMMAP_BEGIN,
114 MEM_AVOID_MEMMAP_END = MEM_AVOID_MEMMAP_BEGIN + MAX_MEMMAP_REGIONS - 1,
115 MEM_AVOID_MAX,
118 static struct mem_vector mem_avoid[MEM_AVOID_MAX];
120 static bool mem_overlaps(struct mem_vector *one, struct mem_vector *two)
122 /* Item one is entirely before item two. */
123 if (one->start + one->size <= two->start)
124 return false;
125 /* Item one is entirely after item two. */
126 if (one->start >= two->start + two->size)
127 return false;
128 return true;
131 char *skip_spaces(const char *str)
133 while (isspace(*str))
134 ++str;
135 return (char *)str;
137 #include "../../../../lib/ctype.c"
138 #include "../../../../lib/cmdline.c"
140 static int
141 parse_memmap(char *p, unsigned long long *start, unsigned long long *size)
143 char *oldp;
145 if (!p)
146 return -EINVAL;
148 /* We don't care about this option here */
149 if (!strncmp(p, "exactmap", 8))
150 return -EINVAL;
152 oldp = p;
153 *size = memparse(p, &p);
154 if (p == oldp)
155 return -EINVAL;
157 switch (*p) {
158 case '#':
159 case '$':
160 case '!':
161 *start = memparse(p + 1, &p);
162 return 0;
163 case '@':
164 /* memmap=nn@ss specifies usable region, should be skipped */
165 *size = 0;
166 /* Fall through */
167 default:
169 * If w/o offset, only size specified, memmap=nn[KMG] has the
170 * same behaviour as mem=nn[KMG]. It limits the max address
171 * system can use. Region above the limit should be avoided.
173 *start = 0;
174 return 0;
177 return -EINVAL;
180 static void mem_avoid_memmap(char *str)
182 static int i;
184 if (i >= MAX_MEMMAP_REGIONS)
185 return;
187 while (str && (i < MAX_MEMMAP_REGIONS)) {
188 int rc;
189 unsigned long long start, size;
190 char *k = strchr(str, ',');
192 if (k)
193 *k++ = 0;
195 rc = parse_memmap(str, &start, &size);
196 if (rc < 0)
197 break;
198 str = k;
200 if (start == 0) {
201 /* Store the specified memory limit if size > 0 */
202 if (size > 0)
203 mem_limit = size;
205 continue;
208 mem_avoid[MEM_AVOID_MEMMAP_BEGIN + i].start = start;
209 mem_avoid[MEM_AVOID_MEMMAP_BEGIN + i].size = size;
210 i++;
213 /* More than 4 memmaps, fail kaslr */
214 if ((i >= MAX_MEMMAP_REGIONS) && str)
215 memmap_too_large = true;
218 static int handle_mem_memmap(void)
220 char *args = (char *)get_cmd_line_ptr();
221 size_t len = strlen((char *)args);
222 char *tmp_cmdline;
223 char *param, *val;
224 u64 mem_size;
226 if (!strstr(args, "memmap=") && !strstr(args, "mem="))
227 return 0;
229 tmp_cmdline = malloc(len + 1);
230 if (!tmp_cmdline)
231 error("Failed to allocate space for tmp_cmdline");
233 memcpy(tmp_cmdline, args, len);
234 tmp_cmdline[len] = 0;
235 args = tmp_cmdline;
237 /* Chew leading spaces */
238 args = skip_spaces(args);
240 while (*args) {
241 args = next_arg(args, &param, &val);
242 /* Stop at -- */
243 if (!val && strcmp(param, "--") == 0) {
244 warn("Only '--' specified in cmdline");
245 free(tmp_cmdline);
246 return -1;
249 if (!strcmp(param, "memmap")) {
250 mem_avoid_memmap(val);
251 } else if (!strcmp(param, "mem")) {
252 char *p = val;
254 if (!strcmp(p, "nopentium"))
255 continue;
256 mem_size = memparse(p, &p);
257 if (mem_size == 0) {
258 free(tmp_cmdline);
259 return -EINVAL;
261 mem_limit = mem_size;
265 free(tmp_cmdline);
266 return 0;
270 * In theory, KASLR can put the kernel anywhere in the range of [16M, 64T).
271 * The mem_avoid array is used to store the ranges that need to be avoided
272 * when KASLR searches for an appropriate random address. We must avoid any
273 * regions that are unsafe to overlap with during decompression, and other
274 * things like the initrd, cmdline and boot_params. This comment seeks to
275 * explain mem_avoid as clearly as possible since incorrect mem_avoid
276 * memory ranges lead to really hard to debug boot failures.
278 * The initrd, cmdline, and boot_params are trivial to identify for
279 * avoiding. They are MEM_AVOID_INITRD, MEM_AVOID_CMDLINE, and
280 * MEM_AVOID_BOOTPARAMS respectively below.
282 * What is not obvious how to avoid is the range of memory that is used
283 * during decompression (MEM_AVOID_ZO_RANGE below). This range must cover
284 * the compressed kernel (ZO) and its run space, which is used to extract
285 * the uncompressed kernel (VO) and relocs.
287 * ZO's full run size sits against the end of the decompression buffer, so
288 * we can calculate where text, data, bss, etc of ZO are positioned more
289 * easily.
291 * For additional background, the decompression calculations can be found
292 * in header.S, and the memory diagram is based on the one found in misc.c.
294 * The following conditions are already enforced by the image layouts and
295 * associated code:
296 * - input + input_size >= output + output_size
297 * - kernel_total_size <= init_size
298 * - kernel_total_size <= output_size (see Note below)
299 * - output + init_size >= output + output_size
301 * (Note that kernel_total_size and output_size have no fundamental
302 * relationship, but output_size is passed to choose_random_location
303 * as a maximum of the two. The diagram is showing a case where
304 * kernel_total_size is larger than output_size, but this case is
305 * handled by bumping output_size.)
307 * The above conditions can be illustrated by a diagram:
309 * 0 output input input+input_size output+init_size
310 * | | | | |
311 * | | | | |
312 * |-----|--------|--------|--------------|-----------|--|-------------|
313 * | | |
314 * | | |
315 * output+init_size-ZO_INIT_SIZE output+output_size output+kernel_total_size
317 * [output, output+init_size) is the entire memory range used for
318 * extracting the compressed image.
320 * [output, output+kernel_total_size) is the range needed for the
321 * uncompressed kernel (VO) and its run size (bss, brk, etc).
323 * [output, output+output_size) is VO plus relocs (i.e. the entire
324 * uncompressed payload contained by ZO). This is the area of the buffer
325 * written to during decompression.
327 * [output+init_size-ZO_INIT_SIZE, output+init_size) is the worst-case
328 * range of the copied ZO and decompression code. (i.e. the range
329 * covered backwards of size ZO_INIT_SIZE, starting from output+init_size.)
331 * [input, input+input_size) is the original copied compressed image (ZO)
332 * (i.e. it does not include its run size). This range must be avoided
333 * because it contains the data used for decompression.
335 * [input+input_size, output+init_size) is [_text, _end) for ZO. This
336 * range includes ZO's heap and stack, and must be avoided since it
337 * performs the decompression.
339 * Since the above two ranges need to be avoided and they are adjacent,
340 * they can be merged, resulting in: [input, output+init_size) which
341 * becomes the MEM_AVOID_ZO_RANGE below.
343 static void mem_avoid_init(unsigned long input, unsigned long input_size,
344 unsigned long output)
346 unsigned long init_size = boot_params->hdr.init_size;
347 u64 initrd_start, initrd_size;
348 u64 cmd_line, cmd_line_size;
349 char *ptr;
352 * Avoid the region that is unsafe to overlap during
353 * decompression.
355 mem_avoid[MEM_AVOID_ZO_RANGE].start = input;
356 mem_avoid[MEM_AVOID_ZO_RANGE].size = (output + init_size) - input;
357 add_identity_map(mem_avoid[MEM_AVOID_ZO_RANGE].start,
358 mem_avoid[MEM_AVOID_ZO_RANGE].size);
360 /* Avoid initrd. */
361 initrd_start = (u64)boot_params->ext_ramdisk_image << 32;
362 initrd_start |= boot_params->hdr.ramdisk_image;
363 initrd_size = (u64)boot_params->ext_ramdisk_size << 32;
364 initrd_size |= boot_params->hdr.ramdisk_size;
365 mem_avoid[MEM_AVOID_INITRD].start = initrd_start;
366 mem_avoid[MEM_AVOID_INITRD].size = initrd_size;
367 /* No need to set mapping for initrd, it will be handled in VO. */
369 /* Avoid kernel command line. */
370 cmd_line = (u64)boot_params->ext_cmd_line_ptr << 32;
371 cmd_line |= boot_params->hdr.cmd_line_ptr;
372 /* Calculate size of cmd_line. */
373 ptr = (char *)(unsigned long)cmd_line;
374 for (cmd_line_size = 0; ptr[cmd_line_size++];)
376 mem_avoid[MEM_AVOID_CMDLINE].start = cmd_line;
377 mem_avoid[MEM_AVOID_CMDLINE].size = cmd_line_size;
378 add_identity_map(mem_avoid[MEM_AVOID_CMDLINE].start,
379 mem_avoid[MEM_AVOID_CMDLINE].size);
381 /* Avoid boot parameters. */
382 mem_avoid[MEM_AVOID_BOOTPARAMS].start = (unsigned long)boot_params;
383 mem_avoid[MEM_AVOID_BOOTPARAMS].size = sizeof(*boot_params);
384 add_identity_map(mem_avoid[MEM_AVOID_BOOTPARAMS].start,
385 mem_avoid[MEM_AVOID_BOOTPARAMS].size);
387 /* We don't need to set a mapping for setup_data. */
389 /* Mark the memmap regions we need to avoid */
390 handle_mem_memmap();
392 #ifdef CONFIG_X86_VERBOSE_BOOTUP
393 /* Make sure video RAM can be used. */
394 add_identity_map(0, PMD_SIZE);
395 #endif
399 * Does this memory vector overlap a known avoided area? If so, record the
400 * overlap region with the lowest address.
402 static bool mem_avoid_overlap(struct mem_vector *img,
403 struct mem_vector *overlap)
405 int i;
406 struct setup_data *ptr;
407 unsigned long earliest = img->start + img->size;
408 bool is_overlapping = false;
410 for (i = 0; i < MEM_AVOID_MAX; i++) {
411 if (mem_overlaps(img, &mem_avoid[i]) &&
412 mem_avoid[i].start < earliest) {
413 *overlap = mem_avoid[i];
414 earliest = overlap->start;
415 is_overlapping = true;
419 /* Avoid all entries in the setup_data linked list. */
420 ptr = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
421 while (ptr) {
422 struct mem_vector avoid;
424 avoid.start = (unsigned long)ptr;
425 avoid.size = sizeof(*ptr) + ptr->len;
427 if (mem_overlaps(img, &avoid) && (avoid.start < earliest)) {
428 *overlap = avoid;
429 earliest = overlap->start;
430 is_overlapping = true;
433 ptr = (struct setup_data *)(unsigned long)ptr->next;
436 return is_overlapping;
439 struct slot_area {
440 unsigned long addr;
441 int num;
444 #define MAX_SLOT_AREA 100
446 static struct slot_area slot_areas[MAX_SLOT_AREA];
448 static unsigned long slot_max;
450 static unsigned long slot_area_index;
452 static void store_slot_info(struct mem_vector *region, unsigned long image_size)
454 struct slot_area slot_area;
456 if (slot_area_index == MAX_SLOT_AREA)
457 return;
459 slot_area.addr = region->start;
460 slot_area.num = (region->size - image_size) /
461 CONFIG_PHYSICAL_ALIGN + 1;
463 if (slot_area.num > 0) {
464 slot_areas[slot_area_index++] = slot_area;
465 slot_max += slot_area.num;
469 static unsigned long slots_fetch_random(void)
471 unsigned long slot;
472 int i;
474 /* Handle case of no slots stored. */
475 if (slot_max == 0)
476 return 0;
478 slot = kaslr_get_random_long("Physical") % slot_max;
480 for (i = 0; i < slot_area_index; i++) {
481 if (slot >= slot_areas[i].num) {
482 slot -= slot_areas[i].num;
483 continue;
485 return slot_areas[i].addr + slot * CONFIG_PHYSICAL_ALIGN;
488 if (i == slot_area_index)
489 debug_putstr("slots_fetch_random() failed!?\n");
490 return 0;
493 static void process_mem_region(struct mem_vector *entry,
494 unsigned long minimum,
495 unsigned long image_size)
497 struct mem_vector region, overlap;
498 struct slot_area slot_area;
499 unsigned long start_orig, end;
500 struct mem_vector cur_entry;
502 /* On 32-bit, ignore entries entirely above our maximum. */
503 if (IS_ENABLED(CONFIG_X86_32) && entry->start >= KERNEL_IMAGE_SIZE)
504 return;
506 /* Ignore entries entirely below our minimum. */
507 if (entry->start + entry->size < minimum)
508 return;
510 /* Ignore entries above memory limit */
511 end = min(entry->size + entry->start, mem_limit);
512 if (entry->start >= end)
513 return;
514 cur_entry.start = entry->start;
515 cur_entry.size = end - entry->start;
517 region.start = cur_entry.start;
518 region.size = cur_entry.size;
520 /* Give up if slot area array is full. */
521 while (slot_area_index < MAX_SLOT_AREA) {
522 start_orig = region.start;
524 /* Potentially raise address to minimum location. */
525 if (region.start < minimum)
526 region.start = minimum;
528 /* Potentially raise address to meet alignment needs. */
529 region.start = ALIGN(region.start, CONFIG_PHYSICAL_ALIGN);
531 /* Did we raise the address above the passed in memory entry? */
532 if (region.start > cur_entry.start + cur_entry.size)
533 return;
535 /* Reduce size by any delta from the original address. */
536 region.size -= region.start - start_orig;
538 /* On 32-bit, reduce region size to fit within max size. */
539 if (IS_ENABLED(CONFIG_X86_32) &&
540 region.start + region.size > KERNEL_IMAGE_SIZE)
541 region.size = KERNEL_IMAGE_SIZE - region.start;
543 /* Return if region can't contain decompressed kernel */
544 if (region.size < image_size)
545 return;
547 /* If nothing overlaps, store the region and return. */
548 if (!mem_avoid_overlap(&region, &overlap)) {
549 store_slot_info(&region, image_size);
550 return;
553 /* Store beginning of region if holds at least image_size. */
554 if (overlap.start > region.start + image_size) {
555 struct mem_vector beginning;
557 beginning.start = region.start;
558 beginning.size = overlap.start - region.start;
559 store_slot_info(&beginning, image_size);
562 /* Return if overlap extends to or past end of region. */
563 if (overlap.start + overlap.size >= region.start + region.size)
564 return;
566 /* Clip off the overlapping region and start over. */
567 region.size -= overlap.start - region.start + overlap.size;
568 region.start = overlap.start + overlap.size;
572 #ifdef CONFIG_EFI
574 * Returns true if mirror region found (and must have been processed
575 * for slots adding)
577 static bool
578 process_efi_entries(unsigned long minimum, unsigned long image_size)
580 struct efi_info *e = &boot_params->efi_info;
581 bool efi_mirror_found = false;
582 struct mem_vector region;
583 efi_memory_desc_t *md;
584 unsigned long pmap;
585 char *signature;
586 u32 nr_desc;
587 int i;
589 signature = (char *)&e->efi_loader_signature;
590 if (strncmp(signature, EFI32_LOADER_SIGNATURE, 4) &&
591 strncmp(signature, EFI64_LOADER_SIGNATURE, 4))
592 return false;
594 #ifdef CONFIG_X86_32
595 /* Can't handle data above 4GB at this time */
596 if (e->efi_memmap_hi) {
597 warn("EFI memmap is above 4GB, can't be handled now on x86_32. EFI should be disabled.\n");
598 return false;
600 pmap = e->efi_memmap;
601 #else
602 pmap = (e->efi_memmap | ((__u64)e->efi_memmap_hi << 32));
603 #endif
605 nr_desc = e->efi_memmap_size / e->efi_memdesc_size;
606 for (i = 0; i < nr_desc; i++) {
607 md = efi_early_memdesc_ptr(pmap, e->efi_memdesc_size, i);
608 if (md->attribute & EFI_MEMORY_MORE_RELIABLE) {
609 efi_mirror_found = true;
610 break;
614 for (i = 0; i < nr_desc; i++) {
615 md = efi_early_memdesc_ptr(pmap, e->efi_memdesc_size, i);
618 * Here we are more conservative in picking free memory than
619 * the EFI spec allows:
621 * According to the spec, EFI_BOOT_SERVICES_{CODE|DATA} are also
622 * free memory and thus available to place the kernel image into,
623 * but in practice there's firmware where using that memory leads
624 * to crashes.
626 * Only EFI_CONVENTIONAL_MEMORY is guaranteed to be free.
628 if (md->type != EFI_CONVENTIONAL_MEMORY)
629 continue;
631 if (efi_mirror_found &&
632 !(md->attribute & EFI_MEMORY_MORE_RELIABLE))
633 continue;
635 region.start = md->phys_addr;
636 region.size = md->num_pages << EFI_PAGE_SHIFT;
637 process_mem_region(&region, minimum, image_size);
638 if (slot_area_index == MAX_SLOT_AREA) {
639 debug_putstr("Aborted EFI scan (slot_areas full)!\n");
640 break;
643 return true;
645 #else
646 static inline bool
647 process_efi_entries(unsigned long minimum, unsigned long image_size)
649 return false;
651 #endif
653 static void process_e820_entries(unsigned long minimum,
654 unsigned long image_size)
656 int i;
657 struct mem_vector region;
658 struct boot_e820_entry *entry;
660 /* Verify potential e820 positions, appending to slots list. */
661 for (i = 0; i < boot_params->e820_entries; i++) {
662 entry = &boot_params->e820_table[i];
663 /* Skip non-RAM entries. */
664 if (entry->type != E820_TYPE_RAM)
665 continue;
666 region.start = entry->addr;
667 region.size = entry->size;
668 process_mem_region(&region, minimum, image_size);
669 if (slot_area_index == MAX_SLOT_AREA) {
670 debug_putstr("Aborted e820 scan (slot_areas full)!\n");
671 break;
676 static unsigned long find_random_phys_addr(unsigned long minimum,
677 unsigned long image_size)
679 /* Check if we had too many memmaps. */
680 if (memmap_too_large) {
681 debug_putstr("Aborted memory entries scan (more than 4 memmap= args)!\n");
682 return 0;
685 /* Make sure minimum is aligned. */
686 minimum = ALIGN(minimum, CONFIG_PHYSICAL_ALIGN);
688 if (process_efi_entries(minimum, image_size))
689 return slots_fetch_random();
691 process_e820_entries(minimum, image_size);
692 return slots_fetch_random();
695 static unsigned long find_random_virt_addr(unsigned long minimum,
696 unsigned long image_size)
698 unsigned long slots, random_addr;
700 /* Make sure minimum is aligned. */
701 minimum = ALIGN(minimum, CONFIG_PHYSICAL_ALIGN);
702 /* Align image_size for easy slot calculations. */
703 image_size = ALIGN(image_size, CONFIG_PHYSICAL_ALIGN);
706 * There are how many CONFIG_PHYSICAL_ALIGN-sized slots
707 * that can hold image_size within the range of minimum to
708 * KERNEL_IMAGE_SIZE?
710 slots = (KERNEL_IMAGE_SIZE - minimum - image_size) /
711 CONFIG_PHYSICAL_ALIGN + 1;
713 random_addr = kaslr_get_random_long("Virtual") % slots;
715 return random_addr * CONFIG_PHYSICAL_ALIGN + minimum;
719 * Since this function examines addresses much more numerically,
720 * it takes the input and output pointers as 'unsigned long'.
722 void choose_random_location(unsigned long input,
723 unsigned long input_size,
724 unsigned long *output,
725 unsigned long output_size,
726 unsigned long *virt_addr)
728 unsigned long random_addr, min_addr;
730 if (cmdline_find_option_bool("nokaslr")) {
731 warn("KASLR disabled: 'nokaslr' on cmdline.");
732 return;
735 #ifdef CONFIG_X86_5LEVEL
736 if (__read_cr4() & X86_CR4_LA57) {
737 __pgtable_l5_enabled = 1;
738 pgdir_shift = 48;
739 ptrs_per_p4d = 512;
741 #endif
743 boot_params->hdr.loadflags |= KASLR_FLAG;
745 /* Prepare to add new identity pagetables on demand. */
746 initialize_identity_maps();
748 /* Record the various known unsafe memory ranges. */
749 mem_avoid_init(input, input_size, *output);
752 * Low end of the randomization range should be the
753 * smaller of 512M or the initial kernel image
754 * location:
756 min_addr = min(*output, 512UL << 20);
758 /* Walk available memory entries to find a random address. */
759 random_addr = find_random_phys_addr(min_addr, output_size);
760 if (!random_addr) {
761 warn("Physical KASLR disabled: no suitable memory region!");
762 } else {
763 /* Update the new physical address location. */
764 if (*output != random_addr) {
765 add_identity_map(random_addr, output_size);
766 *output = random_addr;
770 * This loads the identity mapping page table.
771 * This should only be done if a new physical address
772 * is found for the kernel, otherwise we should keep
773 * the old page table to make it be like the "nokaslr"
774 * case.
776 finalize_identity_maps();
780 /* Pick random virtual address starting from LOAD_PHYSICAL_ADDR. */
781 if (IS_ENABLED(CONFIG_X86_64))
782 random_addr = find_random_virt_addr(LOAD_PHYSICAL_ADDR, output_size);
783 *virt_addr = random_addr;