/*1* Asm versions of Xen pv-ops, suitable for either direct use or2* inlining. The inline versions are the same as the direct-use3* versions, with the pre- and post-amble chopped off.4*5* This code is encoded for size rather than absolute efficiency, with6* a view to being able to inline as much as possible.7*8* We only bother with direct forms (ie, vcpu in pda) of the9* operations here; the indirect forms are better handled in C, since10* they're generally too large to inline anyway.11*/1213#include <asm/thread_info.h>14#include <asm/processor-flags.h>15#include <asm/segment.h>1617#include <xen/interface/xen.h>1819#include "xen-asm.h"2021/*22* Force an event check by making a hypercall, but preserve regs23* before making the call.24*/25check_events:26push %eax27push %ecx28push %edx29call xen_force_evtchn_callback30pop %edx31pop %ecx32pop %eax33ret3435/*36* We can't use sysexit directly, because we're not running in ring0.37* But we can easily fake it up using iret. Assuming xen_sysexit is38* jumped to with a standard stack frame, we can just strip it back to39* a standard iret frame and use iret.40*/41ENTRY(xen_sysexit)42movl PT_EAX(%esp), %eax /* Shouldn't be necessary? */43orl $X86_EFLAGS_IF, PT_EFLAGS(%esp)44lea PT_EIP(%esp), %esp4546jmp xen_iret47ENDPROC(xen_sysexit)4849/*50* This is run where a normal iret would be run, with the same stack setup:51* 8: eflags52* 4: cs53* esp-> 0: eip54*55* This attempts to make sure that any pending events are dealt with56* on return to usermode, but there is a small window in which an57* event can happen just before entering usermode. If the nested58* interrupt ends up setting one of the TIF_WORK_MASK pending work59* flags, they will not be tested again before returning to60* usermode. This means that a process can end up with pending work,61* which will be unprocessed until the process enters and leaves the62* kernel again, which could be an unbounded amount of time. This63* means that a pending signal or reschedule event could be64* indefinitely delayed.65*66* The fix is to notice a nested interrupt in the critical window, and67* if one occurs, then fold the nested interrupt into the current68* interrupt stack frame, and re-process it iteratively rather than69* recursively. This means that it will exit via the normal path, and70* all pending work will be dealt with appropriately.71*72* Because the nested interrupt handler needs to deal with the current73* stack state in whatever form its in, we keep things simple by only74* using a single register which is pushed/popped on the stack.75*/76ENTRY(xen_iret)77/* test eflags for special cases */78testl $(X86_EFLAGS_VM | XEN_EFLAGS_NMI), 8(%esp)79jnz hyper_iret8081push %eax82ESP_OFFSET=4 # bytes pushed onto stack8384/*85* Store vcpu_info pointer for easy access. Do it this way to86* avoid having to reload %fs87*/88#ifdef CONFIG_SMP89GET_THREAD_INFO(%eax)90movl TI_cpu(%eax), %eax91movl __per_cpu_offset(,%eax,4), %eax92mov xen_vcpu(%eax), %eax93#else94movl xen_vcpu, %eax95#endif9697/* check IF state we're restoring */98testb $X86_EFLAGS_IF>>8, 8+1+ESP_OFFSET(%esp)99100/*101* Maybe enable events. Once this happens we could get a102* recursive event, so the critical region starts immediately103* afterwards. However, if that happens we don't end up104* resuming the code, so we don't have to be worried about105* being preempted to another CPU.106*/107setz XEN_vcpu_info_mask(%eax)108xen_iret_start_crit:109110/* check for unmasked and pending */111cmpw $0x0001, XEN_vcpu_info_pending(%eax)112113/*114* If there's something pending, mask events again so we can115* jump back into xen_hypervisor_callback116*/117sete XEN_vcpu_info_mask(%eax)118119popl %eax120121/*122* From this point on the registers are restored and the stack123* updated, so we don't need to worry about it if we're124* preempted125*/126iret_restore_end:127128/*129* Jump to hypervisor_callback after fixing up the stack.130* Events are masked, so jumping out of the critical region is131* OK.132*/133je xen_hypervisor_callback1341351: iret136xen_iret_end_crit:137.section __ex_table, "a"138.align 4139.long 1b, iret_exc140.previous141142hyper_iret:143/* put this out of line since its very rarely used */144jmp hypercall_page + __HYPERVISOR_iret * 32145146.globl xen_iret_start_crit, xen_iret_end_crit147148/*149* This is called by xen_hypervisor_callback in entry.S when it sees150* that the EIP at the time of interrupt was between151* xen_iret_start_crit and xen_iret_end_crit. We're passed the EIP in152* %eax so we can do a more refined determination of what to do.153*154* The stack format at this point is:155* ----------------156* ss : (ss/esp may be present if we came from usermode)157* esp :158* eflags } outer exception info159* cs }160* eip }161* ---------------- <- edi (copy dest)162* eax : outer eax if it hasn't been restored163* ----------------164* eflags } nested exception info165* cs } (no ss/esp because we're nested166* eip } from the same ring)167* orig_eax }<- esi (copy src)168* - - - - - - - -169* fs }170* es }171* ds } SAVE_ALL state172* eax }173* : :174* ebx }<- esp175* ----------------176*177* In order to deliver the nested exception properly, we need to shift178* everything from the return addr up to the error code so it sits179* just under the outer exception info. This means that when we180* handle the exception, we do it in the context of the outer181* exception rather than starting a new one.182*183* The only caveat is that if the outer eax hasn't been restored yet184* (ie, it's still on stack), we need to insert its value into the185* SAVE_ALL state before going on, since it's usermode state which we186* eventually need to restore.187*/188ENTRY(xen_iret_crit_fixup)189/*190* Paranoia: Make sure we're really coming from kernel space.191* One could imagine a case where userspace jumps into the192* critical range address, but just before the CPU delivers a193* GP, it decides to deliver an interrupt instead. Unlikely?194* Definitely. Easy to avoid? Yes. The Intel documents195* explicitly say that the reported EIP for a bad jump is the196* jump instruction itself, not the destination, but some197* virtual environments get this wrong.198*/199movl PT_CS(%esp), %ecx200andl $SEGMENT_RPL_MASK, %ecx201cmpl $USER_RPL, %ecx202je 2f203204lea PT_ORIG_EAX(%esp), %esi205lea PT_EFLAGS(%esp), %edi206207/*208* If eip is before iret_restore_end then stack209* hasn't been restored yet.210*/211cmp $iret_restore_end, %eax212jae 1f213214movl 0+4(%edi), %eax /* copy EAX (just above top of frame) */215movl %eax, PT_EAX(%esp)216217lea ESP_OFFSET(%edi), %edi /* move dest up over saved regs */218219/* set up the copy */2201: std221mov $PT_EIP / 4, %ecx /* saved regs up to orig_eax */222rep movsl223cld224225lea 4(%edi), %esp /* point esp to new frame */2262: jmp xen_do_upcall227228229230