hypercalls.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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_LGUEST_INIT:
  36. /* You can't get here unless you're already initialized. Don't
  37. * do that. */
  38. kill_guest(cpu, "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(cpu, msg, args->arg1, sizeof(msg));
  47. msg[sizeof(msg)-1] = '\0';
  48. kill_guest(cpu, "CRASH: %s", msg);
  49. if (args->arg2 == LGUEST_SHUTDOWN_RESTART)
  50. cpu->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(cpu);
  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(cpu, args->arg1, args->arg2, __pte(args->arg3));
  71. break;
  72. case LHCALL_SET_PMD:
  73. guest_set_pmd(cpu->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. cpu->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(cpu, "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. /* For simplicity, we copy the entire call status array in at once. */
  108. if (copy_from_user(&st, &cpu->lg->lguest_data->hcall_status, sizeof(st)))
  109. return;
  110. /* We process "struct lguest_data"s hcalls[] ring once. */
  111. for (i = 0; i < ARRAY_SIZE(st); i++) {
  112. struct hcall_args args;
  113. /* We remember where we were up to from last time. This makes
  114. * sure that the hypercalls are done in the order the Guest
  115. * places them in the ring. */
  116. unsigned int n = cpu->next_hcall;
  117. /* 0xFF means there's no call here (yet). */
  118. if (st[n] == 0xFF)
  119. break;
  120. /* OK, we have hypercall. Increment the "next_hcall" cursor,
  121. * and wrap back to 0 if we reach the end. */
  122. if (++cpu->next_hcall == LHCALL_RING_SIZE)
  123. cpu->next_hcall = 0;
  124. /* Copy the hypercall arguments into a local copy of
  125. * the hcall_args struct. */
  126. if (copy_from_user(&args, &cpu->lg->lguest_data->hcalls[n],
  127. sizeof(struct hcall_args))) {
  128. kill_guest(cpu, "Fetching async hypercalls");
  129. break;
  130. }
  131. /* Do the hypercall, same as a normal one. */
  132. do_hcall(cpu, &args);
  133. /* Mark the hypercall done. */
  134. if (put_user(0xFF, &cpu->lg->lguest_data->hcall_status[n])) {
  135. kill_guest(cpu, "Writing result for async hypercall");
  136. break;
  137. }
  138. /* Stop doing hypercalls if they want to notify the Launcher:
  139. * it needs to service this first. */
  140. if (cpu->pending_notify)
  141. break;
  142. }
  143. }
  144. /* Last of all, we look at what happens first of all. The very first time the
  145. * Guest makes a hypercall, we end up here to set things up: */
  146. static void initialize(struct lg_cpu *cpu)
  147. {
  148. /* You can't do anything until you're initialized. The Guest knows the
  149. * rules, so we're unforgiving here. */
  150. if (cpu->hcall->arg0 != LHCALL_LGUEST_INIT) {
  151. kill_guest(cpu, "hypercall %li before INIT", cpu->hcall->arg0);
  152. return;
  153. }
  154. if (lguest_arch_init_hypercalls(cpu))
  155. kill_guest(cpu, "bad guest page %p", cpu->lg->lguest_data);
  156. /* The Guest tells us where we're not to deliver interrupts by putting
  157. * the range of addresses into "struct lguest_data". */
  158. if (get_user(cpu->lg->noirq_start, &cpu->lg->lguest_data->noirq_start)
  159. || get_user(cpu->lg->noirq_end, &cpu->lg->lguest_data->noirq_end))
  160. kill_guest(cpu, "bad guest page %p", cpu->lg->lguest_data);
  161. /* We write the current time into the Guest's data page once so it can
  162. * set its clock. */
  163. write_timestamp(cpu);
  164. /* page_tables.c will also do some setup. */
  165. page_table_guest_data_init(cpu);
  166. /* This is the one case where the above accesses might have been the
  167. * first write to a Guest page. This may have caused a copy-on-write
  168. * fault, but the old page might be (read-only) in the Guest
  169. * pagetable. */
  170. guest_pagetable_clear_all(cpu);
  171. }
  172. /*:*/
  173. /*M:013 If a Guest reads from a page (so creates a mapping) that it has never
  174. * written to, and then the Launcher writes to it (ie. the output of a virtual
  175. * device), the Guest will still see the old page. In practice, this never
  176. * happens: why would the Guest read a page which it has never written to? But
  177. * a similar scenario might one day bite us, so it's worth mentioning. :*/
  178. /*H:100
  179. * Hypercalls
  180. *
  181. * Remember from the Guest, hypercalls come in two flavors: normal and
  182. * asynchronous. This file handles both of types.
  183. */
  184. void do_hypercalls(struct lg_cpu *cpu)
  185. {
  186. /* Not initialized yet? This hypercall must do it. */
  187. if (unlikely(!cpu->lg->lguest_data)) {
  188. /* Set up the "struct lguest_data" */
  189. initialize(cpu);
  190. /* Hcall is done. */
  191. cpu->hcall = NULL;
  192. return;
  193. }
  194. /* The Guest has initialized.
  195. *
  196. * Look in the hypercall ring for the async hypercalls: */
  197. do_async_hcalls(cpu);
  198. /* If we stopped reading the hypercall ring because the Guest did a
  199. * NOTIFY to the Launcher, we want to return now. Otherwise we do
  200. * the hypercall. */
  201. if (!cpu->pending_notify) {
  202. do_hcall(cpu, cpu->hcall);
  203. /* Tricky point: we reset the hcall pointer to mark the
  204. * hypercall as "done". We use the hcall pointer rather than
  205. * the trap number to indicate a hypercall is pending.
  206. * Normally it doesn't matter: the Guest will run again and
  207. * update the trap number before we come back here.
  208. *
  209. * However, if we are signalled or the Guest sends I/O to the
  210. * Launcher, the run_guest() loop will exit without running the
  211. * Guest. When it comes back it would try to re-run the
  212. * hypercall. Finding that bug sucked. */
  213. cpu->hcall = NULL;
  214. }
  215. }
  216. /* This routine supplies the Guest with time: it's used for wallclock time at
  217. * initial boot and as a rough time source if the TSC isn't available. */
  218. void write_timestamp(struct lg_cpu *cpu)
  219. {
  220. struct timespec now;
  221. ktime_get_real_ts(&now);
  222. if (copy_to_user(&cpu->lg->lguest_data->time,
  223. &now, sizeof(struct timespec)))
  224. kill_guest(cpu, "Writing timestamp");
  225. }