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