hwpmc_mod.c revision 164033
1145256Sjkoshy/*-
2157144Sjkoshy * Copyright (c) 2003-2006 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 164033 2006-11-06 13:42:10Z rwatson $");
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>
44164033Srwatson#include <sys/priv.h>
45145256Sjkoshy#include <sys/proc.h>
46145256Sjkoshy#include <sys/queue.h>
47147191Sjkoshy#include <sys/resourcevar.h>
48145256Sjkoshy#include <sys/sched.h>
49145256Sjkoshy#include <sys/signalvar.h>
50145256Sjkoshy#include <sys/smp.h>
51145256Sjkoshy#include <sys/sx.h>
52145256Sjkoshy#include <sys/sysctl.h>
53145256Sjkoshy#include <sys/sysent.h>
54145256Sjkoshy#include <sys/systm.h>
55147191Sjkoshy#include <sys/vnode.h>
56145256Sjkoshy
57157144Sjkoshy#include <sys/linker.h>		/* needs to be after <sys/malloc.h> */
58157144Sjkoshy
59147191Sjkoshy#include <machine/atomic.h>
60145256Sjkoshy#include <machine/md_var.h>
61145256Sjkoshy
62145256Sjkoshy/*
63145256Sjkoshy * Types
64145256Sjkoshy */
65145256Sjkoshy
66145256Sjkoshyenum pmc_flags {
67145256Sjkoshy	PMC_FLAG_NONE	  = 0x00, /* do nothing */
68145256Sjkoshy	PMC_FLAG_REMOVE   = 0x01, /* atomically remove entry from hash */
69145256Sjkoshy	PMC_FLAG_ALLOCATE = 0x02, /* add entry to hash if not found */
70145256Sjkoshy};
71145256Sjkoshy
72145256Sjkoshy/*
73145256Sjkoshy * The offset in sysent where the syscall is allocated.
74145256Sjkoshy */
75145256Sjkoshy
76145256Sjkoshystatic int pmc_syscall_num = NO_SYSCALL;
77145256Sjkoshystruct pmc_cpu		**pmc_pcpu;	 /* per-cpu state */
78145256Sjkoshypmc_value_t		*pmc_pcpu_saved; /* saved PMC values: CSW handling */
79145256Sjkoshy
80145256Sjkoshy#define	PMC_PCPU_SAVED(C,R)	pmc_pcpu_saved[(R) + md->pmd_npmc*(C)]
81145256Sjkoshy
82145256Sjkoshystruct mtx_pool		*pmc_mtxpool;
83145256Sjkoshystatic int		*pmc_pmcdisp;	 /* PMC row dispositions */
84145256Sjkoshy
85145256Sjkoshy#define	PMC_ROW_DISP_IS_FREE(R)		(pmc_pmcdisp[(R)] == 0)
86145256Sjkoshy#define	PMC_ROW_DISP_IS_THREAD(R)	(pmc_pmcdisp[(R)] > 0)
87145256Sjkoshy#define	PMC_ROW_DISP_IS_STANDALONE(R)	(pmc_pmcdisp[(R)] < 0)
88145256Sjkoshy
89145256Sjkoshy#define	PMC_MARK_ROW_FREE(R) do {					  \
90145256Sjkoshy	pmc_pmcdisp[(R)] = 0;						  \
91145256Sjkoshy} while (0)
92145256Sjkoshy
93145256Sjkoshy#define	PMC_MARK_ROW_STANDALONE(R) do {					  \
94145256Sjkoshy	KASSERT(pmc_pmcdisp[(R)] <= 0, ("[pmc,%d] row disposition error", \
95145256Sjkoshy		    __LINE__));						  \
96145256Sjkoshy	atomic_add_int(&pmc_pmcdisp[(R)], -1);				  \
97145256Sjkoshy	KASSERT(pmc_pmcdisp[(R)] >= (-mp_ncpus), ("[pmc,%d] row "	  \
98145256Sjkoshy		"disposition error", __LINE__));			  \
99145256Sjkoshy} while (0)
100145256Sjkoshy
101145256Sjkoshy#define	PMC_UNMARK_ROW_STANDALONE(R) do { 				  \
102145256Sjkoshy	atomic_add_int(&pmc_pmcdisp[(R)], 1);				  \
103145256Sjkoshy	KASSERT(pmc_pmcdisp[(R)] <= 0, ("[pmc,%d] row disposition error", \
104145256Sjkoshy		    __LINE__));						  \
105145256Sjkoshy} while (0)
106145256Sjkoshy
107145256Sjkoshy#define	PMC_MARK_ROW_THREAD(R) do {					  \
108145256Sjkoshy	KASSERT(pmc_pmcdisp[(R)] >= 0, ("[pmc,%d] row disposition error", \
109145256Sjkoshy		    __LINE__));						  \
110145256Sjkoshy	atomic_add_int(&pmc_pmcdisp[(R)], 1);				  \
111145256Sjkoshy} while (0)
112145256Sjkoshy
113145256Sjkoshy#define	PMC_UNMARK_ROW_THREAD(R) do {					  \
114145256Sjkoshy	atomic_add_int(&pmc_pmcdisp[(R)], -1);				  \
115145256Sjkoshy	KASSERT(pmc_pmcdisp[(R)] >= 0, ("[pmc,%d] row disposition error", \
116145256Sjkoshy		    __LINE__));						  \
117145256Sjkoshy} while (0)
118145256Sjkoshy
119145256Sjkoshy
120145256Sjkoshy/* various event handlers */
121145256Sjkoshystatic eventhandler_tag	pmc_exit_tag, pmc_fork_tag;
122145256Sjkoshy
123145256Sjkoshy/* Module statistics */
124145256Sjkoshystruct pmc_op_getdriverstats pmc_stats;
125145256Sjkoshy
126145256Sjkoshy/* Machine/processor dependent operations */
127145256Sjkoshystruct pmc_mdep  *md;
128145256Sjkoshy
129145256Sjkoshy/*
130145256Sjkoshy * Hash tables mapping owner processes and target threads to PMCs.
131145256Sjkoshy */
132145256Sjkoshy
133145256Sjkoshystruct mtx pmc_processhash_mtx;		/* spin mutex */
134145256Sjkoshystatic u_long pmc_processhashmask;
135145256Sjkoshystatic LIST_HEAD(pmc_processhash, pmc_process)	*pmc_processhash;
136145256Sjkoshy
137145256Sjkoshy/*
138145256Sjkoshy * Hash table of PMC owner descriptors.  This table is protected by
139145256Sjkoshy * the shared PMC "sx" lock.
140145256Sjkoshy */
141145256Sjkoshy
142145256Sjkoshystatic u_long pmc_ownerhashmask;
143145256Sjkoshystatic LIST_HEAD(pmc_ownerhash, pmc_owner)	*pmc_ownerhash;
144145256Sjkoshy
145145256Sjkoshy/*
146147191Sjkoshy * List of PMC owners with system-wide sampling PMCs.
147147191Sjkoshy */
148147191Sjkoshy
149147191Sjkoshystatic LIST_HEAD(, pmc_owner)			pmc_ss_owners;
150147191Sjkoshy
151147191Sjkoshy
152147191Sjkoshy/*
153145256Sjkoshy * Prototypes
154145256Sjkoshy */
155145256Sjkoshy
156153110Sru#ifdef	DEBUG
157145256Sjkoshystatic int	pmc_debugflags_sysctl_handler(SYSCTL_HANDLER_ARGS);
158145256Sjkoshystatic int	pmc_debugflags_parse(char *newstr, char *fence);
159145256Sjkoshy#endif
160145256Sjkoshy
161145256Sjkoshystatic int	load(struct module *module, int cmd, void *arg);
162147191Sjkoshystatic int	pmc_attach_process(struct proc *p, struct pmc *pm);
163145256Sjkoshystatic struct pmc *pmc_allocate_pmc_descriptor(void);
164147191Sjkoshystatic struct pmc_owner *pmc_allocate_owner_descriptor(struct proc *p);
165147191Sjkoshystatic int	pmc_attach_one_process(struct proc *p, struct pmc *pm);
166147191Sjkoshystatic int	pmc_can_allocate_rowindex(struct proc *p, unsigned int ri,
167147191Sjkoshy    int cpu);
168147191Sjkoshystatic int	pmc_can_attach(struct pmc *pm, struct proc *p);
169147191Sjkoshystatic void	pmc_cleanup(void);
170147191Sjkoshystatic int	pmc_detach_process(struct proc *p, struct pmc *pm);
171147191Sjkoshystatic int	pmc_detach_one_process(struct proc *p, struct pmc *pm,
172147191Sjkoshy    int flags);
173147191Sjkoshystatic void	pmc_destroy_owner_descriptor(struct pmc_owner *po);
174147191Sjkoshystatic struct pmc_owner *pmc_find_owner_descriptor(struct proc *p);
175147191Sjkoshystatic int	pmc_find_pmc(pmc_id_t pmcid, struct pmc **pm);
176145256Sjkoshystatic struct pmc *pmc_find_pmc_descriptor_in_process(struct pmc_owner *po,
177145256Sjkoshy    pmc_id_t pmc);
178145256Sjkoshystatic struct pmc_process *pmc_find_process_descriptor(struct proc *p,
179145256Sjkoshy    uint32_t mode);
180145774Sjkoshystatic void	pmc_force_context_switch(void);
181145256Sjkoshystatic void	pmc_link_target_process(struct pmc *pm,
182145256Sjkoshy    struct pmc_process *pp);
183147191Sjkoshystatic void	pmc_maybe_remove_owner(struct pmc_owner *po);
184147191Sjkoshystatic void	pmc_process_csw_in(struct thread *td);
185147191Sjkoshystatic void	pmc_process_csw_out(struct thread *td);
186145256Sjkoshystatic void	pmc_process_exit(void *arg, struct proc *p);
187145256Sjkoshystatic void	pmc_process_fork(void *arg, struct proc *p1,
188145256Sjkoshy    struct proc *p2, int n);
189147191Sjkoshystatic void	pmc_process_samples(int cpu);
190147191Sjkoshystatic void	pmc_release_pmc_descriptor(struct pmc *pmc);
191147191Sjkoshystatic void	pmc_remove_owner(struct pmc_owner *po);
192147191Sjkoshystatic void	pmc_remove_process_descriptor(struct pmc_process *pp);
193147191Sjkoshystatic void	pmc_restore_cpu_binding(struct pmc_binding *pb);
194147191Sjkoshystatic void	pmc_save_cpu_binding(struct pmc_binding *pb);
195147191Sjkoshystatic void	pmc_select_cpu(int cpu);
196145256Sjkoshystatic int	pmc_start(struct pmc *pm);
197145256Sjkoshystatic int	pmc_stop(struct pmc *pm);
198147191Sjkoshystatic int	pmc_syscall_handler(struct thread *td, void *syscall_args);
199147191Sjkoshystatic void	pmc_unlink_target_process(struct pmc *pmc,
200147191Sjkoshy    struct pmc_process *pp);
201145256Sjkoshy
202145256Sjkoshy/*
203145256Sjkoshy * Kernel tunables and sysctl(8) interface.
204145256Sjkoshy */
205145256Sjkoshy
206145256SjkoshySYSCTL_NODE(_kern, OID_AUTO, hwpmc, CTLFLAG_RW, 0, "HWPMC parameters");
207145256Sjkoshy
208153110Sru#ifdef	DEBUG
209147191Sjkoshystruct pmc_debugflags pmc_debugflags = PMC_DEBUG_DEFAULT_FLAGS;
210145256Sjkoshychar	pmc_debugstr[PMC_DEBUG_STRSIZE];
211145256SjkoshyTUNABLE_STR(PMC_SYSCTL_NAME_PREFIX "debugflags", pmc_debugstr,
212145256Sjkoshy    sizeof(pmc_debugstr));
213145256SjkoshySYSCTL_PROC(_kern_hwpmc, OID_AUTO, debugflags,
214145256Sjkoshy    CTLTYPE_STRING|CTLFLAG_RW|CTLFLAG_TUN,
215145256Sjkoshy    0, 0, pmc_debugflags_sysctl_handler, "A", "debug flags");
216145256Sjkoshy#endif
217145256Sjkoshy
218145256Sjkoshy/*
219147191Sjkoshy * kern.hwpmc.hashrows -- determines the number of rows in the
220145256Sjkoshy * of the hash table used to look up threads
221145256Sjkoshy */
222145256Sjkoshy
223145256Sjkoshystatic int pmc_hashsize = PMC_HASH_SIZE;
224145256SjkoshyTUNABLE_INT(PMC_SYSCTL_NAME_PREFIX "hashsize", &pmc_hashsize);
225145256SjkoshySYSCTL_INT(_kern_hwpmc, OID_AUTO, hashsize, CTLFLAG_TUN|CTLFLAG_RD,
226145256Sjkoshy    &pmc_hashsize, 0, "rows in hash tables");
227145256Sjkoshy
228145256Sjkoshy/*
229147191Sjkoshy * kern.hwpmc.nsamples --- number of PC samples per CPU
230145256Sjkoshy */
231145256Sjkoshy
232147191Sjkoshystatic int pmc_nsamples = PMC_NSAMPLES;
233147191SjkoshyTUNABLE_INT(PMC_SYSCTL_NAME_PREFIX "nsamples", &pmc_nsamples);
234147191SjkoshySYSCTL_INT(_kern_hwpmc, OID_AUTO, nsamples, CTLFLAG_TUN|CTLFLAG_RD,
235147191Sjkoshy    &pmc_nsamples, 0, "number of PC samples per CPU");
236145256Sjkoshy
237145256Sjkoshy/*
238147191Sjkoshy * kern.hwpmc.mtxpoolsize -- number of mutexes in the mutex pool.
239145256Sjkoshy */
240145256Sjkoshy
241145256Sjkoshystatic int pmc_mtxpool_size = PMC_MTXPOOL_SIZE;
242145256SjkoshyTUNABLE_INT(PMC_SYSCTL_NAME_PREFIX "mtxpoolsize", &pmc_mtxpool_size);
243145256SjkoshySYSCTL_INT(_kern_hwpmc, OID_AUTO, mtxpoolsize, CTLFLAG_TUN|CTLFLAG_RD,
244145256Sjkoshy    &pmc_mtxpool_size, 0, "size of spin mutex pool");
245145256Sjkoshy
246145256Sjkoshy
247145256Sjkoshy/*
248145256Sjkoshy * security.bsd.unprivileged_syspmcs -- allow non-root processes to
249145256Sjkoshy * allocate system-wide PMCs.
250145256Sjkoshy *
251145256Sjkoshy * Allowing unprivileged processes to allocate system PMCs is convenient
252145256Sjkoshy * if system-wide measurements need to be taken concurrently with other
253145256Sjkoshy * per-process measurements.  This feature is turned off by default.
254145256Sjkoshy */
255145256Sjkoshy
256145256Sjkoshystatic int pmc_unprivileged_syspmcs = 0;
257145256SjkoshyTUNABLE_INT("security.bsd.unprivileged_syspmcs", &pmc_unprivileged_syspmcs);
258145256SjkoshySYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_syspmcs, CTLFLAG_RW,
259145256Sjkoshy    &pmc_unprivileged_syspmcs, 0,
260145256Sjkoshy    "allow unprivileged process to allocate system PMCs");
261145256Sjkoshy
262147191Sjkoshy/*
263147191Sjkoshy * Hash function.  Discard the lower 2 bits of the pointer since
264147191Sjkoshy * these are always zero for our uses.  The hash multiplier is
265147191Sjkoshy * round((2^LONG_BIT) * ((sqrt(5)-1)/2)).
266147191Sjkoshy */
267145256Sjkoshy
268145256Sjkoshy#if	LONG_BIT == 64
269145256Sjkoshy#define	_PMC_HM		11400714819323198486u
270145256Sjkoshy#elif	LONG_BIT == 32
271145256Sjkoshy#define	_PMC_HM		2654435769u
272145256Sjkoshy#else
273145256Sjkoshy#error 	Must know the size of 'long' to compile
274145256Sjkoshy#endif
275145256Sjkoshy
276145256Sjkoshy#define	PMC_HASH_PTR(P,M)	((((unsigned long) (P) >> 2) * _PMC_HM) & (M))
277145256Sjkoshy
278145256Sjkoshy/*
279145256Sjkoshy * Syscall structures
280145256Sjkoshy */
281145256Sjkoshy
282145256Sjkoshy/* The `sysent' for the new syscall */
283145256Sjkoshystatic struct sysent pmc_sysent = {
284145256Sjkoshy	2,			/* sy_narg */
285145256Sjkoshy	pmc_syscall_handler	/* sy_call */
286145256Sjkoshy};
287145256Sjkoshy
288145256Sjkoshystatic struct syscall_module_data pmc_syscall_mod = {
289145256Sjkoshy	load,
290145256Sjkoshy	NULL,
291145256Sjkoshy	&pmc_syscall_num,
292145256Sjkoshy	&pmc_sysent,
293145256Sjkoshy	{ 0, NULL }
294145256Sjkoshy};
295145256Sjkoshy
296145256Sjkoshystatic moduledata_t pmc_mod = {
297145256Sjkoshy	PMC_MODULE_NAME,
298145256Sjkoshy	syscall_module_handler,
299145256Sjkoshy	&pmc_syscall_mod
300145256Sjkoshy};
301145256Sjkoshy
302145256SjkoshyDECLARE_MODULE(pmc, pmc_mod, SI_SUB_SMP, SI_ORDER_ANY);
303145256SjkoshyMODULE_VERSION(pmc, PMC_VERSION);
304145256Sjkoshy
305153110Sru#ifdef	DEBUG
306147191Sjkoshyenum pmc_dbgparse_state {
307147191Sjkoshy	PMCDS_WS,		/* in whitespace */
308147191Sjkoshy	PMCDS_MAJOR,		/* seen a major keyword */
309147191Sjkoshy	PMCDS_MINOR
310147191Sjkoshy};
311147191Sjkoshy
312145256Sjkoshystatic int
313145256Sjkoshypmc_debugflags_parse(char *newstr, char *fence)
314145256Sjkoshy{
315145313Sjkoshy	char c, *p, *q;
316147191Sjkoshy	struct pmc_debugflags *tmpflags;
317147191Sjkoshy	int error, found, *newbits, tmp;
318147191Sjkoshy	size_t kwlen;
319145256Sjkoshy
320147191Sjkoshy	MALLOC(tmpflags, struct pmc_debugflags *, sizeof(*tmpflags),
321147191Sjkoshy	    M_PMC, M_WAITOK|M_ZERO);
322145256Sjkoshy
323145256Sjkoshy	p = newstr;
324147191Sjkoshy	error = 0;
325145256Sjkoshy
326147191Sjkoshy	for (; p < fence && (c = *p); p++) {
327145256Sjkoshy
328147191Sjkoshy		/* skip white space */
329147191Sjkoshy		if (c == ' ' || c == '\t')
330147191Sjkoshy			continue;
331147191Sjkoshy
332147191Sjkoshy		/* look for a keyword followed by "=" */
333147191Sjkoshy		for (q = p; p < fence && (c = *p) && c != '='; p++)
334147191Sjkoshy			;
335147191Sjkoshy		if (c != '=') {
336147191Sjkoshy			error = EINVAL;
337147191Sjkoshy			goto done;
338145256Sjkoshy		}
339145256Sjkoshy
340147191Sjkoshy		kwlen = p - q;
341147191Sjkoshy		newbits = NULL;
342145256Sjkoshy
343147191Sjkoshy		/* lookup flag group name */
344147191Sjkoshy#define	DBG_SET_FLAG_MAJ(S,F)						\
345147191Sjkoshy		if (kwlen == sizeof(S)-1 && strncmp(q, S, kwlen) == 0)	\
346147191Sjkoshy			newbits = &tmpflags->pdb_ ## F;
347145256Sjkoshy
348147191Sjkoshy		DBG_SET_FLAG_MAJ("cpu",		CPU);
349147191Sjkoshy		DBG_SET_FLAG_MAJ("csw",		CSW);
350147191Sjkoshy		DBG_SET_FLAG_MAJ("logging",	LOG);
351147191Sjkoshy		DBG_SET_FLAG_MAJ("module",	MOD);
352147191Sjkoshy		DBG_SET_FLAG_MAJ("md", 		MDP);
353147191Sjkoshy		DBG_SET_FLAG_MAJ("owner",	OWN);
354147191Sjkoshy		DBG_SET_FLAG_MAJ("pmc",		PMC);
355147191Sjkoshy		DBG_SET_FLAG_MAJ("process",	PRC);
356147191Sjkoshy		DBG_SET_FLAG_MAJ("sampling", 	SAM);
357145256Sjkoshy
358147191Sjkoshy		if (newbits == NULL) {
359147191Sjkoshy			error = EINVAL;
360147191Sjkoshy			goto done;
361145256Sjkoshy		}
362145256Sjkoshy
363147191Sjkoshy		p++;		/* skip the '=' */
364145256Sjkoshy
365147191Sjkoshy		/* Now parse the individual flags */
366147191Sjkoshy		tmp = 0;
367147191Sjkoshy	newflag:
368147191Sjkoshy		for (q = p; p < fence && (c = *p); p++)
369147191Sjkoshy			if (c == ' ' || c == '\t' || c == ',')
370147191Sjkoshy				break;
371147191Sjkoshy
372147191Sjkoshy		/* p == fence or c == ws or c == "," or c == 0 */
373147191Sjkoshy
374147191Sjkoshy		if ((kwlen = p - q) == 0) {
375147191Sjkoshy			*newbits = tmp;
376147191Sjkoshy			continue;
377147191Sjkoshy		}
378147191Sjkoshy
379147191Sjkoshy		found = 0;
380147191Sjkoshy#define	DBG_SET_FLAG_MIN(S,F)						\
381147191Sjkoshy		if (kwlen == sizeof(S)-1 && strncmp(q, S, kwlen) == 0)	\
382147191Sjkoshy			tmp |= found = (1 << PMC_DEBUG_MIN_ ## F)
383147191Sjkoshy
384147191Sjkoshy		/* a '*' denotes all possible flags in the group */
385147191Sjkoshy		if (kwlen == 1 && *q == '*')
386147191Sjkoshy			tmp = found = ~0;
387147191Sjkoshy		/* look for individual flag names */
388147191Sjkoshy		DBG_SET_FLAG_MIN("allocaterow", ALR);
389147191Sjkoshy		DBG_SET_FLAG_MIN("allocate",	ALL);
390147191Sjkoshy		DBG_SET_FLAG_MIN("attach",	ATT);
391147191Sjkoshy		DBG_SET_FLAG_MIN("bind",	BND);
392147191Sjkoshy		DBG_SET_FLAG_MIN("config",	CFG);
393147191Sjkoshy		DBG_SET_FLAG_MIN("exec",	EXC);
394147191Sjkoshy		DBG_SET_FLAG_MIN("exit",	EXT);
395147191Sjkoshy		DBG_SET_FLAG_MIN("find",	FND);
396147191Sjkoshy		DBG_SET_FLAG_MIN("flush",	FLS);
397147191Sjkoshy		DBG_SET_FLAG_MIN("fork",	FRK);
398147191Sjkoshy		DBG_SET_FLAG_MIN("getbuf",	GTB);
399147191Sjkoshy		DBG_SET_FLAG_MIN("hook",	PMH);
400147191Sjkoshy		DBG_SET_FLAG_MIN("init",	INI);
401147191Sjkoshy		DBG_SET_FLAG_MIN("intr",	INT);
402147191Sjkoshy		DBG_SET_FLAG_MIN("linktarget",	TLK);
403147191Sjkoshy		DBG_SET_FLAG_MIN("mayberemove", OMR);
404147191Sjkoshy		DBG_SET_FLAG_MIN("ops",		OPS);
405147191Sjkoshy		DBG_SET_FLAG_MIN("read",	REA);
406147191Sjkoshy		DBG_SET_FLAG_MIN("register",	REG);
407147191Sjkoshy		DBG_SET_FLAG_MIN("release",	REL);
408147191Sjkoshy		DBG_SET_FLAG_MIN("remove",	ORM);
409147191Sjkoshy		DBG_SET_FLAG_MIN("sample",	SAM);
410147191Sjkoshy		DBG_SET_FLAG_MIN("scheduleio",	SIO);
411147191Sjkoshy		DBG_SET_FLAG_MIN("select",	SEL);
412147191Sjkoshy		DBG_SET_FLAG_MIN("signal",	SIG);
413147191Sjkoshy		DBG_SET_FLAG_MIN("swi",		SWI);
414147191Sjkoshy		DBG_SET_FLAG_MIN("swo",		SWO);
415147191Sjkoshy		DBG_SET_FLAG_MIN("start",	STA);
416147191Sjkoshy		DBG_SET_FLAG_MIN("stop",	STO);
417147191Sjkoshy		DBG_SET_FLAG_MIN("syscall",	PMS);
418147191Sjkoshy		DBG_SET_FLAG_MIN("unlinktarget", TUL);
419147191Sjkoshy		DBG_SET_FLAG_MIN("write",	WRI);
420147191Sjkoshy		if (found == 0) {
421147191Sjkoshy			/* unrecognized flag name */
422147191Sjkoshy			error = EINVAL;
423147191Sjkoshy			goto done;
424147191Sjkoshy		}
425147191Sjkoshy
426147191Sjkoshy		if (c == 0 || c == ' ' || c == '\t') {	/* end of flag group */
427147191Sjkoshy			*newbits = tmp;
428147191Sjkoshy			continue;
429147191Sjkoshy		}
430147191Sjkoshy
431147191Sjkoshy		p++;
432147191Sjkoshy		goto newflag;
433145256Sjkoshy	}
434145256Sjkoshy
435147191Sjkoshy	/* save the new flag set */
436147191Sjkoshy	bcopy(tmpflags, &pmc_debugflags, sizeof(pmc_debugflags));
437145256Sjkoshy
438147191Sjkoshy done:
439147191Sjkoshy	FREE(tmpflags, M_PMC);
440147191Sjkoshy	return error;
441145256Sjkoshy}
442145256Sjkoshy
443145256Sjkoshystatic int
444145256Sjkoshypmc_debugflags_sysctl_handler(SYSCTL_HANDLER_ARGS)
445145256Sjkoshy{
446145256Sjkoshy	char *fence, *newstr;
447145256Sjkoshy	int error;
448145256Sjkoshy	unsigned int n;
449145256Sjkoshy
450145256Sjkoshy	(void) arg1; (void) arg2; /* unused parameters */
451145256Sjkoshy
452145256Sjkoshy	n = sizeof(pmc_debugstr);
453145256Sjkoshy	MALLOC(newstr, char *, n, M_PMC, M_ZERO|M_WAITOK);
454147191Sjkoshy	(void) strlcpy(newstr, pmc_debugstr, n);
455145256Sjkoshy
456145256Sjkoshy	error = sysctl_handle_string(oidp, newstr, n, req);
457145256Sjkoshy
458145256Sjkoshy	/* if there is a new string, parse and copy it */
459145256Sjkoshy	if (error == 0 && req->newptr != NULL) {
460147191Sjkoshy		fence = newstr + (n < req->newlen ? n : req->newlen + 1);
461145256Sjkoshy		if ((error = pmc_debugflags_parse(newstr, fence)) == 0)
462145256Sjkoshy			(void) strlcpy(pmc_debugstr, newstr,
463145256Sjkoshy			    sizeof(pmc_debugstr));
464145256Sjkoshy	}
465145256Sjkoshy
466145256Sjkoshy	FREE(newstr, M_PMC);
467145256Sjkoshy
468145256Sjkoshy	return error;
469145256Sjkoshy}
470145256Sjkoshy#endif
471145256Sjkoshy
472145256Sjkoshy/*
473145256Sjkoshy * Concurrency Control
474145256Sjkoshy *
475145256Sjkoshy * The driver manages the following data structures:
476145256Sjkoshy *
477145256Sjkoshy *   - target process descriptors, one per target process
478145256Sjkoshy *   - owner process descriptors (and attached lists), one per owner process
479145256Sjkoshy *   - lookup hash tables for owner and target processes
480145256Sjkoshy *   - PMC descriptors (and attached lists)
481145256Sjkoshy *   - per-cpu hardware state
482145256Sjkoshy *   - the 'hook' variable through which the kernel calls into
483145256Sjkoshy *     this module
484145256Sjkoshy *   - the machine hardware state (managed by the MD layer)
485145256Sjkoshy *
486145256Sjkoshy * These data structures are accessed from:
487145256Sjkoshy *
488145256Sjkoshy * - thread context-switch code
489145256Sjkoshy * - interrupt handlers (possibly on multiple cpus)
490145256Sjkoshy * - kernel threads on multiple cpus running on behalf of user
491145256Sjkoshy *   processes doing system calls
492145256Sjkoshy * - this driver's private kernel threads
493145256Sjkoshy *
494145256Sjkoshy * = Locks and Locking strategy =
495145256Sjkoshy *
496145256Sjkoshy * The driver uses four locking strategies for its operation:
497145256Sjkoshy *
498145256Sjkoshy * - There is a 'global' SX lock "pmc_sx" that is used to protect
499145256Sjkoshy *   the its 'meta-data'.
500145256Sjkoshy *
501145256Sjkoshy *   Calls into the module (via syscall() or by the kernel) start with
502145256Sjkoshy *   this lock being held in exclusive mode.  Depending on the requested
503145256Sjkoshy *   operation, the lock may be downgraded to 'shared' mode to allow
504145256Sjkoshy *   more concurrent readers into the module.
505145256Sjkoshy *
506145256Sjkoshy *   This SX lock is held in exclusive mode for any operations that
507145256Sjkoshy *   modify the linkages between the driver's internal data structures.
508145256Sjkoshy *
509145256Sjkoshy *   The 'pmc_hook' function pointer is also protected by this lock.
510145256Sjkoshy *   It is only examined with the sx lock held in exclusive mode.  The
511145256Sjkoshy *   kernel module is allowed to be unloaded only with the sx lock
512145256Sjkoshy *   held in exclusive mode.  In normal syscall handling, after
513145256Sjkoshy *   acquiring the pmc_sx lock we first check that 'pmc_hook' is
514145256Sjkoshy *   non-null before proceeding.  This prevents races between the
515145256Sjkoshy *   thread unloading the module and other threads seeking to use the
516145256Sjkoshy *   module.
517145256Sjkoshy *
518145256Sjkoshy * - Lookups of target process structures and owner process structures
519145256Sjkoshy *   cannot use the global "pmc_sx" SX lock because these lookups need
520145256Sjkoshy *   to happen during context switches and in other critical sections
521145256Sjkoshy *   where sleeping is not allowed.  We protect these lookup tables
522145256Sjkoshy *   with their own private spin-mutexes, "pmc_processhash_mtx" and
523145256Sjkoshy *   "pmc_ownerhash_mtx".  These are 'leaf' mutexes, in that no other
524145256Sjkoshy *   lock is acquired with these locks held.
525145256Sjkoshy *
526145256Sjkoshy * - Interrupt handlers work in a lock free manner.  At interrupt
527145256Sjkoshy *   time, handlers look at the PMC pointer (phw->phw_pmc) configured
528145256Sjkoshy *   when the PMC was started.  If this pointer is NULL, the interrupt
529145256Sjkoshy *   is ignored after updating driver statistics.  We ensure that this
530145256Sjkoshy *   pointer is set (using an atomic operation if necessary) before the
531145256Sjkoshy *   PMC hardware is started.  Conversely, this pointer is unset atomically
532145256Sjkoshy *   only after the PMC hardware is stopped.
533145256Sjkoshy *
534145256Sjkoshy *   We ensure that everything needed for the operation of an
535145256Sjkoshy *   interrupt handler is available without it needing to acquire any
536145256Sjkoshy *   locks.  We also ensure that a PMC's software state is destroyed only
537145256Sjkoshy *   after the PMC is taken off hardware (on all CPUs).
538145256Sjkoshy *
539145256Sjkoshy * - Context-switch handling with process-private PMCs needs more
540145256Sjkoshy *   care.
541145256Sjkoshy *
542145256Sjkoshy *   A given process may be the target of multiple PMCs.  For example,
543145256Sjkoshy *   PMCATTACH and PMCDETACH may be requested by a process on one CPU
544145256Sjkoshy *   while the target process is running on another.  A PMC could also
545145256Sjkoshy *   be getting released because its owner is exiting.  We tackle
546145256Sjkoshy *   these situations in the following manner:
547145256Sjkoshy *
548145256Sjkoshy *   - each target process structure 'pmc_process' has an array
549145256Sjkoshy *     of 'struct pmc *' pointers, one for each hardware PMC.
550145256Sjkoshy *
551145256Sjkoshy *   - At context switch IN time, each "target" PMC in RUNNING state
552145256Sjkoshy *     gets started on hardware and a pointer to each PMC is copied into
553145256Sjkoshy *     the per-cpu phw array.  The 'runcount' for the PMC is
554145256Sjkoshy *     incremented.
555145256Sjkoshy *
556145256Sjkoshy *   - At context switch OUT time, all process-virtual PMCs are stopped
557145256Sjkoshy *     on hardware.  The saved value is added to the PMCs value field
558145256Sjkoshy *     only if the PMC is in a non-deleted state (the PMCs state could
559145256Sjkoshy *     have changed during the current time slice).
560145256Sjkoshy *
561145256Sjkoshy *     Note that since in-between a switch IN on a processor and a switch
562145256Sjkoshy *     OUT, the PMC could have been released on another CPU.  Therefore
563145256Sjkoshy *     context switch OUT always looks at the hardware state to turn
564145256Sjkoshy *     OFF PMCs and will update a PMC's saved value only if reachable
565145256Sjkoshy *     from the target process record.
566145256Sjkoshy *
567145256Sjkoshy *   - OP PMCRELEASE could be called on a PMC at any time (the PMC could
568145256Sjkoshy *     be attached to many processes at the time of the call and could
569145256Sjkoshy *     be active on multiple CPUs).
570145256Sjkoshy *
571145256Sjkoshy *     We prevent further scheduling of the PMC by marking it as in
572145256Sjkoshy *     state 'DELETED'.  If the runcount of the PMC is non-zero then
573145256Sjkoshy *     this PMC is currently running on a CPU somewhere.  The thread
574145256Sjkoshy *     doing the PMCRELEASE operation waits by repeatedly doing an
575145256Sjkoshy *     tsleep() till the runcount comes to zero.
576145256Sjkoshy *
577145256Sjkoshy */
578145256Sjkoshy
579145256Sjkoshy/*
580145256Sjkoshy * save the cpu binding of the current kthread
581145256Sjkoshy */
582145256Sjkoshy
583145256Sjkoshystatic void
584145256Sjkoshypmc_save_cpu_binding(struct pmc_binding *pb)
585145256Sjkoshy{
586145256Sjkoshy	PMCDBG(CPU,BND,2, "%s", "save-cpu");
587145256Sjkoshy	mtx_lock_spin(&sched_lock);
588145256Sjkoshy	pb->pb_bound = sched_is_bound(curthread);
589145256Sjkoshy	pb->pb_cpu   = curthread->td_oncpu;
590145256Sjkoshy	mtx_unlock_spin(&sched_lock);
591145256Sjkoshy	PMCDBG(CPU,BND,2, "save-cpu cpu=%d", pb->pb_cpu);
592145256Sjkoshy}
593145256Sjkoshy
594145256Sjkoshy/*
595145256Sjkoshy * restore the cpu binding of the current thread
596145256Sjkoshy */
597145256Sjkoshy
598145256Sjkoshystatic void
599145256Sjkoshypmc_restore_cpu_binding(struct pmc_binding *pb)
600145256Sjkoshy{
601145256Sjkoshy	PMCDBG(CPU,BND,2, "restore-cpu curcpu=%d restore=%d",
602145256Sjkoshy	    curthread->td_oncpu, pb->pb_cpu);
603145256Sjkoshy	mtx_lock_spin(&sched_lock);
604145256Sjkoshy	if (pb->pb_bound)
605145256Sjkoshy		sched_bind(curthread, pb->pb_cpu);
606145256Sjkoshy	else
607145256Sjkoshy		sched_unbind(curthread);
608145256Sjkoshy	mtx_unlock_spin(&sched_lock);
609145256Sjkoshy	PMCDBG(CPU,BND,2, "%s", "restore-cpu done");
610145256Sjkoshy}
611145256Sjkoshy
612145256Sjkoshy/*
613145256Sjkoshy * move execution over the specified cpu and bind it there.
614145256Sjkoshy */
615145256Sjkoshy
616145256Sjkoshystatic void
617145256Sjkoshypmc_select_cpu(int cpu)
618145256Sjkoshy{
619145256Sjkoshy	KASSERT(cpu >= 0 && cpu < mp_ncpus,
620145256Sjkoshy	    ("[pmc,%d] bad cpu number %d", __LINE__, cpu));
621145256Sjkoshy
622145256Sjkoshy	/* never move to a disabled CPU */
623145256Sjkoshy	KASSERT(pmc_cpu_is_disabled(cpu) == 0, ("[pmc,%d] selecting "
624145256Sjkoshy	    "disabled CPU %d", __LINE__, cpu));
625145256Sjkoshy
626145256Sjkoshy	PMCDBG(CPU,SEL,2, "select-cpu cpu=%d", cpu);
627145256Sjkoshy	mtx_lock_spin(&sched_lock);
628145256Sjkoshy	sched_bind(curthread, cpu);
629145256Sjkoshy	mtx_unlock_spin(&sched_lock);
630145256Sjkoshy
631145256Sjkoshy	KASSERT(curthread->td_oncpu == cpu,
632145256Sjkoshy	    ("[pmc,%d] CPU not bound [cpu=%d, curr=%d]", __LINE__,
633145256Sjkoshy		cpu, curthread->td_oncpu));
634145256Sjkoshy
635145256Sjkoshy	PMCDBG(CPU,SEL,2, "select-cpu cpu=%d ok", cpu);
636145256Sjkoshy}
637145256Sjkoshy
638145256Sjkoshy/*
639145774Sjkoshy * Force a context switch.
640145774Sjkoshy *
641145774Sjkoshy * We do this by tsleep'ing for 1 tick -- invoking mi_switch() is not
642145774Sjkoshy * guaranteed to force a context switch.
643145774Sjkoshy */
644145774Sjkoshy
645145774Sjkoshystatic void
646145774Sjkoshypmc_force_context_switch(void)
647145774Sjkoshy{
648145774Sjkoshy
649157815Sjhb	(void) tsleep((void *) pmc_force_context_switch, 0, "pmcctx", 1);
650145774Sjkoshy}
651145774Sjkoshy
652145774Sjkoshy/*
653147191Sjkoshy * Get the file name for an executable.  This is a simple wrapper
654147191Sjkoshy * around vn_fullpath(9).
655145256Sjkoshy */
656145256Sjkoshy
657147191Sjkoshystatic void
658147708Sjkoshypmc_getfilename(struct vnode *v, char **fullpath, char **freepath)
659145256Sjkoshy{
660145256Sjkoshy	struct thread *td;
661145256Sjkoshy
662147191Sjkoshy	td = curthread;
663147191Sjkoshy	*fullpath = "unknown";
664147191Sjkoshy	*freepath = NULL;
665148088Sjkoshy	vn_lock(v, LK_CANRECURSE | LK_EXCLUSIVE | LK_RETRY, td);
666147708Sjkoshy	vn_fullpath(td, v, fullpath, freepath);
667147708Sjkoshy	VOP_UNLOCK(v, 0, td);
668145256Sjkoshy}
669145256Sjkoshy
670145256Sjkoshy/*
671145256Sjkoshy * remove an process owning PMCs
672145256Sjkoshy */
673145256Sjkoshy
674145256Sjkoshyvoid
675145256Sjkoshypmc_remove_owner(struct pmc_owner *po)
676145256Sjkoshy{
677147191Sjkoshy	struct pmc *pm, *tmp;
678145256Sjkoshy
679145256Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
680145256Sjkoshy
681145256Sjkoshy	PMCDBG(OWN,ORM,1, "remove-owner po=%p", po);
682145256Sjkoshy
683145256Sjkoshy	/* Remove descriptor from the owner hash table */
684145256Sjkoshy	LIST_REMOVE(po, po_next);
685145256Sjkoshy
686147191Sjkoshy	/* release all owned PMC descriptors */
687147191Sjkoshy	LIST_FOREACH_SAFE(pm, &po->po_pmcs, pm_next, tmp) {
688147191Sjkoshy		PMCDBG(OWN,ORM,2, "pmc=%p", pm);
689147191Sjkoshy		KASSERT(pm->pm_owner == po,
690147191Sjkoshy		    ("[pmc,%d] owner %p != po %p", __LINE__, pm->pm_owner, po));
691145256Sjkoshy
692147191Sjkoshy		pmc_release_pmc_descriptor(pm);	/* will unlink from the list */
693145256Sjkoshy	}
694145256Sjkoshy
695147191Sjkoshy	KASSERT(po->po_sscount == 0,
696147191Sjkoshy	    ("[pmc,%d] SS count not zero", __LINE__));
697145256Sjkoshy	KASSERT(LIST_EMPTY(&po->po_pmcs),
698147191Sjkoshy	    ("[pmc,%d] PMC list not empty", __LINE__));
699145256Sjkoshy
700147191Sjkoshy	/* de-configure the log file if present */
701145774Sjkoshy	if (po->po_flags & PMC_PO_OWNS_LOGFILE)
702147191Sjkoshy		pmclog_deconfigure_log(po);
703145256Sjkoshy}
704145256Sjkoshy
705145256Sjkoshy/*
706145256Sjkoshy * remove an owner process record if all conditions are met.
707145256Sjkoshy */
708145256Sjkoshy
709145256Sjkoshystatic void
710145256Sjkoshypmc_maybe_remove_owner(struct pmc_owner *po)
711145256Sjkoshy{
712145256Sjkoshy
713145256Sjkoshy	PMCDBG(OWN,OMR,1, "maybe-remove-owner po=%p", po);
714145256Sjkoshy
715145256Sjkoshy	/*
716145256Sjkoshy	 * Remove owner record if
717145256Sjkoshy	 * - this process does not own any PMCs
718145256Sjkoshy	 * - this process has not allocated a system-wide sampling buffer
719145256Sjkoshy	 */
720145256Sjkoshy
721145256Sjkoshy	if (LIST_EMPTY(&po->po_pmcs) &&
722145774Sjkoshy	    ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0)) {
723145256Sjkoshy		pmc_remove_owner(po);
724147191Sjkoshy		pmc_destroy_owner_descriptor(po);
725145256Sjkoshy	}
726145256Sjkoshy}
727145256Sjkoshy
728145256Sjkoshy/*
729145256Sjkoshy * Add an association between a target process and a PMC.
730145256Sjkoshy */
731145256Sjkoshy
732145256Sjkoshystatic void
733145256Sjkoshypmc_link_target_process(struct pmc *pm, struct pmc_process *pp)
734145256Sjkoshy{
735145256Sjkoshy	int ri;
736145256Sjkoshy	struct pmc_target *pt;
737145256Sjkoshy
738145256Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
739145256Sjkoshy
740145256Sjkoshy	KASSERT(pm != NULL && pp != NULL,
741145256Sjkoshy	    ("[pmc,%d] Null pm %p or pp %p", __LINE__, pm, pp));
742147191Sjkoshy	KASSERT(PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm)),
743147191Sjkoshy	    ("[pmc,%d] Attaching a non-process-virtual pmc=%p to pid=%d",
744147191Sjkoshy		__LINE__, pm, pp->pp_proc->p_pid));
745145256Sjkoshy	KASSERT(pp->pp_refcnt >= 0 && pp->pp_refcnt < ((int) md->pmd_npmc - 1),
746145256Sjkoshy	    ("[pmc,%d] Illegal reference count %d for process record %p",
747145256Sjkoshy		__LINE__, pp->pp_refcnt, (void *) pp));
748145256Sjkoshy
749145774Sjkoshy	ri = PMC_TO_ROWINDEX(pm);
750145256Sjkoshy
751145256Sjkoshy	PMCDBG(PRC,TLK,1, "link-target pmc=%p ri=%d pmc-process=%p",
752145256Sjkoshy	    pm, ri, pp);
753145256Sjkoshy
754153110Sru#ifdef	DEBUG
755145256Sjkoshy	LIST_FOREACH(pt, &pm->pm_targets, pt_next)
756145256Sjkoshy	    if (pt->pt_process == pp)
757145256Sjkoshy		    KASSERT(0, ("[pmc,%d] pp %p already in pmc %p targets",
758145256Sjkoshy				__LINE__, pp, pm));
759145256Sjkoshy#endif
760145256Sjkoshy
761145256Sjkoshy	MALLOC(pt, struct pmc_target *, sizeof(struct pmc_target),
762145256Sjkoshy	    M_PMC, M_ZERO|M_WAITOK);
763145256Sjkoshy
764145256Sjkoshy	pt->pt_process = pp;
765145256Sjkoshy
766145256Sjkoshy	LIST_INSERT_HEAD(&pm->pm_targets, pt, pt_next);
767145256Sjkoshy
768148067Sjhb	atomic_store_rel_ptr((uintptr_t *)&pp->pp_pmcs[ri].pp_pmc,
769148067Sjhb	    (uintptr_t)pm);
770145256Sjkoshy
771145615Sjkoshy	if (pm->pm_owner->po_owner == pp->pp_proc)
772145774Sjkoshy		pm->pm_flags |= PMC_F_ATTACHED_TO_OWNER;
773145615Sjkoshy
774147191Sjkoshy	/*
775147191Sjkoshy	 * Initialize the per-process values at this row index.
776147191Sjkoshy	 */
777147191Sjkoshy	pp->pp_pmcs[ri].pp_pmcval = PMC_TO_MODE(pm) == PMC_MODE_TS ?
778147191Sjkoshy	    pm->pm_sc.pm_reloadcount : 0;
779147191Sjkoshy
780145256Sjkoshy	pp->pp_refcnt++;
781145256Sjkoshy
782145256Sjkoshy}
783145256Sjkoshy
784145256Sjkoshy/*
785145256Sjkoshy * Removes the association between a target process and a PMC.
786145256Sjkoshy */
787145256Sjkoshy
788145256Sjkoshystatic void
789145256Sjkoshypmc_unlink_target_process(struct pmc *pm, struct pmc_process *pp)
790145256Sjkoshy{
791145256Sjkoshy	int ri;
792147191Sjkoshy	struct proc *p;
793145256Sjkoshy	struct pmc_target *ptgt;
794145256Sjkoshy
795145256Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
796145256Sjkoshy
797145256Sjkoshy	KASSERT(pm != NULL && pp != NULL,
798145256Sjkoshy	    ("[pmc,%d] Null pm %p or pp %p", __LINE__, pm, pp));
799145256Sjkoshy
800145256Sjkoshy	KASSERT(pp->pp_refcnt >= 1 && pp->pp_refcnt < (int) md->pmd_npmc,
801145256Sjkoshy	    ("[pmc,%d] Illegal ref count %d on process record %p",
802145256Sjkoshy		__LINE__, pp->pp_refcnt, (void *) pp));
803145256Sjkoshy
804145774Sjkoshy	ri = PMC_TO_ROWINDEX(pm);
805145256Sjkoshy
806145256Sjkoshy	PMCDBG(PRC,TUL,1, "unlink-target pmc=%p ri=%d pmc-process=%p",
807145256Sjkoshy	    pm, ri, pp);
808145256Sjkoshy
809145256Sjkoshy	KASSERT(pp->pp_pmcs[ri].pp_pmc == pm,
810145256Sjkoshy	    ("[pmc,%d] PMC ri %d mismatch pmc %p pp->[ri] %p", __LINE__,
811145256Sjkoshy		ri, pm, pp->pp_pmcs[ri].pp_pmc));
812145256Sjkoshy
813145256Sjkoshy	pp->pp_pmcs[ri].pp_pmc = NULL;
814145256Sjkoshy	pp->pp_pmcs[ri].pp_pmcval = (pmc_value_t) 0;
815145256Sjkoshy
816145774Sjkoshy	/* Remove owner-specific flags */
817145774Sjkoshy	if (pm->pm_owner->po_owner == pp->pp_proc) {
818145774Sjkoshy		pp->pp_flags &= ~PMC_PP_ENABLE_MSR_ACCESS;
819145774Sjkoshy		pm->pm_flags &= ~PMC_F_ATTACHED_TO_OWNER;
820145774Sjkoshy	}
821145615Sjkoshy
822145256Sjkoshy	pp->pp_refcnt--;
823145256Sjkoshy
824145256Sjkoshy	/* Remove the target process from the PMC structure */
825145256Sjkoshy	LIST_FOREACH(ptgt, &pm->pm_targets, pt_next)
826145256Sjkoshy		if (ptgt->pt_process == pp)
827145256Sjkoshy			break;
828145256Sjkoshy
829145256Sjkoshy	KASSERT(ptgt != NULL, ("[pmc,%d] process %p (pp: %p) not found "
830145256Sjkoshy		    "in pmc %p", __LINE__, pp->pp_proc, pp, pm));
831145256Sjkoshy
832145256Sjkoshy	LIST_REMOVE(ptgt, pt_next);
833145256Sjkoshy	FREE(ptgt, M_PMC);
834145256Sjkoshy
835147191Sjkoshy	/* if the PMC now lacks targets, send the owner a SIGIO */
836147191Sjkoshy	if (LIST_EMPTY(&pm->pm_targets)) {
837147191Sjkoshy		p = pm->pm_owner->po_owner;
838147191Sjkoshy		PROC_LOCK(p);
839147191Sjkoshy		psignal(p, SIGIO);
840147191Sjkoshy		PROC_UNLOCK(p);
841145256Sjkoshy
842147191Sjkoshy		PMCDBG(PRC,SIG,2, "signalling proc=%p signal=%d", p,
843147191Sjkoshy		    SIGIO);
844145256Sjkoshy	}
845145256Sjkoshy}
846145256Sjkoshy
847145256Sjkoshy/*
848145256Sjkoshy * Check if PMC 'pm' may be attached to target process 't'.
849145256Sjkoshy */
850145256Sjkoshy
851145256Sjkoshystatic int
852145256Sjkoshypmc_can_attach(struct pmc *pm, struct proc *t)
853145256Sjkoshy{
854145256Sjkoshy	struct proc *o;		/* pmc owner */
855145256Sjkoshy	struct ucred *oc, *tc;	/* owner, target credentials */
856145256Sjkoshy	int decline_attach, i;
857145256Sjkoshy
858145256Sjkoshy	/*
859145256Sjkoshy	 * A PMC's owner can always attach that PMC to itself.
860145256Sjkoshy	 */
861145256Sjkoshy
862145256Sjkoshy	if ((o = pm->pm_owner->po_owner) == t)
863145256Sjkoshy		return 0;
864145256Sjkoshy
865145256Sjkoshy	PROC_LOCK(o);
866145256Sjkoshy	oc = o->p_ucred;
867145256Sjkoshy	crhold(oc);
868145256Sjkoshy	PROC_UNLOCK(o);
869145256Sjkoshy
870145256Sjkoshy	PROC_LOCK(t);
871145256Sjkoshy	tc = t->p_ucred;
872145256Sjkoshy	crhold(tc);
873145256Sjkoshy	PROC_UNLOCK(t);
874145256Sjkoshy
875145256Sjkoshy	/*
876145256Sjkoshy	 * The effective uid of the PMC owner should match at least one
877145256Sjkoshy	 * of the {effective,real,saved} uids of the target process.
878145256Sjkoshy	 */
879145256Sjkoshy
880145256Sjkoshy	decline_attach = oc->cr_uid != tc->cr_uid &&
881145256Sjkoshy	    oc->cr_uid != tc->cr_svuid &&
882145256Sjkoshy	    oc->cr_uid != tc->cr_ruid;
883145256Sjkoshy
884145256Sjkoshy	/*
885145256Sjkoshy	 * Every one of the target's group ids, must be in the owner's
886145256Sjkoshy	 * group list.
887145256Sjkoshy	 */
888145256Sjkoshy	for (i = 0; !decline_attach && i < tc->cr_ngroups; i++)
889145256Sjkoshy		decline_attach = !groupmember(tc->cr_groups[i], oc);
890145256Sjkoshy
891145256Sjkoshy	/* check the read and saved gids too */
892145256Sjkoshy	if (decline_attach == 0)
893145256Sjkoshy		decline_attach = !groupmember(tc->cr_rgid, oc) ||
894145256Sjkoshy		    !groupmember(tc->cr_svgid, oc);
895145256Sjkoshy
896145256Sjkoshy	crfree(tc);
897145256Sjkoshy	crfree(oc);
898145256Sjkoshy
899145256Sjkoshy	return !decline_attach;
900145256Sjkoshy}
901145256Sjkoshy
902145256Sjkoshy/*
903145256Sjkoshy * Attach a process to a PMC.
904145256Sjkoshy */
905145256Sjkoshy
906145256Sjkoshystatic int
907145256Sjkoshypmc_attach_one_process(struct proc *p, struct pmc *pm)
908145256Sjkoshy{
909145256Sjkoshy	int ri;
910147191Sjkoshy	char *fullpath, *freepath;
911145256Sjkoshy	struct pmc_process	*pp;
912145256Sjkoshy
913145256Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
914145256Sjkoshy
915145256Sjkoshy	PMCDBG(PRC,ATT,2, "attach-one pm=%p ri=%d proc=%p (%d, %s)", pm,
916145774Sjkoshy	    PMC_TO_ROWINDEX(pm), p, p->p_pid, p->p_comm);
917145256Sjkoshy
918145256Sjkoshy	/*
919145256Sjkoshy	 * Locate the process descriptor corresponding to process 'p',
920145256Sjkoshy	 * allocating space as needed.
921145256Sjkoshy	 *
922145256Sjkoshy	 * Verify that rowindex 'pm_rowindex' is free in the process
923145256Sjkoshy	 * descriptor.
924145256Sjkoshy	 *
925145256Sjkoshy	 * If not, allocate space for a descriptor and link the
926145256Sjkoshy	 * process descriptor and PMC.
927145256Sjkoshy	 */
928145774Sjkoshy	ri = PMC_TO_ROWINDEX(pm);
929145256Sjkoshy
930145256Sjkoshy	if ((pp = pmc_find_process_descriptor(p, PMC_FLAG_ALLOCATE)) == NULL)
931145256Sjkoshy		return ENOMEM;
932145256Sjkoshy
933145256Sjkoshy	if (pp->pp_pmcs[ri].pp_pmc == pm) /* already present at slot [ri] */
934145256Sjkoshy		return EEXIST;
935145256Sjkoshy
936145256Sjkoshy	if (pp->pp_pmcs[ri].pp_pmc != NULL)
937145256Sjkoshy		return EBUSY;
938145256Sjkoshy
939145256Sjkoshy	pmc_link_target_process(pm, pp);
940145256Sjkoshy
941147191Sjkoshy	if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)) &&
942147191Sjkoshy	    (pm->pm_flags & PMC_F_ATTACHED_TO_OWNER) == 0)
943147191Sjkoshy		pm->pm_flags |= PMC_F_NEEDS_LOGFILE;
944147191Sjkoshy
945147191Sjkoshy	pm->pm_flags |= PMC_F_ATTACH_DONE; /* mark as attached */
946147191Sjkoshy
947147191Sjkoshy	/* issue an attach event to a configured log file */
948147191Sjkoshy	if (pm->pm_owner->po_flags & PMC_PO_OWNS_LOGFILE) {
949147708Sjkoshy		pmc_getfilename(p->p_textvp, &fullpath, &freepath);
950147191Sjkoshy		pmclog_process_pmcattach(pm, p->p_pid, fullpath);
951147191Sjkoshy		if (freepath)
952147191Sjkoshy			FREE(freepath, M_TEMP);
953147191Sjkoshy	}
954145256Sjkoshy	/* mark process as using HWPMCs */
955145256Sjkoshy	PROC_LOCK(p);
956145256Sjkoshy	p->p_flag |= P_HWPMC;
957145256Sjkoshy	PROC_UNLOCK(p);
958145256Sjkoshy
959145256Sjkoshy	return 0;
960145256Sjkoshy}
961145256Sjkoshy
962145256Sjkoshy/*
963145256Sjkoshy * Attach a process and optionally its children
964145256Sjkoshy */
965145256Sjkoshy
966145256Sjkoshystatic int
967145256Sjkoshypmc_attach_process(struct proc *p, struct pmc *pm)
968145256Sjkoshy{
969145256Sjkoshy	int error;
970145256Sjkoshy	struct proc *top;
971145256Sjkoshy
972145256Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
973145256Sjkoshy
974145256Sjkoshy	PMCDBG(PRC,ATT,1, "attach pm=%p ri=%d proc=%p (%d, %s)", pm,
975145774Sjkoshy	    PMC_TO_ROWINDEX(pm), p, p->p_pid, p->p_comm);
976145256Sjkoshy
977145774Sjkoshy
978145774Sjkoshy	/*
979145774Sjkoshy	 * If this PMC successfully allowed a GETMSR operation
980145774Sjkoshy	 * in the past, disallow further ATTACHes.
981145774Sjkoshy	 */
982145774Sjkoshy
983145774Sjkoshy	if ((pm->pm_flags & PMC_PP_ENABLE_MSR_ACCESS) != 0)
984145774Sjkoshy		return EPERM;
985145774Sjkoshy
986145256Sjkoshy	if ((pm->pm_flags & PMC_F_DESCENDANTS) == 0)
987145256Sjkoshy		return pmc_attach_one_process(p, pm);
988145256Sjkoshy
989145256Sjkoshy	/*
990145256Sjkoshy	 * Traverse all child processes, attaching them to
991145256Sjkoshy	 * this PMC.
992145256Sjkoshy	 */
993145256Sjkoshy
994145256Sjkoshy	sx_slock(&proctree_lock);
995145256Sjkoshy
996145256Sjkoshy	top = p;
997145256Sjkoshy
998145256Sjkoshy	for (;;) {
999145256Sjkoshy		if ((error = pmc_attach_one_process(p, pm)) != 0)
1000145256Sjkoshy			break;
1001145256Sjkoshy		if (!LIST_EMPTY(&p->p_children))
1002145256Sjkoshy			p = LIST_FIRST(&p->p_children);
1003145256Sjkoshy		else for (;;) {
1004145256Sjkoshy			if (p == top)
1005145256Sjkoshy				goto done;
1006145256Sjkoshy			if (LIST_NEXT(p, p_sibling)) {
1007145256Sjkoshy				p = LIST_NEXT(p, p_sibling);
1008145256Sjkoshy				break;
1009145256Sjkoshy			}
1010145256Sjkoshy			p = p->p_pptr;
1011145256Sjkoshy		}
1012145256Sjkoshy	}
1013145256Sjkoshy
1014145256Sjkoshy	if (error)
1015145256Sjkoshy		(void) pmc_detach_process(top, pm);
1016145256Sjkoshy
1017145256Sjkoshy done:
1018145256Sjkoshy	sx_sunlock(&proctree_lock);
1019145256Sjkoshy	return error;
1020145256Sjkoshy}
1021145256Sjkoshy
1022145256Sjkoshy/*
1023145256Sjkoshy * Detach a process from a PMC.  If there are no other PMCs tracking
1024145256Sjkoshy * this process, remove the process structure from its hash table.  If
1025145256Sjkoshy * 'flags' contains PMC_FLAG_REMOVE, then free the process structure.
1026145256Sjkoshy */
1027145256Sjkoshy
1028145256Sjkoshystatic int
1029145256Sjkoshypmc_detach_one_process(struct proc *p, struct pmc *pm, int flags)
1030145256Sjkoshy{
1031145256Sjkoshy	int ri;
1032145256Sjkoshy	struct pmc_process *pp;
1033145256Sjkoshy
1034145256Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
1035145256Sjkoshy
1036145256Sjkoshy	KASSERT(pm != NULL,
1037145256Sjkoshy	    ("[pmc,%d] null pm pointer", __LINE__));
1038145256Sjkoshy
1039145774Sjkoshy	ri = PMC_TO_ROWINDEX(pm);
1040145774Sjkoshy
1041145256Sjkoshy	PMCDBG(PRC,ATT,2, "detach-one pm=%p ri=%d proc=%p (%d, %s) flags=0x%x",
1042145774Sjkoshy	    pm, ri, p, p->p_pid, p->p_comm, flags);
1043145256Sjkoshy
1044145256Sjkoshy	if ((pp = pmc_find_process_descriptor(p, 0)) == NULL)
1045145256Sjkoshy		return ESRCH;
1046145256Sjkoshy
1047145256Sjkoshy	if (pp->pp_pmcs[ri].pp_pmc != pm)
1048145256Sjkoshy		return EINVAL;
1049145256Sjkoshy
1050145256Sjkoshy	pmc_unlink_target_process(pm, pp);
1051145256Sjkoshy
1052147191Sjkoshy	/* Issue a detach entry if a log file is configured */
1053147191Sjkoshy	if (pm->pm_owner->po_flags & PMC_PO_OWNS_LOGFILE)
1054147191Sjkoshy		pmclog_process_pmcdetach(pm, p->p_pid);
1055147191Sjkoshy
1056145256Sjkoshy	/*
1057145256Sjkoshy	 * If there are no PMCs targetting this process, we remove its
1058145256Sjkoshy	 * descriptor from the target hash table and unset the P_HWPMC
1059145256Sjkoshy	 * flag in the struct proc.
1060145256Sjkoshy	 */
1061145256Sjkoshy	KASSERT(pp->pp_refcnt >= 0 && pp->pp_refcnt < (int) md->pmd_npmc,
1062145256Sjkoshy	    ("[pmc,%d] Illegal refcnt %d for process struct %p",
1063145256Sjkoshy		__LINE__, pp->pp_refcnt, pp));
1064145256Sjkoshy
1065145256Sjkoshy	if (pp->pp_refcnt != 0)	/* still a target of some PMC */
1066145256Sjkoshy		return 0;
1067145256Sjkoshy
1068145256Sjkoshy	pmc_remove_process_descriptor(pp);
1069145256Sjkoshy
1070145256Sjkoshy	if (flags & PMC_FLAG_REMOVE)
1071145256Sjkoshy		FREE(pp, M_PMC);
1072145256Sjkoshy
1073145256Sjkoshy	PROC_LOCK(p);
1074145256Sjkoshy	p->p_flag &= ~P_HWPMC;
1075145256Sjkoshy	PROC_UNLOCK(p);
1076145256Sjkoshy
1077145256Sjkoshy	return 0;
1078145256Sjkoshy}
1079145256Sjkoshy
1080145256Sjkoshy/*
1081145256Sjkoshy * Detach a process and optionally its descendants from a PMC.
1082145256Sjkoshy */
1083145256Sjkoshy
1084145256Sjkoshystatic int
1085145256Sjkoshypmc_detach_process(struct proc *p, struct pmc *pm)
1086145256Sjkoshy{
1087145256Sjkoshy	struct proc *top;
1088145256Sjkoshy
1089145256Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
1090145256Sjkoshy
1091145256Sjkoshy	PMCDBG(PRC,ATT,1, "detach pm=%p ri=%d proc=%p (%d, %s)", pm,
1092145774Sjkoshy	    PMC_TO_ROWINDEX(pm), p, p->p_pid, p->p_comm);
1093145256Sjkoshy
1094145256Sjkoshy	if ((pm->pm_flags & PMC_F_DESCENDANTS) == 0)
1095145256Sjkoshy		return pmc_detach_one_process(p, pm, PMC_FLAG_REMOVE);
1096145256Sjkoshy
1097145256Sjkoshy	/*
1098145256Sjkoshy	 * Traverse all children, detaching them from this PMC.  We
1099145256Sjkoshy	 * ignore errors since we could be detaching a PMC from a
1100145256Sjkoshy	 * partially attached proc tree.
1101145256Sjkoshy	 */
1102145256Sjkoshy
1103145256Sjkoshy	sx_slock(&proctree_lock);
1104145256Sjkoshy
1105145256Sjkoshy	top = p;
1106145256Sjkoshy
1107145256Sjkoshy	for (;;) {
1108145256Sjkoshy		(void) pmc_detach_one_process(p, pm, PMC_FLAG_REMOVE);
1109145256Sjkoshy
1110145256Sjkoshy		if (!LIST_EMPTY(&p->p_children))
1111145256Sjkoshy			p = LIST_FIRST(&p->p_children);
1112145256Sjkoshy		else for (;;) {
1113145256Sjkoshy			if (p == top)
1114145256Sjkoshy				goto done;
1115145256Sjkoshy			if (LIST_NEXT(p, p_sibling)) {
1116145256Sjkoshy				p = LIST_NEXT(p, p_sibling);
1117145256Sjkoshy				break;
1118145256Sjkoshy			}
1119145256Sjkoshy			p = p->p_pptr;
1120145256Sjkoshy		}
1121145256Sjkoshy	}
1122145256Sjkoshy
1123145256Sjkoshy done:
1124145256Sjkoshy	sx_sunlock(&proctree_lock);
1125147191Sjkoshy
1126147191Sjkoshy	if (LIST_EMPTY(&pm->pm_targets))
1127147191Sjkoshy		pm->pm_flags &= ~PMC_F_ATTACH_DONE;
1128147191Sjkoshy
1129145256Sjkoshy	return 0;
1130145256Sjkoshy}
1131145256Sjkoshy
1132147191Sjkoshy
1133145256Sjkoshy/*
1134147191Sjkoshy * Thread context switch IN
1135145256Sjkoshy */
1136145256Sjkoshy
1137147191Sjkoshystatic void
1138147191Sjkoshypmc_process_csw_in(struct thread *td)
1139147191Sjkoshy{
1140147191Sjkoshy	int cpu;
1141147191Sjkoshy	unsigned int ri;
1142147191Sjkoshy	struct pmc *pm;
1143147191Sjkoshy	struct proc *p;
1144147191Sjkoshy	struct pmc_cpu *pc;
1145147191Sjkoshy	struct pmc_hw *phw;
1146147191Sjkoshy	struct pmc_process *pp;
1147147191Sjkoshy	pmc_value_t newvalue;
1148145256Sjkoshy
1149147191Sjkoshy	p = td->td_proc;
1150145256Sjkoshy
1151147191Sjkoshy	if ((pp = pmc_find_process_descriptor(p, PMC_FLAG_NONE)) == NULL)
1152147191Sjkoshy		return;
1153145256Sjkoshy
1154147191Sjkoshy	KASSERT(pp->pp_proc == td->td_proc,
1155147191Sjkoshy	    ("[pmc,%d] not my thread state", __LINE__));
1156145256Sjkoshy
1157147191Sjkoshy	critical_enter(); /* no preemption from this point */
1158145256Sjkoshy
1159147191Sjkoshy	cpu = PCPU_GET(cpuid); /* td->td_oncpu is invalid */
1160145256Sjkoshy
1161147191Sjkoshy	PMCDBG(CSW,SWI,1, "cpu=%d proc=%p (%d, %s) pp=%p", cpu, p,
1162147191Sjkoshy	    p->p_pid, p->p_comm, pp);
1163145256Sjkoshy
1164147191Sjkoshy	KASSERT(cpu >= 0 && cpu < mp_ncpus,
1165147191Sjkoshy	    ("[pmc,%d] wierd CPU id %d", __LINE__, cpu));
1166145256Sjkoshy
1167147191Sjkoshy	pc = pmc_pcpu[cpu];
1168145256Sjkoshy
1169147191Sjkoshy	for (ri = 0; ri < md->pmd_npmc; ri++) {
1170145256Sjkoshy
1171147191Sjkoshy		if ((pm = pp->pp_pmcs[ri].pp_pmc) == NULL)
1172147191Sjkoshy			continue;
1173147191Sjkoshy
1174147191Sjkoshy		KASSERT(PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm)),
1175147191Sjkoshy		    ("[pmc,%d] Target PMC in non-virtual mode (%d)",
1176147191Sjkoshy			__LINE__, PMC_TO_MODE(pm)));
1177147191Sjkoshy
1178147191Sjkoshy		KASSERT(PMC_TO_ROWINDEX(pm) == ri,
1179147191Sjkoshy		    ("[pmc,%d] Row index mismatch pmc %d != ri %d",
1180147191Sjkoshy			__LINE__, PMC_TO_ROWINDEX(pm), ri));
1181147191Sjkoshy
1182145256Sjkoshy		/*
1183147191Sjkoshy		 * Only PMCs that are marked as 'RUNNING' need
1184147191Sjkoshy		 * be placed on hardware.
1185145256Sjkoshy		 */
1186145256Sjkoshy
1187147191Sjkoshy		if (pm->pm_state != PMC_STATE_RUNNING)
1188147191Sjkoshy			continue;
1189145256Sjkoshy
1190147191Sjkoshy		/* increment PMC runcount */
1191147191Sjkoshy		atomic_add_rel_32(&pm->pm_runcount, 1);
1192145256Sjkoshy
1193147191Sjkoshy		/* configure the HWPMC we are going to use. */
1194147191Sjkoshy		md->pmd_config_pmc(cpu, ri, pm);
1195145256Sjkoshy
1196147191Sjkoshy		phw = pc->pc_hwpmcs[ri];
1197145256Sjkoshy
1198147191Sjkoshy		KASSERT(phw != NULL,
1199147191Sjkoshy		    ("[pmc,%d] null hw pointer", __LINE__));
1200145256Sjkoshy
1201147191Sjkoshy		KASSERT(phw->phw_pmc == pm,
1202147191Sjkoshy		    ("[pmc,%d] hw->pmc %p != pmc %p", __LINE__,
1203147191Sjkoshy			phw->phw_pmc, pm));
1204145256Sjkoshy
1205147191Sjkoshy		/*
1206147191Sjkoshy		 * Write out saved value and start the PMC.
1207147191Sjkoshy		 *
1208147191Sjkoshy		 * Sampling PMCs use a per-process value, while
1209147191Sjkoshy		 * counting mode PMCs use a per-pmc value that is
1210147191Sjkoshy		 * inherited across descendants.
1211147191Sjkoshy		 */
1212147191Sjkoshy		if (PMC_TO_MODE(pm) == PMC_MODE_TS) {
1213147191Sjkoshy			mtx_pool_lock_spin(pmc_mtxpool, pm);
1214147191Sjkoshy			newvalue = PMC_PCPU_SAVED(cpu,ri) =
1215147191Sjkoshy			    pp->pp_pmcs[ri].pp_pmcval;
1216147191Sjkoshy			mtx_pool_unlock_spin(pmc_mtxpool, pm);
1217147191Sjkoshy		} else {
1218147191Sjkoshy			KASSERT(PMC_TO_MODE(pm) == PMC_MODE_TC,
1219147191Sjkoshy			    ("[pmc,%d] illegal mode=%d", __LINE__,
1220147191Sjkoshy			    PMC_TO_MODE(pm)));
1221147191Sjkoshy			mtx_pool_lock_spin(pmc_mtxpool, pm);
1222147191Sjkoshy			newvalue = PMC_PCPU_SAVED(cpu, ri) =
1223147191Sjkoshy			    pm->pm_gv.pm_savedvalue;
1224147191Sjkoshy			mtx_pool_unlock_spin(pmc_mtxpool, pm);
1225147191Sjkoshy		}
1226145256Sjkoshy
1227147191Sjkoshy		PMCDBG(CSW,SWI,1,"cpu=%d ri=%d new=%jd", cpu, ri, newvalue);
1228145256Sjkoshy
1229147191Sjkoshy		md->pmd_write_pmc(cpu, ri, newvalue);
1230147191Sjkoshy		md->pmd_start_pmc(cpu, ri);
1231147191Sjkoshy	}
1232145256Sjkoshy
1233147191Sjkoshy	/*
1234147191Sjkoshy	 * perform any other architecture/cpu dependent thread
1235147191Sjkoshy	 * switch-in actions.
1236147191Sjkoshy	 */
1237145256Sjkoshy
1238147191Sjkoshy	(void) (*md->pmd_switch_in)(pc, pp);
1239145256Sjkoshy
1240147191Sjkoshy	critical_exit();
1241145256Sjkoshy
1242147191Sjkoshy}
1243145256Sjkoshy
1244147191Sjkoshy/*
1245147191Sjkoshy * Thread context switch OUT.
1246147191Sjkoshy */
1247145256Sjkoshy
1248147191Sjkoshystatic void
1249147191Sjkoshypmc_process_csw_out(struct thread *td)
1250147191Sjkoshy{
1251147191Sjkoshy	int cpu;
1252147191Sjkoshy	enum pmc_mode mode;
1253147191Sjkoshy	unsigned int ri;
1254147191Sjkoshy	struct pmc *pm;
1255147191Sjkoshy	struct proc *p;
1256147191Sjkoshy	struct pmc_cpu *pc;
1257147191Sjkoshy	struct pmc_process *pp;
1258147191Sjkoshy	int64_t tmp;
1259147191Sjkoshy	pmc_value_t newvalue;
1260145256Sjkoshy
1261147191Sjkoshy	/*
1262147191Sjkoshy	 * Locate our process descriptor; this may be NULL if
1263147191Sjkoshy	 * this process is exiting and we have already removed
1264147191Sjkoshy	 * the process from the target process table.
1265147191Sjkoshy	 *
1266147191Sjkoshy	 * Note that due to kernel preemption, multiple
1267147191Sjkoshy	 * context switches may happen while the process is
1268147191Sjkoshy	 * exiting.
1269147191Sjkoshy	 *
1270147191Sjkoshy	 * Note also that if the target process cannot be
1271147191Sjkoshy	 * found we still need to deconfigure any PMCs that
1272147191Sjkoshy	 * are currently running on hardware.
1273147191Sjkoshy	 */
1274145256Sjkoshy
1275147191Sjkoshy	p = td->td_proc;
1276147191Sjkoshy	pp = pmc_find_process_descriptor(p, PMC_FLAG_NONE);
1277145256Sjkoshy
1278147191Sjkoshy	/*
1279147191Sjkoshy	 * save PMCs
1280147191Sjkoshy	 */
1281145256Sjkoshy
1282147191Sjkoshy	critical_enter();
1283145774Sjkoshy
1284147191Sjkoshy	cpu = PCPU_GET(cpuid); /* td->td_oncpu is invalid */
1285145256Sjkoshy
1286147191Sjkoshy	PMCDBG(CSW,SWO,1, "cpu=%d proc=%p (%d, %s) pp=%p", cpu, p,
1287147191Sjkoshy	    p->p_pid, p->p_comm, pp);
1288145615Sjkoshy
1289147191Sjkoshy	KASSERT(cpu >= 0 && cpu < mp_ncpus,
1290147191Sjkoshy	    ("[pmc,%d wierd CPU id %d", __LINE__, cpu));
1291145615Sjkoshy
1292147191Sjkoshy	pc = pmc_pcpu[cpu];
1293145615Sjkoshy
1294147191Sjkoshy	/*
1295147191Sjkoshy	 * When a PMC gets unlinked from a target PMC, it will
1296147191Sjkoshy	 * be removed from the target's pp_pmc[] array.
1297147191Sjkoshy	 *
1298147191Sjkoshy	 * However, on a MP system, the target could have been
1299147191Sjkoshy	 * executing on another CPU at the time of the unlink.
1300147191Sjkoshy	 * So, at context switch OUT time, we need to look at
1301147191Sjkoshy	 * the hardware to determine if a PMC is scheduled on
1302147191Sjkoshy	 * it.
1303147191Sjkoshy	 */
1304145256Sjkoshy
1305147191Sjkoshy	for (ri = 0; ri < md->pmd_npmc; ri++) {
1306145256Sjkoshy
1307147191Sjkoshy		pm = NULL;
1308147191Sjkoshy		(void) (*md->pmd_get_config)(cpu, ri, &pm);
1309145256Sjkoshy
1310147191Sjkoshy		if (pm == NULL)	/* nothing at this row index */
1311147191Sjkoshy			continue;
1312145256Sjkoshy
1313147191Sjkoshy		mode = PMC_TO_MODE(pm);
1314147191Sjkoshy		if (!PMC_IS_VIRTUAL_MODE(mode))
1315147191Sjkoshy			continue; /* not a process virtual PMC */
1316145774Sjkoshy
1317147191Sjkoshy		KASSERT(PMC_TO_ROWINDEX(pm) == ri,
1318147191Sjkoshy		    ("[pmc,%d] ri mismatch pmc(%d) ri(%d)",
1319147191Sjkoshy			__LINE__, PMC_TO_ROWINDEX(pm), ri));
1320145256Sjkoshy
1321147191Sjkoshy		/* Stop hardware if not already stopped */
1322147867Sjkoshy		if (pm->pm_stalled == 0)
1323147191Sjkoshy			md->pmd_stop_pmc(cpu, ri);
1324147191Sjkoshy
1325147191Sjkoshy		/* reduce this PMC's runcount */
1326147191Sjkoshy		atomic_subtract_rel_32(&pm->pm_runcount, 1);
1327147191Sjkoshy
1328145256Sjkoshy		/*
1329147191Sjkoshy		 * If this PMC is associated with this process,
1330147191Sjkoshy		 * save the reading.
1331145256Sjkoshy		 */
1332145256Sjkoshy
1333147191Sjkoshy		if (pp != NULL && pp->pp_pmcs[ri].pp_pmc != NULL) {
1334147191Sjkoshy
1335147191Sjkoshy			KASSERT(pm == pp->pp_pmcs[ri].pp_pmc,
1336147191Sjkoshy			    ("[pmc,%d] pm %p != pp_pmcs[%d] %p", __LINE__,
1337147191Sjkoshy				pm, ri, pp->pp_pmcs[ri].pp_pmc));
1338147191Sjkoshy
1339147191Sjkoshy			KASSERT(pp->pp_refcnt > 0,
1340147191Sjkoshy			    ("[pmc,%d] pp refcnt = %d", __LINE__,
1341147191Sjkoshy				pp->pp_refcnt));
1342147191Sjkoshy
1343147191Sjkoshy			md->pmd_read_pmc(cpu, ri, &newvalue);
1344147191Sjkoshy
1345147191Sjkoshy			tmp = newvalue - PMC_PCPU_SAVED(cpu,ri);
1346147191Sjkoshy
1347147191Sjkoshy			PMCDBG(CSW,SWI,1,"cpu=%d ri=%d tmp=%jd", cpu, ri,
1348147191Sjkoshy			    tmp);
1349147191Sjkoshy
1350147191Sjkoshy			if (mode == PMC_MODE_TS) {
1351147191Sjkoshy
1352147191Sjkoshy				/*
1353147191Sjkoshy				 * For sampling process-virtual PMCs,
1354147191Sjkoshy				 * we expect the count to be
1355147191Sjkoshy				 * decreasing as the 'value'
1356147191Sjkoshy				 * programmed into the PMC is the
1357147191Sjkoshy				 * number of events to be seen till
1358147191Sjkoshy				 * the next sampling interrupt.
1359147191Sjkoshy				 */
1360147191Sjkoshy				if (tmp < 0)
1361147191Sjkoshy					tmp += pm->pm_sc.pm_reloadcount;
1362147191Sjkoshy				mtx_pool_lock_spin(pmc_mtxpool, pm);
1363147191Sjkoshy				pp->pp_pmcs[ri].pp_pmcval -= tmp;
1364147191Sjkoshy				if ((int64_t) pp->pp_pmcs[ri].pp_pmcval < 0)
1365147191Sjkoshy					pp->pp_pmcs[ri].pp_pmcval +=
1366147191Sjkoshy					    pm->pm_sc.pm_reloadcount;
1367147191Sjkoshy				mtx_pool_unlock_spin(pmc_mtxpool, pm);
1368147191Sjkoshy
1369147191Sjkoshy			} else {
1370147191Sjkoshy
1371147191Sjkoshy				/*
1372147191Sjkoshy				 * For counting process-virtual PMCs,
1373147191Sjkoshy				 * we expect the count to be
1374147191Sjkoshy				 * increasing monotonically, modulo a 64
1375147191Sjkoshy				 * bit wraparound.
1376147191Sjkoshy				 */
1377147191Sjkoshy				KASSERT((int64_t) tmp >= 0,
1378147191Sjkoshy				    ("[pmc,%d] negative increment cpu=%d "
1379147191Sjkoshy				     "ri=%d newvalue=%jx saved=%jx "
1380147191Sjkoshy				     "incr=%jx", __LINE__, cpu, ri,
1381147191Sjkoshy				     newvalue, PMC_PCPU_SAVED(cpu,ri), tmp));
1382147191Sjkoshy
1383147191Sjkoshy				mtx_pool_lock_spin(pmc_mtxpool, pm);
1384147191Sjkoshy				pm->pm_gv.pm_savedvalue += tmp;
1385147191Sjkoshy				pp->pp_pmcs[ri].pp_pmcval += tmp;
1386147191Sjkoshy				mtx_pool_unlock_spin(pmc_mtxpool, pm);
1387147191Sjkoshy
1388147191Sjkoshy				if (pm->pm_flags & PMC_F_LOG_PROCCSW)
1389147191Sjkoshy					pmclog_process_proccsw(pm, pp, tmp);
1390147191Sjkoshy			}
1391145256Sjkoshy		}
1392145256Sjkoshy
1393147191Sjkoshy		/* mark hardware as free */
1394147191Sjkoshy		md->pmd_config_pmc(cpu, ri, NULL);
1395145256Sjkoshy	}
1396145256Sjkoshy
1397145256Sjkoshy	/*
1398147191Sjkoshy	 * perform any other architecture/cpu dependent thread
1399147191Sjkoshy	 * switch out functions.
1400147191Sjkoshy	 */
1401147191Sjkoshy
1402147191Sjkoshy	(void) (*md->pmd_switch_out)(pc, pp);
1403147191Sjkoshy
1404147191Sjkoshy	critical_exit();
1405147191Sjkoshy}
1406147191Sjkoshy
1407147191Sjkoshy/*
1408157144Sjkoshy * Log a KLD operation.
1409157144Sjkoshy */
1410157144Sjkoshy
1411157144Sjkoshystatic void
1412157144Sjkoshypmc_process_kld_load(struct pmckern_map_in *pkm)
1413157144Sjkoshy{
1414157144Sjkoshy	struct pmc_owner *po;
1415157144Sjkoshy
1416157144Sjkoshy	sx_assert(&pmc_sx, SX_LOCKED);
1417157144Sjkoshy
1418157144Sjkoshy	/*
1419157144Sjkoshy	 * Notify owners of system sampling PMCs about KLD operations.
1420157144Sjkoshy	 */
1421157144Sjkoshy
1422157144Sjkoshy	LIST_FOREACH(po, &pmc_ss_owners, po_ssnext)
1423157144Sjkoshy	    if (po->po_flags & PMC_PO_OWNS_LOGFILE)
1424157144Sjkoshy	    	pmclog_process_map_in(po, (pid_t) -1, pkm->pm_address,
1425157144Sjkoshy		    (char *) pkm->pm_file);
1426157144Sjkoshy
1427157144Sjkoshy	/*
1428157144Sjkoshy	 * TODO: Notify owners of (all) process-sampling PMCs too.
1429157144Sjkoshy	 */
1430157144Sjkoshy
1431157144Sjkoshy	return;
1432157144Sjkoshy}
1433157144Sjkoshy
1434157144Sjkoshystatic void
1435157144Sjkoshypmc_process_kld_unload(struct pmckern_map_out *pkm)
1436157144Sjkoshy{
1437157144Sjkoshy	struct pmc_owner *po;
1438157144Sjkoshy
1439157144Sjkoshy	sx_assert(&pmc_sx, SX_LOCKED);
1440157144Sjkoshy
1441157144Sjkoshy	LIST_FOREACH(po, &pmc_ss_owners, po_ssnext)
1442157144Sjkoshy	    if (po->po_flags & PMC_PO_OWNS_LOGFILE)
1443157144Sjkoshy		pmclog_process_map_out(po, (pid_t) -1,
1444157144Sjkoshy		    pkm->pm_address, pkm->pm_address + pkm->pm_size);
1445157144Sjkoshy
1446157144Sjkoshy	/*
1447157144Sjkoshy	 * TODO: Notify owners of process-sampling PMCs.
1448157144Sjkoshy	 */
1449157144Sjkoshy}
1450157144Sjkoshy
1451157144Sjkoshy/*
1452157144Sjkoshy * A mapping change for a process.
1453157144Sjkoshy */
1454157144Sjkoshy
1455157144Sjkoshystatic void
1456157144Sjkoshypmc_process_mmap(struct thread *td, struct pmckern_map_in *pkm)
1457157144Sjkoshy{
1458157144Sjkoshy	int ri;
1459157144Sjkoshy	pid_t pid;
1460157144Sjkoshy	char *fullpath, *freepath;
1461157144Sjkoshy	const struct pmc *pm;
1462157144Sjkoshy	struct pmc_owner *po;
1463157144Sjkoshy	const struct pmc_process *pp;
1464157144Sjkoshy
1465157144Sjkoshy	freepath = fullpath = NULL;
1466157144Sjkoshy	pmc_getfilename((struct vnode *) pkm->pm_file, &fullpath, &freepath);
1467157144Sjkoshy
1468157144Sjkoshy	pid = td->td_proc->p_pid;
1469157144Sjkoshy
1470157144Sjkoshy	/* Inform owners of all system-wide sampling PMCs. */
1471157144Sjkoshy	LIST_FOREACH(po, &pmc_ss_owners, po_ssnext)
1472157144Sjkoshy	    if (po->po_flags & PMC_PO_OWNS_LOGFILE)
1473157144Sjkoshy		pmclog_process_map_in(po, pid, pkm->pm_address, fullpath);
1474157144Sjkoshy
1475157144Sjkoshy	if ((pp = pmc_find_process_descriptor(td->td_proc, 0)) == NULL)
1476157144Sjkoshy		goto done;
1477157144Sjkoshy
1478157144Sjkoshy	/*
1479157144Sjkoshy	 * Inform sampling PMC owners tracking this process.
1480157144Sjkoshy	 */
1481157144Sjkoshy	for (ri = 0; ri < md->pmd_npmc; ri++)
1482157144Sjkoshy		if ((pm = pp->pp_pmcs[ri].pp_pmc) != NULL &&
1483157144Sjkoshy		    PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
1484157144Sjkoshy			pmclog_process_map_in(pm->pm_owner,
1485157144Sjkoshy			    pid, pkm->pm_address, fullpath);
1486157144Sjkoshy
1487157144Sjkoshy  done:
1488157144Sjkoshy	if (freepath)
1489157144Sjkoshy		FREE(freepath, M_TEMP);
1490157144Sjkoshy}
1491157144Sjkoshy
1492157144Sjkoshy
1493157144Sjkoshy/*
1494157144Sjkoshy * Log an munmap request.
1495157144Sjkoshy */
1496157144Sjkoshy
1497157144Sjkoshystatic void
1498157144Sjkoshypmc_process_munmap(struct thread *td, struct pmckern_map_out *pkm)
1499157144Sjkoshy{
1500157144Sjkoshy	int ri;
1501157144Sjkoshy	pid_t pid;
1502157144Sjkoshy	struct pmc_owner *po;
1503157144Sjkoshy	const struct pmc *pm;
1504157144Sjkoshy	const struct pmc_process *pp;
1505157144Sjkoshy
1506157144Sjkoshy	pid = td->td_proc->p_pid;
1507157144Sjkoshy
1508157144Sjkoshy	LIST_FOREACH(po, &pmc_ss_owners, po_ssnext)
1509157144Sjkoshy	    if (po->po_flags & PMC_PO_OWNS_LOGFILE)
1510157144Sjkoshy		pmclog_process_map_out(po, pid, pkm->pm_address,
1511157144Sjkoshy		    pkm->pm_address + pkm->pm_size);
1512157144Sjkoshy
1513157144Sjkoshy	if ((pp = pmc_find_process_descriptor(td->td_proc, 0)) == NULL)
1514157144Sjkoshy		return;
1515157144Sjkoshy
1516157144Sjkoshy	for (ri = 0; ri < md->pmd_npmc; ri++)
1517157144Sjkoshy		if ((pm = pp->pp_pmcs[ri].pp_pmc) != NULL &&
1518157144Sjkoshy		    PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
1519157651Sjkoshy			pmclog_process_map_out(pm->pm_owner, pid,
1520157651Sjkoshy			    pkm->pm_address, pkm->pm_address + pkm->pm_size);
1521157144Sjkoshy}
1522157144Sjkoshy
1523157144Sjkoshy/*
1524147191Sjkoshy * The 'hook' invoked from the kernel proper
1525147191Sjkoshy */
1526147191Sjkoshy
1527147191Sjkoshy
1528153110Sru#ifdef	DEBUG
1529147191Sjkoshyconst char *pmc_hooknames[] = {
1530157144Sjkoshy	/* these strings correspond to PMC_FN_* in <sys/pmckern.h> */
1531147191Sjkoshy	"",
1532147191Sjkoshy	"EXEC",
1533147191Sjkoshy	"CSW-IN",
1534147191Sjkoshy	"CSW-OUT",
1535157144Sjkoshy	"SAMPLE",
1536157144Sjkoshy	"KLDLOAD",
1537157144Sjkoshy	"KLDUNLOAD",
1538157144Sjkoshy	"MMAP",
1539157144Sjkoshy	"MUNMAP"
1540147191Sjkoshy};
1541147191Sjkoshy#endif
1542147191Sjkoshy
1543147191Sjkoshystatic int
1544147191Sjkoshypmc_hook_handler(struct thread *td, int function, void *arg)
1545147191Sjkoshy{
1546147191Sjkoshy
1547147191Sjkoshy	PMCDBG(MOD,PMH,1, "hook td=%p func=%d \"%s\" arg=%p", td, function,
1548147191Sjkoshy	    pmc_hooknames[function], arg);
1549147191Sjkoshy
1550147191Sjkoshy	switch (function)
1551147191Sjkoshy	{
1552147191Sjkoshy
1553147191Sjkoshy	/*
1554145256Sjkoshy	 * Process exec()
1555145256Sjkoshy	 */
1556145256Sjkoshy
1557145256Sjkoshy	case PMC_FN_PROCESS_EXEC:
1558145256Sjkoshy	{
1559147191Sjkoshy		char *fullpath, *freepath;
1560145256Sjkoshy		unsigned int ri;
1561147191Sjkoshy		int is_using_hwpmcs;
1562145256Sjkoshy		struct pmc *pm;
1563145256Sjkoshy		struct proc *p;
1564145256Sjkoshy		struct pmc_owner *po;
1565145256Sjkoshy		struct pmc_process *pp;
1566147708Sjkoshy		struct pmckern_procexec *pk;
1567145256Sjkoshy
1568145256Sjkoshy		sx_assert(&pmc_sx, SX_XLOCKED);
1569145256Sjkoshy
1570147191Sjkoshy		p = td->td_proc;
1571147708Sjkoshy		pmc_getfilename(p->p_textvp, &fullpath, &freepath);
1572147191Sjkoshy
1573147708Sjkoshy		pk = (struct pmckern_procexec *) arg;
1574147708Sjkoshy
1575147191Sjkoshy		/* Inform owners of SS mode PMCs of the exec event. */
1576147191Sjkoshy		LIST_FOREACH(po, &pmc_ss_owners, po_ssnext)
1577147191Sjkoshy		    if (po->po_flags & PMC_PO_OWNS_LOGFILE)
1578147708Sjkoshy			    pmclog_process_procexec(po, PMC_ID_INVALID,
1579147708Sjkoshy				p->p_pid, pk->pm_entryaddr, fullpath);
1580147191Sjkoshy
1581147191Sjkoshy		PROC_LOCK(p);
1582147191Sjkoshy		is_using_hwpmcs = p->p_flag & P_HWPMC;
1583147191Sjkoshy		PROC_UNLOCK(p);
1584147191Sjkoshy
1585147191Sjkoshy		if (!is_using_hwpmcs) {
1586147191Sjkoshy			if (freepath)
1587147191Sjkoshy				FREE(freepath, M_TEMP);
1588147191Sjkoshy			break;
1589147191Sjkoshy		}
1590147191Sjkoshy
1591145256Sjkoshy		/*
1592145256Sjkoshy		 * PMCs are not inherited across an exec():  remove any
1593145256Sjkoshy		 * PMCs that this process is the owner of.
1594145256Sjkoshy		 */
1595145256Sjkoshy
1596145256Sjkoshy		if ((po = pmc_find_owner_descriptor(p)) != NULL) {
1597145256Sjkoshy			pmc_remove_owner(po);
1598147191Sjkoshy			pmc_destroy_owner_descriptor(po);
1599145256Sjkoshy		}
1600145256Sjkoshy
1601145256Sjkoshy		/*
1602154483Sjkoshy		 * If the process being exec'ed is not the target of any
1603154483Sjkoshy		 * PMC, we are done.
1604145256Sjkoshy		 */
1605154483Sjkoshy		if ((pp = pmc_find_process_descriptor(p, 0)) == NULL) {
1606154483Sjkoshy			if (freepath)
1607154483Sjkoshy				FREE(freepath, M_TEMP);
1608145256Sjkoshy			break;
1609154483Sjkoshy		}
1610145256Sjkoshy
1611147191Sjkoshy		/*
1612147191Sjkoshy		 * Log the exec event to all monitoring owners.  Skip
1613147191Sjkoshy		 * owners who have already recieved the event because
1614154483Sjkoshy		 * they had system sampling PMCs active.
1615147191Sjkoshy		 */
1616147191Sjkoshy		for (ri = 0; ri < md->pmd_npmc; ri++)
1617147191Sjkoshy			if ((pm = pp->pp_pmcs[ri].pp_pmc) != NULL) {
1618147191Sjkoshy				po = pm->pm_owner;
1619147191Sjkoshy				if (po->po_sscount == 0 &&
1620147191Sjkoshy				    po->po_flags & PMC_PO_OWNS_LOGFILE)
1621147708Sjkoshy					pmclog_process_procexec(po, pm->pm_id,
1622147708Sjkoshy					    p->p_pid, pk->pm_entryaddr,
1623147191Sjkoshy					    fullpath);
1624147191Sjkoshy			}
1625147191Sjkoshy
1626147191Sjkoshy		if (freepath)
1627147191Sjkoshy			FREE(freepath, M_TEMP);
1628147191Sjkoshy
1629145256Sjkoshy
1630145256Sjkoshy		PMCDBG(PRC,EXC,1, "exec proc=%p (%d, %s) cred-changed=%d",
1631147708Sjkoshy		    p, p->p_pid, p->p_comm, pk->pm_credentialschanged);
1632145256Sjkoshy
1633147708Sjkoshy		if (pk->pm_credentialschanged == 0) /* no change */
1634145256Sjkoshy			break;
1635145256Sjkoshy
1636145256Sjkoshy		/*
1637145256Sjkoshy		 * If the newly exec()'ed process has a different credential
1638145256Sjkoshy		 * than before, allow it to be the target of a PMC only if
1639145256Sjkoshy		 * the PMC's owner has sufficient priviledge.
1640145256Sjkoshy		 */
1641145256Sjkoshy
1642145256Sjkoshy		for (ri = 0; ri < md->pmd_npmc; ri++)
1643145256Sjkoshy			if ((pm = pp->pp_pmcs[ri].pp_pmc) != NULL)
1644145256Sjkoshy				if (pmc_can_attach(pm, td->td_proc) != 0)
1645145256Sjkoshy					pmc_detach_one_process(td->td_proc,
1646145256Sjkoshy					    pm, PMC_FLAG_NONE);
1647145256Sjkoshy
1648145256Sjkoshy		KASSERT(pp->pp_refcnt >= 0 && pp->pp_refcnt < (int) md->pmd_npmc,
1649145256Sjkoshy		    ("[pmc,%d] Illegal ref count %d on pp %p", __LINE__,
1650145256Sjkoshy			pp->pp_refcnt, pp));
1651145256Sjkoshy
1652145256Sjkoshy		/*
1653145256Sjkoshy		 * If this process is no longer the target of any
1654145256Sjkoshy		 * PMCs, we can remove the process entry and free
1655145256Sjkoshy		 * up space.
1656145256Sjkoshy		 */
1657145256Sjkoshy
1658145256Sjkoshy		if (pp->pp_refcnt == 0) {
1659145256Sjkoshy			pmc_remove_process_descriptor(pp);
1660145256Sjkoshy			FREE(pp, M_PMC);
1661147191Sjkoshy			break;
1662145256Sjkoshy		}
1663145256Sjkoshy
1664145256Sjkoshy	}
1665145256Sjkoshy	break;
1666145256Sjkoshy
1667145256Sjkoshy	case PMC_FN_CSW_IN:
1668147191Sjkoshy		pmc_process_csw_in(td);
1669147191Sjkoshy		break;
1670145256Sjkoshy
1671147191Sjkoshy	case PMC_FN_CSW_OUT:
1672147191Sjkoshy		pmc_process_csw_out(td);
1673147191Sjkoshy		break;
1674145256Sjkoshy
1675145256Sjkoshy	/*
1676147191Sjkoshy	 * Process accumulated PC samples.
1677147191Sjkoshy	 *
1678147191Sjkoshy	 * This function is expected to be called by hardclock() for
1679147191Sjkoshy	 * each CPU that has accumulated PC samples.
1680147191Sjkoshy	 *
1681147191Sjkoshy	 * This function is to be executed on the CPU whose samples
1682147191Sjkoshy	 * are being processed.
1683145256Sjkoshy	 */
1684147191Sjkoshy	case PMC_FN_DO_SAMPLES:
1685145256Sjkoshy
1686145256Sjkoshy		/*
1687147191Sjkoshy		 * Clear the cpu specific bit in the CPU mask before
1688147191Sjkoshy		 * do the rest of the processing.  If the NMI handler
1689147191Sjkoshy		 * gets invoked after the "atomic_clear_int()" call
1690147191Sjkoshy		 * below but before "pmc_process_samples()" gets
1691147191Sjkoshy		 * around to processing the interrupt, then we will
1692147191Sjkoshy		 * come back here at the next hardclock() tick (and
1693147191Sjkoshy		 * may find nothing to do if "pmc_process_samples()"
1694147191Sjkoshy		 * had already processed the interrupt).  We don't
1695147191Sjkoshy		 * lose the interrupt sample.
1696145256Sjkoshy		 */
1697147191Sjkoshy		atomic_clear_int(&pmc_cpumask, (1 << PCPU_GET(cpuid)));
1698147191Sjkoshy		pmc_process_samples(PCPU_GET(cpuid));
1699147191Sjkoshy		break;
1700145256Sjkoshy
1701157144Sjkoshy
1702157144Sjkoshy	case PMC_FN_KLD_LOAD:
1703157144Sjkoshy		sx_assert(&pmc_sx, SX_LOCKED);
1704157144Sjkoshy		pmc_process_kld_load((struct pmckern_map_in *) arg);
1705157144Sjkoshy		break;
1706157144Sjkoshy
1707157144Sjkoshy	case PMC_FN_KLD_UNLOAD:
1708157144Sjkoshy		sx_assert(&pmc_sx, SX_LOCKED);
1709157144Sjkoshy		pmc_process_kld_unload((struct pmckern_map_out *) arg);
1710157144Sjkoshy		break;
1711157144Sjkoshy
1712157144Sjkoshy	case PMC_FN_MMAP:
1713157144Sjkoshy		sx_assert(&pmc_sx, SX_LOCKED);
1714157144Sjkoshy		pmc_process_mmap(td, (struct pmckern_map_in *) arg);
1715157144Sjkoshy		break;
1716157144Sjkoshy
1717157144Sjkoshy	case PMC_FN_MUNMAP:
1718157144Sjkoshy		sx_assert(&pmc_sx, SX_LOCKED);
1719157144Sjkoshy		pmc_process_munmap(td, (struct pmckern_map_out *) arg);
1720157144Sjkoshy		break;
1721157144Sjkoshy
1722145256Sjkoshy	default:
1723153110Sru#ifdef	DEBUG
1724145256Sjkoshy		KASSERT(0, ("[pmc,%d] unknown hook %d\n", __LINE__, function));
1725145256Sjkoshy#endif
1726145256Sjkoshy		break;
1727145256Sjkoshy
1728145256Sjkoshy	}
1729145256Sjkoshy
1730145256Sjkoshy	return 0;
1731145256Sjkoshy}
1732145256Sjkoshy
1733145256Sjkoshy/*
1734145256Sjkoshy * allocate a 'struct pmc_owner' descriptor in the owner hash table.
1735145256Sjkoshy */
1736145256Sjkoshy
1737145256Sjkoshystatic struct pmc_owner *
1738145256Sjkoshypmc_allocate_owner_descriptor(struct proc *p)
1739145256Sjkoshy{
1740145256Sjkoshy	uint32_t hindex;
1741145256Sjkoshy	struct pmc_owner *po;
1742145256Sjkoshy	struct pmc_ownerhash *poh;
1743145256Sjkoshy
1744145256Sjkoshy	hindex = PMC_HASH_PTR(p, pmc_ownerhashmask);
1745145256Sjkoshy	poh = &pmc_ownerhash[hindex];
1746145256Sjkoshy
1747145256Sjkoshy	/* allocate space for N pointers and one descriptor struct */
1748145256Sjkoshy	MALLOC(po, struct pmc_owner *, sizeof(struct pmc_owner),
1749147191Sjkoshy	    M_PMC, M_ZERO|M_WAITOK);
1750145256Sjkoshy
1751147191Sjkoshy	po->po_sscount = po->po_error = po->po_flags = 0;
1752147191Sjkoshy	po->po_file  = NULL;
1753145256Sjkoshy	po->po_owner = p;
1754147191Sjkoshy	po->po_kthread = NULL;
1755145256Sjkoshy	LIST_INIT(&po->po_pmcs);
1756145256Sjkoshy	LIST_INSERT_HEAD(poh, po, po_next); /* insert into hash table */
1757145256Sjkoshy
1758147191Sjkoshy	TAILQ_INIT(&po->po_logbuffers);
1759147191Sjkoshy	mtx_init(&po->po_mtx, "pmc-owner-mtx", "pmc", MTX_SPIN);
1760147191Sjkoshy
1761145256Sjkoshy	PMCDBG(OWN,ALL,1, "allocate-owner proc=%p (%d, %s) pmc-owner=%p",
1762145256Sjkoshy	    p, p->p_pid, p->p_comm, po);
1763145256Sjkoshy
1764145256Sjkoshy	return po;
1765145256Sjkoshy}
1766145256Sjkoshy
1767147191Sjkoshystatic void
1768147191Sjkoshypmc_destroy_owner_descriptor(struct pmc_owner *po)
1769147191Sjkoshy{
1770147191Sjkoshy
1771147191Sjkoshy	PMCDBG(OWN,REL,1, "destroy-owner po=%p proc=%p (%d, %s)",
1772147191Sjkoshy	    po, po->po_owner, po->po_owner->p_pid, po->po_owner->p_comm);
1773147191Sjkoshy
1774147191Sjkoshy	mtx_destroy(&po->po_mtx);
1775147191Sjkoshy	FREE(po, M_PMC);
1776147191Sjkoshy}
1777147191Sjkoshy
1778145256Sjkoshy/*
1779145256Sjkoshy * find the descriptor corresponding to process 'p', adding or removing it
1780145256Sjkoshy * as specified by 'mode'.
1781145256Sjkoshy */
1782145256Sjkoshy
1783145256Sjkoshystatic struct pmc_process *
1784145256Sjkoshypmc_find_process_descriptor(struct proc *p, uint32_t mode)
1785145256Sjkoshy{
1786145256Sjkoshy	uint32_t hindex;
1787145256Sjkoshy	struct pmc_process *pp, *ppnew;
1788145256Sjkoshy	struct pmc_processhash *pph;
1789145256Sjkoshy
1790145256Sjkoshy	hindex = PMC_HASH_PTR(p, pmc_processhashmask);
1791145256Sjkoshy	pph = &pmc_processhash[hindex];
1792145256Sjkoshy
1793145256Sjkoshy	ppnew = NULL;
1794145256Sjkoshy
1795145256Sjkoshy	/*
1796145256Sjkoshy	 * Pre-allocate memory in the FIND_ALLOCATE case since we
1797145256Sjkoshy	 * cannot call malloc(9) once we hold a spin lock.
1798145256Sjkoshy	 */
1799145256Sjkoshy
1800145256Sjkoshy	if (mode & PMC_FLAG_ALLOCATE) {
1801145256Sjkoshy		/* allocate additional space for 'n' pmc pointers */
1802145256Sjkoshy		MALLOC(ppnew, struct pmc_process *,
1803145256Sjkoshy		    sizeof(struct pmc_process) + md->pmd_npmc *
1804145256Sjkoshy		    sizeof(struct pmc_targetstate), M_PMC, M_ZERO|M_WAITOK);
1805145256Sjkoshy	}
1806145256Sjkoshy
1807145256Sjkoshy	mtx_lock_spin(&pmc_processhash_mtx);
1808145256Sjkoshy	LIST_FOREACH(pp, pph, pp_next)
1809145256Sjkoshy	    if (pp->pp_proc == p)
1810145256Sjkoshy		    break;
1811145256Sjkoshy
1812145256Sjkoshy	if ((mode & PMC_FLAG_REMOVE) && pp != NULL)
1813145256Sjkoshy		LIST_REMOVE(pp, pp_next);
1814145256Sjkoshy
1815145256Sjkoshy	if ((mode & PMC_FLAG_ALLOCATE) && pp == NULL &&
1816145256Sjkoshy	    ppnew != NULL) {
1817145256Sjkoshy		ppnew->pp_proc = p;
1818145256Sjkoshy		LIST_INSERT_HEAD(pph, ppnew, pp_next);
1819145256Sjkoshy		pp = ppnew;
1820145256Sjkoshy		ppnew = NULL;
1821145256Sjkoshy	}
1822145256Sjkoshy	mtx_unlock_spin(&pmc_processhash_mtx);
1823145256Sjkoshy
1824145256Sjkoshy	if (pp != NULL && ppnew != NULL)
1825145256Sjkoshy		FREE(ppnew, M_PMC);
1826145256Sjkoshy
1827145256Sjkoshy	return pp;
1828145256Sjkoshy}
1829145256Sjkoshy
1830145256Sjkoshy/*
1831145256Sjkoshy * remove a process descriptor from the process hash table.
1832145256Sjkoshy */
1833145256Sjkoshy
1834145256Sjkoshystatic void
1835145256Sjkoshypmc_remove_process_descriptor(struct pmc_process *pp)
1836145256Sjkoshy{
1837145256Sjkoshy	KASSERT(pp->pp_refcnt == 0,
1838145256Sjkoshy	    ("[pmc,%d] Removing process descriptor %p with count %d",
1839145256Sjkoshy		__LINE__, pp, pp->pp_refcnt));
1840145256Sjkoshy
1841145256Sjkoshy	mtx_lock_spin(&pmc_processhash_mtx);
1842145256Sjkoshy	LIST_REMOVE(pp, pp_next);
1843145256Sjkoshy	mtx_unlock_spin(&pmc_processhash_mtx);
1844145256Sjkoshy}
1845145256Sjkoshy
1846145256Sjkoshy
1847145256Sjkoshy/*
1848145256Sjkoshy * find an owner descriptor corresponding to proc 'p'
1849145256Sjkoshy */
1850145256Sjkoshy
1851145256Sjkoshystatic struct pmc_owner *
1852145256Sjkoshypmc_find_owner_descriptor(struct proc *p)
1853145256Sjkoshy{
1854145256Sjkoshy	uint32_t hindex;
1855145256Sjkoshy	struct pmc_owner *po;
1856145256Sjkoshy	struct pmc_ownerhash *poh;
1857145256Sjkoshy
1858145256Sjkoshy	hindex = PMC_HASH_PTR(p, pmc_ownerhashmask);
1859145256Sjkoshy	poh = &pmc_ownerhash[hindex];
1860145256Sjkoshy
1861145256Sjkoshy	po = NULL;
1862145256Sjkoshy	LIST_FOREACH(po, poh, po_next)
1863145256Sjkoshy	    if (po->po_owner == p)
1864145256Sjkoshy		    break;
1865145256Sjkoshy
1866145256Sjkoshy	PMCDBG(OWN,FND,1, "find-owner proc=%p (%d, %s) hindex=0x%x -> "
1867145256Sjkoshy	    "pmc-owner=%p", p, p->p_pid, p->p_comm, hindex, po);
1868145256Sjkoshy
1869145256Sjkoshy	return po;
1870145256Sjkoshy}
1871145256Sjkoshy
1872145256Sjkoshy/*
1873145256Sjkoshy * pmc_allocate_pmc_descriptor
1874145256Sjkoshy *
1875145256Sjkoshy * Allocate a pmc descriptor and initialize its
1876145256Sjkoshy * fields.
1877145256Sjkoshy */
1878145256Sjkoshy
1879145256Sjkoshystatic struct pmc *
1880145256Sjkoshypmc_allocate_pmc_descriptor(void)
1881145256Sjkoshy{
1882145256Sjkoshy	struct pmc *pmc;
1883145256Sjkoshy
1884145256Sjkoshy	MALLOC(pmc, struct pmc *, sizeof(struct pmc), M_PMC, M_ZERO|M_WAITOK);
1885145256Sjkoshy
1886145256Sjkoshy	if (pmc != NULL) {
1887145256Sjkoshy		pmc->pm_owner = NULL;
1888145256Sjkoshy		LIST_INIT(&pmc->pm_targets);
1889145256Sjkoshy	}
1890145256Sjkoshy
1891145256Sjkoshy	PMCDBG(PMC,ALL,1, "allocate-pmc -> pmc=%p", pmc);
1892145256Sjkoshy
1893145256Sjkoshy	return pmc;
1894145256Sjkoshy}
1895145256Sjkoshy
1896145256Sjkoshy/*
1897145256Sjkoshy * Destroy a pmc descriptor.
1898145256Sjkoshy */
1899145256Sjkoshy
1900145256Sjkoshystatic void
1901145256Sjkoshypmc_destroy_pmc_descriptor(struct pmc *pm)
1902145256Sjkoshy{
1903145256Sjkoshy	(void) pm;
1904145256Sjkoshy
1905153110Sru#ifdef	DEBUG
1906145256Sjkoshy	KASSERT(pm->pm_state == PMC_STATE_DELETED ||
1907145256Sjkoshy	    pm->pm_state == PMC_STATE_FREE,
1908145256Sjkoshy	    ("[pmc,%d] destroying non-deleted PMC", __LINE__));
1909145256Sjkoshy	KASSERT(LIST_EMPTY(&pm->pm_targets),
1910145256Sjkoshy	    ("[pmc,%d] destroying pmc with targets", __LINE__));
1911145256Sjkoshy	KASSERT(pm->pm_owner == NULL,
1912145256Sjkoshy	    ("[pmc,%d] destroying pmc attached to an owner", __LINE__));
1913145256Sjkoshy	KASSERT(pm->pm_runcount == 0,
1914145256Sjkoshy	    ("[pmc,%d] pmc has non-zero run count %d", __LINE__,
1915145256Sjkoshy		pm->pm_runcount));
1916145256Sjkoshy#endif
1917145256Sjkoshy}
1918145256Sjkoshy
1919147191Sjkoshystatic void
1920147191Sjkoshypmc_wait_for_pmc_idle(struct pmc *pm)
1921147191Sjkoshy{
1922153110Sru#ifdef	DEBUG
1923147191Sjkoshy	volatile int maxloop;
1924147191Sjkoshy
1925147191Sjkoshy	maxloop = 100 * mp_ncpus;
1926147191Sjkoshy#endif
1927147191Sjkoshy
1928147191Sjkoshy	/*
1929147191Sjkoshy	 * Loop (with a forced context switch) till the PMC's runcount
1930147191Sjkoshy	 * comes down to zero.
1931147191Sjkoshy	 */
1932147191Sjkoshy	while (atomic_load_acq_32(&pm->pm_runcount) > 0) {
1933153110Sru#ifdef	DEBUG
1934147191Sjkoshy		maxloop--;
1935147191Sjkoshy		KASSERT(maxloop > 0,
1936147191Sjkoshy		    ("[pmc,%d] (ri%d, rc%d) waiting too long for "
1937147191Sjkoshy			"pmc to be free", __LINE__,
1938147191Sjkoshy			PMC_TO_ROWINDEX(pm), pm->pm_runcount));
1939147191Sjkoshy#endif
1940147191Sjkoshy		pmc_force_context_switch();
1941147191Sjkoshy	}
1942147191Sjkoshy}
1943147191Sjkoshy
1944145256Sjkoshy/*
1945145256Sjkoshy * This function does the following things:
1946145256Sjkoshy *
1947145256Sjkoshy *  - detaches the PMC from hardware
1948145256Sjkoshy *  - unlinks all target threads that were attached to it
1949145256Sjkoshy *  - removes the PMC from its owner's list
1950145256Sjkoshy *  - destroy's the PMC private mutex
1951145256Sjkoshy *
1952145256Sjkoshy * Once this function completes, the given pmc pointer can be safely
1953145256Sjkoshy * FREE'd by the caller.
1954145256Sjkoshy */
1955145256Sjkoshy
1956145256Sjkoshystatic void
1957145256Sjkoshypmc_release_pmc_descriptor(struct pmc *pm)
1958145256Sjkoshy{
1959145256Sjkoshy	u_int ri, cpu;
1960145774Sjkoshy	enum pmc_mode mode;
1961145256Sjkoshy	struct pmc_hw *phw;
1962147191Sjkoshy	struct pmc_owner *po;
1963145256Sjkoshy	struct pmc_process *pp;
1964145256Sjkoshy	struct pmc_target *ptgt, *tmp;
1965145256Sjkoshy	struct pmc_binding pb;
1966145256Sjkoshy
1967145256Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
1968145256Sjkoshy
1969145256Sjkoshy	KASSERT(pm, ("[pmc,%d] null pmc", __LINE__));
1970145256Sjkoshy
1971145774Sjkoshy	ri   = PMC_TO_ROWINDEX(pm);
1972145774Sjkoshy	mode = PMC_TO_MODE(pm);
1973145256Sjkoshy
1974145256Sjkoshy	PMCDBG(PMC,REL,1, "release-pmc pmc=%p ri=%d mode=%d", pm, ri,
1975145774Sjkoshy	    mode);
1976145256Sjkoshy
1977145256Sjkoshy	/*
1978145256Sjkoshy	 * First, we take the PMC off hardware.
1979145256Sjkoshy	 */
1980145301Simp	cpu = 0;
1981145774Sjkoshy	if (PMC_IS_SYSTEM_MODE(mode)) {
1982145256Sjkoshy
1983145256Sjkoshy		/*
1984145256Sjkoshy		 * A system mode PMC runs on a specific CPU.  Switch
1985145256Sjkoshy		 * to this CPU and turn hardware off.
1986145256Sjkoshy		 */
1987145256Sjkoshy		pmc_save_cpu_binding(&pb);
1988145256Sjkoshy
1989145774Sjkoshy		cpu = PMC_TO_CPU(pm);
1990145256Sjkoshy
1991147191Sjkoshy		pmc_select_cpu(cpu);
1992145256Sjkoshy
1993147191Sjkoshy		/* switch off non-stalled CPUs */
1994147191Sjkoshy		if (pm->pm_state == PMC_STATE_RUNNING &&
1995147867Sjkoshy		    pm->pm_stalled == 0) {
1996145256Sjkoshy
1997145256Sjkoshy			phw = pmc_pcpu[cpu]->pc_hwpmcs[ri];
1998145256Sjkoshy
1999145256Sjkoshy			KASSERT(phw->phw_pmc == pm,
2000145256Sjkoshy			    ("[pmc, %d] pmc ptr ri(%d) hw(%p) pm(%p)",
2001145256Sjkoshy				__LINE__, ri, phw->phw_pmc, pm));
2002145256Sjkoshy			PMCDBG(PMC,REL,2, "stopping cpu=%d ri=%d", cpu, ri);
2003145256Sjkoshy
2004145256Sjkoshy			critical_enter();
2005145256Sjkoshy			md->pmd_stop_pmc(cpu, ri);
2006145256Sjkoshy			critical_exit();
2007145256Sjkoshy		}
2008145256Sjkoshy
2009145256Sjkoshy		PMCDBG(PMC,REL,2, "decfg cpu=%d ri=%d", cpu, ri);
2010145256Sjkoshy
2011145256Sjkoshy		critical_enter();
2012145256Sjkoshy		md->pmd_config_pmc(cpu, ri, NULL);
2013145256Sjkoshy		critical_exit();
2014145256Sjkoshy
2015147191Sjkoshy		/* adjust the global and process count of SS mode PMCs */
2016147191Sjkoshy		if (mode == PMC_MODE_SS && pm->pm_state == PMC_STATE_RUNNING) {
2017147191Sjkoshy			po = pm->pm_owner;
2018147191Sjkoshy			po->po_sscount--;
2019147191Sjkoshy			if (po->po_sscount == 0) {
2020147191Sjkoshy				atomic_subtract_rel_int(&pmc_ss_count, 1);
2021147191Sjkoshy				LIST_REMOVE(po, po_ssnext);
2022147191Sjkoshy			}
2023147191Sjkoshy		}
2024147191Sjkoshy
2025145256Sjkoshy		pm->pm_state = PMC_STATE_DELETED;
2026145256Sjkoshy
2027145256Sjkoshy		pmc_restore_cpu_binding(&pb);
2028145256Sjkoshy
2029147191Sjkoshy		/*
2030147191Sjkoshy		 * We could have references to this PMC structure in
2031147191Sjkoshy		 * the per-cpu sample queues.  Wait for the queue to
2032147191Sjkoshy		 * drain.
2033147191Sjkoshy		 */
2034147191Sjkoshy		pmc_wait_for_pmc_idle(pm);
2035147191Sjkoshy
2036145774Sjkoshy	} else if (PMC_IS_VIRTUAL_MODE(mode)) {
2037145256Sjkoshy
2038145256Sjkoshy		/*
2039145256Sjkoshy		 * A virtual PMC could be running on multiple CPUs at
2040145256Sjkoshy		 * a given instant.
2041145256Sjkoshy		 *
2042145256Sjkoshy		 * By marking its state as DELETED, we ensure that
2043145256Sjkoshy		 * this PMC is never further scheduled on hardware.
2044145256Sjkoshy		 *
2045145256Sjkoshy		 * Then we wait till all CPUs are done with this PMC.
2046145256Sjkoshy		 */
2047145256Sjkoshy		pm->pm_state = PMC_STATE_DELETED;
2048145256Sjkoshy
2049145256Sjkoshy
2050147191Sjkoshy		/* Wait for the PMCs runcount to come to zero. */
2051147191Sjkoshy		pmc_wait_for_pmc_idle(pm);
2052145256Sjkoshy
2053145256Sjkoshy		/*
2054145256Sjkoshy		 * At this point the PMC is off all CPUs and cannot be
2055145256Sjkoshy		 * freshly scheduled onto a CPU.  It is now safe to
2056145256Sjkoshy		 * unlink all targets from this PMC.  If a
2057145256Sjkoshy		 * process-record's refcount falls to zero, we remove
2058145256Sjkoshy		 * it from the hash table.  The module-wide SX lock
2059145256Sjkoshy		 * protects us from races.
2060145256Sjkoshy		 */
2061145256Sjkoshy		LIST_FOREACH_SAFE(ptgt, &pm->pm_targets, pt_next, tmp) {
2062145256Sjkoshy			pp = ptgt->pt_process;
2063145256Sjkoshy			pmc_unlink_target_process(pm, pp); /* frees 'ptgt' */
2064145256Sjkoshy
2065145256Sjkoshy			PMCDBG(PMC,REL,3, "pp->refcnt=%d", pp->pp_refcnt);
2066145256Sjkoshy
2067145256Sjkoshy			/*
2068145256Sjkoshy			 * If the target process record shows that no
2069145256Sjkoshy			 * PMCs are attached to it, reclaim its space.
2070145256Sjkoshy			 */
2071145256Sjkoshy
2072145256Sjkoshy			if (pp->pp_refcnt == 0) {
2073145256Sjkoshy				pmc_remove_process_descriptor(pp);
2074145256Sjkoshy				FREE(pp, M_PMC);
2075145256Sjkoshy			}
2076145256Sjkoshy		}
2077145256Sjkoshy
2078145256Sjkoshy		cpu = curthread->td_oncpu; /* setup cpu for pmd_release() */
2079145256Sjkoshy
2080145256Sjkoshy	}
2081145256Sjkoshy
2082145256Sjkoshy	/*
2083145256Sjkoshy	 * Release any MD resources
2084145256Sjkoshy	 */
2085145256Sjkoshy
2086145256Sjkoshy	(void) md->pmd_release_pmc(cpu, ri, pm);
2087145256Sjkoshy
2088145256Sjkoshy	/*
2089145256Sjkoshy	 * Update row disposition
2090145256Sjkoshy	 */
2091145256Sjkoshy
2092145774Sjkoshy	if (PMC_IS_SYSTEM_MODE(PMC_TO_MODE(pm)))
2093145256Sjkoshy		PMC_UNMARK_ROW_STANDALONE(ri);
2094145256Sjkoshy	else
2095145256Sjkoshy		PMC_UNMARK_ROW_THREAD(ri);
2096145256Sjkoshy
2097145256Sjkoshy	/* unlink from the owner's list */
2098147191Sjkoshy	if (pm->pm_owner) {
2099147191Sjkoshy		LIST_REMOVE(pm, pm_next);
2100147191Sjkoshy		pm->pm_owner = NULL;
2101147191Sjkoshy	}
2102145256Sjkoshy
2103145256Sjkoshy	pmc_destroy_pmc_descriptor(pm);
2104145256Sjkoshy}
2105145256Sjkoshy
2106145256Sjkoshy/*
2107145256Sjkoshy * Register an owner and a pmc.
2108145256Sjkoshy */
2109145256Sjkoshy
2110145256Sjkoshystatic int
2111145256Sjkoshypmc_register_owner(struct proc *p, struct pmc *pmc)
2112145256Sjkoshy{
2113145256Sjkoshy	struct pmc_owner *po;
2114145256Sjkoshy
2115145256Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
2116145256Sjkoshy
2117145774Sjkoshy	if ((po = pmc_find_owner_descriptor(p)) == NULL)
2118147191Sjkoshy		if ((po = pmc_allocate_owner_descriptor(p)) == NULL)
2119145256Sjkoshy			return ENOMEM;
2120145256Sjkoshy
2121145256Sjkoshy	KASSERT(pmc->pm_owner == NULL,
2122145256Sjkoshy	    ("[pmc,%d] attempting to own an initialized PMC", __LINE__));
2123145256Sjkoshy	pmc->pm_owner  = po;
2124145256Sjkoshy
2125147191Sjkoshy	LIST_INSERT_HEAD(&po->po_pmcs, pmc, pm_next);
2126145256Sjkoshy
2127145256Sjkoshy	PROC_LOCK(p);
2128145256Sjkoshy	p->p_flag |= P_HWPMC;
2129145256Sjkoshy	PROC_UNLOCK(p);
2130145256Sjkoshy
2131147191Sjkoshy	if (po->po_flags & PMC_PO_OWNS_LOGFILE)
2132147191Sjkoshy		pmclog_process_pmcallocate(pmc);
2133145256Sjkoshy
2134147191Sjkoshy	PMCDBG(PMC,REG,1, "register-owner pmc-owner=%p pmc=%p",
2135147191Sjkoshy	    po, pmc);
2136147191Sjkoshy
2137145256Sjkoshy	return 0;
2138145256Sjkoshy}
2139145256Sjkoshy
2140145256Sjkoshy/*
2141145256Sjkoshy * Return the current row disposition:
2142145256Sjkoshy * == 0 => FREE
2143145256Sjkoshy *  > 0 => PROCESS MODE
2144145256Sjkoshy *  < 0 => SYSTEM MODE
2145145256Sjkoshy */
2146145256Sjkoshy
2147145256Sjkoshyint
2148145256Sjkoshypmc_getrowdisp(int ri)
2149145256Sjkoshy{
2150145256Sjkoshy	return pmc_pmcdisp[ri];
2151145256Sjkoshy}
2152145256Sjkoshy
2153145256Sjkoshy/*
2154145256Sjkoshy * Check if a PMC at row index 'ri' can be allocated to the current
2155145256Sjkoshy * process.
2156145256Sjkoshy *
2157145256Sjkoshy * Allocation can fail if:
2158145256Sjkoshy *   - the current process is already being profiled by a PMC at index 'ri',
2159145256Sjkoshy *     attached to it via OP_PMCATTACH.
2160145256Sjkoshy *   - the current process has already allocated a PMC at index 'ri'
2161145256Sjkoshy *     via OP_ALLOCATE.
2162145256Sjkoshy */
2163145256Sjkoshy
2164145256Sjkoshystatic int
2165145774Sjkoshypmc_can_allocate_rowindex(struct proc *p, unsigned int ri, int cpu)
2166145256Sjkoshy{
2167145774Sjkoshy	enum pmc_mode mode;
2168145774Sjkoshy	struct pmc *pm;
2169145256Sjkoshy	struct pmc_owner *po;
2170145256Sjkoshy	struct pmc_process *pp;
2171145256Sjkoshy
2172145774Sjkoshy	PMCDBG(PMC,ALR,1, "can-allocate-rowindex proc=%p (%d, %s) ri=%d "
2173145774Sjkoshy	    "cpu=%d", p, p->p_pid, p->p_comm, ri, cpu);
2174145256Sjkoshy
2175145774Sjkoshy	/*
2176145774Sjkoshy	 * We shouldn't have already allocated a process-mode PMC at
2177145774Sjkoshy	 * row index 'ri'.
2178145774Sjkoshy	 *
2179145774Sjkoshy	 * We shouldn't have allocated a system-wide PMC on the same
2180145774Sjkoshy	 * CPU and same RI.
2181145774Sjkoshy	 */
2182145256Sjkoshy	if ((po = pmc_find_owner_descriptor(p)) != NULL)
2183147191Sjkoshy		LIST_FOREACH(pm, &po->po_pmcs, pm_next) {
2184145774Sjkoshy		    if (PMC_TO_ROWINDEX(pm) == ri) {
2185145774Sjkoshy			    mode = PMC_TO_MODE(pm);
2186145774Sjkoshy			    if (PMC_IS_VIRTUAL_MODE(mode))
2187145774Sjkoshy				    return EEXIST;
2188145774Sjkoshy			    if (PMC_IS_SYSTEM_MODE(mode) &&
2189145774Sjkoshy				(int) PMC_TO_CPU(pm) == cpu)
2190145774Sjkoshy				    return EEXIST;
2191145774Sjkoshy		    }
2192145774Sjkoshy	        }
2193145256Sjkoshy
2194145774Sjkoshy	/*
2195145774Sjkoshy	 * We also shouldn't be the target of any PMC at this index
2196145774Sjkoshy	 * since otherwise a PMC_ATTACH to ourselves will fail.
2197145774Sjkoshy	 */
2198145256Sjkoshy	if ((pp = pmc_find_process_descriptor(p, 0)) != NULL)
2199145256Sjkoshy		if (pp->pp_pmcs[ri].pp_pmc)
2200145256Sjkoshy			return EEXIST;
2201145256Sjkoshy
2202145256Sjkoshy	PMCDBG(PMC,ALR,2, "can-allocate-rowindex proc=%p (%d, %s) ri=%d ok",
2203145256Sjkoshy	    p, p->p_pid, p->p_comm, ri);
2204145256Sjkoshy
2205145256Sjkoshy	return 0;
2206145256Sjkoshy}
2207145256Sjkoshy
2208145256Sjkoshy/*
2209145256Sjkoshy * Check if a given PMC at row index 'ri' can be currently used in
2210145256Sjkoshy * mode 'mode'.
2211145256Sjkoshy */
2212145256Sjkoshy
2213145256Sjkoshystatic int
2214145256Sjkoshypmc_can_allocate_row(int ri, enum pmc_mode mode)
2215145256Sjkoshy{
2216145256Sjkoshy	enum pmc_disp	disp;
2217145256Sjkoshy
2218145256Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
2219145256Sjkoshy
2220145256Sjkoshy	PMCDBG(PMC,ALR,1, "can-allocate-row ri=%d mode=%d", ri, mode);
2221145256Sjkoshy
2222145256Sjkoshy	if (PMC_IS_SYSTEM_MODE(mode))
2223145256Sjkoshy		disp = PMC_DISP_STANDALONE;
2224145256Sjkoshy	else
2225145256Sjkoshy		disp = PMC_DISP_THREAD;
2226145256Sjkoshy
2227145256Sjkoshy	/*
2228145256Sjkoshy	 * check disposition for PMC row 'ri':
2229145256Sjkoshy	 *
2230145256Sjkoshy	 * Expected disposition		Row-disposition		Result
2231145256Sjkoshy	 *
2232145256Sjkoshy	 * STANDALONE			STANDALONE or FREE	proceed
2233145256Sjkoshy	 * STANDALONE			THREAD			fail
2234145256Sjkoshy	 * THREAD			THREAD or FREE		proceed
2235145256Sjkoshy	 * THREAD			STANDALONE		fail
2236145256Sjkoshy	 */
2237145256Sjkoshy
2238145256Sjkoshy	if (!PMC_ROW_DISP_IS_FREE(ri) &&
2239145256Sjkoshy	    !(disp == PMC_DISP_THREAD && PMC_ROW_DISP_IS_THREAD(ri)) &&
2240145256Sjkoshy	    !(disp == PMC_DISP_STANDALONE && PMC_ROW_DISP_IS_STANDALONE(ri)))
2241145256Sjkoshy		return EBUSY;
2242145256Sjkoshy
2243145256Sjkoshy	/*
2244145256Sjkoshy	 * All OK
2245145256Sjkoshy	 */
2246145256Sjkoshy
2247145256Sjkoshy	PMCDBG(PMC,ALR,2, "can-allocate-row ri=%d mode=%d ok", ri, mode);
2248145256Sjkoshy
2249145256Sjkoshy	return 0;
2250145256Sjkoshy
2251145256Sjkoshy}
2252145256Sjkoshy
2253145256Sjkoshy/*
2254145774Sjkoshy * Find a PMC descriptor with user handle 'pmcid' for thread 'td'.
2255145256Sjkoshy */
2256145256Sjkoshy
2257145256Sjkoshystatic struct pmc *
2258145256Sjkoshypmc_find_pmc_descriptor_in_process(struct pmc_owner *po, pmc_id_t pmcid)
2259145256Sjkoshy{
2260147191Sjkoshy	struct pmc *pm;
2261145256Sjkoshy
2262145774Sjkoshy	KASSERT(PMC_ID_TO_ROWINDEX(pmcid) < md->pmd_npmc,
2263145774Sjkoshy	    ("[pmc,%d] Illegal pmc index %d (max %d)", __LINE__,
2264145774Sjkoshy		PMC_ID_TO_ROWINDEX(pmcid), md->pmd_npmc));
2265145256Sjkoshy
2266147191Sjkoshy	LIST_FOREACH(pm, &po->po_pmcs, pm_next)
2267147191Sjkoshy	    if (pm->pm_id == pmcid)
2268147191Sjkoshy		    return pm;
2269145256Sjkoshy
2270145256Sjkoshy	return NULL;
2271145256Sjkoshy}
2272145256Sjkoshy
2273145256Sjkoshystatic int
2274145256Sjkoshypmc_find_pmc(pmc_id_t pmcid, struct pmc **pmc)
2275145256Sjkoshy{
2276145256Sjkoshy
2277145256Sjkoshy	struct pmc *pm;
2278145256Sjkoshy	struct pmc_owner *po;
2279145256Sjkoshy
2280145256Sjkoshy	PMCDBG(PMC,FND,1, "find-pmc id=%d", pmcid);
2281145256Sjkoshy
2282145256Sjkoshy	if ((po = pmc_find_owner_descriptor(curthread->td_proc)) == NULL)
2283145256Sjkoshy		return ESRCH;
2284145256Sjkoshy
2285145256Sjkoshy	if ((pm = pmc_find_pmc_descriptor_in_process(po, pmcid)) == NULL)
2286145256Sjkoshy		return EINVAL;
2287145256Sjkoshy
2288145256Sjkoshy	PMCDBG(PMC,FND,2, "find-pmc id=%d -> pmc=%p", pmcid, pm);
2289145256Sjkoshy
2290145256Sjkoshy	*pmc = pm;
2291145256Sjkoshy	return 0;
2292145256Sjkoshy}
2293145256Sjkoshy
2294145256Sjkoshy/*
2295145256Sjkoshy * Start a PMC.
2296145256Sjkoshy */
2297145256Sjkoshy
2298145256Sjkoshystatic int
2299145256Sjkoshypmc_start(struct pmc *pm)
2300145256Sjkoshy{
2301145256Sjkoshy	int error, cpu, ri;
2302145774Sjkoshy	enum pmc_mode mode;
2303147191Sjkoshy	struct pmc_owner *po;
2304145256Sjkoshy	struct pmc_binding pb;
2305145256Sjkoshy
2306145256Sjkoshy	KASSERT(pm != NULL,
2307145256Sjkoshy	    ("[pmc,%d] null pm", __LINE__));
2308145256Sjkoshy
2309145774Sjkoshy	mode = PMC_TO_MODE(pm);
2310145774Sjkoshy	ri   = PMC_TO_ROWINDEX(pm);
2311145774Sjkoshy	error = 0;
2312145256Sjkoshy
2313145774Sjkoshy	PMCDBG(PMC,OPS,1, "start pmc=%p mode=%d ri=%d", pm, mode, ri);
2314145774Sjkoshy
2315147191Sjkoshy	po = pm->pm_owner;
2316145256Sjkoshy
2317145774Sjkoshy	if (PMC_IS_VIRTUAL_MODE(mode)) {
2318145256Sjkoshy
2319145256Sjkoshy		/*
2320147191Sjkoshy		 * If a PMCATTACH has never been done on this PMC,
2321147191Sjkoshy		 * attach it to its owner process.
2322145256Sjkoshy		 */
2323145256Sjkoshy
2324145256Sjkoshy		if (LIST_EMPTY(&pm->pm_targets))
2325147191Sjkoshy			error = (pm->pm_flags & PMC_F_ATTACH_DONE) ? ESRCH :
2326147191Sjkoshy			    pmc_attach_process(po->po_owner, pm);
2327145256Sjkoshy
2328145774Sjkoshy		/*
2329147191Sjkoshy		 * Disallow PMCSTART if a logfile is required but has not
2330147191Sjkoshy		 * been configured yet.
2331145774Sjkoshy		 */
2332145256Sjkoshy
2333147191Sjkoshy		if (error == 0 && (pm->pm_flags & PMC_F_NEEDS_LOGFILE) &&
2334147191Sjkoshy		    (po->po_flags & PMC_PO_OWNS_LOGFILE) == 0)
2335147191Sjkoshy			error = EDOOFUS;
2336147191Sjkoshy
2337145256Sjkoshy		/*
2338147191Sjkoshy		 * If the PMC is attached to its owner, then force a context
2339147191Sjkoshy		 * switch to ensure that the MD state gets set correctly.
2340145256Sjkoshy		 */
2341145256Sjkoshy
2342147191Sjkoshy		if (error == 0) {
2343147191Sjkoshy			pm->pm_state = PMC_STATE_RUNNING;
2344147191Sjkoshy			if (pm->pm_flags & PMC_F_ATTACHED_TO_OWNER)
2345147191Sjkoshy				pmc_force_context_switch();
2346147191Sjkoshy		}
2347147191Sjkoshy
2348145774Sjkoshy		return error;
2349147191Sjkoshy	}
2350145256Sjkoshy
2351147191Sjkoshy
2352147191Sjkoshy	/*
2353147191Sjkoshy	 * A system-wide PMC.
2354147191Sjkoshy	 */
2355147191Sjkoshy
2356147191Sjkoshy	if ((pm->pm_flags & PMC_F_NEEDS_LOGFILE) &&
2357147191Sjkoshy	    (po->po_flags & PMC_PO_OWNS_LOGFILE) == 0)
2358147191Sjkoshy		return EDOOFUS;	/* programming error */
2359147191Sjkoshy
2360147191Sjkoshy	/*
2361147191Sjkoshy	 * Add the owner to the global list if this is a system-wide
2362147191Sjkoshy	 * sampling PMC.
2363147191Sjkoshy	 */
2364147191Sjkoshy
2365147191Sjkoshy	if (mode == PMC_MODE_SS) {
2366147191Sjkoshy		if (po->po_sscount == 0) {
2367147191Sjkoshy			LIST_INSERT_HEAD(&pmc_ss_owners, po, po_ssnext);
2368147191Sjkoshy			atomic_add_rel_int(&pmc_ss_count, 1);
2369147191Sjkoshy			PMCDBG(PMC,OPS,1, "po=%p in global list", po);
2370147191Sjkoshy		}
2371147191Sjkoshy		po->po_sscount++;
2372145256Sjkoshy	}
2373145256Sjkoshy
2374157144Sjkoshy	/* TODO: dump system wide process mappings to the log? */
2375157144Sjkoshy
2376145256Sjkoshy	/*
2377147191Sjkoshy	 * Move to the CPU associated with this
2378145256Sjkoshy	 * PMC, and start the hardware.
2379145256Sjkoshy	 */
2380145256Sjkoshy
2381145256Sjkoshy	pmc_save_cpu_binding(&pb);
2382145256Sjkoshy
2383145774Sjkoshy	cpu = PMC_TO_CPU(pm);
2384145256Sjkoshy
2385145256Sjkoshy	if (pmc_cpu_is_disabled(cpu))
2386145256Sjkoshy		return ENXIO;
2387145256Sjkoshy
2388145256Sjkoshy	pmc_select_cpu(cpu);
2389145256Sjkoshy
2390145256Sjkoshy	/*
2391145256Sjkoshy	 * global PMCs are configured at allocation time
2392145256Sjkoshy	 * so write out the initial value and start the PMC.
2393145256Sjkoshy	 */
2394145256Sjkoshy
2395147191Sjkoshy	pm->pm_state = PMC_STATE_RUNNING;
2396147191Sjkoshy
2397145774Sjkoshy	critical_enter();
2398145256Sjkoshy	if ((error = md->pmd_write_pmc(cpu, ri,
2399145774Sjkoshy		 PMC_IS_SAMPLING_MODE(mode) ?
2400145256Sjkoshy		 pm->pm_sc.pm_reloadcount :
2401145256Sjkoshy		 pm->pm_sc.pm_initial)) == 0)
2402145256Sjkoshy		error = md->pmd_start_pmc(cpu, ri);
2403145774Sjkoshy	critical_exit();
2404145256Sjkoshy
2405145256Sjkoshy	pmc_restore_cpu_binding(&pb);
2406145256Sjkoshy
2407145256Sjkoshy	return error;
2408145256Sjkoshy}
2409145256Sjkoshy
2410145256Sjkoshy/*
2411145256Sjkoshy * Stop a PMC.
2412145256Sjkoshy */
2413145256Sjkoshy
2414145256Sjkoshystatic int
2415145256Sjkoshypmc_stop(struct pmc *pm)
2416145256Sjkoshy{
2417145774Sjkoshy	int cpu, error, ri;
2418147191Sjkoshy	struct pmc_owner *po;
2419145256Sjkoshy	struct pmc_binding pb;
2420145256Sjkoshy
2421145256Sjkoshy	KASSERT(pm != NULL, ("[pmc,%d] null pmc", __LINE__));
2422145256Sjkoshy
2423145774Sjkoshy	PMCDBG(PMC,OPS,1, "stop pmc=%p mode=%d ri=%d", pm,
2424145774Sjkoshy	    PMC_TO_MODE(pm), PMC_TO_ROWINDEX(pm));
2425145256Sjkoshy
2426145256Sjkoshy	pm->pm_state = PMC_STATE_STOPPED;
2427145256Sjkoshy
2428145256Sjkoshy	/*
2429145256Sjkoshy	 * If the PMC is a virtual mode one, changing the state to
2430145256Sjkoshy	 * non-RUNNING is enough to ensure that the PMC never gets
2431145256Sjkoshy	 * scheduled.
2432145256Sjkoshy	 *
2433145256Sjkoshy	 * If this PMC is current running on a CPU, then it will
2434145256Sjkoshy	 * handled correctly at the time its target process is context
2435145256Sjkoshy	 * switched out.
2436145256Sjkoshy	 */
2437145256Sjkoshy
2438145774Sjkoshy	if (PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm)))
2439145256Sjkoshy		return 0;
2440145256Sjkoshy
2441145256Sjkoshy	/*
2442145256Sjkoshy	 * A system-mode PMC.  Move to the CPU associated with
2443145256Sjkoshy	 * this PMC, and stop the hardware.  We update the
2444145256Sjkoshy	 * 'initial count' so that a subsequent PMCSTART will
2445145256Sjkoshy	 * resume counting from the current hardware count.
2446145256Sjkoshy	 */
2447145256Sjkoshy
2448145256Sjkoshy	pmc_save_cpu_binding(&pb);
2449145256Sjkoshy
2450145774Sjkoshy	cpu = PMC_TO_CPU(pm);
2451145256Sjkoshy
2452145774Sjkoshy	KASSERT(cpu >= 0 && cpu < mp_ncpus,
2453145774Sjkoshy	    ("[pmc,%d] illegal cpu=%d", __LINE__, cpu));
2454145774Sjkoshy
2455145256Sjkoshy	if (pmc_cpu_is_disabled(cpu))
2456145256Sjkoshy		return ENXIO;
2457145256Sjkoshy
2458145256Sjkoshy	pmc_select_cpu(cpu);
2459145256Sjkoshy
2460145774Sjkoshy	ri = PMC_TO_ROWINDEX(pm);
2461145256Sjkoshy
2462145774Sjkoshy	critical_enter();
2463145774Sjkoshy	if ((error = md->pmd_stop_pmc(cpu, ri)) == 0)
2464145774Sjkoshy		error = md->pmd_read_pmc(cpu, ri, &pm->pm_sc.pm_initial);
2465145774Sjkoshy	critical_exit();
2466145774Sjkoshy
2467145256Sjkoshy	pmc_restore_cpu_binding(&pb);
2468145256Sjkoshy
2469147191Sjkoshy	po = pm->pm_owner;
2470147191Sjkoshy
2471147191Sjkoshy	/* remove this owner from the global list of SS PMC owners */
2472147191Sjkoshy	if (PMC_TO_MODE(pm) == PMC_MODE_SS) {
2473147191Sjkoshy		po->po_sscount--;
2474147191Sjkoshy		if (po->po_sscount == 0) {
2475147191Sjkoshy			atomic_subtract_rel_int(&pmc_ss_count, 1);
2476147191Sjkoshy			LIST_REMOVE(po, po_ssnext);
2477147191Sjkoshy			PMCDBG(PMC,OPS,2,"po=%p removed from global list", po);
2478147191Sjkoshy		}
2479147191Sjkoshy	}
2480147191Sjkoshy
2481145256Sjkoshy	return error;
2482145256Sjkoshy}
2483145256Sjkoshy
2484145256Sjkoshy
2485153110Sru#ifdef	DEBUG
2486145256Sjkoshystatic const char *pmc_op_to_name[] = {
2487145256Sjkoshy#undef	__PMC_OP
2488145256Sjkoshy#define	__PMC_OP(N, D)	#N ,
2489145256Sjkoshy	__PMC_OPS()
2490145256Sjkoshy	NULL
2491145256Sjkoshy};
2492145256Sjkoshy#endif
2493145256Sjkoshy
2494145256Sjkoshy/*
2495145256Sjkoshy * The syscall interface
2496145256Sjkoshy */
2497145256Sjkoshy
2498145256Sjkoshy#define	PMC_GET_SX_XLOCK(...) do {		\
2499145256Sjkoshy	sx_xlock(&pmc_sx);			\
2500145256Sjkoshy	if (pmc_hook == NULL) {			\
2501145256Sjkoshy		sx_xunlock(&pmc_sx);		\
2502145256Sjkoshy		return __VA_ARGS__;		\
2503145256Sjkoshy	}					\
2504145256Sjkoshy} while (0)
2505145256Sjkoshy
2506145256Sjkoshy#define	PMC_DOWNGRADE_SX() do {			\
2507145256Sjkoshy	sx_downgrade(&pmc_sx);			\
2508145256Sjkoshy	is_sx_downgraded = 1;			\
2509145256Sjkoshy} while (0)
2510145256Sjkoshy
2511145256Sjkoshystatic int
2512145256Sjkoshypmc_syscall_handler(struct thread *td, void *syscall_args)
2513145256Sjkoshy{
2514145256Sjkoshy	int error, is_sx_downgraded, op;
2515145256Sjkoshy	struct pmc_syscall_args *c;
2516145256Sjkoshy	void *arg;
2517145256Sjkoshy
2518145256Sjkoshy	PMC_GET_SX_XLOCK(ENOSYS);
2519145256Sjkoshy
2520147191Sjkoshy	DROP_GIANT();
2521147191Sjkoshy
2522145256Sjkoshy	is_sx_downgraded = 0;
2523145256Sjkoshy
2524145256Sjkoshy	c = (struct pmc_syscall_args *) syscall_args;
2525145256Sjkoshy
2526145256Sjkoshy	op = c->pmop_code;
2527145256Sjkoshy	arg = c->pmop_data;
2528145256Sjkoshy
2529145256Sjkoshy	PMCDBG(MOD,PMS,1, "syscall op=%d \"%s\" arg=%p", op,
2530145256Sjkoshy	    pmc_op_to_name[op], arg);
2531145256Sjkoshy
2532145256Sjkoshy	error = 0;
2533145256Sjkoshy	atomic_add_int(&pmc_stats.pm_syscalls, 1);
2534145256Sjkoshy
2535145256Sjkoshy	switch(op)
2536145256Sjkoshy	{
2537145256Sjkoshy
2538145256Sjkoshy
2539145256Sjkoshy	/*
2540145256Sjkoshy	 * Configure a log file.
2541145256Sjkoshy	 *
2542145256Sjkoshy	 * XXX This OP will be reworked.
2543145256Sjkoshy	 */
2544145256Sjkoshy
2545145256Sjkoshy	case PMC_OP_CONFIGURELOG:
2546145256Sjkoshy	{
2547157144Sjkoshy		struct proc *p;
2548156466Sjkoshy		struct pmc *pm;
2549145256Sjkoshy		struct pmc_owner *po;
2550157144Sjkoshy		struct pmckern_map_in *km, *kmbase;
2551145256Sjkoshy		struct pmc_op_configurelog cl;
2552145256Sjkoshy
2553145256Sjkoshy		sx_assert(&pmc_sx, SX_XLOCKED);
2554145256Sjkoshy
2555145256Sjkoshy		if ((error = copyin(arg, &cl, sizeof(cl))) != 0)
2556145256Sjkoshy			break;
2557145256Sjkoshy
2558145256Sjkoshy		/* mark this process as owning a log file */
2559145256Sjkoshy		p = td->td_proc;
2560145256Sjkoshy		if ((po = pmc_find_owner_descriptor(p)) == NULL)
2561147191Sjkoshy			if ((po = pmc_allocate_owner_descriptor(p)) == NULL) {
2562147191Sjkoshy				error = ENOMEM;
2563147191Sjkoshy				break;
2564147191Sjkoshy			}
2565145256Sjkoshy
2566147191Sjkoshy		/*
2567147191Sjkoshy		 * If a valid fd was passed in, try to configure that,
2568147191Sjkoshy		 * otherwise if 'fd' was less than zero and there was
2569147191Sjkoshy		 * a log file configured, flush its buffers and
2570147191Sjkoshy		 * de-configure it.
2571147191Sjkoshy		 */
2572147191Sjkoshy		if (cl.pm_logfd >= 0)
2573147191Sjkoshy			error = pmclog_configure_log(po, cl.pm_logfd);
2574147191Sjkoshy		else if (po->po_flags & PMC_PO_OWNS_LOGFILE) {
2575147191Sjkoshy			pmclog_process_closelog(po);
2576147191Sjkoshy			error = pmclog_flush(po);
2577156466Sjkoshy			if (error == 0) {
2578156466Sjkoshy				LIST_FOREACH(pm, &po->po_pmcs, pm_next)
2579156834Sjkoshy				    if (pm->pm_flags & PMC_F_NEEDS_LOGFILE &&
2580156834Sjkoshy					pm->pm_state == PMC_STATE_RUNNING)
2581156466Sjkoshy					    pmc_stop(pm);
2582147191Sjkoshy				error = pmclog_deconfigure_log(po);
2583156466Sjkoshy			}
2584147191Sjkoshy		} else
2585147191Sjkoshy			error = EINVAL;
2586157144Sjkoshy
2587157144Sjkoshy		if (error)
2588157144Sjkoshy			break;
2589157144Sjkoshy
2590157144Sjkoshy		/*
2591157144Sjkoshy		 * Log the current set of kernel modules.
2592157144Sjkoshy		 */
2593157144Sjkoshy		kmbase = linker_hwpmc_list_objects();
2594157144Sjkoshy		for (km = kmbase; km->pm_file != NULL; km++) {
2595157144Sjkoshy			PMCDBG(LOG,REG,1,"%s %p", (char *) km->pm_file,
2596157144Sjkoshy			    (void *) km->pm_address);
2597157144Sjkoshy			pmclog_process_map_in(po, (pid_t) -1, km->pm_address,
2598157144Sjkoshy			    km->pm_file);
2599157144Sjkoshy		}
2600157144Sjkoshy		FREE(kmbase, M_LINKER);
2601147191Sjkoshy	}
2602147191Sjkoshy	break;
2603147191Sjkoshy
2604147191Sjkoshy
2605147191Sjkoshy	/*
2606147191Sjkoshy	 * Flush a log file.
2607147191Sjkoshy	 */
2608147191Sjkoshy
2609147191Sjkoshy	case PMC_OP_FLUSHLOG:
2610147191Sjkoshy	{
2611147191Sjkoshy		struct pmc_owner *po;
2612147191Sjkoshy
2613147191Sjkoshy		sx_assert(&pmc_sx, SX_XLOCKED);
2614147191Sjkoshy
2615147191Sjkoshy		if ((po = pmc_find_owner_descriptor(td->td_proc)) == NULL) {
2616147191Sjkoshy			error = EINVAL;
2617145256Sjkoshy			break;
2618147191Sjkoshy		}
2619145256Sjkoshy
2620147191Sjkoshy		error = pmclog_flush(po);
2621145256Sjkoshy	}
2622145256Sjkoshy	break;
2623145256Sjkoshy
2624145256Sjkoshy	/*
2625145256Sjkoshy	 * Retrieve hardware configuration.
2626145256Sjkoshy	 */
2627145256Sjkoshy
2628145256Sjkoshy	case PMC_OP_GETCPUINFO:	/* CPU information */
2629145256Sjkoshy	{
2630145256Sjkoshy		struct pmc_op_getcpuinfo gci;
2631145256Sjkoshy
2632145256Sjkoshy		gci.pm_cputype = md->pmd_cputype;
2633145774Sjkoshy		gci.pm_ncpu    = mp_ncpus;
2634145256Sjkoshy		gci.pm_npmc    = md->pmd_npmc;
2635145256Sjkoshy		gci.pm_nclass  = md->pmd_nclass;
2636145256Sjkoshy		bcopy(md->pmd_classes, &gci.pm_classes,
2637145256Sjkoshy		    sizeof(gci.pm_classes));
2638145256Sjkoshy		error = copyout(&gci, arg, sizeof(gci));
2639145256Sjkoshy	}
2640145256Sjkoshy	break;
2641145256Sjkoshy
2642145256Sjkoshy
2643145256Sjkoshy	/*
2644145256Sjkoshy	 * Get module statistics
2645145256Sjkoshy	 */
2646145256Sjkoshy
2647145256Sjkoshy	case PMC_OP_GETDRIVERSTATS:
2648145256Sjkoshy	{
2649145256Sjkoshy		struct pmc_op_getdriverstats gms;
2650145256Sjkoshy
2651145256Sjkoshy		bcopy(&pmc_stats, &gms, sizeof(gms));
2652145256Sjkoshy		error = copyout(&gms, arg, sizeof(gms));
2653145256Sjkoshy	}
2654145256Sjkoshy	break;
2655145256Sjkoshy
2656145256Sjkoshy
2657145256Sjkoshy	/*
2658145256Sjkoshy	 * Retrieve module version number
2659145256Sjkoshy	 */
2660145256Sjkoshy
2661145256Sjkoshy	case PMC_OP_GETMODULEVERSION:
2662145256Sjkoshy	{
2663147191Sjkoshy		uint32_t cv, modv;
2664147191Sjkoshy
2665147191Sjkoshy		/* retrieve the client's idea of the ABI version */
2666147191Sjkoshy		if ((error = copyin(arg, &cv, sizeof(uint32_t))) != 0)
2667147191Sjkoshy			break;
2668147191Sjkoshy		/* don't service clients newer than our driver */
2669147191Sjkoshy		modv = PMC_VERSION;
2670147191Sjkoshy		if ((cv & 0xFFFF0000) > (modv & 0xFFFF0000)) {
2671147191Sjkoshy			error = EPROGMISMATCH;
2672147191Sjkoshy			break;
2673147191Sjkoshy		}
2674147191Sjkoshy		error = copyout(&modv, arg, sizeof(int));
2675145256Sjkoshy	}
2676145256Sjkoshy	break;
2677145256Sjkoshy
2678145256Sjkoshy
2679145256Sjkoshy	/*
2680145256Sjkoshy	 * Retrieve the state of all the PMCs on a given
2681145256Sjkoshy	 * CPU.
2682145256Sjkoshy	 */
2683145256Sjkoshy
2684145256Sjkoshy	case PMC_OP_GETPMCINFO:
2685145256Sjkoshy	{
2686145256Sjkoshy		uint32_t cpu, n, npmc;
2687145256Sjkoshy		size_t pmcinfo_size;
2688145256Sjkoshy		struct pmc *pm;
2689145256Sjkoshy		struct pmc_info *p, *pmcinfo;
2690145256Sjkoshy		struct pmc_op_getpmcinfo *gpi;
2691145256Sjkoshy		struct pmc_owner *po;
2692145256Sjkoshy		struct pmc_binding pb;
2693145256Sjkoshy
2694145256Sjkoshy		PMC_DOWNGRADE_SX();
2695145256Sjkoshy
2696145256Sjkoshy		gpi = (struct pmc_op_getpmcinfo *) arg;
2697145256Sjkoshy
2698145256Sjkoshy		if ((error = copyin(&gpi->pm_cpu, &cpu, sizeof(cpu))) != 0)
2699145256Sjkoshy			break;
2700145256Sjkoshy
2701145256Sjkoshy		if (cpu >= (unsigned int) mp_ncpus) {
2702145256Sjkoshy			error = EINVAL;
2703145256Sjkoshy			break;
2704145256Sjkoshy		}
2705145256Sjkoshy
2706145256Sjkoshy		if (pmc_cpu_is_disabled(cpu)) {
2707145256Sjkoshy			error = ENXIO;
2708145256Sjkoshy			break;
2709145256Sjkoshy		}
2710145256Sjkoshy
2711145256Sjkoshy		/* switch to CPU 'cpu' */
2712145256Sjkoshy		pmc_save_cpu_binding(&pb);
2713145256Sjkoshy		pmc_select_cpu(cpu);
2714145256Sjkoshy
2715145256Sjkoshy		npmc = md->pmd_npmc;
2716145256Sjkoshy
2717145256Sjkoshy		pmcinfo_size = npmc * sizeof(struct pmc_info);
2718145256Sjkoshy		MALLOC(pmcinfo, struct pmc_info *, pmcinfo_size, M_PMC,
2719145256Sjkoshy		    M_WAITOK);
2720145256Sjkoshy
2721145256Sjkoshy		p = pmcinfo;
2722145256Sjkoshy
2723145256Sjkoshy		for (n = 0; n < md->pmd_npmc; n++, p++) {
2724145256Sjkoshy
2725145256Sjkoshy			if ((error = md->pmd_describe(cpu, n, p, &pm)) != 0)
2726145256Sjkoshy				break;
2727145256Sjkoshy
2728145256Sjkoshy			if (PMC_ROW_DISP_IS_STANDALONE(n))
2729145256Sjkoshy				p->pm_rowdisp = PMC_DISP_STANDALONE;
2730145256Sjkoshy			else if (PMC_ROW_DISP_IS_THREAD(n))
2731145256Sjkoshy				p->pm_rowdisp = PMC_DISP_THREAD;
2732145256Sjkoshy			else
2733145256Sjkoshy				p->pm_rowdisp = PMC_DISP_FREE;
2734145256Sjkoshy
2735145256Sjkoshy			p->pm_ownerpid = -1;
2736145256Sjkoshy
2737145256Sjkoshy			if (pm == NULL)	/* no PMC associated */
2738145256Sjkoshy				continue;
2739145256Sjkoshy
2740145256Sjkoshy			po = pm->pm_owner;
2741145256Sjkoshy
2742145256Sjkoshy			KASSERT(po->po_owner != NULL,
2743145256Sjkoshy			    ("[pmc,%d] pmc_owner had a null proc pointer",
2744145256Sjkoshy				__LINE__));
2745145256Sjkoshy
2746145256Sjkoshy			p->pm_ownerpid = po->po_owner->p_pid;
2747145774Sjkoshy			p->pm_mode     = PMC_TO_MODE(pm);
2748145256Sjkoshy			p->pm_event    = pm->pm_event;
2749145256Sjkoshy			p->pm_flags    = pm->pm_flags;
2750145256Sjkoshy
2751145774Sjkoshy			if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
2752145256Sjkoshy				p->pm_reloadcount =
2753145256Sjkoshy				    pm->pm_sc.pm_reloadcount;
2754145256Sjkoshy		}
2755145256Sjkoshy
2756145256Sjkoshy		pmc_restore_cpu_binding(&pb);
2757145256Sjkoshy
2758145256Sjkoshy		/* now copy out the PMC info collected */
2759145256Sjkoshy		if (error == 0)
2760145256Sjkoshy			error = copyout(pmcinfo, &gpi->pm_pmcs, pmcinfo_size);
2761145256Sjkoshy
2762145256Sjkoshy		FREE(pmcinfo, M_PMC);
2763145256Sjkoshy	}
2764145256Sjkoshy	break;
2765145256Sjkoshy
2766145256Sjkoshy
2767145256Sjkoshy	/*
2768145256Sjkoshy	 * Set the administrative state of a PMC.  I.e. whether
2769145256Sjkoshy	 * the PMC is to be used or not.
2770145256Sjkoshy	 */
2771145256Sjkoshy
2772145256Sjkoshy	case PMC_OP_PMCADMIN:
2773145256Sjkoshy	{
2774145256Sjkoshy		int cpu, ri;
2775145256Sjkoshy		enum pmc_state request;
2776145256Sjkoshy		struct pmc_cpu *pc;
2777145256Sjkoshy		struct pmc_hw *phw;
2778145256Sjkoshy		struct pmc_op_pmcadmin pma;
2779145256Sjkoshy		struct pmc_binding pb;
2780145256Sjkoshy
2781145256Sjkoshy		sx_assert(&pmc_sx, SX_XLOCKED);
2782145256Sjkoshy
2783145256Sjkoshy		KASSERT(td == curthread,
2784145256Sjkoshy		    ("[pmc,%d] td != curthread", __LINE__));
2785145256Sjkoshy
2786164033Srwatson		error = priv_check(td, PRIV_PMC_MANAGE);
2787164033Srwatson		if (error)
2788145256Sjkoshy			break;
2789145256Sjkoshy
2790145256Sjkoshy		if ((error = copyin(arg, &pma, sizeof(pma))) != 0)
2791145256Sjkoshy			break;
2792145256Sjkoshy
2793145256Sjkoshy		cpu = pma.pm_cpu;
2794145256Sjkoshy
2795145256Sjkoshy		if (cpu < 0 || cpu >= mp_ncpus) {
2796145256Sjkoshy			error = EINVAL;
2797145256Sjkoshy			break;
2798145256Sjkoshy		}
2799145256Sjkoshy
2800145256Sjkoshy		if (pmc_cpu_is_disabled(cpu)) {
2801145256Sjkoshy			error = ENXIO;
2802145256Sjkoshy			break;
2803145256Sjkoshy		}
2804145256Sjkoshy
2805145256Sjkoshy		request = pma.pm_state;
2806145256Sjkoshy
2807145256Sjkoshy		if (request != PMC_STATE_DISABLED &&
2808145256Sjkoshy		    request != PMC_STATE_FREE) {
2809145256Sjkoshy			error = EINVAL;
2810145256Sjkoshy			break;
2811145256Sjkoshy		}
2812145256Sjkoshy
2813145256Sjkoshy		ri = pma.pm_pmc; /* pmc id == row index */
2814145256Sjkoshy		if (ri < 0 || ri >= (int) md->pmd_npmc) {
2815145256Sjkoshy			error = EINVAL;
2816145256Sjkoshy			break;
2817145256Sjkoshy		}
2818145256Sjkoshy
2819145256Sjkoshy		/*
2820145256Sjkoshy		 * We can't disable a PMC with a row-index allocated
2821145256Sjkoshy		 * for process virtual PMCs.
2822145256Sjkoshy		 */
2823145256Sjkoshy
2824145256Sjkoshy		if (PMC_ROW_DISP_IS_THREAD(ri) &&
2825145256Sjkoshy		    request == PMC_STATE_DISABLED) {
2826145256Sjkoshy			error = EBUSY;
2827145256Sjkoshy			break;
2828145256Sjkoshy		}
2829145256Sjkoshy
2830145256Sjkoshy		/*
2831145256Sjkoshy		 * otherwise, this PMC on this CPU is either free or
2832145256Sjkoshy		 * in system-wide mode.
2833145256Sjkoshy		 */
2834145256Sjkoshy
2835145256Sjkoshy		pmc_save_cpu_binding(&pb);
2836145256Sjkoshy		pmc_select_cpu(cpu);
2837145256Sjkoshy
2838145256Sjkoshy		pc  = pmc_pcpu[cpu];
2839145256Sjkoshy		phw = pc->pc_hwpmcs[ri];
2840145256Sjkoshy
2841145256Sjkoshy		/*
2842145256Sjkoshy		 * XXX do we need some kind of 'forced' disable?
2843145256Sjkoshy		 */
2844145256Sjkoshy
2845145256Sjkoshy		if (phw->phw_pmc == NULL) {
2846145256Sjkoshy			if (request == PMC_STATE_DISABLED &&
2847145256Sjkoshy			    (phw->phw_state & PMC_PHW_FLAG_IS_ENABLED)) {
2848145256Sjkoshy				phw->phw_state &= ~PMC_PHW_FLAG_IS_ENABLED;
2849145256Sjkoshy				PMC_MARK_ROW_STANDALONE(ri);
2850145256Sjkoshy			} else if (request == PMC_STATE_FREE &&
2851145256Sjkoshy			    (phw->phw_state & PMC_PHW_FLAG_IS_ENABLED) == 0) {
2852145256Sjkoshy				phw->phw_state |=  PMC_PHW_FLAG_IS_ENABLED;
2853145256Sjkoshy				PMC_UNMARK_ROW_STANDALONE(ri);
2854145256Sjkoshy			}
2855145256Sjkoshy			/* other cases are a no-op */
2856145256Sjkoshy		} else
2857145256Sjkoshy			error = EBUSY;
2858145256Sjkoshy
2859145256Sjkoshy		pmc_restore_cpu_binding(&pb);
2860145256Sjkoshy	}
2861145256Sjkoshy	break;
2862145256Sjkoshy
2863145256Sjkoshy
2864145256Sjkoshy	/*
2865145256Sjkoshy	 * Allocate a PMC.
2866145256Sjkoshy	 */
2867145256Sjkoshy
2868145256Sjkoshy	case PMC_OP_PMCALLOCATE:
2869145256Sjkoshy	{
2870145256Sjkoshy		uint32_t caps;
2871145256Sjkoshy		u_int cpu;
2872145256Sjkoshy		int n;
2873145256Sjkoshy		enum pmc_mode mode;
2874145256Sjkoshy		struct pmc *pmc;
2875145774Sjkoshy		struct pmc_hw *phw;
2876145256Sjkoshy		struct pmc_op_pmcallocate pa;
2877145256Sjkoshy		struct pmc_binding pb;
2878145256Sjkoshy
2879145256Sjkoshy		if ((error = copyin(arg, &pa, sizeof(pa))) != 0)
2880145256Sjkoshy			break;
2881145256Sjkoshy
2882145256Sjkoshy		caps = pa.pm_caps;
2883145256Sjkoshy		mode = pa.pm_mode;
2884145256Sjkoshy		cpu  = pa.pm_cpu;
2885145256Sjkoshy
2886145256Sjkoshy		if ((mode != PMC_MODE_SS  &&  mode != PMC_MODE_SC  &&
2887145256Sjkoshy		     mode != PMC_MODE_TS  &&  mode != PMC_MODE_TC) ||
2888145256Sjkoshy		    (cpu != (u_int) PMC_CPU_ANY && cpu >= (u_int) mp_ncpus)) {
2889145256Sjkoshy			error = EINVAL;
2890145256Sjkoshy			break;
2891145256Sjkoshy		}
2892145256Sjkoshy
2893145256Sjkoshy		/*
2894145256Sjkoshy		 * Virtual PMCs should only ask for a default CPU.
2895145256Sjkoshy		 * System mode PMCs need to specify a non-default CPU.
2896145256Sjkoshy		 */
2897145256Sjkoshy
2898145256Sjkoshy		if ((PMC_IS_VIRTUAL_MODE(mode) && cpu != (u_int) PMC_CPU_ANY) ||
2899145256Sjkoshy		    (PMC_IS_SYSTEM_MODE(mode) && cpu == (u_int) PMC_CPU_ANY)) {
2900145256Sjkoshy			error = EINVAL;
2901145256Sjkoshy			break;
2902145256Sjkoshy		}
2903145256Sjkoshy
2904145256Sjkoshy		/*
2905145256Sjkoshy		 * Check that a disabled CPU is not being asked for.
2906145256Sjkoshy		 */
2907145256Sjkoshy
2908145256Sjkoshy		if (PMC_IS_SYSTEM_MODE(mode) && pmc_cpu_is_disabled(cpu)) {
2909145256Sjkoshy			error = ENXIO;
2910145256Sjkoshy			break;
2911145256Sjkoshy		}
2912145256Sjkoshy
2913145256Sjkoshy		/*
2914145256Sjkoshy		 * Refuse an allocation for a system-wide PMC if this
2915145256Sjkoshy		 * process has been jailed, or if this process lacks
2916145256Sjkoshy		 * super-user credentials and the sysctl tunable
2917145256Sjkoshy		 * 'security.bsd.unprivileged_syspmcs' is zero.
2918145256Sjkoshy		 */
2919145256Sjkoshy
2920145256Sjkoshy		if (PMC_IS_SYSTEM_MODE(mode)) {
2921164033Srwatson			if (jailed(curthread->td_ucred)) {
2922145256Sjkoshy				error = EPERM;
2923164033Srwatson				break;
2924164033Srwatson			}
2925164033Srwatson			if (!pmc_unprivileged_syspmcs) {
2926164033Srwatson				error = priv_check(curthread,
2927164033Srwatson				    PRIV_PMC_SYSTEM);
2928164033Srwatson				if (error)
2929164033Srwatson					break;
2930164033Srwatson			}
2931145256Sjkoshy		}
2932145256Sjkoshy
2933145256Sjkoshy		if (error)
2934145256Sjkoshy			break;
2935145256Sjkoshy
2936145256Sjkoshy		/*
2937145256Sjkoshy		 * Look for valid values for 'pm_flags'
2938145256Sjkoshy		 */
2939145256Sjkoshy
2940147191Sjkoshy		if ((pa.pm_flags & ~(PMC_F_DESCENDANTS | PMC_F_LOG_PROCCSW |
2941147191Sjkoshy		    PMC_F_LOG_PROCEXIT)) != 0) {
2942145256Sjkoshy			error = EINVAL;
2943145256Sjkoshy			break;
2944145256Sjkoshy		}
2945145256Sjkoshy
2946147191Sjkoshy		/* process logging options are not allowed for system PMCs */
2947147191Sjkoshy		if (PMC_IS_SYSTEM_MODE(mode) && (pa.pm_flags &
2948147191Sjkoshy		    (PMC_F_LOG_PROCCSW | PMC_F_LOG_PROCEXIT))) {
2949147191Sjkoshy			error = EINVAL;
2950147191Sjkoshy			break;
2951147191Sjkoshy		}
2952147191Sjkoshy
2953145256Sjkoshy		/*
2954145256Sjkoshy		 * All sampling mode PMCs need to be able to interrupt the
2955145256Sjkoshy		 * CPU.
2956145256Sjkoshy		 */
2957147191Sjkoshy		if (PMC_IS_SAMPLING_MODE(mode))
2958145256Sjkoshy			caps |= PMC_CAP_INTERRUPT;
2959145256Sjkoshy
2960149374Sjkoshy		/* A valid class specifier should have been passed in. */
2961149374Sjkoshy		for (n = 0; n < md->pmd_nclass; n++)
2962149374Sjkoshy			if (md->pmd_classes[n].pm_class == pa.pm_class)
2963149374Sjkoshy				break;
2964149374Sjkoshy		if (n == md->pmd_nclass) {
2965149374Sjkoshy			error = EINVAL;
2966149374Sjkoshy			break;
2967149374Sjkoshy		}
2968149374Sjkoshy
2969149374Sjkoshy		/* The requested PMC capabilities should be feasible. */
2970149374Sjkoshy		if ((md->pmd_classes[n].pm_caps & caps) != caps) {
2971149374Sjkoshy			error = EOPNOTSUPP;
2972149374Sjkoshy			break;
2973149374Sjkoshy		}
2974149374Sjkoshy
2975145256Sjkoshy		PMCDBG(PMC,ALL,2, "event=%d caps=0x%x mode=%d cpu=%d",
2976145256Sjkoshy		    pa.pm_ev, caps, mode, cpu);
2977145256Sjkoshy
2978145256Sjkoshy		pmc = pmc_allocate_pmc_descriptor();
2979145774Sjkoshy		pmc->pm_id    = PMC_ID_MAKE_ID(cpu,pa.pm_mode,pa.pm_class,
2980145774Sjkoshy		    PMC_ID_INVALID);
2981145256Sjkoshy		pmc->pm_event = pa.pm_ev;
2982145256Sjkoshy		pmc->pm_state = PMC_STATE_FREE;
2983145256Sjkoshy		pmc->pm_caps  = caps;
2984145256Sjkoshy		pmc->pm_flags = pa.pm_flags;
2985145256Sjkoshy
2986145256Sjkoshy		/* switch thread to CPU 'cpu' */
2987145256Sjkoshy		pmc_save_cpu_binding(&pb);
2988145256Sjkoshy
2989145256Sjkoshy#define	PMC_IS_SHAREABLE_PMC(cpu, n)				\
2990145256Sjkoshy	(pmc_pcpu[(cpu)]->pc_hwpmcs[(n)]->phw_state &		\
2991145256Sjkoshy	 PMC_PHW_FLAG_IS_SHAREABLE)
2992145256Sjkoshy#define	PMC_IS_UNALLOCATED(cpu, n)				\
2993145256Sjkoshy	(pmc_pcpu[(cpu)]->pc_hwpmcs[(n)]->phw_pmc == NULL)
2994145256Sjkoshy
2995145256Sjkoshy		if (PMC_IS_SYSTEM_MODE(mode)) {
2996145256Sjkoshy			pmc_select_cpu(cpu);
2997145256Sjkoshy			for (n = 0; n < (int) md->pmd_npmc; n++)
2998145256Sjkoshy				if (pmc_can_allocate_row(n, mode) == 0 &&
2999145256Sjkoshy				    pmc_can_allocate_rowindex(
3000145774Sjkoshy					    curthread->td_proc, n, cpu) == 0 &&
3001145256Sjkoshy				    (PMC_IS_UNALLOCATED(cpu, n) ||
3002145256Sjkoshy				     PMC_IS_SHAREABLE_PMC(cpu, n)) &&
3003145256Sjkoshy				    md->pmd_allocate_pmc(cpu, n, pmc,
3004145256Sjkoshy					&pa) == 0)
3005145256Sjkoshy					break;
3006145256Sjkoshy		} else {
3007145256Sjkoshy			/* Process virtual mode */
3008145256Sjkoshy			for (n = 0; n < (int) md->pmd_npmc; n++) {
3009145256Sjkoshy				if (pmc_can_allocate_row(n, mode) == 0 &&
3010145256Sjkoshy				    pmc_can_allocate_rowindex(
3011145774Sjkoshy					    curthread->td_proc, n,
3012145774Sjkoshy					    PMC_CPU_ANY) == 0 &&
3013145256Sjkoshy				    md->pmd_allocate_pmc(curthread->td_oncpu,
3014145256Sjkoshy					n, pmc, &pa) == 0)
3015145256Sjkoshy					break;
3016145256Sjkoshy			}
3017145256Sjkoshy		}
3018145256Sjkoshy
3019145256Sjkoshy#undef	PMC_IS_UNALLOCATED
3020145256Sjkoshy#undef	PMC_IS_SHAREABLE_PMC
3021145256Sjkoshy
3022145256Sjkoshy		pmc_restore_cpu_binding(&pb);
3023145256Sjkoshy
3024145256Sjkoshy		if (n == (int) md->pmd_npmc) {
3025145256Sjkoshy			pmc_destroy_pmc_descriptor(pmc);
3026145256Sjkoshy			FREE(pmc, M_PMC);
3027145256Sjkoshy			pmc = NULL;
3028145256Sjkoshy			error = EINVAL;
3029145256Sjkoshy			break;
3030145256Sjkoshy		}
3031145256Sjkoshy
3032145774Sjkoshy		/* Fill in the correct value in the ID field */
3033145774Sjkoshy		pmc->pm_id = PMC_ID_MAKE_ID(cpu,mode,pa.pm_class,n);
3034145256Sjkoshy
3035145774Sjkoshy		PMCDBG(PMC,ALL,2, "ev=%d class=%d mode=%d n=%d -> pmcid=%x",
3036145774Sjkoshy		    pmc->pm_event, pa.pm_class, mode, n, pmc->pm_id);
3037145774Sjkoshy
3038147191Sjkoshy		/* Process mode PMCs with logging enabled need log files */
3039147191Sjkoshy		if (pmc->pm_flags & (PMC_F_LOG_PROCEXIT | PMC_F_LOG_PROCCSW))
3040147191Sjkoshy			pmc->pm_flags |= PMC_F_NEEDS_LOGFILE;
3041147191Sjkoshy
3042147191Sjkoshy		/* All system mode sampling PMCs require a log file */
3043147191Sjkoshy		if (PMC_IS_SAMPLING_MODE(mode) && PMC_IS_SYSTEM_MODE(mode))
3044147191Sjkoshy			pmc->pm_flags |= PMC_F_NEEDS_LOGFILE;
3045147191Sjkoshy
3046145256Sjkoshy		/*
3047145256Sjkoshy		 * Configure global pmc's immediately
3048145256Sjkoshy		 */
3049145256Sjkoshy
3050145774Sjkoshy		if (PMC_IS_SYSTEM_MODE(PMC_TO_MODE(pmc))) {
3051145774Sjkoshy
3052145774Sjkoshy			pmc_save_cpu_binding(&pb);
3053145774Sjkoshy			pmc_select_cpu(cpu);
3054145774Sjkoshy
3055145774Sjkoshy			phw = pmc_pcpu[cpu]->pc_hwpmcs[n];
3056145774Sjkoshy
3057145774Sjkoshy			if ((phw->phw_state & PMC_PHW_FLAG_IS_ENABLED) == 0 ||
3058145774Sjkoshy			    (error = md->pmd_config_pmc(cpu, n, pmc)) != 0) {
3059145256Sjkoshy				(void) md->pmd_release_pmc(cpu, n, pmc);
3060145256Sjkoshy				pmc_destroy_pmc_descriptor(pmc);
3061145256Sjkoshy				FREE(pmc, M_PMC);
3062145256Sjkoshy				pmc = NULL;
3063145774Sjkoshy				pmc_restore_cpu_binding(&pb);
3064145774Sjkoshy				error = EPERM;
3065145256Sjkoshy				break;
3066145256Sjkoshy			}
3067145256Sjkoshy
3068145774Sjkoshy			pmc_restore_cpu_binding(&pb);
3069145774Sjkoshy		}
3070145256Sjkoshy
3071145256Sjkoshy		pmc->pm_state    = PMC_STATE_ALLOCATED;
3072145256Sjkoshy
3073145256Sjkoshy		/*
3074145256Sjkoshy		 * mark row disposition
3075145256Sjkoshy		 */
3076145256Sjkoshy
3077145256Sjkoshy		if (PMC_IS_SYSTEM_MODE(mode))
3078145256Sjkoshy			PMC_MARK_ROW_STANDALONE(n);
3079145256Sjkoshy		else
3080145256Sjkoshy			PMC_MARK_ROW_THREAD(n);
3081145256Sjkoshy
3082145256Sjkoshy		/*
3083145256Sjkoshy		 * Register this PMC with the current thread as its owner.
3084145256Sjkoshy		 */
3085145256Sjkoshy
3086145256Sjkoshy		if ((error =
3087145256Sjkoshy		    pmc_register_owner(curthread->td_proc, pmc)) != 0) {
3088145256Sjkoshy			pmc_release_pmc_descriptor(pmc);
3089145256Sjkoshy			FREE(pmc, M_PMC);
3090145256Sjkoshy			pmc = NULL;
3091145256Sjkoshy			break;
3092145256Sjkoshy		}
3093145256Sjkoshy
3094145256Sjkoshy		/*
3095145256Sjkoshy		 * Return the allocated index.
3096145256Sjkoshy		 */
3097145256Sjkoshy
3098145774Sjkoshy		pa.pm_pmcid = pmc->pm_id;
3099145256Sjkoshy
3100145256Sjkoshy		error = copyout(&pa, arg, sizeof(pa));
3101145256Sjkoshy	}
3102145256Sjkoshy	break;
3103145256Sjkoshy
3104145256Sjkoshy
3105145256Sjkoshy	/*
3106145256Sjkoshy	 * Attach a PMC to a process.
3107145256Sjkoshy	 */
3108145256Sjkoshy
3109145256Sjkoshy	case PMC_OP_PMCATTACH:
3110145256Sjkoshy	{
3111145256Sjkoshy		struct pmc *pm;
3112145256Sjkoshy		struct proc *p;
3113145256Sjkoshy		struct pmc_op_pmcattach a;
3114145256Sjkoshy
3115145256Sjkoshy		sx_assert(&pmc_sx, SX_XLOCKED);
3116145256Sjkoshy
3117145256Sjkoshy		if ((error = copyin(arg, &a, sizeof(a))) != 0)
3118145256Sjkoshy			break;
3119145256Sjkoshy
3120145256Sjkoshy		if (a.pm_pid < 0) {
3121145256Sjkoshy			error = EINVAL;
3122145256Sjkoshy			break;
3123145256Sjkoshy		} else if (a.pm_pid == 0)
3124145256Sjkoshy			a.pm_pid = td->td_proc->p_pid;
3125145256Sjkoshy
3126145256Sjkoshy		if ((error = pmc_find_pmc(a.pm_pmc, &pm)) != 0)
3127145256Sjkoshy			break;
3128145256Sjkoshy
3129145774Sjkoshy		if (PMC_IS_SYSTEM_MODE(PMC_TO_MODE(pm))) {
3130145256Sjkoshy			error = EINVAL;
3131145256Sjkoshy			break;
3132145256Sjkoshy		}
3133145256Sjkoshy
3134145256Sjkoshy		/* PMCs may be (re)attached only when allocated or stopped */
3135145256Sjkoshy		if (pm->pm_state == PMC_STATE_RUNNING) {
3136145256Sjkoshy			error = EBUSY;
3137145256Sjkoshy			break;
3138145256Sjkoshy		} else if (pm->pm_state != PMC_STATE_ALLOCATED &&
3139145256Sjkoshy		    pm->pm_state != PMC_STATE_STOPPED) {
3140145256Sjkoshy			error = EINVAL;
3141145256Sjkoshy			break;
3142145256Sjkoshy		}
3143145256Sjkoshy
3144145256Sjkoshy		/* lookup pid */
3145145256Sjkoshy		if ((p = pfind(a.pm_pid)) == NULL) {
3146145256Sjkoshy			error = ESRCH;
3147145256Sjkoshy			break;
3148145256Sjkoshy		}
3149145256Sjkoshy
3150145256Sjkoshy		/*
3151145256Sjkoshy		 * Ignore processes that are working on exiting.
3152145256Sjkoshy		 */
3153145256Sjkoshy		if (p->p_flag & P_WEXIT) {
3154145256Sjkoshy			error = ESRCH;
3155145256Sjkoshy			PROC_UNLOCK(p);	/* pfind() returns a locked process */
3156145256Sjkoshy			break;
3157145256Sjkoshy		}
3158145256Sjkoshy
3159145256Sjkoshy		/*
3160145256Sjkoshy		 * we are allowed to attach a PMC to a process if
3161145256Sjkoshy		 * we can debug it.
3162145256Sjkoshy		 */
3163145256Sjkoshy		error = p_candebug(curthread, p);
3164145256Sjkoshy
3165145256Sjkoshy		PROC_UNLOCK(p);
3166145256Sjkoshy
3167145256Sjkoshy		if (error == 0)
3168145256Sjkoshy			error = pmc_attach_process(p, pm);
3169145256Sjkoshy	}
3170145256Sjkoshy	break;
3171145256Sjkoshy
3172145256Sjkoshy
3173145256Sjkoshy	/*
3174145256Sjkoshy	 * Detach an attached PMC from a process.
3175145256Sjkoshy	 */
3176145256Sjkoshy
3177145256Sjkoshy	case PMC_OP_PMCDETACH:
3178145256Sjkoshy	{
3179145256Sjkoshy		struct pmc *pm;
3180145256Sjkoshy		struct proc *p;
3181145256Sjkoshy		struct pmc_op_pmcattach a;
3182145256Sjkoshy
3183145256Sjkoshy		if ((error = copyin(arg, &a, sizeof(a))) != 0)
3184145256Sjkoshy			break;
3185145256Sjkoshy
3186145256Sjkoshy		if (a.pm_pid < 0) {
3187145256Sjkoshy			error = EINVAL;
3188145256Sjkoshy			break;
3189145256Sjkoshy		} else if (a.pm_pid == 0)
3190145256Sjkoshy			a.pm_pid = td->td_proc->p_pid;
3191145256Sjkoshy
3192145256Sjkoshy		if ((error = pmc_find_pmc(a.pm_pmc, &pm)) != 0)
3193145256Sjkoshy			break;
3194145256Sjkoshy
3195145256Sjkoshy		if ((p = pfind(a.pm_pid)) == NULL) {
3196145256Sjkoshy			error = ESRCH;
3197145256Sjkoshy			break;
3198145256Sjkoshy		}
3199145256Sjkoshy
3200145256Sjkoshy		/*
3201145256Sjkoshy		 * Treat processes that are in the process of exiting
3202145256Sjkoshy		 * as if they were not present.
3203145256Sjkoshy		 */
3204145256Sjkoshy
3205145256Sjkoshy		if (p->p_flag & P_WEXIT)
3206145256Sjkoshy			error = ESRCH;
3207145256Sjkoshy
3208145256Sjkoshy		PROC_UNLOCK(p);	/* pfind() returns a locked process */
3209145256Sjkoshy
3210145256Sjkoshy		if (error == 0)
3211145256Sjkoshy			error = pmc_detach_process(p, pm);
3212145256Sjkoshy	}
3213145256Sjkoshy	break;
3214145256Sjkoshy
3215145256Sjkoshy
3216145256Sjkoshy	/*
3217147191Sjkoshy	 * Retrieve the MSR number associated with the counter
3218147191Sjkoshy	 * 'pmc_id'.  This allows processes to directly use RDPMC
3219147191Sjkoshy	 * instructions to read their PMCs, without the overhead of a
3220147191Sjkoshy	 * system call.
3221147191Sjkoshy	 */
3222147191Sjkoshy
3223147191Sjkoshy	case PMC_OP_PMCGETMSR:
3224147191Sjkoshy	{
3225147191Sjkoshy		int ri;
3226147191Sjkoshy		struct pmc	*pm;
3227147191Sjkoshy		struct pmc_target *pt;
3228147191Sjkoshy		struct pmc_op_getmsr gm;
3229147191Sjkoshy
3230147191Sjkoshy		PMC_DOWNGRADE_SX();
3231147191Sjkoshy
3232147191Sjkoshy		/* CPU has no 'GETMSR' support */
3233147191Sjkoshy		if (md->pmd_get_msr == NULL) {
3234147191Sjkoshy			error = ENOSYS;
3235147191Sjkoshy			break;
3236147191Sjkoshy		}
3237147191Sjkoshy
3238147191Sjkoshy		if ((error = copyin(arg, &gm, sizeof(gm))) != 0)
3239147191Sjkoshy			break;
3240147191Sjkoshy
3241147191Sjkoshy		if ((error = pmc_find_pmc(gm.pm_pmcid, &pm)) != 0)
3242147191Sjkoshy			break;
3243147191Sjkoshy
3244147191Sjkoshy		/*
3245147191Sjkoshy		 * The allocated PMC has to be a process virtual PMC,
3246147191Sjkoshy		 * i.e., of type MODE_T[CS].  Global PMCs can only be
3247147191Sjkoshy		 * read using the PMCREAD operation since they may be
3248147191Sjkoshy		 * allocated on a different CPU than the one we could
3249147191Sjkoshy		 * be running on at the time of the RDPMC instruction.
3250147191Sjkoshy		 *
3251147191Sjkoshy		 * The GETMSR operation is not allowed for PMCs that
3252147191Sjkoshy		 * are inherited across processes.
3253147191Sjkoshy		 */
3254147191Sjkoshy
3255147191Sjkoshy		if (!PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm)) ||
3256147191Sjkoshy		    (pm->pm_flags & PMC_F_DESCENDANTS)) {
3257147191Sjkoshy			error = EINVAL;
3258147191Sjkoshy			break;
3259147191Sjkoshy		}
3260147191Sjkoshy
3261147191Sjkoshy		/*
3262147191Sjkoshy		 * It only makes sense to use a RDPMC (or its
3263147191Sjkoshy		 * equivalent instruction on non-x86 architectures) on
3264147191Sjkoshy		 * a process that has allocated and attached a PMC to
3265147191Sjkoshy		 * itself.  Conversely the PMC is only allowed to have
3266147191Sjkoshy		 * one process attached to it -- its owner.
3267147191Sjkoshy		 */
3268147191Sjkoshy
3269147191Sjkoshy		if ((pt = LIST_FIRST(&pm->pm_targets)) == NULL ||
3270147191Sjkoshy		    LIST_NEXT(pt, pt_next) != NULL ||
3271147191Sjkoshy		    pt->pt_process->pp_proc != pm->pm_owner->po_owner) {
3272147191Sjkoshy			error = EINVAL;
3273147191Sjkoshy			break;
3274147191Sjkoshy		}
3275147191Sjkoshy
3276147191Sjkoshy		ri = PMC_TO_ROWINDEX(pm);
3277147191Sjkoshy
3278147191Sjkoshy		if ((error = (*md->pmd_get_msr)(ri, &gm.pm_msr)) < 0)
3279147191Sjkoshy			break;
3280147191Sjkoshy
3281147191Sjkoshy		if ((error = copyout(&gm, arg, sizeof(gm))) < 0)
3282147191Sjkoshy			break;
3283147191Sjkoshy
3284147191Sjkoshy		/*
3285147191Sjkoshy		 * Mark our process as using MSRs.  Update machine
3286147191Sjkoshy		 * state using a forced context switch.
3287147191Sjkoshy		 */
3288147191Sjkoshy
3289147191Sjkoshy		pt->pt_process->pp_flags |= PMC_PP_ENABLE_MSR_ACCESS;
3290147191Sjkoshy		pmc_force_context_switch();
3291147191Sjkoshy
3292147191Sjkoshy	}
3293147191Sjkoshy	break;
3294147191Sjkoshy
3295147191Sjkoshy	/*
3296145256Sjkoshy	 * Release an allocated PMC
3297145256Sjkoshy	 */
3298145256Sjkoshy
3299145256Sjkoshy	case PMC_OP_PMCRELEASE:
3300145256Sjkoshy	{
3301145256Sjkoshy		pmc_id_t pmcid;
3302145256Sjkoshy		struct pmc *pm;
3303145256Sjkoshy		struct pmc_owner *po;
3304145256Sjkoshy		struct pmc_op_simple sp;
3305145256Sjkoshy
3306145256Sjkoshy		/*
3307145256Sjkoshy		 * Find PMC pointer for the named PMC.
3308145256Sjkoshy		 *
3309145256Sjkoshy		 * Use pmc_release_pmc_descriptor() to switch off the
3310145256Sjkoshy		 * PMC, remove all its target threads, and remove the
3311145256Sjkoshy		 * PMC from its owner's list.
3312145256Sjkoshy		 *
3313145256Sjkoshy		 * Remove the owner record if this is the last PMC
3314145256Sjkoshy		 * owned.
3315145256Sjkoshy		 *
3316145256Sjkoshy		 * Free up space.
3317145256Sjkoshy		 */
3318145256Sjkoshy
3319145256Sjkoshy		if ((error = copyin(arg, &sp, sizeof(sp))) != 0)
3320145256Sjkoshy			break;
3321145256Sjkoshy
3322145256Sjkoshy		pmcid = sp.pm_pmcid;
3323145256Sjkoshy
3324145256Sjkoshy		if ((error = pmc_find_pmc(pmcid, &pm)) != 0)
3325145256Sjkoshy			break;
3326145256Sjkoshy
3327145256Sjkoshy		po = pm->pm_owner;
3328145256Sjkoshy		pmc_release_pmc_descriptor(pm);
3329145256Sjkoshy		pmc_maybe_remove_owner(po);
3330145256Sjkoshy
3331145256Sjkoshy		FREE(pm, M_PMC);
3332145256Sjkoshy	}
3333145256Sjkoshy	break;
3334145256Sjkoshy
3335145256Sjkoshy
3336145256Sjkoshy	/*
3337145256Sjkoshy	 * Read and/or write a PMC.
3338145256Sjkoshy	 */
3339145256Sjkoshy
3340145256Sjkoshy	case PMC_OP_PMCRW:
3341145256Sjkoshy	{
3342145256Sjkoshy		uint32_t cpu, ri;
3343145256Sjkoshy		struct pmc *pm;
3344145256Sjkoshy		struct pmc_op_pmcrw *pprw;
3345145256Sjkoshy		struct pmc_op_pmcrw prw;
3346145256Sjkoshy		struct pmc_binding pb;
3347145256Sjkoshy		pmc_value_t oldvalue;
3348145256Sjkoshy
3349145256Sjkoshy		PMC_DOWNGRADE_SX();
3350145256Sjkoshy
3351145256Sjkoshy		if ((error = copyin(arg, &prw, sizeof(prw))) != 0)
3352145256Sjkoshy			break;
3353145256Sjkoshy
3354145301Simp		ri = 0;
3355145256Sjkoshy		PMCDBG(PMC,OPS,1, "rw id=%d flags=0x%x", prw.pm_pmcid,
3356145256Sjkoshy		    prw.pm_flags);
3357145256Sjkoshy
3358145256Sjkoshy		/* must have at least one flag set */
3359145256Sjkoshy		if ((prw.pm_flags & (PMC_F_OLDVALUE|PMC_F_NEWVALUE)) == 0) {
3360145256Sjkoshy			error = EINVAL;
3361145256Sjkoshy			break;
3362145256Sjkoshy		}
3363145256Sjkoshy
3364145256Sjkoshy		/* locate pmc descriptor */
3365145256Sjkoshy		if ((error = pmc_find_pmc(prw.pm_pmcid, &pm)) != 0)
3366145256Sjkoshy			break;
3367145256Sjkoshy
3368145256Sjkoshy		/* Can't read a PMC that hasn't been started. */
3369145256Sjkoshy		if (pm->pm_state != PMC_STATE_ALLOCATED &&
3370145256Sjkoshy		    pm->pm_state != PMC_STATE_STOPPED &&
3371145256Sjkoshy		    pm->pm_state != PMC_STATE_RUNNING) {
3372145256Sjkoshy			error = EINVAL;
3373145256Sjkoshy			break;
3374145256Sjkoshy		}
3375145256Sjkoshy
3376145256Sjkoshy		/* writing a new value is allowed only for 'STOPPED' pmcs */
3377145256Sjkoshy		if (pm->pm_state == PMC_STATE_RUNNING &&
3378145256Sjkoshy		    (prw.pm_flags & PMC_F_NEWVALUE)) {
3379145256Sjkoshy			error = EBUSY;
3380145256Sjkoshy			break;
3381145256Sjkoshy		}
3382145256Sjkoshy
3383145774Sjkoshy		if (PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm))) {
3384145256Sjkoshy
3385145774Sjkoshy			/*
3386145774Sjkoshy			 * If this PMC is attached to its owner (i.e.,
3387145774Sjkoshy			 * the process requesting this operation) and
3388145774Sjkoshy			 * is running, then attempt to get an
3389145774Sjkoshy			 * upto-date reading from hardware for a READ.
3390145774Sjkoshy			 * Writes are only allowed when the PMC is
3391145774Sjkoshy			 * stopped, so only update the saved value
3392145774Sjkoshy			 * field.
3393145774Sjkoshy			 *
3394145774Sjkoshy			 * If the PMC is not running, or is not
3395145774Sjkoshy			 * attached to its owner, read/write to the
3396145774Sjkoshy			 * savedvalue field.
3397145774Sjkoshy			 */
3398145774Sjkoshy
3399145774Sjkoshy			ri = PMC_TO_ROWINDEX(pm);
3400145774Sjkoshy
3401145256Sjkoshy			mtx_pool_lock_spin(pmc_mtxpool, pm);
3402145774Sjkoshy			cpu = curthread->td_oncpu;
3403145774Sjkoshy
3404145774Sjkoshy			if (prw.pm_flags & PMC_F_OLDVALUE) {
3405145774Sjkoshy				if ((pm->pm_flags & PMC_F_ATTACHED_TO_OWNER) &&
3406145774Sjkoshy				    (pm->pm_state == PMC_STATE_RUNNING))
3407145774Sjkoshy					error = (*md->pmd_read_pmc)(cpu, ri,
3408145774Sjkoshy					    &oldvalue);
3409145774Sjkoshy				else
3410145774Sjkoshy					oldvalue = pm->pm_gv.pm_savedvalue;
3411145774Sjkoshy			}
3412145256Sjkoshy			if (prw.pm_flags & PMC_F_NEWVALUE)
3413145256Sjkoshy				pm->pm_gv.pm_savedvalue = prw.pm_value;
3414145774Sjkoshy
3415145256Sjkoshy			mtx_pool_unlock_spin(pmc_mtxpool, pm);
3416145256Sjkoshy
3417145256Sjkoshy		} else { /* System mode PMCs */
3418145774Sjkoshy			cpu = PMC_TO_CPU(pm);
3419145774Sjkoshy			ri  = PMC_TO_ROWINDEX(pm);
3420145256Sjkoshy
3421145256Sjkoshy			if (pmc_cpu_is_disabled(cpu)) {
3422145256Sjkoshy				error = ENXIO;
3423145256Sjkoshy				break;
3424145256Sjkoshy			}
3425145256Sjkoshy
3426145256Sjkoshy			/* move this thread to CPU 'cpu' */
3427145256Sjkoshy			pmc_save_cpu_binding(&pb);
3428145256Sjkoshy			pmc_select_cpu(cpu);
3429145256Sjkoshy
3430145774Sjkoshy			critical_enter();
3431145256Sjkoshy			/* save old value */
3432145256Sjkoshy			if (prw.pm_flags & PMC_F_OLDVALUE)
3433145256Sjkoshy				if ((error = (*md->pmd_read_pmc)(cpu, ri,
3434145256Sjkoshy					 &oldvalue)))
3435145256Sjkoshy					goto error;
3436145256Sjkoshy			/* write out new value */
3437145256Sjkoshy			if (prw.pm_flags & PMC_F_NEWVALUE)
3438145256Sjkoshy				error = (*md->pmd_write_pmc)(cpu, ri,
3439145256Sjkoshy				    prw.pm_value);
3440145256Sjkoshy		error:
3441145774Sjkoshy			critical_exit();
3442145256Sjkoshy			pmc_restore_cpu_binding(&pb);
3443145256Sjkoshy			if (error)
3444145256Sjkoshy				break;
3445145256Sjkoshy		}
3446145256Sjkoshy
3447145256Sjkoshy		pprw = (struct pmc_op_pmcrw *) arg;
3448145256Sjkoshy
3449153110Sru#ifdef	DEBUG
3450145256Sjkoshy		if (prw.pm_flags & PMC_F_NEWVALUE)
3451145256Sjkoshy			PMCDBG(PMC,OPS,2, "rw id=%d new %jx -> old %jx",
3452145256Sjkoshy			    ri, prw.pm_value, oldvalue);
3453156778Sjkoshy		else if (prw.pm_flags & PMC_F_OLDVALUE)
3454145256Sjkoshy			PMCDBG(PMC,OPS,2, "rw id=%d -> old %jx", ri, oldvalue);
3455145256Sjkoshy#endif
3456145256Sjkoshy
3457145256Sjkoshy		/* return old value if requested */
3458145256Sjkoshy		if (prw.pm_flags & PMC_F_OLDVALUE)
3459145256Sjkoshy			if ((error = copyout(&oldvalue, &pprw->pm_value,
3460145256Sjkoshy				 sizeof(prw.pm_value))))
3461145256Sjkoshy				break;
3462145256Sjkoshy
3463145256Sjkoshy	}
3464145256Sjkoshy	break;
3465145256Sjkoshy
3466145256Sjkoshy
3467145256Sjkoshy	/*
3468145256Sjkoshy	 * Set the sampling rate for a sampling mode PMC and the
3469145256Sjkoshy	 * initial count for a counting mode PMC.
3470145256Sjkoshy	 */
3471145256Sjkoshy
3472145256Sjkoshy	case PMC_OP_PMCSETCOUNT:
3473145256Sjkoshy	{
3474145256Sjkoshy		struct pmc *pm;
3475145256Sjkoshy		struct pmc_op_pmcsetcount sc;
3476145256Sjkoshy
3477145256Sjkoshy		PMC_DOWNGRADE_SX();
3478145256Sjkoshy
3479145256Sjkoshy		if ((error = copyin(arg, &sc, sizeof(sc))) != 0)
3480145256Sjkoshy			break;
3481145256Sjkoshy
3482145256Sjkoshy		if ((error = pmc_find_pmc(sc.pm_pmcid, &pm)) != 0)
3483145256Sjkoshy			break;
3484145256Sjkoshy
3485145256Sjkoshy		if (pm->pm_state == PMC_STATE_RUNNING) {
3486145256Sjkoshy			error = EBUSY;
3487145256Sjkoshy			break;
3488145256Sjkoshy		}
3489145256Sjkoshy
3490145774Sjkoshy		if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
3491145256Sjkoshy			pm->pm_sc.pm_reloadcount = sc.pm_count;
3492145256Sjkoshy		else
3493145256Sjkoshy			pm->pm_sc.pm_initial = sc.pm_count;
3494145256Sjkoshy	}
3495145256Sjkoshy	break;
3496145256Sjkoshy
3497145256Sjkoshy
3498145256Sjkoshy	/*
3499145256Sjkoshy	 * Start a PMC.
3500145256Sjkoshy	 */
3501145256Sjkoshy
3502145256Sjkoshy	case PMC_OP_PMCSTART:
3503145256Sjkoshy	{
3504145256Sjkoshy		pmc_id_t pmcid;
3505145256Sjkoshy		struct pmc *pm;
3506145256Sjkoshy		struct pmc_op_simple sp;
3507145256Sjkoshy
3508145256Sjkoshy		sx_assert(&pmc_sx, SX_XLOCKED);
3509145256Sjkoshy
3510145256Sjkoshy		if ((error = copyin(arg, &sp, sizeof(sp))) != 0)
3511145256Sjkoshy			break;
3512145256Sjkoshy
3513145256Sjkoshy		pmcid = sp.pm_pmcid;
3514145256Sjkoshy
3515145256Sjkoshy		if ((error = pmc_find_pmc(pmcid, &pm)) != 0)
3516145256Sjkoshy			break;
3517145256Sjkoshy
3518145774Sjkoshy		KASSERT(pmcid == pm->pm_id,
3519145774Sjkoshy		    ("[pmc,%d] pmcid %x != id %x", __LINE__,
3520145774Sjkoshy			pm->pm_id, pmcid));
3521145256Sjkoshy
3522145256Sjkoshy		if (pm->pm_state == PMC_STATE_RUNNING) /* already running */
3523145256Sjkoshy			break;
3524145256Sjkoshy		else if (pm->pm_state != PMC_STATE_STOPPED &&
3525145256Sjkoshy		    pm->pm_state != PMC_STATE_ALLOCATED) {
3526145256Sjkoshy			error = EINVAL;
3527145256Sjkoshy			break;
3528145256Sjkoshy		}
3529145256Sjkoshy
3530145256Sjkoshy		error = pmc_start(pm);
3531145256Sjkoshy	}
3532145256Sjkoshy	break;
3533145256Sjkoshy
3534145256Sjkoshy
3535145256Sjkoshy	/*
3536145256Sjkoshy	 * Stop a PMC.
3537145256Sjkoshy	 */
3538145256Sjkoshy
3539145256Sjkoshy	case PMC_OP_PMCSTOP:
3540145256Sjkoshy	{
3541145256Sjkoshy		pmc_id_t pmcid;
3542145256Sjkoshy		struct pmc *pm;
3543145256Sjkoshy		struct pmc_op_simple sp;
3544145256Sjkoshy
3545145256Sjkoshy		PMC_DOWNGRADE_SX();
3546145256Sjkoshy
3547145256Sjkoshy		if ((error = copyin(arg, &sp, sizeof(sp))) != 0)
3548145256Sjkoshy			break;
3549145256Sjkoshy
3550145256Sjkoshy		pmcid = sp.pm_pmcid;
3551145256Sjkoshy
3552145256Sjkoshy		/*
3553145256Sjkoshy		 * Mark the PMC as inactive and invoke the MD stop
3554145256Sjkoshy		 * routines if needed.
3555145256Sjkoshy		 */
3556145256Sjkoshy
3557145256Sjkoshy		if ((error = pmc_find_pmc(pmcid, &pm)) != 0)
3558145256Sjkoshy			break;
3559145256Sjkoshy
3560145774Sjkoshy		KASSERT(pmcid == pm->pm_id,
3561145774Sjkoshy		    ("[pmc,%d] pmc id %x != pmcid %x", __LINE__,
3562145774Sjkoshy			pm->pm_id, pmcid));
3563145256Sjkoshy
3564145256Sjkoshy		if (pm->pm_state == PMC_STATE_STOPPED) /* already stopped */
3565145256Sjkoshy			break;
3566145256Sjkoshy		else if (pm->pm_state != PMC_STATE_RUNNING) {
3567145256Sjkoshy			error = EINVAL;
3568145256Sjkoshy			break;
3569145256Sjkoshy		}
3570145256Sjkoshy
3571145256Sjkoshy		error = pmc_stop(pm);
3572145256Sjkoshy	}
3573145256Sjkoshy	break;
3574145256Sjkoshy
3575145256Sjkoshy
3576145256Sjkoshy	/*
3577147867Sjkoshy	 * Write a user supplied value to the log file.
3578145256Sjkoshy	 */
3579145256Sjkoshy
3580145256Sjkoshy	case PMC_OP_WRITELOG:
3581145256Sjkoshy	{
3582147191Sjkoshy		struct pmc_op_writelog wl;
3583147191Sjkoshy		struct pmc_owner *po;
3584145256Sjkoshy
3585145256Sjkoshy		PMC_DOWNGRADE_SX();
3586145256Sjkoshy
3587147191Sjkoshy		if ((error = copyin(arg, &wl, sizeof(wl))) != 0)
3588145256Sjkoshy			break;
3589145256Sjkoshy
3590147191Sjkoshy		if ((po = pmc_find_owner_descriptor(td->td_proc)) == NULL) {
3591145256Sjkoshy			error = EINVAL;
3592145256Sjkoshy			break;
3593145256Sjkoshy		}
3594145256Sjkoshy
3595147191Sjkoshy		if ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0) {
3596145774Sjkoshy			error = EINVAL;
3597145774Sjkoshy			break;
3598145774Sjkoshy		}
3599145774Sjkoshy
3600147191Sjkoshy		error = pmclog_process_userlog(po, &wl);
3601145256Sjkoshy	}
3602145256Sjkoshy	break;
3603145256Sjkoshy
3604147191Sjkoshy
3605145256Sjkoshy	default:
3606145256Sjkoshy		error = EINVAL;
3607145256Sjkoshy		break;
3608145256Sjkoshy	}
3609145256Sjkoshy
3610145256Sjkoshy	if (is_sx_downgraded)
3611145256Sjkoshy		sx_sunlock(&pmc_sx);
3612145256Sjkoshy	else
3613145256Sjkoshy		sx_xunlock(&pmc_sx);
3614145256Sjkoshy
3615145256Sjkoshy	if (error)
3616145256Sjkoshy		atomic_add_int(&pmc_stats.pm_syscall_errors, 1);
3617145256Sjkoshy
3618147191Sjkoshy	PICKUP_GIANT();
3619147191Sjkoshy
3620145256Sjkoshy	return error;
3621145256Sjkoshy}
3622145256Sjkoshy
3623145256Sjkoshy/*
3624145256Sjkoshy * Helper functions
3625145256Sjkoshy */
3626145256Sjkoshy
3627147191Sjkoshy
3628145256Sjkoshy/*
3629147191Sjkoshy * Interrupt processing.
3630147191Sjkoshy *
3631147191Sjkoshy * Find a free slot in the per-cpu array of PC samples and write the
3632147191Sjkoshy * current (PMC,PID,PC) triple to it.  If an event was successfully
3633147191Sjkoshy * added, a bit is set in mask 'pmc_cpumask' denoting that the
3634147191Sjkoshy * DO_SAMPLES hook needs to be invoked from the clock handler.
3635147191Sjkoshy *
3636147191Sjkoshy * This function is meant to be called from an NMI handler.  It cannot
3637147191Sjkoshy * use any of the locking primitives supplied by the OS.
3638145256Sjkoshy */
3639145256Sjkoshy
3640147191Sjkoshyint
3641147708Sjkoshypmc_process_interrupt(int cpu, struct pmc *pm, uintfptr_t pc, int usermode)
3642145256Sjkoshy{
3643147191Sjkoshy	int error, ri;
3644147191Sjkoshy	struct thread *td;
3645147191Sjkoshy	struct pmc_sample *ps;
3646147191Sjkoshy	struct pmc_samplebuffer *psb;
3647145256Sjkoshy
3648147191Sjkoshy	error = 0;
3649147191Sjkoshy	ri = PMC_TO_ROWINDEX(pm);
3650145256Sjkoshy
3651147191Sjkoshy	psb = pmc_pcpu[cpu]->pc_sb;
3652145256Sjkoshy
3653147191Sjkoshy	ps = psb->ps_write;
3654147191Sjkoshy	if (ps->ps_pc) {	/* in use, reader hasn't caught up */
3655147867Sjkoshy		pm->pm_stalled = 1;
3656147191Sjkoshy		atomic_add_int(&pmc_stats.pm_intr_bufferfull, 1);
3657147191Sjkoshy		PMCDBG(SAM,INT,1,"(spc) cpu=%d pm=%p pc=%jx um=%d wr=%d rd=%d",
3658147708Sjkoshy		    cpu, pm, (uint64_t) pc, usermode,
3659147191Sjkoshy		    (int) (psb->ps_write - psb->ps_samples),
3660147191Sjkoshy		    (int) (psb->ps_read - psb->ps_samples));
3661147191Sjkoshy		error = ENOMEM;
3662147191Sjkoshy		goto done;
3663147191Sjkoshy	}
3664145256Sjkoshy
3665147191Sjkoshy	/* fill in entry */
3666147191Sjkoshy	PMCDBG(SAM,INT,1,"cpu=%d pm=%p pc=%jx um=%d wr=%d rd=%d", cpu, pm,
3667147708Sjkoshy	    (uint64_t) pc, usermode,
3668147191Sjkoshy	    (int) (psb->ps_write - psb->ps_samples),
3669147191Sjkoshy	    (int) (psb->ps_read - psb->ps_samples));
3670145256Sjkoshy
3671147191Sjkoshy	atomic_add_rel_32(&pm->pm_runcount, 1);		/* hold onto PMC */
3672147191Sjkoshy	ps->ps_pmc = pm;
3673147191Sjkoshy	if ((td = curthread) && td->td_proc)
3674147191Sjkoshy		ps->ps_pid = td->td_proc->p_pid;
3675147191Sjkoshy	else
3676147191Sjkoshy		ps->ps_pid = -1;
3677147191Sjkoshy	ps->ps_usermode = usermode;
3678147191Sjkoshy	ps->ps_pc = pc;		/* mark entry as in use */
3679145256Sjkoshy
3680147191Sjkoshy	/* increment write pointer, modulo ring buffer size */
3681147191Sjkoshy	ps++;
3682147191Sjkoshy	if (ps == psb->ps_fence)
3683147191Sjkoshy		psb->ps_write = psb->ps_samples;
3684147191Sjkoshy	else
3685147191Sjkoshy		psb->ps_write = ps;
3686145256Sjkoshy
3687147191Sjkoshy done:
3688147191Sjkoshy	/* mark CPU as needing processing */
3689147191Sjkoshy	atomic_set_rel_int(&pmc_cpumask, (1 << cpu));
3690147191Sjkoshy
3691147191Sjkoshy	return error;
3692145256Sjkoshy}
3693145256Sjkoshy
3694147191Sjkoshy
3695145256Sjkoshy/*
3696147191Sjkoshy * Process saved PC samples.
3697145256Sjkoshy */
3698145256Sjkoshy
3699145256Sjkoshystatic void
3700147191Sjkoshypmc_process_samples(int cpu)
3701145256Sjkoshy{
3702147191Sjkoshy	int n, ri;
3703147191Sjkoshy	struct pmc *pm;
3704147191Sjkoshy	struct thread *td;
3705147191Sjkoshy	struct pmc_owner *po;
3706147191Sjkoshy	struct pmc_sample *ps;
3707147191Sjkoshy	struct pmc_samplebuffer *psb;
3708145256Sjkoshy
3709147191Sjkoshy	KASSERT(PCPU_GET(cpuid) == cpu,
3710147191Sjkoshy	    ("[pmc,%d] not on the correct CPU pcpu=%d cpu=%d", __LINE__,
3711147191Sjkoshy		PCPU_GET(cpuid), cpu));
3712145256Sjkoshy
3713147191Sjkoshy	psb = pmc_pcpu[cpu]->pc_sb;
3714147191Sjkoshy
3715147191Sjkoshy	for (n = 0; n < pmc_nsamples; n++) { /* bound on #iterations */
3716147191Sjkoshy
3717147191Sjkoshy		ps = psb->ps_read;
3718147191Sjkoshy		if (ps->ps_pc == (uintfptr_t) 0)	/* no data */
3719147191Sjkoshy			break;
3720147191Sjkoshy
3721147191Sjkoshy		pm = ps->ps_pmc;
3722147191Sjkoshy		po = pm->pm_owner;
3723147191Sjkoshy
3724147191Sjkoshy		KASSERT(PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)),
3725147191Sjkoshy		    ("[pmc,%d] pmc=%p non-sampling mode=%d", __LINE__,
3726147191Sjkoshy			pm, PMC_TO_MODE(pm)));
3727147191Sjkoshy
3728147191Sjkoshy		/* Ignore PMCs that have been switched off */
3729147191Sjkoshy		if (pm->pm_state != PMC_STATE_RUNNING)
3730147191Sjkoshy			goto entrydone;
3731147191Sjkoshy
3732147191Sjkoshy		PMCDBG(SAM,OPS,1,"cpu=%d pm=%p pc=%jx um=%d wr=%d rd=%d", cpu,
3733147708Sjkoshy		    pm, (uint64_t) ps->ps_pc, ps->ps_usermode,
3734147191Sjkoshy		    (int) (psb->ps_write - psb->ps_samples),
3735147191Sjkoshy		    (int) (psb->ps_read - psb->ps_samples));
3736147191Sjkoshy
3737147191Sjkoshy		/*
3738147191Sjkoshy		 * If this is a process-mode PMC that is attached to
3739147191Sjkoshy		 * its owner, and if the PC is in user mode, update
3740147191Sjkoshy		 * profiling statistics like timer-based profiling
3741147191Sjkoshy		 * would have done.
3742147191Sjkoshy		 */
3743147191Sjkoshy		if (pm->pm_flags & PMC_F_ATTACHED_TO_OWNER) {
3744147191Sjkoshy			if (ps->ps_usermode) {
3745147191Sjkoshy				td = FIRST_THREAD_IN_PROC(po->po_owner);
3746147191Sjkoshy				addupc_intr(td, ps->ps_pc, 1);
3747147191Sjkoshy			}
3748147191Sjkoshy			goto entrydone;
3749147191Sjkoshy		}
3750147191Sjkoshy
3751147191Sjkoshy		/*
3752147191Sjkoshy		 * Otherwise, this is either a sampling mode PMC that
3753147191Sjkoshy		 * is attached to a different process than its owner,
3754147191Sjkoshy		 * or a system-wide sampling PMC.  Dispatch a log
3755147191Sjkoshy		 * entry to the PMC's owner process.
3756147191Sjkoshy		 */
3757147191Sjkoshy
3758147191Sjkoshy		pmclog_process_pcsample(pm, ps);
3759147191Sjkoshy
3760147191Sjkoshy	entrydone:
3761147191Sjkoshy		ps->ps_pc = (uintfptr_t) 0;	/* mark entry as free */
3762147191Sjkoshy		atomic_subtract_rel_32(&pm->pm_runcount, 1);
3763147191Sjkoshy
3764147191Sjkoshy		/* increment read pointer, modulo sample size */
3765147191Sjkoshy		if (++ps == psb->ps_fence)
3766147191Sjkoshy			psb->ps_read = psb->ps_samples;
3767147191Sjkoshy		else
3768147191Sjkoshy			psb->ps_read = ps;
3769147191Sjkoshy	}
3770147191Sjkoshy
3771147191Sjkoshy	atomic_add_int(&pmc_stats.pm_log_sweeps, 1);
3772147191Sjkoshy
3773147191Sjkoshy	/* Do not re-enable stalled PMCs if we failed to process any samples */
3774147191Sjkoshy	if (n == 0)
3775147191Sjkoshy		return;
3776147191Sjkoshy
3777147191Sjkoshy	/*
3778147191Sjkoshy	 * Restart any stalled sampling PMCs on this CPU.
3779147191Sjkoshy	 *
3780147867Sjkoshy	 * If the NMI handler sets the pm_stalled field of a PMC after
3781147867Sjkoshy	 * the check below, we'll end up processing the stalled PMC at
3782147867Sjkoshy	 * the next hardclock tick.
3783147191Sjkoshy	 */
3784147191Sjkoshy	for (n = 0; n < md->pmd_npmc; n++) {
3785147191Sjkoshy		(void) (*md->pmd_get_config)(cpu,n,&pm);
3786147191Sjkoshy		if (pm == NULL ||			 /* !cfg'ed */
3787147191Sjkoshy		    pm->pm_state != PMC_STATE_RUNNING || /* !active */
3788147191Sjkoshy		    !PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)) || /* !sampling */
3789147867Sjkoshy		    pm->pm_stalled == 0) /* !stalled */
3790147191Sjkoshy			continue;
3791147191Sjkoshy
3792147867Sjkoshy		pm->pm_stalled = 0;
3793147191Sjkoshy		ri = PMC_TO_ROWINDEX(pm);
3794147191Sjkoshy		(*md->pmd_start_pmc)(cpu, ri);
3795147191Sjkoshy	}
3796145256Sjkoshy}
3797145256Sjkoshy
3798145256Sjkoshy/*
3799145256Sjkoshy * Event handlers.
3800145256Sjkoshy */
3801145256Sjkoshy
3802145256Sjkoshy/*
3803145256Sjkoshy * Handle a process exit.
3804145256Sjkoshy *
3805147191Sjkoshy * Remove this process from all hash tables.  If this process
3806147191Sjkoshy * owned any PMCs, turn off those PMCs and deallocate them,
3807147191Sjkoshy * removing any associations with target processes.
3808147191Sjkoshy *
3809147191Sjkoshy * This function will be called by the last 'thread' of a
3810147191Sjkoshy * process.
3811147191Sjkoshy *
3812145256Sjkoshy * XXX This eventhandler gets called early in the exit process.
3813145256Sjkoshy * Consider using a 'hook' invocation from thread_exit() or equivalent
3814145256Sjkoshy * spot.  Another negative is that kse_exit doesn't seem to call
3815145256Sjkoshy * exit1() [??].
3816147191Sjkoshy *
3817145256Sjkoshy */
3818145256Sjkoshy
3819145256Sjkoshystatic void
3820145256Sjkoshypmc_process_exit(void *arg __unused, struct proc *p)
3821145256Sjkoshy{
3822145256Sjkoshy	int is_using_hwpmcs;
3823147191Sjkoshy	int cpu;
3824147191Sjkoshy	unsigned int ri;
3825147191Sjkoshy	struct pmc *pm;
3826147191Sjkoshy	struct pmc_process *pp;
3827147191Sjkoshy	struct pmc_owner *po;
3828147191Sjkoshy	pmc_value_t newvalue, tmp;
3829145256Sjkoshy
3830145256Sjkoshy	PROC_LOCK(p);
3831145256Sjkoshy	is_using_hwpmcs = p->p_flag & P_HWPMC;
3832145256Sjkoshy	PROC_UNLOCK(p);
3833145256Sjkoshy
3834147191Sjkoshy	/*
3835147191Sjkoshy	 * Log a sysexit event to all SS PMC owners.
3836147191Sjkoshy	 */
3837147191Sjkoshy	LIST_FOREACH(po, &pmc_ss_owners, po_ssnext)
3838147191Sjkoshy	    if (po->po_flags & PMC_PO_OWNS_LOGFILE)
3839147191Sjkoshy		    pmclog_process_sysexit(po, p->p_pid);
3840145256Sjkoshy
3841147191Sjkoshy	if (!is_using_hwpmcs)
3842147191Sjkoshy		return;
3843147191Sjkoshy
3844147191Sjkoshy	PMC_GET_SX_XLOCK();
3845147191Sjkoshy	PMCDBG(PRC,EXT,1,"process-exit proc=%p (%d, %s)", p, p->p_pid,
3846147191Sjkoshy	    p->p_comm);
3847147191Sjkoshy
3848147191Sjkoshy	/*
3849147191Sjkoshy	 * Since this code is invoked by the last thread in an exiting
3850147191Sjkoshy	 * process, we would have context switched IN at some prior
3851147191Sjkoshy	 * point.  However, with PREEMPTION, kernel mode context
3852147191Sjkoshy	 * switches may happen any time, so we want to disable a
3853147191Sjkoshy	 * context switch OUT till we get any PMCs targetting this
3854147191Sjkoshy	 * process off the hardware.
3855147191Sjkoshy	 *
3856147191Sjkoshy	 * We also need to atomically remove this process'
3857147191Sjkoshy	 * entry from our target process hash table, using
3858147191Sjkoshy	 * PMC_FLAG_REMOVE.
3859147191Sjkoshy	 */
3860147191Sjkoshy	PMCDBG(PRC,EXT,1, "process-exit proc=%p (%d, %s)", p, p->p_pid,
3861147191Sjkoshy	    p->p_comm);
3862147191Sjkoshy
3863147191Sjkoshy	critical_enter(); /* no preemption */
3864147191Sjkoshy
3865147191Sjkoshy	cpu = curthread->td_oncpu;
3866147191Sjkoshy
3867147191Sjkoshy	if ((pp = pmc_find_process_descriptor(p,
3868147191Sjkoshy		 PMC_FLAG_REMOVE)) != NULL) {
3869147191Sjkoshy
3870147191Sjkoshy		PMCDBG(PRC,EXT,2,
3871147191Sjkoshy		    "process-exit proc=%p pmc-process=%p", p, pp);
3872147191Sjkoshy
3873147191Sjkoshy		/*
3874147191Sjkoshy		 * The exiting process could the target of
3875147191Sjkoshy		 * some PMCs which will be running on
3876147191Sjkoshy		 * currently executing CPU.
3877147191Sjkoshy		 *
3878147191Sjkoshy		 * We need to turn these PMCs off like we
3879147191Sjkoshy		 * would do at context switch OUT time.
3880147191Sjkoshy		 */
3881147191Sjkoshy		for (ri = 0; ri < md->pmd_npmc; ri++) {
3882147191Sjkoshy
3883147191Sjkoshy			/*
3884147191Sjkoshy			 * Pick up the pmc pointer from hardware
3885147191Sjkoshy			 * state similar to the CSW_OUT code.
3886147191Sjkoshy			 */
3887147191Sjkoshy			pm = NULL;
3888147191Sjkoshy			(void) (*md->pmd_get_config)(cpu, ri, &pm);
3889147191Sjkoshy
3890147191Sjkoshy			PMCDBG(PRC,EXT,2, "ri=%d pm=%p", ri, pm);
3891147191Sjkoshy
3892147191Sjkoshy			if (pm == NULL ||
3893147191Sjkoshy			    !PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm)))
3894147191Sjkoshy				continue;
3895147191Sjkoshy
3896147191Sjkoshy			PMCDBG(PRC,EXT,2, "ppmcs[%d]=%p pm=%p "
3897147191Sjkoshy			    "state=%d", ri, pp->pp_pmcs[ri].pp_pmc,
3898147191Sjkoshy			    pm, pm->pm_state);
3899147191Sjkoshy
3900147191Sjkoshy			KASSERT(PMC_TO_ROWINDEX(pm) == ri,
3901147191Sjkoshy			    ("[pmc,%d] ri mismatch pmc(%d) ri(%d)",
3902147191Sjkoshy				__LINE__, PMC_TO_ROWINDEX(pm), ri));
3903147191Sjkoshy
3904147191Sjkoshy			KASSERT(pm == pp->pp_pmcs[ri].pp_pmc,
3905147191Sjkoshy			    ("[pmc,%d] pm %p != pp_pmcs[%d] %p",
3906147191Sjkoshy				__LINE__, pm, ri, pp->pp_pmcs[ri].pp_pmc));
3907147191Sjkoshy
3908147191Sjkoshy			(void) md->pmd_stop_pmc(cpu, ri);
3909147191Sjkoshy
3910147191Sjkoshy			KASSERT(pm->pm_runcount > 0,
3911147191Sjkoshy			    ("[pmc,%d] bad runcount ri %d rc %d",
3912147191Sjkoshy				__LINE__, ri, pm->pm_runcount));
3913147191Sjkoshy
3914147867Sjkoshy			/* Stop hardware only if it is actually running */
3915147191Sjkoshy			if (pm->pm_state == PMC_STATE_RUNNING &&
3916147867Sjkoshy			    pm->pm_stalled == 0) {
3917147191Sjkoshy				md->pmd_read_pmc(cpu, ri, &newvalue);
3918147191Sjkoshy				tmp = newvalue -
3919147191Sjkoshy				    PMC_PCPU_SAVED(cpu,ri);
3920147191Sjkoshy
3921147191Sjkoshy				mtx_pool_lock_spin(pmc_mtxpool, pm);
3922147191Sjkoshy				pm->pm_gv.pm_savedvalue += tmp;
3923147191Sjkoshy				pp->pp_pmcs[ri].pp_pmcval += tmp;
3924147191Sjkoshy				mtx_pool_unlock_spin(pmc_mtxpool, pm);
3925147191Sjkoshy			}
3926147191Sjkoshy
3927147191Sjkoshy			atomic_subtract_rel_32(&pm->pm_runcount,1);
3928147191Sjkoshy
3929147191Sjkoshy			KASSERT((int) pm->pm_runcount >= 0,
3930147191Sjkoshy			    ("[pmc,%d] runcount is %d", __LINE__, ri));
3931147191Sjkoshy
3932147191Sjkoshy			(void) md->pmd_config_pmc(cpu, ri, NULL);
3933147191Sjkoshy		}
3934147191Sjkoshy
3935147191Sjkoshy		/*
3936147191Sjkoshy		 * Inform the MD layer of this pseudo "context switch
3937147191Sjkoshy		 * out"
3938147191Sjkoshy		 */
3939147191Sjkoshy		(void) md->pmd_switch_out(pmc_pcpu[cpu], pp);
3940147191Sjkoshy
3941147191Sjkoshy		critical_exit(); /* ok to be pre-empted now */
3942147191Sjkoshy
3943147191Sjkoshy		/*
3944147191Sjkoshy		 * Unlink this process from the PMCs that are
3945147191Sjkoshy		 * targetting it.  This will send a signal to
3946147191Sjkoshy		 * all PMC owner's whose PMCs are orphaned.
3947147191Sjkoshy		 *
3948147191Sjkoshy		 * Log PMC value at exit time if requested.
3949147191Sjkoshy		 */
3950147191Sjkoshy		for (ri = 0; ri < md->pmd_npmc; ri++)
3951147191Sjkoshy			if ((pm = pp->pp_pmcs[ri].pp_pmc) != NULL) {
3952147867Sjkoshy				if (pm->pm_flags & PMC_F_NEEDS_LOGFILE &&
3953147867Sjkoshy				    PMC_IS_COUNTING_MODE(PMC_TO_MODE(pm)))
3954147191Sjkoshy					pmclog_process_procexit(pm, pp);
3955147191Sjkoshy				pmc_unlink_target_process(pm, pp);
3956147191Sjkoshy			}
3957147191Sjkoshy		FREE(pp, M_PMC);
3958147191Sjkoshy
3959147191Sjkoshy	} else
3960147191Sjkoshy		critical_exit(); /* pp == NULL */
3961147191Sjkoshy
3962147191Sjkoshy
3963147191Sjkoshy	/*
3964147191Sjkoshy	 * If the process owned PMCs, free them up and free up
3965147191Sjkoshy	 * memory.
3966147191Sjkoshy	 */
3967147191Sjkoshy	if ((po = pmc_find_owner_descriptor(p)) != NULL) {
3968147191Sjkoshy		pmc_remove_owner(po);
3969147191Sjkoshy		pmc_destroy_owner_descriptor(po);
3970145256Sjkoshy	}
3971147191Sjkoshy
3972147191Sjkoshy	sx_xunlock(&pmc_sx);
3973145256Sjkoshy}
3974145256Sjkoshy
3975145256Sjkoshy/*
3976145256Sjkoshy * Handle a process fork.
3977145256Sjkoshy *
3978145256Sjkoshy * If the parent process 'p1' is under HWPMC monitoring, then copy
3979145256Sjkoshy * over any attached PMCs that have 'do_descendants' semantics.
3980145256Sjkoshy */
3981145256Sjkoshy
3982145256Sjkoshystatic void
3983147191Sjkoshypmc_process_fork(void *arg __unused, struct proc *p1, struct proc *newproc,
3984145256Sjkoshy    int flags)
3985145256Sjkoshy{
3986145256Sjkoshy	int is_using_hwpmcs;
3987147191Sjkoshy	unsigned int ri;
3988147191Sjkoshy	uint32_t do_descendants;
3989147191Sjkoshy	struct pmc *pm;
3990147191Sjkoshy	struct pmc_owner *po;
3991147191Sjkoshy	struct pmc_process *ppnew, *ppold;
3992145256Sjkoshy
3993145256Sjkoshy	(void) flags;		/* unused parameter */
3994145256Sjkoshy
3995145256Sjkoshy	PROC_LOCK(p1);
3996145256Sjkoshy	is_using_hwpmcs = p1->p_flag & P_HWPMC;
3997145256Sjkoshy	PROC_UNLOCK(p1);
3998145256Sjkoshy
3999147191Sjkoshy	/*
4000147191Sjkoshy	 * If there are system-wide sampling PMCs active, we need to
4001147191Sjkoshy	 * log all fork events to their owner's logs.
4002147191Sjkoshy	 */
4003147191Sjkoshy
4004147191Sjkoshy	LIST_FOREACH(po, &pmc_ss_owners, po_ssnext)
4005147191Sjkoshy	    if (po->po_flags & PMC_PO_OWNS_LOGFILE)
4006147191Sjkoshy		    pmclog_process_procfork(po, p1->p_pid, newproc->p_pid);
4007147191Sjkoshy
4008147191Sjkoshy	if (!is_using_hwpmcs)
4009147191Sjkoshy		return;
4010147191Sjkoshy
4011147191Sjkoshy	PMC_GET_SX_XLOCK();
4012147191Sjkoshy	PMCDBG(PMC,FRK,1, "process-fork proc=%p (%d, %s) -> %p", p1,
4013147191Sjkoshy	    p1->p_pid, p1->p_comm, newproc);
4014147191Sjkoshy
4015147191Sjkoshy	/*
4016147191Sjkoshy	 * If the parent process (curthread->td_proc) is a
4017147191Sjkoshy	 * target of any PMCs, look for PMCs that are to be
4018147191Sjkoshy	 * inherited, and link these into the new process
4019147191Sjkoshy	 * descriptor.
4020147191Sjkoshy	 */
4021147191Sjkoshy	if ((ppold = pmc_find_process_descriptor(curthread->td_proc,
4022147191Sjkoshy		 PMC_FLAG_NONE)) == NULL)
4023147191Sjkoshy		goto done;		/* nothing to do */
4024147191Sjkoshy
4025147191Sjkoshy	do_descendants = 0;
4026147191Sjkoshy	for (ri = 0; ri < md->pmd_npmc; ri++)
4027147191Sjkoshy		if ((pm = ppold->pp_pmcs[ri].pp_pmc) != NULL)
4028147191Sjkoshy			do_descendants |= pm->pm_flags & PMC_F_DESCENDANTS;
4029147191Sjkoshy	if (do_descendants == 0) /* nothing to do */
4030147191Sjkoshy		goto done;
4031147191Sjkoshy
4032147191Sjkoshy	/* allocate a descriptor for the new process  */
4033147191Sjkoshy	if ((ppnew = pmc_find_process_descriptor(newproc,
4034147191Sjkoshy		 PMC_FLAG_ALLOCATE)) == NULL)
4035147191Sjkoshy		goto done;
4036147191Sjkoshy
4037147191Sjkoshy	/*
4038147191Sjkoshy	 * Run through all PMCs that were targeting the old process
4039147191Sjkoshy	 * and which specified F_DESCENDANTS and attach them to the
4040147191Sjkoshy	 * new process.
4041147191Sjkoshy	 *
4042147191Sjkoshy	 * Log the fork event to all owners of PMCs attached to this
4043147191Sjkoshy	 * process, if not already logged.
4044147191Sjkoshy	 */
4045147191Sjkoshy	for (ri = 0; ri < md->pmd_npmc; ri++)
4046147191Sjkoshy		if ((pm = ppold->pp_pmcs[ri].pp_pmc) != NULL &&
4047147191Sjkoshy		    (pm->pm_flags & PMC_F_DESCENDANTS)) {
4048147191Sjkoshy			pmc_link_target_process(pm, ppnew);
4049147191Sjkoshy			po = pm->pm_owner;
4050147191Sjkoshy			if (po->po_sscount == 0 &&
4051147191Sjkoshy			    po->po_flags & PMC_PO_OWNS_LOGFILE)
4052147191Sjkoshy				pmclog_process_procfork(po, p1->p_pid,
4053147191Sjkoshy				    newproc->p_pid);
4054147191Sjkoshy		}
4055147191Sjkoshy
4056147191Sjkoshy	/*
4057147191Sjkoshy	 * Now mark the new process as being tracked by this driver.
4058147191Sjkoshy	 */
4059147191Sjkoshy	PROC_LOCK(newproc);
4060147191Sjkoshy	newproc->p_flag |= P_HWPMC;
4061147191Sjkoshy	PROC_UNLOCK(newproc);
4062147191Sjkoshy
4063147191Sjkoshy done:
4064147191Sjkoshy	sx_xunlock(&pmc_sx);
4065145256Sjkoshy}
4066145256Sjkoshy
4067145256Sjkoshy
4068145256Sjkoshy/*
4069145256Sjkoshy * initialization
4070145256Sjkoshy */
4071145256Sjkoshy
4072145256Sjkoshystatic const char *pmc_name_of_pmcclass[] = {
4073145256Sjkoshy#undef	__PMC_CLASS
4074145256Sjkoshy#define	__PMC_CLASS(N) #N ,
4075145256Sjkoshy	__PMC_CLASSES()
4076145256Sjkoshy};
4077145256Sjkoshy
4078145256Sjkoshystatic int
4079145256Sjkoshypmc_initialize(void)
4080145256Sjkoshy{
4081147191Sjkoshy	int cpu, error, n;
4082145256Sjkoshy	struct pmc_binding pb;
4083147191Sjkoshy	struct pmc_samplebuffer *sb;
4084145256Sjkoshy
4085145256Sjkoshy	md = NULL;
4086145256Sjkoshy	error = 0;
4087145256Sjkoshy
4088153110Sru#ifdef	DEBUG
4089145256Sjkoshy	/* parse debug flags first */
4090145256Sjkoshy	if (TUNABLE_STR_FETCH(PMC_SYSCTL_NAME_PREFIX "debugflags",
4091145256Sjkoshy		pmc_debugstr, sizeof(pmc_debugstr)))
4092145256Sjkoshy		pmc_debugflags_parse(pmc_debugstr,
4093145256Sjkoshy		    pmc_debugstr+strlen(pmc_debugstr));
4094145256Sjkoshy#endif
4095145256Sjkoshy
4096145256Sjkoshy	PMCDBG(MOD,INI,0, "PMC Initialize (version %x)", PMC_VERSION);
4097145256Sjkoshy
4098148562Sjkoshy	/* check kernel version */
4099148562Sjkoshy	if (pmc_kernel_version != PMC_VERSION) {
4100148562Sjkoshy		if (pmc_kernel_version == 0)
4101148562Sjkoshy			printf("hwpmc: this kernel has not been compiled with "
4102148562Sjkoshy			    "'options HWPMC_HOOKS'.\n");
4103148562Sjkoshy		else
4104148562Sjkoshy			printf("hwpmc: kernel version (0x%x) does not match "
4105148562Sjkoshy			    "module version (0x%x).\n", pmc_kernel_version,
4106148562Sjkoshy			    PMC_VERSION);
4107148562Sjkoshy		return EPROGMISMATCH;
4108148562Sjkoshy	}
4109148562Sjkoshy
4110145256Sjkoshy	/*
4111145256Sjkoshy	 * check sysctl parameters
4112145256Sjkoshy	 */
4113145256Sjkoshy
4114145256Sjkoshy	if (pmc_hashsize <= 0) {
4115147191Sjkoshy		(void) printf("hwpmc: tunable hashsize=%d must be greater "
4116147191Sjkoshy		    "than zero.\n", pmc_hashsize);
4117145256Sjkoshy		pmc_hashsize = PMC_HASH_SIZE;
4118145256Sjkoshy	}
4119145256Sjkoshy
4120147191Sjkoshy	if (pmc_nsamples <= 0 || pmc_nsamples > 65535) {
4121153735Sjkoshy		(void) printf("hwpmc: tunable nsamples=%d out of range.\n",
4122153735Sjkoshy		    pmc_nsamples);
4123147191Sjkoshy		pmc_nsamples = PMC_NSAMPLES;
4124147191Sjkoshy	}
4125145256Sjkoshy
4126147191Sjkoshy	md = pmc_md_initialize();
4127147191Sjkoshy
4128145256Sjkoshy	if (md == NULL || md->pmd_init == NULL)
4129145256Sjkoshy		return ENOSYS;
4130145256Sjkoshy
4131145256Sjkoshy	/* allocate space for the per-cpu array */
4132145256Sjkoshy	MALLOC(pmc_pcpu, struct pmc_cpu **, mp_ncpus * sizeof(struct pmc_cpu *),
4133145256Sjkoshy	    M_PMC, M_WAITOK|M_ZERO);
4134145256Sjkoshy
4135145256Sjkoshy	/* per-cpu 'saved values' for managing process-mode PMCs */
4136145256Sjkoshy	MALLOC(pmc_pcpu_saved, pmc_value_t *,
4137145256Sjkoshy	    sizeof(pmc_value_t) * mp_ncpus * md->pmd_npmc, M_PMC, M_WAITOK);
4138145256Sjkoshy
4139145256Sjkoshy	/* perform cpu dependent initialization */
4140145256Sjkoshy	pmc_save_cpu_binding(&pb);
4141145256Sjkoshy	for (cpu = 0; cpu < mp_ncpus; cpu++) {
4142145256Sjkoshy		if (pmc_cpu_is_disabled(cpu))
4143145256Sjkoshy			continue;
4144145256Sjkoshy		pmc_select_cpu(cpu);
4145145256Sjkoshy		if ((error = md->pmd_init(cpu)) != 0)
4146145256Sjkoshy			break;
4147145256Sjkoshy	}
4148145256Sjkoshy	pmc_restore_cpu_binding(&pb);
4149145256Sjkoshy
4150145256Sjkoshy	if (error != 0)
4151145256Sjkoshy		return error;
4152145256Sjkoshy
4153147191Sjkoshy	/* allocate space for the sample array */
4154147191Sjkoshy	for (cpu = 0; cpu < mp_ncpus; cpu++) {
4155147191Sjkoshy		if (pmc_cpu_is_disabled(cpu))
4156147191Sjkoshy			continue;
4157147191Sjkoshy		MALLOC(sb, struct pmc_samplebuffer *,
4158147191Sjkoshy		    sizeof(struct pmc_samplebuffer) +
4159147191Sjkoshy		    pmc_nsamples * sizeof(struct pmc_sample), M_PMC,
4160147191Sjkoshy		    M_WAITOK|M_ZERO);
4161147191Sjkoshy
4162147191Sjkoshy		sb->ps_read = sb->ps_write = sb->ps_samples;
4163153735Sjkoshy		sb->ps_fence = sb->ps_samples + pmc_nsamples;
4164147191Sjkoshy		KASSERT(pmc_pcpu[cpu] != NULL,
4165147191Sjkoshy		    ("[pmc,%d] cpu=%d Null per-cpu data", __LINE__, cpu));
4166147191Sjkoshy
4167147191Sjkoshy		pmc_pcpu[cpu]->pc_sb = sb;
4168147191Sjkoshy	}
4169147191Sjkoshy
4170145256Sjkoshy	/* allocate space for the row disposition array */
4171145256Sjkoshy	pmc_pmcdisp = malloc(sizeof(enum pmc_mode) * md->pmd_npmc,
4172145256Sjkoshy	    M_PMC, M_WAITOK|M_ZERO);
4173145256Sjkoshy
4174145256Sjkoshy	KASSERT(pmc_pmcdisp != NULL,
4175145256Sjkoshy	    ("[pmc,%d] pmcdisp allocation returned NULL", __LINE__));
4176145256Sjkoshy
4177145256Sjkoshy	/* mark all PMCs as available */
4178145256Sjkoshy	for (n = 0; n < (int) md->pmd_npmc; n++)
4179145256Sjkoshy		PMC_MARK_ROW_FREE(n);
4180145256Sjkoshy
4181145256Sjkoshy	/* allocate thread hash tables */
4182145256Sjkoshy	pmc_ownerhash = hashinit(pmc_hashsize, M_PMC,
4183145256Sjkoshy	    &pmc_ownerhashmask);
4184145256Sjkoshy
4185145256Sjkoshy	pmc_processhash = hashinit(pmc_hashsize, M_PMC,
4186145256Sjkoshy	    &pmc_processhashmask);
4187145256Sjkoshy	mtx_init(&pmc_processhash_mtx, "pmc-process-hash", "pmc", MTX_SPIN);
4188145256Sjkoshy
4189147191Sjkoshy	LIST_INIT(&pmc_ss_owners);
4190147191Sjkoshy	pmc_ss_count = 0;
4191147191Sjkoshy
4192145256Sjkoshy	/* allocate a pool of spin mutexes */
4193145256Sjkoshy	pmc_mtxpool = mtx_pool_create("pmc", pmc_mtxpool_size, MTX_SPIN);
4194145256Sjkoshy
4195145256Sjkoshy	PMCDBG(MOD,INI,1, "pmc_ownerhash=%p, mask=0x%lx "
4196145256Sjkoshy	    "targethash=%p mask=0x%lx", pmc_ownerhash, pmc_ownerhashmask,
4197145256Sjkoshy	    pmc_processhash, pmc_processhashmask);
4198145256Sjkoshy
4199145256Sjkoshy	/* register process {exit,fork,exec} handlers */
4200145256Sjkoshy	pmc_exit_tag = EVENTHANDLER_REGISTER(process_exit,
4201145256Sjkoshy	    pmc_process_exit, NULL, EVENTHANDLER_PRI_ANY);
4202145256Sjkoshy	pmc_fork_tag = EVENTHANDLER_REGISTER(process_fork,
4203145256Sjkoshy	    pmc_process_fork, NULL, EVENTHANDLER_PRI_ANY);
4204145256Sjkoshy
4205147191Sjkoshy	/* initialize logging */
4206147191Sjkoshy	pmclog_initialize();
4207147191Sjkoshy
4208145256Sjkoshy	/* set hook functions */
4209145256Sjkoshy	pmc_intr = md->pmd_intr;
4210145256Sjkoshy	pmc_hook = pmc_hook_handler;
4211145256Sjkoshy
4212145256Sjkoshy	if (error == 0) {
4213145256Sjkoshy		printf(PMC_MODULE_NAME ":");
4214149373Sjkoshy		for (n = 0; n < (int) md->pmd_nclass; n++) {
4215149373Sjkoshy			printf(" %s/%d/0x%b",
4216145774Sjkoshy			    pmc_name_of_pmcclass[md->pmd_classes[n].pm_class],
4217149373Sjkoshy			    md->pmd_nclasspmcs[n],
4218149373Sjkoshy			    md->pmd_classes[n].pm_caps,
4219149373Sjkoshy			    "\20"
4220149373Sjkoshy			    "\1INT\2USR\3SYS\4EDG\5THR"
4221149373Sjkoshy			    "\6REA\7WRI\10INV\11QUA\12PRC"
4222149373Sjkoshy			    "\13TAG\14CSC");
4223149373Sjkoshy		}
4224145256Sjkoshy		printf("\n");
4225145256Sjkoshy	}
4226145256Sjkoshy
4227145256Sjkoshy	return error;
4228145256Sjkoshy}
4229145256Sjkoshy
4230145256Sjkoshy/* prepare to be unloaded */
4231145256Sjkoshystatic void
4232145256Sjkoshypmc_cleanup(void)
4233145256Sjkoshy{
4234145256Sjkoshy	int cpu;
4235145256Sjkoshy	struct pmc_ownerhash *ph;
4236145256Sjkoshy	struct pmc_owner *po, *tmp;
4237145256Sjkoshy	struct pmc_binding pb;
4238153110Sru#ifdef	DEBUG
4239145256Sjkoshy	struct pmc_processhash *prh;
4240145256Sjkoshy#endif
4241145256Sjkoshy
4242145256Sjkoshy	PMCDBG(MOD,INI,0, "%s", "cleanup");
4243145256Sjkoshy
4244147191Sjkoshy	/* switch off sampling */
4245147191Sjkoshy	atomic_store_rel_int(&pmc_cpumask, 0);
4246147191Sjkoshy	pmc_intr = NULL;
4247145256Sjkoshy
4248145256Sjkoshy	sx_xlock(&pmc_sx);
4249145256Sjkoshy	if (pmc_hook == NULL) {	/* being unloaded already */
4250145256Sjkoshy		sx_xunlock(&pmc_sx);
4251145256Sjkoshy		return;
4252145256Sjkoshy	}
4253145256Sjkoshy
4254145256Sjkoshy	pmc_hook = NULL; /* prevent new threads from entering module */
4255145256Sjkoshy
4256145256Sjkoshy	/* deregister event handlers */
4257145256Sjkoshy	EVENTHANDLER_DEREGISTER(process_fork, pmc_fork_tag);
4258145256Sjkoshy	EVENTHANDLER_DEREGISTER(process_exit, pmc_exit_tag);
4259145256Sjkoshy
4260145256Sjkoshy	/* send SIGBUS to all owner threads, free up allocations */
4261145256Sjkoshy	if (pmc_ownerhash)
4262145256Sjkoshy		for (ph = pmc_ownerhash;
4263145256Sjkoshy		     ph <= &pmc_ownerhash[pmc_ownerhashmask];
4264145256Sjkoshy		     ph++) {
4265145256Sjkoshy			LIST_FOREACH_SAFE(po, ph, po_next, tmp) {
4266145256Sjkoshy				pmc_remove_owner(po);
4267145256Sjkoshy
4268145256Sjkoshy				/* send SIGBUS to owner processes */
4269145256Sjkoshy				PMCDBG(MOD,INI,2, "cleanup signal proc=%p "
4270145256Sjkoshy				    "(%d, %s)", po->po_owner,
4271145256Sjkoshy				    po->po_owner->p_pid,
4272145256Sjkoshy				    po->po_owner->p_comm);
4273145256Sjkoshy
4274145256Sjkoshy				PROC_LOCK(po->po_owner);
4275145256Sjkoshy				psignal(po->po_owner, SIGBUS);
4276145256Sjkoshy				PROC_UNLOCK(po->po_owner);
4277147191Sjkoshy
4278147191Sjkoshy				pmc_destroy_owner_descriptor(po);
4279145256Sjkoshy			}
4280145256Sjkoshy		}
4281145256Sjkoshy
4282145256Sjkoshy	/* reclaim allocated data structures */
4283145256Sjkoshy	if (pmc_mtxpool)
4284145256Sjkoshy		mtx_pool_destroy(&pmc_mtxpool);
4285145256Sjkoshy
4286145256Sjkoshy	mtx_destroy(&pmc_processhash_mtx);
4287145256Sjkoshy	if (pmc_processhash) {
4288153110Sru#ifdef	DEBUG
4289145256Sjkoshy		struct pmc_process *pp;
4290145256Sjkoshy
4291145256Sjkoshy		PMCDBG(MOD,INI,3, "%s", "destroy process hash");
4292145256Sjkoshy		for (prh = pmc_processhash;
4293145256Sjkoshy		     prh <= &pmc_processhash[pmc_processhashmask];
4294145256Sjkoshy		     prh++)
4295145256Sjkoshy			LIST_FOREACH(pp, prh, pp_next)
4296145256Sjkoshy			    PMCDBG(MOD,INI,3, "pid=%d", pp->pp_proc->p_pid);
4297145256Sjkoshy#endif
4298145256Sjkoshy
4299145256Sjkoshy		hashdestroy(pmc_processhash, M_PMC, pmc_processhashmask);
4300145256Sjkoshy		pmc_processhash = NULL;
4301145256Sjkoshy	}
4302145256Sjkoshy
4303145256Sjkoshy	if (pmc_ownerhash) {
4304145256Sjkoshy		PMCDBG(MOD,INI,3, "%s", "destroy owner hash");
4305145256Sjkoshy		hashdestroy(pmc_ownerhash, M_PMC, pmc_ownerhashmask);
4306145256Sjkoshy		pmc_ownerhash = NULL;
4307145256Sjkoshy	}
4308145256Sjkoshy
4309147191Sjkoshy	KASSERT(LIST_EMPTY(&pmc_ss_owners),
4310147191Sjkoshy	    ("[pmc,%d] Global SS owner list not empty", __LINE__));
4311147191Sjkoshy	KASSERT(pmc_ss_count == 0,
4312147191Sjkoshy	    ("[pmc,%d] Global SS count not empty", __LINE__));
4313147191Sjkoshy
4314153735Sjkoshy	/* free the per-cpu sample buffers */
4315153735Sjkoshy	for (cpu = 0; cpu < mp_ncpus; cpu++) {
4316153735Sjkoshy		if (pmc_cpu_is_disabled(cpu))
4317153735Sjkoshy			continue;
4318153735Sjkoshy		KASSERT(pmc_pcpu[cpu]->pc_sb != NULL,
4319153735Sjkoshy		    ("[pmc,%d] Null cpu sample buffer cpu=%d", __LINE__,
4320153735Sjkoshy			cpu));
4321153735Sjkoshy		FREE(pmc_pcpu[cpu]->pc_sb, M_PMC);
4322153735Sjkoshy		pmc_pcpu[cpu]->pc_sb = NULL;
4323153735Sjkoshy	}
4324153735Sjkoshy
4325145256Sjkoshy 	/* do processor dependent cleanup */
4326145256Sjkoshy	PMCDBG(MOD,INI,3, "%s", "md cleanup");
4327145256Sjkoshy	if (md) {
4328145256Sjkoshy		pmc_save_cpu_binding(&pb);
4329145256Sjkoshy		for (cpu = 0; cpu < mp_ncpus; cpu++) {
4330145256Sjkoshy			PMCDBG(MOD,INI,1,"pmc-cleanup cpu=%d pcs=%p",
4331145256Sjkoshy			    cpu, pmc_pcpu[cpu]);
4332145256Sjkoshy			if (pmc_cpu_is_disabled(cpu))
4333145256Sjkoshy				continue;
4334145256Sjkoshy			pmc_select_cpu(cpu);
4335145256Sjkoshy			if (pmc_pcpu[cpu])
4336145256Sjkoshy				(void) md->pmd_cleanup(cpu);
4337145256Sjkoshy		}
4338145256Sjkoshy		FREE(md, M_PMC);
4339145256Sjkoshy		md = NULL;
4340145256Sjkoshy		pmc_restore_cpu_binding(&pb);
4341145256Sjkoshy	}
4342145256Sjkoshy
4343145256Sjkoshy	/* deallocate per-cpu structures */
4344145256Sjkoshy	FREE(pmc_pcpu, M_PMC);
4345145256Sjkoshy	pmc_pcpu = NULL;
4346145256Sjkoshy
4347145256Sjkoshy	FREE(pmc_pcpu_saved, M_PMC);
4348145256Sjkoshy	pmc_pcpu_saved = NULL;
4349145256Sjkoshy
4350145256Sjkoshy	if (pmc_pmcdisp) {
4351145256Sjkoshy		FREE(pmc_pmcdisp, M_PMC);
4352145256Sjkoshy		pmc_pmcdisp = NULL;
4353145256Sjkoshy	}
4354145256Sjkoshy
4355147191Sjkoshy	pmclog_shutdown();
4356147191Sjkoshy
4357145256Sjkoshy	sx_xunlock(&pmc_sx); 	/* we are done */
4358145256Sjkoshy}
4359145256Sjkoshy
4360145256Sjkoshy/*
4361145256Sjkoshy * The function called at load/unload.
4362145256Sjkoshy */
4363145256Sjkoshy
4364145256Sjkoshystatic int
4365145256Sjkoshyload (struct module *module __unused, int cmd, void *arg __unused)
4366145256Sjkoshy{
4367145256Sjkoshy	int error;
4368145256Sjkoshy
4369145256Sjkoshy	error = 0;
4370145256Sjkoshy
4371145256Sjkoshy	switch (cmd) {
4372145256Sjkoshy	case MOD_LOAD :
4373145256Sjkoshy		/* initialize the subsystem */
4374145256Sjkoshy		error = pmc_initialize();
4375145256Sjkoshy		if (error != 0)
4376145256Sjkoshy			break;
4377145256Sjkoshy		PMCDBG(MOD,INI,1, "syscall=%d ncpus=%d",
4378145256Sjkoshy		    pmc_syscall_num, mp_ncpus);
4379145256Sjkoshy		break;
4380145256Sjkoshy
4381145256Sjkoshy
4382145256Sjkoshy	case MOD_UNLOAD :
4383145256Sjkoshy	case MOD_SHUTDOWN:
4384145256Sjkoshy		pmc_cleanup();
4385145256Sjkoshy		PMCDBG(MOD,INI,1, "%s", "unloaded");
4386145256Sjkoshy		break;
4387145256Sjkoshy
4388145256Sjkoshy	default :
4389145256Sjkoshy		error = EINVAL;	/* XXX should panic(9) */
4390145256Sjkoshy		break;
4391145256Sjkoshy	}
4392145256Sjkoshy
4393145256Sjkoshy	return error;
4394145256Sjkoshy}
4395145256Sjkoshy
4396145256Sjkoshy/* memory pool */
4397145256SjkoshyMALLOC_DEFINE(M_PMC, "pmc", "Memory space for the PMC module");
4398