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