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