xref: /netbsd-src/sys/arch/xen/xenbus/xenbus_comms.c (revision 51dd0b0dc5ee6262ae10a3701b9d2ab40fa9830e)
1 /* $NetBSD: xenbus_comms.c,v 1.26 2023/02/25 00:37:34 riastradh Exp $ */
2 /******************************************************************************
3  * xenbus_comms.c
4  *
5  * Low level code to talks to Xen Store: ringbuffer and event channel.
6  *
7  * Copyright (C) 2005 Rusty Russell, IBM 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 __KERNEL_RCSID(0, "$NetBSD: xenbus_comms.c,v 1.26 2023/02/25 00:37:34 riastradh Exp $");
33 
34 #include <sys/types.h>
35 #include <sys/null.h>
36 #include <sys/errno.h>
37 #include <sys/param.h>
38 #include <sys/proc.h>
39 #include <sys/systm.h>
40 #include <sys/mutex.h>
41 
42 #include <xen/xen.h>	/* for xendomain_is_dom0() */
43 #include <xen/intr.h>	/* for xendomain_is_dom0() */
44 #include <xen/hypervisor.h>
45 #include <xen/evtchn.h>
46 #include <xen/xenbus.h>
47 #include "xenbus_comms.h"
48 
49 #undef XENDEBUG
50 #ifdef XENDEBUG
51 #define XENPRINTF(x) printf x
52 #else
53 #define XENPRINTF(x)
54 #endif
55 
56 static struct intrhand *ih;
57 struct xenstore_domain_interface *xenstore_interface;
58 static kmutex_t xenstore_lock;
59 static kcondvar_t xenstore_cv;
60 
61 extern int xenstored_ready;
62 // static DECLARE_WORK(probe_work, xenbus_probe, NULL);
63 
64 static int wake_waiting(void *);
65 static int check_indexes(XENSTORE_RING_IDX, XENSTORE_RING_IDX);
66 static void *get_output_chunk(XENSTORE_RING_IDX, XENSTORE_RING_IDX,
67     char *, uint32_t *);
68 static const void *get_input_chunk(XENSTORE_RING_IDX, XENSTORE_RING_IDX,
69     const char *, uint32_t *);
70 
71 
72 static inline struct xenstore_domain_interface *
xenstore_domain_interface(void)73 xenstore_domain_interface(void)
74 {
75 	return xenstore_interface;
76 }
77 
78 static int
wake_waiting(void * arg)79 wake_waiting(void *arg)
80 {
81 	if (__predict_false(xenstored_ready == 0 && xendomain_is_dom0())) {
82 		xb_xenstored_make_ready();
83 	}
84 
85 	mutex_enter(&xenstore_lock);
86 	cv_broadcast(&xenstore_cv);
87 	mutex_exit(&xenstore_lock);
88 	return 1;
89 }
90 
91 static int
check_indexes(XENSTORE_RING_IDX cons,XENSTORE_RING_IDX prod)92 check_indexes(XENSTORE_RING_IDX cons, XENSTORE_RING_IDX prod)
93 {
94 	return ((prod - cons) <= XENSTORE_RING_SIZE);
95 }
96 
97 static void *
get_output_chunk(XENSTORE_RING_IDX cons,XENSTORE_RING_IDX prod,char * buf,uint32_t * len)98 get_output_chunk(XENSTORE_RING_IDX cons,
99 			      XENSTORE_RING_IDX prod,
100 			      char *buf, uint32_t *len)
101 {
102 	*len = XENSTORE_RING_SIZE - MASK_XENSTORE_IDX(prod);
103 	if ((XENSTORE_RING_SIZE - (prod - cons)) < *len)
104 		*len = XENSTORE_RING_SIZE - (prod - cons);
105 	return buf + MASK_XENSTORE_IDX(prod);
106 }
107 
108 static const void *
get_input_chunk(XENSTORE_RING_IDX cons,XENSTORE_RING_IDX prod,const char * buf,uint32_t * len)109 get_input_chunk(XENSTORE_RING_IDX cons,
110 				   XENSTORE_RING_IDX prod,
111 				   const char *buf, uint32_t *len)
112 {
113 	*len = XENSTORE_RING_SIZE - MASK_XENSTORE_IDX(cons);
114 	if ((prod - cons) < *len)
115 		*len = prod - cons;
116 	return buf + MASK_XENSTORE_IDX(cons);
117 }
118 
119 int
xb_write(const void * data,unsigned len)120 xb_write(const void *data, unsigned len)
121 {
122 	struct xenstore_domain_interface *intf = xenstore_domain_interface();
123 	XENSTORE_RING_IDX cons, prod;
124 
125 	mutex_enter(&xenstore_lock);
126 	while (len != 0) {
127 		void *dst;
128 		unsigned int avail;
129 
130 		while ((intf->req_prod - intf->req_cons) == XENSTORE_RING_SIZE) {
131 			XENPRINTF(("xb_write cv_wait\n"));
132 			cv_wait(&xenstore_cv, &xenstore_lock);
133 			XENPRINTF(("xb_write cv_wait done\n"));
134 		}
135 
136 		/* Read indexes, then verify. */
137 		cons = intf->req_cons;
138 		prod = intf->req_prod;
139 		xen_rmb();
140 		if (!check_indexes(cons, prod)) {
141 			mutex_exit(&xenstore_lock);
142 			return EIO;
143 		}
144 
145 		dst = get_output_chunk(cons, prod, intf->req, &avail);
146 		if (avail == 0)
147 			continue;
148 		if (avail > len)
149 			avail = len;
150 
151 		memcpy(dst, data, avail);
152 		data = (const char *)data + avail;
153 		len -= avail;
154 
155 		/* Other side must not see new header until data is there. */
156 		xen_wmb();
157 		intf->req_prod += avail;
158 		xen_wmb();
159 
160 		hypervisor_notify_via_evtchn(xen_start_info.store_evtchn);
161 	}
162 	mutex_exit(&xenstore_lock);
163 	return 0;
164 }
165 
166 int
xb_read(void * data,unsigned len)167 xb_read(void *data, unsigned len)
168 {
169 	struct xenstore_domain_interface *intf = xenstore_domain_interface();
170 	XENSTORE_RING_IDX cons, prod;
171 
172 	mutex_enter(&xenstore_lock);
173 
174 	while (len != 0) {
175 		unsigned int avail;
176 		const char *src;
177 
178 		while (intf->rsp_cons == intf->rsp_prod)
179 			cv_wait(&xenstore_cv, &xenstore_lock);
180 
181 		/* Read indexes, then verify. */
182 		cons = intf->rsp_cons;
183 		prod = intf->rsp_prod;
184 		xen_rmb();
185 		if (!check_indexes(cons, prod)) {
186 			XENPRINTF(("xb_read EIO\n"));
187 			mutex_exit(&xenstore_lock);
188 			return EIO;
189 		}
190 
191 		src = get_input_chunk(cons, prod, intf->rsp, &avail);
192 		if (avail == 0)
193 			continue;
194 		if (avail > len)
195 			avail = len;
196 
197 		/* We must read header before we read data. */
198 		xen_rmb();
199 
200 		memcpy(data, src, avail);
201 		data = (char *)data + avail;
202 		len -= avail;
203 
204 		/* Other side must not see free space until we've copied out */
205 		xen_wmb();
206 		intf->rsp_cons += avail;
207 		xen_wmb();
208 
209 		XENPRINTF(("Finished read of %i bytes (%i to go)\n",
210 		    avail, len));
211 
212 		hypervisor_notify_via_evtchn(xen_start_info.store_evtchn);
213 	}
214 	mutex_exit(&xenstore_lock);
215 	return 0;
216 }
217 
218 /* Set up interrupt handler of store event channel. */
219 int
xb_init_comms(device_t dev)220 xb_init_comms(device_t dev)
221 {
222 	mutex_init(&xenstore_lock, MUTEX_DEFAULT, IPL_TTY);
223 	cv_init(&xenstore_cv, "xsio");
224 
225 	return xb_resume_comms(dev);
226 }
227 
228 int
xb_resume_comms(device_t dev)229 xb_resume_comms(device_t dev)
230 {
231 	int evtchn;
232 
233 	evtchn = xen_start_info.store_evtchn;
234 
235 	ih = xen_intr_establish_xname(-1, &xen_pic, evtchn, IST_LEVEL, IPL_TTY,
236 	    wake_waiting, NULL, true, device_xname(dev));
237 
238 	hypervisor_unmask_event(evtchn);
239 	aprint_verbose_dev(dev, "using event channel %d\n", evtchn);
240 
241 	return 0;
242 }
243 
244 void
xb_suspend_comms(device_t dev)245 xb_suspend_comms(device_t dev)
246 {
247 	int evtchn;
248 
249 	evtchn = xen_start_info.store_evtchn;
250 
251 	hypervisor_mask_event(evtchn);
252 	xen_intr_disestablish(ih);
253 	aprint_verbose_dev(dev, "removed event channel %d\n", evtchn);
254 }
255 
256 /*
257  * Local variables:
258  *  c-file-style: "linux"
259  *  indent-tabs-mode: t
260  *  c-indent-level: 8
261  *  c-basic-offset: 8
262  *  tab-width: 8
263  * End:
264  */
265