hwpmc_mod.c revision 148088
1145256Sjkoshy/*-
2145256Sjkoshy * Copyright (c) 2003-2005 Joseph Koshy
3145256Sjkoshy * All rights reserved.
4145256Sjkoshy *
5145256Sjkoshy * Redistribution and use in source and binary forms, with or without
6145256Sjkoshy * modification, are permitted provided that the following conditions
7145256Sjkoshy * are met:
8145256Sjkoshy * 1. Redistributions of source code must retain the above copyright
9145256Sjkoshy *    notice, this list of conditions and the following disclaimer.
10145256Sjkoshy * 2. Redistributions in binary form must reproduce the above copyright
11145256Sjkoshy *    notice, this list of conditions and the following disclaimer in the
12145256Sjkoshy *    documentation and/or other materials provided with the distribution.
13145256Sjkoshy *
14145256Sjkoshy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15145256Sjkoshy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16145256Sjkoshy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17145256Sjkoshy * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18145256Sjkoshy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19145256Sjkoshy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20145256Sjkoshy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21145256Sjkoshy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22145256Sjkoshy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23145256Sjkoshy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24145256Sjkoshy * SUCH DAMAGE.
25145256Sjkoshy *
26145256Sjkoshy */
27145256Sjkoshy
28145256Sjkoshy#include <sys/cdefs.h>
29145256Sjkoshy__FBSDID("$FreeBSD: head/sys/dev/hwpmc/hwpmc_mod.c 148088 2005-07-17 04:18:06Z jkoshy $");
30145256Sjkoshy
31145256Sjkoshy#include <sys/param.h>
32145256Sjkoshy#include <sys/eventhandler.h>
33145256Sjkoshy#include <sys/jail.h>
34145256Sjkoshy#include <sys/kernel.h>
35147191Sjkoshy#include <sys/kthread.h>
36145256Sjkoshy#include <sys/limits.h>
37145256Sjkoshy#include <sys/lock.h>
38145256Sjkoshy#include <sys/malloc.h>
39145256Sjkoshy#include <sys/module.h>
40145256Sjkoshy#include <sys/mutex.h>
41145256Sjkoshy#include <sys/pmc.h>
42145256Sjkoshy#include <sys/pmckern.h>
43147191Sjkoshy#include <sys/pmclog.h>
44145256Sjkoshy#include <sys/proc.h>
45145256Sjkoshy#include <sys/queue.h>
46147191Sjkoshy#include <sys/resourcevar.h>
47145256Sjkoshy#include <sys/sched.h>
48145256Sjkoshy#include <sys/signalvar.h>
49145256Sjkoshy#include <sys/smp.h>
50145256Sjkoshy#include <sys/sx.h>
51145256Sjkoshy#include <sys/sysctl.h>
52145256Sjkoshy#include <sys/sysent.h>
53145256Sjkoshy#include <sys/systm.h>
54147191Sjkoshy#include <sys/vnode.h>
55145256Sjkoshy
56147191Sjkoshy#include <machine/atomic.h>
57145256Sjkoshy#include <machine/md_var.h>
58145256Sjkoshy
59145256Sjkoshy/*
60145256Sjkoshy * Types
61145256Sjkoshy */
62145256Sjkoshy
63145256Sjkoshyenum pmc_flags {
64145256Sjkoshy	PMC_FLAG_NONE	  = 0x00, /* do nothing */
65145256Sjkoshy	PMC_FLAG_REMOVE   = 0x01, /* atomically remove entry from hash */
66145256Sjkoshy	PMC_FLAG_ALLOCATE = 0x02, /* add entry to hash if not found */
67145256Sjkoshy};
68145256Sjkoshy
69145256Sjkoshy/*
70145256Sjkoshy * The offset in sysent where the syscall is allocated.
71145256Sjkoshy */
72145256Sjkoshy
73145256Sjkoshystatic int pmc_syscall_num = NO_SYSCALL;
74145256Sjkoshystruct pmc_cpu		**pmc_pcpu;	 /* per-cpu state */
75145256Sjkoshypmc_value_t		*pmc_pcpu_saved; /* saved PMC values: CSW handling */
76145256Sjkoshy
77145256Sjkoshy#define	PMC_PCPU_SAVED(C,R)	pmc_pcpu_saved[(R) + md->pmd_npmc*(C)]
78145256Sjkoshy
79145256Sjkoshystruct mtx_pool		*pmc_mtxpool;
80145256Sjkoshystatic int		*pmc_pmcdisp;	 /* PMC row dispositions */
81145256Sjkoshy
82145256Sjkoshy#define	PMC_ROW_DISP_IS_FREE(R)		(pmc_pmcdisp[(R)] == 0)
83145256Sjkoshy#define	PMC_ROW_DISP_IS_THREAD(R)	(pmc_pmcdisp[(R)] > 0)
84145256Sjkoshy#define	PMC_ROW_DISP_IS_STANDALONE(R)	(pmc_pmcdisp[(R)] < 0)
85145256Sjkoshy
86145256Sjkoshy#define	PMC_MARK_ROW_FREE(R) do {					  \
87145256Sjkoshy	pmc_pmcdisp[(R)] = 0;						  \
88145256Sjkoshy} while (0)
89145256Sjkoshy
90145256Sjkoshy#define	PMC_MARK_ROW_STANDALONE(R) do {					  \
91145256Sjkoshy	KASSERT(pmc_pmcdisp[(R)] <= 0, ("[pmc,%d] row disposition error", \
92145256Sjkoshy		    __LINE__));						  \
93145256Sjkoshy	atomic_add_int(&pmc_pmcdisp[(R)], -1);				  \
94145256Sjkoshy	KASSERT(pmc_pmcdisp[(R)] >= (-mp_ncpus), ("[pmc,%d] row "	  \
95145256Sjkoshy		"disposition error", __LINE__));			  \
96145256Sjkoshy} while (0)
97145256Sjkoshy
98145256Sjkoshy#define	PMC_UNMARK_ROW_STANDALONE(R) do { 				  \
99145256Sjkoshy	atomic_add_int(&pmc_pmcdisp[(R)], 1);				  \
100145256Sjkoshy	KASSERT(pmc_pmcdisp[(R)] <= 0, ("[pmc,%d] row disposition error", \
101145256Sjkoshy		    __LINE__));						  \
102145256Sjkoshy} while (0)
103145256Sjkoshy
104145256Sjkoshy#define	PMC_MARK_ROW_THREAD(R) do {					  \
105145256Sjkoshy	KASSERT(pmc_pmcdisp[(R)] >= 0, ("[pmc,%d] row disposition error", \
106145256Sjkoshy		    __LINE__));						  \
107145256Sjkoshy	atomic_add_int(&pmc_pmcdisp[(R)], 1);				  \
108145256Sjkoshy} while (0)
109145256Sjkoshy
110145256Sjkoshy#define	PMC_UNMARK_ROW_THREAD(R) do {					  \
111145256Sjkoshy	atomic_add_int(&pmc_pmcdisp[(R)], -1);				  \
112145256Sjkoshy	KASSERT(pmc_pmcdisp[(R)] >= 0, ("[pmc,%d] row disposition error", \
113145256Sjkoshy		    __LINE__));						  \
114145256Sjkoshy} while (0)
115145256Sjkoshy
116145256Sjkoshy
117145256Sjkoshy/* various event handlers */
118145256Sjkoshystatic eventhandler_tag	pmc_exit_tag, pmc_fork_tag;
119145256Sjkoshy
120145256Sjkoshy/* Module statistics */
121145256Sjkoshystruct pmc_op_getdriverstats pmc_stats;
122145256Sjkoshy
123145256Sjkoshy/* Machine/processor dependent operations */
124145256Sjkoshystruct pmc_mdep  *md;
125145256Sjkoshy
126145256Sjkoshy/*
127145256Sjkoshy * Hash tables mapping owner processes and target threads to PMCs.
128145256Sjkoshy */
129145256Sjkoshy
130145256Sjkoshystruct mtx pmc_processhash_mtx;		/* spin mutex */
131145256Sjkoshystatic u_long pmc_processhashmask;
132145256Sjkoshystatic LIST_HEAD(pmc_processhash, pmc_process)	*pmc_processhash;
133145256Sjkoshy
134145256Sjkoshy/*
135145256Sjkoshy * Hash table of PMC owner descriptors.  This table is protected by
136145256Sjkoshy * the shared PMC "sx" lock.
137145256Sjkoshy */
138145256Sjkoshy
139145256Sjkoshystatic u_long pmc_ownerhashmask;
140145256Sjkoshystatic LIST_HEAD(pmc_ownerhash, pmc_owner)	*pmc_ownerhash;
141145256Sjkoshy
142145256Sjkoshy/*
143147191Sjkoshy * List of PMC owners with system-wide sampling PMCs.
144147191Sjkoshy */
145147191Sjkoshy
146147191Sjkoshystatic LIST_HEAD(, pmc_owner)			pmc_ss_owners;
147147191Sjkoshy
148147191Sjkoshy
149147191Sjkoshy/*
150145256Sjkoshy * Prototypes
151145256Sjkoshy */
152145256Sjkoshy
153145256Sjkoshy#if	DEBUG
154145256Sjkoshystatic int	pmc_debugflags_sysctl_handler(SYSCTL_HANDLER_ARGS);
155145256Sjkoshystatic int	pmc_debugflags_parse(char *newstr, char *fence);
156145256Sjkoshy#endif
157145256Sjkoshy
158145256Sjkoshystatic int	load(struct module *module, int cmd, void *arg);
159147191Sjkoshystatic int	pmc_attach_process(struct proc *p, struct pmc *pm);
160145256Sjkoshystatic struct pmc *pmc_allocate_pmc_descriptor(void);
161147191Sjkoshystatic struct pmc_owner *pmc_allocate_owner_descriptor(struct proc *p);
162147191Sjkoshystatic int	pmc_attach_one_process(struct proc *p, struct pmc *pm);
163147191Sjkoshystatic int	pmc_can_allocate_rowindex(struct proc *p, unsigned int ri,
164147191Sjkoshy    int cpu);
165147191Sjkoshystatic int	pmc_can_attach(struct pmc *pm, struct proc *p);
166147191Sjkoshystatic void	pmc_cleanup(void);
167147191Sjkoshystatic int	pmc_detach_process(struct proc *p, struct pmc *pm);
168147191Sjkoshystatic int	pmc_detach_one_process(struct proc *p, struct pmc *pm,
169147191Sjkoshy    int flags);
170147191Sjkoshystatic void	pmc_destroy_owner_descriptor(struct pmc_owner *po);
171147191Sjkoshystatic struct pmc_owner *pmc_find_owner_descriptor(struct proc *p);
172147191Sjkoshystatic int	pmc_find_pmc(pmc_id_t pmcid, struct pmc **pm);
173145256Sjkoshystatic struct pmc *pmc_find_pmc_descriptor_in_process(struct pmc_owner *po,
174145256Sjkoshy    pmc_id_t pmc);
175145256Sjkoshystatic struct pmc_process *pmc_find_process_descriptor(struct proc *p,
176145256Sjkoshy    uint32_t mode);
177145774Sjkoshystatic void	pmc_force_context_switch(void);
178145256Sjkoshystatic void	pmc_link_target_process(struct pmc *pm,
179145256Sjkoshy    struct pmc_process *pp);
180147191Sjkoshystatic void	pmc_maybe_remove_owner(struct pmc_owner *po);
181147191Sjkoshystatic void	pmc_process_csw_in(struct thread *td);
182147191Sjkoshystatic void	pmc_process_csw_out(struct thread *td);
183145256Sjkoshystatic void	pmc_process_exit(void *arg, struct proc *p);
184145256Sjkoshystatic void	pmc_process_fork(void *arg, struct proc *p1,
185145256Sjkoshy    struct proc *p2, int n);
186147191Sjkoshystatic void	pmc_process_samples(int cpu);
187147191Sjkoshystatic void	pmc_release_pmc_descriptor(struct pmc *pmc);
188147191Sjkoshystatic void	pmc_remove_owner(struct pmc_owner *po);
189147191Sjkoshystatic void	pmc_remove_process_descriptor(struct pmc_process *pp);
190147191Sjkoshystatic void	pmc_restore_cpu_binding(struct pmc_binding *pb);
191147191Sjkoshystatic void	pmc_save_cpu_binding(struct pmc_binding *pb);
192147191Sjkoshystatic void	pmc_select_cpu(int cpu);
193145256Sjkoshystatic int	pmc_start(struct pmc *pm);
194145256Sjkoshystatic int	pmc_stop(struct pmc *pm);
195147191Sjkoshystatic int	pmc_syscall_handler(struct thread *td, void *syscall_args);
196147191Sjkoshystatic void	pmc_unlink_target_process(struct pmc *pmc,
197147191Sjkoshy    struct pmc_process *pp);
198145256Sjkoshy
199145256Sjkoshy/*
200145256Sjkoshy * Kernel tunables and sysctl(8) interface.
201145256Sjkoshy */
202145256Sjkoshy
203145256SjkoshySYSCTL_NODE(_kern, OID_AUTO, hwpmc, CTLFLAG_RW, 0, "HWPMC parameters");
204145256Sjkoshy
205145256Sjkoshy#if	DEBUG
206147191Sjkoshystruct pmc_debugflags pmc_debugflags = PMC_DEBUG_DEFAULT_FLAGS;
207145256Sjkoshychar	pmc_debugstr[PMC_DEBUG_STRSIZE];
208145256SjkoshyTUNABLE_STR(PMC_SYSCTL_NAME_PREFIX "debugflags", pmc_debugstr,
209145256Sjkoshy    sizeof(pmc_debugstr));
210145256SjkoshySYSCTL_PROC(_kern_hwpmc, OID_AUTO, debugflags,
211145256Sjkoshy    CTLTYPE_STRING|CTLFLAG_RW|CTLFLAG_TUN,
212145256Sjkoshy    0, 0, pmc_debugflags_sysctl_handler, "A", "debug flags");
213145256Sjkoshy#endif
214145256Sjkoshy
215145256Sjkoshy/*
216147191Sjkoshy * kern.hwpmc.hashrows -- determines the number of rows in the
217145256Sjkoshy * of the hash table used to look up threads
218145256Sjkoshy */
219145256Sjkoshy
220145256Sjkoshystatic int pmc_hashsize = PMC_HASH_SIZE;
221145256SjkoshyTUNABLE_INT(PMC_SYSCTL_NAME_PREFIX "hashsize", &pmc_hashsize);
222145256SjkoshySYSCTL_INT(_kern_hwpmc, OID_AUTO, hashsize, CTLFLAG_TUN|CTLFLAG_RD,
223145256Sjkoshy    &pmc_hashsize, 0, "rows in hash tables");
224145256Sjkoshy
225145256Sjkoshy/*
226147191Sjkoshy * kern.hwpmc.nsamples --- number of PC samples per CPU
227145256Sjkoshy */
228145256Sjkoshy
229147191Sjkoshystatic int pmc_nsamples = PMC_NSAMPLES;
230147191SjkoshyTUNABLE_INT(PMC_SYSCTL_NAME_PREFIX "nsamples", &pmc_nsamples);
231147191SjkoshySYSCTL_INT(_kern_hwpmc, OID_AUTO, nsamples, CTLFLAG_TUN|CTLFLAG_RD,
232147191Sjkoshy    &pmc_nsamples, 0, "number of PC samples per CPU");
233145256Sjkoshy
234145256Sjkoshy/*
235147191Sjkoshy * kern.hwpmc.mtxpoolsize -- number of mutexes in the mutex pool.
236145256Sjkoshy */
237145256Sjkoshy
238145256Sjkoshystatic int pmc_mtxpool_size = PMC_MTXPOOL_SIZE;
239145256SjkoshyTUNABLE_INT(PMC_SYSCTL_NAME_PREFIX "mtxpoolsize", &pmc_mtxpool_size);
240145256SjkoshySYSCTL_INT(_kern_hwpmc, OID_AUTO, mtxpoolsize, CTLFLAG_TUN|CTLFLAG_RD,
241145256Sjkoshy    &pmc_mtxpool_size, 0, "size of spin mutex pool");
242145256Sjkoshy
243145256Sjkoshy
244145256Sjkoshy/*
245145256Sjkoshy * security.bsd.unprivileged_syspmcs -- allow non-root processes to
246145256Sjkoshy * allocate system-wide PMCs.
247145256Sjkoshy *
248145256Sjkoshy * Allowing unprivileged processes to allocate system PMCs is convenient
249145256Sjkoshy * if system-wide measurements need to be taken concurrently with other
250145256Sjkoshy * per-process measurements.  This feature is turned off by default.
251145256Sjkoshy */
252145256Sjkoshy
253145256SjkoshySYSCTL_DECL(_security_bsd);
254145256Sjkoshy
255145256Sjkoshystatic int pmc_unprivileged_syspmcs = 0;
256145256SjkoshyTUNABLE_INT("security.bsd.unprivileged_syspmcs", &pmc_unprivileged_syspmcs);
257145256SjkoshySYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_syspmcs, CTLFLAG_RW,
258145256Sjkoshy    &pmc_unprivileged_syspmcs, 0,
259145256Sjkoshy    "allow unprivileged process to allocate system PMCs");
260145256Sjkoshy
261147191Sjkoshy/*
262147191Sjkoshy * Hash function.  Discard the lower 2 bits of the pointer since
263147191Sjkoshy * these are always zero for our uses.  The hash multiplier is
264147191Sjkoshy * round((2^LONG_BIT) * ((sqrt(5)-1)/2)).
265147191Sjkoshy */
266145256Sjkoshy
267145256Sjkoshy#if	LONG_BIT == 64
268145256Sjkoshy#define	_PMC_HM		11400714819323198486u
269145256Sjkoshy#elif	LONG_BIT == 32
270145256Sjkoshy#define	_PMC_HM		2654435769u
271145256Sjkoshy#else
272145256Sjkoshy#error 	Must know the size of 'long' to compile
273145256Sjkoshy#endif
274145256Sjkoshy
275145256Sjkoshy#define	PMC_HASH_PTR(P,M)	((((unsigned long) (P) >> 2) * _PMC_HM) & (M))
276145256Sjkoshy
277145256Sjkoshy/*
278145256Sjkoshy * Syscall structures
279145256Sjkoshy */
280145256Sjkoshy
281145256Sjkoshy/* The `sysent' for the new syscall */
282145256Sjkoshystatic struct sysent pmc_sysent = {
283145256Sjkoshy	2,			/* sy_narg */
284145256Sjkoshy	pmc_syscall_handler	/* sy_call */
285145256Sjkoshy};
286145256Sjkoshy
287145256Sjkoshystatic struct syscall_module_data pmc_syscall_mod = {
288145256Sjkoshy	load,
289145256Sjkoshy	NULL,
290145256Sjkoshy	&pmc_syscall_num,
291145256Sjkoshy	&pmc_sysent,
292145256Sjkoshy	{ 0, NULL }
293145256Sjkoshy};
294145256Sjkoshy
295145256Sjkoshystatic moduledata_t pmc_mod = {
296145256Sjkoshy	PMC_MODULE_NAME,
297145256Sjkoshy	syscall_module_handler,
298145256Sjkoshy	&pmc_syscall_mod
299145256Sjkoshy};
300145256Sjkoshy
301145256SjkoshyDECLARE_MODULE(pmc, pmc_mod, SI_SUB_SMP, SI_ORDER_ANY);
302145256SjkoshyMODULE_VERSION(pmc, PMC_VERSION);
303145256Sjkoshy
304145256Sjkoshy#if	DEBUG
305147191Sjkoshyenum pmc_dbgparse_state {
306147191Sjkoshy	PMCDS_WS,		/* in whitespace */
307147191Sjkoshy	PMCDS_MAJOR,		/* seen a major keyword */
308147191Sjkoshy	PMCDS_MINOR
309147191Sjkoshy};
310147191Sjkoshy
311145256Sjkoshystatic int
312145256Sjkoshypmc_debugflags_parse(char *newstr, char *fence)
313145256Sjkoshy{
314145313Sjkoshy	char c, *p, *q;
315147191Sjkoshy	struct pmc_debugflags *tmpflags;
316147191Sjkoshy	int error, found, *newbits, tmp;
317147191Sjkoshy	size_t kwlen;
318145256Sjkoshy
319147191Sjkoshy	MALLOC(tmpflags, struct pmc_debugflags *, sizeof(*tmpflags),
320147191Sjkoshy	    M_PMC, M_WAITOK|M_ZERO);
321145256Sjkoshy
322145256Sjkoshy	p = newstr;
323147191Sjkoshy	error = 0;
324145256Sjkoshy
325147191Sjkoshy	for (; p < fence && (c = *p); p++) {
326145256Sjkoshy
327147191Sjkoshy		/* skip white space */
328147191Sjkoshy		if (c == ' ' || c == '\t')
329147191Sjkoshy			continue;
330147191Sjkoshy
331147191Sjkoshy		/* look for a keyword followed by "=" */
332147191Sjkoshy		for (q = p; p < fence && (c = *p) && c != '='; p++)
333147191Sjkoshy			;
334147191Sjkoshy		if (c != '=') {
335147191Sjkoshy			error = EINVAL;
336147191Sjkoshy			goto done;
337145256Sjkoshy		}
338145256Sjkoshy
339147191Sjkoshy		kwlen = p - q;
340147191Sjkoshy		newbits = NULL;
341145256Sjkoshy
342147191Sjkoshy		/* lookup flag group name */
343147191Sjkoshy#define	DBG_SET_FLAG_MAJ(S,F)						\
344147191Sjkoshy		if (kwlen == sizeof(S)-1 && strncmp(q, S, kwlen) == 0)	\
345147191Sjkoshy			newbits = &tmpflags->pdb_ ## F;
346145256Sjkoshy
347147191Sjkoshy		DBG_SET_FLAG_MAJ("cpu",		CPU);
348147191Sjkoshy		DBG_SET_FLAG_MAJ("csw",		CSW);
349147191Sjkoshy		DBG_SET_FLAG_MAJ("logging",	LOG);
350147191Sjkoshy		DBG_SET_FLAG_MAJ("module",	MOD);
351147191Sjkoshy		DBG_SET_FLAG_MAJ("md", 		MDP);
352147191Sjkoshy		DBG_SET_FLAG_MAJ("owner",	OWN);
353147191Sjkoshy		DBG_SET_FLAG_MAJ("pmc",		PMC);
354147191Sjkoshy		DBG_SET_FLAG_MAJ("process",	PRC);
355147191Sjkoshy		DBG_SET_FLAG_MAJ("sampling", 	SAM);
356145256Sjkoshy
357147191Sjkoshy		if (newbits == NULL) {
358147191Sjkoshy			error = EINVAL;
359147191Sjkoshy			goto done;
360145256Sjkoshy		}
361145256Sjkoshy
362147191Sjkoshy		p++;		/* skip the '=' */
363145256Sjkoshy
364147191Sjkoshy		/* Now parse the individual flags */
365147191Sjkoshy		tmp = 0;
366147191Sjkoshy	newflag:
367147191Sjkoshy		for (q = p; p < fence && (c = *p); p++)
368147191Sjkoshy			if (c == ' ' || c == '\t' || c == ',')
369147191Sjkoshy				break;
370147191Sjkoshy
371147191Sjkoshy		/* p == fence or c == ws or c == "," or c == 0 */
372147191Sjkoshy
373147191Sjkoshy		if ((kwlen = p - q) == 0) {
374147191Sjkoshy			*newbits = tmp;
375147191Sjkoshy			continue;
376147191Sjkoshy		}
377147191Sjkoshy
378147191Sjkoshy		found = 0;
379147191Sjkoshy#define	DBG_SET_FLAG_MIN(S,F)						\
380147191Sjkoshy		if (kwlen == sizeof(S)-1 && strncmp(q, S, kwlen) == 0)	\
381147191Sjkoshy			tmp |= found = (1 << PMC_DEBUG_MIN_ ## F)
382147191Sjkoshy
383147191Sjkoshy		/* a '*' denotes all possible flags in the group */
384147191Sjkoshy		if (kwlen == 1 && *q == '*')
385147191Sjkoshy			tmp = found = ~0;
386147191Sjkoshy		/* look for individual flag names */
387147191Sjkoshy		DBG_SET_FLAG_MIN("allocaterow", ALR);
388147191Sjkoshy		DBG_SET_FLAG_MIN("allocate",	ALL);
389147191Sjkoshy		DBG_SET_FLAG_MIN("attach",	ATT);
390147191Sjkoshy		DBG_SET_FLAG_MIN("bind",	BND);
391147191Sjkoshy		DBG_SET_FLAG_MIN("config",	CFG);
392147191Sjkoshy		DBG_SET_FLAG_MIN("exec",	EXC);
393147191Sjkoshy		DBG_SET_FLAG_MIN("exit",	EXT);
394147191Sjkoshy		DBG_SET_FLAG_MIN("find",	FND);
395147191Sjkoshy		DBG_SET_FLAG_MIN("flush",	FLS);
396147191Sjkoshy		DBG_SET_FLAG_MIN("fork",	FRK);
397147191Sjkoshy		DBG_SET_FLAG_MIN("getbuf",	GTB);
398147191Sjkoshy		DBG_SET_FLAG_MIN("hook",	PMH);
399147191Sjkoshy		DBG_SET_FLAG_MIN("init",	INI);
400147191Sjkoshy		DBG_SET_FLAG_MIN("intr",	INT);
401147191Sjkoshy		DBG_SET_FLAG_MIN("linktarget",	TLK);
402147191Sjkoshy		DBG_SET_FLAG_MIN("mayberemove", OMR);
403147191Sjkoshy		DBG_SET_FLAG_MIN("ops",		OPS);
404147191Sjkoshy		DBG_SET_FLAG_MIN("read",	REA);
405147191Sjkoshy		DBG_SET_FLAG_MIN("register",	REG);
406147191Sjkoshy		DBG_SET_FLAG_MIN("release",	REL);
407147191Sjkoshy		DBG_SET_FLAG_MIN("remove",	ORM);
408147191Sjkoshy		DBG_SET_FLAG_MIN("sample",	SAM);
409147191Sjkoshy		DBG_SET_FLAG_MIN("scheduleio",	SIO);
410147191Sjkoshy		DBG_SET_FLAG_MIN("select",	SEL);
411147191Sjkoshy		DBG_SET_FLAG_MIN("signal",	SIG);
412147191Sjkoshy		DBG_SET_FLAG_MIN("swi",		SWI);
413147191Sjkoshy		DBG_SET_FLAG_MIN("swo",		SWO);
414147191Sjkoshy		DBG_SET_FLAG_MIN("start",	STA);
415147191Sjkoshy		DBG_SET_FLAG_MIN("stop",	STO);
416147191Sjkoshy		DBG_SET_FLAG_MIN("syscall",	PMS);
417147191Sjkoshy		DBG_SET_FLAG_MIN("unlinktarget", TUL);
418147191Sjkoshy		DBG_SET_FLAG_MIN("write",	WRI);
419147191Sjkoshy		if (found == 0) {
420147191Sjkoshy			/* unrecognized flag name */
421147191Sjkoshy			error = EINVAL;
422147191Sjkoshy			goto done;
423147191Sjkoshy		}
424147191Sjkoshy
425147191Sjkoshy		if (c == 0 || c == ' ' || c == '\t') {	/* end of flag group */
426147191Sjkoshy			*newbits = tmp;
427147191Sjkoshy			continue;
428147191Sjkoshy		}
429147191Sjkoshy
430147191Sjkoshy		p++;
431147191Sjkoshy		goto newflag;
432145256Sjkoshy	}
433145256Sjkoshy
434147191Sjkoshy	/* save the new flag set */
435147191Sjkoshy	bcopy(tmpflags, &pmc_debugflags, sizeof(pmc_debugflags));
436145256Sjkoshy
437147191Sjkoshy done:
438147191Sjkoshy	FREE(tmpflags, M_PMC);
439147191Sjkoshy	return error;
440145256Sjkoshy}
441145256Sjkoshy
442145256Sjkoshystatic int
443145256Sjkoshypmc_debugflags_sysctl_handler(SYSCTL_HANDLER_ARGS)
444145256Sjkoshy{
445145256Sjkoshy	char *fence, *newstr;
446145256Sjkoshy	int error;
447145256Sjkoshy	unsigned int n;
448145256Sjkoshy
449145256Sjkoshy	(void) arg1; (void) arg2; /* unused parameters */
450145256Sjkoshy
451145256Sjkoshy	n = sizeof(pmc_debugstr);
452145256Sjkoshy	MALLOC(newstr, char *, n, M_PMC, M_ZERO|M_WAITOK);
453147191Sjkoshy	(void) strlcpy(newstr, pmc_debugstr, n);
454145256Sjkoshy
455145256Sjkoshy	error = sysctl_handle_string(oidp, newstr, n, req);
456145256Sjkoshy
457145256Sjkoshy	/* if there is a new string, parse and copy it */
458145256Sjkoshy	if (error == 0 && req->newptr != NULL) {
459147191Sjkoshy		fence = newstr + (n < req->newlen ? n : req->newlen + 1);
460145256Sjkoshy		if ((error = pmc_debugflags_parse(newstr, fence)) == 0)
461145256Sjkoshy			(void) strlcpy(pmc_debugstr, newstr,
462145256Sjkoshy			    sizeof(pmc_debugstr));
463145256Sjkoshy	}
464145256Sjkoshy
465145256Sjkoshy	FREE(newstr, M_PMC);
466145256Sjkoshy
467145256Sjkoshy	return error;
468145256Sjkoshy}
469145256Sjkoshy#endif
470145256Sjkoshy
471145256Sjkoshy/*
472145256Sjkoshy * Concurrency Control
473145256Sjkoshy *
474145256Sjkoshy * The driver manages the following data structures:
475145256Sjkoshy *
476145256Sjkoshy *   - target process descriptors, one per target process
477145256Sjkoshy *   - owner process descriptors (and attached lists), one per owner process
478145256Sjkoshy *   - lookup hash tables for owner and target processes
479145256Sjkoshy *   - PMC descriptors (and attached lists)
480145256Sjkoshy *   - per-cpu hardware state
481145256Sjkoshy *   - the 'hook' variable through which the kernel calls into
482145256Sjkoshy *     this module
483145256Sjkoshy *   - the machine hardware state (managed by the MD layer)
484145256Sjkoshy *
485145256Sjkoshy * These data structures are accessed from:
486145256Sjkoshy *
487145256Sjkoshy * - thread context-switch code
488145256Sjkoshy * - interrupt handlers (possibly on multiple cpus)
489145256Sjkoshy * - kernel threads on multiple cpus running on behalf of user
490145256Sjkoshy *   processes doing system calls
491145256Sjkoshy * - this driver's private kernel threads
492145256Sjkoshy *
493145256Sjkoshy * = Locks and Locking strategy =
494145256Sjkoshy *
495145256Sjkoshy * The driver uses four locking strategies for its operation:
496145256Sjkoshy *
497145256Sjkoshy * - There is a 'global' SX lock "pmc_sx" that is used to protect
498145256Sjkoshy *   the its 'meta-data'.
499145256Sjkoshy *
500145256Sjkoshy *   Calls into the module (via syscall() or by the kernel) start with
501145256Sjkoshy *   this lock being held in exclusive mode.  Depending on the requested
502145256Sjkoshy *   operation, the lock may be downgraded to 'shared' mode to allow
503145256Sjkoshy *   more concurrent readers into the module.
504145256Sjkoshy *
505145256Sjkoshy *   This SX lock is held in exclusive mode for any operations that
506145256Sjkoshy *   modify the linkages between the driver's internal data structures.
507145256Sjkoshy *
508145256Sjkoshy *   The 'pmc_hook' function pointer is also protected by this lock.
509145256Sjkoshy *   It is only examined with the sx lock held in exclusive mode.  The
510145256Sjkoshy *   kernel module is allowed to be unloaded only with the sx lock
511145256Sjkoshy *   held in exclusive mode.  In normal syscall handling, after
512145256Sjkoshy *   acquiring the pmc_sx lock we first check that 'pmc_hook' is
513145256Sjkoshy *   non-null before proceeding.  This prevents races between the
514145256Sjkoshy *   thread unloading the module and other threads seeking to use the
515145256Sjkoshy *   module.
516145256Sjkoshy *
517145256Sjkoshy * - Lookups of target process structures and owner process structures
518145256Sjkoshy *   cannot use the global "pmc_sx" SX lock because these lookups need
519145256Sjkoshy *   to happen during context switches and in other critical sections
520145256Sjkoshy *   where sleeping is not allowed.  We protect these lookup tables
521145256Sjkoshy *   with their own private spin-mutexes, "pmc_processhash_mtx" and
522145256Sjkoshy *   "pmc_ownerhash_mtx".  These are 'leaf' mutexes, in that no other
523145256Sjkoshy *   lock is acquired with these locks held.
524145256Sjkoshy *
525145256Sjkoshy * - Interrupt handlers work in a lock free manner.  At interrupt
526145256Sjkoshy *   time, handlers look at the PMC pointer (phw->phw_pmc) configured
527145256Sjkoshy *   when the PMC was started.  If this pointer is NULL, the interrupt
528145256Sjkoshy *   is ignored after updating driver statistics.  We ensure that this
529145256Sjkoshy *   pointer is set (using an atomic operation if necessary) before the
530145256Sjkoshy *   PMC hardware is started.  Conversely, this pointer is unset atomically
531145256Sjkoshy *   only after the PMC hardware is stopped.
532145256Sjkoshy *
533145256Sjkoshy *   We ensure that everything needed for the operation of an
534145256Sjkoshy *   interrupt handler is available without it needing to acquire any
535145256Sjkoshy *   locks.  We also ensure that a PMC's software state is destroyed only
536145256Sjkoshy *   after the PMC is taken off hardware (on all CPUs).
537145256Sjkoshy *
538145256Sjkoshy * - Context-switch handling with process-private PMCs needs more
539145256Sjkoshy *   care.
540145256Sjkoshy *
541145256Sjkoshy *   A given process may be the target of multiple PMCs.  For example,
542145256Sjkoshy *   PMCATTACH and PMCDETACH may be requested by a process on one CPU
543145256Sjkoshy *   while the target process is running on another.  A PMC could also
544145256Sjkoshy *   be getting released because its owner is exiting.  We tackle
545145256Sjkoshy *   these situations in the following manner:
546145256Sjkoshy *
547145256Sjkoshy *   - each target process structure 'pmc_process' has an array
548145256Sjkoshy *     of 'struct pmc *' pointers, one for each hardware PMC.
549145256Sjkoshy *
550145256Sjkoshy *   - At context switch IN time, each "target" PMC in RUNNING state
551145256Sjkoshy *     gets started on hardware and a pointer to each PMC is copied into
552145256Sjkoshy *     the per-cpu phw array.  The 'runcount' for the PMC is
553145256Sjkoshy *     incremented.
554145256Sjkoshy *
555145256Sjkoshy *   - At context switch OUT time, all process-virtual PMCs are stopped
556145256Sjkoshy *     on hardware.  The saved value is added to the PMCs value field
557145256Sjkoshy *     only if the PMC is in a non-deleted state (the PMCs state could
558145256Sjkoshy *     have changed during the current time slice).
559145256Sjkoshy *
560145256Sjkoshy *     Note that since in-between a switch IN on a processor and a switch
561145256Sjkoshy *     OUT, the PMC could have been released on another CPU.  Therefore
562145256Sjkoshy *     context switch OUT always looks at the hardware state to turn
563145256Sjkoshy *     OFF PMCs and will update a PMC's saved value only if reachable
564145256Sjkoshy *     from the target process record.
565145256Sjkoshy *
566145256Sjkoshy *   - OP PMCRELEASE could be called on a PMC at any time (the PMC could
567145256Sjkoshy *     be attached to many processes at the time of the call and could
568145256Sjkoshy *     be active on multiple CPUs).
569145256Sjkoshy *
570145256Sjkoshy *     We prevent further scheduling of the PMC by marking it as in
571145256Sjkoshy *     state 'DELETED'.  If the runcount of the PMC is non-zero then
572145256Sjkoshy *     this PMC is currently running on a CPU somewhere.  The thread
573145256Sjkoshy *     doing the PMCRELEASE operation waits by repeatedly doing an
574145256Sjkoshy *     tsleep() till the runcount comes to zero.
575145256Sjkoshy *
576145256Sjkoshy */
577145256Sjkoshy
578145256Sjkoshy/*
579145256Sjkoshy * save the cpu binding of the current kthread
580145256Sjkoshy */
581145256Sjkoshy
582145256Sjkoshystatic void
583145256Sjkoshypmc_save_cpu_binding(struct pmc_binding *pb)
584145256Sjkoshy{
585145256Sjkoshy	PMCDBG(CPU,BND,2, "%s", "save-cpu");
586145256Sjkoshy	mtx_lock_spin(&sched_lock);
587145256Sjkoshy	pb->pb_bound = sched_is_bound(curthread);
588145256Sjkoshy	pb->pb_cpu   = curthread->td_oncpu;
589145256Sjkoshy	mtx_unlock_spin(&sched_lock);
590145256Sjkoshy	PMCDBG(CPU,BND,2, "save-cpu cpu=%d", pb->pb_cpu);
591145256Sjkoshy}
592145256Sjkoshy
593145256Sjkoshy/*
594145256Sjkoshy * restore the cpu binding of the current thread
595145256Sjkoshy */
596145256Sjkoshy
597145256Sjkoshystatic void
598145256Sjkoshypmc_restore_cpu_binding(struct pmc_binding *pb)
599145256Sjkoshy{
600145256Sjkoshy	PMCDBG(CPU,BND,2, "restore-cpu curcpu=%d restore=%d",
601145256Sjkoshy	    curthread->td_oncpu, pb->pb_cpu);
602145256Sjkoshy	mtx_lock_spin(&sched_lock);
603145256Sjkoshy	if (pb->pb_bound)
604145256Sjkoshy		sched_bind(curthread, pb->pb_cpu);
605145256Sjkoshy	else
606145256Sjkoshy		sched_unbind(curthread);
607145256Sjkoshy	mtx_unlock_spin(&sched_lock);
608145256Sjkoshy	PMCDBG(CPU,BND,2, "%s", "restore-cpu done");
609145256Sjkoshy}
610145256Sjkoshy
611145256Sjkoshy/*
612145256Sjkoshy * move execution over the specified cpu and bind it there.
613145256Sjkoshy */
614145256Sjkoshy
615145256Sjkoshystatic void
616145256Sjkoshypmc_select_cpu(int cpu)
617145256Sjkoshy{
618145256Sjkoshy	KASSERT(cpu >= 0 && cpu < mp_ncpus,
619145256Sjkoshy	    ("[pmc,%d] bad cpu number %d", __LINE__, cpu));
620145256Sjkoshy
621145256Sjkoshy	/* never move to a disabled CPU */
622145256Sjkoshy	KASSERT(pmc_cpu_is_disabled(cpu) == 0, ("[pmc,%d] selecting "
623145256Sjkoshy	    "disabled CPU %d", __LINE__, cpu));
624145256Sjkoshy
625145256Sjkoshy	PMCDBG(CPU,SEL,2, "select-cpu cpu=%d", cpu);
626145256Sjkoshy	mtx_lock_spin(&sched_lock);
627145256Sjkoshy	sched_bind(curthread, cpu);
628145256Sjkoshy	mtx_unlock_spin(&sched_lock);
629145256Sjkoshy
630145256Sjkoshy	KASSERT(curthread->td_oncpu == cpu,
631145256Sjkoshy	    ("[pmc,%d] CPU not bound [cpu=%d, curr=%d]", __LINE__,
632145256Sjkoshy		cpu, curthread->td_oncpu));
633145256Sjkoshy
634145256Sjkoshy	PMCDBG(CPU,SEL,2, "select-cpu cpu=%d ok", cpu);
635145256Sjkoshy}
636145256Sjkoshy
637145256Sjkoshy/*
638145774Sjkoshy * Force a context switch.
639145774Sjkoshy *
640145774Sjkoshy * We do this by tsleep'ing for 1 tick -- invoking mi_switch() is not
641145774Sjkoshy * guaranteed to force a context switch.
642145774Sjkoshy */
643145774Sjkoshy
644145774Sjkoshystatic void
645145774Sjkoshypmc_force_context_switch(void)
646145774Sjkoshy{
647145774Sjkoshy	u_char	curpri;
648145774Sjkoshy
649145774Sjkoshy	mtx_lock_spin(&sched_lock);
650145774Sjkoshy	curpri = curthread->td_priority;
651145774Sjkoshy	mtx_unlock_spin(&sched_lock);
652145774Sjkoshy
653145774Sjkoshy	(void) tsleep((void *) pmc_force_context_switch, curpri,
654145774Sjkoshy	    "pmcctx", 1);
655145774Sjkoshy
656145774Sjkoshy}
657145774Sjkoshy
658145774Sjkoshy/*
659147191Sjkoshy * Get the file name for an executable.  This is a simple wrapper
660147191Sjkoshy * around vn_fullpath(9).
661145256Sjkoshy */
662145256Sjkoshy
663147191Sjkoshystatic void
664147708Sjkoshypmc_getfilename(struct vnode *v, char **fullpath, char **freepath)
665145256Sjkoshy{
666145256Sjkoshy	struct thread *td;
667145256Sjkoshy
668147191Sjkoshy	td = curthread;
669147191Sjkoshy	*fullpath = "unknown";
670147191Sjkoshy	*freepath = NULL;
671148088Sjkoshy	vn_lock(v, LK_CANRECURSE | LK_EXCLUSIVE | LK_RETRY, td);
672147708Sjkoshy	vn_fullpath(td, v, fullpath, freepath);
673147708Sjkoshy	VOP_UNLOCK(v, 0, td);
674145256Sjkoshy}
675145256Sjkoshy
676145256Sjkoshy/*
677145256Sjkoshy * remove an process owning PMCs
678145256Sjkoshy */
679145256Sjkoshy
680145256Sjkoshyvoid
681145256Sjkoshypmc_remove_owner(struct pmc_owner *po)
682145256Sjkoshy{
683147191Sjkoshy	struct pmc *pm, *tmp;
684145256Sjkoshy
685145256Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
686145256Sjkoshy
687145256Sjkoshy	PMCDBG(OWN,ORM,1, "remove-owner po=%p", po);
688145256Sjkoshy
689145256Sjkoshy	/* Remove descriptor from the owner hash table */
690145256Sjkoshy	LIST_REMOVE(po, po_next);
691145256Sjkoshy
692147191Sjkoshy	/* release all owned PMC descriptors */
693147191Sjkoshy	LIST_FOREACH_SAFE(pm, &po->po_pmcs, pm_next, tmp) {
694147191Sjkoshy		PMCDBG(OWN,ORM,2, "pmc=%p", pm);
695147191Sjkoshy		KASSERT(pm->pm_owner == po,
696147191Sjkoshy		    ("[pmc,%d] owner %p != po %p", __LINE__, pm->pm_owner, po));
697145256Sjkoshy
698147191Sjkoshy		pmc_release_pmc_descriptor(pm);	/* will unlink from the list */
699145256Sjkoshy	}
700145256Sjkoshy
701147191Sjkoshy	KASSERT(po->po_sscount == 0,
702147191Sjkoshy	    ("[pmc,%d] SS count not zero", __LINE__));
703145256Sjkoshy	KASSERT(LIST_EMPTY(&po->po_pmcs),
704147191Sjkoshy	    ("[pmc,%d] PMC list not empty", __LINE__));
705145256Sjkoshy
706147191Sjkoshy	/* de-configure the log file if present */
707145774Sjkoshy	if (po->po_flags & PMC_PO_OWNS_LOGFILE)
708147191Sjkoshy		pmclog_deconfigure_log(po);
709145256Sjkoshy}
710145256Sjkoshy
711145256Sjkoshy/*
712145256Sjkoshy * remove an owner process record if all conditions are met.
713145256Sjkoshy */
714145256Sjkoshy
715145256Sjkoshystatic void
716145256Sjkoshypmc_maybe_remove_owner(struct pmc_owner *po)
717145256Sjkoshy{
718145256Sjkoshy
719145256Sjkoshy	PMCDBG(OWN,OMR,1, "maybe-remove-owner po=%p", po);
720145256Sjkoshy
721145256Sjkoshy	/*
722145256Sjkoshy	 * Remove owner record if
723145256Sjkoshy	 * - this process does not own any PMCs
724145256Sjkoshy	 * - this process has not allocated a system-wide sampling buffer
725145256Sjkoshy	 */
726145256Sjkoshy
727145256Sjkoshy	if (LIST_EMPTY(&po->po_pmcs) &&
728145774Sjkoshy	    ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0)) {
729145256Sjkoshy		pmc_remove_owner(po);
730147191Sjkoshy		pmc_destroy_owner_descriptor(po);
731145256Sjkoshy	}
732145256Sjkoshy}
733145256Sjkoshy
734145256Sjkoshy/*
735145256Sjkoshy * Add an association between a target process and a PMC.
736145256Sjkoshy */
737145256Sjkoshy
738145256Sjkoshystatic void
739145256Sjkoshypmc_link_target_process(struct pmc *pm, struct pmc_process *pp)
740145256Sjkoshy{
741145256Sjkoshy	int ri;
742145256Sjkoshy	struct pmc_target *pt;
743145256Sjkoshy
744145256Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
745145256Sjkoshy
746145256Sjkoshy	KASSERT(pm != NULL && pp != NULL,
747145256Sjkoshy	    ("[pmc,%d] Null pm %p or pp %p", __LINE__, pm, pp));
748147191Sjkoshy	KASSERT(PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm)),
749147191Sjkoshy	    ("[pmc,%d] Attaching a non-process-virtual pmc=%p to pid=%d",
750147191Sjkoshy		__LINE__, pm, pp->pp_proc->p_pid));
751145256Sjkoshy	KASSERT(pp->pp_refcnt >= 0 && pp->pp_refcnt < ((int) md->pmd_npmc - 1),
752145256Sjkoshy	    ("[pmc,%d] Illegal reference count %d for process record %p",
753145256Sjkoshy		__LINE__, pp->pp_refcnt, (void *) pp));
754145256Sjkoshy
755145774Sjkoshy	ri = PMC_TO_ROWINDEX(pm);
756145256Sjkoshy
757145256Sjkoshy	PMCDBG(PRC,TLK,1, "link-target pmc=%p ri=%d pmc-process=%p",
758145256Sjkoshy	    pm, ri, pp);
759145256Sjkoshy
760145256Sjkoshy#if	DEBUG
761145256Sjkoshy	LIST_FOREACH(pt, &pm->pm_targets, pt_next)
762145256Sjkoshy	    if (pt->pt_process == pp)
763145256Sjkoshy		    KASSERT(0, ("[pmc,%d] pp %p already in pmc %p targets",
764145256Sjkoshy				__LINE__, pp, pm));
765145256Sjkoshy#endif
766145256Sjkoshy
767145256Sjkoshy	MALLOC(pt, struct pmc_target *, sizeof(struct pmc_target),
768145256Sjkoshy	    M_PMC, M_ZERO|M_WAITOK);
769145256Sjkoshy
770145256Sjkoshy	pt->pt_process = pp;
771145256Sjkoshy
772145256Sjkoshy	LIST_INSERT_HEAD(&pm->pm_targets, pt, pt_next);
773145256Sjkoshy
774148067Sjhb	atomic_store_rel_ptr((uintptr_t *)&pp->pp_pmcs[ri].pp_pmc,
775148067Sjhb	    (uintptr_t)pm);
776145256Sjkoshy
777145615Sjkoshy	if (pm->pm_owner->po_owner == pp->pp_proc)
778145774Sjkoshy		pm->pm_flags |= PMC_F_ATTACHED_TO_OWNER;
779145615Sjkoshy
780147191Sjkoshy	/*
781147191Sjkoshy	 * Initialize the per-process values at this row index.
782147191Sjkoshy	 */
783147191Sjkoshy	pp->pp_pmcs[ri].pp_pmcval = PMC_TO_MODE(pm) == PMC_MODE_TS ?
784147191Sjkoshy	    pm->pm_sc.pm_reloadcount : 0;
785147191Sjkoshy
786145256Sjkoshy	pp->pp_refcnt++;
787145256Sjkoshy
788145256Sjkoshy}
789145256Sjkoshy
790145256Sjkoshy/*
791145256Sjkoshy * Removes the association between a target process and a PMC.
792145256Sjkoshy */
793145256Sjkoshy
794145256Sjkoshystatic void
795145256Sjkoshypmc_unlink_target_process(struct pmc *pm, struct pmc_process *pp)
796145256Sjkoshy{
797145256Sjkoshy	int ri;
798147191Sjkoshy	struct proc *p;
799145256Sjkoshy	struct pmc_target *ptgt;
800145256Sjkoshy
801145256Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
802145256Sjkoshy
803145256Sjkoshy	KASSERT(pm != NULL && pp != NULL,
804145256Sjkoshy	    ("[pmc,%d] Null pm %p or pp %p", __LINE__, pm, pp));
805145256Sjkoshy
806145256Sjkoshy	KASSERT(pp->pp_refcnt >= 1 && pp->pp_refcnt < (int) md->pmd_npmc,
807145256Sjkoshy	    ("[pmc,%d] Illegal ref count %d on process record %p",
808145256Sjkoshy		__LINE__, pp->pp_refcnt, (void *) pp));
809145256Sjkoshy
810145774Sjkoshy	ri = PMC_TO_ROWINDEX(pm);
811145256Sjkoshy
812145256Sjkoshy	PMCDBG(PRC,TUL,1, "unlink-target pmc=%p ri=%d pmc-process=%p",
813145256Sjkoshy	    pm, ri, pp);
814145256Sjkoshy
815145256Sjkoshy	KASSERT(pp->pp_pmcs[ri].pp_pmc == pm,
816145256Sjkoshy	    ("[pmc,%d] PMC ri %d mismatch pmc %p pp->[ri] %p", __LINE__,
817145256Sjkoshy		ri, pm, pp->pp_pmcs[ri].pp_pmc));
818145256Sjkoshy
819145256Sjkoshy	pp->pp_pmcs[ri].pp_pmc = NULL;
820145256Sjkoshy	pp->pp_pmcs[ri].pp_pmcval = (pmc_value_t) 0;
821145256Sjkoshy
822145774Sjkoshy	/* Remove owner-specific flags */
823145774Sjkoshy	if (pm->pm_owner->po_owner == pp->pp_proc) {
824145774Sjkoshy		pp->pp_flags &= ~PMC_PP_ENABLE_MSR_ACCESS;
825145774Sjkoshy		pm->pm_flags &= ~PMC_F_ATTACHED_TO_OWNER;
826145774Sjkoshy	}
827145615Sjkoshy
828145256Sjkoshy	pp->pp_refcnt--;
829145256Sjkoshy
830145256Sjkoshy	/* Remove the target process from the PMC structure */
831145256Sjkoshy	LIST_FOREACH(ptgt, &pm->pm_targets, pt_next)
832145256Sjkoshy		if (ptgt->pt_process == pp)
833145256Sjkoshy			break;
834145256Sjkoshy
835145256Sjkoshy	KASSERT(ptgt != NULL, ("[pmc,%d] process %p (pp: %p) not found "
836145256Sjkoshy		    "in pmc %p", __LINE__, pp->pp_proc, pp, pm));
837145256Sjkoshy
838145256Sjkoshy	LIST_REMOVE(ptgt, pt_next);
839145256Sjkoshy	FREE(ptgt, M_PMC);
840145256Sjkoshy
841147191Sjkoshy	/* if the PMC now lacks targets, send the owner a SIGIO */
842147191Sjkoshy	if (LIST_EMPTY(&pm->pm_targets)) {
843147191Sjkoshy		p = pm->pm_owner->po_owner;
844147191Sjkoshy		PROC_LOCK(p);
845147191Sjkoshy		psignal(p, SIGIO);
846147191Sjkoshy		PROC_UNLOCK(p);
847145256Sjkoshy
848147191Sjkoshy		PMCDBG(PRC,SIG,2, "signalling proc=%p signal=%d", p,
849147191Sjkoshy		    SIGIO);
850145256Sjkoshy	}
851145256Sjkoshy}
852145256Sjkoshy
853145256Sjkoshy/*
854145256Sjkoshy * Check if PMC 'pm' may be attached to target process 't'.
855145256Sjkoshy */
856145256Sjkoshy
857145256Sjkoshystatic int
858145256Sjkoshypmc_can_attach(struct pmc *pm, struct proc *t)
859145256Sjkoshy{
860145256Sjkoshy	struct proc *o;		/* pmc owner */
861145256Sjkoshy	struct ucred *oc, *tc;	/* owner, target credentials */
862145256Sjkoshy	int decline_attach, i;
863145256Sjkoshy
864145256Sjkoshy	/*
865145256Sjkoshy	 * A PMC's owner can always attach that PMC to itself.
866145256Sjkoshy	 */
867145256Sjkoshy
868145256Sjkoshy	if ((o = pm->pm_owner->po_owner) == t)
869145256Sjkoshy		return 0;
870145256Sjkoshy
871145256Sjkoshy	PROC_LOCK(o);
872145256Sjkoshy	oc = o->p_ucred;
873145256Sjkoshy	crhold(oc);
874145256Sjkoshy	PROC_UNLOCK(o);
875145256Sjkoshy
876145256Sjkoshy	PROC_LOCK(t);
877145256Sjkoshy	tc = t->p_ucred;
878145256Sjkoshy	crhold(tc);
879145256Sjkoshy	PROC_UNLOCK(t);
880145256Sjkoshy
881145256Sjkoshy	/*
882145256Sjkoshy	 * The effective uid of the PMC owner should match at least one
883145256Sjkoshy	 * of the {effective,real,saved} uids of the target process.
884145256Sjkoshy	 */
885145256Sjkoshy
886145256Sjkoshy	decline_attach = oc->cr_uid != tc->cr_uid &&
887145256Sjkoshy	    oc->cr_uid != tc->cr_svuid &&
888145256Sjkoshy	    oc->cr_uid != tc->cr_ruid;
889145256Sjkoshy
890145256Sjkoshy	/*
891145256Sjkoshy	 * Every one of the target's group ids, must be in the owner's
892145256Sjkoshy	 * group list.
893145256Sjkoshy	 */
894145256Sjkoshy	for (i = 0; !decline_attach && i < tc->cr_ngroups; i++)
895145256Sjkoshy		decline_attach = !groupmember(tc->cr_groups[i], oc);
896145256Sjkoshy
897145256Sjkoshy	/* check the read and saved gids too */
898145256Sjkoshy	if (decline_attach == 0)
899145256Sjkoshy		decline_attach = !groupmember(tc->cr_rgid, oc) ||
900145256Sjkoshy		    !groupmember(tc->cr_svgid, oc);
901145256Sjkoshy
902145256Sjkoshy	crfree(tc);
903145256Sjkoshy	crfree(oc);
904145256Sjkoshy
905145256Sjkoshy	return !decline_attach;
906145256Sjkoshy}
907145256Sjkoshy
908145256Sjkoshy/*
909145256Sjkoshy * Attach a process to a PMC.
910145256Sjkoshy */
911145256Sjkoshy
912145256Sjkoshystatic int
913145256Sjkoshypmc_attach_one_process(struct proc *p, struct pmc *pm)
914145256Sjkoshy{
915145256Sjkoshy	int ri;
916147191Sjkoshy	char *fullpath, *freepath;
917145256Sjkoshy	struct pmc_process	*pp;
918145256Sjkoshy
919145256Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
920145256Sjkoshy
921145256Sjkoshy	PMCDBG(PRC,ATT,2, "attach-one pm=%p ri=%d proc=%p (%d, %s)", pm,
922145774Sjkoshy	    PMC_TO_ROWINDEX(pm), p, p->p_pid, p->p_comm);
923145256Sjkoshy
924145256Sjkoshy	/*
925145256Sjkoshy	 * Locate the process descriptor corresponding to process 'p',
926145256Sjkoshy	 * allocating space as needed.
927145256Sjkoshy	 *
928145256Sjkoshy	 * Verify that rowindex 'pm_rowindex' is free in the process
929145256Sjkoshy	 * descriptor.
930145256Sjkoshy	 *
931145256Sjkoshy	 * If not, allocate space for a descriptor and link the
932145256Sjkoshy	 * process descriptor and PMC.
933145256Sjkoshy	 */
934145774Sjkoshy	ri = PMC_TO_ROWINDEX(pm);
935145256Sjkoshy
936145256Sjkoshy	if ((pp = pmc_find_process_descriptor(p, PMC_FLAG_ALLOCATE)) == NULL)
937145256Sjkoshy		return ENOMEM;
938145256Sjkoshy
939145256Sjkoshy	if (pp->pp_pmcs[ri].pp_pmc == pm) /* already present at slot [ri] */
940145256Sjkoshy		return EEXIST;
941145256Sjkoshy
942145256Sjkoshy	if (pp->pp_pmcs[ri].pp_pmc != NULL)
943145256Sjkoshy		return EBUSY;
944145256Sjkoshy
945145256Sjkoshy	pmc_link_target_process(pm, pp);
946145256Sjkoshy
947147191Sjkoshy	if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)) &&
948147191Sjkoshy	    (pm->pm_flags & PMC_F_ATTACHED_TO_OWNER) == 0)
949147191Sjkoshy		pm->pm_flags |= PMC_F_NEEDS_LOGFILE;
950147191Sjkoshy
951147191Sjkoshy	pm->pm_flags |= PMC_F_ATTACH_DONE; /* mark as attached */
952147191Sjkoshy
953147191Sjkoshy	/* issue an attach event to a configured log file */
954147191Sjkoshy	if (pm->pm_owner->po_flags & PMC_PO_OWNS_LOGFILE) {
955147708Sjkoshy		pmc_getfilename(p->p_textvp, &fullpath, &freepath);
956147191Sjkoshy		pmclog_process_pmcattach(pm, p->p_pid, fullpath);
957147191Sjkoshy		if (freepath)
958147191Sjkoshy			FREE(freepath, M_TEMP);
959147191Sjkoshy	}
960145256Sjkoshy	/* mark process as using HWPMCs */
961145256Sjkoshy	PROC_LOCK(p);
962145256Sjkoshy	p->p_flag |= P_HWPMC;
963145256Sjkoshy	PROC_UNLOCK(p);
964145256Sjkoshy
965145256Sjkoshy	return 0;
966145256Sjkoshy}
967145256Sjkoshy
968145256Sjkoshy/*
969145256Sjkoshy * Attach a process and optionally its children
970145256Sjkoshy */
971145256Sjkoshy
972145256Sjkoshystatic int
973145256Sjkoshypmc_attach_process(struct proc *p, struct pmc *pm)
974145256Sjkoshy{
975145256Sjkoshy	int error;
976145256Sjkoshy	struct proc *top;
977145256Sjkoshy
978145256Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
979145256Sjkoshy
980145256Sjkoshy	PMCDBG(PRC,ATT,1, "attach pm=%p ri=%d proc=%p (%d, %s)", pm,
981145774Sjkoshy	    PMC_TO_ROWINDEX(pm), p, p->p_pid, p->p_comm);
982145256Sjkoshy
983145774Sjkoshy
984145774Sjkoshy	/*
985145774Sjkoshy	 * If this PMC successfully allowed a GETMSR operation
986145774Sjkoshy	 * in the past, disallow further ATTACHes.
987145774Sjkoshy	 */
988145774Sjkoshy
989145774Sjkoshy	if ((pm->pm_flags & PMC_PP_ENABLE_MSR_ACCESS) != 0)
990145774Sjkoshy		return EPERM;
991145774Sjkoshy
992145256Sjkoshy	if ((pm->pm_flags & PMC_F_DESCENDANTS) == 0)
993145256Sjkoshy		return pmc_attach_one_process(p, pm);
994145256Sjkoshy
995145256Sjkoshy	/*
996145256Sjkoshy	 * Traverse all child processes, attaching them to
997145256Sjkoshy	 * this PMC.
998145256Sjkoshy	 */
999145256Sjkoshy
1000145256Sjkoshy	sx_slock(&proctree_lock);
1001145256Sjkoshy
1002145256Sjkoshy	top = p;
1003145256Sjkoshy
1004145256Sjkoshy	for (;;) {
1005145256Sjkoshy		if ((error = pmc_attach_one_process(p, pm)) != 0)
1006145256Sjkoshy			break;
1007145256Sjkoshy		if (!LIST_EMPTY(&p->p_children))
1008145256Sjkoshy			p = LIST_FIRST(&p->p_children);
1009145256Sjkoshy		else for (;;) {
1010145256Sjkoshy			if (p == top)
1011145256Sjkoshy				goto done;
1012145256Sjkoshy			if (LIST_NEXT(p, p_sibling)) {
1013145256Sjkoshy				p = LIST_NEXT(p, p_sibling);
1014145256Sjkoshy				break;
1015145256Sjkoshy			}
1016145256Sjkoshy			p = p->p_pptr;
1017145256Sjkoshy		}
1018145256Sjkoshy	}
1019145256Sjkoshy
1020145256Sjkoshy	if (error)
1021145256Sjkoshy		(void) pmc_detach_process(top, pm);
1022145256Sjkoshy
1023145256Sjkoshy done:
1024145256Sjkoshy	sx_sunlock(&proctree_lock);
1025145256Sjkoshy	return error;
1026145256Sjkoshy}
1027145256Sjkoshy
1028145256Sjkoshy/*
1029145256Sjkoshy * Detach a process from a PMC.  If there are no other PMCs tracking
1030145256Sjkoshy * this process, remove the process structure from its hash table.  If
1031145256Sjkoshy * 'flags' contains PMC_FLAG_REMOVE, then free the process structure.
1032145256Sjkoshy */
1033145256Sjkoshy
1034145256Sjkoshystatic int
1035145256Sjkoshypmc_detach_one_process(struct proc *p, struct pmc *pm, int flags)
1036145256Sjkoshy{
1037145256Sjkoshy	int ri;
1038145256Sjkoshy	struct pmc_process *pp;
1039145256Sjkoshy
1040145256Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
1041145256Sjkoshy
1042145256Sjkoshy	KASSERT(pm != NULL,
1043145256Sjkoshy	    ("[pmc,%d] null pm pointer", __LINE__));
1044145256Sjkoshy
1045145774Sjkoshy	ri = PMC_TO_ROWINDEX(pm);
1046145774Sjkoshy
1047145256Sjkoshy	PMCDBG(PRC,ATT,2, "detach-one pm=%p ri=%d proc=%p (%d, %s) flags=0x%x",
1048145774Sjkoshy	    pm, ri, p, p->p_pid, p->p_comm, flags);
1049145256Sjkoshy
1050145256Sjkoshy	if ((pp = pmc_find_process_descriptor(p, 0)) == NULL)
1051145256Sjkoshy		return ESRCH;
1052145256Sjkoshy
1053145256Sjkoshy	if (pp->pp_pmcs[ri].pp_pmc != pm)
1054145256Sjkoshy		return EINVAL;
1055145256Sjkoshy
1056145256Sjkoshy	pmc_unlink_target_process(pm, pp);
1057145256Sjkoshy
1058147191Sjkoshy	/* Issue a detach entry if a log file is configured */
1059147191Sjkoshy	if (pm->pm_owner->po_flags & PMC_PO_OWNS_LOGFILE)
1060147191Sjkoshy		pmclog_process_pmcdetach(pm, p->p_pid);
1061147191Sjkoshy
1062145256Sjkoshy	/*
1063145256Sjkoshy	 * If there are no PMCs targetting this process, we remove its
1064145256Sjkoshy	 * descriptor from the target hash table and unset the P_HWPMC
1065145256Sjkoshy	 * flag in the struct proc.
1066145256Sjkoshy	 */
1067145256Sjkoshy	KASSERT(pp->pp_refcnt >= 0 && pp->pp_refcnt < (int) md->pmd_npmc,
1068145256Sjkoshy	    ("[pmc,%d] Illegal refcnt %d for process struct %p",
1069145256Sjkoshy		__LINE__, pp->pp_refcnt, pp));
1070145256Sjkoshy
1071145256Sjkoshy	if (pp->pp_refcnt != 0)	/* still a target of some PMC */
1072145256Sjkoshy		return 0;
1073145256Sjkoshy
1074145256Sjkoshy	pmc_remove_process_descriptor(pp);
1075145256Sjkoshy
1076145256Sjkoshy	if (flags & PMC_FLAG_REMOVE)
1077145256Sjkoshy		FREE(pp, M_PMC);
1078145256Sjkoshy
1079145256Sjkoshy	PROC_LOCK(p);
1080145256Sjkoshy	p->p_flag &= ~P_HWPMC;
1081145256Sjkoshy	PROC_UNLOCK(p);
1082145256Sjkoshy
1083145256Sjkoshy	return 0;
1084145256Sjkoshy}
1085145256Sjkoshy
1086145256Sjkoshy/*
1087145256Sjkoshy * Detach a process and optionally its descendants from a PMC.
1088145256Sjkoshy */
1089145256Sjkoshy
1090145256Sjkoshystatic int
1091145256Sjkoshypmc_detach_process(struct proc *p, struct pmc *pm)
1092145256Sjkoshy{
1093145256Sjkoshy	struct proc *top;
1094145256Sjkoshy
1095145256Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
1096145256Sjkoshy
1097145256Sjkoshy	PMCDBG(PRC,ATT,1, "detach pm=%p ri=%d proc=%p (%d, %s)", pm,
1098145774Sjkoshy	    PMC_TO_ROWINDEX(pm), p, p->p_pid, p->p_comm);
1099145256Sjkoshy
1100145256Sjkoshy	if ((pm->pm_flags & PMC_F_DESCENDANTS) == 0)
1101145256Sjkoshy		return pmc_detach_one_process(p, pm, PMC_FLAG_REMOVE);
1102145256Sjkoshy
1103145256Sjkoshy	/*
1104145256Sjkoshy	 * Traverse all children, detaching them from this PMC.  We
1105145256Sjkoshy	 * ignore errors since we could be detaching a PMC from a
1106145256Sjkoshy	 * partially attached proc tree.
1107145256Sjkoshy	 */
1108145256Sjkoshy
1109145256Sjkoshy	sx_slock(&proctree_lock);
1110145256Sjkoshy
1111145256Sjkoshy	top = p;
1112145256Sjkoshy
1113145256Sjkoshy	for (;;) {
1114145256Sjkoshy		(void) pmc_detach_one_process(p, pm, PMC_FLAG_REMOVE);
1115145256Sjkoshy
1116145256Sjkoshy		if (!LIST_EMPTY(&p->p_children))
1117145256Sjkoshy			p = LIST_FIRST(&p->p_children);
1118145256Sjkoshy		else for (;;) {
1119145256Sjkoshy			if (p == top)
1120145256Sjkoshy				goto done;
1121145256Sjkoshy			if (LIST_NEXT(p, p_sibling)) {
1122145256Sjkoshy				p = LIST_NEXT(p, p_sibling);
1123145256Sjkoshy				break;
1124145256Sjkoshy			}
1125145256Sjkoshy			p = p->p_pptr;
1126145256Sjkoshy		}
1127145256Sjkoshy	}
1128145256Sjkoshy
1129145256Sjkoshy done:
1130145256Sjkoshy	sx_sunlock(&proctree_lock);
1131147191Sjkoshy
1132147191Sjkoshy	if (LIST_EMPTY(&pm->pm_targets))
1133147191Sjkoshy		pm->pm_flags &= ~PMC_F_ATTACH_DONE;
1134147191Sjkoshy
1135145256Sjkoshy	return 0;
1136145256Sjkoshy}
1137145256Sjkoshy
1138147191Sjkoshy
1139145256Sjkoshy/*
1140147191Sjkoshy * Thread context switch IN
1141145256Sjkoshy */
1142145256Sjkoshy
1143147191Sjkoshystatic void
1144147191Sjkoshypmc_process_csw_in(struct thread *td)
1145147191Sjkoshy{
1146147191Sjkoshy	int cpu;
1147147191Sjkoshy	unsigned int ri;
1148147191Sjkoshy	struct pmc *pm;
1149147191Sjkoshy	struct proc *p;
1150147191Sjkoshy	struct pmc_cpu *pc;
1151147191Sjkoshy	struct pmc_hw *phw;
1152147191Sjkoshy	struct pmc_process *pp;
1153147191Sjkoshy	pmc_value_t newvalue;
1154145256Sjkoshy
1155147191Sjkoshy	p = td->td_proc;
1156145256Sjkoshy
1157147191Sjkoshy	if ((pp = pmc_find_process_descriptor(p, PMC_FLAG_NONE)) == NULL)
1158147191Sjkoshy		return;
1159145256Sjkoshy
1160147191Sjkoshy	KASSERT(pp->pp_proc == td->td_proc,
1161147191Sjkoshy	    ("[pmc,%d] not my thread state", __LINE__));
1162145256Sjkoshy
1163147191Sjkoshy	critical_enter(); /* no preemption from this point */
1164145256Sjkoshy
1165147191Sjkoshy	cpu = PCPU_GET(cpuid); /* td->td_oncpu is invalid */
1166145256Sjkoshy
1167147191Sjkoshy	PMCDBG(CSW,SWI,1, "cpu=%d proc=%p (%d, %s) pp=%p", cpu, p,
1168147191Sjkoshy	    p->p_pid, p->p_comm, pp);
1169145256Sjkoshy
1170147191Sjkoshy	KASSERT(cpu >= 0 && cpu < mp_ncpus,
1171147191Sjkoshy	    ("[pmc,%d] wierd CPU id %d", __LINE__, cpu));
1172145256Sjkoshy
1173147191Sjkoshy	pc = pmc_pcpu[cpu];
1174145256Sjkoshy
1175147191Sjkoshy	for (ri = 0; ri < md->pmd_npmc; ri++) {
1176145256Sjkoshy
1177147191Sjkoshy		if ((pm = pp->pp_pmcs[ri].pp_pmc) == NULL)
1178147191Sjkoshy			continue;
1179147191Sjkoshy
1180147191Sjkoshy		KASSERT(PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm)),
1181147191Sjkoshy		    ("[pmc,%d] Target PMC in non-virtual mode (%d)",
1182147191Sjkoshy			__LINE__, PMC_TO_MODE(pm)));
1183147191Sjkoshy
1184147191Sjkoshy		KASSERT(PMC_TO_ROWINDEX(pm) == ri,
1185147191Sjkoshy		    ("[pmc,%d] Row index mismatch pmc %d != ri %d",
1186147191Sjkoshy			__LINE__, PMC_TO_ROWINDEX(pm), ri));
1187147191Sjkoshy
1188145256Sjkoshy		/*
1189147191Sjkoshy		 * Only PMCs that are marked as 'RUNNING' need
1190147191Sjkoshy		 * be placed on hardware.
1191145256Sjkoshy		 */
1192145256Sjkoshy
1193147191Sjkoshy		if (pm->pm_state != PMC_STATE_RUNNING)
1194147191Sjkoshy			continue;
1195145256Sjkoshy
1196147191Sjkoshy		/* increment PMC runcount */
1197147191Sjkoshy		atomic_add_rel_32(&pm->pm_runcount, 1);
1198145256Sjkoshy
1199147191Sjkoshy		/* configure the HWPMC we are going to use. */
1200147191Sjkoshy		md->pmd_config_pmc(cpu, ri, pm);
1201145256Sjkoshy
1202147191Sjkoshy		phw = pc->pc_hwpmcs[ri];
1203145256Sjkoshy
1204147191Sjkoshy		KASSERT(phw != NULL,
1205147191Sjkoshy		    ("[pmc,%d] null hw pointer", __LINE__));
1206145256Sjkoshy
1207147191Sjkoshy		KASSERT(phw->phw_pmc == pm,
1208147191Sjkoshy		    ("[pmc,%d] hw->pmc %p != pmc %p", __LINE__,
1209147191Sjkoshy			phw->phw_pmc, pm));
1210145256Sjkoshy
1211147191Sjkoshy		/*
1212147191Sjkoshy		 * Write out saved value and start the PMC.
1213147191Sjkoshy		 *
1214147191Sjkoshy		 * Sampling PMCs use a per-process value, while
1215147191Sjkoshy		 * counting mode PMCs use a per-pmc value that is
1216147191Sjkoshy		 * inherited across descendants.
1217147191Sjkoshy		 */
1218147191Sjkoshy		if (PMC_TO_MODE(pm) == PMC_MODE_TS) {
1219147191Sjkoshy			mtx_pool_lock_spin(pmc_mtxpool, pm);
1220147191Sjkoshy			newvalue = PMC_PCPU_SAVED(cpu,ri) =
1221147191Sjkoshy			    pp->pp_pmcs[ri].pp_pmcval;
1222147191Sjkoshy			mtx_pool_unlock_spin(pmc_mtxpool, pm);
1223147191Sjkoshy		} else {
1224147191Sjkoshy			KASSERT(PMC_TO_MODE(pm) == PMC_MODE_TC,
1225147191Sjkoshy			    ("[pmc,%d] illegal mode=%d", __LINE__,
1226147191Sjkoshy			    PMC_TO_MODE(pm)));
1227147191Sjkoshy			mtx_pool_lock_spin(pmc_mtxpool, pm);
1228147191Sjkoshy			newvalue = PMC_PCPU_SAVED(cpu, ri) =
1229147191Sjkoshy			    pm->pm_gv.pm_savedvalue;
1230147191Sjkoshy			mtx_pool_unlock_spin(pmc_mtxpool, pm);
1231147191Sjkoshy		}
1232145256Sjkoshy
1233147191Sjkoshy		PMCDBG(CSW,SWI,1,"cpu=%d ri=%d new=%jd", cpu, ri, newvalue);
1234145256Sjkoshy
1235147191Sjkoshy		md->pmd_write_pmc(cpu, ri, newvalue);
1236147191Sjkoshy		md->pmd_start_pmc(cpu, ri);
1237147191Sjkoshy	}
1238145256Sjkoshy
1239147191Sjkoshy	/*
1240147191Sjkoshy	 * perform any other architecture/cpu dependent thread
1241147191Sjkoshy	 * switch-in actions.
1242147191Sjkoshy	 */
1243145256Sjkoshy
1244147191Sjkoshy	(void) (*md->pmd_switch_in)(pc, pp);
1245145256Sjkoshy
1246147191Sjkoshy	critical_exit();
1247145256Sjkoshy
1248147191Sjkoshy}
1249145256Sjkoshy
1250147191Sjkoshy/*
1251147191Sjkoshy * Thread context switch OUT.
1252147191Sjkoshy */
1253145256Sjkoshy
1254147191Sjkoshystatic void
1255147191Sjkoshypmc_process_csw_out(struct thread *td)
1256147191Sjkoshy{
1257147191Sjkoshy	int cpu;
1258147191Sjkoshy	enum pmc_mode mode;
1259147191Sjkoshy	unsigned int ri;
1260147191Sjkoshy	struct pmc *pm;
1261147191Sjkoshy	struct proc *p;
1262147191Sjkoshy	struct pmc_cpu *pc;
1263147191Sjkoshy	struct pmc_process *pp;
1264147191Sjkoshy	int64_t tmp;
1265147191Sjkoshy	pmc_value_t newvalue;
1266145256Sjkoshy
1267147191Sjkoshy	/*
1268147191Sjkoshy	 * Locate our process descriptor; this may be NULL if
1269147191Sjkoshy	 * this process is exiting and we have already removed
1270147191Sjkoshy	 * the process from the target process table.
1271147191Sjkoshy	 *
1272147191Sjkoshy	 * Note that due to kernel preemption, multiple
1273147191Sjkoshy	 * context switches may happen while the process is
1274147191Sjkoshy	 * exiting.
1275147191Sjkoshy	 *
1276147191Sjkoshy	 * Note also that if the target process cannot be
1277147191Sjkoshy	 * found we still need to deconfigure any PMCs that
1278147191Sjkoshy	 * are currently running on hardware.
1279147191Sjkoshy	 */
1280145256Sjkoshy
1281147191Sjkoshy	p = td->td_proc;
1282147191Sjkoshy	pp = pmc_find_process_descriptor(p, PMC_FLAG_NONE);
1283145256Sjkoshy
1284147191Sjkoshy	/*
1285147191Sjkoshy	 * save PMCs
1286147191Sjkoshy	 */
1287145256Sjkoshy
1288147191Sjkoshy	critical_enter();
1289145774Sjkoshy
1290147191Sjkoshy	cpu = PCPU_GET(cpuid); /* td->td_oncpu is invalid */
1291145256Sjkoshy
1292147191Sjkoshy	PMCDBG(CSW,SWO,1, "cpu=%d proc=%p (%d, %s) pp=%p", cpu, p,
1293147191Sjkoshy	    p->p_pid, p->p_comm, pp);
1294145615Sjkoshy
1295147191Sjkoshy	KASSERT(cpu >= 0 && cpu < mp_ncpus,
1296147191Sjkoshy	    ("[pmc,%d wierd CPU id %d", __LINE__, cpu));
1297145615Sjkoshy
1298147191Sjkoshy	pc = pmc_pcpu[cpu];
1299145615Sjkoshy
1300147191Sjkoshy	/*
1301147191Sjkoshy	 * When a PMC gets unlinked from a target PMC, it will
1302147191Sjkoshy	 * be removed from the target's pp_pmc[] array.
1303147191Sjkoshy	 *
1304147191Sjkoshy	 * However, on a MP system, the target could have been
1305147191Sjkoshy	 * executing on another CPU at the time of the unlink.
1306147191Sjkoshy	 * So, at context switch OUT time, we need to look at
1307147191Sjkoshy	 * the hardware to determine if a PMC is scheduled on
1308147191Sjkoshy	 * it.
1309147191Sjkoshy	 */
1310145256Sjkoshy
1311147191Sjkoshy	for (ri = 0; ri < md->pmd_npmc; ri++) {
1312145256Sjkoshy
1313147191Sjkoshy		pm = NULL;
1314147191Sjkoshy		(void) (*md->pmd_get_config)(cpu, ri, &pm);
1315145256Sjkoshy
1316147191Sjkoshy		if (pm == NULL)	/* nothing at this row index */
1317147191Sjkoshy			continue;
1318145256Sjkoshy
1319147191Sjkoshy		mode = PMC_TO_MODE(pm);
1320147191Sjkoshy		if (!PMC_IS_VIRTUAL_MODE(mode))
1321147191Sjkoshy			continue; /* not a process virtual PMC */
1322145774Sjkoshy
1323147191Sjkoshy		KASSERT(PMC_TO_ROWINDEX(pm) == ri,
1324147191Sjkoshy		    ("[pmc,%d] ri mismatch pmc(%d) ri(%d)",
1325147191Sjkoshy			__LINE__, PMC_TO_ROWINDEX(pm), ri));
1326145256Sjkoshy
1327147191Sjkoshy		/* Stop hardware if not already stopped */
1328147867Sjkoshy		if (pm->pm_stalled == 0)
1329147191Sjkoshy			md->pmd_stop_pmc(cpu, ri);
1330147191Sjkoshy
1331147191Sjkoshy		/* reduce this PMC's runcount */
1332147191Sjkoshy		atomic_subtract_rel_32(&pm->pm_runcount, 1);
1333147191Sjkoshy
1334145256Sjkoshy		/*
1335147191Sjkoshy		 * If this PMC is associated with this process,
1336147191Sjkoshy		 * save the reading.
1337145256Sjkoshy		 */
1338145256Sjkoshy
1339147191Sjkoshy		if (pp != NULL && pp->pp_pmcs[ri].pp_pmc != NULL) {
1340147191Sjkoshy
1341147191Sjkoshy			KASSERT(pm == pp->pp_pmcs[ri].pp_pmc,
1342147191Sjkoshy			    ("[pmc,%d] pm %p != pp_pmcs[%d] %p", __LINE__,
1343147191Sjkoshy				pm, ri, pp->pp_pmcs[ri].pp_pmc));
1344147191Sjkoshy
1345147191Sjkoshy			KASSERT(pp->pp_refcnt > 0,
1346147191Sjkoshy			    ("[pmc,%d] pp refcnt = %d", __LINE__,
1347147191Sjkoshy				pp->pp_refcnt));
1348147191Sjkoshy
1349147191Sjkoshy			md->pmd_read_pmc(cpu, ri, &newvalue);
1350147191Sjkoshy
1351147191Sjkoshy			tmp = newvalue - PMC_PCPU_SAVED(cpu,ri);
1352147191Sjkoshy
1353147191Sjkoshy			PMCDBG(CSW,SWI,1,"cpu=%d ri=%d tmp=%jd", cpu, ri,
1354147191Sjkoshy			    tmp);
1355147191Sjkoshy
1356147191Sjkoshy			if (mode == PMC_MODE_TS) {
1357147191Sjkoshy
1358147191Sjkoshy				/*
1359147191Sjkoshy				 * For sampling process-virtual PMCs,
1360147191Sjkoshy				 * we expect the count to be
1361147191Sjkoshy				 * decreasing as the 'value'
1362147191Sjkoshy				 * programmed into the PMC is the
1363147191Sjkoshy				 * number of events to be seen till
1364147191Sjkoshy				 * the next sampling interrupt.
1365147191Sjkoshy				 */
1366147191Sjkoshy				if (tmp < 0)
1367147191Sjkoshy					tmp += pm->pm_sc.pm_reloadcount;
1368147191Sjkoshy				mtx_pool_lock_spin(pmc_mtxpool, pm);
1369147191Sjkoshy				pp->pp_pmcs[ri].pp_pmcval -= tmp;
1370147191Sjkoshy				if ((int64_t) pp->pp_pmcs[ri].pp_pmcval < 0)
1371147191Sjkoshy					pp->pp_pmcs[ri].pp_pmcval +=
1372147191Sjkoshy					    pm->pm_sc.pm_reloadcount;
1373147191Sjkoshy				mtx_pool_unlock_spin(pmc_mtxpool, pm);
1374147191Sjkoshy
1375147191Sjkoshy			} else {
1376147191Sjkoshy
1377147191Sjkoshy				/*
1378147191Sjkoshy				 * For counting process-virtual PMCs,
1379147191Sjkoshy				 * we expect the count to be
1380147191Sjkoshy				 * increasing monotonically, modulo a 64
1381147191Sjkoshy				 * bit wraparound.
1382147191Sjkoshy				 */
1383147191Sjkoshy				KASSERT((int64_t) tmp >= 0,
1384147191Sjkoshy				    ("[pmc,%d] negative increment cpu=%d "
1385147191Sjkoshy				     "ri=%d newvalue=%jx saved=%jx "
1386147191Sjkoshy				     "incr=%jx", __LINE__, cpu, ri,
1387147191Sjkoshy				     newvalue, PMC_PCPU_SAVED(cpu,ri), tmp));
1388147191Sjkoshy
1389147191Sjkoshy				mtx_pool_lock_spin(pmc_mtxpool, pm);
1390147191Sjkoshy				pm->pm_gv.pm_savedvalue += tmp;
1391147191Sjkoshy				pp->pp_pmcs[ri].pp_pmcval += tmp;
1392147191Sjkoshy				mtx_pool_unlock_spin(pmc_mtxpool, pm);
1393147191Sjkoshy
1394147191Sjkoshy				if (pm->pm_flags & PMC_F_LOG_PROCCSW)
1395147191Sjkoshy					pmclog_process_proccsw(pm, pp, tmp);
1396147191Sjkoshy			}
1397145256Sjkoshy		}
1398145256Sjkoshy
1399147191Sjkoshy		/* mark hardware as free */
1400147191Sjkoshy		md->pmd_config_pmc(cpu, ri, NULL);
1401145256Sjkoshy	}
1402145256Sjkoshy
1403145256Sjkoshy	/*
1404147191Sjkoshy	 * perform any other architecture/cpu dependent thread
1405147191Sjkoshy	 * switch out functions.
1406147191Sjkoshy	 */
1407147191Sjkoshy
1408147191Sjkoshy	(void) (*md->pmd_switch_out)(pc, pp);
1409147191Sjkoshy
1410147191Sjkoshy	critical_exit();
1411147191Sjkoshy}
1412147191Sjkoshy
1413147191Sjkoshy/*
1414147191Sjkoshy * The 'hook' invoked from the kernel proper
1415147191Sjkoshy */
1416147191Sjkoshy
1417147191Sjkoshy
1418147191Sjkoshy#if	DEBUG
1419147191Sjkoshyconst char *pmc_hooknames[] = {
1420147191Sjkoshy	"",
1421147191Sjkoshy	"EXIT",
1422147191Sjkoshy	"EXEC",
1423147191Sjkoshy	"FORK",
1424147191Sjkoshy	"CSW-IN",
1425147191Sjkoshy	"CSW-OUT",
1426147191Sjkoshy	"SAMPLE"
1427147191Sjkoshy};
1428147191Sjkoshy#endif
1429147191Sjkoshy
1430147191Sjkoshystatic int
1431147191Sjkoshypmc_hook_handler(struct thread *td, int function, void *arg)
1432147191Sjkoshy{
1433147191Sjkoshy
1434147191Sjkoshy	PMCDBG(MOD,PMH,1, "hook td=%p func=%d \"%s\" arg=%p", td, function,
1435147191Sjkoshy	    pmc_hooknames[function], arg);
1436147191Sjkoshy
1437147191Sjkoshy	switch (function)
1438147191Sjkoshy	{
1439147191Sjkoshy
1440147191Sjkoshy	/*
1441145256Sjkoshy	 * Process exec()
1442145256Sjkoshy	 */
1443145256Sjkoshy
1444145256Sjkoshy	case PMC_FN_PROCESS_EXEC:
1445145256Sjkoshy	{
1446147191Sjkoshy		char *fullpath, *freepath;
1447145256Sjkoshy		unsigned int ri;
1448147191Sjkoshy		int is_using_hwpmcs;
1449145256Sjkoshy		struct pmc *pm;
1450145256Sjkoshy		struct proc *p;
1451145256Sjkoshy		struct pmc_owner *po;
1452145256Sjkoshy		struct pmc_process *pp;
1453147708Sjkoshy		struct pmckern_procexec *pk;
1454145256Sjkoshy
1455145256Sjkoshy		sx_assert(&pmc_sx, SX_XLOCKED);
1456145256Sjkoshy
1457147191Sjkoshy		p = td->td_proc;
1458147708Sjkoshy		pmc_getfilename(p->p_textvp, &fullpath, &freepath);
1459147191Sjkoshy
1460147708Sjkoshy		pk = (struct pmckern_procexec *) arg;
1461147708Sjkoshy
1462147191Sjkoshy		/* Inform owners of SS mode PMCs of the exec event. */
1463147191Sjkoshy		LIST_FOREACH(po, &pmc_ss_owners, po_ssnext)
1464147191Sjkoshy		    if (po->po_flags & PMC_PO_OWNS_LOGFILE)
1465147708Sjkoshy			    pmclog_process_procexec(po, PMC_ID_INVALID,
1466147708Sjkoshy				p->p_pid, pk->pm_entryaddr, fullpath);
1467147191Sjkoshy
1468147191Sjkoshy		PROC_LOCK(p);
1469147191Sjkoshy		is_using_hwpmcs = p->p_flag & P_HWPMC;
1470147191Sjkoshy		PROC_UNLOCK(p);
1471147191Sjkoshy
1472147191Sjkoshy		if (!is_using_hwpmcs) {
1473147191Sjkoshy			if (freepath)
1474147191Sjkoshy				FREE(freepath, M_TEMP);
1475147191Sjkoshy			break;
1476147191Sjkoshy		}
1477147191Sjkoshy
1478145256Sjkoshy		/*
1479145256Sjkoshy		 * PMCs are not inherited across an exec():  remove any
1480145256Sjkoshy		 * PMCs that this process is the owner of.
1481145256Sjkoshy		 */
1482145256Sjkoshy
1483145256Sjkoshy		if ((po = pmc_find_owner_descriptor(p)) != NULL) {
1484145256Sjkoshy			pmc_remove_owner(po);
1485147191Sjkoshy			pmc_destroy_owner_descriptor(po);
1486145256Sjkoshy		}
1487145256Sjkoshy
1488145256Sjkoshy		/*
1489145256Sjkoshy		 * If this process is the target of a PMC, check if the new
1490145256Sjkoshy		 * credentials are compatible with the owner's permissions.
1491145256Sjkoshy		 */
1492145256Sjkoshy
1493145256Sjkoshy		if ((pp = pmc_find_process_descriptor(p, 0)) == NULL)
1494145256Sjkoshy			break;
1495145256Sjkoshy
1496147191Sjkoshy		/*
1497147191Sjkoshy		 * Log the exec event to all monitoring owners.  Skip
1498147191Sjkoshy		 * owners who have already recieved the event because
1499147191Sjkoshy		 * the have system sampling PMCs active.
1500147191Sjkoshy		 */
1501147191Sjkoshy		for (ri = 0; ri < md->pmd_npmc; ri++)
1502147191Sjkoshy			if ((pm = pp->pp_pmcs[ri].pp_pmc) != NULL) {
1503147191Sjkoshy				po = pm->pm_owner;
1504147191Sjkoshy				if (po->po_sscount == 0 &&
1505147191Sjkoshy				    po->po_flags & PMC_PO_OWNS_LOGFILE)
1506147708Sjkoshy					pmclog_process_procexec(po, pm->pm_id,
1507147708Sjkoshy					    p->p_pid, pk->pm_entryaddr,
1508147191Sjkoshy					    fullpath);
1509147191Sjkoshy			}
1510147191Sjkoshy
1511147191Sjkoshy		if (freepath)
1512147191Sjkoshy			FREE(freepath, M_TEMP);
1513147191Sjkoshy
1514145256Sjkoshy
1515145256Sjkoshy		PMCDBG(PRC,EXC,1, "exec proc=%p (%d, %s) cred-changed=%d",
1516147708Sjkoshy		    p, p->p_pid, p->p_comm, pk->pm_credentialschanged);
1517145256Sjkoshy
1518147708Sjkoshy		if (pk->pm_credentialschanged == 0) /* no change */
1519145256Sjkoshy			break;
1520145256Sjkoshy
1521145256Sjkoshy		/*
1522145256Sjkoshy		 * If the newly exec()'ed process has a different credential
1523145256Sjkoshy		 * than before, allow it to be the target of a PMC only if
1524145256Sjkoshy		 * the PMC's owner has sufficient priviledge.
1525145256Sjkoshy		 */
1526145256Sjkoshy
1527145256Sjkoshy		for (ri = 0; ri < md->pmd_npmc; ri++)
1528145256Sjkoshy			if ((pm = pp->pp_pmcs[ri].pp_pmc) != NULL)
1529145256Sjkoshy				if (pmc_can_attach(pm, td->td_proc) != 0)
1530145256Sjkoshy					pmc_detach_one_process(td->td_proc,
1531145256Sjkoshy					    pm, PMC_FLAG_NONE);
1532145256Sjkoshy
1533145256Sjkoshy		KASSERT(pp->pp_refcnt >= 0 && pp->pp_refcnt < (int) md->pmd_npmc,
1534145256Sjkoshy		    ("[pmc,%d] Illegal ref count %d on pp %p", __LINE__,
1535145256Sjkoshy			pp->pp_refcnt, pp));
1536145256Sjkoshy
1537145256Sjkoshy		/*
1538145256Sjkoshy		 * If this process is no longer the target of any
1539145256Sjkoshy		 * PMCs, we can remove the process entry and free
1540145256Sjkoshy		 * up space.
1541145256Sjkoshy		 */
1542145256Sjkoshy
1543145256Sjkoshy		if (pp->pp_refcnt == 0) {
1544145256Sjkoshy			pmc_remove_process_descriptor(pp);
1545145256Sjkoshy			FREE(pp, M_PMC);
1546147191Sjkoshy			break;
1547145256Sjkoshy		}
1548145256Sjkoshy
1549145256Sjkoshy	}
1550145256Sjkoshy	break;
1551145256Sjkoshy
1552145256Sjkoshy	case PMC_FN_CSW_IN:
1553147191Sjkoshy		pmc_process_csw_in(td);
1554147191Sjkoshy		break;
1555145256Sjkoshy
1556147191Sjkoshy	case PMC_FN_CSW_OUT:
1557147191Sjkoshy		pmc_process_csw_out(td);
1558147191Sjkoshy		break;
1559145256Sjkoshy
1560145256Sjkoshy	/*
1561147191Sjkoshy	 * Process accumulated PC samples.
1562147191Sjkoshy	 *
1563147191Sjkoshy	 * This function is expected to be called by hardclock() for
1564147191Sjkoshy	 * each CPU that has accumulated PC samples.
1565147191Sjkoshy	 *
1566147191Sjkoshy	 * This function is to be executed on the CPU whose samples
1567147191Sjkoshy	 * are being processed.
1568145256Sjkoshy	 */
1569147191Sjkoshy	case PMC_FN_DO_SAMPLES:
1570145256Sjkoshy
1571145256Sjkoshy		/*
1572147191Sjkoshy		 * Clear the cpu specific bit in the CPU mask before
1573147191Sjkoshy		 * do the rest of the processing.  If the NMI handler
1574147191Sjkoshy		 * gets invoked after the "atomic_clear_int()" call
1575147191Sjkoshy		 * below but before "pmc_process_samples()" gets
1576147191Sjkoshy		 * around to processing the interrupt, then we will
1577147191Sjkoshy		 * come back here at the next hardclock() tick (and
1578147191Sjkoshy		 * may find nothing to do if "pmc_process_samples()"
1579147191Sjkoshy		 * had already processed the interrupt).  We don't
1580147191Sjkoshy		 * lose the interrupt sample.
1581145256Sjkoshy		 */
1582147191Sjkoshy		atomic_clear_int(&pmc_cpumask, (1 << PCPU_GET(cpuid)));
1583147191Sjkoshy		pmc_process_samples(PCPU_GET(cpuid));
1584147191Sjkoshy		break;
1585145256Sjkoshy
1586145256Sjkoshy	default:
1587145256Sjkoshy#if DEBUG
1588145256Sjkoshy		KASSERT(0, ("[pmc,%d] unknown hook %d\n", __LINE__, function));
1589145256Sjkoshy#endif
1590145256Sjkoshy		break;
1591145256Sjkoshy
1592145256Sjkoshy	}
1593145256Sjkoshy
1594145256Sjkoshy	return 0;
1595145256Sjkoshy}
1596145256Sjkoshy
1597145256Sjkoshy/*
1598145256Sjkoshy * allocate a 'struct pmc_owner' descriptor in the owner hash table.
1599145256Sjkoshy */
1600145256Sjkoshy
1601145256Sjkoshystatic struct pmc_owner *
1602145256Sjkoshypmc_allocate_owner_descriptor(struct proc *p)
1603145256Sjkoshy{
1604145256Sjkoshy	uint32_t hindex;
1605145256Sjkoshy	struct pmc_owner *po;
1606145256Sjkoshy	struct pmc_ownerhash *poh;
1607145256Sjkoshy
1608145256Sjkoshy	hindex = PMC_HASH_PTR(p, pmc_ownerhashmask);
1609145256Sjkoshy	poh = &pmc_ownerhash[hindex];
1610145256Sjkoshy
1611145256Sjkoshy	/* allocate space for N pointers and one descriptor struct */
1612145256Sjkoshy	MALLOC(po, struct pmc_owner *, sizeof(struct pmc_owner),
1613147191Sjkoshy	    M_PMC, M_ZERO|M_WAITOK);
1614145256Sjkoshy
1615147191Sjkoshy	po->po_sscount = po->po_error = po->po_flags = 0;
1616147191Sjkoshy	po->po_file  = NULL;
1617145256Sjkoshy	po->po_owner = p;
1618147191Sjkoshy	po->po_kthread = NULL;
1619145256Sjkoshy	LIST_INIT(&po->po_pmcs);
1620145256Sjkoshy	LIST_INSERT_HEAD(poh, po, po_next); /* insert into hash table */
1621145256Sjkoshy
1622147191Sjkoshy	TAILQ_INIT(&po->po_logbuffers);
1623147191Sjkoshy	mtx_init(&po->po_mtx, "pmc-owner-mtx", "pmc", MTX_SPIN);
1624147191Sjkoshy
1625145256Sjkoshy	PMCDBG(OWN,ALL,1, "allocate-owner proc=%p (%d, %s) pmc-owner=%p",
1626145256Sjkoshy	    p, p->p_pid, p->p_comm, po);
1627145256Sjkoshy
1628145256Sjkoshy	return po;
1629145256Sjkoshy}
1630145256Sjkoshy
1631147191Sjkoshystatic void
1632147191Sjkoshypmc_destroy_owner_descriptor(struct pmc_owner *po)
1633147191Sjkoshy{
1634147191Sjkoshy
1635147191Sjkoshy	PMCDBG(OWN,REL,1, "destroy-owner po=%p proc=%p (%d, %s)",
1636147191Sjkoshy	    po, po->po_owner, po->po_owner->p_pid, po->po_owner->p_comm);
1637147191Sjkoshy
1638147191Sjkoshy	mtx_destroy(&po->po_mtx);
1639147191Sjkoshy	FREE(po, M_PMC);
1640147191Sjkoshy}
1641147191Sjkoshy
1642145256Sjkoshy/*
1643145256Sjkoshy * find the descriptor corresponding to process 'p', adding or removing it
1644145256Sjkoshy * as specified by 'mode'.
1645145256Sjkoshy */
1646145256Sjkoshy
1647145256Sjkoshystatic struct pmc_process *
1648145256Sjkoshypmc_find_process_descriptor(struct proc *p, uint32_t mode)
1649145256Sjkoshy{
1650145256Sjkoshy	uint32_t hindex;
1651145256Sjkoshy	struct pmc_process *pp, *ppnew;
1652145256Sjkoshy	struct pmc_processhash *pph;
1653145256Sjkoshy
1654145256Sjkoshy	hindex = PMC_HASH_PTR(p, pmc_processhashmask);
1655145256Sjkoshy	pph = &pmc_processhash[hindex];
1656145256Sjkoshy
1657145256Sjkoshy	ppnew = NULL;
1658145256Sjkoshy
1659145256Sjkoshy	/*
1660145256Sjkoshy	 * Pre-allocate memory in the FIND_ALLOCATE case since we
1661145256Sjkoshy	 * cannot call malloc(9) once we hold a spin lock.
1662145256Sjkoshy	 */
1663145256Sjkoshy
1664145256Sjkoshy	if (mode & PMC_FLAG_ALLOCATE) {
1665145256Sjkoshy		/* allocate additional space for 'n' pmc pointers */
1666145256Sjkoshy		MALLOC(ppnew, struct pmc_process *,
1667145256Sjkoshy		    sizeof(struct pmc_process) + md->pmd_npmc *
1668145256Sjkoshy		    sizeof(struct pmc_targetstate), M_PMC, M_ZERO|M_WAITOK);
1669145256Sjkoshy	}
1670145256Sjkoshy
1671145256Sjkoshy	mtx_lock_spin(&pmc_processhash_mtx);
1672145256Sjkoshy	LIST_FOREACH(pp, pph, pp_next)
1673145256Sjkoshy	    if (pp->pp_proc == p)
1674145256Sjkoshy		    break;
1675145256Sjkoshy
1676145256Sjkoshy	if ((mode & PMC_FLAG_REMOVE) && pp != NULL)
1677145256Sjkoshy		LIST_REMOVE(pp, pp_next);
1678145256Sjkoshy
1679145256Sjkoshy	if ((mode & PMC_FLAG_ALLOCATE) && pp == NULL &&
1680145256Sjkoshy	    ppnew != NULL) {
1681145256Sjkoshy		ppnew->pp_proc = p;
1682145256Sjkoshy		LIST_INSERT_HEAD(pph, ppnew, pp_next);
1683145256Sjkoshy		pp = ppnew;
1684145256Sjkoshy		ppnew = NULL;
1685145256Sjkoshy	}
1686145256Sjkoshy	mtx_unlock_spin(&pmc_processhash_mtx);
1687145256Sjkoshy
1688145256Sjkoshy	if (pp != NULL && ppnew != NULL)
1689145256Sjkoshy		FREE(ppnew, M_PMC);
1690145256Sjkoshy
1691145256Sjkoshy	return pp;
1692145256Sjkoshy}
1693145256Sjkoshy
1694145256Sjkoshy/*
1695145256Sjkoshy * remove a process descriptor from the process hash table.
1696145256Sjkoshy */
1697145256Sjkoshy
1698145256Sjkoshystatic void
1699145256Sjkoshypmc_remove_process_descriptor(struct pmc_process *pp)
1700145256Sjkoshy{
1701145256Sjkoshy	KASSERT(pp->pp_refcnt == 0,
1702145256Sjkoshy	    ("[pmc,%d] Removing process descriptor %p with count %d",
1703145256Sjkoshy		__LINE__, pp, pp->pp_refcnt));
1704145256Sjkoshy
1705145256Sjkoshy	mtx_lock_spin(&pmc_processhash_mtx);
1706145256Sjkoshy	LIST_REMOVE(pp, pp_next);
1707145256Sjkoshy	mtx_unlock_spin(&pmc_processhash_mtx);
1708145256Sjkoshy}
1709145256Sjkoshy
1710145256Sjkoshy
1711145256Sjkoshy/*
1712145256Sjkoshy * find an owner descriptor corresponding to proc 'p'
1713145256Sjkoshy */
1714145256Sjkoshy
1715145256Sjkoshystatic struct pmc_owner *
1716145256Sjkoshypmc_find_owner_descriptor(struct proc *p)
1717145256Sjkoshy{
1718145256Sjkoshy	uint32_t hindex;
1719145256Sjkoshy	struct pmc_owner *po;
1720145256Sjkoshy	struct pmc_ownerhash *poh;
1721145256Sjkoshy
1722145256Sjkoshy	hindex = PMC_HASH_PTR(p, pmc_ownerhashmask);
1723145256Sjkoshy	poh = &pmc_ownerhash[hindex];
1724145256Sjkoshy
1725145256Sjkoshy	po = NULL;
1726145256Sjkoshy	LIST_FOREACH(po, poh, po_next)
1727145256Sjkoshy	    if (po->po_owner == p)
1728145256Sjkoshy		    break;
1729145256Sjkoshy
1730145256Sjkoshy	PMCDBG(OWN,FND,1, "find-owner proc=%p (%d, %s) hindex=0x%x -> "
1731145256Sjkoshy	    "pmc-owner=%p", p, p->p_pid, p->p_comm, hindex, po);
1732145256Sjkoshy
1733145256Sjkoshy	return po;
1734145256Sjkoshy}
1735145256Sjkoshy
1736145256Sjkoshy/*
1737145256Sjkoshy * pmc_allocate_pmc_descriptor
1738145256Sjkoshy *
1739145256Sjkoshy * Allocate a pmc descriptor and initialize its
1740145256Sjkoshy * fields.
1741145256Sjkoshy */
1742145256Sjkoshy
1743145256Sjkoshystatic struct pmc *
1744145256Sjkoshypmc_allocate_pmc_descriptor(void)
1745145256Sjkoshy{
1746145256Sjkoshy	struct pmc *pmc;
1747145256Sjkoshy
1748145256Sjkoshy	MALLOC(pmc, struct pmc *, sizeof(struct pmc), M_PMC, M_ZERO|M_WAITOK);
1749145256Sjkoshy
1750145256Sjkoshy	if (pmc != NULL) {
1751145256Sjkoshy		pmc->pm_owner = NULL;
1752145256Sjkoshy		LIST_INIT(&pmc->pm_targets);
1753145256Sjkoshy	}
1754145256Sjkoshy
1755145256Sjkoshy	PMCDBG(PMC,ALL,1, "allocate-pmc -> pmc=%p", pmc);
1756145256Sjkoshy
1757145256Sjkoshy	return pmc;
1758145256Sjkoshy}
1759145256Sjkoshy
1760145256Sjkoshy/*
1761145256Sjkoshy * Destroy a pmc descriptor.
1762145256Sjkoshy */
1763145256Sjkoshy
1764145256Sjkoshystatic void
1765145256Sjkoshypmc_destroy_pmc_descriptor(struct pmc *pm)
1766145256Sjkoshy{
1767145256Sjkoshy	(void) pm;
1768145256Sjkoshy
1769145256Sjkoshy#if	DEBUG
1770145256Sjkoshy	KASSERT(pm->pm_state == PMC_STATE_DELETED ||
1771145256Sjkoshy	    pm->pm_state == PMC_STATE_FREE,
1772145256Sjkoshy	    ("[pmc,%d] destroying non-deleted PMC", __LINE__));
1773145256Sjkoshy	KASSERT(LIST_EMPTY(&pm->pm_targets),
1774145256Sjkoshy	    ("[pmc,%d] destroying pmc with targets", __LINE__));
1775145256Sjkoshy	KASSERT(pm->pm_owner == NULL,
1776145256Sjkoshy	    ("[pmc,%d] destroying pmc attached to an owner", __LINE__));
1777145256Sjkoshy	KASSERT(pm->pm_runcount == 0,
1778145256Sjkoshy	    ("[pmc,%d] pmc has non-zero run count %d", __LINE__,
1779145256Sjkoshy		pm->pm_runcount));
1780145256Sjkoshy#endif
1781145256Sjkoshy}
1782145256Sjkoshy
1783147191Sjkoshystatic void
1784147191Sjkoshypmc_wait_for_pmc_idle(struct pmc *pm)
1785147191Sjkoshy{
1786147191Sjkoshy#if	DEBUG
1787147191Sjkoshy	volatile int maxloop;
1788147191Sjkoshy
1789147191Sjkoshy	maxloop = 100 * mp_ncpus;
1790147191Sjkoshy#endif
1791147191Sjkoshy
1792147191Sjkoshy	/*
1793147191Sjkoshy	 * Loop (with a forced context switch) till the PMC's runcount
1794147191Sjkoshy	 * comes down to zero.
1795147191Sjkoshy	 */
1796147191Sjkoshy	while (atomic_load_acq_32(&pm->pm_runcount) > 0) {
1797147191Sjkoshy#if	DEBUG
1798147191Sjkoshy		maxloop--;
1799147191Sjkoshy		KASSERT(maxloop > 0,
1800147191Sjkoshy		    ("[pmc,%d] (ri%d, rc%d) waiting too long for "
1801147191Sjkoshy			"pmc to be free", __LINE__,
1802147191Sjkoshy			PMC_TO_ROWINDEX(pm), pm->pm_runcount));
1803147191Sjkoshy#endif
1804147191Sjkoshy		pmc_force_context_switch();
1805147191Sjkoshy	}
1806147191Sjkoshy}
1807147191Sjkoshy
1808145256Sjkoshy/*
1809145256Sjkoshy * This function does the following things:
1810145256Sjkoshy *
1811145256Sjkoshy *  - detaches the PMC from hardware
1812145256Sjkoshy *  - unlinks all target threads that were attached to it
1813145256Sjkoshy *  - removes the PMC from its owner's list
1814145256Sjkoshy *  - destroy's the PMC private mutex
1815145256Sjkoshy *
1816145256Sjkoshy * Once this function completes, the given pmc pointer can be safely
1817145256Sjkoshy * FREE'd by the caller.
1818145256Sjkoshy */
1819145256Sjkoshy
1820145256Sjkoshystatic void
1821145256Sjkoshypmc_release_pmc_descriptor(struct pmc *pm)
1822145256Sjkoshy{
1823145256Sjkoshy	u_int ri, cpu;
1824145774Sjkoshy	enum pmc_mode mode;
1825145256Sjkoshy	struct pmc_hw *phw;
1826147191Sjkoshy	struct pmc_owner *po;
1827145256Sjkoshy	struct pmc_process *pp;
1828145256Sjkoshy	struct pmc_target *ptgt, *tmp;
1829145256Sjkoshy	struct pmc_binding pb;
1830145256Sjkoshy
1831145256Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
1832145256Sjkoshy
1833145256Sjkoshy	KASSERT(pm, ("[pmc,%d] null pmc", __LINE__));
1834145256Sjkoshy
1835145774Sjkoshy	ri   = PMC_TO_ROWINDEX(pm);
1836145774Sjkoshy	mode = PMC_TO_MODE(pm);
1837145256Sjkoshy
1838145256Sjkoshy	PMCDBG(PMC,REL,1, "release-pmc pmc=%p ri=%d mode=%d", pm, ri,
1839145774Sjkoshy	    mode);
1840145256Sjkoshy
1841145256Sjkoshy	/*
1842145256Sjkoshy	 * First, we take the PMC off hardware.
1843145256Sjkoshy	 */
1844145301Simp	cpu = 0;
1845145774Sjkoshy	if (PMC_IS_SYSTEM_MODE(mode)) {
1846145256Sjkoshy
1847145256Sjkoshy		/*
1848145256Sjkoshy		 * A system mode PMC runs on a specific CPU.  Switch
1849145256Sjkoshy		 * to this CPU and turn hardware off.
1850145256Sjkoshy		 */
1851145256Sjkoshy		pmc_save_cpu_binding(&pb);
1852145256Sjkoshy
1853145774Sjkoshy		cpu = PMC_TO_CPU(pm);
1854145256Sjkoshy
1855147191Sjkoshy		pmc_select_cpu(cpu);
1856145256Sjkoshy
1857147191Sjkoshy		/* switch off non-stalled CPUs */
1858147191Sjkoshy		if (pm->pm_state == PMC_STATE_RUNNING &&
1859147867Sjkoshy		    pm->pm_stalled == 0) {
1860145256Sjkoshy
1861145256Sjkoshy			phw = pmc_pcpu[cpu]->pc_hwpmcs[ri];
1862145256Sjkoshy
1863145256Sjkoshy			KASSERT(phw->phw_pmc == pm,
1864145256Sjkoshy			    ("[pmc, %d] pmc ptr ri(%d) hw(%p) pm(%p)",
1865145256Sjkoshy				__LINE__, ri, phw->phw_pmc, pm));
1866145256Sjkoshy			PMCDBG(PMC,REL,2, "stopping cpu=%d ri=%d", cpu, ri);
1867145256Sjkoshy
1868145256Sjkoshy			critical_enter();
1869145256Sjkoshy			md->pmd_stop_pmc(cpu, ri);
1870145256Sjkoshy			critical_exit();
1871145256Sjkoshy		}
1872145256Sjkoshy
1873145256Sjkoshy		PMCDBG(PMC,REL,2, "decfg cpu=%d ri=%d", cpu, ri);
1874145256Sjkoshy
1875145256Sjkoshy		critical_enter();
1876145256Sjkoshy		md->pmd_config_pmc(cpu, ri, NULL);
1877145256Sjkoshy		critical_exit();
1878145256Sjkoshy
1879147191Sjkoshy		/* adjust the global and process count of SS mode PMCs */
1880147191Sjkoshy		if (mode == PMC_MODE_SS && pm->pm_state == PMC_STATE_RUNNING) {
1881147191Sjkoshy			po = pm->pm_owner;
1882147191Sjkoshy			po->po_sscount--;
1883147191Sjkoshy			if (po->po_sscount == 0) {
1884147191Sjkoshy				atomic_subtract_rel_int(&pmc_ss_count, 1);
1885147191Sjkoshy				LIST_REMOVE(po, po_ssnext);
1886147191Sjkoshy			}
1887147191Sjkoshy		}
1888147191Sjkoshy
1889145256Sjkoshy		pm->pm_state = PMC_STATE_DELETED;
1890145256Sjkoshy
1891145256Sjkoshy		pmc_restore_cpu_binding(&pb);
1892145256Sjkoshy
1893147191Sjkoshy		/*
1894147191Sjkoshy		 * We could have references to this PMC structure in
1895147191Sjkoshy		 * the per-cpu sample queues.  Wait for the queue to
1896147191Sjkoshy		 * drain.
1897147191Sjkoshy		 */
1898147191Sjkoshy		pmc_wait_for_pmc_idle(pm);
1899147191Sjkoshy
1900145774Sjkoshy	} else if (PMC_IS_VIRTUAL_MODE(mode)) {
1901145256Sjkoshy
1902145256Sjkoshy		/*
1903145256Sjkoshy		 * A virtual PMC could be running on multiple CPUs at
1904145256Sjkoshy		 * a given instant.
1905145256Sjkoshy		 *
1906145256Sjkoshy		 * By marking its state as DELETED, we ensure that
1907145256Sjkoshy		 * this PMC is never further scheduled on hardware.
1908145256Sjkoshy		 *
1909145256Sjkoshy		 * Then we wait till all CPUs are done with this PMC.
1910145256Sjkoshy		 */
1911145256Sjkoshy		pm->pm_state = PMC_STATE_DELETED;
1912145256Sjkoshy
1913145256Sjkoshy
1914147191Sjkoshy		/* Wait for the PMCs runcount to come to zero. */
1915147191Sjkoshy		pmc_wait_for_pmc_idle(pm);
1916145256Sjkoshy
1917145256Sjkoshy		/*
1918145256Sjkoshy		 * At this point the PMC is off all CPUs and cannot be
1919145256Sjkoshy		 * freshly scheduled onto a CPU.  It is now safe to
1920145256Sjkoshy		 * unlink all targets from this PMC.  If a
1921145256Sjkoshy		 * process-record's refcount falls to zero, we remove
1922145256Sjkoshy		 * it from the hash table.  The module-wide SX lock
1923145256Sjkoshy		 * protects us from races.
1924145256Sjkoshy		 */
1925145256Sjkoshy		LIST_FOREACH_SAFE(ptgt, &pm->pm_targets, pt_next, tmp) {
1926145256Sjkoshy			pp = ptgt->pt_process;
1927145256Sjkoshy			pmc_unlink_target_process(pm, pp); /* frees 'ptgt' */
1928145256Sjkoshy
1929145256Sjkoshy			PMCDBG(PMC,REL,3, "pp->refcnt=%d", pp->pp_refcnt);
1930145256Sjkoshy
1931145256Sjkoshy			/*
1932145256Sjkoshy			 * If the target process record shows that no
1933145256Sjkoshy			 * PMCs are attached to it, reclaim its space.
1934145256Sjkoshy			 */
1935145256Sjkoshy
1936145256Sjkoshy			if (pp->pp_refcnt == 0) {
1937145256Sjkoshy				pmc_remove_process_descriptor(pp);
1938145256Sjkoshy				FREE(pp, M_PMC);
1939145256Sjkoshy			}
1940145256Sjkoshy		}
1941145256Sjkoshy
1942145256Sjkoshy		cpu = curthread->td_oncpu; /* setup cpu for pmd_release() */
1943145256Sjkoshy
1944145256Sjkoshy	}
1945145256Sjkoshy
1946145256Sjkoshy	/*
1947145256Sjkoshy	 * Release any MD resources
1948145256Sjkoshy	 */
1949145256Sjkoshy
1950145256Sjkoshy	(void) md->pmd_release_pmc(cpu, ri, pm);
1951145256Sjkoshy
1952145256Sjkoshy	/*
1953145256Sjkoshy	 * Update row disposition
1954145256Sjkoshy	 */
1955145256Sjkoshy
1956145774Sjkoshy	if (PMC_IS_SYSTEM_MODE(PMC_TO_MODE(pm)))
1957145256Sjkoshy		PMC_UNMARK_ROW_STANDALONE(ri);
1958145256Sjkoshy	else
1959145256Sjkoshy		PMC_UNMARK_ROW_THREAD(ri);
1960145256Sjkoshy
1961145256Sjkoshy	/* unlink from the owner's list */
1962147191Sjkoshy	if (pm->pm_owner) {
1963147191Sjkoshy		LIST_REMOVE(pm, pm_next);
1964147191Sjkoshy		pm->pm_owner = NULL;
1965147191Sjkoshy	}
1966145256Sjkoshy
1967145256Sjkoshy	pmc_destroy_pmc_descriptor(pm);
1968145256Sjkoshy}
1969145256Sjkoshy
1970145256Sjkoshy/*
1971145256Sjkoshy * Register an owner and a pmc.
1972145256Sjkoshy */
1973145256Sjkoshy
1974145256Sjkoshystatic int
1975145256Sjkoshypmc_register_owner(struct proc *p, struct pmc *pmc)
1976145256Sjkoshy{
1977145256Sjkoshy	struct pmc_owner *po;
1978145256Sjkoshy
1979145256Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
1980145256Sjkoshy
1981145774Sjkoshy	if ((po = pmc_find_owner_descriptor(p)) == NULL)
1982147191Sjkoshy		if ((po = pmc_allocate_owner_descriptor(p)) == NULL)
1983145256Sjkoshy			return ENOMEM;
1984145256Sjkoshy
1985145256Sjkoshy	KASSERT(pmc->pm_owner == NULL,
1986145256Sjkoshy	    ("[pmc,%d] attempting to own an initialized PMC", __LINE__));
1987145256Sjkoshy	pmc->pm_owner  = po;
1988145256Sjkoshy
1989147191Sjkoshy	LIST_INSERT_HEAD(&po->po_pmcs, pmc, pm_next);
1990145256Sjkoshy
1991145256Sjkoshy	PROC_LOCK(p);
1992145256Sjkoshy	p->p_flag |= P_HWPMC;
1993145256Sjkoshy	PROC_UNLOCK(p);
1994145256Sjkoshy
1995147191Sjkoshy	if (po->po_flags & PMC_PO_OWNS_LOGFILE)
1996147191Sjkoshy		pmclog_process_pmcallocate(pmc);
1997145256Sjkoshy
1998147191Sjkoshy	PMCDBG(PMC,REG,1, "register-owner pmc-owner=%p pmc=%p",
1999147191Sjkoshy	    po, pmc);
2000147191Sjkoshy
2001145256Sjkoshy	return 0;
2002145256Sjkoshy}
2003145256Sjkoshy
2004145256Sjkoshy/*
2005145256Sjkoshy * Return the current row disposition:
2006145256Sjkoshy * == 0 => FREE
2007145256Sjkoshy *  > 0 => PROCESS MODE
2008145256Sjkoshy *  < 0 => SYSTEM MODE
2009145256Sjkoshy */
2010145256Sjkoshy
2011145256Sjkoshyint
2012145256Sjkoshypmc_getrowdisp(int ri)
2013145256Sjkoshy{
2014145256Sjkoshy	return pmc_pmcdisp[ri];
2015145256Sjkoshy}
2016145256Sjkoshy
2017145256Sjkoshy/*
2018145256Sjkoshy * Check if a PMC at row index 'ri' can be allocated to the current
2019145256Sjkoshy * process.
2020145256Sjkoshy *
2021145256Sjkoshy * Allocation can fail if:
2022145256Sjkoshy *   - the current process is already being profiled by a PMC at index 'ri',
2023145256Sjkoshy *     attached to it via OP_PMCATTACH.
2024145256Sjkoshy *   - the current process has already allocated a PMC at index 'ri'
2025145256Sjkoshy *     via OP_ALLOCATE.
2026145256Sjkoshy */
2027145256Sjkoshy
2028145256Sjkoshystatic int
2029145774Sjkoshypmc_can_allocate_rowindex(struct proc *p, unsigned int ri, int cpu)
2030145256Sjkoshy{
2031145774Sjkoshy	enum pmc_mode mode;
2032145774Sjkoshy	struct pmc *pm;
2033145256Sjkoshy	struct pmc_owner *po;
2034145256Sjkoshy	struct pmc_process *pp;
2035145256Sjkoshy
2036145774Sjkoshy	PMCDBG(PMC,ALR,1, "can-allocate-rowindex proc=%p (%d, %s) ri=%d "
2037145774Sjkoshy	    "cpu=%d", p, p->p_pid, p->p_comm, ri, cpu);
2038145256Sjkoshy
2039145774Sjkoshy	/*
2040145774Sjkoshy	 * We shouldn't have already allocated a process-mode PMC at
2041145774Sjkoshy	 * row index 'ri'.
2042145774Sjkoshy	 *
2043145774Sjkoshy	 * We shouldn't have allocated a system-wide PMC on the same
2044145774Sjkoshy	 * CPU and same RI.
2045145774Sjkoshy	 */
2046145256Sjkoshy	if ((po = pmc_find_owner_descriptor(p)) != NULL)
2047147191Sjkoshy		LIST_FOREACH(pm, &po->po_pmcs, pm_next) {
2048145774Sjkoshy		    if (PMC_TO_ROWINDEX(pm) == ri) {
2049145774Sjkoshy			    mode = PMC_TO_MODE(pm);
2050145774Sjkoshy			    if (PMC_IS_VIRTUAL_MODE(mode))
2051145774Sjkoshy				    return EEXIST;
2052145774Sjkoshy			    if (PMC_IS_SYSTEM_MODE(mode) &&
2053145774Sjkoshy				(int) PMC_TO_CPU(pm) == cpu)
2054145774Sjkoshy				    return EEXIST;
2055145774Sjkoshy		    }
2056145774Sjkoshy	        }
2057145256Sjkoshy
2058145774Sjkoshy	/*
2059145774Sjkoshy	 * We also shouldn't be the target of any PMC at this index
2060145774Sjkoshy	 * since otherwise a PMC_ATTACH to ourselves will fail.
2061145774Sjkoshy	 */
2062145256Sjkoshy	if ((pp = pmc_find_process_descriptor(p, 0)) != NULL)
2063145256Sjkoshy		if (pp->pp_pmcs[ri].pp_pmc)
2064145256Sjkoshy			return EEXIST;
2065145256Sjkoshy
2066145256Sjkoshy	PMCDBG(PMC,ALR,2, "can-allocate-rowindex proc=%p (%d, %s) ri=%d ok",
2067145256Sjkoshy	    p, p->p_pid, p->p_comm, ri);
2068145256Sjkoshy
2069145256Sjkoshy	return 0;
2070145256Sjkoshy}
2071145256Sjkoshy
2072145256Sjkoshy/*
2073145256Sjkoshy * Check if a given PMC at row index 'ri' can be currently used in
2074145256Sjkoshy * mode 'mode'.
2075145256Sjkoshy */
2076145256Sjkoshy
2077145256Sjkoshystatic int
2078145256Sjkoshypmc_can_allocate_row(int ri, enum pmc_mode mode)
2079145256Sjkoshy{
2080145256Sjkoshy	enum pmc_disp	disp;
2081145256Sjkoshy
2082145256Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
2083145256Sjkoshy
2084145256Sjkoshy	PMCDBG(PMC,ALR,1, "can-allocate-row ri=%d mode=%d", ri, mode);
2085145256Sjkoshy
2086145256Sjkoshy	if (PMC_IS_SYSTEM_MODE(mode))
2087145256Sjkoshy		disp = PMC_DISP_STANDALONE;
2088145256Sjkoshy	else
2089145256Sjkoshy		disp = PMC_DISP_THREAD;
2090145256Sjkoshy
2091145256Sjkoshy	/*
2092145256Sjkoshy	 * check disposition for PMC row 'ri':
2093145256Sjkoshy	 *
2094145256Sjkoshy	 * Expected disposition		Row-disposition		Result
2095145256Sjkoshy	 *
2096145256Sjkoshy	 * STANDALONE			STANDALONE or FREE	proceed
2097145256Sjkoshy	 * STANDALONE			THREAD			fail
2098145256Sjkoshy	 * THREAD			THREAD or FREE		proceed
2099145256Sjkoshy	 * THREAD			STANDALONE		fail
2100145256Sjkoshy	 */
2101145256Sjkoshy
2102145256Sjkoshy	if (!PMC_ROW_DISP_IS_FREE(ri) &&
2103145256Sjkoshy	    !(disp == PMC_DISP_THREAD && PMC_ROW_DISP_IS_THREAD(ri)) &&
2104145256Sjkoshy	    !(disp == PMC_DISP_STANDALONE && PMC_ROW_DISP_IS_STANDALONE(ri)))
2105145256Sjkoshy		return EBUSY;
2106145256Sjkoshy
2107145256Sjkoshy	/*
2108145256Sjkoshy	 * All OK
2109145256Sjkoshy	 */
2110145256Sjkoshy
2111145256Sjkoshy	PMCDBG(PMC,ALR,2, "can-allocate-row ri=%d mode=%d ok", ri, mode);
2112145256Sjkoshy
2113145256Sjkoshy	return 0;
2114145256Sjkoshy
2115145256Sjkoshy}
2116145256Sjkoshy
2117145256Sjkoshy/*
2118145774Sjkoshy * Find a PMC descriptor with user handle 'pmcid' for thread 'td'.
2119145256Sjkoshy */
2120145256Sjkoshy
2121145256Sjkoshystatic struct pmc *
2122145256Sjkoshypmc_find_pmc_descriptor_in_process(struct pmc_owner *po, pmc_id_t pmcid)
2123145256Sjkoshy{
2124147191Sjkoshy	struct pmc *pm;
2125145256Sjkoshy
2126145774Sjkoshy	KASSERT(PMC_ID_TO_ROWINDEX(pmcid) < md->pmd_npmc,
2127145774Sjkoshy	    ("[pmc,%d] Illegal pmc index %d (max %d)", __LINE__,
2128145774Sjkoshy		PMC_ID_TO_ROWINDEX(pmcid), md->pmd_npmc));
2129145256Sjkoshy
2130147191Sjkoshy	LIST_FOREACH(pm, &po->po_pmcs, pm_next)
2131147191Sjkoshy	    if (pm->pm_id == pmcid)
2132147191Sjkoshy		    return pm;
2133145256Sjkoshy
2134145256Sjkoshy	return NULL;
2135145256Sjkoshy}
2136145256Sjkoshy
2137145256Sjkoshystatic int
2138145256Sjkoshypmc_find_pmc(pmc_id_t pmcid, struct pmc **pmc)
2139145256Sjkoshy{
2140145256Sjkoshy
2141145256Sjkoshy	struct pmc *pm;
2142145256Sjkoshy	struct pmc_owner *po;
2143145256Sjkoshy
2144145256Sjkoshy	PMCDBG(PMC,FND,1, "find-pmc id=%d", pmcid);
2145145256Sjkoshy
2146145256Sjkoshy	if ((po = pmc_find_owner_descriptor(curthread->td_proc)) == NULL)
2147145256Sjkoshy		return ESRCH;
2148145256Sjkoshy
2149145256Sjkoshy	if ((pm = pmc_find_pmc_descriptor_in_process(po, pmcid)) == NULL)
2150145256Sjkoshy		return EINVAL;
2151145256Sjkoshy
2152145256Sjkoshy	PMCDBG(PMC,FND,2, "find-pmc id=%d -> pmc=%p", pmcid, pm);
2153145256Sjkoshy
2154145256Sjkoshy	*pmc = pm;
2155145256Sjkoshy	return 0;
2156145256Sjkoshy}
2157145256Sjkoshy
2158145256Sjkoshy/*
2159145256Sjkoshy * Start a PMC.
2160145256Sjkoshy */
2161145256Sjkoshy
2162145256Sjkoshystatic int
2163145256Sjkoshypmc_start(struct pmc *pm)
2164145256Sjkoshy{
2165145256Sjkoshy	int error, cpu, ri;
2166145774Sjkoshy	enum pmc_mode mode;
2167147191Sjkoshy	struct pmc_owner *po;
2168145256Sjkoshy	struct pmc_binding pb;
2169145256Sjkoshy
2170145256Sjkoshy	KASSERT(pm != NULL,
2171145256Sjkoshy	    ("[pmc,%d] null pm", __LINE__));
2172145256Sjkoshy
2173145774Sjkoshy	mode = PMC_TO_MODE(pm);
2174145774Sjkoshy	ri   = PMC_TO_ROWINDEX(pm);
2175145774Sjkoshy	error = 0;
2176145256Sjkoshy
2177145774Sjkoshy	PMCDBG(PMC,OPS,1, "start pmc=%p mode=%d ri=%d", pm, mode, ri);
2178145774Sjkoshy
2179147191Sjkoshy	po = pm->pm_owner;
2180145256Sjkoshy
2181145774Sjkoshy	if (PMC_IS_VIRTUAL_MODE(mode)) {
2182145256Sjkoshy
2183145256Sjkoshy		/*
2184147191Sjkoshy		 * If a PMCATTACH has never been done on this PMC,
2185147191Sjkoshy		 * attach it to its owner process.
2186145256Sjkoshy		 */
2187145256Sjkoshy
2188145256Sjkoshy		if (LIST_EMPTY(&pm->pm_targets))
2189147191Sjkoshy			error = (pm->pm_flags & PMC_F_ATTACH_DONE) ? ESRCH :
2190147191Sjkoshy			    pmc_attach_process(po->po_owner, pm);
2191145256Sjkoshy
2192145774Sjkoshy		/*
2193147191Sjkoshy		 * Disallow PMCSTART if a logfile is required but has not
2194147191Sjkoshy		 * been configured yet.
2195145774Sjkoshy		 */
2196145256Sjkoshy
2197147191Sjkoshy		if (error == 0 && (pm->pm_flags & PMC_F_NEEDS_LOGFILE) &&
2198147191Sjkoshy		    (po->po_flags & PMC_PO_OWNS_LOGFILE) == 0)
2199147191Sjkoshy			error = EDOOFUS;
2200147191Sjkoshy
2201145256Sjkoshy		/*
2202147191Sjkoshy		 * If the PMC is attached to its owner, then force a context
2203147191Sjkoshy		 * switch to ensure that the MD state gets set correctly.
2204145256Sjkoshy		 */
2205145256Sjkoshy
2206147191Sjkoshy		if (error == 0) {
2207147191Sjkoshy			pm->pm_state = PMC_STATE_RUNNING;
2208147191Sjkoshy			if (pm->pm_flags & PMC_F_ATTACHED_TO_OWNER)
2209147191Sjkoshy				pmc_force_context_switch();
2210147191Sjkoshy		}
2211147191Sjkoshy
2212145774Sjkoshy		return error;
2213147191Sjkoshy	}
2214145256Sjkoshy
2215147191Sjkoshy
2216147191Sjkoshy	/*
2217147191Sjkoshy	 * A system-wide PMC.
2218147191Sjkoshy	 */
2219147191Sjkoshy
2220147191Sjkoshy	if ((pm->pm_flags & PMC_F_NEEDS_LOGFILE) &&
2221147191Sjkoshy	    (po->po_flags & PMC_PO_OWNS_LOGFILE) == 0)
2222147191Sjkoshy		return EDOOFUS;	/* programming error */
2223147191Sjkoshy
2224147191Sjkoshy	/*
2225147191Sjkoshy	 * Add the owner to the global list if this is a system-wide
2226147191Sjkoshy	 * sampling PMC.
2227147191Sjkoshy	 */
2228147191Sjkoshy
2229147191Sjkoshy	if (mode == PMC_MODE_SS) {
2230147191Sjkoshy		if (po->po_sscount == 0) {
2231147191Sjkoshy			LIST_INSERT_HEAD(&pmc_ss_owners, po, po_ssnext);
2232147191Sjkoshy			atomic_add_rel_int(&pmc_ss_count, 1);
2233147191Sjkoshy			PMCDBG(PMC,OPS,1, "po=%p in global list", po);
2234147191Sjkoshy		}
2235147191Sjkoshy		po->po_sscount++;
2236145256Sjkoshy	}
2237145256Sjkoshy
2238145256Sjkoshy	/*
2239147191Sjkoshy	 * Move to the CPU associated with this
2240145256Sjkoshy	 * PMC, and start the hardware.
2241145256Sjkoshy	 */
2242145256Sjkoshy
2243145256Sjkoshy	pmc_save_cpu_binding(&pb);
2244145256Sjkoshy
2245145774Sjkoshy	cpu = PMC_TO_CPU(pm);
2246145256Sjkoshy
2247145256Sjkoshy	if (pmc_cpu_is_disabled(cpu))
2248145256Sjkoshy		return ENXIO;
2249145256Sjkoshy
2250145256Sjkoshy	pmc_select_cpu(cpu);
2251145256Sjkoshy
2252145256Sjkoshy	/*
2253145256Sjkoshy	 * global PMCs are configured at allocation time
2254145256Sjkoshy	 * so write out the initial value and start the PMC.
2255145256Sjkoshy	 */
2256145256Sjkoshy
2257147191Sjkoshy	pm->pm_state = PMC_STATE_RUNNING;
2258147191Sjkoshy
2259145774Sjkoshy	critical_enter();
2260145256Sjkoshy	if ((error = md->pmd_write_pmc(cpu, ri,
2261145774Sjkoshy		 PMC_IS_SAMPLING_MODE(mode) ?
2262145256Sjkoshy		 pm->pm_sc.pm_reloadcount :
2263145256Sjkoshy		 pm->pm_sc.pm_initial)) == 0)
2264145256Sjkoshy		error = md->pmd_start_pmc(cpu, ri);
2265145774Sjkoshy	critical_exit();
2266145256Sjkoshy
2267145256Sjkoshy	pmc_restore_cpu_binding(&pb);
2268145256Sjkoshy
2269145256Sjkoshy	return error;
2270145256Sjkoshy}
2271145256Sjkoshy
2272145256Sjkoshy/*
2273145256Sjkoshy * Stop a PMC.
2274145256Sjkoshy */
2275145256Sjkoshy
2276145256Sjkoshystatic int
2277145256Sjkoshypmc_stop(struct pmc *pm)
2278145256Sjkoshy{
2279145774Sjkoshy	int cpu, error, ri;
2280147191Sjkoshy	struct pmc_owner *po;
2281145256Sjkoshy	struct pmc_binding pb;
2282145256Sjkoshy
2283145256Sjkoshy	KASSERT(pm != NULL, ("[pmc,%d] null pmc", __LINE__));
2284145256Sjkoshy
2285145774Sjkoshy	PMCDBG(PMC,OPS,1, "stop pmc=%p mode=%d ri=%d", pm,
2286145774Sjkoshy	    PMC_TO_MODE(pm), PMC_TO_ROWINDEX(pm));
2287145256Sjkoshy
2288145256Sjkoshy	pm->pm_state = PMC_STATE_STOPPED;
2289145256Sjkoshy
2290145256Sjkoshy	/*
2291145256Sjkoshy	 * If the PMC is a virtual mode one, changing the state to
2292145256Sjkoshy	 * non-RUNNING is enough to ensure that the PMC never gets
2293145256Sjkoshy	 * scheduled.
2294145256Sjkoshy	 *
2295145256Sjkoshy	 * If this PMC is current running on a CPU, then it will
2296145256Sjkoshy	 * handled correctly at the time its target process is context
2297145256Sjkoshy	 * switched out.
2298145256Sjkoshy	 */
2299145256Sjkoshy
2300145774Sjkoshy	if (PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm)))
2301145256Sjkoshy		return 0;
2302145256Sjkoshy
2303145256Sjkoshy	/*
2304145256Sjkoshy	 * A system-mode PMC.  Move to the CPU associated with
2305145256Sjkoshy	 * this PMC, and stop the hardware.  We update the
2306145256Sjkoshy	 * 'initial count' so that a subsequent PMCSTART will
2307145256Sjkoshy	 * resume counting from the current hardware count.
2308145256Sjkoshy	 */
2309145256Sjkoshy
2310145256Sjkoshy	pmc_save_cpu_binding(&pb);
2311145256Sjkoshy
2312145774Sjkoshy	cpu = PMC_TO_CPU(pm);
2313145256Sjkoshy
2314145774Sjkoshy	KASSERT(cpu >= 0 && cpu < mp_ncpus,
2315145774Sjkoshy	    ("[pmc,%d] illegal cpu=%d", __LINE__, cpu));
2316145774Sjkoshy
2317145256Sjkoshy	if (pmc_cpu_is_disabled(cpu))
2318145256Sjkoshy		return ENXIO;
2319145256Sjkoshy
2320145256Sjkoshy	pmc_select_cpu(cpu);
2321145256Sjkoshy
2322145774Sjkoshy	ri = PMC_TO_ROWINDEX(pm);
2323145256Sjkoshy
2324145774Sjkoshy	critical_enter();
2325145774Sjkoshy	if ((error = md->pmd_stop_pmc(cpu, ri)) == 0)
2326145774Sjkoshy		error = md->pmd_read_pmc(cpu, ri, &pm->pm_sc.pm_initial);
2327145774Sjkoshy	critical_exit();
2328145774Sjkoshy
2329145256Sjkoshy	pmc_restore_cpu_binding(&pb);
2330145256Sjkoshy
2331147191Sjkoshy	po = pm->pm_owner;
2332147191Sjkoshy
2333147191Sjkoshy	/* remove this owner from the global list of SS PMC owners */
2334147191Sjkoshy	if (PMC_TO_MODE(pm) == PMC_MODE_SS) {
2335147191Sjkoshy		po->po_sscount--;
2336147191Sjkoshy		if (po->po_sscount == 0) {
2337147191Sjkoshy			atomic_subtract_rel_int(&pmc_ss_count, 1);
2338147191Sjkoshy			LIST_REMOVE(po, po_ssnext);
2339147191Sjkoshy			PMCDBG(PMC,OPS,2,"po=%p removed from global list", po);
2340147191Sjkoshy		}
2341147191Sjkoshy	}
2342147191Sjkoshy
2343145256Sjkoshy	return error;
2344145256Sjkoshy}
2345145256Sjkoshy
2346145256Sjkoshy
2347145256Sjkoshy#if	DEBUG
2348145256Sjkoshystatic const char *pmc_op_to_name[] = {
2349145256Sjkoshy#undef	__PMC_OP
2350145256Sjkoshy#define	__PMC_OP(N, D)	#N ,
2351145256Sjkoshy	__PMC_OPS()
2352145256Sjkoshy	NULL
2353145256Sjkoshy};
2354145256Sjkoshy#endif
2355145256Sjkoshy
2356145256Sjkoshy/*
2357145256Sjkoshy * The syscall interface
2358145256Sjkoshy */
2359145256Sjkoshy
2360145256Sjkoshy#define	PMC_GET_SX_XLOCK(...) do {		\
2361145256Sjkoshy	sx_xlock(&pmc_sx);			\
2362145256Sjkoshy	if (pmc_hook == NULL) {			\
2363145256Sjkoshy		sx_xunlock(&pmc_sx);		\
2364145256Sjkoshy		return __VA_ARGS__;		\
2365145256Sjkoshy	}					\
2366145256Sjkoshy} while (0)
2367145256Sjkoshy
2368145256Sjkoshy#define	PMC_DOWNGRADE_SX() do {			\
2369145256Sjkoshy	sx_downgrade(&pmc_sx);			\
2370145256Sjkoshy	is_sx_downgraded = 1;			\
2371145256Sjkoshy} while (0)
2372145256Sjkoshy
2373145256Sjkoshystatic int
2374145256Sjkoshypmc_syscall_handler(struct thread *td, void *syscall_args)
2375145256Sjkoshy{
2376145256Sjkoshy	int error, is_sx_downgraded, op;
2377145256Sjkoshy	struct pmc_syscall_args *c;
2378145256Sjkoshy	void *arg;
2379145256Sjkoshy
2380145256Sjkoshy	PMC_GET_SX_XLOCK(ENOSYS);
2381145256Sjkoshy
2382147191Sjkoshy	DROP_GIANT();
2383147191Sjkoshy
2384145256Sjkoshy	is_sx_downgraded = 0;
2385145256Sjkoshy
2386145256Sjkoshy	c = (struct pmc_syscall_args *) syscall_args;
2387145256Sjkoshy
2388145256Sjkoshy	op = c->pmop_code;
2389145256Sjkoshy	arg = c->pmop_data;
2390145256Sjkoshy
2391145256Sjkoshy	PMCDBG(MOD,PMS,1, "syscall op=%d \"%s\" arg=%p", op,
2392145256Sjkoshy	    pmc_op_to_name[op], arg);
2393145256Sjkoshy
2394145256Sjkoshy	error = 0;
2395145256Sjkoshy	atomic_add_int(&pmc_stats.pm_syscalls, 1);
2396145256Sjkoshy
2397145256Sjkoshy	switch(op)
2398145256Sjkoshy	{
2399145256Sjkoshy
2400145256Sjkoshy
2401145256Sjkoshy	/*
2402145256Sjkoshy	 * Configure a log file.
2403145256Sjkoshy	 *
2404145256Sjkoshy	 * XXX This OP will be reworked.
2405145256Sjkoshy	 */
2406145256Sjkoshy
2407145256Sjkoshy	case PMC_OP_CONFIGURELOG:
2408145256Sjkoshy	{
2409145256Sjkoshy		struct pmc_owner *po;
2410145256Sjkoshy		struct pmc_op_configurelog cl;
2411145256Sjkoshy		struct proc *p;
2412145256Sjkoshy
2413145256Sjkoshy		sx_assert(&pmc_sx, SX_XLOCKED);
2414145256Sjkoshy
2415145256Sjkoshy		if ((error = copyin(arg, &cl, sizeof(cl))) != 0)
2416145256Sjkoshy			break;
2417145256Sjkoshy
2418145256Sjkoshy		/* mark this process as owning a log file */
2419145256Sjkoshy		p = td->td_proc;
2420145256Sjkoshy		if ((po = pmc_find_owner_descriptor(p)) == NULL)
2421147191Sjkoshy			if ((po = pmc_allocate_owner_descriptor(p)) == NULL) {
2422147191Sjkoshy				error = ENOMEM;
2423147191Sjkoshy				break;
2424147191Sjkoshy			}
2425145256Sjkoshy
2426147191Sjkoshy		/*
2427147191Sjkoshy		 * If a valid fd was passed in, try to configure that,
2428147191Sjkoshy		 * otherwise if 'fd' was less than zero and there was
2429147191Sjkoshy		 * a log file configured, flush its buffers and
2430147191Sjkoshy		 * de-configure it.
2431147191Sjkoshy		 */
2432147191Sjkoshy		if (cl.pm_logfd >= 0)
2433147191Sjkoshy			error = pmclog_configure_log(po, cl.pm_logfd);
2434147191Sjkoshy		else if (po->po_flags & PMC_PO_OWNS_LOGFILE) {
2435147191Sjkoshy			pmclog_process_closelog(po);
2436147191Sjkoshy			error = pmclog_flush(po);
2437147191Sjkoshy			if (error == 0)
2438147191Sjkoshy				error = pmclog_deconfigure_log(po);
2439147191Sjkoshy		} else
2440147191Sjkoshy			error = EINVAL;
2441147191Sjkoshy	}
2442147191Sjkoshy	break;
2443147191Sjkoshy
2444147191Sjkoshy
2445147191Sjkoshy	/*
2446147191Sjkoshy	 * Flush a log file.
2447147191Sjkoshy	 */
2448147191Sjkoshy
2449147191Sjkoshy	case PMC_OP_FLUSHLOG:
2450147191Sjkoshy	{
2451147191Sjkoshy		struct pmc_owner *po;
2452147191Sjkoshy
2453147191Sjkoshy		sx_assert(&pmc_sx, SX_XLOCKED);
2454147191Sjkoshy
2455147191Sjkoshy		if ((po = pmc_find_owner_descriptor(td->td_proc)) == NULL) {
2456147191Sjkoshy			error = EINVAL;
2457145256Sjkoshy			break;
2458147191Sjkoshy		}
2459145256Sjkoshy
2460147191Sjkoshy		error = pmclog_flush(po);
2461145256Sjkoshy	}
2462145256Sjkoshy	break;
2463145256Sjkoshy
2464145256Sjkoshy	/*
2465145256Sjkoshy	 * Retrieve hardware configuration.
2466145256Sjkoshy	 */
2467145256Sjkoshy
2468145256Sjkoshy	case PMC_OP_GETCPUINFO:	/* CPU information */
2469145256Sjkoshy	{
2470145256Sjkoshy		struct pmc_op_getcpuinfo gci;
2471145256Sjkoshy
2472145256Sjkoshy		gci.pm_cputype = md->pmd_cputype;
2473145774Sjkoshy		gci.pm_ncpu    = mp_ncpus;
2474145256Sjkoshy		gci.pm_npmc    = md->pmd_npmc;
2475145256Sjkoshy		gci.pm_nclass  = md->pmd_nclass;
2476145256Sjkoshy		bcopy(md->pmd_classes, &gci.pm_classes,
2477145256Sjkoshy		    sizeof(gci.pm_classes));
2478145256Sjkoshy		error = copyout(&gci, arg, sizeof(gci));
2479145256Sjkoshy	}
2480145256Sjkoshy	break;
2481145256Sjkoshy
2482145256Sjkoshy
2483145256Sjkoshy	/*
2484145256Sjkoshy	 * Get module statistics
2485145256Sjkoshy	 */
2486145256Sjkoshy
2487145256Sjkoshy	case PMC_OP_GETDRIVERSTATS:
2488145256Sjkoshy	{
2489145256Sjkoshy		struct pmc_op_getdriverstats gms;
2490145256Sjkoshy
2491145256Sjkoshy		bcopy(&pmc_stats, &gms, sizeof(gms));
2492145256Sjkoshy		error = copyout(&gms, arg, sizeof(gms));
2493145256Sjkoshy	}
2494145256Sjkoshy	break;
2495145256Sjkoshy
2496145256Sjkoshy
2497145256Sjkoshy	/*
2498145256Sjkoshy	 * Retrieve module version number
2499145256Sjkoshy	 */
2500145256Sjkoshy
2501145256Sjkoshy	case PMC_OP_GETMODULEVERSION:
2502145256Sjkoshy	{
2503147191Sjkoshy		uint32_t cv, modv;
2504147191Sjkoshy
2505147191Sjkoshy		/* retrieve the client's idea of the ABI version */
2506147191Sjkoshy		if ((error = copyin(arg, &cv, sizeof(uint32_t))) != 0)
2507147191Sjkoshy			break;
2508147191Sjkoshy		/* don't service clients newer than our driver */
2509147191Sjkoshy		modv = PMC_VERSION;
2510147191Sjkoshy		if ((cv & 0xFFFF0000) > (modv & 0xFFFF0000)) {
2511147191Sjkoshy			error = EPROGMISMATCH;
2512147191Sjkoshy			break;
2513147191Sjkoshy		}
2514147191Sjkoshy		error = copyout(&modv, arg, sizeof(int));
2515145256Sjkoshy	}
2516145256Sjkoshy	break;
2517145256Sjkoshy
2518145256Sjkoshy
2519145256Sjkoshy	/*
2520145256Sjkoshy	 * Retrieve the state of all the PMCs on a given
2521145256Sjkoshy	 * CPU.
2522145256Sjkoshy	 */
2523145256Sjkoshy
2524145256Sjkoshy	case PMC_OP_GETPMCINFO:
2525145256Sjkoshy	{
2526145256Sjkoshy		uint32_t cpu, n, npmc;
2527145256Sjkoshy		size_t pmcinfo_size;
2528145256Sjkoshy		struct pmc *pm;
2529145256Sjkoshy		struct pmc_info *p, *pmcinfo;
2530145256Sjkoshy		struct pmc_op_getpmcinfo *gpi;
2531145256Sjkoshy		struct pmc_owner *po;
2532145256Sjkoshy		struct pmc_binding pb;
2533145256Sjkoshy
2534145256Sjkoshy		PMC_DOWNGRADE_SX();
2535145256Sjkoshy
2536145256Sjkoshy		gpi = (struct pmc_op_getpmcinfo *) arg;
2537145256Sjkoshy
2538145256Sjkoshy		if ((error = copyin(&gpi->pm_cpu, &cpu, sizeof(cpu))) != 0)
2539145256Sjkoshy			break;
2540145256Sjkoshy
2541145256Sjkoshy		if (cpu >= (unsigned int) mp_ncpus) {
2542145256Sjkoshy			error = EINVAL;
2543145256Sjkoshy			break;
2544145256Sjkoshy		}
2545145256Sjkoshy
2546145256Sjkoshy		if (pmc_cpu_is_disabled(cpu)) {
2547145256Sjkoshy			error = ENXIO;
2548145256Sjkoshy			break;
2549145256Sjkoshy		}
2550145256Sjkoshy
2551145256Sjkoshy		/* switch to CPU 'cpu' */
2552145256Sjkoshy		pmc_save_cpu_binding(&pb);
2553145256Sjkoshy		pmc_select_cpu(cpu);
2554145256Sjkoshy
2555145256Sjkoshy		npmc = md->pmd_npmc;
2556145256Sjkoshy
2557145256Sjkoshy		pmcinfo_size = npmc * sizeof(struct pmc_info);
2558145256Sjkoshy		MALLOC(pmcinfo, struct pmc_info *, pmcinfo_size, M_PMC,
2559145256Sjkoshy		    M_WAITOK);
2560145256Sjkoshy
2561145256Sjkoshy		p = pmcinfo;
2562145256Sjkoshy
2563145256Sjkoshy		for (n = 0; n < md->pmd_npmc; n++, p++) {
2564145256Sjkoshy
2565145256Sjkoshy			if ((error = md->pmd_describe(cpu, n, p, &pm)) != 0)
2566145256Sjkoshy				break;
2567145256Sjkoshy
2568145256Sjkoshy			if (PMC_ROW_DISP_IS_STANDALONE(n))
2569145256Sjkoshy				p->pm_rowdisp = PMC_DISP_STANDALONE;
2570145256Sjkoshy			else if (PMC_ROW_DISP_IS_THREAD(n))
2571145256Sjkoshy				p->pm_rowdisp = PMC_DISP_THREAD;
2572145256Sjkoshy			else
2573145256Sjkoshy				p->pm_rowdisp = PMC_DISP_FREE;
2574145256Sjkoshy
2575145256Sjkoshy			p->pm_ownerpid = -1;
2576145256Sjkoshy
2577145256Sjkoshy			if (pm == NULL)	/* no PMC associated */
2578145256Sjkoshy				continue;
2579145256Sjkoshy
2580145256Sjkoshy			po = pm->pm_owner;
2581145256Sjkoshy
2582145256Sjkoshy			KASSERT(po->po_owner != NULL,
2583145256Sjkoshy			    ("[pmc,%d] pmc_owner had a null proc pointer",
2584145256Sjkoshy				__LINE__));
2585145256Sjkoshy
2586145256Sjkoshy			p->pm_ownerpid = po->po_owner->p_pid;
2587145774Sjkoshy			p->pm_mode     = PMC_TO_MODE(pm);
2588145256Sjkoshy			p->pm_event    = pm->pm_event;
2589145256Sjkoshy			p->pm_flags    = pm->pm_flags;
2590145256Sjkoshy
2591145774Sjkoshy			if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
2592145256Sjkoshy				p->pm_reloadcount =
2593145256Sjkoshy				    pm->pm_sc.pm_reloadcount;
2594145256Sjkoshy		}
2595145256Sjkoshy
2596145256Sjkoshy		pmc_restore_cpu_binding(&pb);
2597145256Sjkoshy
2598145256Sjkoshy		/* now copy out the PMC info collected */
2599145256Sjkoshy		if (error == 0)
2600145256Sjkoshy			error = copyout(pmcinfo, &gpi->pm_pmcs, pmcinfo_size);
2601145256Sjkoshy
2602145256Sjkoshy		FREE(pmcinfo, M_PMC);
2603145256Sjkoshy	}
2604145256Sjkoshy	break;
2605145256Sjkoshy
2606145256Sjkoshy
2607145256Sjkoshy	/*
2608145256Sjkoshy	 * Set the administrative state of a PMC.  I.e. whether
2609145256Sjkoshy	 * the PMC is to be used or not.
2610145256Sjkoshy	 */
2611145256Sjkoshy
2612145256Sjkoshy	case PMC_OP_PMCADMIN:
2613145256Sjkoshy	{
2614145256Sjkoshy		int cpu, ri;
2615145256Sjkoshy		enum pmc_state request;
2616145256Sjkoshy		struct pmc_cpu *pc;
2617145256Sjkoshy		struct pmc_hw *phw;
2618145256Sjkoshy		struct pmc_op_pmcadmin pma;
2619145256Sjkoshy		struct pmc_binding pb;
2620145256Sjkoshy
2621145256Sjkoshy		sx_assert(&pmc_sx, SX_XLOCKED);
2622145256Sjkoshy
2623145256Sjkoshy		KASSERT(td == curthread,
2624145256Sjkoshy		    ("[pmc,%d] td != curthread", __LINE__));
2625145256Sjkoshy
2626145256Sjkoshy		if (suser(td) || jailed(td->td_ucred)) {
2627145256Sjkoshy			error =  EPERM;
2628145256Sjkoshy			break;
2629145256Sjkoshy		}
2630145256Sjkoshy
2631145256Sjkoshy		if ((error = copyin(arg, &pma, sizeof(pma))) != 0)
2632145256Sjkoshy			break;
2633145256Sjkoshy
2634145256Sjkoshy		cpu = pma.pm_cpu;
2635145256Sjkoshy
2636145256Sjkoshy		if (cpu < 0 || cpu >= mp_ncpus) {
2637145256Sjkoshy			error = EINVAL;
2638145256Sjkoshy			break;
2639145256Sjkoshy		}
2640145256Sjkoshy
2641145256Sjkoshy		if (pmc_cpu_is_disabled(cpu)) {
2642145256Sjkoshy			error = ENXIO;
2643145256Sjkoshy			break;
2644145256Sjkoshy		}
2645145256Sjkoshy
2646145256Sjkoshy		request = pma.pm_state;
2647145256Sjkoshy
2648145256Sjkoshy		if (request != PMC_STATE_DISABLED &&
2649145256Sjkoshy		    request != PMC_STATE_FREE) {
2650145256Sjkoshy			error = EINVAL;
2651145256Sjkoshy			break;
2652145256Sjkoshy		}
2653145256Sjkoshy
2654145256Sjkoshy		ri = pma.pm_pmc; /* pmc id == row index */
2655145256Sjkoshy		if (ri < 0 || ri >= (int) md->pmd_npmc) {
2656145256Sjkoshy			error = EINVAL;
2657145256Sjkoshy			break;
2658145256Sjkoshy		}
2659145256Sjkoshy
2660145256Sjkoshy		/*
2661145256Sjkoshy		 * We can't disable a PMC with a row-index allocated
2662145256Sjkoshy		 * for process virtual PMCs.
2663145256Sjkoshy		 */
2664145256Sjkoshy
2665145256Sjkoshy		if (PMC_ROW_DISP_IS_THREAD(ri) &&
2666145256Sjkoshy		    request == PMC_STATE_DISABLED) {
2667145256Sjkoshy			error = EBUSY;
2668145256Sjkoshy			break;
2669145256Sjkoshy		}
2670145256Sjkoshy
2671145256Sjkoshy		/*
2672145256Sjkoshy		 * otherwise, this PMC on this CPU is either free or
2673145256Sjkoshy		 * in system-wide mode.
2674145256Sjkoshy		 */
2675145256Sjkoshy
2676145256Sjkoshy		pmc_save_cpu_binding(&pb);
2677145256Sjkoshy		pmc_select_cpu(cpu);
2678145256Sjkoshy
2679145256Sjkoshy		pc  = pmc_pcpu[cpu];
2680145256Sjkoshy		phw = pc->pc_hwpmcs[ri];
2681145256Sjkoshy
2682145256Sjkoshy		/*
2683145256Sjkoshy		 * XXX do we need some kind of 'forced' disable?
2684145256Sjkoshy		 */
2685145256Sjkoshy
2686145256Sjkoshy		if (phw->phw_pmc == NULL) {
2687145256Sjkoshy			if (request == PMC_STATE_DISABLED &&
2688145256Sjkoshy			    (phw->phw_state & PMC_PHW_FLAG_IS_ENABLED)) {
2689145256Sjkoshy				phw->phw_state &= ~PMC_PHW_FLAG_IS_ENABLED;
2690145256Sjkoshy				PMC_MARK_ROW_STANDALONE(ri);
2691145256Sjkoshy			} else if (request == PMC_STATE_FREE &&
2692145256Sjkoshy			    (phw->phw_state & PMC_PHW_FLAG_IS_ENABLED) == 0) {
2693145256Sjkoshy				phw->phw_state |=  PMC_PHW_FLAG_IS_ENABLED;
2694145256Sjkoshy				PMC_UNMARK_ROW_STANDALONE(ri);
2695145256Sjkoshy			}
2696145256Sjkoshy			/* other cases are a no-op */
2697145256Sjkoshy		} else
2698145256Sjkoshy			error = EBUSY;
2699145256Sjkoshy
2700145256Sjkoshy		pmc_restore_cpu_binding(&pb);
2701145256Sjkoshy	}
2702145256Sjkoshy	break;
2703145256Sjkoshy
2704145256Sjkoshy
2705145256Sjkoshy	/*
2706145256Sjkoshy	 * Allocate a PMC.
2707145256Sjkoshy	 */
2708145256Sjkoshy
2709145256Sjkoshy	case PMC_OP_PMCALLOCATE:
2710145256Sjkoshy	{
2711145256Sjkoshy		uint32_t caps;
2712145256Sjkoshy		u_int cpu;
2713145256Sjkoshy		int n;
2714145256Sjkoshy		enum pmc_mode mode;
2715145256Sjkoshy		struct pmc *pmc;
2716145774Sjkoshy		struct pmc_hw *phw;
2717145256Sjkoshy		struct pmc_op_pmcallocate pa;
2718145256Sjkoshy		struct pmc_binding pb;
2719145256Sjkoshy
2720145256Sjkoshy		if ((error = copyin(arg, &pa, sizeof(pa))) != 0)
2721145256Sjkoshy			break;
2722145256Sjkoshy
2723145256Sjkoshy		caps = pa.pm_caps;
2724145256Sjkoshy		mode = pa.pm_mode;
2725145256Sjkoshy		cpu  = pa.pm_cpu;
2726145256Sjkoshy
2727145256Sjkoshy		if ((mode != PMC_MODE_SS  &&  mode != PMC_MODE_SC  &&
2728145256Sjkoshy		     mode != PMC_MODE_TS  &&  mode != PMC_MODE_TC) ||
2729145256Sjkoshy		    (cpu != (u_int) PMC_CPU_ANY && cpu >= (u_int) mp_ncpus)) {
2730145256Sjkoshy			error = EINVAL;
2731145256Sjkoshy			break;
2732145256Sjkoshy		}
2733145256Sjkoshy
2734145256Sjkoshy		/*
2735145256Sjkoshy		 * Virtual PMCs should only ask for a default CPU.
2736145256Sjkoshy		 * System mode PMCs need to specify a non-default CPU.
2737145256Sjkoshy		 */
2738145256Sjkoshy
2739145256Sjkoshy		if ((PMC_IS_VIRTUAL_MODE(mode) && cpu != (u_int) PMC_CPU_ANY) ||
2740145256Sjkoshy		    (PMC_IS_SYSTEM_MODE(mode) && cpu == (u_int) PMC_CPU_ANY)) {
2741145256Sjkoshy			error = EINVAL;
2742145256Sjkoshy			break;
2743145256Sjkoshy		}
2744145256Sjkoshy
2745145256Sjkoshy		/*
2746145256Sjkoshy		 * Check that a disabled CPU is not being asked for.
2747145256Sjkoshy		 */
2748145256Sjkoshy
2749145256Sjkoshy		if (PMC_IS_SYSTEM_MODE(mode) && pmc_cpu_is_disabled(cpu)) {
2750145256Sjkoshy			error = ENXIO;
2751145256Sjkoshy			break;
2752145256Sjkoshy		}
2753145256Sjkoshy
2754145256Sjkoshy		/*
2755145256Sjkoshy		 * Refuse an allocation for a system-wide PMC if this
2756145256Sjkoshy		 * process has been jailed, or if this process lacks
2757145256Sjkoshy		 * super-user credentials and the sysctl tunable
2758145256Sjkoshy		 * 'security.bsd.unprivileged_syspmcs' is zero.
2759145256Sjkoshy		 */
2760145256Sjkoshy
2761145256Sjkoshy		if (PMC_IS_SYSTEM_MODE(mode)) {
2762145256Sjkoshy			if (jailed(curthread->td_ucred))
2763145256Sjkoshy				error = EPERM;
2764145256Sjkoshy			else if (suser(curthread) &&
2765145256Sjkoshy			    (pmc_unprivileged_syspmcs == 0))
2766145256Sjkoshy				error = EPERM;
2767145256Sjkoshy		}
2768145256Sjkoshy
2769145256Sjkoshy		if (error)
2770145256Sjkoshy			break;
2771145256Sjkoshy
2772145256Sjkoshy		/*
2773145256Sjkoshy		 * Look for valid values for 'pm_flags'
2774145256Sjkoshy		 */
2775145256Sjkoshy
2776147191Sjkoshy		if ((pa.pm_flags & ~(PMC_F_DESCENDANTS | PMC_F_LOG_PROCCSW |
2777147191Sjkoshy		    PMC_F_LOG_PROCEXIT)) != 0) {
2778145256Sjkoshy			error = EINVAL;
2779145256Sjkoshy			break;
2780145256Sjkoshy		}
2781145256Sjkoshy
2782147191Sjkoshy		/* process logging options are not allowed for system PMCs */
2783147191Sjkoshy		if (PMC_IS_SYSTEM_MODE(mode) && (pa.pm_flags &
2784147191Sjkoshy		    (PMC_F_LOG_PROCCSW | PMC_F_LOG_PROCEXIT))) {
2785147191Sjkoshy			error = EINVAL;
2786147191Sjkoshy			break;
2787147191Sjkoshy		}
2788147191Sjkoshy
2789145256Sjkoshy		/*
2790145256Sjkoshy		 * All sampling mode PMCs need to be able to interrupt the
2791145256Sjkoshy		 * CPU.
2792145256Sjkoshy		 */
2793145256Sjkoshy
2794147191Sjkoshy		if (PMC_IS_SAMPLING_MODE(mode))
2795145256Sjkoshy			caps |= PMC_CAP_INTERRUPT;
2796145256Sjkoshy
2797145256Sjkoshy		PMCDBG(PMC,ALL,2, "event=%d caps=0x%x mode=%d cpu=%d",
2798145256Sjkoshy		    pa.pm_ev, caps, mode, cpu);
2799145256Sjkoshy
2800145256Sjkoshy		pmc = pmc_allocate_pmc_descriptor();
2801145774Sjkoshy		pmc->pm_id    = PMC_ID_MAKE_ID(cpu,pa.pm_mode,pa.pm_class,
2802145774Sjkoshy		    PMC_ID_INVALID);
2803145256Sjkoshy		pmc->pm_event = pa.pm_ev;
2804145256Sjkoshy		pmc->pm_state = PMC_STATE_FREE;
2805145256Sjkoshy		pmc->pm_caps  = caps;
2806145256Sjkoshy		pmc->pm_flags = pa.pm_flags;
2807145256Sjkoshy
2808145256Sjkoshy		/* switch thread to CPU 'cpu' */
2809145256Sjkoshy		pmc_save_cpu_binding(&pb);
2810145256Sjkoshy
2811145256Sjkoshy#define	PMC_IS_SHAREABLE_PMC(cpu, n)				\
2812145256Sjkoshy	(pmc_pcpu[(cpu)]->pc_hwpmcs[(n)]->phw_state &		\
2813145256Sjkoshy	 PMC_PHW_FLAG_IS_SHAREABLE)
2814145256Sjkoshy#define	PMC_IS_UNALLOCATED(cpu, n)				\
2815145256Sjkoshy	(pmc_pcpu[(cpu)]->pc_hwpmcs[(n)]->phw_pmc == NULL)
2816145256Sjkoshy
2817145256Sjkoshy		if (PMC_IS_SYSTEM_MODE(mode)) {
2818145256Sjkoshy			pmc_select_cpu(cpu);
2819145256Sjkoshy			for (n = 0; n < (int) md->pmd_npmc; n++)
2820145256Sjkoshy				if (pmc_can_allocate_row(n, mode) == 0 &&
2821145256Sjkoshy				    pmc_can_allocate_rowindex(
2822145774Sjkoshy					    curthread->td_proc, n, cpu) == 0 &&
2823145256Sjkoshy				    (PMC_IS_UNALLOCATED(cpu, n) ||
2824145256Sjkoshy				     PMC_IS_SHAREABLE_PMC(cpu, n)) &&
2825145256Sjkoshy				    md->pmd_allocate_pmc(cpu, n, pmc,
2826145256Sjkoshy					&pa) == 0)
2827145256Sjkoshy					break;
2828145256Sjkoshy		} else {
2829145256Sjkoshy			/* Process virtual mode */
2830145256Sjkoshy			for (n = 0; n < (int) md->pmd_npmc; n++) {
2831145256Sjkoshy				if (pmc_can_allocate_row(n, mode) == 0 &&
2832145256Sjkoshy				    pmc_can_allocate_rowindex(
2833145774Sjkoshy					    curthread->td_proc, n,
2834145774Sjkoshy					    PMC_CPU_ANY) == 0 &&
2835145256Sjkoshy				    md->pmd_allocate_pmc(curthread->td_oncpu,
2836145256Sjkoshy					n, pmc, &pa) == 0)
2837145256Sjkoshy					break;
2838145256Sjkoshy			}
2839145256Sjkoshy		}
2840145256Sjkoshy
2841145256Sjkoshy#undef	PMC_IS_UNALLOCATED
2842145256Sjkoshy#undef	PMC_IS_SHAREABLE_PMC
2843145256Sjkoshy
2844145256Sjkoshy		pmc_restore_cpu_binding(&pb);
2845145256Sjkoshy
2846145256Sjkoshy		if (n == (int) md->pmd_npmc) {
2847145256Sjkoshy			pmc_destroy_pmc_descriptor(pmc);
2848145256Sjkoshy			FREE(pmc, M_PMC);
2849145256Sjkoshy			pmc = NULL;
2850145256Sjkoshy			error = EINVAL;
2851145256Sjkoshy			break;
2852145256Sjkoshy		}
2853145256Sjkoshy
2854145774Sjkoshy		/* Fill in the correct value in the ID field */
2855145774Sjkoshy		pmc->pm_id = PMC_ID_MAKE_ID(cpu,mode,pa.pm_class,n);
2856145256Sjkoshy
2857145774Sjkoshy		PMCDBG(PMC,ALL,2, "ev=%d class=%d mode=%d n=%d -> pmcid=%x",
2858145774Sjkoshy		    pmc->pm_event, pa.pm_class, mode, n, pmc->pm_id);
2859145774Sjkoshy
2860147191Sjkoshy		/* Process mode PMCs with logging enabled need log files */
2861147191Sjkoshy		if (pmc->pm_flags & (PMC_F_LOG_PROCEXIT | PMC_F_LOG_PROCCSW))
2862147191Sjkoshy			pmc->pm_flags |= PMC_F_NEEDS_LOGFILE;
2863147191Sjkoshy
2864147191Sjkoshy		/* All system mode sampling PMCs require a log file */
2865147191Sjkoshy		if (PMC_IS_SAMPLING_MODE(mode) && PMC_IS_SYSTEM_MODE(mode))
2866147191Sjkoshy			pmc->pm_flags |= PMC_F_NEEDS_LOGFILE;
2867147191Sjkoshy
2868145256Sjkoshy		/*
2869145256Sjkoshy		 * Configure global pmc's immediately
2870145256Sjkoshy		 */
2871145256Sjkoshy
2872145774Sjkoshy		if (PMC_IS_SYSTEM_MODE(PMC_TO_MODE(pmc))) {
2873145774Sjkoshy
2874145774Sjkoshy			pmc_save_cpu_binding(&pb);
2875145774Sjkoshy			pmc_select_cpu(cpu);
2876145774Sjkoshy
2877145774Sjkoshy			phw = pmc_pcpu[cpu]->pc_hwpmcs[n];
2878145774Sjkoshy
2879145774Sjkoshy			if ((phw->phw_state & PMC_PHW_FLAG_IS_ENABLED) == 0 ||
2880145774Sjkoshy			    (error = md->pmd_config_pmc(cpu, n, pmc)) != 0) {
2881145256Sjkoshy				(void) md->pmd_release_pmc(cpu, n, pmc);
2882145256Sjkoshy				pmc_destroy_pmc_descriptor(pmc);
2883145256Sjkoshy				FREE(pmc, M_PMC);
2884145256Sjkoshy				pmc = NULL;
2885145774Sjkoshy				pmc_restore_cpu_binding(&pb);
2886145774Sjkoshy				error = EPERM;
2887145256Sjkoshy				break;
2888145256Sjkoshy			}
2889145256Sjkoshy
2890145774Sjkoshy			pmc_restore_cpu_binding(&pb);
2891145774Sjkoshy		}
2892145256Sjkoshy
2893145256Sjkoshy		pmc->pm_state    = PMC_STATE_ALLOCATED;
2894145256Sjkoshy
2895145256Sjkoshy		/*
2896145256Sjkoshy		 * mark row disposition
2897145256Sjkoshy		 */
2898145256Sjkoshy
2899145256Sjkoshy		if (PMC_IS_SYSTEM_MODE(mode))
2900145256Sjkoshy			PMC_MARK_ROW_STANDALONE(n);
2901145256Sjkoshy		else
2902145256Sjkoshy			PMC_MARK_ROW_THREAD(n);
2903145256Sjkoshy
2904145256Sjkoshy		/*
2905145256Sjkoshy		 * Register this PMC with the current thread as its owner.
2906145256Sjkoshy		 */
2907145256Sjkoshy
2908145256Sjkoshy		if ((error =
2909145256Sjkoshy		    pmc_register_owner(curthread->td_proc, pmc)) != 0) {
2910145256Sjkoshy			pmc_release_pmc_descriptor(pmc);
2911145256Sjkoshy			FREE(pmc, M_PMC);
2912145256Sjkoshy			pmc = NULL;
2913145256Sjkoshy			break;
2914145256Sjkoshy		}
2915145256Sjkoshy
2916145256Sjkoshy		/*
2917145256Sjkoshy		 * Return the allocated index.
2918145256Sjkoshy		 */
2919145256Sjkoshy
2920145774Sjkoshy		pa.pm_pmcid = pmc->pm_id;
2921145256Sjkoshy
2922145256Sjkoshy		error = copyout(&pa, arg, sizeof(pa));
2923145256Sjkoshy	}
2924145256Sjkoshy	break;
2925145256Sjkoshy
2926145256Sjkoshy
2927145256Sjkoshy	/*
2928145256Sjkoshy	 * Attach a PMC to a process.
2929145256Sjkoshy	 */
2930145256Sjkoshy
2931145256Sjkoshy	case PMC_OP_PMCATTACH:
2932145256Sjkoshy	{
2933145256Sjkoshy		struct pmc *pm;
2934145256Sjkoshy		struct proc *p;
2935145256Sjkoshy		struct pmc_op_pmcattach a;
2936145256Sjkoshy
2937145256Sjkoshy		sx_assert(&pmc_sx, SX_XLOCKED);
2938145256Sjkoshy
2939145256Sjkoshy		if ((error = copyin(arg, &a, sizeof(a))) != 0)
2940145256Sjkoshy			break;
2941145256Sjkoshy
2942145256Sjkoshy		if (a.pm_pid < 0) {
2943145256Sjkoshy			error = EINVAL;
2944145256Sjkoshy			break;
2945145256Sjkoshy		} else if (a.pm_pid == 0)
2946145256Sjkoshy			a.pm_pid = td->td_proc->p_pid;
2947145256Sjkoshy
2948145256Sjkoshy		if ((error = pmc_find_pmc(a.pm_pmc, &pm)) != 0)
2949145256Sjkoshy			break;
2950145256Sjkoshy
2951145774Sjkoshy		if (PMC_IS_SYSTEM_MODE(PMC_TO_MODE(pm))) {
2952145256Sjkoshy			error = EINVAL;
2953145256Sjkoshy			break;
2954145256Sjkoshy		}
2955145256Sjkoshy
2956145256Sjkoshy		/* PMCs may be (re)attached only when allocated or stopped */
2957145256Sjkoshy		if (pm->pm_state == PMC_STATE_RUNNING) {
2958145256Sjkoshy			error = EBUSY;
2959145256Sjkoshy			break;
2960145256Sjkoshy		} else if (pm->pm_state != PMC_STATE_ALLOCATED &&
2961145256Sjkoshy		    pm->pm_state != PMC_STATE_STOPPED) {
2962145256Sjkoshy			error = EINVAL;
2963145256Sjkoshy			break;
2964145256Sjkoshy		}
2965145256Sjkoshy
2966145256Sjkoshy		/* lookup pid */
2967145256Sjkoshy		if ((p = pfind(a.pm_pid)) == NULL) {
2968145256Sjkoshy			error = ESRCH;
2969145256Sjkoshy			break;
2970145256Sjkoshy		}
2971145256Sjkoshy
2972145256Sjkoshy		/*
2973145256Sjkoshy		 * Ignore processes that are working on exiting.
2974145256Sjkoshy		 */
2975145256Sjkoshy		if (p->p_flag & P_WEXIT) {
2976145256Sjkoshy			error = ESRCH;
2977145256Sjkoshy			PROC_UNLOCK(p);	/* pfind() returns a locked process */
2978145256Sjkoshy			break;
2979145256Sjkoshy		}
2980145256Sjkoshy
2981145256Sjkoshy		/*
2982145256Sjkoshy		 * we are allowed to attach a PMC to a process if
2983145256Sjkoshy		 * we can debug it.
2984145256Sjkoshy		 */
2985145256Sjkoshy		error = p_candebug(curthread, p);
2986145256Sjkoshy
2987145256Sjkoshy		PROC_UNLOCK(p);
2988145256Sjkoshy
2989145256Sjkoshy		if (error == 0)
2990145256Sjkoshy			error = pmc_attach_process(p, pm);
2991145256Sjkoshy	}
2992145256Sjkoshy	break;
2993145256Sjkoshy
2994145256Sjkoshy
2995145256Sjkoshy	/*
2996145256Sjkoshy	 * Detach an attached PMC from a process.
2997145256Sjkoshy	 */
2998145256Sjkoshy
2999145256Sjkoshy	case PMC_OP_PMCDETACH:
3000145256Sjkoshy	{
3001145256Sjkoshy		struct pmc *pm;
3002145256Sjkoshy		struct proc *p;
3003145256Sjkoshy		struct pmc_op_pmcattach a;
3004145256Sjkoshy
3005145256Sjkoshy		if ((error = copyin(arg, &a, sizeof(a))) != 0)
3006145256Sjkoshy			break;
3007145256Sjkoshy
3008145256Sjkoshy		if (a.pm_pid < 0) {
3009145256Sjkoshy			error = EINVAL;
3010145256Sjkoshy			break;
3011145256Sjkoshy		} else if (a.pm_pid == 0)
3012145256Sjkoshy			a.pm_pid = td->td_proc->p_pid;
3013145256Sjkoshy
3014145256Sjkoshy		if ((error = pmc_find_pmc(a.pm_pmc, &pm)) != 0)
3015145256Sjkoshy			break;
3016145256Sjkoshy
3017145256Sjkoshy		if ((p = pfind(a.pm_pid)) == NULL) {
3018145256Sjkoshy			error = ESRCH;
3019145256Sjkoshy			break;
3020145256Sjkoshy		}
3021145256Sjkoshy
3022145256Sjkoshy		/*
3023145256Sjkoshy		 * Treat processes that are in the process of exiting
3024145256Sjkoshy		 * as if they were not present.
3025145256Sjkoshy		 */
3026145256Sjkoshy
3027145256Sjkoshy		if (p->p_flag & P_WEXIT)
3028145256Sjkoshy			error = ESRCH;
3029145256Sjkoshy
3030145256Sjkoshy		PROC_UNLOCK(p);	/* pfind() returns a locked process */
3031145256Sjkoshy
3032145256Sjkoshy		if (error == 0)
3033145256Sjkoshy			error = pmc_detach_process(p, pm);
3034145256Sjkoshy	}
3035145256Sjkoshy	break;
3036145256Sjkoshy
3037145256Sjkoshy
3038145256Sjkoshy	/*
3039147191Sjkoshy	 * Retrieve the MSR number associated with the counter
3040147191Sjkoshy	 * 'pmc_id'.  This allows processes to directly use RDPMC
3041147191Sjkoshy	 * instructions to read their PMCs, without the overhead of a
3042147191Sjkoshy	 * system call.
3043147191Sjkoshy	 */
3044147191Sjkoshy
3045147191Sjkoshy	case PMC_OP_PMCGETMSR:
3046147191Sjkoshy	{
3047147191Sjkoshy		int ri;
3048147191Sjkoshy		struct pmc	*pm;
3049147191Sjkoshy		struct pmc_target *pt;
3050147191Sjkoshy		struct pmc_op_getmsr gm;
3051147191Sjkoshy
3052147191Sjkoshy		PMC_DOWNGRADE_SX();
3053147191Sjkoshy
3054147191Sjkoshy		/* CPU has no 'GETMSR' support */
3055147191Sjkoshy		if (md->pmd_get_msr == NULL) {
3056147191Sjkoshy			error = ENOSYS;
3057147191Sjkoshy			break;
3058147191Sjkoshy		}
3059147191Sjkoshy
3060147191Sjkoshy		if ((error = copyin(arg, &gm, sizeof(gm))) != 0)
3061147191Sjkoshy			break;
3062147191Sjkoshy
3063147191Sjkoshy		if ((error = pmc_find_pmc(gm.pm_pmcid, &pm)) != 0)
3064147191Sjkoshy			break;
3065147191Sjkoshy
3066147191Sjkoshy		/*
3067147191Sjkoshy		 * The allocated PMC has to be a process virtual PMC,
3068147191Sjkoshy		 * i.e., of type MODE_T[CS].  Global PMCs can only be
3069147191Sjkoshy		 * read using the PMCREAD operation since they may be
3070147191Sjkoshy		 * allocated on a different CPU than the one we could
3071147191Sjkoshy		 * be running on at the time of the RDPMC instruction.
3072147191Sjkoshy		 *
3073147191Sjkoshy		 * The GETMSR operation is not allowed for PMCs that
3074147191Sjkoshy		 * are inherited across processes.
3075147191Sjkoshy		 */
3076147191Sjkoshy
3077147191Sjkoshy		if (!PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm)) ||
3078147191Sjkoshy		    (pm->pm_flags & PMC_F_DESCENDANTS)) {
3079147191Sjkoshy			error = EINVAL;
3080147191Sjkoshy			break;
3081147191Sjkoshy		}
3082147191Sjkoshy
3083147191Sjkoshy		/*
3084147191Sjkoshy		 * It only makes sense to use a RDPMC (or its
3085147191Sjkoshy		 * equivalent instruction on non-x86 architectures) on
3086147191Sjkoshy		 * a process that has allocated and attached a PMC to
3087147191Sjkoshy		 * itself.  Conversely the PMC is only allowed to have
3088147191Sjkoshy		 * one process attached to it -- its owner.
3089147191Sjkoshy		 */
3090147191Sjkoshy
3091147191Sjkoshy		if ((pt = LIST_FIRST(&pm->pm_targets)) == NULL ||
3092147191Sjkoshy		    LIST_NEXT(pt, pt_next) != NULL ||
3093147191Sjkoshy		    pt->pt_process->pp_proc != pm->pm_owner->po_owner) {
3094147191Sjkoshy			error = EINVAL;
3095147191Sjkoshy			break;
3096147191Sjkoshy		}
3097147191Sjkoshy
3098147191Sjkoshy		ri = PMC_TO_ROWINDEX(pm);
3099147191Sjkoshy
3100147191Sjkoshy		if ((error = (*md->pmd_get_msr)(ri, &gm.pm_msr)) < 0)
3101147191Sjkoshy			break;
3102147191Sjkoshy
3103147191Sjkoshy		if ((error = copyout(&gm, arg, sizeof(gm))) < 0)
3104147191Sjkoshy			break;
3105147191Sjkoshy
3106147191Sjkoshy		/*
3107147191Sjkoshy		 * Mark our process as using MSRs.  Update machine
3108147191Sjkoshy		 * state using a forced context switch.
3109147191Sjkoshy		 */
3110147191Sjkoshy
3111147191Sjkoshy		pt->pt_process->pp_flags |= PMC_PP_ENABLE_MSR_ACCESS;
3112147191Sjkoshy		pmc_force_context_switch();
3113147191Sjkoshy
3114147191Sjkoshy	}
3115147191Sjkoshy	break;
3116147191Sjkoshy
3117147191Sjkoshy	/*
3118145256Sjkoshy	 * Release an allocated PMC
3119145256Sjkoshy	 */
3120145256Sjkoshy
3121145256Sjkoshy	case PMC_OP_PMCRELEASE:
3122145256Sjkoshy	{
3123145256Sjkoshy		pmc_id_t pmcid;
3124145256Sjkoshy		struct pmc *pm;
3125145256Sjkoshy		struct pmc_owner *po;
3126145256Sjkoshy		struct pmc_op_simple sp;
3127145256Sjkoshy
3128145256Sjkoshy		/*
3129145256Sjkoshy		 * Find PMC pointer for the named PMC.
3130145256Sjkoshy		 *
3131145256Sjkoshy		 * Use pmc_release_pmc_descriptor() to switch off the
3132145256Sjkoshy		 * PMC, remove all its target threads, and remove the
3133145256Sjkoshy		 * PMC from its owner's list.
3134145256Sjkoshy		 *
3135145256Sjkoshy		 * Remove the owner record if this is the last PMC
3136145256Sjkoshy		 * owned.
3137145256Sjkoshy		 *
3138145256Sjkoshy		 * Free up space.
3139145256Sjkoshy		 */
3140145256Sjkoshy
3141145256Sjkoshy		if ((error = copyin(arg, &sp, sizeof(sp))) != 0)
3142145256Sjkoshy			break;
3143145256Sjkoshy
3144145256Sjkoshy		pmcid = sp.pm_pmcid;
3145145256Sjkoshy
3146145256Sjkoshy		if ((error = pmc_find_pmc(pmcid, &pm)) != 0)
3147145256Sjkoshy			break;
3148145256Sjkoshy
3149145256Sjkoshy		po = pm->pm_owner;
3150145256Sjkoshy		pmc_release_pmc_descriptor(pm);
3151145256Sjkoshy		pmc_maybe_remove_owner(po);
3152145256Sjkoshy
3153145256Sjkoshy		FREE(pm, M_PMC);
3154145256Sjkoshy	}
3155145256Sjkoshy	break;
3156145256Sjkoshy
3157145256Sjkoshy
3158145256Sjkoshy	/*
3159145256Sjkoshy	 * Read and/or write a PMC.
3160145256Sjkoshy	 */
3161145256Sjkoshy
3162145256Sjkoshy	case PMC_OP_PMCRW:
3163145256Sjkoshy	{
3164145256Sjkoshy		uint32_t cpu, ri;
3165145256Sjkoshy		struct pmc *pm;
3166145256Sjkoshy		struct pmc_op_pmcrw *pprw;
3167145256Sjkoshy		struct pmc_op_pmcrw prw;
3168145256Sjkoshy		struct pmc_binding pb;
3169145256Sjkoshy		pmc_value_t oldvalue;
3170145256Sjkoshy
3171145256Sjkoshy		PMC_DOWNGRADE_SX();
3172145256Sjkoshy
3173145256Sjkoshy		if ((error = copyin(arg, &prw, sizeof(prw))) != 0)
3174145256Sjkoshy			break;
3175145256Sjkoshy
3176145301Simp		ri = 0;
3177145256Sjkoshy		PMCDBG(PMC,OPS,1, "rw id=%d flags=0x%x", prw.pm_pmcid,
3178145256Sjkoshy		    prw.pm_flags);
3179145256Sjkoshy
3180145256Sjkoshy		/* must have at least one flag set */
3181145256Sjkoshy		if ((prw.pm_flags & (PMC_F_OLDVALUE|PMC_F_NEWVALUE)) == 0) {
3182145256Sjkoshy			error = EINVAL;
3183145256Sjkoshy			break;
3184145256Sjkoshy		}
3185145256Sjkoshy
3186145256Sjkoshy		/* locate pmc descriptor */
3187145256Sjkoshy		if ((error = pmc_find_pmc(prw.pm_pmcid, &pm)) != 0)
3188145256Sjkoshy			break;
3189145256Sjkoshy
3190145256Sjkoshy		/* Can't read a PMC that hasn't been started. */
3191145256Sjkoshy		if (pm->pm_state != PMC_STATE_ALLOCATED &&
3192145256Sjkoshy		    pm->pm_state != PMC_STATE_STOPPED &&
3193145256Sjkoshy		    pm->pm_state != PMC_STATE_RUNNING) {
3194145256Sjkoshy			error = EINVAL;
3195145256Sjkoshy			break;
3196145256Sjkoshy		}
3197145256Sjkoshy
3198145256Sjkoshy		/* writing a new value is allowed only for 'STOPPED' pmcs */
3199145256Sjkoshy		if (pm->pm_state == PMC_STATE_RUNNING &&
3200145256Sjkoshy		    (prw.pm_flags & PMC_F_NEWVALUE)) {
3201145256Sjkoshy			error = EBUSY;
3202145256Sjkoshy			break;
3203145256Sjkoshy		}
3204145256Sjkoshy
3205145774Sjkoshy		if (PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm))) {
3206145256Sjkoshy
3207145774Sjkoshy			/*
3208145774Sjkoshy			 * If this PMC is attached to its owner (i.e.,
3209145774Sjkoshy			 * the process requesting this operation) and
3210145774Sjkoshy			 * is running, then attempt to get an
3211145774Sjkoshy			 * upto-date reading from hardware for a READ.
3212145774Sjkoshy			 * Writes are only allowed when the PMC is
3213145774Sjkoshy			 * stopped, so only update the saved value
3214145774Sjkoshy			 * field.
3215145774Sjkoshy			 *
3216145774Sjkoshy			 * If the PMC is not running, or is not
3217145774Sjkoshy			 * attached to its owner, read/write to the
3218145774Sjkoshy			 * savedvalue field.
3219145774Sjkoshy			 */
3220145774Sjkoshy
3221145774Sjkoshy			ri = PMC_TO_ROWINDEX(pm);
3222145774Sjkoshy
3223145256Sjkoshy			mtx_pool_lock_spin(pmc_mtxpool, pm);
3224145774Sjkoshy			cpu = curthread->td_oncpu;
3225145774Sjkoshy
3226145774Sjkoshy			if (prw.pm_flags & PMC_F_OLDVALUE) {
3227145774Sjkoshy				if ((pm->pm_flags & PMC_F_ATTACHED_TO_OWNER) &&
3228145774Sjkoshy				    (pm->pm_state == PMC_STATE_RUNNING))
3229145774Sjkoshy					error = (*md->pmd_read_pmc)(cpu, ri,
3230145774Sjkoshy					    &oldvalue);
3231145774Sjkoshy				else
3232145774Sjkoshy					oldvalue = pm->pm_gv.pm_savedvalue;
3233145774Sjkoshy			}
3234145256Sjkoshy			if (prw.pm_flags & PMC_F_NEWVALUE)
3235145256Sjkoshy				pm->pm_gv.pm_savedvalue = prw.pm_value;
3236145774Sjkoshy
3237145256Sjkoshy			mtx_pool_unlock_spin(pmc_mtxpool, pm);
3238145256Sjkoshy
3239145256Sjkoshy		} else { /* System mode PMCs */
3240145774Sjkoshy			cpu = PMC_TO_CPU(pm);
3241145774Sjkoshy			ri  = PMC_TO_ROWINDEX(pm);
3242145256Sjkoshy
3243145256Sjkoshy			if (pmc_cpu_is_disabled(cpu)) {
3244145256Sjkoshy				error = ENXIO;
3245145256Sjkoshy				break;
3246145256Sjkoshy			}
3247145256Sjkoshy
3248145256Sjkoshy			/* move this thread to CPU 'cpu' */
3249145256Sjkoshy			pmc_save_cpu_binding(&pb);
3250145256Sjkoshy			pmc_select_cpu(cpu);
3251145256Sjkoshy
3252145774Sjkoshy			critical_enter();
3253145256Sjkoshy			/* save old value */
3254145256Sjkoshy			if (prw.pm_flags & PMC_F_OLDVALUE)
3255145256Sjkoshy				if ((error = (*md->pmd_read_pmc)(cpu, ri,
3256145256Sjkoshy					 &oldvalue)))
3257145256Sjkoshy					goto error;
3258145256Sjkoshy			/* write out new value */
3259145256Sjkoshy			if (prw.pm_flags & PMC_F_NEWVALUE)
3260145256Sjkoshy				error = (*md->pmd_write_pmc)(cpu, ri,
3261145256Sjkoshy				    prw.pm_value);
3262145256Sjkoshy		error:
3263145774Sjkoshy			critical_exit();
3264145256Sjkoshy			pmc_restore_cpu_binding(&pb);
3265145256Sjkoshy			if (error)
3266145256Sjkoshy				break;
3267145256Sjkoshy		}
3268145256Sjkoshy
3269145256Sjkoshy		pprw = (struct pmc_op_pmcrw *) arg;
3270145256Sjkoshy
3271145256Sjkoshy#if	DEBUG
3272145256Sjkoshy		if (prw.pm_flags & PMC_F_NEWVALUE)
3273145256Sjkoshy			PMCDBG(PMC,OPS,2, "rw id=%d new %jx -> old %jx",
3274145256Sjkoshy			    ri, prw.pm_value, oldvalue);
3275145256Sjkoshy		else
3276145256Sjkoshy			PMCDBG(PMC,OPS,2, "rw id=%d -> old %jx", ri, oldvalue);
3277145256Sjkoshy#endif
3278145256Sjkoshy
3279145256Sjkoshy		/* return old value if requested */
3280145256Sjkoshy		if (prw.pm_flags & PMC_F_OLDVALUE)
3281145256Sjkoshy			if ((error = copyout(&oldvalue, &pprw->pm_value,
3282145256Sjkoshy				 sizeof(prw.pm_value))))
3283145256Sjkoshy				break;
3284145256Sjkoshy
3285145256Sjkoshy	}
3286145256Sjkoshy	break;
3287145256Sjkoshy
3288145256Sjkoshy
3289145256Sjkoshy	/*
3290145256Sjkoshy	 * Set the sampling rate for a sampling mode PMC and the
3291145256Sjkoshy	 * initial count for a counting mode PMC.
3292145256Sjkoshy	 */
3293145256Sjkoshy
3294145256Sjkoshy	case PMC_OP_PMCSETCOUNT:
3295145256Sjkoshy	{
3296145256Sjkoshy		struct pmc *pm;
3297145256Sjkoshy		struct pmc_op_pmcsetcount sc;
3298145256Sjkoshy
3299145256Sjkoshy		PMC_DOWNGRADE_SX();
3300145256Sjkoshy
3301145256Sjkoshy		if ((error = copyin(arg, &sc, sizeof(sc))) != 0)
3302145256Sjkoshy			break;
3303145256Sjkoshy
3304145256Sjkoshy		if ((error = pmc_find_pmc(sc.pm_pmcid, &pm)) != 0)
3305145256Sjkoshy			break;
3306145256Sjkoshy
3307145256Sjkoshy		if (pm->pm_state == PMC_STATE_RUNNING) {
3308145256Sjkoshy			error = EBUSY;
3309145256Sjkoshy			break;
3310145256Sjkoshy		}
3311145256Sjkoshy
3312145774Sjkoshy		if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
3313145256Sjkoshy			pm->pm_sc.pm_reloadcount = sc.pm_count;
3314145256Sjkoshy		else
3315145256Sjkoshy			pm->pm_sc.pm_initial = sc.pm_count;
3316145256Sjkoshy	}
3317145256Sjkoshy	break;
3318145256Sjkoshy
3319145256Sjkoshy
3320145256Sjkoshy	/*
3321145256Sjkoshy	 * Start a PMC.
3322145256Sjkoshy	 */
3323145256Sjkoshy
3324145256Sjkoshy	case PMC_OP_PMCSTART:
3325145256Sjkoshy	{
3326145256Sjkoshy		pmc_id_t pmcid;
3327145256Sjkoshy		struct pmc *pm;
3328145256Sjkoshy		struct pmc_op_simple sp;
3329145256Sjkoshy
3330145256Sjkoshy		sx_assert(&pmc_sx, SX_XLOCKED);
3331145256Sjkoshy
3332145256Sjkoshy		if ((error = copyin(arg, &sp, sizeof(sp))) != 0)
3333145256Sjkoshy			break;
3334145256Sjkoshy
3335145256Sjkoshy		pmcid = sp.pm_pmcid;
3336145256Sjkoshy
3337145256Sjkoshy		if ((error = pmc_find_pmc(pmcid, &pm)) != 0)
3338145256Sjkoshy			break;
3339145256Sjkoshy
3340145774Sjkoshy		KASSERT(pmcid == pm->pm_id,
3341145774Sjkoshy		    ("[pmc,%d] pmcid %x != id %x", __LINE__,
3342145774Sjkoshy			pm->pm_id, pmcid));
3343145256Sjkoshy
3344145256Sjkoshy		if (pm->pm_state == PMC_STATE_RUNNING) /* already running */
3345145256Sjkoshy			break;
3346145256Sjkoshy		else if (pm->pm_state != PMC_STATE_STOPPED &&
3347145256Sjkoshy		    pm->pm_state != PMC_STATE_ALLOCATED) {
3348145256Sjkoshy			error = EINVAL;
3349145256Sjkoshy			break;
3350145256Sjkoshy		}
3351145256Sjkoshy
3352145256Sjkoshy		error = pmc_start(pm);
3353145256Sjkoshy	}
3354145256Sjkoshy	break;
3355145256Sjkoshy
3356145256Sjkoshy
3357145256Sjkoshy	/*
3358145256Sjkoshy	 * Stop a PMC.
3359145256Sjkoshy	 */
3360145256Sjkoshy
3361145256Sjkoshy	case PMC_OP_PMCSTOP:
3362145256Sjkoshy	{
3363145256Sjkoshy		pmc_id_t pmcid;
3364145256Sjkoshy		struct pmc *pm;
3365145256Sjkoshy		struct pmc_op_simple sp;
3366145256Sjkoshy
3367145256Sjkoshy		PMC_DOWNGRADE_SX();
3368145256Sjkoshy
3369145256Sjkoshy		if ((error = copyin(arg, &sp, sizeof(sp))) != 0)
3370145256Sjkoshy			break;
3371145256Sjkoshy
3372145256Sjkoshy		pmcid = sp.pm_pmcid;
3373145256Sjkoshy
3374145256Sjkoshy		/*
3375145256Sjkoshy		 * Mark the PMC as inactive and invoke the MD stop
3376145256Sjkoshy		 * routines if needed.
3377145256Sjkoshy		 */
3378145256Sjkoshy
3379145256Sjkoshy		if ((error = pmc_find_pmc(pmcid, &pm)) != 0)
3380145256Sjkoshy			break;
3381145256Sjkoshy
3382145774Sjkoshy		KASSERT(pmcid == pm->pm_id,
3383145774Sjkoshy		    ("[pmc,%d] pmc id %x != pmcid %x", __LINE__,
3384145774Sjkoshy			pm->pm_id, pmcid));
3385145256Sjkoshy
3386145256Sjkoshy		if (pm->pm_state == PMC_STATE_STOPPED) /* already stopped */
3387145256Sjkoshy			break;
3388145256Sjkoshy		else if (pm->pm_state != PMC_STATE_RUNNING) {
3389145256Sjkoshy			error = EINVAL;
3390145256Sjkoshy			break;
3391145256Sjkoshy		}
3392145256Sjkoshy
3393145256Sjkoshy		error = pmc_stop(pm);
3394145256Sjkoshy	}
3395145256Sjkoshy	break;
3396145256Sjkoshy
3397145256Sjkoshy
3398145256Sjkoshy	/*
3399147867Sjkoshy	 * Write a user supplied value to the log file.
3400145256Sjkoshy	 */
3401145256Sjkoshy
3402145256Sjkoshy	case PMC_OP_WRITELOG:
3403145256Sjkoshy	{
3404147191Sjkoshy		struct pmc_op_writelog wl;
3405147191Sjkoshy		struct pmc_owner *po;
3406145256Sjkoshy
3407145256Sjkoshy		PMC_DOWNGRADE_SX();
3408145256Sjkoshy
3409147191Sjkoshy		if ((error = copyin(arg, &wl, sizeof(wl))) != 0)
3410145256Sjkoshy			break;
3411145256Sjkoshy
3412147191Sjkoshy		if ((po = pmc_find_owner_descriptor(td->td_proc)) == NULL) {
3413145256Sjkoshy			error = EINVAL;
3414145256Sjkoshy			break;
3415145256Sjkoshy		}
3416145256Sjkoshy
3417147191Sjkoshy		if ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0) {
3418145774Sjkoshy			error = EINVAL;
3419145774Sjkoshy			break;
3420145774Sjkoshy		}
3421145774Sjkoshy
3422147191Sjkoshy		error = pmclog_process_userlog(po, &wl);
3423145256Sjkoshy	}
3424145256Sjkoshy	break;
3425145256Sjkoshy
3426147191Sjkoshy
3427145256Sjkoshy	default:
3428145256Sjkoshy		error = EINVAL;
3429145256Sjkoshy		break;
3430145256Sjkoshy	}
3431145256Sjkoshy
3432145256Sjkoshy	if (is_sx_downgraded)
3433145256Sjkoshy		sx_sunlock(&pmc_sx);
3434145256Sjkoshy	else
3435145256Sjkoshy		sx_xunlock(&pmc_sx);
3436145256Sjkoshy
3437145256Sjkoshy	if (error)
3438145256Sjkoshy		atomic_add_int(&pmc_stats.pm_syscall_errors, 1);
3439145256Sjkoshy
3440147191Sjkoshy	PICKUP_GIANT();
3441147191Sjkoshy
3442145256Sjkoshy	return error;
3443145256Sjkoshy}
3444145256Sjkoshy
3445145256Sjkoshy/*
3446145256Sjkoshy * Helper functions
3447145256Sjkoshy */
3448145256Sjkoshy
3449147191Sjkoshy
3450145256Sjkoshy/*
3451147191Sjkoshy * Interrupt processing.
3452147191Sjkoshy *
3453147191Sjkoshy * Find a free slot in the per-cpu array of PC samples and write the
3454147191Sjkoshy * current (PMC,PID,PC) triple to it.  If an event was successfully
3455147191Sjkoshy * added, a bit is set in mask 'pmc_cpumask' denoting that the
3456147191Sjkoshy * DO_SAMPLES hook needs to be invoked from the clock handler.
3457147191Sjkoshy *
3458147191Sjkoshy * This function is meant to be called from an NMI handler.  It cannot
3459147191Sjkoshy * use any of the locking primitives supplied by the OS.
3460145256Sjkoshy */
3461145256Sjkoshy
3462147191Sjkoshyint
3463147708Sjkoshypmc_process_interrupt(int cpu, struct pmc *pm, uintfptr_t pc, int usermode)
3464145256Sjkoshy{
3465147191Sjkoshy	int error, ri;
3466147191Sjkoshy	struct thread *td;
3467147191Sjkoshy	struct pmc_sample *ps;
3468147191Sjkoshy	struct pmc_samplebuffer *psb;
3469145256Sjkoshy
3470147191Sjkoshy	error = 0;
3471147191Sjkoshy	ri = PMC_TO_ROWINDEX(pm);
3472145256Sjkoshy
3473147191Sjkoshy	psb = pmc_pcpu[cpu]->pc_sb;
3474145256Sjkoshy
3475147191Sjkoshy	ps = psb->ps_write;
3476147191Sjkoshy	if (ps->ps_pc) {	/* in use, reader hasn't caught up */
3477147867Sjkoshy		pm->pm_stalled = 1;
3478147191Sjkoshy		atomic_add_int(&pmc_stats.pm_intr_bufferfull, 1);
3479147191Sjkoshy		PMCDBG(SAM,INT,1,"(spc) cpu=%d pm=%p pc=%jx um=%d wr=%d rd=%d",
3480147708Sjkoshy		    cpu, pm, (uint64_t) pc, usermode,
3481147191Sjkoshy		    (int) (psb->ps_write - psb->ps_samples),
3482147191Sjkoshy		    (int) (psb->ps_read - psb->ps_samples));
3483147191Sjkoshy		error = ENOMEM;
3484147191Sjkoshy		goto done;
3485147191Sjkoshy	}
3486145256Sjkoshy
3487147191Sjkoshy	/* fill in entry */
3488147191Sjkoshy	PMCDBG(SAM,INT,1,"cpu=%d pm=%p pc=%jx um=%d wr=%d rd=%d", cpu, pm,
3489147708Sjkoshy	    (uint64_t) pc, usermode,
3490147191Sjkoshy	    (int) (psb->ps_write - psb->ps_samples),
3491147191Sjkoshy	    (int) (psb->ps_read - psb->ps_samples));
3492145256Sjkoshy
3493147191Sjkoshy	atomic_add_rel_32(&pm->pm_runcount, 1);		/* hold onto PMC */
3494147191Sjkoshy	ps->ps_pmc = pm;
3495147191Sjkoshy	if ((td = curthread) && td->td_proc)
3496147191Sjkoshy		ps->ps_pid = td->td_proc->p_pid;
3497147191Sjkoshy	else
3498147191Sjkoshy		ps->ps_pid = -1;
3499147191Sjkoshy	ps->ps_usermode = usermode;
3500147191Sjkoshy	ps->ps_pc = pc;		/* mark entry as in use */
3501145256Sjkoshy
3502147191Sjkoshy	/* increment write pointer, modulo ring buffer size */
3503147191Sjkoshy	ps++;
3504147191Sjkoshy	if (ps == psb->ps_fence)
3505147191Sjkoshy		psb->ps_write = psb->ps_samples;
3506147191Sjkoshy	else
3507147191Sjkoshy		psb->ps_write = ps;
3508145256Sjkoshy
3509147191Sjkoshy done:
3510147191Sjkoshy	/* mark CPU as needing processing */
3511147191Sjkoshy	atomic_set_rel_int(&pmc_cpumask, (1 << cpu));
3512147191Sjkoshy
3513147191Sjkoshy	return error;
3514145256Sjkoshy}
3515145256Sjkoshy
3516147191Sjkoshy
3517145256Sjkoshy/*
3518147191Sjkoshy * Process saved PC samples.
3519145256Sjkoshy */
3520145256Sjkoshy
3521145256Sjkoshystatic void
3522147191Sjkoshypmc_process_samples(int cpu)
3523145256Sjkoshy{
3524147191Sjkoshy	int n, ri;
3525147191Sjkoshy	struct pmc *pm;
3526147191Sjkoshy	struct thread *td;
3527147191Sjkoshy	struct pmc_owner *po;
3528147191Sjkoshy	struct pmc_sample *ps;
3529147191Sjkoshy	struct pmc_samplebuffer *psb;
3530145256Sjkoshy
3531147191Sjkoshy	KASSERT(PCPU_GET(cpuid) == cpu,
3532147191Sjkoshy	    ("[pmc,%d] not on the correct CPU pcpu=%d cpu=%d", __LINE__,
3533147191Sjkoshy		PCPU_GET(cpuid), cpu));
3534145256Sjkoshy
3535147191Sjkoshy	psb = pmc_pcpu[cpu]->pc_sb;
3536147191Sjkoshy
3537147191Sjkoshy	for (n = 0; n < pmc_nsamples; n++) { /* bound on #iterations */
3538147191Sjkoshy
3539147191Sjkoshy		ps = psb->ps_read;
3540147191Sjkoshy		if (ps->ps_pc == (uintfptr_t) 0)	/* no data */
3541147191Sjkoshy			break;
3542147191Sjkoshy
3543147191Sjkoshy		pm = ps->ps_pmc;
3544147191Sjkoshy		po = pm->pm_owner;
3545147191Sjkoshy
3546147191Sjkoshy		KASSERT(PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)),
3547147191Sjkoshy		    ("[pmc,%d] pmc=%p non-sampling mode=%d", __LINE__,
3548147191Sjkoshy			pm, PMC_TO_MODE(pm)));
3549147191Sjkoshy
3550147191Sjkoshy		/* Ignore PMCs that have been switched off */
3551147191Sjkoshy		if (pm->pm_state != PMC_STATE_RUNNING)
3552147191Sjkoshy			goto entrydone;
3553147191Sjkoshy
3554147191Sjkoshy		PMCDBG(SAM,OPS,1,"cpu=%d pm=%p pc=%jx um=%d wr=%d rd=%d", cpu,
3555147708Sjkoshy		    pm, (uint64_t) ps->ps_pc, ps->ps_usermode,
3556147191Sjkoshy		    (int) (psb->ps_write - psb->ps_samples),
3557147191Sjkoshy		    (int) (psb->ps_read - psb->ps_samples));
3558147191Sjkoshy
3559147191Sjkoshy		/*
3560147191Sjkoshy		 * If this is a process-mode PMC that is attached to
3561147191Sjkoshy		 * its owner, and if the PC is in user mode, update
3562147191Sjkoshy		 * profiling statistics like timer-based profiling
3563147191Sjkoshy		 * would have done.
3564147191Sjkoshy		 */
3565147191Sjkoshy		if (pm->pm_flags & PMC_F_ATTACHED_TO_OWNER) {
3566147191Sjkoshy			if (ps->ps_usermode) {
3567147191Sjkoshy				td = FIRST_THREAD_IN_PROC(po->po_owner);
3568147191Sjkoshy				addupc_intr(td, ps->ps_pc, 1);
3569147191Sjkoshy			}
3570147191Sjkoshy			goto entrydone;
3571147191Sjkoshy		}
3572147191Sjkoshy
3573147191Sjkoshy		/*
3574147191Sjkoshy		 * Otherwise, this is either a sampling mode PMC that
3575147191Sjkoshy		 * is attached to a different process than its owner,
3576147191Sjkoshy		 * or a system-wide sampling PMC.  Dispatch a log
3577147191Sjkoshy		 * entry to the PMC's owner process.
3578147191Sjkoshy		 */
3579147191Sjkoshy
3580147191Sjkoshy		pmclog_process_pcsample(pm, ps);
3581147191Sjkoshy
3582147191Sjkoshy	entrydone:
3583147191Sjkoshy		ps->ps_pc = (uintfptr_t) 0;	/* mark entry as free */
3584147191Sjkoshy		atomic_subtract_rel_32(&pm->pm_runcount, 1);
3585147191Sjkoshy
3586147191Sjkoshy		/* increment read pointer, modulo sample size */
3587147191Sjkoshy		if (++ps == psb->ps_fence)
3588147191Sjkoshy			psb->ps_read = psb->ps_samples;
3589147191Sjkoshy		else
3590147191Sjkoshy			psb->ps_read = ps;
3591147191Sjkoshy	}
3592147191Sjkoshy
3593147191Sjkoshy	atomic_add_int(&pmc_stats.pm_log_sweeps, 1);
3594147191Sjkoshy
3595147191Sjkoshy	/* Do not re-enable stalled PMCs if we failed to process any samples */
3596147191Sjkoshy	if (n == 0)
3597147191Sjkoshy		return;
3598147191Sjkoshy
3599147191Sjkoshy	/*
3600147191Sjkoshy	 * Restart any stalled sampling PMCs on this CPU.
3601147191Sjkoshy	 *
3602147867Sjkoshy	 * If the NMI handler sets the pm_stalled field of a PMC after
3603147867Sjkoshy	 * the check below, we'll end up processing the stalled PMC at
3604147867Sjkoshy	 * the next hardclock tick.
3605147191Sjkoshy	 */
3606147191Sjkoshy	for (n = 0; n < md->pmd_npmc; n++) {
3607147191Sjkoshy		(void) (*md->pmd_get_config)(cpu,n,&pm);
3608147191Sjkoshy		if (pm == NULL ||			 /* !cfg'ed */
3609147191Sjkoshy		    pm->pm_state != PMC_STATE_RUNNING || /* !active */
3610147191Sjkoshy		    !PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)) || /* !sampling */
3611147867Sjkoshy		    pm->pm_stalled == 0) /* !stalled */
3612147191Sjkoshy			continue;
3613147191Sjkoshy
3614147867Sjkoshy		pm->pm_stalled = 0;
3615147191Sjkoshy		ri = PMC_TO_ROWINDEX(pm);
3616147191Sjkoshy		(*md->pmd_start_pmc)(cpu, ri);
3617147191Sjkoshy	}
3618145256Sjkoshy}
3619145256Sjkoshy
3620145256Sjkoshy/*
3621145256Sjkoshy * Event handlers.
3622145256Sjkoshy */
3623145256Sjkoshy
3624145256Sjkoshy/*
3625145256Sjkoshy * Handle a process exit.
3626145256Sjkoshy *
3627147191Sjkoshy * Remove this process from all hash tables.  If this process
3628147191Sjkoshy * owned any PMCs, turn off those PMCs and deallocate them,
3629147191Sjkoshy * removing any associations with target processes.
3630147191Sjkoshy *
3631147191Sjkoshy * This function will be called by the last 'thread' of a
3632147191Sjkoshy * process.
3633147191Sjkoshy *
3634145256Sjkoshy * XXX This eventhandler gets called early in the exit process.
3635145256Sjkoshy * Consider using a 'hook' invocation from thread_exit() or equivalent
3636145256Sjkoshy * spot.  Another negative is that kse_exit doesn't seem to call
3637145256Sjkoshy * exit1() [??].
3638147191Sjkoshy *
3639145256Sjkoshy */
3640145256Sjkoshy
3641145256Sjkoshystatic void
3642145256Sjkoshypmc_process_exit(void *arg __unused, struct proc *p)
3643145256Sjkoshy{
3644145256Sjkoshy	int is_using_hwpmcs;
3645147191Sjkoshy	int cpu;
3646147191Sjkoshy	unsigned int ri;
3647147191Sjkoshy	struct pmc *pm;
3648147191Sjkoshy	struct pmc_process *pp;
3649147191Sjkoshy	struct pmc_owner *po;
3650147191Sjkoshy	pmc_value_t newvalue, tmp;
3651145256Sjkoshy
3652145256Sjkoshy	PROC_LOCK(p);
3653145256Sjkoshy	is_using_hwpmcs = p->p_flag & P_HWPMC;
3654145256Sjkoshy	PROC_UNLOCK(p);
3655145256Sjkoshy
3656147191Sjkoshy	/*
3657147191Sjkoshy	 * Log a sysexit event to all SS PMC owners.
3658147191Sjkoshy	 */
3659147191Sjkoshy	LIST_FOREACH(po, &pmc_ss_owners, po_ssnext)
3660147191Sjkoshy	    if (po->po_flags & PMC_PO_OWNS_LOGFILE)
3661147191Sjkoshy		    pmclog_process_sysexit(po, p->p_pid);
3662145256Sjkoshy
3663147191Sjkoshy	if (!is_using_hwpmcs)
3664147191Sjkoshy		return;
3665147191Sjkoshy
3666147191Sjkoshy	PMC_GET_SX_XLOCK();
3667147191Sjkoshy	PMCDBG(PRC,EXT,1,"process-exit proc=%p (%d, %s)", p, p->p_pid,
3668147191Sjkoshy	    p->p_comm);
3669147191Sjkoshy
3670147191Sjkoshy	/*
3671147191Sjkoshy	 * Since this code is invoked by the last thread in an exiting
3672147191Sjkoshy	 * process, we would have context switched IN at some prior
3673147191Sjkoshy	 * point.  However, with PREEMPTION, kernel mode context
3674147191Sjkoshy	 * switches may happen any time, so we want to disable a
3675147191Sjkoshy	 * context switch OUT till we get any PMCs targetting this
3676147191Sjkoshy	 * process off the hardware.
3677147191Sjkoshy	 *
3678147191Sjkoshy	 * We also need to atomically remove this process'
3679147191Sjkoshy	 * entry from our target process hash table, using
3680147191Sjkoshy	 * PMC_FLAG_REMOVE.
3681147191Sjkoshy	 */
3682147191Sjkoshy	PMCDBG(PRC,EXT,1, "process-exit proc=%p (%d, %s)", p, p->p_pid,
3683147191Sjkoshy	    p->p_comm);
3684147191Sjkoshy
3685147191Sjkoshy	critical_enter(); /* no preemption */
3686147191Sjkoshy
3687147191Sjkoshy	cpu = curthread->td_oncpu;
3688147191Sjkoshy
3689147191Sjkoshy	if ((pp = pmc_find_process_descriptor(p,
3690147191Sjkoshy		 PMC_FLAG_REMOVE)) != NULL) {
3691147191Sjkoshy
3692147191Sjkoshy		PMCDBG(PRC,EXT,2,
3693147191Sjkoshy		    "process-exit proc=%p pmc-process=%p", p, pp);
3694147191Sjkoshy
3695147191Sjkoshy		/*
3696147191Sjkoshy		 * The exiting process could the target of
3697147191Sjkoshy		 * some PMCs which will be running on
3698147191Sjkoshy		 * currently executing CPU.
3699147191Sjkoshy		 *
3700147191Sjkoshy		 * We need to turn these PMCs off like we
3701147191Sjkoshy		 * would do at context switch OUT time.
3702147191Sjkoshy		 */
3703147191Sjkoshy		for (ri = 0; ri < md->pmd_npmc; ri++) {
3704147191Sjkoshy
3705147191Sjkoshy			/*
3706147191Sjkoshy			 * Pick up the pmc pointer from hardware
3707147191Sjkoshy			 * state similar to the CSW_OUT code.
3708147191Sjkoshy			 */
3709147191Sjkoshy			pm = NULL;
3710147191Sjkoshy			(void) (*md->pmd_get_config)(cpu, ri, &pm);
3711147191Sjkoshy
3712147191Sjkoshy			PMCDBG(PRC,EXT,2, "ri=%d pm=%p", ri, pm);
3713147191Sjkoshy
3714147191Sjkoshy			if (pm == NULL ||
3715147191Sjkoshy			    !PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm)))
3716147191Sjkoshy				continue;
3717147191Sjkoshy
3718147191Sjkoshy			PMCDBG(PRC,EXT,2, "ppmcs[%d]=%p pm=%p "
3719147191Sjkoshy			    "state=%d", ri, pp->pp_pmcs[ri].pp_pmc,
3720147191Sjkoshy			    pm, pm->pm_state);
3721147191Sjkoshy
3722147191Sjkoshy			KASSERT(PMC_TO_ROWINDEX(pm) == ri,
3723147191Sjkoshy			    ("[pmc,%d] ri mismatch pmc(%d) ri(%d)",
3724147191Sjkoshy				__LINE__, PMC_TO_ROWINDEX(pm), ri));
3725147191Sjkoshy
3726147191Sjkoshy			KASSERT(pm == pp->pp_pmcs[ri].pp_pmc,
3727147191Sjkoshy			    ("[pmc,%d] pm %p != pp_pmcs[%d] %p",
3728147191Sjkoshy				__LINE__, pm, ri, pp->pp_pmcs[ri].pp_pmc));
3729147191Sjkoshy
3730147191Sjkoshy			(void) md->pmd_stop_pmc(cpu, ri);
3731147191Sjkoshy
3732147191Sjkoshy			KASSERT(pm->pm_runcount > 0,
3733147191Sjkoshy			    ("[pmc,%d] bad runcount ri %d rc %d",
3734147191Sjkoshy				__LINE__, ri, pm->pm_runcount));
3735147191Sjkoshy
3736147867Sjkoshy			/* Stop hardware only if it is actually running */
3737147191Sjkoshy			if (pm->pm_state == PMC_STATE_RUNNING &&
3738147867Sjkoshy			    pm->pm_stalled == 0) {
3739147191Sjkoshy				md->pmd_read_pmc(cpu, ri, &newvalue);
3740147191Sjkoshy				tmp = newvalue -
3741147191Sjkoshy				    PMC_PCPU_SAVED(cpu,ri);
3742147191Sjkoshy
3743147191Sjkoshy				mtx_pool_lock_spin(pmc_mtxpool, pm);
3744147191Sjkoshy				pm->pm_gv.pm_savedvalue += tmp;
3745147191Sjkoshy				pp->pp_pmcs[ri].pp_pmcval += tmp;
3746147191Sjkoshy				mtx_pool_unlock_spin(pmc_mtxpool, pm);
3747147191Sjkoshy			}
3748147191Sjkoshy
3749147191Sjkoshy			atomic_subtract_rel_32(&pm->pm_runcount,1);
3750147191Sjkoshy
3751147191Sjkoshy			KASSERT((int) pm->pm_runcount >= 0,
3752147191Sjkoshy			    ("[pmc,%d] runcount is %d", __LINE__, ri));
3753147191Sjkoshy
3754147191Sjkoshy			(void) md->pmd_config_pmc(cpu, ri, NULL);
3755147191Sjkoshy		}
3756147191Sjkoshy
3757147191Sjkoshy		/*
3758147191Sjkoshy		 * Inform the MD layer of this pseudo "context switch
3759147191Sjkoshy		 * out"
3760147191Sjkoshy		 */
3761147191Sjkoshy		(void) md->pmd_switch_out(pmc_pcpu[cpu], pp);
3762147191Sjkoshy
3763147191Sjkoshy		critical_exit(); /* ok to be pre-empted now */
3764147191Sjkoshy
3765147191Sjkoshy		/*
3766147191Sjkoshy		 * Unlink this process from the PMCs that are
3767147191Sjkoshy		 * targetting it.  This will send a signal to
3768147191Sjkoshy		 * all PMC owner's whose PMCs are orphaned.
3769147191Sjkoshy		 *
3770147191Sjkoshy		 * Log PMC value at exit time if requested.
3771147191Sjkoshy		 */
3772147191Sjkoshy		for (ri = 0; ri < md->pmd_npmc; ri++)
3773147191Sjkoshy			if ((pm = pp->pp_pmcs[ri].pp_pmc) != NULL) {
3774147867Sjkoshy				if (pm->pm_flags & PMC_F_NEEDS_LOGFILE &&
3775147867Sjkoshy				    PMC_IS_COUNTING_MODE(PMC_TO_MODE(pm)))
3776147191Sjkoshy					pmclog_process_procexit(pm, pp);
3777147191Sjkoshy				pmc_unlink_target_process(pm, pp);
3778147191Sjkoshy			}
3779147191Sjkoshy		FREE(pp, M_PMC);
3780147191Sjkoshy
3781147191Sjkoshy	} else
3782147191Sjkoshy		critical_exit(); /* pp == NULL */
3783147191Sjkoshy
3784147191Sjkoshy
3785147191Sjkoshy	/*
3786147191Sjkoshy	 * If the process owned PMCs, free them up and free up
3787147191Sjkoshy	 * memory.
3788147191Sjkoshy	 */
3789147191Sjkoshy	if ((po = pmc_find_owner_descriptor(p)) != NULL) {
3790147191Sjkoshy		pmc_remove_owner(po);
3791147191Sjkoshy		pmc_destroy_owner_descriptor(po);
3792145256Sjkoshy	}
3793147191Sjkoshy
3794147191Sjkoshy	sx_xunlock(&pmc_sx);
3795145256Sjkoshy}
3796145256Sjkoshy
3797145256Sjkoshy/*
3798145256Sjkoshy * Handle a process fork.
3799145256Sjkoshy *
3800145256Sjkoshy * If the parent process 'p1' is under HWPMC monitoring, then copy
3801145256Sjkoshy * over any attached PMCs that have 'do_descendants' semantics.
3802145256Sjkoshy */
3803145256Sjkoshy
3804145256Sjkoshystatic void
3805147191Sjkoshypmc_process_fork(void *arg __unused, struct proc *p1, struct proc *newproc,
3806145256Sjkoshy    int flags)
3807145256Sjkoshy{
3808145256Sjkoshy	int is_using_hwpmcs;
3809147191Sjkoshy	unsigned int ri;
3810147191Sjkoshy	uint32_t do_descendants;
3811147191Sjkoshy	struct pmc *pm;
3812147191Sjkoshy	struct pmc_owner *po;
3813147191Sjkoshy	struct pmc_process *ppnew, *ppold;
3814145256Sjkoshy
3815145256Sjkoshy	(void) flags;		/* unused parameter */
3816145256Sjkoshy
3817145256Sjkoshy	PROC_LOCK(p1);
3818145256Sjkoshy	is_using_hwpmcs = p1->p_flag & P_HWPMC;
3819145256Sjkoshy	PROC_UNLOCK(p1);
3820145256Sjkoshy
3821147191Sjkoshy	/*
3822147191Sjkoshy	 * If there are system-wide sampling PMCs active, we need to
3823147191Sjkoshy	 * log all fork events to their owner's logs.
3824147191Sjkoshy	 */
3825147191Sjkoshy
3826147191Sjkoshy	LIST_FOREACH(po, &pmc_ss_owners, po_ssnext)
3827147191Sjkoshy	    if (po->po_flags & PMC_PO_OWNS_LOGFILE)
3828147191Sjkoshy		    pmclog_process_procfork(po, p1->p_pid, newproc->p_pid);
3829147191Sjkoshy
3830147191Sjkoshy	if (!is_using_hwpmcs)
3831147191Sjkoshy		return;
3832147191Sjkoshy
3833147191Sjkoshy	PMC_GET_SX_XLOCK();
3834147191Sjkoshy	PMCDBG(PMC,FRK,1, "process-fork proc=%p (%d, %s) -> %p", p1,
3835147191Sjkoshy	    p1->p_pid, p1->p_comm, newproc);
3836147191Sjkoshy
3837147191Sjkoshy	/*
3838147191Sjkoshy	 * If the parent process (curthread->td_proc) is a
3839147191Sjkoshy	 * target of any PMCs, look for PMCs that are to be
3840147191Sjkoshy	 * inherited, and link these into the new process
3841147191Sjkoshy	 * descriptor.
3842147191Sjkoshy	 */
3843147191Sjkoshy	if ((ppold = pmc_find_process_descriptor(curthread->td_proc,
3844147191Sjkoshy		 PMC_FLAG_NONE)) == NULL)
3845147191Sjkoshy		goto done;		/* nothing to do */
3846147191Sjkoshy
3847147191Sjkoshy	do_descendants = 0;
3848147191Sjkoshy	for (ri = 0; ri < md->pmd_npmc; ri++)
3849147191Sjkoshy		if ((pm = ppold->pp_pmcs[ri].pp_pmc) != NULL)
3850147191Sjkoshy			do_descendants |= pm->pm_flags & PMC_F_DESCENDANTS;
3851147191Sjkoshy	if (do_descendants == 0) /* nothing to do */
3852147191Sjkoshy		goto done;
3853147191Sjkoshy
3854147191Sjkoshy	/* allocate a descriptor for the new process  */
3855147191Sjkoshy	if ((ppnew = pmc_find_process_descriptor(newproc,
3856147191Sjkoshy		 PMC_FLAG_ALLOCATE)) == NULL)
3857147191Sjkoshy		goto done;
3858147191Sjkoshy
3859147191Sjkoshy	/*
3860147191Sjkoshy	 * Run through all PMCs that were targeting the old process
3861147191Sjkoshy	 * and which specified F_DESCENDANTS and attach them to the
3862147191Sjkoshy	 * new process.
3863147191Sjkoshy	 *
3864147191Sjkoshy	 * Log the fork event to all owners of PMCs attached to this
3865147191Sjkoshy	 * process, if not already logged.
3866147191Sjkoshy	 */
3867147191Sjkoshy	for (ri = 0; ri < md->pmd_npmc; ri++)
3868147191Sjkoshy		if ((pm = ppold->pp_pmcs[ri].pp_pmc) != NULL &&
3869147191Sjkoshy		    (pm->pm_flags & PMC_F_DESCENDANTS)) {
3870147191Sjkoshy			pmc_link_target_process(pm, ppnew);
3871147191Sjkoshy			po = pm->pm_owner;
3872147191Sjkoshy			if (po->po_sscount == 0 &&
3873147191Sjkoshy			    po->po_flags & PMC_PO_OWNS_LOGFILE)
3874147191Sjkoshy				pmclog_process_procfork(po, p1->p_pid,
3875147191Sjkoshy				    newproc->p_pid);
3876147191Sjkoshy		}
3877147191Sjkoshy
3878147191Sjkoshy	/*
3879147191Sjkoshy	 * Now mark the new process as being tracked by this driver.
3880147191Sjkoshy	 */
3881147191Sjkoshy	PROC_LOCK(newproc);
3882147191Sjkoshy	newproc->p_flag |= P_HWPMC;
3883147191Sjkoshy	PROC_UNLOCK(newproc);
3884147191Sjkoshy
3885147191Sjkoshy done:
3886147191Sjkoshy	sx_xunlock(&pmc_sx);
3887145256Sjkoshy}
3888145256Sjkoshy
3889145256Sjkoshy
3890145256Sjkoshy/*
3891145256Sjkoshy * initialization
3892145256Sjkoshy */
3893145256Sjkoshy
3894145256Sjkoshystatic const char *pmc_name_of_pmcclass[] = {
3895145256Sjkoshy#undef	__PMC_CLASS
3896145256Sjkoshy#define	__PMC_CLASS(N) #N ,
3897145256Sjkoshy	__PMC_CLASSES()
3898145256Sjkoshy};
3899145256Sjkoshy
3900145256Sjkoshystatic int
3901145256Sjkoshypmc_initialize(void)
3902145256Sjkoshy{
3903147191Sjkoshy	int cpu, error, n;
3904145256Sjkoshy	struct pmc_binding pb;
3905147191Sjkoshy	struct pmc_samplebuffer *sb;
3906145256Sjkoshy
3907145256Sjkoshy	md = NULL;
3908145256Sjkoshy	error = 0;
3909145256Sjkoshy
3910145256Sjkoshy#if	DEBUG
3911145256Sjkoshy	/* parse debug flags first */
3912145256Sjkoshy	if (TUNABLE_STR_FETCH(PMC_SYSCTL_NAME_PREFIX "debugflags",
3913145256Sjkoshy		pmc_debugstr, sizeof(pmc_debugstr)))
3914145256Sjkoshy		pmc_debugflags_parse(pmc_debugstr,
3915145256Sjkoshy		    pmc_debugstr+strlen(pmc_debugstr));
3916145256Sjkoshy#endif
3917145256Sjkoshy
3918145256Sjkoshy	PMCDBG(MOD,INI,0, "PMC Initialize (version %x)", PMC_VERSION);
3919145256Sjkoshy
3920145256Sjkoshy	/*
3921145256Sjkoshy	 * check sysctl parameters
3922145256Sjkoshy	 */
3923145256Sjkoshy
3924145256Sjkoshy	if (pmc_hashsize <= 0) {
3925147191Sjkoshy		(void) printf("hwpmc: tunable hashsize=%d must be greater "
3926147191Sjkoshy		    "than zero.\n", pmc_hashsize);
3927145256Sjkoshy		pmc_hashsize = PMC_HASH_SIZE;
3928145256Sjkoshy	}
3929145256Sjkoshy
3930147191Sjkoshy	if (pmc_nsamples <= 0 || pmc_nsamples > 65535) {
3931147191Sjkoshy		(void) printf("hwpmc: tunable nsamples=%d out of range.\n", pmc_nsamples);
3932147191Sjkoshy		pmc_nsamples = PMC_NSAMPLES;
3933147191Sjkoshy	}
3934145256Sjkoshy
3935147191Sjkoshy	md = pmc_md_initialize();
3936147191Sjkoshy
3937145256Sjkoshy	if (md == NULL || md->pmd_init == NULL)
3938145256Sjkoshy		return ENOSYS;
3939145256Sjkoshy
3940145256Sjkoshy	/* allocate space for the per-cpu array */
3941145256Sjkoshy	MALLOC(pmc_pcpu, struct pmc_cpu **, mp_ncpus * sizeof(struct pmc_cpu *),
3942145256Sjkoshy	    M_PMC, M_WAITOK|M_ZERO);
3943145256Sjkoshy
3944145256Sjkoshy	/* per-cpu 'saved values' for managing process-mode PMCs */
3945145256Sjkoshy	MALLOC(pmc_pcpu_saved, pmc_value_t *,
3946145256Sjkoshy	    sizeof(pmc_value_t) * mp_ncpus * md->pmd_npmc, M_PMC, M_WAITOK);
3947145256Sjkoshy
3948145256Sjkoshy	/* perform cpu dependent initialization */
3949145256Sjkoshy	pmc_save_cpu_binding(&pb);
3950145256Sjkoshy	for (cpu = 0; cpu < mp_ncpus; cpu++) {
3951145256Sjkoshy		if (pmc_cpu_is_disabled(cpu))
3952145256Sjkoshy			continue;
3953145256Sjkoshy		pmc_select_cpu(cpu);
3954145256Sjkoshy		if ((error = md->pmd_init(cpu)) != 0)
3955145256Sjkoshy			break;
3956145256Sjkoshy	}
3957145256Sjkoshy	pmc_restore_cpu_binding(&pb);
3958145256Sjkoshy
3959145256Sjkoshy	if (error != 0)
3960145256Sjkoshy		return error;
3961145256Sjkoshy
3962147191Sjkoshy	/* allocate space for the sample array */
3963147191Sjkoshy	for (cpu = 0; cpu < mp_ncpus; cpu++) {
3964147191Sjkoshy		if (pmc_cpu_is_disabled(cpu))
3965147191Sjkoshy			continue;
3966147191Sjkoshy		MALLOC(sb, struct pmc_samplebuffer *,
3967147191Sjkoshy		    sizeof(struct pmc_samplebuffer) +
3968147191Sjkoshy		    pmc_nsamples * sizeof(struct pmc_sample), M_PMC,
3969147191Sjkoshy		    M_WAITOK|M_ZERO);
3970147191Sjkoshy
3971147191Sjkoshy		sb->ps_read = sb->ps_write = sb->ps_samples;
3972147191Sjkoshy		sb->ps_fence = sb->ps_samples + pmc_nsamples
3973147191Sjkoshy;
3974147191Sjkoshy		KASSERT(pmc_pcpu[cpu] != NULL,
3975147191Sjkoshy		    ("[pmc,%d] cpu=%d Null per-cpu data", __LINE__, cpu));
3976147191Sjkoshy
3977147191Sjkoshy		pmc_pcpu[cpu]->pc_sb = sb;
3978147191Sjkoshy	}
3979147191Sjkoshy
3980145256Sjkoshy	/* allocate space for the row disposition array */
3981145256Sjkoshy	pmc_pmcdisp = malloc(sizeof(enum pmc_mode) * md->pmd_npmc,
3982145256Sjkoshy	    M_PMC, M_WAITOK|M_ZERO);
3983145256Sjkoshy
3984145256Sjkoshy	KASSERT(pmc_pmcdisp != NULL,
3985145256Sjkoshy	    ("[pmc,%d] pmcdisp allocation returned NULL", __LINE__));
3986145256Sjkoshy
3987145256Sjkoshy	/* mark all PMCs as available */
3988145256Sjkoshy	for (n = 0; n < (int) md->pmd_npmc; n++)
3989145256Sjkoshy		PMC_MARK_ROW_FREE(n);
3990145256Sjkoshy
3991145256Sjkoshy	/* allocate thread hash tables */
3992145256Sjkoshy	pmc_ownerhash = hashinit(pmc_hashsize, M_PMC,
3993145256Sjkoshy	    &pmc_ownerhashmask);
3994145256Sjkoshy
3995145256Sjkoshy	pmc_processhash = hashinit(pmc_hashsize, M_PMC,
3996145256Sjkoshy	    &pmc_processhashmask);
3997145256Sjkoshy	mtx_init(&pmc_processhash_mtx, "pmc-process-hash", "pmc", MTX_SPIN);
3998145256Sjkoshy
3999147191Sjkoshy	LIST_INIT(&pmc_ss_owners);
4000147191Sjkoshy	pmc_ss_count = 0;
4001147191Sjkoshy
4002145256Sjkoshy	/* allocate a pool of spin mutexes */
4003145256Sjkoshy	pmc_mtxpool = mtx_pool_create("pmc", pmc_mtxpool_size, MTX_SPIN);
4004145256Sjkoshy
4005145256Sjkoshy	PMCDBG(MOD,INI,1, "pmc_ownerhash=%p, mask=0x%lx "
4006145256Sjkoshy	    "targethash=%p mask=0x%lx", pmc_ownerhash, pmc_ownerhashmask,
4007145256Sjkoshy	    pmc_processhash, pmc_processhashmask);
4008145256Sjkoshy
4009145256Sjkoshy	/* register process {exit,fork,exec} handlers */
4010145256Sjkoshy	pmc_exit_tag = EVENTHANDLER_REGISTER(process_exit,
4011145256Sjkoshy	    pmc_process_exit, NULL, EVENTHANDLER_PRI_ANY);
4012145256Sjkoshy	pmc_fork_tag = EVENTHANDLER_REGISTER(process_fork,
4013145256Sjkoshy	    pmc_process_fork, NULL, EVENTHANDLER_PRI_ANY);
4014145256Sjkoshy
4015147191Sjkoshy	/* initialize logging */
4016147191Sjkoshy	pmclog_initialize();
4017147191Sjkoshy
4018145256Sjkoshy	/* set hook functions */
4019145256Sjkoshy	pmc_intr = md->pmd_intr;
4020145256Sjkoshy	pmc_hook = pmc_hook_handler;
4021145256Sjkoshy
4022145256Sjkoshy	if (error == 0) {
4023145256Sjkoshy		printf(PMC_MODULE_NAME ":");
4024145256Sjkoshy		for (n = 0; n < (int) md->pmd_nclass; n++)
4025145256Sjkoshy			printf(" %s(%d)",
4026145774Sjkoshy			    pmc_name_of_pmcclass[md->pmd_classes[n].pm_class],
4027145256Sjkoshy			    md->pmd_nclasspmcs[n]);
4028145256Sjkoshy		printf("\n");
4029145256Sjkoshy	}
4030145256Sjkoshy
4031145256Sjkoshy	return error;
4032145256Sjkoshy}
4033145256Sjkoshy
4034145256Sjkoshy/* prepare to be unloaded */
4035145256Sjkoshystatic void
4036145256Sjkoshypmc_cleanup(void)
4037145256Sjkoshy{
4038145256Sjkoshy	int cpu;
4039145256Sjkoshy	struct pmc_ownerhash *ph;
4040145256Sjkoshy	struct pmc_owner *po, *tmp;
4041145256Sjkoshy	struct pmc_binding pb;
4042145256Sjkoshy#if	DEBUG
4043145256Sjkoshy	struct pmc_processhash *prh;
4044145256Sjkoshy#endif
4045145256Sjkoshy
4046145256Sjkoshy	PMCDBG(MOD,INI,0, "%s", "cleanup");
4047145256Sjkoshy
4048147191Sjkoshy	/* switch off sampling */
4049147191Sjkoshy	atomic_store_rel_int(&pmc_cpumask, 0);
4050147191Sjkoshy	pmc_intr = NULL;
4051145256Sjkoshy
4052145256Sjkoshy	sx_xlock(&pmc_sx);
4053145256Sjkoshy	if (pmc_hook == NULL) {	/* being unloaded already */
4054145256Sjkoshy		sx_xunlock(&pmc_sx);
4055145256Sjkoshy		return;
4056145256Sjkoshy	}
4057145256Sjkoshy
4058145256Sjkoshy	pmc_hook = NULL; /* prevent new threads from entering module */
4059145256Sjkoshy
4060145256Sjkoshy	/* deregister event handlers */
4061145256Sjkoshy	EVENTHANDLER_DEREGISTER(process_fork, pmc_fork_tag);
4062145256Sjkoshy	EVENTHANDLER_DEREGISTER(process_exit, pmc_exit_tag);
4063145256Sjkoshy
4064145256Sjkoshy	/* send SIGBUS to all owner threads, free up allocations */
4065145256Sjkoshy	if (pmc_ownerhash)
4066145256Sjkoshy		for (ph = pmc_ownerhash;
4067145256Sjkoshy		     ph <= &pmc_ownerhash[pmc_ownerhashmask];
4068145256Sjkoshy		     ph++) {
4069145256Sjkoshy			LIST_FOREACH_SAFE(po, ph, po_next, tmp) {
4070145256Sjkoshy				pmc_remove_owner(po);
4071145256Sjkoshy
4072145256Sjkoshy				/* send SIGBUS to owner processes */
4073145256Sjkoshy				PMCDBG(MOD,INI,2, "cleanup signal proc=%p "
4074145256Sjkoshy				    "(%d, %s)", po->po_owner,
4075145256Sjkoshy				    po->po_owner->p_pid,
4076145256Sjkoshy				    po->po_owner->p_comm);
4077145256Sjkoshy
4078145256Sjkoshy				PROC_LOCK(po->po_owner);
4079145256Sjkoshy				psignal(po->po_owner, SIGBUS);
4080145256Sjkoshy				PROC_UNLOCK(po->po_owner);
4081147191Sjkoshy
4082147191Sjkoshy				pmc_destroy_owner_descriptor(po);
4083145256Sjkoshy			}
4084145256Sjkoshy		}
4085145256Sjkoshy
4086145256Sjkoshy	/* reclaim allocated data structures */
4087145256Sjkoshy	if (pmc_mtxpool)
4088145256Sjkoshy		mtx_pool_destroy(&pmc_mtxpool);
4089145256Sjkoshy
4090145256Sjkoshy	mtx_destroy(&pmc_processhash_mtx);
4091145256Sjkoshy	if (pmc_processhash) {
4092145256Sjkoshy#if	DEBUG
4093145256Sjkoshy		struct pmc_process *pp;
4094145256Sjkoshy
4095145256Sjkoshy		PMCDBG(MOD,INI,3, "%s", "destroy process hash");
4096145256Sjkoshy		for (prh = pmc_processhash;
4097145256Sjkoshy		     prh <= &pmc_processhash[pmc_processhashmask];
4098145256Sjkoshy		     prh++)
4099145256Sjkoshy			LIST_FOREACH(pp, prh, pp_next)
4100145256Sjkoshy			    PMCDBG(MOD,INI,3, "pid=%d", pp->pp_proc->p_pid);
4101145256Sjkoshy#endif
4102145256Sjkoshy
4103145256Sjkoshy		hashdestroy(pmc_processhash, M_PMC, pmc_processhashmask);
4104145256Sjkoshy		pmc_processhash = NULL;
4105145256Sjkoshy	}
4106145256Sjkoshy
4107145256Sjkoshy	if (pmc_ownerhash) {
4108145256Sjkoshy		PMCDBG(MOD,INI,3, "%s", "destroy owner hash");
4109145256Sjkoshy		hashdestroy(pmc_ownerhash, M_PMC, pmc_ownerhashmask);
4110145256Sjkoshy		pmc_ownerhash = NULL;
4111145256Sjkoshy	}
4112145256Sjkoshy
4113147191Sjkoshy	KASSERT(LIST_EMPTY(&pmc_ss_owners),
4114147191Sjkoshy	    ("[pmc,%d] Global SS owner list not empty", __LINE__));
4115147191Sjkoshy	KASSERT(pmc_ss_count == 0,
4116147191Sjkoshy	    ("[pmc,%d] Global SS count not empty", __LINE__));
4117147191Sjkoshy
4118145256Sjkoshy 	/* do processor dependent cleanup */
4119145256Sjkoshy	PMCDBG(MOD,INI,3, "%s", "md cleanup");
4120145256Sjkoshy	if (md) {
4121145256Sjkoshy		pmc_save_cpu_binding(&pb);
4122145256Sjkoshy		for (cpu = 0; cpu < mp_ncpus; cpu++) {
4123145256Sjkoshy			PMCDBG(MOD,INI,1,"pmc-cleanup cpu=%d pcs=%p",
4124145256Sjkoshy			    cpu, pmc_pcpu[cpu]);
4125145256Sjkoshy			if (pmc_cpu_is_disabled(cpu))
4126145256Sjkoshy				continue;
4127145256Sjkoshy			pmc_select_cpu(cpu);
4128145256Sjkoshy			if (pmc_pcpu[cpu])
4129145256Sjkoshy				(void) md->pmd_cleanup(cpu);
4130145256Sjkoshy		}
4131145256Sjkoshy		FREE(md, M_PMC);
4132145256Sjkoshy		md = NULL;
4133145256Sjkoshy		pmc_restore_cpu_binding(&pb);
4134145256Sjkoshy	}
4135145256Sjkoshy
4136145256Sjkoshy	/* deallocate per-cpu structures */
4137145256Sjkoshy	FREE(pmc_pcpu, M_PMC);
4138145256Sjkoshy	pmc_pcpu = NULL;
4139145256Sjkoshy
4140145256Sjkoshy	FREE(pmc_pcpu_saved, M_PMC);
4141145256Sjkoshy	pmc_pcpu_saved = NULL;
4142145256Sjkoshy
4143145256Sjkoshy	if (pmc_pmcdisp) {
4144145256Sjkoshy		FREE(pmc_pmcdisp, M_PMC);
4145145256Sjkoshy		pmc_pmcdisp = NULL;
4146145256Sjkoshy	}
4147145256Sjkoshy
4148147191Sjkoshy	pmclog_shutdown();
4149147191Sjkoshy
4150145256Sjkoshy	sx_xunlock(&pmc_sx); 	/* we are done */
4151145256Sjkoshy}
4152145256Sjkoshy
4153145256Sjkoshy/*
4154145256Sjkoshy * The function called at load/unload.
4155145256Sjkoshy */
4156145256Sjkoshy
4157145256Sjkoshystatic int
4158145256Sjkoshyload (struct module *module __unused, int cmd, void *arg __unused)
4159145256Sjkoshy{
4160145256Sjkoshy	int error;
4161145256Sjkoshy
4162145256Sjkoshy	error = 0;
4163145256Sjkoshy
4164145256Sjkoshy	switch (cmd) {
4165145256Sjkoshy	case MOD_LOAD :
4166145256Sjkoshy		/* initialize the subsystem */
4167145256Sjkoshy		error = pmc_initialize();
4168145256Sjkoshy		if (error != 0)
4169145256Sjkoshy			break;
4170145256Sjkoshy		PMCDBG(MOD,INI,1, "syscall=%d ncpus=%d",
4171145256Sjkoshy		    pmc_syscall_num, mp_ncpus);
4172145256Sjkoshy		break;
4173145256Sjkoshy
4174145256Sjkoshy
4175145256Sjkoshy	case MOD_UNLOAD :
4176145256Sjkoshy	case MOD_SHUTDOWN:
4177145256Sjkoshy		pmc_cleanup();
4178145256Sjkoshy		PMCDBG(MOD,INI,1, "%s", "unloaded");
4179145256Sjkoshy		break;
4180145256Sjkoshy
4181145256Sjkoshy	default :
4182145256Sjkoshy		error = EINVAL;	/* XXX should panic(9) */
4183145256Sjkoshy		break;
4184145256Sjkoshy	}
4185145256Sjkoshy
4186145256Sjkoshy	return error;
4187145256Sjkoshy}
4188145256Sjkoshy
4189145256Sjkoshy/* memory pool */
4190145256SjkoshyMALLOC_DEFINE(M_PMC, "pmc", "Memory space for the PMC module");
4191