hypercalls.c 9.1 KB

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