xencomm.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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/gfp.h>
  21. #include <linux/mm.h>
  22. #include <asm/page.h>
  23. #include <xen/xencomm.h>
  24. #include <xen/interface/xen.h>
  25. #ifdef __ia64__
  26. #include <asm/xen/xencomm.h> /* for is_kern_addr() */
  27. #endif
  28. #ifdef HAVE_XEN_PLATFORM_COMPAT_H
  29. #include <xen/platform-compat.h>
  30. #endif
  31. static int xencomm_init(struct xencomm_desc *desc,
  32. void *buffer, unsigned long bytes)
  33. {
  34. unsigned long recorded = 0;
  35. int i = 0;
  36. while ((recorded < bytes) && (i < desc->nr_addrs)) {
  37. unsigned long vaddr = (unsigned long)buffer + recorded;
  38. unsigned long paddr;
  39. int offset;
  40. int chunksz;
  41. offset = vaddr % PAGE_SIZE; /* handle partial pages */
  42. chunksz = min(PAGE_SIZE - offset, bytes - recorded);
  43. paddr = xencomm_vtop(vaddr);
  44. if (paddr == ~0UL) {
  45. printk(KERN_DEBUG "%s: couldn't translate vaddr %lx\n",
  46. __func__, vaddr);
  47. return -EINVAL;
  48. }
  49. desc->address[i++] = paddr;
  50. recorded += chunksz;
  51. }
  52. if (recorded < bytes) {
  53. printk(KERN_DEBUG
  54. "%s: could only translate %ld of %ld bytes\n",
  55. __func__, recorded, bytes);
  56. return -ENOSPC;
  57. }
  58. /* mark remaining addresses invalid (just for safety) */
  59. while (i < desc->nr_addrs)
  60. desc->address[i++] = XENCOMM_INVALID;
  61. desc->magic = XENCOMM_MAGIC;
  62. return 0;
  63. }
  64. static struct xencomm_desc *xencomm_alloc(gfp_t gfp_mask,
  65. void *buffer, unsigned long bytes)
  66. {
  67. struct xencomm_desc *desc;
  68. unsigned long buffer_ulong = (unsigned long)buffer;
  69. unsigned long start = buffer_ulong & PAGE_MASK;
  70. unsigned long end = (buffer_ulong + bytes) | ~PAGE_MASK;
  71. unsigned long nr_addrs = (end - start + 1) >> PAGE_SHIFT;
  72. unsigned long size = sizeof(*desc) +
  73. sizeof(desc->address[0]) * nr_addrs;
  74. /*
  75. * slab allocator returns at least sizeof(void*) aligned pointer.
  76. * When sizeof(*desc) > sizeof(void*), struct xencomm_desc might
  77. * cross page boundary.
  78. */
  79. if (sizeof(*desc) > sizeof(void *)) {
  80. unsigned long order = get_order(size);
  81. desc = (struct xencomm_desc *)__get_free_pages(gfp_mask,
  82. order);
  83. if (desc == NULL)
  84. return NULL;
  85. desc->nr_addrs =
  86. ((PAGE_SIZE << order) - sizeof(struct xencomm_desc)) /
  87. sizeof(*desc->address);
  88. } else {
  89. desc = kmalloc(size, gfp_mask);
  90. if (desc == NULL)
  91. return NULL;
  92. desc->nr_addrs = nr_addrs;
  93. }
  94. return desc;
  95. }
  96. void xencomm_free(struct xencomm_handle *desc)
  97. {
  98. if (desc && !((ulong)desc & XENCOMM_INLINE_FLAG)) {
  99. struct xencomm_desc *desc__ = (struct xencomm_desc *)desc;
  100. if (sizeof(*desc__) > sizeof(void *)) {
  101. unsigned long size = sizeof(*desc__) +
  102. sizeof(desc__->address[0]) * desc__->nr_addrs;
  103. unsigned long order = get_order(size);
  104. free_pages((unsigned long)__va(desc), order);
  105. } else
  106. kfree(__va(desc));
  107. }
  108. }
  109. static int xencomm_create(void *buffer, unsigned long bytes,
  110. struct xencomm_desc **ret, gfp_t gfp_mask)
  111. {
  112. struct xencomm_desc *desc;
  113. int rc;
  114. pr_debug("%s: %p[%ld]\n", __func__, buffer, bytes);
  115. if (bytes == 0) {
  116. /* don't create a descriptor; Xen recognizes NULL. */
  117. BUG_ON(buffer != NULL);
  118. *ret = NULL;
  119. return 0;
  120. }
  121. BUG_ON(buffer == NULL); /* 'bytes' is non-zero */
  122. desc = xencomm_alloc(gfp_mask, buffer, bytes);
  123. if (!desc) {
  124. printk(KERN_DEBUG "%s failure\n", "xencomm_alloc");
  125. return -ENOMEM;
  126. }
  127. rc = xencomm_init(desc, buffer, bytes);
  128. if (rc) {
  129. printk(KERN_DEBUG "%s failure: %d\n", "xencomm_init", rc);
  130. xencomm_free((struct xencomm_handle *)__pa(desc));
  131. return rc;
  132. }
  133. *ret = desc;
  134. return 0;
  135. }
  136. /* check if memory address is within VMALLOC region */
  137. static int is_phys_contiguous(unsigned long addr)
  138. {
  139. if (!is_kernel_addr(addr))
  140. return 0;
  141. return (addr < VMALLOC_START) || (addr >= VMALLOC_END);
  142. }
  143. static struct xencomm_handle *xencomm_create_inline(void *ptr)
  144. {
  145. unsigned long paddr;
  146. BUG_ON(!is_phys_contiguous((unsigned long)ptr));
  147. paddr = (unsigned long)xencomm_pa(ptr);
  148. BUG_ON(paddr & XENCOMM_INLINE_FLAG);
  149. return (struct xencomm_handle *)(paddr | XENCOMM_INLINE_FLAG);
  150. }
  151. /* "mini" routine, for stack-based communications: */
  152. static int xencomm_create_mini(void *buffer,
  153. unsigned long bytes, struct xencomm_mini *xc_desc,
  154. struct xencomm_desc **ret)
  155. {
  156. int rc = 0;
  157. struct xencomm_desc *desc;
  158. BUG_ON(((unsigned long)xc_desc) % sizeof(*xc_desc) != 0);
  159. desc = (void *)xc_desc;
  160. desc->nr_addrs = XENCOMM_MINI_ADDRS;
  161. rc = xencomm_init(desc, buffer, bytes);
  162. if (!rc)
  163. *ret = desc;
  164. return rc;
  165. }
  166. struct xencomm_handle *xencomm_map(void *ptr, unsigned long bytes)
  167. {
  168. int rc;
  169. struct xencomm_desc *desc;
  170. if (is_phys_contiguous((unsigned long)ptr))
  171. return xencomm_create_inline(ptr);
  172. rc = xencomm_create(ptr, bytes, &desc, GFP_KERNEL);
  173. if (rc || desc == NULL)
  174. return NULL;
  175. return xencomm_pa(desc);
  176. }
  177. struct xencomm_handle *__xencomm_map_no_alloc(void *ptr, unsigned long bytes,
  178. struct xencomm_mini *xc_desc)
  179. {
  180. int rc;
  181. struct xencomm_desc *desc = NULL;
  182. if (is_phys_contiguous((unsigned long)ptr))
  183. return xencomm_create_inline(ptr);
  184. rc = xencomm_create_mini(ptr, bytes, xc_desc,
  185. &desc);
  186. if (rc)
  187. return NULL;
  188. return xencomm_pa(desc);
  189. }