hypercalls.c 7.9 KB

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