1/******************************************************************************
2 * hypercall.h
3 *
4 * Linux-specific hypervisor handling.
5 *
6 * Copyright (c) 2002-2004, K A Fraser
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version 2
10 * as published by the Free Software Foundation; or, when distributed
11 * separately from the Linux kernel or incorporated into other
12 * software packages, subject to the following license:
13 *
14 * Permission is hereby granted, free of charge, to any person obtaining a copy
15 * of this source file (the "Software"), to deal in the Software without
16 * restriction, including without limitation the rights to use, copy, modify,
17 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
18 * and to permit persons to whom the Software is furnished to do so, subject to
19 * the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30 * IN THE SOFTWARE.
31 */
32
33#ifndef _ASM_X86_XEN_HYPERCALL_H
34#define _ASM_X86_XEN_HYPERCALL_H
35
36#include <linux/kernel.h>
37#include <linux/spinlock.h>
38#include <linux/errno.h>
39#include <linux/string.h>
40#include <linux/types.h>
41#include <linux/pgtable.h>
42#include <linux/instrumentation.h>
43
44#include <trace/events/xen.h>
45
46#include <asm/alternative.h>
47#include <asm/page.h>
48#include <asm/smap.h>
49#include <asm/nospec-branch.h>
50
51#include <xen/interface/xen.h>
52#include <xen/interface/sched.h>
53#include <xen/interface/physdev.h>
54#include <xen/interface/platform.h>
55#include <xen/interface/xen-mca.h>
56
57struct xen_dm_op_buf;
58
59/*
60 * The hypercall asms have to meet several constraints:
61 * - Work on 32- and 64-bit.
62 * The two architectures put their arguments in different sets of
63 * registers.
64 *
65 * - Work around asm syntax quirks
66 * It isn't possible to specify one of the rNN registers in a
67 * constraint, so we use explicit register variables to get the
68 * args into the right place.
69 *
70 * - Mark all registers as potentially clobbered
71 * Even unused parameters can be clobbered by the hypervisor, so we
72 * need to make sure gcc knows it.
73 *
74 * - Avoid compiler bugs.
75 * This is the tricky part. Because x86_32 has such a constrained
76 * register set, gcc versions below 4.3 have trouble generating
77 * code when all the arg registers and memory are trashed by the
78 * asm. There are syntactically simpler ways of achieving the
79 * semantics below, but they cause the compiler to crash.
80 *
81 * The only combination I found which works is:
82 * - assign the __argX variables first
83 * - list all actually used parameters as "+r" (__argX)
84 * - clobber the rest
85 *
86 * The result certainly isn't pretty, and it really shows up cpp's
87 * weakness as a macro language. Sorry. (But let's just give thanks
88 * there aren't more than 5 arguments...)
89 */
90
91void xen_hypercall_func(void);
92DECLARE_STATIC_CALL(xen_hypercall, xen_hypercall_func);
93
94#ifdef MODULE
95#define __ADDRESSABLE_xen_hypercall
96#else
97#define __ADDRESSABLE_xen_hypercall \
98 __stringify(.global STATIC_CALL_KEY(xen_hypercall);)
99#endif
100
101#define __HYPERCALL \
102 __ADDRESSABLE_xen_hypercall \
103 __stringify(call STATIC_CALL_TRAMP(xen_hypercall))
104
105#define __HYPERCALL_ENTRY(x) "a" (x)
106
107#ifdef CONFIG_X86_32
108#define __HYPERCALL_RETREG "eax"
109#define __HYPERCALL_ARG1REG "ebx"
110#define __HYPERCALL_ARG2REG "ecx"
111#define __HYPERCALL_ARG3REG "edx"
112#define __HYPERCALL_ARG4REG "esi"
113#define __HYPERCALL_ARG5REG "edi"
114#else
115#define __HYPERCALL_RETREG "rax"
116#define __HYPERCALL_ARG1REG "rdi"
117#define __HYPERCALL_ARG2REG "rsi"
118#define __HYPERCALL_ARG3REG "rdx"
119#define __HYPERCALL_ARG4REG "r10"
120#define __HYPERCALL_ARG5REG "r8"
121#endif
122
123#define __HYPERCALL_DECLS \
124 register unsigned long __res asm(__HYPERCALL_RETREG); \
125 register unsigned long __arg1 asm(__HYPERCALL_ARG1REG) = __arg1; \
126 register unsigned long __arg2 asm(__HYPERCALL_ARG2REG) = __arg2; \
127 register unsigned long __arg3 asm(__HYPERCALL_ARG3REG) = __arg3; \
128 register unsigned long __arg4 asm(__HYPERCALL_ARG4REG) = __arg4; \
129 register unsigned long __arg5 asm(__HYPERCALL_ARG5REG) = __arg5;
130
131#define __HYPERCALL_0PARAM "=r" (__res), ASM_CALL_CONSTRAINT
132#define __HYPERCALL_1PARAM __HYPERCALL_0PARAM, "+r" (__arg1)
133#define __HYPERCALL_2PARAM __HYPERCALL_1PARAM, "+r" (__arg2)
134#define __HYPERCALL_3PARAM __HYPERCALL_2PARAM, "+r" (__arg3)
135#define __HYPERCALL_4PARAM __HYPERCALL_3PARAM, "+r" (__arg4)
136#define __HYPERCALL_5PARAM __HYPERCALL_4PARAM, "+r" (__arg5)
137
138#define __HYPERCALL_0ARG()
139#define __HYPERCALL_1ARG(a1) \
140 __HYPERCALL_0ARG() __arg1 = (unsigned long)(a1);
141#define __HYPERCALL_2ARG(a1,a2) \
142 __HYPERCALL_1ARG(a1) __arg2 = (unsigned long)(a2);
143#define __HYPERCALL_3ARG(a1,a2,a3) \
144 __HYPERCALL_2ARG(a1,a2) __arg3 = (unsigned long)(a3);
145#define __HYPERCALL_4ARG(a1,a2,a3,a4) \
146 __HYPERCALL_3ARG(a1,a2,a3) __arg4 = (unsigned long)(a4);
147#define __HYPERCALL_5ARG(a1,a2,a3,a4,a5) \
148 __HYPERCALL_4ARG(a1,a2,a3,a4) __arg5 = (unsigned long)(a5);
149
150#define __HYPERCALL_CLOBBER5 "memory"
151#define __HYPERCALL_CLOBBER4 __HYPERCALL_CLOBBER5, __HYPERCALL_ARG5REG
152#define __HYPERCALL_CLOBBER3 __HYPERCALL_CLOBBER4, __HYPERCALL_ARG4REG
153#define __HYPERCALL_CLOBBER2 __HYPERCALL_CLOBBER3, __HYPERCALL_ARG3REG
154#define __HYPERCALL_CLOBBER1 __HYPERCALL_CLOBBER2, __HYPERCALL_ARG2REG
155#define __HYPERCALL_CLOBBER0 __HYPERCALL_CLOBBER1, __HYPERCALL_ARG1REG
156
157#define _hypercall0(type, name) \
158({ \
159 __HYPERCALL_DECLS; \
160 __HYPERCALL_0ARG(); \
161 asm volatile (__HYPERCALL \
162 : __HYPERCALL_0PARAM \
163 : __HYPERCALL_ENTRY(__HYPERVISOR_ ## name) \
164 : __HYPERCALL_CLOBBER0); \
165 (type)__res; \
166})
167
168#define _hypercall1(type, name, a1) \
169({ \
170 __HYPERCALL_DECLS; \
171 __HYPERCALL_1ARG(a1); \
172 asm volatile (__HYPERCALL \
173 : __HYPERCALL_1PARAM \
174 : __HYPERCALL_ENTRY(__HYPERVISOR_ ## name) \
175 : __HYPERCALL_CLOBBER1); \
176 (type)__res; \
177})
178
179#define _hypercall2(type, name, a1, a2) \
180({ \
181 __HYPERCALL_DECLS; \
182 __HYPERCALL_2ARG(a1, a2); \
183 asm volatile (__HYPERCALL \
184 : __HYPERCALL_2PARAM \
185 : __HYPERCALL_ENTRY(__HYPERVISOR_ ## name) \
186 : __HYPERCALL_CLOBBER2); \
187 (type)__res; \
188})
189
190#define _hypercall3(type, name, a1, a2, a3) \
191({ \
192 __HYPERCALL_DECLS; \
193 __HYPERCALL_3ARG(a1, a2, a3); \
194 asm volatile (__HYPERCALL \
195 : __HYPERCALL_3PARAM \
196 : __HYPERCALL_ENTRY(__HYPERVISOR_ ## name) \
197 : __HYPERCALL_CLOBBER3); \
198 (type)__res; \
199})
200
201#define _hypercall4(type, name, a1, a2, a3, a4) \
202({ \
203 __HYPERCALL_DECLS; \
204 __HYPERCALL_4ARG(a1, a2, a3, a4); \
205 asm volatile (__HYPERCALL \
206 : __HYPERCALL_4PARAM \
207 : __HYPERCALL_ENTRY(__HYPERVISOR_ ## name) \
208 : __HYPERCALL_CLOBBER4); \
209 (type)__res; \
210})
211
212static inline long
213xen_single_call(unsigned int call,
214 unsigned long a1, unsigned long a2,
215 unsigned long a3, unsigned long a4,
216 unsigned long a5)
217{
218 __HYPERCALL_DECLS;
219 __HYPERCALL_5ARG(a1, a2, a3, a4, a5);
220
221 asm volatile(__HYPERCALL
222 : __HYPERCALL_5PARAM
223 : __HYPERCALL_ENTRY(call)
224 : __HYPERCALL_CLOBBER5);
225
226 return (long)__res;
227}
228
229static __always_inline void __xen_stac(void)
230{
231 /*
232 * Suppress objtool seeing the STAC/CLAC and getting confused about it
233 * calling random code with AC=1.
234 */
235 asm volatile(ASM_STAC_UNSAFE ::: "memory", "flags");
236}
237
238static __always_inline void __xen_clac(void)
239{
240 asm volatile(ASM_CLAC_UNSAFE ::: "memory", "flags");
241}
242
243static inline long
244privcmd_call(unsigned int call,
245 unsigned long a1, unsigned long a2,
246 unsigned long a3, unsigned long a4,
247 unsigned long a5)
248{
249 long res;
250
251 __xen_stac();
252 res = xen_single_call(call, a1, a2, a3, a4, a5);
253 __xen_clac();
254
255 return res;
256}
257
258#ifdef CONFIG_XEN_PV
259static inline int
260HYPERVISOR_set_trap_table(struct trap_info *table)
261{
262 return _hypercall1(int, set_trap_table, table);
263}
264
265static inline int
266HYPERVISOR_mmu_update(struct mmu_update *req, int count,
267 int *success_count, domid_t domid)
268{
269 return _hypercall4(int, mmu_update, req, count, success_count, domid);
270}
271
272static inline int
273HYPERVISOR_mmuext_op(struct mmuext_op *op, int count,
274 int *success_count, domid_t domid)
275{
276 return _hypercall4(int, mmuext_op, op, count, success_count, domid);
277}
278
279static inline int
280HYPERVISOR_set_gdt(unsigned long *frame_list, int entries)
281{
282 return _hypercall2(int, set_gdt, frame_list, entries);
283}
284
285static inline int
286HYPERVISOR_callback_op(int cmd, void *arg)
287{
288 return _hypercall2(int, callback_op, cmd, arg);
289}
290
291static __always_inline int
292HYPERVISOR_set_debugreg(int reg, unsigned long value)
293{
294 return _hypercall2(int, set_debugreg, reg, value);
295}
296
297static __always_inline unsigned long
298HYPERVISOR_get_debugreg(int reg)
299{
300 return _hypercall1(unsigned long, get_debugreg, reg);
301}
302
303static inline int
304HYPERVISOR_update_descriptor(u64 ma, u64 desc)
305{
306 return _hypercall2(int, update_descriptor, ma, desc);
307}
308
309static inline int
310HYPERVISOR_update_va_mapping(unsigned long va, pte_t new_val,
311 unsigned long flags)
312{
313 return _hypercall3(int, update_va_mapping, va, new_val.pte, flags);
314}
315
316static inline int
317HYPERVISOR_set_segment_base(int reg, unsigned long value)
318{
319 return _hypercall2(int, set_segment_base, reg, value);
320}
321
322static inline void
323MULTI_fpu_taskswitch(struct multicall_entry *mcl, int set)
324{
325 mcl->op = __HYPERVISOR_fpu_taskswitch;
326 mcl->args[0] = set;
327
328 trace_xen_mc_entry(mcl, 1);
329}
330
331static inline void
332MULTI_update_va_mapping(struct multicall_entry *mcl, unsigned long va,
333 pte_t new_val, unsigned long flags)
334{
335 mcl->op = __HYPERVISOR_update_va_mapping;
336 mcl->args[0] = va;
337 mcl->args[1] = new_val.pte;
338 mcl->args[2] = flags;
339
340 trace_xen_mc_entry(mcl, 3);
341}
342
343static inline void
344MULTI_update_descriptor(struct multicall_entry *mcl, u64 maddr,
345 struct desc_struct desc)
346{
347 mcl->op = __HYPERVISOR_update_descriptor;
348 mcl->args[0] = maddr;
349 mcl->args[1] = *(unsigned long *)&desc;
350
351 trace_xen_mc_entry(mcl, 2);
352}
353
354static inline void
355MULTI_mmu_update(struct multicall_entry *mcl, struct mmu_update *req,
356 int count, int *success_count, domid_t domid)
357{
358 mcl->op = __HYPERVISOR_mmu_update;
359 mcl->args[0] = (unsigned long)req;
360 mcl->args[1] = count;
361 mcl->args[2] = (unsigned long)success_count;
362 mcl->args[3] = domid;
363
364 trace_xen_mc_entry(mcl, 4);
365}
366
367static inline void
368MULTI_mmuext_op(struct multicall_entry *mcl, struct mmuext_op *op, int count,
369 int *success_count, domid_t domid)
370{
371 mcl->op = __HYPERVISOR_mmuext_op;
372 mcl->args[0] = (unsigned long)op;
373 mcl->args[1] = count;
374 mcl->args[2] = (unsigned long)success_count;
375 mcl->args[3] = domid;
376
377 trace_xen_mc_entry(mcl, 4);
378}
379
380static inline void
381MULTI_stack_switch(struct multicall_entry *mcl,
382 unsigned long ss, unsigned long esp)
383{
384 mcl->op = __HYPERVISOR_stack_switch;
385 mcl->args[0] = ss;
386 mcl->args[1] = esp;
387
388 trace_xen_mc_entry(mcl, 2);
389}
390#endif
391
392static __always_inline int
393HYPERVISOR_sched_op(int cmd, void *arg)
394{
395 return _hypercall2(int, sched_op, cmd, arg);
396}
397
398static inline long
399HYPERVISOR_set_timer_op(u64 timeout)
400{
401 unsigned long timeout_hi = (unsigned long)(timeout>>32);
402 unsigned long timeout_lo = (unsigned long)timeout;
403 return _hypercall2(long, set_timer_op, timeout_lo, timeout_hi);
404}
405
406static inline int
407HYPERVISOR_mca(struct xen_mc *mc_op)
408{
409 mc_op->interface_version = XEN_MCA_INTERFACE_VERSION;
410 return _hypercall1(int, mca, mc_op);
411}
412
413static inline int
414HYPERVISOR_platform_op(struct xen_platform_op *op)
415{
416 op->interface_version = XENPF_INTERFACE_VERSION;
417 return _hypercall1(int, platform_op, op);
418}
419
420static inline long
421HYPERVISOR_memory_op(unsigned int cmd, void *arg)
422{
423 return _hypercall2(long, memory_op, cmd, arg);
424}
425
426static inline int
427HYPERVISOR_multicall(void *call_list, uint32_t nr_calls)
428{
429 return _hypercall2(int, multicall, call_list, nr_calls);
430}
431
432static inline int
433HYPERVISOR_event_channel_op(int cmd, void *arg)
434{
435 return _hypercall2(int, event_channel_op, cmd, arg);
436}
437
438static __always_inline int
439HYPERVISOR_xen_version(int cmd, void *arg)
440{
441 return _hypercall2(int, xen_version, cmd, arg);
442}
443
444static inline int
445HYPERVISOR_console_io(int cmd, int count, char *str)
446{
447 return _hypercall3(int, console_io, cmd, count, str);
448}
449
450static inline int
451HYPERVISOR_physdev_op(int cmd, void *arg)
452{
453 return _hypercall2(int, physdev_op, cmd, arg);
454}
455
456static inline int
457HYPERVISOR_grant_table_op(unsigned int cmd, void *uop, unsigned int count)
458{
459 return _hypercall3(int, grant_table_op, cmd, uop, count);
460}
461
462static inline int
463HYPERVISOR_vm_assist(unsigned int cmd, unsigned int type)
464{
465 return _hypercall2(int, vm_assist, cmd, type);
466}
467
468static inline int
469HYPERVISOR_vcpu_op(int cmd, int vcpuid, void *extra_args)
470{
471 return _hypercall3(int, vcpu_op, cmd, vcpuid, extra_args);
472}
473
474static inline int
475HYPERVISOR_suspend(unsigned long start_info_mfn)
476{
477 struct sched_shutdown r = { .reason = SHUTDOWN_suspend };
478
479 /*
480 * For a PV guest the tools require that the start_info mfn be
481 * present in rdx/edx when the hypercall is made. Per the
482 * hypercall calling convention this is the third hypercall
483 * argument, which is start_info_mfn here.
484 */
485 return _hypercall3(int, sched_op, SCHEDOP_shutdown, &r, start_info_mfn);
486}
487
488static inline unsigned long __must_check
489HYPERVISOR_hvm_op(int op, void *arg)
490{
491 return _hypercall2(unsigned long, hvm_op, op, arg);
492}
493
494static inline int
495HYPERVISOR_xenpmu_op(unsigned int op, void *arg)
496{
497 return _hypercall2(int, xenpmu_op, op, arg);
498}
499
500static inline int
501HYPERVISOR_dm_op(
502 domid_t dom, unsigned int nr_bufs, struct xen_dm_op_buf *bufs)
503{
504 int ret;
505 __xen_stac();
506 ret = _hypercall3(int, dm_op, dom, nr_bufs, bufs);
507 __xen_clac();
508 return ret;
509}
510
511#endif /* _ASM_X86_XEN_HYPERCALL_H */
512