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