/*
* Copyright (c) 2019-2020 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Siddharth Muralee.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
static int kcov_fops_ioctl(file_t *, u_long, void *);
static int kcov_fops_close(file_t *);
static int kcov_fops_mmap(file_t *, off_t *, size_t, int, int *, int *,
struct uvm_object **, int *);
/*
* The KCOV descriptors (KD) are allocated during open(), and are associated
* with a file descriptor.
*
* An LWP can 'enable' a KD. When this happens, this LWP becomes the owner of
* the KD, and no LWP can 'disable' this KD except the owner.
*
* A KD is freed when its file descriptor is closed _iff_ the KD is not active
* on an LWP. If it is, we ask the LWP to free it when it exits.
*
* The buffers mmapped are in a dedicated uobj, therefore there is no risk
* that the kernel frees a buffer still mmapped in a process: the uobj
* refcount will be non-zero, so the backing is not freed until an munmap
* occurs on said process.
*/
typedef struct kcov_desc {
/* Local only */
kmutex_t lock;
bool lwpfree;
bool silenced;
/* Pointer to the end of the structure, if any */
struct kcov_desc *remote;
/* Can be remote */
kcov_int_t *buf;
struct uvm_object *uobj;
size_t bufnent;
size_t bufsize;
int mode;
bool enabled;
} kcov_t;
static int
kcov_fops_ioctl(file_t *fp, u_long cmd, void *addr)
{
kcov_t *kd;
int error;
kd = fp->f_data;
if (kd == NULL)
return ENXIO;
kcov_lock(kd);
switch (cmd) {
case KCOV_IOC_SETBUFSIZE:
error = kcov_setbufsize(kd, addr);
break;
case KCOV_IOC_ENABLE:
error = kcov_enable(kd, addr);
break;
case KCOV_IOC_DISABLE:
error = kcov_disable(kd);
break;
case KCOV_IOC_REMOTE_ATTACH:
error = kcov_remote_attach(kd, addr);
break;
case KCOV_IOC_REMOTE_DETACH:
error = kcov_remote_detach(kd);
break;
default:
error = EINVAL;
}
kcov_unlock(kd);
return error;
}
static int
kcov_fops_mmap(file_t *fp, off_t *offp, size_t size, int prot, int *flagsp,
int *advicep, struct uvm_object **uobjp, int *maxprotp)
{
off_t off = *offp;
kcov_t *kd, *kdbuf;
int error = 0;
KASSERT(size > 0);
if (prot & PROT_EXEC)
return EACCES;
if (off < 0)
return EINVAL;
if (size > KCOV_BUF_MAX_ENTRIES * KCOV_ENTRY_SIZE)
return EINVAL;
if (off > KCOV_BUF_MAX_ENTRIES * KCOV_ENTRY_SIZE)
return EINVAL;
kd = fp->f_data;
if (kd == NULL)
return ENXIO;
kcov_lock(kd);
pc = (intptr_t)__builtin_return_address(0);
ncases = cases[0];
nbits = cases[1];
switch (nbits) {
case 8:
type = KCOV_CMP_SIZE(0);
break;
case 16:
type = KCOV_CMP_SIZE(1);
break;
case 32:
type = KCOV_CMP_SIZE(2);
break;
case 64:
type = KCOV_CMP_SIZE(3);
break;
default:
return;
}
type |= KCOV_CMP_CONST;
for (i = 0; i < ncases; i++)
trace_cmp(type, cases[i + 2], val, pc);
}