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