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.
10 #include <asm/archrandom.h>
15 * When built for the regular kernel, several functions need to be stubbed out
16 * or changed to their regular kernel equivalent.
18 #ifndef KASLR_COMPRESSED_BOOT
19 #include <asm/cpufeature.h>
20 #include <asm/setup.h>
22 #define debug_putstr(v) early_printk("%s", v)
23 #define has_cpuflag(f) boot_cpu_has(f)
24 #define get_boot_seed() kaslr_offset()
27 #define I8254_PORT_CONTROL 0x43
28 #define I8254_PORT_COUNTER0 0x40
29 #define I8254_CMD_READBACK 0xC0
30 #define I8254_SELECT_COUNTER0 0x02
31 #define I8254_STATUS_NOTREADY 0x40
32 static inline u16
i8254(void)
37 outb(I8254_PORT_CONTROL
,
38 I8254_CMD_READBACK
| I8254_SELECT_COUNTER0
);
39 status
= inb(I8254_PORT_COUNTER0
);
40 timer
= inb(I8254_PORT_COUNTER0
);
41 timer
|= inb(I8254_PORT_COUNTER0
) << 8;
42 } while (status
& I8254_STATUS_NOTREADY
);
47 unsigned long kaslr_get_random_long(const char *purpose
)
50 const unsigned long mix_const
= 0x5d6008cbf3848dd3UL
;
52 const unsigned long mix_const
= 0x3f39e593UL
;
54 unsigned long raw
, random
= get_boot_seed();
55 bool use_i8254
= true;
57 debug_putstr(purpose
);
58 debug_putstr(" KASLR using");
60 if (has_cpuflag(X86_FEATURE_RDRAND
)) {
61 debug_putstr(" RDRAND");
62 if (rdrand_long(&raw
)) {
68 if (has_cpuflag(X86_FEATURE_TSC
)) {
69 debug_putstr(" RDTSC");
77 debug_putstr(" i8254");
81 /* Circular multiply for better bit diffusion */
83 : "=a" (random
), "=d" (raw
)
84 : "a" (random
), "rm" (mix_const
));
87 debug_putstr("...\n");