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