summaryrefslogtreecommitdiff
path: root/usr/src/uts/i86xpv/boot/boot_xconsole.c
blob: 5cc746057cb3a25980656cc5b43a9d27c4229567 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <sys/types.h>

#include <sys/hypervisor.h>
#include <sys/machparam.h>
#include <xen/public/io/console.h>
#include <sys/mach_mmu.h>

shared_info_t *HYPERVISOR_shared_info;
void *HYPERVISOR_console_page;

#if defined(_BOOT)
#include "dboot/dboot_printf.h"
char big_empty[MMU_PAGESIZE * 3];	/* room for 2 page aligned page */
#endif /* _BOOT */

unsigned short video_fb_buf[32 * 1024 + MMU_PAGESIZE];
unsigned char kb_status_buf[MMU_PAGESIZE * 2];
unsigned short *video_fb = NULL;
unsigned char *kb_status = NULL;

static volatile struct xencons_interface *cons_ifp;

#define	XR_FULL(r)	((r)->xr_in_cnt - (r)->xr_out_cnt == XR_SIZE)
#define	XR_EMPTY(r)	((r)->xr_in_cnt == (r)->xr_out_cnt)

#define	PTE_BITS	(PT_VALID | PT_WRITABLE)
#define	PTE_DEV_BITS	(PT_VALID | PT_WRITABLE | PT_NOCACHE | PT_NOCONSIST | \
			PT_FOREIGN)

/*
 * For some unfortunate reason, the hypervisor doesn't bother to include the
 * shared info in the original virtual address space.  This means we can't
 * do any console I/O until we have manipulated some pagetables. So we have to
 * do this bit of code with no ability to get debug output.
 */
/*ARGSUSED*/
void
bcons_init_xen(char *cmdline)
{
#ifdef _BOOT
	int i = 0;
	uintptr_t vaddr;

	/*
	 * find a page aligned virtual address in "big_empty"
	 */
	vaddr = (uintptr_t)&big_empty;
	vaddr = (vaddr + MMU_PAGEOFFSET) & MMU_PAGEMASK;
	HYPERVISOR_shared_info = (shared_info_t *)vaddr;

	/*
	 * Sets the "present" and "writable" bits in the PTE
	 * plus user for amd64.
	 */
	HYPERVISOR_update_va_mapping(vaddr, xen_info->shared_info | PTE_BITS,
	    UVMF_INVLPG | UVMF_LOCAL);

	if (!DOMAIN_IS_INITDOMAIN(xen_info)) {
		/*
		 * map the xen console ring buffers
		 */
		HYPERVISOR_update_va_mapping(vaddr + MMU_PAGESIZE,
		    mmu_ptob((x86pte_t)xen_info->console.domU.mfn) | PTE_BITS,
		    UVMF_INVLPG | UVMF_LOCAL);
	} else {
		/*
		 * Xen will pass dom0 information about the current
		 * display settings via xen_info->console.dom0.  This
		 * information includes what video mode we're in (vga
		 * or vesa) and some basic information about the video
		 * mode.  (screen size, cursor location, etc.)  We're
		 * just going to ignore all this info.  Here's some
		 * reasons why:
		 *
		 * - Currently Solaris itself has no support for vesa.
		 *   Also, the only way to boot Solaris is using our
		 *   patched version of grub, which conveniently doesn't
		 *   support vesa either.
		 *
		 * - By default when solaris boots up it clears the screen
		 *   thereby removing any previously displayed grub/xen
		 *   console messages, so we really don't care about the
		 *   current vga settings.
		 *
		 * Initially we'll map device memory for the frame buffer
		 * and keyboard into some local memory that already has
		 * page table entries so that we can get very basic debug
		 * output.  Later on when we're initializing page tables
		 * we'll map re-map these devices to be at their expected
		 * addresses.  Note that these mappings created below will
		 * be torn down right before the kernel boots up when
		 * all the memory and mappings associated with dboot are
		 * released.
		 *
		 * Map the frame buffer.
		 */
		vaddr = (uintptr_t)&video_fb_buf;
		vaddr = (vaddr + MMU_PAGEOFFSET) & MMU_PAGEMASK;
		for (i = 0; i < 32 * 1024; i += MMU_PAGESIZE)
			(void) HYPERVISOR_update_va_mapping(vaddr + i,
			    0xb8000 + i | PTE_DEV_BITS,
			    UVMF_INVLPG | UVMF_LOCAL);
		video_fb = (unsigned short *)vaddr;

		/* Map the keyboard */
		vaddr = (uintptr_t)&kb_status_buf;
		vaddr = (vaddr + MMU_PAGEOFFSET) & MMU_PAGEMASK;
		(void) HYPERVISOR_update_va_mapping(vaddr, 0x0 | PTE_DEV_BITS,
		    UVMF_INVLPG | UVMF_LOCAL);
		kb_status = (unsigned char *)vaddr;
	}

#endif /* _BOOT */
	if (!DOMAIN_IS_INITDOMAIN(xen_info)) {
		HYPERVISOR_console_page =
		    (void *)((uintptr_t)HYPERVISOR_shared_info + MMU_PAGESIZE);
	} else {
		HYPERVISOR_console_page = NULL;
	}
}


