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