1/******************************************************************************
2 * hypercall.h
3 *
4 * Linux-specific hypervisor handling.
5 *
6 * Copyright (c) 2002-2004, K A Fraser
7 *
8 * This file may be distributed separately from the Linux kernel, or
9 * incorporated into other software packages, subject to the following license:
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this source file (the "Software"), to deal in the Software without
13 * restriction, including without limitation the rights to use, copy, modify,
14 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
15 * and to permit persons to whom the Software is furnished to do so, subject to
16 * the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
27 * IN THE SOFTWARE.
28 */
29
30#ifndef __HYPERCALL_H__
31#define __HYPERCALL_H__
32
33#include <sys/systm.h>
34#include <xen/interface/xen.h>
35#include <xen/interface/sched.h>
36
37extern char *hypercall_page;
38
39#define __STR(x) #x
40#define STR(x) __STR(x)
41#define	ENOXENSYS	38
42#define CONFIG_XEN_COMPAT	0x030002
43
44#define HYPERCALL_STR(name)                                     \
45        "call hypercall_page + ("STR(__HYPERVISOR_##name)" * 32)"
46
47#define _hypercall0(type, name)                 \
48({                                              \
49        long __res;                             \
50        __asm__ volatile (                          \
51                HYPERCALL_STR(name)             \
52                : "=a" (__res)                  \
53                :                               \
54                : "memory" );                   \
55        (type)__res;                            \
56})
57
58#define _hypercall1(type, name, a1)                             \
59({                                                              \
60        long __res, __ign1;                                     \
61        __asm__ volatile (                                          \
62                HYPERCALL_STR(name)                             \
63                : "=a" (__res), "=b" (__ign1)                   \
64                : "1" ((long)(a1))                              \
65                : "memory" );                                   \
66        (type)__res;                                            \
67})
68
69#define _hypercall2(type, name, a1, a2)                         \
70({                                                              \
71        long __res, __ign1, __ign2;                             \
72        __asm__ volatile (                                          \
73                HYPERCALL_STR(name)                             \
74                : "=a" (__res), "=b" (__ign1), "=c" (__ign2)    \
75                : "1" ((long)(a1)), "2" ((long)(a2))            \
76                : "memory" );                                   \
77        (type)__res;                                            \
78})
79
80#define _hypercall3(type, name, a1, a2, a3)                     \
81({                                                              \
82        long __res, __ign1, __ign2, __ign3;                     \
83        __asm__ volatile (                                          \
84                HYPERCALL_STR(name)                             \
85                : "=a" (__res), "=b" (__ign1), "=c" (__ign2),   \
86                "=d" (__ign3)                                   \
87                : "1" ((long)(a1)), "2" ((long)(a2)),           \
88                "3" ((long)(a3))                                \
89                : "memory" );                                   \
90        (type)__res;                                            \
91})
92
93#define _hypercall4(type, name, a1, a2, a3, a4)                 \
94({                                                              \
95        long __res, __ign1, __ign2, __ign3, __ign4;             \
96        __asm__ volatile (                                          \
97                HYPERCALL_STR(name)                             \
98                : "=a" (__res), "=b" (__ign1), "=c" (__ign2),   \
99                "=d" (__ign3), "=S" (__ign4)                    \
100                : "1" ((long)(a1)), "2" ((long)(a2)),           \
101                "3" ((long)(a3)), "4" ((long)(a4))              \
102                : "memory" );                                   \
103        (type)__res;                                            \
104})
105
106#define _hypercall5(type, name, a1, a2, a3, a4, a5)             \
107({                                                              \
108        long __res, __ign1, __ign2, __ign3, __ign4, __ign5;     \
109        __asm__ volatile (                                          \
110                HYPERCALL_STR(name)                             \
111                : "=a" (__res), "=b" (__ign1), "=c" (__ign2),   \
112                "=d" (__ign3), "=S" (__ign4), "=D" (__ign5)     \
113                : "1" ((long)(a1)), "2" ((long)(a2)),           \
114                "3" ((long)(a3)), "4" ((long)(a4)),             \
115                "5" ((long)(a5))                                \
116                : "memory" );                                   \
117        (type)__res;                                            \
118})
119
120static inline long
121privcmd_hypercall(long op, long a1, long a2, long a3, long a4, long a5)
122{
123	long __res, __ign1, __ign2, __ign3, __ign4, __ign5, __call;
124
125	__call = (long)&hypercall_page + (op * 32);
126	__asm__ volatile (
127		"call *%[call]"
128		: "=a" (__res), "=b" (__ign1), "=c" (__ign2),
129                "=d" (__ign3), "=S" (__ign4), "=D" (__ign5)
130                : "1" ((long)(a1)), "2" ((long)(a2)),
131                "3" ((long)(a3)), "4" ((long)(a4)),
132                "5" ((long)(a5)), [call] "a" (__call)
133		: "memory" );
134
135	return __res;
136}
137
138static inline int
139HYPERVISOR_set_trap_table(
140	trap_info_t *table)
141{
142	return _hypercall1(int, set_trap_table, table);
143}
144
145static inline int
146HYPERVISOR_mmu_update(
147	mmu_update_t *req, int count, int *success_count, domid_t domid)
148{
149	return _hypercall4(int, mmu_update, req, count, success_count, domid);
150}
151
152static inline int
153HYPERVISOR_mmuext_op(
154	mmuext_op_t *op, int count, int *success_count, domid_t domid)
155{
156	return _hypercall4(int, mmuext_op, op, count, success_count, domid);
157}
158
159static inline int
160HYPERVISOR_set_gdt(
161	unsigned long *frame_list, int entries)
162{
163	return _hypercall2(int, set_gdt, frame_list, entries);
164}
165
166static inline int
167HYPERVISOR_stack_switch(
168	unsigned long ss, unsigned long esp)
169{
170	return _hypercall2(int, stack_switch, ss, esp);
171}
172
173static inline int
174HYPERVISOR_set_callbacks(
175	unsigned long event_selector, unsigned long event_address,
176	unsigned long failsafe_selector, unsigned long failsafe_address)
177{
178	return _hypercall4(int, set_callbacks,
179			   event_selector, event_address,
180			   failsafe_selector, failsafe_address);
181}
182
183static inline int
184HYPERVISOR_fpu_taskswitch(
185	int set)
186{
187	return _hypercall1(int, fpu_taskswitch, set);
188}
189
190static inline int
191HYPERVISOR_sched_op_compat(
192	int cmd, unsigned long arg)
193{
194	return _hypercall2(int, sched_op_compat, cmd, arg);
195}
196
197static inline int
198HYPERVISOR_sched_op(
199	int cmd, void *arg)
200{
201	return _hypercall2(int, sched_op, cmd, arg);
202}
203
204static inline long
205HYPERVISOR_set_timer_op(
206	uint64_t timeout)
207{
208	unsigned long timeout_hi = (unsigned long)(timeout>>32);
209	unsigned long timeout_lo = (unsigned long)timeout;
210	return _hypercall2(long, set_timer_op, timeout_lo, timeout_hi);
211}
212
213static inline int
214HYPERVISOR_platform_op(
215        struct xen_platform_op *platform_op)
216{
217        platform_op->interface_version = XENPF_INTERFACE_VERSION;
218        return _hypercall1(int, platform_op, platform_op);
219}
220
221static inline int
222HYPERVISOR_set_debugreg(
223	int reg, unsigned long value)
224{
225	return _hypercall2(int, set_debugreg, reg, value);
226}
227
228static inline unsigned long
229HYPERVISOR_get_debugreg(
230	int reg)
231{
232	return _hypercall1(unsigned long, get_debugreg, reg);
233}
234
235static inline int
236HYPERVISOR_update_descriptor(
237	uint64_t ma, uint64_t desc)
238{
239	return _hypercall4(int, update_descriptor, ma, ma>>32, desc, desc>>32);
240}
241
242static inline int
243HYPERVISOR_memory_op(
244	unsigned int cmd, void *arg)
245{
246	return _hypercall2(int, memory_op, cmd, arg);
247}
248
249static inline int
250HYPERVISOR_multicall(
251	void *call_list, int nr_calls)
252{
253	return _hypercall2(int, multicall, call_list, nr_calls);
254}
255
256static inline int
257HYPERVISOR_update_va_mapping(
258	unsigned long va, uint64_t new_val, unsigned long flags)
259{
260	uint32_t hi, lo;
261
262	lo = (uint32_t)(new_val & 0xffffffff);
263	hi = (uint32_t)(new_val >> 32);
264
265	return _hypercall4(int, update_va_mapping, va,
266			   lo, hi, flags);
267}
268
269static inline int
270HYPERVISOR_event_channel_op(
271	int cmd, void *arg)
272{
273	int rc = _hypercall2(int, event_channel_op, cmd, arg);
274
275#if CONFIG_XEN_COMPAT <= 0x030002
276	if (__predict_false(rc == -ENOXENSYS)) {
277		struct evtchn_op op;
278		op.cmd = cmd;
279		memcpy(&op.u, arg, sizeof(op.u));
280		rc = _hypercall1(int, event_channel_op_compat, &op);
281		memcpy(arg, &op.u, sizeof(op.u));
282	}
283#endif
284	return (rc);
285}
286
287static inline int
288HYPERVISOR_xen_version(
289	int cmd, void *arg)
290{
291	return _hypercall2(int, xen_version, cmd, arg);
292}
293
294static inline int
295HYPERVISOR_console_io(
296	int cmd, int count, const char *str)
297{
298	return _hypercall3(int, console_io, cmd, count, str);
299}
300
301static inline int
302HYPERVISOR_physdev_op(
303	int cmd, void *arg)
304{
305	int rc = _hypercall2(int, physdev_op, cmd, arg);
306#if CONFIG_XEN_COMPAT <= 0x030002
307	if (__predict_false(rc == -ENOXENSYS)) {
308		struct physdev_op op;
309		op.cmd = cmd;
310		memcpy(&op.u, arg, sizeof(op.u));
311		rc = _hypercall1(int, physdev_op_compat, &op);
312		memcpy(arg, &op.u, sizeof(op.u));
313	}
314#endif
315	return (rc);
316}
317
318static inline int
319HYPERVISOR_grant_table_op(
320	unsigned int cmd, void *uop, unsigned int count)
321{
322	return _hypercall3(int, grant_table_op, cmd, uop, count);
323}
324
325static inline int
326HYPERVISOR_update_va_mapping_otherdomain(
327	unsigned long va, uint64_t new_val, unsigned long flags, domid_t domid)
328{
329	uint32_t hi, lo;
330
331	lo = (uint32_t)(new_val & 0xffffffff);
332	hi = (uint32_t)(new_val >> 32);
333
334	return _hypercall5(int, update_va_mapping_otherdomain, va,
335			   lo, hi, flags, domid);
336}
337
338static inline int
339HYPERVISOR_vm_assist(
340	unsigned int cmd, unsigned int type)
341{
342	return _hypercall2(int, vm_assist, cmd, type);
343}
344
345static inline int
346HYPERVISOR_vcpu_op(
347	int cmd, int vcpuid, void *extra_args)
348{
349	return _hypercall3(int, vcpu_op, cmd, vcpuid, extra_args);
350}
351
352static inline int
353HYPERVISOR_suspend(
354	unsigned long srec)
355{
356	struct sched_shutdown sched_shutdown = {
357		.reason = SHUTDOWN_suspend
358	};
359	int rc = _hypercall3(int, sched_op, SCHEDOP_shutdown,
360			   &sched_shutdown, srec);
361#if CONFIG_XEN_COMPAT <= 0x030002
362	if (rc == -ENOXENSYS)
363		rc = _hypercall3(int, sched_op_compat, SCHEDOP_shutdown,
364				 SHUTDOWN_suspend, srec);
365#endif
366	return (rc);
367}
368
369#if CONFIG_XEN_COMPAT <= 0x030002
370static inline int
371HYPERVISOR_nmi_op(
372        unsigned long op, void *arg)
373{
374        return _hypercall2(int, nmi_op, op, arg);
375}
376#endif
377
378static inline int
379HYPERVISOR_callback_op(
380        int cmd, void *arg)
381{
382        return _hypercall2(int, callback_op, cmd, arg);
383}
384
385#ifndef CONFIG_XEN
386static inline unsigned long
387HYPERVISOR_hvm_op(
388    int op, void *arg)
389{
390    return _hypercall2(unsigned long, hvm_op, op, arg);
391}
392#endif
393
394static inline int
395HYPERVISOR_xenoprof_op(
396        int op, void *arg)
397{
398        return _hypercall2(int, xenoprof_op, op, arg);
399}
400
401static inline int
402HYPERVISOR_kexec_op(
403        unsigned long op, void *args)
404{
405        return _hypercall2(int, kexec_op, op, args);
406}
407#endif /* __HYPERCALL_H__ */
408
409/*
410 * Local variables:
411 *  c-file-style: "linux"
412 *  indent-tabs-mode: t
413 *  c-indent-level: 8
414 *  c-basic-offset: 8
415 *  tab-width: 8
416 * End:
417 */
418