hypercalls.c 8.1 KB

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