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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
/* VBOX driver interface - synchronous version - by D.C. van Moolenbroek */
#include <minix/drivers.h>
#include <minix/vboxtype.h>
#include <minix/vboxif.h>
#include <minix/vbox.h>
#include <minix/ds.h>
#include <assert.h>
static endpoint_t vbox_endpt = NONE;
int vbox_init(void)
{
/* Initialize the library, by resolving the VBOX driver endpoint.
*/
int r;
if ((r = ds_retrieve_label_endpt("vbox", &vbox_endpt)) != OK) {
printf("libvbox: unable to obtain VBOX endpoint (%d)\n", r);
return EINVAL;
}
return OK;
}
vbox_conn_t vbox_open(const char *name)
{
/* Open a VirtualBox HGCM connection.
*/
message m;
cp_grant_id_t grant;
size_t len;
int r;
if (vbox_endpt == NONE)
return EDEADSRCDST;
len = strlen(name) + 1;
grant = cpf_grant_direct(vbox_endpt, (vir_bytes) name, len, CPF_READ);
if (!GRANT_VALID(grant))
return ENOMEM;
memset(&m, 0, sizeof(m));
m.m_type = VBOX_OPEN;
m.VBOX_GRANT = grant;
m.VBOX_COUNT = len;
m.VBOX_ID = 0;
r = ipc_sendrec(vbox_endpt, &m);
cpf_revoke(grant);
if (r != OK)
return r;
if (m.VBOX_ID != 0)
return EINVAL;
return m.VBOX_RESULT;
}
int vbox_close(vbox_conn_t conn)
{
/* Close a VirtualBox HGCM connection.
*/
message m;
int r;
if (vbox_endpt == NONE)
return EDEADSRCDST;
memset(&m, 0, sizeof(m));
m.m_type = VBOX_CLOSE;
m.VBOX_CONN = conn;
m.VBOX_ID = 0;
r = ipc_sendrec(vbox_endpt, &m);
if (r != OK)
return r;
if (m.VBOX_ID != 0)
return EINVAL;
return m.VBOX_RESULT;
}
int vbox_call(vbox_conn_t conn, u32_t function, vbox_param_t *param, int count,
int *code)
{
/* Call a VirtualBox HGCM function. The caller must set up all buffer grants.
*/
cp_grant_id_t grant = GRANT_INVALID;
message m;
int i, r;
if (vbox_endpt == NONE) {
vbox_put(param, count);
return EDEADSRCDST;
}
/* Check whether all parameters are initialized correctly. */
for (i = 0; i < count; i++) {
switch (param[i].type) {
case VBOX_TYPE_U32:
case VBOX_TYPE_U64:
case VBOX_TYPE_PTR:
break;
default:
vbox_put(param, count);
return ENOMEM;
}
}
if (count > 0) {
grant = cpf_grant_direct(vbox_endpt, (vir_bytes) param,
sizeof(param[0]) * count, CPF_READ | CPF_WRITE);
if (!GRANT_VALID(grant)) {
vbox_put(param, count);
return ENOMEM;
}
}
memset(&m, 0, sizeof(m));
m.m_type = VBOX_CALL;
m.VBOX_CONN = conn;
m.VBOX_GRANT = grant;
m.VBOX_COUNT = count;
m.VBOX_ID = 0;
m.VBOX_FUNCTION = function;
r = ipc_sendrec(vbox_endpt, &m);
if (GRANT_VALID(grant))
cpf_revoke(grant);
vbox_put(param, count);
if (r != OK)
return r;
if (m.VBOX_ID != 0)
return EINVAL;
if (code != NULL)
*code = m.VBOX_CODE;
return m.VBOX_RESULT;
}
void vbox_set_u32(vbox_param_t *param, u32_t value)
{
/* Set the given parameter to a 32-bit value.
*/
param->type = VBOX_TYPE_U32;
param->u32 = value;
}
void vbox_set_u64(vbox_param_t *param, u64_t value)
{
/* Set the given parameter to a 32-bit value.
*/
param->type = VBOX_TYPE_U64;
param->u64 = value;
}
void vbox_set_ptr(vbox_param_t *param, void *ptr, size_t size,
unsigned int dir)
{
/* Set the given parameter to a grant for the given local pointer.
*/
cp_grant_id_t grant = GRANT_INVALID;
int flags;
flags = 0;
if (dir & VBOX_DIR_IN) flags |= CPF_WRITE;
if (dir & VBOX_DIR_OUT) flags |= CPF_READ;
if (size > 0) {
grant = cpf_grant_direct(vbox_endpt, (vir_bytes) ptr, size, flags);
if (!GRANT_VALID(grant)) {
param->type = VBOX_TYPE_INVALID;
return;
}
}
param->type = VBOX_TYPE_PTR;
param->ptr.grant = grant;
param->ptr.off = 0;
param->ptr.size = size;
param->ptr.dir = dir;
}
void vbox_set_grant(vbox_param_t *param, endpoint_t endpt, cp_grant_id_t grant,
size_t off, size_t size, unsigned int dir)
{
/* Set the given parameter to an indirect grant for the given grant.
*/
cp_grant_id_t indir_grant;
/* Unfortunately, the current implementation of indirect grants does not
* support making smaller subgrants out of larger original grants. Therefore,
* we are forced to grant more access than we would like.
*/
indir_grant = cpf_grant_indirect(vbox_endpt, endpt, grant);
if (!GRANT_VALID(indir_grant)) {
param->type = VBOX_TYPE_INVALID;
return;
}
param->type = VBOX_TYPE_PTR;
param->ptr.grant = indir_grant;
param->ptr.off = off;
param->ptr.size = size;
param->ptr.dir = dir;
}
void vbox_put(vbox_param_t *param, int count)
{
/* Free all resources used for the given parameters.
*/
while (count--) {
if (param->type == VBOX_TYPE_PTR && GRANT_VALID(param->ptr.grant))
cpf_revoke(param->ptr.grant);
param++;
}
}
u32_t vbox_get_u32(vbox_param_t *param)
{
/* Retrieve the 32-bit value from the given parameter.
*/
assert(param->type == VBOX_TYPE_U32);
return param->u32;
}
u64_t vbox_get_u64(vbox_param_t *param)
{
/* Retrieve the 64-bit value from the given parameter.
*/
assert(param->type == VBOX_TYPE_U64);
return param->u64;
}
|