Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
H. Peter Anvin | e08cae4 | 2010-05-07 16:57:28 -0700 | [diff] [blame] | 2 | #ifndef _ASM_X86_MSHYPER_H |
| 3 | #define _ASM_X86_MSHYPER_H |
Ky Srinivasan | a2a47c6 | 2010-05-06 12:08:41 -0700 | [diff] [blame] | 4 | |
H. Peter Anvin | e08cae4 | 2010-05-07 16:57:28 -0700 | [diff] [blame] | 5 | #include <linux/types.h> |
Vitaly Kuznetsov | 806c892 | 2017-08-02 18:09:17 +0200 | [diff] [blame] | 6 | #include <linux/nmi.h> |
Boqun Feng | 1cf106d | 2020-02-10 11:39:53 +0800 | [diff] [blame] | 7 | #include <linux/msi.h> |
Vitaly Kuznetsov | fc53662 | 2017-08-02 18:09:14 +0200 | [diff] [blame] | 8 | #include <asm/io.h> |
Vitaly Kuznetsov | 5a48580 | 2018-03-20 15:02:05 +0100 | [diff] [blame] | 9 | #include <asm/hyperv-tlfs.h> |
David Woodhouse | e70e589 | 2018-01-11 21:46:30 +0000 | [diff] [blame] | 10 | #include <asm/nospec-branch.h> |
Michael Kelley | b9d8cf2 | 2020-08-09 18:29:51 -0700 | [diff] [blame] | 11 | #include <asm/paravirt.h> |
H. Peter Anvin | e08cae4 | 2010-05-07 16:57:28 -0700 | [diff] [blame] | 12 | |
Lan Tianyu | cc4edae4 | 2018-12-06 21:21:05 +0800 | [diff] [blame] | 13 | typedef int (*hyperv_fill_flush_list_func)( |
| 14 | struct hv_guest_mapping_flush_list *flush, |
| 15 | void *data); |
| 16 | |
Michael Kelley | 619a4c8 | 2018-06-05 13:37:53 -0700 | [diff] [blame] | 17 | #define hv_init_timer(timer, tick) \ |
| 18 | wrmsrl(HV_X64_MSR_STIMER0_COUNT + (2*timer), tick) |
| 19 | #define hv_init_timer_config(timer, val) \ |
| 20 | wrmsrl(HV_X64_MSR_STIMER0_CONFIG + (2*timer), val) |
K. Y. Srinivasan | d5116b4 | 2017-01-19 11:51:51 -0700 | [diff] [blame] | 21 | |
K. Y. Srinivasan | 155e4a2 | 2017-01-19 11:51:54 -0700 | [diff] [blame] | 22 | #define hv_get_simp(val) rdmsrl(HV_X64_MSR_SIMP, val) |
| 23 | #define hv_set_simp(val) wrmsrl(HV_X64_MSR_SIMP, val) |
| 24 | |
K. Y. Srinivasan | 8e307bf | 2017-01-19 11:51:55 -0700 | [diff] [blame] | 25 | #define hv_get_siefp(val) rdmsrl(HV_X64_MSR_SIEFP, val) |
| 26 | #define hv_set_siefp(val) wrmsrl(HV_X64_MSR_SIEFP, val) |
| 27 | |
K. Y. Srinivasan | 06d1d98 | 2017-01-19 11:51:56 -0700 | [diff] [blame] | 28 | #define hv_get_synic_state(val) rdmsrl(HV_X64_MSR_SCONTROL, val) |
| 29 | #define hv_set_synic_state(val) wrmsrl(HV_X64_MSR_SCONTROL, val) |
| 30 | |
K. Y. Srinivasan | 7297ff0 | 2017-01-19 11:51:57 -0700 | [diff] [blame] | 31 | #define hv_get_vp_index(index) rdmsrl(HV_X64_MSR_VP_INDEX, index) |
| 32 | |
Michael Kelley | 765e33f | 2019-05-30 00:14:00 +0000 | [diff] [blame] | 33 | #define hv_signal_eom() wrmsrl(HV_X64_MSR_EOM, 0) |
| 34 | |
Michael Kelley | 619a4c8 | 2018-06-05 13:37:53 -0700 | [diff] [blame] | 35 | #define hv_get_synint_state(int_num, val) \ |
| 36 | rdmsrl(HV_X64_MSR_SINT0 + int_num, val) |
| 37 | #define hv_set_synint_state(int_num, val) \ |
| 38 | wrmsrl(HV_X64_MSR_SINT0 + int_num, val) |
Michael Kelley | 2ddddd0 | 2020-04-20 09:49:26 -0700 | [diff] [blame] | 39 | #define hv_recommend_using_aeoi() \ |
| 40 | (!(ms_hyperv.hints & HV_DEPRECATING_AEOI_RECOMMENDED)) |
K. Y. Srinivasan | 37e11d5 | 2017-01-19 11:51:58 -0700 | [diff] [blame] | 41 | |
Sunil Muthuswamy | 9d9c965 | 2018-07-28 21:58:47 +0000 | [diff] [blame] | 42 | #define hv_get_crash_ctl(val) \ |
| 43 | rdmsrl(HV_X64_MSR_CRASH_CTL, val) |
Seiji Aguchi | cf910e8 | 2013-06-20 11:46:53 -0400 | [diff] [blame] | 44 | |
Michael Kelley | dd2cb34 | 2019-07-01 04:26:06 +0000 | [diff] [blame] | 45 | #define hv_get_time_ref_count(val) \ |
| 46 | rdmsrl(HV_X64_MSR_TIME_REF_COUNT, val) |
| 47 | |
| 48 | #define hv_get_reference_tsc(val) \ |
| 49 | rdmsrl(HV_X64_MSR_REFERENCE_TSC, val) |
| 50 | #define hv_set_reference_tsc(val) \ |
| 51 | wrmsrl(HV_X64_MSR_REFERENCE_TSC, val) |
| 52 | #define hv_set_clocksource_vdso(val) \ |
Thomas Gleixner | b95a8a2 | 2020-02-07 13:38:56 +0100 | [diff] [blame] | 53 | ((val).vdso_clock_mode = VDSO_CLOCKMODE_HVCLOCK) |
Thomas Gleixner | eec399d | 2020-02-07 13:38:54 +0100 | [diff] [blame] | 54 | #define hv_enable_vdso_clocksource() \ |
Thomas Gleixner | b95a8a2 | 2020-02-07 13:38:56 +0100 | [diff] [blame] | 55 | vclocks_set_used(VDSO_CLOCKMODE_HVCLOCK); |
Michael Kelley | dd2cb34 | 2019-07-01 04:26:06 +0000 | [diff] [blame] | 56 | #define hv_get_raw_timer() rdtsc_ordered() |
Michael Kelley | 626b901 | 2020-08-14 12:45:04 -0700 | [diff] [blame^] | 57 | #define hv_get_vector() HYPERVISOR_CALLBACK_VECTOR |
Michael Kelley | dd2cb34 | 2019-07-01 04:26:06 +0000 | [diff] [blame] | 58 | |
Michael Kelley | b9d8cf2 | 2020-08-09 18:29:51 -0700 | [diff] [blame] | 59 | /* |
| 60 | * Reference to pv_ops must be inline so objtool |
| 61 | * detection of noinstr violations can work correctly. |
| 62 | */ |
| 63 | static __always_inline void hv_setup_sched_clock(void *sched_clock) |
| 64 | { |
| 65 | #ifdef CONFIG_PARAVIRT |
| 66 | pv_ops.time.sched_clock = sched_clock; |
| 67 | #endif |
| 68 | } |
| 69 | |
K. Y. Srinivasan | bc2b033 | 2013-02-03 17:22:39 -0800 | [diff] [blame] | 70 | void hyperv_vector_handler(struct pt_regs *regs); |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 71 | |
Michael Kelley | 248e742 | 2018-03-04 22:17:18 -0700 | [diff] [blame] | 72 | static inline void hv_enable_stimer0_percpu_irq(int irq) {} |
| 73 | static inline void hv_disable_stimer0_percpu_irq(int irq) {} |
| 74 | |
| 75 | |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 76 | #if IS_ENABLED(CONFIG_HYPERV) |
Vitaly Kuznetsov | fc53662 | 2017-08-02 18:09:14 +0200 | [diff] [blame] | 77 | extern void *hv_hypercall_pg; |
K. Y. Srinivasan | 68bb7bf | 2018-05-16 14:53:31 -0700 | [diff] [blame] | 78 | extern void __percpu **hyperv_pcpu_input_arg; |
Vitaly Kuznetsov | fc53662 | 2017-08-02 18:09:14 +0200 | [diff] [blame] | 79 | |
| 80 | static inline u64 hv_do_hypercall(u64 control, void *input, void *output) |
| 81 | { |
| 82 | u64 input_address = input ? virt_to_phys(input) : 0; |
| 83 | u64 output_address = output ? virt_to_phys(output) : 0; |
| 84 | u64 hv_status; |
Vitaly Kuznetsov | fc53662 | 2017-08-02 18:09:14 +0200 | [diff] [blame] | 85 | |
| 86 | #ifdef CONFIG_X86_64 |
| 87 | if (!hv_hypercall_pg) |
| 88 | return U64_MAX; |
| 89 | |
| 90 | __asm__ __volatile__("mov %4, %%r8\n" |
David Woodhouse | e70e589 | 2018-01-11 21:46:30 +0000 | [diff] [blame] | 91 | CALL_NOSPEC |
Josh Poimboeuf | f5caf62 | 2017-09-20 16:24:33 -0500 | [diff] [blame] | 92 | : "=a" (hv_status), ASM_CALL_CONSTRAINT, |
Vitaly Kuznetsov | fc53662 | 2017-08-02 18:09:14 +0200 | [diff] [blame] | 93 | "+c" (control), "+d" (input_address) |
David Woodhouse | e70e589 | 2018-01-11 21:46:30 +0000 | [diff] [blame] | 94 | : "r" (output_address), |
| 95 | THUNK_TARGET(hv_hypercall_pg) |
Vitaly Kuznetsov | fc53662 | 2017-08-02 18:09:14 +0200 | [diff] [blame] | 96 | : "cc", "memory", "r8", "r9", "r10", "r11"); |
| 97 | #else |
| 98 | u32 input_address_hi = upper_32_bits(input_address); |
| 99 | u32 input_address_lo = lower_32_bits(input_address); |
| 100 | u32 output_address_hi = upper_32_bits(output_address); |
| 101 | u32 output_address_lo = lower_32_bits(output_address); |
| 102 | |
| 103 | if (!hv_hypercall_pg) |
| 104 | return U64_MAX; |
| 105 | |
David Woodhouse | e70e589 | 2018-01-11 21:46:30 +0000 | [diff] [blame] | 106 | __asm__ __volatile__(CALL_NOSPEC |
Vitaly Kuznetsov | fc53662 | 2017-08-02 18:09:14 +0200 | [diff] [blame] | 107 | : "=A" (hv_status), |
Josh Poimboeuf | f5caf62 | 2017-09-20 16:24:33 -0500 | [diff] [blame] | 108 | "+c" (input_address_lo), ASM_CALL_CONSTRAINT |
Vitaly Kuznetsov | fc53662 | 2017-08-02 18:09:14 +0200 | [diff] [blame] | 109 | : "A" (control), |
| 110 | "b" (input_address_hi), |
| 111 | "D"(output_address_hi), "S"(output_address_lo), |
David Woodhouse | e70e589 | 2018-01-11 21:46:30 +0000 | [diff] [blame] | 112 | THUNK_TARGET(hv_hypercall_pg) |
Vitaly Kuznetsov | fc53662 | 2017-08-02 18:09:14 +0200 | [diff] [blame] | 113 | : "cc", "memory"); |
| 114 | #endif /* !x86_64 */ |
| 115 | return hv_status; |
| 116 | } |
Vitaly Kuznetsov | dee863b | 2017-02-04 09:57:13 -0700 | [diff] [blame] | 117 | |
Vitaly Kuznetsov | 6a8edbd | 2017-08-02 18:09:15 +0200 | [diff] [blame] | 118 | /* Fast hypercall with 8 bytes of input and no output */ |
| 119 | static inline u64 hv_do_fast_hypercall8(u16 code, u64 input1) |
| 120 | { |
| 121 | u64 hv_status, control = (u64)code | HV_HYPERCALL_FAST_BIT; |
Vitaly Kuznetsov | 6a8edbd | 2017-08-02 18:09:15 +0200 | [diff] [blame] | 122 | |
| 123 | #ifdef CONFIG_X86_64 |
| 124 | { |
David Woodhouse | e70e589 | 2018-01-11 21:46:30 +0000 | [diff] [blame] | 125 | __asm__ __volatile__(CALL_NOSPEC |
Josh Poimboeuf | f5caf62 | 2017-09-20 16:24:33 -0500 | [diff] [blame] | 126 | : "=a" (hv_status), ASM_CALL_CONSTRAINT, |
Vitaly Kuznetsov | 6a8edbd | 2017-08-02 18:09:15 +0200 | [diff] [blame] | 127 | "+c" (control), "+d" (input1) |
David Woodhouse | e70e589 | 2018-01-11 21:46:30 +0000 | [diff] [blame] | 128 | : THUNK_TARGET(hv_hypercall_pg) |
Vitaly Kuznetsov | 6a8edbd | 2017-08-02 18:09:15 +0200 | [diff] [blame] | 129 | : "cc", "r8", "r9", "r10", "r11"); |
| 130 | } |
| 131 | #else |
| 132 | { |
| 133 | u32 input1_hi = upper_32_bits(input1); |
| 134 | u32 input1_lo = lower_32_bits(input1); |
| 135 | |
David Woodhouse | e70e589 | 2018-01-11 21:46:30 +0000 | [diff] [blame] | 136 | __asm__ __volatile__ (CALL_NOSPEC |
Vitaly Kuznetsov | 6a8edbd | 2017-08-02 18:09:15 +0200 | [diff] [blame] | 137 | : "=A"(hv_status), |
| 138 | "+c"(input1_lo), |
Josh Poimboeuf | f5caf62 | 2017-09-20 16:24:33 -0500 | [diff] [blame] | 139 | ASM_CALL_CONSTRAINT |
Vitaly Kuznetsov | 6a8edbd | 2017-08-02 18:09:15 +0200 | [diff] [blame] | 140 | : "A" (control), |
| 141 | "b" (input1_hi), |
David Woodhouse | e70e589 | 2018-01-11 21:46:30 +0000 | [diff] [blame] | 142 | THUNK_TARGET(hv_hypercall_pg) |
Vitaly Kuznetsov | 6a8edbd | 2017-08-02 18:09:15 +0200 | [diff] [blame] | 143 | : "cc", "edi", "esi"); |
| 144 | } |
| 145 | #endif |
| 146 | return hv_status; |
| 147 | } |
| 148 | |
Vitaly Kuznetsov | 53e5296 | 2018-06-22 19:06:22 +0200 | [diff] [blame] | 149 | /* Fast hypercall with 16 bytes of input */ |
| 150 | static inline u64 hv_do_fast_hypercall16(u16 code, u64 input1, u64 input2) |
| 151 | { |
| 152 | u64 hv_status, control = (u64)code | HV_HYPERCALL_FAST_BIT; |
| 153 | |
| 154 | #ifdef CONFIG_X86_64 |
| 155 | { |
| 156 | __asm__ __volatile__("mov %4, %%r8\n" |
| 157 | CALL_NOSPEC |
| 158 | : "=a" (hv_status), ASM_CALL_CONSTRAINT, |
| 159 | "+c" (control), "+d" (input1) |
| 160 | : "r" (input2), |
| 161 | THUNK_TARGET(hv_hypercall_pg) |
| 162 | : "cc", "r8", "r9", "r10", "r11"); |
| 163 | } |
| 164 | #else |
| 165 | { |
| 166 | u32 input1_hi = upper_32_bits(input1); |
| 167 | u32 input1_lo = lower_32_bits(input1); |
| 168 | u32 input2_hi = upper_32_bits(input2); |
| 169 | u32 input2_lo = lower_32_bits(input2); |
| 170 | |
| 171 | __asm__ __volatile__ (CALL_NOSPEC |
| 172 | : "=A"(hv_status), |
| 173 | "+c"(input1_lo), ASM_CALL_CONSTRAINT |
| 174 | : "A" (control), "b" (input1_hi), |
| 175 | "D"(input2_hi), "S"(input2_lo), |
| 176 | THUNK_TARGET(hv_hypercall_pg) |
| 177 | : "cc"); |
| 178 | } |
| 179 | #endif |
Yi Wang | b42967d | 2018-10-29 15:17:31 +0800 | [diff] [blame] | 180 | return hv_status; |
Vitaly Kuznetsov | 53e5296 | 2018-06-22 19:06:22 +0200 | [diff] [blame] | 181 | } |
| 182 | |
Vitaly Kuznetsov | 806c892 | 2017-08-02 18:09:17 +0200 | [diff] [blame] | 183 | /* |
| 184 | * Rep hypercalls. Callers of this functions are supposed to ensure that |
| 185 | * rep_count and varhead_size comply with Hyper-V hypercall definition. |
| 186 | */ |
| 187 | static inline u64 hv_do_rep_hypercall(u16 code, u16 rep_count, u16 varhead_size, |
| 188 | void *input, void *output) |
| 189 | { |
| 190 | u64 control = code; |
| 191 | u64 status; |
| 192 | u16 rep_comp; |
| 193 | |
| 194 | control |= (u64)varhead_size << HV_HYPERCALL_VARHEAD_OFFSET; |
| 195 | control |= (u64)rep_count << HV_HYPERCALL_REP_COMP_OFFSET; |
| 196 | |
| 197 | do { |
| 198 | status = hv_do_hypercall(control, input, output); |
| 199 | if ((status & HV_HYPERCALL_RESULT_MASK) != HV_STATUS_SUCCESS) |
| 200 | return status; |
| 201 | |
| 202 | /* Bits 32-43 of status have 'Reps completed' data. */ |
| 203 | rep_comp = (status & HV_HYPERCALL_REP_COMP_MASK) >> |
| 204 | HV_HYPERCALL_REP_COMP_OFFSET; |
| 205 | |
| 206 | control &= ~HV_HYPERCALL_REP_START_MASK; |
| 207 | control |= (u64)rep_comp << HV_HYPERCALL_REP_START_OFFSET; |
| 208 | |
| 209 | touch_nmi_watchdog(); |
| 210 | } while (rep_comp < rep_count); |
| 211 | |
| 212 | return status; |
| 213 | } |
| 214 | |
Vitaly Kuznetsov | a46d15c | 2018-03-20 15:02:08 +0100 | [diff] [blame] | 215 | extern struct hv_vp_assist_page **hv_vp_assist_page; |
| 216 | |
| 217 | static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu) |
| 218 | { |
| 219 | if (!hv_vp_assist_page) |
| 220 | return NULL; |
| 221 | |
| 222 | return hv_vp_assist_page[cpu]; |
| 223 | } |
Vitaly Kuznetsov | 7415aea | 2017-08-02 18:09:18 +0200 | [diff] [blame] | 224 | |
K. Y. Srinivasan | 6b48cb5 | 2018-05-16 14:53:30 -0700 | [diff] [blame] | 225 | void __init hyperv_init(void); |
Vitaly Kuznetsov | 2ffd9e3 | 2017-08-02 18:09:19 +0200 | [diff] [blame] | 226 | void hyperv_setup_mmu_ops(void); |
Maya Nakamura | 8c3e44b | 2019-07-12 08:21:25 +0000 | [diff] [blame] | 227 | void *hv_alloc_hyperv_page(void); |
Himadri Pandya | fa36dcd | 2019-07-30 09:49:43 +0000 | [diff] [blame] | 228 | void *hv_alloc_hyperv_zeroed_page(void); |
Maya Nakamura | 8c3e44b | 2019-07-12 08:21:25 +0000 | [diff] [blame] | 229 | void hv_free_hyperv_page(unsigned long addr); |
Vitaly Kuznetsov | 9328626 | 2018-01-24 14:23:33 +0100 | [diff] [blame] | 230 | void set_hv_tscchange_cb(void (*cb)(void)); |
| 231 | void clear_hv_tscchange_cb(void); |
| 232 | void hyperv_stop_tsc_emulation(void); |
Tianyu Lan | eb914cf | 2018-07-19 08:40:06 +0000 | [diff] [blame] | 233 | int hyperv_flush_guest_mapping(u64 as); |
Lan Tianyu | cc4edae4 | 2018-12-06 21:21:05 +0800 | [diff] [blame] | 234 | int hyperv_flush_guest_mapping_range(u64 as, |
| 235 | hyperv_fill_flush_list_func fill_func, void *data); |
| 236 | int hyperv_fill_flush_guest_mapping_list( |
| 237 | struct hv_guest_mapping_flush_list *flush, |
| 238 | u64 start_gfn, u64 end_gfn); |
Thomas Gleixner | 2d2ccf24 | 2018-05-19 21:22:48 +0200 | [diff] [blame] | 239 | |
| 240 | #ifdef CONFIG_X86_64 |
K. Y. Srinivasan | 6b48cb5 | 2018-05-16 14:53:30 -0700 | [diff] [blame] | 241 | void hv_apic_init(void); |
Yi Sun | 3a025de | 2018-10-08 16:29:34 +0800 | [diff] [blame] | 242 | void __init hv_init_spinlocks(void); |
| 243 | bool hv_vcpu_is_preempted(int vcpu); |
Thomas Gleixner | 2d2ccf24 | 2018-05-19 21:22:48 +0200 | [diff] [blame] | 244 | #else |
| 245 | static inline void hv_apic_init(void) {} |
| 246 | #endif |
| 247 | |
Boqun Feng | 1cf106d | 2020-02-10 11:39:53 +0800 | [diff] [blame] | 248 | static inline void hv_set_msi_entry_from_desc(union hv_msi_entry *msi_entry, |
| 249 | struct msi_desc *msi_desc) |
| 250 | { |
| 251 | msi_entry->address = msi_desc->msg.address_lo; |
| 252 | msi_entry->data = msi_desc->msg.data; |
| 253 | } |
| 254 | |
Vitaly Kuznetsov | 79cadff | 2017-08-02 18:09:13 +0200 | [diff] [blame] | 255 | #else /* CONFIG_HYPERV */ |
| 256 | static inline void hyperv_init(void) {} |
Vitaly Kuznetsov | 2ffd9e3 | 2017-08-02 18:09:19 +0200 | [diff] [blame] | 257 | static inline void hyperv_setup_mmu_ops(void) {} |
Maya Nakamura | 8c3e44b | 2019-07-12 08:21:25 +0000 | [diff] [blame] | 258 | static inline void *hv_alloc_hyperv_page(void) { return NULL; } |
| 259 | static inline void hv_free_hyperv_page(unsigned long addr) {} |
Vitaly Kuznetsov | 9328626 | 2018-01-24 14:23:33 +0100 | [diff] [blame] | 260 | static inline void set_hv_tscchange_cb(void (*cb)(void)) {} |
| 261 | static inline void clear_hv_tscchange_cb(void) {} |
| 262 | static inline void hyperv_stop_tsc_emulation(void) {}; |
Vitaly Kuznetsov | a46d15c | 2018-03-20 15:02:08 +0100 | [diff] [blame] | 263 | static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu) |
| 264 | { |
| 265 | return NULL; |
| 266 | } |
Tianyu Lan | eb914cf | 2018-07-19 08:40:06 +0000 | [diff] [blame] | 267 | static inline int hyperv_flush_guest_mapping(u64 as) { return -1; } |
Lan Tianyu | cc4edae4 | 2018-12-06 21:21:05 +0800 | [diff] [blame] | 268 | static inline int hyperv_flush_guest_mapping_range(u64 as, |
| 269 | hyperv_fill_flush_list_func fill_func, void *data) |
| 270 | { |
| 271 | return -1; |
| 272 | } |
Vitaly Kuznetsov | 79cadff | 2017-08-02 18:09:13 +0200 | [diff] [blame] | 273 | #endif /* CONFIG_HYPERV */ |
| 274 | |
Michael Kelley | 765e33f | 2019-05-30 00:14:00 +0000 | [diff] [blame] | 275 | |
| 276 | #include <asm-generic/mshyperv.h> |
| 277 | |
Ky Srinivasan | a2a47c6 | 2010-05-06 12:08:41 -0700 | [diff] [blame] | 278 | #endif |