Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * kaslr.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * This contains the routines needed to generate a reasonable level of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * entropy to choose a randomized kernel base address offset in support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * of Kernel Address Space Layout Randomization (KASLR). Additionally
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * handles walking the physical memory maps (and tracking memory regions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * to avoid) in order to select a physical memory location that can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * contain the entire properly aligned running kernel image.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  * isspace() in linux/ctype.h is expected by next_args() to filter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  * out "space/lf/tab". While boot/ctype.h conflicts with linux/ctype.h,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  * since isdigit() is implemented in both of them. Hence disable it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  * here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #define BOOT_CTYPE_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include "misc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include "error.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include "../string.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <generated/compile.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include <linux/uts.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include <linux/utsname.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #include <linux/ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #include <linux/efi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #include <generated/utsrelease.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #include <asm/efi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) /* Macros used by the included decompressor code below. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define STATIC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #include <linux/decompress/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #define _SETUP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #include <asm/setup.h>	/* For COMMAND_LINE_SIZE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #undef _SETUP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) extern unsigned long get_cmd_line_ptr(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) /* Simplified build-specific string for starting entropy. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) static const char build_str[] = UTS_RELEASE " (" LINUX_COMPILE_BY "@"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		LINUX_COMPILE_HOST ") (" LINUX_COMPILER ") " UTS_VERSION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) static unsigned long rotate_xor(unsigned long hash, const void *area,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 				size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	size_t i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	unsigned long *ptr = (unsigned long *)area;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	for (i = 0; i < size / sizeof(hash); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 		/* Rotate by odd number of bits and XOR. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		hash = (hash << ((sizeof(hash) * 8) - 7)) | (hash >> 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		hash ^= ptr[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	return hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) /* Attempt to create a simple but unpredictable starting entropy. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) static unsigned long get_boot_seed(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	unsigned long hash = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	hash = rotate_xor(hash, build_str, sizeof(build_str));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	hash = rotate_xor(hash, boot_params, sizeof(*boot_params));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	return hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) #define KASLR_COMPRESSED_BOOT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) #include "../../lib/kaslr.c"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) /* Only supporting at most 4 unusable memmap regions with kaslr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) #define MAX_MEMMAP_REGIONS	4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) static bool memmap_too_large;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86)  * Store memory limit: MAXMEM on 64-bit and KERNEL_IMAGE_SIZE on 32-bit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87)  * It may be reduced by "mem=nn[KMG]" or "memmap=nn[KMG]" command line options.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) static u64 mem_limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) /* Number of immovable memory regions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) static int num_immovable_mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) enum mem_avoid_index {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	MEM_AVOID_ZO_RANGE = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	MEM_AVOID_INITRD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	MEM_AVOID_CMDLINE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	MEM_AVOID_BOOTPARAMS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	MEM_AVOID_MEMMAP_BEGIN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	MEM_AVOID_MEMMAP_END = MEM_AVOID_MEMMAP_BEGIN + MAX_MEMMAP_REGIONS - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	MEM_AVOID_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) static struct mem_vector mem_avoid[MEM_AVOID_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) static bool mem_overlaps(struct mem_vector *one, struct mem_vector *two)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	/* Item one is entirely before item two. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	if (one->start + one->size <= two->start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	/* Item one is entirely after item two. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	if (one->start >= two->start + two->size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) char *skip_spaces(const char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	while (isspace(*str))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		++str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	return (char *)str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) #include "../../../../lib/ctype.c"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) #include "../../../../lib/cmdline.c"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) enum parse_mode {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	PARSE_MEMMAP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	PARSE_EFI,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) parse_memmap(char *p, u64 *start, u64 *size, enum parse_mode mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	char *oldp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	/* We don't care about this option here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	if (!strncmp(p, "exactmap", 8))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	oldp = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	*size = memparse(p, &p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	if (p == oldp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	switch (*p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	case '#':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	case '$':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	case '!':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		*start = memparse(p + 1, &p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	case '@':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		if (mode == PARSE_MEMMAP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 			 * memmap=nn@ss specifies usable region, should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 			 * be skipped
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 			*size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 			u64 flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 			 * efi_fake_mem=nn@ss:attr the attr specifies
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 			 * flags that might imply a soft-reservation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 			*start = memparse(p + 1, &p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 			if (p && *p == ':') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 				p++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 				if (kstrtoull(p, 0, &flags) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 					*size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 				else if (flags & EFI_MEMORY_SP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 					return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 			*size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		 * If w/o offset, only size specified, memmap=nn[KMG] has the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		 * same behaviour as mem=nn[KMG]. It limits the max address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		 * system can use. Region above the limit should be avoided.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		*start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) static void mem_avoid_memmap(enum parse_mode mode, char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	static int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	if (i >= MAX_MEMMAP_REGIONS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	while (str && (i < MAX_MEMMAP_REGIONS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		u64 start, size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		char *k = strchr(str, ',');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		if (k)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 			*k++ = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		rc = parse_memmap(str, &start, &size, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		if (rc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 		str = k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		if (start == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 			/* Store the specified memory limit if size > 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 			if (size > 0 && size < mem_limit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 				mem_limit = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		mem_avoid[MEM_AVOID_MEMMAP_BEGIN + i].start = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		mem_avoid[MEM_AVOID_MEMMAP_BEGIN + i].size = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	/* More than 4 memmaps, fail kaslr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	if ((i >= MAX_MEMMAP_REGIONS) && str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		memmap_too_large = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) /* Store the number of 1GB huge pages which users specified: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) static unsigned long max_gb_huge_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) static void parse_gb_huge_pages(char *param, char *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	static bool gbpage_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	char *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	if (!strcmp(param, "hugepagesz")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		p = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		if (memparse(p, &p) != PUD_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 			gbpage_sz = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		if (gbpage_sz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 			warn("Repeatedly set hugeTLB page size of 1G!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		gbpage_sz = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	if (!strcmp(param, "hugepages") && gbpage_sz) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		p = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		max_gb_huge_pages = simple_strtoull(p, &p, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) static void handle_mem_options(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	char *args = (char *)get_cmd_line_ptr();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	size_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	char *tmp_cmdline;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	char *param, *val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	u64 mem_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	if (!args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	len = strnlen(args, COMMAND_LINE_SIZE-1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	tmp_cmdline = malloc(len + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	if (!tmp_cmdline)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		error("Failed to allocate space for tmp_cmdline");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	memcpy(tmp_cmdline, args, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	tmp_cmdline[len] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	args = tmp_cmdline;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	/* Chew leading spaces */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	args = skip_spaces(args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	while (*args) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		args = next_arg(args, &param, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		/* Stop at -- */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		if (!val && strcmp(param, "--") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		if (!strcmp(param, "memmap")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 			mem_avoid_memmap(PARSE_MEMMAP, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		} else if (IS_ENABLED(CONFIG_X86_64) && strstr(param, "hugepages")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 			parse_gb_huge_pages(param, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		} else if (!strcmp(param, "mem")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 			char *p = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 			if (!strcmp(p, "nopentium"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 			mem_size = memparse(p, &p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 			if (mem_size == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 			if (mem_size < mem_limit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 				mem_limit = mem_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		} else if (!strcmp(param, "efi_fake_mem")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 			mem_avoid_memmap(PARSE_EFI, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	free(tmp_cmdline);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)  * In theory, KASLR can put the kernel anywhere in the range of [16M, MAXMEM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)  * on 64-bit, and [16M, KERNEL_IMAGE_SIZE) on 32-bit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)  * The mem_avoid array is used to store the ranges that need to be avoided
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)  * when KASLR searches for an appropriate random address. We must avoid any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)  * regions that are unsafe to overlap with during decompression, and other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)  * things like the initrd, cmdline and boot_params. This comment seeks to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)  * explain mem_avoid as clearly as possible since incorrect mem_avoid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)  * memory ranges lead to really hard to debug boot failures.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)  * The initrd, cmdline, and boot_params are trivial to identify for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)  * avoiding. They are MEM_AVOID_INITRD, MEM_AVOID_CMDLINE, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)  * MEM_AVOID_BOOTPARAMS respectively below.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)  * What is not obvious how to avoid is the range of memory that is used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)  * during decompression (MEM_AVOID_ZO_RANGE below). This range must cover
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)  * the compressed kernel (ZO) and its run space, which is used to extract
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)  * the uncompressed kernel (VO) and relocs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)  * ZO's full run size sits against the end of the decompression buffer, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)  * we can calculate where text, data, bss, etc of ZO are positioned more
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)  * easily.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)  * For additional background, the decompression calculations can be found
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)  * in header.S, and the memory diagram is based on the one found in misc.c.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)  * The following conditions are already enforced by the image layouts and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)  * associated code:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)  *  - input + input_size >= output + output_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)  *  - kernel_total_size <= init_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)  *  - kernel_total_size <= output_size (see Note below)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)  *  - output + init_size >= output + output_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)  * (Note that kernel_total_size and output_size have no fundamental
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)  * relationship, but output_size is passed to choose_random_location
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)  * as a maximum of the two. The diagram is showing a case where
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)  * kernel_total_size is larger than output_size, but this case is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)  * handled by bumping output_size.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)  * The above conditions can be illustrated by a diagram:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)  * 0   output            input            input+input_size    output+init_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)  * |     |                 |                             |             |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)  * |     |                 |                             |             |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)  * |-----|--------|--------|--------------|-----------|--|-------------|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)  *                |                       |           |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)  *                |                       |           |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)  * output+init_size-ZO_INIT_SIZE  output+output_size  output+kernel_total_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)  * [output, output+init_size) is the entire memory range used for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)  * extracting the compressed image.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)  * [output, output+kernel_total_size) is the range needed for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)  * uncompressed kernel (VO) and its run size (bss, brk, etc).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)  * [output, output+output_size) is VO plus relocs (i.e. the entire
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)  * uncompressed payload contained by ZO). This is the area of the buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)  * written to during decompression.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)  * [output+init_size-ZO_INIT_SIZE, output+init_size) is the worst-case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)  * range of the copied ZO and decompression code. (i.e. the range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)  * covered backwards of size ZO_INIT_SIZE, starting from output+init_size.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)  * [input, input+input_size) is the original copied compressed image (ZO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)  * (i.e. it does not include its run size). This range must be avoided
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)  * because it contains the data used for decompression.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)  * [input+input_size, output+init_size) is [_text, _end) for ZO. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)  * range includes ZO's heap and stack, and must be avoided since it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)  * performs the decompression.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)  * Since the above two ranges need to be avoided and they are adjacent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)  * they can be merged, resulting in: [input, output+init_size) which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)  * becomes the MEM_AVOID_ZO_RANGE below.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) static void mem_avoid_init(unsigned long input, unsigned long input_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 			   unsigned long output)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	unsigned long init_size = boot_params->hdr.init_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	u64 initrd_start, initrd_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	unsigned long cmd_line, cmd_line_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	 * Avoid the region that is unsafe to overlap during
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	 * decompression.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	mem_avoid[MEM_AVOID_ZO_RANGE].start = input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	mem_avoid[MEM_AVOID_ZO_RANGE].size = (output + init_size) - input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	/* Avoid initrd. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	initrd_start  = (u64)boot_params->ext_ramdisk_image << 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	initrd_start |= boot_params->hdr.ramdisk_image;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	initrd_size  = (u64)boot_params->ext_ramdisk_size << 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	initrd_size |= boot_params->hdr.ramdisk_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	mem_avoid[MEM_AVOID_INITRD].start = initrd_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	mem_avoid[MEM_AVOID_INITRD].size = initrd_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	/* No need to set mapping for initrd, it will be handled in VO. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	/* Avoid kernel command line. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	cmd_line = get_cmd_line_ptr();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	/* Calculate size of cmd_line. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	if (cmd_line) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 		cmd_line_size = strnlen((char *)cmd_line, COMMAND_LINE_SIZE-1) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 		mem_avoid[MEM_AVOID_CMDLINE].start = cmd_line;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 		mem_avoid[MEM_AVOID_CMDLINE].size = cmd_line_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	/* Avoid boot parameters. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	mem_avoid[MEM_AVOID_BOOTPARAMS].start = (unsigned long)boot_params;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	mem_avoid[MEM_AVOID_BOOTPARAMS].size = sizeof(*boot_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	/* We don't need to set a mapping for setup_data. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	/* Mark the memmap regions we need to avoid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	handle_mem_options();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	/* Enumerate the immovable memory regions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	num_immovable_mem = count_immovable_mem_regions();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433)  * Does this memory vector overlap a known avoided area? If so, record the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)  * overlap region with the lowest address.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) static bool mem_avoid_overlap(struct mem_vector *img,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 			      struct mem_vector *overlap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	struct setup_data *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	u64 earliest = img->start + img->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	bool is_overlapping = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	for (i = 0; i < MEM_AVOID_MAX; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 		if (mem_overlaps(img, &mem_avoid[i]) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 		    mem_avoid[i].start < earliest) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 			*overlap = mem_avoid[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 			earliest = overlap->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 			is_overlapping = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	/* Avoid all entries in the setup_data linked list. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	ptr = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	while (ptr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 		struct mem_vector avoid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 		avoid.start = (unsigned long)ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 		avoid.size = sizeof(*ptr) + ptr->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 		if (mem_overlaps(img, &avoid) && (avoid.start < earliest)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 			*overlap = avoid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 			earliest = overlap->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 			is_overlapping = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 		if (ptr->type == SETUP_INDIRECT &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 		    ((struct setup_indirect *)ptr->data)->type != SETUP_INDIRECT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 			avoid.start = ((struct setup_indirect *)ptr->data)->addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 			avoid.size = ((struct setup_indirect *)ptr->data)->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 			if (mem_overlaps(img, &avoid) && (avoid.start < earliest)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 				*overlap = avoid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 				earliest = overlap->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 				is_overlapping = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 		ptr = (struct setup_data *)(unsigned long)ptr->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	return is_overlapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) struct slot_area {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	u64 addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	unsigned long num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) #define MAX_SLOT_AREA 100
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) static struct slot_area slot_areas[MAX_SLOT_AREA];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) static unsigned int slot_area_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) static unsigned long slot_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) static void store_slot_info(struct mem_vector *region, unsigned long image_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	struct slot_area slot_area;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	if (slot_area_index == MAX_SLOT_AREA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	slot_area.addr = region->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	slot_area.num = 1 + (region->size - image_size) / CONFIG_PHYSICAL_ALIGN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	slot_areas[slot_area_index++] = slot_area;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	slot_max += slot_area.num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511)  * Skip as many 1GB huge pages as possible in the passed region
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512)  * according to the number which users specified:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) process_gb_huge_pages(struct mem_vector *region, unsigned long image_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	u64 pud_start, pud_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	unsigned long gb_huge_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	struct mem_vector tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	if (!IS_ENABLED(CONFIG_X86_64) || !max_gb_huge_pages) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 		store_slot_info(region, image_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	/* Are there any 1GB pages in the region? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	pud_start = ALIGN(region->start, PUD_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	pud_end = ALIGN_DOWN(region->start + region->size, PUD_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	/* No good 1GB huge pages found: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	if (pud_start >= pud_end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 		store_slot_info(region, image_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	/* Check if the head part of the region is usable. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	if (pud_start >= region->start + image_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 		tmp.start = region->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 		tmp.size = pud_start - region->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 		store_slot_info(&tmp, image_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 	/* Skip the good 1GB pages. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	gb_huge_pages = (pud_end - pud_start) >> PUD_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 	if (gb_huge_pages > max_gb_huge_pages) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 		pud_end = pud_start + (max_gb_huge_pages << PUD_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 		max_gb_huge_pages = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 		max_gb_huge_pages -= gb_huge_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 	/* Check if the tail part of the region is usable. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 	if (region->start + region->size >= pud_end + image_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 		tmp.start = pud_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 		tmp.size = region->start + region->size - pud_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 		store_slot_info(&tmp, image_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) static u64 slots_fetch_random(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	unsigned long slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	/* Handle case of no slots stored. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 	if (slot_max == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 	slot = kaslr_get_random_long("Physical") % slot_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 	for (i = 0; i < slot_area_index; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 		if (slot >= slot_areas[i].num) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 			slot -= slot_areas[i].num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 		return slot_areas[i].addr + ((u64)slot * CONFIG_PHYSICAL_ALIGN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 	if (i == slot_area_index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 		debug_putstr("slots_fetch_random() failed!?\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) static void __process_mem_region(struct mem_vector *entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 				 unsigned long minimum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 				 unsigned long image_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 	struct mem_vector region, overlap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 	u64 region_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 	/* Enforce minimum and memory limit. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 	region.start = max_t(u64, entry->start, minimum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 	region_end = min(entry->start + entry->size, mem_limit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 	/* Give up if slot area array is full. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 	while (slot_area_index < MAX_SLOT_AREA) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 		/* Potentially raise address to meet alignment needs. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 		region.start = ALIGN(region.start, CONFIG_PHYSICAL_ALIGN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 		/* Did we raise the address above the passed in memory entry? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 		if (region.start > region_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 		/* Reduce size by any delta from the original address. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 		region.size = region_end - region.start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 		/* Return if region can't contain decompressed kernel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 		if (region.size < image_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 		/* If nothing overlaps, store the region and return. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 		if (!mem_avoid_overlap(&region, &overlap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 			process_gb_huge_pages(&region, image_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 		/* Store beginning of region if holds at least image_size. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 		if (overlap.start >= region.start + image_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 			region.size = overlap.start - region.start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 			process_gb_huge_pages(&region, image_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 		/* Clip off the overlapping region and start over. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 		region.start = overlap.start + overlap.size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) static bool process_mem_region(struct mem_vector *region,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 			       unsigned long minimum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 			       unsigned long image_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 	 * If no immovable memory found, or MEMORY_HOTREMOVE disabled,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 	 * use @region directly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 	if (!num_immovable_mem) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 		__process_mem_region(region, minimum, image_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 		if (slot_area_index == MAX_SLOT_AREA) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 			debug_putstr("Aborted e820/efi memmap scan (slot_areas full)!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) #if defined(CONFIG_MEMORY_HOTREMOVE) && defined(CONFIG_ACPI)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 	 * If immovable memory found, filter the intersection between
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 	 * immovable memory and @region.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 	for (i = 0; i < num_immovable_mem; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 		u64 start, end, entry_end, region_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 		struct mem_vector entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 		if (!mem_overlaps(region, &immovable_mem[i]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 		start = immovable_mem[i].start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 		end = start + immovable_mem[i].size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 		region_end = region->start + region->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 		entry.start = clamp(region->start, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 		entry_end = clamp(region_end, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 		entry.size = entry_end - entry.start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 		__process_mem_region(&entry, minimum, image_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 		if (slot_area_index == MAX_SLOT_AREA) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 			debug_putstr("Aborted e820/efi memmap scan when walking immovable regions(slot_areas full)!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) #ifdef CONFIG_EFI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680)  * Returns true if we processed the EFI memmap, which we prefer over the E820
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681)  * table if it is available.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) static bool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) process_efi_entries(unsigned long minimum, unsigned long image_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 	struct efi_info *e = &boot_params->efi_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 	bool efi_mirror_found = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 	struct mem_vector region;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 	efi_memory_desc_t *md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 	unsigned long pmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 	char *signature;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 	u32 nr_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 	signature = (char *)&e->efi_loader_signature;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 	if (strncmp(signature, EFI32_LOADER_SIGNATURE, 4) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 	    strncmp(signature, EFI64_LOADER_SIGNATURE, 4))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) #ifdef CONFIG_X86_32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 	/* Can't handle data above 4GB at this time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 	if (e->efi_memmap_hi) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 		warn("EFI memmap is above 4GB, can't be handled now on x86_32. EFI should be disabled.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 	pmap =  e->efi_memmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 	pmap = (e->efi_memmap | ((__u64)e->efi_memmap_hi << 32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 	nr_desc = e->efi_memmap_size / e->efi_memdesc_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 	for (i = 0; i < nr_desc; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) 		md = efi_early_memdesc_ptr(pmap, e->efi_memdesc_size, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 		if (md->attribute & EFI_MEMORY_MORE_RELIABLE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 			efi_mirror_found = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 	for (i = 0; i < nr_desc; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 		md = efi_early_memdesc_ptr(pmap, e->efi_memdesc_size, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 		 * Here we are more conservative in picking free memory than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 		 * the EFI spec allows:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 		 * According to the spec, EFI_BOOT_SERVICES_{CODE|DATA} are also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 		 * free memory and thus available to place the kernel image into,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 		 * but in practice there's firmware where using that memory leads
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 		 * to crashes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 		 * Only EFI_CONVENTIONAL_MEMORY is guaranteed to be free.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) 		if (md->type != EFI_CONVENTIONAL_MEMORY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 		if (efi_soft_reserve_enabled() &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) 		    (md->attribute & EFI_MEMORY_SP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) 		if (efi_mirror_found &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) 		    !(md->attribute & EFI_MEMORY_MORE_RELIABLE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) 		region.start = md->phys_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 		region.size = md->num_pages << EFI_PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) 		if (process_mem_region(&region, minimum, image_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) static inline bool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) process_efi_entries(unsigned long minimum, unsigned long image_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) static void process_e820_entries(unsigned long minimum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) 				 unsigned long image_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) 	struct mem_vector region;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) 	struct boot_e820_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) 	/* Verify potential e820 positions, appending to slots list. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) 	for (i = 0; i < boot_params->e820_entries; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) 		entry = &boot_params->e820_table[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) 		/* Skip non-RAM entries. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) 		if (entry->type != E820_TYPE_RAM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) 		region.start = entry->addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) 		region.size = entry->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) 		if (process_mem_region(&region, minimum, image_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) static unsigned long find_random_phys_addr(unsigned long minimum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) 					   unsigned long image_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) 	u64 phys_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) 	/* Bail out early if it's impossible to succeed. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) 	if (minimum + image_size > mem_limit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) 	/* Check if we had too many memmaps. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) 	if (memmap_too_large) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) 		debug_putstr("Aborted memory entries scan (more than 4 memmap= args)!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) 	if (!process_efi_entries(minimum, image_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) 		process_e820_entries(minimum, image_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) 	phys_addr = slots_fetch_random();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) 	/* Perform a final check to make sure the address is in range. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) 	if (phys_addr < minimum || phys_addr + image_size > mem_limit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) 		warn("Invalid physical address chosen!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) 	return (unsigned long)phys_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) static unsigned long find_random_virt_addr(unsigned long minimum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) 					   unsigned long image_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) 	unsigned long slots, random_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) 	 * There are how many CONFIG_PHYSICAL_ALIGN-sized slots
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) 	 * that can hold image_size within the range of minimum to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) 	 * KERNEL_IMAGE_SIZE?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) 	slots = 1 + (KERNEL_IMAGE_SIZE - minimum - image_size) / CONFIG_PHYSICAL_ALIGN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) 	random_addr = kaslr_get_random_long("Virtual") % slots;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) 	return random_addr * CONFIG_PHYSICAL_ALIGN + minimum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827)  * Since this function examines addresses much more numerically,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828)  * it takes the input and output pointers as 'unsigned long'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) void choose_random_location(unsigned long input,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) 			    unsigned long input_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) 			    unsigned long *output,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) 			    unsigned long output_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) 			    unsigned long *virt_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) 	unsigned long random_addr, min_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) 	if (cmdline_find_option_bool("nokaslr")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) 		warn("KASLR disabled: 'nokaslr' on cmdline.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) 	boot_params->hdr.loadflags |= KASLR_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) 	if (IS_ENABLED(CONFIG_X86_32))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) 		mem_limit = KERNEL_IMAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) 		mem_limit = MAXMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) 	/* Record the various known unsafe memory ranges. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) 	mem_avoid_init(input, input_size, *output);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) 	 * Low end of the randomization range should be the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) 	 * smaller of 512M or the initial kernel image
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) 	 * location:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) 	min_addr = min(*output, 512UL << 20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) 	/* Make sure minimum is aligned. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) 	min_addr = ALIGN(min_addr, CONFIG_PHYSICAL_ALIGN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) 	/* Walk available memory entries to find a random address. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) 	random_addr = find_random_phys_addr(min_addr, output_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) 	if (!random_addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) 		warn("Physical KASLR disabled: no suitable memory region!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) 		/* Update the new physical address location. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) 		if (*output != random_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) 			*output = random_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) 	/* Pick random virtual address starting from LOAD_PHYSICAL_ADDR. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) 	if (IS_ENABLED(CONFIG_X86_64))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) 		random_addr = find_random_virt_addr(LOAD_PHYSICAL_ADDR, output_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) 	*virt_addr = random_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) }