1 #include <linux/io.h> 2 #include <linux/slab.h> 3 #include <linux/memblock.h> 4 #include <linux/mem_encrypt.h> 5 6 #include <asm/set_memory.h> 7 #include <asm/pgtable.h> 8 #include <asm/realmode.h> 9 #include <asm/tlbflush.h> 10 11 struct real_mode_header *real_mode_header; 12 u32 *trampoline_cr4_features; 13 14 /* Hold the pgd entry used on booting additional CPUs */ 15 pgd_t trampoline_pgd_entry; 16 17 void __init set_real_mode_mem(phys_addr_t mem, size_t size) 18 { 19 void *base = __va(mem); 20 21 real_mode_header = (struct real_mode_header *) base; 22 printk(KERN_DEBUG "Base memory trampoline at [%p] %llx size %zu\n", 23 base, (unsigned long long)mem, size); 24 } 25 26 void __init reserve_real_mode(void) 27 { 28 phys_addr_t mem; 29 size_t size = real_mode_size_needed(); 30 31 if (!size) 32 return; 33 34 WARN_ON(slab_is_available()); 35 36 /* Has to be under 1M so we can execute real-mode AP code. */ 37 mem = memblock_find_in_range(0, 1<<20, size, PAGE_SIZE); 38 if (!mem) { 39 pr_info("No sub-1M memory is available for the trampoline\n"); 40 return; 41 } 42 43 memblock_reserve(mem, size); 44 set_real_mode_mem(mem, size); 45 } 46 47 static void __init setup_real_mode(void) 48 { 49 u16 real_mode_seg; 50 const u32 *rel; 51 u32 count; 52 unsigned char *base; 53 unsigned long phys_base; 54 struct trampoline_header *trampoline_header; 55 size_t size = PAGE_ALIGN(real_mode_blob_end - real_mode_blob); 56 #ifdef CONFIG_X86_64 57 u64 *trampoline_pgd; 58 u64 efer; 59 #endif 60 61 base = (unsigned char *)real_mode_header; 62 63 /* 64 * If SME is active, the trampoline area will need to be in 65 * decrypted memory in order to bring up other processors 66 * successfully. 67 */ 68 set_memory_decrypted((unsigned long)base, size >> PAGE_SHIFT); 69 70 memcpy(base, real_mode_blob, size); 71 72 phys_base = __pa(base); 73 real_mode_seg = phys_base >> 4; 74 75 rel = (u32 *) real_mode_relocs; 76 77 /* 16-bit segment relocations. */ 78 count = *rel++; 79 while (count--) { 80 u16 *seg = (u16 *) (base + *rel++); 81 *seg = real_mode_seg; 82 } 83 84 /* 32-bit linear relocations. */ 85 count = *rel++; 86 while (count--) { 87 u32 *ptr = (u32 *) (base + *rel++); 88 *ptr += phys_base; 89 } 90 91 /* Must be perfomed *after* relocation. */ 92 trampoline_header = (struct trampoline_header *) 93 __va(real_mode_header->trampoline_header); 94 95 #ifdef CONFIG_X86_32 96 trampoline_header->start = __pa_symbol(startup_32_smp); 97 trampoline_header->gdt_limit = __BOOT_DS + 7; 98 trampoline_header->gdt_base = __pa_symbol(boot_gdt); 99 #else 100 /* 101 * Some AMD processors will #GP(0) if EFER.LMA is set in WRMSR 102 * so we need to mask it out. 103 */ 104 rdmsrl(MSR_EFER, efer); 105 trampoline_header->efer = efer & ~EFER_LMA; 106 107 trampoline_header->start = (u64) secondary_startup_64; 108 trampoline_cr4_features = &trampoline_header->cr4; 109 *trampoline_cr4_features = mmu_cr4_features; 110 111 trampoline_header->flags = 0; 112 if (sme_active()) 113 trampoline_header->flags |= TH_FLAGS_SME_ACTIVE; 114 115 trampoline_pgd = (u64 *) __va(real_mode_header->trampoline_pgd); 116 trampoline_pgd[0] = trampoline_pgd_entry.pgd; 117 trampoline_pgd[511] = init_top_pgt[511].pgd; 118 #endif 119 } 120 121 /* 122 * reserve_real_mode() gets called very early, to guarantee the 123 * availability of low memory. This is before the proper kernel page 124 * tables are set up, so we cannot set page permissions in that 125 * function. Also trampoline code will be executed by APs so we 126 * need to mark it executable at do_pre_smp_initcalls() at least, 127 * thus run it as a early_initcall(). 128 */ 129 static void __init set_real_mode_permissions(void) 130 { 131 unsigned char *base = (unsigned char *) real_mode_header; 132 size_t size = PAGE_ALIGN(real_mode_blob_end - real_mode_blob); 133 134 size_t ro_size = 135 PAGE_ALIGN(real_mode_header->ro_end) - 136 __pa(base); 137 138 size_t text_size = 139 PAGE_ALIGN(real_mode_header->ro_end) - 140 real_mode_header->text_start; 141 142 unsigned long text_start = 143 (unsigned long) __va(real_mode_header->text_start); 144 145 set_memory_nx((unsigned long) base, size >> PAGE_SHIFT); 146 set_memory_ro((unsigned long) base, ro_size >> PAGE_SHIFT); 147 set_memory_x((unsigned long) text_start, text_size >> PAGE_SHIFT); 148 } 149 150 static int __init init_real_mode(void) 151 { 152 if (!real_mode_header) 153 panic("Real mode trampoline was not allocated"); 154 155 setup_real_mode(); 156 set_real_mode_permissions(); 157 158 return 0; 159 } 160 early_initcall(init_real_mode); 161