1 /*
2 * Copyright (c) 2021 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
29 #define __APPLE_API_PRIVATE 1
30 #define __APPLE_API_UNSTABLE 1
31
32 #include <kern/debug.h>
33 #include <kern/iotrace.h>
34 #include <kern/zalloc.h>
35
36 #include <pexpert/pexpert.h>
37
38 #define DEFAULT_IOTRACE_ENTRIES_PER_CPU (186)
39 #define IOTRACE_MAX_ENTRIES_PER_CPU (1024)
40
41 volatile int mmiotrace_enabled = 1;
42 uint32_t iotrace_entries_per_cpu;
43
44 uint32_t PERCPU_DATA(iotrace_next);
45 iotrace_entry_t *PERCPU_DATA(iotrace_ring);
46
47 static void
init_iotrace_bufs(int entries_per_cpu)48 init_iotrace_bufs(int entries_per_cpu)
49 {
50 const size_t size = entries_per_cpu * sizeof(iotrace_entry_t);
51
52 percpu_foreach(ring, iotrace_ring) {
53 *ring = zalloc_permanent_tag(size, ZALIGN(iotrace_entry_t),
54 VM_KERN_MEMORY_DIAG);
55 };
56
57 iotrace_entries_per_cpu = entries_per_cpu;
58 }
59
60 __startup_func
61 static void
iotrace_init(void)62 iotrace_init(void)
63 {
64 int entries_per_cpu = DEFAULT_IOTRACE_ENTRIES_PER_CPU;
65 int enable = mmiotrace_enabled;
66
67 if (kern_feature_override(KF_IOTRACE_OVRD)) {
68 enable = 0;
69 }
70
71 (void) PE_parse_boot_argn("iotrace", &enable, sizeof(enable));
72 if (enable != 0 &&
73 PE_parse_boot_argn("iotrace_epc", &entries_per_cpu, sizeof(entries_per_cpu)) &&
74 (entries_per_cpu < 1 || entries_per_cpu > IOTRACE_MAX_ENTRIES_PER_CPU)) {
75 entries_per_cpu = DEFAULT_IOTRACE_ENTRIES_PER_CPU;
76 }
77
78 mmiotrace_enabled = enable;
79
80 if (mmiotrace_enabled) {
81 init_iotrace_bufs(entries_per_cpu);
82 }
83 }
84
85 STARTUP(EARLY_BOOT, STARTUP_RANK_MIDDLE, iotrace_init);
86