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