interrupts_and_traps.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /*P:800 Interrupts (traps) are complicated enough to earn their own file.
  2. * There are three classes of interrupts:
  3. *
  4. * 1) Real hardware interrupts which occur while we're running the Guest,
  5. * 2) Interrupts for virtual devices attached to the Guest, and
  6. * 3) Traps and faults from the Guest.
  7. *
  8. * Real hardware interrupts must be delivered to the Host, not the Guest.
  9. * Virtual interrupts must be delivered to the Guest, but we make them look
  10. * just like real hardware would deliver them. Traps from the Guest can be set
  11. * up to go directly back into the Guest, but sometimes the Host wants to see
  12. * them first, so we also have a way of "reflecting" them into the Guest as if
  13. * they had been delivered to it directly. :*/
  14. #include <linux/uaccess.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/module.h>
  17. #include "lg.h"
  18. /* Allow Guests to use a non-128 (ie. non-Linux) syscall trap. */
  19. static unsigned int syscall_vector = SYSCALL_VECTOR;
  20. module_param(syscall_vector, uint, 0444);
  21. /* The address of the interrupt handler is split into two bits: */
  22. static unsigned long idt_address(u32 lo, u32 hi)
  23. {
  24. return (lo & 0x0000FFFF) | (hi & 0xFFFF0000);
  25. }
  26. /* The "type" of the interrupt handler is a 4 bit field: we only support a
  27. * couple of types. */
  28. static int idt_type(u32 lo, u32 hi)
  29. {
  30. return (hi >> 8) & 0xF;
  31. }
  32. /* An IDT entry can't be used unless the "present" bit is set. */
  33. static int idt_present(u32 lo, u32 hi)
  34. {
  35. return (hi & 0x8000);
  36. }
  37. /* We need a helper to "push" a value onto the Guest's stack, since that's a
  38. * big part of what delivering an interrupt does. */
  39. static void push_guest_stack(struct lguest *lg, unsigned long *gstack, u32 val)
  40. {
  41. /* Stack grows upwards: move stack then write value. */
  42. *gstack -= 4;
  43. lgwrite(lg, *gstack, u32, val);
  44. }
  45. /*H:210 The set_guest_interrupt() routine actually delivers the interrupt or
  46. * trap. The mechanics of delivering traps and interrupts to the Guest are the
  47. * same, except some traps have an "error code" which gets pushed onto the
  48. * stack as well: the caller tells us if this is one.
  49. *
  50. * "lo" and "hi" are the two parts of the Interrupt Descriptor Table for this
  51. * interrupt or trap. It's split into two parts for traditional reasons: gcc
  52. * on i386 used to be frightened by 64 bit numbers.
  53. *
  54. * We set up the stack just like the CPU does for a real interrupt, so it's
  55. * identical for the Guest (and the standard "iret" instruction will undo
  56. * it). */
  57. static void set_guest_interrupt(struct lguest *lg, u32 lo, u32 hi, int has_err)
  58. {
  59. unsigned long gstack, origstack;
  60. u32 eflags, ss, irq_enable;
  61. unsigned long virtstack;
  62. /* There are two cases for interrupts: one where the Guest is already
  63. * in the kernel, and a more complex one where the Guest is in
  64. * userspace. We check the privilege level to find out. */
  65. if ((lg->regs->ss&0x3) != GUEST_PL) {
  66. /* The Guest told us their kernel stack with the SET_STACK
  67. * hypercall: both the virtual address and the segment */
  68. virtstack = lg->esp1;
  69. ss = lg->ss1;
  70. origstack = gstack = guest_pa(lg, virtstack);
  71. /* We push the old stack segment and pointer onto the new
  72. * stack: when the Guest does an "iret" back from the interrupt
  73. * handler the CPU will notice they're dropping privilege
  74. * levels and expect these here. */
  75. push_guest_stack(lg, &gstack, lg->regs->ss);
  76. push_guest_stack(lg, &gstack, lg->regs->esp);
  77. } else {
  78. /* We're staying on the same Guest (kernel) stack. */
  79. virtstack = lg->regs->esp;
  80. ss = lg->regs->ss;
  81. origstack = gstack = guest_pa(lg, virtstack);
  82. }
  83. /* Remember that we never let the Guest actually disable interrupts, so
  84. * the "Interrupt Flag" bit is always set. We copy that bit from the
  85. * Guest's "irq_enabled" field into the eflags word: the Guest copies
  86. * it back in "lguest_iret". */
  87. eflags = lg->regs->eflags;
  88. if (get_user(irq_enable, &lg->lguest_data->irq_enabled) == 0
  89. && !(irq_enable & X86_EFLAGS_IF))
  90. eflags &= ~X86_EFLAGS_IF;
  91. /* An interrupt is expected to push three things on the stack: the old
  92. * "eflags" word, the old code segment, and the old instruction
  93. * pointer. */
  94. push_guest_stack(lg, &gstack, eflags);
  95. push_guest_stack(lg, &gstack, lg->regs->cs);
  96. push_guest_stack(lg, &gstack, lg->regs->eip);
  97. /* For the six traps which supply an error code, we push that, too. */
  98. if (has_err)
  99. push_guest_stack(lg, &gstack, lg->regs->errcode);
  100. /* Now we've pushed all the old state, we change the stack, the code
  101. * segment and the address to execute. */
  102. lg->regs->ss = ss;
  103. lg->regs->esp = virtstack + (gstack - origstack);
  104. lg->regs->cs = (__KERNEL_CS|GUEST_PL);
  105. lg->regs->eip = idt_address(lo, hi);
  106. /* There are two kinds of interrupt handlers: 0xE is an "interrupt
  107. * gate" which expects interrupts to be disabled on entry. */
  108. if (idt_type(lo, hi) == 0xE)
  109. if (put_user(0, &lg->lguest_data->irq_enabled))
  110. kill_guest(lg, "Disabling interrupts");
  111. }
  112. /*H:200
  113. * Virtual Interrupts.
  114. *
  115. * maybe_do_interrupt() gets called before every entry to the Guest, to see if
  116. * we should divert the Guest to running an interrupt handler. */
  117. void maybe_do_interrupt(struct lguest *lg)
  118. {
  119. unsigned int irq;
  120. DECLARE_BITMAP(blk, LGUEST_IRQS);
  121. struct desc_struct *idt;
  122. /* If the Guest hasn't even initialized yet, we can do nothing. */
  123. if (!lg->lguest_data)
  124. return;
  125. /* Take our "irqs_pending" array and remove any interrupts the Guest
  126. * wants blocked: the result ends up in "blk". */
  127. if (copy_from_user(&blk, lg->lguest_data->blocked_interrupts,
  128. sizeof(blk)))
  129. return;
  130. bitmap_andnot(blk, lg->irqs_pending, blk, LGUEST_IRQS);
  131. /* Find the first interrupt. */
  132. irq = find_first_bit(blk, LGUEST_IRQS);
  133. /* None? Nothing to do */
  134. if (irq >= LGUEST_IRQS)
  135. return;
  136. /* They may be in the middle of an iret, where they asked us never to
  137. * deliver interrupts. */
  138. if (lg->regs->eip >= lg->noirq_start && lg->regs->eip < lg->noirq_end)
  139. return;
  140. /* If they're halted, interrupts restart them. */
  141. if (lg->halted) {
  142. /* Re-enable interrupts. */
  143. if (put_user(X86_EFLAGS_IF, &lg->lguest_data->irq_enabled))
  144. kill_guest(lg, "Re-enabling interrupts");
  145. lg->halted = 0;
  146. } else {
  147. /* Otherwise we check if they have interrupts disabled. */
  148. u32 irq_enabled;
  149. if (get_user(irq_enabled, &lg->lguest_data->irq_enabled))
  150. irq_enabled = 0;
  151. if (!irq_enabled)
  152. return;
  153. }
  154. /* Look at the IDT entry the Guest gave us for this interrupt. The
  155. * first 32 (FIRST_EXTERNAL_VECTOR) entries are for traps, so we skip
  156. * over them. */
  157. idt = &lg->arch.idt[FIRST_EXTERNAL_VECTOR+irq];
  158. /* If they don't have a handler (yet?), we just ignore it */
  159. if (idt_present(idt->a, idt->b)) {
  160. /* OK, mark it no longer pending and deliver it. */
  161. clear_bit(irq, lg->irqs_pending);
  162. /* set_guest_interrupt() takes the interrupt descriptor and a
  163. * flag to say whether this interrupt pushes an error code onto
  164. * the stack as well: virtual interrupts never do. */
  165. set_guest_interrupt(lg, idt->a, idt->b, 0);
  166. }
  167. /* Every time we deliver an interrupt, we update the timestamp in the
  168. * Guest's lguest_data struct. It would be better for the Guest if we
  169. * did this more often, but it can actually be quite slow: doing it
  170. * here is a compromise which means at least it gets updated every
  171. * timer interrupt. */
  172. write_timestamp(lg);
  173. }
  174. /*:*/
  175. /* Linux uses trap 128 for system calls. Plan9 uses 64, and Ron Minnich sent
  176. * me a patch, so we support that too. It'd be a big step for lguest if half
  177. * the Plan 9 user base were to start using it.
  178. *
  179. * Actually now I think of it, it's possible that Ron *is* half the Plan 9
  180. * userbase. Oh well. */
  181. static bool could_be_syscall(unsigned int num)
  182. {
  183. /* Normal Linux SYSCALL_VECTOR or reserved vector? */
  184. return num == SYSCALL_VECTOR || num == syscall_vector;
  185. }
  186. /* The syscall vector it wants must be unused by Host. */
  187. bool check_syscall_vector(struct lguest *lg)
  188. {
  189. u32 vector;
  190. if (get_user(vector, &lg->lguest_data->syscall_vec))
  191. return false;
  192. return could_be_syscall(vector);
  193. }
  194. int init_interrupts(void)
  195. {
  196. /* If they want some strange system call vector, reserve it now */
  197. if (syscall_vector != SYSCALL_VECTOR
  198. && test_and_set_bit(syscall_vector, used_vectors)) {
  199. printk("lg: couldn't reserve syscall %u\n", syscall_vector);
  200. return -EBUSY;
  201. }
  202. return 0;
  203. }
  204. void free_interrupts(void)
  205. {
  206. if (syscall_vector != SYSCALL_VECTOR)
  207. clear_bit(syscall_vector, used_vectors);
  208. }
  209. /*H:220 Now we've got the routines to deliver interrupts, delivering traps
  210. * like page fault is easy. The only trick is that Intel decided that some
  211. * traps should have error codes: */
  212. static int has_err(unsigned int trap)
  213. {
  214. return (trap == 8 || (trap >= 10 && trap <= 14) || trap == 17);
  215. }
  216. /* deliver_trap() returns true if it could deliver the trap. */
  217. int deliver_trap(struct lguest *lg, unsigned int num)
  218. {
  219. /* Trap numbers are always 8 bit, but we set an impossible trap number
  220. * for traps inside the Switcher, so check that here. */
  221. if (num >= ARRAY_SIZE(lg->arch.idt))
  222. return 0;
  223. /* Early on the Guest hasn't set the IDT entries (or maybe it put a
  224. * bogus one in): if we fail here, the Guest will be killed. */
  225. if (!idt_present(lg->arch.idt[num].a, lg->arch.idt[num].b))
  226. return 0;
  227. set_guest_interrupt(lg, lg->arch.idt[num].a, lg->arch.idt[num].b, has_err(num));
  228. return 1;
  229. }
  230. /*H:250 Here's the hard part: returning to the Host every time a trap happens
  231. * and then calling deliver_trap() and re-entering the Guest is slow.
  232. * Particularly because Guest userspace system calls are traps (trap 128).
  233. *
  234. * So we'd like to set up the IDT to tell the CPU to deliver traps directly
  235. * into the Guest. This is possible, but the complexities cause the size of
  236. * this file to double! However, 150 lines of code is worth writing for taking
  237. * system calls down from 1750ns to 270ns. Plus, if lguest didn't do it, all
  238. * the other hypervisors would tease it.
  239. *
  240. * This routine indicates if a particular trap number could be delivered
  241. * directly. */
  242. static int direct_trap(unsigned int num)
  243. {
  244. /* Hardware interrupts don't go to the Guest at all (except system
  245. * call). */
  246. if (num >= FIRST_EXTERNAL_VECTOR && !could_be_syscall(num))
  247. return 0;
  248. /* The Host needs to see page faults (for shadow paging and to save the
  249. * fault address), general protection faults (in/out emulation) and
  250. * device not available (TS handling), and of course, the hypercall
  251. * trap. */
  252. return num != 14 && num != 13 && num != 7 && num != LGUEST_TRAP_ENTRY;
  253. }
  254. /*:*/
  255. /*M:005 The Guest has the ability to turn its interrupt gates into trap gates,
  256. * if it is careful. The Host will let trap gates can go directly to the
  257. * Guest, but the Guest needs the interrupts atomically disabled for an
  258. * interrupt gate. It can do this by pointing the trap gate at instructions
  259. * within noirq_start and noirq_end, where it can safely disable interrupts. */
  260. /*M:006 The Guests do not use the sysenter (fast system call) instruction,
  261. * because it's hardcoded to enter privilege level 0 and so can't go direct.
  262. * It's about twice as fast as the older "int 0x80" system call, so it might
  263. * still be worthwhile to handle it in the Switcher and lcall down to the
  264. * Guest. The sysenter semantics are hairy tho: search for that keyword in
  265. * entry.S :*/
  266. /*H:260 When we make traps go directly into the Guest, we need to make sure
  267. * the kernel stack is valid (ie. mapped in the page tables). Otherwise, the
  268. * CPU trying to deliver the trap will fault while trying to push the interrupt
  269. * words on the stack: this is called a double fault, and it forces us to kill
  270. * the Guest.
  271. *
  272. * Which is deeply unfair, because (literally!) it wasn't the Guests' fault. */
  273. void pin_stack_pages(struct lguest *lg)
  274. {
  275. unsigned int i;
  276. /* Depending on the CONFIG_4KSTACKS option, the Guest can have one or
  277. * two pages of stack space. */
  278. for (i = 0; i < lg->stack_pages; i++)
  279. /* The stack grows *upwards*, so the address we're given is the
  280. * start of the page after the kernel stack. Subtract one to
  281. * get back onto the first stack page, and keep subtracting to
  282. * get to the rest of the stack pages. */
  283. pin_page(lg, lg->esp1 - 1 - i * PAGE_SIZE);
  284. }
  285. /* Direct traps also mean that we need to know whenever the Guest wants to use
  286. * a different kernel stack, so we can change the IDT entries to use that
  287. * stack. The IDT entries expect a virtual address, so unlike most addresses
  288. * the Guest gives us, the "esp" (stack pointer) value here is virtual, not
  289. * physical.
  290. *
  291. * In Linux each process has its own kernel stack, so this happens a lot: we
  292. * change stacks on each context switch. */
  293. void guest_set_stack(struct lguest *lg, u32 seg, u32 esp, unsigned int pages)
  294. {
  295. /* You are not allowd have a stack segment with privilege level 0: bad
  296. * Guest! */
  297. if ((seg & 0x3) != GUEST_PL)
  298. kill_guest(lg, "bad stack segment %i", seg);
  299. /* We only expect one or two stack pages. */
  300. if (pages > 2)
  301. kill_guest(lg, "bad stack pages %u", pages);
  302. /* Save where the stack is, and how many pages */
  303. lg->ss1 = seg;
  304. lg->esp1 = esp;
  305. lg->stack_pages = pages;
  306. /* Make sure the new stack pages are mapped */
  307. pin_stack_pages(lg);
  308. }
  309. /* All this reference to mapping stacks leads us neatly into the other complex
  310. * part of the Host: page table handling. */
  311. /*H:235 This is the routine which actually checks the Guest's IDT entry and
  312. * transfers it into our entry in "struct lguest": */
  313. static void set_trap(struct lguest *lg, struct desc_struct *trap,
  314. unsigned int num, u32 lo, u32 hi)
  315. {
  316. u8 type = idt_type(lo, hi);
  317. /* We zero-out a not-present entry */
  318. if (!idt_present(lo, hi)) {
  319. trap->a = trap->b = 0;
  320. return;
  321. }
  322. /* We only support interrupt and trap gates. */
  323. if (type != 0xE && type != 0xF)
  324. kill_guest(lg, "bad IDT type %i", type);
  325. /* We only copy the handler address, present bit, privilege level and
  326. * type. The privilege level controls where the trap can be triggered
  327. * manually with an "int" instruction. This is usually GUEST_PL,
  328. * except for system calls which userspace can use. */
  329. trap->a = ((__KERNEL_CS|GUEST_PL)<<16) | (lo&0x0000FFFF);
  330. trap->b = (hi&0xFFFFEF00);
  331. }
  332. /*H:230 While we're here, dealing with delivering traps and interrupts to the
  333. * Guest, we might as well complete the picture: how the Guest tells us where
  334. * it wants them to go. This would be simple, except making traps fast
  335. * requires some tricks.
  336. *
  337. * We saw the Guest setting Interrupt Descriptor Table (IDT) entries with the
  338. * LHCALL_LOAD_IDT_ENTRY hypercall before: that comes here. */
  339. void load_guest_idt_entry(struct lguest *lg, unsigned int num, u32 lo, u32 hi)
  340. {
  341. /* Guest never handles: NMI, doublefault, spurious interrupt or
  342. * hypercall. We ignore when it tries to set them. */
  343. if (num == 2 || num == 8 || num == 15 || num == LGUEST_TRAP_ENTRY)
  344. return;
  345. /* Mark the IDT as changed: next time the Guest runs we'll know we have
  346. * to copy this again. */
  347. lg->changed |= CHANGED_IDT;
  348. /* Check that the Guest doesn't try to step outside the bounds. */
  349. if (num >= ARRAY_SIZE(lg->arch.idt))
  350. kill_guest(lg, "Setting idt entry %u", num);
  351. else
  352. set_trap(lg, &lg->arch.idt[num], num, lo, hi);
  353. }
  354. /* The default entry for each interrupt points into the Switcher routines which
  355. * simply return to the Host. The run_guest() loop will then call
  356. * deliver_trap() to bounce it back into the Guest. */
  357. static void default_idt_entry(struct desc_struct *idt,
  358. int trap,
  359. const unsigned long handler)
  360. {
  361. /* A present interrupt gate. */
  362. u32 flags = 0x8e00;
  363. /* Set the privilege level on the entry for the hypercall: this allows
  364. * the Guest to use the "int" instruction to trigger it. */
  365. if (trap == LGUEST_TRAP_ENTRY)
  366. flags |= (GUEST_PL << 13);
  367. /* Now pack it into the IDT entry in its weird format. */
  368. idt->a = (LGUEST_CS<<16) | (handler&0x0000FFFF);
  369. idt->b = (handler&0xFFFF0000) | flags;
  370. }
  371. /* When the Guest first starts, we put default entries into the IDT. */
  372. void setup_default_idt_entries(struct lguest_ro_state *state,
  373. const unsigned long *def)
  374. {
  375. unsigned int i;
  376. for (i = 0; i < ARRAY_SIZE(state->guest_idt); i++)
  377. default_idt_entry(&state->guest_idt[i], i, def[i]);
  378. }
  379. /*H:240 We don't use the IDT entries in the "struct lguest" directly, instead
  380. * we copy them into the IDT which we've set up for Guests on this CPU, just
  381. * before we run the Guest. This routine does that copy. */
  382. void copy_traps(const struct lguest *lg, struct desc_struct *idt,
  383. const unsigned long *def)
  384. {
  385. unsigned int i;
  386. /* We can simply copy the direct traps, otherwise we use the default
  387. * ones in the Switcher: they will return to the Host. */
  388. for (i = 0; i < ARRAY_SIZE(lg->arch.idt); i++) {
  389. /* If no Guest can ever override this trap, leave it alone. */
  390. if (!direct_trap(i))
  391. continue;
  392. /* Only trap gates (type 15) can go direct to the Guest.
  393. * Interrupt gates (type 14) disable interrupts as they are
  394. * entered, which we never let the Guest do. Not present
  395. * entries (type 0x0) also can't go direct, of course. */
  396. if (idt_type(lg->arch.idt[i].a, lg->arch.idt[i].b) == 0xF)
  397. idt[i] = lg->arch.idt[i];
  398. else
  399. /* Reset it to the default. */
  400. default_idt_entry(&idt[i], i, def[i]);
  401. }
  402. }
  403. void guest_set_clockevent(struct lguest *lg, unsigned long delta)
  404. {
  405. ktime_t expires;
  406. if (unlikely(delta == 0)) {
  407. /* Clock event device is shutting down. */
  408. hrtimer_cancel(&lg->hrt);
  409. return;
  410. }
  411. expires = ktime_add_ns(ktime_get_real(), delta);
  412. hrtimer_start(&lg->hrt, expires, HRTIMER_MODE_ABS);
  413. }
  414. static enum hrtimer_restart clockdev_fn(struct hrtimer *timer)
  415. {
  416. struct lguest *lg = container_of(timer, struct lguest, hrt);
  417. set_bit(0, lg->irqs_pending);
  418. if (lg->halted)
  419. wake_up_process(lg->tsk);
  420. return HRTIMER_NORESTART;
  421. }
  422. void init_clockdev(struct lguest *lg)
  423. {
  424. hrtimer_init(&lg->hrt, CLOCK_REALTIME, HRTIMER_MODE_ABS);
  425. lg->hrt.function = clockdev_fn;
  426. }