summaryrefslogtreecommitdiffstats
path: root/sys/dev/xen/xenstore/xenstored_dev.c
blob: ae24085dc80918a193a16634469de68661c4efe1 (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
/*
 * Copyright (c) 2014 Roger Pau Monné <roger.pau@citrix.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/types.h>
#include <sys/cdefs.h>
#include <sys/errno.h>
#include <sys/uio.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/conf.h>
#include <sys/module.h>

#include <xen/xen-os.h>

#include <xen/hypervisor.h>
#include <xen/xenstore/xenstorevar.h>
#include <xen/xenstore/xenstore_internal.h>

#include <vm/vm.h>
#include <vm/pmap.h>

#define XSD_READ_SIZE		20

static int xsd_dev_read(struct cdev *dev, struct uio *uio, int ioflag);
static int xsd_dev_mmap(struct cdev *dev, vm_ooffset_t offset,
    vm_paddr_t *paddr, int nprot, vm_memattr_t *memattr);


static struct cdevsw xsd_dev_cdevsw = {
	.d_version = D_VERSION,
	.d_read = xsd_dev_read,
	.d_mmap = xsd_dev_mmap,
	.d_name = "xsd_dev",
};

static int
xsd_dev_read(struct cdev *dev, struct uio *uio, int ioflag)
{
	char evtchn[XSD_READ_SIZE];
	int error, len;

	len = snprintf(evtchn, sizeof(evtchn), "%u",
	    HYPERVISOR_start_info->store_evtchn);
	if (len < 0 || len > uio->uio_resid)
		return (EINVAL);

	error = uiomove(evtchn, len, uio);
	if (error)
		return (error);

	return (0);
}

static int
xsd_dev_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
    int nprot, vm_memattr_t *memattr)
{

	if (offset != 0)
		return (EINVAL);

	*paddr = pmap_kextract((vm_offset_t)xen_store);

	return (0);
}

/*------------------ Private Device Attachment Functions  --------------------*/
/**
 * \brief Identify instances of this device type in the system.
 *
 * \param driver  The driver performing this identify action.
 * \param parent  The NewBus parent device for any devices this method adds.
 */
static void
xsd_dev_identify(driver_t *driver __unused, device_t parent)
{

	if (!xen_pv_domain())
		return;
	if (HYPERVISOR_start_info->store_mfn != 0)
		return;

	/*
	 * Only attach if xenstore is not available, because we are the
	 * domain that's supposed to run it.
	 */
	BUS_ADD_CHILD(parent, 0, driver->name, 0);
}

/**
 * \brief Probe for the existence of the Xenstored device
 *
 * \param dev  NewBus device_t for this instance.
 *
 * \return  Always returns 0 indicating success.
 */
static int 
xsd_dev_probe(device_t dev)
{

	device_set_desc(dev, "Xenstored user-space device");
	return (BUS_PROBE_NOWILDCARD);
}

/**
 * \brief Attach the Xenstored device.
 *
 * \param dev  NewBus device_t for this instance.
 *
 * \return  On success, 0. Otherwise an errno value indicating the
 *          type of failure.
 */
static int
xsd_dev_attach(device_t dev)
{
	struct cdev *xsd_cdev;

	xsd_cdev = make_dev(&xsd_dev_cdevsw, 0, UID_ROOT, GID_WHEEL, 0400,
	    "xen/xenstored");
	if (xsd_cdev == NULL)
		return (EINVAL);

	return (0);
}

/*-------------------- Private Device Attachment Data  -----------------------*/
static device_method_t xsd_dev_methods[] = {
	/* Device interface */
	DEVMETHOD(device_identify,	xsd_dev_identify),
	DEVMETHOD(device_probe,         xsd_dev_probe),
	DEVMETHOD(device_attach,        xsd_dev_attach),

	DEVMETHOD_END
};

DEFINE_CLASS_0(xsd_dev, xsd_dev_driver, xsd_dev_methods, 0);
devclass_t xsd_dev_devclass;

DRIVER_MODULE(xsd_dev, xenpv, xsd_dev_driver, xsd_dev_devclass,
    NULL, NULL);
OpenPOWER on IntegriCloud