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
13 typedef int (*hyperv_fill_flush_list_func)(
14 struct hv_guest_mapping_flush_list *flush,
15 void *data);
16
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)
21
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
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
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
31 #define hv_get_vp_index(index) rdmsrl(HV_X64_MSR_VP_INDEX, index)
32
33 #define hv_signal_eom() wrmsrl(HV_X64_MSR_EOM, 0)
34
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)
39 #define hv_recommend_using_aeoi() \
40 (!(ms_hyperv.hints & HV_DEPRECATING_AEOI_RECOMMENDED))
41
42 #define hv_get_crash_ctl(val) \
43 rdmsrl(HV_X64_MSR_CRASH_CTL, val)
44
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) \
53 ((val).vdso_clock_mode = VDSO_CLOCKMODE_HVCLOCK)
54 #define hv_enable_vdso_clocksource() \
55 vclocks_set_used(VDSO_CLOCKMODE_HVCLOCK);
56 #define hv_get_raw_timer() rdtsc_ordered()
57 #define hv_get_vector() HYPERVISOR_CALLBACK_VECTOR
58
59 /*
60 * Reference to pv_ops must be inline so objtool
61 * detection of noinstr violations can work correctly.
62 */
hv_setup_sched_clock(void * sched_clock)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
70 void hyperv_vector_handler(struct pt_regs *regs);
71
hv_enable_stimer0_percpu_irq(int irq)72 static inline void hv_enable_stimer0_percpu_irq(int irq) {}
hv_disable_stimer0_percpu_irq(int irq)73 static inline void hv_disable_stimer0_percpu_irq(int irq) {}
74
75
76 #if IS_ENABLED(CONFIG_HYPERV)
77 extern int hyperv_init_cpuhp;
78
79 extern void *hv_hypercall_pg;
80 extern void __percpu **hyperv_pcpu_input_arg;
81
hv_do_hypercall(u64 control,void * input,void * output)82 static inline u64 hv_do_hypercall(u64 control, void *input, void *output)
83 {
84 u64 input_address = input ? virt_to_phys(input) : 0;
85 u64 output_address = output ? virt_to_phys(output) : 0;
86 u64 hv_status;
87
88 #ifdef CONFIG_X86_64
89 if (!hv_hypercall_pg)
90 return U64_MAX;
91
92 __asm__ __volatile__("mov %4, %%r8\n"
93 CALL_NOSPEC
94 : "=a" (hv_status), ASM_CALL_CONSTRAINT,
95 "+c" (control), "+d" (input_address)
96 : "r" (output_address),
97 THUNK_TARGET(hv_hypercall_pg)
98 : "cc", "memory", "r8", "r9", "r10", "r11");
99 #else
100 u32 input_address_hi = upper_32_bits(input_address);
101 u32 input_address_lo = lower_32_bits(input_address);
102 u32 output_address_hi = upper_32_bits(output_address);
103 u32 output_address_lo = lower_32_bits(output_address);
104
105 if (!hv_hypercall_pg)
106 return U64_MAX;
107
108 __asm__ __volatile__(CALL_NOSPEC
109 : "=A" (hv_status),
110 "+c" (input_address_lo), ASM_CALL_CONSTRAINT
111 : "A" (control),
112 "b" (input_address_hi),
113 "D"(output_address_hi), "S"(output_address_lo),
114 THUNK_TARGET(hv_hypercall_pg)
115 : "cc", "memory");
116 #endif /* !x86_64 */
117 return hv_status;
118 }
119
120 /* Fast hypercall with 8 bytes of input and no output */
hv_do_fast_hypercall8(u16 code,u64 input1)121 static inline u64 hv_do_fast_hypercall8(u16 code, u64 input1)
122 {
123 u64 hv_status, control = (u64)code | HV_HYPERCALL_FAST_BIT;
124
125 #ifdef CONFIG_X86_64
126 {
127 __asm__ __volatile__(CALL_NOSPEC
128 : "=a" (hv_status), ASM_CALL_CONSTRAINT,
129 "+c" (control), "+d" (input1)
130 : THUNK_TARGET(hv_hypercall_pg)
131 : "cc", "r8", "r9", "r10", "r11");
132 }
133 #else
134 {
135 u32 input1_hi = upper_32_bits(input1);
136 u32 input1_lo = lower_32_bits(input1);
137
138 __asm__ __volatile__ (CALL_NOSPEC
139 : "=A"(hv_status),
140 "+c"(input1_lo),
141 ASM_CALL_CONSTRAINT
142 : "A" (control),
143 "b" (input1_hi),
144 THUNK_TARGET(hv_hypercall_pg)
145 : "cc", "edi", "esi");
146 }
147 #endif
148 return hv_status;
149 }
150
151 /* Fast hypercall with 16 bytes of input */
hv_do_fast_hypercall16(u16 code,u64 input1,u64 input2)152 static inline u64 hv_do_fast_hypercall16(u16 code, u64 input1, u64 input2)
153 {
154 u64 hv_status, control = (u64)code | HV_HYPERCALL_FAST_BIT;
155
156 #ifdef CONFIG_X86_64
157 {
158 __asm__ __volatile__("mov %4, %%r8\n"
159 CALL_NOSPEC
160 : "=a" (hv_status), ASM_CALL_CONSTRAINT,
161 "+c" (control), "+d" (input1)
162 : "r" (input2),
163 THUNK_TARGET(hv_hypercall_pg)
164 : "cc", "r8", "r9", "r10", "r11");
165 }
166 #else
167 {
168 u32 input1_hi = upper_32_bits(input1);
169 u32 input1_lo = lower_32_bits(input1);
170 u32 input2_hi = upper_32_bits(input2);
171 u32 input2_lo = lower_32_bits(input2);
172
173 __asm__ __volatile__ (CALL_NOSPEC
174 : "=A"(hv_status),
175 "+c"(input1_lo), ASM_CALL_CONSTRAINT
176 : "A" (control), "b" (input1_hi),
177 "D"(input2_hi), "S"(input2_lo),
178 THUNK_TARGET(hv_hypercall_pg)
179 : "cc");
180 }
181 #endif
182 return hv_status;
183 }
184
185 /*
186 * Rep hypercalls. Callers of this functions are supposed to ensure that
187 * rep_count and varhead_size comply with Hyper-V hypercall definition.
188 */
hv_do_rep_hypercall(u16 code,u16 rep_count,u16 varhead_size,void * input,void * output)189 static inline u64 hv_do_rep_hypercall(u16 code, u16 rep_count, u16 varhead_size,
190 void *input, void *output)
191 {
192 u64 control = code;
193 u64 status;
194 u16 rep_comp;
195
196 control |= (u64)varhead_size << HV_HYPERCALL_VARHEAD_OFFSET;
197 control |= (u64)rep_count << HV_HYPERCALL_REP_COMP_OFFSET;
198
199 do {
200 status = hv_do_hypercall(control, input, output);
201 if ((status & HV_HYPERCALL_RESULT_MASK) != HV_STATUS_SUCCESS)
202 return status;
203
204 /* Bits 32-43 of status have 'Reps completed' data. */
205 rep_comp = (status & HV_HYPERCALL_REP_COMP_MASK) >>
206 HV_HYPERCALL_REP_COMP_OFFSET;
207
208 control &= ~HV_HYPERCALL_REP_START_MASK;
209 control |= (u64)rep_comp << HV_HYPERCALL_REP_START_OFFSET;
210
211 touch_nmi_watchdog();
212 } while (rep_comp < rep_count);
213
214 return status;
215 }
216
217 extern struct hv_vp_assist_page **hv_vp_assist_page;
218
hv_get_vp_assist_page(unsigned int cpu)219 static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu)
220 {
221 if (!hv_vp_assist_page)
222 return NULL;
223
224 return hv_vp_assist_page[cpu];
225 }
226
227 void __init hyperv_init(void);
228 void hyperv_setup_mmu_ops(void);
229 void *hv_alloc_hyperv_page(void);
230 void *hv_alloc_hyperv_zeroed_page(void);
231 void hv_free_hyperv_page(unsigned long addr);
232 void set_hv_tscchange_cb(void (*cb)(void));
233 void clear_hv_tscchange_cb(void);
234 void hyperv_stop_tsc_emulation(void);
235 int hyperv_flush_guest_mapping(u64 as);
236 int hyperv_flush_guest_mapping_range(u64 as,
237 hyperv_fill_flush_list_func fill_func, void *data);
238 int hyperv_fill_flush_guest_mapping_list(
239 struct hv_guest_mapping_flush_list *flush,
240 u64 start_gfn, u64 end_gfn);
241
242 #ifdef CONFIG_X86_64
243 void hv_apic_init(void);
244 void __init hv_init_spinlocks(void);
245 bool hv_vcpu_is_preempted(int vcpu);
246 #else
hv_apic_init(void)247 static inline void hv_apic_init(void) {}
248 #endif
249
250 #else /* CONFIG_HYPERV */
hyperv_init(void)251 static inline void hyperv_init(void) {}
hyperv_setup_mmu_ops(void)252 static inline void hyperv_setup_mmu_ops(void) {}
hv_alloc_hyperv_page(void)253 static inline void *hv_alloc_hyperv_page(void) { return NULL; }
hv_free_hyperv_page(unsigned long addr)254 static inline void hv_free_hyperv_page(unsigned long addr) {}
set_hv_tscchange_cb(void (* cb)(void))255 static inline void set_hv_tscchange_cb(void (*cb)(void)) {}
clear_hv_tscchange_cb(void)256 static inline void clear_hv_tscchange_cb(void) {}
hyperv_stop_tsc_emulation(void)257 static inline void hyperv_stop_tsc_emulation(void) {};
hv_get_vp_assist_page(unsigned int cpu)258 static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu)
259 {
260 return NULL;
261 }
hyperv_flush_guest_mapping(u64 as)262 static inline int hyperv_flush_guest_mapping(u64 as) { return -1; }
hyperv_flush_guest_mapping_range(u64 as,hyperv_fill_flush_list_func fill_func,void * data)263 static inline int hyperv_flush_guest_mapping_range(u64 as,
264 hyperv_fill_flush_list_func fill_func, void *data)
265 {
266 return -1;
267 }
268 #endif /* CONFIG_HYPERV */
269
270
271 #include <asm-generic/mshyperv.h>
272
273 #endif
274