hypercalls.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*P:500
  2. * Just as userspace programs request kernel operations through a system
  3. * call, the Guest requests Host operations through a "hypercall". You might
  4. * notice this nomenclature doesn't really follow any logic, but the name has
  5. * been around for long enough that we're stuck with it. As you'd expect, this
  6. * code is basically a one big switch statement.
  7. :*/
  8. /* Copyright (C) 2006 Rusty Russell IBM Corporation
  9. This program is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 2 of the License, or
  12. (at your option) any later version.
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <linux/uaccess.h>
  22. #include <linux/syscalls.h>
  23. #include <linux/mm.h>
  24. #include <linux/ktime.h>
  25. #include <asm/page.h>
  26. #include <asm/pgtable.h>
  27. #include "lg.h"
  28. /*H:120
  29. * This is the core hypercall routine: where the Guest gets what it wants.
  30. * Or gets killed. Or, in the case of LHCALL_SHUTDOWN, both.
  31. */
  32. static void do_hcall(struct lg_cpu *cpu, struct hcall_args *args)
  33. {
  34. switch (args->arg0) {
  35. case LHCALL_FLUSH_ASYNC:
  36. /*
  37. * This call does nothing, except by breaking out of the Guest
  38. * it makes us process all the asynchronous hypercalls.
  39. */
  40. break;
  41. case LHCALL_SEND_INTERRUPTS:
  42. /*
  43. * This call does nothing too, but by breaking out of the Guest
  44. * it makes us process any pending interrupts.
  45. */
  46. break;
  47. case LHCALL_LGUEST_INIT:
  48. /*
  49. * You can't get here unless you're already initialized. Don't
  50. * do that.
  51. */
  52. kill_guest(cpu, "already have lguest_data");
  53. break;
  54. case LHCALL_SHUTDOWN: {
  55. char msg[128];
  56. /*
  57. * Shutdown is such a trivial hypercall that we do it in five
  58. * lines right here.
  59. *
  60. * If the lgread fails, it will call kill_guest() itself; the
  61. * kill_guest() with the message will be ignored.
  62. */
  63. __lgread(cpu, msg, args->arg1, sizeof(msg));
  64. msg[sizeof(msg)-1] = '\0';
  65. kill_guest(cpu, "CRASH: %s", msg);
  66. if (args->arg2 == LGUEST_SHUTDOWN_RESTART)
  67. cpu->lg->dead = ERR_PTR(-ERESTART);
  68. break;
  69. }
  70. case LHCALL_FLUSH_TLB:
  71. /* FLUSH_TLB comes in two flavors, depending on the argument: */
  72. if (args->arg1)
  73. guest_pagetable_clear_all(cpu);
  74. else
  75. guest_pagetable_flush_user(cpu);
  76. break;
  77. /*
  78. * All these calls simply pass the arguments through to the right
  79. * routines.
  80. */
  81. case LHCALL_NEW_PGTABLE:
  82. guest_new_pagetable(cpu, args->arg1);
  83. break;
  84. case LHCALL_SET_STACK:
  85. guest_set_stack(cpu, args->arg1, args->arg2, args->arg3);
  86. break;
  87. case LHCALL_SET_PTE:
  88. #ifdef CONFIG_X86_PAE
  89. guest_set_pte(cpu, args->arg1, args->arg2,
  90. __pte(args->arg3 | (u64)args->arg4 << 32));
  91. #else
  92. guest_set_pte(cpu, args->arg1, args->arg2, __pte(args->arg3));
  93. #endif
  94. break;
  95. case LHCALL_SET_PGD:
  96. guest_set_pgd(cpu->lg, args->arg1, args->arg2);
  97. break;
  98. #ifdef CONFIG_X86_PAE
  99. case LHCALL_SET_PMD:
  100. guest_set_pmd(cpu->lg, args->arg1, args->arg2);
  101. break;
  102. #endif
  103. case LHCALL_SET_CLOCKEVENT:
  104. guest_set_clockevent(cpu, args->arg1);
  105. break;
  106. case LHCALL_TS:
  107. /* This sets the TS flag, as we saw used in run_guest(). */
  108. cpu->ts = args->arg1;
  109. break;
  110. case LHCALL_HALT:
  111. /* Similarly, this sets the halted flag for run_guest(). */
  112. cpu->halted = 1;
  113. break;
  114. case LHCALL_NOTIFY:
  115. cpu->pending_notify = args->arg1;
  116. break;
  117. default:
  118. /* It should be an architecture-specific hypercall. */
  119. if (lguest_arch_do_hcall(cpu, args))
  120. kill_guest(cpu, "Bad hypercall %li\n", args->arg0);
  121. }
  122. }
  123. /*H:124
  124. * Asynchronous hypercalls are easy: we just look in the array in the
  125. * Guest's "struct lguest_data" to see if any new ones are marked "ready".
  126. *
  127. * We are careful to do these in order: obviously we respect the order the
  128. * Guest put them in the ring, but we also promise the Guest that they will
  129. * happen before any normal hypercall (which is why we check this before
  130. * checking for a normal hcall).
  131. */
  132. static void do_async_hcalls(struct lg_cpu *cpu)
  133. {
  134. unsigned int i;
  135. u8 st[LHCALL_RING_SIZE];
  136. /* For simplicity, we copy the entire call status array in at once. */
  137. if (copy_from_user(&st, &cpu->lg->lguest_data->hcall_status, sizeof(st)))
  138. return;
  139. /* We process "struct lguest_data"s hcalls[] ring once. */
  140. for (i = 0; i < ARRAY_SIZE(st); i++) {
  141. struct hcall_args args;
  142. /*
  143. * We remember where we were up to from last time. This makes
  144. * sure that the hypercalls are done in the order the Guest
  145. * places them in the ring.
  146. */
  147. unsigned int n = cpu->next_hcall;
  148. /* 0xFF means there's no call here (yet). */
  149. if (st[n] == 0xFF)
  150. break;
  151. /*
  152. * OK, we have hypercall. Increment the "next_hcall" cursor,
  153. * and wrap back to 0 if we reach the end.
  154. */
  155. if (++cpu->next_hcall == LHCALL_RING_SIZE)
  156. cpu->next_hcall = 0;
  157. /*
  158. * Copy the hypercall arguments into a local copy of the
  159. * hcall_args struct.
  160. */
  161. if (copy_from_user(&args, &cpu->lg->lguest_data->hcalls[n],
  162. sizeof(struct hcall_args))) {
  163. kill_guest(cpu, "Fetching async hypercalls");
  164. break;
  165. }
  166. /* Do the hypercall, same as a normal one. */
  167. do_hcall(cpu, &args);
  168. /* Mark the hypercall done. */
  169. if (put_user(0xFF, &cpu->lg->lguest_data->hcall_status[n])) {
  170. kill_guest(cpu, "Writing result for async hypercall");
  171. break;
  172. }
  173. /*
  174. * Stop doing hypercalls if they want to notify the Launcher:
  175. * it needs to service this first.
  176. */
  177. if (cpu->pending_notify)
  178. break;
  179. }
  180. }
  181. /*
  182. * Last of all, we look at what happens first of all. The very first time the
  183. * Guest makes a hypercall, we end up here to set things up:
  184. */
  185. static void initialize(struct lg_cpu *cpu)
  186. {
  187. /*
  188. * You can't do anything until you're initialized. The Guest knows the
  189. * rules, so we're unforgiving here.
  190. */
  191. if (cpu->hcall->arg0 != LHCALL_LGUEST_INIT) {
  192. kill_guest(cpu, "hypercall %li before INIT", cpu->hcall->arg0);
  193. return;
  194. }
  195. if (lguest_arch_init_hypercalls(cpu))
  196. kill_guest(cpu, "bad guest page %p", cpu->lg->lguest_data);
  197. /*
  198. * The Guest tells us where we're not to deliver interrupts by putting
  199. * the range of addresses into "struct lguest_data".
  200. */
  201. if (get_user(cpu->lg->noirq_start, &cpu->lg->lguest_data->noirq_start)
  202. || get_user(cpu->lg->noirq_end, &cpu->lg->lguest_data->noirq_end))
  203. kill_guest(cpu, "bad guest page %p", cpu->lg->lguest_data);
  204. /*
  205. * We write the current time into the Guest's data page once so it can
  206. * set its clock.
  207. */
  208. write_timestamp(cpu);
  209. /* page_tables.c will also do some setup. */
  210. page_table_guest_data_init(cpu);
  211. /*
  212. * This is the one case where the above accesses might have been the
  213. * first write to a Guest page. This may have caused a copy-on-write
  214. * fault, but the old page might be (read-only) in the Guest
  215. * pagetable.
  216. */
  217. guest_pagetable_clear_all(cpu);
  218. }
  219. /*:*/
  220. /*M:013
  221. * If a Guest reads from a page (so creates a mapping) that it has never
  222. * written to, and then the Launcher writes to it (ie. the output of a virtual
  223. * device), the Guest will still see the old page. In practice, this never
  224. * happens: why would the Guest read a page which it has never written to? But
  225. * a similar scenario might one day bite us, so it's worth mentioning.
  226. *
  227. * Note that if we used a shared anonymous mapping in the Launcher instead of
  228. * mapping /dev/zero private, we wouldn't worry about cop-on-write. And we
  229. * need that to switch the Launcher to processes (away from threads) anyway.
  230. :*/
  231. /*H:100
  232. * Hypercalls
  233. *
  234. * Remember from the Guest, hypercalls come in two flavors: normal and
  235. * asynchronous. This file handles both of types.
  236. */
  237. void do_hypercalls(struct lg_cpu *cpu)
  238. {
  239. /* Not initialized yet? This hypercall must do it. */
  240. if (unlikely(!cpu->lg->lguest_data)) {
  241. /* Set up the "struct lguest_data" */
  242. initialize(cpu);
  243. /* Hcall is done. */
  244. cpu->hcall = NULL;
  245. return;
  246. }
  247. /*
  248. * The Guest has initialized.
  249. *
  250. * Look in the hypercall ring for the async hypercalls:
  251. */
  252. do_async_hcalls(cpu);
  253. /*
  254. * If we stopped reading the hypercall ring because the Guest did a
  255. * NOTIFY to the Launcher, we want to return now. Otherwise we do
  256. * the hypercall.
  257. */
  258. if (!cpu->pending_notify) {
  259. do_hcall(cpu, cpu->hcall);
  260. /*
  261. * Tricky point: we reset the hcall pointer to mark the
  262. * hypercall as "done". We use the hcall pointer rather than
  263. * the trap number to indicate a hypercall is pending.
  264. * Normally it doesn't matter: the Guest will run again and
  265. * update the trap number before we come back here.
  266. *
  267. * However, if we are signalled or the Guest sends I/O to the
  268. * Launcher, the run_guest() loop will exit without running the
  269. * Guest. When it comes back it would try to re-run the
  270. * hypercall. Finding that bug sucked.
  271. */
  272. cpu->hcall = NULL;
  273. }
  274. }
  275. /*
  276. * This routine supplies the Guest with time: it's used for wallclock time at
  277. * initial boot and as a rough time source if the TSC isn't available.
  278. */
  279. void write_timestamp(struct lg_cpu *cpu)
  280. {
  281. struct timespec now;
  282. ktime_get_real_ts(&now);
  283. if (copy_to_user(&cpu->lg->lguest_data->time,
  284. &now, sizeof(struct timespec)))
  285. kill_guest(cpu, "Writing timestamp");
  286. }