]> git.saurik.com Git - apple/xnu.git/blame - pexpert/i386/pe_kprintf.c
xnu-2050.22.13.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
A
36#include <kern/simple_lock.h>
37#include <i386/mp.h>
6d2010ae 38#include <machine/pal_routines.h>
7ddcb079 39#include <i386/proc_reg.h>
1c79356b 40
1c79356b 41/* Globals */
2d21ac55 42void (*PE_kputc)(char c);
1c79356b 43
b0d623f7
A
44#if DEBUG
45/* DEBUG kernel starts with true serial, but
46 * may later disable or switch to video
47 * console */
48unsigned int disable_serial_output = FALSE;
49#else
2d21ac55 50unsigned int disable_serial_output = TRUE;
b0d623f7 51#endif
1c79356b 52
55e303ae
A
53decl_simple_lock_data(static, kprintf_lock)
54
1c79356b
A
55void PE_init_kprintf(boolean_t vm_initialized)
56{
57 unsigned int boot_arg;
58
59 if (PE_state.initialized == FALSE)
60 panic("Platform Expert not initialized");
61
2d21ac55 62 if (!vm_initialized) {
b0d623f7
A
63 unsigned int new_disable_serial_output = TRUE;
64
2d21ac55 65 simple_lock_init(&kprintf_lock, 0);
55e303ae 66
593a1d5f 67 if (PE_parse_boot_argn("debug", &boot_arg, sizeof (boot_arg)))
2d21ac55 68 if (boot_arg & DB_KPRT)
b0d623f7 69 new_disable_serial_output = FALSE;
55e303ae 70
6d2010ae
A
71 /* If we are newly enabling serial, make sure we only
72 * call pal_serial_init() if our previous state was
73 * not enabled */
74 if (!new_disable_serial_output && (!disable_serial_output || pal_serial_init()))
75 PE_kputc = pal_serial_putc;
2d21ac55
A
76 else
77 PE_kputc = cnputc;
b0d623f7
A
78
79 disable_serial_output = new_disable_serial_output;
2d21ac55 80 }
1c79356b
A
81}
82
b0d623f7
A
83#if CONFIG_NO_KPRINTF_STRINGS
84/* Prevent CPP from breaking the definition below */
85#undef kprintf
86#endif
87
55e303ae
A
88#ifdef MP_DEBUG
89static void _kprintf(const char *format, ...)
90{
91 va_list listp;
92
93 va_start(listp, format);
94 _doprnt(format, &listp, PE_kputc, 16);
95 va_end(listp);
96}
97#define MP_DEBUG_KPRINTF(x...) _kprintf(x)
98#else /* MP_DEBUG */
99#define MP_DEBUG_KPRINTF(x...)
100#endif /* MP_DEBUG */
101
102static int cpu_last_locked = 0;
1c79356b
A
103void kprintf(const char *fmt, ...)
104{
55e303ae 105 va_list listp;
2d21ac55
A
106 boolean_t state;
107
108 if (!disable_serial_output) {
7ddcb079
A
109 boolean_t early = FALSE;
110 if (rdmsr64(MSR_IA32_GS_BASE) == 0) {
111 early = TRUE;
112 }
b0d623f7
A
113 /* If PE_kputc has not yet been initialized, don't
114 * take any locks, just dump to serial */
7ddcb079 115 if (!PE_kputc || early) {
b0d623f7 116 va_start(listp, fmt);
6d2010ae 117 _doprnt(fmt, &listp, pal_serial_putc, 16);
b0d623f7
A
118 va_end(listp);
119 return;
120 }
121
2d21ac55
A
122 /*
123 * Spin to get kprintf lock but re-enable interrupts while
124 * failing.
125 * This allows interrupts to be handled while waiting but
126 * interrupts are disabled once we have the lock.
127 */
128 state = ml_set_interrupts_enabled(FALSE);
6d2010ae
A
129
130 pal_preemption_assert();
131
2d21ac55
A
132 while (!simple_lock_try(&kprintf_lock)) {
133 ml_set_interrupts_enabled(state);
134 ml_set_interrupts_enabled(FALSE);
135 }
136
137 if (cpu_number() != cpu_last_locked) {
138 MP_DEBUG_KPRINTF("[cpu%d...]\n", cpu_number());
139 cpu_last_locked = cpu_number();
140 }
141
142 va_start(listp, fmt);
143 _doprnt(fmt, &listp, PE_kputc, 16);
144 va_end(listp);
145
146 simple_unlock(&kprintf_lock);
147 ml_set_interrupts_enabled(state);
55e303ae 148 }
1c79356b 149}
0c530ab8
A
150
151extern void kprintf_break_lock(void);
152void
153kprintf_break_lock(void)
154{
155 simple_lock_init(&kprintf_lock, 0);
156}