xref: /linux/arch/x86/hyperv/hv_vtl.c (revision 7a035678fc2bdee81881170764ef08a91a076147)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2023, Microsoft Corporation.
4  *
5  * Author:
6  *   Saurabh Sengar <ssengar@microsoft.com>
7  */
8 
9 #include <linux/acpi.h>
10 
11 #include <asm/acpi.h>
12 #include <asm/apic.h>
13 #include <asm/boot.h>
14 #include <asm/desc.h>
15 #include <asm/fpu/api.h>
16 #include <asm/fpu/types.h>
17 #include <asm/i8259.h>
18 #include <asm/mshyperv.h>
19 #include <asm/msr.h>
20 #include <asm/realmode.h>
21 #include <asm/reboot.h>
22 #include <asm/smap.h>
23 #include <linux/export.h>
24 #include <../kernel/smpboot.h>
25 #include "../../kernel/fpu/legacy.h"
26 
27 extern struct boot_params boot_params;
28 static struct real_mode_header hv_vtl_real_mode_header;
29 
30 static bool __init hv_vtl_msi_ext_dest_id(void)
31 {
32 	return true;
33 }
34 
35 /*
36  * The `native_machine_emergency_restart` function from `reboot.c` writes
37  * to the physical address 0x472 to indicate the type of reboot for the
38  * firmware. We cannot have that in VSM as the memory composition might
39  * be more generic, and such write effectively corrupts the memory thus
40  * making diagnostics harder at the very least.
41  */
42 static void  __noreturn hv_vtl_emergency_restart(void)
43 {
44 	/*
45 	 * Cause a triple fault and the immediate reset. Here the code does not run
46 	 * on the top of any firmware, whereby cannot reach out to its services.
47 	 * The inifinite loop is for the improbable case that the triple fault does
48 	 * not work and have to preserve the state intact for debugging.
49 	 */
50 	for (;;) {
51 		idt_invalidate();
52 		__asm__ __volatile__("int3");
53 	}
54 }
55 
56 /*
57  * The only way to restart in the VTL mode is to triple fault as the kernel runs
58  * as firmware.
59  */
60 static void  __noreturn hv_vtl_restart(char __maybe_unused *cmd)
61 {
62 	hv_vtl_emergency_restart();
63 }
64 
65 static inline bool within_page(u64 addr, u64 start)
66 {
67 	return addr >= start && addr < (start + PAGE_SIZE);
68 }
69 
70 static bool hv_vtl_is_private_mmio_tdx(u64 addr)
71 {
72 	u64 mb_addr = acpi_get_mp_wakeup_mailbox_paddr();
73 
74 	return mb_addr && within_page(addr, mb_addr);
75 }
76 
77 void __init hv_vtl_init_platform(void)
78 {
79 	/*
80 	 * This function is a no-op if the VTL mode is not enabled.
81 	 * If it is, this function runs if and only the kernel boots in
82 	 * VTL2 which the x86 hv initialization path makes sure of.
83 	 */
84 	pr_info("Linux runs in Hyper-V Virtual Trust Level %d\n", ms_hyperv.vtl);
85 
86 	/* There is no paravisor present if we are here. */
87 	if (hv_isolation_type_tdx()) {
88 		x86_init.resources.realmode_limit = SZ_4G;
89 		x86_platform.hyper.is_private_mmio = hv_vtl_is_private_mmio_tdx;
90 
91 	} else {
92 		x86_platform.realmode_reserve = x86_init_noop;
93 		x86_platform.realmode_init = x86_init_noop;
94 		real_mode_header = &hv_vtl_real_mode_header;
95 	}
96 	x86_init.irqs.pre_vector_init = x86_init_noop;
97 	x86_init.timers.timer_init = x86_init_noop;
98 	x86_init.resources.probe_roms = x86_init_noop;
99 
100 	/* Avoid searching for BIOS MP tables */
101 	x86_init.mpparse.find_mptable = x86_init_noop;
102 	x86_init.mpparse.early_parse_smp_cfg = x86_init_noop;
103 
104 	x86_platform.get_wallclock = get_rtc_noop;
105 	x86_platform.set_wallclock = set_rtc_noop;
106 	x86_platform.get_nmi_reason = hv_get_nmi_reason;
107 
108 	x86_platform.legacy.i8042 = X86_LEGACY_I8042_PLATFORM_ABSENT;
109 	x86_platform.legacy.rtc = 0;
110 	x86_platform.legacy.warm_reset = 0;
111 	x86_platform.legacy.reserve_bios_regions = 0;
112 	x86_platform.legacy.devices.pnpbios = 0;
113 
114 	x86_init.hyper.msi_ext_dest_id = hv_vtl_msi_ext_dest_id;
115 }
116 
117 static inline u64 hv_vtl_system_desc_base(struct ldttss_desc *desc)
118 {
119 	return ((u64)desc->base3 << 32) | ((u64)desc->base2 << 24) |
120 		(desc->base1 << 16) | desc->base0;
121 }
122 
123 static inline u32 hv_vtl_system_desc_limit(struct ldttss_desc *desc)
124 {
125 	return ((u32)desc->limit1 << 16) | (u32)desc->limit0;
126 }
127 
128 typedef void (*secondary_startup_64_fn)(void*, void*);
129 static void hv_vtl_ap_entry(void)
130 {
131 	((secondary_startup_64_fn)secondary_startup_64)(&boot_params, &boot_params);
132 }
133 
134 static int hv_vtl_bringup_vcpu(u32 target_vp_index, int cpu, u64 eip_ignored)
135 {
136 	u64 status, rsp, rip;
137 	int ret = 0;
138 	struct hv_enable_vp_vtl *input;
139 	unsigned long irq_flags;
140 
141 	struct desc_ptr gdt_ptr;
142 	struct desc_ptr idt_ptr;
143 
144 	struct ldttss_desc *tss;
145 	struct ldttss_desc *ldt;
146 	struct desc_struct *gdt;
147 
148 	struct task_struct *idle = idle_thread_get(cpu);
149 	if (IS_ERR(idle))
150 		return PTR_ERR(idle);
151 
152 	rsp = (unsigned long)idle->thread.sp;
153 	rip = (u64)&hv_vtl_ap_entry;
154 
155 	native_store_gdt(&gdt_ptr);
156 	store_idt(&idt_ptr);
157 
158 	gdt = (struct desc_struct *)((void *)(gdt_ptr.address));
159 	tss = (struct ldttss_desc *)(gdt + GDT_ENTRY_TSS);
160 	ldt = (struct ldttss_desc *)(gdt + GDT_ENTRY_LDT);
161 
162 	local_irq_save(irq_flags);
163 
164 	input = *this_cpu_ptr(hyperv_pcpu_input_arg);
165 	memset(input, 0, sizeof(*input));
166 
167 	input->partition_id = HV_PARTITION_ID_SELF;
168 	input->vp_index = target_vp_index;
169 	input->target_vtl.target_vtl = HV_VTL_MGMT;
170 
171 	/*
172 	 * The x86_64 Linux kernel follows the 16-bit -> 32-bit -> 64-bit
173 	 * mode transition sequence after waking up an AP with SIPI whose
174 	 * vector points to the 16-bit AP startup trampoline code. Here in
175 	 * VTL2, we can't perform that sequence as the AP has to start in
176 	 * the 64-bit mode.
177 	 *
178 	 * To make this happen, we tell the hypervisor to load a valid 64-bit
179 	 * context (most of which is just magic numbers from the CPU manual)
180 	 * so that AP jumps right to the 64-bit entry of the kernel, and the
181 	 * control registers are loaded with values that let the AP fetch the
182 	 * code and data and carry on with work it gets assigned.
183 	 */
184 
185 	input->vp_context.rip = rip;
186 	input->vp_context.rsp = rsp;
187 	input->vp_context.rflags = 0x0000000000000002;
188 	input->vp_context.efer = native_rdmsrq(MSR_EFER);
189 	input->vp_context.cr0 = native_read_cr0();
190 	input->vp_context.cr3 = __native_read_cr3();
191 	input->vp_context.cr4 = native_read_cr4();
192 	input->vp_context.msr_cr_pat = native_rdmsrq(MSR_IA32_CR_PAT);
193 	input->vp_context.idtr.limit = idt_ptr.size;
194 	input->vp_context.idtr.base = idt_ptr.address;
195 	input->vp_context.gdtr.limit = gdt_ptr.size;
196 	input->vp_context.gdtr.base = gdt_ptr.address;
197 
198 	/* Non-system desc (64bit), long, code, present */
199 	input->vp_context.cs.selector = __KERNEL_CS;
200 	input->vp_context.cs.base = 0;
201 	input->vp_context.cs.limit = 0xffffffff;
202 	input->vp_context.cs.attributes = 0xa09b;
203 	/* Non-system desc (64bit), data, present, granularity, default */
204 	input->vp_context.ss.selector = __KERNEL_DS;
205 	input->vp_context.ss.base = 0;
206 	input->vp_context.ss.limit = 0xffffffff;
207 	input->vp_context.ss.attributes = 0xc093;
208 
209 	/* System desc (128bit), present, LDT */
210 	input->vp_context.ldtr.selector = GDT_ENTRY_LDT * 8;
211 	input->vp_context.ldtr.base = hv_vtl_system_desc_base(ldt);
212 	input->vp_context.ldtr.limit = hv_vtl_system_desc_limit(ldt);
213 	input->vp_context.ldtr.attributes = 0x82;
214 
215 	/* System desc (128bit), present, TSS, 0x8b - busy, 0x89 -- default */
216 	input->vp_context.tr.selector = GDT_ENTRY_TSS * 8;
217 	input->vp_context.tr.base = hv_vtl_system_desc_base(tss);
218 	input->vp_context.tr.limit = hv_vtl_system_desc_limit(tss);
219 	input->vp_context.tr.attributes = 0x8b;
220 
221 	status = hv_do_hypercall(HVCALL_ENABLE_VP_VTL, input, NULL);
222 
223 	if (!hv_result_success(status) &&
224 	    hv_result(status) != HV_STATUS_VTL_ALREADY_ENABLED) {
225 		pr_err("HVCALL_ENABLE_VP_VTL failed for VP : %d ! [Err: %#llx\n]",
226 		       target_vp_index, status);
227 		ret = -EINVAL;
228 		goto free_lock;
229 	}
230 
231 	status = hv_do_hypercall(HVCALL_START_VP, input, NULL);
232 
233 	if (!hv_result_success(status)) {
234 		pr_err("HVCALL_START_VP failed for VP : %d ! [Err: %#llx]\n",
235 		       target_vp_index, status);
236 		ret = -EINVAL;
237 	}
238 
239 free_lock:
240 	local_irq_restore(irq_flags);
241 
242 	return ret;
243 }
244 
245 static int hv_vtl_wakeup_secondary_cpu(u32 apicid, unsigned long start_eip, unsigned int cpu)
246 {
247 	int vp_index;
248 
249 	pr_debug("Bringing up CPU with APIC ID %d in VTL2...\n", apicid);
250 	vp_index = hv_apicid_to_vp_index(apicid);
251 
252 	if (vp_index < 0) {
253 		pr_err("Couldn't find CPU with APIC ID %d\n", apicid);
254 		return -EINVAL;
255 	}
256 	if (vp_index > ms_hyperv.max_vp_index) {
257 		pr_err("Invalid CPU id %d for APIC ID %d\n", vp_index, apicid);
258 		return -EINVAL;
259 	}
260 
261 	return hv_vtl_bringup_vcpu(vp_index, cpu, start_eip);
262 }
263 
264 int __init hv_vtl_early_init(void)
265 {
266 	machine_ops.emergency_restart = hv_vtl_emergency_restart;
267 	machine_ops.restart = hv_vtl_restart;
268 
269 	/*
270 	 * `boot_cpu_has` returns the runtime feature support,
271 	 * and here is the earliest it can be used.
272 	 */
273 	if (cpu_feature_enabled(X86_FEATURE_XSAVE))
274 		panic("XSAVE has to be disabled as it is not supported by this module.\n"
275 			  "Please add 'noxsave' to the kernel command line.\n");
276 
277 	/*
278 	 * TDX confidential VMs do not trust the hypervisor and cannot use it to
279 	 * boot secondary CPUs. Instead, they will be booted using the wakeup
280 	 * mailbox if detected during boot. See setup_arch().
281 	 *
282 	 * There is no paravisor present if we are here.
283 	 */
284 	if (!hv_isolation_type_tdx())
285 		apic_update_callback(wakeup_secondary_cpu_64, hv_vtl_wakeup_secondary_cpu);
286 
287 	return 0;
288 }
289 
290 DEFINE_STATIC_CALL_NULL(__mshv_vtl_return_hypercall, void (*)(void));
291 
292 void mshv_vtl_return_call_init(u64 vtl_return_offset)
293 {
294 	static_call_update(__mshv_vtl_return_hypercall,
295 			   (void *)((u8 *)hv_hypercall_pg + vtl_return_offset));
296 }
297 EXPORT_SYMBOL(mshv_vtl_return_call_init);
298 
299 void mshv_vtl_return_call(struct mshv_vtl_cpu_context *vtl0)
300 {
301 	struct hv_vp_assist_page *hvp;
302 
303 	hvp = hv_vp_assist_page[smp_processor_id()];
304 	hvp->vtl_ret_x64rax = vtl0->rax;
305 	hvp->vtl_ret_x64rcx = vtl0->rcx;
306 
307 	kernel_fpu_begin_mask(0);
308 	fxrstor(&vtl0->fx_state);
309 	__mshv_vtl_return_call(vtl0);
310 	fxsave(&vtl0->fx_state);
311 	kernel_fpu_end();
312 }
313 EXPORT_SYMBOL(mshv_vtl_return_call);
314