grant-table.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /******************************************************************************
  2. * arch/ia64/xen/grant-table.c
  3. *
  4. * Copyright (c) 2006 Isaku Yamahata <yamahata at valinux co jp>
  5. * VA Linux Systems Japan K.K.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. #include <linux/module.h>
  23. #include <linux/vmalloc.h>
  24. #include <linux/slab.h>
  25. #include <linux/mm.h>
  26. #include <xen/interface/xen.h>
  27. #include <xen/interface/memory.h>
  28. #include <xen/grant_table.h>
  29. #include <asm/xen/hypervisor.h>
  30. struct vm_struct *xen_alloc_vm_area(unsigned long size)
  31. {
  32. int order;
  33. unsigned long virt;
  34. unsigned long nr_pages;
  35. struct vm_struct *area;
  36. order = get_order(size);
  37. virt = __get_free_pages(GFP_KERNEL, order);
  38. if (virt == 0)
  39. goto err0;
  40. nr_pages = 1 << order;
  41. scrub_pages(virt, nr_pages);
  42. area = kmalloc(sizeof(*area), GFP_KERNEL);
  43. if (area == NULL)
  44. goto err1;
  45. area->flags = VM_IOREMAP;
  46. area->addr = (void *)virt;
  47. area->size = size;
  48. area->pages = NULL;
  49. area->nr_pages = nr_pages;
  50. area->phys_addr = 0; /* xenbus_map_ring_valloc uses this field! */
  51. return area;
  52. err1:
  53. free_pages(virt, order);
  54. err0:
  55. return NULL;
  56. }
  57. EXPORT_SYMBOL_GPL(xen_alloc_vm_area);
  58. void xen_free_vm_area(struct vm_struct *area)
  59. {
  60. unsigned int order = get_order(area->size);
  61. unsigned long i;
  62. unsigned long phys_addr = __pa(area->addr);
  63. /* This area is used for foreign page mappping.
  64. * So underlying machine page may not be assigned. */
  65. for (i = 0; i < (1 << order); i++) {
  66. unsigned long ret;
  67. unsigned long gpfn = (phys_addr >> PAGE_SHIFT) + i;
  68. struct xen_memory_reservation reservation = {
  69. .nr_extents = 1,
  70. .address_bits = 0,
  71. .extent_order = 0,
  72. .domid = DOMID_SELF
  73. };
  74. set_xen_guest_handle(reservation.extent_start, &gpfn);
  75. ret = HYPERVISOR_memory_op(XENMEM_populate_physmap,
  76. &reservation);
  77. BUG_ON(ret != 1);
  78. }
  79. free_pages((unsigned long)area->addr, order);
  80. kfree(area);
  81. }
  82. EXPORT_SYMBOL_GPL(xen_free_vm_area);
  83. /****************************************************************************
  84. * grant table hack
  85. * cmd: GNTTABOP_xxx
  86. */
  87. int arch_gnttab_map_shared(unsigned long *frames, unsigned long nr_gframes,
  88. unsigned long max_nr_gframes,
  89. struct grant_entry **__shared)
  90. {
  91. *__shared = __va(frames[0] << PAGE_SHIFT);
  92. return 0;
  93. }
  94. void arch_gnttab_unmap_shared(struct grant_entry *shared,
  95. unsigned long nr_gframes)
  96. {
  97. /* nothing */
  98. }
  99. static void
  100. gnttab_map_grant_ref_pre(struct gnttab_map_grant_ref *uop)
  101. {
  102. uint32_t flags;
  103. flags = uop->flags;
  104. if (flags & GNTMAP_host_map) {
  105. if (flags & GNTMAP_application_map) {
  106. printk(KERN_DEBUG
  107. "GNTMAP_application_map is not supported yet: "
  108. "flags 0x%x\n", flags);
  109. BUG();
  110. }
  111. if (flags & GNTMAP_contains_pte) {
  112. printk(KERN_DEBUG
  113. "GNTMAP_contains_pte is not supported yet: "
  114. "flags 0x%x\n", flags);
  115. BUG();
  116. }
  117. } else if (flags & GNTMAP_device_map) {
  118. printk("GNTMAP_device_map is not supported yet 0x%x\n", flags);
  119. BUG(); /* not yet. actually this flag is not used. */
  120. } else {
  121. BUG();
  122. }
  123. }
  124. int
  125. HYPERVISOR_grant_table_op(unsigned int cmd, void *uop, unsigned int count)
  126. {
  127. if (cmd == GNTTABOP_map_grant_ref) {
  128. unsigned int i;
  129. for (i = 0; i < count; i++) {
  130. gnttab_map_grant_ref_pre(
  131. (struct gnttab_map_grant_ref *)uop + i);
  132. }
  133. }
  134. return xencomm_hypercall_grant_table_op(cmd, uop, count);
  135. }
  136. EXPORT_SYMBOL(HYPERVISOR_grant_table_op);