Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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
25union hv_ghcb;
26
27DECLARE_STATIC_KEY_FALSE(isolation_type_snp);
28DECLARE_STATIC_KEY_FALSE(isolation_type_tdx);
29
30typedef int (*hyperv_fill_flush_list_func)(
31 struct hv_guest_mapping_flush_list *flush,
32 void *data);
33
34void hyperv_vector_handler(struct pt_regs *regs);
35
36static inline unsigned char hv_get_nmi_reason(void)
37{
38 return 0;
39}
40
41#if IS_ENABLED(CONFIG_HYPERV)
42extern bool hyperv_paravisor_present;
43
44extern void *hv_hypercall_pg;
45
46extern u64 hv_current_partition_id;
47
48extern union hv_ghcb * __percpu *hv_ghcb_pg;
49
50bool hv_isolation_type_snp(void);
51bool hv_isolation_type_tdx(void);
52u64 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
61int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages);
62int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id);
63int 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 */
69static 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 */
121static 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 */
127static 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
166static 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
173static 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 */
181static 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
224static 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
231static 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
238extern struct hv_vp_assist_page **hv_vp_assist_page;
239
240static 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
248void __init hyperv_init(void);
249void hyperv_setup_mmu_ops(void);
250void set_hv_tscchange_cb(void (*cb)(void));
251void clear_hv_tscchange_cb(void);
252void hyperv_stop_tsc_emulation(void);
253int hyperv_flush_guest_mapping(u64 as);
254int hyperv_flush_guest_mapping_range(u64 as,
255 hyperv_fill_flush_list_func fill_func, void *data);
256int 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
261void hv_apic_init(void);
262void __init hv_init_spinlocks(void);
263bool hv_vcpu_is_preempted(int vcpu);
264#else
265static inline void hv_apic_init(void) {}
266#endif
267
268struct irq_domain *hv_create_pci_msi_domain(void);
269
270int hv_map_ioapic_interrupt(int ioapic_id, bool level, int vcpu, int vector,
271 struct hv_interrupt_entry *entry);
272int hv_unmap_ioapic_interrupt(int ioapic_id, struct hv_interrupt_entry *entry);
273
274#ifdef CONFIG_AMD_MEM_ENCRYPT
275bool hv_ghcb_negotiate_protocol(void);
276void __noreturn hv_ghcb_terminate(unsigned int set, unsigned int reason);
277int hv_snp_boot_ap(u32 cpu, unsigned long start_ip);
278#else
279static inline bool hv_ghcb_negotiate_protocol(void) { return false; }
280static inline void hv_ghcb_terminate(unsigned int set, unsigned int reason) {}
281static 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)
285void hv_vtom_init(void);
286void hv_ivm_msr_write(u64 msr, u64 value);
287void hv_ivm_msr_read(u64 msr, u64 *value);
288#else
289static inline void hv_vtom_init(void) {}
290static inline void hv_ivm_msr_write(u64 msr, u64 value) {}
291static inline void hv_ivm_msr_read(u64 msr, u64 *value) {}
292#endif
293
294static 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
300static 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
306u64 hv_get_msr(unsigned int reg);
307void hv_set_msr(unsigned int reg, u64 value);
308u64 hv_get_non_nested_msr(unsigned int reg);
309void hv_set_non_nested_msr(unsigned int reg, u64 value);
310
311static __always_inline u64 hv_raw_get_msr(unsigned int reg)
312{
313 return __rdmsr(reg);
314}
315
316#else /* CONFIG_HYPERV */
317static inline void hyperv_init(void) {}
318static inline void hyperv_setup_mmu_ops(void) {}
319static inline void set_hv_tscchange_cb(void (*cb)(void)) {}
320static inline void clear_hv_tscchange_cb(void) {}
321static inline void hyperv_stop_tsc_emulation(void) {};
322static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu)
323{
324 return NULL;
325}
326static inline int hyperv_flush_guest_mapping(u64 as) { return -1; }
327static inline int hyperv_flush_guest_mapping_range(u64 as,
328 hyperv_fill_flush_list_func fill_func, void *data)
329{
330 return -1;
331}
332static inline void hv_set_msr(unsigned int reg, u64 value) { }
333static inline u64 hv_get_msr(unsigned int reg) { return 0; }
334static inline void hv_set_non_nested_msr(unsigned int reg, u64 value) { }
335static inline u64 hv_get_non_nested_msr(unsigned int reg) { return 0; }
336#endif /* CONFIG_HYPERV */
337
338
339#ifdef CONFIG_HYPERV_VTL_MODE
340void __init hv_vtl_init_platform(void);
341int __init hv_vtl_early_init(void);
342#else
343static inline void __init hv_vtl_init_platform(void) {}
344static inline int __init hv_vtl_early_init(void) { return 0; }
345#endif
346
347#include <asm-generic/mshyperv.h>
348
349#endif