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