xref: /linux-6.15/include/linux/console.h (revision 7d56936c)
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  * @pbufs:		Pointer to nbcon private buffer
326  */
327 struct console {
328 	char			name[16];
329 	void			(*write)(struct console *co, const char *s, unsigned int count);
330 	int			(*read)(struct console *co, char *s, unsigned int count);
331 	struct tty_driver	*(*device)(struct console *co, int *index);
332 	void			(*unblank)(void);
333 	int			(*setup)(struct console *co, char *options);
334 	int			(*exit)(struct console *co);
335 	int			(*match)(struct console *co, char *name, int idx, char *options);
336 	short			flags;
337 	short			index;
338 	int			cflag;
339 	uint			ispeed;
340 	uint			ospeed;
341 	u64			seq;
342 	unsigned long		dropped;
343 	void			*data;
344 	struct hlist_node	node;
345 
346 	/* nbcon console specific members */
347 
348 	/**
349 	 * @write_atomic:
350 	 *
351 	 * NBCON callback to write out text in any context. (Optional)
352 	 *
353 	 * This callback is called with the console already acquired. However,
354 	 * a higher priority context is allowed to take it over by default.
355 	 *
356 	 * The callback must call nbcon_enter_unsafe() and nbcon_exit_unsafe()
357 	 * around any code where the takeover is not safe, for example, when
358 	 * manipulating the serial port registers.
359 	 *
360 	 * nbcon_enter_unsafe() will fail if the context has lost the console
361 	 * ownership in the meantime. In this case, the callback is no longer
362 	 * allowed to go forward. It must back out immediately and carefully.
363 	 * The buffer content is also no longer trusted since it no longer
364 	 * belongs to the context.
365 	 *
366 	 * The callback should allow the takeover whenever it is safe. It
367 	 * increases the chance to see messages when the system is in trouble.
368 	 *
369 	 * The callback can be called from any context (including NMI).
370 	 * Therefore it must avoid usage of any locking and instead rely
371 	 * on the console ownership for synchronization.
372 	 */
373 	void (*write_atomic)(struct console *con, struct nbcon_write_context *wctxt);
374 
375 	atomic_t		__private nbcon_state;
376 	atomic_long_t		__private nbcon_seq;
377 	struct printk_buffers	*pbufs;
378 };
379 
380 #ifdef CONFIG_LOCKDEP
381 extern void lockdep_assert_console_list_lock_held(void);
382 #else
383 static inline void lockdep_assert_console_list_lock_held(void)
384 {
385 }
386 #endif
387 
388 #ifdef CONFIG_DEBUG_LOCK_ALLOC
389 extern bool console_srcu_read_lock_is_held(void);
390 #else
391 static inline bool console_srcu_read_lock_is_held(void)
392 {
393 	return 1;
394 }
395 #endif
396 
397 extern int console_srcu_read_lock(void);
398 extern void console_srcu_read_unlock(int cookie);
399 
400 extern void console_list_lock(void) __acquires(console_mutex);
401 extern void console_list_unlock(void) __releases(console_mutex);
402 
403 extern struct hlist_head console_list;
404 
405 /**
406  * console_srcu_read_flags - Locklessly read the console flags
407  * @con:	struct console pointer of console to read flags from
408  *
409  * This function provides the necessary READ_ONCE() and data_race()
410  * notation for locklessly reading the console flags. The READ_ONCE()
411  * in this function matches the WRITE_ONCE() when @flags are modified
412  * for registered consoles with console_srcu_write_flags().
413  *
414  * Only use this function to read console flags when locklessly
415  * iterating the console list via srcu.
416  *
417  * Context: Any context.
418  */
419 static inline short console_srcu_read_flags(const struct console *con)
420 {
421 	WARN_ON_ONCE(!console_srcu_read_lock_is_held());
422 
423 	/*
424 	 * Locklessly reading console->flags provides a consistent
425 	 * read value because there is at most one CPU modifying
426 	 * console->flags and that CPU is using only read-modify-write
427 	 * operations to do so.
428 	 */
429 	return data_race(READ_ONCE(con->flags));
430 }
431 
432 /**
433  * console_srcu_write_flags - Write flags for a registered console
434  * @con:	struct console pointer of console to write flags to
435  * @flags:	new flags value to write
436  *
437  * Only use this function to write flags for registered consoles. It
438  * requires holding the console_list_lock.
439  *
440  * Context: Any context.
441  */
442 static inline void console_srcu_write_flags(struct console *con, short flags)
443 {
444 	lockdep_assert_console_list_lock_held();
445 
446 	/* This matches the READ_ONCE() in console_srcu_read_flags(). */
447 	WRITE_ONCE(con->flags, flags);
448 }
449 
450 /* Variant of console_is_registered() when the console_list_lock is held. */
451 static inline bool console_is_registered_locked(const struct console *con)
452 {
453 	lockdep_assert_console_list_lock_held();
454 	return !hlist_unhashed(&con->node);
455 }
456 
457 /*
458  * console_is_registered - Check if the console is registered
459  * @con:	struct console pointer of console to check
460  *
461  * Context: Process context. May sleep while acquiring console list lock.
462  * Return: true if the console is in the console list, otherwise false.
463  *
464  * If false is returned for a console that was previously registered, it
465  * can be assumed that the console's unregistration is fully completed,
466  * including the exit() callback after console list removal.
467  */
468 static inline bool console_is_registered(const struct console *con)
469 {
470 	bool ret;
471 
472 	console_list_lock();
473 	ret = console_is_registered_locked(con);
474 	console_list_unlock();
475 	return ret;
476 }
477 
478 /**
479  * for_each_console_srcu() - Iterator over registered consoles
480  * @con:	struct console pointer used as loop cursor
481  *
482  * Although SRCU guarantees the console list will be consistent, the
483  * struct console fields may be updated by other CPUs while iterating.
484  *
485  * Requires console_srcu_read_lock to be held. Can be invoked from
486  * any context.
487  */
488 #define for_each_console_srcu(con)					\
489 	hlist_for_each_entry_srcu(con, &console_list, node,		\
490 				  console_srcu_read_lock_is_held())
491 
492 /**
493  * for_each_console() - Iterator over registered consoles
494  * @con:	struct console pointer used as loop cursor
495  *
496  * The console list and the &console.flags are immutable while iterating.
497  *
498  * Requires console_list_lock to be held.
499  */
500 #define for_each_console(con)						\
501 	lockdep_assert_console_list_lock_held();			\
502 	hlist_for_each_entry(con, &console_list, node)
503 
504 #ifdef CONFIG_PRINTK
505 extern bool nbcon_can_proceed(struct nbcon_write_context *wctxt);
506 extern bool nbcon_enter_unsafe(struct nbcon_write_context *wctxt);
507 extern bool nbcon_exit_unsafe(struct nbcon_write_context *wctxt);
508 #else
509 static inline bool nbcon_can_proceed(struct nbcon_write_context *wctxt) { return false; }
510 static inline bool nbcon_enter_unsafe(struct nbcon_write_context *wctxt) { return false; }
511 static inline bool nbcon_exit_unsafe(struct nbcon_write_context *wctxt) { return false; }
512 #endif
513 
514 extern int console_set_on_cmdline;
515 extern struct console *early_console;
516 
517 enum con_flush_mode {
518 	CONSOLE_FLUSH_PENDING,
519 	CONSOLE_REPLAY_ALL,
520 };
521 
522 extern int add_preferred_console(const char *name, const short idx, char *options);
523 extern void console_force_preferred_locked(struct console *con);
524 extern void register_console(struct console *);
525 extern int unregister_console(struct console *);
526 extern void console_lock(void);
527 extern int console_trylock(void);
528 extern void console_unlock(void);
529 extern void console_conditional_schedule(void);
530 extern void console_unblank(void);
531 extern void console_flush_on_panic(enum con_flush_mode mode);
532 extern struct tty_driver *console_device(int *);
533 extern void console_stop(struct console *);
534 extern void console_start(struct console *);
535 extern int is_console_locked(void);
536 extern int braille_register_console(struct console *, int index,
537 		char *console_options, char *braille_options);
538 extern int braille_unregister_console(struct console *);
539 #ifdef CONFIG_TTY
540 extern void console_sysfs_notify(void);
541 #else
542 static inline void console_sysfs_notify(void)
543 { }
544 #endif
545 extern bool console_suspend_enabled;
546 
547 /* Suspend and resume console messages over PM events */
548 extern void suspend_console(void);
549 extern void resume_console(void);
550 
551 int mda_console_init(void);
552 
553 void vcs_make_sysfs(int index);
554 void vcs_remove_sysfs(int index);
555 
556 /* Some debug stub to catch some of the obvious races in the VT code */
557 #define WARN_CONSOLE_UNLOCKED()						\
558 	WARN_ON(!atomic_read(&ignore_console_lock_warning) &&		\
559 		!is_console_locked() && !oops_in_progress)
560 /*
561  * Increment ignore_console_lock_warning if you need to quiet
562  * WARN_CONSOLE_UNLOCKED() for debugging purposes.
563  */
564 extern atomic_t ignore_console_lock_warning;
565 
566 extern void console_init(void);
567 
568 /* For deferred console takeover */
569 void dummycon_register_output_notifier(struct notifier_block *nb);
570 void dummycon_unregister_output_notifier(struct notifier_block *nb);
571 
572 #endif /* _LINUX_CONSOLE_H */
573