xref: /titanic_50/usr/src/uts/common/os/console.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
30*7c478bd9Sstevel@tonic-gate #include <sys/varargs.h>
31*7c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/console.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/consdev.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/promif.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/file.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/conf.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/taskq.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/log.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/esunddi.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/fs/snode.h>
46*7c478bd9Sstevel@tonic-gate #include <sys/termios.h>
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate #define	MINLINES	10
49*7c478bd9Sstevel@tonic-gate #define	MAXLINES	48
50*7c478bd9Sstevel@tonic-gate #define	LOSCREENLINES	34
51*7c478bd9Sstevel@tonic-gate #define	HISCREENLINES	48
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate #define	MINCOLS		10
54*7c478bd9Sstevel@tonic-gate #define	MAXCOLS		120
55*7c478bd9Sstevel@tonic-gate #define	LOSCREENCOLS	80
56*7c478bd9Sstevel@tonic-gate #define	HISCREENCOLS	120
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate vnode_t *console_vnode;
59*7c478bd9Sstevel@tonic-gate taskq_t *console_taskq;
60*7c478bd9Sstevel@tonic-gate vnode_t *ltemvp = NULL;
61*7c478bd9Sstevel@tonic-gate dev_t ltemdev;
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate /*
64*7c478bd9Sstevel@tonic-gate  * The current set of polled I/O routines (if any)
65*7c478bd9Sstevel@tonic-gate  */
66*7c478bd9Sstevel@tonic-gate struct cons_polledio *cons_polledio;
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate /*
69*7c478bd9Sstevel@tonic-gate  * Console I/O Routines
70*7c478bd9Sstevel@tonic-gate  *
71*7c478bd9Sstevel@tonic-gate  * In the event that kernel messages are generated with cmn_err(9F) or printf()
72*7c478bd9Sstevel@tonic-gate  * early in boot, after a panic, in resource-constrained situations, or sent
73*7c478bd9Sstevel@tonic-gate  * through /dev/console to the wscons driver, we may be called upon to render
74*7c478bd9Sstevel@tonic-gate  * characters directly to the frame buffer using the underlying prom_*()
75*7c478bd9Sstevel@tonic-gate  * routines.  These in turn may attempt to use PROM services directly, or may
76*7c478bd9Sstevel@tonic-gate  * use a kernel console emulator if one is available.  Unfortunately, if PROM
77*7c478bd9Sstevel@tonic-gate  * services are being used by the kernel on a multi-CPU system, these routines
78*7c478bd9Sstevel@tonic-gate  * might be called while another CPU is simultaneously accessing a frame buffer
79*7c478bd9Sstevel@tonic-gate  * memory mapping (perhaps through the X server).  This situation may not be
80*7c478bd9Sstevel@tonic-gate  * supported by the frame buffer hardware.
81*7c478bd9Sstevel@tonic-gate  *
82*7c478bd9Sstevel@tonic-gate  * To handle this situation, we implement a two-phase locking scheme which we
83*7c478bd9Sstevel@tonic-gate  * use to protect accesses to the underlying prom_*() rendering routines.  The
84*7c478bd9Sstevel@tonic-gate  * common-code functions console_hold() and console_rele() are used to gain
85*7c478bd9Sstevel@tonic-gate  * exclusive access to the console from within the kernel.  We use a standard
86*7c478bd9Sstevel@tonic-gate  * r/w lock in writer-mode only to implement the kernel lock.  We use an r/w
87*7c478bd9Sstevel@tonic-gate  * lock instead of a mutex here because character rendering is slow and hold
88*7c478bd9Sstevel@tonic-gate  * times will be relatively long, and there is no point in adaptively spinning.
89*7c478bd9Sstevel@tonic-gate  * These routines may be called recursively, in which case subsequent calls
90*7c478bd9Sstevel@tonic-gate  * just increment the console_depth hold count.  Once exclusive access is
91*7c478bd9Sstevel@tonic-gate  * gained, we grab the frame buffer device node and block further mappings to
92*7c478bd9Sstevel@tonic-gate  * it by holding the specfs node lock and the device node's lock.  We then
93*7c478bd9Sstevel@tonic-gate  * observe if any mappings are present by examining the specfs node's s_mapcnt
94*7c478bd9Sstevel@tonic-gate  * (non-clone mmaps) and the devinfo node's devi_ref count (clone opens).
95*7c478bd9Sstevel@tonic-gate  *
96*7c478bd9Sstevel@tonic-gate  * Then, around each character rendering call, the routines console_enter()
97*7c478bd9Sstevel@tonic-gate  * and console_exit() are used to inform the platform code that we are
98*7c478bd9Sstevel@tonic-gate  * accessing the character rendering routines.  These platform routines can
99*7c478bd9Sstevel@tonic-gate  * then examine the "busy" flag returned by console_enter() and briefly stop
100*7c478bd9Sstevel@tonic-gate  * the other CPUs so that they cannot access the frame buffer hardware while
101*7c478bd9Sstevel@tonic-gate  * we are busy rendering characters.  This mess can all be removed when the
102*7c478bd9Sstevel@tonic-gate  * impossible dream of a unified kernel console emulator is someday realized.
103*7c478bd9Sstevel@tonic-gate  */
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate static krwlock_t console_lock;
106*7c478bd9Sstevel@tonic-gate static uint_t console_depth;
107*7c478bd9Sstevel@tonic-gate static int console_busy;
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate extern void pm_cfb_check_and_powerup(void);
110*7c478bd9Sstevel@tonic-gate extern void pm_cfb_rele(void);
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate static int
113*7c478bd9Sstevel@tonic-gate console_hold(void)
114*7c478bd9Sstevel@tonic-gate {
115*7c478bd9Sstevel@tonic-gate 	if (panicstr != NULL)
116*7c478bd9Sstevel@tonic-gate 		return (console_busy); /* assume exclusive access in panic */
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate 	if (rw_owner(&console_lock) != curthread)
119*7c478bd9Sstevel@tonic-gate 		rw_enter(&console_lock, RW_WRITER);
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate 	if (console_depth++ != 0)
122*7c478bd9Sstevel@tonic-gate 		return (console_busy); /* lock is being entered recursively */
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate 	pm_cfb_check_and_powerup();
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate #ifdef _CONSOLE_OUTPUT_VIA_FIRMWARE
127*7c478bd9Sstevel@tonic-gate 	if (ncpus > 1 && fbvp != NULL) {
128*7c478bd9Sstevel@tonic-gate 		struct snode *csp = VTOS(VTOS(fbvp)->s_commonvp);
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 		mutex_enter(&csp->s_lock);
131*7c478bd9Sstevel@tonic-gate 		console_busy = csp->s_mapcnt != 0;
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 		if (csp->s_mapcnt == 0 && fbdip != NULL) {
134*7c478bd9Sstevel@tonic-gate 			mutex_enter(&DEVI(fbdip)->devi_lock);
135*7c478bd9Sstevel@tonic-gate 			console_busy = DEVI(fbdip)->devi_ref != 0;
136*7c478bd9Sstevel@tonic-gate 		}
137*7c478bd9Sstevel@tonic-gate 	}
138*7c478bd9Sstevel@tonic-gate #endif
139*7c478bd9Sstevel@tonic-gate 	return (console_busy);
140*7c478bd9Sstevel@tonic-gate }
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate static void
143*7c478bd9Sstevel@tonic-gate console_rele(void)
144*7c478bd9Sstevel@tonic-gate {
145*7c478bd9Sstevel@tonic-gate 	if (panicstr != NULL)
146*7c478bd9Sstevel@tonic-gate 		return; /* do not modify lock states if we are panicking */
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&console_lock));
149*7c478bd9Sstevel@tonic-gate 	ASSERT(console_depth != 0);
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate 	if (--console_depth != 0)
152*7c478bd9Sstevel@tonic-gate 		return; /* lock is being dropped recursively */
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate #ifdef _CONSOLE_OUTPUT_VIA_FIRMWARE
155*7c478bd9Sstevel@tonic-gate 	if (ncpus > 1 && fbvp != NULL) {
156*7c478bd9Sstevel@tonic-gate 		struct snode *csp = VTOS(VTOS(fbvp)->s_commonvp);
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&csp->s_lock));
159*7c478bd9Sstevel@tonic-gate 		if (csp->s_mapcnt == 0 && fbdip != NULL)
160*7c478bd9Sstevel@tonic-gate 			mutex_exit(&DEVI(fbdip)->devi_lock);
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate 		mutex_exit(&csp->s_lock);
163*7c478bd9Sstevel@tonic-gate 	}
164*7c478bd9Sstevel@tonic-gate #endif
165*7c478bd9Sstevel@tonic-gate 	pm_cfb_rele();
166*7c478bd9Sstevel@tonic-gate 	console_busy = 0;
167*7c478bd9Sstevel@tonic-gate 	rw_exit(&console_lock);
168*7c478bd9Sstevel@tonic-gate }
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate static void
171*7c478bd9Sstevel@tonic-gate console_getprop(dev_t dev, dev_info_t *dip, char *name, ushort_t *sp)
172*7c478bd9Sstevel@tonic-gate {
173*7c478bd9Sstevel@tonic-gate 	uchar_t *data;
174*7c478bd9Sstevel@tonic-gate 	uint_t len;
175*7c478bd9Sstevel@tonic-gate 	uint_t i;
176*7c478bd9Sstevel@tonic-gate 
177*7c478bd9Sstevel@tonic-gate 	*sp = 0;
178*7c478bd9Sstevel@tonic-gate 	if (ddi_prop_lookup_byte_array(dev, dip, 0, name, &data, &len) ==
179*7c478bd9Sstevel@tonic-gate 	    DDI_PROP_SUCCESS) {
180*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < len; i++) {
181*7c478bd9Sstevel@tonic-gate 			if (data[i] < '0' || data[i] > '9')
182*7c478bd9Sstevel@tonic-gate 				break;
183*7c478bd9Sstevel@tonic-gate 			*sp = *sp * 10 + data[i] - '0';
184*7c478bd9Sstevel@tonic-gate 		}
185*7c478bd9Sstevel@tonic-gate 		ddi_prop_free(data);
186*7c478bd9Sstevel@tonic-gate 	}
187*7c478bd9Sstevel@tonic-gate }
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate /*
190*7c478bd9Sstevel@tonic-gate  * Gets the number of rows and columns (in char's) and the
191*7c478bd9Sstevel@tonic-gate  * width and height (in pixels) of the console.
192*7c478bd9Sstevel@tonic-gate  */
193*7c478bd9Sstevel@tonic-gate void
194*7c478bd9Sstevel@tonic-gate console_get_size(ushort_t *r, ushort_t *c, ushort_t *x, ushort_t *y)
195*7c478bd9Sstevel@tonic-gate {
196*7c478bd9Sstevel@tonic-gate 	int rel_needed = 0;
197*7c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
198*7c478bd9Sstevel@tonic-gate 	dev_t dev;
199*7c478bd9Sstevel@tonic-gate 
200*7c478bd9Sstevel@tonic-gate 	/*
201*7c478bd9Sstevel@tonic-gate 	 * If we have loaded the console IO stuff, then ask for the screen
202*7c478bd9Sstevel@tonic-gate 	 * size properties from the layered terminal emulator.  Else ask for
203*7c478bd9Sstevel@tonic-gate 	 * them from the root node, which will eventually fall through to the
204*7c478bd9Sstevel@tonic-gate 	 * options node and get them from the prom.
205*7c478bd9Sstevel@tonic-gate 	 */
206*7c478bd9Sstevel@tonic-gate 	if (ltemvp == NULL) {
207*7c478bd9Sstevel@tonic-gate 		dip = ddi_root_node();
208*7c478bd9Sstevel@tonic-gate 		dev = DDI_DEV_T_ANY;
209*7c478bd9Sstevel@tonic-gate 	} else {
210*7c478bd9Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_dev(ltemdev, 0);
211*7c478bd9Sstevel@tonic-gate 		dev = ltemdev;
212*7c478bd9Sstevel@tonic-gate 		rel_needed = 1;
213*7c478bd9Sstevel@tonic-gate 	}
214*7c478bd9Sstevel@tonic-gate 
215*7c478bd9Sstevel@tonic-gate 	/*
216*7c478bd9Sstevel@tonic-gate 	 * If we have not initialized a console yet and don't have a root
217*7c478bd9Sstevel@tonic-gate 	 * node (ie. we have not initialized the DDI yet) return our default
218*7c478bd9Sstevel@tonic-gate 	 * size for the screen.
219*7c478bd9Sstevel@tonic-gate 	 */
220*7c478bd9Sstevel@tonic-gate 	if (dip == NULL) {
221*7c478bd9Sstevel@tonic-gate 		*r = LOSCREENLINES;
222*7c478bd9Sstevel@tonic-gate 		*c = LOSCREENCOLS;
223*7c478bd9Sstevel@tonic-gate 		*x = *y = 0;
224*7c478bd9Sstevel@tonic-gate 		return;
225*7c478bd9Sstevel@tonic-gate 	}
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate 	console_getprop(dev, dip, "screen-#columns", c);
228*7c478bd9Sstevel@tonic-gate 	console_getprop(dev, dip, "screen-#rows", r);
229*7c478bd9Sstevel@tonic-gate 	console_getprop(dev, dip, "screen-width", x);
230*7c478bd9Sstevel@tonic-gate 	console_getprop(dev, dip, "screen-height", y);
231*7c478bd9Sstevel@tonic-gate 
232*7c478bd9Sstevel@tonic-gate 	if (*c < MINCOLS)
233*7c478bd9Sstevel@tonic-gate 		*c = LOSCREENCOLS;
234*7c478bd9Sstevel@tonic-gate 	else if (*c > MAXCOLS)
235*7c478bd9Sstevel@tonic-gate 		*c = HISCREENCOLS;
236*7c478bd9Sstevel@tonic-gate 
237*7c478bd9Sstevel@tonic-gate 	if (*r < MINLINES)
238*7c478bd9Sstevel@tonic-gate 		*r = LOSCREENLINES;
239*7c478bd9Sstevel@tonic-gate 	else if (*r > MAXLINES)
240*7c478bd9Sstevel@tonic-gate 		*r = HISCREENLINES;
241*7c478bd9Sstevel@tonic-gate 
242*7c478bd9Sstevel@tonic-gate 	if (rel_needed)
243*7c478bd9Sstevel@tonic-gate 		ddi_release_devi(dip);
244*7c478bd9Sstevel@tonic-gate }
245*7c478bd9Sstevel@tonic-gate 
246*7c478bd9Sstevel@tonic-gate typedef struct console_msg {
247*7c478bd9Sstevel@tonic-gate 	size_t	cm_size;
248*7c478bd9Sstevel@tonic-gate 	char	cm_text[1];
249*7c478bd9Sstevel@tonic-gate } console_msg_t;
250*7c478bd9Sstevel@tonic-gate 
251*7c478bd9Sstevel@tonic-gate static void
252*7c478bd9Sstevel@tonic-gate console_putmsg(console_msg_t *cm)
253*7c478bd9Sstevel@tonic-gate {
254*7c478bd9Sstevel@tonic-gate 	int busy, spl;
255*7c478bd9Sstevel@tonic-gate 	ssize_t res;
256*7c478bd9Sstevel@tonic-gate 
257*7c478bd9Sstevel@tonic-gate 	ASSERT(taskq_member(console_taskq, curthread));
258*7c478bd9Sstevel@tonic-gate 
259*7c478bd9Sstevel@tonic-gate 	if (rconsvp == NULL || panicstr ||
260*7c478bd9Sstevel@tonic-gate 	    vn_rdwr(UIO_WRITE, console_vnode, cm->cm_text, strlen(cm->cm_text),
261*7c478bd9Sstevel@tonic-gate 	    0, UIO_SYSSPACE, FAPPEND, (rlim64_t)LOG_HIWAT, kcred, &res) != 0) {
262*7c478bd9Sstevel@tonic-gate 
263*7c478bd9Sstevel@tonic-gate 		busy = console_hold();
264*7c478bd9Sstevel@tonic-gate 		spl = console_enter(busy);
265*7c478bd9Sstevel@tonic-gate 
266*7c478bd9Sstevel@tonic-gate 		prom_printf("%s", cm->cm_text);
267*7c478bd9Sstevel@tonic-gate 
268*7c478bd9Sstevel@tonic-gate 		console_exit(busy, spl);
269*7c478bd9Sstevel@tonic-gate 		console_rele();
270*7c478bd9Sstevel@tonic-gate 	}
271*7c478bd9Sstevel@tonic-gate 
272*7c478bd9Sstevel@tonic-gate 	kmem_free(cm, cm->cm_size);
273*7c478bd9Sstevel@tonic-gate }
274*7c478bd9Sstevel@tonic-gate 
275*7c478bd9Sstevel@tonic-gate void
276*7c478bd9Sstevel@tonic-gate console_vprintf(const char *fmt, va_list adx)
277*7c478bd9Sstevel@tonic-gate {
278*7c478bd9Sstevel@tonic-gate 	console_msg_t *cm;
279*7c478bd9Sstevel@tonic-gate 	size_t len = vsnprintf(NULL, 0, fmt, adx);
280*7c478bd9Sstevel@tonic-gate 	int busy, spl;
281*7c478bd9Sstevel@tonic-gate 
282*7c478bd9Sstevel@tonic-gate 	if (console_taskq != NULL && rconsvp != NULL && panicstr == NULL &&
283*7c478bd9Sstevel@tonic-gate 	    (cm = kmem_alloc(sizeof (*cm) + len, KM_NOSLEEP)) != NULL) {
284*7c478bd9Sstevel@tonic-gate 		cm->cm_size = sizeof (*cm) + len;
285*7c478bd9Sstevel@tonic-gate 		(void) vsnprintf(cm->cm_text, len + 1, fmt, adx);
286*7c478bd9Sstevel@tonic-gate 		if (taskq_dispatch(console_taskq, (task_func_t *)console_putmsg,
287*7c478bd9Sstevel@tonic-gate 		    cm, TQ_NOSLEEP) != 0)
288*7c478bd9Sstevel@tonic-gate 			return;
289*7c478bd9Sstevel@tonic-gate 		kmem_free(cm, cm->cm_size);
290*7c478bd9Sstevel@tonic-gate 	}
291*7c478bd9Sstevel@tonic-gate 
292*7c478bd9Sstevel@tonic-gate 	busy = console_hold();
293*7c478bd9Sstevel@tonic-gate 	spl = console_enter(busy);
294*7c478bd9Sstevel@tonic-gate 
295*7c478bd9Sstevel@tonic-gate 	prom_vprintf(fmt, adx);
296*7c478bd9Sstevel@tonic-gate 
297*7c478bd9Sstevel@tonic-gate 	console_exit(busy, spl);
298*7c478bd9Sstevel@tonic-gate 	console_rele();
299*7c478bd9Sstevel@tonic-gate }
300*7c478bd9Sstevel@tonic-gate 
301*7c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
302*7c478bd9Sstevel@tonic-gate void
303*7c478bd9Sstevel@tonic-gate console_printf(const char *fmt, ...)
304*7c478bd9Sstevel@tonic-gate {
305*7c478bd9Sstevel@tonic-gate 	va_list adx;
306*7c478bd9Sstevel@tonic-gate 
307*7c478bd9Sstevel@tonic-gate 	va_start(adx, fmt);
308*7c478bd9Sstevel@tonic-gate 	console_vprintf(fmt, adx);
309*7c478bd9Sstevel@tonic-gate 	va_end(adx);
310*7c478bd9Sstevel@tonic-gate }
311*7c478bd9Sstevel@tonic-gate 
312*7c478bd9Sstevel@tonic-gate /*
313*7c478bd9Sstevel@tonic-gate  * Write the specified number of bytes from a string to the console device.
314*7c478bd9Sstevel@tonic-gate  */
315*7c478bd9Sstevel@tonic-gate void
316*7c478bd9Sstevel@tonic-gate console_puts(const char *s, size_t n)
317*7c478bd9Sstevel@tonic-gate {
318*7c478bd9Sstevel@tonic-gate 	int busy, spl;
319*7c478bd9Sstevel@tonic-gate 
320*7c478bd9Sstevel@tonic-gate 	busy = console_hold();
321*7c478bd9Sstevel@tonic-gate 	spl = console_enter(busy);
322*7c478bd9Sstevel@tonic-gate 
323*7c478bd9Sstevel@tonic-gate 	prom_writestr(s, n);
324*7c478bd9Sstevel@tonic-gate 
325*7c478bd9Sstevel@tonic-gate 	console_exit(busy, spl);
326*7c478bd9Sstevel@tonic-gate 	console_rele();
327*7c478bd9Sstevel@tonic-gate }
328*7c478bd9Sstevel@tonic-gate 
329*7c478bd9Sstevel@tonic-gate void
330*7c478bd9Sstevel@tonic-gate console_putc(int c)
331*7c478bd9Sstevel@tonic-gate {
332*7c478bd9Sstevel@tonic-gate 	int busy = console_hold();
333*7c478bd9Sstevel@tonic-gate 	int spl = console_enter(busy);
334*7c478bd9Sstevel@tonic-gate 
335*7c478bd9Sstevel@tonic-gate 	if (c == '\n')
336*7c478bd9Sstevel@tonic-gate 		prom_putchar('\r');
337*7c478bd9Sstevel@tonic-gate 	prom_putchar(c);
338*7c478bd9Sstevel@tonic-gate 
339*7c478bd9Sstevel@tonic-gate 	console_exit(busy, spl);
340*7c478bd9Sstevel@tonic-gate 	console_rele();
341*7c478bd9Sstevel@tonic-gate }
342*7c478bd9Sstevel@tonic-gate 
343*7c478bd9Sstevel@tonic-gate /*
344*7c478bd9Sstevel@tonic-gate  * Read a string from the console device.  We only permit synchronous
345*7c478bd9Sstevel@tonic-gate  * conversation between the kernel and a console user early in boot prior to
346*7c478bd9Sstevel@tonic-gate  * the initialization of rconsvp.
347*7c478bd9Sstevel@tonic-gate  */
348*7c478bd9Sstevel@tonic-gate void
349*7c478bd9Sstevel@tonic-gate console_gets(char *s, size_t len)
350*7c478bd9Sstevel@tonic-gate {
351*7c478bd9Sstevel@tonic-gate 	char *p = s;
352*7c478bd9Sstevel@tonic-gate 	char *q = s + len - 1;
353*7c478bd9Sstevel@tonic-gate 	int c;
354*7c478bd9Sstevel@tonic-gate 
355*7c478bd9Sstevel@tonic-gate 	ASSERT(rconsvp == NULL);
356*7c478bd9Sstevel@tonic-gate 	(void) console_hold();
357*7c478bd9Sstevel@tonic-gate 
358*7c478bd9Sstevel@tonic-gate 	for (;;) {
359*7c478bd9Sstevel@tonic-gate 		switch (c = (prom_getchar() & 0x7f)) {
360*7c478bd9Sstevel@tonic-gate 		case 0x7f: /* DEL */
361*7c478bd9Sstevel@tonic-gate 			if (p == s)
362*7c478bd9Sstevel@tonic-gate 				break;
363*7c478bd9Sstevel@tonic-gate 			console_putc(c);
364*7c478bd9Sstevel@tonic-gate 			c = '\b';
365*7c478bd9Sstevel@tonic-gate 			/*FALLTHRU*/
366*7c478bd9Sstevel@tonic-gate 
367*7c478bd9Sstevel@tonic-gate 		case '\b':
368*7c478bd9Sstevel@tonic-gate 			if (p == s)
369*7c478bd9Sstevel@tonic-gate 				break;
370*7c478bd9Sstevel@tonic-gate 			console_putc('\b');
371*7c478bd9Sstevel@tonic-gate 			console_putc(' ');
372*7c478bd9Sstevel@tonic-gate 			/*FALLTHRU*/
373*7c478bd9Sstevel@tonic-gate 
374*7c478bd9Sstevel@tonic-gate 		case '#': /* historical backspace alias */
375*7c478bd9Sstevel@tonic-gate 			console_putc(c);
376*7c478bd9Sstevel@tonic-gate 			if (p > s)
377*7c478bd9Sstevel@tonic-gate 				p--;
378*7c478bd9Sstevel@tonic-gate 			break;
379*7c478bd9Sstevel@tonic-gate 
380*7c478bd9Sstevel@tonic-gate 		case CTRL('u'):
381*7c478bd9Sstevel@tonic-gate 			console_putc(c);
382*7c478bd9Sstevel@tonic-gate 			console_putc('\n');
383*7c478bd9Sstevel@tonic-gate 			p = s;
384*7c478bd9Sstevel@tonic-gate 			break;
385*7c478bd9Sstevel@tonic-gate 
386*7c478bd9Sstevel@tonic-gate 		case '\r':
387*7c478bd9Sstevel@tonic-gate 		case '\n':
388*7c478bd9Sstevel@tonic-gate 			console_putc('\n');
389*7c478bd9Sstevel@tonic-gate 			goto done;
390*7c478bd9Sstevel@tonic-gate 
391*7c478bd9Sstevel@tonic-gate 		default:
392*7c478bd9Sstevel@tonic-gate 			if (p < q) {
393*7c478bd9Sstevel@tonic-gate 				console_putc(c);
394*7c478bd9Sstevel@tonic-gate 				*p++ = c;
395*7c478bd9Sstevel@tonic-gate 			} else
396*7c478bd9Sstevel@tonic-gate 				console_putc('\a');
397*7c478bd9Sstevel@tonic-gate 		}
398*7c478bd9Sstevel@tonic-gate 	}
399*7c478bd9Sstevel@tonic-gate done:
400*7c478bd9Sstevel@tonic-gate 	console_rele();
401*7c478bd9Sstevel@tonic-gate 	*p = '\0';
402*7c478bd9Sstevel@tonic-gate }
403*7c478bd9Sstevel@tonic-gate 
404*7c478bd9Sstevel@tonic-gate /*
405*7c478bd9Sstevel@tonic-gate  * Read a character from the console device.  Synchronous conversation between
406*7c478bd9Sstevel@tonic-gate  * the kernel and a console user is only permitted early in boot prior to the
407*7c478bd9Sstevel@tonic-gate  * initialization of rconsvp.
408*7c478bd9Sstevel@tonic-gate  */
409*7c478bd9Sstevel@tonic-gate int
410*7c478bd9Sstevel@tonic-gate console_getc(void)
411*7c478bd9Sstevel@tonic-gate {
412*7c478bd9Sstevel@tonic-gate 	int c;
413*7c478bd9Sstevel@tonic-gate 
414*7c478bd9Sstevel@tonic-gate 	ASSERT(rconsvp == NULL);
415*7c478bd9Sstevel@tonic-gate 	c = prom_getchar();
416*7c478bd9Sstevel@tonic-gate 
417*7c478bd9Sstevel@tonic-gate 	if (c == '\r')
418*7c478bd9Sstevel@tonic-gate 		c = '\n';
419*7c478bd9Sstevel@tonic-gate 
420*7c478bd9Sstevel@tonic-gate 	console_putc(c);
421*7c478bd9Sstevel@tonic-gate 	return (c);
422*7c478bd9Sstevel@tonic-gate }
423