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/atomic.h> 7 #include <linux/nmi.h> 8 #include <asm/io.h> 9 #include <asm/hyperv.h> 10 #include <asm/nospec-branch.h> 11 12 /* 13 * The below CPUID leaves are present if VersionAndFeatures.HypervisorPresent 14 * is set by CPUID(HVCPUID_VERSION_FEATURES). 15 */ 16 enum hv_cpuid_function { 17 HVCPUID_VERSION_FEATURES = 0x00000001, 18 HVCPUID_VENDOR_MAXFUNCTION = 0x40000000, 19 HVCPUID_INTERFACE = 0x40000001, 20 21 /* 22 * The remaining functions depend on the value of 23 * HVCPUID_INTERFACE 24 */ 25 HVCPUID_VERSION = 0x40000002, 26 HVCPUID_FEATURES = 0x40000003, 27 HVCPUID_ENLIGHTENMENT_INFO = 0x40000004, 28 HVCPUID_IMPLEMENTATION_LIMITS = 0x40000005, 29 }; 30 31 struct ms_hyperv_info { 32 u32 features; 33 u32 misc_features; 34 u32 hints; 35 u32 max_vp_index; 36 u32 max_lp_index; 37 }; 38 39 extern struct ms_hyperv_info ms_hyperv; 40 41 /* 42 * Declare the MSR used to setup pages used to communicate with the hypervisor. 43 */ 44 union hv_x64_msr_hypercall_contents { 45 u64 as_uint64; 46 struct { 47 u64 enable:1; 48 u64 reserved:11; 49 u64 guest_physical_address:52; 50 }; 51 }; 52 53 /* 54 * TSC page layout. 55 */ 56 57 struct ms_hyperv_tsc_page { 58 volatile u32 tsc_sequence; 59 u32 reserved1; 60 volatile u64 tsc_scale; 61 volatile s64 tsc_offset; 62 u64 reserved2[509]; 63 }; 64 65 /* 66 * The guest OS needs to register the guest ID with the hypervisor. 67 * The guest ID is a 64 bit entity and the structure of this ID is 68 * specified in the Hyper-V specification: 69 * 70 * msdn.microsoft.com/en-us/library/windows/hardware/ff542653%28v=vs.85%29.aspx 71 * 72 * While the current guideline does not specify how Linux guest ID(s) 73 * need to be generated, our plan is to publish the guidelines for 74 * Linux and other guest operating systems that currently are hosted 75 * on Hyper-V. The implementation here conforms to this yet 76 * unpublished guidelines. 77 * 78 * 79 * Bit(s) 80 * 63 - Indicates if the OS is Open Source or not; 1 is Open Source 81 * 62:56 - Os Type; Linux is 0x100 82 * 55:48 - Distro specific identification 83 * 47:16 - Linux kernel version number 84 * 15:0 - Distro specific identification 85 * 86 * 87 */ 88 89 #define HV_LINUX_VENDOR_ID 0x8100 90 91 /* 92 * Generate the guest ID based on the guideline described above. 93 */ 94 95 static inline __u64 generate_guest_id(__u64 d_info1, __u64 kernel_version, 96 __u64 d_info2) 97 { 98 __u64 guest_id = 0; 99 100 guest_id = (((__u64)HV_LINUX_VENDOR_ID) << 48); 101 guest_id |= (d_info1 << 48); 102 guest_id |= (kernel_version << 16); 103 guest_id |= d_info2; 104 105 return guest_id; 106 } 107 108 109 /* Free the message slot and signal end-of-message if required */ 110 static inline void vmbus_signal_eom(struct hv_message *msg, u32 old_msg_type) 111 { 112 /* 113 * On crash we're reading some other CPU's message page and we need 114 * to be careful: this other CPU may already had cleared the header 115 * and the host may already had delivered some other message there. 116 * In case we blindly write msg->header.message_type we're going 117 * to lose it. We can still lose a message of the same type but 118 * we count on the fact that there can only be one 119 * CHANNELMSG_UNLOAD_RESPONSE and we don't care about other messages 120 * on crash. 121 */ 122 if (cmpxchg(&msg->header.message_type, old_msg_type, 123 HVMSG_NONE) != old_msg_type) 124 return; 125 126 /* 127 * Make sure the write to MessageType (ie set to 128 * HVMSG_NONE) happens before we read the 129 * MessagePending and EOMing. Otherwise, the EOMing 130 * will not deliver any more messages since there is 131 * no empty slot 132 */ 133 mb(); 134 135 if (msg->header.message_flags.msg_pending) { 136 /* 137 * This will cause message queue rescan to 138 * possibly deliver another msg from the 139 * hypervisor 140 */ 141 wrmsrl(HV_X64_MSR_EOM, 0); 142 } 143 } 144 145 #define hv_init_timer(timer, tick) wrmsrl(timer, tick) 146 #define hv_init_timer_config(config, val) wrmsrl(config, val) 147 148 #define hv_get_simp(val) rdmsrl(HV_X64_MSR_SIMP, val) 149 #define hv_set_simp(val) wrmsrl(HV_X64_MSR_SIMP, val) 150 151 #define hv_get_siefp(val) rdmsrl(HV_X64_MSR_SIEFP, val) 152 #define hv_set_siefp(val) wrmsrl(HV_X64_MSR_SIEFP, val) 153 154 #define hv_get_synic_state(val) rdmsrl(HV_X64_MSR_SCONTROL, val) 155 #define hv_set_synic_state(val) wrmsrl(HV_X64_MSR_SCONTROL, val) 156 157 #define hv_get_vp_index(index) rdmsrl(HV_X64_MSR_VP_INDEX, index) 158 159 #define hv_get_synint_state(int_num, val) rdmsrl(int_num, val) 160 #define hv_set_synint_state(int_num, val) wrmsrl(int_num, val) 161 162 void hyperv_callback_vector(void); 163 #ifdef CONFIG_TRACING 164 #define trace_hyperv_callback_vector hyperv_callback_vector 165 #endif 166 void hyperv_vector_handler(struct pt_regs *regs); 167 void hv_setup_vmbus_irq(void (*handler)(void)); 168 void hv_remove_vmbus_irq(void); 169 170 void hv_setup_kexec_handler(void (*handler)(void)); 171 void hv_remove_kexec_handler(void); 172 void hv_setup_crash_handler(void (*handler)(struct pt_regs *regs)); 173 void hv_remove_crash_handler(void); 174 175 #if IS_ENABLED(CONFIG_HYPERV) 176 extern struct clocksource *hyperv_cs; 177 extern void *hv_hypercall_pg; 178 179 static inline u64 hv_do_hypercall(u64 control, void *input, void *output) 180 { 181 u64 input_address = input ? virt_to_phys(input) : 0; 182 u64 output_address = output ? virt_to_phys(output) : 0; 183 u64 hv_status; 184 185 #ifdef CONFIG_X86_64 186 if (!hv_hypercall_pg) 187 return U64_MAX; 188 189 __asm__ __volatile__("mov %4, %%r8\n" 190 CALL_NOSPEC 191 : "=a" (hv_status), ASM_CALL_CONSTRAINT, 192 "+c" (control), "+d" (input_address) 193 : "r" (output_address), 194 THUNK_TARGET(hv_hypercall_pg) 195 : "cc", "memory", "r8", "r9", "r10", "r11"); 196 #else 197 u32 input_address_hi = upper_32_bits(input_address); 198 u32 input_address_lo = lower_32_bits(input_address); 199 u32 output_address_hi = upper_32_bits(output_address); 200 u32 output_address_lo = lower_32_bits(output_address); 201 202 if (!hv_hypercall_pg) 203 return U64_MAX; 204 205 __asm__ __volatile__(CALL_NOSPEC 206 : "=A" (hv_status), 207 "+c" (input_address_lo), ASM_CALL_CONSTRAINT 208 : "A" (control), 209 "b" (input_address_hi), 210 "D"(output_address_hi), "S"(output_address_lo), 211 THUNK_TARGET(hv_hypercall_pg) 212 : "cc", "memory"); 213 #endif /* !x86_64 */ 214 return hv_status; 215 } 216 217 #define HV_HYPERCALL_RESULT_MASK GENMASK_ULL(15, 0) 218 #define HV_HYPERCALL_FAST_BIT BIT(16) 219 #define HV_HYPERCALL_VARHEAD_OFFSET 17 220 #define HV_HYPERCALL_REP_COMP_OFFSET 32 221 #define HV_HYPERCALL_REP_COMP_MASK GENMASK_ULL(43, 32) 222 #define HV_HYPERCALL_REP_START_OFFSET 48 223 #define HV_HYPERCALL_REP_START_MASK GENMASK_ULL(59, 48) 224 225 /* Fast hypercall with 8 bytes of input and no output */ 226 static inline u64 hv_do_fast_hypercall8(u16 code, u64 input1) 227 { 228 u64 hv_status, control = (u64)code | HV_HYPERCALL_FAST_BIT; 229 230 #ifdef CONFIG_X86_64 231 { 232 __asm__ __volatile__(CALL_NOSPEC 233 : "=a" (hv_status), ASM_CALL_CONSTRAINT, 234 "+c" (control), "+d" (input1) 235 : THUNK_TARGET(hv_hypercall_pg) 236 : "cc", "r8", "r9", "r10", "r11"); 237 } 238 #else 239 { 240 u32 input1_hi = upper_32_bits(input1); 241 u32 input1_lo = lower_32_bits(input1); 242 243 __asm__ __volatile__ (CALL_NOSPEC 244 : "=A"(hv_status), 245 "+c"(input1_lo), 246 ASM_CALL_CONSTRAINT 247 : "A" (control), 248 "b" (input1_hi), 249 THUNK_TARGET(hv_hypercall_pg) 250 : "cc", "edi", "esi"); 251 } 252 #endif 253 return hv_status; 254 } 255 256 /* 257 * Rep hypercalls. Callers of this functions are supposed to ensure that 258 * rep_count and varhead_size comply with Hyper-V hypercall definition. 259 */ 260 static inline u64 hv_do_rep_hypercall(u16 code, u16 rep_count, u16 varhead_size, 261 void *input, void *output) 262 { 263 u64 control = code; 264 u64 status; 265 u16 rep_comp; 266 267 control |= (u64)varhead_size << HV_HYPERCALL_VARHEAD_OFFSET; 268 control |= (u64)rep_count << HV_HYPERCALL_REP_COMP_OFFSET; 269 270 do { 271 status = hv_do_hypercall(control, input, output); 272 if ((status & HV_HYPERCALL_RESULT_MASK) != HV_STATUS_SUCCESS) 273 return status; 274 275 /* Bits 32-43 of status have 'Reps completed' data. */ 276 rep_comp = (status & HV_HYPERCALL_REP_COMP_MASK) >> 277 HV_HYPERCALL_REP_COMP_OFFSET; 278 279 control &= ~HV_HYPERCALL_REP_START_MASK; 280 control |= (u64)rep_comp << HV_HYPERCALL_REP_START_OFFSET; 281 282 touch_nmi_watchdog(); 283 } while (rep_comp < rep_count); 284 285 return status; 286 } 287 288 /* 289 * Hypervisor's notion of virtual processor ID is different from 290 * Linux' notion of CPU ID. This information can only be retrieved 291 * in the context of the calling CPU. Setup a map for easy access 292 * to this information. 293 */ 294 extern u32 *hv_vp_index; 295 extern u32 hv_max_vp_index; 296 297 /** 298 * hv_cpu_number_to_vp_number() - Map CPU to VP. 299 * @cpu_number: CPU number in Linux terms 300 * 301 * This function returns the mapping between the Linux processor 302 * number and the hypervisor's virtual processor number, useful 303 * in making hypercalls and such that talk about specific 304 * processors. 305 * 306 * Return: Virtual processor number in Hyper-V terms 307 */ 308 static inline int hv_cpu_number_to_vp_number(int cpu_number) 309 { 310 return hv_vp_index[cpu_number]; 311 } 312 313 void hyperv_init(void); 314 void hyperv_setup_mmu_ops(void); 315 void hyper_alloc_mmu(void); 316 void hyperv_report_panic(struct pt_regs *regs, long err); 317 bool hv_is_hypercall_page_setup(void); 318 void hyperv_cleanup(void); 319 #else /* CONFIG_HYPERV */ 320 static inline void hyperv_init(void) {} 321 static inline bool hv_is_hypercall_page_setup(void) { return false; } 322 static inline void hyperv_cleanup(void) {} 323 static inline void hyperv_setup_mmu_ops(void) {} 324 #endif /* CONFIG_HYPERV */ 325 326 #ifdef CONFIG_HYPERV_TSCPAGE 327 struct ms_hyperv_tsc_page *hv_get_tsc_page(void); 328 static inline u64 hv_read_tsc_page(const struct ms_hyperv_tsc_page *tsc_pg) 329 { 330 u64 scale, offset, cur_tsc; 331 u32 sequence; 332 333 /* 334 * The protocol for reading Hyper-V TSC page is specified in Hypervisor 335 * Top-Level Functional Specification ver. 3.0 and above. To get the 336 * reference time we must do the following: 337 * - READ ReferenceTscSequence 338 * A special '0' value indicates the time source is unreliable and we 339 * need to use something else. The currently published specification 340 * versions (up to 4.0b) contain a mistake and wrongly claim '-1' 341 * instead of '0' as the special value, see commit c35b82ef0294. 342 * - ReferenceTime = 343 * ((RDTSC() * ReferenceTscScale) >> 64) + ReferenceTscOffset 344 * - READ ReferenceTscSequence again. In case its value has changed 345 * since our first reading we need to discard ReferenceTime and repeat 346 * the whole sequence as the hypervisor was updating the page in 347 * between. 348 */ 349 do { 350 sequence = READ_ONCE(tsc_pg->tsc_sequence); 351 if (!sequence) 352 return U64_MAX; 353 /* 354 * Make sure we read sequence before we read other values from 355 * TSC page. 356 */ 357 smp_rmb(); 358 359 scale = READ_ONCE(tsc_pg->tsc_scale); 360 offset = READ_ONCE(tsc_pg->tsc_offset); 361 cur_tsc = rdtsc_ordered(); 362 363 /* 364 * Make sure we read sequence after we read all other values 365 * from TSC page. 366 */ 367 smp_rmb(); 368 369 } while (READ_ONCE(tsc_pg->tsc_sequence) != sequence); 370 371 return mul_u64_u64_shr(cur_tsc, scale, 64) + offset; 372 } 373 374 #else 375 static inline struct ms_hyperv_tsc_page *hv_get_tsc_page(void) 376 { 377 return NULL; 378 } 379 #endif 380 #endif 381