xref: /freebsd/sys/dev/xen/xenstore/xenstored_dev.c (revision d48760ffe9eaac809f8d31fb1b4e6b13ae52d59a)
1557077b5SRoger Pau Monné /*
2557077b5SRoger Pau Monné  * Copyright (c) 2014 Roger Pau Monné <roger.pau@citrix.com>
3557077b5SRoger Pau Monné  * All rights reserved.
4557077b5SRoger Pau Monné  *
5557077b5SRoger Pau Monné  * Redistribution and use in source and binary forms, with or without
6557077b5SRoger Pau Monné  * modification, are permitted provided that the following conditions
7557077b5SRoger Pau Monné  * are met:
8557077b5SRoger Pau Monné  * 1. Redistributions of source code must retain the above copyright
9557077b5SRoger Pau Monné  *    notice, this list of conditions and the following disclaimer.
10557077b5SRoger Pau Monné  * 2. Redistributions in binary form must reproduce the above copyright
11557077b5SRoger Pau Monné  *    notice, this list of conditions and the following disclaimer in the
12557077b5SRoger Pau Monné  *    documentation and/or other materials provided with the distribution.
13557077b5SRoger Pau Monné  *
14557077b5SRoger Pau Monné  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS AS IS'' AND
15557077b5SRoger Pau Monné  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16557077b5SRoger Pau Monné  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17557077b5SRoger Pau Monné  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18557077b5SRoger Pau Monné  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19557077b5SRoger Pau Monné  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20557077b5SRoger Pau Monné  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21557077b5SRoger Pau Monné  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22557077b5SRoger Pau Monné  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23557077b5SRoger Pau Monné  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24557077b5SRoger Pau Monné  * SUCH DAMAGE.
25557077b5SRoger Pau Monné  */
26557077b5SRoger Pau Monné 
27557077b5SRoger Pau Monné #include <sys/types.h>
28557077b5SRoger Pau Monné #include <sys/cdefs.h>
29557077b5SRoger Pau Monné #include <sys/errno.h>
30557077b5SRoger Pau Monné #include <sys/uio.h>
31557077b5SRoger Pau Monné #include <sys/param.h>
32557077b5SRoger Pau Monné #include <sys/systm.h>
33557077b5SRoger Pau Monné #include <sys/proc.h>
34557077b5SRoger Pau Monné #include <sys/kernel.h>
35557077b5SRoger Pau Monné #include <sys/malloc.h>
36557077b5SRoger Pau Monné #include <sys/conf.h>
37557077b5SRoger Pau Monné #include <sys/module.h>
38557077b5SRoger Pau Monné 
39557077b5SRoger Pau Monné #include <xen/xen-os.h>
40557077b5SRoger Pau Monné 
41557077b5SRoger Pau Monné #include <xen/hypervisor.h>
42557077b5SRoger Pau Monné #include <xen/xenstore/xenstorevar.h>
43557077b5SRoger Pau Monné 
44557077b5SRoger Pau Monné #include <vm/vm.h>
45557077b5SRoger Pau Monné #include <vm/pmap.h>
46557077b5SRoger Pau Monné 
47557077b5SRoger Pau Monné #define XSD_READ_SIZE		20
48557077b5SRoger Pau Monné 
49557077b5SRoger Pau Monné static int xsd_dev_read(struct cdev *dev, struct uio *uio, int ioflag);
50557077b5SRoger Pau Monné static int xsd_dev_mmap(struct cdev *dev, vm_ooffset_t offset,
51557077b5SRoger Pau Monné     vm_paddr_t *paddr, int nprot, vm_memattr_t *memattr);
52557077b5SRoger Pau Monné 
53557077b5SRoger Pau Monné static struct cdevsw xsd_dev_cdevsw = {
54557077b5SRoger Pau Monné 	.d_version = D_VERSION,
55557077b5SRoger Pau Monné 	.d_read = xsd_dev_read,
56557077b5SRoger Pau Monné 	.d_mmap = xsd_dev_mmap,
57557077b5SRoger Pau Monné 	.d_name = "xsd_dev",
58557077b5SRoger Pau Monné };
59557077b5SRoger Pau Monné 
60557077b5SRoger Pau Monné static int
xsd_dev_read(struct cdev * dev,struct uio * uio,int ioflag)61557077b5SRoger Pau Monné xsd_dev_read(struct cdev *dev, struct uio *uio, int ioflag)
62557077b5SRoger Pau Monné {
63557077b5SRoger Pau Monné 	char evtchn[XSD_READ_SIZE];
64557077b5SRoger Pau Monné 	int error, len;
65557077b5SRoger Pau Monné 
66cfa0b7b8SRoger Pau Monné 	len = snprintf(evtchn, sizeof(evtchn), "%u", xs_evtchn());
67557077b5SRoger Pau Monné 	if (len < 0 || len > uio->uio_resid)
68557077b5SRoger Pau Monné 		return (EINVAL);
69557077b5SRoger Pau Monné 
70557077b5SRoger Pau Monné 	error = uiomove(evtchn, len, uio);
71557077b5SRoger Pau Monné 	if (error)
72557077b5SRoger Pau Monné 		return (error);
73557077b5SRoger Pau Monné 
74557077b5SRoger Pau Monné 	return (0);
75557077b5SRoger Pau Monné }
76557077b5SRoger Pau Monné 
77557077b5SRoger Pau Monné static int
xsd_dev_mmap(struct cdev * dev,vm_ooffset_t offset,vm_paddr_t * paddr,int nprot,vm_memattr_t * memattr)78557077b5SRoger Pau Monné xsd_dev_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
79557077b5SRoger Pau Monné     int nprot, vm_memattr_t *memattr)
80557077b5SRoger Pau Monné {
81557077b5SRoger Pau Monné 
82557077b5SRoger Pau Monné 	if (offset != 0)
83557077b5SRoger Pau Monné 		return (EINVAL);
84557077b5SRoger Pau Monné 
85cfa0b7b8SRoger Pau Monné 	*paddr = xs_address();
86557077b5SRoger Pau Monné 
87557077b5SRoger Pau Monné 	return (0);
88557077b5SRoger Pau Monné }
89557077b5SRoger Pau Monné 
90557077b5SRoger Pau Monné /*------------------ Private Device Attachment Functions  --------------------*/
91557077b5SRoger Pau Monné /**
92557077b5SRoger Pau Monné  * \brief Identify instances of this device type in the system.
93557077b5SRoger Pau Monné  *
94557077b5SRoger Pau Monné  * \param driver  The driver performing this identify action.
95557077b5SRoger Pau Monné  * \param parent  The NewBus parent device for any devices this method adds.
96557077b5SRoger Pau Monné  */
97557077b5SRoger Pau Monné static void
xsd_dev_identify(driver_t * driver,device_t parent)98*d48760ffSElliott Mitchell xsd_dev_identify(driver_t *driver, device_t parent)
99557077b5SRoger Pau Monné {
100557077b5SRoger Pau Monné 
101cfa0b7b8SRoger Pau Monné 	if (!xen_domain() || xs_initialized())
102557077b5SRoger Pau Monné 		return;
103557077b5SRoger Pau Monné 
104557077b5SRoger Pau Monné 	/*
105557077b5SRoger Pau Monné 	 * Only attach if xenstore is not available, because we are the
106557077b5SRoger Pau Monné 	 * domain that's supposed to run it.
107557077b5SRoger Pau Monné 	 */
108557077b5SRoger Pau Monné 	BUS_ADD_CHILD(parent, 0, driver->name, 0);
109557077b5SRoger Pau Monné }
110557077b5SRoger Pau Monné 
111557077b5SRoger Pau Monné /**
112557077b5SRoger Pau Monné  * \brief Probe for the existence of the Xenstored device
113557077b5SRoger Pau Monné  *
114557077b5SRoger Pau Monné  * \param dev  NewBus device_t for this instance.
115557077b5SRoger Pau Monné  *
116557077b5SRoger Pau Monné  * \return  Always returns 0 indicating success.
117557077b5SRoger Pau Monné  */
118557077b5SRoger Pau Monné static int
xsd_dev_probe(device_t dev)119557077b5SRoger Pau Monné xsd_dev_probe(device_t dev)
120557077b5SRoger Pau Monné {
121557077b5SRoger Pau Monné 
122557077b5SRoger Pau Monné 	device_set_desc(dev, "Xenstored user-space device");
123557077b5SRoger Pau Monné 	return (BUS_PROBE_NOWILDCARD);
124557077b5SRoger Pau Monné }
125557077b5SRoger Pau Monné 
126557077b5SRoger Pau Monné /**
127557077b5SRoger Pau Monné  * \brief Attach the Xenstored device.
128557077b5SRoger Pau Monné  *
129557077b5SRoger Pau Monné  * \param dev  NewBus device_t for this instance.
130557077b5SRoger Pau Monné  *
131557077b5SRoger Pau Monné  * \return  On success, 0. Otherwise an errno value indicating the
132557077b5SRoger Pau Monné  *          type of failure.
133557077b5SRoger Pau Monné  */
134557077b5SRoger Pau Monné static int
xsd_dev_attach(device_t dev)135557077b5SRoger Pau Monné xsd_dev_attach(device_t dev)
136557077b5SRoger Pau Monné {
137557077b5SRoger Pau Monné 	struct cdev *xsd_cdev;
138557077b5SRoger Pau Monné 
139557077b5SRoger Pau Monné 	xsd_cdev = make_dev(&xsd_dev_cdevsw, 0, UID_ROOT, GID_WHEEL, 0400,
140557077b5SRoger Pau Monné 	    "xen/xenstored");
141557077b5SRoger Pau Monné 	if (xsd_cdev == NULL)
142557077b5SRoger Pau Monné 		return (EINVAL);
143557077b5SRoger Pau Monné 
144557077b5SRoger Pau Monné 	return (0);
145557077b5SRoger Pau Monné }
146557077b5SRoger Pau Monné 
147557077b5SRoger Pau Monné /*-------------------- Private Device Attachment Data  -----------------------*/
148557077b5SRoger Pau Monné static device_method_t xsd_dev_methods[] = {
149557077b5SRoger Pau Monné 	/* Device interface */
150557077b5SRoger Pau Monné 	DEVMETHOD(device_identify,	xsd_dev_identify),
151557077b5SRoger Pau Monné 	DEVMETHOD(device_probe,         xsd_dev_probe),
152557077b5SRoger Pau Monné 	DEVMETHOD(device_attach,        xsd_dev_attach),
153557077b5SRoger Pau Monné 
154557077b5SRoger Pau Monné 	DEVMETHOD_END
155557077b5SRoger Pau Monné };
156557077b5SRoger Pau Monné 
157557077b5SRoger Pau Monné DEFINE_CLASS_0(xsd_dev, xsd_dev_driver, xsd_dev_methods, 0);
158557077b5SRoger Pau Monné 
159f929eb1eSJohn Baldwin DRIVER_MODULE(xsd_dev, xenpv, xsd_dev_driver, NULL, NULL);
160