Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Entropy functions used on early boot for KASLR base and memory
0004  * randomization. The base randomization is done in the compressed
0005  * kernel and memory randomization is done early when the regular
0006  * kernel starts. This file is included in the compressed kernel and
0007  * normally linked in the regular.
0008  */
0009 #include <asm/asm.h>
0010 #include <asm/kaslr.h>
0011 #include <asm/msr.h>
0012 #include <asm/archrandom.h>
0013 #include <asm/e820/api.h>
0014 #include <asm/shared/io.h>
0015 
0016 /*
0017  * When built for the regular kernel, several functions need to be stubbed out
0018  * or changed to their regular kernel equivalent.
0019  */
0020 #ifndef KASLR_COMPRESSED_BOOT
0021 #include <asm/cpufeature.h>
0022 #include <asm/setup.h>
0023 
0024 #define debug_putstr(v) early_printk("%s", v)
0025 #define has_cpuflag(f) boot_cpu_has(f)
0026 #define get_boot_seed() kaslr_offset()
0027 #endif
0028 
0029 #define I8254_PORT_CONTROL  0x43
0030 #define I8254_PORT_COUNTER0 0x40
0031 #define I8254_CMD_READBACK  0xC0
0032 #define I8254_SELECT_COUNTER0   0x02
0033 #define I8254_STATUS_NOTREADY   0x40
0034 static inline u16 i8254(void)
0035 {
0036     u16 status, timer;
0037 
0038     do {
0039         outb(I8254_CMD_READBACK | I8254_SELECT_COUNTER0,
0040              I8254_PORT_CONTROL);
0041         status = inb(I8254_PORT_COUNTER0);
0042         timer  = inb(I8254_PORT_COUNTER0);
0043         timer |= inb(I8254_PORT_COUNTER0) << 8;
0044     } while (status & I8254_STATUS_NOTREADY);
0045 
0046     return timer;
0047 }
0048 
0049 unsigned long kaslr_get_random_long(const char *purpose)
0050 {
0051 #ifdef CONFIG_X86_64
0052     const unsigned long mix_const = 0x5d6008cbf3848dd3UL;
0053 #else
0054     const unsigned long mix_const = 0x3f39e593UL;
0055 #endif
0056     unsigned long raw, random = get_boot_seed();
0057     bool use_i8254 = true;
0058 
0059     if (purpose) {
0060         debug_putstr(purpose);
0061         debug_putstr(" KASLR using");
0062     }
0063 
0064     if (has_cpuflag(X86_FEATURE_RDRAND)) {
0065         if (purpose)
0066             debug_putstr(" RDRAND");
0067         if (rdrand_long(&raw)) {
0068             random ^= raw;
0069             use_i8254 = false;
0070         }
0071     }
0072 
0073     if (has_cpuflag(X86_FEATURE_TSC)) {
0074         if (purpose)
0075             debug_putstr(" RDTSC");
0076         raw = rdtsc();
0077 
0078         random ^= raw;
0079         use_i8254 = false;
0080     }
0081 
0082     if (use_i8254) {
0083         if (purpose)
0084             debug_putstr(" i8254");
0085         random ^= i8254();
0086     }
0087 
0088     /* Circular multiply for better bit diffusion */
0089     asm(_ASM_MUL "%3"
0090         : "=a" (random), "=d" (raw)
0091         : "a" (random), "rm" (mix_const));
0092     random += raw;
0093 
0094     if (purpose)
0095         debug_putstr("...\n");
0096 
0097     return random;
0098 }