interrupts_and_traps.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. /*P:800
  2. * Interrupts (traps) are complicated enough to earn their own file.
  3. * There are three classes of interrupts:
  4. *
  5. * 1) Real hardware interrupts which occur while we're running the Guest,
  6. * 2) Interrupts for virtual devices attached to the Guest, and
  7. * 3) Traps and faults from the Guest.
  8. *
  9. * Real hardware interrupts must be delivered to the Host, not the Guest.
  10. * Virtual interrupts must be delivered to the Guest, but we make them look
  11. * just like real hardware would deliver them. Traps from the Guest can be set
  12. * up to go directly back into the Guest, but sometimes the Host wants to see
  13. * them first, so we also have a way of "reflecting" them into the Guest as if
  14. * they had been delivered to it directly.
  15. :*/
  16. #include <linux/uaccess.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/module.h>
  19. #include <linux/sched.h>
  20. #include "lg.h"
  21. /* Allow Guests to use a non-128 (ie. non-Linux) syscall trap. */
  22. static unsigned int syscall_vector = IA32_SYSCALL_VECTOR;
  23. module_param(syscall_vector, uint, 0444);
  24. /* The address of the interrupt handler is split into two bits: */
  25. static unsigned long idt_address(u32 lo, u32 hi)
  26. {
  27. return (lo & 0x0000FFFF) | (hi & 0xFFFF0000);
  28. }
  29. /*
  30. * The "type" of the interrupt handler is a 4 bit field: we only support a
  31. * couple of types.
  32. */
  33. static int idt_type(u32 lo, u32 hi)
  34. {
  35. return (hi >> 8) & 0xF;
  36. }
  37. /* An IDT entry can't be used unless the "present" bit is set. */
  38. static bool idt_present(u32 lo, u32 hi)
  39. {
  40. return (hi & 0x8000);
  41. }
  42. /*
  43. * We need a helper to "push" a value onto the Guest's stack, since that's a
  44. * big part of what delivering an interrupt does.
  45. */
  46. static void push_guest_stack(struct lg_cpu *cpu, unsigned long *gstack, u32 val)
  47. {
  48. /* Stack grows upwards: move stack then write value. */
  49. *gstack -= 4;
  50. lgwrite(cpu, *gstack, u32, val);
  51. }
  52. /*H:210
  53. * The push_guest_interrupt_stack() routine saves Guest state on the stack for
  54. * an interrupt or trap. The mechanics of delivering traps and interrupts to
  55. * the Guest are the same, except some traps have an "error code" which gets
  56. * pushed onto the stack as well: the caller tells us if this is one.
  57. *
  58. * We set up the stack just like the CPU does for a real interrupt, so it's
  59. * identical for the Guest (and the standard "iret" instruction will undo
  60. * it).
  61. */
  62. static void push_guest_interrupt_stack(struct lg_cpu *cpu, bool has_err)
  63. {
  64. unsigned long gstack, origstack;
  65. u32 eflags, ss, irq_enable;
  66. unsigned long virtstack;
  67. /*
  68. * There are two cases for interrupts: one where the Guest is already
  69. * in the kernel, and a more complex one where the Guest is in
  70. * userspace. We check the privilege level to find out.
  71. */
  72. if ((cpu->regs->ss&0x3) != GUEST_PL) {
  73. /*
  74. * The Guest told us their kernel stack with the SET_STACK
  75. * hypercall: both the virtual address and the segment.
  76. */
  77. virtstack = cpu->esp1;
  78. ss = cpu->ss1;
  79. origstack = gstack = guest_pa(cpu, virtstack);
  80. /*
  81. * We push the old stack segment and pointer onto the new
  82. * stack: when the Guest does an "iret" back from the interrupt
  83. * handler the CPU will notice they're dropping privilege
  84. * levels and expect these here.
  85. */
  86. push_guest_stack(cpu, &gstack, cpu->regs->ss);
  87. push_guest_stack(cpu, &gstack, cpu->regs->esp);
  88. } else {
  89. /* We're staying on the same Guest (kernel) stack. */
  90. virtstack = cpu->regs->esp;
  91. ss = cpu->regs->ss;
  92. origstack = gstack = guest_pa(cpu, virtstack);
  93. }
  94. /*
  95. * Remember that we never let the Guest actually disable interrupts, so
  96. * the "Interrupt Flag" bit is always set. We copy that bit from the
  97. * Guest's "irq_enabled" field into the eflags word: we saw the Guest
  98. * copy it back in "lguest_iret".
  99. */
  100. eflags = cpu->regs->eflags;
  101. if (get_user(irq_enable, &cpu->lg->lguest_data->irq_enabled) == 0
  102. && !(irq_enable & X86_EFLAGS_IF))
  103. eflags &= ~X86_EFLAGS_IF;
  104. /*
  105. * An interrupt is expected to push three things on the stack: the old
  106. * "eflags" word, the old code segment, and the old instruction
  107. * pointer.
  108. */
  109. push_guest_stack(cpu, &gstack, eflags);
  110. push_guest_stack(cpu, &gstack, cpu->regs->cs);
  111. push_guest_stack(cpu, &gstack, cpu->regs->eip);
  112. /* For the six traps which supply an error code, we push that, too. */
  113. if (has_err)
  114. push_guest_stack(cpu, &gstack, cpu->regs->errcode);
  115. /* Adjust the stack pointer and stack segment. */
  116. cpu->regs->ss = ss;
  117. cpu->regs->esp = virtstack + (gstack - origstack);
  118. }
  119. /*
  120. * This actually makes the Guest start executing the given interrupt/trap
  121. * handler.
  122. *
  123. * "lo" and "hi" are the two parts of the Interrupt Descriptor Table for this
  124. * interrupt or trap. It's split into two parts for traditional reasons: gcc
  125. * on i386 used to be frightened by 64 bit numbers.
  126. */
  127. static void guest_run_interrupt(struct lg_cpu *cpu, u32 lo, u32 hi)
  128. {
  129. /* If we're already in the kernel, we don't change stacks. */
  130. if ((cpu->regs->ss&0x3) != GUEST_PL)
  131. cpu->regs->ss = cpu->esp1;
  132. /*
  133. * Set the code segment and the address to execute.
  134. */
  135. cpu->regs->cs = (__KERNEL_CS|GUEST_PL);
  136. cpu->regs->eip = idt_address(lo, hi);
  137. /*
  138. * Trapping always clears these flags:
  139. * TF: Trap flag
  140. * VM: Virtual 8086 mode
  141. * RF: Resume
  142. * NT: Nested task.
  143. */
  144. cpu->regs->eflags &=
  145. ~(X86_EFLAGS_TF|X86_EFLAGS_VM|X86_EFLAGS_RF|X86_EFLAGS_NT);
  146. /*
  147. * There are two kinds of interrupt handlers: 0xE is an "interrupt
  148. * gate" which expects interrupts to be disabled on entry.
  149. */
  150. if (idt_type(lo, hi) == 0xE)
  151. if (put_user(0, &cpu->lg->lguest_data->irq_enabled))
  152. kill_guest(cpu, "Disabling interrupts");
  153. }
  154. /* This restores the eflags word which was pushed on the stack by a trap */
  155. static void restore_eflags(struct lg_cpu *cpu)
  156. {
  157. /* This is the physical address of the stack. */
  158. unsigned long stack_pa = guest_pa(cpu, cpu->regs->esp);
  159. /*
  160. * Stack looks like this:
  161. * Address Contents
  162. * esp EIP
  163. * esp + 4 CS
  164. * esp + 8 EFLAGS
  165. */
  166. cpu->regs->eflags = lgread(cpu, stack_pa + 8, u32);
  167. cpu->regs->eflags &=
  168. ~(X86_EFLAGS_TF|X86_EFLAGS_VM|X86_EFLAGS_RF|X86_EFLAGS_NT);
  169. }
  170. /*H:205
  171. * Virtual Interrupts.
  172. *
  173. * interrupt_pending() returns the first pending interrupt which isn't blocked
  174. * by the Guest. It is called before every entry to the Guest, and just before
  175. * we go to sleep when the Guest has halted itself.
  176. */
  177. unsigned int interrupt_pending(struct lg_cpu *cpu, bool *more)
  178. {
  179. unsigned int irq;
  180. DECLARE_BITMAP(blk, LGUEST_IRQS);
  181. /* If the Guest hasn't even initialized yet, we can do nothing. */
  182. if (!cpu->lg->lguest_data)
  183. return LGUEST_IRQS;
  184. /*
  185. * Take our "irqs_pending" array and remove any interrupts the Guest
  186. * wants blocked: the result ends up in "blk".
  187. */
  188. if (copy_from_user(&blk, cpu->lg->lguest_data->blocked_interrupts,
  189. sizeof(blk)))
  190. return LGUEST_IRQS;
  191. bitmap_andnot(blk, cpu->irqs_pending, blk, LGUEST_IRQS);
  192. /* Find the first interrupt. */
  193. irq = find_first_bit(blk, LGUEST_IRQS);
  194. *more = find_next_bit(blk, LGUEST_IRQS, irq+1);
  195. return irq;
  196. }
  197. /*
  198. * This actually diverts the Guest to running an interrupt handler, once an
  199. * interrupt has been identified by interrupt_pending().
  200. */
  201. void try_deliver_interrupt(struct lg_cpu *cpu, unsigned int irq, bool more)
  202. {
  203. struct desc_struct *idt;
  204. BUG_ON(irq >= LGUEST_IRQS);
  205. /* If they're halted, interrupts restart them. */
  206. if (cpu->halted) {
  207. /* Re-enable interrupts. */
  208. if (put_user(X86_EFLAGS_IF, &cpu->lg->lguest_data->irq_enabled))
  209. kill_guest(cpu, "Re-enabling interrupts");
  210. cpu->halted = 0;
  211. } else {
  212. /* Otherwise we check if they have interrupts disabled. */
  213. u32 irq_enabled;
  214. if (get_user(irq_enabled, &cpu->lg->lguest_data->irq_enabled))
  215. irq_enabled = 0;
  216. if (!irq_enabled) {
  217. /* Make sure they know an IRQ is pending. */
  218. put_user(X86_EFLAGS_IF,
  219. &cpu->lg->lguest_data->irq_pending);
  220. return;
  221. }
  222. }
  223. /*
  224. * Look at the IDT entry the Guest gave us for this interrupt. The
  225. * first 32 (FIRST_EXTERNAL_VECTOR) entries are for traps, so we skip
  226. * over them.
  227. */
  228. idt = &cpu->arch.idt[FIRST_EXTERNAL_VECTOR+irq];
  229. /* If they don't have a handler (yet?), we just ignore it */
  230. if (idt_present(idt->a, idt->b)) {
  231. /* OK, mark it no longer pending and deliver it. */
  232. clear_bit(irq, cpu->irqs_pending);
  233. /*
  234. * They may be about to iret, where they asked us never to
  235. * deliver interrupts. In this case, we can emulate that iret
  236. * then immediately deliver the interrupt. This is basically
  237. * a noop: the iret would pop the interrupt frame and restore
  238. * eflags, and then we'd set it up again. So just restore the
  239. * eflags word and jump straight to the handler in this case.
  240. *
  241. * Denys Vlasenko points out that this isn't quite right: if
  242. * the iret was returning to userspace, then that interrupt
  243. * would reset the stack pointer (which the Guest told us
  244. * about via LHCALL_SET_STACK). But unless the Guest is being
  245. * *really* weird, that will be the same as the current stack
  246. * anyway.
  247. */
  248. if (cpu->regs->eip == cpu->lg->noirq_iret) {
  249. restore_eflags(cpu);
  250. } else {
  251. /*
  252. * set_guest_interrupt() takes a flag to say whether
  253. * this interrupt pushes an error code onto the stack
  254. * as well: virtual interrupts never do.
  255. */
  256. push_guest_interrupt_stack(cpu, false);
  257. }
  258. /* Actually make Guest cpu jump to handler. */
  259. guest_run_interrupt(cpu, idt->a, idt->b);
  260. }
  261. /*
  262. * Every time we deliver an interrupt, we update the timestamp in the
  263. * Guest's lguest_data struct. It would be better for the Guest if we
  264. * did this more often, but it can actually be quite slow: doing it
  265. * here is a compromise which means at least it gets updated every
  266. * timer interrupt.
  267. */
  268. write_timestamp(cpu);
  269. /*
  270. * If there are no other interrupts we want to deliver, clear
  271. * the pending flag.
  272. */
  273. if (!more)
  274. put_user(0, &cpu->lg->lguest_data->irq_pending);
  275. }
  276. /* And this is the routine when we want to set an interrupt for the Guest. */
  277. void set_interrupt(struct lg_cpu *cpu, unsigned int irq)
  278. {
  279. /*
  280. * Next time the Guest runs, the core code will see if it can deliver
  281. * this interrupt.
  282. */
  283. set_bit(irq, cpu->irqs_pending);
  284. /*
  285. * Make sure it sees it; it might be asleep (eg. halted), or running
  286. * the Guest right now, in which case kick_process() will knock it out.
  287. */
  288. if (!wake_up_process(cpu->tsk))
  289. kick_process(cpu->tsk);
  290. }
  291. /*:*/
  292. /*
  293. * Linux uses trap 128 for system calls. Plan9 uses 64, and Ron Minnich sent
  294. * me a patch, so we support that too. It'd be a big step for lguest if half
  295. * the Plan 9 user base were to start using it.
  296. *
  297. * Actually now I think of it, it's possible that Ron *is* half the Plan 9
  298. * userbase. Oh well.
  299. */
  300. static bool could_be_syscall(unsigned int num)
  301. {
  302. /* Normal Linux IA32_SYSCALL_VECTOR or reserved vector? */
  303. return num == IA32_SYSCALL_VECTOR || num == syscall_vector;
  304. }
  305. /* The syscall vector it wants must be unused by Host. */
  306. bool check_syscall_vector(struct lguest *lg)
  307. {
  308. u32 vector;
  309. if (get_user(vector, &lg->lguest_data->syscall_vec))
  310. return false;
  311. return could_be_syscall(vector);
  312. }
  313. int init_interrupts(void)
  314. {
  315. /* If they want some strange system call vector, reserve it now */
  316. if (syscall_vector != IA32_SYSCALL_VECTOR) {
  317. if (test_bit(syscall_vector, used_vectors) ||
  318. vector_used_by_percpu_irq(syscall_vector)) {
  319. printk(KERN_ERR "lg: couldn't reserve syscall %u\n",
  320. syscall_vector);
  321. return -EBUSY;
  322. }
  323. set_bit(syscall_vector, used_vectors);
  324. }
  325. return 0;
  326. }
  327. void free_interrupts(void)
  328. {
  329. if (syscall_vector != IA32_SYSCALL_VECTOR)
  330. clear_bit(syscall_vector, used_vectors);
  331. }
  332. /*H:220
  333. * Now we've got the routines to deliver interrupts, delivering traps like
  334. * page fault is easy. The only trick is that Intel decided that some traps
  335. * should have error codes:
  336. */
  337. static bool has_err(unsigned int trap)
  338. {
  339. return (trap == 8 || (trap >= 10 && trap <= 14) || trap == 17);
  340. }
  341. /* deliver_trap() returns true if it could deliver the trap. */
  342. bool deliver_trap(struct lg_cpu *cpu, unsigned int num)
  343. {
  344. /*
  345. * Trap numbers are always 8 bit, but we set an impossible trap number
  346. * for traps inside the Switcher, so check that here.
  347. */
  348. if (num >= ARRAY_SIZE(cpu->arch.idt))
  349. return false;
  350. /*
  351. * Early on the Guest hasn't set the IDT entries (or maybe it put a
  352. * bogus one in): if we fail here, the Guest will be killed.
  353. */
  354. if (!idt_present(cpu->arch.idt[num].a, cpu->arch.idt[num].b))
  355. return false;
  356. push_guest_interrupt_stack(cpu, has_err(num));
  357. guest_run_interrupt(cpu, cpu->arch.idt[num].a,
  358. cpu->arch.idt[num].b);
  359. return true;
  360. }
  361. /*H:250
  362. * Here's the hard part: returning to the Host every time a trap happens
  363. * and then calling deliver_trap() and re-entering the Guest is slow.
  364. * Particularly because Guest userspace system calls are traps (usually trap
  365. * 128).
  366. *
  367. * So we'd like to set up the IDT to tell the CPU to deliver traps directly
  368. * into the Guest. This is possible, but the complexities cause the size of
  369. * this file to double! However, 150 lines of code is worth writing for taking
  370. * system calls down from 1750ns to 270ns. Plus, if lguest didn't do it, all
  371. * the other hypervisors would beat it up at lunchtime.
  372. *
  373. * This routine indicates if a particular trap number could be delivered
  374. * directly.
  375. */
  376. static bool direct_trap(unsigned int num)
  377. {
  378. /*
  379. * Hardware interrupts don't go to the Guest at all (except system
  380. * call).
  381. */
  382. if (num >= FIRST_EXTERNAL_VECTOR && !could_be_syscall(num))
  383. return false;
  384. /*
  385. * The Host needs to see page faults (for shadow paging and to save the
  386. * fault address), general protection faults (in/out emulation) and
  387. * device not available (TS handling) and of course, the hypercall trap.
  388. */
  389. return num != 14 && num != 13 && num != 7 && num != LGUEST_TRAP_ENTRY;
  390. }
  391. /*:*/
  392. /*M:005
  393. * The Guest has the ability to turn its interrupt gates into trap gates,
  394. * if it is careful. The Host will let trap gates can go directly to the
  395. * Guest, but the Guest needs the interrupts atomically disabled for an
  396. * interrupt gate. The Host could provide a mechanism to register more
  397. * "no-interrupt" regions, and the Guest could point the trap gate at
  398. * instructions within that region, where it can safely disable interrupts.
  399. */
  400. /*M:006
  401. * The Guests do not use the sysenter (fast system call) instruction,
  402. * because it's hardcoded to enter privilege level 0 and so can't go direct.
  403. * It's about twice as fast as the older "int 0x80" system call, so it might
  404. * still be worthwhile to handle it in the Switcher and lcall down to the
  405. * Guest. The sysenter semantics are hairy tho: search for that keyword in
  406. * entry.S
  407. :*/
  408. /*H:260
  409. * When we make traps go directly into the Guest, we need to make sure
  410. * the kernel stack is valid (ie. mapped in the page tables). Otherwise, the
  411. * CPU trying to deliver the trap will fault while trying to push the interrupt
  412. * words on the stack: this is called a double fault, and it forces us to kill
  413. * the Guest.
  414. *
  415. * Which is deeply unfair, because (literally!) it wasn't the Guests' fault.
  416. */
  417. void pin_stack_pages(struct lg_cpu *cpu)
  418. {
  419. unsigned int i;
  420. /*
  421. * Depending on the CONFIG_4KSTACKS option, the Guest can have one or
  422. * two pages of stack space.
  423. */
  424. for (i = 0; i < cpu->lg->stack_pages; i++)
  425. /*
  426. * The stack grows *upwards*, so the address we're given is the
  427. * start of the page after the kernel stack. Subtract one to
  428. * get back onto the first stack page, and keep subtracting to
  429. * get to the rest of the stack pages.
  430. */
  431. pin_page(cpu, cpu->esp1 - 1 - i * PAGE_SIZE);
  432. }
  433. /*
  434. * Direct traps also mean that we need to know whenever the Guest wants to use
  435. * a different kernel stack, so we can change the guest TSS to use that
  436. * stack. The TSS entries expect a virtual address, so unlike most addresses
  437. * the Guest gives us, the "esp" (stack pointer) value here is virtual, not
  438. * physical.
  439. *
  440. * In Linux each process has its own kernel stack, so this happens a lot: we
  441. * change stacks on each context switch.
  442. */
  443. void guest_set_stack(struct lg_cpu *cpu, u32 seg, u32 esp, unsigned int pages)
  444. {
  445. /*
  446. * You're not allowed a stack segment with privilege level 0: bad Guest!
  447. */
  448. if ((seg & 0x3) != GUEST_PL)
  449. kill_guest(cpu, "bad stack segment %i", seg);
  450. /* We only expect one or two stack pages. */
  451. if (pages > 2)
  452. kill_guest(cpu, "bad stack pages %u", pages);
  453. /* Save where the stack is, and how many pages */
  454. cpu->ss1 = seg;
  455. cpu->esp1 = esp;
  456. cpu->lg->stack_pages = pages;
  457. /* Make sure the new stack pages are mapped */
  458. pin_stack_pages(cpu);
  459. }
  460. /*
  461. * All this reference to mapping stacks leads us neatly into the other complex
  462. * part of the Host: page table handling.
  463. */
  464. /*H:235
  465. * This is the routine which actually checks the Guest's IDT entry and
  466. * transfers it into the entry in "struct lguest":
  467. */
  468. static void set_trap(struct lg_cpu *cpu, struct desc_struct *trap,
  469. unsigned int num, u32 lo, u32 hi)
  470. {
  471. u8 type = idt_type(lo, hi);
  472. /* We zero-out a not-present entry */
  473. if (!idt_present(lo, hi)) {
  474. trap->a = trap->b = 0;
  475. return;
  476. }
  477. /* We only support interrupt and trap gates. */
  478. if (type != 0xE && type != 0xF)
  479. kill_guest(cpu, "bad IDT type %i", type);
  480. /*
  481. * We only copy the handler address, present bit, privilege level and
  482. * type. The privilege level controls where the trap can be triggered
  483. * manually with an "int" instruction. This is usually GUEST_PL,
  484. * except for system calls which userspace can use.
  485. */
  486. trap->a = ((__KERNEL_CS|GUEST_PL)<<16) | (lo&0x0000FFFF);
  487. trap->b = (hi&0xFFFFEF00);
  488. }
  489. /*H:230
  490. * While we're here, dealing with delivering traps and interrupts to the
  491. * Guest, we might as well complete the picture: how the Guest tells us where
  492. * it wants them to go. This would be simple, except making traps fast
  493. * requires some tricks.
  494. *
  495. * We saw the Guest setting Interrupt Descriptor Table (IDT) entries with the
  496. * LHCALL_LOAD_IDT_ENTRY hypercall before: that comes here.
  497. */
  498. void load_guest_idt_entry(struct lg_cpu *cpu, unsigned int num, u32 lo, u32 hi)
  499. {
  500. /*
  501. * Guest never handles: NMI, doublefault, spurious interrupt or
  502. * hypercall. We ignore when it tries to set them.
  503. */
  504. if (num == 2 || num == 8 || num == 15 || num == LGUEST_TRAP_ENTRY)
  505. return;
  506. /*
  507. * Mark the IDT as changed: next time the Guest runs we'll know we have
  508. * to copy this again.
  509. */
  510. cpu->changed |= CHANGED_IDT;
  511. /* Check that the Guest doesn't try to step outside the bounds. */
  512. if (num >= ARRAY_SIZE(cpu->arch.idt))
  513. kill_guest(cpu, "Setting idt entry %u", num);
  514. else
  515. set_trap(cpu, &cpu->arch.idt[num], num, lo, hi);
  516. }
  517. /*
  518. * The default entry for each interrupt points into the Switcher routines which
  519. * simply return to the Host. The run_guest() loop will then call
  520. * deliver_trap() to bounce it back into the Guest.
  521. */
  522. static void default_idt_entry(struct desc_struct *idt,
  523. int trap,
  524. const unsigned long handler,
  525. const struct desc_struct *base)
  526. {
  527. /* A present interrupt gate. */
  528. u32 flags = 0x8e00;
  529. /*
  530. * Set the privilege level on the entry for the hypercall: this allows
  531. * the Guest to use the "int" instruction to trigger it.
  532. */
  533. if (trap == LGUEST_TRAP_ENTRY)
  534. flags |= (GUEST_PL << 13);
  535. else if (base)
  536. /*
  537. * Copy privilege level from what Guest asked for. This allows
  538. * debug (int 3) traps from Guest userspace, for example.
  539. */
  540. flags |= (base->b & 0x6000);
  541. /* Now pack it into the IDT entry in its weird format. */
  542. idt->a = (LGUEST_CS<<16) | (handler&0x0000FFFF);
  543. idt->b = (handler&0xFFFF0000) | flags;
  544. }
  545. /* When the Guest first starts, we put default entries into the IDT. */
  546. void setup_default_idt_entries(struct lguest_ro_state *state,
  547. const unsigned long *def)
  548. {
  549. unsigned int i;
  550. for (i = 0; i < ARRAY_SIZE(state->guest_idt); i++)
  551. default_idt_entry(&state->guest_idt[i], i, def[i], NULL);
  552. }
  553. /*H:240
  554. * We don't use the IDT entries in the "struct lguest" directly, instead
  555. * we copy them into the IDT which we've set up for Guests on this CPU, just
  556. * before we run the Guest. This routine does that copy.
  557. */
  558. void copy_traps(const struct lg_cpu *cpu, struct desc_struct *idt,
  559. const unsigned long *def)
  560. {
  561. unsigned int i;
  562. /*
  563. * We can simply copy the direct traps, otherwise we use the default
  564. * ones in the Switcher: they will return to the Host.
  565. */
  566. for (i = 0; i < ARRAY_SIZE(cpu->arch.idt); i++) {
  567. const struct desc_struct *gidt = &cpu->arch.idt[i];
  568. /* If no Guest can ever override this trap, leave it alone. */
  569. if (!direct_trap(i))
  570. continue;
  571. /*
  572. * Only trap gates (type 15) can go direct to the Guest.
  573. * Interrupt gates (type 14) disable interrupts as they are
  574. * entered, which we never let the Guest do. Not present
  575. * entries (type 0x0) also can't go direct, of course.
  576. *
  577. * If it can't go direct, we still need to copy the priv. level:
  578. * they might want to give userspace access to a software
  579. * interrupt.
  580. */
  581. if (idt_type(gidt->a, gidt->b) == 0xF)
  582. idt[i] = *gidt;
  583. else
  584. default_idt_entry(&idt[i], i, def[i], gidt);
  585. }
  586. }
  587. /*H:200
  588. * The Guest Clock.
  589. *
  590. * There are two sources of virtual interrupts. We saw one in lguest_user.c:
  591. * the Launcher sending interrupts for virtual devices. The other is the Guest
  592. * timer interrupt.
  593. *
  594. * The Guest uses the LHCALL_SET_CLOCKEVENT hypercall to tell us how long to
  595. * the next timer interrupt (in nanoseconds). We use the high-resolution timer
  596. * infrastructure to set a callback at that time.
  597. *
  598. * 0 means "turn off the clock".
  599. */
  600. void guest_set_clockevent(struct lg_cpu *cpu, unsigned long delta)
  601. {
  602. ktime_t expires;
  603. if (unlikely(delta == 0)) {
  604. /* Clock event device is shutting down. */
  605. hrtimer_cancel(&cpu->hrt);
  606. return;
  607. }
  608. /*
  609. * We use wallclock time here, so the Guest might not be running for
  610. * all the time between now and the timer interrupt it asked for. This
  611. * is almost always the right thing to do.
  612. */
  613. expires = ktime_add_ns(ktime_get_real(), delta);
  614. hrtimer_start(&cpu->hrt, expires, HRTIMER_MODE_ABS);
  615. }
  616. /* This is the function called when the Guest's timer expires. */
  617. static enum hrtimer_restart clockdev_fn(struct hrtimer *timer)
  618. {
  619. struct lg_cpu *cpu = container_of(timer, struct lg_cpu, hrt);
  620. /* Remember the first interrupt is the timer interrupt. */
  621. set_interrupt(cpu, 0);
  622. return HRTIMER_NORESTART;
  623. }
  624. /* This sets up the timer for this Guest. */
  625. void init_clockdev(struct lg_cpu *cpu)
  626. {
  627. hrtimer_init(&cpu->hrt, CLOCK_REALTIME, HRTIMER_MODE_ABS);
  628. cpu->hrt.function = clockdev_fn;
  629. }