xref: /xnu-8796.141.3/san/coverage/kcov_ksancov.h (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
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 /*
59  * ioctl
60  */
61 
62 struct ksancov_buf_desc {
63 	uintptr_t ptr;  /* ptr to shared buffer [out] */
64 	size_t sz;      /* size of shared buffer [out] */
65 };
66 
67 /*
68  * shared kernel-user mapping
69  */
70 
71 #define KSANCOV_MAX_EDGES       512UL*1024
72 #define KSANCOV_MAX_HITS        UINT8_MAX
73 #define KSANCOV_TRACE_MAGIC     (uint32_t)0x5AD17F5BU
74 #define KSANCOV_COUNTERS_MAGIC  (uint32_t)0x5AD27F6BU
75 #define KSANCOV_EDGEMAP_MAGIC   (uint32_t)0x5AD37F7BU
76 #define KSANCOV_STKSIZE_MAGIC   (uint32_t)0x5AD47F8BU
77 
78 
79 __BEGIN_DECLS
80 
81 int ksancov_init_dev(void);
82 
83 void kcov_ksancov_init_thread(ksancov_dev_t *);
84 void kcov_ksancov_trace_guard(uint32_t *, void *);
85 void kcov_ksancov_trace_pc(kcov_thread_data_t *, uint32_t *, void*, uintptr_t);
86 void kcov_ksancov_trace_pc_guard_init(uint32_t *, uint32_t *);
87 void kcov_ksancov_pcs_init(uintptr_t *, uintptr_t *);
88 
89 __END_DECLS
90 
91 
92 #else
93 
94 #define kcov_ksancov_init_thread(dev)
95 #define kcov_ksancov_trace_guard(guardp, caller)
96 #define kcov_ksancov_trace_pc(dev, guardp, caller, sp)
97 #define kcov_ksancov_trace_pc_guard_init(start, stop)
98 #define kcov_ksancov_pcs_init(start, stop)
99 
100 #endif /* CONFIG_KSANCOV */
101 
102 #endif /* KERNEL_PRIVATE */
103 
104 #endif /* _KCOV_KSANCOV_H_ */
105