xref: /xnu-12377.81.4/san/coverage/kcov_ksancov.h (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
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_H_
28 #define _KCOV_KSANCOV_H_
29 
30 #include <stdint.h>
31 #include <sys/ioccom.h>
32 #include <san/kcov_data.h>
33 
34 #if KERNEL_PRIVATE
35 
36 #if CONFIG_KSANCOV
37 
38 
39 #define KSANCOV_DEVNODE "ksancov"
40 #define KSANCOV_PATH "/dev/" KSANCOV_DEVNODE
41 
42 /* Set mode */
43 #define KSANCOV_IOC_TRACE            _IOW('K', 1, size_t) /* number of pcs */
44 #define KSANCOV_IOC_COUNTERS         _IO('K', 2)
45 #define KSANCOV_IOC_STKSIZE          _IOW('K', 3, size_t) /* number of pcs */
46 
47 /* Establish a shared mapping of the coverage buffer. */
48 #define KSANCOV_IOC_MAP              _IOWR('K', 8, struct ksancov_buf_desc)
49 
50 /* Establish a shared mapping of the edge address buffer. */
51 #define KSANCOV_IOC_MAP_EDGEMAP      _IOWR('K', 9, struct ksancov_buf_desc)
52 
53 /* Log the current thread */
54 #define KSANCOV_IOC_START            _IOW('K', 10, uintptr_t)
55 #define KSANCOV_IOC_NEDGES           _IOR('K', 50, size_t)
56 #define KSANCOV_IOC_TESTPANIC        _IOW('K', 20, uint64_t)
57 
58 /* Operations related to on-demand instrumentation */
59 #define KSANCOV_IOC_ON_DEMAND        _IOWR('K', 60, struct ksancov_on_demand_msg)
60 
61 /* Set comparison log mode */
62 #define KSANCOV_IOC_CMPS_TRACE       _IOW('K', 70, size_t) /* number of cmps */
63 #define KSANCOV_IOC_CMPS_TRACE_FUNC  _IOW('K', 71, size_t) /* number of cmps */
64 
65 /* Establish a shared mapping of the comparisons buffer. */
66 #define KSANCOV_IOC_CMPS_MAP         _IOWR('K', 90, struct ksancov_buf_desc)
67 
68 /* Testcases buffer */
69 #define KSANCOV_IOC_TESTCASES       _IOW('K', 100, size_t) /* number of testcases */
70 #define KSANCOV_IOC_TESTCASES_MAP   _IOWR('K', 101, struct ksancov_buf_desc)
71 #define KSANCOV_IOC_TESTCASES_LOG   _IO('K', 102)
72 
73 
74 /*
75  * ioctl
76  */
77 
78 struct ksancov_buf_desc {
79 	uintptr_t ptr;  /* ptr to shared buffer [out] */
80 	size_t sz;      /* size of shared buffer [out] */
81 };
82 
83 /*
84  * On-demand related functionalities
85  */
86 typedef enum {
87 	KS_OD_GET_GATE = 1,
88 	KS_OD_SET_GATE = 2,
89 	KS_OD_GET_RANGE = 3,
90 	KS_OD_GET_BUNDLE = 4,
91 } ksancov_on_demand_operation_t;
92 
93 struct ksancov_on_demand_msg {
94 	char bundle[/*KMOD_MAX_NAME*/ 64];
95 	ksancov_on_demand_operation_t operation;
96 	union {
97 		uint64_t gate;
98 		struct {
99 			uint32_t start;
100 			uint32_t stop;
101 		} range;
102 		uint64_t pc;
103 	};
104 };
105 
106 /*
107  * shared kernel-user mapping
108  */
109 
110 #define KSANCOV_MAX_EDGES         (1 << 24)
111 #define KSANCOV_MAX_HITS          UINT8_MAX
112 #define KSANCOV_TRACE_MAGIC       (uint32_t)0x5AD17F5BU
113 #define KSANCOV_COUNTERS_MAGIC    (uint32_t)0x5AD27F6BU
114 #define KSANCOV_EDGEMAP_MAGIC     (uint32_t)0x5AD37F7BU
115 #define KSANCOV_STKSIZE_MAGIC     (uint32_t)0x5AD47F8BU
116 #define KSANCOV_CMPS_TRACE_MAGIC  (uint32_t)0x5AD47F9BU
117 
118 __BEGIN_DECLS
119 
120 void ksancov_init(void); /* invoked by kcov_init */
121 int ksancov_init_dev(void);
122 
123 void kcov_ksancov_init_thread(ksancov_dev_t *);
124 void kcov_ksancov_trace_guard(uint32_t *, void *);
125 void kcov_ksancov_trace_pc(kcov_thread_data_t *, uint32_t *, void*, uintptr_t);
126 void kcov_ksancov_trace_pc_guard_init(uint32_t *, uint32_t *);
127 void kcov_ksancov_pcs_init(uintptr_t *, uintptr_t *);
128 void kcov_ksancov_trace_cmp(kcov_thread_data_t *, uint32_t, uint64_t, uint64_t, void*);
129 void kcov_ksancov_trace_cmp_func(kcov_thread_data_t *, uint32_t, const void*, size_t, const void*, size_t, void*, bool);
130 bool kcov_ksancov_must_instrument(uintptr_t);
131 void ksancov_on_panic_log(void);
132 
133 __END_DECLS
134 
135 
136 #else
137 
138 #define kcov_ksancov_init_thread(dev)
139 #define kcov_ksancov_trace_guard(guardp, caller)
140 #define kcov_ksancov_trace_pc(dev, guardp, caller, sp)
141 #define kcov_ksancov_trace_pc_guard_init(start, stop)
142 #define kcov_ksancov_pcs_init(start, stop)
143 #define kcov_ksancov_trace_cmp(data, type, arg1, arg2, caller)
144 #define kcov_ksancov_trace_cmp_func(data, type, arg1, arg2, size, caller, always_log)
145 #define kcov_ksancov_must_instrument(addr)
146 #define ksancov_on_panic_log()
147 
148 #endif /* CONFIG_KSANCOV */
149 
150 #endif /* KERNEL_PRIVATE */
151 
152 #endif /* _KCOV_KSANCOV_H_ */
153