core-admin/vchan/init.c

414 lines
9.1 KiB
C
Raw Normal View History

2011-03-08 12:24:47 +01:00
/*
* The Qubes OS Project, http://www.qubes-os.org
*
* Copyright (C) 2010 Rafal Wojtczuk <rafal@invisiblethingslab.com>
*
* 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; either version 2
* of the License, or (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#ifndef WINNT
2012-02-22 17:54:58 +01:00
#include <sys/types.h>
#include <sys/unistd.h>
#include <sys/stat.h>
2011-03-08 12:24:47 +01:00
#include <sys/mman.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <malloc.h>
#include <string.h>
#include <xenctrl.h>
#include <unistd.h>
2012-02-22 17:54:58 +01:00
#ifndef CONFIG_STUBDOM
2011-03-08 12:24:47 +01:00
#include "../u2mfn/u2mfnlib.h"
2012-02-27 14:58:46 +01:00
#else
#include <mm.h>
2012-02-22 17:54:58 +01:00
#endif
2011-03-08 12:24:47 +01:00
#endif
#include <xs.h>
#include <stdio.h>
#include <stdlib.h>
#include "libvchan.h"
static int fill_ctrl(struct libvchan *ctrl, struct vchan_interface *ring, int ring_ref)
{
if (!ctrl || !ring)
return -1;
ctrl->ring = ring;
ctrl->ring_ref = ring_ref;
ring->cons_in = ring->prod_in = ring->cons_out = ring->prod_out =
0;
ring->server_closed = ring->client_closed = 0;
ring->debug = 0xaabbccdd;
return 0;
}
#ifdef QREXEC_RING_V2
static int ring_init(struct libvchan *ctrl)
{
struct gntmem_handle* h;
grant_ref_t grants[1];
int result;
struct vchan_interface *ring;
h = gntmem_open();
if (h == INVALID_HANDLE_VALUE)
return -1;
gntmem_set_local_quota(h, 1);
gntmem_set_global_quota(h, 1);
memset(grants, 0, sizeof(grants));
ring = gntmem_grant_pages_to_domain(h, 0, 1, grants);
if (!ring) {
gntmem_close(h);
return -1;
}
return fill_ctrl(ctrl, ring, grants[0]);
}
#else
2011-03-08 12:24:47 +01:00
static int ring_init(struct libvchan *ctrl)
{
int mfn;
struct vchan_interface *ring;
2012-02-22 17:54:58 +01:00
#ifdef CONFIG_STUBDOM
ring = (struct vchan_interface *) memalign(XC_PAGE_SIZE, sizeof(*ring));
if (!ring)
return -1;
mfn = virtual_to_mfn(ring);
#else
2011-03-08 12:24:47 +01:00
ring = (struct vchan_interface *) u2mfn_alloc_kpage ();
2012-02-22 17:54:58 +01:00
2011-03-08 12:24:47 +01:00
if (ring == MAP_FAILED)
return -1;
if (u2mfn_get_last_mfn (&mfn) < 0)
return -1;
2012-02-22 17:54:58 +01:00
#endif
2011-03-08 12:24:47 +01:00
return fill_ctrl(ctrl, ring, mfn);
2011-03-08 12:24:47 +01:00
}
#endif
2011-03-08 12:24:47 +01:00
/**
creates event channel;
creates "ring-ref" and "event-channel" xenstore entries;
waits for connection to event channel from the peer
*/
static int server_interface_init(struct libvchan *ctrl, int devno)
{
int ret = -1;
struct xs_handle *xs;
char buf[64];
char ref[16];
#ifdef XENCTRL_HAS_XC_INTERFACE
xc_evtchn *evfd;
#else
EVTCHN evfd;
#endif
2011-03-08 12:24:47 +01:00
evtchn_port_or_error_t port;
#ifdef WINNT
xs = xs_domain_open();
#else
2012-02-22 17:54:58 +01:00
xs = xs_daemon_open();
#endif
2011-03-08 12:24:47 +01:00
if (!xs) {
return ret;
}
#ifdef XENCTRL_HAS_XC_INTERFACE
evfd = xc_evtchn_open(NULL, 0);
if (!evfd)
goto fail;
#else
2011-03-08 12:24:47 +01:00
evfd = xc_evtchn_open();
if (evfd < 0)
goto fail;
#endif
2011-03-08 12:24:47 +01:00
ctrl->evfd = evfd;
// the following hardcoded 0 is the peer domain id
port = xc_evtchn_bind_unbound_port(evfd, 0);
if (port < 0)
goto fail2;
ctrl->evport = port;
2012-02-22 17:54:58 +01:00
ctrl->devno = devno;
#ifdef QREXEC_RING_V2
snprintf(buf, sizeof buf, "device/vchan/%d/version", devno);
if (!xs_write(xs, 0, buf, "2", strlen("2")))
goto fail2;
#endif
2011-03-08 12:24:47 +01:00
snprintf(ref, sizeof ref, "%d", ctrl->ring_ref);
snprintf(buf, sizeof buf, "device/vchan/%d/ring-ref", devno);
if (!xs_write(xs, 0, buf, ref, strlen(ref)))
goto fail2;
snprintf(ref, sizeof ref, "%d", ctrl->evport);
snprintf(buf, sizeof buf, "device/vchan/%d/event-channel", devno);
if (!xs_write(xs, 0, buf, ref, strlen(ref)))
goto fail2;
// do not block in stubdom and windows - libvchan_server_handle_connected will be
// called on first input
#ifdef ASYNC_INIT
// wait for the peer to arrive
2011-03-08 12:24:47 +01:00
if (xc_evtchn_pending(evfd) == -1)
goto fail2;
xc_evtchn_unmask(ctrl->evfd, ctrl->evport);
snprintf(buf, sizeof buf, "device/vchan/%d", devno);
xs_rm(xs, 0, buf);
2012-02-22 17:54:58 +01:00
#endif
2011-03-08 12:24:47 +01:00
ret = 0;
fail2:
if (ret)
xc_evtchn_close(evfd);
2011-03-08 12:24:47 +01:00
fail:
xs_daemon_close(xs);
return ret;
}
#define dir_select(dir1, dir2) \
ctrl->wr_cons = &ctrl->ring->cons_##dir1; \
ctrl->wr_prod = &ctrl->ring->prod_##dir1; \
ctrl->rd_cons = &ctrl->ring->cons_##dir2; \
ctrl->rd_prod = &ctrl->ring->prod_##dir2; \
ctrl->wr_ring = ctrl->ring->buf_##dir1; \
ctrl->rd_ring = ctrl->ring->buf_##dir2; \
ctrl->wr_ring_size = sizeof(ctrl->ring->buf_##dir1); \
ctrl->rd_ring_size = sizeof(ctrl->ring->buf_##dir2)
/**
2012-02-22 17:54:58 +01:00
Run in AppVM (any domain).
Sleeps until the connection is established. (unless in stubdom)
2011-03-08 12:24:47 +01:00
\param devno something like a well-known port.
\returns NULL on failure, handle on success
*/
struct libvchan *libvchan_server_init(int devno)
{
struct libvchan *ctrl =
(struct libvchan *) malloc(sizeof(struct libvchan));
if (!ctrl)
return 0;
if (ring_init(ctrl))
return 0;;
if (server_interface_init(ctrl, devno))
return 0;
/*
We want the same code for read/write functions, regardless whether
we are client, or server. Thus, we do not access buf_in nor buf_out
buffers directly. Instead, in *_init functions, the dir_select
macro assigns proper values to wr* and rd* pointers, so that they
point to correct one out of buf_in or buf_out related fields.
*/
dir_select(in, out);
ctrl->is_server = 1;
return ctrl;
}
2012-02-22 17:54:58 +01:00
int libvchan_server_handle_connected(struct libvchan *ctrl)
{
struct xs_handle *xs;
char buf[64];
int ret = -1;
#ifdef WINNT
xs = xs_domain_open();
#else
2012-02-22 17:54:58 +01:00
xs = xs_daemon_open();
#endif
2012-02-22 17:54:58 +01:00
if (!xs) {
return ret;
}
#ifndef WINNT
2012-02-22 17:54:58 +01:00
// clear the pending flag
xc_evtchn_pending(ctrl->evfd);
#endif
2012-02-22 17:54:58 +01:00
snprintf(buf, sizeof buf, "device/vchan/%d", ctrl->devno);
xs_rm(xs, 0, buf);
ret = 0;
#if 0
fail2:
if (ret)
xc_evtchn_close(ctrl->evfd);
#endif
xs_daemon_close(xs);
return ret;
}
#ifndef WINNT
2011-03-08 12:24:47 +01:00
/**
retrieves ring-ref and event-channel numbers from xenstore (if
they don't exist, return error, because nobody seems to listen);
map the ring, connect the event channel
*/
static int client_interface_init(struct libvchan *ctrl, int domain, int devno)
{
int ret = -1;
unsigned int len;
struct xs_handle *xs;
#ifdef XENCTRL_HAS_XC_INTERFACE
xc_interface *xcfd;
2012-06-03 16:38:00 +02:00
xc_gnttab *xcg;
#else
2011-03-08 12:24:47 +01:00
int xcfd;
int xcg;
2012-06-03 16:38:00 +02:00
#endif
2011-03-08 12:24:47 +01:00
char buf[64];
char *ref;
int version;
#ifdef XENCTRL_HAS_XC_INTERFACE
xc_evtchn *evfd;
#else
2011-03-08 12:24:47 +01:00
int evfd;
#endif
2011-03-08 12:24:47 +01:00
int remote_port;
xs = xs_daemon_open();
if (!xs) {
return ret;
}
version = 1;
snprintf(buf, sizeof buf,
"/local/domain/%d/device/vchan/%d/version", domain,
devno);
ref = xs_read(xs, 0, buf, &len);
if (ref) {
version = atoi(ref);
free(ref);
}
2011-03-08 12:24:47 +01:00
snprintf(buf, sizeof buf,
"/local/domain/%d/device/vchan/%d/ring-ref", domain,
devno);
ref = xs_read(xs, 0, buf, &len);
if (!ref)
goto fail;
ctrl->ring_ref = atoi(ref);
2012-05-12 16:32:36 +02:00
free(ref);
2011-03-08 12:24:47 +01:00
if (!ctrl->ring_ref)
goto fail;
snprintf(buf, sizeof buf,
"/local/domain/%d/device/vchan/%d/event-channel", domain,
devno);
ref = xs_read(xs, 0, buf, &len);
if (!ref)
goto fail;
remote_port = atoi(ref);
2012-05-12 16:32:36 +02:00
free(ref);
2011-03-08 12:24:47 +01:00
if (!remote_port)
goto fail;
switch (version) {
case 1:
#ifdef XENCTRL_HAS_XC_INTERFACE
xcfd = xc_interface_open(NULL, NULL, 0);
if (!xcfd)
goto fail;
#else
xcfd = xc_interface_open();
if (xcfd < 0)
goto fail;
#endif
ctrl->ring = (struct vchan_interface *)
xc_map_foreign_range(xcfd, domain, 4096,
PROT_READ | PROT_WRITE, ctrl->ring_ref);
xc_interface_close(xcfd);
break;
case 2:
2012-06-03 16:38:00 +02:00
xcg = xc_gnttab_open(NULL, 0);
if (xcg < 0)
goto fail;
ctrl->ring = (struct vchan_interface *)
xc_gnttab_map_grant_ref(xcg, domain, ctrl->ring_ref, PROT_READ | PROT_WRITE);
xc_gnttab_close(xcg);
break;
default:
goto fail;
}
2011-03-08 12:24:47 +01:00
if (ctrl->ring == 0 || ctrl->ring == MAP_FAILED)
goto fail;
#ifdef XENCTRL_HAS_XC_INTERFACE
evfd = xc_evtchn_open(NULL, 0);
if (!evfd)
goto fail;
#else
2011-03-08 12:24:47 +01:00
evfd = xc_evtchn_open();
if (evfd < 0)
goto fail;
#endif
2011-03-08 12:24:47 +01:00
ctrl->evfd = evfd;
ctrl->evport =
xc_evtchn_bind_interdomain(evfd, domain, remote_port);
if (ctrl->evport < 0 || xc_evtchn_notify(evfd, ctrl->evport))
xc_evtchn_close(evfd);
2011-03-08 12:24:47 +01:00
else
ret = 0;
fail:
xs_daemon_close(xs);
return ret;
}
/**
Run on the client side of connection (currently, must be dom0).
\returns NULL on failure (e.g. noone listening), handle on success
2012-02-22 17:54:58 +01:00
*/
2011-03-08 12:24:47 +01:00
struct libvchan *libvchan_client_init(int domain, int devno)
{
struct libvchan *ctrl =
(struct libvchan *) malloc(sizeof(struct libvchan));
if (!ctrl)
return 0;
if (client_interface_init(ctrl, domain, devno))
return 0;
// See comment in libvchan_server_init
dir_select(out, in);
ctrl->is_server = 0;
return ctrl;
}
#else
// Windows domains can not be dom0
struct libvchan *libvchan_client_init(int domain, int devno)
{
return NULL;
}
#endif