FreeBSD kernel /amd64 XEN device code
xenstore.c
Go to the documentation of this file.
1/******************************************************************************
2 * xenstore.c
3 *
4 * Low-level kernel interface to the XenStore.
5 *
6 * Copyright (C) 2005 Rusty Russell, IBM Corporation
7 * Copyright (C) 2009,2010 Spectra Logic Corporation
8 *
9 * This file may be distributed separately from the Linux kernel, or
10 * incorporated into other software packages, subject to the following license:
11 *
12 * Permission is hereby granted, free of charge, to any person obtaining a copy
13 * of this source file (the "Software"), to deal in the Software without
14 * restriction, including without limitation the rights to use, copy, modify,
15 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
16 * and to permit persons to whom the Software is furnished to do so, subject to
17 * the following conditions:
18 *
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28 * IN THE SOFTWARE.
29 */
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD$");
33
34#include <sys/param.h>
35#include <sys/bus.h>
36#include <sys/kernel.h>
37#include <sys/lock.h>
38#include <sys/module.h>
39#include <sys/mutex.h>
40#include <sys/sx.h>
41#include <sys/syslog.h>
42#include <sys/malloc.h>
43#include <sys/systm.h>
44#include <sys/proc.h>
45#include <sys/kthread.h>
46#include <sys/sbuf.h>
47#include <sys/sysctl.h>
48#include <sys/uio.h>
49#include <sys/unistd.h>
50#include <sys/queue.h>
51#include <sys/taskqueue.h>
52
53#include <machine/stdarg.h>
54
55#include <xen/xen-os.h>
56#include <xen/hypervisor.h>
57#include <xen/xen_intr.h>
58
59#include <contrib/xen/hvm/params.h>
60#include <xen/hvm.h>
61
62#include <xen/xenstore/xenstorevar.h>
63#include <xen/xenstore/xenstore_internal.h>
64
65#include <vm/vm.h>
66#include <vm/pmap.h>
67
104static struct xs_watch *find_watch(const char *token);
105
106MALLOC_DEFINE(M_XENSTORE, "xenstore", "XenStore data and results");
107
117static struct xenstore_domain_interface *xen_store;
118
119/*-------------------------- Private Data Structures ------------------------*/
120
126
127 struct xsd_sockmsg hdr;
128
129 union {
130 /* Queued replies. */
131 struct {
132 char *body;
133 } reply;
134
135 /* Queued watch events. */
136 struct {
137 struct xs_watch *handle;
138 const char **vec;
139 u_int vec_size;
140 } watch;
141 } u;
142};
143TAILQ_HEAD(xs_stored_msg_list, xs_stored_msg);
144
148struct xs_softc {
150 device_t xs_dev;
151
158 struct mtx ring_lock;
159
160 /*
161 * Mutex used to insure exclusive access to the outgoing
162 * communication ring. We use a lock type that can be
163 * held while sleeping so that xs_write() can block waiting
164 * for space in the ring to free up, without allowing another
165 * writer to come in and corrupt a partial message write.
166 */
167 struct sx request_mutex;
168
182 struct xs_stored_msg_list reply_list;
183
185 struct mtx reply_lock;
186
190 struct xs_watch_list registered_watches;
191
194
198 struct xs_stored_msg_list watch_events;
199
202
207
218 struct sx xenwatch_mutex;
219
224 unsigned long gpfn;
225
231
233 xen_intr_handle_t xen_intr_handle;
234
240 struct intr_config_hook xs_attachcb;
241
249
254 struct task xs_late_init;
255};
256
257/*-------------------------------- Global Data ------------------------------*/
258static struct xs_softc xs;
259
260/*------------------------- Private Utility Functions -----------------------*/
261
272static u_int
273extract_strings(const char *strings, const char **dest, u_int len)
274{
275 u_int num;
276 const char *p;
277
278 for (p = strings, num = 0; p < strings + len; p += strlen(p) + 1) {
279 if (dest != NULL)
280 *dest++ = p;
281 num++;
282 }
283
284 return (num);
285}
286
304static const char **
305split(char *strings, u_int len, u_int *num)
306{
307 const char **ret;
308
309 /* Protect against unterminated buffers. */
310 if (len > 0)
311 strings[len - 1] = '\0';
312
313 /* Count the strings. */
314 *num = extract_strings(strings, /*dest*/NULL, len);
315
316 /* Transfer to one big alloc for easy freeing by the caller. */
317 ret = malloc(*num * sizeof(char *) + len, M_XENSTORE, M_WAITOK);
318 memcpy(&ret[*num], strings, len);
319 free(strings, M_XENSTORE);
320
321 /* Extract pointers to newly allocated array. */
322 strings = (char *)&ret[*num];
323 (void)extract_strings(strings, /*dest*/ret, len);
324
325 return (ret);
326}
327
328/*------------------------- Public Utility Functions -------------------------*/
329/*------- API comments for these methods can be found in xenstorevar.h -------*/
330struct sbuf *
331xs_join(const char *dir, const char *name)
332{
333 struct sbuf *sb;
334
335 sb = sbuf_new_auto();
336 sbuf_cat(sb, dir);
337 if (name[0] != '\0') {
338 sbuf_putc(sb, '/');
339 sbuf_cat(sb, name);
340 }
341 sbuf_finish(sb);
342
343 return (sb);
344}
345
346/*-------------------- Low Level Communication Management --------------------*/
354static void
355xs_intr(void * arg __unused /*__attribute__((unused))*/)
356{
357
358 /* If xenstore has not been initialized, initialize it now */
359 if (!xs.initialized) {
360 xs.initialized = true;
361 /*
362 * Since this task is probing and attaching devices we
363 * have to hold the Giant lock.
364 */
365 taskqueue_enqueue(taskqueue_swi_giant, &xs.xs_late_init);
366 }
367
368 /*
369 * Hold ring lock across wakeup so that clients
370 * cannot miss a wakeup.
371 */
372 mtx_lock(&xs.ring_lock);
373 wakeup(xen_store);
374 mtx_unlock(&xs.ring_lock);
375}
376
389static int
390xs_check_indexes(XENSTORE_RING_IDX cons, XENSTORE_RING_IDX prod)
391{
392
393 return ((prod - cons) <= XENSTORE_RING_SIZE);
394}
395
407static void *
408xs_get_output_chunk(XENSTORE_RING_IDX cons, XENSTORE_RING_IDX prod,
409 char *buf, uint32_t *len)
410{
411
412 *len = XENSTORE_RING_SIZE - MASK_XENSTORE_IDX(prod);
413 if ((XENSTORE_RING_SIZE - (prod - cons)) < *len)
414 *len = XENSTORE_RING_SIZE - (prod - cons);
415 return (buf + MASK_XENSTORE_IDX(prod));
416}
417
429static const void *
430xs_get_input_chunk(XENSTORE_RING_IDX cons, XENSTORE_RING_IDX prod,
431 const char *buf, uint32_t *len)
432{
433
434 *len = XENSTORE_RING_SIZE - MASK_XENSTORE_IDX(cons);
435 if ((prod - cons) < *len)
436 *len = prod - cons;
437 return (buf + MASK_XENSTORE_IDX(cons));
438}
439
454static int
455xs_write_store(const void *tdata, unsigned len)
456{
457 XENSTORE_RING_IDX cons, prod;
458 const char *data = (const char *)tdata;
459 int error;
460
461 sx_assert(&xs.request_mutex, SX_XLOCKED);
462 while (len != 0) {
463 void *dst;
464 u_int avail;
465
466 /* Hold lock so we can't miss wakeups should we block. */
467 mtx_lock(&xs.ring_lock);
468 cons = xen_store->req_cons;
469 prod = xen_store->req_prod;
470 if ((prod - cons) == XENSTORE_RING_SIZE) {
471 /*
472 * Output ring is full. Wait for a ring event.
473 *
474 * Note that the events from both queues
475 * are combined, so being woken does not
476 * guarantee that data exist in the read
477 * ring.
478 *
479 * To simplify error recovery and the retry,
480 * we specify PDROP so our lock is *not* held
481 * when msleep returns.
482 */
483 error = msleep(xen_store, &xs.ring_lock, PCATCH|PDROP,
484 "xbwrite", /*timeout*/0);
485 if (error && error != EWOULDBLOCK)
486 return (error);
487
488 /* Try again. */
489 continue;
490 }
491 mtx_unlock(&xs.ring_lock);
492
493 /* Verify queue sanity. */
494 if (!xs_check_indexes(cons, prod)) {
495 xen_store->req_cons = xen_store->req_prod = 0;
496 return (EIO);
497 }
498
499 dst = xs_get_output_chunk(cons, prod, xen_store->req, &avail);
500 if (avail > len)
501 avail = len;
502
503 memcpy(dst, data, avail);
504 data += avail;
505 len -= avail;
506
507 /*
508 * The store to the producer index, which indicates
509 * to the other side that new data has arrived, must
510 * be visible only after our copy of the data into the
511 * ring has completed.
512 */
513 wmb();
514 xen_store->req_prod += avail;
515
516 /*
517 * xen_intr_signal() implies mb(). The other side will see
518 * the change to req_prod at the time of the interrupt.
519 */
520 xen_intr_signal(xs.xen_intr_handle);
521 }
522
523 return (0);
524}
525
543static int
544xs_read_store(void *tdata, unsigned len)
545{
546 XENSTORE_RING_IDX cons, prod;
547 char *data = (char *)tdata;
548 int error;
549
550 while (len != 0) {
551 u_int avail;
552 const char *src;
553
554 /* Hold lock so we can't miss wakeups should we block. */
555 mtx_lock(&xs.ring_lock);
556 cons = xen_store->rsp_cons;
557 prod = xen_store->rsp_prod;
558 if (cons == prod) {
559 /*
560 * Nothing to read. Wait for a ring event.
561 *
562 * Note that the events from both queues
563 * are combined, so being woken does not
564 * guarantee that data exist in the read
565 * ring.
566 *
567 * To simplify error recovery and the retry,
568 * we specify PDROP so our lock is *not* held
569 * when msleep returns.
570 */
571 error = msleep(xen_store, &xs.ring_lock, PCATCH|PDROP,
572 "xbread", /*timeout*/0);
573 if (error && error != EWOULDBLOCK)
574 return (error);
575 continue;
576 }
577 mtx_unlock(&xs.ring_lock);
578
579 /* Verify queue sanity. */
580 if (!xs_check_indexes(cons, prod)) {
581 xen_store->rsp_cons = xen_store->rsp_prod = 0;
582 return (EIO);
583 }
584
585 src = xs_get_input_chunk(cons, prod, xen_store->rsp, &avail);
586 if (avail > len)
587 avail = len;
588
589 /*
590 * Insure the data we read is related to the indexes
591 * we read above.
592 */
593 rmb();
594
595 memcpy(data, src, avail);
596 data += avail;
597 len -= avail;
598
599 /*
600 * Insure that the producer of this ring does not see
601 * the ring space as free until after we have copied it
602 * out.
603 */
604 mb();
605 xen_store->rsp_cons += avail;
606
607 /*
608 * xen_intr_signal() implies mb(). The producer will see
609 * the updated consumer index when the event is delivered.
610 */
611 xen_intr_signal(xs.xen_intr_handle);
612 }
613
614 return (0);
615}
616
617/*----------------------- Received Message Processing ------------------------*/
627static int
628xs_process_msg(enum xsd_sockmsg_type *type)
629{
630 struct xs_stored_msg *msg;
631 char *body;
632 int error;
633
634 msg = malloc(sizeof(*msg), M_XENSTORE, M_WAITOK);
635 error = xs_read_store(&msg->hdr, sizeof(msg->hdr));
636 if (error) {
637 free(msg, M_XENSTORE);
638 return (error);
639 }
640
641 body = malloc(msg->hdr.len + 1, M_XENSTORE, M_WAITOK);
642 error = xs_read_store(body, msg->hdr.len);
643 if (error) {
644 free(body, M_XENSTORE);
645 free(msg, M_XENSTORE);
646 return (error);
647 }
648 body[msg->hdr.len] = '\0';
649
650 *type = msg->hdr.type;
651 if (msg->hdr.type == XS_WATCH_EVENT) {
652 msg->u.watch.vec = split(body, msg->hdr.len,
653 &msg->u.watch.vec_size);
654
655 mtx_lock(&xs.registered_watches_lock);
656 msg->u.watch.handle = find_watch(
657 msg->u.watch.vec[XS_WATCH_TOKEN]);
658 mtx_lock(&xs.watch_events_lock);
659 if (msg->u.watch.handle != NULL &&
660 (!msg->u.watch.handle->max_pending ||
661 msg->u.watch.handle->pending <
662 msg->u.watch.handle->max_pending)) {
663 msg->u.watch.handle->pending++;
664 TAILQ_INSERT_TAIL(&xs.watch_events, msg, list);
665 wakeup(&xs.watch_events);
666 mtx_unlock(&xs.watch_events_lock);
667 } else {
668 mtx_unlock(&xs.watch_events_lock);
669 free(msg->u.watch.vec, M_XENSTORE);
670 free(msg, M_XENSTORE);
671 }
672 mtx_unlock(&xs.registered_watches_lock);
673 } else {
674 msg->u.reply.body = body;
675 mtx_lock(&xs.reply_lock);
676 TAILQ_INSERT_TAIL(&xs.reply_list, msg, list);
677 wakeup(&xs.reply_list);
678 mtx_unlock(&xs.reply_lock);
679 }
680
681 return (0);
682}
683
690static void
691xs_rcv_thread(void *arg __unused)
692{
693 int error;
694 enum xsd_sockmsg_type type;
695
696 for (;;) {
697 error = xs_process_msg(&type);
698 if (error)
699 printf("XENSTORE error %d while reading message\n",
700 error);
701 }
702}
703
704/*---------------- XenStore Message Request/Reply Processing -----------------*/
705#define xsd_error_count (sizeof(xsd_errors) / sizeof(xsd_errors[0]))
706
716static int
717xs_get_error(const char *errorstring)
718{
719 u_int i;
720
721 for (i = 0; i < xsd_error_count; i++) {
722 if (!strcmp(errorstring, xsd_errors[i].errstring))
723 return (xsd_errors[i].errnum);
724 }
725 log(LOG_WARNING, "XENSTORE xen store gave: unknown error %s",
726 errorstring);
727 return (EINVAL);
728}
729
740static int
741xs_read_reply(enum xsd_sockmsg_type *type, u_int *len, void **result)
742{
743 struct xs_stored_msg *msg;
744 char *body;
745 int error;
746
747 mtx_lock(&xs.reply_lock);
748 while (TAILQ_EMPTY(&xs.reply_list)) {
749 error = mtx_sleep(&xs.reply_list, &xs.reply_lock, 0, "xswait",
750 hz/10);
751 if (error && error != EWOULDBLOCK) {
752 mtx_unlock(&xs.reply_lock);
753 return (error);
754 }
755 }
756 msg = TAILQ_FIRST(&xs.reply_list);
757 TAILQ_REMOVE(&xs.reply_list, msg, list);
758 mtx_unlock(&xs.reply_lock);
759
760 *type = msg->hdr.type;
761 if (len)
762 *len = msg->hdr.len;
763 body = msg->u.reply.body;
764
765 free(msg, M_XENSTORE);
766 *result = body;
767 return (0);
768}
769
787int
788xs_dev_request_and_reply(struct xsd_sockmsg *msg, void **result)
789{
790 int error;
791
792 sx_xlock(&xs.request_mutex);
793 if ((error = xs_write_store(msg, sizeof(*msg) + msg->len)) == 0)
794 error = xs_read_reply(&msg->type, &msg->len, result);
795 sx_xunlock(&xs.request_mutex);
796
797 return (error);
798}
799
816static int
817xs_talkv(struct xs_transaction t, enum xsd_sockmsg_type request_type,
818 const struct iovec *iovec, u_int num_vecs, u_int *len, void **result)
819{
820 struct xsd_sockmsg msg;
821 void *ret = NULL;
822 u_int i;
823 int error;
824
825 msg.tx_id = t.id;
826 msg.req_id = 0;
827 msg.type = request_type;
828 msg.len = 0;
829 for (i = 0; i < num_vecs; i++)
830 msg.len += iovec[i].iov_len;
831
832 sx_xlock(&xs.request_mutex);
833 error = xs_write_store(&msg, sizeof(msg));
834 if (error) {
835 printf("xs_talkv failed %d\n", error);
836 goto error_lock_held;
837 }
838
839 for (i = 0; i < num_vecs; i++) {
840 error = xs_write_store(iovec[i].iov_base, iovec[i].iov_len);
841 if (error) {
842 printf("xs_talkv failed %d\n", error);
843 goto error_lock_held;
844 }
845 }
846
847 error = xs_read_reply(&msg.type, len, &ret);
848
849error_lock_held:
850 sx_xunlock(&xs.request_mutex);
851 if (error)
852 return (error);
853
854 if (msg.type == XS_ERROR) {
855 error = xs_get_error(ret);
856 free(ret, M_XENSTORE);
857 return (error);
858 }
859
860 /* Reply is either error or an echo of our request message type. */
861 KASSERT(msg.type == request_type, ("bad xenstore message type"));
862
863 if (result)
864 *result = ret;
865 else
866 free(ret, M_XENSTORE);
867
868 return (0);
869}
870
887static int
888xs_single(struct xs_transaction t, enum xsd_sockmsg_type request_type,
889 const char *body, u_int *len, void **result)
890{
891 struct iovec iovec;
892
893 iovec.iov_base = (void *)(uintptr_t)body;
894 iovec.iov_len = strlen(body) + 1;
895
896 return (xs_talkv(t, request_type, &iovec, 1, len, result));
897}
898
899/*------------------------- XenStore Watch Support ---------------------------*/
909static int
910xs_watch(const char *path, const char *token)
911{
912 struct iovec iov[2];
913
914 iov[0].iov_base = (void *)(uintptr_t) path;
915 iov[0].iov_len = strlen(path) + 1;
916 iov[1].iov_base = (void *)(uintptr_t) token;
917 iov[1].iov_len = strlen(token) + 1;
918
919 return (xs_talkv(XST_NIL, XS_WATCH, iov, 2, NULL, NULL));
920}
921
931static int
932xs_unwatch(const char *path, const char *token)
933{
934 struct iovec iov[2];
935
936 iov[0].iov_base = (void *)(uintptr_t) path;
937 iov[0].iov_len = strlen(path) + 1;
938 iov[1].iov_base = (void *)(uintptr_t) token;
939 iov[1].iov_len = strlen(token) + 1;
940
941 return (xs_talkv(XST_NIL, XS_UNWATCH, iov, 2, NULL, NULL));
942}
943
952static struct xs_watch *
953find_watch(const char *token)
954{
955 struct xs_watch *i, *cmp;
956
957 cmp = (void *)strtoul(token, NULL, 16);
958
959 LIST_FOREACH(i, &xs.registered_watches, list)
960 if (i == cmp)
961 return (i);
962
963 return (NULL);
964}
965
969static void
970xenwatch_thread(void *unused)
971{
972 struct xs_stored_msg *msg;
973
974 for (;;) {
975 mtx_lock(&xs.watch_events_lock);
976 while (TAILQ_EMPTY(&xs.watch_events))
977 mtx_sleep(&xs.watch_events,
979 PWAIT | PCATCH, "waitev", hz/10);
980
981 mtx_unlock(&xs.watch_events_lock);
982 sx_xlock(&xs.xenwatch_mutex);
983
984 mtx_lock(&xs.watch_events_lock);
985 msg = TAILQ_FIRST(&xs.watch_events);
986 if (msg) {
987 TAILQ_REMOVE(&xs.watch_events, msg, list);
988 msg->u.watch.handle->pending--;
989 }
990 mtx_unlock(&xs.watch_events_lock);
991
992 if (msg != NULL) {
993 /*
994 * XXX There are messages coming in with a NULL
995 * XXX callback. This deserves further investigation;
996 * XXX the workaround here simply prevents the kernel
997 * XXX from panic'ing on startup.
998 */
999 if (msg->u.watch.handle->callback != NULL)
1000 msg->u.watch.handle->callback(
1001 msg->u.watch.handle,
1002 (const char **)msg->u.watch.vec,
1003 msg->u.watch.vec_size);
1004 free(msg->u.watch.vec, M_XENSTORE);
1005 free(msg, M_XENSTORE);
1006 }
1007
1008 sx_xunlock(&xs.xenwatch_mutex);
1009 }
1010}
1011
1012/*----------- XenStore Configuration, Initialization, and Control ------------*/
1019static int
1021{
1022 int error;
1023
1024 if (xen_store->rsp_prod != xen_store->rsp_cons) {
1025 log(LOG_WARNING, "XENSTORE response ring is not quiescent "
1026 "(%08x:%08x): fixing up\n",
1027 xen_store->rsp_cons, xen_store->rsp_prod);
1028 xen_store->rsp_cons = xen_store->rsp_prod;
1029 }
1030
1031 xen_intr_unbind(&xs.xen_intr_handle);
1032
1033 error = xen_intr_bind_local_port(xs.xs_dev, xs.evtchn,
1034 /*filter*/NULL, xs_intr, /*arg*/NULL, INTR_TYPE_NET|INTR_MPSAFE,
1036 if (error) {
1037 log(LOG_WARNING, "XENSTORE request irq failed %i\n", error);
1038 return (error);
1039 }
1040
1041 return (0);
1042}
1043
1044/*------------------ Private Device Attachment Functions --------------------*/
1045static void
1046xs_identify(driver_t *driver, device_t parent)
1047{
1048
1049 BUS_ADD_CHILD(parent, 0, "xenstore", 0);
1050}
1051
1057static int
1058xs_probe(device_t dev)
1059{
1060 /*
1061 * We are either operating within a PV kernel or being probed
1062 * as the child of the successfully attached xenpci device.
1063 * Thus we are in a Xen environment and there will be a XenStore.
1064 * Unconditionally return success.
1065 */
1066 device_set_desc(dev, "XenStore");
1067 return (BUS_PROBE_NOWILDCARD);
1068}
1069
1070static void
1072{
1073
1074 bus_generic_probe(xs.xs_dev);
1075 bus_generic_attach(xs.xs_dev);
1076
1077 config_intrhook_disestablish(&xs.xs_attachcb);
1078}
1079
1080static void
1081xs_attach_late(void *arg, int pending)
1082{
1083
1084 KASSERT((pending == 1), ("xs late attach queued several times"));
1085 bus_generic_probe(xs.xs_dev);
1086 bus_generic_attach(xs.xs_dev);
1087}
1088
1095static int
1096xs_attach(device_t dev)
1097{
1098 int error;
1099
1100 /* Allow us to get device_t from softc and vice-versa. */
1101 xs.xs_dev = dev;
1102 device_set_softc(dev, &xs);
1103
1104 /* Initialize the interface to xenstore. */
1105 struct proc *p;
1106
1107 xs.initialized = false;
1108 xs.evtchn = xen_get_xenstore_evtchn();
1109 if (xs.evtchn == 0) {
1110 struct evtchn_alloc_unbound alloc_unbound;
1111
1112 /* Allocate a local event channel for xenstore */
1113 alloc_unbound.dom = DOMID_SELF;
1114 alloc_unbound.remote_dom = DOMID_SELF;
1115 error = HYPERVISOR_event_channel_op(
1116 EVTCHNOP_alloc_unbound, &alloc_unbound);
1117 if (error != 0)
1118 panic(
1119 "unable to alloc event channel for Dom0: %d",
1120 error);
1121
1122 xs.evtchn = alloc_unbound.port;
1123
1124 /* Allocate memory for the xs shared ring */
1125 xen_store = malloc(PAGE_SIZE, M_XENSTORE, M_WAITOK | M_ZERO);
1126 xs.gpfn = atop(pmap_kextract((vm_offset_t)xen_store));
1127 } else {
1128 xs.gpfn = xen_get_xenstore_mfn();
1129 xen_store = pmap_mapdev_attr(ptoa(xs.gpfn), PAGE_SIZE,
1130 VM_MEMATTR_XEN);
1131 xs.initialized = true;
1132 }
1133
1134 TAILQ_INIT(&xs.reply_list);
1135 TAILQ_INIT(&xs.watch_events);
1136
1137 mtx_init(&xs.ring_lock, "ring lock", NULL, MTX_DEF);
1138 mtx_init(&xs.reply_lock, "reply lock", NULL, MTX_DEF);
1139 sx_init(&xs.xenwatch_mutex, "xenwatch");
1140 sx_init(&xs.request_mutex, "xenstore request");
1141 mtx_init(&xs.registered_watches_lock, "watches", NULL, MTX_DEF);
1142 mtx_init(&xs.watch_events_lock, "watch events", NULL, MTX_DEF);
1143
1144 /* Initialize the shared memory rings to talk to xenstored */
1145 error = xs_init_comms();
1146 if (error)
1147 return (error);
1148
1149 error = kproc_create(xenwatch_thread, NULL, &p, RFHIGHPID,
1150 0, "xenwatch");
1151 if (error)
1152 return (error);
1153 xs.xenwatch_pid = p->p_pid;
1154
1155 error = kproc_create(xs_rcv_thread, NULL, NULL,
1156 RFHIGHPID, 0, "xenstore_rcv");
1157
1159 xs.xs_attachcb.ich_arg = NULL;
1160 if (xs.initialized) {
1161 config_intrhook_establish(&xs.xs_attachcb);
1162 } else {
1163 TASK_INIT(&xs.xs_late_init, 0, xs_attach_late, NULL);
1164 }
1165
1166 return (error);
1167}
1168
1173static int
1174xs_suspend(device_t dev)
1175{
1176 int error;
1177
1178 /* Suspend child Xen devices. */
1179 error = bus_generic_suspend(dev);
1180 if (error != 0)
1181 return (error);
1182
1183 sx_xlock(&xs.request_mutex);
1184
1185 return (0);
1186}
1187
1191static int
1192xs_resume(device_t dev __unused)
1193{
1194 struct xs_watch *watch;
1195 char token[sizeof(watch) * 2 + 1];
1196
1197 xs_init_comms();
1198
1199 sx_xunlock(&xs.request_mutex);
1200
1201 /*
1202 * NB: since xenstore childs have not been resumed yet, there's
1203 * no need to hold any watch mutex. Having clients try to add or
1204 * remove watches at this point (before xenstore is resumed) is
1205 * clearly a violantion of the resume order.
1206 */
1207 LIST_FOREACH(watch, &xs.registered_watches, list) {
1208 sprintf(token, "%lX", (long)watch);
1209 xs_watch(watch->node, token);
1210 }
1211
1212 /* Resume child Xen devices. */
1213 bus_generic_resume(dev);
1214
1215 return (0);
1216}
1217
1218/*-------------------- Private Device Attachment Data -----------------------*/
1219static device_method_t xenstore_methods[] = {
1220 /* Device interface */
1221 DEVMETHOD(device_identify, xs_identify),
1222 DEVMETHOD(device_probe, xs_probe),
1223 DEVMETHOD(device_attach, xs_attach),
1224 DEVMETHOD(device_detach, bus_generic_detach),
1225 DEVMETHOD(device_shutdown, bus_generic_shutdown),
1226 DEVMETHOD(device_suspend, xs_suspend),
1227 DEVMETHOD(device_resume, xs_resume),
1228
1229 /* Bus interface */
1230 DEVMETHOD(bus_add_child, bus_generic_add_child),
1231 DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource),
1232 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
1233 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
1234 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
1235
1236 DEVMETHOD_END
1237};
1238
1239DEFINE_CLASS_0(xenstore, xenstore_driver, xenstore_methods, 0);
1240static devclass_t xenstore_devclass;
1241
1242DRIVER_MODULE(xenstore, xenpv, xenstore_driver, xenstore_devclass, 0, 0);
1243
1244/*------------------------------- Sysctl Data --------------------------------*/
1245/* XXX Shouldn't the node be somewhere else? */
1246SYSCTL_NODE(_dev, OID_AUTO, xen, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
1247 "Xen");
1248SYSCTL_INT(_dev_xen, OID_AUTO, xsd_port, CTLFLAG_RD, &xs.evtchn, 0, "");
1249SYSCTL_ULONG(_dev_xen, OID_AUTO, xsd_kva, CTLFLAG_RD, (u_long *) &xen_store, 0, "");
1250
1251/*-------------------------------- Public API --------------------------------*/
1252/*------- API comments for these methods can be found in xenstorevar.h -------*/
1253bool
1255{
1256
1257 return (xs.initialized);
1258}
1259
1260evtchn_port_t
1262{
1263
1264 return (xs.evtchn);
1265}
1266
1267vm_paddr_t
1269{
1270
1271 return (ptoa(xs.gpfn));
1272}
1273
1274int
1275xs_directory(struct xs_transaction t, const char *dir, const char *node,
1276 u_int *num, const char ***result)
1277{
1278 struct sbuf *path;
1279 char *strings;
1280 u_int len = 0;
1281 int error;
1282
1283 path = xs_join(dir, node);
1284 error = xs_single(t, XS_DIRECTORY, sbuf_data(path), &len,
1285 (void **)&strings);
1286 sbuf_delete(path);
1287 if (error)
1288 return (error);
1289
1290 *result = split(strings, len, num);
1291
1292 return (0);
1293}
1294
1295int
1296xs_exists(struct xs_transaction t, const char *dir, const char *node)
1297{
1298 const char **d;
1299 int error, dir_n;
1300
1301 error = xs_directory(t, dir, node, &dir_n, &d);
1302 if (error)
1303 return (0);
1304 free(d, M_XENSTORE);
1305 return (1);
1306}
1307
1308int
1309xs_read(struct xs_transaction t, const char *dir, const char *node,
1310 u_int *len, void **result)
1311{
1312 struct sbuf *path;
1313 void *ret;
1314 int error;
1315
1316 path = xs_join(dir, node);
1317 error = xs_single(t, XS_READ, sbuf_data(path), len, &ret);
1318 sbuf_delete(path);
1319 if (error)
1320 return (error);
1321 *result = ret;
1322 return (0);
1323}
1324
1325int
1326xs_write(struct xs_transaction t, const char *dir, const char *node,
1327 const char *string)
1328{
1329 struct sbuf *path;
1330 struct iovec iovec[2];
1331 int error;
1332
1333 path = xs_join(dir, node);
1334
1335 iovec[0].iov_base = (void *)(uintptr_t) sbuf_data(path);
1336 iovec[0].iov_len = sbuf_len(path) + 1;
1337 iovec[1].iov_base = (void *)(uintptr_t) string;
1338 iovec[1].iov_len = strlen(string);
1339
1340 error = xs_talkv(t, XS_WRITE, iovec, 2, NULL, NULL);
1341 sbuf_delete(path);
1342
1343 return (error);
1344}
1345
1346int
1347xs_mkdir(struct xs_transaction t, const char *dir, const char *node)
1348{
1349 struct sbuf *path;
1350 int ret;
1351
1352 path = xs_join(dir, node);
1353 ret = xs_single(t, XS_MKDIR, sbuf_data(path), NULL, NULL);
1354 sbuf_delete(path);
1355
1356 return (ret);
1357}
1358
1359int
1360xs_rm(struct xs_transaction t, const char *dir, const char *node)
1361{
1362 struct sbuf *path;
1363 int ret;
1364
1365 path = xs_join(dir, node);
1366 ret = xs_single(t, XS_RM, sbuf_data(path), NULL, NULL);
1367 sbuf_delete(path);
1368
1369 return (ret);
1370}
1371
1372int
1373xs_rm_tree(struct xs_transaction xbt, const char *base, const char *node)
1374{
1375 struct xs_transaction local_xbt;
1376 struct sbuf *root_path_sbuf;
1377 struct sbuf *cur_path_sbuf;
1378 char *root_path;
1379 char *cur_path;
1380 const char **dir;
1381 int error;
1382
1383retry:
1384 root_path_sbuf = xs_join(base, node);
1385 cur_path_sbuf = xs_join(base, node);
1386 root_path = sbuf_data(root_path_sbuf);
1387 cur_path = sbuf_data(cur_path_sbuf);
1388 dir = NULL;
1389 local_xbt.id = 0;
1390
1391 if (xbt.id == 0) {
1392 error = xs_transaction_start(&local_xbt);
1393 if (error != 0)
1394 goto out;
1395 xbt = local_xbt;
1396 }
1397
1398 while (1) {
1399 u_int count;
1400 u_int i;
1401
1402 error = xs_directory(xbt, cur_path, "", &count, &dir);
1403 if (error)
1404 goto out;
1405
1406 for (i = 0; i < count; i++) {
1407 error = xs_rm(xbt, cur_path, dir[i]);
1408 if (error == ENOTEMPTY) {
1409 struct sbuf *push_dir;
1410
1411 /*
1412 * Descend to clear out this sub directory.
1413 * We'll return to cur_dir once push_dir
1414 * is empty.
1415 */
1416 push_dir = xs_join(cur_path, dir[i]);
1417 sbuf_delete(cur_path_sbuf);
1418 cur_path_sbuf = push_dir;
1419 cur_path = sbuf_data(cur_path_sbuf);
1420 break;
1421 } else if (error != 0) {
1422 goto out;
1423 }
1424 }
1425
1426 free(dir, M_XENSTORE);
1427 dir = NULL;
1428
1429 if (i == count) {
1430 char *last_slash;
1431
1432 /* Directory is empty. It is now safe to remove. */
1433 error = xs_rm(xbt, cur_path, "");
1434 if (error != 0)
1435 goto out;
1436
1437 if (!strcmp(cur_path, root_path))
1438 break;
1439
1440 /* Return to processing the parent directory. */
1441 last_slash = strrchr(cur_path, '/');
1442 KASSERT(last_slash != NULL,
1443 ("xs_rm_tree: mangled path %s", cur_path));
1444 *last_slash = '\0';
1445 }
1446 }
1447
1448out:
1449 sbuf_delete(cur_path_sbuf);
1450 sbuf_delete(root_path_sbuf);
1451 if (dir != NULL)
1452 free(dir, M_XENSTORE);
1453
1454 if (local_xbt.id != 0) {
1455 int terror;
1456
1457 terror = xs_transaction_end(local_xbt, /*abort*/error != 0);
1458 xbt.id = 0;
1459 if (terror == EAGAIN && error == 0)
1460 goto retry;
1461 }
1462 return (error);
1463}
1464
1465int
1466xs_transaction_start(struct xs_transaction *t)
1467{
1468 char *id_str;
1469 int error;
1470
1471 error = xs_single(XST_NIL, XS_TRANSACTION_START, "", NULL,
1472 (void **)&id_str);
1473 if (error == 0) {
1474 t->id = strtoul(id_str, NULL, 0);
1475 free(id_str, M_XENSTORE);
1476 }
1477 return (error);
1478}
1479
1480int
1481xs_transaction_end(struct xs_transaction t, int abort)
1482{
1483 char abortstr[2];
1484
1485 if (abort)
1486 strcpy(abortstr, "F");
1487 else
1488 strcpy(abortstr, "T");
1489
1490 return (xs_single(t, XS_TRANSACTION_END, abortstr, NULL, NULL));
1491}
1492
1493int
1494xs_scanf(struct xs_transaction t, const char *dir, const char *node,
1495 int *scancountp, const char *fmt, ...)
1496{
1497 va_list ap;
1498 int error, ns;
1499 char *val;
1500
1501 error = xs_read(t, dir, node, NULL, (void **) &val);
1502 if (error)
1503 return (error);
1504
1505 va_start(ap, fmt);
1506 ns = vsscanf(val, fmt, ap);
1507 va_end(ap);
1508 free(val, M_XENSTORE);
1509 /* Distinctive errno. */
1510 if (ns == 0)
1511 return (ERANGE);
1512 if (scancountp)
1513 *scancountp = ns;
1514 return (0);
1515}
1516
1517int
1518xs_vprintf(struct xs_transaction t,
1519 const char *dir, const char *node, const char *fmt, va_list ap)
1520{
1521 struct sbuf *sb;
1522 int error;
1523
1524 sb = sbuf_new_auto();
1525 sbuf_vprintf(sb, fmt, ap);
1526 sbuf_finish(sb);
1527 error = xs_write(t, dir, node, sbuf_data(sb));
1528 sbuf_delete(sb);
1529
1530 return (error);
1531}
1532
1533int
1534xs_printf(struct xs_transaction t, const char *dir, const char *node,
1535 const char *fmt, ...)
1536{
1537 va_list ap;
1538 int error;
1539
1540 va_start(ap, fmt);
1541 error = xs_vprintf(t, dir, node, fmt, ap);
1542 va_end(ap);
1543
1544 return (error);
1545}
1546
1547int
1548xs_gather(struct xs_transaction t, const char *dir, ...)
1549{
1550 va_list ap;
1551 const char *name;
1552 int error;
1553
1554 va_start(ap, dir);
1555 error = 0;
1556 while (error == 0 && (name = va_arg(ap, char *)) != NULL) {
1557 const char *fmt = va_arg(ap, char *);
1558 void *result = va_arg(ap, void *);
1559 char *p;
1560
1561 error = xs_read(t, dir, name, NULL, (void **) &p);
1562 if (error)
1563 break;
1564
1565 if (fmt) {
1566 if (sscanf(p, fmt, result) == 0)
1567 error = EINVAL;
1568 free(p, M_XENSTORE);
1569 } else
1570 *(char **)result = p;
1571 }
1572 va_end(ap);
1573
1574 return (error);
1575}
1576
1577int
1579{
1580 /* Pointer in ascii is the token. */
1581 char token[sizeof(watch) * 2 + 1];
1582 int error;
1583
1584 watch->pending = 0;
1585 sprintf(token, "%lX", (long)watch);
1586
1587 mtx_lock(&xs.registered_watches_lock);
1588 KASSERT(find_watch(token) == NULL, ("watch already registered"));
1589 LIST_INSERT_HEAD(&xs.registered_watches, watch, list);
1590 mtx_unlock(&xs.registered_watches_lock);
1591
1592 error = xs_watch(watch->node, token);
1593
1594 /* Ignore errors due to multiple registration. */
1595 if (error == EEXIST)
1596 error = 0;
1597
1598 if (error != 0) {
1599 mtx_lock(&xs.registered_watches_lock);
1600 LIST_REMOVE(watch, list);
1601 mtx_unlock(&xs.registered_watches_lock);
1602 }
1603
1604 return (error);
1605}
1606
1607void
1609{
1610 struct xs_stored_msg *msg, *tmp;
1611 char token[sizeof(watch) * 2 + 1];
1612 int error;
1613
1614 sprintf(token, "%lX", (long)watch);
1615
1616 mtx_lock(&xs.registered_watches_lock);
1617 if (find_watch(token) == NULL) {
1618 mtx_unlock(&xs.registered_watches_lock);
1619 return;
1620 }
1621 LIST_REMOVE(watch, list);
1622 mtx_unlock(&xs.registered_watches_lock);
1623
1624 error = xs_unwatch(watch->node, token);
1625 if (error)
1626 log(LOG_WARNING, "XENSTORE Failed to release watch %s: %i\n",
1627 watch->node, error);
1628
1629 /* Cancel pending watch events. */
1630 mtx_lock(&xs.watch_events_lock);
1631 TAILQ_FOREACH_SAFE(msg, &xs.watch_events, list, tmp) {
1632 if (msg->u.watch.handle != watch)
1633 continue;
1634 TAILQ_REMOVE(&xs.watch_events, msg, list);
1635 free(msg->u.watch.vec, M_XENSTORE);
1636 free(msg, M_XENSTORE);
1637 }
1638 mtx_unlock(&xs.watch_events_lock);
1639
1640 /* Flush any currently-executing callback, unless we are it. :-) */
1641 if (curproc->p_pid != xs.xenwatch_pid) {
1642 sx_xlock(&xs.xenwatch_mutex);
1643 sx_xunlock(&xs.xenwatch_mutex);
1644 }
1645}
1646
1647void
1649{
1650
1651 sx_xlock(&xs.request_mutex);
1652 return;
1653}
1654
1655void
1657{
1658
1659 sx_xunlock(&xs.request_mutex);
1660 return;
1661}
static struct sbuf * buf
Definition: debug.c:58
struct intr_config_hook xs_attachcb
Definition: xenstore.c:240
device_t xs_dev
Definition: xenstore.c:150
struct xs_watch_list registered_watches
Definition: xenstore.c:190
pid_t xenwatch_pid
Definition: xenstore.c:206
struct mtx ring_lock
Definition: xenstore.c:158
struct sx xenwatch_mutex
Definition: xenstore.c:218
int evtchn
Definition: xenstore.c:230
bool initialized
Definition: xenstore.c:248
struct mtx registered_watches_lock
Definition: xenstore.c:193
struct xs_stored_msg_list reply_list
Definition: xenstore.c:182
xen_intr_handle_t xen_intr_handle
Definition: xenstore.c:233
struct sx request_mutex
Definition: xenstore.c:167
struct mtx watch_events_lock
Definition: xenstore.c:201
struct mtx reply_lock
Definition: xenstore.c:185
struct xs_stored_msg_list watch_events
Definition: xenstore.c:198
unsigned long gpfn
Definition: xenstore.c:224
struct task xs_late_init
Definition: xenstore.c:254
TAILQ_ENTRY(xs_stored_msg)
Definition: xenstore.c:125
#define xsd_error_count
Definition: xenstore.c:705
static int xs_watch(const char *path, const char *token)
Definition: xenstore.c:910
void xs_lock(void)
Definition: xenstore.c:1648
static int xs_process_msg(enum xsd_sockmsg_type *type)
Definition: xenstore.c:628
int xs_dev_request_and_reply(struct xsd_sockmsg *msg, void **result)
Definition: xenstore.c:788
static int xs_check_indexes(XENSTORE_RING_IDX cons, XENSTORE_RING_IDX prod)
Definition: xenstore.c:390
static void * xs_get_output_chunk(XENSTORE_RING_IDX cons, XENSTORE_RING_IDX prod, char *buf, uint32_t *len)
Definition: xenstore.c:408
static int xs_get_error(const char *errorstring)
Definition: xenstore.c:717
struct sbuf * xs_join(const char *dir, const char *name)
Definition: xenstore.c:331
static void xs_rcv_thread(void *arg __unused)
Definition: xenstore.c:691
static int xs_attach(device_t dev)
Definition: xenstore.c:1096
static int xs_single(struct xs_transaction t, enum xsd_sockmsg_type request_type, const char *body, u_int *len, void **result)
Definition: xenstore.c:888
int xs_write(struct xs_transaction t, const char *dir, const char *node, const char *string)
Definition: xenstore.c:1326
int xs_printf(struct xs_transaction t, const char *dir, const char *node, const char *fmt,...)
Definition: xenstore.c:1534
static void xenwatch_thread(void *unused)
Definition: xenstore.c:970
evtchn_port_t xs_evtchn(void)
Definition: xenstore.c:1261
static int xs_init_comms(void)
Definition: xenstore.c:1020
bool xs_initialized(void)
Definition: xenstore.c:1254
int xs_transaction_start(struct xs_transaction *t)
Definition: xenstore.c:1466
static struct xenstore_domain_interface * xen_store
Definition: xenstore.c:117
static device_method_t xenstore_methods[]
Definition: xenstore.c:1219
static int xs_suspend(device_t dev)
Definition: xenstore.c:1174
static void xs_intr(void *arg __unused)
Definition: xenstore.c:355
DRIVER_MODULE(xenstore, xenpv, xenstore_driver, xenstore_devclass, 0, 0)
int xs_read(struct xs_transaction t, const char *dir, const char *node, u_int *len, void **result)
Definition: xenstore.c:1309
static void xs_attach_late(void *arg, int pending)
Definition: xenstore.c:1081
int xs_transaction_end(struct xs_transaction t, int abort)
Definition: xenstore.c:1481
int xs_directory(struct xs_transaction t, const char *dir, const char *node, u_int *num, const char ***result)
Definition: xenstore.c:1275
void xs_unregister_watch(struct xs_watch *watch)
Definition: xenstore.c:1608
static u_int extract_strings(const char *strings, const char **dest, u_int len)
Definition: xenstore.c:273
void xs_unlock(void)
Definition: xenstore.c:1656
static int xs_probe(device_t dev)
Definition: xenstore.c:1058
static int xs_talkv(struct xs_transaction t, enum xsd_sockmsg_type request_type, const struct iovec *iovec, u_int num_vecs, u_int *len, void **result)
Definition: xenstore.c:817
int xs_scanf(struct xs_transaction t, const char *dir, const char *node, int *scancountp, const char *fmt,...)
Definition: xenstore.c:1494
static const void * xs_get_input_chunk(XENSTORE_RING_IDX cons, XENSTORE_RING_IDX prod, const char *buf, uint32_t *len)
Definition: xenstore.c:430
__FBSDID("$FreeBSD$")
int xs_mkdir(struct xs_transaction t, const char *dir, const char *node)
Definition: xenstore.c:1347
vm_paddr_t xs_address(void)
Definition: xenstore.c:1268
static struct xs_softc xs
Definition: xenstore.c:258
static void xs_attach_deferred(void *arg)
Definition: xenstore.c:1071
static int xs_read_reply(enum xsd_sockmsg_type *type, u_int *len, void **result)
Definition: xenstore.c:741
int xs_vprintf(struct xs_transaction t, const char *dir, const char *node, const char *fmt, va_list ap)
Definition: xenstore.c:1518
TAILQ_HEAD(xs_stored_msg_list, xs_stored_msg)
SYSCTL_NODE(_dev, OID_AUTO, xen, CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "Xen")
static const char ** split(char *strings, u_int len, u_int *num)
Definition: xenstore.c:305
int xs_gather(struct xs_transaction t, const char *dir,...)
Definition: xenstore.c:1548
static int xs_unwatch(const char *path, const char *token)
Definition: xenstore.c:932
int xs_exists(struct xs_transaction t, const char *dir, const char *node)
Definition: xenstore.c:1296
SYSCTL_INT(_dev_xen, OID_AUTO, xsd_port, CTLFLAG_RD, &xs.evtchn, 0, "")
int xs_rm(struct xs_transaction t, const char *dir, const char *node)
Definition: xenstore.c:1360
static void xs_identify(driver_t *driver, device_t parent)
Definition: xenstore.c:1046
static int xs_resume(device_t dev __unused)
Definition: xenstore.c:1192
static int xs_read_store(void *tdata, unsigned len)
Definition: xenstore.c:544
int xs_rm_tree(struct xs_transaction xbt, const char *base, const char *node)
Definition: xenstore.c:1373
static devclass_t xenstore_devclass
Definition: xenstore.c:1240
DEFINE_CLASS_0(xenstore, xenstore_driver, xenstore_methods, 0)
MALLOC_DEFINE(M_XENSTORE, "xenstore", "XenStore data and results")
int xs_register_watch(struct xs_watch *watch)
Definition: xenstore.c:1578
static struct xs_watch * find_watch(const char *token)
Definition: xenstore.c:953
static int xs_write_store(const void *tdata, unsigned len)
Definition: xenstore.c:455
SYSCTL_ULONG(_dev_xen, OID_AUTO, xsd_kva, CTLFLAG_RD,(u_long *) &xen_store, 0, "")