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