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 5*fea9cb91Slq150181 * Common Development and Distribution License (the "License"). 6*fea9cb91Slq150181 * 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 */ 21*fea9cb91Slq150181 227c478bd9Sstevel@tonic-gate /* 23*fea9cb91Slq150181 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate #include <sys/types.h> 307c478bd9Sstevel@tonic-gate #include <sys/varargs.h> 317c478bd9Sstevel@tonic-gate #include <sys/modctl.h> 327c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h> 337c478bd9Sstevel@tonic-gate #include <sys/console.h> 347c478bd9Sstevel@tonic-gate #include <sys/consdev.h> 357c478bd9Sstevel@tonic-gate #include <sys/promif.h> 36*fea9cb91Slq150181 #include <sys/note.h> 37*fea9cb91Slq150181 #include <sys/polled_io.h> 387c478bd9Sstevel@tonic-gate #include <sys/systm.h> 397c478bd9Sstevel@tonic-gate #include <sys/file.h> 407c478bd9Sstevel@tonic-gate #include <sys/conf.h> 417c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 427c478bd9Sstevel@tonic-gate #include <sys/taskq.h> 437c478bd9Sstevel@tonic-gate #include <sys/log.h> 447c478bd9Sstevel@tonic-gate #include <sys/ddi.h> 457c478bd9Sstevel@tonic-gate #include <sys/sunddi.h> 467c478bd9Sstevel@tonic-gate #include <sys/esunddi.h> 477c478bd9Sstevel@tonic-gate #include <sys/fs/snode.h> 487c478bd9Sstevel@tonic-gate #include <sys/termios.h> 49*fea9cb91Slq150181 #include <sys/tem_impl.h> 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate #define MINLINES 10 527c478bd9Sstevel@tonic-gate #define MAXLINES 48 537c478bd9Sstevel@tonic-gate #define LOSCREENLINES 34 547c478bd9Sstevel@tonic-gate #define HISCREENLINES 48 557c478bd9Sstevel@tonic-gate 567c478bd9Sstevel@tonic-gate #define MINCOLS 10 577c478bd9Sstevel@tonic-gate #define MAXCOLS 120 587c478bd9Sstevel@tonic-gate #define LOSCREENCOLS 80 597c478bd9Sstevel@tonic-gate #define HISCREENCOLS 120 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate vnode_t *console_vnode; 627c478bd9Sstevel@tonic-gate taskq_t *console_taskq; 637c478bd9Sstevel@tonic-gate 647c478bd9Sstevel@tonic-gate /* 657c478bd9Sstevel@tonic-gate * The current set of polled I/O routines (if any) 667c478bd9Sstevel@tonic-gate */ 677c478bd9Sstevel@tonic-gate struct cons_polledio *cons_polledio; 687c478bd9Sstevel@tonic-gate 697c478bd9Sstevel@tonic-gate /* 707c478bd9Sstevel@tonic-gate * Console I/O Routines 717c478bd9Sstevel@tonic-gate * 727c478bd9Sstevel@tonic-gate * In the event that kernel messages are generated with cmn_err(9F) or printf() 737c478bd9Sstevel@tonic-gate * early in boot, after a panic, in resource-constrained situations, or sent 747c478bd9Sstevel@tonic-gate * through /dev/console to the wscons driver, we may be called upon to render 757c478bd9Sstevel@tonic-gate * characters directly to the frame buffer using the underlying prom_*() 767c478bd9Sstevel@tonic-gate * routines. These in turn may attempt to use PROM services directly, or may 777c478bd9Sstevel@tonic-gate * use a kernel console emulator if one is available. Unfortunately, if PROM 787c478bd9Sstevel@tonic-gate * services are being used by the kernel on a multi-CPU system, these routines 797c478bd9Sstevel@tonic-gate * might be called while another CPU is simultaneously accessing a frame buffer 807c478bd9Sstevel@tonic-gate * memory mapping (perhaps through the X server). This situation may not be 817c478bd9Sstevel@tonic-gate * supported by the frame buffer hardware. 827c478bd9Sstevel@tonic-gate * 837c478bd9Sstevel@tonic-gate * To handle this situation, we implement a two-phase locking scheme which we 847c478bd9Sstevel@tonic-gate * use to protect accesses to the underlying prom_*() rendering routines. The 857c478bd9Sstevel@tonic-gate * common-code functions console_hold() and console_rele() are used to gain 867c478bd9Sstevel@tonic-gate * exclusive access to the console from within the kernel. We use a standard 877c478bd9Sstevel@tonic-gate * r/w lock in writer-mode only to implement the kernel lock. We use an r/w 887c478bd9Sstevel@tonic-gate * lock instead of a mutex here because character rendering is slow and hold 897c478bd9Sstevel@tonic-gate * times will be relatively long, and there is no point in adaptively spinning. 907c478bd9Sstevel@tonic-gate * These routines may be called recursively, in which case subsequent calls 917c478bd9Sstevel@tonic-gate * just increment the console_depth hold count. Once exclusive access is 927c478bd9Sstevel@tonic-gate * gained, we grab the frame buffer device node and block further mappings to 937c478bd9Sstevel@tonic-gate * it by holding the specfs node lock and the device node's lock. We then 947c478bd9Sstevel@tonic-gate * observe if any mappings are present by examining the specfs node's s_mapcnt 957c478bd9Sstevel@tonic-gate * (non-clone mmaps) and the devinfo node's devi_ref count (clone opens). 967c478bd9Sstevel@tonic-gate * 977c478bd9Sstevel@tonic-gate * Then, around each character rendering call, the routines console_enter() 987c478bd9Sstevel@tonic-gate * and console_exit() are used to inform the platform code that we are 997c478bd9Sstevel@tonic-gate * accessing the character rendering routines. These platform routines can 1007c478bd9Sstevel@tonic-gate * then examine the "busy" flag returned by console_enter() and briefly stop 1017c478bd9Sstevel@tonic-gate * the other CPUs so that they cannot access the frame buffer hardware while 1027c478bd9Sstevel@tonic-gate * we are busy rendering characters. This mess can all be removed when the 1037c478bd9Sstevel@tonic-gate * impossible dream of a unified kernel console emulator is someday realized. 1047c478bd9Sstevel@tonic-gate */ 1057c478bd9Sstevel@tonic-gate 1067c478bd9Sstevel@tonic-gate static krwlock_t console_lock; 1077c478bd9Sstevel@tonic-gate static uint_t console_depth; 1087c478bd9Sstevel@tonic-gate static int console_busy; 1097c478bd9Sstevel@tonic-gate 1107c478bd9Sstevel@tonic-gate extern void pm_cfb_check_and_powerup(void); 1117c478bd9Sstevel@tonic-gate extern void pm_cfb_rele(void); 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate static int 1147c478bd9Sstevel@tonic-gate console_hold(void) 1157c478bd9Sstevel@tonic-gate { 1167c478bd9Sstevel@tonic-gate if (panicstr != NULL) 1177c478bd9Sstevel@tonic-gate return (console_busy); /* assume exclusive access in panic */ 1187c478bd9Sstevel@tonic-gate 1197c478bd9Sstevel@tonic-gate if (rw_owner(&console_lock) != curthread) 1207c478bd9Sstevel@tonic-gate rw_enter(&console_lock, RW_WRITER); 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate if (console_depth++ != 0) 1237c478bd9Sstevel@tonic-gate return (console_busy); /* lock is being entered recursively */ 1247c478bd9Sstevel@tonic-gate 1257c478bd9Sstevel@tonic-gate pm_cfb_check_and_powerup(); 1267c478bd9Sstevel@tonic-gate 127*fea9cb91Slq150181 #ifdef _HAVE_TEM_FIRMWARE 128*fea9cb91Slq150181 if (consmode == CONS_FW && ncpus > 1 && fbvp != NULL) { 1297c478bd9Sstevel@tonic-gate struct snode *csp = VTOS(VTOS(fbvp)->s_commonvp); 1307c478bd9Sstevel@tonic-gate 1317c478bd9Sstevel@tonic-gate mutex_enter(&csp->s_lock); 1327c478bd9Sstevel@tonic-gate console_busy = csp->s_mapcnt != 0; 1337c478bd9Sstevel@tonic-gate 1347c478bd9Sstevel@tonic-gate if (csp->s_mapcnt == 0 && fbdip != NULL) { 1357c478bd9Sstevel@tonic-gate mutex_enter(&DEVI(fbdip)->devi_lock); 1367c478bd9Sstevel@tonic-gate console_busy = DEVI(fbdip)->devi_ref != 0; 1377c478bd9Sstevel@tonic-gate } 1387c478bd9Sstevel@tonic-gate } 139*fea9cb91Slq150181 #endif /* _HAVE_TEM_FIRMWARE */ 1407c478bd9Sstevel@tonic-gate return (console_busy); 1417c478bd9Sstevel@tonic-gate } 1427c478bd9Sstevel@tonic-gate 1437c478bd9Sstevel@tonic-gate static void 1447c478bd9Sstevel@tonic-gate console_rele(void) 1457c478bd9Sstevel@tonic-gate { 1467c478bd9Sstevel@tonic-gate if (panicstr != NULL) 1477c478bd9Sstevel@tonic-gate return; /* do not modify lock states if we are panicking */ 1487c478bd9Sstevel@tonic-gate 1497c478bd9Sstevel@tonic-gate ASSERT(RW_WRITE_HELD(&console_lock)); 1507c478bd9Sstevel@tonic-gate ASSERT(console_depth != 0); 1517c478bd9Sstevel@tonic-gate 1527c478bd9Sstevel@tonic-gate if (--console_depth != 0) 1537c478bd9Sstevel@tonic-gate return; /* lock is being dropped recursively */ 1547c478bd9Sstevel@tonic-gate 155*fea9cb91Slq150181 #ifdef _HAVE_TEM_FIRMWARE 156*fea9cb91Slq150181 if (consmode == CONS_FW && ncpus > 1 && fbvp != NULL) { 1577c478bd9Sstevel@tonic-gate struct snode *csp = VTOS(VTOS(fbvp)->s_commonvp); 1587c478bd9Sstevel@tonic-gate 1597c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&csp->s_lock)); 1607c478bd9Sstevel@tonic-gate if (csp->s_mapcnt == 0 && fbdip != NULL) 1617c478bd9Sstevel@tonic-gate mutex_exit(&DEVI(fbdip)->devi_lock); 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate mutex_exit(&csp->s_lock); 1647c478bd9Sstevel@tonic-gate } 165*fea9cb91Slq150181 #endif /* _HAVE_TEM_FIRMWARE */ 1667c478bd9Sstevel@tonic-gate pm_cfb_rele(); 1677c478bd9Sstevel@tonic-gate console_busy = 0; 1687c478bd9Sstevel@tonic-gate rw_exit(&console_lock); 1697c478bd9Sstevel@tonic-gate } 1707c478bd9Sstevel@tonic-gate 171*fea9cb91Slq150181 #ifdef _HAVE_TEM_FIRMWARE 172*fea9cb91Slq150181 /* 173*fea9cb91Slq150181 * This routine exists so that prom_write() can redirect writes 174*fea9cb91Slq150181 * to the framebuffer through the kernel terminal emulator, if 175*fea9cb91Slq150181 * that configuration is selected during consconfig. 176*fea9cb91Slq150181 * When the kernel terminal emulator is enabled, consconfig_dacf 177*fea9cb91Slq150181 * sets up the PROM output redirect vector to enter this function. 178*fea9cb91Slq150181 * During panic the console will already be powered up as part of 179*fea9cb91Slq150181 * calling into the prom_*() layer. 180*fea9cb91Slq150181 */ 181*fea9cb91Slq150181 ssize_t 182*fea9cb91Slq150181 console_prom_write_cb(promif_redir_arg_t arg, uchar_t *s, size_t n) 183*fea9cb91Slq150181 { 184*fea9cb91Slq150181 struct tem *tem = (struct tem *)arg; 185*fea9cb91Slq150181 186*fea9cb91Slq150181 ASSERT(consmode == CONS_KFB); 187*fea9cb91Slq150181 188*fea9cb91Slq150181 if (panicstr) 189*fea9cb91Slq150181 polled_io_cons_write(s, n); 190*fea9cb91Slq150181 else 191*fea9cb91Slq150181 tem->cons_wrtvec(tem, s, n, kcred); 192*fea9cb91Slq150181 193*fea9cb91Slq150181 return (n); 194*fea9cb91Slq150181 } 195*fea9cb91Slq150181 #endif /* _HAVE_TEM_FIRMWARE */ 196*fea9cb91Slq150181 1977c478bd9Sstevel@tonic-gate static void 1987c478bd9Sstevel@tonic-gate console_getprop(dev_t dev, dev_info_t *dip, char *name, ushort_t *sp) 1997c478bd9Sstevel@tonic-gate { 2007c478bd9Sstevel@tonic-gate uchar_t *data; 2017c478bd9Sstevel@tonic-gate uint_t len; 2027c478bd9Sstevel@tonic-gate uint_t i; 2037c478bd9Sstevel@tonic-gate 2047c478bd9Sstevel@tonic-gate *sp = 0; 2057c478bd9Sstevel@tonic-gate if (ddi_prop_lookup_byte_array(dev, dip, 0, name, &data, &len) == 2067c478bd9Sstevel@tonic-gate DDI_PROP_SUCCESS) { 2077c478bd9Sstevel@tonic-gate for (i = 0; i < len; i++) { 2087c478bd9Sstevel@tonic-gate if (data[i] < '0' || data[i] > '9') 2097c478bd9Sstevel@tonic-gate break; 2107c478bd9Sstevel@tonic-gate *sp = *sp * 10 + data[i] - '0'; 2117c478bd9Sstevel@tonic-gate } 2127c478bd9Sstevel@tonic-gate ddi_prop_free(data); 2137c478bd9Sstevel@tonic-gate } 2147c478bd9Sstevel@tonic-gate } 2157c478bd9Sstevel@tonic-gate 2167c478bd9Sstevel@tonic-gate /* 2177c478bd9Sstevel@tonic-gate * Gets the number of rows and columns (in char's) and the 2187c478bd9Sstevel@tonic-gate * width and height (in pixels) of the console. 2197c478bd9Sstevel@tonic-gate */ 2207c478bd9Sstevel@tonic-gate void 2217c478bd9Sstevel@tonic-gate console_get_size(ushort_t *r, ushort_t *c, ushort_t *x, ushort_t *y) 2227c478bd9Sstevel@tonic-gate { 2237c478bd9Sstevel@tonic-gate int rel_needed = 0; 2247c478bd9Sstevel@tonic-gate dev_info_t *dip; 2257c478bd9Sstevel@tonic-gate dev_t dev; 2267c478bd9Sstevel@tonic-gate 2277c478bd9Sstevel@tonic-gate /* 2287c478bd9Sstevel@tonic-gate * If we have loaded the console IO stuff, then ask for the screen 2297c478bd9Sstevel@tonic-gate * size properties from the layered terminal emulator. Else ask for 2307c478bd9Sstevel@tonic-gate * them from the root node, which will eventually fall through to the 2317c478bd9Sstevel@tonic-gate * options node and get them from the prom. 2327c478bd9Sstevel@tonic-gate */ 233*fea9cb91Slq150181 if (rwsconsvp == NULL || consmode == CONS_FW) { 2347c478bd9Sstevel@tonic-gate dip = ddi_root_node(); 2357c478bd9Sstevel@tonic-gate dev = DDI_DEV_T_ANY; 2367c478bd9Sstevel@tonic-gate } else { 237*fea9cb91Slq150181 dev = rwsconsvp->v_rdev; /* layering is wc -> tem */ 238*fea9cb91Slq150181 dip = e_ddi_hold_devi_by_dev(dev, 0); 2397c478bd9Sstevel@tonic-gate rel_needed = 1; 2407c478bd9Sstevel@tonic-gate } 2417c478bd9Sstevel@tonic-gate 2427c478bd9Sstevel@tonic-gate /* 2437c478bd9Sstevel@tonic-gate * If we have not initialized a console yet and don't have a root 2447c478bd9Sstevel@tonic-gate * node (ie. we have not initialized the DDI yet) return our default 2457c478bd9Sstevel@tonic-gate * size for the screen. 2467c478bd9Sstevel@tonic-gate */ 2477c478bd9Sstevel@tonic-gate if (dip == NULL) { 2487c478bd9Sstevel@tonic-gate *r = LOSCREENLINES; 2497c478bd9Sstevel@tonic-gate *c = LOSCREENCOLS; 2507c478bd9Sstevel@tonic-gate *x = *y = 0; 2517c478bd9Sstevel@tonic-gate return; 2527c478bd9Sstevel@tonic-gate } 2537c478bd9Sstevel@tonic-gate 254*fea9cb91Slq150181 console_getprop(DDI_DEV_T_ANY, dip, "screen-#columns", c); 255*fea9cb91Slq150181 console_getprop(DDI_DEV_T_ANY, dip, "screen-#rows", r); 256*fea9cb91Slq150181 console_getprop(DDI_DEV_T_ANY, dip, "screen-width", x); 257*fea9cb91Slq150181 console_getprop(DDI_DEV_T_ANY, dip, "screen-height", y); 2587c478bd9Sstevel@tonic-gate 2597c478bd9Sstevel@tonic-gate if (*c < MINCOLS) 2607c478bd9Sstevel@tonic-gate *c = LOSCREENCOLS; 2617c478bd9Sstevel@tonic-gate else if (*c > MAXCOLS) 2627c478bd9Sstevel@tonic-gate *c = HISCREENCOLS; 2637c478bd9Sstevel@tonic-gate 2647c478bd9Sstevel@tonic-gate if (*r < MINLINES) 2657c478bd9Sstevel@tonic-gate *r = LOSCREENLINES; 2667c478bd9Sstevel@tonic-gate else if (*r > MAXLINES) 2677c478bd9Sstevel@tonic-gate *r = HISCREENLINES; 2687c478bd9Sstevel@tonic-gate 2697c478bd9Sstevel@tonic-gate if (rel_needed) 2707c478bd9Sstevel@tonic-gate ddi_release_devi(dip); 2717c478bd9Sstevel@tonic-gate } 2727c478bd9Sstevel@tonic-gate 2737c478bd9Sstevel@tonic-gate typedef struct console_msg { 2747c478bd9Sstevel@tonic-gate size_t cm_size; 2757c478bd9Sstevel@tonic-gate char cm_text[1]; 2767c478bd9Sstevel@tonic-gate } console_msg_t; 2777c478bd9Sstevel@tonic-gate 278*fea9cb91Slq150181 /* 279*fea9cb91Slq150181 * If we can't access the console stream, fall through to PROM, which redirects 280*fea9cb91Slq150181 * it back into to terminal emulator as appropriate. The console stream should 281*fea9cb91Slq150181 * be available after consconfig runs. 282*fea9cb91Slq150181 */ 2837c478bd9Sstevel@tonic-gate static void 2847c478bd9Sstevel@tonic-gate console_putmsg(console_msg_t *cm) 2857c478bd9Sstevel@tonic-gate { 2867c478bd9Sstevel@tonic-gate int busy, spl; 2877c478bd9Sstevel@tonic-gate ssize_t res; 2887c478bd9Sstevel@tonic-gate 2897c478bd9Sstevel@tonic-gate ASSERT(taskq_member(console_taskq, curthread)); 2907c478bd9Sstevel@tonic-gate 2917c478bd9Sstevel@tonic-gate if (rconsvp == NULL || panicstr || 2927c478bd9Sstevel@tonic-gate vn_rdwr(UIO_WRITE, console_vnode, cm->cm_text, strlen(cm->cm_text), 2937c478bd9Sstevel@tonic-gate 0, UIO_SYSSPACE, FAPPEND, (rlim64_t)LOG_HIWAT, kcred, &res) != 0) { 2947c478bd9Sstevel@tonic-gate 2957c478bd9Sstevel@tonic-gate busy = console_hold(); 2967c478bd9Sstevel@tonic-gate spl = console_enter(busy); 2977c478bd9Sstevel@tonic-gate 2987c478bd9Sstevel@tonic-gate prom_printf("%s", cm->cm_text); 2997c478bd9Sstevel@tonic-gate 3007c478bd9Sstevel@tonic-gate console_exit(busy, spl); 3017c478bd9Sstevel@tonic-gate console_rele(); 3027c478bd9Sstevel@tonic-gate } 3037c478bd9Sstevel@tonic-gate 3047c478bd9Sstevel@tonic-gate kmem_free(cm, cm->cm_size); 3057c478bd9Sstevel@tonic-gate } 3067c478bd9Sstevel@tonic-gate 3077c478bd9Sstevel@tonic-gate void 3087c478bd9Sstevel@tonic-gate console_vprintf(const char *fmt, va_list adx) 3097c478bd9Sstevel@tonic-gate { 3107c478bd9Sstevel@tonic-gate console_msg_t *cm; 3117c478bd9Sstevel@tonic-gate size_t len = vsnprintf(NULL, 0, fmt, adx); 3127c478bd9Sstevel@tonic-gate int busy, spl; 3137c478bd9Sstevel@tonic-gate 3147c478bd9Sstevel@tonic-gate if (console_taskq != NULL && rconsvp != NULL && panicstr == NULL && 3157c478bd9Sstevel@tonic-gate (cm = kmem_alloc(sizeof (*cm) + len, KM_NOSLEEP)) != NULL) { 3167c478bd9Sstevel@tonic-gate cm->cm_size = sizeof (*cm) + len; 3177c478bd9Sstevel@tonic-gate (void) vsnprintf(cm->cm_text, len + 1, fmt, adx); 3187c478bd9Sstevel@tonic-gate if (taskq_dispatch(console_taskq, (task_func_t *)console_putmsg, 3197c478bd9Sstevel@tonic-gate cm, TQ_NOSLEEP) != 0) 3207c478bd9Sstevel@tonic-gate return; 3217c478bd9Sstevel@tonic-gate kmem_free(cm, cm->cm_size); 3227c478bd9Sstevel@tonic-gate } 3237c478bd9Sstevel@tonic-gate 3247c478bd9Sstevel@tonic-gate busy = console_hold(); 3257c478bd9Sstevel@tonic-gate spl = console_enter(busy); 3267c478bd9Sstevel@tonic-gate 3277c478bd9Sstevel@tonic-gate prom_vprintf(fmt, adx); 3287c478bd9Sstevel@tonic-gate 3297c478bd9Sstevel@tonic-gate console_exit(busy, spl); 3307c478bd9Sstevel@tonic-gate console_rele(); 3317c478bd9Sstevel@tonic-gate } 3327c478bd9Sstevel@tonic-gate 3337c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/ 3347c478bd9Sstevel@tonic-gate void 3357c478bd9Sstevel@tonic-gate console_printf(const char *fmt, ...) 3367c478bd9Sstevel@tonic-gate { 3377c478bd9Sstevel@tonic-gate va_list adx; 3387c478bd9Sstevel@tonic-gate 3397c478bd9Sstevel@tonic-gate va_start(adx, fmt); 3407c478bd9Sstevel@tonic-gate console_vprintf(fmt, adx); 3417c478bd9Sstevel@tonic-gate va_end(adx); 3427c478bd9Sstevel@tonic-gate } 3437c478bd9Sstevel@tonic-gate 3447c478bd9Sstevel@tonic-gate /* 345*fea9cb91Slq150181 * Avoid calling this function. 346*fea9cb91Slq150181 * 347*fea9cb91Slq150181 * Nothing in the kernel besides the wscons driver (wc) uses this 348*fea9cb91Slq150181 * function. It may hopefully one day be removed altogether. 349*fea9cb91Slq150181 * If a wayward module calls this they will pass through to PROM, 350*fea9cb91Slq150181 * get redirected into the kernel emulator as appropriate. 3517c478bd9Sstevel@tonic-gate */ 3527c478bd9Sstevel@tonic-gate void 3537c478bd9Sstevel@tonic-gate console_puts(const char *s, size_t n) 3547c478bd9Sstevel@tonic-gate { 3557c478bd9Sstevel@tonic-gate int busy, spl; 3567c478bd9Sstevel@tonic-gate 3577c478bd9Sstevel@tonic-gate busy = console_hold(); 3587c478bd9Sstevel@tonic-gate spl = console_enter(busy); 3597c478bd9Sstevel@tonic-gate 3607c478bd9Sstevel@tonic-gate prom_writestr(s, n); 3617c478bd9Sstevel@tonic-gate 3627c478bd9Sstevel@tonic-gate console_exit(busy, spl); 3637c478bd9Sstevel@tonic-gate console_rele(); 3647c478bd9Sstevel@tonic-gate } 3657c478bd9Sstevel@tonic-gate 366*fea9cb91Slq150181 /* 367*fea9cb91Slq150181 * Let this function just go straight through to the PROM, since 368*fea9cb91Slq150181 * we are called in early boot prior to the kernel terminal 369*fea9cb91Slq150181 * emulator being available, and prior to the PROM stdout redirect 370*fea9cb91Slq150181 * vector being set. 371*fea9cb91Slq150181 */ 372*fea9cb91Slq150181 static void 3737c478bd9Sstevel@tonic-gate console_putc(int c) 3747c478bd9Sstevel@tonic-gate { 3757c478bd9Sstevel@tonic-gate int busy = console_hold(); 3767c478bd9Sstevel@tonic-gate int spl = console_enter(busy); 3777c478bd9Sstevel@tonic-gate 3787c478bd9Sstevel@tonic-gate if (c == '\n') 3797c478bd9Sstevel@tonic-gate prom_putchar('\r'); 3807c478bd9Sstevel@tonic-gate prom_putchar(c); 3817c478bd9Sstevel@tonic-gate 3827c478bd9Sstevel@tonic-gate console_exit(busy, spl); 3837c478bd9Sstevel@tonic-gate console_rele(); 3847c478bd9Sstevel@tonic-gate } 3857c478bd9Sstevel@tonic-gate 3867c478bd9Sstevel@tonic-gate /* 3877c478bd9Sstevel@tonic-gate * Read a string from the console device. We only permit synchronous 3887c478bd9Sstevel@tonic-gate * conversation between the kernel and a console user early in boot prior to 3897c478bd9Sstevel@tonic-gate * the initialization of rconsvp. 3907c478bd9Sstevel@tonic-gate */ 3917c478bd9Sstevel@tonic-gate void 3927c478bd9Sstevel@tonic-gate console_gets(char *s, size_t len) 3937c478bd9Sstevel@tonic-gate { 3947c478bd9Sstevel@tonic-gate char *p = s; 3957c478bd9Sstevel@tonic-gate char *q = s + len - 1; 3967c478bd9Sstevel@tonic-gate int c; 3977c478bd9Sstevel@tonic-gate 3987c478bd9Sstevel@tonic-gate ASSERT(rconsvp == NULL); 3997c478bd9Sstevel@tonic-gate (void) console_hold(); 4007c478bd9Sstevel@tonic-gate 4017c478bd9Sstevel@tonic-gate for (;;) { 4027c478bd9Sstevel@tonic-gate switch (c = (prom_getchar() & 0x7f)) { 4037c478bd9Sstevel@tonic-gate case 0x7f: /* DEL */ 4047c478bd9Sstevel@tonic-gate if (p == s) 4057c478bd9Sstevel@tonic-gate break; 4067c478bd9Sstevel@tonic-gate console_putc(c); 4077c478bd9Sstevel@tonic-gate c = '\b'; 4087c478bd9Sstevel@tonic-gate /*FALLTHRU*/ 4097c478bd9Sstevel@tonic-gate 4107c478bd9Sstevel@tonic-gate case '\b': 4117c478bd9Sstevel@tonic-gate if (p == s) 4127c478bd9Sstevel@tonic-gate break; 4137c478bd9Sstevel@tonic-gate console_putc('\b'); 4147c478bd9Sstevel@tonic-gate console_putc(' '); 4157c478bd9Sstevel@tonic-gate /*FALLTHRU*/ 4167c478bd9Sstevel@tonic-gate 4177c478bd9Sstevel@tonic-gate case '#': /* historical backspace alias */ 4187c478bd9Sstevel@tonic-gate console_putc(c); 4197c478bd9Sstevel@tonic-gate if (p > s) 4207c478bd9Sstevel@tonic-gate p--; 4217c478bd9Sstevel@tonic-gate break; 4227c478bd9Sstevel@tonic-gate 4237c478bd9Sstevel@tonic-gate case CTRL('u'): 4247c478bd9Sstevel@tonic-gate console_putc(c); 4257c478bd9Sstevel@tonic-gate console_putc('\n'); 4267c478bd9Sstevel@tonic-gate p = s; 4277c478bd9Sstevel@tonic-gate break; 4287c478bd9Sstevel@tonic-gate 4297c478bd9Sstevel@tonic-gate case '\r': 4307c478bd9Sstevel@tonic-gate case '\n': 4317c478bd9Sstevel@tonic-gate console_putc('\n'); 4327c478bd9Sstevel@tonic-gate goto done; 4337c478bd9Sstevel@tonic-gate 4347c478bd9Sstevel@tonic-gate default: 4357c478bd9Sstevel@tonic-gate if (p < q) { 4367c478bd9Sstevel@tonic-gate console_putc(c); 4377c478bd9Sstevel@tonic-gate *p++ = c; 4387c478bd9Sstevel@tonic-gate } else 4397c478bd9Sstevel@tonic-gate console_putc('\a'); 4407c478bd9Sstevel@tonic-gate } 4417c478bd9Sstevel@tonic-gate } 4427c478bd9Sstevel@tonic-gate done: 4437c478bd9Sstevel@tonic-gate console_rele(); 4447c478bd9Sstevel@tonic-gate *p = '\0'; 4457c478bd9Sstevel@tonic-gate } 4467c478bd9Sstevel@tonic-gate 4477c478bd9Sstevel@tonic-gate /* 4487c478bd9Sstevel@tonic-gate * Read a character from the console device. Synchronous conversation between 4497c478bd9Sstevel@tonic-gate * the kernel and a console user is only permitted early in boot prior to the 4507c478bd9Sstevel@tonic-gate * initialization of rconsvp. 4517c478bd9Sstevel@tonic-gate */ 4527c478bd9Sstevel@tonic-gate int 4537c478bd9Sstevel@tonic-gate console_getc(void) 4547c478bd9Sstevel@tonic-gate { 4557c478bd9Sstevel@tonic-gate int c; 4567c478bd9Sstevel@tonic-gate 4577c478bd9Sstevel@tonic-gate ASSERT(rconsvp == NULL); 4587c478bd9Sstevel@tonic-gate c = prom_getchar(); 4597c478bd9Sstevel@tonic-gate 4607c478bd9Sstevel@tonic-gate if (c == '\r') 4617c478bd9Sstevel@tonic-gate c = '\n'; 4627c478bd9Sstevel@tonic-gate 4637c478bd9Sstevel@tonic-gate console_putc(c); 4647c478bd9Sstevel@tonic-gate return (c); 4657c478bd9Sstevel@tonic-gate } 466