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 <asm/io.h> 8 #include <asm/hyperv-tlfs.h> 9 #include <asm/nospec-branch.h> 10 11 typedef int (*hyperv_fill_flush_list_func)( 12 struct hv_guest_mapping_flush_list *flush, 13 void *data); 14 15 #define hv_init_timer(timer, tick) \ 16 wrmsrl(HV_X64_MSR_STIMER0_COUNT + (2*timer), tick) 17 #define hv_init_timer_config(timer, val) \ 18 wrmsrl(HV_X64_MSR_STIMER0_CONFIG + (2*timer), val) 19 20 #define hv_get_simp(val) rdmsrl(HV_X64_MSR_SIMP, val) 21 #define hv_set_simp(val) wrmsrl(HV_X64_MSR_SIMP, val) 22 23 #define hv_get_siefp(val) rdmsrl(HV_X64_MSR_SIEFP, val) 24 #define hv_set_siefp(val) wrmsrl(HV_X64_MSR_SIEFP, val) 25 26 #define hv_get_synic_state(val) rdmsrl(HV_X64_MSR_SCONTROL, val) 27 #define hv_set_synic_state(val) wrmsrl(HV_X64_MSR_SCONTROL, val) 28 29 #define hv_get_vp_index(index) rdmsrl(HV_X64_MSR_VP_INDEX, index) 30 31 #define hv_signal_eom() wrmsrl(HV_X64_MSR_EOM, 0) 32 33 #define hv_get_synint_state(int_num, val) \ 34 rdmsrl(HV_X64_MSR_SINT0 + int_num, val) 35 #define hv_set_synint_state(int_num, val) \ 36 wrmsrl(HV_X64_MSR_SINT0 + int_num, val) 37 38 #define hv_get_crash_ctl(val) \ 39 rdmsrl(HV_X64_MSR_CRASH_CTL, val) 40 41 #define hv_get_time_ref_count(val) \ 42 rdmsrl(HV_X64_MSR_TIME_REF_COUNT, val) 43 44 #define hv_get_reference_tsc(val) \ 45 rdmsrl(HV_X64_MSR_REFERENCE_TSC, val) 46 #define hv_set_reference_tsc(val) \ 47 wrmsrl(HV_X64_MSR_REFERENCE_TSC, val) 48 #define hv_set_clocksource_vdso(val) \ 49 ((val).vdso_clock_mode = VDSO_CLOCKMODE_HVCLOCK) 50 #define hv_enable_vdso_clocksource() \ 51 vclocks_set_used(VDSO_CLOCKMODE_HVCLOCK); 52 #define hv_get_raw_timer() rdtsc_ordered() 53 54 void hyperv_callback_vector(void); 55 void hyperv_reenlightenment_vector(void); 56 #ifdef CONFIG_TRACING 57 #define trace_hyperv_callback_vector hyperv_callback_vector 58 #endif 59 void hyperv_vector_handler(struct pt_regs *regs); 60 61 /* 62 * Routines for stimer0 Direct Mode handling. 63 * On x86/x64, there are no percpu actions to take. 64 */ 65 void hv_stimer0_vector_handler(struct pt_regs *regs); 66 void hv_stimer0_callback_vector(void); 67 68 static inline void hv_enable_stimer0_percpu_irq(int irq) {} 69 static inline void hv_disable_stimer0_percpu_irq(int irq) {} 70 71 72 #if IS_ENABLED(CONFIG_HYPERV) 73 extern void *hv_hypercall_pg; 74 extern void __percpu **hyperv_pcpu_input_arg; 75 76 static inline u64 hv_do_hypercall(u64 control, void *input, void *output) 77 { 78 u64 input_address = input ? virt_to_phys(input) : 0; 79 u64 output_address = output ? virt_to_phys(output) : 0; 80 u64 hv_status; 81 82 #ifdef CONFIG_X86_64 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 /* Fast hypercall with 8 bytes of input and no output */ 115 static inline u64 hv_do_fast_hypercall8(u16 code, u64 input1) 116 { 117 u64 hv_status, control = (u64)code | HV_HYPERCALL_FAST_BIT; 118 119 #ifdef CONFIG_X86_64 120 { 121 __asm__ __volatile__(CALL_NOSPEC 122 : "=a" (hv_status), ASM_CALL_CONSTRAINT, 123 "+c" (control), "+d" (input1) 124 : THUNK_TARGET(hv_hypercall_pg) 125 : "cc", "r8", "r9", "r10", "r11"); 126 } 127 #else 128 { 129 u32 input1_hi = upper_32_bits(input1); 130 u32 input1_lo = lower_32_bits(input1); 131 132 __asm__ __volatile__ (CALL_NOSPEC 133 : "=A"(hv_status), 134 "+c"(input1_lo), 135 ASM_CALL_CONSTRAINT 136 : "A" (control), 137 "b" (input1_hi), 138 THUNK_TARGET(hv_hypercall_pg) 139 : "cc", "edi", "esi"); 140 } 141 #endif 142 return hv_status; 143 } 144 145 /* Fast hypercall with 16 bytes of input */ 146 static inline u64 hv_do_fast_hypercall16(u16 code, u64 input1, u64 input2) 147 { 148 u64 hv_status, control = (u64)code | HV_HYPERCALL_FAST_BIT; 149 150 #ifdef CONFIG_X86_64 151 { 152 __asm__ __volatile__("mov %4, %%r8\n" 153 CALL_NOSPEC 154 : "=a" (hv_status), ASM_CALL_CONSTRAINT, 155 "+c" (control), "+d" (input1) 156 : "r" (input2), 157 THUNK_TARGET(hv_hypercall_pg) 158 : "cc", "r8", "r9", "r10", "r11"); 159 } 160 #else 161 { 162 u32 input1_hi = upper_32_bits(input1); 163 u32 input1_lo = lower_32_bits(input1); 164 u32 input2_hi = upper_32_bits(input2); 165 u32 input2_lo = lower_32_bits(input2); 166 167 __asm__ __volatile__ (CALL_NOSPEC 168 : "=A"(hv_status), 169 "+c"(input1_lo), ASM_CALL_CONSTRAINT 170 : "A" (control), "b" (input1_hi), 171 "D"(input2_hi), "S"(input2_lo), 172 THUNK_TARGET(hv_hypercall_pg) 173 : "cc"); 174 } 175 #endif 176 return hv_status; 177 } 178 179 /* 180 * Rep hypercalls. Callers of this functions are supposed to ensure that 181 * rep_count and varhead_size comply with Hyper-V hypercall definition. 182 */ 183 static inline u64 hv_do_rep_hypercall(u16 code, u16 rep_count, u16 varhead_size, 184 void *input, void *output) 185 { 186 u64 control = code; 187 u64 status; 188 u16 rep_comp; 189 190 control |= (u64)varhead_size << HV_HYPERCALL_VARHEAD_OFFSET; 191 control |= (u64)rep_count << HV_HYPERCALL_REP_COMP_OFFSET; 192 193 do { 194 status = hv_do_hypercall(control, input, output); 195 if ((status & HV_HYPERCALL_RESULT_MASK) != HV_STATUS_SUCCESS) 196 return status; 197 198 /* Bits 32-43 of status have 'Reps completed' data. */ 199 rep_comp = (status & HV_HYPERCALL_REP_COMP_MASK) >> 200 HV_HYPERCALL_REP_COMP_OFFSET; 201 202 control &= ~HV_HYPERCALL_REP_START_MASK; 203 control |= (u64)rep_comp << HV_HYPERCALL_REP_START_OFFSET; 204 205 touch_nmi_watchdog(); 206 } while (rep_comp < rep_count); 207 208 return status; 209 } 210 211 extern struct hv_vp_assist_page **hv_vp_assist_page; 212 213 static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu) 214 { 215 if (!hv_vp_assist_page) 216 return NULL; 217 218 return hv_vp_assist_page[cpu]; 219 } 220 221 void __init hyperv_init(void); 222 void hyperv_setup_mmu_ops(void); 223 void *hv_alloc_hyperv_page(void); 224 void *hv_alloc_hyperv_zeroed_page(void); 225 void hv_free_hyperv_page(unsigned long addr); 226 void hyperv_reenlightenment_intr(struct pt_regs *regs); 227 void set_hv_tscchange_cb(void (*cb)(void)); 228 void clear_hv_tscchange_cb(void); 229 void hyperv_stop_tsc_emulation(void); 230 int hyperv_flush_guest_mapping(u64 as); 231 int hyperv_flush_guest_mapping_range(u64 as, 232 hyperv_fill_flush_list_func fill_func, void *data); 233 int hyperv_fill_flush_guest_mapping_list( 234 struct hv_guest_mapping_flush_list *flush, 235 u64 start_gfn, u64 end_gfn); 236 237 #ifdef CONFIG_X86_64 238 void hv_apic_init(void); 239 void __init hv_init_spinlocks(void); 240 bool hv_vcpu_is_preempted(int vcpu); 241 #else 242 static inline void hv_apic_init(void) {} 243 #endif 244 245 #else /* CONFIG_HYPERV */ 246 static inline void hyperv_init(void) {} 247 static inline void hyperv_setup_mmu_ops(void) {} 248 static inline void *hv_alloc_hyperv_page(void) { return NULL; } 249 static inline void hv_free_hyperv_page(unsigned long addr) {} 250 static inline void set_hv_tscchange_cb(void (*cb)(void)) {} 251 static inline void clear_hv_tscchange_cb(void) {} 252 static inline void hyperv_stop_tsc_emulation(void) {}; 253 static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu) 254 { 255 return NULL; 256 } 257 static inline int hyperv_flush_guest_mapping(u64 as) { return -1; } 258 static inline int hyperv_flush_guest_mapping_range(u64 as, 259 hyperv_fill_flush_list_func fill_func, void *data) 260 { 261 return -1; 262 } 263 #endif /* CONFIG_HYPERV */ 264 265 266 #include <asm-generic/mshyperv.h> 267 268 #endif 269