Home | History | Annotate | Line # | Download | only in x86
hypervisor_machdep.c revision 1.2.4.2
      1  1.2.4.2  ad /*	$NetBSD: hypervisor_machdep.c,v 1.2.4.2 2007/12/03 19:04:40 ad Exp $	*/
      2  1.2.4.2  ad 
      3  1.2.4.2  ad /*
      4  1.2.4.2  ad  *
      5  1.2.4.2  ad  * Copyright (c) 2004 Christian Limpach.
      6  1.2.4.2  ad  * All rights reserved.
      7  1.2.4.2  ad  *
      8  1.2.4.2  ad  * Redistribution and use in source and binary forms, with or without
      9  1.2.4.2  ad  * modification, are permitted provided that the following conditions
     10  1.2.4.2  ad  * are met:
     11  1.2.4.2  ad  * 1. Redistributions of source code must retain the above copyright
     12  1.2.4.2  ad  *    notice, this list of conditions and the following disclaimer.
     13  1.2.4.2  ad  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.2.4.2  ad  *    notice, this list of conditions and the following disclaimer in the
     15  1.2.4.2  ad  *    documentation and/or other materials provided with the distribution.
     16  1.2.4.2  ad  * 3. All advertising materials mentioning features or use of this software
     17  1.2.4.2  ad  *    must display the following acknowledgement:
     18  1.2.4.2  ad  *      This product includes software developed by Christian Limpach.
     19  1.2.4.2  ad  * 4. The name of the author may not be used to endorse or promote products
     20  1.2.4.2  ad  *    derived from this software without specific prior written permission.
     21  1.2.4.2  ad  *
     22  1.2.4.2  ad  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  1.2.4.2  ad  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  1.2.4.2  ad  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  1.2.4.2  ad  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  1.2.4.2  ad  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  1.2.4.2  ad  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  1.2.4.2  ad  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  1.2.4.2  ad  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  1.2.4.2  ad  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  1.2.4.2  ad  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  1.2.4.2  ad  */
     33  1.2.4.2  ad 
     34  1.2.4.2  ad /******************************************************************************
     35  1.2.4.2  ad  * hypervisor.c
     36  1.2.4.2  ad  *
     37  1.2.4.2  ad  * Communication to/from hypervisor.
     38  1.2.4.2  ad  *
     39  1.2.4.2  ad  * Copyright (c) 2002-2004, K A Fraser
     40  1.2.4.2  ad  *
     41  1.2.4.2  ad  * Permission is hereby granted, free of charge, to any person obtaining a copy
     42  1.2.4.2  ad  * of this software and associated documentation files (the "Software"), to
     43  1.2.4.2  ad  * deal in the Software without restriction, including without limitation the
     44  1.2.4.2  ad  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
     45  1.2.4.2  ad  * sell copies of the Software, and to permit persons to whom the Software is
     46  1.2.4.2  ad  * furnished to do so, subject to the following conditions:
     47  1.2.4.2  ad  *
     48  1.2.4.2  ad  * The above copyright notice and this permission notice shall be included in
     49  1.2.4.2  ad  * all copies or substantial portions of the Software.
     50  1.2.4.2  ad  *
     51  1.2.4.2  ad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     52  1.2.4.2  ad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     53  1.2.4.2  ad  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     54  1.2.4.2  ad  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     55  1.2.4.2  ad  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     56  1.2.4.2  ad  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
     57  1.2.4.2  ad  * DEALINGS IN THE SOFTWARE.
     58  1.2.4.2  ad  */
     59  1.2.4.2  ad 
     60  1.2.4.2  ad 
     61  1.2.4.2  ad #include <sys/cdefs.h>
     62  1.2.4.2  ad __KERNEL_RCSID(0, "$NetBSD: hypervisor_machdep.c,v 1.2.4.2 2007/12/03 19:04:40 ad Exp $");
     63  1.2.4.2  ad 
     64  1.2.4.2  ad #include <sys/param.h>
     65  1.2.4.2  ad #include <sys/systm.h>
     66  1.2.4.2  ad 
     67  1.2.4.2  ad #include <xen/xen.h>
     68  1.2.4.2  ad #include <xen/hypervisor.h>
     69  1.2.4.2  ad #include <xen/evtchn.h>
     70  1.2.4.2  ad #include <machine/atomic.h>
     71  1.2.4.2  ad 
     72  1.2.4.2  ad #include "opt_xen.h"
     73  1.2.4.2  ad 
     74  1.2.4.2  ad // #define PORT_DEBUG 4
     75  1.2.4.2  ad // #define EARLY_DEBUG_EVENT
     76  1.2.4.2  ad 
     77  1.2.4.2  ad #ifdef EARLY_DEBUG_EVENT
     78  1.2.4.2  ad extern int debug_port;
     79  1.2.4.2  ad extern int xen_debug_handler(void *);
     80  1.2.4.2  ad #endif
     81  1.2.4.2  ad 
     82  1.2.4.2  ad int stipending(void);
     83  1.2.4.2  ad int
     84  1.2.4.2  ad stipending()
     85  1.2.4.2  ad {
     86  1.2.4.2  ad 	uint32_t l1;
     87  1.2.4.2  ad 	unsigned long l2;
     88  1.2.4.2  ad 	unsigned int l1i, l2i, port;
     89  1.2.4.2  ad 	volatile shared_info_t *s = HYPERVISOR_shared_info;
     90  1.2.4.2  ad 	struct cpu_info *ci;
     91  1.2.4.2  ad 	int ret;
     92  1.2.4.2  ad 
     93  1.2.4.2  ad 	ret = 0;
     94  1.2.4.2  ad 	ci = curcpu();
     95  1.2.4.2  ad 
     96  1.2.4.2  ad #if 0
     97  1.2.4.2  ad 	if (HYPERVISOR_shared_info->events)
     98  1.2.4.2  ad 		printf("stipending events %08lx mask %08lx ilevel %d\n",
     99  1.2.4.2  ad 		    HYPERVISOR_shared_info->events,
    100  1.2.4.2  ad 		    HYPERVISOR_shared_info->events_mask, ci->ci_ilevel);
    101  1.2.4.2  ad #endif
    102  1.2.4.2  ad 
    103  1.2.4.2  ad #ifdef EARLY_DEBUG_EVENT
    104  1.2.4.2  ad 	if (xen_atomic_test_bit(&s->evtchn_pending[0], debug_port)) {
    105  1.2.4.2  ad 		xen_debug_handler(NULL);
    106  1.2.4.2  ad 		xen_atomic_clear_bit(&s->evtchn_pending[0], debug_port);
    107  1.2.4.2  ad 	}
    108  1.2.4.2  ad #endif
    109  1.2.4.2  ad 
    110  1.2.4.2  ad 	/*
    111  1.2.4.2  ad 	 * we're only called after STIC, so we know that we'll have to
    112  1.2.4.2  ad 	 * STI at the end
    113  1.2.4.2  ad 	 */
    114  1.2.4.2  ad 	while (s->vcpu_info[0].evtchn_upcall_pending) {
    115  1.2.4.2  ad 		cli();
    116  1.2.4.2  ad 		s->vcpu_info[0].evtchn_upcall_pending = 0;
    117  1.2.4.2  ad 		/* NB. No need for a barrier here -- XCHG is a barrier
    118  1.2.4.2  ad 		 * on x86. */
    119  1.2.4.2  ad #ifdef XEN3
    120  1.2.4.2  ad 		l1 = xen_atomic_xchg(&s->vcpu_info[0].evtchn_pending_sel, 0);
    121  1.2.4.2  ad #else
    122  1.2.4.2  ad 		l1 = xen_atomic_xchg(&s->evtchn_pending_sel, 0);
    123  1.2.4.2  ad #endif
    124  1.2.4.2  ad 		while ((l1i = ffs(l1)) != 0) {
    125  1.2.4.2  ad 			l1i--;
    126  1.2.4.2  ad 			l1 &= ~(1 << l1i);
    127  1.2.4.2  ad 
    128  1.2.4.2  ad 			l2 = s->evtchn_pending[l1i] & ~s->evtchn_mask[l1i];
    129  1.2.4.2  ad 			/*
    130  1.2.4.2  ad 			 * mask and clear event. More efficient than calling
    131  1.2.4.2  ad 			 * hypervisor_mask/clear_event for each event.
    132  1.2.4.2  ad 			 */
    133  1.2.4.2  ad 			xen_atomic_setbits_l(&s->evtchn_mask[l1i], l2);
    134  1.2.4.2  ad 			xen_atomic_clearbits_l(&s->evtchn_pending[l1i], l2);
    135  1.2.4.2  ad 			while ((l2i = ffs(l2)) != 0) {
    136  1.2.4.2  ad 				l2i--;
    137  1.2.4.2  ad 				l2 &= ~(1 << l2i);
    138  1.2.4.2  ad 
    139  1.2.4.2  ad 				port = (l1i << 5) + l2i;
    140  1.2.4.2  ad 				if (evtsource[port]) {
    141  1.2.4.2  ad 					hypervisor_set_ipending(
    142  1.2.4.2  ad 					    evtsource[port]->ev_imask,
    143  1.2.4.2  ad 					    l1i, l2i);
    144  1.2.4.2  ad 					evtsource[port]->ev_evcnt.ev_count++;
    145  1.2.4.2  ad 					if (ret == 0 && ci->ci_ilevel <
    146  1.2.4.2  ad 					    evtsource[port]->ev_maxlevel)
    147  1.2.4.2  ad 						ret = 1;
    148  1.2.4.2  ad 				}
    149  1.2.4.2  ad #ifdef DOM0OPS
    150  1.2.4.2  ad 				else
    151  1.2.4.2  ad 					xenevt_event(port);
    152  1.2.4.2  ad #endif
    153  1.2.4.2  ad 			}
    154  1.2.4.2  ad 		}
    155  1.2.4.2  ad 		sti();
    156  1.2.4.2  ad 	}
    157  1.2.4.2  ad 
    158  1.2.4.2  ad #if 0
    159  1.2.4.2  ad 	if (ci->ci_ipending & 0x1)
    160  1.2.4.2  ad 		printf("stipending events %08lx mask %08lx ilevel %d ipending %08x\n",
    161  1.2.4.2  ad 		    HYPERVISOR_shared_info->events,
    162  1.2.4.2  ad 		    HYPERVISOR_shared_info->events_mask, ci->ci_ilevel,
    163  1.2.4.2  ad 		    ci->ci_ipending);
    164  1.2.4.2  ad #endif
    165  1.2.4.2  ad 
    166  1.2.4.2  ad 	return (ret);
    167  1.2.4.2  ad }
    168  1.2.4.2  ad 
    169  1.2.4.2  ad void
    170  1.2.4.2  ad do_hypervisor_callback(struct intrframe *regs)
    171  1.2.4.2  ad {
    172  1.2.4.2  ad 	uint32_t l1;
    173  1.2.4.2  ad 	unsigned long l2;
    174  1.2.4.2  ad 	unsigned int l1i, l2i, port;
    175  1.2.4.2  ad 	volatile shared_info_t *s = HYPERVISOR_shared_info;
    176  1.2.4.2  ad 	struct cpu_info *ci;
    177  1.2.4.2  ad 	int level;
    178  1.2.4.2  ad 
    179  1.2.4.2  ad 	ci = curcpu();
    180  1.2.4.2  ad 	level = ci->ci_ilevel;
    181  1.2.4.2  ad 
    182  1.2.4.2  ad 	// DDD printf("do_hypervisor_callback\n");
    183  1.2.4.2  ad 
    184  1.2.4.2  ad #ifdef EARLY_DEBUG_EVENT
    185  1.2.4.2  ad 	if (xen_atomic_test_bit(&s->evtchn_pending[0], debug_port)) {
    186  1.2.4.2  ad 		xen_debug_handler(NULL);
    187  1.2.4.2  ad 		xen_atomic_clear_bit(&s->evtchn_pending[0], debug_port);
    188  1.2.4.2  ad 	}
    189  1.2.4.2  ad #endif
    190  1.2.4.2  ad 
    191  1.2.4.2  ad 	while (s->vcpu_info[0].evtchn_upcall_pending) {
    192  1.2.4.2  ad 		s->vcpu_info[0].evtchn_upcall_pending = 0;
    193  1.2.4.2  ad 		/* NB. No need for a barrier here -- XCHG is a barrier
    194  1.2.4.2  ad 		 * on x86. */
    195  1.2.4.2  ad #ifdef XEN3
    196  1.2.4.2  ad 		l1 = xen_atomic_xchg(&s->vcpu_info[0].evtchn_pending_sel, 0);
    197  1.2.4.2  ad #else
    198  1.2.4.2  ad 		l1 = xen_atomic_xchg(&s->evtchn_pending_sel, 0);
    199  1.2.4.2  ad #endif
    200  1.2.4.2  ad 		while ((l1i = ffs(l1)) != 0) {
    201  1.2.4.2  ad 			l1i--;
    202  1.2.4.2  ad 			l1 &= ~(1 << l1i);
    203  1.2.4.2  ad 
    204  1.2.4.2  ad 			l2 = s->evtchn_pending[l1i] & ~s->evtchn_mask[l1i];
    205  1.2.4.2  ad 			/*
    206  1.2.4.2  ad 			 * mask and clear the pending events.
    207  1.2.4.2  ad 			 * Doing it here for all event that will be processed
    208  1.2.4.2  ad 			 * avoids a race with stipending (which can be called
    209  1.2.4.2  ad 			 * though evtchn_do_event->splx) that could cause an event to
    210  1.2.4.2  ad 			 * be both processed and marked pending.
    211  1.2.4.2  ad 			 */
    212  1.2.4.2  ad 			xen_atomic_setbits_l(&s->evtchn_mask[l1i], l2);
    213  1.2.4.2  ad 			xen_atomic_clearbits_l(&s->evtchn_pending[l1i], l2);
    214  1.2.4.2  ad 
    215  1.2.4.2  ad 			while ((l2i = ffs(l2)) != 0) {
    216  1.2.4.2  ad 				l2i--;
    217  1.2.4.2  ad 				l2 &= ~(1 << l2i);
    218  1.2.4.2  ad 
    219  1.2.4.2  ad 				port = (l1i << 5) + l2i;
    220  1.2.4.2  ad #ifdef PORT_DEBUG
    221  1.2.4.2  ad 				if (port == PORT_DEBUG)
    222  1.2.4.2  ad 					printf("do_hypervisor_callback event %d\n", port);
    223  1.2.4.2  ad #endif
    224  1.2.4.2  ad 				if (evtsource[port])
    225  1.2.4.2  ad 					call_evtchn_do_event(port, regs);
    226  1.2.4.2  ad #ifdef DOM0OPS
    227  1.2.4.2  ad 				else
    228  1.2.4.2  ad 					xenevt_event(port);
    229  1.2.4.2  ad #endif
    230  1.2.4.2  ad 			}
    231  1.2.4.2  ad 		}
    232  1.2.4.2  ad 	}
    233  1.2.4.2  ad 
    234  1.2.4.2  ad #ifdef DIAGNOSTIC
    235  1.2.4.2  ad 	if (level != ci->ci_ilevel)
    236  1.2.4.2  ad 		printf("hypervisor done %08x level %d/%d ipending %08x\n",
    237  1.2.4.2  ad #ifdef XEN3
    238  1.2.4.2  ad 		    (uint)HYPERVISOR_shared_info->vcpu_info[0].evtchn_pending_sel,
    239  1.2.4.2  ad #else
    240  1.2.4.2  ad 		    (uint)HYPERVISOR_shared_info->evtchn_pending_sel,
    241  1.2.4.2  ad #endif
    242  1.2.4.2  ad 		    level, ci->ci_ilevel, ci->ci_ipending);
    243  1.2.4.2  ad #endif
    244  1.2.4.2  ad }
    245  1.2.4.2  ad 
    246  1.2.4.2  ad void
    247  1.2.4.2  ad hypervisor_unmask_event(unsigned int ev)
    248  1.2.4.2  ad {
    249  1.2.4.2  ad 	volatile shared_info_t *s = HYPERVISOR_shared_info;
    250  1.2.4.2  ad #ifdef PORT_DEBUG
    251  1.2.4.2  ad 	if (ev == PORT_DEBUG)
    252  1.2.4.2  ad 		printf("hypervisor_unmask_event %d\n", ev);
    253  1.2.4.2  ad #endif
    254  1.2.4.2  ad 
    255  1.2.4.2  ad 	xen_atomic_clear_bit(&s->evtchn_mask[0], ev);
    256  1.2.4.2  ad 	/*
    257  1.2.4.2  ad 	 * The following is basically the equivalent of
    258  1.2.4.2  ad 	 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose the
    259  1.2.4.2  ad 	 * interrupt edge' if the channel is masked.
    260  1.2.4.2  ad 	 */
    261  1.2.4.2  ad 	if (xen_atomic_test_bit(&s->evtchn_pending[0], ev) &&
    262  1.2.4.2  ad #ifdef XEN3
    263  1.2.4.2  ad 	    !xen_atomic_test_and_set_bit(&s->vcpu_info[0].evtchn_pending_sel, ev>>5)) {
    264  1.2.4.2  ad #else
    265  1.2.4.2  ad 	    !xen_atomic_test_and_set_bit(&s->evtchn_pending_sel, ev>>5)) {
    266  1.2.4.2  ad #endif
    267  1.2.4.2  ad 		xen_atomic_set_bit(&s->vcpu_info[0].evtchn_upcall_pending, 0);
    268  1.2.4.2  ad 		if (!s->vcpu_info[0].evtchn_upcall_mask)
    269  1.2.4.2  ad 			hypervisor_force_callback();
    270  1.2.4.2  ad 	}
    271  1.2.4.2  ad }
    272  1.2.4.2  ad 
    273  1.2.4.2  ad void
    274  1.2.4.2  ad hypervisor_mask_event(unsigned int ev)
    275  1.2.4.2  ad {
    276  1.2.4.2  ad 	volatile shared_info_t *s = HYPERVISOR_shared_info;
    277  1.2.4.2  ad #ifdef PORT_DEBUG
    278  1.2.4.2  ad 	if (ev == PORT_DEBUG)
    279  1.2.4.2  ad 		printf("hypervisor_mask_event %d\n", ev);
    280  1.2.4.2  ad #endif
    281  1.2.4.2  ad 
    282  1.2.4.2  ad 	xen_atomic_set_bit(&s->evtchn_mask[0], ev);
    283  1.2.4.2  ad }
    284  1.2.4.2  ad 
    285  1.2.4.2  ad void
    286  1.2.4.2  ad hypervisor_clear_event(unsigned int ev)
    287  1.2.4.2  ad {
    288  1.2.4.2  ad 	volatile shared_info_t *s = HYPERVISOR_shared_info;
    289  1.2.4.2  ad #ifdef PORT_DEBUG
    290  1.2.4.2  ad 	if (ev == PORT_DEBUG)
    291  1.2.4.2  ad 		printf("hypervisor_clear_event %d\n", ev);
    292  1.2.4.2  ad #endif
    293  1.2.4.2  ad 
    294  1.2.4.2  ad 	xen_atomic_clear_bit(&s->evtchn_pending[0], ev);
    295  1.2.4.2  ad }
    296  1.2.4.2  ad 
    297  1.2.4.2  ad void
    298  1.2.4.2  ad hypervisor_enable_ipl(unsigned int ipl)
    299  1.2.4.2  ad {
    300  1.2.4.2  ad 	u_int32_t l1, l2;
    301  1.2.4.2  ad 	int l1i, l2i;
    302  1.2.4.2  ad 	struct cpu_info *ci = curcpu();
    303  1.2.4.2  ad 
    304  1.2.4.2  ad 	/*
    305  1.2.4.2  ad 	 * enable all events for ipl. As we only set an event in ipl_evt_mask
    306  1.2.4.2  ad 	 * for its lowest IPL, and pending IPLs are processed high to low,
    307  1.2.4.2  ad 	 * we know that all callback for this event have been processed.
    308  1.2.4.2  ad 	 */
    309  1.2.4.2  ad 
    310  1.2.4.2  ad 	l1 = ci->ci_isources[ipl]->ipl_evt_mask1;
    311  1.2.4.2  ad 	ci->ci_isources[ipl]->ipl_evt_mask1 = 0;
    312  1.2.4.2  ad 	while ((l1i = ffs(l1)) != 0) {
    313  1.2.4.2  ad 		l1i--;
    314  1.2.4.2  ad 		l1 &= ~(1 << l1i);
    315  1.2.4.2  ad 		l2 = ci->ci_isources[ipl]->ipl_evt_mask2[l1i];
    316  1.2.4.2  ad 		ci->ci_isources[ipl]->ipl_evt_mask2[l1i] = 0;
    317  1.2.4.2  ad 		while ((l2i = ffs(l2)) != 0) {
    318  1.2.4.2  ad 			int evtch;
    319  1.2.4.2  ad 
    320  1.2.4.2  ad 			l2i--;
    321  1.2.4.2  ad 			l2 &= ~(1 << l2i);
    322  1.2.4.2  ad 
    323  1.2.4.2  ad 			evtch = (l1i << 5) + l2i;
    324  1.2.4.2  ad 			hypervisor_enable_event(evtch);
    325  1.2.4.2  ad 		}
    326  1.2.4.2  ad 	}
    327  1.2.4.2  ad }
    328  1.2.4.2  ad 
    329  1.2.4.2  ad void
    330  1.2.4.2  ad hypervisor_set_ipending(u_int32_t iplmask, int l1, int l2)
    331  1.2.4.2  ad {
    332  1.2.4.2  ad 	int ipl;
    333  1.2.4.2  ad 	struct cpu_info *ci = curcpu();
    334  1.2.4.2  ad 
    335  1.2.4.2  ad 	/* set pending bit for the appropriate IPLs */
    336  1.2.4.2  ad 	ci->ci_ipending |= iplmask;
    337  1.2.4.2  ad 
    338  1.2.4.2  ad 	/*
    339  1.2.4.2  ad 	 * And set event pending bit for the lowest IPL. As IPL are handled
    340  1.2.4.2  ad 	 * from high to low, this ensure that all callbacks will have been
    341  1.2.4.2  ad 	 * called when we ack the event
    342  1.2.4.2  ad 	 */
    343  1.2.4.2  ad 	ipl = ffs(iplmask);
    344  1.2.4.2  ad 	KASSERT(ipl > 0);
    345  1.2.4.2  ad 	ipl--;
    346  1.2.4.2  ad 	ci->ci_isources[ipl]->ipl_evt_mask1 |= 1 << l1;
    347  1.2.4.2  ad 	ci->ci_isources[ipl]->ipl_evt_mask2[l1] |= 1 << l2;
    348  1.2.4.2  ad }
    349  1.2.4.2  ad /*	$NetBSD: hypervisor_machdep.c,v 1.2.4.2 2007/12/03 19:04:40 ad Exp $	*/
    350  1.2.4.2  ad 
    351  1.2.4.2  ad /*
    352  1.2.4.2  ad  *
    353  1.2.4.2  ad  * Copyright (c) 2004 Christian Limpach.
    354  1.2.4.2  ad  * All rights reserved.
    355  1.2.4.2  ad  *
    356  1.2.4.2  ad  * Redistribution and use in source and binary forms, with or without
    357  1.2.4.2  ad  * modification, are permitted provided that the following conditions
    358  1.2.4.2  ad  * are met:
    359  1.2.4.2  ad  * 1. Redistributions of source code must retain the above copyright
    360  1.2.4.2  ad  *    notice, this list of conditions and the following disclaimer.
    361  1.2.4.2  ad  * 2. Redistributions in binary form must reproduce the above copyright
    362  1.2.4.2  ad  *    notice, this list of conditions and the following disclaimer in the
    363  1.2.4.2  ad  *    documentation and/or other materials provided with the distribution.
    364  1.2.4.2  ad  * 3. All advertising materials mentioning features or use of this software
    365  1.2.4.2  ad  *    must display the following acknowledgement:
    366  1.2.4.2  ad  *      This product includes software developed by Christian Limpach.
    367  1.2.4.2  ad  * 4. The name of the author may not be used to endorse or promote products
    368  1.2.4.2  ad  *    derived from this software without specific prior written permission.
    369  1.2.4.2  ad  *
    370  1.2.4.2  ad  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
    371  1.2.4.2  ad  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    372  1.2.4.2  ad  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    373  1.2.4.2  ad  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
    374  1.2.4.2  ad  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
    375  1.2.4.2  ad  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    376  1.2.4.2  ad  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    377  1.2.4.2  ad  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    378  1.2.4.2  ad  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
    379  1.2.4.2  ad  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    380  1.2.4.2  ad  */
    381  1.2.4.2  ad 
    382  1.2.4.2  ad /******************************************************************************
    383  1.2.4.2  ad  * hypervisor.c
    384  1.2.4.2  ad  *
    385  1.2.4.2  ad  * Communication to/from hypervisor.
    386  1.2.4.2  ad  *
    387  1.2.4.2  ad  * Copyright (c) 2002-2004, K A Fraser
    388  1.2.4.2  ad  *
    389  1.2.4.2  ad  * Permission is hereby granted, free of charge, to any person obtaining a copy
    390  1.2.4.2  ad  * of this software and associated documentation files (the "Software"), to
    391  1.2.4.2  ad  * deal in the Software without restriction, including without limitation the
    392  1.2.4.2  ad  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
    393  1.2.4.2  ad  * sell copies of the Software, and to permit persons to whom the Software is
    394  1.2.4.2  ad  * furnished to do so, subject to the following conditions:
    395  1.2.4.2  ad  *
    396  1.2.4.2  ad  * The above copyright notice and this permission notice shall be included in
    397  1.2.4.2  ad  * all copies or substantial portions of the Software.
    398  1.2.4.2  ad  *
    399  1.2.4.2  ad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    400  1.2.4.2  ad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    401  1.2.4.2  ad  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    402  1.2.4.2  ad  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    403  1.2.4.2  ad  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    404  1.2.4.2  ad  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
    405  1.2.4.2  ad  * DEALINGS IN THE SOFTWARE.
    406  1.2.4.2  ad  */
    407  1.2.4.2  ad 
    408  1.2.4.2  ad 
    409  1.2.4.2  ad #include <sys/cdefs.h>
    410  1.2.4.2  ad __KERNEL_RCSID(0, "$NetBSD: hypervisor_machdep.c,v 1.2.4.2 2007/12/03 19:04:40 ad Exp $");
    411  1.2.4.2  ad 
    412  1.2.4.2  ad #include <sys/param.h>
    413  1.2.4.2  ad #include <sys/systm.h>
    414  1.2.4.2  ad 
    415  1.2.4.2  ad #include <xen/xen.h>
    416  1.2.4.2  ad #include <xen/hypervisor.h>
    417  1.2.4.2  ad #include <xen/evtchn.h>
    418  1.2.4.2  ad #include <machine/atomic.h>
    419  1.2.4.2  ad 
    420  1.2.4.2  ad #include "opt_xen.h"
    421  1.2.4.2  ad 
    422  1.2.4.2  ad // #define PORT_DEBUG 4
    423  1.2.4.2  ad // #define EARLY_DEBUG_EVENT
    424  1.2.4.2  ad 
    425  1.2.4.2  ad #ifdef EARLY_DEBUG_EVENT
    426  1.2.4.2  ad extern int debug_port;
    427  1.2.4.2  ad extern int xen_debug_handler(void *);
    428  1.2.4.2  ad #endif
    429  1.2.4.2  ad 
    430  1.2.4.2  ad int stipending(void);
    431  1.2.4.2  ad int
    432  1.2.4.2  ad stipending()
    433  1.2.4.2  ad {
    434  1.2.4.2  ad 	uint32_t l1;
    435  1.2.4.2  ad 	unsigned long l2;
    436  1.2.4.2  ad 	unsigned int l1i, l2i, port;
    437  1.2.4.2  ad 	volatile shared_info_t *s = HYPERVISOR_shared_info;
    438  1.2.4.2  ad 	struct cpu_info *ci;
    439  1.2.4.2  ad 	int ret;
    440  1.2.4.2  ad 
    441  1.2.4.2  ad 	ret = 0;
    442  1.2.4.2  ad 	ci = curcpu();
    443  1.2.4.2  ad 
    444  1.2.4.2  ad #if 0
    445  1.2.4.2  ad 	if (HYPERVISOR_shared_info->events)
    446  1.2.4.2  ad 		printf("stipending events %08lx mask %08lx ilevel %d\n",
    447  1.2.4.2  ad 		    HYPERVISOR_shared_info->events,
    448  1.2.4.2  ad 		    HYPERVISOR_shared_info->events_mask, ci->ci_ilevel);
    449  1.2.4.2  ad #endif
    450  1.2.4.2  ad 
    451  1.2.4.2  ad #ifdef EARLY_DEBUG_EVENT
    452  1.2.4.2  ad 	if (xen_atomic_test_bit(&s->evtchn_pending[0], debug_port)) {
    453  1.2.4.2  ad 		xen_debug_handler(NULL);
    454  1.2.4.2  ad 		xen_atomic_clear_bit(&s->evtchn_pending[0], debug_port);
    455  1.2.4.2  ad 	}
    456  1.2.4.2  ad #endif
    457  1.2.4.2  ad 
    458  1.2.4.2  ad 	/*
    459  1.2.4.2  ad 	 * we're only called after STIC, so we know that we'll have to
    460  1.2.4.2  ad 	 * STI at the end
    461  1.2.4.2  ad 	 */
    462  1.2.4.2  ad 	while (s->vcpu_info[0].evtchn_upcall_pending) {
    463  1.2.4.2  ad 		cli();
    464  1.2.4.2  ad 		s->vcpu_info[0].evtchn_upcall_pending = 0;
    465  1.2.4.2  ad 		/* NB. No need for a barrier here -- XCHG is a barrier
    466  1.2.4.2  ad 		 * on x86. */
    467  1.2.4.2  ad #ifdef XEN3
    468  1.2.4.2  ad 		l1 = xen_atomic_xchg(&s->vcpu_info[0].evtchn_pending_sel, 0);
    469  1.2.4.2  ad #else
    470  1.2.4.2  ad 		l1 = xen_atomic_xchg(&s->evtchn_pending_sel, 0);
    471  1.2.4.2  ad #endif
    472  1.2.4.2  ad 		while ((l1i = ffs(l1)) != 0) {
    473  1.2.4.2  ad 			l1i--;
    474  1.2.4.2  ad 			l1 &= ~(1 << l1i);
    475  1.2.4.2  ad 
    476  1.2.4.2  ad 			l2 = s->evtchn_pending[l1i] & ~s->evtchn_mask[l1i];
    477  1.2.4.2  ad 			/*
    478  1.2.4.2  ad 			 * mask and clear event. More efficient than calling
    479  1.2.4.2  ad 			 * hypervisor_mask/clear_event for each event.
    480  1.2.4.2  ad 			 */
    481  1.2.4.2  ad 			xen_atomic_setbits_l(&s->evtchn_mask[l1i], l2);
    482  1.2.4.2  ad 			xen_atomic_clearbits_l(&s->evtchn_pending[l1i], l2);
    483  1.2.4.2  ad 			while ((l2i = ffs(l2)) != 0) {
    484  1.2.4.2  ad 				l2i--;
    485  1.2.4.2  ad 				l2 &= ~(1 << l2i);
    486  1.2.4.2  ad 
    487  1.2.4.2  ad 				port = (l1i << 5) + l2i;
    488  1.2.4.2  ad 				if (evtsource[port]) {
    489  1.2.4.2  ad 					hypervisor_set_ipending(
    490  1.2.4.2  ad 					    evtsource[port]->ev_imask,
    491  1.2.4.2  ad 					    l1i, l2i);
    492  1.2.4.2  ad 					evtsource[port]->ev_evcnt.ev_count++;
    493  1.2.4.2  ad 					if (ret == 0 && ci->ci_ilevel <
    494  1.2.4.2  ad 					    evtsource[port]->ev_maxlevel)
    495  1.2.4.2  ad 						ret = 1;
    496  1.2.4.2  ad 				}
    497  1.2.4.2  ad #ifdef DOM0OPS
    498  1.2.4.2  ad 				else
    499  1.2.4.2  ad 					xenevt_event(port);
    500  1.2.4.2  ad #endif
    501  1.2.4.2  ad 			}
    502  1.2.4.2  ad 		}
    503  1.2.4.2  ad 		sti();
    504  1.2.4.2  ad 	}
    505  1.2.4.2  ad 
    506  1.2.4.2  ad #if 0
    507  1.2.4.2  ad 	if (ci->ci_ipending & 0x1)
    508  1.2.4.2  ad 		printf("stipending events %08lx mask %08lx ilevel %d ipending %08x\n",
    509  1.2.4.2  ad 		    HYPERVISOR_shared_info->events,
    510  1.2.4.2  ad 		    HYPERVISOR_shared_info->events_mask, ci->ci_ilevel,
    511  1.2.4.2  ad 		    ci->ci_ipending);
    512  1.2.4.2  ad #endif
    513  1.2.4.2  ad 
    514  1.2.4.2  ad 	return (ret);
    515  1.2.4.2  ad }
    516  1.2.4.2  ad 
    517  1.2.4.2  ad void
    518  1.2.4.2  ad do_hypervisor_callback(struct intrframe *regs)
    519  1.2.4.2  ad {
    520  1.2.4.2  ad 	uint32_t l1;
    521  1.2.4.2  ad 	unsigned long l2;
    522  1.2.4.2  ad 	unsigned int l1i, l2i, port;
    523  1.2.4.2  ad 	volatile shared_info_t *s = HYPERVISOR_shared_info;
    524  1.2.4.2  ad 	struct cpu_info *ci;
    525  1.2.4.2  ad 	int level;
    526  1.2.4.2  ad 
    527  1.2.4.2  ad 	ci = curcpu();
    528  1.2.4.2  ad 	level = ci->ci_ilevel;
    529  1.2.4.2  ad 
    530  1.2.4.2  ad 	// DDD printf("do_hypervisor_callback\n");
    531  1.2.4.2  ad 
    532  1.2.4.2  ad #ifdef EARLY_DEBUG_EVENT
    533  1.2.4.2  ad 	if (xen_atomic_test_bit(&s->evtchn_pending[0], debug_port)) {
    534  1.2.4.2  ad 		xen_debug_handler(NULL);
    535  1.2.4.2  ad 		xen_atomic_clear_bit(&s->evtchn_pending[0], debug_port);
    536  1.2.4.2  ad 	}
    537  1.2.4.2  ad #endif
    538  1.2.4.2  ad 
    539  1.2.4.2  ad 	while (s->vcpu_info[0].evtchn_upcall_pending) {
    540  1.2.4.2  ad 		s->vcpu_info[0].evtchn_upcall_pending = 0;
    541  1.2.4.2  ad 		/* NB. No need for a barrier here -- XCHG is a barrier
    542  1.2.4.2  ad 		 * on x86. */
    543  1.2.4.2  ad #ifdef XEN3
    544  1.2.4.2  ad 		l1 = xen_atomic_xchg(&s->vcpu_info[0].evtchn_pending_sel, 0);
    545  1.2.4.2  ad #else
    546  1.2.4.2  ad 		l1 = xen_atomic_xchg(&s->evtchn_pending_sel, 0);
    547  1.2.4.2  ad #endif
    548  1.2.4.2  ad 		while ((l1i = ffs(l1)) != 0) {
    549  1.2.4.2  ad 			l1i--;
    550  1.2.4.2  ad 			l1 &= ~(1 << l1i);
    551  1.2.4.2  ad 
    552  1.2.4.2  ad 			l2 = s->evtchn_pending[l1i] & ~s->evtchn_mask[l1i];
    553  1.2.4.2  ad 			/*
    554  1.2.4.2  ad 			 * mask and clear the pending events.
    555  1.2.4.2  ad 			 * Doing it here for all event that will be processed
    556  1.2.4.2  ad 			 * avoids a race with stipending (which can be called
    557  1.2.4.2  ad 			 * though evtchn_do_event->splx) that could cause an event to
    558  1.2.4.2  ad 			 * be both processed and marked pending.
    559  1.2.4.2  ad 			 */
    560  1.2.4.2  ad 			xen_atomic_setbits_l(&s->evtchn_mask[l1i], l2);
    561  1.2.4.2  ad 			xen_atomic_clearbits_l(&s->evtchn_pending[l1i], l2);
    562  1.2.4.2  ad 
    563  1.2.4.2  ad 			while ((l2i = ffs(l2)) != 0) {
    564  1.2.4.2  ad 				l2i--;
    565  1.2.4.2  ad 				l2 &= ~(1 << l2i);
    566  1.2.4.2  ad 
    567  1.2.4.2  ad 				port = (l1i << 5) + l2i;
    568  1.2.4.2  ad #ifdef PORT_DEBUG
    569  1.2.4.2  ad 				if (port == PORT_DEBUG)
    570  1.2.4.2  ad 					printf("do_hypervisor_callback event %d\n", port);
    571  1.2.4.2  ad #endif
    572  1.2.4.2  ad 				if (evtsource[port])
    573  1.2.4.2  ad 					call_evtchn_do_event(port, regs);
    574  1.2.4.2  ad #ifdef DOM0OPS
    575  1.2.4.2  ad 				else
    576  1.2.4.2  ad 					xenevt_event(port);
    577  1.2.4.2  ad #endif
    578  1.2.4.2  ad 			}
    579  1.2.4.2  ad 		}
    580  1.2.4.2  ad 	}
    581  1.2.4.2  ad 
    582  1.2.4.2  ad #ifdef DIAGNOSTIC
    583  1.2.4.2  ad 	if (level != ci->ci_ilevel)
    584  1.2.4.2  ad 		printf("hypervisor done %08x level %d/%d ipending %08x\n",
    585  1.2.4.2  ad #ifdef XEN3
    586  1.2.4.2  ad 		    (uint)HYPERVISOR_shared_info->vcpu_info[0].evtchn_pending_sel,
    587  1.2.4.2  ad #else
    588  1.2.4.2  ad 		    (uint)HYPERVISOR_shared_info->evtchn_pending_sel,
    589  1.2.4.2  ad #endif
    590  1.2.4.2  ad 		    level, ci->ci_ilevel, ci->ci_ipending);
    591  1.2.4.2  ad #endif
    592  1.2.4.2  ad }
    593  1.2.4.2  ad 
    594  1.2.4.2  ad void
    595  1.2.4.2  ad hypervisor_unmask_event(unsigned int ev)
    596  1.2.4.2  ad {
    597  1.2.4.2  ad 	volatile shared_info_t *s = HYPERVISOR_shared_info;
    598  1.2.4.2  ad #ifdef PORT_DEBUG
    599  1.2.4.2  ad 	if (ev == PORT_DEBUG)
    600  1.2.4.2  ad 		printf("hypervisor_unmask_event %d\n", ev);
    601  1.2.4.2  ad #endif
    602  1.2.4.2  ad 
    603  1.2.4.2  ad 	xen_atomic_clear_bit(&s->evtchn_mask[0], ev);
    604  1.2.4.2  ad 	/*
    605  1.2.4.2  ad 	 * The following is basically the equivalent of
    606  1.2.4.2  ad 	 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose the
    607  1.2.4.2  ad 	 * interrupt edge' if the channel is masked.
    608  1.2.4.2  ad 	 */
    609  1.2.4.2  ad 	if (xen_atomic_test_bit(&s->evtchn_pending[0], ev) &&
    610  1.2.4.2  ad #ifdef XEN3
    611  1.2.4.2  ad 	    !xen_atomic_test_and_set_bit(&s->vcpu_info[0].evtchn_pending_sel, ev>>5)) {
    612  1.2.4.2  ad #else
    613  1.2.4.2  ad 	    !xen_atomic_test_and_set_bit(&s->evtchn_pending_sel, ev>>5)) {
    614  1.2.4.2  ad #endif
    615  1.2.4.2  ad 		xen_atomic_set_bit(&s->vcpu_info[0].evtchn_upcall_pending, 0);
    616  1.2.4.2  ad 		if (!s->vcpu_info[0].evtchn_upcall_mask)
    617  1.2.4.2  ad 			hypervisor_force_callback();
    618  1.2.4.2  ad 	}
    619  1.2.4.2  ad }
    620  1.2.4.2  ad 
    621  1.2.4.2  ad void
    622  1.2.4.2  ad hypervisor_mask_event(unsigned int ev)
    623  1.2.4.2  ad {
    624  1.2.4.2  ad 	volatile shared_info_t *s = HYPERVISOR_shared_info;
    625  1.2.4.2  ad #ifdef PORT_DEBUG
    626  1.2.4.2  ad 	if (ev == PORT_DEBUG)
    627  1.2.4.2  ad 		printf("hypervisor_mask_event %d\n", ev);
    628  1.2.4.2  ad #endif
    629  1.2.4.2  ad 
    630  1.2.4.2  ad 	xen_atomic_set_bit(&s->evtchn_mask[0], ev);
    631  1.2.4.2  ad }
    632  1.2.4.2  ad 
    633  1.2.4.2  ad void
    634  1.2.4.2  ad hypervisor_clear_event(unsigned int ev)
    635  1.2.4.2  ad {
    636  1.2.4.2  ad 	volatile shared_info_t *s = HYPERVISOR_shared_info;
    637  1.2.4.2  ad #ifdef PORT_DEBUG
    638  1.2.4.2  ad 	if (ev == PORT_DEBUG)
    639  1.2.4.2  ad 		printf("hypervisor_clear_event %d\n", ev);
    640  1.2.4.2  ad #endif
    641  1.2.4.2  ad 
    642  1.2.4.2  ad 	xen_atomic_clear_bit(&s->evtchn_pending[0], ev);
    643  1.2.4.2  ad }
    644  1.2.4.2  ad 
    645  1.2.4.2  ad void
    646  1.2.4.2  ad hypervisor_enable_ipl(unsigned int ipl)
    647  1.2.4.2  ad {
    648  1.2.4.2  ad 	u_int32_t l1, l2;
    649  1.2.4.2  ad 	int l1i, l2i;
    650  1.2.4.2  ad 	struct cpu_info *ci = curcpu();
    651  1.2.4.2  ad 
    652  1.2.4.2  ad 	/*
    653  1.2.4.2  ad 	 * enable all events for ipl. As we only set an event in ipl_evt_mask
    654  1.2.4.2  ad 	 * for its lowest IPL, and pending IPLs are processed high to low,
    655  1.2.4.2  ad 	 * we know that all callback for this event have been processed.
    656  1.2.4.2  ad 	 */
    657  1.2.4.2  ad 
    658  1.2.4.2  ad 	l1 = ci->ci_isources[ipl]->ipl_evt_mask1;
    659  1.2.4.2  ad 	ci->ci_isources[ipl]->ipl_evt_mask1 = 0;
    660  1.2.4.2  ad 	while ((l1i = ffs(l1)) != 0) {
    661  1.2.4.2  ad 		l1i--;
    662  1.2.4.2  ad 		l1 &= ~(1 << l1i);
    663  1.2.4.2  ad 		l2 = ci->ci_isources[ipl]->ipl_evt_mask2[l1i];
    664  1.2.4.2  ad 		ci->ci_isources[ipl]->ipl_evt_mask2[l1i] = 0;
    665  1.2.4.2  ad 		while ((l2i = ffs(l2)) != 0) {
    666  1.2.4.2  ad 			int evtch;
    667  1.2.4.2  ad 
    668  1.2.4.2  ad 			l2i--;
    669  1.2.4.2  ad 			l2 &= ~(1 << l2i);
    670  1.2.4.2  ad 
    671  1.2.4.2  ad 			evtch = (l1i << 5) + l2i;
    672  1.2.4.2  ad 			hypervisor_enable_event(evtch);
    673  1.2.4.2  ad 		}
    674  1.2.4.2  ad 	}
    675  1.2.4.2  ad }
    676  1.2.4.2  ad 
    677  1.2.4.2  ad void
    678  1.2.4.2  ad hypervisor_set_ipending(u_int32_t iplmask, int l1, int l2)
    679  1.2.4.2  ad {
    680  1.2.4.2  ad 	int ipl;
    681  1.2.4.2  ad 	struct cpu_info *ci = curcpu();
    682  1.2.4.2  ad 
    683  1.2.4.2  ad 	/* set pending bit for the appropriate IPLs */
    684  1.2.4.2  ad 	ci->ci_ipending |= iplmask;
    685  1.2.4.2  ad 
    686  1.2.4.2  ad 	/*
    687  1.2.4.2  ad 	 * And set event pending bit for the lowest IPL. As IPL are handled
    688  1.2.4.2  ad 	 * from high to low, this ensure that all callbacks will have been
    689  1.2.4.2  ad 	 * called when we ack the event
    690  1.2.4.2  ad 	 */
    691  1.2.4.2  ad 	ipl = ffs(iplmask);
    692  1.2.4.2  ad 	KASSERT(ipl > 0);
    693  1.2.4.2  ad 	ipl--;
    694  1.2.4.2  ad 	ci->ci_isources[ipl]->ipl_evt_mask1 |= 1 << l1;
    695  1.2.4.2  ad 	ci->ci_isources[ipl]->ipl_evt_mask2[l1] |= 1 << l2;
    696  1.2.4.2  ad }
    697