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/priv.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 153 /* 154 * Check if we should mute the console (for security reasons perhaps) 155 * It can be changes dynamically using sysctl kern.consmute 156 * once we are up and going. 157 * 158 */ 159 cn_mute = ((boothowto & (RB_MUTE 160 |RB_SINGLE 161 |RB_VERBOSE 162 |RB_ASKNAME)) == RB_MUTE); 163 164 /* 165 * Find the first console with the highest priority. 166 */ 167 best_cp = NULL; 168 SET_FOREACH(list, cons_set) { 169 cp = *list; 170 if (cp->cn_probe == NULL) 171 continue; 172 (*cp->cn_probe)(cp); 173 if (cp->cn_pri > CN_DEAD && cp->cn_probegood && 174 (best_cp == NULL || cp->cn_pri > best_cp->cn_pri)) 175 best_cp = cp; 176 } 177 178 /* 179 * If no console, give up. 180 */ 181 if (best_cp == NULL) { 182 if (cn_tab != NULL && cn_tab->cn_term != NULL) 183 (*cn_tab->cn_term)(cn_tab); 184 goto done; 185 } 186 187 /* 188 * Initialize console, then attach to it. This ordering allows 189 * debugging using the previous console, if any. 190 */ 191 (*best_cp->cn_init)(best_cp); 192 if (cn_tab != NULL && cn_tab != best_cp) { 193 /* Turn off the previous console. */ 194 if (cn_tab->cn_term != NULL) 195 (*cn_tab->cn_term)(cn_tab); 196 } 197 if (boothowto & RB_PAUSE) 198 console_pausing = 1; 199 done: 200 cn_tab = best_cp; 201 202 /* 203 * We can safely release the token after the init is done. 204 * Also assert that the mpcount is still correct or otherwise 205 * the SMP/AP boot will blow up on us. 206 */ 207 lwkt_reltoken(&tty_token); 208 } 209 210 211 /* 212 * Hook the open and close functions on the selected device. 213 */ 214 void 215 cninit_finish(void) 216 { 217 if ((cn_tab == NULL) || cn_mute) 218 return; 219 if (cn_tab->cn_dev == NULL) { 220 cn_tab->cn_init_fini(cn_tab); 221 if (cn_tab->cn_dev == NULL) { 222 kprintf("Unable to hook console! cn_tab %p\n", cn_tab); 223 return; 224 } 225 } 226 227 cn_fwd_ops = dev_ops_intercept(cn_tab->cn_dev, &cn_iops); 228 cn_dev = cn_tab->cn_dev; 229 if (cn_dev) { 230 console_rdev = makeudev(cn_dev->si_umajor, 231 (cn_dev->si_uminor == 255 ? 232 0 : cn_dev->si_uminor)); 233 } else { 234 console_rdev = 0; 235 } 236 console_pausing = 0; 237 } 238 239 static void 240 cnuninit(void) 241 { 242 if (cn_tab == NULL) 243 return; 244 if (cn_fwd_ops) 245 dev_ops_restore(cn_tab->cn_dev, cn_fwd_ops); 246 cn_fwd_ops = NULL; 247 cn_dev = NULL; 248 } 249 250 251 /* 252 * User has changed the state of the console muting. 253 * This may require us to open or close the device in question. 254 */ 255 static int 256 sysctl_kern_consmute(SYSCTL_HANDLER_ARGS) 257 { 258 int error; 259 int ocn_mute; 260 261 ocn_mute = cn_mute; 262 error = sysctl_handle_int(oidp, &cn_mute, 0, req); 263 if((error == 0) && (cn_tab != NULL) && (req->newptr != NULL)) { 264 if(ocn_mute && !cn_mute) { 265 /* 266 * going from muted to unmuted.. open the physical dev 267 * if the console has been openned 268 */ 269 cninit_finish(); 270 if (cn_is_open) { 271 /* XXX curproc is not what we want really */ 272 error = dev_dopen(cn_dev, openflag, 273 openmode, curproc->p_ucred, NULL); 274 } 275 /* if it failed, back it out */ 276 if ( error != 0) cnuninit(); 277 } else if (!ocn_mute && cn_mute) { 278 /* 279 * going from unmuted to muted.. close the physical dev 280 * if it's only open via /dev/console 281 */ 282 if (cn_is_open) { 283 error = dev_dclose(cn_dev, openflag, 284 openmode, NULL); 285 } 286 if (error == 0) 287 cnuninit(); 288 } 289 if (error != 0) { 290 /* 291 * back out the change if there was an error 292 */ 293 cn_mute = ocn_mute; 294 } 295 } 296 return (error); 297 } 298 299 SYSCTL_PROC(_kern, OID_AUTO, consmute, CTLTYPE_INT|CTLFLAG_RW, 300 0, sizeof cn_mute, sysctl_kern_consmute, "I", ""); 301 302 303 /* 304 * We intercept the OPEN and CLOSE calls on the original device, and 305 * forward the rest through. 306 */ 307 static int 308 cnintercept(struct dev_generic_args *ap) 309 { 310 int error; 311 312 if (ap->a_desc == &dev_open_desc) { 313 error = cnopen((struct dev_open_args *)ap); 314 } else if (ap->a_desc == &dev_close_desc) { 315 error = cnclose((struct dev_close_args *)ap); 316 } else if (cn_fwd_ops) { 317 error = dev_doperate_ops(cn_fwd_ops, ap); 318 } else { 319 error = ENXIO; 320 } 321 return (error); 322 } 323 324 /* 325 * cnopen() is called as an intercept function (dev will be that of the 326 * actual physical device representing our console), and also called from 327 * the muting code and from the /dev/console switch (dev will have the 328 * console's cdevsw). 329 */ 330 static int 331 cnopen(struct dev_open_args *ap) 332 { 333 cdev_t dev = ap->a_head.a_dev; 334 int flag = ap->a_oflags; 335 int mode = ap->a_devtype; 336 cdev_t cndev; 337 cdev_t physdev; 338 int retval = 0; 339 340 if (cn_tab == NULL || cn_fwd_ops == NULL) 341 return (0); 342 343 cndev = cn_tab->cn_dev; /* actual physical device */ 344 physdev = (dev == cn_devfsdev) ? cndev : dev; 345 346 /* 347 * If mute is active, then non console opens don't get here 348 * so we don't need to check for that. They bypass this and go 349 * straight to the device. 350 * 351 * It is important to note that due to our intercept and the fact 352 * that we might be called via the original (intercepted) device, 353 * the original device's ops may point to us, so to avoid an 354 * infinite recursion we have to forward through cn_fwd_ops. 355 * This is a severe hack that really needs to be fixed XXX. 356 * 357 * XXX at the moment we assume that the port forwarding function 358 * is synchronous for open. 359 */ 360 if (!cn_mute) { 361 ap->a_head.a_dev = physdev; 362 retval = dev_doperate_ops(cn_fwd_ops, &ap->a_head); 363 } 364 if (retval == 0) { 365 /* 366 * check if we openned it via /dev/console or 367 * via the physical entry (e.g. /dev/sio0). 368 */ 369 if (dev == cndev) { 370 cn_phys_is_open = 1; 371 } else if (physdev == cndev) { 372 openmode = mode; 373 openflag = flag; 374 cn_is_open = 1; 375 } 376 dev->si_tty = cndev->si_tty; 377 } 378 return (retval); 379 } 380 381 /* 382 * cnclose() is called as a port intercept function (dev will be that of the 383 * actual physical device representing our console), and also called from 384 * the muting code and from the /dev/console switch (dev will have the 385 * console's cdevsw). 386 */ 387 static int 388 cnclose(struct dev_close_args *ap) 389 { 390 struct tty *cn_tp; 391 cdev_t cndev; 392 cdev_t physdev; 393 cdev_t dev = ap->a_head.a_dev; 394 395 if (cn_tab == NULL || cn_fwd_ops == NULL) 396 return(0); 397 cndev = cn_tab->cn_dev; 398 cn_tp = cndev->si_tty; 399 physdev = (dev == cn_devfsdev) ? cndev : dev; 400 401 /* 402 * act appropriatly depending on whether it's /dev/console 403 * or the pysical device (e.g. /dev/sio) that's being closed. 404 * in either case, don't actually close the device unless 405 * both are closed. 406 */ 407 if (dev == cndev) { 408 /* the physical device is about to be closed */ 409 cn_phys_is_open = 0; 410 if (cn_is_open) { 411 if (cn_tp) { 412 /* perform a ttyhalfclose() */ 413 /* reset session and proc group */ 414 ttyclearsession(cn_tp); 415 } 416 return(0); 417 } 418 } else if (physdev == cndev) { 419 /* the logical console is about to be closed */ 420 cn_is_open = 0; 421 if (cn_phys_is_open) 422 return(0); 423 dev = cndev; 424 } 425 if (cn_fwd_ops) { 426 ap->a_head.a_dev = dev; 427 return (dev_doperate_ops(cn_fwd_ops, &ap->a_head)); 428 } 429 return (0); 430 } 431 432 /* 433 * The following functions are dispatched solely from the /dev/console 434 * device. Their job is primarily to forward the request through. 435 * If the console is not attached to anything then write()'s are sunk 436 * to null and reads return 0 (mostly). 437 */ 438 static int 439 cnread(struct dev_read_args *ap) 440 { 441 if (cn_tab == NULL || cn_fwd_ops == NULL) 442 return (0); 443 ap->a_head.a_dev = cn_tab->cn_dev; 444 return (dev_doperate(&ap->a_head)); 445 } 446 447 static int 448 cnwrite(struct dev_write_args *ap) 449 { 450 struct uio *uio = ap->a_uio; 451 cdev_t dev; 452 453 if (cn_tab == NULL || cn_fwd_ops == NULL) { 454 uio->uio_resid = 0; /* dump the data */ 455 return (0); 456 } 457 if (constty) 458 dev = constty->t_dev; 459 else 460 dev = cn_tab->cn_dev; 461 log_console(uio); 462 ap->a_head.a_dev = dev; 463 return (dev_doperate(&ap->a_head)); 464 } 465 466 static int 467 cnioctl(struct dev_ioctl_args *ap) 468 { 469 int error; 470 471 if (cn_tab == NULL || cn_fwd_ops == NULL) 472 return (0); 473 KKASSERT(curproc != NULL); 474 /* 475 * Superuser can always use this to wrest control of console 476 * output from the "virtual" console. 477 */ 478 if (ap->a_cmd == TIOCCONS && constty) { 479 if (ap->a_cred) { 480 error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0); 481 if (error) 482 return (error); 483 } 484 constty = NULL; 485 return (0); 486 } 487 ap->a_head.a_dev = cn_tab->cn_dev; 488 return (dev_doperate(&ap->a_head)); 489 } 490 491 static int 492 cnkqfilter(struct dev_kqfilter_args *ap) 493 { 494 if ((cn_tab == NULL) || cn_mute || cn_fwd_ops == NULL) 495 return (1); 496 ap->a_head.a_dev = cn_tab->cn_dev; 497 return (dev_doperate(&ap->a_head)); 498 } 499 500 /* 501 * These synchronous functions are primarily used the kernel needs to 502 * access the keyboard (e.g. when running the debugger), or output data 503 * directly to the console. 504 */ 505 int 506 cngetc(void) 507 { 508 int c; 509 if ((cn_tab == NULL) || cn_mute) 510 return (-1); 511 c = (*cn_tab->cn_getc)(cn_tab->cn_private); 512 if (c == '\r') c = '\n'; /* console input is always ICRNL */ 513 return (c); 514 } 515 516 int 517 cncheckc(void) 518 { 519 if ((cn_tab == NULL) || cn_mute) 520 return (-1); 521 return ((*cn_tab->cn_checkc)(cn_tab->cn_private)); 522 } 523 524 void 525 cnpoll(int on) 526 { 527 if ((cn_tab == NULL) || cn_mute) 528 return; 529 if (cn_tab->cn_poll) 530 ((*cn_tab->cn_poll)(cn_tab->cn_private, on)); 531 } 532 533 void 534 cnputc(int c) 535 { 536 char *cp; 537 538 if ((cn_tab == NULL) || cn_mute) 539 return; 540 if (c) { 541 if (c == '\n') 542 (*cn_tab->cn_putc)(cn_tab->cn_private, '\r'); 543 (*cn_tab->cn_putc)(cn_tab->cn_private, c); 544 #ifdef DDB 545 if (console_pausing && !db_active && (c == '\n')) { 546 #else 547 if (console_pausing && (c == '\n')) { 548 #endif 549 for(cp=console_pausestr; *cp != '\0'; cp++) 550 (*cn_tab->cn_putc)(cn_tab->cn_private, *cp); 551 if (cngetc() == '.') 552 console_pausing = 0; 553 (*cn_tab->cn_putc)(cn_tab->cn_private, '\r'); 554 for(cp=console_pausestr; *cp != '\0'; cp++) 555 (*cn_tab->cn_putc)(cn_tab->cn_private, ' '); 556 (*cn_tab->cn_putc)(cn_tab->cn_private, '\r'); 557 } 558 } 559 } 560 561 void 562 cndbctl(int on) 563 { 564 static int refcount; 565 566 if (cn_tab == NULL) 567 return; 568 if (!on) 569 refcount--; 570 if (refcount == 0 && cn_tab->cn_dbctl != NULL) 571 (*cn_tab->cn_dbctl)(cn_tab->cn_private, on); 572 if (on) 573 refcount++; 574 } 575 576 static void 577 cn_drvinit(void *unused) 578 { 579 cn_devfsdev = make_only_devfs_dev(&cn_ops, 0, UID_ROOT, GID_WHEEL, 580 0600, "console"); 581 } 582 583 SYSINIT(cndev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE + CDEV_MAJOR, cn_drvinit, NULL); 584