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