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