1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
34extern void cnputc(char c);
35extern int serial_init(void);
36extern void serial_putc(char c);
37
38
39void (*PE_kputc)(char c) = 0;
40
41unsigned int disableSerialOuput = TRUE;
42
43decl_simple_lock_data(static, kprintf_lock)
44
45void 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
67static 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
77#define MP_DEBUG_KPRINTF(x...)
78#endif
79
80static int cpu_last_locked = 0;
81void kprintf(const char *fmt, ...)
82{
83 va_list listp;
84 boolean_t state;
85
86 if (!disableSerialOuput) {
87
88
89
90
91
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}
112