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