xref: /linux/arch/x86/hyperv/hv_vtl.c (revision 785cdec46e9227f9433884ed3b436471e944007c)
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 	pr_info("Linux runs in Hyper-V Virtual Trust Level\n");
60 
61 	x86_platform.realmode_reserve = x86_init_noop;
62 	x86_platform.realmode_init = x86_init_noop;
63 	x86_init.irqs.pre_vector_init = x86_init_noop;
64 	x86_init.timers.timer_init = x86_init_noop;
65 	x86_init.resources.probe_roms = x86_init_noop;
66 
67 	/* Avoid searching for BIOS MP tables */
68 	x86_init.mpparse.find_mptable = x86_init_noop;
69 	x86_init.mpparse.early_parse_smp_cfg = x86_init_noop;
70 
71 	x86_platform.get_wallclock = get_rtc_noop;
72 	x86_platform.set_wallclock = set_rtc_noop;
73 	x86_platform.get_nmi_reason = hv_get_nmi_reason;
74 
75 	x86_platform.legacy.i8042 = X86_LEGACY_I8042_PLATFORM_ABSENT;
76 	x86_platform.legacy.rtc = 0;
77 	x86_platform.legacy.warm_reset = 0;
78 	x86_platform.legacy.reserve_bios_regions = 0;
79 	x86_platform.legacy.devices.pnpbios = 0;
80 
81 	x86_init.hyper.msi_ext_dest_id = hv_vtl_msi_ext_dest_id;
82 }
83 
84 static inline u64 hv_vtl_system_desc_base(struct ldttss_desc *desc)
85 {
86 	return ((u64)desc->base3 << 32) | ((u64)desc->base2 << 24) |
87 		(desc->base1 << 16) | desc->base0;
88 }
89 
90 static inline u32 hv_vtl_system_desc_limit(struct ldttss_desc *desc)
91 {
92 	return ((u32)desc->limit1 << 16) | (u32)desc->limit0;
93 }
94 
95 typedef void (*secondary_startup_64_fn)(void*, void*);
96 static void hv_vtl_ap_entry(void)
97 {
98 	((secondary_startup_64_fn)secondary_startup_64)(&boot_params, &boot_params);
99 }
100 
101 static int hv_vtl_bringup_vcpu(u32 target_vp_index, int cpu, u64 eip_ignored)
102 {
103 	u64 status;
104 	int ret = 0;
105 	struct hv_enable_vp_vtl *input;
106 	unsigned long irq_flags;
107 
108 	struct desc_ptr gdt_ptr;
109 	struct desc_ptr idt_ptr;
110 
111 	struct ldttss_desc *tss;
112 	struct ldttss_desc *ldt;
113 	struct desc_struct *gdt;
114 
115 	struct task_struct *idle = idle_thread_get(cpu);
116 	u64 rsp = (unsigned long)idle->thread.sp;
117 
118 	u64 rip = (u64)&hv_vtl_ap_entry;
119 
120 	native_store_gdt(&gdt_ptr);
121 	store_idt(&idt_ptr);
122 
123 	gdt = (struct desc_struct *)((void *)(gdt_ptr.address));
124 	tss = (struct ldttss_desc *)(gdt + GDT_ENTRY_TSS);
125 	ldt = (struct ldttss_desc *)(gdt + GDT_ENTRY_LDT);
126 
127 	local_irq_save(irq_flags);
128 
129 	input = *this_cpu_ptr(hyperv_pcpu_input_arg);
130 	memset(input, 0, sizeof(*input));
131 
132 	input->partition_id = HV_PARTITION_ID_SELF;
133 	input->vp_index = target_vp_index;
134 	input->target_vtl.target_vtl = HV_VTL_MGMT;
135 
136 	/*
137 	 * The x86_64 Linux kernel follows the 16-bit -> 32-bit -> 64-bit
138 	 * mode transition sequence after waking up an AP with SIPI whose
139 	 * vector points to the 16-bit AP startup trampoline code. Here in
140 	 * VTL2, we can't perform that sequence as the AP has to start in
141 	 * the 64-bit mode.
142 	 *
143 	 * To make this happen, we tell the hypervisor to load a valid 64-bit
144 	 * context (most of which is just magic numbers from the CPU manual)
145 	 * so that AP jumps right to the 64-bit entry of the kernel, and the
146 	 * control registers are loaded with values that let the AP fetch the
147 	 * code and data and carry on with work it gets assigned.
148 	 */
149 
150 	input->vp_context.rip = rip;
151 	input->vp_context.rsp = rsp;
152 	input->vp_context.rflags = 0x0000000000000002;
153 	input->vp_context.efer = native_rdmsrq(MSR_EFER);
154 	input->vp_context.cr0 = native_read_cr0();
155 	input->vp_context.cr3 = __native_read_cr3();
156 	input->vp_context.cr4 = native_read_cr4();
157 	input->vp_context.msr_cr_pat = native_rdmsrq(MSR_IA32_CR_PAT);
158 	input->vp_context.idtr.limit = idt_ptr.size;
159 	input->vp_context.idtr.base = idt_ptr.address;
160 	input->vp_context.gdtr.limit = gdt_ptr.size;
161 	input->vp_context.gdtr.base = gdt_ptr.address;
162 
163 	/* Non-system desc (64bit), long, code, present */
164 	input->vp_context.cs.selector = __KERNEL_CS;
165 	input->vp_context.cs.base = 0;
166 	input->vp_context.cs.limit = 0xffffffff;
167 	input->vp_context.cs.attributes = 0xa09b;
168 	/* Non-system desc (64bit), data, present, granularity, default */
169 	input->vp_context.ss.selector = __KERNEL_DS;
170 	input->vp_context.ss.base = 0;
171 	input->vp_context.ss.limit = 0xffffffff;
172 	input->vp_context.ss.attributes = 0xc093;
173 
174 	/* System desc (128bit), present, LDT */
175 	input->vp_context.ldtr.selector = GDT_ENTRY_LDT * 8;
176 	input->vp_context.ldtr.base = hv_vtl_system_desc_base(ldt);
177 	input->vp_context.ldtr.limit = hv_vtl_system_desc_limit(ldt);
178 	input->vp_context.ldtr.attributes = 0x82;
179 
180 	/* System desc (128bit), present, TSS, 0x8b - busy, 0x89 -- default */
181 	input->vp_context.tr.selector = GDT_ENTRY_TSS * 8;
182 	input->vp_context.tr.base = hv_vtl_system_desc_base(tss);
183 	input->vp_context.tr.limit = hv_vtl_system_desc_limit(tss);
184 	input->vp_context.tr.attributes = 0x8b;
185 
186 	status = hv_do_hypercall(HVCALL_ENABLE_VP_VTL, input, NULL);
187 
188 	if (!hv_result_success(status) &&
189 	    hv_result(status) != HV_STATUS_VTL_ALREADY_ENABLED) {
190 		pr_err("HVCALL_ENABLE_VP_VTL failed for VP : %d ! [Err: %#llx\n]",
191 		       target_vp_index, status);
192 		ret = -EINVAL;
193 		goto free_lock;
194 	}
195 
196 	status = hv_do_hypercall(HVCALL_START_VP, input, NULL);
197 
198 	if (!hv_result_success(status)) {
199 		pr_err("HVCALL_START_VP failed for VP : %d ! [Err: %#llx]\n",
200 		       target_vp_index, status);
201 		ret = -EINVAL;
202 	}
203 
204 free_lock:
205 	local_irq_restore(irq_flags);
206 
207 	return ret;
208 }
209 
210 static int hv_vtl_apicid_to_vp_id(u32 apic_id)
211 {
212 	u64 control;
213 	u64 status;
214 	unsigned long irq_flags;
215 	struct hv_get_vp_from_apic_id_in *input;
216 	u32 *output, ret;
217 
218 	local_irq_save(irq_flags);
219 
220 	input = *this_cpu_ptr(hyperv_pcpu_input_arg);
221 	memset(input, 0, sizeof(*input));
222 	input->partition_id = HV_PARTITION_ID_SELF;
223 	input->apic_ids[0] = apic_id;
224 
225 	output = *this_cpu_ptr(hyperv_pcpu_output_arg);
226 
227 	control = HV_HYPERCALL_REP_COMP_1 | HVCALL_GET_VP_ID_FROM_APIC_ID;
228 	status = hv_do_hypercall(control, input, output);
229 	ret = output[0];
230 
231 	local_irq_restore(irq_flags);
232 
233 	if (!hv_result_success(status)) {
234 		pr_err("failed to get vp id from apic id %d, status %#llx\n",
235 		       apic_id, status);
236 		return -EINVAL;
237 	}
238 
239 	return ret;
240 }
241 
242 static int hv_vtl_wakeup_secondary_cpu(u32 apicid, unsigned long start_eip)
243 {
244 	int vp_id, cpu;
245 
246 	/* Find the logical CPU for the APIC ID */
247 	for_each_present_cpu(cpu) {
248 		if (arch_match_cpu_phys_id(cpu, apicid))
249 			break;
250 	}
251 	if (cpu >= nr_cpu_ids)
252 		return -EINVAL;
253 
254 	pr_debug("Bringing up CPU with APIC ID %d in VTL2...\n", apicid);
255 	vp_id = hv_vtl_apicid_to_vp_id(apicid);
256 
257 	if (vp_id < 0) {
258 		pr_err("Couldn't find CPU with APIC ID %d\n", apicid);
259 		return -EINVAL;
260 	}
261 	if (vp_id > ms_hyperv.max_vp_index) {
262 		pr_err("Invalid CPU id %d for APIC ID %d\n", vp_id, apicid);
263 		return -EINVAL;
264 	}
265 
266 	return hv_vtl_bringup_vcpu(vp_id, cpu, start_eip);
267 }
268 
269 int __init hv_vtl_early_init(void)
270 {
271 	machine_ops.emergency_restart = hv_vtl_emergency_restart;
272 	machine_ops.restart = hv_vtl_restart;
273 
274 	/*
275 	 * `boot_cpu_has` returns the runtime feature support,
276 	 * and here is the earliest it can be used.
277 	 */
278 	if (cpu_feature_enabled(X86_FEATURE_XSAVE))
279 		panic("XSAVE has to be disabled as it is not supported by this module.\n"
280 			  "Please add 'noxsave' to the kernel command line.\n");
281 
282 	real_mode_header = &hv_vtl_real_mode_header;
283 	apic_update_callback(wakeup_secondary_cpu_64, hv_vtl_wakeup_secondary_cpu);
284 
285 	return 0;
286 }
287