xref: /xnu-8792.61.2/osfmk/kern/iotrace.h (revision 42e220869062b56f8d7d0726fd4c88954f87902c)
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 #if CONFIG_IOTRACE
30 
31 #pragma once
32 
33 #include <kern/percpu.h>
34 #include <libkern/OSDebug.h>
35 #include <stdint.h>
36 
37 #define MAX_IOTRACE_BTFRAMES (16)
38 
39 typedef enum {
40 	IOTRACE_PHYS_READ = 1,
41 	IOTRACE_PHYS_WRITE,
42 	IOTRACE_IO_READ,
43 	IOTRACE_IO_WRITE,
44 	IOTRACE_PORTIO_READ,
45 	IOTRACE_PORTIO_WRITE
46 } iotrace_type_e;
47 
48 typedef struct {
49 	iotrace_type_e  iotype;
50 	int             size;
51 	uint64_t        vaddr;
52 	uint64_t        paddr;
53 	uint64_t        val;
54 	uint64_t        start_time_abs;
55 	uint64_t        duration;
56 	void           *backtrace[MAX_IOTRACE_BTFRAMES];
57 } iotrace_entry_t;
58 
59 extern volatile int mmiotrace_enabled;
60 extern uint32_t iotrace_entries_per_cpu;
61 
62 PERCPU_DECL(uint32_t, iotrace_next);
63 PERCPU_DECL(iotrace_entry_t * __unsafe_indexable, iotrace_ring);
64 
65 static inline void
iotrace(iotrace_type_e type,uint64_t vaddr,uint64_t paddr,int size,uint64_t val,uint64_t sabs,uint64_t duration)66 iotrace(iotrace_type_e type, uint64_t vaddr, uint64_t paddr, int size, uint64_t val,
67     uint64_t sabs, uint64_t duration)
68 {
69 	uint32_t nextidx;
70 	iotrace_entry_t *cur_iotrace_ring;
71 	uint32_t *nextidxp;
72 
73 	if (__improbable(mmiotrace_enabled == 0 ||
74 	    iotrace_entries_per_cpu == 0)) {
75 		return;
76 	}
77 
78 	nextidxp = PERCPU_GET(iotrace_next);
79 	nextidx = *nextidxp;
80 	cur_iotrace_ring = *PERCPU_GET(iotrace_ring);
81 
82 	cur_iotrace_ring[nextidx].iotype = type;
83 	cur_iotrace_ring[nextidx].vaddr = vaddr;
84 	cur_iotrace_ring[nextidx].paddr = paddr;
85 	cur_iotrace_ring[nextidx].size = size;
86 	cur_iotrace_ring[nextidx].val = val;
87 	cur_iotrace_ring[nextidx].start_time_abs = sabs;
88 	cur_iotrace_ring[nextidx].duration = duration;
89 
90 	*nextidxp = ((nextidx + 1) >= iotrace_entries_per_cpu) ? 0 : (nextidx + 1);
91 
92 	(void) OSBacktrace(cur_iotrace_ring[nextidx].backtrace,
93 	    MAX_IOTRACE_BTFRAMES);
94 }
95 
96 static inline void
iotrace_disable(void)97 iotrace_disable(void)
98 {
99 	mmiotrace_enabled = 0;
100 }
101 
102 #else /* CONFIG_IOTRACE */
103 
104 #define iotrace_disable()
105 #define iotrace(type, vaddr, paddr, size, val, sabs, duration)
106 
107 #endif /* CONFIG_IOTRACE */
108