io.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. /*P:300 The I/O mechanism in lguest is simple yet flexible, allowing the Guest
  2. * to talk to the Launcher or directly to another Guest. It uses familiar
  3. * concepts of DMA and interrupts, plus some neat code stolen from
  4. * futexes... :*/
  5. /* Copyright (C) 2006 Rusty Russell IBM Corporation
  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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <linux/types.h>
  22. #include <linux/futex.h>
  23. #include <linux/jhash.h>
  24. #include <linux/mm.h>
  25. #include <linux/highmem.h>
  26. #include <linux/uaccess.h>
  27. #include "lg.h"
  28. /*L:300
  29. * I/O
  30. *
  31. * Getting data in and out of the Guest is quite an art. There are numerous
  32. * ways to do it, and they all suck differently. We try to keep things fairly
  33. * close to "real" hardware so our Guest's drivers don't look like an alien
  34. * visitation in the middle of the Linux code, and yet make sure that Guests
  35. * can talk directly to other Guests, not just the Launcher.
  36. *
  37. * To do this, the Guest gives us a key when it binds or sends DMA buffers.
  38. * The key corresponds to a "physical" address inside the Guest (ie. a virtual
  39. * address inside the Launcher process). We don't, however, use this key
  40. * directly.
  41. *
  42. * We want Guests which share memory to be able to DMA to each other: two
  43. * Launchers can mmap memory the same file, then the Guests can communicate.
  44. * Fortunately, the futex code provides us with a way to get a "union
  45. * futex_key" corresponding to the memory lying at a virtual address: if the
  46. * two processes share memory, the "union futex_key" for that memory will match
  47. * even if the memory is mapped at different addresses in each. So we always
  48. * convert the keys to "union futex_key"s to compare them.
  49. *
  50. * Before we dive into this though, we need to look at another set of helper
  51. * routines used throughout the Host kernel code to access Guest memory.
  52. :*/
  53. static struct list_head dma_hash[61];
  54. /* An unfortunate side effect of the Linux double-linked list implementation is
  55. * that there's no good way to statically initialize an array of linked
  56. * lists. */
  57. void lguest_io_init(void)
  58. {
  59. unsigned int i;
  60. for (i = 0; i < ARRAY_SIZE(dma_hash); i++)
  61. INIT_LIST_HEAD(&dma_hash[i]);
  62. }
  63. /* FIXME: allow multi-page lengths. */
  64. static int check_dma_list(struct lguest *lg, const struct lguest_dma *dma)
  65. {
  66. unsigned int i;
  67. for (i = 0; i < LGUEST_MAX_DMA_SECTIONS; i++) {
  68. if (!dma->len[i])
  69. return 1;
  70. if (!lguest_address_ok(lg, dma->addr[i], dma->len[i]))
  71. goto kill;
  72. if (dma->len[i] > PAGE_SIZE)
  73. goto kill;
  74. /* We could do over a page, but is it worth it? */
  75. if ((dma->addr[i] % PAGE_SIZE) + dma->len[i] > PAGE_SIZE)
  76. goto kill;
  77. }
  78. return 1;
  79. kill:
  80. kill_guest(lg, "bad DMA entry: %u@%#lx", dma->len[i], dma->addr[i]);
  81. return 0;
  82. }
  83. /*L:330 This is our hash function, using the wonderful Jenkins hash.
  84. *
  85. * The futex key is a union with three parts: an unsigned long word, a pointer,
  86. * and an int "offset". We could use jhash_2words() which takes three u32s.
  87. * (Ok, the hash functions are great: the naming sucks though).
  88. *
  89. * It's nice to be portable to 64-bit platforms, so we use the more generic
  90. * jhash2(), which takes an array of u32, the number of u32s, and an initial
  91. * u32 to roll in. This is uglier, but breaks down to almost the same code on
  92. * 32-bit platforms like this one.
  93. *
  94. * We want a position in the array, so we modulo ARRAY_SIZE(dma_hash) (ie. 61).
  95. */
  96. static unsigned int hash(const union futex_key *key)
  97. {
  98. return jhash2((u32*)&key->both.word,
  99. (sizeof(key->both.word)+sizeof(key->both.ptr))/4,
  100. key->both.offset)
  101. % ARRAY_SIZE(dma_hash);
  102. }
  103. /* This is a convenience routine to compare two keys. It's a much bemoaned C
  104. * weakness that it doesn't allow '==' on structures or unions, so we have to
  105. * open-code it like this. */
  106. static inline int key_eq(const union futex_key *a, const union futex_key *b)
  107. {
  108. return (a->both.word == b->both.word
  109. && a->both.ptr == b->both.ptr
  110. && a->both.offset == b->both.offset);
  111. }
  112. /*L:360 OK, when we need to actually free up a Guest's DMA array we do several
  113. * things, so we have a convenient function to do it.
  114. *
  115. * The caller must hold a read lock on dmainfo owner's current->mm->mmap_sem
  116. * for the drop_futex_key_refs(). */
  117. static void unlink_dma(struct lguest_dma_info *dmainfo)
  118. {
  119. /* You locked this too, right? */
  120. BUG_ON(!mutex_is_locked(&lguest_lock));
  121. /* This is how we know that the entry is free. */
  122. dmainfo->interrupt = 0;
  123. /* Remove it from the hash table. */
  124. list_del(&dmainfo->list);
  125. /* Drop the references we were holding (to the inode or mm). */
  126. drop_futex_key_refs(&dmainfo->key);
  127. }
  128. /*L:350 This is the routine which we call when the Guest asks to unregister a
  129. * DMA array attached to a given key. Returns true if the array was found. */
  130. static int unbind_dma(struct lguest *lg,
  131. const union futex_key *key,
  132. unsigned long dmas)
  133. {
  134. int i, ret = 0;
  135. /* We don't bother with the hash table, just look through all this
  136. * Guest's DMA arrays. */
  137. for (i = 0; i < LGUEST_MAX_DMA; i++) {
  138. /* In theory it could have more than one array on the same key,
  139. * or one array on multiple keys, so we check both */
  140. if (key_eq(key, &lg->dma[i].key) && dmas == lg->dma[i].dmas) {
  141. unlink_dma(&lg->dma[i]);
  142. ret = 1;
  143. break;
  144. }
  145. }
  146. return ret;
  147. }
  148. /*L:340 BIND_DMA: this is the hypercall which sets up an array of "struct
  149. * lguest_dma" for receiving I/O.
  150. *
  151. * The Guest wants to bind an array of "struct lguest_dma"s to a particular key
  152. * to receive input. This only happens when the Guest is setting up a new
  153. * device, so it doesn't have to be very fast.
  154. *
  155. * It returns 1 on a successful registration (it can fail if we hit the limit
  156. * of registrations for this Guest).
  157. */
  158. int bind_dma(struct lguest *lg,
  159. unsigned long ukey, unsigned long dmas, u16 numdmas, u8 interrupt)
  160. {
  161. unsigned int i;
  162. int ret = 0;
  163. union futex_key key;
  164. /* Futex code needs the mmap_sem. */
  165. struct rw_semaphore *fshared = &current->mm->mmap_sem;
  166. /* Invalid interrupt? (We could kill the guest here). */
  167. if (interrupt >= LGUEST_IRQS)
  168. return 0;
  169. /* We need to grab the Big Lguest Lock, because other Guests may be
  170. * trying to look through this Guest's DMAs to send something while
  171. * we're doing this. */
  172. mutex_lock(&lguest_lock);
  173. down_read(fshared);
  174. if (get_futex_key((u32 __user *)ukey, fshared, &key) != 0) {
  175. kill_guest(lg, "bad dma key %#lx", ukey);
  176. goto unlock;
  177. }
  178. /* We want to keep this key valid once we drop mmap_sem, so we have to
  179. * hold a reference. */
  180. get_futex_key_refs(&key);
  181. /* If the Guest specified an interrupt of 0, that means they want to
  182. * unregister this array of "struct lguest_dma"s. */
  183. if (interrupt == 0)
  184. ret = unbind_dma(lg, &key, dmas);
  185. else {
  186. /* Look through this Guest's dma array for an unused entry. */
  187. for (i = 0; i < LGUEST_MAX_DMA; i++) {
  188. /* If the interrupt is non-zero, the entry is already
  189. * used. */
  190. if (lg->dma[i].interrupt)
  191. continue;
  192. /* OK, a free one! Fill on our details. */
  193. lg->dma[i].dmas = dmas;
  194. lg->dma[i].num_dmas = numdmas;
  195. lg->dma[i].next_dma = 0;
  196. lg->dma[i].key = key;
  197. lg->dma[i].guestid = lg->guestid;
  198. lg->dma[i].interrupt = interrupt;
  199. /* Now we add it to the hash table: the position
  200. * depends on the futex key that we got. */
  201. list_add(&lg->dma[i].list, &dma_hash[hash(&key)]);
  202. /* Success! */
  203. ret = 1;
  204. goto unlock;
  205. }
  206. }
  207. /* If we didn't find a slot to put the key in, drop the reference
  208. * again. */
  209. drop_futex_key_refs(&key);
  210. unlock:
  211. /* Unlock and out. */
  212. up_read(fshared);
  213. mutex_unlock(&lguest_lock);
  214. return ret;
  215. }
  216. /*L:385 Note that our routines to access a different Guest's memory are called
  217. * lgread_other() and lgwrite_other(): these names emphasize that they are only
  218. * used when the Guest is *not* the current Guest.
  219. *
  220. * The interface for copying from another process's memory is called
  221. * access_process_vm(), with a final argument of 0 for a read, and 1 for a
  222. * write.
  223. *
  224. * We need lgread_other() to read the destination Guest's "struct lguest_dma"
  225. * array. */
  226. static int lgread_other(struct lguest *lg,
  227. void *buf, u32 addr, unsigned bytes)
  228. {
  229. if (!lguest_address_ok(lg, addr, bytes)
  230. || access_process_vm(lg->tsk, addr, buf, bytes, 0) != bytes) {
  231. memset(buf, 0, bytes);
  232. kill_guest(lg, "bad address in registered DMA struct");
  233. return 0;
  234. }
  235. return 1;
  236. }
  237. /* "lgwrite()" to another Guest: used to update the destination "used_len" once
  238. * we've transferred data into the buffer. */
  239. static int lgwrite_other(struct lguest *lg, u32 addr,
  240. const void *buf, unsigned bytes)
  241. {
  242. if (!lguest_address_ok(lg, addr, bytes)
  243. || (access_process_vm(lg->tsk, addr, (void *)buf, bytes, 1)
  244. != bytes)) {
  245. kill_guest(lg, "bad address writing to registered DMA");
  246. return 0;
  247. }
  248. return 1;
  249. }
  250. /*L:400 This is the generic engine which copies from a source "struct
  251. * lguest_dma" from this Guest into another Guest's "struct lguest_dma". The
  252. * destination Guest's pages have already been mapped, as contained in the
  253. * pages array.
  254. *
  255. * If you're wondering if there's a nice "copy from one process to another"
  256. * routine, so was I. But Linux isn't really set up to copy between two
  257. * unrelated processes, so we have to write it ourselves.
  258. */
  259. static u32 copy_data(struct lguest *srclg,
  260. const struct lguest_dma *src,
  261. const struct lguest_dma *dst,
  262. struct page *pages[])
  263. {
  264. unsigned int totlen, si, di, srcoff, dstoff;
  265. void *maddr = NULL;
  266. /* We return the total length transferred. */
  267. totlen = 0;
  268. /* We keep indexes into the source and destination "struct lguest_dma",
  269. * and an offset within each region. */
  270. si = di = 0;
  271. srcoff = dstoff = 0;
  272. /* We loop until the source or destination is exhausted. */
  273. while (si < LGUEST_MAX_DMA_SECTIONS && src->len[si]
  274. && di < LGUEST_MAX_DMA_SECTIONS && dst->len[di]) {
  275. /* We can only transfer the rest of the src buffer, or as much
  276. * as will fit into the destination buffer. */
  277. u32 len = min(src->len[si] - srcoff, dst->len[di] - dstoff);
  278. /* For systems using "highmem" we need to use kmap() to access
  279. * the page we want. We often use the same page over and over,
  280. * so rather than kmap() it on every loop, we set the maddr
  281. * pointer to NULL when we need to move to the next
  282. * destination page. */
  283. if (!maddr)
  284. maddr = kmap(pages[di]);
  285. /* Copy directly from (this Guest's) source address to the
  286. * destination Guest's kmap()ed buffer. Note that maddr points
  287. * to the start of the page: we need to add the offset of the
  288. * destination address and offset within the buffer. */
  289. /* FIXME: This is not completely portable. I looked at
  290. * copy_to_user_page(), and some arch's seem to need special
  291. * flushes. x86 is fine. */
  292. if (copy_from_user(maddr + (dst->addr[di] + dstoff)%PAGE_SIZE,
  293. (void __user *)src->addr[si], len) != 0) {
  294. /* If a copy failed, it's the source's fault. */
  295. kill_guest(srclg, "bad address in sending DMA");
  296. totlen = 0;
  297. break;
  298. }
  299. /* Increment the total and src & dst offsets */
  300. totlen += len;
  301. srcoff += len;
  302. dstoff += len;
  303. /* Presumably we reached the end of the src or dest buffers: */
  304. if (srcoff == src->len[si]) {
  305. /* Move to the next buffer at offset 0 */
  306. si++;
  307. srcoff = 0;
  308. }
  309. if (dstoff == dst->len[di]) {
  310. /* We need to unmap that destination page and reset
  311. * maddr ready for the next one. */
  312. kunmap(pages[di]);
  313. maddr = NULL;
  314. di++;
  315. dstoff = 0;
  316. }
  317. }
  318. /* If we still had a page mapped at the end, unmap now. */
  319. if (maddr)
  320. kunmap(pages[di]);
  321. return totlen;
  322. }
  323. /*L:390 This is how we transfer a "struct lguest_dma" from the source Guest
  324. * (the current Guest which called SEND_DMA) to another Guest. */
  325. static u32 do_dma(struct lguest *srclg, const struct lguest_dma *src,
  326. struct lguest *dstlg, const struct lguest_dma *dst)
  327. {
  328. int i;
  329. u32 ret;
  330. struct page *pages[LGUEST_MAX_DMA_SECTIONS];
  331. /* We check that both source and destination "struct lguest_dma"s are
  332. * within the bounds of the source and destination Guests */
  333. if (!check_dma_list(dstlg, dst) || !check_dma_list(srclg, src))
  334. return 0;
  335. /* We need to map the pages which correspond to each parts of
  336. * destination buffer. */
  337. for (i = 0; i < LGUEST_MAX_DMA_SECTIONS; i++) {
  338. if (dst->len[i] == 0)
  339. break;
  340. /* get_user_pages() is a complicated function, especially since
  341. * we only want a single page. But it works, and returns the
  342. * number of pages. Note that we're holding the destination's
  343. * mmap_sem, as get_user_pages() requires. */
  344. if (get_user_pages(dstlg->tsk, dstlg->mm,
  345. dst->addr[i], 1, 1, 1, pages+i, NULL)
  346. != 1) {
  347. /* This means the destination gave us a bogus buffer */
  348. kill_guest(dstlg, "Error mapping DMA pages");
  349. ret = 0;
  350. goto drop_pages;
  351. }
  352. }
  353. /* Now copy the data until we run out of src or dst. */
  354. ret = copy_data(srclg, src, dst, pages);
  355. drop_pages:
  356. while (--i >= 0)
  357. put_page(pages[i]);
  358. return ret;
  359. }
  360. /*L:380 Transferring data from one Guest to another is not as simple as I'd
  361. * like. We've found the "struct lguest_dma_info" bound to the same address as
  362. * the send, we need to copy into it.
  363. *
  364. * This function returns true if the destination array was empty. */
  365. static int dma_transfer(struct lguest *srclg,
  366. unsigned long udma,
  367. struct lguest_dma_info *dst)
  368. {
  369. struct lguest_dma dst_dma, src_dma;
  370. struct lguest *dstlg;
  371. u32 i, dma = 0;
  372. /* From the "struct lguest_dma_info" we found in the hash, grab the
  373. * Guest. */
  374. dstlg = &lguests[dst->guestid];
  375. /* Read in the source "struct lguest_dma" handed to SEND_DMA. */
  376. lgread(srclg, &src_dma, udma, sizeof(src_dma));
  377. /* We need the destination's mmap_sem, and we already hold the source's
  378. * mmap_sem for the futex key lookup. Normally this would suggest that
  379. * we could deadlock if the destination Guest was trying to send to
  380. * this source Guest at the same time, which is another reason that all
  381. * I/O is done under the big lguest_lock. */
  382. down_read(&dstlg->mm->mmap_sem);
  383. /* Look through the destination DMA array for an available buffer. */
  384. for (i = 0; i < dst->num_dmas; i++) {
  385. /* We keep a "next_dma" pointer which often helps us avoid
  386. * looking at lots of previously-filled entries. */
  387. dma = (dst->next_dma + i) % dst->num_dmas;
  388. if (!lgread_other(dstlg, &dst_dma,
  389. dst->dmas + dma * sizeof(struct lguest_dma),
  390. sizeof(dst_dma))) {
  391. goto fail;
  392. }
  393. if (!dst_dma.used_len)
  394. break;
  395. }
  396. /* If we found a buffer, we do the actual data copy. */
  397. if (i != dst->num_dmas) {
  398. unsigned long used_lenp;
  399. unsigned int ret;
  400. ret = do_dma(srclg, &src_dma, dstlg, &dst_dma);
  401. /* Put used length in the source "struct lguest_dma"'s used_len
  402. * field. It's a little tricky to figure out where that is,
  403. * though. */
  404. lgwrite_u32(srclg,
  405. udma+offsetof(struct lguest_dma, used_len), ret);
  406. /* Tranferring 0 bytes is OK if the source buffer was empty. */
  407. if (ret == 0 && src_dma.len[0] != 0)
  408. goto fail;
  409. /* The destination Guest might be running on a different CPU:
  410. * we have to make sure that it will see the "used_len" field
  411. * change to non-zero *after* it sees the data we copied into
  412. * the buffer. Hence a write memory barrier. */
  413. wmb();
  414. /* Figuring out where the destination's used_len field for this
  415. * "struct lguest_dma" in the array is also a little ugly. */
  416. used_lenp = dst->dmas
  417. + dma * sizeof(struct lguest_dma)
  418. + offsetof(struct lguest_dma, used_len);
  419. lgwrite_other(dstlg, used_lenp, &ret, sizeof(ret));
  420. /* Move the cursor for next time. */
  421. dst->next_dma++;
  422. }
  423. up_read(&dstlg->mm->mmap_sem);
  424. /* We trigger the destination interrupt, even if the destination was
  425. * empty and we didn't transfer anything: this gives them a chance to
  426. * wake up and refill. */
  427. set_bit(dst->interrupt, dstlg->irqs_pending);
  428. /* Wake up the destination process. */
  429. wake_up_process(dstlg->tsk);
  430. /* If we passed the last "struct lguest_dma", the receive had no
  431. * buffers left. */
  432. return i == dst->num_dmas;
  433. fail:
  434. up_read(&dstlg->mm->mmap_sem);
  435. return 0;
  436. }
  437. /*L:370 This is the counter-side to the BIND_DMA hypercall; the SEND_DMA
  438. * hypercall. We find out who's listening, and send to them. */
  439. void send_dma(struct lguest *lg, unsigned long ukey, unsigned long udma)
  440. {
  441. union futex_key key;
  442. int empty = 0;
  443. struct rw_semaphore *fshared = &current->mm->mmap_sem;
  444. again:
  445. mutex_lock(&lguest_lock);
  446. down_read(fshared);
  447. /* Get the futex key for the key the Guest gave us */
  448. if (get_futex_key((u32 __user *)ukey, fshared, &key) != 0) {
  449. kill_guest(lg, "bad sending DMA key");
  450. goto unlock;
  451. }
  452. /* Since the key must be a multiple of 4, the futex key uses the lower
  453. * bit of the "offset" field (which would always be 0) to indicate a
  454. * mapping which is shared with other processes (ie. Guests). */
  455. if (key.shared.offset & 1) {
  456. struct lguest_dma_info *i;
  457. /* Look through the hash for other Guests. */
  458. list_for_each_entry(i, &dma_hash[hash(&key)], list) {
  459. /* Don't send to ourselves. */
  460. if (i->guestid == lg->guestid)
  461. continue;
  462. if (!key_eq(&key, &i->key))
  463. continue;
  464. /* If dma_transfer() tells us the destination has no
  465. * available buffers, we increment "empty". */
  466. empty += dma_transfer(lg, udma, i);
  467. break;
  468. }
  469. /* If the destination is empty, we release our locks and
  470. * give the destination Guest a brief chance to restock. */
  471. if (empty == 1) {
  472. /* Give any recipients one chance to restock. */
  473. up_read(&current->mm->mmap_sem);
  474. mutex_unlock(&lguest_lock);
  475. /* Next time, we won't try again. */
  476. empty++;
  477. goto again;
  478. }
  479. } else {
  480. /* Private mapping: Guest is sending to its Launcher. We set
  481. * the "dma_is_pending" flag so that the main loop will exit
  482. * and the Launcher's read() from /dev/lguest will return. */
  483. lg->dma_is_pending = 1;
  484. lg->pending_dma = udma;
  485. lg->pending_key = ukey;
  486. }
  487. unlock:
  488. up_read(fshared);
  489. mutex_unlock(&lguest_lock);
  490. }
  491. /*:*/
  492. void release_all_dma(struct lguest *lg)
  493. {
  494. unsigned int i;
  495. BUG_ON(!mutex_is_locked(&lguest_lock));
  496. down_read(&lg->mm->mmap_sem);
  497. for (i = 0; i < LGUEST_MAX_DMA; i++) {
  498. if (lg->dma[i].interrupt)
  499. unlink_dma(&lg->dma[i]);
  500. }
  501. up_read(&lg->mm->mmap_sem);
  502. }
  503. /*M:007 We only return a single DMA buffer to the Launcher, but it would be
  504. * more efficient to return a pointer to the entire array of DMA buffers, which
  505. * it can cache and choose one whenever it wants.
  506. *
  507. * Currently the Launcher uses a write to /dev/lguest, and the return value is
  508. * the address of the DMA structure with the interrupt number placed in
  509. * dma->used_len. If we wanted to return the entire array, we need to return
  510. * the address, array size and interrupt number: this seems to require an
  511. * ioctl(). :*/
  512. /*L:320 This routine looks for a DMA buffer registered by the Guest on the
  513. * given key (using the BIND_DMA hypercall). */
  514. unsigned long get_dma_buffer(struct lguest *lg,
  515. unsigned long ukey, unsigned long *interrupt)
  516. {
  517. unsigned long ret = 0;
  518. union futex_key key;
  519. struct lguest_dma_info *i;
  520. struct rw_semaphore *fshared = &current->mm->mmap_sem;
  521. /* Take the Big Lguest Lock to stop other Guests sending this Guest DMA
  522. * at the same time. */
  523. mutex_lock(&lguest_lock);
  524. /* To match between Guests sharing the same underlying memory we steal
  525. * code from the futex infrastructure. This requires that we hold the
  526. * "mmap_sem" for our process (the Launcher), and pass it to the futex
  527. * code. */
  528. down_read(fshared);
  529. /* This can fail if it's not a valid address, or if the address is not
  530. * divisible by 4 (the futex code needs that, we don't really). */
  531. if (get_futex_key((u32 __user *)ukey, fshared, &key) != 0) {
  532. kill_guest(lg, "bad registered DMA buffer");
  533. goto unlock;
  534. }
  535. /* Search the hash table for matching entries (the Launcher can only
  536. * send to its own Guest for the moment, so the entry must be for this
  537. * Guest) */
  538. list_for_each_entry(i, &dma_hash[hash(&key)], list) {
  539. if (key_eq(&key, &i->key) && i->guestid == lg->guestid) {
  540. unsigned int j;
  541. /* Look through the registered DMA array for an
  542. * available buffer. */
  543. for (j = 0; j < i->num_dmas; j++) {
  544. struct lguest_dma dma;
  545. ret = i->dmas + j * sizeof(struct lguest_dma);
  546. lgread(lg, &dma, ret, sizeof(dma));
  547. if (dma.used_len == 0)
  548. break;
  549. }
  550. /* Store the interrupt the Guest wants when the buffer
  551. * is used. */
  552. *interrupt = i->interrupt;
  553. break;
  554. }
  555. }
  556. unlock:
  557. up_read(fshared);
  558. mutex_unlock(&lguest_lock);
  559. return ret;
  560. }
  561. /*:*/
  562. /*L:410 This really has completed the Launcher. Not only have we now finished
  563. * the longest chapter in our journey, but this also means we are over halfway
  564. * through!
  565. *
  566. * Enough prevaricating around the bush: it is time for us to dive into the
  567. * core of the Host, in "make Host".
  568. */