Home | History | Annotate | Line # | Download | only in kern
subr_kcov.c revision 1.8.2.3
      1  1.8.2.3    martin /*	$NetBSD: subr_kcov.c,v 1.8.2.3 2020/04/13 08:05:04 martin Exp $	*/
      2  1.8.2.2  christos 
      3  1.8.2.2  christos /*
      4  1.8.2.3    martin  * Copyright (c) 2019-2020 The NetBSD Foundation, Inc.
      5  1.8.2.2  christos  * All rights reserved.
      6  1.8.2.2  christos  *
      7  1.8.2.2  christos  * This code is derived from software contributed to The NetBSD Foundation
      8  1.8.2.2  christos  * by Siddharth Muralee.
      9  1.8.2.2  christos  *
     10  1.8.2.2  christos  * Redistribution and use in source and binary forms, with or without
     11  1.8.2.2  christos  * modification, are permitted provided that the following conditions
     12  1.8.2.2  christos  * are met:
     13  1.8.2.2  christos  * 1. Redistributions of source code must retain the above copyright
     14  1.8.2.2  christos  *    notice, this list of conditions and the following disclaimer.
     15  1.8.2.2  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.8.2.2  christos  *    notice, this list of conditions and the following disclaimer in the
     17  1.8.2.2  christos  *    documentation and/or other materials provided with the distribution.
     18  1.8.2.2  christos  *
     19  1.8.2.2  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.8.2.2  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.8.2.2  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.8.2.2  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.8.2.2  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.8.2.2  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.8.2.2  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.8.2.2  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.8.2.2  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.8.2.2  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.8.2.2  christos  * POSSIBILITY OF SUCH DAMAGE.
     30  1.8.2.2  christos  */
     31  1.8.2.2  christos 
     32  1.8.2.2  christos #include <sys/cdefs.h>
     33  1.8.2.2  christos 
     34  1.8.2.2  christos #include <sys/module.h>
     35  1.8.2.2  christos #include <sys/param.h>
     36  1.8.2.2  christos #include <sys/systm.h>
     37  1.8.2.2  christos #include <sys/kernel.h>
     38  1.8.2.2  christos 
     39  1.8.2.2  christos #include <sys/conf.h>
     40  1.8.2.2  christos #include <sys/condvar.h>
     41  1.8.2.2  christos #include <sys/file.h>
     42  1.8.2.2  christos #include <sys/filedesc.h>
     43  1.8.2.2  christos #include <sys/kmem.h>
     44  1.8.2.2  christos #include <sys/mman.h>
     45  1.8.2.2  christos #include <sys/mutex.h>
     46  1.8.2.2  christos #include <sys/queue.h>
     47  1.8.2.2  christos 
     48  1.8.2.2  christos #include <uvm/uvm_extern.h>
     49  1.8.2.2  christos #include <sys/kcov.h>
     50  1.8.2.2  christos 
     51  1.8.2.2  christos #define KCOV_BUF_MAX_ENTRIES	(256 << 10)
     52  1.8.2.2  christos 
     53  1.8.2.2  christos #define KCOV_CMP_CONST		1
     54  1.8.2.2  christos #define KCOV_CMP_SIZE(x)	((x) << 1)
     55  1.8.2.2  christos 
     56  1.8.2.2  christos static dev_type_open(kcov_open);
     57  1.8.2.2  christos 
     58  1.8.2.2  christos const struct cdevsw kcov_cdevsw = {
     59  1.8.2.2  christos 	.d_open = kcov_open,
     60  1.8.2.2  christos 	.d_close = noclose,
     61  1.8.2.2  christos 	.d_read = noread,
     62  1.8.2.2  christos 	.d_write = nowrite,
     63  1.8.2.2  christos 	.d_ioctl = noioctl,
     64  1.8.2.2  christos 	.d_stop = nostop,
     65  1.8.2.2  christos 	.d_tty = notty,
     66  1.8.2.2  christos 	.d_poll = nopoll,
     67  1.8.2.2  christos 	.d_mmap = nommap,
     68  1.8.2.2  christos 	.d_kqfilter = nokqfilter,
     69  1.8.2.2  christos 	.d_discard = nodiscard,
     70  1.8.2.2  christos 	.d_flag = D_OTHER | D_MPSAFE
     71  1.8.2.2  christos };
     72  1.8.2.2  christos 
     73  1.8.2.2  christos static int kcov_fops_ioctl(file_t *, u_long, void *);
     74  1.8.2.2  christos static int kcov_fops_close(file_t *);
     75  1.8.2.2  christos static int kcov_fops_mmap(file_t *, off_t *, size_t, int, int *, int *,
     76  1.8.2.2  christos     struct uvm_object **, int *);
     77  1.8.2.2  christos 
     78  1.8.2.2  christos const struct fileops kcov_fileops = {
     79  1.8.2.2  christos 	.fo_read = fbadop_read,
     80  1.8.2.2  christos 	.fo_write = fbadop_write,
     81  1.8.2.2  christos 	.fo_ioctl = kcov_fops_ioctl,
     82  1.8.2.2  christos 	.fo_fcntl = fnullop_fcntl,
     83  1.8.2.2  christos 	.fo_poll = fnullop_poll,
     84  1.8.2.2  christos 	.fo_stat = fbadop_stat,
     85  1.8.2.2  christos 	.fo_close = kcov_fops_close,
     86  1.8.2.2  christos 	.fo_kqfilter = fnullop_kqfilter,
     87  1.8.2.2  christos 	.fo_restart = fnullop_restart,
     88  1.8.2.2  christos 	.fo_mmap = kcov_fops_mmap,
     89  1.8.2.2  christos };
     90  1.8.2.2  christos 
     91  1.8.2.2  christos /*
     92  1.8.2.2  christos  * The KCOV descriptors (KD) are allocated during open(), and are associated
     93  1.8.2.2  christos  * with a file descriptor.
     94  1.8.2.2  christos  *
     95  1.8.2.2  christos  * An LWP can 'enable' a KD. When this happens, this LWP becomes the owner of
     96  1.8.2.2  christos  * the KD, and no LWP can 'disable' this KD except the owner.
     97  1.8.2.2  christos  *
     98  1.8.2.2  christos  * A KD is freed when its file descriptor is closed _iff_ the KD is not active
     99  1.8.2.2  christos  * on an LWP. If it is, we ask the LWP to free it when it exits.
    100  1.8.2.2  christos  *
    101  1.8.2.2  christos  * The buffers mmapped are in a dedicated uobj, therefore there is no risk
    102  1.8.2.2  christos  * that the kernel frees a buffer still mmapped in a process: the uobj
    103  1.8.2.2  christos  * refcount will be non-zero, so the backing is not freed until an munmap
    104  1.8.2.2  christos  * occurs on said process.
    105  1.8.2.2  christos  */
    106  1.8.2.2  christos 
    107  1.8.2.2  christos typedef struct kcov_desc {
    108  1.8.2.2  christos 	kmutex_t lock;
    109  1.8.2.2  christos 	kcov_int_t *buf;
    110  1.8.2.2  christos 	struct uvm_object *uobj;
    111  1.8.2.2  christos 	size_t bufnent;
    112  1.8.2.2  christos 	size_t bufsize;
    113  1.8.2.2  christos 	int mode;
    114  1.8.2.2  christos 	bool enabled;
    115  1.8.2.2  christos 	bool lwpfree;
    116  1.8.2.2  christos } kcov_t;
    117  1.8.2.2  christos 
    118  1.8.2.2  christos static void
    119  1.8.2.2  christos kcov_lock(kcov_t *kd)
    120  1.8.2.2  christos {
    121  1.8.2.2  christos 
    122  1.8.2.2  christos 	mutex_enter(&kd->lock);
    123  1.8.2.2  christos }
    124  1.8.2.2  christos 
    125  1.8.2.2  christos static void
    126  1.8.2.2  christos kcov_unlock(kcov_t *kd)
    127  1.8.2.2  christos {
    128  1.8.2.2  christos 
    129  1.8.2.2  christos 	mutex_exit(&kd->lock);
    130  1.8.2.2  christos }
    131  1.8.2.2  christos 
    132  1.8.2.2  christos static void
    133  1.8.2.2  christos kcov_free(kcov_t *kd)
    134  1.8.2.2  christos {
    135  1.8.2.2  christos 
    136  1.8.2.2  christos 	KASSERT(kd != NULL);
    137  1.8.2.2  christos 	if (kd->buf != NULL) {
    138  1.8.2.2  christos 		uvm_deallocate(kernel_map, (vaddr_t)kd->buf, kd->bufsize);
    139  1.8.2.2  christos 	}
    140  1.8.2.2  christos 	mutex_destroy(&kd->lock);
    141  1.8.2.2  christos 	kmem_free(kd, sizeof(*kd));
    142  1.8.2.2  christos }
    143  1.8.2.2  christos 
    144  1.8.2.3    martin void
    145  1.8.2.3    martin kcov_lwp_free(struct lwp *l)
    146  1.8.2.2  christos {
    147  1.8.2.3    martin 	kcov_t *kd = (kcov_t *)l->l_kcov;
    148  1.8.2.2  christos 
    149  1.8.2.2  christos 	if (kd == NULL) {
    150  1.8.2.2  christos 		return;
    151  1.8.2.2  christos 	}
    152  1.8.2.2  christos 	kcov_lock(kd);
    153  1.8.2.2  christos 	kd->enabled = false;
    154  1.8.2.2  christos 	kcov_unlock(kd);
    155  1.8.2.2  christos 	if (kd->lwpfree) {
    156  1.8.2.2  christos 		kcov_free(kd);
    157  1.8.2.2  christos 	}
    158  1.8.2.2  christos }
    159  1.8.2.2  christos 
    160  1.8.2.2  christos static int
    161  1.8.2.2  christos kcov_allocbuf(kcov_t *kd, uint64_t nent)
    162  1.8.2.2  christos {
    163  1.8.2.2  christos 	size_t size;
    164  1.8.2.2  christos 	int error;
    165  1.8.2.2  christos 
    166  1.8.2.2  christos 	if (nent < 2 || nent > KCOV_BUF_MAX_ENTRIES)
    167  1.8.2.2  christos 		return EINVAL;
    168  1.8.2.2  christos 	if (kd->buf != NULL)
    169  1.8.2.2  christos 		return EEXIST;
    170  1.8.2.2  christos 
    171  1.8.2.2  christos 	size = roundup(nent * KCOV_ENTRY_SIZE, PAGE_SIZE);
    172  1.8.2.2  christos 	kd->bufnent = nent - 1;
    173  1.8.2.2  christos 	kd->bufsize = size;
    174  1.8.2.2  christos 	kd->uobj = uao_create(kd->bufsize, 0);
    175  1.8.2.2  christos 
    176  1.8.2.2  christos 	/* Map the uobj into the kernel address space, as wired. */
    177  1.8.2.2  christos 	kd->buf = NULL;
    178  1.8.2.2  christos 	error = uvm_map(kernel_map, (vaddr_t *)&kd->buf, kd->bufsize, kd->uobj,
    179  1.8.2.2  christos 	    0, 0, UVM_MAPFLAG(UVM_PROT_RW, UVM_PROT_RW, UVM_INH_SHARE,
    180  1.8.2.2  christos 	    UVM_ADV_RANDOM, 0));
    181  1.8.2.2  christos 	if (error) {
    182  1.8.2.2  christos 		uao_detach(kd->uobj);
    183  1.8.2.2  christos 		return error;
    184  1.8.2.2  christos 	}
    185  1.8.2.2  christos 	error = uvm_map_pageable(kernel_map, (vaddr_t)kd->buf,
    186  1.8.2.2  christos 	    (vaddr_t)kd->buf + size, false, 0);
    187  1.8.2.2  christos 	if (error) {
    188  1.8.2.2  christos 		uvm_deallocate(kernel_map, (vaddr_t)kd->buf, size);
    189  1.8.2.2  christos 		return error;
    190  1.8.2.2  christos 	}
    191  1.8.2.2  christos 
    192  1.8.2.2  christos 	return 0;
    193  1.8.2.2  christos }
    194  1.8.2.2  christos 
    195  1.8.2.2  christos /* -------------------------------------------------------------------------- */
    196  1.8.2.2  christos 
    197  1.8.2.2  christos static int
    198  1.8.2.2  christos kcov_open(dev_t dev, int flag, int mode, struct lwp *l)
    199  1.8.2.2  christos {
    200  1.8.2.2  christos 	struct file *fp;
    201  1.8.2.2  christos 	int error, fd;
    202  1.8.2.2  christos 	kcov_t *kd;
    203  1.8.2.2  christos 
    204  1.8.2.2  christos 	error = fd_allocfile(&fp, &fd);
    205  1.8.2.2  christos 	if (error)
    206  1.8.2.2  christos 		return error;
    207  1.8.2.2  christos 
    208  1.8.2.2  christos 	kd = kmem_zalloc(sizeof(*kd), KM_SLEEP);
    209  1.8.2.2  christos 	mutex_init(&kd->lock, MUTEX_DEFAULT, IPL_NONE);
    210  1.8.2.2  christos 
    211  1.8.2.2  christos 	return fd_clone(fp, fd, flag, &kcov_fileops, kd);
    212  1.8.2.2  christos }
    213  1.8.2.2  christos 
    214  1.8.2.2  christos static int
    215  1.8.2.2  christos kcov_fops_close(file_t *fp)
    216  1.8.2.2  christos {
    217  1.8.2.2  christos 	kcov_t *kd = fp->f_data;
    218  1.8.2.2  christos 
    219  1.8.2.2  christos 	kcov_lock(kd);
    220  1.8.2.2  christos 	if (kd->enabled) {
    221  1.8.2.2  christos 		kd->lwpfree = true;
    222  1.8.2.2  christos 		kcov_unlock(kd);
    223  1.8.2.2  christos 	} else {
    224  1.8.2.2  christos 		kcov_unlock(kd);
    225  1.8.2.2  christos 		kcov_free(kd);
    226  1.8.2.2  christos 	}
    227  1.8.2.2  christos 	fp->f_data = NULL;
    228  1.8.2.2  christos 
    229  1.8.2.2  christos    	return 0;
    230  1.8.2.2  christos }
    231  1.8.2.2  christos 
    232  1.8.2.2  christos static int
    233  1.8.2.2  christos kcov_fops_ioctl(file_t *fp, u_long cmd, void *addr)
    234  1.8.2.2  christos {
    235  1.8.2.3    martin 	struct lwp *l = curlwp;
    236  1.8.2.2  christos 	int error = 0;
    237  1.8.2.2  christos 	int mode;
    238  1.8.2.2  christos 	kcov_t *kd;
    239  1.8.2.2  christos 
    240  1.8.2.2  christos 	kd = fp->f_data;
    241  1.8.2.2  christos 	if (kd == NULL)
    242  1.8.2.2  christos 		return ENXIO;
    243  1.8.2.2  christos 	kcov_lock(kd);
    244  1.8.2.2  christos 
    245  1.8.2.2  christos 	switch (cmd) {
    246  1.8.2.2  christos 	case KCOV_IOC_SETBUFSIZE:
    247  1.8.2.2  christos 		if (kd->enabled) {
    248  1.8.2.2  christos 			error = EBUSY;
    249  1.8.2.2  christos 			break;
    250  1.8.2.2  christos 		}
    251  1.8.2.2  christos 		error = kcov_allocbuf(kd, *((uint64_t *)addr));
    252  1.8.2.2  christos 		break;
    253  1.8.2.2  christos 	case KCOV_IOC_ENABLE:
    254  1.8.2.2  christos 		if (kd->enabled) {
    255  1.8.2.2  christos 			error = EBUSY;
    256  1.8.2.2  christos 			break;
    257  1.8.2.2  christos 		}
    258  1.8.2.3    martin 		if (l->l_kcov != NULL) {
    259  1.8.2.2  christos 			error = EBUSY;
    260  1.8.2.2  christos 			break;
    261  1.8.2.2  christos 		}
    262  1.8.2.2  christos 		if (kd->buf == NULL) {
    263  1.8.2.2  christos 			error = ENOBUFS;
    264  1.8.2.2  christos 			break;
    265  1.8.2.2  christos 		}
    266  1.8.2.2  christos 
    267  1.8.2.2  christos 		mode = *((int *)addr);
    268  1.8.2.2  christos 		switch (mode) {
    269  1.8.2.2  christos 		case KCOV_MODE_NONE:
    270  1.8.2.2  christos 		case KCOV_MODE_TRACE_PC:
    271  1.8.2.2  christos 		case KCOV_MODE_TRACE_CMP:
    272  1.8.2.2  christos 			kd->mode = mode;
    273  1.8.2.2  christos 			break;
    274  1.8.2.2  christos 		default:
    275  1.8.2.2  christos 			error = EINVAL;
    276  1.8.2.2  christos 		}
    277  1.8.2.2  christos 		if (error)
    278  1.8.2.2  christos 			break;
    279  1.8.2.2  christos 
    280  1.8.2.3    martin 		l->l_kcov = kd;
    281  1.8.2.2  christos 		kd->enabled = true;
    282  1.8.2.2  christos 		break;
    283  1.8.2.2  christos 	case KCOV_IOC_DISABLE:
    284  1.8.2.2  christos 		if (!kd->enabled) {
    285  1.8.2.2  christos 			error = ENOENT;
    286  1.8.2.2  christos 			break;
    287  1.8.2.2  christos 		}
    288  1.8.2.3    martin 		if (l->l_kcov != kd) {
    289  1.8.2.2  christos 			error = ENOENT;
    290  1.8.2.2  christos 			break;
    291  1.8.2.2  christos 		}
    292  1.8.2.3    martin 		l->l_kcov = NULL;
    293  1.8.2.2  christos 		kd->enabled = false;
    294  1.8.2.2  christos 		break;
    295  1.8.2.2  christos 	default:
    296  1.8.2.2  christos 		error = EINVAL;
    297  1.8.2.2  christos 	}
    298  1.8.2.2  christos 
    299  1.8.2.2  christos 	kcov_unlock(kd);
    300  1.8.2.2  christos 	return error;
    301  1.8.2.2  christos }
    302  1.8.2.2  christos 
    303  1.8.2.2  christos static int
    304  1.8.2.2  christos kcov_fops_mmap(file_t *fp, off_t *offp, size_t size, int prot, int *flagsp,
    305  1.8.2.2  christos     int *advicep, struct uvm_object **uobjp, int *maxprotp)
    306  1.8.2.2  christos {
    307  1.8.2.2  christos 	off_t off = *offp;
    308  1.8.2.2  christos 	kcov_t *kd;
    309  1.8.2.2  christos 	int error = 0;
    310  1.8.2.2  christos 
    311  1.8.2.2  christos 	if (prot & PROT_EXEC)
    312  1.8.2.2  christos 		return EACCES;
    313  1.8.2.2  christos 	if (off < 0)
    314  1.8.2.2  christos 		return EINVAL;
    315  1.8.2.2  christos 	if (size > KCOV_BUF_MAX_ENTRIES * KCOV_ENTRY_SIZE)
    316  1.8.2.2  christos 		return EINVAL;
    317  1.8.2.2  christos 	if (off > KCOV_BUF_MAX_ENTRIES * KCOV_ENTRY_SIZE)
    318  1.8.2.2  christos 		return EINVAL;
    319  1.8.2.2  christos 
    320  1.8.2.2  christos 	kd = fp->f_data;
    321  1.8.2.2  christos 	if (kd == NULL)
    322  1.8.2.2  christos 		return ENXIO;
    323  1.8.2.2  christos 	kcov_lock(kd);
    324  1.8.2.2  christos 
    325  1.8.2.2  christos 	if ((size + off) > kd->bufsize) {
    326  1.8.2.2  christos 		error = ENOMEM;
    327  1.8.2.2  christos 		goto out;
    328  1.8.2.2  christos 	}
    329  1.8.2.2  christos 
    330  1.8.2.2  christos 	uao_reference(kd->uobj);
    331  1.8.2.2  christos 
    332  1.8.2.2  christos 	*uobjp = kd->uobj;
    333  1.8.2.2  christos 	*maxprotp = prot;
    334  1.8.2.2  christos 	*advicep = UVM_ADV_RANDOM;
    335  1.8.2.2  christos 
    336  1.8.2.2  christos out:
    337  1.8.2.2  christos 	kcov_unlock(kd);
    338  1.8.2.2  christos 	return error;
    339  1.8.2.2  christos }
    340  1.8.2.2  christos 
    341  1.8.2.3    martin /* -------------------------------------------------------------------------- */
    342  1.8.2.3    martin 
    343  1.8.2.3    martin /*
    344  1.8.2.3    martin  * Constraints on the functions here: they must be marked with __nomsan, and
    345  1.8.2.3    martin  * must not make any external call.
    346  1.8.2.3    martin  */
    347  1.8.2.3    martin 
    348  1.8.2.3    martin static inline bool __nomsan
    349  1.8.2.2  christos in_interrupt(void)
    350  1.8.2.2  christos {
    351  1.8.2.2  christos 	return curcpu()->ci_idepth >= 0;
    352  1.8.2.2  christos }
    353  1.8.2.2  christos 
    354  1.8.2.2  christos void __sanitizer_cov_trace_pc(void);
    355  1.8.2.2  christos 
    356  1.8.2.3    martin void __nomsan
    357  1.8.2.2  christos __sanitizer_cov_trace_pc(void)
    358  1.8.2.2  christos {
    359  1.8.2.2  christos 	extern int cold;
    360  1.8.2.2  christos 	uint64_t idx;
    361  1.8.2.2  christos 	kcov_t *kd;
    362  1.8.2.2  christos 
    363  1.8.2.2  christos 	if (__predict_false(cold)) {
    364  1.8.2.2  christos 		/* Do not trace during boot. */
    365  1.8.2.2  christos 		return;
    366  1.8.2.2  christos 	}
    367  1.8.2.2  christos 
    368  1.8.2.2  christos 	if (in_interrupt()) {
    369  1.8.2.2  christos 		/* Do not trace in interrupts. */
    370  1.8.2.2  christos 		return;
    371  1.8.2.2  christos 	}
    372  1.8.2.2  christos 
    373  1.8.2.3    martin 	kd = curlwp->l_kcov;
    374  1.8.2.2  christos 	if (__predict_true(kd == NULL)) {
    375  1.8.2.2  christos 		/* Not traced. */
    376  1.8.2.2  christos 		return;
    377  1.8.2.2  christos 	}
    378  1.8.2.2  christos 
    379  1.8.2.2  christos 	if (!kd->enabled) {
    380  1.8.2.2  christos 		/* Tracing not enabled */
    381  1.8.2.2  christos 		return;
    382  1.8.2.2  christos 	}
    383  1.8.2.2  christos 
    384  1.8.2.2  christos 	if (kd->mode != KCOV_MODE_TRACE_PC) {
    385  1.8.2.2  christos 		/* PC tracing mode not enabled */
    386  1.8.2.2  christos 		return;
    387  1.8.2.2  christos 	}
    388  1.8.2.2  christos 
    389  1.8.2.2  christos 	idx = kd->buf[0];
    390  1.8.2.2  christos 	if (idx < kd->bufnent) {
    391  1.8.2.2  christos 		kd->buf[idx+1] =
    392  1.8.2.2  christos 		    (intptr_t)__builtin_return_address(0);
    393  1.8.2.2  christos 		kd->buf[0] = idx + 1;
    394  1.8.2.2  christos 	}
    395  1.8.2.2  christos }
    396  1.8.2.2  christos 
    397  1.8.2.3    martin static void __nomsan
    398  1.8.2.2  christos trace_cmp(uint64_t type, uint64_t arg1, uint64_t arg2, intptr_t pc)
    399  1.8.2.2  christos {
    400  1.8.2.2  christos 	extern int cold;
    401  1.8.2.2  christos 	uint64_t idx;
    402  1.8.2.2  christos 	kcov_t *kd;
    403  1.8.2.2  christos 
    404  1.8.2.2  christos 	if (__predict_false(cold)) {
    405  1.8.2.2  christos 		/* Do not trace during boot. */
    406  1.8.2.2  christos 		return;
    407  1.8.2.2  christos 	}
    408  1.8.2.2  christos 
    409  1.8.2.2  christos 	if (in_interrupt()) {
    410  1.8.2.2  christos 		/* Do not trace in interrupts. */
    411  1.8.2.2  christos 		return;
    412  1.8.2.2  christos 	}
    413  1.8.2.2  christos 
    414  1.8.2.3    martin 	kd = curlwp->l_kcov;
    415  1.8.2.2  christos 	if (__predict_true(kd == NULL)) {
    416  1.8.2.2  christos 		/* Not traced. */
    417  1.8.2.2  christos 		return;
    418  1.8.2.2  christos 	}
    419  1.8.2.2  christos 
    420  1.8.2.2  christos 	if (!kd->enabled) {
    421  1.8.2.2  christos 		/* Tracing not enabled */
    422  1.8.2.2  christos 		return;
    423  1.8.2.2  christos 	}
    424  1.8.2.2  christos 
    425  1.8.2.2  christos 	if (kd->mode != KCOV_MODE_TRACE_CMP) {
    426  1.8.2.2  christos 		/* CMP tracing mode not enabled */
    427  1.8.2.2  christos 		return;
    428  1.8.2.2  christos 	}
    429  1.8.2.2  christos 
    430  1.8.2.2  christos 	idx = kd->buf[0];
    431  1.8.2.2  christos 	if ((idx * 4 + 4) <= kd->bufnent) {
    432  1.8.2.2  christos 		kd->buf[idx * 4 + 1] = type;
    433  1.8.2.2  christos 		kd->buf[idx * 4 + 2] = arg1;
    434  1.8.2.2  christos 		kd->buf[idx * 4 + 3] = arg2;
    435  1.8.2.2  christos 		kd->buf[idx * 4 + 4] = pc;
    436  1.8.2.2  christos 		kd->buf[0] = idx + 1;
    437  1.8.2.2  christos 	}
    438  1.8.2.2  christos }
    439  1.8.2.2  christos 
    440  1.8.2.2  christos void __sanitizer_cov_trace_cmp1(uint8_t arg1, uint8_t arg2);
    441  1.8.2.2  christos 
    442  1.8.2.3    martin void __nomsan
    443  1.8.2.2  christos __sanitizer_cov_trace_cmp1(uint8_t arg1, uint8_t arg2)
    444  1.8.2.2  christos {
    445  1.8.2.2  christos 
    446  1.8.2.2  christos 	trace_cmp(KCOV_CMP_SIZE(0), arg1, arg2,
    447  1.8.2.2  christos 	    (intptr_t)__builtin_return_address(0));
    448  1.8.2.2  christos }
    449  1.8.2.2  christos 
    450  1.8.2.2  christos void __sanitizer_cov_trace_cmp2(uint16_t arg1, uint16_t arg2);
    451  1.8.2.2  christos 
    452  1.8.2.3    martin void __nomsan
    453  1.8.2.2  christos __sanitizer_cov_trace_cmp2(uint16_t arg1, uint16_t arg2)
    454  1.8.2.2  christos {
    455  1.8.2.2  christos 
    456  1.8.2.2  christos 	trace_cmp(KCOV_CMP_SIZE(1), arg1, arg2,
    457  1.8.2.2  christos 	    (intptr_t)__builtin_return_address(0));
    458  1.8.2.2  christos }
    459  1.8.2.2  christos 
    460  1.8.2.2  christos void __sanitizer_cov_trace_cmp4(uint32_t arg1, uint32_t arg2);
    461  1.8.2.2  christos 
    462  1.8.2.3    martin void __nomsan
    463  1.8.2.2  christos __sanitizer_cov_trace_cmp4(uint32_t arg1, uint32_t arg2)
    464  1.8.2.2  christos {
    465  1.8.2.2  christos 
    466  1.8.2.2  christos 	trace_cmp(KCOV_CMP_SIZE(2), arg1, arg2,
    467  1.8.2.2  christos 	    (intptr_t)__builtin_return_address(0));
    468  1.8.2.2  christos }
    469  1.8.2.2  christos 
    470  1.8.2.2  christos void __sanitizer_cov_trace_cmp8(uint64_t arg1, uint64_t arg2);
    471  1.8.2.2  christos 
    472  1.8.2.3    martin void __nomsan
    473  1.8.2.2  christos __sanitizer_cov_trace_cmp8(uint64_t arg1, uint64_t arg2)
    474  1.8.2.2  christos {
    475  1.8.2.2  christos 
    476  1.8.2.2  christos 	trace_cmp(KCOV_CMP_SIZE(3), arg1, arg2,
    477  1.8.2.2  christos 	    (intptr_t)__builtin_return_address(0));
    478  1.8.2.2  christos }
    479  1.8.2.2  christos 
    480  1.8.2.2  christos void __sanitizer_cov_trace_const_cmp1(uint8_t arg1, uint8_t arg2);
    481  1.8.2.2  christos 
    482  1.8.2.3    martin void __nomsan
    483  1.8.2.2  christos __sanitizer_cov_trace_const_cmp1(uint8_t arg1, uint8_t arg2)
    484  1.8.2.2  christos {
    485  1.8.2.2  christos 
    486  1.8.2.2  christos 	trace_cmp(KCOV_CMP_SIZE(0) | KCOV_CMP_CONST, arg1, arg2,
    487  1.8.2.2  christos 	    (intptr_t)__builtin_return_address(0));
    488  1.8.2.2  christos }
    489  1.8.2.2  christos 
    490  1.8.2.2  christos void __sanitizer_cov_trace_const_cmp2(uint16_t arg1, uint16_t arg2);
    491  1.8.2.2  christos 
    492  1.8.2.3    martin void __nomsan
    493  1.8.2.2  christos __sanitizer_cov_trace_const_cmp2(uint16_t arg1, uint16_t arg2)
    494  1.8.2.2  christos {
    495  1.8.2.2  christos 
    496  1.8.2.2  christos 	trace_cmp(KCOV_CMP_SIZE(1) | KCOV_CMP_CONST, arg1, arg2,
    497  1.8.2.2  christos 	    (intptr_t)__builtin_return_address(0));
    498  1.8.2.2  christos }
    499  1.8.2.2  christos 
    500  1.8.2.2  christos void __sanitizer_cov_trace_const_cmp4(uint32_t arg1, uint32_t arg2);
    501  1.8.2.2  christos 
    502  1.8.2.3    martin void __nomsan
    503  1.8.2.2  christos __sanitizer_cov_trace_const_cmp4(uint32_t arg1, uint32_t arg2)
    504  1.8.2.2  christos {
    505  1.8.2.2  christos 
    506  1.8.2.2  christos 	trace_cmp(KCOV_CMP_SIZE(2) | KCOV_CMP_CONST, arg1, arg2,
    507  1.8.2.2  christos 	    (intptr_t)__builtin_return_address(0));
    508  1.8.2.2  christos }
    509  1.8.2.2  christos 
    510  1.8.2.2  christos void __sanitizer_cov_trace_const_cmp8(uint64_t arg1, uint64_t arg2);
    511  1.8.2.2  christos 
    512  1.8.2.3    martin void __nomsan
    513  1.8.2.2  christos __sanitizer_cov_trace_const_cmp8(uint64_t arg1, uint64_t arg2)
    514  1.8.2.2  christos {
    515  1.8.2.2  christos 
    516  1.8.2.2  christos 	trace_cmp(KCOV_CMP_SIZE(3) | KCOV_CMP_CONST, arg1, arg2,
    517  1.8.2.2  christos 	    (intptr_t)__builtin_return_address(0));
    518  1.8.2.2  christos }
    519  1.8.2.2  christos 
    520  1.8.2.2  christos void __sanitizer_cov_trace_switch(uint64_t val, uint64_t *cases);
    521  1.8.2.2  christos 
    522  1.8.2.3    martin void __nomsan
    523  1.8.2.2  christos __sanitizer_cov_trace_switch(uint64_t val, uint64_t *cases)
    524  1.8.2.2  christos {
    525  1.8.2.2  christos 	uint64_t i, nbits, ncases, type;
    526  1.8.2.2  christos 	intptr_t pc;
    527  1.8.2.2  christos 
    528  1.8.2.2  christos 	pc = (intptr_t)__builtin_return_address(0);
    529  1.8.2.2  christos 	ncases = cases[0];
    530  1.8.2.2  christos 	nbits = cases[1];
    531  1.8.2.2  christos 
    532  1.8.2.2  christos 	switch (nbits) {
    533  1.8.2.2  christos 	case 8:
    534  1.8.2.2  christos 		type = KCOV_CMP_SIZE(0);
    535  1.8.2.2  christos 		break;
    536  1.8.2.2  christos 	case 16:
    537  1.8.2.2  christos 		type = KCOV_CMP_SIZE(1);
    538  1.8.2.2  christos 		break;
    539  1.8.2.2  christos 	case 32:
    540  1.8.2.2  christos 		type = KCOV_CMP_SIZE(2);
    541  1.8.2.2  christos 		break;
    542  1.8.2.2  christos 	case 64:
    543  1.8.2.2  christos 		type = KCOV_CMP_SIZE(3);
    544  1.8.2.2  christos 		break;
    545  1.8.2.2  christos 	default:
    546  1.8.2.2  christos 		return;
    547  1.8.2.2  christos 	}
    548  1.8.2.2  christos 	type |= KCOV_CMP_CONST;
    549  1.8.2.2  christos 
    550  1.8.2.2  christos 	for (i = 0; i < ncases; i++)
    551  1.8.2.2  christos 		trace_cmp(type, cases[i + 2], val, pc);
    552  1.8.2.2  christos }
    553  1.8.2.2  christos 
    554  1.8.2.2  christos /* -------------------------------------------------------------------------- */
    555  1.8.2.2  christos 
    556  1.8.2.2  christos MODULE(MODULE_CLASS_MISC, kcov, NULL);
    557  1.8.2.2  christos 
    558  1.8.2.2  christos static int
    559  1.8.2.2  christos kcov_modcmd(modcmd_t cmd, void *arg)
    560  1.8.2.2  christos {
    561  1.8.2.2  christos 
    562  1.8.2.2  christos    	switch (cmd) {
    563  1.8.2.2  christos 	case MODULE_CMD_INIT:
    564  1.8.2.2  christos 		return 0;
    565  1.8.2.2  christos 	case MODULE_CMD_FINI:
    566  1.8.2.2  christos 		return EINVAL;
    567  1.8.2.2  christos 	default:
    568  1.8.2.2  christos 		return ENOTTY;
    569  1.8.2.2  christos 	}
    570  1.8.2.2  christos }
    571