| 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | #ifndef _LINUX_KCOV_H |
| 3 | #define _LINUX_KCOV_H |
| 4 | |
| 5 | #include <linux/sched.h> |
| 6 | #include <uapi/linux/kcov.h> |
| 7 | |
| 8 | struct task_struct; |
| 9 | |
| 10 | #ifdef CONFIG_KCOV |
| 11 | |
| 12 | enum kcov_mode { |
| 13 | /* Coverage collection is not enabled yet. */ |
| 14 | KCOV_MODE_DISABLED = 0, |
| 15 | /* KCOV was initialized, but tracing mode hasn't been chosen yet. */ |
| 16 | KCOV_MODE_INIT = 1, |
| 17 | /* |
| 18 | * Tracing coverage collection mode. |
| 19 | * Covered PCs are collected in a per-task buffer. |
| 20 | */ |
| 21 | KCOV_MODE_TRACE_PC = 2, |
| 22 | /* Collecting comparison operands mode. */ |
| 23 | KCOV_MODE_TRACE_CMP = 3, |
| 24 | /* The process owns a KCOV remote reference. */ |
| 25 | KCOV_MODE_REMOTE = 4, |
| 26 | }; |
| 27 | |
| 28 | #define KCOV_IN_CTXSW (1 << 30) |
| 29 | |
| 30 | void kcov_task_init(struct task_struct *t); |
| 31 | void kcov_task_exit(struct task_struct *t); |
| 32 | |
| 33 | #define kcov_prepare_switch(t) \ |
| 34 | do { \ |
| 35 | (t)->kcov_mode |= KCOV_IN_CTXSW; \ |
| 36 | } while (0) |
| 37 | |
| 38 | #define kcov_finish_switch(t) \ |
| 39 | do { \ |
| 40 | (t)->kcov_mode &= ~KCOV_IN_CTXSW; \ |
| 41 | } while (0) |
| 42 | |
| 43 | /* See Documentation/dev-tools/kcov.rst for usage details. */ |
| 44 | void kcov_remote_start(u64 handle); |
| 45 | void kcov_remote_stop(void); |
| 46 | u64 kcov_common_handle(void); |
| 47 | |
| 48 | static inline void kcov_remote_start_common(u64 id) |
| 49 | { |
| 50 | kcov_remote_start(handle: kcov_remote_handle(KCOV_SUBSYSTEM_COMMON, inst: id)); |
| 51 | } |
| 52 | |
| 53 | static inline void kcov_remote_start_usb(u64 id) |
| 54 | { |
| 55 | kcov_remote_start(handle: kcov_remote_handle(KCOV_SUBSYSTEM_USB, inst: id)); |
| 56 | } |
| 57 | |
| 58 | /* |
| 59 | * The softirq flavor of kcov_remote_*() functions is introduced as a temporary |
| 60 | * work around for kcov's lack of nested remote coverage sections support in |
| 61 | * task context. Adding support for nested sections is tracked in: |
| 62 | * https://bugzilla.kernel.org/show_bug.cgi?id=210337 |
| 63 | */ |
| 64 | |
| 65 | static inline void kcov_remote_start_usb_softirq(u64 id) |
| 66 | { |
| 67 | if (in_serving_softirq() && !in_hardirq()) |
| 68 | kcov_remote_start_usb(id); |
| 69 | } |
| 70 | |
| 71 | static inline void kcov_remote_stop_softirq(void) |
| 72 | { |
| 73 | if (in_serving_softirq() && !in_hardirq()) |
| 74 | kcov_remote_stop(); |
| 75 | } |
| 76 | |
| 77 | #ifdef CONFIG_64BIT |
| 78 | typedef unsigned long kcov_u64; |
| 79 | #else |
| 80 | typedef unsigned long long kcov_u64; |
| 81 | #endif |
| 82 | |
| 83 | void __sanitizer_cov_trace_pc(void); |
| 84 | void __sanitizer_cov_trace_cmp1(u8 arg1, u8 arg2); |
| 85 | void __sanitizer_cov_trace_cmp2(u16 arg1, u16 arg2); |
| 86 | void __sanitizer_cov_trace_cmp4(u32 arg1, u32 arg2); |
| 87 | void __sanitizer_cov_trace_cmp8(kcov_u64 arg1, kcov_u64 arg2); |
| 88 | void __sanitizer_cov_trace_const_cmp1(u8 arg1, u8 arg2); |
| 89 | void __sanitizer_cov_trace_const_cmp2(u16 arg1, u16 arg2); |
| 90 | void __sanitizer_cov_trace_const_cmp4(u32 arg1, u32 arg2); |
| 91 | void __sanitizer_cov_trace_const_cmp8(kcov_u64 arg1, kcov_u64 arg2); |
| 92 | void __sanitizer_cov_trace_switch(kcov_u64 val, void *cases); |
| 93 | |
| 94 | #else |
| 95 | |
| 96 | static inline void kcov_task_init(struct task_struct *t) {} |
| 97 | static inline void kcov_task_exit(struct task_struct *t) {} |
| 98 | static inline void kcov_prepare_switch(struct task_struct *t) {} |
| 99 | static inline void kcov_finish_switch(struct task_struct *t) {} |
| 100 | static inline void kcov_remote_start(u64 handle) {} |
| 101 | static inline void kcov_remote_stop(void) {} |
| 102 | static inline u64 kcov_common_handle(void) |
| 103 | { |
| 104 | return 0; |
| 105 | } |
| 106 | static inline void kcov_remote_start_common(u64 id) {} |
| 107 | static inline void kcov_remote_start_usb(u64 id) {} |
| 108 | static inline void kcov_remote_start_usb_softirq(u64 id) {} |
| 109 | static inline void kcov_remote_stop_softirq(void) {} |
| 110 | |
| 111 | #endif /* CONFIG_KCOV */ |
| 112 | #endif /* _LINUX_KCOV_H */ |
| 113 | |