1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_X86_MSHYPER_H
3 #define _ASM_X86_MSHYPER_H
5 #include <linux/types.h>
6 #include <linux/atomic.h>
9 #include <asm/hyperv.h>
10 #include <asm/nospec-branch.h>
13 * The below CPUID leaves are present if VersionAndFeatures.HypervisorPresent
14 * is set by CPUID(HVCPUID_VERSION_FEATURES).
16 enum hv_cpuid_function {
17 HVCPUID_VERSION_FEATURES = 0x00000001,
18 HVCPUID_VENDOR_MAXFUNCTION = 0x40000000,
19 HVCPUID_INTERFACE = 0x40000001,
22 * The remaining functions depend on the value of
25 HVCPUID_VERSION = 0x40000002,
26 HVCPUID_FEATURES = 0x40000003,
27 HVCPUID_ENLIGHTENMENT_INFO = 0x40000004,
28 HVCPUID_IMPLEMENTATION_LIMITS = 0x40000005,
31 struct ms_hyperv_info {
39 extern struct ms_hyperv_info ms_hyperv;
42 * Declare the MSR used to setup pages used to communicate with the hypervisor.
44 union hv_x64_msr_hypercall_contents {
49 u64 guest_physical_address:52;
57 struct ms_hyperv_tsc_page {
58 volatile u32 tsc_sequence;
60 volatile u64 tsc_scale;
61 volatile s64 tsc_offset;
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:
70 * msdn.microsoft.com/en-us/library/windows/hardware/ff542653%28v=vs.85%29.aspx
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.
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
89 #define HV_LINUX_VENDOR_ID 0x8100
92 * Generate the guest ID based on the guideline described above.
95 static inline __u64 generate_guest_id(__u64 d_info1, __u64 kernel_version,
100 guest_id = (((__u64)HV_LINUX_VENDOR_ID) << 48);
101 guest_id |= (d_info1 << 48);
102 guest_id |= (kernel_version << 16);
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)
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
122 if (cmpxchg(&msg->header.message_type, old_msg_type,
123 HVMSG_NONE) != old_msg_type)
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
135 if (msg->header.message_flags.msg_pending) {
137 * This will cause message queue rescan to
138 * possibly deliver another msg from the
141 wrmsrl(HV_X64_MSR_EOM, 0);
145 #define hv_init_timer(timer, tick) wrmsrl(timer, tick)
146 #define hv_init_timer_config(config, val) wrmsrl(config, val)
148 #define hv_get_simp(val) rdmsrl(HV_X64_MSR_SIMP, val)
149 #define hv_set_simp(val) wrmsrl(HV_X64_MSR_SIMP, val)
151 #define hv_get_siefp(val) rdmsrl(HV_X64_MSR_SIEFP, val)
152 #define hv_set_siefp(val) wrmsrl(HV_X64_MSR_SIEFP, val)
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)
157 #define hv_get_vp_index(index) rdmsrl(HV_X64_MSR_VP_INDEX, index)
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)
162 void hyperv_callback_vector(void);
163 void hyperv_reenlightenment_vector(void);
164 #ifdef CONFIG_TRACING
165 #define trace_hyperv_callback_vector hyperv_callback_vector
167 void hyperv_vector_handler(struct pt_regs *regs);
168 void hv_setup_vmbus_irq(void (*handler)(void));
169 void hv_remove_vmbus_irq(void);
171 void hv_setup_kexec_handler(void (*handler)(void));
172 void hv_remove_kexec_handler(void);
173 void hv_setup_crash_handler(void (*handler)(struct pt_regs *regs));
174 void hv_remove_crash_handler(void);
177 * Routines for stimer0 Direct Mode handling.
178 * On x86/x64, there are no percpu actions to take.
180 void hv_stimer0_vector_handler(struct pt_regs *regs);
181 void hv_stimer0_callback_vector(void);
182 int hv_setup_stimer0_irq(int *irq, int *vector, void (*handler)(void));
183 void hv_remove_stimer0_irq(int irq);
185 static inline void hv_enable_stimer0_percpu_irq(int irq) {}
186 static inline void hv_disable_stimer0_percpu_irq(int irq) {}
189 #if IS_ENABLED(CONFIG_HYPERV)
190 extern struct clocksource *hyperv_cs;
191 extern void *hv_hypercall_pg;
193 static inline u64 hv_do_hypercall(u64 control, void *input, void *output)
195 u64 input_address = input ? virt_to_phys(input) : 0;
196 u64 output_address = output ? virt_to_phys(output) : 0;
200 if (!hv_hypercall_pg)
203 __asm__ __volatile__("mov %4, %%r8\n"
205 : "=a" (hv_status), ASM_CALL_CONSTRAINT,
206 "+c" (control), "+d" (input_address)
207 : "r" (output_address),
208 THUNK_TARGET(hv_hypercall_pg)
209 : "cc", "memory", "r8", "r9", "r10", "r11");
211 u32 input_address_hi = upper_32_bits(input_address);
212 u32 input_address_lo = lower_32_bits(input_address);
213 u32 output_address_hi = upper_32_bits(output_address);
214 u32 output_address_lo = lower_32_bits(output_address);
216 if (!hv_hypercall_pg)
219 __asm__ __volatile__(CALL_NOSPEC
221 "+c" (input_address_lo), ASM_CALL_CONSTRAINT
223 "b" (input_address_hi),
224 "D"(output_address_hi), "S"(output_address_lo),
225 THUNK_TARGET(hv_hypercall_pg)
231 #define HV_HYPERCALL_RESULT_MASK GENMASK_ULL(15, 0)
232 #define HV_HYPERCALL_FAST_BIT BIT(16)
233 #define HV_HYPERCALL_VARHEAD_OFFSET 17
234 #define HV_HYPERCALL_REP_COMP_OFFSET 32
235 #define HV_HYPERCALL_REP_COMP_MASK GENMASK_ULL(43, 32)
236 #define HV_HYPERCALL_REP_START_OFFSET 48
237 #define HV_HYPERCALL_REP_START_MASK GENMASK_ULL(59, 48)
239 /* Fast hypercall with 8 bytes of input and no output */
240 static inline u64 hv_do_fast_hypercall8(u16 code, u64 input1)
242 u64 hv_status, control = (u64)code | HV_HYPERCALL_FAST_BIT;
246 __asm__ __volatile__(CALL_NOSPEC
247 : "=a" (hv_status), ASM_CALL_CONSTRAINT,
248 "+c" (control), "+d" (input1)
249 : THUNK_TARGET(hv_hypercall_pg)
250 : "cc", "r8", "r9", "r10", "r11");
254 u32 input1_hi = upper_32_bits(input1);
255 u32 input1_lo = lower_32_bits(input1);
257 __asm__ __volatile__ (CALL_NOSPEC
263 THUNK_TARGET(hv_hypercall_pg)
264 : "cc", "edi", "esi");
271 * Rep hypercalls. Callers of this functions are supposed to ensure that
272 * rep_count and varhead_size comply with Hyper-V hypercall definition.
274 static inline u64 hv_do_rep_hypercall(u16 code, u16 rep_count, u16 varhead_size,
275 void *input, void *output)
281 control |= (u64)varhead_size << HV_HYPERCALL_VARHEAD_OFFSET;
282 control |= (u64)rep_count << HV_HYPERCALL_REP_COMP_OFFSET;
285 status = hv_do_hypercall(control, input, output);
286 if ((status & HV_HYPERCALL_RESULT_MASK) != HV_STATUS_SUCCESS)
289 /* Bits 32-43 of status have 'Reps completed' data. */
290 rep_comp = (status & HV_HYPERCALL_REP_COMP_MASK) >>
291 HV_HYPERCALL_REP_COMP_OFFSET;
293 control &= ~HV_HYPERCALL_REP_START_MASK;
294 control |= (u64)rep_comp << HV_HYPERCALL_REP_START_OFFSET;
296 touch_nmi_watchdog();
297 } while (rep_comp < rep_count);
303 * Hypervisor's notion of virtual processor ID is different from
304 * Linux' notion of CPU ID. This information can only be retrieved
305 * in the context of the calling CPU. Setup a map for easy access
306 * to this information.
308 extern u32 *hv_vp_index;
309 extern u32 hv_max_vp_index;
312 * hv_cpu_number_to_vp_number() - Map CPU to VP.
313 * @cpu_number: CPU number in Linux terms
315 * This function returns the mapping between the Linux processor
316 * number and the hypervisor's virtual processor number, useful
317 * in making hypercalls and such that talk about specific
320 * Return: Virtual processor number in Hyper-V terms
322 static inline int hv_cpu_number_to_vp_number(int cpu_number)
324 return hv_vp_index[cpu_number];
327 void hyperv_init(void);
328 void hyperv_setup_mmu_ops(void);
329 void hyper_alloc_mmu(void);
330 void hyperv_report_panic(struct pt_regs *regs, long err);
331 bool hv_is_hyperv_initialized(void);
332 void hyperv_cleanup(void);
334 void hyperv_reenlightenment_intr(struct pt_regs *regs);
335 void set_hv_tscchange_cb(void (*cb)(void));
336 void clear_hv_tscchange_cb(void);
337 void hyperv_stop_tsc_emulation(void);
338 #else /* CONFIG_HYPERV */
339 static inline void hyperv_init(void) {}
340 static inline bool hv_is_hyperv_initialized(void) { return false; }
341 static inline void hyperv_cleanup(void) {}
342 static inline void hyperv_setup_mmu_ops(void) {}
343 static inline void set_hv_tscchange_cb(void (*cb)(void)) {}
344 static inline void clear_hv_tscchange_cb(void) {}
345 static inline void hyperv_stop_tsc_emulation(void) {};
346 #endif /* CONFIG_HYPERV */
348 #ifdef CONFIG_HYPERV_TSCPAGE
349 struct ms_hyperv_tsc_page *hv_get_tsc_page(void);
350 static inline u64 hv_read_tsc_page_tsc(const struct ms_hyperv_tsc_page *tsc_pg,
357 * The protocol for reading Hyper-V TSC page is specified in Hypervisor
358 * Top-Level Functional Specification ver. 3.0 and above. To get the
359 * reference time we must do the following:
360 * - READ ReferenceTscSequence
361 * A special '0' value indicates the time source is unreliable and we
362 * need to use something else. The currently published specification
363 * versions (up to 4.0b) contain a mistake and wrongly claim '-1'
364 * instead of '0' as the special value, see commit c35b82ef0294.
366 * ((RDTSC() * ReferenceTscScale) >> 64) + ReferenceTscOffset
367 * - READ ReferenceTscSequence again. In case its value has changed
368 * since our first reading we need to discard ReferenceTime and repeat
369 * the whole sequence as the hypervisor was updating the page in
373 sequence = READ_ONCE(tsc_pg->tsc_sequence);
377 * Make sure we read sequence before we read other values from
382 scale = READ_ONCE(tsc_pg->tsc_scale);
383 offset = READ_ONCE(tsc_pg->tsc_offset);
384 *cur_tsc = rdtsc_ordered();
387 * Make sure we read sequence after we read all other values
392 } while (READ_ONCE(tsc_pg->tsc_sequence) != sequence);
394 return mul_u64_u64_shr(*cur_tsc, scale, 64) + offset;
397 static inline u64 hv_read_tsc_page(const struct ms_hyperv_tsc_page *tsc_pg)
401 return hv_read_tsc_page_tsc(tsc_pg, &cur_tsc);
405 static inline struct ms_hyperv_tsc_page *hv_get_tsc_page(void)
410 static inline u64 hv_read_tsc_page_tsc(const struct ms_hyperv_tsc_page *tsc_pg,