xenstored_dev.c revision 303975
167754Smsmith/*
267754Smsmith * Copyright (c) 2014 Roger Pau Monn�� <roger.pau@citrix.com>
367754Smsmith * All rights reserved.
483174Smsmith *
567754Smsmith * Redistribution and use in source and binary forms, with or without
667754Smsmith * modification, are permitted provided that the following conditions
767754Smsmith * are met:
867754Smsmith * 1. Redistributions of source code must retain the above copyright
967754Smsmith *    notice, this list of conditions and the following disclaimer.
1067754Smsmith * 2. Redistributions in binary form must reproduce the above copyright
1167754Smsmith *    notice, this list of conditions and the following disclaimer in the
1271867Smsmith *    documentation and/or other materials provided with the distribution.
1370243Smsmith *
1467754Smsmith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS AS IS'' AND
1567754Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1667754Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1767754Smsmith * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1867754Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1967754Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2067754Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2167754Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2267754Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2367754Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2467754Smsmith * SUCH DAMAGE.
2567754Smsmith */
2667754Smsmith
2767754Smsmith#include <sys/cdefs.h>
2867754Smsmith__FBSDID("$FreeBSD: releng/11.0/sys/dev/xen/xenstore/xenstored_dev.c 272319 2014-09-30 17:37:26Z royger $");
2967754Smsmith
3067754Smsmith#include <sys/types.h>
3167754Smsmith#include <sys/cdefs.h>
3267754Smsmith#include <sys/errno.h>
3367754Smsmith#include <sys/uio.h>
3467754Smsmith#include <sys/param.h>
3567754Smsmith#include <sys/systm.h>
3667754Smsmith#include <sys/proc.h>
3767754Smsmith#include <sys/kernel.h>
3867754Smsmith#include <sys/malloc.h>
3967754Smsmith#include <sys/conf.h>
4067754Smsmith#include <sys/module.h>
4167754Smsmith
4267754Smsmith#include <xen/xen-os.h>
4367754Smsmith
4467754Smsmith#include <xen/hypervisor.h>
4567754Smsmith#include <xen/xenstore/xenstorevar.h>
4667754Smsmith#include <xen/xenstore/xenstore_internal.h>
4767754Smsmith
4867754Smsmith#include <vm/vm.h>
4967754Smsmith#include <vm/pmap.h>
5067754Smsmith
5167754Smsmith#define XSD_READ_SIZE		20
5267754Smsmith
5367754Smsmithstatic int xsd_dev_read(struct cdev *dev, struct uio *uio, int ioflag);
5467754Smsmithstatic int xsd_dev_mmap(struct cdev *dev, vm_ooffset_t offset,
5567754Smsmith    vm_paddr_t *paddr, int nprot, vm_memattr_t *memattr);
5667754Smsmith
5767754Smsmith
5867754Smsmithstatic struct cdevsw xsd_dev_cdevsw = {
5967754Smsmith	.d_version = D_VERSION,
6067754Smsmith	.d_read = xsd_dev_read,
6167754Smsmith	.d_mmap = xsd_dev_mmap,
6267754Smsmith	.d_name = "xsd_dev",
6367754Smsmith};
6467754Smsmith
6567754Smsmithstatic int
6667754Smsmithxsd_dev_read(struct cdev *dev, struct uio *uio, int ioflag)
6767754Smsmith{
6867754Smsmith	char evtchn[XSD_READ_SIZE];
6967754Smsmith	int error, len;
7067754Smsmith
7167754Smsmith	len = snprintf(evtchn, sizeof(evtchn), "%u",
7267754Smsmith	    HYPERVISOR_start_info->store_evtchn);
7367754Smsmith	if (len < 0 || len > uio->uio_resid)
7467754Smsmith		return (EINVAL);
7567754Smsmith
7667754Smsmith	error = uiomove(evtchn, len, uio);
7767754Smsmith	if (error)
7867754Smsmith		return (error);
7967754Smsmith
8067754Smsmith	return (0);
8167754Smsmith}
8267754Smsmith
8367754Smsmithstatic int
8467754Smsmithxsd_dev_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
8567754Smsmith    int nprot, vm_memattr_t *memattr)
8667754Smsmith{
8767754Smsmith
8867754Smsmith	if (offset != 0)
8967754Smsmith		return (EINVAL);
9067754Smsmith
9167754Smsmith	*paddr = pmap_kextract((vm_offset_t)xen_store);
9267754Smsmith
9367754Smsmith	return (0);
9467754Smsmith}
9567754Smsmith
9667754Smsmith/*------------------ Private Device Attachment Functions  --------------------*/
9767754Smsmith/**
9867754Smsmith * \brief Identify instances of this device type in the system.
9967754Smsmith *
10067754Smsmith * \param driver  The driver performing this identify action.
10167754Smsmith * \param parent  The NewBus parent device for any devices this method adds.
10267754Smsmith */
10367754Smsmithstatic void
10467754Smsmithxsd_dev_identify(driver_t *driver __unused, device_t parent)
10567754Smsmith{
10667754Smsmith
10767754Smsmith	if (!xen_pv_domain())
10867754Smsmith		return;
10967754Smsmith	if (HYPERVISOR_start_info->store_mfn != 0)
11067754Smsmith		return;
11167754Smsmith
11267754Smsmith	/*
11367754Smsmith	 * Only attach if xenstore is not available, because we are the
11467754Smsmith	 * domain that's supposed to run it.
11567754Smsmith	 */
11667754Smsmith	BUS_ADD_CHILD(parent, 0, driver->name, 0);
11767754Smsmith}
11867754Smsmith
11967754Smsmith/**
12067754Smsmith * \brief Probe for the existence of the Xenstored device
12167754Smsmith *
12267754Smsmith * \param dev  NewBus device_t for this instance.
12367754Smsmith *
12467754Smsmith * \return  Always returns 0 indicating success.
12577424Smsmith */
12667754Smsmithstatic int
12767754Smsmithxsd_dev_probe(device_t dev)
12867754Smsmith{
12967754Smsmith
13067754Smsmith	device_set_desc(dev, "Xenstored user-space device");
13167754Smsmith	return (BUS_PROBE_NOWILDCARD);
13267754Smsmith}
13383174Smsmith
13467754Smsmith/**
13567754Smsmith * \brief Attach the Xenstored device.
13667754Smsmith *
13783174Smsmith * \param dev  NewBus device_t for this instance.
13867754Smsmith *
13967754Smsmith * \return  On success, 0. Otherwise an errno value indicating the
14067754Smsmith *          type of failure.
14167754Smsmith */
14267754Smsmithstatic int
14367754Smsmithxsd_dev_attach(device_t dev)
14467754Smsmith{
14567754Smsmith	struct cdev *xsd_cdev;
14667754Smsmith
14783174Smsmith	xsd_cdev = make_dev(&xsd_dev_cdevsw, 0, UID_ROOT, GID_WHEEL, 0400,
14867754Smsmith	    "xen/xenstored");
14967754Smsmith	if (xsd_cdev == NULL)
15067754Smsmith		return (EINVAL);
15180062Smsmith
15267754Smsmith	return (0);
15367754Smsmith}
15467754Smsmith
15567754Smsmith/*-------------------- Private Device Attachment Data  -----------------------*/
15667754Smsmithstatic device_method_t xsd_dev_methods[] = {
15782367Smsmith	/* Device interface */
15867754Smsmith	DEVMETHOD(device_identify,	xsd_dev_identify),
15967754Smsmith	DEVMETHOD(device_probe,         xsd_dev_probe),
16067754Smsmith	DEVMETHOD(device_attach,        xsd_dev_attach),
16167754Smsmith
16267754Smsmith	DEVMETHOD_END
16367754Smsmith};
16467754Smsmith
16567754SmsmithDEFINE_CLASS_0(xsd_dev, xsd_dev_driver, xsd_dev_methods, 0);
16667754Smsmithdevclass_t xsd_dev_devclass;
16767754Smsmith
16867754SmsmithDRIVER_MODULE(xsd_dev, xenpv, xsd_dev_driver, xsd_dev_devclass,
16967754Smsmith    NULL, NULL);
17067754Smsmith