]> git.saurik.com Git - apple/xnu.git/blob - pexpert/i386/pe_kprintf.c
xnu-6153.141.1.tar.gz
[apple/xnu.git] / pexpert / i386 / pe_kprintf.c
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 extern processor_t current_processor(void);
49
50 /* Globals */
51 void (*PE_kputc)(char c);
52
53 #if DEVELOPMENT || DEBUG
54 /* DEBUG kernel starts with true serial, but
55 * may later disable or switch to video
56 * console */
57 SECURITY_READ_ONLY_LATE(unsigned int) disable_serial_output = FALSE;
58 #else
59 SECURITY_READ_ONLY_LATE(unsigned int) disable_serial_output = TRUE;
60 #endif
61
62 decl_simple_lock_data(static, kprintf_lock);
63
64 void
65 PE_init_kprintf(boolean_t vm_initialized)
66 {
67 unsigned int boot_arg;
68
69 if (PE_state.initialized == FALSE) {
70 panic("Platform Expert not initialized");
71 }
72
73 if (!vm_initialized) {
74 unsigned int new_disable_serial_output = TRUE;
75
76 simple_lock_init(&kprintf_lock, 0);
77
78 if (PE_parse_boot_argn("debug", &boot_arg, sizeof(boot_arg))) {
79 if (boot_arg & DB_KPRT) {
80 new_disable_serial_output = FALSE;
81 }
82 }
83
84 /* If we are newly enabling serial, make sure we only
85 * call pal_serial_init() if our previous state was
86 * not enabled */
87 if (!new_disable_serial_output && (!disable_serial_output || pal_serial_init())) {
88 PE_kputc = pal_serial_putc;
89 } else {
90 PE_kputc = cnputc;
91 }
92
93 disable_serial_output = new_disable_serial_output;
94 }
95 }
96
97 #if CONFIG_NO_KPRINTF_STRINGS
98 /* Prevent CPP from breaking the definition below */
99 #undef kprintf
100 #endif
101
102 #ifdef MP_DEBUG
103 static void
104 _kprintf(const char *format, ...)
105 {
106 va_list listp;
107
108 va_start(listp, format);
109 _doprnt(format, &listp, PE_kputc, 16);
110 va_end(listp);
111 }
112 #define MP_DEBUG_KPRINTF(x...) _kprintf(x)
113 #else /* MP_DEBUG */
114 #define MP_DEBUG_KPRINTF(x...)
115 #endif /* MP_DEBUG */
116
117 static int cpu_last_locked = 0;
118
119 #define KPRINTF_LOCKWAIT_PATIENT (LockTimeOut)
120 #define KPRINTF_LOCKWAIT_IMPATIENT (LockTimeOut >> 4)
121
122 __attribute__((noinline, not_tail_called))
123 void
124 kprintf(const char *fmt, ...)
125 {
126 va_list listp;
127 va_list listp2;
128 boolean_t state;
129 boolean_t in_panic_context = FALSE;
130 unsigned int kprintf_lock_grabbed;
131 void *caller = __builtin_return_address(0);
132
133 if (!disable_serial_output) {
134 boolean_t early = FALSE;
135 uint64_t gsbase = rdmsr64(MSR_IA32_GS_BASE);
136 if (gsbase == EARLY_GSBASE_MAGIC || gsbase == 0) {
137 early = TRUE;
138 }
139 /* If PE_kputc has not yet been initialized, don't
140 * take any locks, just dump to serial */
141 if (!PE_kputc || early) {
142 va_start(listp, fmt);
143 va_copy(listp2, listp);
144
145 _doprnt_log(fmt, &listp, pal_serial_putc, 16);
146 va_end(listp);
147
148 // If interrupts are enabled
149 if (ml_get_interrupts_enabled()) {
150 os_log_with_args(OS_LOG_DEFAULT, OS_LOG_TYPE_DEFAULT, fmt, listp2, caller);
151 }
152 va_end(listp2);
153 return;
154 }
155
156 state = ml_set_interrupts_enabled(FALSE);
157
158 pal_preemption_assert();
159
160 in_panic_context = processor_in_panic_context(current_processor());
161
162 // If current CPU is in panic context, be a little more impatient.
163 kprintf_lock_grabbed = simple_lock_try_lock_mp_signal_safe_loop_duration(&kprintf_lock,
164 in_panic_context ? KPRINTF_LOCKWAIT_IMPATIENT : KPRINTF_LOCKWAIT_PATIENT,
165 LCK_GRP_NULL);
166
167 if (cpu_number() != cpu_last_locked) {
168 MP_DEBUG_KPRINTF("[cpu%d...]\n", cpu_number());
169 cpu_last_locked = cpu_number();
170 }
171
172 va_start(listp, fmt);
173 va_copy(listp2, listp);
174 _doprnt(fmt, &listp, PE_kputc, 16);
175 va_end(listp);
176
177 if (kprintf_lock_grabbed) {
178 simple_unlock(&kprintf_lock);
179 }
180
181 ml_set_interrupts_enabled(state);
182
183 // If interrupts are enabled
184 if (ml_get_interrupts_enabled()) {
185 os_log_with_args(OS_LOG_DEFAULT, OS_LOG_TYPE_DEFAULT, fmt, listp2, caller);
186 }
187 va_end(listp2);
188 } else {
189 if (ml_get_interrupts_enabled()) {
190 va_start(listp, fmt);
191 os_log_with_args(OS_LOG_DEFAULT, OS_LOG_TYPE_DEFAULT, fmt, listp, caller);
192 va_end(listp);
193 }
194 }
195 }
196
197
198
199 extern void kprintf_break_lock(void);
200 void
201 kprintf_break_lock(void)
202 {
203 simple_lock_init(&kprintf_lock, 0);
204 }