io.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /* Simple I/O model for guests, based on shared memory.
  2. * Copyright (C) 2006 Rusty Russell IBM Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <linux/types.h>
  19. #include <linux/futex.h>
  20. #include <linux/jhash.h>
  21. #include <linux/mm.h>
  22. #include <linux/highmem.h>
  23. #include <linux/uaccess.h>
  24. #include "lg.h"
  25. static struct list_head dma_hash[61];
  26. void lguest_io_init(void)
  27. {
  28. unsigned int i;
  29. for (i = 0; i < ARRAY_SIZE(dma_hash); i++)
  30. INIT_LIST_HEAD(&dma_hash[i]);
  31. }
  32. /* FIXME: allow multi-page lengths. */
  33. static int check_dma_list(struct lguest *lg, const struct lguest_dma *dma)
  34. {
  35. unsigned int i;
  36. for (i = 0; i < LGUEST_MAX_DMA_SECTIONS; i++) {
  37. if (!dma->len[i])
  38. return 1;
  39. if (!lguest_address_ok(lg, dma->addr[i], dma->len[i]))
  40. goto kill;
  41. if (dma->len[i] > PAGE_SIZE)
  42. goto kill;
  43. /* We could do over a page, but is it worth it? */
  44. if ((dma->addr[i] % PAGE_SIZE) + dma->len[i] > PAGE_SIZE)
  45. goto kill;
  46. }
  47. return 1;
  48. kill:
  49. kill_guest(lg, "bad DMA entry: %u@%#lx", dma->len[i], dma->addr[i]);
  50. return 0;
  51. }
  52. static unsigned int hash(const union futex_key *key)
  53. {
  54. return jhash2((u32*)&key->both.word,
  55. (sizeof(key->both.word)+sizeof(key->both.ptr))/4,
  56. key->both.offset)
  57. % ARRAY_SIZE(dma_hash);
  58. }
  59. static inline int key_eq(const union futex_key *a, const union futex_key *b)
  60. {
  61. return (a->both.word == b->both.word
  62. && a->both.ptr == b->both.ptr
  63. && a->both.offset == b->both.offset);
  64. }
  65. /* Must hold read lock on dmainfo owner's current->mm->mmap_sem */
  66. static void unlink_dma(struct lguest_dma_info *dmainfo)
  67. {
  68. BUG_ON(!mutex_is_locked(&lguest_lock));
  69. dmainfo->interrupt = 0;
  70. list_del(&dmainfo->list);
  71. drop_futex_key_refs(&dmainfo->key);
  72. }
  73. static int unbind_dma(struct lguest *lg,
  74. const union futex_key *key,
  75. unsigned long dmas)
  76. {
  77. int i, ret = 0;
  78. for (i = 0; i < LGUEST_MAX_DMA; i++) {
  79. if (key_eq(key, &lg->dma[i].key) && dmas == lg->dma[i].dmas) {
  80. unlink_dma(&lg->dma[i]);
  81. ret = 1;
  82. break;
  83. }
  84. }
  85. return ret;
  86. }
  87. int bind_dma(struct lguest *lg,
  88. unsigned long ukey, unsigned long dmas, u16 numdmas, u8 interrupt)
  89. {
  90. unsigned int i;
  91. int ret = 0;
  92. union futex_key key;
  93. struct rw_semaphore *fshared = &current->mm->mmap_sem;
  94. if (interrupt >= LGUEST_IRQS)
  95. return 0;
  96. mutex_lock(&lguest_lock);
  97. down_read(fshared);
  98. if (get_futex_key((u32 __user *)ukey, fshared, &key) != 0) {
  99. kill_guest(lg, "bad dma key %#lx", ukey);
  100. goto unlock;
  101. }
  102. get_futex_key_refs(&key);
  103. if (interrupt == 0)
  104. ret = unbind_dma(lg, &key, dmas);
  105. else {
  106. for (i = 0; i < LGUEST_MAX_DMA; i++) {
  107. if (lg->dma[i].interrupt)
  108. continue;
  109. lg->dma[i].dmas = dmas;
  110. lg->dma[i].num_dmas = numdmas;
  111. lg->dma[i].next_dma = 0;
  112. lg->dma[i].key = key;
  113. lg->dma[i].guestid = lg->guestid;
  114. lg->dma[i].interrupt = interrupt;
  115. list_add(&lg->dma[i].list, &dma_hash[hash(&key)]);
  116. ret = 1;
  117. goto unlock;
  118. }
  119. }
  120. drop_futex_key_refs(&key);
  121. unlock:
  122. up_read(fshared);
  123. mutex_unlock(&lguest_lock);
  124. return ret;
  125. }
  126. /* lgread from another guest */
  127. static int lgread_other(struct lguest *lg,
  128. void *buf, u32 addr, unsigned bytes)
  129. {
  130. if (!lguest_address_ok(lg, addr, bytes)
  131. || access_process_vm(lg->tsk, addr, buf, bytes, 0) != bytes) {
  132. memset(buf, 0, bytes);
  133. kill_guest(lg, "bad address in registered DMA struct");
  134. return 0;
  135. }
  136. return 1;
  137. }
  138. /* lgwrite to another guest */
  139. static int lgwrite_other(struct lguest *lg, u32 addr,
  140. const void *buf, unsigned bytes)
  141. {
  142. if (!lguest_address_ok(lg, addr, bytes)
  143. || (access_process_vm(lg->tsk, addr, (void *)buf, bytes, 1)
  144. != bytes)) {
  145. kill_guest(lg, "bad address writing to registered DMA");
  146. return 0;
  147. }
  148. return 1;
  149. }
  150. static u32 copy_data(struct lguest *srclg,
  151. const struct lguest_dma *src,
  152. const struct lguest_dma *dst,
  153. struct page *pages[])
  154. {
  155. unsigned int totlen, si, di, srcoff, dstoff;
  156. void *maddr = NULL;
  157. totlen = 0;
  158. si = di = 0;
  159. srcoff = dstoff = 0;
  160. while (si < LGUEST_MAX_DMA_SECTIONS && src->len[si]
  161. && di < LGUEST_MAX_DMA_SECTIONS && dst->len[di]) {
  162. u32 len = min(src->len[si] - srcoff, dst->len[di] - dstoff);
  163. if (!maddr)
  164. maddr = kmap(pages[di]);
  165. /* FIXME: This is not completely portable, since
  166. archs do different things for copy_to_user_page. */
  167. if (copy_from_user(maddr + (dst->addr[di] + dstoff)%PAGE_SIZE,
  168. (void *__user)src->addr[si], len) != 0) {
  169. kill_guest(srclg, "bad address in sending DMA");
  170. totlen = 0;
  171. break;
  172. }
  173. totlen += len;
  174. srcoff += len;
  175. dstoff += len;
  176. if (srcoff == src->len[si]) {
  177. si++;
  178. srcoff = 0;
  179. }
  180. if (dstoff == dst->len[di]) {
  181. kunmap(pages[di]);
  182. maddr = NULL;
  183. di++;
  184. dstoff = 0;
  185. }
  186. }
  187. if (maddr)
  188. kunmap(pages[di]);
  189. return totlen;
  190. }
  191. /* Src is us, ie. current. */
  192. static u32 do_dma(struct lguest *srclg, const struct lguest_dma *src,
  193. struct lguest *dstlg, const struct lguest_dma *dst)
  194. {
  195. int i;
  196. u32 ret;
  197. struct page *pages[LGUEST_MAX_DMA_SECTIONS];
  198. if (!check_dma_list(dstlg, dst) || !check_dma_list(srclg, src))
  199. return 0;
  200. /* First get the destination pages */
  201. for (i = 0; i < LGUEST_MAX_DMA_SECTIONS; i++) {
  202. if (dst->len[i] == 0)
  203. break;
  204. if (get_user_pages(dstlg->tsk, dstlg->mm,
  205. dst->addr[i], 1, 1, 1, pages+i, NULL)
  206. != 1) {
  207. kill_guest(dstlg, "Error mapping DMA pages");
  208. ret = 0;
  209. goto drop_pages;
  210. }
  211. }
  212. /* Now copy until we run out of src or dst. */
  213. ret = copy_data(srclg, src, dst, pages);
  214. drop_pages:
  215. while (--i >= 0)
  216. put_page(pages[i]);
  217. return ret;
  218. }
  219. static int dma_transfer(struct lguest *srclg,
  220. unsigned long udma,
  221. struct lguest_dma_info *dst)
  222. {
  223. struct lguest_dma dst_dma, src_dma;
  224. struct lguest *dstlg;
  225. u32 i, dma = 0;
  226. dstlg = &lguests[dst->guestid];
  227. /* Get our dma list. */
  228. lgread(srclg, &src_dma, udma, sizeof(src_dma));
  229. /* We can't deadlock against them dmaing to us, because this
  230. * is all under the lguest_lock. */
  231. down_read(&dstlg->mm->mmap_sem);
  232. for (i = 0; i < dst->num_dmas; i++) {
  233. dma = (dst->next_dma + i) % dst->num_dmas;
  234. if (!lgread_other(dstlg, &dst_dma,
  235. dst->dmas + dma * sizeof(struct lguest_dma),
  236. sizeof(dst_dma))) {
  237. goto fail;
  238. }
  239. if (!dst_dma.used_len)
  240. break;
  241. }
  242. if (i != dst->num_dmas) {
  243. unsigned long used_lenp;
  244. unsigned int ret;
  245. ret = do_dma(srclg, &src_dma, dstlg, &dst_dma);
  246. /* Put used length in src. */
  247. lgwrite_u32(srclg,
  248. udma+offsetof(struct lguest_dma, used_len), ret);
  249. if (ret == 0 && src_dma.len[0] != 0)
  250. goto fail;
  251. /* Make sure destination sees contents before length. */
  252. wmb();
  253. used_lenp = dst->dmas
  254. + dma * sizeof(struct lguest_dma)
  255. + offsetof(struct lguest_dma, used_len);
  256. lgwrite_other(dstlg, used_lenp, &ret, sizeof(ret));
  257. dst->next_dma++;
  258. }
  259. up_read(&dstlg->mm->mmap_sem);
  260. /* Do this last so dst doesn't simply sleep on lock. */
  261. set_bit(dst->interrupt, dstlg->irqs_pending);
  262. wake_up_process(dstlg->tsk);
  263. return i == dst->num_dmas;
  264. fail:
  265. up_read(&dstlg->mm->mmap_sem);
  266. return 0;
  267. }
  268. void send_dma(struct lguest *lg, unsigned long ukey, unsigned long udma)
  269. {
  270. union futex_key key;
  271. int empty = 0;
  272. struct rw_semaphore *fshared = &current->mm->mmap_sem;
  273. again:
  274. mutex_lock(&lguest_lock);
  275. down_read(fshared);
  276. if (get_futex_key((u32 __user *)ukey, fshared, &key) != 0) {
  277. kill_guest(lg, "bad sending DMA key");
  278. goto unlock;
  279. }
  280. /* Shared mapping? Look for other guests... */
  281. if (key.shared.offset & 1) {
  282. struct lguest_dma_info *i;
  283. list_for_each_entry(i, &dma_hash[hash(&key)], list) {
  284. if (i->guestid == lg->guestid)
  285. continue;
  286. if (!key_eq(&key, &i->key))
  287. continue;
  288. empty += dma_transfer(lg, udma, i);
  289. break;
  290. }
  291. if (empty == 1) {
  292. /* Give any recipients one chance to restock. */
  293. up_read(&current->mm->mmap_sem);
  294. mutex_unlock(&lguest_lock);
  295. empty++;
  296. goto again;
  297. }
  298. } else {
  299. /* Private mapping: tell our userspace. */
  300. lg->dma_is_pending = 1;
  301. lg->pending_dma = udma;
  302. lg->pending_key = ukey;
  303. }
  304. unlock:
  305. up_read(fshared);
  306. mutex_unlock(&lguest_lock);
  307. }
  308. void release_all_dma(struct lguest *lg)
  309. {
  310. unsigned int i;
  311. BUG_ON(!mutex_is_locked(&lguest_lock));
  312. down_read(&lg->mm->mmap_sem);
  313. for (i = 0; i < LGUEST_MAX_DMA; i++) {
  314. if (lg->dma[i].interrupt)
  315. unlink_dma(&lg->dma[i]);
  316. }
  317. up_read(&lg->mm->mmap_sem);
  318. }
  319. /* Userspace wants a dma buffer from this guest. */
  320. unsigned long get_dma_buffer(struct lguest *lg,
  321. unsigned long ukey, unsigned long *interrupt)
  322. {
  323. unsigned long ret = 0;
  324. union futex_key key;
  325. struct lguest_dma_info *i;
  326. struct rw_semaphore *fshared = &current->mm->mmap_sem;
  327. mutex_lock(&lguest_lock);
  328. down_read(fshared);
  329. if (get_futex_key((u32 __user *)ukey, fshared, &key) != 0) {
  330. kill_guest(lg, "bad registered DMA buffer");
  331. goto unlock;
  332. }
  333. list_for_each_entry(i, &dma_hash[hash(&key)], list) {
  334. if (key_eq(&key, &i->key) && i->guestid == lg->guestid) {
  335. unsigned int j;
  336. for (j = 0; j < i->num_dmas; j++) {
  337. struct lguest_dma dma;
  338. ret = i->dmas + j * sizeof(struct lguest_dma);
  339. lgread(lg, &dma, ret, sizeof(dma));
  340. if (dma.used_len == 0)
  341. break;
  342. }
  343. *interrupt = i->interrupt;
  344. break;
  345. }
  346. }
  347. unlock:
  348. up_read(fshared);
  349. mutex_unlock(&lguest_lock);
  350. return ret;
  351. }