Home | History | Annotate | Line # | Download | only in x86
pvh_consinit.c revision 1.4
      1  1.4     mrg /* $NetBSD: pvh_consinit.c,v 1.4 2023/07/22 19:13:17 mrg Exp $ */
      2  1.1  bouyer 
      3  1.1  bouyer /*
      4  1.1  bouyer  * Copyright (c) 2020 Manuel Bouyer.
      5  1.1  bouyer  *
      6  1.1  bouyer  * Redistribution and use in source and binary forms, with or without
      7  1.1  bouyer  * modification, are permitted provided that the following conditions
      8  1.1  bouyer  * are met:
      9  1.1  bouyer  * 1. Redistributions of source code must retain the above copyright
     10  1.1  bouyer  *    notice, this list of conditions and the following disclaimer.
     11  1.1  bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1  bouyer  *    notice, this list of conditions and the following disclaimer in the
     13  1.1  bouyer  *    documentation and/or other materials provided with the distribution.
     14  1.1  bouyer  *
     15  1.1  bouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  1.1  bouyer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17  1.1  bouyer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18  1.1  bouyer  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19  1.1  bouyer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     20  1.1  bouyer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     21  1.1  bouyer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     22  1.1  bouyer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23  1.1  bouyer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     24  1.1  bouyer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25  1.1  bouyer  *
     26  1.1  bouyer  */
     27  1.1  bouyer 
     28  1.1  bouyer #include <sys/cdefs.h>
     29  1.4     mrg __KERNEL_RCSID(0, "$NetBSD: pvh_consinit.c,v 1.4 2023/07/22 19:13:17 mrg Exp $");
     30  1.1  bouyer 
     31  1.1  bouyer #include "xencons.h"
     32  1.1  bouyer #include <sys/param.h>
     33  1.1  bouyer #include <sys/systm.h>
     34  1.1  bouyer #include <sys/device.h>
     35  1.1  bouyer #include <uvm/uvm_extern.h>
     36  1.1  bouyer #include <uvm/uvm_prot.h>
     37  1.1  bouyer 
     38  1.1  bouyer #include <dev/cons.h>
     39  1.1  bouyer #include <xen/xen.h>
     40  1.1  bouyer #include <xen/hypervisor.h>
     41  1.1  bouyer #include <xen/include/public/hvm/hvm_op.h>
     42  1.1  bouyer #include <xen/include/public/hvm/params.h>
     43  1.1  bouyer 
     44  1.4     mrg #include "xen_def_cons.h"
     45  1.4     mrg 
     46  1.1  bouyer static int pvh_xenconscn_getc(dev_t);
     47  1.1  bouyer static void pvh_xenconscn_putc(dev_t, int);
     48  1.1  bouyer static void pvh_xenconscn_pollc(dev_t, int);
     49  1.1  bouyer 
     50  1.1  bouyer static struct consdev pvh_xencons = {
     51  1.1  bouyer         NULL, NULL, pvh_xenconscn_getc, pvh_xenconscn_putc, pvh_xenconscn_pollc,
     52  1.1  bouyer 	NULL, NULL, NULL, NODEV, CN_NORMAL
     53  1.1  bouyer };
     54  1.1  bouyer 
     55  1.1  bouyer 
     56  1.3  bouyer int
     57  1.1  bouyer xen_pvh_consinit(void)
     58  1.1  bouyer {
     59  1.1  bouyer 	/*
     60  1.1  bouyer 	 * hugly hack because we're called multiple times at different
     61  1.1  bouyer 	 * boot stage.
     62  1.1  bouyer 	 */
     63  1.1  bouyer 	static int initted = 0;
     64  1.3  bouyer 	if (xendomain_is_dom0()) {
     65  1.3  bouyer 		union xen_cmdline_parseinfo xcp;
     66  1.3  bouyer 		xen_parse_cmdline(XEN_PARSE_CONSOLE, &xcp);
     67  1.3  bouyer #ifdef CONS_OVERRIDE
     68  1.3  bouyer                 if (strcmp(default_consinfo.devname, "tty0") == 0 ||
     69  1.3  bouyer 		    strcmp(default_consinfo.devname, "pc") == 0) {
     70  1.3  bouyer #else
     71  1.3  bouyer 		if (strcmp(xcp.xcp_console, "tty0") == 0 || /* linux name */
     72  1.3  bouyer 		    strcmp(xcp.xcp_console, "pc") == 0) { /* NetBSD name */
     73  1.3  bouyer #endif /* CONS_OVERRIDE */
     74  1.3  bouyer 			return 0; /* native console code will do it */
     75  1.3  bouyer 		}
     76  1.3  bouyer 	}
     77  1.2  bouyer 	if (initted == 0 && !xendomain_is_dom0()) {
     78  1.1  bouyer 		/* pmap not up yet, fall back to printk() */
     79  1.1  bouyer 		cn_tab = &pvh_xencons;
     80  1.1  bouyer 		initted++;
     81  1.3  bouyer 		return 1;
     82  1.1  bouyer 	} else if (initted > 1) {
     83  1.3  bouyer 		return 1;
     84  1.1  bouyer 	}
     85  1.1  bouyer 	initted++;
     86  1.2  bouyer 	if (xendomain_is_dom0()) {
     87  1.3  bouyer 		/* we know we're using Xen's console at this point */
     88  1.2  bouyer 		xenconscn_attach(); /* no ring in this case */
     89  1.2  bouyer 		initted++; /* don't init console twice */
     90  1.3  bouyer 		return 1;
     91  1.2  bouyer 	}
     92  1.3  bouyer 
     93  1.1  bouyer #if NXENCONS > 0
     94  1.1  bouyer 	/* we can now map the xencons rings. */
     95  1.1  bouyer 	struct xen_hvm_param xen_hvm_param;
     96  1.1  bouyer 
     97  1.1  bouyer 
     98  1.1  bouyer 	xen_hvm_param.domid = DOMID_SELF;
     99  1.1  bouyer 	xen_hvm_param.index = HVM_PARAM_CONSOLE_PFN;
    100  1.1  bouyer 
    101  1.1  bouyer 	if ( HYPERVISOR_hvm_op(HVMOP_get_param, &xen_hvm_param) < 0)
    102  1.1  bouyer 		panic("xen_pvh_consinit: can't get console PFN");
    103  1.1  bouyer 
    104  1.1  bouyer 	xen_start_info.console.domU.mfn = xen_hvm_param.value;
    105  1.1  bouyer 	pmap_kenter_pa((vaddr_t) xencons_interface, ptoa(xen_hvm_param.value),
    106  1.1  bouyer 	    VM_PROT_READ|VM_PROT_WRITE, 0);
    107  1.1  bouyer 
    108  1.1  bouyer 	xen_hvm_param.domid = DOMID_SELF;
    109  1.1  bouyer 	xen_hvm_param.index = HVM_PARAM_CONSOLE_EVTCHN;
    110  1.1  bouyer 
    111  1.1  bouyer 	if ( HYPERVISOR_hvm_op(HVMOP_get_param, &xen_hvm_param) < 0)
    112  1.1  bouyer 		panic("xen_pvh_consinit: can't get console event");
    113  1.1  bouyer 
    114  1.1  bouyer 	xen_start_info.console.domU.evtchn = xen_hvm_param.value;
    115  1.1  bouyer 	xenconscn_attach();
    116  1.1  bouyer #endif
    117  1.3  bouyer 	return 1;
    118  1.1  bouyer }
    119  1.1  bouyer 
    120  1.1  bouyer static int
    121  1.1  bouyer pvh_xenconscn_getc(dev_t dev)
    122  1.1  bouyer {
    123  1.1  bouyer 	while(1)
    124  1.1  bouyer 		;
    125  1.1  bouyer 	return -1;
    126  1.1  bouyer }
    127  1.1  bouyer 
    128  1.1  bouyer static void
    129  1.1  bouyer pvh_xenconscn_putc(dev_t dev, int c)
    130  1.1  bouyer {
    131  1.1  bouyer 	printk("%c", c);
    132  1.1  bouyer }
    133  1.1  bouyer 
    134  1.1  bouyer static void
    135  1.1  bouyer pvh_xenconscn_pollc(dev_t dev, int on)
    136  1.1  bouyer {
    137  1.1  bouyer 	return;
    138  1.1  bouyer }
    139