aboutsummaryrefslogtreecommitdiffstats
path: root/tools/libxc/xc_minios.c
blob: 53f7a148daece2f98a64780a841fc26f970f07a0 (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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/******************************************************************************
 *
 * Copyright 2007-2008 Samuel Thibault <samuel.thibault@eu.citrix.com>.
 * All rights reserved.
 * Use is subject to license terms.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, version 2 of the
 * License.
 */

#undef NDEBUG
#include <types.h>
#include <os.h>
#include <mm.h>
#include <lib.h>
#include <events.h>
#include <wait.h>
#include <sys/mman.h>
#include <errno.h>

#include <xen/memory.h>
#include <xen/sys/evtchn.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <assert.h>
#include <stdint.h>
#include <inttypes.h>

#include "xc_private.h"

extern struct wait_queue_head event_queue;

int xc_interface_open(void)
{
    return 0;
}

int xc_interface_close(int xc_handle)
{
    return 0;
}

void *xc_map_foreign_batch(int xc_handle, uint32_t dom, int prot,
                           xen_pfn_t *arr, int num)
{
    unsigned long pt_prot = 0;
#ifdef __ia64__
    /* TODO */
#else
    if (prot & PROT_READ)
	pt_prot = L1_PROT_RO;
    if (prot & PROT_WRITE)
	pt_prot = L1_PROT;
#endif
    return map_frames_ex(arr, num, 1, 0, 1, dom, 1, pt_prot);
}

void *xc_map_foreign_range(int xc_handle, uint32_t dom,
                           int size, int prot,
                           unsigned long mfn)
{
    unsigned long pt_prot = 0;
    printf("xc_map_foreign_range(%lx, %d)\n", mfn, size);
#ifdef __ia64__
    /* TODO */
#else
    if (prot & PROT_READ)
	pt_prot = L1_PROT_RO;
    if (prot & PROT_WRITE)
	pt_prot = L1_PROT;
#endif
    assert(!(size % getpagesize()));
    return map_frames_ex(&mfn, size / getpagesize(), 0, 1, 1, dom, 0, pt_prot);
}

int xc_map_foreign_ranges(int xc_handle, uint32_t dom,
                          privcmd_mmap_entry_t *entries, int nr)
{
    printf("xc_map_foreign_ranges, TODO\n");
    do_exit();
}

int do_xen_hypercall(int xc_handle, privcmd_hypercall_t *hypercall)
{
    multicall_entry_t call;
    int i, ret;

    call.op = hypercall->op;
    for (i = 0; i < sizeof(hypercall->arg) / sizeof(*hypercall->arg); i++)
	call.args[i] = hypercall->arg[i];

    ret = HYPERVISOR_multicall(&call, 1);

    if (ret < 0) {
	errno = -ret;
	return -1;
    }
    if (call.result < 0) {
        errno = -call.result;
        return -1;
    }
    return call.result;
}

int xc_find_device_number(const char *name)
{
    printf("xc_find_device_number(%s)\n", name);
    do_exit();
}

int xc_evtchn_open(void)
{
    int fd = alloc_fd(FTYPE_EVTCHN), i;
    for (i = 0; i < MAX_EVTCHN_PORTS; i++) {
	files[fd].evtchn.ports[i].port = -1;
        files[fd].evtchn.ports[i].bound = 0;
    }
    printf("evtchn_open() -> %d\n", fd);
    return fd;
}

int xc_evtchn_close(int xce_handle)
{
    int i;
    for (i = 0; i < MAX_EVTCHN_PORTS; i++)
        if (files[xce_handle].evtchn.ports[i].bound)
            unbind_evtchn(files[xce_handle].evtchn.ports[i].port);
    files[xce_handle].type = FTYPE_NONE;
    return 0;
}

int xc_evtchn_fd(int xce_handle)
{
    return xce_handle;
}

int xc_evtchn_notify(int xce_handle, evtchn_port_t port)
{
    int ret;

    ret = notify_remote_via_evtchn(port);

    if (ret < 0) {
	errno = -ret;
	ret = -1;
    }
    return ret;
}

/* XXX Note: This is not threadsafe */
static int port_alloc(int xce_handle) {
    int i;
    for (i= 0; i < MAX_EVTCHN_PORTS; i++)
	if (files[xce_handle].evtchn.ports[i].port == -1)
	    break;
    if (i == MAX_EVTCHN_PORTS) {
	printf("Too many ports in xc handle\n");
	errno = EMFILE;
	return -1;
    }
    files[xce_handle].evtchn.ports[i].pending = 0;
    return i;
}

static void poke_port(int xce_handle, evtchn_port_t port)
{
    shared_info_t *s = HYPERVISOR_shared_info;
    printk("poking port %d\n", port);
    synch_set_bit(port, &s->evtchn_pending[0]);
    xc_evtchn_unmask(xce_handle, port);
}

static void evtchn_handler(evtchn_port_t port, struct pt_regs *regs, void *data)
{
    int xce_handle = (intptr_t) data;
    int i;
    assert(files[xce_handle].type == FTYPE_EVTCHN);
    mask_evtchn(port);
    for (i= 0; i < MAX_EVTCHN_PORTS; i++)
	if (files[xce_handle].evtchn.ports[i].port == port)
	    break;
    if (i == MAX_EVTCHN_PORTS) {
	printk("Unknown port for handle %d\n", xce_handle);
	return;
    }
    files[xce_handle].evtchn.ports[i].pending++;
    files[xce_handle].read = 1;
    wake_up(&event_queue);
}

evtchn_port_or_error_t xc_evtchn_bind_unbound_port(int xce_handle, int domid)
{
    int ret, i;
    evtchn_port_t port;

    assert(get_current() == main_thread);
    i = port_alloc(xce_handle);
    if (i == -1)
	return -1;

    printf("xc_evtchn_bind_unbound_port(%d)", domid);
    ret = evtchn_alloc_unbound(domid, evtchn_handler, (void*)(intptr_t)xce_handle, &port);
    printf(" = %d\n", ret);

    if (ret < 0) {
	errno = -ret;
	return -1;
    }
    files[xce_handle].evtchn.ports[i].bound = 1;
    files[xce_handle].evtchn.ports[i].port = port;
    return port;
}

evtchn_port_or_error_t xc_evtchn_bind_interdomain(int xce_handle, int domid,
    evtchn_port_t remote_port)
{
    evtchn_port_t local_port;
    int ret, i;

    assert(get_current() == main_thread);
    i = port_alloc(xce_handle);
    if (i == -1)
	return -1;

    printf("xc_evtchn_bind_interdomain(%d, %"PRId32")", domid, remote_port);
    ret = evtchn_bind_interdomain(domid, remote_port, evtchn_handler, (void*)(intptr_t)xce_handle, &local_port);
    printf(" = %d\n", ret);

    if (ret < 0) {
	errno = -ret;
	return -1;
    }
    files[xce_handle].evtchn.ports[i].bound = 1;
    files[xce_handle].evtchn.ports[i].port = local_port;
/* Poke port on start: HVM won't send an event for the very first request since
 * we were not ready yet */
    poke_port(xce_handle, local_port);
    return local_port;
}

int xc_evtchn_unbind(int xce_handle, evtchn_port_t port)
{
    int i;
    for (i = 0; i < MAX_EVTCHN_PORTS; i++)
	if (files[xce_handle].evtchn.ports[i].port == port) {
	    files[xce_handle].evtchn.ports[i].port = -1;
	    break;
	}
    if (i == MAX_EVTCHN_PORTS)
	printf("Warning: couldn't find port %"PRId32" for xc handle %x\n", port, xce_handle);
    files[xce_handle].evtchn.ports[i].bound = 0;
    unbind_evtchn(port);
    return 0;
}

evtchn_port_or_error_t xc_evtchn_bind_virq(int xce_handle, unsigned int virq)
{
    evtchn_port_t port;
    int i;

    assert(get_current() == main_thread);
    i = port_alloc(xce_handle);
    if (i == -1)
	return -1;

    printf("xc_evtchn_bind_virq(%d)", virq);
    port = bind_virq(virq, evtchn_handler, (void*)(intptr_t)xce_handle);

    if (port < 0) {
	errno = -port;
	return -1;
    }
    files[xce_handle].evtchn.ports[i].bound = 1;
    files[xce_handle].evtchn.ports[i].port = port;
    return port;
}

evtchn_port_or_error_t xc_evtchn_pending(int xce_handle)
{
    int i;
    unsigned long flags;
    local_irq_save(flags);
    for (i = 0; i < MAX_EVTCHN_PORTS; i++) {
	evtchn_port_t port = files[xce_handle].evtchn.ports[i].port;
	if (port != -1 && files[xce_handle].evtchn.ports[i].pending) {
	    files[xce_handle].evtchn.ports[i].pending--;
	    local_irq_restore(flags);
	    return port;
	}
    }
    files[xce_handle].read = 0;
    local_irq_restore(flags);
    return -1;
}

int xc_evtchn_unmask(int xce_handle, evtchn_port_t port)
{
    unmask_evtchn(port);
    return 0;
}

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