xref: /freebsd/sys/dev/xen/xenstore/xenstore.c (revision 453130d9bfc1c6d68b366dfcb041689d69f81295)
1a6aedc5dSRoger Pau Monné /******************************************************************************
2a6aedc5dSRoger Pau Monné  * xenstore.c
3a6aedc5dSRoger Pau Monné  *
4a6aedc5dSRoger Pau Monné  * Low-level kernel interface to the XenStore.
5a6aedc5dSRoger Pau Monné  *
6a6aedc5dSRoger Pau Monné  * Copyright (C) 2005 Rusty Russell, IBM Corporation
7a6aedc5dSRoger Pau Monné  * Copyright (C) 2009,2010 Spectra Logic Corporation
8a6aedc5dSRoger Pau Monné  *
9a6aedc5dSRoger Pau Monné  * This file may be distributed separately from the Linux kernel, or
10a6aedc5dSRoger Pau Monné  * incorporated into other software packages, subject to the following license:
11a6aedc5dSRoger Pau Monné  *
12a6aedc5dSRoger Pau Monné  * Permission is hereby granted, free of charge, to any person obtaining a copy
13a6aedc5dSRoger Pau Monné  * of this source file (the "Software"), to deal in the Software without
14a6aedc5dSRoger Pau Monné  * restriction, including without limitation the rights to use, copy, modify,
15a6aedc5dSRoger Pau Monné  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
16a6aedc5dSRoger Pau Monné  * and to permit persons to whom the Software is furnished to do so, subject to
17a6aedc5dSRoger Pau Monné  * the following conditions:
18a6aedc5dSRoger Pau Monné  *
19a6aedc5dSRoger Pau Monné  * The above copyright notice and this permission notice shall be included in
20a6aedc5dSRoger Pau Monné  * all copies or substantial portions of the Software.
21a6aedc5dSRoger Pau Monné  *
22a6aedc5dSRoger Pau Monné  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23a6aedc5dSRoger Pau Monné  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24a6aedc5dSRoger Pau Monné  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25a6aedc5dSRoger Pau Monné  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26a6aedc5dSRoger Pau Monné  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27a6aedc5dSRoger Pau Monné  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28a6aedc5dSRoger Pau Monné  * IN THE SOFTWARE.
29a6aedc5dSRoger Pau Monné  */
30a6aedc5dSRoger Pau Monné 
31a6aedc5dSRoger Pau Monné 
32a6aedc5dSRoger Pau Monné #include <sys/cdefs.h>
33a6aedc5dSRoger Pau Monné __FBSDID("$FreeBSD$");
34a6aedc5dSRoger Pau Monné 
35a6aedc5dSRoger Pau Monné #include <sys/param.h>
36a6aedc5dSRoger Pau Monné #include <sys/bus.h>
37a6aedc5dSRoger Pau Monné #include <sys/kernel.h>
38a6aedc5dSRoger Pau Monné #include <sys/lock.h>
39a6aedc5dSRoger Pau Monné #include <sys/module.h>
40a6aedc5dSRoger Pau Monné #include <sys/mutex.h>
41a6aedc5dSRoger Pau Monné #include <sys/sx.h>
42a6aedc5dSRoger Pau Monné #include <sys/syslog.h>
43a6aedc5dSRoger Pau Monné #include <sys/malloc.h>
44a6aedc5dSRoger Pau Monné #include <sys/systm.h>
45a6aedc5dSRoger Pau Monné #include <sys/proc.h>
46a6aedc5dSRoger Pau Monné #include <sys/kthread.h>
47a6aedc5dSRoger Pau Monné #include <sys/sbuf.h>
48a6aedc5dSRoger Pau Monné #include <sys/sysctl.h>
49a6aedc5dSRoger Pau Monné #include <sys/uio.h>
50a6aedc5dSRoger Pau Monné #include <sys/unistd.h>
511d84e2b3SRoger Pau Monné #include <sys/queue.h>
521d84e2b3SRoger Pau Monné #include <sys/taskqueue.h>
53a6aedc5dSRoger Pau Monné 
54a6aedc5dSRoger Pau Monné #include <machine/stdarg.h>
55a6aedc5dSRoger Pau Monné 
56a6aedc5dSRoger Pau Monné #include <xen/xen-os.h>
57a6aedc5dSRoger Pau Monné #include <xen/hypervisor.h>
58a6aedc5dSRoger Pau Monné #include <xen/xen_intr.h>
59a6aedc5dSRoger Pau Monné 
60a6aedc5dSRoger Pau Monné #include <xen/interface/hvm/params.h>
61a6aedc5dSRoger Pau Monné #include <xen/hvm.h>
62a6aedc5dSRoger Pau Monné 
63a6aedc5dSRoger Pau Monné #include <xen/xenstore/xenstorevar.h>
64a6aedc5dSRoger Pau Monné #include <xen/xenstore/xenstore_internal.h>
65a6aedc5dSRoger Pau Monné 
66a6aedc5dSRoger Pau Monné #include <vm/vm.h>
67a6aedc5dSRoger Pau Monné #include <vm/pmap.h>
68a6aedc5dSRoger Pau Monné 
69a6aedc5dSRoger Pau Monné /**
70a6aedc5dSRoger Pau Monné  * \file xenstore.c
71a6aedc5dSRoger Pau Monné  * \brief XenStore interface
72a6aedc5dSRoger Pau Monné  *
73a6aedc5dSRoger Pau Monné  * The XenStore interface is a simple storage system that is a means of
74a6aedc5dSRoger Pau Monné  * communicating state and configuration data between the Xen Domain 0
75a6aedc5dSRoger Pau Monné  * and the various guest domains.  All configuration data other than
76a6aedc5dSRoger Pau Monné  * a small amount of essential information required during the early
77a6aedc5dSRoger Pau Monné  * boot process of launching a Xen aware guest, is managed using the
78a6aedc5dSRoger Pau Monné  * XenStore.
79a6aedc5dSRoger Pau Monné  *
80a6aedc5dSRoger Pau Monné  * The XenStore is ASCII string based, and has a structure and semantics
81a6aedc5dSRoger Pau Monné  * similar to a filesystem.  There are files and directories, the directories
82*453130d9SPedro F. Giffuni  * able to contain files or other directories.  The depth of the hierarchy
83a6aedc5dSRoger Pau Monné  * is only limited by the XenStore's maximum path length.
84a6aedc5dSRoger Pau Monné  *
85a6aedc5dSRoger Pau Monné  * The communication channel between the XenStore service and other
86a6aedc5dSRoger Pau Monné  * domains is via two, guest specific, ring buffers in a shared memory
87a6aedc5dSRoger Pau Monné  * area.  One ring buffer is used for communicating in each direction.
88a6aedc5dSRoger Pau Monné  * The grant table references for this shared memory are given to the
89a6aedc5dSRoger Pau Monné  * guest either via the xen_start_info structure for a fully para-
90a6aedc5dSRoger Pau Monné  * virtualized guest, or via HVM hypercalls for a hardware virtualized
91a6aedc5dSRoger Pau Monné  * guest.
92a6aedc5dSRoger Pau Monné  *
93a6aedc5dSRoger Pau Monné  * The XenStore communication relies on an event channel and thus
94a6aedc5dSRoger Pau Monné  * interrupts.  For this reason, the attachment of the XenStore
95a6aedc5dSRoger Pau Monné  * relies on an interrupt driven configuration hook to hold off
96a6aedc5dSRoger Pau Monné  * boot processing until communication with the XenStore service
97a6aedc5dSRoger Pau Monné  * can be established.
98a6aedc5dSRoger Pau Monné  *
99a6aedc5dSRoger Pau Monné  * Several Xen services depend on the XenStore, most notably the
100a6aedc5dSRoger Pau Monné  * XenBus used to discover and manage Xen devices.  These services
101a6aedc5dSRoger Pau Monné  * are implemented as NewBus child attachments to a bus exported
102a6aedc5dSRoger Pau Monné  * by this XenStore driver.
103a6aedc5dSRoger Pau Monné  */
104a6aedc5dSRoger Pau Monné 
105a6aedc5dSRoger Pau Monné static struct xs_watch *find_watch(const char *token);
106a6aedc5dSRoger Pau Monné 
107a6aedc5dSRoger Pau Monné MALLOC_DEFINE(M_XENSTORE, "xenstore", "XenStore data and results");
108a6aedc5dSRoger Pau Monné 
109a6aedc5dSRoger Pau Monné /**
110a6aedc5dSRoger Pau Monné  * Pointer to shared memory communication structures allowing us
111a6aedc5dSRoger Pau Monné  * to communicate with the XenStore service.
112a6aedc5dSRoger Pau Monné  *
113a6aedc5dSRoger Pau Monné  * When operating in full PV mode, this pointer is set early in kernel
114a6aedc5dSRoger Pau Monné  * startup from within xen_machdep.c.  In HVM mode, we use hypercalls
115a6aedc5dSRoger Pau Monné  * to get the guest frame number for the shared page and then map it
116a6aedc5dSRoger Pau Monné  * into kva.  See xs_init() for details.
117a6aedc5dSRoger Pau Monné  */
118a6aedc5dSRoger Pau Monné struct xenstore_domain_interface *xen_store;
119a6aedc5dSRoger Pau Monné 
120a6aedc5dSRoger Pau Monné /*-------------------------- Private Data Structures ------------------------*/
121a6aedc5dSRoger Pau Monné 
122a6aedc5dSRoger Pau Monné /**
123a6aedc5dSRoger Pau Monné  * Structure capturing messages received from the XenStore service.
124a6aedc5dSRoger Pau Monné  */
125a6aedc5dSRoger Pau Monné struct xs_stored_msg {
126a6aedc5dSRoger Pau Monné 	TAILQ_ENTRY(xs_stored_msg) list;
127a6aedc5dSRoger Pau Monné 
128a6aedc5dSRoger Pau Monné 	struct xsd_sockmsg hdr;
129a6aedc5dSRoger Pau Monné 
130a6aedc5dSRoger Pau Monné 	union {
131a6aedc5dSRoger Pau Monné 		/* Queued replies. */
132a6aedc5dSRoger Pau Monné 		struct {
133a6aedc5dSRoger Pau Monné 			char *body;
134a6aedc5dSRoger Pau Monné 		} reply;
135a6aedc5dSRoger Pau Monné 
136a6aedc5dSRoger Pau Monné 		/* Queued watch events. */
137a6aedc5dSRoger Pau Monné 		struct {
138a6aedc5dSRoger Pau Monné 			struct xs_watch *handle;
139a6aedc5dSRoger Pau Monné 			const char **vec;
140a6aedc5dSRoger Pau Monné 			u_int vec_size;
141a6aedc5dSRoger Pau Monné 		} watch;
142a6aedc5dSRoger Pau Monné 	} u;
143a6aedc5dSRoger Pau Monné };
144a6aedc5dSRoger Pau Monné TAILQ_HEAD(xs_stored_msg_list, xs_stored_msg);
145a6aedc5dSRoger Pau Monné 
146a6aedc5dSRoger Pau Monné /**
147a6aedc5dSRoger Pau Monné  * Container for all XenStore related state.
148a6aedc5dSRoger Pau Monné  */
149a6aedc5dSRoger Pau Monné struct xs_softc {
150a6aedc5dSRoger Pau Monné 	/** Newbus device for the XenStore. */
151a6aedc5dSRoger Pau Monné 	device_t xs_dev;
152a6aedc5dSRoger Pau Monné 
153a6aedc5dSRoger Pau Monné 	/**
154a6aedc5dSRoger Pau Monné 	 * Lock serializing access to ring producer/consumer
155a6aedc5dSRoger Pau Monné 	 * indexes.  Use of this lock guarantees that wakeups
156a6aedc5dSRoger Pau Monné 	 * of blocking readers/writers are not missed due to
157a6aedc5dSRoger Pau Monné 	 * races with the XenStore service.
158a6aedc5dSRoger Pau Monné 	 */
159a6aedc5dSRoger Pau Monné 	struct mtx ring_lock;
160a6aedc5dSRoger Pau Monné 
161a6aedc5dSRoger Pau Monné 	/*
162a6aedc5dSRoger Pau Monné 	 * Mutex used to insure exclusive access to the outgoing
163a6aedc5dSRoger Pau Monné 	 * communication ring.  We use a lock type that can be
164a6aedc5dSRoger Pau Monné 	 * held while sleeping so that xs_write() can block waiting
165a6aedc5dSRoger Pau Monné 	 * for space in the ring to free up, without allowing another
166a6aedc5dSRoger Pau Monné 	 * writer to come in and corrupt a partial message write.
167a6aedc5dSRoger Pau Monné 	 */
168a6aedc5dSRoger Pau Monné 	struct sx request_mutex;
169a6aedc5dSRoger Pau Monné 
170a6aedc5dSRoger Pau Monné 	/**
171a6aedc5dSRoger Pau Monné 	 * A list of replies to our requests.
172a6aedc5dSRoger Pau Monné 	 *
173a6aedc5dSRoger Pau Monné 	 * The reply list is filled by xs_rcv_thread().  It
174a6aedc5dSRoger Pau Monné 	 * is consumed by the context that issued the request
175a6aedc5dSRoger Pau Monné 	 * to which a reply is made.  The requester blocks in
176a6aedc5dSRoger Pau Monné 	 * xs_read_reply().
177a6aedc5dSRoger Pau Monné 	 *
178a6aedc5dSRoger Pau Monné 	 * /note Only one requesting context can be active at a time.
179a6aedc5dSRoger Pau Monné 	 *       This is guaranteed by the request_mutex and insures
180a6aedc5dSRoger Pau Monné 	 *	 that the requester sees replies matching the order
181a6aedc5dSRoger Pau Monné 	 *	 of its requests.
182a6aedc5dSRoger Pau Monné 	 */
183a6aedc5dSRoger Pau Monné 	struct xs_stored_msg_list reply_list;
184a6aedc5dSRoger Pau Monné 
185a6aedc5dSRoger Pau Monné 	/** Lock protecting the reply list. */
186a6aedc5dSRoger Pau Monné 	struct mtx reply_lock;
187a6aedc5dSRoger Pau Monné 
188a6aedc5dSRoger Pau Monné 	/**
189a6aedc5dSRoger Pau Monné 	 * List of registered watches.
190a6aedc5dSRoger Pau Monné 	 */
191a6aedc5dSRoger Pau Monné 	struct xs_watch_list  registered_watches;
192a6aedc5dSRoger Pau Monné 
193a6aedc5dSRoger Pau Monné 	/** Lock protecting the registered watches list. */
194a6aedc5dSRoger Pau Monné 	struct mtx registered_watches_lock;
195a6aedc5dSRoger Pau Monné 
196a6aedc5dSRoger Pau Monné 	/**
197a6aedc5dSRoger Pau Monné 	 * List of pending watch callback events.
198a6aedc5dSRoger Pau Monné 	 */
199a6aedc5dSRoger Pau Monné 	struct xs_stored_msg_list watch_events;
200a6aedc5dSRoger Pau Monné 
201a6aedc5dSRoger Pau Monné 	/** Lock protecting the watch calback list. */
202a6aedc5dSRoger Pau Monné 	struct mtx watch_events_lock;
203a6aedc5dSRoger Pau Monné 
204a6aedc5dSRoger Pau Monné 	/**
205a6aedc5dSRoger Pau Monné 	 * Sleepable lock used to prevent VM suspension while a
206a6aedc5dSRoger Pau Monné 	 * xenstore transaction is outstanding.
207a6aedc5dSRoger Pau Monné 	 *
208a6aedc5dSRoger Pau Monné 	 * Each active transaction holds a shared lock on the
209a6aedc5dSRoger Pau Monné 	 * suspend mutex.  Our suspend method blocks waiting
210a6aedc5dSRoger Pau Monné 	 * to acquire an exclusive lock.  This guarantees that
211a6aedc5dSRoger Pau Monné 	 * suspend processing will only proceed once all active
212a6aedc5dSRoger Pau Monné 	 * transactions have been retired.
213a6aedc5dSRoger Pau Monné 	 */
214a6aedc5dSRoger Pau Monné 	struct sx suspend_mutex;
215a6aedc5dSRoger Pau Monné 
216a6aedc5dSRoger Pau Monné 	/**
217a6aedc5dSRoger Pau Monné 	 * The processid of the xenwatch thread.
218a6aedc5dSRoger Pau Monné 	 */
219a6aedc5dSRoger Pau Monné 	pid_t xenwatch_pid;
220a6aedc5dSRoger Pau Monné 
221a6aedc5dSRoger Pau Monné 	/**
222a6aedc5dSRoger Pau Monné 	 * Sleepable mutex used to gate the execution of XenStore
223a6aedc5dSRoger Pau Monné 	 * watch event callbacks.
224a6aedc5dSRoger Pau Monné 	 *
225a6aedc5dSRoger Pau Monné 	 * xenwatch_thread holds an exclusive lock on this mutex
226a6aedc5dSRoger Pau Monné 	 * while delivering event callbacks, and xenstore_unregister_watch()
227a6aedc5dSRoger Pau Monné 	 * uses an exclusive lock of this mutex to guarantee that no
228a6aedc5dSRoger Pau Monné 	 * callbacks of the just unregistered watch are pending
229a6aedc5dSRoger Pau Monné 	 * before returning to its caller.
230a6aedc5dSRoger Pau Monné 	 */
231a6aedc5dSRoger Pau Monné 	struct sx xenwatch_mutex;
232a6aedc5dSRoger Pau Monné 
233a6aedc5dSRoger Pau Monné 	/**
234a6aedc5dSRoger Pau Monné 	 * The HVM guest pseudo-physical frame number.  This is Xen's mapping
235a6aedc5dSRoger Pau Monné 	 * of the true machine frame number into our "physical address space".
236a6aedc5dSRoger Pau Monné 	 */
237a6aedc5dSRoger Pau Monné 	unsigned long gpfn;
238a6aedc5dSRoger Pau Monné 
239a6aedc5dSRoger Pau Monné 	/**
240a6aedc5dSRoger Pau Monné 	 * The event channel for communicating with the
241a6aedc5dSRoger Pau Monné 	 * XenStore service.
242a6aedc5dSRoger Pau Monné 	 */
243a6aedc5dSRoger Pau Monné 	int evtchn;
244a6aedc5dSRoger Pau Monné 
245a6aedc5dSRoger Pau Monné 	/** Handle for XenStore interrupts. */
246a6aedc5dSRoger Pau Monné 	xen_intr_handle_t xen_intr_handle;
247a6aedc5dSRoger Pau Monné 
248a6aedc5dSRoger Pau Monné 	/**
249a6aedc5dSRoger Pau Monné 	 * Interrupt driven config hook allowing us to defer
250a6aedc5dSRoger Pau Monné 	 * attaching children until interrupts (and thus communication
251a6aedc5dSRoger Pau Monné 	 * with the XenStore service) are available.
252a6aedc5dSRoger Pau Monné 	 */
253a6aedc5dSRoger Pau Monné 	struct intr_config_hook xs_attachcb;
2541d84e2b3SRoger Pau Monné 
2551d84e2b3SRoger Pau Monné 	/**
2561d84e2b3SRoger Pau Monné 	 * Xenstore is a user-space process that usually runs in Dom0,
2571d84e2b3SRoger Pau Monné 	 * so if this domain is booting as Dom0, xenstore wont we accessible,
2581d84e2b3SRoger Pau Monné 	 * and we have to defer the initialization of xenstore related
2591d84e2b3SRoger Pau Monné 	 * devices to later (when xenstore is started).
2601d84e2b3SRoger Pau Monné 	 */
2611d84e2b3SRoger Pau Monné 	bool initialized;
2621d84e2b3SRoger Pau Monné 
2631d84e2b3SRoger Pau Monné 	/**
2641d84e2b3SRoger Pau Monné 	 * Task to run when xenstore is initialized (Dom0 only), will
2651d84e2b3SRoger Pau Monné 	 * take care of attaching xenstore related devices.
2661d84e2b3SRoger Pau Monné 	 */
2671d84e2b3SRoger Pau Monné 	struct task xs_late_init;
268a6aedc5dSRoger Pau Monné };
269a6aedc5dSRoger Pau Monné 
270a6aedc5dSRoger Pau Monné /*-------------------------------- Global Data ------------------------------*/
271a6aedc5dSRoger Pau Monné static struct xs_softc xs;
272a6aedc5dSRoger Pau Monné 
273a6aedc5dSRoger Pau Monné /*------------------------- Private Utility Functions -----------------------*/
274a6aedc5dSRoger Pau Monné 
275a6aedc5dSRoger Pau Monné /**
276a6aedc5dSRoger Pau Monné  * Count and optionally record pointers to a number of NUL terminated
277a6aedc5dSRoger Pau Monné  * strings in a buffer.
278a6aedc5dSRoger Pau Monné  *
279a6aedc5dSRoger Pau Monné  * \param strings  A pointer to a contiguous buffer of NUL terminated strings.
280a6aedc5dSRoger Pau Monné  * \param dest	   An array to store pointers to each string found in strings.
281a6aedc5dSRoger Pau Monné  * \param len	   The length of the buffer pointed to by strings.
282a6aedc5dSRoger Pau Monné  *
283a6aedc5dSRoger Pau Monné  * \return  A count of the number of strings found.
284a6aedc5dSRoger Pau Monné  */
285a6aedc5dSRoger Pau Monné static u_int
286a6aedc5dSRoger Pau Monné extract_strings(const char *strings, const char **dest, u_int len)
287a6aedc5dSRoger Pau Monné {
288a6aedc5dSRoger Pau Monné 	u_int num;
289a6aedc5dSRoger Pau Monné 	const char *p;
290a6aedc5dSRoger Pau Monné 
291a6aedc5dSRoger Pau Monné 	for (p = strings, num = 0; p < strings + len; p += strlen(p) + 1) {
292a6aedc5dSRoger Pau Monné 		if (dest != NULL)
293a6aedc5dSRoger Pau Monné 			*dest++ = p;
294a6aedc5dSRoger Pau Monné 		num++;
295a6aedc5dSRoger Pau Monné 	}
296a6aedc5dSRoger Pau Monné 
297a6aedc5dSRoger Pau Monné 	return (num);
298a6aedc5dSRoger Pau Monné }
299a6aedc5dSRoger Pau Monné 
300a6aedc5dSRoger Pau Monné /**
301a6aedc5dSRoger Pau Monné  * Convert a contiguous buffer containing a series of NUL terminated
302a6aedc5dSRoger Pau Monné  * strings into an array of pointers to strings.
303a6aedc5dSRoger Pau Monné  *
304a6aedc5dSRoger Pau Monné  * The returned pointer references the array of string pointers which
305a6aedc5dSRoger Pau Monné  * is followed by the storage for the string data.  It is the client's
306a6aedc5dSRoger Pau Monné  * responsibility to free this storage.
307a6aedc5dSRoger Pau Monné  *
308a6aedc5dSRoger Pau Monné  * The storage addressed by strings is free'd prior to split returning.
309a6aedc5dSRoger Pau Monné  *
310a6aedc5dSRoger Pau Monné  * \param strings  A pointer to a contiguous buffer of NUL terminated strings.
311a6aedc5dSRoger Pau Monné  * \param len	   The length of the buffer pointed to by strings.
312a6aedc5dSRoger Pau Monné  * \param num	   The number of strings found and returned in the strings
313a6aedc5dSRoger Pau Monné  *                 array.
314a6aedc5dSRoger Pau Monné  *
315a6aedc5dSRoger Pau Monné  * \return  An array of pointers to the strings found in the input buffer.
316a6aedc5dSRoger Pau Monné  */
317a6aedc5dSRoger Pau Monné static const char **
318a6aedc5dSRoger Pau Monné split(char *strings, u_int len, u_int *num)
319a6aedc5dSRoger Pau Monné {
320a6aedc5dSRoger Pau Monné 	const char **ret;
321a6aedc5dSRoger Pau Monné 
322a6aedc5dSRoger Pau Monné 	/* Protect against unterminated buffers. */
323a6aedc5dSRoger Pau Monné 	if (len > 0)
324a6aedc5dSRoger Pau Monné 		strings[len - 1] = '\0';
325a6aedc5dSRoger Pau Monné 
326a6aedc5dSRoger Pau Monné 	/* Count the strings. */
327a6aedc5dSRoger Pau Monné 	*num = extract_strings(strings, /*dest*/NULL, len);
328a6aedc5dSRoger Pau Monné 
329a6aedc5dSRoger Pau Monné 	/* Transfer to one big alloc for easy freeing by the caller. */
330a6aedc5dSRoger Pau Monné 	ret = malloc(*num * sizeof(char *) + len, M_XENSTORE, M_WAITOK);
331a6aedc5dSRoger Pau Monné 	memcpy(&ret[*num], strings, len);
332a6aedc5dSRoger Pau Monné 	free(strings, M_XENSTORE);
333a6aedc5dSRoger Pau Monné 
334a6aedc5dSRoger Pau Monné 	/* Extract pointers to newly allocated array. */
335a6aedc5dSRoger Pau Monné 	strings = (char *)&ret[*num];
336a6aedc5dSRoger Pau Monné 	(void)extract_strings(strings, /*dest*/ret, len);
337a6aedc5dSRoger Pau Monné 
338a6aedc5dSRoger Pau Monné 	return (ret);
339a6aedc5dSRoger Pau Monné }
340a6aedc5dSRoger Pau Monné 
341a6aedc5dSRoger Pau Monné /*------------------------- Public Utility Functions -------------------------*/
342a6aedc5dSRoger Pau Monné /*------- API comments for these methods can be found in xenstorevar.h -------*/
343a6aedc5dSRoger Pau Monné struct sbuf *
344a6aedc5dSRoger Pau Monné xs_join(const char *dir, const char *name)
345a6aedc5dSRoger Pau Monné {
346a6aedc5dSRoger Pau Monné 	struct sbuf *sb;
347a6aedc5dSRoger Pau Monné 
348a6aedc5dSRoger Pau Monné 	sb = sbuf_new_auto();
349a6aedc5dSRoger Pau Monné 	sbuf_cat(sb, dir);
350a6aedc5dSRoger Pau Monné 	if (name[0] != '\0') {
351a6aedc5dSRoger Pau Monné 		sbuf_putc(sb, '/');
352a6aedc5dSRoger Pau Monné 		sbuf_cat(sb, name);
353a6aedc5dSRoger Pau Monné 	}
354a6aedc5dSRoger Pau Monné 	sbuf_finish(sb);
355a6aedc5dSRoger Pau Monné 
356a6aedc5dSRoger Pau Monné 	return (sb);
357a6aedc5dSRoger Pau Monné }
358a6aedc5dSRoger Pau Monné 
359a6aedc5dSRoger Pau Monné /*-------------------- Low Level Communication Management --------------------*/
360a6aedc5dSRoger Pau Monné /**
361a6aedc5dSRoger Pau Monné  * Interrupt handler for the XenStore event channel.
362a6aedc5dSRoger Pau Monné  *
363a6aedc5dSRoger Pau Monné  * XenStore reads and writes block on "xen_store" for buffer
364a6aedc5dSRoger Pau Monné  * space.  Wakeup any blocking operations when the XenStore
365a6aedc5dSRoger Pau Monné  * service has modified the queues.
366a6aedc5dSRoger Pau Monné  */
367a6aedc5dSRoger Pau Monné static void
368a6aedc5dSRoger Pau Monné xs_intr(void * arg __unused /*__attribute__((unused))*/)
369a6aedc5dSRoger Pau Monné {
370a6aedc5dSRoger Pau Monné 
3711d84e2b3SRoger Pau Monné 	/* If xenstore has not been initialized, initialize it now */
3721d84e2b3SRoger Pau Monné 	if (!xs.initialized) {
3731d84e2b3SRoger Pau Monné 		xs.initialized = true;
3741d84e2b3SRoger Pau Monné 		/*
3751d84e2b3SRoger Pau Monné 		 * Since this task is probing and attaching devices we
3761d84e2b3SRoger Pau Monné 		 * have to hold the Giant lock.
3771d84e2b3SRoger Pau Monné 		 */
3781d84e2b3SRoger Pau Monné 		taskqueue_enqueue(taskqueue_swi_giant, &xs.xs_late_init);
3791d84e2b3SRoger Pau Monné 	}
3801d84e2b3SRoger Pau Monné 
381a6aedc5dSRoger Pau Monné 	/*
382a6aedc5dSRoger Pau Monné 	 * Hold ring lock across wakeup so that clients
383a6aedc5dSRoger Pau Monné 	 * cannot miss a wakeup.
384a6aedc5dSRoger Pau Monné 	 */
385a6aedc5dSRoger Pau Monné 	mtx_lock(&xs.ring_lock);
386a6aedc5dSRoger Pau Monné 	wakeup(xen_store);
387a6aedc5dSRoger Pau Monné 	mtx_unlock(&xs.ring_lock);
388a6aedc5dSRoger Pau Monné }
389a6aedc5dSRoger Pau Monné 
390a6aedc5dSRoger Pau Monné /**
391a6aedc5dSRoger Pau Monné  * Verify that the indexes for a ring are valid.
392a6aedc5dSRoger Pau Monné  *
393a6aedc5dSRoger Pau Monné  * The difference between the producer and consumer cannot
394a6aedc5dSRoger Pau Monné  * exceed the size of the ring.
395a6aedc5dSRoger Pau Monné  *
396a6aedc5dSRoger Pau Monné  * \param cons  The consumer index for the ring to test.
397a6aedc5dSRoger Pau Monné  * \param prod  The producer index for the ring to test.
398a6aedc5dSRoger Pau Monné  *
399a6aedc5dSRoger Pau Monné  * \retval 1  If indexes are in range.
400a6aedc5dSRoger Pau Monné  * \retval 0  If the indexes are out of range.
401a6aedc5dSRoger Pau Monné  */
402a6aedc5dSRoger Pau Monné static int
403a6aedc5dSRoger Pau Monné xs_check_indexes(XENSTORE_RING_IDX cons, XENSTORE_RING_IDX prod)
404a6aedc5dSRoger Pau Monné {
405a6aedc5dSRoger Pau Monné 
406a6aedc5dSRoger Pau Monné 	return ((prod - cons) <= XENSTORE_RING_SIZE);
407a6aedc5dSRoger Pau Monné }
408a6aedc5dSRoger Pau Monné 
409a6aedc5dSRoger Pau Monné /**
410a6aedc5dSRoger Pau Monné  * Return a pointer to, and the length of, the contiguous
411a6aedc5dSRoger Pau Monné  * free region available for output in a ring buffer.
412a6aedc5dSRoger Pau Monné  *
413a6aedc5dSRoger Pau Monné  * \param cons  The consumer index for the ring.
414a6aedc5dSRoger Pau Monné  * \param prod  The producer index for the ring.
415a6aedc5dSRoger Pau Monné  * \param buf   The base address of the ring's storage.
416a6aedc5dSRoger Pau Monné  * \param len   The amount of contiguous storage available.
417a6aedc5dSRoger Pau Monné  *
418a6aedc5dSRoger Pau Monné  * \return  A pointer to the start location of the free region.
419a6aedc5dSRoger Pau Monné  */
420a6aedc5dSRoger Pau Monné static void *
421a6aedc5dSRoger Pau Monné xs_get_output_chunk(XENSTORE_RING_IDX cons, XENSTORE_RING_IDX prod,
422a6aedc5dSRoger Pau Monné     char *buf, uint32_t *len)
423a6aedc5dSRoger Pau Monné {
424a6aedc5dSRoger Pau Monné 
425a6aedc5dSRoger Pau Monné 	*len = XENSTORE_RING_SIZE - MASK_XENSTORE_IDX(prod);
426a6aedc5dSRoger Pau Monné 	if ((XENSTORE_RING_SIZE - (prod - cons)) < *len)
427a6aedc5dSRoger Pau Monné 		*len = XENSTORE_RING_SIZE - (prod - cons);
428a6aedc5dSRoger Pau Monné 	return (buf + MASK_XENSTORE_IDX(prod));
429a6aedc5dSRoger Pau Monné }
430a6aedc5dSRoger Pau Monné 
431a6aedc5dSRoger Pau Monné /**
432a6aedc5dSRoger Pau Monné  * Return a pointer to, and the length of, the contiguous
433a6aedc5dSRoger Pau Monné  * data available to read from a ring buffer.
434a6aedc5dSRoger Pau Monné  *
435a6aedc5dSRoger Pau Monné  * \param cons  The consumer index for the ring.
436a6aedc5dSRoger Pau Monné  * \param prod  The producer index for the ring.
437a6aedc5dSRoger Pau Monné  * \param buf   The base address of the ring's storage.
438a6aedc5dSRoger Pau Monné  * \param len   The amount of contiguous data available to read.
439a6aedc5dSRoger Pau Monné  *
440a6aedc5dSRoger Pau Monné  * \return  A pointer to the start location of the available data.
441a6aedc5dSRoger Pau Monné  */
442a6aedc5dSRoger Pau Monné static const void *
443a6aedc5dSRoger Pau Monné xs_get_input_chunk(XENSTORE_RING_IDX cons, XENSTORE_RING_IDX prod,
444a6aedc5dSRoger Pau Monné     const char *buf, uint32_t *len)
445a6aedc5dSRoger Pau Monné {
446a6aedc5dSRoger Pau Monné 
447a6aedc5dSRoger Pau Monné 	*len = XENSTORE_RING_SIZE - MASK_XENSTORE_IDX(cons);
448a6aedc5dSRoger Pau Monné 	if ((prod - cons) < *len)
449a6aedc5dSRoger Pau Monné 		*len = prod - cons;
450a6aedc5dSRoger Pau Monné 	return (buf + MASK_XENSTORE_IDX(cons));
451a6aedc5dSRoger Pau Monné }
452a6aedc5dSRoger Pau Monné 
453a6aedc5dSRoger Pau Monné /**
454a6aedc5dSRoger Pau Monné  * Transmit data to the XenStore service.
455a6aedc5dSRoger Pau Monné  *
456a6aedc5dSRoger Pau Monné  * \param tdata  A pointer to the contiguous data to send.
457a6aedc5dSRoger Pau Monné  * \param len    The amount of data to send.
458a6aedc5dSRoger Pau Monné  *
459a6aedc5dSRoger Pau Monné  * \return  On success 0, otherwise an errno value indicating the
460a6aedc5dSRoger Pau Monné  *          cause of failure.
461a6aedc5dSRoger Pau Monné  *
462a6aedc5dSRoger Pau Monné  * \invariant  Called from thread context.
463a6aedc5dSRoger Pau Monné  * \invariant  The buffer pointed to by tdata is at least len bytes
464a6aedc5dSRoger Pau Monné  *             in length.
465a6aedc5dSRoger Pau Monné  * \invariant  xs.request_mutex exclusively locked.
466a6aedc5dSRoger Pau Monné  */
467a6aedc5dSRoger Pau Monné static int
468a6aedc5dSRoger Pau Monné xs_write_store(const void *tdata, unsigned len)
469a6aedc5dSRoger Pau Monné {
470a6aedc5dSRoger Pau Monné 	XENSTORE_RING_IDX cons, prod;
471a6aedc5dSRoger Pau Monné 	const char *data = (const char *)tdata;
472a6aedc5dSRoger Pau Monné 	int error;
473a6aedc5dSRoger Pau Monné 
474a6aedc5dSRoger Pau Monné 	sx_assert(&xs.request_mutex, SX_XLOCKED);
475a6aedc5dSRoger Pau Monné 	while (len != 0) {
476a6aedc5dSRoger Pau Monné 		void *dst;
477a6aedc5dSRoger Pau Monné 		u_int avail;
478a6aedc5dSRoger Pau Monné 
479a6aedc5dSRoger Pau Monné 		/* Hold lock so we can't miss wakeups should we block. */
480a6aedc5dSRoger Pau Monné 		mtx_lock(&xs.ring_lock);
481a6aedc5dSRoger Pau Monné 		cons = xen_store->req_cons;
482a6aedc5dSRoger Pau Monné 		prod = xen_store->req_prod;
483a6aedc5dSRoger Pau Monné 		if ((prod - cons) == XENSTORE_RING_SIZE) {
484a6aedc5dSRoger Pau Monné 			/*
485a6aedc5dSRoger Pau Monné 			 * Output ring is full. Wait for a ring event.
486a6aedc5dSRoger Pau Monné 			 *
487a6aedc5dSRoger Pau Monné 			 * Note that the events from both queues
488a6aedc5dSRoger Pau Monné 			 * are combined, so being woken does not
489a6aedc5dSRoger Pau Monné 			 * guarantee that data exist in the read
490a6aedc5dSRoger Pau Monné 			 * ring.
491a6aedc5dSRoger Pau Monné 			 *
492a6aedc5dSRoger Pau Monné 			 * To simplify error recovery and the retry,
493a6aedc5dSRoger Pau Monné 			 * we specify PDROP so our lock is *not* held
494a6aedc5dSRoger Pau Monné 			 * when msleep returns.
495a6aedc5dSRoger Pau Monné 			 */
496a6aedc5dSRoger Pau Monné 			error = msleep(xen_store, &xs.ring_lock, PCATCH|PDROP,
497a6aedc5dSRoger Pau Monné 			     "xbwrite", /*timeout*/0);
498a6aedc5dSRoger Pau Monné 			if (error && error != EWOULDBLOCK)
499a6aedc5dSRoger Pau Monné 				return (error);
500a6aedc5dSRoger Pau Monné 
501a6aedc5dSRoger Pau Monné 			/* Try again. */
502a6aedc5dSRoger Pau Monné 			continue;
503a6aedc5dSRoger Pau Monné 		}
504a6aedc5dSRoger Pau Monné 		mtx_unlock(&xs.ring_lock);
505a6aedc5dSRoger Pau Monné 
506a6aedc5dSRoger Pau Monné 		/* Verify queue sanity. */
507a6aedc5dSRoger Pau Monné 		if (!xs_check_indexes(cons, prod)) {
508a6aedc5dSRoger Pau Monné 			xen_store->req_cons = xen_store->req_prod = 0;
509a6aedc5dSRoger Pau Monné 			return (EIO);
510a6aedc5dSRoger Pau Monné 		}
511a6aedc5dSRoger Pau Monné 
512a6aedc5dSRoger Pau Monné 		dst = xs_get_output_chunk(cons, prod, xen_store->req, &avail);
513a6aedc5dSRoger Pau Monné 		if (avail > len)
514a6aedc5dSRoger Pau Monné 			avail = len;
515a6aedc5dSRoger Pau Monné 
516a6aedc5dSRoger Pau Monné 		memcpy(dst, data, avail);
517a6aedc5dSRoger Pau Monné 		data += avail;
518a6aedc5dSRoger Pau Monné 		len -= avail;
519a6aedc5dSRoger Pau Monné 
520a6aedc5dSRoger Pau Monné 		/*
521a6aedc5dSRoger Pau Monné 		 * The store to the producer index, which indicates
522a6aedc5dSRoger Pau Monné 		 * to the other side that new data has arrived, must
523a6aedc5dSRoger Pau Monné 		 * be visible only after our copy of the data into the
524a6aedc5dSRoger Pau Monné 		 * ring has completed.
525a6aedc5dSRoger Pau Monné 		 */
526a6aedc5dSRoger Pau Monné 		wmb();
527a6aedc5dSRoger Pau Monné 		xen_store->req_prod += avail;
528a6aedc5dSRoger Pau Monné 
529a6aedc5dSRoger Pau Monné 		/*
530a6aedc5dSRoger Pau Monné 		 * xen_intr_signal() implies mb(). The other side will see
531a6aedc5dSRoger Pau Monné 		 * the change to req_prod at the time of the interrupt.
532a6aedc5dSRoger Pau Monné 		 */
533a6aedc5dSRoger Pau Monné 		xen_intr_signal(xs.xen_intr_handle);
534a6aedc5dSRoger Pau Monné 	}
535a6aedc5dSRoger Pau Monné 
536a6aedc5dSRoger Pau Monné 	return (0);
537a6aedc5dSRoger Pau Monné }
538a6aedc5dSRoger Pau Monné 
539a6aedc5dSRoger Pau Monné /**
540a6aedc5dSRoger Pau Monné  * Receive data from the XenStore service.
541a6aedc5dSRoger Pau Monné  *
542a6aedc5dSRoger Pau Monné  * \param tdata  A pointer to the contiguous buffer to receive the data.
543a6aedc5dSRoger Pau Monné  * \param len    The amount of data to receive.
544a6aedc5dSRoger Pau Monné  *
545a6aedc5dSRoger Pau Monné  * \return  On success 0, otherwise an errno value indicating the
546a6aedc5dSRoger Pau Monné  *          cause of failure.
547a6aedc5dSRoger Pau Monné  *
548a6aedc5dSRoger Pau Monné  * \invariant  Called from thread context.
549a6aedc5dSRoger Pau Monné  * \invariant  The buffer pointed to by tdata is at least len bytes
550a6aedc5dSRoger Pau Monné  *             in length.
551a6aedc5dSRoger Pau Monné  *
552a6aedc5dSRoger Pau Monné  * \note xs_read does not perform any internal locking to guarantee
553a6aedc5dSRoger Pau Monné  *       serial access to the incoming ring buffer.  However, there
554a6aedc5dSRoger Pau Monné  *	 is only one context processing reads: xs_rcv_thread().
555a6aedc5dSRoger Pau Monné  */
556a6aedc5dSRoger Pau Monné static int
557a6aedc5dSRoger Pau Monné xs_read_store(void *tdata, unsigned len)
558a6aedc5dSRoger Pau Monné {
559a6aedc5dSRoger Pau Monné 	XENSTORE_RING_IDX cons, prod;
560a6aedc5dSRoger Pau Monné 	char *data = (char *)tdata;
561a6aedc5dSRoger Pau Monné 	int error;
562a6aedc5dSRoger Pau Monné 
563a6aedc5dSRoger Pau Monné 	while (len != 0) {
564a6aedc5dSRoger Pau Monné 		u_int avail;
565a6aedc5dSRoger Pau Monné 		const char *src;
566a6aedc5dSRoger Pau Monné 
567a6aedc5dSRoger Pau Monné 		/* Hold lock so we can't miss wakeups should we block. */
568a6aedc5dSRoger Pau Monné 		mtx_lock(&xs.ring_lock);
569a6aedc5dSRoger Pau Monné 		cons = xen_store->rsp_cons;
570a6aedc5dSRoger Pau Monné 		prod = xen_store->rsp_prod;
571a6aedc5dSRoger Pau Monné 		if (cons == prod) {
572a6aedc5dSRoger Pau Monné 			/*
573a6aedc5dSRoger Pau Monné 			 * Nothing to read. Wait for a ring event.
574a6aedc5dSRoger Pau Monné 			 *
575a6aedc5dSRoger Pau Monné 			 * Note that the events from both queues
576a6aedc5dSRoger Pau Monné 			 * are combined, so being woken does not
577a6aedc5dSRoger Pau Monné 			 * guarantee that data exist in the read
578a6aedc5dSRoger Pau Monné 			 * ring.
579a6aedc5dSRoger Pau Monné 			 *
580a6aedc5dSRoger Pau Monné 			 * To simplify error recovery and the retry,
581a6aedc5dSRoger Pau Monné 			 * we specify PDROP so our lock is *not* held
582a6aedc5dSRoger Pau Monné 			 * when msleep returns.
583a6aedc5dSRoger Pau Monné 			 */
584a6aedc5dSRoger Pau Monné 			error = msleep(xen_store, &xs.ring_lock, PCATCH|PDROP,
585a6aedc5dSRoger Pau Monné 			    "xbread", /*timeout*/0);
586a6aedc5dSRoger Pau Monné 			if (error && error != EWOULDBLOCK)
587a6aedc5dSRoger Pau Monné 				return (error);
588a6aedc5dSRoger Pau Monné 			continue;
589a6aedc5dSRoger Pau Monné 		}
590a6aedc5dSRoger Pau Monné 		mtx_unlock(&xs.ring_lock);
591a6aedc5dSRoger Pau Monné 
592a6aedc5dSRoger Pau Monné 		/* Verify queue sanity. */
593a6aedc5dSRoger Pau Monné 		if (!xs_check_indexes(cons, prod)) {
594a6aedc5dSRoger Pau Monné 			xen_store->rsp_cons = xen_store->rsp_prod = 0;
595a6aedc5dSRoger Pau Monné 			return (EIO);
596a6aedc5dSRoger Pau Monné 		}
597a6aedc5dSRoger Pau Monné 
598a6aedc5dSRoger Pau Monné 		src = xs_get_input_chunk(cons, prod, xen_store->rsp, &avail);
599a6aedc5dSRoger Pau Monné 		if (avail > len)
600a6aedc5dSRoger Pau Monné 			avail = len;
601a6aedc5dSRoger Pau Monné 
602a6aedc5dSRoger Pau Monné 		/*
603a6aedc5dSRoger Pau Monné 		 * Insure the data we read is related to the indexes
604a6aedc5dSRoger Pau Monné 		 * we read above.
605a6aedc5dSRoger Pau Monné 		 */
606a6aedc5dSRoger Pau Monné 		rmb();
607a6aedc5dSRoger Pau Monné 
608a6aedc5dSRoger Pau Monné 		memcpy(data, src, avail);
609a6aedc5dSRoger Pau Monné 		data += avail;
610a6aedc5dSRoger Pau Monné 		len -= avail;
611a6aedc5dSRoger Pau Monné 
612a6aedc5dSRoger Pau Monné 		/*
613a6aedc5dSRoger Pau Monné 		 * Insure that the producer of this ring does not see
614a6aedc5dSRoger Pau Monné 		 * the ring space as free until after we have copied it
615a6aedc5dSRoger Pau Monné 		 * out.
616a6aedc5dSRoger Pau Monné 		 */
617a6aedc5dSRoger Pau Monné 		mb();
618a6aedc5dSRoger Pau Monné 		xen_store->rsp_cons += avail;
619a6aedc5dSRoger Pau Monné 
620a6aedc5dSRoger Pau Monné 		/*
621a6aedc5dSRoger Pau Monné 		 * xen_intr_signal() implies mb(). The producer will see
622a6aedc5dSRoger Pau Monné 		 * the updated consumer index when the event is delivered.
623a6aedc5dSRoger Pau Monné 		 */
624a6aedc5dSRoger Pau Monné 		xen_intr_signal(xs.xen_intr_handle);
625a6aedc5dSRoger Pau Monné 	}
626a6aedc5dSRoger Pau Monné 
627a6aedc5dSRoger Pau Monné 	return (0);
628a6aedc5dSRoger Pau Monné }
629a6aedc5dSRoger Pau Monné 
630a6aedc5dSRoger Pau Monné /*----------------------- Received Message Processing ------------------------*/
631a6aedc5dSRoger Pau Monné /**
632a6aedc5dSRoger Pau Monné  * Block reading the next message from the XenStore service and
633a6aedc5dSRoger Pau Monné  * process the result.
634a6aedc5dSRoger Pau Monné  *
635a6aedc5dSRoger Pau Monné  * \param type  The returned type of the XenStore message received.
636a6aedc5dSRoger Pau Monné  *
637a6aedc5dSRoger Pau Monné  * \return  0 on success.  Otherwise an errno value indicating the
638a6aedc5dSRoger Pau Monné  *          type of failure encountered.
639a6aedc5dSRoger Pau Monné  */
640a6aedc5dSRoger Pau Monné static int
641a6aedc5dSRoger Pau Monné xs_process_msg(enum xsd_sockmsg_type *type)
642a6aedc5dSRoger Pau Monné {
643a6aedc5dSRoger Pau Monné 	struct xs_stored_msg *msg;
644a6aedc5dSRoger Pau Monné 	char *body;
645a6aedc5dSRoger Pau Monné 	int error;
646a6aedc5dSRoger Pau Monné 
647a6aedc5dSRoger Pau Monné 	msg = malloc(sizeof(*msg), M_XENSTORE, M_WAITOK);
648a6aedc5dSRoger Pau Monné 	error = xs_read_store(&msg->hdr, sizeof(msg->hdr));
649a6aedc5dSRoger Pau Monné 	if (error) {
650a6aedc5dSRoger Pau Monné 		free(msg, M_XENSTORE);
651a6aedc5dSRoger Pau Monné 		return (error);
652a6aedc5dSRoger Pau Monné 	}
653a6aedc5dSRoger Pau Monné 
654a6aedc5dSRoger Pau Monné 	body = malloc(msg->hdr.len + 1, M_XENSTORE, M_WAITOK);
655a6aedc5dSRoger Pau Monné 	error = xs_read_store(body, msg->hdr.len);
656a6aedc5dSRoger Pau Monné 	if (error) {
657a6aedc5dSRoger Pau Monné 		free(body, M_XENSTORE);
658a6aedc5dSRoger Pau Monné 		free(msg, M_XENSTORE);
659a6aedc5dSRoger Pau Monné 		return (error);
660a6aedc5dSRoger Pau Monné 	}
661a6aedc5dSRoger Pau Monné 	body[msg->hdr.len] = '\0';
662a6aedc5dSRoger Pau Monné 
663a6aedc5dSRoger Pau Monné 	*type = msg->hdr.type;
664a6aedc5dSRoger Pau Monné 	if (msg->hdr.type == XS_WATCH_EVENT) {
665a6aedc5dSRoger Pau Monné 		msg->u.watch.vec = split(body, msg->hdr.len,
666a6aedc5dSRoger Pau Monné 		    &msg->u.watch.vec_size);
667a6aedc5dSRoger Pau Monné 
668a6aedc5dSRoger Pau Monné 		mtx_lock(&xs.registered_watches_lock);
669a6aedc5dSRoger Pau Monné 		msg->u.watch.handle = find_watch(
670a6aedc5dSRoger Pau Monné 		    msg->u.watch.vec[XS_WATCH_TOKEN]);
671a6aedc5dSRoger Pau Monné 		if (msg->u.watch.handle != NULL) {
672a6aedc5dSRoger Pau Monné 			mtx_lock(&xs.watch_events_lock);
673a6aedc5dSRoger Pau Monné 			TAILQ_INSERT_TAIL(&xs.watch_events, msg, list);
674a6aedc5dSRoger Pau Monné 			wakeup(&xs.watch_events);
675a6aedc5dSRoger Pau Monné 			mtx_unlock(&xs.watch_events_lock);
676a6aedc5dSRoger Pau Monné 		} else {
677a6aedc5dSRoger Pau Monné 			free(msg->u.watch.vec, M_XENSTORE);
678a6aedc5dSRoger Pau Monné 			free(msg, M_XENSTORE);
679a6aedc5dSRoger Pau Monné 		}
680a6aedc5dSRoger Pau Monné 		mtx_unlock(&xs.registered_watches_lock);
681a6aedc5dSRoger Pau Monné 	} else {
682a6aedc5dSRoger Pau Monné 		msg->u.reply.body = body;
683a6aedc5dSRoger Pau Monné 		mtx_lock(&xs.reply_lock);
684a6aedc5dSRoger Pau Monné 		TAILQ_INSERT_TAIL(&xs.reply_list, msg, list);
685a6aedc5dSRoger Pau Monné 		wakeup(&xs.reply_list);
686a6aedc5dSRoger Pau Monné 		mtx_unlock(&xs.reply_lock);
687a6aedc5dSRoger Pau Monné 	}
688a6aedc5dSRoger Pau Monné 
689a6aedc5dSRoger Pau Monné 	return (0);
690a6aedc5dSRoger Pau Monné }
691a6aedc5dSRoger Pau Monné 
692a6aedc5dSRoger Pau Monné /**
693a6aedc5dSRoger Pau Monné  * Thread body of the XenStore receive thread.
694a6aedc5dSRoger Pau Monné  *
695a6aedc5dSRoger Pau Monné  * This thread blocks waiting for data from the XenStore service
696a6aedc5dSRoger Pau Monné  * and processes and received messages.
697a6aedc5dSRoger Pau Monné  */
698a6aedc5dSRoger Pau Monné static void
699a6aedc5dSRoger Pau Monné xs_rcv_thread(void *arg __unused)
700a6aedc5dSRoger Pau Monné {
701a6aedc5dSRoger Pau Monné 	int error;
702a6aedc5dSRoger Pau Monné 	enum xsd_sockmsg_type type;
703a6aedc5dSRoger Pau Monné 
704a6aedc5dSRoger Pau Monné 	for (;;) {
705a6aedc5dSRoger Pau Monné 		error = xs_process_msg(&type);
706a6aedc5dSRoger Pau Monné 		if (error)
707a6aedc5dSRoger Pau Monné 			printf("XENSTORE error %d while reading message\n",
708a6aedc5dSRoger Pau Monné 			    error);
709a6aedc5dSRoger Pau Monné 	}
710a6aedc5dSRoger Pau Monné }
711a6aedc5dSRoger Pau Monné 
712a6aedc5dSRoger Pau Monné /*---------------- XenStore Message Request/Reply Processing -----------------*/
713a6aedc5dSRoger Pau Monné /**
714a6aedc5dSRoger Pau Monné  * Filter invoked before transmitting any message to the XenStore service.
715a6aedc5dSRoger Pau Monné  *
716a6aedc5dSRoger Pau Monné  * The role of the filter may expand, but currently serves to manage
717a6aedc5dSRoger Pau Monné  * the interactions of messages with transaction state.
718a6aedc5dSRoger Pau Monné  *
719a6aedc5dSRoger Pau Monné  * \param request_msg_type  The message type for the request.
720a6aedc5dSRoger Pau Monné  */
721a6aedc5dSRoger Pau Monné static inline void
722a6aedc5dSRoger Pau Monné xs_request_filter(uint32_t request_msg_type)
723a6aedc5dSRoger Pau Monné {
724a6aedc5dSRoger Pau Monné 	if (request_msg_type == XS_TRANSACTION_START)
725a6aedc5dSRoger Pau Monné 		sx_slock(&xs.suspend_mutex);
726a6aedc5dSRoger Pau Monné }
727a6aedc5dSRoger Pau Monné 
728a6aedc5dSRoger Pau Monné /**
729a6aedc5dSRoger Pau Monné  * Filter invoked after transmitting any message to the XenStore service.
730a6aedc5dSRoger Pau Monné  *
731a6aedc5dSRoger Pau Monné  * The role of the filter may expand, but currently serves to manage
732a6aedc5dSRoger Pau Monné  * the interactions of messages with transaction state.
733a6aedc5dSRoger Pau Monné  *
734a6aedc5dSRoger Pau Monné  * \param request_msg_type     The message type for the original request.
735a6aedc5dSRoger Pau Monné  * \param reply_msg_type       The message type for any received reply.
736a6aedc5dSRoger Pau Monné  * \param request_reply_error  The error status from the attempt to send
737a6aedc5dSRoger Pau Monné  *                             the request or retrieve the reply.
738a6aedc5dSRoger Pau Monné  */
739a6aedc5dSRoger Pau Monné static inline void
740a6aedc5dSRoger Pau Monné xs_reply_filter(uint32_t request_msg_type,
741a6aedc5dSRoger Pau Monné     uint32_t reply_msg_type, int request_reply_error)
742a6aedc5dSRoger Pau Monné {
743a6aedc5dSRoger Pau Monné 	/*
744a6aedc5dSRoger Pau Monné 	 * The count of transactions drops if we attempted
745a6aedc5dSRoger Pau Monné 	 * to end a transaction (even if that attempt fails
746a6aedc5dSRoger Pau Monné 	 * in error), we receive a transaction end acknowledgement,
747a6aedc5dSRoger Pau Monné 	 * or if our attempt to begin a transaction fails.
748a6aedc5dSRoger Pau Monné 	 */
749a6aedc5dSRoger Pau Monné 	if (request_msg_type == XS_TRANSACTION_END
750a6aedc5dSRoger Pau Monné 	 || (request_reply_error == 0 && reply_msg_type == XS_TRANSACTION_END)
751a6aedc5dSRoger Pau Monné 	 || (request_msg_type == XS_TRANSACTION_START
752a6aedc5dSRoger Pau Monné 	  && (request_reply_error != 0 || reply_msg_type == XS_ERROR)))
753a6aedc5dSRoger Pau Monné 		sx_sunlock(&xs.suspend_mutex);
754a6aedc5dSRoger Pau Monné 
755a6aedc5dSRoger Pau Monné }
756a6aedc5dSRoger Pau Monné 
757a6aedc5dSRoger Pau Monné #define xsd_error_count	(sizeof(xsd_errors) / sizeof(xsd_errors[0]))
758a6aedc5dSRoger Pau Monné 
759a6aedc5dSRoger Pau Monné /**
760a6aedc5dSRoger Pau Monné  * Convert a XenStore error string into an errno number.
761a6aedc5dSRoger Pau Monné  *
762a6aedc5dSRoger Pau Monné  * \param errorstring  The error string to convert.
763a6aedc5dSRoger Pau Monné  *
764a6aedc5dSRoger Pau Monné  * \return  The errno best matching the input string.
765a6aedc5dSRoger Pau Monné  *
766a6aedc5dSRoger Pau Monné  * \note Unknown error strings are converted to EINVAL.
767a6aedc5dSRoger Pau Monné  */
768a6aedc5dSRoger Pau Monné static int
769a6aedc5dSRoger Pau Monné xs_get_error(const char *errorstring)
770a6aedc5dSRoger Pau Monné {
771a6aedc5dSRoger Pau Monné 	u_int i;
772a6aedc5dSRoger Pau Monné 
773a6aedc5dSRoger Pau Monné 	for (i = 0; i < xsd_error_count; i++) {
774a6aedc5dSRoger Pau Monné 		if (!strcmp(errorstring, xsd_errors[i].errstring))
775a6aedc5dSRoger Pau Monné 			return (xsd_errors[i].errnum);
776a6aedc5dSRoger Pau Monné 	}
777a6aedc5dSRoger Pau Monné 	log(LOG_WARNING, "XENSTORE xen store gave: unknown error %s",
778a6aedc5dSRoger Pau Monné 	    errorstring);
779a6aedc5dSRoger Pau Monné 	return (EINVAL);
780a6aedc5dSRoger Pau Monné }
781a6aedc5dSRoger Pau Monné 
782a6aedc5dSRoger Pau Monné /**
783a6aedc5dSRoger Pau Monné  * Block waiting for a reply to a message request.
784a6aedc5dSRoger Pau Monné  *
785a6aedc5dSRoger Pau Monné  * \param type	  The returned type of the reply.
786a6aedc5dSRoger Pau Monné  * \param len	  The returned body length of the reply.
787a6aedc5dSRoger Pau Monné  * \param result  The returned body of the reply.
788a6aedc5dSRoger Pau Monné  *
789a6aedc5dSRoger Pau Monné  * \return  0 on success.  Otherwise an errno indicating the
790a6aedc5dSRoger Pau Monné  *          cause of failure.
791a6aedc5dSRoger Pau Monné  */
792a6aedc5dSRoger Pau Monné static int
793a6aedc5dSRoger Pau Monné xs_read_reply(enum xsd_sockmsg_type *type, u_int *len, void **result)
794a6aedc5dSRoger Pau Monné {
795a6aedc5dSRoger Pau Monné 	struct xs_stored_msg *msg;
796a6aedc5dSRoger Pau Monné 	char *body;
797a6aedc5dSRoger Pau Monné 	int error;
798a6aedc5dSRoger Pau Monné 
799a6aedc5dSRoger Pau Monné 	mtx_lock(&xs.reply_lock);
800a6aedc5dSRoger Pau Monné 	while (TAILQ_EMPTY(&xs.reply_list)) {
801a6aedc5dSRoger Pau Monné 		error = mtx_sleep(&xs.reply_list, &xs.reply_lock,
802a6aedc5dSRoger Pau Monné 		    PCATCH, "xswait", hz/10);
803a6aedc5dSRoger Pau Monné 		if (error && error != EWOULDBLOCK) {
804a6aedc5dSRoger Pau Monné 			mtx_unlock(&xs.reply_lock);
805a6aedc5dSRoger Pau Monné 			return (error);
806a6aedc5dSRoger Pau Monné 		}
807a6aedc5dSRoger Pau Monné 	}
808a6aedc5dSRoger Pau Monné 	msg = TAILQ_FIRST(&xs.reply_list);
809a6aedc5dSRoger Pau Monné 	TAILQ_REMOVE(&xs.reply_list, msg, list);
810a6aedc5dSRoger Pau Monné 	mtx_unlock(&xs.reply_lock);
811a6aedc5dSRoger Pau Monné 
812a6aedc5dSRoger Pau Monné 	*type = msg->hdr.type;
813a6aedc5dSRoger Pau Monné 	if (len)
814a6aedc5dSRoger Pau Monné 		*len = msg->hdr.len;
815a6aedc5dSRoger Pau Monné 	body = msg->u.reply.body;
816a6aedc5dSRoger Pau Monné 
817a6aedc5dSRoger Pau Monné 	free(msg, M_XENSTORE);
818a6aedc5dSRoger Pau Monné 	*result = body;
819a6aedc5dSRoger Pau Monné 	return (0);
820a6aedc5dSRoger Pau Monné }
821a6aedc5dSRoger Pau Monné 
822a6aedc5dSRoger Pau Monné /**
823a6aedc5dSRoger Pau Monné  * Pass-thru interface for XenStore access by userland processes
824a6aedc5dSRoger Pau Monné  * via the XenStore device.
825a6aedc5dSRoger Pau Monné  *
826a6aedc5dSRoger Pau Monné  * Reply type and length data are returned by overwriting these
827a6aedc5dSRoger Pau Monné  * fields in the passed in request message.
828a6aedc5dSRoger Pau Monné  *
829a6aedc5dSRoger Pau Monné  * \param msg	  A properly formatted message to transmit to
830a6aedc5dSRoger Pau Monné  *		  the XenStore service.
831a6aedc5dSRoger Pau Monné  * \param result  The returned body of the reply.
832a6aedc5dSRoger Pau Monné  *
833a6aedc5dSRoger Pau Monné  * \return  0 on success.  Otherwise an errno indicating the cause
834a6aedc5dSRoger Pau Monné  *          of failure.
835a6aedc5dSRoger Pau Monné  *
836a6aedc5dSRoger Pau Monné  * \note The returned result is provided in malloced storage and thus
837a6aedc5dSRoger Pau Monné  *       must be free'd by the caller with 'free(result, M_XENSTORE);
838a6aedc5dSRoger Pau Monné  */
839a6aedc5dSRoger Pau Monné int
840a6aedc5dSRoger Pau Monné xs_dev_request_and_reply(struct xsd_sockmsg *msg, void **result)
841a6aedc5dSRoger Pau Monné {
842a6aedc5dSRoger Pau Monné 	uint32_t request_type;
843a6aedc5dSRoger Pau Monné 	int error;
844a6aedc5dSRoger Pau Monné 
845a6aedc5dSRoger Pau Monné 	request_type = msg->type;
846a6aedc5dSRoger Pau Monné 	xs_request_filter(request_type);
847a6aedc5dSRoger Pau Monné 
848a6aedc5dSRoger Pau Monné 	sx_xlock(&xs.request_mutex);
849a6aedc5dSRoger Pau Monné 	if ((error = xs_write_store(msg, sizeof(*msg) + msg->len)) == 0)
850a6aedc5dSRoger Pau Monné 		error = xs_read_reply(&msg->type, &msg->len, result);
851a6aedc5dSRoger Pau Monné 	sx_xunlock(&xs.request_mutex);
852a6aedc5dSRoger Pau Monné 
853a6aedc5dSRoger Pau Monné 	xs_reply_filter(request_type, msg->type, error);
854a6aedc5dSRoger Pau Monné 
855a6aedc5dSRoger Pau Monné 	return (error);
856a6aedc5dSRoger Pau Monné }
857a6aedc5dSRoger Pau Monné 
858a6aedc5dSRoger Pau Monné /**
859a6aedc5dSRoger Pau Monné  * Send a message with an optionally muti-part body to the XenStore service.
860a6aedc5dSRoger Pau Monné  *
861a6aedc5dSRoger Pau Monné  * \param t              The transaction to use for this request.
862a6aedc5dSRoger Pau Monné  * \param request_type   The type of message to send.
863a6aedc5dSRoger Pau Monné  * \param iovec          Pointers to the body sections of the request.
864a6aedc5dSRoger Pau Monné  * \param num_vecs       The number of body sections in the request.
865a6aedc5dSRoger Pau Monné  * \param len            The returned length of the reply.
866a6aedc5dSRoger Pau Monné  * \param result         The returned body of the reply.
867a6aedc5dSRoger Pau Monné  *
868a6aedc5dSRoger Pau Monné  * \return  0 on success.  Otherwise an errno indicating
869a6aedc5dSRoger Pau Monné  *          the cause of failure.
870a6aedc5dSRoger Pau Monné  *
871a6aedc5dSRoger Pau Monné  * \note The returned result is provided in malloced storage and thus
872a6aedc5dSRoger Pau Monné  *       must be free'd by the caller with 'free(*result, M_XENSTORE);
873a6aedc5dSRoger Pau Monné  */
874a6aedc5dSRoger Pau Monné static int
875a6aedc5dSRoger Pau Monné xs_talkv(struct xs_transaction t, enum xsd_sockmsg_type request_type,
876a6aedc5dSRoger Pau Monné     const struct iovec *iovec, u_int num_vecs, u_int *len, void **result)
877a6aedc5dSRoger Pau Monné {
878a6aedc5dSRoger Pau Monné 	struct xsd_sockmsg msg;
879a6aedc5dSRoger Pau Monné 	void *ret = NULL;
880a6aedc5dSRoger Pau Monné 	u_int i;
881a6aedc5dSRoger Pau Monné 	int error;
882a6aedc5dSRoger Pau Monné 
883a6aedc5dSRoger Pau Monné 	msg.tx_id = t.id;
884a6aedc5dSRoger Pau Monné 	msg.req_id = 0;
885a6aedc5dSRoger Pau Monné 	msg.type = request_type;
886a6aedc5dSRoger Pau Monné 	msg.len = 0;
887a6aedc5dSRoger Pau Monné 	for (i = 0; i < num_vecs; i++)
888a6aedc5dSRoger Pau Monné 		msg.len += iovec[i].iov_len;
889a6aedc5dSRoger Pau Monné 
890a6aedc5dSRoger Pau Monné 	xs_request_filter(request_type);
891a6aedc5dSRoger Pau Monné 
892a6aedc5dSRoger Pau Monné 	sx_xlock(&xs.request_mutex);
893a6aedc5dSRoger Pau Monné 	error = xs_write_store(&msg, sizeof(msg));
894a6aedc5dSRoger Pau Monné 	if (error) {
895a6aedc5dSRoger Pau Monné 		printf("xs_talkv failed %d\n", error);
896a6aedc5dSRoger Pau Monné 		goto error_lock_held;
897a6aedc5dSRoger Pau Monné 	}
898a6aedc5dSRoger Pau Monné 
899a6aedc5dSRoger Pau Monné 	for (i = 0; i < num_vecs; i++) {
900a6aedc5dSRoger Pau Monné 		error = xs_write_store(iovec[i].iov_base, iovec[i].iov_len);
901a6aedc5dSRoger Pau Monné 		if (error) {
902a6aedc5dSRoger Pau Monné 			printf("xs_talkv failed %d\n", error);
903a6aedc5dSRoger Pau Monné 			goto error_lock_held;
904a6aedc5dSRoger Pau Monné 		}
905a6aedc5dSRoger Pau Monné 	}
906a6aedc5dSRoger Pau Monné 
907a6aedc5dSRoger Pau Monné 	error = xs_read_reply(&msg.type, len, &ret);
908a6aedc5dSRoger Pau Monné 
909a6aedc5dSRoger Pau Monné error_lock_held:
910a6aedc5dSRoger Pau Monné 	sx_xunlock(&xs.request_mutex);
911a6aedc5dSRoger Pau Monné 	xs_reply_filter(request_type, msg.type, error);
912a6aedc5dSRoger Pau Monné 	if (error)
913a6aedc5dSRoger Pau Monné 		return (error);
914a6aedc5dSRoger Pau Monné 
915a6aedc5dSRoger Pau Monné 	if (msg.type == XS_ERROR) {
916a6aedc5dSRoger Pau Monné 		error = xs_get_error(ret);
917a6aedc5dSRoger Pau Monné 		free(ret, M_XENSTORE);
918a6aedc5dSRoger Pau Monné 		return (error);
919a6aedc5dSRoger Pau Monné 	}
920a6aedc5dSRoger Pau Monné 
921a6aedc5dSRoger Pau Monné 	/* Reply is either error or an echo of our request message type. */
922a6aedc5dSRoger Pau Monné 	KASSERT(msg.type == request_type, ("bad xenstore message type"));
923a6aedc5dSRoger Pau Monné 
924a6aedc5dSRoger Pau Monné 	if (result)
925a6aedc5dSRoger Pau Monné 		*result = ret;
926a6aedc5dSRoger Pau Monné 	else
927a6aedc5dSRoger Pau Monné 		free(ret, M_XENSTORE);
928a6aedc5dSRoger Pau Monné 
929a6aedc5dSRoger Pau Monné 	return (0);
930a6aedc5dSRoger Pau Monné }
931a6aedc5dSRoger Pau Monné 
932a6aedc5dSRoger Pau Monné /**
933a6aedc5dSRoger Pau Monné  * Wrapper for xs_talkv allowing easy transmission of a message with
934a6aedc5dSRoger Pau Monné  * a single, contiguous, message body.
935a6aedc5dSRoger Pau Monné  *
936a6aedc5dSRoger Pau Monné  * \param t              The transaction to use for this request.
937a6aedc5dSRoger Pau Monné  * \param request_type   The type of message to send.
938a6aedc5dSRoger Pau Monné  * \param body           The body of the request.
939a6aedc5dSRoger Pau Monné  * \param len            The returned length of the reply.
940a6aedc5dSRoger Pau Monné  * \param result         The returned body of the reply.
941a6aedc5dSRoger Pau Monné  *
942a6aedc5dSRoger Pau Monné  * \return  0 on success.  Otherwise an errno indicating
943a6aedc5dSRoger Pau Monné  *          the cause of failure.
944a6aedc5dSRoger Pau Monné  *
945a6aedc5dSRoger Pau Monné  * \note The returned result is provided in malloced storage and thus
946a6aedc5dSRoger Pau Monné  *       must be free'd by the caller with 'free(*result, M_XENSTORE);
947a6aedc5dSRoger Pau Monné  */
948a6aedc5dSRoger Pau Monné static int
949a6aedc5dSRoger Pau Monné xs_single(struct xs_transaction t, enum xsd_sockmsg_type request_type,
950a6aedc5dSRoger Pau Monné     const char *body, u_int *len, void **result)
951a6aedc5dSRoger Pau Monné {
952a6aedc5dSRoger Pau Monné 	struct iovec iovec;
953a6aedc5dSRoger Pau Monné 
954a6aedc5dSRoger Pau Monné 	iovec.iov_base = (void *)(uintptr_t)body;
955a6aedc5dSRoger Pau Monné 	iovec.iov_len = strlen(body) + 1;
956a6aedc5dSRoger Pau Monné 
957a6aedc5dSRoger Pau Monné 	return (xs_talkv(t, request_type, &iovec, 1, len, result));
958a6aedc5dSRoger Pau Monné }
959a6aedc5dSRoger Pau Monné 
960a6aedc5dSRoger Pau Monné /*------------------------- XenStore Watch Support ---------------------------*/
961a6aedc5dSRoger Pau Monné /**
962a6aedc5dSRoger Pau Monné  * Transmit a watch request to the XenStore service.
963a6aedc5dSRoger Pau Monné  *
964a6aedc5dSRoger Pau Monné  * \param path    The path in the XenStore to watch.
965a6aedc5dSRoger Pau Monné  * \param tocken  A unique identifier for this watch.
966a6aedc5dSRoger Pau Monné  *
967a6aedc5dSRoger Pau Monné  * \return  0 on success.  Otherwise an errno indicating the
968a6aedc5dSRoger Pau Monné  *          cause of failure.
969a6aedc5dSRoger Pau Monné  */
970a6aedc5dSRoger Pau Monné static int
971a6aedc5dSRoger Pau Monné xs_watch(const char *path, const char *token)
972a6aedc5dSRoger Pau Monné {
973a6aedc5dSRoger Pau Monné 	struct iovec iov[2];
974a6aedc5dSRoger Pau Monné 
975a6aedc5dSRoger Pau Monné 	iov[0].iov_base = (void *)(uintptr_t) path;
976a6aedc5dSRoger Pau Monné 	iov[0].iov_len = strlen(path) + 1;
977a6aedc5dSRoger Pau Monné 	iov[1].iov_base = (void *)(uintptr_t) token;
978a6aedc5dSRoger Pau Monné 	iov[1].iov_len = strlen(token) + 1;
979a6aedc5dSRoger Pau Monné 
980a6aedc5dSRoger Pau Monné 	return (xs_talkv(XST_NIL, XS_WATCH, iov, 2, NULL, NULL));
981a6aedc5dSRoger Pau Monné }
982a6aedc5dSRoger Pau Monné 
983a6aedc5dSRoger Pau Monné /**
984a6aedc5dSRoger Pau Monné  * Transmit an uwatch request to the XenStore service.
985a6aedc5dSRoger Pau Monné  *
986a6aedc5dSRoger Pau Monné  * \param path    The path in the XenStore to watch.
987a6aedc5dSRoger Pau Monné  * \param tocken  A unique identifier for this watch.
988a6aedc5dSRoger Pau Monné  *
989a6aedc5dSRoger Pau Monné  * \return  0 on success.  Otherwise an errno indicating the
990a6aedc5dSRoger Pau Monné  *          cause of failure.
991a6aedc5dSRoger Pau Monné  */
992a6aedc5dSRoger Pau Monné static int
993a6aedc5dSRoger Pau Monné xs_unwatch(const char *path, const char *token)
994a6aedc5dSRoger Pau Monné {
995a6aedc5dSRoger Pau Monné 	struct iovec iov[2];
996a6aedc5dSRoger Pau Monné 
997a6aedc5dSRoger Pau Monné 	iov[0].iov_base = (void *)(uintptr_t) path;
998a6aedc5dSRoger Pau Monné 	iov[0].iov_len = strlen(path) + 1;
999a6aedc5dSRoger Pau Monné 	iov[1].iov_base = (void *)(uintptr_t) token;
1000a6aedc5dSRoger Pau Monné 	iov[1].iov_len = strlen(token) + 1;
1001a6aedc5dSRoger Pau Monné 
1002a6aedc5dSRoger Pau Monné 	return (xs_talkv(XST_NIL, XS_UNWATCH, iov, 2, NULL, NULL));
1003a6aedc5dSRoger Pau Monné }
1004a6aedc5dSRoger Pau Monné 
1005a6aedc5dSRoger Pau Monné /**
1006a6aedc5dSRoger Pau Monné  * Convert from watch token (unique identifier) to the associated
1007a6aedc5dSRoger Pau Monné  * internal tracking structure for this watch.
1008a6aedc5dSRoger Pau Monné  *
1009a6aedc5dSRoger Pau Monné  * \param tocken  The unique identifier for the watch to find.
1010a6aedc5dSRoger Pau Monné  *
1011a6aedc5dSRoger Pau Monné  * \return  A pointer to the found watch structure or NULL.
1012a6aedc5dSRoger Pau Monné  */
1013a6aedc5dSRoger Pau Monné static struct xs_watch *
1014a6aedc5dSRoger Pau Monné find_watch(const char *token)
1015a6aedc5dSRoger Pau Monné {
1016a6aedc5dSRoger Pau Monné 	struct xs_watch *i, *cmp;
1017a6aedc5dSRoger Pau Monné 
1018a6aedc5dSRoger Pau Monné 	cmp = (void *)strtoul(token, NULL, 16);
1019a6aedc5dSRoger Pau Monné 
1020a6aedc5dSRoger Pau Monné 	LIST_FOREACH(i, &xs.registered_watches, list)
1021a6aedc5dSRoger Pau Monné 		if (i == cmp)
1022a6aedc5dSRoger Pau Monné 			return (i);
1023a6aedc5dSRoger Pau Monné 
1024a6aedc5dSRoger Pau Monné 	return (NULL);
1025a6aedc5dSRoger Pau Monné }
1026a6aedc5dSRoger Pau Monné 
1027a6aedc5dSRoger Pau Monné /**
1028a6aedc5dSRoger Pau Monné  * Thread body of the XenStore watch event dispatch thread.
1029a6aedc5dSRoger Pau Monné  */
1030a6aedc5dSRoger Pau Monné static void
1031a6aedc5dSRoger Pau Monné xenwatch_thread(void *unused)
1032a6aedc5dSRoger Pau Monné {
1033a6aedc5dSRoger Pau Monné 	struct xs_stored_msg *msg;
1034a6aedc5dSRoger Pau Monné 
1035a6aedc5dSRoger Pau Monné 	for (;;) {
1036a6aedc5dSRoger Pau Monné 
1037a6aedc5dSRoger Pau Monné 		mtx_lock(&xs.watch_events_lock);
1038a6aedc5dSRoger Pau Monné 		while (TAILQ_EMPTY(&xs.watch_events))
1039a6aedc5dSRoger Pau Monné 			mtx_sleep(&xs.watch_events,
1040a6aedc5dSRoger Pau Monné 			    &xs.watch_events_lock,
1041a6aedc5dSRoger Pau Monné 			    PWAIT | PCATCH, "waitev", hz/10);
1042a6aedc5dSRoger Pau Monné 
1043a6aedc5dSRoger Pau Monné 		mtx_unlock(&xs.watch_events_lock);
1044a6aedc5dSRoger Pau Monné 		sx_xlock(&xs.xenwatch_mutex);
1045a6aedc5dSRoger Pau Monné 
1046a6aedc5dSRoger Pau Monné 		mtx_lock(&xs.watch_events_lock);
1047a6aedc5dSRoger Pau Monné 		msg = TAILQ_FIRST(&xs.watch_events);
1048a6aedc5dSRoger Pau Monné 		if (msg)
1049a6aedc5dSRoger Pau Monné 			TAILQ_REMOVE(&xs.watch_events, msg, list);
1050a6aedc5dSRoger Pau Monné 		mtx_unlock(&xs.watch_events_lock);
1051a6aedc5dSRoger Pau Monné 
1052a6aedc5dSRoger Pau Monné 		if (msg != NULL) {
1053a6aedc5dSRoger Pau Monné 			/*
1054a6aedc5dSRoger Pau Monné 			 * XXX There are messages coming in with a NULL
1055a6aedc5dSRoger Pau Monné 			 * XXX callback.  This deserves further investigation;
1056a6aedc5dSRoger Pau Monné 			 * XXX the workaround here simply prevents the kernel
1057a6aedc5dSRoger Pau Monné 			 * XXX from panic'ing on startup.
1058a6aedc5dSRoger Pau Monné 			 */
1059a6aedc5dSRoger Pau Monné 			if (msg->u.watch.handle->callback != NULL)
1060a6aedc5dSRoger Pau Monné 				msg->u.watch.handle->callback(
1061a6aedc5dSRoger Pau Monné 					msg->u.watch.handle,
1062a6aedc5dSRoger Pau Monné 					(const char **)msg->u.watch.vec,
1063a6aedc5dSRoger Pau Monné 					msg->u.watch.vec_size);
1064a6aedc5dSRoger Pau Monné 			free(msg->u.watch.vec, M_XENSTORE);
1065a6aedc5dSRoger Pau Monné 			free(msg, M_XENSTORE);
1066a6aedc5dSRoger Pau Monné 		}
1067a6aedc5dSRoger Pau Monné 
1068a6aedc5dSRoger Pau Monné 		sx_xunlock(&xs.xenwatch_mutex);
1069a6aedc5dSRoger Pau Monné 	}
1070a6aedc5dSRoger Pau Monné }
1071a6aedc5dSRoger Pau Monné 
1072a6aedc5dSRoger Pau Monné /*----------- XenStore Configuration, Initialization, and Control ------------*/
1073a6aedc5dSRoger Pau Monné /**
1074a6aedc5dSRoger Pau Monné  * Setup communication channels with the XenStore service.
1075a6aedc5dSRoger Pau Monné  *
1076a6aedc5dSRoger Pau Monné  * \return  On success, 0. Otherwise an errno value indicating the
1077a6aedc5dSRoger Pau Monné  *          type of failure.
1078a6aedc5dSRoger Pau Monné  */
1079a6aedc5dSRoger Pau Monné static int
1080a6aedc5dSRoger Pau Monné xs_init_comms(void)
1081a6aedc5dSRoger Pau Monné {
1082a6aedc5dSRoger Pau Monné 	int error;
1083a6aedc5dSRoger Pau Monné 
1084a6aedc5dSRoger Pau Monné 	if (xen_store->rsp_prod != xen_store->rsp_cons) {
1085a6aedc5dSRoger Pau Monné 		log(LOG_WARNING, "XENSTORE response ring is not quiescent "
1086a6aedc5dSRoger Pau Monné 		    "(%08x:%08x): fixing up\n",
1087a6aedc5dSRoger Pau Monné 		    xen_store->rsp_cons, xen_store->rsp_prod);
1088a6aedc5dSRoger Pau Monné 		xen_store->rsp_cons = xen_store->rsp_prod;
1089a6aedc5dSRoger Pau Monné 	}
1090a6aedc5dSRoger Pau Monné 
1091a6aedc5dSRoger Pau Monné 	xen_intr_unbind(&xs.xen_intr_handle);
1092a6aedc5dSRoger Pau Monné 
1093a6aedc5dSRoger Pau Monné 	error = xen_intr_bind_local_port(xs.xs_dev, xs.evtchn,
1094a6aedc5dSRoger Pau Monné 	    /*filter*/NULL, xs_intr, /*arg*/NULL, INTR_TYPE_NET|INTR_MPSAFE,
1095a6aedc5dSRoger Pau Monné 	    &xs.xen_intr_handle);
1096a6aedc5dSRoger Pau Monné 	if (error) {
1097a6aedc5dSRoger Pau Monné 		log(LOG_WARNING, "XENSTORE request irq failed %i\n", error);
1098a6aedc5dSRoger Pau Monné 		return (error);
1099a6aedc5dSRoger Pau Monné 	}
1100a6aedc5dSRoger Pau Monné 
1101a6aedc5dSRoger Pau Monné 	return (0);
1102a6aedc5dSRoger Pau Monné }
1103a6aedc5dSRoger Pau Monné 
1104a6aedc5dSRoger Pau Monné /*------------------ Private Device Attachment Functions  --------------------*/
1105a6aedc5dSRoger Pau Monné static void
1106a6aedc5dSRoger Pau Monné xs_identify(driver_t *driver, device_t parent)
1107a6aedc5dSRoger Pau Monné {
1108a6aedc5dSRoger Pau Monné 
1109a6aedc5dSRoger Pau Monné 	BUS_ADD_CHILD(parent, 0, "xenstore", 0);
1110a6aedc5dSRoger Pau Monné }
1111a6aedc5dSRoger Pau Monné 
1112a6aedc5dSRoger Pau Monné /**
1113*453130d9SPedro F. Giffuni  * Probe for the existence of the XenStore.
1114a6aedc5dSRoger Pau Monné  *
1115a6aedc5dSRoger Pau Monné  * \param dev
1116a6aedc5dSRoger Pau Monné  */
1117a6aedc5dSRoger Pau Monné static int
1118a6aedc5dSRoger Pau Monné xs_probe(device_t dev)
1119a6aedc5dSRoger Pau Monné {
1120a6aedc5dSRoger Pau Monné 	/*
1121a6aedc5dSRoger Pau Monné 	 * We are either operating within a PV kernel or being probed
1122a6aedc5dSRoger Pau Monné 	 * as the child of the successfully attached xenpci device.
1123a6aedc5dSRoger Pau Monné 	 * Thus we are in a Xen environment and there will be a XenStore.
1124a6aedc5dSRoger Pau Monné 	 * Unconditionally return success.
1125a6aedc5dSRoger Pau Monné 	 */
1126a6aedc5dSRoger Pau Monné 	device_set_desc(dev, "XenStore");
11272f9ec994SRoger Pau Monné 	return (BUS_PROBE_NOWILDCARD);
1128a6aedc5dSRoger Pau Monné }
1129a6aedc5dSRoger Pau Monné 
1130a6aedc5dSRoger Pau Monné static void
1131a6aedc5dSRoger Pau Monné xs_attach_deferred(void *arg)
1132a6aedc5dSRoger Pau Monné {
1133a6aedc5dSRoger Pau Monné 
1134a6aedc5dSRoger Pau Monné 	bus_generic_probe(xs.xs_dev);
1135a6aedc5dSRoger Pau Monné 	bus_generic_attach(xs.xs_dev);
1136a6aedc5dSRoger Pau Monné 
1137a6aedc5dSRoger Pau Monné 	config_intrhook_disestablish(&xs.xs_attachcb);
1138a6aedc5dSRoger Pau Monné }
1139a6aedc5dSRoger Pau Monné 
11401d84e2b3SRoger Pau Monné static void
11411d84e2b3SRoger Pau Monné xs_attach_late(void *arg, int pending)
11421d84e2b3SRoger Pau Monné {
11431d84e2b3SRoger Pau Monné 
11441d84e2b3SRoger Pau Monné 	KASSERT((pending == 1), ("xs late attach queued several times"));
11451d84e2b3SRoger Pau Monné 	bus_generic_probe(xs.xs_dev);
11461d84e2b3SRoger Pau Monné 	bus_generic_attach(xs.xs_dev);
11471d84e2b3SRoger Pau Monné }
11481d84e2b3SRoger Pau Monné 
1149a6aedc5dSRoger Pau Monné /**
1150a6aedc5dSRoger Pau Monné  * Attach to the XenStore.
1151a6aedc5dSRoger Pau Monné  *
1152a6aedc5dSRoger Pau Monné  * This routine also prepares for the probe/attach of drivers that rely
1153a6aedc5dSRoger Pau Monné  * on the XenStore.
1154a6aedc5dSRoger Pau Monné  */
1155a6aedc5dSRoger Pau Monné static int
1156a6aedc5dSRoger Pau Monné xs_attach(device_t dev)
1157a6aedc5dSRoger Pau Monné {
1158a6aedc5dSRoger Pau Monné 	int error;
1159a6aedc5dSRoger Pau Monné 
1160a6aedc5dSRoger Pau Monné 	/* Allow us to get device_t from softc and vice-versa. */
1161a6aedc5dSRoger Pau Monné 	xs.xs_dev = dev;
1162a6aedc5dSRoger Pau Monné 	device_set_softc(dev, &xs);
1163a6aedc5dSRoger Pau Monné 
1164a6aedc5dSRoger Pau Monné 	/* Initialize the interface to xenstore. */
1165a6aedc5dSRoger Pau Monné 	struct proc *p;
1166a6aedc5dSRoger Pau Monné 
11671d84e2b3SRoger Pau Monné 	xs.initialized = false;
1168a6aedc5dSRoger Pau Monné 	if (xen_hvm_domain()) {
1169a6aedc5dSRoger Pau Monné 		xs.evtchn = hvm_get_parameter(HVM_PARAM_STORE_EVTCHN);
1170a6aedc5dSRoger Pau Monné 		xs.gpfn = hvm_get_parameter(HVM_PARAM_STORE_PFN);
1171a6aedc5dSRoger Pau Monné 		xen_store = pmap_mapdev(xs.gpfn * PAGE_SIZE, PAGE_SIZE);
11721d84e2b3SRoger Pau Monné 		xs.initialized = true;
1173a6aedc5dSRoger Pau Monné 	} else if (xen_pv_domain()) {
11741d84e2b3SRoger Pau Monné 		if (HYPERVISOR_start_info->store_evtchn == 0) {
11751d84e2b3SRoger Pau Monné 			struct evtchn_alloc_unbound alloc_unbound;
11761d84e2b3SRoger Pau Monné 
11771d84e2b3SRoger Pau Monné 			/* Allocate a local event channel for xenstore */
11781d84e2b3SRoger Pau Monné 			alloc_unbound.dom = DOMID_SELF;
11791d84e2b3SRoger Pau Monné 			alloc_unbound.remote_dom = DOMID_SELF;
11801d84e2b3SRoger Pau Monné 			error = HYPERVISOR_event_channel_op(
11811d84e2b3SRoger Pau Monné 			    EVTCHNOP_alloc_unbound, &alloc_unbound);
11821d84e2b3SRoger Pau Monné 			if (error != 0)
11831d84e2b3SRoger Pau Monné 				panic(
11841d84e2b3SRoger Pau Monné 				   "unable to alloc event channel for Dom0: %d",
11851d84e2b3SRoger Pau Monné 				    error);
11861d84e2b3SRoger Pau Monné 
11871d84e2b3SRoger Pau Monné 			HYPERVISOR_start_info->store_evtchn =
11881d84e2b3SRoger Pau Monné 			    alloc_unbound.port;
11891d84e2b3SRoger Pau Monné 			xs.evtchn = alloc_unbound.port;
11901d84e2b3SRoger Pau Monné 
11911d84e2b3SRoger Pau Monné 			/* Allocate memory for the xs shared ring */
11921d84e2b3SRoger Pau Monné 			xen_store = malloc(PAGE_SIZE, M_XENSTORE,
11931d84e2b3SRoger Pau Monné 			    M_WAITOK | M_ZERO);
11941d84e2b3SRoger Pau Monné 		} else {
1195a6aedc5dSRoger Pau Monné 			xs.evtchn = HYPERVISOR_start_info->store_evtchn;
11961d84e2b3SRoger Pau Monné 			xs.initialized = true;
11971d84e2b3SRoger Pau Monné 		}
1198a6aedc5dSRoger Pau Monné 	} else {
1199a6aedc5dSRoger Pau Monné 		panic("Unknown domain type, cannot initialize xenstore.");
1200a6aedc5dSRoger Pau Monné 	}
1201a6aedc5dSRoger Pau Monné 
1202a6aedc5dSRoger Pau Monné 	TAILQ_INIT(&xs.reply_list);
1203a6aedc5dSRoger Pau Monné 	TAILQ_INIT(&xs.watch_events);
1204a6aedc5dSRoger Pau Monné 
1205a6aedc5dSRoger Pau Monné 	mtx_init(&xs.ring_lock, "ring lock", NULL, MTX_DEF);
1206a6aedc5dSRoger Pau Monné 	mtx_init(&xs.reply_lock, "reply lock", NULL, MTX_DEF);
1207a6aedc5dSRoger Pau Monné 	sx_init(&xs.xenwatch_mutex, "xenwatch");
1208a6aedc5dSRoger Pau Monné 	sx_init(&xs.request_mutex, "xenstore request");
1209a6aedc5dSRoger Pau Monné 	sx_init(&xs.suspend_mutex, "xenstore suspend");
1210a6aedc5dSRoger Pau Monné 	mtx_init(&xs.registered_watches_lock, "watches", NULL, MTX_DEF);
1211a6aedc5dSRoger Pau Monné 	mtx_init(&xs.watch_events_lock, "watch events", NULL, MTX_DEF);
1212a6aedc5dSRoger Pau Monné 
1213a6aedc5dSRoger Pau Monné 	/* Initialize the shared memory rings to talk to xenstored */
1214a6aedc5dSRoger Pau Monné 	error = xs_init_comms();
1215a6aedc5dSRoger Pau Monné 	if (error)
1216a6aedc5dSRoger Pau Monné 		return (error);
1217a6aedc5dSRoger Pau Monné 
1218a6aedc5dSRoger Pau Monné 	error = kproc_create(xenwatch_thread, NULL, &p, RFHIGHPID,
1219a6aedc5dSRoger Pau Monné 	    0, "xenwatch");
1220a6aedc5dSRoger Pau Monné 	if (error)
1221a6aedc5dSRoger Pau Monné 		return (error);
1222a6aedc5dSRoger Pau Monné 	xs.xenwatch_pid = p->p_pid;
1223a6aedc5dSRoger Pau Monné 
1224a6aedc5dSRoger Pau Monné 	error = kproc_create(xs_rcv_thread, NULL, NULL,
1225a6aedc5dSRoger Pau Monné 	    RFHIGHPID, 0, "xenstore_rcv");
1226a6aedc5dSRoger Pau Monné 
1227a6aedc5dSRoger Pau Monné 	xs.xs_attachcb.ich_func = xs_attach_deferred;
1228a6aedc5dSRoger Pau Monné 	xs.xs_attachcb.ich_arg = NULL;
12291d84e2b3SRoger Pau Monné 	if (xs.initialized) {
1230a6aedc5dSRoger Pau Monné 		config_intrhook_establish(&xs.xs_attachcb);
12311d84e2b3SRoger Pau Monné 	} else {
12321d84e2b3SRoger Pau Monné 		TASK_INIT(&xs.xs_late_init, 0, xs_attach_late, NULL);
12331d84e2b3SRoger Pau Monné 	}
1234a6aedc5dSRoger Pau Monné 
1235a6aedc5dSRoger Pau Monné 	return (error);
1236a6aedc5dSRoger Pau Monné }
1237a6aedc5dSRoger Pau Monné 
1238a6aedc5dSRoger Pau Monné /**
1239a6aedc5dSRoger Pau Monné  * Prepare for suspension of this VM by halting XenStore access after
1240a6aedc5dSRoger Pau Monné  * all transactions and individual requests have completed.
1241a6aedc5dSRoger Pau Monné  */
1242a6aedc5dSRoger Pau Monné static int
1243a6aedc5dSRoger Pau Monné xs_suspend(device_t dev)
1244a6aedc5dSRoger Pau Monné {
1245a6aedc5dSRoger Pau Monné 	int error;
1246a6aedc5dSRoger Pau Monné 
1247a6aedc5dSRoger Pau Monné 	/* Suspend child Xen devices. */
1248a6aedc5dSRoger Pau Monné 	error = bus_generic_suspend(dev);
1249a6aedc5dSRoger Pau Monné 	if (error != 0)
1250a6aedc5dSRoger Pau Monné 		return (error);
1251a6aedc5dSRoger Pau Monné 
1252a6aedc5dSRoger Pau Monné 	sx_xlock(&xs.suspend_mutex);
1253a6aedc5dSRoger Pau Monné 	sx_xlock(&xs.request_mutex);
1254a6aedc5dSRoger Pau Monné 
1255a6aedc5dSRoger Pau Monné 	return (0);
1256a6aedc5dSRoger Pau Monné }
1257a6aedc5dSRoger Pau Monné 
1258a6aedc5dSRoger Pau Monné /**
1259a6aedc5dSRoger Pau Monné  * Resume XenStore operations after this VM is resumed.
1260a6aedc5dSRoger Pau Monné  */
1261a6aedc5dSRoger Pau Monné static int
1262a6aedc5dSRoger Pau Monné xs_resume(device_t dev __unused)
1263a6aedc5dSRoger Pau Monné {
1264a6aedc5dSRoger Pau Monné 	struct xs_watch *watch;
1265a6aedc5dSRoger Pau Monné 	char token[sizeof(watch) * 2 + 1];
1266a6aedc5dSRoger Pau Monné 
1267a6aedc5dSRoger Pau Monné 	xs_init_comms();
1268a6aedc5dSRoger Pau Monné 
1269a6aedc5dSRoger Pau Monné 	sx_xunlock(&xs.request_mutex);
1270a6aedc5dSRoger Pau Monné 
1271a6aedc5dSRoger Pau Monné 	/*
1272a6aedc5dSRoger Pau Monné 	 * No need for registered_watches_lock: the suspend_mutex
1273a6aedc5dSRoger Pau Monné 	 * is sufficient.
1274a6aedc5dSRoger Pau Monné 	 */
1275a6aedc5dSRoger Pau Monné 	LIST_FOREACH(watch, &xs.registered_watches, list) {
1276a6aedc5dSRoger Pau Monné 		sprintf(token, "%lX", (long)watch);
1277a6aedc5dSRoger Pau Monné 		xs_watch(watch->node, token);
1278a6aedc5dSRoger Pau Monné 	}
1279a6aedc5dSRoger Pau Monné 
1280a6aedc5dSRoger Pau Monné 	sx_xunlock(&xs.suspend_mutex);
1281a6aedc5dSRoger Pau Monné 
1282a6aedc5dSRoger Pau Monné 	/* Resume child Xen devices. */
1283a6aedc5dSRoger Pau Monné 	bus_generic_resume(dev);
1284a6aedc5dSRoger Pau Monné 
1285a6aedc5dSRoger Pau Monné 	return (0);
1286a6aedc5dSRoger Pau Monné }
1287a6aedc5dSRoger Pau Monné 
1288a6aedc5dSRoger Pau Monné /*-------------------- Private Device Attachment Data  -----------------------*/
1289a6aedc5dSRoger Pau Monné static device_method_t xenstore_methods[] = {
1290a6aedc5dSRoger Pau Monné 	/* Device interface */
1291a6aedc5dSRoger Pau Monné 	DEVMETHOD(device_identify,	xs_identify),
1292a6aedc5dSRoger Pau Monné 	DEVMETHOD(device_probe,         xs_probe),
1293a6aedc5dSRoger Pau Monné 	DEVMETHOD(device_attach,        xs_attach),
1294a6aedc5dSRoger Pau Monné 	DEVMETHOD(device_detach,        bus_generic_detach),
1295a6aedc5dSRoger Pau Monné 	DEVMETHOD(device_shutdown,      bus_generic_shutdown),
1296a6aedc5dSRoger Pau Monné 	DEVMETHOD(device_suspend,       xs_suspend),
1297a6aedc5dSRoger Pau Monné 	DEVMETHOD(device_resume,        xs_resume),
1298a6aedc5dSRoger Pau Monné 
1299a6aedc5dSRoger Pau Monné 	/* Bus interface */
1300a6aedc5dSRoger Pau Monné 	DEVMETHOD(bus_add_child,        bus_generic_add_child),
1301a6aedc5dSRoger Pau Monné 	DEVMETHOD(bus_alloc_resource,   bus_generic_alloc_resource),
1302a6aedc5dSRoger Pau Monné 	DEVMETHOD(bus_release_resource, bus_generic_release_resource),
1303a6aedc5dSRoger Pau Monné 	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
1304a6aedc5dSRoger Pau Monné 	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
1305a6aedc5dSRoger Pau Monné 
1306a6aedc5dSRoger Pau Monné 	DEVMETHOD_END
1307a6aedc5dSRoger Pau Monné };
1308a6aedc5dSRoger Pau Monné 
1309a6aedc5dSRoger Pau Monné DEFINE_CLASS_0(xenstore, xenstore_driver, xenstore_methods, 0);
1310a6aedc5dSRoger Pau Monné static devclass_t xenstore_devclass;
1311a6aedc5dSRoger Pau Monné 
1312a6aedc5dSRoger Pau Monné DRIVER_MODULE(xenstore, xenpv, xenstore_driver, xenstore_devclass, 0, 0);
1313a6aedc5dSRoger Pau Monné 
1314a6aedc5dSRoger Pau Monné /*------------------------------- Sysctl Data --------------------------------*/
1315a6aedc5dSRoger Pau Monné /* XXX Shouldn't the node be somewhere else? */
1316a6aedc5dSRoger Pau Monné SYSCTL_NODE(_dev, OID_AUTO, xen, CTLFLAG_RD, NULL, "Xen");
1317a6aedc5dSRoger Pau Monné SYSCTL_INT(_dev_xen, OID_AUTO, xsd_port, CTLFLAG_RD, &xs.evtchn, 0, "");
1318a6aedc5dSRoger Pau Monné SYSCTL_ULONG(_dev_xen, OID_AUTO, xsd_kva, CTLFLAG_RD, (u_long *) &xen_store, 0, "");
1319a6aedc5dSRoger Pau Monné 
1320a6aedc5dSRoger Pau Monné /*-------------------------------- Public API --------------------------------*/
1321a6aedc5dSRoger Pau Monné /*------- API comments for these methods can be found in xenstorevar.h -------*/
1322a6aedc5dSRoger Pau Monné int
1323a6aedc5dSRoger Pau Monné xs_directory(struct xs_transaction t, const char *dir, const char *node,
1324a6aedc5dSRoger Pau Monné     u_int *num, const char ***result)
1325a6aedc5dSRoger Pau Monné {
1326a6aedc5dSRoger Pau Monné 	struct sbuf *path;
1327a6aedc5dSRoger Pau Monné 	char *strings;
1328a6aedc5dSRoger Pau Monné 	u_int len = 0;
1329a6aedc5dSRoger Pau Monné 	int error;
1330a6aedc5dSRoger Pau Monné 
1331a6aedc5dSRoger Pau Monné 	path = xs_join(dir, node);
1332a6aedc5dSRoger Pau Monné 	error = xs_single(t, XS_DIRECTORY, sbuf_data(path), &len,
1333a6aedc5dSRoger Pau Monné 	    (void **)&strings);
1334a6aedc5dSRoger Pau Monné 	sbuf_delete(path);
1335a6aedc5dSRoger Pau Monné 	if (error)
1336a6aedc5dSRoger Pau Monné 		return (error);
1337a6aedc5dSRoger Pau Monné 
1338a6aedc5dSRoger Pau Monné 	*result = split(strings, len, num);
1339a6aedc5dSRoger Pau Monné 
1340a6aedc5dSRoger Pau Monné 	return (0);
1341a6aedc5dSRoger Pau Monné }
1342a6aedc5dSRoger Pau Monné 
1343a6aedc5dSRoger Pau Monné int
1344a6aedc5dSRoger Pau Monné xs_exists(struct xs_transaction t, const char *dir, const char *node)
1345a6aedc5dSRoger Pau Monné {
1346a6aedc5dSRoger Pau Monné 	const char **d;
1347a6aedc5dSRoger Pau Monné 	int error, dir_n;
1348a6aedc5dSRoger Pau Monné 
1349a6aedc5dSRoger Pau Monné 	error = xs_directory(t, dir, node, &dir_n, &d);
1350a6aedc5dSRoger Pau Monné 	if (error)
1351a6aedc5dSRoger Pau Monné 		return (0);
1352a6aedc5dSRoger Pau Monné 	free(d, M_XENSTORE);
1353a6aedc5dSRoger Pau Monné 	return (1);
1354a6aedc5dSRoger Pau Monné }
1355a6aedc5dSRoger Pau Monné 
1356a6aedc5dSRoger Pau Monné int
1357a6aedc5dSRoger Pau Monné xs_read(struct xs_transaction t, const char *dir, const char *node,
1358a6aedc5dSRoger Pau Monné     u_int *len, void **result)
1359a6aedc5dSRoger Pau Monné {
1360a6aedc5dSRoger Pau Monné 	struct sbuf *path;
1361a6aedc5dSRoger Pau Monné 	void *ret;
1362a6aedc5dSRoger Pau Monné 	int error;
1363a6aedc5dSRoger Pau Monné 
1364a6aedc5dSRoger Pau Monné 	path = xs_join(dir, node);
1365a6aedc5dSRoger Pau Monné 	error = xs_single(t, XS_READ, sbuf_data(path), len, &ret);
1366a6aedc5dSRoger Pau Monné 	sbuf_delete(path);
1367a6aedc5dSRoger Pau Monné 	if (error)
1368a6aedc5dSRoger Pau Monné 		return (error);
1369a6aedc5dSRoger Pau Monné 	*result = ret;
1370a6aedc5dSRoger Pau Monné 	return (0);
1371a6aedc5dSRoger Pau Monné }
1372a6aedc5dSRoger Pau Monné 
1373a6aedc5dSRoger Pau Monné int
1374a6aedc5dSRoger Pau Monné xs_write(struct xs_transaction t, const char *dir, const char *node,
1375a6aedc5dSRoger Pau Monné     const char *string)
1376a6aedc5dSRoger Pau Monné {
1377a6aedc5dSRoger Pau Monné 	struct sbuf *path;
1378a6aedc5dSRoger Pau Monné 	struct iovec iovec[2];
1379a6aedc5dSRoger Pau Monné 	int error;
1380a6aedc5dSRoger Pau Monné 
1381a6aedc5dSRoger Pau Monné 	path = xs_join(dir, node);
1382a6aedc5dSRoger Pau Monné 
1383a6aedc5dSRoger Pau Monné 	iovec[0].iov_base = (void *)(uintptr_t) sbuf_data(path);
1384a6aedc5dSRoger Pau Monné 	iovec[0].iov_len = sbuf_len(path) + 1;
1385a6aedc5dSRoger Pau Monné 	iovec[1].iov_base = (void *)(uintptr_t) string;
1386a6aedc5dSRoger Pau Monné 	iovec[1].iov_len = strlen(string);
1387a6aedc5dSRoger Pau Monné 
1388a6aedc5dSRoger Pau Monné 	error = xs_talkv(t, XS_WRITE, iovec, 2, NULL, NULL);
1389a6aedc5dSRoger Pau Monné 	sbuf_delete(path);
1390a6aedc5dSRoger Pau Monné 
1391a6aedc5dSRoger Pau Monné 	return (error);
1392a6aedc5dSRoger Pau Monné }
1393a6aedc5dSRoger Pau Monné 
1394a6aedc5dSRoger Pau Monné int
1395a6aedc5dSRoger Pau Monné xs_mkdir(struct xs_transaction t, const char *dir, const char *node)
1396a6aedc5dSRoger Pau Monné {
1397a6aedc5dSRoger Pau Monné 	struct sbuf *path;
1398a6aedc5dSRoger Pau Monné 	int ret;
1399a6aedc5dSRoger Pau Monné 
1400a6aedc5dSRoger Pau Monné 	path = xs_join(dir, node);
1401a6aedc5dSRoger Pau Monné 	ret = xs_single(t, XS_MKDIR, sbuf_data(path), NULL, NULL);
1402a6aedc5dSRoger Pau Monné 	sbuf_delete(path);
1403a6aedc5dSRoger Pau Monné 
1404a6aedc5dSRoger Pau Monné 	return (ret);
1405a6aedc5dSRoger Pau Monné }
1406a6aedc5dSRoger Pau Monné 
1407a6aedc5dSRoger Pau Monné int
1408a6aedc5dSRoger Pau Monné xs_rm(struct xs_transaction t, const char *dir, const char *node)
1409a6aedc5dSRoger Pau Monné {
1410a6aedc5dSRoger Pau Monné 	struct sbuf *path;
1411a6aedc5dSRoger Pau Monné 	int ret;
1412a6aedc5dSRoger Pau Monné 
1413a6aedc5dSRoger Pau Monné 	path = xs_join(dir, node);
1414a6aedc5dSRoger Pau Monné 	ret = xs_single(t, XS_RM, sbuf_data(path), NULL, NULL);
1415a6aedc5dSRoger Pau Monné 	sbuf_delete(path);
1416a6aedc5dSRoger Pau Monné 
1417a6aedc5dSRoger Pau Monné 	return (ret);
1418a6aedc5dSRoger Pau Monné }
1419a6aedc5dSRoger Pau Monné 
1420a6aedc5dSRoger Pau Monné int
1421a6aedc5dSRoger Pau Monné xs_rm_tree(struct xs_transaction xbt, const char *base, const char *node)
1422a6aedc5dSRoger Pau Monné {
1423a6aedc5dSRoger Pau Monné 	struct xs_transaction local_xbt;
1424a6aedc5dSRoger Pau Monné 	struct sbuf *root_path_sbuf;
1425a6aedc5dSRoger Pau Monné 	struct sbuf *cur_path_sbuf;
1426a6aedc5dSRoger Pau Monné 	char *root_path;
1427a6aedc5dSRoger Pau Monné 	char *cur_path;
1428a6aedc5dSRoger Pau Monné 	const char **dir;
1429a6aedc5dSRoger Pau Monné 	int error;
1430a6aedc5dSRoger Pau Monné 
1431a6aedc5dSRoger Pau Monné retry:
1432a6aedc5dSRoger Pau Monné 	root_path_sbuf = xs_join(base, node);
1433a6aedc5dSRoger Pau Monné 	cur_path_sbuf  = xs_join(base, node);
1434a6aedc5dSRoger Pau Monné 	root_path      = sbuf_data(root_path_sbuf);
1435a6aedc5dSRoger Pau Monné 	cur_path       = sbuf_data(cur_path_sbuf);
1436a6aedc5dSRoger Pau Monné 	dir            = NULL;
1437a6aedc5dSRoger Pau Monné 	local_xbt.id   = 0;
1438a6aedc5dSRoger Pau Monné 
1439a6aedc5dSRoger Pau Monné 	if (xbt.id == 0) {
1440a6aedc5dSRoger Pau Monné 		error = xs_transaction_start(&local_xbt);
1441a6aedc5dSRoger Pau Monné 		if (error != 0)
1442a6aedc5dSRoger Pau Monné 			goto out;
1443a6aedc5dSRoger Pau Monné 		xbt = local_xbt;
1444a6aedc5dSRoger Pau Monné 	}
1445a6aedc5dSRoger Pau Monné 
1446a6aedc5dSRoger Pau Monné 	while (1) {
1447a6aedc5dSRoger Pau Monné 		u_int count;
1448a6aedc5dSRoger Pau Monné 		u_int i;
1449a6aedc5dSRoger Pau Monné 
1450a6aedc5dSRoger Pau Monné 		error = xs_directory(xbt, cur_path, "", &count, &dir);
1451a6aedc5dSRoger Pau Monné 		if (error)
1452a6aedc5dSRoger Pau Monné 			goto out;
1453a6aedc5dSRoger Pau Monné 
1454a6aedc5dSRoger Pau Monné 		for (i = 0; i < count; i++) {
1455a6aedc5dSRoger Pau Monné 			error = xs_rm(xbt, cur_path, dir[i]);
1456a6aedc5dSRoger Pau Monné 			if (error == ENOTEMPTY) {
1457a6aedc5dSRoger Pau Monné 				struct sbuf *push_dir;
1458a6aedc5dSRoger Pau Monné 
1459a6aedc5dSRoger Pau Monné 				/*
1460a6aedc5dSRoger Pau Monné 				 * Descend to clear out this sub directory.
1461a6aedc5dSRoger Pau Monné 				 * We'll return to cur_dir once push_dir
1462a6aedc5dSRoger Pau Monné 				 * is empty.
1463a6aedc5dSRoger Pau Monné 				 */
1464a6aedc5dSRoger Pau Monné 				push_dir = xs_join(cur_path, dir[i]);
1465a6aedc5dSRoger Pau Monné 				sbuf_delete(cur_path_sbuf);
1466a6aedc5dSRoger Pau Monné 				cur_path_sbuf = push_dir;
1467a6aedc5dSRoger Pau Monné 				cur_path = sbuf_data(cur_path_sbuf);
1468a6aedc5dSRoger Pau Monné 				break;
1469a6aedc5dSRoger Pau Monné 			} else if (error != 0) {
1470a6aedc5dSRoger Pau Monné 				goto out;
1471a6aedc5dSRoger Pau Monné 			}
1472a6aedc5dSRoger Pau Monné 		}
1473a6aedc5dSRoger Pau Monné 
1474a6aedc5dSRoger Pau Monné 		free(dir, M_XENSTORE);
1475a6aedc5dSRoger Pau Monné 		dir = NULL;
1476a6aedc5dSRoger Pau Monné 
1477a6aedc5dSRoger Pau Monné 		if (i == count) {
1478a6aedc5dSRoger Pau Monné 			char *last_slash;
1479a6aedc5dSRoger Pau Monné 
1480a6aedc5dSRoger Pau Monné 			/* Directory is empty.  It is now safe to remove. */
1481a6aedc5dSRoger Pau Monné 			error = xs_rm(xbt, cur_path, "");
1482a6aedc5dSRoger Pau Monné 			if (error != 0)
1483a6aedc5dSRoger Pau Monné 				goto out;
1484a6aedc5dSRoger Pau Monné 
1485a6aedc5dSRoger Pau Monné 			if (!strcmp(cur_path, root_path))
1486a6aedc5dSRoger Pau Monné 				break;
1487a6aedc5dSRoger Pau Monné 
1488a6aedc5dSRoger Pau Monné 			/* Return to processing the parent directory. */
1489a6aedc5dSRoger Pau Monné 			last_slash = strrchr(cur_path, '/');
1490a6aedc5dSRoger Pau Monné 			KASSERT(last_slash != NULL,
1491a6aedc5dSRoger Pau Monné 				("xs_rm_tree: mangled path %s", cur_path));
1492a6aedc5dSRoger Pau Monné 			*last_slash = '\0';
1493a6aedc5dSRoger Pau Monné 		}
1494a6aedc5dSRoger Pau Monné 	}
1495a6aedc5dSRoger Pau Monné 
1496a6aedc5dSRoger Pau Monné out:
1497a6aedc5dSRoger Pau Monné 	sbuf_delete(cur_path_sbuf);
1498a6aedc5dSRoger Pau Monné 	sbuf_delete(root_path_sbuf);
1499a6aedc5dSRoger Pau Monné 	if (dir != NULL)
1500a6aedc5dSRoger Pau Monné 		free(dir, M_XENSTORE);
1501a6aedc5dSRoger Pau Monné 
1502a6aedc5dSRoger Pau Monné 	if (local_xbt.id != 0) {
1503a6aedc5dSRoger Pau Monné 		int terror;
1504a6aedc5dSRoger Pau Monné 
1505a6aedc5dSRoger Pau Monné 		terror = xs_transaction_end(local_xbt, /*abort*/error != 0);
1506a6aedc5dSRoger Pau Monné 		xbt.id = 0;
1507a6aedc5dSRoger Pau Monné 		if (terror == EAGAIN && error == 0)
1508a6aedc5dSRoger Pau Monné 			goto retry;
1509a6aedc5dSRoger Pau Monné 	}
1510a6aedc5dSRoger Pau Monné 	return (error);
1511a6aedc5dSRoger Pau Monné }
1512a6aedc5dSRoger Pau Monné 
1513a6aedc5dSRoger Pau Monné int
1514a6aedc5dSRoger Pau Monné xs_transaction_start(struct xs_transaction *t)
1515a6aedc5dSRoger Pau Monné {
1516a6aedc5dSRoger Pau Monné 	char *id_str;
1517a6aedc5dSRoger Pau Monné 	int error;
1518a6aedc5dSRoger Pau Monné 
1519a6aedc5dSRoger Pau Monné 	error = xs_single(XST_NIL, XS_TRANSACTION_START, "", NULL,
1520a6aedc5dSRoger Pau Monné 	    (void **)&id_str);
1521a6aedc5dSRoger Pau Monné 	if (error == 0) {
1522a6aedc5dSRoger Pau Monné 		t->id = strtoul(id_str, NULL, 0);
1523a6aedc5dSRoger Pau Monné 		free(id_str, M_XENSTORE);
1524a6aedc5dSRoger Pau Monné 	}
1525a6aedc5dSRoger Pau Monné 	return (error);
1526a6aedc5dSRoger Pau Monné }
1527a6aedc5dSRoger Pau Monné 
1528a6aedc5dSRoger Pau Monné int
1529a6aedc5dSRoger Pau Monné xs_transaction_end(struct xs_transaction t, int abort)
1530a6aedc5dSRoger Pau Monné {
1531a6aedc5dSRoger Pau Monné 	char abortstr[2];
1532a6aedc5dSRoger Pau Monné 
1533a6aedc5dSRoger Pau Monné 	if (abort)
1534a6aedc5dSRoger Pau Monné 		strcpy(abortstr, "F");
1535a6aedc5dSRoger Pau Monné 	else
1536a6aedc5dSRoger Pau Monné 		strcpy(abortstr, "T");
1537a6aedc5dSRoger Pau Monné 
1538a6aedc5dSRoger Pau Monné 	return (xs_single(t, XS_TRANSACTION_END, abortstr, NULL, NULL));
1539a6aedc5dSRoger Pau Monné }
1540a6aedc5dSRoger Pau Monné 
1541a6aedc5dSRoger Pau Monné int
1542a6aedc5dSRoger Pau Monné xs_scanf(struct xs_transaction t, const char *dir, const char *node,
1543a6aedc5dSRoger Pau Monné      int *scancountp, const char *fmt, ...)
1544a6aedc5dSRoger Pau Monné {
1545a6aedc5dSRoger Pau Monné 	va_list ap;
1546a6aedc5dSRoger Pau Monné 	int error, ns;
1547a6aedc5dSRoger Pau Monné 	char *val;
1548a6aedc5dSRoger Pau Monné 
1549a6aedc5dSRoger Pau Monné 	error = xs_read(t, dir, node, NULL, (void **) &val);
1550a6aedc5dSRoger Pau Monné 	if (error)
1551a6aedc5dSRoger Pau Monné 		return (error);
1552a6aedc5dSRoger Pau Monné 
1553a6aedc5dSRoger Pau Monné 	va_start(ap, fmt);
1554a6aedc5dSRoger Pau Monné 	ns = vsscanf(val, fmt, ap);
1555a6aedc5dSRoger Pau Monné 	va_end(ap);
1556a6aedc5dSRoger Pau Monné 	free(val, M_XENSTORE);
1557a6aedc5dSRoger Pau Monné 	/* Distinctive errno. */
1558a6aedc5dSRoger Pau Monné 	if (ns == 0)
1559a6aedc5dSRoger Pau Monné 		return (ERANGE);
1560a6aedc5dSRoger Pau Monné 	if (scancountp)
1561a6aedc5dSRoger Pau Monné 		*scancountp = ns;
1562a6aedc5dSRoger Pau Monné 	return (0);
1563a6aedc5dSRoger Pau Monné }
1564a6aedc5dSRoger Pau Monné 
1565a6aedc5dSRoger Pau Monné int
1566a6aedc5dSRoger Pau Monné xs_vprintf(struct xs_transaction t,
1567a6aedc5dSRoger Pau Monné     const char *dir, const char *node, const char *fmt, va_list ap)
1568a6aedc5dSRoger Pau Monné {
1569a6aedc5dSRoger Pau Monné 	struct sbuf *sb;
1570a6aedc5dSRoger Pau Monné 	int error;
1571a6aedc5dSRoger Pau Monné 
1572a6aedc5dSRoger Pau Monné 	sb = sbuf_new_auto();
1573a6aedc5dSRoger Pau Monné 	sbuf_vprintf(sb, fmt, ap);
1574a6aedc5dSRoger Pau Monné 	sbuf_finish(sb);
1575a6aedc5dSRoger Pau Monné 	error = xs_write(t, dir, node, sbuf_data(sb));
1576a6aedc5dSRoger Pau Monné 	sbuf_delete(sb);
1577a6aedc5dSRoger Pau Monné 
1578a6aedc5dSRoger Pau Monné 	return (error);
1579a6aedc5dSRoger Pau Monné }
1580a6aedc5dSRoger Pau Monné 
1581a6aedc5dSRoger Pau Monné int
1582a6aedc5dSRoger Pau Monné xs_printf(struct xs_transaction t, const char *dir, const char *node,
1583a6aedc5dSRoger Pau Monné      const char *fmt, ...)
1584a6aedc5dSRoger Pau Monné {
1585a6aedc5dSRoger Pau Monné 	va_list ap;
1586a6aedc5dSRoger Pau Monné 	int error;
1587a6aedc5dSRoger Pau Monné 
1588a6aedc5dSRoger Pau Monné 	va_start(ap, fmt);
1589a6aedc5dSRoger Pau Monné 	error = xs_vprintf(t, dir, node, fmt, ap);
1590a6aedc5dSRoger Pau Monné 	va_end(ap);
1591a6aedc5dSRoger Pau Monné 
1592a6aedc5dSRoger Pau Monné 	return (error);
1593a6aedc5dSRoger Pau Monné }
1594a6aedc5dSRoger Pau Monné 
1595a6aedc5dSRoger Pau Monné int
1596a6aedc5dSRoger Pau Monné xs_gather(struct xs_transaction t, const char *dir, ...)
1597a6aedc5dSRoger Pau Monné {
1598a6aedc5dSRoger Pau Monné 	va_list ap;
1599a6aedc5dSRoger Pau Monné 	const char *name;
1600a6aedc5dSRoger Pau Monné 	int error;
1601a6aedc5dSRoger Pau Monné 
1602a6aedc5dSRoger Pau Monné 	va_start(ap, dir);
1603a6aedc5dSRoger Pau Monné 	error = 0;
1604a6aedc5dSRoger Pau Monné 	while (error == 0 && (name = va_arg(ap, char *)) != NULL) {
1605a6aedc5dSRoger Pau Monné 		const char *fmt = va_arg(ap, char *);
1606a6aedc5dSRoger Pau Monné 		void *result = va_arg(ap, void *);
1607a6aedc5dSRoger Pau Monné 		char *p;
1608a6aedc5dSRoger Pau Monné 
1609a6aedc5dSRoger Pau Monné 		error = xs_read(t, dir, name, NULL, (void **) &p);
1610a6aedc5dSRoger Pau Monné 		if (error)
1611a6aedc5dSRoger Pau Monné 			break;
1612a6aedc5dSRoger Pau Monné 
1613a6aedc5dSRoger Pau Monné 		if (fmt) {
1614a6aedc5dSRoger Pau Monné 			if (sscanf(p, fmt, result) == 0)
1615a6aedc5dSRoger Pau Monné 				error = EINVAL;
1616a6aedc5dSRoger Pau Monné 			free(p, M_XENSTORE);
1617a6aedc5dSRoger Pau Monné 		} else
1618a6aedc5dSRoger Pau Monné 			*(char **)result = p;
1619a6aedc5dSRoger Pau Monné 	}
1620a6aedc5dSRoger Pau Monné 	va_end(ap);
1621a6aedc5dSRoger Pau Monné 
1622a6aedc5dSRoger Pau Monné 	return (error);
1623a6aedc5dSRoger Pau Monné }
1624a6aedc5dSRoger Pau Monné 
1625a6aedc5dSRoger Pau Monné int
1626a6aedc5dSRoger Pau Monné xs_register_watch(struct xs_watch *watch)
1627a6aedc5dSRoger Pau Monné {
1628a6aedc5dSRoger Pau Monné 	/* Pointer in ascii is the token. */
1629a6aedc5dSRoger Pau Monné 	char token[sizeof(watch) * 2 + 1];
1630a6aedc5dSRoger Pau Monné 	int error;
1631a6aedc5dSRoger Pau Monné 
1632a6aedc5dSRoger Pau Monné 	sprintf(token, "%lX", (long)watch);
1633a6aedc5dSRoger Pau Monné 
1634a6aedc5dSRoger Pau Monné 	sx_slock(&xs.suspend_mutex);
1635a6aedc5dSRoger Pau Monné 
1636a6aedc5dSRoger Pau Monné 	mtx_lock(&xs.registered_watches_lock);
1637a6aedc5dSRoger Pau Monné 	KASSERT(find_watch(token) == NULL, ("watch already registered"));
1638a6aedc5dSRoger Pau Monné 	LIST_INSERT_HEAD(&xs.registered_watches, watch, list);
1639a6aedc5dSRoger Pau Monné 	mtx_unlock(&xs.registered_watches_lock);
1640a6aedc5dSRoger Pau Monné 
1641a6aedc5dSRoger Pau Monné 	error = xs_watch(watch->node, token);
1642a6aedc5dSRoger Pau Monné 
1643a6aedc5dSRoger Pau Monné 	/* Ignore errors due to multiple registration. */
1644a6aedc5dSRoger Pau Monné 	if (error == EEXIST)
1645a6aedc5dSRoger Pau Monné 		error = 0;
1646a6aedc5dSRoger Pau Monné 
1647a6aedc5dSRoger Pau Monné 	if (error != 0) {
1648a6aedc5dSRoger Pau Monné 		mtx_lock(&xs.registered_watches_lock);
1649a6aedc5dSRoger Pau Monné 		LIST_REMOVE(watch, list);
1650a6aedc5dSRoger Pau Monné 		mtx_unlock(&xs.registered_watches_lock);
1651a6aedc5dSRoger Pau Monné 	}
1652a6aedc5dSRoger Pau Monné 
1653a6aedc5dSRoger Pau Monné 	sx_sunlock(&xs.suspend_mutex);
1654a6aedc5dSRoger Pau Monné 
1655a6aedc5dSRoger Pau Monné 	return (error);
1656a6aedc5dSRoger Pau Monné }
1657a6aedc5dSRoger Pau Monné 
1658a6aedc5dSRoger Pau Monné void
1659a6aedc5dSRoger Pau Monné xs_unregister_watch(struct xs_watch *watch)
1660a6aedc5dSRoger Pau Monné {
1661a6aedc5dSRoger Pau Monné 	struct xs_stored_msg *msg, *tmp;
1662a6aedc5dSRoger Pau Monné 	char token[sizeof(watch) * 2 + 1];
1663a6aedc5dSRoger Pau Monné 	int error;
1664a6aedc5dSRoger Pau Monné 
1665a6aedc5dSRoger Pau Monné 	sprintf(token, "%lX", (long)watch);
1666a6aedc5dSRoger Pau Monné 
1667a6aedc5dSRoger Pau Monné 	sx_slock(&xs.suspend_mutex);
1668a6aedc5dSRoger Pau Monné 
1669a6aedc5dSRoger Pau Monné 	mtx_lock(&xs.registered_watches_lock);
1670a6aedc5dSRoger Pau Monné 	if (find_watch(token) == NULL) {
1671a6aedc5dSRoger Pau Monné 		mtx_unlock(&xs.registered_watches_lock);
1672a6aedc5dSRoger Pau Monné 		sx_sunlock(&xs.suspend_mutex);
1673a6aedc5dSRoger Pau Monné 		return;
1674a6aedc5dSRoger Pau Monné 	}
1675a6aedc5dSRoger Pau Monné 	LIST_REMOVE(watch, list);
1676a6aedc5dSRoger Pau Monné 	mtx_unlock(&xs.registered_watches_lock);
1677a6aedc5dSRoger Pau Monné 
1678a6aedc5dSRoger Pau Monné 	error = xs_unwatch(watch->node, token);
1679a6aedc5dSRoger Pau Monné 	if (error)
1680a6aedc5dSRoger Pau Monné 		log(LOG_WARNING, "XENSTORE Failed to release watch %s: %i\n",
1681a6aedc5dSRoger Pau Monné 		    watch->node, error);
1682a6aedc5dSRoger Pau Monné 
1683a6aedc5dSRoger Pau Monné 	sx_sunlock(&xs.suspend_mutex);
1684a6aedc5dSRoger Pau Monné 
1685a6aedc5dSRoger Pau Monné 	/* Cancel pending watch events. */
1686a6aedc5dSRoger Pau Monné 	mtx_lock(&xs.watch_events_lock);
1687a6aedc5dSRoger Pau Monné 	TAILQ_FOREACH_SAFE(msg, &xs.watch_events, list, tmp) {
1688a6aedc5dSRoger Pau Monné 		if (msg->u.watch.handle != watch)
1689a6aedc5dSRoger Pau Monné 			continue;
1690a6aedc5dSRoger Pau Monné 		TAILQ_REMOVE(&xs.watch_events, msg, list);
1691a6aedc5dSRoger Pau Monné 		free(msg->u.watch.vec, M_XENSTORE);
1692a6aedc5dSRoger Pau Monné 		free(msg, M_XENSTORE);
1693a6aedc5dSRoger Pau Monné 	}
1694a6aedc5dSRoger Pau Monné 	mtx_unlock(&xs.watch_events_lock);
1695a6aedc5dSRoger Pau Monné 
1696a6aedc5dSRoger Pau Monné 	/* Flush any currently-executing callback, unless we are it. :-) */
1697a6aedc5dSRoger Pau Monné 	if (curproc->p_pid != xs.xenwatch_pid) {
1698a6aedc5dSRoger Pau Monné 		sx_xlock(&xs.xenwatch_mutex);
1699a6aedc5dSRoger Pau Monné 		sx_xunlock(&xs.xenwatch_mutex);
1700a6aedc5dSRoger Pau Monné 	}
1701a6aedc5dSRoger Pau Monné }
1702