summaryrefslogtreecommitdiff
path: root/xen/arch/x86/include/asm/guest/xen-hcall.h
blob: 665b472d05ac38fcc0fd3c6f6630491f6be3f510 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* SPDX-License-Identifier: GPL-2.0-only */
/******************************************************************************
 * asm-x86/guest/xen-hcall.h
 *
 * Copyright (c) 2017 Citrix Systems Ltd.
 */

#ifndef __X86_XEN_HYPERCALL_H__
#define __X86_XEN_HYPERCALL_H__

#ifdef CONFIG_XEN_GUEST

#include <xen/types.h>

#include <asm/asm_defns.h>

#include <public/xen.h>
#include <public/sched.h>
#include <public/hvm/hvm_op.h>

#include <public/vcpu.h>

/*
 * Hypercall primatives for 64bit
 *
 * Inputs: %rdi, %rsi, %rdx, %r10, %r8, %r9 (arguments 1-6)
 */

#define _hypercall64_1(type, hcall, a1)                                 \
    ({                                                                  \
        long res, tmp__;                                                \
        asm volatile (                                                  \
            "call hypercall_page + %c[offset]"                          \
            : "=a" (res), "=D" (tmp__) ASM_CALL_CONSTRAINT              \
            : [offset] "i" (hcall * 32),                                \
              "1" ((long)(a1))                                          \
            : "memory" );                                               \
        (type)res;                                                      \
    })

#define _hypercall64_2(type, hcall, a1, a2)                             \
    ({                                                                  \
        long res, tmp__;                                                \
        asm volatile (                                                  \
            "call hypercall_page + %c[offset]"                          \
            : "=a" (res), "=D" (tmp__), "=S" (tmp__)                    \
              ASM_CALL_CONSTRAINT                                       \
            : [offset] "i" (hcall * 32),                                \
              "1" ((long)(a1)), "2" ((long)(a2))                        \
            : "memory" );                                               \
        (type)res;                                                      \
    })

#define _hypercall64_3(type, hcall, a1, a2, a3)                         \
    ({                                                                  \
        long res, tmp__;                                                \
        asm volatile (                                                  \
            "call hypercall_page + %c[offset]"                          \
            : "=a" (res), "=D" (tmp__), "=S" (tmp__), "=d" (tmp__)      \
              ASM_CALL_CONSTRAINT                                       \
            : [offset] "i" (hcall * 32),                                \
              "1" ((long)(a1)), "2" ((long)(a2)), "3" ((long)(a3))      \
            : "memory" );                                               \
        (type)res;                                                      \
    })

#define _hypercall64_4(type, hcall, a1, a2, a3, a4)                     \
    ({                                                                  \
        long res, tmp__;                                                \
        register long _a4 asm ("r10") = ((long)(a4));                   \
        asm volatile (                                                  \
            "call hypercall_page + %c[offset]"                          \
            : "=a" (res), "=D" (tmp__), "=S" (tmp__), "=d" (tmp__),     \
              "=&r" (tmp__) ASM_CALL_CONSTRAINT                         \
            : [offset] "i" (hcall * 32),                                \
              "1" ((long)(a1)), "2" ((long)(a2)), "3" ((long)(a3)),     \
              "4" (_a4)                                                 \
            : "memory" );                                               \
        (type)res;                                                      \
    })

/*
 * Primitive Hypercall wrappers
 */
static inline long xen_hypercall_sched_op(unsigned int cmd, void *arg)
{
    return _hypercall64_2(long, __HYPERVISOR_sched_op, cmd, arg);
}

static inline long xen_hypercall_memory_op(unsigned int cmd, void *arg)
{
    return _hypercall64_2(long, __HYPERVISOR_memory_op, cmd, arg);
}

static inline int xen_hypercall_vcpu_op(unsigned int cmd, unsigned int vcpu,
                                        void *arg)
{
    return _hypercall64_3(long, __HYPERVISOR_vcpu_op, cmd, vcpu, arg);
}

static inline long xen_hypercall_event_channel_op(unsigned int cmd, void *arg)
{
    return _hypercall64_2(long, __HYPERVISOR_event_channel_op, cmd, arg);
}

static inline long xen_hypercall_grant_table_op(unsigned int cmd, void *arg,
                                                unsigned int count)
{
    return _hypercall64_3(long, __HYPERVISOR_grant_table_op, cmd, arg, count);
}

static inline long xen_hypercall_hvm_op(unsigned int op, void *arg)
{
    return _hypercall64_2(long, __HYPERVISOR_hvm_op, op, arg);
}

/*
 * Higher level hypercall helpers
 */
static inline void xen_hypercall_console_write(
    const char *buf, unsigned int count)
{
    (void)_hypercall64_3(long, __HYPERVISOR_console_io,
                         CONSOLEIO_write, count, buf);
}

static inline long xen_hypercall_shutdown(unsigned int reason)
{
    struct sched_shutdown s = { .reason = reason };
    return xen_hypercall_sched_op(SCHEDOP_shutdown, &s);
}

static inline long xen_hypercall_evtchn_send(evtchn_port_t port)
{
    struct evtchn_send send = { .port = port };

    return xen_hypercall_event_channel_op(EVTCHNOP_send, &send);
}

static inline long xen_hypercall_evtchn_unmask(evtchn_port_t port)
{
    struct evtchn_unmask unmask = { .port = port };

    return xen_hypercall_event_channel_op(EVTCHNOP_unmask, &unmask);
}

static inline long xen_hypercall_hvm_get_param(uint32_t index, uint64_t *value)
{
    struct xen_hvm_param xhv = {
        .domid = DOMID_SELF,
        .index = index,
    };
    long ret = xen_hypercall_hvm_op(HVMOP_get_param, &xhv);

    if ( ret == 0 )
        *value = xhv.value;

    return ret;
}

static inline long xen_hypercall_set_evtchn_upcall_vector(
    unsigned int cpu, unsigned int vector)
{
    struct xen_hvm_evtchn_upcall_vector a = {
        .vcpu = cpu,
        .vector = vector,
    };

    return xen_hypercall_hvm_op(HVMOP_set_evtchn_upcall_vector, &a);
}

#else /* CONFIG_XEN_GUEST */

#include <xen/lib.h>

#include <public/sched.h>

static inline void xen_hypercall_console_write(
    const char *buf, unsigned int count)
{
    ASSERT_UNREACHABLE();
}

static inline long xen_hypercall_shutdown(unsigned int reason)
{
    ASSERT_UNREACHABLE();
    return 0;
}

#endif /* CONFIG_XEN_GUEST */
#endif /* __X86_XEN_HYPERCALL_H__ */

/*
 * Local variables:
 * mode: C
 * c-file-style: "BSD"
 * c-basic-offset: 4
 * tab-width: 4
 * indent-tabs-mode: nil
 * End:
 */