xref: /freebsd/sys/kern/kern_kcov.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
1b3c0d957SAndrew Turner /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3b3c0d957SAndrew Turner  *
4b3c0d957SAndrew Turner  * Copyright (C) 2018 The FreeBSD Foundation. All rights reserved.
5b3c0d957SAndrew Turner  * Copyright (C) 2018, 2019 Andrew Turner
6b3c0d957SAndrew Turner  *
7b3c0d957SAndrew Turner  * This software was developed by Mitchell Horne under sponsorship of
8b3c0d957SAndrew Turner  * the FreeBSD Foundation.
9b3c0d957SAndrew Turner  *
10b3c0d957SAndrew Turner  * This software was developed by SRI International and the University of
11b3c0d957SAndrew Turner  * Cambridge Computer Laboratory under DARPA/AFRL contract FA8750-10-C-0237
12b3c0d957SAndrew Turner  * ("CTSRD"), as part of the DARPA CRASH research programme.
13b3c0d957SAndrew Turner  *
14b3c0d957SAndrew Turner  * Redistribution and use in source and binary forms, with or without
15b3c0d957SAndrew Turner  * modification, are permitted provided that the following conditions
16b3c0d957SAndrew Turner  * are met:
17b3c0d957SAndrew Turner  * 1. Redistributions of source code must retain the above copyright
18b3c0d957SAndrew Turner  *    notice, this list of conditions and the following disclaimer.
19b3c0d957SAndrew Turner  * 2. Redistributions in binary form must reproduce the above copyright
20b3c0d957SAndrew Turner  *    notice, this list of conditions and the following disclaimer in the
21b3c0d957SAndrew Turner  *    documentation and/or other materials provided with the distribution.
22b3c0d957SAndrew Turner  *
23b3c0d957SAndrew Turner  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24b3c0d957SAndrew Turner  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25b3c0d957SAndrew Turner  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26b3c0d957SAndrew Turner  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27b3c0d957SAndrew Turner  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28b3c0d957SAndrew Turner  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29b3c0d957SAndrew Turner  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30b3c0d957SAndrew Turner  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31b3c0d957SAndrew Turner  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32b3c0d957SAndrew Turner  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33b3c0d957SAndrew Turner  * SUCH DAMAGE.
34b3c0d957SAndrew Turner  */
35b3c0d957SAndrew Turner 
36cf4670feSMark Johnston /* Interceptors are required for KMSAN. */
37cf4670feSMark Johnston #if defined(KASAN) || defined(KCSAN)
383ead6023SMark Johnston #define	SAN_RUNTIME
393ead6023SMark Johnston #endif
40fd8f4f3bSAndrew Turner 
41b3c0d957SAndrew Turner #include <sys/param.h>
42524553f5SAndrew Turner #include <sys/systm.h>
43b3c0d957SAndrew Turner #include <sys/conf.h>
44a1fa04c0SMark Johnston #include <sys/eventhandler.h>
45b3c0d957SAndrew Turner #include <sys/kcov.h>
46b3c0d957SAndrew Turner #include <sys/kernel.h>
47524553f5SAndrew Turner #include <sys/limits.h>
48b3c0d957SAndrew Turner #include <sys/lock.h>
49b3c0d957SAndrew Turner #include <sys/malloc.h>
50b3c0d957SAndrew Turner #include <sys/mman.h>
51b3c0d957SAndrew Turner #include <sys/mutex.h>
52b3c0d957SAndrew Turner #include <sys/proc.h>
53b3c0d957SAndrew Turner #include <sys/rwlock.h>
54b3c0d957SAndrew Turner #include <sys/sysctl.h>
55b3c0d957SAndrew Turner 
56b3c0d957SAndrew Turner #include <vm/vm.h>
57524553f5SAndrew Turner #include <vm/pmap.h>
58b3c0d957SAndrew Turner #include <vm/vm_extern.h>
59b3c0d957SAndrew Turner #include <vm/vm_object.h>
60b3c0d957SAndrew Turner #include <vm/vm_page.h>
61b3c0d957SAndrew Turner #include <vm/vm_pager.h>
6201ffedf5SAndrew Turner #include <vm/vm_param.h>
63b3c0d957SAndrew Turner 
64b3c0d957SAndrew Turner MALLOC_DEFINE(M_KCOV_INFO, "kcovinfo", "KCOV info type");
65b3c0d957SAndrew Turner 
66b3c0d957SAndrew Turner #define	KCOV_ELEMENT_SIZE	sizeof(uint64_t)
67b3c0d957SAndrew Turner 
68b3c0d957SAndrew Turner /*
69b3c0d957SAndrew Turner  * To know what the code can safely perform at any point in time we use a
70b3c0d957SAndrew Turner  * state machine. In the normal case the state transitions are:
71b3c0d957SAndrew Turner  *
72b3c0d957SAndrew Turner  * OPEN -> READY -> RUNNING -> DYING
73b3c0d957SAndrew Turner  *  |       | ^        |        ^ ^
74b3c0d957SAndrew Turner  *  |       | +--------+        | |
75b3c0d957SAndrew Turner  *  |       +-------------------+ |
76b3c0d957SAndrew Turner  *  +-----------------------------+
77b3c0d957SAndrew Turner  *
78b3c0d957SAndrew Turner  * The states are:
79b3c0d957SAndrew Turner  *  OPEN:   The kcov fd has been opened, but no buffer is available to store
80b3c0d957SAndrew Turner  *          coverage data.
81b3c0d957SAndrew Turner  *  READY:  The buffer to store coverage data has been allocated. Userspace
82b3c0d957SAndrew Turner  *          can set this by using ioctl(fd, KIOSETBUFSIZE, entries);. When
83b3c0d957SAndrew Turner  *          this has been set the buffer can be written to by the kernel,
84b3c0d957SAndrew Turner  *          and mmaped by userspace.
85b3c0d957SAndrew Turner  * RUNNING: The coverage probes are able to store coverage data in the buffer.
86b3c0d957SAndrew Turner  *          This is entered with ioctl(fd, KIOENABLE, mode);. The READY state
87b3c0d957SAndrew Turner  *          can be exited by ioctl(fd, KIODISABLE); or exiting the thread to
88b3c0d957SAndrew Turner  *          return to the READY state to allow tracing to be reused, or by
89b3c0d957SAndrew Turner  *          closing the kcov fd to enter the DYING state.
90b3c0d957SAndrew Turner  * DYING:   The fd has been closed. All states can enter into this state when
91b3c0d957SAndrew Turner  *          userspace closes the kcov fd.
92b3c0d957SAndrew Turner  *
93b3c0d957SAndrew Turner  * We need to be careful when moving into and out of the RUNNING state. As
94b3c0d957SAndrew Turner  * an interrupt may happen while this is happening the ordering of memory
95b3c0d957SAndrew Turner  * operations is important so struct kcov_info is valid for the tracing
96b3c0d957SAndrew Turner  * functions.
97b3c0d957SAndrew Turner  *
98b3c0d957SAndrew Turner  * When moving into the RUNNING state prior stores to struct kcov_info need
99b3c0d957SAndrew Turner  * to be observed before the state is set. This allows for interrupts that
100b3c0d957SAndrew Turner  * may call into one of the coverage functions to fire at any point while
101b3c0d957SAndrew Turner  * being enabled and see a consistent struct kcov_info.
102b3c0d957SAndrew Turner  *
103b3c0d957SAndrew Turner  * When moving out of the RUNNING state any later stores to struct kcov_info
104b3c0d957SAndrew Turner  * need to be observed after the state is set. As with entering this is to
105b3c0d957SAndrew Turner  * present a consistent struct kcov_info to interrupts.
106b3c0d957SAndrew Turner  */
107b3c0d957SAndrew Turner typedef enum {
108b3c0d957SAndrew Turner 	KCOV_STATE_INVALID,
109b3c0d957SAndrew Turner 	KCOV_STATE_OPEN,	/* The device is open, but with no buffer */
110b3c0d957SAndrew Turner 	KCOV_STATE_READY,	/* The buffer has been allocated */
111b3c0d957SAndrew Turner 	KCOV_STATE_RUNNING,	/* Recording trace data */
112b3c0d957SAndrew Turner 	KCOV_STATE_DYING,	/* The fd was closed */
113b3c0d957SAndrew Turner } kcov_state_t;
114b3c0d957SAndrew Turner 
115b3c0d957SAndrew Turner /*
116b3c0d957SAndrew Turner  * (l) Set while holding the kcov_lock mutex and not in the RUNNING state.
117b3c0d957SAndrew Turner  * (o) Only set once while in the OPEN state. Cleaned up while in the DYING
118b3c0d957SAndrew Turner  *     state, and with no thread associated with the struct kcov_info.
119b3c0d957SAndrew Turner  * (s) Set atomically to enter or exit the RUNNING state, non-atomically
120b3c0d957SAndrew Turner  *     otherwise. See above for a description of the other constraints while
121b3c0d957SAndrew Turner  *     moving into or out of the RUNNING state.
122b3c0d957SAndrew Turner  */
123b3c0d957SAndrew Turner struct kcov_info {
124b3c0d957SAndrew Turner 	struct thread	*thread;	/* (l) */
125b3c0d957SAndrew Turner 	vm_object_t	bufobj;		/* (o) */
126b3c0d957SAndrew Turner 	vm_offset_t	kvaddr;		/* (o) */
127b3c0d957SAndrew Turner 	size_t		entries;	/* (o) */
128b3c0d957SAndrew Turner 	size_t		bufsize;	/* (o) */
129b3c0d957SAndrew Turner 	kcov_state_t	state;		/* (s) */
130b3c0d957SAndrew Turner 	int		mode;		/* (l) */
131b3c0d957SAndrew Turner };
132b3c0d957SAndrew Turner 
133b3c0d957SAndrew Turner /* Prototypes */
134b3c0d957SAndrew Turner static d_open_t		kcov_open;
135b3c0d957SAndrew Turner static d_close_t	kcov_close;
136b3c0d957SAndrew Turner static d_mmap_single_t	kcov_mmap_single;
137b3c0d957SAndrew Turner static d_ioctl_t	kcov_ioctl;
138b3c0d957SAndrew Turner 
139b3c0d957SAndrew Turner static int  kcov_alloc(struct kcov_info *info, size_t entries);
14072b66398SAndrew Turner static void kcov_free(struct kcov_info *info);
141b3c0d957SAndrew Turner static void kcov_init(const void *unused);
142b3c0d957SAndrew Turner 
143b3c0d957SAndrew Turner static struct cdevsw kcov_cdevsw = {
144b3c0d957SAndrew Turner 	.d_version =	D_VERSION,
145b3c0d957SAndrew Turner 	.d_open =	kcov_open,
146b3c0d957SAndrew Turner 	.d_close =	kcov_close,
147b3c0d957SAndrew Turner 	.d_mmap_single = kcov_mmap_single,
148b3c0d957SAndrew Turner 	.d_ioctl =	kcov_ioctl,
149b3c0d957SAndrew Turner 	.d_name =	"kcov",
150b3c0d957SAndrew Turner };
151b3c0d957SAndrew Turner 
1527029da5cSPawel Biernacki SYSCTL_NODE(_kern, OID_AUTO, kcov, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1537029da5cSPawel Biernacki     "Kernel coverage");
154b3c0d957SAndrew Turner 
155b3c0d957SAndrew Turner static u_int kcov_max_entries = KCOV_MAXENTRIES;
156b3c0d957SAndrew Turner SYSCTL_UINT(_kern_kcov, OID_AUTO, max_entries, CTLFLAG_RW,
157b3c0d957SAndrew Turner     &kcov_max_entries, 0,
158b3c0d957SAndrew Turner     "Maximum number of entries in the kcov buffer");
159b3c0d957SAndrew Turner 
160b3c0d957SAndrew Turner static struct mtx kcov_lock;
161524553f5SAndrew Turner static int active_count;
162b3c0d957SAndrew Turner 
16340fcdb93SMark Johnston static struct kcov_info * __nosanitizeaddress __nosanitizememory
get_kinfo(struct thread * td)164b3c0d957SAndrew Turner get_kinfo(struct thread *td)
165b3c0d957SAndrew Turner {
166b3c0d957SAndrew Turner 	struct kcov_info *info;
167b3c0d957SAndrew Turner 
168b3c0d957SAndrew Turner 	/* We might have a NULL thread when releasing the secondary CPUs */
169b3c0d957SAndrew Turner 	if (td == NULL)
170b3c0d957SAndrew Turner 		return (NULL);
171b3c0d957SAndrew Turner 
172b3c0d957SAndrew Turner 	/*
173b3c0d957SAndrew Turner 	 * We are in an interrupt, stop tracing as it is not explicitly
174b3c0d957SAndrew Turner 	 * part of a syscall.
175b3c0d957SAndrew Turner 	 */
176b3c0d957SAndrew Turner 	if (td->td_intr_nesting_level > 0 || td->td_intr_frame != NULL)
177b3c0d957SAndrew Turner 		return (NULL);
178b3c0d957SAndrew Turner 
179b3c0d957SAndrew Turner 	/*
180b3c0d957SAndrew Turner 	 * If info is NULL or the state is not running we are not tracing.
181b3c0d957SAndrew Turner 	 */
182b3c0d957SAndrew Turner 	info = td->td_kcov_info;
183b3c0d957SAndrew Turner 	if (info == NULL ||
184b3c0d957SAndrew Turner 	    atomic_load_acq_int(&info->state) != KCOV_STATE_RUNNING)
185b3c0d957SAndrew Turner 		return (NULL);
186b3c0d957SAndrew Turner 
187b3c0d957SAndrew Turner 	return (info);
188b3c0d957SAndrew Turner }
189b3c0d957SAndrew Turner 
190cf4670feSMark Johnston static void __nosanitizeaddress __nosanitizememory
trace_pc(uintptr_t ret)191524553f5SAndrew Turner trace_pc(uintptr_t ret)
192b3c0d957SAndrew Turner {
193b3c0d957SAndrew Turner 	struct thread *td;
194b3c0d957SAndrew Turner 	struct kcov_info *info;
195b3c0d957SAndrew Turner 	uint64_t *buf, index;
196b3c0d957SAndrew Turner 
197b3c0d957SAndrew Turner 	td = curthread;
198b3c0d957SAndrew Turner 	info = get_kinfo(td);
199b3c0d957SAndrew Turner 	if (info == NULL)
200b3c0d957SAndrew Turner 		return;
201b3c0d957SAndrew Turner 
202b3c0d957SAndrew Turner 	/*
203b3c0d957SAndrew Turner 	 * Check we are in the PC-trace mode.
204b3c0d957SAndrew Turner 	 */
205b3c0d957SAndrew Turner 	if (info->mode != KCOV_MODE_TRACE_PC)
206b3c0d957SAndrew Turner 		return;
207b3c0d957SAndrew Turner 
208080b4e8aSEd Maste 	KASSERT(info->kvaddr != 0, ("%s: NULL buf while running", __func__));
209b3c0d957SAndrew Turner 
210b3c0d957SAndrew Turner 	buf = (uint64_t *)info->kvaddr;
211b3c0d957SAndrew Turner 
212b3c0d957SAndrew Turner 	/* The first entry of the buffer holds the index */
213b3c0d957SAndrew Turner 	index = buf[0];
214b3c0d957SAndrew Turner 	if (index + 2 > info->entries)
215b3c0d957SAndrew Turner 		return;
216b3c0d957SAndrew Turner 
217524553f5SAndrew Turner 	buf[index + 1] = ret;
218b3c0d957SAndrew Turner 	buf[0] = index + 1;
219b3c0d957SAndrew Turner }
220b3c0d957SAndrew Turner 
221cf4670feSMark Johnston static bool __nosanitizeaddress __nosanitizememory
trace_cmp(uint64_t type,uint64_t arg1,uint64_t arg2,uint64_t ret)222b3c0d957SAndrew Turner trace_cmp(uint64_t type, uint64_t arg1, uint64_t arg2, uint64_t ret)
223b3c0d957SAndrew Turner {
224b3c0d957SAndrew Turner 	struct thread *td;
225b3c0d957SAndrew Turner 	struct kcov_info *info;
226b3c0d957SAndrew Turner 	uint64_t *buf, index;
227b3c0d957SAndrew Turner 
228b3c0d957SAndrew Turner 	td = curthread;
229b3c0d957SAndrew Turner 	info = get_kinfo(td);
230b3c0d957SAndrew Turner 	if (info == NULL)
231b3c0d957SAndrew Turner 		return (false);
232b3c0d957SAndrew Turner 
233b3c0d957SAndrew Turner 	/*
234b3c0d957SAndrew Turner 	 * Check we are in the comparison-trace mode.
235b3c0d957SAndrew Turner 	 */
236b3c0d957SAndrew Turner 	if (info->mode != KCOV_MODE_TRACE_CMP)
237b3c0d957SAndrew Turner 		return (false);
238b3c0d957SAndrew Turner 
239080b4e8aSEd Maste 	KASSERT(info->kvaddr != 0, ("%s: NULL buf while running", __func__));
240b3c0d957SAndrew Turner 
241b3c0d957SAndrew Turner 	buf = (uint64_t *)info->kvaddr;
242b3c0d957SAndrew Turner 
243b3c0d957SAndrew Turner 	/* The first entry of the buffer holds the index */
244b3c0d957SAndrew Turner 	index = buf[0];
245b3c0d957SAndrew Turner 
246b3c0d957SAndrew Turner 	/* Check we have space to store all elements */
247b3c0d957SAndrew Turner 	if (index * 4 + 4 + 1 > info->entries)
248b3c0d957SAndrew Turner 		return (false);
249b3c0d957SAndrew Turner 
250feb2cc80SAndrew Turner 	while (1) {
251b3c0d957SAndrew Turner 		buf[index * 4 + 1] = type;
252b3c0d957SAndrew Turner 		buf[index * 4 + 2] = arg1;
253b3c0d957SAndrew Turner 		buf[index * 4 + 3] = arg2;
254b3c0d957SAndrew Turner 		buf[index * 4 + 4] = ret;
255feb2cc80SAndrew Turner 
256feb2cc80SAndrew Turner 		if (atomic_cmpset_64(&buf[0], index, index + 1))
257feb2cc80SAndrew Turner 			break;
258feb2cc80SAndrew Turner 		buf[0] = index;
259feb2cc80SAndrew Turner 	}
260b3c0d957SAndrew Turner 
261b3c0d957SAndrew Turner 	return (true);
262b3c0d957SAndrew Turner }
263b3c0d957SAndrew Turner 
264b3c0d957SAndrew Turner /*
265b3c0d957SAndrew Turner  * The fd is being closed, cleanup everything we can.
266b3c0d957SAndrew Turner  */
267b3c0d957SAndrew Turner static void
kcov_mmap_cleanup(void * arg)268b3c0d957SAndrew Turner kcov_mmap_cleanup(void *arg)
269b3c0d957SAndrew Turner {
270b3c0d957SAndrew Turner 	struct kcov_info *info = arg;
271b3c0d957SAndrew Turner 	struct thread *thread;
272b3c0d957SAndrew Turner 
273b3c0d957SAndrew Turner 	mtx_lock_spin(&kcov_lock);
274b3c0d957SAndrew Turner 	/*
275b3c0d957SAndrew Turner 	 * Move to KCOV_STATE_DYING to stop adding new entries.
276b3c0d957SAndrew Turner 	 *
277b3c0d957SAndrew Turner 	 * If the thread is running we need to wait until thread exit to
278b3c0d957SAndrew Turner 	 * clean up as it may currently be adding a new entry. If this is
279b3c0d957SAndrew Turner 	 * the case being in KCOV_STATE_DYING will signal that the buffer
280b3c0d957SAndrew Turner 	 * needs to be cleaned up.
281b3c0d957SAndrew Turner 	 */
282b3c0d957SAndrew Turner 	atomic_store_int(&info->state, KCOV_STATE_DYING);
283b3c0d957SAndrew Turner 	atomic_thread_fence_seq_cst();
284b3c0d957SAndrew Turner 	thread = info->thread;
285b3c0d957SAndrew Turner 	mtx_unlock_spin(&kcov_lock);
286b3c0d957SAndrew Turner 
287b3c0d957SAndrew Turner 	if (thread != NULL)
288b3c0d957SAndrew Turner 		return;
289b3c0d957SAndrew Turner 
290b3c0d957SAndrew Turner 	/*
291b3c0d957SAndrew Turner 	 * We can safely clean up the info struct as it is in the
292b3c0d957SAndrew Turner 	 * KCOV_STATE_DYING state with no thread associated.
293b3c0d957SAndrew Turner 	 *
294b3c0d957SAndrew Turner 	 * The KCOV_STATE_DYING stops new threads from using it.
295b3c0d957SAndrew Turner 	 * The lack of a thread means nothing is currently using the buffers.
296b3c0d957SAndrew Turner 	 */
29772b66398SAndrew Turner 	kcov_free(info);
298b3c0d957SAndrew Turner }
299b3c0d957SAndrew Turner 
300b3c0d957SAndrew Turner static int
kcov_open(struct cdev * dev,int oflags,int devtype,struct thread * td)301b3c0d957SAndrew Turner kcov_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
302b3c0d957SAndrew Turner {
303b3c0d957SAndrew Turner 	struct kcov_info *info;
304b3c0d957SAndrew Turner 	int error;
305b3c0d957SAndrew Turner 
306b3c0d957SAndrew Turner 	info = malloc(sizeof(struct kcov_info), M_KCOV_INFO, M_ZERO | M_WAITOK);
307b3c0d957SAndrew Turner 	info->state = KCOV_STATE_OPEN;
308b3c0d957SAndrew Turner 	info->thread = NULL;
309b3c0d957SAndrew Turner 	info->mode = -1;
310b3c0d957SAndrew Turner 
311b3c0d957SAndrew Turner 	if ((error = devfs_set_cdevpriv(info, kcov_mmap_cleanup)) != 0)
312b3c0d957SAndrew Turner 		kcov_mmap_cleanup(info);
313b3c0d957SAndrew Turner 
314b3c0d957SAndrew Turner 	return (error);
315b3c0d957SAndrew Turner }
316b3c0d957SAndrew Turner 
317b3c0d957SAndrew Turner static int
kcov_close(struct cdev * dev,int fflag,int devtype,struct thread * td)318b3c0d957SAndrew Turner kcov_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
319b3c0d957SAndrew Turner {
320b3c0d957SAndrew Turner 	struct kcov_info *info;
321b3c0d957SAndrew Turner 	int error;
322b3c0d957SAndrew Turner 
323b3c0d957SAndrew Turner 	if ((error = devfs_get_cdevpriv((void **)&info)) != 0)
324b3c0d957SAndrew Turner 		return (error);
325b3c0d957SAndrew Turner 
326b3c0d957SAndrew Turner 	KASSERT(info != NULL, ("kcov_close with no kcov_info structure"));
327b3c0d957SAndrew Turner 
328b3c0d957SAndrew Turner 	/* Trying to close, but haven't disabled */
329b3c0d957SAndrew Turner 	if (info->state == KCOV_STATE_RUNNING)
330b3c0d957SAndrew Turner 		return (EBUSY);
331b3c0d957SAndrew Turner 
332b3c0d957SAndrew Turner 	return (0);
333b3c0d957SAndrew Turner }
334b3c0d957SAndrew Turner 
335b3c0d957SAndrew Turner static int
kcov_mmap_single(struct cdev * dev,vm_ooffset_t * offset,vm_size_t size,struct vm_object ** object,int nprot)336b3c0d957SAndrew Turner kcov_mmap_single(struct cdev *dev, vm_ooffset_t *offset, vm_size_t size,
337b3c0d957SAndrew Turner     struct vm_object **object, int nprot)
338b3c0d957SAndrew Turner {
339b3c0d957SAndrew Turner 	struct kcov_info *info;
340b3c0d957SAndrew Turner 	int error;
341b3c0d957SAndrew Turner 
342b3c0d957SAndrew Turner 	if ((nprot & (PROT_EXEC | PROT_READ | PROT_WRITE)) !=
343b3c0d957SAndrew Turner 	    (PROT_READ | PROT_WRITE))
344b3c0d957SAndrew Turner 		return (EINVAL);
345b3c0d957SAndrew Turner 
346b3c0d957SAndrew Turner 	if ((error = devfs_get_cdevpriv((void **)&info)) != 0)
347b3c0d957SAndrew Turner 		return (error);
348b3c0d957SAndrew Turner 
349bdffe3b5SAndrew Turner 	if (info->kvaddr == 0 || size / KCOV_ELEMENT_SIZE != info->entries)
350b3c0d957SAndrew Turner 		return (EINVAL);
351b3c0d957SAndrew Turner 
35201ffedf5SAndrew Turner 	vm_object_reference(info->bufobj);
353b3c0d957SAndrew Turner 	*offset = 0;
354b3c0d957SAndrew Turner 	*object = info->bufobj;
355b3c0d957SAndrew Turner 	return (0);
356b3c0d957SAndrew Turner }
357b3c0d957SAndrew Turner 
358b3c0d957SAndrew Turner static int
kcov_alloc(struct kcov_info * info,size_t entries)359b3c0d957SAndrew Turner kcov_alloc(struct kcov_info *info, size_t entries)
360b3c0d957SAndrew Turner {
361b3c0d957SAndrew Turner 	size_t n, pages;
362a759a0a0SAndrew Turner 	vm_page_t m;
363b3c0d957SAndrew Turner 
364b3c0d957SAndrew Turner 	KASSERT(info->kvaddr == 0, ("kcov_alloc: Already have a buffer"));
365b3c0d957SAndrew Turner 	KASSERT(info->state == KCOV_STATE_OPEN,
366b3c0d957SAndrew Turner 	    ("kcov_alloc: Not in open state (%x)", info->state));
367b3c0d957SAndrew Turner 
368b3c0d957SAndrew Turner 	if (entries < 2 || entries > kcov_max_entries)
369b3c0d957SAndrew Turner 		return (EINVAL);
370b3c0d957SAndrew Turner 
371b3c0d957SAndrew Turner 	/* Align to page size so mmap can't access other kernel memory */
372b3c0d957SAndrew Turner 	info->bufsize = roundup2(entries * KCOV_ELEMENT_SIZE, PAGE_SIZE);
373b3c0d957SAndrew Turner 	pages = info->bufsize / PAGE_SIZE;
374b3c0d957SAndrew Turner 
375b3c0d957SAndrew Turner 	if ((info->kvaddr = kva_alloc(info->bufsize)) == 0)
376b3c0d957SAndrew Turner 		return (ENOMEM);
377b3c0d957SAndrew Turner 
378b3c0d957SAndrew Turner 	info->bufobj = vm_pager_allocate(OBJT_PHYS, 0, info->bufsize,
379b3c0d957SAndrew Turner 	    PROT_READ | PROT_WRITE, 0, curthread->td_ucred);
380b3c0d957SAndrew Turner 
381b3c0d957SAndrew Turner 	VM_OBJECT_WLOCK(info->bufobj);
382b3c0d957SAndrew Turner 	for (n = 0; n < pages; n++) {
383a759a0a0SAndrew Turner 		m = vm_page_grab(info->bufobj, n,
38491e31c3cSJeff Roberson 		    VM_ALLOC_ZERO | VM_ALLOC_WIRED);
38591e31c3cSJeff Roberson 		vm_page_valid(m);
38691e31c3cSJeff Roberson 		vm_page_xunbusy(m);
387a759a0a0SAndrew Turner 		pmap_qenter(info->kvaddr + n * PAGE_SIZE, &m, 1);
388b3c0d957SAndrew Turner 	}
389b3c0d957SAndrew Turner 	VM_OBJECT_WUNLOCK(info->bufobj);
390b3c0d957SAndrew Turner 
391b3c0d957SAndrew Turner 	info->entries = entries;
392b3c0d957SAndrew Turner 
393b3c0d957SAndrew Turner 	return (0);
394b3c0d957SAndrew Turner }
395b3c0d957SAndrew Turner 
39672b66398SAndrew Turner static void
kcov_free(struct kcov_info * info)39772b66398SAndrew Turner kcov_free(struct kcov_info *info)
39872b66398SAndrew Turner {
39901ffedf5SAndrew Turner 	vm_page_t m;
40001ffedf5SAndrew Turner 	size_t i;
40172b66398SAndrew Turner 
40272b66398SAndrew Turner 	if (info->kvaddr != 0) {
40372b66398SAndrew Turner 		pmap_qremove(info->kvaddr, info->bufsize / PAGE_SIZE);
40472b66398SAndrew Turner 		kva_free(info->kvaddr, info->bufsize);
40572b66398SAndrew Turner 	}
40601ffedf5SAndrew Turner 	if (info->bufobj != NULL) {
40701ffedf5SAndrew Turner 		VM_OBJECT_WLOCK(info->bufobj);
40801ffedf5SAndrew Turner 		m = vm_page_lookup(info->bufobj, 0);
40901ffedf5SAndrew Turner 		for (i = 0; i < info->bufsize / PAGE_SIZE; i++) {
41001ffedf5SAndrew Turner 			vm_page_unwire_noq(m);
41101ffedf5SAndrew Turner 			m = vm_page_next(m);
41201ffedf5SAndrew Turner 		}
41301ffedf5SAndrew Turner 		VM_OBJECT_WUNLOCK(info->bufobj);
41472b66398SAndrew Turner 		vm_object_deallocate(info->bufobj);
41501ffedf5SAndrew Turner 	}
41672b66398SAndrew Turner 	free(info, M_KCOV_INFO);
41772b66398SAndrew Turner }
41872b66398SAndrew Turner 
419b3c0d957SAndrew Turner static int
kcov_ioctl(struct cdev * dev,u_long cmd,caddr_t data,int fflag __unused,struct thread * td)420b3c0d957SAndrew Turner kcov_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag __unused,
421b3c0d957SAndrew Turner     struct thread *td)
422b3c0d957SAndrew Turner {
423b3c0d957SAndrew Turner 	struct kcov_info *info;
424b3c0d957SAndrew Turner 	int mode, error;
425b3c0d957SAndrew Turner 
426b3c0d957SAndrew Turner 	if ((error = devfs_get_cdevpriv((void **)&info)) != 0)
427b3c0d957SAndrew Turner 		return (error);
428b3c0d957SAndrew Turner 
429b3c0d957SAndrew Turner 	if (cmd == KIOSETBUFSIZE) {
430b3c0d957SAndrew Turner 		/*
431b3c0d957SAndrew Turner 		 * Set the size of the coverage buffer. Should be called
432b3c0d957SAndrew Turner 		 * before enabling coverage collection for that thread.
433b3c0d957SAndrew Turner 		 */
434b3c0d957SAndrew Turner 		if (info->state != KCOV_STATE_OPEN) {
435b3c0d957SAndrew Turner 			return (EBUSY);
436b3c0d957SAndrew Turner 		}
437b3c0d957SAndrew Turner 		error = kcov_alloc(info, *(u_int *)data);
438b3c0d957SAndrew Turner 		if (error == 0)
439b3c0d957SAndrew Turner 			info->state = KCOV_STATE_READY;
440b3c0d957SAndrew Turner 		return (error);
441b3c0d957SAndrew Turner 	}
442b3c0d957SAndrew Turner 
443b3c0d957SAndrew Turner 	mtx_lock_spin(&kcov_lock);
444b3c0d957SAndrew Turner 	switch (cmd) {
445b3c0d957SAndrew Turner 	case KIOENABLE:
446b3c0d957SAndrew Turner 		if (info->state != KCOV_STATE_READY) {
447b3c0d957SAndrew Turner 			error = EBUSY;
448b3c0d957SAndrew Turner 			break;
449b3c0d957SAndrew Turner 		}
450b3c0d957SAndrew Turner 		if (td->td_kcov_info != NULL) {
451b3c0d957SAndrew Turner 			error = EINVAL;
452b3c0d957SAndrew Turner 			break;
453b3c0d957SAndrew Turner 		}
454b3c0d957SAndrew Turner 		mode = *(int *)data;
455b3c0d957SAndrew Turner 		if (mode != KCOV_MODE_TRACE_PC && mode != KCOV_MODE_TRACE_CMP) {
456b3c0d957SAndrew Turner 			error = EINVAL;
457b3c0d957SAndrew Turner 			break;
458b3c0d957SAndrew Turner 		}
459524553f5SAndrew Turner 
460524553f5SAndrew Turner 		/* Lets hope nobody opens this 2 billion times */
461524553f5SAndrew Turner 		KASSERT(active_count < INT_MAX,
462524553f5SAndrew Turner 		    ("%s: Open too many times", __func__));
463524553f5SAndrew Turner 		active_count++;
464524553f5SAndrew Turner 		if (active_count == 1) {
465524553f5SAndrew Turner 			cov_register_pc(&trace_pc);
466524553f5SAndrew Turner 			cov_register_cmp(&trace_cmp);
467524553f5SAndrew Turner 		}
468524553f5SAndrew Turner 
469b3c0d957SAndrew Turner 		KASSERT(info->thread == NULL,
470b3c0d957SAndrew Turner 		    ("Enabling kcov when already enabled"));
471b3c0d957SAndrew Turner 		info->thread = td;
472b3c0d957SAndrew Turner 		info->mode = mode;
473b3c0d957SAndrew Turner 		/*
474b3c0d957SAndrew Turner 		 * Ensure the mode has been set before starting coverage
475b3c0d957SAndrew Turner 		 * tracing.
476b3c0d957SAndrew Turner 		 */
477b3c0d957SAndrew Turner 		atomic_store_rel_int(&info->state, KCOV_STATE_RUNNING);
478b3c0d957SAndrew Turner 		td->td_kcov_info = info;
479b3c0d957SAndrew Turner 		break;
480b3c0d957SAndrew Turner 	case KIODISABLE:
481b3c0d957SAndrew Turner 		/* Only the currently enabled thread may disable itself */
482b3c0d957SAndrew Turner 		if (info->state != KCOV_STATE_RUNNING ||
483b3c0d957SAndrew Turner 		    info != td->td_kcov_info) {
484b3c0d957SAndrew Turner 			error = EINVAL;
485b3c0d957SAndrew Turner 			break;
486b3c0d957SAndrew Turner 		}
487524553f5SAndrew Turner 		KASSERT(active_count > 0, ("%s: Open count is zero", __func__));
488524553f5SAndrew Turner 		active_count--;
489524553f5SAndrew Turner 		if (active_count == 0) {
490c50c26aaSAndrew Turner 			cov_unregister_pc();
491c50c26aaSAndrew Turner 			cov_unregister_cmp();
492524553f5SAndrew Turner 		}
493524553f5SAndrew Turner 
494b3c0d957SAndrew Turner 		td->td_kcov_info = NULL;
495b3c0d957SAndrew Turner 		atomic_store_int(&info->state, KCOV_STATE_READY);
496b3c0d957SAndrew Turner 		/*
497b3c0d957SAndrew Turner 		 * Ensure we have exited the READY state before clearing the
498b3c0d957SAndrew Turner 		 * rest of the info struct.
499b3c0d957SAndrew Turner 		 */
500b3c0d957SAndrew Turner 		atomic_thread_fence_rel();
501b3c0d957SAndrew Turner 		info->mode = -1;
502b3c0d957SAndrew Turner 		info->thread = NULL;
503b3c0d957SAndrew Turner 		break;
504b3c0d957SAndrew Turner 	default:
505b3c0d957SAndrew Turner 		error = EINVAL;
506b3c0d957SAndrew Turner 		break;
507b3c0d957SAndrew Turner 	}
508b3c0d957SAndrew Turner 	mtx_unlock_spin(&kcov_lock);
509b3c0d957SAndrew Turner 
510b3c0d957SAndrew Turner 	return (error);
511b3c0d957SAndrew Turner }
512b3c0d957SAndrew Turner 
513b3c0d957SAndrew Turner static void
kcov_thread_dtor(void * arg __unused,struct thread * td)514b3c0d957SAndrew Turner kcov_thread_dtor(void *arg __unused, struct thread *td)
515b3c0d957SAndrew Turner {
516b3c0d957SAndrew Turner 	struct kcov_info *info;
517b3c0d957SAndrew Turner 
518b3c0d957SAndrew Turner 	info = td->td_kcov_info;
519b3c0d957SAndrew Turner 	if (info == NULL)
520b3c0d957SAndrew Turner 		return;
521b3c0d957SAndrew Turner 
522b3c0d957SAndrew Turner 	mtx_lock_spin(&kcov_lock);
523524553f5SAndrew Turner 	KASSERT(active_count > 0, ("%s: Open count is zero", __func__));
524524553f5SAndrew Turner 	active_count--;
525524553f5SAndrew Turner 	if (active_count == 0) {
526c50c26aaSAndrew Turner 		cov_unregister_pc();
527c50c26aaSAndrew Turner 		cov_unregister_cmp();
528524553f5SAndrew Turner 	}
529b3c0d957SAndrew Turner 	td->td_kcov_info = NULL;
530b3c0d957SAndrew Turner 	if (info->state != KCOV_STATE_DYING) {
531b3c0d957SAndrew Turner 		/*
532b3c0d957SAndrew Turner 		 * The kcov file is still open. Mark it as unused and
533b3c0d957SAndrew Turner 		 * wait for it to be closed before cleaning up.
534b3c0d957SAndrew Turner 		 */
535b3c0d957SAndrew Turner 		atomic_store_int(&info->state, KCOV_STATE_READY);
536b3c0d957SAndrew Turner 		atomic_thread_fence_seq_cst();
537b3c0d957SAndrew Turner 		/* This info struct is unused */
538b3c0d957SAndrew Turner 		info->thread = NULL;
539b3c0d957SAndrew Turner 		mtx_unlock_spin(&kcov_lock);
540b3c0d957SAndrew Turner 		return;
541b3c0d957SAndrew Turner 	}
542b3c0d957SAndrew Turner 	mtx_unlock_spin(&kcov_lock);
543b3c0d957SAndrew Turner 
544b3c0d957SAndrew Turner 	/*
545b3c0d957SAndrew Turner 	 * We can safely clean up the info struct as it is in the
546b3c0d957SAndrew Turner 	 * KCOV_STATE_DYING state where the info struct is associated with
547b3c0d957SAndrew Turner 	 * the current thread that's about to exit.
548b3c0d957SAndrew Turner 	 *
549b3c0d957SAndrew Turner 	 * The KCOV_STATE_DYING stops new threads from using it.
550b3c0d957SAndrew Turner 	 * It also stops the current thread from trying to use the info struct.
551b3c0d957SAndrew Turner 	 */
55272b66398SAndrew Turner 	kcov_free(info);
553b3c0d957SAndrew Turner }
554b3c0d957SAndrew Turner 
555b3c0d957SAndrew Turner static void
kcov_init(const void * unused)556b3c0d957SAndrew Turner kcov_init(const void *unused)
557b3c0d957SAndrew Turner {
558b3c0d957SAndrew Turner 	struct make_dev_args args;
559b3c0d957SAndrew Turner 	struct cdev *dev;
560b3c0d957SAndrew Turner 
561b3c0d957SAndrew Turner 	mtx_init(&kcov_lock, "kcov lock", NULL, MTX_SPIN);
562b3c0d957SAndrew Turner 
563b3c0d957SAndrew Turner 	make_dev_args_init(&args);
564b3c0d957SAndrew Turner 	args.mda_devsw = &kcov_cdevsw;
565b3c0d957SAndrew Turner 	args.mda_uid = UID_ROOT;
566b3c0d957SAndrew Turner 	args.mda_gid = GID_WHEEL;
567b3c0d957SAndrew Turner 	args.mda_mode = 0600;
568b3c0d957SAndrew Turner 	if (make_dev_s(&args, &dev, "kcov") != 0) {
569b3c0d957SAndrew Turner 		printf("%s", "Failed to create kcov device");
570b3c0d957SAndrew Turner 		return;
571b3c0d957SAndrew Turner 	}
572b3c0d957SAndrew Turner 
573b3c0d957SAndrew Turner 	EVENTHANDLER_REGISTER(thread_dtor, kcov_thread_dtor, NULL,
574b3c0d957SAndrew Turner 	    EVENTHANDLER_PRI_ANY);
575b3c0d957SAndrew Turner }
576b3c0d957SAndrew Turner 
577524553f5SAndrew Turner SYSINIT(kcovdev, SI_SUB_LAST, SI_ORDER_ANY, kcov_init, NULL);
578