2 * Entropy functions used on early boot for KASLR base and memory
3 * randomization. The base randomization is done in the compressed
4 * kernel and memory randomization is done early when the regular
5 * kernel starts. This file is included in the compressed kernel and
6 * normally linked in the regular.
11 #include <asm/archrandom.h>
12 #include <asm/e820/api.h>
16 * When built for the regular kernel, several functions need to be stubbed out
17 * or changed to their regular kernel equivalent.
19 #ifndef KASLR_COMPRESSED_BOOT
20 #include <asm/cpufeature.h>
21 #include <asm/setup.h>
23 #define debug_putstr(v) early_printk("%s", v)
24 #define has_cpuflag(f) boot_cpu_has(f)
25 #define get_boot_seed() kaslr_offset()
28 #define I8254_PORT_CONTROL 0x43
29 #define I8254_PORT_COUNTER0 0x40
30 #define I8254_CMD_READBACK 0xC0
31 #define I8254_SELECT_COUNTER0 0x02
32 #define I8254_STATUS_NOTREADY 0x40
33 static inline u16 i8254(void)
38 outb(I8254_PORT_CONTROL,
39 I8254_CMD_READBACK | I8254_SELECT_COUNTER0);
40 status = inb(I8254_PORT_COUNTER0);
41 timer = inb(I8254_PORT_COUNTER0);
42 timer |= inb(I8254_PORT_COUNTER0) << 8;
43 } while (status & I8254_STATUS_NOTREADY);
48 unsigned long kaslr_get_random_long(const char *purpose)
51 const unsigned long mix_const = 0x5d6008cbf3848dd3UL;
53 const unsigned long mix_const = 0x3f39e593UL;
55 unsigned long raw, random = get_boot_seed();
56 bool use_i8254 = true;
58 debug_putstr(purpose);
59 debug_putstr(" KASLR using");
61 if (has_cpuflag(X86_FEATURE_RDRAND)) {
62 debug_putstr(" RDRAND");
63 if (rdrand_long(&raw)) {
69 if (has_cpuflag(X86_FEATURE_TSC)) {
70 debug_putstr(" RDTSC");
78 debug_putstr(" i8254");
82 /* Circular multiply for better bit diffusion */
84 : "=a" (random), "=d" (raw)
85 : "a" (random), "rm" (mix_const));
88 debug_putstr("...\n");