xref: /OK3568_Linux_fs/kernel/include/linux/kcov.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun #ifndef _LINUX_KCOV_H
3*4882a593Smuzhiyun #define _LINUX_KCOV_H
4*4882a593Smuzhiyun 
5*4882a593Smuzhiyun #include <uapi/linux/kcov.h>
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun struct task_struct;
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #ifdef CONFIG_KCOV
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun enum kcov_mode {
12*4882a593Smuzhiyun 	/* Coverage collection is not enabled yet. */
13*4882a593Smuzhiyun 	KCOV_MODE_DISABLED = 0,
14*4882a593Smuzhiyun 	/* KCOV was initialized, but tracing mode hasn't been chosen yet. */
15*4882a593Smuzhiyun 	KCOV_MODE_INIT = 1,
16*4882a593Smuzhiyun 	/*
17*4882a593Smuzhiyun 	 * Tracing coverage collection mode.
18*4882a593Smuzhiyun 	 * Covered PCs are collected in a per-task buffer.
19*4882a593Smuzhiyun 	 */
20*4882a593Smuzhiyun 	KCOV_MODE_TRACE_PC = 2,
21*4882a593Smuzhiyun 	/* Collecting comparison operands mode. */
22*4882a593Smuzhiyun 	KCOV_MODE_TRACE_CMP = 3,
23*4882a593Smuzhiyun };
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun #define KCOV_IN_CTXSW	(1 << 30)
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun void kcov_task_init(struct task_struct *t);
28*4882a593Smuzhiyun void kcov_task_exit(struct task_struct *t);
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun #define kcov_prepare_switch(t)			\
31*4882a593Smuzhiyun do {						\
32*4882a593Smuzhiyun 	(t)->kcov_mode |= KCOV_IN_CTXSW;	\
33*4882a593Smuzhiyun } while (0)
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun #define kcov_finish_switch(t)			\
36*4882a593Smuzhiyun do {						\
37*4882a593Smuzhiyun 	(t)->kcov_mode &= ~KCOV_IN_CTXSW;	\
38*4882a593Smuzhiyun } while (0)
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun /* See Documentation/dev-tools/kcov.rst for usage details. */
41*4882a593Smuzhiyun void kcov_remote_start(u64 handle);
42*4882a593Smuzhiyun void kcov_remote_stop(void);
43*4882a593Smuzhiyun u64 kcov_common_handle(void);
44*4882a593Smuzhiyun 
kcov_remote_start_common(u64 id)45*4882a593Smuzhiyun static inline void kcov_remote_start_common(u64 id)
46*4882a593Smuzhiyun {
47*4882a593Smuzhiyun 	kcov_remote_start(kcov_remote_handle(KCOV_SUBSYSTEM_COMMON, id));
48*4882a593Smuzhiyun }
49*4882a593Smuzhiyun 
kcov_remote_start_usb(u64 id)50*4882a593Smuzhiyun static inline void kcov_remote_start_usb(u64 id)
51*4882a593Smuzhiyun {
52*4882a593Smuzhiyun 	kcov_remote_start(kcov_remote_handle(KCOV_SUBSYSTEM_USB, id));
53*4882a593Smuzhiyun }
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun #else
56*4882a593Smuzhiyun 
kcov_task_init(struct task_struct * t)57*4882a593Smuzhiyun static inline void kcov_task_init(struct task_struct *t) {}
kcov_task_exit(struct task_struct * t)58*4882a593Smuzhiyun static inline void kcov_task_exit(struct task_struct *t) {}
kcov_prepare_switch(struct task_struct * t)59*4882a593Smuzhiyun static inline void kcov_prepare_switch(struct task_struct *t) {}
kcov_finish_switch(struct task_struct * t)60*4882a593Smuzhiyun static inline void kcov_finish_switch(struct task_struct *t) {}
kcov_remote_start(u64 handle)61*4882a593Smuzhiyun static inline void kcov_remote_start(u64 handle) {}
kcov_remote_stop(void)62*4882a593Smuzhiyun static inline void kcov_remote_stop(void) {}
kcov_common_handle(void)63*4882a593Smuzhiyun static inline u64 kcov_common_handle(void)
64*4882a593Smuzhiyun {
65*4882a593Smuzhiyun 	return 0;
66*4882a593Smuzhiyun }
kcov_remote_start_common(u64 id)67*4882a593Smuzhiyun static inline void kcov_remote_start_common(u64 id) {}
kcov_remote_start_usb(u64 id)68*4882a593Smuzhiyun static inline void kcov_remote_start_usb(u64 id) {}
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun #endif /* CONFIG_KCOV */
71*4882a593Smuzhiyun #endif /* _LINUX_KCOV_H */
72