hypercalls.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. */
  188. lg->lguest_data = (struct lguest_data __user *)lg->regs->edx;
  189. /* If we check the address they gave is OK now, we can simply
  190. * copy_to_user/from_user from now on rather than using lgread/lgwrite.
  191. * I put this in to show that I'm not immune to writing stupid
  192. * optimizations. */
  193. if (!lguest_address_ok(lg, lg->regs->edx, sizeof(*lg->lguest_data))) {
  194. kill_guest(lg, "bad guest page %p", lg->lguest_data);
  195. return;
  196. }
  197. /* The Guest tells us where we're not to deliver interrupts by putting
  198. * the range of addresses into "struct lguest_data". */
  199. if (get_user(lg->noirq_start, &lg->lguest_data->noirq_start)
  200. || get_user(lg->noirq_end, &lg->lguest_data->noirq_end)
  201. /* We tell the Guest that it can't use the top 4MB of virtual
  202. * addresses used by the Switcher. */
  203. || put_user(4U*1024*1024, &lg->lguest_data->reserve_mem)
  204. || put_user(tsc_speed, &lg->lguest_data->tsc_khz)
  205. /* We also give the Guest a unique id, as used in lguest_net.c. */
  206. || put_user(lg->guestid, &lg->lguest_data->guestid))
  207. kill_guest(lg, "bad guest page %p", lg->lguest_data);
  208. /* We write the current time into the Guest's data page once now. */
  209. write_timestamp(lg);
  210. /* This is the one case where the above accesses might have been the
  211. * first write to a Guest page. This may have caused a copy-on-write
  212. * fault, but the Guest might be referring to the old (read-only)
  213. * page. */
  214. guest_pagetable_clear_all(lg);
  215. }
  216. /* Now we've examined the hypercall code; our Guest can make requests. There
  217. * is one other way we can do things for the Guest, as we see in
  218. * emulate_insn(). */
  219. /*H:110 Tricky point: we mark the hypercall as "done" once we've done it.
  220. * Normally we don't need to do this: the Guest will run again and update the
  221. * trap number before we come back around the run_guest() loop to
  222. * do_hypercalls().
  223. *
  224. * However, if we are signalled or the Guest sends DMA to the Launcher, that
  225. * loop will exit without running the Guest. When it comes back it would try
  226. * to re-run the hypercall. */
  227. static void clear_hcall(struct lguest *lg)
  228. {
  229. lg->regs->trapnum = 255;
  230. }
  231. /*H:100
  232. * Hypercalls
  233. *
  234. * Remember from the Guest, hypercalls come in two flavors: normal and
  235. * asynchronous. This file handles both of types.
  236. */
  237. void do_hypercalls(struct lguest *lg)
  238. {
  239. /* Not initialized yet? */
  240. if (unlikely(!lg->lguest_data)) {
  241. /* Did the Guest make a hypercall? We might have come back for
  242. * some other reason (an interrupt, a different trap). */
  243. if (lg->regs->trapnum == LGUEST_TRAP_ENTRY) {
  244. /* Set up the "struct lguest_data" */
  245. initialize(lg);
  246. /* The hypercall is done. */
  247. clear_hcall(lg);
  248. }
  249. return;
  250. }
  251. /* The Guest has initialized.
  252. *
  253. * Look in the hypercall ring for the async hypercalls: */
  254. do_async_hcalls(lg);
  255. /* If we stopped reading the hypercall ring because the Guest did a
  256. * SEND_DMA to the Launcher, we want to return now. Otherwise if the
  257. * Guest asked us to do a hypercall, we do it. */
  258. if (!lg->dma_is_pending && lg->regs->trapnum == LGUEST_TRAP_ENTRY) {
  259. do_hcall(lg, lg->regs);
  260. /* The hypercall is done. */
  261. clear_hcall(lg);
  262. }
  263. }
  264. /* This routine supplies the Guest with time: it's used for wallclock time at
  265. * initial boot and as a rough time source if the TSC isn't available. */
  266. void write_timestamp(struct lguest *lg)
  267. {
  268. struct timespec now;
  269. ktime_get_real_ts(&now);
  270. if (put_user(now, &lg->lguest_data->time))
  271. kill_guest(lg, "Writing timestamp");
  272. }