xref: /xnu-10063.121.3/san/coverage/kcov_ksancov_data.h (revision 2c2f96dc2b9a4408a43d3150ae9c105355ca3daa)
1*2c2f96dcSApple OSS Distributions /*
2*2c2f96dcSApple OSS Distributions  * Copyright (c) 2021 Apple Inc. All rights reserved.
3*2c2f96dcSApple OSS Distributions  *
4*2c2f96dcSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*2c2f96dcSApple OSS Distributions  *
6*2c2f96dcSApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*2c2f96dcSApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*2c2f96dcSApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*2c2f96dcSApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*2c2f96dcSApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*2c2f96dcSApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*2c2f96dcSApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*2c2f96dcSApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*2c2f96dcSApple OSS Distributions  *
15*2c2f96dcSApple OSS Distributions  * Please obtain a copy of the License at
16*2c2f96dcSApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*2c2f96dcSApple OSS Distributions  *
18*2c2f96dcSApple OSS Distributions  * The Original Code and all software distributed under the License are
19*2c2f96dcSApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*2c2f96dcSApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*2c2f96dcSApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*2c2f96dcSApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*2c2f96dcSApple OSS Distributions  * Please see the License for the specific language governing rights and
24*2c2f96dcSApple OSS Distributions  * limitations under the License.
25*2c2f96dcSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
26*2c2f96dcSApple OSS Distributions  */
27*2c2f96dcSApple OSS Distributions #ifndef _KCOV_KSANCOV_DATA_H_
28*2c2f96dcSApple OSS Distributions #define _KCOV_KSANCOV_DATA_H_
29*2c2f96dcSApple OSS Distributions 
30*2c2f96dcSApple OSS Distributions #if KERNEL_PRIVATE
31*2c2f96dcSApple OSS Distributions 
32*2c2f96dcSApple OSS Distributions #if CONFIG_KSANCOV
33*2c2f96dcSApple OSS Distributions 
34*2c2f96dcSApple OSS Distributions /*
35*2c2f96dcSApple OSS Distributions  * Supported coverage modes.
36*2c2f96dcSApple OSS Distributions  */
37*2c2f96dcSApple OSS Distributions typedef enum {
38*2c2f96dcSApple OSS Distributions 	KS_MODE_NONE,
39*2c2f96dcSApple OSS Distributions 	KS_MODE_TRACE,
40*2c2f96dcSApple OSS Distributions 	KS_MODE_COUNTERS,
41*2c2f96dcSApple OSS Distributions 	KS_MODE_STKSIZE,
42*2c2f96dcSApple OSS Distributions 	KS_MODE_MAX
43*2c2f96dcSApple OSS Distributions } ksancov_mode_t;
44*2c2f96dcSApple OSS Distributions 
45*2c2f96dcSApple OSS Distributions /*
46*2c2f96dcSApple OSS Distributions  * A header that is always present in every ksancov mode shared memory structure.
47*2c2f96dcSApple OSS Distributions  */
48*2c2f96dcSApple OSS Distributions typedef struct ksancov_header {
49*2c2f96dcSApple OSS Distributions 	uint32_t         kh_magic;
50*2c2f96dcSApple OSS Distributions 	_Atomic uint32_t kh_enabled;
51*2c2f96dcSApple OSS Distributions } ksancov_header_t;
52*2c2f96dcSApple OSS Distributions 
53*2c2f96dcSApple OSS Distributions /*
54*2c2f96dcSApple OSS Distributions  * TRACE mode data structure.
55*2c2f96dcSApple OSS Distributions  */
56*2c2f96dcSApple OSS Distributions 
57*2c2f96dcSApple OSS Distributions /*
58*2c2f96dcSApple OSS Distributions  * All trace based tools share this structure.
59*2c2f96dcSApple OSS Distributions  */
60*2c2f96dcSApple OSS Distributions typedef struct ksancov_trace {
61*2c2f96dcSApple OSS Distributions 	ksancov_header_t kt_hdr;         /* header (must be always first) */
62*2c2f96dcSApple OSS Distributions 	uint32_t         kt_maxent;      /* Maximum entries in this shared buffer. */
63*2c2f96dcSApple OSS Distributions 	_Atomic uint32_t kt_head;        /* Pointer to the first unused element. */
64*2c2f96dcSApple OSS Distributions 	uint64_t         kt_entries[];   /* Trace entries in this buffer. */
65*2c2f96dcSApple OSS Distributions } ksancov_trace_t;
66*2c2f96dcSApple OSS Distributions 
67*2c2f96dcSApple OSS Distributions /* PC tracing only records PCs */
68*2c2f96dcSApple OSS Distributions typedef uintptr_t ksancov_trace_pc_ent_t;
69*2c2f96dcSApple OSS Distributions 
70*2c2f96dcSApple OSS Distributions /* STKSIZE tracing records PCs and stack size. */
71*2c2f96dcSApple OSS Distributions typedef struct ksancov_trace_stksize_entry {
72*2c2f96dcSApple OSS Distributions 	uintptr_t pc;                      /* PC */
73*2c2f96dcSApple OSS Distributions 	uint32_t  stksize;                 /* associated stack size */
74*2c2f96dcSApple OSS Distributions } ksancov_trace_stksize_ent_t;
75*2c2f96dcSApple OSS Distributions 
76*2c2f96dcSApple OSS Distributions /*
77*2c2f96dcSApple OSS Distributions  * COUNTERS mode data structure.
78*2c2f96dcSApple OSS Distributions  */
79*2c2f96dcSApple OSS Distributions typedef struct ksancov_counters {
80*2c2f96dcSApple OSS Distributions 	ksancov_header_t kc_hdr;
81*2c2f96dcSApple OSS Distributions 	uint32_t         kc_nedges;       /* total number of edges */
82*2c2f96dcSApple OSS Distributions 	uint8_t          kc_hits[];       /* hits on each edge (8bit saturating) */
83*2c2f96dcSApple OSS Distributions } ksancov_counters_t;
84*2c2f96dcSApple OSS Distributions 
85*2c2f96dcSApple OSS Distributions /*
86*2c2f96dcSApple OSS Distributions  * Edge to PC mapping.
87*2c2f96dcSApple OSS Distributions  */
88*2c2f96dcSApple OSS Distributions typedef struct ksancov_edgemap {
89*2c2f96dcSApple OSS Distributions 	uint32_t  ke_magic;
90*2c2f96dcSApple OSS Distributions 	uint32_t  ke_nedges;
91*2c2f96dcSApple OSS Distributions 	uintptr_t ke_addrs[];             /* address of each edge relative to 'offset' */
92*2c2f96dcSApple OSS Distributions } ksancov_edgemap_t;
93*2c2f96dcSApple OSS Distributions 
94*2c2f96dcSApple OSS Distributions /*
95*2c2f96dcSApple OSS Distributions  * Represents state of a ksancov device when userspace asks for coverage data recording.
96*2c2f96dcSApple OSS Distributions  */
97*2c2f96dcSApple OSS Distributions 
98*2c2f96dcSApple OSS Distributions struct ksancov_dev {
99*2c2f96dcSApple OSS Distributions 	ksancov_mode_t mode;
100*2c2f96dcSApple OSS Distributions 
101*2c2f96dcSApple OSS Distributions 	union {
102*2c2f96dcSApple OSS Distributions 		ksancov_header_t       *hdr;
103*2c2f96dcSApple OSS Distributions 		ksancov_trace_t        *trace;
104*2c2f96dcSApple OSS Distributions 		ksancov_counters_t     *counters;
105*2c2f96dcSApple OSS Distributions 	};
106*2c2f96dcSApple OSS Distributions 	size_t sz;     /* size of allocated trace/counters buffer */
107*2c2f96dcSApple OSS Distributions 
108*2c2f96dcSApple OSS Distributions 	size_t maxpcs;
109*2c2f96dcSApple OSS Distributions 
110*2c2f96dcSApple OSS Distributions 	thread_t thread;
111*2c2f96dcSApple OSS Distributions 	dev_t dev;
112*2c2f96dcSApple OSS Distributions 	lck_mtx_t lock;
113*2c2f96dcSApple OSS Distributions };
114*2c2f96dcSApple OSS Distributions typedef struct ksancov_dev * ksancov_dev_t;
115*2c2f96dcSApple OSS Distributions 
116*2c2f96dcSApple OSS Distributions 
117*2c2f96dcSApple OSS Distributions #endif /* CONFIG_KSANCOV */
118*2c2f96dcSApple OSS Distributions 
119*2c2f96dcSApple OSS Distributions #endif /* KERNEL_PRIVATE */
120*2c2f96dcSApple OSS Distributions 
121*2c2f96dcSApple OSS Distributions #endif /* _KCOV_KSANCOV_DATA_H_ */
122