| 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/mp.h> |
| 38 | |
| 39 | /* Globals */ |
| 40 | void (*PE_kputc)(char c); |
| 41 | |
| 42 | unsigned int disable_serial_output = TRUE; |
| 43 | |
| 44 | decl_simple_lock_data(static, kprintf_lock) |
| 45 | |
| 46 | void PE_init_kprintf(boolean_t vm_initialized) |
| 47 | { |
| 48 | unsigned int boot_arg; |
| 49 | |
| 50 | if (PE_state.initialized == FALSE) |
| 51 | panic("Platform Expert not initialized"); |
| 52 | |
| 53 | if (!vm_initialized) { |
| 54 | simple_lock_init(&kprintf_lock, 0); |
| 55 | |
| 56 | if (PE_parse_boot_arg("debug", &boot_arg)) |
| 57 | if (boot_arg & DB_KPRT) |
| 58 | disable_serial_output = FALSE; |
| 59 | |
| 60 | if (!disable_serial_output && serial_init()) |
| 61 | PE_kputc = serial_putc; |
| 62 | else |
| 63 | PE_kputc = cnputc; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | #ifdef MP_DEBUG |
| 68 | static void _kprintf(const char *format, ...) |
| 69 | { |
| 70 | va_list listp; |
| 71 | |
| 72 | va_start(listp, format); |
| 73 | _doprnt(format, &listp, PE_kputc, 16); |
| 74 | va_end(listp); |
| 75 | } |
| 76 | #define MP_DEBUG_KPRINTF(x...) _kprintf(x) |
| 77 | #else /* MP_DEBUG */ |
| 78 | #define MP_DEBUG_KPRINTF(x...) |
| 79 | #endif /* MP_DEBUG */ |
| 80 | |
| 81 | static int cpu_last_locked = 0; |
| 82 | void kprintf(const char *fmt, ...) |
| 83 | { |
| 84 | va_list listp; |
| 85 | boolean_t state; |
| 86 | |
| 87 | if (!disable_serial_output) { |
| 88 | |
| 89 | /* |
| 90 | * Spin to get kprintf lock but re-enable interrupts while |
| 91 | * failing. |
| 92 | * This allows interrupts to be handled while waiting but |
| 93 | * interrupts are disabled once we have the lock. |
| 94 | */ |
| 95 | state = ml_set_interrupts_enabled(FALSE); |
| 96 | while (!simple_lock_try(&kprintf_lock)) { |
| 97 | ml_set_interrupts_enabled(state); |
| 98 | ml_set_interrupts_enabled(FALSE); |
| 99 | } |
| 100 | |
| 101 | if (cpu_number() != cpu_last_locked) { |
| 102 | MP_DEBUG_KPRINTF("[cpu%d...]\n", cpu_number()); |
| 103 | cpu_last_locked = cpu_number(); |
| 104 | } |
| 105 | |
| 106 | va_start(listp, fmt); |
| 107 | _doprnt(fmt, &listp, PE_kputc, 16); |
| 108 | va_end(listp); |
| 109 | |
| 110 | simple_unlock(&kprintf_lock); |
| 111 | ml_set_interrupts_enabled(state); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | extern void kprintf_break_lock(void); |
| 116 | void |
| 117 | kprintf_break_lock(void) |
| 118 | { |
| 119 | simple_lock_init(&kprintf_lock, 0); |
| 120 | } |