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