/*
 * This is the equivalent of polled I/O across the hypervisor CONSOLE
 * channel to output 1 character at a time.
 */
void
bcons_putchar_xen(int c)
{
	evtchn_send_t send;
	char buffer = (char)c;

	if (DOMAIN_IS_INITDOMAIN(xen_info)) {
		(void) HYPERVISOR_console_io(CONSOLEIO_write, 1, &buffer);
		return;
	}

	cons_ifp = (volatile struct xencons_interface *)HYPERVISOR_console_page;

	/*
	 * need to add carriage return for new lines
	 */
	if (c == '\n')
		bcons_putchar_xen('\r');

	/*
	 * We have to wait till we have an open transmit slot.
	 */
	while (cons_ifp->out_prod - cons_ifp->out_cons >=
	    sizeof (cons_ifp->out))
		(void) HYPERVISOR_yield();

	cons_ifp->out[MASK_XENCONS_IDX(cons_ifp->out_prod, cons_ifp->out)] =
	    (char)c;
	++cons_ifp->out_prod;

	/*
	 * Signal Domain 0 that it has something to do for us.
	 */
	send.port = xen_info->console.domU.evtchn;
	(void) HYPERVISOR_event_channel_op(EVTCHNOP_send, &send);
}

static uint_t have_char = 0;
static char buffered;

/*
 * See if there is a character on input.
 */
int
bcons_ischar_xen(void)
{
	if (DOMAIN_IS_INITDOMAIN(xen_info)) {
		if (have_char)
			return (1);
		if (HYPERVISOR_console_io(CONSOLEIO_read, 1, &buffered) > 0)
			return (have_char = 1);
		return (0);
	}

	cons_ifp = (volatile struct xencons_interface *)HYPERVISOR_console_page;
	if (cons_ifp->in_cons == cons_ifp->in_prod)
		return (0);
	return (1);
}

/*
 * get a console input character
 */
int
bcons_getchar_xen(void)
{
	evtchn_send_t send;
	char c;

	if (DOMAIN_IS_INITDOMAIN(xen_info)) {
		while (have_char == 0)
			(void) bcons_ischar_xen();
		have_char = 0;
		return (buffered);
	}

	cons_ifp = (volatile struct xencons_interface *)HYPERVISOR_console_page;
	while (cons_ifp->in_cons == cons_ifp->in_prod)
		(void) HYPERVISOR_yield();

	c = cons_ifp->in[MASK_XENCONS_IDX(cons_ifp->in_cons, cons_ifp->in)];
	++cons_ifp->in_cons;

	/*
	 * Signal Domain 0 that we ate a character.
	 */
	send.port = xen_info->console.domU.evtchn;
	(void) HYPERVISOR_event_channel_op(EVTCHNOP_send, &send);
	return (c);
}