hypervisor.c 3.81 KB
Newer Older
1 2 3 4 5
/******************************************************************************
 * hypervisor.c
 * 
 * Communication to/from hypervisor.
 * 
6
 * Copyright (c) 2002-2003, K A Fraser
kaf24's avatar
kaf24 committed
7
 * Copyright (c) 2005, Grzegorz Milos, gm281@cam.ac.uk,Intel Research Cambridge
8
 * 
9 10 11 12 13 14
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
15
 * 
16 17
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
18
 * 
19 20 21 22 23 24 25
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
26 27
 */

28 29 30 31
#include <mini-os/os.h>
#include <mini-os/lib.h>
#include <mini-os/hypervisor.h>
#include <mini-os/events.h>
32

kaf24's avatar
kaf24 committed
33 34 35
#define active_evtchns(cpu,sh,idx)              \
    ((sh)->evtchn_pending[idx] &                \
     ~(sh)->evtchn_mask[idx])
36

37 38
int in_callback;

39 40
void do_hypervisor_callback(struct pt_regs *regs)
{
kaf24's avatar
kaf24 committed
41 42
    unsigned long  l1, l2, l1i, l2i;
    unsigned int   port;
kaf24's avatar
kaf24 committed
43 44
    int            cpu = 0;
    shared_info_t *s = HYPERVISOR_shared_info;
45
    vcpu_info_t   *vcpu_info = &s->vcpu_info[cpu];
46

47
    in_callback = 1;
48
   
kaf24's avatar
kaf24 committed
49
    vcpu_info->evtchn_upcall_pending = 0;
50 51 52 53 54
    /* NB x86. No need for a barrier here -- XCHG is a barrier on x86. */
#if !defined(__i386__) && !defined(__x86_64__)
    /* Clear master flag /before/ clearing selector flag. */
    wmb();
#endif
kaf24's avatar
kaf24 committed
55 56 57 58
    l1 = xchg(&vcpu_info->evtchn_pending_sel, 0);
    while ( l1 != 0 )
    {
        l1i = __ffs(l1);
Keir Fraser's avatar
Keir Fraser committed
59
        l1 &= ~(1UL << l1i);
kaf24's avatar
kaf24 committed
60 61 62 63
        
        while ( (l2 = active_evtchns(cpu, s, l1i)) != 0 )
        {
            l2i = __ffs(l2);
Keir Fraser's avatar
Keir Fraser committed
64
            l2 &= ~(1UL << l2i);
65

66
            port = (l1i * (sizeof(unsigned long) * 8)) + l2i;
kaf24's avatar
kaf24 committed
67 68
			do_event(port, regs);
        }
69
    }
70 71

    in_callback = 0;
72 73
}

74 75
void force_evtchn_callback(void)
{
Keir Fraser's avatar
Keir Fraser committed
76
    int save;
77 78
    vcpu_info_t *vcpu;
    vcpu = &HYPERVISOR_shared_info->vcpu_info[smp_processor_id()];
Keir Fraser's avatar
Keir Fraser committed
79
    save = vcpu->evtchn_upcall_mask;
80 81 82 83 84 85 86 87 88 89

    while (vcpu->evtchn_upcall_pending) {
        vcpu->evtchn_upcall_mask = 1;
        barrier();
        do_hypervisor_callback(NULL);
        barrier();
        vcpu->evtchn_upcall_mask = save;
        barrier();
    };
}
kaf24's avatar
kaf24 committed
90

91
inline void mask_evtchn(uint32_t port)
92
{
kaf24's avatar
kaf24 committed
93 94
    shared_info_t *s = HYPERVISOR_shared_info;
    synch_set_bit(port, &s->evtchn_mask[0]);
95 96
}

97
inline void unmask_evtchn(uint32_t port)
98
{
kaf24's avatar
kaf24 committed
99
    shared_info_t *s = HYPERVISOR_shared_info;
100
    vcpu_info_t *vcpu_info = &s->vcpu_info[smp_processor_id()];
kaf24's avatar
kaf24 committed
101 102 103 104 105 106 107 108

    synch_clear_bit(port, &s->evtchn_mask[0]);

    /*
     * The following is basically the equivalent of 'hw_resend_irq'. Just like
     * a real IO-APIC we 'lose the interrupt edge' if the channel is masked.
     */
    if (  synch_test_bit        (port,    &s->evtchn_pending[0]) && 
109 110
         !synch_test_and_set_bit(port / (sizeof(unsigned long) * 8),
              &vcpu_info->evtchn_pending_sel) )
kaf24's avatar
kaf24 committed
111 112 113 114 115
    {
        vcpu_info->evtchn_upcall_pending = 1;
        if ( !vcpu_info->evtchn_upcall_mask )
            force_evtchn_callback();
    }
116 117
}

118
inline void clear_evtchn(uint32_t port)
119
{
kaf24's avatar
kaf24 committed
120 121
    shared_info_t *s = HYPERVISOR_shared_info;
    synch_clear_bit(port, &s->evtchn_pending[0]);
122
}