blob: 1a702c6a226ec2daff44df71e742c0cc126bc370 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +03002#include <linux/io.h>
Andy Lutomirski5ff3e2c2016-08-10 02:29:16 -07003#include <linux/slab.h>
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +03004#include <linux/memblock.h>
Tom Lendacky163ea3c2017-07-17 16:10:20 -05005#include <linux/mem_encrypt.h>
Mike Rapoport65fddcf2020-06-08 21:32:42 -07006#include <linux/pgtable.h>
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +03007
Laura Abbottd1163652017-05-08 15:58:11 -07008#include <asm/set_memory.h>
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +03009#include <asm/realmode.h>
Andy Lutomirski18bc7bd2016-08-10 02:29:14 -070010#include <asm/tlbflush.h>
Lianbo Jiang6f599d82019-11-08 17:00:25 +080011#include <asm/crash.h>
Tom Lendacky8940ac92020-09-07 15:16:07 +020012#include <asm/sev-es.h>
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +030013
Jarkko Sakkinenb429dbf2012-05-08 21:22:41 +030014struct real_mode_header *real_mode_header;
Jarkko Sakkinencda846f2012-05-08 21:22:46 +030015u32 *trampoline_cr4_features;
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +030016
Thomas Garnierb234e8a2016-06-21 17:47:01 -070017/* Hold the pgd entry used on booting additional CPUs */
18pgd_t trampoline_pgd_entry;
19
Joerg Roedele61aa462021-12-02 16:32:25 +010020void load_trampoline_pgtable(void)
21{
22#ifdef CONFIG_X86_32
23 load_cr3(initial_page_table);
24#else
25 /*
26 * This function is called before exiting to real-mode and that will
27 * fail with CR4.PCIDE still set.
28 */
29 if (boot_cpu_has(X86_FEATURE_PCID))
30 cr4_clear_bits(X86_CR4_PCIDE);
31
32 write_cr3(real_mode_header->trampoline_pgd);
33#endif
34
35 /*
36 * The CR3 write above will not flush global TLB entries.
37 * Stale, global entries from previous page tables may still be
38 * present. Flush those stale entries.
39 *
40 * This ensures that memory accessed while running with
41 * trampoline_pgd is *actually* mapped into trampoline_pgd.
42 */
43 __flush_tlb_all();
44}
45
Andy Lutomirski5ff3e2c2016-08-10 02:29:16 -070046void __init reserve_real_mode(void)
47{
48 phys_addr_t mem;
49 size_t size = real_mode_size_needed();
50
51 if (!size)
52 return;
53
54 WARN_ON(slab_is_available());
55
56 /* Has to be under 1M so we can execute real-mode AP code. */
57 mem = memblock_find_in_range(0, 1<<20, size, PAGE_SIZE);
58 if (!mem) {
59 pr_info("No sub-1M memory is available for the trampoline\n");
60 return;
61 }
62
63 memblock_reserve(mem, size);
Matteo Crocef560bd12019-03-28 12:42:33 +010064 set_real_mode_mem(mem);
Lianbo Jiang6f599d82019-11-08 17:00:25 +080065 crash_reserve_low_1M();
Andy Lutomirski5ff3e2c2016-08-10 02:29:16 -070066}
67
Tom Lendacky8940ac92020-09-07 15:16:07 +020068static void sme_sev_setup_real_mode(struct trampoline_header *th)
69{
70#ifdef CONFIG_AMD_MEM_ENCRYPT
71 if (sme_active())
72 th->flags |= TH_FLAGS_SME_ACTIVE;
73
74 if (sev_es_active()) {
Joerg Roedel3ecacdb2020-09-07 15:16:09 +020075 /*
76 * Skip the call to verify_cpu() in secondary_startup_64 as it
77 * will cause #VC exceptions when the AP can't handle them yet.
78 */
79 th->start = (u64) secondary_startup_64_no_verify;
80
Tom Lendacky8940ac92020-09-07 15:16:07 +020081 if (sev_es_setup_ap_jump_table(real_mode_header))
82 panic("Failed to get/update SEV-ES AP Jump Table");
83 }
84#endif
85}
86
Andy Lutomirskid0de0f62016-08-10 02:29:15 -070087static void __init setup_real_mode(void)
Yinghai Lu4f7b92262013-01-24 12:19:51 -080088{
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +030089 u16 real_mode_seg;
H. Peter Anvin73060062013-12-18 15:52:13 -080090 const u32 *rel;
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +030091 u32 count;
Jarkko Sakkinenb429dbf2012-05-08 21:22:41 +030092 unsigned char *base;
H. Peter Anvin73060062013-12-18 15:52:13 -080093 unsigned long phys_base;
Jarkko Sakkinenf37240f2012-05-08 21:22:43 +030094 struct trampoline_header *trampoline_header;
Jarkko Sakkinenb429dbf2012-05-08 21:22:41 +030095 size_t size = PAGE_ALIGN(real_mode_blob_end - real_mode_blob);
Jarkko Sakkinenf37240f2012-05-08 21:22:43 +030096#ifdef CONFIG_X86_64
97 u64 *trampoline_pgd;
H. Peter Anvin638d9572012-05-16 14:02:05 -070098 u64 efer;
Joerg Roedel67d08452021-12-02 16:32:26 +010099 int i;
Jarkko Sakkinenf37240f2012-05-08 21:22:43 +0300100#endif
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +0300101
Yinghai Lu4f7b92262013-01-24 12:19:51 -0800102 base = (unsigned char *)real_mode_header;
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +0300103
Tom Lendacky163ea3c2017-07-17 16:10:20 -0500104 /*
105 * If SME is active, the trampoline area will need to be in
106 * decrypted memory in order to bring up other processors
Tom Lendackyfcdcd6c2017-10-20 09:30:46 -0500107 * successfully. This is not needed for SEV.
Tom Lendacky163ea3c2017-07-17 16:10:20 -0500108 */
Tom Lendackyfcdcd6c2017-10-20 09:30:46 -0500109 if (sme_active())
110 set_memory_decrypted((unsigned long)base, size >> PAGE_SHIFT);
Tom Lendacky163ea3c2017-07-17 16:10:20 -0500111
Jarkko Sakkinenb429dbf2012-05-08 21:22:41 +0300112 memcpy(base, real_mode_blob, size);
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +0300113
H. Peter Anvin73060062013-12-18 15:52:13 -0800114 phys_base = __pa(base);
115 real_mode_seg = phys_base >> 4;
116
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +0300117 rel = (u32 *) real_mode_relocs;
118
119 /* 16-bit segment relocations. */
H. Peter Anvin73060062013-12-18 15:52:13 -0800120 count = *rel++;
121 while (count--) {
122 u16 *seg = (u16 *) (base + *rel++);
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +0300123 *seg = real_mode_seg;
124 }
125
126 /* 32-bit linear relocations. */
H. Peter Anvin73060062013-12-18 15:52:13 -0800127 count = *rel++;
128 while (count--) {
129 u32 *ptr = (u32 *) (base + *rel++);
130 *ptr += phys_base;
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +0300131 }
132
Jarkko Sakkinenf37240f2012-05-08 21:22:43 +0300133 /* Must be perfomed *after* relocation. */
134 trampoline_header = (struct trampoline_header *)
135 __va(real_mode_header->trampoline_header);
136
Jarkko Sakkinen48927bb2012-05-08 21:22:28 +0300137#ifdef CONFIG_X86_32
Alexander Duyckfc8d7822012-11-16 13:57:13 -0800138 trampoline_header->start = __pa_symbol(startup_32_smp);
Jarkko Sakkinenf37240f2012-05-08 21:22:43 +0300139 trampoline_header->gdt_limit = __BOOT_DS + 7;
Alexander Duyckfc8d7822012-11-16 13:57:13 -0800140 trampoline_header->gdt_base = __pa_symbol(boot_gdt);
Jarkko Sakkinen48927bb2012-05-08 21:22:28 +0300141#else
H. Peter Anvin79603872012-05-16 13:22:41 -0700142 /*
143 * Some AMD processors will #GP(0) if EFER.LMA is set in WRMSR
144 * so we need to mask it out.
145 */
H. Peter Anvin638d9572012-05-16 14:02:05 -0700146 rdmsrl(MSR_EFER, efer);
147 trampoline_header->efer = efer & ~EFER_LMA;
Jarkko Sakkinencda846f2012-05-08 21:22:46 +0300148
Jarkko Sakkinenf37240f2012-05-08 21:22:43 +0300149 trampoline_header->start = (u64) secondary_startup_64;
Jarkko Sakkinencda846f2012-05-08 21:22:46 +0300150 trampoline_cr4_features = &trampoline_header->cr4;
Andy Lutomirski18bc7bd2016-08-10 02:29:14 -0700151 *trampoline_cr4_features = mmu_cr4_features;
Jarkko Sakkinencda846f2012-05-08 21:22:46 +0300152
Tom Lendacky46d010e2017-07-17 16:10:25 -0500153 trampoline_header->flags = 0;
Tom Lendacky46d010e2017-07-17 16:10:25 -0500154
Jarkko Sakkinenf37240f2012-05-08 21:22:43 +0300155 trampoline_pgd = (u64 *) __va(real_mode_header->trampoline_pgd);
Joerg Roedel67d08452021-12-02 16:32:26 +0100156
157 /* Map the real mode stub as virtual == physical */
Thomas Garnierb234e8a2016-06-21 17:47:01 -0700158 trampoline_pgd[0] = trampoline_pgd_entry.pgd;
Joerg Roedel67d08452021-12-02 16:32:26 +0100159
160 /*
161 * Include the entirety of the kernel mapping into the trampoline
162 * PGD. This way, all mappings present in the normal kernel page
163 * tables are usable while running on trampoline_pgd.
164 */
165 for (i = pgd_index(__PAGE_OFFSET); i < PTRS_PER_PGD; i++)
166 trampoline_pgd[i] = init_top_pgt[i].pgd;
Jarkko Sakkinen48927bb2012-05-08 21:22:28 +0300167#endif
Tom Lendacky8940ac92020-09-07 15:16:07 +0200168
169 sme_sev_setup_real_mode(trampoline_header);
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +0300170}
171
172/*
Yinghai Lu4f7b92262013-01-24 12:19:51 -0800173 * reserve_real_mode() gets called very early, to guarantee the
Yinghai Lu231b3642013-01-24 12:19:47 -0800174 * availability of low memory. This is before the proper kernel page
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +0300175 * tables are set up, so we cannot set page permissions in that
Yinghai Lu231b3642013-01-24 12:19:47 -0800176 * function. Also trampoline code will be executed by APs so we
177 * need to mark it executable at do_pre_smp_initcalls() at least,
178 * thus run it as a early_initcall().
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +0300179 */
Andy Lutomirskid0de0f62016-08-10 02:29:15 -0700180static void __init set_real_mode_permissions(void)
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +0300181{
Jarkko Sakkinenb429dbf2012-05-08 21:22:41 +0300182 unsigned char *base = (unsigned char *) real_mode_header;
183 size_t size = PAGE_ALIGN(real_mode_blob_end - real_mode_blob);
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +0300184
Jarkko Sakkinenf156ffc2012-05-08 21:22:30 +0300185 size_t ro_size =
Jarkko Sakkinenb429dbf2012-05-08 21:22:41 +0300186 PAGE_ALIGN(real_mode_header->ro_end) -
187 __pa(base);
Jarkko Sakkinenf156ffc2012-05-08 21:22:30 +0300188
189 size_t text_size =
Jarkko Sakkinenb429dbf2012-05-08 21:22:41 +0300190 PAGE_ALIGN(real_mode_header->ro_end) -
191 real_mode_header->text_start;
Jarkko Sakkinenf156ffc2012-05-08 21:22:30 +0300192
193 unsigned long text_start =
Jarkko Sakkinenb429dbf2012-05-08 21:22:41 +0300194 (unsigned long) __va(real_mode_header->text_start);
Jarkko Sakkinenf156ffc2012-05-08 21:22:30 +0300195
Jarkko Sakkinenb429dbf2012-05-08 21:22:41 +0300196 set_memory_nx((unsigned long) base, size >> PAGE_SHIFT);
197 set_memory_ro((unsigned long) base, ro_size >> PAGE_SHIFT);
Jarkko Sakkinenf156ffc2012-05-08 21:22:30 +0300198 set_memory_x((unsigned long) text_start, text_size >> PAGE_SHIFT);
Andy Lutomirskid0de0f62016-08-10 02:29:15 -0700199}
200
201static int __init init_real_mode(void)
202{
203 if (!real_mode_header)
204 panic("Real mode trampoline was not allocated");
205
206 setup_real_mode();
207 set_real_mode_permissions();
Jarkko Sakkinenf156ffc2012-05-08 21:22:30 +0300208
Jarkko Sakkinen084ee1c62012-05-08 21:22:26 +0300209 return 0;
210}
Andy Lutomirskid0de0f62016-08-10 02:29:15 -0700211early_initcall(init_real_mode);