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