xenbus_client.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  1. /******************************************************************************
  2. * Client-facing interface for the Xenbus driver. In other words, the
  3. * interface between the Xenbus and the device-specific code, be it the
  4. * frontend or the backend of that driver.
  5. *
  6. * Copyright (C) 2005 XenSource Ltd
  7. *
  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:
  13. *
  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:
  20. *
  21. * The above copyright notice and this permission notice shall be included in
  22. * all copies or substantial portions of the Software.
  23. *
  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
  30. * IN THE SOFTWARE.
  31. */
  32. #include <linux/mm.h>
  33. #include <linux/slab.h>
  34. #include <linux/types.h>
  35. #include <linux/spinlock.h>
  36. #include <linux/vmalloc.h>
  37. #include <linux/export.h>
  38. #include <asm/xen/hypervisor.h>
  39. #include <xen/page.h>
  40. #include <xen/interface/xen.h>
  41. #include <xen/interface/event_channel.h>
  42. #include <xen/balloon.h>
  43. #include <xen/events.h>
  44. #include <xen/grant_table.h>
  45. #include <xen/xenbus.h>
  46. #include <xen/xen.h>
  47. #include <xen/features.h>
  48. #include "xenbus.h"
  49. #define XENBUS_PAGES(_grants) (DIV_ROUND_UP(_grants, XEN_PFN_PER_PAGE))
  50. #define XENBUS_MAX_RING_PAGES (XENBUS_PAGES(XENBUS_MAX_RING_GRANTS))
  51. struct xenbus_map_node {
  52. struct list_head next;
  53. union {
  54. struct {
  55. struct vm_struct *area;
  56. } pv;
  57. struct {
  58. struct page *pages[XENBUS_MAX_RING_PAGES];
  59. unsigned long addrs[XENBUS_MAX_RING_GRANTS];
  60. void *addr;
  61. } hvm;
  62. };
  63. grant_handle_t handles[XENBUS_MAX_RING_GRANTS];
  64. unsigned int nr_handles;
  65. };
  66. struct map_ring_valloc {
  67. struct xenbus_map_node *node;
  68. /* Why do we need two arrays? See comment of __xenbus_map_ring */
  69. unsigned long addrs[XENBUS_MAX_RING_GRANTS];
  70. phys_addr_t phys_addrs[XENBUS_MAX_RING_GRANTS];
  71. struct gnttab_map_grant_ref map[XENBUS_MAX_RING_GRANTS];
  72. struct gnttab_unmap_grant_ref unmap[XENBUS_MAX_RING_GRANTS];
  73. unsigned int idx;
  74. };
  75. static DEFINE_SPINLOCK(xenbus_valloc_lock);
  76. static LIST_HEAD(xenbus_valloc_pages);
  77. struct xenbus_ring_ops {
  78. int (*map)(struct xenbus_device *dev, struct map_ring_valloc *info,
  79. grant_ref_t *gnt_refs, unsigned int nr_grefs,
  80. void **vaddr);
  81. int (*unmap)(struct xenbus_device *dev, void *vaddr);
  82. };
  83. static const struct xenbus_ring_ops *ring_ops __read_mostly;
  84. const char *xenbus_strstate(enum xenbus_state state)
  85. {
  86. static const char *const name[] = {
  87. [ XenbusStateUnknown ] = "Unknown",
  88. [ XenbusStateInitialising ] = "Initialising",
  89. [ XenbusStateInitWait ] = "InitWait",
  90. [ XenbusStateInitialised ] = "Initialised",
  91. [ XenbusStateConnected ] = "Connected",
  92. [ XenbusStateClosing ] = "Closing",
  93. [ XenbusStateClosed ] = "Closed",
  94. [XenbusStateReconfiguring] = "Reconfiguring",
  95. [XenbusStateReconfigured] = "Reconfigured",
  96. };
  97. return (state < ARRAY_SIZE(name)) ? name[state] : "INVALID";
  98. }
  99. EXPORT_SYMBOL_GPL(xenbus_strstate);
  100. /**
  101. * xenbus_watch_path - register a watch
  102. * @dev: xenbus device
  103. * @path: path to watch
  104. * @watch: watch to register
  105. * @callback: callback to register
  106. *
  107. * Register a @watch on the given path, using the given xenbus_watch structure
  108. * for storage, and the given @callback function as the callback. Return 0 on
  109. * success, or -errno on error. On success, the given @path will be saved as
  110. * @watch->node, and remains the caller's to free. On error, @watch->node will
  111. * be NULL, the device will switch to %XenbusStateClosing, and the error will
  112. * be saved in the store.
  113. */
  114. int xenbus_watch_path(struct xenbus_device *dev, const char *path,
  115. struct xenbus_watch *watch,
  116. bool (*will_handle)(struct xenbus_watch *,
  117. const char *, const char *),
  118. void (*callback)(struct xenbus_watch *,
  119. const char *, const char *))
  120. {
  121. int err;
  122. watch->node = path;
  123. watch->will_handle = will_handle;
  124. watch->callback = callback;
  125. err = register_xenbus_watch(watch);
  126. if (err) {
  127. watch->node = NULL;
  128. watch->will_handle = NULL;
  129. watch->callback = NULL;
  130. xenbus_dev_fatal(dev, err, "adding watch on %s", path);
  131. }
  132. return err;
  133. }
  134. EXPORT_SYMBOL_GPL(xenbus_watch_path);
  135. /**
  136. * xenbus_watch_pathfmt - register a watch on a sprintf-formatted path
  137. * @dev: xenbus device
  138. * @watch: watch to register
  139. * @callback: callback to register
  140. * @pathfmt: format of path to watch
  141. *
  142. * Register a watch on the given @path, using the given xenbus_watch
  143. * structure for storage, and the given @callback function as the callback.
  144. * Return 0 on success, or -errno on error. On success, the watched path
  145. * (@path/@path2) will be saved as @watch->node, and becomes the caller's to
  146. * kfree(). On error, watch->node will be NULL, so the caller has nothing to
  147. * free, the device will switch to %XenbusStateClosing, and the error will be
  148. * saved in the store.
  149. */
  150. int xenbus_watch_pathfmt(struct xenbus_device *dev,
  151. struct xenbus_watch *watch,
  152. bool (*will_handle)(struct xenbus_watch *,
  153. const char *, const char *),
  154. void (*callback)(struct xenbus_watch *,
  155. const char *, const char *),
  156. const char *pathfmt, ...)
  157. {
  158. int err;
  159. va_list ap;
  160. char *path;
  161. va_start(ap, pathfmt);
  162. path = kvasprintf(GFP_NOIO | __GFP_HIGH, pathfmt, ap);
  163. va_end(ap);
  164. if (!path) {
  165. xenbus_dev_fatal(dev, -ENOMEM, "allocating path for watch");
  166. return -ENOMEM;
  167. }
  168. err = xenbus_watch_path(dev, path, watch, will_handle, callback);
  169. if (err)
  170. kfree(path);
  171. return err;
  172. }
  173. EXPORT_SYMBOL_GPL(xenbus_watch_pathfmt);
  174. static void xenbus_switch_fatal(struct xenbus_device *, int, int,
  175. const char *, ...);
  176. static int
  177. __xenbus_switch_state(struct xenbus_device *dev,
  178. enum xenbus_state state, int depth)
  179. {
  180. /* We check whether the state is currently set to the given value, and
  181. if not, then the state is set. We don't want to unconditionally
  182. write the given state, because we don't want to fire watches
  183. unnecessarily. Furthermore, if the node has gone, we don't write
  184. to it, as the device will be tearing down, and we don't want to
  185. resurrect that directory.
  186. Note that, because of this cached value of our state, this
  187. function will not take a caller's Xenstore transaction
  188. (something it was trying to in the past) because dev->state
  189. would not get reset if the transaction was aborted.
  190. */
  191. struct xenbus_transaction xbt;
  192. int current_state;
  193. int err, abort;
  194. if (state == dev->state)
  195. return 0;
  196. again:
  197. abort = 1;
  198. err = xenbus_transaction_start(&xbt);
  199. if (err) {
  200. xenbus_switch_fatal(dev, depth, err, "starting transaction");
  201. return 0;
  202. }
  203. err = xenbus_scanf(xbt, dev->nodename, "state", "%d", &current_state);
  204. if (err != 1)
  205. goto abort;
  206. err = xenbus_printf(xbt, dev->nodename, "state", "%d", state);
  207. if (err) {
  208. xenbus_switch_fatal(dev, depth, err, "writing new state");
  209. goto abort;
  210. }
  211. abort = 0;
  212. abort:
  213. err = xenbus_transaction_end(xbt, abort);
  214. if (err) {
  215. if (err == -EAGAIN && !abort)
  216. goto again;
  217. xenbus_switch_fatal(dev, depth, err, "ending transaction");
  218. } else
  219. dev->state = state;
  220. return 0;
  221. }
  222. /**
  223. * xenbus_switch_state
  224. * @dev: xenbus device
  225. * @state: new state
  226. *
  227. * Advertise in the store a change of the given driver to the given new_state.
  228. * Return 0 on success, or -errno on error. On error, the device will switch
  229. * to XenbusStateClosing, and the error will be saved in the store.
  230. */
  231. int xenbus_switch_state(struct xenbus_device *dev, enum xenbus_state state)
  232. {
  233. return __xenbus_switch_state(dev, state, 0);
  234. }
  235. EXPORT_SYMBOL_GPL(xenbus_switch_state);
  236. int xenbus_frontend_closed(struct xenbus_device *dev)
  237. {
  238. xenbus_switch_state(dev, XenbusStateClosed);
  239. complete(&dev->down);
  240. return 0;
  241. }
  242. EXPORT_SYMBOL_GPL(xenbus_frontend_closed);
  243. static void xenbus_va_dev_error(struct xenbus_device *dev, int err,
  244. const char *fmt, va_list ap)
  245. {
  246. unsigned int len;
  247. char *printf_buffer;
  248. char *path_buffer;
  249. #define PRINTF_BUFFER_SIZE 4096
  250. printf_buffer = kmalloc(PRINTF_BUFFER_SIZE, GFP_KERNEL);
  251. if (!printf_buffer)
  252. return;
  253. len = sprintf(printf_buffer, "%i ", -err);
  254. vsnprintf(printf_buffer + len, PRINTF_BUFFER_SIZE - len, fmt, ap);
  255. dev_err(&dev->dev, "%s\n", printf_buffer);
  256. path_buffer = kasprintf(GFP_KERNEL, "error/%s", dev->nodename);
  257. if (path_buffer)
  258. xenbus_write(XBT_NIL, path_buffer, "error", printf_buffer);
  259. kfree(printf_buffer);
  260. kfree(path_buffer);
  261. }
  262. /**
  263. * xenbus_dev_error
  264. * @dev: xenbus device
  265. * @err: error to report
  266. * @fmt: error message format
  267. *
  268. * Report the given negative errno into the store, along with the given
  269. * formatted message.
  270. */
  271. void xenbus_dev_error(struct xenbus_device *dev, int err, const char *fmt, ...)
  272. {
  273. va_list ap;
  274. va_start(ap, fmt);
  275. xenbus_va_dev_error(dev, err, fmt, ap);
  276. va_end(ap);
  277. }
  278. EXPORT_SYMBOL_GPL(xenbus_dev_error);
  279. /**
  280. * xenbus_dev_fatal
  281. * @dev: xenbus device
  282. * @err: error to report
  283. * @fmt: error message format
  284. *
  285. * Equivalent to xenbus_dev_error(dev, err, fmt, args), followed by
  286. * xenbus_switch_state(dev, XenbusStateClosing) to schedule an orderly
  287. * closedown of this driver and its peer.
  288. */
  289. void xenbus_dev_fatal(struct xenbus_device *dev, int err, const char *fmt, ...)
  290. {
  291. va_list ap;
  292. va_start(ap, fmt);
  293. xenbus_va_dev_error(dev, err, fmt, ap);
  294. va_end(ap);
  295. xenbus_switch_state(dev, XenbusStateClosing);
  296. }
  297. EXPORT_SYMBOL_GPL(xenbus_dev_fatal);
  298. /**
  299. * Equivalent to xenbus_dev_fatal(dev, err, fmt, args), but helps
  300. * avoiding recursion within xenbus_switch_state.
  301. */
  302. static void xenbus_switch_fatal(struct xenbus_device *dev, int depth, int err,
  303. const char *fmt, ...)
  304. {
  305. va_list ap;
  306. va_start(ap, fmt);
  307. xenbus_va_dev_error(dev, err, fmt, ap);
  308. va_end(ap);
  309. if (!depth)
  310. __xenbus_switch_state(dev, XenbusStateClosing, 1);
  311. }
  312. /*
  313. * xenbus_setup_ring
  314. * @dev: xenbus device
  315. * @vaddr: pointer to starting virtual address of the ring
  316. * @nr_pages: number of pages to be granted
  317. * @grefs: grant reference array to be filled in
  318. *
  319. * Allocate physically contiguous pages for a shared ring buffer and grant it
  320. * to the peer of the given device. The ring buffer is initially filled with
  321. * zeroes. The virtual address of the ring is stored at @vaddr and the
  322. * grant references are stored in the @grefs array. In case of error @vaddr
  323. * will be set to NULL and @grefs will be filled with INVALID_GRANT_REF.
  324. */
  325. int xenbus_setup_ring(struct xenbus_device *dev, gfp_t gfp, void **vaddr,
  326. unsigned int nr_pages, grant_ref_t *grefs)
  327. {
  328. unsigned long ring_size = nr_pages * XEN_PAGE_SIZE;
  329. grant_ref_t gref_head;
  330. unsigned int i;
  331. void *addr;
  332. int ret;
  333. addr = *vaddr = alloc_pages_exact(ring_size, gfp | __GFP_ZERO);
  334. if (!*vaddr) {
  335. ret = -ENOMEM;
  336. goto err;
  337. }
  338. ret = gnttab_alloc_grant_references(nr_pages, &gref_head);
  339. if (ret) {
  340. xenbus_dev_fatal(dev, ret, "granting access to %u ring pages",
  341. nr_pages);
  342. goto err;
  343. }
  344. for (i = 0; i < nr_pages; i++) {
  345. unsigned long gfn;
  346. if (is_vmalloc_addr(*vaddr))
  347. gfn = pfn_to_gfn(vmalloc_to_pfn(addr));
  348. else
  349. gfn = virt_to_gfn(addr);
  350. grefs[i] = gnttab_claim_grant_reference(&gref_head);
  351. gnttab_grant_foreign_access_ref(grefs[i], dev->otherend_id,
  352. gfn, 0);
  353. addr += XEN_PAGE_SIZE;
  354. }
  355. return 0;
  356. err:
  357. if (*vaddr)
  358. free_pages_exact(*vaddr, ring_size);
  359. for (i = 0; i < nr_pages; i++)
  360. grefs[i] = INVALID_GRANT_REF;
  361. *vaddr = NULL;
  362. return ret;
  363. }
  364. EXPORT_SYMBOL_GPL(xenbus_setup_ring);
  365. /*
  366. * xenbus_teardown_ring
  367. * @vaddr: starting virtual address of the ring
  368. * @nr_pages: number of pages
  369. * @grefs: grant reference array
  370. *
  371. * Remove grants for the shared ring buffer and free the associated memory.
  372. * On return the grant reference array is filled with INVALID_GRANT_REF.
  373. */
  374. void xenbus_teardown_ring(void **vaddr, unsigned int nr_pages,
  375. grant_ref_t *grefs)
  376. {
  377. unsigned int i;
  378. for (i = 0; i < nr_pages; i++) {
  379. if (grefs[i] != INVALID_GRANT_REF) {
  380. gnttab_end_foreign_access(grefs[i], NULL);
  381. grefs[i] = INVALID_GRANT_REF;
  382. }
  383. }
  384. if (*vaddr)
  385. free_pages_exact(*vaddr, nr_pages * XEN_PAGE_SIZE);
  386. *vaddr = NULL;
  387. }
  388. EXPORT_SYMBOL_GPL(xenbus_teardown_ring);
  389. /**
  390. * Allocate an event channel for the given xenbus_device, assigning the newly
  391. * created local port to *port. Return 0 on success, or -errno on error. On
  392. * error, the device will switch to XenbusStateClosing, and the error will be
  393. * saved in the store.
  394. */
  395. int xenbus_alloc_evtchn(struct xenbus_device *dev, evtchn_port_t *port)
  396. {
  397. struct evtchn_alloc_unbound alloc_unbound;
  398. int err;
  399. alloc_unbound.dom = DOMID_SELF;
  400. alloc_unbound.remote_dom = dev->otherend_id;
  401. err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
  402. &alloc_unbound);
  403. if (err)
  404. xenbus_dev_fatal(dev, err, "allocating event channel");
  405. else
  406. *port = alloc_unbound.port;
  407. return err;
  408. }
  409. EXPORT_SYMBOL_GPL(xenbus_alloc_evtchn);
  410. /**
  411. * Free an existing event channel. Returns 0 on success or -errno on error.
  412. */
  413. int xenbus_free_evtchn(struct xenbus_device *dev, evtchn_port_t port)
  414. {
  415. struct evtchn_close close;
  416. int err;
  417. close.port = port;
  418. err = HYPERVISOR_event_channel_op(EVTCHNOP_close, &close);
  419. if (err)
  420. xenbus_dev_error(dev, err, "freeing event channel %u", port);
  421. return err;
  422. }
  423. EXPORT_SYMBOL_GPL(xenbus_free_evtchn);
  424. /**
  425. * xenbus_map_ring_valloc
  426. * @dev: xenbus device
  427. * @gnt_refs: grant reference array
  428. * @nr_grefs: number of grant references
  429. * @vaddr: pointer to address to be filled out by mapping
  430. *
  431. * Map @nr_grefs pages of memory into this domain from another
  432. * domain's grant table. xenbus_map_ring_valloc allocates @nr_grefs
  433. * pages of virtual address space, maps the pages to that address, and
  434. * sets *vaddr to that address. Returns 0 on success, and -errno on
  435. * error. If an error is returned, device will switch to
  436. * XenbusStateClosing and the error message will be saved in XenStore.
  437. */
  438. int xenbus_map_ring_valloc(struct xenbus_device *dev, grant_ref_t *gnt_refs,
  439. unsigned int nr_grefs, void **vaddr)
  440. {
  441. int err;
  442. struct map_ring_valloc *info;
  443. *vaddr = NULL;
  444. if (nr_grefs > XENBUS_MAX_RING_GRANTS)
  445. return -EINVAL;
  446. info = kzalloc(sizeof(*info), GFP_KERNEL);
  447. if (!info)
  448. return -ENOMEM;
  449. info->node = kzalloc(sizeof(*info->node), GFP_KERNEL);
  450. if (!info->node)
  451. err = -ENOMEM;
  452. else
  453. err = ring_ops->map(dev, info, gnt_refs, nr_grefs, vaddr);
  454. kfree(info->node);
  455. kfree(info);
  456. return err;
  457. }
  458. EXPORT_SYMBOL_GPL(xenbus_map_ring_valloc);
  459. /* N.B. sizeof(phys_addr_t) doesn't always equal to sizeof(unsigned
  460. * long), e.g. 32-on-64. Caller is responsible for preparing the
  461. * right array to feed into this function */
  462. static int __xenbus_map_ring(struct xenbus_device *dev,
  463. grant_ref_t *gnt_refs,
  464. unsigned int nr_grefs,
  465. grant_handle_t *handles,
  466. struct map_ring_valloc *info,
  467. unsigned int flags,
  468. bool *leaked)
  469. {
  470. int i, j;
  471. if (nr_grefs > XENBUS_MAX_RING_GRANTS)
  472. return -EINVAL;
  473. for (i = 0; i < nr_grefs; i++) {
  474. gnttab_set_map_op(&info->map[i], info->phys_addrs[i], flags,
  475. gnt_refs[i], dev->otherend_id);
  476. handles[i] = INVALID_GRANT_HANDLE;
  477. }
  478. gnttab_batch_map(info->map, i);
  479. for (i = 0; i < nr_grefs; i++) {
  480. if (info->map[i].status != GNTST_okay) {
  481. xenbus_dev_fatal(dev, info->map[i].status,
  482. "mapping in shared page %d from domain %d",
  483. gnt_refs[i], dev->otherend_id);
  484. goto fail;
  485. } else
  486. handles[i] = info->map[i].handle;
  487. }
  488. return 0;
  489. fail:
  490. for (i = j = 0; i < nr_grefs; i++) {
  491. if (handles[i] != INVALID_GRANT_HANDLE) {
  492. gnttab_set_unmap_op(&info->unmap[j],
  493. info->phys_addrs[i],
  494. GNTMAP_host_map, handles[i]);
  495. j++;
  496. }
  497. }
  498. BUG_ON(HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, info->unmap, j));
  499. *leaked = false;
  500. for (i = 0; i < j; i++) {
  501. if (info->unmap[i].status != GNTST_okay) {
  502. *leaked = true;
  503. break;
  504. }
  505. }
  506. return -ENOENT;
  507. }
  508. /**
  509. * xenbus_unmap_ring
  510. * @dev: xenbus device
  511. * @handles: grant handle array
  512. * @nr_handles: number of handles in the array
  513. * @vaddrs: addresses to unmap
  514. *
  515. * Unmap memory in this domain that was imported from another domain.
  516. * Returns 0 on success and returns GNTST_* on error
  517. * (see xen/include/interface/grant_table.h).
  518. */
  519. static int xenbus_unmap_ring(struct xenbus_device *dev, grant_handle_t *handles,
  520. unsigned int nr_handles, unsigned long *vaddrs)
  521. {
  522. struct gnttab_unmap_grant_ref unmap[XENBUS_MAX_RING_GRANTS];
  523. int i;
  524. int err;
  525. if (nr_handles > XENBUS_MAX_RING_GRANTS)
  526. return -EINVAL;
  527. for (i = 0; i < nr_handles; i++)
  528. gnttab_set_unmap_op(&unmap[i], vaddrs[i],
  529. GNTMAP_host_map, handles[i]);
  530. BUG_ON(HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, unmap, i));
  531. err = GNTST_okay;
  532. for (i = 0; i < nr_handles; i++) {
  533. if (unmap[i].status != GNTST_okay) {
  534. xenbus_dev_error(dev, unmap[i].status,
  535. "unmapping page at handle %d error %d",
  536. handles[i], unmap[i].status);
  537. err = unmap[i].status;
  538. break;
  539. }
  540. }
  541. return err;
  542. }
  543. static void xenbus_map_ring_setup_grant_hvm(unsigned long gfn,
  544. unsigned int goffset,
  545. unsigned int len,
  546. void *data)
  547. {
  548. struct map_ring_valloc *info = data;
  549. unsigned long vaddr = (unsigned long)gfn_to_virt(gfn);
  550. info->phys_addrs[info->idx] = vaddr;
  551. info->addrs[info->idx] = vaddr;
  552. info->idx++;
  553. }
  554. static int xenbus_map_ring_hvm(struct xenbus_device *dev,
  555. struct map_ring_valloc *info,
  556. grant_ref_t *gnt_ref,
  557. unsigned int nr_grefs,
  558. void **vaddr)
  559. {
  560. struct xenbus_map_node *node = info->node;
  561. int err;
  562. void *addr;
  563. bool leaked = false;
  564. unsigned int nr_pages = XENBUS_PAGES(nr_grefs);
  565. err = xen_alloc_unpopulated_pages(nr_pages, node->hvm.pages);
  566. if (err)
  567. goto out_err;
  568. gnttab_foreach_grant(node->hvm.pages, nr_grefs,
  569. xenbus_map_ring_setup_grant_hvm,
  570. info);
  571. err = __xenbus_map_ring(dev, gnt_ref, nr_grefs, node->handles,
  572. info, GNTMAP_host_map, &leaked);
  573. node->nr_handles = nr_grefs;
  574. if (err)
  575. goto out_free_ballooned_pages;
  576. addr = vmap(node->hvm.pages, nr_pages, VM_MAP | VM_IOREMAP,
  577. PAGE_KERNEL);
  578. if (!addr) {
  579. err = -ENOMEM;
  580. goto out_xenbus_unmap_ring;
  581. }
  582. node->hvm.addr = addr;
  583. spin_lock(&xenbus_valloc_lock);
  584. list_add(&node->next, &xenbus_valloc_pages);
  585. spin_unlock(&xenbus_valloc_lock);
  586. *vaddr = addr;
  587. info->node = NULL;
  588. return 0;
  589. out_xenbus_unmap_ring:
  590. if (!leaked)
  591. xenbus_unmap_ring(dev, node->handles, nr_grefs, info->addrs);
  592. else
  593. pr_alert("leaking %p size %u page(s)",
  594. addr, nr_pages);
  595. out_free_ballooned_pages:
  596. if (!leaked)
  597. xen_free_unpopulated_pages(nr_pages, node->hvm.pages);
  598. out_err:
  599. return err;
  600. }
  601. /**
  602. * xenbus_unmap_ring_vfree
  603. * @dev: xenbus device
  604. * @vaddr: addr to unmap
  605. *
  606. * Based on Rusty Russell's skeleton driver's unmap_page.
  607. * Unmap a page of memory in this domain that was imported from another domain.
  608. * Use xenbus_unmap_ring_vfree if you mapped in your memory with
  609. * xenbus_map_ring_valloc (it will free the virtual address space).
  610. * Returns 0 on success and returns GNTST_* on error
  611. * (see xen/include/interface/grant_table.h).
  612. */
  613. int xenbus_unmap_ring_vfree(struct xenbus_device *dev, void *vaddr)
  614. {
  615. return ring_ops->unmap(dev, vaddr);
  616. }
  617. EXPORT_SYMBOL_GPL(xenbus_unmap_ring_vfree);
  618. #ifdef CONFIG_XEN_PV
  619. static int map_ring_apply(pte_t *pte, unsigned long addr, void *data)
  620. {
  621. struct map_ring_valloc *info = data;
  622. info->phys_addrs[info->idx++] = arbitrary_virt_to_machine(pte).maddr;
  623. return 0;
  624. }
  625. static int xenbus_map_ring_pv(struct xenbus_device *dev,
  626. struct map_ring_valloc *info,
  627. grant_ref_t *gnt_refs,
  628. unsigned int nr_grefs,
  629. void **vaddr)
  630. {
  631. struct xenbus_map_node *node = info->node;
  632. struct vm_struct *area;
  633. bool leaked = false;
  634. int err = -ENOMEM;
  635. area = get_vm_area(XEN_PAGE_SIZE * nr_grefs, VM_IOREMAP);
  636. if (!area)
  637. return -ENOMEM;
  638. if (apply_to_page_range(&init_mm, (unsigned long)area->addr,
  639. XEN_PAGE_SIZE * nr_grefs, map_ring_apply, info))
  640. goto failed;
  641. err = __xenbus_map_ring(dev, gnt_refs, nr_grefs, node->handles,
  642. info, GNTMAP_host_map | GNTMAP_contains_pte,
  643. &leaked);
  644. if (err)
  645. goto failed;
  646. node->nr_handles = nr_grefs;
  647. node->pv.area = area;
  648. spin_lock(&xenbus_valloc_lock);
  649. list_add(&node->next, &xenbus_valloc_pages);
  650. spin_unlock(&xenbus_valloc_lock);
  651. *vaddr = area->addr;
  652. info->node = NULL;
  653. return 0;
  654. failed:
  655. if (!leaked)
  656. free_vm_area(area);
  657. else
  658. pr_alert("leaking VM area %p size %u page(s)", area, nr_grefs);
  659. return err;
  660. }
  661. static int xenbus_unmap_ring_pv(struct xenbus_device *dev, void *vaddr)
  662. {
  663. struct xenbus_map_node *node;
  664. struct gnttab_unmap_grant_ref unmap[XENBUS_MAX_RING_GRANTS];
  665. unsigned int level;
  666. int i;
  667. bool leaked = false;
  668. int err;
  669. spin_lock(&xenbus_valloc_lock);
  670. list_for_each_entry(node, &xenbus_valloc_pages, next) {
  671. if (node->pv.area->addr == vaddr) {
  672. list_del(&node->next);
  673. goto found;
  674. }
  675. }
  676. node = NULL;
  677. found:
  678. spin_unlock(&xenbus_valloc_lock);
  679. if (!node) {
  680. xenbus_dev_error(dev, -ENOENT,
  681. "can't find mapped virtual address %p", vaddr);
  682. return GNTST_bad_virt_addr;
  683. }
  684. for (i = 0; i < node->nr_handles; i++) {
  685. unsigned long addr;
  686. memset(&unmap[i], 0, sizeof(unmap[i]));
  687. addr = (unsigned long)vaddr + (XEN_PAGE_SIZE * i);
  688. unmap[i].host_addr = arbitrary_virt_to_machine(
  689. lookup_address(addr, &level)).maddr;
  690. unmap[i].dev_bus_addr = 0;
  691. unmap[i].handle = node->handles[i];
  692. }
  693. BUG_ON(HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, unmap, i));
  694. err = GNTST_okay;
  695. leaked = false;
  696. for (i = 0; i < node->nr_handles; i++) {
  697. if (unmap[i].status != GNTST_okay) {
  698. leaked = true;
  699. xenbus_dev_error(dev, unmap[i].status,
  700. "unmapping page at handle %d error %d",
  701. node->handles[i], unmap[i].status);
  702. err = unmap[i].status;
  703. break;
  704. }
  705. }
  706. if (!leaked)
  707. free_vm_area(node->pv.area);
  708. else
  709. pr_alert("leaking VM area %p size %u page(s)",
  710. node->pv.area, node->nr_handles);
  711. kfree(node);
  712. return err;
  713. }
  714. static const struct xenbus_ring_ops ring_ops_pv = {
  715. .map = xenbus_map_ring_pv,
  716. .unmap = xenbus_unmap_ring_pv,
  717. };
  718. #endif
  719. struct unmap_ring_hvm
  720. {
  721. unsigned int idx;
  722. unsigned long addrs[XENBUS_MAX_RING_GRANTS];
  723. };
  724. static void xenbus_unmap_ring_setup_grant_hvm(unsigned long gfn,
  725. unsigned int goffset,
  726. unsigned int len,
  727. void *data)
  728. {
  729. struct unmap_ring_hvm *info = data;
  730. info->addrs[info->idx] = (unsigned long)gfn_to_virt(gfn);
  731. info->idx++;
  732. }
  733. static int xenbus_unmap_ring_hvm(struct xenbus_device *dev, void *vaddr)
  734. {
  735. int rv;
  736. struct xenbus_map_node *node;
  737. void *addr;
  738. struct unmap_ring_hvm info = {
  739. .idx = 0,
  740. };
  741. unsigned int nr_pages;
  742. spin_lock(&xenbus_valloc_lock);
  743. list_for_each_entry(node, &xenbus_valloc_pages, next) {
  744. addr = node->hvm.addr;
  745. if (addr == vaddr) {
  746. list_del(&node->next);
  747. goto found;
  748. }
  749. }
  750. node = addr = NULL;
  751. found:
  752. spin_unlock(&xenbus_valloc_lock);
  753. if (!node) {
  754. xenbus_dev_error(dev, -ENOENT,
  755. "can't find mapped virtual address %p", vaddr);
  756. return GNTST_bad_virt_addr;
  757. }
  758. nr_pages = XENBUS_PAGES(node->nr_handles);
  759. gnttab_foreach_grant(node->hvm.pages, node->nr_handles,
  760. xenbus_unmap_ring_setup_grant_hvm,
  761. &info);
  762. rv = xenbus_unmap_ring(dev, node->handles, node->nr_handles,
  763. info.addrs);
  764. if (!rv) {
  765. vunmap(vaddr);
  766. xen_free_unpopulated_pages(nr_pages, node->hvm.pages);
  767. }
  768. else
  769. WARN(1, "Leaking %p, size %u page(s)\n", vaddr, nr_pages);
  770. kfree(node);
  771. return rv;
  772. }
  773. /**
  774. * xenbus_read_driver_state
  775. * @path: path for driver
  776. *
  777. * Return the state of the driver rooted at the given store path, or
  778. * XenbusStateUnknown if no state can be read.
  779. */
  780. enum xenbus_state xenbus_read_driver_state(const char *path)
  781. {
  782. enum xenbus_state result;
  783. int err = xenbus_gather(XBT_NIL, path, "state", "%d", &result, NULL);
  784. if (err)
  785. result = XenbusStateUnknown;
  786. return result;
  787. }
  788. EXPORT_SYMBOL_GPL(xenbus_read_driver_state);
  789. static const struct xenbus_ring_ops ring_ops_hvm = {
  790. .map = xenbus_map_ring_hvm,
  791. .unmap = xenbus_unmap_ring_hvm,
  792. };
  793. void __init xenbus_ring_ops_init(void)
  794. {
  795. #ifdef CONFIG_XEN_PV
  796. if (!xen_feature(XENFEAT_auto_translated_physmap))
  797. ring_ops = &ring_ops_pv;
  798. else
  799. #endif
  800. ring_ops = &ring_ops_hvm;
  801. }