]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 2000-2019 Apple Inc. All rights reserved. | |
3 | */ | |
4 | /* | |
5 | * file: pe_kprintf.c | |
6 | * arm platform expert debugging output initialization. | |
7 | */ | |
8 | #include <stdarg.h> | |
9 | #include <machine/machine_routines.h> | |
10 | #include <pexpert/pexpert.h> | |
11 | #include <kern/debug.h> | |
12 | #include <kern/simple_lock.h> | |
13 | #include <os/log_private.h> | |
14 | #include <libkern/section_keywords.h> | |
15 | ||
16 | /* Globals */ | |
17 | typedef void (*PE_kputc_t)(char); | |
18 | SECURITY_READ_ONLY_LATE(PE_kputc_t) PE_kputc; | |
19 | ||
20 | // disable_serial_output disables kprintf() *and* unbuffered panic output. | |
21 | // disable_kprintf_output only disables kprintf(). | |
22 | SECURITY_READ_ONLY_LATE(unsigned int) disable_serial_output = TRUE; | |
23 | static SECURITY_READ_ONLY_LATE(unsigned int) disable_kprintf_output = TRUE; | |
24 | ||
25 | static SIMPLE_LOCK_DECLARE(kprintf_lock, 0); | |
26 | ||
27 | static void serial_putc_crlf(char c); | |
28 | ||
29 | __startup_func | |
30 | static void | |
31 | PE_init_kprintf(void) | |
32 | { | |
33 | if (PE_state.initialized == FALSE) { | |
34 | panic("Platform Expert not initialized"); | |
35 | } | |
36 | ||
37 | if (debug_boot_arg & DB_KPRT) { | |
38 | disable_serial_output = FALSE; | |
39 | } | |
40 | ||
41 | #if DEBUG | |
42 | disable_kprintf_output = FALSE; | |
43 | #elif DEVELOPMENT | |
44 | bool enable_kprintf_spam = false; | |
45 | if (PE_parse_boot_argn("-enable_kprintf_spam", &enable_kprintf_spam, sizeof(enable_kprintf_spam))) { | |
46 | disable_kprintf_output = FALSE; | |
47 | } | |
48 | #endif | |
49 | ||
50 | if (serial_init()) { | |
51 | PE_kputc = serial_putc_crlf; | |
52 | } else { | |
53 | PE_kputc = cnputc_unbuffered; | |
54 | } | |
55 | } | |
56 | STARTUP(KPRINTF, STARTUP_RANK_FIRST, PE_init_kprintf); | |
57 | ||
58 | #ifdef MP_DEBUG | |
59 | static void | |
60 | _kprintf(const char *format, ...) | |
61 | { | |
62 | va_list listp; | |
63 | ||
64 | va_start(listp, format); | |
65 | _doprnt_log(format, &listp, PE_kputc, 16); | |
66 | va_end(listp); | |
67 | } | |
68 | #define MP_DEBUG_KPRINTF(x...) _kprintf(x) | |
69 | #else /* MP_DEBUG */ | |
70 | #define MP_DEBUG_KPRINTF(x...) | |
71 | #endif /* MP_DEBUG */ | |
72 | ||
73 | #if CONFIG_NO_KPRINTF_STRINGS | |
74 | /* Prevent CPP from breaking the definition below */ | |
75 | #undef kprintf | |
76 | #endif | |
77 | ||
78 | static int cpu_last_locked = 0; | |
79 | ||
80 | __attribute__((noinline, not_tail_called)) | |
81 | void | |
82 | kprintf(const char *fmt, ...) | |
83 | { | |
84 | va_list listp; | |
85 | va_list listp2; | |
86 | boolean_t state; | |
87 | void *caller = __builtin_return_address(0); | |
88 | ||
89 | if (!disable_serial_output && !disable_kprintf_output) { | |
90 | va_start(listp, fmt); | |
91 | va_copy(listp2, listp); | |
92 | /* | |
93 | * Spin to get kprintf lock but re-enable interrupts while failing. | |
94 | * This allows interrupts to be handled while waiting but | |
95 | * interrupts are disabled once we have the lock. | |
96 | */ | |
97 | state = ml_set_interrupts_enabled(FALSE); | |
98 | while (!simple_lock_try(&kprintf_lock, LCK_GRP_NULL)) { | |
99 | ml_set_interrupts_enabled(state); | |
100 | ml_set_interrupts_enabled(FALSE); | |
101 | } | |
102 | ||
103 | if (cpu_number() != cpu_last_locked) { | |
104 | MP_DEBUG_KPRINTF("[cpu%d...]\n", cpu_number()); | |
105 | cpu_last_locked = cpu_number(); | |
106 | } | |
107 | ||
108 | _doprnt_log(fmt, &listp, PE_kputc, 16); | |
109 | ||
110 | simple_unlock(&kprintf_lock); | |
111 | ||
112 | #if INTERRUPT_MASKED_DEBUG | |
113 | /* | |
114 | * kprintf holds interrupts disabled for far too long | |
115 | * and would trip the spin-debugger. If we are about to reenable | |
116 | * interrupts then clear the timer and avoid panicking on the delay. | |
117 | * Otherwise, let the code that printed with interrupt disabled | |
118 | * take the panic when it reenables interrupts. | |
119 | * Hopefully one day this is fixed so that this workaround is unnecessary. | |
120 | */ | |
121 | if (state == TRUE) { | |
122 | ml_spin_debug_clear_self(); | |
123 | } | |
124 | #endif | |
125 | ml_set_interrupts_enabled(state); | |
126 | va_end(listp); | |
127 | ||
128 | os_log_with_args(OS_LOG_DEFAULT, OS_LOG_TYPE_DEFAULT, fmt, listp2, caller); | |
129 | va_end(listp2); | |
130 | } else { | |
131 | va_start(listp, fmt); | |
132 | os_log_with_args(OS_LOG_DEFAULT, OS_LOG_TYPE_DEFAULT, fmt, listp, caller); | |
133 | va_end(listp); | |
134 | } | |
135 | } | |
136 | ||
137 | static void | |
138 | serial_putc_crlf(char c) | |
139 | { | |
140 | if (c == '\n') { | |
141 | uart_putc('\r'); | |
142 | } | |
143 | uart_putc(c); | |
144 | } | |
145 | ||
146 | void | |
147 | serial_putc(char c) | |
148 | { | |
149 | uart_putc(c); | |
150 | } | |
151 | ||
152 | int | |
153 | serial_getc(void) | |
154 | { | |
155 | return uart_getc(); | |
156 | } |