1 /*
2 * Copyright (c) 2000-2019 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * file: pe_kprintf.c
30 * i386 platform expert debugging output initialization.
31 */
32 #include <stdarg.h>
33 #include <machine/machine_routines.h>
34 #include <pexpert/pexpert.h>
35 #include <kern/debug.h>
36 #include <kern/simple_lock.h>
37 #include <i386/machine_cpu.h>
38 #include <i386/mp.h>
39 #include <machine/pal_routines.h>
40 #include <i386/proc_reg.h>
41 #include <os/log_private.h>
42 #include <libkern/section_keywords.h>
43 #include <kern/processor.h>
44 #include <kern/clock.h>
45 #include <mach/clock_types.h>
46
47 extern uint64_t LockTimeOut;
48
49 /* Globals */
50 typedef void (*PE_kputc_t)(char);
51
52 #if XNU_TARGET_OS_OSX
53 PE_kputc_t PE_kputc;
54 #else
55 SECURITY_READ_ONLY_LATE(PE_kputc_t) PE_kputc;
56 #endif
57
58 #if DEVELOPMENT || DEBUG
59 /* DEBUG kernel starts with true serial, but
60 * may later disable or switch to video
61 * console */
62 SECURITY_READ_ONLY_LATE(bool) disable_serial_output = false;
63 #else
64 SECURITY_READ_ONLY_LATE(bool) disable_serial_output = true;
65 #endif
66 SECURITY_READ_ONLY_LATE(bool) disable_iolog_serial_output = false;
67 SECURITY_READ_ONLY_LATE(bool) enable_dklog_serial_output = false;
68
69 static SIMPLE_LOCK_DECLARE(kprintf_lock, 0);
70
71 __startup_func
72 static void
PE_init_kprintf(void)73 PE_init_kprintf(void)
74 {
75 if (PE_state.initialized == FALSE) {
76 panic("Platform Expert not initialized");
77 }
78
79 bool new_disable_serial_output = true;
80
81 if (debug_boot_arg & DB_KPRT) {
82 new_disable_serial_output = false;
83 }
84
85 /* If we are newly enabling serial, make sure we only
86 * call pal_serial_init() if our previous state was
87 * not enabled */
88 if (!new_disable_serial_output && (!disable_serial_output || pal_serial_init())) {
89 PE_kputc = pal_serial_putc;
90 } else {
91 PE_kputc = console_write_unbuffered;
92 }
93
94 disable_serial_output = new_disable_serial_output;
95 }
96 STARTUP(KPRINTF, STARTUP_RANK_FIRST, PE_init_kprintf);
97
98 #if CONFIG_NO_KPRINTF_STRINGS
99 /* Prevent CPP from breaking the definition below */
100 #undef kprintf
101 #endif
102
103 #ifdef MP_DEBUG
104 static void
_kprintf(const char * format,...)105 _kprintf(const char *format, ...)
106 {
107 va_list listp;
108
109 va_start(listp, format);
110 _doprnt(format, &listp, PE_kputc, 16);
111 va_end(listp);
112 }
113 #define MP_DEBUG_KPRINTF(x...) _kprintf(x)
114 #else /* MP_DEBUG */
115 #define MP_DEBUG_KPRINTF(x...)
116 #endif /* MP_DEBUG */
117
118 static int cpu_last_locked = 0;
119
120 #define KPRINTF_LOCKWAIT_PATIENT (LockTimeOut)
121 #define KPRINTF_LOCKWAIT_IMPATIENT (LockTimeOut >> 4)
122
123 __attribute__((noinline, not_tail_called))
124 void
kprintf(const char * fmt,...)125 kprintf(const char *fmt, ...)
126 {
127 va_list listp;
128 va_list listp2;
129 boolean_t state;
130 boolean_t in_panic_context = FALSE;
131 unsigned int kprintf_lock_grabbed;
132 void *caller = __builtin_return_address(0);
133
134 if (!disable_serial_output) {
135 boolean_t early = FALSE;
136 uint64_t gsbase = rdmsr64(MSR_IA32_GS_BASE);
137 if (gsbase == EARLY_GSBASE_MAGIC || gsbase == 0) {
138 early = TRUE;
139 }
140 /* If PE_kputc has not yet been initialized, don't
141 * take any locks, just dump to serial */
142 if (!PE_kputc || early) {
143 va_start(listp, fmt);
144 va_copy(listp2, listp);
145
146 _doprnt_log(fmt, &listp, pal_serial_putc, 16);
147 va_end(listp);
148
149 // If interrupts are enabled
150 if (ml_get_interrupts_enabled()) {
151 #pragma clang diagnostic push
152 #pragma clang diagnostic ignored "-Wformat-nonliteral"
153 os_log_with_args(OS_LOG_DEFAULT, OS_LOG_TYPE_DEFAULT, fmt, listp2, caller);
154 #pragma clang diagnostic pop
155 }
156 va_end(listp2);
157 return;
158 }
159
160 va_start(listp, fmt);
161 va_copy(listp2, listp);
162
163 state = ml_set_interrupts_enabled(FALSE);
164
165 pal_preemption_assert();
166
167 in_panic_context = debug_is_current_cpu_in_panic_state();
168
169 // If current CPU is in panic context, be a little more impatient.
170 kprintf_lock_grabbed = simple_lock_try_lock_mp_signal_safe_loop_duration(&kprintf_lock,
171 in_panic_context ? KPRINTF_LOCKWAIT_IMPATIENT : KPRINTF_LOCKWAIT_PATIENT,
172 LCK_GRP_NULL);
173
174 if (cpu_number() != cpu_last_locked) {
175 MP_DEBUG_KPRINTF("[cpu%d...]\n", cpu_number());
176 cpu_last_locked = cpu_number();
177 }
178
179 _doprnt(fmt, &listp, PE_kputc, 16);
180
181 if (kprintf_lock_grabbed) {
182 simple_unlock(&kprintf_lock);
183 }
184
185 ml_set_interrupts_enabled(state);
186
187 va_end(listp);
188
189 // If interrupts are enabled
190 if (ml_get_interrupts_enabled()) {
191 #pragma clang diagnostic push
192 #pragma clang diagnostic ignored "-Wformat-nonliteral"
193 os_log_with_args(OS_LOG_DEFAULT, OS_LOG_TYPE_DEFAULT, fmt, listp2, caller);
194 #pragma clang diagnostic pop
195 }
196 va_end(listp2);
197 } else {
198 if (ml_get_interrupts_enabled()) {
199 va_start(listp, fmt);
200 #pragma clang diagnostic push
201 #pragma clang diagnostic ignored "-Wformat-nonliteral"
202 os_log_with_args(OS_LOG_DEFAULT, OS_LOG_TYPE_DEFAULT, fmt, listp, caller);
203 #pragma clang diagnostic pop
204 va_end(listp);
205 }
206 }
207 }
208
209 extern void kprintf_break_lock(void);
210 void
kprintf_break_lock(void)211 kprintf_break_lock(void)
212 {
213 simple_lock_init(&kprintf_lock, 0);
214 }
215