xref: /linux/arch/x86/hyperv/hv_vtl.c (revision c894ec016c9d0418dd832202225a8c64f450d71e)
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/realmode.h>
15 
16 extern struct boot_params boot_params;
17 static struct real_mode_header hv_vtl_real_mode_header;
18 
19 void __init hv_vtl_init_platform(void)
20 {
21 	pr_info("Linux runs in Hyper-V Virtual Trust Level\n");
22 
23 	x86_init.irqs.pre_vector_init = x86_init_noop;
24 	x86_init.timers.timer_init = x86_init_noop;
25 
26 	x86_platform.get_wallclock = get_rtc_noop;
27 	x86_platform.set_wallclock = set_rtc_noop;
28 	x86_platform.get_nmi_reason = hv_get_nmi_reason;
29 
30 	x86_platform.legacy.i8042 = X86_LEGACY_I8042_PLATFORM_ABSENT;
31 	x86_platform.legacy.rtc = 0;
32 	x86_platform.legacy.warm_reset = 0;
33 	x86_platform.legacy.reserve_bios_regions = 0;
34 	x86_platform.legacy.devices.pnpbios = 0;
35 }
36 
37 static inline u64 hv_vtl_system_desc_base(struct ldttss_desc *desc)
38 {
39 	return ((u64)desc->base3 << 32) | ((u64)desc->base2 << 24) |
40 		(desc->base1 << 16) | desc->base0;
41 }
42 
43 static inline u32 hv_vtl_system_desc_limit(struct ldttss_desc *desc)
44 {
45 	return ((u32)desc->limit1 << 16) | (u32)desc->limit0;
46 }
47 
48 typedef void (*secondary_startup_64_fn)(void*, void*);
49 static void hv_vtl_ap_entry(void)
50 {
51 	((secondary_startup_64_fn)secondary_startup_64)(&boot_params, &boot_params);
52 }
53 
54 static int hv_vtl_bringup_vcpu(u32 target_vp_index, u64 eip_ignored)
55 {
56 	u64 status;
57 	int ret = 0;
58 	struct hv_enable_vp_vtl *input;
59 	unsigned long irq_flags;
60 
61 	struct desc_ptr gdt_ptr;
62 	struct desc_ptr idt_ptr;
63 
64 	struct ldttss_desc *tss;
65 	struct ldttss_desc *ldt;
66 	struct desc_struct *gdt;
67 
68 	u64 rsp = current->thread.sp;
69 	u64 rip = (u64)&hv_vtl_ap_entry;
70 
71 	native_store_gdt(&gdt_ptr);
72 	store_idt(&idt_ptr);
73 
74 	gdt = (struct desc_struct *)((void *)(gdt_ptr.address));
75 	tss = (struct ldttss_desc *)(gdt + GDT_ENTRY_TSS);
76 	ldt = (struct ldttss_desc *)(gdt + GDT_ENTRY_LDT);
77 
78 	local_irq_save(irq_flags);
79 
80 	input = *this_cpu_ptr(hyperv_pcpu_input_arg);
81 	memset(input, 0, sizeof(*input));
82 
83 	input->partition_id = HV_PARTITION_ID_SELF;
84 	input->vp_index = target_vp_index;
85 	input->target_vtl.target_vtl = HV_VTL_MGMT;
86 
87 	/*
88 	 * The x86_64 Linux kernel follows the 16-bit -> 32-bit -> 64-bit
89 	 * mode transition sequence after waking up an AP with SIPI whose
90 	 * vector points to the 16-bit AP startup trampoline code. Here in
91 	 * VTL2, we can't perform that sequence as the AP has to start in
92 	 * the 64-bit mode.
93 	 *
94 	 * To make this happen, we tell the hypervisor to load a valid 64-bit
95 	 * context (most of which is just magic numbers from the CPU manual)
96 	 * so that AP jumps right to the 64-bit entry of the kernel, and the
97 	 * control registers are loaded with values that let the AP fetch the
98 	 * code and data and carry on with work it gets assigned.
99 	 */
100 
101 	input->vp_context.rip = rip;
102 	input->vp_context.rsp = rsp;
103 	input->vp_context.rflags = 0x0000000000000002;
104 	input->vp_context.efer = __rdmsr(MSR_EFER);
105 	input->vp_context.cr0 = native_read_cr0();
106 	input->vp_context.cr3 = __native_read_cr3();
107 	input->vp_context.cr4 = native_read_cr4();
108 	input->vp_context.msr_cr_pat = __rdmsr(MSR_IA32_CR_PAT);
109 	input->vp_context.idtr.limit = idt_ptr.size;
110 	input->vp_context.idtr.base = idt_ptr.address;
111 	input->vp_context.gdtr.limit = gdt_ptr.size;
112 	input->vp_context.gdtr.base = gdt_ptr.address;
113 
114 	/* Non-system desc (64bit), long, code, present */
115 	input->vp_context.cs.selector = __KERNEL_CS;
116 	input->vp_context.cs.base = 0;
117 	input->vp_context.cs.limit = 0xffffffff;
118 	input->vp_context.cs.attributes = 0xa09b;
119 	/* Non-system desc (64bit), data, present, granularity, default */
120 	input->vp_context.ss.selector = __KERNEL_DS;
121 	input->vp_context.ss.base = 0;
122 	input->vp_context.ss.limit = 0xffffffff;
123 	input->vp_context.ss.attributes = 0xc093;
124 
125 	/* System desc (128bit), present, LDT */
126 	input->vp_context.ldtr.selector = GDT_ENTRY_LDT * 8;
127 	input->vp_context.ldtr.base = hv_vtl_system_desc_base(ldt);
128 	input->vp_context.ldtr.limit = hv_vtl_system_desc_limit(ldt);
129 	input->vp_context.ldtr.attributes = 0x82;
130 
131 	/* System desc (128bit), present, TSS, 0x8b - busy, 0x89 -- default */
132 	input->vp_context.tr.selector = GDT_ENTRY_TSS * 8;
133 	input->vp_context.tr.base = hv_vtl_system_desc_base(tss);
134 	input->vp_context.tr.limit = hv_vtl_system_desc_limit(tss);
135 	input->vp_context.tr.attributes = 0x8b;
136 
137 	status = hv_do_hypercall(HVCALL_ENABLE_VP_VTL, input, NULL);
138 
139 	if (!hv_result_success(status) &&
140 	    hv_result(status) != HV_STATUS_VTL_ALREADY_ENABLED) {
141 		pr_err("HVCALL_ENABLE_VP_VTL failed for VP : %d ! [Err: %#llx\n]",
142 		       target_vp_index, status);
143 		ret = -EINVAL;
144 		goto free_lock;
145 	}
146 
147 	status = hv_do_hypercall(HVCALL_START_VP, input, NULL);
148 
149 	if (!hv_result_success(status)) {
150 		pr_err("HVCALL_START_VP failed for VP : %d ! [Err: %#llx]\n",
151 		       target_vp_index, status);
152 		ret = -EINVAL;
153 	}
154 
155 free_lock:
156 	local_irq_restore(irq_flags);
157 
158 	return ret;
159 }
160 
161 static int hv_vtl_apicid_to_vp_id(u32 apic_id)
162 {
163 	u64 control;
164 	u64 status;
165 	unsigned long irq_flags;
166 	struct hv_get_vp_from_apic_id_in *input;
167 	u32 *output, ret;
168 
169 	local_irq_save(irq_flags);
170 
171 	input = *this_cpu_ptr(hyperv_pcpu_input_arg);
172 	memset(input, 0, sizeof(*input));
173 	input->partition_id = HV_PARTITION_ID_SELF;
174 	input->apic_ids[0] = apic_id;
175 
176 	output = (u32 *)input;
177 
178 	control = HV_HYPERCALL_REP_COMP_1 | HVCALL_GET_VP_ID_FROM_APIC_ID;
179 	status = hv_do_hypercall(control, input, output);
180 	ret = output[0];
181 
182 	local_irq_restore(irq_flags);
183 
184 	if (!hv_result_success(status)) {
185 		pr_err("failed to get vp id from apic id %d, status %#llx\n",
186 		       apic_id, status);
187 		return -EINVAL;
188 	}
189 
190 	return ret;
191 }
192 
193 static int hv_vtl_wakeup_secondary_cpu(int apicid, unsigned long start_eip)
194 {
195 	int vp_id;
196 
197 	pr_debug("Bringing up CPU with APIC ID %d in VTL2...\n", apicid);
198 	vp_id = hv_vtl_apicid_to_vp_id(apicid);
199 
200 	if (vp_id < 0) {
201 		pr_err("Couldn't find CPU with APIC ID %d\n", apicid);
202 		return -EINVAL;
203 	}
204 	if (vp_id > ms_hyperv.max_vp_index) {
205 		pr_err("Invalid CPU id %d for APIC ID %d\n", vp_id, apicid);
206 		return -EINVAL;
207 	}
208 
209 	return hv_vtl_bringup_vcpu(vp_id, start_eip);
210 }
211 
212 static int __init hv_vtl_early_init(void)
213 {
214 	/*
215 	 * `boot_cpu_has` returns the runtime feature support,
216 	 * and here is the earliest it can be used.
217 	 */
218 	if (cpu_feature_enabled(X86_FEATURE_XSAVE))
219 		panic("XSAVE has to be disabled as it is not supported by this module.\n"
220 			  "Please add 'noxsave' to the kernel command line.\n");
221 
222 	real_mode_header = &hv_vtl_real_mode_header;
223 	apic->wakeup_secondary_cpu_64 = hv_vtl_wakeup_secondary_cpu;
224 
225 	return 0;
226 }
227 early_initcall(hv_vtl_early_init);
228