irq.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include <linux/hardirq.h>
  2. #include <asm/x86_init.h>
  3. #include <xen/interface/xen.h>
  4. #include <xen/interface/sched.h>
  5. #include <xen/interface/vcpu.h>
  6. #include <xen/features.h>
  7. #include <xen/events.h>
  8. #include <asm/xen/hypercall.h>
  9. #include <asm/xen/hypervisor.h>
  10. #include "xen-ops.h"
  11. /*
  12. * Force a proper event-channel callback from Xen after clearing the
  13. * callback mask. We do this in a very simple manner, by making a call
  14. * down into Xen. The pending flag will be checked by Xen on return.
  15. */
  16. void xen_force_evtchn_callback(void)
  17. {
  18. (void)HYPERVISOR_xen_version(0, NULL);
  19. }
  20. asmlinkage __visible unsigned long xen_save_fl(void)
  21. {
  22. struct vcpu_info *vcpu;
  23. unsigned long flags;
  24. vcpu = this_cpu_read(xen_vcpu);
  25. /* flag has opposite sense of mask */
  26. flags = !vcpu->evtchn_upcall_mask;
  27. /* convert to IF type flag
  28. -0 -> 0x00000000
  29. -1 -> 0xffffffff
  30. */
  31. return (-flags) & X86_EFLAGS_IF;
  32. }
  33. PV_CALLEE_SAVE_REGS_THUNK(xen_save_fl);
  34. __visible void xen_restore_fl(unsigned long flags)
  35. {
  36. struct vcpu_info *vcpu;
  37. /* convert from IF type flag */
  38. flags = !(flags & X86_EFLAGS_IF);
  39. /* See xen_irq_enable() for why preemption must be disabled. */
  40. preempt_disable();
  41. vcpu = this_cpu_read(xen_vcpu);
  42. vcpu->evtchn_upcall_mask = flags;
  43. if (flags == 0) {
  44. barrier(); /* unmask then check (avoid races) */
  45. if (unlikely(vcpu->evtchn_upcall_pending))
  46. xen_force_evtchn_callback();
  47. preempt_enable();
  48. } else
  49. preempt_enable_no_resched();
  50. }
  51. PV_CALLEE_SAVE_REGS_THUNK(xen_restore_fl);
  52. asmlinkage __visible void xen_irq_disable(void)
  53. {
  54. /* There's a one instruction preempt window here. We need to
  55. make sure we're don't switch CPUs between getting the vcpu
  56. pointer and updating the mask. */
  57. preempt_disable();
  58. this_cpu_read(xen_vcpu)->evtchn_upcall_mask = 1;
  59. preempt_enable_no_resched();
  60. }
  61. PV_CALLEE_SAVE_REGS_THUNK(xen_irq_disable);
  62. asmlinkage __visible void xen_irq_enable(void)
  63. {
  64. struct vcpu_info *vcpu;
  65. /*
  66. * We may be preempted as soon as vcpu->evtchn_upcall_mask is
  67. * cleared, so disable preemption to ensure we check for
  68. * events on the VCPU we are still running on.
  69. */
  70. preempt_disable();
  71. vcpu = this_cpu_read(xen_vcpu);
  72. vcpu->evtchn_upcall_mask = 0;
  73. /* Doesn't matter if we get preempted here, because any
  74. pending event will get dealt with anyway. */
  75. barrier(); /* unmask then check (avoid races) */
  76. if (unlikely(vcpu->evtchn_upcall_pending))
  77. xen_force_evtchn_callback();
  78. preempt_enable();
  79. }
  80. PV_CALLEE_SAVE_REGS_THUNK(xen_irq_enable);
  81. static void xen_safe_halt(void)
  82. {
  83. /* Blocking includes an implicit local_irq_enable(). */
  84. if (HYPERVISOR_sched_op(SCHEDOP_block, NULL) != 0)
  85. BUG();
  86. }
  87. static void xen_halt(void)
  88. {
  89. if (irqs_disabled())
  90. HYPERVISOR_vcpu_op(VCPUOP_down,
  91. xen_vcpu_nr(smp_processor_id()), NULL);
  92. else
  93. xen_safe_halt();
  94. }
  95. static const struct pv_irq_ops xen_irq_ops __initconst = {
  96. .save_fl = PV_CALLEE_SAVE(xen_save_fl),
  97. .restore_fl = PV_CALLEE_SAVE(xen_restore_fl),
  98. .irq_disable = PV_CALLEE_SAVE(xen_irq_disable),
  99. .irq_enable = PV_CALLEE_SAVE(xen_irq_enable),
  100. .safe_halt = xen_safe_halt,
  101. .halt = xen_halt,
  102. #ifdef CONFIG_X86_64
  103. .adjust_exception_frame = xen_adjust_exception_frame,
  104. #endif
  105. };
  106. void __init xen_init_irq_ops(void)
  107. {
  108. /* For PVH we use default pv_irq_ops settings. */
  109. if (!xen_feature(XENFEAT_hvm_callback_vector))
  110. pv_irq_ops = xen_irq_ops;
  111. x86_init.irqs.intr_init = xen_init_IRQ;
  112. }