hwpmc_mod.c revision 154483
1145256Sjkoshy/*-
2145256Sjkoshy * Copyright (c) 2003-2005 Joseph Koshy
3145256Sjkoshy * All rights reserved.
4145256Sjkoshy *
5145256Sjkoshy * Redistribution and use in source and binary forms, with or without
6145256Sjkoshy * modification, are permitted provided that the following conditions
7145256Sjkoshy * are met:
8145256Sjkoshy * 1. Redistributions of source code must retain the above copyright
9145256Sjkoshy *    notice, this list of conditions and the following disclaimer.
10145256Sjkoshy * 2. Redistributions in binary form must reproduce the above copyright
11145256Sjkoshy *    notice, this list of conditions and the following disclaimer in the
12145256Sjkoshy *    documentation and/or other materials provided with the distribution.
13145256Sjkoshy *
14145256Sjkoshy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15145256Sjkoshy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16145256Sjkoshy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17145256Sjkoshy * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18145256Sjkoshy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19145256Sjkoshy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20145256Sjkoshy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21145256Sjkoshy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22145256Sjkoshy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23145256Sjkoshy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24145256Sjkoshy * SUCH DAMAGE.
25145256Sjkoshy *
26145256Sjkoshy */
27145256Sjkoshy
28145256Sjkoshy#include <sys/cdefs.h>
29145256Sjkoshy__FBSDID("$FreeBSD: head/sys/dev/hwpmc/hwpmc_mod.c 154483 2006-01-17 16:53:50Z jkoshy $");
30145256Sjkoshy
31145256Sjkoshy#include <sys/param.h>
32145256Sjkoshy#include <sys/eventhandler.h>
33145256Sjkoshy#include <sys/jail.h>
34145256Sjkoshy#include <sys/kernel.h>
35147191Sjkoshy#include <sys/kthread.h>
36145256Sjkoshy#include <sys/limits.h>
37145256Sjkoshy#include <sys/lock.h>
38145256Sjkoshy#include <sys/malloc.h>
39145256Sjkoshy#include <sys/module.h>
40145256Sjkoshy#include <sys/mutex.h>
41145256Sjkoshy#include <sys/pmc.h>
42145256Sjkoshy#include <sys/pmckern.h>
43147191Sjkoshy#include <sys/pmclog.h>
44145256Sjkoshy#include <sys/proc.h>
45145256Sjkoshy#include <sys/queue.h>
46147191Sjkoshy#include <sys/resourcevar.h>
47145256Sjkoshy#include <sys/sched.h>
48145256Sjkoshy#include <sys/signalvar.h>
49145256Sjkoshy#include <sys/smp.h>
50145256Sjkoshy#include <sys/sx.h>
51145256Sjkoshy#include <sys/sysctl.h>
52145256Sjkoshy#include <sys/sysent.h>
53145256Sjkoshy#include <sys/systm.h>
54147191Sjkoshy#include <sys/vnode.h>
55145256Sjkoshy
56147191Sjkoshy#include <machine/atomic.h>
57145256Sjkoshy#include <machine/md_var.h>
58145256Sjkoshy
59145256Sjkoshy/*
60145256Sjkoshy * Types
61145256Sjkoshy */
62145256Sjkoshy
63145256Sjkoshyenum pmc_flags {
64145256Sjkoshy	PMC_FLAG_NONE	  = 0x00, /* do nothing */
65145256Sjkoshy	PMC_FLAG_REMOVE   = 0x01, /* atomically remove entry from hash */
66145256Sjkoshy	PMC_FLAG_ALLOCATE = 0x02, /* add entry to hash if not found */
67145256Sjkoshy};
68145256Sjkoshy
69145256Sjkoshy/*
70145256Sjkoshy * The offset in sysent where the syscall is allocated.
71145256Sjkoshy */
72145256Sjkoshy
73145256Sjkoshystatic int pmc_syscall_num = NO_SYSCALL;
74145256Sjkoshystruct pmc_cpu		**pmc_pcpu;	 /* per-cpu state */
75145256Sjkoshypmc_value_t		*pmc_pcpu_saved; /* saved PMC values: CSW handling */
76145256Sjkoshy
77145256Sjkoshy#define	PMC_PCPU_SAVED(C,R)	pmc_pcpu_saved[(R) + md->pmd_npmc*(C)]
78145256Sjkoshy
79145256Sjkoshystruct mtx_pool		*pmc_mtxpool;
80145256Sjkoshystatic int		*pmc_pmcdisp;	 /* PMC row dispositions */
81145256Sjkoshy
82145256Sjkoshy#define	PMC_ROW_DISP_IS_FREE(R)		(pmc_pmcdisp[(R)] == 0)
83145256Sjkoshy#define	PMC_ROW_DISP_IS_THREAD(R)	(pmc_pmcdisp[(R)] > 0)
84145256Sjkoshy#define	PMC_ROW_DISP_IS_STANDALONE(R)	(pmc_pmcdisp[(R)] < 0)
85145256Sjkoshy
86145256Sjkoshy#define	PMC_MARK_ROW_FREE(R) do {					  \
87145256Sjkoshy	pmc_pmcdisp[(R)] = 0;						  \
88145256Sjkoshy} while (0)
89145256Sjkoshy
90145256Sjkoshy#define	PMC_MARK_ROW_STANDALONE(R) do {					  \
91145256Sjkoshy	KASSERT(pmc_pmcdisp[(R)] <= 0, ("[pmc,%d] row disposition error", \
92145256Sjkoshy		    __LINE__));						  \
93145256Sjkoshy	atomic_add_int(&pmc_pmcdisp[(R)], -1);				  \
94145256Sjkoshy	KASSERT(pmc_pmcdisp[(R)] >= (-mp_ncpus), ("[pmc,%d] row "	  \
95145256Sjkoshy		"disposition error", __LINE__));			  \
96145256Sjkoshy} while (0)
97145256Sjkoshy
98145256Sjkoshy#define	PMC_UNMARK_ROW_STANDALONE(R) do { 				  \
99145256Sjkoshy	atomic_add_int(&pmc_pmcdisp[(R)], 1);				  \
100145256Sjkoshy	KASSERT(pmc_pmcdisp[(R)] <= 0, ("[pmc,%d] row disposition error", \
101145256Sjkoshy		    __LINE__));						  \
102145256Sjkoshy} while (0)
103145256Sjkoshy
104145256Sjkoshy#define	PMC_MARK_ROW_THREAD(R) do {					  \
105145256Sjkoshy	KASSERT(pmc_pmcdisp[(R)] >= 0, ("[pmc,%d] row disposition error", \
106145256Sjkoshy		    __LINE__));						  \
107145256Sjkoshy	atomic_add_int(&pmc_pmcdisp[(R)], 1);				  \
108145256Sjkoshy} while (0)
109145256Sjkoshy
110145256Sjkoshy#define	PMC_UNMARK_ROW_THREAD(R) do {					  \
111145256Sjkoshy	atomic_add_int(&pmc_pmcdisp[(R)], -1);				  \
112145256Sjkoshy	KASSERT(pmc_pmcdisp[(R)] >= 0, ("[pmc,%d] row disposition error", \
113145256Sjkoshy		    __LINE__));						  \
114145256Sjkoshy} while (0)
115145256Sjkoshy
116145256Sjkoshy
117145256Sjkoshy/* various event handlers */
118145256Sjkoshystatic eventhandler_tag	pmc_exit_tag, pmc_fork_tag;
119145256Sjkoshy
120145256Sjkoshy/* Module statistics */
121145256Sjkoshystruct pmc_op_getdriverstats pmc_stats;
122145256Sjkoshy
123145256Sjkoshy/* Machine/processor dependent operations */
124145256Sjkoshystruct pmc_mdep  *md;
125145256Sjkoshy
126145256Sjkoshy/*
127145256Sjkoshy * Hash tables mapping owner processes and target threads to PMCs.
128145256Sjkoshy */
129145256Sjkoshy
130145256Sjkoshystruct mtx pmc_processhash_mtx;		/* spin mutex */
131145256Sjkoshystatic u_long pmc_processhashmask;
132145256Sjkoshystatic LIST_HEAD(pmc_processhash, pmc_process)	*pmc_processhash;
133145256Sjkoshy
134145256Sjkoshy/*
135145256Sjkoshy * Hash table of PMC owner descriptors.  This table is protected by
136145256Sjkoshy * the shared PMC "sx" lock.
137145256Sjkoshy */
138145256Sjkoshy
139145256Sjkoshystatic u_long pmc_ownerhashmask;
140145256Sjkoshystatic LIST_HEAD(pmc_ownerhash, pmc_owner)	*pmc_ownerhash;
141145256Sjkoshy
142145256Sjkoshy/*
143147191Sjkoshy * List of PMC owners with system-wide sampling PMCs.
144147191Sjkoshy */
145147191Sjkoshy
146147191Sjkoshystatic LIST_HEAD(, pmc_owner)			pmc_ss_owners;
147147191Sjkoshy
148147191Sjkoshy
149147191Sjkoshy/*
150145256Sjkoshy * Prototypes
151145256Sjkoshy */
152145256Sjkoshy
153153110Sru#ifdef	DEBUG
154145256Sjkoshystatic int	pmc_debugflags_sysctl_handler(SYSCTL_HANDLER_ARGS);
155145256Sjkoshystatic int	pmc_debugflags_parse(char *newstr, char *fence);
156145256Sjkoshy#endif
157145256Sjkoshy
158145256Sjkoshystatic int	load(struct module *module, int cmd, void *arg);
159147191Sjkoshystatic int	pmc_attach_process(struct proc *p, struct pmc *pm);
160145256Sjkoshystatic struct pmc *pmc_allocate_pmc_descriptor(void);
161147191Sjkoshystatic struct pmc_owner *pmc_allocate_owner_descriptor(struct proc *p);
162147191Sjkoshystatic int	pmc_attach_one_process(struct proc *p, struct pmc *pm);
163147191Sjkoshystatic int	pmc_can_allocate_rowindex(struct proc *p, unsigned int ri,
164147191Sjkoshy    int cpu);
165147191Sjkoshystatic int	pmc_can_attach(struct pmc *pm, struct proc *p);
166147191Sjkoshystatic void	pmc_cleanup(void);
167147191Sjkoshystatic int	pmc_detach_process(struct proc *p, struct pmc *pm);
168147191Sjkoshystatic int	pmc_detach_one_process(struct proc *p, struct pmc *pm,
169147191Sjkoshy    int flags);
170147191Sjkoshystatic void	pmc_destroy_owner_descriptor(struct pmc_owner *po);
171147191Sjkoshystatic struct pmc_owner *pmc_find_owner_descriptor(struct proc *p);
172147191Sjkoshystatic int	pmc_find_pmc(pmc_id_t pmcid, struct pmc **pm);
173145256Sjkoshystatic struct pmc *pmc_find_pmc_descriptor_in_process(struct pmc_owner *po,
174145256Sjkoshy    pmc_id_t pmc);
175145256Sjkoshystatic struct pmc_process *pmc_find_process_descriptor(struct proc *p,
176145256Sjkoshy    uint32_t mode);
177145774Sjkoshystatic void	pmc_force_context_switch(void);
178145256Sjkoshystatic void	pmc_link_target_process(struct pmc *pm,
179145256Sjkoshy    struct pmc_process *pp);
180147191Sjkoshystatic void	pmc_maybe_remove_owner(struct pmc_owner *po);
181147191Sjkoshystatic void	pmc_process_csw_in(struct thread *td);
182147191Sjkoshystatic void	pmc_process_csw_out(struct thread *td);
183145256Sjkoshystatic void	pmc_process_exit(void *arg, struct proc *p);
184145256Sjkoshystatic void	pmc_process_fork(void *arg, struct proc *p1,
185145256Sjkoshy    struct proc *p2, int n);
186147191Sjkoshystatic void	pmc_process_samples(int cpu);
187147191Sjkoshystatic void	pmc_release_pmc_descriptor(struct pmc *pmc);
188147191Sjkoshystatic void	pmc_remove_owner(struct pmc_owner *po);
189147191Sjkoshystatic void	pmc_remove_process_descriptor(struct pmc_process *pp);
190147191Sjkoshystatic void	pmc_restore_cpu_binding(struct pmc_binding *pb);
191147191Sjkoshystatic void	pmc_save_cpu_binding(struct pmc_binding *pb);
192147191Sjkoshystatic void	pmc_select_cpu(int cpu);
193145256Sjkoshystatic int	pmc_start(struct pmc *pm);
194145256Sjkoshystatic int	pmc_stop(struct pmc *pm);
195147191Sjkoshystatic int	pmc_syscall_handler(struct thread *td, void *syscall_args);
196147191Sjkoshystatic void	pmc_unlink_target_process(struct pmc *pmc,
197147191Sjkoshy    struct pmc_process *pp);
198145256Sjkoshy
199145256Sjkoshy/*
200145256Sjkoshy * Kernel tunables and sysctl(8) interface.
201145256Sjkoshy */
202145256Sjkoshy
203145256SjkoshySYSCTL_NODE(_kern, OID_AUTO, hwpmc, CTLFLAG_RW, 0, "HWPMC parameters");
204145256Sjkoshy
205153110Sru#ifdef	DEBUG
206147191Sjkoshystruct pmc_debugflags pmc_debugflags = PMC_DEBUG_DEFAULT_FLAGS;
207145256Sjkoshychar	pmc_debugstr[PMC_DEBUG_STRSIZE];
208145256SjkoshyTUNABLE_STR(PMC_SYSCTL_NAME_PREFIX "debugflags", pmc_debugstr,
209145256Sjkoshy    sizeof(pmc_debugstr));
210145256SjkoshySYSCTL_PROC(_kern_hwpmc, OID_AUTO, debugflags,
211145256Sjkoshy    CTLTYPE_STRING|CTLFLAG_RW|CTLFLAG_TUN,
212145256Sjkoshy    0, 0, pmc_debugflags_sysctl_handler, "A", "debug flags");
213145256Sjkoshy#endif
214145256Sjkoshy
215145256Sjkoshy/*
216147191Sjkoshy * kern.hwpmc.hashrows -- determines the number of rows in the
217145256Sjkoshy * of the hash table used to look up threads
218145256Sjkoshy */
219145256Sjkoshy
220145256Sjkoshystatic int pmc_hashsize = PMC_HASH_SIZE;
221145256SjkoshyTUNABLE_INT(PMC_SYSCTL_NAME_PREFIX "hashsize", &pmc_hashsize);
222145256SjkoshySYSCTL_INT(_kern_hwpmc, OID_AUTO, hashsize, CTLFLAG_TUN|CTLFLAG_RD,
223145256Sjkoshy    &pmc_hashsize, 0, "rows in hash tables");
224145256Sjkoshy
225145256Sjkoshy/*
226147191Sjkoshy * kern.hwpmc.nsamples --- number of PC samples per CPU
227145256Sjkoshy */
228145256Sjkoshy
229147191Sjkoshystatic int pmc_nsamples = PMC_NSAMPLES;
230147191SjkoshyTUNABLE_INT(PMC_SYSCTL_NAME_PREFIX "nsamples", &pmc_nsamples);
231147191SjkoshySYSCTL_INT(_kern_hwpmc, OID_AUTO, nsamples, CTLFLAG_TUN|CTLFLAG_RD,
232147191Sjkoshy    &pmc_nsamples, 0, "number of PC samples per CPU");
233145256Sjkoshy
234145256Sjkoshy/*
235147191Sjkoshy * kern.hwpmc.mtxpoolsize -- number of mutexes in the mutex pool.
236145256Sjkoshy */
237145256Sjkoshy
238145256Sjkoshystatic int pmc_mtxpool_size = PMC_MTXPOOL_SIZE;
239145256SjkoshyTUNABLE_INT(PMC_SYSCTL_NAME_PREFIX "mtxpoolsize", &pmc_mtxpool_size);
240145256SjkoshySYSCTL_INT(_kern_hwpmc, OID_AUTO, mtxpoolsize, CTLFLAG_TUN|CTLFLAG_RD,
241145256Sjkoshy    &pmc_mtxpool_size, 0, "size of spin mutex pool");
242145256Sjkoshy
243145256Sjkoshy
244145256Sjkoshy/*
245145256Sjkoshy * security.bsd.unprivileged_syspmcs -- allow non-root processes to
246145256Sjkoshy * allocate system-wide PMCs.
247145256Sjkoshy *
248145256Sjkoshy * Allowing unprivileged processes to allocate system PMCs is convenient
249145256Sjkoshy * if system-wide measurements need to be taken concurrently with other
250145256Sjkoshy * per-process measurements.  This feature is turned off by default.
251145256Sjkoshy */
252145256Sjkoshy
253145256SjkoshySYSCTL_DECL(_security_bsd);
254145256Sjkoshy
255145256Sjkoshystatic int pmc_unprivileged_syspmcs = 0;
256145256SjkoshyTUNABLE_INT("security.bsd.unprivileged_syspmcs", &pmc_unprivileged_syspmcs);
257145256SjkoshySYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_syspmcs, CTLFLAG_RW,
258145256Sjkoshy    &pmc_unprivileged_syspmcs, 0,
259145256Sjkoshy    "allow unprivileged process to allocate system PMCs");
260145256Sjkoshy
261147191Sjkoshy/*
262147191Sjkoshy * Hash function.  Discard the lower 2 bits of the pointer since
263147191Sjkoshy * these are always zero for our uses.  The hash multiplier is
264147191Sjkoshy * round((2^LONG_BIT) * ((sqrt(5)-1)/2)).
265147191Sjkoshy */
266145256Sjkoshy
267145256Sjkoshy#if	LONG_BIT == 64
268145256Sjkoshy#define	_PMC_HM		11400714819323198486u
269145256Sjkoshy#elif	LONG_BIT == 32
270145256Sjkoshy#define	_PMC_HM		2654435769u
271145256Sjkoshy#else
272145256Sjkoshy#error 	Must know the size of 'long' to compile
273145256Sjkoshy#endif
274145256Sjkoshy
275145256Sjkoshy#define	PMC_HASH_PTR(P,M)	((((unsigned long) (P) >> 2) * _PMC_HM) & (M))
276145256Sjkoshy
277145256Sjkoshy/*
278145256Sjkoshy * Syscall structures
279145256Sjkoshy */
280145256Sjkoshy
281145256Sjkoshy/* The `sysent' for the new syscall */
282145256Sjkoshystatic struct sysent pmc_sysent = {
283145256Sjkoshy	2,			/* sy_narg */
284145256Sjkoshy	pmc_syscall_handler	/* sy_call */
285145256Sjkoshy};
286145256Sjkoshy
287145256Sjkoshystatic struct syscall_module_data pmc_syscall_mod = {
288145256Sjkoshy	load,
289145256Sjkoshy	NULL,
290145256Sjkoshy	&pmc_syscall_num,
291145256Sjkoshy	&pmc_sysent,
292145256Sjkoshy	{ 0, NULL }
293145256Sjkoshy};
294145256Sjkoshy
295145256Sjkoshystatic moduledata_t pmc_mod = {
296145256Sjkoshy	PMC_MODULE_NAME,
297145256Sjkoshy	syscall_module_handler,
298145256Sjkoshy	&pmc_syscall_mod
299145256Sjkoshy};
300145256Sjkoshy
301145256SjkoshyDECLARE_MODULE(pmc, pmc_mod, SI_SUB_SMP, SI_ORDER_ANY);
302145256SjkoshyMODULE_VERSION(pmc, PMC_VERSION);
303145256Sjkoshy
304153110Sru#ifdef	DEBUG
305147191Sjkoshyenum pmc_dbgparse_state {
306147191Sjkoshy	PMCDS_WS,		/* in whitespace */
307147191Sjkoshy	PMCDS_MAJOR,		/* seen a major keyword */
308147191Sjkoshy	PMCDS_MINOR
309147191Sjkoshy};
310147191Sjkoshy
311145256Sjkoshystatic int
312145256Sjkoshypmc_debugflags_parse(char *newstr, char *fence)
313145256Sjkoshy{
314145313Sjkoshy	char c, *p, *q;
315147191Sjkoshy	struct pmc_debugflags *tmpflags;
316147191Sjkoshy	int error, found, *newbits, tmp;
317147191Sjkoshy	size_t kwlen;
318145256Sjkoshy
319147191Sjkoshy	MALLOC(tmpflags, struct pmc_debugflags *, sizeof(*tmpflags),
320147191Sjkoshy	    M_PMC, M_WAITOK|M_ZERO);
321145256Sjkoshy
322145256Sjkoshy	p = newstr;
323147191Sjkoshy	error = 0;
324145256Sjkoshy
325147191Sjkoshy	for (; p < fence && (c = *p); p++) {
326145256Sjkoshy
327147191Sjkoshy		/* skip white space */
328147191Sjkoshy		if (c == ' ' || c == '\t')
329147191Sjkoshy			continue;
330147191Sjkoshy
331147191Sjkoshy		/* look for a keyword followed by "=" */
332147191Sjkoshy		for (q = p; p < fence && (c = *p) && c != '='; p++)
333147191Sjkoshy			;
334147191Sjkoshy		if (c != '=') {
335147191Sjkoshy			error = EINVAL;
336147191Sjkoshy			goto done;
337145256Sjkoshy		}
338145256Sjkoshy
339147191Sjkoshy		kwlen = p - q;
340147191Sjkoshy		newbits = NULL;
341145256Sjkoshy
342147191Sjkoshy		/* lookup flag group name */
343147191Sjkoshy#define	DBG_SET_FLAG_MAJ(S,F)						\
344147191Sjkoshy		if (kwlen == sizeof(S)-1 && strncmp(q, S, kwlen) == 0)	\
345147191Sjkoshy			newbits = &tmpflags->pdb_ ## F;
346145256Sjkoshy
347147191Sjkoshy		DBG_SET_FLAG_MAJ("cpu",		CPU);
348147191Sjkoshy		DBG_SET_FLAG_MAJ("csw",		CSW);
349147191Sjkoshy		DBG_SET_FLAG_MAJ("logging",	LOG);
350147191Sjkoshy		DBG_SET_FLAG_MAJ("module",	MOD);
351147191Sjkoshy		DBG_SET_FLAG_MAJ("md", 		MDP);
352147191Sjkoshy		DBG_SET_FLAG_MAJ("owner",	OWN);
353147191Sjkoshy		DBG_SET_FLAG_MAJ("pmc",		PMC);
354147191Sjkoshy		DBG_SET_FLAG_MAJ("process",	PRC);
355147191Sjkoshy		DBG_SET_FLAG_MAJ("sampling", 	SAM);
356145256Sjkoshy
357147191Sjkoshy		if (newbits == NULL) {
358147191Sjkoshy			error = EINVAL;
359147191Sjkoshy			goto done;
360145256Sjkoshy		}
361145256Sjkoshy
362147191Sjkoshy		p++;		/* skip the '=' */
363145256Sjkoshy
364147191Sjkoshy		/* Now parse the individual flags */
365147191Sjkoshy		tmp = 0;
366147191Sjkoshy	newflag:
367147191Sjkoshy		for (q = p; p < fence && (c = *p); p++)
368147191Sjkoshy			if (c == ' ' || c == '\t' || c == ',')
369147191Sjkoshy				break;
370147191Sjkoshy
371147191Sjkoshy		/* p == fence or c == ws or c == "," or c == 0 */
372147191Sjkoshy
373147191Sjkoshy		if ((kwlen = p - q) == 0) {
374147191Sjkoshy			*newbits = tmp;
375147191Sjkoshy			continue;
376147191Sjkoshy		}
377147191Sjkoshy
378147191Sjkoshy		found = 0;
379147191Sjkoshy#define	DBG_SET_FLAG_MIN(S,F)						\
380147191Sjkoshy		if (kwlen == sizeof(S)-1 && strncmp(q, S, kwlen) == 0)	\
381147191Sjkoshy			tmp |= found = (1 << PMC_DEBUG_MIN_ ## F)
382147191Sjkoshy
383147191Sjkoshy		/* a '*' denotes all possible flags in the group */
384147191Sjkoshy		if (kwlen == 1 && *q == '*')
385147191Sjkoshy			tmp = found = ~0;
386147191Sjkoshy		/* look for individual flag names */
387147191Sjkoshy		DBG_SET_FLAG_MIN("allocaterow", ALR);
388147191Sjkoshy		DBG_SET_FLAG_MIN("allocate",	ALL);
389147191Sjkoshy		DBG_SET_FLAG_MIN("attach",	ATT);
390147191Sjkoshy		DBG_SET_FLAG_MIN("bind",	BND);
391147191Sjkoshy		DBG_SET_FLAG_MIN("config",	CFG);
392147191Sjkoshy		DBG_SET_FLAG_MIN("exec",	EXC);
393147191Sjkoshy		DBG_SET_FLAG_MIN("exit",	EXT);
394147191Sjkoshy		DBG_SET_FLAG_MIN("find",	FND);
395147191Sjkoshy		DBG_SET_FLAG_MIN("flush",	FLS);
396147191Sjkoshy		DBG_SET_FLAG_MIN("fork",	FRK);
397147191Sjkoshy		DBG_SET_FLAG_MIN("getbuf",	GTB);
398147191Sjkoshy		DBG_SET_FLAG_MIN("hook",	PMH);
399147191Sjkoshy		DBG_SET_FLAG_MIN("init",	INI);
400147191Sjkoshy		DBG_SET_FLAG_MIN("intr",	INT);
401147191Sjkoshy		DBG_SET_FLAG_MIN("linktarget",	TLK);
402147191Sjkoshy		DBG_SET_FLAG_MIN("mayberemove", OMR);
403147191Sjkoshy		DBG_SET_FLAG_MIN("ops",		OPS);
404147191Sjkoshy		DBG_SET_FLAG_MIN("read",	REA);
405147191Sjkoshy		DBG_SET_FLAG_MIN("register",	REG);
406147191Sjkoshy		DBG_SET_FLAG_MIN("release",	REL);
407147191Sjkoshy		DBG_SET_FLAG_MIN("remove",	ORM);
408147191Sjkoshy		DBG_SET_FLAG_MIN("sample",	SAM);
409147191Sjkoshy		DBG_SET_FLAG_MIN("scheduleio",	SIO);
410147191Sjkoshy		DBG_SET_FLAG_MIN("select",	SEL);
411147191Sjkoshy		DBG_SET_FLAG_MIN("signal",	SIG);
412147191Sjkoshy		DBG_SET_FLAG_MIN("swi",		SWI);
413147191Sjkoshy		DBG_SET_FLAG_MIN("swo",		SWO);
414147191Sjkoshy		DBG_SET_FLAG_MIN("start",	STA);
415147191Sjkoshy		DBG_SET_FLAG_MIN("stop",	STO);
416147191Sjkoshy		DBG_SET_FLAG_MIN("syscall",	PMS);
417147191Sjkoshy		DBG_SET_FLAG_MIN("unlinktarget", TUL);
418147191Sjkoshy		DBG_SET_FLAG_MIN("write",	WRI);
419147191Sjkoshy		if (found == 0) {
420147191Sjkoshy			/* unrecognized flag name */
421147191Sjkoshy			error = EINVAL;
422147191Sjkoshy			goto done;
423147191Sjkoshy		}
424147191Sjkoshy
425147191Sjkoshy		if (c == 0 || c == ' ' || c == '\t') {	/* end of flag group */
426147191Sjkoshy			*newbits = tmp;
427147191Sjkoshy			continue;
428147191Sjkoshy		}
429147191Sjkoshy
430147191Sjkoshy		p++;
431147191Sjkoshy		goto newflag;
432145256Sjkoshy	}
433145256Sjkoshy
434147191Sjkoshy	/* save the new flag set */
435147191Sjkoshy	bcopy(tmpflags, &pmc_debugflags, sizeof(pmc_debugflags));
436145256Sjkoshy
437147191Sjkoshy done:
438147191Sjkoshy	FREE(tmpflags, M_PMC);
439147191Sjkoshy	return error;
440145256Sjkoshy}
441145256Sjkoshy
442145256Sjkoshystatic int
443145256Sjkoshypmc_debugflags_sysctl_handler(SYSCTL_HANDLER_ARGS)
444145256Sjkoshy{
445145256Sjkoshy	char *fence, *newstr;
446145256Sjkoshy	int error;
447145256Sjkoshy	unsigned int n;
448145256Sjkoshy
449145256Sjkoshy	(void) arg1; (void) arg2; /* unused parameters */
450145256Sjkoshy
451145256Sjkoshy	n = sizeof(pmc_debugstr);
452145256Sjkoshy	MALLOC(newstr, char *, n, M_PMC, M_ZERO|M_WAITOK);
453147191Sjkoshy	(void) strlcpy(newstr, pmc_debugstr, n);
454145256Sjkoshy
455145256Sjkoshy	error = sysctl_handle_string(oidp, newstr, n, req);
456145256Sjkoshy
457145256Sjkoshy	/* if there is a new string, parse and copy it */
458145256Sjkoshy	if (error == 0 && req->newptr != NULL) {
459147191Sjkoshy		fence = newstr + (n < req->newlen ? n : req->newlen + 1);
460145256Sjkoshy		if ((error = pmc_debugflags_parse(newstr, fence)) == 0)
461145256Sjkoshy			(void) strlcpy(pmc_debugstr, newstr,
462145256Sjkoshy			    sizeof(pmc_debugstr));
463145256Sjkoshy	}
464145256Sjkoshy
465145256Sjkoshy	FREE(newstr, M_PMC);
466145256Sjkoshy
467145256Sjkoshy	return error;
468145256Sjkoshy}
469145256Sjkoshy#endif
470145256Sjkoshy
471145256Sjkoshy/*
472145256Sjkoshy * Concurrency Control
473145256Sjkoshy *
474145256Sjkoshy * The driver manages the following data structures:
475145256Sjkoshy *
476145256Sjkoshy *   - target process descriptors, one per target process
477145256Sjkoshy *   - owner process descriptors (and attached lists), one per owner process
478145256Sjkoshy *   - lookup hash tables for owner and target processes
479145256Sjkoshy *   - PMC descriptors (and attached lists)
480145256Sjkoshy *   - per-cpu hardware state
481145256Sjkoshy *   - the 'hook' variable through which the kernel calls into
482145256Sjkoshy *     this module
483145256Sjkoshy *   - the machine hardware state (managed by the MD layer)
484145256Sjkoshy *
485145256Sjkoshy * These data structures are accessed from:
486145256Sjkoshy *
487145256Sjkoshy * - thread context-switch code
488145256Sjkoshy * - interrupt handlers (possibly on multiple cpus)
489145256Sjkoshy * - kernel threads on multiple cpus running on behalf of user
490145256Sjkoshy *   processes doing system calls
491145256Sjkoshy * - this driver's private kernel threads
492145256Sjkoshy *
493145256Sjkoshy * = Locks and Locking strategy =
494145256Sjkoshy *
495145256Sjkoshy * The driver uses four locking strategies for its operation:
496145256Sjkoshy *
497145256Sjkoshy * - There is a 'global' SX lock "pmc_sx" that is used to protect
498145256Sjkoshy *   the its 'meta-data'.
499145256Sjkoshy *
500145256Sjkoshy *   Calls into the module (via syscall() or by the kernel) start with
501145256Sjkoshy *   this lock being held in exclusive mode.  Depending on the requested
502145256Sjkoshy *   operation, the lock may be downgraded to 'shared' mode to allow
503145256Sjkoshy *   more concurrent readers into the module.
504145256Sjkoshy *
505145256Sjkoshy *   This SX lock is held in exclusive mode for any operations that
506145256Sjkoshy *   modify the linkages between the driver's internal data structures.
507145256Sjkoshy *
508145256Sjkoshy *   The 'pmc_hook' function pointer is also protected by this lock.
509145256Sjkoshy *   It is only examined with the sx lock held in exclusive mode.  The
510145256Sjkoshy *   kernel module is allowed to be unloaded only with the sx lock
511145256Sjkoshy *   held in exclusive mode.  In normal syscall handling, after
512145256Sjkoshy *   acquiring the pmc_sx lock we first check that 'pmc_hook' is
513145256Sjkoshy *   non-null before proceeding.  This prevents races between the
514145256Sjkoshy *   thread unloading the module and other threads seeking to use the
515145256Sjkoshy *   module.
516145256Sjkoshy *
517145256Sjkoshy * - Lookups of target process structures and owner process structures
518145256Sjkoshy *   cannot use the global "pmc_sx" SX lock because these lookups need
519145256Sjkoshy *   to happen during context switches and in other critical sections
520145256Sjkoshy *   where sleeping is not allowed.  We protect these lookup tables
521145256Sjkoshy *   with their own private spin-mutexes, "pmc_processhash_mtx" and
522145256Sjkoshy *   "pmc_ownerhash_mtx".  These are 'leaf' mutexes, in that no other
523145256Sjkoshy *   lock is acquired with these locks held.
524145256Sjkoshy *
525145256Sjkoshy * - Interrupt handlers work in a lock free manner.  At interrupt
526145256Sjkoshy *   time, handlers look at the PMC pointer (phw->phw_pmc) configured
527145256Sjkoshy *   when the PMC was started.  If this pointer is NULL, the interrupt
528145256Sjkoshy *   is ignored after updating driver statistics.  We ensure that this
529145256Sjkoshy *   pointer is set (using an atomic operation if necessary) before the
530145256Sjkoshy *   PMC hardware is started.  Conversely, this pointer is unset atomically
531145256Sjkoshy *   only after the PMC hardware is stopped.
532145256Sjkoshy *
533145256Sjkoshy *   We ensure that everything needed for the operation of an
534145256Sjkoshy *   interrupt handler is available without it needing to acquire any
535145256Sjkoshy *   locks.  We also ensure that a PMC's software state is destroyed only
536145256Sjkoshy *   after the PMC is taken off hardware (on all CPUs).
537145256Sjkoshy *
538145256Sjkoshy * - Context-switch handling with process-private PMCs needs more
539145256Sjkoshy *   care.
540145256Sjkoshy *
541145256Sjkoshy *   A given process may be the target of multiple PMCs.  For example,
542145256Sjkoshy *   PMCATTACH and PMCDETACH may be requested by a process on one CPU
543145256Sjkoshy *   while the target process is running on another.  A PMC could also
544145256Sjkoshy *   be getting released because its owner is exiting.  We tackle
545145256Sjkoshy *   these situations in the following manner:
546145256Sjkoshy *
547145256Sjkoshy *   - each target process structure 'pmc_process' has an array
548145256Sjkoshy *     of 'struct pmc *' pointers, one for each hardware PMC.
549145256Sjkoshy *
550145256Sjkoshy *   - At context switch IN time, each "target" PMC in RUNNING state
551145256Sjkoshy *     gets started on hardware and a pointer to each PMC is copied into
552145256Sjkoshy *     the per-cpu phw array.  The 'runcount' for the PMC is
553145256Sjkoshy *     incremented.
554145256Sjkoshy *
555145256Sjkoshy *   - At context switch OUT time, all process-virtual PMCs are stopped
556145256Sjkoshy *     on hardware.  The saved value is added to the PMCs value field
557145256Sjkoshy *     only if the PMC is in a non-deleted state (the PMCs state could
558145256Sjkoshy *     have changed during the current time slice).
559145256Sjkoshy *
560145256Sjkoshy *     Note that since in-between a switch IN on a processor and a switch
561145256Sjkoshy *     OUT, the PMC could have been released on another CPU.  Therefore
562145256Sjkoshy *     context switch OUT always looks at the hardware state to turn
563145256Sjkoshy *     OFF PMCs and will update a PMC's saved value only if reachable
564145256Sjkoshy *     from the target process record.
565145256Sjkoshy *
566145256Sjkoshy *   - OP PMCRELEASE could be called on a PMC at any time (the PMC could
567145256Sjkoshy *     be attached to many processes at the time of the call and could
568145256Sjkoshy *     be active on multiple CPUs).
569145256Sjkoshy *
570145256Sjkoshy *     We prevent further scheduling of the PMC by marking it as in
571145256Sjkoshy *     state 'DELETED'.  If the runcount of the PMC is non-zero then
572145256Sjkoshy *     this PMC is currently running on a CPU somewhere.  The thread
573145256Sjkoshy *     doing the PMCRELEASE operation waits by repeatedly doing an
574145256Sjkoshy *     tsleep() till the runcount comes to zero.
575145256Sjkoshy *
576145256Sjkoshy */
577145256Sjkoshy
578145256Sjkoshy/*
579145256Sjkoshy * save the cpu binding of the current kthread
580145256Sjkoshy */
581145256Sjkoshy
582145256Sjkoshystatic void
583145256Sjkoshypmc_save_cpu_binding(struct pmc_binding *pb)
584145256Sjkoshy{
585145256Sjkoshy	PMCDBG(CPU,BND,2, "%s", "save-cpu");
586145256Sjkoshy	mtx_lock_spin(&sched_lock);
587145256Sjkoshy	pb->pb_bound = sched_is_bound(curthread);
588145256Sjkoshy	pb->pb_cpu   = curthread->td_oncpu;
589145256Sjkoshy	mtx_unlock_spin(&sched_lock);
590145256Sjkoshy	PMCDBG(CPU,BND,2, "save-cpu cpu=%d", pb->pb_cpu);
591145256Sjkoshy}
592145256Sjkoshy
593145256Sjkoshy/*
594145256Sjkoshy * restore the cpu binding of the current thread
595145256Sjkoshy */
596145256Sjkoshy
597145256Sjkoshystatic void
598145256Sjkoshypmc_restore_cpu_binding(struct pmc_binding *pb)
599145256Sjkoshy{
600145256Sjkoshy	PMCDBG(CPU,BND,2, "restore-cpu curcpu=%d restore=%d",
601145256Sjkoshy	    curthread->td_oncpu, pb->pb_cpu);
602145256Sjkoshy	mtx_lock_spin(&sched_lock);
603145256Sjkoshy	if (pb->pb_bound)
604145256Sjkoshy		sched_bind(curthread, pb->pb_cpu);
605145256Sjkoshy	else
606145256Sjkoshy		sched_unbind(curthread);
607145256Sjkoshy	mtx_unlock_spin(&sched_lock);
608145256Sjkoshy	PMCDBG(CPU,BND,2, "%s", "restore-cpu done");
609145256Sjkoshy}
610145256Sjkoshy
611145256Sjkoshy/*
612145256Sjkoshy * move execution over the specified cpu and bind it there.
613145256Sjkoshy */
614145256Sjkoshy
615145256Sjkoshystatic void
616145256Sjkoshypmc_select_cpu(int cpu)
617145256Sjkoshy{
618145256Sjkoshy	KASSERT(cpu >= 0 && cpu < mp_ncpus,
619145256Sjkoshy	    ("[pmc,%d] bad cpu number %d", __LINE__, cpu));
620145256Sjkoshy
621145256Sjkoshy	/* never move to a disabled CPU */
622145256Sjkoshy	KASSERT(pmc_cpu_is_disabled(cpu) == 0, ("[pmc,%d] selecting "
623145256Sjkoshy	    "disabled CPU %d", __LINE__, cpu));
624145256Sjkoshy
625145256Sjkoshy	PMCDBG(CPU,SEL,2, "select-cpu cpu=%d", cpu);
626145256Sjkoshy	mtx_lock_spin(&sched_lock);
627145256Sjkoshy	sched_bind(curthread, cpu);
628145256Sjkoshy	mtx_unlock_spin(&sched_lock);
629145256Sjkoshy
630145256Sjkoshy	KASSERT(curthread->td_oncpu == cpu,
631145256Sjkoshy	    ("[pmc,%d] CPU not bound [cpu=%d, curr=%d]", __LINE__,
632145256Sjkoshy		cpu, curthread->td_oncpu));
633145256Sjkoshy
634145256Sjkoshy	PMCDBG(CPU,SEL,2, "select-cpu cpu=%d ok", cpu);
635145256Sjkoshy}
636145256Sjkoshy
637145256Sjkoshy/*
638145774Sjkoshy * Force a context switch.
639145774Sjkoshy *
640145774Sjkoshy * We do this by tsleep'ing for 1 tick -- invoking mi_switch() is not
641145774Sjkoshy * guaranteed to force a context switch.
642145774Sjkoshy */
643145774Sjkoshy
644145774Sjkoshystatic void
645145774Sjkoshypmc_force_context_switch(void)
646145774Sjkoshy{
647145774Sjkoshy	u_char	curpri;
648145774Sjkoshy
649145774Sjkoshy	mtx_lock_spin(&sched_lock);
650145774Sjkoshy	curpri = curthread->td_priority;
651145774Sjkoshy	mtx_unlock_spin(&sched_lock);
652145774Sjkoshy
653145774Sjkoshy	(void) tsleep((void *) pmc_force_context_switch, curpri,
654145774Sjkoshy	    "pmcctx", 1);
655145774Sjkoshy
656145774Sjkoshy}
657145774Sjkoshy
658145774Sjkoshy/*
659147191Sjkoshy * Get the file name for an executable.  This is a simple wrapper
660147191Sjkoshy * around vn_fullpath(9).
661145256Sjkoshy */
662145256Sjkoshy
663147191Sjkoshystatic void
664147708Sjkoshypmc_getfilename(struct vnode *v, char **fullpath, char **freepath)
665145256Sjkoshy{
666145256Sjkoshy	struct thread *td;
667145256Sjkoshy
668147191Sjkoshy	td = curthread;
669147191Sjkoshy	*fullpath = "unknown";
670147191Sjkoshy	*freepath = NULL;
671148088Sjkoshy	vn_lock(v, LK_CANRECURSE | LK_EXCLUSIVE | LK_RETRY, td);
672147708Sjkoshy	vn_fullpath(td, v, fullpath, freepath);
673147708Sjkoshy	VOP_UNLOCK(v, 0, td);
674145256Sjkoshy}
675145256Sjkoshy
676145256Sjkoshy/*
677145256Sjkoshy * remove an process owning PMCs
678145256Sjkoshy */
679145256Sjkoshy
680145256Sjkoshyvoid
681145256Sjkoshypmc_remove_owner(struct pmc_owner *po)
682145256Sjkoshy{
683147191Sjkoshy	struct pmc *pm, *tmp;
684145256Sjkoshy
685145256Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
686145256Sjkoshy
687145256Sjkoshy	PMCDBG(OWN,ORM,1, "remove-owner po=%p", po);
688145256Sjkoshy
689145256Sjkoshy	/* Remove descriptor from the owner hash table */
690145256Sjkoshy	LIST_REMOVE(po, po_next);
691145256Sjkoshy
692147191Sjkoshy	/* release all owned PMC descriptors */
693147191Sjkoshy	LIST_FOREACH_SAFE(pm, &po->po_pmcs, pm_next, tmp) {
694147191Sjkoshy		PMCDBG(OWN,ORM,2, "pmc=%p", pm);
695147191Sjkoshy		KASSERT(pm->pm_owner == po,
696147191Sjkoshy		    ("[pmc,%d] owner %p != po %p", __LINE__, pm->pm_owner, po));
697145256Sjkoshy
698147191Sjkoshy		pmc_release_pmc_descriptor(pm);	/* will unlink from the list */
699145256Sjkoshy	}
700145256Sjkoshy
701147191Sjkoshy	KASSERT(po->po_sscount == 0,
702147191Sjkoshy	    ("[pmc,%d] SS count not zero", __LINE__));
703145256Sjkoshy	KASSERT(LIST_EMPTY(&po->po_pmcs),
704147191Sjkoshy	    ("[pmc,%d] PMC list not empty", __LINE__));
705145256Sjkoshy
706147191Sjkoshy	/* de-configure the log file if present */
707145774Sjkoshy	if (po->po_flags & PMC_PO_OWNS_LOGFILE)
708147191Sjkoshy		pmclog_deconfigure_log(po);
709145256Sjkoshy}
710145256Sjkoshy
711145256Sjkoshy/*
712145256Sjkoshy * remove an owner process record if all conditions are met.
713145256Sjkoshy */
714145256Sjkoshy
715145256Sjkoshystatic void
716145256Sjkoshypmc_maybe_remove_owner(struct pmc_owner *po)
717145256Sjkoshy{
718145256Sjkoshy
719145256Sjkoshy	PMCDBG(OWN,OMR,1, "maybe-remove-owner po=%p", po);
720145256Sjkoshy
721145256Sjkoshy	/*
722145256Sjkoshy	 * Remove owner record if
723145256Sjkoshy	 * - this process does not own any PMCs
724145256Sjkoshy	 * - this process has not allocated a system-wide sampling buffer
725145256Sjkoshy	 */
726145256Sjkoshy
727145256Sjkoshy	if (LIST_EMPTY(&po->po_pmcs) &&
728145774Sjkoshy	    ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0)) {
729145256Sjkoshy		pmc_remove_owner(po);
730147191Sjkoshy		pmc_destroy_owner_descriptor(po);
731145256Sjkoshy	}
732145256Sjkoshy}
733145256Sjkoshy
734145256Sjkoshy/*
735145256Sjkoshy * Add an association between a target process and a PMC.
736145256Sjkoshy */
737145256Sjkoshy
738145256Sjkoshystatic void
739145256Sjkoshypmc_link_target_process(struct pmc *pm, struct pmc_process *pp)
740145256Sjkoshy{
741145256Sjkoshy	int ri;
742145256Sjkoshy	struct pmc_target *pt;
743145256Sjkoshy
744145256Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
745145256Sjkoshy
746145256Sjkoshy	KASSERT(pm != NULL && pp != NULL,
747145256Sjkoshy	    ("[pmc,%d] Null pm %p or pp %p", __LINE__, pm, pp));
748147191Sjkoshy	KASSERT(PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm)),
749147191Sjkoshy	    ("[pmc,%d] Attaching a non-process-virtual pmc=%p to pid=%d",
750147191Sjkoshy		__LINE__, pm, pp->pp_proc->p_pid));
751145256Sjkoshy	KASSERT(pp->pp_refcnt >= 0 && pp->pp_refcnt < ((int) md->pmd_npmc - 1),
752145256Sjkoshy	    ("[pmc,%d] Illegal reference count %d for process record %p",
753145256Sjkoshy		__LINE__, pp->pp_refcnt, (void *) pp));
754145256Sjkoshy
755145774Sjkoshy	ri = PMC_TO_ROWINDEX(pm);
756145256Sjkoshy
757145256Sjkoshy	PMCDBG(PRC,TLK,1, "link-target pmc=%p ri=%d pmc-process=%p",
758145256Sjkoshy	    pm, ri, pp);
759145256Sjkoshy
760153110Sru#ifdef	DEBUG
761145256Sjkoshy	LIST_FOREACH(pt, &pm->pm_targets, pt_next)
762145256Sjkoshy	    if (pt->pt_process == pp)
763145256Sjkoshy		    KASSERT(0, ("[pmc,%d] pp %p already in pmc %p targets",
764145256Sjkoshy				__LINE__, pp, pm));
765145256Sjkoshy#endif
766145256Sjkoshy
767145256Sjkoshy	MALLOC(pt, struct pmc_target *, sizeof(struct pmc_target),
768145256Sjkoshy	    M_PMC, M_ZERO|M_WAITOK);
769145256Sjkoshy
770145256Sjkoshy	pt->pt_process = pp;
771145256Sjkoshy
772145256Sjkoshy	LIST_INSERT_HEAD(&pm->pm_targets, pt, pt_next);
773145256Sjkoshy
774148067Sjhb	atomic_store_rel_ptr((uintptr_t *)&pp->pp_pmcs[ri].pp_pmc,
775148067Sjhb	    (uintptr_t)pm);
776145256Sjkoshy
777145615Sjkoshy	if (pm->pm_owner->po_owner == pp->pp_proc)
778145774Sjkoshy		pm->pm_flags |= PMC_F_ATTACHED_TO_OWNER;
779145615Sjkoshy
780147191Sjkoshy	/*
781147191Sjkoshy	 * Initialize the per-process values at this row index.
782147191Sjkoshy	 */
783147191Sjkoshy	pp->pp_pmcs[ri].pp_pmcval = PMC_TO_MODE(pm) == PMC_MODE_TS ?
784147191Sjkoshy	    pm->pm_sc.pm_reloadcount : 0;
785147191Sjkoshy
786145256Sjkoshy	pp->pp_refcnt++;
787145256Sjkoshy
788145256Sjkoshy}
789145256Sjkoshy
790145256Sjkoshy/*
791145256Sjkoshy * Removes the association between a target process and a PMC.
792145256Sjkoshy */
793145256Sjkoshy
794145256Sjkoshystatic void
795145256Sjkoshypmc_unlink_target_process(struct pmc *pm, struct pmc_process *pp)
796145256Sjkoshy{
797145256Sjkoshy	int ri;
798147191Sjkoshy	struct proc *p;
799145256Sjkoshy	struct pmc_target *ptgt;
800145256Sjkoshy
801145256Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
802145256Sjkoshy
803145256Sjkoshy	KASSERT(pm != NULL && pp != NULL,
804145256Sjkoshy	    ("[pmc,%d] Null pm %p or pp %p", __LINE__, pm, pp));
805145256Sjkoshy
806145256Sjkoshy	KASSERT(pp->pp_refcnt >= 1 && pp->pp_refcnt < (int) md->pmd_npmc,
807145256Sjkoshy	    ("[pmc,%d] Illegal ref count %d on process record %p",
808145256Sjkoshy		__LINE__, pp->pp_refcnt, (void *) pp));
809145256Sjkoshy
810145774Sjkoshy	ri = PMC_TO_ROWINDEX(pm);
811145256Sjkoshy
812145256Sjkoshy	PMCDBG(PRC,TUL,1, "unlink-target pmc=%p ri=%d pmc-process=%p",
813145256Sjkoshy	    pm, ri, pp);
814145256Sjkoshy
815145256Sjkoshy	KASSERT(pp->pp_pmcs[ri].pp_pmc == pm,
816145256Sjkoshy	    ("[pmc,%d] PMC ri %d mismatch pmc %p pp->[ri] %p", __LINE__,
817145256Sjkoshy		ri, pm, pp->pp_pmcs[ri].pp_pmc));
818145256Sjkoshy
819145256Sjkoshy	pp->pp_pmcs[ri].pp_pmc = NULL;
820145256Sjkoshy	pp->pp_pmcs[ri].pp_pmcval = (pmc_value_t) 0;
821145256Sjkoshy
822145774Sjkoshy	/* Remove owner-specific flags */
823145774Sjkoshy	if (pm->pm_owner->po_owner == pp->pp_proc) {
824145774Sjkoshy		pp->pp_flags &= ~PMC_PP_ENABLE_MSR_ACCESS;
825145774Sjkoshy		pm->pm_flags &= ~PMC_F_ATTACHED_TO_OWNER;
826145774Sjkoshy	}
827145615Sjkoshy
828145256Sjkoshy	pp->pp_refcnt--;
829145256Sjkoshy
830145256Sjkoshy	/* Remove the target process from the PMC structure */
831145256Sjkoshy	LIST_FOREACH(ptgt, &pm->pm_targets, pt_next)
832145256Sjkoshy		if (ptgt->pt_process == pp)
833145256Sjkoshy			break;
834145256Sjkoshy
835145256Sjkoshy	KASSERT(ptgt != NULL, ("[pmc,%d] process %p (pp: %p) not found "
836145256Sjkoshy		    "in pmc %p", __LINE__, pp->pp_proc, pp, pm));
837145256Sjkoshy
838145256Sjkoshy	LIST_REMOVE(ptgt, pt_next);
839145256Sjkoshy	FREE(ptgt, M_PMC);
840145256Sjkoshy
841147191Sjkoshy	/* if the PMC now lacks targets, send the owner a SIGIO */
842147191Sjkoshy	if (LIST_EMPTY(&pm->pm_targets)) {
843147191Sjkoshy		p = pm->pm_owner->po_owner;
844147191Sjkoshy		PROC_LOCK(p);
845147191Sjkoshy		psignal(p, SIGIO);
846147191Sjkoshy		PROC_UNLOCK(p);
847145256Sjkoshy
848147191Sjkoshy		PMCDBG(PRC,SIG,2, "signalling proc=%p signal=%d", p,
849147191Sjkoshy		    SIGIO);
850145256Sjkoshy	}
851145256Sjkoshy}
852145256Sjkoshy
853145256Sjkoshy/*
854145256Sjkoshy * Check if PMC 'pm' may be attached to target process 't'.
855145256Sjkoshy */
856145256Sjkoshy
857145256Sjkoshystatic int
858145256Sjkoshypmc_can_attach(struct pmc *pm, struct proc *t)
859145256Sjkoshy{
860145256Sjkoshy	struct proc *o;		/* pmc owner */
861145256Sjkoshy	struct ucred *oc, *tc;	/* owner, target credentials */
862145256Sjkoshy	int decline_attach, i;
863145256Sjkoshy
864145256Sjkoshy	/*
865145256Sjkoshy	 * A PMC's owner can always attach that PMC to itself.
866145256Sjkoshy	 */
867145256Sjkoshy
868145256Sjkoshy	if ((o = pm->pm_owner->po_owner) == t)
869145256Sjkoshy		return 0;
870145256Sjkoshy
871145256Sjkoshy	PROC_LOCK(o);
872145256Sjkoshy	oc = o->p_ucred;
873145256Sjkoshy	crhold(oc);
874145256Sjkoshy	PROC_UNLOCK(o);
875145256Sjkoshy
876145256Sjkoshy	PROC_LOCK(t);
877145256Sjkoshy	tc = t->p_ucred;
878145256Sjkoshy	crhold(tc);
879145256Sjkoshy	PROC_UNLOCK(t);
880145256Sjkoshy
881145256Sjkoshy	/*
882145256Sjkoshy	 * The effective uid of the PMC owner should match at least one
883145256Sjkoshy	 * of the {effective,real,saved} uids of the target process.
884145256Sjkoshy	 */
885145256Sjkoshy
886145256Sjkoshy	decline_attach = oc->cr_uid != tc->cr_uid &&
887145256Sjkoshy	    oc->cr_uid != tc->cr_svuid &&
888145256Sjkoshy	    oc->cr_uid != tc->cr_ruid;
889145256Sjkoshy
890145256Sjkoshy	/*
891145256Sjkoshy	 * Every one of the target's group ids, must be in the owner's
892145256Sjkoshy	 * group list.
893145256Sjkoshy	 */
894145256Sjkoshy	for (i = 0; !decline_attach && i < tc->cr_ngroups; i++)
895145256Sjkoshy		decline_attach = !groupmember(tc->cr_groups[i], oc);
896145256Sjkoshy
897145256Sjkoshy	/* check the read and saved gids too */
898145256Sjkoshy	if (decline_attach == 0)
899145256Sjkoshy		decline_attach = !groupmember(tc->cr_rgid, oc) ||
900145256Sjkoshy		    !groupmember(tc->cr_svgid, oc);
901145256Sjkoshy
902145256Sjkoshy	crfree(tc);
903145256Sjkoshy	crfree(oc);
904145256Sjkoshy
905145256Sjkoshy	return !decline_attach;
906145256Sjkoshy}
907145256Sjkoshy
908145256Sjkoshy/*
909145256Sjkoshy * Attach a process to a PMC.
910145256Sjkoshy */
911145256Sjkoshy
912145256Sjkoshystatic int
913145256Sjkoshypmc_attach_one_process(struct proc *p, struct pmc *pm)
914145256Sjkoshy{
915145256Sjkoshy	int ri;
916147191Sjkoshy	char *fullpath, *freepath;
917145256Sjkoshy	struct pmc_process	*pp;
918145256Sjkoshy
919145256Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
920145256Sjkoshy
921145256Sjkoshy	PMCDBG(PRC,ATT,2, "attach-one pm=%p ri=%d proc=%p (%d, %s)", pm,
922145774Sjkoshy	    PMC_TO_ROWINDEX(pm), p, p->p_pid, p->p_comm);
923145256Sjkoshy
924145256Sjkoshy	/*
925145256Sjkoshy	 * Locate the process descriptor corresponding to process 'p',
926145256Sjkoshy	 * allocating space as needed.
927145256Sjkoshy	 *
928145256Sjkoshy	 * Verify that rowindex 'pm_rowindex' is free in the process
929145256Sjkoshy	 * descriptor.
930145256Sjkoshy	 *
931145256Sjkoshy	 * If not, allocate space for a descriptor and link the
932145256Sjkoshy	 * process descriptor and PMC.
933145256Sjkoshy	 */
934145774Sjkoshy	ri = PMC_TO_ROWINDEX(pm);
935145256Sjkoshy
936145256Sjkoshy	if ((pp = pmc_find_process_descriptor(p, PMC_FLAG_ALLOCATE)) == NULL)
937145256Sjkoshy		return ENOMEM;
938145256Sjkoshy
939145256Sjkoshy	if (pp->pp_pmcs[ri].pp_pmc == pm) /* already present at slot [ri] */
940145256Sjkoshy		return EEXIST;
941145256Sjkoshy
942145256Sjkoshy	if (pp->pp_pmcs[ri].pp_pmc != NULL)
943145256Sjkoshy		return EBUSY;
944145256Sjkoshy
945145256Sjkoshy	pmc_link_target_process(pm, pp);
946145256Sjkoshy
947147191Sjkoshy	if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)) &&
948147191Sjkoshy	    (pm->pm_flags & PMC_F_ATTACHED_TO_OWNER) == 0)
949147191Sjkoshy		pm->pm_flags |= PMC_F_NEEDS_LOGFILE;
950147191Sjkoshy
951147191Sjkoshy	pm->pm_flags |= PMC_F_ATTACH_DONE; /* mark as attached */
952147191Sjkoshy
953147191Sjkoshy	/* issue an attach event to a configured log file */
954147191Sjkoshy	if (pm->pm_owner->po_flags & PMC_PO_OWNS_LOGFILE) {
955147708Sjkoshy		pmc_getfilename(p->p_textvp, &fullpath, &freepath);
956147191Sjkoshy		pmclog_process_pmcattach(pm, p->p_pid, fullpath);
957147191Sjkoshy		if (freepath)
958147191Sjkoshy			FREE(freepath, M_TEMP);
959147191Sjkoshy	}
960145256Sjkoshy	/* mark process as using HWPMCs */
961145256Sjkoshy	PROC_LOCK(p);
962145256Sjkoshy	p->p_flag |= P_HWPMC;
963145256Sjkoshy	PROC_UNLOCK(p);
964145256Sjkoshy
965145256Sjkoshy	return 0;
966145256Sjkoshy}
967145256Sjkoshy
968145256Sjkoshy/*
969145256Sjkoshy * Attach a process and optionally its children
970145256Sjkoshy */
971145256Sjkoshy
972145256Sjkoshystatic int
973145256Sjkoshypmc_attach_process(struct proc *p, struct pmc *pm)
974145256Sjkoshy{
975145256Sjkoshy	int error;
976145256Sjkoshy	struct proc *top;
977145256Sjkoshy
978145256Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
979145256Sjkoshy
980145256Sjkoshy	PMCDBG(PRC,ATT,1, "attach pm=%p ri=%d proc=%p (%d, %s)", pm,
981145774Sjkoshy	    PMC_TO_ROWINDEX(pm), p, p->p_pid, p->p_comm);
982145256Sjkoshy
983145774Sjkoshy
984145774Sjkoshy	/*
985145774Sjkoshy	 * If this PMC successfully allowed a GETMSR operation
986145774Sjkoshy	 * in the past, disallow further ATTACHes.
987145774Sjkoshy	 */
988145774Sjkoshy
989145774Sjkoshy	if ((pm->pm_flags & PMC_PP_ENABLE_MSR_ACCESS) != 0)
990145774Sjkoshy		return EPERM;
991145774Sjkoshy
992145256Sjkoshy	if ((pm->pm_flags & PMC_F_DESCENDANTS) == 0)
993145256Sjkoshy		return pmc_attach_one_process(p, pm);
994145256Sjkoshy
995145256Sjkoshy	/*
996145256Sjkoshy	 * Traverse all child processes, attaching them to
997145256Sjkoshy	 * this PMC.
998145256Sjkoshy	 */
999145256Sjkoshy
1000145256Sjkoshy	sx_slock(&proctree_lock);
1001145256Sjkoshy
1002145256Sjkoshy	top = p;
1003145256Sjkoshy
1004145256Sjkoshy	for (;;) {
1005145256Sjkoshy		if ((error = pmc_attach_one_process(p, pm)) != 0)
1006145256Sjkoshy			break;
1007145256Sjkoshy		if (!LIST_EMPTY(&p->p_children))
1008145256Sjkoshy			p = LIST_FIRST(&p->p_children);
1009145256Sjkoshy		else for (;;) {
1010145256Sjkoshy			if (p == top)
1011145256Sjkoshy				goto done;
1012145256Sjkoshy			if (LIST_NEXT(p, p_sibling)) {
1013145256Sjkoshy				p = LIST_NEXT(p, p_sibling);
1014145256Sjkoshy				break;
1015145256Sjkoshy			}
1016145256Sjkoshy			p = p->p_pptr;
1017145256Sjkoshy		}
1018145256Sjkoshy	}
1019145256Sjkoshy
1020145256Sjkoshy	if (error)
1021145256Sjkoshy		(void) pmc_detach_process(top, pm);
1022145256Sjkoshy
1023145256Sjkoshy done:
1024145256Sjkoshy	sx_sunlock(&proctree_lock);
1025145256Sjkoshy	return error;
1026145256Sjkoshy}
1027145256Sjkoshy
1028145256Sjkoshy/*
1029145256Sjkoshy * Detach a process from a PMC.  If there are no other PMCs tracking
1030145256Sjkoshy * this process, remove the process structure from its hash table.  If
1031145256Sjkoshy * 'flags' contains PMC_FLAG_REMOVE, then free the process structure.
1032145256Sjkoshy */
1033145256Sjkoshy
1034145256Sjkoshystatic int
1035145256Sjkoshypmc_detach_one_process(struct proc *p, struct pmc *pm, int flags)
1036145256Sjkoshy{
1037145256Sjkoshy	int ri;
1038145256Sjkoshy	struct pmc_process *pp;
1039145256Sjkoshy
1040145256Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
1041145256Sjkoshy
1042145256Sjkoshy	KASSERT(pm != NULL,
1043145256Sjkoshy	    ("[pmc,%d] null pm pointer", __LINE__));
1044145256Sjkoshy
1045145774Sjkoshy	ri = PMC_TO_ROWINDEX(pm);
1046145774Sjkoshy
1047145256Sjkoshy	PMCDBG(PRC,ATT,2, "detach-one pm=%p ri=%d proc=%p (%d, %s) flags=0x%x",
1048145774Sjkoshy	    pm, ri, p, p->p_pid, p->p_comm, flags);
1049145256Sjkoshy
1050145256Sjkoshy	if ((pp = pmc_find_process_descriptor(p, 0)) == NULL)
1051145256Sjkoshy		return ESRCH;
1052145256Sjkoshy
1053145256Sjkoshy	if (pp->pp_pmcs[ri].pp_pmc != pm)
1054145256Sjkoshy		return EINVAL;
1055145256Sjkoshy
1056145256Sjkoshy	pmc_unlink_target_process(pm, pp);
1057145256Sjkoshy
1058147191Sjkoshy	/* Issue a detach entry if a log file is configured */
1059147191Sjkoshy	if (pm->pm_owner->po_flags & PMC_PO_OWNS_LOGFILE)
1060147191Sjkoshy		pmclog_process_pmcdetach(pm, p->p_pid);
1061147191Sjkoshy
1062145256Sjkoshy	/*
1063145256Sjkoshy	 * If there are no PMCs targetting this process, we remove its
1064145256Sjkoshy	 * descriptor from the target hash table and unset the P_HWPMC
1065145256Sjkoshy	 * flag in the struct proc.
1066145256Sjkoshy	 */
1067145256Sjkoshy	KASSERT(pp->pp_refcnt >= 0 && pp->pp_refcnt < (int) md->pmd_npmc,
1068145256Sjkoshy	    ("[pmc,%d] Illegal refcnt %d for process struct %p",
1069145256Sjkoshy		__LINE__, pp->pp_refcnt, pp));
1070145256Sjkoshy
1071145256Sjkoshy	if (pp->pp_refcnt != 0)	/* still a target of some PMC */
1072145256Sjkoshy		return 0;
1073145256Sjkoshy
1074145256Sjkoshy	pmc_remove_process_descriptor(pp);
1075145256Sjkoshy
1076145256Sjkoshy	if (flags & PMC_FLAG_REMOVE)
1077145256Sjkoshy		FREE(pp, M_PMC);
1078145256Sjkoshy
1079145256Sjkoshy	PROC_LOCK(p);
1080145256Sjkoshy	p->p_flag &= ~P_HWPMC;
1081145256Sjkoshy	PROC_UNLOCK(p);
1082145256Sjkoshy
1083145256Sjkoshy	return 0;
1084145256Sjkoshy}
1085145256Sjkoshy
1086145256Sjkoshy/*
1087145256Sjkoshy * Detach a process and optionally its descendants from a PMC.
1088145256Sjkoshy */
1089145256Sjkoshy
1090145256Sjkoshystatic int
1091145256Sjkoshypmc_detach_process(struct proc *p, struct pmc *pm)
1092145256Sjkoshy{
1093145256Sjkoshy	struct proc *top;
1094145256Sjkoshy
1095145256Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
1096145256Sjkoshy
1097145256Sjkoshy	PMCDBG(PRC,ATT,1, "detach pm=%p ri=%d proc=%p (%d, %s)", pm,
1098145774Sjkoshy	    PMC_TO_ROWINDEX(pm), p, p->p_pid, p->p_comm);
1099145256Sjkoshy
1100145256Sjkoshy	if ((pm->pm_flags & PMC_F_DESCENDANTS) == 0)
1101145256Sjkoshy		return pmc_detach_one_process(p, pm, PMC_FLAG_REMOVE);
1102145256Sjkoshy
1103145256Sjkoshy	/*
1104145256Sjkoshy	 * Traverse all children, detaching them from this PMC.  We
1105145256Sjkoshy	 * ignore errors since we could be detaching a PMC from a
1106145256Sjkoshy	 * partially attached proc tree.
1107145256Sjkoshy	 */
1108145256Sjkoshy
1109145256Sjkoshy	sx_slock(&proctree_lock);
1110145256Sjkoshy
1111145256Sjkoshy	top = p;
1112145256Sjkoshy
1113145256Sjkoshy	for (;;) {
1114145256Sjkoshy		(void) pmc_detach_one_process(p, pm, PMC_FLAG_REMOVE);
1115145256Sjkoshy
1116145256Sjkoshy		if (!LIST_EMPTY(&p->p_children))
1117145256Sjkoshy			p = LIST_FIRST(&p->p_children);
1118145256Sjkoshy		else for (;;) {
1119145256Sjkoshy			if (p == top)
1120145256Sjkoshy				goto done;
1121145256Sjkoshy			if (LIST_NEXT(p, p_sibling)) {
1122145256Sjkoshy				p = LIST_NEXT(p, p_sibling);
1123145256Sjkoshy				break;
1124145256Sjkoshy			}
1125145256Sjkoshy			p = p->p_pptr;
1126145256Sjkoshy		}
1127145256Sjkoshy	}
1128145256Sjkoshy
1129145256Sjkoshy done:
1130145256Sjkoshy	sx_sunlock(&proctree_lock);
1131147191Sjkoshy
1132147191Sjkoshy	if (LIST_EMPTY(&pm->pm_targets))
1133147191Sjkoshy		pm->pm_flags &= ~PMC_F_ATTACH_DONE;
1134147191Sjkoshy
1135145256Sjkoshy	return 0;
1136145256Sjkoshy}
1137145256Sjkoshy
1138147191Sjkoshy
1139145256Sjkoshy/*
1140147191Sjkoshy * Thread context switch IN
1141145256Sjkoshy */
1142145256Sjkoshy
1143147191Sjkoshystatic void
1144147191Sjkoshypmc_process_csw_in(struct thread *td)
1145147191Sjkoshy{
1146147191Sjkoshy	int cpu;
1147147191Sjkoshy	unsigned int ri;
1148147191Sjkoshy	struct pmc *pm;
1149147191Sjkoshy	struct proc *p;
1150147191Sjkoshy	struct pmc_cpu *pc;
1151147191Sjkoshy	struct pmc_hw *phw;
1152147191Sjkoshy	struct pmc_process *pp;
1153147191Sjkoshy	pmc_value_t newvalue;
1154145256Sjkoshy
1155147191Sjkoshy	p = td->td_proc;
1156145256Sjkoshy
1157147191Sjkoshy	if ((pp = pmc_find_process_descriptor(p, PMC_FLAG_NONE)) == NULL)
1158147191Sjkoshy		return;
1159145256Sjkoshy
1160147191Sjkoshy	KASSERT(pp->pp_proc == td->td_proc,
1161147191Sjkoshy	    ("[pmc,%d] not my thread state", __LINE__));
1162145256Sjkoshy
1163147191Sjkoshy	critical_enter(); /* no preemption from this point */
1164145256Sjkoshy
1165147191Sjkoshy	cpu = PCPU_GET(cpuid); /* td->td_oncpu is invalid */
1166145256Sjkoshy
1167147191Sjkoshy	PMCDBG(CSW,SWI,1, "cpu=%d proc=%p (%d, %s) pp=%p", cpu, p,
1168147191Sjkoshy	    p->p_pid, p->p_comm, pp);
1169145256Sjkoshy
1170147191Sjkoshy	KASSERT(cpu >= 0 && cpu < mp_ncpus,
1171147191Sjkoshy	    ("[pmc,%d] wierd CPU id %d", __LINE__, cpu));
1172145256Sjkoshy
1173147191Sjkoshy	pc = pmc_pcpu[cpu];
1174145256Sjkoshy
1175147191Sjkoshy	for (ri = 0; ri < md->pmd_npmc; ri++) {
1176145256Sjkoshy
1177147191Sjkoshy		if ((pm = pp->pp_pmcs[ri].pp_pmc) == NULL)
1178147191Sjkoshy			continue;
1179147191Sjkoshy
1180147191Sjkoshy		KASSERT(PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm)),
1181147191Sjkoshy		    ("[pmc,%d] Target PMC in non-virtual mode (%d)",
1182147191Sjkoshy			__LINE__, PMC_TO_MODE(pm)));
1183147191Sjkoshy
1184147191Sjkoshy		KASSERT(PMC_TO_ROWINDEX(pm) == ri,
1185147191Sjkoshy		    ("[pmc,%d] Row index mismatch pmc %d != ri %d",
1186147191Sjkoshy			__LINE__, PMC_TO_ROWINDEX(pm), ri));
1187147191Sjkoshy
1188145256Sjkoshy		/*
1189147191Sjkoshy		 * Only PMCs that are marked as 'RUNNING' need
1190147191Sjkoshy		 * be placed on hardware.
1191145256Sjkoshy		 */
1192145256Sjkoshy
1193147191Sjkoshy		if (pm->pm_state != PMC_STATE_RUNNING)
1194147191Sjkoshy			continue;
1195145256Sjkoshy
1196147191Sjkoshy		/* increment PMC runcount */
1197147191Sjkoshy		atomic_add_rel_32(&pm->pm_runcount, 1);
1198145256Sjkoshy
1199147191Sjkoshy		/* configure the HWPMC we are going to use. */
1200147191Sjkoshy		md->pmd_config_pmc(cpu, ri, pm);
1201145256Sjkoshy
1202147191Sjkoshy		phw = pc->pc_hwpmcs[ri];
1203145256Sjkoshy
1204147191Sjkoshy		KASSERT(phw != NULL,
1205147191Sjkoshy		    ("[pmc,%d] null hw pointer", __LINE__));
1206145256Sjkoshy
1207147191Sjkoshy		KASSERT(phw->phw_pmc == pm,
1208147191Sjkoshy		    ("[pmc,%d] hw->pmc %p != pmc %p", __LINE__,
1209147191Sjkoshy			phw->phw_pmc, pm));
1210145256Sjkoshy
1211147191Sjkoshy		/*
1212147191Sjkoshy		 * Write out saved value and start the PMC.
1213147191Sjkoshy		 *
1214147191Sjkoshy		 * Sampling PMCs use a per-process value, while
1215147191Sjkoshy		 * counting mode PMCs use a per-pmc value that is
1216147191Sjkoshy		 * inherited across descendants.
1217147191Sjkoshy		 */
1218147191Sjkoshy		if (PMC_TO_MODE(pm) == PMC_MODE_TS) {
1219147191Sjkoshy			mtx_pool_lock_spin(pmc_mtxpool, pm);
1220147191Sjkoshy			newvalue = PMC_PCPU_SAVED(cpu,ri) =
1221147191Sjkoshy			    pp->pp_pmcs[ri].pp_pmcval;
1222147191Sjkoshy			mtx_pool_unlock_spin(pmc_mtxpool, pm);
1223147191Sjkoshy		} else {
1224147191Sjkoshy			KASSERT(PMC_TO_MODE(pm) == PMC_MODE_TC,
1225147191Sjkoshy			    ("[pmc,%d] illegal mode=%d", __LINE__,
1226147191Sjkoshy			    PMC_TO_MODE(pm)));
1227147191Sjkoshy			mtx_pool_lock_spin(pmc_mtxpool, pm);
1228147191Sjkoshy			newvalue = PMC_PCPU_SAVED(cpu, ri) =
1229147191Sjkoshy			    pm->pm_gv.pm_savedvalue;
1230147191Sjkoshy			mtx_pool_unlock_spin(pmc_mtxpool, pm);
1231147191Sjkoshy		}
1232145256Sjkoshy
1233147191Sjkoshy		PMCDBG(CSW,SWI,1,"cpu=%d ri=%d new=%jd", cpu, ri, newvalue);
1234145256Sjkoshy
1235147191Sjkoshy		md->pmd_write_pmc(cpu, ri, newvalue);
1236147191Sjkoshy		md->pmd_start_pmc(cpu, ri);
1237147191Sjkoshy	}
1238145256Sjkoshy
1239147191Sjkoshy	/*
1240147191Sjkoshy	 * perform any other architecture/cpu dependent thread
1241147191Sjkoshy	 * switch-in actions.
1242147191Sjkoshy	 */
1243145256Sjkoshy
1244147191Sjkoshy	(void) (*md->pmd_switch_in)(pc, pp);
1245145256Sjkoshy
1246147191Sjkoshy	critical_exit();
1247145256Sjkoshy
1248147191Sjkoshy}
1249145256Sjkoshy
1250147191Sjkoshy/*
1251147191Sjkoshy * Thread context switch OUT.
1252147191Sjkoshy */
1253145256Sjkoshy
1254147191Sjkoshystatic void
1255147191Sjkoshypmc_process_csw_out(struct thread *td)
1256147191Sjkoshy{
1257147191Sjkoshy	int cpu;
1258147191Sjkoshy	enum pmc_mode mode;
1259147191Sjkoshy	unsigned int ri;
1260147191Sjkoshy	struct pmc *pm;
1261147191Sjkoshy	struct proc *p;
1262147191Sjkoshy	struct pmc_cpu *pc;
1263147191Sjkoshy	struct pmc_process *pp;
1264147191Sjkoshy	int64_t tmp;
1265147191Sjkoshy	pmc_value_t newvalue;
1266145256Sjkoshy
1267147191Sjkoshy	/*
1268147191Sjkoshy	 * Locate our process descriptor; this may be NULL if
1269147191Sjkoshy	 * this process is exiting and we have already removed
1270147191Sjkoshy	 * the process from the target process table.
1271147191Sjkoshy	 *
1272147191Sjkoshy	 * Note that due to kernel preemption, multiple
1273147191Sjkoshy	 * context switches may happen while the process is
1274147191Sjkoshy	 * exiting.
1275147191Sjkoshy	 *
1276147191Sjkoshy	 * Note also that if the target process cannot be
1277147191Sjkoshy	 * found we still need to deconfigure any PMCs that
1278147191Sjkoshy	 * are currently running on hardware.
1279147191Sjkoshy	 */
1280145256Sjkoshy
1281147191Sjkoshy	p = td->td_proc;
1282147191Sjkoshy	pp = pmc_find_process_descriptor(p, PMC_FLAG_NONE);
1283145256Sjkoshy
1284147191Sjkoshy	/*
1285147191Sjkoshy	 * save PMCs
1286147191Sjkoshy	 */
1287145256Sjkoshy
1288147191Sjkoshy	critical_enter();
1289145774Sjkoshy
1290147191Sjkoshy	cpu = PCPU_GET(cpuid); /* td->td_oncpu is invalid */
1291145256Sjkoshy
1292147191Sjkoshy	PMCDBG(CSW,SWO,1, "cpu=%d proc=%p (%d, %s) pp=%p", cpu, p,
1293147191Sjkoshy	    p->p_pid, p->p_comm, pp);
1294145615Sjkoshy
1295147191Sjkoshy	KASSERT(cpu >= 0 && cpu < mp_ncpus,
1296147191Sjkoshy	    ("[pmc,%d wierd CPU id %d", __LINE__, cpu));
1297145615Sjkoshy
1298147191Sjkoshy	pc = pmc_pcpu[cpu];
1299145615Sjkoshy
1300147191Sjkoshy	/*
1301147191Sjkoshy	 * When a PMC gets unlinked from a target PMC, it will
1302147191Sjkoshy	 * be removed from the target's pp_pmc[] array.
1303147191Sjkoshy	 *
1304147191Sjkoshy	 * However, on a MP system, the target could have been
1305147191Sjkoshy	 * executing on another CPU at the time of the unlink.
1306147191Sjkoshy	 * So, at context switch OUT time, we need to look at
1307147191Sjkoshy	 * the hardware to determine if a PMC is scheduled on
1308147191Sjkoshy	 * it.
1309147191Sjkoshy	 */
1310145256Sjkoshy
1311147191Sjkoshy	for (ri = 0; ri < md->pmd_npmc; ri++) {
1312145256Sjkoshy
1313147191Sjkoshy		pm = NULL;
1314147191Sjkoshy		(void) (*md->pmd_get_config)(cpu, ri, &pm);
1315145256Sjkoshy
1316147191Sjkoshy		if (pm == NULL)	/* nothing at this row index */
1317147191Sjkoshy			continue;
1318145256Sjkoshy
1319147191Sjkoshy		mode = PMC_TO_MODE(pm);
1320147191Sjkoshy		if (!PMC_IS_VIRTUAL_MODE(mode))
1321147191Sjkoshy			continue; /* not a process virtual PMC */
1322145774Sjkoshy
1323147191Sjkoshy		KASSERT(PMC_TO_ROWINDEX(pm) == ri,
1324147191Sjkoshy		    ("[pmc,%d] ri mismatch pmc(%d) ri(%d)",
1325147191Sjkoshy			__LINE__, PMC_TO_ROWINDEX(pm), ri));
1326145256Sjkoshy
1327147191Sjkoshy		/* Stop hardware if not already stopped */
1328147867Sjkoshy		if (pm->pm_stalled == 0)
1329147191Sjkoshy			md->pmd_stop_pmc(cpu, ri);
1330147191Sjkoshy
1331147191Sjkoshy		/* reduce this PMC's runcount */
1332147191Sjkoshy		atomic_subtract_rel_32(&pm->pm_runcount, 1);
1333147191Sjkoshy
1334145256Sjkoshy		/*
1335147191Sjkoshy		 * If this PMC is associated with this process,
1336147191Sjkoshy		 * save the reading.
1337145256Sjkoshy		 */
1338145256Sjkoshy
1339147191Sjkoshy		if (pp != NULL && pp->pp_pmcs[ri].pp_pmc != NULL) {
1340147191Sjkoshy
1341147191Sjkoshy			KASSERT(pm == pp->pp_pmcs[ri].pp_pmc,
1342147191Sjkoshy			    ("[pmc,%d] pm %p != pp_pmcs[%d] %p", __LINE__,
1343147191Sjkoshy				pm, ri, pp->pp_pmcs[ri].pp_pmc));
1344147191Sjkoshy
1345147191Sjkoshy			KASSERT(pp->pp_refcnt > 0,
1346147191Sjkoshy			    ("[pmc,%d] pp refcnt = %d", __LINE__,
1347147191Sjkoshy				pp->pp_refcnt));
1348147191Sjkoshy
1349147191Sjkoshy			md->pmd_read_pmc(cpu, ri, &newvalue);
1350147191Sjkoshy
1351147191Sjkoshy			tmp = newvalue - PMC_PCPU_SAVED(cpu,ri);
1352147191Sjkoshy
1353147191Sjkoshy			PMCDBG(CSW,SWI,1,"cpu=%d ri=%d tmp=%jd", cpu, ri,
1354147191Sjkoshy			    tmp);
1355147191Sjkoshy
1356147191Sjkoshy			if (mode == PMC_MODE_TS) {
1357147191Sjkoshy
1358147191Sjkoshy				/*
1359147191Sjkoshy				 * For sampling process-virtual PMCs,
1360147191Sjkoshy				 * we expect the count to be
1361147191Sjkoshy				 * decreasing as the 'value'
1362147191Sjkoshy				 * programmed into the PMC is the
1363147191Sjkoshy				 * number of events to be seen till
1364147191Sjkoshy				 * the next sampling interrupt.
1365147191Sjkoshy				 */
1366147191Sjkoshy				if (tmp < 0)
1367147191Sjkoshy					tmp += pm->pm_sc.pm_reloadcount;
1368147191Sjkoshy				mtx_pool_lock_spin(pmc_mtxpool, pm);
1369147191Sjkoshy				pp->pp_pmcs[ri].pp_pmcval -= tmp;
1370147191Sjkoshy				if ((int64_t) pp->pp_pmcs[ri].pp_pmcval < 0)
1371147191Sjkoshy					pp->pp_pmcs[ri].pp_pmcval +=
1372147191Sjkoshy					    pm->pm_sc.pm_reloadcount;
1373147191Sjkoshy				mtx_pool_unlock_spin(pmc_mtxpool, pm);
1374147191Sjkoshy
1375147191Sjkoshy			} else {
1376147191Sjkoshy
1377147191Sjkoshy				/*
1378147191Sjkoshy				 * For counting process-virtual PMCs,
1379147191Sjkoshy				 * we expect the count to be
1380147191Sjkoshy				 * increasing monotonically, modulo a 64
1381147191Sjkoshy				 * bit wraparound.
1382147191Sjkoshy				 */
1383147191Sjkoshy				KASSERT((int64_t) tmp >= 0,
1384147191Sjkoshy				    ("[pmc,%d] negative increment cpu=%d "
1385147191Sjkoshy				     "ri=%d newvalue=%jx saved=%jx "
1386147191Sjkoshy				     "incr=%jx", __LINE__, cpu, ri,
1387147191Sjkoshy				     newvalue, PMC_PCPU_SAVED(cpu,ri), tmp));
1388147191Sjkoshy
1389147191Sjkoshy				mtx_pool_lock_spin(pmc_mtxpool, pm);
1390147191Sjkoshy				pm->pm_gv.pm_savedvalue += tmp;
1391147191Sjkoshy				pp->pp_pmcs[ri].pp_pmcval += tmp;
1392147191Sjkoshy				mtx_pool_unlock_spin(pmc_mtxpool, pm);
1393147191Sjkoshy
1394147191Sjkoshy				if (pm->pm_flags & PMC_F_LOG_PROCCSW)
1395147191Sjkoshy					pmclog_process_proccsw(pm, pp, tmp);
1396147191Sjkoshy			}
1397145256Sjkoshy		}
1398145256Sjkoshy
1399147191Sjkoshy		/* mark hardware as free */
1400147191Sjkoshy		md->pmd_config_pmc(cpu, ri, NULL);
1401145256Sjkoshy	}
1402145256Sjkoshy
1403145256Sjkoshy	/*
1404147191Sjkoshy	 * perform any other architecture/cpu dependent thread
1405147191Sjkoshy	 * switch out functions.
1406147191Sjkoshy	 */
1407147191Sjkoshy
1408147191Sjkoshy	(void) (*md->pmd_switch_out)(pc, pp);
1409147191Sjkoshy
1410147191Sjkoshy	critical_exit();
1411147191Sjkoshy}
1412147191Sjkoshy
1413147191Sjkoshy/*
1414147191Sjkoshy * The 'hook' invoked from the kernel proper
1415147191Sjkoshy */
1416147191Sjkoshy
1417147191Sjkoshy
1418153110Sru#ifdef	DEBUG
1419147191Sjkoshyconst char *pmc_hooknames[] = {
1420147191Sjkoshy	"",
1421147191Sjkoshy	"EXIT",
1422147191Sjkoshy	"EXEC",
1423147191Sjkoshy	"FORK",
1424147191Sjkoshy	"CSW-IN",
1425147191Sjkoshy	"CSW-OUT",
1426147191Sjkoshy	"SAMPLE"
1427147191Sjkoshy};
1428147191Sjkoshy#endif
1429147191Sjkoshy
1430147191Sjkoshystatic int
1431147191Sjkoshypmc_hook_handler(struct thread *td, int function, void *arg)
1432147191Sjkoshy{
1433147191Sjkoshy
1434147191Sjkoshy	PMCDBG(MOD,PMH,1, "hook td=%p func=%d \"%s\" arg=%p", td, function,
1435147191Sjkoshy	    pmc_hooknames[function], arg);
1436147191Sjkoshy
1437147191Sjkoshy	switch (function)
1438147191Sjkoshy	{
1439147191Sjkoshy
1440147191Sjkoshy	/*
1441145256Sjkoshy	 * Process exec()
1442145256Sjkoshy	 */
1443145256Sjkoshy
1444145256Sjkoshy	case PMC_FN_PROCESS_EXEC:
1445145256Sjkoshy	{
1446147191Sjkoshy		char *fullpath, *freepath;
1447145256Sjkoshy		unsigned int ri;
1448147191Sjkoshy		int is_using_hwpmcs;
1449145256Sjkoshy		struct pmc *pm;
1450145256Sjkoshy		struct proc *p;
1451145256Sjkoshy		struct pmc_owner *po;
1452145256Sjkoshy		struct pmc_process *pp;
1453147708Sjkoshy		struct pmckern_procexec *pk;
1454145256Sjkoshy
1455145256Sjkoshy		sx_assert(&pmc_sx, SX_XLOCKED);
1456145256Sjkoshy
1457147191Sjkoshy		p = td->td_proc;
1458147708Sjkoshy		pmc_getfilename(p->p_textvp, &fullpath, &freepath);
1459147191Sjkoshy
1460147708Sjkoshy		pk = (struct pmckern_procexec *) arg;
1461147708Sjkoshy
1462147191Sjkoshy		/* Inform owners of SS mode PMCs of the exec event. */
1463147191Sjkoshy		LIST_FOREACH(po, &pmc_ss_owners, po_ssnext)
1464147191Sjkoshy		    if (po->po_flags & PMC_PO_OWNS_LOGFILE)
1465147708Sjkoshy			    pmclog_process_procexec(po, PMC_ID_INVALID,
1466147708Sjkoshy				p->p_pid, pk->pm_entryaddr, fullpath);
1467147191Sjkoshy
1468147191Sjkoshy		PROC_LOCK(p);
1469147191Sjkoshy		is_using_hwpmcs = p->p_flag & P_HWPMC;
1470147191Sjkoshy		PROC_UNLOCK(p);
1471147191Sjkoshy
1472147191Sjkoshy		if (!is_using_hwpmcs) {
1473147191Sjkoshy			if (freepath)
1474147191Sjkoshy				FREE(freepath, M_TEMP);
1475147191Sjkoshy			break;
1476147191Sjkoshy		}
1477147191Sjkoshy
1478145256Sjkoshy		/*
1479145256Sjkoshy		 * PMCs are not inherited across an exec():  remove any
1480145256Sjkoshy		 * PMCs that this process is the owner of.
1481145256Sjkoshy		 */
1482145256Sjkoshy
1483145256Sjkoshy		if ((po = pmc_find_owner_descriptor(p)) != NULL) {
1484145256Sjkoshy			pmc_remove_owner(po);
1485147191Sjkoshy			pmc_destroy_owner_descriptor(po);
1486145256Sjkoshy		}
1487145256Sjkoshy
1488145256Sjkoshy		/*
1489154483Sjkoshy		 * If the process being exec'ed is not the target of any
1490154483Sjkoshy		 * PMC, we are done.
1491145256Sjkoshy		 */
1492154483Sjkoshy		if ((pp = pmc_find_process_descriptor(p, 0)) == NULL) {
1493154483Sjkoshy			if (freepath)
1494154483Sjkoshy				FREE(freepath, M_TEMP);
1495145256Sjkoshy			break;
1496154483Sjkoshy		}
1497145256Sjkoshy
1498147191Sjkoshy		/*
1499147191Sjkoshy		 * Log the exec event to all monitoring owners.  Skip
1500147191Sjkoshy		 * owners who have already recieved the event because
1501154483Sjkoshy		 * they had system sampling PMCs active.
1502147191Sjkoshy		 */
1503147191Sjkoshy		for (ri = 0; ri < md->pmd_npmc; ri++)
1504147191Sjkoshy			if ((pm = pp->pp_pmcs[ri].pp_pmc) != NULL) {
1505147191Sjkoshy				po = pm->pm_owner;
1506147191Sjkoshy				if (po->po_sscount == 0 &&
1507147191Sjkoshy				    po->po_flags & PMC_PO_OWNS_LOGFILE)
1508147708Sjkoshy					pmclog_process_procexec(po, pm->pm_id,
1509147708Sjkoshy					    p->p_pid, pk->pm_entryaddr,
1510147191Sjkoshy					    fullpath);
1511147191Sjkoshy			}
1512147191Sjkoshy
1513147191Sjkoshy		if (freepath)
1514147191Sjkoshy			FREE(freepath, M_TEMP);
1515147191Sjkoshy
1516145256Sjkoshy
1517145256Sjkoshy		PMCDBG(PRC,EXC,1, "exec proc=%p (%d, %s) cred-changed=%d",
1518147708Sjkoshy		    p, p->p_pid, p->p_comm, pk->pm_credentialschanged);
1519145256Sjkoshy
1520147708Sjkoshy		if (pk->pm_credentialschanged == 0) /* no change */
1521145256Sjkoshy			break;
1522145256Sjkoshy
1523145256Sjkoshy		/*
1524145256Sjkoshy		 * If the newly exec()'ed process has a different credential
1525145256Sjkoshy		 * than before, allow it to be the target of a PMC only if
1526145256Sjkoshy		 * the PMC's owner has sufficient priviledge.
1527145256Sjkoshy		 */
1528145256Sjkoshy
1529145256Sjkoshy		for (ri = 0; ri < md->pmd_npmc; ri++)
1530145256Sjkoshy			if ((pm = pp->pp_pmcs[ri].pp_pmc) != NULL)
1531145256Sjkoshy				if (pmc_can_attach(pm, td->td_proc) != 0)
1532145256Sjkoshy					pmc_detach_one_process(td->td_proc,
1533145256Sjkoshy					    pm, PMC_FLAG_NONE);
1534145256Sjkoshy
1535145256Sjkoshy		KASSERT(pp->pp_refcnt >= 0 && pp->pp_refcnt < (int) md->pmd_npmc,
1536145256Sjkoshy		    ("[pmc,%d] Illegal ref count %d on pp %p", __LINE__,
1537145256Sjkoshy			pp->pp_refcnt, pp));
1538145256Sjkoshy
1539145256Sjkoshy		/*
1540145256Sjkoshy		 * If this process is no longer the target of any
1541145256Sjkoshy		 * PMCs, we can remove the process entry and free
1542145256Sjkoshy		 * up space.
1543145256Sjkoshy		 */
1544145256Sjkoshy
1545145256Sjkoshy		if (pp->pp_refcnt == 0) {
1546145256Sjkoshy			pmc_remove_process_descriptor(pp);
1547145256Sjkoshy			FREE(pp, M_PMC);
1548147191Sjkoshy			break;
1549145256Sjkoshy		}
1550145256Sjkoshy
1551145256Sjkoshy	}
1552145256Sjkoshy	break;
1553145256Sjkoshy
1554145256Sjkoshy	case PMC_FN_CSW_IN:
1555147191Sjkoshy		pmc_process_csw_in(td);
1556147191Sjkoshy		break;
1557145256Sjkoshy
1558147191Sjkoshy	case PMC_FN_CSW_OUT:
1559147191Sjkoshy		pmc_process_csw_out(td);
1560147191Sjkoshy		break;
1561145256Sjkoshy
1562145256Sjkoshy	/*
1563147191Sjkoshy	 * Process accumulated PC samples.
1564147191Sjkoshy	 *
1565147191Sjkoshy	 * This function is expected to be called by hardclock() for
1566147191Sjkoshy	 * each CPU that has accumulated PC samples.
1567147191Sjkoshy	 *
1568147191Sjkoshy	 * This function is to be executed on the CPU whose samples
1569147191Sjkoshy	 * are being processed.
1570145256Sjkoshy	 */
1571147191Sjkoshy	case PMC_FN_DO_SAMPLES:
1572145256Sjkoshy
1573145256Sjkoshy		/*
1574147191Sjkoshy		 * Clear the cpu specific bit in the CPU mask before
1575147191Sjkoshy		 * do the rest of the processing.  If the NMI handler
1576147191Sjkoshy		 * gets invoked after the "atomic_clear_int()" call
1577147191Sjkoshy		 * below but before "pmc_process_samples()" gets
1578147191Sjkoshy		 * around to processing the interrupt, then we will
1579147191Sjkoshy		 * come back here at the next hardclock() tick (and
1580147191Sjkoshy		 * may find nothing to do if "pmc_process_samples()"
1581147191Sjkoshy		 * had already processed the interrupt).  We don't
1582147191Sjkoshy		 * lose the interrupt sample.
1583145256Sjkoshy		 */
1584147191Sjkoshy		atomic_clear_int(&pmc_cpumask, (1 << PCPU_GET(cpuid)));
1585147191Sjkoshy		pmc_process_samples(PCPU_GET(cpuid));
1586147191Sjkoshy		break;
1587145256Sjkoshy
1588145256Sjkoshy	default:
1589153110Sru#ifdef	DEBUG
1590145256Sjkoshy		KASSERT(0, ("[pmc,%d] unknown hook %d\n", __LINE__, function));
1591145256Sjkoshy#endif
1592145256Sjkoshy		break;
1593145256Sjkoshy
1594145256Sjkoshy	}
1595145256Sjkoshy
1596145256Sjkoshy	return 0;
1597145256Sjkoshy}
1598145256Sjkoshy
1599145256Sjkoshy/*
1600145256Sjkoshy * allocate a 'struct pmc_owner' descriptor in the owner hash table.
1601145256Sjkoshy */
1602145256Sjkoshy
1603145256Sjkoshystatic struct pmc_owner *
1604145256Sjkoshypmc_allocate_owner_descriptor(struct proc *p)
1605145256Sjkoshy{
1606145256Sjkoshy	uint32_t hindex;
1607145256Sjkoshy	struct pmc_owner *po;
1608145256Sjkoshy	struct pmc_ownerhash *poh;
1609145256Sjkoshy
1610145256Sjkoshy	hindex = PMC_HASH_PTR(p, pmc_ownerhashmask);
1611145256Sjkoshy	poh = &pmc_ownerhash[hindex];
1612145256Sjkoshy
1613145256Sjkoshy	/* allocate space for N pointers and one descriptor struct */
1614145256Sjkoshy	MALLOC(po, struct pmc_owner *, sizeof(struct pmc_owner),
1615147191Sjkoshy	    M_PMC, M_ZERO|M_WAITOK);
1616145256Sjkoshy
1617147191Sjkoshy	po->po_sscount = po->po_error = po->po_flags = 0;
1618147191Sjkoshy	po->po_file  = NULL;
1619145256Sjkoshy	po->po_owner = p;
1620147191Sjkoshy	po->po_kthread = NULL;
1621145256Sjkoshy	LIST_INIT(&po->po_pmcs);
1622145256Sjkoshy	LIST_INSERT_HEAD(poh, po, po_next); /* insert into hash table */
1623145256Sjkoshy
1624147191Sjkoshy	TAILQ_INIT(&po->po_logbuffers);
1625147191Sjkoshy	mtx_init(&po->po_mtx, "pmc-owner-mtx", "pmc", MTX_SPIN);
1626147191Sjkoshy
1627145256Sjkoshy	PMCDBG(OWN,ALL,1, "allocate-owner proc=%p (%d, %s) pmc-owner=%p",
1628145256Sjkoshy	    p, p->p_pid, p->p_comm, po);
1629145256Sjkoshy
1630145256Sjkoshy	return po;
1631145256Sjkoshy}
1632145256Sjkoshy
1633147191Sjkoshystatic void
1634147191Sjkoshypmc_destroy_owner_descriptor(struct pmc_owner *po)
1635147191Sjkoshy{
1636147191Sjkoshy
1637147191Sjkoshy	PMCDBG(OWN,REL,1, "destroy-owner po=%p proc=%p (%d, %s)",
1638147191Sjkoshy	    po, po->po_owner, po->po_owner->p_pid, po->po_owner->p_comm);
1639147191Sjkoshy
1640147191Sjkoshy	mtx_destroy(&po->po_mtx);
1641147191Sjkoshy	FREE(po, M_PMC);
1642147191Sjkoshy}
1643147191Sjkoshy
1644145256Sjkoshy/*
1645145256Sjkoshy * find the descriptor corresponding to process 'p', adding or removing it
1646145256Sjkoshy * as specified by 'mode'.
1647145256Sjkoshy */
1648145256Sjkoshy
1649145256Sjkoshystatic struct pmc_process *
1650145256Sjkoshypmc_find_process_descriptor(struct proc *p, uint32_t mode)
1651145256Sjkoshy{
1652145256Sjkoshy	uint32_t hindex;
1653145256Sjkoshy	struct pmc_process *pp, *ppnew;
1654145256Sjkoshy	struct pmc_processhash *pph;
1655145256Sjkoshy
1656145256Sjkoshy	hindex = PMC_HASH_PTR(p, pmc_processhashmask);
1657145256Sjkoshy	pph = &pmc_processhash[hindex];
1658145256Sjkoshy
1659145256Sjkoshy	ppnew = NULL;
1660145256Sjkoshy
1661145256Sjkoshy	/*
1662145256Sjkoshy	 * Pre-allocate memory in the FIND_ALLOCATE case since we
1663145256Sjkoshy	 * cannot call malloc(9) once we hold a spin lock.
1664145256Sjkoshy	 */
1665145256Sjkoshy
1666145256Sjkoshy	if (mode & PMC_FLAG_ALLOCATE) {
1667145256Sjkoshy		/* allocate additional space for 'n' pmc pointers */
1668145256Sjkoshy		MALLOC(ppnew, struct pmc_process *,
1669145256Sjkoshy		    sizeof(struct pmc_process) + md->pmd_npmc *
1670145256Sjkoshy		    sizeof(struct pmc_targetstate), M_PMC, M_ZERO|M_WAITOK);
1671145256Sjkoshy	}
1672145256Sjkoshy
1673145256Sjkoshy	mtx_lock_spin(&pmc_processhash_mtx);
1674145256Sjkoshy	LIST_FOREACH(pp, pph, pp_next)
1675145256Sjkoshy	    if (pp->pp_proc == p)
1676145256Sjkoshy		    break;
1677145256Sjkoshy
1678145256Sjkoshy	if ((mode & PMC_FLAG_REMOVE) && pp != NULL)
1679145256Sjkoshy		LIST_REMOVE(pp, pp_next);
1680145256Sjkoshy
1681145256Sjkoshy	if ((mode & PMC_FLAG_ALLOCATE) && pp == NULL &&
1682145256Sjkoshy	    ppnew != NULL) {
1683145256Sjkoshy		ppnew->pp_proc = p;
1684145256Sjkoshy		LIST_INSERT_HEAD(pph, ppnew, pp_next);
1685145256Sjkoshy		pp = ppnew;
1686145256Sjkoshy		ppnew = NULL;
1687145256Sjkoshy	}
1688145256Sjkoshy	mtx_unlock_spin(&pmc_processhash_mtx);
1689145256Sjkoshy
1690145256Sjkoshy	if (pp != NULL && ppnew != NULL)
1691145256Sjkoshy		FREE(ppnew, M_PMC);
1692145256Sjkoshy
1693145256Sjkoshy	return pp;
1694145256Sjkoshy}
1695145256Sjkoshy
1696145256Sjkoshy/*
1697145256Sjkoshy * remove a process descriptor from the process hash table.
1698145256Sjkoshy */
1699145256Sjkoshy
1700145256Sjkoshystatic void
1701145256Sjkoshypmc_remove_process_descriptor(struct pmc_process *pp)
1702145256Sjkoshy{
1703145256Sjkoshy	KASSERT(pp->pp_refcnt == 0,
1704145256Sjkoshy	    ("[pmc,%d] Removing process descriptor %p with count %d",
1705145256Sjkoshy		__LINE__, pp, pp->pp_refcnt));
1706145256Sjkoshy
1707145256Sjkoshy	mtx_lock_spin(&pmc_processhash_mtx);
1708145256Sjkoshy	LIST_REMOVE(pp, pp_next);
1709145256Sjkoshy	mtx_unlock_spin(&pmc_processhash_mtx);
1710145256Sjkoshy}
1711145256Sjkoshy
1712145256Sjkoshy
1713145256Sjkoshy/*
1714145256Sjkoshy * find an owner descriptor corresponding to proc 'p'
1715145256Sjkoshy */
1716145256Sjkoshy
1717145256Sjkoshystatic struct pmc_owner *
1718145256Sjkoshypmc_find_owner_descriptor(struct proc *p)
1719145256Sjkoshy{
1720145256Sjkoshy	uint32_t hindex;
1721145256Sjkoshy	struct pmc_owner *po;
1722145256Sjkoshy	struct pmc_ownerhash *poh;
1723145256Sjkoshy
1724145256Sjkoshy	hindex = PMC_HASH_PTR(p, pmc_ownerhashmask);
1725145256Sjkoshy	poh = &pmc_ownerhash[hindex];
1726145256Sjkoshy
1727145256Sjkoshy	po = NULL;
1728145256Sjkoshy	LIST_FOREACH(po, poh, po_next)
1729145256Sjkoshy	    if (po->po_owner == p)
1730145256Sjkoshy		    break;
1731145256Sjkoshy
1732145256Sjkoshy	PMCDBG(OWN,FND,1, "find-owner proc=%p (%d, %s) hindex=0x%x -> "
1733145256Sjkoshy	    "pmc-owner=%p", p, p->p_pid, p->p_comm, hindex, po);
1734145256Sjkoshy
1735145256Sjkoshy	return po;
1736145256Sjkoshy}
1737145256Sjkoshy
1738145256Sjkoshy/*
1739145256Sjkoshy * pmc_allocate_pmc_descriptor
1740145256Sjkoshy *
1741145256Sjkoshy * Allocate a pmc descriptor and initialize its
1742145256Sjkoshy * fields.
1743145256Sjkoshy */
1744145256Sjkoshy
1745145256Sjkoshystatic struct pmc *
1746145256Sjkoshypmc_allocate_pmc_descriptor(void)
1747145256Sjkoshy{
1748145256Sjkoshy	struct pmc *pmc;
1749145256Sjkoshy
1750145256Sjkoshy	MALLOC(pmc, struct pmc *, sizeof(struct pmc), M_PMC, M_ZERO|M_WAITOK);
1751145256Sjkoshy
1752145256Sjkoshy	if (pmc != NULL) {
1753145256Sjkoshy		pmc->pm_owner = NULL;
1754145256Sjkoshy		LIST_INIT(&pmc->pm_targets);
1755145256Sjkoshy	}
1756145256Sjkoshy
1757145256Sjkoshy	PMCDBG(PMC,ALL,1, "allocate-pmc -> pmc=%p", pmc);
1758145256Sjkoshy
1759145256Sjkoshy	return pmc;
1760145256Sjkoshy}
1761145256Sjkoshy
1762145256Sjkoshy/*
1763145256Sjkoshy * Destroy a pmc descriptor.
1764145256Sjkoshy */
1765145256Sjkoshy
1766145256Sjkoshystatic void
1767145256Sjkoshypmc_destroy_pmc_descriptor(struct pmc *pm)
1768145256Sjkoshy{
1769145256Sjkoshy	(void) pm;
1770145256Sjkoshy
1771153110Sru#ifdef	DEBUG
1772145256Sjkoshy	KASSERT(pm->pm_state == PMC_STATE_DELETED ||
1773145256Sjkoshy	    pm->pm_state == PMC_STATE_FREE,
1774145256Sjkoshy	    ("[pmc,%d] destroying non-deleted PMC", __LINE__));
1775145256Sjkoshy	KASSERT(LIST_EMPTY(&pm->pm_targets),
1776145256Sjkoshy	    ("[pmc,%d] destroying pmc with targets", __LINE__));
1777145256Sjkoshy	KASSERT(pm->pm_owner == NULL,
1778145256Sjkoshy	    ("[pmc,%d] destroying pmc attached to an owner", __LINE__));
1779145256Sjkoshy	KASSERT(pm->pm_runcount == 0,
1780145256Sjkoshy	    ("[pmc,%d] pmc has non-zero run count %d", __LINE__,
1781145256Sjkoshy		pm->pm_runcount));
1782145256Sjkoshy#endif
1783145256Sjkoshy}
1784145256Sjkoshy
1785147191Sjkoshystatic void
1786147191Sjkoshypmc_wait_for_pmc_idle(struct pmc *pm)
1787147191Sjkoshy{
1788153110Sru#ifdef	DEBUG
1789147191Sjkoshy	volatile int maxloop;
1790147191Sjkoshy
1791147191Sjkoshy	maxloop = 100 * mp_ncpus;
1792147191Sjkoshy#endif
1793147191Sjkoshy
1794147191Sjkoshy	/*
1795147191Sjkoshy	 * Loop (with a forced context switch) till the PMC's runcount
1796147191Sjkoshy	 * comes down to zero.
1797147191Sjkoshy	 */
1798147191Sjkoshy	while (atomic_load_acq_32(&pm->pm_runcount) > 0) {
1799153110Sru#ifdef	DEBUG
1800147191Sjkoshy		maxloop--;
1801147191Sjkoshy		KASSERT(maxloop > 0,
1802147191Sjkoshy		    ("[pmc,%d] (ri%d, rc%d) waiting too long for "
1803147191Sjkoshy			"pmc to be free", __LINE__,
1804147191Sjkoshy			PMC_TO_ROWINDEX(pm), pm->pm_runcount));
1805147191Sjkoshy#endif
1806147191Sjkoshy		pmc_force_context_switch();
1807147191Sjkoshy	}
1808147191Sjkoshy}
1809147191Sjkoshy
1810145256Sjkoshy/*
1811145256Sjkoshy * This function does the following things:
1812145256Sjkoshy *
1813145256Sjkoshy *  - detaches the PMC from hardware
1814145256Sjkoshy *  - unlinks all target threads that were attached to it
1815145256Sjkoshy *  - removes the PMC from its owner's list
1816145256Sjkoshy *  - destroy's the PMC private mutex
1817145256Sjkoshy *
1818145256Sjkoshy * Once this function completes, the given pmc pointer can be safely
1819145256Sjkoshy * FREE'd by the caller.
1820145256Sjkoshy */
1821145256Sjkoshy
1822145256Sjkoshystatic void
1823145256Sjkoshypmc_release_pmc_descriptor(struct pmc *pm)
1824145256Sjkoshy{
1825145256Sjkoshy	u_int ri, cpu;
1826145774Sjkoshy	enum pmc_mode mode;
1827145256Sjkoshy	struct pmc_hw *phw;
1828147191Sjkoshy	struct pmc_owner *po;
1829145256Sjkoshy	struct pmc_process *pp;
1830145256Sjkoshy	struct pmc_target *ptgt, *tmp;
1831145256Sjkoshy	struct pmc_binding pb;
1832145256Sjkoshy
1833145256Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
1834145256Sjkoshy
1835145256Sjkoshy	KASSERT(pm, ("[pmc,%d] null pmc", __LINE__));
1836145256Sjkoshy
1837145774Sjkoshy	ri   = PMC_TO_ROWINDEX(pm);
1838145774Sjkoshy	mode = PMC_TO_MODE(pm);
1839145256Sjkoshy
1840145256Sjkoshy	PMCDBG(PMC,REL,1, "release-pmc pmc=%p ri=%d mode=%d", pm, ri,
1841145774Sjkoshy	    mode);
1842145256Sjkoshy
1843145256Sjkoshy	/*
1844145256Sjkoshy	 * First, we take the PMC off hardware.
1845145256Sjkoshy	 */
1846145301Simp	cpu = 0;
1847145774Sjkoshy	if (PMC_IS_SYSTEM_MODE(mode)) {
1848145256Sjkoshy
1849145256Sjkoshy		/*
1850145256Sjkoshy		 * A system mode PMC runs on a specific CPU.  Switch
1851145256Sjkoshy		 * to this CPU and turn hardware off.
1852145256Sjkoshy		 */
1853145256Sjkoshy		pmc_save_cpu_binding(&pb);
1854145256Sjkoshy
1855145774Sjkoshy		cpu = PMC_TO_CPU(pm);
1856145256Sjkoshy
1857147191Sjkoshy		pmc_select_cpu(cpu);
1858145256Sjkoshy
1859147191Sjkoshy		/* switch off non-stalled CPUs */
1860147191Sjkoshy		if (pm->pm_state == PMC_STATE_RUNNING &&
1861147867Sjkoshy		    pm->pm_stalled == 0) {
1862145256Sjkoshy
1863145256Sjkoshy			phw = pmc_pcpu[cpu]->pc_hwpmcs[ri];
1864145256Sjkoshy
1865145256Sjkoshy			KASSERT(phw->phw_pmc == pm,
1866145256Sjkoshy			    ("[pmc, %d] pmc ptr ri(%d) hw(%p) pm(%p)",
1867145256Sjkoshy				__LINE__, ri, phw->phw_pmc, pm));
1868145256Sjkoshy			PMCDBG(PMC,REL,2, "stopping cpu=%d ri=%d", cpu, ri);
1869145256Sjkoshy
1870145256Sjkoshy			critical_enter();
1871145256Sjkoshy			md->pmd_stop_pmc(cpu, ri);
1872145256Sjkoshy			critical_exit();
1873145256Sjkoshy		}
1874145256Sjkoshy
1875145256Sjkoshy		PMCDBG(PMC,REL,2, "decfg cpu=%d ri=%d", cpu, ri);
1876145256Sjkoshy
1877145256Sjkoshy		critical_enter();
1878145256Sjkoshy		md->pmd_config_pmc(cpu, ri, NULL);
1879145256Sjkoshy		critical_exit();
1880145256Sjkoshy
1881147191Sjkoshy		/* adjust the global and process count of SS mode PMCs */
1882147191Sjkoshy		if (mode == PMC_MODE_SS && pm->pm_state == PMC_STATE_RUNNING) {
1883147191Sjkoshy			po = pm->pm_owner;
1884147191Sjkoshy			po->po_sscount--;
1885147191Sjkoshy			if (po->po_sscount == 0) {
1886147191Sjkoshy				atomic_subtract_rel_int(&pmc_ss_count, 1);
1887147191Sjkoshy				LIST_REMOVE(po, po_ssnext);
1888147191Sjkoshy			}
1889147191Sjkoshy		}
1890147191Sjkoshy
1891145256Sjkoshy		pm->pm_state = PMC_STATE_DELETED;
1892145256Sjkoshy
1893145256Sjkoshy		pmc_restore_cpu_binding(&pb);
1894145256Sjkoshy
1895147191Sjkoshy		/*
1896147191Sjkoshy		 * We could have references to this PMC structure in
1897147191Sjkoshy		 * the per-cpu sample queues.  Wait for the queue to
1898147191Sjkoshy		 * drain.
1899147191Sjkoshy		 */
1900147191Sjkoshy		pmc_wait_for_pmc_idle(pm);
1901147191Sjkoshy
1902145774Sjkoshy	} else if (PMC_IS_VIRTUAL_MODE(mode)) {
1903145256Sjkoshy
1904145256Sjkoshy		/*
1905145256Sjkoshy		 * A virtual PMC could be running on multiple CPUs at
1906145256Sjkoshy		 * a given instant.
1907145256Sjkoshy		 *
1908145256Sjkoshy		 * By marking its state as DELETED, we ensure that
1909145256Sjkoshy		 * this PMC is never further scheduled on hardware.
1910145256Sjkoshy		 *
1911145256Sjkoshy		 * Then we wait till all CPUs are done with this PMC.
1912145256Sjkoshy		 */
1913145256Sjkoshy		pm->pm_state = PMC_STATE_DELETED;
1914145256Sjkoshy
1915145256Sjkoshy
1916147191Sjkoshy		/* Wait for the PMCs runcount to come to zero. */
1917147191Sjkoshy		pmc_wait_for_pmc_idle(pm);
1918145256Sjkoshy
1919145256Sjkoshy		/*
1920145256Sjkoshy		 * At this point the PMC is off all CPUs and cannot be
1921145256Sjkoshy		 * freshly scheduled onto a CPU.  It is now safe to
1922145256Sjkoshy		 * unlink all targets from this PMC.  If a
1923145256Sjkoshy		 * process-record's refcount falls to zero, we remove
1924145256Sjkoshy		 * it from the hash table.  The module-wide SX lock
1925145256Sjkoshy		 * protects us from races.
1926145256Sjkoshy		 */
1927145256Sjkoshy		LIST_FOREACH_SAFE(ptgt, &pm->pm_targets, pt_next, tmp) {
1928145256Sjkoshy			pp = ptgt->pt_process;
1929145256Sjkoshy			pmc_unlink_target_process(pm, pp); /* frees 'ptgt' */
1930145256Sjkoshy
1931145256Sjkoshy			PMCDBG(PMC,REL,3, "pp->refcnt=%d", pp->pp_refcnt);
1932145256Sjkoshy
1933145256Sjkoshy			/*
1934145256Sjkoshy			 * If the target process record shows that no
1935145256Sjkoshy			 * PMCs are attached to it, reclaim its space.
1936145256Sjkoshy			 */
1937145256Sjkoshy
1938145256Sjkoshy			if (pp->pp_refcnt == 0) {
1939145256Sjkoshy				pmc_remove_process_descriptor(pp);
1940145256Sjkoshy				FREE(pp, M_PMC);
1941145256Sjkoshy			}
1942145256Sjkoshy		}
1943145256Sjkoshy
1944145256Sjkoshy		cpu = curthread->td_oncpu; /* setup cpu for pmd_release() */
1945145256Sjkoshy
1946145256Sjkoshy	}
1947145256Sjkoshy
1948145256Sjkoshy	/*
1949145256Sjkoshy	 * Release any MD resources
1950145256Sjkoshy	 */
1951145256Sjkoshy
1952145256Sjkoshy	(void) md->pmd_release_pmc(cpu, ri, pm);
1953145256Sjkoshy
1954145256Sjkoshy	/*
1955145256Sjkoshy	 * Update row disposition
1956145256Sjkoshy	 */
1957145256Sjkoshy
1958145774Sjkoshy	if (PMC_IS_SYSTEM_MODE(PMC_TO_MODE(pm)))
1959145256Sjkoshy		PMC_UNMARK_ROW_STANDALONE(ri);
1960145256Sjkoshy	else
1961145256Sjkoshy		PMC_UNMARK_ROW_THREAD(ri);
1962145256Sjkoshy
1963145256Sjkoshy	/* unlink from the owner's list */
1964147191Sjkoshy	if (pm->pm_owner) {
1965147191Sjkoshy		LIST_REMOVE(pm, pm_next);
1966147191Sjkoshy		pm->pm_owner = NULL;
1967147191Sjkoshy	}
1968145256Sjkoshy
1969145256Sjkoshy	pmc_destroy_pmc_descriptor(pm);
1970145256Sjkoshy}
1971145256Sjkoshy
1972145256Sjkoshy/*
1973145256Sjkoshy * Register an owner and a pmc.
1974145256Sjkoshy */
1975145256Sjkoshy
1976145256Sjkoshystatic int
1977145256Sjkoshypmc_register_owner(struct proc *p, struct pmc *pmc)
1978145256Sjkoshy{
1979145256Sjkoshy	struct pmc_owner *po;
1980145256Sjkoshy
1981145256Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
1982145256Sjkoshy
1983145774Sjkoshy	if ((po = pmc_find_owner_descriptor(p)) == NULL)
1984147191Sjkoshy		if ((po = pmc_allocate_owner_descriptor(p)) == NULL)
1985145256Sjkoshy			return ENOMEM;
1986145256Sjkoshy
1987145256Sjkoshy	KASSERT(pmc->pm_owner == NULL,
1988145256Sjkoshy	    ("[pmc,%d] attempting to own an initialized PMC", __LINE__));
1989145256Sjkoshy	pmc->pm_owner  = po;
1990145256Sjkoshy
1991147191Sjkoshy	LIST_INSERT_HEAD(&po->po_pmcs, pmc, pm_next);
1992145256Sjkoshy
1993145256Sjkoshy	PROC_LOCK(p);
1994145256Sjkoshy	p->p_flag |= P_HWPMC;
1995145256Sjkoshy	PROC_UNLOCK(p);
1996145256Sjkoshy
1997147191Sjkoshy	if (po->po_flags & PMC_PO_OWNS_LOGFILE)
1998147191Sjkoshy		pmclog_process_pmcallocate(pmc);
1999145256Sjkoshy
2000147191Sjkoshy	PMCDBG(PMC,REG,1, "register-owner pmc-owner=%p pmc=%p",
2001147191Sjkoshy	    po, pmc);
2002147191Sjkoshy
2003145256Sjkoshy	return 0;
2004145256Sjkoshy}
2005145256Sjkoshy
2006145256Sjkoshy/*
2007145256Sjkoshy * Return the current row disposition:
2008145256Sjkoshy * == 0 => FREE
2009145256Sjkoshy *  > 0 => PROCESS MODE
2010145256Sjkoshy *  < 0 => SYSTEM MODE
2011145256Sjkoshy */
2012145256Sjkoshy
2013145256Sjkoshyint
2014145256Sjkoshypmc_getrowdisp(int ri)
2015145256Sjkoshy{
2016145256Sjkoshy	return pmc_pmcdisp[ri];
2017145256Sjkoshy}
2018145256Sjkoshy
2019145256Sjkoshy/*
2020145256Sjkoshy * Check if a PMC at row index 'ri' can be allocated to the current
2021145256Sjkoshy * process.
2022145256Sjkoshy *
2023145256Sjkoshy * Allocation can fail if:
2024145256Sjkoshy *   - the current process is already being profiled by a PMC at index 'ri',
2025145256Sjkoshy *     attached to it via OP_PMCATTACH.
2026145256Sjkoshy *   - the current process has already allocated a PMC at index 'ri'
2027145256Sjkoshy *     via OP_ALLOCATE.
2028145256Sjkoshy */
2029145256Sjkoshy
2030145256Sjkoshystatic int
2031145774Sjkoshypmc_can_allocate_rowindex(struct proc *p, unsigned int ri, int cpu)
2032145256Sjkoshy{
2033145774Sjkoshy	enum pmc_mode mode;
2034145774Sjkoshy	struct pmc *pm;
2035145256Sjkoshy	struct pmc_owner *po;
2036145256Sjkoshy	struct pmc_process *pp;
2037145256Sjkoshy
2038145774Sjkoshy	PMCDBG(PMC,ALR,1, "can-allocate-rowindex proc=%p (%d, %s) ri=%d "
2039145774Sjkoshy	    "cpu=%d", p, p->p_pid, p->p_comm, ri, cpu);
2040145256Sjkoshy
2041145774Sjkoshy	/*
2042145774Sjkoshy	 * We shouldn't have already allocated a process-mode PMC at
2043145774Sjkoshy	 * row index 'ri'.
2044145774Sjkoshy	 *
2045145774Sjkoshy	 * We shouldn't have allocated a system-wide PMC on the same
2046145774Sjkoshy	 * CPU and same RI.
2047145774Sjkoshy	 */
2048145256Sjkoshy	if ((po = pmc_find_owner_descriptor(p)) != NULL)
2049147191Sjkoshy		LIST_FOREACH(pm, &po->po_pmcs, pm_next) {
2050145774Sjkoshy		    if (PMC_TO_ROWINDEX(pm) == ri) {
2051145774Sjkoshy			    mode = PMC_TO_MODE(pm);
2052145774Sjkoshy			    if (PMC_IS_VIRTUAL_MODE(mode))
2053145774Sjkoshy				    return EEXIST;
2054145774Sjkoshy			    if (PMC_IS_SYSTEM_MODE(mode) &&
2055145774Sjkoshy				(int) PMC_TO_CPU(pm) == cpu)
2056145774Sjkoshy				    return EEXIST;
2057145774Sjkoshy		    }
2058145774Sjkoshy	        }
2059145256Sjkoshy
2060145774Sjkoshy	/*
2061145774Sjkoshy	 * We also shouldn't be the target of any PMC at this index
2062145774Sjkoshy	 * since otherwise a PMC_ATTACH to ourselves will fail.
2063145774Sjkoshy	 */
2064145256Sjkoshy	if ((pp = pmc_find_process_descriptor(p, 0)) != NULL)
2065145256Sjkoshy		if (pp->pp_pmcs[ri].pp_pmc)
2066145256Sjkoshy			return EEXIST;
2067145256Sjkoshy
2068145256Sjkoshy	PMCDBG(PMC,ALR,2, "can-allocate-rowindex proc=%p (%d, %s) ri=%d ok",
2069145256Sjkoshy	    p, p->p_pid, p->p_comm, ri);
2070145256Sjkoshy
2071145256Sjkoshy	return 0;
2072145256Sjkoshy}
2073145256Sjkoshy
2074145256Sjkoshy/*
2075145256Sjkoshy * Check if a given PMC at row index 'ri' can be currently used in
2076145256Sjkoshy * mode 'mode'.
2077145256Sjkoshy */
2078145256Sjkoshy
2079145256Sjkoshystatic int
2080145256Sjkoshypmc_can_allocate_row(int ri, enum pmc_mode mode)
2081145256Sjkoshy{
2082145256Sjkoshy	enum pmc_disp	disp;
2083145256Sjkoshy
2084145256Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
2085145256Sjkoshy
2086145256Sjkoshy	PMCDBG(PMC,ALR,1, "can-allocate-row ri=%d mode=%d", ri, mode);
2087145256Sjkoshy
2088145256Sjkoshy	if (PMC_IS_SYSTEM_MODE(mode))
2089145256Sjkoshy		disp = PMC_DISP_STANDALONE;
2090145256Sjkoshy	else
2091145256Sjkoshy		disp = PMC_DISP_THREAD;
2092145256Sjkoshy
2093145256Sjkoshy	/*
2094145256Sjkoshy	 * check disposition for PMC row 'ri':
2095145256Sjkoshy	 *
2096145256Sjkoshy	 * Expected disposition		Row-disposition		Result
2097145256Sjkoshy	 *
2098145256Sjkoshy	 * STANDALONE			STANDALONE or FREE	proceed
2099145256Sjkoshy	 * STANDALONE			THREAD			fail
2100145256Sjkoshy	 * THREAD			THREAD or FREE		proceed
2101145256Sjkoshy	 * THREAD			STANDALONE		fail
2102145256Sjkoshy	 */
2103145256Sjkoshy
2104145256Sjkoshy	if (!PMC_ROW_DISP_IS_FREE(ri) &&
2105145256Sjkoshy	    !(disp == PMC_DISP_THREAD && PMC_ROW_DISP_IS_THREAD(ri)) &&
2106145256Sjkoshy	    !(disp == PMC_DISP_STANDALONE && PMC_ROW_DISP_IS_STANDALONE(ri)))
2107145256Sjkoshy		return EBUSY;
2108145256Sjkoshy
2109145256Sjkoshy	/*
2110145256Sjkoshy	 * All OK
2111145256Sjkoshy	 */
2112145256Sjkoshy
2113145256Sjkoshy	PMCDBG(PMC,ALR,2, "can-allocate-row ri=%d mode=%d ok", ri, mode);
2114145256Sjkoshy
2115145256Sjkoshy	return 0;
2116145256Sjkoshy
2117145256Sjkoshy}
2118145256Sjkoshy
2119145256Sjkoshy/*
2120145774Sjkoshy * Find a PMC descriptor with user handle 'pmcid' for thread 'td'.
2121145256Sjkoshy */
2122145256Sjkoshy
2123145256Sjkoshystatic struct pmc *
2124145256Sjkoshypmc_find_pmc_descriptor_in_process(struct pmc_owner *po, pmc_id_t pmcid)
2125145256Sjkoshy{
2126147191Sjkoshy	struct pmc *pm;
2127145256Sjkoshy
2128145774Sjkoshy	KASSERT(PMC_ID_TO_ROWINDEX(pmcid) < md->pmd_npmc,
2129145774Sjkoshy	    ("[pmc,%d] Illegal pmc index %d (max %d)", __LINE__,
2130145774Sjkoshy		PMC_ID_TO_ROWINDEX(pmcid), md->pmd_npmc));
2131145256Sjkoshy
2132147191Sjkoshy	LIST_FOREACH(pm, &po->po_pmcs, pm_next)
2133147191Sjkoshy	    if (pm->pm_id == pmcid)
2134147191Sjkoshy		    return pm;
2135145256Sjkoshy
2136145256Sjkoshy	return NULL;
2137145256Sjkoshy}
2138145256Sjkoshy
2139145256Sjkoshystatic int
2140145256Sjkoshypmc_find_pmc(pmc_id_t pmcid, struct pmc **pmc)
2141145256Sjkoshy{
2142145256Sjkoshy
2143145256Sjkoshy	struct pmc *pm;
2144145256Sjkoshy	struct pmc_owner *po;
2145145256Sjkoshy
2146145256Sjkoshy	PMCDBG(PMC,FND,1, "find-pmc id=%d", pmcid);
2147145256Sjkoshy
2148145256Sjkoshy	if ((po = pmc_find_owner_descriptor(curthread->td_proc)) == NULL)
2149145256Sjkoshy		return ESRCH;
2150145256Sjkoshy
2151145256Sjkoshy	if ((pm = pmc_find_pmc_descriptor_in_process(po, pmcid)) == NULL)
2152145256Sjkoshy		return EINVAL;
2153145256Sjkoshy
2154145256Sjkoshy	PMCDBG(PMC,FND,2, "find-pmc id=%d -> pmc=%p", pmcid, pm);
2155145256Sjkoshy
2156145256Sjkoshy	*pmc = pm;
2157145256Sjkoshy	return 0;
2158145256Sjkoshy}
2159145256Sjkoshy
2160145256Sjkoshy/*
2161145256Sjkoshy * Start a PMC.
2162145256Sjkoshy */
2163145256Sjkoshy
2164145256Sjkoshystatic int
2165145256Sjkoshypmc_start(struct pmc *pm)
2166145256Sjkoshy{
2167145256Sjkoshy	int error, cpu, ri;
2168145774Sjkoshy	enum pmc_mode mode;
2169147191Sjkoshy	struct pmc_owner *po;
2170145256Sjkoshy	struct pmc_binding pb;
2171145256Sjkoshy
2172145256Sjkoshy	KASSERT(pm != NULL,
2173145256Sjkoshy	    ("[pmc,%d] null pm", __LINE__));
2174145256Sjkoshy
2175145774Sjkoshy	mode = PMC_TO_MODE(pm);
2176145774Sjkoshy	ri   = PMC_TO_ROWINDEX(pm);
2177145774Sjkoshy	error = 0;
2178145256Sjkoshy
2179145774Sjkoshy	PMCDBG(PMC,OPS,1, "start pmc=%p mode=%d ri=%d", pm, mode, ri);
2180145774Sjkoshy
2181147191Sjkoshy	po = pm->pm_owner;
2182145256Sjkoshy
2183145774Sjkoshy	if (PMC_IS_VIRTUAL_MODE(mode)) {
2184145256Sjkoshy
2185145256Sjkoshy		/*
2186147191Sjkoshy		 * If a PMCATTACH has never been done on this PMC,
2187147191Sjkoshy		 * attach it to its owner process.
2188145256Sjkoshy		 */
2189145256Sjkoshy
2190145256Sjkoshy		if (LIST_EMPTY(&pm->pm_targets))
2191147191Sjkoshy			error = (pm->pm_flags & PMC_F_ATTACH_DONE) ? ESRCH :
2192147191Sjkoshy			    pmc_attach_process(po->po_owner, pm);
2193145256Sjkoshy
2194145774Sjkoshy		/*
2195147191Sjkoshy		 * Disallow PMCSTART if a logfile is required but has not
2196147191Sjkoshy		 * been configured yet.
2197145774Sjkoshy		 */
2198145256Sjkoshy
2199147191Sjkoshy		if (error == 0 && (pm->pm_flags & PMC_F_NEEDS_LOGFILE) &&
2200147191Sjkoshy		    (po->po_flags & PMC_PO_OWNS_LOGFILE) == 0)
2201147191Sjkoshy			error = EDOOFUS;
2202147191Sjkoshy
2203145256Sjkoshy		/*
2204147191Sjkoshy		 * If the PMC is attached to its owner, then force a context
2205147191Sjkoshy		 * switch to ensure that the MD state gets set correctly.
2206145256Sjkoshy		 */
2207145256Sjkoshy
2208147191Sjkoshy		if (error == 0) {
2209147191Sjkoshy			pm->pm_state = PMC_STATE_RUNNING;
2210147191Sjkoshy			if (pm->pm_flags & PMC_F_ATTACHED_TO_OWNER)
2211147191Sjkoshy				pmc_force_context_switch();
2212147191Sjkoshy		}
2213147191Sjkoshy
2214145774Sjkoshy		return error;
2215147191Sjkoshy	}
2216145256Sjkoshy
2217147191Sjkoshy
2218147191Sjkoshy	/*
2219147191Sjkoshy	 * A system-wide PMC.
2220147191Sjkoshy	 */
2221147191Sjkoshy
2222147191Sjkoshy	if ((pm->pm_flags & PMC_F_NEEDS_LOGFILE) &&
2223147191Sjkoshy	    (po->po_flags & PMC_PO_OWNS_LOGFILE) == 0)
2224147191Sjkoshy		return EDOOFUS;	/* programming error */
2225147191Sjkoshy
2226147191Sjkoshy	/*
2227147191Sjkoshy	 * Add the owner to the global list if this is a system-wide
2228147191Sjkoshy	 * sampling PMC.
2229147191Sjkoshy	 */
2230147191Sjkoshy
2231147191Sjkoshy	if (mode == PMC_MODE_SS) {
2232147191Sjkoshy		if (po->po_sscount == 0) {
2233147191Sjkoshy			LIST_INSERT_HEAD(&pmc_ss_owners, po, po_ssnext);
2234147191Sjkoshy			atomic_add_rel_int(&pmc_ss_count, 1);
2235147191Sjkoshy			PMCDBG(PMC,OPS,1, "po=%p in global list", po);
2236147191Sjkoshy		}
2237147191Sjkoshy		po->po_sscount++;
2238145256Sjkoshy	}
2239145256Sjkoshy
2240145256Sjkoshy	/*
2241147191Sjkoshy	 * Move to the CPU associated with this
2242145256Sjkoshy	 * PMC, and start the hardware.
2243145256Sjkoshy	 */
2244145256Sjkoshy
2245145256Sjkoshy	pmc_save_cpu_binding(&pb);
2246145256Sjkoshy
2247145774Sjkoshy	cpu = PMC_TO_CPU(pm);
2248145256Sjkoshy
2249145256Sjkoshy	if (pmc_cpu_is_disabled(cpu))
2250145256Sjkoshy		return ENXIO;
2251145256Sjkoshy
2252145256Sjkoshy	pmc_select_cpu(cpu);
2253145256Sjkoshy
2254145256Sjkoshy	/*
2255145256Sjkoshy	 * global PMCs are configured at allocation time
2256145256Sjkoshy	 * so write out the initial value and start the PMC.
2257145256Sjkoshy	 */
2258145256Sjkoshy
2259147191Sjkoshy	pm->pm_state = PMC_STATE_RUNNING;
2260147191Sjkoshy
2261145774Sjkoshy	critical_enter();
2262145256Sjkoshy	if ((error = md->pmd_write_pmc(cpu, ri,
2263145774Sjkoshy		 PMC_IS_SAMPLING_MODE(mode) ?
2264145256Sjkoshy		 pm->pm_sc.pm_reloadcount :
2265145256Sjkoshy		 pm->pm_sc.pm_initial)) == 0)
2266145256Sjkoshy		error = md->pmd_start_pmc(cpu, ri);
2267145774Sjkoshy	critical_exit();
2268145256Sjkoshy
2269145256Sjkoshy	pmc_restore_cpu_binding(&pb);
2270145256Sjkoshy
2271145256Sjkoshy	return error;
2272145256Sjkoshy}
2273145256Sjkoshy
2274145256Sjkoshy/*
2275145256Sjkoshy * Stop a PMC.
2276145256Sjkoshy */
2277145256Sjkoshy
2278145256Sjkoshystatic int
2279145256Sjkoshypmc_stop(struct pmc *pm)
2280145256Sjkoshy{
2281145774Sjkoshy	int cpu, error, ri;
2282147191Sjkoshy	struct pmc_owner *po;
2283145256Sjkoshy	struct pmc_binding pb;
2284145256Sjkoshy
2285145256Sjkoshy	KASSERT(pm != NULL, ("[pmc,%d] null pmc", __LINE__));
2286145256Sjkoshy
2287145774Sjkoshy	PMCDBG(PMC,OPS,1, "stop pmc=%p mode=%d ri=%d", pm,
2288145774Sjkoshy	    PMC_TO_MODE(pm), PMC_TO_ROWINDEX(pm));
2289145256Sjkoshy
2290145256Sjkoshy	pm->pm_state = PMC_STATE_STOPPED;
2291145256Sjkoshy
2292145256Sjkoshy	/*
2293145256Sjkoshy	 * If the PMC is a virtual mode one, changing the state to
2294145256Sjkoshy	 * non-RUNNING is enough to ensure that the PMC never gets
2295145256Sjkoshy	 * scheduled.
2296145256Sjkoshy	 *
2297145256Sjkoshy	 * If this PMC is current running on a CPU, then it will
2298145256Sjkoshy	 * handled correctly at the time its target process is context
2299145256Sjkoshy	 * switched out.
2300145256Sjkoshy	 */
2301145256Sjkoshy
2302145774Sjkoshy	if (PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm)))
2303145256Sjkoshy		return 0;
2304145256Sjkoshy
2305145256Sjkoshy	/*
2306145256Sjkoshy	 * A system-mode PMC.  Move to the CPU associated with
2307145256Sjkoshy	 * this PMC, and stop the hardware.  We update the
2308145256Sjkoshy	 * 'initial count' so that a subsequent PMCSTART will
2309145256Sjkoshy	 * resume counting from the current hardware count.
2310145256Sjkoshy	 */
2311145256Sjkoshy
2312145256Sjkoshy	pmc_save_cpu_binding(&pb);
2313145256Sjkoshy
2314145774Sjkoshy	cpu = PMC_TO_CPU(pm);
2315145256Sjkoshy
2316145774Sjkoshy	KASSERT(cpu >= 0 && cpu < mp_ncpus,
2317145774Sjkoshy	    ("[pmc,%d] illegal cpu=%d", __LINE__, cpu));
2318145774Sjkoshy
2319145256Sjkoshy	if (pmc_cpu_is_disabled(cpu))
2320145256Sjkoshy		return ENXIO;
2321145256Sjkoshy
2322145256Sjkoshy	pmc_select_cpu(cpu);
2323145256Sjkoshy
2324145774Sjkoshy	ri = PMC_TO_ROWINDEX(pm);
2325145256Sjkoshy
2326145774Sjkoshy	critical_enter();
2327145774Sjkoshy	if ((error = md->pmd_stop_pmc(cpu, ri)) == 0)
2328145774Sjkoshy		error = md->pmd_read_pmc(cpu, ri, &pm->pm_sc.pm_initial);
2329145774Sjkoshy	critical_exit();
2330145774Sjkoshy
2331145256Sjkoshy	pmc_restore_cpu_binding(&pb);
2332145256Sjkoshy
2333147191Sjkoshy	po = pm->pm_owner;
2334147191Sjkoshy
2335147191Sjkoshy	/* remove this owner from the global list of SS PMC owners */
2336147191Sjkoshy	if (PMC_TO_MODE(pm) == PMC_MODE_SS) {
2337147191Sjkoshy		po->po_sscount--;
2338147191Sjkoshy		if (po->po_sscount == 0) {
2339147191Sjkoshy			atomic_subtract_rel_int(&pmc_ss_count, 1);
2340147191Sjkoshy			LIST_REMOVE(po, po_ssnext);
2341147191Sjkoshy			PMCDBG(PMC,OPS,2,"po=%p removed from global list", po);
2342147191Sjkoshy		}
2343147191Sjkoshy	}
2344147191Sjkoshy
2345145256Sjkoshy	return error;
2346145256Sjkoshy}
2347145256Sjkoshy
2348145256Sjkoshy
2349153110Sru#ifdef	DEBUG
2350145256Sjkoshystatic const char *pmc_op_to_name[] = {
2351145256Sjkoshy#undef	__PMC_OP
2352145256Sjkoshy#define	__PMC_OP(N, D)	#N ,
2353145256Sjkoshy	__PMC_OPS()
2354145256Sjkoshy	NULL
2355145256Sjkoshy};
2356145256Sjkoshy#endif
2357145256Sjkoshy
2358145256Sjkoshy/*
2359145256Sjkoshy * The syscall interface
2360145256Sjkoshy */
2361145256Sjkoshy
2362145256Sjkoshy#define	PMC_GET_SX_XLOCK(...) do {		\
2363145256Sjkoshy	sx_xlock(&pmc_sx);			\
2364145256Sjkoshy	if (pmc_hook == NULL) {			\
2365145256Sjkoshy		sx_xunlock(&pmc_sx);		\
2366145256Sjkoshy		return __VA_ARGS__;		\
2367145256Sjkoshy	}					\
2368145256Sjkoshy} while (0)
2369145256Sjkoshy
2370145256Sjkoshy#define	PMC_DOWNGRADE_SX() do {			\
2371145256Sjkoshy	sx_downgrade(&pmc_sx);			\
2372145256Sjkoshy	is_sx_downgraded = 1;			\
2373145256Sjkoshy} while (0)
2374145256Sjkoshy
2375145256Sjkoshystatic int
2376145256Sjkoshypmc_syscall_handler(struct thread *td, void *syscall_args)
2377145256Sjkoshy{
2378145256Sjkoshy	int error, is_sx_downgraded, op;
2379145256Sjkoshy	struct pmc_syscall_args *c;
2380145256Sjkoshy	void *arg;
2381145256Sjkoshy
2382145256Sjkoshy	PMC_GET_SX_XLOCK(ENOSYS);
2383145256Sjkoshy
2384147191Sjkoshy	DROP_GIANT();
2385147191Sjkoshy
2386145256Sjkoshy	is_sx_downgraded = 0;
2387145256Sjkoshy
2388145256Sjkoshy	c = (struct pmc_syscall_args *) syscall_args;
2389145256Sjkoshy
2390145256Sjkoshy	op = c->pmop_code;
2391145256Sjkoshy	arg = c->pmop_data;
2392145256Sjkoshy
2393145256Sjkoshy	PMCDBG(MOD,PMS,1, "syscall op=%d \"%s\" arg=%p", op,
2394145256Sjkoshy	    pmc_op_to_name[op], arg);
2395145256Sjkoshy
2396145256Sjkoshy	error = 0;
2397145256Sjkoshy	atomic_add_int(&pmc_stats.pm_syscalls, 1);
2398145256Sjkoshy
2399145256Sjkoshy	switch(op)
2400145256Sjkoshy	{
2401145256Sjkoshy
2402145256Sjkoshy
2403145256Sjkoshy	/*
2404145256Sjkoshy	 * Configure a log file.
2405145256Sjkoshy	 *
2406145256Sjkoshy	 * XXX This OP will be reworked.
2407145256Sjkoshy	 */
2408145256Sjkoshy
2409145256Sjkoshy	case PMC_OP_CONFIGURELOG:
2410145256Sjkoshy	{
2411145256Sjkoshy		struct pmc_owner *po;
2412145256Sjkoshy		struct pmc_op_configurelog cl;
2413145256Sjkoshy		struct proc *p;
2414145256Sjkoshy
2415145256Sjkoshy		sx_assert(&pmc_sx, SX_XLOCKED);
2416145256Sjkoshy
2417145256Sjkoshy		if ((error = copyin(arg, &cl, sizeof(cl))) != 0)
2418145256Sjkoshy			break;
2419145256Sjkoshy
2420145256Sjkoshy		/* mark this process as owning a log file */
2421145256Sjkoshy		p = td->td_proc;
2422145256Sjkoshy		if ((po = pmc_find_owner_descriptor(p)) == NULL)
2423147191Sjkoshy			if ((po = pmc_allocate_owner_descriptor(p)) == NULL) {
2424147191Sjkoshy				error = ENOMEM;
2425147191Sjkoshy				break;
2426147191Sjkoshy			}
2427145256Sjkoshy
2428147191Sjkoshy		/*
2429147191Sjkoshy		 * If a valid fd was passed in, try to configure that,
2430147191Sjkoshy		 * otherwise if 'fd' was less than zero and there was
2431147191Sjkoshy		 * a log file configured, flush its buffers and
2432147191Sjkoshy		 * de-configure it.
2433147191Sjkoshy		 */
2434147191Sjkoshy		if (cl.pm_logfd >= 0)
2435147191Sjkoshy			error = pmclog_configure_log(po, cl.pm_logfd);
2436147191Sjkoshy		else if (po->po_flags & PMC_PO_OWNS_LOGFILE) {
2437147191Sjkoshy			pmclog_process_closelog(po);
2438147191Sjkoshy			error = pmclog_flush(po);
2439147191Sjkoshy			if (error == 0)
2440147191Sjkoshy				error = pmclog_deconfigure_log(po);
2441147191Sjkoshy		} else
2442147191Sjkoshy			error = EINVAL;
2443147191Sjkoshy	}
2444147191Sjkoshy	break;
2445147191Sjkoshy
2446147191Sjkoshy
2447147191Sjkoshy	/*
2448147191Sjkoshy	 * Flush a log file.
2449147191Sjkoshy	 */
2450147191Sjkoshy
2451147191Sjkoshy	case PMC_OP_FLUSHLOG:
2452147191Sjkoshy	{
2453147191Sjkoshy		struct pmc_owner *po;
2454147191Sjkoshy
2455147191Sjkoshy		sx_assert(&pmc_sx, SX_XLOCKED);
2456147191Sjkoshy
2457147191Sjkoshy		if ((po = pmc_find_owner_descriptor(td->td_proc)) == NULL) {
2458147191Sjkoshy			error = EINVAL;
2459145256Sjkoshy			break;
2460147191Sjkoshy		}
2461145256Sjkoshy
2462147191Sjkoshy		error = pmclog_flush(po);
2463145256Sjkoshy	}
2464145256Sjkoshy	break;
2465145256Sjkoshy
2466145256Sjkoshy	/*
2467145256Sjkoshy	 * Retrieve hardware configuration.
2468145256Sjkoshy	 */
2469145256Sjkoshy
2470145256Sjkoshy	case PMC_OP_GETCPUINFO:	/* CPU information */
2471145256Sjkoshy	{
2472145256Sjkoshy		struct pmc_op_getcpuinfo gci;
2473145256Sjkoshy
2474145256Sjkoshy		gci.pm_cputype = md->pmd_cputype;
2475145774Sjkoshy		gci.pm_ncpu    = mp_ncpus;
2476145256Sjkoshy		gci.pm_npmc    = md->pmd_npmc;
2477145256Sjkoshy		gci.pm_nclass  = md->pmd_nclass;
2478145256Sjkoshy		bcopy(md->pmd_classes, &gci.pm_classes,
2479145256Sjkoshy		    sizeof(gci.pm_classes));
2480145256Sjkoshy		error = copyout(&gci, arg, sizeof(gci));
2481145256Sjkoshy	}
2482145256Sjkoshy	break;
2483145256Sjkoshy
2484145256Sjkoshy
2485145256Sjkoshy	/*
2486145256Sjkoshy	 * Get module statistics
2487145256Sjkoshy	 */
2488145256Sjkoshy
2489145256Sjkoshy	case PMC_OP_GETDRIVERSTATS:
2490145256Sjkoshy	{
2491145256Sjkoshy		struct pmc_op_getdriverstats gms;
2492145256Sjkoshy
2493145256Sjkoshy		bcopy(&pmc_stats, &gms, sizeof(gms));
2494145256Sjkoshy		error = copyout(&gms, arg, sizeof(gms));
2495145256Sjkoshy	}
2496145256Sjkoshy	break;
2497145256Sjkoshy
2498145256Sjkoshy
2499145256Sjkoshy	/*
2500145256Sjkoshy	 * Retrieve module version number
2501145256Sjkoshy	 */
2502145256Sjkoshy
2503145256Sjkoshy	case PMC_OP_GETMODULEVERSION:
2504145256Sjkoshy	{
2505147191Sjkoshy		uint32_t cv, modv;
2506147191Sjkoshy
2507147191Sjkoshy		/* retrieve the client's idea of the ABI version */
2508147191Sjkoshy		if ((error = copyin(arg, &cv, sizeof(uint32_t))) != 0)
2509147191Sjkoshy			break;
2510147191Sjkoshy		/* don't service clients newer than our driver */
2511147191Sjkoshy		modv = PMC_VERSION;
2512147191Sjkoshy		if ((cv & 0xFFFF0000) > (modv & 0xFFFF0000)) {
2513147191Sjkoshy			error = EPROGMISMATCH;
2514147191Sjkoshy			break;
2515147191Sjkoshy		}
2516147191Sjkoshy		error = copyout(&modv, arg, sizeof(int));
2517145256Sjkoshy	}
2518145256Sjkoshy	break;
2519145256Sjkoshy
2520145256Sjkoshy
2521145256Sjkoshy	/*
2522145256Sjkoshy	 * Retrieve the state of all the PMCs on a given
2523145256Sjkoshy	 * CPU.
2524145256Sjkoshy	 */
2525145256Sjkoshy
2526145256Sjkoshy	case PMC_OP_GETPMCINFO:
2527145256Sjkoshy	{
2528145256Sjkoshy		uint32_t cpu, n, npmc;
2529145256Sjkoshy		size_t pmcinfo_size;
2530145256Sjkoshy		struct pmc *pm;
2531145256Sjkoshy		struct pmc_info *p, *pmcinfo;
2532145256Sjkoshy		struct pmc_op_getpmcinfo *gpi;
2533145256Sjkoshy		struct pmc_owner *po;
2534145256Sjkoshy		struct pmc_binding pb;
2535145256Sjkoshy
2536145256Sjkoshy		PMC_DOWNGRADE_SX();
2537145256Sjkoshy
2538145256Sjkoshy		gpi = (struct pmc_op_getpmcinfo *) arg;
2539145256Sjkoshy
2540145256Sjkoshy		if ((error = copyin(&gpi->pm_cpu, &cpu, sizeof(cpu))) != 0)
2541145256Sjkoshy			break;
2542145256Sjkoshy
2543145256Sjkoshy		if (cpu >= (unsigned int) mp_ncpus) {
2544145256Sjkoshy			error = EINVAL;
2545145256Sjkoshy			break;
2546145256Sjkoshy		}
2547145256Sjkoshy
2548145256Sjkoshy		if (pmc_cpu_is_disabled(cpu)) {
2549145256Sjkoshy			error = ENXIO;
2550145256Sjkoshy			break;
2551145256Sjkoshy		}
2552145256Sjkoshy
2553145256Sjkoshy		/* switch to CPU 'cpu' */
2554145256Sjkoshy		pmc_save_cpu_binding(&pb);
2555145256Sjkoshy		pmc_select_cpu(cpu);
2556145256Sjkoshy
2557145256Sjkoshy		npmc = md->pmd_npmc;
2558145256Sjkoshy
2559145256Sjkoshy		pmcinfo_size = npmc * sizeof(struct pmc_info);
2560145256Sjkoshy		MALLOC(pmcinfo, struct pmc_info *, pmcinfo_size, M_PMC,
2561145256Sjkoshy		    M_WAITOK);
2562145256Sjkoshy
2563145256Sjkoshy		p = pmcinfo;
2564145256Sjkoshy
2565145256Sjkoshy		for (n = 0; n < md->pmd_npmc; n++, p++) {
2566145256Sjkoshy
2567145256Sjkoshy			if ((error = md->pmd_describe(cpu, n, p, &pm)) != 0)
2568145256Sjkoshy				break;
2569145256Sjkoshy
2570145256Sjkoshy			if (PMC_ROW_DISP_IS_STANDALONE(n))
2571145256Sjkoshy				p->pm_rowdisp = PMC_DISP_STANDALONE;
2572145256Sjkoshy			else if (PMC_ROW_DISP_IS_THREAD(n))
2573145256Sjkoshy				p->pm_rowdisp = PMC_DISP_THREAD;
2574145256Sjkoshy			else
2575145256Sjkoshy				p->pm_rowdisp = PMC_DISP_FREE;
2576145256Sjkoshy
2577145256Sjkoshy			p->pm_ownerpid = -1;
2578145256Sjkoshy
2579145256Sjkoshy			if (pm == NULL)	/* no PMC associated */
2580145256Sjkoshy				continue;
2581145256Sjkoshy
2582145256Sjkoshy			po = pm->pm_owner;
2583145256Sjkoshy
2584145256Sjkoshy			KASSERT(po->po_owner != NULL,
2585145256Sjkoshy			    ("[pmc,%d] pmc_owner had a null proc pointer",
2586145256Sjkoshy				__LINE__));
2587145256Sjkoshy
2588145256Sjkoshy			p->pm_ownerpid = po->po_owner->p_pid;
2589145774Sjkoshy			p->pm_mode     = PMC_TO_MODE(pm);
2590145256Sjkoshy			p->pm_event    = pm->pm_event;
2591145256Sjkoshy			p->pm_flags    = pm->pm_flags;
2592145256Sjkoshy
2593145774Sjkoshy			if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
2594145256Sjkoshy				p->pm_reloadcount =
2595145256Sjkoshy				    pm->pm_sc.pm_reloadcount;
2596145256Sjkoshy		}
2597145256Sjkoshy
2598145256Sjkoshy		pmc_restore_cpu_binding(&pb);
2599145256Sjkoshy
2600145256Sjkoshy		/* now copy out the PMC info collected */
2601145256Sjkoshy		if (error == 0)
2602145256Sjkoshy			error = copyout(pmcinfo, &gpi->pm_pmcs, pmcinfo_size);
2603145256Sjkoshy
2604145256Sjkoshy		FREE(pmcinfo, M_PMC);
2605145256Sjkoshy	}
2606145256Sjkoshy	break;
2607145256Sjkoshy
2608145256Sjkoshy
2609145256Sjkoshy	/*
2610145256Sjkoshy	 * Set the administrative state of a PMC.  I.e. whether
2611145256Sjkoshy	 * the PMC is to be used or not.
2612145256Sjkoshy	 */
2613145256Sjkoshy
2614145256Sjkoshy	case PMC_OP_PMCADMIN:
2615145256Sjkoshy	{
2616145256Sjkoshy		int cpu, ri;
2617145256Sjkoshy		enum pmc_state request;
2618145256Sjkoshy		struct pmc_cpu *pc;
2619145256Sjkoshy		struct pmc_hw *phw;
2620145256Sjkoshy		struct pmc_op_pmcadmin pma;
2621145256Sjkoshy		struct pmc_binding pb;
2622145256Sjkoshy
2623145256Sjkoshy		sx_assert(&pmc_sx, SX_XLOCKED);
2624145256Sjkoshy
2625145256Sjkoshy		KASSERT(td == curthread,
2626145256Sjkoshy		    ("[pmc,%d] td != curthread", __LINE__));
2627145256Sjkoshy
2628145256Sjkoshy		if (suser(td) || jailed(td->td_ucred)) {
2629145256Sjkoshy			error =  EPERM;
2630145256Sjkoshy			break;
2631145256Sjkoshy		}
2632145256Sjkoshy
2633145256Sjkoshy		if ((error = copyin(arg, &pma, sizeof(pma))) != 0)
2634145256Sjkoshy			break;
2635145256Sjkoshy
2636145256Sjkoshy		cpu = pma.pm_cpu;
2637145256Sjkoshy
2638145256Sjkoshy		if (cpu < 0 || cpu >= mp_ncpus) {
2639145256Sjkoshy			error = EINVAL;
2640145256Sjkoshy			break;
2641145256Sjkoshy		}
2642145256Sjkoshy
2643145256Sjkoshy		if (pmc_cpu_is_disabled(cpu)) {
2644145256Sjkoshy			error = ENXIO;
2645145256Sjkoshy			break;
2646145256Sjkoshy		}
2647145256Sjkoshy
2648145256Sjkoshy		request = pma.pm_state;
2649145256Sjkoshy
2650145256Sjkoshy		if (request != PMC_STATE_DISABLED &&
2651145256Sjkoshy		    request != PMC_STATE_FREE) {
2652145256Sjkoshy			error = EINVAL;
2653145256Sjkoshy			break;
2654145256Sjkoshy		}
2655145256Sjkoshy
2656145256Sjkoshy		ri = pma.pm_pmc; /* pmc id == row index */
2657145256Sjkoshy		if (ri < 0 || ri >= (int) md->pmd_npmc) {
2658145256Sjkoshy			error = EINVAL;
2659145256Sjkoshy			break;
2660145256Sjkoshy		}
2661145256Sjkoshy
2662145256Sjkoshy		/*
2663145256Sjkoshy		 * We can't disable a PMC with a row-index allocated
2664145256Sjkoshy		 * for process virtual PMCs.
2665145256Sjkoshy		 */
2666145256Sjkoshy
2667145256Sjkoshy		if (PMC_ROW_DISP_IS_THREAD(ri) &&
2668145256Sjkoshy		    request == PMC_STATE_DISABLED) {
2669145256Sjkoshy			error = EBUSY;
2670145256Sjkoshy			break;
2671145256Sjkoshy		}
2672145256Sjkoshy
2673145256Sjkoshy		/*
2674145256Sjkoshy		 * otherwise, this PMC on this CPU is either free or
2675145256Sjkoshy		 * in system-wide mode.
2676145256Sjkoshy		 */
2677145256Sjkoshy
2678145256Sjkoshy		pmc_save_cpu_binding(&pb);
2679145256Sjkoshy		pmc_select_cpu(cpu);
2680145256Sjkoshy
2681145256Sjkoshy		pc  = pmc_pcpu[cpu];
2682145256Sjkoshy		phw = pc->pc_hwpmcs[ri];
2683145256Sjkoshy
2684145256Sjkoshy		/*
2685145256Sjkoshy		 * XXX do we need some kind of 'forced' disable?
2686145256Sjkoshy		 */
2687145256Sjkoshy
2688145256Sjkoshy		if (phw->phw_pmc == NULL) {
2689145256Sjkoshy			if (request == PMC_STATE_DISABLED &&
2690145256Sjkoshy			    (phw->phw_state & PMC_PHW_FLAG_IS_ENABLED)) {
2691145256Sjkoshy				phw->phw_state &= ~PMC_PHW_FLAG_IS_ENABLED;
2692145256Sjkoshy				PMC_MARK_ROW_STANDALONE(ri);
2693145256Sjkoshy			} else if (request == PMC_STATE_FREE &&
2694145256Sjkoshy			    (phw->phw_state & PMC_PHW_FLAG_IS_ENABLED) == 0) {
2695145256Sjkoshy				phw->phw_state |=  PMC_PHW_FLAG_IS_ENABLED;
2696145256Sjkoshy				PMC_UNMARK_ROW_STANDALONE(ri);
2697145256Sjkoshy			}
2698145256Sjkoshy			/* other cases are a no-op */
2699145256Sjkoshy		} else
2700145256Sjkoshy			error = EBUSY;
2701145256Sjkoshy
2702145256Sjkoshy		pmc_restore_cpu_binding(&pb);
2703145256Sjkoshy	}
2704145256Sjkoshy	break;
2705145256Sjkoshy
2706145256Sjkoshy
2707145256Sjkoshy	/*
2708145256Sjkoshy	 * Allocate a PMC.
2709145256Sjkoshy	 */
2710145256Sjkoshy
2711145256Sjkoshy	case PMC_OP_PMCALLOCATE:
2712145256Sjkoshy	{
2713145256Sjkoshy		uint32_t caps;
2714145256Sjkoshy		u_int cpu;
2715145256Sjkoshy		int n;
2716145256Sjkoshy		enum pmc_mode mode;
2717145256Sjkoshy		struct pmc *pmc;
2718145774Sjkoshy		struct pmc_hw *phw;
2719145256Sjkoshy		struct pmc_op_pmcallocate pa;
2720145256Sjkoshy		struct pmc_binding pb;
2721145256Sjkoshy
2722145256Sjkoshy		if ((error = copyin(arg, &pa, sizeof(pa))) != 0)
2723145256Sjkoshy			break;
2724145256Sjkoshy
2725145256Sjkoshy		caps = pa.pm_caps;
2726145256Sjkoshy		mode = pa.pm_mode;
2727145256Sjkoshy		cpu  = pa.pm_cpu;
2728145256Sjkoshy
2729145256Sjkoshy		if ((mode != PMC_MODE_SS  &&  mode != PMC_MODE_SC  &&
2730145256Sjkoshy		     mode != PMC_MODE_TS  &&  mode != PMC_MODE_TC) ||
2731145256Sjkoshy		    (cpu != (u_int) PMC_CPU_ANY && cpu >= (u_int) mp_ncpus)) {
2732145256Sjkoshy			error = EINVAL;
2733145256Sjkoshy			break;
2734145256Sjkoshy		}
2735145256Sjkoshy
2736145256Sjkoshy		/*
2737145256Sjkoshy		 * Virtual PMCs should only ask for a default CPU.
2738145256Sjkoshy		 * System mode PMCs need to specify a non-default CPU.
2739145256Sjkoshy		 */
2740145256Sjkoshy
2741145256Sjkoshy		if ((PMC_IS_VIRTUAL_MODE(mode) && cpu != (u_int) PMC_CPU_ANY) ||
2742145256Sjkoshy		    (PMC_IS_SYSTEM_MODE(mode) && cpu == (u_int) PMC_CPU_ANY)) {
2743145256Sjkoshy			error = EINVAL;
2744145256Sjkoshy			break;
2745145256Sjkoshy		}
2746145256Sjkoshy
2747145256Sjkoshy		/*
2748145256Sjkoshy		 * Check that a disabled CPU is not being asked for.
2749145256Sjkoshy		 */
2750145256Sjkoshy
2751145256Sjkoshy		if (PMC_IS_SYSTEM_MODE(mode) && pmc_cpu_is_disabled(cpu)) {
2752145256Sjkoshy			error = ENXIO;
2753145256Sjkoshy			break;
2754145256Sjkoshy		}
2755145256Sjkoshy
2756145256Sjkoshy		/*
2757145256Sjkoshy		 * Refuse an allocation for a system-wide PMC if this
2758145256Sjkoshy		 * process has been jailed, or if this process lacks
2759145256Sjkoshy		 * super-user credentials and the sysctl tunable
2760145256Sjkoshy		 * 'security.bsd.unprivileged_syspmcs' is zero.
2761145256Sjkoshy		 */
2762145256Sjkoshy
2763145256Sjkoshy		if (PMC_IS_SYSTEM_MODE(mode)) {
2764145256Sjkoshy			if (jailed(curthread->td_ucred))
2765145256Sjkoshy				error = EPERM;
2766145256Sjkoshy			else if (suser(curthread) &&
2767145256Sjkoshy			    (pmc_unprivileged_syspmcs == 0))
2768145256Sjkoshy				error = EPERM;
2769145256Sjkoshy		}
2770145256Sjkoshy
2771145256Sjkoshy		if (error)
2772145256Sjkoshy			break;
2773145256Sjkoshy
2774145256Sjkoshy		/*
2775145256Sjkoshy		 * Look for valid values for 'pm_flags'
2776145256Sjkoshy		 */
2777145256Sjkoshy
2778147191Sjkoshy		if ((pa.pm_flags & ~(PMC_F_DESCENDANTS | PMC_F_LOG_PROCCSW |
2779147191Sjkoshy		    PMC_F_LOG_PROCEXIT)) != 0) {
2780145256Sjkoshy			error = EINVAL;
2781145256Sjkoshy			break;
2782145256Sjkoshy		}
2783145256Sjkoshy
2784147191Sjkoshy		/* process logging options are not allowed for system PMCs */
2785147191Sjkoshy		if (PMC_IS_SYSTEM_MODE(mode) && (pa.pm_flags &
2786147191Sjkoshy		    (PMC_F_LOG_PROCCSW | PMC_F_LOG_PROCEXIT))) {
2787147191Sjkoshy			error = EINVAL;
2788147191Sjkoshy			break;
2789147191Sjkoshy		}
2790147191Sjkoshy
2791145256Sjkoshy		/*
2792145256Sjkoshy		 * All sampling mode PMCs need to be able to interrupt the
2793145256Sjkoshy		 * CPU.
2794145256Sjkoshy		 */
2795147191Sjkoshy		if (PMC_IS_SAMPLING_MODE(mode))
2796145256Sjkoshy			caps |= PMC_CAP_INTERRUPT;
2797145256Sjkoshy
2798149374Sjkoshy		/* A valid class specifier should have been passed in. */
2799149374Sjkoshy		for (n = 0; n < md->pmd_nclass; n++)
2800149374Sjkoshy			if (md->pmd_classes[n].pm_class == pa.pm_class)
2801149374Sjkoshy				break;
2802149374Sjkoshy		if (n == md->pmd_nclass) {
2803149374Sjkoshy			error = EINVAL;
2804149374Sjkoshy			break;
2805149374Sjkoshy		}
2806149374Sjkoshy
2807149374Sjkoshy		/* The requested PMC capabilities should be feasible. */
2808149374Sjkoshy		if ((md->pmd_classes[n].pm_caps & caps) != caps) {
2809149374Sjkoshy			error = EOPNOTSUPP;
2810149374Sjkoshy			break;
2811149374Sjkoshy		}
2812149374Sjkoshy
2813145256Sjkoshy		PMCDBG(PMC,ALL,2, "event=%d caps=0x%x mode=%d cpu=%d",
2814145256Sjkoshy		    pa.pm_ev, caps, mode, cpu);
2815145256Sjkoshy
2816145256Sjkoshy		pmc = pmc_allocate_pmc_descriptor();
2817145774Sjkoshy		pmc->pm_id    = PMC_ID_MAKE_ID(cpu,pa.pm_mode,pa.pm_class,
2818145774Sjkoshy		    PMC_ID_INVALID);
2819145256Sjkoshy		pmc->pm_event = pa.pm_ev;
2820145256Sjkoshy		pmc->pm_state = PMC_STATE_FREE;
2821145256Sjkoshy		pmc->pm_caps  = caps;
2822145256Sjkoshy		pmc->pm_flags = pa.pm_flags;
2823145256Sjkoshy
2824145256Sjkoshy		/* switch thread to CPU 'cpu' */
2825145256Sjkoshy		pmc_save_cpu_binding(&pb);
2826145256Sjkoshy
2827145256Sjkoshy#define	PMC_IS_SHAREABLE_PMC(cpu, n)				\
2828145256Sjkoshy	(pmc_pcpu[(cpu)]->pc_hwpmcs[(n)]->phw_state &		\
2829145256Sjkoshy	 PMC_PHW_FLAG_IS_SHAREABLE)
2830145256Sjkoshy#define	PMC_IS_UNALLOCATED(cpu, n)				\
2831145256Sjkoshy	(pmc_pcpu[(cpu)]->pc_hwpmcs[(n)]->phw_pmc == NULL)
2832145256Sjkoshy
2833145256Sjkoshy		if (PMC_IS_SYSTEM_MODE(mode)) {
2834145256Sjkoshy			pmc_select_cpu(cpu);
2835145256Sjkoshy			for (n = 0; n < (int) md->pmd_npmc; n++)
2836145256Sjkoshy				if (pmc_can_allocate_row(n, mode) == 0 &&
2837145256Sjkoshy				    pmc_can_allocate_rowindex(
2838145774Sjkoshy					    curthread->td_proc, n, cpu) == 0 &&
2839145256Sjkoshy				    (PMC_IS_UNALLOCATED(cpu, n) ||
2840145256Sjkoshy				     PMC_IS_SHAREABLE_PMC(cpu, n)) &&
2841145256Sjkoshy				    md->pmd_allocate_pmc(cpu, n, pmc,
2842145256Sjkoshy					&pa) == 0)
2843145256Sjkoshy					break;
2844145256Sjkoshy		} else {
2845145256Sjkoshy			/* Process virtual mode */
2846145256Sjkoshy			for (n = 0; n < (int) md->pmd_npmc; n++) {
2847145256Sjkoshy				if (pmc_can_allocate_row(n, mode) == 0 &&
2848145256Sjkoshy				    pmc_can_allocate_rowindex(
2849145774Sjkoshy					    curthread->td_proc, n,
2850145774Sjkoshy					    PMC_CPU_ANY) == 0 &&
2851145256Sjkoshy				    md->pmd_allocate_pmc(curthread->td_oncpu,
2852145256Sjkoshy					n, pmc, &pa) == 0)
2853145256Sjkoshy					break;
2854145256Sjkoshy			}
2855145256Sjkoshy		}
2856145256Sjkoshy
2857145256Sjkoshy#undef	PMC_IS_UNALLOCATED
2858145256Sjkoshy#undef	PMC_IS_SHAREABLE_PMC
2859145256Sjkoshy
2860145256Sjkoshy		pmc_restore_cpu_binding(&pb);
2861145256Sjkoshy
2862145256Sjkoshy		if (n == (int) md->pmd_npmc) {
2863145256Sjkoshy			pmc_destroy_pmc_descriptor(pmc);
2864145256Sjkoshy			FREE(pmc, M_PMC);
2865145256Sjkoshy			pmc = NULL;
2866145256Sjkoshy			error = EINVAL;
2867145256Sjkoshy			break;
2868145256Sjkoshy		}
2869145256Sjkoshy
2870145774Sjkoshy		/* Fill in the correct value in the ID field */
2871145774Sjkoshy		pmc->pm_id = PMC_ID_MAKE_ID(cpu,mode,pa.pm_class,n);
2872145256Sjkoshy
2873145774Sjkoshy		PMCDBG(PMC,ALL,2, "ev=%d class=%d mode=%d n=%d -> pmcid=%x",
2874145774Sjkoshy		    pmc->pm_event, pa.pm_class, mode, n, pmc->pm_id);
2875145774Sjkoshy
2876147191Sjkoshy		/* Process mode PMCs with logging enabled need log files */
2877147191Sjkoshy		if (pmc->pm_flags & (PMC_F_LOG_PROCEXIT | PMC_F_LOG_PROCCSW))
2878147191Sjkoshy			pmc->pm_flags |= PMC_F_NEEDS_LOGFILE;
2879147191Sjkoshy
2880147191Sjkoshy		/* All system mode sampling PMCs require a log file */
2881147191Sjkoshy		if (PMC_IS_SAMPLING_MODE(mode) && PMC_IS_SYSTEM_MODE(mode))
2882147191Sjkoshy			pmc->pm_flags |= PMC_F_NEEDS_LOGFILE;
2883147191Sjkoshy
2884145256Sjkoshy		/*
2885145256Sjkoshy		 * Configure global pmc's immediately
2886145256Sjkoshy		 */
2887145256Sjkoshy
2888145774Sjkoshy		if (PMC_IS_SYSTEM_MODE(PMC_TO_MODE(pmc))) {
2889145774Sjkoshy
2890145774Sjkoshy			pmc_save_cpu_binding(&pb);
2891145774Sjkoshy			pmc_select_cpu(cpu);
2892145774Sjkoshy
2893145774Sjkoshy			phw = pmc_pcpu[cpu]->pc_hwpmcs[n];
2894145774Sjkoshy
2895145774Sjkoshy			if ((phw->phw_state & PMC_PHW_FLAG_IS_ENABLED) == 0 ||
2896145774Sjkoshy			    (error = md->pmd_config_pmc(cpu, n, pmc)) != 0) {
2897145256Sjkoshy				(void) md->pmd_release_pmc(cpu, n, pmc);
2898145256Sjkoshy				pmc_destroy_pmc_descriptor(pmc);
2899145256Sjkoshy				FREE(pmc, M_PMC);
2900145256Sjkoshy				pmc = NULL;
2901145774Sjkoshy				pmc_restore_cpu_binding(&pb);
2902145774Sjkoshy				error = EPERM;
2903145256Sjkoshy				break;
2904145256Sjkoshy			}
2905145256Sjkoshy
2906145774Sjkoshy			pmc_restore_cpu_binding(&pb);
2907145774Sjkoshy		}
2908145256Sjkoshy
2909145256Sjkoshy		pmc->pm_state    = PMC_STATE_ALLOCATED;
2910145256Sjkoshy
2911145256Sjkoshy		/*
2912145256Sjkoshy		 * mark row disposition
2913145256Sjkoshy		 */
2914145256Sjkoshy
2915145256Sjkoshy		if (PMC_IS_SYSTEM_MODE(mode))
2916145256Sjkoshy			PMC_MARK_ROW_STANDALONE(n);
2917145256Sjkoshy		else
2918145256Sjkoshy			PMC_MARK_ROW_THREAD(n);
2919145256Sjkoshy
2920145256Sjkoshy		/*
2921145256Sjkoshy		 * Register this PMC with the current thread as its owner.
2922145256Sjkoshy		 */
2923145256Sjkoshy
2924145256Sjkoshy		if ((error =
2925145256Sjkoshy		    pmc_register_owner(curthread->td_proc, pmc)) != 0) {
2926145256Sjkoshy			pmc_release_pmc_descriptor(pmc);
2927145256Sjkoshy			FREE(pmc, M_PMC);
2928145256Sjkoshy			pmc = NULL;
2929145256Sjkoshy			break;
2930145256Sjkoshy		}
2931145256Sjkoshy
2932145256Sjkoshy		/*
2933145256Sjkoshy		 * Return the allocated index.
2934145256Sjkoshy		 */
2935145256Sjkoshy
2936145774Sjkoshy		pa.pm_pmcid = pmc->pm_id;
2937145256Sjkoshy
2938145256Sjkoshy		error = copyout(&pa, arg, sizeof(pa));
2939145256Sjkoshy	}
2940145256Sjkoshy	break;
2941145256Sjkoshy
2942145256Sjkoshy
2943145256Sjkoshy	/*
2944145256Sjkoshy	 * Attach a PMC to a process.
2945145256Sjkoshy	 */
2946145256Sjkoshy
2947145256Sjkoshy	case PMC_OP_PMCATTACH:
2948145256Sjkoshy	{
2949145256Sjkoshy		struct pmc *pm;
2950145256Sjkoshy		struct proc *p;
2951145256Sjkoshy		struct pmc_op_pmcattach a;
2952145256Sjkoshy
2953145256Sjkoshy		sx_assert(&pmc_sx, SX_XLOCKED);
2954145256Sjkoshy
2955145256Sjkoshy		if ((error = copyin(arg, &a, sizeof(a))) != 0)
2956145256Sjkoshy			break;
2957145256Sjkoshy
2958145256Sjkoshy		if (a.pm_pid < 0) {
2959145256Sjkoshy			error = EINVAL;
2960145256Sjkoshy			break;
2961145256Sjkoshy		} else if (a.pm_pid == 0)
2962145256Sjkoshy			a.pm_pid = td->td_proc->p_pid;
2963145256Sjkoshy
2964145256Sjkoshy		if ((error = pmc_find_pmc(a.pm_pmc, &pm)) != 0)
2965145256Sjkoshy			break;
2966145256Sjkoshy
2967145774Sjkoshy		if (PMC_IS_SYSTEM_MODE(PMC_TO_MODE(pm))) {
2968145256Sjkoshy			error = EINVAL;
2969145256Sjkoshy			break;
2970145256Sjkoshy		}
2971145256Sjkoshy
2972145256Sjkoshy		/* PMCs may be (re)attached only when allocated or stopped */
2973145256Sjkoshy		if (pm->pm_state == PMC_STATE_RUNNING) {
2974145256Sjkoshy			error = EBUSY;
2975145256Sjkoshy			break;
2976145256Sjkoshy		} else if (pm->pm_state != PMC_STATE_ALLOCATED &&
2977145256Sjkoshy		    pm->pm_state != PMC_STATE_STOPPED) {
2978145256Sjkoshy			error = EINVAL;
2979145256Sjkoshy			break;
2980145256Sjkoshy		}
2981145256Sjkoshy
2982145256Sjkoshy		/* lookup pid */
2983145256Sjkoshy		if ((p = pfind(a.pm_pid)) == NULL) {
2984145256Sjkoshy			error = ESRCH;
2985145256Sjkoshy			break;
2986145256Sjkoshy		}
2987145256Sjkoshy
2988145256Sjkoshy		/*
2989145256Sjkoshy		 * Ignore processes that are working on exiting.
2990145256Sjkoshy		 */
2991145256Sjkoshy		if (p->p_flag & P_WEXIT) {
2992145256Sjkoshy			error = ESRCH;
2993145256Sjkoshy			PROC_UNLOCK(p);	/* pfind() returns a locked process */
2994145256Sjkoshy			break;
2995145256Sjkoshy		}
2996145256Sjkoshy
2997145256Sjkoshy		/*
2998145256Sjkoshy		 * we are allowed to attach a PMC to a process if
2999145256Sjkoshy		 * we can debug it.
3000145256Sjkoshy		 */
3001145256Sjkoshy		error = p_candebug(curthread, p);
3002145256Sjkoshy
3003145256Sjkoshy		PROC_UNLOCK(p);
3004145256Sjkoshy
3005145256Sjkoshy		if (error == 0)
3006145256Sjkoshy			error = pmc_attach_process(p, pm);
3007145256Sjkoshy	}
3008145256Sjkoshy	break;
3009145256Sjkoshy
3010145256Sjkoshy
3011145256Sjkoshy	/*
3012145256Sjkoshy	 * Detach an attached PMC from a process.
3013145256Sjkoshy	 */
3014145256Sjkoshy
3015145256Sjkoshy	case PMC_OP_PMCDETACH:
3016145256Sjkoshy	{
3017145256Sjkoshy		struct pmc *pm;
3018145256Sjkoshy		struct proc *p;
3019145256Sjkoshy		struct pmc_op_pmcattach a;
3020145256Sjkoshy
3021145256Sjkoshy		if ((error = copyin(arg, &a, sizeof(a))) != 0)
3022145256Sjkoshy			break;
3023145256Sjkoshy
3024145256Sjkoshy		if (a.pm_pid < 0) {
3025145256Sjkoshy			error = EINVAL;
3026145256Sjkoshy			break;
3027145256Sjkoshy		} else if (a.pm_pid == 0)
3028145256Sjkoshy			a.pm_pid = td->td_proc->p_pid;
3029145256Sjkoshy
3030145256Sjkoshy		if ((error = pmc_find_pmc(a.pm_pmc, &pm)) != 0)
3031145256Sjkoshy			break;
3032145256Sjkoshy
3033145256Sjkoshy		if ((p = pfind(a.pm_pid)) == NULL) {
3034145256Sjkoshy			error = ESRCH;
3035145256Sjkoshy			break;
3036145256Sjkoshy		}
3037145256Sjkoshy
3038145256Sjkoshy		/*
3039145256Sjkoshy		 * Treat processes that are in the process of exiting
3040145256Sjkoshy		 * as if they were not present.
3041145256Sjkoshy		 */
3042145256Sjkoshy
3043145256Sjkoshy		if (p->p_flag & P_WEXIT)
3044145256Sjkoshy			error = ESRCH;
3045145256Sjkoshy
3046145256Sjkoshy		PROC_UNLOCK(p);	/* pfind() returns a locked process */
3047145256Sjkoshy
3048145256Sjkoshy		if (error == 0)
3049145256Sjkoshy			error = pmc_detach_process(p, pm);
3050145256Sjkoshy	}
3051145256Sjkoshy	break;
3052145256Sjkoshy
3053145256Sjkoshy
3054145256Sjkoshy	/*
3055147191Sjkoshy	 * Retrieve the MSR number associated with the counter
3056147191Sjkoshy	 * 'pmc_id'.  This allows processes to directly use RDPMC
3057147191Sjkoshy	 * instructions to read their PMCs, without the overhead of a
3058147191Sjkoshy	 * system call.
3059147191Sjkoshy	 */
3060147191Sjkoshy
3061147191Sjkoshy	case PMC_OP_PMCGETMSR:
3062147191Sjkoshy	{
3063147191Sjkoshy		int ri;
3064147191Sjkoshy		struct pmc	*pm;
3065147191Sjkoshy		struct pmc_target *pt;
3066147191Sjkoshy		struct pmc_op_getmsr gm;
3067147191Sjkoshy
3068147191Sjkoshy		PMC_DOWNGRADE_SX();
3069147191Sjkoshy
3070147191Sjkoshy		/* CPU has no 'GETMSR' support */
3071147191Sjkoshy		if (md->pmd_get_msr == NULL) {
3072147191Sjkoshy			error = ENOSYS;
3073147191Sjkoshy			break;
3074147191Sjkoshy		}
3075147191Sjkoshy
3076147191Sjkoshy		if ((error = copyin(arg, &gm, sizeof(gm))) != 0)
3077147191Sjkoshy			break;
3078147191Sjkoshy
3079147191Sjkoshy		if ((error = pmc_find_pmc(gm.pm_pmcid, &pm)) != 0)
3080147191Sjkoshy			break;
3081147191Sjkoshy
3082147191Sjkoshy		/*
3083147191Sjkoshy		 * The allocated PMC has to be a process virtual PMC,
3084147191Sjkoshy		 * i.e., of type MODE_T[CS].  Global PMCs can only be
3085147191Sjkoshy		 * read using the PMCREAD operation since they may be
3086147191Sjkoshy		 * allocated on a different CPU than the one we could
3087147191Sjkoshy		 * be running on at the time of the RDPMC instruction.
3088147191Sjkoshy		 *
3089147191Sjkoshy		 * The GETMSR operation is not allowed for PMCs that
3090147191Sjkoshy		 * are inherited across processes.
3091147191Sjkoshy		 */
3092147191Sjkoshy
3093147191Sjkoshy		if (!PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm)) ||
3094147191Sjkoshy		    (pm->pm_flags & PMC_F_DESCENDANTS)) {
3095147191Sjkoshy			error = EINVAL;
3096147191Sjkoshy			break;
3097147191Sjkoshy		}
3098147191Sjkoshy
3099147191Sjkoshy		/*
3100147191Sjkoshy		 * It only makes sense to use a RDPMC (or its
3101147191Sjkoshy		 * equivalent instruction on non-x86 architectures) on
3102147191Sjkoshy		 * a process that has allocated and attached a PMC to
3103147191Sjkoshy		 * itself.  Conversely the PMC is only allowed to have
3104147191Sjkoshy		 * one process attached to it -- its owner.
3105147191Sjkoshy		 */
3106147191Sjkoshy
3107147191Sjkoshy		if ((pt = LIST_FIRST(&pm->pm_targets)) == NULL ||
3108147191Sjkoshy		    LIST_NEXT(pt, pt_next) != NULL ||
3109147191Sjkoshy		    pt->pt_process->pp_proc != pm->pm_owner->po_owner) {
3110147191Sjkoshy			error = EINVAL;
3111147191Sjkoshy			break;
3112147191Sjkoshy		}
3113147191Sjkoshy
3114147191Sjkoshy		ri = PMC_TO_ROWINDEX(pm);
3115147191Sjkoshy
3116147191Sjkoshy		if ((error = (*md->pmd_get_msr)(ri, &gm.pm_msr)) < 0)
3117147191Sjkoshy			break;
3118147191Sjkoshy
3119147191Sjkoshy		if ((error = copyout(&gm, arg, sizeof(gm))) < 0)
3120147191Sjkoshy			break;
3121147191Sjkoshy
3122147191Sjkoshy		/*
3123147191Sjkoshy		 * Mark our process as using MSRs.  Update machine
3124147191Sjkoshy		 * state using a forced context switch.
3125147191Sjkoshy		 */
3126147191Sjkoshy
3127147191Sjkoshy		pt->pt_process->pp_flags |= PMC_PP_ENABLE_MSR_ACCESS;
3128147191Sjkoshy		pmc_force_context_switch();
3129147191Sjkoshy
3130147191Sjkoshy	}
3131147191Sjkoshy	break;
3132147191Sjkoshy
3133147191Sjkoshy	/*
3134145256Sjkoshy	 * Release an allocated PMC
3135145256Sjkoshy	 */
3136145256Sjkoshy
3137145256Sjkoshy	case PMC_OP_PMCRELEASE:
3138145256Sjkoshy	{
3139145256Sjkoshy		pmc_id_t pmcid;
3140145256Sjkoshy		struct pmc *pm;
3141145256Sjkoshy		struct pmc_owner *po;
3142145256Sjkoshy		struct pmc_op_simple sp;
3143145256Sjkoshy
3144145256Sjkoshy		/*
3145145256Sjkoshy		 * Find PMC pointer for the named PMC.
3146145256Sjkoshy		 *
3147145256Sjkoshy		 * Use pmc_release_pmc_descriptor() to switch off the
3148145256Sjkoshy		 * PMC, remove all its target threads, and remove the
3149145256Sjkoshy		 * PMC from its owner's list.
3150145256Sjkoshy		 *
3151145256Sjkoshy		 * Remove the owner record if this is the last PMC
3152145256Sjkoshy		 * owned.
3153145256Sjkoshy		 *
3154145256Sjkoshy		 * Free up space.
3155145256Sjkoshy		 */
3156145256Sjkoshy
3157145256Sjkoshy		if ((error = copyin(arg, &sp, sizeof(sp))) != 0)
3158145256Sjkoshy			break;
3159145256Sjkoshy
3160145256Sjkoshy		pmcid = sp.pm_pmcid;
3161145256Sjkoshy
3162145256Sjkoshy		if ((error = pmc_find_pmc(pmcid, &pm)) != 0)
3163145256Sjkoshy			break;
3164145256Sjkoshy
3165145256Sjkoshy		po = pm->pm_owner;
3166145256Sjkoshy		pmc_release_pmc_descriptor(pm);
3167145256Sjkoshy		pmc_maybe_remove_owner(po);
3168145256Sjkoshy
3169145256Sjkoshy		FREE(pm, M_PMC);
3170145256Sjkoshy	}
3171145256Sjkoshy	break;
3172145256Sjkoshy
3173145256Sjkoshy
3174145256Sjkoshy	/*
3175145256Sjkoshy	 * Read and/or write a PMC.
3176145256Sjkoshy	 */
3177145256Sjkoshy
3178145256Sjkoshy	case PMC_OP_PMCRW:
3179145256Sjkoshy	{
3180145256Sjkoshy		uint32_t cpu, ri;
3181145256Sjkoshy		struct pmc *pm;
3182145256Sjkoshy		struct pmc_op_pmcrw *pprw;
3183145256Sjkoshy		struct pmc_op_pmcrw prw;
3184145256Sjkoshy		struct pmc_binding pb;
3185145256Sjkoshy		pmc_value_t oldvalue;
3186145256Sjkoshy
3187145256Sjkoshy		PMC_DOWNGRADE_SX();
3188145256Sjkoshy
3189145256Sjkoshy		if ((error = copyin(arg, &prw, sizeof(prw))) != 0)
3190145256Sjkoshy			break;
3191145256Sjkoshy
3192145301Simp		ri = 0;
3193145256Sjkoshy		PMCDBG(PMC,OPS,1, "rw id=%d flags=0x%x", prw.pm_pmcid,
3194145256Sjkoshy		    prw.pm_flags);
3195145256Sjkoshy
3196145256Sjkoshy		/* must have at least one flag set */
3197145256Sjkoshy		if ((prw.pm_flags & (PMC_F_OLDVALUE|PMC_F_NEWVALUE)) == 0) {
3198145256Sjkoshy			error = EINVAL;
3199145256Sjkoshy			break;
3200145256Sjkoshy		}
3201145256Sjkoshy
3202145256Sjkoshy		/* locate pmc descriptor */
3203145256Sjkoshy		if ((error = pmc_find_pmc(prw.pm_pmcid, &pm)) != 0)
3204145256Sjkoshy			break;
3205145256Sjkoshy
3206145256Sjkoshy		/* Can't read a PMC that hasn't been started. */
3207145256Sjkoshy		if (pm->pm_state != PMC_STATE_ALLOCATED &&
3208145256Sjkoshy		    pm->pm_state != PMC_STATE_STOPPED &&
3209145256Sjkoshy		    pm->pm_state != PMC_STATE_RUNNING) {
3210145256Sjkoshy			error = EINVAL;
3211145256Sjkoshy			break;
3212145256Sjkoshy		}
3213145256Sjkoshy
3214145256Sjkoshy		/* writing a new value is allowed only for 'STOPPED' pmcs */
3215145256Sjkoshy		if (pm->pm_state == PMC_STATE_RUNNING &&
3216145256Sjkoshy		    (prw.pm_flags & PMC_F_NEWVALUE)) {
3217145256Sjkoshy			error = EBUSY;
3218145256Sjkoshy			break;
3219145256Sjkoshy		}
3220145256Sjkoshy
3221145774Sjkoshy		if (PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm))) {
3222145256Sjkoshy
3223145774Sjkoshy			/*
3224145774Sjkoshy			 * If this PMC is attached to its owner (i.e.,
3225145774Sjkoshy			 * the process requesting this operation) and
3226145774Sjkoshy			 * is running, then attempt to get an
3227145774Sjkoshy			 * upto-date reading from hardware for a READ.
3228145774Sjkoshy			 * Writes are only allowed when the PMC is
3229145774Sjkoshy			 * stopped, so only update the saved value
3230145774Sjkoshy			 * field.
3231145774Sjkoshy			 *
3232145774Sjkoshy			 * If the PMC is not running, or is not
3233145774Sjkoshy			 * attached to its owner, read/write to the
3234145774Sjkoshy			 * savedvalue field.
3235145774Sjkoshy			 */
3236145774Sjkoshy
3237145774Sjkoshy			ri = PMC_TO_ROWINDEX(pm);
3238145774Sjkoshy
3239145256Sjkoshy			mtx_pool_lock_spin(pmc_mtxpool, pm);
3240145774Sjkoshy			cpu = curthread->td_oncpu;
3241145774Sjkoshy
3242145774Sjkoshy			if (prw.pm_flags & PMC_F_OLDVALUE) {
3243145774Sjkoshy				if ((pm->pm_flags & PMC_F_ATTACHED_TO_OWNER) &&
3244145774Sjkoshy				    (pm->pm_state == PMC_STATE_RUNNING))
3245145774Sjkoshy					error = (*md->pmd_read_pmc)(cpu, ri,
3246145774Sjkoshy					    &oldvalue);
3247145774Sjkoshy				else
3248145774Sjkoshy					oldvalue = pm->pm_gv.pm_savedvalue;
3249145774Sjkoshy			}
3250145256Sjkoshy			if (prw.pm_flags & PMC_F_NEWVALUE)
3251145256Sjkoshy				pm->pm_gv.pm_savedvalue = prw.pm_value;
3252145774Sjkoshy
3253145256Sjkoshy			mtx_pool_unlock_spin(pmc_mtxpool, pm);
3254145256Sjkoshy
3255145256Sjkoshy		} else { /* System mode PMCs */
3256145774Sjkoshy			cpu = PMC_TO_CPU(pm);
3257145774Sjkoshy			ri  = PMC_TO_ROWINDEX(pm);
3258145256Sjkoshy
3259145256Sjkoshy			if (pmc_cpu_is_disabled(cpu)) {
3260145256Sjkoshy				error = ENXIO;
3261145256Sjkoshy				break;
3262145256Sjkoshy			}
3263145256Sjkoshy
3264145256Sjkoshy			/* move this thread to CPU 'cpu' */
3265145256Sjkoshy			pmc_save_cpu_binding(&pb);
3266145256Sjkoshy			pmc_select_cpu(cpu);
3267145256Sjkoshy
3268145774Sjkoshy			critical_enter();
3269145256Sjkoshy			/* save old value */
3270145256Sjkoshy			if (prw.pm_flags & PMC_F_OLDVALUE)
3271145256Sjkoshy				if ((error = (*md->pmd_read_pmc)(cpu, ri,
3272145256Sjkoshy					 &oldvalue)))
3273145256Sjkoshy					goto error;
3274145256Sjkoshy			/* write out new value */
3275145256Sjkoshy			if (prw.pm_flags & PMC_F_NEWVALUE)
3276145256Sjkoshy				error = (*md->pmd_write_pmc)(cpu, ri,
3277145256Sjkoshy				    prw.pm_value);
3278145256Sjkoshy		error:
3279145774Sjkoshy			critical_exit();
3280145256Sjkoshy			pmc_restore_cpu_binding(&pb);
3281145256Sjkoshy			if (error)
3282145256Sjkoshy				break;
3283145256Sjkoshy		}
3284145256Sjkoshy
3285145256Sjkoshy		pprw = (struct pmc_op_pmcrw *) arg;
3286145256Sjkoshy
3287153110Sru#ifdef	DEBUG
3288145256Sjkoshy		if (prw.pm_flags & PMC_F_NEWVALUE)
3289145256Sjkoshy			PMCDBG(PMC,OPS,2, "rw id=%d new %jx -> old %jx",
3290145256Sjkoshy			    ri, prw.pm_value, oldvalue);
3291145256Sjkoshy		else
3292145256Sjkoshy			PMCDBG(PMC,OPS,2, "rw id=%d -> old %jx", ri, oldvalue);
3293145256Sjkoshy#endif
3294145256Sjkoshy
3295145256Sjkoshy		/* return old value if requested */
3296145256Sjkoshy		if (prw.pm_flags & PMC_F_OLDVALUE)
3297145256Sjkoshy			if ((error = copyout(&oldvalue, &pprw->pm_value,
3298145256Sjkoshy				 sizeof(prw.pm_value))))
3299145256Sjkoshy				break;
3300145256Sjkoshy
3301145256Sjkoshy	}
3302145256Sjkoshy	break;
3303145256Sjkoshy
3304145256Sjkoshy
3305145256Sjkoshy	/*
3306145256Sjkoshy	 * Set the sampling rate for a sampling mode PMC and the
3307145256Sjkoshy	 * initial count for a counting mode PMC.
3308145256Sjkoshy	 */
3309145256Sjkoshy
3310145256Sjkoshy	case PMC_OP_PMCSETCOUNT:
3311145256Sjkoshy	{
3312145256Sjkoshy		struct pmc *pm;
3313145256Sjkoshy		struct pmc_op_pmcsetcount sc;
3314145256Sjkoshy
3315145256Sjkoshy		PMC_DOWNGRADE_SX();
3316145256Sjkoshy
3317145256Sjkoshy		if ((error = copyin(arg, &sc, sizeof(sc))) != 0)
3318145256Sjkoshy			break;
3319145256Sjkoshy
3320145256Sjkoshy		if ((error = pmc_find_pmc(sc.pm_pmcid, &pm)) != 0)
3321145256Sjkoshy			break;
3322145256Sjkoshy
3323145256Sjkoshy		if (pm->pm_state == PMC_STATE_RUNNING) {
3324145256Sjkoshy			error = EBUSY;
3325145256Sjkoshy			break;
3326145256Sjkoshy		}
3327145256Sjkoshy
3328145774Sjkoshy		if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
3329145256Sjkoshy			pm->pm_sc.pm_reloadcount = sc.pm_count;
3330145256Sjkoshy		else
3331145256Sjkoshy			pm->pm_sc.pm_initial = sc.pm_count;
3332145256Sjkoshy	}
3333145256Sjkoshy	break;
3334145256Sjkoshy
3335145256Sjkoshy
3336145256Sjkoshy	/*
3337145256Sjkoshy	 * Start a PMC.
3338145256Sjkoshy	 */
3339145256Sjkoshy
3340145256Sjkoshy	case PMC_OP_PMCSTART:
3341145256Sjkoshy	{
3342145256Sjkoshy		pmc_id_t pmcid;
3343145256Sjkoshy		struct pmc *pm;
3344145256Sjkoshy		struct pmc_op_simple sp;
3345145256Sjkoshy
3346145256Sjkoshy		sx_assert(&pmc_sx, SX_XLOCKED);
3347145256Sjkoshy
3348145256Sjkoshy		if ((error = copyin(arg, &sp, sizeof(sp))) != 0)
3349145256Sjkoshy			break;
3350145256Sjkoshy
3351145256Sjkoshy		pmcid = sp.pm_pmcid;
3352145256Sjkoshy
3353145256Sjkoshy		if ((error = pmc_find_pmc(pmcid, &pm)) != 0)
3354145256Sjkoshy			break;
3355145256Sjkoshy
3356145774Sjkoshy		KASSERT(pmcid == pm->pm_id,
3357145774Sjkoshy		    ("[pmc,%d] pmcid %x != id %x", __LINE__,
3358145774Sjkoshy			pm->pm_id, pmcid));
3359145256Sjkoshy
3360145256Sjkoshy		if (pm->pm_state == PMC_STATE_RUNNING) /* already running */
3361145256Sjkoshy			break;
3362145256Sjkoshy		else if (pm->pm_state != PMC_STATE_STOPPED &&
3363145256Sjkoshy		    pm->pm_state != PMC_STATE_ALLOCATED) {
3364145256Sjkoshy			error = EINVAL;
3365145256Sjkoshy			break;
3366145256Sjkoshy		}
3367145256Sjkoshy
3368145256Sjkoshy		error = pmc_start(pm);
3369145256Sjkoshy	}
3370145256Sjkoshy	break;
3371145256Sjkoshy
3372145256Sjkoshy
3373145256Sjkoshy	/*
3374145256Sjkoshy	 * Stop a PMC.
3375145256Sjkoshy	 */
3376145256Sjkoshy
3377145256Sjkoshy	case PMC_OP_PMCSTOP:
3378145256Sjkoshy	{
3379145256Sjkoshy		pmc_id_t pmcid;
3380145256Sjkoshy		struct pmc *pm;
3381145256Sjkoshy		struct pmc_op_simple sp;
3382145256Sjkoshy
3383145256Sjkoshy		PMC_DOWNGRADE_SX();
3384145256Sjkoshy
3385145256Sjkoshy		if ((error = copyin(arg, &sp, sizeof(sp))) != 0)
3386145256Sjkoshy			break;
3387145256Sjkoshy
3388145256Sjkoshy		pmcid = sp.pm_pmcid;
3389145256Sjkoshy
3390145256Sjkoshy		/*
3391145256Sjkoshy		 * Mark the PMC as inactive and invoke the MD stop
3392145256Sjkoshy		 * routines if needed.
3393145256Sjkoshy		 */
3394145256Sjkoshy
3395145256Sjkoshy		if ((error = pmc_find_pmc(pmcid, &pm)) != 0)
3396145256Sjkoshy			break;
3397145256Sjkoshy
3398145774Sjkoshy		KASSERT(pmcid == pm->pm_id,
3399145774Sjkoshy		    ("[pmc,%d] pmc id %x != pmcid %x", __LINE__,
3400145774Sjkoshy			pm->pm_id, pmcid));
3401145256Sjkoshy
3402145256Sjkoshy		if (pm->pm_state == PMC_STATE_STOPPED) /* already stopped */
3403145256Sjkoshy			break;
3404145256Sjkoshy		else if (pm->pm_state != PMC_STATE_RUNNING) {
3405145256Sjkoshy			error = EINVAL;
3406145256Sjkoshy			break;
3407145256Sjkoshy		}
3408145256Sjkoshy
3409145256Sjkoshy		error = pmc_stop(pm);
3410145256Sjkoshy	}
3411145256Sjkoshy	break;
3412145256Sjkoshy
3413145256Sjkoshy
3414145256Sjkoshy	/*
3415147867Sjkoshy	 * Write a user supplied value to the log file.
3416145256Sjkoshy	 */
3417145256Sjkoshy
3418145256Sjkoshy	case PMC_OP_WRITELOG:
3419145256Sjkoshy	{
3420147191Sjkoshy		struct pmc_op_writelog wl;
3421147191Sjkoshy		struct pmc_owner *po;
3422145256Sjkoshy
3423145256Sjkoshy		PMC_DOWNGRADE_SX();
3424145256Sjkoshy
3425147191Sjkoshy		if ((error = copyin(arg, &wl, sizeof(wl))) != 0)
3426145256Sjkoshy			break;
3427145256Sjkoshy
3428147191Sjkoshy		if ((po = pmc_find_owner_descriptor(td->td_proc)) == NULL) {
3429145256Sjkoshy			error = EINVAL;
3430145256Sjkoshy			break;
3431145256Sjkoshy		}
3432145256Sjkoshy
3433147191Sjkoshy		if ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0) {
3434145774Sjkoshy			error = EINVAL;
3435145774Sjkoshy			break;
3436145774Sjkoshy		}
3437145774Sjkoshy
3438147191Sjkoshy		error = pmclog_process_userlog(po, &wl);
3439145256Sjkoshy	}
3440145256Sjkoshy	break;
3441145256Sjkoshy
3442147191Sjkoshy
3443145256Sjkoshy	default:
3444145256Sjkoshy		error = EINVAL;
3445145256Sjkoshy		break;
3446145256Sjkoshy	}
3447145256Sjkoshy
3448145256Sjkoshy	if (is_sx_downgraded)
3449145256Sjkoshy		sx_sunlock(&pmc_sx);
3450145256Sjkoshy	else
3451145256Sjkoshy		sx_xunlock(&pmc_sx);
3452145256Sjkoshy
3453145256Sjkoshy	if (error)
3454145256Sjkoshy		atomic_add_int(&pmc_stats.pm_syscall_errors, 1);
3455145256Sjkoshy
3456147191Sjkoshy	PICKUP_GIANT();
3457147191Sjkoshy
3458145256Sjkoshy	return error;
3459145256Sjkoshy}
3460145256Sjkoshy
3461145256Sjkoshy/*
3462145256Sjkoshy * Helper functions
3463145256Sjkoshy */
3464145256Sjkoshy
3465147191Sjkoshy
3466145256Sjkoshy/*
3467147191Sjkoshy * Interrupt processing.
3468147191Sjkoshy *
3469147191Sjkoshy * Find a free slot in the per-cpu array of PC samples and write the
3470147191Sjkoshy * current (PMC,PID,PC) triple to it.  If an event was successfully
3471147191Sjkoshy * added, a bit is set in mask 'pmc_cpumask' denoting that the
3472147191Sjkoshy * DO_SAMPLES hook needs to be invoked from the clock handler.
3473147191Sjkoshy *
3474147191Sjkoshy * This function is meant to be called from an NMI handler.  It cannot
3475147191Sjkoshy * use any of the locking primitives supplied by the OS.
3476145256Sjkoshy */
3477145256Sjkoshy
3478147191Sjkoshyint
3479147708Sjkoshypmc_process_interrupt(int cpu, struct pmc *pm, uintfptr_t pc, int usermode)
3480145256Sjkoshy{
3481147191Sjkoshy	int error, ri;
3482147191Sjkoshy	struct thread *td;
3483147191Sjkoshy	struct pmc_sample *ps;
3484147191Sjkoshy	struct pmc_samplebuffer *psb;
3485145256Sjkoshy
3486147191Sjkoshy	error = 0;
3487147191Sjkoshy	ri = PMC_TO_ROWINDEX(pm);
3488145256Sjkoshy
3489147191Sjkoshy	psb = pmc_pcpu[cpu]->pc_sb;
3490145256Sjkoshy
3491147191Sjkoshy	ps = psb->ps_write;
3492147191Sjkoshy	if (ps->ps_pc) {	/* in use, reader hasn't caught up */
3493147867Sjkoshy		pm->pm_stalled = 1;
3494147191Sjkoshy		atomic_add_int(&pmc_stats.pm_intr_bufferfull, 1);
3495147191Sjkoshy		PMCDBG(SAM,INT,1,"(spc) cpu=%d pm=%p pc=%jx um=%d wr=%d rd=%d",
3496147708Sjkoshy		    cpu, pm, (uint64_t) pc, usermode,
3497147191Sjkoshy		    (int) (psb->ps_write - psb->ps_samples),
3498147191Sjkoshy		    (int) (psb->ps_read - psb->ps_samples));
3499147191Sjkoshy		error = ENOMEM;
3500147191Sjkoshy		goto done;
3501147191Sjkoshy	}
3502145256Sjkoshy
3503147191Sjkoshy	/* fill in entry */
3504147191Sjkoshy	PMCDBG(SAM,INT,1,"cpu=%d pm=%p pc=%jx um=%d wr=%d rd=%d", cpu, pm,
3505147708Sjkoshy	    (uint64_t) pc, usermode,
3506147191Sjkoshy	    (int) (psb->ps_write - psb->ps_samples),
3507147191Sjkoshy	    (int) (psb->ps_read - psb->ps_samples));
3508145256Sjkoshy
3509147191Sjkoshy	atomic_add_rel_32(&pm->pm_runcount, 1);		/* hold onto PMC */
3510147191Sjkoshy	ps->ps_pmc = pm;
3511147191Sjkoshy	if ((td = curthread) && td->td_proc)
3512147191Sjkoshy		ps->ps_pid = td->td_proc->p_pid;
3513147191Sjkoshy	else
3514147191Sjkoshy		ps->ps_pid = -1;
3515147191Sjkoshy	ps->ps_usermode = usermode;
3516147191Sjkoshy	ps->ps_pc = pc;		/* mark entry as in use */
3517145256Sjkoshy
3518147191Sjkoshy	/* increment write pointer, modulo ring buffer size */
3519147191Sjkoshy	ps++;
3520147191Sjkoshy	if (ps == psb->ps_fence)
3521147191Sjkoshy		psb->ps_write = psb->ps_samples;
3522147191Sjkoshy	else
3523147191Sjkoshy		psb->ps_write = ps;
3524145256Sjkoshy
3525147191Sjkoshy done:
3526147191Sjkoshy	/* mark CPU as needing processing */
3527147191Sjkoshy	atomic_set_rel_int(&pmc_cpumask, (1 << cpu));
3528147191Sjkoshy
3529147191Sjkoshy	return error;
3530145256Sjkoshy}
3531145256Sjkoshy
3532147191Sjkoshy
3533145256Sjkoshy/*
3534147191Sjkoshy * Process saved PC samples.
3535145256Sjkoshy */
3536145256Sjkoshy
3537145256Sjkoshystatic void
3538147191Sjkoshypmc_process_samples(int cpu)
3539145256Sjkoshy{
3540147191Sjkoshy	int n, ri;
3541147191Sjkoshy	struct pmc *pm;
3542147191Sjkoshy	struct thread *td;
3543147191Sjkoshy	struct pmc_owner *po;
3544147191Sjkoshy	struct pmc_sample *ps;
3545147191Sjkoshy	struct pmc_samplebuffer *psb;
3546145256Sjkoshy
3547147191Sjkoshy	KASSERT(PCPU_GET(cpuid) == cpu,
3548147191Sjkoshy	    ("[pmc,%d] not on the correct CPU pcpu=%d cpu=%d", __LINE__,
3549147191Sjkoshy		PCPU_GET(cpuid), cpu));
3550145256Sjkoshy
3551147191Sjkoshy	psb = pmc_pcpu[cpu]->pc_sb;
3552147191Sjkoshy
3553147191Sjkoshy	for (n = 0; n < pmc_nsamples; n++) { /* bound on #iterations */
3554147191Sjkoshy
3555147191Sjkoshy		ps = psb->ps_read;
3556147191Sjkoshy		if (ps->ps_pc == (uintfptr_t) 0)	/* no data */
3557147191Sjkoshy			break;
3558147191Sjkoshy
3559147191Sjkoshy		pm = ps->ps_pmc;
3560147191Sjkoshy		po = pm->pm_owner;
3561147191Sjkoshy
3562147191Sjkoshy		KASSERT(PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)),
3563147191Sjkoshy		    ("[pmc,%d] pmc=%p non-sampling mode=%d", __LINE__,
3564147191Sjkoshy			pm, PMC_TO_MODE(pm)));
3565147191Sjkoshy
3566147191Sjkoshy		/* Ignore PMCs that have been switched off */
3567147191Sjkoshy		if (pm->pm_state != PMC_STATE_RUNNING)
3568147191Sjkoshy			goto entrydone;
3569147191Sjkoshy
3570147191Sjkoshy		PMCDBG(SAM,OPS,1,"cpu=%d pm=%p pc=%jx um=%d wr=%d rd=%d", cpu,
3571147708Sjkoshy		    pm, (uint64_t) ps->ps_pc, ps->ps_usermode,
3572147191Sjkoshy		    (int) (psb->ps_write - psb->ps_samples),
3573147191Sjkoshy		    (int) (psb->ps_read - psb->ps_samples));
3574147191Sjkoshy
3575147191Sjkoshy		/*
3576147191Sjkoshy		 * If this is a process-mode PMC that is attached to
3577147191Sjkoshy		 * its owner, and if the PC is in user mode, update
3578147191Sjkoshy		 * profiling statistics like timer-based profiling
3579147191Sjkoshy		 * would have done.
3580147191Sjkoshy		 */
3581147191Sjkoshy		if (pm->pm_flags & PMC_F_ATTACHED_TO_OWNER) {
3582147191Sjkoshy			if (ps->ps_usermode) {
3583147191Sjkoshy				td = FIRST_THREAD_IN_PROC(po->po_owner);
3584147191Sjkoshy				addupc_intr(td, ps->ps_pc, 1);
3585147191Sjkoshy			}
3586147191Sjkoshy			goto entrydone;
3587147191Sjkoshy		}
3588147191Sjkoshy
3589147191Sjkoshy		/*
3590147191Sjkoshy		 * Otherwise, this is either a sampling mode PMC that
3591147191Sjkoshy		 * is attached to a different process than its owner,
3592147191Sjkoshy		 * or a system-wide sampling PMC.  Dispatch a log
3593147191Sjkoshy		 * entry to the PMC's owner process.
3594147191Sjkoshy		 */
3595147191Sjkoshy
3596147191Sjkoshy		pmclog_process_pcsample(pm, ps);
3597147191Sjkoshy
3598147191Sjkoshy	entrydone:
3599147191Sjkoshy		ps->ps_pc = (uintfptr_t) 0;	/* mark entry as free */
3600147191Sjkoshy		atomic_subtract_rel_32(&pm->pm_runcount, 1);
3601147191Sjkoshy
3602147191Sjkoshy		/* increment read pointer, modulo sample size */
3603147191Sjkoshy		if (++ps == psb->ps_fence)
3604147191Sjkoshy			psb->ps_read = psb->ps_samples;
3605147191Sjkoshy		else
3606147191Sjkoshy			psb->ps_read = ps;
3607147191Sjkoshy	}
3608147191Sjkoshy
3609147191Sjkoshy	atomic_add_int(&pmc_stats.pm_log_sweeps, 1);
3610147191Sjkoshy
3611147191Sjkoshy	/* Do not re-enable stalled PMCs if we failed to process any samples */
3612147191Sjkoshy	if (n == 0)
3613147191Sjkoshy		return;
3614147191Sjkoshy
3615147191Sjkoshy	/*
3616147191Sjkoshy	 * Restart any stalled sampling PMCs on this CPU.
3617147191Sjkoshy	 *
3618147867Sjkoshy	 * If the NMI handler sets the pm_stalled field of a PMC after
3619147867Sjkoshy	 * the check below, we'll end up processing the stalled PMC at
3620147867Sjkoshy	 * the next hardclock tick.
3621147191Sjkoshy	 */
3622147191Sjkoshy	for (n = 0; n < md->pmd_npmc; n++) {
3623147191Sjkoshy		(void) (*md->pmd_get_config)(cpu,n,&pm);
3624147191Sjkoshy		if (pm == NULL ||			 /* !cfg'ed */
3625147191Sjkoshy		    pm->pm_state != PMC_STATE_RUNNING || /* !active */
3626147191Sjkoshy		    !PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)) || /* !sampling */
3627147867Sjkoshy		    pm->pm_stalled == 0) /* !stalled */
3628147191Sjkoshy			continue;
3629147191Sjkoshy
3630147867Sjkoshy		pm->pm_stalled = 0;
3631147191Sjkoshy		ri = PMC_TO_ROWINDEX(pm);
3632147191Sjkoshy		(*md->pmd_start_pmc)(cpu, ri);
3633147191Sjkoshy	}
3634145256Sjkoshy}
3635145256Sjkoshy
3636145256Sjkoshy/*
3637145256Sjkoshy * Event handlers.
3638145256Sjkoshy */
3639145256Sjkoshy
3640145256Sjkoshy/*
3641145256Sjkoshy * Handle a process exit.
3642145256Sjkoshy *
3643147191Sjkoshy * Remove this process from all hash tables.  If this process
3644147191Sjkoshy * owned any PMCs, turn off those PMCs and deallocate them,
3645147191Sjkoshy * removing any associations with target processes.
3646147191Sjkoshy *
3647147191Sjkoshy * This function will be called by the last 'thread' of a
3648147191Sjkoshy * process.
3649147191Sjkoshy *
3650145256Sjkoshy * XXX This eventhandler gets called early in the exit process.
3651145256Sjkoshy * Consider using a 'hook' invocation from thread_exit() or equivalent
3652145256Sjkoshy * spot.  Another negative is that kse_exit doesn't seem to call
3653145256Sjkoshy * exit1() [??].
3654147191Sjkoshy *
3655145256Sjkoshy */
3656145256Sjkoshy
3657145256Sjkoshystatic void
3658145256Sjkoshypmc_process_exit(void *arg __unused, struct proc *p)
3659145256Sjkoshy{
3660145256Sjkoshy	int is_using_hwpmcs;
3661147191Sjkoshy	int cpu;
3662147191Sjkoshy	unsigned int ri;
3663147191Sjkoshy	struct pmc *pm;
3664147191Sjkoshy	struct pmc_process *pp;
3665147191Sjkoshy	struct pmc_owner *po;
3666147191Sjkoshy	pmc_value_t newvalue, tmp;
3667145256Sjkoshy
3668145256Sjkoshy	PROC_LOCK(p);
3669145256Sjkoshy	is_using_hwpmcs = p->p_flag & P_HWPMC;
3670145256Sjkoshy	PROC_UNLOCK(p);
3671145256Sjkoshy
3672147191Sjkoshy	/*
3673147191Sjkoshy	 * Log a sysexit event to all SS PMC owners.
3674147191Sjkoshy	 */
3675147191Sjkoshy	LIST_FOREACH(po, &pmc_ss_owners, po_ssnext)
3676147191Sjkoshy	    if (po->po_flags & PMC_PO_OWNS_LOGFILE)
3677147191Sjkoshy		    pmclog_process_sysexit(po, p->p_pid);
3678145256Sjkoshy
3679147191Sjkoshy	if (!is_using_hwpmcs)
3680147191Sjkoshy		return;
3681147191Sjkoshy
3682147191Sjkoshy	PMC_GET_SX_XLOCK();
3683147191Sjkoshy	PMCDBG(PRC,EXT,1,"process-exit proc=%p (%d, %s)", p, p->p_pid,
3684147191Sjkoshy	    p->p_comm);
3685147191Sjkoshy
3686147191Sjkoshy	/*
3687147191Sjkoshy	 * Since this code is invoked by the last thread in an exiting
3688147191Sjkoshy	 * process, we would have context switched IN at some prior
3689147191Sjkoshy	 * point.  However, with PREEMPTION, kernel mode context
3690147191Sjkoshy	 * switches may happen any time, so we want to disable a
3691147191Sjkoshy	 * context switch OUT till we get any PMCs targetting this
3692147191Sjkoshy	 * process off the hardware.
3693147191Sjkoshy	 *
3694147191Sjkoshy	 * We also need to atomically remove this process'
3695147191Sjkoshy	 * entry from our target process hash table, using
3696147191Sjkoshy	 * PMC_FLAG_REMOVE.
3697147191Sjkoshy	 */
3698147191Sjkoshy	PMCDBG(PRC,EXT,1, "process-exit proc=%p (%d, %s)", p, p->p_pid,
3699147191Sjkoshy	    p->p_comm);
3700147191Sjkoshy
3701147191Sjkoshy	critical_enter(); /* no preemption */
3702147191Sjkoshy
3703147191Sjkoshy	cpu = curthread->td_oncpu;
3704147191Sjkoshy
3705147191Sjkoshy	if ((pp = pmc_find_process_descriptor(p,
3706147191Sjkoshy		 PMC_FLAG_REMOVE)) != NULL) {
3707147191Sjkoshy
3708147191Sjkoshy		PMCDBG(PRC,EXT,2,
3709147191Sjkoshy		    "process-exit proc=%p pmc-process=%p", p, pp);
3710147191Sjkoshy
3711147191Sjkoshy		/*
3712147191Sjkoshy		 * The exiting process could the target of
3713147191Sjkoshy		 * some PMCs which will be running on
3714147191Sjkoshy		 * currently executing CPU.
3715147191Sjkoshy		 *
3716147191Sjkoshy		 * We need to turn these PMCs off like we
3717147191Sjkoshy		 * would do at context switch OUT time.
3718147191Sjkoshy		 */
3719147191Sjkoshy		for (ri = 0; ri < md->pmd_npmc; ri++) {
3720147191Sjkoshy
3721147191Sjkoshy			/*
3722147191Sjkoshy			 * Pick up the pmc pointer from hardware
3723147191Sjkoshy			 * state similar to the CSW_OUT code.
3724147191Sjkoshy			 */
3725147191Sjkoshy			pm = NULL;
3726147191Sjkoshy			(void) (*md->pmd_get_config)(cpu, ri, &pm);
3727147191Sjkoshy
3728147191Sjkoshy			PMCDBG(PRC,EXT,2, "ri=%d pm=%p", ri, pm);
3729147191Sjkoshy
3730147191Sjkoshy			if (pm == NULL ||
3731147191Sjkoshy			    !PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm)))
3732147191Sjkoshy				continue;
3733147191Sjkoshy
3734147191Sjkoshy			PMCDBG(PRC,EXT,2, "ppmcs[%d]=%p pm=%p "
3735147191Sjkoshy			    "state=%d", ri, pp->pp_pmcs[ri].pp_pmc,
3736147191Sjkoshy			    pm, pm->pm_state);
3737147191Sjkoshy
3738147191Sjkoshy			KASSERT(PMC_TO_ROWINDEX(pm) == ri,
3739147191Sjkoshy			    ("[pmc,%d] ri mismatch pmc(%d) ri(%d)",
3740147191Sjkoshy				__LINE__, PMC_TO_ROWINDEX(pm), ri));
3741147191Sjkoshy
3742147191Sjkoshy			KASSERT(pm == pp->pp_pmcs[ri].pp_pmc,
3743147191Sjkoshy			    ("[pmc,%d] pm %p != pp_pmcs[%d] %p",
3744147191Sjkoshy				__LINE__, pm, ri, pp->pp_pmcs[ri].pp_pmc));
3745147191Sjkoshy
3746147191Sjkoshy			(void) md->pmd_stop_pmc(cpu, ri);
3747147191Sjkoshy
3748147191Sjkoshy			KASSERT(pm->pm_runcount > 0,
3749147191Sjkoshy			    ("[pmc,%d] bad runcount ri %d rc %d",
3750147191Sjkoshy				__LINE__, ri, pm->pm_runcount));
3751147191Sjkoshy
3752147867Sjkoshy			/* Stop hardware only if it is actually running */
3753147191Sjkoshy			if (pm->pm_state == PMC_STATE_RUNNING &&
3754147867Sjkoshy			    pm->pm_stalled == 0) {
3755147191Sjkoshy				md->pmd_read_pmc(cpu, ri, &newvalue);
3756147191Sjkoshy				tmp = newvalue -
3757147191Sjkoshy				    PMC_PCPU_SAVED(cpu,ri);
3758147191Sjkoshy
3759147191Sjkoshy				mtx_pool_lock_spin(pmc_mtxpool, pm);
3760147191Sjkoshy				pm->pm_gv.pm_savedvalue += tmp;
3761147191Sjkoshy				pp->pp_pmcs[ri].pp_pmcval += tmp;
3762147191Sjkoshy				mtx_pool_unlock_spin(pmc_mtxpool, pm);
3763147191Sjkoshy			}
3764147191Sjkoshy
3765147191Sjkoshy			atomic_subtract_rel_32(&pm->pm_runcount,1);
3766147191Sjkoshy
3767147191Sjkoshy			KASSERT((int) pm->pm_runcount >= 0,
3768147191Sjkoshy			    ("[pmc,%d] runcount is %d", __LINE__, ri));
3769147191Sjkoshy
3770147191Sjkoshy			(void) md->pmd_config_pmc(cpu, ri, NULL);
3771147191Sjkoshy		}
3772147191Sjkoshy
3773147191Sjkoshy		/*
3774147191Sjkoshy		 * Inform the MD layer of this pseudo "context switch
3775147191Sjkoshy		 * out"
3776147191Sjkoshy		 */
3777147191Sjkoshy		(void) md->pmd_switch_out(pmc_pcpu[cpu], pp);
3778147191Sjkoshy
3779147191Sjkoshy		critical_exit(); /* ok to be pre-empted now */
3780147191Sjkoshy
3781147191Sjkoshy		/*
3782147191Sjkoshy		 * Unlink this process from the PMCs that are
3783147191Sjkoshy		 * targetting it.  This will send a signal to
3784147191Sjkoshy		 * all PMC owner's whose PMCs are orphaned.
3785147191Sjkoshy		 *
3786147191Sjkoshy		 * Log PMC value at exit time if requested.
3787147191Sjkoshy		 */
3788147191Sjkoshy		for (ri = 0; ri < md->pmd_npmc; ri++)
3789147191Sjkoshy			if ((pm = pp->pp_pmcs[ri].pp_pmc) != NULL) {
3790147867Sjkoshy				if (pm->pm_flags & PMC_F_NEEDS_LOGFILE &&
3791147867Sjkoshy				    PMC_IS_COUNTING_MODE(PMC_TO_MODE(pm)))
3792147191Sjkoshy					pmclog_process_procexit(pm, pp);
3793147191Sjkoshy				pmc_unlink_target_process(pm, pp);
3794147191Sjkoshy			}
3795147191Sjkoshy		FREE(pp, M_PMC);
3796147191Sjkoshy
3797147191Sjkoshy	} else
3798147191Sjkoshy		critical_exit(); /* pp == NULL */
3799147191Sjkoshy
3800147191Sjkoshy
3801147191Sjkoshy	/*
3802147191Sjkoshy	 * If the process owned PMCs, free them up and free up
3803147191Sjkoshy	 * memory.
3804147191Sjkoshy	 */
3805147191Sjkoshy	if ((po = pmc_find_owner_descriptor(p)) != NULL) {
3806147191Sjkoshy		pmc_remove_owner(po);
3807147191Sjkoshy		pmc_destroy_owner_descriptor(po);
3808145256Sjkoshy	}
3809147191Sjkoshy
3810147191Sjkoshy	sx_xunlock(&pmc_sx);
3811145256Sjkoshy}
3812145256Sjkoshy
3813145256Sjkoshy/*
3814145256Sjkoshy * Handle a process fork.
3815145256Sjkoshy *
3816145256Sjkoshy * If the parent process 'p1' is under HWPMC monitoring, then copy
3817145256Sjkoshy * over any attached PMCs that have 'do_descendants' semantics.
3818145256Sjkoshy */
3819145256Sjkoshy
3820145256Sjkoshystatic void
3821147191Sjkoshypmc_process_fork(void *arg __unused, struct proc *p1, struct proc *newproc,
3822145256Sjkoshy    int flags)
3823145256Sjkoshy{
3824145256Sjkoshy	int is_using_hwpmcs;
3825147191Sjkoshy	unsigned int ri;
3826147191Sjkoshy	uint32_t do_descendants;
3827147191Sjkoshy	struct pmc *pm;
3828147191Sjkoshy	struct pmc_owner *po;
3829147191Sjkoshy	struct pmc_process *ppnew, *ppold;
3830145256Sjkoshy
3831145256Sjkoshy	(void) flags;		/* unused parameter */
3832145256Sjkoshy
3833145256Sjkoshy	PROC_LOCK(p1);
3834145256Sjkoshy	is_using_hwpmcs = p1->p_flag & P_HWPMC;
3835145256Sjkoshy	PROC_UNLOCK(p1);
3836145256Sjkoshy
3837147191Sjkoshy	/*
3838147191Sjkoshy	 * If there are system-wide sampling PMCs active, we need to
3839147191Sjkoshy	 * log all fork events to their owner's logs.
3840147191Sjkoshy	 */
3841147191Sjkoshy
3842147191Sjkoshy	LIST_FOREACH(po, &pmc_ss_owners, po_ssnext)
3843147191Sjkoshy	    if (po->po_flags & PMC_PO_OWNS_LOGFILE)
3844147191Sjkoshy		    pmclog_process_procfork(po, p1->p_pid, newproc->p_pid);
3845147191Sjkoshy
3846147191Sjkoshy	if (!is_using_hwpmcs)
3847147191Sjkoshy		return;
3848147191Sjkoshy
3849147191Sjkoshy	PMC_GET_SX_XLOCK();
3850147191Sjkoshy	PMCDBG(PMC,FRK,1, "process-fork proc=%p (%d, %s) -> %p", p1,
3851147191Sjkoshy	    p1->p_pid, p1->p_comm, newproc);
3852147191Sjkoshy
3853147191Sjkoshy	/*
3854147191Sjkoshy	 * If the parent process (curthread->td_proc) is a
3855147191Sjkoshy	 * target of any PMCs, look for PMCs that are to be
3856147191Sjkoshy	 * inherited, and link these into the new process
3857147191Sjkoshy	 * descriptor.
3858147191Sjkoshy	 */
3859147191Sjkoshy	if ((ppold = pmc_find_process_descriptor(curthread->td_proc,
3860147191Sjkoshy		 PMC_FLAG_NONE)) == NULL)
3861147191Sjkoshy		goto done;		/* nothing to do */
3862147191Sjkoshy
3863147191Sjkoshy	do_descendants = 0;
3864147191Sjkoshy	for (ri = 0; ri < md->pmd_npmc; ri++)
3865147191Sjkoshy		if ((pm = ppold->pp_pmcs[ri].pp_pmc) != NULL)
3866147191Sjkoshy			do_descendants |= pm->pm_flags & PMC_F_DESCENDANTS;
3867147191Sjkoshy	if (do_descendants == 0) /* nothing to do */
3868147191Sjkoshy		goto done;
3869147191Sjkoshy
3870147191Sjkoshy	/* allocate a descriptor for the new process  */
3871147191Sjkoshy	if ((ppnew = pmc_find_process_descriptor(newproc,
3872147191Sjkoshy		 PMC_FLAG_ALLOCATE)) == NULL)
3873147191Sjkoshy		goto done;
3874147191Sjkoshy
3875147191Sjkoshy	/*
3876147191Sjkoshy	 * Run through all PMCs that were targeting the old process
3877147191Sjkoshy	 * and which specified F_DESCENDANTS and attach them to the
3878147191Sjkoshy	 * new process.
3879147191Sjkoshy	 *
3880147191Sjkoshy	 * Log the fork event to all owners of PMCs attached to this
3881147191Sjkoshy	 * process, if not already logged.
3882147191Sjkoshy	 */
3883147191Sjkoshy	for (ri = 0; ri < md->pmd_npmc; ri++)
3884147191Sjkoshy		if ((pm = ppold->pp_pmcs[ri].pp_pmc) != NULL &&
3885147191Sjkoshy		    (pm->pm_flags & PMC_F_DESCENDANTS)) {
3886147191Sjkoshy			pmc_link_target_process(pm, ppnew);
3887147191Sjkoshy			po = pm->pm_owner;
3888147191Sjkoshy			if (po->po_sscount == 0 &&
3889147191Sjkoshy			    po->po_flags & PMC_PO_OWNS_LOGFILE)
3890147191Sjkoshy				pmclog_process_procfork(po, p1->p_pid,
3891147191Sjkoshy				    newproc->p_pid);
3892147191Sjkoshy		}
3893147191Sjkoshy
3894147191Sjkoshy	/*
3895147191Sjkoshy	 * Now mark the new process as being tracked by this driver.
3896147191Sjkoshy	 */
3897147191Sjkoshy	PROC_LOCK(newproc);
3898147191Sjkoshy	newproc->p_flag |= P_HWPMC;
3899147191Sjkoshy	PROC_UNLOCK(newproc);
3900147191Sjkoshy
3901147191Sjkoshy done:
3902147191Sjkoshy	sx_xunlock(&pmc_sx);
3903145256Sjkoshy}
3904145256Sjkoshy
3905145256Sjkoshy
3906145256Sjkoshy/*
3907145256Sjkoshy * initialization
3908145256Sjkoshy */
3909145256Sjkoshy
3910145256Sjkoshystatic const char *pmc_name_of_pmcclass[] = {
3911145256Sjkoshy#undef	__PMC_CLASS
3912145256Sjkoshy#define	__PMC_CLASS(N) #N ,
3913145256Sjkoshy	__PMC_CLASSES()
3914145256Sjkoshy};
3915145256Sjkoshy
3916145256Sjkoshystatic int
3917145256Sjkoshypmc_initialize(void)
3918145256Sjkoshy{
3919147191Sjkoshy	int cpu, error, n;
3920145256Sjkoshy	struct pmc_binding pb;
3921147191Sjkoshy	struct pmc_samplebuffer *sb;
3922145256Sjkoshy
3923145256Sjkoshy	md = NULL;
3924145256Sjkoshy	error = 0;
3925145256Sjkoshy
3926153110Sru#ifdef	DEBUG
3927145256Sjkoshy	/* parse debug flags first */
3928145256Sjkoshy	if (TUNABLE_STR_FETCH(PMC_SYSCTL_NAME_PREFIX "debugflags",
3929145256Sjkoshy		pmc_debugstr, sizeof(pmc_debugstr)))
3930145256Sjkoshy		pmc_debugflags_parse(pmc_debugstr,
3931145256Sjkoshy		    pmc_debugstr+strlen(pmc_debugstr));
3932145256Sjkoshy#endif
3933145256Sjkoshy
3934145256Sjkoshy	PMCDBG(MOD,INI,0, "PMC Initialize (version %x)", PMC_VERSION);
3935145256Sjkoshy
3936148562Sjkoshy	/* check kernel version */
3937148562Sjkoshy	if (pmc_kernel_version != PMC_VERSION) {
3938148562Sjkoshy		if (pmc_kernel_version == 0)
3939148562Sjkoshy			printf("hwpmc: this kernel has not been compiled with "
3940148562Sjkoshy			    "'options HWPMC_HOOKS'.\n");
3941148562Sjkoshy		else
3942148562Sjkoshy			printf("hwpmc: kernel version (0x%x) does not match "
3943148562Sjkoshy			    "module version (0x%x).\n", pmc_kernel_version,
3944148562Sjkoshy			    PMC_VERSION);
3945148562Sjkoshy		return EPROGMISMATCH;
3946148562Sjkoshy	}
3947148562Sjkoshy
3948145256Sjkoshy	/*
3949145256Sjkoshy	 * check sysctl parameters
3950145256Sjkoshy	 */
3951145256Sjkoshy
3952145256Sjkoshy	if (pmc_hashsize <= 0) {
3953147191Sjkoshy		(void) printf("hwpmc: tunable hashsize=%d must be greater "
3954147191Sjkoshy		    "than zero.\n", pmc_hashsize);
3955145256Sjkoshy		pmc_hashsize = PMC_HASH_SIZE;
3956145256Sjkoshy	}
3957145256Sjkoshy
3958147191Sjkoshy	if (pmc_nsamples <= 0 || pmc_nsamples > 65535) {
3959153735Sjkoshy		(void) printf("hwpmc: tunable nsamples=%d out of range.\n",
3960153735Sjkoshy		    pmc_nsamples);
3961147191Sjkoshy		pmc_nsamples = PMC_NSAMPLES;
3962147191Sjkoshy	}
3963145256Sjkoshy
3964147191Sjkoshy	md = pmc_md_initialize();
3965147191Sjkoshy
3966145256Sjkoshy	if (md == NULL || md->pmd_init == NULL)
3967145256Sjkoshy		return ENOSYS;
3968145256Sjkoshy
3969145256Sjkoshy	/* allocate space for the per-cpu array */
3970145256Sjkoshy	MALLOC(pmc_pcpu, struct pmc_cpu **, mp_ncpus * sizeof(struct pmc_cpu *),
3971145256Sjkoshy	    M_PMC, M_WAITOK|M_ZERO);
3972145256Sjkoshy
3973145256Sjkoshy	/* per-cpu 'saved values' for managing process-mode PMCs */
3974145256Sjkoshy	MALLOC(pmc_pcpu_saved, pmc_value_t *,
3975145256Sjkoshy	    sizeof(pmc_value_t) * mp_ncpus * md->pmd_npmc, M_PMC, M_WAITOK);
3976145256Sjkoshy
3977145256Sjkoshy	/* perform cpu dependent initialization */
3978145256Sjkoshy	pmc_save_cpu_binding(&pb);
3979145256Sjkoshy	for (cpu = 0; cpu < mp_ncpus; cpu++) {
3980145256Sjkoshy		if (pmc_cpu_is_disabled(cpu))
3981145256Sjkoshy			continue;
3982145256Sjkoshy		pmc_select_cpu(cpu);
3983145256Sjkoshy		if ((error = md->pmd_init(cpu)) != 0)
3984145256Sjkoshy			break;
3985145256Sjkoshy	}
3986145256Sjkoshy	pmc_restore_cpu_binding(&pb);
3987145256Sjkoshy
3988145256Sjkoshy	if (error != 0)
3989145256Sjkoshy		return error;
3990145256Sjkoshy
3991147191Sjkoshy	/* allocate space for the sample array */
3992147191Sjkoshy	for (cpu = 0; cpu < mp_ncpus; cpu++) {
3993147191Sjkoshy		if (pmc_cpu_is_disabled(cpu))
3994147191Sjkoshy			continue;
3995147191Sjkoshy		MALLOC(sb, struct pmc_samplebuffer *,
3996147191Sjkoshy		    sizeof(struct pmc_samplebuffer) +
3997147191Sjkoshy		    pmc_nsamples * sizeof(struct pmc_sample), M_PMC,
3998147191Sjkoshy		    M_WAITOK|M_ZERO);
3999147191Sjkoshy
4000147191Sjkoshy		sb->ps_read = sb->ps_write = sb->ps_samples;
4001153735Sjkoshy		sb->ps_fence = sb->ps_samples + pmc_nsamples;
4002147191Sjkoshy		KASSERT(pmc_pcpu[cpu] != NULL,
4003147191Sjkoshy		    ("[pmc,%d] cpu=%d Null per-cpu data", __LINE__, cpu));
4004147191Sjkoshy
4005147191Sjkoshy		pmc_pcpu[cpu]->pc_sb = sb;
4006147191Sjkoshy	}
4007147191Sjkoshy
4008145256Sjkoshy	/* allocate space for the row disposition array */
4009145256Sjkoshy	pmc_pmcdisp = malloc(sizeof(enum pmc_mode) * md->pmd_npmc,
4010145256Sjkoshy	    M_PMC, M_WAITOK|M_ZERO);
4011145256Sjkoshy
4012145256Sjkoshy	KASSERT(pmc_pmcdisp != NULL,
4013145256Sjkoshy	    ("[pmc,%d] pmcdisp allocation returned NULL", __LINE__));
4014145256Sjkoshy
4015145256Sjkoshy	/* mark all PMCs as available */
4016145256Sjkoshy	for (n = 0; n < (int) md->pmd_npmc; n++)
4017145256Sjkoshy		PMC_MARK_ROW_FREE(n);
4018145256Sjkoshy
4019145256Sjkoshy	/* allocate thread hash tables */
4020145256Sjkoshy	pmc_ownerhash = hashinit(pmc_hashsize, M_PMC,
4021145256Sjkoshy	    &pmc_ownerhashmask);
4022145256Sjkoshy
4023145256Sjkoshy	pmc_processhash = hashinit(pmc_hashsize, M_PMC,
4024145256Sjkoshy	    &pmc_processhashmask);
4025145256Sjkoshy	mtx_init(&pmc_processhash_mtx, "pmc-process-hash", "pmc", MTX_SPIN);
4026145256Sjkoshy
4027147191Sjkoshy	LIST_INIT(&pmc_ss_owners);
4028147191Sjkoshy	pmc_ss_count = 0;
4029147191Sjkoshy
4030145256Sjkoshy	/* allocate a pool of spin mutexes */
4031145256Sjkoshy	pmc_mtxpool = mtx_pool_create("pmc", pmc_mtxpool_size, MTX_SPIN);
4032145256Sjkoshy
4033145256Sjkoshy	PMCDBG(MOD,INI,1, "pmc_ownerhash=%p, mask=0x%lx "
4034145256Sjkoshy	    "targethash=%p mask=0x%lx", pmc_ownerhash, pmc_ownerhashmask,
4035145256Sjkoshy	    pmc_processhash, pmc_processhashmask);
4036145256Sjkoshy
4037145256Sjkoshy	/* register process {exit,fork,exec} handlers */
4038145256Sjkoshy	pmc_exit_tag = EVENTHANDLER_REGISTER(process_exit,
4039145256Sjkoshy	    pmc_process_exit, NULL, EVENTHANDLER_PRI_ANY);
4040145256Sjkoshy	pmc_fork_tag = EVENTHANDLER_REGISTER(process_fork,
4041145256Sjkoshy	    pmc_process_fork, NULL, EVENTHANDLER_PRI_ANY);
4042145256Sjkoshy
4043147191Sjkoshy	/* initialize logging */
4044147191Sjkoshy	pmclog_initialize();
4045147191Sjkoshy
4046145256Sjkoshy	/* set hook functions */
4047145256Sjkoshy	pmc_intr = md->pmd_intr;
4048145256Sjkoshy	pmc_hook = pmc_hook_handler;
4049145256Sjkoshy
4050145256Sjkoshy	if (error == 0) {
4051145256Sjkoshy		printf(PMC_MODULE_NAME ":");
4052149373Sjkoshy		for (n = 0; n < (int) md->pmd_nclass; n++) {
4053149373Sjkoshy			printf(" %s/%d/0x%b",
4054145774Sjkoshy			    pmc_name_of_pmcclass[md->pmd_classes[n].pm_class],
4055149373Sjkoshy			    md->pmd_nclasspmcs[n],
4056149373Sjkoshy			    md->pmd_classes[n].pm_caps,
4057149373Sjkoshy			    "\20"
4058149373Sjkoshy			    "\1INT\2USR\3SYS\4EDG\5THR"
4059149373Sjkoshy			    "\6REA\7WRI\10INV\11QUA\12PRC"
4060149373Sjkoshy			    "\13TAG\14CSC");
4061149373Sjkoshy		}
4062145256Sjkoshy		printf("\n");
4063145256Sjkoshy	}
4064145256Sjkoshy
4065145256Sjkoshy	return error;
4066145256Sjkoshy}
4067145256Sjkoshy
4068145256Sjkoshy/* prepare to be unloaded */
4069145256Sjkoshystatic void
4070145256Sjkoshypmc_cleanup(void)
4071145256Sjkoshy{
4072145256Sjkoshy	int cpu;
4073145256Sjkoshy	struct pmc_ownerhash *ph;
4074145256Sjkoshy	struct pmc_owner *po, *tmp;
4075145256Sjkoshy	struct pmc_binding pb;
4076153110Sru#ifdef	DEBUG
4077145256Sjkoshy	struct pmc_processhash *prh;
4078145256Sjkoshy#endif
4079145256Sjkoshy
4080145256Sjkoshy	PMCDBG(MOD,INI,0, "%s", "cleanup");
4081145256Sjkoshy
4082147191Sjkoshy	/* switch off sampling */
4083147191Sjkoshy	atomic_store_rel_int(&pmc_cpumask, 0);
4084147191Sjkoshy	pmc_intr = NULL;
4085145256Sjkoshy
4086145256Sjkoshy	sx_xlock(&pmc_sx);
4087145256Sjkoshy	if (pmc_hook == NULL) {	/* being unloaded already */
4088145256Sjkoshy		sx_xunlock(&pmc_sx);
4089145256Sjkoshy		return;
4090145256Sjkoshy	}
4091145256Sjkoshy
4092145256Sjkoshy	pmc_hook = NULL; /* prevent new threads from entering module */
4093145256Sjkoshy
4094145256Sjkoshy	/* deregister event handlers */
4095145256Sjkoshy	EVENTHANDLER_DEREGISTER(process_fork, pmc_fork_tag);
4096145256Sjkoshy	EVENTHANDLER_DEREGISTER(process_exit, pmc_exit_tag);
4097145256Sjkoshy
4098145256Sjkoshy	/* send SIGBUS to all owner threads, free up allocations */
4099145256Sjkoshy	if (pmc_ownerhash)
4100145256Sjkoshy		for (ph = pmc_ownerhash;
4101145256Sjkoshy		     ph <= &pmc_ownerhash[pmc_ownerhashmask];
4102145256Sjkoshy		     ph++) {
4103145256Sjkoshy			LIST_FOREACH_SAFE(po, ph, po_next, tmp) {
4104145256Sjkoshy				pmc_remove_owner(po);
4105145256Sjkoshy
4106145256Sjkoshy				/* send SIGBUS to owner processes */
4107145256Sjkoshy				PMCDBG(MOD,INI,2, "cleanup signal proc=%p "
4108145256Sjkoshy				    "(%d, %s)", po->po_owner,
4109145256Sjkoshy				    po->po_owner->p_pid,
4110145256Sjkoshy				    po->po_owner->p_comm);
4111145256Sjkoshy
4112145256Sjkoshy				PROC_LOCK(po->po_owner);
4113145256Sjkoshy				psignal(po->po_owner, SIGBUS);
4114145256Sjkoshy				PROC_UNLOCK(po->po_owner);
4115147191Sjkoshy
4116147191Sjkoshy				pmc_destroy_owner_descriptor(po);
4117145256Sjkoshy			}
4118145256Sjkoshy		}
4119145256Sjkoshy
4120145256Sjkoshy	/* reclaim allocated data structures */
4121145256Sjkoshy	if (pmc_mtxpool)
4122145256Sjkoshy		mtx_pool_destroy(&pmc_mtxpool);
4123145256Sjkoshy
4124145256Sjkoshy	mtx_destroy(&pmc_processhash_mtx);
4125145256Sjkoshy	if (pmc_processhash) {
4126153110Sru#ifdef	DEBUG
4127145256Sjkoshy		struct pmc_process *pp;
4128145256Sjkoshy
4129145256Sjkoshy		PMCDBG(MOD,INI,3, "%s", "destroy process hash");
4130145256Sjkoshy		for (prh = pmc_processhash;
4131145256Sjkoshy		     prh <= &pmc_processhash[pmc_processhashmask];
4132145256Sjkoshy		     prh++)
4133145256Sjkoshy			LIST_FOREACH(pp, prh, pp_next)
4134145256Sjkoshy			    PMCDBG(MOD,INI,3, "pid=%d", pp->pp_proc->p_pid);
4135145256Sjkoshy#endif
4136145256Sjkoshy
4137145256Sjkoshy		hashdestroy(pmc_processhash, M_PMC, pmc_processhashmask);
4138145256Sjkoshy		pmc_processhash = NULL;
4139145256Sjkoshy	}
4140145256Sjkoshy
4141145256Sjkoshy	if (pmc_ownerhash) {
4142145256Sjkoshy		PMCDBG(MOD,INI,3, "%s", "destroy owner hash");
4143145256Sjkoshy		hashdestroy(pmc_ownerhash, M_PMC, pmc_ownerhashmask);
4144145256Sjkoshy		pmc_ownerhash = NULL;
4145145256Sjkoshy	}
4146145256Sjkoshy
4147147191Sjkoshy	KASSERT(LIST_EMPTY(&pmc_ss_owners),
4148147191Sjkoshy	    ("[pmc,%d] Global SS owner list not empty", __LINE__));
4149147191Sjkoshy	KASSERT(pmc_ss_count == 0,
4150147191Sjkoshy	    ("[pmc,%d] Global SS count not empty", __LINE__));
4151147191Sjkoshy
4152153735Sjkoshy	/* free the per-cpu sample buffers */
4153153735Sjkoshy	for (cpu = 0; cpu < mp_ncpus; cpu++) {
4154153735Sjkoshy		if (pmc_cpu_is_disabled(cpu))
4155153735Sjkoshy			continue;
4156153735Sjkoshy		KASSERT(pmc_pcpu[cpu]->pc_sb != NULL,
4157153735Sjkoshy		    ("[pmc,%d] Null cpu sample buffer cpu=%d", __LINE__,
4158153735Sjkoshy			cpu));
4159153735Sjkoshy		FREE(pmc_pcpu[cpu]->pc_sb, M_PMC);
4160153735Sjkoshy		pmc_pcpu[cpu]->pc_sb = NULL;
4161153735Sjkoshy	}
4162153735Sjkoshy
4163145256Sjkoshy 	/* do processor dependent cleanup */
4164145256Sjkoshy	PMCDBG(MOD,INI,3, "%s", "md cleanup");
4165145256Sjkoshy	if (md) {
4166145256Sjkoshy		pmc_save_cpu_binding(&pb);
4167145256Sjkoshy		for (cpu = 0; cpu < mp_ncpus; cpu++) {
4168145256Sjkoshy			PMCDBG(MOD,INI,1,"pmc-cleanup cpu=%d pcs=%p",
4169145256Sjkoshy			    cpu, pmc_pcpu[cpu]);
4170145256Sjkoshy			if (pmc_cpu_is_disabled(cpu))
4171145256Sjkoshy				continue;
4172145256Sjkoshy			pmc_select_cpu(cpu);
4173145256Sjkoshy			if (pmc_pcpu[cpu])
4174145256Sjkoshy				(void) md->pmd_cleanup(cpu);
4175145256Sjkoshy		}
4176145256Sjkoshy		FREE(md, M_PMC);
4177145256Sjkoshy		md = NULL;
4178145256Sjkoshy		pmc_restore_cpu_binding(&pb);
4179145256Sjkoshy	}
4180145256Sjkoshy
4181145256Sjkoshy	/* deallocate per-cpu structures */
4182145256Sjkoshy	FREE(pmc_pcpu, M_PMC);
4183145256Sjkoshy	pmc_pcpu = NULL;
4184145256Sjkoshy
4185145256Sjkoshy	FREE(pmc_pcpu_saved, M_PMC);
4186145256Sjkoshy	pmc_pcpu_saved = NULL;
4187145256Sjkoshy
4188145256Sjkoshy	if (pmc_pmcdisp) {
4189145256Sjkoshy		FREE(pmc_pmcdisp, M_PMC);
4190145256Sjkoshy		pmc_pmcdisp = NULL;
4191145256Sjkoshy	}
4192145256Sjkoshy
4193147191Sjkoshy	pmclog_shutdown();
4194147191Sjkoshy
4195145256Sjkoshy	sx_xunlock(&pmc_sx); 	/* we are done */
4196145256Sjkoshy}
4197145256Sjkoshy
4198145256Sjkoshy/*
4199145256Sjkoshy * The function called at load/unload.
4200145256Sjkoshy */
4201145256Sjkoshy
4202145256Sjkoshystatic int
4203145256Sjkoshyload (struct module *module __unused, int cmd, void *arg __unused)
4204145256Sjkoshy{
4205145256Sjkoshy	int error;
4206145256Sjkoshy
4207145256Sjkoshy	error = 0;
4208145256Sjkoshy
4209145256Sjkoshy	switch (cmd) {
4210145256Sjkoshy	case MOD_LOAD :
4211145256Sjkoshy		/* initialize the subsystem */
4212145256Sjkoshy		error = pmc_initialize();
4213145256Sjkoshy		if (error != 0)
4214145256Sjkoshy			break;
4215145256Sjkoshy		PMCDBG(MOD,INI,1, "syscall=%d ncpus=%d",
4216145256Sjkoshy		    pmc_syscall_num, mp_ncpus);
4217145256Sjkoshy		break;
4218145256Sjkoshy
4219145256Sjkoshy
4220145256Sjkoshy	case MOD_UNLOAD :
4221145256Sjkoshy	case MOD_SHUTDOWN:
4222145256Sjkoshy		pmc_cleanup();
4223145256Sjkoshy		PMCDBG(MOD,INI,1, "%s", "unloaded");
4224145256Sjkoshy		break;
4225145256Sjkoshy
4226145256Sjkoshy	default :
4227145256Sjkoshy		error = EINVAL;	/* XXX should panic(9) */
4228145256Sjkoshy		break;
4229145256Sjkoshy	}
4230145256Sjkoshy
4231145256Sjkoshy	return error;
4232145256Sjkoshy}
4233145256Sjkoshy
4234145256Sjkoshy/* memory pool */
4235145256SjkoshyMALLOC_DEFINE(M_PMC, "pmc", "Memory space for the PMC module");
4236