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