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