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 42/* Globals */ 43void (*PE_kputc)(char c); 44 45#if DEBUG 46/* DEBUG kernel starts with true serial, but 47 * may later disable or switch to video 48 * console */ 49unsigned int disable_serial_output = FALSE; 50#else 51unsigned int disable_serial_output = TRUE; 52#endif 53 54decl_simple_lock_data(static, kprintf_lock) 55 56void PE_init_kprintf(boolean_t vm_initialized) 57{ 58 unsigned int boot_arg; 59 60 if (PE_state.initialized == FALSE) 61 panic("Platform Expert not initialized"); 62 63 if (!vm_initialized) { 64 unsigned int new_disable_serial_output = TRUE; 65 66 simple_lock_init(&kprintf_lock, 0); 67 68 if (PE_parse_boot_argn("debug", &boot_arg, sizeof (boot_arg))) 69 if (boot_arg & DB_KPRT) 70 new_disable_serial_output = FALSE; 71 72 /* If we are newly enabling serial, make sure we only 73 * call pal_serial_init() if our previous state was 74 * not enabled */ 75 if (!new_disable_serial_output && (!disable_serial_output || pal_serial_init())) 76 PE_kputc = pal_serial_putc; 77 else 78 PE_kputc = cnputc; 79 80 disable_serial_output = new_disable_serial_output; 81 } 82} 83 84#if CONFIG_NO_KPRINTF_STRINGS 85/* Prevent CPP from breaking the definition below */ 86#undef kprintf 87#endif 88 89#ifdef MP_DEBUG 90static void _kprintf(const char *format, ...) 91{ 92 va_list listp; 93 94 va_start(listp, format); 95 _doprnt(format, &listp, PE_kputc, 16); 96 va_end(listp); 97} 98#define MP_DEBUG_KPRINTF(x...) _kprintf(x) 99#else /* MP_DEBUG */ 100#define MP_DEBUG_KPRINTF(x...) 101#endif /* MP_DEBUG */ 102 103static int cpu_last_locked = 0; 104void kprintf(const char *fmt, ...) 105{ 106 va_list listp; 107 boolean_t state; 108 109 if (!disable_serial_output) { 110 boolean_t early = FALSE; 111 if (rdmsr64(MSR_IA32_GS_BASE) == 0) { 112 early = TRUE; 113 } 114 /* If PE_kputc has not yet been initialized, don't 115 * take any locks, just dump to serial */ 116 if (!PE_kputc || early) { 117 va_start(listp, fmt); 118 _doprnt(fmt, &listp, pal_serial_putc, 16); 119 va_end(listp); 120 return; 121 } 122 123 /* 124 * Spin to get kprintf lock but poll for incoming signals 125 * while interrupts are masked. 126 */ 127 state = ml_set_interrupts_enabled(FALSE); 128 129 pal_preemption_assert(); 130 131 while (!simple_lock_try(&kprintf_lock)) { 132 (void) cpu_signal_handler(NULL); 133 } 134 135 if (cpu_number() != cpu_last_locked) { 136 MP_DEBUG_KPRINTF("[cpu%d...]\n", cpu_number()); 137 cpu_last_locked = cpu_number(); 138 } 139 140 va_start(listp, fmt); 141 _doprnt(fmt, &listp, PE_kputc, 16); 142 va_end(listp); 143 144 simple_unlock(&kprintf_lock); 145 ml_set_interrupts_enabled(state); 146 } 147} 148 149extern void kprintf_break_lock(void); 150void 151kprintf_break_lock(void) 152{ 153 simple_lock_init(&kprintf_lock, 0); 154} 155