xref: /freebsd/sys/dev/xen/xenstore/xenstore.c (revision 7c743c89a068dd36b60dab9a485e9b5195c8e1cc)
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
82453130d9SPedro 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é 	 * The processid of the xenwatch thread.
206a6aedc5dSRoger Pau Monné 	 */
207a6aedc5dSRoger Pau Monné 	pid_t xenwatch_pid;
208a6aedc5dSRoger Pau Monné 
209a6aedc5dSRoger Pau Monné 	/**
210a6aedc5dSRoger Pau Monné 	 * Sleepable mutex used to gate the execution of XenStore
211a6aedc5dSRoger Pau Monné 	 * watch event callbacks.
212a6aedc5dSRoger Pau Monné 	 *
213a6aedc5dSRoger Pau Monné 	 * xenwatch_thread holds an exclusive lock on this mutex
214a6aedc5dSRoger Pau Monné 	 * while delivering event callbacks, and xenstore_unregister_watch()
215a6aedc5dSRoger Pau Monné 	 * uses an exclusive lock of this mutex to guarantee that no
216a6aedc5dSRoger Pau Monné 	 * callbacks of the just unregistered watch are pending
217a6aedc5dSRoger Pau Monné 	 * before returning to its caller.
218a6aedc5dSRoger Pau Monné 	 */
219a6aedc5dSRoger Pau Monné 	struct sx xenwatch_mutex;
220a6aedc5dSRoger Pau Monné 
221a6aedc5dSRoger Pau Monné 	/**
222a6aedc5dSRoger Pau Monné 	 * The HVM guest pseudo-physical frame number.  This is Xen's mapping
223a6aedc5dSRoger Pau Monné 	 * of the true machine frame number into our "physical address space".
224a6aedc5dSRoger Pau Monné 	 */
225a6aedc5dSRoger Pau Monné 	unsigned long gpfn;
226a6aedc5dSRoger Pau Monné 
227a6aedc5dSRoger Pau Monné 	/**
228a6aedc5dSRoger Pau Monné 	 * The event channel for communicating with the
229a6aedc5dSRoger Pau Monné 	 * XenStore service.
230a6aedc5dSRoger Pau Monné 	 */
231a6aedc5dSRoger Pau Monné 	int evtchn;
232a6aedc5dSRoger Pau Monné 
233a6aedc5dSRoger Pau Monné 	/** Handle for XenStore interrupts. */
234a6aedc5dSRoger Pau Monné 	xen_intr_handle_t xen_intr_handle;
235a6aedc5dSRoger Pau Monné 
236a6aedc5dSRoger Pau Monné 	/**
237a6aedc5dSRoger Pau Monné 	 * Interrupt driven config hook allowing us to defer
238a6aedc5dSRoger Pau Monné 	 * attaching children until interrupts (and thus communication
239a6aedc5dSRoger Pau Monné 	 * with the XenStore service) are available.
240a6aedc5dSRoger Pau Monné 	 */
241a6aedc5dSRoger Pau Monné 	struct intr_config_hook xs_attachcb;
2421d84e2b3SRoger Pau Monné 
2431d84e2b3SRoger Pau Monné 	/**
2441d84e2b3SRoger Pau Monné 	 * Xenstore is a user-space process that usually runs in Dom0,
2451d84e2b3SRoger Pau Monné 	 * so if this domain is booting as Dom0, xenstore wont we accessible,
2461d84e2b3SRoger Pau Monné 	 * and we have to defer the initialization of xenstore related
2471d84e2b3SRoger Pau Monné 	 * devices to later (when xenstore is started).
2481d84e2b3SRoger Pau Monné 	 */
2491d84e2b3SRoger Pau Monné 	bool initialized;
2501d84e2b3SRoger Pau Monné 
2511d84e2b3SRoger Pau Monné 	/**
2521d84e2b3SRoger Pau Monné 	 * Task to run when xenstore is initialized (Dom0 only), will
2531d84e2b3SRoger Pau Monné 	 * take care of attaching xenstore related devices.
2541d84e2b3SRoger Pau Monné 	 */
2551d84e2b3SRoger Pau Monné 	struct task xs_late_init;
256a6aedc5dSRoger Pau Monné };
257a6aedc5dSRoger Pau Monné 
258a6aedc5dSRoger Pau Monné /*-------------------------------- Global Data ------------------------------*/
259a6aedc5dSRoger Pau Monné static struct xs_softc xs;
260a6aedc5dSRoger Pau Monné 
261a6aedc5dSRoger Pau Monné /*------------------------- Private Utility Functions -----------------------*/
262a6aedc5dSRoger Pau Monné 
263a6aedc5dSRoger Pau Monné /**
264a6aedc5dSRoger Pau Monné  * Count and optionally record pointers to a number of NUL terminated
265a6aedc5dSRoger Pau Monné  * strings in a buffer.
266a6aedc5dSRoger Pau Monné  *
267a6aedc5dSRoger Pau Monné  * \param strings  A pointer to a contiguous buffer of NUL terminated strings.
268a6aedc5dSRoger Pau Monné  * \param dest	   An array to store pointers to each string found in strings.
269a6aedc5dSRoger Pau Monné  * \param len	   The length of the buffer pointed to by strings.
270a6aedc5dSRoger Pau Monné  *
271a6aedc5dSRoger Pau Monné  * \return  A count of the number of strings found.
272a6aedc5dSRoger Pau Monné  */
273a6aedc5dSRoger Pau Monné static u_int
274a6aedc5dSRoger Pau Monné extract_strings(const char *strings, const char **dest, u_int len)
275a6aedc5dSRoger Pau Monné {
276a6aedc5dSRoger Pau Monné 	u_int num;
277a6aedc5dSRoger Pau Monné 	const char *p;
278a6aedc5dSRoger Pau Monné 
279a6aedc5dSRoger Pau Monné 	for (p = strings, num = 0; p < strings + len; p += strlen(p) + 1) {
280a6aedc5dSRoger Pau Monné 		if (dest != NULL)
281a6aedc5dSRoger Pau Monné 			*dest++ = p;
282a6aedc5dSRoger Pau Monné 		num++;
283a6aedc5dSRoger Pau Monné 	}
284a6aedc5dSRoger Pau Monné 
285a6aedc5dSRoger Pau Monné 	return (num);
286a6aedc5dSRoger Pau Monné }
287a6aedc5dSRoger Pau Monné 
288a6aedc5dSRoger Pau Monné /**
289a6aedc5dSRoger Pau Monné  * Convert a contiguous buffer containing a series of NUL terminated
290a6aedc5dSRoger Pau Monné  * strings into an array of pointers to strings.
291a6aedc5dSRoger Pau Monné  *
292a6aedc5dSRoger Pau Monné  * The returned pointer references the array of string pointers which
293a6aedc5dSRoger Pau Monné  * is followed by the storage for the string data.  It is the client's
294a6aedc5dSRoger Pau Monné  * responsibility to free this storage.
295a6aedc5dSRoger Pau Monné  *
296a6aedc5dSRoger Pau Monné  * The storage addressed by strings is free'd prior to split returning.
297a6aedc5dSRoger Pau Monné  *
298a6aedc5dSRoger Pau Monné  * \param strings  A pointer to a contiguous buffer of NUL terminated strings.
299a6aedc5dSRoger Pau Monné  * \param len	   The length of the buffer pointed to by strings.
300a6aedc5dSRoger Pau Monné  * \param num	   The number of strings found and returned in the strings
301a6aedc5dSRoger Pau Monné  *                 array.
302a6aedc5dSRoger Pau Monné  *
303a6aedc5dSRoger Pau Monné  * \return  An array of pointers to the strings found in the input buffer.
304a6aedc5dSRoger Pau Monné  */
305a6aedc5dSRoger Pau Monné static const char **
306a6aedc5dSRoger Pau Monné split(char *strings, u_int len, u_int *num)
307a6aedc5dSRoger Pau Monné {
308a6aedc5dSRoger Pau Monné 	const char **ret;
309a6aedc5dSRoger Pau Monné 
310a6aedc5dSRoger Pau Monné 	/* Protect against unterminated buffers. */
311a6aedc5dSRoger Pau Monné 	if (len > 0)
312a6aedc5dSRoger Pau Monné 		strings[len - 1] = '\0';
313a6aedc5dSRoger Pau Monné 
314a6aedc5dSRoger Pau Monné 	/* Count the strings. */
315a6aedc5dSRoger Pau Monné 	*num = extract_strings(strings, /*dest*/NULL, len);
316a6aedc5dSRoger Pau Monné 
317a6aedc5dSRoger Pau Monné 	/* Transfer to one big alloc for easy freeing by the caller. */
318a6aedc5dSRoger Pau Monné 	ret = malloc(*num * sizeof(char *) + len, M_XENSTORE, M_WAITOK);
319a6aedc5dSRoger Pau Monné 	memcpy(&ret[*num], strings, len);
320a6aedc5dSRoger Pau Monné 	free(strings, M_XENSTORE);
321a6aedc5dSRoger Pau Monné 
322a6aedc5dSRoger Pau Monné 	/* Extract pointers to newly allocated array. */
323a6aedc5dSRoger Pau Monné 	strings = (char *)&ret[*num];
324a6aedc5dSRoger Pau Monné 	(void)extract_strings(strings, /*dest*/ret, len);
325a6aedc5dSRoger Pau Monné 
326a6aedc5dSRoger Pau Monné 	return (ret);
327a6aedc5dSRoger Pau Monné }
328a6aedc5dSRoger Pau Monné 
329a6aedc5dSRoger Pau Monné /*------------------------- Public Utility Functions -------------------------*/
330a6aedc5dSRoger Pau Monné /*------- API comments for these methods can be found in xenstorevar.h -------*/
331a6aedc5dSRoger Pau Monné struct sbuf *
332a6aedc5dSRoger Pau Monné xs_join(const char *dir, const char *name)
333a6aedc5dSRoger Pau Monné {
334a6aedc5dSRoger Pau Monné 	struct sbuf *sb;
335a6aedc5dSRoger Pau Monné 
336a6aedc5dSRoger Pau Monné 	sb = sbuf_new_auto();
337a6aedc5dSRoger Pau Monné 	sbuf_cat(sb, dir);
338a6aedc5dSRoger Pau Monné 	if (name[0] != '\0') {
339a6aedc5dSRoger Pau Monné 		sbuf_putc(sb, '/');
340a6aedc5dSRoger Pau Monné 		sbuf_cat(sb, name);
341a6aedc5dSRoger Pau Monné 	}
342a6aedc5dSRoger Pau Monné 	sbuf_finish(sb);
343a6aedc5dSRoger Pau Monné 
344a6aedc5dSRoger Pau Monné 	return (sb);
345a6aedc5dSRoger Pau Monné }
346a6aedc5dSRoger Pau Monné 
347a6aedc5dSRoger Pau Monné /*-------------------- Low Level Communication Management --------------------*/
348a6aedc5dSRoger Pau Monné /**
349a6aedc5dSRoger Pau Monné  * Interrupt handler for the XenStore event channel.
350a6aedc5dSRoger Pau Monné  *
351a6aedc5dSRoger Pau Monné  * XenStore reads and writes block on "xen_store" for buffer
352a6aedc5dSRoger Pau Monné  * space.  Wakeup any blocking operations when the XenStore
353a6aedc5dSRoger Pau Monné  * service has modified the queues.
354a6aedc5dSRoger Pau Monné  */
355a6aedc5dSRoger Pau Monné static void
356a6aedc5dSRoger Pau Monné xs_intr(void * arg __unused /*__attribute__((unused))*/)
357a6aedc5dSRoger Pau Monné {
358a6aedc5dSRoger Pau Monné 
3591d84e2b3SRoger Pau Monné 	/* If xenstore has not been initialized, initialize it now */
3601d84e2b3SRoger Pau Monné 	if (!xs.initialized) {
3611d84e2b3SRoger Pau Monné 		xs.initialized = true;
3621d84e2b3SRoger Pau Monné 		/*
3631d84e2b3SRoger Pau Monné 		 * Since this task is probing and attaching devices we
3641d84e2b3SRoger Pau Monné 		 * have to hold the Giant lock.
3651d84e2b3SRoger Pau Monné 		 */
3661d84e2b3SRoger Pau Monné 		taskqueue_enqueue(taskqueue_swi_giant, &xs.xs_late_init);
3671d84e2b3SRoger Pau Monné 	}
3681d84e2b3SRoger Pau Monné 
369a6aedc5dSRoger Pau Monné 	/*
370a6aedc5dSRoger Pau Monné 	 * Hold ring lock across wakeup so that clients
371a6aedc5dSRoger Pau Monné 	 * cannot miss a wakeup.
372a6aedc5dSRoger Pau Monné 	 */
373a6aedc5dSRoger Pau Monné 	mtx_lock(&xs.ring_lock);
374a6aedc5dSRoger Pau Monné 	wakeup(xen_store);
375a6aedc5dSRoger Pau Monné 	mtx_unlock(&xs.ring_lock);
376a6aedc5dSRoger Pau Monné }
377a6aedc5dSRoger Pau Monné 
378a6aedc5dSRoger Pau Monné /**
379a6aedc5dSRoger Pau Monné  * Verify that the indexes for a ring are valid.
380a6aedc5dSRoger Pau Monné  *
381a6aedc5dSRoger Pau Monné  * The difference between the producer and consumer cannot
382a6aedc5dSRoger Pau Monné  * exceed the size of the ring.
383a6aedc5dSRoger Pau Monné  *
384a6aedc5dSRoger Pau Monné  * \param cons  The consumer index for the ring to test.
385a6aedc5dSRoger Pau Monné  * \param prod  The producer index for the ring to test.
386a6aedc5dSRoger Pau Monné  *
387a6aedc5dSRoger Pau Monné  * \retval 1  If indexes are in range.
388a6aedc5dSRoger Pau Monné  * \retval 0  If the indexes are out of range.
389a6aedc5dSRoger Pau Monné  */
390a6aedc5dSRoger Pau Monné static int
391a6aedc5dSRoger Pau Monné xs_check_indexes(XENSTORE_RING_IDX cons, XENSTORE_RING_IDX prod)
392a6aedc5dSRoger Pau Monné {
393a6aedc5dSRoger Pau Monné 
394a6aedc5dSRoger Pau Monné 	return ((prod - cons) <= XENSTORE_RING_SIZE);
395a6aedc5dSRoger Pau Monné }
396a6aedc5dSRoger Pau Monné 
397a6aedc5dSRoger Pau Monné /**
398a6aedc5dSRoger Pau Monné  * Return a pointer to, and the length of, the contiguous
399a6aedc5dSRoger Pau Monné  * free region available for output in a ring buffer.
400a6aedc5dSRoger Pau Monné  *
401a6aedc5dSRoger Pau Monné  * \param cons  The consumer index for the ring.
402a6aedc5dSRoger Pau Monné  * \param prod  The producer index for the ring.
403a6aedc5dSRoger Pau Monné  * \param buf   The base address of the ring's storage.
404a6aedc5dSRoger Pau Monné  * \param len   The amount of contiguous storage available.
405a6aedc5dSRoger Pau Monné  *
406a6aedc5dSRoger Pau Monné  * \return  A pointer to the start location of the free region.
407a6aedc5dSRoger Pau Monné  */
408a6aedc5dSRoger Pau Monné static void *
409a6aedc5dSRoger Pau Monné xs_get_output_chunk(XENSTORE_RING_IDX cons, XENSTORE_RING_IDX prod,
410a6aedc5dSRoger Pau Monné     char *buf, uint32_t *len)
411a6aedc5dSRoger Pau Monné {
412a6aedc5dSRoger Pau Monné 
413a6aedc5dSRoger Pau Monné 	*len = XENSTORE_RING_SIZE - MASK_XENSTORE_IDX(prod);
414a6aedc5dSRoger Pau Monné 	if ((XENSTORE_RING_SIZE - (prod - cons)) < *len)
415a6aedc5dSRoger Pau Monné 		*len = XENSTORE_RING_SIZE - (prod - cons);
416a6aedc5dSRoger Pau Monné 	return (buf + MASK_XENSTORE_IDX(prod));
417a6aedc5dSRoger Pau Monné }
418a6aedc5dSRoger Pau Monné 
419a6aedc5dSRoger Pau Monné /**
420a6aedc5dSRoger Pau Monné  * Return a pointer to, and the length of, the contiguous
421a6aedc5dSRoger Pau Monné  * data available to read from a ring buffer.
422a6aedc5dSRoger Pau Monné  *
423a6aedc5dSRoger Pau Monné  * \param cons  The consumer index for the ring.
424a6aedc5dSRoger Pau Monné  * \param prod  The producer index for the ring.
425a6aedc5dSRoger Pau Monné  * \param buf   The base address of the ring's storage.
426a6aedc5dSRoger Pau Monné  * \param len   The amount of contiguous data available to read.
427a6aedc5dSRoger Pau Monné  *
428a6aedc5dSRoger Pau Monné  * \return  A pointer to the start location of the available data.
429a6aedc5dSRoger Pau Monné  */
430a6aedc5dSRoger Pau Monné static const void *
431a6aedc5dSRoger Pau Monné xs_get_input_chunk(XENSTORE_RING_IDX cons, XENSTORE_RING_IDX prod,
432a6aedc5dSRoger Pau Monné     const char *buf, uint32_t *len)
433a6aedc5dSRoger Pau Monné {
434a6aedc5dSRoger Pau Monné 
435a6aedc5dSRoger Pau Monné 	*len = XENSTORE_RING_SIZE - MASK_XENSTORE_IDX(cons);
436a6aedc5dSRoger Pau Monné 	if ((prod - cons) < *len)
437a6aedc5dSRoger Pau Monné 		*len = prod - cons;
438a6aedc5dSRoger Pau Monné 	return (buf + MASK_XENSTORE_IDX(cons));
439a6aedc5dSRoger Pau Monné }
440a6aedc5dSRoger Pau Monné 
441a6aedc5dSRoger Pau Monné /**
442a6aedc5dSRoger Pau Monné  * Transmit data to the XenStore service.
443a6aedc5dSRoger Pau Monné  *
444a6aedc5dSRoger Pau Monné  * \param tdata  A pointer to the contiguous data to send.
445a6aedc5dSRoger Pau Monné  * \param len    The amount of data to send.
446a6aedc5dSRoger Pau Monné  *
447a6aedc5dSRoger Pau Monné  * \return  On success 0, otherwise an errno value indicating the
448a6aedc5dSRoger Pau Monné  *          cause of failure.
449a6aedc5dSRoger Pau Monné  *
450a6aedc5dSRoger Pau Monné  * \invariant  Called from thread context.
451a6aedc5dSRoger Pau Monné  * \invariant  The buffer pointed to by tdata is at least len bytes
452a6aedc5dSRoger Pau Monné  *             in length.
453a6aedc5dSRoger Pau Monné  * \invariant  xs.request_mutex exclusively locked.
454a6aedc5dSRoger Pau Monné  */
455a6aedc5dSRoger Pau Monné static int
456a6aedc5dSRoger Pau Monné xs_write_store(const void *tdata, unsigned len)
457a6aedc5dSRoger Pau Monné {
458a6aedc5dSRoger Pau Monné 	XENSTORE_RING_IDX cons, prod;
459a6aedc5dSRoger Pau Monné 	const char *data = (const char *)tdata;
460a6aedc5dSRoger Pau Monné 	int error;
461a6aedc5dSRoger Pau Monné 
462a6aedc5dSRoger Pau Monné 	sx_assert(&xs.request_mutex, SX_XLOCKED);
463a6aedc5dSRoger Pau Monné 	while (len != 0) {
464a6aedc5dSRoger Pau Monné 		void *dst;
465a6aedc5dSRoger Pau Monné 		u_int avail;
466a6aedc5dSRoger Pau Monné 
467a6aedc5dSRoger Pau Monné 		/* Hold lock so we can't miss wakeups should we block. */
468a6aedc5dSRoger Pau Monné 		mtx_lock(&xs.ring_lock);
469a6aedc5dSRoger Pau Monné 		cons = xen_store->req_cons;
470a6aedc5dSRoger Pau Monné 		prod = xen_store->req_prod;
471a6aedc5dSRoger Pau Monné 		if ((prod - cons) == XENSTORE_RING_SIZE) {
472a6aedc5dSRoger Pau Monné 			/*
473a6aedc5dSRoger Pau Monné 			 * Output ring is full. Wait for a ring event.
474a6aedc5dSRoger Pau Monné 			 *
475a6aedc5dSRoger Pau Monné 			 * Note that the events from both queues
476a6aedc5dSRoger Pau Monné 			 * are combined, so being woken does not
477a6aedc5dSRoger Pau Monné 			 * guarantee that data exist in the read
478a6aedc5dSRoger Pau Monné 			 * ring.
479a6aedc5dSRoger Pau Monné 			 *
480a6aedc5dSRoger Pau Monné 			 * To simplify error recovery and the retry,
481a6aedc5dSRoger Pau Monné 			 * we specify PDROP so our lock is *not* held
482a6aedc5dSRoger Pau Monné 			 * when msleep returns.
483a6aedc5dSRoger Pau Monné 			 */
484a6aedc5dSRoger Pau Monné 			error = msleep(xen_store, &xs.ring_lock, PCATCH|PDROP,
485a6aedc5dSRoger Pau Monné 			     "xbwrite", /*timeout*/0);
486a6aedc5dSRoger Pau Monné 			if (error && error != EWOULDBLOCK)
487a6aedc5dSRoger Pau Monné 				return (error);
488a6aedc5dSRoger Pau Monné 
489a6aedc5dSRoger Pau Monné 			/* Try again. */
490a6aedc5dSRoger Pau Monné 			continue;
491a6aedc5dSRoger Pau Monné 		}
492a6aedc5dSRoger Pau Monné 		mtx_unlock(&xs.ring_lock);
493a6aedc5dSRoger Pau Monné 
494a6aedc5dSRoger Pau Monné 		/* Verify queue sanity. */
495a6aedc5dSRoger Pau Monné 		if (!xs_check_indexes(cons, prod)) {
496a6aedc5dSRoger Pau Monné 			xen_store->req_cons = xen_store->req_prod = 0;
497a6aedc5dSRoger Pau Monné 			return (EIO);
498a6aedc5dSRoger Pau Monné 		}
499a6aedc5dSRoger Pau Monné 
500a6aedc5dSRoger Pau Monné 		dst = xs_get_output_chunk(cons, prod, xen_store->req, &avail);
501a6aedc5dSRoger Pau Monné 		if (avail > len)
502a6aedc5dSRoger Pau Monné 			avail = len;
503a6aedc5dSRoger Pau Monné 
504a6aedc5dSRoger Pau Monné 		memcpy(dst, data, avail);
505a6aedc5dSRoger Pau Monné 		data += avail;
506a6aedc5dSRoger Pau Monné 		len -= avail;
507a6aedc5dSRoger Pau Monné 
508a6aedc5dSRoger Pau Monné 		/*
509a6aedc5dSRoger Pau Monné 		 * The store to the producer index, which indicates
510a6aedc5dSRoger Pau Monné 		 * to the other side that new data has arrived, must
511a6aedc5dSRoger Pau Monné 		 * be visible only after our copy of the data into the
512a6aedc5dSRoger Pau Monné 		 * ring has completed.
513a6aedc5dSRoger Pau Monné 		 */
514a6aedc5dSRoger Pau Monné 		wmb();
515a6aedc5dSRoger Pau Monné 		xen_store->req_prod += avail;
516a6aedc5dSRoger Pau Monné 
517a6aedc5dSRoger Pau Monné 		/*
518a6aedc5dSRoger Pau Monné 		 * xen_intr_signal() implies mb(). The other side will see
519a6aedc5dSRoger Pau Monné 		 * the change to req_prod at the time of the interrupt.
520a6aedc5dSRoger Pau Monné 		 */
521a6aedc5dSRoger Pau Monné 		xen_intr_signal(xs.xen_intr_handle);
522a6aedc5dSRoger Pau Monné 	}
523a6aedc5dSRoger Pau Monné 
524a6aedc5dSRoger Pau Monné 	return (0);
525a6aedc5dSRoger Pau Monné }
526a6aedc5dSRoger Pau Monné 
527a6aedc5dSRoger Pau Monné /**
528a6aedc5dSRoger Pau Monné  * Receive data from the XenStore service.
529a6aedc5dSRoger Pau Monné  *
530a6aedc5dSRoger Pau Monné  * \param tdata  A pointer to the contiguous buffer to receive the data.
531a6aedc5dSRoger Pau Monné  * \param len    The amount of data to receive.
532a6aedc5dSRoger Pau Monné  *
533a6aedc5dSRoger Pau Monné  * \return  On success 0, otherwise an errno value indicating the
534a6aedc5dSRoger Pau Monné  *          cause of failure.
535a6aedc5dSRoger Pau Monné  *
536a6aedc5dSRoger Pau Monné  * \invariant  Called from thread context.
537a6aedc5dSRoger Pau Monné  * \invariant  The buffer pointed to by tdata is at least len bytes
538a6aedc5dSRoger Pau Monné  *             in length.
539a6aedc5dSRoger Pau Monné  *
540a6aedc5dSRoger Pau Monné  * \note xs_read does not perform any internal locking to guarantee
541a6aedc5dSRoger Pau Monné  *       serial access to the incoming ring buffer.  However, there
542a6aedc5dSRoger Pau Monné  *	 is only one context processing reads: xs_rcv_thread().
543a6aedc5dSRoger Pau Monné  */
544a6aedc5dSRoger Pau Monné static int
545a6aedc5dSRoger Pau Monné xs_read_store(void *tdata, unsigned len)
546a6aedc5dSRoger Pau Monné {
547a6aedc5dSRoger Pau Monné 	XENSTORE_RING_IDX cons, prod;
548a6aedc5dSRoger Pau Monné 	char *data = (char *)tdata;
549a6aedc5dSRoger Pau Monné 	int error;
550a6aedc5dSRoger Pau Monné 
551a6aedc5dSRoger Pau Monné 	while (len != 0) {
552a6aedc5dSRoger Pau Monné 		u_int avail;
553a6aedc5dSRoger Pau Monné 		const char *src;
554a6aedc5dSRoger Pau Monné 
555a6aedc5dSRoger Pau Monné 		/* Hold lock so we can't miss wakeups should we block. */
556a6aedc5dSRoger Pau Monné 		mtx_lock(&xs.ring_lock);
557a6aedc5dSRoger Pau Monné 		cons = xen_store->rsp_cons;
558a6aedc5dSRoger Pau Monné 		prod = xen_store->rsp_prod;
559a6aedc5dSRoger Pau Monné 		if (cons == prod) {
560a6aedc5dSRoger Pau Monné 			/*
561a6aedc5dSRoger Pau Monné 			 * Nothing to read. Wait for a ring event.
562a6aedc5dSRoger Pau Monné 			 *
563a6aedc5dSRoger Pau Monné 			 * Note that the events from both queues
564a6aedc5dSRoger Pau Monné 			 * are combined, so being woken does not
565a6aedc5dSRoger Pau Monné 			 * guarantee that data exist in the read
566a6aedc5dSRoger Pau Monné 			 * ring.
567a6aedc5dSRoger Pau Monné 			 *
568a6aedc5dSRoger Pau Monné 			 * To simplify error recovery and the retry,
569a6aedc5dSRoger Pau Monné 			 * we specify PDROP so our lock is *not* held
570a6aedc5dSRoger Pau Monné 			 * when msleep returns.
571a6aedc5dSRoger Pau Monné 			 */
572a6aedc5dSRoger Pau Monné 			error = msleep(xen_store, &xs.ring_lock, PCATCH|PDROP,
573a6aedc5dSRoger Pau Monné 			    "xbread", /*timeout*/0);
574a6aedc5dSRoger Pau Monné 			if (error && error != EWOULDBLOCK)
575a6aedc5dSRoger Pau Monné 				return (error);
576a6aedc5dSRoger Pau Monné 			continue;
577a6aedc5dSRoger Pau Monné 		}
578a6aedc5dSRoger Pau Monné 		mtx_unlock(&xs.ring_lock);
579a6aedc5dSRoger Pau Monné 
580a6aedc5dSRoger Pau Monné 		/* Verify queue sanity. */
581a6aedc5dSRoger Pau Monné 		if (!xs_check_indexes(cons, prod)) {
582a6aedc5dSRoger Pau Monné 			xen_store->rsp_cons = xen_store->rsp_prod = 0;
583a6aedc5dSRoger Pau Monné 			return (EIO);
584a6aedc5dSRoger Pau Monné 		}
585a6aedc5dSRoger Pau Monné 
586a6aedc5dSRoger Pau Monné 		src = xs_get_input_chunk(cons, prod, xen_store->rsp, &avail);
587a6aedc5dSRoger Pau Monné 		if (avail > len)
588a6aedc5dSRoger Pau Monné 			avail = len;
589a6aedc5dSRoger Pau Monné 
590a6aedc5dSRoger Pau Monné 		/*
591a6aedc5dSRoger Pau Monné 		 * Insure the data we read is related to the indexes
592a6aedc5dSRoger Pau Monné 		 * we read above.
593a6aedc5dSRoger Pau Monné 		 */
594a6aedc5dSRoger Pau Monné 		rmb();
595a6aedc5dSRoger Pau Monné 
596a6aedc5dSRoger Pau Monné 		memcpy(data, src, avail);
597a6aedc5dSRoger Pau Monné 		data += avail;
598a6aedc5dSRoger Pau Monné 		len -= avail;
599a6aedc5dSRoger Pau Monné 
600a6aedc5dSRoger Pau Monné 		/*
601a6aedc5dSRoger Pau Monné 		 * Insure that the producer of this ring does not see
602a6aedc5dSRoger Pau Monné 		 * the ring space as free until after we have copied it
603a6aedc5dSRoger Pau Monné 		 * out.
604a6aedc5dSRoger Pau Monné 		 */
605a6aedc5dSRoger Pau Monné 		mb();
606a6aedc5dSRoger Pau Monné 		xen_store->rsp_cons += avail;
607a6aedc5dSRoger Pau Monné 
608a6aedc5dSRoger Pau Monné 		/*
609a6aedc5dSRoger Pau Monné 		 * xen_intr_signal() implies mb(). The producer will see
610a6aedc5dSRoger Pau Monné 		 * the updated consumer index when the event is delivered.
611a6aedc5dSRoger Pau Monné 		 */
612a6aedc5dSRoger Pau Monné 		xen_intr_signal(xs.xen_intr_handle);
613a6aedc5dSRoger Pau Monné 	}
614a6aedc5dSRoger Pau Monné 
615a6aedc5dSRoger Pau Monné 	return (0);
616a6aedc5dSRoger Pau Monné }
617a6aedc5dSRoger Pau Monné 
618a6aedc5dSRoger Pau Monné /*----------------------- Received Message Processing ------------------------*/
619a6aedc5dSRoger Pau Monné /**
620a6aedc5dSRoger Pau Monné  * Block reading the next message from the XenStore service and
621a6aedc5dSRoger Pau Monné  * process the result.
622a6aedc5dSRoger Pau Monné  *
623a6aedc5dSRoger Pau Monné  * \param type  The returned type of the XenStore message received.
624a6aedc5dSRoger Pau Monné  *
625a6aedc5dSRoger Pau Monné  * \return  0 on success.  Otherwise an errno value indicating the
626a6aedc5dSRoger Pau Monné  *          type of failure encountered.
627a6aedc5dSRoger Pau Monné  */
628a6aedc5dSRoger Pau Monné static int
629a6aedc5dSRoger Pau Monné xs_process_msg(enum xsd_sockmsg_type *type)
630a6aedc5dSRoger Pau Monné {
631a6aedc5dSRoger Pau Monné 	struct xs_stored_msg *msg;
632a6aedc5dSRoger Pau Monné 	char *body;
633a6aedc5dSRoger Pau Monné 	int error;
634a6aedc5dSRoger Pau Monné 
635a6aedc5dSRoger Pau Monné 	msg = malloc(sizeof(*msg), M_XENSTORE, M_WAITOK);
636a6aedc5dSRoger Pau Monné 	error = xs_read_store(&msg->hdr, sizeof(msg->hdr));
637a6aedc5dSRoger Pau Monné 	if (error) {
638a6aedc5dSRoger Pau Monné 		free(msg, M_XENSTORE);
639a6aedc5dSRoger Pau Monné 		return (error);
640a6aedc5dSRoger Pau Monné 	}
641a6aedc5dSRoger Pau Monné 
642a6aedc5dSRoger Pau Monné 	body = malloc(msg->hdr.len + 1, M_XENSTORE, M_WAITOK);
643a6aedc5dSRoger Pau Monné 	error = xs_read_store(body, msg->hdr.len);
644a6aedc5dSRoger Pau Monné 	if (error) {
645a6aedc5dSRoger Pau Monné 		free(body, M_XENSTORE);
646a6aedc5dSRoger Pau Monné 		free(msg, M_XENSTORE);
647a6aedc5dSRoger Pau Monné 		return (error);
648a6aedc5dSRoger Pau Monné 	}
649a6aedc5dSRoger Pau Monné 	body[msg->hdr.len] = '\0';
650a6aedc5dSRoger Pau Monné 
651a6aedc5dSRoger Pau Monné 	*type = msg->hdr.type;
652a6aedc5dSRoger Pau Monné 	if (msg->hdr.type == XS_WATCH_EVENT) {
653a6aedc5dSRoger Pau Monné 		msg->u.watch.vec = split(body, msg->hdr.len,
654a6aedc5dSRoger Pau Monné 		    &msg->u.watch.vec_size);
655a6aedc5dSRoger Pau Monné 
656a6aedc5dSRoger Pau Monné 		mtx_lock(&xs.registered_watches_lock);
657a6aedc5dSRoger Pau Monné 		msg->u.watch.handle = find_watch(
658a6aedc5dSRoger Pau Monné 		    msg->u.watch.vec[XS_WATCH_TOKEN]);
659a6aedc5dSRoger Pau Monné 		if (msg->u.watch.handle != NULL) {
660a6aedc5dSRoger Pau Monné 			mtx_lock(&xs.watch_events_lock);
661a6aedc5dSRoger Pau Monné 			TAILQ_INSERT_TAIL(&xs.watch_events, msg, list);
662a6aedc5dSRoger Pau Monné 			wakeup(&xs.watch_events);
663a6aedc5dSRoger Pau Monné 			mtx_unlock(&xs.watch_events_lock);
664a6aedc5dSRoger Pau Monné 		} else {
665a6aedc5dSRoger Pau Monné 			free(msg->u.watch.vec, M_XENSTORE);
666a6aedc5dSRoger Pau Monné 			free(msg, M_XENSTORE);
667a6aedc5dSRoger Pau Monné 		}
668a6aedc5dSRoger Pau Monné 		mtx_unlock(&xs.registered_watches_lock);
669a6aedc5dSRoger Pau Monné 	} else {
670a6aedc5dSRoger Pau Monné 		msg->u.reply.body = body;
671a6aedc5dSRoger Pau Monné 		mtx_lock(&xs.reply_lock);
672a6aedc5dSRoger Pau Monné 		TAILQ_INSERT_TAIL(&xs.reply_list, msg, list);
673a6aedc5dSRoger Pau Monné 		wakeup(&xs.reply_list);
674a6aedc5dSRoger Pau Monné 		mtx_unlock(&xs.reply_lock);
675a6aedc5dSRoger Pau Monné 	}
676a6aedc5dSRoger Pau Monné 
677a6aedc5dSRoger Pau Monné 	return (0);
678a6aedc5dSRoger Pau Monné }
679a6aedc5dSRoger Pau Monné 
680a6aedc5dSRoger Pau Monné /**
681a6aedc5dSRoger Pau Monné  * Thread body of the XenStore receive thread.
682a6aedc5dSRoger Pau Monné  *
683a6aedc5dSRoger Pau Monné  * This thread blocks waiting for data from the XenStore service
684a6aedc5dSRoger Pau Monné  * and processes and received messages.
685a6aedc5dSRoger Pau Monné  */
686a6aedc5dSRoger Pau Monné static void
687a6aedc5dSRoger Pau Monné xs_rcv_thread(void *arg __unused)
688a6aedc5dSRoger Pau Monné {
689a6aedc5dSRoger Pau Monné 	int error;
690a6aedc5dSRoger Pau Monné 	enum xsd_sockmsg_type type;
691a6aedc5dSRoger Pau Monné 
692a6aedc5dSRoger Pau Monné 	for (;;) {
693a6aedc5dSRoger Pau Monné 		error = xs_process_msg(&type);
694a6aedc5dSRoger Pau Monné 		if (error)
695a6aedc5dSRoger Pau Monné 			printf("XENSTORE error %d while reading message\n",
696a6aedc5dSRoger Pau Monné 			    error);
697a6aedc5dSRoger Pau Monné 	}
698a6aedc5dSRoger Pau Monné }
699a6aedc5dSRoger Pau Monné 
700a6aedc5dSRoger Pau Monné /*---------------- XenStore Message Request/Reply Processing -----------------*/
701a6aedc5dSRoger Pau Monné #define xsd_error_count	(sizeof(xsd_errors) / sizeof(xsd_errors[0]))
702a6aedc5dSRoger Pau Monné 
703a6aedc5dSRoger Pau Monné /**
704a6aedc5dSRoger Pau Monné  * Convert a XenStore error string into an errno number.
705a6aedc5dSRoger Pau Monné  *
706a6aedc5dSRoger Pau Monné  * \param errorstring  The error string to convert.
707a6aedc5dSRoger Pau Monné  *
708a6aedc5dSRoger Pau Monné  * \return  The errno best matching the input string.
709a6aedc5dSRoger Pau Monné  *
710a6aedc5dSRoger Pau Monné  * \note Unknown error strings are converted to EINVAL.
711a6aedc5dSRoger Pau Monné  */
712a6aedc5dSRoger Pau Monné static int
713a6aedc5dSRoger Pau Monné xs_get_error(const char *errorstring)
714a6aedc5dSRoger Pau Monné {
715a6aedc5dSRoger Pau Monné 	u_int i;
716a6aedc5dSRoger Pau Monné 
717a6aedc5dSRoger Pau Monné 	for (i = 0; i < xsd_error_count; i++) {
718a6aedc5dSRoger Pau Monné 		if (!strcmp(errorstring, xsd_errors[i].errstring))
719a6aedc5dSRoger Pau Monné 			return (xsd_errors[i].errnum);
720a6aedc5dSRoger Pau Monné 	}
721a6aedc5dSRoger Pau Monné 	log(LOG_WARNING, "XENSTORE xen store gave: unknown error %s",
722a6aedc5dSRoger Pau Monné 	    errorstring);
723a6aedc5dSRoger Pau Monné 	return (EINVAL);
724a6aedc5dSRoger Pau Monné }
725a6aedc5dSRoger Pau Monné 
726a6aedc5dSRoger Pau Monné /**
727a6aedc5dSRoger Pau Monné  * Block waiting for a reply to a message request.
728a6aedc5dSRoger Pau Monné  *
729a6aedc5dSRoger Pau Monné  * \param type	  The returned type of the reply.
730a6aedc5dSRoger Pau Monné  * \param len	  The returned body length of the reply.
731a6aedc5dSRoger Pau Monné  * \param result  The returned body of the reply.
732a6aedc5dSRoger Pau Monné  *
733a6aedc5dSRoger Pau Monné  * \return  0 on success.  Otherwise an errno indicating the
734a6aedc5dSRoger Pau Monné  *          cause of failure.
735a6aedc5dSRoger Pau Monné  */
736a6aedc5dSRoger Pau Monné static int
737a6aedc5dSRoger Pau Monné xs_read_reply(enum xsd_sockmsg_type *type, u_int *len, void **result)
738a6aedc5dSRoger Pau Monné {
739a6aedc5dSRoger Pau Monné 	struct xs_stored_msg *msg;
740a6aedc5dSRoger Pau Monné 	char *body;
741a6aedc5dSRoger Pau Monné 	int error;
742a6aedc5dSRoger Pau Monné 
743a6aedc5dSRoger Pau Monné 	mtx_lock(&xs.reply_lock);
744a6aedc5dSRoger Pau Monné 	while (TAILQ_EMPTY(&xs.reply_list)) {
745*7c743c89SRoger Pau Monné 		error = mtx_sleep(&xs.reply_list, &xs.reply_lock, 0, "xswait",
746*7c743c89SRoger Pau Monné 		    hz/10);
747a6aedc5dSRoger Pau Monné 		if (error && error != EWOULDBLOCK) {
748a6aedc5dSRoger Pau Monné 			mtx_unlock(&xs.reply_lock);
749a6aedc5dSRoger Pau Monné 			return (error);
750a6aedc5dSRoger Pau Monné 		}
751a6aedc5dSRoger Pau Monné 	}
752a6aedc5dSRoger Pau Monné 	msg = TAILQ_FIRST(&xs.reply_list);
753a6aedc5dSRoger Pau Monné 	TAILQ_REMOVE(&xs.reply_list, msg, list);
754a6aedc5dSRoger Pau Monné 	mtx_unlock(&xs.reply_lock);
755a6aedc5dSRoger Pau Monné 
756a6aedc5dSRoger Pau Monné 	*type = msg->hdr.type;
757a6aedc5dSRoger Pau Monné 	if (len)
758a6aedc5dSRoger Pau Monné 		*len = msg->hdr.len;
759a6aedc5dSRoger Pau Monné 	body = msg->u.reply.body;
760a6aedc5dSRoger Pau Monné 
761a6aedc5dSRoger Pau Monné 	free(msg, M_XENSTORE);
762a6aedc5dSRoger Pau Monné 	*result = body;
763a6aedc5dSRoger Pau Monné 	return (0);
764a6aedc5dSRoger Pau Monné }
765a6aedc5dSRoger Pau Monné 
766a6aedc5dSRoger Pau Monné /**
767a6aedc5dSRoger Pau Monné  * Pass-thru interface for XenStore access by userland processes
768a6aedc5dSRoger Pau Monné  * via the XenStore device.
769a6aedc5dSRoger Pau Monné  *
770a6aedc5dSRoger Pau Monné  * Reply type and length data are returned by overwriting these
771a6aedc5dSRoger Pau Monné  * fields in the passed in request message.
772a6aedc5dSRoger Pau Monné  *
773a6aedc5dSRoger Pau Monné  * \param msg	  A properly formatted message to transmit to
774a6aedc5dSRoger Pau Monné  *		  the XenStore service.
775a6aedc5dSRoger Pau Monné  * \param result  The returned body of the reply.
776a6aedc5dSRoger Pau Monné  *
777a6aedc5dSRoger Pau Monné  * \return  0 on success.  Otherwise an errno indicating the cause
778a6aedc5dSRoger Pau Monné  *          of failure.
779a6aedc5dSRoger Pau Monné  *
780a6aedc5dSRoger Pau Monné  * \note The returned result is provided in malloced storage and thus
781a6aedc5dSRoger Pau Monné  *       must be free'd by the caller with 'free(result, M_XENSTORE);
782a6aedc5dSRoger Pau Monné  */
783a6aedc5dSRoger Pau Monné int
784a6aedc5dSRoger Pau Monné xs_dev_request_and_reply(struct xsd_sockmsg *msg, void **result)
785a6aedc5dSRoger Pau Monné {
786a6aedc5dSRoger Pau Monné 	uint32_t request_type;
787a6aedc5dSRoger Pau Monné 	int error;
788a6aedc5dSRoger Pau Monné 
789a6aedc5dSRoger Pau Monné 	request_type = msg->type;
790a6aedc5dSRoger Pau Monné 
791a6aedc5dSRoger Pau Monné 	sx_xlock(&xs.request_mutex);
792a6aedc5dSRoger Pau Monné 	if ((error = xs_write_store(msg, sizeof(*msg) + msg->len)) == 0)
793a6aedc5dSRoger Pau Monné 		error = xs_read_reply(&msg->type, &msg->len, result);
794a6aedc5dSRoger Pau Monné 	sx_xunlock(&xs.request_mutex);
795a6aedc5dSRoger Pau Monné 
796a6aedc5dSRoger Pau Monné 	return (error);
797a6aedc5dSRoger Pau Monné }
798a6aedc5dSRoger Pau Monné 
799a6aedc5dSRoger Pau Monné /**
800a6aedc5dSRoger Pau Monné  * Send a message with an optionally muti-part body to the XenStore service.
801a6aedc5dSRoger Pau Monné  *
802a6aedc5dSRoger Pau Monné  * \param t              The transaction to use for this request.
803a6aedc5dSRoger Pau Monné  * \param request_type   The type of message to send.
804a6aedc5dSRoger Pau Monné  * \param iovec          Pointers to the body sections of the request.
805a6aedc5dSRoger Pau Monné  * \param num_vecs       The number of body sections in the request.
806a6aedc5dSRoger Pau Monné  * \param len            The returned length of the reply.
807a6aedc5dSRoger Pau Monné  * \param result         The returned body of the reply.
808a6aedc5dSRoger Pau Monné  *
809a6aedc5dSRoger Pau Monné  * \return  0 on success.  Otherwise an errno indicating
810a6aedc5dSRoger Pau Monné  *          the cause of failure.
811a6aedc5dSRoger Pau Monné  *
812a6aedc5dSRoger Pau Monné  * \note The returned result is provided in malloced storage and thus
813a6aedc5dSRoger Pau Monné  *       must be free'd by the caller with 'free(*result, M_XENSTORE);
814a6aedc5dSRoger Pau Monné  */
815a6aedc5dSRoger Pau Monné static int
816a6aedc5dSRoger Pau Monné xs_talkv(struct xs_transaction t, enum xsd_sockmsg_type request_type,
817a6aedc5dSRoger Pau Monné     const struct iovec *iovec, u_int num_vecs, u_int *len, void **result)
818a6aedc5dSRoger Pau Monné {
819a6aedc5dSRoger Pau Monné 	struct xsd_sockmsg msg;
820a6aedc5dSRoger Pau Monné 	void *ret = NULL;
821a6aedc5dSRoger Pau Monné 	u_int i;
822a6aedc5dSRoger Pau Monné 	int error;
823a6aedc5dSRoger Pau Monné 
824a6aedc5dSRoger Pau Monné 	msg.tx_id = t.id;
825a6aedc5dSRoger Pau Monné 	msg.req_id = 0;
826a6aedc5dSRoger Pau Monné 	msg.type = request_type;
827a6aedc5dSRoger Pau Monné 	msg.len = 0;
828a6aedc5dSRoger Pau Monné 	for (i = 0; i < num_vecs; i++)
829a6aedc5dSRoger Pau Monné 		msg.len += iovec[i].iov_len;
830a6aedc5dSRoger Pau Monné 
831a6aedc5dSRoger Pau Monné 	sx_xlock(&xs.request_mutex);
832a6aedc5dSRoger Pau Monné 	error = xs_write_store(&msg, sizeof(msg));
833a6aedc5dSRoger Pau Monné 	if (error) {
834a6aedc5dSRoger Pau Monné 		printf("xs_talkv failed %d\n", error);
835a6aedc5dSRoger Pau Monné 		goto error_lock_held;
836a6aedc5dSRoger Pau Monné 	}
837a6aedc5dSRoger Pau Monné 
838a6aedc5dSRoger Pau Monné 	for (i = 0; i < num_vecs; i++) {
839a6aedc5dSRoger Pau Monné 		error = xs_write_store(iovec[i].iov_base, iovec[i].iov_len);
840a6aedc5dSRoger Pau Monné 		if (error) {
841a6aedc5dSRoger Pau Monné 			printf("xs_talkv failed %d\n", error);
842a6aedc5dSRoger Pau Monné 			goto error_lock_held;
843a6aedc5dSRoger Pau Monné 		}
844a6aedc5dSRoger Pau Monné 	}
845a6aedc5dSRoger Pau Monné 
846a6aedc5dSRoger Pau Monné 	error = xs_read_reply(&msg.type, len, &ret);
847a6aedc5dSRoger Pau Monné 
848a6aedc5dSRoger Pau Monné error_lock_held:
849a6aedc5dSRoger Pau Monné 	sx_xunlock(&xs.request_mutex);
850a6aedc5dSRoger Pau Monné 	if (error)
851a6aedc5dSRoger Pau Monné 		return (error);
852a6aedc5dSRoger Pau Monné 
853a6aedc5dSRoger Pau Monné 	if (msg.type == XS_ERROR) {
854a6aedc5dSRoger Pau Monné 		error = xs_get_error(ret);
855a6aedc5dSRoger Pau Monné 		free(ret, M_XENSTORE);
856a6aedc5dSRoger Pau Monné 		return (error);
857a6aedc5dSRoger Pau Monné 	}
858a6aedc5dSRoger Pau Monné 
859a6aedc5dSRoger Pau Monné 	/* Reply is either error or an echo of our request message type. */
860a6aedc5dSRoger Pau Monné 	KASSERT(msg.type == request_type, ("bad xenstore message type"));
861a6aedc5dSRoger Pau Monné 
862a6aedc5dSRoger Pau Monné 	if (result)
863a6aedc5dSRoger Pau Monné 		*result = ret;
864a6aedc5dSRoger Pau Monné 	else
865a6aedc5dSRoger Pau Monné 		free(ret, M_XENSTORE);
866a6aedc5dSRoger Pau Monné 
867a6aedc5dSRoger Pau Monné 	return (0);
868a6aedc5dSRoger Pau Monné }
869a6aedc5dSRoger Pau Monné 
870a6aedc5dSRoger Pau Monné /**
871a6aedc5dSRoger Pau Monné  * Wrapper for xs_talkv allowing easy transmission of a message with
872a6aedc5dSRoger Pau Monné  * a single, contiguous, message body.
873a6aedc5dSRoger Pau Monné  *
874a6aedc5dSRoger Pau Monné  * \param t              The transaction to use for this request.
875a6aedc5dSRoger Pau Monné  * \param request_type   The type of message to send.
876a6aedc5dSRoger Pau Monné  * \param body           The body of the request.
877a6aedc5dSRoger Pau Monné  * \param len            The returned length of the reply.
878a6aedc5dSRoger Pau Monné  * \param result         The returned body of the reply.
879a6aedc5dSRoger Pau Monné  *
880a6aedc5dSRoger Pau Monné  * \return  0 on success.  Otherwise an errno indicating
881a6aedc5dSRoger Pau Monné  *          the cause of failure.
882a6aedc5dSRoger Pau Monné  *
883a6aedc5dSRoger Pau Monné  * \note The returned result is provided in malloced storage and thus
884a6aedc5dSRoger Pau Monné  *       must be free'd by the caller with 'free(*result, M_XENSTORE);
885a6aedc5dSRoger Pau Monné  */
886a6aedc5dSRoger Pau Monné static int
887a6aedc5dSRoger Pau Monné xs_single(struct xs_transaction t, enum xsd_sockmsg_type request_type,
888a6aedc5dSRoger Pau Monné     const char *body, u_int *len, void **result)
889a6aedc5dSRoger Pau Monné {
890a6aedc5dSRoger Pau Monné 	struct iovec iovec;
891a6aedc5dSRoger Pau Monné 
892a6aedc5dSRoger Pau Monné 	iovec.iov_base = (void *)(uintptr_t)body;
893a6aedc5dSRoger Pau Monné 	iovec.iov_len = strlen(body) + 1;
894a6aedc5dSRoger Pau Monné 
895a6aedc5dSRoger Pau Monné 	return (xs_talkv(t, request_type, &iovec, 1, len, result));
896a6aedc5dSRoger Pau Monné }
897a6aedc5dSRoger Pau Monné 
898a6aedc5dSRoger Pau Monné /*------------------------- XenStore Watch Support ---------------------------*/
899a6aedc5dSRoger Pau Monné /**
900a6aedc5dSRoger Pau Monné  * Transmit a watch request to the XenStore service.
901a6aedc5dSRoger Pau Monné  *
902a6aedc5dSRoger Pau Monné  * \param path    The path in the XenStore to watch.
903a6aedc5dSRoger Pau Monné  * \param tocken  A unique identifier for this watch.
904a6aedc5dSRoger Pau Monné  *
905a6aedc5dSRoger Pau Monné  * \return  0 on success.  Otherwise an errno indicating the
906a6aedc5dSRoger Pau Monné  *          cause of failure.
907a6aedc5dSRoger Pau Monné  */
908a6aedc5dSRoger Pau Monné static int
909a6aedc5dSRoger Pau Monné xs_watch(const char *path, const char *token)
910a6aedc5dSRoger Pau Monné {
911a6aedc5dSRoger Pau Monné 	struct iovec iov[2];
912a6aedc5dSRoger Pau Monné 
913a6aedc5dSRoger Pau Monné 	iov[0].iov_base = (void *)(uintptr_t) path;
914a6aedc5dSRoger Pau Monné 	iov[0].iov_len = strlen(path) + 1;
915a6aedc5dSRoger Pau Monné 	iov[1].iov_base = (void *)(uintptr_t) token;
916a6aedc5dSRoger Pau Monné 	iov[1].iov_len = strlen(token) + 1;
917a6aedc5dSRoger Pau Monné 
918a6aedc5dSRoger Pau Monné 	return (xs_talkv(XST_NIL, XS_WATCH, iov, 2, NULL, NULL));
919a6aedc5dSRoger Pau Monné }
920a6aedc5dSRoger Pau Monné 
921a6aedc5dSRoger Pau Monné /**
922a6aedc5dSRoger Pau Monné  * Transmit an uwatch request to the XenStore service.
923a6aedc5dSRoger Pau Monné  *
924a6aedc5dSRoger Pau Monné  * \param path    The path in the XenStore to watch.
925a6aedc5dSRoger Pau Monné  * \param tocken  A unique identifier for this watch.
926a6aedc5dSRoger Pau Monné  *
927a6aedc5dSRoger Pau Monné  * \return  0 on success.  Otherwise an errno indicating the
928a6aedc5dSRoger Pau Monné  *          cause of failure.
929a6aedc5dSRoger Pau Monné  */
930a6aedc5dSRoger Pau Monné static int
931a6aedc5dSRoger Pau Monné xs_unwatch(const char *path, const char *token)
932a6aedc5dSRoger Pau Monné {
933a6aedc5dSRoger Pau Monné 	struct iovec iov[2];
934a6aedc5dSRoger Pau Monné 
935a6aedc5dSRoger Pau Monné 	iov[0].iov_base = (void *)(uintptr_t) path;
936a6aedc5dSRoger Pau Monné 	iov[0].iov_len = strlen(path) + 1;
937a6aedc5dSRoger Pau Monné 	iov[1].iov_base = (void *)(uintptr_t) token;
938a6aedc5dSRoger Pau Monné 	iov[1].iov_len = strlen(token) + 1;
939a6aedc5dSRoger Pau Monné 
940a6aedc5dSRoger Pau Monné 	return (xs_talkv(XST_NIL, XS_UNWATCH, iov, 2, NULL, NULL));
941a6aedc5dSRoger Pau Monné }
942a6aedc5dSRoger Pau Monné 
943a6aedc5dSRoger Pau Monné /**
944a6aedc5dSRoger Pau Monné  * Convert from watch token (unique identifier) to the associated
945a6aedc5dSRoger Pau Monné  * internal tracking structure for this watch.
946a6aedc5dSRoger Pau Monné  *
947a6aedc5dSRoger Pau Monné  * \param tocken  The unique identifier for the watch to find.
948a6aedc5dSRoger Pau Monné  *
949a6aedc5dSRoger Pau Monné  * \return  A pointer to the found watch structure or NULL.
950a6aedc5dSRoger Pau Monné  */
951a6aedc5dSRoger Pau Monné static struct xs_watch *
952a6aedc5dSRoger Pau Monné find_watch(const char *token)
953a6aedc5dSRoger Pau Monné {
954a6aedc5dSRoger Pau Monné 	struct xs_watch *i, *cmp;
955a6aedc5dSRoger Pau Monné 
956a6aedc5dSRoger Pau Monné 	cmp = (void *)strtoul(token, NULL, 16);
957a6aedc5dSRoger Pau Monné 
958a6aedc5dSRoger Pau Monné 	LIST_FOREACH(i, &xs.registered_watches, list)
959a6aedc5dSRoger Pau Monné 		if (i == cmp)
960a6aedc5dSRoger Pau Monné 			return (i);
961a6aedc5dSRoger Pau Monné 
962a6aedc5dSRoger Pau Monné 	return (NULL);
963a6aedc5dSRoger Pau Monné }
964a6aedc5dSRoger Pau Monné 
965a6aedc5dSRoger Pau Monné /**
966a6aedc5dSRoger Pau Monné  * Thread body of the XenStore watch event dispatch thread.
967a6aedc5dSRoger Pau Monné  */
968a6aedc5dSRoger Pau Monné static void
969a6aedc5dSRoger Pau Monné xenwatch_thread(void *unused)
970a6aedc5dSRoger Pau Monné {
971a6aedc5dSRoger Pau Monné 	struct xs_stored_msg *msg;
972a6aedc5dSRoger Pau Monné 
973a6aedc5dSRoger Pau Monné 	for (;;) {
974a6aedc5dSRoger Pau Monné 
975a6aedc5dSRoger Pau Monné 		mtx_lock(&xs.watch_events_lock);
976a6aedc5dSRoger Pau Monné 		while (TAILQ_EMPTY(&xs.watch_events))
977a6aedc5dSRoger Pau Monné 			mtx_sleep(&xs.watch_events,
978a6aedc5dSRoger Pau Monné 			    &xs.watch_events_lock,
979a6aedc5dSRoger Pau Monné 			    PWAIT | PCATCH, "waitev", hz/10);
980a6aedc5dSRoger Pau Monné 
981a6aedc5dSRoger Pau Monné 		mtx_unlock(&xs.watch_events_lock);
982a6aedc5dSRoger Pau Monné 		sx_xlock(&xs.xenwatch_mutex);
983a6aedc5dSRoger Pau Monné 
984a6aedc5dSRoger Pau Monné 		mtx_lock(&xs.watch_events_lock);
985a6aedc5dSRoger Pau Monné 		msg = TAILQ_FIRST(&xs.watch_events);
986a6aedc5dSRoger Pau Monné 		if (msg)
987a6aedc5dSRoger Pau Monné 			TAILQ_REMOVE(&xs.watch_events, msg, list);
988a6aedc5dSRoger Pau Monné 		mtx_unlock(&xs.watch_events_lock);
989a6aedc5dSRoger Pau Monné 
990a6aedc5dSRoger Pau Monné 		if (msg != NULL) {
991a6aedc5dSRoger Pau Monné 			/*
992a6aedc5dSRoger Pau Monné 			 * XXX There are messages coming in with a NULL
993a6aedc5dSRoger Pau Monné 			 * XXX callback.  This deserves further investigation;
994a6aedc5dSRoger Pau Monné 			 * XXX the workaround here simply prevents the kernel
995a6aedc5dSRoger Pau Monné 			 * XXX from panic'ing on startup.
996a6aedc5dSRoger Pau Monné 			 */
997a6aedc5dSRoger Pau Monné 			if (msg->u.watch.handle->callback != NULL)
998a6aedc5dSRoger Pau Monné 				msg->u.watch.handle->callback(
999a6aedc5dSRoger Pau Monné 					msg->u.watch.handle,
1000a6aedc5dSRoger Pau Monné 					(const char **)msg->u.watch.vec,
1001a6aedc5dSRoger Pau Monné 					msg->u.watch.vec_size);
1002a6aedc5dSRoger Pau Monné 			free(msg->u.watch.vec, M_XENSTORE);
1003a6aedc5dSRoger Pau Monné 			free(msg, M_XENSTORE);
1004a6aedc5dSRoger Pau Monné 		}
1005a6aedc5dSRoger Pau Monné 
1006a6aedc5dSRoger Pau Monné 		sx_xunlock(&xs.xenwatch_mutex);
1007a6aedc5dSRoger Pau Monné 	}
1008a6aedc5dSRoger Pau Monné }
1009a6aedc5dSRoger Pau Monné 
1010a6aedc5dSRoger Pau Monné /*----------- XenStore Configuration, Initialization, and Control ------------*/
1011a6aedc5dSRoger Pau Monné /**
1012a6aedc5dSRoger Pau Monné  * Setup communication channels with the XenStore service.
1013a6aedc5dSRoger Pau Monné  *
1014a6aedc5dSRoger Pau Monné  * \return  On success, 0. Otherwise an errno value indicating the
1015a6aedc5dSRoger Pau Monné  *          type of failure.
1016a6aedc5dSRoger Pau Monné  */
1017a6aedc5dSRoger Pau Monné static int
1018a6aedc5dSRoger Pau Monné xs_init_comms(void)
1019a6aedc5dSRoger Pau Monné {
1020a6aedc5dSRoger Pau Monné 	int error;
1021a6aedc5dSRoger Pau Monné 
1022a6aedc5dSRoger Pau Monné 	if (xen_store->rsp_prod != xen_store->rsp_cons) {
1023a6aedc5dSRoger Pau Monné 		log(LOG_WARNING, "XENSTORE response ring is not quiescent "
1024a6aedc5dSRoger Pau Monné 		    "(%08x:%08x): fixing up\n",
1025a6aedc5dSRoger Pau Monné 		    xen_store->rsp_cons, xen_store->rsp_prod);
1026a6aedc5dSRoger Pau Monné 		xen_store->rsp_cons = xen_store->rsp_prod;
1027a6aedc5dSRoger Pau Monné 	}
1028a6aedc5dSRoger Pau Monné 
1029a6aedc5dSRoger Pau Monné 	xen_intr_unbind(&xs.xen_intr_handle);
1030a6aedc5dSRoger Pau Monné 
1031a6aedc5dSRoger Pau Monné 	error = xen_intr_bind_local_port(xs.xs_dev, xs.evtchn,
1032a6aedc5dSRoger Pau Monné 	    /*filter*/NULL, xs_intr, /*arg*/NULL, INTR_TYPE_NET|INTR_MPSAFE,
1033a6aedc5dSRoger Pau Monné 	    &xs.xen_intr_handle);
1034a6aedc5dSRoger Pau Monné 	if (error) {
1035a6aedc5dSRoger Pau Monné 		log(LOG_WARNING, "XENSTORE request irq failed %i\n", error);
1036a6aedc5dSRoger Pau Monné 		return (error);
1037a6aedc5dSRoger Pau Monné 	}
1038a6aedc5dSRoger Pau Monné 
1039a6aedc5dSRoger Pau Monné 	return (0);
1040a6aedc5dSRoger Pau Monné }
1041a6aedc5dSRoger Pau Monné 
1042a6aedc5dSRoger Pau Monné /*------------------ Private Device Attachment Functions  --------------------*/
1043a6aedc5dSRoger Pau Monné static void
1044a6aedc5dSRoger Pau Monné xs_identify(driver_t *driver, device_t parent)
1045a6aedc5dSRoger Pau Monné {
1046a6aedc5dSRoger Pau Monné 
1047a6aedc5dSRoger Pau Monné 	BUS_ADD_CHILD(parent, 0, "xenstore", 0);
1048a6aedc5dSRoger Pau Monné }
1049a6aedc5dSRoger Pau Monné 
1050a6aedc5dSRoger Pau Monné /**
1051453130d9SPedro F. Giffuni  * Probe for the existence of the XenStore.
1052a6aedc5dSRoger Pau Monné  *
1053a6aedc5dSRoger Pau Monné  * \param dev
1054a6aedc5dSRoger Pau Monné  */
1055a6aedc5dSRoger Pau Monné static int
1056a6aedc5dSRoger Pau Monné xs_probe(device_t dev)
1057a6aedc5dSRoger Pau Monné {
1058a6aedc5dSRoger Pau Monné 	/*
1059a6aedc5dSRoger Pau Monné 	 * We are either operating within a PV kernel or being probed
1060a6aedc5dSRoger Pau Monné 	 * as the child of the successfully attached xenpci device.
1061a6aedc5dSRoger Pau Monné 	 * Thus we are in a Xen environment and there will be a XenStore.
1062a6aedc5dSRoger Pau Monné 	 * Unconditionally return success.
1063a6aedc5dSRoger Pau Monné 	 */
1064a6aedc5dSRoger Pau Monné 	device_set_desc(dev, "XenStore");
10652f9ec994SRoger Pau Monné 	return (BUS_PROBE_NOWILDCARD);
1066a6aedc5dSRoger Pau Monné }
1067a6aedc5dSRoger Pau Monné 
1068a6aedc5dSRoger Pau Monné static void
1069a6aedc5dSRoger Pau Monné xs_attach_deferred(void *arg)
1070a6aedc5dSRoger Pau Monné {
1071a6aedc5dSRoger Pau Monné 
1072a6aedc5dSRoger Pau Monné 	bus_generic_probe(xs.xs_dev);
1073a6aedc5dSRoger Pau Monné 	bus_generic_attach(xs.xs_dev);
1074a6aedc5dSRoger Pau Monné 
1075a6aedc5dSRoger Pau Monné 	config_intrhook_disestablish(&xs.xs_attachcb);
1076a6aedc5dSRoger Pau Monné }
1077a6aedc5dSRoger Pau Monné 
10781d84e2b3SRoger Pau Monné static void
10791d84e2b3SRoger Pau Monné xs_attach_late(void *arg, int pending)
10801d84e2b3SRoger Pau Monné {
10811d84e2b3SRoger Pau Monné 
10821d84e2b3SRoger Pau Monné 	KASSERT((pending == 1), ("xs late attach queued several times"));
10831d84e2b3SRoger Pau Monné 	bus_generic_probe(xs.xs_dev);
10841d84e2b3SRoger Pau Monné 	bus_generic_attach(xs.xs_dev);
10851d84e2b3SRoger Pau Monné }
10861d84e2b3SRoger Pau Monné 
1087a6aedc5dSRoger Pau Monné /**
1088a6aedc5dSRoger Pau Monné  * Attach to the XenStore.
1089a6aedc5dSRoger Pau Monné  *
1090a6aedc5dSRoger Pau Monné  * This routine also prepares for the probe/attach of drivers that rely
1091a6aedc5dSRoger Pau Monné  * on the XenStore.
1092a6aedc5dSRoger Pau Monné  */
1093a6aedc5dSRoger Pau Monné static int
1094a6aedc5dSRoger Pau Monné xs_attach(device_t dev)
1095a6aedc5dSRoger Pau Monné {
1096a6aedc5dSRoger Pau Monné 	int error;
1097a6aedc5dSRoger Pau Monné 
1098a6aedc5dSRoger Pau Monné 	/* Allow us to get device_t from softc and vice-versa. */
1099a6aedc5dSRoger Pau Monné 	xs.xs_dev = dev;
1100a6aedc5dSRoger Pau Monné 	device_set_softc(dev, &xs);
1101a6aedc5dSRoger Pau Monné 
1102a6aedc5dSRoger Pau Monné 	/* Initialize the interface to xenstore. */
1103a6aedc5dSRoger Pau Monné 	struct proc *p;
1104a6aedc5dSRoger Pau Monné 
11051d84e2b3SRoger Pau Monné 	xs.initialized = false;
1106a6aedc5dSRoger Pau Monné 	if (xen_hvm_domain()) {
1107a6aedc5dSRoger Pau Monné 		xs.evtchn = hvm_get_parameter(HVM_PARAM_STORE_EVTCHN);
1108a6aedc5dSRoger Pau Monné 		xs.gpfn = hvm_get_parameter(HVM_PARAM_STORE_PFN);
1109a6aedc5dSRoger Pau Monné 		xen_store = pmap_mapdev(xs.gpfn * PAGE_SIZE, PAGE_SIZE);
11101d84e2b3SRoger Pau Monné 		xs.initialized = true;
1111a6aedc5dSRoger Pau Monné 	} else if (xen_pv_domain()) {
11121d84e2b3SRoger Pau Monné 		if (HYPERVISOR_start_info->store_evtchn == 0) {
11131d84e2b3SRoger Pau Monné 			struct evtchn_alloc_unbound alloc_unbound;
11141d84e2b3SRoger Pau Monné 
11151d84e2b3SRoger Pau Monné 			/* Allocate a local event channel for xenstore */
11161d84e2b3SRoger Pau Monné 			alloc_unbound.dom = DOMID_SELF;
11171d84e2b3SRoger Pau Monné 			alloc_unbound.remote_dom = DOMID_SELF;
11181d84e2b3SRoger Pau Monné 			error = HYPERVISOR_event_channel_op(
11191d84e2b3SRoger Pau Monné 			    EVTCHNOP_alloc_unbound, &alloc_unbound);
11201d84e2b3SRoger Pau Monné 			if (error != 0)
11211d84e2b3SRoger Pau Monné 				panic(
11221d84e2b3SRoger Pau Monné 				   "unable to alloc event channel for Dom0: %d",
11231d84e2b3SRoger Pau Monné 				    error);
11241d84e2b3SRoger Pau Monné 
11251d84e2b3SRoger Pau Monné 			HYPERVISOR_start_info->store_evtchn =
11261d84e2b3SRoger Pau Monné 			    alloc_unbound.port;
11271d84e2b3SRoger Pau Monné 			xs.evtchn = alloc_unbound.port;
11281d84e2b3SRoger Pau Monné 
11291d84e2b3SRoger Pau Monné 			/* Allocate memory for the xs shared ring */
11301d84e2b3SRoger Pau Monné 			xen_store = malloc(PAGE_SIZE, M_XENSTORE,
11311d84e2b3SRoger Pau Monné 			    M_WAITOK | M_ZERO);
11321d84e2b3SRoger Pau Monné 		} else {
1133a6aedc5dSRoger Pau Monné 			xs.evtchn = HYPERVISOR_start_info->store_evtchn;
11341d84e2b3SRoger Pau Monné 			xs.initialized = true;
11351d84e2b3SRoger Pau Monné 		}
1136a6aedc5dSRoger Pau Monné 	} else {
1137a6aedc5dSRoger Pau Monné 		panic("Unknown domain type, cannot initialize xenstore.");
1138a6aedc5dSRoger Pau Monné 	}
1139a6aedc5dSRoger Pau Monné 
1140a6aedc5dSRoger Pau Monné 	TAILQ_INIT(&xs.reply_list);
1141a6aedc5dSRoger Pau Monné 	TAILQ_INIT(&xs.watch_events);
1142a6aedc5dSRoger Pau Monné 
1143a6aedc5dSRoger Pau Monné 	mtx_init(&xs.ring_lock, "ring lock", NULL, MTX_DEF);
1144a6aedc5dSRoger Pau Monné 	mtx_init(&xs.reply_lock, "reply lock", NULL, MTX_DEF);
1145a6aedc5dSRoger Pau Monné 	sx_init(&xs.xenwatch_mutex, "xenwatch");
1146a6aedc5dSRoger Pau Monné 	sx_init(&xs.request_mutex, "xenstore request");
1147a6aedc5dSRoger Pau Monné 	mtx_init(&xs.registered_watches_lock, "watches", NULL, MTX_DEF);
1148a6aedc5dSRoger Pau Monné 	mtx_init(&xs.watch_events_lock, "watch events", NULL, MTX_DEF);
1149a6aedc5dSRoger Pau Monné 
1150a6aedc5dSRoger Pau Monné 	/* Initialize the shared memory rings to talk to xenstored */
1151a6aedc5dSRoger Pau Monné 	error = xs_init_comms();
1152a6aedc5dSRoger Pau Monné 	if (error)
1153a6aedc5dSRoger Pau Monné 		return (error);
1154a6aedc5dSRoger Pau Monné 
1155a6aedc5dSRoger Pau Monné 	error = kproc_create(xenwatch_thread, NULL, &p, RFHIGHPID,
1156a6aedc5dSRoger Pau Monné 	    0, "xenwatch");
1157a6aedc5dSRoger Pau Monné 	if (error)
1158a6aedc5dSRoger Pau Monné 		return (error);
1159a6aedc5dSRoger Pau Monné 	xs.xenwatch_pid = p->p_pid;
1160a6aedc5dSRoger Pau Monné 
1161a6aedc5dSRoger Pau Monné 	error = kproc_create(xs_rcv_thread, NULL, NULL,
1162a6aedc5dSRoger Pau Monné 	    RFHIGHPID, 0, "xenstore_rcv");
1163a6aedc5dSRoger Pau Monné 
1164a6aedc5dSRoger Pau Monné 	xs.xs_attachcb.ich_func = xs_attach_deferred;
1165a6aedc5dSRoger Pau Monné 	xs.xs_attachcb.ich_arg = NULL;
11661d84e2b3SRoger Pau Monné 	if (xs.initialized) {
1167a6aedc5dSRoger Pau Monné 		config_intrhook_establish(&xs.xs_attachcb);
11681d84e2b3SRoger Pau Monné 	} else {
11691d84e2b3SRoger Pau Monné 		TASK_INIT(&xs.xs_late_init, 0, xs_attach_late, NULL);
11701d84e2b3SRoger Pau Monné 	}
1171a6aedc5dSRoger Pau Monné 
1172a6aedc5dSRoger Pau Monné 	return (error);
1173a6aedc5dSRoger Pau Monné }
1174a6aedc5dSRoger Pau Monné 
1175a6aedc5dSRoger Pau Monné /**
1176a6aedc5dSRoger Pau Monné  * Prepare for suspension of this VM by halting XenStore access after
1177a6aedc5dSRoger Pau Monné  * all transactions and individual requests have completed.
1178a6aedc5dSRoger Pau Monné  */
1179a6aedc5dSRoger Pau Monné static int
1180a6aedc5dSRoger Pau Monné xs_suspend(device_t dev)
1181a6aedc5dSRoger Pau Monné {
1182a6aedc5dSRoger Pau Monné 	int error;
1183a6aedc5dSRoger Pau Monné 
1184a6aedc5dSRoger Pau Monné 	/* Suspend child Xen devices. */
1185a6aedc5dSRoger Pau Monné 	error = bus_generic_suspend(dev);
1186a6aedc5dSRoger Pau Monné 	if (error != 0)
1187a6aedc5dSRoger Pau Monné 		return (error);
1188a6aedc5dSRoger Pau Monné 
1189a6aedc5dSRoger Pau Monné 	sx_xlock(&xs.request_mutex);
1190a6aedc5dSRoger Pau Monné 
1191a6aedc5dSRoger Pau Monné 	return (0);
1192a6aedc5dSRoger Pau Monné }
1193a6aedc5dSRoger Pau Monné 
1194a6aedc5dSRoger Pau Monné /**
1195a6aedc5dSRoger Pau Monné  * Resume XenStore operations after this VM is resumed.
1196a6aedc5dSRoger Pau Monné  */
1197a6aedc5dSRoger Pau Monné static int
1198a6aedc5dSRoger Pau Monné xs_resume(device_t dev __unused)
1199a6aedc5dSRoger Pau Monné {
1200a6aedc5dSRoger Pau Monné 	struct xs_watch *watch;
1201a6aedc5dSRoger Pau Monné 	char token[sizeof(watch) * 2 + 1];
1202a6aedc5dSRoger Pau Monné 
1203a6aedc5dSRoger Pau Monné 	xs_init_comms();
1204a6aedc5dSRoger Pau Monné 
1205a6aedc5dSRoger Pau Monné 	sx_xunlock(&xs.request_mutex);
1206a6aedc5dSRoger Pau Monné 
1207a6aedc5dSRoger Pau Monné 	/*
12085f8f6646SRoger Pau Monné 	 * NB: since xenstore childs have not been resumed yet, there's
12095f8f6646SRoger Pau Monné 	 * no need to hold any watch mutex. Having clients try to add or
12105f8f6646SRoger Pau Monné 	 * remove watches at this point (before xenstore is resumed) is
12115f8f6646SRoger Pau Monné 	 * clearly a violantion of the resume order.
1212a6aedc5dSRoger Pau Monné 	 */
1213a6aedc5dSRoger Pau Monné 	LIST_FOREACH(watch, &xs.registered_watches, list) {
1214a6aedc5dSRoger Pau Monné 		sprintf(token, "%lX", (long)watch);
1215a6aedc5dSRoger Pau Monné 		xs_watch(watch->node, token);
1216a6aedc5dSRoger Pau Monné 	}
1217a6aedc5dSRoger Pau Monné 
1218a6aedc5dSRoger Pau Monné 	/* Resume child Xen devices. */
1219a6aedc5dSRoger Pau Monné 	bus_generic_resume(dev);
1220a6aedc5dSRoger Pau Monné 
1221a6aedc5dSRoger Pau Monné 	return (0);
1222a6aedc5dSRoger Pau Monné }
1223a6aedc5dSRoger Pau Monné 
1224a6aedc5dSRoger Pau Monné /*-------------------- Private Device Attachment Data  -----------------------*/
1225a6aedc5dSRoger Pau Monné static device_method_t xenstore_methods[] = {
1226a6aedc5dSRoger Pau Monné 	/* Device interface */
1227a6aedc5dSRoger Pau Monné 	DEVMETHOD(device_identify,	xs_identify),
1228a6aedc5dSRoger Pau Monné 	DEVMETHOD(device_probe,         xs_probe),
1229a6aedc5dSRoger Pau Monné 	DEVMETHOD(device_attach,        xs_attach),
1230a6aedc5dSRoger Pau Monné 	DEVMETHOD(device_detach,        bus_generic_detach),
1231a6aedc5dSRoger Pau Monné 	DEVMETHOD(device_shutdown,      bus_generic_shutdown),
1232a6aedc5dSRoger Pau Monné 	DEVMETHOD(device_suspend,       xs_suspend),
1233a6aedc5dSRoger Pau Monné 	DEVMETHOD(device_resume,        xs_resume),
1234a6aedc5dSRoger Pau Monné 
1235a6aedc5dSRoger Pau Monné 	/* Bus interface */
1236a6aedc5dSRoger Pau Monné 	DEVMETHOD(bus_add_child,        bus_generic_add_child),
1237a6aedc5dSRoger Pau Monné 	DEVMETHOD(bus_alloc_resource,   bus_generic_alloc_resource),
1238a6aedc5dSRoger Pau Monné 	DEVMETHOD(bus_release_resource, bus_generic_release_resource),
1239a6aedc5dSRoger Pau Monné 	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
1240a6aedc5dSRoger Pau Monné 	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
1241a6aedc5dSRoger Pau Monné 
1242a6aedc5dSRoger Pau Monné 	DEVMETHOD_END
1243a6aedc5dSRoger Pau Monné };
1244a6aedc5dSRoger Pau Monné 
1245a6aedc5dSRoger Pau Monné DEFINE_CLASS_0(xenstore, xenstore_driver, xenstore_methods, 0);
1246a6aedc5dSRoger Pau Monné static devclass_t xenstore_devclass;
1247a6aedc5dSRoger Pau Monné 
1248a6aedc5dSRoger Pau Monné DRIVER_MODULE(xenstore, xenpv, xenstore_driver, xenstore_devclass, 0, 0);
1249a6aedc5dSRoger Pau Monné 
1250a6aedc5dSRoger Pau Monné /*------------------------------- Sysctl Data --------------------------------*/
1251a6aedc5dSRoger Pau Monné /* XXX Shouldn't the node be somewhere else? */
1252a6aedc5dSRoger Pau Monné SYSCTL_NODE(_dev, OID_AUTO, xen, CTLFLAG_RD, NULL, "Xen");
1253a6aedc5dSRoger Pau Monné SYSCTL_INT(_dev_xen, OID_AUTO, xsd_port, CTLFLAG_RD, &xs.evtchn, 0, "");
1254a6aedc5dSRoger Pau Monné SYSCTL_ULONG(_dev_xen, OID_AUTO, xsd_kva, CTLFLAG_RD, (u_long *) &xen_store, 0, "");
1255a6aedc5dSRoger Pau Monné 
1256a6aedc5dSRoger Pau Monné /*-------------------------------- Public API --------------------------------*/
1257a6aedc5dSRoger Pau Monné /*------- API comments for these methods can be found in xenstorevar.h -------*/
1258a6aedc5dSRoger Pau Monné int
1259a6aedc5dSRoger Pau Monné xs_directory(struct xs_transaction t, const char *dir, const char *node,
1260a6aedc5dSRoger Pau Monné     u_int *num, const char ***result)
1261a6aedc5dSRoger Pau Monné {
1262a6aedc5dSRoger Pau Monné 	struct sbuf *path;
1263a6aedc5dSRoger Pau Monné 	char *strings;
1264a6aedc5dSRoger Pau Monné 	u_int len = 0;
1265a6aedc5dSRoger Pau Monné 	int error;
1266a6aedc5dSRoger Pau Monné 
1267a6aedc5dSRoger Pau Monné 	path = xs_join(dir, node);
1268a6aedc5dSRoger Pau Monné 	error = xs_single(t, XS_DIRECTORY, sbuf_data(path), &len,
1269a6aedc5dSRoger Pau Monné 	    (void **)&strings);
1270a6aedc5dSRoger Pau Monné 	sbuf_delete(path);
1271a6aedc5dSRoger Pau Monné 	if (error)
1272a6aedc5dSRoger Pau Monné 		return (error);
1273a6aedc5dSRoger Pau Monné 
1274a6aedc5dSRoger Pau Monné 	*result = split(strings, len, num);
1275a6aedc5dSRoger Pau Monné 
1276a6aedc5dSRoger Pau Monné 	return (0);
1277a6aedc5dSRoger Pau Monné }
1278a6aedc5dSRoger Pau Monné 
1279a6aedc5dSRoger Pau Monné int
1280a6aedc5dSRoger Pau Monné xs_exists(struct xs_transaction t, const char *dir, const char *node)
1281a6aedc5dSRoger Pau Monné {
1282a6aedc5dSRoger Pau Monné 	const char **d;
1283a6aedc5dSRoger Pau Monné 	int error, dir_n;
1284a6aedc5dSRoger Pau Monné 
1285a6aedc5dSRoger Pau Monné 	error = xs_directory(t, dir, node, &dir_n, &d);
1286a6aedc5dSRoger Pau Monné 	if (error)
1287a6aedc5dSRoger Pau Monné 		return (0);
1288a6aedc5dSRoger Pau Monné 	free(d, M_XENSTORE);
1289a6aedc5dSRoger Pau Monné 	return (1);
1290a6aedc5dSRoger Pau Monné }
1291a6aedc5dSRoger Pau Monné 
1292a6aedc5dSRoger Pau Monné int
1293a6aedc5dSRoger Pau Monné xs_read(struct xs_transaction t, const char *dir, const char *node,
1294a6aedc5dSRoger Pau Monné     u_int *len, void **result)
1295a6aedc5dSRoger Pau Monné {
1296a6aedc5dSRoger Pau Monné 	struct sbuf *path;
1297a6aedc5dSRoger Pau Monné 	void *ret;
1298a6aedc5dSRoger Pau Monné 	int error;
1299a6aedc5dSRoger Pau Monné 
1300a6aedc5dSRoger Pau Monné 	path = xs_join(dir, node);
1301a6aedc5dSRoger Pau Monné 	error = xs_single(t, XS_READ, sbuf_data(path), len, &ret);
1302a6aedc5dSRoger Pau Monné 	sbuf_delete(path);
1303a6aedc5dSRoger Pau Monné 	if (error)
1304a6aedc5dSRoger Pau Monné 		return (error);
1305a6aedc5dSRoger Pau Monné 	*result = ret;
1306a6aedc5dSRoger Pau Monné 	return (0);
1307a6aedc5dSRoger Pau Monné }
1308a6aedc5dSRoger Pau Monné 
1309a6aedc5dSRoger Pau Monné int
1310a6aedc5dSRoger Pau Monné xs_write(struct xs_transaction t, const char *dir, const char *node,
1311a6aedc5dSRoger Pau Monné     const char *string)
1312a6aedc5dSRoger Pau Monné {
1313a6aedc5dSRoger Pau Monné 	struct sbuf *path;
1314a6aedc5dSRoger Pau Monné 	struct iovec iovec[2];
1315a6aedc5dSRoger Pau Monné 	int error;
1316a6aedc5dSRoger Pau Monné 
1317a6aedc5dSRoger Pau Monné 	path = xs_join(dir, node);
1318a6aedc5dSRoger Pau Monné 
1319a6aedc5dSRoger Pau Monné 	iovec[0].iov_base = (void *)(uintptr_t) sbuf_data(path);
1320a6aedc5dSRoger Pau Monné 	iovec[0].iov_len = sbuf_len(path) + 1;
1321a6aedc5dSRoger Pau Monné 	iovec[1].iov_base = (void *)(uintptr_t) string;
1322a6aedc5dSRoger Pau Monné 	iovec[1].iov_len = strlen(string);
1323a6aedc5dSRoger Pau Monné 
1324a6aedc5dSRoger Pau Monné 	error = xs_talkv(t, XS_WRITE, iovec, 2, NULL, NULL);
1325a6aedc5dSRoger Pau Monné 	sbuf_delete(path);
1326a6aedc5dSRoger Pau Monné 
1327a6aedc5dSRoger Pau Monné 	return (error);
1328a6aedc5dSRoger Pau Monné }
1329a6aedc5dSRoger Pau Monné 
1330a6aedc5dSRoger Pau Monné int
1331a6aedc5dSRoger Pau Monné xs_mkdir(struct xs_transaction t, const char *dir, const char *node)
1332a6aedc5dSRoger Pau Monné {
1333a6aedc5dSRoger Pau Monné 	struct sbuf *path;
1334a6aedc5dSRoger Pau Monné 	int ret;
1335a6aedc5dSRoger Pau Monné 
1336a6aedc5dSRoger Pau Monné 	path = xs_join(dir, node);
1337a6aedc5dSRoger Pau Monné 	ret = xs_single(t, XS_MKDIR, sbuf_data(path), NULL, NULL);
1338a6aedc5dSRoger Pau Monné 	sbuf_delete(path);
1339a6aedc5dSRoger Pau Monné 
1340a6aedc5dSRoger Pau Monné 	return (ret);
1341a6aedc5dSRoger Pau Monné }
1342a6aedc5dSRoger Pau Monné 
1343a6aedc5dSRoger Pau Monné int
1344a6aedc5dSRoger Pau Monné xs_rm(struct xs_transaction t, const char *dir, const char *node)
1345a6aedc5dSRoger Pau Monné {
1346a6aedc5dSRoger Pau Monné 	struct sbuf *path;
1347a6aedc5dSRoger Pau Monné 	int ret;
1348a6aedc5dSRoger Pau Monné 
1349a6aedc5dSRoger Pau Monné 	path = xs_join(dir, node);
1350a6aedc5dSRoger Pau Monné 	ret = xs_single(t, XS_RM, sbuf_data(path), NULL, NULL);
1351a6aedc5dSRoger Pau Monné 	sbuf_delete(path);
1352a6aedc5dSRoger Pau Monné 
1353a6aedc5dSRoger Pau Monné 	return (ret);
1354a6aedc5dSRoger Pau Monné }
1355a6aedc5dSRoger Pau Monné 
1356a6aedc5dSRoger Pau Monné int
1357a6aedc5dSRoger Pau Monné xs_rm_tree(struct xs_transaction xbt, const char *base, const char *node)
1358a6aedc5dSRoger Pau Monné {
1359a6aedc5dSRoger Pau Monné 	struct xs_transaction local_xbt;
1360a6aedc5dSRoger Pau Monné 	struct sbuf *root_path_sbuf;
1361a6aedc5dSRoger Pau Monné 	struct sbuf *cur_path_sbuf;
1362a6aedc5dSRoger Pau Monné 	char *root_path;
1363a6aedc5dSRoger Pau Monné 	char *cur_path;
1364a6aedc5dSRoger Pau Monné 	const char **dir;
1365a6aedc5dSRoger Pau Monné 	int error;
1366a6aedc5dSRoger Pau Monné 
1367a6aedc5dSRoger Pau Monné retry:
1368a6aedc5dSRoger Pau Monné 	root_path_sbuf = xs_join(base, node);
1369a6aedc5dSRoger Pau Monné 	cur_path_sbuf  = xs_join(base, node);
1370a6aedc5dSRoger Pau Monné 	root_path      = sbuf_data(root_path_sbuf);
1371a6aedc5dSRoger Pau Monné 	cur_path       = sbuf_data(cur_path_sbuf);
1372a6aedc5dSRoger Pau Monné 	dir            = NULL;
1373a6aedc5dSRoger Pau Monné 	local_xbt.id   = 0;
1374a6aedc5dSRoger Pau Monné 
1375a6aedc5dSRoger Pau Monné 	if (xbt.id == 0) {
1376a6aedc5dSRoger Pau Monné 		error = xs_transaction_start(&local_xbt);
1377a6aedc5dSRoger Pau Monné 		if (error != 0)
1378a6aedc5dSRoger Pau Monné 			goto out;
1379a6aedc5dSRoger Pau Monné 		xbt = local_xbt;
1380a6aedc5dSRoger Pau Monné 	}
1381a6aedc5dSRoger Pau Monné 
1382a6aedc5dSRoger Pau Monné 	while (1) {
1383a6aedc5dSRoger Pau Monné 		u_int count;
1384a6aedc5dSRoger Pau Monné 		u_int i;
1385a6aedc5dSRoger Pau Monné 
1386a6aedc5dSRoger Pau Monné 		error = xs_directory(xbt, cur_path, "", &count, &dir);
1387a6aedc5dSRoger Pau Monné 		if (error)
1388a6aedc5dSRoger Pau Monné 			goto out;
1389a6aedc5dSRoger Pau Monné 
1390a6aedc5dSRoger Pau Monné 		for (i = 0; i < count; i++) {
1391a6aedc5dSRoger Pau Monné 			error = xs_rm(xbt, cur_path, dir[i]);
1392a6aedc5dSRoger Pau Monné 			if (error == ENOTEMPTY) {
1393a6aedc5dSRoger Pau Monné 				struct sbuf *push_dir;
1394a6aedc5dSRoger Pau Monné 
1395a6aedc5dSRoger Pau Monné 				/*
1396a6aedc5dSRoger Pau Monné 				 * Descend to clear out this sub directory.
1397a6aedc5dSRoger Pau Monné 				 * We'll return to cur_dir once push_dir
1398a6aedc5dSRoger Pau Monné 				 * is empty.
1399a6aedc5dSRoger Pau Monné 				 */
1400a6aedc5dSRoger Pau Monné 				push_dir = xs_join(cur_path, dir[i]);
1401a6aedc5dSRoger Pau Monné 				sbuf_delete(cur_path_sbuf);
1402a6aedc5dSRoger Pau Monné 				cur_path_sbuf = push_dir;
1403a6aedc5dSRoger Pau Monné 				cur_path = sbuf_data(cur_path_sbuf);
1404a6aedc5dSRoger Pau Monné 				break;
1405a6aedc5dSRoger Pau Monné 			} else if (error != 0) {
1406a6aedc5dSRoger Pau Monné 				goto out;
1407a6aedc5dSRoger Pau Monné 			}
1408a6aedc5dSRoger Pau Monné 		}
1409a6aedc5dSRoger Pau Monné 
1410a6aedc5dSRoger Pau Monné 		free(dir, M_XENSTORE);
1411a6aedc5dSRoger Pau Monné 		dir = NULL;
1412a6aedc5dSRoger Pau Monné 
1413a6aedc5dSRoger Pau Monné 		if (i == count) {
1414a6aedc5dSRoger Pau Monné 			char *last_slash;
1415a6aedc5dSRoger Pau Monné 
1416a6aedc5dSRoger Pau Monné 			/* Directory is empty.  It is now safe to remove. */
1417a6aedc5dSRoger Pau Monné 			error = xs_rm(xbt, cur_path, "");
1418a6aedc5dSRoger Pau Monné 			if (error != 0)
1419a6aedc5dSRoger Pau Monné 				goto out;
1420a6aedc5dSRoger Pau Monné 
1421a6aedc5dSRoger Pau Monné 			if (!strcmp(cur_path, root_path))
1422a6aedc5dSRoger Pau Monné 				break;
1423a6aedc5dSRoger Pau Monné 
1424a6aedc5dSRoger Pau Monné 			/* Return to processing the parent directory. */
1425a6aedc5dSRoger Pau Monné 			last_slash = strrchr(cur_path, '/');
1426a6aedc5dSRoger Pau Monné 			KASSERT(last_slash != NULL,
1427a6aedc5dSRoger Pau Monné 				("xs_rm_tree: mangled path %s", cur_path));
1428a6aedc5dSRoger Pau Monné 			*last_slash = '\0';
1429a6aedc5dSRoger Pau Monné 		}
1430a6aedc5dSRoger Pau Monné 	}
1431a6aedc5dSRoger Pau Monné 
1432a6aedc5dSRoger Pau Monné out:
1433a6aedc5dSRoger Pau Monné 	sbuf_delete(cur_path_sbuf);
1434a6aedc5dSRoger Pau Monné 	sbuf_delete(root_path_sbuf);
1435a6aedc5dSRoger Pau Monné 	if (dir != NULL)
1436a6aedc5dSRoger Pau Monné 		free(dir, M_XENSTORE);
1437a6aedc5dSRoger Pau Monné 
1438a6aedc5dSRoger Pau Monné 	if (local_xbt.id != 0) {
1439a6aedc5dSRoger Pau Monné 		int terror;
1440a6aedc5dSRoger Pau Monné 
1441a6aedc5dSRoger Pau Monné 		terror = xs_transaction_end(local_xbt, /*abort*/error != 0);
1442a6aedc5dSRoger Pau Monné 		xbt.id = 0;
1443a6aedc5dSRoger Pau Monné 		if (terror == EAGAIN && error == 0)
1444a6aedc5dSRoger Pau Monné 			goto retry;
1445a6aedc5dSRoger Pau Monné 	}
1446a6aedc5dSRoger Pau Monné 	return (error);
1447a6aedc5dSRoger Pau Monné }
1448a6aedc5dSRoger Pau Monné 
1449a6aedc5dSRoger Pau Monné int
1450a6aedc5dSRoger Pau Monné xs_transaction_start(struct xs_transaction *t)
1451a6aedc5dSRoger Pau Monné {
1452a6aedc5dSRoger Pau Monné 	char *id_str;
1453a6aedc5dSRoger Pau Monné 	int error;
1454a6aedc5dSRoger Pau Monné 
1455a6aedc5dSRoger Pau Monné 	error = xs_single(XST_NIL, XS_TRANSACTION_START, "", NULL,
1456a6aedc5dSRoger Pau Monné 	    (void **)&id_str);
1457a6aedc5dSRoger Pau Monné 	if (error == 0) {
1458a6aedc5dSRoger Pau Monné 		t->id = strtoul(id_str, NULL, 0);
1459a6aedc5dSRoger Pau Monné 		free(id_str, M_XENSTORE);
1460a6aedc5dSRoger Pau Monné 	}
1461a6aedc5dSRoger Pau Monné 	return (error);
1462a6aedc5dSRoger Pau Monné }
1463a6aedc5dSRoger Pau Monné 
1464a6aedc5dSRoger Pau Monné int
1465a6aedc5dSRoger Pau Monné xs_transaction_end(struct xs_transaction t, int abort)
1466a6aedc5dSRoger Pau Monné {
1467a6aedc5dSRoger Pau Monné 	char abortstr[2];
1468a6aedc5dSRoger Pau Monné 
1469a6aedc5dSRoger Pau Monné 	if (abort)
1470a6aedc5dSRoger Pau Monné 		strcpy(abortstr, "F");
1471a6aedc5dSRoger Pau Monné 	else
1472a6aedc5dSRoger Pau Monné 		strcpy(abortstr, "T");
1473a6aedc5dSRoger Pau Monné 
1474a6aedc5dSRoger Pau Monné 	return (xs_single(t, XS_TRANSACTION_END, abortstr, NULL, NULL));
1475a6aedc5dSRoger Pau Monné }
1476a6aedc5dSRoger Pau Monné 
1477a6aedc5dSRoger Pau Monné int
1478a6aedc5dSRoger Pau Monné xs_scanf(struct xs_transaction t, const char *dir, const char *node,
1479a6aedc5dSRoger Pau Monné      int *scancountp, const char *fmt, ...)
1480a6aedc5dSRoger Pau Monné {
1481a6aedc5dSRoger Pau Monné 	va_list ap;
1482a6aedc5dSRoger Pau Monné 	int error, ns;
1483a6aedc5dSRoger Pau Monné 	char *val;
1484a6aedc5dSRoger Pau Monné 
1485a6aedc5dSRoger Pau Monné 	error = xs_read(t, dir, node, NULL, (void **) &val);
1486a6aedc5dSRoger Pau Monné 	if (error)
1487a6aedc5dSRoger Pau Monné 		return (error);
1488a6aedc5dSRoger Pau Monné 
1489a6aedc5dSRoger Pau Monné 	va_start(ap, fmt);
1490a6aedc5dSRoger Pau Monné 	ns = vsscanf(val, fmt, ap);
1491a6aedc5dSRoger Pau Monné 	va_end(ap);
1492a6aedc5dSRoger Pau Monné 	free(val, M_XENSTORE);
1493a6aedc5dSRoger Pau Monné 	/* Distinctive errno. */
1494a6aedc5dSRoger Pau Monné 	if (ns == 0)
1495a6aedc5dSRoger Pau Monné 		return (ERANGE);
1496a6aedc5dSRoger Pau Monné 	if (scancountp)
1497a6aedc5dSRoger Pau Monné 		*scancountp = ns;
1498a6aedc5dSRoger Pau Monné 	return (0);
1499a6aedc5dSRoger Pau Monné }
1500a6aedc5dSRoger Pau Monné 
1501a6aedc5dSRoger Pau Monné int
1502a6aedc5dSRoger Pau Monné xs_vprintf(struct xs_transaction t,
1503a6aedc5dSRoger Pau Monné     const char *dir, const char *node, const char *fmt, va_list ap)
1504a6aedc5dSRoger Pau Monné {
1505a6aedc5dSRoger Pau Monné 	struct sbuf *sb;
1506a6aedc5dSRoger Pau Monné 	int error;
1507a6aedc5dSRoger Pau Monné 
1508a6aedc5dSRoger Pau Monné 	sb = sbuf_new_auto();
1509a6aedc5dSRoger Pau Monné 	sbuf_vprintf(sb, fmt, ap);
1510a6aedc5dSRoger Pau Monné 	sbuf_finish(sb);
1511a6aedc5dSRoger Pau Monné 	error = xs_write(t, dir, node, sbuf_data(sb));
1512a6aedc5dSRoger Pau Monné 	sbuf_delete(sb);
1513a6aedc5dSRoger Pau Monné 
1514a6aedc5dSRoger Pau Monné 	return (error);
1515a6aedc5dSRoger Pau Monné }
1516a6aedc5dSRoger Pau Monné 
1517a6aedc5dSRoger Pau Monné int
1518a6aedc5dSRoger Pau Monné xs_printf(struct xs_transaction t, const char *dir, const char *node,
1519a6aedc5dSRoger Pau Monné      const char *fmt, ...)
1520a6aedc5dSRoger Pau Monné {
1521a6aedc5dSRoger Pau Monné 	va_list ap;
1522a6aedc5dSRoger Pau Monné 	int error;
1523a6aedc5dSRoger Pau Monné 
1524a6aedc5dSRoger Pau Monné 	va_start(ap, fmt);
1525a6aedc5dSRoger Pau Monné 	error = xs_vprintf(t, dir, node, fmt, ap);
1526a6aedc5dSRoger Pau Monné 	va_end(ap);
1527a6aedc5dSRoger Pau Monné 
1528a6aedc5dSRoger Pau Monné 	return (error);
1529a6aedc5dSRoger Pau Monné }
1530a6aedc5dSRoger Pau Monné 
1531a6aedc5dSRoger Pau Monné int
1532a6aedc5dSRoger Pau Monné xs_gather(struct xs_transaction t, const char *dir, ...)
1533a6aedc5dSRoger Pau Monné {
1534a6aedc5dSRoger Pau Monné 	va_list ap;
1535a6aedc5dSRoger Pau Monné 	const char *name;
1536a6aedc5dSRoger Pau Monné 	int error;
1537a6aedc5dSRoger Pau Monné 
1538a6aedc5dSRoger Pau Monné 	va_start(ap, dir);
1539a6aedc5dSRoger Pau Monné 	error = 0;
1540a6aedc5dSRoger Pau Monné 	while (error == 0 && (name = va_arg(ap, char *)) != NULL) {
1541a6aedc5dSRoger Pau Monné 		const char *fmt = va_arg(ap, char *);
1542a6aedc5dSRoger Pau Monné 		void *result = va_arg(ap, void *);
1543a6aedc5dSRoger Pau Monné 		char *p;
1544a6aedc5dSRoger Pau Monné 
1545a6aedc5dSRoger Pau Monné 		error = xs_read(t, dir, name, NULL, (void **) &p);
1546a6aedc5dSRoger Pau Monné 		if (error)
1547a6aedc5dSRoger Pau Monné 			break;
1548a6aedc5dSRoger Pau Monné 
1549a6aedc5dSRoger Pau Monné 		if (fmt) {
1550a6aedc5dSRoger Pau Monné 			if (sscanf(p, fmt, result) == 0)
1551a6aedc5dSRoger Pau Monné 				error = EINVAL;
1552a6aedc5dSRoger Pau Monné 			free(p, M_XENSTORE);
1553a6aedc5dSRoger Pau Monné 		} else
1554a6aedc5dSRoger Pau Monné 			*(char **)result = p;
1555a6aedc5dSRoger Pau Monné 	}
1556a6aedc5dSRoger Pau Monné 	va_end(ap);
1557a6aedc5dSRoger Pau Monné 
1558a6aedc5dSRoger Pau Monné 	return (error);
1559a6aedc5dSRoger Pau Monné }
1560a6aedc5dSRoger Pau Monné 
1561a6aedc5dSRoger Pau Monné int
1562a6aedc5dSRoger Pau Monné xs_register_watch(struct xs_watch *watch)
1563a6aedc5dSRoger Pau Monné {
1564a6aedc5dSRoger Pau Monné 	/* Pointer in ascii is the token. */
1565a6aedc5dSRoger Pau Monné 	char token[sizeof(watch) * 2 + 1];
1566a6aedc5dSRoger Pau Monné 	int error;
1567a6aedc5dSRoger Pau Monné 
1568a6aedc5dSRoger Pau Monné 	sprintf(token, "%lX", (long)watch);
1569a6aedc5dSRoger Pau Monné 
1570a6aedc5dSRoger Pau Monné 	mtx_lock(&xs.registered_watches_lock);
1571a6aedc5dSRoger Pau Monné 	KASSERT(find_watch(token) == NULL, ("watch already registered"));
1572a6aedc5dSRoger Pau Monné 	LIST_INSERT_HEAD(&xs.registered_watches, watch, list);
1573a6aedc5dSRoger Pau Monné 	mtx_unlock(&xs.registered_watches_lock);
1574a6aedc5dSRoger Pau Monné 
1575a6aedc5dSRoger Pau Monné 	error = xs_watch(watch->node, token);
1576a6aedc5dSRoger Pau Monné 
1577a6aedc5dSRoger Pau Monné 	/* Ignore errors due to multiple registration. */
1578a6aedc5dSRoger Pau Monné 	if (error == EEXIST)
1579a6aedc5dSRoger Pau Monné 		error = 0;
1580a6aedc5dSRoger Pau Monné 
1581a6aedc5dSRoger Pau Monné 	if (error != 0) {
1582a6aedc5dSRoger Pau Monné 		mtx_lock(&xs.registered_watches_lock);
1583a6aedc5dSRoger Pau Monné 		LIST_REMOVE(watch, list);
1584a6aedc5dSRoger Pau Monné 		mtx_unlock(&xs.registered_watches_lock);
1585a6aedc5dSRoger Pau Monné 	}
1586a6aedc5dSRoger Pau Monné 
1587a6aedc5dSRoger Pau Monné 	return (error);
1588a6aedc5dSRoger Pau Monné }
1589a6aedc5dSRoger Pau Monné 
1590a6aedc5dSRoger Pau Monné void
1591a6aedc5dSRoger Pau Monné xs_unregister_watch(struct xs_watch *watch)
1592a6aedc5dSRoger Pau Monné {
1593a6aedc5dSRoger Pau Monné 	struct xs_stored_msg *msg, *tmp;
1594a6aedc5dSRoger Pau Monné 	char token[sizeof(watch) * 2 + 1];
1595a6aedc5dSRoger Pau Monné 	int error;
1596a6aedc5dSRoger Pau Monné 
1597a6aedc5dSRoger Pau Monné 	sprintf(token, "%lX", (long)watch);
1598a6aedc5dSRoger Pau Monné 
1599a6aedc5dSRoger Pau Monné 	mtx_lock(&xs.registered_watches_lock);
1600a6aedc5dSRoger Pau Monné 	if (find_watch(token) == NULL) {
1601a6aedc5dSRoger Pau Monné 		mtx_unlock(&xs.registered_watches_lock);
1602a6aedc5dSRoger Pau Monné 		return;
1603a6aedc5dSRoger Pau Monné 	}
1604a6aedc5dSRoger Pau Monné 	LIST_REMOVE(watch, list);
1605a6aedc5dSRoger Pau Monné 	mtx_unlock(&xs.registered_watches_lock);
1606a6aedc5dSRoger Pau Monné 
1607a6aedc5dSRoger Pau Monné 	error = xs_unwatch(watch->node, token);
1608a6aedc5dSRoger Pau Monné 	if (error)
1609a6aedc5dSRoger Pau Monné 		log(LOG_WARNING, "XENSTORE Failed to release watch %s: %i\n",
1610a6aedc5dSRoger Pau Monné 		    watch->node, error);
1611a6aedc5dSRoger Pau Monné 
1612a6aedc5dSRoger Pau Monné 	/* Cancel pending watch events. */
1613a6aedc5dSRoger Pau Monné 	mtx_lock(&xs.watch_events_lock);
1614a6aedc5dSRoger Pau Monné 	TAILQ_FOREACH_SAFE(msg, &xs.watch_events, list, tmp) {
1615a6aedc5dSRoger Pau Monné 		if (msg->u.watch.handle != watch)
1616a6aedc5dSRoger Pau Monné 			continue;
1617a6aedc5dSRoger Pau Monné 		TAILQ_REMOVE(&xs.watch_events, msg, list);
1618a6aedc5dSRoger Pau Monné 		free(msg->u.watch.vec, M_XENSTORE);
1619a6aedc5dSRoger Pau Monné 		free(msg, M_XENSTORE);
1620a6aedc5dSRoger Pau Monné 	}
1621a6aedc5dSRoger Pau Monné 	mtx_unlock(&xs.watch_events_lock);
1622a6aedc5dSRoger Pau Monné 
1623a6aedc5dSRoger Pau Monné 	/* Flush any currently-executing callback, unless we are it. :-) */
1624a6aedc5dSRoger Pau Monné 	if (curproc->p_pid != xs.xenwatch_pid) {
1625a6aedc5dSRoger Pau Monné 		sx_xlock(&xs.xenwatch_mutex);
1626a6aedc5dSRoger Pau Monné 		sx_xunlock(&xs.xenwatch_mutex);
1627a6aedc5dSRoger Pau Monné 	}
1628a6aedc5dSRoger Pau Monné }
162941716b8dSRoger Pau Monné 
163041716b8dSRoger Pau Monné void
163141716b8dSRoger Pau Monné xs_lock(void)
163241716b8dSRoger Pau Monné {
163341716b8dSRoger Pau Monné 
163441716b8dSRoger Pau Monné 	sx_xlock(&xs.request_mutex);
163541716b8dSRoger Pau Monné 	return;
163641716b8dSRoger Pau Monné }
163741716b8dSRoger Pau Monné 
163841716b8dSRoger Pau Monné void
163941716b8dSRoger Pau Monné xs_unlock(void)
164041716b8dSRoger Pau Monné {
164141716b8dSRoger Pau Monné 
164241716b8dSRoger Pau Monné 	sx_xunlock(&xs.request_mutex);
164341716b8dSRoger Pau Monné 	return;
164441716b8dSRoger Pau Monné }
164541716b8dSRoger Pau Monné 
1646