interrupts_and_traps.c 20 KB

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