hwpmc_mod.c revision 145774
1145256Sjkoshy/*- 2145256Sjkoshy * Copyright (c) 2003-2005 Joseph Koshy 3145256Sjkoshy * All rights reserved. 4145256Sjkoshy * 5145256Sjkoshy * Redistribution and use in source and binary forms, with or without 6145256Sjkoshy * modification, are permitted provided that the following conditions 7145256Sjkoshy * are met: 8145256Sjkoshy * 1. Redistributions of source code must retain the above copyright 9145256Sjkoshy * notice, this list of conditions and the following disclaimer. 10145256Sjkoshy * 2. Redistributions in binary form must reproduce the above copyright 11145256Sjkoshy * notice, this list of conditions and the following disclaimer in the 12145256Sjkoshy * documentation and/or other materials provided with the distribution. 13145256Sjkoshy * 14145256Sjkoshy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15145256Sjkoshy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16145256Sjkoshy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17145256Sjkoshy * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18145256Sjkoshy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19145256Sjkoshy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20145256Sjkoshy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21145256Sjkoshy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22145256Sjkoshy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23145256Sjkoshy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24145256Sjkoshy * SUCH DAMAGE. 25145256Sjkoshy * 26145256Sjkoshy */ 27145256Sjkoshy 28145256Sjkoshy#include <sys/cdefs.h> 29145256Sjkoshy__FBSDID("$FreeBSD: head/sys/dev/hwpmc/hwpmc_mod.c 145774 2005-05-01 14:11:49Z jkoshy $"); 30145256Sjkoshy 31145256Sjkoshy#include <sys/param.h> 32145256Sjkoshy#include <sys/eventhandler.h> 33145256Sjkoshy#include <sys/jail.h> 34145256Sjkoshy#include <sys/kernel.h> 35145256Sjkoshy#include <sys/limits.h> 36145256Sjkoshy#include <sys/lock.h> 37145256Sjkoshy#include <sys/malloc.h> 38145256Sjkoshy#include <sys/module.h> 39145256Sjkoshy#include <sys/mutex.h> 40145256Sjkoshy#include <sys/pmc.h> 41145256Sjkoshy#include <sys/pmckern.h> 42145256Sjkoshy#include <sys/proc.h> 43145256Sjkoshy#include <sys/queue.h> 44145256Sjkoshy#include <sys/sched.h> 45145256Sjkoshy#include <sys/signalvar.h> 46145256Sjkoshy#include <sys/smp.h> 47145256Sjkoshy#include <sys/sx.h> 48145256Sjkoshy#include <sys/sysctl.h> 49145256Sjkoshy#include <sys/sysent.h> 50145256Sjkoshy#include <sys/systm.h> 51145256Sjkoshy 52145256Sjkoshy#include <machine/md_var.h> 53145256Sjkoshy 54145256Sjkoshy/* 55145256Sjkoshy * Types 56145256Sjkoshy */ 57145256Sjkoshy 58145256Sjkoshyenum pmc_flags { 59145256Sjkoshy PMC_FLAG_NONE = 0x00, /* do nothing */ 60145256Sjkoshy PMC_FLAG_REMOVE = 0x01, /* atomically remove entry from hash */ 61145256Sjkoshy PMC_FLAG_ALLOCATE = 0x02, /* add entry to hash if not found */ 62145256Sjkoshy}; 63145256Sjkoshy 64145256Sjkoshy/* 65145256Sjkoshy * The offset in sysent where the syscall is allocated. 66145256Sjkoshy */ 67145256Sjkoshy 68145256Sjkoshystatic int pmc_syscall_num = NO_SYSCALL; 69145256Sjkoshystruct pmc_cpu **pmc_pcpu; /* per-cpu state */ 70145256Sjkoshypmc_value_t *pmc_pcpu_saved; /* saved PMC values: CSW handling */ 71145256Sjkoshy 72145256Sjkoshy#define PMC_PCPU_SAVED(C,R) pmc_pcpu_saved[(R) + md->pmd_npmc*(C)] 73145256Sjkoshy 74145256Sjkoshystruct mtx_pool *pmc_mtxpool; 75145256Sjkoshystatic int *pmc_pmcdisp; /* PMC row dispositions */ 76145256Sjkoshy 77145256Sjkoshy#define PMC_ROW_DISP_IS_FREE(R) (pmc_pmcdisp[(R)] == 0) 78145256Sjkoshy#define PMC_ROW_DISP_IS_THREAD(R) (pmc_pmcdisp[(R)] > 0) 79145256Sjkoshy#define PMC_ROW_DISP_IS_STANDALONE(R) (pmc_pmcdisp[(R)] < 0) 80145256Sjkoshy 81145256Sjkoshy#define PMC_MARK_ROW_FREE(R) do { \ 82145256Sjkoshy pmc_pmcdisp[(R)] = 0; \ 83145256Sjkoshy} while (0) 84145256Sjkoshy 85145256Sjkoshy#define PMC_MARK_ROW_STANDALONE(R) do { \ 86145256Sjkoshy KASSERT(pmc_pmcdisp[(R)] <= 0, ("[pmc,%d] row disposition error", \ 87145256Sjkoshy __LINE__)); \ 88145256Sjkoshy atomic_add_int(&pmc_pmcdisp[(R)], -1); \ 89145256Sjkoshy KASSERT(pmc_pmcdisp[(R)] >= (-mp_ncpus), ("[pmc,%d] row " \ 90145256Sjkoshy "disposition error", __LINE__)); \ 91145256Sjkoshy} while (0) 92145256Sjkoshy 93145256Sjkoshy#define PMC_UNMARK_ROW_STANDALONE(R) do { \ 94145256Sjkoshy atomic_add_int(&pmc_pmcdisp[(R)], 1); \ 95145256Sjkoshy KASSERT(pmc_pmcdisp[(R)] <= 0, ("[pmc,%d] row disposition error", \ 96145256Sjkoshy __LINE__)); \ 97145256Sjkoshy} while (0) 98145256Sjkoshy 99145256Sjkoshy#define PMC_MARK_ROW_THREAD(R) do { \ 100145256Sjkoshy KASSERT(pmc_pmcdisp[(R)] >= 0, ("[pmc,%d] row disposition error", \ 101145256Sjkoshy __LINE__)); \ 102145256Sjkoshy atomic_add_int(&pmc_pmcdisp[(R)], 1); \ 103145256Sjkoshy} while (0) 104145256Sjkoshy 105145256Sjkoshy#define PMC_UNMARK_ROW_THREAD(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 112145256Sjkoshy/* various event handlers */ 113145256Sjkoshystatic eventhandler_tag pmc_exit_tag, pmc_fork_tag; 114145256Sjkoshy 115145256Sjkoshy/* Module statistics */ 116145256Sjkoshystruct pmc_op_getdriverstats pmc_stats; 117145256Sjkoshy 118145256Sjkoshy/* Machine/processor dependent operations */ 119145256Sjkoshystruct pmc_mdep *md; 120145256Sjkoshy 121145256Sjkoshy/* 122145256Sjkoshy * Hash tables mapping owner processes and target threads to PMCs. 123145256Sjkoshy */ 124145256Sjkoshy 125145256Sjkoshystruct mtx pmc_processhash_mtx; /* spin mutex */ 126145256Sjkoshystatic u_long pmc_processhashmask; 127145256Sjkoshystatic LIST_HEAD(pmc_processhash, pmc_process) *pmc_processhash; 128145256Sjkoshy 129145256Sjkoshy/* 130145256Sjkoshy * Hash table of PMC owner descriptors. This table is protected by 131145256Sjkoshy * the shared PMC "sx" lock. 132145256Sjkoshy */ 133145256Sjkoshy 134145256Sjkoshystatic u_long pmc_ownerhashmask; 135145256Sjkoshystatic LIST_HEAD(pmc_ownerhash, pmc_owner) *pmc_ownerhash; 136145256Sjkoshy 137145256Sjkoshy/* 138145256Sjkoshy * Prototypes 139145256Sjkoshy */ 140145256Sjkoshy 141145256Sjkoshy#if DEBUG 142145256Sjkoshystatic int pmc_debugflags_sysctl_handler(SYSCTL_HANDLER_ARGS); 143145256Sjkoshystatic int pmc_debugflags_parse(char *newstr, char *fence); 144145256Sjkoshy#endif 145145256Sjkoshy 146145256Sjkoshystatic int load(struct module *module, int cmd, void *arg); 147145256Sjkoshystatic int pmc_syscall_handler(struct thread *td, void *syscall_args); 148145256Sjkoshystatic int pmc_configure_log(struct pmc_owner *po, int logfd); 149145256Sjkoshystatic void pmc_log_process_exit(struct pmc *pm, struct pmc_process *pp); 150145256Sjkoshystatic struct pmc *pmc_allocate_pmc_descriptor(void); 151145256Sjkoshystatic struct pmc *pmc_find_pmc_descriptor_in_process(struct pmc_owner *po, 152145256Sjkoshy pmc_id_t pmc); 153145256Sjkoshystatic void pmc_release_pmc_descriptor(struct pmc *pmc); 154145774Sjkoshystatic int pmc_can_allocate_rowindex(struct proc *p, unsigned int ri, 155145774Sjkoshy int cpu); 156145256Sjkoshystatic struct pmc_process *pmc_find_process_descriptor(struct proc *p, 157145256Sjkoshy uint32_t mode); 158145256Sjkoshystatic void pmc_remove_process_descriptor(struct pmc_process *pp); 159145256Sjkoshystatic struct pmc_owner *pmc_find_owner_descriptor(struct proc *p); 160145256Sjkoshystatic int pmc_find_pmc(pmc_id_t pmcid, struct pmc **pm); 161145774Sjkoshystatic void pmc_force_context_switch(void); 162145256Sjkoshystatic void pmc_remove_owner(struct pmc_owner *po); 163145256Sjkoshystatic void pmc_maybe_remove_owner(struct pmc_owner *po); 164145256Sjkoshystatic void pmc_unlink_target_process(struct pmc *pmc, 165145256Sjkoshy struct pmc_process *pp); 166145256Sjkoshystatic void pmc_link_target_process(struct pmc *pm, 167145256Sjkoshy struct pmc_process *pp); 168145256Sjkoshystatic void pmc_unlink_owner(struct pmc *pmc); 169145256Sjkoshystatic void pmc_cleanup(void); 170145256Sjkoshystatic void pmc_save_cpu_binding(struct pmc_binding *pb); 171145256Sjkoshystatic void pmc_restore_cpu_binding(struct pmc_binding *pb); 172145256Sjkoshystatic void pmc_select_cpu(int cpu); 173145256Sjkoshystatic void pmc_process_exit(void *arg, struct proc *p); 174145256Sjkoshystatic void pmc_process_fork(void *arg, struct proc *p1, 175145256Sjkoshy struct proc *p2, int n); 176145256Sjkoshystatic int pmc_attach_one_process(struct proc *p, struct pmc *pm); 177145256Sjkoshystatic int pmc_attach_process(struct proc *p, struct pmc *pm); 178145256Sjkoshystatic int pmc_detach_one_process(struct proc *p, struct pmc *pm, 179145256Sjkoshy int flags); 180145256Sjkoshystatic int pmc_detach_process(struct proc *p, struct pmc *pm); 181145256Sjkoshystatic int pmc_start(struct pmc *pm); 182145256Sjkoshystatic int pmc_stop(struct pmc *pm); 183145256Sjkoshystatic int pmc_can_attach(struct pmc *pm, struct proc *p); 184145256Sjkoshy 185145256Sjkoshy/* 186145256Sjkoshy * Kernel tunables and sysctl(8) interface. 187145256Sjkoshy */ 188145256Sjkoshy 189145256Sjkoshy#define PMC_SYSCTL_NAME_PREFIX "kern." PMC_MODULE_NAME "." 190145256Sjkoshy 191145256SjkoshySYSCTL_NODE(_kern, OID_AUTO, hwpmc, CTLFLAG_RW, 0, "HWPMC parameters"); 192145256Sjkoshy 193145256Sjkoshy#if DEBUG 194145256Sjkoshyunsigned int pmc_debugflags = PMC_DEBUG_DEFAULT_FLAGS; 195145256Sjkoshychar pmc_debugstr[PMC_DEBUG_STRSIZE]; 196145256SjkoshyTUNABLE_STR(PMC_SYSCTL_NAME_PREFIX "debugflags", pmc_debugstr, 197145256Sjkoshy sizeof(pmc_debugstr)); 198145256SjkoshySYSCTL_PROC(_kern_hwpmc, OID_AUTO, debugflags, 199145256Sjkoshy CTLTYPE_STRING|CTLFLAG_RW|CTLFLAG_TUN, 200145256Sjkoshy 0, 0, pmc_debugflags_sysctl_handler, "A", "debug flags"); 201145256Sjkoshy#endif 202145256Sjkoshy 203145256Sjkoshy/* 204145256Sjkoshy * kern.pmc.hashrows -- determines the number of rows in the 205145256Sjkoshy * of the hash table used to look up threads 206145256Sjkoshy */ 207145256Sjkoshy 208145256Sjkoshystatic int pmc_hashsize = PMC_HASH_SIZE; 209145256SjkoshyTUNABLE_INT(PMC_SYSCTL_NAME_PREFIX "hashsize", &pmc_hashsize); 210145256SjkoshySYSCTL_INT(_kern_hwpmc, OID_AUTO, hashsize, CTLFLAG_TUN|CTLFLAG_RD, 211145256Sjkoshy &pmc_hashsize, 0, "rows in hash tables"); 212145256Sjkoshy 213145256Sjkoshy/* 214145256Sjkoshy * kern.pmc.pcpusize -- the size of each per-cpu 215145256Sjkoshy * area for collection PC samples. 216145256Sjkoshy */ 217145256Sjkoshy 218145256Sjkoshystatic int pmc_pcpu_buffer_size = PMC_PCPU_BUFFER_SIZE; 219145256SjkoshyTUNABLE_INT(PMC_SYSCTL_NAME_PREFIX "pcpubuffersize", &pmc_pcpu_buffer_size); 220145256SjkoshySYSCTL_INT(_kern_hwpmc, OID_AUTO, pcpubuffersize, CTLFLAG_TUN|CTLFLAG_RD, 221145256Sjkoshy &pmc_pcpu_buffer_size, 0, "size of per-cpu buffer in 4K pages"); 222145256Sjkoshy 223145256Sjkoshy/* 224145256Sjkoshy * kern.pmc.mtxpoolsize -- number of mutexes in the mutex pool. 225145256Sjkoshy */ 226145256Sjkoshy 227145256Sjkoshystatic int pmc_mtxpool_size = PMC_MTXPOOL_SIZE; 228145256SjkoshyTUNABLE_INT(PMC_SYSCTL_NAME_PREFIX "mtxpoolsize", &pmc_mtxpool_size); 229145256SjkoshySYSCTL_INT(_kern_hwpmc, OID_AUTO, mtxpoolsize, CTLFLAG_TUN|CTLFLAG_RD, 230145256Sjkoshy &pmc_mtxpool_size, 0, "size of spin mutex pool"); 231145256Sjkoshy 232145256Sjkoshy 233145256Sjkoshy 234145256Sjkoshy/* 235145256Sjkoshy * security.bsd.unprivileged_syspmcs -- allow non-root processes to 236145256Sjkoshy * allocate system-wide PMCs. 237145256Sjkoshy * 238145256Sjkoshy * Allowing unprivileged processes to allocate system PMCs is convenient 239145256Sjkoshy * if system-wide measurements need to be taken concurrently with other 240145256Sjkoshy * per-process measurements. This feature is turned off by default. 241145256Sjkoshy */ 242145256Sjkoshy 243145256SjkoshySYSCTL_DECL(_security_bsd); 244145256Sjkoshy 245145256Sjkoshystatic int pmc_unprivileged_syspmcs = 0; 246145256SjkoshyTUNABLE_INT("security.bsd.unprivileged_syspmcs", &pmc_unprivileged_syspmcs); 247145256SjkoshySYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_syspmcs, CTLFLAG_RW, 248145256Sjkoshy &pmc_unprivileged_syspmcs, 0, 249145256Sjkoshy "allow unprivileged process to allocate system PMCs"); 250145256Sjkoshy 251145256Sjkoshy#if PMC_HASH_USE_CRC32 252145256Sjkoshy 253145256Sjkoshy#define PMC_HASH_PTR(P,M) (crc32(&(P), sizeof((P))) & (M)) 254145256Sjkoshy 255145256Sjkoshy#else /* integer multiplication */ 256145256Sjkoshy 257145256Sjkoshy#if LONG_BIT == 64 258145256Sjkoshy#define _PMC_HM 11400714819323198486u 259145256Sjkoshy#elif LONG_BIT == 32 260145256Sjkoshy#define _PMC_HM 2654435769u 261145256Sjkoshy#else 262145256Sjkoshy#error Must know the size of 'long' to compile 263145256Sjkoshy#endif 264145256Sjkoshy 265145256Sjkoshy/* 266145256Sjkoshy * Hash function. Discard the lower 2 bits of the pointer since 267145256Sjkoshy * these are always zero for our uses. The hash multiplier is 268145256Sjkoshy * round((2^LONG_BIT) * ((sqrt(5)-1)/2)). 269145256Sjkoshy */ 270145256Sjkoshy 271145256Sjkoshy#define PMC_HASH_PTR(P,M) ((((unsigned long) (P) >> 2) * _PMC_HM) & (M)) 272145256Sjkoshy 273145256Sjkoshy#endif 274145256Sjkoshy 275145256Sjkoshy/* 276145256Sjkoshy * Syscall structures 277145256Sjkoshy */ 278145256Sjkoshy 279145256Sjkoshy/* The `sysent' for the new syscall */ 280145256Sjkoshystatic struct sysent pmc_sysent = { 281145256Sjkoshy 2, /* sy_narg */ 282145256Sjkoshy pmc_syscall_handler /* sy_call */ 283145256Sjkoshy}; 284145256Sjkoshy 285145256Sjkoshystatic struct syscall_module_data pmc_syscall_mod = { 286145256Sjkoshy load, 287145256Sjkoshy NULL, 288145256Sjkoshy &pmc_syscall_num, 289145256Sjkoshy &pmc_sysent, 290145256Sjkoshy { 0, NULL } 291145256Sjkoshy}; 292145256Sjkoshy 293145256Sjkoshystatic moduledata_t pmc_mod = { 294145256Sjkoshy PMC_MODULE_NAME, 295145256Sjkoshy syscall_module_handler, 296145256Sjkoshy &pmc_syscall_mod 297145256Sjkoshy}; 298145256Sjkoshy 299145256SjkoshyDECLARE_MODULE(pmc, pmc_mod, SI_SUB_SMP, SI_ORDER_ANY); 300145256SjkoshyMODULE_VERSION(pmc, PMC_VERSION); 301145256Sjkoshy 302145256Sjkoshy#if DEBUG 303145256Sjkoshystatic int 304145256Sjkoshypmc_debugflags_parse(char *newstr, char *fence) 305145256Sjkoshy{ 306145313Sjkoshy char c, *p, *q; 307145256Sjkoshy unsigned int tmpflags; 308145256Sjkoshy int level; 309145256Sjkoshy char tmpbuf[4]; /* 3 character keyword + '\0' */ 310145256Sjkoshy 311145256Sjkoshy tmpflags = 0; 312145256Sjkoshy level = 0xF; /* max verbosity */ 313145256Sjkoshy 314145256Sjkoshy p = newstr; 315145256Sjkoshy 316145256Sjkoshy for (; p < fence && (c = *p);) { 317145256Sjkoshy 318145256Sjkoshy /* skip separators */ 319145256Sjkoshy if (c == ' ' || c == '\t' || c == ',') { 320145256Sjkoshy p++; continue; 321145256Sjkoshy } 322145256Sjkoshy 323145256Sjkoshy (void) strlcpy(tmpbuf, p, sizeof(tmpbuf)); 324145256Sjkoshy 325145256Sjkoshy#define CMP_SET_FLAG_MAJ(S,F) \ 326145256Sjkoshy else if (strncmp(tmpbuf, S, 3) == 0) \ 327145256Sjkoshy tmpflags |= __PMCDFMAJ(F) 328145256Sjkoshy 329145256Sjkoshy#define CMP_SET_FLAG_MIN(S,F) \ 330145256Sjkoshy else if (strncmp(tmpbuf, S, 3) == 0) \ 331145256Sjkoshy tmpflags |= __PMCDFMIN(F) 332145256Sjkoshy 333145313Sjkoshy if (fence - p > 6 && strncmp(p, "level=", 6) == 0) { 334145256Sjkoshy p += 6; /* skip over keyword */ 335145256Sjkoshy level = strtoul(p, &q, 16); 336145256Sjkoshy } 337145256Sjkoshy CMP_SET_FLAG_MAJ("mod", MOD); 338145256Sjkoshy CMP_SET_FLAG_MAJ("pmc", PMC); 339145256Sjkoshy CMP_SET_FLAG_MAJ("ctx", CTX); 340145256Sjkoshy CMP_SET_FLAG_MAJ("own", OWN); 341145256Sjkoshy CMP_SET_FLAG_MAJ("prc", PRC); 342145256Sjkoshy CMP_SET_FLAG_MAJ("mdp", MDP); 343145256Sjkoshy CMP_SET_FLAG_MAJ("cpu", CPU); 344145256Sjkoshy 345145256Sjkoshy CMP_SET_FLAG_MIN("all", ALL); 346145256Sjkoshy CMP_SET_FLAG_MIN("rel", REL); 347145256Sjkoshy CMP_SET_FLAG_MIN("ops", OPS); 348145256Sjkoshy CMP_SET_FLAG_MIN("ini", INI); 349145256Sjkoshy CMP_SET_FLAG_MIN("fnd", FND); 350145256Sjkoshy CMP_SET_FLAG_MIN("pmh", PMH); 351145256Sjkoshy CMP_SET_FLAG_MIN("pms", PMS); 352145256Sjkoshy CMP_SET_FLAG_MIN("orm", ORM); 353145256Sjkoshy CMP_SET_FLAG_MIN("omr", OMR); 354145256Sjkoshy CMP_SET_FLAG_MIN("tlk", TLK); 355145256Sjkoshy CMP_SET_FLAG_MIN("tul", TUL); 356145256Sjkoshy CMP_SET_FLAG_MIN("ext", EXT); 357145256Sjkoshy CMP_SET_FLAG_MIN("exc", EXC); 358145256Sjkoshy CMP_SET_FLAG_MIN("frk", FRK); 359145256Sjkoshy CMP_SET_FLAG_MIN("att", ATT); 360145256Sjkoshy CMP_SET_FLAG_MIN("swi", SWI); 361145256Sjkoshy CMP_SET_FLAG_MIN("swo", SWO); 362145256Sjkoshy CMP_SET_FLAG_MIN("reg", REG); 363145256Sjkoshy CMP_SET_FLAG_MIN("alr", ALR); 364145256Sjkoshy CMP_SET_FLAG_MIN("rea", REA); 365145256Sjkoshy CMP_SET_FLAG_MIN("wri", WRI); 366145256Sjkoshy CMP_SET_FLAG_MIN("cfg", CFG); 367145256Sjkoshy CMP_SET_FLAG_MIN("sta", STA); 368145256Sjkoshy CMP_SET_FLAG_MIN("sto", STO); 369145774Sjkoshy CMP_SET_FLAG_MIN("int", INT); 370145256Sjkoshy CMP_SET_FLAG_MIN("bnd", BND); 371145256Sjkoshy CMP_SET_FLAG_MIN("sel", SEL); 372145256Sjkoshy else /* unrecognized keyword */ 373145256Sjkoshy return EINVAL; 374145256Sjkoshy 375145256Sjkoshy p += 4; /* skip keyword and separator */ 376145256Sjkoshy } 377145256Sjkoshy 378145256Sjkoshy pmc_debugflags = (tmpflags|level); 379145256Sjkoshy 380145256Sjkoshy return 0; 381145256Sjkoshy} 382145256Sjkoshy 383145256Sjkoshystatic int 384145256Sjkoshypmc_debugflags_sysctl_handler(SYSCTL_HANDLER_ARGS) 385145256Sjkoshy{ 386145256Sjkoshy char *fence, *newstr; 387145256Sjkoshy int error; 388145256Sjkoshy unsigned int n; 389145256Sjkoshy 390145256Sjkoshy (void) arg1; (void) arg2; /* unused parameters */ 391145256Sjkoshy 392145256Sjkoshy n = sizeof(pmc_debugstr); 393145256Sjkoshy MALLOC(newstr, char *, n, M_PMC, M_ZERO|M_WAITOK); 394145256Sjkoshy (void) strlcpy(newstr, pmc_debugstr, sizeof(pmc_debugstr)); 395145256Sjkoshy 396145256Sjkoshy error = sysctl_handle_string(oidp, newstr, n, req); 397145256Sjkoshy 398145256Sjkoshy /* if there is a new string, parse and copy it */ 399145256Sjkoshy if (error == 0 && req->newptr != NULL) { 400145256Sjkoshy fence = newstr + (n < req->newlen ? n : req->newlen); 401145256Sjkoshy if ((error = pmc_debugflags_parse(newstr, fence)) == 0) 402145256Sjkoshy (void) strlcpy(pmc_debugstr, newstr, 403145256Sjkoshy sizeof(pmc_debugstr)); 404145256Sjkoshy } 405145256Sjkoshy 406145256Sjkoshy FREE(newstr, M_PMC); 407145256Sjkoshy 408145256Sjkoshy return error; 409145256Sjkoshy} 410145256Sjkoshy#endif 411145256Sjkoshy 412145256Sjkoshy/* 413145256Sjkoshy * Concurrency Control 414145256Sjkoshy * 415145256Sjkoshy * The driver manages the following data structures: 416145256Sjkoshy * 417145256Sjkoshy * - target process descriptors, one per target process 418145256Sjkoshy * - owner process descriptors (and attached lists), one per owner process 419145256Sjkoshy * - lookup hash tables for owner and target processes 420145256Sjkoshy * - PMC descriptors (and attached lists) 421145256Sjkoshy * - per-cpu hardware state 422145256Sjkoshy * - the 'hook' variable through which the kernel calls into 423145256Sjkoshy * this module 424145256Sjkoshy * - the machine hardware state (managed by the MD layer) 425145256Sjkoshy * 426145256Sjkoshy * These data structures are accessed from: 427145256Sjkoshy * 428145256Sjkoshy * - thread context-switch code 429145256Sjkoshy * - interrupt handlers (possibly on multiple cpus) 430145256Sjkoshy * - kernel threads on multiple cpus running on behalf of user 431145256Sjkoshy * processes doing system calls 432145256Sjkoshy * - this driver's private kernel threads 433145256Sjkoshy * 434145256Sjkoshy * = Locks and Locking strategy = 435145256Sjkoshy * 436145256Sjkoshy * The driver uses four locking strategies for its operation: 437145256Sjkoshy * 438145256Sjkoshy * - There is a 'global' SX lock "pmc_sx" that is used to protect 439145256Sjkoshy * the its 'meta-data'. 440145256Sjkoshy * 441145256Sjkoshy * Calls into the module (via syscall() or by the kernel) start with 442145256Sjkoshy * this lock being held in exclusive mode. Depending on the requested 443145256Sjkoshy * operation, the lock may be downgraded to 'shared' mode to allow 444145256Sjkoshy * more concurrent readers into the module. 445145256Sjkoshy * 446145256Sjkoshy * This SX lock is held in exclusive mode for any operations that 447145256Sjkoshy * modify the linkages between the driver's internal data structures. 448145256Sjkoshy * 449145256Sjkoshy * The 'pmc_hook' function pointer is also protected by this lock. 450145256Sjkoshy * It is only examined with the sx lock held in exclusive mode. The 451145256Sjkoshy * kernel module is allowed to be unloaded only with the sx lock 452145256Sjkoshy * held in exclusive mode. In normal syscall handling, after 453145256Sjkoshy * acquiring the pmc_sx lock we first check that 'pmc_hook' is 454145256Sjkoshy * non-null before proceeding. This prevents races between the 455145256Sjkoshy * thread unloading the module and other threads seeking to use the 456145256Sjkoshy * module. 457145256Sjkoshy * 458145256Sjkoshy * - Lookups of target process structures and owner process structures 459145256Sjkoshy * cannot use the global "pmc_sx" SX lock because these lookups need 460145256Sjkoshy * to happen during context switches and in other critical sections 461145256Sjkoshy * where sleeping is not allowed. We protect these lookup tables 462145256Sjkoshy * with their own private spin-mutexes, "pmc_processhash_mtx" and 463145256Sjkoshy * "pmc_ownerhash_mtx". These are 'leaf' mutexes, in that no other 464145256Sjkoshy * lock is acquired with these locks held. 465145256Sjkoshy * 466145256Sjkoshy * - Interrupt handlers work in a lock free manner. At interrupt 467145256Sjkoshy * time, handlers look at the PMC pointer (phw->phw_pmc) configured 468145256Sjkoshy * when the PMC was started. If this pointer is NULL, the interrupt 469145256Sjkoshy * is ignored after updating driver statistics. We ensure that this 470145256Sjkoshy * pointer is set (using an atomic operation if necessary) before the 471145256Sjkoshy * PMC hardware is started. Conversely, this pointer is unset atomically 472145256Sjkoshy * only after the PMC hardware is stopped. 473145256Sjkoshy * 474145256Sjkoshy * We ensure that everything needed for the operation of an 475145256Sjkoshy * interrupt handler is available without it needing to acquire any 476145256Sjkoshy * locks. We also ensure that a PMC's software state is destroyed only 477145256Sjkoshy * after the PMC is taken off hardware (on all CPUs). 478145256Sjkoshy * 479145256Sjkoshy * - Context-switch handling with process-private PMCs needs more 480145256Sjkoshy * care. 481145256Sjkoshy * 482145256Sjkoshy * A given process may be the target of multiple PMCs. For example, 483145256Sjkoshy * PMCATTACH and PMCDETACH may be requested by a process on one CPU 484145256Sjkoshy * while the target process is running on another. A PMC could also 485145256Sjkoshy * be getting released because its owner is exiting. We tackle 486145256Sjkoshy * these situations in the following manner: 487145256Sjkoshy * 488145256Sjkoshy * - each target process structure 'pmc_process' has an array 489145256Sjkoshy * of 'struct pmc *' pointers, one for each hardware PMC. 490145256Sjkoshy * 491145256Sjkoshy * - At context switch IN time, each "target" PMC in RUNNING state 492145256Sjkoshy * gets started on hardware and a pointer to each PMC is copied into 493145256Sjkoshy * the per-cpu phw array. The 'runcount' for the PMC is 494145256Sjkoshy * incremented. 495145256Sjkoshy * 496145256Sjkoshy * - At context switch OUT time, all process-virtual PMCs are stopped 497145256Sjkoshy * on hardware. The saved value is added to the PMCs value field 498145256Sjkoshy * only if the PMC is in a non-deleted state (the PMCs state could 499145256Sjkoshy * have changed during the current time slice). 500145256Sjkoshy * 501145256Sjkoshy * Note that since in-between a switch IN on a processor and a switch 502145256Sjkoshy * OUT, the PMC could have been released on another CPU. Therefore 503145256Sjkoshy * context switch OUT always looks at the hardware state to turn 504145256Sjkoshy * OFF PMCs and will update a PMC's saved value only if reachable 505145256Sjkoshy * from the target process record. 506145256Sjkoshy * 507145256Sjkoshy * - OP PMCRELEASE could be called on a PMC at any time (the PMC could 508145256Sjkoshy * be attached to many processes at the time of the call and could 509145256Sjkoshy * be active on multiple CPUs). 510145256Sjkoshy * 511145256Sjkoshy * We prevent further scheduling of the PMC by marking it as in 512145256Sjkoshy * state 'DELETED'. If the runcount of the PMC is non-zero then 513145256Sjkoshy * this PMC is currently running on a CPU somewhere. The thread 514145256Sjkoshy * doing the PMCRELEASE operation waits by repeatedly doing an 515145256Sjkoshy * tsleep() till the runcount comes to zero. 516145256Sjkoshy * 517145256Sjkoshy */ 518145256Sjkoshy 519145256Sjkoshy/* 520145256Sjkoshy * save the cpu binding of the current kthread 521145256Sjkoshy */ 522145256Sjkoshy 523145256Sjkoshystatic void 524145256Sjkoshypmc_save_cpu_binding(struct pmc_binding *pb) 525145256Sjkoshy{ 526145256Sjkoshy PMCDBG(CPU,BND,2, "%s", "save-cpu"); 527145256Sjkoshy mtx_lock_spin(&sched_lock); 528145256Sjkoshy pb->pb_bound = sched_is_bound(curthread); 529145256Sjkoshy pb->pb_cpu = curthread->td_oncpu; 530145256Sjkoshy mtx_unlock_spin(&sched_lock); 531145256Sjkoshy PMCDBG(CPU,BND,2, "save-cpu cpu=%d", pb->pb_cpu); 532145256Sjkoshy} 533145256Sjkoshy 534145256Sjkoshy/* 535145256Sjkoshy * restore the cpu binding of the current thread 536145256Sjkoshy */ 537145256Sjkoshy 538145256Sjkoshystatic void 539145256Sjkoshypmc_restore_cpu_binding(struct pmc_binding *pb) 540145256Sjkoshy{ 541145256Sjkoshy PMCDBG(CPU,BND,2, "restore-cpu curcpu=%d restore=%d", 542145256Sjkoshy curthread->td_oncpu, pb->pb_cpu); 543145256Sjkoshy mtx_lock_spin(&sched_lock); 544145256Sjkoshy if (pb->pb_bound) 545145256Sjkoshy sched_bind(curthread, pb->pb_cpu); 546145256Sjkoshy else 547145256Sjkoshy sched_unbind(curthread); 548145256Sjkoshy mtx_unlock_spin(&sched_lock); 549145256Sjkoshy PMCDBG(CPU,BND,2, "%s", "restore-cpu done"); 550145256Sjkoshy} 551145256Sjkoshy 552145256Sjkoshy/* 553145256Sjkoshy * move execution over the specified cpu and bind it there. 554145256Sjkoshy */ 555145256Sjkoshy 556145256Sjkoshystatic void 557145256Sjkoshypmc_select_cpu(int cpu) 558145256Sjkoshy{ 559145256Sjkoshy KASSERT(cpu >= 0 && cpu < mp_ncpus, 560145256Sjkoshy ("[pmc,%d] bad cpu number %d", __LINE__, cpu)); 561145256Sjkoshy 562145256Sjkoshy /* never move to a disabled CPU */ 563145256Sjkoshy KASSERT(pmc_cpu_is_disabled(cpu) == 0, ("[pmc,%d] selecting " 564145256Sjkoshy "disabled CPU %d", __LINE__, cpu)); 565145256Sjkoshy 566145256Sjkoshy PMCDBG(CPU,SEL,2, "select-cpu cpu=%d", cpu); 567145256Sjkoshy mtx_lock_spin(&sched_lock); 568145256Sjkoshy sched_bind(curthread, cpu); 569145256Sjkoshy mtx_unlock_spin(&sched_lock); 570145256Sjkoshy 571145256Sjkoshy KASSERT(curthread->td_oncpu == cpu, 572145256Sjkoshy ("[pmc,%d] CPU not bound [cpu=%d, curr=%d]", __LINE__, 573145256Sjkoshy cpu, curthread->td_oncpu)); 574145256Sjkoshy 575145256Sjkoshy PMCDBG(CPU,SEL,2, "select-cpu cpu=%d ok", cpu); 576145256Sjkoshy} 577145256Sjkoshy 578145256Sjkoshy/* 579145774Sjkoshy * Force a context switch. 580145774Sjkoshy * 581145774Sjkoshy * We do this by tsleep'ing for 1 tick -- invoking mi_switch() is not 582145774Sjkoshy * guaranteed to force a context switch. 583145774Sjkoshy */ 584145774Sjkoshy 585145774Sjkoshystatic void 586145774Sjkoshypmc_force_context_switch(void) 587145774Sjkoshy{ 588145774Sjkoshy u_char curpri; 589145774Sjkoshy 590145774Sjkoshy mtx_lock_spin(&sched_lock); 591145774Sjkoshy curpri = curthread->td_priority; 592145774Sjkoshy mtx_unlock_spin(&sched_lock); 593145774Sjkoshy 594145774Sjkoshy (void) tsleep((void *) pmc_force_context_switch, curpri, 595145774Sjkoshy "pmcctx", 1); 596145774Sjkoshy 597145774Sjkoshy} 598145774Sjkoshy 599145774Sjkoshy/* 600145256Sjkoshy * Update the per-pmc histogram 601145256Sjkoshy */ 602145256Sjkoshy 603145256Sjkoshyvoid 604145256Sjkoshypmc_update_histogram(struct pmc_hw *phw, uintptr_t pc) 605145256Sjkoshy{ 606145256Sjkoshy (void) phw; 607145256Sjkoshy (void) pc; 608145256Sjkoshy} 609145256Sjkoshy 610145256Sjkoshy/* 611145256Sjkoshy * Send a signal to a process. This is meant to be invoked from an 612145256Sjkoshy * interrupt handler. 613145256Sjkoshy */ 614145256Sjkoshy 615145256Sjkoshyvoid 616145256Sjkoshypmc_send_signal(struct pmc *pmc) 617145256Sjkoshy{ 618145256Sjkoshy (void) pmc; /* shutup gcc */ 619145256Sjkoshy 620145256Sjkoshy#if 0 621145256Sjkoshy struct proc *proc; 622145256Sjkoshy struct thread *td; 623145256Sjkoshy 624145256Sjkoshy KASSERT(pmc->pm_owner != NULL, 625145256Sjkoshy ("[pmc,%d] No owner for PMC", __LINE__)); 626145256Sjkoshy 627145256Sjkoshy KASSERT((pmc->pm_owner->po_flags & PMC_FLAG_IS_OWNER) && 628145256Sjkoshy (pmc->pm_owner->po_flags & PMC_FLAG_HAS_TS_PMC), 629145256Sjkoshy ("[pmc,%d] interrupting PMC owner has wrong flags 0x%x", 630145256Sjkoshy __LINE__, pmc->pm_owner->po_flags)); 631145256Sjkoshy 632145256Sjkoshy proc = pmc->pm_owner->po_owner; 633145256Sjkoshy 634145256Sjkoshy KASSERT(curthread->td_proc == proc, 635145256Sjkoshy ("[pmc,%d] interruping the wrong thread (owner %p, " 636145256Sjkoshy "cur %p)", __LINE__, (void *) proc, curthread->td_proc)); 637145256Sjkoshy 638145256Sjkoshy mtx_lock_spin(&sched_lock); 639145256Sjkoshy td = TAILQ_FIRST(&proc->p_threads); 640145256Sjkoshy mtx_unlock_spin(&sched_lock); 641145256Sjkoshy /* XXX RACE HERE: can 'td' disappear now? */ 642145256Sjkoshy trapsignal(td, SIGPROF, 0); 643145256Sjkoshy /* XXX rework this to use the regular 'psignal' interface from a 644145256Sjkoshy helper thread */ 645145256Sjkoshy#endif 646145256Sjkoshy 647145256Sjkoshy} 648145256Sjkoshy 649145256Sjkoshy/* 650145256Sjkoshy * remove an process owning PMCs 651145256Sjkoshy */ 652145256Sjkoshy 653145256Sjkoshyvoid 654145256Sjkoshypmc_remove_owner(struct pmc_owner *po) 655145256Sjkoshy{ 656145256Sjkoshy struct pmc_list *pl, *tmp; 657145256Sjkoshy 658145256Sjkoshy sx_assert(&pmc_sx, SX_XLOCKED); 659145256Sjkoshy 660145256Sjkoshy PMCDBG(OWN,ORM,1, "remove-owner po=%p", po); 661145256Sjkoshy 662145256Sjkoshy /* Remove descriptor from the owner hash table */ 663145256Sjkoshy LIST_REMOVE(po, po_next); 664145256Sjkoshy 665145256Sjkoshy /* pass 1: release all owned PMC descriptors */ 666145256Sjkoshy LIST_FOREACH_SAFE(pl, &po->po_pmcs, pl_next, tmp) { 667145256Sjkoshy 668145256Sjkoshy PMCDBG(OWN,ORM,2, "pl=%p pmc=%p", pl, pl->pl_pmc); 669145256Sjkoshy 670145256Sjkoshy /* remove the associated PMC descriptor, if present */ 671145256Sjkoshy if (pl->pl_pmc) 672145256Sjkoshy pmc_release_pmc_descriptor(pl->pl_pmc); 673145256Sjkoshy 674145256Sjkoshy /* remove the linked list entry */ 675145256Sjkoshy LIST_REMOVE(pl, pl_next); 676145256Sjkoshy FREE(pl, M_PMC); 677145256Sjkoshy } 678145256Sjkoshy 679145256Sjkoshy /* pass 2: delete the pmc_list chain */ 680145256Sjkoshy LIST_FOREACH_SAFE(pl, &po->po_pmcs, pl_next, tmp) { 681145256Sjkoshy KASSERT(pl->pl_pmc == NULL, 682145256Sjkoshy ("[pmc,%d] non-null pmc pointer", __LINE__)); 683145256Sjkoshy LIST_REMOVE(pl, pl_next); 684145256Sjkoshy FREE(pl, M_PMC); 685145256Sjkoshy } 686145256Sjkoshy 687145256Sjkoshy KASSERT(LIST_EMPTY(&po->po_pmcs), 688145256Sjkoshy ("[pmc,%d] PMC list not empty", __LINE__)); 689145256Sjkoshy 690145256Sjkoshy 691145256Sjkoshy /* 692145256Sjkoshy * If this process owns a log file used for system wide logging, 693145256Sjkoshy * remove the log file. 694145256Sjkoshy * 695145256Sjkoshy * XXX rework needed. 696145256Sjkoshy */ 697145256Sjkoshy 698145774Sjkoshy if (po->po_flags & PMC_PO_OWNS_LOGFILE) 699145256Sjkoshy pmc_configure_log(po, -1); 700145256Sjkoshy 701145256Sjkoshy} 702145256Sjkoshy 703145256Sjkoshy/* 704145256Sjkoshy * remove an owner process record if all conditions are met. 705145256Sjkoshy */ 706145256Sjkoshy 707145256Sjkoshystatic void 708145256Sjkoshypmc_maybe_remove_owner(struct pmc_owner *po) 709145256Sjkoshy{ 710145256Sjkoshy 711145256Sjkoshy PMCDBG(OWN,OMR,1, "maybe-remove-owner po=%p", po); 712145256Sjkoshy 713145256Sjkoshy /* 714145256Sjkoshy * Remove owner record if 715145256Sjkoshy * - this process does not own any PMCs 716145256Sjkoshy * - this process has not allocated a system-wide sampling buffer 717145256Sjkoshy */ 718145256Sjkoshy 719145256Sjkoshy if (LIST_EMPTY(&po->po_pmcs) && 720145774Sjkoshy ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0)) { 721145256Sjkoshy pmc_remove_owner(po); 722145256Sjkoshy FREE(po, M_PMC); 723145256Sjkoshy } 724145256Sjkoshy} 725145256Sjkoshy 726145256Sjkoshy/* 727145256Sjkoshy * Add an association between a target process and a PMC. 728145256Sjkoshy */ 729145256Sjkoshy 730145256Sjkoshystatic void 731145256Sjkoshypmc_link_target_process(struct pmc *pm, struct pmc_process *pp) 732145256Sjkoshy{ 733145256Sjkoshy int ri; 734145256Sjkoshy struct pmc_target *pt; 735145256Sjkoshy 736145256Sjkoshy sx_assert(&pmc_sx, SX_XLOCKED); 737145256Sjkoshy 738145256Sjkoshy KASSERT(pm != NULL && pp != NULL, 739145256Sjkoshy ("[pmc,%d] Null pm %p or pp %p", __LINE__, pm, pp)); 740145256Sjkoshy 741145256Sjkoshy KASSERT(pp->pp_refcnt >= 0 && pp->pp_refcnt < ((int) md->pmd_npmc - 1), 742145256Sjkoshy ("[pmc,%d] Illegal reference count %d for process record %p", 743145256Sjkoshy __LINE__, pp->pp_refcnt, (void *) pp)); 744145256Sjkoshy 745145774Sjkoshy ri = PMC_TO_ROWINDEX(pm); 746145256Sjkoshy 747145256Sjkoshy PMCDBG(PRC,TLK,1, "link-target pmc=%p ri=%d pmc-process=%p", 748145256Sjkoshy pm, ri, pp); 749145256Sjkoshy 750145256Sjkoshy#if DEBUG 751145256Sjkoshy LIST_FOREACH(pt, &pm->pm_targets, pt_next) 752145256Sjkoshy if (pt->pt_process == pp) 753145256Sjkoshy KASSERT(0, ("[pmc,%d] pp %p already in pmc %p targets", 754145256Sjkoshy __LINE__, pp, pm)); 755145256Sjkoshy#endif 756145256Sjkoshy 757145256Sjkoshy MALLOC(pt, struct pmc_target *, sizeof(struct pmc_target), 758145256Sjkoshy M_PMC, M_ZERO|M_WAITOK); 759145256Sjkoshy 760145256Sjkoshy pt->pt_process = pp; 761145256Sjkoshy 762145256Sjkoshy LIST_INSERT_HEAD(&pm->pm_targets, pt, pt_next); 763145256Sjkoshy 764145256Sjkoshy atomic_store_rel_ptr(&pp->pp_pmcs[ri].pp_pmc, pm); 765145256Sjkoshy 766145615Sjkoshy if (pm->pm_owner->po_owner == pp->pp_proc) 767145774Sjkoshy pm->pm_flags |= PMC_F_ATTACHED_TO_OWNER; 768145615Sjkoshy 769145256Sjkoshy pp->pp_refcnt++; 770145256Sjkoshy 771145256Sjkoshy} 772145256Sjkoshy 773145256Sjkoshy/* 774145256Sjkoshy * Removes the association between a target process and a PMC. 775145256Sjkoshy */ 776145256Sjkoshy 777145256Sjkoshystatic void 778145256Sjkoshypmc_unlink_target_process(struct pmc *pm, struct pmc_process *pp) 779145256Sjkoshy{ 780145256Sjkoshy int ri; 781145256Sjkoshy struct pmc_target *ptgt; 782145256Sjkoshy 783145256Sjkoshy sx_assert(&pmc_sx, SX_XLOCKED); 784145256Sjkoshy 785145256Sjkoshy KASSERT(pm != NULL && pp != NULL, 786145256Sjkoshy ("[pmc,%d] Null pm %p or pp %p", __LINE__, pm, pp)); 787145256Sjkoshy 788145256Sjkoshy KASSERT(pp->pp_refcnt >= 1 && pp->pp_refcnt < (int) md->pmd_npmc, 789145256Sjkoshy ("[pmc,%d] Illegal ref count %d on process record %p", 790145256Sjkoshy __LINE__, pp->pp_refcnt, (void *) pp)); 791145256Sjkoshy 792145774Sjkoshy ri = PMC_TO_ROWINDEX(pm); 793145256Sjkoshy 794145256Sjkoshy PMCDBG(PRC,TUL,1, "unlink-target pmc=%p ri=%d pmc-process=%p", 795145256Sjkoshy pm, ri, pp); 796145256Sjkoshy 797145256Sjkoshy KASSERT(pp->pp_pmcs[ri].pp_pmc == pm, 798145256Sjkoshy ("[pmc,%d] PMC ri %d mismatch pmc %p pp->[ri] %p", __LINE__, 799145256Sjkoshy ri, pm, pp->pp_pmcs[ri].pp_pmc)); 800145256Sjkoshy 801145256Sjkoshy pp->pp_pmcs[ri].pp_pmc = NULL; 802145256Sjkoshy pp->pp_pmcs[ri].pp_pmcval = (pmc_value_t) 0; 803145256Sjkoshy 804145774Sjkoshy /* Remove owner-specific flags */ 805145774Sjkoshy if (pm->pm_owner->po_owner == pp->pp_proc) { 806145774Sjkoshy pp->pp_flags &= ~PMC_PP_ENABLE_MSR_ACCESS; 807145774Sjkoshy pm->pm_flags &= ~PMC_F_ATTACHED_TO_OWNER; 808145774Sjkoshy } 809145615Sjkoshy 810145256Sjkoshy pp->pp_refcnt--; 811145256Sjkoshy 812145256Sjkoshy /* Remove the target process from the PMC structure */ 813145256Sjkoshy LIST_FOREACH(ptgt, &pm->pm_targets, pt_next) 814145256Sjkoshy if (ptgt->pt_process == pp) 815145256Sjkoshy break; 816145256Sjkoshy 817145256Sjkoshy KASSERT(ptgt != NULL, ("[pmc,%d] process %p (pp: %p) not found " 818145256Sjkoshy "in pmc %p", __LINE__, pp->pp_proc, pp, pm)); 819145256Sjkoshy 820145256Sjkoshy LIST_REMOVE(ptgt, pt_next); 821145256Sjkoshy FREE(ptgt, M_PMC); 822145256Sjkoshy} 823145256Sjkoshy 824145256Sjkoshy/* 825145256Sjkoshy * Remove PMC descriptor 'pmc' from the owner descriptor. 826145256Sjkoshy */ 827145256Sjkoshy 828145256Sjkoshyvoid 829145256Sjkoshypmc_unlink_owner(struct pmc *pm) 830145256Sjkoshy{ 831145256Sjkoshy struct pmc_list *pl, *tmp; 832145256Sjkoshy struct pmc_owner *po; 833145256Sjkoshy 834145256Sjkoshy#if DEBUG 835145256Sjkoshy KASSERT(LIST_EMPTY(&pm->pm_targets), 836145256Sjkoshy ("[pmc,%d] unlinking PMC with targets", __LINE__)); 837145256Sjkoshy#endif 838145256Sjkoshy 839145256Sjkoshy po = pm->pm_owner; 840145256Sjkoshy 841145256Sjkoshy KASSERT(po != NULL, ("[pmc,%d] No owner for PMC", __LINE__)); 842145256Sjkoshy 843145256Sjkoshy LIST_FOREACH_SAFE(pl, &po->po_pmcs, pl_next, tmp) { 844145256Sjkoshy if (pl->pl_pmc == pm) { 845145256Sjkoshy pl->pl_pmc = NULL; 846145256Sjkoshy pm->pm_owner = NULL; 847145256Sjkoshy return; 848145256Sjkoshy } 849145256Sjkoshy } 850145256Sjkoshy 851145256Sjkoshy KASSERT(0, ("[pmc,%d] couldn't find pmc in owner list", __LINE__)); 852145256Sjkoshy} 853145256Sjkoshy 854145256Sjkoshy/* 855145256Sjkoshy * Check if PMC 'pm' may be attached to target process 't'. 856145256Sjkoshy */ 857145256Sjkoshy 858145256Sjkoshystatic int 859145256Sjkoshypmc_can_attach(struct pmc *pm, struct proc *t) 860145256Sjkoshy{ 861145256Sjkoshy struct proc *o; /* pmc owner */ 862145256Sjkoshy struct ucred *oc, *tc; /* owner, target credentials */ 863145256Sjkoshy int decline_attach, i; 864145256Sjkoshy 865145256Sjkoshy /* 866145256Sjkoshy * A PMC's owner can always attach that PMC to itself. 867145256Sjkoshy */ 868145256Sjkoshy 869145256Sjkoshy if ((o = pm->pm_owner->po_owner) == t) 870145256Sjkoshy return 0; 871145256Sjkoshy 872145256Sjkoshy PROC_LOCK(o); 873145256Sjkoshy oc = o->p_ucred; 874145256Sjkoshy crhold(oc); 875145256Sjkoshy PROC_UNLOCK(o); 876145256Sjkoshy 877145256Sjkoshy PROC_LOCK(t); 878145256Sjkoshy tc = t->p_ucred; 879145256Sjkoshy crhold(tc); 880145256Sjkoshy PROC_UNLOCK(t); 881145256Sjkoshy 882145256Sjkoshy /* 883145256Sjkoshy * The effective uid of the PMC owner should match at least one 884145256Sjkoshy * of the {effective,real,saved} uids of the target process. 885145256Sjkoshy */ 886145256Sjkoshy 887145256Sjkoshy decline_attach = oc->cr_uid != tc->cr_uid && 888145256Sjkoshy oc->cr_uid != tc->cr_svuid && 889145256Sjkoshy oc->cr_uid != tc->cr_ruid; 890145256Sjkoshy 891145256Sjkoshy /* 892145256Sjkoshy * Every one of the target's group ids, must be in the owner's 893145256Sjkoshy * group list. 894145256Sjkoshy */ 895145256Sjkoshy for (i = 0; !decline_attach && i < tc->cr_ngroups; i++) 896145256Sjkoshy decline_attach = !groupmember(tc->cr_groups[i], oc); 897145256Sjkoshy 898145256Sjkoshy /* check the read and saved gids too */ 899145256Sjkoshy if (decline_attach == 0) 900145256Sjkoshy decline_attach = !groupmember(tc->cr_rgid, oc) || 901145256Sjkoshy !groupmember(tc->cr_svgid, oc); 902145256Sjkoshy 903145256Sjkoshy crfree(tc); 904145256Sjkoshy crfree(oc); 905145256Sjkoshy 906145256Sjkoshy return !decline_attach; 907145256Sjkoshy} 908145256Sjkoshy 909145256Sjkoshy/* 910145256Sjkoshy * Attach a process to a PMC. 911145256Sjkoshy */ 912145256Sjkoshy 913145256Sjkoshystatic int 914145256Sjkoshypmc_attach_one_process(struct proc *p, struct pmc *pm) 915145256Sjkoshy{ 916145256Sjkoshy int ri; 917145256Sjkoshy struct pmc_process *pp; 918145256Sjkoshy 919145256Sjkoshy sx_assert(&pmc_sx, SX_XLOCKED); 920145256Sjkoshy 921145256Sjkoshy PMCDBG(PRC,ATT,2, "attach-one pm=%p ri=%d proc=%p (%d, %s)", pm, 922145774Sjkoshy PMC_TO_ROWINDEX(pm), p, p->p_pid, p->p_comm); 923145256Sjkoshy 924145256Sjkoshy /* 925145256Sjkoshy * Locate the process descriptor corresponding to process 'p', 926145256Sjkoshy * allocating space as needed. 927145256Sjkoshy * 928145256Sjkoshy * Verify that rowindex 'pm_rowindex' is free in the process 929145256Sjkoshy * descriptor. 930145256Sjkoshy * 931145256Sjkoshy * If not, allocate space for a descriptor and link the 932145256Sjkoshy * process descriptor and PMC. 933145256Sjkoshy */ 934145256Sjkoshy 935145774Sjkoshy ri = PMC_TO_ROWINDEX(pm); 936145256Sjkoshy 937145256Sjkoshy if ((pp = pmc_find_process_descriptor(p, PMC_FLAG_ALLOCATE)) == NULL) 938145256Sjkoshy return ENOMEM; 939145256Sjkoshy 940145256Sjkoshy if (pp->pp_pmcs[ri].pp_pmc == pm) /* already present at slot [ri] */ 941145256Sjkoshy return EEXIST; 942145256Sjkoshy 943145256Sjkoshy if (pp->pp_pmcs[ri].pp_pmc != NULL) 944145256Sjkoshy return EBUSY; 945145256Sjkoshy 946145256Sjkoshy pmc_link_target_process(pm, pp); 947145256Sjkoshy 948145256Sjkoshy /* mark process as using HWPMCs */ 949145256Sjkoshy PROC_LOCK(p); 950145256Sjkoshy p->p_flag |= P_HWPMC; 951145256Sjkoshy PROC_UNLOCK(p); 952145256Sjkoshy 953145256Sjkoshy return 0; 954145256Sjkoshy} 955145256Sjkoshy 956145256Sjkoshy/* 957145256Sjkoshy * Attach a process and optionally its children 958145256Sjkoshy */ 959145256Sjkoshy 960145256Sjkoshystatic int 961145256Sjkoshypmc_attach_process(struct proc *p, struct pmc *pm) 962145256Sjkoshy{ 963145256Sjkoshy int error; 964145256Sjkoshy struct proc *top; 965145256Sjkoshy 966145256Sjkoshy sx_assert(&pmc_sx, SX_XLOCKED); 967145256Sjkoshy 968145256Sjkoshy PMCDBG(PRC,ATT,1, "attach pm=%p ri=%d proc=%p (%d, %s)", pm, 969145774Sjkoshy PMC_TO_ROWINDEX(pm), p, p->p_pid, p->p_comm); 970145256Sjkoshy 971145774Sjkoshy 972145774Sjkoshy /* 973145774Sjkoshy * If this PMC successfully allowed a GETMSR operation 974145774Sjkoshy * in the past, disallow further ATTACHes. 975145774Sjkoshy */ 976145774Sjkoshy 977145774Sjkoshy if ((pm->pm_flags & PMC_PP_ENABLE_MSR_ACCESS) != 0) 978145774Sjkoshy return EPERM; 979145774Sjkoshy 980145256Sjkoshy if ((pm->pm_flags & PMC_F_DESCENDANTS) == 0) 981145256Sjkoshy return pmc_attach_one_process(p, pm); 982145256Sjkoshy 983145256Sjkoshy /* 984145256Sjkoshy * Traverse all child processes, attaching them to 985145256Sjkoshy * this PMC. 986145256Sjkoshy */ 987145256Sjkoshy 988145256Sjkoshy sx_slock(&proctree_lock); 989145256Sjkoshy 990145256Sjkoshy top = p; 991145256Sjkoshy 992145256Sjkoshy for (;;) { 993145256Sjkoshy if ((error = pmc_attach_one_process(p, pm)) != 0) 994145256Sjkoshy break; 995145256Sjkoshy if (!LIST_EMPTY(&p->p_children)) 996145256Sjkoshy p = LIST_FIRST(&p->p_children); 997145256Sjkoshy else for (;;) { 998145256Sjkoshy if (p == top) 999145256Sjkoshy goto done; 1000145256Sjkoshy if (LIST_NEXT(p, p_sibling)) { 1001145256Sjkoshy p = LIST_NEXT(p, p_sibling); 1002145256Sjkoshy break; 1003145256Sjkoshy } 1004145256Sjkoshy p = p->p_pptr; 1005145256Sjkoshy } 1006145256Sjkoshy } 1007145256Sjkoshy 1008145256Sjkoshy if (error) 1009145256Sjkoshy (void) pmc_detach_process(top, pm); 1010145256Sjkoshy 1011145256Sjkoshy done: 1012145256Sjkoshy sx_sunlock(&proctree_lock); 1013145256Sjkoshy return error; 1014145256Sjkoshy} 1015145256Sjkoshy 1016145256Sjkoshy/* 1017145256Sjkoshy * Detach a process from a PMC. If there are no other PMCs tracking 1018145256Sjkoshy * this process, remove the process structure from its hash table. If 1019145256Sjkoshy * 'flags' contains PMC_FLAG_REMOVE, then free the process structure. 1020145256Sjkoshy */ 1021145256Sjkoshy 1022145256Sjkoshystatic int 1023145256Sjkoshypmc_detach_one_process(struct proc *p, struct pmc *pm, int flags) 1024145256Sjkoshy{ 1025145256Sjkoshy int ri; 1026145256Sjkoshy struct pmc_process *pp; 1027145256Sjkoshy 1028145256Sjkoshy sx_assert(&pmc_sx, SX_XLOCKED); 1029145256Sjkoshy 1030145256Sjkoshy KASSERT(pm != NULL, 1031145256Sjkoshy ("[pmc,%d] null pm pointer", __LINE__)); 1032145256Sjkoshy 1033145774Sjkoshy ri = PMC_TO_ROWINDEX(pm); 1034145774Sjkoshy 1035145256Sjkoshy PMCDBG(PRC,ATT,2, "detach-one pm=%p ri=%d proc=%p (%d, %s) flags=0x%x", 1036145774Sjkoshy pm, ri, p, p->p_pid, p->p_comm, flags); 1037145256Sjkoshy 1038145256Sjkoshy if ((pp = pmc_find_process_descriptor(p, 0)) == NULL) 1039145256Sjkoshy return ESRCH; 1040145256Sjkoshy 1041145256Sjkoshy if (pp->pp_pmcs[ri].pp_pmc != pm) 1042145256Sjkoshy return EINVAL; 1043145256Sjkoshy 1044145256Sjkoshy pmc_unlink_target_process(pm, pp); 1045145256Sjkoshy 1046145256Sjkoshy /* 1047145256Sjkoshy * If there are no PMCs targetting this process, we remove its 1048145256Sjkoshy * descriptor from the target hash table and unset the P_HWPMC 1049145256Sjkoshy * flag in the struct proc. 1050145256Sjkoshy */ 1051145256Sjkoshy 1052145256Sjkoshy KASSERT(pp->pp_refcnt >= 0 && pp->pp_refcnt < (int) md->pmd_npmc, 1053145256Sjkoshy ("[pmc,%d] Illegal refcnt %d for process struct %p", 1054145256Sjkoshy __LINE__, pp->pp_refcnt, pp)); 1055145256Sjkoshy 1056145256Sjkoshy if (pp->pp_refcnt != 0) /* still a target of some PMC */ 1057145256Sjkoshy return 0; 1058145256Sjkoshy 1059145256Sjkoshy pmc_remove_process_descriptor(pp); 1060145256Sjkoshy 1061145256Sjkoshy if (flags & PMC_FLAG_REMOVE) 1062145256Sjkoshy FREE(pp, M_PMC); 1063145256Sjkoshy 1064145256Sjkoshy PROC_LOCK(p); 1065145256Sjkoshy p->p_flag &= ~P_HWPMC; 1066145256Sjkoshy PROC_UNLOCK(p); 1067145256Sjkoshy 1068145256Sjkoshy return 0; 1069145256Sjkoshy} 1070145256Sjkoshy 1071145256Sjkoshy/* 1072145256Sjkoshy * Detach a process and optionally its descendants from a PMC. 1073145256Sjkoshy */ 1074145256Sjkoshy 1075145256Sjkoshystatic int 1076145256Sjkoshypmc_detach_process(struct proc *p, struct pmc *pm) 1077145256Sjkoshy{ 1078145256Sjkoshy struct proc *top; 1079145256Sjkoshy 1080145256Sjkoshy sx_assert(&pmc_sx, SX_XLOCKED); 1081145256Sjkoshy 1082145256Sjkoshy PMCDBG(PRC,ATT,1, "detach pm=%p ri=%d proc=%p (%d, %s)", pm, 1083145774Sjkoshy PMC_TO_ROWINDEX(pm), p, p->p_pid, p->p_comm); 1084145256Sjkoshy 1085145256Sjkoshy if ((pm->pm_flags & PMC_F_DESCENDANTS) == 0) 1086145256Sjkoshy return pmc_detach_one_process(p, pm, PMC_FLAG_REMOVE); 1087145256Sjkoshy 1088145256Sjkoshy /* 1089145256Sjkoshy * Traverse all children, detaching them from this PMC. We 1090145256Sjkoshy * ignore errors since we could be detaching a PMC from a 1091145256Sjkoshy * partially attached proc tree. 1092145256Sjkoshy */ 1093145256Sjkoshy 1094145256Sjkoshy sx_slock(&proctree_lock); 1095145256Sjkoshy 1096145256Sjkoshy top = p; 1097145256Sjkoshy 1098145256Sjkoshy for (;;) { 1099145256Sjkoshy (void) pmc_detach_one_process(p, pm, PMC_FLAG_REMOVE); 1100145256Sjkoshy 1101145256Sjkoshy if (!LIST_EMPTY(&p->p_children)) 1102145256Sjkoshy p = LIST_FIRST(&p->p_children); 1103145256Sjkoshy else for (;;) { 1104145256Sjkoshy if (p == top) 1105145256Sjkoshy goto done; 1106145256Sjkoshy if (LIST_NEXT(p, p_sibling)) { 1107145256Sjkoshy p = LIST_NEXT(p, p_sibling); 1108145256Sjkoshy break; 1109145256Sjkoshy } 1110145256Sjkoshy p = p->p_pptr; 1111145256Sjkoshy } 1112145256Sjkoshy } 1113145256Sjkoshy 1114145256Sjkoshy done: 1115145256Sjkoshy sx_sunlock(&proctree_lock); 1116145256Sjkoshy return 0; 1117145256Sjkoshy} 1118145256Sjkoshy 1119145256Sjkoshy/* 1120145256Sjkoshy * The 'hook' invoked from the kernel proper 1121145256Sjkoshy */ 1122145256Sjkoshy 1123145256Sjkoshy 1124145256Sjkoshy#if DEBUG 1125145256Sjkoshyconst char *pmc_hooknames[] = { 1126145256Sjkoshy "", 1127145256Sjkoshy "EXIT", 1128145256Sjkoshy "EXEC", 1129145256Sjkoshy "FORK", 1130145256Sjkoshy "CSW-IN", 1131145256Sjkoshy "CSW-OUT" 1132145256Sjkoshy}; 1133145256Sjkoshy#endif 1134145256Sjkoshy 1135145256Sjkoshystatic int 1136145256Sjkoshypmc_hook_handler(struct thread *td, int function, void *arg) 1137145256Sjkoshy{ 1138145256Sjkoshy 1139145256Sjkoshy KASSERT(td->td_proc->p_flag & P_HWPMC, 1140145256Sjkoshy ("[pmc,%d] unregistered thread called pmc_hook()", __LINE__)); 1141145256Sjkoshy 1142145256Sjkoshy PMCDBG(MOD,PMH,1, "hook td=%p func=%d \"%s\" arg=%p", td, function, 1143145256Sjkoshy pmc_hooknames[function], arg); 1144145256Sjkoshy 1145145256Sjkoshy switch (function) 1146145256Sjkoshy { 1147145256Sjkoshy 1148145256Sjkoshy /* 1149145256Sjkoshy * Process exit. 1150145256Sjkoshy * 1151145256Sjkoshy * Remove this process from all hash tables. If this process 1152145256Sjkoshy * owned any PMCs, turn off those PMCs and deallocate them, 1153145256Sjkoshy * removing any associations with target processes. 1154145256Sjkoshy * 1155145256Sjkoshy * This function will be called by the last 'thread' of a 1156145256Sjkoshy * process. 1157145256Sjkoshy * 1158145256Sjkoshy */ 1159145256Sjkoshy 1160145256Sjkoshy case PMC_FN_PROCESS_EXIT: /* release PMCs */ 1161145256Sjkoshy { 1162145256Sjkoshy int cpu; 1163145256Sjkoshy unsigned int ri; 1164145256Sjkoshy struct pmc *pm; 1165145256Sjkoshy struct pmc_process *pp; 1166145256Sjkoshy struct pmc_owner *po; 1167145256Sjkoshy struct proc *p; 1168145256Sjkoshy pmc_value_t newvalue, tmp; 1169145256Sjkoshy 1170145256Sjkoshy sx_assert(&pmc_sx, SX_XLOCKED); 1171145256Sjkoshy 1172145256Sjkoshy p = (struct proc *) arg; 1173145256Sjkoshy 1174145256Sjkoshy /* 1175145256Sjkoshy * Since this code is invoked by the last thread in an 1176145256Sjkoshy * exiting process, we would have context switched IN 1177145256Sjkoshy * at some prior point. Kernel mode context switches 1178145256Sjkoshy * may happen any time, so we want to disable a context 1179145256Sjkoshy * switch OUT till we get any PMCs targetting this 1180145256Sjkoshy * process off the hardware. 1181145256Sjkoshy * 1182145256Sjkoshy * We also need to atomically remove this process' 1183145256Sjkoshy * entry from our target process hash table, using 1184145256Sjkoshy * PMC_FLAG_REMOVE. 1185145256Sjkoshy */ 1186145256Sjkoshy 1187145256Sjkoshy PMCDBG(PRC,EXT,1, "process-exit proc=%p (%d, %s)", p, p->p_pid, 1188145256Sjkoshy p->p_comm); 1189145256Sjkoshy 1190145256Sjkoshy critical_enter(); /* no preemption */ 1191145256Sjkoshy 1192145256Sjkoshy cpu = curthread->td_oncpu; 1193145256Sjkoshy 1194145256Sjkoshy if ((pp = pmc_find_process_descriptor(p, 1195145256Sjkoshy PMC_FLAG_REMOVE)) != NULL) { 1196145256Sjkoshy 1197145256Sjkoshy PMCDBG(PRC,EXT,2, 1198145256Sjkoshy "process-exit proc=%p pmc-process=%p", p, pp); 1199145256Sjkoshy 1200145256Sjkoshy /* 1201145615Sjkoshy * The exiting process could the target of 1202145615Sjkoshy * some PMCs which will be running on 1203145615Sjkoshy * currently executing CPU. 1204145615Sjkoshy * 1205145256Sjkoshy * We need to turn these PMCs off like we 1206145256Sjkoshy * would do at context switch OUT time. 1207145256Sjkoshy */ 1208145256Sjkoshy 1209145256Sjkoshy for (ri = 0; ri < md->pmd_npmc; ri++) { 1210145256Sjkoshy 1211145256Sjkoshy /* 1212145256Sjkoshy * Pick up the pmc pointer from hardware 1213145256Sjkoshy * state similar to the CSW_OUT code. 1214145256Sjkoshy */ 1215145256Sjkoshy 1216145774Sjkoshy pm = NULL; 1217145774Sjkoshy (void) (*md->pmd_get_config)(cpu, ri, &pm); 1218145256Sjkoshy 1219145256Sjkoshy PMCDBG(PRC,EXT,2, "ri=%d pm=%p", ri, pm); 1220145256Sjkoshy 1221145256Sjkoshy if (pm == NULL || 1222145774Sjkoshy !PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm))) 1223145256Sjkoshy continue; 1224145256Sjkoshy 1225145256Sjkoshy PMCDBG(PRC,EXT,2, "ppmcs[%d]=%p pm=%p " 1226145256Sjkoshy "state=%d", ri, pp->pp_pmcs[ri].pp_pmc, 1227145256Sjkoshy pm, pm->pm_state); 1228145256Sjkoshy 1229145774Sjkoshy KASSERT(PMC_TO_ROWINDEX(pm) == ri, 1230145256Sjkoshy ("[pmc,%d] ri mismatch pmc(%d) ri(%d)", 1231145774Sjkoshy __LINE__, PMC_TO_ROWINDEX(pm), ri)); 1232145256Sjkoshy 1233145256Sjkoshy KASSERT(pm == pp->pp_pmcs[ri].pp_pmc, 1234145256Sjkoshy ("[pmc,%d] pm %p != pp_pmcs[%d] %p", 1235145256Sjkoshy __LINE__, pm, ri, 1236145256Sjkoshy pp->pp_pmcs[ri].pp_pmc)); 1237145256Sjkoshy 1238145256Sjkoshy (void) md->pmd_stop_pmc(cpu, ri); 1239145256Sjkoshy 1240145256Sjkoshy KASSERT(pm->pm_runcount > 0, 1241145256Sjkoshy ("[pmc,%d] bad runcount ri %d rc %d", 1242145256Sjkoshy __LINE__, ri, pm->pm_runcount)); 1243145256Sjkoshy 1244145256Sjkoshy if (pm->pm_state == PMC_STATE_RUNNING) { 1245145256Sjkoshy md->pmd_read_pmc(cpu, ri, &newvalue); 1246145256Sjkoshy tmp = newvalue - 1247145256Sjkoshy PMC_PCPU_SAVED(cpu,ri); 1248145256Sjkoshy 1249145256Sjkoshy mtx_pool_lock_spin(pmc_mtxpool, pm); 1250145256Sjkoshy pm->pm_gv.pm_savedvalue += tmp; 1251145256Sjkoshy pp->pp_pmcs[ri].pp_pmcval += tmp; 1252145256Sjkoshy mtx_pool_unlock_spin(pmc_mtxpool, pm); 1253145256Sjkoshy } 1254145256Sjkoshy 1255145774Sjkoshy atomic_subtract_rel_32(&pm->pm_runcount,1); 1256145774Sjkoshy 1257145256Sjkoshy KASSERT((int) pm->pm_runcount >= 0, 1258145256Sjkoshy ("[pmc,%d] runcount is %d", __LINE__, ri)); 1259145256Sjkoshy 1260145256Sjkoshy (void) md->pmd_config_pmc(cpu, ri, NULL); 1261145256Sjkoshy } 1262145615Sjkoshy 1263145615Sjkoshy /* 1264145615Sjkoshy * Inform the MD layer of this pseudo "context switch 1265145615Sjkoshy * out" 1266145615Sjkoshy */ 1267145615Sjkoshy 1268145615Sjkoshy (void) md->pmd_switch_out(pmc_pcpu[cpu], pp); 1269145615Sjkoshy 1270145256Sjkoshy critical_exit(); /* ok to be pre-empted now */ 1271145256Sjkoshy 1272145256Sjkoshy /* 1273145256Sjkoshy * Unlink this process from the PMCs that are 1274145256Sjkoshy * targetting it. Log value at exit() time if 1275145256Sjkoshy * requested. 1276145256Sjkoshy */ 1277145256Sjkoshy 1278145256Sjkoshy for (ri = 0; ri < md->pmd_npmc; ri++) 1279145256Sjkoshy if ((pm = pp->pp_pmcs[ri].pp_pmc) != NULL) { 1280145256Sjkoshy if (pm->pm_flags & 1281145256Sjkoshy PMC_F_LOG_TC_PROCEXIT) 1282145256Sjkoshy pmc_log_process_exit(pm, pp); 1283145256Sjkoshy pmc_unlink_target_process(pm, pp); 1284145256Sjkoshy } 1285145256Sjkoshy 1286145256Sjkoshy FREE(pp, M_PMC); 1287145256Sjkoshy 1288145774Sjkoshy 1289145256Sjkoshy } else 1290145256Sjkoshy critical_exit(); /* pp == NULL */ 1291145256Sjkoshy 1292145256Sjkoshy /* 1293145256Sjkoshy * If the process owned PMCs, free them up and free up 1294145256Sjkoshy * memory. 1295145256Sjkoshy */ 1296145256Sjkoshy 1297145256Sjkoshy if ((po = pmc_find_owner_descriptor(p)) != NULL) { 1298145256Sjkoshy pmc_remove_owner(po); 1299145256Sjkoshy FREE(po, M_PMC); 1300145256Sjkoshy } 1301145256Sjkoshy 1302145256Sjkoshy } 1303145256Sjkoshy break; 1304145256Sjkoshy 1305145256Sjkoshy /* 1306145256Sjkoshy * Process exec() 1307145256Sjkoshy */ 1308145256Sjkoshy 1309145256Sjkoshy case PMC_FN_PROCESS_EXEC: 1310145256Sjkoshy { 1311145256Sjkoshy int *credentials_changed; 1312145256Sjkoshy unsigned int ri; 1313145256Sjkoshy struct pmc *pm; 1314145256Sjkoshy struct proc *p; 1315145256Sjkoshy struct pmc_owner *po; 1316145256Sjkoshy struct pmc_process *pp; 1317145256Sjkoshy 1318145256Sjkoshy sx_assert(&pmc_sx, SX_XLOCKED); 1319145256Sjkoshy 1320145256Sjkoshy /* 1321145256Sjkoshy * PMCs are not inherited across an exec(): remove any 1322145256Sjkoshy * PMCs that this process is the owner of. 1323145256Sjkoshy */ 1324145256Sjkoshy 1325145256Sjkoshy p = td->td_proc; 1326145256Sjkoshy 1327145256Sjkoshy if ((po = pmc_find_owner_descriptor(p)) != NULL) { 1328145256Sjkoshy pmc_remove_owner(po); 1329145256Sjkoshy FREE(po, M_PMC); 1330145256Sjkoshy } 1331145256Sjkoshy 1332145256Sjkoshy /* 1333145256Sjkoshy * If this process is the target of a PMC, check if the new 1334145256Sjkoshy * credentials are compatible with the owner's permissions. 1335145256Sjkoshy */ 1336145256Sjkoshy 1337145256Sjkoshy if ((pp = pmc_find_process_descriptor(p, 0)) == NULL) 1338145256Sjkoshy break; 1339145256Sjkoshy 1340145256Sjkoshy credentials_changed = arg; 1341145256Sjkoshy 1342145256Sjkoshy PMCDBG(PRC,EXC,1, "exec proc=%p (%d, %s) cred-changed=%d", 1343145256Sjkoshy p, p->p_pid, p->p_comm, *credentials_changed); 1344145256Sjkoshy 1345145256Sjkoshy if (*credentials_changed == 0) /* credentials didn't change */ 1346145256Sjkoshy break; 1347145256Sjkoshy 1348145256Sjkoshy /* 1349145256Sjkoshy * If the newly exec()'ed process has a different credential 1350145256Sjkoshy * than before, allow it to be the target of a PMC only if 1351145256Sjkoshy * the PMC's owner has sufficient priviledge. 1352145256Sjkoshy */ 1353145256Sjkoshy 1354145256Sjkoshy for (ri = 0; ri < md->pmd_npmc; ri++) 1355145256Sjkoshy if ((pm = pp->pp_pmcs[ri].pp_pmc) != NULL) 1356145256Sjkoshy if (pmc_can_attach(pm, td->td_proc) != 0) 1357145256Sjkoshy pmc_detach_one_process(td->td_proc, 1358145256Sjkoshy pm, PMC_FLAG_NONE); 1359145256Sjkoshy 1360145256Sjkoshy KASSERT(pp->pp_refcnt >= 0 && pp->pp_refcnt < (int) md->pmd_npmc, 1361145256Sjkoshy ("[pmc,%d] Illegal ref count %d on pp %p", __LINE__, 1362145256Sjkoshy pp->pp_refcnt, pp)); 1363145256Sjkoshy 1364145256Sjkoshy /* 1365145256Sjkoshy * If this process is no longer the target of any 1366145256Sjkoshy * PMCs, we can remove the process entry and free 1367145256Sjkoshy * up space. 1368145256Sjkoshy */ 1369145256Sjkoshy 1370145256Sjkoshy if (pp->pp_refcnt == 0) { 1371145256Sjkoshy pmc_remove_process_descriptor(pp); 1372145256Sjkoshy FREE(pp, M_PMC); 1373145256Sjkoshy } 1374145256Sjkoshy } 1375145256Sjkoshy break; 1376145256Sjkoshy 1377145256Sjkoshy /* 1378145256Sjkoshy * Process fork() 1379145256Sjkoshy */ 1380145256Sjkoshy 1381145256Sjkoshy case PMC_FN_PROCESS_FORK: 1382145256Sjkoshy { 1383145256Sjkoshy unsigned int ri; 1384145256Sjkoshy uint32_t do_descendants; 1385145256Sjkoshy struct pmc *pm; 1386145256Sjkoshy struct pmc_process *ppnew, *ppold; 1387145256Sjkoshy struct proc *newproc; 1388145256Sjkoshy 1389145256Sjkoshy sx_assert(&pmc_sx, SX_XLOCKED); 1390145256Sjkoshy 1391145256Sjkoshy newproc = (struct proc *) arg; 1392145256Sjkoshy 1393145256Sjkoshy PMCDBG(PMC,FRK,2, "process-fork p1=%p p2=%p", 1394145256Sjkoshy curthread->td_proc, newproc); 1395145256Sjkoshy /* 1396145256Sjkoshy * If the parent process (curthread->td_proc) is a 1397145256Sjkoshy * target of any PMCs, look for PMCs that are to be 1398145256Sjkoshy * inherited, and link these into the new process 1399145256Sjkoshy * descriptor. 1400145256Sjkoshy */ 1401145256Sjkoshy 1402145256Sjkoshy if ((ppold = pmc_find_process_descriptor( 1403145256Sjkoshy curthread->td_proc, PMC_FLAG_NONE)) == NULL) 1404145256Sjkoshy break; 1405145256Sjkoshy 1406145256Sjkoshy do_descendants = 0; 1407145256Sjkoshy for (ri = 0; ri < md->pmd_npmc; ri++) 1408145256Sjkoshy if ((pm = ppold->pp_pmcs[ri].pp_pmc) != NULL) 1409145256Sjkoshy do_descendants |= 1410145256Sjkoshy pm->pm_flags & PMC_F_DESCENDANTS; 1411145256Sjkoshy if (do_descendants == 0) /* nothing to do */ 1412145256Sjkoshy break; 1413145256Sjkoshy 1414145256Sjkoshy if ((ppnew = pmc_find_process_descriptor(newproc, 1415145256Sjkoshy PMC_FLAG_ALLOCATE)) == NULL) 1416145256Sjkoshy return ENOMEM; 1417145256Sjkoshy 1418145256Sjkoshy /* 1419145256Sjkoshy * Run through all PMCs targeting the old process and 1420145256Sjkoshy * attach them to the new process. 1421145256Sjkoshy */ 1422145256Sjkoshy 1423145256Sjkoshy for (ri = 0; ri < md->pmd_npmc; ri++) 1424145256Sjkoshy if ((pm = ppold->pp_pmcs[ri].pp_pmc) != NULL && 1425145256Sjkoshy pm->pm_flags & PMC_F_DESCENDANTS) 1426145256Sjkoshy pmc_link_target_process(pm, ppnew); 1427145256Sjkoshy 1428145256Sjkoshy /* 1429145256Sjkoshy * Now mark the new process as being tracked by this 1430145256Sjkoshy * driver. 1431145256Sjkoshy */ 1432145256Sjkoshy 1433145256Sjkoshy PROC_LOCK(newproc); 1434145256Sjkoshy newproc->p_flag |= P_HWPMC; 1435145256Sjkoshy PROC_UNLOCK(newproc); 1436145256Sjkoshy 1437145256Sjkoshy } 1438145256Sjkoshy break; 1439145256Sjkoshy 1440145256Sjkoshy /* 1441145256Sjkoshy * Thread context switch IN 1442145256Sjkoshy */ 1443145256Sjkoshy 1444145256Sjkoshy case PMC_FN_CSW_IN: 1445145256Sjkoshy { 1446145256Sjkoshy int cpu; 1447145256Sjkoshy unsigned int ri; 1448145256Sjkoshy struct pmc *pm; 1449145256Sjkoshy struct proc *p; 1450145256Sjkoshy struct pmc_cpu *pc; 1451145256Sjkoshy struct pmc_hw *phw; 1452145256Sjkoshy struct pmc_process *pp; 1453145256Sjkoshy pmc_value_t newvalue; 1454145256Sjkoshy 1455145256Sjkoshy p = td->td_proc; 1456145256Sjkoshy 1457145256Sjkoshy if ((pp = pmc_find_process_descriptor(p, PMC_FLAG_NONE)) == NULL) 1458145256Sjkoshy break; 1459145256Sjkoshy 1460145256Sjkoshy KASSERT(pp->pp_proc == td->td_proc, 1461145256Sjkoshy ("[pmc,%d] not my thread state", __LINE__)); 1462145256Sjkoshy 1463145256Sjkoshy critical_enter(); /* no preemption on this CPU */ 1464145256Sjkoshy 1465145256Sjkoshy cpu = PCPU_GET(cpuid); /* td->td_oncpu is invalid */ 1466145256Sjkoshy 1467145256Sjkoshy PMCDBG(CTX,SWI,1, "cpu=%d proc=%p (%d, %s) pp=%p", cpu, p, 1468145256Sjkoshy p->p_pid, p->p_comm, pp); 1469145256Sjkoshy 1470145256Sjkoshy KASSERT(cpu >= 0 && cpu < mp_ncpus, 1471145256Sjkoshy ("[pmc,%d] wierd CPU id %d", __LINE__, cpu)); 1472145256Sjkoshy 1473145256Sjkoshy pc = pmc_pcpu[cpu]; 1474145256Sjkoshy 1475145256Sjkoshy for (ri = 0; ri < md->pmd_npmc; ri++) { 1476145256Sjkoshy 1477145256Sjkoshy if ((pm = pp->pp_pmcs[ri].pp_pmc) == NULL) 1478145256Sjkoshy continue; 1479145256Sjkoshy 1480145774Sjkoshy KASSERT(PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm)), 1481145256Sjkoshy ("[pmc,%d] Target PMC in non-virtual mode (%d)", 1482145774Sjkoshy __LINE__, PMC_TO_MODE(pm))); 1483145256Sjkoshy 1484145774Sjkoshy KASSERT(PMC_TO_ROWINDEX(pm) == ri, 1485145256Sjkoshy ("[pmc,%d] Row index mismatch pmc %d != ri %d", 1486145774Sjkoshy __LINE__, PMC_TO_ROWINDEX(pm), ri)); 1487145256Sjkoshy 1488145256Sjkoshy /* 1489145256Sjkoshy * Only PMCs that are marked as 'RUNNING' need 1490145256Sjkoshy * be placed on hardware. 1491145256Sjkoshy */ 1492145256Sjkoshy 1493145256Sjkoshy if (pm->pm_state != PMC_STATE_RUNNING) 1494145256Sjkoshy continue; 1495145256Sjkoshy 1496145256Sjkoshy /* increment PMC runcount */ 1497145256Sjkoshy atomic_add_rel_32(&pm->pm_runcount, 1); 1498145256Sjkoshy 1499145256Sjkoshy /* configure the HWPMC we are going to use. */ 1500145256Sjkoshy md->pmd_config_pmc(cpu, ri, pm); 1501145256Sjkoshy 1502145256Sjkoshy phw = pc->pc_hwpmcs[ri]; 1503145256Sjkoshy 1504145256Sjkoshy KASSERT(phw != NULL, 1505145256Sjkoshy ("[pmc,%d] null hw pointer", __LINE__)); 1506145256Sjkoshy 1507145256Sjkoshy KASSERT(phw->phw_pmc == pm, 1508145256Sjkoshy ("[pmc,%d] hw->pmc %p != pmc %p", __LINE__, 1509145256Sjkoshy phw->phw_pmc, pm)); 1510145256Sjkoshy 1511145256Sjkoshy /* write out saved value and start the PMC */ 1512145256Sjkoshy mtx_pool_lock_spin(pmc_mtxpool, pm); 1513145256Sjkoshy newvalue = PMC_PCPU_SAVED(cpu, ri) = 1514145256Sjkoshy pm->pm_gv.pm_savedvalue; 1515145256Sjkoshy mtx_pool_unlock_spin(pmc_mtxpool, pm); 1516145256Sjkoshy 1517145256Sjkoshy md->pmd_write_pmc(cpu, ri, newvalue); 1518145256Sjkoshy md->pmd_start_pmc(cpu, ri); 1519145256Sjkoshy 1520145256Sjkoshy } 1521145256Sjkoshy 1522145256Sjkoshy /* 1523145256Sjkoshy * perform any other architecture/cpu dependent thread 1524145256Sjkoshy * switch-in actions. 1525145256Sjkoshy */ 1526145256Sjkoshy 1527145615Sjkoshy (void) (*md->pmd_switch_in)(pc, pp); 1528145256Sjkoshy 1529145256Sjkoshy critical_exit(); 1530145256Sjkoshy 1531145256Sjkoshy } 1532145256Sjkoshy break; 1533145256Sjkoshy 1534145256Sjkoshy /* 1535145256Sjkoshy * Thread context switch OUT. 1536145256Sjkoshy */ 1537145256Sjkoshy 1538145256Sjkoshy case PMC_FN_CSW_OUT: 1539145256Sjkoshy { 1540145256Sjkoshy int cpu; 1541145256Sjkoshy unsigned int ri; 1542145256Sjkoshy struct pmc *pm; 1543145256Sjkoshy struct proc *p; 1544145256Sjkoshy struct pmc_cpu *pc; 1545145256Sjkoshy struct pmc_process *pp; 1546145256Sjkoshy pmc_value_t newvalue, tmp; 1547145256Sjkoshy 1548145256Sjkoshy /* 1549145256Sjkoshy * Locate our process descriptor; this may be NULL if 1550145256Sjkoshy * this process is exiting and we have already removed 1551145256Sjkoshy * the process from the target process table. 1552145256Sjkoshy * 1553145256Sjkoshy * Note that due to kernel preemption, multiple 1554145256Sjkoshy * context switches may happen while the process is 1555145256Sjkoshy * exiting. 1556145256Sjkoshy * 1557145256Sjkoshy * Note also that if the target process cannot be 1558145256Sjkoshy * found we still need to deconfigure any PMCs that 1559145256Sjkoshy * are currently running on hardware. 1560145256Sjkoshy */ 1561145256Sjkoshy 1562145256Sjkoshy p = td->td_proc; 1563145256Sjkoshy pp = pmc_find_process_descriptor(p, PMC_FLAG_NONE); 1564145256Sjkoshy 1565145256Sjkoshy /* 1566145256Sjkoshy * save PMCs 1567145256Sjkoshy */ 1568145256Sjkoshy 1569145256Sjkoshy critical_enter(); 1570145256Sjkoshy 1571145256Sjkoshy cpu = PCPU_GET(cpuid); /* td->td_oncpu is invalid */ 1572145256Sjkoshy 1573145256Sjkoshy PMCDBG(CTX,SWO,1, "cpu=%d proc=%p (%d, %s) pp=%p", cpu, p, 1574145256Sjkoshy p->p_pid, p->p_comm, pp); 1575145256Sjkoshy 1576145256Sjkoshy KASSERT(cpu >= 0 && cpu < mp_ncpus, 1577145256Sjkoshy ("[pmc,%d wierd CPU id %d", __LINE__, cpu)); 1578145256Sjkoshy 1579145256Sjkoshy pc = pmc_pcpu[cpu]; 1580145256Sjkoshy 1581145256Sjkoshy /* 1582145256Sjkoshy * When a PMC gets unlinked from a target PMC, it will 1583145256Sjkoshy * be removed from the target's pp_pmc[] array. 1584145256Sjkoshy * 1585145256Sjkoshy * However, on a MP system, the target could have been 1586145256Sjkoshy * executing on another CPU at the time of the unlink. 1587145256Sjkoshy * So, at context switch OUT time, we need to look at 1588145256Sjkoshy * the hardware to determine if a PMC is scheduled on 1589145256Sjkoshy * it. 1590145256Sjkoshy */ 1591145256Sjkoshy 1592145256Sjkoshy for (ri = 0; ri < md->pmd_npmc; ri++) { 1593145256Sjkoshy 1594145774Sjkoshy pm = NULL; 1595145774Sjkoshy (void) (*md->pmd_get_config)(cpu, ri, &pm); 1596145256Sjkoshy 1597145256Sjkoshy if (pm == NULL) /* nothing at this row index */ 1598145256Sjkoshy continue; 1599145256Sjkoshy 1600145774Sjkoshy if (!PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm))) 1601145256Sjkoshy continue; /* not a process virtual PMC */ 1602145256Sjkoshy 1603145774Sjkoshy KASSERT(PMC_TO_ROWINDEX(pm) == ri, 1604145256Sjkoshy ("[pmc,%d] ri mismatch pmc(%d) ri(%d)", 1605145774Sjkoshy __LINE__, PMC_TO_ROWINDEX(pm), ri)); 1606145256Sjkoshy 1607145256Sjkoshy /* Stop hardware */ 1608145256Sjkoshy md->pmd_stop_pmc(cpu, ri); 1609145256Sjkoshy 1610145256Sjkoshy /* reduce this PMC's runcount */ 1611145256Sjkoshy atomic_subtract_rel_32(&pm->pm_runcount, 1); 1612145256Sjkoshy 1613145256Sjkoshy /* 1614145256Sjkoshy * If this PMC is associated with this process, 1615145256Sjkoshy * save the reading. 1616145256Sjkoshy */ 1617145256Sjkoshy 1618145256Sjkoshy if (pp != NULL && pp->pp_pmcs[ri].pp_pmc != NULL) { 1619145256Sjkoshy 1620145256Sjkoshy KASSERT(pm == pp->pp_pmcs[ri].pp_pmc, 1621145256Sjkoshy ("[pmc,%d] pm %p != pp_pmcs[%d] %p", 1622145256Sjkoshy __LINE__, pm, ri, 1623145256Sjkoshy pp->pp_pmcs[ri].pp_pmc)); 1624145256Sjkoshy 1625145256Sjkoshy KASSERT(pp->pp_refcnt > 0, 1626145256Sjkoshy ("[pmc,%d] pp refcnt = %d", __LINE__, 1627145256Sjkoshy pp->pp_refcnt)); 1628145256Sjkoshy 1629145256Sjkoshy md->pmd_read_pmc(cpu, ri, &newvalue); 1630145256Sjkoshy 1631145256Sjkoshy tmp = newvalue - PMC_PCPU_SAVED(cpu,ri); 1632145256Sjkoshy 1633145256Sjkoshy KASSERT((int64_t) tmp >= 0, 1634145256Sjkoshy ("[pmc,%d] negative increment cpu=%d " 1635145256Sjkoshy "ri=%d newvalue=%jx saved=%jx " 1636145256Sjkoshy "incr=%jx", __LINE__, cpu, ri, 1637145256Sjkoshy newvalue, PMC_PCPU_SAVED(cpu,ri), 1638145256Sjkoshy tmp)); 1639145256Sjkoshy 1640145256Sjkoshy /* 1641145256Sjkoshy * Increment the PMC's count and this 1642145256Sjkoshy * target process's count by the difference 1643145256Sjkoshy * between the current reading and the 1644145256Sjkoshy * saved value at context switch in time. 1645145256Sjkoshy */ 1646145256Sjkoshy 1647145256Sjkoshy mtx_pool_lock_spin(pmc_mtxpool, pm); 1648145256Sjkoshy 1649145256Sjkoshy pm->pm_gv.pm_savedvalue += tmp; 1650145256Sjkoshy pp->pp_pmcs[ri].pp_pmcval += tmp; 1651145256Sjkoshy 1652145256Sjkoshy mtx_pool_unlock_spin(pmc_mtxpool, pm); 1653145256Sjkoshy 1654145256Sjkoshy } 1655145256Sjkoshy 1656145256Sjkoshy /* mark hardware as free */ 1657145256Sjkoshy md->pmd_config_pmc(cpu, ri, NULL); 1658145256Sjkoshy } 1659145256Sjkoshy 1660145256Sjkoshy /* 1661145256Sjkoshy * perform any other architecture/cpu dependent thread 1662145256Sjkoshy * switch out functions. 1663145256Sjkoshy */ 1664145256Sjkoshy 1665145615Sjkoshy (void) (*md->pmd_switch_out)(pc, pp); 1666145256Sjkoshy 1667145256Sjkoshy critical_exit(); 1668145256Sjkoshy 1669145256Sjkoshy } 1670145256Sjkoshy break; 1671145256Sjkoshy 1672145256Sjkoshy default: 1673145256Sjkoshy#if DEBUG 1674145256Sjkoshy KASSERT(0, ("[pmc,%d] unknown hook %d\n", __LINE__, function)); 1675145256Sjkoshy#endif 1676145256Sjkoshy break; 1677145256Sjkoshy 1678145256Sjkoshy } 1679145256Sjkoshy 1680145256Sjkoshy return 0; 1681145256Sjkoshy} 1682145256Sjkoshy 1683145256Sjkoshy/* 1684145256Sjkoshy * allocate a 'struct pmc_owner' descriptor in the owner hash table. 1685145256Sjkoshy */ 1686145256Sjkoshy 1687145256Sjkoshystatic struct pmc_owner * 1688145256Sjkoshypmc_allocate_owner_descriptor(struct proc *p) 1689145256Sjkoshy{ 1690145256Sjkoshy uint32_t hindex; 1691145256Sjkoshy struct pmc_owner *po; 1692145256Sjkoshy struct pmc_ownerhash *poh; 1693145256Sjkoshy 1694145256Sjkoshy hindex = PMC_HASH_PTR(p, pmc_ownerhashmask); 1695145256Sjkoshy poh = &pmc_ownerhash[hindex]; 1696145256Sjkoshy 1697145256Sjkoshy /* allocate space for N pointers and one descriptor struct */ 1698145256Sjkoshy MALLOC(po, struct pmc_owner *, sizeof(struct pmc_owner), 1699145256Sjkoshy M_PMC, M_WAITOK); 1700145256Sjkoshy 1701145256Sjkoshy po->po_flags = 0; 1702145256Sjkoshy po->po_owner = p; 1703145256Sjkoshy LIST_INIT(&po->po_pmcs); 1704145256Sjkoshy LIST_INSERT_HEAD(poh, po, po_next); /* insert into hash table */ 1705145256Sjkoshy 1706145256Sjkoshy PMCDBG(OWN,ALL,1, "allocate-owner proc=%p (%d, %s) pmc-owner=%p", 1707145256Sjkoshy p, p->p_pid, p->p_comm, po); 1708145256Sjkoshy 1709145256Sjkoshy return po; 1710145256Sjkoshy} 1711145256Sjkoshy 1712145256Sjkoshy/* 1713145256Sjkoshy * find the descriptor corresponding to process 'p', adding or removing it 1714145256Sjkoshy * as specified by 'mode'. 1715145256Sjkoshy */ 1716145256Sjkoshy 1717145256Sjkoshystatic struct pmc_process * 1718145256Sjkoshypmc_find_process_descriptor(struct proc *p, uint32_t mode) 1719145256Sjkoshy{ 1720145256Sjkoshy uint32_t hindex; 1721145256Sjkoshy struct pmc_process *pp, *ppnew; 1722145256Sjkoshy struct pmc_processhash *pph; 1723145256Sjkoshy 1724145256Sjkoshy hindex = PMC_HASH_PTR(p, pmc_processhashmask); 1725145256Sjkoshy pph = &pmc_processhash[hindex]; 1726145256Sjkoshy 1727145256Sjkoshy ppnew = NULL; 1728145256Sjkoshy 1729145256Sjkoshy /* 1730145256Sjkoshy * Pre-allocate memory in the FIND_ALLOCATE case since we 1731145256Sjkoshy * cannot call malloc(9) once we hold a spin lock. 1732145256Sjkoshy */ 1733145256Sjkoshy 1734145256Sjkoshy if (mode & PMC_FLAG_ALLOCATE) { 1735145256Sjkoshy /* allocate additional space for 'n' pmc pointers */ 1736145256Sjkoshy MALLOC(ppnew, struct pmc_process *, 1737145256Sjkoshy sizeof(struct pmc_process) + md->pmd_npmc * 1738145256Sjkoshy sizeof(struct pmc_targetstate), M_PMC, M_ZERO|M_WAITOK); 1739145256Sjkoshy } 1740145256Sjkoshy 1741145256Sjkoshy mtx_lock_spin(&pmc_processhash_mtx); 1742145256Sjkoshy LIST_FOREACH(pp, pph, pp_next) 1743145256Sjkoshy if (pp->pp_proc == p) 1744145256Sjkoshy break; 1745145256Sjkoshy 1746145256Sjkoshy if ((mode & PMC_FLAG_REMOVE) && pp != NULL) 1747145256Sjkoshy LIST_REMOVE(pp, pp_next); 1748145256Sjkoshy 1749145256Sjkoshy if ((mode & PMC_FLAG_ALLOCATE) && pp == NULL && 1750145256Sjkoshy ppnew != NULL) { 1751145256Sjkoshy ppnew->pp_proc = p; 1752145256Sjkoshy LIST_INSERT_HEAD(pph, ppnew, pp_next); 1753145256Sjkoshy pp = ppnew; 1754145256Sjkoshy ppnew = NULL; 1755145256Sjkoshy } 1756145256Sjkoshy mtx_unlock_spin(&pmc_processhash_mtx); 1757145256Sjkoshy 1758145256Sjkoshy if (pp != NULL && ppnew != NULL) 1759145256Sjkoshy FREE(ppnew, M_PMC); 1760145256Sjkoshy 1761145256Sjkoshy return pp; 1762145256Sjkoshy} 1763145256Sjkoshy 1764145256Sjkoshy/* 1765145256Sjkoshy * remove a process descriptor from the process hash table. 1766145256Sjkoshy */ 1767145256Sjkoshy 1768145256Sjkoshystatic void 1769145256Sjkoshypmc_remove_process_descriptor(struct pmc_process *pp) 1770145256Sjkoshy{ 1771145256Sjkoshy KASSERT(pp->pp_refcnt == 0, 1772145256Sjkoshy ("[pmc,%d] Removing process descriptor %p with count %d", 1773145256Sjkoshy __LINE__, pp, pp->pp_refcnt)); 1774145256Sjkoshy 1775145256Sjkoshy mtx_lock_spin(&pmc_processhash_mtx); 1776145256Sjkoshy LIST_REMOVE(pp, pp_next); 1777145256Sjkoshy mtx_unlock_spin(&pmc_processhash_mtx); 1778145256Sjkoshy} 1779145256Sjkoshy 1780145256Sjkoshy 1781145256Sjkoshy/* 1782145256Sjkoshy * find an owner descriptor corresponding to proc 'p' 1783145256Sjkoshy */ 1784145256Sjkoshy 1785145256Sjkoshystatic struct pmc_owner * 1786145256Sjkoshypmc_find_owner_descriptor(struct proc *p) 1787145256Sjkoshy{ 1788145256Sjkoshy uint32_t hindex; 1789145256Sjkoshy struct pmc_owner *po; 1790145256Sjkoshy struct pmc_ownerhash *poh; 1791145256Sjkoshy 1792145256Sjkoshy hindex = PMC_HASH_PTR(p, pmc_ownerhashmask); 1793145256Sjkoshy poh = &pmc_ownerhash[hindex]; 1794145256Sjkoshy 1795145256Sjkoshy po = NULL; 1796145256Sjkoshy LIST_FOREACH(po, poh, po_next) 1797145256Sjkoshy if (po->po_owner == p) 1798145256Sjkoshy break; 1799145256Sjkoshy 1800145256Sjkoshy PMCDBG(OWN,FND,1, "find-owner proc=%p (%d, %s) hindex=0x%x -> " 1801145256Sjkoshy "pmc-owner=%p", p, p->p_pid, p->p_comm, hindex, po); 1802145256Sjkoshy 1803145256Sjkoshy return po; 1804145256Sjkoshy} 1805145256Sjkoshy 1806145256Sjkoshy/* 1807145256Sjkoshy * pmc_allocate_pmc_descriptor 1808145256Sjkoshy * 1809145256Sjkoshy * Allocate a pmc descriptor and initialize its 1810145256Sjkoshy * fields. 1811145256Sjkoshy */ 1812145256Sjkoshy 1813145256Sjkoshystatic struct pmc * 1814145256Sjkoshypmc_allocate_pmc_descriptor(void) 1815145256Sjkoshy{ 1816145256Sjkoshy struct pmc *pmc; 1817145256Sjkoshy 1818145256Sjkoshy MALLOC(pmc, struct pmc *, sizeof(struct pmc), M_PMC, M_ZERO|M_WAITOK); 1819145256Sjkoshy 1820145256Sjkoshy if (pmc != NULL) { 1821145256Sjkoshy pmc->pm_owner = NULL; 1822145256Sjkoshy LIST_INIT(&pmc->pm_targets); 1823145256Sjkoshy } 1824145256Sjkoshy 1825145256Sjkoshy PMCDBG(PMC,ALL,1, "allocate-pmc -> pmc=%p", pmc); 1826145256Sjkoshy 1827145256Sjkoshy return pmc; 1828145256Sjkoshy} 1829145256Sjkoshy 1830145256Sjkoshy/* 1831145256Sjkoshy * Destroy a pmc descriptor. 1832145256Sjkoshy */ 1833145256Sjkoshy 1834145256Sjkoshystatic void 1835145256Sjkoshypmc_destroy_pmc_descriptor(struct pmc *pm) 1836145256Sjkoshy{ 1837145256Sjkoshy (void) pm; 1838145256Sjkoshy 1839145256Sjkoshy#if DEBUG 1840145256Sjkoshy KASSERT(pm->pm_state == PMC_STATE_DELETED || 1841145256Sjkoshy pm->pm_state == PMC_STATE_FREE, 1842145256Sjkoshy ("[pmc,%d] destroying non-deleted PMC", __LINE__)); 1843145256Sjkoshy KASSERT(LIST_EMPTY(&pm->pm_targets), 1844145256Sjkoshy ("[pmc,%d] destroying pmc with targets", __LINE__)); 1845145256Sjkoshy KASSERT(pm->pm_owner == NULL, 1846145256Sjkoshy ("[pmc,%d] destroying pmc attached to an owner", __LINE__)); 1847145256Sjkoshy KASSERT(pm->pm_runcount == 0, 1848145256Sjkoshy ("[pmc,%d] pmc has non-zero run count %d", __LINE__, 1849145256Sjkoshy pm->pm_runcount)); 1850145256Sjkoshy#endif 1851145256Sjkoshy} 1852145256Sjkoshy 1853145256Sjkoshy/* 1854145256Sjkoshy * This function does the following things: 1855145256Sjkoshy * 1856145256Sjkoshy * - detaches the PMC from hardware 1857145256Sjkoshy * - unlinks all target threads that were attached to it 1858145256Sjkoshy * - removes the PMC from its owner's list 1859145256Sjkoshy * - destroy's the PMC private mutex 1860145256Sjkoshy * 1861145256Sjkoshy * Once this function completes, the given pmc pointer can be safely 1862145256Sjkoshy * FREE'd by the caller. 1863145256Sjkoshy */ 1864145256Sjkoshy 1865145256Sjkoshystatic void 1866145256Sjkoshypmc_release_pmc_descriptor(struct pmc *pm) 1867145256Sjkoshy{ 1868145256Sjkoshy#if DEBUG 1869145256Sjkoshy volatile int maxloop; 1870145256Sjkoshy#endif 1871145256Sjkoshy u_int ri, cpu; 1872145774Sjkoshy enum pmc_mode mode; 1873145256Sjkoshy struct pmc_hw *phw; 1874145256Sjkoshy struct pmc_process *pp; 1875145256Sjkoshy struct pmc_target *ptgt, *tmp; 1876145256Sjkoshy struct pmc_binding pb; 1877145256Sjkoshy 1878145256Sjkoshy sx_assert(&pmc_sx, SX_XLOCKED); 1879145256Sjkoshy 1880145256Sjkoshy KASSERT(pm, ("[pmc,%d] null pmc", __LINE__)); 1881145256Sjkoshy 1882145774Sjkoshy ri = PMC_TO_ROWINDEX(pm); 1883145774Sjkoshy mode = PMC_TO_MODE(pm); 1884145256Sjkoshy 1885145256Sjkoshy PMCDBG(PMC,REL,1, "release-pmc pmc=%p ri=%d mode=%d", pm, ri, 1886145774Sjkoshy mode); 1887145256Sjkoshy 1888145256Sjkoshy /* 1889145256Sjkoshy * First, we take the PMC off hardware. 1890145256Sjkoshy */ 1891145301Simp cpu = 0; 1892145774Sjkoshy if (PMC_IS_SYSTEM_MODE(mode)) { 1893145256Sjkoshy 1894145256Sjkoshy /* 1895145256Sjkoshy * A system mode PMC runs on a specific CPU. Switch 1896145256Sjkoshy * to this CPU and turn hardware off. 1897145256Sjkoshy */ 1898145256Sjkoshy 1899145256Sjkoshy pmc_save_cpu_binding(&pb); 1900145256Sjkoshy 1901145774Sjkoshy cpu = PMC_TO_CPU(pm); 1902145256Sjkoshy 1903145256Sjkoshy if (pm->pm_state == PMC_STATE_RUNNING) { 1904145256Sjkoshy 1905145256Sjkoshy pmc_select_cpu(cpu); 1906145256Sjkoshy 1907145256Sjkoshy phw = pmc_pcpu[cpu]->pc_hwpmcs[ri]; 1908145256Sjkoshy 1909145256Sjkoshy KASSERT(phw->phw_pmc == pm, 1910145256Sjkoshy ("[pmc, %d] pmc ptr ri(%d) hw(%p) pm(%p)", 1911145256Sjkoshy __LINE__, ri, phw->phw_pmc, pm)); 1912145256Sjkoshy 1913145256Sjkoshy PMCDBG(PMC,REL,2, "stopping cpu=%d ri=%d", cpu, ri); 1914145256Sjkoshy 1915145256Sjkoshy critical_enter(); 1916145256Sjkoshy md->pmd_stop_pmc(cpu, ri); 1917145256Sjkoshy critical_exit(); 1918145256Sjkoshy } 1919145256Sjkoshy 1920145256Sjkoshy PMCDBG(PMC,REL,2, "decfg cpu=%d ri=%d", cpu, ri); 1921145256Sjkoshy 1922145256Sjkoshy critical_enter(); 1923145256Sjkoshy md->pmd_config_pmc(cpu, ri, NULL); 1924145256Sjkoshy critical_exit(); 1925145256Sjkoshy 1926145256Sjkoshy pm->pm_state = PMC_STATE_DELETED; 1927145256Sjkoshy 1928145256Sjkoshy pmc_restore_cpu_binding(&pb); 1929145256Sjkoshy 1930145774Sjkoshy } else if (PMC_IS_VIRTUAL_MODE(mode)) { 1931145256Sjkoshy 1932145256Sjkoshy /* 1933145256Sjkoshy * A virtual PMC could be running on multiple CPUs at 1934145256Sjkoshy * a given instant. 1935145256Sjkoshy * 1936145256Sjkoshy * By marking its state as DELETED, we ensure that 1937145256Sjkoshy * this PMC is never further scheduled on hardware. 1938145256Sjkoshy * 1939145256Sjkoshy * Then we wait till all CPUs are done with this PMC. 1940145256Sjkoshy */ 1941145256Sjkoshy 1942145256Sjkoshy pm->pm_state = PMC_STATE_DELETED; 1943145256Sjkoshy 1944145256Sjkoshy 1945145256Sjkoshy /* 1946145256Sjkoshy * Wait for the PMCs runcount to come to zero. 1947145256Sjkoshy */ 1948145256Sjkoshy 1949145256Sjkoshy#if DEBUG 1950145256Sjkoshy maxloop = 100 * mp_ncpus; 1951145256Sjkoshy#endif 1952145256Sjkoshy 1953145256Sjkoshy while (atomic_load_acq_32(&pm->pm_runcount) > 0) { 1954145256Sjkoshy 1955145256Sjkoshy#if DEBUG 1956145256Sjkoshy maxloop--; 1957145256Sjkoshy KASSERT(maxloop > 0, 1958145256Sjkoshy ("[pmc,%d] (ri%d, rc%d) waiting too long for " 1959145774Sjkoshy "pmc to be free", __LINE__, 1960145774Sjkoshy PMC_TO_ROWINDEX(pm), pm->pm_runcount)); 1961145256Sjkoshy#endif 1962145256Sjkoshy 1963145774Sjkoshy pmc_force_context_switch(); 1964145256Sjkoshy } 1965145256Sjkoshy 1966145256Sjkoshy /* 1967145256Sjkoshy * At this point the PMC is off all CPUs and cannot be 1968145256Sjkoshy * freshly scheduled onto a CPU. It is now safe to 1969145256Sjkoshy * unlink all targets from this PMC. If a 1970145256Sjkoshy * process-record's refcount falls to zero, we remove 1971145256Sjkoshy * it from the hash table. The module-wide SX lock 1972145256Sjkoshy * protects us from races. 1973145256Sjkoshy */ 1974145256Sjkoshy 1975145256Sjkoshy LIST_FOREACH_SAFE(ptgt, &pm->pm_targets, pt_next, tmp) { 1976145256Sjkoshy pp = ptgt->pt_process; 1977145256Sjkoshy pmc_unlink_target_process(pm, pp); /* frees 'ptgt' */ 1978145256Sjkoshy 1979145256Sjkoshy PMCDBG(PMC,REL,3, "pp->refcnt=%d", pp->pp_refcnt); 1980145256Sjkoshy 1981145256Sjkoshy /* 1982145256Sjkoshy * If the target process record shows that no 1983145256Sjkoshy * PMCs are attached to it, reclaim its space. 1984145256Sjkoshy */ 1985145256Sjkoshy 1986145256Sjkoshy if (pp->pp_refcnt == 0) { 1987145256Sjkoshy pmc_remove_process_descriptor(pp); 1988145256Sjkoshy FREE(pp, M_PMC); 1989145256Sjkoshy } 1990145256Sjkoshy } 1991145256Sjkoshy 1992145256Sjkoshy cpu = curthread->td_oncpu; /* setup cpu for pmd_release() */ 1993145256Sjkoshy 1994145256Sjkoshy } 1995145256Sjkoshy 1996145256Sjkoshy /* 1997145256Sjkoshy * Release any MD resources 1998145256Sjkoshy */ 1999145256Sjkoshy 2000145256Sjkoshy (void) md->pmd_release_pmc(cpu, ri, pm); 2001145256Sjkoshy 2002145256Sjkoshy /* 2003145256Sjkoshy * Update row disposition 2004145256Sjkoshy */ 2005145256Sjkoshy 2006145774Sjkoshy if (PMC_IS_SYSTEM_MODE(PMC_TO_MODE(pm))) 2007145256Sjkoshy PMC_UNMARK_ROW_STANDALONE(ri); 2008145256Sjkoshy else 2009145256Sjkoshy PMC_UNMARK_ROW_THREAD(ri); 2010145256Sjkoshy 2011145256Sjkoshy /* unlink from the owner's list */ 2012145256Sjkoshy if (pm->pm_owner) 2013145256Sjkoshy pmc_unlink_owner(pm); 2014145256Sjkoshy 2015145256Sjkoshy pmc_destroy_pmc_descriptor(pm); 2016145256Sjkoshy} 2017145256Sjkoshy 2018145256Sjkoshy/* 2019145256Sjkoshy * Register an owner and a pmc. 2020145256Sjkoshy */ 2021145256Sjkoshy 2022145256Sjkoshystatic int 2023145256Sjkoshypmc_register_owner(struct proc *p, struct pmc *pmc) 2024145256Sjkoshy{ 2025145256Sjkoshy struct pmc_list *pl; 2026145256Sjkoshy struct pmc_owner *po; 2027145256Sjkoshy 2028145256Sjkoshy sx_assert(&pmc_sx, SX_XLOCKED); 2029145256Sjkoshy 2030145256Sjkoshy MALLOC(pl, struct pmc_list *, sizeof(struct pmc_list), M_PMC, 2031145256Sjkoshy M_WAITOK); 2032145256Sjkoshy 2033145256Sjkoshy if (pl == NULL) 2034145256Sjkoshy return ENOMEM; 2035145256Sjkoshy 2036145774Sjkoshy if ((po = pmc_find_owner_descriptor(p)) == NULL) 2037145256Sjkoshy if ((po = pmc_allocate_owner_descriptor(p)) == NULL) { 2038145256Sjkoshy FREE(pl, M_PMC); 2039145256Sjkoshy return ENOMEM; 2040145256Sjkoshy } 2041145256Sjkoshy 2042145774Sjkoshy /* XXX is this too restrictive */ 2043145774Sjkoshy if (PMC_ID_TO_MODE(pmc->pm_id) == PMC_MODE_TS) { 2044145256Sjkoshy /* can have only one TS mode PMC per process */ 2045145774Sjkoshy if (po->po_flags & PMC_PO_HAS_TS_PMC) { 2046145256Sjkoshy FREE(pl, M_PMC); 2047145256Sjkoshy return EINVAL; 2048145256Sjkoshy } 2049145774Sjkoshy po->po_flags |= PMC_PO_HAS_TS_PMC; 2050145256Sjkoshy } 2051145256Sjkoshy 2052145256Sjkoshy KASSERT(pmc->pm_owner == NULL, 2053145256Sjkoshy ("[pmc,%d] attempting to own an initialized PMC", __LINE__)); 2054145256Sjkoshy pmc->pm_owner = po; 2055145256Sjkoshy 2056145256Sjkoshy pl->pl_pmc = pmc; 2057145256Sjkoshy 2058145256Sjkoshy LIST_INSERT_HEAD(&po->po_pmcs, pl, pl_next); 2059145256Sjkoshy 2060145256Sjkoshy PROC_LOCK(p); 2061145256Sjkoshy p->p_flag |= P_HWPMC; 2062145256Sjkoshy PROC_UNLOCK(p); 2063145256Sjkoshy 2064145256Sjkoshy PMCDBG(PMC,REG,1, "register-owner pmc-owner=%p pl=%p pmc=%p", 2065145256Sjkoshy po, pl, pmc); 2066145256Sjkoshy 2067145256Sjkoshy return 0; 2068145256Sjkoshy} 2069145256Sjkoshy 2070145256Sjkoshy/* 2071145256Sjkoshy * Return the current row disposition: 2072145256Sjkoshy * == 0 => FREE 2073145256Sjkoshy * > 0 => PROCESS MODE 2074145256Sjkoshy * < 0 => SYSTEM MODE 2075145256Sjkoshy */ 2076145256Sjkoshy 2077145256Sjkoshyint 2078145256Sjkoshypmc_getrowdisp(int ri) 2079145256Sjkoshy{ 2080145256Sjkoshy return pmc_pmcdisp[ri]; 2081145256Sjkoshy} 2082145256Sjkoshy 2083145256Sjkoshy/* 2084145256Sjkoshy * Check if a PMC at row index 'ri' can be allocated to the current 2085145256Sjkoshy * process. 2086145256Sjkoshy * 2087145256Sjkoshy * Allocation can fail if: 2088145256Sjkoshy * - the current process is already being profiled by a PMC at index 'ri', 2089145256Sjkoshy * attached to it via OP_PMCATTACH. 2090145256Sjkoshy * - the current process has already allocated a PMC at index 'ri' 2091145256Sjkoshy * via OP_ALLOCATE. 2092145256Sjkoshy */ 2093145256Sjkoshy 2094145256Sjkoshystatic int 2095145774Sjkoshypmc_can_allocate_rowindex(struct proc *p, unsigned int ri, int cpu) 2096145256Sjkoshy{ 2097145774Sjkoshy enum pmc_mode mode; 2098145774Sjkoshy struct pmc *pm; 2099145256Sjkoshy struct pmc_list *pl; 2100145256Sjkoshy struct pmc_owner *po; 2101145256Sjkoshy struct pmc_process *pp; 2102145256Sjkoshy 2103145774Sjkoshy PMCDBG(PMC,ALR,1, "can-allocate-rowindex proc=%p (%d, %s) ri=%d " 2104145774Sjkoshy "cpu=%d", p, p->p_pid, p->p_comm, ri, cpu); 2105145256Sjkoshy 2106145774Sjkoshy /* 2107145774Sjkoshy * We shouldn't have already allocated a process-mode PMC at 2108145774Sjkoshy * row index 'ri'. 2109145774Sjkoshy * 2110145774Sjkoshy * We shouldn't have allocated a system-wide PMC on the same 2111145774Sjkoshy * CPU and same RI. 2112145774Sjkoshy */ 2113145256Sjkoshy if ((po = pmc_find_owner_descriptor(p)) != NULL) 2114145774Sjkoshy LIST_FOREACH(pl, &po->po_pmcs, pl_next) { 2115145774Sjkoshy pm = pl->pl_pmc; 2116145774Sjkoshy if (PMC_TO_ROWINDEX(pm) == ri) { 2117145774Sjkoshy mode = PMC_TO_MODE(pm); 2118145774Sjkoshy if (PMC_IS_VIRTUAL_MODE(mode)) 2119145774Sjkoshy return EEXIST; 2120145774Sjkoshy if (PMC_IS_SYSTEM_MODE(mode) && 2121145774Sjkoshy (int) PMC_TO_CPU(pm) == cpu) 2122145774Sjkoshy return EEXIST; 2123145774Sjkoshy } 2124145774Sjkoshy } 2125145256Sjkoshy 2126145774Sjkoshy /* 2127145774Sjkoshy * We also shouldn't be the target of any PMC at this index 2128145774Sjkoshy * since otherwise a PMC_ATTACH to ourselves will fail. 2129145774Sjkoshy */ 2130145256Sjkoshy if ((pp = pmc_find_process_descriptor(p, 0)) != NULL) 2131145256Sjkoshy if (pp->pp_pmcs[ri].pp_pmc) 2132145256Sjkoshy return EEXIST; 2133145256Sjkoshy 2134145256Sjkoshy PMCDBG(PMC,ALR,2, "can-allocate-rowindex proc=%p (%d, %s) ri=%d ok", 2135145256Sjkoshy p, p->p_pid, p->p_comm, ri); 2136145256Sjkoshy 2137145256Sjkoshy return 0; 2138145256Sjkoshy} 2139145256Sjkoshy 2140145256Sjkoshy/* 2141145256Sjkoshy * Check if a given PMC at row index 'ri' can be currently used in 2142145256Sjkoshy * mode 'mode'. 2143145256Sjkoshy */ 2144145256Sjkoshy 2145145256Sjkoshystatic int 2146145256Sjkoshypmc_can_allocate_row(int ri, enum pmc_mode mode) 2147145256Sjkoshy{ 2148145256Sjkoshy enum pmc_disp disp; 2149145256Sjkoshy 2150145256Sjkoshy sx_assert(&pmc_sx, SX_XLOCKED); 2151145256Sjkoshy 2152145256Sjkoshy PMCDBG(PMC,ALR,1, "can-allocate-row ri=%d mode=%d", ri, mode); 2153145256Sjkoshy 2154145256Sjkoshy if (PMC_IS_SYSTEM_MODE(mode)) 2155145256Sjkoshy disp = PMC_DISP_STANDALONE; 2156145256Sjkoshy else 2157145256Sjkoshy disp = PMC_DISP_THREAD; 2158145256Sjkoshy 2159145256Sjkoshy /* 2160145256Sjkoshy * check disposition for PMC row 'ri': 2161145256Sjkoshy * 2162145256Sjkoshy * Expected disposition Row-disposition Result 2163145256Sjkoshy * 2164145256Sjkoshy * STANDALONE STANDALONE or FREE proceed 2165145256Sjkoshy * STANDALONE THREAD fail 2166145256Sjkoshy * THREAD THREAD or FREE proceed 2167145256Sjkoshy * THREAD STANDALONE fail 2168145256Sjkoshy */ 2169145256Sjkoshy 2170145256Sjkoshy if (!PMC_ROW_DISP_IS_FREE(ri) && 2171145256Sjkoshy !(disp == PMC_DISP_THREAD && PMC_ROW_DISP_IS_THREAD(ri)) && 2172145256Sjkoshy !(disp == PMC_DISP_STANDALONE && PMC_ROW_DISP_IS_STANDALONE(ri))) 2173145256Sjkoshy return EBUSY; 2174145256Sjkoshy 2175145256Sjkoshy /* 2176145256Sjkoshy * All OK 2177145256Sjkoshy */ 2178145256Sjkoshy 2179145256Sjkoshy PMCDBG(PMC,ALR,2, "can-allocate-row ri=%d mode=%d ok", ri, mode); 2180145256Sjkoshy 2181145256Sjkoshy return 0; 2182145256Sjkoshy 2183145256Sjkoshy} 2184145256Sjkoshy 2185145256Sjkoshy/* 2186145774Sjkoshy * Find a PMC descriptor with user handle 'pmcid' for thread 'td'. 2187145256Sjkoshy */ 2188145256Sjkoshy 2189145256Sjkoshystatic struct pmc * 2190145256Sjkoshypmc_find_pmc_descriptor_in_process(struct pmc_owner *po, pmc_id_t pmcid) 2191145256Sjkoshy{ 2192145256Sjkoshy struct pmc_list *pl; 2193145256Sjkoshy 2194145774Sjkoshy KASSERT(PMC_ID_TO_ROWINDEX(pmcid) < md->pmd_npmc, 2195145774Sjkoshy ("[pmc,%d] Illegal pmc index %d (max %d)", __LINE__, 2196145774Sjkoshy PMC_ID_TO_ROWINDEX(pmcid), md->pmd_npmc)); 2197145256Sjkoshy 2198145256Sjkoshy LIST_FOREACH(pl, &po->po_pmcs, pl_next) 2199145774Sjkoshy if (pl->pl_pmc->pm_id == pmcid) 2200145256Sjkoshy return pl->pl_pmc; 2201145256Sjkoshy 2202145256Sjkoshy return NULL; 2203145256Sjkoshy} 2204145256Sjkoshy 2205145256Sjkoshystatic int 2206145256Sjkoshypmc_find_pmc(pmc_id_t pmcid, struct pmc **pmc) 2207145256Sjkoshy{ 2208145256Sjkoshy 2209145256Sjkoshy struct pmc *pm; 2210145256Sjkoshy struct pmc_owner *po; 2211145256Sjkoshy 2212145256Sjkoshy PMCDBG(PMC,FND,1, "find-pmc id=%d", pmcid); 2213145256Sjkoshy 2214145256Sjkoshy if ((po = pmc_find_owner_descriptor(curthread->td_proc)) == NULL) 2215145256Sjkoshy return ESRCH; 2216145256Sjkoshy 2217145256Sjkoshy if ((pm = pmc_find_pmc_descriptor_in_process(po, pmcid)) == NULL) 2218145256Sjkoshy return EINVAL; 2219145256Sjkoshy 2220145256Sjkoshy PMCDBG(PMC,FND,2, "find-pmc id=%d -> pmc=%p", pmcid, pm); 2221145256Sjkoshy 2222145256Sjkoshy *pmc = pm; 2223145256Sjkoshy return 0; 2224145256Sjkoshy} 2225145256Sjkoshy 2226145256Sjkoshy/* 2227145256Sjkoshy * Start a PMC. 2228145256Sjkoshy */ 2229145256Sjkoshy 2230145256Sjkoshystatic int 2231145256Sjkoshypmc_start(struct pmc *pm) 2232145256Sjkoshy{ 2233145256Sjkoshy int error, cpu, ri; 2234145774Sjkoshy enum pmc_mode mode; 2235145256Sjkoshy struct pmc_binding pb; 2236145256Sjkoshy 2237145256Sjkoshy KASSERT(pm != NULL, 2238145256Sjkoshy ("[pmc,%d] null pm", __LINE__)); 2239145256Sjkoshy 2240145774Sjkoshy mode = PMC_TO_MODE(pm); 2241145774Sjkoshy ri = PMC_TO_ROWINDEX(pm); 2242145774Sjkoshy error = 0; 2243145256Sjkoshy 2244145774Sjkoshy PMCDBG(PMC,OPS,1, "start pmc=%p mode=%d ri=%d", pm, mode, ri); 2245145774Sjkoshy 2246145256Sjkoshy pm->pm_state = PMC_STATE_RUNNING; 2247145256Sjkoshy 2248145774Sjkoshy if (PMC_IS_VIRTUAL_MODE(mode)) { 2249145256Sjkoshy 2250145256Sjkoshy /* 2251145256Sjkoshy * If a PMCATTACH hadn't been done on this 2252145256Sjkoshy * PMC, attach this PMC to its owner process. 2253145256Sjkoshy */ 2254145256Sjkoshy 2255145256Sjkoshy if (LIST_EMPTY(&pm->pm_targets)) 2256145774Sjkoshy error = pmc_attach_process(pm->pm_owner->po_owner, pm); 2257145256Sjkoshy 2258145774Sjkoshy /* 2259145774Sjkoshy * If the PMC is attached to its owner, then force a context 2260145774Sjkoshy * switch to ensure that the MD state gets set correctly. 2261145774Sjkoshy */ 2262145774Sjkoshy if (error == 0 && (pm->pm_flags & PMC_F_ATTACHED_TO_OWNER)) 2263145774Sjkoshy pmc_force_context_switch(); 2264145256Sjkoshy 2265145256Sjkoshy /* 2266145256Sjkoshy * Nothing further to be done; thread context switch code 2267145774Sjkoshy * will start/stop the hardware as appropriate. 2268145256Sjkoshy */ 2269145256Sjkoshy 2270145774Sjkoshy return error; 2271145256Sjkoshy 2272145256Sjkoshy } 2273145256Sjkoshy 2274145256Sjkoshy /* 2275145774Sjkoshy * A system-wide PMC. Move to the CPU associated with this 2276145256Sjkoshy * PMC, and start the hardware. 2277145256Sjkoshy */ 2278145256Sjkoshy 2279145256Sjkoshy pmc_save_cpu_binding(&pb); 2280145256Sjkoshy 2281145774Sjkoshy cpu = PMC_TO_CPU(pm); 2282145256Sjkoshy 2283145256Sjkoshy if (pmc_cpu_is_disabled(cpu)) 2284145256Sjkoshy return ENXIO; 2285145256Sjkoshy 2286145256Sjkoshy pmc_select_cpu(cpu); 2287145256Sjkoshy 2288145256Sjkoshy /* 2289145256Sjkoshy * global PMCs are configured at allocation time 2290145256Sjkoshy * so write out the initial value and start the PMC. 2291145256Sjkoshy */ 2292145256Sjkoshy 2293145774Sjkoshy critical_enter(); 2294145256Sjkoshy if ((error = md->pmd_write_pmc(cpu, ri, 2295145774Sjkoshy PMC_IS_SAMPLING_MODE(mode) ? 2296145256Sjkoshy pm->pm_sc.pm_reloadcount : 2297145256Sjkoshy pm->pm_sc.pm_initial)) == 0) 2298145256Sjkoshy error = md->pmd_start_pmc(cpu, ri); 2299145774Sjkoshy critical_exit(); 2300145256Sjkoshy 2301145256Sjkoshy pmc_restore_cpu_binding(&pb); 2302145256Sjkoshy 2303145256Sjkoshy return error; 2304145256Sjkoshy} 2305145256Sjkoshy 2306145256Sjkoshy/* 2307145256Sjkoshy * Stop a PMC. 2308145256Sjkoshy */ 2309145256Sjkoshy 2310145256Sjkoshystatic int 2311145256Sjkoshypmc_stop(struct pmc *pm) 2312145256Sjkoshy{ 2313145774Sjkoshy int cpu, error, ri; 2314145256Sjkoshy struct pmc_binding pb; 2315145256Sjkoshy 2316145256Sjkoshy KASSERT(pm != NULL, ("[pmc,%d] null pmc", __LINE__)); 2317145256Sjkoshy 2318145774Sjkoshy PMCDBG(PMC,OPS,1, "stop pmc=%p mode=%d ri=%d", pm, 2319145774Sjkoshy PMC_TO_MODE(pm), PMC_TO_ROWINDEX(pm)); 2320145256Sjkoshy 2321145256Sjkoshy pm->pm_state = PMC_STATE_STOPPED; 2322145256Sjkoshy 2323145256Sjkoshy /* 2324145256Sjkoshy * If the PMC is a virtual mode one, changing the state to 2325145256Sjkoshy * non-RUNNING is enough to ensure that the PMC never gets 2326145256Sjkoshy * scheduled. 2327145256Sjkoshy * 2328145256Sjkoshy * If this PMC is current running on a CPU, then it will 2329145256Sjkoshy * handled correctly at the time its target process is context 2330145256Sjkoshy * switched out. 2331145256Sjkoshy */ 2332145256Sjkoshy 2333145774Sjkoshy if (PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm))) 2334145256Sjkoshy return 0; 2335145256Sjkoshy 2336145256Sjkoshy /* 2337145256Sjkoshy * A system-mode PMC. Move to the CPU associated with 2338145256Sjkoshy * this PMC, and stop the hardware. We update the 2339145256Sjkoshy * 'initial count' so that a subsequent PMCSTART will 2340145256Sjkoshy * resume counting from the current hardware count. 2341145256Sjkoshy */ 2342145256Sjkoshy 2343145256Sjkoshy pmc_save_cpu_binding(&pb); 2344145256Sjkoshy 2345145774Sjkoshy cpu = PMC_TO_CPU(pm); 2346145256Sjkoshy 2347145774Sjkoshy KASSERT(cpu >= 0 && cpu < mp_ncpus, 2348145774Sjkoshy ("[pmc,%d] illegal cpu=%d", __LINE__, cpu)); 2349145774Sjkoshy 2350145256Sjkoshy if (pmc_cpu_is_disabled(cpu)) 2351145256Sjkoshy return ENXIO; 2352145256Sjkoshy 2353145256Sjkoshy pmc_select_cpu(cpu); 2354145256Sjkoshy 2355145774Sjkoshy ri = PMC_TO_ROWINDEX(pm); 2356145256Sjkoshy 2357145774Sjkoshy critical_enter(); 2358145774Sjkoshy if ((error = md->pmd_stop_pmc(cpu, ri)) == 0) 2359145774Sjkoshy error = md->pmd_read_pmc(cpu, ri, &pm->pm_sc.pm_initial); 2360145774Sjkoshy critical_exit(); 2361145774Sjkoshy 2362145256Sjkoshy pmc_restore_cpu_binding(&pb); 2363145256Sjkoshy 2364145256Sjkoshy return error; 2365145256Sjkoshy} 2366145256Sjkoshy 2367145256Sjkoshy 2368145256Sjkoshy#if DEBUG 2369145256Sjkoshystatic const char *pmc_op_to_name[] = { 2370145256Sjkoshy#undef __PMC_OP 2371145256Sjkoshy#define __PMC_OP(N, D) #N , 2372145256Sjkoshy __PMC_OPS() 2373145256Sjkoshy NULL 2374145256Sjkoshy}; 2375145256Sjkoshy#endif 2376145256Sjkoshy 2377145256Sjkoshy/* 2378145256Sjkoshy * The syscall interface 2379145256Sjkoshy */ 2380145256Sjkoshy 2381145256Sjkoshy#define PMC_GET_SX_XLOCK(...) do { \ 2382145256Sjkoshy sx_xlock(&pmc_sx); \ 2383145256Sjkoshy if (pmc_hook == NULL) { \ 2384145256Sjkoshy sx_xunlock(&pmc_sx); \ 2385145256Sjkoshy return __VA_ARGS__; \ 2386145256Sjkoshy } \ 2387145256Sjkoshy} while (0) 2388145256Sjkoshy 2389145256Sjkoshy#define PMC_DOWNGRADE_SX() do { \ 2390145256Sjkoshy sx_downgrade(&pmc_sx); \ 2391145256Sjkoshy is_sx_downgraded = 1; \ 2392145256Sjkoshy} while (0) 2393145256Sjkoshy 2394145256Sjkoshystatic int 2395145256Sjkoshypmc_syscall_handler(struct thread *td, void *syscall_args) 2396145256Sjkoshy{ 2397145256Sjkoshy int error, is_sx_downgraded, op; 2398145256Sjkoshy struct pmc_syscall_args *c; 2399145256Sjkoshy void *arg; 2400145256Sjkoshy 2401145256Sjkoshy PMC_GET_SX_XLOCK(ENOSYS); 2402145256Sjkoshy 2403145256Sjkoshy is_sx_downgraded = 0; 2404145256Sjkoshy 2405145256Sjkoshy c = (struct pmc_syscall_args *) syscall_args; 2406145256Sjkoshy 2407145256Sjkoshy op = c->pmop_code; 2408145256Sjkoshy arg = c->pmop_data; 2409145256Sjkoshy 2410145256Sjkoshy PMCDBG(MOD,PMS,1, "syscall op=%d \"%s\" arg=%p", op, 2411145256Sjkoshy pmc_op_to_name[op], arg); 2412145256Sjkoshy 2413145256Sjkoshy error = 0; 2414145256Sjkoshy atomic_add_int(&pmc_stats.pm_syscalls, 1); 2415145256Sjkoshy 2416145256Sjkoshy switch(op) 2417145256Sjkoshy { 2418145256Sjkoshy 2419145256Sjkoshy 2420145256Sjkoshy /* 2421145256Sjkoshy * Configure a log file. 2422145256Sjkoshy * 2423145256Sjkoshy * XXX This OP will be reworked. 2424145256Sjkoshy */ 2425145256Sjkoshy 2426145256Sjkoshy case PMC_OP_CONFIGURELOG: 2427145256Sjkoshy { 2428145256Sjkoshy struct pmc_owner *po; 2429145256Sjkoshy struct pmc_op_configurelog cl; 2430145256Sjkoshy struct proc *p; 2431145256Sjkoshy 2432145256Sjkoshy sx_assert(&pmc_sx, SX_XLOCKED); 2433145256Sjkoshy 2434145256Sjkoshy if ((error = copyin(arg, &cl, sizeof(cl))) != 0) 2435145256Sjkoshy break; 2436145256Sjkoshy 2437145256Sjkoshy /* mark this process as owning a log file */ 2438145256Sjkoshy p = td->td_proc; 2439145256Sjkoshy if ((po = pmc_find_owner_descriptor(p)) == NULL) 2440145256Sjkoshy if ((po = pmc_allocate_owner_descriptor(p)) == NULL) 2441145256Sjkoshy return ENOMEM; 2442145256Sjkoshy 2443145256Sjkoshy if ((error = pmc_configure_log(po, cl.pm_logfd)) != 0) 2444145256Sjkoshy break; 2445145256Sjkoshy 2446145256Sjkoshy } 2447145256Sjkoshy break; 2448145256Sjkoshy 2449145256Sjkoshy 2450145256Sjkoshy /* 2451145256Sjkoshy * Retrieve hardware configuration. 2452145256Sjkoshy */ 2453145256Sjkoshy 2454145256Sjkoshy case PMC_OP_GETCPUINFO: /* CPU information */ 2455145256Sjkoshy { 2456145256Sjkoshy struct pmc_op_getcpuinfo gci; 2457145256Sjkoshy 2458145256Sjkoshy gci.pm_cputype = md->pmd_cputype; 2459145774Sjkoshy gci.pm_ncpu = mp_ncpus; 2460145256Sjkoshy gci.pm_npmc = md->pmd_npmc; 2461145256Sjkoshy gci.pm_nclass = md->pmd_nclass; 2462145256Sjkoshy bcopy(md->pmd_classes, &gci.pm_classes, 2463145256Sjkoshy sizeof(gci.pm_classes)); 2464145256Sjkoshy error = copyout(&gci, arg, sizeof(gci)); 2465145256Sjkoshy } 2466145256Sjkoshy break; 2467145256Sjkoshy 2468145256Sjkoshy 2469145256Sjkoshy /* 2470145256Sjkoshy * Get module statistics 2471145256Sjkoshy */ 2472145256Sjkoshy 2473145256Sjkoshy case PMC_OP_GETDRIVERSTATS: 2474145256Sjkoshy { 2475145256Sjkoshy struct pmc_op_getdriverstats gms; 2476145256Sjkoshy 2477145256Sjkoshy bcopy(&pmc_stats, &gms, sizeof(gms)); 2478145256Sjkoshy error = copyout(&gms, arg, sizeof(gms)); 2479145256Sjkoshy } 2480145256Sjkoshy break; 2481145256Sjkoshy 2482145256Sjkoshy 2483145256Sjkoshy /* 2484145256Sjkoshy * Retrieve module version number 2485145256Sjkoshy */ 2486145256Sjkoshy 2487145256Sjkoshy case PMC_OP_GETMODULEVERSION: 2488145256Sjkoshy { 2489145256Sjkoshy error = copyout(&_pmc_version.mv_version, arg, sizeof(int)); 2490145256Sjkoshy } 2491145256Sjkoshy break; 2492145256Sjkoshy 2493145256Sjkoshy 2494145256Sjkoshy /* 2495145256Sjkoshy * Retrieve the state of all the PMCs on a given 2496145256Sjkoshy * CPU. 2497145256Sjkoshy */ 2498145256Sjkoshy 2499145256Sjkoshy case PMC_OP_GETPMCINFO: 2500145256Sjkoshy { 2501145256Sjkoshy uint32_t cpu, n, npmc; 2502145256Sjkoshy size_t pmcinfo_size; 2503145256Sjkoshy struct pmc *pm; 2504145256Sjkoshy struct pmc_info *p, *pmcinfo; 2505145256Sjkoshy struct pmc_op_getpmcinfo *gpi; 2506145256Sjkoshy struct pmc_owner *po; 2507145256Sjkoshy struct pmc_binding pb; 2508145256Sjkoshy 2509145256Sjkoshy PMC_DOWNGRADE_SX(); 2510145256Sjkoshy 2511145256Sjkoshy gpi = (struct pmc_op_getpmcinfo *) arg; 2512145256Sjkoshy 2513145256Sjkoshy if ((error = copyin(&gpi->pm_cpu, &cpu, sizeof(cpu))) != 0) 2514145256Sjkoshy break; 2515145256Sjkoshy 2516145256Sjkoshy if (cpu >= (unsigned int) mp_ncpus) { 2517145256Sjkoshy error = EINVAL; 2518145256Sjkoshy break; 2519145256Sjkoshy } 2520145256Sjkoshy 2521145256Sjkoshy if (pmc_cpu_is_disabled(cpu)) { 2522145256Sjkoshy error = ENXIO; 2523145256Sjkoshy break; 2524145256Sjkoshy } 2525145256Sjkoshy 2526145256Sjkoshy /* switch to CPU 'cpu' */ 2527145256Sjkoshy pmc_save_cpu_binding(&pb); 2528145256Sjkoshy pmc_select_cpu(cpu); 2529145256Sjkoshy 2530145256Sjkoshy npmc = md->pmd_npmc; 2531145256Sjkoshy 2532145256Sjkoshy pmcinfo_size = npmc * sizeof(struct pmc_info); 2533145256Sjkoshy MALLOC(pmcinfo, struct pmc_info *, pmcinfo_size, M_PMC, 2534145256Sjkoshy M_WAITOK); 2535145256Sjkoshy 2536145256Sjkoshy p = pmcinfo; 2537145256Sjkoshy 2538145256Sjkoshy for (n = 0; n < md->pmd_npmc; n++, p++) { 2539145256Sjkoshy 2540145256Sjkoshy if ((error = md->pmd_describe(cpu, n, p, &pm)) != 0) 2541145256Sjkoshy break; 2542145256Sjkoshy 2543145256Sjkoshy if (PMC_ROW_DISP_IS_STANDALONE(n)) 2544145256Sjkoshy p->pm_rowdisp = PMC_DISP_STANDALONE; 2545145256Sjkoshy else if (PMC_ROW_DISP_IS_THREAD(n)) 2546145256Sjkoshy p->pm_rowdisp = PMC_DISP_THREAD; 2547145256Sjkoshy else 2548145256Sjkoshy p->pm_rowdisp = PMC_DISP_FREE; 2549145256Sjkoshy 2550145256Sjkoshy p->pm_ownerpid = -1; 2551145256Sjkoshy 2552145256Sjkoshy if (pm == NULL) /* no PMC associated */ 2553145256Sjkoshy continue; 2554145256Sjkoshy 2555145256Sjkoshy po = pm->pm_owner; 2556145256Sjkoshy 2557145256Sjkoshy KASSERT(po->po_owner != NULL, 2558145256Sjkoshy ("[pmc,%d] pmc_owner had a null proc pointer", 2559145256Sjkoshy __LINE__)); 2560145256Sjkoshy 2561145256Sjkoshy p->pm_ownerpid = po->po_owner->p_pid; 2562145774Sjkoshy p->pm_mode = PMC_TO_MODE(pm); 2563145256Sjkoshy p->pm_event = pm->pm_event; 2564145256Sjkoshy p->pm_flags = pm->pm_flags; 2565145256Sjkoshy 2566145774Sjkoshy if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm))) 2567145256Sjkoshy p->pm_reloadcount = 2568145256Sjkoshy pm->pm_sc.pm_reloadcount; 2569145256Sjkoshy } 2570145256Sjkoshy 2571145256Sjkoshy pmc_restore_cpu_binding(&pb); 2572145256Sjkoshy 2573145256Sjkoshy /* now copy out the PMC info collected */ 2574145256Sjkoshy if (error == 0) 2575145256Sjkoshy error = copyout(pmcinfo, &gpi->pm_pmcs, pmcinfo_size); 2576145256Sjkoshy 2577145256Sjkoshy FREE(pmcinfo, M_PMC); 2578145256Sjkoshy } 2579145256Sjkoshy break; 2580145256Sjkoshy 2581145256Sjkoshy 2582145256Sjkoshy /* 2583145256Sjkoshy * Set the administrative state of a PMC. I.e. whether 2584145256Sjkoshy * the PMC is to be used or not. 2585145256Sjkoshy */ 2586145256Sjkoshy 2587145256Sjkoshy case PMC_OP_PMCADMIN: 2588145256Sjkoshy { 2589145256Sjkoshy int cpu, ri; 2590145256Sjkoshy enum pmc_state request; 2591145256Sjkoshy struct pmc_cpu *pc; 2592145256Sjkoshy struct pmc_hw *phw; 2593145256Sjkoshy struct pmc_op_pmcadmin pma; 2594145256Sjkoshy struct pmc_binding pb; 2595145256Sjkoshy 2596145256Sjkoshy sx_assert(&pmc_sx, SX_XLOCKED); 2597145256Sjkoshy 2598145256Sjkoshy KASSERT(td == curthread, 2599145256Sjkoshy ("[pmc,%d] td != curthread", __LINE__)); 2600145256Sjkoshy 2601145256Sjkoshy if (suser(td) || jailed(td->td_ucred)) { 2602145256Sjkoshy error = EPERM; 2603145256Sjkoshy break; 2604145256Sjkoshy } 2605145256Sjkoshy 2606145256Sjkoshy if ((error = copyin(arg, &pma, sizeof(pma))) != 0) 2607145256Sjkoshy break; 2608145256Sjkoshy 2609145256Sjkoshy cpu = pma.pm_cpu; 2610145256Sjkoshy 2611145256Sjkoshy if (cpu < 0 || cpu >= mp_ncpus) { 2612145256Sjkoshy error = EINVAL; 2613145256Sjkoshy break; 2614145256Sjkoshy } 2615145256Sjkoshy 2616145256Sjkoshy if (pmc_cpu_is_disabled(cpu)) { 2617145256Sjkoshy error = ENXIO; 2618145256Sjkoshy break; 2619145256Sjkoshy } 2620145256Sjkoshy 2621145256Sjkoshy request = pma.pm_state; 2622145256Sjkoshy 2623145256Sjkoshy if (request != PMC_STATE_DISABLED && 2624145256Sjkoshy request != PMC_STATE_FREE) { 2625145256Sjkoshy error = EINVAL; 2626145256Sjkoshy break; 2627145256Sjkoshy } 2628145256Sjkoshy 2629145256Sjkoshy ri = pma.pm_pmc; /* pmc id == row index */ 2630145256Sjkoshy if (ri < 0 || ri >= (int) md->pmd_npmc) { 2631145256Sjkoshy error = EINVAL; 2632145256Sjkoshy break; 2633145256Sjkoshy } 2634145256Sjkoshy 2635145256Sjkoshy /* 2636145256Sjkoshy * We can't disable a PMC with a row-index allocated 2637145256Sjkoshy * for process virtual PMCs. 2638145256Sjkoshy */ 2639145256Sjkoshy 2640145256Sjkoshy if (PMC_ROW_DISP_IS_THREAD(ri) && 2641145256Sjkoshy request == PMC_STATE_DISABLED) { 2642145256Sjkoshy error = EBUSY; 2643145256Sjkoshy break; 2644145256Sjkoshy } 2645145256Sjkoshy 2646145256Sjkoshy /* 2647145256Sjkoshy * otherwise, this PMC on this CPU is either free or 2648145256Sjkoshy * in system-wide mode. 2649145256Sjkoshy */ 2650145256Sjkoshy 2651145256Sjkoshy pmc_save_cpu_binding(&pb); 2652145256Sjkoshy pmc_select_cpu(cpu); 2653145256Sjkoshy 2654145256Sjkoshy pc = pmc_pcpu[cpu]; 2655145256Sjkoshy phw = pc->pc_hwpmcs[ri]; 2656145256Sjkoshy 2657145256Sjkoshy /* 2658145256Sjkoshy * XXX do we need some kind of 'forced' disable? 2659145256Sjkoshy */ 2660145256Sjkoshy 2661145256Sjkoshy if (phw->phw_pmc == NULL) { 2662145256Sjkoshy if (request == PMC_STATE_DISABLED && 2663145256Sjkoshy (phw->phw_state & PMC_PHW_FLAG_IS_ENABLED)) { 2664145256Sjkoshy phw->phw_state &= ~PMC_PHW_FLAG_IS_ENABLED; 2665145256Sjkoshy PMC_MARK_ROW_STANDALONE(ri); 2666145256Sjkoshy } else if (request == PMC_STATE_FREE && 2667145256Sjkoshy (phw->phw_state & PMC_PHW_FLAG_IS_ENABLED) == 0) { 2668145256Sjkoshy phw->phw_state |= PMC_PHW_FLAG_IS_ENABLED; 2669145256Sjkoshy PMC_UNMARK_ROW_STANDALONE(ri); 2670145256Sjkoshy } 2671145256Sjkoshy /* other cases are a no-op */ 2672145256Sjkoshy } else 2673145256Sjkoshy error = EBUSY; 2674145256Sjkoshy 2675145256Sjkoshy pmc_restore_cpu_binding(&pb); 2676145256Sjkoshy } 2677145256Sjkoshy break; 2678145256Sjkoshy 2679145256Sjkoshy 2680145256Sjkoshy /* 2681145256Sjkoshy * Allocate a PMC. 2682145256Sjkoshy */ 2683145256Sjkoshy 2684145256Sjkoshy case PMC_OP_PMCALLOCATE: 2685145256Sjkoshy { 2686145256Sjkoshy uint32_t caps; 2687145256Sjkoshy u_int cpu; 2688145256Sjkoshy int n; 2689145256Sjkoshy enum pmc_mode mode; 2690145256Sjkoshy struct pmc *pmc; 2691145774Sjkoshy struct pmc_hw *phw; 2692145256Sjkoshy struct pmc_op_pmcallocate pa; 2693145256Sjkoshy struct pmc_binding pb; 2694145256Sjkoshy 2695145256Sjkoshy if ((error = copyin(arg, &pa, sizeof(pa))) != 0) 2696145256Sjkoshy break; 2697145256Sjkoshy 2698145256Sjkoshy caps = pa.pm_caps; 2699145256Sjkoshy mode = pa.pm_mode; 2700145256Sjkoshy cpu = pa.pm_cpu; 2701145256Sjkoshy 2702145256Sjkoshy if ((mode != PMC_MODE_SS && mode != PMC_MODE_SC && 2703145256Sjkoshy mode != PMC_MODE_TS && mode != PMC_MODE_TC) || 2704145256Sjkoshy (cpu != (u_int) PMC_CPU_ANY && cpu >= (u_int) mp_ncpus)) { 2705145256Sjkoshy error = EINVAL; 2706145256Sjkoshy break; 2707145256Sjkoshy } 2708145256Sjkoshy 2709145256Sjkoshy /* 2710145256Sjkoshy * Virtual PMCs should only ask for a default CPU. 2711145256Sjkoshy * System mode PMCs need to specify a non-default CPU. 2712145256Sjkoshy */ 2713145256Sjkoshy 2714145256Sjkoshy if ((PMC_IS_VIRTUAL_MODE(mode) && cpu != (u_int) PMC_CPU_ANY) || 2715145256Sjkoshy (PMC_IS_SYSTEM_MODE(mode) && cpu == (u_int) PMC_CPU_ANY)) { 2716145256Sjkoshy error = EINVAL; 2717145256Sjkoshy break; 2718145256Sjkoshy } 2719145256Sjkoshy 2720145256Sjkoshy /* 2721145256Sjkoshy * Check that a disabled CPU is not being asked for. 2722145256Sjkoshy */ 2723145256Sjkoshy 2724145256Sjkoshy if (PMC_IS_SYSTEM_MODE(mode) && pmc_cpu_is_disabled(cpu)) { 2725145256Sjkoshy error = ENXIO; 2726145256Sjkoshy break; 2727145256Sjkoshy } 2728145256Sjkoshy 2729145256Sjkoshy /* 2730145256Sjkoshy * Refuse an allocation for a system-wide PMC if this 2731145256Sjkoshy * process has been jailed, or if this process lacks 2732145256Sjkoshy * super-user credentials and the sysctl tunable 2733145256Sjkoshy * 'security.bsd.unprivileged_syspmcs' is zero. 2734145256Sjkoshy */ 2735145256Sjkoshy 2736145256Sjkoshy if (PMC_IS_SYSTEM_MODE(mode)) { 2737145256Sjkoshy if (jailed(curthread->td_ucred)) 2738145256Sjkoshy error = EPERM; 2739145256Sjkoshy else if (suser(curthread) && 2740145256Sjkoshy (pmc_unprivileged_syspmcs == 0)) 2741145256Sjkoshy error = EPERM; 2742145256Sjkoshy } 2743145256Sjkoshy 2744145256Sjkoshy if (error) 2745145256Sjkoshy break; 2746145256Sjkoshy 2747145256Sjkoshy /* 2748145256Sjkoshy * Look for valid values for 'pm_flags' 2749145256Sjkoshy */ 2750145256Sjkoshy 2751145256Sjkoshy if ((pa.pm_flags & ~(PMC_F_DESCENDANTS|PMC_F_LOG_TC_CSW)) 2752145256Sjkoshy != 0) { 2753145256Sjkoshy error = EINVAL; 2754145256Sjkoshy break; 2755145256Sjkoshy } 2756145256Sjkoshy 2757145256Sjkoshy /* 2758145256Sjkoshy * All sampling mode PMCs need to be able to interrupt the 2759145256Sjkoshy * CPU. 2760145256Sjkoshy */ 2761145256Sjkoshy 2762145256Sjkoshy if (PMC_IS_SAMPLING_MODE(mode)) { 2763145256Sjkoshy caps |= PMC_CAP_INTERRUPT; 2764145256Sjkoshy error = ENOSYS; /* for snapshot 6 */ 2765145256Sjkoshy break; 2766145256Sjkoshy } 2767145256Sjkoshy 2768145256Sjkoshy PMCDBG(PMC,ALL,2, "event=%d caps=0x%x mode=%d cpu=%d", 2769145256Sjkoshy pa.pm_ev, caps, mode, cpu); 2770145256Sjkoshy 2771145256Sjkoshy pmc = pmc_allocate_pmc_descriptor(); 2772145774Sjkoshy pmc->pm_id = PMC_ID_MAKE_ID(cpu,pa.pm_mode,pa.pm_class, 2773145774Sjkoshy PMC_ID_INVALID); 2774145256Sjkoshy pmc->pm_event = pa.pm_ev; 2775145256Sjkoshy pmc->pm_state = PMC_STATE_FREE; 2776145256Sjkoshy pmc->pm_caps = caps; 2777145256Sjkoshy pmc->pm_flags = pa.pm_flags; 2778145256Sjkoshy 2779145256Sjkoshy /* switch thread to CPU 'cpu' */ 2780145256Sjkoshy pmc_save_cpu_binding(&pb); 2781145256Sjkoshy 2782145256Sjkoshy#define PMC_IS_SHAREABLE_PMC(cpu, n) \ 2783145256Sjkoshy (pmc_pcpu[(cpu)]->pc_hwpmcs[(n)]->phw_state & \ 2784145256Sjkoshy PMC_PHW_FLAG_IS_SHAREABLE) 2785145256Sjkoshy#define PMC_IS_UNALLOCATED(cpu, n) \ 2786145256Sjkoshy (pmc_pcpu[(cpu)]->pc_hwpmcs[(n)]->phw_pmc == NULL) 2787145256Sjkoshy 2788145256Sjkoshy if (PMC_IS_SYSTEM_MODE(mode)) { 2789145256Sjkoshy pmc_select_cpu(cpu); 2790145256Sjkoshy for (n = 0; n < (int) md->pmd_npmc; n++) 2791145256Sjkoshy if (pmc_can_allocate_row(n, mode) == 0 && 2792145256Sjkoshy pmc_can_allocate_rowindex( 2793145774Sjkoshy curthread->td_proc, n, cpu) == 0 && 2794145256Sjkoshy (PMC_IS_UNALLOCATED(cpu, n) || 2795145256Sjkoshy PMC_IS_SHAREABLE_PMC(cpu, n)) && 2796145256Sjkoshy md->pmd_allocate_pmc(cpu, n, pmc, 2797145256Sjkoshy &pa) == 0) 2798145256Sjkoshy break; 2799145256Sjkoshy } else { 2800145256Sjkoshy /* Process virtual mode */ 2801145256Sjkoshy for (n = 0; n < (int) md->pmd_npmc; n++) { 2802145256Sjkoshy if (pmc_can_allocate_row(n, mode) == 0 && 2803145256Sjkoshy pmc_can_allocate_rowindex( 2804145774Sjkoshy curthread->td_proc, n, 2805145774Sjkoshy PMC_CPU_ANY) == 0 && 2806145256Sjkoshy md->pmd_allocate_pmc(curthread->td_oncpu, 2807145256Sjkoshy n, pmc, &pa) == 0) 2808145256Sjkoshy break; 2809145256Sjkoshy } 2810145256Sjkoshy } 2811145256Sjkoshy 2812145256Sjkoshy#undef PMC_IS_UNALLOCATED 2813145256Sjkoshy#undef PMC_IS_SHAREABLE_PMC 2814145256Sjkoshy 2815145256Sjkoshy pmc_restore_cpu_binding(&pb); 2816145256Sjkoshy 2817145256Sjkoshy if (n == (int) md->pmd_npmc) { 2818145256Sjkoshy pmc_destroy_pmc_descriptor(pmc); 2819145256Sjkoshy FREE(pmc, M_PMC); 2820145256Sjkoshy pmc = NULL; 2821145256Sjkoshy error = EINVAL; 2822145256Sjkoshy break; 2823145256Sjkoshy } 2824145256Sjkoshy 2825145774Sjkoshy /* Fill in the correct value in the ID field */ 2826145774Sjkoshy pmc->pm_id = PMC_ID_MAKE_ID(cpu,mode,pa.pm_class,n); 2827145256Sjkoshy 2828145774Sjkoshy PMCDBG(PMC,ALL,2, "ev=%d class=%d mode=%d n=%d -> pmcid=%x", 2829145774Sjkoshy pmc->pm_event, pa.pm_class, mode, n, pmc->pm_id); 2830145774Sjkoshy 2831145256Sjkoshy /* 2832145256Sjkoshy * Configure global pmc's immediately 2833145256Sjkoshy */ 2834145256Sjkoshy 2835145774Sjkoshy if (PMC_IS_SYSTEM_MODE(PMC_TO_MODE(pmc))) { 2836145774Sjkoshy 2837145774Sjkoshy pmc_save_cpu_binding(&pb); 2838145774Sjkoshy pmc_select_cpu(cpu); 2839145774Sjkoshy 2840145774Sjkoshy phw = pmc_pcpu[cpu]->pc_hwpmcs[n]; 2841145774Sjkoshy 2842145774Sjkoshy if ((phw->phw_state & PMC_PHW_FLAG_IS_ENABLED) == 0 || 2843145774Sjkoshy (error = md->pmd_config_pmc(cpu, n, pmc)) != 0) { 2844145256Sjkoshy (void) md->pmd_release_pmc(cpu, n, pmc); 2845145256Sjkoshy pmc_destroy_pmc_descriptor(pmc); 2846145256Sjkoshy FREE(pmc, M_PMC); 2847145256Sjkoshy pmc = NULL; 2848145774Sjkoshy pmc_restore_cpu_binding(&pb); 2849145774Sjkoshy error = EPERM; 2850145256Sjkoshy break; 2851145256Sjkoshy } 2852145256Sjkoshy 2853145774Sjkoshy pmc_restore_cpu_binding(&pb); 2854145774Sjkoshy } 2855145256Sjkoshy 2856145256Sjkoshy pmc->pm_state = PMC_STATE_ALLOCATED; 2857145256Sjkoshy 2858145256Sjkoshy /* 2859145256Sjkoshy * mark row disposition 2860145256Sjkoshy */ 2861145256Sjkoshy 2862145256Sjkoshy if (PMC_IS_SYSTEM_MODE(mode)) 2863145256Sjkoshy PMC_MARK_ROW_STANDALONE(n); 2864145256Sjkoshy else 2865145256Sjkoshy PMC_MARK_ROW_THREAD(n); 2866145256Sjkoshy 2867145256Sjkoshy /* 2868145256Sjkoshy * Register this PMC with the current thread as its owner. 2869145256Sjkoshy */ 2870145256Sjkoshy 2871145256Sjkoshy if ((error = 2872145256Sjkoshy pmc_register_owner(curthread->td_proc, pmc)) != 0) { 2873145256Sjkoshy pmc_release_pmc_descriptor(pmc); 2874145256Sjkoshy FREE(pmc, M_PMC); 2875145256Sjkoshy pmc = NULL; 2876145256Sjkoshy break; 2877145256Sjkoshy } 2878145256Sjkoshy 2879145256Sjkoshy /* 2880145256Sjkoshy * Return the allocated index. 2881145256Sjkoshy */ 2882145256Sjkoshy 2883145774Sjkoshy pa.pm_pmcid = pmc->pm_id; 2884145256Sjkoshy 2885145256Sjkoshy error = copyout(&pa, arg, sizeof(pa)); 2886145256Sjkoshy } 2887145256Sjkoshy break; 2888145256Sjkoshy 2889145256Sjkoshy 2890145256Sjkoshy /* 2891145256Sjkoshy * Attach a PMC to a process. 2892145256Sjkoshy */ 2893145256Sjkoshy 2894145256Sjkoshy case PMC_OP_PMCATTACH: 2895145256Sjkoshy { 2896145256Sjkoshy struct pmc *pm; 2897145256Sjkoshy struct proc *p; 2898145256Sjkoshy struct pmc_op_pmcattach a; 2899145256Sjkoshy 2900145256Sjkoshy sx_assert(&pmc_sx, SX_XLOCKED); 2901145256Sjkoshy 2902145256Sjkoshy if ((error = copyin(arg, &a, sizeof(a))) != 0) 2903145256Sjkoshy break; 2904145256Sjkoshy 2905145256Sjkoshy if (a.pm_pid < 0) { 2906145256Sjkoshy error = EINVAL; 2907145256Sjkoshy break; 2908145256Sjkoshy } else if (a.pm_pid == 0) 2909145256Sjkoshy a.pm_pid = td->td_proc->p_pid; 2910145256Sjkoshy 2911145256Sjkoshy if ((error = pmc_find_pmc(a.pm_pmc, &pm)) != 0) 2912145256Sjkoshy break; 2913145256Sjkoshy 2914145774Sjkoshy if (PMC_IS_SYSTEM_MODE(PMC_TO_MODE(pm))) { 2915145256Sjkoshy error = EINVAL; 2916145256Sjkoshy break; 2917145256Sjkoshy } 2918145256Sjkoshy 2919145256Sjkoshy /* PMCs may be (re)attached only when allocated or stopped */ 2920145256Sjkoshy if (pm->pm_state == PMC_STATE_RUNNING) { 2921145256Sjkoshy error = EBUSY; 2922145256Sjkoshy break; 2923145256Sjkoshy } else if (pm->pm_state != PMC_STATE_ALLOCATED && 2924145256Sjkoshy pm->pm_state != PMC_STATE_STOPPED) { 2925145256Sjkoshy error = EINVAL; 2926145256Sjkoshy break; 2927145256Sjkoshy } 2928145256Sjkoshy 2929145256Sjkoshy /* lookup pid */ 2930145256Sjkoshy if ((p = pfind(a.pm_pid)) == NULL) { 2931145256Sjkoshy error = ESRCH; 2932145256Sjkoshy break; 2933145256Sjkoshy } 2934145256Sjkoshy 2935145256Sjkoshy /* 2936145256Sjkoshy * Ignore processes that are working on exiting. 2937145256Sjkoshy */ 2938145256Sjkoshy if (p->p_flag & P_WEXIT) { 2939145256Sjkoshy error = ESRCH; 2940145256Sjkoshy PROC_UNLOCK(p); /* pfind() returns a locked process */ 2941145256Sjkoshy break; 2942145256Sjkoshy } 2943145256Sjkoshy 2944145256Sjkoshy /* 2945145256Sjkoshy * we are allowed to attach a PMC to a process if 2946145256Sjkoshy * we can debug it. 2947145256Sjkoshy */ 2948145256Sjkoshy error = p_candebug(curthread, p); 2949145256Sjkoshy 2950145256Sjkoshy PROC_UNLOCK(p); 2951145256Sjkoshy 2952145256Sjkoshy if (error == 0) 2953145256Sjkoshy error = pmc_attach_process(p, pm); 2954145256Sjkoshy } 2955145256Sjkoshy break; 2956145256Sjkoshy 2957145256Sjkoshy 2958145256Sjkoshy /* 2959145256Sjkoshy * Detach an attached PMC from a process. 2960145256Sjkoshy */ 2961145256Sjkoshy 2962145256Sjkoshy case PMC_OP_PMCDETACH: 2963145256Sjkoshy { 2964145256Sjkoshy struct pmc *pm; 2965145256Sjkoshy struct proc *p; 2966145256Sjkoshy struct pmc_op_pmcattach a; 2967145256Sjkoshy 2968145256Sjkoshy if ((error = copyin(arg, &a, sizeof(a))) != 0) 2969145256Sjkoshy break; 2970145256Sjkoshy 2971145256Sjkoshy if (a.pm_pid < 0) { 2972145256Sjkoshy error = EINVAL; 2973145256Sjkoshy break; 2974145256Sjkoshy } else if (a.pm_pid == 0) 2975145256Sjkoshy a.pm_pid = td->td_proc->p_pid; 2976145256Sjkoshy 2977145256Sjkoshy if ((error = pmc_find_pmc(a.pm_pmc, &pm)) != 0) 2978145256Sjkoshy break; 2979145256Sjkoshy 2980145256Sjkoshy if ((p = pfind(a.pm_pid)) == NULL) { 2981145256Sjkoshy error = ESRCH; 2982145256Sjkoshy break; 2983145256Sjkoshy } 2984145256Sjkoshy 2985145256Sjkoshy /* 2986145256Sjkoshy * Treat processes that are in the process of exiting 2987145256Sjkoshy * as if they were not present. 2988145256Sjkoshy */ 2989145256Sjkoshy 2990145256Sjkoshy if (p->p_flag & P_WEXIT) 2991145256Sjkoshy error = ESRCH; 2992145256Sjkoshy 2993145256Sjkoshy PROC_UNLOCK(p); /* pfind() returns a locked process */ 2994145256Sjkoshy 2995145256Sjkoshy if (error == 0) 2996145256Sjkoshy error = pmc_detach_process(p, pm); 2997145256Sjkoshy } 2998145256Sjkoshy break; 2999145256Sjkoshy 3000145256Sjkoshy 3001145256Sjkoshy /* 3002145256Sjkoshy * Release an allocated PMC 3003145256Sjkoshy */ 3004145256Sjkoshy 3005145256Sjkoshy case PMC_OP_PMCRELEASE: 3006145256Sjkoshy { 3007145256Sjkoshy pmc_id_t pmcid; 3008145256Sjkoshy struct pmc *pm; 3009145256Sjkoshy struct pmc_owner *po; 3010145256Sjkoshy struct pmc_op_simple sp; 3011145256Sjkoshy 3012145256Sjkoshy /* 3013145256Sjkoshy * Find PMC pointer for the named PMC. 3014145256Sjkoshy * 3015145256Sjkoshy * Use pmc_release_pmc_descriptor() to switch off the 3016145256Sjkoshy * PMC, remove all its target threads, and remove the 3017145256Sjkoshy * PMC from its owner's list. 3018145256Sjkoshy * 3019145256Sjkoshy * Remove the owner record if this is the last PMC 3020145256Sjkoshy * owned. 3021145256Sjkoshy * 3022145256Sjkoshy * Free up space. 3023145256Sjkoshy */ 3024145256Sjkoshy 3025145256Sjkoshy if ((error = copyin(arg, &sp, sizeof(sp))) != 0) 3026145256Sjkoshy break; 3027145256Sjkoshy 3028145256Sjkoshy pmcid = sp.pm_pmcid; 3029145256Sjkoshy 3030145256Sjkoshy if ((error = pmc_find_pmc(pmcid, &pm)) != 0) 3031145256Sjkoshy break; 3032145256Sjkoshy 3033145256Sjkoshy po = pm->pm_owner; 3034145256Sjkoshy pmc_release_pmc_descriptor(pm); 3035145256Sjkoshy pmc_maybe_remove_owner(po); 3036145256Sjkoshy 3037145256Sjkoshy FREE(pm, M_PMC); 3038145256Sjkoshy } 3039145256Sjkoshy break; 3040145256Sjkoshy 3041145256Sjkoshy 3042145256Sjkoshy /* 3043145256Sjkoshy * Read and/or write a PMC. 3044145256Sjkoshy */ 3045145256Sjkoshy 3046145256Sjkoshy case PMC_OP_PMCRW: 3047145256Sjkoshy { 3048145256Sjkoshy uint32_t cpu, ri; 3049145256Sjkoshy struct pmc *pm; 3050145256Sjkoshy struct pmc_op_pmcrw *pprw; 3051145256Sjkoshy struct pmc_op_pmcrw prw; 3052145256Sjkoshy struct pmc_binding pb; 3053145256Sjkoshy pmc_value_t oldvalue; 3054145256Sjkoshy 3055145256Sjkoshy PMC_DOWNGRADE_SX(); 3056145256Sjkoshy 3057145256Sjkoshy if ((error = copyin(arg, &prw, sizeof(prw))) != 0) 3058145256Sjkoshy break; 3059145256Sjkoshy 3060145301Simp ri = 0; 3061145256Sjkoshy PMCDBG(PMC,OPS,1, "rw id=%d flags=0x%x", prw.pm_pmcid, 3062145256Sjkoshy prw.pm_flags); 3063145256Sjkoshy 3064145256Sjkoshy /* must have at least one flag set */ 3065145256Sjkoshy if ((prw.pm_flags & (PMC_F_OLDVALUE|PMC_F_NEWVALUE)) == 0) { 3066145256Sjkoshy error = EINVAL; 3067145256Sjkoshy break; 3068145256Sjkoshy } 3069145256Sjkoshy 3070145256Sjkoshy /* locate pmc descriptor */ 3071145256Sjkoshy if ((error = pmc_find_pmc(prw.pm_pmcid, &pm)) != 0) 3072145256Sjkoshy break; 3073145256Sjkoshy 3074145256Sjkoshy /* Can't read a PMC that hasn't been started. */ 3075145256Sjkoshy if (pm->pm_state != PMC_STATE_ALLOCATED && 3076145256Sjkoshy pm->pm_state != PMC_STATE_STOPPED && 3077145256Sjkoshy pm->pm_state != PMC_STATE_RUNNING) { 3078145256Sjkoshy error = EINVAL; 3079145256Sjkoshy break; 3080145256Sjkoshy } 3081145256Sjkoshy 3082145256Sjkoshy /* writing a new value is allowed only for 'STOPPED' pmcs */ 3083145256Sjkoshy if (pm->pm_state == PMC_STATE_RUNNING && 3084145256Sjkoshy (prw.pm_flags & PMC_F_NEWVALUE)) { 3085145256Sjkoshy error = EBUSY; 3086145256Sjkoshy break; 3087145256Sjkoshy } 3088145256Sjkoshy 3089145774Sjkoshy if (PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm))) { 3090145256Sjkoshy 3091145774Sjkoshy /* 3092145774Sjkoshy * If this PMC is attached to its owner (i.e., 3093145774Sjkoshy * the process requesting this operation) and 3094145774Sjkoshy * is running, then attempt to get an 3095145774Sjkoshy * upto-date reading from hardware for a READ. 3096145774Sjkoshy * Writes are only allowed when the PMC is 3097145774Sjkoshy * stopped, so only update the saved value 3098145774Sjkoshy * field. 3099145774Sjkoshy * 3100145774Sjkoshy * If the PMC is not running, or is not 3101145774Sjkoshy * attached to its owner, read/write to the 3102145774Sjkoshy * savedvalue field. 3103145774Sjkoshy */ 3104145774Sjkoshy 3105145774Sjkoshy ri = PMC_TO_ROWINDEX(pm); 3106145774Sjkoshy 3107145256Sjkoshy mtx_pool_lock_spin(pmc_mtxpool, pm); 3108145774Sjkoshy cpu = curthread->td_oncpu; 3109145774Sjkoshy 3110145774Sjkoshy if (prw.pm_flags & PMC_F_OLDVALUE) { 3111145774Sjkoshy if ((pm->pm_flags & PMC_F_ATTACHED_TO_OWNER) && 3112145774Sjkoshy (pm->pm_state == PMC_STATE_RUNNING)) 3113145774Sjkoshy error = (*md->pmd_read_pmc)(cpu, ri, 3114145774Sjkoshy &oldvalue); 3115145774Sjkoshy else 3116145774Sjkoshy oldvalue = pm->pm_gv.pm_savedvalue; 3117145774Sjkoshy } 3118145256Sjkoshy if (prw.pm_flags & PMC_F_NEWVALUE) 3119145256Sjkoshy pm->pm_gv.pm_savedvalue = prw.pm_value; 3120145774Sjkoshy 3121145256Sjkoshy mtx_pool_unlock_spin(pmc_mtxpool, pm); 3122145256Sjkoshy 3123145256Sjkoshy } else { /* System mode PMCs */ 3124145774Sjkoshy cpu = PMC_TO_CPU(pm); 3125145774Sjkoshy ri = PMC_TO_ROWINDEX(pm); 3126145256Sjkoshy 3127145256Sjkoshy if (pmc_cpu_is_disabled(cpu)) { 3128145256Sjkoshy error = ENXIO; 3129145256Sjkoshy break; 3130145256Sjkoshy } 3131145256Sjkoshy 3132145256Sjkoshy /* move this thread to CPU 'cpu' */ 3133145256Sjkoshy pmc_save_cpu_binding(&pb); 3134145256Sjkoshy pmc_select_cpu(cpu); 3135145256Sjkoshy 3136145774Sjkoshy critical_enter(); 3137145256Sjkoshy /* save old value */ 3138145256Sjkoshy if (prw.pm_flags & PMC_F_OLDVALUE) 3139145256Sjkoshy if ((error = (*md->pmd_read_pmc)(cpu, ri, 3140145256Sjkoshy &oldvalue))) 3141145256Sjkoshy goto error; 3142145256Sjkoshy /* write out new value */ 3143145256Sjkoshy if (prw.pm_flags & PMC_F_NEWVALUE) 3144145256Sjkoshy error = (*md->pmd_write_pmc)(cpu, ri, 3145145256Sjkoshy prw.pm_value); 3146145256Sjkoshy error: 3147145774Sjkoshy critical_exit(); 3148145256Sjkoshy pmc_restore_cpu_binding(&pb); 3149145256Sjkoshy if (error) 3150145256Sjkoshy break; 3151145256Sjkoshy } 3152145256Sjkoshy 3153145256Sjkoshy pprw = (struct pmc_op_pmcrw *) arg; 3154145256Sjkoshy 3155145256Sjkoshy#if DEBUG 3156145256Sjkoshy if (prw.pm_flags & PMC_F_NEWVALUE) 3157145256Sjkoshy PMCDBG(PMC,OPS,2, "rw id=%d new %jx -> old %jx", 3158145256Sjkoshy ri, prw.pm_value, oldvalue); 3159145256Sjkoshy else 3160145256Sjkoshy PMCDBG(PMC,OPS,2, "rw id=%d -> old %jx", ri, oldvalue); 3161145256Sjkoshy#endif 3162145256Sjkoshy 3163145256Sjkoshy /* return old value if requested */ 3164145256Sjkoshy if (prw.pm_flags & PMC_F_OLDVALUE) 3165145256Sjkoshy if ((error = copyout(&oldvalue, &pprw->pm_value, 3166145256Sjkoshy sizeof(prw.pm_value)))) 3167145256Sjkoshy break; 3168145256Sjkoshy 3169145256Sjkoshy /* 3170145256Sjkoshy * send a signal (SIGIO) to the owner if it is trying to read 3171145256Sjkoshy * a PMC with no target processes attached. 3172145256Sjkoshy */ 3173145256Sjkoshy 3174145256Sjkoshy if (LIST_EMPTY(&pm->pm_targets) && 3175145256Sjkoshy (prw.pm_flags & PMC_F_OLDVALUE)) { 3176145256Sjkoshy PROC_LOCK(curthread->td_proc); 3177145256Sjkoshy psignal(curthread->td_proc, SIGIO); 3178145256Sjkoshy PROC_UNLOCK(curthread->td_proc); 3179145256Sjkoshy } 3180145256Sjkoshy } 3181145256Sjkoshy break; 3182145256Sjkoshy 3183145256Sjkoshy 3184145256Sjkoshy /* 3185145256Sjkoshy * Set the sampling rate for a sampling mode PMC and the 3186145256Sjkoshy * initial count for a counting mode PMC. 3187145256Sjkoshy */ 3188145256Sjkoshy 3189145256Sjkoshy case PMC_OP_PMCSETCOUNT: 3190145256Sjkoshy { 3191145256Sjkoshy struct pmc *pm; 3192145256Sjkoshy struct pmc_op_pmcsetcount sc; 3193145256Sjkoshy 3194145256Sjkoshy PMC_DOWNGRADE_SX(); 3195145256Sjkoshy 3196145256Sjkoshy if ((error = copyin(arg, &sc, sizeof(sc))) != 0) 3197145256Sjkoshy break; 3198145256Sjkoshy 3199145256Sjkoshy if ((error = pmc_find_pmc(sc.pm_pmcid, &pm)) != 0) 3200145256Sjkoshy break; 3201145256Sjkoshy 3202145256Sjkoshy if (pm->pm_state == PMC_STATE_RUNNING) { 3203145256Sjkoshy error = EBUSY; 3204145256Sjkoshy break; 3205145256Sjkoshy } 3206145256Sjkoshy 3207145774Sjkoshy if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm))) 3208145256Sjkoshy pm->pm_sc.pm_reloadcount = sc.pm_count; 3209145256Sjkoshy else 3210145256Sjkoshy pm->pm_sc.pm_initial = sc.pm_count; 3211145256Sjkoshy } 3212145256Sjkoshy break; 3213145256Sjkoshy 3214145256Sjkoshy 3215145256Sjkoshy /* 3216145256Sjkoshy * Start a PMC. 3217145256Sjkoshy */ 3218145256Sjkoshy 3219145256Sjkoshy case PMC_OP_PMCSTART: 3220145256Sjkoshy { 3221145256Sjkoshy pmc_id_t pmcid; 3222145256Sjkoshy struct pmc *pm; 3223145256Sjkoshy struct pmc_op_simple sp; 3224145256Sjkoshy 3225145256Sjkoshy sx_assert(&pmc_sx, SX_XLOCKED); 3226145256Sjkoshy 3227145256Sjkoshy if ((error = copyin(arg, &sp, sizeof(sp))) != 0) 3228145256Sjkoshy break; 3229145256Sjkoshy 3230145256Sjkoshy pmcid = sp.pm_pmcid; 3231145256Sjkoshy 3232145256Sjkoshy if ((error = pmc_find_pmc(pmcid, &pm)) != 0) 3233145256Sjkoshy break; 3234145256Sjkoshy 3235145774Sjkoshy KASSERT(pmcid == pm->pm_id, 3236145774Sjkoshy ("[pmc,%d] pmcid %x != id %x", __LINE__, 3237145774Sjkoshy pm->pm_id, pmcid)); 3238145256Sjkoshy 3239145256Sjkoshy if (pm->pm_state == PMC_STATE_RUNNING) /* already running */ 3240145256Sjkoshy break; 3241145256Sjkoshy else if (pm->pm_state != PMC_STATE_STOPPED && 3242145256Sjkoshy pm->pm_state != PMC_STATE_ALLOCATED) { 3243145256Sjkoshy error = EINVAL; 3244145256Sjkoshy break; 3245145256Sjkoshy } 3246145256Sjkoshy 3247145256Sjkoshy error = pmc_start(pm); 3248145256Sjkoshy } 3249145256Sjkoshy break; 3250145256Sjkoshy 3251145256Sjkoshy 3252145256Sjkoshy /* 3253145256Sjkoshy * Stop a PMC. 3254145256Sjkoshy */ 3255145256Sjkoshy 3256145256Sjkoshy case PMC_OP_PMCSTOP: 3257145256Sjkoshy { 3258145256Sjkoshy pmc_id_t pmcid; 3259145256Sjkoshy struct pmc *pm; 3260145256Sjkoshy struct pmc_op_simple sp; 3261145256Sjkoshy 3262145256Sjkoshy PMC_DOWNGRADE_SX(); 3263145256Sjkoshy 3264145256Sjkoshy if ((error = copyin(arg, &sp, sizeof(sp))) != 0) 3265145256Sjkoshy break; 3266145256Sjkoshy 3267145256Sjkoshy pmcid = sp.pm_pmcid; 3268145256Sjkoshy 3269145256Sjkoshy /* 3270145256Sjkoshy * Mark the PMC as inactive and invoke the MD stop 3271145256Sjkoshy * routines if needed. 3272145256Sjkoshy */ 3273145256Sjkoshy 3274145256Sjkoshy if ((error = pmc_find_pmc(pmcid, &pm)) != 0) 3275145256Sjkoshy break; 3276145256Sjkoshy 3277145774Sjkoshy KASSERT(pmcid == pm->pm_id, 3278145774Sjkoshy ("[pmc,%d] pmc id %x != pmcid %x", __LINE__, 3279145774Sjkoshy pm->pm_id, pmcid)); 3280145256Sjkoshy 3281145256Sjkoshy if (pm->pm_state == PMC_STATE_STOPPED) /* already stopped */ 3282145256Sjkoshy break; 3283145256Sjkoshy else if (pm->pm_state != PMC_STATE_RUNNING) { 3284145256Sjkoshy error = EINVAL; 3285145256Sjkoshy break; 3286145256Sjkoshy } 3287145256Sjkoshy 3288145256Sjkoshy error = pmc_stop(pm); 3289145256Sjkoshy } 3290145256Sjkoshy break; 3291145256Sjkoshy 3292145256Sjkoshy 3293145256Sjkoshy /* 3294145256Sjkoshy * Write a user-entry to the log file. 3295145256Sjkoshy */ 3296145256Sjkoshy 3297145256Sjkoshy case PMC_OP_WRITELOG: 3298145256Sjkoshy { 3299145256Sjkoshy 3300145256Sjkoshy PMC_DOWNGRADE_SX(); 3301145256Sjkoshy 3302145256Sjkoshy /* 3303145256Sjkoshy * flush all per-cpu hash tables 3304145256Sjkoshy * append user-log entry 3305145256Sjkoshy */ 3306145256Sjkoshy 3307145256Sjkoshy error = ENOSYS; 3308145256Sjkoshy } 3309145256Sjkoshy break; 3310145256Sjkoshy 3311145256Sjkoshy 3312145256Sjkoshy#if __i386__ || __amd64__ 3313145256Sjkoshy 3314145256Sjkoshy /* 3315145256Sjkoshy * Machine dependent operation for i386-class processors. 3316145256Sjkoshy * 3317145256Sjkoshy * Retrieve the MSR number associated with the counter 3318145256Sjkoshy * 'pmc_id'. This allows processes to directly use RDPMC 3319145256Sjkoshy * instructions to read their PMCs, without the overhead of a 3320145256Sjkoshy * system call. 3321145256Sjkoshy */ 3322145256Sjkoshy 3323145256Sjkoshy case PMC_OP_PMCX86GETMSR: 3324145256Sjkoshy { 3325145256Sjkoshy int ri; 3326145256Sjkoshy struct pmc *pm; 3327145774Sjkoshy struct pmc_target *pt; 3328145256Sjkoshy struct pmc_op_x86_getmsr gm; 3329145256Sjkoshy 3330145256Sjkoshy PMC_DOWNGRADE_SX(); 3331145256Sjkoshy 3332145256Sjkoshy /* CPU has no 'GETMSR' support */ 3333145256Sjkoshy if (md->pmd_get_msr == NULL) { 3334145256Sjkoshy error = ENOSYS; 3335145256Sjkoshy break; 3336145256Sjkoshy } 3337145256Sjkoshy 3338145256Sjkoshy if ((error = copyin(arg, &gm, sizeof(gm))) != 0) 3339145256Sjkoshy break; 3340145256Sjkoshy 3341145256Sjkoshy if ((error = pmc_find_pmc(gm.pm_pmcid, &pm)) != 0) 3342145256Sjkoshy break; 3343145256Sjkoshy 3344145256Sjkoshy /* 3345145774Sjkoshy * The allocated PMC has to be a process virtual PMC, 3346145774Sjkoshy * i.e., of type MODE_T[CS]. Global PMCs can only be 3347145774Sjkoshy * read using the PMCREAD operation since they may be 3348145774Sjkoshy * allocated on a different CPU than the one we could 3349145774Sjkoshy * be running on at the time of the RDPMC instruction. 3350145256Sjkoshy * 3351145774Sjkoshy * The GETMSR operation is not allowed for PMCs that 3352145774Sjkoshy * are inherited across processes. 3353145256Sjkoshy */ 3354145256Sjkoshy 3355145774Sjkoshy if (!PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm)) || 3356145774Sjkoshy (pm->pm_flags & PMC_F_DESCENDANTS)) { 3357145256Sjkoshy error = EINVAL; 3358145256Sjkoshy break; 3359145256Sjkoshy } 3360145256Sjkoshy 3361145774Sjkoshy /* 3362145774Sjkoshy * It only makes sense to use a RDPMC (or its 3363145774Sjkoshy * equivalent instruction on non-x86 architectures) on 3364145774Sjkoshy * a process that has allocated and attached a PMC to 3365145774Sjkoshy * itself. Conversely the PMC is only allowed to have 3366145774Sjkoshy * one process attached to it -- its owner. 3367145774Sjkoshy */ 3368145256Sjkoshy 3369145774Sjkoshy if ((pt = LIST_FIRST(&pm->pm_targets)) == NULL || 3370145774Sjkoshy LIST_NEXT(pt, pt_next) != NULL || 3371145774Sjkoshy pt->pt_process->pp_proc != pm->pm_owner->po_owner) { 3372145774Sjkoshy error = EINVAL; 3373145774Sjkoshy break; 3374145774Sjkoshy } 3375145774Sjkoshy 3376145774Sjkoshy ri = PMC_TO_ROWINDEX(pm); 3377145774Sjkoshy 3378145256Sjkoshy if ((error = (*md->pmd_get_msr)(ri, &gm.pm_msr)) < 0) 3379145256Sjkoshy break; 3380145774Sjkoshy 3381145256Sjkoshy if ((error = copyout(&gm, arg, sizeof(gm))) < 0) 3382145256Sjkoshy break; 3383145774Sjkoshy 3384145774Sjkoshy /* 3385145774Sjkoshy * Mark our process as using MSRs. Update machine 3386145774Sjkoshy * state using a forced context switch. 3387145774Sjkoshy */ 3388145774Sjkoshy 3389145774Sjkoshy pt->pt_process->pp_flags |= PMC_PP_ENABLE_MSR_ACCESS; 3390145774Sjkoshy pmc_force_context_switch(); 3391145774Sjkoshy 3392145256Sjkoshy } 3393145256Sjkoshy break; 3394145256Sjkoshy#endif 3395145256Sjkoshy 3396145256Sjkoshy default: 3397145256Sjkoshy error = EINVAL; 3398145256Sjkoshy break; 3399145256Sjkoshy } 3400145256Sjkoshy 3401145256Sjkoshy if (is_sx_downgraded) 3402145256Sjkoshy sx_sunlock(&pmc_sx); 3403145256Sjkoshy else 3404145256Sjkoshy sx_xunlock(&pmc_sx); 3405145256Sjkoshy 3406145256Sjkoshy if (error) 3407145256Sjkoshy atomic_add_int(&pmc_stats.pm_syscall_errors, 1); 3408145256Sjkoshy 3409145256Sjkoshy return error; 3410145256Sjkoshy} 3411145256Sjkoshy 3412145256Sjkoshy/* 3413145256Sjkoshy * Helper functions 3414145256Sjkoshy */ 3415145256Sjkoshy 3416145256Sjkoshy/* 3417145256Sjkoshy * Configure a log file. 3418145256Sjkoshy */ 3419145256Sjkoshy 3420145256Sjkoshystatic int 3421145256Sjkoshypmc_configure_log(struct pmc_owner *po, int logfd) 3422145256Sjkoshy{ 3423145256Sjkoshy struct proc *p; 3424145256Sjkoshy 3425145256Sjkoshy return ENOSYS; /* for now */ 3426145256Sjkoshy 3427145256Sjkoshy p = po->po_owner; 3428145256Sjkoshy 3429145256Sjkoshy if (po->po_logfd < 0 && logfd < 0) /* nothing to do */ 3430145256Sjkoshy return 0; 3431145256Sjkoshy 3432145256Sjkoshy if (po->po_logfd >= 0 && logfd < 0) { 3433145256Sjkoshy /* deconfigure log */ 3434145256Sjkoshy /* XXX */ 3435145774Sjkoshy po->po_flags &= ~PMC_PO_OWNS_LOGFILE; 3436145256Sjkoshy pmc_maybe_remove_owner(po); 3437145256Sjkoshy 3438145256Sjkoshy } else if (po->po_logfd < 0 && logfd >= 0) { 3439145256Sjkoshy /* configure log file */ 3440145256Sjkoshy /* XXX */ 3441145774Sjkoshy po->po_flags |= PMC_PO_OWNS_LOGFILE; 3442145256Sjkoshy 3443145256Sjkoshy /* mark process as using HWPMCs */ 3444145256Sjkoshy PROC_LOCK(p); 3445145256Sjkoshy p->p_flag |= P_HWPMC; 3446145256Sjkoshy PROC_UNLOCK(p); 3447145256Sjkoshy } else 3448145256Sjkoshy return EBUSY; 3449145256Sjkoshy 3450145256Sjkoshy return 0; 3451145256Sjkoshy} 3452145256Sjkoshy 3453145256Sjkoshy/* 3454145256Sjkoshy * Log an exit event to the PMC owner's log file. 3455145256Sjkoshy */ 3456145256Sjkoshy 3457145256Sjkoshystatic void 3458145256Sjkoshypmc_log_process_exit(struct pmc *pm, struct pmc_process *pp) 3459145256Sjkoshy{ 3460145256Sjkoshy KASSERT(pm->pm_flags & PMC_F_LOG_TC_PROCEXIT, 3461145256Sjkoshy ("[pmc,%d] log-process-exit called gratuitously", __LINE__)); 3462145256Sjkoshy 3463145256Sjkoshy (void) pm; 3464145256Sjkoshy (void) pp; 3465145256Sjkoshy 3466145256Sjkoshy return; 3467145256Sjkoshy} 3468145256Sjkoshy 3469145256Sjkoshy/* 3470145256Sjkoshy * Event handlers. 3471145256Sjkoshy */ 3472145256Sjkoshy 3473145256Sjkoshy/* 3474145256Sjkoshy * Handle a process exit. 3475145256Sjkoshy * 3476145256Sjkoshy * XXX This eventhandler gets called early in the exit process. 3477145256Sjkoshy * Consider using a 'hook' invocation from thread_exit() or equivalent 3478145256Sjkoshy * spot. Another negative is that kse_exit doesn't seem to call 3479145256Sjkoshy * exit1() [??]. 3480145256Sjkoshy */ 3481145256Sjkoshy 3482145256Sjkoshystatic void 3483145256Sjkoshypmc_process_exit(void *arg __unused, struct proc *p) 3484145256Sjkoshy{ 3485145256Sjkoshy int is_using_hwpmcs; 3486145256Sjkoshy 3487145256Sjkoshy PROC_LOCK(p); 3488145256Sjkoshy is_using_hwpmcs = p->p_flag & P_HWPMC; 3489145256Sjkoshy PROC_UNLOCK(p); 3490145256Sjkoshy 3491145256Sjkoshy if (is_using_hwpmcs) { 3492145256Sjkoshy PMCDBG(PRC,EXT,1,"process-exit proc=%p (%d, %s)", p, p->p_pid, 3493145256Sjkoshy p->p_comm); 3494145256Sjkoshy 3495145256Sjkoshy PMC_GET_SX_XLOCK(); 3496145256Sjkoshy (void) pmc_hook_handler(curthread, PMC_FN_PROCESS_EXIT, 3497145256Sjkoshy (void *) p); 3498145256Sjkoshy sx_xunlock(&pmc_sx); 3499145256Sjkoshy } 3500145256Sjkoshy} 3501145256Sjkoshy 3502145256Sjkoshy/* 3503145256Sjkoshy * Handle a process fork. 3504145256Sjkoshy * 3505145256Sjkoshy * If the parent process 'p1' is under HWPMC monitoring, then copy 3506145256Sjkoshy * over any attached PMCs that have 'do_descendants' semantics. 3507145256Sjkoshy */ 3508145256Sjkoshy 3509145256Sjkoshystatic void 3510145256Sjkoshypmc_process_fork(void *arg __unused, struct proc *p1, struct proc *p2, 3511145256Sjkoshy int flags) 3512145256Sjkoshy{ 3513145256Sjkoshy int is_using_hwpmcs; 3514145256Sjkoshy 3515145256Sjkoshy (void) flags; /* unused parameter */ 3516145256Sjkoshy 3517145256Sjkoshy PROC_LOCK(p1); 3518145256Sjkoshy is_using_hwpmcs = p1->p_flag & P_HWPMC; 3519145256Sjkoshy PROC_UNLOCK(p1); 3520145256Sjkoshy 3521145256Sjkoshy if (is_using_hwpmcs) { 3522145256Sjkoshy PMCDBG(PMC,FRK,1, "process-fork proc=%p (%d, %s)", p1, 3523145256Sjkoshy p1->p_pid, p1->p_comm); 3524145256Sjkoshy PMC_GET_SX_XLOCK(); 3525145256Sjkoshy (void) pmc_hook_handler(curthread, PMC_FN_PROCESS_FORK, 3526145256Sjkoshy (void *) p2); 3527145256Sjkoshy sx_xunlock(&pmc_sx); 3528145256Sjkoshy } 3529145256Sjkoshy} 3530145256Sjkoshy 3531145256Sjkoshy 3532145256Sjkoshy/* 3533145256Sjkoshy * initialization 3534145256Sjkoshy */ 3535145256Sjkoshy 3536145256Sjkoshystatic const char *pmc_name_of_pmcclass[] = { 3537145256Sjkoshy#undef __PMC_CLASS 3538145256Sjkoshy#define __PMC_CLASS(N) #N , 3539145256Sjkoshy __PMC_CLASSES() 3540145256Sjkoshy}; 3541145256Sjkoshy 3542145256Sjkoshystatic int 3543145256Sjkoshypmc_initialize(void) 3544145256Sjkoshy{ 3545145256Sjkoshy int error, cpu, n; 3546145256Sjkoshy struct pmc_binding pb; 3547145256Sjkoshy 3548145256Sjkoshy md = NULL; 3549145256Sjkoshy error = 0; 3550145256Sjkoshy 3551145256Sjkoshy#if DEBUG 3552145256Sjkoshy /* parse debug flags first */ 3553145256Sjkoshy if (TUNABLE_STR_FETCH(PMC_SYSCTL_NAME_PREFIX "debugflags", 3554145256Sjkoshy pmc_debugstr, sizeof(pmc_debugstr))) 3555145256Sjkoshy pmc_debugflags_parse(pmc_debugstr, 3556145256Sjkoshy pmc_debugstr+strlen(pmc_debugstr)); 3557145256Sjkoshy#endif 3558145256Sjkoshy 3559145256Sjkoshy PMCDBG(MOD,INI,0, "PMC Initialize (version %x)", PMC_VERSION); 3560145256Sjkoshy 3561145256Sjkoshy /* 3562145256Sjkoshy * check sysctl parameters 3563145256Sjkoshy */ 3564145256Sjkoshy 3565145256Sjkoshy if (pmc_hashsize <= 0) { 3566145256Sjkoshy (void) printf("pmc: sysctl variable \"" 3567145256Sjkoshy PMC_SYSCTL_NAME_PREFIX "hashsize\" must be greater than " 3568145256Sjkoshy "zero\n"); 3569145256Sjkoshy pmc_hashsize = PMC_HASH_SIZE; 3570145256Sjkoshy } 3571145256Sjkoshy 3572145256Sjkoshy#if defined(__i386__) 3573145256Sjkoshy /* determine the CPU kind. This is i386 specific */ 3574145256Sjkoshy if (strcmp(cpu_vendor, "AuthenticAMD") == 0) 3575145256Sjkoshy md = pmc_amd_initialize(); 3576145256Sjkoshy else if (strcmp(cpu_vendor, "GenuineIntel") == 0) 3577145256Sjkoshy md = pmc_intel_initialize(); 3578145256Sjkoshy /* XXX: what about the other i386 CPU manufacturers? */ 3579145256Sjkoshy#elif defined(__amd64__) 3580145256Sjkoshy if (strcmp(cpu_vendor, "AuthenticAMD") == 0) 3581145256Sjkoshy md = pmc_amd_initialize(); 3582145256Sjkoshy#else /* other architectures */ 3583145256Sjkoshy md = NULL; 3584145256Sjkoshy#endif 3585145256Sjkoshy 3586145256Sjkoshy if (md == NULL || md->pmd_init == NULL) 3587145256Sjkoshy return ENOSYS; 3588145256Sjkoshy 3589145256Sjkoshy /* allocate space for the per-cpu array */ 3590145256Sjkoshy MALLOC(pmc_pcpu, struct pmc_cpu **, mp_ncpus * sizeof(struct pmc_cpu *), 3591145256Sjkoshy M_PMC, M_WAITOK|M_ZERO); 3592145256Sjkoshy 3593145256Sjkoshy /* per-cpu 'saved values' for managing process-mode PMCs */ 3594145256Sjkoshy MALLOC(pmc_pcpu_saved, pmc_value_t *, 3595145256Sjkoshy sizeof(pmc_value_t) * mp_ncpus * md->pmd_npmc, M_PMC, M_WAITOK); 3596145256Sjkoshy 3597145256Sjkoshy /* perform cpu dependent initialization */ 3598145256Sjkoshy pmc_save_cpu_binding(&pb); 3599145256Sjkoshy for (cpu = 0; cpu < mp_ncpus; cpu++) { 3600145256Sjkoshy if (pmc_cpu_is_disabled(cpu)) 3601145256Sjkoshy continue; 3602145256Sjkoshy pmc_select_cpu(cpu); 3603145256Sjkoshy if ((error = md->pmd_init(cpu)) != 0) 3604145256Sjkoshy break; 3605145256Sjkoshy } 3606145256Sjkoshy pmc_restore_cpu_binding(&pb); 3607145256Sjkoshy 3608145256Sjkoshy if (error != 0) 3609145256Sjkoshy return error; 3610145256Sjkoshy 3611145256Sjkoshy /* allocate space for the row disposition array */ 3612145256Sjkoshy pmc_pmcdisp = malloc(sizeof(enum pmc_mode) * md->pmd_npmc, 3613145256Sjkoshy M_PMC, M_WAITOK|M_ZERO); 3614145256Sjkoshy 3615145256Sjkoshy KASSERT(pmc_pmcdisp != NULL, 3616145256Sjkoshy ("[pmc,%d] pmcdisp allocation returned NULL", __LINE__)); 3617145256Sjkoshy 3618145256Sjkoshy /* mark all PMCs as available */ 3619145256Sjkoshy for (n = 0; n < (int) md->pmd_npmc; n++) 3620145256Sjkoshy PMC_MARK_ROW_FREE(n); 3621145256Sjkoshy 3622145256Sjkoshy /* allocate thread hash tables */ 3623145256Sjkoshy pmc_ownerhash = hashinit(pmc_hashsize, M_PMC, 3624145256Sjkoshy &pmc_ownerhashmask); 3625145256Sjkoshy 3626145256Sjkoshy pmc_processhash = hashinit(pmc_hashsize, M_PMC, 3627145256Sjkoshy &pmc_processhashmask); 3628145256Sjkoshy mtx_init(&pmc_processhash_mtx, "pmc-process-hash", "pmc", MTX_SPIN); 3629145256Sjkoshy 3630145256Sjkoshy /* allocate a pool of spin mutexes */ 3631145256Sjkoshy pmc_mtxpool = mtx_pool_create("pmc", pmc_mtxpool_size, MTX_SPIN); 3632145256Sjkoshy 3633145256Sjkoshy PMCDBG(MOD,INI,1, "pmc_ownerhash=%p, mask=0x%lx " 3634145256Sjkoshy "targethash=%p mask=0x%lx", pmc_ownerhash, pmc_ownerhashmask, 3635145256Sjkoshy pmc_processhash, pmc_processhashmask); 3636145256Sjkoshy 3637145256Sjkoshy /* register process {exit,fork,exec} handlers */ 3638145256Sjkoshy pmc_exit_tag = EVENTHANDLER_REGISTER(process_exit, 3639145256Sjkoshy pmc_process_exit, NULL, EVENTHANDLER_PRI_ANY); 3640145256Sjkoshy pmc_fork_tag = EVENTHANDLER_REGISTER(process_fork, 3641145256Sjkoshy pmc_process_fork, NULL, EVENTHANDLER_PRI_ANY); 3642145256Sjkoshy 3643145256Sjkoshy /* set hook functions */ 3644145256Sjkoshy pmc_intr = md->pmd_intr; 3645145256Sjkoshy pmc_hook = pmc_hook_handler; 3646145256Sjkoshy 3647145256Sjkoshy if (error == 0) { 3648145256Sjkoshy printf(PMC_MODULE_NAME ":"); 3649145256Sjkoshy for (n = 0; n < (int) md->pmd_nclass; n++) 3650145256Sjkoshy printf(" %s(%d)", 3651145774Sjkoshy pmc_name_of_pmcclass[md->pmd_classes[n].pm_class], 3652145256Sjkoshy md->pmd_nclasspmcs[n]); 3653145256Sjkoshy printf("\n"); 3654145256Sjkoshy } 3655145256Sjkoshy 3656145256Sjkoshy return error; 3657145256Sjkoshy} 3658145256Sjkoshy 3659145256Sjkoshy/* prepare to be unloaded */ 3660145256Sjkoshystatic void 3661145256Sjkoshypmc_cleanup(void) 3662145256Sjkoshy{ 3663145256Sjkoshy int cpu; 3664145256Sjkoshy struct pmc_ownerhash *ph; 3665145256Sjkoshy struct pmc_owner *po, *tmp; 3666145256Sjkoshy struct pmc_binding pb; 3667145256Sjkoshy#if DEBUG 3668145256Sjkoshy struct pmc_processhash *prh; 3669145256Sjkoshy#endif 3670145256Sjkoshy 3671145256Sjkoshy PMCDBG(MOD,INI,0, "%s", "cleanup"); 3672145256Sjkoshy 3673145256Sjkoshy pmc_intr = NULL; /* no more interrupts please */ 3674145256Sjkoshy 3675145256Sjkoshy sx_xlock(&pmc_sx); 3676145256Sjkoshy if (pmc_hook == NULL) { /* being unloaded already */ 3677145256Sjkoshy sx_xunlock(&pmc_sx); 3678145256Sjkoshy return; 3679145256Sjkoshy } 3680145256Sjkoshy 3681145256Sjkoshy pmc_hook = NULL; /* prevent new threads from entering module */ 3682145256Sjkoshy 3683145256Sjkoshy /* deregister event handlers */ 3684145256Sjkoshy EVENTHANDLER_DEREGISTER(process_fork, pmc_fork_tag); 3685145256Sjkoshy EVENTHANDLER_DEREGISTER(process_exit, pmc_exit_tag); 3686145256Sjkoshy 3687145256Sjkoshy /* send SIGBUS to all owner threads, free up allocations */ 3688145256Sjkoshy if (pmc_ownerhash) 3689145256Sjkoshy for (ph = pmc_ownerhash; 3690145256Sjkoshy ph <= &pmc_ownerhash[pmc_ownerhashmask]; 3691145256Sjkoshy ph++) { 3692145256Sjkoshy LIST_FOREACH_SAFE(po, ph, po_next, tmp) { 3693145256Sjkoshy pmc_remove_owner(po); 3694145256Sjkoshy 3695145256Sjkoshy /* send SIGBUS to owner processes */ 3696145256Sjkoshy PMCDBG(MOD,INI,2, "cleanup signal proc=%p " 3697145256Sjkoshy "(%d, %s)", po->po_owner, 3698145256Sjkoshy po->po_owner->p_pid, 3699145256Sjkoshy po->po_owner->p_comm); 3700145256Sjkoshy 3701145256Sjkoshy PROC_LOCK(po->po_owner); 3702145256Sjkoshy psignal(po->po_owner, SIGBUS); 3703145256Sjkoshy PROC_UNLOCK(po->po_owner); 3704145256Sjkoshy FREE(po, M_PMC); 3705145256Sjkoshy } 3706145256Sjkoshy } 3707145256Sjkoshy 3708145256Sjkoshy /* reclaim allocated data structures */ 3709145256Sjkoshy if (pmc_mtxpool) 3710145256Sjkoshy mtx_pool_destroy(&pmc_mtxpool); 3711145256Sjkoshy 3712145256Sjkoshy mtx_destroy(&pmc_processhash_mtx); 3713145256Sjkoshy if (pmc_processhash) { 3714145256Sjkoshy#if DEBUG 3715145256Sjkoshy struct pmc_process *pp; 3716145256Sjkoshy 3717145256Sjkoshy PMCDBG(MOD,INI,3, "%s", "destroy process hash"); 3718145256Sjkoshy for (prh = pmc_processhash; 3719145256Sjkoshy prh <= &pmc_processhash[pmc_processhashmask]; 3720145256Sjkoshy prh++) 3721145256Sjkoshy LIST_FOREACH(pp, prh, pp_next) 3722145256Sjkoshy PMCDBG(MOD,INI,3, "pid=%d", pp->pp_proc->p_pid); 3723145256Sjkoshy#endif 3724145256Sjkoshy 3725145256Sjkoshy hashdestroy(pmc_processhash, M_PMC, pmc_processhashmask); 3726145256Sjkoshy pmc_processhash = NULL; 3727145256Sjkoshy } 3728145256Sjkoshy 3729145256Sjkoshy if (pmc_ownerhash) { 3730145256Sjkoshy PMCDBG(MOD,INI,3, "%s", "destroy owner hash"); 3731145256Sjkoshy hashdestroy(pmc_ownerhash, M_PMC, pmc_ownerhashmask); 3732145256Sjkoshy pmc_ownerhash = NULL; 3733145256Sjkoshy } 3734145256Sjkoshy 3735145256Sjkoshy /* do processor dependent cleanup */ 3736145256Sjkoshy PMCDBG(MOD,INI,3, "%s", "md cleanup"); 3737145256Sjkoshy if (md) { 3738145256Sjkoshy pmc_save_cpu_binding(&pb); 3739145256Sjkoshy for (cpu = 0; cpu < mp_ncpus; cpu++) { 3740145256Sjkoshy PMCDBG(MOD,INI,1,"pmc-cleanup cpu=%d pcs=%p", 3741145256Sjkoshy cpu, pmc_pcpu[cpu]); 3742145256Sjkoshy if (pmc_cpu_is_disabled(cpu)) 3743145256Sjkoshy continue; 3744145256Sjkoshy pmc_select_cpu(cpu); 3745145256Sjkoshy if (pmc_pcpu[cpu]) 3746145256Sjkoshy (void) md->pmd_cleanup(cpu); 3747145256Sjkoshy } 3748145256Sjkoshy FREE(md, M_PMC); 3749145256Sjkoshy md = NULL; 3750145256Sjkoshy pmc_restore_cpu_binding(&pb); 3751145256Sjkoshy } 3752145256Sjkoshy 3753145256Sjkoshy /* deallocate per-cpu structures */ 3754145256Sjkoshy FREE(pmc_pcpu, M_PMC); 3755145256Sjkoshy pmc_pcpu = NULL; 3756145256Sjkoshy 3757145256Sjkoshy FREE(pmc_pcpu_saved, M_PMC); 3758145256Sjkoshy pmc_pcpu_saved = NULL; 3759145256Sjkoshy 3760145256Sjkoshy if (pmc_pmcdisp) { 3761145256Sjkoshy FREE(pmc_pmcdisp, M_PMC); 3762145256Sjkoshy pmc_pmcdisp = NULL; 3763145256Sjkoshy } 3764145256Sjkoshy 3765145256Sjkoshy sx_xunlock(&pmc_sx); /* we are done */ 3766145256Sjkoshy} 3767145256Sjkoshy 3768145256Sjkoshy/* 3769145256Sjkoshy * The function called at load/unload. 3770145256Sjkoshy */ 3771145256Sjkoshy 3772145256Sjkoshystatic int 3773145256Sjkoshyload (struct module *module __unused, int cmd, void *arg __unused) 3774145256Sjkoshy{ 3775145256Sjkoshy int error; 3776145256Sjkoshy 3777145256Sjkoshy error = 0; 3778145256Sjkoshy 3779145256Sjkoshy switch (cmd) { 3780145256Sjkoshy case MOD_LOAD : 3781145256Sjkoshy /* initialize the subsystem */ 3782145256Sjkoshy error = pmc_initialize(); 3783145256Sjkoshy if (error != 0) 3784145256Sjkoshy break; 3785145256Sjkoshy PMCDBG(MOD,INI,1, "syscall=%d ncpus=%d", 3786145256Sjkoshy pmc_syscall_num, mp_ncpus); 3787145256Sjkoshy break; 3788145256Sjkoshy 3789145256Sjkoshy 3790145256Sjkoshy case MOD_UNLOAD : 3791145256Sjkoshy case MOD_SHUTDOWN: 3792145256Sjkoshy pmc_cleanup(); 3793145256Sjkoshy PMCDBG(MOD,INI,1, "%s", "unloaded"); 3794145256Sjkoshy break; 3795145256Sjkoshy 3796145256Sjkoshy default : 3797145256Sjkoshy error = EINVAL; /* XXX should panic(9) */ 3798145256Sjkoshy break; 3799145256Sjkoshy } 3800145256Sjkoshy 3801145256Sjkoshy return error; 3802145256Sjkoshy} 3803145256Sjkoshy 3804145256Sjkoshy/* memory pool */ 3805145256SjkoshyMALLOC_DEFINE(M_PMC, "pmc", "Memory space for the PMC module"); 3806