]> git.saurik.com Git - apple/xnu.git/blob - pexpert/i386/pe_kprintf.c
deb7faee2cfbc7b353fb63d4f8b3df3905b29748
[apple/xnu.git] / pexpert / i386 / pe_kprintf.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /*
31 * file: pe_kprintf.c
32 * i386 platform expert debugging output initialization.
33 */
34 #include <stdarg.h>
35 #include <machine/machine_routines.h>
36 #include <pexpert/pexpert.h>
37 #include <kern/debug.h>
38 #include <kern/simple_lock.h>
39 #include <i386/mp.h>
40
41 /* extern references */
42 extern void cnputc(char c);
43 extern int serial_init(void);
44 extern void serial_putc(char c);
45
46 /* Globals */
47 void (*PE_kputc)(char c) = 0;
48
49 unsigned int disableSerialOuput = TRUE;
50
51 decl_simple_lock_data(static, kprintf_lock)
52
53 void PE_init_kprintf(boolean_t vm_initialized)
54 {
55 unsigned int boot_arg;
56
57 if (PE_state.initialized == FALSE)
58 panic("Platform Expert not initialized");
59
60 if (!vm_initialized)
61 {
62 simple_lock_init(&kprintf_lock, 0);
63
64 if (PE_parse_boot_arg("debug", &boot_arg))
65 if (boot_arg & DB_KPRT) disableSerialOuput = FALSE;
66
67 if (!disableSerialOuput && serial_init())
68 PE_kputc = serial_putc;
69 else
70 PE_kputc = cnputc;
71 }
72 }
73
74 #ifdef MP_DEBUG
75 static void _kprintf(const char *format, ...)
76 {
77 va_list listp;
78
79 va_start(listp, format);
80 _doprnt(format, &listp, PE_kputc, 16);
81 va_end(listp);
82 }
83 #define MP_DEBUG_KPRINTF(x...) _kprintf(x)
84 #else /* MP_DEBUG */
85 #define MP_DEBUG_KPRINTF(x...)
86 #endif /* MP_DEBUG */
87
88 static int cpu_last_locked = 0;
89 void kprintf(const char *fmt, ...)
90 {
91 va_list listp;
92 boolean_t state;
93
94 if (!disableSerialOuput) {
95
96 /*
97 * Spin to get kprintf lock but re-enable interrupts while failing.
98 * This allows interrupts to be handled while waiting but
99 * interrupts are disabled once we have the lock.
100 */
101 state = ml_set_interrupts_enabled(FALSE);
102 while (!simple_lock_try(&kprintf_lock)) {
103 ml_set_interrupts_enabled(state);
104 ml_set_interrupts_enabled(FALSE);
105 }
106
107 if (cpu_number() != cpu_last_locked) {
108 MP_DEBUG_KPRINTF("[cpu%d...]\n", cpu_number());
109 cpu_last_locked = cpu_number();
110 }
111
112 va_start(listp, fmt);
113 _doprnt(fmt, &listp, PE_kputc, 16);
114 va_end(listp);
115
116 simple_unlock(&kprintf_lock);
117 ml_set_interrupts_enabled(state);
118 }
119 }
120
121 extern void kprintf_break_lock(void);
122 void
123 kprintf_break_lock(void)
124 {
125 simple_lock_init(&kprintf_lock, 0);
126 }