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 * @con_init: initialize the console on @vc. @init is true for the very first 41 * call on this @vc. 42 * @con_clear: erase @count characters at [@x, @y] on @vc. @count >= 1. 43 * @con_putc: emit one character with attributes @ca to [@x, @y] on @vc. 44 * (optional -- @con_putcs would be called instead) 45 * @con_putcs: emit @count characters with attributes @s to [@x, @y] on @vc. 46 * @con_cursor: enable/disable cursor depending on @enable 47 * @con_scroll: move lines from @top to @bottom in direction @dir by @lines. 48 * Return true if no generic handling should be done. 49 * Invoked by csi_M and printing to the console. 50 * @con_switch: notifier about the console switch; it is supposed to return 51 * true if a redraw is needed. 52 * @con_blank: blank/unblank the console. The target mode is passed in @blank. 53 * @mode_switch is set if changing from/to text/graphics. The hook 54 * is supposed to return true if a redraw is needed. 55 * @con_set_palette: sets the palette of the console to @table (optional) 56 * @con_scrolldelta: the contents of the console should be scrolled by @lines. 57 * Invoked by user. (optional) 58 */ 59 struct consw { 60 struct module *owner; 61 const char *(*con_startup)(void); 62 void (*con_init)(struct vc_data *vc, bool init); 63 void (*con_deinit)(struct vc_data *vc); 64 void (*con_clear)(struct vc_data *vc, unsigned int y, 65 unsigned int x, unsigned int count); 66 void (*con_putc)(struct vc_data *vc, u16 ca, unsigned int y, 67 unsigned int x); 68 void (*con_putcs)(struct vc_data *vc, const u16 *s, 69 unsigned int count, unsigned int ypos, 70 unsigned int xpos); 71 void (*con_cursor)(struct vc_data *vc, bool enable); 72 bool (*con_scroll)(struct vc_data *vc, unsigned int top, 73 unsigned int bottom, enum con_scroll dir, 74 unsigned int lines); 75 bool (*con_switch)(struct vc_data *vc); 76 bool (*con_blank)(struct vc_data *vc, enum vesa_blank_mode blank, 77 bool mode_switch); 78 int (*con_font_set)(struct vc_data *vc, struct console_font *font, 79 unsigned int vpitch, unsigned int flags); 80 int (*con_font_get)(struct vc_data *vc, struct console_font *font, 81 unsigned int vpitch); 82 int (*con_font_default)(struct vc_data *vc, 83 struct console_font *font, char *name); 84 int (*con_resize)(struct vc_data *vc, unsigned int width, 85 unsigned int height, bool from_user); 86 void (*con_set_palette)(struct vc_data *vc, 87 const unsigned char *table); 88 void (*con_scrolldelta)(struct vc_data *vc, int lines); 89 int (*con_set_origin)(struct vc_data *vc); 90 void (*con_save_screen)(struct vc_data *vc); 91 u8 (*con_build_attr)(struct vc_data *vc, u8 color, 92 enum vc_intensity intensity, 93 bool blink, bool underline, bool reverse, bool italic); 94 void (*con_invert_region)(struct vc_data *vc, u16 *p, int count); 95 u16 *(*con_screen_pos)(const struct vc_data *vc, int offset); 96 unsigned long (*con_getxy)(struct vc_data *vc, unsigned long position, 97 int *px, int *py); 98 /* 99 * Flush the video console driver's scrollback buffer 100 */ 101 void (*con_flush_scrollback)(struct vc_data *vc); 102 /* 103 * Prepare the console for the debugger. This includes, but is not 104 * limited to, unblanking the console, loading an appropriate 105 * palette, and allowing debugger generated output. 106 */ 107 void (*con_debug_enter)(struct vc_data *vc); 108 /* 109 * Restore the console to its pre-debug state as closely as possible. 110 */ 111 void (*con_debug_leave)(struct vc_data *vc); 112 }; 113 114 extern const struct consw *conswitchp; 115 116 extern const struct consw dummy_con; /* dummy console buffer */ 117 extern const struct consw vga_con; /* VGA text console */ 118 extern const struct consw newport_con; /* SGI Newport console */ 119 120 struct screen_info; 121 #ifdef CONFIG_VGA_CONSOLE 122 void vgacon_register_screen(struct screen_info *si); 123 #else 124 static inline void vgacon_register_screen(struct screen_info *si) { } 125 #endif 126 127 int con_is_bound(const struct consw *csw); 128 int do_unregister_con_driver(const struct consw *csw); 129 int do_take_over_console(const struct consw *sw, int first, int last, int deflt); 130 void give_up_console(const struct consw *sw); 131 #ifdef CONFIG_HW_CONSOLE 132 void con_debug_enter(struct vc_data *vc); 133 void con_debug_leave(void); 134 #else 135 static inline void con_debug_enter(struct vc_data *vc) { } 136 static inline void con_debug_leave(void) { } 137 #endif 138 139 /* 140 * The interface for a console, or any other device that wants to capture 141 * console messages (printer driver?) 142 */ 143 144 /** 145 * cons_flags - General console flags 146 * @CON_PRINTBUFFER: Used by newly registered consoles to avoid duplicate 147 * output of messages that were already shown by boot 148 * consoles or read by userspace via syslog() syscall. 149 * @CON_CONSDEV: Indicates that the console driver is backing 150 * /dev/console. 151 * @CON_ENABLED: Indicates if a console is allowed to print records. If 152 * false, the console also will not advance to later 153 * records. 154 * @CON_BOOT: Marks the console driver as early console driver which 155 * is used during boot before the real driver becomes 156 * available. It will be automatically unregistered 157 * when the real console driver is registered unless 158 * "keep_bootcon" parameter is used. 159 * @CON_ANYTIME: A misnomed historical flag which tells the core code 160 * that the legacy @console::write callback can be invoked 161 * on a CPU which is marked OFFLINE. That is misleading as 162 * it suggests that there is no contextual limit for 163 * invoking the callback. The original motivation was 164 * readiness of the per-CPU areas. 165 * @CON_BRL: Indicates a braille device which is exempt from 166 * receiving the printk spam for obvious reasons. 167 * @CON_EXTENDED: The console supports the extended output format of 168 * /dev/kmesg which requires a larger output buffer. 169 * @CON_SUSPENDED: Indicates if a console is suspended. If true, the 170 * printing callbacks must not be called. 171 * @CON_NBCON: Console can operate outside of the legacy style console_lock 172 * constraints. 173 */ 174 enum cons_flags { 175 CON_PRINTBUFFER = BIT(0), 176 CON_CONSDEV = BIT(1), 177 CON_ENABLED = BIT(2), 178 CON_BOOT = BIT(3), 179 CON_ANYTIME = BIT(4), 180 CON_BRL = BIT(5), 181 CON_EXTENDED = BIT(6), 182 CON_SUSPENDED = BIT(7), 183 CON_NBCON = BIT(8), 184 }; 185 186 /** 187 * struct nbcon_state - console state for nbcon consoles 188 * @atom: Compound of the state fields for atomic operations 189 * 190 * @req_prio: The priority of a handover request 191 * @prio: The priority of the current owner 192 * @unsafe: Console is busy in a non takeover region 193 * @unsafe_takeover: A hostile takeover in an unsafe state happened in the 194 * past. The console cannot be safe until re-initialized. 195 * @cpu: The CPU on which the owner runs 196 * 197 * To be used for reading and preparing of the value stored in the nbcon 198 * state variable @console::nbcon_state. 199 * 200 * The @prio and @req_prio fields are particularly important to allow 201 * spin-waiting to timeout and give up without the risk of a waiter being 202 * assigned the lock after giving up. 203 */ 204 struct nbcon_state { 205 union { 206 unsigned int atom; 207 struct { 208 unsigned int prio : 2; 209 unsigned int req_prio : 2; 210 unsigned int unsafe : 1; 211 unsigned int unsafe_takeover : 1; 212 unsigned int cpu : 24; 213 }; 214 }; 215 }; 216 217 /* 218 * The nbcon_state struct is used to easily create and interpret values that 219 * are stored in the @console::nbcon_state variable. Ensure this struct stays 220 * within the size boundaries of the atomic variable's underlying type in 221 * order to avoid any accidental truncation. 222 */ 223 static_assert(sizeof(struct nbcon_state) <= sizeof(int)); 224 225 /** 226 * nbcon_prio - console owner priority for nbcon consoles 227 * @NBCON_PRIO_NONE: Unused 228 * @NBCON_PRIO_NORMAL: Normal (non-emergency) usage 229 * @NBCON_PRIO_EMERGENCY: Emergency output (WARN/OOPS...) 230 * @NBCON_PRIO_PANIC: Panic output 231 * @NBCON_PRIO_MAX: The number of priority levels 232 * 233 * A higher priority context can takeover the console when it is 234 * in the safe state. The final attempt to flush consoles in panic() 235 * can be allowed to do so even in an unsafe state (Hope and pray). 236 */ 237 enum nbcon_prio { 238 NBCON_PRIO_NONE = 0, 239 NBCON_PRIO_NORMAL, 240 NBCON_PRIO_EMERGENCY, 241 NBCON_PRIO_PANIC, 242 NBCON_PRIO_MAX, 243 }; 244 245 struct console; 246 struct printk_buffers; 247 248 /** 249 * struct nbcon_context - Context for console acquire/release 250 * @console: The associated console 251 * @spinwait_max_us: Limit for spin-wait acquire 252 * @prio: Priority of the context 253 * @allow_unsafe_takeover: Allow performing takeover even if unsafe. Can 254 * be used only with NBCON_PRIO_PANIC @prio. It 255 * might cause a system freeze when the console 256 * is used later. 257 * @backlog: Ringbuffer has pending records 258 * @pbufs: Pointer to the text buffer for this context 259 * @seq: The sequence number to print for this context 260 */ 261 struct nbcon_context { 262 /* members set by caller */ 263 struct console *console; 264 unsigned int spinwait_max_us; 265 enum nbcon_prio prio; 266 unsigned int allow_unsafe_takeover : 1; 267 268 /* members set by emit */ 269 unsigned int backlog : 1; 270 271 /* members set by acquire */ 272 struct printk_buffers *pbufs; 273 u64 seq; 274 }; 275 276 /** 277 * struct nbcon_write_context - Context handed to the nbcon write callbacks 278 * @ctxt: The core console context 279 * @outbuf: Pointer to the text buffer for output 280 * @len: Length to write 281 * @unsafe_takeover: If a hostile takeover in an unsafe state has occurred 282 */ 283 struct nbcon_write_context { 284 struct nbcon_context __private ctxt; 285 char *outbuf; 286 unsigned int len; 287 bool unsafe_takeover; 288 }; 289 290 /** 291 * struct console - The console descriptor structure 292 * @name: The name of the console driver 293 * @write: Write callback to output messages (Optional) 294 * @read: Read callback for console input (Optional) 295 * @device: The underlying TTY device driver (Optional) 296 * @unblank: Callback to unblank the console (Optional) 297 * @setup: Callback for initializing the console (Optional) 298 * @exit: Callback for teardown of the console (Optional) 299 * @match: Callback for matching a console (Optional) 300 * @flags: Console flags. See enum cons_flags 301 * @index: Console index, e.g. port number 302 * @cflag: TTY control mode flags 303 * @ispeed: TTY input speed 304 * @ospeed: TTY output speed 305 * @seq: Sequence number of the next ringbuffer record to print 306 * @dropped: Number of unreported dropped ringbuffer records 307 * @data: Driver private data 308 * @node: hlist node for the console list 309 * 310 * @write_atomic: Write callback for atomic context 311 * @nbcon_state: State for nbcon consoles 312 * @nbcon_seq: Sequence number of the next record for nbcon to print 313 * @pbufs: Pointer to nbcon private buffer 314 */ 315 struct console { 316 char name[16]; 317 void (*write)(struct console *co, const char *s, unsigned int count); 318 int (*read)(struct console *co, char *s, unsigned int count); 319 struct tty_driver *(*device)(struct console *co, int *index); 320 void (*unblank)(void); 321 int (*setup)(struct console *co, char *options); 322 int (*exit)(struct console *co); 323 int (*match)(struct console *co, char *name, int idx, char *options); 324 short flags; 325 short index; 326 int cflag; 327 uint ispeed; 328 uint ospeed; 329 u64 seq; 330 unsigned long dropped; 331 void *data; 332 struct hlist_node node; 333 334 /* nbcon console specific members */ 335 bool (*write_atomic)(struct console *con, 336 struct nbcon_write_context *wctxt); 337 atomic_t __private nbcon_state; 338 atomic_long_t __private nbcon_seq; 339 struct printk_buffers *pbufs; 340 }; 341 342 #ifdef CONFIG_LOCKDEP 343 extern void lockdep_assert_console_list_lock_held(void); 344 #else 345 static inline void lockdep_assert_console_list_lock_held(void) 346 { 347 } 348 #endif 349 350 #ifdef CONFIG_DEBUG_LOCK_ALLOC 351 extern bool console_srcu_read_lock_is_held(void); 352 #else 353 static inline bool console_srcu_read_lock_is_held(void) 354 { 355 return 1; 356 } 357 #endif 358 359 extern int console_srcu_read_lock(void); 360 extern void console_srcu_read_unlock(int cookie); 361 362 extern void console_list_lock(void) __acquires(console_mutex); 363 extern void console_list_unlock(void) __releases(console_mutex); 364 365 extern struct hlist_head console_list; 366 367 /** 368 * console_srcu_read_flags - Locklessly read the console flags 369 * @con: struct console pointer of console to read flags from 370 * 371 * This function provides the necessary READ_ONCE() and data_race() 372 * notation for locklessly reading the console flags. The READ_ONCE() 373 * in this function matches the WRITE_ONCE() when @flags are modified 374 * for registered consoles with console_srcu_write_flags(). 375 * 376 * Only use this function to read console flags when locklessly 377 * iterating the console list via srcu. 378 * 379 * Context: Any context. 380 */ 381 static inline short console_srcu_read_flags(const struct console *con) 382 { 383 WARN_ON_ONCE(!console_srcu_read_lock_is_held()); 384 385 /* 386 * Locklessly reading console->flags provides a consistent 387 * read value because there is at most one CPU modifying 388 * console->flags and that CPU is using only read-modify-write 389 * operations to do so. 390 */ 391 return data_race(READ_ONCE(con->flags)); 392 } 393 394 /** 395 * console_srcu_write_flags - Write flags for a registered console 396 * @con: struct console pointer of console to write flags to 397 * @flags: new flags value to write 398 * 399 * Only use this function to write flags for registered consoles. It 400 * requires holding the console_list_lock. 401 * 402 * Context: Any context. 403 */ 404 static inline void console_srcu_write_flags(struct console *con, short flags) 405 { 406 lockdep_assert_console_list_lock_held(); 407 408 /* This matches the READ_ONCE() in console_srcu_read_flags(). */ 409 WRITE_ONCE(con->flags, flags); 410 } 411 412 /* Variant of console_is_registered() when the console_list_lock is held. */ 413 static inline bool console_is_registered_locked(const struct console *con) 414 { 415 lockdep_assert_console_list_lock_held(); 416 return !hlist_unhashed(&con->node); 417 } 418 419 /* 420 * console_is_registered - Check if the console is registered 421 * @con: struct console pointer of console to check 422 * 423 * Context: Process context. May sleep while acquiring console list lock. 424 * Return: true if the console is in the console list, otherwise false. 425 * 426 * If false is returned for a console that was previously registered, it 427 * can be assumed that the console's unregistration is fully completed, 428 * including the exit() callback after console list removal. 429 */ 430 static inline bool console_is_registered(const struct console *con) 431 { 432 bool ret; 433 434 console_list_lock(); 435 ret = console_is_registered_locked(con); 436 console_list_unlock(); 437 return ret; 438 } 439 440 /** 441 * for_each_console_srcu() - Iterator over registered consoles 442 * @con: struct console pointer used as loop cursor 443 * 444 * Although SRCU guarantees the console list will be consistent, the 445 * struct console fields may be updated by other CPUs while iterating. 446 * 447 * Requires console_srcu_read_lock to be held. Can be invoked from 448 * any context. 449 */ 450 #define for_each_console_srcu(con) \ 451 hlist_for_each_entry_srcu(con, &console_list, node, \ 452 console_srcu_read_lock_is_held()) 453 454 /** 455 * for_each_console() - Iterator over registered consoles 456 * @con: struct console pointer used as loop cursor 457 * 458 * The console list and the console->flags are immutable while iterating. 459 * 460 * Requires console_list_lock to be held. 461 */ 462 #define for_each_console(con) \ 463 lockdep_assert_console_list_lock_held(); \ 464 hlist_for_each_entry(con, &console_list, node) 465 466 #ifdef CONFIG_PRINTK 467 extern bool nbcon_can_proceed(struct nbcon_write_context *wctxt); 468 extern bool nbcon_enter_unsafe(struct nbcon_write_context *wctxt); 469 extern bool nbcon_exit_unsafe(struct nbcon_write_context *wctxt); 470 #else 471 static inline bool nbcon_can_proceed(struct nbcon_write_context *wctxt) { return false; } 472 static inline bool nbcon_enter_unsafe(struct nbcon_write_context *wctxt) { return false; } 473 static inline bool nbcon_exit_unsafe(struct nbcon_write_context *wctxt) { return false; } 474 #endif 475 476 extern int console_set_on_cmdline; 477 extern struct console *early_console; 478 479 enum con_flush_mode { 480 CONSOLE_FLUSH_PENDING, 481 CONSOLE_REPLAY_ALL, 482 }; 483 484 extern int add_preferred_console(const char *name, const short idx, char *options); 485 extern void console_force_preferred_locked(struct console *con); 486 extern void register_console(struct console *); 487 extern int unregister_console(struct console *); 488 extern void console_lock(void); 489 extern int console_trylock(void); 490 extern void console_unlock(void); 491 extern void console_conditional_schedule(void); 492 extern void console_unblank(void); 493 extern void console_flush_on_panic(enum con_flush_mode mode); 494 extern struct tty_driver *console_device(int *); 495 extern void console_stop(struct console *); 496 extern void console_start(struct console *); 497 extern int is_console_locked(void); 498 extern int braille_register_console(struct console *, int index, 499 char *console_options, char *braille_options); 500 extern int braille_unregister_console(struct console *); 501 #ifdef CONFIG_TTY 502 extern void console_sysfs_notify(void); 503 #else 504 static inline void console_sysfs_notify(void) 505 { } 506 #endif 507 extern bool console_suspend_enabled; 508 509 /* Suspend and resume console messages over PM events */ 510 extern void suspend_console(void); 511 extern void resume_console(void); 512 513 int mda_console_init(void); 514 515 void vcs_make_sysfs(int index); 516 void vcs_remove_sysfs(int index); 517 518 /* Some debug stub to catch some of the obvious races in the VT code */ 519 #define WARN_CONSOLE_UNLOCKED() \ 520 WARN_ON(!atomic_read(&ignore_console_lock_warning) && \ 521 !is_console_locked() && !oops_in_progress) 522 /* 523 * Increment ignore_console_lock_warning if you need to quiet 524 * WARN_CONSOLE_UNLOCKED() for debugging purposes. 525 */ 526 extern atomic_t ignore_console_lock_warning; 527 528 extern void console_init(void); 529 530 /* For deferred console takeover */ 531 void dummycon_register_output_notifier(struct notifier_block *nb); 532 void dummycon_unregister_output_notifier(struct notifier_block *nb); 533 534 #endif /* _LINUX_CONSOLE_H */ 535