xref: /netbsd-src/sys/arch/xen/x86/pvh_consinit.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /* $NetBSD: pvh_consinit.c,v 1.2 2020/05/03 17:23:14 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.2 2020/05/03 17:23:14 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 void
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 (initted == 0 && !xendomain_is_dom0()) {
63 		/* pmap not up yet, fall back to printk() */
64 		cn_tab = &pvh_xencons;
65 		initted++;
66 		return;
67 	} else if (initted > 1) {
68 		return;
69 	}
70 	initted++;
71 	if (xendomain_is_dom0()) {
72 		xenconscn_attach(); /* no ring in this case */
73 		initted++; /* don't init console twice */
74 		return;
75 	}
76 
77 #if NXENCONS > 0
78 	/* we can now map the xencons rings. */
79 	struct xen_hvm_param xen_hvm_param;
80 
81 
82 	xen_hvm_param.domid = DOMID_SELF;
83 	xen_hvm_param.index = HVM_PARAM_CONSOLE_PFN;
84 
85 	if ( HYPERVISOR_hvm_op(HVMOP_get_param, &xen_hvm_param) < 0)
86 		panic("xen_pvh_consinit: can't get console PFN");
87 
88 	xen_start_info.console.domU.mfn = xen_hvm_param.value;
89 	pmap_kenter_pa((vaddr_t) xencons_interface, ptoa(xen_hvm_param.value),
90 	    VM_PROT_READ|VM_PROT_WRITE, 0);
91 
92 	xen_hvm_param.domid = DOMID_SELF;
93 	xen_hvm_param.index = HVM_PARAM_CONSOLE_EVTCHN;
94 
95 	if ( HYPERVISOR_hvm_op(HVMOP_get_param, &xen_hvm_param) < 0)
96 		panic("xen_pvh_consinit: can't get console event");
97 
98 	xen_start_info.console.domU.evtchn = xen_hvm_param.value;
99 	xenconscn_attach();
100 #endif
101 }
102 
103 static int
104 pvh_xenconscn_getc(dev_t dev)
105 {
106 	while(1)
107 		;
108 	return -1;
109 }
110 
111 static void
112 pvh_xenconscn_putc(dev_t dev, int c)
113 {
114 	printk("%c", c);
115 }
116 
117 static void
118 pvh_xenconscn_pollc(dev_t dev, int on)
119 {
120 	return;
121 }
122