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