Home | History | Annotate | Line # | Download | only in xenbus
xenbus_comms.c revision 1.18.2.1
      1  1.18.2.1  christos /* $NetBSD: xenbus_comms.c,v 1.18.2.1 2019/06/10 22:06:56 christos Exp $ */
      2       1.1    bouyer /******************************************************************************
      3       1.1    bouyer  * xenbus_comms.c
      4       1.1    bouyer  *
      5       1.1    bouyer  * Low level code to talks to Xen Store: ringbuffer and event channel.
      6       1.1    bouyer  *
      7       1.1    bouyer  * Copyright (C) 2005 Rusty Russell, IBM Corporation
      8       1.1    bouyer  *
      9       1.1    bouyer  * This file may be distributed separately from the Linux kernel, or
     10       1.1    bouyer  * incorporated into other software packages, subject to the following license:
     11       1.1    bouyer  *
     12       1.1    bouyer  * Permission is hereby granted, free of charge, to any person obtaining a copy
     13       1.1    bouyer  * of this source file (the "Software"), to deal in the Software without
     14       1.1    bouyer  * restriction, including without limitation the rights to use, copy, modify,
     15       1.1    bouyer  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
     16       1.1    bouyer  * and to permit persons to whom the Software is furnished to do so, subject to
     17       1.1    bouyer  * the following conditions:
     18       1.1    bouyer  *
     19       1.1    bouyer  * The above copyright notice and this permission notice shall be included in
     20       1.1    bouyer  * all copies or substantial portions of the Software.
     21       1.1    bouyer  *
     22       1.1    bouyer  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     23       1.1    bouyer  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     24       1.1    bouyer  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     25       1.1    bouyer  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     26       1.1    bouyer  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     27       1.1    bouyer  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
     28       1.1    bouyer  * IN THE SOFTWARE.
     29       1.1    bouyer  */
     30       1.1    bouyer 
     31       1.2    bouyer #include <sys/cdefs.h>
     32  1.18.2.1  christos __KERNEL_RCSID(0, "$NetBSD: xenbus_comms.c,v 1.18.2.1 2019/06/10 22:06:56 christos Exp $");
     33       1.2    bouyer 
     34       1.2    bouyer #include <sys/types.h>
     35       1.2    bouyer #include <sys/null.h>
     36       1.2    bouyer #include <sys/errno.h>
     37       1.2    bouyer #include <sys/param.h>
     38       1.2    bouyer #include <sys/proc.h>
     39       1.2    bouyer #include <sys/systm.h>
     40       1.2    bouyer 
     41      1.10    cegger #include <xen/xen.h>	/* for xendomain_is_dom0() */
     42       1.5    bouyer #include <xen/hypervisor.h>
     43       1.5    bouyer #include <xen/evtchn.h>
     44       1.5    bouyer #include <xen/xenbus.h>
     45       1.1    bouyer #include "xenbus_comms.h"
     46       1.1    bouyer 
     47       1.2    bouyer #undef XENDEBUG
     48       1.2    bouyer #ifdef XENDEBUG
     49       1.2    bouyer #define XENPRINTF(x) printf x
     50       1.2    bouyer #else
     51       1.2    bouyer #define XENPRINTF(x)
     52       1.2    bouyer #endif
     53       1.2    bouyer 
     54      1.16    cherry static struct intrhand *ih;
     55       1.2    bouyer struct xenstore_domain_interface *xenstore_interface;
     56       1.2    bouyer 
     57      1.15   msaitoh extern int xenstored_ready;
     58       1.2    bouyer // static DECLARE_WORK(probe_work, xenbus_probe, NULL);
     59       1.2    bouyer 
     60       1.2    bouyer static int wake_waiting(void *);
     61       1.2    bouyer static int check_indexes(XENSTORE_RING_IDX, XENSTORE_RING_IDX);
     62       1.4  christos static void *get_output_chunk(XENSTORE_RING_IDX, XENSTORE_RING_IDX,
     63       1.2    bouyer     char *, uint32_t *);
     64       1.4  christos static const void *get_input_chunk(XENSTORE_RING_IDX, XENSTORE_RING_IDX,
     65       1.2    bouyer     const char *, uint32_t *);
     66       1.1    bouyer 
     67       1.1    bouyer 
     68       1.2    bouyer static inline struct xenstore_domain_interface *
     69       1.2    bouyer xenstore_domain_interface(void)
     70       1.1    bouyer {
     71       1.2    bouyer 	return xenstore_interface;
     72       1.1    bouyer }
     73       1.1    bouyer 
     74       1.2    bouyer static int
     75       1.2    bouyer wake_waiting(void *arg)
     76       1.1    bouyer {
     77       1.8    cegger 	if (__predict_false(xenstored_ready == 0 && xendomain_is_dom0())) {
     78      1.15   msaitoh 		xenstored_ready = 1;
     79       1.3    bouyer 		wakeup(&xenstored_ready);
     80       1.1    bouyer 	}
     81       1.1    bouyer 
     82       1.2    bouyer 	wakeup(&xenstore_interface);
     83       1.2    bouyer 	return 1;
     84       1.1    bouyer }
     85       1.1    bouyer 
     86       1.2    bouyer static int
     87       1.2    bouyer check_indexes(XENSTORE_RING_IDX cons, XENSTORE_RING_IDX prod)
     88       1.1    bouyer {
     89       1.1    bouyer 	return ((prod - cons) <= XENSTORE_RING_SIZE);
     90       1.1    bouyer }
     91       1.1    bouyer 
     92       1.2    bouyer static void *
     93       1.2    bouyer get_output_chunk(XENSTORE_RING_IDX cons,
     94       1.1    bouyer 			      XENSTORE_RING_IDX prod,
     95       1.1    bouyer 			      char *buf, uint32_t *len)
     96       1.1    bouyer {
     97       1.1    bouyer 	*len = XENSTORE_RING_SIZE - MASK_XENSTORE_IDX(prod);
     98       1.1    bouyer 	if ((XENSTORE_RING_SIZE - (prod - cons)) < *len)
     99       1.1    bouyer 		*len = XENSTORE_RING_SIZE - (prod - cons);
    100       1.1    bouyer 	return buf + MASK_XENSTORE_IDX(prod);
    101       1.1    bouyer }
    102       1.1    bouyer 
    103       1.2    bouyer static const void *
    104       1.2    bouyer get_input_chunk(XENSTORE_RING_IDX cons,
    105       1.1    bouyer 				   XENSTORE_RING_IDX prod,
    106       1.1    bouyer 				   const char *buf, uint32_t *len)
    107       1.1    bouyer {
    108       1.1    bouyer 	*len = XENSTORE_RING_SIZE - MASK_XENSTORE_IDX(cons);
    109       1.1    bouyer 	if ((prod - cons) < *len)
    110       1.1    bouyer 		*len = prod - cons;
    111       1.1    bouyer 	return buf + MASK_XENSTORE_IDX(cons);
    112       1.1    bouyer }
    113       1.1    bouyer 
    114       1.2    bouyer int
    115       1.2    bouyer xb_write(const void *data, unsigned len)
    116       1.1    bouyer {
    117       1.1    bouyer 	struct xenstore_domain_interface *intf = xenstore_domain_interface();
    118       1.1    bouyer 	XENSTORE_RING_IDX cons, prod;
    119       1.1    bouyer 
    120       1.2    bouyer 	int s = spltty();
    121       1.2    bouyer 
    122       1.1    bouyer 	while (len != 0) {
    123       1.1    bouyer 		void *dst;
    124       1.1    bouyer 		unsigned int avail;
    125       1.1    bouyer 
    126       1.2    bouyer 		while ((intf->req_prod - intf->req_cons) == XENSTORE_RING_SIZE) {
    127       1.2    bouyer 			XENPRINTF(("xb_write tsleep\n"));
    128       1.2    bouyer 			tsleep(&xenstore_interface, PRIBIO, "wrst", 0);
    129       1.2    bouyer 			XENPRINTF(("xb_write tsleep done\n"));
    130       1.2    bouyer 		}
    131       1.1    bouyer 
    132       1.1    bouyer 		/* Read indexes, then verify. */
    133       1.1    bouyer 		cons = intf->req_cons;
    134       1.1    bouyer 		prod = intf->req_prod;
    135      1.12       jym 		xen_rmb();
    136       1.2    bouyer 		if (!check_indexes(cons, prod)) {
    137       1.2    bouyer 			splx(s);
    138       1.2    bouyer 			return EIO;
    139       1.2    bouyer 		}
    140       1.1    bouyer 
    141       1.1    bouyer 		dst = get_output_chunk(cons, prod, intf->req, &avail);
    142       1.1    bouyer 		if (avail == 0)
    143       1.1    bouyer 			continue;
    144       1.1    bouyer 		if (avail > len)
    145       1.1    bouyer 			avail = len;
    146       1.1    bouyer 
    147       1.1    bouyer 		memcpy(dst, data, avail);
    148       1.2    bouyer 		data = (const char *)data + avail;
    149       1.1    bouyer 		len -= avail;
    150       1.1    bouyer 
    151       1.1    bouyer 		/* Other side must not see new header until data is there. */
    152      1.12       jym 		xen_rmb();
    153       1.1    bouyer 		intf->req_prod += avail;
    154      1.12       jym 		xen_rmb();
    155       1.1    bouyer 
    156       1.2    bouyer 		hypervisor_notify_via_evtchn(xen_start_info.store_evtchn);
    157       1.1    bouyer 	}
    158       1.1    bouyer 
    159       1.2    bouyer 	splx(s);
    160       1.1    bouyer 	return 0;
    161       1.1    bouyer }
    162       1.1    bouyer 
    163       1.2    bouyer int
    164       1.2    bouyer xb_read(void *data, unsigned len)
    165       1.1    bouyer {
    166       1.1    bouyer 	struct xenstore_domain_interface *intf = xenstore_domain_interface();
    167       1.1    bouyer 	XENSTORE_RING_IDX cons, prod;
    168       1.1    bouyer 
    169       1.2    bouyer 	int s = spltty();
    170       1.2    bouyer 
    171       1.1    bouyer 	while (len != 0) {
    172       1.1    bouyer 		unsigned int avail;
    173       1.1    bouyer 		const char *src;
    174       1.1    bouyer 
    175       1.2    bouyer 		while (intf->rsp_cons == intf->rsp_prod)
    176       1.2    bouyer 			tsleep(&xenstore_interface, PRIBIO, "rdst", 0);
    177       1.1    bouyer 
    178       1.1    bouyer 		/* Read indexes, then verify. */
    179       1.1    bouyer 		cons = intf->rsp_cons;
    180       1.1    bouyer 		prod = intf->rsp_prod;
    181      1.12       jym 		xen_rmb();
    182       1.2    bouyer 		if (!check_indexes(cons, prod)) {
    183       1.2    bouyer 			XENPRINTF(("xb_read EIO\n"));
    184       1.2    bouyer 			splx(s);
    185       1.2    bouyer 			return EIO;
    186       1.2    bouyer 		}
    187       1.1    bouyer 
    188       1.1    bouyer 		src = get_input_chunk(cons, prod, intf->rsp, &avail);
    189       1.1    bouyer 		if (avail == 0)
    190       1.1    bouyer 			continue;
    191       1.1    bouyer 		if (avail > len)
    192       1.1    bouyer 			avail = len;
    193       1.1    bouyer 
    194       1.1    bouyer 		/* We must read header before we read data. */
    195      1.12       jym 		xen_rmb();
    196       1.1    bouyer 
    197       1.1    bouyer 		memcpy(data, src, avail);
    198       1.2    bouyer 		data = (char *)data + avail;
    199       1.1    bouyer 		len -= avail;
    200       1.1    bouyer 
    201       1.1    bouyer 		/* Other side must not see free space until we've copied out */
    202      1.12       jym 		xen_rmb();
    203       1.1    bouyer 		intf->rsp_cons += avail;
    204      1.12       jym 		xen_rmb();
    205       1.1    bouyer 
    206       1.2    bouyer 		XENPRINTF(("Finished read of %i bytes (%i to go)\n",
    207       1.2    bouyer 		    avail, len));
    208       1.1    bouyer 
    209       1.2    bouyer 		hypervisor_notify_via_evtchn(xen_start_info.store_evtchn);
    210       1.1    bouyer 	}
    211       1.1    bouyer 
    212       1.2    bouyer 	splx(s);
    213       1.1    bouyer 	return 0;
    214       1.1    bouyer }
    215       1.1    bouyer 
    216       1.9       jym /* Set up interrupt handler of store event channel. */
    217       1.2    bouyer int
    218       1.7    cegger xb_init_comms(device_t dev)
    219       1.1    bouyer {
    220      1.14       jym 	int evtchn;
    221      1.14       jym 
    222      1.14       jym 	evtchn = xen_start_info.store_evtchn;
    223       1.1    bouyer 
    224  1.18.2.1  christos 	ih = xen_intr_establish_xname(-1, &xen_pic, evtchn, IST_LEVEL, IPL_TTY,
    225      1.18  jdolecek 	    wake_waiting, NULL, false, device_xname(dev));
    226      1.16    cherry 
    227  1.18.2.1  christos 	hypervisor_unmask_event(evtchn);
    228      1.14       jym 	aprint_verbose_dev(dev, "using event channel %d\n", evtchn);
    229      1.13       jym 
    230       1.1    bouyer 	return 0;
    231       1.1    bouyer }
    232       1.1    bouyer 
    233      1.14       jym void
    234      1.14       jym xb_suspend_comms(device_t dev)
    235      1.14       jym {
    236      1.14       jym 	int evtchn;
    237      1.14       jym 
    238      1.14       jym 	evtchn = xen_start_info.store_evtchn;
    239      1.14       jym 
    240      1.14       jym 	hypervisor_mask_event(evtchn);
    241  1.18.2.1  christos 	xen_intr_disestablish(ih);
    242      1.14       jym 	aprint_verbose_dev(dev, "removed event channel %d\n", evtchn);
    243      1.14       jym }
    244      1.14       jym 
    245       1.1    bouyer /*
    246       1.1    bouyer  * Local variables:
    247       1.1    bouyer  *  c-file-style: "linux"
    248       1.1    bouyer  *  indent-tabs-mode: t
    249       1.1    bouyer  *  c-indent-level: 8
    250       1.1    bouyer  *  c-basic-offset: 8
    251       1.1    bouyer  *  tab-width: 8
    252       1.1    bouyer  * End:
    253       1.1    bouyer  */
    254