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