cons.c revision 1.94 1 /* $NetBSD: cons.c,v 1.94 2023/09/02 17:44:12 riastradh Exp $ */
2
3 /*
4 * Copyright (c) 1988 University of Utah.
5 * Copyright (c) 1990, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * the Systems Programming Group of the University of Utah Computer
10 * Science Department.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * from: Utah $Hdr: cons.c 1.7 92/01/21$
37 *
38 * @(#)cons.c 8.2 (Berkeley) 1/12/94
39 */
40
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: cons.c,v 1.94 2023/09/02 17:44:12 riastradh Exp $");
43
44 #ifdef _KERNEL_OPT
45 #include "opt_heartbeat.h"
46 #endif
47
48 #include <sys/param.h>
49
50 #include <sys/atomic.h>
51 #include <sys/buf.h>
52 #include <sys/conf.h>
53 #include <sys/file.h>
54 #include <sys/heartbeat.h>
55 #include <sys/ioctl.h>
56 #include <sys/kauth.h>
57 #include <sys/module.h>
58 #include <sys/mutex.h>
59 #include <sys/poll.h>
60 #include <sys/proc.h>
61 #include <sys/pserialize.h>
62 #include <sys/systm.h>
63 #include <sys/tty.h>
64 #include <sys/vnode.h>
65
66 #include <dev/cons.h>
67
68 #include "nullcons.h"
69
70 dev_type_open(cnopen);
71 dev_type_close(cnclose);
72 dev_type_read(cnread);
73 dev_type_write(cnwrite);
74 dev_type_ioctl(cnioctl);
75 dev_type_poll(cnpoll);
76 dev_type_kqfilter(cnkqfilter);
77
78 static bool cn_redirect(dev_t *, int, int *, struct tty **);
79 static void cn_release(struct tty *);
80
81 const struct cdevsw cons_cdevsw = {
82 .d_open = cnopen,
83 .d_close = cnclose,
84 .d_read = cnread,
85 .d_write = cnwrite,
86 .d_ioctl = cnioctl,
87 .d_stop = nostop,
88 .d_tty = notty,
89 .d_poll = cnpoll,
90 .d_mmap = nommap,
91 .d_kqfilter = cnkqfilter,
92 .d_discard = nodiscard,
93 .d_flag = D_TTY|D_MPSAFE,
94 };
95
96 static struct kmutex cn_lock;
97
98 struct tty *volatile constty; /* virtual console output device */
99 struct consdev *cn_tab; /* physical console device info */
100 struct vnode *cn_devvp[2]; /* vnode for underlying device. */
101
102 void
103 cn_set_tab(struct consdev *tab)
104 {
105
106 /*
107 * This is a point that we should have KASSERT(cold) or add
108 * synchronization in case this can happen after cold boot.
109 * However, cn_tab initialization is so critical to any
110 * diagnostics or debugging that we need to tread carefully
111 * about introducing new ways to crash. So let's put the
112 * assertion in only after we've audited most or all of the
113 * cn_tab updates.
114 */
115 cn_tab = tab;
116 }
117
118 int
119 cnopen(dev_t dev, int flag, int mode, struct lwp *l)
120 {
121 dev_t cndev;
122 int unit, error;
123
124 unit = minor(dev);
125 if (unit > 1)
126 return ENODEV;
127
128 mutex_enter(&cn_lock);
129
130 if (cn_tab == NULL) {
131 error = 0;
132 goto out;
133 }
134
135 /*
136 * always open the 'real' console device, so we don't get nailed
137 * later. This follows normal device semantics; they always get
138 * open() calls.
139 */
140 cndev = cn_tab->cn_dev;
141 #if NNULLCONS > 0
142 if (cndev == NODEV) {
143 nullconsattach(0);
144 }
145 #else /* NNULLCONS > 0 */
146 if (cndev == NODEV) {
147 /*
148 * This is most likely an error in the console attach
149 * code. Panicking looks better than jumping into nowhere
150 * through cdevsw below....
151 */
152 panic("cnopen: no console device");
153 }
154 #endif /* NNULLCONS > 0 */
155 if (dev == cndev) {
156 /*
157 * This causes cnopen() to be called recursively, which
158 * is generally a bad thing. It is often caused when
159 * dev == 0 and cn_dev has not been set, but was probably
160 * initialised to 0.
161 */
162 panic("cnopen: cn_tab->cn_dev == dev");
163 }
164 if (cn_devvp[unit] != NULLVP) {
165 error = 0;
166 goto out;
167 }
168 if ((error = cdevvp(cndev, &cn_devvp[unit])) != 0) {
169 printf("cnopen: unable to get vnode reference\n");
170 goto out;
171 }
172 vn_lock(cn_devvp[unit], LK_EXCLUSIVE | LK_RETRY);
173 error = VOP_OPEN(cn_devvp[unit], flag, kauth_cred_get());
174 VOP_UNLOCK(cn_devvp[unit]);
175
176 out: mutex_exit(&cn_lock);
177 return error;
178 }
179
180 int
181 cnclose(dev_t dev, int flag, int mode, struct lwp *l)
182 {
183 struct vnode *vp;
184 int unit, error;
185
186 unit = minor(dev);
187 if (unit > 1)
188 return ENODEV;
189
190 mutex_enter(&cn_lock);
191
192 if (cn_tab == NULL) {
193 error = 0;
194 goto out;
195 }
196
197 vp = cn_devvp[unit];
198 cn_devvp[unit] = NULL;
199 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
200 error = VOP_CLOSE(vp, flag, kauth_cred_get());
201 VOP_UNLOCK(vp);
202 vrele(vp);
203
204 out: mutex_exit(&cn_lock);
205 return error;
206 }
207
208 int
209 cnread(dev_t dev, struct uio *uio, int flag)
210 {
211 struct tty *ctp = NULL;
212 int error;
213
214 /*
215 * If we would redirect input, punt. This will keep strange
216 * things from happening to people who are using the real
217 * console. Nothing should be using /dev/console for
218 * input (except a shell in single-user mode, but then,
219 * one wouldn't TIOCCONS then).
220 */
221 if (!cn_redirect(&dev, 1, &error, &ctp))
222 return error;
223 error = cdev_read(dev, uio, flag);
224 cn_release(ctp);
225 return error;
226 }
227
228 int
229 cnwrite(dev_t dev, struct uio *uio, int flag)
230 {
231 struct tty *ctp = NULL;
232 int error;
233
234 /* Redirect output, if that's appropriate. */
235 if (!cn_redirect(&dev, 0, &error, &ctp))
236 return error;
237 error = cdev_write(dev, uio, flag);
238 cn_release(ctp);
239 return error;
240 }
241
242 int
243 cnioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
244 {
245 struct tty *ctp = NULL;
246 int error;
247
248 error = 0;
249
250 /*
251 * Superuser can always use this to wrest control of console
252 * output from the "virtual" console.
253 */
254 if (cmd == TIOCCONS) {
255 struct tty *tp;
256
257 mutex_enter(&constty_lock);
258 tp = atomic_load_relaxed(&constty);
259 if (tp == NULL) {
260 mutex_exit(&constty_lock);
261 goto passthrough; /* XXX ??? */
262 }
263 error = kauth_authorize_device_tty(l->l_cred,
264 KAUTH_DEVICE_TTY_VIRTUAL, tp);
265 if (!error)
266 atomic_store_relaxed(&constty, NULL);
267 mutex_exit(&constty_lock);
268 return error;
269 }
270 passthrough:
271 /*
272 * Redirect the ioctl, if that's appropriate.
273 * Note that strange things can happen, if a program does
274 * ioctls on /dev/console, then the console is redirected
275 * out from under it.
276 */
277 if (!cn_redirect(&dev, 0, &error, &ctp))
278 return error;
279 error = cdev_ioctl(dev, cmd, data, flag, l);
280 cn_release(ctp);
281 return error;
282 }
283
284 /*ARGSUSED*/
285 int
286 cnpoll(dev_t dev, int events, struct lwp *l)
287 {
288 struct tty *ctp = NULL;
289 int error;
290
291 /*
292 * Redirect the poll, if that's appropriate.
293 * I don't want to think of the possible side effects
294 * of console redirection here.
295 */
296 if (!cn_redirect(&dev, 0, &error, &ctp))
297 return POLLHUP;
298 error = cdev_poll(dev, events, l);
299 cn_release(ctp);
300 return error;
301 }
302
303 /*ARGSUSED*/
304 int
305 cnkqfilter(dev_t dev, struct knote *kn)
306 {
307 struct tty *ctp = NULL;
308 int error;
309
310 /*
311 * Redirect the kqfilter, if that's appropriate.
312 * I don't want to think of the possible side effects
313 * of console redirection here.
314 */
315 if (!cn_redirect(&dev, 0, &error, &ctp))
316 return error;
317 error = cdev_kqfilter(dev, kn);
318 cn_release(ctp);
319 return error;
320 }
321
322 int
323 cngetc(void)
324 {
325 if (cn_tab == NULL)
326 return (0);
327 int s = splhigh();
328 for (;;) {
329 const int rv = (*cn_tab->cn_getc)(cn_tab->cn_dev);
330 if (rv >= 0) {
331 splx(s);
332 return rv;
333 }
334 docritpollhooks();
335 }
336 }
337
338 int
339 cngetsn(char *cp, int size)
340 {
341 char *lp;
342 int c, len;
343
344 cnpollc(1);
345
346 lp = cp;
347 len = 0;
348 for (;;) {
349 c = cngetc();
350 switch (c) {
351 case '\n':
352 case '\r':
353 printf("\n");
354 *lp++ = '\0';
355 cnpollc(0);
356 return (len);
357 case '\b':
358 case '\177':
359 case '#':
360 if (len) {
361 --len;
362 --lp;
363 printf("\b \b");
364 }
365 continue;
366 case '@':
367 case 'u'&037: /* CTRL-u */
368 len = 0;
369 lp = cp;
370 printf("\n");
371 continue;
372 default:
373 if (len + 1 >= size || c < ' ') {
374 printf("\007");
375 continue;
376 }
377 printf("%c", c);
378 ++len;
379 *lp++ = c;
380 }
381 }
382 }
383
384 void
385 cnputc(int c)
386 {
387
388 if (cn_tab == NULL)
389 return;
390
391 /*
392 * XXX
393 * for some reason this causes ARCS firmware to output an endless stream of
394 * whitespaces with n32 kernels, so use the pre-1.74 code for now until I can
395 * figure out why this happens
396 */
397 #ifndef sgimips
398 if (c) {
399 if (c == '\n') {
400 (*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
401 docritpollhooks();
402 }
403 (*cn_tab->cn_putc)(cn_tab->cn_dev, c);
404 }
405 #else
406 if (c) {
407 (*cn_tab->cn_putc)(cn_tab->cn_dev, c);
408 if (c == '\n') {
409 docritpollhooks();
410 (*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
411 }
412 }
413 #endif
414 }
415
416 void
417 cnpollc(int on)
418 {
419 static int refcount = 0;
420
421 if (cn_tab == NULL)
422 return;
423 if (!on)
424 --refcount;
425 if (refcount == 0) {
426 #ifdef HEARTBEAT
427 if (on) {
428 /*
429 * Bind to the current CPU by disabling
430 * preemption (more convenient than finding a
431 * place to store a stack to unwind for
432 * curlwp_bind/bindx, and preemption wouldn't
433 * happen anyway while spinning at high IPL in
434 * cngetc) so that curcpu() is stable so that
435 * we can suspend heartbeat checks for it.
436 */
437 kpreempt_disable();
438 heartbeat_suspend();
439 }
440 #endif
441 (*cn_tab->cn_pollc)(cn_tab->cn_dev, on);
442 #ifdef HEARTBEAT
443 if (!on) {
444 heartbeat_resume();
445 kpreempt_enable();
446 }
447 #endif
448 }
449 if (on)
450 ++refcount;
451 }
452
453 void
454 nullcnpollc(dev_t dev, int on)
455 {
456
457 }
458
459 void
460 cnbell(u_int pitch, u_int period, u_int volume)
461 {
462
463 if (cn_tab == NULL || cn_tab->cn_bell == NULL)
464 return;
465 (*cn_tab->cn_bell)(cn_tab->cn_dev, pitch, period, volume);
466 }
467
468 void
469 cnflush(void)
470 {
471 if (cn_tab == NULL || cn_tab->cn_flush == NULL)
472 return;
473 (*cn_tab->cn_flush)(cn_tab->cn_dev);
474 }
475
476 void
477 cnhalt(void)
478 {
479 if (cn_tab == NULL || cn_tab->cn_halt == NULL)
480 return;
481 (*cn_tab->cn_halt)(cn_tab->cn_dev);
482 }
483
484 /*
485 * Redirect output, if that's appropriate. If there's no real console,
486 * return ENXIO.
487 */
488 static bool
489 cn_redirect(dev_t *devp, int is_read, int *error, struct tty **ctpp)
490 {
491 dev_t dev = *devp;
492 struct tty *ctp;
493 int s;
494 bool ok = false;
495
496 *error = ENXIO;
497 *ctpp = NULL;
498 s = pserialize_read_enter();
499 if ((ctp = atomic_load_consume(&constty)) != NULL && minor(dev) == 0 &&
500 (cn_tab == NULL || (cn_tab->cn_pri != CN_REMOTE))) {
501 if (is_read) {
502 *error = 0;
503 goto out;
504 }
505 tty_acquire(ctp);
506 *ctpp = ctp;
507 dev = ctp->t_dev;
508 } else if (cn_tab == NULL)
509 goto out;
510 else
511 dev = cn_tab->cn_dev;
512 ok = true;
513 *devp = dev;
514 out: pserialize_read_exit(s);
515 return ok;
516 }
517
518 static void
519 cn_release(struct tty *ctp)
520 {
521
522 if (ctp == NULL)
523 return;
524 tty_release(ctp);
525 }
526
527 MODULE(MODULE_CLASS_DRIVER, cons, NULL);
528
529 static int
530 cons_modcmd(modcmd_t cmd, void *arg)
531 {
532
533 switch (cmd) {
534 case MODULE_CMD_INIT:
535 mutex_init(&cn_lock, MUTEX_DEFAULT, IPL_NONE);
536 return 0;
537 case MODULE_CMD_FINI:
538 mutex_destroy(&cn_lock);
539 return 0;
540 default:
541 return ENOTTY;
542 }
543 }
544