1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_X86_MSHYPER_H
3 #define _ASM_X86_MSHYPER_H
5 #include <linux/types.h>
9 #include <asm/hyperv-tlfs.h>
10 #include <asm/nospec-branch.h>
11 #include <asm/paravirt.h>
12 #include <asm/mshyperv.h>
16 DECLARE_STATIC_KEY_FALSE(isolation_type_snp);
18 typedef int (*hyperv_fill_flush_list_func)(
19 struct hv_guest_mapping_flush_list *flush,
22 void hyperv_vector_handler(struct pt_regs *regs);
24 #if IS_ENABLED(CONFIG_HYPERV)
25 extern int hyperv_init_cpuhp;
27 extern void *hv_hypercall_pg;
29 extern u64 hv_current_partition_id;
31 extern union hv_ghcb * __percpu *hv_ghcb_pg;
33 int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages);
34 int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id);
35 int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u32 flags);
37 static inline u64 hv_do_hypercall(u64 control, void *input, void *output)
39 u64 input_address = input ? virt_to_phys(input) : 0;
40 u64 output_address = output ? virt_to_phys(output) : 0;
47 __asm__ __volatile__("mov %4, %%r8\n"
49 : "=a" (hv_status), ASM_CALL_CONSTRAINT,
50 "+c" (control), "+d" (input_address)
51 : "r" (output_address),
52 THUNK_TARGET(hv_hypercall_pg)
53 : "cc", "memory", "r8", "r9", "r10", "r11");
55 u32 input_address_hi = upper_32_bits(input_address);
56 u32 input_address_lo = lower_32_bits(input_address);
57 u32 output_address_hi = upper_32_bits(output_address);
58 u32 output_address_lo = lower_32_bits(output_address);
63 __asm__ __volatile__(CALL_NOSPEC
65 "+c" (input_address_lo), ASM_CALL_CONSTRAINT
67 "b" (input_address_hi),
68 "D"(output_address_hi), "S"(output_address_lo),
69 THUNK_TARGET(hv_hypercall_pg)
75 /* Fast hypercall with 8 bytes of input and no output */
76 static inline u64 hv_do_fast_hypercall8(u16 code, u64 input1)
78 u64 hv_status, control = (u64)code | HV_HYPERCALL_FAST_BIT;
82 __asm__ __volatile__(CALL_NOSPEC
83 : "=a" (hv_status), ASM_CALL_CONSTRAINT,
84 "+c" (control), "+d" (input1)
85 : THUNK_TARGET(hv_hypercall_pg)
86 : "cc", "r8", "r9", "r10", "r11");
90 u32 input1_hi = upper_32_bits(input1);
91 u32 input1_lo = lower_32_bits(input1);
93 __asm__ __volatile__ (CALL_NOSPEC
99 THUNK_TARGET(hv_hypercall_pg)
100 : "cc", "edi", "esi");
106 /* Fast hypercall with 16 bytes of input */
107 static inline u64 hv_do_fast_hypercall16(u16 code, u64 input1, u64 input2)
109 u64 hv_status, control = (u64)code | HV_HYPERCALL_FAST_BIT;
113 __asm__ __volatile__("mov %4, %%r8\n"
115 : "=a" (hv_status), ASM_CALL_CONSTRAINT,
116 "+c" (control), "+d" (input1)
118 THUNK_TARGET(hv_hypercall_pg)
119 : "cc", "r8", "r9", "r10", "r11");
123 u32 input1_hi = upper_32_bits(input1);
124 u32 input1_lo = lower_32_bits(input1);
125 u32 input2_hi = upper_32_bits(input2);
126 u32 input2_lo = lower_32_bits(input2);
128 __asm__ __volatile__ (CALL_NOSPEC
130 "+c"(input1_lo), ASM_CALL_CONSTRAINT
131 : "A" (control), "b" (input1_hi),
132 "D"(input2_hi), "S"(input2_lo),
133 THUNK_TARGET(hv_hypercall_pg)
140 extern struct hv_vp_assist_page **hv_vp_assist_page;
142 static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu)
144 if (!hv_vp_assist_page)
147 return hv_vp_assist_page[cpu];
150 void __init hyperv_init(void);
151 void hyperv_setup_mmu_ops(void);
152 void set_hv_tscchange_cb(void (*cb)(void));
153 void clear_hv_tscchange_cb(void);
154 void hyperv_stop_tsc_emulation(void);
155 int hyperv_flush_guest_mapping(u64 as);
156 int hyperv_flush_guest_mapping_range(u64 as,
157 hyperv_fill_flush_list_func fill_func, void *data);
158 int hyperv_fill_flush_guest_mapping_list(
159 struct hv_guest_mapping_flush_list *flush,
160 u64 start_gfn, u64 end_gfn);
163 void hv_apic_init(void);
164 void __init hv_init_spinlocks(void);
165 bool hv_vcpu_is_preempted(int vcpu);
167 static inline void hv_apic_init(void) {}
170 struct irq_domain *hv_create_pci_msi_domain(void);
172 int hv_map_ioapic_interrupt(int ioapic_id, bool level, int vcpu, int vector,
173 struct hv_interrupt_entry *entry);
174 int hv_unmap_ioapic_interrupt(int ioapic_id, struct hv_interrupt_entry *entry);
175 int hv_set_mem_host_visibility(unsigned long addr, int numpages, bool visible);
177 #ifdef CONFIG_AMD_MEM_ENCRYPT
178 void hv_ghcb_msr_write(u64 msr, u64 value);
179 void hv_ghcb_msr_read(u64 msr, u64 *value);
180 bool hv_ghcb_negotiate_protocol(void);
181 void hv_ghcb_terminate(unsigned int set, unsigned int reason);
183 static inline void hv_ghcb_msr_write(u64 msr, u64 value) {}
184 static inline void hv_ghcb_msr_read(u64 msr, u64 *value) {}
185 static inline bool hv_ghcb_negotiate_protocol(void) { return false; }
186 static inline void hv_ghcb_terminate(unsigned int set, unsigned int reason) {}
189 extern bool hv_isolation_type_snp(void);
191 static inline bool hv_is_synic_reg(unsigned int reg)
193 if ((reg >= HV_REGISTER_SCONTROL) &&
194 (reg <= HV_REGISTER_SINT15))
199 static inline u64 hv_get_register(unsigned int reg)
203 if (hv_is_synic_reg(reg) && hv_isolation_type_snp())
204 hv_ghcb_msr_read(reg, &value);
210 static inline void hv_set_register(unsigned int reg, u64 value)
212 if (hv_is_synic_reg(reg) && hv_isolation_type_snp()) {
213 hv_ghcb_msr_write(reg, value);
215 /* Write proxy bit via wrmsl instruction */
216 if (reg >= HV_REGISTER_SINT0 &&
217 reg <= HV_REGISTER_SINT15)
218 wrmsrl(reg, value | 1 << 20);
224 #else /* CONFIG_HYPERV */
225 static inline void hyperv_init(void) {}
226 static inline void hyperv_setup_mmu_ops(void) {}
227 static inline void set_hv_tscchange_cb(void (*cb)(void)) {}
228 static inline void clear_hv_tscchange_cb(void) {}
229 static inline void hyperv_stop_tsc_emulation(void) {};
230 static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu)
234 static inline int hyperv_flush_guest_mapping(u64 as) { return -1; }
235 static inline int hyperv_flush_guest_mapping_range(u64 as,
236 hyperv_fill_flush_list_func fill_func, void *data)
240 static inline void hv_set_register(unsigned int reg, u64 value) { }
241 static inline u64 hv_get_register(unsigned int reg) { return 0; }
242 static inline int hv_set_mem_host_visibility(unsigned long addr, int numpages,
247 #endif /* CONFIG_HYPERV */
250 #include <asm-generic/mshyperv.h>