1 /* 2 * linux/include/linux/console.h 3 * 4 * Copyright (C) 1993 Hamish Macdonald 5 * 6 * This file is subject to the terms and conditions of the GNU General Public 7 * License. See the file COPYING in the main directory of this archive 8 * for more details. 9 * 10 * Changed: 11 * 10-Mar-94: Arno Griffioen: Conversion for vt100 emulator port from PC LINUX 12 */ 13 14 #ifndef _LINUX_CONSOLE_H_ 15 #define _LINUX_CONSOLE_H_ 1 16 17 #include <linux/atomic.h> 18 #include <linux/bits.h> 19 #include <linux/rculist.h> 20 #include <linux/types.h> 21 #include <linux/vesa.h> 22 23 struct vc_data; 24 struct console_font_op; 25 struct console_font; 26 struct module; 27 struct tty_struct; 28 struct notifier_block; 29 30 enum con_scroll { 31 SM_UP, 32 SM_DOWN, 33 }; 34 35 enum vc_intensity; 36 37 /** 38 * struct consw - callbacks for consoles 39 * 40 * @owner: the module to get references of when this console is used 41 * @con_startup: set up the console and return its name (like VGA, EGA, ...) 42 * @con_init: initialize the console on @vc. @init is true for the very first 43 * call on this @vc. 44 * @con_deinit: deinitialize the console from @vc. 45 * @con_clear: erase @count characters at [@x, @y] on @vc. @count >= 1. 46 * @con_putc: emit one character with attributes @ca to [@x, @y] on @vc. 47 * (optional -- @con_putcs would be called instead) 48 * @con_putcs: emit @count characters with attributes @s to [@x, @y] on @vc. 49 * @con_cursor: enable/disable cursor depending on @enable 50 * @con_scroll: move lines from @top to @bottom in direction @dir by @lines. 51 * Return true if no generic handling should be done. 52 * Invoked by csi_M and printing to the console. 53 * @con_switch: notifier about the console switch; it is supposed to return 54 * true if a redraw is needed. 55 * @con_blank: blank/unblank the console. The target mode is passed in @blank. 56 * @mode_switch is set if changing from/to text/graphics. The hook 57 * is supposed to return true if a redraw is needed. 58 * @con_font_set: set console @vc font to @font with height @vpitch. @flags can 59 * be %KD_FONT_FLAG_DONT_RECALC. (optional) 60 * @con_font_get: fetch the current font on @vc of height @vpitch into @font. 61 * (optional) 62 * @con_font_default: set default font on @vc. @name can be %NULL or font name 63 * to search for. @font can be filled back. (optional) 64 * @con_resize: resize the @vc console to @width x @height. @from_user is true 65 * when this change comes from the user space. 66 * @con_set_palette: sets the palette of the console @vc to @table (optional) 67 * @con_scrolldelta: the contents of the console should be scrolled by @lines. 68 * Invoked by user. (optional) 69 * @con_set_origin: set origin (see &vc_data::vc_origin) of the @vc. If not 70 * provided or returns false, the origin is set to 71 * @vc->vc_screenbuf. (optional) 72 * @con_save_screen: save screen content into @vc->vc_screenbuf. Called e.g. 73 * upon entering graphics. (optional) 74 * @con_build_attr: build attributes based on @color, @intensity and other 75 * parameters. The result is used for both normal and erase 76 * characters. (optional) 77 * @con_invert_region: invert a region of length @count on @vc starting at @p. 78 * (optional) 79 * @con_debug_enter: prepare the console for the debugger. This includes, but 80 * is not limited to, unblanking the console, loading an 81 * appropriate palette, and allowing debugger generated output. 82 * (optional) 83 * @con_debug_leave: restore the console to its pre-debug state as closely as 84 * possible. (optional) 85 */ 86 struct consw { 87 struct module *owner; 88 const char *(*con_startup)(void); 89 void (*con_init)(struct vc_data *vc, bool init); 90 void (*con_deinit)(struct vc_data *vc); 91 void (*con_clear)(struct vc_data *vc, unsigned int y, 92 unsigned int x, unsigned int count); 93 void (*con_putc)(struct vc_data *vc, u16 ca, unsigned int y, 94 unsigned int x); 95 void (*con_putcs)(struct vc_data *vc, const u16 *s, 96 unsigned int count, unsigned int ypos, 97 unsigned int xpos); 98 void (*con_cursor)(struct vc_data *vc, bool enable); 99 bool (*con_scroll)(struct vc_data *vc, unsigned int top, 100 unsigned int bottom, enum con_scroll dir, 101 unsigned int lines); 102 bool (*con_switch)(struct vc_data *vc); 103 bool (*con_blank)(struct vc_data *vc, enum vesa_blank_mode blank, 104 bool mode_switch); 105 int (*con_font_set)(struct vc_data *vc, 106 const struct console_font *font, 107 unsigned int vpitch, unsigned int flags); 108 int (*con_font_get)(struct vc_data *vc, struct console_font *font, 109 unsigned int vpitch); 110 int (*con_font_default)(struct vc_data *vc, 111 struct console_font *font, const char *name); 112 int (*con_resize)(struct vc_data *vc, unsigned int width, 113 unsigned int height, bool from_user); 114 void (*con_set_palette)(struct vc_data *vc, 115 const unsigned char *table); 116 void (*con_scrolldelta)(struct vc_data *vc, int lines); 117 bool (*con_set_origin)(struct vc_data *vc); 118 void (*con_save_screen)(struct vc_data *vc); 119 u8 (*con_build_attr)(struct vc_data *vc, u8 color, 120 enum vc_intensity intensity, 121 bool blink, bool underline, bool reverse, bool italic); 122 void (*con_invert_region)(struct vc_data *vc, u16 *p, int count); 123 void (*con_debug_enter)(struct vc_data *vc); 124 void (*con_debug_leave)(struct vc_data *vc); 125 }; 126 127 extern const struct consw *conswitchp; 128 129 extern const struct consw dummy_con; /* dummy console buffer */ 130 extern const struct consw vga_con; /* VGA text console */ 131 extern const struct consw newport_con; /* SGI Newport console */ 132 133 struct screen_info; 134 #ifdef CONFIG_VGA_CONSOLE 135 void vgacon_register_screen(struct screen_info *si); 136 #else 137 static inline void vgacon_register_screen(struct screen_info *si) { } 138 #endif 139 140 int con_is_bound(const struct consw *csw); 141 int do_unregister_con_driver(const struct consw *csw); 142 int do_take_over_console(const struct consw *sw, int first, int last, int deflt); 143 void give_up_console(const struct consw *sw); 144 #ifdef CONFIG_VT 145 void con_debug_enter(struct vc_data *vc); 146 void con_debug_leave(void); 147 #else 148 static inline void con_debug_enter(struct vc_data *vc) { } 149 static inline void con_debug_leave(void) { } 150 #endif 151 152 /* 153 * The interface for a console, or any other device that wants to capture 154 * console messages (printer driver?) 155 */ 156 157 /** 158 * enum cons_flags - General console flags 159 * @CON_PRINTBUFFER: Used by newly registered consoles to avoid duplicate 160 * output of messages that were already shown by boot 161 * consoles or read by userspace via syslog() syscall. 162 * @CON_CONSDEV: Indicates that the console driver is backing 163 * /dev/console. 164 * @CON_ENABLED: Indicates if a console is allowed to print records. If 165 * false, the console also will not advance to later 166 * records. 167 * @CON_BOOT: Marks the console driver as early console driver which 168 * is used during boot before the real driver becomes 169 * available. It will be automatically unregistered 170 * when the real console driver is registered unless 171 * "keep_bootcon" parameter is used. 172 * @CON_ANYTIME: A misnomed historical flag which tells the core code 173 * that the legacy @console::write callback can be invoked 174 * on a CPU which is marked OFFLINE. That is misleading as 175 * it suggests that there is no contextual limit for 176 * invoking the callback. The original motivation was 177 * readiness of the per-CPU areas. 178 * @CON_BRL: Indicates a braille device which is exempt from 179 * receiving the printk spam for obvious reasons. 180 * @CON_EXTENDED: The console supports the extended output format of 181 * /dev/kmesg which requires a larger output buffer. 182 * @CON_SUSPENDED: Indicates if a console is suspended. If true, the 183 * printing callbacks must not be called. 184 * @CON_NBCON: Console can operate outside of the legacy style console_lock 185 * constraints. 186 */ 187 enum cons_flags { 188 CON_PRINTBUFFER = BIT(0), 189 CON_CONSDEV = BIT(1), 190 CON_ENABLED = BIT(2), 191 CON_BOOT = BIT(3), 192 CON_ANYTIME = BIT(4), 193 CON_BRL = BIT(5), 194 CON_EXTENDED = BIT(6), 195 CON_SUSPENDED = BIT(7), 196 CON_NBCON = BIT(8), 197 }; 198 199 /** 200 * struct nbcon_state - console state for nbcon consoles 201 * @atom: Compound of the state fields for atomic operations 202 * 203 * @req_prio: The priority of a handover request 204 * @prio: The priority of the current owner 205 * @unsafe: Console is busy in a non takeover region 206 * @unsafe_takeover: A hostile takeover in an unsafe state happened in the 207 * past. The console cannot be safe until re-initialized. 208 * @cpu: The CPU on which the owner runs 209 * 210 * To be used for reading and preparing of the value stored in the nbcon 211 * state variable @console::nbcon_state. 212 * 213 * The @prio and @req_prio fields are particularly important to allow 214 * spin-waiting to timeout and give up without the risk of a waiter being 215 * assigned the lock after giving up. 216 */ 217 struct nbcon_state { 218 union { 219 unsigned int atom; 220 struct { 221 unsigned int prio : 2; 222 unsigned int req_prio : 2; 223 unsigned int unsafe : 1; 224 unsigned int unsafe_takeover : 1; 225 unsigned int cpu : 24; 226 }; 227 }; 228 }; 229 230 /* 231 * The nbcon_state struct is used to easily create and interpret values that 232 * are stored in the @console::nbcon_state variable. Ensure this struct stays 233 * within the size boundaries of the atomic variable's underlying type in 234 * order to avoid any accidental truncation. 235 */ 236 static_assert(sizeof(struct nbcon_state) <= sizeof(int)); 237 238 /** 239 * enum nbcon_prio - console owner priority for nbcon consoles 240 * @NBCON_PRIO_NONE: Unused 241 * @NBCON_PRIO_NORMAL: Normal (non-emergency) usage 242 * @NBCON_PRIO_EMERGENCY: Emergency output (WARN/OOPS...) 243 * @NBCON_PRIO_PANIC: Panic output 244 * @NBCON_PRIO_MAX: The number of priority levels 245 * 246 * A higher priority context can takeover the console when it is 247 * in the safe state. The final attempt to flush consoles in panic() 248 * can be allowed to do so even in an unsafe state (Hope and pray). 249 */ 250 enum nbcon_prio { 251 NBCON_PRIO_NONE = 0, 252 NBCON_PRIO_NORMAL, 253 NBCON_PRIO_EMERGENCY, 254 NBCON_PRIO_PANIC, 255 NBCON_PRIO_MAX, 256 }; 257 258 struct console; 259 struct printk_buffers; 260 261 /** 262 * struct nbcon_context - Context for console acquire/release 263 * @console: The associated console 264 * @spinwait_max_us: Limit for spin-wait acquire 265 * @prio: Priority of the context 266 * @allow_unsafe_takeover: Allow performing takeover even if unsafe. Can 267 * be used only with NBCON_PRIO_PANIC @prio. It 268 * might cause a system freeze when the console 269 * is used later. 270 * @backlog: Ringbuffer has pending records 271 * @pbufs: Pointer to the text buffer for this context 272 * @seq: The sequence number to print for this context 273 */ 274 struct nbcon_context { 275 /* members set by caller */ 276 struct console *console; 277 unsigned int spinwait_max_us; 278 enum nbcon_prio prio; 279 unsigned int allow_unsafe_takeover : 1; 280 281 /* members set by emit */ 282 unsigned int backlog : 1; 283 284 /* members set by acquire */ 285 struct printk_buffers *pbufs; 286 u64 seq; 287 }; 288 289 /** 290 * struct nbcon_write_context - Context handed to the nbcon write callbacks 291 * @ctxt: The core console context 292 * @outbuf: Pointer to the text buffer for output 293 * @len: Length to write 294 * @unsafe_takeover: If a hostile takeover in an unsafe state has occurred 295 */ 296 struct nbcon_write_context { 297 struct nbcon_context __private ctxt; 298 char *outbuf; 299 unsigned int len; 300 bool unsafe_takeover; 301 }; 302 303 /** 304 * struct console - The console descriptor structure 305 * @name: The name of the console driver 306 * @write: Legacy write callback to output messages (Optional) 307 * @read: Read callback for console input (Optional) 308 * @device: The underlying TTY device driver (Optional) 309 * @unblank: Callback to unblank the console (Optional) 310 * @setup: Callback for initializing the console (Optional) 311 * @exit: Callback for teardown of the console (Optional) 312 * @match: Callback for matching a console (Optional) 313 * @flags: Console flags. See enum cons_flags 314 * @index: Console index, e.g. port number 315 * @cflag: TTY control mode flags 316 * @ispeed: TTY input speed 317 * @ospeed: TTY output speed 318 * @seq: Sequence number of the next ringbuffer record to print 319 * @dropped: Number of unreported dropped ringbuffer records 320 * @data: Driver private data 321 * @node: hlist node for the console list 322 * 323 * @nbcon_state: State for nbcon consoles 324 * @nbcon_seq: Sequence number of the next record for nbcon to print 325 * @nbcon_device_ctxt: Context available for non-printing operations 326 * @pbufs: Pointer to nbcon private buffer 327 */ 328 struct console { 329 char name[16]; 330 void (*write)(struct console *co, const char *s, unsigned int count); 331 int (*read)(struct console *co, char *s, unsigned int count); 332 struct tty_driver *(*device)(struct console *co, int *index); 333 void (*unblank)(void); 334 int (*setup)(struct console *co, char *options); 335 int (*exit)(struct console *co); 336 int (*match)(struct console *co, char *name, int idx, char *options); 337 short flags; 338 short index; 339 int cflag; 340 uint ispeed; 341 uint ospeed; 342 u64 seq; 343 unsigned long dropped; 344 void *data; 345 struct hlist_node node; 346 347 /* nbcon console specific members */ 348 349 /** 350 * @write_atomic: 351 * 352 * NBCON callback to write out text in any context. (Optional) 353 * 354 * This callback is called with the console already acquired. However, 355 * a higher priority context is allowed to take it over by default. 356 * 357 * The callback must call nbcon_enter_unsafe() and nbcon_exit_unsafe() 358 * around any code where the takeover is not safe, for example, when 359 * manipulating the serial port registers. 360 * 361 * nbcon_enter_unsafe() will fail if the context has lost the console 362 * ownership in the meantime. In this case, the callback is no longer 363 * allowed to go forward. It must back out immediately and carefully. 364 * The buffer content is also no longer trusted since it no longer 365 * belongs to the context. 366 * 367 * The callback should allow the takeover whenever it is safe. It 368 * increases the chance to see messages when the system is in trouble. 369 * 370 * The callback can be called from any context (including NMI). 371 * Therefore it must avoid usage of any locking and instead rely 372 * on the console ownership for synchronization. 373 */ 374 void (*write_atomic)(struct console *con, struct nbcon_write_context *wctxt); 375 376 /** 377 * @device_lock: 378 * 379 * NBCON callback to begin synchronization with driver code. 380 * 381 * Console drivers typically must deal with access to the hardware 382 * via user input/output (such as an interactive login shell) and 383 * output of kernel messages via printk() calls. This callback is 384 * called by the printk-subsystem whenever it needs to synchronize 385 * with hardware access by the driver. It should be implemented to 386 * use whatever synchronization mechanism the driver is using for 387 * itself (for example, the port lock for uart serial consoles). 388 * 389 * The callback is always called from task context. It may use any 390 * synchronization method required by the driver. 391 * 392 * IMPORTANT: The callback MUST disable migration. The console driver 393 * may be using a synchronization mechanism that already takes 394 * care of this (such as spinlocks). Otherwise this function must 395 * explicitly call migrate_disable(). 396 * 397 * The flags argument is provided as a convenience to the driver. It 398 * will be passed again to device_unlock(). It can be ignored if the 399 * driver does not need it. 400 */ 401 void (*device_lock)(struct console *con, unsigned long *flags); 402 403 /** 404 * @device_unlock: 405 * 406 * NBCON callback to finish synchronization with driver code. 407 * 408 * It is the counterpart to device_lock(). 409 * 410 * This callback is always called from task context. It must 411 * appropriately re-enable migration (depending on how device_lock() 412 * disabled migration). 413 * 414 * The flags argument is the value of the same variable that was 415 * passed to device_lock(). 416 */ 417 void (*device_unlock)(struct console *con, unsigned long flags); 418 419 atomic_t __private nbcon_state; 420 atomic_long_t __private nbcon_seq; 421 struct nbcon_context __private nbcon_device_ctxt; 422 struct printk_buffers *pbufs; 423 }; 424 425 #ifdef CONFIG_LOCKDEP 426 extern void lockdep_assert_console_list_lock_held(void); 427 #else 428 static inline void lockdep_assert_console_list_lock_held(void) 429 { 430 } 431 #endif 432 433 #ifdef CONFIG_DEBUG_LOCK_ALLOC 434 extern bool console_srcu_read_lock_is_held(void); 435 #else 436 static inline bool console_srcu_read_lock_is_held(void) 437 { 438 return 1; 439 } 440 #endif 441 442 extern int console_srcu_read_lock(void); 443 extern void console_srcu_read_unlock(int cookie); 444 445 extern void console_list_lock(void) __acquires(console_mutex); 446 extern void console_list_unlock(void) __releases(console_mutex); 447 448 extern struct hlist_head console_list; 449 450 /** 451 * console_srcu_read_flags - Locklessly read flags of a possibly registered 452 * console 453 * @con: struct console pointer of console to read flags from 454 * 455 * Locklessly reading @con->flags provides a consistent read value because 456 * there is at most one CPU modifying @con->flags and that CPU is using only 457 * read-modify-write operations to do so. 458 * 459 * Requires console_srcu_read_lock to be held, which implies that @con might 460 * be a registered console. The purpose of holding console_srcu_read_lock is 461 * to guarantee that the console state is valid (CON_SUSPENDED/CON_ENABLED) 462 * and that no exit/cleanup routines will run if the console is currently 463 * undergoing unregistration. 464 * 465 * If the caller is holding the console_list_lock or it is _certain_ that 466 * @con is not and will not become registered, the caller may read 467 * @con->flags directly instead. 468 * 469 * Context: Any context. 470 * Return: The current value of the @con->flags field. 471 */ 472 static inline short console_srcu_read_flags(const struct console *con) 473 { 474 WARN_ON_ONCE(!console_srcu_read_lock_is_held()); 475 476 /* 477 * The READ_ONCE() matches the WRITE_ONCE() when @flags are modified 478 * for registered consoles with console_srcu_write_flags(). 479 */ 480 return data_race(READ_ONCE(con->flags)); 481 } 482 483 /** 484 * console_srcu_write_flags - Write flags for a registered console 485 * @con: struct console pointer of console to write flags to 486 * @flags: new flags value to write 487 * 488 * Only use this function to write flags for registered consoles. It 489 * requires holding the console_list_lock. 490 * 491 * Context: Any context. 492 */ 493 static inline void console_srcu_write_flags(struct console *con, short flags) 494 { 495 lockdep_assert_console_list_lock_held(); 496 497 /* This matches the READ_ONCE() in console_srcu_read_flags(). */ 498 WRITE_ONCE(con->flags, flags); 499 } 500 501 /* Variant of console_is_registered() when the console_list_lock is held. */ 502 static inline bool console_is_registered_locked(const struct console *con) 503 { 504 lockdep_assert_console_list_lock_held(); 505 return !hlist_unhashed(&con->node); 506 } 507 508 /* 509 * console_is_registered - Check if the console is registered 510 * @con: struct console pointer of console to check 511 * 512 * Context: Process context. May sleep while acquiring console list lock. 513 * Return: true if the console is in the console list, otherwise false. 514 * 515 * If false is returned for a console that was previously registered, it 516 * can be assumed that the console's unregistration is fully completed, 517 * including the exit() callback after console list removal. 518 */ 519 static inline bool console_is_registered(const struct console *con) 520 { 521 bool ret; 522 523 console_list_lock(); 524 ret = console_is_registered_locked(con); 525 console_list_unlock(); 526 return ret; 527 } 528 529 /** 530 * for_each_console_srcu() - Iterator over registered consoles 531 * @con: struct console pointer used as loop cursor 532 * 533 * Although SRCU guarantees the console list will be consistent, the 534 * struct console fields may be updated by other CPUs while iterating. 535 * 536 * Requires console_srcu_read_lock to be held. Can be invoked from 537 * any context. 538 */ 539 #define for_each_console_srcu(con) \ 540 hlist_for_each_entry_srcu(con, &console_list, node, \ 541 console_srcu_read_lock_is_held()) 542 543 /** 544 * for_each_console() - Iterator over registered consoles 545 * @con: struct console pointer used as loop cursor 546 * 547 * The console list and the &console.flags are immutable while iterating. 548 * 549 * Requires console_list_lock to be held. 550 */ 551 #define for_each_console(con) \ 552 lockdep_assert_console_list_lock_held(); \ 553 hlist_for_each_entry(con, &console_list, node) 554 555 #ifdef CONFIG_PRINTK 556 extern void nbcon_cpu_emergency_enter(void); 557 extern void nbcon_cpu_emergency_exit(void); 558 extern bool nbcon_can_proceed(struct nbcon_write_context *wctxt); 559 extern bool nbcon_enter_unsafe(struct nbcon_write_context *wctxt); 560 extern bool nbcon_exit_unsafe(struct nbcon_write_context *wctxt); 561 #else 562 static inline void nbcon_cpu_emergency_enter(void) { } 563 static inline void nbcon_cpu_emergency_exit(void) { } 564 static inline bool nbcon_can_proceed(struct nbcon_write_context *wctxt) { return false; } 565 static inline bool nbcon_enter_unsafe(struct nbcon_write_context *wctxt) { return false; } 566 static inline bool nbcon_exit_unsafe(struct nbcon_write_context *wctxt) { return false; } 567 #endif 568 569 extern int console_set_on_cmdline; 570 extern struct console *early_console; 571 572 enum con_flush_mode { 573 CONSOLE_FLUSH_PENDING, 574 CONSOLE_REPLAY_ALL, 575 }; 576 577 extern int add_preferred_console(const char *name, const short idx, char *options); 578 extern void console_force_preferred_locked(struct console *con); 579 extern void register_console(struct console *); 580 extern int unregister_console(struct console *); 581 extern void console_lock(void); 582 extern int console_trylock(void); 583 extern void console_unlock(void); 584 extern void console_conditional_schedule(void); 585 extern void console_unblank(void); 586 extern void console_flush_on_panic(enum con_flush_mode mode); 587 extern struct tty_driver *console_device(int *); 588 extern void console_stop(struct console *); 589 extern void console_start(struct console *); 590 extern int is_console_locked(void); 591 extern int braille_register_console(struct console *, int index, 592 char *console_options, char *braille_options); 593 extern int braille_unregister_console(struct console *); 594 #ifdef CONFIG_TTY 595 extern void console_sysfs_notify(void); 596 #else 597 static inline void console_sysfs_notify(void) 598 { } 599 #endif 600 extern bool console_suspend_enabled; 601 602 /* Suspend and resume console messages over PM events */ 603 extern void suspend_console(void); 604 extern void resume_console(void); 605 606 int mda_console_init(void); 607 608 void vcs_make_sysfs(int index); 609 void vcs_remove_sysfs(int index); 610 611 /* Some debug stub to catch some of the obvious races in the VT code */ 612 #define WARN_CONSOLE_UNLOCKED() \ 613 WARN_ON(!atomic_read(&ignore_console_lock_warning) && \ 614 !is_console_locked() && !oops_in_progress) 615 /* 616 * Increment ignore_console_lock_warning if you need to quiet 617 * WARN_CONSOLE_UNLOCKED() for debugging purposes. 618 */ 619 extern atomic_t ignore_console_lock_warning; 620 621 extern void console_init(void); 622 623 /* For deferred console takeover */ 624 void dummycon_register_output_notifier(struct notifier_block *nb); 625 void dummycon_unregister_output_notifier(struct notifier_block *nb); 626 627 #endif /* _LINUX_CONSOLE_H */ 628