]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/kern_cons.c
Merge compiler-rt trunk r321017 to contrib/compiler-rt.
[FreeBSD/FreeBSD.git] / sys / kern / kern_cons.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1988 University of Utah.
5  * Copyright (c) 1991 The Regents of the University of California.
6  * Copyright (c) 1999 Michael Smith
7  * Copyright (c) 2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
8  *
9  * All rights reserved.
10  *
11  * This code is derived from software contributed to Berkeley by
12  * the Systems Programming Group of the University of Utah Computer
13  * Science Department.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions
17  * are met:
18  * 1. Redistributions of source code must retain the above copyright
19  *    notice, this list of conditions and the following disclaimer.
20  * 2. Redistributions in binary form must reproduce the above copyright
21  *    notice, this list of conditions and the following disclaimer in the
22  *    documentation and/or other materials provided with the distribution.
23  * 3. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  *
39  *      from: @(#)cons.c        7.2 (Berkeley) 5/9/91
40  */
41
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
44
45 #include "opt_ddb.h"
46 #include "opt_syscons.h"
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/lock.h>
51 #include <sys/mutex.h>
52 #include <sys/conf.h>
53 #include <sys/cons.h>
54 #include <sys/fcntl.h>
55 #include <sys/kdb.h>
56 #include <sys/kernel.h>
57 #include <sys/malloc.h>
58 #include <sys/msgbuf.h>
59 #include <sys/namei.h>
60 #include <sys/priv.h>
61 #include <sys/proc.h>
62 #include <sys/queue.h>
63 #include <sys/reboot.h>
64 #include <sys/sysctl.h>
65 #include <sys/sbuf.h>
66 #include <sys/tty.h>
67 #include <sys/uio.h>
68 #include <sys/vnode.h>
69
70 #include <ddb/ddb.h>
71
72 #include <machine/cpu.h>
73 #include <machine/clock.h>
74
75 static MALLOC_DEFINE(M_TTYCONS, "tty console", "tty console handling");
76
77 struct cn_device {
78         STAILQ_ENTRY(cn_device) cnd_next;
79         struct          consdev *cnd_cn;
80 };
81
82 #define CNDEVPATHMAX    32
83 #define CNDEVTAB_SIZE   4
84 static struct cn_device cn_devtab[CNDEVTAB_SIZE];
85 static STAILQ_HEAD(, cn_device) cn_devlist =
86     STAILQ_HEAD_INITIALIZER(cn_devlist);
87
88 int     cons_avail_mask = 0;    /* Bit mask. Each registered low level console
89                                  * which is currently unavailable for inpit
90                                  * (i.e., if it is in graphics mode) will have
91                                  * this bit cleared.
92                                  */
93 static int cn_mute;
94 static char *consbuf;                   /* buffer used by `consmsgbuf' */
95 static struct callout conscallout;      /* callout for outputting to constty */
96 struct msgbuf consmsgbuf;               /* message buffer for console tty */
97 static u_char console_pausing;          /* pause after each line during probe */
98 static char *console_pausestr=
99 "<pause; press any key to proceed to next line or '.' to end pause mode>";
100 struct tty *constty;                    /* pointer to console "window" tty */
101 static struct mtx cnputs_mtx;           /* Mutex for cnputs(). */
102 static int use_cnputs_mtx = 0;          /* != 0 if cnputs_mtx locking reqd. */
103
104 static void constty_timeout(void *arg);
105
106 static struct consdev cons_consdev;
107 DATA_SET(cons_set, cons_consdev);
108 SET_DECLARE(cons_set, struct consdev);
109
110 void
111 cninit(void)
112 {
113         struct consdev *best_cn, *cn, **list;
114
115         /*
116          * Check if we should mute the console (for security reasons perhaps)
117          * It can be changes dynamically using sysctl kern.consmute
118          * once we are up and going.
119          * 
120          */
121         cn_mute = ((boothowto & (RB_MUTE
122                         |RB_SINGLE
123                         |RB_VERBOSE
124                         |RB_ASKNAME)) == RB_MUTE);
125
126         /*
127          * Find the first console with the highest priority.
128          */
129         best_cn = NULL;
130         SET_FOREACH(list, cons_set) {
131                 cn = *list;
132                 cnremove(cn);
133                 /* Skip cons_consdev. */
134                 if (cn->cn_ops == NULL)
135                         continue;
136                 cn->cn_ops->cn_probe(cn);
137                 if (cn->cn_pri == CN_DEAD)
138                         continue;
139                 if (best_cn == NULL || cn->cn_pri > best_cn->cn_pri)
140                         best_cn = cn;
141                 if (boothowto & RB_MULTIPLE) {
142                         /*
143                          * Initialize console, and attach to it.
144                          */
145                         cn->cn_ops->cn_init(cn);
146                         cnadd(cn);
147                 }
148         }
149         if (best_cn == NULL)
150                 return;
151         if ((boothowto & RB_MULTIPLE) == 0) {
152                 best_cn->cn_ops->cn_init(best_cn);
153                 cnadd(best_cn);
154         }
155         if (boothowto & RB_PAUSE)
156                 console_pausing = 1;
157         /*
158          * Make the best console the preferred console.
159          */
160         cnselect(best_cn);
161
162 #ifdef EARLY_PRINTF
163         /*
164          * Release early console.
165          */
166         early_putc = NULL;
167 #endif
168 }
169
170 void
171 cninit_finish()
172 {
173         console_pausing = 0;
174
175
176 /* add a new physical console to back the virtual console */
177 int
178 cnadd(struct consdev *cn)
179 {
180         struct cn_device *cnd;
181         int i;
182
183         STAILQ_FOREACH(cnd, &cn_devlist, cnd_next)
184                 if (cnd->cnd_cn == cn)
185                         return (0);
186         for (i = 0; i < CNDEVTAB_SIZE; i++) {
187                 cnd = &cn_devtab[i];
188                 if (cnd->cnd_cn == NULL)
189                         break;
190         }
191         if (cnd->cnd_cn != NULL)
192                 return (ENOMEM);
193         cnd->cnd_cn = cn;
194         if (cn->cn_name[0] == '\0') {
195                 /* XXX: it is unclear if/where this print might output */
196                 printf("WARNING: console at %p has no name\n", cn);
197         }
198         STAILQ_INSERT_TAIL(&cn_devlist, cnd, cnd_next);
199         if (STAILQ_FIRST(&cn_devlist) == cnd)
200                 ttyconsdev_select(cnd->cnd_cn->cn_name);
201
202         /* Add device to the active mask. */
203         cnavailable(cn, (cn->cn_flags & CN_FLAG_NOAVAIL) == 0);
204
205         return (0);
206 }
207
208 void
209 cnremove(struct consdev *cn)
210 {
211         struct cn_device *cnd;
212         int i;
213
214         STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
215                 if (cnd->cnd_cn != cn)
216                         continue;
217                 if (STAILQ_FIRST(&cn_devlist) == cnd)
218                         ttyconsdev_select(NULL);
219                 STAILQ_REMOVE(&cn_devlist, cnd, cn_device, cnd_next);
220                 cnd->cnd_cn = NULL;
221
222                 /* Remove this device from available mask. */
223                 for (i = 0; i < CNDEVTAB_SIZE; i++) 
224                         if (cnd == &cn_devtab[i]) {
225                                 cons_avail_mask &= ~(1 << i);
226                                 break;
227                         }
228 #if 0
229                 /*
230                  * XXX
231                  * syscons gets really confused if console resources are
232                  * freed after the system has initialized.
233                  */
234                 if (cn->cn_term != NULL)
235                         cn->cn_ops->cn_term(cn);
236 #endif
237                 return;
238         }
239 }
240
241 void
242 cnselect(struct consdev *cn)
243 {
244         struct cn_device *cnd;
245
246         STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
247                 if (cnd->cnd_cn != cn)
248                         continue;
249                 if (cnd == STAILQ_FIRST(&cn_devlist))
250                         return;
251                 STAILQ_REMOVE(&cn_devlist, cnd, cn_device, cnd_next);
252                 STAILQ_INSERT_HEAD(&cn_devlist, cnd, cnd_next);
253                 ttyconsdev_select(cnd->cnd_cn->cn_name);
254                 return;
255         }
256 }
257
258 void
259 cnavailable(struct consdev *cn, int available)
260 {
261         int i;
262
263         for (i = 0; i < CNDEVTAB_SIZE; i++) {
264                 if (cn_devtab[i].cnd_cn == cn)
265                         break;
266         }
267         if (available) {
268                 if (i < CNDEVTAB_SIZE)
269                         cons_avail_mask |= (1 << i); 
270                 cn->cn_flags &= ~CN_FLAG_NOAVAIL;
271         } else {
272                 if (i < CNDEVTAB_SIZE)
273                         cons_avail_mask &= ~(1 << i);
274                 cn->cn_flags |= CN_FLAG_NOAVAIL;
275         }
276 }
277
278 int
279 cnunavailable(void)
280 {
281
282         return (cons_avail_mask == 0);
283 }
284
285 /*
286  * sysctl_kern_console() provides output parseable in conscontrol(1).
287  */
288 static int
289 sysctl_kern_console(SYSCTL_HANDLER_ARGS)
290 {
291         struct cn_device *cnd;
292         struct consdev *cp, **list;
293         char *p;
294         int delete, error;
295         struct sbuf *sb;
296
297         sb = sbuf_new(NULL, NULL, CNDEVPATHMAX * 2, SBUF_AUTOEXTEND |
298             SBUF_INCLUDENUL);
299         if (sb == NULL)
300                 return (ENOMEM);
301         sbuf_clear(sb);
302         STAILQ_FOREACH(cnd, &cn_devlist, cnd_next)
303                 sbuf_printf(sb, "%s,", cnd->cnd_cn->cn_name);
304         sbuf_printf(sb, "/");
305         SET_FOREACH(list, cons_set) {
306                 cp = *list;
307                 if (cp->cn_name[0] != '\0')
308                         sbuf_printf(sb, "%s,", cp->cn_name);
309         }
310         sbuf_finish(sb);
311         error = sysctl_handle_string(oidp, sbuf_data(sb), sbuf_len(sb), req);
312         if (error == 0 && req->newptr != NULL) {
313                 p = sbuf_data(sb);
314                 error = ENXIO;
315                 delete = 0;
316                 if (*p == '-') {
317                         delete = 1;
318                         p++;
319                 }
320                 SET_FOREACH(list, cons_set) {
321                         cp = *list;
322                         if (strcmp(p, cp->cn_name) != 0)
323                                 continue;
324                         if (delete) {
325                                 cnremove(cp);
326                                 error = 0;
327                         } else {
328                                 error = cnadd(cp);
329                                 if (error == 0)
330                                         cnselect(cp);
331                         }
332                         break;
333                 }
334         }
335         sbuf_delete(sb);
336         return (error);
337 }
338
339 SYSCTL_PROC(_kern, OID_AUTO, console, CTLTYPE_STRING|CTLFLAG_RW,
340         0, 0, sysctl_kern_console, "A", "Console device control");
341
342 /*
343  * User has changed the state of the console muting.
344  * This may require us to open or close the device in question.
345  */
346 static int
347 sysctl_kern_consmute(SYSCTL_HANDLER_ARGS)
348 {
349         int error;
350
351         error = sysctl_handle_int(oidp, &cn_mute, 0, req);
352         if (error != 0 || req->newptr == NULL)
353                 return (error);
354         return (error);
355 }
356
357 SYSCTL_PROC(_kern, OID_AUTO, consmute, CTLTYPE_INT|CTLFLAG_RW,
358         0, sizeof(cn_mute), sysctl_kern_consmute, "I",
359         "State of the console muting");
360
361 void
362 cngrab()
363 {
364         struct cn_device *cnd;
365         struct consdev *cn;
366
367         STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
368                 cn = cnd->cnd_cn;
369                 if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG))
370                         cn->cn_ops->cn_grab(cn);
371         }
372 }
373
374 void
375 cnungrab()
376 {
377         struct cn_device *cnd;
378         struct consdev *cn;
379
380         STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
381                 cn = cnd->cnd_cn;
382                 if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG))
383                         cn->cn_ops->cn_ungrab(cn);
384         }
385 }
386
387 /*
388  * Low level console routines.
389  */
390 int
391 cngetc(void)
392 {
393         int c;
394
395         if (cn_mute)
396                 return (-1);
397         while ((c = cncheckc()) == -1)
398                 cpu_spinwait();
399         if (c == '\r')
400                 c = '\n';               /* console input is always ICRNL */
401         return (c);
402 }
403
404 int
405 cncheckc(void)
406 {
407         struct cn_device *cnd;
408         struct consdev *cn;
409         int c;
410
411         if (cn_mute)
412                 return (-1);
413         STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
414                 cn = cnd->cnd_cn;
415                 if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG)) {
416                         c = cn->cn_ops->cn_getc(cn);
417                         if (c != -1)
418                                 return (c);
419                 }
420         }
421         return (-1);
422 }
423
424 void
425 cngets(char *cp, size_t size, int visible)
426 {
427         char *lp, *end;
428         int c;
429
430         cngrab();
431
432         lp = cp;
433         end = cp + size - 1;
434         for (;;) {
435                 c = cngetc() & 0177;
436                 switch (c) {
437                 case '\n':
438                 case '\r':
439                         cnputc(c);
440                         *lp = '\0';
441                         cnungrab();
442                         return;
443                 case '\b':
444                 case '\177':
445                         if (lp > cp) {
446                                 if (visible)
447                                         cnputs("\b \b");
448                                 lp--;
449                         }
450                         continue;
451                 case '\0':
452                         continue;
453                 default:
454                         if (lp < end) {
455                                 switch (visible) {
456                                 case GETS_NOECHO:
457                                         break;
458                                 case GETS_ECHOPASS:
459                                         cnputc('*');
460                                         break;
461                                 default:
462                                         cnputc(c);
463                                         break;
464                                 }
465                                 *lp++ = c;
466                         }
467                 }
468         }
469 }
470
471 void
472 cnputc(int c)
473 {
474         struct cn_device *cnd;
475         struct consdev *cn;
476         char *cp;
477
478 #ifdef EARLY_PRINTF
479         if (early_putc != NULL) {
480                 if (c == '\n')
481                         early_putc('\r');
482                 early_putc(c);
483                 return;
484         }
485 #endif
486
487         if (cn_mute || c == '\0')
488                 return;
489         STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
490                 cn = cnd->cnd_cn;
491                 if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG)) {
492                         if (c == '\n')
493                                 cn->cn_ops->cn_putc(cn, '\r');
494                         cn->cn_ops->cn_putc(cn, c);
495                 }
496         }
497         if (console_pausing && c == '\n' && !kdb_active) {
498                 for (cp = console_pausestr; *cp != '\0'; cp++)
499                         cnputc(*cp);
500                 cngrab();
501                 if (cngetc() == '.')
502                         console_pausing = 0;
503                 cnungrab();
504                 cnputc('\r');
505                 for (cp = console_pausestr; *cp != '\0'; cp++)
506                         cnputc(' ');
507                 cnputc('\r');
508         }
509 }
510
511 void
512 cnputs(char *p)
513 {
514         int c;
515         int unlock_reqd = 0;
516
517         if (use_cnputs_mtx) {
518                 /*
519                  * NOTE: Debug prints and/or witness printouts in
520                  * console driver clients can cause the "cnputs_mtx"
521                  * mutex to recurse. Simply return if that happens.
522                  */
523                 if (mtx_owned(&cnputs_mtx))
524                         return;
525                 mtx_lock_spin(&cnputs_mtx);
526                 unlock_reqd = 1;
527         }
528
529         while ((c = *p++) != '\0')
530                 cnputc(c);
531
532         if (unlock_reqd)
533                 mtx_unlock_spin(&cnputs_mtx);
534 }
535
536 static int consmsgbuf_size = 8192;
537 SYSCTL_INT(_kern, OID_AUTO, consmsgbuf_size, CTLFLAG_RW, &consmsgbuf_size, 0,
538     "Console tty buffer size");
539
540 /*
541  * Redirect console output to a tty.
542  */
543 void
544 constty_set(struct tty *tp)
545 {
546         int size;
547
548         KASSERT(tp != NULL, ("constty_set: NULL tp"));
549         if (consbuf == NULL) {
550                 size = consmsgbuf_size;
551                 consbuf = malloc(size, M_TTYCONS, M_WAITOK);
552                 msgbuf_init(&consmsgbuf, consbuf, size);
553                 callout_init(&conscallout, 0);
554         }
555         constty = tp;
556         constty_timeout(NULL);
557 }
558
559 /*
560  * Disable console redirection to a tty.
561  */
562 void
563 constty_clear(void)
564 {
565         int c;
566
567         constty = NULL;
568         if (consbuf == NULL)
569                 return;
570         callout_stop(&conscallout);
571         while ((c = msgbuf_getchar(&consmsgbuf)) != -1)
572                 cnputc(c);
573         free(consbuf, M_TTYCONS);
574         consbuf = NULL;
575 }
576
577 /* Times per second to check for pending console tty messages. */
578 static int constty_wakeups_per_second = 5;
579 SYSCTL_INT(_kern, OID_AUTO, constty_wakeups_per_second, CTLFLAG_RW,
580     &constty_wakeups_per_second, 0,
581     "Times per second to check for pending console tty messages");
582
583 static void
584 constty_timeout(void *arg)
585 {
586         int c;
587
588         if (constty != NULL) {
589                 tty_lock(constty);
590                 while ((c = msgbuf_getchar(&consmsgbuf)) != -1) {
591                         if (tty_putchar(constty, c) < 0) {
592                                 tty_unlock(constty);
593                                 constty = NULL;
594                                 break;
595                         }
596                 }
597
598                 if (constty != NULL)
599                         tty_unlock(constty);
600         }
601         if (constty != NULL) {
602                 callout_reset(&conscallout, hz / constty_wakeups_per_second,
603                     constty_timeout, NULL);
604         } else {
605                 /* Deallocate the constty buffer memory. */
606                 constty_clear();
607         }
608 }
609
610 static void
611 cn_drvinit(void *unused)
612 {
613
614         mtx_init(&cnputs_mtx, "cnputs_mtx", NULL, MTX_SPIN | MTX_NOWITNESS);
615         use_cnputs_mtx = 1;
616 }
617
618 SYSINIT(cndev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, cn_drvinit, NULL);
619
620 /*
621  * Sysbeep(), if we have hardware for it
622  */
623
624 #ifdef HAS_TIMER_SPKR
625
626 static int beeping;
627 static struct callout beeping_timer;
628
629 static void
630 sysbeepstop(void *chan)
631 {
632
633         timer_spkr_release();
634         beeping = 0;
635 }
636
637 int
638 sysbeep(int pitch, int period)
639 {
640
641         if (timer_spkr_acquire()) {
642                 if (!beeping) {
643                         /* Something else owns it. */
644                         return (EBUSY);
645                 }
646         }
647         timer_spkr_setfreq(pitch);
648         if (!beeping) {
649                 beeping = period;
650                 callout_reset(&beeping_timer, period, sysbeepstop, NULL);
651         }
652         return (0);
653 }
654
655 static void
656 sysbeep_init(void *unused)
657 {
658
659         callout_init(&beeping_timer, 1);
660 }
661 SYSINIT(sysbeep, SI_SUB_SOFTINTR, SI_ORDER_ANY, sysbeep_init, NULL);
662 #else
663
664 /*
665  * No hardware, no sound
666  */
667
668 int
669 sysbeep(int pitch __unused, int period __unused)
670 {
671
672         return (ENODEV);
673 }
674
675 #endif
676
677 /*
678  * Temporary support for sc(4) to vt(4) transition.
679  */
680 static unsigned vty_prefer;
681 static char vty_name[16];
682 SYSCTL_STRING(_kern, OID_AUTO, vty, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, vty_name,
683     0, "Console vty driver");
684
685 int
686 vty_enabled(unsigned vty)
687 {
688         static unsigned vty_selected = 0;
689
690         if (vty_selected == 0) {
691                 TUNABLE_STR_FETCH("kern.vty", vty_name, sizeof(vty_name));
692                 do {
693 #if defined(DEV_SC)
694                         if (strcmp(vty_name, "sc") == 0) {
695                                 vty_selected = VTY_SC;
696                                 break;
697                         }
698 #endif
699 #if defined(DEV_VT)
700                         if (strcmp(vty_name, "vt") == 0) {
701                                 vty_selected = VTY_VT;
702                                 break;
703                         }
704 #endif
705                         if (vty_prefer != 0) {
706                                 vty_selected = vty_prefer;
707                                 break;
708                         }
709 #if defined(DEV_VT)
710                         vty_selected = VTY_VT;
711 #elif defined(DEV_SC)
712                         vty_selected = VTY_SC;
713 #endif
714                 } while (0);
715
716                 if (vty_selected == VTY_VT)
717                         strcpy(vty_name, "vt");
718                 else if (vty_selected == VTY_SC)
719                         strcpy(vty_name, "sc");
720         }
721         return ((vty_selected & vty) != 0);
722 }
723
724 void
725 vty_set_preferred(unsigned vty)
726 {
727
728         vty_prefer = vty;
729 #if !defined(DEV_SC)
730         vty_prefer &= ~VTY_SC;
731 #endif
732 #if !defined(DEV_VT)
733         vty_prefer &= ~VTY_VT;
734 #endif
735 }
736