xref: /openbmc/linux/arch/x86/include/asm/mshyperv.h (revision b9b4fe3a)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_X86_MSHYPER_H
3 #define _ASM_X86_MSHYPER_H
4 
5 #include <linux/types.h>
6 #include <linux/nmi.h>
7 #include <linux/msi.h>
8 #include <asm/io.h>
9 #include <asm/hyperv-tlfs.h>
10 #include <asm/nospec-branch.h>
11 #include <asm/paravirt.h>
12 #include <asm/mshyperv.h>
13 
14 /*
15  * Hyper-V always provides a single IO-APIC at this MMIO address.
16  * Ideally, the value should be looked up in ACPI tables, but it
17  * is needed for mapping the IO-APIC early in boot on Confidential
18  * VMs, before ACPI functions can be used.
19  */
20 #define HV_IOAPIC_BASE_ADDRESS 0xfec00000
21 
22 #define HV_VTL_NORMAL 0x0
23 #define HV_VTL_SECURE 0x1
24 #define HV_VTL_MGMT   0x2
25 
26 union hv_ghcb;
27 
28 DECLARE_STATIC_KEY_FALSE(isolation_type_snp);
29 DECLARE_STATIC_KEY_FALSE(isolation_type_en_snp);
30 DECLARE_STATIC_KEY_FALSE(isolation_type_tdx);
31 
32 typedef int (*hyperv_fill_flush_list_func)(
33 		struct hv_guest_mapping_flush_list *flush,
34 		void *data);
35 
36 void hyperv_vector_handler(struct pt_regs *regs);
37 
38 static inline unsigned char hv_get_nmi_reason(void)
39 {
40 	return 0;
41 }
42 
43 #if IS_ENABLED(CONFIG_HYPERV)
44 extern int hyperv_init_cpuhp;
45 extern bool hyperv_paravisor_present;
46 
47 extern void *hv_hypercall_pg;
48 
49 extern u64 hv_current_partition_id;
50 
51 extern union hv_ghcb * __percpu *hv_ghcb_pg;
52 
53 extern bool hv_isolation_type_en_snp(void);
54 bool hv_isolation_type_tdx(void);
55 u64 hv_tdx_hypercall(u64 control, u64 param1, u64 param2);
56 
57 /*
58  * DEFAULT INIT GPAT and SEGMENT LIMIT value in struct VMSA
59  * to start AP in enlightened SEV guest.
60  */
61 #define HV_AP_INIT_GPAT_DEFAULT		0x0007040600070406ULL
62 #define HV_AP_SEGMENT_LIMIT		0xffffffff
63 
64 int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages);
65 int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id);
66 int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u32 flags);
67 
68 /*
69  * If the hypercall involves no input or output parameters, the hypervisor
70  * ignores the corresponding GPA pointer.
71  */
72 static inline u64 hv_do_hypercall(u64 control, void *input, void *output)
73 {
74 	u64 input_address = input ? virt_to_phys(input) : 0;
75 	u64 output_address = output ? virt_to_phys(output) : 0;
76 	u64 hv_status;
77 
78 #ifdef CONFIG_X86_64
79 	if (hv_isolation_type_tdx() && !hyperv_paravisor_present)
80 		return hv_tdx_hypercall(control, input_address, output_address);
81 
82 	if (hv_isolation_type_en_snp()) {
83 		__asm__ __volatile__("mov %4, %%r8\n"
84 				     "vmmcall"
85 				     : "=a" (hv_status), ASM_CALL_CONSTRAINT,
86 				       "+c" (control), "+d" (input_address)
87 				     :  "r" (output_address)
88 				     : "cc", "memory", "r8", "r9", "r10", "r11");
89 		return hv_status;
90 	}
91 
92 	if (!hv_hypercall_pg)
93 		return U64_MAX;
94 
95 	__asm__ __volatile__("mov %4, %%r8\n"
96 			     CALL_NOSPEC
97 			     : "=a" (hv_status), ASM_CALL_CONSTRAINT,
98 			       "+c" (control), "+d" (input_address)
99 			     :  "r" (output_address),
100 				THUNK_TARGET(hv_hypercall_pg)
101 			     : "cc", "memory", "r8", "r9", "r10", "r11");
102 #else
103 	u32 input_address_hi = upper_32_bits(input_address);
104 	u32 input_address_lo = lower_32_bits(input_address);
105 	u32 output_address_hi = upper_32_bits(output_address);
106 	u32 output_address_lo = lower_32_bits(output_address);
107 
108 	if (!hv_hypercall_pg)
109 		return U64_MAX;
110 
111 	__asm__ __volatile__(CALL_NOSPEC
112 			     : "=A" (hv_status),
113 			       "+c" (input_address_lo), ASM_CALL_CONSTRAINT
114 			     : "A" (control),
115 			       "b" (input_address_hi),
116 			       "D"(output_address_hi), "S"(output_address_lo),
117 			       THUNK_TARGET(hv_hypercall_pg)
118 			     : "cc", "memory");
119 #endif /* !x86_64 */
120 	return hv_status;
121 }
122 
123 /* Hypercall to the L0 hypervisor */
124 static inline u64 hv_do_nested_hypercall(u64 control, void *input, void *output)
125 {
126 	return hv_do_hypercall(control | HV_HYPERCALL_NESTED, input, output);
127 }
128 
129 /* Fast hypercall with 8 bytes of input and no output */
130 static inline u64 _hv_do_fast_hypercall8(u64 control, u64 input1)
131 {
132 	u64 hv_status;
133 
134 #ifdef CONFIG_X86_64
135 	if (hv_isolation_type_tdx() && !hyperv_paravisor_present)
136 		return hv_tdx_hypercall(control, input1, 0);
137 
138 	if (hv_isolation_type_en_snp()) {
139 		__asm__ __volatile__(
140 				"vmmcall"
141 				: "=a" (hv_status), ASM_CALL_CONSTRAINT,
142 				"+c" (control), "+d" (input1)
143 				:: "cc", "r8", "r9", "r10", "r11");
144 	} else {
145 		__asm__ __volatile__(CALL_NOSPEC
146 				     : "=a" (hv_status), ASM_CALL_CONSTRAINT,
147 				       "+c" (control), "+d" (input1)
148 				     : THUNK_TARGET(hv_hypercall_pg)
149 				     : "cc", "r8", "r9", "r10", "r11");
150 	}
151 #else
152 	{
153 		u32 input1_hi = upper_32_bits(input1);
154 		u32 input1_lo = lower_32_bits(input1);
155 
156 		__asm__ __volatile__ (CALL_NOSPEC
157 				      : "=A"(hv_status),
158 					"+c"(input1_lo),
159 					ASM_CALL_CONSTRAINT
160 				      :	"A" (control),
161 					"b" (input1_hi),
162 					THUNK_TARGET(hv_hypercall_pg)
163 				      : "cc", "edi", "esi");
164 	}
165 #endif
166 		return hv_status;
167 }
168 
169 static inline u64 hv_do_fast_hypercall8(u16 code, u64 input1)
170 {
171 	u64 control = (u64)code | HV_HYPERCALL_FAST_BIT;
172 
173 	return _hv_do_fast_hypercall8(control, input1);
174 }
175 
176 static inline u64 hv_do_fast_nested_hypercall8(u16 code, u64 input1)
177 {
178 	u64 control = (u64)code | HV_HYPERCALL_FAST_BIT | HV_HYPERCALL_NESTED;
179 
180 	return _hv_do_fast_hypercall8(control, input1);
181 }
182 
183 /* Fast hypercall with 16 bytes of input */
184 static inline u64 _hv_do_fast_hypercall16(u64 control, u64 input1, u64 input2)
185 {
186 	u64 hv_status;
187 
188 #ifdef CONFIG_X86_64
189 	if (hv_isolation_type_tdx() && !hyperv_paravisor_present)
190 		return hv_tdx_hypercall(control, input1, input2);
191 
192 	if (hv_isolation_type_en_snp()) {
193 		__asm__ __volatile__("mov %4, %%r8\n"
194 				     "vmmcall"
195 				     : "=a" (hv_status), ASM_CALL_CONSTRAINT,
196 				       "+c" (control), "+d" (input1)
197 				     : "r" (input2)
198 				     : "cc", "r8", "r9", "r10", "r11");
199 	} else {
200 		__asm__ __volatile__("mov %4, %%r8\n"
201 				     CALL_NOSPEC
202 				     : "=a" (hv_status), ASM_CALL_CONSTRAINT,
203 				       "+c" (control), "+d" (input1)
204 				     : "r" (input2),
205 				       THUNK_TARGET(hv_hypercall_pg)
206 				     : "cc", "r8", "r9", "r10", "r11");
207 	}
208 #else
209 	{
210 		u32 input1_hi = upper_32_bits(input1);
211 		u32 input1_lo = lower_32_bits(input1);
212 		u32 input2_hi = upper_32_bits(input2);
213 		u32 input2_lo = lower_32_bits(input2);
214 
215 		__asm__ __volatile__ (CALL_NOSPEC
216 				      : "=A"(hv_status),
217 					"+c"(input1_lo), ASM_CALL_CONSTRAINT
218 				      :	"A" (control), "b" (input1_hi),
219 					"D"(input2_hi), "S"(input2_lo),
220 					THUNK_TARGET(hv_hypercall_pg)
221 				      : "cc");
222 	}
223 #endif
224 	return hv_status;
225 }
226 
227 static inline u64 hv_do_fast_hypercall16(u16 code, u64 input1, u64 input2)
228 {
229 	u64 control = (u64)code | HV_HYPERCALL_FAST_BIT;
230 
231 	return _hv_do_fast_hypercall16(control, input1, input2);
232 }
233 
234 static inline u64 hv_do_fast_nested_hypercall16(u16 code, u64 input1, u64 input2)
235 {
236 	u64 control = (u64)code | HV_HYPERCALL_FAST_BIT | HV_HYPERCALL_NESTED;
237 
238 	return _hv_do_fast_hypercall16(control, input1, input2);
239 }
240 
241 extern struct hv_vp_assist_page **hv_vp_assist_page;
242 
243 static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu)
244 {
245 	if (!hv_vp_assist_page)
246 		return NULL;
247 
248 	return hv_vp_assist_page[cpu];
249 }
250 
251 void __init hyperv_init(void);
252 void hyperv_setup_mmu_ops(void);
253 void set_hv_tscchange_cb(void (*cb)(void));
254 void clear_hv_tscchange_cb(void);
255 void hyperv_stop_tsc_emulation(void);
256 int hyperv_flush_guest_mapping(u64 as);
257 int hyperv_flush_guest_mapping_range(u64 as,
258 		hyperv_fill_flush_list_func fill_func, void *data);
259 int hyperv_fill_flush_guest_mapping_list(
260 		struct hv_guest_mapping_flush_list *flush,
261 		u64 start_gfn, u64 end_gfn);
262 
263 #ifdef CONFIG_X86_64
264 void hv_apic_init(void);
265 void __init hv_init_spinlocks(void);
266 bool hv_vcpu_is_preempted(int vcpu);
267 #else
268 static inline void hv_apic_init(void) {}
269 #endif
270 
271 struct irq_domain *hv_create_pci_msi_domain(void);
272 
273 int hv_map_ioapic_interrupt(int ioapic_id, bool level, int vcpu, int vector,
274 		struct hv_interrupt_entry *entry);
275 int hv_unmap_ioapic_interrupt(int ioapic_id, struct hv_interrupt_entry *entry);
276 
277 #ifdef CONFIG_AMD_MEM_ENCRYPT
278 bool hv_ghcb_negotiate_protocol(void);
279 void __noreturn hv_ghcb_terminate(unsigned int set, unsigned int reason);
280 int hv_snp_boot_ap(int cpu, unsigned long start_ip);
281 #else
282 static inline bool hv_ghcb_negotiate_protocol(void) { return false; }
283 static inline void hv_ghcb_terminate(unsigned int set, unsigned int reason) {}
284 static inline int hv_snp_boot_ap(int cpu, unsigned long start_ip) { return 0; }
285 #endif
286 
287 extern bool hv_isolation_type_snp(void);
288 
289 #if defined(CONFIG_AMD_MEM_ENCRYPT) || defined(CONFIG_INTEL_TDX_GUEST)
290 void hv_vtom_init(void);
291 void hv_ivm_msr_write(u64 msr, u64 value);
292 void hv_ivm_msr_read(u64 msr, u64 *value);
293 #else
294 static inline void hv_vtom_init(void) {}
295 static inline void hv_ivm_msr_write(u64 msr, u64 value) {}
296 static inline void hv_ivm_msr_read(u64 msr, u64 *value) {}
297 #endif
298 
299 static inline bool hv_is_synic_reg(unsigned int reg)
300 {
301 	return (reg >= HV_REGISTER_SCONTROL) &&
302 	       (reg <= HV_REGISTER_SINT15);
303 }
304 
305 static inline bool hv_is_sint_reg(unsigned int reg)
306 {
307 	return (reg >= HV_REGISTER_SINT0) &&
308 	       (reg <= HV_REGISTER_SINT15);
309 }
310 
311 u64 hv_get_register(unsigned int reg);
312 void hv_set_register(unsigned int reg, u64 value);
313 u64 hv_get_non_nested_register(unsigned int reg);
314 void hv_set_non_nested_register(unsigned int reg, u64 value);
315 
316 static __always_inline u64 hv_raw_get_register(unsigned int reg)
317 {
318 	return __rdmsr(reg);
319 }
320 
321 #else /* CONFIG_HYPERV */
322 static inline void hyperv_init(void) {}
323 static inline void hyperv_setup_mmu_ops(void) {}
324 static inline void set_hv_tscchange_cb(void (*cb)(void)) {}
325 static inline void clear_hv_tscchange_cb(void) {}
326 static inline void hyperv_stop_tsc_emulation(void) {};
327 static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu)
328 {
329 	return NULL;
330 }
331 static inline int hyperv_flush_guest_mapping(u64 as) { return -1; }
332 static inline int hyperv_flush_guest_mapping_range(u64 as,
333 		hyperv_fill_flush_list_func fill_func, void *data)
334 {
335 	return -1;
336 }
337 static inline void hv_set_register(unsigned int reg, u64 value) { }
338 static inline u64 hv_get_register(unsigned int reg) { return 0; }
339 static inline void hv_set_non_nested_register(unsigned int reg, u64 value) { }
340 static inline u64 hv_get_non_nested_register(unsigned int reg) { return 0; }
341 #endif /* CONFIG_HYPERV */
342 
343 
344 #ifdef CONFIG_HYPERV_VTL_MODE
345 void __init hv_vtl_init_platform(void);
346 #else
347 static inline void __init hv_vtl_init_platform(void) {}
348 #endif
349 
350 #include <asm-generic/mshyperv.h>
351 
352 #endif
353