Index: sys/conf/files =================================================================== --- sys/conf/files +++ sys/conf/files @@ -1812,6 +1812,7 @@ dev/hptiop/hptiop.c optional hptiop scbus dev/hwpmc/hwpmc_logging.c optional hwpmc dev/hwpmc/hwpmc_mod.c optional hwpmc +dev/hwpmc/hwpmc_vm.c optional hwpmc dev/hwpmc/hwpmc_soft.c optional hwpmc dev/ichiic/ig4_acpi.c optional ig4 acpi iicbus dev/ichiic/ig4_iic.c optional ig4 iicbus Index: sys/dev/hwpmc/hwpmc_vm.h =================================================================== --- /dev/null +++ sys/dev/hwpmc/hwpmc_vm.h @@ -0,0 +1,54 @@ +/*- + * Copyright (c) 2017 Ruslan Bukin + * All rights reserved. + * + * This software was developed by BAE Systems, the University of Cambridge + * Computer Laboratory, and Memorial University under DARPA/AFRL contract + * FA8650-15-C-7558 ("CADETS"), as part of the DARPA Transparent Computing + * (TC) research program. + * + * 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 AUTHOR 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 AUTHOR 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. + * + * $FreeBSD$ + */ + +#ifndef _DEV_HWPMC_VM_H_ +#define _DEV_HWPMC_VM_H_ + +int pmc_vm_initialize(struct pmc_mdep *md); +int pmc_vm_finalize(void); + +struct pmc_vm_map { + TAILQ_ENTRY(pmc_vm_map) map_next; + struct thread *t; + vm_object_t obj; + void * buf; +}; + +struct cdev_cpu { + struct pmc_mdep *md; + struct mtx vm_mtx; + TAILQ_HEAD(, pmc_vm_map) pmc_maplist; + uint32_t cpu; +}; + +#endif /* !_DEV_HWPMC_VM_H_ */ Index: sys/dev/hwpmc/hwpmc_vm.c =================================================================== --- /dev/null +++ sys/dev/hwpmc/hwpmc_vm.c @@ -0,0 +1,134 @@ +/*- + * Copyright (c) 2017 Ruslan Bukin + * All rights reserved. + * + * This software was developed by BAE Systems, the University of Cambridge + * Computer Laboratory, and Memorial University under DARPA/AFRL contract + * FA8650-15-C-7558 ("CADETS"), as part of the DARPA Transparent Computing + * (TC) research program. + * + * 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 AUTHOR 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 AUTHOR 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. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#define PMC_VM_DEBUG +#undef PMC_VM_DEBUG + +#ifdef PMC_VM_DEBUG +#define dprintf(fmt, ...) printf(fmt, ##__VA_ARGS__) +#else +#define dprintf(fmt, ...) +#endif + +#include "hwpmc_vm.h" + +struct cdev *pmc_cdev[MAXCPU]; + +static int +pmc_mmap_single(struct cdev *cdev, vm_ooffset_t *offset, + vm_size_t mapsize, struct vm_object **objp, int nprot) +{ + struct pmc_vm_map *map, *map_tmp; + struct cdev_cpu *cc; + + cc = cdev->si_drv1; + + if (nprot != PROT_READ || *offset != 0) + return (ENXIO); + + mtx_lock(&cc->vm_mtx); + TAILQ_FOREACH_SAFE(map, &cc->pmc_maplist, map_next, map_tmp) { + if (map->t == curthread) { + mtx_unlock(&cc->vm_mtx); + *objp = map->obj; + return (0); + } + } + mtx_unlock(&cc->vm_mtx); + + return (ENXIO); +} + +static struct cdevsw pmc_cdevsw = { + .d_version = D_VERSION, + .d_mmap_single = pmc_mmap_single, + .d_name = "HWPMC", +}; + +int +pmc_vm_initialize(struct pmc_mdep *md) +{ + unsigned int maxcpu; + struct cdev_cpu *cc; + int cpu; + + maxcpu = pmc_cpu_max(); + + for (cpu = 0; cpu < maxcpu; cpu++) { + cc = malloc(sizeof(struct cdev_cpu), M_PMC, M_WAITOK | M_ZERO); + cc->cpu = cpu; + cc->md = md; + mtx_init(&cc->vm_mtx, "PMC VM", NULL, MTX_DEF); + TAILQ_INIT(&cc->pmc_maplist); + + pmc_cdev[cpu] = make_dev(&pmc_cdevsw, 0, UID_ROOT, GID_WHEEL, + 0666, "pmc%d", cpu); + pmc_cdev[cpu]->si_drv1 = cc; + } + + return (0); +} + +int +pmc_vm_finalize(void) +{ + unsigned int maxcpu; + struct cdev_cpu *cc; + int cpu; + + maxcpu = pmc_cpu_max(); + + for (cpu = 0; cpu < maxcpu; cpu++) { + cc = pmc_cdev[cpu]->si_drv1; + mtx_destroy(&cc->vm_mtx); + free(cc, M_PMC); + destroy_dev(pmc_cdev[cpu]); + } + + return (0); +} Index: sys/modules/hwpmc/Makefile =================================================================== --- sys/modules/hwpmc/Makefile +++ sys/modules/hwpmc/Makefile @@ -6,14 +6,16 @@ KMOD= hwpmc -SRCS= hwpmc_mod.c hwpmc_logging.c hwpmc_soft.c vnode_if.h +SRCS= hwpmc_mod.c hwpmc_logging.c hwpmc_soft.c hwpmc_vm.c vnode_if.h .if ${MACHINE_CPUARCH} == "aarch64" SRCS+= hwpmc_arm64.c hwpmc_arm64_md.c +SRCS+= hwpmc_cs.c .endif .if ${MACHINE_CPUARCH} == "amd64" SRCS+= hwpmc_amd.c hwpmc_core.c hwpmc_intel.c hwpmc_piv.c hwpmc_tsc.c +SRCS+= hwpmc_pt.c SRCS+= hwpmc_x86.c hwpmc_uncore.c SRCS+= device_if.h bus_if.h .endif