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