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