| 1 | /* |
| 2 | * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved. |
| 3 | * |
| 4 | * @APPLE_OSREFERENCE_LICENSE_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 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. |
| 14 | * |
| 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 |
| 20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
| 21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, |
| 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. |
| 25 | * |
| 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
| 27 | */ |
| 28 | /* |
| 29 | * file: pe_kprintf.c |
| 30 | * i386 platform expert debugging output initialization. |
| 31 | */ |
| 32 | #include <stdarg.h> |
| 33 | #include <machine/machine_routines.h> |
| 34 | #include <pexpert/pexpert.h> |
| 35 | #include <kern/debug.h> |
| 36 | #include <kern/simple_lock.h> |
| 37 | #include <i386/machine_cpu.h> |
| 38 | #include <i386/mp.h> |
| 39 | #include <machine/pal_routines.h> |
| 40 | #include <i386/proc_reg.h> |
| 41 | #include <os/log_private.h> |
| 42 | #include <libkern/section_keywords.h> |
| 43 | |
| 44 | /* Globals */ |
| 45 | void (*PE_kputc)(char c); |
| 46 | |
| 47 | #if DEVELOPMENT || DEBUG |
| 48 | /* DEBUG kernel starts with true serial, but |
| 49 | * may later disable or switch to video |
| 50 | * console */ |
| 51 | SECURITY_READ_ONLY_LATE(unsigned int) disable_serial_output = FALSE; |
| 52 | #else |
| 53 | SECURITY_READ_ONLY_LATE(unsigned int) disable_serial_output = TRUE; |
| 54 | #endif |
| 55 | |
| 56 | decl_simple_lock_data(static, kprintf_lock) |
| 57 | |
| 58 | void PE_init_kprintf(boolean_t vm_initialized) |
| 59 | { |
| 60 | unsigned int boot_arg; |
| 61 | |
| 62 | if (PE_state.initialized == FALSE) |
| 63 | panic("Platform Expert not initialized" ); |
| 64 | |
| 65 | if (!vm_initialized) { |
| 66 | unsigned int new_disable_serial_output = TRUE; |
| 67 | |
| 68 | simple_lock_init(&kprintf_lock, 0); |
| 69 | |
| 70 | if (PE_parse_boot_argn("debug" , &boot_arg, sizeof (boot_arg))) |
| 71 | if (boot_arg & DB_KPRT) |
| 72 | new_disable_serial_output = FALSE; |
| 73 | |
| 74 | /* If we are newly enabling serial, make sure we only |
| 75 | * call pal_serial_init() if our previous state was |
| 76 | * not enabled */ |
| 77 | if (!new_disable_serial_output && (!disable_serial_output || pal_serial_init())) |
| 78 | PE_kputc = pal_serial_putc; |
| 79 | else |
| 80 | PE_kputc = cnputc; |
| 81 | |
| 82 | disable_serial_output = new_disable_serial_output; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | #if CONFIG_NO_KPRINTF_STRINGS |
| 87 | /* Prevent CPP from breaking the definition below */ |
| 88 | #undef kprintf |
| 89 | #endif |
| 90 | |
| 91 | #ifdef MP_DEBUG |
| 92 | static void _kprintf(const char *format, ...) |
| 93 | { |
| 94 | va_list listp; |
| 95 | |
| 96 | va_start(listp, format); |
| 97 | _doprnt(format, &listp, PE_kputc, 16); |
| 98 | va_end(listp); |
| 99 | } |
| 100 | #define MP_DEBUG_KPRINTF(x...) _kprintf(x) |
| 101 | #else /* MP_DEBUG */ |
| 102 | #define MP_DEBUG_KPRINTF(x...) |
| 103 | #endif /* MP_DEBUG */ |
| 104 | |
| 105 | static int cpu_last_locked = 0; |
| 106 | |
| 107 | __attribute__((noinline,not_tail_called)) |
| 108 | void kprintf(const char *fmt, ...) |
| 109 | { |
| 110 | va_list listp; |
| 111 | va_list listp2; |
| 112 | boolean_t state; |
| 113 | void *caller = __builtin_return_address(0); |
| 114 | |
| 115 | if (!disable_serial_output) { |
| 116 | boolean_t early = FALSE; |
| 117 | if (rdmsr64(MSR_IA32_GS_BASE) == 0) { |
| 118 | early = TRUE; |
| 119 | } |
| 120 | /* If PE_kputc has not yet been initialized, don't |
| 121 | * take any locks, just dump to serial */ |
| 122 | if (!PE_kputc || early) { |
| 123 | va_start(listp, fmt); |
| 124 | va_copy(listp2, listp); |
| 125 | |
| 126 | _doprnt_log(fmt, &listp, pal_serial_putc, 16); |
| 127 | va_end(listp); |
| 128 | |
| 129 | // If interrupts are enabled |
| 130 | if (ml_get_interrupts_enabled()) { |
| 131 | os_log_with_args(OS_LOG_DEFAULT, OS_LOG_TYPE_DEFAULT, fmt, listp2, caller); |
| 132 | } |
| 133 | va_end(listp2); |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | /* |
| 138 | * Spin to get kprintf lock but poll for incoming signals |
| 139 | * while interrupts are masked. |
| 140 | */ |
| 141 | state = ml_set_interrupts_enabled(FALSE); |
| 142 | |
| 143 | pal_preemption_assert(); |
| 144 | |
| 145 | while (!simple_lock_try(&kprintf_lock)) { |
| 146 | (void) cpu_signal_handler(NULL); |
| 147 | } |
| 148 | |
| 149 | if (cpu_number() != cpu_last_locked) { |
| 150 | MP_DEBUG_KPRINTF("[cpu%d...]\n" , cpu_number()); |
| 151 | cpu_last_locked = cpu_number(); |
| 152 | } |
| 153 | |
| 154 | va_start(listp, fmt); |
| 155 | va_copy(listp2, listp); |
| 156 | _doprnt(fmt, &listp, PE_kputc, 16); |
| 157 | va_end(listp); |
| 158 | |
| 159 | simple_unlock(&kprintf_lock); |
| 160 | ml_set_interrupts_enabled(state); |
| 161 | |
| 162 | // If interrupts are enabled |
| 163 | if (ml_get_interrupts_enabled()) { |
| 164 | os_log_with_args(OS_LOG_DEFAULT, OS_LOG_TYPE_DEFAULT, fmt, listp2, caller); |
| 165 | } |
| 166 | va_end(listp2); |
| 167 | |
| 168 | } |
| 169 | else { |
| 170 | if (ml_get_interrupts_enabled()) { |
| 171 | va_start(listp, fmt); |
| 172 | os_log_with_args(OS_LOG_DEFAULT, OS_LOG_TYPE_DEFAULT, fmt, listp, caller); |
| 173 | va_end(listp); |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | |
| 179 | |
| 180 | extern void kprintf_break_lock(void); |
| 181 | void |
| 182 | kprintf_break_lock(void) |
| 183 | { |
| 184 | simple_lock_init(&kprintf_lock, 0); |
| 185 | } |
| 186 | |