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