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