summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/os/console.c
blob: 9145eaf8089d073f77f617bbe185bf88a29eea7e (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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*
 * 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 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include <sys/types.h>
#include <sys/varargs.h>
#include <sys/modctl.h>
#include <sys/cmn_err.h>
#include <sys/console.h>
#include <sys/consdev.h>
#include <sys/promif.h>
#include <sys/note.h>
#include <sys/polled_io.h>
#include <sys/systm.h>
#include <sys/file.h>
#include <sys/conf.h>
#include <sys/kmem.h>
#include <sys/taskq.h>
#include <sys/log.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/esunddi.h>
#include <sys/fs/snode.h>
#include <sys/termios.h>
#include <sys/tem_impl.h>

#define	MINLINES	10
#define	MAXLINES	48
#define	LOSCREENLINES	34
#define	HISCREENLINES	48

#define	MINCOLS		10
#define	MAXCOLS		120
#define	LOSCREENCOLS	80
#define	HISCREENCOLS	120

vnode_t *console_vnode;
taskq_t *console_taskq;

/*
 * The current set of polled I/O routines (if any)
 */
struct cons_polledio *cons_polledio;

/*
 * Console I/O Routines
 *
 * In the event that kernel messages are generated with cmn_err(9F) or printf()
 * early in boot, after a panic, in resource-constrained situations, or sent
 * through /dev/console to the wscons driver, we may be called upon to render
 * characters directly to the frame buffer using the underlying prom_*()
 * routines.  These in turn may attempt to use PROM services directly, or may
 * use a kernel console emulator if one is available.  Unfortunately, if PROM
 * services are being used by the kernel on a multi-CPU system, these routines
 * might be called while another CPU is simultaneously accessing a frame buffer
 * memory mapping (perhaps through the X server).  This situation may not be
 * supported by the frame buffer hardware.
 *
 * To handle this situation, we implement a two-phase locking scheme which we
 * use to protect accesses to the underlying prom_*() rendering routines.  The
 * common-code functions console_hold() and console_rele() are used to gain
 * exclusive access to the console from within the kernel.  We use a standard
 * r/w lock in writer-mode only to implement the kernel lock.  We use an r/w
 * lock instead of a mutex here because character rendering is slow and hold
 * times will be relatively long, and there is no point in adaptively spinning.
 * These routines may be called recursively, in which case subsequent calls
 * just increment the console_depth hold count.  Once exclusive access is
 * gained, we grab the frame buffer device node and block further mappings to
 * it by holding the specfs node lock and the device node's lock.  We then
 * observe if any mappings are present by examining the specfs node's s_mapcnt
 * (non-clone mmaps) and the devinfo node's devi_ref count (clone opens).
 *
 * Then, around each character rendering call, the routines console_enter()
 * and console_exit() are used to inform the platform code that we are
 * accessing the character rendering routines.  These platform routines can
 * then examine the "busy" flag returned by console_enter() and briefly stop
 * the other CPUs so that they cannot access the frame buffer hardware while
 * we are busy rendering characters.  This mess can all be removed when the
 * impossible dream of a unified kernel console emulator is someday realized.
 */

static krwlock_t console_lock;
static uint_t console_depth;
static int console_busy;

extern void pm_cfb_check_and_powerup(void);
extern void pm_cfb_rele(void);

static int
console_hold(void)
{
	if (panicstr != NULL)
		return (console_busy); /* assume exclusive access in panic */

	if (rw_owner(&console_lock) != curthread)
		rw_enter(&console_lock, RW_WRITER);

	if (console_depth++ != 0)
		return (console_busy); /* lock is being entered recursively */

	pm_cfb_check_and_powerup();

#ifdef _HAVE_TEM_FIRMWARE
	if (consmode == CONS_FW && ncpus > 1 && fbvp != NULL) {
		struct snode *csp = VTOS(VTOS(fbvp)->s_commonvp);

		mutex_enter(&csp->s_lock);
		console_busy = csp->s_mapcnt != 0;

		if (csp->s_mapcnt == 0 && fbdip != NULL) {
			mutex_enter(&DEVI(fbdip)->devi_lock);
			console_busy = DEVI(fbdip)->devi_ref != 0;
		}
	}
#endif /* _HAVE_TEM_FIRMWARE */
	return (console_busy);
}

static void
console_rele(void)
{
	if (panicstr != NULL)
		return; /* do not modify lock states if we are panicking */

	ASSERT(RW_WRITE_HELD(&console_lock));
	ASSERT(console_depth != 0);

	if (--console_depth != 0)
		return; /* lock is being dropped recursively */

#ifdef _HAVE_TEM_FIRMWARE
	if (consmode == CONS_FW && ncpus > 1 && fbvp != NULL) {
		struct snode *csp = VTOS(VTOS(fbvp)->s_commonvp);

		ASSERT(MUTEX_HELD(&csp->s_lock));
		if (csp->s_mapcnt == 0 && fbdip != NULL)
			mutex_exit(&DEVI(fbdip)->devi_lock);

		mutex_exit(&csp->s_lock);
	}
#endif /* _HAVE_TEM_FIRMWARE */
	pm_cfb_rele();
	console_busy = 0;
	rw_exit(&console_lock);
}

static void
console_getprop(dev_t dev, dev_info_t *dip, char *name, ushort_t *sp)
{
	uchar_t *data;
	uint_t len;
	uint_t i;

	*sp = 0;
	if (ddi_prop_lookup_byte_array(dev, dip, 0, name, &data, &len) ==
	    DDI_PROP_SUCCESS) {
		for (i = 0; i < len; i++) {
			if (data[i] < '0' || data[i] > '9')
				break;
			*sp = *sp * 10 + data[i] - '0';
		}
		ddi_prop_free(data);
	}
}

/*
 * Gets the number of rows and columns (in char's) and the
 * width and height (in pixels) of the console.
 */
void
console_get_size(ushort_t *r, ushort_t *c, ushort_t *x, ushort_t *y)
{
	int rel_needed = 0;
	dev_info_t *dip;
	dev_t dev;

	/*
	 * If we have loaded the console IO stuff, then ask for the screen
	 * size properties from the layered terminal emulator.  Else ask for
	 * them from the root node, which will eventually fall through to the
	 * options node and get them from the prom.
	 */
	if (rwsconsvp == NULL || consmode == CONS_FW) {
		dip = ddi_root_node();
		dev = DDI_DEV_T_ANY;
	} else {
		dev = rwsconsvp->v_rdev; /* layering is wc -> tem */
		dip = e_ddi_hold_devi_by_dev(dev, 0);
		rel_needed = 1;
	}

	/*
	 * If we have not initialized a console yet and don't have a root
	 * node (ie. we have not initialized the DDI yet) return our default
	 * size for the screen.
	 */
	if (dip == NULL) {
		*r = LOSCREENLINES;
		*c = LOSCREENCOLS;
		*x = *y = 0;
		return;
	}

	console_getprop(DDI_DEV_T_ANY, dip, "screen-#columns", c);
	console_getprop(DDI_DEV_T_ANY, dip, "screen-#rows", r);
	console_getprop(DDI_DEV_T_ANY, dip, "screen-width", x);
	console_getprop(DDI_DEV_T_ANY, dip, "screen-height", y);

	if (*c < MINCOLS)
		*c = LOSCREENCOLS;
	else if (*c > MAXCOLS)
		*c = HISCREENCOLS;

	if (*r < MINLINES)
		*r = LOSCREENLINES;
	else if (*r > MAXLINES)
		*r = HISCREENLINES;

	if (rel_needed)
		ddi_release_devi(dip);
}

typedef struct console_msg {
	size_t	cm_size;
	char	cm_text[1];
} console_msg_t;

/*
 * If we can't access the console stream, fall through to PROM, which redirects
 * it back into to terminal emulator as appropriate.  The console stream should
 * be available after consconfig runs.
 */
static void
console_putmsg(console_msg_t *cm)
{
	int busy, spl;
	ssize_t res;

	ASSERT(taskq_member(console_taskq, curthread));

	if (rconsvp == NULL || panicstr ||
	    vn_rdwr(UIO_WRITE, console_vnode, cm->cm_text, strlen(cm->cm_text),
	    0, UIO_SYSSPACE, FAPPEND, (rlim64_t)LOG_HIWAT, kcred, &res) != 0) {

		busy = console_hold();
		spl = console_enter(busy);

		prom_printf("%s", cm->cm_text);

		console_exit(busy, spl);
		console_rele();
	}

	kmem_free(cm, cm->cm_size);
}

void
console_vprintf(const char *fmt, va_list adx)
{
	console_msg_t *cm;
	size_t len = vsnprintf(NULL, 0, fmt, adx);
	int busy, spl;

	if (console_taskq != NULL && rconsvp != NULL && panicstr == NULL &&
	    (cm = kmem_alloc(sizeof (*cm) + len, KM_NOSLEEP)) != NULL) {
		cm->cm_size = sizeof (*cm) + len;
		(void) vsnprintf(cm->cm_text, len + 1, fmt, adx);
		if (taskq_dispatch(console_taskq, (task_func_t *)console_putmsg,
		    cm, TQ_NOSLEEP) != 0)
			return;
		kmem_free(cm, cm->cm_size);
	}

	busy = console_hold();
	spl = console_enter(busy);

	prom_vprintf(fmt, adx);

	console_exit(busy, spl);
	console_rele();
}

/*PRINTFLIKE1*/
void
console_printf(const char *fmt, ...)
{
	va_list adx;

	va_start(adx, fmt);
	console_vprintf(fmt, adx);
	va_end(adx);
}

/*
 * Avoid calling this function.
 *
 * Nothing in the kernel besides the wscons driver (wc) uses this
 * function. It may hopefully one day be removed altogether.
 * If a wayward module calls this they will pass through to PROM,
 * get redirected into the kernel emulator as appropriate.
 */
void
console_puts(const char *s, size_t n)
{
	int busy, spl;

	busy = console_hold();
	spl = console_enter(busy);

	prom_writestr(s, n);

	console_exit(busy, spl);
	console_rele();
}

/*
 * Let this function just go straight through to the PROM, since
 * we are called in early boot prior to the kernel terminal
 * emulator being available, and prior to the PROM stdout redirect
 * vector being set.
 */
static void
console_putc(int c)
{
	int busy = console_hold();
	int spl = console_enter(busy);

	if (c == '\n')
		prom_putchar('\r');
	prom_putchar(c);

	console_exit(busy, spl);
	console_rele();
}

/*
 * Read a string from the console device.  We only permit synchronous
 * conversation between the kernel and a console user early in boot prior to
 * the initialization of rconsvp.
 */
void
console_gets(char *s, size_t len)
{
	char *p = s;
	char *q = s + len - 1;
	int c;

	ASSERT(rconsvp == NULL);
	(void) console_hold();

	for (;;) {
		switch (c = (prom_getchar() & 0x7f)) {
		case 0x7f: /* DEL */
			if (p == s)
				break;
			console_putc(c);
			c = '\b';
			/*FALLTHRU*/

		case '\b':
			if (p == s)
				break;
			console_putc('\b');
			console_putc(' ');
			/*FALLTHRU*/

		case '#': /* historical backspace alias */
			console_putc(c);
			if (p > s)
				p--;
			break;

		case CTRL('u'):
			console_putc(c);
			console_putc('\n');
			p = s;
			break;

		case '\r':
		case '\n':
			console_putc('\n');
			goto done;

		default:
			if (p < q) {
				console_putc(c);
				*p++ = c;
			} else
				console_putc('\a');
		}
	}
done:
	console_rele();
	*p = '\0';
}

/*
 * Read a character from the console device.  Synchronous conversation between
 * the kernel and a console user is only permitted early in boot prior to the
 * initialization of rconsvp.
 */
int
console_getc(void)
{
	int c;

	ASSERT(rconsvp == NULL);
	c = prom_getchar();

	if (c == '\r')
		c = '\n';

	console_putc(c);
	return (c);
}