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