]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
cb323159 | 2 | * Copyright (c) 2000-2019 Apple Inc. All rights reserved. |
1c79356b | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
0a7de745 | 5 | * |
2d21ac55 A |
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. | |
0a7de745 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
0a7de745 | 17 | * |
2d21ac55 A |
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 | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
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. | |
0a7de745 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* | |
29 | * file: pe_kprintf.c | |
30 | * i386 platform expert debugging output initialization. | |
31 | */ | |
32 | #include <stdarg.h> | |
55e303ae | 33 | #include <machine/machine_routines.h> |
1c79356b A |
34 | #include <pexpert/pexpert.h> |
35 | #include <kern/debug.h> | |
55e303ae | 36 | #include <kern/simple_lock.h> |
39236c6e | 37 | #include <i386/machine_cpu.h> |
55e303ae | 38 | #include <i386/mp.h> |
6d2010ae | 39 | #include <machine/pal_routines.h> |
7ddcb079 | 40 | #include <i386/proc_reg.h> |
39037602 | 41 | #include <os/log_private.h> |
813fb2f6 | 42 | #include <libkern/section_keywords.h> |
cb323159 A |
43 | #include <kern/processor.h> |
44 | #include <kern/clock.h> | |
45 | #include <mach/clock_types.h> | |
46 | ||
47 | extern uint64_t LockTimeOut; | |
1c79356b | 48 | |
1c79356b | 49 | /* Globals */ |
f427ee49 A |
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 | |
1c79356b | 57 | |
0a7de745 | 58 | #if DEVELOPMENT || DEBUG |
b0d623f7 A |
59 | /* DEBUG kernel starts with true serial, but |
60 | * may later disable or switch to video | |
61 | * console */ | |
813fb2f6 | 62 | SECURITY_READ_ONLY_LATE(unsigned int) disable_serial_output = FALSE; |
b0d623f7 | 63 | #else |
813fb2f6 | 64 | SECURITY_READ_ONLY_LATE(unsigned int) disable_serial_output = TRUE; |
b0d623f7 | 65 | #endif |
1c79356b | 66 | |
f427ee49 | 67 | static SIMPLE_LOCK_DECLARE(kprintf_lock, 0); |
55e303ae | 68 | |
f427ee49 A |
69 | __startup_func |
70 | static void | |
71 | PE_init_kprintf(void) | |
1c79356b | 72 | { |
0a7de745 | 73 | if (PE_state.initialized == FALSE) { |
1c79356b | 74 | panic("Platform Expert not initialized"); |
0a7de745 | 75 | } |
1c79356b | 76 | |
f427ee49 | 77 | unsigned int new_disable_serial_output = TRUE; |
b0d623f7 | 78 | |
f427ee49 A |
79 | if (debug_boot_arg & DB_KPRT) { |
80 | new_disable_serial_output = FALSE; | |
81 | } | |
b0d623f7 | 82 | |
f427ee49 A |
83 | /* If we are newly enabling serial, make sure we only |
84 | * call pal_serial_init() if our previous state was | |
85 | * not enabled */ | |
86 | if (!new_disable_serial_output && (!disable_serial_output || pal_serial_init())) { | |
87 | PE_kputc = pal_serial_putc; | |
88 | } else { | |
89 | PE_kputc = cnputc_unbuffered; | |
2d21ac55 | 90 | } |
f427ee49 A |
91 | |
92 | disable_serial_output = new_disable_serial_output; | |
1c79356b | 93 | } |
f427ee49 | 94 | STARTUP(KPRINTF, STARTUP_RANK_FIRST, PE_init_kprintf); |
1c79356b | 95 | |
b0d623f7 A |
96 | #if CONFIG_NO_KPRINTF_STRINGS |
97 | /* Prevent CPP from breaking the definition below */ | |
98 | #undef kprintf | |
99 | #endif | |
100 | ||
55e303ae | 101 | #ifdef MP_DEBUG |
0a7de745 A |
102 | static void |
103 | _kprintf(const char *format, ...) | |
55e303ae A |
104 | { |
105 | va_list listp; | |
106 | ||
0a7de745 A |
107 | va_start(listp, format); |
108 | _doprnt(format, &listp, PE_kputc, 16); | |
109 | va_end(listp); | |
55e303ae | 110 | } |
0a7de745 | 111 | #define MP_DEBUG_KPRINTF(x...) _kprintf(x) |
55e303ae A |
112 | #else /* MP_DEBUG */ |
113 | #define MP_DEBUG_KPRINTF(x...) | |
114 | #endif /* MP_DEBUG */ | |
115 | ||
116 | static int cpu_last_locked = 0; | |
39037602 | 117 | |
cb323159 A |
118 | #define KPRINTF_LOCKWAIT_PATIENT (LockTimeOut) |
119 | #define KPRINTF_LOCKWAIT_IMPATIENT (LockTimeOut >> 4) | |
120 | ||
0a7de745 A |
121 | __attribute__((noinline, not_tail_called)) |
122 | void | |
123 | kprintf(const char *fmt, ...) | |
1c79356b | 124 | { |
39037602 A |
125 | va_list listp; |
126 | va_list listp2; | |
127 | boolean_t state; | |
cb323159 A |
128 | boolean_t in_panic_context = FALSE; |
129 | unsigned int kprintf_lock_grabbed; | |
39037602 | 130 | void *caller = __builtin_return_address(0); |
2d21ac55 A |
131 | |
132 | if (!disable_serial_output) { | |
7ddcb079 | 133 | boolean_t early = FALSE; |
0a7de745 A |
134 | uint64_t gsbase = rdmsr64(MSR_IA32_GS_BASE); |
135 | if (gsbase == EARLY_GSBASE_MAGIC || gsbase == 0) { | |
7ddcb079 A |
136 | early = TRUE; |
137 | } | |
b0d623f7 A |
138 | /* If PE_kputc has not yet been initialized, don't |
139 | * take any locks, just dump to serial */ | |
7ddcb079 | 140 | if (!PE_kputc || early) { |
b0d623f7 | 141 | va_start(listp, fmt); |
39037602 A |
142 | va_copy(listp2, listp); |
143 | ||
3e170ce0 | 144 | _doprnt_log(fmt, &listp, pal_serial_putc, 16); |
b0d623f7 | 145 | va_end(listp); |
39037602 A |
146 | |
147 | // If interrupts are enabled | |
148 | if (ml_get_interrupts_enabled()) { | |
149 | os_log_with_args(OS_LOG_DEFAULT, OS_LOG_TYPE_DEFAULT, fmt, listp2, caller); | |
150 | } | |
151 | va_end(listp2); | |
b0d623f7 A |
152 | return; |
153 | } | |
154 | ||
f427ee49 A |
155 | va_start(listp, fmt); |
156 | va_copy(listp2, listp); | |
157 | ||
2d21ac55 | 158 | state = ml_set_interrupts_enabled(FALSE); |
6d2010ae A |
159 | |
160 | pal_preemption_assert(); | |
161 | ||
f427ee49 | 162 | in_panic_context = debug_is_current_cpu_in_panic_state(); |
cb323159 A |
163 | |
164 | // If current CPU is in panic context, be a little more impatient. | |
165 | kprintf_lock_grabbed = simple_lock_try_lock_mp_signal_safe_loop_duration(&kprintf_lock, | |
166 | in_panic_context ? KPRINTF_LOCKWAIT_IMPATIENT : KPRINTF_LOCKWAIT_PATIENT, | |
167 | LCK_GRP_NULL); | |
2d21ac55 A |
168 | |
169 | if (cpu_number() != cpu_last_locked) { | |
170 | MP_DEBUG_KPRINTF("[cpu%d...]\n", cpu_number()); | |
171 | cpu_last_locked = cpu_number(); | |
172 | } | |
173 | ||
2d21ac55 | 174 | _doprnt(fmt, &listp, PE_kputc, 16); |
2d21ac55 | 175 | |
cb323159 A |
176 | if (kprintf_lock_grabbed) { |
177 | simple_unlock(&kprintf_lock); | |
178 | } | |
179 | ||
2d21ac55 | 180 | ml_set_interrupts_enabled(state); |
39037602 | 181 | |
f427ee49 A |
182 | va_end(listp); |
183 | ||
39037602 A |
184 | // If interrupts are enabled |
185 | if (ml_get_interrupts_enabled()) { | |
186 | os_log_with_args(OS_LOG_DEFAULT, OS_LOG_TYPE_DEFAULT, fmt, listp2, caller); | |
187 | } | |
188 | va_end(listp2); | |
0a7de745 | 189 | } else { |
39037602 A |
190 | if (ml_get_interrupts_enabled()) { |
191 | va_start(listp, fmt); | |
192 | os_log_with_args(OS_LOG_DEFAULT, OS_LOG_TYPE_DEFAULT, fmt, listp, caller); | |
193 | va_end(listp); | |
194 | } | |
55e303ae | 195 | } |
1c79356b | 196 | } |
0c530ab8 A |
197 | |
198 | extern void kprintf_break_lock(void); | |
199 | void | |
200 | kprintf_break_lock(void) | |
201 | { | |
202 | simple_lock_init(&kprintf_lock, 0); | |
203 | } |