xen-asm_32.S 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Asm versions of Xen pv-ops, suitable for either direct use or
  3. * inlining. The inline versions are the same as the direct-use
  4. * versions, with the pre- and post-amble chopped off.
  5. *
  6. * This code is encoded for size rather than absolute efficiency, with
  7. * a view to being able to inline as much as possible.
  8. *
  9. * We only bother with direct forms (ie, vcpu in pda) of the
  10. * operations here; the indirect forms are better handled in C, since
  11. * they're generally too large to inline anyway.
  12. */
  13. #include <asm/thread_info.h>
  14. #include <asm/processor-flags.h>
  15. #include <asm/segment.h>
  16. #include <asm/asm.h>
  17. #include <xen/interface/xen.h>
  18. #include "xen-asm.h"
  19. /*
  20. * Force an event check by making a hypercall, but preserve regs
  21. * before making the call.
  22. */
  23. check_events:
  24. push %eax
  25. push %ecx
  26. push %edx
  27. call xen_force_evtchn_callback
  28. pop %edx
  29. pop %ecx
  30. pop %eax
  31. ret
  32. /*
  33. * This is run where a normal iret would be run, with the same stack setup:
  34. * 8: eflags
  35. * 4: cs
  36. * esp-> 0: eip
  37. *
  38. * This attempts to make sure that any pending events are dealt with
  39. * on return to usermode, but there is a small window in which an
  40. * event can happen just before entering usermode. If the nested
  41. * interrupt ends up setting one of the TIF_WORK_MASK pending work
  42. * flags, they will not be tested again before returning to
  43. * usermode. This means that a process can end up with pending work,
  44. * which will be unprocessed until the process enters and leaves the
  45. * kernel again, which could be an unbounded amount of time. This
  46. * means that a pending signal or reschedule event could be
  47. * indefinitely delayed.
  48. *
  49. * The fix is to notice a nested interrupt in the critical window, and
  50. * if one occurs, then fold the nested interrupt into the current
  51. * interrupt stack frame, and re-process it iteratively rather than
  52. * recursively. This means that it will exit via the normal path, and
  53. * all pending work will be dealt with appropriately.
  54. *
  55. * Because the nested interrupt handler needs to deal with the current
  56. * stack state in whatever form its in, we keep things simple by only
  57. * using a single register which is pushed/popped on the stack.
  58. */
  59. .macro POP_FS
  60. 1:
  61. popw %fs
  62. .pushsection .fixup, "ax"
  63. 2: movw $0, (%esp)
  64. jmp 1b
  65. .popsection
  66. _ASM_EXTABLE(1b,2b)
  67. .endm
  68. ENTRY(xen_iret)
  69. /* test eflags for special cases */
  70. testl $(X86_EFLAGS_VM | XEN_EFLAGS_NMI), 8(%esp)
  71. jnz hyper_iret
  72. push %eax
  73. ESP_OFFSET=4 # bytes pushed onto stack
  74. /* Store vcpu_info pointer for easy access */
  75. #ifdef CONFIG_SMP
  76. pushw %fs
  77. movl $(__KERNEL_PERCPU), %eax
  78. movl %eax, %fs
  79. movl %fs:xen_vcpu, %eax
  80. POP_FS
  81. #else
  82. movl %ss:xen_vcpu, %eax
  83. #endif
  84. /* check IF state we're restoring */
  85. testb $X86_EFLAGS_IF>>8, 8+1+ESP_OFFSET(%esp)
  86. /*
  87. * Maybe enable events. Once this happens we could get a
  88. * recursive event, so the critical region starts immediately
  89. * afterwards. However, if that happens we don't end up
  90. * resuming the code, so we don't have to be worried about
  91. * being preempted to another CPU.
  92. */
  93. setz %ss:XEN_vcpu_info_mask(%eax)
  94. xen_iret_start_crit:
  95. /* check for unmasked and pending */
  96. cmpw $0x0001, %ss:XEN_vcpu_info_pending(%eax)
  97. /*
  98. * If there's something pending, mask events again so we can
  99. * jump back into xen_hypervisor_callback. Otherwise do not
  100. * touch XEN_vcpu_info_mask.
  101. */
  102. jne 1f
  103. movb $1, %ss:XEN_vcpu_info_mask(%eax)
  104. 1: popl %eax
  105. /*
  106. * From this point on the registers are restored and the stack
  107. * updated, so we don't need to worry about it if we're
  108. * preempted
  109. */
  110. iret_restore_end:
  111. /*
  112. * Jump to hypervisor_callback after fixing up the stack.
  113. * Events are masked, so jumping out of the critical region is
  114. * OK.
  115. */
  116. je xen_hypervisor_callback
  117. 1: iret
  118. xen_iret_end_crit:
  119. _ASM_EXTABLE(1b, iret_exc)
  120. hyper_iret:
  121. /* put this out of line since its very rarely used */
  122. jmp hypercall_page + __HYPERVISOR_iret * 32
  123. .globl xen_iret_start_crit, xen_iret_end_crit
  124. /*
  125. * This is called by xen_hypervisor_callback in entry.S when it sees
  126. * that the EIP at the time of interrupt was between
  127. * xen_iret_start_crit and xen_iret_end_crit. We're passed the EIP in
  128. * %eax so we can do a more refined determination of what to do.
  129. *
  130. * The stack format at this point is:
  131. * ----------------
  132. * ss : (ss/esp may be present if we came from usermode)
  133. * esp :
  134. * eflags } outer exception info
  135. * cs }
  136. * eip }
  137. * ---------------- <- edi (copy dest)
  138. * eax : outer eax if it hasn't been restored
  139. * ----------------
  140. * eflags } nested exception info
  141. * cs } (no ss/esp because we're nested
  142. * eip } from the same ring)
  143. * orig_eax }<- esi (copy src)
  144. * - - - - - - - -
  145. * fs }
  146. * es }
  147. * ds } SAVE_ALL state
  148. * eax }
  149. * : :
  150. * ebx }<- esp
  151. * ----------------
  152. *
  153. * In order to deliver the nested exception properly, we need to shift
  154. * everything from the return addr up to the error code so it sits
  155. * just under the outer exception info. This means that when we
  156. * handle the exception, we do it in the context of the outer
  157. * exception rather than starting a new one.
  158. *
  159. * The only caveat is that if the outer eax hasn't been restored yet
  160. * (ie, it's still on stack), we need to insert its value into the
  161. * SAVE_ALL state before going on, since it's usermode state which we
  162. * eventually need to restore.
  163. */
  164. ENTRY(xen_iret_crit_fixup)
  165. /*
  166. * Paranoia: Make sure we're really coming from kernel space.
  167. * One could imagine a case where userspace jumps into the
  168. * critical range address, but just before the CPU delivers a
  169. * GP, it decides to deliver an interrupt instead. Unlikely?
  170. * Definitely. Easy to avoid? Yes. The Intel documents
  171. * explicitly say that the reported EIP for a bad jump is the
  172. * jump instruction itself, not the destination, but some
  173. * virtual environments get this wrong.
  174. */
  175. movl PT_CS(%esp), %ecx
  176. andl $SEGMENT_RPL_MASK, %ecx
  177. cmpl $USER_RPL, %ecx
  178. je 2f
  179. lea PT_ORIG_EAX(%esp), %esi
  180. lea PT_EFLAGS(%esp), %edi
  181. /*
  182. * If eip is before iret_restore_end then stack
  183. * hasn't been restored yet.
  184. */
  185. cmp $iret_restore_end, %eax
  186. jae 1f
  187. movl 0+4(%edi), %eax /* copy EAX (just above top of frame) */
  188. movl %eax, PT_EAX(%esp)
  189. lea ESP_OFFSET(%edi), %edi /* move dest up over saved regs */
  190. /* set up the copy */
  191. 1: std
  192. mov $PT_EIP / 4, %ecx /* saved regs up to orig_eax */
  193. rep movsl
  194. cld
  195. lea 4(%edi), %esp /* point esp to new frame */
  196. 2: jmp xen_do_upcall