hwpmc_mod.c revision 199972
1145256Sjkoshy/*- 2183266Sjkoshy * Copyright (c) 2003-2008 Joseph Koshy 3174395Sjkoshy * Copyright (c) 2007 The FreeBSD Foundation 4145256Sjkoshy * All rights reserved. 5145256Sjkoshy * 6174395Sjkoshy * Portions of this software were developed by A. Joseph Koshy under 7174395Sjkoshy * sponsorship from the FreeBSD Foundation and Google, Inc. 8174395Sjkoshy * 9145256Sjkoshy * Redistribution and use in source and binary forms, with or without 10145256Sjkoshy * modification, are permitted provided that the following conditions 11145256Sjkoshy * are met: 12145256Sjkoshy * 1. Redistributions of source code must retain the above copyright 13145256Sjkoshy * notice, this list of conditions and the following disclaimer. 14145256Sjkoshy * 2. Redistributions in binary form must reproduce the above copyright 15145256Sjkoshy * notice, this list of conditions and the following disclaimer in the 16145256Sjkoshy * documentation and/or other materials provided with the distribution. 17145256Sjkoshy * 18145256Sjkoshy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19145256Sjkoshy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20145256Sjkoshy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21145256Sjkoshy * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22145256Sjkoshy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23145256Sjkoshy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24145256Sjkoshy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25145256Sjkoshy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26145256Sjkoshy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27145256Sjkoshy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28145256Sjkoshy * SUCH DAMAGE. 29145256Sjkoshy * 30145256Sjkoshy */ 31145256Sjkoshy 32145256Sjkoshy#include <sys/cdefs.h> 33145256Sjkoshy__FBSDID("$FreeBSD: head/sys/dev/hwpmc/hwpmc_mod.c 199972 2009-11-30 20:41:30Z emaste $"); 34145256Sjkoshy 35145256Sjkoshy#include <sys/param.h> 36145256Sjkoshy#include <sys/eventhandler.h> 37145256Sjkoshy#include <sys/jail.h> 38145256Sjkoshy#include <sys/kernel.h> 39147191Sjkoshy#include <sys/kthread.h> 40145256Sjkoshy#include <sys/limits.h> 41145256Sjkoshy#include <sys/lock.h> 42145256Sjkoshy#include <sys/malloc.h> 43145256Sjkoshy#include <sys/module.h> 44145256Sjkoshy#include <sys/mutex.h> 45145256Sjkoshy#include <sys/pmc.h> 46145256Sjkoshy#include <sys/pmckern.h> 47147191Sjkoshy#include <sys/pmclog.h> 48164033Srwatson#include <sys/priv.h> 49145256Sjkoshy#include <sys/proc.h> 50145256Sjkoshy#include <sys/queue.h> 51147191Sjkoshy#include <sys/resourcevar.h> 52145256Sjkoshy#include <sys/sched.h> 53145256Sjkoshy#include <sys/signalvar.h> 54145256Sjkoshy#include <sys/smp.h> 55145256Sjkoshy#include <sys/sx.h> 56145256Sjkoshy#include <sys/sysctl.h> 57145256Sjkoshy#include <sys/sysent.h> 58145256Sjkoshy#include <sys/systm.h> 59147191Sjkoshy#include <sys/vnode.h> 60145256Sjkoshy 61157144Sjkoshy#include <sys/linker.h> /* needs to be after <sys/malloc.h> */ 62157144Sjkoshy 63147191Sjkoshy#include <machine/atomic.h> 64145256Sjkoshy#include <machine/md_var.h> 65145256Sjkoshy 66145256Sjkoshy/* 67145256Sjkoshy * Types 68145256Sjkoshy */ 69145256Sjkoshy 70145256Sjkoshyenum pmc_flags { 71145256Sjkoshy PMC_FLAG_NONE = 0x00, /* do nothing */ 72145256Sjkoshy PMC_FLAG_REMOVE = 0x01, /* atomically remove entry from hash */ 73145256Sjkoshy PMC_FLAG_ALLOCATE = 0x02, /* add entry to hash if not found */ 74145256Sjkoshy}; 75145256Sjkoshy 76145256Sjkoshy/* 77145256Sjkoshy * The offset in sysent where the syscall is allocated. 78145256Sjkoshy */ 79145256Sjkoshy 80145256Sjkoshystatic int pmc_syscall_num = NO_SYSCALL; 81145256Sjkoshystruct pmc_cpu **pmc_pcpu; /* per-cpu state */ 82145256Sjkoshypmc_value_t *pmc_pcpu_saved; /* saved PMC values: CSW handling */ 83145256Sjkoshy 84145256Sjkoshy#define PMC_PCPU_SAVED(C,R) pmc_pcpu_saved[(R) + md->pmd_npmc*(C)] 85145256Sjkoshy 86145256Sjkoshystruct mtx_pool *pmc_mtxpool; 87145256Sjkoshystatic int *pmc_pmcdisp; /* PMC row dispositions */ 88145256Sjkoshy 89145256Sjkoshy#define PMC_ROW_DISP_IS_FREE(R) (pmc_pmcdisp[(R)] == 0) 90145256Sjkoshy#define PMC_ROW_DISP_IS_THREAD(R) (pmc_pmcdisp[(R)] > 0) 91145256Sjkoshy#define PMC_ROW_DISP_IS_STANDALONE(R) (pmc_pmcdisp[(R)] < 0) 92145256Sjkoshy 93145256Sjkoshy#define PMC_MARK_ROW_FREE(R) do { \ 94145256Sjkoshy pmc_pmcdisp[(R)] = 0; \ 95145256Sjkoshy} while (0) 96145256Sjkoshy 97145256Sjkoshy#define PMC_MARK_ROW_STANDALONE(R) do { \ 98145256Sjkoshy KASSERT(pmc_pmcdisp[(R)] <= 0, ("[pmc,%d] row disposition error", \ 99145256Sjkoshy __LINE__)); \ 100145256Sjkoshy atomic_add_int(&pmc_pmcdisp[(R)], -1); \ 101183266Sjkoshy KASSERT(pmc_pmcdisp[(R)] >= (-pmc_cpu_max_active()), \ 102183266Sjkoshy ("[pmc,%d] row disposition error", __LINE__)); \ 103145256Sjkoshy} while (0) 104145256Sjkoshy 105145256Sjkoshy#define PMC_UNMARK_ROW_STANDALONE(R) do { \ 106145256Sjkoshy atomic_add_int(&pmc_pmcdisp[(R)], 1); \ 107145256Sjkoshy KASSERT(pmc_pmcdisp[(R)] <= 0, ("[pmc,%d] row disposition error", \ 108145256Sjkoshy __LINE__)); \ 109145256Sjkoshy} while (0) 110145256Sjkoshy 111145256Sjkoshy#define PMC_MARK_ROW_THREAD(R) do { \ 112145256Sjkoshy KASSERT(pmc_pmcdisp[(R)] >= 0, ("[pmc,%d] row disposition error", \ 113145256Sjkoshy __LINE__)); \ 114145256Sjkoshy atomic_add_int(&pmc_pmcdisp[(R)], 1); \ 115145256Sjkoshy} while (0) 116145256Sjkoshy 117145256Sjkoshy#define PMC_UNMARK_ROW_THREAD(R) do { \ 118145256Sjkoshy atomic_add_int(&pmc_pmcdisp[(R)], -1); \ 119145256Sjkoshy KASSERT(pmc_pmcdisp[(R)] >= 0, ("[pmc,%d] row disposition error", \ 120145256Sjkoshy __LINE__)); \ 121145256Sjkoshy} while (0) 122145256Sjkoshy 123145256Sjkoshy 124145256Sjkoshy/* various event handlers */ 125145256Sjkoshystatic eventhandler_tag pmc_exit_tag, pmc_fork_tag; 126145256Sjkoshy 127145256Sjkoshy/* Module statistics */ 128145256Sjkoshystruct pmc_op_getdriverstats pmc_stats; 129145256Sjkoshy 130145256Sjkoshy/* Machine/processor dependent operations */ 131185363Sjkoshystatic struct pmc_mdep *md; 132145256Sjkoshy 133145256Sjkoshy/* 134145256Sjkoshy * Hash tables mapping owner processes and target threads to PMCs. 135145256Sjkoshy */ 136145256Sjkoshy 137145256Sjkoshystruct mtx pmc_processhash_mtx; /* spin mutex */ 138145256Sjkoshystatic u_long pmc_processhashmask; 139145256Sjkoshystatic LIST_HEAD(pmc_processhash, pmc_process) *pmc_processhash; 140145256Sjkoshy 141145256Sjkoshy/* 142145256Sjkoshy * Hash table of PMC owner descriptors. This table is protected by 143145256Sjkoshy * the shared PMC "sx" lock. 144145256Sjkoshy */ 145145256Sjkoshy 146145256Sjkoshystatic u_long pmc_ownerhashmask; 147145256Sjkoshystatic LIST_HEAD(pmc_ownerhash, pmc_owner) *pmc_ownerhash; 148145256Sjkoshy 149145256Sjkoshy/* 150147191Sjkoshy * List of PMC owners with system-wide sampling PMCs. 151147191Sjkoshy */ 152147191Sjkoshy 153147191Sjkoshystatic LIST_HEAD(, pmc_owner) pmc_ss_owners; 154147191Sjkoshy 155147191Sjkoshy 156147191Sjkoshy/* 157184802Sjkoshy * A map of row indices to classdep structures. 158184802Sjkoshy */ 159184802Sjkoshystatic struct pmc_classdep **pmc_rowindex_to_classdep; 160184802Sjkoshy 161184802Sjkoshy/* 162145256Sjkoshy * Prototypes 163145256Sjkoshy */ 164145256Sjkoshy 165153110Sru#ifdef DEBUG 166145256Sjkoshystatic int pmc_debugflags_sysctl_handler(SYSCTL_HANDLER_ARGS); 167145256Sjkoshystatic int pmc_debugflags_parse(char *newstr, char *fence); 168145256Sjkoshy#endif 169145256Sjkoshy 170145256Sjkoshystatic int load(struct module *module, int cmd, void *arg); 171147191Sjkoshystatic int pmc_attach_process(struct proc *p, struct pmc *pm); 172145256Sjkoshystatic struct pmc *pmc_allocate_pmc_descriptor(void); 173147191Sjkoshystatic struct pmc_owner *pmc_allocate_owner_descriptor(struct proc *p); 174147191Sjkoshystatic int pmc_attach_one_process(struct proc *p, struct pmc *pm); 175147191Sjkoshystatic int pmc_can_allocate_rowindex(struct proc *p, unsigned int ri, 176147191Sjkoshy int cpu); 177147191Sjkoshystatic int pmc_can_attach(struct pmc *pm, struct proc *p); 178174395Sjkoshystatic void pmc_capture_user_callchain(int cpu, struct trapframe *tf); 179147191Sjkoshystatic void pmc_cleanup(void); 180147191Sjkoshystatic int pmc_detach_process(struct proc *p, struct pmc *pm); 181147191Sjkoshystatic int pmc_detach_one_process(struct proc *p, struct pmc *pm, 182147191Sjkoshy int flags); 183147191Sjkoshystatic void pmc_destroy_owner_descriptor(struct pmc_owner *po); 184147191Sjkoshystatic struct pmc_owner *pmc_find_owner_descriptor(struct proc *p); 185147191Sjkoshystatic int pmc_find_pmc(pmc_id_t pmcid, struct pmc **pm); 186145256Sjkoshystatic struct pmc *pmc_find_pmc_descriptor_in_process(struct pmc_owner *po, 187145256Sjkoshy pmc_id_t pmc); 188145256Sjkoshystatic struct pmc_process *pmc_find_process_descriptor(struct proc *p, 189145256Sjkoshy uint32_t mode); 190145774Sjkoshystatic void pmc_force_context_switch(void); 191145256Sjkoshystatic void pmc_link_target_process(struct pmc *pm, 192145256Sjkoshy struct pmc_process *pp); 193174395Sjkoshystatic void pmc_log_all_process_mappings(struct pmc_owner *po); 194174395Sjkoshystatic void pmc_log_kernel_mappings(struct pmc *pm); 195174395Sjkoshystatic void pmc_log_process_mappings(struct pmc_owner *po, struct proc *p); 196147191Sjkoshystatic void pmc_maybe_remove_owner(struct pmc_owner *po); 197147191Sjkoshystatic void pmc_process_csw_in(struct thread *td); 198147191Sjkoshystatic void pmc_process_csw_out(struct thread *td); 199145256Sjkoshystatic void pmc_process_exit(void *arg, struct proc *p); 200145256Sjkoshystatic void pmc_process_fork(void *arg, struct proc *p1, 201145256Sjkoshy struct proc *p2, int n); 202147191Sjkoshystatic void pmc_process_samples(int cpu); 203147191Sjkoshystatic void pmc_release_pmc_descriptor(struct pmc *pmc); 204147191Sjkoshystatic void pmc_remove_owner(struct pmc_owner *po); 205147191Sjkoshystatic void pmc_remove_process_descriptor(struct pmc_process *pp); 206147191Sjkoshystatic void pmc_restore_cpu_binding(struct pmc_binding *pb); 207147191Sjkoshystatic void pmc_save_cpu_binding(struct pmc_binding *pb); 208147191Sjkoshystatic void pmc_select_cpu(int cpu); 209145256Sjkoshystatic int pmc_start(struct pmc *pm); 210145256Sjkoshystatic int pmc_stop(struct pmc *pm); 211147191Sjkoshystatic int pmc_syscall_handler(struct thread *td, void *syscall_args); 212147191Sjkoshystatic void pmc_unlink_target_process(struct pmc *pmc, 213147191Sjkoshy struct pmc_process *pp); 214145256Sjkoshy 215145256Sjkoshy/* 216145256Sjkoshy * Kernel tunables and sysctl(8) interface. 217145256Sjkoshy */ 218145256Sjkoshy 219145256SjkoshySYSCTL_NODE(_kern, OID_AUTO, hwpmc, CTLFLAG_RW, 0, "HWPMC parameters"); 220145256Sjkoshy 221174395Sjkoshystatic int pmc_callchaindepth = PMC_CALLCHAIN_DEPTH; 222174395SjkoshyTUNABLE_INT(PMC_SYSCTL_NAME_PREFIX "callchaindepth", &pmc_callchaindepth); 223174395SjkoshySYSCTL_INT(_kern_hwpmc, OID_AUTO, callchaindepth, CTLFLAG_TUN|CTLFLAG_RD, 224174395Sjkoshy &pmc_callchaindepth, 0, "depth of call chain records"); 225174395Sjkoshy 226153110Sru#ifdef DEBUG 227147191Sjkoshystruct pmc_debugflags pmc_debugflags = PMC_DEBUG_DEFAULT_FLAGS; 228145256Sjkoshychar pmc_debugstr[PMC_DEBUG_STRSIZE]; 229145256SjkoshyTUNABLE_STR(PMC_SYSCTL_NAME_PREFIX "debugflags", pmc_debugstr, 230145256Sjkoshy sizeof(pmc_debugstr)); 231145256SjkoshySYSCTL_PROC(_kern_hwpmc, OID_AUTO, debugflags, 232145256Sjkoshy CTLTYPE_STRING|CTLFLAG_RW|CTLFLAG_TUN, 233145256Sjkoshy 0, 0, pmc_debugflags_sysctl_handler, "A", "debug flags"); 234145256Sjkoshy#endif 235145256Sjkoshy 236145256Sjkoshy/* 237147191Sjkoshy * kern.hwpmc.hashrows -- determines the number of rows in the 238145256Sjkoshy * of the hash table used to look up threads 239145256Sjkoshy */ 240145256Sjkoshy 241145256Sjkoshystatic int pmc_hashsize = PMC_HASH_SIZE; 242145256SjkoshyTUNABLE_INT(PMC_SYSCTL_NAME_PREFIX "hashsize", &pmc_hashsize); 243145256SjkoshySYSCTL_INT(_kern_hwpmc, OID_AUTO, hashsize, CTLFLAG_TUN|CTLFLAG_RD, 244145256Sjkoshy &pmc_hashsize, 0, "rows in hash tables"); 245145256Sjkoshy 246145256Sjkoshy/* 247174395Sjkoshy * kern.hwpmc.nsamples --- number of PC samples/callchain stacks per CPU 248145256Sjkoshy */ 249145256Sjkoshy 250147191Sjkoshystatic int pmc_nsamples = PMC_NSAMPLES; 251147191SjkoshyTUNABLE_INT(PMC_SYSCTL_NAME_PREFIX "nsamples", &pmc_nsamples); 252147191SjkoshySYSCTL_INT(_kern_hwpmc, OID_AUTO, nsamples, CTLFLAG_TUN|CTLFLAG_RD, 253147191Sjkoshy &pmc_nsamples, 0, "number of PC samples per CPU"); 254145256Sjkoshy 255174395Sjkoshy 256145256Sjkoshy/* 257147191Sjkoshy * kern.hwpmc.mtxpoolsize -- number of mutexes in the mutex pool. 258145256Sjkoshy */ 259145256Sjkoshy 260145256Sjkoshystatic int pmc_mtxpool_size = PMC_MTXPOOL_SIZE; 261145256SjkoshyTUNABLE_INT(PMC_SYSCTL_NAME_PREFIX "mtxpoolsize", &pmc_mtxpool_size); 262145256SjkoshySYSCTL_INT(_kern_hwpmc, OID_AUTO, mtxpoolsize, CTLFLAG_TUN|CTLFLAG_RD, 263145256Sjkoshy &pmc_mtxpool_size, 0, "size of spin mutex pool"); 264145256Sjkoshy 265145256Sjkoshy 266145256Sjkoshy/* 267145256Sjkoshy * security.bsd.unprivileged_syspmcs -- allow non-root processes to 268145256Sjkoshy * allocate system-wide PMCs. 269145256Sjkoshy * 270145256Sjkoshy * Allowing unprivileged processes to allocate system PMCs is convenient 271145256Sjkoshy * if system-wide measurements need to be taken concurrently with other 272145256Sjkoshy * per-process measurements. This feature is turned off by default. 273145256Sjkoshy */ 274145256Sjkoshy 275145256Sjkoshystatic int pmc_unprivileged_syspmcs = 0; 276145256SjkoshyTUNABLE_INT("security.bsd.unprivileged_syspmcs", &pmc_unprivileged_syspmcs); 277145256SjkoshySYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_syspmcs, CTLFLAG_RW, 278145256Sjkoshy &pmc_unprivileged_syspmcs, 0, 279145256Sjkoshy "allow unprivileged process to allocate system PMCs"); 280145256Sjkoshy 281147191Sjkoshy/* 282147191Sjkoshy * Hash function. Discard the lower 2 bits of the pointer since 283147191Sjkoshy * these are always zero for our uses. The hash multiplier is 284147191Sjkoshy * round((2^LONG_BIT) * ((sqrt(5)-1)/2)). 285147191Sjkoshy */ 286145256Sjkoshy 287145256Sjkoshy#if LONG_BIT == 64 288145256Sjkoshy#define _PMC_HM 11400714819323198486u 289145256Sjkoshy#elif LONG_BIT == 32 290145256Sjkoshy#define _PMC_HM 2654435769u 291145256Sjkoshy#else 292145256Sjkoshy#error Must know the size of 'long' to compile 293145256Sjkoshy#endif 294145256Sjkoshy 295145256Sjkoshy#define PMC_HASH_PTR(P,M) ((((unsigned long) (P) >> 2) * _PMC_HM) & (M)) 296145256Sjkoshy 297145256Sjkoshy/* 298145256Sjkoshy * Syscall structures 299145256Sjkoshy */ 300145256Sjkoshy 301145256Sjkoshy/* The `sysent' for the new syscall */ 302145256Sjkoshystatic struct sysent pmc_sysent = { 303145256Sjkoshy 2, /* sy_narg */ 304145256Sjkoshy pmc_syscall_handler /* sy_call */ 305145256Sjkoshy}; 306145256Sjkoshy 307145256Sjkoshystatic struct syscall_module_data pmc_syscall_mod = { 308145256Sjkoshy load, 309145256Sjkoshy NULL, 310145256Sjkoshy &pmc_syscall_num, 311145256Sjkoshy &pmc_sysent, 312145256Sjkoshy { 0, NULL } 313145256Sjkoshy}; 314145256Sjkoshy 315145256Sjkoshystatic moduledata_t pmc_mod = { 316145256Sjkoshy PMC_MODULE_NAME, 317145256Sjkoshy syscall_module_handler, 318145256Sjkoshy &pmc_syscall_mod 319145256Sjkoshy}; 320145256Sjkoshy 321145256SjkoshyDECLARE_MODULE(pmc, pmc_mod, SI_SUB_SMP, SI_ORDER_ANY); 322145256SjkoshyMODULE_VERSION(pmc, PMC_VERSION); 323145256Sjkoshy 324153110Sru#ifdef DEBUG 325147191Sjkoshyenum pmc_dbgparse_state { 326147191Sjkoshy PMCDS_WS, /* in whitespace */ 327147191Sjkoshy PMCDS_MAJOR, /* seen a major keyword */ 328147191Sjkoshy PMCDS_MINOR 329147191Sjkoshy}; 330147191Sjkoshy 331145256Sjkoshystatic int 332145256Sjkoshypmc_debugflags_parse(char *newstr, char *fence) 333145256Sjkoshy{ 334145313Sjkoshy char c, *p, *q; 335147191Sjkoshy struct pmc_debugflags *tmpflags; 336147191Sjkoshy int error, found, *newbits, tmp; 337147191Sjkoshy size_t kwlen; 338145256Sjkoshy 339184214Sdes tmpflags = malloc(sizeof(*tmpflags), M_PMC, M_WAITOK|M_ZERO); 340145256Sjkoshy 341145256Sjkoshy p = newstr; 342147191Sjkoshy error = 0; 343145256Sjkoshy 344147191Sjkoshy for (; p < fence && (c = *p); p++) { 345145256Sjkoshy 346147191Sjkoshy /* skip white space */ 347147191Sjkoshy if (c == ' ' || c == '\t') 348147191Sjkoshy continue; 349147191Sjkoshy 350147191Sjkoshy /* look for a keyword followed by "=" */ 351147191Sjkoshy for (q = p; p < fence && (c = *p) && c != '='; p++) 352147191Sjkoshy ; 353147191Sjkoshy if (c != '=') { 354147191Sjkoshy error = EINVAL; 355147191Sjkoshy goto done; 356145256Sjkoshy } 357145256Sjkoshy 358147191Sjkoshy kwlen = p - q; 359147191Sjkoshy newbits = NULL; 360145256Sjkoshy 361147191Sjkoshy /* lookup flag group name */ 362147191Sjkoshy#define DBG_SET_FLAG_MAJ(S,F) \ 363147191Sjkoshy if (kwlen == sizeof(S)-1 && strncmp(q, S, kwlen) == 0) \ 364147191Sjkoshy newbits = &tmpflags->pdb_ ## F; 365145256Sjkoshy 366147191Sjkoshy DBG_SET_FLAG_MAJ("cpu", CPU); 367147191Sjkoshy DBG_SET_FLAG_MAJ("csw", CSW); 368147191Sjkoshy DBG_SET_FLAG_MAJ("logging", LOG); 369147191Sjkoshy DBG_SET_FLAG_MAJ("module", MOD); 370147191Sjkoshy DBG_SET_FLAG_MAJ("md", MDP); 371147191Sjkoshy DBG_SET_FLAG_MAJ("owner", OWN); 372147191Sjkoshy DBG_SET_FLAG_MAJ("pmc", PMC); 373147191Sjkoshy DBG_SET_FLAG_MAJ("process", PRC); 374147191Sjkoshy DBG_SET_FLAG_MAJ("sampling", SAM); 375145256Sjkoshy 376147191Sjkoshy if (newbits == NULL) { 377147191Sjkoshy error = EINVAL; 378147191Sjkoshy goto done; 379145256Sjkoshy } 380145256Sjkoshy 381147191Sjkoshy p++; /* skip the '=' */ 382145256Sjkoshy 383147191Sjkoshy /* Now parse the individual flags */ 384147191Sjkoshy tmp = 0; 385147191Sjkoshy newflag: 386147191Sjkoshy for (q = p; p < fence && (c = *p); p++) 387147191Sjkoshy if (c == ' ' || c == '\t' || c == ',') 388147191Sjkoshy break; 389147191Sjkoshy 390147191Sjkoshy /* p == fence or c == ws or c == "," or c == 0 */ 391147191Sjkoshy 392147191Sjkoshy if ((kwlen = p - q) == 0) { 393147191Sjkoshy *newbits = tmp; 394147191Sjkoshy continue; 395147191Sjkoshy } 396147191Sjkoshy 397147191Sjkoshy found = 0; 398147191Sjkoshy#define DBG_SET_FLAG_MIN(S,F) \ 399147191Sjkoshy if (kwlen == sizeof(S)-1 && strncmp(q, S, kwlen) == 0) \ 400147191Sjkoshy tmp |= found = (1 << PMC_DEBUG_MIN_ ## F) 401147191Sjkoshy 402147191Sjkoshy /* a '*' denotes all possible flags in the group */ 403147191Sjkoshy if (kwlen == 1 && *q == '*') 404147191Sjkoshy tmp = found = ~0; 405147191Sjkoshy /* look for individual flag names */ 406147191Sjkoshy DBG_SET_FLAG_MIN("allocaterow", ALR); 407147191Sjkoshy DBG_SET_FLAG_MIN("allocate", ALL); 408147191Sjkoshy DBG_SET_FLAG_MIN("attach", ATT); 409147191Sjkoshy DBG_SET_FLAG_MIN("bind", BND); 410147191Sjkoshy DBG_SET_FLAG_MIN("config", CFG); 411147191Sjkoshy DBG_SET_FLAG_MIN("exec", EXC); 412147191Sjkoshy DBG_SET_FLAG_MIN("exit", EXT); 413147191Sjkoshy DBG_SET_FLAG_MIN("find", FND); 414147191Sjkoshy DBG_SET_FLAG_MIN("flush", FLS); 415147191Sjkoshy DBG_SET_FLAG_MIN("fork", FRK); 416147191Sjkoshy DBG_SET_FLAG_MIN("getbuf", GTB); 417147191Sjkoshy DBG_SET_FLAG_MIN("hook", PMH); 418147191Sjkoshy DBG_SET_FLAG_MIN("init", INI); 419147191Sjkoshy DBG_SET_FLAG_MIN("intr", INT); 420147191Sjkoshy DBG_SET_FLAG_MIN("linktarget", TLK); 421147191Sjkoshy DBG_SET_FLAG_MIN("mayberemove", OMR); 422147191Sjkoshy DBG_SET_FLAG_MIN("ops", OPS); 423147191Sjkoshy DBG_SET_FLAG_MIN("read", REA); 424147191Sjkoshy DBG_SET_FLAG_MIN("register", REG); 425147191Sjkoshy DBG_SET_FLAG_MIN("release", REL); 426147191Sjkoshy DBG_SET_FLAG_MIN("remove", ORM); 427147191Sjkoshy DBG_SET_FLAG_MIN("sample", SAM); 428147191Sjkoshy DBG_SET_FLAG_MIN("scheduleio", SIO); 429147191Sjkoshy DBG_SET_FLAG_MIN("select", SEL); 430147191Sjkoshy DBG_SET_FLAG_MIN("signal", SIG); 431147191Sjkoshy DBG_SET_FLAG_MIN("swi", SWI); 432147191Sjkoshy DBG_SET_FLAG_MIN("swo", SWO); 433147191Sjkoshy DBG_SET_FLAG_MIN("start", STA); 434147191Sjkoshy DBG_SET_FLAG_MIN("stop", STO); 435147191Sjkoshy DBG_SET_FLAG_MIN("syscall", PMS); 436147191Sjkoshy DBG_SET_FLAG_MIN("unlinktarget", TUL); 437147191Sjkoshy DBG_SET_FLAG_MIN("write", WRI); 438147191Sjkoshy if (found == 0) { 439147191Sjkoshy /* unrecognized flag name */ 440147191Sjkoshy error = EINVAL; 441147191Sjkoshy goto done; 442147191Sjkoshy } 443147191Sjkoshy 444147191Sjkoshy if (c == 0 || c == ' ' || c == '\t') { /* end of flag group */ 445147191Sjkoshy *newbits = tmp; 446147191Sjkoshy continue; 447147191Sjkoshy } 448147191Sjkoshy 449147191Sjkoshy p++; 450147191Sjkoshy goto newflag; 451145256Sjkoshy } 452145256Sjkoshy 453147191Sjkoshy /* save the new flag set */ 454147191Sjkoshy bcopy(tmpflags, &pmc_debugflags, sizeof(pmc_debugflags)); 455145256Sjkoshy 456147191Sjkoshy done: 457184205Sdes free(tmpflags, M_PMC); 458147191Sjkoshy return error; 459145256Sjkoshy} 460145256Sjkoshy 461145256Sjkoshystatic int 462145256Sjkoshypmc_debugflags_sysctl_handler(SYSCTL_HANDLER_ARGS) 463145256Sjkoshy{ 464145256Sjkoshy char *fence, *newstr; 465145256Sjkoshy int error; 466145256Sjkoshy unsigned int n; 467145256Sjkoshy 468145256Sjkoshy (void) arg1; (void) arg2; /* unused parameters */ 469145256Sjkoshy 470145256Sjkoshy n = sizeof(pmc_debugstr); 471184802Sjkoshy newstr = malloc(n, M_PMC, M_WAITOK|M_ZERO); 472147191Sjkoshy (void) strlcpy(newstr, pmc_debugstr, n); 473145256Sjkoshy 474145256Sjkoshy error = sysctl_handle_string(oidp, newstr, n, req); 475145256Sjkoshy 476145256Sjkoshy /* if there is a new string, parse and copy it */ 477145256Sjkoshy if (error == 0 && req->newptr != NULL) { 478147191Sjkoshy fence = newstr + (n < req->newlen ? n : req->newlen + 1); 479145256Sjkoshy if ((error = pmc_debugflags_parse(newstr, fence)) == 0) 480145256Sjkoshy (void) strlcpy(pmc_debugstr, newstr, 481145256Sjkoshy sizeof(pmc_debugstr)); 482145256Sjkoshy } 483145256Sjkoshy 484184205Sdes free(newstr, M_PMC); 485145256Sjkoshy 486145256Sjkoshy return error; 487145256Sjkoshy} 488145256Sjkoshy#endif 489145256Sjkoshy 490145256Sjkoshy/* 491184802Sjkoshy * Map a row index to a classdep structure and return the adjusted row 492184802Sjkoshy * index for the PMC class index. 493184802Sjkoshy */ 494184802Sjkoshystatic struct pmc_classdep * 495184802Sjkoshypmc_ri_to_classdep(struct pmc_mdep *md, int ri, int *adjri) 496184802Sjkoshy{ 497184802Sjkoshy struct pmc_classdep *pcd; 498184802Sjkoshy 499184802Sjkoshy (void) md; 500184802Sjkoshy 501184802Sjkoshy KASSERT(ri >= 0 && ri < md->pmd_npmc, 502184802Sjkoshy ("[pmc,%d] illegal row-index %d", __LINE__, ri)); 503184802Sjkoshy 504184802Sjkoshy pcd = pmc_rowindex_to_classdep[ri]; 505184802Sjkoshy 506184802Sjkoshy KASSERT(pcd != NULL, 507198204Srpaulo ("[pmc,%d] ri %d null pcd", __LINE__, ri)); 508184802Sjkoshy 509184802Sjkoshy *adjri = ri - pcd->pcd_ri; 510184802Sjkoshy 511184802Sjkoshy KASSERT(*adjri >= 0 && *adjri < pcd->pcd_num, 512184802Sjkoshy ("[pmc,%d] adjusted row-index %d", __LINE__, *adjri)); 513184802Sjkoshy 514184802Sjkoshy return (pcd); 515184802Sjkoshy} 516184802Sjkoshy 517184802Sjkoshy/* 518145256Sjkoshy * Concurrency Control 519145256Sjkoshy * 520145256Sjkoshy * The driver manages the following data structures: 521145256Sjkoshy * 522145256Sjkoshy * - target process descriptors, one per target process 523145256Sjkoshy * - owner process descriptors (and attached lists), one per owner process 524145256Sjkoshy * - lookup hash tables for owner and target processes 525145256Sjkoshy * - PMC descriptors (and attached lists) 526145256Sjkoshy * - per-cpu hardware state 527145256Sjkoshy * - the 'hook' variable through which the kernel calls into 528145256Sjkoshy * this module 529145256Sjkoshy * - the machine hardware state (managed by the MD layer) 530145256Sjkoshy * 531145256Sjkoshy * These data structures are accessed from: 532145256Sjkoshy * 533145256Sjkoshy * - thread context-switch code 534145256Sjkoshy * - interrupt handlers (possibly on multiple cpus) 535145256Sjkoshy * - kernel threads on multiple cpus running on behalf of user 536145256Sjkoshy * processes doing system calls 537145256Sjkoshy * - this driver's private kernel threads 538145256Sjkoshy * 539145256Sjkoshy * = Locks and Locking strategy = 540145256Sjkoshy * 541145256Sjkoshy * The driver uses four locking strategies for its operation: 542145256Sjkoshy * 543168856Sjkoshy * - The global SX lock "pmc_sx" is used to protect internal 544168856Sjkoshy * data structures. 545145256Sjkoshy * 546168856Sjkoshy * Calls into the module by syscall() start with this lock being 547168856Sjkoshy * held in exclusive mode. Depending on the requested operation, 548168856Sjkoshy * the lock may be downgraded to 'shared' mode to allow more 549168856Sjkoshy * concurrent readers into the module. Calls into the module from 550168856Sjkoshy * other parts of the kernel acquire the lock in shared mode. 551145256Sjkoshy * 552145256Sjkoshy * This SX lock is held in exclusive mode for any operations that 553145256Sjkoshy * modify the linkages between the driver's internal data structures. 554145256Sjkoshy * 555145256Sjkoshy * The 'pmc_hook' function pointer is also protected by this lock. 556145256Sjkoshy * It is only examined with the sx lock held in exclusive mode. The 557168856Sjkoshy * kernel module is allowed to be unloaded only with the sx lock held 558168856Sjkoshy * in exclusive mode. In normal syscall handling, after acquiring the 559168856Sjkoshy * pmc_sx lock we first check that 'pmc_hook' is non-null before 560168856Sjkoshy * proceeding. This prevents races between the thread unloading the module 561168856Sjkoshy * and other threads seeking to use the module. 562145256Sjkoshy * 563145256Sjkoshy * - Lookups of target process structures and owner process structures 564145256Sjkoshy * cannot use the global "pmc_sx" SX lock because these lookups need 565145256Sjkoshy * to happen during context switches and in other critical sections 566145256Sjkoshy * where sleeping is not allowed. We protect these lookup tables 567145256Sjkoshy * with their own private spin-mutexes, "pmc_processhash_mtx" and 568168856Sjkoshy * "pmc_ownerhash_mtx". 569145256Sjkoshy * 570145256Sjkoshy * - Interrupt handlers work in a lock free manner. At interrupt 571145256Sjkoshy * time, handlers look at the PMC pointer (phw->phw_pmc) configured 572145256Sjkoshy * when the PMC was started. If this pointer is NULL, the interrupt 573145256Sjkoshy * is ignored after updating driver statistics. We ensure that this 574145256Sjkoshy * pointer is set (using an atomic operation if necessary) before the 575145256Sjkoshy * PMC hardware is started. Conversely, this pointer is unset atomically 576145256Sjkoshy * only after the PMC hardware is stopped. 577145256Sjkoshy * 578145256Sjkoshy * We ensure that everything needed for the operation of an 579145256Sjkoshy * interrupt handler is available without it needing to acquire any 580145256Sjkoshy * locks. We also ensure that a PMC's software state is destroyed only 581145256Sjkoshy * after the PMC is taken off hardware (on all CPUs). 582145256Sjkoshy * 583145256Sjkoshy * - Context-switch handling with process-private PMCs needs more 584145256Sjkoshy * care. 585145256Sjkoshy * 586145256Sjkoshy * A given process may be the target of multiple PMCs. For example, 587145256Sjkoshy * PMCATTACH and PMCDETACH may be requested by a process on one CPU 588145256Sjkoshy * while the target process is running on another. A PMC could also 589145256Sjkoshy * be getting released because its owner is exiting. We tackle 590145256Sjkoshy * these situations in the following manner: 591145256Sjkoshy * 592145256Sjkoshy * - each target process structure 'pmc_process' has an array 593145256Sjkoshy * of 'struct pmc *' pointers, one for each hardware PMC. 594145256Sjkoshy * 595145256Sjkoshy * - At context switch IN time, each "target" PMC in RUNNING state 596145256Sjkoshy * gets started on hardware and a pointer to each PMC is copied into 597145256Sjkoshy * the per-cpu phw array. The 'runcount' for the PMC is 598145256Sjkoshy * incremented. 599145256Sjkoshy * 600145256Sjkoshy * - At context switch OUT time, all process-virtual PMCs are stopped 601145256Sjkoshy * on hardware. The saved value is added to the PMCs value field 602145256Sjkoshy * only if the PMC is in a non-deleted state (the PMCs state could 603145256Sjkoshy * have changed during the current time slice). 604145256Sjkoshy * 605145256Sjkoshy * Note that since in-between a switch IN on a processor and a switch 606145256Sjkoshy * OUT, the PMC could have been released on another CPU. Therefore 607145256Sjkoshy * context switch OUT always looks at the hardware state to turn 608145256Sjkoshy * OFF PMCs and will update a PMC's saved value only if reachable 609145256Sjkoshy * from the target process record. 610145256Sjkoshy * 611145256Sjkoshy * - OP PMCRELEASE could be called on a PMC at any time (the PMC could 612145256Sjkoshy * be attached to many processes at the time of the call and could 613145256Sjkoshy * be active on multiple CPUs). 614145256Sjkoshy * 615145256Sjkoshy * We prevent further scheduling of the PMC by marking it as in 616145256Sjkoshy * state 'DELETED'. If the runcount of the PMC is non-zero then 617145256Sjkoshy * this PMC is currently running on a CPU somewhere. The thread 618167086Sjhb * doing the PMCRELEASE operation waits by repeatedly doing a 619167086Sjhb * pause() till the runcount comes to zero. 620145256Sjkoshy * 621168856Sjkoshy * The contents of a PMC descriptor (struct pmc) are protected using 622168856Sjkoshy * a spin-mutex. In order to save space, we use a mutex pool. 623168856Sjkoshy * 624168856Sjkoshy * In terms of lock types used by witness(4), we use: 625168856Sjkoshy * - Type "pmc-sx", used by the global SX lock. 626168856Sjkoshy * - Type "pmc-sleep", for sleep mutexes used by logger threads. 627168856Sjkoshy * - Type "pmc-per-proc", for protecting PMC owner descriptors. 628168856Sjkoshy * - Type "pmc-leaf", used for all other spin mutexes. 629145256Sjkoshy */ 630145256Sjkoshy 631145256Sjkoshy/* 632145256Sjkoshy * save the cpu binding of the current kthread 633145256Sjkoshy */ 634145256Sjkoshy 635145256Sjkoshystatic void 636145256Sjkoshypmc_save_cpu_binding(struct pmc_binding *pb) 637145256Sjkoshy{ 638145256Sjkoshy PMCDBG(CPU,BND,2, "%s", "save-cpu"); 639170307Sjeff thread_lock(curthread); 640145256Sjkoshy pb->pb_bound = sched_is_bound(curthread); 641145256Sjkoshy pb->pb_cpu = curthread->td_oncpu; 642170307Sjeff thread_unlock(curthread); 643145256Sjkoshy PMCDBG(CPU,BND,2, "save-cpu cpu=%d", pb->pb_cpu); 644145256Sjkoshy} 645145256Sjkoshy 646145256Sjkoshy/* 647145256Sjkoshy * restore the cpu binding of the current thread 648145256Sjkoshy */ 649145256Sjkoshy 650145256Sjkoshystatic void 651145256Sjkoshypmc_restore_cpu_binding(struct pmc_binding *pb) 652145256Sjkoshy{ 653145256Sjkoshy PMCDBG(CPU,BND,2, "restore-cpu curcpu=%d restore=%d", 654145256Sjkoshy curthread->td_oncpu, pb->pb_cpu); 655170307Sjeff thread_lock(curthread); 656145256Sjkoshy if (pb->pb_bound) 657145256Sjkoshy sched_bind(curthread, pb->pb_cpu); 658145256Sjkoshy else 659145256Sjkoshy sched_unbind(curthread); 660170307Sjeff thread_unlock(curthread); 661145256Sjkoshy PMCDBG(CPU,BND,2, "%s", "restore-cpu done"); 662145256Sjkoshy} 663145256Sjkoshy 664145256Sjkoshy/* 665145256Sjkoshy * move execution over the specified cpu and bind it there. 666145256Sjkoshy */ 667145256Sjkoshy 668145256Sjkoshystatic void 669145256Sjkoshypmc_select_cpu(int cpu) 670145256Sjkoshy{ 671183266Sjkoshy KASSERT(cpu >= 0 && cpu < pmc_cpu_max(), 672145256Sjkoshy ("[pmc,%d] bad cpu number %d", __LINE__, cpu)); 673145256Sjkoshy 674183266Sjkoshy /* Never move to an inactive CPU. */ 675183266Sjkoshy KASSERT(pmc_cpu_is_active(cpu), ("[pmc,%d] selecting inactive " 676183266Sjkoshy "CPU %d", __LINE__, cpu)); 677145256Sjkoshy 678145256Sjkoshy PMCDBG(CPU,SEL,2, "select-cpu cpu=%d", cpu); 679170307Sjeff thread_lock(curthread); 680145256Sjkoshy sched_bind(curthread, cpu); 681170307Sjeff thread_unlock(curthread); 682145256Sjkoshy 683145256Sjkoshy KASSERT(curthread->td_oncpu == cpu, 684145256Sjkoshy ("[pmc,%d] CPU not bound [cpu=%d, curr=%d]", __LINE__, 685145256Sjkoshy cpu, curthread->td_oncpu)); 686145256Sjkoshy 687145256Sjkoshy PMCDBG(CPU,SEL,2, "select-cpu cpu=%d ok", cpu); 688145256Sjkoshy} 689145256Sjkoshy 690145256Sjkoshy/* 691145774Sjkoshy * Force a context switch. 692145774Sjkoshy * 693167086Sjhb * We do this by pause'ing for 1 tick -- invoking mi_switch() is not 694145774Sjkoshy * guaranteed to force a context switch. 695145774Sjkoshy */ 696145774Sjkoshy 697145774Sjkoshystatic void 698145774Sjkoshypmc_force_context_switch(void) 699145774Sjkoshy{ 700145774Sjkoshy 701167086Sjhb pause("pmcctx", 1); 702145774Sjkoshy} 703145774Sjkoshy 704145774Sjkoshy/* 705147191Sjkoshy * Get the file name for an executable. This is a simple wrapper 706147191Sjkoshy * around vn_fullpath(9). 707145256Sjkoshy */ 708145256Sjkoshy 709147191Sjkoshystatic void 710147708Sjkoshypmc_getfilename(struct vnode *v, char **fullpath, char **freepath) 711145256Sjkoshy{ 712145256Sjkoshy 713147191Sjkoshy *fullpath = "unknown"; 714147191Sjkoshy *freepath = NULL; 715175294Sattilio vn_fullpath(curthread, v, fullpath, freepath); 716145256Sjkoshy} 717145256Sjkoshy 718145256Sjkoshy/* 719145256Sjkoshy * remove an process owning PMCs 720145256Sjkoshy */ 721145256Sjkoshy 722145256Sjkoshyvoid 723145256Sjkoshypmc_remove_owner(struct pmc_owner *po) 724145256Sjkoshy{ 725147191Sjkoshy struct pmc *pm, *tmp; 726145256Sjkoshy 727145256Sjkoshy sx_assert(&pmc_sx, SX_XLOCKED); 728145256Sjkoshy 729145256Sjkoshy PMCDBG(OWN,ORM,1, "remove-owner po=%p", po); 730145256Sjkoshy 731145256Sjkoshy /* Remove descriptor from the owner hash table */ 732145256Sjkoshy LIST_REMOVE(po, po_next); 733145256Sjkoshy 734147191Sjkoshy /* release all owned PMC descriptors */ 735147191Sjkoshy LIST_FOREACH_SAFE(pm, &po->po_pmcs, pm_next, tmp) { 736147191Sjkoshy PMCDBG(OWN,ORM,2, "pmc=%p", pm); 737147191Sjkoshy KASSERT(pm->pm_owner == po, 738147191Sjkoshy ("[pmc,%d] owner %p != po %p", __LINE__, pm->pm_owner, po)); 739145256Sjkoshy 740147191Sjkoshy pmc_release_pmc_descriptor(pm); /* will unlink from the list */ 741145256Sjkoshy } 742145256Sjkoshy 743147191Sjkoshy KASSERT(po->po_sscount == 0, 744147191Sjkoshy ("[pmc,%d] SS count not zero", __LINE__)); 745145256Sjkoshy KASSERT(LIST_EMPTY(&po->po_pmcs), 746147191Sjkoshy ("[pmc,%d] PMC list not empty", __LINE__)); 747145256Sjkoshy 748147191Sjkoshy /* de-configure the log file if present */ 749145774Sjkoshy if (po->po_flags & PMC_PO_OWNS_LOGFILE) 750147191Sjkoshy pmclog_deconfigure_log(po); 751145256Sjkoshy} 752145256Sjkoshy 753145256Sjkoshy/* 754145256Sjkoshy * remove an owner process record if all conditions are met. 755145256Sjkoshy */ 756145256Sjkoshy 757145256Sjkoshystatic void 758145256Sjkoshypmc_maybe_remove_owner(struct pmc_owner *po) 759145256Sjkoshy{ 760145256Sjkoshy 761145256Sjkoshy PMCDBG(OWN,OMR,1, "maybe-remove-owner po=%p", po); 762145256Sjkoshy 763145256Sjkoshy /* 764145256Sjkoshy * Remove owner record if 765145256Sjkoshy * - this process does not own any PMCs 766145256Sjkoshy * - this process has not allocated a system-wide sampling buffer 767145256Sjkoshy */ 768145256Sjkoshy 769145256Sjkoshy if (LIST_EMPTY(&po->po_pmcs) && 770145774Sjkoshy ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0)) { 771145256Sjkoshy pmc_remove_owner(po); 772147191Sjkoshy pmc_destroy_owner_descriptor(po); 773145256Sjkoshy } 774145256Sjkoshy} 775145256Sjkoshy 776145256Sjkoshy/* 777145256Sjkoshy * Add an association between a target process and a PMC. 778145256Sjkoshy */ 779145256Sjkoshy 780145256Sjkoshystatic void 781145256Sjkoshypmc_link_target_process(struct pmc *pm, struct pmc_process *pp) 782145256Sjkoshy{ 783145256Sjkoshy int ri; 784145256Sjkoshy struct pmc_target *pt; 785145256Sjkoshy 786145256Sjkoshy sx_assert(&pmc_sx, SX_XLOCKED); 787145256Sjkoshy 788145256Sjkoshy KASSERT(pm != NULL && pp != NULL, 789145256Sjkoshy ("[pmc,%d] Null pm %p or pp %p", __LINE__, pm, pp)); 790147191Sjkoshy KASSERT(PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm)), 791147191Sjkoshy ("[pmc,%d] Attaching a non-process-virtual pmc=%p to pid=%d", 792147191Sjkoshy __LINE__, pm, pp->pp_proc->p_pid)); 793198343Sfabient KASSERT(pp->pp_refcnt >= 0 && pp->pp_refcnt <= ((int) md->pmd_npmc - 1), 794145256Sjkoshy ("[pmc,%d] Illegal reference count %d for process record %p", 795145256Sjkoshy __LINE__, pp->pp_refcnt, (void *) pp)); 796145256Sjkoshy 797145774Sjkoshy ri = PMC_TO_ROWINDEX(pm); 798145256Sjkoshy 799145256Sjkoshy PMCDBG(PRC,TLK,1, "link-target pmc=%p ri=%d pmc-process=%p", 800145256Sjkoshy pm, ri, pp); 801145256Sjkoshy 802153110Sru#ifdef DEBUG 803145256Sjkoshy LIST_FOREACH(pt, &pm->pm_targets, pt_next) 804145256Sjkoshy if (pt->pt_process == pp) 805145256Sjkoshy KASSERT(0, ("[pmc,%d] pp %p already in pmc %p targets", 806145256Sjkoshy __LINE__, pp, pm)); 807145256Sjkoshy#endif 808145256Sjkoshy 809184802Sjkoshy pt = malloc(sizeof(struct pmc_target), M_PMC, M_WAITOK|M_ZERO); 810145256Sjkoshy pt->pt_process = pp; 811145256Sjkoshy 812145256Sjkoshy LIST_INSERT_HEAD(&pm->pm_targets, pt, pt_next); 813145256Sjkoshy 814148067Sjhb atomic_store_rel_ptr((uintptr_t *)&pp->pp_pmcs[ri].pp_pmc, 815148067Sjhb (uintptr_t)pm); 816145256Sjkoshy 817145615Sjkoshy if (pm->pm_owner->po_owner == pp->pp_proc) 818145774Sjkoshy pm->pm_flags |= PMC_F_ATTACHED_TO_OWNER; 819145615Sjkoshy 820147191Sjkoshy /* 821147191Sjkoshy * Initialize the per-process values at this row index. 822147191Sjkoshy */ 823147191Sjkoshy pp->pp_pmcs[ri].pp_pmcval = PMC_TO_MODE(pm) == PMC_MODE_TS ? 824147191Sjkoshy pm->pm_sc.pm_reloadcount : 0; 825147191Sjkoshy 826145256Sjkoshy pp->pp_refcnt++; 827145256Sjkoshy 828145256Sjkoshy} 829145256Sjkoshy 830145256Sjkoshy/* 831145256Sjkoshy * Removes the association between a target process and a PMC. 832145256Sjkoshy */ 833145256Sjkoshy 834145256Sjkoshystatic void 835145256Sjkoshypmc_unlink_target_process(struct pmc *pm, struct pmc_process *pp) 836145256Sjkoshy{ 837145256Sjkoshy int ri; 838147191Sjkoshy struct proc *p; 839145256Sjkoshy struct pmc_target *ptgt; 840145256Sjkoshy 841145256Sjkoshy sx_assert(&pmc_sx, SX_XLOCKED); 842145256Sjkoshy 843145256Sjkoshy KASSERT(pm != NULL && pp != NULL, 844145256Sjkoshy ("[pmc,%d] Null pm %p or pp %p", __LINE__, pm, pp)); 845145256Sjkoshy 846198343Sfabient KASSERT(pp->pp_refcnt >= 1 && pp->pp_refcnt <= (int) md->pmd_npmc, 847145256Sjkoshy ("[pmc,%d] Illegal ref count %d on process record %p", 848145256Sjkoshy __LINE__, pp->pp_refcnt, (void *) pp)); 849145256Sjkoshy 850145774Sjkoshy ri = PMC_TO_ROWINDEX(pm); 851145256Sjkoshy 852145256Sjkoshy PMCDBG(PRC,TUL,1, "unlink-target pmc=%p ri=%d pmc-process=%p", 853145256Sjkoshy pm, ri, pp); 854145256Sjkoshy 855145256Sjkoshy KASSERT(pp->pp_pmcs[ri].pp_pmc == pm, 856145256Sjkoshy ("[pmc,%d] PMC ri %d mismatch pmc %p pp->[ri] %p", __LINE__, 857145256Sjkoshy ri, pm, pp->pp_pmcs[ri].pp_pmc)); 858145256Sjkoshy 859145256Sjkoshy pp->pp_pmcs[ri].pp_pmc = NULL; 860145256Sjkoshy pp->pp_pmcs[ri].pp_pmcval = (pmc_value_t) 0; 861145256Sjkoshy 862145774Sjkoshy /* Remove owner-specific flags */ 863145774Sjkoshy if (pm->pm_owner->po_owner == pp->pp_proc) { 864145774Sjkoshy pp->pp_flags &= ~PMC_PP_ENABLE_MSR_ACCESS; 865145774Sjkoshy pm->pm_flags &= ~PMC_F_ATTACHED_TO_OWNER; 866145774Sjkoshy } 867145615Sjkoshy 868145256Sjkoshy pp->pp_refcnt--; 869145256Sjkoshy 870145256Sjkoshy /* Remove the target process from the PMC structure */ 871145256Sjkoshy LIST_FOREACH(ptgt, &pm->pm_targets, pt_next) 872145256Sjkoshy if (ptgt->pt_process == pp) 873145256Sjkoshy break; 874145256Sjkoshy 875145256Sjkoshy KASSERT(ptgt != NULL, ("[pmc,%d] process %p (pp: %p) not found " 876145256Sjkoshy "in pmc %p", __LINE__, pp->pp_proc, pp, pm)); 877145256Sjkoshy 878145256Sjkoshy LIST_REMOVE(ptgt, pt_next); 879184205Sdes free(ptgt, M_PMC); 880145256Sjkoshy 881147191Sjkoshy /* if the PMC now lacks targets, send the owner a SIGIO */ 882147191Sjkoshy if (LIST_EMPTY(&pm->pm_targets)) { 883147191Sjkoshy p = pm->pm_owner->po_owner; 884147191Sjkoshy PROC_LOCK(p); 885147191Sjkoshy psignal(p, SIGIO); 886147191Sjkoshy PROC_UNLOCK(p); 887145256Sjkoshy 888147191Sjkoshy PMCDBG(PRC,SIG,2, "signalling proc=%p signal=%d", p, 889147191Sjkoshy SIGIO); 890145256Sjkoshy } 891145256Sjkoshy} 892145256Sjkoshy 893145256Sjkoshy/* 894145256Sjkoshy * Check if PMC 'pm' may be attached to target process 't'. 895145256Sjkoshy */ 896145256Sjkoshy 897145256Sjkoshystatic int 898145256Sjkoshypmc_can_attach(struct pmc *pm, struct proc *t) 899145256Sjkoshy{ 900145256Sjkoshy struct proc *o; /* pmc owner */ 901145256Sjkoshy struct ucred *oc, *tc; /* owner, target credentials */ 902145256Sjkoshy int decline_attach, i; 903145256Sjkoshy 904145256Sjkoshy /* 905145256Sjkoshy * A PMC's owner can always attach that PMC to itself. 906145256Sjkoshy */ 907145256Sjkoshy 908145256Sjkoshy if ((o = pm->pm_owner->po_owner) == t) 909145256Sjkoshy return 0; 910145256Sjkoshy 911145256Sjkoshy PROC_LOCK(o); 912145256Sjkoshy oc = o->p_ucred; 913145256Sjkoshy crhold(oc); 914145256Sjkoshy PROC_UNLOCK(o); 915145256Sjkoshy 916145256Sjkoshy PROC_LOCK(t); 917145256Sjkoshy tc = t->p_ucred; 918145256Sjkoshy crhold(tc); 919145256Sjkoshy PROC_UNLOCK(t); 920145256Sjkoshy 921145256Sjkoshy /* 922145256Sjkoshy * The effective uid of the PMC owner should match at least one 923145256Sjkoshy * of the {effective,real,saved} uids of the target process. 924145256Sjkoshy */ 925145256Sjkoshy 926145256Sjkoshy decline_attach = oc->cr_uid != tc->cr_uid && 927145256Sjkoshy oc->cr_uid != tc->cr_svuid && 928145256Sjkoshy oc->cr_uid != tc->cr_ruid; 929145256Sjkoshy 930145256Sjkoshy /* 931145256Sjkoshy * Every one of the target's group ids, must be in the owner's 932145256Sjkoshy * group list. 933145256Sjkoshy */ 934145256Sjkoshy for (i = 0; !decline_attach && i < tc->cr_ngroups; i++) 935145256Sjkoshy decline_attach = !groupmember(tc->cr_groups[i], oc); 936145256Sjkoshy 937145256Sjkoshy /* check the read and saved gids too */ 938145256Sjkoshy if (decline_attach == 0) 939145256Sjkoshy decline_attach = !groupmember(tc->cr_rgid, oc) || 940145256Sjkoshy !groupmember(tc->cr_svgid, oc); 941145256Sjkoshy 942145256Sjkoshy crfree(tc); 943145256Sjkoshy crfree(oc); 944145256Sjkoshy 945145256Sjkoshy return !decline_attach; 946145256Sjkoshy} 947145256Sjkoshy 948145256Sjkoshy/* 949145256Sjkoshy * Attach a process to a PMC. 950145256Sjkoshy */ 951145256Sjkoshy 952145256Sjkoshystatic int 953145256Sjkoshypmc_attach_one_process(struct proc *p, struct pmc *pm) 954145256Sjkoshy{ 955145256Sjkoshy int ri; 956147191Sjkoshy char *fullpath, *freepath; 957145256Sjkoshy struct pmc_process *pp; 958145256Sjkoshy 959145256Sjkoshy sx_assert(&pmc_sx, SX_XLOCKED); 960145256Sjkoshy 961145256Sjkoshy PMCDBG(PRC,ATT,2, "attach-one pm=%p ri=%d proc=%p (%d, %s)", pm, 962145774Sjkoshy PMC_TO_ROWINDEX(pm), p, p->p_pid, p->p_comm); 963145256Sjkoshy 964145256Sjkoshy /* 965145256Sjkoshy * Locate the process descriptor corresponding to process 'p', 966145256Sjkoshy * allocating space as needed. 967145256Sjkoshy * 968145256Sjkoshy * Verify that rowindex 'pm_rowindex' is free in the process 969145256Sjkoshy * descriptor. 970145256Sjkoshy * 971145256Sjkoshy * If not, allocate space for a descriptor and link the 972145256Sjkoshy * process descriptor and PMC. 973145256Sjkoshy */ 974145774Sjkoshy ri = PMC_TO_ROWINDEX(pm); 975145256Sjkoshy 976145256Sjkoshy if ((pp = pmc_find_process_descriptor(p, PMC_FLAG_ALLOCATE)) == NULL) 977145256Sjkoshy return ENOMEM; 978145256Sjkoshy 979145256Sjkoshy if (pp->pp_pmcs[ri].pp_pmc == pm) /* already present at slot [ri] */ 980145256Sjkoshy return EEXIST; 981145256Sjkoshy 982145256Sjkoshy if (pp->pp_pmcs[ri].pp_pmc != NULL) 983145256Sjkoshy return EBUSY; 984145256Sjkoshy 985145256Sjkoshy pmc_link_target_process(pm, pp); 986145256Sjkoshy 987147191Sjkoshy if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)) && 988147191Sjkoshy (pm->pm_flags & PMC_F_ATTACHED_TO_OWNER) == 0) 989147191Sjkoshy pm->pm_flags |= PMC_F_NEEDS_LOGFILE; 990147191Sjkoshy 991147191Sjkoshy pm->pm_flags |= PMC_F_ATTACH_DONE; /* mark as attached */ 992147191Sjkoshy 993147191Sjkoshy /* issue an attach event to a configured log file */ 994147191Sjkoshy if (pm->pm_owner->po_flags & PMC_PO_OWNS_LOGFILE) { 995147708Sjkoshy pmc_getfilename(p->p_textvp, &fullpath, &freepath); 996180794Sjeff if (p->p_flag & P_KTHREAD) { 997180794Sjeff fullpath = kernelname; 998180794Sjeff freepath = NULL; 999180794Sjeff } else 1000180794Sjeff pmclog_process_pmcattach(pm, p->p_pid, fullpath); 1001147191Sjkoshy if (freepath) 1002184205Sdes free(freepath, M_TEMP); 1003174395Sjkoshy if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm))) 1004174395Sjkoshy pmc_log_process_mappings(pm->pm_owner, p); 1005147191Sjkoshy } 1006145256Sjkoshy /* mark process as using HWPMCs */ 1007145256Sjkoshy PROC_LOCK(p); 1008145256Sjkoshy p->p_flag |= P_HWPMC; 1009145256Sjkoshy PROC_UNLOCK(p); 1010145256Sjkoshy 1011145256Sjkoshy return 0; 1012145256Sjkoshy} 1013145256Sjkoshy 1014145256Sjkoshy/* 1015145256Sjkoshy * Attach a process and optionally its children 1016145256Sjkoshy */ 1017145256Sjkoshy 1018145256Sjkoshystatic int 1019145256Sjkoshypmc_attach_process(struct proc *p, struct pmc *pm) 1020145256Sjkoshy{ 1021145256Sjkoshy int error; 1022145256Sjkoshy struct proc *top; 1023145256Sjkoshy 1024145256Sjkoshy sx_assert(&pmc_sx, SX_XLOCKED); 1025145256Sjkoshy 1026145256Sjkoshy PMCDBG(PRC,ATT,1, "attach pm=%p ri=%d proc=%p (%d, %s)", pm, 1027145774Sjkoshy PMC_TO_ROWINDEX(pm), p, p->p_pid, p->p_comm); 1028145256Sjkoshy 1029145774Sjkoshy 1030145774Sjkoshy /* 1031145774Sjkoshy * If this PMC successfully allowed a GETMSR operation 1032145774Sjkoshy * in the past, disallow further ATTACHes. 1033145774Sjkoshy */ 1034145774Sjkoshy 1035145774Sjkoshy if ((pm->pm_flags & PMC_PP_ENABLE_MSR_ACCESS) != 0) 1036145774Sjkoshy return EPERM; 1037145774Sjkoshy 1038145256Sjkoshy if ((pm->pm_flags & PMC_F_DESCENDANTS) == 0) 1039145256Sjkoshy return pmc_attach_one_process(p, pm); 1040145256Sjkoshy 1041145256Sjkoshy /* 1042145256Sjkoshy * Traverse all child processes, attaching them to 1043145256Sjkoshy * this PMC. 1044145256Sjkoshy */ 1045145256Sjkoshy 1046145256Sjkoshy sx_slock(&proctree_lock); 1047145256Sjkoshy 1048145256Sjkoshy top = p; 1049145256Sjkoshy 1050145256Sjkoshy for (;;) { 1051145256Sjkoshy if ((error = pmc_attach_one_process(p, pm)) != 0) 1052145256Sjkoshy break; 1053145256Sjkoshy if (!LIST_EMPTY(&p->p_children)) 1054145256Sjkoshy p = LIST_FIRST(&p->p_children); 1055145256Sjkoshy else for (;;) { 1056145256Sjkoshy if (p == top) 1057145256Sjkoshy goto done; 1058145256Sjkoshy if (LIST_NEXT(p, p_sibling)) { 1059145256Sjkoshy p = LIST_NEXT(p, p_sibling); 1060145256Sjkoshy break; 1061145256Sjkoshy } 1062145256Sjkoshy p = p->p_pptr; 1063145256Sjkoshy } 1064145256Sjkoshy } 1065145256Sjkoshy 1066145256Sjkoshy if (error) 1067145256Sjkoshy (void) pmc_detach_process(top, pm); 1068145256Sjkoshy 1069145256Sjkoshy done: 1070145256Sjkoshy sx_sunlock(&proctree_lock); 1071145256Sjkoshy return error; 1072145256Sjkoshy} 1073145256Sjkoshy 1074145256Sjkoshy/* 1075145256Sjkoshy * Detach a process from a PMC. If there are no other PMCs tracking 1076145256Sjkoshy * this process, remove the process structure from its hash table. If 1077145256Sjkoshy * 'flags' contains PMC_FLAG_REMOVE, then free the process structure. 1078145256Sjkoshy */ 1079145256Sjkoshy 1080145256Sjkoshystatic int 1081145256Sjkoshypmc_detach_one_process(struct proc *p, struct pmc *pm, int flags) 1082145256Sjkoshy{ 1083145256Sjkoshy int ri; 1084145256Sjkoshy struct pmc_process *pp; 1085145256Sjkoshy 1086145256Sjkoshy sx_assert(&pmc_sx, SX_XLOCKED); 1087145256Sjkoshy 1088145256Sjkoshy KASSERT(pm != NULL, 1089145256Sjkoshy ("[pmc,%d] null pm pointer", __LINE__)); 1090145256Sjkoshy 1091145774Sjkoshy ri = PMC_TO_ROWINDEX(pm); 1092145774Sjkoshy 1093145256Sjkoshy PMCDBG(PRC,ATT,2, "detach-one pm=%p ri=%d proc=%p (%d, %s) flags=0x%x", 1094145774Sjkoshy pm, ri, p, p->p_pid, p->p_comm, flags); 1095145256Sjkoshy 1096145256Sjkoshy if ((pp = pmc_find_process_descriptor(p, 0)) == NULL) 1097145256Sjkoshy return ESRCH; 1098145256Sjkoshy 1099145256Sjkoshy if (pp->pp_pmcs[ri].pp_pmc != pm) 1100145256Sjkoshy return EINVAL; 1101145256Sjkoshy 1102145256Sjkoshy pmc_unlink_target_process(pm, pp); 1103145256Sjkoshy 1104147191Sjkoshy /* Issue a detach entry if a log file is configured */ 1105147191Sjkoshy if (pm->pm_owner->po_flags & PMC_PO_OWNS_LOGFILE) 1106147191Sjkoshy pmclog_process_pmcdetach(pm, p->p_pid); 1107147191Sjkoshy 1108145256Sjkoshy /* 1109145256Sjkoshy * If there are no PMCs targetting this process, we remove its 1110145256Sjkoshy * descriptor from the target hash table and unset the P_HWPMC 1111145256Sjkoshy * flag in the struct proc. 1112145256Sjkoshy */ 1113198343Sfabient KASSERT(pp->pp_refcnt >= 0 && pp->pp_refcnt <= (int) md->pmd_npmc, 1114145256Sjkoshy ("[pmc,%d] Illegal refcnt %d for process struct %p", 1115145256Sjkoshy __LINE__, pp->pp_refcnt, pp)); 1116145256Sjkoshy 1117145256Sjkoshy if (pp->pp_refcnt != 0) /* still a target of some PMC */ 1118145256Sjkoshy return 0; 1119145256Sjkoshy 1120145256Sjkoshy pmc_remove_process_descriptor(pp); 1121145256Sjkoshy 1122145256Sjkoshy if (flags & PMC_FLAG_REMOVE) 1123184205Sdes free(pp, M_PMC); 1124145256Sjkoshy 1125145256Sjkoshy PROC_LOCK(p); 1126145256Sjkoshy p->p_flag &= ~P_HWPMC; 1127145256Sjkoshy PROC_UNLOCK(p); 1128145256Sjkoshy 1129145256Sjkoshy return 0; 1130145256Sjkoshy} 1131145256Sjkoshy 1132145256Sjkoshy/* 1133145256Sjkoshy * Detach a process and optionally its descendants from a PMC. 1134145256Sjkoshy */ 1135145256Sjkoshy 1136145256Sjkoshystatic int 1137145256Sjkoshypmc_detach_process(struct proc *p, struct pmc *pm) 1138145256Sjkoshy{ 1139145256Sjkoshy struct proc *top; 1140145256Sjkoshy 1141145256Sjkoshy sx_assert(&pmc_sx, SX_XLOCKED); 1142145256Sjkoshy 1143145256Sjkoshy PMCDBG(PRC,ATT,1, "detach pm=%p ri=%d proc=%p (%d, %s)", pm, 1144145774Sjkoshy PMC_TO_ROWINDEX(pm), p, p->p_pid, p->p_comm); 1145145256Sjkoshy 1146145256Sjkoshy if ((pm->pm_flags & PMC_F_DESCENDANTS) == 0) 1147145256Sjkoshy return pmc_detach_one_process(p, pm, PMC_FLAG_REMOVE); 1148145256Sjkoshy 1149145256Sjkoshy /* 1150145256Sjkoshy * Traverse all children, detaching them from this PMC. We 1151145256Sjkoshy * ignore errors since we could be detaching a PMC from a 1152145256Sjkoshy * partially attached proc tree. 1153145256Sjkoshy */ 1154145256Sjkoshy 1155145256Sjkoshy sx_slock(&proctree_lock); 1156145256Sjkoshy 1157145256Sjkoshy top = p; 1158145256Sjkoshy 1159145256Sjkoshy for (;;) { 1160145256Sjkoshy (void) pmc_detach_one_process(p, pm, PMC_FLAG_REMOVE); 1161145256Sjkoshy 1162145256Sjkoshy if (!LIST_EMPTY(&p->p_children)) 1163145256Sjkoshy p = LIST_FIRST(&p->p_children); 1164145256Sjkoshy else for (;;) { 1165145256Sjkoshy if (p == top) 1166145256Sjkoshy goto done; 1167145256Sjkoshy if (LIST_NEXT(p, p_sibling)) { 1168145256Sjkoshy p = LIST_NEXT(p, p_sibling); 1169145256Sjkoshy break; 1170145256Sjkoshy } 1171145256Sjkoshy p = p->p_pptr; 1172145256Sjkoshy } 1173145256Sjkoshy } 1174145256Sjkoshy 1175145256Sjkoshy done: 1176145256Sjkoshy sx_sunlock(&proctree_lock); 1177147191Sjkoshy 1178147191Sjkoshy if (LIST_EMPTY(&pm->pm_targets)) 1179147191Sjkoshy pm->pm_flags &= ~PMC_F_ATTACH_DONE; 1180147191Sjkoshy 1181145256Sjkoshy return 0; 1182145256Sjkoshy} 1183145256Sjkoshy 1184147191Sjkoshy 1185145256Sjkoshy/* 1186147191Sjkoshy * Thread context switch IN 1187145256Sjkoshy */ 1188145256Sjkoshy 1189147191Sjkoshystatic void 1190147191Sjkoshypmc_process_csw_in(struct thread *td) 1191147191Sjkoshy{ 1192147191Sjkoshy int cpu; 1193184802Sjkoshy unsigned int adjri, ri; 1194147191Sjkoshy struct pmc *pm; 1195147191Sjkoshy struct proc *p; 1196147191Sjkoshy struct pmc_cpu *pc; 1197147191Sjkoshy struct pmc_hw *phw; 1198184802Sjkoshy pmc_value_t newvalue; 1199147191Sjkoshy struct pmc_process *pp; 1200184802Sjkoshy struct pmc_classdep *pcd; 1201145256Sjkoshy 1202147191Sjkoshy p = td->td_proc; 1203145256Sjkoshy 1204147191Sjkoshy if ((pp = pmc_find_process_descriptor(p, PMC_FLAG_NONE)) == NULL) 1205147191Sjkoshy return; 1206145256Sjkoshy 1207147191Sjkoshy KASSERT(pp->pp_proc == td->td_proc, 1208147191Sjkoshy ("[pmc,%d] not my thread state", __LINE__)); 1209145256Sjkoshy 1210147191Sjkoshy critical_enter(); /* no preemption from this point */ 1211145256Sjkoshy 1212147191Sjkoshy cpu = PCPU_GET(cpuid); /* td->td_oncpu is invalid */ 1213145256Sjkoshy 1214147191Sjkoshy PMCDBG(CSW,SWI,1, "cpu=%d proc=%p (%d, %s) pp=%p", cpu, p, 1215147191Sjkoshy p->p_pid, p->p_comm, pp); 1216145256Sjkoshy 1217183266Sjkoshy KASSERT(cpu >= 0 && cpu < pmc_cpu_max(), 1218147191Sjkoshy ("[pmc,%d] wierd CPU id %d", __LINE__, cpu)); 1219145256Sjkoshy 1220147191Sjkoshy pc = pmc_pcpu[cpu]; 1221145256Sjkoshy 1222147191Sjkoshy for (ri = 0; ri < md->pmd_npmc; ri++) { 1223145256Sjkoshy 1224147191Sjkoshy if ((pm = pp->pp_pmcs[ri].pp_pmc) == NULL) 1225147191Sjkoshy continue; 1226147191Sjkoshy 1227147191Sjkoshy KASSERT(PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm)), 1228147191Sjkoshy ("[pmc,%d] Target PMC in non-virtual mode (%d)", 1229147191Sjkoshy __LINE__, PMC_TO_MODE(pm))); 1230147191Sjkoshy 1231147191Sjkoshy KASSERT(PMC_TO_ROWINDEX(pm) == ri, 1232147191Sjkoshy ("[pmc,%d] Row index mismatch pmc %d != ri %d", 1233147191Sjkoshy __LINE__, PMC_TO_ROWINDEX(pm), ri)); 1234147191Sjkoshy 1235145256Sjkoshy /* 1236147191Sjkoshy * Only PMCs that are marked as 'RUNNING' need 1237147191Sjkoshy * be placed on hardware. 1238145256Sjkoshy */ 1239145256Sjkoshy 1240147191Sjkoshy if (pm->pm_state != PMC_STATE_RUNNING) 1241147191Sjkoshy continue; 1242145256Sjkoshy 1243147191Sjkoshy /* increment PMC runcount */ 1244147191Sjkoshy atomic_add_rel_32(&pm->pm_runcount, 1); 1245145256Sjkoshy 1246147191Sjkoshy /* configure the HWPMC we are going to use. */ 1247184802Sjkoshy pcd = pmc_ri_to_classdep(md, ri, &adjri); 1248184802Sjkoshy pcd->pcd_config_pmc(cpu, adjri, pm); 1249145256Sjkoshy 1250147191Sjkoshy phw = pc->pc_hwpmcs[ri]; 1251145256Sjkoshy 1252147191Sjkoshy KASSERT(phw != NULL, 1253147191Sjkoshy ("[pmc,%d] null hw pointer", __LINE__)); 1254145256Sjkoshy 1255147191Sjkoshy KASSERT(phw->phw_pmc == pm, 1256147191Sjkoshy ("[pmc,%d] hw->pmc %p != pmc %p", __LINE__, 1257147191Sjkoshy phw->phw_pmc, pm)); 1258145256Sjkoshy 1259147191Sjkoshy /* 1260147191Sjkoshy * Write out saved value and start the PMC. 1261147191Sjkoshy * 1262147191Sjkoshy * Sampling PMCs use a per-process value, while 1263147191Sjkoshy * counting mode PMCs use a per-pmc value that is 1264147191Sjkoshy * inherited across descendants. 1265147191Sjkoshy */ 1266147191Sjkoshy if (PMC_TO_MODE(pm) == PMC_MODE_TS) { 1267147191Sjkoshy mtx_pool_lock_spin(pmc_mtxpool, pm); 1268147191Sjkoshy newvalue = PMC_PCPU_SAVED(cpu,ri) = 1269147191Sjkoshy pp->pp_pmcs[ri].pp_pmcval; 1270147191Sjkoshy mtx_pool_unlock_spin(pmc_mtxpool, pm); 1271147191Sjkoshy } else { 1272147191Sjkoshy KASSERT(PMC_TO_MODE(pm) == PMC_MODE_TC, 1273147191Sjkoshy ("[pmc,%d] illegal mode=%d", __LINE__, 1274147191Sjkoshy PMC_TO_MODE(pm))); 1275147191Sjkoshy mtx_pool_lock_spin(pmc_mtxpool, pm); 1276147191Sjkoshy newvalue = PMC_PCPU_SAVED(cpu, ri) = 1277147191Sjkoshy pm->pm_gv.pm_savedvalue; 1278147191Sjkoshy mtx_pool_unlock_spin(pmc_mtxpool, pm); 1279147191Sjkoshy } 1280145256Sjkoshy 1281147191Sjkoshy PMCDBG(CSW,SWI,1,"cpu=%d ri=%d new=%jd", cpu, ri, newvalue); 1282145256Sjkoshy 1283184802Sjkoshy pcd->pcd_write_pmc(cpu, adjri, newvalue); 1284184802Sjkoshy pcd->pcd_start_pmc(cpu, adjri); 1285147191Sjkoshy } 1286145256Sjkoshy 1287147191Sjkoshy /* 1288147191Sjkoshy * perform any other architecture/cpu dependent thread 1289147191Sjkoshy * switch-in actions. 1290147191Sjkoshy */ 1291145256Sjkoshy 1292147191Sjkoshy (void) (*md->pmd_switch_in)(pc, pp); 1293145256Sjkoshy 1294147191Sjkoshy critical_exit(); 1295145256Sjkoshy 1296147191Sjkoshy} 1297145256Sjkoshy 1298147191Sjkoshy/* 1299147191Sjkoshy * Thread context switch OUT. 1300147191Sjkoshy */ 1301145256Sjkoshy 1302147191Sjkoshystatic void 1303147191Sjkoshypmc_process_csw_out(struct thread *td) 1304147191Sjkoshy{ 1305147191Sjkoshy int cpu; 1306184802Sjkoshy int64_t tmp; 1307147191Sjkoshy struct pmc *pm; 1308147191Sjkoshy struct proc *p; 1309184802Sjkoshy enum pmc_mode mode; 1310147191Sjkoshy struct pmc_cpu *pc; 1311184802Sjkoshy pmc_value_t newvalue; 1312184802Sjkoshy unsigned int adjri, ri; 1313147191Sjkoshy struct pmc_process *pp; 1314184802Sjkoshy struct pmc_classdep *pcd; 1315145256Sjkoshy 1316184802Sjkoshy 1317147191Sjkoshy /* 1318147191Sjkoshy * Locate our process descriptor; this may be NULL if 1319147191Sjkoshy * this process is exiting and we have already removed 1320147191Sjkoshy * the process from the target process table. 1321147191Sjkoshy * 1322147191Sjkoshy * Note that due to kernel preemption, multiple 1323147191Sjkoshy * context switches may happen while the process is 1324147191Sjkoshy * exiting. 1325147191Sjkoshy * 1326147191Sjkoshy * Note also that if the target process cannot be 1327147191Sjkoshy * found we still need to deconfigure any PMCs that 1328147191Sjkoshy * are currently running on hardware. 1329147191Sjkoshy */ 1330145256Sjkoshy 1331147191Sjkoshy p = td->td_proc; 1332147191Sjkoshy pp = pmc_find_process_descriptor(p, PMC_FLAG_NONE); 1333145256Sjkoshy 1334147191Sjkoshy /* 1335147191Sjkoshy * save PMCs 1336147191Sjkoshy */ 1337145256Sjkoshy 1338147191Sjkoshy critical_enter(); 1339145774Sjkoshy 1340147191Sjkoshy cpu = PCPU_GET(cpuid); /* td->td_oncpu is invalid */ 1341145256Sjkoshy 1342147191Sjkoshy PMCDBG(CSW,SWO,1, "cpu=%d proc=%p (%d, %s) pp=%p", cpu, p, 1343147191Sjkoshy p->p_pid, p->p_comm, pp); 1344145615Sjkoshy 1345183266Sjkoshy KASSERT(cpu >= 0 && cpu < pmc_cpu_max(), 1346147191Sjkoshy ("[pmc,%d wierd CPU id %d", __LINE__, cpu)); 1347145615Sjkoshy 1348147191Sjkoshy pc = pmc_pcpu[cpu]; 1349145615Sjkoshy 1350147191Sjkoshy /* 1351147191Sjkoshy * When a PMC gets unlinked from a target PMC, it will 1352147191Sjkoshy * be removed from the target's pp_pmc[] array. 1353147191Sjkoshy * 1354147191Sjkoshy * However, on a MP system, the target could have been 1355147191Sjkoshy * executing on another CPU at the time of the unlink. 1356147191Sjkoshy * So, at context switch OUT time, we need to look at 1357147191Sjkoshy * the hardware to determine if a PMC is scheduled on 1358147191Sjkoshy * it. 1359147191Sjkoshy */ 1360145256Sjkoshy 1361147191Sjkoshy for (ri = 0; ri < md->pmd_npmc; ri++) { 1362145256Sjkoshy 1363184802Sjkoshy pcd = pmc_ri_to_classdep(md, ri, &adjri); 1364184802Sjkoshy pm = NULL; 1365184802Sjkoshy (void) (*pcd->pcd_get_config)(cpu, adjri, &pm); 1366145256Sjkoshy 1367147191Sjkoshy if (pm == NULL) /* nothing at this row index */ 1368147191Sjkoshy continue; 1369145256Sjkoshy 1370147191Sjkoshy mode = PMC_TO_MODE(pm); 1371147191Sjkoshy if (!PMC_IS_VIRTUAL_MODE(mode)) 1372147191Sjkoshy continue; /* not a process virtual PMC */ 1373145774Sjkoshy 1374147191Sjkoshy KASSERT(PMC_TO_ROWINDEX(pm) == ri, 1375147191Sjkoshy ("[pmc,%d] ri mismatch pmc(%d) ri(%d)", 1376147191Sjkoshy __LINE__, PMC_TO_ROWINDEX(pm), ri)); 1377145256Sjkoshy 1378147191Sjkoshy /* Stop hardware if not already stopped */ 1379147867Sjkoshy if (pm->pm_stalled == 0) 1380184802Sjkoshy pcd->pcd_stop_pmc(cpu, adjri); 1381147191Sjkoshy 1382147191Sjkoshy /* reduce this PMC's runcount */ 1383147191Sjkoshy atomic_subtract_rel_32(&pm->pm_runcount, 1); 1384147191Sjkoshy 1385145256Sjkoshy /* 1386147191Sjkoshy * If this PMC is associated with this process, 1387147191Sjkoshy * save the reading. 1388145256Sjkoshy */ 1389145256Sjkoshy 1390147191Sjkoshy if (pp != NULL && pp->pp_pmcs[ri].pp_pmc != NULL) { 1391147191Sjkoshy 1392147191Sjkoshy KASSERT(pm == pp->pp_pmcs[ri].pp_pmc, 1393147191Sjkoshy ("[pmc,%d] pm %p != pp_pmcs[%d] %p", __LINE__, 1394147191Sjkoshy pm, ri, pp->pp_pmcs[ri].pp_pmc)); 1395147191Sjkoshy 1396147191Sjkoshy KASSERT(pp->pp_refcnt > 0, 1397147191Sjkoshy ("[pmc,%d] pp refcnt = %d", __LINE__, 1398147191Sjkoshy pp->pp_refcnt)); 1399147191Sjkoshy 1400184802Sjkoshy pcd->pcd_read_pmc(cpu, adjri, &newvalue); 1401147191Sjkoshy 1402147191Sjkoshy tmp = newvalue - PMC_PCPU_SAVED(cpu,ri); 1403147191Sjkoshy 1404199972Semaste PMCDBG(CSW,SWO,1,"cpu=%d ri=%d tmp=%jd", cpu, ri, 1405147191Sjkoshy tmp); 1406147191Sjkoshy 1407147191Sjkoshy if (mode == PMC_MODE_TS) { 1408147191Sjkoshy 1409147191Sjkoshy /* 1410147191Sjkoshy * For sampling process-virtual PMCs, 1411147191Sjkoshy * we expect the count to be 1412147191Sjkoshy * decreasing as the 'value' 1413147191Sjkoshy * programmed into the PMC is the 1414147191Sjkoshy * number of events to be seen till 1415147191Sjkoshy * the next sampling interrupt. 1416147191Sjkoshy */ 1417147191Sjkoshy if (tmp < 0) 1418147191Sjkoshy tmp += pm->pm_sc.pm_reloadcount; 1419147191Sjkoshy mtx_pool_lock_spin(pmc_mtxpool, pm); 1420147191Sjkoshy pp->pp_pmcs[ri].pp_pmcval -= tmp; 1421147191Sjkoshy if ((int64_t) pp->pp_pmcs[ri].pp_pmcval < 0) 1422147191Sjkoshy pp->pp_pmcs[ri].pp_pmcval += 1423147191Sjkoshy pm->pm_sc.pm_reloadcount; 1424147191Sjkoshy mtx_pool_unlock_spin(pmc_mtxpool, pm); 1425147191Sjkoshy 1426147191Sjkoshy } else { 1427147191Sjkoshy 1428147191Sjkoshy /* 1429147191Sjkoshy * For counting process-virtual PMCs, 1430147191Sjkoshy * we expect the count to be 1431147191Sjkoshy * increasing monotonically, modulo a 64 1432147191Sjkoshy * bit wraparound. 1433147191Sjkoshy */ 1434147191Sjkoshy KASSERT((int64_t) tmp >= 0, 1435147191Sjkoshy ("[pmc,%d] negative increment cpu=%d " 1436147191Sjkoshy "ri=%d newvalue=%jx saved=%jx " 1437147191Sjkoshy "incr=%jx", __LINE__, cpu, ri, 1438147191Sjkoshy newvalue, PMC_PCPU_SAVED(cpu,ri), tmp)); 1439147191Sjkoshy 1440147191Sjkoshy mtx_pool_lock_spin(pmc_mtxpool, pm); 1441147191Sjkoshy pm->pm_gv.pm_savedvalue += tmp; 1442147191Sjkoshy pp->pp_pmcs[ri].pp_pmcval += tmp; 1443147191Sjkoshy mtx_pool_unlock_spin(pmc_mtxpool, pm); 1444147191Sjkoshy 1445147191Sjkoshy if (pm->pm_flags & PMC_F_LOG_PROCCSW) 1446147191Sjkoshy pmclog_process_proccsw(pm, pp, tmp); 1447147191Sjkoshy } 1448145256Sjkoshy } 1449145256Sjkoshy 1450147191Sjkoshy /* mark hardware as free */ 1451184802Sjkoshy pcd->pcd_config_pmc(cpu, adjri, NULL); 1452145256Sjkoshy } 1453145256Sjkoshy 1454145256Sjkoshy /* 1455147191Sjkoshy * perform any other architecture/cpu dependent thread 1456147191Sjkoshy * switch out functions. 1457147191Sjkoshy */ 1458147191Sjkoshy 1459147191Sjkoshy (void) (*md->pmd_switch_out)(pc, pp); 1460147191Sjkoshy 1461147191Sjkoshy critical_exit(); 1462147191Sjkoshy} 1463147191Sjkoshy 1464147191Sjkoshy/* 1465157144Sjkoshy * Log a KLD operation. 1466157144Sjkoshy */ 1467157144Sjkoshy 1468157144Sjkoshystatic void 1469157144Sjkoshypmc_process_kld_load(struct pmckern_map_in *pkm) 1470157144Sjkoshy{ 1471157144Sjkoshy struct pmc_owner *po; 1472157144Sjkoshy 1473157144Sjkoshy sx_assert(&pmc_sx, SX_LOCKED); 1474157144Sjkoshy 1475157144Sjkoshy /* 1476157144Sjkoshy * Notify owners of system sampling PMCs about KLD operations. 1477157144Sjkoshy */ 1478157144Sjkoshy 1479157144Sjkoshy LIST_FOREACH(po, &pmc_ss_owners, po_ssnext) 1480157144Sjkoshy if (po->po_flags & PMC_PO_OWNS_LOGFILE) 1481157144Sjkoshy pmclog_process_map_in(po, (pid_t) -1, pkm->pm_address, 1482157144Sjkoshy (char *) pkm->pm_file); 1483157144Sjkoshy 1484157144Sjkoshy /* 1485157144Sjkoshy * TODO: Notify owners of (all) process-sampling PMCs too. 1486157144Sjkoshy */ 1487157144Sjkoshy 1488157144Sjkoshy return; 1489157144Sjkoshy} 1490157144Sjkoshy 1491157144Sjkoshystatic void 1492157144Sjkoshypmc_process_kld_unload(struct pmckern_map_out *pkm) 1493157144Sjkoshy{ 1494157144Sjkoshy struct pmc_owner *po; 1495157144Sjkoshy 1496157144Sjkoshy sx_assert(&pmc_sx, SX_LOCKED); 1497157144Sjkoshy 1498157144Sjkoshy LIST_FOREACH(po, &pmc_ss_owners, po_ssnext) 1499157144Sjkoshy if (po->po_flags & PMC_PO_OWNS_LOGFILE) 1500157144Sjkoshy pmclog_process_map_out(po, (pid_t) -1, 1501157144Sjkoshy pkm->pm_address, pkm->pm_address + pkm->pm_size); 1502174395Sjkoshy 1503157144Sjkoshy /* 1504157144Sjkoshy * TODO: Notify owners of process-sampling PMCs. 1505157144Sjkoshy */ 1506157144Sjkoshy} 1507157144Sjkoshy 1508157144Sjkoshy/* 1509157144Sjkoshy * A mapping change for a process. 1510157144Sjkoshy */ 1511157144Sjkoshy 1512157144Sjkoshystatic void 1513157144Sjkoshypmc_process_mmap(struct thread *td, struct pmckern_map_in *pkm) 1514157144Sjkoshy{ 1515157144Sjkoshy int ri; 1516157144Sjkoshy pid_t pid; 1517157144Sjkoshy char *fullpath, *freepath; 1518157144Sjkoshy const struct pmc *pm; 1519157144Sjkoshy struct pmc_owner *po; 1520157144Sjkoshy const struct pmc_process *pp; 1521157144Sjkoshy 1522157144Sjkoshy freepath = fullpath = NULL; 1523157144Sjkoshy pmc_getfilename((struct vnode *) pkm->pm_file, &fullpath, &freepath); 1524157144Sjkoshy 1525157144Sjkoshy pid = td->td_proc->p_pid; 1526157144Sjkoshy 1527157144Sjkoshy /* Inform owners of all system-wide sampling PMCs. */ 1528157144Sjkoshy LIST_FOREACH(po, &pmc_ss_owners, po_ssnext) 1529157144Sjkoshy if (po->po_flags & PMC_PO_OWNS_LOGFILE) 1530157144Sjkoshy pmclog_process_map_in(po, pid, pkm->pm_address, fullpath); 1531157144Sjkoshy 1532157144Sjkoshy if ((pp = pmc_find_process_descriptor(td->td_proc, 0)) == NULL) 1533157144Sjkoshy goto done; 1534157144Sjkoshy 1535157144Sjkoshy /* 1536157144Sjkoshy * Inform sampling PMC owners tracking this process. 1537157144Sjkoshy */ 1538157144Sjkoshy for (ri = 0; ri < md->pmd_npmc; ri++) 1539157144Sjkoshy if ((pm = pp->pp_pmcs[ri].pp_pmc) != NULL && 1540157144Sjkoshy PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm))) 1541157144Sjkoshy pmclog_process_map_in(pm->pm_owner, 1542157144Sjkoshy pid, pkm->pm_address, fullpath); 1543157144Sjkoshy 1544157144Sjkoshy done: 1545157144Sjkoshy if (freepath) 1546184205Sdes free(freepath, M_TEMP); 1547157144Sjkoshy} 1548157144Sjkoshy 1549157144Sjkoshy 1550157144Sjkoshy/* 1551157144Sjkoshy * Log an munmap request. 1552157144Sjkoshy */ 1553157144Sjkoshy 1554157144Sjkoshystatic void 1555157144Sjkoshypmc_process_munmap(struct thread *td, struct pmckern_map_out *pkm) 1556157144Sjkoshy{ 1557157144Sjkoshy int ri; 1558157144Sjkoshy pid_t pid; 1559157144Sjkoshy struct pmc_owner *po; 1560157144Sjkoshy const struct pmc *pm; 1561157144Sjkoshy const struct pmc_process *pp; 1562157144Sjkoshy 1563157144Sjkoshy pid = td->td_proc->p_pid; 1564157144Sjkoshy 1565157144Sjkoshy LIST_FOREACH(po, &pmc_ss_owners, po_ssnext) 1566157144Sjkoshy if (po->po_flags & PMC_PO_OWNS_LOGFILE) 1567157144Sjkoshy pmclog_process_map_out(po, pid, pkm->pm_address, 1568157144Sjkoshy pkm->pm_address + pkm->pm_size); 1569157144Sjkoshy 1570157144Sjkoshy if ((pp = pmc_find_process_descriptor(td->td_proc, 0)) == NULL) 1571157144Sjkoshy return; 1572157144Sjkoshy 1573157144Sjkoshy for (ri = 0; ri < md->pmd_npmc; ri++) 1574157144Sjkoshy if ((pm = pp->pp_pmcs[ri].pp_pmc) != NULL && 1575157144Sjkoshy PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm))) 1576157651Sjkoshy pmclog_process_map_out(pm->pm_owner, pid, 1577157651Sjkoshy pkm->pm_address, pkm->pm_address + pkm->pm_size); 1578157144Sjkoshy} 1579157144Sjkoshy 1580157144Sjkoshy/* 1581174395Sjkoshy * Log mapping information about the kernel. 1582174395Sjkoshy */ 1583174395Sjkoshy 1584174395Sjkoshystatic void 1585174395Sjkoshypmc_log_kernel_mappings(struct pmc *pm) 1586174395Sjkoshy{ 1587174395Sjkoshy struct pmc_owner *po; 1588174395Sjkoshy struct pmckern_map_in *km, *kmbase; 1589174395Sjkoshy 1590174395Sjkoshy sx_assert(&pmc_sx, SX_LOCKED); 1591174395Sjkoshy KASSERT(PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)), 1592174395Sjkoshy ("[pmc,%d] non-sampling PMC (%p) desires mapping information", 1593174395Sjkoshy __LINE__, (void *) pm)); 1594174395Sjkoshy 1595174395Sjkoshy po = pm->pm_owner; 1596174395Sjkoshy 1597174395Sjkoshy if (po->po_flags & PMC_PO_INITIAL_MAPPINGS_DONE) 1598174395Sjkoshy return; 1599174395Sjkoshy 1600174395Sjkoshy /* 1601174395Sjkoshy * Log the current set of kernel modules. 1602174395Sjkoshy */ 1603174395Sjkoshy kmbase = linker_hwpmc_list_objects(); 1604174395Sjkoshy for (km = kmbase; km->pm_file != NULL; km++) { 1605174395Sjkoshy PMCDBG(LOG,REG,1,"%s %p", (char *) km->pm_file, 1606174395Sjkoshy (void *) km->pm_address); 1607174395Sjkoshy pmclog_process_map_in(po, (pid_t) -1, km->pm_address, 1608174395Sjkoshy km->pm_file); 1609174395Sjkoshy } 1610184205Sdes free(kmbase, M_LINKER); 1611174395Sjkoshy 1612174395Sjkoshy po->po_flags |= PMC_PO_INITIAL_MAPPINGS_DONE; 1613174395Sjkoshy} 1614174395Sjkoshy 1615174395Sjkoshy/* 1616174395Sjkoshy * Log the mappings for a single process. 1617174395Sjkoshy */ 1618174395Sjkoshy 1619174395Sjkoshystatic void 1620174395Sjkoshypmc_log_process_mappings(struct pmc_owner *po, struct proc *p) 1621174395Sjkoshy{ 1622174395Sjkoshy} 1623174395Sjkoshy 1624174395Sjkoshy/* 1625174395Sjkoshy * Log mappings for all processes in the system. 1626174395Sjkoshy */ 1627174395Sjkoshy 1628174395Sjkoshystatic void 1629174395Sjkoshypmc_log_all_process_mappings(struct pmc_owner *po) 1630174395Sjkoshy{ 1631174395Sjkoshy struct proc *p, *top; 1632174395Sjkoshy 1633174395Sjkoshy sx_assert(&pmc_sx, SX_XLOCKED); 1634174395Sjkoshy 1635174395Sjkoshy if ((p = pfind(1)) == NULL) 1636174395Sjkoshy panic("[pmc,%d] Cannot find init", __LINE__); 1637174395Sjkoshy 1638174395Sjkoshy PROC_UNLOCK(p); 1639174395Sjkoshy 1640174395Sjkoshy sx_slock(&proctree_lock); 1641174395Sjkoshy 1642174395Sjkoshy top = p; 1643174395Sjkoshy 1644174395Sjkoshy for (;;) { 1645174395Sjkoshy pmc_log_process_mappings(po, p); 1646174395Sjkoshy if (!LIST_EMPTY(&p->p_children)) 1647174395Sjkoshy p = LIST_FIRST(&p->p_children); 1648174395Sjkoshy else for (;;) { 1649174395Sjkoshy if (p == top) 1650174395Sjkoshy goto done; 1651174395Sjkoshy if (LIST_NEXT(p, p_sibling)) { 1652174395Sjkoshy p = LIST_NEXT(p, p_sibling); 1653174395Sjkoshy break; 1654174395Sjkoshy } 1655174395Sjkoshy p = p->p_pptr; 1656174395Sjkoshy } 1657174395Sjkoshy } 1658174395Sjkoshy done: 1659174395Sjkoshy sx_sunlock(&proctree_lock); 1660174395Sjkoshy} 1661174395Sjkoshy 1662174395Sjkoshy/* 1663147191Sjkoshy * The 'hook' invoked from the kernel proper 1664147191Sjkoshy */ 1665147191Sjkoshy 1666147191Sjkoshy 1667153110Sru#ifdef DEBUG 1668147191Sjkoshyconst char *pmc_hooknames[] = { 1669157144Sjkoshy /* these strings correspond to PMC_FN_* in <sys/pmckern.h> */ 1670147191Sjkoshy "", 1671147191Sjkoshy "EXEC", 1672147191Sjkoshy "CSW-IN", 1673147191Sjkoshy "CSW-OUT", 1674157144Sjkoshy "SAMPLE", 1675157144Sjkoshy "KLDLOAD", 1676157144Sjkoshy "KLDUNLOAD", 1677157144Sjkoshy "MMAP", 1678174395Sjkoshy "MUNMAP", 1679174395Sjkoshy "CALLCHAIN" 1680147191Sjkoshy}; 1681147191Sjkoshy#endif 1682147191Sjkoshy 1683147191Sjkoshystatic int 1684147191Sjkoshypmc_hook_handler(struct thread *td, int function, void *arg) 1685147191Sjkoshy{ 1686147191Sjkoshy 1687147191Sjkoshy PMCDBG(MOD,PMH,1, "hook td=%p func=%d \"%s\" arg=%p", td, function, 1688147191Sjkoshy pmc_hooknames[function], arg); 1689147191Sjkoshy 1690147191Sjkoshy switch (function) 1691147191Sjkoshy { 1692147191Sjkoshy 1693147191Sjkoshy /* 1694145256Sjkoshy * Process exec() 1695145256Sjkoshy */ 1696145256Sjkoshy 1697145256Sjkoshy case PMC_FN_PROCESS_EXEC: 1698145256Sjkoshy { 1699147191Sjkoshy char *fullpath, *freepath; 1700145256Sjkoshy unsigned int ri; 1701147191Sjkoshy int is_using_hwpmcs; 1702145256Sjkoshy struct pmc *pm; 1703145256Sjkoshy struct proc *p; 1704145256Sjkoshy struct pmc_owner *po; 1705145256Sjkoshy struct pmc_process *pp; 1706147708Sjkoshy struct pmckern_procexec *pk; 1707145256Sjkoshy 1708145256Sjkoshy sx_assert(&pmc_sx, SX_XLOCKED); 1709145256Sjkoshy 1710147191Sjkoshy p = td->td_proc; 1711147708Sjkoshy pmc_getfilename(p->p_textvp, &fullpath, &freepath); 1712147191Sjkoshy 1713147708Sjkoshy pk = (struct pmckern_procexec *) arg; 1714147708Sjkoshy 1715147191Sjkoshy /* Inform owners of SS mode PMCs of the exec event. */ 1716147191Sjkoshy LIST_FOREACH(po, &pmc_ss_owners, po_ssnext) 1717147191Sjkoshy if (po->po_flags & PMC_PO_OWNS_LOGFILE) 1718147708Sjkoshy pmclog_process_procexec(po, PMC_ID_INVALID, 1719147708Sjkoshy p->p_pid, pk->pm_entryaddr, fullpath); 1720147191Sjkoshy 1721147191Sjkoshy PROC_LOCK(p); 1722147191Sjkoshy is_using_hwpmcs = p->p_flag & P_HWPMC; 1723147191Sjkoshy PROC_UNLOCK(p); 1724147191Sjkoshy 1725147191Sjkoshy if (!is_using_hwpmcs) { 1726147191Sjkoshy if (freepath) 1727184205Sdes free(freepath, M_TEMP); 1728147191Sjkoshy break; 1729147191Sjkoshy } 1730147191Sjkoshy 1731145256Sjkoshy /* 1732145256Sjkoshy * PMCs are not inherited across an exec(): remove any 1733145256Sjkoshy * PMCs that this process is the owner of. 1734145256Sjkoshy */ 1735145256Sjkoshy 1736145256Sjkoshy if ((po = pmc_find_owner_descriptor(p)) != NULL) { 1737145256Sjkoshy pmc_remove_owner(po); 1738147191Sjkoshy pmc_destroy_owner_descriptor(po); 1739145256Sjkoshy } 1740145256Sjkoshy 1741145256Sjkoshy /* 1742154483Sjkoshy * If the process being exec'ed is not the target of any 1743154483Sjkoshy * PMC, we are done. 1744145256Sjkoshy */ 1745154483Sjkoshy if ((pp = pmc_find_process_descriptor(p, 0)) == NULL) { 1746154483Sjkoshy if (freepath) 1747184205Sdes free(freepath, M_TEMP); 1748145256Sjkoshy break; 1749154483Sjkoshy } 1750145256Sjkoshy 1751147191Sjkoshy /* 1752147191Sjkoshy * Log the exec event to all monitoring owners. Skip 1753147191Sjkoshy * owners who have already recieved the event because 1754154483Sjkoshy * they had system sampling PMCs active. 1755147191Sjkoshy */ 1756147191Sjkoshy for (ri = 0; ri < md->pmd_npmc; ri++) 1757147191Sjkoshy if ((pm = pp->pp_pmcs[ri].pp_pmc) != NULL) { 1758147191Sjkoshy po = pm->pm_owner; 1759147191Sjkoshy if (po->po_sscount == 0 && 1760147191Sjkoshy po->po_flags & PMC_PO_OWNS_LOGFILE) 1761147708Sjkoshy pmclog_process_procexec(po, pm->pm_id, 1762147708Sjkoshy p->p_pid, pk->pm_entryaddr, 1763147191Sjkoshy fullpath); 1764147191Sjkoshy } 1765147191Sjkoshy 1766147191Sjkoshy if (freepath) 1767184205Sdes free(freepath, M_TEMP); 1768147191Sjkoshy 1769145256Sjkoshy 1770145256Sjkoshy PMCDBG(PRC,EXC,1, "exec proc=%p (%d, %s) cred-changed=%d", 1771147708Sjkoshy p, p->p_pid, p->p_comm, pk->pm_credentialschanged); 1772145256Sjkoshy 1773147708Sjkoshy if (pk->pm_credentialschanged == 0) /* no change */ 1774145256Sjkoshy break; 1775145256Sjkoshy 1776145256Sjkoshy /* 1777145256Sjkoshy * If the newly exec()'ed process has a different credential 1778145256Sjkoshy * than before, allow it to be the target of a PMC only if 1779145256Sjkoshy * the PMC's owner has sufficient priviledge. 1780145256Sjkoshy */ 1781145256Sjkoshy 1782145256Sjkoshy for (ri = 0; ri < md->pmd_npmc; ri++) 1783145256Sjkoshy if ((pm = pp->pp_pmcs[ri].pp_pmc) != NULL) 1784145256Sjkoshy if (pmc_can_attach(pm, td->td_proc) != 0) 1785145256Sjkoshy pmc_detach_one_process(td->td_proc, 1786145256Sjkoshy pm, PMC_FLAG_NONE); 1787145256Sjkoshy 1788198343Sfabient KASSERT(pp->pp_refcnt >= 0 && pp->pp_refcnt <= (int) md->pmd_npmc, 1789145256Sjkoshy ("[pmc,%d] Illegal ref count %d on pp %p", __LINE__, 1790145256Sjkoshy pp->pp_refcnt, pp)); 1791145256Sjkoshy 1792145256Sjkoshy /* 1793145256Sjkoshy * If this process is no longer the target of any 1794145256Sjkoshy * PMCs, we can remove the process entry and free 1795145256Sjkoshy * up space. 1796145256Sjkoshy */ 1797145256Sjkoshy 1798145256Sjkoshy if (pp->pp_refcnt == 0) { 1799145256Sjkoshy pmc_remove_process_descriptor(pp); 1800184205Sdes free(pp, M_PMC); 1801147191Sjkoshy break; 1802145256Sjkoshy } 1803145256Sjkoshy 1804145256Sjkoshy } 1805145256Sjkoshy break; 1806145256Sjkoshy 1807145256Sjkoshy case PMC_FN_CSW_IN: 1808147191Sjkoshy pmc_process_csw_in(td); 1809147191Sjkoshy break; 1810145256Sjkoshy 1811147191Sjkoshy case PMC_FN_CSW_OUT: 1812147191Sjkoshy pmc_process_csw_out(td); 1813147191Sjkoshy break; 1814145256Sjkoshy 1815145256Sjkoshy /* 1816147191Sjkoshy * Process accumulated PC samples. 1817147191Sjkoshy * 1818147191Sjkoshy * This function is expected to be called by hardclock() for 1819147191Sjkoshy * each CPU that has accumulated PC samples. 1820147191Sjkoshy * 1821147191Sjkoshy * This function is to be executed on the CPU whose samples 1822147191Sjkoshy * are being processed. 1823145256Sjkoshy */ 1824147191Sjkoshy case PMC_FN_DO_SAMPLES: 1825145256Sjkoshy 1826145256Sjkoshy /* 1827147191Sjkoshy * Clear the cpu specific bit in the CPU mask before 1828147191Sjkoshy * do the rest of the processing. If the NMI handler 1829147191Sjkoshy * gets invoked after the "atomic_clear_int()" call 1830147191Sjkoshy * below but before "pmc_process_samples()" gets 1831147191Sjkoshy * around to processing the interrupt, then we will 1832147191Sjkoshy * come back here at the next hardclock() tick (and 1833147191Sjkoshy * may find nothing to do if "pmc_process_samples()" 1834147191Sjkoshy * had already processed the interrupt). We don't 1835147191Sjkoshy * lose the interrupt sample. 1836145256Sjkoshy */ 1837147191Sjkoshy atomic_clear_int(&pmc_cpumask, (1 << PCPU_GET(cpuid))); 1838147191Sjkoshy pmc_process_samples(PCPU_GET(cpuid)); 1839147191Sjkoshy break; 1840145256Sjkoshy 1841157144Sjkoshy 1842157144Sjkoshy case PMC_FN_KLD_LOAD: 1843157144Sjkoshy sx_assert(&pmc_sx, SX_LOCKED); 1844157144Sjkoshy pmc_process_kld_load((struct pmckern_map_in *) arg); 1845157144Sjkoshy break; 1846157144Sjkoshy 1847157144Sjkoshy case PMC_FN_KLD_UNLOAD: 1848157144Sjkoshy sx_assert(&pmc_sx, SX_LOCKED); 1849157144Sjkoshy pmc_process_kld_unload((struct pmckern_map_out *) arg); 1850157144Sjkoshy break; 1851157144Sjkoshy 1852157144Sjkoshy case PMC_FN_MMAP: 1853157144Sjkoshy sx_assert(&pmc_sx, SX_LOCKED); 1854157144Sjkoshy pmc_process_mmap(td, (struct pmckern_map_in *) arg); 1855157144Sjkoshy break; 1856157144Sjkoshy 1857157144Sjkoshy case PMC_FN_MUNMAP: 1858157144Sjkoshy sx_assert(&pmc_sx, SX_LOCKED); 1859157144Sjkoshy pmc_process_munmap(td, (struct pmckern_map_out *) arg); 1860157144Sjkoshy break; 1861157144Sjkoshy 1862174395Sjkoshy case PMC_FN_USER_CALLCHAIN: 1863174395Sjkoshy /* 1864174395Sjkoshy * Record a call chain. 1865174395Sjkoshy */ 1866186037Sjkoshy KASSERT(td == curthread, ("[pmc,%d] td != curthread", 1867186037Sjkoshy __LINE__)); 1868174395Sjkoshy pmc_capture_user_callchain(PCPU_GET(cpuid), 1869174395Sjkoshy (struct trapframe *) arg); 1870186037Sjkoshy td->td_pflags &= ~TDP_CALLCHAIN; 1871174395Sjkoshy break; 1872174395Sjkoshy 1873145256Sjkoshy default: 1874153110Sru#ifdef DEBUG 1875145256Sjkoshy KASSERT(0, ("[pmc,%d] unknown hook %d\n", __LINE__, function)); 1876145256Sjkoshy#endif 1877145256Sjkoshy break; 1878145256Sjkoshy 1879145256Sjkoshy } 1880145256Sjkoshy 1881145256Sjkoshy return 0; 1882145256Sjkoshy} 1883145256Sjkoshy 1884145256Sjkoshy/* 1885145256Sjkoshy * allocate a 'struct pmc_owner' descriptor in the owner hash table. 1886145256Sjkoshy */ 1887145256Sjkoshy 1888145256Sjkoshystatic struct pmc_owner * 1889145256Sjkoshypmc_allocate_owner_descriptor(struct proc *p) 1890145256Sjkoshy{ 1891145256Sjkoshy uint32_t hindex; 1892145256Sjkoshy struct pmc_owner *po; 1893145256Sjkoshy struct pmc_ownerhash *poh; 1894145256Sjkoshy 1895145256Sjkoshy hindex = PMC_HASH_PTR(p, pmc_ownerhashmask); 1896145256Sjkoshy poh = &pmc_ownerhash[hindex]; 1897145256Sjkoshy 1898145256Sjkoshy /* allocate space for N pointers and one descriptor struct */ 1899184802Sjkoshy po = malloc(sizeof(struct pmc_owner), M_PMC, M_WAITOK|M_ZERO); 1900147191Sjkoshy po->po_sscount = po->po_error = po->po_flags = 0; 1901147191Sjkoshy po->po_file = NULL; 1902145256Sjkoshy po->po_owner = p; 1903147191Sjkoshy po->po_kthread = NULL; 1904145256Sjkoshy LIST_INIT(&po->po_pmcs); 1905145256Sjkoshy LIST_INSERT_HEAD(poh, po, po_next); /* insert into hash table */ 1906145256Sjkoshy 1907147191Sjkoshy TAILQ_INIT(&po->po_logbuffers); 1908168856Sjkoshy mtx_init(&po->po_mtx, "pmc-owner-mtx", "pmc-per-proc", MTX_SPIN); 1909147191Sjkoshy 1910145256Sjkoshy PMCDBG(OWN,ALL,1, "allocate-owner proc=%p (%d, %s) pmc-owner=%p", 1911145256Sjkoshy p, p->p_pid, p->p_comm, po); 1912145256Sjkoshy 1913145256Sjkoshy return po; 1914145256Sjkoshy} 1915145256Sjkoshy 1916147191Sjkoshystatic void 1917147191Sjkoshypmc_destroy_owner_descriptor(struct pmc_owner *po) 1918147191Sjkoshy{ 1919147191Sjkoshy 1920147191Sjkoshy PMCDBG(OWN,REL,1, "destroy-owner po=%p proc=%p (%d, %s)", 1921147191Sjkoshy po, po->po_owner, po->po_owner->p_pid, po->po_owner->p_comm); 1922147191Sjkoshy 1923147191Sjkoshy mtx_destroy(&po->po_mtx); 1924184205Sdes free(po, M_PMC); 1925147191Sjkoshy} 1926147191Sjkoshy 1927145256Sjkoshy/* 1928145256Sjkoshy * find the descriptor corresponding to process 'p', adding or removing it 1929145256Sjkoshy * as specified by 'mode'. 1930145256Sjkoshy */ 1931145256Sjkoshy 1932145256Sjkoshystatic struct pmc_process * 1933145256Sjkoshypmc_find_process_descriptor(struct proc *p, uint32_t mode) 1934145256Sjkoshy{ 1935145256Sjkoshy uint32_t hindex; 1936145256Sjkoshy struct pmc_process *pp, *ppnew; 1937145256Sjkoshy struct pmc_processhash *pph; 1938145256Sjkoshy 1939145256Sjkoshy hindex = PMC_HASH_PTR(p, pmc_processhashmask); 1940145256Sjkoshy pph = &pmc_processhash[hindex]; 1941145256Sjkoshy 1942145256Sjkoshy ppnew = NULL; 1943145256Sjkoshy 1944145256Sjkoshy /* 1945145256Sjkoshy * Pre-allocate memory in the FIND_ALLOCATE case since we 1946145256Sjkoshy * cannot call malloc(9) once we hold a spin lock. 1947145256Sjkoshy */ 1948184802Sjkoshy if (mode & PMC_FLAG_ALLOCATE) 1949184214Sdes ppnew = malloc(sizeof(struct pmc_process) + md->pmd_npmc * 1950184802Sjkoshy sizeof(struct pmc_targetstate), M_PMC, M_WAITOK|M_ZERO); 1951145256Sjkoshy 1952145256Sjkoshy mtx_lock_spin(&pmc_processhash_mtx); 1953145256Sjkoshy LIST_FOREACH(pp, pph, pp_next) 1954145256Sjkoshy if (pp->pp_proc == p) 1955145256Sjkoshy break; 1956145256Sjkoshy 1957145256Sjkoshy if ((mode & PMC_FLAG_REMOVE) && pp != NULL) 1958145256Sjkoshy LIST_REMOVE(pp, pp_next); 1959145256Sjkoshy 1960145256Sjkoshy if ((mode & PMC_FLAG_ALLOCATE) && pp == NULL && 1961145256Sjkoshy ppnew != NULL) { 1962145256Sjkoshy ppnew->pp_proc = p; 1963145256Sjkoshy LIST_INSERT_HEAD(pph, ppnew, pp_next); 1964145256Sjkoshy pp = ppnew; 1965145256Sjkoshy ppnew = NULL; 1966145256Sjkoshy } 1967145256Sjkoshy mtx_unlock_spin(&pmc_processhash_mtx); 1968145256Sjkoshy 1969145256Sjkoshy if (pp != NULL && ppnew != NULL) 1970184205Sdes free(ppnew, M_PMC); 1971145256Sjkoshy 1972145256Sjkoshy return pp; 1973145256Sjkoshy} 1974145256Sjkoshy 1975145256Sjkoshy/* 1976145256Sjkoshy * remove a process descriptor from the process hash table. 1977145256Sjkoshy */ 1978145256Sjkoshy 1979145256Sjkoshystatic void 1980145256Sjkoshypmc_remove_process_descriptor(struct pmc_process *pp) 1981145256Sjkoshy{ 1982145256Sjkoshy KASSERT(pp->pp_refcnt == 0, 1983145256Sjkoshy ("[pmc,%d] Removing process descriptor %p with count %d", 1984145256Sjkoshy __LINE__, pp, pp->pp_refcnt)); 1985145256Sjkoshy 1986145256Sjkoshy mtx_lock_spin(&pmc_processhash_mtx); 1987145256Sjkoshy LIST_REMOVE(pp, pp_next); 1988145256Sjkoshy mtx_unlock_spin(&pmc_processhash_mtx); 1989145256Sjkoshy} 1990145256Sjkoshy 1991145256Sjkoshy 1992145256Sjkoshy/* 1993145256Sjkoshy * find an owner descriptor corresponding to proc 'p' 1994145256Sjkoshy */ 1995145256Sjkoshy 1996145256Sjkoshystatic struct pmc_owner * 1997145256Sjkoshypmc_find_owner_descriptor(struct proc *p) 1998145256Sjkoshy{ 1999145256Sjkoshy uint32_t hindex; 2000145256Sjkoshy struct pmc_owner *po; 2001145256Sjkoshy struct pmc_ownerhash *poh; 2002145256Sjkoshy 2003145256Sjkoshy hindex = PMC_HASH_PTR(p, pmc_ownerhashmask); 2004145256Sjkoshy poh = &pmc_ownerhash[hindex]; 2005145256Sjkoshy 2006145256Sjkoshy po = NULL; 2007145256Sjkoshy LIST_FOREACH(po, poh, po_next) 2008145256Sjkoshy if (po->po_owner == p) 2009145256Sjkoshy break; 2010145256Sjkoshy 2011145256Sjkoshy PMCDBG(OWN,FND,1, "find-owner proc=%p (%d, %s) hindex=0x%x -> " 2012145256Sjkoshy "pmc-owner=%p", p, p->p_pid, p->p_comm, hindex, po); 2013145256Sjkoshy 2014145256Sjkoshy return po; 2015145256Sjkoshy} 2016145256Sjkoshy 2017145256Sjkoshy/* 2018145256Sjkoshy * pmc_allocate_pmc_descriptor 2019145256Sjkoshy * 2020145256Sjkoshy * Allocate a pmc descriptor and initialize its 2021145256Sjkoshy * fields. 2022145256Sjkoshy */ 2023145256Sjkoshy 2024145256Sjkoshystatic struct pmc * 2025145256Sjkoshypmc_allocate_pmc_descriptor(void) 2026145256Sjkoshy{ 2027145256Sjkoshy struct pmc *pmc; 2028145256Sjkoshy 2029184802Sjkoshy pmc = malloc(sizeof(struct pmc), M_PMC, M_WAITOK|M_ZERO); 2030145256Sjkoshy 2031145256Sjkoshy if (pmc != NULL) { 2032145256Sjkoshy pmc->pm_owner = NULL; 2033145256Sjkoshy LIST_INIT(&pmc->pm_targets); 2034145256Sjkoshy } 2035145256Sjkoshy 2036145256Sjkoshy PMCDBG(PMC,ALL,1, "allocate-pmc -> pmc=%p", pmc); 2037145256Sjkoshy 2038145256Sjkoshy return pmc; 2039145256Sjkoshy} 2040145256Sjkoshy 2041145256Sjkoshy/* 2042145256Sjkoshy * Destroy a pmc descriptor. 2043145256Sjkoshy */ 2044145256Sjkoshy 2045145256Sjkoshystatic void 2046145256Sjkoshypmc_destroy_pmc_descriptor(struct pmc *pm) 2047145256Sjkoshy{ 2048145256Sjkoshy (void) pm; 2049145256Sjkoshy 2050153110Sru#ifdef DEBUG 2051145256Sjkoshy KASSERT(pm->pm_state == PMC_STATE_DELETED || 2052145256Sjkoshy pm->pm_state == PMC_STATE_FREE, 2053145256Sjkoshy ("[pmc,%d] destroying non-deleted PMC", __LINE__)); 2054145256Sjkoshy KASSERT(LIST_EMPTY(&pm->pm_targets), 2055145256Sjkoshy ("[pmc,%d] destroying pmc with targets", __LINE__)); 2056145256Sjkoshy KASSERT(pm->pm_owner == NULL, 2057145256Sjkoshy ("[pmc,%d] destroying pmc attached to an owner", __LINE__)); 2058145256Sjkoshy KASSERT(pm->pm_runcount == 0, 2059145256Sjkoshy ("[pmc,%d] pmc has non-zero run count %d", __LINE__, 2060145256Sjkoshy pm->pm_runcount)); 2061145256Sjkoshy#endif 2062145256Sjkoshy} 2063145256Sjkoshy 2064147191Sjkoshystatic void 2065147191Sjkoshypmc_wait_for_pmc_idle(struct pmc *pm) 2066147191Sjkoshy{ 2067153110Sru#ifdef DEBUG 2068147191Sjkoshy volatile int maxloop; 2069147191Sjkoshy 2070183266Sjkoshy maxloop = 100 * pmc_cpu_max(); 2071147191Sjkoshy#endif 2072147191Sjkoshy 2073147191Sjkoshy /* 2074147191Sjkoshy * Loop (with a forced context switch) till the PMC's runcount 2075147191Sjkoshy * comes down to zero. 2076147191Sjkoshy */ 2077147191Sjkoshy while (atomic_load_acq_32(&pm->pm_runcount) > 0) { 2078153110Sru#ifdef DEBUG 2079147191Sjkoshy maxloop--; 2080147191Sjkoshy KASSERT(maxloop > 0, 2081147191Sjkoshy ("[pmc,%d] (ri%d, rc%d) waiting too long for " 2082147191Sjkoshy "pmc to be free", __LINE__, 2083147191Sjkoshy PMC_TO_ROWINDEX(pm), pm->pm_runcount)); 2084147191Sjkoshy#endif 2085147191Sjkoshy pmc_force_context_switch(); 2086147191Sjkoshy } 2087147191Sjkoshy} 2088147191Sjkoshy 2089145256Sjkoshy/* 2090145256Sjkoshy * This function does the following things: 2091145256Sjkoshy * 2092145256Sjkoshy * - detaches the PMC from hardware 2093145256Sjkoshy * - unlinks all target threads that were attached to it 2094145256Sjkoshy * - removes the PMC from its owner's list 2095145256Sjkoshy * - destroy's the PMC private mutex 2096145256Sjkoshy * 2097145256Sjkoshy * Once this function completes, the given pmc pointer can be safely 2098145256Sjkoshy * FREE'd by the caller. 2099145256Sjkoshy */ 2100145256Sjkoshy 2101145256Sjkoshystatic void 2102145256Sjkoshypmc_release_pmc_descriptor(struct pmc *pm) 2103145256Sjkoshy{ 2104145774Sjkoshy enum pmc_mode mode; 2105145256Sjkoshy struct pmc_hw *phw; 2106184802Sjkoshy u_int adjri, ri, cpu; 2107147191Sjkoshy struct pmc_owner *po; 2108184802Sjkoshy struct pmc_binding pb; 2109145256Sjkoshy struct pmc_process *pp; 2110184802Sjkoshy struct pmc_classdep *pcd; 2111145256Sjkoshy struct pmc_target *ptgt, *tmp; 2112145256Sjkoshy 2113145256Sjkoshy sx_assert(&pmc_sx, SX_XLOCKED); 2114145256Sjkoshy 2115145256Sjkoshy KASSERT(pm, ("[pmc,%d] null pmc", __LINE__)); 2116145256Sjkoshy 2117145774Sjkoshy ri = PMC_TO_ROWINDEX(pm); 2118184802Sjkoshy pcd = pmc_ri_to_classdep(md, ri, &adjri); 2119145774Sjkoshy mode = PMC_TO_MODE(pm); 2120145256Sjkoshy 2121145256Sjkoshy PMCDBG(PMC,REL,1, "release-pmc pmc=%p ri=%d mode=%d", pm, ri, 2122145774Sjkoshy mode); 2123145256Sjkoshy 2124145256Sjkoshy /* 2125145256Sjkoshy * First, we take the PMC off hardware. 2126145256Sjkoshy */ 2127145301Simp cpu = 0; 2128145774Sjkoshy if (PMC_IS_SYSTEM_MODE(mode)) { 2129145256Sjkoshy 2130145256Sjkoshy /* 2131145256Sjkoshy * A system mode PMC runs on a specific CPU. Switch 2132145256Sjkoshy * to this CPU and turn hardware off. 2133145256Sjkoshy */ 2134145256Sjkoshy pmc_save_cpu_binding(&pb); 2135145256Sjkoshy 2136145774Sjkoshy cpu = PMC_TO_CPU(pm); 2137145256Sjkoshy 2138147191Sjkoshy pmc_select_cpu(cpu); 2139145256Sjkoshy 2140147191Sjkoshy /* switch off non-stalled CPUs */ 2141147191Sjkoshy if (pm->pm_state == PMC_STATE_RUNNING && 2142147867Sjkoshy pm->pm_stalled == 0) { 2143145256Sjkoshy 2144145256Sjkoshy phw = pmc_pcpu[cpu]->pc_hwpmcs[ri]; 2145145256Sjkoshy 2146145256Sjkoshy KASSERT(phw->phw_pmc == pm, 2147145256Sjkoshy ("[pmc, %d] pmc ptr ri(%d) hw(%p) pm(%p)", 2148145256Sjkoshy __LINE__, ri, phw->phw_pmc, pm)); 2149145256Sjkoshy PMCDBG(PMC,REL,2, "stopping cpu=%d ri=%d", cpu, ri); 2150145256Sjkoshy 2151145256Sjkoshy critical_enter(); 2152184802Sjkoshy pcd->pcd_stop_pmc(cpu, adjri); 2153145256Sjkoshy critical_exit(); 2154145256Sjkoshy } 2155145256Sjkoshy 2156145256Sjkoshy PMCDBG(PMC,REL,2, "decfg cpu=%d ri=%d", cpu, ri); 2157145256Sjkoshy 2158145256Sjkoshy critical_enter(); 2159184802Sjkoshy pcd->pcd_config_pmc(cpu, adjri, NULL); 2160145256Sjkoshy critical_exit(); 2161145256Sjkoshy 2162147191Sjkoshy /* adjust the global and process count of SS mode PMCs */ 2163147191Sjkoshy if (mode == PMC_MODE_SS && pm->pm_state == PMC_STATE_RUNNING) { 2164147191Sjkoshy po = pm->pm_owner; 2165147191Sjkoshy po->po_sscount--; 2166147191Sjkoshy if (po->po_sscount == 0) { 2167147191Sjkoshy atomic_subtract_rel_int(&pmc_ss_count, 1); 2168147191Sjkoshy LIST_REMOVE(po, po_ssnext); 2169147191Sjkoshy } 2170147191Sjkoshy } 2171147191Sjkoshy 2172145256Sjkoshy pm->pm_state = PMC_STATE_DELETED; 2173145256Sjkoshy 2174145256Sjkoshy pmc_restore_cpu_binding(&pb); 2175145256Sjkoshy 2176147191Sjkoshy /* 2177147191Sjkoshy * We could have references to this PMC structure in 2178147191Sjkoshy * the per-cpu sample queues. Wait for the queue to 2179147191Sjkoshy * drain. 2180147191Sjkoshy */ 2181147191Sjkoshy pmc_wait_for_pmc_idle(pm); 2182147191Sjkoshy 2183145774Sjkoshy } else if (PMC_IS_VIRTUAL_MODE(mode)) { 2184145256Sjkoshy 2185145256Sjkoshy /* 2186145256Sjkoshy * A virtual PMC could be running on multiple CPUs at 2187145256Sjkoshy * a given instant. 2188145256Sjkoshy * 2189145256Sjkoshy * By marking its state as DELETED, we ensure that 2190145256Sjkoshy * this PMC is never further scheduled on hardware. 2191145256Sjkoshy * 2192145256Sjkoshy * Then we wait till all CPUs are done with this PMC. 2193145256Sjkoshy */ 2194145256Sjkoshy pm->pm_state = PMC_STATE_DELETED; 2195145256Sjkoshy 2196145256Sjkoshy 2197147191Sjkoshy /* Wait for the PMCs runcount to come to zero. */ 2198147191Sjkoshy pmc_wait_for_pmc_idle(pm); 2199145256Sjkoshy 2200145256Sjkoshy /* 2201145256Sjkoshy * At this point the PMC is off all CPUs and cannot be 2202145256Sjkoshy * freshly scheduled onto a CPU. It is now safe to 2203145256Sjkoshy * unlink all targets from this PMC. If a 2204145256Sjkoshy * process-record's refcount falls to zero, we remove 2205145256Sjkoshy * it from the hash table. The module-wide SX lock 2206145256Sjkoshy * protects us from races. 2207145256Sjkoshy */ 2208145256Sjkoshy LIST_FOREACH_SAFE(ptgt, &pm->pm_targets, pt_next, tmp) { 2209145256Sjkoshy pp = ptgt->pt_process; 2210145256Sjkoshy pmc_unlink_target_process(pm, pp); /* frees 'ptgt' */ 2211145256Sjkoshy 2212145256Sjkoshy PMCDBG(PMC,REL,3, "pp->refcnt=%d", pp->pp_refcnt); 2213145256Sjkoshy 2214145256Sjkoshy /* 2215145256Sjkoshy * If the target process record shows that no 2216145256Sjkoshy * PMCs are attached to it, reclaim its space. 2217145256Sjkoshy */ 2218145256Sjkoshy 2219145256Sjkoshy if (pp->pp_refcnt == 0) { 2220145256Sjkoshy pmc_remove_process_descriptor(pp); 2221184205Sdes free(pp, M_PMC); 2222145256Sjkoshy } 2223145256Sjkoshy } 2224145256Sjkoshy 2225145256Sjkoshy cpu = curthread->td_oncpu; /* setup cpu for pmd_release() */ 2226145256Sjkoshy 2227145256Sjkoshy } 2228145256Sjkoshy 2229145256Sjkoshy /* 2230145256Sjkoshy * Release any MD resources 2231145256Sjkoshy */ 2232184802Sjkoshy (void) pcd->pcd_release_pmc(cpu, adjri, pm); 2233145256Sjkoshy 2234145256Sjkoshy /* 2235145256Sjkoshy * Update row disposition 2236145256Sjkoshy */ 2237145256Sjkoshy 2238145774Sjkoshy if (PMC_IS_SYSTEM_MODE(PMC_TO_MODE(pm))) 2239145256Sjkoshy PMC_UNMARK_ROW_STANDALONE(ri); 2240145256Sjkoshy else 2241145256Sjkoshy PMC_UNMARK_ROW_THREAD(ri); 2242145256Sjkoshy 2243145256Sjkoshy /* unlink from the owner's list */ 2244147191Sjkoshy if (pm->pm_owner) { 2245147191Sjkoshy LIST_REMOVE(pm, pm_next); 2246147191Sjkoshy pm->pm_owner = NULL; 2247147191Sjkoshy } 2248145256Sjkoshy 2249145256Sjkoshy pmc_destroy_pmc_descriptor(pm); 2250145256Sjkoshy} 2251145256Sjkoshy 2252145256Sjkoshy/* 2253145256Sjkoshy * Register an owner and a pmc. 2254145256Sjkoshy */ 2255145256Sjkoshy 2256145256Sjkoshystatic int 2257145256Sjkoshypmc_register_owner(struct proc *p, struct pmc *pmc) 2258145256Sjkoshy{ 2259145256Sjkoshy struct pmc_owner *po; 2260145256Sjkoshy 2261145256Sjkoshy sx_assert(&pmc_sx, SX_XLOCKED); 2262145256Sjkoshy 2263145774Sjkoshy if ((po = pmc_find_owner_descriptor(p)) == NULL) 2264147191Sjkoshy if ((po = pmc_allocate_owner_descriptor(p)) == NULL) 2265145256Sjkoshy return ENOMEM; 2266145256Sjkoshy 2267145256Sjkoshy KASSERT(pmc->pm_owner == NULL, 2268145256Sjkoshy ("[pmc,%d] attempting to own an initialized PMC", __LINE__)); 2269145256Sjkoshy pmc->pm_owner = po; 2270145256Sjkoshy 2271147191Sjkoshy LIST_INSERT_HEAD(&po->po_pmcs, pmc, pm_next); 2272145256Sjkoshy 2273145256Sjkoshy PROC_LOCK(p); 2274145256Sjkoshy p->p_flag |= P_HWPMC; 2275145256Sjkoshy PROC_UNLOCK(p); 2276145256Sjkoshy 2277147191Sjkoshy if (po->po_flags & PMC_PO_OWNS_LOGFILE) 2278147191Sjkoshy pmclog_process_pmcallocate(pmc); 2279145256Sjkoshy 2280147191Sjkoshy PMCDBG(PMC,REG,1, "register-owner pmc-owner=%p pmc=%p", 2281147191Sjkoshy po, pmc); 2282147191Sjkoshy 2283145256Sjkoshy return 0; 2284145256Sjkoshy} 2285145256Sjkoshy 2286145256Sjkoshy/* 2287145256Sjkoshy * Return the current row disposition: 2288145256Sjkoshy * == 0 => FREE 2289145256Sjkoshy * > 0 => PROCESS MODE 2290145256Sjkoshy * < 0 => SYSTEM MODE 2291145256Sjkoshy */ 2292145256Sjkoshy 2293145256Sjkoshyint 2294145256Sjkoshypmc_getrowdisp(int ri) 2295145256Sjkoshy{ 2296145256Sjkoshy return pmc_pmcdisp[ri]; 2297145256Sjkoshy} 2298145256Sjkoshy 2299145256Sjkoshy/* 2300145256Sjkoshy * Check if a PMC at row index 'ri' can be allocated to the current 2301145256Sjkoshy * process. 2302145256Sjkoshy * 2303145256Sjkoshy * Allocation can fail if: 2304145256Sjkoshy * - the current process is already being profiled by a PMC at index 'ri', 2305145256Sjkoshy * attached to it via OP_PMCATTACH. 2306145256Sjkoshy * - the current process has already allocated a PMC at index 'ri' 2307145256Sjkoshy * via OP_ALLOCATE. 2308145256Sjkoshy */ 2309145256Sjkoshy 2310145256Sjkoshystatic int 2311145774Sjkoshypmc_can_allocate_rowindex(struct proc *p, unsigned int ri, int cpu) 2312145256Sjkoshy{ 2313145774Sjkoshy enum pmc_mode mode; 2314145774Sjkoshy struct pmc *pm; 2315145256Sjkoshy struct pmc_owner *po; 2316145256Sjkoshy struct pmc_process *pp; 2317145256Sjkoshy 2318145774Sjkoshy PMCDBG(PMC,ALR,1, "can-allocate-rowindex proc=%p (%d, %s) ri=%d " 2319145774Sjkoshy "cpu=%d", p, p->p_pid, p->p_comm, ri, cpu); 2320145256Sjkoshy 2321145774Sjkoshy /* 2322145774Sjkoshy * We shouldn't have already allocated a process-mode PMC at 2323145774Sjkoshy * row index 'ri'. 2324145774Sjkoshy * 2325145774Sjkoshy * We shouldn't have allocated a system-wide PMC on the same 2326145774Sjkoshy * CPU and same RI. 2327145774Sjkoshy */ 2328145256Sjkoshy if ((po = pmc_find_owner_descriptor(p)) != NULL) 2329147191Sjkoshy LIST_FOREACH(pm, &po->po_pmcs, pm_next) { 2330145774Sjkoshy if (PMC_TO_ROWINDEX(pm) == ri) { 2331145774Sjkoshy mode = PMC_TO_MODE(pm); 2332145774Sjkoshy if (PMC_IS_VIRTUAL_MODE(mode)) 2333145774Sjkoshy return EEXIST; 2334145774Sjkoshy if (PMC_IS_SYSTEM_MODE(mode) && 2335145774Sjkoshy (int) PMC_TO_CPU(pm) == cpu) 2336145774Sjkoshy return EEXIST; 2337145774Sjkoshy } 2338145774Sjkoshy } 2339145256Sjkoshy 2340145774Sjkoshy /* 2341145774Sjkoshy * We also shouldn't be the target of any PMC at this index 2342145774Sjkoshy * since otherwise a PMC_ATTACH to ourselves will fail. 2343145774Sjkoshy */ 2344145256Sjkoshy if ((pp = pmc_find_process_descriptor(p, 0)) != NULL) 2345145256Sjkoshy if (pp->pp_pmcs[ri].pp_pmc) 2346145256Sjkoshy return EEXIST; 2347145256Sjkoshy 2348145256Sjkoshy PMCDBG(PMC,ALR,2, "can-allocate-rowindex proc=%p (%d, %s) ri=%d ok", 2349145256Sjkoshy p, p->p_pid, p->p_comm, ri); 2350145256Sjkoshy 2351145256Sjkoshy return 0; 2352145256Sjkoshy} 2353145256Sjkoshy 2354145256Sjkoshy/* 2355145256Sjkoshy * Check if a given PMC at row index 'ri' can be currently used in 2356145256Sjkoshy * mode 'mode'. 2357145256Sjkoshy */ 2358145256Sjkoshy 2359145256Sjkoshystatic int 2360145256Sjkoshypmc_can_allocate_row(int ri, enum pmc_mode mode) 2361145256Sjkoshy{ 2362145256Sjkoshy enum pmc_disp disp; 2363145256Sjkoshy 2364145256Sjkoshy sx_assert(&pmc_sx, SX_XLOCKED); 2365145256Sjkoshy 2366145256Sjkoshy PMCDBG(PMC,ALR,1, "can-allocate-row ri=%d mode=%d", ri, mode); 2367145256Sjkoshy 2368145256Sjkoshy if (PMC_IS_SYSTEM_MODE(mode)) 2369145256Sjkoshy disp = PMC_DISP_STANDALONE; 2370145256Sjkoshy else 2371145256Sjkoshy disp = PMC_DISP_THREAD; 2372145256Sjkoshy 2373145256Sjkoshy /* 2374145256Sjkoshy * check disposition for PMC row 'ri': 2375145256Sjkoshy * 2376145256Sjkoshy * Expected disposition Row-disposition Result 2377145256Sjkoshy * 2378145256Sjkoshy * STANDALONE STANDALONE or FREE proceed 2379145256Sjkoshy * STANDALONE THREAD fail 2380145256Sjkoshy * THREAD THREAD or FREE proceed 2381145256Sjkoshy * THREAD STANDALONE fail 2382145256Sjkoshy */ 2383145256Sjkoshy 2384145256Sjkoshy if (!PMC_ROW_DISP_IS_FREE(ri) && 2385145256Sjkoshy !(disp == PMC_DISP_THREAD && PMC_ROW_DISP_IS_THREAD(ri)) && 2386145256Sjkoshy !(disp == PMC_DISP_STANDALONE && PMC_ROW_DISP_IS_STANDALONE(ri))) 2387145256Sjkoshy return EBUSY; 2388145256Sjkoshy 2389145256Sjkoshy /* 2390145256Sjkoshy * All OK 2391145256Sjkoshy */ 2392145256Sjkoshy 2393145256Sjkoshy PMCDBG(PMC,ALR,2, "can-allocate-row ri=%d mode=%d ok", ri, mode); 2394145256Sjkoshy 2395145256Sjkoshy return 0; 2396145256Sjkoshy 2397145256Sjkoshy} 2398145256Sjkoshy 2399145256Sjkoshy/* 2400145774Sjkoshy * Find a PMC descriptor with user handle 'pmcid' for thread 'td'. 2401145256Sjkoshy */ 2402145256Sjkoshy 2403145256Sjkoshystatic struct pmc * 2404145256Sjkoshypmc_find_pmc_descriptor_in_process(struct pmc_owner *po, pmc_id_t pmcid) 2405145256Sjkoshy{ 2406147191Sjkoshy struct pmc *pm; 2407145256Sjkoshy 2408145774Sjkoshy KASSERT(PMC_ID_TO_ROWINDEX(pmcid) < md->pmd_npmc, 2409145774Sjkoshy ("[pmc,%d] Illegal pmc index %d (max %d)", __LINE__, 2410145774Sjkoshy PMC_ID_TO_ROWINDEX(pmcid), md->pmd_npmc)); 2411145256Sjkoshy 2412147191Sjkoshy LIST_FOREACH(pm, &po->po_pmcs, pm_next) 2413147191Sjkoshy if (pm->pm_id == pmcid) 2414147191Sjkoshy return pm; 2415145256Sjkoshy 2416145256Sjkoshy return NULL; 2417145256Sjkoshy} 2418145256Sjkoshy 2419145256Sjkoshystatic int 2420145256Sjkoshypmc_find_pmc(pmc_id_t pmcid, struct pmc **pmc) 2421145256Sjkoshy{ 2422145256Sjkoshy 2423145256Sjkoshy struct pmc *pm; 2424145256Sjkoshy struct pmc_owner *po; 2425145256Sjkoshy 2426145256Sjkoshy PMCDBG(PMC,FND,1, "find-pmc id=%d", pmcid); 2427145256Sjkoshy 2428145256Sjkoshy if ((po = pmc_find_owner_descriptor(curthread->td_proc)) == NULL) 2429145256Sjkoshy return ESRCH; 2430145256Sjkoshy 2431145256Sjkoshy if ((pm = pmc_find_pmc_descriptor_in_process(po, pmcid)) == NULL) 2432145256Sjkoshy return EINVAL; 2433145256Sjkoshy 2434145256Sjkoshy PMCDBG(PMC,FND,2, "find-pmc id=%d -> pmc=%p", pmcid, pm); 2435145256Sjkoshy 2436145256Sjkoshy *pmc = pm; 2437145256Sjkoshy return 0; 2438145256Sjkoshy} 2439145256Sjkoshy 2440145256Sjkoshy/* 2441145256Sjkoshy * Start a PMC. 2442145256Sjkoshy */ 2443145256Sjkoshy 2444145256Sjkoshystatic int 2445145256Sjkoshypmc_start(struct pmc *pm) 2446145256Sjkoshy{ 2447145774Sjkoshy enum pmc_mode mode; 2448147191Sjkoshy struct pmc_owner *po; 2449145256Sjkoshy struct pmc_binding pb; 2450184802Sjkoshy struct pmc_classdep *pcd; 2451184802Sjkoshy int adjri, error, cpu, ri; 2452145256Sjkoshy 2453145256Sjkoshy KASSERT(pm != NULL, 2454145256Sjkoshy ("[pmc,%d] null pm", __LINE__)); 2455145256Sjkoshy 2456145774Sjkoshy mode = PMC_TO_MODE(pm); 2457145774Sjkoshy ri = PMC_TO_ROWINDEX(pm); 2458184802Sjkoshy pcd = pmc_ri_to_classdep(md, ri, &adjri); 2459184802Sjkoshy 2460145774Sjkoshy error = 0; 2461145256Sjkoshy 2462145774Sjkoshy PMCDBG(PMC,OPS,1, "start pmc=%p mode=%d ri=%d", pm, mode, ri); 2463145774Sjkoshy 2464147191Sjkoshy po = pm->pm_owner; 2465145256Sjkoshy 2466174395Sjkoshy /* 2467174395Sjkoshy * Disallow PMCSTART if a logfile is required but has not been 2468174395Sjkoshy * configured yet. 2469174395Sjkoshy */ 2470174395Sjkoshy if ((pm->pm_flags & PMC_F_NEEDS_LOGFILE) && 2471174395Sjkoshy (po->po_flags & PMC_PO_OWNS_LOGFILE) == 0) 2472184802Sjkoshy return (EDOOFUS); /* programming error */ 2473174395Sjkoshy 2474174395Sjkoshy /* 2475174395Sjkoshy * If this is a sampling mode PMC, log mapping information for 2476174395Sjkoshy * the kernel modules that are currently loaded. 2477174395Sjkoshy */ 2478174395Sjkoshy if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm))) 2479174395Sjkoshy pmc_log_kernel_mappings(pm); 2480174395Sjkoshy 2481145774Sjkoshy if (PMC_IS_VIRTUAL_MODE(mode)) { 2482145256Sjkoshy 2483145256Sjkoshy /* 2484147191Sjkoshy * If a PMCATTACH has never been done on this PMC, 2485147191Sjkoshy * attach it to its owner process. 2486145256Sjkoshy */ 2487145256Sjkoshy 2488145256Sjkoshy if (LIST_EMPTY(&pm->pm_targets)) 2489147191Sjkoshy error = (pm->pm_flags & PMC_F_ATTACH_DONE) ? ESRCH : 2490147191Sjkoshy pmc_attach_process(po->po_owner, pm); 2491145256Sjkoshy 2492145774Sjkoshy /* 2493147191Sjkoshy * If the PMC is attached to its owner, then force a context 2494147191Sjkoshy * switch to ensure that the MD state gets set correctly. 2495145256Sjkoshy */ 2496145256Sjkoshy 2497147191Sjkoshy if (error == 0) { 2498147191Sjkoshy pm->pm_state = PMC_STATE_RUNNING; 2499147191Sjkoshy if (pm->pm_flags & PMC_F_ATTACHED_TO_OWNER) 2500147191Sjkoshy pmc_force_context_switch(); 2501147191Sjkoshy } 2502147191Sjkoshy 2503184802Sjkoshy return (error); 2504147191Sjkoshy } 2505145256Sjkoshy 2506147191Sjkoshy 2507147191Sjkoshy /* 2508147191Sjkoshy * A system-wide PMC. 2509174395Sjkoshy * 2510147191Sjkoshy * Add the owner to the global list if this is a system-wide 2511147191Sjkoshy * sampling PMC. 2512147191Sjkoshy */ 2513147191Sjkoshy 2514147191Sjkoshy if (mode == PMC_MODE_SS) { 2515147191Sjkoshy if (po->po_sscount == 0) { 2516147191Sjkoshy LIST_INSERT_HEAD(&pmc_ss_owners, po, po_ssnext); 2517147191Sjkoshy atomic_add_rel_int(&pmc_ss_count, 1); 2518147191Sjkoshy PMCDBG(PMC,OPS,1, "po=%p in global list", po); 2519147191Sjkoshy } 2520147191Sjkoshy po->po_sscount++; 2521145256Sjkoshy } 2522145256Sjkoshy 2523174395Sjkoshy /* Log mapping information for all processes in the system. */ 2524174395Sjkoshy pmc_log_all_process_mappings(po); 2525157144Sjkoshy 2526145256Sjkoshy /* 2527147191Sjkoshy * Move to the CPU associated with this 2528145256Sjkoshy * PMC, and start the hardware. 2529145256Sjkoshy */ 2530145256Sjkoshy 2531145256Sjkoshy pmc_save_cpu_binding(&pb); 2532145256Sjkoshy 2533145774Sjkoshy cpu = PMC_TO_CPU(pm); 2534145256Sjkoshy 2535183266Sjkoshy if (!pmc_cpu_is_active(cpu)) 2536184802Sjkoshy return (ENXIO); 2537145256Sjkoshy 2538145256Sjkoshy pmc_select_cpu(cpu); 2539145256Sjkoshy 2540145256Sjkoshy /* 2541145256Sjkoshy * global PMCs are configured at allocation time 2542145256Sjkoshy * so write out the initial value and start the PMC. 2543145256Sjkoshy */ 2544145256Sjkoshy 2545147191Sjkoshy pm->pm_state = PMC_STATE_RUNNING; 2546147191Sjkoshy 2547145774Sjkoshy critical_enter(); 2548184802Sjkoshy if ((error = pcd->pcd_write_pmc(cpu, adjri, 2549145774Sjkoshy PMC_IS_SAMPLING_MODE(mode) ? 2550145256Sjkoshy pm->pm_sc.pm_reloadcount : 2551145256Sjkoshy pm->pm_sc.pm_initial)) == 0) 2552184802Sjkoshy error = pcd->pcd_start_pmc(cpu, adjri); 2553145774Sjkoshy critical_exit(); 2554145256Sjkoshy 2555145256Sjkoshy pmc_restore_cpu_binding(&pb); 2556145256Sjkoshy 2557184802Sjkoshy return (error); 2558145256Sjkoshy} 2559145256Sjkoshy 2560145256Sjkoshy/* 2561145256Sjkoshy * Stop a PMC. 2562145256Sjkoshy */ 2563145256Sjkoshy 2564145256Sjkoshystatic int 2565145256Sjkoshypmc_stop(struct pmc *pm) 2566145256Sjkoshy{ 2567147191Sjkoshy struct pmc_owner *po; 2568145256Sjkoshy struct pmc_binding pb; 2569184802Sjkoshy struct pmc_classdep *pcd; 2570184802Sjkoshy int adjri, cpu, error, ri; 2571145256Sjkoshy 2572145256Sjkoshy KASSERT(pm != NULL, ("[pmc,%d] null pmc", __LINE__)); 2573145256Sjkoshy 2574145774Sjkoshy PMCDBG(PMC,OPS,1, "stop pmc=%p mode=%d ri=%d", pm, 2575145774Sjkoshy PMC_TO_MODE(pm), PMC_TO_ROWINDEX(pm)); 2576145256Sjkoshy 2577145256Sjkoshy pm->pm_state = PMC_STATE_STOPPED; 2578145256Sjkoshy 2579145256Sjkoshy /* 2580145256Sjkoshy * If the PMC is a virtual mode one, changing the state to 2581145256Sjkoshy * non-RUNNING is enough to ensure that the PMC never gets 2582145256Sjkoshy * scheduled. 2583145256Sjkoshy * 2584145256Sjkoshy * If this PMC is current running on a CPU, then it will 2585145256Sjkoshy * handled correctly at the time its target process is context 2586145256Sjkoshy * switched out. 2587145256Sjkoshy */ 2588145256Sjkoshy 2589145774Sjkoshy if (PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm))) 2590145256Sjkoshy return 0; 2591145256Sjkoshy 2592145256Sjkoshy /* 2593145256Sjkoshy * A system-mode PMC. Move to the CPU associated with 2594145256Sjkoshy * this PMC, and stop the hardware. We update the 2595145256Sjkoshy * 'initial count' so that a subsequent PMCSTART will 2596145256Sjkoshy * resume counting from the current hardware count. 2597145256Sjkoshy */ 2598145256Sjkoshy 2599145256Sjkoshy pmc_save_cpu_binding(&pb); 2600145256Sjkoshy 2601145774Sjkoshy cpu = PMC_TO_CPU(pm); 2602145256Sjkoshy 2603183266Sjkoshy KASSERT(cpu >= 0 && cpu < pmc_cpu_max(), 2604145774Sjkoshy ("[pmc,%d] illegal cpu=%d", __LINE__, cpu)); 2605145774Sjkoshy 2606183266Sjkoshy if (!pmc_cpu_is_active(cpu)) 2607145256Sjkoshy return ENXIO; 2608145256Sjkoshy 2609145256Sjkoshy pmc_select_cpu(cpu); 2610145256Sjkoshy 2611145774Sjkoshy ri = PMC_TO_ROWINDEX(pm); 2612184802Sjkoshy pcd = pmc_ri_to_classdep(md, ri, &adjri); 2613145256Sjkoshy 2614145774Sjkoshy critical_enter(); 2615184802Sjkoshy if ((error = pcd->pcd_stop_pmc(cpu, adjri)) == 0) 2616184802Sjkoshy error = pcd->pcd_read_pmc(cpu, adjri, &pm->pm_sc.pm_initial); 2617145774Sjkoshy critical_exit(); 2618145774Sjkoshy 2619145256Sjkoshy pmc_restore_cpu_binding(&pb); 2620145256Sjkoshy 2621147191Sjkoshy po = pm->pm_owner; 2622147191Sjkoshy 2623147191Sjkoshy /* remove this owner from the global list of SS PMC owners */ 2624147191Sjkoshy if (PMC_TO_MODE(pm) == PMC_MODE_SS) { 2625147191Sjkoshy po->po_sscount--; 2626147191Sjkoshy if (po->po_sscount == 0) { 2627147191Sjkoshy atomic_subtract_rel_int(&pmc_ss_count, 1); 2628147191Sjkoshy LIST_REMOVE(po, po_ssnext); 2629147191Sjkoshy PMCDBG(PMC,OPS,2,"po=%p removed from global list", po); 2630147191Sjkoshy } 2631147191Sjkoshy } 2632147191Sjkoshy 2633184802Sjkoshy return (error); 2634145256Sjkoshy} 2635145256Sjkoshy 2636145256Sjkoshy 2637153110Sru#ifdef DEBUG 2638145256Sjkoshystatic const char *pmc_op_to_name[] = { 2639145256Sjkoshy#undef __PMC_OP 2640145256Sjkoshy#define __PMC_OP(N, D) #N , 2641145256Sjkoshy __PMC_OPS() 2642145256Sjkoshy NULL 2643145256Sjkoshy}; 2644145256Sjkoshy#endif 2645145256Sjkoshy 2646145256Sjkoshy/* 2647145256Sjkoshy * The syscall interface 2648145256Sjkoshy */ 2649145256Sjkoshy 2650145256Sjkoshy#define PMC_GET_SX_XLOCK(...) do { \ 2651145256Sjkoshy sx_xlock(&pmc_sx); \ 2652145256Sjkoshy if (pmc_hook == NULL) { \ 2653145256Sjkoshy sx_xunlock(&pmc_sx); \ 2654145256Sjkoshy return __VA_ARGS__; \ 2655145256Sjkoshy } \ 2656145256Sjkoshy} while (0) 2657145256Sjkoshy 2658145256Sjkoshy#define PMC_DOWNGRADE_SX() do { \ 2659145256Sjkoshy sx_downgrade(&pmc_sx); \ 2660145256Sjkoshy is_sx_downgraded = 1; \ 2661145256Sjkoshy} while (0) 2662145256Sjkoshy 2663145256Sjkoshystatic int 2664145256Sjkoshypmc_syscall_handler(struct thread *td, void *syscall_args) 2665145256Sjkoshy{ 2666195005Sattilio int error, is_sx_downgraded, is_sx_locked, op; 2667145256Sjkoshy struct pmc_syscall_args *c; 2668145256Sjkoshy void *arg; 2669145256Sjkoshy 2670145256Sjkoshy PMC_GET_SX_XLOCK(ENOSYS); 2671145256Sjkoshy 2672147191Sjkoshy DROP_GIANT(); 2673147191Sjkoshy 2674145256Sjkoshy is_sx_downgraded = 0; 2675195005Sattilio is_sx_locked = 1; 2676145256Sjkoshy 2677145256Sjkoshy c = (struct pmc_syscall_args *) syscall_args; 2678145256Sjkoshy 2679145256Sjkoshy op = c->pmop_code; 2680145256Sjkoshy arg = c->pmop_data; 2681145256Sjkoshy 2682145256Sjkoshy PMCDBG(MOD,PMS,1, "syscall op=%d \"%s\" arg=%p", op, 2683145256Sjkoshy pmc_op_to_name[op], arg); 2684145256Sjkoshy 2685145256Sjkoshy error = 0; 2686145256Sjkoshy atomic_add_int(&pmc_stats.pm_syscalls, 1); 2687145256Sjkoshy 2688145256Sjkoshy switch(op) 2689145256Sjkoshy { 2690145256Sjkoshy 2691145256Sjkoshy 2692145256Sjkoshy /* 2693145256Sjkoshy * Configure a log file. 2694145256Sjkoshy * 2695145256Sjkoshy * XXX This OP will be reworked. 2696145256Sjkoshy */ 2697145256Sjkoshy 2698145256Sjkoshy case PMC_OP_CONFIGURELOG: 2699145256Sjkoshy { 2700157144Sjkoshy struct proc *p; 2701156466Sjkoshy struct pmc *pm; 2702145256Sjkoshy struct pmc_owner *po; 2703145256Sjkoshy struct pmc_op_configurelog cl; 2704145256Sjkoshy 2705145256Sjkoshy sx_assert(&pmc_sx, SX_XLOCKED); 2706145256Sjkoshy 2707145256Sjkoshy if ((error = copyin(arg, &cl, sizeof(cl))) != 0) 2708145256Sjkoshy break; 2709145256Sjkoshy 2710145256Sjkoshy /* mark this process as owning a log file */ 2711145256Sjkoshy p = td->td_proc; 2712145256Sjkoshy if ((po = pmc_find_owner_descriptor(p)) == NULL) 2713147191Sjkoshy if ((po = pmc_allocate_owner_descriptor(p)) == NULL) { 2714147191Sjkoshy error = ENOMEM; 2715147191Sjkoshy break; 2716147191Sjkoshy } 2717145256Sjkoshy 2718147191Sjkoshy /* 2719147191Sjkoshy * If a valid fd was passed in, try to configure that, 2720147191Sjkoshy * otherwise if 'fd' was less than zero and there was 2721147191Sjkoshy * a log file configured, flush its buffers and 2722147191Sjkoshy * de-configure it. 2723147191Sjkoshy */ 2724195005Sattilio if (cl.pm_logfd >= 0) { 2725195005Sattilio sx_xunlock(&pmc_sx); 2726195005Sattilio is_sx_locked = 0; 2727185363Sjkoshy error = pmclog_configure_log(md, po, cl.pm_logfd); 2728195005Sattilio } else if (po->po_flags & PMC_PO_OWNS_LOGFILE) { 2729147191Sjkoshy pmclog_process_closelog(po); 2730147191Sjkoshy error = pmclog_flush(po); 2731156466Sjkoshy if (error == 0) { 2732156466Sjkoshy LIST_FOREACH(pm, &po->po_pmcs, pm_next) 2733156834Sjkoshy if (pm->pm_flags & PMC_F_NEEDS_LOGFILE && 2734156834Sjkoshy pm->pm_state == PMC_STATE_RUNNING) 2735156466Sjkoshy pmc_stop(pm); 2736147191Sjkoshy error = pmclog_deconfigure_log(po); 2737156466Sjkoshy } 2738147191Sjkoshy } else 2739147191Sjkoshy error = EINVAL; 2740157144Sjkoshy 2741157144Sjkoshy if (error) 2742157144Sjkoshy break; 2743147191Sjkoshy } 2744147191Sjkoshy break; 2745147191Sjkoshy 2746147191Sjkoshy 2747147191Sjkoshy /* 2748147191Sjkoshy * Flush a log file. 2749147191Sjkoshy */ 2750147191Sjkoshy 2751147191Sjkoshy case PMC_OP_FLUSHLOG: 2752147191Sjkoshy { 2753147191Sjkoshy struct pmc_owner *po; 2754147191Sjkoshy 2755147191Sjkoshy sx_assert(&pmc_sx, SX_XLOCKED); 2756147191Sjkoshy 2757147191Sjkoshy if ((po = pmc_find_owner_descriptor(td->td_proc)) == NULL) { 2758147191Sjkoshy error = EINVAL; 2759145256Sjkoshy break; 2760147191Sjkoshy } 2761145256Sjkoshy 2762147191Sjkoshy error = pmclog_flush(po); 2763145256Sjkoshy } 2764145256Sjkoshy break; 2765145256Sjkoshy 2766145256Sjkoshy /* 2767145256Sjkoshy * Retrieve hardware configuration. 2768145256Sjkoshy */ 2769145256Sjkoshy 2770145256Sjkoshy case PMC_OP_GETCPUINFO: /* CPU information */ 2771145256Sjkoshy { 2772145256Sjkoshy struct pmc_op_getcpuinfo gci; 2773184802Sjkoshy struct pmc_classinfo *pci; 2774184802Sjkoshy struct pmc_classdep *pcd; 2775184802Sjkoshy int cl; 2776145256Sjkoshy 2777145256Sjkoshy gci.pm_cputype = md->pmd_cputype; 2778183266Sjkoshy gci.pm_ncpu = pmc_cpu_max(); 2779145256Sjkoshy gci.pm_npmc = md->pmd_npmc; 2780145256Sjkoshy gci.pm_nclass = md->pmd_nclass; 2781184802Sjkoshy pci = gci.pm_classes; 2782184802Sjkoshy pcd = md->pmd_classdep; 2783184802Sjkoshy for (cl = 0; cl < md->pmd_nclass; cl++, pci++, pcd++) { 2784184802Sjkoshy pci->pm_caps = pcd->pcd_caps; 2785184802Sjkoshy pci->pm_class = pcd->pcd_class; 2786184802Sjkoshy pci->pm_width = pcd->pcd_width; 2787184802Sjkoshy pci->pm_num = pcd->pcd_num; 2788184802Sjkoshy } 2789145256Sjkoshy error = copyout(&gci, arg, sizeof(gci)); 2790145256Sjkoshy } 2791145256Sjkoshy break; 2792145256Sjkoshy 2793145256Sjkoshy 2794145256Sjkoshy /* 2795145256Sjkoshy * Get module statistics 2796145256Sjkoshy */ 2797145256Sjkoshy 2798145256Sjkoshy case PMC_OP_GETDRIVERSTATS: 2799145256Sjkoshy { 2800145256Sjkoshy struct pmc_op_getdriverstats gms; 2801145256Sjkoshy 2802145256Sjkoshy bcopy(&pmc_stats, &gms, sizeof(gms)); 2803145256Sjkoshy error = copyout(&gms, arg, sizeof(gms)); 2804145256Sjkoshy } 2805145256Sjkoshy break; 2806145256Sjkoshy 2807145256Sjkoshy 2808145256Sjkoshy /* 2809145256Sjkoshy * Retrieve module version number 2810145256Sjkoshy */ 2811145256Sjkoshy 2812145256Sjkoshy case PMC_OP_GETMODULEVERSION: 2813145256Sjkoshy { 2814147191Sjkoshy uint32_t cv, modv; 2815147191Sjkoshy 2816147191Sjkoshy /* retrieve the client's idea of the ABI version */ 2817147191Sjkoshy if ((error = copyin(arg, &cv, sizeof(uint32_t))) != 0) 2818147191Sjkoshy break; 2819147191Sjkoshy /* don't service clients newer than our driver */ 2820147191Sjkoshy modv = PMC_VERSION; 2821147191Sjkoshy if ((cv & 0xFFFF0000) > (modv & 0xFFFF0000)) { 2822147191Sjkoshy error = EPROGMISMATCH; 2823147191Sjkoshy break; 2824147191Sjkoshy } 2825147191Sjkoshy error = copyout(&modv, arg, sizeof(int)); 2826145256Sjkoshy } 2827145256Sjkoshy break; 2828145256Sjkoshy 2829145256Sjkoshy 2830145256Sjkoshy /* 2831145256Sjkoshy * Retrieve the state of all the PMCs on a given 2832145256Sjkoshy * CPU. 2833145256Sjkoshy */ 2834145256Sjkoshy 2835145256Sjkoshy case PMC_OP_GETPMCINFO: 2836145256Sjkoshy { 2837184802Sjkoshy int ari; 2838184802Sjkoshy struct pmc *pm; 2839184802Sjkoshy size_t pmcinfo_size; 2840145256Sjkoshy uint32_t cpu, n, npmc; 2841184802Sjkoshy struct pmc_owner *po; 2842184802Sjkoshy struct pmc_binding pb; 2843184802Sjkoshy struct pmc_classdep *pcd; 2844145256Sjkoshy struct pmc_info *p, *pmcinfo; 2845145256Sjkoshy struct pmc_op_getpmcinfo *gpi; 2846145256Sjkoshy 2847145256Sjkoshy PMC_DOWNGRADE_SX(); 2848145256Sjkoshy 2849145256Sjkoshy gpi = (struct pmc_op_getpmcinfo *) arg; 2850145256Sjkoshy 2851145256Sjkoshy if ((error = copyin(&gpi->pm_cpu, &cpu, sizeof(cpu))) != 0) 2852145256Sjkoshy break; 2853145256Sjkoshy 2854183266Sjkoshy if (cpu >= pmc_cpu_max()) { 2855145256Sjkoshy error = EINVAL; 2856145256Sjkoshy break; 2857145256Sjkoshy } 2858145256Sjkoshy 2859183266Sjkoshy if (!pmc_cpu_is_active(cpu)) { 2860145256Sjkoshy error = ENXIO; 2861145256Sjkoshy break; 2862145256Sjkoshy } 2863145256Sjkoshy 2864145256Sjkoshy /* switch to CPU 'cpu' */ 2865145256Sjkoshy pmc_save_cpu_binding(&pb); 2866145256Sjkoshy pmc_select_cpu(cpu); 2867145256Sjkoshy 2868145256Sjkoshy npmc = md->pmd_npmc; 2869145256Sjkoshy 2870145256Sjkoshy pmcinfo_size = npmc * sizeof(struct pmc_info); 2871184214Sdes pmcinfo = malloc(pmcinfo_size, M_PMC, M_WAITOK); 2872145256Sjkoshy 2873145256Sjkoshy p = pmcinfo; 2874145256Sjkoshy 2875145256Sjkoshy for (n = 0; n < md->pmd_npmc; n++, p++) { 2876145256Sjkoshy 2877184802Sjkoshy pcd = pmc_ri_to_classdep(md, n, &ari); 2878184802Sjkoshy 2879184802Sjkoshy KASSERT(pcd != NULL, 2880184802Sjkoshy ("[pmc,%d] null pcd ri=%d", __LINE__, n)); 2881184802Sjkoshy 2882184802Sjkoshy if ((error = pcd->pcd_describe(cpu, ari, p, &pm)) != 0) 2883145256Sjkoshy break; 2884145256Sjkoshy 2885145256Sjkoshy if (PMC_ROW_DISP_IS_STANDALONE(n)) 2886145256Sjkoshy p->pm_rowdisp = PMC_DISP_STANDALONE; 2887145256Sjkoshy else if (PMC_ROW_DISP_IS_THREAD(n)) 2888145256Sjkoshy p->pm_rowdisp = PMC_DISP_THREAD; 2889145256Sjkoshy else 2890145256Sjkoshy p->pm_rowdisp = PMC_DISP_FREE; 2891145256Sjkoshy 2892145256Sjkoshy p->pm_ownerpid = -1; 2893145256Sjkoshy 2894145256Sjkoshy if (pm == NULL) /* no PMC associated */ 2895145256Sjkoshy continue; 2896145256Sjkoshy 2897145256Sjkoshy po = pm->pm_owner; 2898145256Sjkoshy 2899145256Sjkoshy KASSERT(po->po_owner != NULL, 2900145256Sjkoshy ("[pmc,%d] pmc_owner had a null proc pointer", 2901145256Sjkoshy __LINE__)); 2902145256Sjkoshy 2903145256Sjkoshy p->pm_ownerpid = po->po_owner->p_pid; 2904145774Sjkoshy p->pm_mode = PMC_TO_MODE(pm); 2905145256Sjkoshy p->pm_event = pm->pm_event; 2906145256Sjkoshy p->pm_flags = pm->pm_flags; 2907145256Sjkoshy 2908145774Sjkoshy if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm))) 2909145256Sjkoshy p->pm_reloadcount = 2910145256Sjkoshy pm->pm_sc.pm_reloadcount; 2911145256Sjkoshy } 2912145256Sjkoshy 2913145256Sjkoshy pmc_restore_cpu_binding(&pb); 2914145256Sjkoshy 2915145256Sjkoshy /* now copy out the PMC info collected */ 2916145256Sjkoshy if (error == 0) 2917145256Sjkoshy error = copyout(pmcinfo, &gpi->pm_pmcs, pmcinfo_size); 2918145256Sjkoshy 2919184205Sdes free(pmcinfo, M_PMC); 2920145256Sjkoshy } 2921145256Sjkoshy break; 2922145256Sjkoshy 2923145256Sjkoshy 2924145256Sjkoshy /* 2925145256Sjkoshy * Set the administrative state of a PMC. I.e. whether 2926145256Sjkoshy * the PMC is to be used or not. 2927145256Sjkoshy */ 2928145256Sjkoshy 2929145256Sjkoshy case PMC_OP_PMCADMIN: 2930145256Sjkoshy { 2931145256Sjkoshy int cpu, ri; 2932145256Sjkoshy enum pmc_state request; 2933145256Sjkoshy struct pmc_cpu *pc; 2934145256Sjkoshy struct pmc_hw *phw; 2935145256Sjkoshy struct pmc_op_pmcadmin pma; 2936145256Sjkoshy struct pmc_binding pb; 2937145256Sjkoshy 2938145256Sjkoshy sx_assert(&pmc_sx, SX_XLOCKED); 2939145256Sjkoshy 2940145256Sjkoshy KASSERT(td == curthread, 2941145256Sjkoshy ("[pmc,%d] td != curthread", __LINE__)); 2942145256Sjkoshy 2943164033Srwatson error = priv_check(td, PRIV_PMC_MANAGE); 2944164033Srwatson if (error) 2945145256Sjkoshy break; 2946145256Sjkoshy 2947145256Sjkoshy if ((error = copyin(arg, &pma, sizeof(pma))) != 0) 2948145256Sjkoshy break; 2949145256Sjkoshy 2950145256Sjkoshy cpu = pma.pm_cpu; 2951145256Sjkoshy 2952183266Sjkoshy if (cpu < 0 || cpu >= (int) pmc_cpu_max()) { 2953145256Sjkoshy error = EINVAL; 2954145256Sjkoshy break; 2955145256Sjkoshy } 2956145256Sjkoshy 2957183266Sjkoshy if (!pmc_cpu_is_active(cpu)) { 2958145256Sjkoshy error = ENXIO; 2959145256Sjkoshy break; 2960145256Sjkoshy } 2961145256Sjkoshy 2962145256Sjkoshy request = pma.pm_state; 2963145256Sjkoshy 2964145256Sjkoshy if (request != PMC_STATE_DISABLED && 2965145256Sjkoshy request != PMC_STATE_FREE) { 2966145256Sjkoshy error = EINVAL; 2967145256Sjkoshy break; 2968145256Sjkoshy } 2969145256Sjkoshy 2970145256Sjkoshy ri = pma.pm_pmc; /* pmc id == row index */ 2971145256Sjkoshy if (ri < 0 || ri >= (int) md->pmd_npmc) { 2972145256Sjkoshy error = EINVAL; 2973145256Sjkoshy break; 2974145256Sjkoshy } 2975145256Sjkoshy 2976145256Sjkoshy /* 2977145256Sjkoshy * We can't disable a PMC with a row-index allocated 2978145256Sjkoshy * for process virtual PMCs. 2979145256Sjkoshy */ 2980145256Sjkoshy 2981145256Sjkoshy if (PMC_ROW_DISP_IS_THREAD(ri) && 2982145256Sjkoshy request == PMC_STATE_DISABLED) { 2983145256Sjkoshy error = EBUSY; 2984145256Sjkoshy break; 2985145256Sjkoshy } 2986145256Sjkoshy 2987145256Sjkoshy /* 2988145256Sjkoshy * otherwise, this PMC on this CPU is either free or 2989145256Sjkoshy * in system-wide mode. 2990145256Sjkoshy */ 2991145256Sjkoshy 2992145256Sjkoshy pmc_save_cpu_binding(&pb); 2993145256Sjkoshy pmc_select_cpu(cpu); 2994145256Sjkoshy 2995145256Sjkoshy pc = pmc_pcpu[cpu]; 2996145256Sjkoshy phw = pc->pc_hwpmcs[ri]; 2997145256Sjkoshy 2998145256Sjkoshy /* 2999145256Sjkoshy * XXX do we need some kind of 'forced' disable? 3000145256Sjkoshy */ 3001145256Sjkoshy 3002145256Sjkoshy if (phw->phw_pmc == NULL) { 3003145256Sjkoshy if (request == PMC_STATE_DISABLED && 3004145256Sjkoshy (phw->phw_state & PMC_PHW_FLAG_IS_ENABLED)) { 3005145256Sjkoshy phw->phw_state &= ~PMC_PHW_FLAG_IS_ENABLED; 3006145256Sjkoshy PMC_MARK_ROW_STANDALONE(ri); 3007145256Sjkoshy } else if (request == PMC_STATE_FREE && 3008145256Sjkoshy (phw->phw_state & PMC_PHW_FLAG_IS_ENABLED) == 0) { 3009145256Sjkoshy phw->phw_state |= PMC_PHW_FLAG_IS_ENABLED; 3010145256Sjkoshy PMC_UNMARK_ROW_STANDALONE(ri); 3011145256Sjkoshy } 3012145256Sjkoshy /* other cases are a no-op */ 3013145256Sjkoshy } else 3014145256Sjkoshy error = EBUSY; 3015145256Sjkoshy 3016145256Sjkoshy pmc_restore_cpu_binding(&pb); 3017145256Sjkoshy } 3018145256Sjkoshy break; 3019145256Sjkoshy 3020145256Sjkoshy 3021145256Sjkoshy /* 3022145256Sjkoshy * Allocate a PMC. 3023145256Sjkoshy */ 3024145256Sjkoshy 3025145256Sjkoshy case PMC_OP_PMCALLOCATE: 3026145256Sjkoshy { 3027184802Sjkoshy int adjri, n; 3028184802Sjkoshy u_int cpu; 3029145256Sjkoshy uint32_t caps; 3030184802Sjkoshy struct pmc *pmc; 3031145256Sjkoshy enum pmc_mode mode; 3032145774Sjkoshy struct pmc_hw *phw; 3033184802Sjkoshy struct pmc_binding pb; 3034184802Sjkoshy struct pmc_classdep *pcd; 3035145256Sjkoshy struct pmc_op_pmcallocate pa; 3036145256Sjkoshy 3037145256Sjkoshy if ((error = copyin(arg, &pa, sizeof(pa))) != 0) 3038145256Sjkoshy break; 3039145256Sjkoshy 3040145256Sjkoshy caps = pa.pm_caps; 3041145256Sjkoshy mode = pa.pm_mode; 3042145256Sjkoshy cpu = pa.pm_cpu; 3043145256Sjkoshy 3044145256Sjkoshy if ((mode != PMC_MODE_SS && mode != PMC_MODE_SC && 3045145256Sjkoshy mode != PMC_MODE_TS && mode != PMC_MODE_TC) || 3046183266Sjkoshy (cpu != (u_int) PMC_CPU_ANY && cpu >= pmc_cpu_max())) { 3047145256Sjkoshy error = EINVAL; 3048145256Sjkoshy break; 3049145256Sjkoshy } 3050145256Sjkoshy 3051145256Sjkoshy /* 3052145256Sjkoshy * Virtual PMCs should only ask for a default CPU. 3053145256Sjkoshy * System mode PMCs need to specify a non-default CPU. 3054145256Sjkoshy */ 3055145256Sjkoshy 3056145256Sjkoshy if ((PMC_IS_VIRTUAL_MODE(mode) && cpu != (u_int) PMC_CPU_ANY) || 3057145256Sjkoshy (PMC_IS_SYSTEM_MODE(mode) && cpu == (u_int) PMC_CPU_ANY)) { 3058145256Sjkoshy error = EINVAL; 3059145256Sjkoshy break; 3060145256Sjkoshy } 3061145256Sjkoshy 3062145256Sjkoshy /* 3063183266Sjkoshy * Check that an inactive CPU is not being asked for. 3064145256Sjkoshy */ 3065145256Sjkoshy 3066183266Sjkoshy if (PMC_IS_SYSTEM_MODE(mode) && !pmc_cpu_is_active(cpu)) { 3067145256Sjkoshy error = ENXIO; 3068145256Sjkoshy break; 3069145256Sjkoshy } 3070145256Sjkoshy 3071145256Sjkoshy /* 3072145256Sjkoshy * Refuse an allocation for a system-wide PMC if this 3073145256Sjkoshy * process has been jailed, or if this process lacks 3074145256Sjkoshy * super-user credentials and the sysctl tunable 3075145256Sjkoshy * 'security.bsd.unprivileged_syspmcs' is zero. 3076145256Sjkoshy */ 3077145256Sjkoshy 3078145256Sjkoshy if (PMC_IS_SYSTEM_MODE(mode)) { 3079164033Srwatson if (jailed(curthread->td_ucred)) { 3080145256Sjkoshy error = EPERM; 3081164033Srwatson break; 3082164033Srwatson } 3083164033Srwatson if (!pmc_unprivileged_syspmcs) { 3084164033Srwatson error = priv_check(curthread, 3085164033Srwatson PRIV_PMC_SYSTEM); 3086164033Srwatson if (error) 3087164033Srwatson break; 3088164033Srwatson } 3089145256Sjkoshy } 3090145256Sjkoshy 3091145256Sjkoshy if (error) 3092145256Sjkoshy break; 3093145256Sjkoshy 3094145256Sjkoshy /* 3095145256Sjkoshy * Look for valid values for 'pm_flags' 3096145256Sjkoshy */ 3097145256Sjkoshy 3098147191Sjkoshy if ((pa.pm_flags & ~(PMC_F_DESCENDANTS | PMC_F_LOG_PROCCSW | 3099174395Sjkoshy PMC_F_LOG_PROCEXIT | PMC_F_CALLCHAIN)) != 0) { 3100145256Sjkoshy error = EINVAL; 3101145256Sjkoshy break; 3102145256Sjkoshy } 3103145256Sjkoshy 3104147191Sjkoshy /* process logging options are not allowed for system PMCs */ 3105147191Sjkoshy if (PMC_IS_SYSTEM_MODE(mode) && (pa.pm_flags & 3106147191Sjkoshy (PMC_F_LOG_PROCCSW | PMC_F_LOG_PROCEXIT))) { 3107147191Sjkoshy error = EINVAL; 3108147191Sjkoshy break; 3109147191Sjkoshy } 3110147191Sjkoshy 3111145256Sjkoshy /* 3112145256Sjkoshy * All sampling mode PMCs need to be able to interrupt the 3113145256Sjkoshy * CPU. 3114145256Sjkoshy */ 3115147191Sjkoshy if (PMC_IS_SAMPLING_MODE(mode)) 3116145256Sjkoshy caps |= PMC_CAP_INTERRUPT; 3117145256Sjkoshy 3118149374Sjkoshy /* A valid class specifier should have been passed in. */ 3119149374Sjkoshy for (n = 0; n < md->pmd_nclass; n++) 3120184802Sjkoshy if (md->pmd_classdep[n].pcd_class == pa.pm_class) 3121149374Sjkoshy break; 3122149374Sjkoshy if (n == md->pmd_nclass) { 3123149374Sjkoshy error = EINVAL; 3124149374Sjkoshy break; 3125149374Sjkoshy } 3126149374Sjkoshy 3127149374Sjkoshy /* The requested PMC capabilities should be feasible. */ 3128184802Sjkoshy if ((md->pmd_classdep[n].pcd_caps & caps) != caps) { 3129149374Sjkoshy error = EOPNOTSUPP; 3130149374Sjkoshy break; 3131149374Sjkoshy } 3132149374Sjkoshy 3133145256Sjkoshy PMCDBG(PMC,ALL,2, "event=%d caps=0x%x mode=%d cpu=%d", 3134145256Sjkoshy pa.pm_ev, caps, mode, cpu); 3135145256Sjkoshy 3136145256Sjkoshy pmc = pmc_allocate_pmc_descriptor(); 3137145774Sjkoshy pmc->pm_id = PMC_ID_MAKE_ID(cpu,pa.pm_mode,pa.pm_class, 3138145774Sjkoshy PMC_ID_INVALID); 3139145256Sjkoshy pmc->pm_event = pa.pm_ev; 3140145256Sjkoshy pmc->pm_state = PMC_STATE_FREE; 3141145256Sjkoshy pmc->pm_caps = caps; 3142145256Sjkoshy pmc->pm_flags = pa.pm_flags; 3143145256Sjkoshy 3144145256Sjkoshy /* switch thread to CPU 'cpu' */ 3145145256Sjkoshy pmc_save_cpu_binding(&pb); 3146145256Sjkoshy 3147145256Sjkoshy#define PMC_IS_SHAREABLE_PMC(cpu, n) \ 3148145256Sjkoshy (pmc_pcpu[(cpu)]->pc_hwpmcs[(n)]->phw_state & \ 3149145256Sjkoshy PMC_PHW_FLAG_IS_SHAREABLE) 3150145256Sjkoshy#define PMC_IS_UNALLOCATED(cpu, n) \ 3151145256Sjkoshy (pmc_pcpu[(cpu)]->pc_hwpmcs[(n)]->phw_pmc == NULL) 3152145256Sjkoshy 3153145256Sjkoshy if (PMC_IS_SYSTEM_MODE(mode)) { 3154145256Sjkoshy pmc_select_cpu(cpu); 3155184802Sjkoshy for (n = 0; n < (int) md->pmd_npmc; n++) { 3156184802Sjkoshy pcd = pmc_ri_to_classdep(md, n, &adjri); 3157145256Sjkoshy if (pmc_can_allocate_row(n, mode) == 0 && 3158145256Sjkoshy pmc_can_allocate_rowindex( 3159145774Sjkoshy curthread->td_proc, n, cpu) == 0 && 3160145256Sjkoshy (PMC_IS_UNALLOCATED(cpu, n) || 3161145256Sjkoshy PMC_IS_SHAREABLE_PMC(cpu, n)) && 3162184802Sjkoshy pcd->pcd_allocate_pmc(cpu, adjri, pmc, 3163145256Sjkoshy &pa) == 0) 3164145256Sjkoshy break; 3165184802Sjkoshy } 3166145256Sjkoshy } else { 3167145256Sjkoshy /* Process virtual mode */ 3168145256Sjkoshy for (n = 0; n < (int) md->pmd_npmc; n++) { 3169184802Sjkoshy pcd = pmc_ri_to_classdep(md, n, &adjri); 3170145256Sjkoshy if (pmc_can_allocate_row(n, mode) == 0 && 3171145256Sjkoshy pmc_can_allocate_rowindex( 3172145774Sjkoshy curthread->td_proc, n, 3173145774Sjkoshy PMC_CPU_ANY) == 0 && 3174184802Sjkoshy pcd->pcd_allocate_pmc(curthread->td_oncpu, 3175184802Sjkoshy adjri, pmc, &pa) == 0) 3176145256Sjkoshy break; 3177145256Sjkoshy } 3178145256Sjkoshy } 3179145256Sjkoshy 3180145256Sjkoshy#undef PMC_IS_UNALLOCATED 3181145256Sjkoshy#undef PMC_IS_SHAREABLE_PMC 3182145256Sjkoshy 3183145256Sjkoshy pmc_restore_cpu_binding(&pb); 3184145256Sjkoshy 3185145256Sjkoshy if (n == (int) md->pmd_npmc) { 3186145256Sjkoshy pmc_destroy_pmc_descriptor(pmc); 3187184205Sdes free(pmc, M_PMC); 3188145256Sjkoshy pmc = NULL; 3189145256Sjkoshy error = EINVAL; 3190145256Sjkoshy break; 3191145256Sjkoshy } 3192145256Sjkoshy 3193145774Sjkoshy /* Fill in the correct value in the ID field */ 3194145774Sjkoshy pmc->pm_id = PMC_ID_MAKE_ID(cpu,mode,pa.pm_class,n); 3195145256Sjkoshy 3196145774Sjkoshy PMCDBG(PMC,ALL,2, "ev=%d class=%d mode=%d n=%d -> pmcid=%x", 3197145774Sjkoshy pmc->pm_event, pa.pm_class, mode, n, pmc->pm_id); 3198145774Sjkoshy 3199147191Sjkoshy /* Process mode PMCs with logging enabled need log files */ 3200147191Sjkoshy if (pmc->pm_flags & (PMC_F_LOG_PROCEXIT | PMC_F_LOG_PROCCSW)) 3201147191Sjkoshy pmc->pm_flags |= PMC_F_NEEDS_LOGFILE; 3202147191Sjkoshy 3203147191Sjkoshy /* All system mode sampling PMCs require a log file */ 3204147191Sjkoshy if (PMC_IS_SAMPLING_MODE(mode) && PMC_IS_SYSTEM_MODE(mode)) 3205147191Sjkoshy pmc->pm_flags |= PMC_F_NEEDS_LOGFILE; 3206147191Sjkoshy 3207145256Sjkoshy /* 3208145256Sjkoshy * Configure global pmc's immediately 3209145256Sjkoshy */ 3210145256Sjkoshy 3211145774Sjkoshy if (PMC_IS_SYSTEM_MODE(PMC_TO_MODE(pmc))) { 3212145774Sjkoshy 3213145774Sjkoshy pmc_save_cpu_binding(&pb); 3214145774Sjkoshy pmc_select_cpu(cpu); 3215145774Sjkoshy 3216145774Sjkoshy phw = pmc_pcpu[cpu]->pc_hwpmcs[n]; 3217184802Sjkoshy pcd = pmc_ri_to_classdep(md, n, &adjri); 3218145774Sjkoshy 3219145774Sjkoshy if ((phw->phw_state & PMC_PHW_FLAG_IS_ENABLED) == 0 || 3220184802Sjkoshy (error = pcd->pcd_config_pmc(cpu, adjri, pmc)) != 0) { 3221184802Sjkoshy (void) pcd->pcd_release_pmc(cpu, adjri, pmc); 3222145256Sjkoshy pmc_destroy_pmc_descriptor(pmc); 3223184205Sdes free(pmc, M_PMC); 3224145256Sjkoshy pmc = NULL; 3225145774Sjkoshy pmc_restore_cpu_binding(&pb); 3226145774Sjkoshy error = EPERM; 3227145256Sjkoshy break; 3228145256Sjkoshy } 3229145256Sjkoshy 3230145774Sjkoshy pmc_restore_cpu_binding(&pb); 3231145774Sjkoshy } 3232145256Sjkoshy 3233145256Sjkoshy pmc->pm_state = PMC_STATE_ALLOCATED; 3234145256Sjkoshy 3235145256Sjkoshy /* 3236145256Sjkoshy * mark row disposition 3237145256Sjkoshy */ 3238145256Sjkoshy 3239145256Sjkoshy if (PMC_IS_SYSTEM_MODE(mode)) 3240145256Sjkoshy PMC_MARK_ROW_STANDALONE(n); 3241145256Sjkoshy else 3242145256Sjkoshy PMC_MARK_ROW_THREAD(n); 3243145256Sjkoshy 3244145256Sjkoshy /* 3245145256Sjkoshy * Register this PMC with the current thread as its owner. 3246145256Sjkoshy */ 3247145256Sjkoshy 3248145256Sjkoshy if ((error = 3249145256Sjkoshy pmc_register_owner(curthread->td_proc, pmc)) != 0) { 3250145256Sjkoshy pmc_release_pmc_descriptor(pmc); 3251184205Sdes free(pmc, M_PMC); 3252145256Sjkoshy pmc = NULL; 3253145256Sjkoshy break; 3254145256Sjkoshy } 3255145256Sjkoshy 3256145256Sjkoshy /* 3257145256Sjkoshy * Return the allocated index. 3258145256Sjkoshy */ 3259145256Sjkoshy 3260145774Sjkoshy pa.pm_pmcid = pmc->pm_id; 3261145256Sjkoshy 3262145256Sjkoshy error = copyout(&pa, arg, sizeof(pa)); 3263145256Sjkoshy } 3264145256Sjkoshy break; 3265145256Sjkoshy 3266145256Sjkoshy 3267145256Sjkoshy /* 3268145256Sjkoshy * Attach a PMC to a process. 3269145256Sjkoshy */ 3270145256Sjkoshy 3271145256Sjkoshy case PMC_OP_PMCATTACH: 3272145256Sjkoshy { 3273145256Sjkoshy struct pmc *pm; 3274145256Sjkoshy struct proc *p; 3275145256Sjkoshy struct pmc_op_pmcattach a; 3276145256Sjkoshy 3277145256Sjkoshy sx_assert(&pmc_sx, SX_XLOCKED); 3278145256Sjkoshy 3279145256Sjkoshy if ((error = copyin(arg, &a, sizeof(a))) != 0) 3280145256Sjkoshy break; 3281145256Sjkoshy 3282145256Sjkoshy if (a.pm_pid < 0) { 3283145256Sjkoshy error = EINVAL; 3284145256Sjkoshy break; 3285145256Sjkoshy } else if (a.pm_pid == 0) 3286145256Sjkoshy a.pm_pid = td->td_proc->p_pid; 3287145256Sjkoshy 3288145256Sjkoshy if ((error = pmc_find_pmc(a.pm_pmc, &pm)) != 0) 3289145256Sjkoshy break; 3290145256Sjkoshy 3291145774Sjkoshy if (PMC_IS_SYSTEM_MODE(PMC_TO_MODE(pm))) { 3292145256Sjkoshy error = EINVAL; 3293145256Sjkoshy break; 3294145256Sjkoshy } 3295145256Sjkoshy 3296145256Sjkoshy /* PMCs may be (re)attached only when allocated or stopped */ 3297145256Sjkoshy if (pm->pm_state == PMC_STATE_RUNNING) { 3298145256Sjkoshy error = EBUSY; 3299145256Sjkoshy break; 3300145256Sjkoshy } else if (pm->pm_state != PMC_STATE_ALLOCATED && 3301145256Sjkoshy pm->pm_state != PMC_STATE_STOPPED) { 3302145256Sjkoshy error = EINVAL; 3303145256Sjkoshy break; 3304145256Sjkoshy } 3305145256Sjkoshy 3306145256Sjkoshy /* lookup pid */ 3307145256Sjkoshy if ((p = pfind(a.pm_pid)) == NULL) { 3308145256Sjkoshy error = ESRCH; 3309145256Sjkoshy break; 3310145256Sjkoshy } 3311145256Sjkoshy 3312145256Sjkoshy /* 3313145256Sjkoshy * Ignore processes that are working on exiting. 3314145256Sjkoshy */ 3315145256Sjkoshy if (p->p_flag & P_WEXIT) { 3316145256Sjkoshy error = ESRCH; 3317145256Sjkoshy PROC_UNLOCK(p); /* pfind() returns a locked process */ 3318145256Sjkoshy break; 3319145256Sjkoshy } 3320145256Sjkoshy 3321145256Sjkoshy /* 3322145256Sjkoshy * we are allowed to attach a PMC to a process if 3323145256Sjkoshy * we can debug it. 3324145256Sjkoshy */ 3325145256Sjkoshy error = p_candebug(curthread, p); 3326145256Sjkoshy 3327145256Sjkoshy PROC_UNLOCK(p); 3328145256Sjkoshy 3329145256Sjkoshy if (error == 0) 3330145256Sjkoshy error = pmc_attach_process(p, pm); 3331145256Sjkoshy } 3332145256Sjkoshy break; 3333145256Sjkoshy 3334145256Sjkoshy 3335145256Sjkoshy /* 3336145256Sjkoshy * Detach an attached PMC from a process. 3337145256Sjkoshy */ 3338145256Sjkoshy 3339145256Sjkoshy case PMC_OP_PMCDETACH: 3340145256Sjkoshy { 3341145256Sjkoshy struct pmc *pm; 3342145256Sjkoshy struct proc *p; 3343145256Sjkoshy struct pmc_op_pmcattach a; 3344145256Sjkoshy 3345145256Sjkoshy if ((error = copyin(arg, &a, sizeof(a))) != 0) 3346145256Sjkoshy break; 3347145256Sjkoshy 3348145256Sjkoshy if (a.pm_pid < 0) { 3349145256Sjkoshy error = EINVAL; 3350145256Sjkoshy break; 3351145256Sjkoshy } else if (a.pm_pid == 0) 3352145256Sjkoshy a.pm_pid = td->td_proc->p_pid; 3353145256Sjkoshy 3354145256Sjkoshy if ((error = pmc_find_pmc(a.pm_pmc, &pm)) != 0) 3355145256Sjkoshy break; 3356145256Sjkoshy 3357145256Sjkoshy if ((p = pfind(a.pm_pid)) == NULL) { 3358145256Sjkoshy error = ESRCH; 3359145256Sjkoshy break; 3360145256Sjkoshy } 3361145256Sjkoshy 3362145256Sjkoshy /* 3363145256Sjkoshy * Treat processes that are in the process of exiting 3364145256Sjkoshy * as if they were not present. 3365145256Sjkoshy */ 3366145256Sjkoshy 3367145256Sjkoshy if (p->p_flag & P_WEXIT) 3368145256Sjkoshy error = ESRCH; 3369145256Sjkoshy 3370145256Sjkoshy PROC_UNLOCK(p); /* pfind() returns a locked process */ 3371145256Sjkoshy 3372145256Sjkoshy if (error == 0) 3373145256Sjkoshy error = pmc_detach_process(p, pm); 3374145256Sjkoshy } 3375145256Sjkoshy break; 3376145256Sjkoshy 3377145256Sjkoshy 3378145256Sjkoshy /* 3379147191Sjkoshy * Retrieve the MSR number associated with the counter 3380147191Sjkoshy * 'pmc_id'. This allows processes to directly use RDPMC 3381147191Sjkoshy * instructions to read their PMCs, without the overhead of a 3382147191Sjkoshy * system call. 3383147191Sjkoshy */ 3384147191Sjkoshy 3385147191Sjkoshy case PMC_OP_PMCGETMSR: 3386147191Sjkoshy { 3387184802Sjkoshy int adjri, ri; 3388184802Sjkoshy struct pmc *pm; 3389147191Sjkoshy struct pmc_target *pt; 3390147191Sjkoshy struct pmc_op_getmsr gm; 3391184802Sjkoshy struct pmc_classdep *pcd; 3392147191Sjkoshy 3393147191Sjkoshy PMC_DOWNGRADE_SX(); 3394147191Sjkoshy 3395147191Sjkoshy if ((error = copyin(arg, &gm, sizeof(gm))) != 0) 3396147191Sjkoshy break; 3397147191Sjkoshy 3398147191Sjkoshy if ((error = pmc_find_pmc(gm.pm_pmcid, &pm)) != 0) 3399147191Sjkoshy break; 3400147191Sjkoshy 3401147191Sjkoshy /* 3402147191Sjkoshy * The allocated PMC has to be a process virtual PMC, 3403147191Sjkoshy * i.e., of type MODE_T[CS]. Global PMCs can only be 3404147191Sjkoshy * read using the PMCREAD operation since they may be 3405147191Sjkoshy * allocated on a different CPU than the one we could 3406147191Sjkoshy * be running on at the time of the RDPMC instruction. 3407147191Sjkoshy * 3408147191Sjkoshy * The GETMSR operation is not allowed for PMCs that 3409147191Sjkoshy * are inherited across processes. 3410147191Sjkoshy */ 3411147191Sjkoshy 3412147191Sjkoshy if (!PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm)) || 3413147191Sjkoshy (pm->pm_flags & PMC_F_DESCENDANTS)) { 3414147191Sjkoshy error = EINVAL; 3415147191Sjkoshy break; 3416147191Sjkoshy } 3417147191Sjkoshy 3418147191Sjkoshy /* 3419147191Sjkoshy * It only makes sense to use a RDPMC (or its 3420147191Sjkoshy * equivalent instruction on non-x86 architectures) on 3421147191Sjkoshy * a process that has allocated and attached a PMC to 3422147191Sjkoshy * itself. Conversely the PMC is only allowed to have 3423147191Sjkoshy * one process attached to it -- its owner. 3424147191Sjkoshy */ 3425147191Sjkoshy 3426147191Sjkoshy if ((pt = LIST_FIRST(&pm->pm_targets)) == NULL || 3427147191Sjkoshy LIST_NEXT(pt, pt_next) != NULL || 3428147191Sjkoshy pt->pt_process->pp_proc != pm->pm_owner->po_owner) { 3429147191Sjkoshy error = EINVAL; 3430147191Sjkoshy break; 3431147191Sjkoshy } 3432147191Sjkoshy 3433147191Sjkoshy ri = PMC_TO_ROWINDEX(pm); 3434184802Sjkoshy pcd = pmc_ri_to_classdep(md, ri, &adjri); 3435147191Sjkoshy 3436184802Sjkoshy /* PMC class has no 'GETMSR' support */ 3437184802Sjkoshy if (pcd->pcd_get_msr == NULL) { 3438184802Sjkoshy error = ENOSYS; 3439147191Sjkoshy break; 3440184802Sjkoshy } 3441147191Sjkoshy 3442184802Sjkoshy if ((error = (*pcd->pcd_get_msr)(adjri, &gm.pm_msr)) < 0) 3443184802Sjkoshy break; 3444184802Sjkoshy 3445147191Sjkoshy if ((error = copyout(&gm, arg, sizeof(gm))) < 0) 3446147191Sjkoshy break; 3447147191Sjkoshy 3448147191Sjkoshy /* 3449147191Sjkoshy * Mark our process as using MSRs. Update machine 3450147191Sjkoshy * state using a forced context switch. 3451147191Sjkoshy */ 3452147191Sjkoshy 3453147191Sjkoshy pt->pt_process->pp_flags |= PMC_PP_ENABLE_MSR_ACCESS; 3454147191Sjkoshy pmc_force_context_switch(); 3455147191Sjkoshy 3456147191Sjkoshy } 3457147191Sjkoshy break; 3458147191Sjkoshy 3459147191Sjkoshy /* 3460145256Sjkoshy * Release an allocated PMC 3461145256Sjkoshy */ 3462145256Sjkoshy 3463145256Sjkoshy case PMC_OP_PMCRELEASE: 3464145256Sjkoshy { 3465145256Sjkoshy pmc_id_t pmcid; 3466145256Sjkoshy struct pmc *pm; 3467145256Sjkoshy struct pmc_owner *po; 3468145256Sjkoshy struct pmc_op_simple sp; 3469145256Sjkoshy 3470145256Sjkoshy /* 3471145256Sjkoshy * Find PMC pointer for the named PMC. 3472145256Sjkoshy * 3473145256Sjkoshy * Use pmc_release_pmc_descriptor() to switch off the 3474145256Sjkoshy * PMC, remove all its target threads, and remove the 3475145256Sjkoshy * PMC from its owner's list. 3476145256Sjkoshy * 3477145256Sjkoshy * Remove the owner record if this is the last PMC 3478145256Sjkoshy * owned. 3479145256Sjkoshy * 3480145256Sjkoshy * Free up space. 3481145256Sjkoshy */ 3482145256Sjkoshy 3483145256Sjkoshy if ((error = copyin(arg, &sp, sizeof(sp))) != 0) 3484145256Sjkoshy break; 3485145256Sjkoshy 3486145256Sjkoshy pmcid = sp.pm_pmcid; 3487145256Sjkoshy 3488145256Sjkoshy if ((error = pmc_find_pmc(pmcid, &pm)) != 0) 3489145256Sjkoshy break; 3490145256Sjkoshy 3491145256Sjkoshy po = pm->pm_owner; 3492145256Sjkoshy pmc_release_pmc_descriptor(pm); 3493145256Sjkoshy pmc_maybe_remove_owner(po); 3494145256Sjkoshy 3495184205Sdes free(pm, M_PMC); 3496145256Sjkoshy } 3497145256Sjkoshy break; 3498145256Sjkoshy 3499145256Sjkoshy 3500145256Sjkoshy /* 3501145256Sjkoshy * Read and/or write a PMC. 3502145256Sjkoshy */ 3503145256Sjkoshy 3504145256Sjkoshy case PMC_OP_PMCRW: 3505145256Sjkoshy { 3506184802Sjkoshy int adjri; 3507184802Sjkoshy struct pmc *pm; 3508145256Sjkoshy uint32_t cpu, ri; 3509184802Sjkoshy pmc_value_t oldvalue; 3510184802Sjkoshy struct pmc_binding pb; 3511184802Sjkoshy struct pmc_op_pmcrw prw; 3512184802Sjkoshy struct pmc_classdep *pcd; 3513145256Sjkoshy struct pmc_op_pmcrw *pprw; 3514145256Sjkoshy 3515145256Sjkoshy PMC_DOWNGRADE_SX(); 3516145256Sjkoshy 3517145256Sjkoshy if ((error = copyin(arg, &prw, sizeof(prw))) != 0) 3518145256Sjkoshy break; 3519145256Sjkoshy 3520145301Simp ri = 0; 3521145256Sjkoshy PMCDBG(PMC,OPS,1, "rw id=%d flags=0x%x", prw.pm_pmcid, 3522145256Sjkoshy prw.pm_flags); 3523145256Sjkoshy 3524145256Sjkoshy /* must have at least one flag set */ 3525145256Sjkoshy if ((prw.pm_flags & (PMC_F_OLDVALUE|PMC_F_NEWVALUE)) == 0) { 3526145256Sjkoshy error = EINVAL; 3527145256Sjkoshy break; 3528145256Sjkoshy } 3529145256Sjkoshy 3530145256Sjkoshy /* locate pmc descriptor */ 3531145256Sjkoshy if ((error = pmc_find_pmc(prw.pm_pmcid, &pm)) != 0) 3532145256Sjkoshy break; 3533145256Sjkoshy 3534145256Sjkoshy /* Can't read a PMC that hasn't been started. */ 3535145256Sjkoshy if (pm->pm_state != PMC_STATE_ALLOCATED && 3536145256Sjkoshy pm->pm_state != PMC_STATE_STOPPED && 3537145256Sjkoshy pm->pm_state != PMC_STATE_RUNNING) { 3538145256Sjkoshy error = EINVAL; 3539145256Sjkoshy break; 3540145256Sjkoshy } 3541145256Sjkoshy 3542145256Sjkoshy /* writing a new value is allowed only for 'STOPPED' pmcs */ 3543145256Sjkoshy if (pm->pm_state == PMC_STATE_RUNNING && 3544145256Sjkoshy (prw.pm_flags & PMC_F_NEWVALUE)) { 3545145256Sjkoshy error = EBUSY; 3546145256Sjkoshy break; 3547145256Sjkoshy } 3548145256Sjkoshy 3549145774Sjkoshy if (PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm))) { 3550145256Sjkoshy 3551145774Sjkoshy /* 3552145774Sjkoshy * If this PMC is attached to its owner (i.e., 3553145774Sjkoshy * the process requesting this operation) and 3554145774Sjkoshy * is running, then attempt to get an 3555145774Sjkoshy * upto-date reading from hardware for a READ. 3556145774Sjkoshy * Writes are only allowed when the PMC is 3557145774Sjkoshy * stopped, so only update the saved value 3558145774Sjkoshy * field. 3559145774Sjkoshy * 3560145774Sjkoshy * If the PMC is not running, or is not 3561145774Sjkoshy * attached to its owner, read/write to the 3562145774Sjkoshy * savedvalue field. 3563145774Sjkoshy */ 3564145774Sjkoshy 3565145774Sjkoshy ri = PMC_TO_ROWINDEX(pm); 3566184802Sjkoshy pcd = pmc_ri_to_classdep(md, ri, &adjri); 3567145774Sjkoshy 3568145256Sjkoshy mtx_pool_lock_spin(pmc_mtxpool, pm); 3569145774Sjkoshy cpu = curthread->td_oncpu; 3570145774Sjkoshy 3571145774Sjkoshy if (prw.pm_flags & PMC_F_OLDVALUE) { 3572145774Sjkoshy if ((pm->pm_flags & PMC_F_ATTACHED_TO_OWNER) && 3573145774Sjkoshy (pm->pm_state == PMC_STATE_RUNNING)) 3574184802Sjkoshy error = (*pcd->pcd_read_pmc)(cpu, adjri, 3575145774Sjkoshy &oldvalue); 3576145774Sjkoshy else 3577145774Sjkoshy oldvalue = pm->pm_gv.pm_savedvalue; 3578145774Sjkoshy } 3579145256Sjkoshy if (prw.pm_flags & PMC_F_NEWVALUE) 3580145256Sjkoshy pm->pm_gv.pm_savedvalue = prw.pm_value; 3581145774Sjkoshy 3582145256Sjkoshy mtx_pool_unlock_spin(pmc_mtxpool, pm); 3583145256Sjkoshy 3584145256Sjkoshy } else { /* System mode PMCs */ 3585145774Sjkoshy cpu = PMC_TO_CPU(pm); 3586145774Sjkoshy ri = PMC_TO_ROWINDEX(pm); 3587184802Sjkoshy pcd = pmc_ri_to_classdep(md, ri, &adjri); 3588145256Sjkoshy 3589183266Sjkoshy if (!pmc_cpu_is_active(cpu)) { 3590145256Sjkoshy error = ENXIO; 3591145256Sjkoshy break; 3592145256Sjkoshy } 3593145256Sjkoshy 3594145256Sjkoshy /* move this thread to CPU 'cpu' */ 3595145256Sjkoshy pmc_save_cpu_binding(&pb); 3596145256Sjkoshy pmc_select_cpu(cpu); 3597145256Sjkoshy 3598145774Sjkoshy critical_enter(); 3599145256Sjkoshy /* save old value */ 3600145256Sjkoshy if (prw.pm_flags & PMC_F_OLDVALUE) 3601184802Sjkoshy if ((error = (*pcd->pcd_read_pmc)(cpu, adjri, 3602145256Sjkoshy &oldvalue))) 3603145256Sjkoshy goto error; 3604145256Sjkoshy /* write out new value */ 3605145256Sjkoshy if (prw.pm_flags & PMC_F_NEWVALUE) 3606184802Sjkoshy error = (*pcd->pcd_write_pmc)(cpu, adjri, 3607145256Sjkoshy prw.pm_value); 3608145256Sjkoshy error: 3609145774Sjkoshy critical_exit(); 3610145256Sjkoshy pmc_restore_cpu_binding(&pb); 3611145256Sjkoshy if (error) 3612145256Sjkoshy break; 3613145256Sjkoshy } 3614145256Sjkoshy 3615145256Sjkoshy pprw = (struct pmc_op_pmcrw *) arg; 3616145256Sjkoshy 3617153110Sru#ifdef DEBUG 3618145256Sjkoshy if (prw.pm_flags & PMC_F_NEWVALUE) 3619145256Sjkoshy PMCDBG(PMC,OPS,2, "rw id=%d new %jx -> old %jx", 3620145256Sjkoshy ri, prw.pm_value, oldvalue); 3621156778Sjkoshy else if (prw.pm_flags & PMC_F_OLDVALUE) 3622145256Sjkoshy PMCDBG(PMC,OPS,2, "rw id=%d -> old %jx", ri, oldvalue); 3623145256Sjkoshy#endif 3624145256Sjkoshy 3625145256Sjkoshy /* return old value if requested */ 3626145256Sjkoshy if (prw.pm_flags & PMC_F_OLDVALUE) 3627145256Sjkoshy if ((error = copyout(&oldvalue, &pprw->pm_value, 3628145256Sjkoshy sizeof(prw.pm_value)))) 3629145256Sjkoshy break; 3630145256Sjkoshy 3631145256Sjkoshy } 3632145256Sjkoshy break; 3633145256Sjkoshy 3634145256Sjkoshy 3635145256Sjkoshy /* 3636145256Sjkoshy * Set the sampling rate for a sampling mode PMC and the 3637145256Sjkoshy * initial count for a counting mode PMC. 3638145256Sjkoshy */ 3639145256Sjkoshy 3640145256Sjkoshy case PMC_OP_PMCSETCOUNT: 3641145256Sjkoshy { 3642145256Sjkoshy struct pmc *pm; 3643145256Sjkoshy struct pmc_op_pmcsetcount sc; 3644145256Sjkoshy 3645145256Sjkoshy PMC_DOWNGRADE_SX(); 3646145256Sjkoshy 3647145256Sjkoshy if ((error = copyin(arg, &sc, sizeof(sc))) != 0) 3648145256Sjkoshy break; 3649145256Sjkoshy 3650145256Sjkoshy if ((error = pmc_find_pmc(sc.pm_pmcid, &pm)) != 0) 3651145256Sjkoshy break; 3652145256Sjkoshy 3653145256Sjkoshy if (pm->pm_state == PMC_STATE_RUNNING) { 3654145256Sjkoshy error = EBUSY; 3655145256Sjkoshy break; 3656145256Sjkoshy } 3657145256Sjkoshy 3658145774Sjkoshy if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm))) 3659145256Sjkoshy pm->pm_sc.pm_reloadcount = sc.pm_count; 3660145256Sjkoshy else 3661145256Sjkoshy pm->pm_sc.pm_initial = sc.pm_count; 3662145256Sjkoshy } 3663145256Sjkoshy break; 3664145256Sjkoshy 3665145256Sjkoshy 3666145256Sjkoshy /* 3667145256Sjkoshy * Start a PMC. 3668145256Sjkoshy */ 3669145256Sjkoshy 3670145256Sjkoshy case PMC_OP_PMCSTART: 3671145256Sjkoshy { 3672145256Sjkoshy pmc_id_t pmcid; 3673145256Sjkoshy struct pmc *pm; 3674145256Sjkoshy struct pmc_op_simple sp; 3675145256Sjkoshy 3676145256Sjkoshy sx_assert(&pmc_sx, SX_XLOCKED); 3677145256Sjkoshy 3678145256Sjkoshy if ((error = copyin(arg, &sp, sizeof(sp))) != 0) 3679145256Sjkoshy break; 3680145256Sjkoshy 3681145256Sjkoshy pmcid = sp.pm_pmcid; 3682145256Sjkoshy 3683145256Sjkoshy if ((error = pmc_find_pmc(pmcid, &pm)) != 0) 3684145256Sjkoshy break; 3685145256Sjkoshy 3686145774Sjkoshy KASSERT(pmcid == pm->pm_id, 3687145774Sjkoshy ("[pmc,%d] pmcid %x != id %x", __LINE__, 3688145774Sjkoshy pm->pm_id, pmcid)); 3689145256Sjkoshy 3690145256Sjkoshy if (pm->pm_state == PMC_STATE_RUNNING) /* already running */ 3691145256Sjkoshy break; 3692145256Sjkoshy else if (pm->pm_state != PMC_STATE_STOPPED && 3693145256Sjkoshy pm->pm_state != PMC_STATE_ALLOCATED) { 3694145256Sjkoshy error = EINVAL; 3695145256Sjkoshy break; 3696145256Sjkoshy } 3697145256Sjkoshy 3698145256Sjkoshy error = pmc_start(pm); 3699145256Sjkoshy } 3700145256Sjkoshy break; 3701145256Sjkoshy 3702145256Sjkoshy 3703145256Sjkoshy /* 3704145256Sjkoshy * Stop a PMC. 3705145256Sjkoshy */ 3706145256Sjkoshy 3707145256Sjkoshy case PMC_OP_PMCSTOP: 3708145256Sjkoshy { 3709145256Sjkoshy pmc_id_t pmcid; 3710145256Sjkoshy struct pmc *pm; 3711145256Sjkoshy struct pmc_op_simple sp; 3712145256Sjkoshy 3713145256Sjkoshy PMC_DOWNGRADE_SX(); 3714145256Sjkoshy 3715145256Sjkoshy if ((error = copyin(arg, &sp, sizeof(sp))) != 0) 3716145256Sjkoshy break; 3717145256Sjkoshy 3718145256Sjkoshy pmcid = sp.pm_pmcid; 3719145256Sjkoshy 3720145256Sjkoshy /* 3721145256Sjkoshy * Mark the PMC as inactive and invoke the MD stop 3722145256Sjkoshy * routines if needed. 3723145256Sjkoshy */ 3724145256Sjkoshy 3725145256Sjkoshy if ((error = pmc_find_pmc(pmcid, &pm)) != 0) 3726145256Sjkoshy break; 3727145256Sjkoshy 3728145774Sjkoshy KASSERT(pmcid == pm->pm_id, 3729145774Sjkoshy ("[pmc,%d] pmc id %x != pmcid %x", __LINE__, 3730145774Sjkoshy pm->pm_id, pmcid)); 3731145256Sjkoshy 3732145256Sjkoshy if (pm->pm_state == PMC_STATE_STOPPED) /* already stopped */ 3733145256Sjkoshy break; 3734145256Sjkoshy else if (pm->pm_state != PMC_STATE_RUNNING) { 3735145256Sjkoshy error = EINVAL; 3736145256Sjkoshy break; 3737145256Sjkoshy } 3738145256Sjkoshy 3739145256Sjkoshy error = pmc_stop(pm); 3740145256Sjkoshy } 3741145256Sjkoshy break; 3742145256Sjkoshy 3743145256Sjkoshy 3744145256Sjkoshy /* 3745147867Sjkoshy * Write a user supplied value to the log file. 3746145256Sjkoshy */ 3747145256Sjkoshy 3748145256Sjkoshy case PMC_OP_WRITELOG: 3749145256Sjkoshy { 3750147191Sjkoshy struct pmc_op_writelog wl; 3751147191Sjkoshy struct pmc_owner *po; 3752145256Sjkoshy 3753145256Sjkoshy PMC_DOWNGRADE_SX(); 3754145256Sjkoshy 3755147191Sjkoshy if ((error = copyin(arg, &wl, sizeof(wl))) != 0) 3756145256Sjkoshy break; 3757145256Sjkoshy 3758147191Sjkoshy if ((po = pmc_find_owner_descriptor(td->td_proc)) == NULL) { 3759145256Sjkoshy error = EINVAL; 3760145256Sjkoshy break; 3761145256Sjkoshy } 3762145256Sjkoshy 3763147191Sjkoshy if ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0) { 3764145774Sjkoshy error = EINVAL; 3765145774Sjkoshy break; 3766145774Sjkoshy } 3767145774Sjkoshy 3768147191Sjkoshy error = pmclog_process_userlog(po, &wl); 3769145256Sjkoshy } 3770145256Sjkoshy break; 3771145256Sjkoshy 3772147191Sjkoshy 3773145256Sjkoshy default: 3774145256Sjkoshy error = EINVAL; 3775145256Sjkoshy break; 3776145256Sjkoshy } 3777145256Sjkoshy 3778195005Sattilio if (is_sx_locked != 0) { 3779195005Sattilio if (is_sx_downgraded) 3780195005Sattilio sx_sunlock(&pmc_sx); 3781195005Sattilio else 3782195005Sattilio sx_xunlock(&pmc_sx); 3783195005Sattilio } 3784145256Sjkoshy 3785145256Sjkoshy if (error) 3786145256Sjkoshy atomic_add_int(&pmc_stats.pm_syscall_errors, 1); 3787145256Sjkoshy 3788147191Sjkoshy PICKUP_GIANT(); 3789147191Sjkoshy 3790145256Sjkoshy return error; 3791145256Sjkoshy} 3792145256Sjkoshy 3793145256Sjkoshy/* 3794145256Sjkoshy * Helper functions 3795145256Sjkoshy */ 3796145256Sjkoshy 3797147191Sjkoshy 3798145256Sjkoshy/* 3799174395Sjkoshy * Mark the thread as needing callchain capture and post an AST. The 3800174395Sjkoshy * actual callchain capture will be done in a context where it is safe 3801174395Sjkoshy * to take page faults. 3802174395Sjkoshy */ 3803174395Sjkoshy 3804174395Sjkoshystatic void 3805186037Sjkoshypmc_post_callchain_callback(void) 3806174395Sjkoshy{ 3807174395Sjkoshy struct thread *td; 3808174395Sjkoshy 3809174395Sjkoshy td = curthread; 3810174395Sjkoshy 3811186037Sjkoshy KASSERT((td->td_pflags & TDP_CALLCHAIN) == 0, 3812186037Sjkoshy ("[pmc,%d] thread %p already marked for callchain capture", 3813186037Sjkoshy __LINE__, (void *) td)); 3814186037Sjkoshy 3815174395Sjkoshy /* 3816186037Sjkoshy * Mark this thread as needing callchain capture. 3817186037Sjkoshy * `td->td_pflags' will be safe to touch because this thread 3818186037Sjkoshy * was in user space when it was interrupted. 3819174395Sjkoshy */ 3820174395Sjkoshy td->td_pflags |= TDP_CALLCHAIN; 3821174395Sjkoshy 3822174395Sjkoshy /* 3823186037Sjkoshy * Don't let this thread migrate between CPUs until callchain 3824186037Sjkoshy * capture completes. 3825174395Sjkoshy */ 3826186037Sjkoshy sched_pin(); 3827174395Sjkoshy 3828174395Sjkoshy return; 3829174395Sjkoshy} 3830174395Sjkoshy 3831174395Sjkoshy/* 3832147191Sjkoshy * Interrupt processing. 3833147191Sjkoshy * 3834174395Sjkoshy * Find a free slot in the per-cpu array of samples and capture the 3835174395Sjkoshy * current callchain there. If a sample was successfully added, a bit 3836174395Sjkoshy * is set in mask 'pmc_cpumask' denoting that the DO_SAMPLES hook 3837174395Sjkoshy * needs to be invoked from the clock handler. 3838147191Sjkoshy * 3839147191Sjkoshy * This function is meant to be called from an NMI handler. It cannot 3840147191Sjkoshy * use any of the locking primitives supplied by the OS. 3841145256Sjkoshy */ 3842145256Sjkoshy 3843147191Sjkoshyint 3844174395Sjkoshypmc_process_interrupt(int cpu, struct pmc *pm, struct trapframe *tf, 3845174395Sjkoshy int inuserspace) 3846145256Sjkoshy{ 3847174395Sjkoshy int error, callchaindepth; 3848147191Sjkoshy struct thread *td; 3849147191Sjkoshy struct pmc_sample *ps; 3850147191Sjkoshy struct pmc_samplebuffer *psb; 3851145256Sjkoshy 3852147191Sjkoshy error = 0; 3853145256Sjkoshy 3854174395Sjkoshy /* 3855174395Sjkoshy * Allocate space for a sample buffer. 3856174395Sjkoshy */ 3857147191Sjkoshy psb = pmc_pcpu[cpu]->pc_sb; 3858145256Sjkoshy 3859147191Sjkoshy ps = psb->ps_write; 3860174395Sjkoshy if (ps->ps_nsamples) { /* in use, reader hasn't caught up */ 3861147867Sjkoshy pm->pm_stalled = 1; 3862147191Sjkoshy atomic_add_int(&pmc_stats.pm_intr_bufferfull, 1); 3863174395Sjkoshy PMCDBG(SAM,INT,1,"(spc) cpu=%d pm=%p tf=%p um=%d wr=%d rd=%d", 3864174395Sjkoshy cpu, pm, (void *) tf, inuserspace, 3865147191Sjkoshy (int) (psb->ps_write - psb->ps_samples), 3866147191Sjkoshy (int) (psb->ps_read - psb->ps_samples)); 3867147191Sjkoshy error = ENOMEM; 3868147191Sjkoshy goto done; 3869147191Sjkoshy } 3870145256Sjkoshy 3871174395Sjkoshy 3872174395Sjkoshy /* Fill in entry. */ 3873174395Sjkoshy PMCDBG(SAM,INT,1,"cpu=%d pm=%p tf=%p um=%d wr=%d rd=%d", cpu, pm, 3874174395Sjkoshy (void *) tf, inuserspace, 3875147191Sjkoshy (int) (psb->ps_write - psb->ps_samples), 3876147191Sjkoshy (int) (psb->ps_read - psb->ps_samples)); 3877145256Sjkoshy 3878186037Sjkoshy KASSERT(pm->pm_runcount >= 0, 3879186037Sjkoshy ("[pmc,%d] pm=%p runcount %d", __LINE__, (void *) pm, 3880186037Sjkoshy pm->pm_runcount)); 3881186037Sjkoshy 3882174395Sjkoshy atomic_add_rel_32(&pm->pm_runcount, 1); /* hold onto PMC */ 3883147191Sjkoshy ps->ps_pmc = pm; 3884147191Sjkoshy if ((td = curthread) && td->td_proc) 3885147191Sjkoshy ps->ps_pid = td->td_proc->p_pid; 3886147191Sjkoshy else 3887147191Sjkoshy ps->ps_pid = -1; 3888174395Sjkoshy ps->ps_cpu = cpu; 3889186037Sjkoshy ps->ps_td = td; 3890174395Sjkoshy ps->ps_flags = inuserspace ? PMC_CC_F_USERSPACE : 0; 3891145256Sjkoshy 3892174395Sjkoshy callchaindepth = (pm->pm_flags & PMC_F_CALLCHAIN) ? 3893174395Sjkoshy pmc_callchaindepth : 1; 3894174395Sjkoshy 3895174395Sjkoshy if (callchaindepth == 1) 3896174395Sjkoshy ps->ps_pc[0] = PMC_TRAPFRAME_TO_PC(tf); 3897174395Sjkoshy else { 3898174395Sjkoshy /* 3899174395Sjkoshy * Kernel stack traversals can be done immediately, 3900174395Sjkoshy * while we defer to an AST for user space traversals. 3901174395Sjkoshy */ 3902174395Sjkoshy if (!inuserspace) 3903174395Sjkoshy callchaindepth = 3904174395Sjkoshy pmc_save_kernel_callchain(ps->ps_pc, 3905174395Sjkoshy callchaindepth, tf); 3906174395Sjkoshy else { 3907186037Sjkoshy pmc_post_callchain_callback(); 3908174395Sjkoshy callchaindepth = PMC_SAMPLE_INUSE; 3909174395Sjkoshy } 3910174395Sjkoshy } 3911174395Sjkoshy 3912174395Sjkoshy ps->ps_nsamples = callchaindepth; /* mark entry as in use */ 3913174395Sjkoshy 3914147191Sjkoshy /* increment write pointer, modulo ring buffer size */ 3915147191Sjkoshy ps++; 3916147191Sjkoshy if (ps == psb->ps_fence) 3917147191Sjkoshy psb->ps_write = psb->ps_samples; 3918147191Sjkoshy else 3919147191Sjkoshy psb->ps_write = ps; 3920145256Sjkoshy 3921147191Sjkoshy done: 3922147191Sjkoshy /* mark CPU as needing processing */ 3923147191Sjkoshy atomic_set_rel_int(&pmc_cpumask, (1 << cpu)); 3924147191Sjkoshy 3925174395Sjkoshy return (error); 3926145256Sjkoshy} 3927145256Sjkoshy 3928174395Sjkoshy/* 3929174395Sjkoshy * Capture a user call chain. This function will be called from ast() 3930174395Sjkoshy * before control returns to userland and before the process gets 3931174395Sjkoshy * rescheduled. 3932174395Sjkoshy */ 3933147191Sjkoshy 3934174395Sjkoshystatic void 3935174395Sjkoshypmc_capture_user_callchain(int cpu, struct trapframe *tf) 3936174395Sjkoshy{ 3937174395Sjkoshy int i; 3938174395Sjkoshy struct pmc *pm; 3939186037Sjkoshy struct thread *td; 3940174395Sjkoshy struct pmc_sample *ps; 3941174395Sjkoshy struct pmc_samplebuffer *psb; 3942186037Sjkoshy#ifdef INVARIANTS 3943186037Sjkoshy int ncallchains; 3944186037Sjkoshy#endif 3945174395Sjkoshy 3946186037Sjkoshy sched_unpin(); /* Can migrate safely now. */ 3947186037Sjkoshy 3948174395Sjkoshy psb = pmc_pcpu[cpu]->pc_sb; 3949186037Sjkoshy td = curthread; 3950174395Sjkoshy 3951186037Sjkoshy KASSERT(td->td_pflags & TDP_CALLCHAIN, 3952186037Sjkoshy ("[pmc,%d] Retrieving callchain for thread that doesn't want it", 3953186037Sjkoshy __LINE__)); 3954186037Sjkoshy 3955186037Sjkoshy#ifdef INVARIANTS 3956186037Sjkoshy ncallchains = 0; 3957186037Sjkoshy#endif 3958186037Sjkoshy 3959174395Sjkoshy /* 3960174395Sjkoshy * Iterate through all deferred callchain requests. 3961174395Sjkoshy */ 3962174395Sjkoshy 3963186037Sjkoshy ps = psb->ps_samples; 3964186037Sjkoshy for (i = 0; i < pmc_nsamples; i++, ps++) { 3965174395Sjkoshy 3966174395Sjkoshy if (ps->ps_nsamples != PMC_SAMPLE_INUSE) 3967174395Sjkoshy continue; 3968186037Sjkoshy if (ps->ps_td != td) 3969186037Sjkoshy continue; 3970174395Sjkoshy 3971186037Sjkoshy KASSERT(ps->ps_cpu == cpu, 3972186037Sjkoshy ("[pmc,%d] cpu mismatch ps_cpu=%d pcpu=%d", __LINE__, 3973186037Sjkoshy ps->ps_cpu, PCPU_GET(cpuid))); 3974186037Sjkoshy 3975174395Sjkoshy pm = ps->ps_pmc; 3976174395Sjkoshy 3977174395Sjkoshy KASSERT(pm->pm_flags & PMC_F_CALLCHAIN, 3978174395Sjkoshy ("[pmc,%d] Retrieving callchain for PMC that doesn't " 3979174395Sjkoshy "want it", __LINE__)); 3980174395Sjkoshy 3981186037Sjkoshy KASSERT(pm->pm_runcount > 0, 3982186037Sjkoshy ("[pmc,%d] runcount %d", __LINE__, pm->pm_runcount)); 3983186037Sjkoshy 3984174395Sjkoshy /* 3985174395Sjkoshy * Retrieve the callchain and mark the sample buffer 3986174395Sjkoshy * as 'processable' by the timer tick sweep code. 3987174395Sjkoshy */ 3988174395Sjkoshy ps->ps_nsamples = pmc_save_user_callchain(ps->ps_pc, 3989174395Sjkoshy pmc_callchaindepth, tf); 3990186037Sjkoshy 3991186037Sjkoshy#ifdef INVARIANTS 3992186037Sjkoshy ncallchains++; 3993186037Sjkoshy#endif 3994186037Sjkoshy 3995174395Sjkoshy } 3996174395Sjkoshy 3997186037Sjkoshy KASSERT(ncallchains > 0, 3998186037Sjkoshy ("[pmc,%d] cpu %d didn't find a sample to collect", __LINE__, 3999186037Sjkoshy cpu)); 4000186037Sjkoshy 4001174395Sjkoshy return; 4002174395Sjkoshy} 4003174395Sjkoshy 4004174395Sjkoshy 4005145256Sjkoshy/* 4006147191Sjkoshy * Process saved PC samples. 4007145256Sjkoshy */ 4008145256Sjkoshy 4009145256Sjkoshystatic void 4010147191Sjkoshypmc_process_samples(int cpu) 4011145256Sjkoshy{ 4012147191Sjkoshy struct pmc *pm; 4013185363Sjkoshy int adjri, n; 4014147191Sjkoshy struct thread *td; 4015147191Sjkoshy struct pmc_owner *po; 4016147191Sjkoshy struct pmc_sample *ps; 4017184802Sjkoshy struct pmc_classdep *pcd; 4018147191Sjkoshy struct pmc_samplebuffer *psb; 4019145256Sjkoshy 4020147191Sjkoshy KASSERT(PCPU_GET(cpuid) == cpu, 4021147191Sjkoshy ("[pmc,%d] not on the correct CPU pcpu=%d cpu=%d", __LINE__, 4022147191Sjkoshy PCPU_GET(cpuid), cpu)); 4023145256Sjkoshy 4024147191Sjkoshy psb = pmc_pcpu[cpu]->pc_sb; 4025147191Sjkoshy 4026147191Sjkoshy for (n = 0; n < pmc_nsamples; n++) { /* bound on #iterations */ 4027147191Sjkoshy 4028147191Sjkoshy ps = psb->ps_read; 4029174395Sjkoshy if (ps->ps_nsamples == PMC_SAMPLE_FREE) 4030147191Sjkoshy break; 4031174395Sjkoshy if (ps->ps_nsamples == PMC_SAMPLE_INUSE) { 4032174395Sjkoshy /* Need a rescan at a later time. */ 4033174395Sjkoshy atomic_set_rel_int(&pmc_cpumask, (1 << cpu)); 4034174395Sjkoshy break; 4035174395Sjkoshy } 4036147191Sjkoshy 4037147191Sjkoshy pm = ps->ps_pmc; 4038186037Sjkoshy 4039186037Sjkoshy KASSERT(pm->pm_runcount > 0, 4040186037Sjkoshy ("[pmc,%d] pm=%p runcount %d", __LINE__, (void *) pm, 4041186037Sjkoshy pm->pm_runcount)); 4042186037Sjkoshy 4043147191Sjkoshy po = pm->pm_owner; 4044147191Sjkoshy 4045147191Sjkoshy KASSERT(PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)), 4046147191Sjkoshy ("[pmc,%d] pmc=%p non-sampling mode=%d", __LINE__, 4047147191Sjkoshy pm, PMC_TO_MODE(pm))); 4048147191Sjkoshy 4049147191Sjkoshy /* Ignore PMCs that have been switched off */ 4050147191Sjkoshy if (pm->pm_state != PMC_STATE_RUNNING) 4051147191Sjkoshy goto entrydone; 4052147191Sjkoshy 4053174395Sjkoshy PMCDBG(SAM,OPS,1,"cpu=%d pm=%p n=%d fl=%x wr=%d rd=%d", cpu, 4054174395Sjkoshy pm, ps->ps_nsamples, ps->ps_flags, 4055147191Sjkoshy (int) (psb->ps_write - psb->ps_samples), 4056147191Sjkoshy (int) (psb->ps_read - psb->ps_samples)); 4057147191Sjkoshy 4058147191Sjkoshy /* 4059147191Sjkoshy * If this is a process-mode PMC that is attached to 4060147191Sjkoshy * its owner, and if the PC is in user mode, update 4061147191Sjkoshy * profiling statistics like timer-based profiling 4062147191Sjkoshy * would have done. 4063147191Sjkoshy */ 4064147191Sjkoshy if (pm->pm_flags & PMC_F_ATTACHED_TO_OWNER) { 4065174395Sjkoshy if (ps->ps_flags & PMC_CC_F_USERSPACE) { 4066147191Sjkoshy td = FIRST_THREAD_IN_PROC(po->po_owner); 4067174395Sjkoshy addupc_intr(td, ps->ps_pc[0], 1); 4068147191Sjkoshy } 4069147191Sjkoshy goto entrydone; 4070147191Sjkoshy } 4071147191Sjkoshy 4072147191Sjkoshy /* 4073147191Sjkoshy * Otherwise, this is either a sampling mode PMC that 4074147191Sjkoshy * is attached to a different process than its owner, 4075147191Sjkoshy * or a system-wide sampling PMC. Dispatch a log 4076147191Sjkoshy * entry to the PMC's owner process. 4077147191Sjkoshy */ 4078147191Sjkoshy 4079174395Sjkoshy pmclog_process_callchain(pm, ps); 4080147191Sjkoshy 4081147191Sjkoshy entrydone: 4082174395Sjkoshy ps->ps_nsamples = 0; /* mark entry as free */ 4083147191Sjkoshy atomic_subtract_rel_32(&pm->pm_runcount, 1); 4084147191Sjkoshy 4085147191Sjkoshy /* increment read pointer, modulo sample size */ 4086147191Sjkoshy if (++ps == psb->ps_fence) 4087147191Sjkoshy psb->ps_read = psb->ps_samples; 4088147191Sjkoshy else 4089147191Sjkoshy psb->ps_read = ps; 4090147191Sjkoshy } 4091147191Sjkoshy 4092147191Sjkoshy atomic_add_int(&pmc_stats.pm_log_sweeps, 1); 4093147191Sjkoshy 4094147191Sjkoshy /* Do not re-enable stalled PMCs if we failed to process any samples */ 4095147191Sjkoshy if (n == 0) 4096147191Sjkoshy return; 4097147191Sjkoshy 4098147191Sjkoshy /* 4099147191Sjkoshy * Restart any stalled sampling PMCs on this CPU. 4100147191Sjkoshy * 4101147867Sjkoshy * If the NMI handler sets the pm_stalled field of a PMC after 4102147867Sjkoshy * the check below, we'll end up processing the stalled PMC at 4103147867Sjkoshy * the next hardclock tick. 4104147191Sjkoshy */ 4105147191Sjkoshy for (n = 0; n < md->pmd_npmc; n++) { 4106184802Sjkoshy pcd = pmc_ri_to_classdep(md, n, &adjri); 4107184802Sjkoshy KASSERT(pcd != NULL, 4108184802Sjkoshy ("[pmc,%d] null pcd ri=%d", __LINE__, n)); 4109184802Sjkoshy (void) (*pcd->pcd_get_config)(cpu,adjri,&pm); 4110184802Sjkoshy 4111147191Sjkoshy if (pm == NULL || /* !cfg'ed */ 4112147191Sjkoshy pm->pm_state != PMC_STATE_RUNNING || /* !active */ 4113147191Sjkoshy !PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)) || /* !sampling */ 4114147867Sjkoshy pm->pm_stalled == 0) /* !stalled */ 4115147191Sjkoshy continue; 4116147191Sjkoshy 4117147867Sjkoshy pm->pm_stalled = 0; 4118184802Sjkoshy (*pcd->pcd_start_pmc)(cpu, adjri); 4119147191Sjkoshy } 4120145256Sjkoshy} 4121145256Sjkoshy 4122145256Sjkoshy/* 4123145256Sjkoshy * Event handlers. 4124145256Sjkoshy */ 4125145256Sjkoshy 4126145256Sjkoshy/* 4127145256Sjkoshy * Handle a process exit. 4128145256Sjkoshy * 4129147191Sjkoshy * Remove this process from all hash tables. If this process 4130147191Sjkoshy * owned any PMCs, turn off those PMCs and deallocate them, 4131147191Sjkoshy * removing any associations with target processes. 4132147191Sjkoshy * 4133147191Sjkoshy * This function will be called by the last 'thread' of a 4134147191Sjkoshy * process. 4135147191Sjkoshy * 4136145256Sjkoshy * XXX This eventhandler gets called early in the exit process. 4137145256Sjkoshy * Consider using a 'hook' invocation from thread_exit() or equivalent 4138145256Sjkoshy * spot. Another negative is that kse_exit doesn't seem to call 4139145256Sjkoshy * exit1() [??]. 4140147191Sjkoshy * 4141145256Sjkoshy */ 4142145256Sjkoshy 4143145256Sjkoshystatic void 4144145256Sjkoshypmc_process_exit(void *arg __unused, struct proc *p) 4145145256Sjkoshy{ 4146184802Sjkoshy struct pmc *pm; 4147184802Sjkoshy int adjri, cpu; 4148184802Sjkoshy unsigned int ri; 4149145256Sjkoshy int is_using_hwpmcs; 4150184802Sjkoshy struct pmc_owner *po; 4151147191Sjkoshy struct pmc_process *pp; 4152184802Sjkoshy struct pmc_classdep *pcd; 4153147191Sjkoshy pmc_value_t newvalue, tmp; 4154145256Sjkoshy 4155145256Sjkoshy PROC_LOCK(p); 4156145256Sjkoshy is_using_hwpmcs = p->p_flag & P_HWPMC; 4157145256Sjkoshy PROC_UNLOCK(p); 4158145256Sjkoshy 4159147191Sjkoshy /* 4160147191Sjkoshy * Log a sysexit event to all SS PMC owners. 4161147191Sjkoshy */ 4162147191Sjkoshy LIST_FOREACH(po, &pmc_ss_owners, po_ssnext) 4163147191Sjkoshy if (po->po_flags & PMC_PO_OWNS_LOGFILE) 4164147191Sjkoshy pmclog_process_sysexit(po, p->p_pid); 4165145256Sjkoshy 4166147191Sjkoshy if (!is_using_hwpmcs) 4167147191Sjkoshy return; 4168147191Sjkoshy 4169147191Sjkoshy PMC_GET_SX_XLOCK(); 4170147191Sjkoshy PMCDBG(PRC,EXT,1,"process-exit proc=%p (%d, %s)", p, p->p_pid, 4171147191Sjkoshy p->p_comm); 4172147191Sjkoshy 4173147191Sjkoshy /* 4174147191Sjkoshy * Since this code is invoked by the last thread in an exiting 4175147191Sjkoshy * process, we would have context switched IN at some prior 4176147191Sjkoshy * point. However, with PREEMPTION, kernel mode context 4177147191Sjkoshy * switches may happen any time, so we want to disable a 4178147191Sjkoshy * context switch OUT till we get any PMCs targetting this 4179147191Sjkoshy * process off the hardware. 4180147191Sjkoshy * 4181147191Sjkoshy * We also need to atomically remove this process' 4182147191Sjkoshy * entry from our target process hash table, using 4183147191Sjkoshy * PMC_FLAG_REMOVE. 4184147191Sjkoshy */ 4185147191Sjkoshy PMCDBG(PRC,EXT,1, "process-exit proc=%p (%d, %s)", p, p->p_pid, 4186147191Sjkoshy p->p_comm); 4187147191Sjkoshy 4188147191Sjkoshy critical_enter(); /* no preemption */ 4189147191Sjkoshy 4190147191Sjkoshy cpu = curthread->td_oncpu; 4191147191Sjkoshy 4192147191Sjkoshy if ((pp = pmc_find_process_descriptor(p, 4193147191Sjkoshy PMC_FLAG_REMOVE)) != NULL) { 4194147191Sjkoshy 4195147191Sjkoshy PMCDBG(PRC,EXT,2, 4196147191Sjkoshy "process-exit proc=%p pmc-process=%p", p, pp); 4197147191Sjkoshy 4198147191Sjkoshy /* 4199147191Sjkoshy * The exiting process could the target of 4200147191Sjkoshy * some PMCs which will be running on 4201147191Sjkoshy * currently executing CPU. 4202147191Sjkoshy * 4203147191Sjkoshy * We need to turn these PMCs off like we 4204147191Sjkoshy * would do at context switch OUT time. 4205147191Sjkoshy */ 4206147191Sjkoshy for (ri = 0; ri < md->pmd_npmc; ri++) { 4207147191Sjkoshy 4208147191Sjkoshy /* 4209147191Sjkoshy * Pick up the pmc pointer from hardware 4210147191Sjkoshy * state similar to the CSW_OUT code. 4211147191Sjkoshy */ 4212147191Sjkoshy pm = NULL; 4213147191Sjkoshy 4214184802Sjkoshy pcd = pmc_ri_to_classdep(md, ri, &adjri); 4215184802Sjkoshy 4216184802Sjkoshy (void) (*pcd->pcd_get_config)(cpu, adjri, &pm); 4217184802Sjkoshy 4218147191Sjkoshy PMCDBG(PRC,EXT,2, "ri=%d pm=%p", ri, pm); 4219147191Sjkoshy 4220147191Sjkoshy if (pm == NULL || 4221147191Sjkoshy !PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm))) 4222147191Sjkoshy continue; 4223147191Sjkoshy 4224147191Sjkoshy PMCDBG(PRC,EXT,2, "ppmcs[%d]=%p pm=%p " 4225147191Sjkoshy "state=%d", ri, pp->pp_pmcs[ri].pp_pmc, 4226147191Sjkoshy pm, pm->pm_state); 4227147191Sjkoshy 4228147191Sjkoshy KASSERT(PMC_TO_ROWINDEX(pm) == ri, 4229147191Sjkoshy ("[pmc,%d] ri mismatch pmc(%d) ri(%d)", 4230147191Sjkoshy __LINE__, PMC_TO_ROWINDEX(pm), ri)); 4231147191Sjkoshy 4232147191Sjkoshy KASSERT(pm == pp->pp_pmcs[ri].pp_pmc, 4233147191Sjkoshy ("[pmc,%d] pm %p != pp_pmcs[%d] %p", 4234147191Sjkoshy __LINE__, pm, ri, pp->pp_pmcs[ri].pp_pmc)); 4235147191Sjkoshy 4236184802Sjkoshy (void) pcd->pcd_stop_pmc(cpu, adjri); 4237147191Sjkoshy 4238147191Sjkoshy KASSERT(pm->pm_runcount > 0, 4239147191Sjkoshy ("[pmc,%d] bad runcount ri %d rc %d", 4240147191Sjkoshy __LINE__, ri, pm->pm_runcount)); 4241147191Sjkoshy 4242147867Sjkoshy /* Stop hardware only if it is actually running */ 4243147191Sjkoshy if (pm->pm_state == PMC_STATE_RUNNING && 4244147867Sjkoshy pm->pm_stalled == 0) { 4245184802Sjkoshy pcd->pcd_read_pmc(cpu, adjri, &newvalue); 4246147191Sjkoshy tmp = newvalue - 4247147191Sjkoshy PMC_PCPU_SAVED(cpu,ri); 4248147191Sjkoshy 4249147191Sjkoshy mtx_pool_lock_spin(pmc_mtxpool, pm); 4250147191Sjkoshy pm->pm_gv.pm_savedvalue += tmp; 4251147191Sjkoshy pp->pp_pmcs[ri].pp_pmcval += tmp; 4252147191Sjkoshy mtx_pool_unlock_spin(pmc_mtxpool, pm); 4253147191Sjkoshy } 4254147191Sjkoshy 4255147191Sjkoshy atomic_subtract_rel_32(&pm->pm_runcount,1); 4256147191Sjkoshy 4257147191Sjkoshy KASSERT((int) pm->pm_runcount >= 0, 4258147191Sjkoshy ("[pmc,%d] runcount is %d", __LINE__, ri)); 4259147191Sjkoshy 4260184802Sjkoshy (void) pcd->pcd_config_pmc(cpu, adjri, NULL); 4261147191Sjkoshy } 4262147191Sjkoshy 4263147191Sjkoshy /* 4264147191Sjkoshy * Inform the MD layer of this pseudo "context switch 4265147191Sjkoshy * out" 4266147191Sjkoshy */ 4267147191Sjkoshy (void) md->pmd_switch_out(pmc_pcpu[cpu], pp); 4268147191Sjkoshy 4269147191Sjkoshy critical_exit(); /* ok to be pre-empted now */ 4270147191Sjkoshy 4271147191Sjkoshy /* 4272147191Sjkoshy * Unlink this process from the PMCs that are 4273147191Sjkoshy * targetting it. This will send a signal to 4274147191Sjkoshy * all PMC owner's whose PMCs are orphaned. 4275147191Sjkoshy * 4276147191Sjkoshy * Log PMC value at exit time if requested. 4277147191Sjkoshy */ 4278147191Sjkoshy for (ri = 0; ri < md->pmd_npmc; ri++) 4279147191Sjkoshy if ((pm = pp->pp_pmcs[ri].pp_pmc) != NULL) { 4280147867Sjkoshy if (pm->pm_flags & PMC_F_NEEDS_LOGFILE && 4281147867Sjkoshy PMC_IS_COUNTING_MODE(PMC_TO_MODE(pm))) 4282147191Sjkoshy pmclog_process_procexit(pm, pp); 4283147191Sjkoshy pmc_unlink_target_process(pm, pp); 4284147191Sjkoshy } 4285184205Sdes free(pp, M_PMC); 4286147191Sjkoshy 4287147191Sjkoshy } else 4288147191Sjkoshy critical_exit(); /* pp == NULL */ 4289147191Sjkoshy 4290147191Sjkoshy 4291147191Sjkoshy /* 4292147191Sjkoshy * If the process owned PMCs, free them up and free up 4293147191Sjkoshy * memory. 4294147191Sjkoshy */ 4295147191Sjkoshy if ((po = pmc_find_owner_descriptor(p)) != NULL) { 4296147191Sjkoshy pmc_remove_owner(po); 4297147191Sjkoshy pmc_destroy_owner_descriptor(po); 4298145256Sjkoshy } 4299147191Sjkoshy 4300147191Sjkoshy sx_xunlock(&pmc_sx); 4301145256Sjkoshy} 4302145256Sjkoshy 4303145256Sjkoshy/* 4304145256Sjkoshy * Handle a process fork. 4305145256Sjkoshy * 4306145256Sjkoshy * If the parent process 'p1' is under HWPMC monitoring, then copy 4307145256Sjkoshy * over any attached PMCs that have 'do_descendants' semantics. 4308145256Sjkoshy */ 4309145256Sjkoshy 4310145256Sjkoshystatic void 4311147191Sjkoshypmc_process_fork(void *arg __unused, struct proc *p1, struct proc *newproc, 4312145256Sjkoshy int flags) 4313145256Sjkoshy{ 4314145256Sjkoshy int is_using_hwpmcs; 4315147191Sjkoshy unsigned int ri; 4316147191Sjkoshy uint32_t do_descendants; 4317147191Sjkoshy struct pmc *pm; 4318147191Sjkoshy struct pmc_owner *po; 4319147191Sjkoshy struct pmc_process *ppnew, *ppold; 4320145256Sjkoshy 4321145256Sjkoshy (void) flags; /* unused parameter */ 4322145256Sjkoshy 4323145256Sjkoshy PROC_LOCK(p1); 4324145256Sjkoshy is_using_hwpmcs = p1->p_flag & P_HWPMC; 4325145256Sjkoshy PROC_UNLOCK(p1); 4326145256Sjkoshy 4327147191Sjkoshy /* 4328147191Sjkoshy * If there are system-wide sampling PMCs active, we need to 4329147191Sjkoshy * log all fork events to their owner's logs. 4330147191Sjkoshy */ 4331147191Sjkoshy 4332147191Sjkoshy LIST_FOREACH(po, &pmc_ss_owners, po_ssnext) 4333147191Sjkoshy if (po->po_flags & PMC_PO_OWNS_LOGFILE) 4334147191Sjkoshy pmclog_process_procfork(po, p1->p_pid, newproc->p_pid); 4335147191Sjkoshy 4336147191Sjkoshy if (!is_using_hwpmcs) 4337147191Sjkoshy return; 4338147191Sjkoshy 4339147191Sjkoshy PMC_GET_SX_XLOCK(); 4340147191Sjkoshy PMCDBG(PMC,FRK,1, "process-fork proc=%p (%d, %s) -> %p", p1, 4341147191Sjkoshy p1->p_pid, p1->p_comm, newproc); 4342147191Sjkoshy 4343147191Sjkoshy /* 4344147191Sjkoshy * If the parent process (curthread->td_proc) is a 4345147191Sjkoshy * target of any PMCs, look for PMCs that are to be 4346147191Sjkoshy * inherited, and link these into the new process 4347147191Sjkoshy * descriptor. 4348147191Sjkoshy */ 4349147191Sjkoshy if ((ppold = pmc_find_process_descriptor(curthread->td_proc, 4350147191Sjkoshy PMC_FLAG_NONE)) == NULL) 4351147191Sjkoshy goto done; /* nothing to do */ 4352147191Sjkoshy 4353147191Sjkoshy do_descendants = 0; 4354147191Sjkoshy for (ri = 0; ri < md->pmd_npmc; ri++) 4355147191Sjkoshy if ((pm = ppold->pp_pmcs[ri].pp_pmc) != NULL) 4356147191Sjkoshy do_descendants |= pm->pm_flags & PMC_F_DESCENDANTS; 4357147191Sjkoshy if (do_descendants == 0) /* nothing to do */ 4358147191Sjkoshy goto done; 4359147191Sjkoshy 4360147191Sjkoshy /* allocate a descriptor for the new process */ 4361147191Sjkoshy if ((ppnew = pmc_find_process_descriptor(newproc, 4362147191Sjkoshy PMC_FLAG_ALLOCATE)) == NULL) 4363147191Sjkoshy goto done; 4364147191Sjkoshy 4365147191Sjkoshy /* 4366147191Sjkoshy * Run through all PMCs that were targeting the old process 4367147191Sjkoshy * and which specified F_DESCENDANTS and attach them to the 4368147191Sjkoshy * new process. 4369147191Sjkoshy * 4370147191Sjkoshy * Log the fork event to all owners of PMCs attached to this 4371147191Sjkoshy * process, if not already logged. 4372147191Sjkoshy */ 4373147191Sjkoshy for (ri = 0; ri < md->pmd_npmc; ri++) 4374147191Sjkoshy if ((pm = ppold->pp_pmcs[ri].pp_pmc) != NULL && 4375147191Sjkoshy (pm->pm_flags & PMC_F_DESCENDANTS)) { 4376147191Sjkoshy pmc_link_target_process(pm, ppnew); 4377147191Sjkoshy po = pm->pm_owner; 4378147191Sjkoshy if (po->po_sscount == 0 && 4379147191Sjkoshy po->po_flags & PMC_PO_OWNS_LOGFILE) 4380147191Sjkoshy pmclog_process_procfork(po, p1->p_pid, 4381147191Sjkoshy newproc->p_pid); 4382147191Sjkoshy } 4383147191Sjkoshy 4384147191Sjkoshy /* 4385147191Sjkoshy * Now mark the new process as being tracked by this driver. 4386147191Sjkoshy */ 4387147191Sjkoshy PROC_LOCK(newproc); 4388147191Sjkoshy newproc->p_flag |= P_HWPMC; 4389147191Sjkoshy PROC_UNLOCK(newproc); 4390147191Sjkoshy 4391147191Sjkoshy done: 4392147191Sjkoshy sx_xunlock(&pmc_sx); 4393145256Sjkoshy} 4394145256Sjkoshy 4395145256Sjkoshy 4396145256Sjkoshy/* 4397145256Sjkoshy * initialization 4398145256Sjkoshy */ 4399145256Sjkoshy 4400145256Sjkoshystatic const char *pmc_name_of_pmcclass[] = { 4401145256Sjkoshy#undef __PMC_CLASS 4402145256Sjkoshy#define __PMC_CLASS(N) #N , 4403145256Sjkoshy __PMC_CLASSES() 4404145256Sjkoshy}; 4405145256Sjkoshy 4406145256Sjkoshystatic int 4407145256Sjkoshypmc_initialize(void) 4408145256Sjkoshy{ 4409184802Sjkoshy int c, cpu, error, n, ri; 4410183266Sjkoshy unsigned int maxcpu; 4411145256Sjkoshy struct pmc_binding pb; 4412174395Sjkoshy struct pmc_sample *ps; 4413184802Sjkoshy struct pmc_classdep *pcd; 4414147191Sjkoshy struct pmc_samplebuffer *sb; 4415145256Sjkoshy 4416145256Sjkoshy md = NULL; 4417145256Sjkoshy error = 0; 4418145256Sjkoshy 4419153110Sru#ifdef DEBUG 4420145256Sjkoshy /* parse debug flags first */ 4421145256Sjkoshy if (TUNABLE_STR_FETCH(PMC_SYSCTL_NAME_PREFIX "debugflags", 4422145256Sjkoshy pmc_debugstr, sizeof(pmc_debugstr))) 4423145256Sjkoshy pmc_debugflags_parse(pmc_debugstr, 4424145256Sjkoshy pmc_debugstr+strlen(pmc_debugstr)); 4425145256Sjkoshy#endif 4426145256Sjkoshy 4427145256Sjkoshy PMCDBG(MOD,INI,0, "PMC Initialize (version %x)", PMC_VERSION); 4428145256Sjkoshy 4429148562Sjkoshy /* check kernel version */ 4430148562Sjkoshy if (pmc_kernel_version != PMC_VERSION) { 4431148562Sjkoshy if (pmc_kernel_version == 0) 4432148562Sjkoshy printf("hwpmc: this kernel has not been compiled with " 4433148562Sjkoshy "'options HWPMC_HOOKS'.\n"); 4434148562Sjkoshy else 4435148562Sjkoshy printf("hwpmc: kernel version (0x%x) does not match " 4436148562Sjkoshy "module version (0x%x).\n", pmc_kernel_version, 4437148562Sjkoshy PMC_VERSION); 4438148562Sjkoshy return EPROGMISMATCH; 4439148562Sjkoshy } 4440148562Sjkoshy 4441145256Sjkoshy /* 4442145256Sjkoshy * check sysctl parameters 4443145256Sjkoshy */ 4444145256Sjkoshy 4445145256Sjkoshy if (pmc_hashsize <= 0) { 4446174395Sjkoshy (void) printf("hwpmc: tunable \"hashsize\"=%d must be " 4447174395Sjkoshy "greater than zero.\n", pmc_hashsize); 4448145256Sjkoshy pmc_hashsize = PMC_HASH_SIZE; 4449145256Sjkoshy } 4450145256Sjkoshy 4451147191Sjkoshy if (pmc_nsamples <= 0 || pmc_nsamples > 65535) { 4452174395Sjkoshy (void) printf("hwpmc: tunable \"nsamples\"=%d out of " 4453174395Sjkoshy "range.\n", pmc_nsamples); 4454147191Sjkoshy pmc_nsamples = PMC_NSAMPLES; 4455147191Sjkoshy } 4456145256Sjkoshy 4457174395Sjkoshy if (pmc_callchaindepth <= 0 || 4458174395Sjkoshy pmc_callchaindepth > PMC_CALLCHAIN_DEPTH_MAX) { 4459174395Sjkoshy (void) printf("hwpmc: tunable \"callchaindepth\"=%d out of " 4460174395Sjkoshy "range.\n", pmc_callchaindepth); 4461174395Sjkoshy pmc_callchaindepth = PMC_CALLCHAIN_DEPTH; 4462174395Sjkoshy } 4463174395Sjkoshy 4464147191Sjkoshy md = pmc_md_initialize(); 4465147191Sjkoshy 4466184802Sjkoshy if (md == NULL) 4467184802Sjkoshy return (ENOSYS); 4468145256Sjkoshy 4469184802Sjkoshy KASSERT(md->pmd_nclass >= 1 && md->pmd_npmc >= 1, 4470184802Sjkoshy ("[pmc,%d] no classes or pmcs", __LINE__)); 4471184802Sjkoshy 4472184802Sjkoshy /* Compute the map from row-indices to classdep pointers. */ 4473184802Sjkoshy pmc_rowindex_to_classdep = malloc(sizeof(struct pmc_classdep *) * 4474184802Sjkoshy md->pmd_npmc, M_PMC, M_WAITOK|M_ZERO); 4475184802Sjkoshy 4476184802Sjkoshy for (n = 0; n < md->pmd_npmc; n++) 4477184802Sjkoshy pmc_rowindex_to_classdep[n] = NULL; 4478184802Sjkoshy for (ri = c = 0; c < md->pmd_nclass; c++) { 4479184802Sjkoshy pcd = &md->pmd_classdep[c]; 4480184802Sjkoshy for (n = 0; n < pcd->pcd_num; n++, ri++) 4481184802Sjkoshy pmc_rowindex_to_classdep[ri] = pcd; 4482184802Sjkoshy } 4483184802Sjkoshy 4484184802Sjkoshy KASSERT(ri == md->pmd_npmc, 4485184802Sjkoshy ("[pmc,%d] npmc miscomputed: ri=%d, md->npmc=%d", __LINE__, 4486184802Sjkoshy ri, md->pmd_npmc)); 4487184802Sjkoshy 4488183266Sjkoshy maxcpu = pmc_cpu_max(); 4489183266Sjkoshy 4490145256Sjkoshy /* allocate space for the per-cpu array */ 4491184802Sjkoshy pmc_pcpu = malloc(maxcpu * sizeof(struct pmc_cpu *), M_PMC, 4492184802Sjkoshy M_WAITOK|M_ZERO); 4493145256Sjkoshy 4494145256Sjkoshy /* per-cpu 'saved values' for managing process-mode PMCs */ 4495184214Sdes pmc_pcpu_saved = malloc(sizeof(pmc_value_t) * maxcpu * md->pmd_npmc, 4496184214Sdes M_PMC, M_WAITOK); 4497145256Sjkoshy 4498183266Sjkoshy /* Perform CPU-dependent initialization. */ 4499145256Sjkoshy pmc_save_cpu_binding(&pb); 4500184802Sjkoshy error = 0; 4501184802Sjkoshy for (cpu = 0; error == 0 && cpu < maxcpu; cpu++) { 4502183266Sjkoshy if (!pmc_cpu_is_active(cpu)) 4503145256Sjkoshy continue; 4504145256Sjkoshy pmc_select_cpu(cpu); 4505184802Sjkoshy pmc_pcpu[cpu] = malloc(sizeof(struct pmc_cpu) + 4506184802Sjkoshy md->pmd_npmc * sizeof(struct pmc_hw *), M_PMC, 4507184802Sjkoshy M_WAITOK|M_ZERO); 4508184802Sjkoshy if (md->pmd_pcpu_init) 4509185363Sjkoshy error = md->pmd_pcpu_init(md, cpu); 4510184802Sjkoshy for (n = 0; error == 0 && n < md->pmd_nclass; n++) 4511184802Sjkoshy error = md->pmd_classdep[n].pcd_pcpu_init(md, cpu); 4512145256Sjkoshy } 4513145256Sjkoshy pmc_restore_cpu_binding(&pb); 4514145256Sjkoshy 4515184802Sjkoshy if (error) 4516184802Sjkoshy return (error); 4517145256Sjkoshy 4518147191Sjkoshy /* allocate space for the sample array */ 4519183266Sjkoshy for (cpu = 0; cpu < maxcpu; cpu++) { 4520183266Sjkoshy if (!pmc_cpu_is_active(cpu)) 4521147191Sjkoshy continue; 4522184802Sjkoshy 4523184214Sdes sb = malloc(sizeof(struct pmc_samplebuffer) + 4524147191Sjkoshy pmc_nsamples * sizeof(struct pmc_sample), M_PMC, 4525147191Sjkoshy M_WAITOK|M_ZERO); 4526147191Sjkoshy sb->ps_read = sb->ps_write = sb->ps_samples; 4527153735Sjkoshy sb->ps_fence = sb->ps_samples + pmc_nsamples; 4528184802Sjkoshy 4529147191Sjkoshy KASSERT(pmc_pcpu[cpu] != NULL, 4530147191Sjkoshy ("[pmc,%d] cpu=%d Null per-cpu data", __LINE__, cpu)); 4531147191Sjkoshy 4532184802Sjkoshy sb->ps_callchains = malloc(pmc_callchaindepth * pmc_nsamples * 4533184802Sjkoshy sizeof(uintptr_t), M_PMC, M_WAITOK|M_ZERO); 4534174395Sjkoshy 4535174395Sjkoshy for (n = 0, ps = sb->ps_samples; n < pmc_nsamples; n++, ps++) 4536174395Sjkoshy ps->ps_pc = sb->ps_callchains + 4537174395Sjkoshy (n * pmc_callchaindepth); 4538174395Sjkoshy 4539147191Sjkoshy pmc_pcpu[cpu]->pc_sb = sb; 4540147191Sjkoshy } 4541147191Sjkoshy 4542145256Sjkoshy /* allocate space for the row disposition array */ 4543145256Sjkoshy pmc_pmcdisp = malloc(sizeof(enum pmc_mode) * md->pmd_npmc, 4544145256Sjkoshy M_PMC, M_WAITOK|M_ZERO); 4545145256Sjkoshy 4546145256Sjkoshy KASSERT(pmc_pmcdisp != NULL, 4547145256Sjkoshy ("[pmc,%d] pmcdisp allocation returned NULL", __LINE__)); 4548145256Sjkoshy 4549145256Sjkoshy /* mark all PMCs as available */ 4550145256Sjkoshy for (n = 0; n < (int) md->pmd_npmc; n++) 4551145256Sjkoshy PMC_MARK_ROW_FREE(n); 4552145256Sjkoshy 4553145256Sjkoshy /* allocate thread hash tables */ 4554145256Sjkoshy pmc_ownerhash = hashinit(pmc_hashsize, M_PMC, 4555145256Sjkoshy &pmc_ownerhashmask); 4556145256Sjkoshy 4557145256Sjkoshy pmc_processhash = hashinit(pmc_hashsize, M_PMC, 4558145256Sjkoshy &pmc_processhashmask); 4559168856Sjkoshy mtx_init(&pmc_processhash_mtx, "pmc-process-hash", "pmc-leaf", 4560168856Sjkoshy MTX_SPIN); 4561145256Sjkoshy 4562147191Sjkoshy LIST_INIT(&pmc_ss_owners); 4563147191Sjkoshy pmc_ss_count = 0; 4564147191Sjkoshy 4565145256Sjkoshy /* allocate a pool of spin mutexes */ 4566168856Sjkoshy pmc_mtxpool = mtx_pool_create("pmc-leaf", pmc_mtxpool_size, 4567168856Sjkoshy MTX_SPIN); 4568145256Sjkoshy 4569145256Sjkoshy PMCDBG(MOD,INI,1, "pmc_ownerhash=%p, mask=0x%lx " 4570145256Sjkoshy "targethash=%p mask=0x%lx", pmc_ownerhash, pmc_ownerhashmask, 4571145256Sjkoshy pmc_processhash, pmc_processhashmask); 4572145256Sjkoshy 4573145256Sjkoshy /* register process {exit,fork,exec} handlers */ 4574145256Sjkoshy pmc_exit_tag = EVENTHANDLER_REGISTER(process_exit, 4575145256Sjkoshy pmc_process_exit, NULL, EVENTHANDLER_PRI_ANY); 4576145256Sjkoshy pmc_fork_tag = EVENTHANDLER_REGISTER(process_fork, 4577145256Sjkoshy pmc_process_fork, NULL, EVENTHANDLER_PRI_ANY); 4578145256Sjkoshy 4579147191Sjkoshy /* initialize logging */ 4580147191Sjkoshy pmclog_initialize(); 4581147191Sjkoshy 4582145256Sjkoshy /* set hook functions */ 4583145256Sjkoshy pmc_intr = md->pmd_intr; 4584145256Sjkoshy pmc_hook = pmc_hook_handler; 4585145256Sjkoshy 4586145256Sjkoshy if (error == 0) { 4587145256Sjkoshy printf(PMC_MODULE_NAME ":"); 4588149373Sjkoshy for (n = 0; n < (int) md->pmd_nclass; n++) { 4589184802Sjkoshy pcd = &md->pmd_classdep[n]; 4590184997Sjkoshy printf(" %s/%d/%d/0x%b", 4591184802Sjkoshy pmc_name_of_pmcclass[pcd->pcd_class], 4592184802Sjkoshy pcd->pcd_num, 4593184997Sjkoshy pcd->pcd_width, 4594184802Sjkoshy pcd->pcd_caps, 4595149373Sjkoshy "\20" 4596149373Sjkoshy "\1INT\2USR\3SYS\4EDG\5THR" 4597149373Sjkoshy "\6REA\7WRI\10INV\11QUA\12PRC" 4598149373Sjkoshy "\13TAG\14CSC"); 4599149373Sjkoshy } 4600145256Sjkoshy printf("\n"); 4601145256Sjkoshy } 4602145256Sjkoshy 4603184802Sjkoshy return (error); 4604145256Sjkoshy} 4605145256Sjkoshy 4606145256Sjkoshy/* prepare to be unloaded */ 4607145256Sjkoshystatic void 4608145256Sjkoshypmc_cleanup(void) 4609145256Sjkoshy{ 4610184802Sjkoshy int c, cpu; 4611183266Sjkoshy unsigned int maxcpu; 4612145256Sjkoshy struct pmc_ownerhash *ph; 4613145256Sjkoshy struct pmc_owner *po, *tmp; 4614145256Sjkoshy struct pmc_binding pb; 4615153110Sru#ifdef DEBUG 4616145256Sjkoshy struct pmc_processhash *prh; 4617145256Sjkoshy#endif 4618145256Sjkoshy 4619145256Sjkoshy PMCDBG(MOD,INI,0, "%s", "cleanup"); 4620145256Sjkoshy 4621147191Sjkoshy /* switch off sampling */ 4622147191Sjkoshy atomic_store_rel_int(&pmc_cpumask, 0); 4623147191Sjkoshy pmc_intr = NULL; 4624145256Sjkoshy 4625145256Sjkoshy sx_xlock(&pmc_sx); 4626145256Sjkoshy if (pmc_hook == NULL) { /* being unloaded already */ 4627145256Sjkoshy sx_xunlock(&pmc_sx); 4628145256Sjkoshy return; 4629145256Sjkoshy } 4630145256Sjkoshy 4631145256Sjkoshy pmc_hook = NULL; /* prevent new threads from entering module */ 4632145256Sjkoshy 4633145256Sjkoshy /* deregister event handlers */ 4634145256Sjkoshy EVENTHANDLER_DEREGISTER(process_fork, pmc_fork_tag); 4635145256Sjkoshy EVENTHANDLER_DEREGISTER(process_exit, pmc_exit_tag); 4636145256Sjkoshy 4637145256Sjkoshy /* send SIGBUS to all owner threads, free up allocations */ 4638145256Sjkoshy if (pmc_ownerhash) 4639145256Sjkoshy for (ph = pmc_ownerhash; 4640145256Sjkoshy ph <= &pmc_ownerhash[pmc_ownerhashmask]; 4641145256Sjkoshy ph++) { 4642145256Sjkoshy LIST_FOREACH_SAFE(po, ph, po_next, tmp) { 4643145256Sjkoshy pmc_remove_owner(po); 4644145256Sjkoshy 4645145256Sjkoshy /* send SIGBUS to owner processes */ 4646145256Sjkoshy PMCDBG(MOD,INI,2, "cleanup signal proc=%p " 4647145256Sjkoshy "(%d, %s)", po->po_owner, 4648145256Sjkoshy po->po_owner->p_pid, 4649145256Sjkoshy po->po_owner->p_comm); 4650145256Sjkoshy 4651145256Sjkoshy PROC_LOCK(po->po_owner); 4652145256Sjkoshy psignal(po->po_owner, SIGBUS); 4653145256Sjkoshy PROC_UNLOCK(po->po_owner); 4654147191Sjkoshy 4655147191Sjkoshy pmc_destroy_owner_descriptor(po); 4656145256Sjkoshy } 4657145256Sjkoshy } 4658145256Sjkoshy 4659145256Sjkoshy /* reclaim allocated data structures */ 4660145256Sjkoshy if (pmc_mtxpool) 4661145256Sjkoshy mtx_pool_destroy(&pmc_mtxpool); 4662145256Sjkoshy 4663145256Sjkoshy mtx_destroy(&pmc_processhash_mtx); 4664145256Sjkoshy if (pmc_processhash) { 4665153110Sru#ifdef DEBUG 4666145256Sjkoshy struct pmc_process *pp; 4667145256Sjkoshy 4668145256Sjkoshy PMCDBG(MOD,INI,3, "%s", "destroy process hash"); 4669145256Sjkoshy for (prh = pmc_processhash; 4670145256Sjkoshy prh <= &pmc_processhash[pmc_processhashmask]; 4671145256Sjkoshy prh++) 4672145256Sjkoshy LIST_FOREACH(pp, prh, pp_next) 4673145256Sjkoshy PMCDBG(MOD,INI,3, "pid=%d", pp->pp_proc->p_pid); 4674145256Sjkoshy#endif 4675145256Sjkoshy 4676145256Sjkoshy hashdestroy(pmc_processhash, M_PMC, pmc_processhashmask); 4677145256Sjkoshy pmc_processhash = NULL; 4678145256Sjkoshy } 4679145256Sjkoshy 4680145256Sjkoshy if (pmc_ownerhash) { 4681145256Sjkoshy PMCDBG(MOD,INI,3, "%s", "destroy owner hash"); 4682145256Sjkoshy hashdestroy(pmc_ownerhash, M_PMC, pmc_ownerhashmask); 4683145256Sjkoshy pmc_ownerhash = NULL; 4684145256Sjkoshy } 4685145256Sjkoshy 4686147191Sjkoshy KASSERT(LIST_EMPTY(&pmc_ss_owners), 4687147191Sjkoshy ("[pmc,%d] Global SS owner list not empty", __LINE__)); 4688147191Sjkoshy KASSERT(pmc_ss_count == 0, 4689147191Sjkoshy ("[pmc,%d] Global SS count not empty", __LINE__)); 4690147191Sjkoshy 4691184802Sjkoshy /* do processor and pmc-class dependent cleanup */ 4692183266Sjkoshy maxcpu = pmc_cpu_max(); 4693153735Sjkoshy 4694145256Sjkoshy PMCDBG(MOD,INI,3, "%s", "md cleanup"); 4695145256Sjkoshy if (md) { 4696145256Sjkoshy pmc_save_cpu_binding(&pb); 4697183266Sjkoshy for (cpu = 0; cpu < maxcpu; cpu++) { 4698145256Sjkoshy PMCDBG(MOD,INI,1,"pmc-cleanup cpu=%d pcs=%p", 4699145256Sjkoshy cpu, pmc_pcpu[cpu]); 4700183266Sjkoshy if (!pmc_cpu_is_active(cpu) || pmc_pcpu[cpu] == NULL) 4701145256Sjkoshy continue; 4702145256Sjkoshy pmc_select_cpu(cpu); 4703184802Sjkoshy for (c = 0; c < md->pmd_nclass; c++) 4704184802Sjkoshy md->pmd_classdep[c].pcd_pcpu_fini(md, cpu); 4705184802Sjkoshy if (md->pmd_pcpu_fini) 4706185363Sjkoshy md->pmd_pcpu_fini(md, cpu); 4707145256Sjkoshy } 4708184994Sjkoshy 4709184994Sjkoshy pmc_md_finalize(md); 4710184994Sjkoshy 4711184205Sdes free(md, M_PMC); 4712145256Sjkoshy md = NULL; 4713145256Sjkoshy pmc_restore_cpu_binding(&pb); 4714145256Sjkoshy } 4715145256Sjkoshy 4716184802Sjkoshy /* Free per-cpu descriptors. */ 4717184802Sjkoshy for (cpu = 0; cpu < maxcpu; cpu++) { 4718184802Sjkoshy if (!pmc_cpu_is_active(cpu)) 4719184802Sjkoshy continue; 4720184802Sjkoshy KASSERT(pmc_pcpu[cpu]->pc_sb != NULL, 4721184802Sjkoshy ("[pmc,%d] Null cpu sample buffer cpu=%d", __LINE__, 4722184802Sjkoshy cpu)); 4723184802Sjkoshy free(pmc_pcpu[cpu]->pc_sb->ps_callchains, M_PMC); 4724184802Sjkoshy free(pmc_pcpu[cpu]->pc_sb, M_PMC); 4725184802Sjkoshy free(pmc_pcpu[cpu], M_PMC); 4726184802Sjkoshy } 4727184802Sjkoshy 4728184205Sdes free(pmc_pcpu, M_PMC); 4729145256Sjkoshy pmc_pcpu = NULL; 4730145256Sjkoshy 4731184205Sdes free(pmc_pcpu_saved, M_PMC); 4732145256Sjkoshy pmc_pcpu_saved = NULL; 4733145256Sjkoshy 4734145256Sjkoshy if (pmc_pmcdisp) { 4735184205Sdes free(pmc_pmcdisp, M_PMC); 4736145256Sjkoshy pmc_pmcdisp = NULL; 4737145256Sjkoshy } 4738145256Sjkoshy 4739184802Sjkoshy if (pmc_rowindex_to_classdep) { 4740184802Sjkoshy free(pmc_rowindex_to_classdep, M_PMC); 4741184802Sjkoshy pmc_rowindex_to_classdep = NULL; 4742184802Sjkoshy } 4743184802Sjkoshy 4744147191Sjkoshy pmclog_shutdown(); 4745147191Sjkoshy 4746145256Sjkoshy sx_xunlock(&pmc_sx); /* we are done */ 4747145256Sjkoshy} 4748145256Sjkoshy 4749145256Sjkoshy/* 4750145256Sjkoshy * The function called at load/unload. 4751145256Sjkoshy */ 4752145256Sjkoshy 4753145256Sjkoshystatic int 4754145256Sjkoshyload (struct module *module __unused, int cmd, void *arg __unused) 4755145256Sjkoshy{ 4756145256Sjkoshy int error; 4757145256Sjkoshy 4758145256Sjkoshy error = 0; 4759145256Sjkoshy 4760145256Sjkoshy switch (cmd) { 4761145256Sjkoshy case MOD_LOAD : 4762145256Sjkoshy /* initialize the subsystem */ 4763145256Sjkoshy error = pmc_initialize(); 4764145256Sjkoshy if (error != 0) 4765145256Sjkoshy break; 4766183266Sjkoshy PMCDBG(MOD,INI,1, "syscall=%d maxcpu=%d", 4767183266Sjkoshy pmc_syscall_num, pmc_cpu_max()); 4768145256Sjkoshy break; 4769145256Sjkoshy 4770145256Sjkoshy 4771145256Sjkoshy case MOD_UNLOAD : 4772145256Sjkoshy case MOD_SHUTDOWN: 4773145256Sjkoshy pmc_cleanup(); 4774145256Sjkoshy PMCDBG(MOD,INI,1, "%s", "unloaded"); 4775145256Sjkoshy break; 4776145256Sjkoshy 4777145256Sjkoshy default : 4778145256Sjkoshy error = EINVAL; /* XXX should panic(9) */ 4779145256Sjkoshy break; 4780145256Sjkoshy } 4781145256Sjkoshy 4782145256Sjkoshy return error; 4783145256Sjkoshy} 4784145256Sjkoshy 4785145256Sjkoshy/* memory pool */ 4786145256SjkoshyMALLOC_DEFINE(M_PMC, "pmc", "Memory space for the PMC module"); 4787