ecc_logging.c
1 /* 2 * Copyright (c) 2013 Apple 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 #include <mach/mach_types.h> 29 #include <mach/host_info.h> 30 #include <kern/locks.h> 31 #include <kern/ecc.h> 32 #include <kern/spl.h> 33 #include <pexpert/pexpert.h> 34 #include <libkern/OSAtomic.h> 35 36 /* 37 * ECC data. Not really KPCs, but this still seems like the 38 * best home for this code. 39 * 40 * Circular buffer of events. When we fill up, drop data. 41 */ 42 #define ECC_EVENT_BUFFER_COUNT 5 43 struct ecc_event ecc_data[ECC_EVENT_BUFFER_COUNT]; 44 static uint32_t ecc_data_next_read; 45 static uint32_t ecc_data_next_write; 46 static boolean_t ecc_data_empty = TRUE; // next read == next write : empty or full? 47 static LCK_GRP_DECLARE(ecc_data_lock_group, "ecc-data"); 48 static LCK_SPIN_DECLARE(ecc_data_lock, &ecc_data_lock_group); 49 static uint32_t ecc_correction_count; 50 51 uint32_t 52 ecc_log_get_correction_count() 53 { 54 return ecc_correction_count; 55 } 56 57 kern_return_t 58 ecc_log_record_event(const struct ecc_event *ev) 59 { 60 spl_t x; 61 62 if (ev->count > ECC_EVENT_INFO_DATA_ENTRIES) { 63 panic("Count of %u on ecc event is too large.", (unsigned)ev->count); 64 } 65 66 x = splhigh(); 67 lck_spin_lock(&ecc_data_lock); 68 69 ecc_correction_count++; 70 71 if (ecc_data_next_read == ecc_data_next_write && !ecc_data_empty) { 72 lck_spin_unlock(&ecc_data_lock); 73 splx(x); 74 return KERN_FAILURE; 75 } 76 77 bcopy(ev, &ecc_data[ecc_data_next_write], sizeof(*ev)); 78 ecc_data_next_write++; 79 ecc_data_next_write %= ECC_EVENT_BUFFER_COUNT; 80 ecc_data_empty = FALSE; 81 82 lck_spin_unlock(&ecc_data_lock); 83 splx(x); 84 85 return KERN_SUCCESS; 86 } 87 88 89 kern_return_t 90 ecc_log_get_next_event(struct ecc_event *ev) 91 { 92 spl_t x; 93 94 x = splhigh(); 95 lck_spin_lock(&ecc_data_lock); 96 97 if (ecc_data_empty) { 98 assert(ecc_data_next_write == ecc_data_next_read); 99 100 lck_spin_unlock(&ecc_data_lock); 101 splx(x); 102 return KERN_FAILURE; 103 } 104 105 bcopy(&ecc_data[ecc_data_next_read], ev, sizeof(*ev)); 106 ecc_data_next_read++; 107 ecc_data_next_read %= ECC_EVENT_BUFFER_COUNT; 108 109 if (ecc_data_next_read == ecc_data_next_write) { 110 ecc_data_empty = TRUE; 111 } 112 113 lck_spin_unlock(&ecc_data_lock); 114 splx(x); 115 116 return KERN_SUCCESS; 117 }