hypercalls.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 <irq_vectors.h>
  25. #include "lg.h"
  26. /*H:120 This is the core hypercall routine: where the Guest gets what it
  27. * wants. Or gets killed. Or, in the case of LHCALL_CRASH, both.
  28. *
  29. * Remember from the Guest: %eax == which call to make, and the arguments are
  30. * packed into %edx, %ebx and %ecx if needed. */
  31. static void do_hcall(struct lguest *lg, struct lguest_regs *regs)
  32. {
  33. switch (regs->eax) {
  34. case LHCALL_FLUSH_ASYNC:
  35. /* This call does nothing, except by breaking out of the Guest
  36. * it makes us process all the asynchronous hypercalls. */
  37. break;
  38. case LHCALL_LGUEST_INIT:
  39. /* You can't get here unless you're already initialized. Don't
  40. * do that. */
  41. kill_guest(lg, "already have lguest_data");
  42. break;
  43. case LHCALL_CRASH: {
  44. /* Crash is such a trivial hypercall that we do it in four
  45. * lines right here. */
  46. char msg[128];
  47. /* If the lgread fails, it will call kill_guest() itself; the
  48. * kill_guest() with the message will be ignored. */
  49. lgread(lg, msg, regs->edx, sizeof(msg));
  50. msg[sizeof(msg)-1] = '\0';
  51. kill_guest(lg, "CRASH: %s", msg);
  52. break;
  53. }
  54. case LHCALL_FLUSH_TLB:
  55. /* FLUSH_TLB comes in two flavors, depending on the
  56. * argument: */
  57. if (regs->edx)
  58. guest_pagetable_clear_all(lg);
  59. else
  60. guest_pagetable_flush_user(lg);
  61. break;
  62. case LHCALL_BIND_DMA:
  63. /* BIND_DMA really wants four arguments, but it's the only call
  64. * which does. So the Guest packs the number of buffers and
  65. * the interrupt number into the final argument, and we decode
  66. * it here. This can legitimately fail, since we currently
  67. * place a limit on the number of DMA pools a Guest can have.
  68. * So we return true or false from this call. */
  69. regs->eax = bind_dma(lg, regs->edx, regs->ebx,
  70. regs->ecx >> 8, regs->ecx & 0xFF);
  71. break;
  72. /* All these calls simply pass the arguments through to the right
  73. * routines. */
  74. case LHCALL_SEND_DMA:
  75. send_dma(lg, regs->edx, regs->ebx);
  76. break;
  77. case LHCALL_LOAD_GDT:
  78. load_guest_gdt(lg, regs->edx, regs->ebx);
  79. break;
  80. case LHCALL_LOAD_IDT_ENTRY:
  81. load_guest_idt_entry(lg, regs->edx, regs->ebx, regs->ecx);
  82. break;
  83. case LHCALL_NEW_PGTABLE:
  84. guest_new_pagetable(lg, regs->edx);
  85. break;
  86. case LHCALL_SET_STACK:
  87. guest_set_stack(lg, regs->edx, regs->ebx, regs->ecx);
  88. break;
  89. case LHCALL_SET_PTE:
  90. guest_set_pte(lg, regs->edx, regs->ebx, mkgpte(regs->ecx));
  91. break;
  92. case LHCALL_SET_PMD:
  93. guest_set_pmd(lg, regs->edx, regs->ebx);
  94. break;
  95. case LHCALL_LOAD_TLS:
  96. guest_load_tls(lg, regs->edx);
  97. break;
  98. case LHCALL_SET_CLOCKEVENT:
  99. guest_set_clockevent(lg, regs->edx);
  100. break;
  101. case LHCALL_TS:
  102. /* This sets the TS flag, as we saw used in run_guest(). */
  103. lg->ts = regs->edx;
  104. break;
  105. case LHCALL_HALT:
  106. /* Similarly, this sets the halted flag for run_guest(). */
  107. lg->halted = 1;
  108. break;
  109. default:
  110. kill_guest(lg, "Bad hypercall %li\n", regs->eax);
  111. }
  112. }
  113. /* Asynchronous hypercalls are easy: we just look in the array in the Guest's
  114. * "struct lguest_data" and see if there are any new ones marked "ready".
  115. *
  116. * We are careful to do these in order: obviously we respect the order the
  117. * Guest put them in the ring, but we also promise the Guest that they will
  118. * happen before any normal hypercall (which is why we check this before
  119. * checking for a normal hcall). */
  120. static void do_async_hcalls(struct lguest *lg)
  121. {
  122. unsigned int i;
  123. u8 st[LHCALL_RING_SIZE];
  124. /* For simplicity, we copy the entire call status array in at once. */
  125. if (copy_from_user(&st, &lg->lguest_data->hcall_status, sizeof(st)))
  126. return;
  127. /* We process "struct lguest_data"s hcalls[] ring once. */
  128. for (i = 0; i < ARRAY_SIZE(st); i++) {
  129. struct lguest_regs regs;
  130. /* We remember where we were up to from last time. This makes
  131. * sure that the hypercalls are done in the order the Guest
  132. * places them in the ring. */
  133. unsigned int n = lg->next_hcall;
  134. /* 0xFF means there's no call here (yet). */
  135. if (st[n] == 0xFF)
  136. break;
  137. /* OK, we have hypercall. Increment the "next_hcall" cursor,
  138. * and wrap back to 0 if we reach the end. */
  139. if (++lg->next_hcall == LHCALL_RING_SIZE)
  140. lg->next_hcall = 0;
  141. /* We copy the hypercall arguments into a fake register
  142. * structure. This makes life simple for do_hcall(). */
  143. if (get_user(regs.eax, &lg->lguest_data->hcalls[n].eax)
  144. || get_user(regs.edx, &lg->lguest_data->hcalls[n].edx)
  145. || get_user(regs.ecx, &lg->lguest_data->hcalls[n].ecx)
  146. || get_user(regs.ebx, &lg->lguest_data->hcalls[n].ebx)) {
  147. kill_guest(lg, "Fetching async hypercalls");
  148. break;
  149. }
  150. /* Do the hypercall, same as a normal one. */
  151. do_hcall(lg, &regs);
  152. /* Mark the hypercall done. */
  153. if (put_user(0xFF, &lg->lguest_data->hcall_status[n])) {
  154. kill_guest(lg, "Writing result for async hypercall");
  155. break;
  156. }
  157. /* Stop doing hypercalls if we've just done a DMA to the
  158. * Launcher: it needs to service this first. */
  159. if (lg->dma_is_pending)
  160. break;
  161. }
  162. }
  163. /* Last of all, we look at what happens first of all. The very first time the
  164. * Guest makes a hypercall, we end up here to set things up: */
  165. static void initialize(struct lguest *lg)
  166. {
  167. u32 tsc_speed;
  168. /* You can't do anything until you're initialized. The Guest knows the
  169. * rules, so we're unforgiving here. */
  170. if (lg->regs->eax != LHCALL_LGUEST_INIT) {
  171. kill_guest(lg, "hypercall %li before LGUEST_INIT",
  172. lg->regs->eax);
  173. return;
  174. }
  175. /* We insist that the Time Stamp Counter exist and doesn't change with
  176. * cpu frequency. Some devious chip manufacturers decided that TSC
  177. * changes could be handled in software. I decided that time going
  178. * backwards might be good for benchmarks, but it's bad for users.
  179. *
  180. * We also insist that the TSC be stable: the kernel detects unreliable
  181. * TSCs for its own purposes, and we use that here. */
  182. if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC) && !check_tsc_unstable())
  183. tsc_speed = tsc_khz;
  184. else
  185. tsc_speed = 0;
  186. /* The pointer to the Guest's "struct lguest_data" is the only
  187. * argument. We check that address now. */
  188. if (!lguest_address_ok(lg, lg->regs->edx, sizeof(*lg->lguest_data))) {
  189. kill_guest(lg, "bad guest page %p", lg->lguest_data);
  190. return;
  191. }
  192. /* Having checked it, we simply set lg->lguest_data to point straight
  193. * into the Launcher's memory at the right place and then use
  194. * copy_to_user/from_user from now on, instead of lgread/write. I put
  195. * this in to show that I'm not immune to writing stupid
  196. * optimizations. */
  197. lg->lguest_data = lg->mem_base + lg->regs->edx;
  198. /* The Guest tells us where we're not to deliver interrupts by putting
  199. * the range of addresses into "struct lguest_data". */
  200. if (get_user(lg->noirq_start, &lg->lguest_data->noirq_start)
  201. || get_user(lg->noirq_end, &lg->lguest_data->noirq_end)
  202. /* We tell the Guest that it can't use the top 4MB of virtual
  203. * addresses used by the Switcher. */
  204. || put_user(4U*1024*1024, &lg->lguest_data->reserve_mem)
  205. || put_user(tsc_speed, &lg->lguest_data->tsc_khz))
  206. kill_guest(lg, "bad guest page %p", lg->lguest_data);
  207. /* We write the current time into the Guest's data page once now. */
  208. write_timestamp(lg);
  209. /* This is the one case where the above accesses might have been the
  210. * first write to a Guest page. This may have caused a copy-on-write
  211. * fault, but the Guest might be referring to the old (read-only)
  212. * page. */
  213. guest_pagetable_clear_all(lg);
  214. }
  215. /* Now we've examined the hypercall code; our Guest can make requests. There
  216. * is one other way we can do things for the Guest, as we see in
  217. * emulate_insn(). */
  218. /*H:100
  219. * Hypercalls
  220. *
  221. * Remember from the Guest, hypercalls come in two flavors: normal and
  222. * asynchronous. This file handles both of types.
  223. */
  224. void do_hypercalls(struct lguest *lg)
  225. {
  226. /* Not initialized yet? This hypercall must do it. */
  227. if (unlikely(!lg->lguest_data)) {
  228. /* Set up the "struct lguest_data" */
  229. initialize(lg);
  230. /* Hcall is done. */
  231. lg->hcall = NULL;
  232. return;
  233. }
  234. /* The Guest has initialized.
  235. *
  236. * Look in the hypercall ring for the async hypercalls: */
  237. do_async_hcalls(lg);
  238. /* If we stopped reading the hypercall ring because the Guest did a
  239. * SEND_DMA to the Launcher, we want to return now. Otherwise we do
  240. * the hypercall. */
  241. if (!lg->dma_is_pending) {
  242. do_hcall(lg, lg->hcall);
  243. /* Tricky point: we reset the hcall pointer to mark the
  244. * hypercall as "done". We use the hcall pointer rather than
  245. * the trap number to indicate a hypercall is pending.
  246. * Normally it doesn't matter: the Guest will run again and
  247. * update the trap number before we come back here.
  248. *
  249. * However, if we are signalled or the Guest sends DMA to the
  250. * Launcher, the run_guest() loop will exit without running the
  251. * Guest. When it comes back it would try to re-run the
  252. * hypercall. */
  253. lg->hcall = NULL;
  254. }
  255. }
  256. /* This routine supplies the Guest with time: it's used for wallclock time at
  257. * initial boot and as a rough time source if the TSC isn't available. */
  258. void write_timestamp(struct lguest *lg)
  259. {
  260. struct timespec now;
  261. ktime_get_real_ts(&now);
  262. if (copy_to_user(&lg->lguest_data->time, &now, sizeof(struct timespec)))
  263. kill_guest(lg, "Writing timestamp");
  264. }