xref: /xnu-8796.101.5/pexpert/arm/pe_kprintf.c (revision aca3beaa3dfbd42498b42c5e5ce20a938e6554e5)
1 /*
2  * Copyright (c) 2000-2019 Apple Inc. All rights reserved.
3  */
4 /*
5  * file: pe_kprintf.c
6  *    arm platform expert debugging output initialization.
7  */
8 #include <stdarg.h>
9 #include <machine/machine_routines.h>
10 #include <pexpert/pexpert.h>
11 #include <kern/debug.h>
12 #include <kern/simple_lock.h>
13 #include <os/log_private.h>
14 #include <libkern/section_keywords.h>
15 
16 /* Globals */
17 typedef void (*PE_kputc_t)(char);
18 SECURITY_READ_ONLY_LATE(PE_kputc_t) PE_kputc;
19 
20 // disable_serial_output disables kprintf() *and* unbuffered panic output.
21 SECURITY_READ_ONLY_LATE(bool) disable_serial_output = true;
22 // disable_kprintf_output only disables kprintf().
23 SECURITY_READ_ONLY_LATE(bool) disable_kprintf_output = true;
24 // disable_iolog_serial_output only disables IOLog, controlled by
25 // SERIALMODE_NO_IOLOG.
26 SECURITY_READ_ONLY_LATE(bool) disable_iolog_serial_output = false;
27 SECURITY_READ_ONLY_LATE(bool) enable_dklog_serial_output = false;
28 
29 static SIMPLE_LOCK_DECLARE(kprintf_lock, 0);
30 
31 static void serial_putc_crlf(char c);
32 
33 __startup_func
34 static void
PE_init_kprintf_config(void)35 PE_init_kprintf_config(void)
36 {
37 	if (PE_state.initialized == FALSE) {
38 		panic("Platform Expert not initialized");
39 	}
40 
41 	if (debug_boot_arg & DB_KPRT) {
42 		disable_serial_output = false;
43 	}
44 
45 #if DEBUG
46 	disable_kprintf_output = false;
47 #elif DEVELOPMENT
48 	bool enable_kprintf_spam = false;
49 	if (PE_parse_boot_argn("-enable_kprintf_spam", &enable_kprintf_spam, sizeof(enable_kprintf_spam))) {
50 		disable_kprintf_output = false;
51 	}
52 #endif
53 }
54 // Do this early, so other code can depend on whether kprintf is enabled.
55 STARTUP(TUNABLES, STARTUP_RANK_LAST, PE_init_kprintf_config);
56 
57 __startup_func
58 static void
PE_init_kprintf(void)59 PE_init_kprintf(void)
60 {
61 	if (serial_init()) {
62 		PE_kputc = serial_putc_crlf;
63 	} else {
64 		/**
65 		 * If serial failed to initialize then fall back to using the console,
66 		 * and assume the console is using the video console (because clearly
67 		 * serial doesn't work).
68 		 */
69 		PE_kputc = console_write_unbuffered;
70 	}
71 }
72 STARTUP(KPRINTF, STARTUP_RANK_FIRST, PE_init_kprintf);
73 
74 #ifdef MP_DEBUG
75 static void
_kprintf(const char * format,...)76 _kprintf(const char *format, ...)
77 {
78 	va_list         listp;
79 
80 	va_start(listp, format);
81 	_doprnt_log(format, &listp, PE_kputc, 16);
82 	va_end(listp);
83 }
84 #define MP_DEBUG_KPRINTF(x...)  _kprintf(x)
85 #else                           /* MP_DEBUG */
86 #define MP_DEBUG_KPRINTF(x...)
87 #endif                          /* MP_DEBUG */
88 
89 #if CONFIG_NO_KPRINTF_STRINGS
90 /* Prevent CPP from breaking the definition below */
91 #undef kprintf
92 #endif
93 
94 static int      cpu_last_locked = 0;
95 
96 __attribute__((noinline, not_tail_called))
97 void
kprintf(const char * fmt,...)98 kprintf(const char *fmt, ...)
99 {
100 	va_list         listp;
101 	va_list         listp2;
102 	boolean_t       state;
103 	void           *caller = __builtin_return_address(0);
104 
105 	if (!disable_serial_output && !disable_kprintf_output) {
106 		va_start(listp, fmt);
107 		va_copy(listp2, listp);
108 		/*
109 		 * Spin to get kprintf lock but re-enable interrupts while failing.
110 		 * This allows interrupts to be handled while waiting but
111 		 * interrupts are disabled once we have the lock.
112 		 */
113 		state = ml_set_interrupts_enabled(FALSE);
114 		while (!simple_lock_try(&kprintf_lock, LCK_GRP_NULL)) {
115 			ml_set_interrupts_enabled(state);
116 			ml_set_interrupts_enabled(FALSE);
117 		}
118 
119 		if (cpu_number() != cpu_last_locked) {
120 			MP_DEBUG_KPRINTF("[cpu%d...]\n", cpu_number());
121 			cpu_last_locked = cpu_number();
122 		}
123 
124 		_doprnt_log(fmt, &listp, PE_kputc, 16);
125 
126 		simple_unlock(&kprintf_lock);
127 
128 #if SCHED_HYGIENE_DEBUG
129 		/*
130 		 * kprintf holds interrupts disabled for far too long
131 		 * and would trip the spin-debugger.  If we are about to reenable
132 		 * interrupts then clear the timer and avoid panicking on the delay.
133 		 * Otherwise, let the code that printed with interrupt disabled
134 		 * take the panic when it reenables interrupts.
135 		 * Hopefully one day this is fixed so that this workaround is unnecessary.
136 		 */
137 		if (state == TRUE) {
138 			ml_spin_debug_clear_self();
139 		}
140 #endif
141 		ml_set_interrupts_enabled(state);
142 		va_end(listp);
143 
144 #pragma clang diagnostic push
145 #pragma clang diagnostic ignored "-Wformat-nonliteral"
146 		os_log_with_args(OS_LOG_DEFAULT, OS_LOG_TYPE_DEFAULT, fmt, listp2, caller);
147 		va_end(listp2);
148 	} else {
149 		va_start(listp, fmt);
150 		os_log_with_args(OS_LOG_DEFAULT, OS_LOG_TYPE_DEFAULT, fmt, listp, caller);
151 		va_end(listp);
152 	}
153 #pragma clang diagnostic pop
154 }
155 
156 static void
serial_putc_crlf(char c)157 serial_putc_crlf(char c)
158 {
159 	if (c == '\n') {
160 		uart_putc('\r');
161 	}
162 	uart_putc(c);
163 }
164 
165 void
serial_putc_options(char c,bool poll)166 serial_putc_options(char c, bool poll)
167 {
168 	uart_putc_options(c, poll);
169 }
170 
171 void
serial_putc(char c)172 serial_putc(char c)
173 {
174 	uart_putc(c);
175 }
176 
177 int
serial_getc(void)178 serial_getc(void)
179 {
180 	return uart_getc();
181 }
182