init.c 4.4 KB
Newer Older
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
#include <linux/pgtable.h>
7

L
Laura Abbott 已提交
8
#include <asm/set_memory.h>
9
#include <asm/realmode.h>
10
#include <asm/tlbflush.h>
11
#include <asm/crash.h>
T
Tom Lendacky 已提交
12
#include <asm/sev-es.h>
13

14
struct real_mode_header *real_mode_header;
15
u32 *trampoline_cr4_features;
16

17 18 19
/* Hold the pgd entry used on booting additional CPUs */
pgd_t trampoline_pgd_entry;

20
void __init reserve_real_mode(void)
21 22
{
	phys_addr_t mem;
23 24 25 26 27 28
	size_t size = real_mode_size_needed();

	if (!size)
		return;

	WARN_ON(slab_is_available());
29 30 31

	/* Has to be under 1M so we can execute real-mode AP code. */
	mem = memblock_find_in_range(0, 1<<20, size, PAGE_SIZE);
32 33 34 35
	if (!mem) {
		pr_info("No sub-1M memory is available for the trampoline\n");
		return;
	}
36 37

	memblock_reserve(mem, size);
38
	set_real_mode_mem(mem);
39
	crash_reserve_low_1M();
40 41
}

T
Tom Lendacky 已提交
42 43 44 45 46 47 48
static void sme_sev_setup_real_mode(struct trampoline_header *th)
{
#ifdef CONFIG_AMD_MEM_ENCRYPT
	if (sme_active())
		th->flags |= TH_FLAGS_SME_ACTIVE;

	if (sev_es_active()) {
49 50 51 52 53 54
		/*
		 * Skip the call to verify_cpu() in secondary_startup_64 as it
		 * will cause #VC exceptions when the AP can't handle them yet.
		 */
		th->start = (u64) secondary_startup_64_no_verify;

T
Tom Lendacky 已提交
55 56 57 58 59 60
		if (sev_es_setup_ap_jump_table(real_mode_header))
			panic("Failed to get/update SEV-ES AP Jump Table");
	}
#endif
}

61
static void __init setup_real_mode(void)
62
{
63
	u16 real_mode_seg;
64
	const u32 *rel;
65
	u32 count;
66
	unsigned char *base;
67
	unsigned long phys_base;
68
	struct trampoline_header *trampoline_header;
69
	size_t size = PAGE_ALIGN(real_mode_blob_end - real_mode_blob);
70 71
#ifdef CONFIG_X86_64
	u64 *trampoline_pgd;
72
	u64 efer;
73
#endif
74

75
	base = (unsigned char *)real_mode_header;
76

77 78 79
	/*
	 * If SME is active, the trampoline area will need to be in
	 * decrypted memory in order to bring up other processors
80
	 * successfully. This is not needed for SEV.
81
	 */
82 83
	if (sme_active())
		set_memory_decrypted((unsigned long)base, size >> PAGE_SHIFT);
84

85
	memcpy(base, real_mode_blob, size);
86

87 88 89
	phys_base = __pa(base);
	real_mode_seg = phys_base >> 4;

90 91 92
	rel = (u32 *) real_mode_relocs;

	/* 16-bit segment relocations. */
93 94 95
	count = *rel++;
	while (count--) {
		u16 *seg = (u16 *) (base + *rel++);
96 97 98 99
		*seg = real_mode_seg;
	}

	/* 32-bit linear relocations. */
100 101 102 103
	count = *rel++;
	while (count--) {
		u32 *ptr = (u32 *) (base + *rel++);
		*ptr += phys_base;
104 105
	}

106
	/* Must be performed *after* relocation. */
107 108 109
	trampoline_header = (struct trampoline_header *)
		__va(real_mode_header->trampoline_header);

110
#ifdef CONFIG_X86_32
111
	trampoline_header->start = __pa_symbol(startup_32_smp);
112
	trampoline_header->gdt_limit = __BOOT_DS + 7;
113
	trampoline_header->gdt_base = __pa_symbol(boot_gdt);
114
#else
115 116 117 118
	/*
	 * Some AMD processors will #GP(0) if EFER.LMA is set in WRMSR
	 * so we need to mask it out.
	 */
119 120
	rdmsrl(MSR_EFER, efer);
	trampoline_header->efer = efer & ~EFER_LMA;
121

122
	trampoline_header->start = (u64) secondary_startup_64;
123
	trampoline_cr4_features = &trampoline_header->cr4;
124
	*trampoline_cr4_features = mmu_cr4_features;
125

126 127
	trampoline_header->flags = 0;

128
	trampoline_pgd = (u64 *) __va(real_mode_header->trampoline_pgd);
129
	trampoline_pgd[0] = trampoline_pgd_entry.pgd;
130
	trampoline_pgd[511] = init_top_pgt[511].pgd;
131
#endif
T
Tom Lendacky 已提交
132 133

	sme_sev_setup_real_mode(trampoline_header);
134 135 136
}

/*
137
 * reserve_real_mode() gets called very early, to guarantee the
138
 * availability of low memory. This is before the proper kernel page
139
 * tables are set up, so we cannot set page permissions in that
140 141 142
 * function. Also trampoline code will be executed by APs so we
 * need to mark it executable at do_pre_smp_initcalls() at least,
 * thus run it as a early_initcall().
143
 */
144
static void __init set_real_mode_permissions(void)
145
{
146 147
	unsigned char *base = (unsigned char *) real_mode_header;
	size_t size = PAGE_ALIGN(real_mode_blob_end - real_mode_blob);
148

149
	size_t ro_size =
150 151
		PAGE_ALIGN(real_mode_header->ro_end) -
		__pa(base);
152 153

	size_t text_size =
154 155
		PAGE_ALIGN(real_mode_header->ro_end) -
		real_mode_header->text_start;
156 157

	unsigned long text_start =
158
		(unsigned long) __va(real_mode_header->text_start);
159

160 161
	set_memory_nx((unsigned long) base, size >> PAGE_SHIFT);
	set_memory_ro((unsigned long) base, ro_size >> PAGE_SHIFT);
162
	set_memory_x((unsigned long) text_start, text_size >> PAGE_SHIFT);
163 164 165 166 167 168 169 170 171
}

static int __init init_real_mode(void)
{
	if (!real_mode_header)
		panic("Real mode trampoline was not allocated");

	setup_real_mode();
	set_real_mode_permissions();
172

173 174
	return 0;
}
175
early_initcall(init_real_mode);