hypercalls.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 <linux/ktime.h>
  23. #include <asm/page.h>
  24. #include <asm/pgtable.h>
  25. #include "lg.h"
  26. /*H:120 This is the core hypercall routine: where the Guest gets what it wants.
  27. * Or gets killed. Or, in the case of LHCALL_SHUTDOWN, both. */
  28. static void do_hcall(struct lg_cpu *cpu, struct hcall_args *args)
  29. {
  30. switch (args->arg0) {
  31. case LHCALL_FLUSH_ASYNC:
  32. /* This call does nothing, except by breaking out of the Guest
  33. * it makes us process all the asynchronous hypercalls. */
  34. break;
  35. case LHCALL_SEND_INTERRUPTS:
  36. /* This call does nothing too, but by breaking out of the Guest
  37. * it makes us process any pending interrupts. */
  38. break;
  39. case LHCALL_LGUEST_INIT:
  40. /* You can't get here unless you're already initialized. Don't
  41. * do that. */
  42. kill_guest(cpu, "already have lguest_data");
  43. break;
  44. case LHCALL_SHUTDOWN: {
  45. /* Shutdown is such a trivial hypercall that we do it in four
  46. * lines right here. */
  47. char msg[128];
  48. /* If the lgread fails, it will call kill_guest() itself; the
  49. * kill_guest() with the message will be ignored. */
  50. __lgread(cpu, msg, args->arg1, sizeof(msg));
  51. msg[sizeof(msg)-1] = '\0';
  52. kill_guest(cpu, "CRASH: %s", msg);
  53. if (args->arg2 == LGUEST_SHUTDOWN_RESTART)
  54. cpu->lg->dead = ERR_PTR(-ERESTART);
  55. break;
  56. }
  57. case LHCALL_FLUSH_TLB:
  58. /* FLUSH_TLB comes in two flavors, depending on the
  59. * argument: */
  60. if (args->arg1)
  61. guest_pagetable_clear_all(cpu);
  62. else
  63. guest_pagetable_flush_user(cpu);
  64. break;
  65. /* All these calls simply pass the arguments through to the right
  66. * routines. */
  67. case LHCALL_NEW_PGTABLE:
  68. guest_new_pagetable(cpu, args->arg1);
  69. break;
  70. case LHCALL_SET_STACK:
  71. guest_set_stack(cpu, args->arg1, args->arg2, args->arg3);
  72. break;
  73. case LHCALL_SET_PTE:
  74. #ifdef CONFIG_X86_PAE
  75. guest_set_pte(cpu, args->arg1, args->arg2,
  76. __pte(args->arg3 | (u64)args->arg4 << 32));
  77. #else
  78. guest_set_pte(cpu, args->arg1, args->arg2, __pte(args->arg3));
  79. #endif
  80. break;
  81. case LHCALL_SET_PGD:
  82. guest_set_pgd(cpu->lg, args->arg1, args->arg2);
  83. break;
  84. #ifdef CONFIG_X86_PAE
  85. case LHCALL_SET_PMD:
  86. guest_set_pmd(cpu->lg, args->arg1, args->arg2);
  87. break;
  88. #endif
  89. case LHCALL_SET_CLOCKEVENT:
  90. guest_set_clockevent(cpu, args->arg1);
  91. break;
  92. case LHCALL_TS:
  93. /* This sets the TS flag, as we saw used in run_guest(). */
  94. cpu->ts = args->arg1;
  95. break;
  96. case LHCALL_HALT:
  97. /* Similarly, this sets the halted flag for run_guest(). */
  98. cpu->halted = 1;
  99. break;
  100. case LHCALL_NOTIFY:
  101. cpu->pending_notify = args->arg1;
  102. break;
  103. default:
  104. /* It should be an architecture-specific hypercall. */
  105. if (lguest_arch_do_hcall(cpu, args))
  106. kill_guest(cpu, "Bad hypercall %li\n", args->arg0);
  107. }
  108. }
  109. /*:*/
  110. /*H:124 Asynchronous hypercalls are easy: we just look in the array in the
  111. * Guest's "struct lguest_data" to see if any new ones are marked "ready".
  112. *
  113. * We are careful to do these in order: obviously we respect the order the
  114. * Guest put them in the ring, but we also promise the Guest that they will
  115. * happen before any normal hypercall (which is why we check this before
  116. * checking for a normal hcall). */
  117. static void do_async_hcalls(struct lg_cpu *cpu)
  118. {
  119. unsigned int i;
  120. u8 st[LHCALL_RING_SIZE];
  121. /* For simplicity, we copy the entire call status array in at once. */
  122. if (copy_from_user(&st, &cpu->lg->lguest_data->hcall_status, sizeof(st)))
  123. return;
  124. /* We process "struct lguest_data"s hcalls[] ring once. */
  125. for (i = 0; i < ARRAY_SIZE(st); i++) {
  126. struct hcall_args args;
  127. /* We remember where we were up to from last time. This makes
  128. * sure that the hypercalls are done in the order the Guest
  129. * places them in the ring. */
  130. unsigned int n = cpu->next_hcall;
  131. /* 0xFF means there's no call here (yet). */
  132. if (st[n] == 0xFF)
  133. break;
  134. /* OK, we have hypercall. Increment the "next_hcall" cursor,
  135. * and wrap back to 0 if we reach the end. */
  136. if (++cpu->next_hcall == LHCALL_RING_SIZE)
  137. cpu->next_hcall = 0;
  138. /* Copy the hypercall arguments into a local copy of
  139. * the hcall_args struct. */
  140. if (copy_from_user(&args, &cpu->lg->lguest_data->hcalls[n],
  141. sizeof(struct hcall_args))) {
  142. kill_guest(cpu, "Fetching async hypercalls");
  143. break;
  144. }
  145. /* Do the hypercall, same as a normal one. */
  146. do_hcall(cpu, &args);
  147. /* Mark the hypercall done. */
  148. if (put_user(0xFF, &cpu->lg->lguest_data->hcall_status[n])) {
  149. kill_guest(cpu, "Writing result for async hypercall");
  150. break;
  151. }
  152. /* Stop doing hypercalls if they want to notify the Launcher:
  153. * it needs to service this first. */
  154. if (cpu->pending_notify)
  155. break;
  156. }
  157. }
  158. /* Last of all, we look at what happens first of all. The very first time the
  159. * Guest makes a hypercall, we end up here to set things up: */
  160. static void initialize(struct lg_cpu *cpu)
  161. {
  162. /* You can't do anything until you're initialized. The Guest knows the
  163. * rules, so we're unforgiving here. */
  164. if (cpu->hcall->arg0 != LHCALL_LGUEST_INIT) {
  165. kill_guest(cpu, "hypercall %li before INIT", cpu->hcall->arg0);
  166. return;
  167. }
  168. if (lguest_arch_init_hypercalls(cpu))
  169. kill_guest(cpu, "bad guest page %p", cpu->lg->lguest_data);
  170. /* The Guest tells us where we're not to deliver interrupts by putting
  171. * the range of addresses into "struct lguest_data". */
  172. if (get_user(cpu->lg->noirq_start, &cpu->lg->lguest_data->noirq_start)
  173. || get_user(cpu->lg->noirq_end, &cpu->lg->lguest_data->noirq_end))
  174. kill_guest(cpu, "bad guest page %p", cpu->lg->lguest_data);
  175. /* We write the current time into the Guest's data page once so it can
  176. * set its clock. */
  177. write_timestamp(cpu);
  178. /* page_tables.c will also do some setup. */
  179. page_table_guest_data_init(cpu);
  180. /* This is the one case where the above accesses might have been the
  181. * first write to a Guest page. This may have caused a copy-on-write
  182. * fault, but the old page might be (read-only) in the Guest
  183. * pagetable. */
  184. guest_pagetable_clear_all(cpu);
  185. }
  186. /*:*/
  187. /*M:013 If a Guest reads from a page (so creates a mapping) that it has never
  188. * written to, and then the Launcher writes to it (ie. the output of a virtual
  189. * device), the Guest will still see the old page. In practice, this never
  190. * happens: why would the Guest read a page which it has never written to? But
  191. * a similar scenario might one day bite us, so it's worth mentioning. :*/
  192. /*H:100
  193. * Hypercalls
  194. *
  195. * Remember from the Guest, hypercalls come in two flavors: normal and
  196. * asynchronous. This file handles both of types.
  197. */
  198. void do_hypercalls(struct lg_cpu *cpu)
  199. {
  200. /* Not initialized yet? This hypercall must do it. */
  201. if (unlikely(!cpu->lg->lguest_data)) {
  202. /* Set up the "struct lguest_data" */
  203. initialize(cpu);
  204. /* Hcall is done. */
  205. cpu->hcall = NULL;
  206. return;
  207. }
  208. /* The Guest has initialized.
  209. *
  210. * Look in the hypercall ring for the async hypercalls: */
  211. do_async_hcalls(cpu);
  212. /* If we stopped reading the hypercall ring because the Guest did a
  213. * NOTIFY to the Launcher, we want to return now. Otherwise we do
  214. * the hypercall. */
  215. if (!cpu->pending_notify) {
  216. do_hcall(cpu, cpu->hcall);
  217. /* Tricky point: we reset the hcall pointer to mark the
  218. * hypercall as "done". We use the hcall pointer rather than
  219. * the trap number to indicate a hypercall is pending.
  220. * Normally it doesn't matter: the Guest will run again and
  221. * update the trap number before we come back here.
  222. *
  223. * However, if we are signalled or the Guest sends I/O to the
  224. * Launcher, the run_guest() loop will exit without running the
  225. * Guest. When it comes back it would try to re-run the
  226. * hypercall. Finding that bug sucked. */
  227. cpu->hcall = NULL;
  228. }
  229. }
  230. /* This routine supplies the Guest with time: it's used for wallclock time at
  231. * initial boot and as a rough time source if the TSC isn't available. */
  232. void write_timestamp(struct lg_cpu *cpu)
  233. {
  234. struct timespec now;
  235. ktime_get_real_ts(&now);
  236. if (copy_to_user(&cpu->lg->lguest_data->time,
  237. &now, sizeof(struct timespec)))
  238. kill_guest(cpu, "Writing timestamp");
  239. }