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 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 *
26 * Copyright 2026 Oxide Computer Company
27 */
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/systm.h>
38 #include <sys/file.h>
39 #include <sys/conf.h>
40 #include <sys/kmem.h>
41 #include <sys/taskq.h>
42 #include <sys/log.h>
43 #include <sys/ddi.h>
44 #include <sys/sunddi.h>
45 #include <sys/esunddi.h>
46 #include <sys/fs/snode.h>
47 #include <sys/termios.h>
48 #include <sys/tem_impl.h>
49
50 #define MINLINES 10
51 #define MAXLINES 48
52 #define LOSCREENLINES 34
53 #define HISCREENLINES 48
54
55 #define MINCOLS 10
56 #define MAXCOLS 120
57 #define LOSCREENCOLS 80
58 #define HISCREENCOLS 120
59
60 vnode_t *console_vnode;
61 taskq_t *console_taskq;
62
63 /*
64 * The current set of polled I/O routines (if any)
65 */
66 struct cons_polledio *cons_polledio;
67
68 /*
69 * Ask the console driver to prepare for polled use by the debugger or by
70 * panic, and to undo that afterwards. These may be called with the system in
71 * an arbitrary state, including from the debugger with every other CPU
72 * stopped, so the driver hooks behind them must not take any locks nor
73 * depend on any other kernel service.
74 */
75 void
console_polled_enter(void)76 console_polled_enter(void)
77 {
78 if (cons_polledio != NULL &&
79 cons_polledio->cons_polledio_enter != NULL) {
80 cons_polledio->cons_polledio_enter(
81 cons_polledio->cons_polledio_argument);
82 }
83 }
84
85 void
console_polled_exit(void)86 console_polled_exit(void)
87 {
88 if (cons_polledio != NULL &&
89 cons_polledio->cons_polledio_exit != NULL) {
90 cons_polledio->cons_polledio_exit(
91 cons_polledio->cons_polledio_argument);
92 }
93 }
94
95 /*
96 * Console I/O Routines
97 *
98 * In the event that kernel messages are generated with cmn_err(9F) or printf()
99 * early in boot, after a panic, in resource-constrained situations, or sent
100 * through /dev/console to the wscons driver, we may be called upon to render
101 * characters directly to the frame buffer using the underlying prom_*()
102 * routines. These in turn may attempt to use PROM services directly, or may
103 * use a kernel console emulator if one is available. Unfortunately, if PROM
104 * services are being used by the kernel on a multi-CPU system, these routines
105 * might be called while another CPU is simultaneously accessing a frame buffer
106 * memory mapping (perhaps through the X server). This situation may not be
107 * supported by the frame buffer hardware.
108 *
109 * To handle this situation, we implement a two-phase locking scheme which we
110 * use to protect accesses to the underlying prom_*() rendering routines. The
111 * common-code functions console_hold() and console_rele() are used to gain
112 * exclusive access to the console from within the kernel. We use a standard
113 * r/w lock in writer-mode only to implement the kernel lock. We use an r/w
114 * lock instead of a mutex here because character rendering is slow and hold
115 * times will be relatively long, and there is no point in adaptively spinning.
116 * These routines may be called recursively, in which case subsequent calls
117 * just increment the console_depth hold count. Once exclusive access is
118 * gained, we grab the frame buffer device node and block further mappings to
119 * it by holding the specfs node lock and the device node's lock. We then
120 * observe if any mappings are present by examining the specfs node's s_mapcnt
121 * (non-clone mmaps) and the devinfo node's devi_ref count (clone opens).
122 *
123 * Then, around each character rendering call, the routines console_enter()
124 * and console_exit() are used to inform the platform code that we are
125 * accessing the character rendering routines. These platform routines can
126 * then examine the "busy" flag returned by console_enter() and briefly stop
127 * the other CPUs so that they cannot access the frame buffer hardware while
128 * we are busy rendering characters. This mess can all be removed when the
129 * impossible dream of a unified kernel console emulator is someday realized.
130 */
131
132 static krwlock_t console_lock;
133 static uint_t console_depth;
134 static int console_busy;
135
136 extern void pm_cfb_check_and_powerup(void);
137 extern void pm_cfb_rele(void);
138
139 static int
console_hold(void)140 console_hold(void)
141 {
142 if (panicstr != NULL)
143 return (console_busy); /* assume exclusive access in panic */
144
145 if (rw_owner(&console_lock) != curthread)
146 rw_enter(&console_lock, RW_WRITER);
147
148 if (console_depth++ != 0)
149 return (console_busy); /* lock is being entered recursively */
150
151 pm_cfb_check_and_powerup();
152
153 #ifdef _HAVE_TEM_FIRMWARE
154 if (consmode == CONS_FW && ncpus > 1 && fbvp != NULL) {
155 struct snode *csp = VTOS(VTOS(fbvp)->s_commonvp);
156
157 mutex_enter(&csp->s_lock);
158 console_busy = csp->s_mapcnt != 0;
159
160 if (csp->s_mapcnt == 0 && fbdip != NULL) {
161 mutex_enter(&DEVI(fbdip)->devi_lock);
162 console_busy = DEVI(fbdip)->devi_ref != 0;
163 }
164 }
165 #endif /* _HAVE_TEM_FIRMWARE */
166 return (console_busy);
167 }
168
169 static void
console_rele(void)170 console_rele(void)
171 {
172 if (panicstr != NULL)
173 return; /* do not modify lock states if we are panicking */
174
175 ASSERT(RW_WRITE_HELD(&console_lock));
176 ASSERT(console_depth != 0);
177
178 if (--console_depth != 0)
179 return; /* lock is being dropped recursively */
180
181 #ifdef _HAVE_TEM_FIRMWARE
182 if (consmode == CONS_FW && ncpus > 1 && fbvp != NULL) {
183 struct snode *csp = VTOS(VTOS(fbvp)->s_commonvp);
184
185 ASSERT(MUTEX_HELD(&csp->s_lock));
186 if (csp->s_mapcnt == 0 && fbdip != NULL)
187 mutex_exit(&DEVI(fbdip)->devi_lock);
188
189 mutex_exit(&csp->s_lock);
190 }
191 #endif /* _HAVE_TEM_FIRMWARE */
192 pm_cfb_rele();
193 console_busy = 0;
194 rw_exit(&console_lock);
195 }
196
197 static void
console_getprop(dev_t dev,dev_info_t * dip,char * name,ushort_t * sp)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
console_get_size(ushort_t * r,ushort_t * c,ushort_t * x,ushort_t * y)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
console_putmsg(console_msg_t * cm)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
console_vprintf(const char * fmt,va_list adx)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) != TASKQID_INVALID)
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
console_printf(const char * fmt,...)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
console_puts(const char * s,size_t n)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
console_putc(int c)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
console_gets(char * s,size_t len)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
console_getc(void)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