store.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. * Copyright (C) 2006-2009 Free Software Foundation
  3. *
  4. * This program is free software ; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation ; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY ; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with the program ; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <sys/types.h>
  19. #include <mach/mig_support.h>
  20. #include <machine/pmap.h>
  21. #include <machine/ipl.h>
  22. #include <kern/kalloc.h>
  23. #include <stdarg.h>
  24. #include <string.h>
  25. #include <alloca.h>
  26. #include <xen/public/xen.h>
  27. #include <xen/public/io/xs_wire.h>
  28. #include <util/atoi.h>
  29. #include "store.h"
  30. #include "ring.h"
  31. #include "evt.h"
  32. #include "xen.h"
  33. /* TODO use events instead of just yielding */
  34. /* Hypervisor part */
  35. decl_simple_lock_data(static, lock);
  36. static struct xenstore_domain_interface *store;
  37. struct store_req {
  38. const char *data;
  39. unsigned len;
  40. };
  41. /* Send a request */
  42. static void store_put(hyp_store_transaction_t t, uint32_t type, struct store_req *req, unsigned nr_reqs) {
  43. struct xsd_sockmsg head = {
  44. .type = type,
  45. .req_id = 0,
  46. .tx_id = t,
  47. };
  48. unsigned totlen, len;
  49. unsigned i;
  50. totlen = 0;
  51. for (i = 0; i < nr_reqs; i++)
  52. totlen += req[i].len;
  53. head.len = totlen;
  54. totlen += sizeof(head);
  55. if (totlen > sizeof(store->req) - 1)
  56. panic("too big store message %d, max %d", totlen, sizeof(store->req));
  57. while (hyp_ring_available(store->req, store->req_prod, store->req_cons) < totlen)
  58. hyp_yield();
  59. mb();
  60. hyp_ring_store(&hyp_ring_cell(store->req, store->req_prod), &head, sizeof(head), store->req, store->req + sizeof(store->req));
  61. len = sizeof(head);
  62. for (i=0; i<nr_reqs; i++) {
  63. hyp_ring_store(&hyp_ring_cell(store->req, store->req_prod + len), req[i].data, req[i].len, store->req, store->req + sizeof(store->req));
  64. len += req[i].len;
  65. }
  66. wmb();
  67. store->req_prod += totlen;
  68. hyp_event_channel_send(boot_info.store_evtchn);
  69. }
  70. static const char *errors[] = {
  71. "EINVAL",
  72. "EACCES",
  73. "EEXIST",
  74. "EISDIR",
  75. "ENOENT",
  76. "ENOMEM",
  77. "ENOSPC",
  78. "EIO",
  79. "ENOTEMPTY",
  80. "ENOSYS",
  81. "EROFS",
  82. "EBUSY",
  83. "EAGAIN",
  84. "EISCONN",
  85. NULL,
  86. };
  87. /* Send a request and wait for a reply, whose header is put in head, and
  88. * data is returned (beware, that's in the ring !)
  89. * On error, returns NULL. Else takes the lock and return pointer on data and
  90. * store_put_wait_end shall be called after reading it. */
  91. static struct xsd_sockmsg head;
  92. const char *hyp_store_error;
  93. static void *store_put_wait(hyp_store_transaction_t t, uint32_t type, struct store_req *req, unsigned nr_reqs) {
  94. unsigned len;
  95. const char **error;
  96. void *data;
  97. simple_lock(&lock);
  98. store_put(t, type, req, nr_reqs);
  99. again:
  100. while (store->rsp_prod - store->rsp_cons < sizeof(head))
  101. hyp_yield();
  102. rmb();
  103. hyp_ring_fetch(&head, &hyp_ring_cell(store->rsp, store->rsp_cons), sizeof(head), store->rsp, store->rsp + sizeof(store->rsp));
  104. len = sizeof(head) + head.len;
  105. while (store->rsp_prod - store->rsp_cons < len)
  106. hyp_yield();
  107. rmb();
  108. if (head.type == XS_WATCH_EVENT) {
  109. /* Spurious watch event, drop */
  110. store->rsp_cons += sizeof(head) + head.len;
  111. hyp_event_channel_send(boot_info.store_evtchn);
  112. goto again;
  113. }
  114. data = &hyp_ring_cell(store->rsp, store->rsp_cons + sizeof(head));
  115. if (head.len <= 10) {
  116. char c[10];
  117. hyp_ring_fetch(c, data, head.len, store->rsp, store->rsp + sizeof(store->rsp));
  118. for (error = errors; *error; error++) {
  119. if (head.len == strlen(*error) + 1 && !memcmp(*error, c, head.len)) {
  120. hyp_store_error = *error;
  121. store->rsp_cons += len;
  122. hyp_event_channel_send(boot_info.store_evtchn);
  123. simple_unlock(&lock);
  124. return NULL;
  125. }
  126. }
  127. }
  128. return data;
  129. }
  130. /* Must be called after each store_put_wait. Releases lock. */
  131. static void store_put_wait_end(void) {
  132. mb();
  133. store->rsp_cons += sizeof(head) + head.len;
  134. hyp_event_channel_send(boot_info.store_evtchn);
  135. simple_unlock(&lock);
  136. }
  137. /* Start a transaction. */
  138. hyp_store_transaction_t hyp_store_transaction_start(void) {
  139. struct store_req req = {
  140. .data = "",
  141. .len = 1,
  142. };
  143. char *rep;
  144. char *s;
  145. int i;
  146. rep = store_put_wait(0, XS_TRANSACTION_START, &req, 1);
  147. if (!rep)
  148. panic("couldn't start transaction (%s)", hyp_store_error);
  149. s = alloca(head.len);
  150. hyp_ring_fetch(s, rep, head.len, store->rsp, store->rsp + sizeof(store->rsp));
  151. mach_atoi((u_char*) s, &i);
  152. if (i == MACH_ATOI_DEFAULT)
  153. panic("bogus transaction id len %d '%s'", head.len, s);
  154. store_put_wait_end();
  155. return i;
  156. }
  157. /* Stop a transaction. */
  158. int hyp_store_transaction_stop(hyp_store_transaction_t t) {
  159. struct store_req req = {
  160. .data = "T",
  161. .len = 2,
  162. };
  163. int ret = 1;
  164. void *rep;
  165. rep = store_put_wait(t, XS_TRANSACTION_END, &req, 1);
  166. if (!rep)
  167. return 0;
  168. store_put_wait_end();
  169. return ret;
  170. }
  171. /* List a directory: returns an array to file names, terminated by NULL. Free
  172. * with kfree. */
  173. char **hyp_store_ls(hyp_store_transaction_t t, int n, ...) {
  174. struct store_req req[n];
  175. va_list listp;
  176. int i;
  177. char *rep;
  178. char *c;
  179. char **res, **rsp;
  180. va_start (listp, n);
  181. for (i = 0; i < n; i++) {
  182. req[i].data = va_arg(listp, char *);
  183. req[i].len = strlen(req[i].data);
  184. }
  185. req[n - 1].len++;
  186. va_end (listp);
  187. rep = store_put_wait(t, XS_DIRECTORY, req, n);
  188. if (!rep)
  189. return NULL;
  190. i = 0;
  191. for ( c = rep, n = 0;
  192. n < head.len;
  193. n += hyp_ring_next_word(&c, store->rsp, store->rsp + sizeof(store->rsp)) + 1)
  194. i++;
  195. res = (void*) kalloc((i + 1) * sizeof(char*) + head.len);
  196. if (!res)
  197. hyp_store_error = "ENOMEM";
  198. else {
  199. hyp_ring_fetch(res + (i + 1), rep, head.len, store->rsp, store->rsp + sizeof(store->rsp));
  200. rsp = res;
  201. for (c = (char*) (res + (i + 1)); i; i--, c += strlen(c) + 1)
  202. *rsp++ = c;
  203. *rsp = NULL;
  204. }
  205. store_put_wait_end();
  206. return res;
  207. }
  208. /* Get the value of an entry, va version. */
  209. static void *hyp_store_read_va(hyp_store_transaction_t t, int n, va_list listp) {
  210. struct store_req req[n];
  211. int i;
  212. void *rep;
  213. char *res;
  214. for (i = 0; i < n; i++) {
  215. req[i].data = va_arg(listp, char *);
  216. req[i].len = strlen(req[i].data);
  217. }
  218. req[n - 1].len++;
  219. rep = store_put_wait(t, XS_READ, req, n);
  220. if (!rep)
  221. return NULL;
  222. res = (void*) kalloc(head.len + 1);
  223. if (!res)
  224. hyp_store_error = "ENOMEM";
  225. else {
  226. hyp_ring_fetch(res, rep, head.len, store->rsp, store->rsp + sizeof(store->rsp));
  227. res[head.len] = 0;
  228. }
  229. store_put_wait_end();
  230. return res;
  231. }
  232. /* Get the value of an entry. Free with kfree. */
  233. void *hyp_store_read(hyp_store_transaction_t t, int n, ...) {
  234. va_list listp;
  235. char *res;
  236. va_start(listp, n);
  237. res = hyp_store_read_va(t, n, listp);
  238. va_end(listp);
  239. return res;
  240. }
  241. /* Get the integer value of an entry, -1 on error. */
  242. int hyp_store_read_int(hyp_store_transaction_t t, int n, ...) {
  243. va_list listp;
  244. char *res;
  245. int i;
  246. va_start(listp, n);
  247. res = hyp_store_read_va(t, n, listp);
  248. va_end(listp);
  249. if (!res)
  250. return -1;
  251. mach_atoi((u_char *) res, &i);
  252. if (i == MACH_ATOI_DEFAULT)
  253. printf("bogus integer '%s'\n", res);
  254. kfree((vm_offset_t) res, strlen(res)+1);
  255. return i;
  256. }
  257. /* Set the value of an entry. */
  258. char *hyp_store_write(hyp_store_transaction_t t, const char *data, int n, ...) {
  259. struct store_req req[n + 1];
  260. va_list listp;
  261. int i;
  262. void *rep;
  263. char *res;
  264. va_start (listp, n);
  265. for (i = 0; i < n; i++) {
  266. req[i].data = va_arg(listp, char *);
  267. req[i].len = strlen(req[i].data);
  268. }
  269. req[n - 1].len++;
  270. req[n].data = data;
  271. req[n].len = strlen (data);
  272. va_end (listp);
  273. rep = store_put_wait (t, XS_WRITE, req, n + 1);
  274. if (!rep)
  275. return NULL;
  276. res = (void*) kalloc(head.len + 1);
  277. if (!res)
  278. hyp_store_error = NULL;
  279. else {
  280. hyp_ring_fetch(res, rep, head.len, store->rsp, store->rsp + sizeof(store->rsp));
  281. res[head.len] = 0;
  282. }
  283. store_put_wait_end();
  284. return res;
  285. }
  286. static void hyp_store_handler(int unit)
  287. {
  288. thread_wakeup(&boot_info.store_evtchn);
  289. }
  290. /* Map store's shared page. */
  291. void hyp_store_init(void)
  292. {
  293. if (store)
  294. return;
  295. simple_lock_init(&lock);
  296. store = (void*) mfn_to_kv(boot_info.store_mfn);
  297. #ifdef MACH_PV_PAGETABLES
  298. pmap_set_page_readwrite(store);
  299. #endif /* MACH_PV_PAGETABLES */
  300. /* SPL sched */
  301. hyp_evt_handler(boot_info.store_evtchn, hyp_store_handler, 0, SPL7);
  302. }