xen-asm_32.S 9.1 KB

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