3 #ifdef CONFIG_RANDOMIZE_BASE
5 #include <asm/archrandom.h>
8 #define I8254_PORT_CONTROL 0x43
9 #define I8254_PORT_COUNTER0 0x40
10 #define I8254_CMD_READBACK 0xC0
11 #define I8254_SELECT_COUNTER0 0x02
12 #define I8254_STATUS_NOTREADY 0x40
13 static inline u16 i8254(void)
18 outb(I8254_PORT_CONTROL,
19 I8254_CMD_READBACK | I8254_SELECT_COUNTER0);
20 status = inb(I8254_PORT_COUNTER0);
21 timer = inb(I8254_PORT_COUNTER0);
22 timer |= inb(I8254_PORT_COUNTER0) << 8;
23 } while (status & I8254_STATUS_NOTREADY);
28 static unsigned long get_random_long(void)
32 if (has_cpuflag(X86_FEATURE_RDRAND)) {
33 debug_putstr("KASLR using RDRAND...\n");
34 if (rdrand_long(&random))
38 if (has_cpuflag(X86_FEATURE_TSC)) {
41 debug_putstr("KASLR using RDTSC...\n");
44 /* Only use the low bits of rdtsc. */
45 random = raw & 0xffff;
47 debug_putstr("KASLR using i8254...\n");
51 /* Extend timer bits poorly... */
52 random |= (random << 16);
54 random |= (random << 32);
64 #define MEM_AVOID_MAX 5
65 struct mem_vector mem_avoid[MEM_AVOID_MAX];
67 static bool mem_contains(struct mem_vector *region, struct mem_vector *item)
69 /* Item at least partially before region. */
70 if (item->start < region->start)
72 /* Item at least partially after region. */
73 if (item->start + item->size > region->start + region->size)
78 static bool mem_overlaps(struct mem_vector *one, struct mem_vector *two)
80 /* Item one is entirely before item two. */
81 if (one->start + one->size <= two->start)
83 /* Item one is entirely after item two. */
84 if (one->start >= two->start + two->size)
89 static void mem_avoid_init(unsigned long input, unsigned long input_size,
90 unsigned long output, unsigned long output_size)
92 u64 initrd_start, initrd_size;
93 u64 cmd_line, cmd_line_size;
94 unsigned long unsafe, unsafe_len;
98 * Avoid the region that is unsafe to overlap during
99 * decompression (see calculations at top of misc.c).
101 unsafe_len = (output_size >> 12) + 32768 + 18;
102 unsafe = (unsigned long)input + input_size - unsafe_len;
103 mem_avoid[0].start = unsafe;
104 mem_avoid[0].size = unsafe_len;
107 initrd_start = (u64)real_mode->ext_ramdisk_image << 32;
108 initrd_start |= real_mode->hdr.ramdisk_image;
109 initrd_size = (u64)real_mode->ext_ramdisk_size << 32;
110 initrd_size |= real_mode->hdr.ramdisk_size;
111 mem_avoid[1].start = initrd_start;
112 mem_avoid[1].size = initrd_size;
114 /* Avoid kernel command line. */
115 cmd_line = (u64)real_mode->ext_cmd_line_ptr << 32;
116 cmd_line |= real_mode->hdr.cmd_line_ptr;
117 /* Calculate size of cmd_line. */
118 ptr = (char *)(unsigned long)cmd_line;
119 for (cmd_line_size = 0; ptr[cmd_line_size++]; )
121 mem_avoid[2].start = cmd_line;
122 mem_avoid[2].size = cmd_line_size;
124 /* Avoid heap memory. */
125 mem_avoid[3].start = (unsigned long)free_mem_ptr;
126 mem_avoid[3].size = BOOT_HEAP_SIZE;
128 /* Avoid stack memory. */
129 mem_avoid[4].start = (unsigned long)free_mem_end_ptr;
130 mem_avoid[4].size = BOOT_STACK_SIZE;
133 /* Does this memory vector overlap a known avoided area? */
134 bool mem_avoid_overlap(struct mem_vector *img)
138 for (i = 0; i < MEM_AVOID_MAX; i++) {
139 if (mem_overlaps(img, &mem_avoid[i]))
146 unsigned long slots[CONFIG_RANDOMIZE_BASE_MAX_OFFSET / CONFIG_PHYSICAL_ALIGN];
147 unsigned long slot_max = 0;
149 static void slots_append(unsigned long addr)
151 /* Overflowing the slots list should be impossible. */
152 if (slot_max >= CONFIG_RANDOMIZE_BASE_MAX_OFFSET /
153 CONFIG_PHYSICAL_ALIGN)
156 slots[slot_max++] = addr;
159 static unsigned long slots_fetch_random(void)
161 /* Handle case of no slots stored. */
165 return slots[get_random_long() % slot_max];
168 static void process_e820_entry(struct e820entry *entry,
169 unsigned long minimum,
170 unsigned long image_size)
172 struct mem_vector region, img;
174 /* Skip non-RAM entries. */
175 if (entry->type != E820_RAM)
178 /* Ignore entries entirely above our maximum. */
179 if (entry->addr >= CONFIG_RANDOMIZE_BASE_MAX_OFFSET)
182 /* Ignore entries entirely below our minimum. */
183 if (entry->addr + entry->size < minimum)
186 region.start = entry->addr;
187 region.size = entry->size;
189 /* Potentially raise address to minimum location. */
190 if (region.start < minimum)
191 region.start = minimum;
193 /* Potentially raise address to meet alignment requirements. */
194 region.start = ALIGN(region.start, CONFIG_PHYSICAL_ALIGN);
196 /* Did we raise the address above the bounds of this e820 region? */
197 if (region.start > entry->addr + entry->size)
200 /* Reduce size by any delta from the original address. */
201 region.size -= region.start - entry->addr;
203 /* Reduce maximum size to fit end of image within maximum limit. */
204 if (region.start + region.size > CONFIG_RANDOMIZE_BASE_MAX_OFFSET)
205 region.size = CONFIG_RANDOMIZE_BASE_MAX_OFFSET - region.start;
207 /* Walk each aligned slot and check for avoided areas. */
208 for (img.start = region.start, img.size = image_size ;
209 mem_contains(®ion, &img) ;
210 img.start += CONFIG_PHYSICAL_ALIGN) {
211 if (mem_avoid_overlap(&img))
213 slots_append(img.start);
217 static unsigned long find_random_addr(unsigned long minimum,
223 /* Make sure minimum is aligned. */
224 minimum = ALIGN(minimum, CONFIG_PHYSICAL_ALIGN);
226 /* Verify potential e820 positions, appending to slots list. */
227 for (i = 0; i < real_mode->e820_entries; i++) {
228 process_e820_entry(&real_mode->e820_map[i], minimum, size);
231 return slots_fetch_random();
234 unsigned char *choose_kernel_location(unsigned char *input,
235 unsigned long input_size,
236 unsigned char *output,
237 unsigned long output_size)
239 unsigned long choice = (unsigned long)output;
240 unsigned long random;
242 if (cmdline_find_option_bool("nokaslr")) {
243 debug_putstr("KASLR disabled...\n");
247 /* Record the various known unsafe memory ranges. */
248 mem_avoid_init((unsigned long)input, input_size,
249 (unsigned long)output, output_size);
251 /* Walk e820 and find a random address. */
252 random = find_random_addr(choice, output_size);
254 debug_putstr("KASLR could not find suitable E820 region...\n");
258 /* Always enforce the minimum. */
264 return (unsigned char *)choice;
267 #endif /* CONFIG_RANDOMIZE_BASE */