xenbus_client.c 24 KB

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