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