xen-asm.S 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. Asm versions of Xen pv-ops, suitable for either direct use or inlining.
  3. The inline versions are the same as the direct-use versions, with the
  4. pre- and post-amble chopped off.
  5. This code is encoded for size rather than absolute efficiency,
  6. with a view to being able to inline as much as possible.
  7. We only bother with direct forms (ie, vcpu in pda) of the operations
  8. here; the indirect forms are better handled in C, since they're
  9. generally too large to inline anyway.
  10. */
  11. #include <linux/linkage.h>
  12. #include <asm/asm-offsets.h>
  13. #include <asm/thread_info.h>
  14. #include <asm/percpu.h>
  15. #include <asm/processor-flags.h>
  16. #include <asm/segment.h>
  17. #include <xen/interface/xen.h>
  18. #define RELOC(x, v) .globl x##_reloc; x##_reloc=v
  19. #define ENDPATCH(x) .globl x##_end; x##_end=.
  20. /* Pseudo-flag used for virtual NMI, which we don't implement yet */
  21. #define XEN_EFLAGS_NMI 0x80000000
  22. /*
  23. Enable events. This clears the event mask and tests the pending
  24. event status with one and operation. If there are pending
  25. events, then enter the hypervisor to get them handled.
  26. */
  27. ENTRY(xen_irq_enable_direct)
  28. /* Clear mask and test pending */
  29. andw $0x00ff, PER_CPU_VAR(xen_vcpu_info)+XEN_vcpu_info_pending
  30. /* Preempt here doesn't matter because that will deal with
  31. any pending interrupts. The pending check may end up being
  32. run on the wrong CPU, but that doesn't hurt. */
  33. jz 1f
  34. 2: call check_events
  35. 1:
  36. ENDPATCH(xen_irq_enable_direct)
  37. ret
  38. ENDPROC(xen_irq_enable_direct)
  39. RELOC(xen_irq_enable_direct, 2b+1)
  40. /*
  41. Disabling events is simply a matter of making the event mask
  42. non-zero.
  43. */
  44. ENTRY(xen_irq_disable_direct)
  45. movb $1, PER_CPU_VAR(xen_vcpu_info)+XEN_vcpu_info_mask
  46. ENDPATCH(xen_irq_disable_direct)
  47. ret
  48. ENDPROC(xen_irq_disable_direct)
  49. RELOC(xen_irq_disable_direct, 0)
  50. /*
  51. (xen_)save_fl is used to get the current interrupt enable status.
  52. Callers expect the status to be in X86_EFLAGS_IF, and other bits
  53. may be set in the return value. We take advantage of this by
  54. making sure that X86_EFLAGS_IF has the right value (and other bits
  55. in that byte are 0), but other bits in the return value are
  56. undefined. We need to toggle the state of the bit, because
  57. Xen and x86 use opposite senses (mask vs enable).
  58. */
  59. ENTRY(xen_save_fl_direct)
  60. testb $0xff, PER_CPU_VAR(xen_vcpu_info)+XEN_vcpu_info_mask
  61. setz %ah
  62. addb %ah,%ah
  63. ENDPATCH(xen_save_fl_direct)
  64. ret
  65. ENDPROC(xen_save_fl_direct)
  66. RELOC(xen_save_fl_direct, 0)
  67. /*
  68. In principle the caller should be passing us a value return
  69. from xen_save_fl_direct, but for robustness sake we test only
  70. the X86_EFLAGS_IF flag rather than the whole byte. After
  71. setting the interrupt mask state, it checks for unmasked
  72. pending events and enters the hypervisor to get them delivered
  73. if so.
  74. */
  75. ENTRY(xen_restore_fl_direct)
  76. testb $X86_EFLAGS_IF>>8, %ah
  77. setz PER_CPU_VAR(xen_vcpu_info)+XEN_vcpu_info_mask
  78. /* Preempt here doesn't matter because that will deal with
  79. any pending interrupts. The pending check may end up being
  80. run on the wrong CPU, but that doesn't hurt. */
  81. /* check for unmasked and pending */
  82. cmpw $0x0001, PER_CPU_VAR(xen_vcpu_info)+XEN_vcpu_info_pending
  83. jz 1f
  84. 2: call check_events
  85. 1:
  86. ENDPATCH(xen_restore_fl_direct)
  87. ret
  88. ENDPROC(xen_restore_fl_direct)
  89. RELOC(xen_restore_fl_direct, 2b+1)
  90. /*
  91. This is run where a normal iret would be run, with the same stack setup:
  92. 8: eflags
  93. 4: cs
  94. esp-> 0: eip
  95. This attempts to make sure that any pending events are dealt
  96. with on return to usermode, but there is a small window in
  97. which an event can happen just before entering usermode. If
  98. the nested interrupt ends up setting one of the TIF_WORK_MASK
  99. pending work flags, they will not be tested again before
  100. returning to usermode. This means that a process can end up
  101. with pending work, which will be unprocessed until the process
  102. enters and leaves the kernel again, which could be an
  103. unbounded amount of time. This means that a pending signal or
  104. reschedule event could be indefinitely delayed.
  105. The fix is to notice a nested interrupt in the critical
  106. window, and if one occurs, then fold the nested interrupt into
  107. the current interrupt stack frame, and re-process it
  108. iteratively rather than recursively. This means that it will
  109. exit via the normal path, and all pending work will be dealt
  110. with appropriately.
  111. Because the nested interrupt handler needs to deal with the
  112. current stack state in whatever form its in, we keep things
  113. simple by only using a single register which is pushed/popped
  114. on the stack.
  115. Non-direct iret could be done in the same way, but it would
  116. require an annoying amount of code duplication. We'll assume
  117. that direct mode will be the common case once the hypervisor
  118. support becomes commonplace.
  119. */
  120. ENTRY(xen_iret_direct)
  121. /* test eflags for special cases */
  122. testl $(X86_EFLAGS_VM | XEN_EFLAGS_NMI), 8(%esp)
  123. jnz hyper_iret
  124. push %eax
  125. ESP_OFFSET=4 # bytes pushed onto stack
  126. /* Store vcpu_info pointer for easy access. Do it this
  127. way to avoid having to reload %fs */
  128. #ifdef CONFIG_SMP
  129. GET_THREAD_INFO(%eax)
  130. movl TI_cpu(%eax),%eax
  131. movl __per_cpu_offset(,%eax,4),%eax
  132. lea per_cpu__xen_vcpu_info(%eax),%eax
  133. #else
  134. movl $per_cpu__xen_vcpu_info, %eax
  135. #endif
  136. /* check IF state we're restoring */
  137. testb $X86_EFLAGS_IF>>8, 8+1+ESP_OFFSET(%esp)
  138. /* Maybe enable events. Once this happens we could get a
  139. recursive event, so the critical region starts immediately
  140. afterwards. However, if that happens we don't end up
  141. resuming the code, so we don't have to be worried about
  142. being preempted to another CPU. */
  143. setz XEN_vcpu_info_mask(%eax)
  144. xen_iret_start_crit:
  145. /* check for unmasked and pending */
  146. cmpw $0x0001, XEN_vcpu_info_pending(%eax)
  147. /* If there's something pending, mask events again so we
  148. can jump back into xen_hypervisor_callback */
  149. sete XEN_vcpu_info_mask(%eax)
  150. popl %eax
  151. /* From this point on the registers are restored and the stack
  152. updated, so we don't need to worry about it if we're preempted */
  153. iret_restore_end:
  154. /* Jump to hypervisor_callback after fixing up the stack.
  155. Events are masked, so jumping out of the critical
  156. region is OK. */
  157. je xen_hypervisor_callback
  158. iret
  159. xen_iret_end_crit:
  160. hyper_iret:
  161. /* put this out of line since its very rarely used */
  162. jmp hypercall_page + __HYPERVISOR_iret * 32
  163. .globl xen_iret_start_crit, xen_iret_end_crit
  164. /*
  165. This is called by xen_hypervisor_callback in entry.S when it sees
  166. that the EIP at the time of interrupt was between xen_iret_start_crit
  167. and xen_iret_end_crit. We're passed the EIP in %eax so we can do
  168. a more refined determination of what to do.
  169. The stack format at this point is:
  170. ----------------
  171. ss : (ss/esp may be present if we came from usermode)
  172. esp :
  173. eflags } outer exception info
  174. cs }
  175. eip }
  176. ---------------- <- edi (copy dest)
  177. eax : outer eax if it hasn't been restored
  178. ----------------
  179. eflags } nested exception info
  180. cs } (no ss/esp because we're nested
  181. eip } from the same ring)
  182. orig_eax }<- esi (copy src)
  183. - - - - - - - -
  184. fs }
  185. es }
  186. ds } SAVE_ALL state
  187. eax }
  188. : :
  189. ebx }
  190. ----------------
  191. return addr <- esp
  192. ----------------
  193. In order to deliver the nested exception properly, we need to shift
  194. everything from the return addr up to the error code so it
  195. sits just under the outer exception info. This means that when we
  196. handle the exception, we do it in the context of the outer exception
  197. rather than starting a new one.
  198. The only caveat is that if the outer eax hasn't been
  199. restored yet (ie, it's still on stack), we need to insert
  200. its value into the SAVE_ALL state before going on, since
  201. it's usermode state which we eventually need to restore.
  202. */
  203. ENTRY(xen_iret_crit_fixup)
  204. /* offsets +4 for return address */
  205. /*
  206. Paranoia: Make sure we're really coming from userspace.
  207. One could imagine a case where userspace jumps into the
  208. critical range address, but just before the CPU delivers a GP,
  209. it decides to deliver an interrupt instead. Unlikely?
  210. Definitely. Easy to avoid? Yes. The Intel documents
  211. explicitly say that the reported EIP for a bad jump is the
  212. jump instruction itself, not the destination, but some virtual
  213. environments get this wrong.
  214. */
  215. movl PT_CS+4(%esp), %ecx
  216. andl $SEGMENT_RPL_MASK, %ecx
  217. cmpl $USER_RPL, %ecx
  218. je 2f
  219. lea PT_ORIG_EAX+4(%esp), %esi
  220. lea PT_EFLAGS+4(%esp), %edi
  221. /* If eip is before iret_restore_end then stack
  222. hasn't been restored yet. */
  223. cmp $iret_restore_end, %eax
  224. jae 1f
  225. movl 0+4(%edi),%eax /* copy EAX */
  226. movl %eax, PT_EAX+4(%esp)
  227. lea ESP_OFFSET(%edi),%edi /* move dest up over saved regs */
  228. /* set up the copy */
  229. 1: std
  230. mov $(PT_EIP+4) / 4, %ecx /* copy ret+saved regs up to orig_eax */
  231. rep movsl
  232. cld
  233. lea 4(%edi),%esp /* point esp to new frame */
  234. 2: ret
  235. /*
  236. Force an event check by making a hypercall,
  237. but preserve regs before making the call.
  238. */
  239. check_events:
  240. push %eax
  241. push %ecx
  242. push %edx
  243. call force_evtchn_callback
  244. pop %edx
  245. pop %ecx
  246. pop %eax
  247. ret