interrupts_and_traps.c 20 KB

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