Home | History | Annotate | Line # | Download | only in x86
hypervisor_machdep.c revision 1.2.10.1
      1  1.2.10.1    yamt /*	$NetBSD: hypervisor_machdep.c,v 1.2.10.1 2007/12/13 05:05:24 yamt Exp $	*/
      2       1.2  bouyer 
      3       1.2  bouyer /*
      4       1.2  bouyer  *
      5       1.2  bouyer  * Copyright (c) 2004 Christian Limpach.
      6       1.2  bouyer  * All rights reserved.
      7       1.2  bouyer  *
      8       1.2  bouyer  * Redistribution and use in source and binary forms, with or without
      9       1.2  bouyer  * modification, are permitted provided that the following conditions
     10       1.2  bouyer  * are met:
     11       1.2  bouyer  * 1. Redistributions of source code must retain the above copyright
     12       1.2  bouyer  *    notice, this list of conditions and the following disclaimer.
     13       1.2  bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     14       1.2  bouyer  *    notice, this list of conditions and the following disclaimer in the
     15       1.2  bouyer  *    documentation and/or other materials provided with the distribution.
     16       1.2  bouyer  * 3. All advertising materials mentioning features or use of this software
     17       1.2  bouyer  *    must display the following acknowledgement:
     18       1.2  bouyer  *      This product includes software developed by Christian Limpach.
     19       1.2  bouyer  * 4. The name of the author may not be used to endorse or promote products
     20       1.2  bouyer  *    derived from this software without specific prior written permission.
     21       1.2  bouyer  *
     22       1.2  bouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23       1.2  bouyer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24       1.2  bouyer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25       1.2  bouyer  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26       1.2  bouyer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27       1.2  bouyer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28       1.2  bouyer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29       1.2  bouyer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30       1.2  bouyer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31       1.2  bouyer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32       1.2  bouyer  */
     33       1.2  bouyer 
     34       1.2  bouyer /******************************************************************************
     35       1.2  bouyer  * hypervisor.c
     36       1.2  bouyer  *
     37       1.2  bouyer  * Communication to/from hypervisor.
     38       1.2  bouyer  *
     39       1.2  bouyer  * Copyright (c) 2002-2004, K A Fraser
     40       1.2  bouyer  *
     41       1.2  bouyer  * Permission is hereby granted, free of charge, to any person obtaining a copy
     42       1.2  bouyer  * of this software and associated documentation files (the "Software"), to
     43       1.2  bouyer  * deal in the Software without restriction, including without limitation the
     44       1.2  bouyer  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
     45       1.2  bouyer  * sell copies of the Software, and to permit persons to whom the Software is
     46       1.2  bouyer  * furnished to do so, subject to the following conditions:
     47       1.2  bouyer  *
     48       1.2  bouyer  * The above copyright notice and this permission notice shall be included in
     49       1.2  bouyer  * all copies or substantial portions of the Software.
     50       1.2  bouyer  *
     51       1.2  bouyer  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     52       1.2  bouyer  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     53       1.2  bouyer  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     54       1.2  bouyer  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     55       1.2  bouyer  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     56       1.2  bouyer  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
     57       1.2  bouyer  * DEALINGS IN THE SOFTWARE.
     58       1.2  bouyer  */
     59       1.2  bouyer 
     60       1.2  bouyer 
     61       1.2  bouyer #include <sys/cdefs.h>
     62  1.2.10.1    yamt __KERNEL_RCSID(0, "$NetBSD: hypervisor_machdep.c,v 1.2.10.1 2007/12/13 05:05:24 yamt Exp $");
     63       1.2  bouyer 
     64       1.2  bouyer #include <sys/param.h>
     65       1.2  bouyer #include <sys/systm.h>
     66       1.2  bouyer 
     67       1.2  bouyer #include <xen/xen.h>
     68       1.2  bouyer #include <xen/hypervisor.h>
     69       1.2  bouyer #include <xen/evtchn.h>
     70       1.2  bouyer #include <machine/atomic.h>
     71       1.2  bouyer 
     72       1.2  bouyer #include "opt_xen.h"
     73       1.2  bouyer 
     74       1.2  bouyer // #define PORT_DEBUG 4
     75       1.2  bouyer // #define EARLY_DEBUG_EVENT
     76       1.2  bouyer 
     77       1.2  bouyer int stipending(void);
     78       1.2  bouyer int
     79       1.2  bouyer stipending()
     80       1.2  bouyer {
     81       1.2  bouyer 	uint32_t l1;
     82       1.2  bouyer 	unsigned long l2;
     83       1.2  bouyer 	unsigned int l1i, l2i, port;
     84       1.2  bouyer 	volatile shared_info_t *s = HYPERVISOR_shared_info;
     85       1.2  bouyer 	struct cpu_info *ci;
     86       1.2  bouyer 	int ret;
     87       1.2  bouyer 
     88       1.2  bouyer 	ret = 0;
     89       1.2  bouyer 	ci = curcpu();
     90       1.2  bouyer 
     91       1.2  bouyer #if 0
     92       1.2  bouyer 	if (HYPERVISOR_shared_info->events)
     93       1.2  bouyer 		printf("stipending events %08lx mask %08lx ilevel %d\n",
     94       1.2  bouyer 		    HYPERVISOR_shared_info->events,
     95       1.2  bouyer 		    HYPERVISOR_shared_info->events_mask, ci->ci_ilevel);
     96       1.2  bouyer #endif
     97       1.2  bouyer 
     98       1.2  bouyer #ifdef EARLY_DEBUG_EVENT
     99       1.2  bouyer 	if (xen_atomic_test_bit(&s->evtchn_pending[0], debug_port)) {
    100       1.2  bouyer 		xen_debug_handler(NULL);
    101       1.2  bouyer 		xen_atomic_clear_bit(&s->evtchn_pending[0], debug_port);
    102       1.2  bouyer 	}
    103       1.2  bouyer #endif
    104       1.2  bouyer 
    105       1.2  bouyer 	/*
    106       1.2  bouyer 	 * we're only called after STIC, so we know that we'll have to
    107       1.2  bouyer 	 * STI at the end
    108       1.2  bouyer 	 */
    109       1.2  bouyer 	while (s->vcpu_info[0].evtchn_upcall_pending) {
    110       1.2  bouyer 		cli();
    111       1.2  bouyer 		s->vcpu_info[0].evtchn_upcall_pending = 0;
    112       1.2  bouyer 		/* NB. No need for a barrier here -- XCHG is a barrier
    113       1.2  bouyer 		 * on x86. */
    114       1.2  bouyer #ifdef XEN3
    115       1.2  bouyer 		l1 = xen_atomic_xchg(&s->vcpu_info[0].evtchn_pending_sel, 0);
    116       1.2  bouyer #else
    117       1.2  bouyer 		l1 = xen_atomic_xchg(&s->evtchn_pending_sel, 0);
    118       1.2  bouyer #endif
    119       1.2  bouyer 		while ((l1i = ffs(l1)) != 0) {
    120       1.2  bouyer 			l1i--;
    121       1.2  bouyer 			l1 &= ~(1 << l1i);
    122       1.2  bouyer 
    123       1.2  bouyer 			l2 = s->evtchn_pending[l1i] & ~s->evtchn_mask[l1i];
    124       1.2  bouyer 			/*
    125       1.2  bouyer 			 * mask and clear event. More efficient than calling
    126       1.2  bouyer 			 * hypervisor_mask/clear_event for each event.
    127       1.2  bouyer 			 */
    128       1.2  bouyer 			xen_atomic_setbits_l(&s->evtchn_mask[l1i], l2);
    129       1.2  bouyer 			xen_atomic_clearbits_l(&s->evtchn_pending[l1i], l2);
    130       1.2  bouyer 			while ((l2i = ffs(l2)) != 0) {
    131       1.2  bouyer 				l2i--;
    132       1.2  bouyer 				l2 &= ~(1 << l2i);
    133       1.2  bouyer 
    134       1.2  bouyer 				port = (l1i << 5) + l2i;
    135       1.2  bouyer 				if (evtsource[port]) {
    136       1.2  bouyer 					hypervisor_set_ipending(
    137       1.2  bouyer 					    evtsource[port]->ev_imask,
    138       1.2  bouyer 					    l1i, l2i);
    139       1.2  bouyer 					evtsource[port]->ev_evcnt.ev_count++;
    140       1.2  bouyer 					if (ret == 0 && ci->ci_ilevel <
    141       1.2  bouyer 					    evtsource[port]->ev_maxlevel)
    142       1.2  bouyer 						ret = 1;
    143       1.2  bouyer 				}
    144       1.2  bouyer #ifdef DOM0OPS
    145       1.2  bouyer 				else
    146       1.2  bouyer 					xenevt_event(port);
    147       1.2  bouyer #endif
    148       1.2  bouyer 			}
    149       1.2  bouyer 		}
    150       1.2  bouyer 		sti();
    151       1.2  bouyer 	}
    152       1.2  bouyer 
    153       1.2  bouyer #if 0
    154       1.2  bouyer 	if (ci->ci_ipending & 0x1)
    155       1.2  bouyer 		printf("stipending events %08lx mask %08lx ilevel %d ipending %08x\n",
    156       1.2  bouyer 		    HYPERVISOR_shared_info->events,
    157       1.2  bouyer 		    HYPERVISOR_shared_info->events_mask, ci->ci_ilevel,
    158       1.2  bouyer 		    ci->ci_ipending);
    159       1.2  bouyer #endif
    160       1.2  bouyer 
    161       1.2  bouyer 	return (ret);
    162       1.2  bouyer }
    163       1.2  bouyer 
    164       1.2  bouyer void
    165       1.2  bouyer do_hypervisor_callback(struct intrframe *regs)
    166       1.2  bouyer {
    167       1.2  bouyer 	uint32_t l1;
    168       1.2  bouyer 	unsigned long l2;
    169       1.2  bouyer 	unsigned int l1i, l2i, port;
    170       1.2  bouyer 	volatile shared_info_t *s = HYPERVISOR_shared_info;
    171       1.2  bouyer 	struct cpu_info *ci;
    172       1.2  bouyer 	int level;
    173       1.2  bouyer 
    174       1.2  bouyer 	ci = curcpu();
    175       1.2  bouyer 	level = ci->ci_ilevel;
    176       1.2  bouyer 
    177       1.2  bouyer 	// DDD printf("do_hypervisor_callback\n");
    178       1.2  bouyer 
    179       1.2  bouyer #ifdef EARLY_DEBUG_EVENT
    180       1.2  bouyer 	if (xen_atomic_test_bit(&s->evtchn_pending[0], debug_port)) {
    181       1.2  bouyer 		xen_debug_handler(NULL);
    182       1.2  bouyer 		xen_atomic_clear_bit(&s->evtchn_pending[0], debug_port);
    183       1.2  bouyer 	}
    184       1.2  bouyer #endif
    185       1.2  bouyer 
    186       1.2  bouyer 	while (s->vcpu_info[0].evtchn_upcall_pending) {
    187       1.2  bouyer 		s->vcpu_info[0].evtchn_upcall_pending = 0;
    188       1.2  bouyer 		/* NB. No need for a barrier here -- XCHG is a barrier
    189       1.2  bouyer 		 * on x86. */
    190       1.2  bouyer #ifdef XEN3
    191       1.2  bouyer 		l1 = xen_atomic_xchg(&s->vcpu_info[0].evtchn_pending_sel, 0);
    192       1.2  bouyer #else
    193       1.2  bouyer 		l1 = xen_atomic_xchg(&s->evtchn_pending_sel, 0);
    194       1.2  bouyer #endif
    195       1.2  bouyer 		while ((l1i = ffs(l1)) != 0) {
    196       1.2  bouyer 			l1i--;
    197       1.2  bouyer 			l1 &= ~(1 << l1i);
    198       1.2  bouyer 
    199       1.2  bouyer 			l2 = s->evtchn_pending[l1i] & ~s->evtchn_mask[l1i];
    200       1.2  bouyer 			/*
    201       1.2  bouyer 			 * mask and clear the pending events.
    202       1.2  bouyer 			 * Doing it here for all event that will be processed
    203       1.2  bouyer 			 * avoids a race with stipending (which can be called
    204       1.2  bouyer 			 * though evtchn_do_event->splx) that could cause an event to
    205       1.2  bouyer 			 * be both processed and marked pending.
    206       1.2  bouyer 			 */
    207       1.2  bouyer 			xen_atomic_setbits_l(&s->evtchn_mask[l1i], l2);
    208       1.2  bouyer 			xen_atomic_clearbits_l(&s->evtchn_pending[l1i], l2);
    209       1.2  bouyer 
    210       1.2  bouyer 			while ((l2i = ffs(l2)) != 0) {
    211       1.2  bouyer 				l2i--;
    212       1.2  bouyer 				l2 &= ~(1 << l2i);
    213       1.2  bouyer 
    214       1.2  bouyer 				port = (l1i << 5) + l2i;
    215       1.2  bouyer #ifdef PORT_DEBUG
    216       1.2  bouyer 				if (port == PORT_DEBUG)
    217       1.2  bouyer 					printf("do_hypervisor_callback event %d\n", port);
    218       1.2  bouyer #endif
    219       1.2  bouyer 				if (evtsource[port])
    220       1.2  bouyer 					call_evtchn_do_event(port, regs);
    221       1.2  bouyer #ifdef DOM0OPS
    222       1.2  bouyer 				else
    223       1.2  bouyer 					xenevt_event(port);
    224       1.2  bouyer #endif
    225       1.2  bouyer 			}
    226       1.2  bouyer 		}
    227       1.2  bouyer 	}
    228       1.2  bouyer 
    229       1.2  bouyer #ifdef DIAGNOSTIC
    230       1.2  bouyer 	if (level != ci->ci_ilevel)
    231       1.2  bouyer 		printf("hypervisor done %08x level %d/%d ipending %08x\n",
    232       1.2  bouyer #ifdef XEN3
    233       1.2  bouyer 		    (uint)HYPERVISOR_shared_info->vcpu_info[0].evtchn_pending_sel,
    234       1.2  bouyer #else
    235       1.2  bouyer 		    (uint)HYPERVISOR_shared_info->evtchn_pending_sel,
    236       1.2  bouyer #endif
    237       1.2  bouyer 		    level, ci->ci_ilevel, ci->ci_ipending);
    238       1.2  bouyer #endif
    239       1.2  bouyer }
    240       1.2  bouyer 
    241       1.2  bouyer void
    242       1.2  bouyer hypervisor_unmask_event(unsigned int ev)
    243       1.2  bouyer {
    244       1.2  bouyer 	volatile shared_info_t *s = HYPERVISOR_shared_info;
    245       1.2  bouyer #ifdef PORT_DEBUG
    246       1.2  bouyer 	if (ev == PORT_DEBUG)
    247       1.2  bouyer 		printf("hypervisor_unmask_event %d\n", ev);
    248       1.2  bouyer #endif
    249       1.2  bouyer 
    250       1.2  bouyer 	xen_atomic_clear_bit(&s->evtchn_mask[0], ev);
    251       1.2  bouyer 	/*
    252       1.2  bouyer 	 * The following is basically the equivalent of
    253       1.2  bouyer 	 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose the
    254       1.2  bouyer 	 * interrupt edge' if the channel is masked.
    255       1.2  bouyer 	 */
    256       1.2  bouyer 	if (xen_atomic_test_bit(&s->evtchn_pending[0], ev) &&
    257       1.2  bouyer #ifdef XEN3
    258       1.2  bouyer 	    !xen_atomic_test_and_set_bit(&s->vcpu_info[0].evtchn_pending_sel, ev>>5)) {
    259       1.2  bouyer #else
    260       1.2  bouyer 	    !xen_atomic_test_and_set_bit(&s->evtchn_pending_sel, ev>>5)) {
    261       1.2  bouyer #endif
    262       1.2  bouyer 		xen_atomic_set_bit(&s->vcpu_info[0].evtchn_upcall_pending, 0);
    263       1.2  bouyer 		if (!s->vcpu_info[0].evtchn_upcall_mask)
    264       1.2  bouyer 			hypervisor_force_callback();
    265       1.2  bouyer 	}
    266       1.2  bouyer }
    267       1.2  bouyer 
    268       1.2  bouyer void
    269       1.2  bouyer hypervisor_mask_event(unsigned int ev)
    270       1.2  bouyer {
    271       1.2  bouyer 	volatile shared_info_t *s = HYPERVISOR_shared_info;
    272       1.2  bouyer #ifdef PORT_DEBUG
    273       1.2  bouyer 	if (ev == PORT_DEBUG)
    274       1.2  bouyer 		printf("hypervisor_mask_event %d\n", ev);
    275       1.2  bouyer #endif
    276       1.2  bouyer 
    277       1.2  bouyer 	xen_atomic_set_bit(&s->evtchn_mask[0], ev);
    278       1.2  bouyer }
    279       1.2  bouyer 
    280       1.2  bouyer void
    281       1.2  bouyer hypervisor_clear_event(unsigned int ev)
    282       1.2  bouyer {
    283       1.2  bouyer 	volatile shared_info_t *s = HYPERVISOR_shared_info;
    284       1.2  bouyer #ifdef PORT_DEBUG
    285       1.2  bouyer 	if (ev == PORT_DEBUG)
    286       1.2  bouyer 		printf("hypervisor_clear_event %d\n", ev);
    287       1.2  bouyer #endif
    288       1.2  bouyer 
    289       1.2  bouyer 	xen_atomic_clear_bit(&s->evtchn_pending[0], ev);
    290       1.2  bouyer }
    291       1.2  bouyer 
    292       1.2  bouyer void
    293       1.2  bouyer hypervisor_enable_ipl(unsigned int ipl)
    294       1.2  bouyer {
    295       1.2  bouyer 	u_int32_t l1, l2;
    296       1.2  bouyer 	int l1i, l2i;
    297       1.2  bouyer 	struct cpu_info *ci = curcpu();
    298       1.2  bouyer 
    299       1.2  bouyer 	/*
    300       1.2  bouyer 	 * enable all events for ipl. As we only set an event in ipl_evt_mask
    301       1.2  bouyer 	 * for its lowest IPL, and pending IPLs are processed high to low,
    302       1.2  bouyer 	 * we know that all callback for this event have been processed.
    303       1.2  bouyer 	 */
    304       1.2  bouyer 
    305       1.2  bouyer 	l1 = ci->ci_isources[ipl]->ipl_evt_mask1;
    306       1.2  bouyer 	ci->ci_isources[ipl]->ipl_evt_mask1 = 0;
    307       1.2  bouyer 	while ((l1i = ffs(l1)) != 0) {
    308       1.2  bouyer 		l1i--;
    309       1.2  bouyer 		l1 &= ~(1 << l1i);
    310       1.2  bouyer 		l2 = ci->ci_isources[ipl]->ipl_evt_mask2[l1i];
    311       1.2  bouyer 		ci->ci_isources[ipl]->ipl_evt_mask2[l1i] = 0;
    312       1.2  bouyer 		while ((l2i = ffs(l2)) != 0) {
    313       1.2  bouyer 			int evtch;
    314       1.2  bouyer 
    315       1.2  bouyer 			l2i--;
    316       1.2  bouyer 			l2 &= ~(1 << l2i);
    317       1.2  bouyer 
    318       1.2  bouyer 			evtch = (l1i << 5) + l2i;
    319       1.2  bouyer 			hypervisor_enable_event(evtch);
    320       1.2  bouyer 		}
    321       1.2  bouyer 	}
    322       1.2  bouyer }
    323       1.2  bouyer 
    324       1.2  bouyer void
    325       1.2  bouyer hypervisor_set_ipending(u_int32_t iplmask, int l1, int l2)
    326       1.2  bouyer {
    327       1.2  bouyer 	int ipl;
    328       1.2  bouyer 	struct cpu_info *ci = curcpu();
    329       1.2  bouyer 
    330       1.2  bouyer 	/* set pending bit for the appropriate IPLs */
    331       1.2  bouyer 	ci->ci_ipending |= iplmask;
    332       1.2  bouyer 
    333       1.2  bouyer 	/*
    334       1.2  bouyer 	 * And set event pending bit for the lowest IPL. As IPL are handled
    335       1.2  bouyer 	 * from high to low, this ensure that all callbacks will have been
    336       1.2  bouyer 	 * called when we ack the event
    337       1.2  bouyer 	 */
    338       1.2  bouyer 	ipl = ffs(iplmask);
    339       1.2  bouyer 	KASSERT(ipl > 0);
    340       1.2  bouyer 	ipl--;
    341       1.2  bouyer 	ci->ci_isources[ipl]->ipl_evt_mask1 |= 1 << l1;
    342       1.2  bouyer 	ci->ci_isources[ipl]->ipl_evt_mask2[l1] |= 1 << l2;
    343       1.2  bouyer }
    344