xen-asm_32.S 5.9 KB

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