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