summaryrefslogtreecommitdiff
path: root/tools/libs/call/core.c
blob: 02c4f8e1aefa9a87846daf0ae811cc91e12a0c00 (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
/*
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdlib.h>

#include "private.h"

static int all_restrict_cb(Xentoolcore__Active_Handle *ah, domid_t domid) {
    xencall_handle *xcall = CONTAINER_OF(ah, *xcall, tc_ah);
    int rc;

    rc = xentoolcore__restrict_by_dup2_null(xcall->buf_fd);
    if ( rc )
        goto out;

    rc = xentoolcore__restrict_by_dup2_null(xcall->fd);

out:
    return rc;
}

xencall_handle *xencall_open(xentoollog_logger *logger, unsigned open_flags)
{
    xencall_handle *xcall = malloc(sizeof(*xcall));
    int rc;

    if (!xcall) return NULL;

    xcall->fd = -1;
    xcall->buf_fd = -1;
    xcall->tc_ah.restrict_callback = all_restrict_cb;
    xentoolcore__register_active_handle(&xcall->tc_ah);

    xcall->flags = open_flags;
    xcall->buffer_cache_nr = 0;

    xcall->buffer_total_allocations = 0;
    xcall->buffer_total_releases = 0;
    xcall->buffer_current_allocations = 0;
    xcall->buffer_maximum_allocations = 0;
    xcall->buffer_cache_hits = 0;
    xcall->buffer_cache_misses = 0;
    xcall->buffer_cache_toobig = 0;
    xcall->logger = logger;
    xcall->logger_tofree = NULL;

    if (!xcall->logger) {
        xcall->logger = xcall->logger_tofree =
            (xentoollog_logger*)
            xtl_createlogger_stdiostream(stderr, XTL_PROGRESS, 0);
        if (!xcall->logger) goto err;
    }

    rc = osdep_xencall_open(xcall);
    if ( rc  < 0 ) goto err;

    return xcall;

err:
    xentoolcore__deregister_active_handle(&xcall->tc_ah);
    osdep_xencall_close(xcall);
    xtl_logger_destroy(xcall->logger_tofree);
    free(xcall);
    return NULL;
}

int xencall_close(xencall_handle *xcall)
{
    int rc;

    if ( !xcall )
        return 0;

    xentoolcore__deregister_active_handle(&xcall->tc_ah);
    rc = osdep_xencall_close(xcall);
    buffer_release_cache(xcall);
    xtl_logger_destroy(xcall->logger_tofree);
    free(xcall);
    return rc;
}

int xencall_fd(xencall_handle *xcall)
{
    return xcall->fd;
}

int xencall0(xencall_handle *xcall, unsigned int op)
{
    privcmd_hypercall_t call = {
        .op = op,
    };

    return osdep_hypercall(xcall, &call);
}

int xencall1(xencall_handle *xcall, unsigned int op,
             uint64_t arg1)
{
    privcmd_hypercall_t call = {
        .op = op,
        .arg = { arg1 },
    };

    return osdep_hypercall(xcall, &call);
}

int xencall2(xencall_handle *xcall, unsigned int op,
             uint64_t arg1, uint64_t arg2)
{
    privcmd_hypercall_t call = {
        .op = op,
        .arg = { arg1, arg2 },
    };

    return osdep_hypercall(xcall, &call);
}

long xencall2L(xencall_handle *xcall, unsigned int op,
               uint64_t arg1, uint64_t arg2)
{
    privcmd_hypercall_t call = {
        .op = op,
        .arg = { arg1, arg2 },
    };

    return osdep_hypercall(xcall, &call);
}

int xencall3(xencall_handle *xcall, unsigned int op,
             uint64_t arg1, uint64_t arg2, uint64_t arg3)
{
    privcmd_hypercall_t call = {
        .op = op,
        .arg = { arg1, arg2, arg3},
    };

    return osdep_hypercall(xcall, &call);
}

int xencall4(xencall_handle *xcall, unsigned int op,
             uint64_t arg1, uint64_t arg2, uint64_t arg3,
             uint64_t arg4)
{
    privcmd_hypercall_t call = {
        .op = op,
        .arg = { arg1, arg2, arg3, arg4 },
    };

    return osdep_hypercall(xcall, &call);
}

int xencall5(xencall_handle *xcall, unsigned int op,
             uint64_t arg1, uint64_t arg2, uint64_t arg3,
             uint64_t arg4, uint64_t arg5)
{
    privcmd_hypercall_t call = {
        .op = op,
        .arg = { arg1, arg2, arg3, arg4, arg5 },
    };

    return osdep_hypercall(xcall, &call);
}

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