2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (C) 2018 The FreeBSD Foundation. All rights reserved.
5 * Copyright (C) 2018, 2019 Andrew Turner
7 * This software was developed by Mitchell Horne under sponsorship of
8 * the FreeBSD Foundation.
10 * This software was developed by SRI International and the University of
11 * Cambridge Computer Laboratory under DARPA/AFRL contract FA8750-10-C-0237
12 * ("CTSRD"), as part of the DARPA CRASH research programme.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 #include <sys/param.h>
42 #include <sys/systm.h>
44 #include <sys/eventhandler.h>
46 #include <sys/kernel.h>
47 #include <sys/limits.h>
49 #include <sys/malloc.h>
51 #include <sys/mutex.h>
53 #include <sys/rwlock.h>
54 #include <sys/sysctl.h>
58 #include <vm/vm_extern.h>
59 #include <vm/vm_object.h>
60 #include <vm/vm_page.h>
61 #include <vm/vm_pager.h>
62 #include <vm/vm_param.h>
64 MALLOC_DEFINE(M_KCOV_INFO, "kcovinfo", "KCOV info type");
66 #define KCOV_ELEMENT_SIZE sizeof(uint64_t)
69 * To know what the code can safely perform at any point in time we use a
70 * state machine. In the normal case the state transitions are:
72 * OPEN -> READY -> RUNNING -> DYING
75 * | +-------------------+ |
76 * +-----------------------------+
79 * OPEN: The kcov fd has been opened, but no buffer is available to store
81 * READY: The buffer to store coverage data has been allocated. Userspace
82 * can set this by using ioctl(fd, KIOSETBUFSIZE, entries);. When
83 * this has been set the buffer can be written to by the kernel,
84 * and mmaped by userspace.
85 * RUNNING: The coverage probes are able to store coverage data in the buffer.
86 * This is entered with ioctl(fd, KIOENABLE, mode);. The READY state
87 * can be exited by ioctl(fd, KIODISABLE); or exiting the thread to
88 * return to the READY state to allow tracing to be reused, or by
89 * closing the kcov fd to enter the DYING state.
90 * DYING: The fd has been closed. All states can enter into this state when
91 * userspace closes the kcov fd.
93 * We need to be careful when moving into and out of the RUNNING state. As
94 * an interrupt may happen while this is happening the ordering of memory
95 * operations is important so struct kcov_info is valid for the tracing
98 * When moving into the RUNNING state prior stores to struct kcov_info need
99 * to be observed before the state is set. This allows for interrupts that
100 * may call into one of the coverage functions to fire at any point while
101 * being enabled and see a consistent struct kcov_info.
103 * When moving out of the RUNNING state any later stores to struct kcov_info
104 * need to be observed after the state is set. As with entering this is to
105 * present a consistent struct kcov_info to interrupts.
109 KCOV_STATE_OPEN, /* The device is open, but with no buffer */
110 KCOV_STATE_READY, /* The buffer has been allocated */
111 KCOV_STATE_RUNNING, /* Recording trace data */
112 KCOV_STATE_DYING, /* The fd was closed */
116 * (l) Set while holding the kcov_lock mutex and not in the RUNNING state.
117 * (o) Only set once while in the OPEN state. Cleaned up while in the DYING
118 * state, and with no thread associated with the struct kcov_info.
119 * (s) Set atomically to enter or exit the RUNNING state, non-atomically
120 * otherwise. See above for a description of the other constraints while
121 * moving into or out of the RUNNING state.
124 struct thread *thread; /* (l) */
125 vm_object_t bufobj; /* (o) */
126 vm_offset_t kvaddr; /* (o) */
127 size_t entries; /* (o) */
128 size_t bufsize; /* (o) */
129 kcov_state_t state; /* (s) */
134 static d_open_t kcov_open;
135 static d_close_t kcov_close;
136 static d_mmap_single_t kcov_mmap_single;
137 static d_ioctl_t kcov_ioctl;
139 static int kcov_alloc(struct kcov_info *info, size_t entries);
140 static void kcov_free(struct kcov_info *info);
141 static void kcov_init(const void *unused);
143 static struct cdevsw kcov_cdevsw = {
144 .d_version = D_VERSION,
146 .d_close = kcov_close,
147 .d_mmap_single = kcov_mmap_single,
148 .d_ioctl = kcov_ioctl,
152 SYSCTL_NODE(_kern, OID_AUTO, kcov, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
155 static u_int kcov_max_entries = KCOV_MAXENTRIES;
156 SYSCTL_UINT(_kern_kcov, OID_AUTO, max_entries, CTLFLAG_RW,
157 &kcov_max_entries, 0,
158 "Maximum number of entries in the kcov buffer");
160 static struct mtx kcov_lock;
161 static int active_count;
163 static struct kcov_info *
164 get_kinfo(struct thread *td)
166 struct kcov_info *info;
168 /* We might have a NULL thread when releasing the secondary CPUs */
173 * We are in an interrupt, stop tracing as it is not explicitly
176 if (td->td_intr_nesting_level > 0 || td->td_intr_frame != NULL)
180 * If info is NULL or the state is not running we are not tracing.
182 info = td->td_kcov_info;
184 atomic_load_acq_int(&info->state) != KCOV_STATE_RUNNING)
191 trace_pc(uintptr_t ret)
194 struct kcov_info *info;
195 uint64_t *buf, index;
198 info = get_kinfo(td);
203 * Check we are in the PC-trace mode.
205 if (info->mode != KCOV_MODE_TRACE_PC)
208 KASSERT(info->kvaddr != 0,
209 ("__sanitizer_cov_trace_pc: NULL buf while running"));
211 buf = (uint64_t *)info->kvaddr;
213 /* The first entry of the buffer holds the index */
215 if (index + 2 > info->entries)
218 buf[index + 1] = ret;
223 trace_cmp(uint64_t type, uint64_t arg1, uint64_t arg2, uint64_t ret)
226 struct kcov_info *info;
227 uint64_t *buf, index;
230 info = get_kinfo(td);
235 * Check we are in the comparison-trace mode.
237 if (info->mode != KCOV_MODE_TRACE_CMP)
240 KASSERT(info->kvaddr != 0,
241 ("__sanitizer_cov_trace_pc: NULL buf while running"));
243 buf = (uint64_t *)info->kvaddr;
245 /* The first entry of the buffer holds the index */
248 /* Check we have space to store all elements */
249 if (index * 4 + 4 + 1 > info->entries)
253 buf[index * 4 + 1] = type;
254 buf[index * 4 + 2] = arg1;
255 buf[index * 4 + 3] = arg2;
256 buf[index * 4 + 4] = ret;
258 if (atomic_cmpset_64(&buf[0], index, index + 1))
267 * The fd is being closed, cleanup everything we can.
270 kcov_mmap_cleanup(void *arg)
272 struct kcov_info *info = arg;
273 struct thread *thread;
275 mtx_lock_spin(&kcov_lock);
277 * Move to KCOV_STATE_DYING to stop adding new entries.
279 * If the thread is running we need to wait until thread exit to
280 * clean up as it may currently be adding a new entry. If this is
281 * the case being in KCOV_STATE_DYING will signal that the buffer
282 * needs to be cleaned up.
284 atomic_store_int(&info->state, KCOV_STATE_DYING);
285 atomic_thread_fence_seq_cst();
286 thread = info->thread;
287 mtx_unlock_spin(&kcov_lock);
293 * We can safely clean up the info struct as it is in the
294 * KCOV_STATE_DYING state with no thread associated.
296 * The KCOV_STATE_DYING stops new threads from using it.
297 * The lack of a thread means nothing is currently using the buffers.
303 kcov_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
305 struct kcov_info *info;
308 info = malloc(sizeof(struct kcov_info), M_KCOV_INFO, M_ZERO | M_WAITOK);
309 info->state = KCOV_STATE_OPEN;
313 if ((error = devfs_set_cdevpriv(info, kcov_mmap_cleanup)) != 0)
314 kcov_mmap_cleanup(info);
320 kcov_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
322 struct kcov_info *info;
325 if ((error = devfs_get_cdevpriv((void **)&info)) != 0)
328 KASSERT(info != NULL, ("kcov_close with no kcov_info structure"));
330 /* Trying to close, but haven't disabled */
331 if (info->state == KCOV_STATE_RUNNING)
338 kcov_mmap_single(struct cdev *dev, vm_ooffset_t *offset, vm_size_t size,
339 struct vm_object **object, int nprot)
341 struct kcov_info *info;
344 if ((nprot & (PROT_EXEC | PROT_READ | PROT_WRITE)) !=
345 (PROT_READ | PROT_WRITE))
348 if ((error = devfs_get_cdevpriv((void **)&info)) != 0)
351 if (info->kvaddr == 0 || size / KCOV_ELEMENT_SIZE != info->entries)
354 vm_object_reference(info->bufobj);
356 *object = info->bufobj;
361 kcov_alloc(struct kcov_info *info, size_t entries)
366 KASSERT(info->kvaddr == 0, ("kcov_alloc: Already have a buffer"));
367 KASSERT(info->state == KCOV_STATE_OPEN,
368 ("kcov_alloc: Not in open state (%x)", info->state));
370 if (entries < 2 || entries > kcov_max_entries)
373 /* Align to page size so mmap can't access other kernel memory */
374 info->bufsize = roundup2(entries * KCOV_ELEMENT_SIZE, PAGE_SIZE);
375 pages = info->bufsize / PAGE_SIZE;
377 if ((info->kvaddr = kva_alloc(info->bufsize)) == 0)
380 info->bufobj = vm_pager_allocate(OBJT_PHYS, 0, info->bufsize,
381 PROT_READ | PROT_WRITE, 0, curthread->td_ucred);
383 VM_OBJECT_WLOCK(info->bufobj);
384 for (n = 0; n < pages; n++) {
385 m = vm_page_grab(info->bufobj, n,
386 VM_ALLOC_ZERO | VM_ALLOC_WIRED);
389 pmap_qenter(info->kvaddr + n * PAGE_SIZE, &m, 1);
391 VM_OBJECT_WUNLOCK(info->bufobj);
393 info->entries = entries;
399 kcov_free(struct kcov_info *info)
404 if (info->kvaddr != 0) {
405 pmap_qremove(info->kvaddr, info->bufsize / PAGE_SIZE);
406 kva_free(info->kvaddr, info->bufsize);
408 if (info->bufobj != NULL) {
409 VM_OBJECT_WLOCK(info->bufobj);
410 m = vm_page_lookup(info->bufobj, 0);
411 for (i = 0; i < info->bufsize / PAGE_SIZE; i++) {
412 vm_page_unwire_noq(m);
415 VM_OBJECT_WUNLOCK(info->bufobj);
416 vm_object_deallocate(info->bufobj);
418 free(info, M_KCOV_INFO);
422 kcov_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag __unused,
425 struct kcov_info *info;
428 if ((error = devfs_get_cdevpriv((void **)&info)) != 0)
431 if (cmd == KIOSETBUFSIZE) {
433 * Set the size of the coverage buffer. Should be called
434 * before enabling coverage collection for that thread.
436 if (info->state != KCOV_STATE_OPEN) {
439 error = kcov_alloc(info, *(u_int *)data);
441 info->state = KCOV_STATE_READY;
445 mtx_lock_spin(&kcov_lock);
448 if (info->state != KCOV_STATE_READY) {
452 if (td->td_kcov_info != NULL) {
457 if (mode != KCOV_MODE_TRACE_PC && mode != KCOV_MODE_TRACE_CMP) {
462 /* Lets hope nobody opens this 2 billion times */
463 KASSERT(active_count < INT_MAX,
464 ("%s: Open too many times", __func__));
466 if (active_count == 1) {
467 cov_register_pc(&trace_pc);
468 cov_register_cmp(&trace_cmp);
471 KASSERT(info->thread == NULL,
472 ("Enabling kcov when already enabled"));
476 * Ensure the mode has been set before starting coverage
479 atomic_store_rel_int(&info->state, KCOV_STATE_RUNNING);
480 td->td_kcov_info = info;
483 /* Only the currently enabled thread may disable itself */
484 if (info->state != KCOV_STATE_RUNNING ||
485 info != td->td_kcov_info) {
489 KASSERT(active_count > 0, ("%s: Open count is zero", __func__));
491 if (active_count == 0) {
493 cov_unregister_cmp();
496 td->td_kcov_info = NULL;
497 atomic_store_int(&info->state, KCOV_STATE_READY);
499 * Ensure we have exited the READY state before clearing the
500 * rest of the info struct.
502 atomic_thread_fence_rel();
510 mtx_unlock_spin(&kcov_lock);
516 kcov_thread_dtor(void *arg __unused, struct thread *td)
518 struct kcov_info *info;
520 info = td->td_kcov_info;
524 mtx_lock_spin(&kcov_lock);
525 KASSERT(active_count > 0, ("%s: Open count is zero", __func__));
527 if (active_count == 0) {
529 cov_unregister_cmp();
531 td->td_kcov_info = NULL;
532 if (info->state != KCOV_STATE_DYING) {
534 * The kcov file is still open. Mark it as unused and
535 * wait for it to be closed before cleaning up.
537 atomic_store_int(&info->state, KCOV_STATE_READY);
538 atomic_thread_fence_seq_cst();
539 /* This info struct is unused */
541 mtx_unlock_spin(&kcov_lock);
544 mtx_unlock_spin(&kcov_lock);
547 * We can safely clean up the info struct as it is in the
548 * KCOV_STATE_DYING state where the info struct is associated with
549 * the current thread that's about to exit.
551 * The KCOV_STATE_DYING stops new threads from using it.
552 * It also stops the current thread from trying to use the info struct.
558 kcov_init(const void *unused)
560 struct make_dev_args args;
563 mtx_init(&kcov_lock, "kcov lock", NULL, MTX_SPIN);
565 make_dev_args_init(&args);
566 args.mda_devsw = &kcov_cdevsw;
567 args.mda_uid = UID_ROOT;
568 args.mda_gid = GID_WHEEL;
569 args.mda_mode = 0600;
570 if (make_dev_s(&args, &dev, "kcov") != 0) {
571 printf("%s", "Failed to create kcov device");
575 EVENTHANDLER_REGISTER(thread_dtor, kcov_thread_dtor, NULL,
576 EVENTHANDLER_PRI_ANY);
579 SYSINIT(kcovdev, SI_SUB_LAST, SI_ORDER_ANY, kcov_init, NULL);