xref: /xnu-12377.1.9/san/coverage/kcov_ksancov.h (revision f6217f891ac0bb64f3d375211650a4c1ff8ca1ea)
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 /*
69  * ioctl
70  */
71 
72 struct ksancov_buf_desc {
73 	uintptr_t ptr;  /* ptr to shared buffer [out] */
74 	size_t sz;      /* size of shared buffer [out] */
75 };
76 
77 /*
78  * On-demand related functionalities
79  */
80 typedef enum {
81 	KS_OD_GET_GATE = 1,
82 	KS_OD_SET_GATE = 2,
83 	KS_OD_GET_RANGE = 3,
84 	KS_OD_GET_BUNDLE = 4,
85 } ksancov_on_demand_operation_t;
86 
87 struct ksancov_on_demand_msg {
88 	char bundle[/*KMOD_MAX_NAME*/ 64];
89 	ksancov_on_demand_operation_t operation;
90 	union {
91 		uint64_t gate;
92 		struct {
93 			uint32_t start;
94 			uint32_t stop;
95 		} range;
96 		uint64_t pc;
97 	};
98 };
99 
100 /*
101  * shared kernel-user mapping
102  */
103 
104 #define KSANCOV_MAX_EDGES         (1 << 24)
105 #define KSANCOV_MAX_HITS          UINT8_MAX
106 #define KSANCOV_TRACE_MAGIC       (uint32_t)0x5AD17F5BU
107 #define KSANCOV_COUNTERS_MAGIC    (uint32_t)0x5AD27F6BU
108 #define KSANCOV_EDGEMAP_MAGIC     (uint32_t)0x5AD37F7BU
109 #define KSANCOV_STKSIZE_MAGIC     (uint32_t)0x5AD47F8BU
110 #define KSANCOV_CMPS_TRACE_MAGIC  (uint32_t)0x5AD47F9BU
111 
112 __BEGIN_DECLS
113 
114 void ksancov_init(void); /* invoked by kcov_init */
115 int ksancov_init_dev(void);
116 
117 void kcov_ksancov_init_thread(ksancov_dev_t *);
118 void kcov_ksancov_trace_guard(uint32_t *, void *);
119 void kcov_ksancov_trace_pc(kcov_thread_data_t *, uint32_t *, void*, uintptr_t);
120 void kcov_ksancov_trace_pc_guard_init(uint32_t *, uint32_t *);
121 void kcov_ksancov_pcs_init(uintptr_t *, uintptr_t *);
122 void kcov_ksancov_trace_cmp(kcov_thread_data_t *, uint32_t, uint64_t, uint64_t, void*);
123 void kcov_ksancov_trace_cmp_func(kcov_thread_data_t *, uint32_t, const void*, size_t, const void*, size_t, void*, bool);
124 bool kcov_ksancov_must_instrument(uintptr_t);
125 
126 __END_DECLS
127 
128 
129 #else
130 
131 #define kcov_ksancov_init_thread(dev)
132 #define kcov_ksancov_trace_guard(guardp, caller)
133 #define kcov_ksancov_trace_pc(dev, guardp, caller, sp)
134 #define kcov_ksancov_trace_pc_guard_init(start, stop)
135 #define kcov_ksancov_pcs_init(start, stop)
136 #define kcov_ksancov_trace_cmp(data, type, arg1, arg2, caller)
137 #define kcov_ksancov_trace_cmp_func(data, type, arg1, arg2, size, caller, always_log)
138 
139 #endif /* CONFIG_KSANCOV */
140 
141 #endif /* KERNEL_PRIVATE */
142 
143 #endif /* _KCOV_KSANCOV_H_ */
144