xref: /openbmc/linux/include/linux/kcov.h (revision 57904291176fa16a981cefca5cbe1a0b50196792)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
25c9a8750SDmitry Vyukov #ifndef _LINUX_KCOV_H
35c9a8750SDmitry Vyukov #define _LINUX_KCOV_H
45c9a8750SDmitry Vyukov 
5183f47fcSSebastian Andrzej Siewior #include <linux/sched.h>
65c9a8750SDmitry Vyukov #include <uapi/linux/kcov.h>
75c9a8750SDmitry Vyukov 
85c9a8750SDmitry Vyukov struct task_struct;
95c9a8750SDmitry Vyukov 
105c9a8750SDmitry Vyukov #ifdef CONFIG_KCOV
115c9a8750SDmitry Vyukov 
125c9a8750SDmitry Vyukov enum kcov_mode {
135c9a8750SDmitry Vyukov 	/* Coverage collection is not enabled yet. */
145c9a8750SDmitry Vyukov 	KCOV_MODE_DISABLED = 0,
15ded97d2cSVictor Chibotaru 	/* KCOV was initialized, but tracing mode hasn't been chosen yet. */
16ded97d2cSVictor Chibotaru 	KCOV_MODE_INIT = 1,
175c9a8750SDmitry Vyukov 	/*
185c9a8750SDmitry Vyukov 	 * Tracing coverage collection mode.
195c9a8750SDmitry Vyukov 	 * Covered PCs are collected in a per-task buffer.
205c9a8750SDmitry Vyukov 	 */
21ded97d2cSVictor Chibotaru 	KCOV_MODE_TRACE_PC = 2,
22ded97d2cSVictor Chibotaru 	/* Collecting comparison operands mode. */
23ded97d2cSVictor Chibotaru 	KCOV_MODE_TRACE_CMP = 3,
24*637619b0SAleksandr Nogikh 	/* The process owns a KCOV remote reference. */
25*637619b0SAleksandr Nogikh 	KCOV_MODE_REMOTE = 4,
265c9a8750SDmitry Vyukov };
275c9a8750SDmitry Vyukov 
280ed557aaSMark Rutland #define KCOV_IN_CTXSW	(1 << 30)
290ed557aaSMark Rutland 
30ded97d2cSVictor Chibotaru void kcov_task_init(struct task_struct *t);
31ded97d2cSVictor Chibotaru void kcov_task_exit(struct task_struct *t);
32ded97d2cSVictor Chibotaru 
330ed557aaSMark Rutland #define kcov_prepare_switch(t)			\
340ed557aaSMark Rutland do {						\
350ed557aaSMark Rutland 	(t)->kcov_mode |= KCOV_IN_CTXSW;	\
360ed557aaSMark Rutland } while (0)
370ed557aaSMark Rutland 
380ed557aaSMark Rutland #define kcov_finish_switch(t)			\
390ed557aaSMark Rutland do {						\
400ed557aaSMark Rutland 	(t)->kcov_mode &= ~KCOV_IN_CTXSW;	\
410ed557aaSMark Rutland } while (0)
420ed557aaSMark Rutland 
43eec028c9SAndrey Konovalov /* See Documentation/dev-tools/kcov.rst for usage details. */
44eec028c9SAndrey Konovalov void kcov_remote_start(u64 handle);
45eec028c9SAndrey Konovalov void kcov_remote_stop(void);
46eec028c9SAndrey Konovalov u64 kcov_common_handle(void);
47eec028c9SAndrey Konovalov 
kcov_remote_start_common(u64 id)48eec028c9SAndrey Konovalov static inline void kcov_remote_start_common(u64 id)
49eec028c9SAndrey Konovalov {
50eec028c9SAndrey Konovalov 	kcov_remote_start(kcov_remote_handle(KCOV_SUBSYSTEM_COMMON, id));
51eec028c9SAndrey Konovalov }
52eec028c9SAndrey Konovalov 
kcov_remote_start_usb(u64 id)53eec028c9SAndrey Konovalov static inline void kcov_remote_start_usb(u64 id)
54eec028c9SAndrey Konovalov {
55eec028c9SAndrey Konovalov 	kcov_remote_start(kcov_remote_handle(KCOV_SUBSYSTEM_USB, id));
56eec028c9SAndrey Konovalov }
57eec028c9SAndrey Konovalov 
58e89eed02SAndrey Konovalov /*
59e89eed02SAndrey Konovalov  * The softirq flavor of kcov_remote_*() functions is introduced as a temporary
60e89eed02SAndrey Konovalov  * work around for kcov's lack of nested remote coverage sections support in
61204c2f53SRong Tao  * task context. Adding support for nested sections is tracked in:
62e89eed02SAndrey Konovalov  * https://bugzilla.kernel.org/show_bug.cgi?id=210337
63e89eed02SAndrey Konovalov  */
64e89eed02SAndrey Konovalov 
kcov_remote_start_usb_softirq(u64 id)65e89eed02SAndrey Konovalov static inline void kcov_remote_start_usb_softirq(u64 id)
66e89eed02SAndrey Konovalov {
67e89eed02SAndrey Konovalov 	if (in_serving_softirq())
68e89eed02SAndrey Konovalov 		kcov_remote_start_usb(id);
69e89eed02SAndrey Konovalov }
70e89eed02SAndrey Konovalov 
kcov_remote_stop_softirq(void)71e89eed02SAndrey Konovalov static inline void kcov_remote_stop_softirq(void)
72e89eed02SAndrey Konovalov {
73e89eed02SAndrey Konovalov 	if (in_serving_softirq())
74e89eed02SAndrey Konovalov 		kcov_remote_stop();
75e89eed02SAndrey Konovalov }
76e89eed02SAndrey Konovalov 
77e0ddec73SArnd Bergmann #ifdef CONFIG_64BIT
78e0ddec73SArnd Bergmann typedef unsigned long kcov_u64;
79e0ddec73SArnd Bergmann #else
80e0ddec73SArnd Bergmann typedef unsigned long long kcov_u64;
81e0ddec73SArnd Bergmann #endif
82e0ddec73SArnd Bergmann 
83e0ddec73SArnd Bergmann void __sanitizer_cov_trace_pc(void);
84e0ddec73SArnd Bergmann void __sanitizer_cov_trace_cmp1(u8 arg1, u8 arg2);
85e0ddec73SArnd Bergmann void __sanitizer_cov_trace_cmp2(u16 arg1, u16 arg2);
86e0ddec73SArnd Bergmann void __sanitizer_cov_trace_cmp4(u32 arg1, u32 arg2);
87e0ddec73SArnd Bergmann void __sanitizer_cov_trace_cmp8(kcov_u64 arg1, kcov_u64 arg2);
88e0ddec73SArnd Bergmann void __sanitizer_cov_trace_const_cmp1(u8 arg1, u8 arg2);
89e0ddec73SArnd Bergmann void __sanitizer_cov_trace_const_cmp2(u16 arg1, u16 arg2);
90e0ddec73SArnd Bergmann void __sanitizer_cov_trace_const_cmp4(u32 arg1, u32 arg2);
91e0ddec73SArnd Bergmann void __sanitizer_cov_trace_const_cmp8(kcov_u64 arg1, kcov_u64 arg2);
92e0ddec73SArnd Bergmann void __sanitizer_cov_trace_switch(kcov_u64 val, void *cases);
93e0ddec73SArnd Bergmann 
945c9a8750SDmitry Vyukov #else
955c9a8750SDmitry Vyukov 
kcov_task_init(struct task_struct * t)965c9a8750SDmitry Vyukov static inline void kcov_task_init(struct task_struct *t) {}
kcov_task_exit(struct task_struct * t)975c9a8750SDmitry Vyukov static inline void kcov_task_exit(struct task_struct *t) {}
kcov_prepare_switch(struct task_struct * t)980ed557aaSMark Rutland static inline void kcov_prepare_switch(struct task_struct *t) {}
kcov_finish_switch(struct task_struct * t)990ed557aaSMark Rutland static inline void kcov_finish_switch(struct task_struct *t) {}
kcov_remote_start(u64 handle)100eec028c9SAndrey Konovalov static inline void kcov_remote_start(u64 handle) {}
kcov_remote_stop(void)101eec028c9SAndrey Konovalov static inline void kcov_remote_stop(void) {}
kcov_common_handle(void)102eec028c9SAndrey Konovalov static inline u64 kcov_common_handle(void)
103eec028c9SAndrey Konovalov {
104eec028c9SAndrey Konovalov 	return 0;
105eec028c9SAndrey Konovalov }
kcov_remote_start_common(u64 id)106eec028c9SAndrey Konovalov static inline void kcov_remote_start_common(u64 id) {}
kcov_remote_start_usb(u64 id)107eec028c9SAndrey Konovalov static inline void kcov_remote_start_usb(u64 id) {}
kcov_remote_start_usb_softirq(u64 id)108e89eed02SAndrey Konovalov static inline void kcov_remote_start_usb_softirq(u64 id) {}
kcov_remote_stop_softirq(void)109e89eed02SAndrey Konovalov static inline void kcov_remote_stop_softirq(void) {}
1105c9a8750SDmitry Vyukov 
1115c9a8750SDmitry Vyukov #endif /* CONFIG_KCOV */
1125c9a8750SDmitry Vyukov #endif /* _LINUX_KCOV_H */
113