hypercalls.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*P:500 Just as userspace programs request kernel operations through a system
  2. * call, the Guest requests Host operations through a "hypercall". You might
  3. * notice this nomenclature doesn't really follow any logic, but the name has
  4. * been around for long enough that we're stuck with it. As you'd expect, this
  5. * code is basically a one big switch statement. :*/
  6. /* Copyright (C) 2006 Rusty Russell IBM Corporation
  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. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include <linux/uaccess.h>
  20. #include <linux/syscalls.h>
  21. #include <linux/mm.h>
  22. #include <asm/page.h>
  23. #include <asm/pgtable.h>
  24. #include "lg.h"
  25. /*H:120 This is the core hypercall routine: where the Guest gets what it wants.
  26. * Or gets killed. Or, in the case of LHCALL_CRASH, both. */
  27. static void do_hcall(struct lguest *lg, struct hcall_args *args)
  28. {
  29. switch (args->arg0) {
  30. case LHCALL_FLUSH_ASYNC:
  31. /* This call does nothing, except by breaking out of the Guest
  32. * it makes us process all the asynchronous hypercalls. */
  33. break;
  34. case LHCALL_LGUEST_INIT:
  35. /* You can't get here unless you're already initialized. Don't
  36. * do that. */
  37. kill_guest(lg, "already have lguest_data");
  38. break;
  39. case LHCALL_CRASH: {
  40. /* Crash is such a trivial hypercall that we do it in four
  41. * lines right here. */
  42. char msg[128];
  43. /* If the lgread fails, it will call kill_guest() itself; the
  44. * kill_guest() with the message will be ignored. */
  45. lgread(lg, msg, args->arg1, sizeof(msg));
  46. msg[sizeof(msg)-1] = '\0';
  47. kill_guest(lg, "CRASH: %s", msg);
  48. break;
  49. }
  50. case LHCALL_FLUSH_TLB:
  51. /* FLUSH_TLB comes in two flavors, depending on the
  52. * argument: */
  53. if (args->arg1)
  54. guest_pagetable_clear_all(lg);
  55. else
  56. guest_pagetable_flush_user(lg);
  57. break;
  58. case LHCALL_BIND_DMA:
  59. /* BIND_DMA really wants four arguments, but it's the only call
  60. * which does. So the Guest packs the number of buffers and
  61. * the interrupt number into the final argument, and we decode
  62. * it here. This can legitimately fail, since we currently
  63. * place a limit on the number of DMA pools a Guest can have.
  64. * So we return true or false from this call. */
  65. args->arg0 = bind_dma(lg, args->arg1, args->arg2,
  66. args->arg3 >> 8, args->arg3 & 0xFF);
  67. break;
  68. /* All these calls simply pass the arguments through to the right
  69. * routines. */
  70. case LHCALL_SEND_DMA:
  71. send_dma(lg, args->arg1, args->arg2);
  72. break;
  73. case LHCALL_NEW_PGTABLE:
  74. guest_new_pagetable(lg, args->arg1);
  75. break;
  76. case LHCALL_SET_STACK:
  77. guest_set_stack(lg, args->arg1, args->arg2, args->arg3);
  78. break;
  79. case LHCALL_SET_PTE:
  80. guest_set_pte(lg, args->arg1, args->arg2, mkgpte(args->arg3));
  81. break;
  82. case LHCALL_SET_PMD:
  83. guest_set_pmd(lg, args->arg1, args->arg2);
  84. break;
  85. case LHCALL_SET_CLOCKEVENT:
  86. guest_set_clockevent(lg, args->arg1);
  87. break;
  88. case LHCALL_TS:
  89. /* This sets the TS flag, as we saw used in run_guest(). */
  90. lg->ts = args->arg1;
  91. break;
  92. case LHCALL_HALT:
  93. /* Similarly, this sets the halted flag for run_guest(). */
  94. lg->halted = 1;
  95. break;
  96. default:
  97. if (lguest_arch_do_hcall(lg, args))
  98. kill_guest(lg, "Bad hypercall %li\n", args->arg0);
  99. }
  100. }
  101. /*:*/
  102. /*H:124 Asynchronous hypercalls are easy: we just look in the array in the
  103. * Guest's "struct lguest_data" to see if any new ones are marked "ready".
  104. *
  105. * We are careful to do these in order: obviously we respect the order the
  106. * Guest put them in the ring, but we also promise the Guest that they will
  107. * happen before any normal hypercall (which is why we check this before
  108. * checking for a normal hcall). */
  109. static void do_async_hcalls(struct lguest *lg)
  110. {
  111. unsigned int i;
  112. u8 st[LHCALL_RING_SIZE];
  113. /* For simplicity, we copy the entire call status array in at once. */
  114. if (copy_from_user(&st, &lg->lguest_data->hcall_status, sizeof(st)))
  115. return;
  116. /* We process "struct lguest_data"s hcalls[] ring once. */
  117. for (i = 0; i < ARRAY_SIZE(st); i++) {
  118. struct hcall_args args;
  119. /* We remember where we were up to from last time. This makes
  120. * sure that the hypercalls are done in the order the Guest
  121. * places them in the ring. */
  122. unsigned int n = lg->next_hcall;
  123. /* 0xFF means there's no call here (yet). */
  124. if (st[n] == 0xFF)
  125. break;
  126. /* OK, we have hypercall. Increment the "next_hcall" cursor,
  127. * and wrap back to 0 if we reach the end. */
  128. if (++lg->next_hcall == LHCALL_RING_SIZE)
  129. lg->next_hcall = 0;
  130. /* Copy the hypercall arguments into a local copy of
  131. * the hcall_args struct. */
  132. if (copy_from_user(&args, &lg->lguest_data->hcalls[n],
  133. sizeof(struct hcall_args))) {
  134. kill_guest(lg, "Fetching async hypercalls");
  135. break;
  136. }
  137. /* Do the hypercall, same as a normal one. */
  138. do_hcall(lg, &args);
  139. /* Mark the hypercall done. */
  140. if (put_user(0xFF, &lg->lguest_data->hcall_status[n])) {
  141. kill_guest(lg, "Writing result for async hypercall");
  142. break;
  143. }
  144. /* Stop doing hypercalls if we've just done a DMA to the
  145. * Launcher: it needs to service this first. */
  146. if (lg->dma_is_pending)
  147. break;
  148. }
  149. }
  150. /* Last of all, we look at what happens first of all. The very first time the
  151. * Guest makes a hypercall, we end up here to set things up: */
  152. static void initialize(struct lguest *lg)
  153. {
  154. /* You can't do anything until you're initialized. The Guest knows the
  155. * rules, so we're unforgiving here. */
  156. if (lg->hcall->arg0 != LHCALL_LGUEST_INIT) {
  157. kill_guest(lg, "hypercall %li before INIT", lg->hcall->arg0);
  158. return;
  159. }
  160. if (lguest_arch_init_hypercalls(lg))
  161. kill_guest(lg, "bad guest page %p", lg->lguest_data);
  162. /* The Guest tells us where we're not to deliver interrupts by putting
  163. * the range of addresses into "struct lguest_data". */
  164. if (get_user(lg->noirq_start, &lg->lguest_data->noirq_start)
  165. || get_user(lg->noirq_end, &lg->lguest_data->noirq_end)
  166. /* We tell the Guest that it can't use the top 4MB of virtual
  167. * addresses used by the Switcher. */
  168. || put_user(4U*1024*1024, &lg->lguest_data->reserve_mem))
  169. kill_guest(lg, "bad guest page %p", lg->lguest_data);
  170. /* We write the current time into the Guest's data page once now. */
  171. write_timestamp(lg);
  172. /* This is the one case where the above accesses might have been the
  173. * first write to a Guest page. This may have caused a copy-on-write
  174. * fault, but the Guest might be referring to the old (read-only)
  175. * page. */
  176. guest_pagetable_clear_all(lg);
  177. }
  178. /*H:100
  179. * Hypercalls
  180. *
  181. * Remember from the Guest, hypercalls come in two flavors: normal and
  182. * asynchronous. This file handles both of types.
  183. */
  184. void do_hypercalls(struct lguest *lg)
  185. {
  186. /* Not initialized yet? This hypercall must do it. */
  187. if (unlikely(!lg->lguest_data)) {
  188. /* Set up the "struct lguest_data" */
  189. initialize(lg);
  190. /* Hcall is done. */
  191. lg->hcall = NULL;
  192. return;
  193. }
  194. /* The Guest has initialized.
  195. *
  196. * Look in the hypercall ring for the async hypercalls: */
  197. do_async_hcalls(lg);
  198. /* If we stopped reading the hypercall ring because the Guest did a
  199. * SEND_DMA to the Launcher, we want to return now. Otherwise we do
  200. * the hypercall. */
  201. if (!lg->dma_is_pending) {
  202. do_hcall(lg, lg->hcall);
  203. /* Tricky point: we reset the hcall pointer to mark the
  204. * hypercall as "done". We use the hcall pointer rather than
  205. * the trap number to indicate a hypercall is pending.
  206. * Normally it doesn't matter: the Guest will run again and
  207. * update the trap number before we come back here.
  208. *
  209. * However, if we are signalled or the Guest sends DMA to the
  210. * Launcher, the run_guest() loop will exit without running the
  211. * Guest. When it comes back it would try to re-run the
  212. * hypercall. */
  213. lg->hcall = NULL;
  214. }
  215. }
  216. /* This routine supplies the Guest with time: it's used for wallclock time at
  217. * initial boot and as a rough time source if the TSC isn't available. */
  218. void write_timestamp(struct lguest *lg)
  219. {
  220. struct timespec now;
  221. ktime_get_real_ts(&now);
  222. if (copy_to_user(&lg->lguest_data->time, &now, sizeof(struct timespec)))
  223. kill_guest(lg, "Writing timestamp");
  224. }