xencomm.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. *
  16. * Copyright (C) IBM Corp. 2006
  17. *
  18. * Authors: Hollis Blanchard <hollisb@us.ibm.com>
  19. */
  20. #include <linux/mm.h>
  21. #include <linux/slab.h>
  22. #include <asm/page.h>
  23. #include <xen/xencomm.h>
  24. #include <xen/interface/xen.h>
  25. #include <asm/xen/xencomm.h> /* for xencomm_is_phys_contiguous() */
  26. static int xencomm_init(struct xencomm_desc *desc,
  27. void *buffer, unsigned long bytes)
  28. {
  29. unsigned long recorded = 0;
  30. int i = 0;
  31. while ((recorded < bytes) && (i < desc->nr_addrs)) {
  32. unsigned long vaddr = (unsigned long)buffer + recorded;
  33. unsigned long paddr;
  34. int offset;
  35. int chunksz;
  36. offset = vaddr % PAGE_SIZE; /* handle partial pages */
  37. chunksz = min(PAGE_SIZE - offset, bytes - recorded);
  38. paddr = xencomm_vtop(vaddr);
  39. if (paddr == ~0UL) {
  40. printk(KERN_DEBUG "%s: couldn't translate vaddr %lx\n",
  41. __func__, vaddr);
  42. return -EINVAL;
  43. }
  44. desc->address[i++] = paddr;
  45. recorded += chunksz;
  46. }
  47. if (recorded < bytes) {
  48. printk(KERN_DEBUG
  49. "%s: could only translate %ld of %ld bytes\n",
  50. __func__, recorded, bytes);
  51. return -ENOSPC;
  52. }
  53. /* mark remaining addresses invalid (just for safety) */
  54. while (i < desc->nr_addrs)
  55. desc->address[i++] = XENCOMM_INVALID;
  56. desc->magic = XENCOMM_MAGIC;
  57. return 0;
  58. }
  59. static struct xencomm_desc *xencomm_alloc(gfp_t gfp_mask,
  60. void *buffer, unsigned long bytes)
  61. {
  62. struct xencomm_desc *desc;
  63. unsigned long buffer_ulong = (unsigned long)buffer;
  64. unsigned long start = buffer_ulong & PAGE_MASK;
  65. unsigned long end = (buffer_ulong + bytes) | ~PAGE_MASK;
  66. unsigned long nr_addrs = (end - start + 1) >> PAGE_SHIFT;
  67. unsigned long size = sizeof(*desc) +
  68. sizeof(desc->address[0]) * nr_addrs;
  69. /*
  70. * slab allocator returns at least sizeof(void*) aligned pointer.
  71. * When sizeof(*desc) > sizeof(void*), struct xencomm_desc might
  72. * cross page boundary.
  73. */
  74. if (sizeof(*desc) > sizeof(void *)) {
  75. unsigned long order = get_order(size);
  76. desc = (struct xencomm_desc *)__get_free_pages(gfp_mask,
  77. order);
  78. if (desc == NULL)
  79. return NULL;
  80. desc->nr_addrs =
  81. ((PAGE_SIZE << order) - sizeof(struct xencomm_desc)) /
  82. sizeof(*desc->address);
  83. } else {
  84. desc = kmalloc(size, gfp_mask);
  85. if (desc == NULL)
  86. return NULL;
  87. desc->nr_addrs = nr_addrs;
  88. }
  89. return desc;
  90. }
  91. void xencomm_free(struct xencomm_handle *desc)
  92. {
  93. if (desc && !((ulong)desc & XENCOMM_INLINE_FLAG)) {
  94. struct xencomm_desc *desc__ = (struct xencomm_desc *)desc;
  95. if (sizeof(*desc__) > sizeof(void *)) {
  96. unsigned long size = sizeof(*desc__) +
  97. sizeof(desc__->address[0]) * desc__->nr_addrs;
  98. unsigned long order = get_order(size);
  99. free_pages((unsigned long)__va(desc), order);
  100. } else
  101. kfree(__va(desc));
  102. }
  103. }
  104. static int xencomm_create(void *buffer, unsigned long bytes,
  105. struct xencomm_desc **ret, gfp_t gfp_mask)
  106. {
  107. struct xencomm_desc *desc;
  108. int rc;
  109. pr_debug("%s: %p[%ld]\n", __func__, buffer, bytes);
  110. if (bytes == 0) {
  111. /* don't create a descriptor; Xen recognizes NULL. */
  112. BUG_ON(buffer != NULL);
  113. *ret = NULL;
  114. return 0;
  115. }
  116. BUG_ON(buffer == NULL); /* 'bytes' is non-zero */
  117. desc = xencomm_alloc(gfp_mask, buffer, bytes);
  118. if (!desc) {
  119. printk(KERN_DEBUG "%s failure\n", "xencomm_alloc");
  120. return -ENOMEM;
  121. }
  122. rc = xencomm_init(desc, buffer, bytes);
  123. if (rc) {
  124. printk(KERN_DEBUG "%s failure: %d\n", "xencomm_init", rc);
  125. xencomm_free((struct xencomm_handle *)__pa(desc));
  126. return rc;
  127. }
  128. *ret = desc;
  129. return 0;
  130. }
  131. static struct xencomm_handle *xencomm_create_inline(void *ptr)
  132. {
  133. unsigned long paddr;
  134. BUG_ON(!xencomm_is_phys_contiguous((unsigned long)ptr));
  135. paddr = (unsigned long)xencomm_pa(ptr);
  136. BUG_ON(paddr & XENCOMM_INLINE_FLAG);
  137. return (struct xencomm_handle *)(paddr | XENCOMM_INLINE_FLAG);
  138. }
  139. /* "mini" routine, for stack-based communications: */
  140. static int xencomm_create_mini(void *buffer,
  141. unsigned long bytes, struct xencomm_mini *xc_desc,
  142. struct xencomm_desc **ret)
  143. {
  144. int rc = 0;
  145. struct xencomm_desc *desc;
  146. BUG_ON(((unsigned long)xc_desc) % sizeof(*xc_desc) != 0);
  147. desc = (void *)xc_desc;
  148. desc->nr_addrs = XENCOMM_MINI_ADDRS;
  149. rc = xencomm_init(desc, buffer, bytes);
  150. if (!rc)
  151. *ret = desc;
  152. return rc;
  153. }
  154. struct xencomm_handle *xencomm_map(void *ptr, unsigned long bytes)
  155. {
  156. int rc;
  157. struct xencomm_desc *desc;
  158. if (xencomm_is_phys_contiguous((unsigned long)ptr))
  159. return xencomm_create_inline(ptr);
  160. rc = xencomm_create(ptr, bytes, &desc, GFP_KERNEL);
  161. if (rc || desc == NULL)
  162. return NULL;
  163. return xencomm_pa(desc);
  164. }
  165. struct xencomm_handle *__xencomm_map_no_alloc(void *ptr, unsigned long bytes,
  166. struct xencomm_mini *xc_desc)
  167. {
  168. int rc;
  169. struct xencomm_desc *desc = NULL;
  170. if (xencomm_is_phys_contiguous((unsigned long)ptr))
  171. return xencomm_create_inline(ptr);
  172. rc = xencomm_create_mini(ptr, bytes, xc_desc,
  173. &desc);
  174. if (rc)
  175. return NULL;
  176. return xencomm_pa(desc);
  177. }