1 /* $NetBSD: pvh_consinit.c,v 1.4 2023/07/22 19:13:17 mrg 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.4 2023/07/22 19:13:17 mrg 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 #include "xen_def_cons.h" 45 46 static int pvh_xenconscn_getc(dev_t); 47 static void pvh_xenconscn_putc(dev_t, int); 48 static void pvh_xenconscn_pollc(dev_t, int); 49 50 static struct consdev pvh_xencons = { 51 NULL, NULL, pvh_xenconscn_getc, pvh_xenconscn_putc, pvh_xenconscn_pollc, 52 NULL, NULL, NULL, NODEV, CN_NORMAL 53 }; 54 55 56 int 57 xen_pvh_consinit(void) 58 { 59 /* 60 * hugly hack because we're called multiple times at different 61 * boot stage. 62 */ 63 static int initted = 0; 64 if (xendomain_is_dom0()) { 65 union xen_cmdline_parseinfo xcp; 66 xen_parse_cmdline(XEN_PARSE_CONSOLE, &xcp); 67 #ifdef CONS_OVERRIDE 68 if (strcmp(default_consinfo.devname, "tty0") == 0 || 69 strcmp(default_consinfo.devname, "pc") == 0) { 70 #else 71 if (strcmp(xcp.xcp_console, "tty0") == 0 || /* linux name */ 72 strcmp(xcp.xcp_console, "pc") == 0) { /* NetBSD name */ 73 #endif /* CONS_OVERRIDE */ 74 return 0; /* native console code will do it */ 75 } 76 } 77 if (initted == 0 && !xendomain_is_dom0()) { 78 /* pmap not up yet, fall back to printk() */ 79 cn_tab = &pvh_xencons; 80 initted++; 81 return 1; 82 } else if (initted > 1) { 83 return 1; 84 } 85 initted++; 86 if (xendomain_is_dom0()) { 87 /* we know we're using Xen's console at this point */ 88 xenconscn_attach(); /* no ring in this case */ 89 initted++; /* don't init console twice */ 90 return 1; 91 } 92 93 #if NXENCONS > 0 94 /* we can now map the xencons rings. */ 95 struct xen_hvm_param xen_hvm_param; 96 97 98 xen_hvm_param.domid = DOMID_SELF; 99 xen_hvm_param.index = HVM_PARAM_CONSOLE_PFN; 100 101 if ( HYPERVISOR_hvm_op(HVMOP_get_param, &xen_hvm_param) < 0) 102 panic("xen_pvh_consinit: can't get console PFN"); 103 104 xen_start_info.console.domU.mfn = xen_hvm_param.value; 105 pmap_kenter_pa((vaddr_t) xencons_interface, ptoa(xen_hvm_param.value), 106 VM_PROT_READ|VM_PROT_WRITE, 0); 107 108 xen_hvm_param.domid = DOMID_SELF; 109 xen_hvm_param.index = HVM_PARAM_CONSOLE_EVTCHN; 110 111 if ( HYPERVISOR_hvm_op(HVMOP_get_param, &xen_hvm_param) < 0) 112 panic("xen_pvh_consinit: can't get console event"); 113 114 xen_start_info.console.domU.evtchn = xen_hvm_param.value; 115 xenconscn_attach(); 116 #endif 117 return 1; 118 } 119 120 static int 121 pvh_xenconscn_getc(dev_t dev) 122 { 123 while(1) 124 ; 125 return -1; 126 } 127 128 static void 129 pvh_xenconscn_putc(dev_t dev, int c) 130 { 131 printk("%c", c); 132 } 133 134 static void 135 pvh_xenconscn_pollc(dev_t dev, int on) 136 { 137 return; 138 } 139