hypercalls.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. /* All these calls simply pass the arguments through to the right
  59. * routines. */
  60. case LHCALL_NEW_PGTABLE:
  61. guest_new_pagetable(lg, args->arg1);
  62. break;
  63. case LHCALL_SET_STACK:
  64. guest_set_stack(lg, args->arg1, args->arg2, args->arg3);
  65. break;
  66. case LHCALL_SET_PTE:
  67. guest_set_pte(lg, args->arg1, args->arg2, __pte(args->arg3));
  68. break;
  69. case LHCALL_SET_PMD:
  70. guest_set_pmd(lg, args->arg1, args->arg2);
  71. break;
  72. case LHCALL_SET_CLOCKEVENT:
  73. guest_set_clockevent(lg, args->arg1);
  74. break;
  75. case LHCALL_TS:
  76. /* This sets the TS flag, as we saw used in run_guest(). */
  77. lg->ts = args->arg1;
  78. break;
  79. case LHCALL_HALT:
  80. /* Similarly, this sets the halted flag for run_guest(). */
  81. lg->halted = 1;
  82. break;
  83. case LHCALL_NOTIFY:
  84. lg->pending_notify = args->arg1;
  85. break;
  86. default:
  87. /* It should be an architecture-specific hypercall. */
  88. if (lguest_arch_do_hcall(lg, args))
  89. kill_guest(lg, "Bad hypercall %li\n", args->arg0);
  90. }
  91. }
  92. /*:*/
  93. /*H:124 Asynchronous hypercalls are easy: we just look in the array in the
  94. * Guest's "struct lguest_data" to see if any new ones are marked "ready".
  95. *
  96. * We are careful to do these in order: obviously we respect the order the
  97. * Guest put them in the ring, but we also promise the Guest that they will
  98. * happen before any normal hypercall (which is why we check this before
  99. * checking for a normal hcall). */
  100. static void do_async_hcalls(struct lguest *lg)
  101. {
  102. unsigned int i;
  103. u8 st[LHCALL_RING_SIZE];
  104. /* For simplicity, we copy the entire call status array in at once. */
  105. if (copy_from_user(&st, &lg->lguest_data->hcall_status, sizeof(st)))
  106. return;
  107. /* We process "struct lguest_data"s hcalls[] ring once. */
  108. for (i = 0; i < ARRAY_SIZE(st); i++) {
  109. struct hcall_args args;
  110. /* We remember where we were up to from last time. This makes
  111. * sure that the hypercalls are done in the order the Guest
  112. * places them in the ring. */
  113. unsigned int n = lg->next_hcall;
  114. /* 0xFF means there's no call here (yet). */
  115. if (st[n] == 0xFF)
  116. break;
  117. /* OK, we have hypercall. Increment the "next_hcall" cursor,
  118. * and wrap back to 0 if we reach the end. */
  119. if (++lg->next_hcall == LHCALL_RING_SIZE)
  120. lg->next_hcall = 0;
  121. /* Copy the hypercall arguments into a local copy of
  122. * the hcall_args struct. */
  123. if (copy_from_user(&args, &lg->lguest_data->hcalls[n],
  124. sizeof(struct hcall_args))) {
  125. kill_guest(lg, "Fetching async hypercalls");
  126. break;
  127. }
  128. /* Do the hypercall, same as a normal one. */
  129. do_hcall(lg, &args);
  130. /* Mark the hypercall done. */
  131. if (put_user(0xFF, &lg->lguest_data->hcall_status[n])) {
  132. kill_guest(lg, "Writing result for async hypercall");
  133. break;
  134. }
  135. /* Stop doing hypercalls if they want to notify the Launcher:
  136. * it needs to service this first. */
  137. if (lg->pending_notify)
  138. break;
  139. }
  140. }
  141. /* Last of all, we look at what happens first of all. The very first time the
  142. * Guest makes a hypercall, we end up here to set things up: */
  143. static void initialize(struct lguest *lg)
  144. {
  145. /* You can't do anything until you're initialized. The Guest knows the
  146. * rules, so we're unforgiving here. */
  147. if (lg->hcall->arg0 != LHCALL_LGUEST_INIT) {
  148. kill_guest(lg, "hypercall %li before INIT", lg->hcall->arg0);
  149. return;
  150. }
  151. if (lguest_arch_init_hypercalls(lg))
  152. kill_guest(lg, "bad guest page %p", lg->lguest_data);
  153. /* The Guest tells us where we're not to deliver interrupts by putting
  154. * the range of addresses into "struct lguest_data". */
  155. if (get_user(lg->noirq_start, &lg->lguest_data->noirq_start)
  156. || get_user(lg->noirq_end, &lg->lguest_data->noirq_end))
  157. kill_guest(lg, "bad guest page %p", lg->lguest_data);
  158. /* We write the current time into the Guest's data page once so it can
  159. * set its clock. */
  160. write_timestamp(lg);
  161. /* page_tables.c will also do some setup. */
  162. page_table_guest_data_init(lg);
  163. /* This is the one case where the above accesses might have been the
  164. * first write to a Guest page. This may have caused a copy-on-write
  165. * fault, but the old page might be (read-only) in the Guest
  166. * pagetable. */
  167. guest_pagetable_clear_all(lg);
  168. }
  169. /*H:100
  170. * Hypercalls
  171. *
  172. * Remember from the Guest, hypercalls come in two flavors: normal and
  173. * asynchronous. This file handles both of types.
  174. */
  175. void do_hypercalls(struct lguest *lg)
  176. {
  177. /* Not initialized yet? This hypercall must do it. */
  178. if (unlikely(!lg->lguest_data)) {
  179. /* Set up the "struct lguest_data" */
  180. initialize(lg);
  181. /* Hcall is done. */
  182. lg->hcall = NULL;
  183. return;
  184. }
  185. /* The Guest has initialized.
  186. *
  187. * Look in the hypercall ring for the async hypercalls: */
  188. do_async_hcalls(lg);
  189. /* If we stopped reading the hypercall ring because the Guest did a
  190. * NOTIFY to the Launcher, we want to return now. Otherwise we do
  191. * the hypercall. */
  192. if (!lg->pending_notify) {
  193. do_hcall(lg, lg->hcall);
  194. /* Tricky point: we reset the hcall pointer to mark the
  195. * hypercall as "done". We use the hcall pointer rather than
  196. * the trap number to indicate a hypercall is pending.
  197. * Normally it doesn't matter: the Guest will run again and
  198. * update the trap number before we come back here.
  199. *
  200. * However, if we are signalled or the Guest sends I/O to the
  201. * Launcher, the run_guest() loop will exit without running the
  202. * Guest. When it comes back it would try to re-run the
  203. * hypercall. */
  204. lg->hcall = NULL;
  205. }
  206. }
  207. /* This routine supplies the Guest with time: it's used for wallclock time at
  208. * initial boot and as a rough time source if the TSC isn't available. */
  209. void write_timestamp(struct lguest *lg)
  210. {
  211. struct timespec now;
  212. ktime_get_real_ts(&now);
  213. if (copy_to_user(&lg->lguest_data->time, &now, sizeof(struct timespec)))
  214. kill_guest(lg, "Writing timestamp");
  215. }