xref: /illumos-gate/usr/src/uts/common/os/console.c (revision fc8ae2ec)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5fea9cb91Slq  * Common Development and Distribution License (the "License").
6fea9cb91Slq  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21fea9cb91Slq 
227c478bd9Sstevel@tonic-gate /*
23aecfc01dSrui zang - Sun Microsystems - Beijing China  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <sys/types.h>
287c478bd9Sstevel@tonic-gate #include <sys/varargs.h>
297c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
307c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
317c478bd9Sstevel@tonic-gate #include <sys/console.h>
327c478bd9Sstevel@tonic-gate #include <sys/consdev.h>
337c478bd9Sstevel@tonic-gate #include <sys/promif.h>
34fea9cb91Slq #include <sys/note.h>
35fea9cb91Slq #include <sys/polled_io.h>
367c478bd9Sstevel@tonic-gate #include <sys/systm.h>
377c478bd9Sstevel@tonic-gate #include <sys/file.h>
387c478bd9Sstevel@tonic-gate #include <sys/conf.h>
397c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
407c478bd9Sstevel@tonic-gate #include <sys/taskq.h>
417c478bd9Sstevel@tonic-gate #include <sys/log.h>
427c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
437c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
447c478bd9Sstevel@tonic-gate #include <sys/esunddi.h>
457c478bd9Sstevel@tonic-gate #include <sys/fs/snode.h>
467c478bd9Sstevel@tonic-gate #include <sys/termios.h>
47fea9cb91Slq #include <sys/tem_impl.h>
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #define	MINLINES	10
507c478bd9Sstevel@tonic-gate #define	MAXLINES	48
517c478bd9Sstevel@tonic-gate #define	LOSCREENLINES	34
527c478bd9Sstevel@tonic-gate #define	HISCREENLINES	48
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #define	MINCOLS		10
557c478bd9Sstevel@tonic-gate #define	MAXCOLS		120
567c478bd9Sstevel@tonic-gate #define	LOSCREENCOLS	80
577c478bd9Sstevel@tonic-gate #define	HISCREENCOLS	120
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate vnode_t *console_vnode;
607c478bd9Sstevel@tonic-gate taskq_t *console_taskq;
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate /*
637c478bd9Sstevel@tonic-gate  * The current set of polled I/O routines (if any)
647c478bd9Sstevel@tonic-gate  */
657c478bd9Sstevel@tonic-gate struct cons_polledio *cons_polledio;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate  * Console I/O Routines
697c478bd9Sstevel@tonic-gate  *
707c478bd9Sstevel@tonic-gate  * In the event that kernel messages are generated with cmn_err(9F) or printf()
717c478bd9Sstevel@tonic-gate  * early in boot, after a panic, in resource-constrained situations, or sent
727c478bd9Sstevel@tonic-gate  * through /dev/console to the wscons driver, we may be called upon to render
737c478bd9Sstevel@tonic-gate  * characters directly to the frame buffer using the underlying prom_*()
747c478bd9Sstevel@tonic-gate  * routines.  These in turn may attempt to use PROM services directly, or may
757c478bd9Sstevel@tonic-gate  * use a kernel console emulator if one is available.  Unfortunately, if PROM
767c478bd9Sstevel@tonic-gate  * services are being used by the kernel on a multi-CPU system, these routines
777c478bd9Sstevel@tonic-gate  * might be called while another CPU is simultaneously accessing a frame buffer
787c478bd9Sstevel@tonic-gate  * memory mapping (perhaps through the X server).  This situation may not be
797c478bd9Sstevel@tonic-gate  * supported by the frame buffer hardware.
807c478bd9Sstevel@tonic-gate  *
817c478bd9Sstevel@tonic-gate  * To handle this situation, we implement a two-phase locking scheme which we
827c478bd9Sstevel@tonic-gate  * use to protect accesses to the underlying prom_*() rendering routines.  The
837c478bd9Sstevel@tonic-gate  * common-code functions console_hold() and console_rele() are used to gain
847c478bd9Sstevel@tonic-gate  * exclusive access to the console from within the kernel.  We use a standard
857c478bd9Sstevel@tonic-gate  * r/w lock in writer-mode only to implement the kernel lock.  We use an r/w
867c478bd9Sstevel@tonic-gate  * lock instead of a mutex here because character rendering is slow and hold
877c478bd9Sstevel@tonic-gate  * times will be relatively long, and there is no point in adaptively spinning.
887c478bd9Sstevel@tonic-gate  * These routines may be called recursively, in which case subsequent calls
897c478bd9Sstevel@tonic-gate  * just increment the console_depth hold count.  Once exclusive access is
907c478bd9Sstevel@tonic-gate  * gained, we grab the frame buffer device node and block further mappings to
917c478bd9Sstevel@tonic-gate  * it by holding the specfs node lock and the device node's lock.  We then
927c478bd9Sstevel@tonic-gate  * observe if any mappings are present by examining the specfs node's s_mapcnt
937c478bd9Sstevel@tonic-gate  * (non-clone mmaps) and the devinfo node's devi_ref count (clone opens).
947c478bd9Sstevel@tonic-gate  *
957c478bd9Sstevel@tonic-gate  * Then, around each character rendering call, the routines console_enter()
967c478bd9Sstevel@tonic-gate  * and console_exit() are used to inform the platform code that we are
977c478bd9Sstevel@tonic-gate  * accessing the character rendering routines.  These platform routines can
987c478bd9Sstevel@tonic-gate  * then examine the "busy" flag returned by console_enter() and briefly stop
997c478bd9Sstevel@tonic-gate  * the other CPUs so that they cannot access the frame buffer hardware while
1007c478bd9Sstevel@tonic-gate  * we are busy rendering characters.  This mess can all be removed when the
1017c478bd9Sstevel@tonic-gate  * impossible dream of a unified kernel console emulator is someday realized.
1027c478bd9Sstevel@tonic-gate  */
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate static krwlock_t console_lock;
1057c478bd9Sstevel@tonic-gate static uint_t console_depth;
1067c478bd9Sstevel@tonic-gate static int console_busy;
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate extern void pm_cfb_check_and_powerup(void);
1097c478bd9Sstevel@tonic-gate extern void pm_cfb_rele(void);
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate static int
console_hold(void)1127c478bd9Sstevel@tonic-gate console_hold(void)
1137c478bd9Sstevel@tonic-gate {
1147c478bd9Sstevel@tonic-gate 	if (panicstr != NULL)
1157c478bd9Sstevel@tonic-gate 		return (console_busy); /* assume exclusive access in panic */
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	if (rw_owner(&console_lock) != curthread)
1187c478bd9Sstevel@tonic-gate 		rw_enter(&console_lock, RW_WRITER);
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	if (console_depth++ != 0)
1217c478bd9Sstevel@tonic-gate 		return (console_busy); /* lock is being entered recursively */
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	pm_cfb_check_and_powerup();
1247c478bd9Sstevel@tonic-gate 
125fea9cb91Slq #ifdef _HAVE_TEM_FIRMWARE
126fea9cb91Slq 	if (consmode == CONS_FW && ncpus > 1 && fbvp != NULL) {
1277c478bd9Sstevel@tonic-gate 		struct snode *csp = VTOS(VTOS(fbvp)->s_commonvp);
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 		mutex_enter(&csp->s_lock);
1307c478bd9Sstevel@tonic-gate 		console_busy = csp->s_mapcnt != 0;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 		if (csp->s_mapcnt == 0 && fbdip != NULL) {
1337c478bd9Sstevel@tonic-gate 			mutex_enter(&DEVI(fbdip)->devi_lock);
1347c478bd9Sstevel@tonic-gate 			console_busy = DEVI(fbdip)->devi_ref != 0;
1357c478bd9Sstevel@tonic-gate 		}
1367c478bd9Sstevel@tonic-gate 	}
137fea9cb91Slq #endif /* _HAVE_TEM_FIRMWARE */
1387c478bd9Sstevel@tonic-gate 	return (console_busy);
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate static void
console_rele(void)1427c478bd9Sstevel@tonic-gate console_rele(void)
1437c478bd9Sstevel@tonic-gate {
1447c478bd9Sstevel@tonic-gate 	if (panicstr != NULL)
1457c478bd9Sstevel@tonic-gate 		return; /* do not modify lock states if we are panicking */
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&console_lock));
1487c478bd9Sstevel@tonic-gate 	ASSERT(console_depth != 0);
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	if (--console_depth != 0)
1517c478bd9Sstevel@tonic-gate 		return; /* lock is being dropped recursively */
1527c478bd9Sstevel@tonic-gate 
153fea9cb91Slq #ifdef _HAVE_TEM_FIRMWARE
154fea9cb91Slq 	if (consmode == CONS_FW && ncpus > 1 && fbvp != NULL) {
1557c478bd9Sstevel@tonic-gate 		struct snode *csp = VTOS(VTOS(fbvp)->s_commonvp);
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&csp->s_lock));
1587c478bd9Sstevel@tonic-gate 		if (csp->s_mapcnt == 0 && fbdip != NULL)
1597c478bd9Sstevel@tonic-gate 			mutex_exit(&DEVI(fbdip)->devi_lock);
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 		mutex_exit(&csp->s_lock);
1627c478bd9Sstevel@tonic-gate 	}
163fea9cb91Slq #endif /* _HAVE_TEM_FIRMWARE */
1647c478bd9Sstevel@tonic-gate 	pm_cfb_rele();
1657c478bd9Sstevel@tonic-gate 	console_busy = 0;
1667c478bd9Sstevel@tonic-gate 	rw_exit(&console_lock);
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate static void
console_getprop(dev_t dev,dev_info_t * dip,char * name,ushort_t * sp)1707c478bd9Sstevel@tonic-gate console_getprop(dev_t dev, dev_info_t *dip, char *name, ushort_t *sp)
1717c478bd9Sstevel@tonic-gate {
1727c478bd9Sstevel@tonic-gate 	uchar_t *data;
1737c478bd9Sstevel@tonic-gate 	uint_t len;
1747c478bd9Sstevel@tonic-gate 	uint_t i;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	*sp = 0;
1777c478bd9Sstevel@tonic-gate 	if (ddi_prop_lookup_byte_array(dev, dip, 0, name, &data, &len) ==
1787c478bd9Sstevel@tonic-gate 	    DDI_PROP_SUCCESS) {
1797c478bd9Sstevel@tonic-gate 		for (i = 0; i < len; i++) {
1807c478bd9Sstevel@tonic-gate 			if (data[i] < '0' || data[i] > '9')
1817c478bd9Sstevel@tonic-gate 				break;
1827c478bd9Sstevel@tonic-gate 			*sp = *sp * 10 + data[i] - '0';
1837c478bd9Sstevel@tonic-gate 		}
1847c478bd9Sstevel@tonic-gate 		ddi_prop_free(data);
1857c478bd9Sstevel@tonic-gate 	}
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate /*
1897c478bd9Sstevel@tonic-gate  * Gets the number of rows and columns (in char's) and the
1907c478bd9Sstevel@tonic-gate  * width and height (in pixels) of the console.
1917c478bd9Sstevel@tonic-gate  */
1927c478bd9Sstevel@tonic-gate void
console_get_size(ushort_t * r,ushort_t * c,ushort_t * x,ushort_t * y)1937c478bd9Sstevel@tonic-gate console_get_size(ushort_t *r, ushort_t *c, ushort_t *x, ushort_t *y)
1947c478bd9Sstevel@tonic-gate {
1957c478bd9Sstevel@tonic-gate 	int rel_needed = 0;
1967c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
1977c478bd9Sstevel@tonic-gate 	dev_t dev;
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	/*
2007c478bd9Sstevel@tonic-gate 	 * If we have loaded the console IO stuff, then ask for the screen
2017c478bd9Sstevel@tonic-gate 	 * size properties from the layered terminal emulator.  Else ask for
2027c478bd9Sstevel@tonic-gate 	 * them from the root node, which will eventually fall through to the
2037c478bd9Sstevel@tonic-gate 	 * options node and get them from the prom.
2047c478bd9Sstevel@tonic-gate 	 */
205fea9cb91Slq 	if (rwsconsvp == NULL || consmode == CONS_FW) {
2067c478bd9Sstevel@tonic-gate 		dip = ddi_root_node();
2077c478bd9Sstevel@tonic-gate 		dev = DDI_DEV_T_ANY;
2087c478bd9Sstevel@tonic-gate 	} else {
209fea9cb91Slq 		dev = rwsconsvp->v_rdev; /* layering is wc -> tem */
210fea9cb91Slq 		dip = e_ddi_hold_devi_by_dev(dev, 0);
2117c478bd9Sstevel@tonic-gate 		rel_needed = 1;
2127c478bd9Sstevel@tonic-gate 	}
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	/*
2157c478bd9Sstevel@tonic-gate 	 * If we have not initialized a console yet and don't have a root
2167c478bd9Sstevel@tonic-gate 	 * node (ie. we have not initialized the DDI yet) return our default
2177c478bd9Sstevel@tonic-gate 	 * size for the screen.
2187c478bd9Sstevel@tonic-gate 	 */
2197c478bd9Sstevel@tonic-gate 	if (dip == NULL) {
2207c478bd9Sstevel@tonic-gate 		*r = LOSCREENLINES;
2217c478bd9Sstevel@tonic-gate 		*c = LOSCREENCOLS;
2227c478bd9Sstevel@tonic-gate 		*x = *y = 0;
2237c478bd9Sstevel@tonic-gate 		return;
2247c478bd9Sstevel@tonic-gate 	}
2257c478bd9Sstevel@tonic-gate 
226fea9cb91Slq 	console_getprop(DDI_DEV_T_ANY, dip, "screen-#columns", c);
227fea9cb91Slq 	console_getprop(DDI_DEV_T_ANY, dip, "screen-#rows", r);
228fea9cb91Slq 	console_getprop(DDI_DEV_T_ANY, dip, "screen-width", x);
229fea9cb91Slq 	console_getprop(DDI_DEV_T_ANY, dip, "screen-height", y);
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	if (*c < MINCOLS)
2327c478bd9Sstevel@tonic-gate 		*c = LOSCREENCOLS;
2337c478bd9Sstevel@tonic-gate 	else if (*c > MAXCOLS)
2347c478bd9Sstevel@tonic-gate 		*c = HISCREENCOLS;
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	if (*r < MINLINES)
2377c478bd9Sstevel@tonic-gate 		*r = LOSCREENLINES;
2387c478bd9Sstevel@tonic-gate 	else if (*r > MAXLINES)
2397c478bd9Sstevel@tonic-gate 		*r = HISCREENLINES;
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	if (rel_needed)
2427c478bd9Sstevel@tonic-gate 		ddi_release_devi(dip);
2437c478bd9Sstevel@tonic-gate }
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate typedef struct console_msg {
2467c478bd9Sstevel@tonic-gate 	size_t	cm_size;
2477c478bd9Sstevel@tonic-gate 	char	cm_text[1];
2487c478bd9Sstevel@tonic-gate } console_msg_t;
2497c478bd9Sstevel@tonic-gate 
250fea9cb91Slq /*
251fea9cb91Slq  * If we can't access the console stream, fall through to PROM, which redirects
252fea9cb91Slq  * it back into to terminal emulator as appropriate.  The console stream should
253fea9cb91Slq  * be available after consconfig runs.
254fea9cb91Slq  */
2557c478bd9Sstevel@tonic-gate static void
console_putmsg(console_msg_t * cm)2567c478bd9Sstevel@tonic-gate console_putmsg(console_msg_t *cm)
2577c478bd9Sstevel@tonic-gate {
2587c478bd9Sstevel@tonic-gate 	int busy, spl;
2597c478bd9Sstevel@tonic-gate 	ssize_t res;
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	ASSERT(taskq_member(console_taskq, curthread));
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	if (rconsvp == NULL || panicstr ||
2647c478bd9Sstevel@tonic-gate 	    vn_rdwr(UIO_WRITE, console_vnode, cm->cm_text, strlen(cm->cm_text),
2657c478bd9Sstevel@tonic-gate 	    0, UIO_SYSSPACE, FAPPEND, (rlim64_t)LOG_HIWAT, kcred, &res) != 0) {
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 		busy = console_hold();
2687c478bd9Sstevel@tonic-gate 		spl = console_enter(busy);
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 		prom_printf("%s", cm->cm_text);
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 		console_exit(busy, spl);
2737c478bd9Sstevel@tonic-gate 		console_rele();
2747c478bd9Sstevel@tonic-gate 	}
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	kmem_free(cm, cm->cm_size);
2777c478bd9Sstevel@tonic-gate }
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate void
console_vprintf(const char * fmt,va_list adx)2807c478bd9Sstevel@tonic-gate console_vprintf(const char *fmt, va_list adx)
2817c478bd9Sstevel@tonic-gate {
2827c478bd9Sstevel@tonic-gate 	console_msg_t *cm;
2837c478bd9Sstevel@tonic-gate 	size_t len = vsnprintf(NULL, 0, fmt, adx);
2847c478bd9Sstevel@tonic-gate 	int busy, spl;
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	if (console_taskq != NULL && rconsvp != NULL && panicstr == NULL &&
2877c478bd9Sstevel@tonic-gate 	    (cm = kmem_alloc(sizeof (*cm) + len, KM_NOSLEEP)) != NULL) {
2887c478bd9Sstevel@tonic-gate 		cm->cm_size = sizeof (*cm) + len;
2897c478bd9Sstevel@tonic-gate 		(void) vsnprintf(cm->cm_text, len + 1, fmt, adx);
2907c478bd9Sstevel@tonic-gate 		if (taskq_dispatch(console_taskq, (task_func_t *)console_putmsg,
291*fc8ae2ecSToomas Soome 		    cm, TQ_NOSLEEP) != TASKQID_INVALID)
2927c478bd9Sstevel@tonic-gate 			return;
2937c478bd9Sstevel@tonic-gate 		kmem_free(cm, cm->cm_size);
2947c478bd9Sstevel@tonic-gate 	}
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	busy = console_hold();
2977c478bd9Sstevel@tonic-gate 	spl = console_enter(busy);
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	prom_vprintf(fmt, adx);
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	console_exit(busy, spl);
3027c478bd9Sstevel@tonic-gate 	console_rele();
3037c478bd9Sstevel@tonic-gate }
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
3067c478bd9Sstevel@tonic-gate void
console_printf(const char * fmt,...)3077c478bd9Sstevel@tonic-gate console_printf(const char *fmt, ...)
3087c478bd9Sstevel@tonic-gate {
3097c478bd9Sstevel@tonic-gate 	va_list adx;
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	va_start(adx, fmt);
3127c478bd9Sstevel@tonic-gate 	console_vprintf(fmt, adx);
3137c478bd9Sstevel@tonic-gate 	va_end(adx);
3147c478bd9Sstevel@tonic-gate }
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate /*
317fea9cb91Slq  * Avoid calling this function.
318fea9cb91Slq  *
319fea9cb91Slq  * Nothing in the kernel besides the wscons driver (wc) uses this
320fea9cb91Slq  * function. It may hopefully one day be removed altogether.
321fea9cb91Slq  * If a wayward module calls this they will pass through to PROM,
322fea9cb91Slq  * get redirected into the kernel emulator as appropriate.
3237c478bd9Sstevel@tonic-gate  */
3247c478bd9Sstevel@tonic-gate void
console_puts(const char * s,size_t n)3257c478bd9Sstevel@tonic-gate console_puts(const char *s, size_t n)
3267c478bd9Sstevel@tonic-gate {
3277c478bd9Sstevel@tonic-gate 	int busy, spl;
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	busy = console_hold();
3307c478bd9Sstevel@tonic-gate 	spl = console_enter(busy);
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	prom_writestr(s, n);
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	console_exit(busy, spl);
3357c478bd9Sstevel@tonic-gate 	console_rele();
3367c478bd9Sstevel@tonic-gate }
3377c478bd9Sstevel@tonic-gate 
338fea9cb91Slq /*
339fea9cb91Slq  * Let this function just go straight through to the PROM, since
340fea9cb91Slq  * we are called in early boot prior to the kernel terminal
341fea9cb91Slq  * emulator being available, and prior to the PROM stdout redirect
342fea9cb91Slq  * vector being set.
343fea9cb91Slq  */
344fea9cb91Slq static void
console_putc(int c)3457c478bd9Sstevel@tonic-gate console_putc(int c)
3467c478bd9Sstevel@tonic-gate {
3477c478bd9Sstevel@tonic-gate 	int busy = console_hold();
3487c478bd9Sstevel@tonic-gate 	int spl = console_enter(busy);
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	if (c == '\n')
3517c478bd9Sstevel@tonic-gate 		prom_putchar('\r');
3527c478bd9Sstevel@tonic-gate 	prom_putchar(c);
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	console_exit(busy, spl);
3557c478bd9Sstevel@tonic-gate 	console_rele();
3567c478bd9Sstevel@tonic-gate }
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate /*
3597c478bd9Sstevel@tonic-gate  * Read a string from the console device.  We only permit synchronous
3607c478bd9Sstevel@tonic-gate  * conversation between the kernel and a console user early in boot prior to
3617c478bd9Sstevel@tonic-gate  * the initialization of rconsvp.
3627c478bd9Sstevel@tonic-gate  */
3637c478bd9Sstevel@tonic-gate void
console_gets(char * s,size_t len)3647c478bd9Sstevel@tonic-gate console_gets(char *s, size_t len)
3657c478bd9Sstevel@tonic-gate {
3667c478bd9Sstevel@tonic-gate 	char *p = s;
3677c478bd9Sstevel@tonic-gate 	char *q = s + len - 1;
3687c478bd9Sstevel@tonic-gate 	int c;
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	ASSERT(rconsvp == NULL);
3717c478bd9Sstevel@tonic-gate 	(void) console_hold();
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	for (;;) {
3747c478bd9Sstevel@tonic-gate 		switch (c = (prom_getchar() & 0x7f)) {
3757c478bd9Sstevel@tonic-gate 		case 0x7f: /* DEL */
3767c478bd9Sstevel@tonic-gate 			if (p == s)
3777c478bd9Sstevel@tonic-gate 				break;
3787c478bd9Sstevel@tonic-gate 			console_putc(c);
3797c478bd9Sstevel@tonic-gate 			c = '\b';
3807c478bd9Sstevel@tonic-gate 			/*FALLTHRU*/
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 		case '\b':
3837c478bd9Sstevel@tonic-gate 			if (p == s)
3847c478bd9Sstevel@tonic-gate 				break;
3857c478bd9Sstevel@tonic-gate 			console_putc('\b');
3867c478bd9Sstevel@tonic-gate 			console_putc(' ');
3877c478bd9Sstevel@tonic-gate 			/*FALLTHRU*/
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 		case '#': /* historical backspace alias */
3907c478bd9Sstevel@tonic-gate 			console_putc(c);
3917c478bd9Sstevel@tonic-gate 			if (p > s)
3927c478bd9Sstevel@tonic-gate 				p--;
3937c478bd9Sstevel@tonic-gate 			break;
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 		case CTRL('u'):
3967c478bd9Sstevel@tonic-gate 			console_putc(c);
3977c478bd9Sstevel@tonic-gate 			console_putc('\n');
3987c478bd9Sstevel@tonic-gate 			p = s;
3997c478bd9Sstevel@tonic-gate 			break;
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 		case '\r':
4027c478bd9Sstevel@tonic-gate 		case '\n':
4037c478bd9Sstevel@tonic-gate 			console_putc('\n');
4047c478bd9Sstevel@tonic-gate 			goto done;
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 		default:
4077c478bd9Sstevel@tonic-gate 			if (p < q) {
4087c478bd9Sstevel@tonic-gate 				console_putc(c);
4097c478bd9Sstevel@tonic-gate 				*p++ = c;
4107c478bd9Sstevel@tonic-gate 			} else
4117c478bd9Sstevel@tonic-gate 				console_putc('\a');
4127c478bd9Sstevel@tonic-gate 		}
4137c478bd9Sstevel@tonic-gate 	}
4147c478bd9Sstevel@tonic-gate done:
4157c478bd9Sstevel@tonic-gate 	console_rele();
4167c478bd9Sstevel@tonic-gate 	*p = '\0';
4177c478bd9Sstevel@tonic-gate }
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate /*
4207c478bd9Sstevel@tonic-gate  * Read a character from the console device.  Synchronous conversation between
4217c478bd9Sstevel@tonic-gate  * the kernel and a console user is only permitted early in boot prior to the
4227c478bd9Sstevel@tonic-gate  * initialization of rconsvp.
4237c478bd9Sstevel@tonic-gate  */
4247c478bd9Sstevel@tonic-gate int
console_getc(void)4257c478bd9Sstevel@tonic-gate console_getc(void)
4267c478bd9Sstevel@tonic-gate {
4277c478bd9Sstevel@tonic-gate 	int c;
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	ASSERT(rconsvp == NULL);
4307c478bd9Sstevel@tonic-gate 	c = prom_getchar();
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	if (c == '\r')
4337c478bd9Sstevel@tonic-gate 		c = '\n';
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 	console_putc(c);
4367c478bd9Sstevel@tonic-gate 	return (c);
4377c478bd9Sstevel@tonic-gate }
438