xref: /linux/arch/x86/include/asm/mshyperv.h (revision 0e9b70c1e3623fa110fb6be553e644524228ef60)
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 #include <asm/mshyperv.h>
13 
14 union hv_ghcb;
15 
16 DECLARE_STATIC_KEY_FALSE(isolation_type_snp);
17 
18 typedef int (*hyperv_fill_flush_list_func)(
19 		struct hv_guest_mapping_flush_list *flush,
20 		void *data);
21 
22 void hyperv_vector_handler(struct pt_regs *regs);
23 
24 #if IS_ENABLED(CONFIG_HYPERV)
25 extern int hyperv_init_cpuhp;
26 
27 extern void *hv_hypercall_pg;
28 
29 extern u64 hv_current_partition_id;
30 
31 extern union hv_ghcb * __percpu *hv_ghcb_pg;
32 
33 int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages);
34 int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id);
35 int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u32 flags);
36 
37 static inline u64 hv_do_hypercall(u64 control, void *input, void *output)
38 {
39 	u64 input_address = input ? virt_to_phys(input) : 0;
40 	u64 output_address = output ? virt_to_phys(output) : 0;
41 	u64 hv_status;
42 
43 #ifdef CONFIG_X86_64
44 	if (!hv_hypercall_pg)
45 		return U64_MAX;
46 
47 	__asm__ __volatile__("mov %4, %%r8\n"
48 			     CALL_NOSPEC
49 			     : "=a" (hv_status), ASM_CALL_CONSTRAINT,
50 			       "+c" (control), "+d" (input_address)
51 			     :  "r" (output_address),
52 				THUNK_TARGET(hv_hypercall_pg)
53 			     : "cc", "memory", "r8", "r9", "r10", "r11");
54 #else
55 	u32 input_address_hi = upper_32_bits(input_address);
56 	u32 input_address_lo = lower_32_bits(input_address);
57 	u32 output_address_hi = upper_32_bits(output_address);
58 	u32 output_address_lo = lower_32_bits(output_address);
59 
60 	if (!hv_hypercall_pg)
61 		return U64_MAX;
62 
63 	__asm__ __volatile__(CALL_NOSPEC
64 			     : "=A" (hv_status),
65 			       "+c" (input_address_lo), ASM_CALL_CONSTRAINT
66 			     : "A" (control),
67 			       "b" (input_address_hi),
68 			       "D"(output_address_hi), "S"(output_address_lo),
69 			       THUNK_TARGET(hv_hypercall_pg)
70 			     : "cc", "memory");
71 #endif /* !x86_64 */
72 	return hv_status;
73 }
74 
75 /* Hypercall to the L0 hypervisor */
76 static inline u64 hv_do_nested_hypercall(u64 control, void *input, void *output)
77 {
78 	return hv_do_hypercall(control | HV_HYPERCALL_NESTED, input, output);
79 }
80 
81 /* Fast hypercall with 8 bytes of input and no output */
82 static inline u64 _hv_do_fast_hypercall8(u64 control, u64 input1)
83 {
84 	u64 hv_status;
85 
86 #ifdef CONFIG_X86_64
87 	{
88 		__asm__ __volatile__(CALL_NOSPEC
89 				     : "=a" (hv_status), ASM_CALL_CONSTRAINT,
90 				       "+c" (control), "+d" (input1)
91 				     : THUNK_TARGET(hv_hypercall_pg)
92 				     : "cc", "r8", "r9", "r10", "r11");
93 	}
94 #else
95 	{
96 		u32 input1_hi = upper_32_bits(input1);
97 		u32 input1_lo = lower_32_bits(input1);
98 
99 		__asm__ __volatile__ (CALL_NOSPEC
100 				      : "=A"(hv_status),
101 					"+c"(input1_lo),
102 					ASM_CALL_CONSTRAINT
103 				      :	"A" (control),
104 					"b" (input1_hi),
105 					THUNK_TARGET(hv_hypercall_pg)
106 				      : "cc", "edi", "esi");
107 	}
108 #endif
109 		return hv_status;
110 }
111 
112 static inline u64 hv_do_fast_hypercall8(u16 code, u64 input1)
113 {
114 	u64 control = (u64)code | HV_HYPERCALL_FAST_BIT;
115 
116 	return _hv_do_fast_hypercall8(control, input1);
117 }
118 
119 static inline u64 hv_do_fast_nested_hypercall8(u16 code, u64 input1)
120 {
121 	u64 control = (u64)code | HV_HYPERCALL_FAST_BIT | HV_HYPERCALL_NESTED;
122 
123 	return _hv_do_fast_hypercall8(control, input1);
124 }
125 
126 /* Fast hypercall with 16 bytes of input */
127 static inline u64 _hv_do_fast_hypercall16(u64 control, u64 input1, u64 input2)
128 {
129 	u64 hv_status;
130 
131 #ifdef CONFIG_X86_64
132 	{
133 		__asm__ __volatile__("mov %4, %%r8\n"
134 				     CALL_NOSPEC
135 				     : "=a" (hv_status), ASM_CALL_CONSTRAINT,
136 				       "+c" (control), "+d" (input1)
137 				     : "r" (input2),
138 				       THUNK_TARGET(hv_hypercall_pg)
139 				     : "cc", "r8", "r9", "r10", "r11");
140 	}
141 #else
142 	{
143 		u32 input1_hi = upper_32_bits(input1);
144 		u32 input1_lo = lower_32_bits(input1);
145 		u32 input2_hi = upper_32_bits(input2);
146 		u32 input2_lo = lower_32_bits(input2);
147 
148 		__asm__ __volatile__ (CALL_NOSPEC
149 				      : "=A"(hv_status),
150 					"+c"(input1_lo), ASM_CALL_CONSTRAINT
151 				      :	"A" (control), "b" (input1_hi),
152 					"D"(input2_hi), "S"(input2_lo),
153 					THUNK_TARGET(hv_hypercall_pg)
154 				      : "cc");
155 	}
156 #endif
157 	return hv_status;
158 }
159 
160 static inline u64 hv_do_fast_hypercall16(u16 code, u64 input1, u64 input2)
161 {
162 	u64 control = (u64)code | HV_HYPERCALL_FAST_BIT;
163 
164 	return _hv_do_fast_hypercall16(control, input1, input2);
165 }
166 
167 static inline u64 hv_do_fast_nested_hypercall16(u16 code, u64 input1, u64 input2)
168 {
169 	u64 control = (u64)code | HV_HYPERCALL_FAST_BIT | HV_HYPERCALL_NESTED;
170 
171 	return _hv_do_fast_hypercall16(control, input1, input2);
172 }
173 
174 extern struct hv_vp_assist_page **hv_vp_assist_page;
175 
176 static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu)
177 {
178 	if (!hv_vp_assist_page)
179 		return NULL;
180 
181 	return hv_vp_assist_page[cpu];
182 }
183 
184 void __init hyperv_init(void);
185 void hyperv_setup_mmu_ops(void);
186 void set_hv_tscchange_cb(void (*cb)(void));
187 void clear_hv_tscchange_cb(void);
188 void hyperv_stop_tsc_emulation(void);
189 int hyperv_flush_guest_mapping(u64 as);
190 int hyperv_flush_guest_mapping_range(u64 as,
191 		hyperv_fill_flush_list_func fill_func, void *data);
192 int hyperv_fill_flush_guest_mapping_list(
193 		struct hv_guest_mapping_flush_list *flush,
194 		u64 start_gfn, u64 end_gfn);
195 
196 #ifdef CONFIG_X86_64
197 void hv_apic_init(void);
198 void __init hv_init_spinlocks(void);
199 bool hv_vcpu_is_preempted(int vcpu);
200 #else
201 static inline void hv_apic_init(void) {}
202 #endif
203 
204 struct irq_domain *hv_create_pci_msi_domain(void);
205 
206 int hv_map_ioapic_interrupt(int ioapic_id, bool level, int vcpu, int vector,
207 		struct hv_interrupt_entry *entry);
208 int hv_unmap_ioapic_interrupt(int ioapic_id, struct hv_interrupt_entry *entry);
209 int hv_set_mem_host_visibility(unsigned long addr, int numpages, bool visible);
210 
211 #ifdef CONFIG_AMD_MEM_ENCRYPT
212 void hv_ghcb_msr_write(u64 msr, u64 value);
213 void hv_ghcb_msr_read(u64 msr, u64 *value);
214 bool hv_ghcb_negotiate_protocol(void);
215 void hv_ghcb_terminate(unsigned int set, unsigned int reason);
216 #else
217 static inline void hv_ghcb_msr_write(u64 msr, u64 value) {}
218 static inline void hv_ghcb_msr_read(u64 msr, u64 *value) {}
219 static inline bool hv_ghcb_negotiate_protocol(void) { return false; }
220 static inline void hv_ghcb_terminate(unsigned int set, unsigned int reason) {}
221 #endif
222 
223 extern bool hv_isolation_type_snp(void);
224 
225 static inline bool hv_is_synic_reg(unsigned int reg)
226 {
227 	return (reg >= HV_REGISTER_SCONTROL) &&
228 	       (reg <= HV_REGISTER_SINT15);
229 }
230 
231 static inline bool hv_is_sint_reg(unsigned int reg)
232 {
233 	return (reg >= HV_REGISTER_SINT0) &&
234 	       (reg <= HV_REGISTER_SINT15);
235 }
236 
237 u64 hv_get_register(unsigned int reg);
238 void hv_set_register(unsigned int reg, u64 value);
239 u64 hv_get_non_nested_register(unsigned int reg);
240 void hv_set_non_nested_register(unsigned int reg, u64 value);
241 
242 #else /* CONFIG_HYPERV */
243 static inline void hyperv_init(void) {}
244 static inline void hyperv_setup_mmu_ops(void) {}
245 static inline void set_hv_tscchange_cb(void (*cb)(void)) {}
246 static inline void clear_hv_tscchange_cb(void) {}
247 static inline void hyperv_stop_tsc_emulation(void) {};
248 static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu)
249 {
250 	return NULL;
251 }
252 static inline int hyperv_flush_guest_mapping(u64 as) { return -1; }
253 static inline int hyperv_flush_guest_mapping_range(u64 as,
254 		hyperv_fill_flush_list_func fill_func, void *data)
255 {
256 	return -1;
257 }
258 static inline void hv_set_register(unsigned int reg, u64 value) { }
259 static inline u64 hv_get_register(unsigned int reg) { return 0; }
260 static inline void hv_set_non_nested_register(unsigned int reg, u64 value) { }
261 static inline u64 hv_get_non_nested_register(unsigned int reg) { return 0; }
262 static inline int hv_set_mem_host_visibility(unsigned long addr, int numpages,
263 					     bool visible)
264 {
265 	return -1;
266 }
267 #endif /* CONFIG_HYPERV */
268 
269 
270 #include <asm-generic/mshyperv.h>
271 
272 #endif
273