xref: /linux/include/asm-generic/mshyperv.h (revision 8a922b7728a93d837954315c98b84f6b78de0c4f)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 
3 /*
4  * Linux-specific definitions for managing interactions with Microsoft's
5  * Hyper-V hypervisor. The definitions in this file are architecture
6  * independent. See arch/<arch>/include/asm/mshyperv.h for definitions
7  * that are specific to architecture <arch>.
8  *
9  * Definitions that are specified in the Hyper-V Top Level Functional
10  * Spec (TLFS) should not go in this file, but should instead go in
11  * hyperv-tlfs.h.
12  *
13  * Copyright (C) 2019, Microsoft, Inc.
14  *
15  * Author : Michael Kelley <mikelley@microsoft.com>
16  */
17 
18 #ifndef _ASM_GENERIC_MSHYPERV_H
19 #define _ASM_GENERIC_MSHYPERV_H
20 
21 #include <linux/types.h>
22 #include <linux/atomic.h>
23 #include <linux/bitops.h>
24 #include <linux/cpumask.h>
25 #include <linux/nmi.h>
26 #include <asm/ptrace.h>
27 #include <asm/hyperv-tlfs.h>
28 
29 struct ms_hyperv_info {
30 	u32 features;
31 	u32 priv_high;
32 	u32 misc_features;
33 	u32 hints;
34 	u32 nested_features;
35 	u32 max_vp_index;
36 	u32 max_lp_index;
37 	u32 isolation_config_a;
38 	union {
39 		u32 isolation_config_b;
40 		struct {
41 			u32 cvm_type : 4;
42 			u32 reserved1 : 1;
43 			u32 shared_gpa_boundary_active : 1;
44 			u32 shared_gpa_boundary_bits : 6;
45 			u32 reserved2 : 20;
46 		};
47 	};
48 	u64 shared_gpa_boundary;
49 };
50 extern struct ms_hyperv_info ms_hyperv;
51 extern bool hv_nested;
52 
53 extern void * __percpu *hyperv_pcpu_input_arg;
54 extern void * __percpu *hyperv_pcpu_output_arg;
55 
56 extern u64 hv_do_hypercall(u64 control, void *inputaddr, void *outputaddr);
57 extern u64 hv_do_fast_hypercall8(u16 control, u64 input8);
58 extern bool hv_isolation_type_snp(void);
59 
60 /* Helper functions that provide a consistent pattern for checking Hyper-V hypercall status. */
61 static inline int hv_result(u64 status)
62 {
63 	return status & HV_HYPERCALL_RESULT_MASK;
64 }
65 
66 static inline bool hv_result_success(u64 status)
67 {
68 	return hv_result(status) == HV_STATUS_SUCCESS;
69 }
70 
71 static inline unsigned int hv_repcomp(u64 status)
72 {
73 	/* Bits [43:32] of status have 'Reps completed' data. */
74 	return (status & HV_HYPERCALL_REP_COMP_MASK) >>
75 			 HV_HYPERCALL_REP_COMP_OFFSET;
76 }
77 
78 /*
79  * Rep hypercalls. Callers of this functions are supposed to ensure that
80  * rep_count and varhead_size comply with Hyper-V hypercall definition.
81  */
82 static inline u64 hv_do_rep_hypercall(u16 code, u16 rep_count, u16 varhead_size,
83 				      void *input, void *output)
84 {
85 	u64 control = code;
86 	u64 status;
87 	u16 rep_comp;
88 
89 	control |= (u64)varhead_size << HV_HYPERCALL_VARHEAD_OFFSET;
90 	control |= (u64)rep_count << HV_HYPERCALL_REP_COMP_OFFSET;
91 
92 	do {
93 		status = hv_do_hypercall(control, input, output);
94 		if (!hv_result_success(status))
95 			return status;
96 
97 		rep_comp = hv_repcomp(status);
98 
99 		control &= ~HV_HYPERCALL_REP_START_MASK;
100 		control |= (u64)rep_comp << HV_HYPERCALL_REP_START_OFFSET;
101 
102 		touch_nmi_watchdog();
103 	} while (rep_comp < rep_count);
104 
105 	return status;
106 }
107 
108 /* Generate the guest OS identifier as described in the Hyper-V TLFS */
109 static inline u64 hv_generate_guest_id(u64 kernel_version)
110 {
111 	u64 guest_id;
112 
113 	guest_id = (((u64)HV_LINUX_VENDOR_ID) << 48);
114 	guest_id |= (kernel_version << 16);
115 
116 	return guest_id;
117 }
118 
119 /* Free the message slot and signal end-of-message if required */
120 static inline void vmbus_signal_eom(struct hv_message *msg, u32 old_msg_type)
121 {
122 	/*
123 	 * On crash we're reading some other CPU's message page and we need
124 	 * to be careful: this other CPU may already had cleared the header
125 	 * and the host may already had delivered some other message there.
126 	 * In case we blindly write msg->header.message_type we're going
127 	 * to lose it. We can still lose a message of the same type but
128 	 * we count on the fact that there can only be one
129 	 * CHANNELMSG_UNLOAD_RESPONSE and we don't care about other messages
130 	 * on crash.
131 	 */
132 	if (cmpxchg(&msg->header.message_type, old_msg_type,
133 		    HVMSG_NONE) != old_msg_type)
134 		return;
135 
136 	/*
137 	 * The cmxchg() above does an implicit memory barrier to
138 	 * ensure the write to MessageType (ie set to
139 	 * HVMSG_NONE) happens before we read the
140 	 * MessagePending and EOMing. Otherwise, the EOMing
141 	 * will not deliver any more messages since there is
142 	 * no empty slot
143 	 */
144 	if (msg->header.message_flags.msg_pending) {
145 		/*
146 		 * This will cause message queue rescan to
147 		 * possibly deliver another msg from the
148 		 * hypervisor
149 		 */
150 		hv_set_register(HV_REGISTER_EOM, 0);
151 	}
152 }
153 
154 void hv_setup_vmbus_handler(void (*handler)(void));
155 void hv_remove_vmbus_handler(void);
156 void hv_setup_stimer0_handler(void (*handler)(void));
157 void hv_remove_stimer0_handler(void);
158 
159 void hv_setup_kexec_handler(void (*handler)(void));
160 void hv_remove_kexec_handler(void);
161 void hv_setup_crash_handler(void (*handler)(struct pt_regs *regs));
162 void hv_remove_crash_handler(void);
163 
164 extern int vmbus_interrupt;
165 extern int vmbus_irq;
166 
167 extern bool hv_root_partition;
168 
169 #if IS_ENABLED(CONFIG_HYPERV)
170 /*
171  * Hypervisor's notion of virtual processor ID is different from
172  * Linux' notion of CPU ID. This information can only be retrieved
173  * in the context of the calling CPU. Setup a map for easy access
174  * to this information.
175  */
176 extern u32 *hv_vp_index;
177 extern u32 hv_max_vp_index;
178 
179 extern u64 (*hv_read_reference_counter)(void);
180 
181 /* Sentinel value for an uninitialized entry in hv_vp_index array */
182 #define VP_INVAL	U32_MAX
183 
184 int __init hv_common_init(void);
185 void __init hv_common_free(void);
186 int hv_common_cpu_init(unsigned int cpu);
187 int hv_common_cpu_die(unsigned int cpu);
188 
189 void *hv_alloc_hyperv_page(void);
190 void *hv_alloc_hyperv_zeroed_page(void);
191 void hv_free_hyperv_page(unsigned long addr);
192 
193 /**
194  * hv_cpu_number_to_vp_number() - Map CPU to VP.
195  * @cpu_number: CPU number in Linux terms
196  *
197  * This function returns the mapping between the Linux processor
198  * number and the hypervisor's virtual processor number, useful
199  * in making hypercalls and such that talk about specific
200  * processors.
201  *
202  * Return: Virtual processor number in Hyper-V terms
203  */
204 static inline int hv_cpu_number_to_vp_number(int cpu_number)
205 {
206 	return hv_vp_index[cpu_number];
207 }
208 
209 static inline int __cpumask_to_vpset(struct hv_vpset *vpset,
210 				    const struct cpumask *cpus,
211 				    bool exclude_self)
212 {
213 	int cpu, vcpu, vcpu_bank, vcpu_offset, nr_bank = 1;
214 	int this_cpu = smp_processor_id();
215 	int max_vcpu_bank = hv_max_vp_index / HV_VCPUS_PER_SPARSE_BANK;
216 
217 	/* vpset.valid_bank_mask can represent up to HV_MAX_SPARSE_VCPU_BANKS banks */
218 	if (max_vcpu_bank >= HV_MAX_SPARSE_VCPU_BANKS)
219 		return 0;
220 
221 	/*
222 	 * Clear all banks up to the maximum possible bank as hv_tlb_flush_ex
223 	 * structs are not cleared between calls, we risk flushing unneeded
224 	 * vCPUs otherwise.
225 	 */
226 	for (vcpu_bank = 0; vcpu_bank <= max_vcpu_bank; vcpu_bank++)
227 		vpset->bank_contents[vcpu_bank] = 0;
228 
229 	/*
230 	 * Some banks may end up being empty but this is acceptable.
231 	 */
232 	for_each_cpu(cpu, cpus) {
233 		if (exclude_self && cpu == this_cpu)
234 			continue;
235 		vcpu = hv_cpu_number_to_vp_number(cpu);
236 		if (vcpu == VP_INVAL)
237 			return -1;
238 		vcpu_bank = vcpu / HV_VCPUS_PER_SPARSE_BANK;
239 		vcpu_offset = vcpu % HV_VCPUS_PER_SPARSE_BANK;
240 		__set_bit(vcpu_offset, (unsigned long *)
241 			  &vpset->bank_contents[vcpu_bank]);
242 		if (vcpu_bank >= nr_bank)
243 			nr_bank = vcpu_bank + 1;
244 	}
245 	vpset->valid_bank_mask = GENMASK_ULL(nr_bank - 1, 0);
246 	return nr_bank;
247 }
248 
249 static inline int cpumask_to_vpset(struct hv_vpset *vpset,
250 				    const struct cpumask *cpus)
251 {
252 	return __cpumask_to_vpset(vpset, cpus, false);
253 }
254 
255 static inline int cpumask_to_vpset_noself(struct hv_vpset *vpset,
256 				    const struct cpumask *cpus)
257 {
258 	WARN_ON_ONCE(preemptible());
259 	return __cpumask_to_vpset(vpset, cpus, true);
260 }
261 
262 void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die);
263 bool hv_is_hyperv_initialized(void);
264 bool hv_is_hibernation_supported(void);
265 enum hv_isolation_type hv_get_isolation_type(void);
266 bool hv_is_isolation_supported(void);
267 bool hv_isolation_type_snp(void);
268 u64 hv_ghcb_hypercall(u64 control, void *input, void *output, u32 input_size);
269 void hyperv_cleanup(void);
270 bool hv_query_ext_cap(u64 cap_query);
271 void hv_setup_dma_ops(struct device *dev, bool coherent);
272 void *hv_map_memory(void *addr, unsigned long size);
273 void hv_unmap_memory(void *addr);
274 #else /* CONFIG_HYPERV */
275 static inline bool hv_is_hyperv_initialized(void) { return false; }
276 static inline bool hv_is_hibernation_supported(void) { return false; }
277 static inline void hyperv_cleanup(void) {}
278 static inline bool hv_is_isolation_supported(void) { return false; }
279 static inline enum hv_isolation_type hv_get_isolation_type(void)
280 {
281 	return HV_ISOLATION_TYPE_NONE;
282 }
283 #endif /* CONFIG_HYPERV */
284 
285 #endif
286