xref: /xnu-10002.61.3/san/coverage/kcov_ksancov_data.h (revision 0f4c859e951fba394238ab619495c4e1d54d0f34)
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  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
26  */
27 #ifndef _KCOV_KSANCOV_DATA_H_
28 #define _KCOV_KSANCOV_DATA_H_
29 
30 #if KERNEL_PRIVATE
31 
32 #if CONFIG_KSANCOV
33 
34 /*
35  * Supported coverage modes.
36  */
37 typedef enum {
38 	KS_MODE_NONE,
39 	KS_MODE_TRACE,
40 	KS_MODE_COUNTERS,
41 	KS_MODE_STKSIZE,
42 	KS_MODE_MAX
43 } ksancov_mode_t;
44 
45 /*
46  * A header that is always present in every ksancov mode shared memory structure.
47  */
48 typedef struct ksancov_header {
49 	uint32_t         kh_magic;
50 	_Atomic uint32_t kh_enabled;
51 } ksancov_header_t;
52 
53 /*
54  * TRACE mode data structure.
55  */
56 
57 /*
58  * All trace based tools share this structure.
59  */
60 typedef struct ksancov_trace {
61 	ksancov_header_t kt_hdr;         /* header (must be always first) */
62 	uint32_t         kt_maxent;      /* Maximum entries in this shared buffer. */
63 	_Atomic uint32_t kt_head;        /* Pointer to the first unused element. */
64 	uint64_t         kt_entries[];   /* Trace entries in this buffer. */
65 } ksancov_trace_t;
66 
67 /* PC tracing only records PCs */
68 typedef uintptr_t ksancov_trace_pc_ent_t;
69 
70 /* STKSIZE tracing records PCs and stack size. */
71 typedef struct ksancov_trace_stksize_entry {
72 	uintptr_t pc;                      /* PC */
73 	uint32_t  stksize;                 /* associated stack size */
74 } ksancov_trace_stksize_ent_t;
75 
76 /*
77  * COUNTERS mode data structure.
78  */
79 typedef struct ksancov_counters {
80 	ksancov_header_t kc_hdr;
81 	uint32_t         kc_nedges;       /* total number of edges */
82 	uint8_t          kc_hits[];       /* hits on each edge (8bit saturating) */
83 } ksancov_counters_t;
84 
85 /*
86  * Edge to PC mapping.
87  */
88 typedef struct ksancov_edgemap {
89 	uint32_t  ke_magic;
90 	uint32_t  ke_nedges;
91 	uintptr_t ke_addrs[];             /* address of each edge relative to 'offset' */
92 } ksancov_edgemap_t;
93 
94 /*
95  * Represents state of a ksancov device when userspace asks for coverage data recording.
96  */
97 
98 struct ksancov_dev {
99 	ksancov_mode_t mode;
100 
101 	union {
102 		ksancov_header_t       *hdr;
103 		ksancov_trace_t        *trace;
104 		ksancov_counters_t     *counters;
105 	};
106 	size_t sz;     /* size of allocated trace/counters buffer */
107 
108 	size_t maxpcs;
109 
110 	thread_t thread;
111 	dev_t dev;
112 	lck_mtx_t lock;
113 };
114 typedef struct ksancov_dev * ksancov_dev_t;
115 
116 
117 #endif /* CONFIG_KSANCOV */
118 
119 #endif /* KERNEL_PRIVATE */
120 
121 #endif /* _KCOV_KSANCOV_DATA_H_ */
122