1 /* 2 * Copyright (c) 1988 University of Utah. 3 * Copyright (c) 1991 The Regents of the University of California. 4 * All rights reserved. 5 * 6 * This code is derived from software contributed to Berkeley by 7 * the Systems Programming Group of the University of Utah Computer 8 * Science Department. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * from: @(#)cons.c 7.2 (Berkeley) 5/9/91 35 * $FreeBSD: src/sys/kern/tty_cons.c,v 1.81.2.4 2001/12/17 18:44:41 guido Exp $ 36 */ 37 38 #include "opt_ddb.h" 39 #include "opt_comconsole.h" 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/conf.h> 44 #include <sys/cons.h> 45 #include <sys/kernel.h> 46 #include <sys/proc.h> 47 #include <sys/caps.h> 48 #include <sys/reboot.h> 49 #include <sys/sysctl.h> 50 #include <sys/tty.h> 51 #include <sys/uio.h> 52 #include <sys/msgport.h> 53 #include <sys/msgport2.h> 54 #include <sys/device.h> 55 56 #include <ddb/ddb.h> 57 58 #include <machine/cpu.h> 59 60 static d_open_t cnopen; 61 static d_close_t cnclose; 62 static d_read_t cnread; 63 static d_write_t cnwrite; 64 static d_ioctl_t cnioctl; 65 static d_kqfilter_t cnkqfilter; 66 67 static int cnintercept(struct dev_generic_args *ap); 68 69 #define CDEV_MAJOR 0 70 static struct dev_ops cn_ops = { 71 { "console", 0, D_TTY | D_MPSAFE }, 72 .d_open = cnopen, 73 .d_close = cnclose, 74 .d_read = cnread, 75 .d_write = cnwrite, 76 .d_ioctl = cnioctl, 77 .d_kqfilter = cnkqfilter, 78 }; 79 80 static struct dev_ops cn_iops = { 81 { "intercept", 0, D_TTY | D_MPSAFE }, 82 .d_default = cnintercept 83 }; 84 85 static struct dev_ops *cn_fwd_ops; 86 static cdev_t cn_dev; 87 88 //XXX: get this shit out! (alexh) 89 #if 0 90 SYSCTL_OPAQUE(_machdep, CPU_CONSDEV, consdev, CTLFLAG_RD, 91 &cn_udev, sizeof cn_udev, "T,udev_t", ""); 92 #endif 93 94 static int cn_mute; 95 96 #ifdef BREAK_TO_DEBUGGER 97 int break_to_debugger = 1; /* CTL-ALT-ESC on system keyboard */ 98 #else 99 int break_to_debugger = 0; 100 #endif 101 TUNABLE_INT("kern.break_to_debugger", &break_to_debugger); 102 SYSCTL_INT(_kern, OID_AUTO, break_to_debugger, CTLFLAG_RW, 103 &break_to_debugger, 0, ""); 104 105 #ifdef ALT_BREAK_TO_DEBUGGER 106 int alt_break_to_debugger = 1; /* CR ~ ^B on serial port */ 107 #else 108 int alt_break_to_debugger = 0; 109 #endif 110 TUNABLE_INT("kern.alt_break_to_debugger", &alt_break_to_debugger); 111 SYSCTL_INT(_kern, OID_AUTO, alt_break_to_debugger, CTLFLAG_RW, 112 &alt_break_to_debugger, 0, ""); 113 114 int cons_unavail = 0; /* XXX: 115 * physical console not available for 116 * input (i.e., it is in graphics mode) 117 */ 118 int sysbeep_enable = 1; 119 120 static u_char cn_is_open; /* nonzero if logical console is open */ 121 static int openmode, openflag; /* how /dev/console was openned */ 122 static cdev_t cn_devfsdev; /* represents the device private info */ 123 static u_char cn_phys_is_open; /* nonzero if physical device is open */ 124 static u_char console_pausing; /* pause after each line during probe */ 125 static char *console_pausestr= 126 "<pause; press any key to proceed to next line or '.' to end pause mode>"; 127 128 struct consdev *cn_tab; /* physical console device info */ 129 struct consdev *gdb_tab; /* physical gdb debugger device info */ 130 131 SYSCTL_INT(_kern, OID_AUTO, sysbeep_enable, CTLFLAG_RW, &sysbeep_enable, 0, ""); 132 static uint32_t console_rdev; 133 SYSCTL_INT(_kern, OID_AUTO, console_rdev, CTLFLAG_RW, 134 &console_rdev, 0, ""); 135 136 CONS_DRIVER(cons, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); 137 SET_DECLARE(cons_set, struct consdev); 138 139 void 140 cninit(void) 141 { 142 struct consdev *best_cp, *cp, **list; 143 144 /* 145 * Workaround for token acquisition and releases during the 146 * console init. For some reason if lwkt_gettoken()'s mpcount 147 * optimization is turned off the console init blows up. It 148 * might be trying to kprintf() something in the middle of 149 * its init. 150 */ 151 lwkt_gettoken(&tty_token); 152 lwkt_gettoken(&vga_token); 153 154 /* 155 * Check if we should mute the console (for security reasons perhaps) 156 * It can be changes dynamically using sysctl kern.consmute 157 * once we are up and going. 158 * 159 */ 160 cn_mute = ((boothowto & (RB_MUTE 161 |RB_SINGLE 162 |RB_VERBOSE 163 |RB_ASKNAME)) == RB_MUTE); 164 165 /* 166 * Find the first console with the highest priority. 167 */ 168 best_cp = NULL; 169 SET_FOREACH(list, cons_set) { 170 cp = *list; 171 if (cp->cn_probe == NULL) 172 continue; 173 (*cp->cn_probe)(cp); 174 if (cp->cn_pri > CN_DEAD && cp->cn_probegood && 175 (best_cp == NULL || cp->cn_pri > best_cp->cn_pri)) 176 best_cp = cp; 177 } 178 179 /* 180 * If no console, give up. 181 */ 182 if (best_cp == NULL) { 183 if (cn_tab != NULL && cn_tab->cn_term != NULL) 184 (*cn_tab->cn_term)(cn_tab); 185 goto done; 186 } 187 188 /* 189 * Initialize console, then attach to it. This ordering allows 190 * debugging using the previous console, if any. 191 */ 192 (*best_cp->cn_init)(best_cp); 193 if (cn_tab != NULL && cn_tab != best_cp) { 194 /* Turn off the previous console. */ 195 if (cn_tab->cn_term != NULL) 196 (*cn_tab->cn_term)(cn_tab); 197 } 198 if (boothowto & RB_PAUSE) 199 console_pausing = 1; 200 done: 201 cn_tab = best_cp; 202 203 /* 204 * We can safely release the token after the init is done. 205 * Also assert that the mpcount is still correct or otherwise 206 * the SMP/AP boot will blow up on us. 207 */ 208 lwkt_reltoken(&vga_token); 209 lwkt_reltoken(&tty_token); 210 } 211 212 213 /* 214 * Hook the open and close functions on the selected device. 215 */ 216 void 217 cninit_finish(void) 218 { 219 if ((cn_tab == NULL) || cn_mute) 220 return; 221 if (cn_tab->cn_dev == NULL) { 222 cn_tab->cn_init_fini(cn_tab); 223 if (cn_tab->cn_dev == NULL) { 224 kprintf("Unable to hook console! cn_tab %p\n", cn_tab); 225 return; 226 } 227 } 228 229 cn_fwd_ops = dev_ops_intercept(cn_tab->cn_dev, &cn_iops); 230 cn_dev = cn_tab->cn_dev; 231 if (cn_dev) { 232 console_rdev = makeudev(cn_dev->si_umajor, 233 (cn_dev->si_uminor == 255 ? 234 0 : cn_dev->si_uminor)); 235 } else { 236 console_rdev = 0; 237 } 238 console_pausing = 0; 239 } 240 241 static void 242 cnuninit(void) 243 { 244 if (cn_tab == NULL) 245 return; 246 if (cn_fwd_ops) 247 dev_ops_restore(cn_tab->cn_dev, cn_fwd_ops); 248 cn_fwd_ops = NULL; 249 cn_dev = NULL; 250 } 251 252 253 /* 254 * User has changed the state of the console muting. 255 * This may require us to open or close the device in question. 256 */ 257 static int 258 sysctl_kern_consmute(SYSCTL_HANDLER_ARGS) 259 { 260 int error; 261 int ocn_mute; 262 263 ocn_mute = cn_mute; 264 error = sysctl_handle_int(oidp, &cn_mute, 0, req); 265 if((error == 0) && (cn_tab != NULL) && (req->newptr != NULL)) { 266 if(ocn_mute && !cn_mute) { 267 /* 268 * going from muted to unmuted.. open the physical dev 269 * if the console has been openned 270 */ 271 cninit_finish(); 272 if (cn_is_open) { 273 /* XXX curproc is not what we want really */ 274 error = dev_dopen(cn_dev, openflag, openmode, 275 curproc->p_ucred, NULL, NULL); 276 } 277 /* if it failed, back it out */ 278 if ( error != 0) cnuninit(); 279 } else if (!ocn_mute && cn_mute) { 280 /* 281 * going from unmuted to muted.. close the physical dev 282 * if it's only open via /dev/console 283 */ 284 if (cn_is_open) { 285 error = dev_dclose(cn_dev, openflag, 286 openmode, NULL); 287 } 288 if (error == 0) 289 cnuninit(); 290 } 291 if (error != 0) { 292 /* 293 * back out the change if there was an error 294 */ 295 cn_mute = ocn_mute; 296 } 297 } 298 return (error); 299 } 300 301 SYSCTL_PROC(_kern, OID_AUTO, consmute, CTLTYPE_INT|CTLFLAG_RW, 302 0, sizeof cn_mute, sysctl_kern_consmute, "I", ""); 303 304 305 /* 306 * We intercept the OPEN and CLOSE calls on the original device, and 307 * forward the rest through. 308 */ 309 static int 310 cnintercept(struct dev_generic_args *ap) 311 { 312 int error; 313 314 if (ap->a_desc == &dev_open_desc) { 315 error = cnopen((struct dev_open_args *)ap); 316 } else if (ap->a_desc == &dev_close_desc) { 317 error = cnclose((struct dev_close_args *)ap); 318 } else if (cn_fwd_ops) { 319 error = dev_doperate_ops(cn_fwd_ops, ap); 320 } else { 321 error = ENXIO; 322 } 323 return (error); 324 } 325 326 /* 327 * cnopen() is called as an intercept function (dev will be that of the 328 * actual physical device representing our console), and also called from 329 * the muting code and from the /dev/console switch (dev will have the 330 * console's cdevsw). 331 */ 332 static int 333 cnopen(struct dev_open_args *ap) 334 { 335 cdev_t dev = ap->a_head.a_dev; 336 int flag = ap->a_oflags; 337 int mode = ap->a_devtype; 338 cdev_t cndev; 339 cdev_t physdev; 340 int retval = 0; 341 342 if (cn_tab == NULL || cn_fwd_ops == NULL) 343 return (0); 344 345 cndev = cn_tab->cn_dev; /* actual physical device */ 346 physdev = (dev == cn_devfsdev) ? cndev : dev; 347 348 /* 349 * If mute is active, then non console opens don't get here 350 * so we don't need to check for that. They bypass this and go 351 * straight to the device. 352 * 353 * It is important to note that due to our intercept and the fact 354 * that we might be called via the original (intercepted) device, 355 * the original device's ops may point to us, so to avoid an 356 * infinite recursion we have to forward through cn_fwd_ops. 357 * This is a severe hack that really needs to be fixed XXX. 358 * 359 * XXX at the moment we assume that the port forwarding function 360 * is synchronous for open. 361 */ 362 if (!cn_mute) { 363 ap->a_head.a_dev = physdev; 364 retval = dev_doperate_ops(cn_fwd_ops, &ap->a_head); 365 } 366 if (retval == 0) { 367 /* 368 * check if we openned it via /dev/console or 369 * via the physical entry (e.g. /dev/sio0). 370 */ 371 if (dev == cndev) { 372 cn_phys_is_open = 1; 373 } else if (physdev == cndev) { 374 openmode = mode; 375 openflag = flag; 376 cn_is_open = 1; 377 } 378 dev->si_tty = cndev->si_tty; 379 } 380 return (retval); 381 } 382 383 /* 384 * cnclose() is called as a port intercept function (dev will be that of the 385 * actual physical device representing our console), and also called from 386 * the muting code and from the /dev/console switch (dev will have the 387 * console's cdevsw). 388 */ 389 static int 390 cnclose(struct dev_close_args *ap) 391 { 392 struct tty *cn_tp; 393 cdev_t cndev; 394 cdev_t physdev; 395 cdev_t dev = ap->a_head.a_dev; 396 397 if (cn_tab == NULL || cn_fwd_ops == NULL) 398 return(0); 399 cndev = cn_tab->cn_dev; 400 cn_tp = cndev->si_tty; 401 physdev = (dev == cn_devfsdev) ? cndev : dev; 402 403 /* 404 * act appropriatly depending on whether it's /dev/console 405 * or the pysical device (e.g. /dev/sio) that's being closed. 406 * in either case, don't actually close the device unless 407 * both are closed. 408 */ 409 if (dev == cndev) { 410 /* the physical device is about to be closed */ 411 cn_phys_is_open = 0; 412 if (cn_is_open) { 413 if (cn_tp) { 414 /* perform a ttyhalfclose() */ 415 /* reset session and proc group */ 416 ttyclearsession(cn_tp); 417 } 418 return(0); 419 } 420 } else if (physdev == cndev) { 421 /* the logical console is about to be closed */ 422 cn_is_open = 0; 423 if (cn_phys_is_open) 424 return(0); 425 dev = cndev; 426 } 427 if (cn_fwd_ops) { 428 ap->a_head.a_dev = dev; 429 return (dev_doperate_ops(cn_fwd_ops, &ap->a_head)); 430 } 431 return (0); 432 } 433 434 /* 435 * The following functions are dispatched solely from the /dev/console 436 * device. Their job is primarily to forward the request through. 437 * If the console is not attached to anything then write()'s are sunk 438 * to null and reads return 0 (mostly). 439 */ 440 static int 441 cnread(struct dev_read_args *ap) 442 { 443 if (cn_tab == NULL || cn_fwd_ops == NULL) 444 return (0); 445 ap->a_head.a_dev = cn_tab->cn_dev; 446 return (dev_doperate(&ap->a_head)); 447 } 448 449 static int 450 cnwrite(struct dev_write_args *ap) 451 { 452 struct uio *uio = ap->a_uio; 453 cdev_t dev; 454 455 if (cn_tab == NULL || cn_fwd_ops == NULL) { 456 uio->uio_resid = 0; /* dump the data */ 457 return (0); 458 } 459 if (constty) 460 dev = constty->t_dev; 461 else 462 dev = cn_tab->cn_dev; 463 log_console(uio); 464 ap->a_head.a_dev = dev; 465 return (dev_doperate(&ap->a_head)); 466 } 467 468 static int 469 cnioctl(struct dev_ioctl_args *ap) 470 { 471 int error; 472 473 if (cn_tab == NULL || cn_fwd_ops == NULL) 474 return (0); 475 KKASSERT(curproc != NULL); 476 /* 477 * Superuser can always use this to wrest control of console 478 * output from the "virtual" console. 479 */ 480 if (ap->a_cmd == TIOCCONS && constty) { 481 if (ap->a_cred) { 482 error = caps_priv_check(ap->a_cred, 483 SYSCAP_RESTRICTEDROOT); 484 if (error) 485 return (error); 486 } 487 constty = NULL; 488 return (0); 489 } 490 ap->a_head.a_dev = cn_tab->cn_dev; 491 return (dev_doperate(&ap->a_head)); 492 } 493 494 static int 495 cnkqfilter(struct dev_kqfilter_args *ap) 496 { 497 if ((cn_tab == NULL) || cn_mute || cn_fwd_ops == NULL) 498 return (1); 499 ap->a_head.a_dev = cn_tab->cn_dev; 500 return (dev_doperate(&ap->a_head)); 501 } 502 503 /* 504 * These synchronous functions are primarily used the kernel needs to 505 * access the keyboard (e.g. when running the debugger), or output data 506 * directly to the console. 507 */ 508 int 509 cngetc(void) 510 { 511 int c; 512 if ((cn_tab == NULL) || cn_mute) 513 return (-1); 514 c = (*cn_tab->cn_getc)(cn_tab->cn_private); 515 if (c == '\r') c = '\n'; /* console input is always ICRNL */ 516 return (c); 517 } 518 519 int 520 cncheckc(void) 521 { 522 if ((cn_tab == NULL) || cn_mute) 523 return (-1); 524 return ((*cn_tab->cn_checkc)(cn_tab->cn_private)); 525 } 526 527 void 528 cnpoll(int on) 529 { 530 if ((cn_tab == NULL) || cn_mute) 531 return; 532 if (cn_tab->cn_poll) 533 ((*cn_tab->cn_poll)(cn_tab->cn_private, on)); 534 } 535 536 void 537 cnputc(int c) 538 { 539 char *cp; 540 541 if ((cn_tab == NULL) || cn_mute) 542 return; 543 if (c) { 544 if (c == '\n') 545 (*cn_tab->cn_putc)(cn_tab->cn_private, '\r'); 546 (*cn_tab->cn_putc)(cn_tab->cn_private, c); 547 #ifdef DDB 548 if (console_pausing && !db_active && (c == '\n')) { 549 #else 550 if (console_pausing && (c == '\n')) { 551 #endif 552 for(cp=console_pausestr; *cp != '\0'; cp++) 553 (*cn_tab->cn_putc)(cn_tab->cn_private, *cp); 554 if (cngetc() == '.') 555 console_pausing = 0; 556 (*cn_tab->cn_putc)(cn_tab->cn_private, '\r'); 557 for(cp=console_pausestr; *cp != '\0'; cp++) 558 (*cn_tab->cn_putc)(cn_tab->cn_private, ' '); 559 (*cn_tab->cn_putc)(cn_tab->cn_private, '\r'); 560 } 561 } 562 } 563 564 void 565 cndbctl(int on) 566 { 567 static int refcount; 568 569 if (cn_tab == NULL) 570 return; 571 if (!on) 572 refcount--; 573 if (refcount == 0 && cn_tab->cn_dbctl != NULL) 574 (*cn_tab->cn_dbctl)(cn_tab->cn_private, on); 575 if (on) 576 refcount++; 577 } 578 579 static void 580 cn_drvinit(void *unused) 581 { 582 cn_devfsdev = make_only_devfs_dev(&cn_ops, 0, UID_ROOT, GID_WHEEL, 583 0600, "console"); 584 } 585 586 SYSINIT(cndev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE + CDEV_MAJOR, cn_drvinit, NULL); 587