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