hwpmc_mod.c revision 185363
1145256Sjkoshy/*-
2183266Sjkoshy * Copyright (c) 2003-2008 Joseph Koshy
3174395Sjkoshy * Copyright (c) 2007 The FreeBSD Foundation
4145256Sjkoshy * All rights reserved.
5145256Sjkoshy *
6174395Sjkoshy * Portions of this software were developed by A. Joseph Koshy under
7174395Sjkoshy * sponsorship from the FreeBSD Foundation and Google, Inc.
8174395Sjkoshy *
9145256Sjkoshy * Redistribution and use in source and binary forms, with or without
10145256Sjkoshy * modification, are permitted provided that the following conditions
11145256Sjkoshy * are met:
12145256Sjkoshy * 1. Redistributions of source code must retain the above copyright
13145256Sjkoshy *    notice, this list of conditions and the following disclaimer.
14145256Sjkoshy * 2. Redistributions in binary form must reproduce the above copyright
15145256Sjkoshy *    notice, this list of conditions and the following disclaimer in the
16145256Sjkoshy *    documentation and/or other materials provided with the distribution.
17145256Sjkoshy *
18145256Sjkoshy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19145256Sjkoshy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20145256Sjkoshy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21145256Sjkoshy * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22145256Sjkoshy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23145256Sjkoshy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24145256Sjkoshy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25145256Sjkoshy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26145256Sjkoshy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27145256Sjkoshy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28145256Sjkoshy * SUCH DAMAGE.
29145256Sjkoshy *
30145256Sjkoshy */
31145256Sjkoshy
32145256Sjkoshy#include <sys/cdefs.h>
33145256Sjkoshy__FBSDID("$FreeBSD: head/sys/dev/hwpmc/hwpmc_mod.c 185363 2008-11-27 09:00:47Z jkoshy $");
34145256Sjkoshy
35145256Sjkoshy#include <sys/param.h>
36145256Sjkoshy#include <sys/eventhandler.h>
37145256Sjkoshy#include <sys/jail.h>
38145256Sjkoshy#include <sys/kernel.h>
39147191Sjkoshy#include <sys/kthread.h>
40145256Sjkoshy#include <sys/limits.h>
41145256Sjkoshy#include <sys/lock.h>
42145256Sjkoshy#include <sys/malloc.h>
43145256Sjkoshy#include <sys/module.h>
44145256Sjkoshy#include <sys/mutex.h>
45145256Sjkoshy#include <sys/pmc.h>
46145256Sjkoshy#include <sys/pmckern.h>
47147191Sjkoshy#include <sys/pmclog.h>
48164033Srwatson#include <sys/priv.h>
49145256Sjkoshy#include <sys/proc.h>
50145256Sjkoshy#include <sys/queue.h>
51147191Sjkoshy#include <sys/resourcevar.h>
52145256Sjkoshy#include <sys/sched.h>
53145256Sjkoshy#include <sys/signalvar.h>
54145256Sjkoshy#include <sys/smp.h>
55145256Sjkoshy#include <sys/sx.h>
56145256Sjkoshy#include <sys/sysctl.h>
57145256Sjkoshy#include <sys/sysent.h>
58145256Sjkoshy#include <sys/systm.h>
59147191Sjkoshy#include <sys/vnode.h>
60145256Sjkoshy
61157144Sjkoshy#include <sys/linker.h>		/* needs to be after <sys/malloc.h> */
62157144Sjkoshy
63147191Sjkoshy#include <machine/atomic.h>
64145256Sjkoshy#include <machine/md_var.h>
65145256Sjkoshy
66145256Sjkoshy/*
67145256Sjkoshy * Types
68145256Sjkoshy */
69145256Sjkoshy
70145256Sjkoshyenum pmc_flags {
71145256Sjkoshy	PMC_FLAG_NONE	  = 0x00, /* do nothing */
72145256Sjkoshy	PMC_FLAG_REMOVE   = 0x01, /* atomically remove entry from hash */
73145256Sjkoshy	PMC_FLAG_ALLOCATE = 0x02, /* add entry to hash if not found */
74145256Sjkoshy};
75145256Sjkoshy
76145256Sjkoshy/*
77145256Sjkoshy * The offset in sysent where the syscall is allocated.
78145256Sjkoshy */
79145256Sjkoshy
80145256Sjkoshystatic int pmc_syscall_num = NO_SYSCALL;
81145256Sjkoshystruct pmc_cpu		**pmc_pcpu;	 /* per-cpu state */
82145256Sjkoshypmc_value_t		*pmc_pcpu_saved; /* saved PMC values: CSW handling */
83145256Sjkoshy
84145256Sjkoshy#define	PMC_PCPU_SAVED(C,R)	pmc_pcpu_saved[(R) + md->pmd_npmc*(C)]
85145256Sjkoshy
86145256Sjkoshystruct mtx_pool		*pmc_mtxpool;
87145256Sjkoshystatic int		*pmc_pmcdisp;	 /* PMC row dispositions */
88145256Sjkoshy
89145256Sjkoshy#define	PMC_ROW_DISP_IS_FREE(R)		(pmc_pmcdisp[(R)] == 0)
90145256Sjkoshy#define	PMC_ROW_DISP_IS_THREAD(R)	(pmc_pmcdisp[(R)] > 0)
91145256Sjkoshy#define	PMC_ROW_DISP_IS_STANDALONE(R)	(pmc_pmcdisp[(R)] < 0)
92145256Sjkoshy
93145256Sjkoshy#define	PMC_MARK_ROW_FREE(R) do {					  \
94145256Sjkoshy	pmc_pmcdisp[(R)] = 0;						  \
95145256Sjkoshy} while (0)
96145256Sjkoshy
97145256Sjkoshy#define	PMC_MARK_ROW_STANDALONE(R) do {					  \
98145256Sjkoshy	KASSERT(pmc_pmcdisp[(R)] <= 0, ("[pmc,%d] row disposition error", \
99145256Sjkoshy		    __LINE__));						  \
100145256Sjkoshy	atomic_add_int(&pmc_pmcdisp[(R)], -1);				  \
101183266Sjkoshy	KASSERT(pmc_pmcdisp[(R)] >= (-pmc_cpu_max_active()),		  \
102183266Sjkoshy		("[pmc,%d] row disposition error", __LINE__));		  \
103145256Sjkoshy} while (0)
104145256Sjkoshy
105145256Sjkoshy#define	PMC_UNMARK_ROW_STANDALONE(R) do { 				  \
106145256Sjkoshy	atomic_add_int(&pmc_pmcdisp[(R)], 1);				  \
107145256Sjkoshy	KASSERT(pmc_pmcdisp[(R)] <= 0, ("[pmc,%d] row disposition error", \
108145256Sjkoshy		    __LINE__));						  \
109145256Sjkoshy} while (0)
110145256Sjkoshy
111145256Sjkoshy#define	PMC_MARK_ROW_THREAD(R) do {					  \
112145256Sjkoshy	KASSERT(pmc_pmcdisp[(R)] >= 0, ("[pmc,%d] row disposition error", \
113145256Sjkoshy		    __LINE__));						  \
114145256Sjkoshy	atomic_add_int(&pmc_pmcdisp[(R)], 1);				  \
115145256Sjkoshy} while (0)
116145256Sjkoshy
117145256Sjkoshy#define	PMC_UNMARK_ROW_THREAD(R) do {					  \
118145256Sjkoshy	atomic_add_int(&pmc_pmcdisp[(R)], -1);				  \
119145256Sjkoshy	KASSERT(pmc_pmcdisp[(R)] >= 0, ("[pmc,%d] row disposition error", \
120145256Sjkoshy		    __LINE__));						  \
121145256Sjkoshy} while (0)
122145256Sjkoshy
123145256Sjkoshy
124145256Sjkoshy/* various event handlers */
125145256Sjkoshystatic eventhandler_tag	pmc_exit_tag, pmc_fork_tag;
126145256Sjkoshy
127145256Sjkoshy/* Module statistics */
128145256Sjkoshystruct pmc_op_getdriverstats pmc_stats;
129145256Sjkoshy
130145256Sjkoshy/* Machine/processor dependent operations */
131185363Sjkoshystatic struct pmc_mdep  *md;
132145256Sjkoshy
133145256Sjkoshy/*
134145256Sjkoshy * Hash tables mapping owner processes and target threads to PMCs.
135145256Sjkoshy */
136145256Sjkoshy
137145256Sjkoshystruct mtx pmc_processhash_mtx;		/* spin mutex */
138145256Sjkoshystatic u_long pmc_processhashmask;
139145256Sjkoshystatic LIST_HEAD(pmc_processhash, pmc_process)	*pmc_processhash;
140145256Sjkoshy
141145256Sjkoshy/*
142145256Sjkoshy * Hash table of PMC owner descriptors.  This table is protected by
143145256Sjkoshy * the shared PMC "sx" lock.
144145256Sjkoshy */
145145256Sjkoshy
146145256Sjkoshystatic u_long pmc_ownerhashmask;
147145256Sjkoshystatic LIST_HEAD(pmc_ownerhash, pmc_owner)	*pmc_ownerhash;
148145256Sjkoshy
149145256Sjkoshy/*
150147191Sjkoshy * List of PMC owners with system-wide sampling PMCs.
151147191Sjkoshy */
152147191Sjkoshy
153147191Sjkoshystatic LIST_HEAD(, pmc_owner)			pmc_ss_owners;
154147191Sjkoshy
155147191Sjkoshy
156147191Sjkoshy/*
157184802Sjkoshy * A map of row indices to classdep structures.
158184802Sjkoshy */
159184802Sjkoshystatic struct pmc_classdep **pmc_rowindex_to_classdep;
160184802Sjkoshy
161184802Sjkoshy/*
162145256Sjkoshy * Prototypes
163145256Sjkoshy */
164145256Sjkoshy
165153110Sru#ifdef	DEBUG
166145256Sjkoshystatic int	pmc_debugflags_sysctl_handler(SYSCTL_HANDLER_ARGS);
167145256Sjkoshystatic int	pmc_debugflags_parse(char *newstr, char *fence);
168145256Sjkoshy#endif
169145256Sjkoshy
170145256Sjkoshystatic int	load(struct module *module, int cmd, void *arg);
171147191Sjkoshystatic int	pmc_attach_process(struct proc *p, struct pmc *pm);
172145256Sjkoshystatic struct pmc *pmc_allocate_pmc_descriptor(void);
173147191Sjkoshystatic struct pmc_owner *pmc_allocate_owner_descriptor(struct proc *p);
174147191Sjkoshystatic int	pmc_attach_one_process(struct proc *p, struct pmc *pm);
175147191Sjkoshystatic int	pmc_can_allocate_rowindex(struct proc *p, unsigned int ri,
176147191Sjkoshy    int cpu);
177147191Sjkoshystatic int	pmc_can_attach(struct pmc *pm, struct proc *p);
178174395Sjkoshystatic void	pmc_capture_user_callchain(int cpu, struct trapframe *tf);
179147191Sjkoshystatic void	pmc_cleanup(void);
180147191Sjkoshystatic int	pmc_detach_process(struct proc *p, struct pmc *pm);
181147191Sjkoshystatic int	pmc_detach_one_process(struct proc *p, struct pmc *pm,
182147191Sjkoshy    int flags);
183147191Sjkoshystatic void	pmc_destroy_owner_descriptor(struct pmc_owner *po);
184147191Sjkoshystatic struct pmc_owner *pmc_find_owner_descriptor(struct proc *p);
185147191Sjkoshystatic int	pmc_find_pmc(pmc_id_t pmcid, struct pmc **pm);
186145256Sjkoshystatic struct pmc *pmc_find_pmc_descriptor_in_process(struct pmc_owner *po,
187145256Sjkoshy    pmc_id_t pmc);
188145256Sjkoshystatic struct pmc_process *pmc_find_process_descriptor(struct proc *p,
189145256Sjkoshy    uint32_t mode);
190145774Sjkoshystatic void	pmc_force_context_switch(void);
191145256Sjkoshystatic void	pmc_link_target_process(struct pmc *pm,
192145256Sjkoshy    struct pmc_process *pp);
193174395Sjkoshystatic void	pmc_log_all_process_mappings(struct pmc_owner *po);
194174395Sjkoshystatic void	pmc_log_kernel_mappings(struct pmc *pm);
195174395Sjkoshystatic void	pmc_log_process_mappings(struct pmc_owner *po, struct proc *p);
196147191Sjkoshystatic void	pmc_maybe_remove_owner(struct pmc_owner *po);
197147191Sjkoshystatic void	pmc_process_csw_in(struct thread *td);
198147191Sjkoshystatic void	pmc_process_csw_out(struct thread *td);
199145256Sjkoshystatic void	pmc_process_exit(void *arg, struct proc *p);
200145256Sjkoshystatic void	pmc_process_fork(void *arg, struct proc *p1,
201145256Sjkoshy    struct proc *p2, int n);
202147191Sjkoshystatic void	pmc_process_samples(int cpu);
203147191Sjkoshystatic void	pmc_release_pmc_descriptor(struct pmc *pmc);
204147191Sjkoshystatic void	pmc_remove_owner(struct pmc_owner *po);
205147191Sjkoshystatic void	pmc_remove_process_descriptor(struct pmc_process *pp);
206147191Sjkoshystatic void	pmc_restore_cpu_binding(struct pmc_binding *pb);
207147191Sjkoshystatic void	pmc_save_cpu_binding(struct pmc_binding *pb);
208147191Sjkoshystatic void	pmc_select_cpu(int cpu);
209145256Sjkoshystatic int	pmc_start(struct pmc *pm);
210145256Sjkoshystatic int	pmc_stop(struct pmc *pm);
211147191Sjkoshystatic int	pmc_syscall_handler(struct thread *td, void *syscall_args);
212147191Sjkoshystatic void	pmc_unlink_target_process(struct pmc *pmc,
213147191Sjkoshy    struct pmc_process *pp);
214145256Sjkoshy
215145256Sjkoshy/*
216145256Sjkoshy * Kernel tunables and sysctl(8) interface.
217145256Sjkoshy */
218145256Sjkoshy
219145256SjkoshySYSCTL_NODE(_kern, OID_AUTO, hwpmc, CTLFLAG_RW, 0, "HWPMC parameters");
220145256Sjkoshy
221174395Sjkoshystatic int pmc_callchaindepth = PMC_CALLCHAIN_DEPTH;
222174395SjkoshyTUNABLE_INT(PMC_SYSCTL_NAME_PREFIX "callchaindepth", &pmc_callchaindepth);
223174395SjkoshySYSCTL_INT(_kern_hwpmc, OID_AUTO, callchaindepth, CTLFLAG_TUN|CTLFLAG_RD,
224174395Sjkoshy    &pmc_callchaindepth, 0, "depth of call chain records");
225174395Sjkoshy
226153110Sru#ifdef	DEBUG
227147191Sjkoshystruct pmc_debugflags pmc_debugflags = PMC_DEBUG_DEFAULT_FLAGS;
228145256Sjkoshychar	pmc_debugstr[PMC_DEBUG_STRSIZE];
229145256SjkoshyTUNABLE_STR(PMC_SYSCTL_NAME_PREFIX "debugflags", pmc_debugstr,
230145256Sjkoshy    sizeof(pmc_debugstr));
231145256SjkoshySYSCTL_PROC(_kern_hwpmc, OID_AUTO, debugflags,
232145256Sjkoshy    CTLTYPE_STRING|CTLFLAG_RW|CTLFLAG_TUN,
233145256Sjkoshy    0, 0, pmc_debugflags_sysctl_handler, "A", "debug flags");
234145256Sjkoshy#endif
235145256Sjkoshy
236145256Sjkoshy/*
237147191Sjkoshy * kern.hwpmc.hashrows -- determines the number of rows in the
238145256Sjkoshy * of the hash table used to look up threads
239145256Sjkoshy */
240145256Sjkoshy
241145256Sjkoshystatic int pmc_hashsize = PMC_HASH_SIZE;
242145256SjkoshyTUNABLE_INT(PMC_SYSCTL_NAME_PREFIX "hashsize", &pmc_hashsize);
243145256SjkoshySYSCTL_INT(_kern_hwpmc, OID_AUTO, hashsize, CTLFLAG_TUN|CTLFLAG_RD,
244145256Sjkoshy    &pmc_hashsize, 0, "rows in hash tables");
245145256Sjkoshy
246145256Sjkoshy/*
247174395Sjkoshy * kern.hwpmc.nsamples --- number of PC samples/callchain stacks per CPU
248145256Sjkoshy */
249145256Sjkoshy
250147191Sjkoshystatic int pmc_nsamples = PMC_NSAMPLES;
251147191SjkoshyTUNABLE_INT(PMC_SYSCTL_NAME_PREFIX "nsamples", &pmc_nsamples);
252147191SjkoshySYSCTL_INT(_kern_hwpmc, OID_AUTO, nsamples, CTLFLAG_TUN|CTLFLAG_RD,
253147191Sjkoshy    &pmc_nsamples, 0, "number of PC samples per CPU");
254145256Sjkoshy
255174395Sjkoshy
256145256Sjkoshy/*
257147191Sjkoshy * kern.hwpmc.mtxpoolsize -- number of mutexes in the mutex pool.
258145256Sjkoshy */
259145256Sjkoshy
260145256Sjkoshystatic int pmc_mtxpool_size = PMC_MTXPOOL_SIZE;
261145256SjkoshyTUNABLE_INT(PMC_SYSCTL_NAME_PREFIX "mtxpoolsize", &pmc_mtxpool_size);
262145256SjkoshySYSCTL_INT(_kern_hwpmc, OID_AUTO, mtxpoolsize, CTLFLAG_TUN|CTLFLAG_RD,
263145256Sjkoshy    &pmc_mtxpool_size, 0, "size of spin mutex pool");
264145256Sjkoshy
265145256Sjkoshy
266145256Sjkoshy/*
267145256Sjkoshy * security.bsd.unprivileged_syspmcs -- allow non-root processes to
268145256Sjkoshy * allocate system-wide PMCs.
269145256Sjkoshy *
270145256Sjkoshy * Allowing unprivileged processes to allocate system PMCs is convenient
271145256Sjkoshy * if system-wide measurements need to be taken concurrently with other
272145256Sjkoshy * per-process measurements.  This feature is turned off by default.
273145256Sjkoshy */
274145256Sjkoshy
275145256Sjkoshystatic int pmc_unprivileged_syspmcs = 0;
276145256SjkoshyTUNABLE_INT("security.bsd.unprivileged_syspmcs", &pmc_unprivileged_syspmcs);
277145256SjkoshySYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_syspmcs, CTLFLAG_RW,
278145256Sjkoshy    &pmc_unprivileged_syspmcs, 0,
279145256Sjkoshy    "allow unprivileged process to allocate system PMCs");
280145256Sjkoshy
281147191Sjkoshy/*
282147191Sjkoshy * Hash function.  Discard the lower 2 bits of the pointer since
283147191Sjkoshy * these are always zero for our uses.  The hash multiplier is
284147191Sjkoshy * round((2^LONG_BIT) * ((sqrt(5)-1)/2)).
285147191Sjkoshy */
286145256Sjkoshy
287145256Sjkoshy#if	LONG_BIT == 64
288145256Sjkoshy#define	_PMC_HM		11400714819323198486u
289145256Sjkoshy#elif	LONG_BIT == 32
290145256Sjkoshy#define	_PMC_HM		2654435769u
291145256Sjkoshy#else
292145256Sjkoshy#error 	Must know the size of 'long' to compile
293145256Sjkoshy#endif
294145256Sjkoshy
295145256Sjkoshy#define	PMC_HASH_PTR(P,M)	((((unsigned long) (P) >> 2) * _PMC_HM) & (M))
296145256Sjkoshy
297145256Sjkoshy/*
298145256Sjkoshy * Syscall structures
299145256Sjkoshy */
300145256Sjkoshy
301145256Sjkoshy/* The `sysent' for the new syscall */
302145256Sjkoshystatic struct sysent pmc_sysent = {
303145256Sjkoshy	2,			/* sy_narg */
304145256Sjkoshy	pmc_syscall_handler	/* sy_call */
305145256Sjkoshy};
306145256Sjkoshy
307145256Sjkoshystatic struct syscall_module_data pmc_syscall_mod = {
308145256Sjkoshy	load,
309145256Sjkoshy	NULL,
310145256Sjkoshy	&pmc_syscall_num,
311145256Sjkoshy	&pmc_sysent,
312145256Sjkoshy	{ 0, NULL }
313145256Sjkoshy};
314145256Sjkoshy
315145256Sjkoshystatic moduledata_t pmc_mod = {
316145256Sjkoshy	PMC_MODULE_NAME,
317145256Sjkoshy	syscall_module_handler,
318145256Sjkoshy	&pmc_syscall_mod
319145256Sjkoshy};
320145256Sjkoshy
321145256SjkoshyDECLARE_MODULE(pmc, pmc_mod, SI_SUB_SMP, SI_ORDER_ANY);
322145256SjkoshyMODULE_VERSION(pmc, PMC_VERSION);
323145256Sjkoshy
324153110Sru#ifdef	DEBUG
325147191Sjkoshyenum pmc_dbgparse_state {
326147191Sjkoshy	PMCDS_WS,		/* in whitespace */
327147191Sjkoshy	PMCDS_MAJOR,		/* seen a major keyword */
328147191Sjkoshy	PMCDS_MINOR
329147191Sjkoshy};
330147191Sjkoshy
331145256Sjkoshystatic int
332145256Sjkoshypmc_debugflags_parse(char *newstr, char *fence)
333145256Sjkoshy{
334145313Sjkoshy	char c, *p, *q;
335147191Sjkoshy	struct pmc_debugflags *tmpflags;
336147191Sjkoshy	int error, found, *newbits, tmp;
337147191Sjkoshy	size_t kwlen;
338145256Sjkoshy
339184214Sdes	tmpflags = malloc(sizeof(*tmpflags), M_PMC, M_WAITOK|M_ZERO);
340145256Sjkoshy
341145256Sjkoshy	p = newstr;
342147191Sjkoshy	error = 0;
343145256Sjkoshy
344147191Sjkoshy	for (; p < fence && (c = *p); p++) {
345145256Sjkoshy
346147191Sjkoshy		/* skip white space */
347147191Sjkoshy		if (c == ' ' || c == '\t')
348147191Sjkoshy			continue;
349147191Sjkoshy
350147191Sjkoshy		/* look for a keyword followed by "=" */
351147191Sjkoshy		for (q = p; p < fence && (c = *p) && c != '='; p++)
352147191Sjkoshy			;
353147191Sjkoshy		if (c != '=') {
354147191Sjkoshy			error = EINVAL;
355147191Sjkoshy			goto done;
356145256Sjkoshy		}
357145256Sjkoshy
358147191Sjkoshy		kwlen = p - q;
359147191Sjkoshy		newbits = NULL;
360145256Sjkoshy
361147191Sjkoshy		/* lookup flag group name */
362147191Sjkoshy#define	DBG_SET_FLAG_MAJ(S,F)						\
363147191Sjkoshy		if (kwlen == sizeof(S)-1 && strncmp(q, S, kwlen) == 0)	\
364147191Sjkoshy			newbits = &tmpflags->pdb_ ## F;
365145256Sjkoshy
366147191Sjkoshy		DBG_SET_FLAG_MAJ("cpu",		CPU);
367147191Sjkoshy		DBG_SET_FLAG_MAJ("csw",		CSW);
368147191Sjkoshy		DBG_SET_FLAG_MAJ("logging",	LOG);
369147191Sjkoshy		DBG_SET_FLAG_MAJ("module",	MOD);
370147191Sjkoshy		DBG_SET_FLAG_MAJ("md", 		MDP);
371147191Sjkoshy		DBG_SET_FLAG_MAJ("owner",	OWN);
372147191Sjkoshy		DBG_SET_FLAG_MAJ("pmc",		PMC);
373147191Sjkoshy		DBG_SET_FLAG_MAJ("process",	PRC);
374147191Sjkoshy		DBG_SET_FLAG_MAJ("sampling", 	SAM);
375145256Sjkoshy
376147191Sjkoshy		if (newbits == NULL) {
377147191Sjkoshy			error = EINVAL;
378147191Sjkoshy			goto done;
379145256Sjkoshy		}
380145256Sjkoshy
381147191Sjkoshy		p++;		/* skip the '=' */
382145256Sjkoshy
383147191Sjkoshy		/* Now parse the individual flags */
384147191Sjkoshy		tmp = 0;
385147191Sjkoshy	newflag:
386147191Sjkoshy		for (q = p; p < fence && (c = *p); p++)
387147191Sjkoshy			if (c == ' ' || c == '\t' || c == ',')
388147191Sjkoshy				break;
389147191Sjkoshy
390147191Sjkoshy		/* p == fence or c == ws or c == "," or c == 0 */
391147191Sjkoshy
392147191Sjkoshy		if ((kwlen = p - q) == 0) {
393147191Sjkoshy			*newbits = tmp;
394147191Sjkoshy			continue;
395147191Sjkoshy		}
396147191Sjkoshy
397147191Sjkoshy		found = 0;
398147191Sjkoshy#define	DBG_SET_FLAG_MIN(S,F)						\
399147191Sjkoshy		if (kwlen == sizeof(S)-1 && strncmp(q, S, kwlen) == 0)	\
400147191Sjkoshy			tmp |= found = (1 << PMC_DEBUG_MIN_ ## F)
401147191Sjkoshy
402147191Sjkoshy		/* a '*' denotes all possible flags in the group */
403147191Sjkoshy		if (kwlen == 1 && *q == '*')
404147191Sjkoshy			tmp = found = ~0;
405147191Sjkoshy		/* look for individual flag names */
406147191Sjkoshy		DBG_SET_FLAG_MIN("allocaterow", ALR);
407147191Sjkoshy		DBG_SET_FLAG_MIN("allocate",	ALL);
408147191Sjkoshy		DBG_SET_FLAG_MIN("attach",	ATT);
409147191Sjkoshy		DBG_SET_FLAG_MIN("bind",	BND);
410147191Sjkoshy		DBG_SET_FLAG_MIN("config",	CFG);
411147191Sjkoshy		DBG_SET_FLAG_MIN("exec",	EXC);
412147191Sjkoshy		DBG_SET_FLAG_MIN("exit",	EXT);
413147191Sjkoshy		DBG_SET_FLAG_MIN("find",	FND);
414147191Sjkoshy		DBG_SET_FLAG_MIN("flush",	FLS);
415147191Sjkoshy		DBG_SET_FLAG_MIN("fork",	FRK);
416147191Sjkoshy		DBG_SET_FLAG_MIN("getbuf",	GTB);
417147191Sjkoshy		DBG_SET_FLAG_MIN("hook",	PMH);
418147191Sjkoshy		DBG_SET_FLAG_MIN("init",	INI);
419147191Sjkoshy		DBG_SET_FLAG_MIN("intr",	INT);
420147191Sjkoshy		DBG_SET_FLAG_MIN("linktarget",	TLK);
421147191Sjkoshy		DBG_SET_FLAG_MIN("mayberemove", OMR);
422147191Sjkoshy		DBG_SET_FLAG_MIN("ops",		OPS);
423147191Sjkoshy		DBG_SET_FLAG_MIN("read",	REA);
424147191Sjkoshy		DBG_SET_FLAG_MIN("register",	REG);
425147191Sjkoshy		DBG_SET_FLAG_MIN("release",	REL);
426147191Sjkoshy		DBG_SET_FLAG_MIN("remove",	ORM);
427147191Sjkoshy		DBG_SET_FLAG_MIN("sample",	SAM);
428147191Sjkoshy		DBG_SET_FLAG_MIN("scheduleio",	SIO);
429147191Sjkoshy		DBG_SET_FLAG_MIN("select",	SEL);
430147191Sjkoshy		DBG_SET_FLAG_MIN("signal",	SIG);
431147191Sjkoshy		DBG_SET_FLAG_MIN("swi",		SWI);
432147191Sjkoshy		DBG_SET_FLAG_MIN("swo",		SWO);
433147191Sjkoshy		DBG_SET_FLAG_MIN("start",	STA);
434147191Sjkoshy		DBG_SET_FLAG_MIN("stop",	STO);
435147191Sjkoshy		DBG_SET_FLAG_MIN("syscall",	PMS);
436147191Sjkoshy		DBG_SET_FLAG_MIN("unlinktarget", TUL);
437147191Sjkoshy		DBG_SET_FLAG_MIN("write",	WRI);
438147191Sjkoshy		if (found == 0) {
439147191Sjkoshy			/* unrecognized flag name */
440147191Sjkoshy			error = EINVAL;
441147191Sjkoshy			goto done;
442147191Sjkoshy		}
443147191Sjkoshy
444147191Sjkoshy		if (c == 0 || c == ' ' || c == '\t') {	/* end of flag group */
445147191Sjkoshy			*newbits = tmp;
446147191Sjkoshy			continue;
447147191Sjkoshy		}
448147191Sjkoshy
449147191Sjkoshy		p++;
450147191Sjkoshy		goto newflag;
451145256Sjkoshy	}
452145256Sjkoshy
453147191Sjkoshy	/* save the new flag set */
454147191Sjkoshy	bcopy(tmpflags, &pmc_debugflags, sizeof(pmc_debugflags));
455145256Sjkoshy
456147191Sjkoshy done:
457184205Sdes	free(tmpflags, M_PMC);
458147191Sjkoshy	return error;
459145256Sjkoshy}
460145256Sjkoshy
461145256Sjkoshystatic int
462145256Sjkoshypmc_debugflags_sysctl_handler(SYSCTL_HANDLER_ARGS)
463145256Sjkoshy{
464145256Sjkoshy	char *fence, *newstr;
465145256Sjkoshy	int error;
466145256Sjkoshy	unsigned int n;
467145256Sjkoshy
468145256Sjkoshy	(void) arg1; (void) arg2; /* unused parameters */
469145256Sjkoshy
470145256Sjkoshy	n = sizeof(pmc_debugstr);
471184802Sjkoshy	newstr = malloc(n, M_PMC, M_WAITOK|M_ZERO);
472147191Sjkoshy	(void) strlcpy(newstr, pmc_debugstr, n);
473145256Sjkoshy
474145256Sjkoshy	error = sysctl_handle_string(oidp, newstr, n, req);
475145256Sjkoshy
476145256Sjkoshy	/* if there is a new string, parse and copy it */
477145256Sjkoshy	if (error == 0 && req->newptr != NULL) {
478147191Sjkoshy		fence = newstr + (n < req->newlen ? n : req->newlen + 1);
479145256Sjkoshy		if ((error = pmc_debugflags_parse(newstr, fence)) == 0)
480145256Sjkoshy			(void) strlcpy(pmc_debugstr, newstr,
481145256Sjkoshy			    sizeof(pmc_debugstr));
482145256Sjkoshy	}
483145256Sjkoshy
484184205Sdes	free(newstr, M_PMC);
485145256Sjkoshy
486145256Sjkoshy	return error;
487145256Sjkoshy}
488145256Sjkoshy#endif
489145256Sjkoshy
490145256Sjkoshy/*
491184802Sjkoshy * Map a row index to a classdep structure and return the adjusted row
492184802Sjkoshy * index for the PMC class index.
493184802Sjkoshy */
494184802Sjkoshystatic struct pmc_classdep *
495184802Sjkoshypmc_ri_to_classdep(struct pmc_mdep *md, int ri, int *adjri)
496184802Sjkoshy{
497184802Sjkoshy	struct pmc_classdep *pcd;
498184802Sjkoshy
499184802Sjkoshy	(void) md;
500184802Sjkoshy
501184802Sjkoshy	KASSERT(ri >= 0 && ri < md->pmd_npmc,
502184802Sjkoshy	    ("[pmc,%d] illegal row-index %d", __LINE__, ri));
503184802Sjkoshy
504184802Sjkoshy	pcd = pmc_rowindex_to_classdep[ri];
505184802Sjkoshy
506184802Sjkoshy	KASSERT(pcd != NULL,
507184802Sjkoshy	    ("[amd,%d] ri %d null pcd", __LINE__, ri));
508184802Sjkoshy
509184802Sjkoshy	*adjri = ri - pcd->pcd_ri;
510184802Sjkoshy
511184802Sjkoshy	KASSERT(*adjri >= 0 && *adjri < pcd->pcd_num,
512184802Sjkoshy	    ("[pmc,%d] adjusted row-index %d", __LINE__, *adjri));
513184802Sjkoshy
514184802Sjkoshy	return (pcd);
515184802Sjkoshy}
516184802Sjkoshy
517184802Sjkoshy/*
518145256Sjkoshy * Concurrency Control
519145256Sjkoshy *
520145256Sjkoshy * The driver manages the following data structures:
521145256Sjkoshy *
522145256Sjkoshy *   - target process descriptors, one per target process
523145256Sjkoshy *   - owner process descriptors (and attached lists), one per owner process
524145256Sjkoshy *   - lookup hash tables for owner and target processes
525145256Sjkoshy *   - PMC descriptors (and attached lists)
526145256Sjkoshy *   - per-cpu hardware state
527145256Sjkoshy *   - the 'hook' variable through which the kernel calls into
528145256Sjkoshy *     this module
529145256Sjkoshy *   - the machine hardware state (managed by the MD layer)
530145256Sjkoshy *
531145256Sjkoshy * These data structures are accessed from:
532145256Sjkoshy *
533145256Sjkoshy * - thread context-switch code
534145256Sjkoshy * - interrupt handlers (possibly on multiple cpus)
535145256Sjkoshy * - kernel threads on multiple cpus running on behalf of user
536145256Sjkoshy *   processes doing system calls
537145256Sjkoshy * - this driver's private kernel threads
538145256Sjkoshy *
539145256Sjkoshy * = Locks and Locking strategy =
540145256Sjkoshy *
541145256Sjkoshy * The driver uses four locking strategies for its operation:
542145256Sjkoshy *
543168856Sjkoshy * - The global SX lock "pmc_sx" is used to protect internal
544168856Sjkoshy *   data structures.
545145256Sjkoshy *
546168856Sjkoshy *   Calls into the module by syscall() start with this lock being
547168856Sjkoshy *   held in exclusive mode.  Depending on the requested operation,
548168856Sjkoshy *   the lock may be downgraded to 'shared' mode to allow more
549168856Sjkoshy *   concurrent readers into the module.  Calls into the module from
550168856Sjkoshy *   other parts of the kernel acquire the lock in shared mode.
551145256Sjkoshy *
552145256Sjkoshy *   This SX lock is held in exclusive mode for any operations that
553145256Sjkoshy *   modify the linkages between the driver's internal data structures.
554145256Sjkoshy *
555145256Sjkoshy *   The 'pmc_hook' function pointer is also protected by this lock.
556145256Sjkoshy *   It is only examined with the sx lock held in exclusive mode.  The
557168856Sjkoshy *   kernel module is allowed to be unloaded only with the sx lock held
558168856Sjkoshy *   in exclusive mode.  In normal syscall handling, after acquiring the
559168856Sjkoshy *   pmc_sx lock we first check that 'pmc_hook' is non-null before
560168856Sjkoshy *   proceeding.  This prevents races between the thread unloading the module
561168856Sjkoshy *   and other threads seeking to use the module.
562145256Sjkoshy *
563145256Sjkoshy * - Lookups of target process structures and owner process structures
564145256Sjkoshy *   cannot use the global "pmc_sx" SX lock because these lookups need
565145256Sjkoshy *   to happen during context switches and in other critical sections
566145256Sjkoshy *   where sleeping is not allowed.  We protect these lookup tables
567145256Sjkoshy *   with their own private spin-mutexes, "pmc_processhash_mtx" and
568168856Sjkoshy *   "pmc_ownerhash_mtx".
569145256Sjkoshy *
570145256Sjkoshy * - Interrupt handlers work in a lock free manner.  At interrupt
571145256Sjkoshy *   time, handlers look at the PMC pointer (phw->phw_pmc) configured
572145256Sjkoshy *   when the PMC was started.  If this pointer is NULL, the interrupt
573145256Sjkoshy *   is ignored after updating driver statistics.  We ensure that this
574145256Sjkoshy *   pointer is set (using an atomic operation if necessary) before the
575145256Sjkoshy *   PMC hardware is started.  Conversely, this pointer is unset atomically
576145256Sjkoshy *   only after the PMC hardware is stopped.
577145256Sjkoshy *
578145256Sjkoshy *   We ensure that everything needed for the operation of an
579145256Sjkoshy *   interrupt handler is available without it needing to acquire any
580145256Sjkoshy *   locks.  We also ensure that a PMC's software state is destroyed only
581145256Sjkoshy *   after the PMC is taken off hardware (on all CPUs).
582145256Sjkoshy *
583145256Sjkoshy * - Context-switch handling with process-private PMCs needs more
584145256Sjkoshy *   care.
585145256Sjkoshy *
586145256Sjkoshy *   A given process may be the target of multiple PMCs.  For example,
587145256Sjkoshy *   PMCATTACH and PMCDETACH may be requested by a process on one CPU
588145256Sjkoshy *   while the target process is running on another.  A PMC could also
589145256Sjkoshy *   be getting released because its owner is exiting.  We tackle
590145256Sjkoshy *   these situations in the following manner:
591145256Sjkoshy *
592145256Sjkoshy *   - each target process structure 'pmc_process' has an array
593145256Sjkoshy *     of 'struct pmc *' pointers, one for each hardware PMC.
594145256Sjkoshy *
595145256Sjkoshy *   - At context switch IN time, each "target" PMC in RUNNING state
596145256Sjkoshy *     gets started on hardware and a pointer to each PMC is copied into
597145256Sjkoshy *     the per-cpu phw array.  The 'runcount' for the PMC is
598145256Sjkoshy *     incremented.
599145256Sjkoshy *
600145256Sjkoshy *   - At context switch OUT time, all process-virtual PMCs are stopped
601145256Sjkoshy *     on hardware.  The saved value is added to the PMCs value field
602145256Sjkoshy *     only if the PMC is in a non-deleted state (the PMCs state could
603145256Sjkoshy *     have changed during the current time slice).
604145256Sjkoshy *
605145256Sjkoshy *     Note that since in-between a switch IN on a processor and a switch
606145256Sjkoshy *     OUT, the PMC could have been released on another CPU.  Therefore
607145256Sjkoshy *     context switch OUT always looks at the hardware state to turn
608145256Sjkoshy *     OFF PMCs and will update a PMC's saved value only if reachable
609145256Sjkoshy *     from the target process record.
610145256Sjkoshy *
611145256Sjkoshy *   - OP PMCRELEASE could be called on a PMC at any time (the PMC could
612145256Sjkoshy *     be attached to many processes at the time of the call and could
613145256Sjkoshy *     be active on multiple CPUs).
614145256Sjkoshy *
615145256Sjkoshy *     We prevent further scheduling of the PMC by marking it as in
616145256Sjkoshy *     state 'DELETED'.  If the runcount of the PMC is non-zero then
617145256Sjkoshy *     this PMC is currently running on a CPU somewhere.  The thread
618167086Sjhb *     doing the PMCRELEASE operation waits by repeatedly doing a
619167086Sjhb *     pause() till the runcount comes to zero.
620145256Sjkoshy *
621168856Sjkoshy * The contents of a PMC descriptor (struct pmc) are protected using
622168856Sjkoshy * a spin-mutex.  In order to save space, we use a mutex pool.
623168856Sjkoshy *
624168856Sjkoshy * In terms of lock types used by witness(4), we use:
625168856Sjkoshy * - Type "pmc-sx", used by the global SX lock.
626168856Sjkoshy * - Type "pmc-sleep", for sleep mutexes used by logger threads.
627168856Sjkoshy * - Type "pmc-per-proc", for protecting PMC owner descriptors.
628168856Sjkoshy * - Type "pmc-leaf", used for all other spin mutexes.
629145256Sjkoshy */
630145256Sjkoshy
631145256Sjkoshy/*
632145256Sjkoshy * save the cpu binding of the current kthread
633145256Sjkoshy */
634145256Sjkoshy
635145256Sjkoshystatic void
636145256Sjkoshypmc_save_cpu_binding(struct pmc_binding *pb)
637145256Sjkoshy{
638145256Sjkoshy	PMCDBG(CPU,BND,2, "%s", "save-cpu");
639170307Sjeff	thread_lock(curthread);
640145256Sjkoshy	pb->pb_bound = sched_is_bound(curthread);
641145256Sjkoshy	pb->pb_cpu   = curthread->td_oncpu;
642170307Sjeff	thread_unlock(curthread);
643145256Sjkoshy	PMCDBG(CPU,BND,2, "save-cpu cpu=%d", pb->pb_cpu);
644145256Sjkoshy}
645145256Sjkoshy
646145256Sjkoshy/*
647145256Sjkoshy * restore the cpu binding of the current thread
648145256Sjkoshy */
649145256Sjkoshy
650145256Sjkoshystatic void
651145256Sjkoshypmc_restore_cpu_binding(struct pmc_binding *pb)
652145256Sjkoshy{
653145256Sjkoshy	PMCDBG(CPU,BND,2, "restore-cpu curcpu=%d restore=%d",
654145256Sjkoshy	    curthread->td_oncpu, pb->pb_cpu);
655170307Sjeff	thread_lock(curthread);
656145256Sjkoshy	if (pb->pb_bound)
657145256Sjkoshy		sched_bind(curthread, pb->pb_cpu);
658145256Sjkoshy	else
659145256Sjkoshy		sched_unbind(curthread);
660170307Sjeff	thread_unlock(curthread);
661145256Sjkoshy	PMCDBG(CPU,BND,2, "%s", "restore-cpu done");
662145256Sjkoshy}
663145256Sjkoshy
664145256Sjkoshy/*
665145256Sjkoshy * move execution over the specified cpu and bind it there.
666145256Sjkoshy */
667145256Sjkoshy
668145256Sjkoshystatic void
669145256Sjkoshypmc_select_cpu(int cpu)
670145256Sjkoshy{
671183266Sjkoshy	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
672145256Sjkoshy	    ("[pmc,%d] bad cpu number %d", __LINE__, cpu));
673145256Sjkoshy
674183266Sjkoshy	/* Never move to an inactive CPU. */
675183266Sjkoshy	KASSERT(pmc_cpu_is_active(cpu), ("[pmc,%d] selecting inactive "
676183266Sjkoshy	    "CPU %d", __LINE__, cpu));
677145256Sjkoshy
678145256Sjkoshy	PMCDBG(CPU,SEL,2, "select-cpu cpu=%d", cpu);
679170307Sjeff	thread_lock(curthread);
680145256Sjkoshy	sched_bind(curthread, cpu);
681170307Sjeff	thread_unlock(curthread);
682145256Sjkoshy
683145256Sjkoshy	KASSERT(curthread->td_oncpu == cpu,
684145256Sjkoshy	    ("[pmc,%d] CPU not bound [cpu=%d, curr=%d]", __LINE__,
685145256Sjkoshy		cpu, curthread->td_oncpu));
686145256Sjkoshy
687145256Sjkoshy	PMCDBG(CPU,SEL,2, "select-cpu cpu=%d ok", cpu);
688145256Sjkoshy}
689145256Sjkoshy
690145256Sjkoshy/*
691145774Sjkoshy * Force a context switch.
692145774Sjkoshy *
693167086Sjhb * We do this by pause'ing for 1 tick -- invoking mi_switch() is not
694145774Sjkoshy * guaranteed to force a context switch.
695145774Sjkoshy */
696145774Sjkoshy
697145774Sjkoshystatic void
698145774Sjkoshypmc_force_context_switch(void)
699145774Sjkoshy{
700145774Sjkoshy
701167086Sjhb	pause("pmcctx", 1);
702145774Sjkoshy}
703145774Sjkoshy
704145774Sjkoshy/*
705147191Sjkoshy * Get the file name for an executable.  This is a simple wrapper
706147191Sjkoshy * around vn_fullpath(9).
707145256Sjkoshy */
708145256Sjkoshy
709147191Sjkoshystatic void
710147708Sjkoshypmc_getfilename(struct vnode *v, char **fullpath, char **freepath)
711145256Sjkoshy{
712145256Sjkoshy
713147191Sjkoshy	*fullpath = "unknown";
714147191Sjkoshy	*freepath = NULL;
715175294Sattilio	vn_fullpath(curthread, v, fullpath, freepath);
716145256Sjkoshy}
717145256Sjkoshy
718145256Sjkoshy/*
719145256Sjkoshy * remove an process owning PMCs
720145256Sjkoshy */
721145256Sjkoshy
722145256Sjkoshyvoid
723145256Sjkoshypmc_remove_owner(struct pmc_owner *po)
724145256Sjkoshy{
725147191Sjkoshy	struct pmc *pm, *tmp;
726145256Sjkoshy
727145256Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
728145256Sjkoshy
729145256Sjkoshy	PMCDBG(OWN,ORM,1, "remove-owner po=%p", po);
730145256Sjkoshy
731145256Sjkoshy	/* Remove descriptor from the owner hash table */
732145256Sjkoshy	LIST_REMOVE(po, po_next);
733145256Sjkoshy
734147191Sjkoshy	/* release all owned PMC descriptors */
735147191Sjkoshy	LIST_FOREACH_SAFE(pm, &po->po_pmcs, pm_next, tmp) {
736147191Sjkoshy		PMCDBG(OWN,ORM,2, "pmc=%p", pm);
737147191Sjkoshy		KASSERT(pm->pm_owner == po,
738147191Sjkoshy		    ("[pmc,%d] owner %p != po %p", __LINE__, pm->pm_owner, po));
739145256Sjkoshy
740147191Sjkoshy		pmc_release_pmc_descriptor(pm);	/* will unlink from the list */
741145256Sjkoshy	}
742145256Sjkoshy
743147191Sjkoshy	KASSERT(po->po_sscount == 0,
744147191Sjkoshy	    ("[pmc,%d] SS count not zero", __LINE__));
745145256Sjkoshy	KASSERT(LIST_EMPTY(&po->po_pmcs),
746147191Sjkoshy	    ("[pmc,%d] PMC list not empty", __LINE__));
747145256Sjkoshy
748147191Sjkoshy	/* de-configure the log file if present */
749145774Sjkoshy	if (po->po_flags & PMC_PO_OWNS_LOGFILE)
750147191Sjkoshy		pmclog_deconfigure_log(po);
751145256Sjkoshy}
752145256Sjkoshy
753145256Sjkoshy/*
754145256Sjkoshy * remove an owner process record if all conditions are met.
755145256Sjkoshy */
756145256Sjkoshy
757145256Sjkoshystatic void
758145256Sjkoshypmc_maybe_remove_owner(struct pmc_owner *po)
759145256Sjkoshy{
760145256Sjkoshy
761145256Sjkoshy	PMCDBG(OWN,OMR,1, "maybe-remove-owner po=%p", po);
762145256Sjkoshy
763145256Sjkoshy	/*
764145256Sjkoshy	 * Remove owner record if
765145256Sjkoshy	 * - this process does not own any PMCs
766145256Sjkoshy	 * - this process has not allocated a system-wide sampling buffer
767145256Sjkoshy	 */
768145256Sjkoshy
769145256Sjkoshy	if (LIST_EMPTY(&po->po_pmcs) &&
770145774Sjkoshy	    ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0)) {
771145256Sjkoshy		pmc_remove_owner(po);
772147191Sjkoshy		pmc_destroy_owner_descriptor(po);
773145256Sjkoshy	}
774145256Sjkoshy}
775145256Sjkoshy
776145256Sjkoshy/*
777145256Sjkoshy * Add an association between a target process and a PMC.
778145256Sjkoshy */
779145256Sjkoshy
780145256Sjkoshystatic void
781145256Sjkoshypmc_link_target_process(struct pmc *pm, struct pmc_process *pp)
782145256Sjkoshy{
783145256Sjkoshy	int ri;
784145256Sjkoshy	struct pmc_target *pt;
785145256Sjkoshy
786145256Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
787145256Sjkoshy
788145256Sjkoshy	KASSERT(pm != NULL && pp != NULL,
789145256Sjkoshy	    ("[pmc,%d] Null pm %p or pp %p", __LINE__, pm, pp));
790147191Sjkoshy	KASSERT(PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm)),
791147191Sjkoshy	    ("[pmc,%d] Attaching a non-process-virtual pmc=%p to pid=%d",
792147191Sjkoshy		__LINE__, pm, pp->pp_proc->p_pid));
793145256Sjkoshy	KASSERT(pp->pp_refcnt >= 0 && pp->pp_refcnt < ((int) md->pmd_npmc - 1),
794145256Sjkoshy	    ("[pmc,%d] Illegal reference count %d for process record %p",
795145256Sjkoshy		__LINE__, pp->pp_refcnt, (void *) pp));
796145256Sjkoshy
797145774Sjkoshy	ri = PMC_TO_ROWINDEX(pm);
798145256Sjkoshy
799145256Sjkoshy	PMCDBG(PRC,TLK,1, "link-target pmc=%p ri=%d pmc-process=%p",
800145256Sjkoshy	    pm, ri, pp);
801145256Sjkoshy
802153110Sru#ifdef	DEBUG
803145256Sjkoshy	LIST_FOREACH(pt, &pm->pm_targets, pt_next)
804145256Sjkoshy	    if (pt->pt_process == pp)
805145256Sjkoshy		    KASSERT(0, ("[pmc,%d] pp %p already in pmc %p targets",
806145256Sjkoshy				__LINE__, pp, pm));
807145256Sjkoshy#endif
808145256Sjkoshy
809184802Sjkoshy	pt = malloc(sizeof(struct pmc_target), M_PMC, M_WAITOK|M_ZERO);
810145256Sjkoshy	pt->pt_process = pp;
811145256Sjkoshy
812145256Sjkoshy	LIST_INSERT_HEAD(&pm->pm_targets, pt, pt_next);
813145256Sjkoshy
814148067Sjhb	atomic_store_rel_ptr((uintptr_t *)&pp->pp_pmcs[ri].pp_pmc,
815148067Sjhb	    (uintptr_t)pm);
816145256Sjkoshy
817145615Sjkoshy	if (pm->pm_owner->po_owner == pp->pp_proc)
818145774Sjkoshy		pm->pm_flags |= PMC_F_ATTACHED_TO_OWNER;
819145615Sjkoshy
820147191Sjkoshy	/*
821147191Sjkoshy	 * Initialize the per-process values at this row index.
822147191Sjkoshy	 */
823147191Sjkoshy	pp->pp_pmcs[ri].pp_pmcval = PMC_TO_MODE(pm) == PMC_MODE_TS ?
824147191Sjkoshy	    pm->pm_sc.pm_reloadcount : 0;
825147191Sjkoshy
826145256Sjkoshy	pp->pp_refcnt++;
827145256Sjkoshy
828145256Sjkoshy}
829145256Sjkoshy
830145256Sjkoshy/*
831145256Sjkoshy * Removes the association between a target process and a PMC.
832145256Sjkoshy */
833145256Sjkoshy
834145256Sjkoshystatic void
835145256Sjkoshypmc_unlink_target_process(struct pmc *pm, struct pmc_process *pp)
836145256Sjkoshy{
837145256Sjkoshy	int ri;
838147191Sjkoshy	struct proc *p;
839145256Sjkoshy	struct pmc_target *ptgt;
840145256Sjkoshy
841145256Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
842145256Sjkoshy
843145256Sjkoshy	KASSERT(pm != NULL && pp != NULL,
844145256Sjkoshy	    ("[pmc,%d] Null pm %p or pp %p", __LINE__, pm, pp));
845145256Sjkoshy
846145256Sjkoshy	KASSERT(pp->pp_refcnt >= 1 && pp->pp_refcnt < (int) md->pmd_npmc,
847145256Sjkoshy	    ("[pmc,%d] Illegal ref count %d on process record %p",
848145256Sjkoshy		__LINE__, pp->pp_refcnt, (void *) pp));
849145256Sjkoshy
850145774Sjkoshy	ri = PMC_TO_ROWINDEX(pm);
851145256Sjkoshy
852145256Sjkoshy	PMCDBG(PRC,TUL,1, "unlink-target pmc=%p ri=%d pmc-process=%p",
853145256Sjkoshy	    pm, ri, pp);
854145256Sjkoshy
855145256Sjkoshy	KASSERT(pp->pp_pmcs[ri].pp_pmc == pm,
856145256Sjkoshy	    ("[pmc,%d] PMC ri %d mismatch pmc %p pp->[ri] %p", __LINE__,
857145256Sjkoshy		ri, pm, pp->pp_pmcs[ri].pp_pmc));
858145256Sjkoshy
859145256Sjkoshy	pp->pp_pmcs[ri].pp_pmc = NULL;
860145256Sjkoshy	pp->pp_pmcs[ri].pp_pmcval = (pmc_value_t) 0;
861145256Sjkoshy
862145774Sjkoshy	/* Remove owner-specific flags */
863145774Sjkoshy	if (pm->pm_owner->po_owner == pp->pp_proc) {
864145774Sjkoshy		pp->pp_flags &= ~PMC_PP_ENABLE_MSR_ACCESS;
865145774Sjkoshy		pm->pm_flags &= ~PMC_F_ATTACHED_TO_OWNER;
866145774Sjkoshy	}
867145615Sjkoshy
868145256Sjkoshy	pp->pp_refcnt--;
869145256Sjkoshy
870145256Sjkoshy	/* Remove the target process from the PMC structure */
871145256Sjkoshy	LIST_FOREACH(ptgt, &pm->pm_targets, pt_next)
872145256Sjkoshy		if (ptgt->pt_process == pp)
873145256Sjkoshy			break;
874145256Sjkoshy
875145256Sjkoshy	KASSERT(ptgt != NULL, ("[pmc,%d] process %p (pp: %p) not found "
876145256Sjkoshy		    "in pmc %p", __LINE__, pp->pp_proc, pp, pm));
877145256Sjkoshy
878145256Sjkoshy	LIST_REMOVE(ptgt, pt_next);
879184205Sdes	free(ptgt, M_PMC);
880145256Sjkoshy
881147191Sjkoshy	/* if the PMC now lacks targets, send the owner a SIGIO */
882147191Sjkoshy	if (LIST_EMPTY(&pm->pm_targets)) {
883147191Sjkoshy		p = pm->pm_owner->po_owner;
884147191Sjkoshy		PROC_LOCK(p);
885147191Sjkoshy		psignal(p, SIGIO);
886147191Sjkoshy		PROC_UNLOCK(p);
887145256Sjkoshy
888147191Sjkoshy		PMCDBG(PRC,SIG,2, "signalling proc=%p signal=%d", p,
889147191Sjkoshy		    SIGIO);
890145256Sjkoshy	}
891145256Sjkoshy}
892145256Sjkoshy
893145256Sjkoshy/*
894145256Sjkoshy * Check if PMC 'pm' may be attached to target process 't'.
895145256Sjkoshy */
896145256Sjkoshy
897145256Sjkoshystatic int
898145256Sjkoshypmc_can_attach(struct pmc *pm, struct proc *t)
899145256Sjkoshy{
900145256Sjkoshy	struct proc *o;		/* pmc owner */
901145256Sjkoshy	struct ucred *oc, *tc;	/* owner, target credentials */
902145256Sjkoshy	int decline_attach, i;
903145256Sjkoshy
904145256Sjkoshy	/*
905145256Sjkoshy	 * A PMC's owner can always attach that PMC to itself.
906145256Sjkoshy	 */
907145256Sjkoshy
908145256Sjkoshy	if ((o = pm->pm_owner->po_owner) == t)
909145256Sjkoshy		return 0;
910145256Sjkoshy
911145256Sjkoshy	PROC_LOCK(o);
912145256Sjkoshy	oc = o->p_ucred;
913145256Sjkoshy	crhold(oc);
914145256Sjkoshy	PROC_UNLOCK(o);
915145256Sjkoshy
916145256Sjkoshy	PROC_LOCK(t);
917145256Sjkoshy	tc = t->p_ucred;
918145256Sjkoshy	crhold(tc);
919145256Sjkoshy	PROC_UNLOCK(t);
920145256Sjkoshy
921145256Sjkoshy	/*
922145256Sjkoshy	 * The effective uid of the PMC owner should match at least one
923145256Sjkoshy	 * of the {effective,real,saved} uids of the target process.
924145256Sjkoshy	 */
925145256Sjkoshy
926145256Sjkoshy	decline_attach = oc->cr_uid != tc->cr_uid &&
927145256Sjkoshy	    oc->cr_uid != tc->cr_svuid &&
928145256Sjkoshy	    oc->cr_uid != tc->cr_ruid;
929145256Sjkoshy
930145256Sjkoshy	/*
931145256Sjkoshy	 * Every one of the target's group ids, must be in the owner's
932145256Sjkoshy	 * group list.
933145256Sjkoshy	 */
934145256Sjkoshy	for (i = 0; !decline_attach && i < tc->cr_ngroups; i++)
935145256Sjkoshy		decline_attach = !groupmember(tc->cr_groups[i], oc);
936145256Sjkoshy
937145256Sjkoshy	/* check the read and saved gids too */
938145256Sjkoshy	if (decline_attach == 0)
939145256Sjkoshy		decline_attach = !groupmember(tc->cr_rgid, oc) ||
940145256Sjkoshy		    !groupmember(tc->cr_svgid, oc);
941145256Sjkoshy
942145256Sjkoshy	crfree(tc);
943145256Sjkoshy	crfree(oc);
944145256Sjkoshy
945145256Sjkoshy	return !decline_attach;
946145256Sjkoshy}
947145256Sjkoshy
948145256Sjkoshy/*
949145256Sjkoshy * Attach a process to a PMC.
950145256Sjkoshy */
951145256Sjkoshy
952145256Sjkoshystatic int
953145256Sjkoshypmc_attach_one_process(struct proc *p, struct pmc *pm)
954145256Sjkoshy{
955145256Sjkoshy	int ri;
956147191Sjkoshy	char *fullpath, *freepath;
957145256Sjkoshy	struct pmc_process	*pp;
958145256Sjkoshy
959145256Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
960145256Sjkoshy
961145256Sjkoshy	PMCDBG(PRC,ATT,2, "attach-one pm=%p ri=%d proc=%p (%d, %s)", pm,
962145774Sjkoshy	    PMC_TO_ROWINDEX(pm), p, p->p_pid, p->p_comm);
963145256Sjkoshy
964145256Sjkoshy	/*
965145256Sjkoshy	 * Locate the process descriptor corresponding to process 'p',
966145256Sjkoshy	 * allocating space as needed.
967145256Sjkoshy	 *
968145256Sjkoshy	 * Verify that rowindex 'pm_rowindex' is free in the process
969145256Sjkoshy	 * descriptor.
970145256Sjkoshy	 *
971145256Sjkoshy	 * If not, allocate space for a descriptor and link the
972145256Sjkoshy	 * process descriptor and PMC.
973145256Sjkoshy	 */
974145774Sjkoshy	ri = PMC_TO_ROWINDEX(pm);
975145256Sjkoshy
976145256Sjkoshy	if ((pp = pmc_find_process_descriptor(p, PMC_FLAG_ALLOCATE)) == NULL)
977145256Sjkoshy		return ENOMEM;
978145256Sjkoshy
979145256Sjkoshy	if (pp->pp_pmcs[ri].pp_pmc == pm) /* already present at slot [ri] */
980145256Sjkoshy		return EEXIST;
981145256Sjkoshy
982145256Sjkoshy	if (pp->pp_pmcs[ri].pp_pmc != NULL)
983145256Sjkoshy		return EBUSY;
984145256Sjkoshy
985145256Sjkoshy	pmc_link_target_process(pm, pp);
986145256Sjkoshy
987147191Sjkoshy	if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)) &&
988147191Sjkoshy	    (pm->pm_flags & PMC_F_ATTACHED_TO_OWNER) == 0)
989147191Sjkoshy		pm->pm_flags |= PMC_F_NEEDS_LOGFILE;
990147191Sjkoshy
991147191Sjkoshy	pm->pm_flags |= PMC_F_ATTACH_DONE; /* mark as attached */
992147191Sjkoshy
993147191Sjkoshy	/* issue an attach event to a configured log file */
994147191Sjkoshy	if (pm->pm_owner->po_flags & PMC_PO_OWNS_LOGFILE) {
995147708Sjkoshy		pmc_getfilename(p->p_textvp, &fullpath, &freepath);
996180794Sjeff		if (p->p_flag & P_KTHREAD) {
997180794Sjeff			fullpath = kernelname;
998180794Sjeff			freepath = NULL;
999180794Sjeff		} else
1000180794Sjeff			pmclog_process_pmcattach(pm, p->p_pid, fullpath);
1001147191Sjkoshy		if (freepath)
1002184205Sdes			free(freepath, M_TEMP);
1003174395Sjkoshy		if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
1004174395Sjkoshy			pmc_log_process_mappings(pm->pm_owner, p);
1005147191Sjkoshy	}
1006145256Sjkoshy	/* mark process as using HWPMCs */
1007145256Sjkoshy	PROC_LOCK(p);
1008145256Sjkoshy	p->p_flag |= P_HWPMC;
1009145256Sjkoshy	PROC_UNLOCK(p);
1010145256Sjkoshy
1011145256Sjkoshy	return 0;
1012145256Sjkoshy}
1013145256Sjkoshy
1014145256Sjkoshy/*
1015145256Sjkoshy * Attach a process and optionally its children
1016145256Sjkoshy */
1017145256Sjkoshy
1018145256Sjkoshystatic int
1019145256Sjkoshypmc_attach_process(struct proc *p, struct pmc *pm)
1020145256Sjkoshy{
1021145256Sjkoshy	int error;
1022145256Sjkoshy	struct proc *top;
1023145256Sjkoshy
1024145256Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
1025145256Sjkoshy
1026145256Sjkoshy	PMCDBG(PRC,ATT,1, "attach pm=%p ri=%d proc=%p (%d, %s)", pm,
1027145774Sjkoshy	    PMC_TO_ROWINDEX(pm), p, p->p_pid, p->p_comm);
1028145256Sjkoshy
1029145774Sjkoshy
1030145774Sjkoshy	/*
1031145774Sjkoshy	 * If this PMC successfully allowed a GETMSR operation
1032145774Sjkoshy	 * in the past, disallow further ATTACHes.
1033145774Sjkoshy	 */
1034145774Sjkoshy
1035145774Sjkoshy	if ((pm->pm_flags & PMC_PP_ENABLE_MSR_ACCESS) != 0)
1036145774Sjkoshy		return EPERM;
1037145774Sjkoshy
1038145256Sjkoshy	if ((pm->pm_flags & PMC_F_DESCENDANTS) == 0)
1039145256Sjkoshy		return pmc_attach_one_process(p, pm);
1040145256Sjkoshy
1041145256Sjkoshy	/*
1042145256Sjkoshy	 * Traverse all child processes, attaching them to
1043145256Sjkoshy	 * this PMC.
1044145256Sjkoshy	 */
1045145256Sjkoshy
1046145256Sjkoshy	sx_slock(&proctree_lock);
1047145256Sjkoshy
1048145256Sjkoshy	top = p;
1049145256Sjkoshy
1050145256Sjkoshy	for (;;) {
1051145256Sjkoshy		if ((error = pmc_attach_one_process(p, pm)) != 0)
1052145256Sjkoshy			break;
1053145256Sjkoshy		if (!LIST_EMPTY(&p->p_children))
1054145256Sjkoshy			p = LIST_FIRST(&p->p_children);
1055145256Sjkoshy		else for (;;) {
1056145256Sjkoshy			if (p == top)
1057145256Sjkoshy				goto done;
1058145256Sjkoshy			if (LIST_NEXT(p, p_sibling)) {
1059145256Sjkoshy				p = LIST_NEXT(p, p_sibling);
1060145256Sjkoshy				break;
1061145256Sjkoshy			}
1062145256Sjkoshy			p = p->p_pptr;
1063145256Sjkoshy		}
1064145256Sjkoshy	}
1065145256Sjkoshy
1066145256Sjkoshy	if (error)
1067145256Sjkoshy		(void) pmc_detach_process(top, pm);
1068145256Sjkoshy
1069145256Sjkoshy done:
1070145256Sjkoshy	sx_sunlock(&proctree_lock);
1071145256Sjkoshy	return error;
1072145256Sjkoshy}
1073145256Sjkoshy
1074145256Sjkoshy/*
1075145256Sjkoshy * Detach a process from a PMC.  If there are no other PMCs tracking
1076145256Sjkoshy * this process, remove the process structure from its hash table.  If
1077145256Sjkoshy * 'flags' contains PMC_FLAG_REMOVE, then free the process structure.
1078145256Sjkoshy */
1079145256Sjkoshy
1080145256Sjkoshystatic int
1081145256Sjkoshypmc_detach_one_process(struct proc *p, struct pmc *pm, int flags)
1082145256Sjkoshy{
1083145256Sjkoshy	int ri;
1084145256Sjkoshy	struct pmc_process *pp;
1085145256Sjkoshy
1086145256Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
1087145256Sjkoshy
1088145256Sjkoshy	KASSERT(pm != NULL,
1089145256Sjkoshy	    ("[pmc,%d] null pm pointer", __LINE__));
1090145256Sjkoshy
1091145774Sjkoshy	ri = PMC_TO_ROWINDEX(pm);
1092145774Sjkoshy
1093145256Sjkoshy	PMCDBG(PRC,ATT,2, "detach-one pm=%p ri=%d proc=%p (%d, %s) flags=0x%x",
1094145774Sjkoshy	    pm, ri, p, p->p_pid, p->p_comm, flags);
1095145256Sjkoshy
1096145256Sjkoshy	if ((pp = pmc_find_process_descriptor(p, 0)) == NULL)
1097145256Sjkoshy		return ESRCH;
1098145256Sjkoshy
1099145256Sjkoshy	if (pp->pp_pmcs[ri].pp_pmc != pm)
1100145256Sjkoshy		return EINVAL;
1101145256Sjkoshy
1102145256Sjkoshy	pmc_unlink_target_process(pm, pp);
1103145256Sjkoshy
1104147191Sjkoshy	/* Issue a detach entry if a log file is configured */
1105147191Sjkoshy	if (pm->pm_owner->po_flags & PMC_PO_OWNS_LOGFILE)
1106147191Sjkoshy		pmclog_process_pmcdetach(pm, p->p_pid);
1107147191Sjkoshy
1108145256Sjkoshy	/*
1109145256Sjkoshy	 * If there are no PMCs targetting this process, we remove its
1110145256Sjkoshy	 * descriptor from the target hash table and unset the P_HWPMC
1111145256Sjkoshy	 * flag in the struct proc.
1112145256Sjkoshy	 */
1113145256Sjkoshy	KASSERT(pp->pp_refcnt >= 0 && pp->pp_refcnt < (int) md->pmd_npmc,
1114145256Sjkoshy	    ("[pmc,%d] Illegal refcnt %d for process struct %p",
1115145256Sjkoshy		__LINE__, pp->pp_refcnt, pp));
1116145256Sjkoshy
1117145256Sjkoshy	if (pp->pp_refcnt != 0)	/* still a target of some PMC */
1118145256Sjkoshy		return 0;
1119145256Sjkoshy
1120145256Sjkoshy	pmc_remove_process_descriptor(pp);
1121145256Sjkoshy
1122145256Sjkoshy	if (flags & PMC_FLAG_REMOVE)
1123184205Sdes		free(pp, M_PMC);
1124145256Sjkoshy
1125145256Sjkoshy	PROC_LOCK(p);
1126145256Sjkoshy	p->p_flag &= ~P_HWPMC;
1127145256Sjkoshy	PROC_UNLOCK(p);
1128145256Sjkoshy
1129145256Sjkoshy	return 0;
1130145256Sjkoshy}
1131145256Sjkoshy
1132145256Sjkoshy/*
1133145256Sjkoshy * Detach a process and optionally its descendants from a PMC.
1134145256Sjkoshy */
1135145256Sjkoshy
1136145256Sjkoshystatic int
1137145256Sjkoshypmc_detach_process(struct proc *p, struct pmc *pm)
1138145256Sjkoshy{
1139145256Sjkoshy	struct proc *top;
1140145256Sjkoshy
1141145256Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
1142145256Sjkoshy
1143145256Sjkoshy	PMCDBG(PRC,ATT,1, "detach pm=%p ri=%d proc=%p (%d, %s)", pm,
1144145774Sjkoshy	    PMC_TO_ROWINDEX(pm), p, p->p_pid, p->p_comm);
1145145256Sjkoshy
1146145256Sjkoshy	if ((pm->pm_flags & PMC_F_DESCENDANTS) == 0)
1147145256Sjkoshy		return pmc_detach_one_process(p, pm, PMC_FLAG_REMOVE);
1148145256Sjkoshy
1149145256Sjkoshy	/*
1150145256Sjkoshy	 * Traverse all children, detaching them from this PMC.  We
1151145256Sjkoshy	 * ignore errors since we could be detaching a PMC from a
1152145256Sjkoshy	 * partially attached proc tree.
1153145256Sjkoshy	 */
1154145256Sjkoshy
1155145256Sjkoshy	sx_slock(&proctree_lock);
1156145256Sjkoshy
1157145256Sjkoshy	top = p;
1158145256Sjkoshy
1159145256Sjkoshy	for (;;) {
1160145256Sjkoshy		(void) pmc_detach_one_process(p, pm, PMC_FLAG_REMOVE);
1161145256Sjkoshy
1162145256Sjkoshy		if (!LIST_EMPTY(&p->p_children))
1163145256Sjkoshy			p = LIST_FIRST(&p->p_children);
1164145256Sjkoshy		else for (;;) {
1165145256Sjkoshy			if (p == top)
1166145256Sjkoshy				goto done;
1167145256Sjkoshy			if (LIST_NEXT(p, p_sibling)) {
1168145256Sjkoshy				p = LIST_NEXT(p, p_sibling);
1169145256Sjkoshy				break;
1170145256Sjkoshy			}
1171145256Sjkoshy			p = p->p_pptr;
1172145256Sjkoshy		}
1173145256Sjkoshy	}
1174145256Sjkoshy
1175145256Sjkoshy done:
1176145256Sjkoshy	sx_sunlock(&proctree_lock);
1177147191Sjkoshy
1178147191Sjkoshy	if (LIST_EMPTY(&pm->pm_targets))
1179147191Sjkoshy		pm->pm_flags &= ~PMC_F_ATTACH_DONE;
1180147191Sjkoshy
1181145256Sjkoshy	return 0;
1182145256Sjkoshy}
1183145256Sjkoshy
1184147191Sjkoshy
1185145256Sjkoshy/*
1186147191Sjkoshy * Thread context switch IN
1187145256Sjkoshy */
1188145256Sjkoshy
1189147191Sjkoshystatic void
1190147191Sjkoshypmc_process_csw_in(struct thread *td)
1191147191Sjkoshy{
1192147191Sjkoshy	int cpu;
1193184802Sjkoshy	unsigned int adjri, ri;
1194147191Sjkoshy	struct pmc *pm;
1195147191Sjkoshy	struct proc *p;
1196147191Sjkoshy	struct pmc_cpu *pc;
1197147191Sjkoshy	struct pmc_hw *phw;
1198184802Sjkoshy	pmc_value_t newvalue;
1199147191Sjkoshy	struct pmc_process *pp;
1200184802Sjkoshy	struct pmc_classdep *pcd;
1201145256Sjkoshy
1202147191Sjkoshy	p = td->td_proc;
1203145256Sjkoshy
1204147191Sjkoshy	if ((pp = pmc_find_process_descriptor(p, PMC_FLAG_NONE)) == NULL)
1205147191Sjkoshy		return;
1206145256Sjkoshy
1207147191Sjkoshy	KASSERT(pp->pp_proc == td->td_proc,
1208147191Sjkoshy	    ("[pmc,%d] not my thread state", __LINE__));
1209145256Sjkoshy
1210147191Sjkoshy	critical_enter(); /* no preemption from this point */
1211145256Sjkoshy
1212147191Sjkoshy	cpu = PCPU_GET(cpuid); /* td->td_oncpu is invalid */
1213145256Sjkoshy
1214147191Sjkoshy	PMCDBG(CSW,SWI,1, "cpu=%d proc=%p (%d, %s) pp=%p", cpu, p,
1215147191Sjkoshy	    p->p_pid, p->p_comm, pp);
1216145256Sjkoshy
1217183266Sjkoshy	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
1218147191Sjkoshy	    ("[pmc,%d] wierd CPU id %d", __LINE__, cpu));
1219145256Sjkoshy
1220147191Sjkoshy	pc = pmc_pcpu[cpu];
1221145256Sjkoshy
1222147191Sjkoshy	for (ri = 0; ri < md->pmd_npmc; ri++) {
1223145256Sjkoshy
1224147191Sjkoshy		if ((pm = pp->pp_pmcs[ri].pp_pmc) == NULL)
1225147191Sjkoshy			continue;
1226147191Sjkoshy
1227147191Sjkoshy		KASSERT(PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm)),
1228147191Sjkoshy		    ("[pmc,%d] Target PMC in non-virtual mode (%d)",
1229147191Sjkoshy			__LINE__, PMC_TO_MODE(pm)));
1230147191Sjkoshy
1231147191Sjkoshy		KASSERT(PMC_TO_ROWINDEX(pm) == ri,
1232147191Sjkoshy		    ("[pmc,%d] Row index mismatch pmc %d != ri %d",
1233147191Sjkoshy			__LINE__, PMC_TO_ROWINDEX(pm), ri));
1234147191Sjkoshy
1235145256Sjkoshy		/*
1236147191Sjkoshy		 * Only PMCs that are marked as 'RUNNING' need
1237147191Sjkoshy		 * be placed on hardware.
1238145256Sjkoshy		 */
1239145256Sjkoshy
1240147191Sjkoshy		if (pm->pm_state != PMC_STATE_RUNNING)
1241147191Sjkoshy			continue;
1242145256Sjkoshy
1243147191Sjkoshy		/* increment PMC runcount */
1244147191Sjkoshy		atomic_add_rel_32(&pm->pm_runcount, 1);
1245145256Sjkoshy
1246147191Sjkoshy		/* configure the HWPMC we are going to use. */
1247184802Sjkoshy		pcd = pmc_ri_to_classdep(md, ri, &adjri);
1248184802Sjkoshy		pcd->pcd_config_pmc(cpu, adjri, pm);
1249145256Sjkoshy
1250147191Sjkoshy		phw = pc->pc_hwpmcs[ri];
1251145256Sjkoshy
1252147191Sjkoshy		KASSERT(phw != NULL,
1253147191Sjkoshy		    ("[pmc,%d] null hw pointer", __LINE__));
1254145256Sjkoshy
1255147191Sjkoshy		KASSERT(phw->phw_pmc == pm,
1256147191Sjkoshy		    ("[pmc,%d] hw->pmc %p != pmc %p", __LINE__,
1257147191Sjkoshy			phw->phw_pmc, pm));
1258145256Sjkoshy
1259147191Sjkoshy		/*
1260147191Sjkoshy		 * Write out saved value and start the PMC.
1261147191Sjkoshy		 *
1262147191Sjkoshy		 * Sampling PMCs use a per-process value, while
1263147191Sjkoshy		 * counting mode PMCs use a per-pmc value that is
1264147191Sjkoshy		 * inherited across descendants.
1265147191Sjkoshy		 */
1266147191Sjkoshy		if (PMC_TO_MODE(pm) == PMC_MODE_TS) {
1267147191Sjkoshy			mtx_pool_lock_spin(pmc_mtxpool, pm);
1268147191Sjkoshy			newvalue = PMC_PCPU_SAVED(cpu,ri) =
1269147191Sjkoshy			    pp->pp_pmcs[ri].pp_pmcval;
1270147191Sjkoshy			mtx_pool_unlock_spin(pmc_mtxpool, pm);
1271147191Sjkoshy		} else {
1272147191Sjkoshy			KASSERT(PMC_TO_MODE(pm) == PMC_MODE_TC,
1273147191Sjkoshy			    ("[pmc,%d] illegal mode=%d", __LINE__,
1274147191Sjkoshy			    PMC_TO_MODE(pm)));
1275147191Sjkoshy			mtx_pool_lock_spin(pmc_mtxpool, pm);
1276147191Sjkoshy			newvalue = PMC_PCPU_SAVED(cpu, ri) =
1277147191Sjkoshy			    pm->pm_gv.pm_savedvalue;
1278147191Sjkoshy			mtx_pool_unlock_spin(pmc_mtxpool, pm);
1279147191Sjkoshy		}
1280145256Sjkoshy
1281147191Sjkoshy		PMCDBG(CSW,SWI,1,"cpu=%d ri=%d new=%jd", cpu, ri, newvalue);
1282145256Sjkoshy
1283184802Sjkoshy		pcd->pcd_write_pmc(cpu, adjri, newvalue);
1284184802Sjkoshy		pcd->pcd_start_pmc(cpu, adjri);
1285147191Sjkoshy	}
1286145256Sjkoshy
1287147191Sjkoshy	/*
1288147191Sjkoshy	 * perform any other architecture/cpu dependent thread
1289147191Sjkoshy	 * switch-in actions.
1290147191Sjkoshy	 */
1291145256Sjkoshy
1292147191Sjkoshy	(void) (*md->pmd_switch_in)(pc, pp);
1293145256Sjkoshy
1294147191Sjkoshy	critical_exit();
1295145256Sjkoshy
1296147191Sjkoshy}
1297145256Sjkoshy
1298147191Sjkoshy/*
1299147191Sjkoshy * Thread context switch OUT.
1300147191Sjkoshy */
1301145256Sjkoshy
1302147191Sjkoshystatic void
1303147191Sjkoshypmc_process_csw_out(struct thread *td)
1304147191Sjkoshy{
1305147191Sjkoshy	int cpu;
1306184802Sjkoshy	int64_t tmp;
1307147191Sjkoshy	struct pmc *pm;
1308147191Sjkoshy	struct proc *p;
1309184802Sjkoshy	enum pmc_mode mode;
1310147191Sjkoshy	struct pmc_cpu *pc;
1311184802Sjkoshy	pmc_value_t newvalue;
1312184802Sjkoshy	unsigned int adjri, ri;
1313147191Sjkoshy	struct pmc_process *pp;
1314184802Sjkoshy	struct pmc_classdep *pcd;
1315145256Sjkoshy
1316184802Sjkoshy
1317147191Sjkoshy	/*
1318147191Sjkoshy	 * Locate our process descriptor; this may be NULL if
1319147191Sjkoshy	 * this process is exiting and we have already removed
1320147191Sjkoshy	 * the process from the target process table.
1321147191Sjkoshy	 *
1322147191Sjkoshy	 * Note that due to kernel preemption, multiple
1323147191Sjkoshy	 * context switches may happen while the process is
1324147191Sjkoshy	 * exiting.
1325147191Sjkoshy	 *
1326147191Sjkoshy	 * Note also that if the target process cannot be
1327147191Sjkoshy	 * found we still need to deconfigure any PMCs that
1328147191Sjkoshy	 * are currently running on hardware.
1329147191Sjkoshy	 */
1330145256Sjkoshy
1331147191Sjkoshy	p = td->td_proc;
1332147191Sjkoshy	pp = pmc_find_process_descriptor(p, PMC_FLAG_NONE);
1333145256Sjkoshy
1334147191Sjkoshy	/*
1335147191Sjkoshy	 * save PMCs
1336147191Sjkoshy	 */
1337145256Sjkoshy
1338147191Sjkoshy	critical_enter();
1339145774Sjkoshy
1340147191Sjkoshy	cpu = PCPU_GET(cpuid); /* td->td_oncpu is invalid */
1341145256Sjkoshy
1342147191Sjkoshy	PMCDBG(CSW,SWO,1, "cpu=%d proc=%p (%d, %s) pp=%p", cpu, p,
1343147191Sjkoshy	    p->p_pid, p->p_comm, pp);
1344145615Sjkoshy
1345183266Sjkoshy	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
1346147191Sjkoshy	    ("[pmc,%d wierd CPU id %d", __LINE__, cpu));
1347145615Sjkoshy
1348147191Sjkoshy	pc = pmc_pcpu[cpu];
1349145615Sjkoshy
1350147191Sjkoshy	/*
1351147191Sjkoshy	 * When a PMC gets unlinked from a target PMC, it will
1352147191Sjkoshy	 * be removed from the target's pp_pmc[] array.
1353147191Sjkoshy	 *
1354147191Sjkoshy	 * However, on a MP system, the target could have been
1355147191Sjkoshy	 * executing on another CPU at the time of the unlink.
1356147191Sjkoshy	 * So, at context switch OUT time, we need to look at
1357147191Sjkoshy	 * the hardware to determine if a PMC is scheduled on
1358147191Sjkoshy	 * it.
1359147191Sjkoshy	 */
1360145256Sjkoshy
1361147191Sjkoshy	for (ri = 0; ri < md->pmd_npmc; ri++) {
1362145256Sjkoshy
1363184802Sjkoshy		pcd = pmc_ri_to_classdep(md, ri, &adjri);
1364184802Sjkoshy		pm  = NULL;
1365184802Sjkoshy		(void) (*pcd->pcd_get_config)(cpu, adjri, &pm);
1366145256Sjkoshy
1367147191Sjkoshy		if (pm == NULL)	/* nothing at this row index */
1368147191Sjkoshy			continue;
1369145256Sjkoshy
1370147191Sjkoshy		mode = PMC_TO_MODE(pm);
1371147191Sjkoshy		if (!PMC_IS_VIRTUAL_MODE(mode))
1372147191Sjkoshy			continue; /* not a process virtual PMC */
1373145774Sjkoshy
1374147191Sjkoshy		KASSERT(PMC_TO_ROWINDEX(pm) == ri,
1375147191Sjkoshy		    ("[pmc,%d] ri mismatch pmc(%d) ri(%d)",
1376147191Sjkoshy			__LINE__, PMC_TO_ROWINDEX(pm), ri));
1377145256Sjkoshy
1378147191Sjkoshy		/* Stop hardware if not already stopped */
1379147867Sjkoshy		if (pm->pm_stalled == 0)
1380184802Sjkoshy			pcd->pcd_stop_pmc(cpu, adjri);
1381147191Sjkoshy
1382147191Sjkoshy		/* reduce this PMC's runcount */
1383147191Sjkoshy		atomic_subtract_rel_32(&pm->pm_runcount, 1);
1384147191Sjkoshy
1385145256Sjkoshy		/*
1386147191Sjkoshy		 * If this PMC is associated with this process,
1387147191Sjkoshy		 * save the reading.
1388145256Sjkoshy		 */
1389145256Sjkoshy
1390147191Sjkoshy		if (pp != NULL && pp->pp_pmcs[ri].pp_pmc != NULL) {
1391147191Sjkoshy
1392147191Sjkoshy			KASSERT(pm == pp->pp_pmcs[ri].pp_pmc,
1393147191Sjkoshy			    ("[pmc,%d] pm %p != pp_pmcs[%d] %p", __LINE__,
1394147191Sjkoshy				pm, ri, pp->pp_pmcs[ri].pp_pmc));
1395147191Sjkoshy
1396147191Sjkoshy			KASSERT(pp->pp_refcnt > 0,
1397147191Sjkoshy			    ("[pmc,%d] pp refcnt = %d", __LINE__,
1398147191Sjkoshy				pp->pp_refcnt));
1399147191Sjkoshy
1400184802Sjkoshy			pcd->pcd_read_pmc(cpu, adjri, &newvalue);
1401147191Sjkoshy
1402147191Sjkoshy			tmp = newvalue - PMC_PCPU_SAVED(cpu,ri);
1403147191Sjkoshy
1404147191Sjkoshy			PMCDBG(CSW,SWI,1,"cpu=%d ri=%d tmp=%jd", cpu, ri,
1405147191Sjkoshy			    tmp);
1406147191Sjkoshy
1407147191Sjkoshy			if (mode == PMC_MODE_TS) {
1408147191Sjkoshy
1409147191Sjkoshy				/*
1410147191Sjkoshy				 * For sampling process-virtual PMCs,
1411147191Sjkoshy				 * we expect the count to be
1412147191Sjkoshy				 * decreasing as the 'value'
1413147191Sjkoshy				 * programmed into the PMC is the
1414147191Sjkoshy				 * number of events to be seen till
1415147191Sjkoshy				 * the next sampling interrupt.
1416147191Sjkoshy				 */
1417147191Sjkoshy				if (tmp < 0)
1418147191Sjkoshy					tmp += pm->pm_sc.pm_reloadcount;
1419147191Sjkoshy				mtx_pool_lock_spin(pmc_mtxpool, pm);
1420147191Sjkoshy				pp->pp_pmcs[ri].pp_pmcval -= tmp;
1421147191Sjkoshy				if ((int64_t) pp->pp_pmcs[ri].pp_pmcval < 0)
1422147191Sjkoshy					pp->pp_pmcs[ri].pp_pmcval +=
1423147191Sjkoshy					    pm->pm_sc.pm_reloadcount;
1424147191Sjkoshy				mtx_pool_unlock_spin(pmc_mtxpool, pm);
1425147191Sjkoshy
1426147191Sjkoshy			} else {
1427147191Sjkoshy
1428147191Sjkoshy				/*
1429147191Sjkoshy				 * For counting process-virtual PMCs,
1430147191Sjkoshy				 * we expect the count to be
1431147191Sjkoshy				 * increasing monotonically, modulo a 64
1432147191Sjkoshy				 * bit wraparound.
1433147191Sjkoshy				 */
1434147191Sjkoshy				KASSERT((int64_t) tmp >= 0,
1435147191Sjkoshy				    ("[pmc,%d] negative increment cpu=%d "
1436147191Sjkoshy				     "ri=%d newvalue=%jx saved=%jx "
1437147191Sjkoshy				     "incr=%jx", __LINE__, cpu, ri,
1438147191Sjkoshy				     newvalue, PMC_PCPU_SAVED(cpu,ri), tmp));
1439147191Sjkoshy
1440147191Sjkoshy				mtx_pool_lock_spin(pmc_mtxpool, pm);
1441147191Sjkoshy				pm->pm_gv.pm_savedvalue += tmp;
1442147191Sjkoshy				pp->pp_pmcs[ri].pp_pmcval += tmp;
1443147191Sjkoshy				mtx_pool_unlock_spin(pmc_mtxpool, pm);
1444147191Sjkoshy
1445147191Sjkoshy				if (pm->pm_flags & PMC_F_LOG_PROCCSW)
1446147191Sjkoshy					pmclog_process_proccsw(pm, pp, tmp);
1447147191Sjkoshy			}
1448145256Sjkoshy		}
1449145256Sjkoshy
1450147191Sjkoshy		/* mark hardware as free */
1451184802Sjkoshy		pcd->pcd_config_pmc(cpu, adjri, NULL);
1452145256Sjkoshy	}
1453145256Sjkoshy
1454145256Sjkoshy	/*
1455147191Sjkoshy	 * perform any other architecture/cpu dependent thread
1456147191Sjkoshy	 * switch out functions.
1457147191Sjkoshy	 */
1458147191Sjkoshy
1459147191Sjkoshy	(void) (*md->pmd_switch_out)(pc, pp);
1460147191Sjkoshy
1461147191Sjkoshy	critical_exit();
1462147191Sjkoshy}
1463147191Sjkoshy
1464147191Sjkoshy/*
1465157144Sjkoshy * Log a KLD operation.
1466157144Sjkoshy */
1467157144Sjkoshy
1468157144Sjkoshystatic void
1469157144Sjkoshypmc_process_kld_load(struct pmckern_map_in *pkm)
1470157144Sjkoshy{
1471157144Sjkoshy	struct pmc_owner *po;
1472157144Sjkoshy
1473157144Sjkoshy	sx_assert(&pmc_sx, SX_LOCKED);
1474157144Sjkoshy
1475157144Sjkoshy	/*
1476157144Sjkoshy	 * Notify owners of system sampling PMCs about KLD operations.
1477157144Sjkoshy	 */
1478157144Sjkoshy
1479157144Sjkoshy	LIST_FOREACH(po, &pmc_ss_owners, po_ssnext)
1480157144Sjkoshy	    if (po->po_flags & PMC_PO_OWNS_LOGFILE)
1481157144Sjkoshy	    	pmclog_process_map_in(po, (pid_t) -1, pkm->pm_address,
1482157144Sjkoshy		    (char *) pkm->pm_file);
1483157144Sjkoshy
1484157144Sjkoshy	/*
1485157144Sjkoshy	 * TODO: Notify owners of (all) process-sampling PMCs too.
1486157144Sjkoshy	 */
1487157144Sjkoshy
1488157144Sjkoshy	return;
1489157144Sjkoshy}
1490157144Sjkoshy
1491157144Sjkoshystatic void
1492157144Sjkoshypmc_process_kld_unload(struct pmckern_map_out *pkm)
1493157144Sjkoshy{
1494157144Sjkoshy	struct pmc_owner *po;
1495157144Sjkoshy
1496157144Sjkoshy	sx_assert(&pmc_sx, SX_LOCKED);
1497157144Sjkoshy
1498157144Sjkoshy	LIST_FOREACH(po, &pmc_ss_owners, po_ssnext)
1499157144Sjkoshy	    if (po->po_flags & PMC_PO_OWNS_LOGFILE)
1500157144Sjkoshy		pmclog_process_map_out(po, (pid_t) -1,
1501157144Sjkoshy		    pkm->pm_address, pkm->pm_address + pkm->pm_size);
1502174395Sjkoshy
1503157144Sjkoshy	/*
1504157144Sjkoshy	 * TODO: Notify owners of process-sampling PMCs.
1505157144Sjkoshy	 */
1506157144Sjkoshy}
1507157144Sjkoshy
1508157144Sjkoshy/*
1509157144Sjkoshy * A mapping change for a process.
1510157144Sjkoshy */
1511157144Sjkoshy
1512157144Sjkoshystatic void
1513157144Sjkoshypmc_process_mmap(struct thread *td, struct pmckern_map_in *pkm)
1514157144Sjkoshy{
1515157144Sjkoshy	int ri;
1516157144Sjkoshy	pid_t pid;
1517157144Sjkoshy	char *fullpath, *freepath;
1518157144Sjkoshy	const struct pmc *pm;
1519157144Sjkoshy	struct pmc_owner *po;
1520157144Sjkoshy	const struct pmc_process *pp;
1521157144Sjkoshy
1522157144Sjkoshy	freepath = fullpath = NULL;
1523157144Sjkoshy	pmc_getfilename((struct vnode *) pkm->pm_file, &fullpath, &freepath);
1524157144Sjkoshy
1525157144Sjkoshy	pid = td->td_proc->p_pid;
1526157144Sjkoshy
1527157144Sjkoshy	/* Inform owners of all system-wide sampling PMCs. */
1528157144Sjkoshy	LIST_FOREACH(po, &pmc_ss_owners, po_ssnext)
1529157144Sjkoshy	    if (po->po_flags & PMC_PO_OWNS_LOGFILE)
1530157144Sjkoshy		pmclog_process_map_in(po, pid, pkm->pm_address, fullpath);
1531157144Sjkoshy
1532157144Sjkoshy	if ((pp = pmc_find_process_descriptor(td->td_proc, 0)) == NULL)
1533157144Sjkoshy		goto done;
1534157144Sjkoshy
1535157144Sjkoshy	/*
1536157144Sjkoshy	 * Inform sampling PMC owners tracking this process.
1537157144Sjkoshy	 */
1538157144Sjkoshy	for (ri = 0; ri < md->pmd_npmc; ri++)
1539157144Sjkoshy		if ((pm = pp->pp_pmcs[ri].pp_pmc) != NULL &&
1540157144Sjkoshy		    PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
1541157144Sjkoshy			pmclog_process_map_in(pm->pm_owner,
1542157144Sjkoshy			    pid, pkm->pm_address, fullpath);
1543157144Sjkoshy
1544157144Sjkoshy  done:
1545157144Sjkoshy	if (freepath)
1546184205Sdes		free(freepath, M_TEMP);
1547157144Sjkoshy}
1548157144Sjkoshy
1549157144Sjkoshy
1550157144Sjkoshy/*
1551157144Sjkoshy * Log an munmap request.
1552157144Sjkoshy */
1553157144Sjkoshy
1554157144Sjkoshystatic void
1555157144Sjkoshypmc_process_munmap(struct thread *td, struct pmckern_map_out *pkm)
1556157144Sjkoshy{
1557157144Sjkoshy	int ri;
1558157144Sjkoshy	pid_t pid;
1559157144Sjkoshy	struct pmc_owner *po;
1560157144Sjkoshy	const struct pmc *pm;
1561157144Sjkoshy	const struct pmc_process *pp;
1562157144Sjkoshy
1563157144Sjkoshy	pid = td->td_proc->p_pid;
1564157144Sjkoshy
1565157144Sjkoshy	LIST_FOREACH(po, &pmc_ss_owners, po_ssnext)
1566157144Sjkoshy	    if (po->po_flags & PMC_PO_OWNS_LOGFILE)
1567157144Sjkoshy		pmclog_process_map_out(po, pid, pkm->pm_address,
1568157144Sjkoshy		    pkm->pm_address + pkm->pm_size);
1569157144Sjkoshy
1570157144Sjkoshy	if ((pp = pmc_find_process_descriptor(td->td_proc, 0)) == NULL)
1571157144Sjkoshy		return;
1572157144Sjkoshy
1573157144Sjkoshy	for (ri = 0; ri < md->pmd_npmc; ri++)
1574157144Sjkoshy		if ((pm = pp->pp_pmcs[ri].pp_pmc) != NULL &&
1575157144Sjkoshy		    PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
1576157651Sjkoshy			pmclog_process_map_out(pm->pm_owner, pid,
1577157651Sjkoshy			    pkm->pm_address, pkm->pm_address + pkm->pm_size);
1578157144Sjkoshy}
1579157144Sjkoshy
1580157144Sjkoshy/*
1581174395Sjkoshy * Log mapping information about the kernel.
1582174395Sjkoshy */
1583174395Sjkoshy
1584174395Sjkoshystatic void
1585174395Sjkoshypmc_log_kernel_mappings(struct pmc *pm)
1586174395Sjkoshy{
1587174395Sjkoshy	struct pmc_owner *po;
1588174395Sjkoshy	struct pmckern_map_in *km, *kmbase;
1589174395Sjkoshy
1590174395Sjkoshy	sx_assert(&pmc_sx, SX_LOCKED);
1591174395Sjkoshy	KASSERT(PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)),
1592174395Sjkoshy	    ("[pmc,%d] non-sampling PMC (%p) desires mapping information",
1593174395Sjkoshy		__LINE__, (void *) pm));
1594174395Sjkoshy
1595174395Sjkoshy	po = pm->pm_owner;
1596174395Sjkoshy
1597174395Sjkoshy	if (po->po_flags & PMC_PO_INITIAL_MAPPINGS_DONE)
1598174395Sjkoshy		return;
1599174395Sjkoshy
1600174395Sjkoshy	/*
1601174395Sjkoshy	 * Log the current set of kernel modules.
1602174395Sjkoshy	 */
1603174395Sjkoshy	kmbase = linker_hwpmc_list_objects();
1604174395Sjkoshy	for (km = kmbase; km->pm_file != NULL; km++) {
1605174395Sjkoshy		PMCDBG(LOG,REG,1,"%s %p", (char *) km->pm_file,
1606174395Sjkoshy		    (void *) km->pm_address);
1607174395Sjkoshy		pmclog_process_map_in(po, (pid_t) -1, km->pm_address,
1608174395Sjkoshy		    km->pm_file);
1609174395Sjkoshy	}
1610184205Sdes	free(kmbase, M_LINKER);
1611174395Sjkoshy
1612174395Sjkoshy	po->po_flags |= PMC_PO_INITIAL_MAPPINGS_DONE;
1613174395Sjkoshy}
1614174395Sjkoshy
1615174395Sjkoshy/*
1616174395Sjkoshy * Log the mappings for a single process.
1617174395Sjkoshy */
1618174395Sjkoshy
1619174395Sjkoshystatic void
1620174395Sjkoshypmc_log_process_mappings(struct pmc_owner *po, struct proc *p)
1621174395Sjkoshy{
1622174395Sjkoshy}
1623174395Sjkoshy
1624174395Sjkoshy/*
1625174395Sjkoshy * Log mappings for all processes in the system.
1626174395Sjkoshy */
1627174395Sjkoshy
1628174395Sjkoshystatic void
1629174395Sjkoshypmc_log_all_process_mappings(struct pmc_owner *po)
1630174395Sjkoshy{
1631174395Sjkoshy	struct proc *p, *top;
1632174395Sjkoshy
1633174395Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
1634174395Sjkoshy
1635174395Sjkoshy	if ((p = pfind(1)) == NULL)
1636174395Sjkoshy		panic("[pmc,%d] Cannot find init", __LINE__);
1637174395Sjkoshy
1638174395Sjkoshy	PROC_UNLOCK(p);
1639174395Sjkoshy
1640174395Sjkoshy	sx_slock(&proctree_lock);
1641174395Sjkoshy
1642174395Sjkoshy	top = p;
1643174395Sjkoshy
1644174395Sjkoshy	for (;;) {
1645174395Sjkoshy		pmc_log_process_mappings(po, p);
1646174395Sjkoshy		if (!LIST_EMPTY(&p->p_children))
1647174395Sjkoshy			p = LIST_FIRST(&p->p_children);
1648174395Sjkoshy		else for (;;) {
1649174395Sjkoshy			if (p == top)
1650174395Sjkoshy				goto done;
1651174395Sjkoshy			if (LIST_NEXT(p, p_sibling)) {
1652174395Sjkoshy				p = LIST_NEXT(p, p_sibling);
1653174395Sjkoshy				break;
1654174395Sjkoshy			}
1655174395Sjkoshy			p = p->p_pptr;
1656174395Sjkoshy		}
1657174395Sjkoshy	}
1658174395Sjkoshy done:
1659174395Sjkoshy	sx_sunlock(&proctree_lock);
1660174395Sjkoshy}
1661174395Sjkoshy
1662174395Sjkoshy/*
1663147191Sjkoshy * The 'hook' invoked from the kernel proper
1664147191Sjkoshy */
1665147191Sjkoshy
1666147191Sjkoshy
1667153110Sru#ifdef	DEBUG
1668147191Sjkoshyconst char *pmc_hooknames[] = {
1669157144Sjkoshy	/* these strings correspond to PMC_FN_* in <sys/pmckern.h> */
1670147191Sjkoshy	"",
1671147191Sjkoshy	"EXEC",
1672147191Sjkoshy	"CSW-IN",
1673147191Sjkoshy	"CSW-OUT",
1674157144Sjkoshy	"SAMPLE",
1675157144Sjkoshy	"KLDLOAD",
1676157144Sjkoshy	"KLDUNLOAD",
1677157144Sjkoshy	"MMAP",
1678174395Sjkoshy	"MUNMAP",
1679174395Sjkoshy	"CALLCHAIN"
1680147191Sjkoshy};
1681147191Sjkoshy#endif
1682147191Sjkoshy
1683147191Sjkoshystatic int
1684147191Sjkoshypmc_hook_handler(struct thread *td, int function, void *arg)
1685147191Sjkoshy{
1686147191Sjkoshy
1687147191Sjkoshy	PMCDBG(MOD,PMH,1, "hook td=%p func=%d \"%s\" arg=%p", td, function,
1688147191Sjkoshy	    pmc_hooknames[function], arg);
1689147191Sjkoshy
1690147191Sjkoshy	switch (function)
1691147191Sjkoshy	{
1692147191Sjkoshy
1693147191Sjkoshy	/*
1694145256Sjkoshy	 * Process exec()
1695145256Sjkoshy	 */
1696145256Sjkoshy
1697145256Sjkoshy	case PMC_FN_PROCESS_EXEC:
1698145256Sjkoshy	{
1699147191Sjkoshy		char *fullpath, *freepath;
1700145256Sjkoshy		unsigned int ri;
1701147191Sjkoshy		int is_using_hwpmcs;
1702145256Sjkoshy		struct pmc *pm;
1703145256Sjkoshy		struct proc *p;
1704145256Sjkoshy		struct pmc_owner *po;
1705145256Sjkoshy		struct pmc_process *pp;
1706147708Sjkoshy		struct pmckern_procexec *pk;
1707145256Sjkoshy
1708145256Sjkoshy		sx_assert(&pmc_sx, SX_XLOCKED);
1709145256Sjkoshy
1710147191Sjkoshy		p = td->td_proc;
1711147708Sjkoshy		pmc_getfilename(p->p_textvp, &fullpath, &freepath);
1712147191Sjkoshy
1713147708Sjkoshy		pk = (struct pmckern_procexec *) arg;
1714147708Sjkoshy
1715147191Sjkoshy		/* Inform owners of SS mode PMCs of the exec event. */
1716147191Sjkoshy		LIST_FOREACH(po, &pmc_ss_owners, po_ssnext)
1717147191Sjkoshy		    if (po->po_flags & PMC_PO_OWNS_LOGFILE)
1718147708Sjkoshy			    pmclog_process_procexec(po, PMC_ID_INVALID,
1719147708Sjkoshy				p->p_pid, pk->pm_entryaddr, fullpath);
1720147191Sjkoshy
1721147191Sjkoshy		PROC_LOCK(p);
1722147191Sjkoshy		is_using_hwpmcs = p->p_flag & P_HWPMC;
1723147191Sjkoshy		PROC_UNLOCK(p);
1724147191Sjkoshy
1725147191Sjkoshy		if (!is_using_hwpmcs) {
1726147191Sjkoshy			if (freepath)
1727184205Sdes				free(freepath, M_TEMP);
1728147191Sjkoshy			break;
1729147191Sjkoshy		}
1730147191Sjkoshy
1731145256Sjkoshy		/*
1732145256Sjkoshy		 * PMCs are not inherited across an exec():  remove any
1733145256Sjkoshy		 * PMCs that this process is the owner of.
1734145256Sjkoshy		 */
1735145256Sjkoshy
1736145256Sjkoshy		if ((po = pmc_find_owner_descriptor(p)) != NULL) {
1737145256Sjkoshy			pmc_remove_owner(po);
1738147191Sjkoshy			pmc_destroy_owner_descriptor(po);
1739145256Sjkoshy		}
1740145256Sjkoshy
1741145256Sjkoshy		/*
1742154483Sjkoshy		 * If the process being exec'ed is not the target of any
1743154483Sjkoshy		 * PMC, we are done.
1744145256Sjkoshy		 */
1745154483Sjkoshy		if ((pp = pmc_find_process_descriptor(p, 0)) == NULL) {
1746154483Sjkoshy			if (freepath)
1747184205Sdes				free(freepath, M_TEMP);
1748145256Sjkoshy			break;
1749154483Sjkoshy		}
1750145256Sjkoshy
1751147191Sjkoshy		/*
1752147191Sjkoshy		 * Log the exec event to all monitoring owners.  Skip
1753147191Sjkoshy		 * owners who have already recieved the event because
1754154483Sjkoshy		 * they had system sampling PMCs active.
1755147191Sjkoshy		 */
1756147191Sjkoshy		for (ri = 0; ri < md->pmd_npmc; ri++)
1757147191Sjkoshy			if ((pm = pp->pp_pmcs[ri].pp_pmc) != NULL) {
1758147191Sjkoshy				po = pm->pm_owner;
1759147191Sjkoshy				if (po->po_sscount == 0 &&
1760147191Sjkoshy				    po->po_flags & PMC_PO_OWNS_LOGFILE)
1761147708Sjkoshy					pmclog_process_procexec(po, pm->pm_id,
1762147708Sjkoshy					    p->p_pid, pk->pm_entryaddr,
1763147191Sjkoshy					    fullpath);
1764147191Sjkoshy			}
1765147191Sjkoshy
1766147191Sjkoshy		if (freepath)
1767184205Sdes			free(freepath, M_TEMP);
1768147191Sjkoshy
1769145256Sjkoshy
1770145256Sjkoshy		PMCDBG(PRC,EXC,1, "exec proc=%p (%d, %s) cred-changed=%d",
1771147708Sjkoshy		    p, p->p_pid, p->p_comm, pk->pm_credentialschanged);
1772145256Sjkoshy
1773147708Sjkoshy		if (pk->pm_credentialschanged == 0) /* no change */
1774145256Sjkoshy			break;
1775145256Sjkoshy
1776145256Sjkoshy		/*
1777145256Sjkoshy		 * If the newly exec()'ed process has a different credential
1778145256Sjkoshy		 * than before, allow it to be the target of a PMC only if
1779145256Sjkoshy		 * the PMC's owner has sufficient priviledge.
1780145256Sjkoshy		 */
1781145256Sjkoshy
1782145256Sjkoshy		for (ri = 0; ri < md->pmd_npmc; ri++)
1783145256Sjkoshy			if ((pm = pp->pp_pmcs[ri].pp_pmc) != NULL)
1784145256Sjkoshy				if (pmc_can_attach(pm, td->td_proc) != 0)
1785145256Sjkoshy					pmc_detach_one_process(td->td_proc,
1786145256Sjkoshy					    pm, PMC_FLAG_NONE);
1787145256Sjkoshy
1788145256Sjkoshy		KASSERT(pp->pp_refcnt >= 0 && pp->pp_refcnt < (int) md->pmd_npmc,
1789145256Sjkoshy		    ("[pmc,%d] Illegal ref count %d on pp %p", __LINE__,
1790145256Sjkoshy			pp->pp_refcnt, pp));
1791145256Sjkoshy
1792145256Sjkoshy		/*
1793145256Sjkoshy		 * If this process is no longer the target of any
1794145256Sjkoshy		 * PMCs, we can remove the process entry and free
1795145256Sjkoshy		 * up space.
1796145256Sjkoshy		 */
1797145256Sjkoshy
1798145256Sjkoshy		if (pp->pp_refcnt == 0) {
1799145256Sjkoshy			pmc_remove_process_descriptor(pp);
1800184205Sdes			free(pp, M_PMC);
1801147191Sjkoshy			break;
1802145256Sjkoshy		}
1803145256Sjkoshy
1804145256Sjkoshy	}
1805145256Sjkoshy	break;
1806145256Sjkoshy
1807145256Sjkoshy	case PMC_FN_CSW_IN:
1808147191Sjkoshy		pmc_process_csw_in(td);
1809147191Sjkoshy		break;
1810145256Sjkoshy
1811147191Sjkoshy	case PMC_FN_CSW_OUT:
1812147191Sjkoshy		pmc_process_csw_out(td);
1813147191Sjkoshy		break;
1814145256Sjkoshy
1815145256Sjkoshy	/*
1816147191Sjkoshy	 * Process accumulated PC samples.
1817147191Sjkoshy	 *
1818147191Sjkoshy	 * This function is expected to be called by hardclock() for
1819147191Sjkoshy	 * each CPU that has accumulated PC samples.
1820147191Sjkoshy	 *
1821147191Sjkoshy	 * This function is to be executed on the CPU whose samples
1822147191Sjkoshy	 * are being processed.
1823145256Sjkoshy	 */
1824147191Sjkoshy	case PMC_FN_DO_SAMPLES:
1825145256Sjkoshy
1826145256Sjkoshy		/*
1827147191Sjkoshy		 * Clear the cpu specific bit in the CPU mask before
1828147191Sjkoshy		 * do the rest of the processing.  If the NMI handler
1829147191Sjkoshy		 * gets invoked after the "atomic_clear_int()" call
1830147191Sjkoshy		 * below but before "pmc_process_samples()" gets
1831147191Sjkoshy		 * around to processing the interrupt, then we will
1832147191Sjkoshy		 * come back here at the next hardclock() tick (and
1833147191Sjkoshy		 * may find nothing to do if "pmc_process_samples()"
1834147191Sjkoshy		 * had already processed the interrupt).  We don't
1835147191Sjkoshy		 * lose the interrupt sample.
1836145256Sjkoshy		 */
1837147191Sjkoshy		atomic_clear_int(&pmc_cpumask, (1 << PCPU_GET(cpuid)));
1838147191Sjkoshy		pmc_process_samples(PCPU_GET(cpuid));
1839147191Sjkoshy		break;
1840145256Sjkoshy
1841157144Sjkoshy
1842157144Sjkoshy	case PMC_FN_KLD_LOAD:
1843157144Sjkoshy		sx_assert(&pmc_sx, SX_LOCKED);
1844157144Sjkoshy		pmc_process_kld_load((struct pmckern_map_in *) arg);
1845157144Sjkoshy		break;
1846157144Sjkoshy
1847157144Sjkoshy	case PMC_FN_KLD_UNLOAD:
1848157144Sjkoshy		sx_assert(&pmc_sx, SX_LOCKED);
1849157144Sjkoshy		pmc_process_kld_unload((struct pmckern_map_out *) arg);
1850157144Sjkoshy		break;
1851157144Sjkoshy
1852157144Sjkoshy	case PMC_FN_MMAP:
1853157144Sjkoshy		sx_assert(&pmc_sx, SX_LOCKED);
1854157144Sjkoshy		pmc_process_mmap(td, (struct pmckern_map_in *) arg);
1855157144Sjkoshy		break;
1856157144Sjkoshy
1857157144Sjkoshy	case PMC_FN_MUNMAP:
1858157144Sjkoshy		sx_assert(&pmc_sx, SX_LOCKED);
1859157144Sjkoshy		pmc_process_munmap(td, (struct pmckern_map_out *) arg);
1860157144Sjkoshy		break;
1861157144Sjkoshy
1862174395Sjkoshy	case PMC_FN_USER_CALLCHAIN:
1863174395Sjkoshy		/*
1864174395Sjkoshy		 * Record a call chain.
1865174395Sjkoshy		 */
1866174395Sjkoshy		pmc_capture_user_callchain(PCPU_GET(cpuid),
1867174395Sjkoshy		    (struct trapframe *) arg);
1868174395Sjkoshy		break;
1869174395Sjkoshy
1870145256Sjkoshy	default:
1871153110Sru#ifdef	DEBUG
1872145256Sjkoshy		KASSERT(0, ("[pmc,%d] unknown hook %d\n", __LINE__, function));
1873145256Sjkoshy#endif
1874145256Sjkoshy		break;
1875145256Sjkoshy
1876145256Sjkoshy	}
1877145256Sjkoshy
1878145256Sjkoshy	return 0;
1879145256Sjkoshy}
1880145256Sjkoshy
1881145256Sjkoshy/*
1882145256Sjkoshy * allocate a 'struct pmc_owner' descriptor in the owner hash table.
1883145256Sjkoshy */
1884145256Sjkoshy
1885145256Sjkoshystatic struct pmc_owner *
1886145256Sjkoshypmc_allocate_owner_descriptor(struct proc *p)
1887145256Sjkoshy{
1888145256Sjkoshy	uint32_t hindex;
1889145256Sjkoshy	struct pmc_owner *po;
1890145256Sjkoshy	struct pmc_ownerhash *poh;
1891145256Sjkoshy
1892145256Sjkoshy	hindex = PMC_HASH_PTR(p, pmc_ownerhashmask);
1893145256Sjkoshy	poh = &pmc_ownerhash[hindex];
1894145256Sjkoshy
1895145256Sjkoshy	/* allocate space for N pointers and one descriptor struct */
1896184802Sjkoshy	po = malloc(sizeof(struct pmc_owner), M_PMC, M_WAITOK|M_ZERO);
1897147191Sjkoshy	po->po_sscount = po->po_error = po->po_flags = 0;
1898147191Sjkoshy	po->po_file  = NULL;
1899145256Sjkoshy	po->po_owner = p;
1900147191Sjkoshy	po->po_kthread = NULL;
1901145256Sjkoshy	LIST_INIT(&po->po_pmcs);
1902145256Sjkoshy	LIST_INSERT_HEAD(poh, po, po_next); /* insert into hash table */
1903145256Sjkoshy
1904147191Sjkoshy	TAILQ_INIT(&po->po_logbuffers);
1905168856Sjkoshy	mtx_init(&po->po_mtx, "pmc-owner-mtx", "pmc-per-proc", MTX_SPIN);
1906147191Sjkoshy
1907145256Sjkoshy	PMCDBG(OWN,ALL,1, "allocate-owner proc=%p (%d, %s) pmc-owner=%p",
1908145256Sjkoshy	    p, p->p_pid, p->p_comm, po);
1909145256Sjkoshy
1910145256Sjkoshy	return po;
1911145256Sjkoshy}
1912145256Sjkoshy
1913147191Sjkoshystatic void
1914147191Sjkoshypmc_destroy_owner_descriptor(struct pmc_owner *po)
1915147191Sjkoshy{
1916147191Sjkoshy
1917147191Sjkoshy	PMCDBG(OWN,REL,1, "destroy-owner po=%p proc=%p (%d, %s)",
1918147191Sjkoshy	    po, po->po_owner, po->po_owner->p_pid, po->po_owner->p_comm);
1919147191Sjkoshy
1920147191Sjkoshy	mtx_destroy(&po->po_mtx);
1921184205Sdes	free(po, M_PMC);
1922147191Sjkoshy}
1923147191Sjkoshy
1924145256Sjkoshy/*
1925145256Sjkoshy * find the descriptor corresponding to process 'p', adding or removing it
1926145256Sjkoshy * as specified by 'mode'.
1927145256Sjkoshy */
1928145256Sjkoshy
1929145256Sjkoshystatic struct pmc_process *
1930145256Sjkoshypmc_find_process_descriptor(struct proc *p, uint32_t mode)
1931145256Sjkoshy{
1932145256Sjkoshy	uint32_t hindex;
1933145256Sjkoshy	struct pmc_process *pp, *ppnew;
1934145256Sjkoshy	struct pmc_processhash *pph;
1935145256Sjkoshy
1936145256Sjkoshy	hindex = PMC_HASH_PTR(p, pmc_processhashmask);
1937145256Sjkoshy	pph = &pmc_processhash[hindex];
1938145256Sjkoshy
1939145256Sjkoshy	ppnew = NULL;
1940145256Sjkoshy
1941145256Sjkoshy	/*
1942145256Sjkoshy	 * Pre-allocate memory in the FIND_ALLOCATE case since we
1943145256Sjkoshy	 * cannot call malloc(9) once we hold a spin lock.
1944145256Sjkoshy	 */
1945184802Sjkoshy	if (mode & PMC_FLAG_ALLOCATE)
1946184214Sdes		ppnew = malloc(sizeof(struct pmc_process) + md->pmd_npmc *
1947184802Sjkoshy		    sizeof(struct pmc_targetstate), M_PMC, M_WAITOK|M_ZERO);
1948145256Sjkoshy
1949145256Sjkoshy	mtx_lock_spin(&pmc_processhash_mtx);
1950145256Sjkoshy	LIST_FOREACH(pp, pph, pp_next)
1951145256Sjkoshy	    if (pp->pp_proc == p)
1952145256Sjkoshy		    break;
1953145256Sjkoshy
1954145256Sjkoshy	if ((mode & PMC_FLAG_REMOVE) && pp != NULL)
1955145256Sjkoshy		LIST_REMOVE(pp, pp_next);
1956145256Sjkoshy
1957145256Sjkoshy	if ((mode & PMC_FLAG_ALLOCATE) && pp == NULL &&
1958145256Sjkoshy	    ppnew != NULL) {
1959145256Sjkoshy		ppnew->pp_proc = p;
1960145256Sjkoshy		LIST_INSERT_HEAD(pph, ppnew, pp_next);
1961145256Sjkoshy		pp = ppnew;
1962145256Sjkoshy		ppnew = NULL;
1963145256Sjkoshy	}
1964145256Sjkoshy	mtx_unlock_spin(&pmc_processhash_mtx);
1965145256Sjkoshy
1966145256Sjkoshy	if (pp != NULL && ppnew != NULL)
1967184205Sdes		free(ppnew, M_PMC);
1968145256Sjkoshy
1969145256Sjkoshy	return pp;
1970145256Sjkoshy}
1971145256Sjkoshy
1972145256Sjkoshy/*
1973145256Sjkoshy * remove a process descriptor from the process hash table.
1974145256Sjkoshy */
1975145256Sjkoshy
1976145256Sjkoshystatic void
1977145256Sjkoshypmc_remove_process_descriptor(struct pmc_process *pp)
1978145256Sjkoshy{
1979145256Sjkoshy	KASSERT(pp->pp_refcnt == 0,
1980145256Sjkoshy	    ("[pmc,%d] Removing process descriptor %p with count %d",
1981145256Sjkoshy		__LINE__, pp, pp->pp_refcnt));
1982145256Sjkoshy
1983145256Sjkoshy	mtx_lock_spin(&pmc_processhash_mtx);
1984145256Sjkoshy	LIST_REMOVE(pp, pp_next);
1985145256Sjkoshy	mtx_unlock_spin(&pmc_processhash_mtx);
1986145256Sjkoshy}
1987145256Sjkoshy
1988145256Sjkoshy
1989145256Sjkoshy/*
1990145256Sjkoshy * find an owner descriptor corresponding to proc 'p'
1991145256Sjkoshy */
1992145256Sjkoshy
1993145256Sjkoshystatic struct pmc_owner *
1994145256Sjkoshypmc_find_owner_descriptor(struct proc *p)
1995145256Sjkoshy{
1996145256Sjkoshy	uint32_t hindex;
1997145256Sjkoshy	struct pmc_owner *po;
1998145256Sjkoshy	struct pmc_ownerhash *poh;
1999145256Sjkoshy
2000145256Sjkoshy	hindex = PMC_HASH_PTR(p, pmc_ownerhashmask);
2001145256Sjkoshy	poh = &pmc_ownerhash[hindex];
2002145256Sjkoshy
2003145256Sjkoshy	po = NULL;
2004145256Sjkoshy	LIST_FOREACH(po, poh, po_next)
2005145256Sjkoshy	    if (po->po_owner == p)
2006145256Sjkoshy		    break;
2007145256Sjkoshy
2008145256Sjkoshy	PMCDBG(OWN,FND,1, "find-owner proc=%p (%d, %s) hindex=0x%x -> "
2009145256Sjkoshy	    "pmc-owner=%p", p, p->p_pid, p->p_comm, hindex, po);
2010145256Sjkoshy
2011145256Sjkoshy	return po;
2012145256Sjkoshy}
2013145256Sjkoshy
2014145256Sjkoshy/*
2015145256Sjkoshy * pmc_allocate_pmc_descriptor
2016145256Sjkoshy *
2017145256Sjkoshy * Allocate a pmc descriptor and initialize its
2018145256Sjkoshy * fields.
2019145256Sjkoshy */
2020145256Sjkoshy
2021145256Sjkoshystatic struct pmc *
2022145256Sjkoshypmc_allocate_pmc_descriptor(void)
2023145256Sjkoshy{
2024145256Sjkoshy	struct pmc *pmc;
2025145256Sjkoshy
2026184802Sjkoshy	pmc = malloc(sizeof(struct pmc), M_PMC, M_WAITOK|M_ZERO);
2027145256Sjkoshy
2028145256Sjkoshy	if (pmc != NULL) {
2029145256Sjkoshy		pmc->pm_owner = NULL;
2030145256Sjkoshy		LIST_INIT(&pmc->pm_targets);
2031145256Sjkoshy	}
2032145256Sjkoshy
2033145256Sjkoshy	PMCDBG(PMC,ALL,1, "allocate-pmc -> pmc=%p", pmc);
2034145256Sjkoshy
2035145256Sjkoshy	return pmc;
2036145256Sjkoshy}
2037145256Sjkoshy
2038145256Sjkoshy/*
2039145256Sjkoshy * Destroy a pmc descriptor.
2040145256Sjkoshy */
2041145256Sjkoshy
2042145256Sjkoshystatic void
2043145256Sjkoshypmc_destroy_pmc_descriptor(struct pmc *pm)
2044145256Sjkoshy{
2045145256Sjkoshy	(void) pm;
2046145256Sjkoshy
2047153110Sru#ifdef	DEBUG
2048145256Sjkoshy	KASSERT(pm->pm_state == PMC_STATE_DELETED ||
2049145256Sjkoshy	    pm->pm_state == PMC_STATE_FREE,
2050145256Sjkoshy	    ("[pmc,%d] destroying non-deleted PMC", __LINE__));
2051145256Sjkoshy	KASSERT(LIST_EMPTY(&pm->pm_targets),
2052145256Sjkoshy	    ("[pmc,%d] destroying pmc with targets", __LINE__));
2053145256Sjkoshy	KASSERT(pm->pm_owner == NULL,
2054145256Sjkoshy	    ("[pmc,%d] destroying pmc attached to an owner", __LINE__));
2055145256Sjkoshy	KASSERT(pm->pm_runcount == 0,
2056145256Sjkoshy	    ("[pmc,%d] pmc has non-zero run count %d", __LINE__,
2057145256Sjkoshy		pm->pm_runcount));
2058145256Sjkoshy#endif
2059145256Sjkoshy}
2060145256Sjkoshy
2061147191Sjkoshystatic void
2062147191Sjkoshypmc_wait_for_pmc_idle(struct pmc *pm)
2063147191Sjkoshy{
2064153110Sru#ifdef	DEBUG
2065147191Sjkoshy	volatile int maxloop;
2066147191Sjkoshy
2067183266Sjkoshy	maxloop = 100 * pmc_cpu_max();
2068147191Sjkoshy#endif
2069147191Sjkoshy
2070147191Sjkoshy	/*
2071147191Sjkoshy	 * Loop (with a forced context switch) till the PMC's runcount
2072147191Sjkoshy	 * comes down to zero.
2073147191Sjkoshy	 */
2074147191Sjkoshy	while (atomic_load_acq_32(&pm->pm_runcount) > 0) {
2075153110Sru#ifdef	DEBUG
2076147191Sjkoshy		maxloop--;
2077147191Sjkoshy		KASSERT(maxloop > 0,
2078147191Sjkoshy		    ("[pmc,%d] (ri%d, rc%d) waiting too long for "
2079147191Sjkoshy			"pmc to be free", __LINE__,
2080147191Sjkoshy			PMC_TO_ROWINDEX(pm), pm->pm_runcount));
2081147191Sjkoshy#endif
2082147191Sjkoshy		pmc_force_context_switch();
2083147191Sjkoshy	}
2084147191Sjkoshy}
2085147191Sjkoshy
2086145256Sjkoshy/*
2087145256Sjkoshy * This function does the following things:
2088145256Sjkoshy *
2089145256Sjkoshy *  - detaches the PMC from hardware
2090145256Sjkoshy *  - unlinks all target threads that were attached to it
2091145256Sjkoshy *  - removes the PMC from its owner's list
2092145256Sjkoshy *  - destroy's the PMC private mutex
2093145256Sjkoshy *
2094145256Sjkoshy * Once this function completes, the given pmc pointer can be safely
2095145256Sjkoshy * FREE'd by the caller.
2096145256Sjkoshy */
2097145256Sjkoshy
2098145256Sjkoshystatic void
2099145256Sjkoshypmc_release_pmc_descriptor(struct pmc *pm)
2100145256Sjkoshy{
2101145774Sjkoshy	enum pmc_mode mode;
2102145256Sjkoshy	struct pmc_hw *phw;
2103184802Sjkoshy	u_int adjri, ri, cpu;
2104147191Sjkoshy	struct pmc_owner *po;
2105184802Sjkoshy	struct pmc_binding pb;
2106145256Sjkoshy	struct pmc_process *pp;
2107184802Sjkoshy	struct pmc_classdep *pcd;
2108145256Sjkoshy	struct pmc_target *ptgt, *tmp;
2109145256Sjkoshy
2110145256Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
2111145256Sjkoshy
2112145256Sjkoshy	KASSERT(pm, ("[pmc,%d] null pmc", __LINE__));
2113145256Sjkoshy
2114145774Sjkoshy	ri   = PMC_TO_ROWINDEX(pm);
2115184802Sjkoshy	pcd  = pmc_ri_to_classdep(md, ri, &adjri);
2116145774Sjkoshy	mode = PMC_TO_MODE(pm);
2117145256Sjkoshy
2118145256Sjkoshy	PMCDBG(PMC,REL,1, "release-pmc pmc=%p ri=%d mode=%d", pm, ri,
2119145774Sjkoshy	    mode);
2120145256Sjkoshy
2121145256Sjkoshy	/*
2122145256Sjkoshy	 * First, we take the PMC off hardware.
2123145256Sjkoshy	 */
2124145301Simp	cpu = 0;
2125145774Sjkoshy	if (PMC_IS_SYSTEM_MODE(mode)) {
2126145256Sjkoshy
2127145256Sjkoshy		/*
2128145256Sjkoshy		 * A system mode PMC runs on a specific CPU.  Switch
2129145256Sjkoshy		 * to this CPU and turn hardware off.
2130145256Sjkoshy		 */
2131145256Sjkoshy		pmc_save_cpu_binding(&pb);
2132145256Sjkoshy
2133145774Sjkoshy		cpu = PMC_TO_CPU(pm);
2134145256Sjkoshy
2135147191Sjkoshy		pmc_select_cpu(cpu);
2136145256Sjkoshy
2137147191Sjkoshy		/* switch off non-stalled CPUs */
2138147191Sjkoshy		if (pm->pm_state == PMC_STATE_RUNNING &&
2139147867Sjkoshy		    pm->pm_stalled == 0) {
2140145256Sjkoshy
2141145256Sjkoshy			phw = pmc_pcpu[cpu]->pc_hwpmcs[ri];
2142145256Sjkoshy
2143145256Sjkoshy			KASSERT(phw->phw_pmc == pm,
2144145256Sjkoshy			    ("[pmc, %d] pmc ptr ri(%d) hw(%p) pm(%p)",
2145145256Sjkoshy				__LINE__, ri, phw->phw_pmc, pm));
2146145256Sjkoshy			PMCDBG(PMC,REL,2, "stopping cpu=%d ri=%d", cpu, ri);
2147145256Sjkoshy
2148145256Sjkoshy			critical_enter();
2149184802Sjkoshy			pcd->pcd_stop_pmc(cpu, adjri);
2150145256Sjkoshy			critical_exit();
2151145256Sjkoshy		}
2152145256Sjkoshy
2153145256Sjkoshy		PMCDBG(PMC,REL,2, "decfg cpu=%d ri=%d", cpu, ri);
2154145256Sjkoshy
2155145256Sjkoshy		critical_enter();
2156184802Sjkoshy		pcd->pcd_config_pmc(cpu, adjri, NULL);
2157145256Sjkoshy		critical_exit();
2158145256Sjkoshy
2159147191Sjkoshy		/* adjust the global and process count of SS mode PMCs */
2160147191Sjkoshy		if (mode == PMC_MODE_SS && pm->pm_state == PMC_STATE_RUNNING) {
2161147191Sjkoshy			po = pm->pm_owner;
2162147191Sjkoshy			po->po_sscount--;
2163147191Sjkoshy			if (po->po_sscount == 0) {
2164147191Sjkoshy				atomic_subtract_rel_int(&pmc_ss_count, 1);
2165147191Sjkoshy				LIST_REMOVE(po, po_ssnext);
2166147191Sjkoshy			}
2167147191Sjkoshy		}
2168147191Sjkoshy
2169145256Sjkoshy		pm->pm_state = PMC_STATE_DELETED;
2170145256Sjkoshy
2171145256Sjkoshy		pmc_restore_cpu_binding(&pb);
2172145256Sjkoshy
2173147191Sjkoshy		/*
2174147191Sjkoshy		 * We could have references to this PMC structure in
2175147191Sjkoshy		 * the per-cpu sample queues.  Wait for the queue to
2176147191Sjkoshy		 * drain.
2177147191Sjkoshy		 */
2178147191Sjkoshy		pmc_wait_for_pmc_idle(pm);
2179147191Sjkoshy
2180145774Sjkoshy	} else if (PMC_IS_VIRTUAL_MODE(mode)) {
2181145256Sjkoshy
2182145256Sjkoshy		/*
2183145256Sjkoshy		 * A virtual PMC could be running on multiple CPUs at
2184145256Sjkoshy		 * a given instant.
2185145256Sjkoshy		 *
2186145256Sjkoshy		 * By marking its state as DELETED, we ensure that
2187145256Sjkoshy		 * this PMC is never further scheduled on hardware.
2188145256Sjkoshy		 *
2189145256Sjkoshy		 * Then we wait till all CPUs are done with this PMC.
2190145256Sjkoshy		 */
2191145256Sjkoshy		pm->pm_state = PMC_STATE_DELETED;
2192145256Sjkoshy
2193145256Sjkoshy
2194147191Sjkoshy		/* Wait for the PMCs runcount to come to zero. */
2195147191Sjkoshy		pmc_wait_for_pmc_idle(pm);
2196145256Sjkoshy
2197145256Sjkoshy		/*
2198145256Sjkoshy		 * At this point the PMC is off all CPUs and cannot be
2199145256Sjkoshy		 * freshly scheduled onto a CPU.  It is now safe to
2200145256Sjkoshy		 * unlink all targets from this PMC.  If a
2201145256Sjkoshy		 * process-record's refcount falls to zero, we remove
2202145256Sjkoshy		 * it from the hash table.  The module-wide SX lock
2203145256Sjkoshy		 * protects us from races.
2204145256Sjkoshy		 */
2205145256Sjkoshy		LIST_FOREACH_SAFE(ptgt, &pm->pm_targets, pt_next, tmp) {
2206145256Sjkoshy			pp = ptgt->pt_process;
2207145256Sjkoshy			pmc_unlink_target_process(pm, pp); /* frees 'ptgt' */
2208145256Sjkoshy
2209145256Sjkoshy			PMCDBG(PMC,REL,3, "pp->refcnt=%d", pp->pp_refcnt);
2210145256Sjkoshy
2211145256Sjkoshy			/*
2212145256Sjkoshy			 * If the target process record shows that no
2213145256Sjkoshy			 * PMCs are attached to it, reclaim its space.
2214145256Sjkoshy			 */
2215145256Sjkoshy
2216145256Sjkoshy			if (pp->pp_refcnt == 0) {
2217145256Sjkoshy				pmc_remove_process_descriptor(pp);
2218184205Sdes				free(pp, M_PMC);
2219145256Sjkoshy			}
2220145256Sjkoshy		}
2221145256Sjkoshy
2222145256Sjkoshy		cpu = curthread->td_oncpu; /* setup cpu for pmd_release() */
2223145256Sjkoshy
2224145256Sjkoshy	}
2225145256Sjkoshy
2226145256Sjkoshy	/*
2227145256Sjkoshy	 * Release any MD resources
2228145256Sjkoshy	 */
2229184802Sjkoshy	(void) pcd->pcd_release_pmc(cpu, adjri, pm);
2230145256Sjkoshy
2231145256Sjkoshy	/*
2232145256Sjkoshy	 * Update row disposition
2233145256Sjkoshy	 */
2234145256Sjkoshy
2235145774Sjkoshy	if (PMC_IS_SYSTEM_MODE(PMC_TO_MODE(pm)))
2236145256Sjkoshy		PMC_UNMARK_ROW_STANDALONE(ri);
2237145256Sjkoshy	else
2238145256Sjkoshy		PMC_UNMARK_ROW_THREAD(ri);
2239145256Sjkoshy
2240145256Sjkoshy	/* unlink from the owner's list */
2241147191Sjkoshy	if (pm->pm_owner) {
2242147191Sjkoshy		LIST_REMOVE(pm, pm_next);
2243147191Sjkoshy		pm->pm_owner = NULL;
2244147191Sjkoshy	}
2245145256Sjkoshy
2246145256Sjkoshy	pmc_destroy_pmc_descriptor(pm);
2247145256Sjkoshy}
2248145256Sjkoshy
2249145256Sjkoshy/*
2250145256Sjkoshy * Register an owner and a pmc.
2251145256Sjkoshy */
2252145256Sjkoshy
2253145256Sjkoshystatic int
2254145256Sjkoshypmc_register_owner(struct proc *p, struct pmc *pmc)
2255145256Sjkoshy{
2256145256Sjkoshy	struct pmc_owner *po;
2257145256Sjkoshy
2258145256Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
2259145256Sjkoshy
2260145774Sjkoshy	if ((po = pmc_find_owner_descriptor(p)) == NULL)
2261147191Sjkoshy		if ((po = pmc_allocate_owner_descriptor(p)) == NULL)
2262145256Sjkoshy			return ENOMEM;
2263145256Sjkoshy
2264145256Sjkoshy	KASSERT(pmc->pm_owner == NULL,
2265145256Sjkoshy	    ("[pmc,%d] attempting to own an initialized PMC", __LINE__));
2266145256Sjkoshy	pmc->pm_owner  = po;
2267145256Sjkoshy
2268147191Sjkoshy	LIST_INSERT_HEAD(&po->po_pmcs, pmc, pm_next);
2269145256Sjkoshy
2270145256Sjkoshy	PROC_LOCK(p);
2271145256Sjkoshy	p->p_flag |= P_HWPMC;
2272145256Sjkoshy	PROC_UNLOCK(p);
2273145256Sjkoshy
2274147191Sjkoshy	if (po->po_flags & PMC_PO_OWNS_LOGFILE)
2275147191Sjkoshy		pmclog_process_pmcallocate(pmc);
2276145256Sjkoshy
2277147191Sjkoshy	PMCDBG(PMC,REG,1, "register-owner pmc-owner=%p pmc=%p",
2278147191Sjkoshy	    po, pmc);
2279147191Sjkoshy
2280145256Sjkoshy	return 0;
2281145256Sjkoshy}
2282145256Sjkoshy
2283145256Sjkoshy/*
2284145256Sjkoshy * Return the current row disposition:
2285145256Sjkoshy * == 0 => FREE
2286145256Sjkoshy *  > 0 => PROCESS MODE
2287145256Sjkoshy *  < 0 => SYSTEM MODE
2288145256Sjkoshy */
2289145256Sjkoshy
2290145256Sjkoshyint
2291145256Sjkoshypmc_getrowdisp(int ri)
2292145256Sjkoshy{
2293145256Sjkoshy	return pmc_pmcdisp[ri];
2294145256Sjkoshy}
2295145256Sjkoshy
2296145256Sjkoshy/*
2297145256Sjkoshy * Check if a PMC at row index 'ri' can be allocated to the current
2298145256Sjkoshy * process.
2299145256Sjkoshy *
2300145256Sjkoshy * Allocation can fail if:
2301145256Sjkoshy *   - the current process is already being profiled by a PMC at index 'ri',
2302145256Sjkoshy *     attached to it via OP_PMCATTACH.
2303145256Sjkoshy *   - the current process has already allocated a PMC at index 'ri'
2304145256Sjkoshy *     via OP_ALLOCATE.
2305145256Sjkoshy */
2306145256Sjkoshy
2307145256Sjkoshystatic int
2308145774Sjkoshypmc_can_allocate_rowindex(struct proc *p, unsigned int ri, int cpu)
2309145256Sjkoshy{
2310145774Sjkoshy	enum pmc_mode mode;
2311145774Sjkoshy	struct pmc *pm;
2312145256Sjkoshy	struct pmc_owner *po;
2313145256Sjkoshy	struct pmc_process *pp;
2314145256Sjkoshy
2315145774Sjkoshy	PMCDBG(PMC,ALR,1, "can-allocate-rowindex proc=%p (%d, %s) ri=%d "
2316145774Sjkoshy	    "cpu=%d", p, p->p_pid, p->p_comm, ri, cpu);
2317145256Sjkoshy
2318145774Sjkoshy	/*
2319145774Sjkoshy	 * We shouldn't have already allocated a process-mode PMC at
2320145774Sjkoshy	 * row index 'ri'.
2321145774Sjkoshy	 *
2322145774Sjkoshy	 * We shouldn't have allocated a system-wide PMC on the same
2323145774Sjkoshy	 * CPU and same RI.
2324145774Sjkoshy	 */
2325145256Sjkoshy	if ((po = pmc_find_owner_descriptor(p)) != NULL)
2326147191Sjkoshy		LIST_FOREACH(pm, &po->po_pmcs, pm_next) {
2327145774Sjkoshy		    if (PMC_TO_ROWINDEX(pm) == ri) {
2328145774Sjkoshy			    mode = PMC_TO_MODE(pm);
2329145774Sjkoshy			    if (PMC_IS_VIRTUAL_MODE(mode))
2330145774Sjkoshy				    return EEXIST;
2331145774Sjkoshy			    if (PMC_IS_SYSTEM_MODE(mode) &&
2332145774Sjkoshy				(int) PMC_TO_CPU(pm) == cpu)
2333145774Sjkoshy				    return EEXIST;
2334145774Sjkoshy		    }
2335145774Sjkoshy	        }
2336145256Sjkoshy
2337145774Sjkoshy	/*
2338145774Sjkoshy	 * We also shouldn't be the target of any PMC at this index
2339145774Sjkoshy	 * since otherwise a PMC_ATTACH to ourselves will fail.
2340145774Sjkoshy	 */
2341145256Sjkoshy	if ((pp = pmc_find_process_descriptor(p, 0)) != NULL)
2342145256Sjkoshy		if (pp->pp_pmcs[ri].pp_pmc)
2343145256Sjkoshy			return EEXIST;
2344145256Sjkoshy
2345145256Sjkoshy	PMCDBG(PMC,ALR,2, "can-allocate-rowindex proc=%p (%d, %s) ri=%d ok",
2346145256Sjkoshy	    p, p->p_pid, p->p_comm, ri);
2347145256Sjkoshy
2348145256Sjkoshy	return 0;
2349145256Sjkoshy}
2350145256Sjkoshy
2351145256Sjkoshy/*
2352145256Sjkoshy * Check if a given PMC at row index 'ri' can be currently used in
2353145256Sjkoshy * mode 'mode'.
2354145256Sjkoshy */
2355145256Sjkoshy
2356145256Sjkoshystatic int
2357145256Sjkoshypmc_can_allocate_row(int ri, enum pmc_mode mode)
2358145256Sjkoshy{
2359145256Sjkoshy	enum pmc_disp	disp;
2360145256Sjkoshy
2361145256Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
2362145256Sjkoshy
2363145256Sjkoshy	PMCDBG(PMC,ALR,1, "can-allocate-row ri=%d mode=%d", ri, mode);
2364145256Sjkoshy
2365145256Sjkoshy	if (PMC_IS_SYSTEM_MODE(mode))
2366145256Sjkoshy		disp = PMC_DISP_STANDALONE;
2367145256Sjkoshy	else
2368145256Sjkoshy		disp = PMC_DISP_THREAD;
2369145256Sjkoshy
2370145256Sjkoshy	/*
2371145256Sjkoshy	 * check disposition for PMC row 'ri':
2372145256Sjkoshy	 *
2373145256Sjkoshy	 * Expected disposition		Row-disposition		Result
2374145256Sjkoshy	 *
2375145256Sjkoshy	 * STANDALONE			STANDALONE or FREE	proceed
2376145256Sjkoshy	 * STANDALONE			THREAD			fail
2377145256Sjkoshy	 * THREAD			THREAD or FREE		proceed
2378145256Sjkoshy	 * THREAD			STANDALONE		fail
2379145256Sjkoshy	 */
2380145256Sjkoshy
2381145256Sjkoshy	if (!PMC_ROW_DISP_IS_FREE(ri) &&
2382145256Sjkoshy	    !(disp == PMC_DISP_THREAD && PMC_ROW_DISP_IS_THREAD(ri)) &&
2383145256Sjkoshy	    !(disp == PMC_DISP_STANDALONE && PMC_ROW_DISP_IS_STANDALONE(ri)))
2384145256Sjkoshy		return EBUSY;
2385145256Sjkoshy
2386145256Sjkoshy	/*
2387145256Sjkoshy	 * All OK
2388145256Sjkoshy	 */
2389145256Sjkoshy
2390145256Sjkoshy	PMCDBG(PMC,ALR,2, "can-allocate-row ri=%d mode=%d ok", ri, mode);
2391145256Sjkoshy
2392145256Sjkoshy	return 0;
2393145256Sjkoshy
2394145256Sjkoshy}
2395145256Sjkoshy
2396145256Sjkoshy/*
2397145774Sjkoshy * Find a PMC descriptor with user handle 'pmcid' for thread 'td'.
2398145256Sjkoshy */
2399145256Sjkoshy
2400145256Sjkoshystatic struct pmc *
2401145256Sjkoshypmc_find_pmc_descriptor_in_process(struct pmc_owner *po, pmc_id_t pmcid)
2402145256Sjkoshy{
2403147191Sjkoshy	struct pmc *pm;
2404145256Sjkoshy
2405145774Sjkoshy	KASSERT(PMC_ID_TO_ROWINDEX(pmcid) < md->pmd_npmc,
2406145774Sjkoshy	    ("[pmc,%d] Illegal pmc index %d (max %d)", __LINE__,
2407145774Sjkoshy		PMC_ID_TO_ROWINDEX(pmcid), md->pmd_npmc));
2408145256Sjkoshy
2409147191Sjkoshy	LIST_FOREACH(pm, &po->po_pmcs, pm_next)
2410147191Sjkoshy	    if (pm->pm_id == pmcid)
2411147191Sjkoshy		    return pm;
2412145256Sjkoshy
2413145256Sjkoshy	return NULL;
2414145256Sjkoshy}
2415145256Sjkoshy
2416145256Sjkoshystatic int
2417145256Sjkoshypmc_find_pmc(pmc_id_t pmcid, struct pmc **pmc)
2418145256Sjkoshy{
2419145256Sjkoshy
2420145256Sjkoshy	struct pmc *pm;
2421145256Sjkoshy	struct pmc_owner *po;
2422145256Sjkoshy
2423145256Sjkoshy	PMCDBG(PMC,FND,1, "find-pmc id=%d", pmcid);
2424145256Sjkoshy
2425145256Sjkoshy	if ((po = pmc_find_owner_descriptor(curthread->td_proc)) == NULL)
2426145256Sjkoshy		return ESRCH;
2427145256Sjkoshy
2428145256Sjkoshy	if ((pm = pmc_find_pmc_descriptor_in_process(po, pmcid)) == NULL)
2429145256Sjkoshy		return EINVAL;
2430145256Sjkoshy
2431145256Sjkoshy	PMCDBG(PMC,FND,2, "find-pmc id=%d -> pmc=%p", pmcid, pm);
2432145256Sjkoshy
2433145256Sjkoshy	*pmc = pm;
2434145256Sjkoshy	return 0;
2435145256Sjkoshy}
2436145256Sjkoshy
2437145256Sjkoshy/*
2438145256Sjkoshy * Start a PMC.
2439145256Sjkoshy */
2440145256Sjkoshy
2441145256Sjkoshystatic int
2442145256Sjkoshypmc_start(struct pmc *pm)
2443145256Sjkoshy{
2444145774Sjkoshy	enum pmc_mode mode;
2445147191Sjkoshy	struct pmc_owner *po;
2446145256Sjkoshy	struct pmc_binding pb;
2447184802Sjkoshy	struct pmc_classdep *pcd;
2448184802Sjkoshy	int adjri, error, cpu, ri;
2449145256Sjkoshy
2450145256Sjkoshy	KASSERT(pm != NULL,
2451145256Sjkoshy	    ("[pmc,%d] null pm", __LINE__));
2452145256Sjkoshy
2453145774Sjkoshy	mode = PMC_TO_MODE(pm);
2454145774Sjkoshy	ri   = PMC_TO_ROWINDEX(pm);
2455184802Sjkoshy	pcd  = pmc_ri_to_classdep(md, ri, &adjri);
2456184802Sjkoshy
2457145774Sjkoshy	error = 0;
2458145256Sjkoshy
2459145774Sjkoshy	PMCDBG(PMC,OPS,1, "start pmc=%p mode=%d ri=%d", pm, mode, ri);
2460145774Sjkoshy
2461147191Sjkoshy	po = pm->pm_owner;
2462145256Sjkoshy
2463174395Sjkoshy	/*
2464174395Sjkoshy	 * Disallow PMCSTART if a logfile is required but has not been
2465174395Sjkoshy	 * configured yet.
2466174395Sjkoshy	 */
2467174395Sjkoshy	if ((pm->pm_flags & PMC_F_NEEDS_LOGFILE) &&
2468174395Sjkoshy	    (po->po_flags & PMC_PO_OWNS_LOGFILE) == 0)
2469184802Sjkoshy		return (EDOOFUS);	/* programming error */
2470174395Sjkoshy
2471174395Sjkoshy	/*
2472174395Sjkoshy	 * If this is a sampling mode PMC, log mapping information for
2473174395Sjkoshy	 * the kernel modules that are currently loaded.
2474174395Sjkoshy	 */
2475174395Sjkoshy	if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
2476174395Sjkoshy	    pmc_log_kernel_mappings(pm);
2477174395Sjkoshy
2478145774Sjkoshy	if (PMC_IS_VIRTUAL_MODE(mode)) {
2479145256Sjkoshy
2480145256Sjkoshy		/*
2481147191Sjkoshy		 * If a PMCATTACH has never been done on this PMC,
2482147191Sjkoshy		 * attach it to its owner process.
2483145256Sjkoshy		 */
2484145256Sjkoshy
2485145256Sjkoshy		if (LIST_EMPTY(&pm->pm_targets))
2486147191Sjkoshy			error = (pm->pm_flags & PMC_F_ATTACH_DONE) ? ESRCH :
2487147191Sjkoshy			    pmc_attach_process(po->po_owner, pm);
2488145256Sjkoshy
2489145774Sjkoshy		/*
2490147191Sjkoshy		 * If the PMC is attached to its owner, then force a context
2491147191Sjkoshy		 * switch to ensure that the MD state gets set correctly.
2492145256Sjkoshy		 */
2493145256Sjkoshy
2494147191Sjkoshy		if (error == 0) {
2495147191Sjkoshy			pm->pm_state = PMC_STATE_RUNNING;
2496147191Sjkoshy			if (pm->pm_flags & PMC_F_ATTACHED_TO_OWNER)
2497147191Sjkoshy				pmc_force_context_switch();
2498147191Sjkoshy		}
2499147191Sjkoshy
2500184802Sjkoshy		return (error);
2501147191Sjkoshy	}
2502145256Sjkoshy
2503147191Sjkoshy
2504147191Sjkoshy	/*
2505147191Sjkoshy	 * A system-wide PMC.
2506174395Sjkoshy	 *
2507147191Sjkoshy	 * Add the owner to the global list if this is a system-wide
2508147191Sjkoshy	 * sampling PMC.
2509147191Sjkoshy	 */
2510147191Sjkoshy
2511147191Sjkoshy	if (mode == PMC_MODE_SS) {
2512147191Sjkoshy		if (po->po_sscount == 0) {
2513147191Sjkoshy			LIST_INSERT_HEAD(&pmc_ss_owners, po, po_ssnext);
2514147191Sjkoshy			atomic_add_rel_int(&pmc_ss_count, 1);
2515147191Sjkoshy			PMCDBG(PMC,OPS,1, "po=%p in global list", po);
2516147191Sjkoshy		}
2517147191Sjkoshy		po->po_sscount++;
2518145256Sjkoshy	}
2519145256Sjkoshy
2520174395Sjkoshy	/* Log mapping information for all processes in the system. */
2521174395Sjkoshy	pmc_log_all_process_mappings(po);
2522157144Sjkoshy
2523145256Sjkoshy	/*
2524147191Sjkoshy	 * Move to the CPU associated with this
2525145256Sjkoshy	 * PMC, and start the hardware.
2526145256Sjkoshy	 */
2527145256Sjkoshy
2528145256Sjkoshy	pmc_save_cpu_binding(&pb);
2529145256Sjkoshy
2530145774Sjkoshy	cpu = PMC_TO_CPU(pm);
2531145256Sjkoshy
2532183266Sjkoshy	if (!pmc_cpu_is_active(cpu))
2533184802Sjkoshy		return (ENXIO);
2534145256Sjkoshy
2535145256Sjkoshy	pmc_select_cpu(cpu);
2536145256Sjkoshy
2537145256Sjkoshy	/*
2538145256Sjkoshy	 * global PMCs are configured at allocation time
2539145256Sjkoshy	 * so write out the initial value and start the PMC.
2540145256Sjkoshy	 */
2541145256Sjkoshy
2542147191Sjkoshy	pm->pm_state = PMC_STATE_RUNNING;
2543147191Sjkoshy
2544145774Sjkoshy	critical_enter();
2545184802Sjkoshy	if ((error = pcd->pcd_write_pmc(cpu, adjri,
2546145774Sjkoshy		 PMC_IS_SAMPLING_MODE(mode) ?
2547145256Sjkoshy		 pm->pm_sc.pm_reloadcount :
2548145256Sjkoshy		 pm->pm_sc.pm_initial)) == 0)
2549184802Sjkoshy		error = pcd->pcd_start_pmc(cpu, adjri);
2550145774Sjkoshy	critical_exit();
2551145256Sjkoshy
2552145256Sjkoshy	pmc_restore_cpu_binding(&pb);
2553145256Sjkoshy
2554184802Sjkoshy	return (error);
2555145256Sjkoshy}
2556145256Sjkoshy
2557145256Sjkoshy/*
2558145256Sjkoshy * Stop a PMC.
2559145256Sjkoshy */
2560145256Sjkoshy
2561145256Sjkoshystatic int
2562145256Sjkoshypmc_stop(struct pmc *pm)
2563145256Sjkoshy{
2564147191Sjkoshy	struct pmc_owner *po;
2565145256Sjkoshy	struct pmc_binding pb;
2566184802Sjkoshy	struct pmc_classdep *pcd;
2567184802Sjkoshy	int adjri, cpu, error, ri;
2568145256Sjkoshy
2569145256Sjkoshy	KASSERT(pm != NULL, ("[pmc,%d] null pmc", __LINE__));
2570145256Sjkoshy
2571145774Sjkoshy	PMCDBG(PMC,OPS,1, "stop pmc=%p mode=%d ri=%d", pm,
2572145774Sjkoshy	    PMC_TO_MODE(pm), PMC_TO_ROWINDEX(pm));
2573145256Sjkoshy
2574145256Sjkoshy	pm->pm_state = PMC_STATE_STOPPED;
2575145256Sjkoshy
2576145256Sjkoshy	/*
2577145256Sjkoshy	 * If the PMC is a virtual mode one, changing the state to
2578145256Sjkoshy	 * non-RUNNING is enough to ensure that the PMC never gets
2579145256Sjkoshy	 * scheduled.
2580145256Sjkoshy	 *
2581145256Sjkoshy	 * If this PMC is current running on a CPU, then it will
2582145256Sjkoshy	 * handled correctly at the time its target process is context
2583145256Sjkoshy	 * switched out.
2584145256Sjkoshy	 */
2585145256Sjkoshy
2586145774Sjkoshy	if (PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm)))
2587145256Sjkoshy		return 0;
2588145256Sjkoshy
2589145256Sjkoshy	/*
2590145256Sjkoshy	 * A system-mode PMC.  Move to the CPU associated with
2591145256Sjkoshy	 * this PMC, and stop the hardware.  We update the
2592145256Sjkoshy	 * 'initial count' so that a subsequent PMCSTART will
2593145256Sjkoshy	 * resume counting from the current hardware count.
2594145256Sjkoshy	 */
2595145256Sjkoshy
2596145256Sjkoshy	pmc_save_cpu_binding(&pb);
2597145256Sjkoshy
2598145774Sjkoshy	cpu = PMC_TO_CPU(pm);
2599145256Sjkoshy
2600183266Sjkoshy	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
2601145774Sjkoshy	    ("[pmc,%d] illegal cpu=%d", __LINE__, cpu));
2602145774Sjkoshy
2603183266Sjkoshy	if (!pmc_cpu_is_active(cpu))
2604145256Sjkoshy		return ENXIO;
2605145256Sjkoshy
2606145256Sjkoshy	pmc_select_cpu(cpu);
2607145256Sjkoshy
2608145774Sjkoshy	ri = PMC_TO_ROWINDEX(pm);
2609184802Sjkoshy	pcd = pmc_ri_to_classdep(md, ri, &adjri);
2610145256Sjkoshy
2611145774Sjkoshy	critical_enter();
2612184802Sjkoshy	if ((error = pcd->pcd_stop_pmc(cpu, adjri)) == 0)
2613184802Sjkoshy		error = pcd->pcd_read_pmc(cpu, adjri, &pm->pm_sc.pm_initial);
2614145774Sjkoshy	critical_exit();
2615145774Sjkoshy
2616145256Sjkoshy	pmc_restore_cpu_binding(&pb);
2617145256Sjkoshy
2618147191Sjkoshy	po = pm->pm_owner;
2619147191Sjkoshy
2620147191Sjkoshy	/* remove this owner from the global list of SS PMC owners */
2621147191Sjkoshy	if (PMC_TO_MODE(pm) == PMC_MODE_SS) {
2622147191Sjkoshy		po->po_sscount--;
2623147191Sjkoshy		if (po->po_sscount == 0) {
2624147191Sjkoshy			atomic_subtract_rel_int(&pmc_ss_count, 1);
2625147191Sjkoshy			LIST_REMOVE(po, po_ssnext);
2626147191Sjkoshy			PMCDBG(PMC,OPS,2,"po=%p removed from global list", po);
2627147191Sjkoshy		}
2628147191Sjkoshy	}
2629147191Sjkoshy
2630184802Sjkoshy	return (error);
2631145256Sjkoshy}
2632145256Sjkoshy
2633145256Sjkoshy
2634153110Sru#ifdef	DEBUG
2635145256Sjkoshystatic const char *pmc_op_to_name[] = {
2636145256Sjkoshy#undef	__PMC_OP
2637145256Sjkoshy#define	__PMC_OP(N, D)	#N ,
2638145256Sjkoshy	__PMC_OPS()
2639145256Sjkoshy	NULL
2640145256Sjkoshy};
2641145256Sjkoshy#endif
2642145256Sjkoshy
2643145256Sjkoshy/*
2644145256Sjkoshy * The syscall interface
2645145256Sjkoshy */
2646145256Sjkoshy
2647145256Sjkoshy#define	PMC_GET_SX_XLOCK(...) do {		\
2648145256Sjkoshy	sx_xlock(&pmc_sx);			\
2649145256Sjkoshy	if (pmc_hook == NULL) {			\
2650145256Sjkoshy		sx_xunlock(&pmc_sx);		\
2651145256Sjkoshy		return __VA_ARGS__;		\
2652145256Sjkoshy	}					\
2653145256Sjkoshy} while (0)
2654145256Sjkoshy
2655145256Sjkoshy#define	PMC_DOWNGRADE_SX() do {			\
2656145256Sjkoshy	sx_downgrade(&pmc_sx);			\
2657145256Sjkoshy	is_sx_downgraded = 1;			\
2658145256Sjkoshy} while (0)
2659145256Sjkoshy
2660145256Sjkoshystatic int
2661145256Sjkoshypmc_syscall_handler(struct thread *td, void *syscall_args)
2662145256Sjkoshy{
2663145256Sjkoshy	int error, is_sx_downgraded, op;
2664145256Sjkoshy	struct pmc_syscall_args *c;
2665145256Sjkoshy	void *arg;
2666145256Sjkoshy
2667145256Sjkoshy	PMC_GET_SX_XLOCK(ENOSYS);
2668145256Sjkoshy
2669147191Sjkoshy	DROP_GIANT();
2670147191Sjkoshy
2671145256Sjkoshy	is_sx_downgraded = 0;
2672145256Sjkoshy
2673145256Sjkoshy	c = (struct pmc_syscall_args *) syscall_args;
2674145256Sjkoshy
2675145256Sjkoshy	op = c->pmop_code;
2676145256Sjkoshy	arg = c->pmop_data;
2677145256Sjkoshy
2678145256Sjkoshy	PMCDBG(MOD,PMS,1, "syscall op=%d \"%s\" arg=%p", op,
2679145256Sjkoshy	    pmc_op_to_name[op], arg);
2680145256Sjkoshy
2681145256Sjkoshy	error = 0;
2682145256Sjkoshy	atomic_add_int(&pmc_stats.pm_syscalls, 1);
2683145256Sjkoshy
2684145256Sjkoshy	switch(op)
2685145256Sjkoshy	{
2686145256Sjkoshy
2687145256Sjkoshy
2688145256Sjkoshy	/*
2689145256Sjkoshy	 * Configure a log file.
2690145256Sjkoshy	 *
2691145256Sjkoshy	 * XXX This OP will be reworked.
2692145256Sjkoshy	 */
2693145256Sjkoshy
2694145256Sjkoshy	case PMC_OP_CONFIGURELOG:
2695145256Sjkoshy	{
2696157144Sjkoshy		struct proc *p;
2697156466Sjkoshy		struct pmc *pm;
2698145256Sjkoshy		struct pmc_owner *po;
2699145256Sjkoshy		struct pmc_op_configurelog cl;
2700145256Sjkoshy
2701145256Sjkoshy		sx_assert(&pmc_sx, SX_XLOCKED);
2702145256Sjkoshy
2703145256Sjkoshy		if ((error = copyin(arg, &cl, sizeof(cl))) != 0)
2704145256Sjkoshy			break;
2705145256Sjkoshy
2706145256Sjkoshy		/* mark this process as owning a log file */
2707145256Sjkoshy		p = td->td_proc;
2708145256Sjkoshy		if ((po = pmc_find_owner_descriptor(p)) == NULL)
2709147191Sjkoshy			if ((po = pmc_allocate_owner_descriptor(p)) == NULL) {
2710147191Sjkoshy				error = ENOMEM;
2711147191Sjkoshy				break;
2712147191Sjkoshy			}
2713145256Sjkoshy
2714147191Sjkoshy		/*
2715147191Sjkoshy		 * If a valid fd was passed in, try to configure that,
2716147191Sjkoshy		 * otherwise if 'fd' was less than zero and there was
2717147191Sjkoshy		 * a log file configured, flush its buffers and
2718147191Sjkoshy		 * de-configure it.
2719147191Sjkoshy		 */
2720147191Sjkoshy		if (cl.pm_logfd >= 0)
2721185363Sjkoshy			error = pmclog_configure_log(md, po, cl.pm_logfd);
2722147191Sjkoshy		else if (po->po_flags & PMC_PO_OWNS_LOGFILE) {
2723147191Sjkoshy			pmclog_process_closelog(po);
2724147191Sjkoshy			error = pmclog_flush(po);
2725156466Sjkoshy			if (error == 0) {
2726156466Sjkoshy				LIST_FOREACH(pm, &po->po_pmcs, pm_next)
2727156834Sjkoshy				    if (pm->pm_flags & PMC_F_NEEDS_LOGFILE &&
2728156834Sjkoshy					pm->pm_state == PMC_STATE_RUNNING)
2729156466Sjkoshy					    pmc_stop(pm);
2730147191Sjkoshy				error = pmclog_deconfigure_log(po);
2731156466Sjkoshy			}
2732147191Sjkoshy		} else
2733147191Sjkoshy			error = EINVAL;
2734157144Sjkoshy
2735157144Sjkoshy		if (error)
2736157144Sjkoshy			break;
2737147191Sjkoshy	}
2738147191Sjkoshy	break;
2739147191Sjkoshy
2740147191Sjkoshy
2741147191Sjkoshy	/*
2742147191Sjkoshy	 * Flush a log file.
2743147191Sjkoshy	 */
2744147191Sjkoshy
2745147191Sjkoshy	case PMC_OP_FLUSHLOG:
2746147191Sjkoshy	{
2747147191Sjkoshy		struct pmc_owner *po;
2748147191Sjkoshy
2749147191Sjkoshy		sx_assert(&pmc_sx, SX_XLOCKED);
2750147191Sjkoshy
2751147191Sjkoshy		if ((po = pmc_find_owner_descriptor(td->td_proc)) == NULL) {
2752147191Sjkoshy			error = EINVAL;
2753145256Sjkoshy			break;
2754147191Sjkoshy		}
2755145256Sjkoshy
2756147191Sjkoshy		error = pmclog_flush(po);
2757145256Sjkoshy	}
2758145256Sjkoshy	break;
2759145256Sjkoshy
2760145256Sjkoshy	/*
2761145256Sjkoshy	 * Retrieve hardware configuration.
2762145256Sjkoshy	 */
2763145256Sjkoshy
2764145256Sjkoshy	case PMC_OP_GETCPUINFO:	/* CPU information */
2765145256Sjkoshy	{
2766145256Sjkoshy		struct pmc_op_getcpuinfo gci;
2767184802Sjkoshy		struct pmc_classinfo *pci;
2768184802Sjkoshy		struct pmc_classdep *pcd;
2769184802Sjkoshy		int cl;
2770145256Sjkoshy
2771145256Sjkoshy		gci.pm_cputype = md->pmd_cputype;
2772183266Sjkoshy		gci.pm_ncpu    = pmc_cpu_max();
2773145256Sjkoshy		gci.pm_npmc    = md->pmd_npmc;
2774145256Sjkoshy		gci.pm_nclass  = md->pmd_nclass;
2775184802Sjkoshy		pci = gci.pm_classes;
2776184802Sjkoshy		pcd = md->pmd_classdep;
2777184802Sjkoshy		for (cl = 0; cl < md->pmd_nclass; cl++, pci++, pcd++) {
2778184802Sjkoshy			pci->pm_caps  = pcd->pcd_caps;
2779184802Sjkoshy			pci->pm_class = pcd->pcd_class;
2780184802Sjkoshy			pci->pm_width = pcd->pcd_width;
2781184802Sjkoshy			pci->pm_num   = pcd->pcd_num;
2782184802Sjkoshy		}
2783145256Sjkoshy		error = copyout(&gci, arg, sizeof(gci));
2784145256Sjkoshy	}
2785145256Sjkoshy	break;
2786145256Sjkoshy
2787145256Sjkoshy
2788145256Sjkoshy	/*
2789145256Sjkoshy	 * Get module statistics
2790145256Sjkoshy	 */
2791145256Sjkoshy
2792145256Sjkoshy	case PMC_OP_GETDRIVERSTATS:
2793145256Sjkoshy	{
2794145256Sjkoshy		struct pmc_op_getdriverstats gms;
2795145256Sjkoshy
2796145256Sjkoshy		bcopy(&pmc_stats, &gms, sizeof(gms));
2797145256Sjkoshy		error = copyout(&gms, arg, sizeof(gms));
2798145256Sjkoshy	}
2799145256Sjkoshy	break;
2800145256Sjkoshy
2801145256Sjkoshy
2802145256Sjkoshy	/*
2803145256Sjkoshy	 * Retrieve module version number
2804145256Sjkoshy	 */
2805145256Sjkoshy
2806145256Sjkoshy	case PMC_OP_GETMODULEVERSION:
2807145256Sjkoshy	{
2808147191Sjkoshy		uint32_t cv, modv;
2809147191Sjkoshy
2810147191Sjkoshy		/* retrieve the client's idea of the ABI version */
2811147191Sjkoshy		if ((error = copyin(arg, &cv, sizeof(uint32_t))) != 0)
2812147191Sjkoshy			break;
2813147191Sjkoshy		/* don't service clients newer than our driver */
2814147191Sjkoshy		modv = PMC_VERSION;
2815147191Sjkoshy		if ((cv & 0xFFFF0000) > (modv & 0xFFFF0000)) {
2816147191Sjkoshy			error = EPROGMISMATCH;
2817147191Sjkoshy			break;
2818147191Sjkoshy		}
2819147191Sjkoshy		error = copyout(&modv, arg, sizeof(int));
2820145256Sjkoshy	}
2821145256Sjkoshy	break;
2822145256Sjkoshy
2823145256Sjkoshy
2824145256Sjkoshy	/*
2825145256Sjkoshy	 * Retrieve the state of all the PMCs on a given
2826145256Sjkoshy	 * CPU.
2827145256Sjkoshy	 */
2828145256Sjkoshy
2829145256Sjkoshy	case PMC_OP_GETPMCINFO:
2830145256Sjkoshy	{
2831184802Sjkoshy		int ari;
2832184802Sjkoshy		struct pmc *pm;
2833184802Sjkoshy		size_t pmcinfo_size;
2834145256Sjkoshy		uint32_t cpu, n, npmc;
2835184802Sjkoshy		struct pmc_owner *po;
2836184802Sjkoshy		struct pmc_binding pb;
2837184802Sjkoshy		struct pmc_classdep *pcd;
2838145256Sjkoshy		struct pmc_info *p, *pmcinfo;
2839145256Sjkoshy		struct pmc_op_getpmcinfo *gpi;
2840145256Sjkoshy
2841145256Sjkoshy		PMC_DOWNGRADE_SX();
2842145256Sjkoshy
2843145256Sjkoshy		gpi = (struct pmc_op_getpmcinfo *) arg;
2844145256Sjkoshy
2845145256Sjkoshy		if ((error = copyin(&gpi->pm_cpu, &cpu, sizeof(cpu))) != 0)
2846145256Sjkoshy			break;
2847145256Sjkoshy
2848183266Sjkoshy		if (cpu >= pmc_cpu_max()) {
2849145256Sjkoshy			error = EINVAL;
2850145256Sjkoshy			break;
2851145256Sjkoshy		}
2852145256Sjkoshy
2853183266Sjkoshy		if (!pmc_cpu_is_active(cpu)) {
2854145256Sjkoshy			error = ENXIO;
2855145256Sjkoshy			break;
2856145256Sjkoshy		}
2857145256Sjkoshy
2858145256Sjkoshy		/* switch to CPU 'cpu' */
2859145256Sjkoshy		pmc_save_cpu_binding(&pb);
2860145256Sjkoshy		pmc_select_cpu(cpu);
2861145256Sjkoshy
2862145256Sjkoshy		npmc = md->pmd_npmc;
2863145256Sjkoshy
2864145256Sjkoshy		pmcinfo_size = npmc * sizeof(struct pmc_info);
2865184214Sdes		pmcinfo = malloc(pmcinfo_size, M_PMC, M_WAITOK);
2866145256Sjkoshy
2867145256Sjkoshy		p = pmcinfo;
2868145256Sjkoshy
2869145256Sjkoshy		for (n = 0; n < md->pmd_npmc; n++, p++) {
2870145256Sjkoshy
2871184802Sjkoshy			pcd = pmc_ri_to_classdep(md, n, &ari);
2872184802Sjkoshy
2873184802Sjkoshy			KASSERT(pcd != NULL,
2874184802Sjkoshy			    ("[pmc,%d] null pcd ri=%d", __LINE__, n));
2875184802Sjkoshy
2876184802Sjkoshy			if ((error = pcd->pcd_describe(cpu, ari, p, &pm)) != 0)
2877145256Sjkoshy				break;
2878145256Sjkoshy
2879145256Sjkoshy			if (PMC_ROW_DISP_IS_STANDALONE(n))
2880145256Sjkoshy				p->pm_rowdisp = PMC_DISP_STANDALONE;
2881145256Sjkoshy			else if (PMC_ROW_DISP_IS_THREAD(n))
2882145256Sjkoshy				p->pm_rowdisp = PMC_DISP_THREAD;
2883145256Sjkoshy			else
2884145256Sjkoshy				p->pm_rowdisp = PMC_DISP_FREE;
2885145256Sjkoshy
2886145256Sjkoshy			p->pm_ownerpid = -1;
2887145256Sjkoshy
2888145256Sjkoshy			if (pm == NULL)	/* no PMC associated */
2889145256Sjkoshy				continue;
2890145256Sjkoshy
2891145256Sjkoshy			po = pm->pm_owner;
2892145256Sjkoshy
2893145256Sjkoshy			KASSERT(po->po_owner != NULL,
2894145256Sjkoshy			    ("[pmc,%d] pmc_owner had a null proc pointer",
2895145256Sjkoshy				__LINE__));
2896145256Sjkoshy
2897145256Sjkoshy			p->pm_ownerpid = po->po_owner->p_pid;
2898145774Sjkoshy			p->pm_mode     = PMC_TO_MODE(pm);
2899145256Sjkoshy			p->pm_event    = pm->pm_event;
2900145256Sjkoshy			p->pm_flags    = pm->pm_flags;
2901145256Sjkoshy
2902145774Sjkoshy			if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
2903145256Sjkoshy				p->pm_reloadcount =
2904145256Sjkoshy				    pm->pm_sc.pm_reloadcount;
2905145256Sjkoshy		}
2906145256Sjkoshy
2907145256Sjkoshy		pmc_restore_cpu_binding(&pb);
2908145256Sjkoshy
2909145256Sjkoshy		/* now copy out the PMC info collected */
2910145256Sjkoshy		if (error == 0)
2911145256Sjkoshy			error = copyout(pmcinfo, &gpi->pm_pmcs, pmcinfo_size);
2912145256Sjkoshy
2913184205Sdes		free(pmcinfo, M_PMC);
2914145256Sjkoshy	}
2915145256Sjkoshy	break;
2916145256Sjkoshy
2917145256Sjkoshy
2918145256Sjkoshy	/*
2919145256Sjkoshy	 * Set the administrative state of a PMC.  I.e. whether
2920145256Sjkoshy	 * the PMC is to be used or not.
2921145256Sjkoshy	 */
2922145256Sjkoshy
2923145256Sjkoshy	case PMC_OP_PMCADMIN:
2924145256Sjkoshy	{
2925145256Sjkoshy		int cpu, ri;
2926145256Sjkoshy		enum pmc_state request;
2927145256Sjkoshy		struct pmc_cpu *pc;
2928145256Sjkoshy		struct pmc_hw *phw;
2929145256Sjkoshy		struct pmc_op_pmcadmin pma;
2930145256Sjkoshy		struct pmc_binding pb;
2931145256Sjkoshy
2932145256Sjkoshy		sx_assert(&pmc_sx, SX_XLOCKED);
2933145256Sjkoshy
2934145256Sjkoshy		KASSERT(td == curthread,
2935145256Sjkoshy		    ("[pmc,%d] td != curthread", __LINE__));
2936145256Sjkoshy
2937164033Srwatson		error = priv_check(td, PRIV_PMC_MANAGE);
2938164033Srwatson		if (error)
2939145256Sjkoshy			break;
2940145256Sjkoshy
2941145256Sjkoshy		if ((error = copyin(arg, &pma, sizeof(pma))) != 0)
2942145256Sjkoshy			break;
2943145256Sjkoshy
2944145256Sjkoshy		cpu = pma.pm_cpu;
2945145256Sjkoshy
2946183266Sjkoshy		if (cpu < 0 || cpu >= (int) pmc_cpu_max()) {
2947145256Sjkoshy			error = EINVAL;
2948145256Sjkoshy			break;
2949145256Sjkoshy		}
2950145256Sjkoshy
2951183266Sjkoshy		if (!pmc_cpu_is_active(cpu)) {
2952145256Sjkoshy			error = ENXIO;
2953145256Sjkoshy			break;
2954145256Sjkoshy		}
2955145256Sjkoshy
2956145256Sjkoshy		request = pma.pm_state;
2957145256Sjkoshy
2958145256Sjkoshy		if (request != PMC_STATE_DISABLED &&
2959145256Sjkoshy		    request != PMC_STATE_FREE) {
2960145256Sjkoshy			error = EINVAL;
2961145256Sjkoshy			break;
2962145256Sjkoshy		}
2963145256Sjkoshy
2964145256Sjkoshy		ri = pma.pm_pmc; /* pmc id == row index */
2965145256Sjkoshy		if (ri < 0 || ri >= (int) md->pmd_npmc) {
2966145256Sjkoshy			error = EINVAL;
2967145256Sjkoshy			break;
2968145256Sjkoshy		}
2969145256Sjkoshy
2970145256Sjkoshy		/*
2971145256Sjkoshy		 * We can't disable a PMC with a row-index allocated
2972145256Sjkoshy		 * for process virtual PMCs.
2973145256Sjkoshy		 */
2974145256Sjkoshy
2975145256Sjkoshy		if (PMC_ROW_DISP_IS_THREAD(ri) &&
2976145256Sjkoshy		    request == PMC_STATE_DISABLED) {
2977145256Sjkoshy			error = EBUSY;
2978145256Sjkoshy			break;
2979145256Sjkoshy		}
2980145256Sjkoshy
2981145256Sjkoshy		/*
2982145256Sjkoshy		 * otherwise, this PMC on this CPU is either free or
2983145256Sjkoshy		 * in system-wide mode.
2984145256Sjkoshy		 */
2985145256Sjkoshy
2986145256Sjkoshy		pmc_save_cpu_binding(&pb);
2987145256Sjkoshy		pmc_select_cpu(cpu);
2988145256Sjkoshy
2989145256Sjkoshy		pc  = pmc_pcpu[cpu];
2990145256Sjkoshy		phw = pc->pc_hwpmcs[ri];
2991145256Sjkoshy
2992145256Sjkoshy		/*
2993145256Sjkoshy		 * XXX do we need some kind of 'forced' disable?
2994145256Sjkoshy		 */
2995145256Sjkoshy
2996145256Sjkoshy		if (phw->phw_pmc == NULL) {
2997145256Sjkoshy			if (request == PMC_STATE_DISABLED &&
2998145256Sjkoshy			    (phw->phw_state & PMC_PHW_FLAG_IS_ENABLED)) {
2999145256Sjkoshy				phw->phw_state &= ~PMC_PHW_FLAG_IS_ENABLED;
3000145256Sjkoshy				PMC_MARK_ROW_STANDALONE(ri);
3001145256Sjkoshy			} else if (request == PMC_STATE_FREE &&
3002145256Sjkoshy			    (phw->phw_state & PMC_PHW_FLAG_IS_ENABLED) == 0) {
3003145256Sjkoshy				phw->phw_state |=  PMC_PHW_FLAG_IS_ENABLED;
3004145256Sjkoshy				PMC_UNMARK_ROW_STANDALONE(ri);
3005145256Sjkoshy			}
3006145256Sjkoshy			/* other cases are a no-op */
3007145256Sjkoshy		} else
3008145256Sjkoshy			error = EBUSY;
3009145256Sjkoshy
3010145256Sjkoshy		pmc_restore_cpu_binding(&pb);
3011145256Sjkoshy	}
3012145256Sjkoshy	break;
3013145256Sjkoshy
3014145256Sjkoshy
3015145256Sjkoshy	/*
3016145256Sjkoshy	 * Allocate a PMC.
3017145256Sjkoshy	 */
3018145256Sjkoshy
3019145256Sjkoshy	case PMC_OP_PMCALLOCATE:
3020145256Sjkoshy	{
3021184802Sjkoshy		int adjri, n;
3022184802Sjkoshy		u_int cpu;
3023145256Sjkoshy		uint32_t caps;
3024184802Sjkoshy		struct pmc *pmc;
3025145256Sjkoshy		enum pmc_mode mode;
3026145774Sjkoshy		struct pmc_hw *phw;
3027184802Sjkoshy		struct pmc_binding pb;
3028184802Sjkoshy		struct pmc_classdep *pcd;
3029145256Sjkoshy		struct pmc_op_pmcallocate pa;
3030145256Sjkoshy
3031145256Sjkoshy		if ((error = copyin(arg, &pa, sizeof(pa))) != 0)
3032145256Sjkoshy			break;
3033145256Sjkoshy
3034145256Sjkoshy		caps = pa.pm_caps;
3035145256Sjkoshy		mode = pa.pm_mode;
3036145256Sjkoshy		cpu  = pa.pm_cpu;
3037145256Sjkoshy
3038145256Sjkoshy		if ((mode != PMC_MODE_SS  &&  mode != PMC_MODE_SC  &&
3039145256Sjkoshy		     mode != PMC_MODE_TS  &&  mode != PMC_MODE_TC) ||
3040183266Sjkoshy		    (cpu != (u_int) PMC_CPU_ANY && cpu >= pmc_cpu_max())) {
3041145256Sjkoshy			error = EINVAL;
3042145256Sjkoshy			break;
3043145256Sjkoshy		}
3044145256Sjkoshy
3045145256Sjkoshy		/*
3046145256Sjkoshy		 * Virtual PMCs should only ask for a default CPU.
3047145256Sjkoshy		 * System mode PMCs need to specify a non-default CPU.
3048145256Sjkoshy		 */
3049145256Sjkoshy
3050145256Sjkoshy		if ((PMC_IS_VIRTUAL_MODE(mode) && cpu != (u_int) PMC_CPU_ANY) ||
3051145256Sjkoshy		    (PMC_IS_SYSTEM_MODE(mode) && cpu == (u_int) PMC_CPU_ANY)) {
3052145256Sjkoshy			error = EINVAL;
3053145256Sjkoshy			break;
3054145256Sjkoshy		}
3055145256Sjkoshy
3056145256Sjkoshy		/*
3057183266Sjkoshy		 * Check that an inactive CPU is not being asked for.
3058145256Sjkoshy		 */
3059145256Sjkoshy
3060183266Sjkoshy		if (PMC_IS_SYSTEM_MODE(mode) && !pmc_cpu_is_active(cpu)) {
3061145256Sjkoshy			error = ENXIO;
3062145256Sjkoshy			break;
3063145256Sjkoshy		}
3064145256Sjkoshy
3065145256Sjkoshy		/*
3066145256Sjkoshy		 * Refuse an allocation for a system-wide PMC if this
3067145256Sjkoshy		 * process has been jailed, or if this process lacks
3068145256Sjkoshy		 * super-user credentials and the sysctl tunable
3069145256Sjkoshy		 * 'security.bsd.unprivileged_syspmcs' is zero.
3070145256Sjkoshy		 */
3071145256Sjkoshy
3072145256Sjkoshy		if (PMC_IS_SYSTEM_MODE(mode)) {
3073164033Srwatson			if (jailed(curthread->td_ucred)) {
3074145256Sjkoshy				error = EPERM;
3075164033Srwatson				break;
3076164033Srwatson			}
3077164033Srwatson			if (!pmc_unprivileged_syspmcs) {
3078164033Srwatson				error = priv_check(curthread,
3079164033Srwatson				    PRIV_PMC_SYSTEM);
3080164033Srwatson				if (error)
3081164033Srwatson					break;
3082164033Srwatson			}
3083145256Sjkoshy		}
3084145256Sjkoshy
3085145256Sjkoshy		if (error)
3086145256Sjkoshy			break;
3087145256Sjkoshy
3088145256Sjkoshy		/*
3089145256Sjkoshy		 * Look for valid values for 'pm_flags'
3090145256Sjkoshy		 */
3091145256Sjkoshy
3092147191Sjkoshy		if ((pa.pm_flags & ~(PMC_F_DESCENDANTS | PMC_F_LOG_PROCCSW |
3093174395Sjkoshy		    PMC_F_LOG_PROCEXIT | PMC_F_CALLCHAIN)) != 0) {
3094145256Sjkoshy			error = EINVAL;
3095145256Sjkoshy			break;
3096145256Sjkoshy		}
3097145256Sjkoshy
3098147191Sjkoshy		/* process logging options are not allowed for system PMCs */
3099147191Sjkoshy		if (PMC_IS_SYSTEM_MODE(mode) && (pa.pm_flags &
3100147191Sjkoshy		    (PMC_F_LOG_PROCCSW | PMC_F_LOG_PROCEXIT))) {
3101147191Sjkoshy			error = EINVAL;
3102147191Sjkoshy			break;
3103147191Sjkoshy		}
3104147191Sjkoshy
3105145256Sjkoshy		/*
3106145256Sjkoshy		 * All sampling mode PMCs need to be able to interrupt the
3107145256Sjkoshy		 * CPU.
3108145256Sjkoshy		 */
3109147191Sjkoshy		if (PMC_IS_SAMPLING_MODE(mode))
3110145256Sjkoshy			caps |= PMC_CAP_INTERRUPT;
3111145256Sjkoshy
3112149374Sjkoshy		/* A valid class specifier should have been passed in. */
3113149374Sjkoshy		for (n = 0; n < md->pmd_nclass; n++)
3114184802Sjkoshy			if (md->pmd_classdep[n].pcd_class == pa.pm_class)
3115149374Sjkoshy				break;
3116149374Sjkoshy		if (n == md->pmd_nclass) {
3117149374Sjkoshy			error = EINVAL;
3118149374Sjkoshy			break;
3119149374Sjkoshy		}
3120149374Sjkoshy
3121149374Sjkoshy		/* The requested PMC capabilities should be feasible. */
3122184802Sjkoshy		if ((md->pmd_classdep[n].pcd_caps & caps) != caps) {
3123149374Sjkoshy			error = EOPNOTSUPP;
3124149374Sjkoshy			break;
3125149374Sjkoshy		}
3126149374Sjkoshy
3127145256Sjkoshy		PMCDBG(PMC,ALL,2, "event=%d caps=0x%x mode=%d cpu=%d",
3128145256Sjkoshy		    pa.pm_ev, caps, mode, cpu);
3129145256Sjkoshy
3130145256Sjkoshy		pmc = pmc_allocate_pmc_descriptor();
3131145774Sjkoshy		pmc->pm_id    = PMC_ID_MAKE_ID(cpu,pa.pm_mode,pa.pm_class,
3132145774Sjkoshy		    PMC_ID_INVALID);
3133145256Sjkoshy		pmc->pm_event = pa.pm_ev;
3134145256Sjkoshy		pmc->pm_state = PMC_STATE_FREE;
3135145256Sjkoshy		pmc->pm_caps  = caps;
3136145256Sjkoshy		pmc->pm_flags = pa.pm_flags;
3137145256Sjkoshy
3138145256Sjkoshy		/* switch thread to CPU 'cpu' */
3139145256Sjkoshy		pmc_save_cpu_binding(&pb);
3140145256Sjkoshy
3141145256Sjkoshy#define	PMC_IS_SHAREABLE_PMC(cpu, n)				\
3142145256Sjkoshy	(pmc_pcpu[(cpu)]->pc_hwpmcs[(n)]->phw_state &		\
3143145256Sjkoshy	 PMC_PHW_FLAG_IS_SHAREABLE)
3144145256Sjkoshy#define	PMC_IS_UNALLOCATED(cpu, n)				\
3145145256Sjkoshy	(pmc_pcpu[(cpu)]->pc_hwpmcs[(n)]->phw_pmc == NULL)
3146145256Sjkoshy
3147145256Sjkoshy		if (PMC_IS_SYSTEM_MODE(mode)) {
3148145256Sjkoshy			pmc_select_cpu(cpu);
3149184802Sjkoshy			for (n = 0; n < (int) md->pmd_npmc; n++) {
3150184802Sjkoshy				pcd = pmc_ri_to_classdep(md, n, &adjri);
3151145256Sjkoshy				if (pmc_can_allocate_row(n, mode) == 0 &&
3152145256Sjkoshy				    pmc_can_allocate_rowindex(
3153145774Sjkoshy					    curthread->td_proc, n, cpu) == 0 &&
3154145256Sjkoshy				    (PMC_IS_UNALLOCATED(cpu, n) ||
3155145256Sjkoshy				     PMC_IS_SHAREABLE_PMC(cpu, n)) &&
3156184802Sjkoshy				    pcd->pcd_allocate_pmc(cpu, adjri, pmc,
3157145256Sjkoshy					&pa) == 0)
3158145256Sjkoshy					break;
3159184802Sjkoshy			}
3160145256Sjkoshy		} else {
3161145256Sjkoshy			/* Process virtual mode */
3162145256Sjkoshy			for (n = 0; n < (int) md->pmd_npmc; n++) {
3163184802Sjkoshy				pcd = pmc_ri_to_classdep(md, n, &adjri);
3164145256Sjkoshy				if (pmc_can_allocate_row(n, mode) == 0 &&
3165145256Sjkoshy				    pmc_can_allocate_rowindex(
3166145774Sjkoshy					    curthread->td_proc, n,
3167145774Sjkoshy					    PMC_CPU_ANY) == 0 &&
3168184802Sjkoshy				    pcd->pcd_allocate_pmc(curthread->td_oncpu,
3169184802Sjkoshy					adjri, pmc, &pa) == 0)
3170145256Sjkoshy					break;
3171145256Sjkoshy			}
3172145256Sjkoshy		}
3173145256Sjkoshy
3174145256Sjkoshy#undef	PMC_IS_UNALLOCATED
3175145256Sjkoshy#undef	PMC_IS_SHAREABLE_PMC
3176145256Sjkoshy
3177145256Sjkoshy		pmc_restore_cpu_binding(&pb);
3178145256Sjkoshy
3179145256Sjkoshy		if (n == (int) md->pmd_npmc) {
3180145256Sjkoshy			pmc_destroy_pmc_descriptor(pmc);
3181184205Sdes			free(pmc, M_PMC);
3182145256Sjkoshy			pmc = NULL;
3183145256Sjkoshy			error = EINVAL;
3184145256Sjkoshy			break;
3185145256Sjkoshy		}
3186145256Sjkoshy
3187145774Sjkoshy		/* Fill in the correct value in the ID field */
3188145774Sjkoshy		pmc->pm_id = PMC_ID_MAKE_ID(cpu,mode,pa.pm_class,n);
3189145256Sjkoshy
3190145774Sjkoshy		PMCDBG(PMC,ALL,2, "ev=%d class=%d mode=%d n=%d -> pmcid=%x",
3191145774Sjkoshy		    pmc->pm_event, pa.pm_class, mode, n, pmc->pm_id);
3192145774Sjkoshy
3193147191Sjkoshy		/* Process mode PMCs with logging enabled need log files */
3194147191Sjkoshy		if (pmc->pm_flags & (PMC_F_LOG_PROCEXIT | PMC_F_LOG_PROCCSW))
3195147191Sjkoshy			pmc->pm_flags |= PMC_F_NEEDS_LOGFILE;
3196147191Sjkoshy
3197147191Sjkoshy		/* All system mode sampling PMCs require a log file */
3198147191Sjkoshy		if (PMC_IS_SAMPLING_MODE(mode) && PMC_IS_SYSTEM_MODE(mode))
3199147191Sjkoshy			pmc->pm_flags |= PMC_F_NEEDS_LOGFILE;
3200147191Sjkoshy
3201145256Sjkoshy		/*
3202145256Sjkoshy		 * Configure global pmc's immediately
3203145256Sjkoshy		 */
3204145256Sjkoshy
3205145774Sjkoshy		if (PMC_IS_SYSTEM_MODE(PMC_TO_MODE(pmc))) {
3206145774Sjkoshy
3207145774Sjkoshy			pmc_save_cpu_binding(&pb);
3208145774Sjkoshy			pmc_select_cpu(cpu);
3209145774Sjkoshy
3210145774Sjkoshy			phw = pmc_pcpu[cpu]->pc_hwpmcs[n];
3211184802Sjkoshy			pcd = pmc_ri_to_classdep(md, n, &adjri);
3212145774Sjkoshy
3213145774Sjkoshy			if ((phw->phw_state & PMC_PHW_FLAG_IS_ENABLED) == 0 ||
3214184802Sjkoshy			    (error = pcd->pcd_config_pmc(cpu, adjri, pmc)) != 0) {
3215184802Sjkoshy				(void) pcd->pcd_release_pmc(cpu, adjri, pmc);
3216145256Sjkoshy				pmc_destroy_pmc_descriptor(pmc);
3217184205Sdes				free(pmc, M_PMC);
3218145256Sjkoshy				pmc = NULL;
3219145774Sjkoshy				pmc_restore_cpu_binding(&pb);
3220145774Sjkoshy				error = EPERM;
3221145256Sjkoshy				break;
3222145256Sjkoshy			}
3223145256Sjkoshy
3224145774Sjkoshy			pmc_restore_cpu_binding(&pb);
3225145774Sjkoshy		}
3226145256Sjkoshy
3227145256Sjkoshy		pmc->pm_state    = PMC_STATE_ALLOCATED;
3228145256Sjkoshy
3229145256Sjkoshy		/*
3230145256Sjkoshy		 * mark row disposition
3231145256Sjkoshy		 */
3232145256Sjkoshy
3233145256Sjkoshy		if (PMC_IS_SYSTEM_MODE(mode))
3234145256Sjkoshy			PMC_MARK_ROW_STANDALONE(n);
3235145256Sjkoshy		else
3236145256Sjkoshy			PMC_MARK_ROW_THREAD(n);
3237145256Sjkoshy
3238145256Sjkoshy		/*
3239145256Sjkoshy		 * Register this PMC with the current thread as its owner.
3240145256Sjkoshy		 */
3241145256Sjkoshy
3242145256Sjkoshy		if ((error =
3243145256Sjkoshy		    pmc_register_owner(curthread->td_proc, pmc)) != 0) {
3244145256Sjkoshy			pmc_release_pmc_descriptor(pmc);
3245184205Sdes			free(pmc, M_PMC);
3246145256Sjkoshy			pmc = NULL;
3247145256Sjkoshy			break;
3248145256Sjkoshy		}
3249145256Sjkoshy
3250145256Sjkoshy		/*
3251145256Sjkoshy		 * Return the allocated index.
3252145256Sjkoshy		 */
3253145256Sjkoshy
3254145774Sjkoshy		pa.pm_pmcid = pmc->pm_id;
3255145256Sjkoshy
3256145256Sjkoshy		error = copyout(&pa, arg, sizeof(pa));
3257145256Sjkoshy	}
3258145256Sjkoshy	break;
3259145256Sjkoshy
3260145256Sjkoshy
3261145256Sjkoshy	/*
3262145256Sjkoshy	 * Attach a PMC to a process.
3263145256Sjkoshy	 */
3264145256Sjkoshy
3265145256Sjkoshy	case PMC_OP_PMCATTACH:
3266145256Sjkoshy	{
3267145256Sjkoshy		struct pmc *pm;
3268145256Sjkoshy		struct proc *p;
3269145256Sjkoshy		struct pmc_op_pmcattach a;
3270145256Sjkoshy
3271145256Sjkoshy		sx_assert(&pmc_sx, SX_XLOCKED);
3272145256Sjkoshy
3273145256Sjkoshy		if ((error = copyin(arg, &a, sizeof(a))) != 0)
3274145256Sjkoshy			break;
3275145256Sjkoshy
3276145256Sjkoshy		if (a.pm_pid < 0) {
3277145256Sjkoshy			error = EINVAL;
3278145256Sjkoshy			break;
3279145256Sjkoshy		} else if (a.pm_pid == 0)
3280145256Sjkoshy			a.pm_pid = td->td_proc->p_pid;
3281145256Sjkoshy
3282145256Sjkoshy		if ((error = pmc_find_pmc(a.pm_pmc, &pm)) != 0)
3283145256Sjkoshy			break;
3284145256Sjkoshy
3285145774Sjkoshy		if (PMC_IS_SYSTEM_MODE(PMC_TO_MODE(pm))) {
3286145256Sjkoshy			error = EINVAL;
3287145256Sjkoshy			break;
3288145256Sjkoshy		}
3289145256Sjkoshy
3290145256Sjkoshy		/* PMCs may be (re)attached only when allocated or stopped */
3291145256Sjkoshy		if (pm->pm_state == PMC_STATE_RUNNING) {
3292145256Sjkoshy			error = EBUSY;
3293145256Sjkoshy			break;
3294145256Sjkoshy		} else if (pm->pm_state != PMC_STATE_ALLOCATED &&
3295145256Sjkoshy		    pm->pm_state != PMC_STATE_STOPPED) {
3296145256Sjkoshy			error = EINVAL;
3297145256Sjkoshy			break;
3298145256Sjkoshy		}
3299145256Sjkoshy
3300145256Sjkoshy		/* lookup pid */
3301145256Sjkoshy		if ((p = pfind(a.pm_pid)) == NULL) {
3302145256Sjkoshy			error = ESRCH;
3303145256Sjkoshy			break;
3304145256Sjkoshy		}
3305145256Sjkoshy
3306145256Sjkoshy		/*
3307145256Sjkoshy		 * Ignore processes that are working on exiting.
3308145256Sjkoshy		 */
3309145256Sjkoshy		if (p->p_flag & P_WEXIT) {
3310145256Sjkoshy			error = ESRCH;
3311145256Sjkoshy			PROC_UNLOCK(p);	/* pfind() returns a locked process */
3312145256Sjkoshy			break;
3313145256Sjkoshy		}
3314145256Sjkoshy
3315145256Sjkoshy		/*
3316145256Sjkoshy		 * we are allowed to attach a PMC to a process if
3317145256Sjkoshy		 * we can debug it.
3318145256Sjkoshy		 */
3319145256Sjkoshy		error = p_candebug(curthread, p);
3320145256Sjkoshy
3321145256Sjkoshy		PROC_UNLOCK(p);
3322145256Sjkoshy
3323145256Sjkoshy		if (error == 0)
3324145256Sjkoshy			error = pmc_attach_process(p, pm);
3325145256Sjkoshy	}
3326145256Sjkoshy	break;
3327145256Sjkoshy
3328145256Sjkoshy
3329145256Sjkoshy	/*
3330145256Sjkoshy	 * Detach an attached PMC from a process.
3331145256Sjkoshy	 */
3332145256Sjkoshy
3333145256Sjkoshy	case PMC_OP_PMCDETACH:
3334145256Sjkoshy	{
3335145256Sjkoshy		struct pmc *pm;
3336145256Sjkoshy		struct proc *p;
3337145256Sjkoshy		struct pmc_op_pmcattach a;
3338145256Sjkoshy
3339145256Sjkoshy		if ((error = copyin(arg, &a, sizeof(a))) != 0)
3340145256Sjkoshy			break;
3341145256Sjkoshy
3342145256Sjkoshy		if (a.pm_pid < 0) {
3343145256Sjkoshy			error = EINVAL;
3344145256Sjkoshy			break;
3345145256Sjkoshy		} else if (a.pm_pid == 0)
3346145256Sjkoshy			a.pm_pid = td->td_proc->p_pid;
3347145256Sjkoshy
3348145256Sjkoshy		if ((error = pmc_find_pmc(a.pm_pmc, &pm)) != 0)
3349145256Sjkoshy			break;
3350145256Sjkoshy
3351145256Sjkoshy		if ((p = pfind(a.pm_pid)) == NULL) {
3352145256Sjkoshy			error = ESRCH;
3353145256Sjkoshy			break;
3354145256Sjkoshy		}
3355145256Sjkoshy
3356145256Sjkoshy		/*
3357145256Sjkoshy		 * Treat processes that are in the process of exiting
3358145256Sjkoshy		 * as if they were not present.
3359145256Sjkoshy		 */
3360145256Sjkoshy
3361145256Sjkoshy		if (p->p_flag & P_WEXIT)
3362145256Sjkoshy			error = ESRCH;
3363145256Sjkoshy
3364145256Sjkoshy		PROC_UNLOCK(p);	/* pfind() returns a locked process */
3365145256Sjkoshy
3366145256Sjkoshy		if (error == 0)
3367145256Sjkoshy			error = pmc_detach_process(p, pm);
3368145256Sjkoshy	}
3369145256Sjkoshy	break;
3370145256Sjkoshy
3371145256Sjkoshy
3372145256Sjkoshy	/*
3373147191Sjkoshy	 * Retrieve the MSR number associated with the counter
3374147191Sjkoshy	 * 'pmc_id'.  This allows processes to directly use RDPMC
3375147191Sjkoshy	 * instructions to read their PMCs, without the overhead of a
3376147191Sjkoshy	 * system call.
3377147191Sjkoshy	 */
3378147191Sjkoshy
3379147191Sjkoshy	case PMC_OP_PMCGETMSR:
3380147191Sjkoshy	{
3381184802Sjkoshy		int adjri, ri;
3382184802Sjkoshy		struct pmc *pm;
3383147191Sjkoshy		struct pmc_target *pt;
3384147191Sjkoshy		struct pmc_op_getmsr gm;
3385184802Sjkoshy		struct pmc_classdep *pcd;
3386147191Sjkoshy
3387147191Sjkoshy		PMC_DOWNGRADE_SX();
3388147191Sjkoshy
3389147191Sjkoshy		if ((error = copyin(arg, &gm, sizeof(gm))) != 0)
3390147191Sjkoshy			break;
3391147191Sjkoshy
3392147191Sjkoshy		if ((error = pmc_find_pmc(gm.pm_pmcid, &pm)) != 0)
3393147191Sjkoshy			break;
3394147191Sjkoshy
3395147191Sjkoshy		/*
3396147191Sjkoshy		 * The allocated PMC has to be a process virtual PMC,
3397147191Sjkoshy		 * i.e., of type MODE_T[CS].  Global PMCs can only be
3398147191Sjkoshy		 * read using the PMCREAD operation since they may be
3399147191Sjkoshy		 * allocated on a different CPU than the one we could
3400147191Sjkoshy		 * be running on at the time of the RDPMC instruction.
3401147191Sjkoshy		 *
3402147191Sjkoshy		 * The GETMSR operation is not allowed for PMCs that
3403147191Sjkoshy		 * are inherited across processes.
3404147191Sjkoshy		 */
3405147191Sjkoshy
3406147191Sjkoshy		if (!PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm)) ||
3407147191Sjkoshy		    (pm->pm_flags & PMC_F_DESCENDANTS)) {
3408147191Sjkoshy			error = EINVAL;
3409147191Sjkoshy			break;
3410147191Sjkoshy		}
3411147191Sjkoshy
3412147191Sjkoshy		/*
3413147191Sjkoshy		 * It only makes sense to use a RDPMC (or its
3414147191Sjkoshy		 * equivalent instruction on non-x86 architectures) on
3415147191Sjkoshy		 * a process that has allocated and attached a PMC to
3416147191Sjkoshy		 * itself.  Conversely the PMC is only allowed to have
3417147191Sjkoshy		 * one process attached to it -- its owner.
3418147191Sjkoshy		 */
3419147191Sjkoshy
3420147191Sjkoshy		if ((pt = LIST_FIRST(&pm->pm_targets)) == NULL ||
3421147191Sjkoshy		    LIST_NEXT(pt, pt_next) != NULL ||
3422147191Sjkoshy		    pt->pt_process->pp_proc != pm->pm_owner->po_owner) {
3423147191Sjkoshy			error = EINVAL;
3424147191Sjkoshy			break;
3425147191Sjkoshy		}
3426147191Sjkoshy
3427147191Sjkoshy		ri = PMC_TO_ROWINDEX(pm);
3428184802Sjkoshy		pcd = pmc_ri_to_classdep(md, ri, &adjri);
3429147191Sjkoshy
3430184802Sjkoshy		/* PMC class has no 'GETMSR' support */
3431184802Sjkoshy		if (pcd->pcd_get_msr == NULL) {
3432184802Sjkoshy			error = ENOSYS;
3433147191Sjkoshy			break;
3434184802Sjkoshy		}
3435147191Sjkoshy
3436184802Sjkoshy		if ((error = (*pcd->pcd_get_msr)(adjri, &gm.pm_msr)) < 0)
3437184802Sjkoshy			break;
3438184802Sjkoshy
3439147191Sjkoshy		if ((error = copyout(&gm, arg, sizeof(gm))) < 0)
3440147191Sjkoshy			break;
3441147191Sjkoshy
3442147191Sjkoshy		/*
3443147191Sjkoshy		 * Mark our process as using MSRs.  Update machine
3444147191Sjkoshy		 * state using a forced context switch.
3445147191Sjkoshy		 */
3446147191Sjkoshy
3447147191Sjkoshy		pt->pt_process->pp_flags |= PMC_PP_ENABLE_MSR_ACCESS;
3448147191Sjkoshy		pmc_force_context_switch();
3449147191Sjkoshy
3450147191Sjkoshy	}
3451147191Sjkoshy	break;
3452147191Sjkoshy
3453147191Sjkoshy	/*
3454145256Sjkoshy	 * Release an allocated PMC
3455145256Sjkoshy	 */
3456145256Sjkoshy
3457145256Sjkoshy	case PMC_OP_PMCRELEASE:
3458145256Sjkoshy	{
3459145256Sjkoshy		pmc_id_t pmcid;
3460145256Sjkoshy		struct pmc *pm;
3461145256Sjkoshy		struct pmc_owner *po;
3462145256Sjkoshy		struct pmc_op_simple sp;
3463145256Sjkoshy
3464145256Sjkoshy		/*
3465145256Sjkoshy		 * Find PMC pointer for the named PMC.
3466145256Sjkoshy		 *
3467145256Sjkoshy		 * Use pmc_release_pmc_descriptor() to switch off the
3468145256Sjkoshy		 * PMC, remove all its target threads, and remove the
3469145256Sjkoshy		 * PMC from its owner's list.
3470145256Sjkoshy		 *
3471145256Sjkoshy		 * Remove the owner record if this is the last PMC
3472145256Sjkoshy		 * owned.
3473145256Sjkoshy		 *
3474145256Sjkoshy		 * Free up space.
3475145256Sjkoshy		 */
3476145256Sjkoshy
3477145256Sjkoshy		if ((error = copyin(arg, &sp, sizeof(sp))) != 0)
3478145256Sjkoshy			break;
3479145256Sjkoshy
3480145256Sjkoshy		pmcid = sp.pm_pmcid;
3481145256Sjkoshy
3482145256Sjkoshy		if ((error = pmc_find_pmc(pmcid, &pm)) != 0)
3483145256Sjkoshy			break;
3484145256Sjkoshy
3485145256Sjkoshy		po = pm->pm_owner;
3486145256Sjkoshy		pmc_release_pmc_descriptor(pm);
3487145256Sjkoshy		pmc_maybe_remove_owner(po);
3488145256Sjkoshy
3489184205Sdes		free(pm, M_PMC);
3490145256Sjkoshy	}
3491145256Sjkoshy	break;
3492145256Sjkoshy
3493145256Sjkoshy
3494145256Sjkoshy	/*
3495145256Sjkoshy	 * Read and/or write a PMC.
3496145256Sjkoshy	 */
3497145256Sjkoshy
3498145256Sjkoshy	case PMC_OP_PMCRW:
3499145256Sjkoshy	{
3500184802Sjkoshy		int adjri;
3501184802Sjkoshy		struct pmc *pm;
3502145256Sjkoshy		uint32_t cpu, ri;
3503184802Sjkoshy		pmc_value_t oldvalue;
3504184802Sjkoshy		struct pmc_binding pb;
3505184802Sjkoshy		struct pmc_op_pmcrw prw;
3506184802Sjkoshy		struct pmc_classdep *pcd;
3507145256Sjkoshy		struct pmc_op_pmcrw *pprw;
3508145256Sjkoshy
3509145256Sjkoshy		PMC_DOWNGRADE_SX();
3510145256Sjkoshy
3511145256Sjkoshy		if ((error = copyin(arg, &prw, sizeof(prw))) != 0)
3512145256Sjkoshy			break;
3513145256Sjkoshy
3514145301Simp		ri = 0;
3515145256Sjkoshy		PMCDBG(PMC,OPS,1, "rw id=%d flags=0x%x", prw.pm_pmcid,
3516145256Sjkoshy		    prw.pm_flags);
3517145256Sjkoshy
3518145256Sjkoshy		/* must have at least one flag set */
3519145256Sjkoshy		if ((prw.pm_flags & (PMC_F_OLDVALUE|PMC_F_NEWVALUE)) == 0) {
3520145256Sjkoshy			error = EINVAL;
3521145256Sjkoshy			break;
3522145256Sjkoshy		}
3523145256Sjkoshy
3524145256Sjkoshy		/* locate pmc descriptor */
3525145256Sjkoshy		if ((error = pmc_find_pmc(prw.pm_pmcid, &pm)) != 0)
3526145256Sjkoshy			break;
3527145256Sjkoshy
3528145256Sjkoshy		/* Can't read a PMC that hasn't been started. */
3529145256Sjkoshy		if (pm->pm_state != PMC_STATE_ALLOCATED &&
3530145256Sjkoshy		    pm->pm_state != PMC_STATE_STOPPED &&
3531145256Sjkoshy		    pm->pm_state != PMC_STATE_RUNNING) {
3532145256Sjkoshy			error = EINVAL;
3533145256Sjkoshy			break;
3534145256Sjkoshy		}
3535145256Sjkoshy
3536145256Sjkoshy		/* writing a new value is allowed only for 'STOPPED' pmcs */
3537145256Sjkoshy		if (pm->pm_state == PMC_STATE_RUNNING &&
3538145256Sjkoshy		    (prw.pm_flags & PMC_F_NEWVALUE)) {
3539145256Sjkoshy			error = EBUSY;
3540145256Sjkoshy			break;
3541145256Sjkoshy		}
3542145256Sjkoshy
3543145774Sjkoshy		if (PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm))) {
3544145256Sjkoshy
3545145774Sjkoshy			/*
3546145774Sjkoshy			 * If this PMC is attached to its owner (i.e.,
3547145774Sjkoshy			 * the process requesting this operation) and
3548145774Sjkoshy			 * is running, then attempt to get an
3549145774Sjkoshy			 * upto-date reading from hardware for a READ.
3550145774Sjkoshy			 * Writes are only allowed when the PMC is
3551145774Sjkoshy			 * stopped, so only update the saved value
3552145774Sjkoshy			 * field.
3553145774Sjkoshy			 *
3554145774Sjkoshy			 * If the PMC is not running, or is not
3555145774Sjkoshy			 * attached to its owner, read/write to the
3556145774Sjkoshy			 * savedvalue field.
3557145774Sjkoshy			 */
3558145774Sjkoshy
3559145774Sjkoshy			ri = PMC_TO_ROWINDEX(pm);
3560184802Sjkoshy			pcd = pmc_ri_to_classdep(md, ri, &adjri);
3561145774Sjkoshy
3562145256Sjkoshy			mtx_pool_lock_spin(pmc_mtxpool, pm);
3563145774Sjkoshy			cpu = curthread->td_oncpu;
3564145774Sjkoshy
3565145774Sjkoshy			if (prw.pm_flags & PMC_F_OLDVALUE) {
3566145774Sjkoshy				if ((pm->pm_flags & PMC_F_ATTACHED_TO_OWNER) &&
3567145774Sjkoshy				    (pm->pm_state == PMC_STATE_RUNNING))
3568184802Sjkoshy					error = (*pcd->pcd_read_pmc)(cpu, adjri,
3569145774Sjkoshy					    &oldvalue);
3570145774Sjkoshy				else
3571145774Sjkoshy					oldvalue = pm->pm_gv.pm_savedvalue;
3572145774Sjkoshy			}
3573145256Sjkoshy			if (prw.pm_flags & PMC_F_NEWVALUE)
3574145256Sjkoshy				pm->pm_gv.pm_savedvalue = prw.pm_value;
3575145774Sjkoshy
3576145256Sjkoshy			mtx_pool_unlock_spin(pmc_mtxpool, pm);
3577145256Sjkoshy
3578145256Sjkoshy		} else { /* System mode PMCs */
3579145774Sjkoshy			cpu = PMC_TO_CPU(pm);
3580145774Sjkoshy			ri  = PMC_TO_ROWINDEX(pm);
3581184802Sjkoshy			pcd = pmc_ri_to_classdep(md, ri, &adjri);
3582145256Sjkoshy
3583183266Sjkoshy			if (!pmc_cpu_is_active(cpu)) {
3584145256Sjkoshy				error = ENXIO;
3585145256Sjkoshy				break;
3586145256Sjkoshy			}
3587145256Sjkoshy
3588145256Sjkoshy			/* move this thread to CPU 'cpu' */
3589145256Sjkoshy			pmc_save_cpu_binding(&pb);
3590145256Sjkoshy			pmc_select_cpu(cpu);
3591145256Sjkoshy
3592145774Sjkoshy			critical_enter();
3593145256Sjkoshy			/* save old value */
3594145256Sjkoshy			if (prw.pm_flags & PMC_F_OLDVALUE)
3595184802Sjkoshy				if ((error = (*pcd->pcd_read_pmc)(cpu, adjri,
3596145256Sjkoshy					 &oldvalue)))
3597145256Sjkoshy					goto error;
3598145256Sjkoshy			/* write out new value */
3599145256Sjkoshy			if (prw.pm_flags & PMC_F_NEWVALUE)
3600184802Sjkoshy				error = (*pcd->pcd_write_pmc)(cpu, adjri,
3601145256Sjkoshy				    prw.pm_value);
3602145256Sjkoshy		error:
3603145774Sjkoshy			critical_exit();
3604145256Sjkoshy			pmc_restore_cpu_binding(&pb);
3605145256Sjkoshy			if (error)
3606145256Sjkoshy				break;
3607145256Sjkoshy		}
3608145256Sjkoshy
3609145256Sjkoshy		pprw = (struct pmc_op_pmcrw *) arg;
3610145256Sjkoshy
3611153110Sru#ifdef	DEBUG
3612145256Sjkoshy		if (prw.pm_flags & PMC_F_NEWVALUE)
3613145256Sjkoshy			PMCDBG(PMC,OPS,2, "rw id=%d new %jx -> old %jx",
3614145256Sjkoshy			    ri, prw.pm_value, oldvalue);
3615156778Sjkoshy		else if (prw.pm_flags & PMC_F_OLDVALUE)
3616145256Sjkoshy			PMCDBG(PMC,OPS,2, "rw id=%d -> old %jx", ri, oldvalue);
3617145256Sjkoshy#endif
3618145256Sjkoshy
3619145256Sjkoshy		/* return old value if requested */
3620145256Sjkoshy		if (prw.pm_flags & PMC_F_OLDVALUE)
3621145256Sjkoshy			if ((error = copyout(&oldvalue, &pprw->pm_value,
3622145256Sjkoshy				 sizeof(prw.pm_value))))
3623145256Sjkoshy				break;
3624145256Sjkoshy
3625145256Sjkoshy	}
3626145256Sjkoshy	break;
3627145256Sjkoshy
3628145256Sjkoshy
3629145256Sjkoshy	/*
3630145256Sjkoshy	 * Set the sampling rate for a sampling mode PMC and the
3631145256Sjkoshy	 * initial count for a counting mode PMC.
3632145256Sjkoshy	 */
3633145256Sjkoshy
3634145256Sjkoshy	case PMC_OP_PMCSETCOUNT:
3635145256Sjkoshy	{
3636145256Sjkoshy		struct pmc *pm;
3637145256Sjkoshy		struct pmc_op_pmcsetcount sc;
3638145256Sjkoshy
3639145256Sjkoshy		PMC_DOWNGRADE_SX();
3640145256Sjkoshy
3641145256Sjkoshy		if ((error = copyin(arg, &sc, sizeof(sc))) != 0)
3642145256Sjkoshy			break;
3643145256Sjkoshy
3644145256Sjkoshy		if ((error = pmc_find_pmc(sc.pm_pmcid, &pm)) != 0)
3645145256Sjkoshy			break;
3646145256Sjkoshy
3647145256Sjkoshy		if (pm->pm_state == PMC_STATE_RUNNING) {
3648145256Sjkoshy			error = EBUSY;
3649145256Sjkoshy			break;
3650145256Sjkoshy		}
3651145256Sjkoshy
3652145774Sjkoshy		if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
3653145256Sjkoshy			pm->pm_sc.pm_reloadcount = sc.pm_count;
3654145256Sjkoshy		else
3655145256Sjkoshy			pm->pm_sc.pm_initial = sc.pm_count;
3656145256Sjkoshy	}
3657145256Sjkoshy	break;
3658145256Sjkoshy
3659145256Sjkoshy
3660145256Sjkoshy	/*
3661145256Sjkoshy	 * Start a PMC.
3662145256Sjkoshy	 */
3663145256Sjkoshy
3664145256Sjkoshy	case PMC_OP_PMCSTART:
3665145256Sjkoshy	{
3666145256Sjkoshy		pmc_id_t pmcid;
3667145256Sjkoshy		struct pmc *pm;
3668145256Sjkoshy		struct pmc_op_simple sp;
3669145256Sjkoshy
3670145256Sjkoshy		sx_assert(&pmc_sx, SX_XLOCKED);
3671145256Sjkoshy
3672145256Sjkoshy		if ((error = copyin(arg, &sp, sizeof(sp))) != 0)
3673145256Sjkoshy			break;
3674145256Sjkoshy
3675145256Sjkoshy		pmcid = sp.pm_pmcid;
3676145256Sjkoshy
3677145256Sjkoshy		if ((error = pmc_find_pmc(pmcid, &pm)) != 0)
3678145256Sjkoshy			break;
3679145256Sjkoshy
3680145774Sjkoshy		KASSERT(pmcid == pm->pm_id,
3681145774Sjkoshy		    ("[pmc,%d] pmcid %x != id %x", __LINE__,
3682145774Sjkoshy			pm->pm_id, pmcid));
3683145256Sjkoshy
3684145256Sjkoshy		if (pm->pm_state == PMC_STATE_RUNNING) /* already running */
3685145256Sjkoshy			break;
3686145256Sjkoshy		else if (pm->pm_state != PMC_STATE_STOPPED &&
3687145256Sjkoshy		    pm->pm_state != PMC_STATE_ALLOCATED) {
3688145256Sjkoshy			error = EINVAL;
3689145256Sjkoshy			break;
3690145256Sjkoshy		}
3691145256Sjkoshy
3692145256Sjkoshy		error = pmc_start(pm);
3693145256Sjkoshy	}
3694145256Sjkoshy	break;
3695145256Sjkoshy
3696145256Sjkoshy
3697145256Sjkoshy	/*
3698145256Sjkoshy	 * Stop a PMC.
3699145256Sjkoshy	 */
3700145256Sjkoshy
3701145256Sjkoshy	case PMC_OP_PMCSTOP:
3702145256Sjkoshy	{
3703145256Sjkoshy		pmc_id_t pmcid;
3704145256Sjkoshy		struct pmc *pm;
3705145256Sjkoshy		struct pmc_op_simple sp;
3706145256Sjkoshy
3707145256Sjkoshy		PMC_DOWNGRADE_SX();
3708145256Sjkoshy
3709145256Sjkoshy		if ((error = copyin(arg, &sp, sizeof(sp))) != 0)
3710145256Sjkoshy			break;
3711145256Sjkoshy
3712145256Sjkoshy		pmcid = sp.pm_pmcid;
3713145256Sjkoshy
3714145256Sjkoshy		/*
3715145256Sjkoshy		 * Mark the PMC as inactive and invoke the MD stop
3716145256Sjkoshy		 * routines if needed.
3717145256Sjkoshy		 */
3718145256Sjkoshy
3719145256Sjkoshy		if ((error = pmc_find_pmc(pmcid, &pm)) != 0)
3720145256Sjkoshy			break;
3721145256Sjkoshy
3722145774Sjkoshy		KASSERT(pmcid == pm->pm_id,
3723145774Sjkoshy		    ("[pmc,%d] pmc id %x != pmcid %x", __LINE__,
3724145774Sjkoshy			pm->pm_id, pmcid));
3725145256Sjkoshy
3726145256Sjkoshy		if (pm->pm_state == PMC_STATE_STOPPED) /* already stopped */
3727145256Sjkoshy			break;
3728145256Sjkoshy		else if (pm->pm_state != PMC_STATE_RUNNING) {
3729145256Sjkoshy			error = EINVAL;
3730145256Sjkoshy			break;
3731145256Sjkoshy		}
3732145256Sjkoshy
3733145256Sjkoshy		error = pmc_stop(pm);
3734145256Sjkoshy	}
3735145256Sjkoshy	break;
3736145256Sjkoshy
3737145256Sjkoshy
3738145256Sjkoshy	/*
3739147867Sjkoshy	 * Write a user supplied value to the log file.
3740145256Sjkoshy	 */
3741145256Sjkoshy
3742145256Sjkoshy	case PMC_OP_WRITELOG:
3743145256Sjkoshy	{
3744147191Sjkoshy		struct pmc_op_writelog wl;
3745147191Sjkoshy		struct pmc_owner *po;
3746145256Sjkoshy
3747145256Sjkoshy		PMC_DOWNGRADE_SX();
3748145256Sjkoshy
3749147191Sjkoshy		if ((error = copyin(arg, &wl, sizeof(wl))) != 0)
3750145256Sjkoshy			break;
3751145256Sjkoshy
3752147191Sjkoshy		if ((po = pmc_find_owner_descriptor(td->td_proc)) == NULL) {
3753145256Sjkoshy			error = EINVAL;
3754145256Sjkoshy			break;
3755145256Sjkoshy		}
3756145256Sjkoshy
3757147191Sjkoshy		if ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0) {
3758145774Sjkoshy			error = EINVAL;
3759145774Sjkoshy			break;
3760145774Sjkoshy		}
3761145774Sjkoshy
3762147191Sjkoshy		error = pmclog_process_userlog(po, &wl);
3763145256Sjkoshy	}
3764145256Sjkoshy	break;
3765145256Sjkoshy
3766147191Sjkoshy
3767145256Sjkoshy	default:
3768145256Sjkoshy		error = EINVAL;
3769145256Sjkoshy		break;
3770145256Sjkoshy	}
3771145256Sjkoshy
3772145256Sjkoshy	if (is_sx_downgraded)
3773145256Sjkoshy		sx_sunlock(&pmc_sx);
3774145256Sjkoshy	else
3775145256Sjkoshy		sx_xunlock(&pmc_sx);
3776145256Sjkoshy
3777145256Sjkoshy	if (error)
3778145256Sjkoshy		atomic_add_int(&pmc_stats.pm_syscall_errors, 1);
3779145256Sjkoshy
3780147191Sjkoshy	PICKUP_GIANT();
3781147191Sjkoshy
3782145256Sjkoshy	return error;
3783145256Sjkoshy}
3784145256Sjkoshy
3785145256Sjkoshy/*
3786145256Sjkoshy * Helper functions
3787145256Sjkoshy */
3788145256Sjkoshy
3789147191Sjkoshy
3790145256Sjkoshy/*
3791174395Sjkoshy * Mark the thread as needing callchain capture and post an AST.  The
3792174395Sjkoshy * actual callchain capture will be done in a context where it is safe
3793174395Sjkoshy * to take page faults.
3794174395Sjkoshy */
3795174395Sjkoshy
3796174395Sjkoshystatic void
3797174395Sjkoshypmc_post_callchain_ast(void)
3798174395Sjkoshy{
3799174395Sjkoshy	struct thread *td;
3800174395Sjkoshy
3801174395Sjkoshy	td = curthread;
3802174395Sjkoshy
3803174395Sjkoshy	/*
3804174395Sjkoshy	 * Mark this thread as needing processing in ast().
3805174395Sjkoshy	 * td->td_pflags will be safe to touch as the process was in
3806174395Sjkoshy	 * user space when it was interrupted.
3807174395Sjkoshy	 */
3808174395Sjkoshy	td->td_pflags |= TDP_CALLCHAIN;
3809174395Sjkoshy
3810174395Sjkoshy	/*
3811174395Sjkoshy	 * Again, since we've entered this function directly from
3812174395Sjkoshy	 * userland, `td' is guaranteed to be not locked by this CPU,
3813174395Sjkoshy	 * so its safe to try acquire the thread lock even though we
3814174395Sjkoshy	 * are executing in an NMI context.  We need to acquire this
3815174395Sjkoshy	 * lock before touching `td_flags' because other CPUs may be
3816174395Sjkoshy	 * in the process of touching this field.
3817174395Sjkoshy	 */
3818174395Sjkoshy	thread_lock(td);
3819174395Sjkoshy	td->td_flags |= TDF_ASTPENDING;
3820174395Sjkoshy	thread_unlock(td);
3821174395Sjkoshy
3822174395Sjkoshy	return;
3823174395Sjkoshy}
3824174395Sjkoshy
3825174395Sjkoshy/*
3826147191Sjkoshy * Interrupt processing.
3827147191Sjkoshy *
3828174395Sjkoshy * Find a free slot in the per-cpu array of samples and capture the
3829174395Sjkoshy * current callchain there.  If a sample was successfully added, a bit
3830174395Sjkoshy * is set in mask 'pmc_cpumask' denoting that the DO_SAMPLES hook
3831174395Sjkoshy * needs to be invoked from the clock handler.
3832147191Sjkoshy *
3833147191Sjkoshy * This function is meant to be called from an NMI handler.  It cannot
3834147191Sjkoshy * use any of the locking primitives supplied by the OS.
3835145256Sjkoshy */
3836145256Sjkoshy
3837147191Sjkoshyint
3838174395Sjkoshypmc_process_interrupt(int cpu, struct pmc *pm, struct trapframe *tf,
3839174395Sjkoshy    int inuserspace)
3840145256Sjkoshy{
3841174395Sjkoshy	int error, callchaindepth;
3842147191Sjkoshy	struct thread *td;
3843147191Sjkoshy	struct pmc_sample *ps;
3844147191Sjkoshy	struct pmc_samplebuffer *psb;
3845145256Sjkoshy
3846147191Sjkoshy	error = 0;
3847145256Sjkoshy
3848174395Sjkoshy	/*
3849174395Sjkoshy	 * Allocate space for a sample buffer.
3850174395Sjkoshy	 */
3851147191Sjkoshy	psb = pmc_pcpu[cpu]->pc_sb;
3852145256Sjkoshy
3853147191Sjkoshy	ps = psb->ps_write;
3854174395Sjkoshy	if (ps->ps_nsamples) {	/* in use, reader hasn't caught up */
3855147867Sjkoshy		pm->pm_stalled = 1;
3856147191Sjkoshy		atomic_add_int(&pmc_stats.pm_intr_bufferfull, 1);
3857174395Sjkoshy		PMCDBG(SAM,INT,1,"(spc) cpu=%d pm=%p tf=%p um=%d wr=%d rd=%d",
3858174395Sjkoshy		    cpu, pm, (void *) tf, inuserspace,
3859147191Sjkoshy		    (int) (psb->ps_write - psb->ps_samples),
3860147191Sjkoshy		    (int) (psb->ps_read - psb->ps_samples));
3861147191Sjkoshy		error = ENOMEM;
3862147191Sjkoshy		goto done;
3863147191Sjkoshy	}
3864145256Sjkoshy
3865174395Sjkoshy
3866174395Sjkoshy	/* Fill in entry. */
3867174395Sjkoshy	PMCDBG(SAM,INT,1,"cpu=%d pm=%p tf=%p um=%d wr=%d rd=%d", cpu, pm,
3868174395Sjkoshy	    (void *) tf, inuserspace,
3869147191Sjkoshy	    (int) (psb->ps_write - psb->ps_samples),
3870147191Sjkoshy	    (int) (psb->ps_read - psb->ps_samples));
3871145256Sjkoshy
3872174395Sjkoshy	atomic_add_rel_32(&pm->pm_runcount, 1);	/* hold onto PMC */
3873147191Sjkoshy	ps->ps_pmc = pm;
3874147191Sjkoshy	if ((td = curthread) && td->td_proc)
3875147191Sjkoshy		ps->ps_pid = td->td_proc->p_pid;
3876147191Sjkoshy	else
3877147191Sjkoshy		ps->ps_pid = -1;
3878174395Sjkoshy	ps->ps_cpu = cpu;
3879174395Sjkoshy	ps->ps_flags = inuserspace ? PMC_CC_F_USERSPACE : 0;
3880145256Sjkoshy
3881174395Sjkoshy	callchaindepth = (pm->pm_flags & PMC_F_CALLCHAIN) ?
3882174395Sjkoshy	    pmc_callchaindepth : 1;
3883174395Sjkoshy
3884174395Sjkoshy	if (callchaindepth == 1)
3885174395Sjkoshy		ps->ps_pc[0] = PMC_TRAPFRAME_TO_PC(tf);
3886174395Sjkoshy	else {
3887174395Sjkoshy		/*
3888174395Sjkoshy		 * Kernel stack traversals can be done immediately,
3889174395Sjkoshy		 * while we defer to an AST for user space traversals.
3890174395Sjkoshy		 */
3891174395Sjkoshy		if (!inuserspace)
3892174395Sjkoshy			callchaindepth =
3893174395Sjkoshy			    pmc_save_kernel_callchain(ps->ps_pc,
3894174395Sjkoshy				callchaindepth, tf);
3895174395Sjkoshy		else {
3896174395Sjkoshy			pmc_post_callchain_ast();
3897174395Sjkoshy			callchaindepth = PMC_SAMPLE_INUSE;
3898174395Sjkoshy		}
3899174395Sjkoshy	}
3900174395Sjkoshy
3901174395Sjkoshy	ps->ps_nsamples = callchaindepth;	/* mark entry as in use */
3902174395Sjkoshy
3903147191Sjkoshy	/* increment write pointer, modulo ring buffer size */
3904147191Sjkoshy	ps++;
3905147191Sjkoshy	if (ps == psb->ps_fence)
3906147191Sjkoshy		psb->ps_write = psb->ps_samples;
3907147191Sjkoshy	else
3908147191Sjkoshy		psb->ps_write = ps;
3909145256Sjkoshy
3910147191Sjkoshy done:
3911147191Sjkoshy	/* mark CPU as needing processing */
3912147191Sjkoshy	atomic_set_rel_int(&pmc_cpumask, (1 << cpu));
3913147191Sjkoshy
3914174395Sjkoshy	return (error);
3915145256Sjkoshy}
3916145256Sjkoshy
3917174395Sjkoshy/*
3918174395Sjkoshy * Capture a user call chain.  This function will be called from ast()
3919174395Sjkoshy * before control returns to userland and before the process gets
3920174395Sjkoshy * rescheduled.
3921174395Sjkoshy */
3922147191Sjkoshy
3923174395Sjkoshystatic void
3924174395Sjkoshypmc_capture_user_callchain(int cpu, struct trapframe *tf)
3925174395Sjkoshy{
3926174395Sjkoshy	int i;
3927174395Sjkoshy	struct pmc *pm;
3928174395Sjkoshy	struct pmc_sample *ps;
3929174395Sjkoshy	struct pmc_samplebuffer *psb;
3930174395Sjkoshy
3931174395Sjkoshy	psb = pmc_pcpu[cpu]->pc_sb;
3932174395Sjkoshy
3933174395Sjkoshy	/*
3934174395Sjkoshy	 * Iterate through all deferred callchain requests.
3935174395Sjkoshy	 */
3936174395Sjkoshy
3937174395Sjkoshy	for (i = 0; i < pmc_nsamples; i++) {
3938174395Sjkoshy
3939174395Sjkoshy		ps = &psb->ps_samples[i];
3940174395Sjkoshy		if (ps->ps_nsamples != PMC_SAMPLE_INUSE)
3941174395Sjkoshy			continue;
3942174395Sjkoshy
3943174395Sjkoshy		pm = ps->ps_pmc;
3944174395Sjkoshy
3945174395Sjkoshy		KASSERT(pm->pm_flags & PMC_F_CALLCHAIN,
3946174395Sjkoshy		    ("[pmc,%d] Retrieving callchain for PMC that doesn't "
3947174395Sjkoshy			"want it", __LINE__));
3948174395Sjkoshy
3949174395Sjkoshy		/*
3950174395Sjkoshy		 * Retrieve the callchain and mark the sample buffer
3951174395Sjkoshy		 * as 'processable' by the timer tick sweep code.
3952174395Sjkoshy		 */
3953174395Sjkoshy		ps->ps_nsamples = pmc_save_user_callchain(ps->ps_pc,
3954174395Sjkoshy		    pmc_callchaindepth, tf);
3955174395Sjkoshy	}
3956174395Sjkoshy
3957174395Sjkoshy	return;
3958174395Sjkoshy}
3959174395Sjkoshy
3960174395Sjkoshy
3961145256Sjkoshy/*
3962147191Sjkoshy * Process saved PC samples.
3963145256Sjkoshy */
3964145256Sjkoshy
3965145256Sjkoshystatic void
3966147191Sjkoshypmc_process_samples(int cpu)
3967145256Sjkoshy{
3968147191Sjkoshy	struct pmc *pm;
3969185363Sjkoshy	int adjri, n;
3970147191Sjkoshy	struct thread *td;
3971147191Sjkoshy	struct pmc_owner *po;
3972147191Sjkoshy	struct pmc_sample *ps;
3973184802Sjkoshy	struct pmc_classdep *pcd;
3974147191Sjkoshy	struct pmc_samplebuffer *psb;
3975145256Sjkoshy
3976147191Sjkoshy	KASSERT(PCPU_GET(cpuid) == cpu,
3977147191Sjkoshy	    ("[pmc,%d] not on the correct CPU pcpu=%d cpu=%d", __LINE__,
3978147191Sjkoshy		PCPU_GET(cpuid), cpu));
3979145256Sjkoshy
3980147191Sjkoshy	psb = pmc_pcpu[cpu]->pc_sb;
3981147191Sjkoshy
3982147191Sjkoshy	for (n = 0; n < pmc_nsamples; n++) { /* bound on #iterations */
3983147191Sjkoshy
3984147191Sjkoshy		ps = psb->ps_read;
3985174395Sjkoshy		if (ps->ps_nsamples == PMC_SAMPLE_FREE)
3986147191Sjkoshy			break;
3987174395Sjkoshy		if (ps->ps_nsamples == PMC_SAMPLE_INUSE) {
3988174395Sjkoshy			/* Need a rescan at a later time. */
3989174395Sjkoshy			atomic_set_rel_int(&pmc_cpumask, (1 << cpu));
3990174395Sjkoshy			break;
3991174395Sjkoshy		}
3992147191Sjkoshy
3993147191Sjkoshy		pm = ps->ps_pmc;
3994147191Sjkoshy		po = pm->pm_owner;
3995147191Sjkoshy
3996147191Sjkoshy		KASSERT(PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)),
3997147191Sjkoshy		    ("[pmc,%d] pmc=%p non-sampling mode=%d", __LINE__,
3998147191Sjkoshy			pm, PMC_TO_MODE(pm)));
3999147191Sjkoshy
4000147191Sjkoshy		/* Ignore PMCs that have been switched off */
4001147191Sjkoshy		if (pm->pm_state != PMC_STATE_RUNNING)
4002147191Sjkoshy			goto entrydone;
4003147191Sjkoshy
4004174395Sjkoshy		PMCDBG(SAM,OPS,1,"cpu=%d pm=%p n=%d fl=%x wr=%d rd=%d", cpu,
4005174395Sjkoshy		    pm, ps->ps_nsamples, ps->ps_flags,
4006147191Sjkoshy		    (int) (psb->ps_write - psb->ps_samples),
4007147191Sjkoshy		    (int) (psb->ps_read - psb->ps_samples));
4008147191Sjkoshy
4009147191Sjkoshy		/*
4010147191Sjkoshy		 * If this is a process-mode PMC that is attached to
4011147191Sjkoshy		 * its owner, and if the PC is in user mode, update
4012147191Sjkoshy		 * profiling statistics like timer-based profiling
4013147191Sjkoshy		 * would have done.
4014147191Sjkoshy		 */
4015147191Sjkoshy		if (pm->pm_flags & PMC_F_ATTACHED_TO_OWNER) {
4016174395Sjkoshy			if (ps->ps_flags & PMC_CC_F_USERSPACE) {
4017147191Sjkoshy				td = FIRST_THREAD_IN_PROC(po->po_owner);
4018174395Sjkoshy				addupc_intr(td, ps->ps_pc[0], 1);
4019147191Sjkoshy			}
4020147191Sjkoshy			goto entrydone;
4021147191Sjkoshy		}
4022147191Sjkoshy
4023147191Sjkoshy		/*
4024147191Sjkoshy		 * Otherwise, this is either a sampling mode PMC that
4025147191Sjkoshy		 * is attached to a different process than its owner,
4026147191Sjkoshy		 * or a system-wide sampling PMC.  Dispatch a log
4027147191Sjkoshy		 * entry to the PMC's owner process.
4028147191Sjkoshy		 */
4029147191Sjkoshy
4030174395Sjkoshy		pmclog_process_callchain(pm, ps);
4031147191Sjkoshy
4032147191Sjkoshy	entrydone:
4033174395Sjkoshy		ps->ps_nsamples = 0;	/* mark entry as free */
4034147191Sjkoshy		atomic_subtract_rel_32(&pm->pm_runcount, 1);
4035147191Sjkoshy
4036147191Sjkoshy		/* increment read pointer, modulo sample size */
4037147191Sjkoshy		if (++ps == psb->ps_fence)
4038147191Sjkoshy			psb->ps_read = psb->ps_samples;
4039147191Sjkoshy		else
4040147191Sjkoshy			psb->ps_read = ps;
4041147191Sjkoshy	}
4042147191Sjkoshy
4043147191Sjkoshy	atomic_add_int(&pmc_stats.pm_log_sweeps, 1);
4044147191Sjkoshy
4045147191Sjkoshy	/* Do not re-enable stalled PMCs if we failed to process any samples */
4046147191Sjkoshy	if (n == 0)
4047147191Sjkoshy		return;
4048147191Sjkoshy
4049147191Sjkoshy	/*
4050147191Sjkoshy	 * Restart any stalled sampling PMCs on this CPU.
4051147191Sjkoshy	 *
4052147867Sjkoshy	 * If the NMI handler sets the pm_stalled field of a PMC after
4053147867Sjkoshy	 * the check below, we'll end up processing the stalled PMC at
4054147867Sjkoshy	 * the next hardclock tick.
4055147191Sjkoshy	 */
4056147191Sjkoshy	for (n = 0; n < md->pmd_npmc; n++) {
4057184802Sjkoshy		pcd = pmc_ri_to_classdep(md, n, &adjri);
4058184802Sjkoshy		KASSERT(pcd != NULL,
4059184802Sjkoshy		    ("[pmc,%d] null pcd ri=%d", __LINE__, n));
4060184802Sjkoshy		(void) (*pcd->pcd_get_config)(cpu,adjri,&pm);
4061184802Sjkoshy
4062147191Sjkoshy		if (pm == NULL ||			 /* !cfg'ed */
4063147191Sjkoshy		    pm->pm_state != PMC_STATE_RUNNING || /* !active */
4064147191Sjkoshy		    !PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)) || /* !sampling */
4065147867Sjkoshy		    pm->pm_stalled == 0) /* !stalled */
4066147191Sjkoshy			continue;
4067147191Sjkoshy
4068147867Sjkoshy		pm->pm_stalled = 0;
4069184802Sjkoshy		(*pcd->pcd_start_pmc)(cpu, adjri);
4070147191Sjkoshy	}
4071145256Sjkoshy}
4072145256Sjkoshy
4073145256Sjkoshy/*
4074145256Sjkoshy * Event handlers.
4075145256Sjkoshy */
4076145256Sjkoshy
4077145256Sjkoshy/*
4078145256Sjkoshy * Handle a process exit.
4079145256Sjkoshy *
4080147191Sjkoshy * Remove this process from all hash tables.  If this process
4081147191Sjkoshy * owned any PMCs, turn off those PMCs and deallocate them,
4082147191Sjkoshy * removing any associations with target processes.
4083147191Sjkoshy *
4084147191Sjkoshy * This function will be called by the last 'thread' of a
4085147191Sjkoshy * process.
4086147191Sjkoshy *
4087145256Sjkoshy * XXX This eventhandler gets called early in the exit process.
4088145256Sjkoshy * Consider using a 'hook' invocation from thread_exit() or equivalent
4089145256Sjkoshy * spot.  Another negative is that kse_exit doesn't seem to call
4090145256Sjkoshy * exit1() [??].
4091147191Sjkoshy *
4092145256Sjkoshy */
4093145256Sjkoshy
4094145256Sjkoshystatic void
4095145256Sjkoshypmc_process_exit(void *arg __unused, struct proc *p)
4096145256Sjkoshy{
4097184802Sjkoshy	struct pmc *pm;
4098184802Sjkoshy	int adjri, cpu;
4099184802Sjkoshy	unsigned int ri;
4100145256Sjkoshy	int is_using_hwpmcs;
4101184802Sjkoshy	struct pmc_owner *po;
4102147191Sjkoshy	struct pmc_process *pp;
4103184802Sjkoshy	struct pmc_classdep *pcd;
4104147191Sjkoshy	pmc_value_t newvalue, tmp;
4105145256Sjkoshy
4106145256Sjkoshy	PROC_LOCK(p);
4107145256Sjkoshy	is_using_hwpmcs = p->p_flag & P_HWPMC;
4108145256Sjkoshy	PROC_UNLOCK(p);
4109145256Sjkoshy
4110147191Sjkoshy	/*
4111147191Sjkoshy	 * Log a sysexit event to all SS PMC owners.
4112147191Sjkoshy	 */
4113147191Sjkoshy	LIST_FOREACH(po, &pmc_ss_owners, po_ssnext)
4114147191Sjkoshy	    if (po->po_flags & PMC_PO_OWNS_LOGFILE)
4115147191Sjkoshy		    pmclog_process_sysexit(po, p->p_pid);
4116145256Sjkoshy
4117147191Sjkoshy	if (!is_using_hwpmcs)
4118147191Sjkoshy		return;
4119147191Sjkoshy
4120147191Sjkoshy	PMC_GET_SX_XLOCK();
4121147191Sjkoshy	PMCDBG(PRC,EXT,1,"process-exit proc=%p (%d, %s)", p, p->p_pid,
4122147191Sjkoshy	    p->p_comm);
4123147191Sjkoshy
4124147191Sjkoshy	/*
4125147191Sjkoshy	 * Since this code is invoked by the last thread in an exiting
4126147191Sjkoshy	 * process, we would have context switched IN at some prior
4127147191Sjkoshy	 * point.  However, with PREEMPTION, kernel mode context
4128147191Sjkoshy	 * switches may happen any time, so we want to disable a
4129147191Sjkoshy	 * context switch OUT till we get any PMCs targetting this
4130147191Sjkoshy	 * process off the hardware.
4131147191Sjkoshy	 *
4132147191Sjkoshy	 * We also need to atomically remove this process'
4133147191Sjkoshy	 * entry from our target process hash table, using
4134147191Sjkoshy	 * PMC_FLAG_REMOVE.
4135147191Sjkoshy	 */
4136147191Sjkoshy	PMCDBG(PRC,EXT,1, "process-exit proc=%p (%d, %s)", p, p->p_pid,
4137147191Sjkoshy	    p->p_comm);
4138147191Sjkoshy
4139147191Sjkoshy	critical_enter(); /* no preemption */
4140147191Sjkoshy
4141147191Sjkoshy	cpu = curthread->td_oncpu;
4142147191Sjkoshy
4143147191Sjkoshy	if ((pp = pmc_find_process_descriptor(p,
4144147191Sjkoshy		 PMC_FLAG_REMOVE)) != NULL) {
4145147191Sjkoshy
4146147191Sjkoshy		PMCDBG(PRC,EXT,2,
4147147191Sjkoshy		    "process-exit proc=%p pmc-process=%p", p, pp);
4148147191Sjkoshy
4149147191Sjkoshy		/*
4150147191Sjkoshy		 * The exiting process could the target of
4151147191Sjkoshy		 * some PMCs which will be running on
4152147191Sjkoshy		 * currently executing CPU.
4153147191Sjkoshy		 *
4154147191Sjkoshy		 * We need to turn these PMCs off like we
4155147191Sjkoshy		 * would do at context switch OUT time.
4156147191Sjkoshy		 */
4157147191Sjkoshy		for (ri = 0; ri < md->pmd_npmc; ri++) {
4158147191Sjkoshy
4159147191Sjkoshy			/*
4160147191Sjkoshy			 * Pick up the pmc pointer from hardware
4161147191Sjkoshy			 * state similar to the CSW_OUT code.
4162147191Sjkoshy			 */
4163147191Sjkoshy			pm = NULL;
4164147191Sjkoshy
4165184802Sjkoshy			pcd = pmc_ri_to_classdep(md, ri, &adjri);
4166184802Sjkoshy
4167184802Sjkoshy			(void) (*pcd->pcd_get_config)(cpu, adjri, &pm);
4168184802Sjkoshy
4169147191Sjkoshy			PMCDBG(PRC,EXT,2, "ri=%d pm=%p", ri, pm);
4170147191Sjkoshy
4171147191Sjkoshy			if (pm == NULL ||
4172147191Sjkoshy			    !PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm)))
4173147191Sjkoshy				continue;
4174147191Sjkoshy
4175147191Sjkoshy			PMCDBG(PRC,EXT,2, "ppmcs[%d]=%p pm=%p "
4176147191Sjkoshy			    "state=%d", ri, pp->pp_pmcs[ri].pp_pmc,
4177147191Sjkoshy			    pm, pm->pm_state);
4178147191Sjkoshy
4179147191Sjkoshy			KASSERT(PMC_TO_ROWINDEX(pm) == ri,
4180147191Sjkoshy			    ("[pmc,%d] ri mismatch pmc(%d) ri(%d)",
4181147191Sjkoshy				__LINE__, PMC_TO_ROWINDEX(pm), ri));
4182147191Sjkoshy
4183147191Sjkoshy			KASSERT(pm == pp->pp_pmcs[ri].pp_pmc,
4184147191Sjkoshy			    ("[pmc,%d] pm %p != pp_pmcs[%d] %p",
4185147191Sjkoshy				__LINE__, pm, ri, pp->pp_pmcs[ri].pp_pmc));
4186147191Sjkoshy
4187184802Sjkoshy			(void) pcd->pcd_stop_pmc(cpu, adjri);
4188147191Sjkoshy
4189147191Sjkoshy			KASSERT(pm->pm_runcount > 0,
4190147191Sjkoshy			    ("[pmc,%d] bad runcount ri %d rc %d",
4191147191Sjkoshy				__LINE__, ri, pm->pm_runcount));
4192147191Sjkoshy
4193147867Sjkoshy			/* Stop hardware only if it is actually running */
4194147191Sjkoshy			if (pm->pm_state == PMC_STATE_RUNNING &&
4195147867Sjkoshy			    pm->pm_stalled == 0) {
4196184802Sjkoshy				pcd->pcd_read_pmc(cpu, adjri, &newvalue);
4197147191Sjkoshy				tmp = newvalue -
4198147191Sjkoshy				    PMC_PCPU_SAVED(cpu,ri);
4199147191Sjkoshy
4200147191Sjkoshy				mtx_pool_lock_spin(pmc_mtxpool, pm);
4201147191Sjkoshy				pm->pm_gv.pm_savedvalue += tmp;
4202147191Sjkoshy				pp->pp_pmcs[ri].pp_pmcval += tmp;
4203147191Sjkoshy				mtx_pool_unlock_spin(pmc_mtxpool, pm);
4204147191Sjkoshy			}
4205147191Sjkoshy
4206147191Sjkoshy			atomic_subtract_rel_32(&pm->pm_runcount,1);
4207147191Sjkoshy
4208147191Sjkoshy			KASSERT((int) pm->pm_runcount >= 0,
4209147191Sjkoshy			    ("[pmc,%d] runcount is %d", __LINE__, ri));
4210147191Sjkoshy
4211184802Sjkoshy			(void) pcd->pcd_config_pmc(cpu, adjri, NULL);
4212147191Sjkoshy		}
4213147191Sjkoshy
4214147191Sjkoshy		/*
4215147191Sjkoshy		 * Inform the MD layer of this pseudo "context switch
4216147191Sjkoshy		 * out"
4217147191Sjkoshy		 */
4218147191Sjkoshy		(void) md->pmd_switch_out(pmc_pcpu[cpu], pp);
4219147191Sjkoshy
4220147191Sjkoshy		critical_exit(); /* ok to be pre-empted now */
4221147191Sjkoshy
4222147191Sjkoshy		/*
4223147191Sjkoshy		 * Unlink this process from the PMCs that are
4224147191Sjkoshy		 * targetting it.  This will send a signal to
4225147191Sjkoshy		 * all PMC owner's whose PMCs are orphaned.
4226147191Sjkoshy		 *
4227147191Sjkoshy		 * Log PMC value at exit time if requested.
4228147191Sjkoshy		 */
4229147191Sjkoshy		for (ri = 0; ri < md->pmd_npmc; ri++)
4230147191Sjkoshy			if ((pm = pp->pp_pmcs[ri].pp_pmc) != NULL) {
4231147867Sjkoshy				if (pm->pm_flags & PMC_F_NEEDS_LOGFILE &&
4232147867Sjkoshy				    PMC_IS_COUNTING_MODE(PMC_TO_MODE(pm)))
4233147191Sjkoshy					pmclog_process_procexit(pm, pp);
4234147191Sjkoshy				pmc_unlink_target_process(pm, pp);
4235147191Sjkoshy			}
4236184205Sdes		free(pp, M_PMC);
4237147191Sjkoshy
4238147191Sjkoshy	} else
4239147191Sjkoshy		critical_exit(); /* pp == NULL */
4240147191Sjkoshy
4241147191Sjkoshy
4242147191Sjkoshy	/*
4243147191Sjkoshy	 * If the process owned PMCs, free them up and free up
4244147191Sjkoshy	 * memory.
4245147191Sjkoshy	 */
4246147191Sjkoshy	if ((po = pmc_find_owner_descriptor(p)) != NULL) {
4247147191Sjkoshy		pmc_remove_owner(po);
4248147191Sjkoshy		pmc_destroy_owner_descriptor(po);
4249145256Sjkoshy	}
4250147191Sjkoshy
4251147191Sjkoshy	sx_xunlock(&pmc_sx);
4252145256Sjkoshy}
4253145256Sjkoshy
4254145256Sjkoshy/*
4255145256Sjkoshy * Handle a process fork.
4256145256Sjkoshy *
4257145256Sjkoshy * If the parent process 'p1' is under HWPMC monitoring, then copy
4258145256Sjkoshy * over any attached PMCs that have 'do_descendants' semantics.
4259145256Sjkoshy */
4260145256Sjkoshy
4261145256Sjkoshystatic void
4262147191Sjkoshypmc_process_fork(void *arg __unused, struct proc *p1, struct proc *newproc,
4263145256Sjkoshy    int flags)
4264145256Sjkoshy{
4265145256Sjkoshy	int is_using_hwpmcs;
4266147191Sjkoshy	unsigned int ri;
4267147191Sjkoshy	uint32_t do_descendants;
4268147191Sjkoshy	struct pmc *pm;
4269147191Sjkoshy	struct pmc_owner *po;
4270147191Sjkoshy	struct pmc_process *ppnew, *ppold;
4271145256Sjkoshy
4272145256Sjkoshy	(void) flags;		/* unused parameter */
4273145256Sjkoshy
4274145256Sjkoshy	PROC_LOCK(p1);
4275145256Sjkoshy	is_using_hwpmcs = p1->p_flag & P_HWPMC;
4276145256Sjkoshy	PROC_UNLOCK(p1);
4277145256Sjkoshy
4278147191Sjkoshy	/*
4279147191Sjkoshy	 * If there are system-wide sampling PMCs active, we need to
4280147191Sjkoshy	 * log all fork events to their owner's logs.
4281147191Sjkoshy	 */
4282147191Sjkoshy
4283147191Sjkoshy	LIST_FOREACH(po, &pmc_ss_owners, po_ssnext)
4284147191Sjkoshy	    if (po->po_flags & PMC_PO_OWNS_LOGFILE)
4285147191Sjkoshy		    pmclog_process_procfork(po, p1->p_pid, newproc->p_pid);
4286147191Sjkoshy
4287147191Sjkoshy	if (!is_using_hwpmcs)
4288147191Sjkoshy		return;
4289147191Sjkoshy
4290147191Sjkoshy	PMC_GET_SX_XLOCK();
4291147191Sjkoshy	PMCDBG(PMC,FRK,1, "process-fork proc=%p (%d, %s) -> %p", p1,
4292147191Sjkoshy	    p1->p_pid, p1->p_comm, newproc);
4293147191Sjkoshy
4294147191Sjkoshy	/*
4295147191Sjkoshy	 * If the parent process (curthread->td_proc) is a
4296147191Sjkoshy	 * target of any PMCs, look for PMCs that are to be
4297147191Sjkoshy	 * inherited, and link these into the new process
4298147191Sjkoshy	 * descriptor.
4299147191Sjkoshy	 */
4300147191Sjkoshy	if ((ppold = pmc_find_process_descriptor(curthread->td_proc,
4301147191Sjkoshy		 PMC_FLAG_NONE)) == NULL)
4302147191Sjkoshy		goto done;		/* nothing to do */
4303147191Sjkoshy
4304147191Sjkoshy	do_descendants = 0;
4305147191Sjkoshy	for (ri = 0; ri < md->pmd_npmc; ri++)
4306147191Sjkoshy		if ((pm = ppold->pp_pmcs[ri].pp_pmc) != NULL)
4307147191Sjkoshy			do_descendants |= pm->pm_flags & PMC_F_DESCENDANTS;
4308147191Sjkoshy	if (do_descendants == 0) /* nothing to do */
4309147191Sjkoshy		goto done;
4310147191Sjkoshy
4311147191Sjkoshy	/* allocate a descriptor for the new process  */
4312147191Sjkoshy	if ((ppnew = pmc_find_process_descriptor(newproc,
4313147191Sjkoshy		 PMC_FLAG_ALLOCATE)) == NULL)
4314147191Sjkoshy		goto done;
4315147191Sjkoshy
4316147191Sjkoshy	/*
4317147191Sjkoshy	 * Run through all PMCs that were targeting the old process
4318147191Sjkoshy	 * and which specified F_DESCENDANTS and attach them to the
4319147191Sjkoshy	 * new process.
4320147191Sjkoshy	 *
4321147191Sjkoshy	 * Log the fork event to all owners of PMCs attached to this
4322147191Sjkoshy	 * process, if not already logged.
4323147191Sjkoshy	 */
4324147191Sjkoshy	for (ri = 0; ri < md->pmd_npmc; ri++)
4325147191Sjkoshy		if ((pm = ppold->pp_pmcs[ri].pp_pmc) != NULL &&
4326147191Sjkoshy		    (pm->pm_flags & PMC_F_DESCENDANTS)) {
4327147191Sjkoshy			pmc_link_target_process(pm, ppnew);
4328147191Sjkoshy			po = pm->pm_owner;
4329147191Sjkoshy			if (po->po_sscount == 0 &&
4330147191Sjkoshy			    po->po_flags & PMC_PO_OWNS_LOGFILE)
4331147191Sjkoshy				pmclog_process_procfork(po, p1->p_pid,
4332147191Sjkoshy				    newproc->p_pid);
4333147191Sjkoshy		}
4334147191Sjkoshy
4335147191Sjkoshy	/*
4336147191Sjkoshy	 * Now mark the new process as being tracked by this driver.
4337147191Sjkoshy	 */
4338147191Sjkoshy	PROC_LOCK(newproc);
4339147191Sjkoshy	newproc->p_flag |= P_HWPMC;
4340147191Sjkoshy	PROC_UNLOCK(newproc);
4341147191Sjkoshy
4342147191Sjkoshy done:
4343147191Sjkoshy	sx_xunlock(&pmc_sx);
4344145256Sjkoshy}
4345145256Sjkoshy
4346145256Sjkoshy
4347145256Sjkoshy/*
4348145256Sjkoshy * initialization
4349145256Sjkoshy */
4350145256Sjkoshy
4351145256Sjkoshystatic const char *pmc_name_of_pmcclass[] = {
4352145256Sjkoshy#undef	__PMC_CLASS
4353145256Sjkoshy#define	__PMC_CLASS(N) #N ,
4354145256Sjkoshy	__PMC_CLASSES()
4355145256Sjkoshy};
4356145256Sjkoshy
4357145256Sjkoshystatic int
4358145256Sjkoshypmc_initialize(void)
4359145256Sjkoshy{
4360184802Sjkoshy	int c, cpu, error, n, ri;
4361183266Sjkoshy	unsigned int maxcpu;
4362145256Sjkoshy	struct pmc_binding pb;
4363174395Sjkoshy	struct pmc_sample *ps;
4364184802Sjkoshy	struct pmc_classdep *pcd;
4365147191Sjkoshy	struct pmc_samplebuffer *sb;
4366145256Sjkoshy
4367145256Sjkoshy	md = NULL;
4368145256Sjkoshy	error = 0;
4369145256Sjkoshy
4370153110Sru#ifdef	DEBUG
4371145256Sjkoshy	/* parse debug flags first */
4372145256Sjkoshy	if (TUNABLE_STR_FETCH(PMC_SYSCTL_NAME_PREFIX "debugflags",
4373145256Sjkoshy		pmc_debugstr, sizeof(pmc_debugstr)))
4374145256Sjkoshy		pmc_debugflags_parse(pmc_debugstr,
4375145256Sjkoshy		    pmc_debugstr+strlen(pmc_debugstr));
4376145256Sjkoshy#endif
4377145256Sjkoshy
4378145256Sjkoshy	PMCDBG(MOD,INI,0, "PMC Initialize (version %x)", PMC_VERSION);
4379145256Sjkoshy
4380148562Sjkoshy	/* check kernel version */
4381148562Sjkoshy	if (pmc_kernel_version != PMC_VERSION) {
4382148562Sjkoshy		if (pmc_kernel_version == 0)
4383148562Sjkoshy			printf("hwpmc: this kernel has not been compiled with "
4384148562Sjkoshy			    "'options HWPMC_HOOKS'.\n");
4385148562Sjkoshy		else
4386148562Sjkoshy			printf("hwpmc: kernel version (0x%x) does not match "
4387148562Sjkoshy			    "module version (0x%x).\n", pmc_kernel_version,
4388148562Sjkoshy			    PMC_VERSION);
4389148562Sjkoshy		return EPROGMISMATCH;
4390148562Sjkoshy	}
4391148562Sjkoshy
4392145256Sjkoshy	/*
4393145256Sjkoshy	 * check sysctl parameters
4394145256Sjkoshy	 */
4395145256Sjkoshy
4396145256Sjkoshy	if (pmc_hashsize <= 0) {
4397174395Sjkoshy		(void) printf("hwpmc: tunable \"hashsize\"=%d must be "
4398174395Sjkoshy		    "greater than zero.\n", pmc_hashsize);
4399145256Sjkoshy		pmc_hashsize = PMC_HASH_SIZE;
4400145256Sjkoshy	}
4401145256Sjkoshy
4402147191Sjkoshy	if (pmc_nsamples <= 0 || pmc_nsamples > 65535) {
4403174395Sjkoshy		(void) printf("hwpmc: tunable \"nsamples\"=%d out of "
4404174395Sjkoshy		    "range.\n", pmc_nsamples);
4405147191Sjkoshy		pmc_nsamples = PMC_NSAMPLES;
4406147191Sjkoshy	}
4407145256Sjkoshy
4408174395Sjkoshy	if (pmc_callchaindepth <= 0 ||
4409174395Sjkoshy	    pmc_callchaindepth > PMC_CALLCHAIN_DEPTH_MAX) {
4410174395Sjkoshy		(void) printf("hwpmc: tunable \"callchaindepth\"=%d out of "
4411174395Sjkoshy		    "range.\n", pmc_callchaindepth);
4412174395Sjkoshy		pmc_callchaindepth = PMC_CALLCHAIN_DEPTH;
4413174395Sjkoshy	}
4414174395Sjkoshy
4415147191Sjkoshy	md = pmc_md_initialize();
4416147191Sjkoshy
4417184802Sjkoshy	if (md == NULL)
4418184802Sjkoshy		return (ENOSYS);
4419145256Sjkoshy
4420184802Sjkoshy	KASSERT(md->pmd_nclass >= 1 && md->pmd_npmc >= 1,
4421184802Sjkoshy	    ("[pmc,%d] no classes or pmcs", __LINE__));
4422184802Sjkoshy
4423184802Sjkoshy	/* Compute the map from row-indices to classdep pointers. */
4424184802Sjkoshy	pmc_rowindex_to_classdep = malloc(sizeof(struct pmc_classdep *) *
4425184802Sjkoshy	    md->pmd_npmc, M_PMC, M_WAITOK|M_ZERO);
4426184802Sjkoshy
4427184802Sjkoshy	for (n = 0; n < md->pmd_npmc; n++)
4428184802Sjkoshy		pmc_rowindex_to_classdep[n] = NULL;
4429184802Sjkoshy	for (ri = c = 0; c < md->pmd_nclass; c++) {
4430184802Sjkoshy		pcd = &md->pmd_classdep[c];
4431184802Sjkoshy		for (n = 0; n < pcd->pcd_num; n++, ri++)
4432184802Sjkoshy			pmc_rowindex_to_classdep[ri] = pcd;
4433184802Sjkoshy	}
4434184802Sjkoshy
4435184802Sjkoshy	KASSERT(ri == md->pmd_npmc,
4436184802Sjkoshy	    ("[pmc,%d] npmc miscomputed: ri=%d, md->npmc=%d", __LINE__,
4437184802Sjkoshy	    ri, md->pmd_npmc));
4438184802Sjkoshy
4439183266Sjkoshy	maxcpu = pmc_cpu_max();
4440183266Sjkoshy
4441145256Sjkoshy	/* allocate space for the per-cpu array */
4442184802Sjkoshy	pmc_pcpu = malloc(maxcpu * sizeof(struct pmc_cpu *), M_PMC,
4443184802Sjkoshy	    M_WAITOK|M_ZERO);
4444145256Sjkoshy
4445145256Sjkoshy	/* per-cpu 'saved values' for managing process-mode PMCs */
4446184214Sdes	pmc_pcpu_saved = malloc(sizeof(pmc_value_t) * maxcpu * md->pmd_npmc,
4447184214Sdes	    M_PMC, M_WAITOK);
4448145256Sjkoshy
4449183266Sjkoshy	/* Perform CPU-dependent initialization. */
4450145256Sjkoshy	pmc_save_cpu_binding(&pb);
4451184802Sjkoshy	error = 0;
4452184802Sjkoshy	for (cpu = 0; error == 0 && cpu < maxcpu; cpu++) {
4453183266Sjkoshy		if (!pmc_cpu_is_active(cpu))
4454145256Sjkoshy			continue;
4455145256Sjkoshy		pmc_select_cpu(cpu);
4456184802Sjkoshy		pmc_pcpu[cpu] = malloc(sizeof(struct pmc_cpu) +
4457184802Sjkoshy		    md->pmd_npmc * sizeof(struct pmc_hw *), M_PMC,
4458184802Sjkoshy		    M_WAITOK|M_ZERO);
4459184802Sjkoshy		if (md->pmd_pcpu_init)
4460185363Sjkoshy			error = md->pmd_pcpu_init(md, cpu);
4461184802Sjkoshy		for (n = 0; error == 0 && n < md->pmd_nclass; n++)
4462184802Sjkoshy			error = md->pmd_classdep[n].pcd_pcpu_init(md, cpu);
4463145256Sjkoshy	}
4464145256Sjkoshy	pmc_restore_cpu_binding(&pb);
4465145256Sjkoshy
4466184802Sjkoshy	if (error)
4467184802Sjkoshy		return (error);
4468145256Sjkoshy
4469147191Sjkoshy	/* allocate space for the sample array */
4470183266Sjkoshy	for (cpu = 0; cpu < maxcpu; cpu++) {
4471183266Sjkoshy		if (!pmc_cpu_is_active(cpu))
4472147191Sjkoshy			continue;
4473184802Sjkoshy
4474184214Sdes		sb = malloc(sizeof(struct pmc_samplebuffer) +
4475147191Sjkoshy		    pmc_nsamples * sizeof(struct pmc_sample), M_PMC,
4476147191Sjkoshy		    M_WAITOK|M_ZERO);
4477147191Sjkoshy		sb->ps_read = sb->ps_write = sb->ps_samples;
4478153735Sjkoshy		sb->ps_fence = sb->ps_samples + pmc_nsamples;
4479184802Sjkoshy
4480147191Sjkoshy		KASSERT(pmc_pcpu[cpu] != NULL,
4481147191Sjkoshy		    ("[pmc,%d] cpu=%d Null per-cpu data", __LINE__, cpu));
4482147191Sjkoshy
4483184802Sjkoshy		sb->ps_callchains = malloc(pmc_callchaindepth * pmc_nsamples *
4484184802Sjkoshy		    sizeof(uintptr_t), M_PMC, M_WAITOK|M_ZERO);
4485174395Sjkoshy
4486174395Sjkoshy		for (n = 0, ps = sb->ps_samples; n < pmc_nsamples; n++, ps++)
4487174395Sjkoshy			ps->ps_pc = sb->ps_callchains +
4488174395Sjkoshy			    (n * pmc_callchaindepth);
4489174395Sjkoshy
4490147191Sjkoshy		pmc_pcpu[cpu]->pc_sb = sb;
4491147191Sjkoshy	}
4492147191Sjkoshy
4493145256Sjkoshy	/* allocate space for the row disposition array */
4494145256Sjkoshy	pmc_pmcdisp = malloc(sizeof(enum pmc_mode) * md->pmd_npmc,
4495145256Sjkoshy	    M_PMC, M_WAITOK|M_ZERO);
4496145256Sjkoshy
4497145256Sjkoshy	KASSERT(pmc_pmcdisp != NULL,
4498145256Sjkoshy	    ("[pmc,%d] pmcdisp allocation returned NULL", __LINE__));
4499145256Sjkoshy
4500145256Sjkoshy	/* mark all PMCs as available */
4501145256Sjkoshy	for (n = 0; n < (int) md->pmd_npmc; n++)
4502145256Sjkoshy		PMC_MARK_ROW_FREE(n);
4503145256Sjkoshy
4504145256Sjkoshy	/* allocate thread hash tables */
4505145256Sjkoshy	pmc_ownerhash = hashinit(pmc_hashsize, M_PMC,
4506145256Sjkoshy	    &pmc_ownerhashmask);
4507145256Sjkoshy
4508145256Sjkoshy	pmc_processhash = hashinit(pmc_hashsize, M_PMC,
4509145256Sjkoshy	    &pmc_processhashmask);
4510168856Sjkoshy	mtx_init(&pmc_processhash_mtx, "pmc-process-hash", "pmc-leaf",
4511168856Sjkoshy	    MTX_SPIN);
4512145256Sjkoshy
4513147191Sjkoshy	LIST_INIT(&pmc_ss_owners);
4514147191Sjkoshy	pmc_ss_count = 0;
4515147191Sjkoshy
4516145256Sjkoshy	/* allocate a pool of spin mutexes */
4517168856Sjkoshy	pmc_mtxpool = mtx_pool_create("pmc-leaf", pmc_mtxpool_size,
4518168856Sjkoshy	    MTX_SPIN);
4519145256Sjkoshy
4520145256Sjkoshy	PMCDBG(MOD,INI,1, "pmc_ownerhash=%p, mask=0x%lx "
4521145256Sjkoshy	    "targethash=%p mask=0x%lx", pmc_ownerhash, pmc_ownerhashmask,
4522145256Sjkoshy	    pmc_processhash, pmc_processhashmask);
4523145256Sjkoshy
4524145256Sjkoshy	/* register process {exit,fork,exec} handlers */
4525145256Sjkoshy	pmc_exit_tag = EVENTHANDLER_REGISTER(process_exit,
4526145256Sjkoshy	    pmc_process_exit, NULL, EVENTHANDLER_PRI_ANY);
4527145256Sjkoshy	pmc_fork_tag = EVENTHANDLER_REGISTER(process_fork,
4528145256Sjkoshy	    pmc_process_fork, NULL, EVENTHANDLER_PRI_ANY);
4529145256Sjkoshy
4530147191Sjkoshy	/* initialize logging */
4531147191Sjkoshy	pmclog_initialize();
4532147191Sjkoshy
4533145256Sjkoshy	/* set hook functions */
4534145256Sjkoshy	pmc_intr = md->pmd_intr;
4535145256Sjkoshy	pmc_hook = pmc_hook_handler;
4536145256Sjkoshy
4537145256Sjkoshy	if (error == 0) {
4538145256Sjkoshy		printf(PMC_MODULE_NAME ":");
4539149373Sjkoshy		for (n = 0; n < (int) md->pmd_nclass; n++) {
4540184802Sjkoshy			pcd = &md->pmd_classdep[n];
4541184997Sjkoshy			printf(" %s/%d/%d/0x%b",
4542184802Sjkoshy			    pmc_name_of_pmcclass[pcd->pcd_class],
4543184802Sjkoshy			    pcd->pcd_num,
4544184997Sjkoshy			    pcd->pcd_width,
4545184802Sjkoshy			    pcd->pcd_caps,
4546149373Sjkoshy			    "\20"
4547149373Sjkoshy			    "\1INT\2USR\3SYS\4EDG\5THR"
4548149373Sjkoshy			    "\6REA\7WRI\10INV\11QUA\12PRC"
4549149373Sjkoshy			    "\13TAG\14CSC");
4550149373Sjkoshy		}
4551145256Sjkoshy		printf("\n");
4552145256Sjkoshy	}
4553145256Sjkoshy
4554184802Sjkoshy	return (error);
4555145256Sjkoshy}
4556145256Sjkoshy
4557145256Sjkoshy/* prepare to be unloaded */
4558145256Sjkoshystatic void
4559145256Sjkoshypmc_cleanup(void)
4560145256Sjkoshy{
4561184802Sjkoshy	int c, cpu;
4562183266Sjkoshy	unsigned int maxcpu;
4563145256Sjkoshy	struct pmc_ownerhash *ph;
4564145256Sjkoshy	struct pmc_owner *po, *tmp;
4565145256Sjkoshy	struct pmc_binding pb;
4566153110Sru#ifdef	DEBUG
4567145256Sjkoshy	struct pmc_processhash *prh;
4568145256Sjkoshy#endif
4569145256Sjkoshy
4570145256Sjkoshy	PMCDBG(MOD,INI,0, "%s", "cleanup");
4571145256Sjkoshy
4572147191Sjkoshy	/* switch off sampling */
4573147191Sjkoshy	atomic_store_rel_int(&pmc_cpumask, 0);
4574147191Sjkoshy	pmc_intr = NULL;
4575145256Sjkoshy
4576145256Sjkoshy	sx_xlock(&pmc_sx);
4577145256Sjkoshy	if (pmc_hook == NULL) {	/* being unloaded already */
4578145256Sjkoshy		sx_xunlock(&pmc_sx);
4579145256Sjkoshy		return;
4580145256Sjkoshy	}
4581145256Sjkoshy
4582145256Sjkoshy	pmc_hook = NULL; /* prevent new threads from entering module */
4583145256Sjkoshy
4584145256Sjkoshy	/* deregister event handlers */
4585145256Sjkoshy	EVENTHANDLER_DEREGISTER(process_fork, pmc_fork_tag);
4586145256Sjkoshy	EVENTHANDLER_DEREGISTER(process_exit, pmc_exit_tag);
4587145256Sjkoshy
4588145256Sjkoshy	/* send SIGBUS to all owner threads, free up allocations */
4589145256Sjkoshy	if (pmc_ownerhash)
4590145256Sjkoshy		for (ph = pmc_ownerhash;
4591145256Sjkoshy		     ph <= &pmc_ownerhash[pmc_ownerhashmask];
4592145256Sjkoshy		     ph++) {
4593145256Sjkoshy			LIST_FOREACH_SAFE(po, ph, po_next, tmp) {
4594145256Sjkoshy				pmc_remove_owner(po);
4595145256Sjkoshy
4596145256Sjkoshy				/* send SIGBUS to owner processes */
4597145256Sjkoshy				PMCDBG(MOD,INI,2, "cleanup signal proc=%p "
4598145256Sjkoshy				    "(%d, %s)", po->po_owner,
4599145256Sjkoshy				    po->po_owner->p_pid,
4600145256Sjkoshy				    po->po_owner->p_comm);
4601145256Sjkoshy
4602145256Sjkoshy				PROC_LOCK(po->po_owner);
4603145256Sjkoshy				psignal(po->po_owner, SIGBUS);
4604145256Sjkoshy				PROC_UNLOCK(po->po_owner);
4605147191Sjkoshy
4606147191Sjkoshy				pmc_destroy_owner_descriptor(po);
4607145256Sjkoshy			}
4608145256Sjkoshy		}
4609145256Sjkoshy
4610145256Sjkoshy	/* reclaim allocated data structures */
4611145256Sjkoshy	if (pmc_mtxpool)
4612145256Sjkoshy		mtx_pool_destroy(&pmc_mtxpool);
4613145256Sjkoshy
4614145256Sjkoshy	mtx_destroy(&pmc_processhash_mtx);
4615145256Sjkoshy	if (pmc_processhash) {
4616153110Sru#ifdef	DEBUG
4617145256Sjkoshy		struct pmc_process *pp;
4618145256Sjkoshy
4619145256Sjkoshy		PMCDBG(MOD,INI,3, "%s", "destroy process hash");
4620145256Sjkoshy		for (prh = pmc_processhash;
4621145256Sjkoshy		     prh <= &pmc_processhash[pmc_processhashmask];
4622145256Sjkoshy		     prh++)
4623145256Sjkoshy			LIST_FOREACH(pp, prh, pp_next)
4624145256Sjkoshy			    PMCDBG(MOD,INI,3, "pid=%d", pp->pp_proc->p_pid);
4625145256Sjkoshy#endif
4626145256Sjkoshy
4627145256Sjkoshy		hashdestroy(pmc_processhash, M_PMC, pmc_processhashmask);
4628145256Sjkoshy		pmc_processhash = NULL;
4629145256Sjkoshy	}
4630145256Sjkoshy
4631145256Sjkoshy	if (pmc_ownerhash) {
4632145256Sjkoshy		PMCDBG(MOD,INI,3, "%s", "destroy owner hash");
4633145256Sjkoshy		hashdestroy(pmc_ownerhash, M_PMC, pmc_ownerhashmask);
4634145256Sjkoshy		pmc_ownerhash = NULL;
4635145256Sjkoshy	}
4636145256Sjkoshy
4637147191Sjkoshy	KASSERT(LIST_EMPTY(&pmc_ss_owners),
4638147191Sjkoshy	    ("[pmc,%d] Global SS owner list not empty", __LINE__));
4639147191Sjkoshy	KASSERT(pmc_ss_count == 0,
4640147191Sjkoshy	    ("[pmc,%d] Global SS count not empty", __LINE__));
4641147191Sjkoshy
4642184802Sjkoshy 	/* do processor and pmc-class dependent cleanup */
4643183266Sjkoshy	maxcpu = pmc_cpu_max();
4644153735Sjkoshy
4645145256Sjkoshy	PMCDBG(MOD,INI,3, "%s", "md cleanup");
4646145256Sjkoshy	if (md) {
4647145256Sjkoshy		pmc_save_cpu_binding(&pb);
4648183266Sjkoshy		for (cpu = 0; cpu < maxcpu; cpu++) {
4649145256Sjkoshy			PMCDBG(MOD,INI,1,"pmc-cleanup cpu=%d pcs=%p",
4650145256Sjkoshy			    cpu, pmc_pcpu[cpu]);
4651183266Sjkoshy			if (!pmc_cpu_is_active(cpu) || pmc_pcpu[cpu] == NULL)
4652145256Sjkoshy				continue;
4653145256Sjkoshy			pmc_select_cpu(cpu);
4654184802Sjkoshy			for (c = 0; c < md->pmd_nclass; c++)
4655184802Sjkoshy				md->pmd_classdep[c].pcd_pcpu_fini(md, cpu);
4656184802Sjkoshy			if (md->pmd_pcpu_fini)
4657185363Sjkoshy				md->pmd_pcpu_fini(md, cpu);
4658145256Sjkoshy		}
4659184994Sjkoshy
4660184994Sjkoshy		pmc_md_finalize(md);
4661184994Sjkoshy
4662184205Sdes		free(md, M_PMC);
4663145256Sjkoshy		md = NULL;
4664145256Sjkoshy		pmc_restore_cpu_binding(&pb);
4665145256Sjkoshy	}
4666145256Sjkoshy
4667184802Sjkoshy	/* Free per-cpu descriptors. */
4668184802Sjkoshy	for (cpu = 0; cpu < maxcpu; cpu++) {
4669184802Sjkoshy		if (!pmc_cpu_is_active(cpu))
4670184802Sjkoshy			continue;
4671184802Sjkoshy		KASSERT(pmc_pcpu[cpu]->pc_sb != NULL,
4672184802Sjkoshy		    ("[pmc,%d] Null cpu sample buffer cpu=%d", __LINE__,
4673184802Sjkoshy			cpu));
4674184802Sjkoshy		free(pmc_pcpu[cpu]->pc_sb->ps_callchains, M_PMC);
4675184802Sjkoshy		free(pmc_pcpu[cpu]->pc_sb, M_PMC);
4676184802Sjkoshy		free(pmc_pcpu[cpu], M_PMC);
4677184802Sjkoshy	}
4678184802Sjkoshy
4679184205Sdes	free(pmc_pcpu, M_PMC);
4680145256Sjkoshy	pmc_pcpu = NULL;
4681145256Sjkoshy
4682184205Sdes	free(pmc_pcpu_saved, M_PMC);
4683145256Sjkoshy	pmc_pcpu_saved = NULL;
4684145256Sjkoshy
4685145256Sjkoshy	if (pmc_pmcdisp) {
4686184205Sdes		free(pmc_pmcdisp, M_PMC);
4687145256Sjkoshy		pmc_pmcdisp = NULL;
4688145256Sjkoshy	}
4689145256Sjkoshy
4690184802Sjkoshy	if (pmc_rowindex_to_classdep) {
4691184802Sjkoshy		free(pmc_rowindex_to_classdep, M_PMC);
4692184802Sjkoshy		pmc_rowindex_to_classdep = NULL;
4693184802Sjkoshy	}
4694184802Sjkoshy
4695147191Sjkoshy	pmclog_shutdown();
4696147191Sjkoshy
4697145256Sjkoshy	sx_xunlock(&pmc_sx); 	/* we are done */
4698145256Sjkoshy}
4699145256Sjkoshy
4700145256Sjkoshy/*
4701145256Sjkoshy * The function called at load/unload.
4702145256Sjkoshy */
4703145256Sjkoshy
4704145256Sjkoshystatic int
4705145256Sjkoshyload (struct module *module __unused, int cmd, void *arg __unused)
4706145256Sjkoshy{
4707145256Sjkoshy	int error;
4708145256Sjkoshy
4709145256Sjkoshy	error = 0;
4710145256Sjkoshy
4711145256Sjkoshy	switch (cmd) {
4712145256Sjkoshy	case MOD_LOAD :
4713145256Sjkoshy		/* initialize the subsystem */
4714145256Sjkoshy		error = pmc_initialize();
4715145256Sjkoshy		if (error != 0)
4716145256Sjkoshy			break;
4717183266Sjkoshy		PMCDBG(MOD,INI,1, "syscall=%d maxcpu=%d",
4718183266Sjkoshy		    pmc_syscall_num, pmc_cpu_max());
4719145256Sjkoshy		break;
4720145256Sjkoshy
4721145256Sjkoshy
4722145256Sjkoshy	case MOD_UNLOAD :
4723145256Sjkoshy	case MOD_SHUTDOWN:
4724145256Sjkoshy		pmc_cleanup();
4725145256Sjkoshy		PMCDBG(MOD,INI,1, "%s", "unloaded");
4726145256Sjkoshy		break;
4727145256Sjkoshy
4728145256Sjkoshy	default :
4729145256Sjkoshy		error = EINVAL;	/* XXX should panic(9) */
4730145256Sjkoshy		break;
4731145256Sjkoshy	}
4732145256Sjkoshy
4733145256Sjkoshy	return error;
4734145256Sjkoshy}
4735145256Sjkoshy
4736145256Sjkoshy/* memory pool */
4737145256SjkoshyMALLOC_DEFINE(M_PMC, "pmc", "Memory space for the PMC module");
4738