1 /******************************************************************************
4 * Low level code to talks to Xen Store: ringbuffer and event channel.
6 * Copyright (C) 2005 Rusty Russell, IBM Corporation
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version 2
10 * as published by the Free Software Foundation; or, when distributed
11 * separately from the Linux kernel or incorporated into other
12 * software packages, subject to the following license:
14 * Permission is hereby granted, free of charge, to any person obtaining a copy
15 * of this source file (the "Software"), to deal in the Software without
16 * restriction, including without limitation the rights to use, copy, modify,
17 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
18 * and to permit persons to whom the Software is furnished to do so, subject to
19 * the following conditions:
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
33 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
35 #include <linux/wait.h>
36 #include <linux/interrupt.h>
37 #include <linux/kthread.h>
38 #include <linux/sched.h>
39 #include <linux/err.h>
40 #include <xen/xenbus.h>
41 #include <asm/xen/hypervisor.h>
42 #include <xen/events.h>
46 /* A list of replies. Currently only one will ever be outstanding. */
47 LIST_HEAD(xs_reply_list
);
49 /* A list of write requests. */
50 LIST_HEAD(xb_write_list
);
51 DECLARE_WAIT_QUEUE_HEAD(xb_waitq
);
52 DEFINE_MUTEX(xb_write_mutex
);
54 /* Protect xenbus reader thread against save/restore. */
55 DEFINE_MUTEX(xs_response_mutex
);
57 static int xenbus_irq
;
58 static struct task_struct
*xenbus_task
;
60 static irqreturn_t
wake_waiting(int irq
, void *unused
)
66 static int check_indexes(XENSTORE_RING_IDX cons
, XENSTORE_RING_IDX prod
)
68 return ((prod
- cons
) <= XENSTORE_RING_SIZE
);
71 static void *get_output_chunk(XENSTORE_RING_IDX cons
,
72 XENSTORE_RING_IDX prod
,
73 char *buf
, uint32_t *len
)
75 *len
= XENSTORE_RING_SIZE
- MASK_XENSTORE_IDX(prod
);
76 if ((XENSTORE_RING_SIZE
- (prod
- cons
)) < *len
)
77 *len
= XENSTORE_RING_SIZE
- (prod
- cons
);
78 return buf
+ MASK_XENSTORE_IDX(prod
);
81 static const void *get_input_chunk(XENSTORE_RING_IDX cons
,
82 XENSTORE_RING_IDX prod
,
83 const char *buf
, uint32_t *len
)
85 *len
= XENSTORE_RING_SIZE
- MASK_XENSTORE_IDX(cons
);
86 if ((prod
- cons
) < *len
)
88 return buf
+ MASK_XENSTORE_IDX(cons
);
91 static int xb_data_to_write(void)
93 struct xenstore_domain_interface
*intf
= xen_store_interface
;
95 return (intf
->req_prod
- intf
->req_cons
) != XENSTORE_RING_SIZE
&&
96 !list_empty(&xb_write_list
);
100 * xb_write - low level write
101 * @data: buffer to send
102 * @len: length of buffer
104 * Returns number of bytes written or -err.
106 static int xb_write(const void *data
, unsigned int len
)
108 struct xenstore_domain_interface
*intf
= xen_store_interface
;
109 XENSTORE_RING_IDX cons
, prod
;
110 unsigned int bytes
= 0;
116 /* Read indexes, then verify. */
117 cons
= intf
->req_cons
;
118 prod
= intf
->req_prod
;
119 if (!check_indexes(cons
, prod
)) {
120 intf
->req_cons
= intf
->req_prod
= 0;
123 if (!xb_data_to_write())
126 /* Must write data /after/ reading the consumer index. */
129 dst
= get_output_chunk(cons
, prod
, intf
->req
, &avail
);
135 memcpy(dst
, data
, avail
);
140 /* Other side must not see new producer until data is there. */
142 intf
->req_prod
+= avail
;
144 /* Implies mb(): other side will see the updated producer. */
145 if (prod
<= intf
->req_cons
)
146 notify_remote_via_evtchn(xen_store_evtchn
);
152 static int xb_data_to_read(void)
154 struct xenstore_domain_interface
*intf
= xen_store_interface
;
155 return (intf
->rsp_cons
!= intf
->rsp_prod
);
158 static int xb_read(void *data
, unsigned int len
)
160 struct xenstore_domain_interface
*intf
= xen_store_interface
;
161 XENSTORE_RING_IDX cons
, prod
;
162 unsigned int bytes
= 0;
168 /* Read indexes, then verify. */
169 cons
= intf
->rsp_cons
;
170 prod
= intf
->rsp_prod
;
174 if (!check_indexes(cons
, prod
)) {
175 intf
->rsp_cons
= intf
->rsp_prod
= 0;
179 src
= get_input_chunk(cons
, prod
, intf
->rsp
, &avail
);
185 /* Must read data /after/ reading the producer index. */
188 memcpy(data
, src
, avail
);
193 /* Other side must not see free space until we've copied out */
195 intf
->rsp_cons
+= avail
;
197 /* Implies mb(): other side will see the updated consumer. */
198 if (intf
->rsp_prod
- cons
>= XENSTORE_RING_SIZE
)
199 notify_remote_via_evtchn(xen_store_evtchn
);
205 static int process_msg(void)
208 struct xsd_sockmsg msg
;
212 struct xs_watch_event
*watch
;
218 struct xb_req_data
*req
;
228 * We must disallow save/restore while reading a message.
229 * A partial read across s/r leaves us out of sync with
231 * xs_response_mutex is locked as long as we are processing one
232 * message. state.in_msg will be true as long as we are holding
235 mutex_lock(&xs_response_mutex
);
237 if (!xb_data_to_read()) {
238 /* We raced with save/restore: pending data 'gone'. */
239 mutex_unlock(&xs_response_mutex
);
240 state
.in_msg
= false;
246 if (state
.read
!= sizeof(state
.msg
)) {
247 err
= xb_read((void *)&state
.msg
+ state
.read
,
248 sizeof(state
.msg
) - state
.read
);
252 if (state
.read
!= sizeof(state
.msg
))
254 if (state
.msg
.len
> XENSTORE_PAYLOAD_MAX
) {
260 len
= state
.msg
.len
+ 1;
261 if (state
.msg
.type
== XS_WATCH_EVENT
)
262 len
+= sizeof(*state
.watch
);
264 state
.alloc
= kmalloc(len
, GFP_NOIO
| __GFP_HIGH
);
268 if (state
.msg
.type
== XS_WATCH_EVENT
)
269 state
.body
= state
.watch
->body
;
271 state
.body
= state
.alloc
;
272 state
.in_hdr
= false;
276 err
= xb_read(state
.body
+ state
.read
, state
.msg
.len
- state
.read
);
281 if (state
.read
!= state
.msg
.len
)
284 state
.body
[state
.msg
.len
] = '\0';
286 if (state
.msg
.type
== XS_WATCH_EVENT
) {
287 state
.watch
->len
= state
.msg
.len
;
288 err
= xs_watch_msg(state
.watch
);
291 mutex_lock(&xb_write_mutex
);
292 list_for_each_entry(req
, &xs_reply_list
, list
) {
293 if (req
->msg
.req_id
== state
.msg
.req_id
) {
294 list_del(&req
->list
);
299 mutex_unlock(&xb_write_mutex
);
303 if (req
->state
== xb_req_state_wait_reply
) {
304 req
->msg
.req_id
= req
->caller_req_id
;
305 req
->msg
.type
= state
.msg
.type
;
306 req
->msg
.len
= state
.msg
.len
;
307 req
->body
= state
.body
;
308 /* write body, then update state */
310 req
->state
= xb_req_state_got_reply
;
316 mutex_unlock(&xs_response_mutex
);
318 state
.in_msg
= false;
323 mutex_unlock(&xs_response_mutex
);
324 state
.in_msg
= false;
330 static int process_writes(void)
333 struct xb_req_data
*req
;
335 unsigned int written
;
341 if (!xb_data_to_write())
344 mutex_lock(&xb_write_mutex
);
347 state
.req
= list_first_entry(&xb_write_list
,
348 struct xb_req_data
, list
);
353 if (state
.req
->state
== xb_req_state_aborted
)
356 while (state
.idx
< state
.req
->num_vecs
) {
358 base
= &state
.req
->msg
;
359 len
= sizeof(state
.req
->msg
);
361 base
= state
.req
->vec
[state
.idx
].iov_base
;
362 len
= state
.req
->vec
[state
.idx
].iov_len
;
364 err
= xb_write(base
+ state
.written
, len
- state
.written
);
367 state
.written
+= err
;
368 if (state
.written
!= len
)
375 list_del(&state
.req
->list
);
376 state
.req
->state
= xb_req_state_wait_reply
;
377 list_add_tail(&state
.req
->list
, &xs_reply_list
);
381 mutex_unlock(&xb_write_mutex
);
386 state
.req
->msg
.type
= XS_ERROR
;
387 state
.req
->err
= err
;
388 list_del(&state
.req
->list
);
389 if (state
.req
->state
== xb_req_state_aborted
)
392 /* write err, then update state */
394 state
.req
->state
= xb_req_state_got_reply
;
395 wake_up(&state
.req
->wq
);
398 mutex_unlock(&xb_write_mutex
);
405 static int xb_thread_work(void)
407 return xb_data_to_read() || xb_data_to_write();
410 static int xenbus_thread(void *unused
)
414 while (!kthread_should_stop()) {
415 if (wait_event_interruptible(xb_waitq
, xb_thread_work()))
422 pr_warn_ratelimited("error %d while reading message\n",
425 err
= process_writes();
427 pr_warn_ratelimited("error %d while writing message\n",
436 * xb_init_comms - Set up interrupt handler off store event channel.
438 int xb_init_comms(void)
440 struct xenstore_domain_interface
*intf
= xen_store_interface
;
442 if (intf
->req_prod
!= intf
->req_cons
)
443 pr_err("request ring is not quiescent (%08x:%08x)!\n",
444 intf
->req_cons
, intf
->req_prod
);
446 if (intf
->rsp_prod
!= intf
->rsp_cons
) {
447 pr_warn("response ring is not quiescent (%08x:%08x): fixing up\n",
448 intf
->rsp_cons
, intf
->rsp_prod
);
451 intf
->rsp_cons
= intf
->rsp_prod
;
455 /* Already have an irq; assume we're resuming */
456 rebind_evtchn_irq(xen_store_evtchn
, xenbus_irq
);
460 err
= bind_evtchn_to_irqhandler(xen_store_evtchn
, wake_waiting
,
461 0, "xenbus", &xb_waitq
);
463 pr_err("request irq failed %i\n", err
);
470 xenbus_task
= kthread_run(xenbus_thread
, NULL
,
472 if (IS_ERR(xenbus_task
))
473 return PTR_ERR(xenbus_task
);
480 void xb_deinit_comms(void)
482 unbind_from_irqhandler(xenbus_irq
, &xb_waitq
);