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é 59a6aedc5dSRoger Pau Monné #include <xen/interface/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); 659*4e4e43dcSRoger Pau Monné if (msg->u.watch.handle != NULL && 660*4e4e43dcSRoger Pau Monné (!msg->u.watch.handle->max_pending || 661*4e4e43dcSRoger Pau Monné msg->u.watch.handle->pending < 662*4e4e43dcSRoger Pau Monné msg->u.watch.handle->max_pending)) { 663*4e4e43dcSRoger 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 { 668*4e4e43dcSRoger 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é uint32_t request_type; 791a6aedc5dSRoger Pau Monné int error; 792a6aedc5dSRoger Pau Monné 793a6aedc5dSRoger Pau Monné request_type = msg->type; 794a6aedc5dSRoger Pau Monné 795a6aedc5dSRoger Pau Monné sx_xlock(&xs.request_mutex); 796a6aedc5dSRoger Pau Monné if ((error = xs_write_store(msg, sizeof(*msg) + msg->len)) == 0) 797a6aedc5dSRoger Pau Monné error = xs_read_reply(&msg->type, &msg->len, result); 798a6aedc5dSRoger Pau Monné sx_xunlock(&xs.request_mutex); 799a6aedc5dSRoger Pau Monné 800a6aedc5dSRoger Pau Monné return (error); 801a6aedc5dSRoger Pau Monné } 802a6aedc5dSRoger Pau Monné 803a6aedc5dSRoger Pau Monné /** 804a6aedc5dSRoger Pau Monné * Send a message with an optionally muti-part body to the XenStore service. 805a6aedc5dSRoger Pau Monné * 806a6aedc5dSRoger Pau Monné * \param t The transaction to use for this request. 807a6aedc5dSRoger Pau Monné * \param request_type The type of message to send. 808a6aedc5dSRoger Pau Monné * \param iovec Pointers to the body sections of the request. 809a6aedc5dSRoger Pau Monné * \param num_vecs The number of body sections in the request. 810a6aedc5dSRoger Pau Monné * \param len The returned length of the reply. 811a6aedc5dSRoger Pau Monné * \param result The returned body of the reply. 812a6aedc5dSRoger Pau Monné * 813a6aedc5dSRoger Pau Monné * \return 0 on success. Otherwise an errno indicating 814a6aedc5dSRoger Pau Monné * the cause of failure. 815a6aedc5dSRoger Pau Monné * 816a6aedc5dSRoger Pau Monné * \note The returned result is provided in malloced storage and thus 817a6aedc5dSRoger Pau Monné * must be free'd by the caller with 'free(*result, M_XENSTORE); 818a6aedc5dSRoger Pau Monné */ 819a6aedc5dSRoger Pau Monné static int 820a6aedc5dSRoger Pau Monné xs_talkv(struct xs_transaction t, enum xsd_sockmsg_type request_type, 821a6aedc5dSRoger Pau Monné const struct iovec *iovec, u_int num_vecs, u_int *len, void **result) 822a6aedc5dSRoger Pau Monné { 823a6aedc5dSRoger Pau Monné struct xsd_sockmsg msg; 824a6aedc5dSRoger Pau Monné void *ret = NULL; 825a6aedc5dSRoger Pau Monné u_int i; 826a6aedc5dSRoger Pau Monné int error; 827a6aedc5dSRoger Pau Monné 828a6aedc5dSRoger Pau Monné msg.tx_id = t.id; 829a6aedc5dSRoger Pau Monné msg.req_id = 0; 830a6aedc5dSRoger Pau Monné msg.type = request_type; 831a6aedc5dSRoger Pau Monné msg.len = 0; 832a6aedc5dSRoger Pau Monné for (i = 0; i < num_vecs; i++) 833a6aedc5dSRoger Pau Monné msg.len += iovec[i].iov_len; 834a6aedc5dSRoger Pau Monné 835a6aedc5dSRoger Pau Monné sx_xlock(&xs.request_mutex); 836a6aedc5dSRoger Pau Monné error = xs_write_store(&msg, sizeof(msg)); 837a6aedc5dSRoger Pau Monné if (error) { 838a6aedc5dSRoger Pau Monné printf("xs_talkv failed %d\n", error); 839a6aedc5dSRoger Pau Monné goto error_lock_held; 840a6aedc5dSRoger Pau Monné } 841a6aedc5dSRoger Pau Monné 842a6aedc5dSRoger Pau Monné for (i = 0; i < num_vecs; i++) { 843a6aedc5dSRoger Pau Monné error = xs_write_store(iovec[i].iov_base, iovec[i].iov_len); 844a6aedc5dSRoger Pau Monné if (error) { 845a6aedc5dSRoger Pau Monné printf("xs_talkv failed %d\n", error); 846a6aedc5dSRoger Pau Monné goto error_lock_held; 847a6aedc5dSRoger Pau Monné } 848a6aedc5dSRoger Pau Monné } 849a6aedc5dSRoger Pau Monné 850a6aedc5dSRoger Pau Monné error = xs_read_reply(&msg.type, len, &ret); 851a6aedc5dSRoger Pau Monné 852a6aedc5dSRoger Pau Monné error_lock_held: 853a6aedc5dSRoger Pau Monné sx_xunlock(&xs.request_mutex); 854a6aedc5dSRoger Pau Monné if (error) 855a6aedc5dSRoger Pau Monné return (error); 856a6aedc5dSRoger Pau Monné 857a6aedc5dSRoger Pau Monné if (msg.type == XS_ERROR) { 858a6aedc5dSRoger Pau Monné error = xs_get_error(ret); 859a6aedc5dSRoger Pau Monné free(ret, M_XENSTORE); 860a6aedc5dSRoger Pau Monné return (error); 861a6aedc5dSRoger Pau Monné } 862a6aedc5dSRoger Pau Monné 863a6aedc5dSRoger Pau Monné /* Reply is either error or an echo of our request message type. */ 864a6aedc5dSRoger Pau Monné KASSERT(msg.type == request_type, ("bad xenstore message type")); 865a6aedc5dSRoger Pau Monné 866a6aedc5dSRoger Pau Monné if (result) 867a6aedc5dSRoger Pau Monné *result = ret; 868a6aedc5dSRoger Pau Monné else 869a6aedc5dSRoger Pau Monné free(ret, M_XENSTORE); 870a6aedc5dSRoger Pau Monné 871a6aedc5dSRoger Pau Monné return (0); 872a6aedc5dSRoger Pau Monné } 873a6aedc5dSRoger Pau Monné 874a6aedc5dSRoger Pau Monné /** 875a6aedc5dSRoger Pau Monné * Wrapper for xs_talkv allowing easy transmission of a message with 876a6aedc5dSRoger Pau Monné * a single, contiguous, message body. 877a6aedc5dSRoger Pau Monné * 878a6aedc5dSRoger Pau Monné * \param t The transaction to use for this request. 879a6aedc5dSRoger Pau Monné * \param request_type The type of message to send. 880a6aedc5dSRoger Pau Monné * \param body The body of the request. 881a6aedc5dSRoger Pau Monné * \param len The returned length of the reply. 882a6aedc5dSRoger Pau Monné * \param result The returned body of the reply. 883a6aedc5dSRoger Pau Monné * 884a6aedc5dSRoger Pau Monné * \return 0 on success. Otherwise an errno indicating 885a6aedc5dSRoger Pau Monné * the cause of failure. 886a6aedc5dSRoger Pau Monné * 887a6aedc5dSRoger Pau Monné * \note The returned result is provided in malloced storage and thus 888a6aedc5dSRoger Pau Monné * must be free'd by the caller with 'free(*result, M_XENSTORE); 889a6aedc5dSRoger Pau Monné */ 890a6aedc5dSRoger Pau Monné static int 891a6aedc5dSRoger Pau Monné xs_single(struct xs_transaction t, enum xsd_sockmsg_type request_type, 892a6aedc5dSRoger Pau Monné const char *body, u_int *len, void **result) 893a6aedc5dSRoger Pau Monné { 894a6aedc5dSRoger Pau Monné struct iovec iovec; 895a6aedc5dSRoger Pau Monné 896a6aedc5dSRoger Pau Monné iovec.iov_base = (void *)(uintptr_t)body; 897a6aedc5dSRoger Pau Monné iovec.iov_len = strlen(body) + 1; 898a6aedc5dSRoger Pau Monné 899a6aedc5dSRoger Pau Monné return (xs_talkv(t, request_type, &iovec, 1, len, result)); 900a6aedc5dSRoger Pau Monné } 901a6aedc5dSRoger Pau Monné 902a6aedc5dSRoger Pau Monné /*------------------------- XenStore Watch Support ---------------------------*/ 903a6aedc5dSRoger Pau Monné /** 904a6aedc5dSRoger Pau Monné * Transmit a watch request to the XenStore service. 905a6aedc5dSRoger Pau Monné * 906a6aedc5dSRoger Pau Monné * \param path The path in the XenStore to watch. 907a6aedc5dSRoger Pau Monné * \param tocken A unique identifier for this watch. 908a6aedc5dSRoger Pau Monné * 909a6aedc5dSRoger Pau Monné * \return 0 on success. Otherwise an errno indicating the 910a6aedc5dSRoger Pau Monné * cause of failure. 911a6aedc5dSRoger Pau Monné */ 912a6aedc5dSRoger Pau Monné static int 913a6aedc5dSRoger Pau Monné xs_watch(const char *path, const char *token) 914a6aedc5dSRoger Pau Monné { 915a6aedc5dSRoger Pau Monné struct iovec iov[2]; 916a6aedc5dSRoger Pau Monné 917a6aedc5dSRoger Pau Monné iov[0].iov_base = (void *)(uintptr_t) path; 918a6aedc5dSRoger Pau Monné iov[0].iov_len = strlen(path) + 1; 919a6aedc5dSRoger Pau Monné iov[1].iov_base = (void *)(uintptr_t) token; 920a6aedc5dSRoger Pau Monné iov[1].iov_len = strlen(token) + 1; 921a6aedc5dSRoger Pau Monné 922a6aedc5dSRoger Pau Monné return (xs_talkv(XST_NIL, XS_WATCH, iov, 2, NULL, NULL)); 923a6aedc5dSRoger Pau Monné } 924a6aedc5dSRoger Pau Monné 925a6aedc5dSRoger Pau Monné /** 926a6aedc5dSRoger Pau Monné * Transmit an uwatch request to the XenStore service. 927a6aedc5dSRoger Pau Monné * 928a6aedc5dSRoger Pau Monné * \param path The path in the XenStore to watch. 929a6aedc5dSRoger Pau Monné * \param tocken A unique identifier for this watch. 930a6aedc5dSRoger Pau Monné * 931a6aedc5dSRoger Pau Monné * \return 0 on success. Otherwise an errno indicating the 932a6aedc5dSRoger Pau Monné * cause of failure. 933a6aedc5dSRoger Pau Monné */ 934a6aedc5dSRoger Pau Monné static int 935a6aedc5dSRoger Pau Monné xs_unwatch(const char *path, const char *token) 936a6aedc5dSRoger Pau Monné { 937a6aedc5dSRoger Pau Monné struct iovec iov[2]; 938a6aedc5dSRoger Pau Monné 939a6aedc5dSRoger Pau Monné iov[0].iov_base = (void *)(uintptr_t) path; 940a6aedc5dSRoger Pau Monné iov[0].iov_len = strlen(path) + 1; 941a6aedc5dSRoger Pau Monné iov[1].iov_base = (void *)(uintptr_t) token; 942a6aedc5dSRoger Pau Monné iov[1].iov_len = strlen(token) + 1; 943a6aedc5dSRoger Pau Monné 944a6aedc5dSRoger Pau Monné return (xs_talkv(XST_NIL, XS_UNWATCH, iov, 2, NULL, NULL)); 945a6aedc5dSRoger Pau Monné } 946a6aedc5dSRoger Pau Monné 947a6aedc5dSRoger Pau Monné /** 948a6aedc5dSRoger Pau Monné * Convert from watch token (unique identifier) to the associated 949a6aedc5dSRoger Pau Monné * internal tracking structure for this watch. 950a6aedc5dSRoger Pau Monné * 951a6aedc5dSRoger Pau Monné * \param tocken The unique identifier for the watch to find. 952a6aedc5dSRoger Pau Monné * 953a6aedc5dSRoger Pau Monné * \return A pointer to the found watch structure or NULL. 954a6aedc5dSRoger Pau Monné */ 955a6aedc5dSRoger Pau Monné static struct xs_watch * 956a6aedc5dSRoger Pau Monné find_watch(const char *token) 957a6aedc5dSRoger Pau Monné { 958a6aedc5dSRoger Pau Monné struct xs_watch *i, *cmp; 959a6aedc5dSRoger Pau Monné 960a6aedc5dSRoger Pau Monné cmp = (void *)strtoul(token, NULL, 16); 961a6aedc5dSRoger Pau Monné 962a6aedc5dSRoger Pau Monné LIST_FOREACH(i, &xs.registered_watches, list) 963a6aedc5dSRoger Pau Monné if (i == cmp) 964a6aedc5dSRoger Pau Monné return (i); 965a6aedc5dSRoger Pau Monné 966a6aedc5dSRoger Pau Monné return (NULL); 967a6aedc5dSRoger Pau Monné } 968a6aedc5dSRoger Pau Monné 969a6aedc5dSRoger Pau Monné /** 970a6aedc5dSRoger Pau Monné * Thread body of the XenStore watch event dispatch thread. 971a6aedc5dSRoger Pau Monné */ 972a6aedc5dSRoger Pau Monné static void 973a6aedc5dSRoger Pau Monné xenwatch_thread(void *unused) 974a6aedc5dSRoger Pau Monné { 975a6aedc5dSRoger Pau Monné struct xs_stored_msg *msg; 976a6aedc5dSRoger Pau Monné 977a6aedc5dSRoger Pau Monné for (;;) { 978a6aedc5dSRoger Pau Monné mtx_lock(&xs.watch_events_lock); 979a6aedc5dSRoger Pau Monné while (TAILQ_EMPTY(&xs.watch_events)) 980a6aedc5dSRoger Pau Monné mtx_sleep(&xs.watch_events, 981a6aedc5dSRoger Pau Monné &xs.watch_events_lock, 982a6aedc5dSRoger Pau Monné PWAIT | PCATCH, "waitev", hz/10); 983a6aedc5dSRoger Pau Monné 984a6aedc5dSRoger Pau Monné mtx_unlock(&xs.watch_events_lock); 985a6aedc5dSRoger Pau Monné sx_xlock(&xs.xenwatch_mutex); 986a6aedc5dSRoger Pau Monné 987a6aedc5dSRoger Pau Monné mtx_lock(&xs.watch_events_lock); 988a6aedc5dSRoger Pau Monné msg = TAILQ_FIRST(&xs.watch_events); 989*4e4e43dcSRoger Pau Monné if (msg) { 990a6aedc5dSRoger Pau Monné TAILQ_REMOVE(&xs.watch_events, msg, list); 991*4e4e43dcSRoger Pau Monné msg->u.watch.handle->pending--; 992*4e4e43dcSRoger Pau Monné } 993a6aedc5dSRoger Pau Monné mtx_unlock(&xs.watch_events_lock); 994a6aedc5dSRoger Pau Monné 995a6aedc5dSRoger Pau Monné if (msg != NULL) { 996a6aedc5dSRoger Pau Monné /* 997a6aedc5dSRoger Pau Monné * XXX There are messages coming in with a NULL 998a6aedc5dSRoger Pau Monné * XXX callback. This deserves further investigation; 999a6aedc5dSRoger Pau Monné * XXX the workaround here simply prevents the kernel 1000a6aedc5dSRoger Pau Monné * XXX from panic'ing on startup. 1001a6aedc5dSRoger Pau Monné */ 1002a6aedc5dSRoger Pau Monné if (msg->u.watch.handle->callback != NULL) 1003a6aedc5dSRoger Pau Monné msg->u.watch.handle->callback( 1004a6aedc5dSRoger Pau Monné msg->u.watch.handle, 1005a6aedc5dSRoger Pau Monné (const char **)msg->u.watch.vec, 1006a6aedc5dSRoger Pau Monné msg->u.watch.vec_size); 1007a6aedc5dSRoger Pau Monné free(msg->u.watch.vec, M_XENSTORE); 1008a6aedc5dSRoger Pau Monné free(msg, M_XENSTORE); 1009a6aedc5dSRoger Pau Monné } 1010a6aedc5dSRoger Pau Monné 1011a6aedc5dSRoger Pau Monné sx_xunlock(&xs.xenwatch_mutex); 1012a6aedc5dSRoger Pau Monné } 1013a6aedc5dSRoger Pau Monné } 1014a6aedc5dSRoger Pau Monné 1015a6aedc5dSRoger Pau Monné /*----------- XenStore Configuration, Initialization, and Control ------------*/ 1016a6aedc5dSRoger Pau Monné /** 1017a6aedc5dSRoger Pau Monné * Setup communication channels with the XenStore service. 1018a6aedc5dSRoger Pau Monné * 1019a6aedc5dSRoger Pau Monné * \return On success, 0. Otherwise an errno value indicating the 1020a6aedc5dSRoger Pau Monné * type of failure. 1021a6aedc5dSRoger Pau Monné */ 1022a6aedc5dSRoger Pau Monné static int 1023a6aedc5dSRoger Pau Monné xs_init_comms(void) 1024a6aedc5dSRoger Pau Monné { 1025a6aedc5dSRoger Pau Monné int error; 1026a6aedc5dSRoger Pau Monné 1027a6aedc5dSRoger Pau Monné if (xen_store->rsp_prod != xen_store->rsp_cons) { 1028a6aedc5dSRoger Pau Monné log(LOG_WARNING, "XENSTORE response ring is not quiescent " 1029a6aedc5dSRoger Pau Monné "(%08x:%08x): fixing up\n", 1030a6aedc5dSRoger Pau Monné xen_store->rsp_cons, xen_store->rsp_prod); 1031a6aedc5dSRoger Pau Monné xen_store->rsp_cons = xen_store->rsp_prod; 1032a6aedc5dSRoger Pau Monné } 1033a6aedc5dSRoger Pau Monné 1034a6aedc5dSRoger Pau Monné xen_intr_unbind(&xs.xen_intr_handle); 1035a6aedc5dSRoger Pau Monné 1036a6aedc5dSRoger Pau Monné error = xen_intr_bind_local_port(xs.xs_dev, xs.evtchn, 1037a6aedc5dSRoger Pau Monné /*filter*/NULL, xs_intr, /*arg*/NULL, INTR_TYPE_NET|INTR_MPSAFE, 1038a6aedc5dSRoger Pau Monné &xs.xen_intr_handle); 1039a6aedc5dSRoger Pau Monné if (error) { 1040a6aedc5dSRoger Pau Monné log(LOG_WARNING, "XENSTORE request irq failed %i\n", error); 1041a6aedc5dSRoger Pau Monné return (error); 1042a6aedc5dSRoger Pau Monné } 1043a6aedc5dSRoger Pau Monné 1044a6aedc5dSRoger Pau Monné return (0); 1045a6aedc5dSRoger Pau Monné } 1046a6aedc5dSRoger Pau Monné 1047a6aedc5dSRoger Pau Monné /*------------------ Private Device Attachment Functions --------------------*/ 1048a6aedc5dSRoger Pau Monné static void 1049a6aedc5dSRoger Pau Monné xs_identify(driver_t *driver, device_t parent) 1050a6aedc5dSRoger Pau Monné { 1051a6aedc5dSRoger Pau Monné 1052a6aedc5dSRoger Pau Monné BUS_ADD_CHILD(parent, 0, "xenstore", 0); 1053a6aedc5dSRoger Pau Monné } 1054a6aedc5dSRoger Pau Monné 1055a6aedc5dSRoger Pau Monné /** 1056453130d9SPedro F. Giffuni * Probe for the existence of the XenStore. 1057a6aedc5dSRoger Pau Monné * 1058a6aedc5dSRoger Pau Monné * \param dev 1059a6aedc5dSRoger Pau Monné */ 1060a6aedc5dSRoger Pau Monné static int 1061a6aedc5dSRoger Pau Monné xs_probe(device_t dev) 1062a6aedc5dSRoger Pau Monné { 1063a6aedc5dSRoger Pau Monné /* 1064a6aedc5dSRoger Pau Monné * We are either operating within a PV kernel or being probed 1065a6aedc5dSRoger Pau Monné * as the child of the successfully attached xenpci device. 1066a6aedc5dSRoger Pau Monné * Thus we are in a Xen environment and there will be a XenStore. 1067a6aedc5dSRoger Pau Monné * Unconditionally return success. 1068a6aedc5dSRoger Pau Monné */ 1069a6aedc5dSRoger Pau Monné device_set_desc(dev, "XenStore"); 10702f9ec994SRoger Pau Monné return (BUS_PROBE_NOWILDCARD); 1071a6aedc5dSRoger Pau Monné } 1072a6aedc5dSRoger Pau Monné 1073a6aedc5dSRoger Pau Monné static void 1074a6aedc5dSRoger Pau Monné xs_attach_deferred(void *arg) 1075a6aedc5dSRoger Pau Monné { 1076a6aedc5dSRoger Pau Monné 1077a6aedc5dSRoger Pau Monné bus_generic_probe(xs.xs_dev); 1078a6aedc5dSRoger Pau Monné bus_generic_attach(xs.xs_dev); 1079a6aedc5dSRoger Pau Monné 1080a6aedc5dSRoger Pau Monné config_intrhook_disestablish(&xs.xs_attachcb); 1081a6aedc5dSRoger Pau Monné } 1082a6aedc5dSRoger Pau Monné 10831d84e2b3SRoger Pau Monné static void 10841d84e2b3SRoger Pau Monné xs_attach_late(void *arg, int pending) 10851d84e2b3SRoger Pau Monné { 10861d84e2b3SRoger Pau Monné 10871d84e2b3SRoger Pau Monné KASSERT((pending == 1), ("xs late attach queued several times")); 10881d84e2b3SRoger Pau Monné bus_generic_probe(xs.xs_dev); 10891d84e2b3SRoger Pau Monné bus_generic_attach(xs.xs_dev); 10901d84e2b3SRoger Pau Monné } 10911d84e2b3SRoger Pau Monné 1092a6aedc5dSRoger Pau Monné /** 1093a6aedc5dSRoger Pau Monné * Attach to the XenStore. 1094a6aedc5dSRoger Pau Monné * 1095a6aedc5dSRoger Pau Monné * This routine also prepares for the probe/attach of drivers that rely 1096a6aedc5dSRoger Pau Monné * on the XenStore. 1097a6aedc5dSRoger Pau Monné */ 1098a6aedc5dSRoger Pau Monné static int 1099a6aedc5dSRoger Pau Monné xs_attach(device_t dev) 1100a6aedc5dSRoger Pau Monné { 1101a6aedc5dSRoger Pau Monné int error; 1102a6aedc5dSRoger Pau Monné 1103a6aedc5dSRoger Pau Monné /* Allow us to get device_t from softc and vice-versa. */ 1104a6aedc5dSRoger Pau Monné xs.xs_dev = dev; 1105a6aedc5dSRoger Pau Monné device_set_softc(dev, &xs); 1106a6aedc5dSRoger Pau Monné 1107a6aedc5dSRoger Pau Monné /* Initialize the interface to xenstore. */ 1108a6aedc5dSRoger Pau Monné struct proc *p; 1109a6aedc5dSRoger Pau Monné 11101d84e2b3SRoger Pau Monné xs.initialized = false; 1111cfa0b7b8SRoger Pau Monné xs.evtchn = xen_get_xenstore_evtchn(); 1112cfa0b7b8SRoger Pau Monné if (xs.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é xs.evtchn = alloc_unbound.port; 11261d84e2b3SRoger Pau Monné 11271d84e2b3SRoger Pau Monné /* Allocate memory for the xs shared ring */ 1128cfa0b7b8SRoger Pau Monné xen_store = malloc(PAGE_SIZE, M_XENSTORE, M_WAITOK | M_ZERO); 1129cfa0b7b8SRoger Pau Monné xs.gpfn = atop(pmap_kextract((vm_offset_t)xen_store)); 11301d84e2b3SRoger Pau Monné } else { 1131cfa0b7b8SRoger Pau Monné xs.gpfn = xen_get_xenstore_mfn(); 1132cfa0b7b8SRoger Pau Monné xen_store = pmap_mapdev_attr(ptoa(xs.gpfn), PAGE_SIZE, 1133cfa0b7b8SRoger Pau Monné PAT_WRITE_BACK); 11341d84e2b3SRoger Pau Monné xs.initialized = true; 11351d84e2b3SRoger Pau Monné } 1136a6aedc5dSRoger Pau Monné 1137a6aedc5dSRoger Pau Monné TAILQ_INIT(&xs.reply_list); 1138a6aedc5dSRoger Pau Monné TAILQ_INIT(&xs.watch_events); 1139a6aedc5dSRoger Pau Monné 1140a6aedc5dSRoger Pau Monné mtx_init(&xs.ring_lock, "ring lock", NULL, MTX_DEF); 1141a6aedc5dSRoger Pau Monné mtx_init(&xs.reply_lock, "reply lock", NULL, MTX_DEF); 1142a6aedc5dSRoger Pau Monné sx_init(&xs.xenwatch_mutex, "xenwatch"); 1143a6aedc5dSRoger Pau Monné sx_init(&xs.request_mutex, "xenstore request"); 1144a6aedc5dSRoger Pau Monné mtx_init(&xs.registered_watches_lock, "watches", NULL, MTX_DEF); 1145a6aedc5dSRoger Pau Monné mtx_init(&xs.watch_events_lock, "watch events", NULL, MTX_DEF); 1146a6aedc5dSRoger Pau Monné 1147a6aedc5dSRoger Pau Monné /* Initialize the shared memory rings to talk to xenstored */ 1148a6aedc5dSRoger Pau Monné error = xs_init_comms(); 1149a6aedc5dSRoger Pau Monné if (error) 1150a6aedc5dSRoger Pau Monné return (error); 1151a6aedc5dSRoger Pau Monné 1152a6aedc5dSRoger Pau Monné error = kproc_create(xenwatch_thread, NULL, &p, RFHIGHPID, 1153a6aedc5dSRoger Pau Monné 0, "xenwatch"); 1154a6aedc5dSRoger Pau Monné if (error) 1155a6aedc5dSRoger Pau Monné return (error); 1156a6aedc5dSRoger Pau Monné xs.xenwatch_pid = p->p_pid; 1157a6aedc5dSRoger Pau Monné 1158a6aedc5dSRoger Pau Monné error = kproc_create(xs_rcv_thread, NULL, NULL, 1159a6aedc5dSRoger Pau Monné RFHIGHPID, 0, "xenstore_rcv"); 1160a6aedc5dSRoger Pau Monné 1161a6aedc5dSRoger Pau Monné xs.xs_attachcb.ich_func = xs_attach_deferred; 1162a6aedc5dSRoger Pau Monné xs.xs_attachcb.ich_arg = NULL; 11631d84e2b3SRoger Pau Monné if (xs.initialized) { 1164a6aedc5dSRoger Pau Monné config_intrhook_establish(&xs.xs_attachcb); 11651d84e2b3SRoger Pau Monné } else { 11661d84e2b3SRoger Pau Monné TASK_INIT(&xs.xs_late_init, 0, xs_attach_late, NULL); 11671d84e2b3SRoger Pau Monné } 1168a6aedc5dSRoger Pau Monné 1169a6aedc5dSRoger Pau Monné return (error); 1170a6aedc5dSRoger Pau Monné } 1171a6aedc5dSRoger Pau Monné 1172a6aedc5dSRoger Pau Monné /** 1173a6aedc5dSRoger Pau Monné * Prepare for suspension of this VM by halting XenStore access after 1174a6aedc5dSRoger Pau Monné * all transactions and individual requests have completed. 1175a6aedc5dSRoger Pau Monné */ 1176a6aedc5dSRoger Pau Monné static int 1177a6aedc5dSRoger Pau Monné xs_suspend(device_t dev) 1178a6aedc5dSRoger Pau Monné { 1179a6aedc5dSRoger Pau Monné int error; 1180a6aedc5dSRoger Pau Monné 1181a6aedc5dSRoger Pau Monné /* Suspend child Xen devices. */ 1182a6aedc5dSRoger Pau Monné error = bus_generic_suspend(dev); 1183a6aedc5dSRoger Pau Monné if (error != 0) 1184a6aedc5dSRoger Pau Monné return (error); 1185a6aedc5dSRoger Pau Monné 1186a6aedc5dSRoger Pau Monné sx_xlock(&xs.request_mutex); 1187a6aedc5dSRoger Pau Monné 1188a6aedc5dSRoger Pau Monné return (0); 1189a6aedc5dSRoger Pau Monné } 1190a6aedc5dSRoger Pau Monné 1191a6aedc5dSRoger Pau Monné /** 1192a6aedc5dSRoger Pau Monné * Resume XenStore operations after this VM is resumed. 1193a6aedc5dSRoger Pau Monné */ 1194a6aedc5dSRoger Pau Monné static int 1195a6aedc5dSRoger Pau Monné xs_resume(device_t dev __unused) 1196a6aedc5dSRoger Pau Monné { 1197a6aedc5dSRoger Pau Monné struct xs_watch *watch; 1198a6aedc5dSRoger Pau Monné char token[sizeof(watch) * 2 + 1]; 1199a6aedc5dSRoger Pau Monné 1200a6aedc5dSRoger Pau Monné xs_init_comms(); 1201a6aedc5dSRoger Pau Monné 1202a6aedc5dSRoger Pau Monné sx_xunlock(&xs.request_mutex); 1203a6aedc5dSRoger Pau Monné 1204a6aedc5dSRoger Pau Monné /* 12055f8f6646SRoger Pau Monné * NB: since xenstore childs have not been resumed yet, there's 12065f8f6646SRoger Pau Monné * no need to hold any watch mutex. Having clients try to add or 12075f8f6646SRoger Pau Monné * remove watches at this point (before xenstore is resumed) is 12085f8f6646SRoger Pau Monné * clearly a violantion of the resume order. 1209a6aedc5dSRoger Pau Monné */ 1210a6aedc5dSRoger Pau Monné LIST_FOREACH(watch, &xs.registered_watches, list) { 1211a6aedc5dSRoger Pau Monné sprintf(token, "%lX", (long)watch); 1212a6aedc5dSRoger Pau Monné xs_watch(watch->node, token); 1213a6aedc5dSRoger Pau Monné } 1214a6aedc5dSRoger Pau Monné 1215a6aedc5dSRoger Pau Monné /* Resume child Xen devices. */ 1216a6aedc5dSRoger Pau Monné bus_generic_resume(dev); 1217a6aedc5dSRoger Pau Monné 1218a6aedc5dSRoger Pau Monné return (0); 1219a6aedc5dSRoger Pau Monné } 1220a6aedc5dSRoger Pau Monné 1221a6aedc5dSRoger Pau Monné /*-------------------- Private Device Attachment Data -----------------------*/ 1222a6aedc5dSRoger Pau Monné static device_method_t xenstore_methods[] = { 1223a6aedc5dSRoger Pau Monné /* Device interface */ 1224a6aedc5dSRoger Pau Monné DEVMETHOD(device_identify, xs_identify), 1225a6aedc5dSRoger Pau Monné DEVMETHOD(device_probe, xs_probe), 1226a6aedc5dSRoger Pau Monné DEVMETHOD(device_attach, xs_attach), 1227a6aedc5dSRoger Pau Monné DEVMETHOD(device_detach, bus_generic_detach), 1228a6aedc5dSRoger Pau Monné DEVMETHOD(device_shutdown, bus_generic_shutdown), 1229a6aedc5dSRoger Pau Monné DEVMETHOD(device_suspend, xs_suspend), 1230a6aedc5dSRoger Pau Monné DEVMETHOD(device_resume, xs_resume), 1231a6aedc5dSRoger Pau Monné 1232a6aedc5dSRoger Pau Monné /* Bus interface */ 1233a6aedc5dSRoger Pau Monné DEVMETHOD(bus_add_child, bus_generic_add_child), 1234a6aedc5dSRoger Pau Monné DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource), 1235a6aedc5dSRoger Pau Monné DEVMETHOD(bus_release_resource, bus_generic_release_resource), 1236a6aedc5dSRoger Pau Monné DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 1237a6aedc5dSRoger Pau Monné DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 1238a6aedc5dSRoger Pau Monné 1239a6aedc5dSRoger Pau Monné DEVMETHOD_END 1240a6aedc5dSRoger Pau Monné }; 1241a6aedc5dSRoger Pau Monné 1242a6aedc5dSRoger Pau Monné DEFINE_CLASS_0(xenstore, xenstore_driver, xenstore_methods, 0); 1243a6aedc5dSRoger Pau Monné static devclass_t xenstore_devclass; 1244a6aedc5dSRoger Pau Monné 1245a6aedc5dSRoger Pau Monné DRIVER_MODULE(xenstore, xenpv, xenstore_driver, xenstore_devclass, 0, 0); 1246a6aedc5dSRoger Pau Monné 1247a6aedc5dSRoger Pau Monné /*------------------------------- Sysctl Data --------------------------------*/ 1248a6aedc5dSRoger Pau Monné /* XXX Shouldn't the node be somewhere else? */ 124941fc1ce1SPawel Biernacki SYSCTL_NODE(_dev, OID_AUTO, xen, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 125041fc1ce1SPawel Biernacki "Xen"); 1251a6aedc5dSRoger Pau Monné SYSCTL_INT(_dev_xen, OID_AUTO, xsd_port, CTLFLAG_RD, &xs.evtchn, 0, ""); 1252a6aedc5dSRoger Pau Monné SYSCTL_ULONG(_dev_xen, OID_AUTO, xsd_kva, CTLFLAG_RD, (u_long *) &xen_store, 0, ""); 1253a6aedc5dSRoger Pau Monné 1254a6aedc5dSRoger Pau Monné /*-------------------------------- Public API --------------------------------*/ 1255a6aedc5dSRoger Pau Monné /*------- API comments for these methods can be found in xenstorevar.h -------*/ 1256cfa0b7b8SRoger Pau Monné bool 1257cfa0b7b8SRoger Pau Monné xs_initialized(void) 1258cfa0b7b8SRoger Pau Monné { 1259cfa0b7b8SRoger Pau Monné 1260cfa0b7b8SRoger Pau Monné return (xs.initialized); 1261cfa0b7b8SRoger Pau Monné } 1262cfa0b7b8SRoger Pau Monné 1263cfa0b7b8SRoger Pau Monné evtchn_port_t 1264cfa0b7b8SRoger Pau Monné xs_evtchn(void) 1265cfa0b7b8SRoger Pau Monné { 1266cfa0b7b8SRoger Pau Monné 1267cfa0b7b8SRoger Pau Monné return (xs.evtchn); 1268cfa0b7b8SRoger Pau Monné } 1269cfa0b7b8SRoger Pau Monné 1270cfa0b7b8SRoger Pau Monné vm_paddr_t 1271cfa0b7b8SRoger Pau Monné xs_address(void) 1272cfa0b7b8SRoger Pau Monné { 1273cfa0b7b8SRoger Pau Monné 1274cfa0b7b8SRoger Pau Monné return (ptoa(xs.gpfn)); 1275cfa0b7b8SRoger Pau Monné } 1276cfa0b7b8SRoger Pau Monné 1277a6aedc5dSRoger Pau Monné int 1278a6aedc5dSRoger Pau Monné xs_directory(struct xs_transaction t, const char *dir, const char *node, 1279a6aedc5dSRoger Pau Monné u_int *num, const char ***result) 1280a6aedc5dSRoger Pau Monné { 1281a6aedc5dSRoger Pau Monné struct sbuf *path; 1282a6aedc5dSRoger Pau Monné char *strings; 1283a6aedc5dSRoger Pau Monné u_int len = 0; 1284a6aedc5dSRoger Pau Monné int error; 1285a6aedc5dSRoger Pau Monné 1286a6aedc5dSRoger Pau Monné path = xs_join(dir, node); 1287a6aedc5dSRoger Pau Monné error = xs_single(t, XS_DIRECTORY, sbuf_data(path), &len, 1288a6aedc5dSRoger Pau Monné (void **)&strings); 1289a6aedc5dSRoger Pau Monné sbuf_delete(path); 1290a6aedc5dSRoger Pau Monné if (error) 1291a6aedc5dSRoger Pau Monné return (error); 1292a6aedc5dSRoger Pau Monné 1293a6aedc5dSRoger Pau Monné *result = split(strings, len, num); 1294a6aedc5dSRoger Pau Monné 1295a6aedc5dSRoger Pau Monné return (0); 1296a6aedc5dSRoger Pau Monné } 1297a6aedc5dSRoger Pau Monné 1298a6aedc5dSRoger Pau Monné int 1299a6aedc5dSRoger Pau Monné xs_exists(struct xs_transaction t, const char *dir, const char *node) 1300a6aedc5dSRoger Pau Monné { 1301a6aedc5dSRoger Pau Monné const char **d; 1302a6aedc5dSRoger Pau Monné int error, dir_n; 1303a6aedc5dSRoger Pau Monné 1304a6aedc5dSRoger Pau Monné error = xs_directory(t, dir, node, &dir_n, &d); 1305a6aedc5dSRoger Pau Monné if (error) 1306a6aedc5dSRoger Pau Monné return (0); 1307a6aedc5dSRoger Pau Monné free(d, M_XENSTORE); 1308a6aedc5dSRoger Pau Monné return (1); 1309a6aedc5dSRoger Pau Monné } 1310a6aedc5dSRoger Pau Monné 1311a6aedc5dSRoger Pau Monné int 1312a6aedc5dSRoger Pau Monné xs_read(struct xs_transaction t, const char *dir, const char *node, 1313a6aedc5dSRoger Pau Monné u_int *len, void **result) 1314a6aedc5dSRoger Pau Monné { 1315a6aedc5dSRoger Pau Monné struct sbuf *path; 1316a6aedc5dSRoger Pau Monné void *ret; 1317a6aedc5dSRoger Pau Monné int error; 1318a6aedc5dSRoger Pau Monné 1319a6aedc5dSRoger Pau Monné path = xs_join(dir, node); 1320a6aedc5dSRoger Pau Monné error = xs_single(t, XS_READ, sbuf_data(path), len, &ret); 1321a6aedc5dSRoger Pau Monné sbuf_delete(path); 1322a6aedc5dSRoger Pau Monné if (error) 1323a6aedc5dSRoger Pau Monné return (error); 1324a6aedc5dSRoger Pau Monné *result = ret; 1325a6aedc5dSRoger Pau Monné return (0); 1326a6aedc5dSRoger Pau Monné } 1327a6aedc5dSRoger Pau Monné 1328a6aedc5dSRoger Pau Monné int 1329a6aedc5dSRoger Pau Monné xs_write(struct xs_transaction t, const char *dir, const char *node, 1330a6aedc5dSRoger Pau Monné const char *string) 1331a6aedc5dSRoger Pau Monné { 1332a6aedc5dSRoger Pau Monné struct sbuf *path; 1333a6aedc5dSRoger Pau Monné struct iovec iovec[2]; 1334a6aedc5dSRoger Pau Monné int error; 1335a6aedc5dSRoger Pau Monné 1336a6aedc5dSRoger Pau Monné path = xs_join(dir, node); 1337a6aedc5dSRoger Pau Monné 1338a6aedc5dSRoger Pau Monné iovec[0].iov_base = (void *)(uintptr_t) sbuf_data(path); 1339a6aedc5dSRoger Pau Monné iovec[0].iov_len = sbuf_len(path) + 1; 1340a6aedc5dSRoger Pau Monné iovec[1].iov_base = (void *)(uintptr_t) string; 1341a6aedc5dSRoger Pau Monné iovec[1].iov_len = strlen(string); 1342a6aedc5dSRoger Pau Monné 1343a6aedc5dSRoger Pau Monné error = xs_talkv(t, XS_WRITE, iovec, 2, NULL, NULL); 1344a6aedc5dSRoger Pau Monné sbuf_delete(path); 1345a6aedc5dSRoger Pau Monné 1346a6aedc5dSRoger Pau Monné return (error); 1347a6aedc5dSRoger Pau Monné } 1348a6aedc5dSRoger Pau Monné 1349a6aedc5dSRoger Pau Monné int 1350a6aedc5dSRoger Pau Monné xs_mkdir(struct xs_transaction t, const char *dir, const char *node) 1351a6aedc5dSRoger Pau Monné { 1352a6aedc5dSRoger Pau Monné struct sbuf *path; 1353a6aedc5dSRoger Pau Monné int ret; 1354a6aedc5dSRoger Pau Monné 1355a6aedc5dSRoger Pau Monné path = xs_join(dir, node); 1356a6aedc5dSRoger Pau Monné ret = xs_single(t, XS_MKDIR, sbuf_data(path), NULL, NULL); 1357a6aedc5dSRoger Pau Monné sbuf_delete(path); 1358a6aedc5dSRoger Pau Monné 1359a6aedc5dSRoger Pau Monné return (ret); 1360a6aedc5dSRoger Pau Monné } 1361a6aedc5dSRoger Pau Monné 1362a6aedc5dSRoger Pau Monné int 1363a6aedc5dSRoger Pau Monné xs_rm(struct xs_transaction t, const char *dir, const char *node) 1364a6aedc5dSRoger Pau Monné { 1365a6aedc5dSRoger Pau Monné struct sbuf *path; 1366a6aedc5dSRoger Pau Monné int ret; 1367a6aedc5dSRoger Pau Monné 1368a6aedc5dSRoger Pau Monné path = xs_join(dir, node); 1369a6aedc5dSRoger Pau Monné ret = xs_single(t, XS_RM, sbuf_data(path), NULL, NULL); 1370a6aedc5dSRoger Pau Monné sbuf_delete(path); 1371a6aedc5dSRoger Pau Monné 1372a6aedc5dSRoger Pau Monné return (ret); 1373a6aedc5dSRoger Pau Monné } 1374a6aedc5dSRoger Pau Monné 1375a6aedc5dSRoger Pau Monné int 1376a6aedc5dSRoger Pau Monné xs_rm_tree(struct xs_transaction xbt, const char *base, const char *node) 1377a6aedc5dSRoger Pau Monné { 1378a6aedc5dSRoger Pau Monné struct xs_transaction local_xbt; 1379a6aedc5dSRoger Pau Monné struct sbuf *root_path_sbuf; 1380a6aedc5dSRoger Pau Monné struct sbuf *cur_path_sbuf; 1381a6aedc5dSRoger Pau Monné char *root_path; 1382a6aedc5dSRoger Pau Monné char *cur_path; 1383a6aedc5dSRoger Pau Monné const char **dir; 1384a6aedc5dSRoger Pau Monné int error; 1385a6aedc5dSRoger Pau Monné 1386a6aedc5dSRoger Pau Monné retry: 1387a6aedc5dSRoger Pau Monné root_path_sbuf = xs_join(base, node); 1388a6aedc5dSRoger Pau Monné cur_path_sbuf = xs_join(base, node); 1389a6aedc5dSRoger Pau Monné root_path = sbuf_data(root_path_sbuf); 1390a6aedc5dSRoger Pau Monné cur_path = sbuf_data(cur_path_sbuf); 1391a6aedc5dSRoger Pau Monné dir = NULL; 1392a6aedc5dSRoger Pau Monné local_xbt.id = 0; 1393a6aedc5dSRoger Pau Monné 1394a6aedc5dSRoger Pau Monné if (xbt.id == 0) { 1395a6aedc5dSRoger Pau Monné error = xs_transaction_start(&local_xbt); 1396a6aedc5dSRoger Pau Monné if (error != 0) 1397a6aedc5dSRoger Pau Monné goto out; 1398a6aedc5dSRoger Pau Monné xbt = local_xbt; 1399a6aedc5dSRoger Pau Monné } 1400a6aedc5dSRoger Pau Monné 1401a6aedc5dSRoger Pau Monné while (1) { 1402a6aedc5dSRoger Pau Monné u_int count; 1403a6aedc5dSRoger Pau Monné u_int i; 1404a6aedc5dSRoger Pau Monné 1405a6aedc5dSRoger Pau Monné error = xs_directory(xbt, cur_path, "", &count, &dir); 1406a6aedc5dSRoger Pau Monné if (error) 1407a6aedc5dSRoger Pau Monné goto out; 1408a6aedc5dSRoger Pau Monné 1409a6aedc5dSRoger Pau Monné for (i = 0; i < count; i++) { 1410a6aedc5dSRoger Pau Monné error = xs_rm(xbt, cur_path, dir[i]); 1411a6aedc5dSRoger Pau Monné if (error == ENOTEMPTY) { 1412a6aedc5dSRoger Pau Monné struct sbuf *push_dir; 1413a6aedc5dSRoger Pau Monné 1414a6aedc5dSRoger Pau Monné /* 1415a6aedc5dSRoger Pau Monné * Descend to clear out this sub directory. 1416a6aedc5dSRoger Pau Monné * We'll return to cur_dir once push_dir 1417a6aedc5dSRoger Pau Monné * is empty. 1418a6aedc5dSRoger Pau Monné */ 1419a6aedc5dSRoger Pau Monné push_dir = xs_join(cur_path, dir[i]); 1420a6aedc5dSRoger Pau Monné sbuf_delete(cur_path_sbuf); 1421a6aedc5dSRoger Pau Monné cur_path_sbuf = push_dir; 1422a6aedc5dSRoger Pau Monné cur_path = sbuf_data(cur_path_sbuf); 1423a6aedc5dSRoger Pau Monné break; 1424a6aedc5dSRoger Pau Monné } else if (error != 0) { 1425a6aedc5dSRoger Pau Monné goto out; 1426a6aedc5dSRoger Pau Monné } 1427a6aedc5dSRoger Pau Monné } 1428a6aedc5dSRoger Pau Monné 1429a6aedc5dSRoger Pau Monné free(dir, M_XENSTORE); 1430a6aedc5dSRoger Pau Monné dir = NULL; 1431a6aedc5dSRoger Pau Monné 1432a6aedc5dSRoger Pau Monné if (i == count) { 1433a6aedc5dSRoger Pau Monné char *last_slash; 1434a6aedc5dSRoger Pau Monné 1435a6aedc5dSRoger Pau Monné /* Directory is empty. It is now safe to remove. */ 1436a6aedc5dSRoger Pau Monné error = xs_rm(xbt, cur_path, ""); 1437a6aedc5dSRoger Pau Monné if (error != 0) 1438a6aedc5dSRoger Pau Monné goto out; 1439a6aedc5dSRoger Pau Monné 1440a6aedc5dSRoger Pau Monné if (!strcmp(cur_path, root_path)) 1441a6aedc5dSRoger Pau Monné break; 1442a6aedc5dSRoger Pau Monné 1443a6aedc5dSRoger Pau Monné /* Return to processing the parent directory. */ 1444a6aedc5dSRoger Pau Monné last_slash = strrchr(cur_path, '/'); 1445a6aedc5dSRoger Pau Monné KASSERT(last_slash != NULL, 1446a6aedc5dSRoger Pau Monné ("xs_rm_tree: mangled path %s", cur_path)); 1447a6aedc5dSRoger Pau Monné *last_slash = '\0'; 1448a6aedc5dSRoger Pau Monné } 1449a6aedc5dSRoger Pau Monné } 1450a6aedc5dSRoger Pau Monné 1451a6aedc5dSRoger Pau Monné out: 1452a6aedc5dSRoger Pau Monné sbuf_delete(cur_path_sbuf); 1453a6aedc5dSRoger Pau Monné sbuf_delete(root_path_sbuf); 1454a6aedc5dSRoger Pau Monné if (dir != NULL) 1455a6aedc5dSRoger Pau Monné free(dir, M_XENSTORE); 1456a6aedc5dSRoger Pau Monné 1457a6aedc5dSRoger Pau Monné if (local_xbt.id != 0) { 1458a6aedc5dSRoger Pau Monné int terror; 1459a6aedc5dSRoger Pau Monné 1460a6aedc5dSRoger Pau Monné terror = xs_transaction_end(local_xbt, /*abort*/error != 0); 1461a6aedc5dSRoger Pau Monné xbt.id = 0; 1462a6aedc5dSRoger Pau Monné if (terror == EAGAIN && error == 0) 1463a6aedc5dSRoger Pau Monné goto retry; 1464a6aedc5dSRoger Pau Monné } 1465a6aedc5dSRoger Pau Monné return (error); 1466a6aedc5dSRoger Pau Monné } 1467a6aedc5dSRoger Pau Monné 1468a6aedc5dSRoger Pau Monné int 1469a6aedc5dSRoger Pau Monné xs_transaction_start(struct xs_transaction *t) 1470a6aedc5dSRoger Pau Monné { 1471a6aedc5dSRoger Pau Monné char *id_str; 1472a6aedc5dSRoger Pau Monné int error; 1473a6aedc5dSRoger Pau Monné 1474a6aedc5dSRoger Pau Monné error = xs_single(XST_NIL, XS_TRANSACTION_START, "", NULL, 1475a6aedc5dSRoger Pau Monné (void **)&id_str); 1476a6aedc5dSRoger Pau Monné if (error == 0) { 1477a6aedc5dSRoger Pau Monné t->id = strtoul(id_str, NULL, 0); 1478a6aedc5dSRoger Pau Monné free(id_str, M_XENSTORE); 1479a6aedc5dSRoger Pau Monné } 1480a6aedc5dSRoger Pau Monné return (error); 1481a6aedc5dSRoger Pau Monné } 1482a6aedc5dSRoger Pau Monné 1483a6aedc5dSRoger Pau Monné int 1484a6aedc5dSRoger Pau Monné xs_transaction_end(struct xs_transaction t, int abort) 1485a6aedc5dSRoger Pau Monné { 1486a6aedc5dSRoger Pau Monné char abortstr[2]; 1487a6aedc5dSRoger Pau Monné 1488a6aedc5dSRoger Pau Monné if (abort) 1489a6aedc5dSRoger Pau Monné strcpy(abortstr, "F"); 1490a6aedc5dSRoger Pau Monné else 1491a6aedc5dSRoger Pau Monné strcpy(abortstr, "T"); 1492a6aedc5dSRoger Pau Monné 1493a6aedc5dSRoger Pau Monné return (xs_single(t, XS_TRANSACTION_END, abortstr, NULL, NULL)); 1494a6aedc5dSRoger Pau Monné } 1495a6aedc5dSRoger Pau Monné 1496a6aedc5dSRoger Pau Monné int 1497a6aedc5dSRoger Pau Monné xs_scanf(struct xs_transaction t, const char *dir, const char *node, 1498a6aedc5dSRoger Pau Monné int *scancountp, const char *fmt, ...) 1499a6aedc5dSRoger Pau Monné { 1500a6aedc5dSRoger Pau Monné va_list ap; 1501a6aedc5dSRoger Pau Monné int error, ns; 1502a6aedc5dSRoger Pau Monné char *val; 1503a6aedc5dSRoger Pau Monné 1504a6aedc5dSRoger Pau Monné error = xs_read(t, dir, node, NULL, (void **) &val); 1505a6aedc5dSRoger Pau Monné if (error) 1506a6aedc5dSRoger Pau Monné return (error); 1507a6aedc5dSRoger Pau Monné 1508a6aedc5dSRoger Pau Monné va_start(ap, fmt); 1509a6aedc5dSRoger Pau Monné ns = vsscanf(val, fmt, ap); 1510a6aedc5dSRoger Pau Monné va_end(ap); 1511a6aedc5dSRoger Pau Monné free(val, M_XENSTORE); 1512a6aedc5dSRoger Pau Monné /* Distinctive errno. */ 1513a6aedc5dSRoger Pau Monné if (ns == 0) 1514a6aedc5dSRoger Pau Monné return (ERANGE); 1515a6aedc5dSRoger Pau Monné if (scancountp) 1516a6aedc5dSRoger Pau Monné *scancountp = ns; 1517a6aedc5dSRoger Pau Monné return (0); 1518a6aedc5dSRoger Pau Monné } 1519a6aedc5dSRoger Pau Monné 1520a6aedc5dSRoger Pau Monné int 1521a6aedc5dSRoger Pau Monné xs_vprintf(struct xs_transaction t, 1522a6aedc5dSRoger Pau Monné const char *dir, const char *node, const char *fmt, va_list ap) 1523a6aedc5dSRoger Pau Monné { 1524a6aedc5dSRoger Pau Monné struct sbuf *sb; 1525a6aedc5dSRoger Pau Monné int error; 1526a6aedc5dSRoger Pau Monné 1527a6aedc5dSRoger Pau Monné sb = sbuf_new_auto(); 1528a6aedc5dSRoger Pau Monné sbuf_vprintf(sb, fmt, ap); 1529a6aedc5dSRoger Pau Monné sbuf_finish(sb); 1530a6aedc5dSRoger Pau Monné error = xs_write(t, dir, node, sbuf_data(sb)); 1531a6aedc5dSRoger Pau Monné sbuf_delete(sb); 1532a6aedc5dSRoger Pau Monné 1533a6aedc5dSRoger Pau Monné return (error); 1534a6aedc5dSRoger Pau Monné } 1535a6aedc5dSRoger Pau Monné 1536a6aedc5dSRoger Pau Monné int 1537a6aedc5dSRoger Pau Monné xs_printf(struct xs_transaction t, const char *dir, const char *node, 1538a6aedc5dSRoger Pau Monné const char *fmt, ...) 1539a6aedc5dSRoger Pau Monné { 1540a6aedc5dSRoger Pau Monné va_list ap; 1541a6aedc5dSRoger Pau Monné int error; 1542a6aedc5dSRoger Pau Monné 1543a6aedc5dSRoger Pau Monné va_start(ap, fmt); 1544a6aedc5dSRoger Pau Monné error = xs_vprintf(t, dir, node, fmt, ap); 1545a6aedc5dSRoger Pau Monné va_end(ap); 1546a6aedc5dSRoger Pau Monné 1547a6aedc5dSRoger Pau Monné return (error); 1548a6aedc5dSRoger Pau Monné } 1549a6aedc5dSRoger Pau Monné 1550a6aedc5dSRoger Pau Monné int 1551a6aedc5dSRoger Pau Monné xs_gather(struct xs_transaction t, const char *dir, ...) 1552a6aedc5dSRoger Pau Monné { 1553a6aedc5dSRoger Pau Monné va_list ap; 1554a6aedc5dSRoger Pau Monné const char *name; 1555a6aedc5dSRoger Pau Monné int error; 1556a6aedc5dSRoger Pau Monné 1557a6aedc5dSRoger Pau Monné va_start(ap, dir); 1558a6aedc5dSRoger Pau Monné error = 0; 1559a6aedc5dSRoger Pau Monné while (error == 0 && (name = va_arg(ap, char *)) != NULL) { 1560a6aedc5dSRoger Pau Monné const char *fmt = va_arg(ap, char *); 1561a6aedc5dSRoger Pau Monné void *result = va_arg(ap, void *); 1562a6aedc5dSRoger Pau Monné char *p; 1563a6aedc5dSRoger Pau Monné 1564a6aedc5dSRoger Pau Monné error = xs_read(t, dir, name, NULL, (void **) &p); 1565a6aedc5dSRoger Pau Monné if (error) 1566a6aedc5dSRoger Pau Monné break; 1567a6aedc5dSRoger Pau Monné 1568a6aedc5dSRoger Pau Monné if (fmt) { 1569a6aedc5dSRoger Pau Monné if (sscanf(p, fmt, result) == 0) 1570a6aedc5dSRoger Pau Monné error = EINVAL; 1571a6aedc5dSRoger Pau Monné free(p, M_XENSTORE); 1572a6aedc5dSRoger Pau Monné } else 1573a6aedc5dSRoger Pau Monné *(char **)result = p; 1574a6aedc5dSRoger Pau Monné } 1575a6aedc5dSRoger Pau Monné va_end(ap); 1576a6aedc5dSRoger Pau Monné 1577a6aedc5dSRoger Pau Monné return (error); 1578a6aedc5dSRoger Pau Monné } 1579a6aedc5dSRoger Pau Monné 1580a6aedc5dSRoger Pau Monné int 1581a6aedc5dSRoger Pau Monné xs_register_watch(struct xs_watch *watch) 1582a6aedc5dSRoger Pau Monné { 1583a6aedc5dSRoger Pau Monné /* Pointer in ascii is the token. */ 1584a6aedc5dSRoger Pau Monné char token[sizeof(watch) * 2 + 1]; 1585a6aedc5dSRoger Pau Monné int error; 1586a6aedc5dSRoger Pau Monné 1587*4e4e43dcSRoger Pau Monné watch->pending = 0; 1588a6aedc5dSRoger Pau Monné sprintf(token, "%lX", (long)watch); 1589a6aedc5dSRoger Pau Monné 1590a6aedc5dSRoger Pau Monné mtx_lock(&xs.registered_watches_lock); 1591a6aedc5dSRoger Pau Monné KASSERT(find_watch(token) == NULL, ("watch already registered")); 1592a6aedc5dSRoger Pau Monné LIST_INSERT_HEAD(&xs.registered_watches, watch, list); 1593a6aedc5dSRoger Pau Monné mtx_unlock(&xs.registered_watches_lock); 1594a6aedc5dSRoger Pau Monné 1595a6aedc5dSRoger Pau Monné error = xs_watch(watch->node, token); 1596a6aedc5dSRoger Pau Monné 1597a6aedc5dSRoger Pau Monné /* Ignore errors due to multiple registration. */ 1598a6aedc5dSRoger Pau Monné if (error == EEXIST) 1599a6aedc5dSRoger Pau Monné error = 0; 1600a6aedc5dSRoger Pau Monné 1601a6aedc5dSRoger Pau Monné if (error != 0) { 1602a6aedc5dSRoger Pau Monné mtx_lock(&xs.registered_watches_lock); 1603a6aedc5dSRoger Pau Monné LIST_REMOVE(watch, list); 1604a6aedc5dSRoger Pau Monné mtx_unlock(&xs.registered_watches_lock); 1605a6aedc5dSRoger Pau Monné } 1606a6aedc5dSRoger Pau Monné 1607a6aedc5dSRoger Pau Monné return (error); 1608a6aedc5dSRoger Pau Monné } 1609a6aedc5dSRoger Pau Monné 1610a6aedc5dSRoger Pau Monné void 1611a6aedc5dSRoger Pau Monné xs_unregister_watch(struct xs_watch *watch) 1612a6aedc5dSRoger Pau Monné { 1613a6aedc5dSRoger Pau Monné struct xs_stored_msg *msg, *tmp; 1614a6aedc5dSRoger Pau Monné char token[sizeof(watch) * 2 + 1]; 1615a6aedc5dSRoger Pau Monné int error; 1616a6aedc5dSRoger Pau Monné 1617a6aedc5dSRoger Pau Monné sprintf(token, "%lX", (long)watch); 1618a6aedc5dSRoger Pau Monné 1619a6aedc5dSRoger Pau Monné mtx_lock(&xs.registered_watches_lock); 1620a6aedc5dSRoger Pau Monné if (find_watch(token) == NULL) { 1621a6aedc5dSRoger Pau Monné mtx_unlock(&xs.registered_watches_lock); 1622a6aedc5dSRoger Pau Monné return; 1623a6aedc5dSRoger Pau Monné } 1624a6aedc5dSRoger Pau Monné LIST_REMOVE(watch, list); 1625a6aedc5dSRoger Pau Monné mtx_unlock(&xs.registered_watches_lock); 1626a6aedc5dSRoger Pau Monné 1627a6aedc5dSRoger Pau Monné error = xs_unwatch(watch->node, token); 1628a6aedc5dSRoger Pau Monné if (error) 1629a6aedc5dSRoger Pau Monné log(LOG_WARNING, "XENSTORE Failed to release watch %s: %i\n", 1630a6aedc5dSRoger Pau Monné watch->node, error); 1631a6aedc5dSRoger Pau Monné 1632a6aedc5dSRoger Pau Monné /* Cancel pending watch events. */ 1633a6aedc5dSRoger Pau Monné mtx_lock(&xs.watch_events_lock); 1634a6aedc5dSRoger Pau Monné TAILQ_FOREACH_SAFE(msg, &xs.watch_events, list, tmp) { 1635a6aedc5dSRoger Pau Monné if (msg->u.watch.handle != watch) 1636a6aedc5dSRoger Pau Monné continue; 1637a6aedc5dSRoger Pau Monné TAILQ_REMOVE(&xs.watch_events, msg, list); 1638a6aedc5dSRoger Pau Monné free(msg->u.watch.vec, M_XENSTORE); 1639a6aedc5dSRoger Pau Monné free(msg, M_XENSTORE); 1640a6aedc5dSRoger Pau Monné } 1641a6aedc5dSRoger Pau Monné mtx_unlock(&xs.watch_events_lock); 1642a6aedc5dSRoger Pau Monné 1643a6aedc5dSRoger Pau Monné /* Flush any currently-executing callback, unless we are it. :-) */ 1644a6aedc5dSRoger Pau Monné if (curproc->p_pid != xs.xenwatch_pid) { 1645a6aedc5dSRoger Pau Monné sx_xlock(&xs.xenwatch_mutex); 1646a6aedc5dSRoger Pau Monné sx_xunlock(&xs.xenwatch_mutex); 1647a6aedc5dSRoger Pau Monné } 1648a6aedc5dSRoger Pau Monné } 164941716b8dSRoger Pau Monné 165041716b8dSRoger Pau Monné void 165141716b8dSRoger Pau Monné xs_lock(void) 165241716b8dSRoger Pau Monné { 165341716b8dSRoger Pau Monné 165441716b8dSRoger Pau Monné sx_xlock(&xs.request_mutex); 165541716b8dSRoger Pau Monné return; 165641716b8dSRoger Pau Monné } 165741716b8dSRoger Pau Monné 165841716b8dSRoger Pau Monné void 165941716b8dSRoger Pau Monné xs_unlock(void) 166041716b8dSRoger Pau Monné { 166141716b8dSRoger Pau Monné 166241716b8dSRoger Pau Monné sx_xunlock(&xs.request_mutex); 166341716b8dSRoger Pau Monné return; 166441716b8dSRoger Pau Monné } 1665