kcov.h (2463B)
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 8struct task_struct; 9 10#ifdef CONFIG_KCOV 11 12enum 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}; 25 26#define KCOV_IN_CTXSW (1 << 30) 27 28void kcov_task_init(struct task_struct *t); 29void kcov_task_exit(struct task_struct *t); 30 31#define kcov_prepare_switch(t) \ 32do { \ 33 (t)->kcov_mode |= KCOV_IN_CTXSW; \ 34} while (0) 35 36#define kcov_finish_switch(t) \ 37do { \ 38 (t)->kcov_mode &= ~KCOV_IN_CTXSW; \ 39} while (0) 40 41/* See Documentation/dev-tools/kcov.rst for usage details. */ 42void kcov_remote_start(u64 handle); 43void kcov_remote_stop(void); 44u64 kcov_common_handle(void); 45 46static inline void kcov_remote_start_common(u64 id) 47{ 48 kcov_remote_start(kcov_remote_handle(KCOV_SUBSYSTEM_COMMON, id)); 49} 50 51static inline void kcov_remote_start_usb(u64 id) 52{ 53 kcov_remote_start(kcov_remote_handle(KCOV_SUBSYSTEM_USB, id)); 54} 55 56/* 57 * The softirq flavor of kcov_remote_*() functions is introduced as a temporary 58 * work around for kcov's lack of nested remote coverage sections support in 59 * task context. Adding suport for nested sections is tracked in: 60 * https://bugzilla.kernel.org/show_bug.cgi?id=210337 61 */ 62 63static inline void kcov_remote_start_usb_softirq(u64 id) 64{ 65 if (in_serving_softirq()) 66 kcov_remote_start_usb(id); 67} 68 69static inline void kcov_remote_stop_softirq(void) 70{ 71 if (in_serving_softirq()) 72 kcov_remote_stop(); 73} 74 75#else 76 77static inline void kcov_task_init(struct task_struct *t) {} 78static inline void kcov_task_exit(struct task_struct *t) {} 79static inline void kcov_prepare_switch(struct task_struct *t) {} 80static inline void kcov_finish_switch(struct task_struct *t) {} 81static inline void kcov_remote_start(u64 handle) {} 82static inline void kcov_remote_stop(void) {} 83static inline u64 kcov_common_handle(void) 84{ 85 return 0; 86} 87static inline void kcov_remote_start_common(u64 id) {} 88static inline void kcov_remote_start_usb(u64 id) {} 89static inline void kcov_remote_start_usb_softirq(u64 id) {} 90static inline void kcov_remote_stop_softirq(void) {} 91 92#endif /* CONFIG_KCOV */ 93#endif /* _LINUX_KCOV_H */