hypercalls.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. default:
  115. /* It should be an architecture-specific hypercall. */
  116. if (lguest_arch_do_hcall(cpu, args))
  117. kill_guest(cpu, "Bad hypercall %li\n", args->arg0);
  118. }
  119. }
  120. /*H:124
  121. * Asynchronous hypercalls are easy: we just look in the array in the
  122. * Guest's "struct lguest_data" to see if any new ones are marked "ready".
  123. *
  124. * We are careful to do these in order: obviously we respect the order the
  125. * Guest put them in the ring, but we also promise the Guest that they will
  126. * happen before any normal hypercall (which is why we check this before
  127. * checking for a normal hcall).
  128. */
  129. static void do_async_hcalls(struct lg_cpu *cpu)
  130. {
  131. unsigned int i;
  132. u8 st[LHCALL_RING_SIZE];
  133. /* For simplicity, we copy the entire call status array in at once. */
  134. if (copy_from_user(&st, &cpu->lg->lguest_data->hcall_status, sizeof(st)))
  135. return;
  136. /* We process "struct lguest_data"s hcalls[] ring once. */
  137. for (i = 0; i < ARRAY_SIZE(st); i++) {
  138. struct hcall_args args;
  139. /*
  140. * We remember where we were up to from last time. This makes
  141. * sure that the hypercalls are done in the order the Guest
  142. * places them in the ring.
  143. */
  144. unsigned int n = cpu->next_hcall;
  145. /* 0xFF means there's no call here (yet). */
  146. if (st[n] == 0xFF)
  147. break;
  148. /*
  149. * OK, we have hypercall. Increment the "next_hcall" cursor,
  150. * and wrap back to 0 if we reach the end.
  151. */
  152. if (++cpu->next_hcall == LHCALL_RING_SIZE)
  153. cpu->next_hcall = 0;
  154. /*
  155. * Copy the hypercall arguments into a local copy of the
  156. * hcall_args struct.
  157. */
  158. if (copy_from_user(&args, &cpu->lg->lguest_data->hcalls[n],
  159. sizeof(struct hcall_args))) {
  160. kill_guest(cpu, "Fetching async hypercalls");
  161. break;
  162. }
  163. /* Do the hypercall, same as a normal one. */
  164. do_hcall(cpu, &args);
  165. /* Mark the hypercall done. */
  166. if (put_user(0xFF, &cpu->lg->lguest_data->hcall_status[n])) {
  167. kill_guest(cpu, "Writing result for async hypercall");
  168. break;
  169. }
  170. /*
  171. * Stop doing hypercalls if they want to notify the Launcher:
  172. * it needs to service this first.
  173. */
  174. if (cpu->pending.trap)
  175. break;
  176. }
  177. }
  178. /*
  179. * Last of all, we look at what happens first of all. The very first time the
  180. * Guest makes a hypercall, we end up here to set things up:
  181. */
  182. static void initialize(struct lg_cpu *cpu)
  183. {
  184. /*
  185. * You can't do anything until you're initialized. The Guest knows the
  186. * rules, so we're unforgiving here.
  187. */
  188. if (cpu->hcall->arg0 != LHCALL_LGUEST_INIT) {
  189. kill_guest(cpu, "hypercall %li before INIT", cpu->hcall->arg0);
  190. return;
  191. }
  192. if (lguest_arch_init_hypercalls(cpu))
  193. kill_guest(cpu, "bad guest page %p", cpu->lg->lguest_data);
  194. /*
  195. * The Guest tells us where we're not to deliver interrupts by putting
  196. * the instruction address into "struct lguest_data".
  197. */
  198. if (get_user(cpu->lg->noirq_iret, &cpu->lg->lguest_data->noirq_iret))
  199. kill_guest(cpu, "bad guest page %p", cpu->lg->lguest_data);
  200. /*
  201. * We write the current time into the Guest's data page once so it can
  202. * set its clock.
  203. */
  204. write_timestamp(cpu);
  205. /* page_tables.c will also do some setup. */
  206. page_table_guest_data_init(cpu);
  207. /*
  208. * This is the one case where the above accesses might have been the
  209. * first write to a Guest page. This may have caused a copy-on-write
  210. * fault, but the old page might be (read-only) in the Guest
  211. * pagetable.
  212. */
  213. guest_pagetable_clear_all(cpu);
  214. }
  215. /*:*/
  216. /*M:013
  217. * If a Guest reads from a page (so creates a mapping) that it has never
  218. * written to, and then the Launcher writes to it (ie. the output of a virtual
  219. * device), the Guest will still see the old page. In practice, this never
  220. * happens: why would the Guest read a page which it has never written to? But
  221. * a similar scenario might one day bite us, so it's worth mentioning.
  222. *
  223. * Note that if we used a shared anonymous mapping in the Launcher instead of
  224. * mapping /dev/zero private, we wouldn't worry about cop-on-write. And we
  225. * need that to switch the Launcher to processes (away from threads) anyway.
  226. :*/
  227. /*H:100
  228. * Hypercalls
  229. *
  230. * Remember from the Guest, hypercalls come in two flavors: normal and
  231. * asynchronous. This file handles both of types.
  232. */
  233. void do_hypercalls(struct lg_cpu *cpu)
  234. {
  235. /* Not initialized yet? This hypercall must do it. */
  236. if (unlikely(!cpu->lg->lguest_data)) {
  237. /* Set up the "struct lguest_data" */
  238. initialize(cpu);
  239. /* Hcall is done. */
  240. cpu->hcall = NULL;
  241. return;
  242. }
  243. /*
  244. * The Guest has initialized.
  245. *
  246. * Look in the hypercall ring for the async hypercalls:
  247. */
  248. do_async_hcalls(cpu);
  249. /*
  250. * If we stopped reading the hypercall ring because the Guest did a
  251. * NOTIFY to the Launcher, we want to return now. Otherwise we do
  252. * the hypercall.
  253. */
  254. if (!cpu->pending.trap) {
  255. do_hcall(cpu, cpu->hcall);
  256. /*
  257. * Tricky point: we reset the hcall pointer to mark the
  258. * hypercall as "done". We use the hcall pointer rather than
  259. * the trap number to indicate a hypercall is pending.
  260. * Normally it doesn't matter: the Guest will run again and
  261. * update the trap number before we come back here.
  262. *
  263. * However, if we are signalled or the Guest sends I/O to the
  264. * Launcher, the run_guest() loop will exit without running the
  265. * Guest. When it comes back it would try to re-run the
  266. * hypercall. Finding that bug sucked.
  267. */
  268. cpu->hcall = NULL;
  269. }
  270. }
  271. /*
  272. * This routine supplies the Guest with time: it's used for wallclock time at
  273. * initial boot and as a rough time source if the TSC isn't available.
  274. */
  275. void write_timestamp(struct lg_cpu *cpu)
  276. {
  277. struct timespec now;
  278. ktime_get_real_ts(&now);
  279. if (copy_to_user(&cpu->lg->lguest_data->time,
  280. &now, sizeof(struct timespec)))
  281. kill_guest(cpu, "Writing timestamp");
  282. }