1 /* $NetBSD: cons.c,v 1.97 2026/11/10 03:05:00 thorpej 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.97 2026/11/10 03:05:00 thorpej Exp $"); 43 44 #include <sys/param.h> 45 46 #include <sys/atomic.h> 47 #include <sys/buf.h> 48 #include <sys/conf.h> 49 #include <sys/file.h> 50 #include <sys/heartbeat.h> 51 #include <sys/ioctl.h> 52 #include <sys/kauth.h> 53 #include <sys/module.h> 54 #include <sys/mutex.h> 55 #include <sys/poll.h> 56 #include <sys/proc.h> 57 #include <sys/pserialize.h> 58 #include <sys/systm.h> 59 #include <sys/tty.h> 60 #include <sys/vnode.h> 61 62 #include <dev/cons.h> 63 64 #include "nullcons.h" 65 66 dev_type_open(cnopen); 67 dev_type_close(cnclose); 68 dev_type_read(cnread); 69 dev_type_write(cnwrite); 70 dev_type_ioctl(cnioctl); 71 dev_type_poll(cnpoll); 72 dev_type_kqfilter(cnkqfilter); 73 74 static bool cn_redirect(dev_t *, int, int *, struct tty **); 75 static void cn_release(struct tty *); 76 77 const struct cdevsw cons_cdevsw = { 78 .d_open = cnopen, 79 .d_close = cnclose, 80 .d_read = cnread, 81 .d_write = cnwrite, 82 .d_ioctl = cnioctl, 83 .d_stop = nostop, 84 .d_tty = notty, 85 .d_poll = cnpoll, 86 .d_mmap = nommap, 87 .d_kqfilter = cnkqfilter, 88 .d_discard = nodiscard, 89 .d_flag = D_TTY|D_MPSAFE, 90 }; 91 92 static struct kmutex cn_lock; 93 94 struct tty *volatile constty; /* virtual console output device */ 95 struct vnode *cn_devvp[2]; /* vnode for underlying device. */ 96 97 struct consdev *cn_tab; /* physical console device info */ 98 99 /* 100 * Some platforms have a split upper/lower console scheme and have 101 * to check both layers in cn_isconsole(). Those platforms will 102 * provide the cn_hw symbol. 103 */ 104 struct consdev * const cn_hw_null; 105 __weak_alias(cn_hw, cn_hw_null); 106 107 /* 108 * Alternative function that's consulted after cn_tab and cn_hw 109 * to see if the specified dev_t matches the console. For when 110 * complex logic is necessary. 111 */ 112 bool (*_cn_isconsole_func)(dev_t); 113 114 void 115 cn_set_isconsole_func(bool (*func)(dev_t)) 116 { 117 _cn_isconsole_func = func; 118 } 119 120 void 121 cn_set_tab(struct consdev *tab) 122 { 123 124 /* 125 * This is a point that we should have KASSERT(cold) or add 126 * synchronization in case this can happen after cold boot. 127 * However, cn_tab initialization is so critical to any 128 * diagnostics or debugging that we need to tread carefully 129 * about introducing new ways to crash. So let's put the 130 * assertion in only after we've audited most or all of the 131 * cn_tab updates. 132 */ 133 cn_tab = tab; 134 } 135 136 int 137 cnopen(dev_t dev, int flag, int mode, struct lwp *l) 138 { 139 dev_t cndev; 140 int unit, error; 141 142 unit = minor(dev); 143 if (unit > 1) 144 return ENODEV; 145 146 mutex_enter(&cn_lock); 147 148 if (cn_tab == NULL) { 149 error = 0; 150 goto out; 151 } 152 153 /* 154 * always open the 'real' console device, so we don't get nailed 155 * later. This follows normal device semantics; they always get 156 * open() calls. 157 */ 158 cndev = cn_tab->cn_dev; 159 #if NNULLCONS > 0 160 if (cndev == NODEV) { 161 nullconsattach(0); 162 } 163 #else /* NNULLCONS > 0 */ 164 if (cndev == NODEV) { 165 /* 166 * This is most likely an error in the console attach 167 * code. Panicking looks better than jumping into nowhere 168 * through cdevsw below.... 169 */ 170 panic("cnopen: no console device"); 171 } 172 #endif /* NNULLCONS > 0 */ 173 if (dev == cndev) { 174 /* 175 * This causes cnopen() to be called recursively, which 176 * is generally a bad thing. It is often caused when 177 * dev == 0 and cn_dev has not been set, but was probably 178 * initialised to 0. 179 */ 180 panic("cnopen: cn_tab->cn_dev == dev"); 181 } 182 if (cn_devvp[unit] != NULLVP) { 183 error = 0; 184 goto out; 185 } 186 if ((error = cdevvp(cndev, &cn_devvp[unit])) != 0) { 187 printf("cnopen: unable to get vnode reference\n"); 188 goto out; 189 } 190 vn_lock(cn_devvp[unit], LK_EXCLUSIVE | LK_RETRY); 191 error = VOP_OPEN(cn_devvp[unit], flag, kauth_cred_get()); 192 VOP_UNLOCK(cn_devvp[unit]); 193 194 out: mutex_exit(&cn_lock); 195 return error; 196 } 197 198 int 199 cnclose(dev_t dev, int flag, int mode, struct lwp *l) 200 { 201 struct vnode *vp; 202 int unit, error; 203 204 unit = minor(dev); 205 if (unit > 1) 206 return ENODEV; 207 208 mutex_enter(&cn_lock); 209 210 if (cn_tab == NULL) { 211 error = 0; 212 goto out; 213 } 214 215 vp = cn_devvp[unit]; 216 cn_devvp[unit] = NULL; 217 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 218 error = VOP_CLOSE(vp, flag, kauth_cred_get()); 219 VOP_UNLOCK(vp); 220 vrele(vp); 221 222 out: mutex_exit(&cn_lock); 223 return error; 224 } 225 226 int 227 cnread(dev_t dev, struct uio *uio, int flag) 228 { 229 struct tty *ctp = NULL; 230 int error; 231 232 /* 233 * If we would redirect input, punt. This will keep strange 234 * things from happening to people who are using the real 235 * console. Nothing should be using /dev/console for 236 * input (except a shell in single-user mode, but then, 237 * one wouldn't TIOCCONS then). 238 */ 239 if (!cn_redirect(&dev, 1, &error, &ctp)) 240 return error; 241 error = cdev_read(dev, uio, flag); 242 cn_release(ctp); 243 return error; 244 } 245 246 int 247 cnwrite(dev_t dev, struct uio *uio, int flag) 248 { 249 struct tty *ctp = NULL; 250 int error; 251 252 /* Redirect output, if that's appropriate. */ 253 if (!cn_redirect(&dev, 0, &error, &ctp)) 254 return error; 255 error = cdev_write(dev, uio, flag); 256 cn_release(ctp); 257 return error; 258 } 259 260 int 261 cnioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l) 262 { 263 struct tty *ctp = NULL; 264 int error; 265 266 error = 0; 267 268 /* 269 * Superuser can always use this to wrest control of console 270 * output from the "virtual" console. 271 */ 272 if (cmd == TIOCCONS) { 273 struct tty *tp; 274 275 mutex_enter(&constty_lock); 276 tp = atomic_load_relaxed(&constty); 277 if (tp == NULL) { 278 mutex_exit(&constty_lock); 279 goto passthrough; /* XXX ??? */ 280 } 281 error = kauth_authorize_device_tty(l->l_cred, 282 KAUTH_DEVICE_TTY_VIRTUAL, tp); 283 if (!error) 284 atomic_store_relaxed(&constty, NULL); 285 mutex_exit(&constty_lock); 286 return error; 287 } 288 passthrough: 289 /* 290 * Redirect the ioctl, if that's appropriate. 291 * Note that strange things can happen, if a program does 292 * ioctls on /dev/console, then the console is redirected 293 * out from under it. 294 */ 295 if (!cn_redirect(&dev, 0, &error, &ctp)) 296 return error; 297 error = cdev_ioctl(dev, cmd, data, flag, l); 298 cn_release(ctp); 299 return error; 300 } 301 302 /*ARGSUSED*/ 303 int 304 cnpoll(dev_t dev, int events, struct lwp *l) 305 { 306 struct tty *ctp = NULL; 307 int error; 308 309 /* 310 * Redirect the poll, if that's appropriate. 311 * I don't want to think of the possible side effects 312 * of console redirection here. 313 */ 314 if (!cn_redirect(&dev, 0, &error, &ctp)) 315 return POLLHUP; 316 error = cdev_poll(dev, events, l); 317 cn_release(ctp); 318 return error; 319 } 320 321 /*ARGSUSED*/ 322 int 323 cnkqfilter(dev_t dev, struct knote *kn) 324 { 325 struct tty *ctp = NULL; 326 int error; 327 328 /* 329 * Redirect the kqfilter, if that's appropriate. 330 * I don't want to think of the possible side effects 331 * of console redirection here. 332 */ 333 if (!cn_redirect(&dev, 0, &error, &ctp)) 334 return error; 335 error = cdev_kqfilter(dev, kn); 336 cn_release(ctp); 337 return error; 338 } 339 340 int 341 cngetc(void) 342 { 343 if (cn_tab == NULL) 344 return (0); 345 int s = splhigh(); 346 for (;;) { 347 const int rv = (*cn_tab->cn_getc)(cn_tab->cn_dev); 348 if (rv >= 0) { 349 splx(s); 350 return rv; 351 } 352 docritpollhooks(); 353 } 354 } 355 356 int 357 cngetsn(char *cp, int size) 358 { 359 char *lp; 360 int c, len; 361 362 cnpollc(true); 363 364 lp = cp; 365 len = 0; 366 for (;;) { 367 c = cngetc(); 368 switch (c) { 369 case '\n': 370 case '\r': 371 printf("\n"); 372 *lp++ = '\0'; 373 cnpollc(false); 374 return (len); 375 case '\b': 376 case '\177': 377 case '#': 378 if (len) { 379 --len; 380 --lp; 381 printf("\b \b"); 382 } 383 continue; 384 case '@': 385 case 'u'&037: /* CTRL-u */ 386 len = 0; 387 lp = cp; 388 printf("\n"); 389 continue; 390 default: 391 if (len + 1 >= size || c < ' ') { 392 printf("\007"); 393 continue; 394 } 395 printf("%c", c); 396 ++len; 397 *lp++ = c; 398 } 399 } 400 } 401 402 void 403 cnputc(int c) 404 { 405 406 if (cn_tab == NULL) 407 return; 408 409 /* 410 * XXX 411 * for some reason this causes ARCS firmware to output an endless stream of 412 * whitespaces with n32 kernels, so use the pre-1.74 code for now until I can 413 * figure out why this happens 414 */ 415 #ifndef sgimips 416 if (c) { 417 if (c == '\n') { 418 (*cn_tab->cn_putc)(cn_tab->cn_dev, '\r'); 419 docritpollhooks(); 420 } 421 (*cn_tab->cn_putc)(cn_tab->cn_dev, c); 422 } 423 #else 424 if (c) { 425 (*cn_tab->cn_putc)(cn_tab->cn_dev, c); 426 if (c == '\n') { 427 docritpollhooks(); 428 (*cn_tab->cn_putc)(cn_tab->cn_dev, '\r'); 429 } 430 } 431 #endif 432 } 433 434 void 435 cnpollc(int on) 436 { 437 static int refcount = 0; 438 439 if (cn_tab == NULL) 440 return; 441 if (!on) 442 --refcount; 443 if (refcount == 0) { 444 if (on) { 445 /* 446 * Bind to the current CPU by disabling 447 * preemption (more convenient than finding a 448 * place to store a stack to unwind for 449 * curlwp_bind/bindx, and preemption wouldn't 450 * happen anyway while spinning at high IPL in 451 * cngetc) so that curcpu() is stable so that 452 * we can suspend heartbeat checks for it. 453 */ 454 kpreempt_disable(); 455 heartbeat_suspend(); 456 } 457 (*cn_tab->cn_pollc)(cn_tab->cn_dev, on); 458 if (!on) { 459 heartbeat_resume(); 460 kpreempt_enable(); 461 } 462 } 463 if (on) 464 ++refcount; 465 } 466 467 void 468 nullcnpollc(dev_t dev, int on) 469 { 470 471 } 472 473 void 474 cnbell(u_int pitch, u_int period, u_int volume) 475 { 476 477 if (cn_tab == NULL || cn_tab->cn_bell == NULL) 478 return; 479 (*cn_tab->cn_bell)(cn_tab->cn_dev, pitch, period, volume); 480 } 481 482 void 483 cnflush(void) 484 { 485 if (cn_tab == NULL || cn_tab->cn_flush == NULL) 486 return; 487 (*cn_tab->cn_flush)(cn_tab->cn_dev); 488 } 489 490 void 491 cnhalt(void) 492 { 493 if (cn_tab == NULL || cn_tab->cn_halt == NULL) 494 return; 495 (*cn_tab->cn_halt)(cn_tab->cn_dev); 496 } 497 498 /* 499 * Redirect output, if that's appropriate. If there's no real console, 500 * return ENXIO. 501 */ 502 static bool 503 cn_redirect(dev_t *devp, int is_read, int *error, struct tty **ctpp) 504 { 505 dev_t dev = *devp; 506 struct tty *ctp; 507 int s; 508 bool ok = false; 509 510 *error = ENXIO; 511 *ctpp = NULL; 512 s = pserialize_read_enter(); 513 if ((ctp = atomic_load_consume(&constty)) != NULL && minor(dev) == 0 && 514 (cn_tab == NULL || (cn_tab->cn_pri != CN_REMOTE))) { 515 if (is_read) { 516 *error = 0; 517 goto out; 518 } 519 tty_acquire(ctp); 520 *ctpp = ctp; 521 dev = ctp->t_dev; 522 } else if (cn_tab == NULL) 523 goto out; 524 else 525 dev = cn_tab->cn_dev; 526 ok = true; 527 *devp = dev; 528 out: pserialize_read_exit(s); 529 return ok; 530 } 531 532 static void 533 cn_release(struct tty *ctp) 534 { 535 536 if (ctp == NULL) 537 return; 538 tty_release(ctp); 539 } 540 541 MODULE(MODULE_CLASS_DRIVER, cons, NULL); 542 543 static int 544 cons_modcmd(modcmd_t cmd, void *arg) 545 { 546 547 switch (cmd) { 548 case MODULE_CMD_INIT: 549 mutex_init(&cn_lock, MUTEX_DEFAULT, IPL_NONE); 550 return 0; 551 case MODULE_CMD_FINI: 552 mutex_destroy(&cn_lock); 553 return 0; 554 default: 555 return ENOTTY; 556 } 557 } 558