1 // SPDX-License-Identifier: GPL-2.0
3 * (C) 2006 - Cambridge University
4 * (C) 2020 - EPAM Systems Inc.
7 * Author: Steven Smith (sos22@cam.ac.uk)
8 * Changes: Grzegorz Milos (gm281@cam.ac.uk)
9 * Changes: John D. Ramsdell
11 * Date: Jun 2006, changes Aug 2006
13 * Description: Minimal implementation of xenbus
15 * [1] - http://xenbits.xen.org/gitweb/?p=mini-os.git;a=summary
20 #include <asm/armv8/mmu.h>
22 #include <asm/xen/system.h>
24 #include <linux/bug.h>
25 #include <linux/compat.h>
27 #include <xen/events.h>
29 #include <xen/xenbus.h>
31 #include <xen/interface/io/xs_wire.h>
33 #define map_frame_virt(v) (v << PAGE_SHIFT)
37 /* Wait for reply time out, ms */
38 #define WAIT_XENBUS_TO_MS 5000
39 /* Polling time out, ms */
40 #define WAIT_XENBUS_POLL_TO_MS 1
42 static struct xenstore_domain_interface *xenstore_buf;
44 static char *errmsg(struct xsd_sockmsg *rep);
53 static void memcpy_from_ring(const void *r, void *d, int off, int len)
59 c1 = min(len, XENSTORE_RING_SIZE - off);
61 memcpy(dest, ring + off, c1);
62 memcpy(dest + c1, ring, c2);
66 * xenbus_get_reply() - Receive reply from xenbus
67 * @req_reply: reply message structure
69 * Wait for reply message event from the ring and copy received message
70 * to input xsd_sockmsg structure. Repeat until full reply is
73 * Return: false - timeout
74 * true - reply is received
76 static bool xenbus_get_reply(struct xsd_sockmsg **req_reply)
78 struct xsd_sockmsg msg;
79 unsigned int prod = xenstore_buf->rsp_prod;
82 if (!wait_event_timeout(NULL, prod != xenstore_buf->rsp_prod,
84 printk("%s: wait_event timeout\n", __func__);
88 prod = xenstore_buf->rsp_prod;
89 if (xenstore_buf->rsp_prod - xenstore_buf->rsp_cons < sizeof(msg))
93 memcpy_from_ring(xenstore_buf->rsp, &msg,
94 MASK_XENSTORE_IDX(xenstore_buf->rsp_cons),
97 if (xenstore_buf->rsp_prod - xenstore_buf->rsp_cons < sizeof(msg) + msg.len)
100 /* We do not support and expect any Xen bus wathes. */
101 BUG_ON(msg.type == XS_WATCH_EVENT);
103 *req_reply = malloc(sizeof(msg) + msg.len);
104 memcpy_from_ring(xenstore_buf->rsp, *req_reply,
105 MASK_XENSTORE_IDX(xenstore_buf->rsp_cons),
106 msg.len + sizeof(msg));
108 xenstore_buf->rsp_cons += msg.len + sizeof(msg);
111 notify_remote_via_evtchn(xenbus_evtchn);
115 char *xenbus_switch_state(xenbus_transaction_t xbt, const char *path,
127 if (xbt == XBT_NIL) {
128 msg = xenbus_transaction_start(&xbt);
134 msg = xenbus_read(xbt, path, ¤t_state);
138 rs = (XenbusState)(current_state[0] - '0');
145 snprintf(value, 2, "%d", state);
146 msg = xenbus_write(xbt, path, value);
150 msg2 = xenbus_transaction_end(xbt, 0, &retry);
153 if (msg == NULL && msg2 != NULL)
162 char *xenbus_wait_for_state_change(const char *path, XenbusState *state)
168 msg = xenbus_read(XBT_NIL, path, &res);
172 rs = (XenbusState)(res[0] - 48);
176 wait_event_timeout(NULL, false, WAIT_XENBUS_POLL_TO_MS);
185 /* Send data to xenbus. This can block. All of the requests are seen
186 * by xenbus as if sent atomically. The header is added
187 * automatically, using type %type, req_id %req_id, and trans_id
190 static void xb_write(int type, int req_id, xenbus_transaction_t trans_id,
191 const struct write_req *req, int nr_reqs)
193 XENSTORE_RING_IDX prod;
196 const struct write_req *cur_req;
200 struct xsd_sockmsg m = {
205 struct write_req header_req = {
210 for (r = 0; r < nr_reqs; r++)
215 cur_req = &header_req;
217 BUG_ON(len > XENSTORE_RING_SIZE);
218 prod = xenstore_buf->req_prod;
219 /* We are running synchronously, so it is a bug if we do not
220 * have enough room to send a message: please note that a message
221 * can occupy multiple slots in the ring buffer.
223 BUG_ON(prod + len - xenstore_buf->req_cons > XENSTORE_RING_SIZE);
227 while (total_off < len) {
228 this_chunk = min(cur_req->len - req_off,
229 XENSTORE_RING_SIZE - MASK_XENSTORE_IDX(prod));
230 memcpy((char *)xenstore_buf->req + MASK_XENSTORE_IDX(prod),
231 (char *)cur_req->data + req_off, this_chunk);
233 req_off += this_chunk;
234 total_off += this_chunk;
235 if (req_off == cur_req->len) {
237 if (cur_req == &header_req)
244 BUG_ON(req_off != 0);
245 BUG_ON(total_off != len);
246 BUG_ON(prod > xenstore_buf->req_cons + XENSTORE_RING_SIZE);
248 /* Remote must see entire message before updating indexes */
251 xenstore_buf->req_prod += len;
253 /* Send evtchn to notify remote */
254 notify_remote_via_evtchn(xenbus_evtchn);
257 /* Send a message to xenbus, in the same fashion as xb_write, and
258 * block waiting for a reply. The reply is malloced and should be
259 * freed by the caller.
261 struct xsd_sockmsg *xenbus_msg_reply(int type,
262 xenbus_transaction_t trans,
263 struct write_req *io,
266 struct xsd_sockmsg *rep;
268 /* We do not use request identifier which is echoed in daemon's response. */
269 xb_write(type, 0, trans, io, nr_reqs);
270 /* Now wait for the message to arrive. */
271 if (!xenbus_get_reply(&rep))
276 static char *errmsg(struct xsd_sockmsg *rep)
281 char msg[] = "No reply";
282 size_t len = strlen(msg) + 1;
284 return memcpy(malloc(len), msg, len);
286 if (rep->type != XS_ERROR)
288 res = malloc(rep->len + 1);
289 memcpy(res, rep + 1, rep->len);
295 /* List the contents of a directory. Returns a malloc()ed array of
296 * pointers to malloc()ed strings. The array is NULL terminated. May
299 char *xenbus_ls(xenbus_transaction_t xbt, const char *pre, char ***contents)
301 struct xsd_sockmsg *reply, *repmsg;
302 struct write_req req[] = { { pre, strlen(pre) + 1 } };
306 repmsg = xenbus_msg_reply(XS_DIRECTORY, xbt, req, ARRAY_SIZE(req));
307 msg = errmsg(repmsg);
313 for (x = nr_elems = 0; x < repmsg->len; x++)
314 nr_elems += (((char *)reply)[x] == 0);
315 res = malloc(sizeof(res[0]) * (nr_elems + 1));
316 for (x = i = 0; i < nr_elems; i++) {
317 int l = strlen((char *)reply + x);
319 res[i] = malloc(l + 1);
320 memcpy(res[i], (char *)reply + x, l + 1);
329 char *xenbus_read(xenbus_transaction_t xbt, const char *path, char **value)
331 struct write_req req[] = { {path, strlen(path) + 1} };
332 struct xsd_sockmsg *rep;
335 rep = xenbus_msg_reply(XS_READ, xbt, req, ARRAY_SIZE(req));
341 res = malloc(rep->len + 1);
342 memcpy(res, rep + 1, rep->len);
349 char *xenbus_write(xenbus_transaction_t xbt, const char *path,
352 struct write_req req[] = {
353 {path, strlen(path) + 1},
354 {value, strlen(value)},
356 struct xsd_sockmsg *rep;
359 rep = xenbus_msg_reply(XS_WRITE, xbt, req, ARRAY_SIZE(req));
367 char *xenbus_rm(xenbus_transaction_t xbt, const char *path)
369 struct write_req req[] = { {path, strlen(path) + 1} };
370 struct xsd_sockmsg *rep;
373 rep = xenbus_msg_reply(XS_RM, xbt, req, ARRAY_SIZE(req));
381 char *xenbus_get_perms(xenbus_transaction_t xbt, const char *path, char **value)
383 struct write_req req[] = { {path, strlen(path) + 1} };
384 struct xsd_sockmsg *rep;
387 rep = xenbus_msg_reply(XS_GET_PERMS, xbt, req, ARRAY_SIZE(req));
393 res = malloc(rep->len + 1);
394 memcpy(res, rep + 1, rep->len);
401 #define PERM_MAX_SIZE 32
402 char *xenbus_set_perms(xenbus_transaction_t xbt, const char *path,
403 domid_t dom, char perm)
405 char value[PERM_MAX_SIZE];
406 struct write_req req[] = {
407 {path, strlen(path) + 1},
410 struct xsd_sockmsg *rep;
413 snprintf(value, PERM_MAX_SIZE, "%c%hu", perm, dom);
414 req[1].len = strlen(value) + 1;
415 rep = xenbus_msg_reply(XS_SET_PERMS, xbt, req, ARRAY_SIZE(req));
423 char *xenbus_transaction_start(xenbus_transaction_t *xbt)
425 /* Xenstored becomes angry if you send a length 0 message, so just
426 * shove a nul terminator on the end
428 struct write_req req = { "", 1};
429 struct xsd_sockmsg *rep;
432 rep = xenbus_msg_reply(XS_TRANSACTION_START, 0, &req, 1);
436 sscanf((char *)(rep + 1), "%lu", xbt);
441 char *xenbus_transaction_end(xenbus_transaction_t t, int abort, int *retry)
443 struct xsd_sockmsg *rep;
444 struct write_req req;
449 req.data = abort ? "F" : "T";
451 rep = xenbus_msg_reply(XS_TRANSACTION_END, t, &req, 1);
454 if (!strcmp(err, "EAGAIN")) {
466 int xenbus_read_integer(const char *path)
471 res = xenbus_read(XBT_NIL, path, &buf);
473 printk("Failed to read %s.\n", path);
477 sscanf(buf, "%d", &t);
482 int xenbus_read_uuid(const char *path, unsigned char uuid[16])
486 res = xenbus_read(XBT_NIL, path, &buf);
488 printk("Failed to read %s.\n", path);
492 if (strlen(buf) != ((2 * 16) + 4) /* 16 hex bytes and 4 hyphens */
494 "%2hhx%2hhx%2hhx%2hhx-"
498 "%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx",
499 uuid, uuid + 1, uuid + 2, uuid + 3,
500 uuid + 4, uuid + 5, uuid + 6, uuid + 7,
501 uuid + 8, uuid + 9, uuid + 10, uuid + 11,
502 uuid + 12, uuid + 13, uuid + 14, uuid + 15) != 16) {
503 printk("Xenbus path %s value %s is not a uuid!\n", path, buf);
511 char *xenbus_printf(xenbus_transaction_t xbt,
512 const char *node, const char *path,
513 const char *fmt, ...)
515 #define BUFFER_SIZE 256
516 char fullpath[BUFFER_SIZE];
517 char val[BUFFER_SIZE];
520 BUG_ON(strlen(node) + strlen(path) + 1 >= BUFFER_SIZE);
521 sprintf(fullpath, "%s/%s", node, path);
523 vsprintf(val, fmt, args);
525 return xenbus_write(xbt, fullpath, val);
528 domid_t xenbus_get_self_id(void)
533 BUG_ON(xenbus_read(XBT_NIL, "domid", &dom_id));
534 sscanf(dom_id, "%"SCNd16, &ret);
539 void init_xenbus(void)
543 debug("%s\n", __func__);
544 if (hvm_get_parameter(HVM_PARAM_STORE_EVTCHN, &v))
548 if (hvm_get_parameter(HVM_PARAM_STORE_PFN, &v))
550 xenstore_buf = (struct xenstore_domain_interface *)map_frame_virt(v);
553 void fini_xenbus(void)
555 debug("%s\n", __func__);