1145256Sjkoshy/*-
2183266Sjkoshy * Copyright (c) 2003-2008 Joseph Koshy
3174395Sjkoshy * Copyright (c) 2007 The FreeBSD Foundation
4145256Sjkoshy * All rights reserved.
5145256Sjkoshy *
6174395Sjkoshy * Portions of this software were developed by A. Joseph Koshy under
7174395Sjkoshy * sponsorship from the FreeBSD Foundation and Google, Inc.
8174395Sjkoshy *
9145256Sjkoshy * Redistribution and use in source and binary forms, with or without
10145256Sjkoshy * modification, are permitted provided that the following conditions
11145256Sjkoshy * are met:
12145256Sjkoshy * 1. Redistributions of source code must retain the above copyright
13145256Sjkoshy *    notice, this list of conditions and the following disclaimer.
14145256Sjkoshy * 2. Redistributions in binary form must reproduce the above copyright
15145256Sjkoshy *    notice, this list of conditions and the following disclaimer in the
16145256Sjkoshy *    documentation and/or other materials provided with the distribution.
17145256Sjkoshy *
18145256Sjkoshy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19145256Sjkoshy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20145256Sjkoshy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21145256Sjkoshy * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22145256Sjkoshy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23145256Sjkoshy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24145256Sjkoshy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25145256Sjkoshy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26145256Sjkoshy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27145256Sjkoshy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28145256Sjkoshy * SUCH DAMAGE.
29145256Sjkoshy *
30145256Sjkoshy */
31145256Sjkoshy
32145256Sjkoshy#include <sys/cdefs.h>
33145256Sjkoshy__FBSDID("$FreeBSD$");
34145256Sjkoshy
35145256Sjkoshy#include <sys/param.h>
36145256Sjkoshy#include <sys/eventhandler.h>
37145256Sjkoshy#include <sys/jail.h>
38145256Sjkoshy#include <sys/kernel.h>
39147191Sjkoshy#include <sys/kthread.h>
40145256Sjkoshy#include <sys/limits.h>
41145256Sjkoshy#include <sys/lock.h>
42145256Sjkoshy#include <sys/malloc.h>
43145256Sjkoshy#include <sys/module.h>
44201151Sjkoshy#include <sys/mount.h>
45145256Sjkoshy#include <sys/mutex.h>
46145256Sjkoshy#include <sys/pmc.h>
47145256Sjkoshy#include <sys/pmckern.h>
48147191Sjkoshy#include <sys/pmclog.h>
49164033Srwatson#include <sys/priv.h>
50145256Sjkoshy#include <sys/proc.h>
51145256Sjkoshy#include <sys/queue.h>
52147191Sjkoshy#include <sys/resourcevar.h>
53248084Sattilio#include <sys/rwlock.h>
54145256Sjkoshy#include <sys/sched.h>
55145256Sjkoshy#include <sys/signalvar.h>
56145256Sjkoshy#include <sys/smp.h>
57145256Sjkoshy#include <sys/sx.h>
58145256Sjkoshy#include <sys/sysctl.h>
59145256Sjkoshy#include <sys/sysent.h>
60145256Sjkoshy#include <sys/systm.h>
61147191Sjkoshy#include <sys/vnode.h>
62145256Sjkoshy
63157144Sjkoshy#include <sys/linker.h>		/* needs to be after <sys/malloc.h> */
64157144Sjkoshy
65147191Sjkoshy#include <machine/atomic.h>
66145256Sjkoshy#include <machine/md_var.h>
67145256Sjkoshy
68201021Sjkoshy#include <vm/vm.h>
69201021Sjkoshy#include <vm/vm_extern.h>
70201021Sjkoshy#include <vm/pmap.h>
71201021Sjkoshy#include <vm/vm_map.h>
72201021Sjkoshy#include <vm/vm_object.h>
73201021Sjkoshy
74233628Sfabient#include "hwpmc_soft.h"
75233628Sfabient
76145256Sjkoshy/*
77145256Sjkoshy * Types
78145256Sjkoshy */
79145256Sjkoshy
80145256Sjkoshyenum pmc_flags {
81145256Sjkoshy	PMC_FLAG_NONE	  = 0x00, /* do nothing */
82145256Sjkoshy	PMC_FLAG_REMOVE   = 0x01, /* atomically remove entry from hash */
83145256Sjkoshy	PMC_FLAG_ALLOCATE = 0x02, /* add entry to hash if not found */
84145256Sjkoshy};
85145256Sjkoshy
86145256Sjkoshy/*
87145256Sjkoshy * The offset in sysent where the syscall is allocated.
88145256Sjkoshy */
89145256Sjkoshy
90145256Sjkoshystatic int pmc_syscall_num = NO_SYSCALL;
91145256Sjkoshystruct pmc_cpu		**pmc_pcpu;	 /* per-cpu state */
92145256Sjkoshypmc_value_t		*pmc_pcpu_saved; /* saved PMC values: CSW handling */
93145256Sjkoshy
94145256Sjkoshy#define	PMC_PCPU_SAVED(C,R)	pmc_pcpu_saved[(R) + md->pmd_npmc*(C)]
95145256Sjkoshy
96145256Sjkoshystruct mtx_pool		*pmc_mtxpool;
97145256Sjkoshystatic int		*pmc_pmcdisp;	 /* PMC row dispositions */
98145256Sjkoshy
99145256Sjkoshy#define	PMC_ROW_DISP_IS_FREE(R)		(pmc_pmcdisp[(R)] == 0)
100145256Sjkoshy#define	PMC_ROW_DISP_IS_THREAD(R)	(pmc_pmcdisp[(R)] > 0)
101145256Sjkoshy#define	PMC_ROW_DISP_IS_STANDALONE(R)	(pmc_pmcdisp[(R)] < 0)
102145256Sjkoshy
103145256Sjkoshy#define	PMC_MARK_ROW_FREE(R) do {					  \
104145256Sjkoshy	pmc_pmcdisp[(R)] = 0;						  \
105145256Sjkoshy} while (0)
106145256Sjkoshy
107145256Sjkoshy#define	PMC_MARK_ROW_STANDALONE(R) do {					  \
108145256Sjkoshy	KASSERT(pmc_pmcdisp[(R)] <= 0, ("[pmc,%d] row disposition error", \
109145256Sjkoshy		    __LINE__));						  \
110145256Sjkoshy	atomic_add_int(&pmc_pmcdisp[(R)], -1);				  \
111183266Sjkoshy	KASSERT(pmc_pmcdisp[(R)] >= (-pmc_cpu_max_active()),		  \
112183266Sjkoshy		("[pmc,%d] row disposition error", __LINE__));		  \
113145256Sjkoshy} while (0)
114145256Sjkoshy
115145256Sjkoshy#define	PMC_UNMARK_ROW_STANDALONE(R) do { 				  \
116145256Sjkoshy	atomic_add_int(&pmc_pmcdisp[(R)], 1);				  \
117145256Sjkoshy	KASSERT(pmc_pmcdisp[(R)] <= 0, ("[pmc,%d] row disposition error", \
118145256Sjkoshy		    __LINE__));						  \
119145256Sjkoshy} while (0)
120145256Sjkoshy
121145256Sjkoshy#define	PMC_MARK_ROW_THREAD(R) do {					  \
122145256Sjkoshy	KASSERT(pmc_pmcdisp[(R)] >= 0, ("[pmc,%d] row disposition error", \
123145256Sjkoshy		    __LINE__));						  \
124145256Sjkoshy	atomic_add_int(&pmc_pmcdisp[(R)], 1);				  \
125145256Sjkoshy} while (0)
126145256Sjkoshy
127145256Sjkoshy#define	PMC_UNMARK_ROW_THREAD(R) do {					  \
128145256Sjkoshy	atomic_add_int(&pmc_pmcdisp[(R)], -1);				  \
129145256Sjkoshy	KASSERT(pmc_pmcdisp[(R)] >= 0, ("[pmc,%d] row disposition error", \
130145256Sjkoshy		    __LINE__));						  \
131145256Sjkoshy} while (0)
132145256Sjkoshy
133145256Sjkoshy
134145256Sjkoshy/* various event handlers */
135254813Smarkjstatic eventhandler_tag	pmc_exit_tag, pmc_fork_tag, pmc_kld_load_tag,
136254813Smarkj    pmc_kld_unload_tag;
137145256Sjkoshy
138145256Sjkoshy/* Module statistics */
139145256Sjkoshystruct pmc_op_getdriverstats pmc_stats;
140145256Sjkoshy
141145256Sjkoshy/* Machine/processor dependent operations */
142185363Sjkoshystatic struct pmc_mdep  *md;
143145256Sjkoshy
144145256Sjkoshy/*
145145256Sjkoshy * Hash tables mapping owner processes and target threads to PMCs.
146145256Sjkoshy */
147145256Sjkoshy
148145256Sjkoshystruct mtx pmc_processhash_mtx;		/* spin mutex */
149145256Sjkoshystatic u_long pmc_processhashmask;
150145256Sjkoshystatic LIST_HEAD(pmc_processhash, pmc_process)	*pmc_processhash;
151145256Sjkoshy
152145256Sjkoshy/*
153145256Sjkoshy * Hash table of PMC owner descriptors.  This table is protected by
154145256Sjkoshy * the shared PMC "sx" lock.
155145256Sjkoshy */
156145256Sjkoshy
157145256Sjkoshystatic u_long pmc_ownerhashmask;
158145256Sjkoshystatic LIST_HEAD(pmc_ownerhash, pmc_owner)	*pmc_ownerhash;
159145256Sjkoshy
160145256Sjkoshy/*
161147191Sjkoshy * List of PMC owners with system-wide sampling PMCs.
162147191Sjkoshy */
163147191Sjkoshy
164147191Sjkoshystatic LIST_HEAD(, pmc_owner)			pmc_ss_owners;
165147191Sjkoshy
166147191Sjkoshy
167147191Sjkoshy/*
168184802Sjkoshy * A map of row indices to classdep structures.
169184802Sjkoshy */
170184802Sjkoshystatic struct pmc_classdep **pmc_rowindex_to_classdep;
171184802Sjkoshy
172184802Sjkoshy/*
173145256Sjkoshy * Prototypes
174145256Sjkoshy */
175145256Sjkoshy
176153110Sru#ifdef	DEBUG
177145256Sjkoshystatic int	pmc_debugflags_sysctl_handler(SYSCTL_HANDLER_ARGS);
178145256Sjkoshystatic int	pmc_debugflags_parse(char *newstr, char *fence);
179145256Sjkoshy#endif
180145256Sjkoshy
181145256Sjkoshystatic int	load(struct module *module, int cmd, void *arg);
182147191Sjkoshystatic int	pmc_attach_process(struct proc *p, struct pmc *pm);
183145256Sjkoshystatic struct pmc *pmc_allocate_pmc_descriptor(void);
184147191Sjkoshystatic struct pmc_owner *pmc_allocate_owner_descriptor(struct proc *p);
185147191Sjkoshystatic int	pmc_attach_one_process(struct proc *p, struct pmc *pm);
186147191Sjkoshystatic int	pmc_can_allocate_rowindex(struct proc *p, unsigned int ri,
187147191Sjkoshy    int cpu);
188147191Sjkoshystatic int	pmc_can_attach(struct pmc *pm, struct proc *p);
189233628Sfabientstatic void	pmc_capture_user_callchain(int cpu, int soft, struct trapframe *tf);
190147191Sjkoshystatic void	pmc_cleanup(void);
191147191Sjkoshystatic int	pmc_detach_process(struct proc *p, struct pmc *pm);
192147191Sjkoshystatic int	pmc_detach_one_process(struct proc *p, struct pmc *pm,
193147191Sjkoshy    int flags);
194147191Sjkoshystatic void	pmc_destroy_owner_descriptor(struct pmc_owner *po);
195147191Sjkoshystatic struct pmc_owner *pmc_find_owner_descriptor(struct proc *p);
196147191Sjkoshystatic int	pmc_find_pmc(pmc_id_t pmcid, struct pmc **pm);
197145256Sjkoshystatic struct pmc *pmc_find_pmc_descriptor_in_process(struct pmc_owner *po,
198145256Sjkoshy    pmc_id_t pmc);
199145256Sjkoshystatic struct pmc_process *pmc_find_process_descriptor(struct proc *p,
200145256Sjkoshy    uint32_t mode);
201145774Sjkoshystatic void	pmc_force_context_switch(void);
202145256Sjkoshystatic void	pmc_link_target_process(struct pmc *pm,
203145256Sjkoshy    struct pmc_process *pp);
204174395Sjkoshystatic void	pmc_log_all_process_mappings(struct pmc_owner *po);
205174395Sjkoshystatic void	pmc_log_kernel_mappings(struct pmc *pm);
206174395Sjkoshystatic void	pmc_log_process_mappings(struct pmc_owner *po, struct proc *p);
207147191Sjkoshystatic void	pmc_maybe_remove_owner(struct pmc_owner *po);
208147191Sjkoshystatic void	pmc_process_csw_in(struct thread *td);
209147191Sjkoshystatic void	pmc_process_csw_out(struct thread *td);
210145256Sjkoshystatic void	pmc_process_exit(void *arg, struct proc *p);
211145256Sjkoshystatic void	pmc_process_fork(void *arg, struct proc *p1,
212145256Sjkoshy    struct proc *p2, int n);
213233628Sfabientstatic void	pmc_process_samples(int cpu, int soft);
214147191Sjkoshystatic void	pmc_release_pmc_descriptor(struct pmc *pmc);
215147191Sjkoshystatic void	pmc_remove_owner(struct pmc_owner *po);
216147191Sjkoshystatic void	pmc_remove_process_descriptor(struct pmc_process *pp);
217147191Sjkoshystatic void	pmc_restore_cpu_binding(struct pmc_binding *pb);
218147191Sjkoshystatic void	pmc_save_cpu_binding(struct pmc_binding *pb);
219147191Sjkoshystatic void	pmc_select_cpu(int cpu);
220145256Sjkoshystatic int	pmc_start(struct pmc *pm);
221145256Sjkoshystatic int	pmc_stop(struct pmc *pm);
222147191Sjkoshystatic int	pmc_syscall_handler(struct thread *td, void *syscall_args);
223147191Sjkoshystatic void	pmc_unlink_target_process(struct pmc *pmc,
224147191Sjkoshy    struct pmc_process *pp);
225233628Sfabientstatic int generic_switch_in(struct pmc_cpu *pc, struct pmc_process *pp);
226233628Sfabientstatic int generic_switch_out(struct pmc_cpu *pc, struct pmc_process *pp);
227233628Sfabientstatic struct pmc_mdep *pmc_generic_cpu_initialize(void);
228233628Sfabientstatic void pmc_generic_cpu_finalize(struct pmc_mdep *md);
229145256Sjkoshy
230145256Sjkoshy/*
231145256Sjkoshy * Kernel tunables and sysctl(8) interface.
232145256Sjkoshy */
233145256Sjkoshy
234233628SfabientSYSCTL_DECL(_kern_hwpmc);
235145256Sjkoshy
236174395Sjkoshystatic int pmc_callchaindepth = PMC_CALLCHAIN_DEPTH;
237174395SjkoshyTUNABLE_INT(PMC_SYSCTL_NAME_PREFIX "callchaindepth", &pmc_callchaindepth);
238174395SjkoshySYSCTL_INT(_kern_hwpmc, OID_AUTO, callchaindepth, CTLFLAG_TUN|CTLFLAG_RD,
239174395Sjkoshy    &pmc_callchaindepth, 0, "depth of call chain records");
240174395Sjkoshy
241153110Sru#ifdef	DEBUG
242147191Sjkoshystruct pmc_debugflags pmc_debugflags = PMC_DEBUG_DEFAULT_FLAGS;
243145256Sjkoshychar	pmc_debugstr[PMC_DEBUG_STRSIZE];
244145256SjkoshyTUNABLE_STR(PMC_SYSCTL_NAME_PREFIX "debugflags", pmc_debugstr,
245145256Sjkoshy    sizeof(pmc_debugstr));
246145256SjkoshySYSCTL_PROC(_kern_hwpmc, OID_AUTO, debugflags,
247145256Sjkoshy    CTLTYPE_STRING|CTLFLAG_RW|CTLFLAG_TUN,
248145256Sjkoshy    0, 0, pmc_debugflags_sysctl_handler, "A", "debug flags");
249145256Sjkoshy#endif
250145256Sjkoshy
251145256Sjkoshy/*
252147191Sjkoshy * kern.hwpmc.hashrows -- determines the number of rows in the
253145256Sjkoshy * of the hash table used to look up threads
254145256Sjkoshy */
255145256Sjkoshy
256145256Sjkoshystatic int pmc_hashsize = PMC_HASH_SIZE;
257145256SjkoshyTUNABLE_INT(PMC_SYSCTL_NAME_PREFIX "hashsize", &pmc_hashsize);
258145256SjkoshySYSCTL_INT(_kern_hwpmc, OID_AUTO, hashsize, CTLFLAG_TUN|CTLFLAG_RD,
259145256Sjkoshy    &pmc_hashsize, 0, "rows in hash tables");
260145256Sjkoshy
261145256Sjkoshy/*
262174395Sjkoshy * kern.hwpmc.nsamples --- number of PC samples/callchain stacks per CPU
263145256Sjkoshy */
264145256Sjkoshy
265147191Sjkoshystatic int pmc_nsamples = PMC_NSAMPLES;
266147191SjkoshyTUNABLE_INT(PMC_SYSCTL_NAME_PREFIX "nsamples", &pmc_nsamples);
267147191SjkoshySYSCTL_INT(_kern_hwpmc, OID_AUTO, nsamples, CTLFLAG_TUN|CTLFLAG_RD,
268147191Sjkoshy    &pmc_nsamples, 0, "number of PC samples per CPU");
269145256Sjkoshy
270174395Sjkoshy
271145256Sjkoshy/*
272147191Sjkoshy * kern.hwpmc.mtxpoolsize -- number of mutexes in the mutex pool.
273145256Sjkoshy */
274145256Sjkoshy
275145256Sjkoshystatic int pmc_mtxpool_size = PMC_MTXPOOL_SIZE;
276145256SjkoshyTUNABLE_INT(PMC_SYSCTL_NAME_PREFIX "mtxpoolsize", &pmc_mtxpool_size);
277145256SjkoshySYSCTL_INT(_kern_hwpmc, OID_AUTO, mtxpoolsize, CTLFLAG_TUN|CTLFLAG_RD,
278145256Sjkoshy    &pmc_mtxpool_size, 0, "size of spin mutex pool");
279145256Sjkoshy
280145256Sjkoshy
281145256Sjkoshy/*
282145256Sjkoshy * security.bsd.unprivileged_syspmcs -- allow non-root processes to
283145256Sjkoshy * allocate system-wide PMCs.
284145256Sjkoshy *
285145256Sjkoshy * Allowing unprivileged processes to allocate system PMCs is convenient
286145256Sjkoshy * if system-wide measurements need to be taken concurrently with other
287145256Sjkoshy * per-process measurements.  This feature is turned off by default.
288145256Sjkoshy */
289145256Sjkoshy
290145256Sjkoshystatic int pmc_unprivileged_syspmcs = 0;
291145256SjkoshyTUNABLE_INT("security.bsd.unprivileged_syspmcs", &pmc_unprivileged_syspmcs);
292145256SjkoshySYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_syspmcs, CTLFLAG_RW,
293145256Sjkoshy    &pmc_unprivileged_syspmcs, 0,
294145256Sjkoshy    "allow unprivileged process to allocate system PMCs");
295145256Sjkoshy
296147191Sjkoshy/*
297147191Sjkoshy * Hash function.  Discard the lower 2 bits of the pointer since
298147191Sjkoshy * these are always zero for our uses.  The hash multiplier is
299147191Sjkoshy * round((2^LONG_BIT) * ((sqrt(5)-1)/2)).
300147191Sjkoshy */
301145256Sjkoshy
302145256Sjkoshy#if	LONG_BIT == 64
303145256Sjkoshy#define	_PMC_HM		11400714819323198486u
304145256Sjkoshy#elif	LONG_BIT == 32
305145256Sjkoshy#define	_PMC_HM		2654435769u
306145256Sjkoshy#else
307145256Sjkoshy#error 	Must know the size of 'long' to compile
308145256Sjkoshy#endif
309145256Sjkoshy
310145256Sjkoshy#define	PMC_HASH_PTR(P,M)	((((unsigned long) (P) >> 2) * _PMC_HM) & (M))
311145256Sjkoshy
312145256Sjkoshy/*
313145256Sjkoshy * Syscall structures
314145256Sjkoshy */
315145256Sjkoshy
316145256Sjkoshy/* The `sysent' for the new syscall */
317145256Sjkoshystatic struct sysent pmc_sysent = {
318145256Sjkoshy	2,			/* sy_narg */
319145256Sjkoshy	pmc_syscall_handler	/* sy_call */
320145256Sjkoshy};
321145256Sjkoshy
322145256Sjkoshystatic struct syscall_module_data pmc_syscall_mod = {
323145256Sjkoshy	load,
324145256Sjkoshy	NULL,
325145256Sjkoshy	&pmc_syscall_num,
326145256Sjkoshy	&pmc_sysent,
327145256Sjkoshy	{ 0, NULL }
328145256Sjkoshy};
329145256Sjkoshy
330145256Sjkoshystatic moduledata_t pmc_mod = {
331145256Sjkoshy	PMC_MODULE_NAME,
332145256Sjkoshy	syscall_module_handler,
333145256Sjkoshy	&pmc_syscall_mod
334145256Sjkoshy};
335145256Sjkoshy
336145256SjkoshyDECLARE_MODULE(pmc, pmc_mod, SI_SUB_SMP, SI_ORDER_ANY);
337145256SjkoshyMODULE_VERSION(pmc, PMC_VERSION);
338145256Sjkoshy
339153110Sru#ifdef	DEBUG
340147191Sjkoshyenum pmc_dbgparse_state {
341147191Sjkoshy	PMCDS_WS,		/* in whitespace */
342147191Sjkoshy	PMCDS_MAJOR,		/* seen a major keyword */
343147191Sjkoshy	PMCDS_MINOR
344147191Sjkoshy};
345147191Sjkoshy
346145256Sjkoshystatic int
347145256Sjkoshypmc_debugflags_parse(char *newstr, char *fence)
348145256Sjkoshy{
349145313Sjkoshy	char c, *p, *q;
350147191Sjkoshy	struct pmc_debugflags *tmpflags;
351147191Sjkoshy	int error, found, *newbits, tmp;
352147191Sjkoshy	size_t kwlen;
353145256Sjkoshy
354184214Sdes	tmpflags = malloc(sizeof(*tmpflags), M_PMC, M_WAITOK|M_ZERO);
355145256Sjkoshy
356145256Sjkoshy	p = newstr;
357147191Sjkoshy	error = 0;
358145256Sjkoshy
359147191Sjkoshy	for (; p < fence && (c = *p); p++) {
360145256Sjkoshy
361147191Sjkoshy		/* skip white space */
362147191Sjkoshy		if (c == ' ' || c == '\t')
363147191Sjkoshy			continue;
364147191Sjkoshy
365147191Sjkoshy		/* look for a keyword followed by "=" */
366147191Sjkoshy		for (q = p; p < fence && (c = *p) && c != '='; p++)
367147191Sjkoshy			;
368147191Sjkoshy		if (c != '=') {
369147191Sjkoshy			error = EINVAL;
370147191Sjkoshy			goto done;
371145256Sjkoshy		}
372145256Sjkoshy
373147191Sjkoshy		kwlen = p - q;
374147191Sjkoshy		newbits = NULL;
375145256Sjkoshy
376147191Sjkoshy		/* lookup flag group name */
377147191Sjkoshy#define	DBG_SET_FLAG_MAJ(S,F)						\
378147191Sjkoshy		if (kwlen == sizeof(S)-1 && strncmp(q, S, kwlen) == 0)	\
379147191Sjkoshy			newbits = &tmpflags->pdb_ ## F;
380145256Sjkoshy
381147191Sjkoshy		DBG_SET_FLAG_MAJ("cpu",		CPU);
382147191Sjkoshy		DBG_SET_FLAG_MAJ("csw",		CSW);
383147191Sjkoshy		DBG_SET_FLAG_MAJ("logging",	LOG);
384147191Sjkoshy		DBG_SET_FLAG_MAJ("module",	MOD);
385147191Sjkoshy		DBG_SET_FLAG_MAJ("md", 		MDP);
386147191Sjkoshy		DBG_SET_FLAG_MAJ("owner",	OWN);
387147191Sjkoshy		DBG_SET_FLAG_MAJ("pmc",		PMC);
388147191Sjkoshy		DBG_SET_FLAG_MAJ("process",	PRC);
389147191Sjkoshy		DBG_SET_FLAG_MAJ("sampling", 	SAM);
390145256Sjkoshy
391147191Sjkoshy		if (newbits == NULL) {
392147191Sjkoshy			error = EINVAL;
393147191Sjkoshy			goto done;
394145256Sjkoshy		}
395145256Sjkoshy
396147191Sjkoshy		p++;		/* skip the '=' */
397145256Sjkoshy
398147191Sjkoshy		/* Now parse the individual flags */
399147191Sjkoshy		tmp = 0;
400147191Sjkoshy	newflag:
401147191Sjkoshy		for (q = p; p < fence && (c = *p); p++)
402147191Sjkoshy			if (c == ' ' || c == '\t' || c == ',')
403147191Sjkoshy				break;
404147191Sjkoshy
405147191Sjkoshy		/* p == fence or c == ws or c == "," or c == 0 */
406147191Sjkoshy
407147191Sjkoshy		if ((kwlen = p - q) == 0) {
408147191Sjkoshy			*newbits = tmp;
409147191Sjkoshy			continue;
410147191Sjkoshy		}
411147191Sjkoshy
412147191Sjkoshy		found = 0;
413147191Sjkoshy#define	DBG_SET_FLAG_MIN(S,F)						\
414147191Sjkoshy		if (kwlen == sizeof(S)-1 && strncmp(q, S, kwlen) == 0)	\
415147191Sjkoshy			tmp |= found = (1 << PMC_DEBUG_MIN_ ## F)
416147191Sjkoshy
417147191Sjkoshy		/* a '*' denotes all possible flags in the group */
418147191Sjkoshy		if (kwlen == 1 && *q == '*')
419147191Sjkoshy			tmp = found = ~0;
420147191Sjkoshy		/* look for individual flag names */
421147191Sjkoshy		DBG_SET_FLAG_MIN("allocaterow", ALR);
422147191Sjkoshy		DBG_SET_FLAG_MIN("allocate",	ALL);
423147191Sjkoshy		DBG_SET_FLAG_MIN("attach",	ATT);
424147191Sjkoshy		DBG_SET_FLAG_MIN("bind",	BND);
425147191Sjkoshy		DBG_SET_FLAG_MIN("config",	CFG);
426147191Sjkoshy		DBG_SET_FLAG_MIN("exec",	EXC);
427147191Sjkoshy		DBG_SET_FLAG_MIN("exit",	EXT);
428147191Sjkoshy		DBG_SET_FLAG_MIN("find",	FND);
429147191Sjkoshy		DBG_SET_FLAG_MIN("flush",	FLS);
430147191Sjkoshy		DBG_SET_FLAG_MIN("fork",	FRK);
431147191Sjkoshy		DBG_SET_FLAG_MIN("getbuf",	GTB);
432147191Sjkoshy		DBG_SET_FLAG_MIN("hook",	PMH);
433147191Sjkoshy		DBG_SET_FLAG_MIN("init",	INI);
434147191Sjkoshy		DBG_SET_FLAG_MIN("intr",	INT);
435147191Sjkoshy		DBG_SET_FLAG_MIN("linktarget",	TLK);
436147191Sjkoshy		DBG_SET_FLAG_MIN("mayberemove", OMR);
437147191Sjkoshy		DBG_SET_FLAG_MIN("ops",		OPS);
438147191Sjkoshy		DBG_SET_FLAG_MIN("read",	REA);
439147191Sjkoshy		DBG_SET_FLAG_MIN("register",	REG);
440147191Sjkoshy		DBG_SET_FLAG_MIN("release",	REL);
441147191Sjkoshy		DBG_SET_FLAG_MIN("remove",	ORM);
442147191Sjkoshy		DBG_SET_FLAG_MIN("sample",	SAM);
443147191Sjkoshy		DBG_SET_FLAG_MIN("scheduleio",	SIO);
444147191Sjkoshy		DBG_SET_FLAG_MIN("select",	SEL);
445147191Sjkoshy		DBG_SET_FLAG_MIN("signal",	SIG);
446147191Sjkoshy		DBG_SET_FLAG_MIN("swi",		SWI);
447147191Sjkoshy		DBG_SET_FLAG_MIN("swo",		SWO);
448147191Sjkoshy		DBG_SET_FLAG_MIN("start",	STA);
449147191Sjkoshy		DBG_SET_FLAG_MIN("stop",	STO);
450147191Sjkoshy		DBG_SET_FLAG_MIN("syscall",	PMS);
451147191Sjkoshy		DBG_SET_FLAG_MIN("unlinktarget", TUL);
452147191Sjkoshy		DBG_SET_FLAG_MIN("write",	WRI);
453147191Sjkoshy		if (found == 0) {
454147191Sjkoshy			/* unrecognized flag name */
455147191Sjkoshy			error = EINVAL;
456147191Sjkoshy			goto done;
457147191Sjkoshy		}
458147191Sjkoshy
459147191Sjkoshy		if (c == 0 || c == ' ' || c == '\t') {	/* end of flag group */
460147191Sjkoshy			*newbits = tmp;
461147191Sjkoshy			continue;
462147191Sjkoshy		}
463147191Sjkoshy
464147191Sjkoshy		p++;
465147191Sjkoshy		goto newflag;
466145256Sjkoshy	}
467145256Sjkoshy
468147191Sjkoshy	/* save the new flag set */
469147191Sjkoshy	bcopy(tmpflags, &pmc_debugflags, sizeof(pmc_debugflags));
470145256Sjkoshy
471147191Sjkoshy done:
472184205Sdes	free(tmpflags, M_PMC);
473147191Sjkoshy	return error;
474145256Sjkoshy}
475145256Sjkoshy
476145256Sjkoshystatic int
477145256Sjkoshypmc_debugflags_sysctl_handler(SYSCTL_HANDLER_ARGS)
478145256Sjkoshy{
479145256Sjkoshy	char *fence, *newstr;
480145256Sjkoshy	int error;
481145256Sjkoshy	unsigned int n;
482145256Sjkoshy
483145256Sjkoshy	(void) arg1; (void) arg2; /* unused parameters */
484145256Sjkoshy
485145256Sjkoshy	n = sizeof(pmc_debugstr);
486184802Sjkoshy	newstr = malloc(n, M_PMC, M_WAITOK|M_ZERO);
487147191Sjkoshy	(void) strlcpy(newstr, pmc_debugstr, n);
488145256Sjkoshy
489145256Sjkoshy	error = sysctl_handle_string(oidp, newstr, n, req);
490145256Sjkoshy
491145256Sjkoshy	/* if there is a new string, parse and copy it */
492145256Sjkoshy	if (error == 0 && req->newptr != NULL) {
493147191Sjkoshy		fence = newstr + (n < req->newlen ? n : req->newlen + 1);
494145256Sjkoshy		if ((error = pmc_debugflags_parse(newstr, fence)) == 0)
495145256Sjkoshy			(void) strlcpy(pmc_debugstr, newstr,
496145256Sjkoshy			    sizeof(pmc_debugstr));
497145256Sjkoshy	}
498145256Sjkoshy
499184205Sdes	free(newstr, M_PMC);
500145256Sjkoshy
501145256Sjkoshy	return error;
502145256Sjkoshy}
503145256Sjkoshy#endif
504145256Sjkoshy
505145256Sjkoshy/*
506184802Sjkoshy * Map a row index to a classdep structure and return the adjusted row
507184802Sjkoshy * index for the PMC class index.
508184802Sjkoshy */
509184802Sjkoshystatic struct pmc_classdep *
510184802Sjkoshypmc_ri_to_classdep(struct pmc_mdep *md, int ri, int *adjri)
511184802Sjkoshy{
512184802Sjkoshy	struct pmc_classdep *pcd;
513184802Sjkoshy
514184802Sjkoshy	(void) md;
515184802Sjkoshy
516184802Sjkoshy	KASSERT(ri >= 0 && ri < md->pmd_npmc,
517184802Sjkoshy	    ("[pmc,%d] illegal row-index %d", __LINE__, ri));
518184802Sjkoshy
519184802Sjkoshy	pcd = pmc_rowindex_to_classdep[ri];
520184802Sjkoshy
521184802Sjkoshy	KASSERT(pcd != NULL,
522198204Srpaulo	    ("[pmc,%d] ri %d null pcd", __LINE__, ri));
523184802Sjkoshy
524184802Sjkoshy	*adjri = ri - pcd->pcd_ri;
525184802Sjkoshy
526184802Sjkoshy	KASSERT(*adjri >= 0 && *adjri < pcd->pcd_num,
527184802Sjkoshy	    ("[pmc,%d] adjusted row-index %d", __LINE__, *adjri));
528184802Sjkoshy
529184802Sjkoshy	return (pcd);
530184802Sjkoshy}
531184802Sjkoshy
532184802Sjkoshy/*
533145256Sjkoshy * Concurrency Control
534145256Sjkoshy *
535145256Sjkoshy * The driver manages the following data structures:
536145256Sjkoshy *
537145256Sjkoshy *   - target process descriptors, one per target process
538145256Sjkoshy *   - owner process descriptors (and attached lists), one per owner process
539145256Sjkoshy *   - lookup hash tables for owner and target processes
540145256Sjkoshy *   - PMC descriptors (and attached lists)
541145256Sjkoshy *   - per-cpu hardware state
542145256Sjkoshy *   - the 'hook' variable through which the kernel calls into
543145256Sjkoshy *     this module
544145256Sjkoshy *   - the machine hardware state (managed by the MD layer)
545145256Sjkoshy *
546145256Sjkoshy * These data structures are accessed from:
547145256Sjkoshy *
548145256Sjkoshy * - thread context-switch code
549145256Sjkoshy * - interrupt handlers (possibly on multiple cpus)
550145256Sjkoshy * - kernel threads on multiple cpus running on behalf of user
551145256Sjkoshy *   processes doing system calls
552145256Sjkoshy * - this driver's private kernel threads
553145256Sjkoshy *
554145256Sjkoshy * = Locks and Locking strategy =
555145256Sjkoshy *
556145256Sjkoshy * The driver uses four locking strategies for its operation:
557145256Sjkoshy *
558168856Sjkoshy * - The global SX lock "pmc_sx" is used to protect internal
559168856Sjkoshy *   data structures.
560145256Sjkoshy *
561168856Sjkoshy *   Calls into the module by syscall() start with this lock being
562168856Sjkoshy *   held in exclusive mode.  Depending on the requested operation,
563168856Sjkoshy *   the lock may be downgraded to 'shared' mode to allow more
564168856Sjkoshy *   concurrent readers into the module.  Calls into the module from
565168856Sjkoshy *   other parts of the kernel acquire the lock in shared mode.
566145256Sjkoshy *
567145256Sjkoshy *   This SX lock is held in exclusive mode for any operations that
568145256Sjkoshy *   modify the linkages between the driver's internal data structures.
569145256Sjkoshy *
570145256Sjkoshy *   The 'pmc_hook' function pointer is also protected by this lock.
571145256Sjkoshy *   It is only examined with the sx lock held in exclusive mode.  The
572168856Sjkoshy *   kernel module is allowed to be unloaded only with the sx lock held
573168856Sjkoshy *   in exclusive mode.  In normal syscall handling, after acquiring the
574168856Sjkoshy *   pmc_sx lock we first check that 'pmc_hook' is non-null before
575168856Sjkoshy *   proceeding.  This prevents races between the thread unloading the module
576168856Sjkoshy *   and other threads seeking to use the module.
577145256Sjkoshy *
578145256Sjkoshy * - Lookups of target process structures and owner process structures
579145256Sjkoshy *   cannot use the global "pmc_sx" SX lock because these lookups need
580145256Sjkoshy *   to happen during context switches and in other critical sections
581145256Sjkoshy *   where sleeping is not allowed.  We protect these lookup tables
582145256Sjkoshy *   with their own private spin-mutexes, "pmc_processhash_mtx" and
583168856Sjkoshy *   "pmc_ownerhash_mtx".
584145256Sjkoshy *
585145256Sjkoshy * - Interrupt handlers work in a lock free manner.  At interrupt
586145256Sjkoshy *   time, handlers look at the PMC pointer (phw->phw_pmc) configured
587145256Sjkoshy *   when the PMC was started.  If this pointer is NULL, the interrupt
588145256Sjkoshy *   is ignored after updating driver statistics.  We ensure that this
589145256Sjkoshy *   pointer is set (using an atomic operation if necessary) before the
590145256Sjkoshy *   PMC hardware is started.  Conversely, this pointer is unset atomically
591145256Sjkoshy *   only after the PMC hardware is stopped.
592145256Sjkoshy *
593145256Sjkoshy *   We ensure that everything needed for the operation of an
594145256Sjkoshy *   interrupt handler is available without it needing to acquire any
595145256Sjkoshy *   locks.  We also ensure that a PMC's software state is destroyed only
596145256Sjkoshy *   after the PMC is taken off hardware (on all CPUs).
597145256Sjkoshy *
598145256Sjkoshy * - Context-switch handling with process-private PMCs needs more
599145256Sjkoshy *   care.
600145256Sjkoshy *
601145256Sjkoshy *   A given process may be the target of multiple PMCs.  For example,
602145256Sjkoshy *   PMCATTACH and PMCDETACH may be requested by a process on one CPU
603145256Sjkoshy *   while the target process is running on another.  A PMC could also
604145256Sjkoshy *   be getting released because its owner is exiting.  We tackle
605145256Sjkoshy *   these situations in the following manner:
606145256Sjkoshy *
607145256Sjkoshy *   - each target process structure 'pmc_process' has an array
608145256Sjkoshy *     of 'struct pmc *' pointers, one for each hardware PMC.
609145256Sjkoshy *
610145256Sjkoshy *   - At context switch IN time, each "target" PMC in RUNNING state
611145256Sjkoshy *     gets started on hardware and a pointer to each PMC is copied into
612145256Sjkoshy *     the per-cpu phw array.  The 'runcount' for the PMC is
613145256Sjkoshy *     incremented.
614145256Sjkoshy *
615145256Sjkoshy *   - At context switch OUT time, all process-virtual PMCs are stopped
616145256Sjkoshy *     on hardware.  The saved value is added to the PMCs value field
617145256Sjkoshy *     only if the PMC is in a non-deleted state (the PMCs state could
618145256Sjkoshy *     have changed during the current time slice).
619145256Sjkoshy *
620145256Sjkoshy *     Note that since in-between a switch IN on a processor and a switch
621145256Sjkoshy *     OUT, the PMC could have been released on another CPU.  Therefore
622145256Sjkoshy *     context switch OUT always looks at the hardware state to turn
623145256Sjkoshy *     OFF PMCs and will update a PMC's saved value only if reachable
624145256Sjkoshy *     from the target process record.
625145256Sjkoshy *
626145256Sjkoshy *   - OP PMCRELEASE could be called on a PMC at any time (the PMC could
627145256Sjkoshy *     be attached to many processes at the time of the call and could
628145256Sjkoshy *     be active on multiple CPUs).
629145256Sjkoshy *
630145256Sjkoshy *     We prevent further scheduling of the PMC by marking it as in
631145256Sjkoshy *     state 'DELETED'.  If the runcount of the PMC is non-zero then
632145256Sjkoshy *     this PMC is currently running on a CPU somewhere.  The thread
633167086Sjhb *     doing the PMCRELEASE operation waits by repeatedly doing a
634167086Sjhb *     pause() till the runcount comes to zero.
635145256Sjkoshy *
636168856Sjkoshy * The contents of a PMC descriptor (struct pmc) are protected using
637168856Sjkoshy * a spin-mutex.  In order to save space, we use a mutex pool.
638168856Sjkoshy *
639168856Sjkoshy * In terms of lock types used by witness(4), we use:
640168856Sjkoshy * - Type "pmc-sx", used by the global SX lock.
641168856Sjkoshy * - Type "pmc-sleep", for sleep mutexes used by logger threads.
642168856Sjkoshy * - Type "pmc-per-proc", for protecting PMC owner descriptors.
643168856Sjkoshy * - Type "pmc-leaf", used for all other spin mutexes.
644145256Sjkoshy */
645145256Sjkoshy
646145256Sjkoshy/*
647145256Sjkoshy * save the cpu binding of the current kthread
648145256Sjkoshy */
649145256Sjkoshy
650145256Sjkoshystatic void
651145256Sjkoshypmc_save_cpu_binding(struct pmc_binding *pb)
652145256Sjkoshy{
653145256Sjkoshy	PMCDBG(CPU,BND,2, "%s", "save-cpu");
654170307Sjeff	thread_lock(curthread);
655145256Sjkoshy	pb->pb_bound = sched_is_bound(curthread);
656145256Sjkoshy	pb->pb_cpu   = curthread->td_oncpu;
657170307Sjeff	thread_unlock(curthread);
658145256Sjkoshy	PMCDBG(CPU,BND,2, "save-cpu cpu=%d", pb->pb_cpu);
659145256Sjkoshy}
660145256Sjkoshy
661145256Sjkoshy/*
662145256Sjkoshy * restore the cpu binding of the current thread
663145256Sjkoshy */
664145256Sjkoshy
665145256Sjkoshystatic void
666145256Sjkoshypmc_restore_cpu_binding(struct pmc_binding *pb)
667145256Sjkoshy{
668145256Sjkoshy	PMCDBG(CPU,BND,2, "restore-cpu curcpu=%d restore=%d",
669145256Sjkoshy	    curthread->td_oncpu, pb->pb_cpu);
670170307Sjeff	thread_lock(curthread);
671145256Sjkoshy	if (pb->pb_bound)
672145256Sjkoshy		sched_bind(curthread, pb->pb_cpu);
673145256Sjkoshy	else
674145256Sjkoshy		sched_unbind(curthread);
675170307Sjeff	thread_unlock(curthread);
676145256Sjkoshy	PMCDBG(CPU,BND,2, "%s", "restore-cpu done");
677145256Sjkoshy}
678145256Sjkoshy
679145256Sjkoshy/*
680145256Sjkoshy * move execution over the specified cpu and bind it there.
681145256Sjkoshy */
682145256Sjkoshy
683145256Sjkoshystatic void
684145256Sjkoshypmc_select_cpu(int cpu)
685145256Sjkoshy{
686183266Sjkoshy	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
687145256Sjkoshy	    ("[pmc,%d] bad cpu number %d", __LINE__, cpu));
688145256Sjkoshy
689183266Sjkoshy	/* Never move to an inactive CPU. */
690183266Sjkoshy	KASSERT(pmc_cpu_is_active(cpu), ("[pmc,%d] selecting inactive "
691183266Sjkoshy	    "CPU %d", __LINE__, cpu));
692145256Sjkoshy
693145256Sjkoshy	PMCDBG(CPU,SEL,2, "select-cpu cpu=%d", cpu);
694170307Sjeff	thread_lock(curthread);
695145256Sjkoshy	sched_bind(curthread, cpu);
696170307Sjeff	thread_unlock(curthread);
697145256Sjkoshy
698145256Sjkoshy	KASSERT(curthread->td_oncpu == cpu,
699145256Sjkoshy	    ("[pmc,%d] CPU not bound [cpu=%d, curr=%d]", __LINE__,
700145256Sjkoshy		cpu, curthread->td_oncpu));
701145256Sjkoshy
702145256Sjkoshy	PMCDBG(CPU,SEL,2, "select-cpu cpu=%d ok", cpu);
703145256Sjkoshy}
704145256Sjkoshy
705145256Sjkoshy/*
706145774Sjkoshy * Force a context switch.
707145774Sjkoshy *
708167086Sjhb * We do this by pause'ing for 1 tick -- invoking mi_switch() is not
709145774Sjkoshy * guaranteed to force a context switch.
710145774Sjkoshy */
711145774Sjkoshy
712145774Sjkoshystatic void
713145774Sjkoshypmc_force_context_switch(void)
714145774Sjkoshy{
715145774Sjkoshy
716167086Sjhb	pause("pmcctx", 1);
717145774Sjkoshy}
718145774Sjkoshy
719145774Sjkoshy/*
720147191Sjkoshy * Get the file name for an executable.  This is a simple wrapper
721147191Sjkoshy * around vn_fullpath(9).
722145256Sjkoshy */
723145256Sjkoshy
724147191Sjkoshystatic void
725147708Sjkoshypmc_getfilename(struct vnode *v, char **fullpath, char **freepath)
726145256Sjkoshy{
727145256Sjkoshy
728147191Sjkoshy	*fullpath = "unknown";
729147191Sjkoshy	*freepath = NULL;
730175294Sattilio	vn_fullpath(curthread, v, fullpath, freepath);
731145256Sjkoshy}
732145256Sjkoshy
733145256Sjkoshy/*
734145256Sjkoshy * remove an process owning PMCs
735145256Sjkoshy */
736145256Sjkoshy
737145256Sjkoshyvoid
738145256Sjkoshypmc_remove_owner(struct pmc_owner *po)
739145256Sjkoshy{
740147191Sjkoshy	struct pmc *pm, *tmp;
741145256Sjkoshy
742145256Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
743145256Sjkoshy
744145256Sjkoshy	PMCDBG(OWN,ORM,1, "remove-owner po=%p", po);
745145256Sjkoshy
746145256Sjkoshy	/* Remove descriptor from the owner hash table */
747145256Sjkoshy	LIST_REMOVE(po, po_next);
748145256Sjkoshy
749147191Sjkoshy	/* release all owned PMC descriptors */
750147191Sjkoshy	LIST_FOREACH_SAFE(pm, &po->po_pmcs, pm_next, tmp) {
751147191Sjkoshy		PMCDBG(OWN,ORM,2, "pmc=%p", pm);
752147191Sjkoshy		KASSERT(pm->pm_owner == po,
753147191Sjkoshy		    ("[pmc,%d] owner %p != po %p", __LINE__, pm->pm_owner, po));
754145256Sjkoshy
755147191Sjkoshy		pmc_release_pmc_descriptor(pm);	/* will unlink from the list */
756145256Sjkoshy	}
757145256Sjkoshy
758147191Sjkoshy	KASSERT(po->po_sscount == 0,
759147191Sjkoshy	    ("[pmc,%d] SS count not zero", __LINE__));
760145256Sjkoshy	KASSERT(LIST_EMPTY(&po->po_pmcs),
761147191Sjkoshy	    ("[pmc,%d] PMC list not empty", __LINE__));
762145256Sjkoshy
763147191Sjkoshy	/* de-configure the log file if present */
764145774Sjkoshy	if (po->po_flags & PMC_PO_OWNS_LOGFILE)
765147191Sjkoshy		pmclog_deconfigure_log(po);
766145256Sjkoshy}
767145256Sjkoshy
768145256Sjkoshy/*
769145256Sjkoshy * remove an owner process record if all conditions are met.
770145256Sjkoshy */
771145256Sjkoshy
772145256Sjkoshystatic void
773145256Sjkoshypmc_maybe_remove_owner(struct pmc_owner *po)
774145256Sjkoshy{
775145256Sjkoshy
776145256Sjkoshy	PMCDBG(OWN,OMR,1, "maybe-remove-owner po=%p", po);
777145256Sjkoshy
778145256Sjkoshy	/*
779145256Sjkoshy	 * Remove owner record if
780145256Sjkoshy	 * - this process does not own any PMCs
781145256Sjkoshy	 * - this process has not allocated a system-wide sampling buffer
782145256Sjkoshy	 */
783145256Sjkoshy
784145256Sjkoshy	if (LIST_EMPTY(&po->po_pmcs) &&
785145774Sjkoshy	    ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0)) {
786145256Sjkoshy		pmc_remove_owner(po);
787147191Sjkoshy		pmc_destroy_owner_descriptor(po);
788145256Sjkoshy	}
789145256Sjkoshy}
790145256Sjkoshy
791145256Sjkoshy/*
792145256Sjkoshy * Add an association between a target process and a PMC.
793145256Sjkoshy */
794145256Sjkoshy
795145256Sjkoshystatic void
796145256Sjkoshypmc_link_target_process(struct pmc *pm, struct pmc_process *pp)
797145256Sjkoshy{
798145256Sjkoshy	int ri;
799145256Sjkoshy	struct pmc_target *pt;
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));
805147191Sjkoshy	KASSERT(PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm)),
806147191Sjkoshy	    ("[pmc,%d] Attaching a non-process-virtual pmc=%p to pid=%d",
807147191Sjkoshy		__LINE__, pm, pp->pp_proc->p_pid));
808198343Sfabient	KASSERT(pp->pp_refcnt >= 0 && pp->pp_refcnt <= ((int) md->pmd_npmc - 1),
809145256Sjkoshy	    ("[pmc,%d] Illegal reference count %d for process record %p",
810145256Sjkoshy		__LINE__, pp->pp_refcnt, (void *) pp));
811145256Sjkoshy
812145774Sjkoshy	ri = PMC_TO_ROWINDEX(pm);
813145256Sjkoshy
814145256Sjkoshy	PMCDBG(PRC,TLK,1, "link-target pmc=%p ri=%d pmc-process=%p",
815145256Sjkoshy	    pm, ri, pp);
816145256Sjkoshy
817153110Sru#ifdef	DEBUG
818145256Sjkoshy	LIST_FOREACH(pt, &pm->pm_targets, pt_next)
819145256Sjkoshy	    if (pt->pt_process == pp)
820145256Sjkoshy		    KASSERT(0, ("[pmc,%d] pp %p already in pmc %p targets",
821145256Sjkoshy				__LINE__, pp, pm));
822145256Sjkoshy#endif
823145256Sjkoshy
824184802Sjkoshy	pt = malloc(sizeof(struct pmc_target), M_PMC, M_WAITOK|M_ZERO);
825145256Sjkoshy	pt->pt_process = pp;
826145256Sjkoshy
827145256Sjkoshy	LIST_INSERT_HEAD(&pm->pm_targets, pt, pt_next);
828145256Sjkoshy
829148067Sjhb	atomic_store_rel_ptr((uintptr_t *)&pp->pp_pmcs[ri].pp_pmc,
830148067Sjhb	    (uintptr_t)pm);
831145256Sjkoshy
832145615Sjkoshy	if (pm->pm_owner->po_owner == pp->pp_proc)
833145774Sjkoshy		pm->pm_flags |= PMC_F_ATTACHED_TO_OWNER;
834145615Sjkoshy
835147191Sjkoshy	/*
836147191Sjkoshy	 * Initialize the per-process values at this row index.
837147191Sjkoshy	 */
838147191Sjkoshy	pp->pp_pmcs[ri].pp_pmcval = PMC_TO_MODE(pm) == PMC_MODE_TS ?
839147191Sjkoshy	    pm->pm_sc.pm_reloadcount : 0;
840147191Sjkoshy
841145256Sjkoshy	pp->pp_refcnt++;
842145256Sjkoshy
843145256Sjkoshy}
844145256Sjkoshy
845145256Sjkoshy/*
846145256Sjkoshy * Removes the association between a target process and a PMC.
847145256Sjkoshy */
848145256Sjkoshy
849145256Sjkoshystatic void
850145256Sjkoshypmc_unlink_target_process(struct pmc *pm, struct pmc_process *pp)
851145256Sjkoshy{
852145256Sjkoshy	int ri;
853147191Sjkoshy	struct proc *p;
854145256Sjkoshy	struct pmc_target *ptgt;
855145256Sjkoshy
856145256Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
857145256Sjkoshy
858145256Sjkoshy	KASSERT(pm != NULL && pp != NULL,
859145256Sjkoshy	    ("[pmc,%d] Null pm %p or pp %p", __LINE__, pm, pp));
860145256Sjkoshy
861198343Sfabient	KASSERT(pp->pp_refcnt >= 1 && pp->pp_refcnt <= (int) md->pmd_npmc,
862145256Sjkoshy	    ("[pmc,%d] Illegal ref count %d on process record %p",
863145256Sjkoshy		__LINE__, pp->pp_refcnt, (void *) pp));
864145256Sjkoshy
865145774Sjkoshy	ri = PMC_TO_ROWINDEX(pm);
866145256Sjkoshy
867145256Sjkoshy	PMCDBG(PRC,TUL,1, "unlink-target pmc=%p ri=%d pmc-process=%p",
868145256Sjkoshy	    pm, ri, pp);
869145256Sjkoshy
870145256Sjkoshy	KASSERT(pp->pp_pmcs[ri].pp_pmc == pm,
871145256Sjkoshy	    ("[pmc,%d] PMC ri %d mismatch pmc %p pp->[ri] %p", __LINE__,
872145256Sjkoshy		ri, pm, pp->pp_pmcs[ri].pp_pmc));
873145256Sjkoshy
874145256Sjkoshy	pp->pp_pmcs[ri].pp_pmc = NULL;
875145256Sjkoshy	pp->pp_pmcs[ri].pp_pmcval = (pmc_value_t) 0;
876145256Sjkoshy
877145774Sjkoshy	/* Remove owner-specific flags */
878145774Sjkoshy	if (pm->pm_owner->po_owner == pp->pp_proc) {
879145774Sjkoshy		pp->pp_flags &= ~PMC_PP_ENABLE_MSR_ACCESS;
880145774Sjkoshy		pm->pm_flags &= ~PMC_F_ATTACHED_TO_OWNER;
881145774Sjkoshy	}
882145615Sjkoshy
883145256Sjkoshy	pp->pp_refcnt--;
884145256Sjkoshy
885145256Sjkoshy	/* Remove the target process from the PMC structure */
886145256Sjkoshy	LIST_FOREACH(ptgt, &pm->pm_targets, pt_next)
887145256Sjkoshy		if (ptgt->pt_process == pp)
888145256Sjkoshy			break;
889145256Sjkoshy
890145256Sjkoshy	KASSERT(ptgt != NULL, ("[pmc,%d] process %p (pp: %p) not found "
891145256Sjkoshy		    "in pmc %p", __LINE__, pp->pp_proc, pp, pm));
892145256Sjkoshy
893145256Sjkoshy	LIST_REMOVE(ptgt, pt_next);
894184205Sdes	free(ptgt, M_PMC);
895145256Sjkoshy
896147191Sjkoshy	/* if the PMC now lacks targets, send the owner a SIGIO */
897147191Sjkoshy	if (LIST_EMPTY(&pm->pm_targets)) {
898147191Sjkoshy		p = pm->pm_owner->po_owner;
899147191Sjkoshy		PROC_LOCK(p);
900225617Skmacy		kern_psignal(p, SIGIO);
901147191Sjkoshy		PROC_UNLOCK(p);
902145256Sjkoshy
903147191Sjkoshy		PMCDBG(PRC,SIG,2, "signalling proc=%p signal=%d", p,
904147191Sjkoshy		    SIGIO);
905145256Sjkoshy	}
906145256Sjkoshy}
907145256Sjkoshy
908145256Sjkoshy/*
909145256Sjkoshy * Check if PMC 'pm' may be attached to target process 't'.
910145256Sjkoshy */
911145256Sjkoshy
912145256Sjkoshystatic int
913145256Sjkoshypmc_can_attach(struct pmc *pm, struct proc *t)
914145256Sjkoshy{
915145256Sjkoshy	struct proc *o;		/* pmc owner */
916145256Sjkoshy	struct ucred *oc, *tc;	/* owner, target credentials */
917145256Sjkoshy	int decline_attach, i;
918145256Sjkoshy
919145256Sjkoshy	/*
920145256Sjkoshy	 * A PMC's owner can always attach that PMC to itself.
921145256Sjkoshy	 */
922145256Sjkoshy
923145256Sjkoshy	if ((o = pm->pm_owner->po_owner) == t)
924145256Sjkoshy		return 0;
925145256Sjkoshy
926145256Sjkoshy	PROC_LOCK(o);
927145256Sjkoshy	oc = o->p_ucred;
928145256Sjkoshy	crhold(oc);
929145256Sjkoshy	PROC_UNLOCK(o);
930145256Sjkoshy
931145256Sjkoshy	PROC_LOCK(t);
932145256Sjkoshy	tc = t->p_ucred;
933145256Sjkoshy	crhold(tc);
934145256Sjkoshy	PROC_UNLOCK(t);
935145256Sjkoshy
936145256Sjkoshy	/*
937145256Sjkoshy	 * The effective uid of the PMC owner should match at least one
938145256Sjkoshy	 * of the {effective,real,saved} uids of the target process.
939145256Sjkoshy	 */
940145256Sjkoshy
941145256Sjkoshy	decline_attach = oc->cr_uid != tc->cr_uid &&
942145256Sjkoshy	    oc->cr_uid != tc->cr_svuid &&
943145256Sjkoshy	    oc->cr_uid != tc->cr_ruid;
944145256Sjkoshy
945145256Sjkoshy	/*
946145256Sjkoshy	 * Every one of the target's group ids, must be in the owner's
947145256Sjkoshy	 * group list.
948145256Sjkoshy	 */
949145256Sjkoshy	for (i = 0; !decline_attach && i < tc->cr_ngroups; i++)
950145256Sjkoshy		decline_attach = !groupmember(tc->cr_groups[i], oc);
951145256Sjkoshy
952145256Sjkoshy	/* check the read and saved gids too */
953145256Sjkoshy	if (decline_attach == 0)
954145256Sjkoshy		decline_attach = !groupmember(tc->cr_rgid, oc) ||
955145256Sjkoshy		    !groupmember(tc->cr_svgid, oc);
956145256Sjkoshy
957145256Sjkoshy	crfree(tc);
958145256Sjkoshy	crfree(oc);
959145256Sjkoshy
960145256Sjkoshy	return !decline_attach;
961145256Sjkoshy}
962145256Sjkoshy
963145256Sjkoshy/*
964145256Sjkoshy * Attach a process to a PMC.
965145256Sjkoshy */
966145256Sjkoshy
967145256Sjkoshystatic int
968145256Sjkoshypmc_attach_one_process(struct proc *p, struct pmc *pm)
969145256Sjkoshy{
970145256Sjkoshy	int ri;
971147191Sjkoshy	char *fullpath, *freepath;
972145256Sjkoshy	struct pmc_process	*pp;
973145256Sjkoshy
974145256Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
975145256Sjkoshy
976145256Sjkoshy	PMCDBG(PRC,ATT,2, "attach-one pm=%p ri=%d proc=%p (%d, %s)", pm,
977145774Sjkoshy	    PMC_TO_ROWINDEX(pm), p, p->p_pid, p->p_comm);
978145256Sjkoshy
979145256Sjkoshy	/*
980145256Sjkoshy	 * Locate the process descriptor corresponding to process 'p',
981145256Sjkoshy	 * allocating space as needed.
982145256Sjkoshy	 *
983145256Sjkoshy	 * Verify that rowindex 'pm_rowindex' is free in the process
984145256Sjkoshy	 * descriptor.
985145256Sjkoshy	 *
986145256Sjkoshy	 * If not, allocate space for a descriptor and link the
987145256Sjkoshy	 * process descriptor and PMC.
988145256Sjkoshy	 */
989145774Sjkoshy	ri = PMC_TO_ROWINDEX(pm);
990145256Sjkoshy
991145256Sjkoshy	if ((pp = pmc_find_process_descriptor(p, PMC_FLAG_ALLOCATE)) == NULL)
992145256Sjkoshy		return ENOMEM;
993145256Sjkoshy
994145256Sjkoshy	if (pp->pp_pmcs[ri].pp_pmc == pm) /* already present at slot [ri] */
995145256Sjkoshy		return EEXIST;
996145256Sjkoshy
997145256Sjkoshy	if (pp->pp_pmcs[ri].pp_pmc != NULL)
998145256Sjkoshy		return EBUSY;
999145256Sjkoshy
1000145256Sjkoshy	pmc_link_target_process(pm, pp);
1001145256Sjkoshy
1002147191Sjkoshy	if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)) &&
1003147191Sjkoshy	    (pm->pm_flags & PMC_F_ATTACHED_TO_OWNER) == 0)
1004147191Sjkoshy		pm->pm_flags |= PMC_F_NEEDS_LOGFILE;
1005147191Sjkoshy
1006147191Sjkoshy	pm->pm_flags |= PMC_F_ATTACH_DONE; /* mark as attached */
1007147191Sjkoshy
1008147191Sjkoshy	/* issue an attach event to a configured log file */
1009147191Sjkoshy	if (pm->pm_owner->po_flags & PMC_PO_OWNS_LOGFILE) {
1010147708Sjkoshy		pmc_getfilename(p->p_textvp, &fullpath, &freepath);
1011180794Sjeff		if (p->p_flag & P_KTHREAD) {
1012180794Sjeff			fullpath = kernelname;
1013180794Sjeff			freepath = NULL;
1014180794Sjeff		} else
1015180794Sjeff			pmclog_process_pmcattach(pm, p->p_pid, fullpath);
1016147191Sjkoshy		if (freepath)
1017184205Sdes			free(freepath, M_TEMP);
1018174395Sjkoshy		if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
1019174395Sjkoshy			pmc_log_process_mappings(pm->pm_owner, p);
1020147191Sjkoshy	}
1021145256Sjkoshy	/* mark process as using HWPMCs */
1022145256Sjkoshy	PROC_LOCK(p);
1023145256Sjkoshy	p->p_flag |= P_HWPMC;
1024145256Sjkoshy	PROC_UNLOCK(p);
1025145256Sjkoshy
1026145256Sjkoshy	return 0;
1027145256Sjkoshy}
1028145256Sjkoshy
1029145256Sjkoshy/*
1030145256Sjkoshy * Attach a process and optionally its children
1031145256Sjkoshy */
1032145256Sjkoshy
1033145256Sjkoshystatic int
1034145256Sjkoshypmc_attach_process(struct proc *p, struct pmc *pm)
1035145256Sjkoshy{
1036145256Sjkoshy	int error;
1037145256Sjkoshy	struct proc *top;
1038145256Sjkoshy
1039145256Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
1040145256Sjkoshy
1041145256Sjkoshy	PMCDBG(PRC,ATT,1, "attach pm=%p ri=%d proc=%p (%d, %s)", pm,
1042145774Sjkoshy	    PMC_TO_ROWINDEX(pm), p, p->p_pid, p->p_comm);
1043145256Sjkoshy
1044145774Sjkoshy
1045145774Sjkoshy	/*
1046145774Sjkoshy	 * If this PMC successfully allowed a GETMSR operation
1047145774Sjkoshy	 * in the past, disallow further ATTACHes.
1048145774Sjkoshy	 */
1049145774Sjkoshy
1050145774Sjkoshy	if ((pm->pm_flags & PMC_PP_ENABLE_MSR_ACCESS) != 0)
1051145774Sjkoshy		return EPERM;
1052145774Sjkoshy
1053145256Sjkoshy	if ((pm->pm_flags & PMC_F_DESCENDANTS) == 0)
1054145256Sjkoshy		return pmc_attach_one_process(p, pm);
1055145256Sjkoshy
1056145256Sjkoshy	/*
1057145256Sjkoshy	 * Traverse all child processes, attaching them to
1058145256Sjkoshy	 * this PMC.
1059145256Sjkoshy	 */
1060145256Sjkoshy
1061145256Sjkoshy	sx_slock(&proctree_lock);
1062145256Sjkoshy
1063145256Sjkoshy	top = p;
1064145256Sjkoshy
1065145256Sjkoshy	for (;;) {
1066145256Sjkoshy		if ((error = pmc_attach_one_process(p, pm)) != 0)
1067145256Sjkoshy			break;
1068145256Sjkoshy		if (!LIST_EMPTY(&p->p_children))
1069145256Sjkoshy			p = LIST_FIRST(&p->p_children);
1070145256Sjkoshy		else for (;;) {
1071145256Sjkoshy			if (p == top)
1072145256Sjkoshy				goto done;
1073145256Sjkoshy			if (LIST_NEXT(p, p_sibling)) {
1074145256Sjkoshy				p = LIST_NEXT(p, p_sibling);
1075145256Sjkoshy				break;
1076145256Sjkoshy			}
1077145256Sjkoshy			p = p->p_pptr;
1078145256Sjkoshy		}
1079145256Sjkoshy	}
1080145256Sjkoshy
1081145256Sjkoshy	if (error)
1082145256Sjkoshy		(void) pmc_detach_process(top, pm);
1083145256Sjkoshy
1084145256Sjkoshy done:
1085145256Sjkoshy	sx_sunlock(&proctree_lock);
1086145256Sjkoshy	return error;
1087145256Sjkoshy}
1088145256Sjkoshy
1089145256Sjkoshy/*
1090145256Sjkoshy * Detach a process from a PMC.  If there are no other PMCs tracking
1091145256Sjkoshy * this process, remove the process structure from its hash table.  If
1092145256Sjkoshy * 'flags' contains PMC_FLAG_REMOVE, then free the process structure.
1093145256Sjkoshy */
1094145256Sjkoshy
1095145256Sjkoshystatic int
1096145256Sjkoshypmc_detach_one_process(struct proc *p, struct pmc *pm, int flags)
1097145256Sjkoshy{
1098145256Sjkoshy	int ri;
1099145256Sjkoshy	struct pmc_process *pp;
1100145256Sjkoshy
1101145256Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
1102145256Sjkoshy
1103145256Sjkoshy	KASSERT(pm != NULL,
1104145256Sjkoshy	    ("[pmc,%d] null pm pointer", __LINE__));
1105145256Sjkoshy
1106145774Sjkoshy	ri = PMC_TO_ROWINDEX(pm);
1107145774Sjkoshy
1108145256Sjkoshy	PMCDBG(PRC,ATT,2, "detach-one pm=%p ri=%d proc=%p (%d, %s) flags=0x%x",
1109145774Sjkoshy	    pm, ri, p, p->p_pid, p->p_comm, flags);
1110145256Sjkoshy
1111145256Sjkoshy	if ((pp = pmc_find_process_descriptor(p, 0)) == NULL)
1112145256Sjkoshy		return ESRCH;
1113145256Sjkoshy
1114145256Sjkoshy	if (pp->pp_pmcs[ri].pp_pmc != pm)
1115145256Sjkoshy		return EINVAL;
1116145256Sjkoshy
1117145256Sjkoshy	pmc_unlink_target_process(pm, pp);
1118145256Sjkoshy
1119147191Sjkoshy	/* Issue a detach entry if a log file is configured */
1120147191Sjkoshy	if (pm->pm_owner->po_flags & PMC_PO_OWNS_LOGFILE)
1121147191Sjkoshy		pmclog_process_pmcdetach(pm, p->p_pid);
1122147191Sjkoshy
1123145256Sjkoshy	/*
1124145256Sjkoshy	 * If there are no PMCs targetting this process, we remove its
1125145256Sjkoshy	 * descriptor from the target hash table and unset the P_HWPMC
1126145256Sjkoshy	 * flag in the struct proc.
1127145256Sjkoshy	 */
1128198343Sfabient	KASSERT(pp->pp_refcnt >= 0 && pp->pp_refcnt <= (int) md->pmd_npmc,
1129145256Sjkoshy	    ("[pmc,%d] Illegal refcnt %d for process struct %p",
1130145256Sjkoshy		__LINE__, pp->pp_refcnt, pp));
1131145256Sjkoshy
1132145256Sjkoshy	if (pp->pp_refcnt != 0)	/* still a target of some PMC */
1133145256Sjkoshy		return 0;
1134145256Sjkoshy
1135145256Sjkoshy	pmc_remove_process_descriptor(pp);
1136145256Sjkoshy
1137145256Sjkoshy	if (flags & PMC_FLAG_REMOVE)
1138184205Sdes		free(pp, M_PMC);
1139145256Sjkoshy
1140145256Sjkoshy	PROC_LOCK(p);
1141145256Sjkoshy	p->p_flag &= ~P_HWPMC;
1142145256Sjkoshy	PROC_UNLOCK(p);
1143145256Sjkoshy
1144145256Sjkoshy	return 0;
1145145256Sjkoshy}
1146145256Sjkoshy
1147145256Sjkoshy/*
1148145256Sjkoshy * Detach a process and optionally its descendants from a PMC.
1149145256Sjkoshy */
1150145256Sjkoshy
1151145256Sjkoshystatic int
1152145256Sjkoshypmc_detach_process(struct proc *p, struct pmc *pm)
1153145256Sjkoshy{
1154145256Sjkoshy	struct proc *top;
1155145256Sjkoshy
1156145256Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
1157145256Sjkoshy
1158145256Sjkoshy	PMCDBG(PRC,ATT,1, "detach pm=%p ri=%d proc=%p (%d, %s)", pm,
1159145774Sjkoshy	    PMC_TO_ROWINDEX(pm), p, p->p_pid, p->p_comm);
1160145256Sjkoshy
1161145256Sjkoshy	if ((pm->pm_flags & PMC_F_DESCENDANTS) == 0)
1162145256Sjkoshy		return pmc_detach_one_process(p, pm, PMC_FLAG_REMOVE);
1163145256Sjkoshy
1164145256Sjkoshy	/*
1165145256Sjkoshy	 * Traverse all children, detaching them from this PMC.  We
1166145256Sjkoshy	 * ignore errors since we could be detaching a PMC from a
1167145256Sjkoshy	 * partially attached proc tree.
1168145256Sjkoshy	 */
1169145256Sjkoshy
1170145256Sjkoshy	sx_slock(&proctree_lock);
1171145256Sjkoshy
1172145256Sjkoshy	top = p;
1173145256Sjkoshy
1174145256Sjkoshy	for (;;) {
1175145256Sjkoshy		(void) pmc_detach_one_process(p, pm, PMC_FLAG_REMOVE);
1176145256Sjkoshy
1177145256Sjkoshy		if (!LIST_EMPTY(&p->p_children))
1178145256Sjkoshy			p = LIST_FIRST(&p->p_children);
1179145256Sjkoshy		else for (;;) {
1180145256Sjkoshy			if (p == top)
1181145256Sjkoshy				goto done;
1182145256Sjkoshy			if (LIST_NEXT(p, p_sibling)) {
1183145256Sjkoshy				p = LIST_NEXT(p, p_sibling);
1184145256Sjkoshy				break;
1185145256Sjkoshy			}
1186145256Sjkoshy			p = p->p_pptr;
1187145256Sjkoshy		}
1188145256Sjkoshy	}
1189145256Sjkoshy
1190145256Sjkoshy done:
1191145256Sjkoshy	sx_sunlock(&proctree_lock);
1192147191Sjkoshy
1193147191Sjkoshy	if (LIST_EMPTY(&pm->pm_targets))
1194147191Sjkoshy		pm->pm_flags &= ~PMC_F_ATTACH_DONE;
1195147191Sjkoshy
1196145256Sjkoshy	return 0;
1197145256Sjkoshy}
1198145256Sjkoshy
1199147191Sjkoshy
1200145256Sjkoshy/*
1201147191Sjkoshy * Thread context switch IN
1202145256Sjkoshy */
1203145256Sjkoshy
1204147191Sjkoshystatic void
1205147191Sjkoshypmc_process_csw_in(struct thread *td)
1206147191Sjkoshy{
1207147191Sjkoshy	int cpu;
1208184802Sjkoshy	unsigned int adjri, ri;
1209147191Sjkoshy	struct pmc *pm;
1210147191Sjkoshy	struct proc *p;
1211147191Sjkoshy	struct pmc_cpu *pc;
1212147191Sjkoshy	struct pmc_hw *phw;
1213184802Sjkoshy	pmc_value_t newvalue;
1214147191Sjkoshy	struct pmc_process *pp;
1215184802Sjkoshy	struct pmc_classdep *pcd;
1216145256Sjkoshy
1217147191Sjkoshy	p = td->td_proc;
1218145256Sjkoshy
1219147191Sjkoshy	if ((pp = pmc_find_process_descriptor(p, PMC_FLAG_NONE)) == NULL)
1220147191Sjkoshy		return;
1221145256Sjkoshy
1222147191Sjkoshy	KASSERT(pp->pp_proc == td->td_proc,
1223147191Sjkoshy	    ("[pmc,%d] not my thread state", __LINE__));
1224145256Sjkoshy
1225147191Sjkoshy	critical_enter(); /* no preemption from this point */
1226145256Sjkoshy
1227147191Sjkoshy	cpu = PCPU_GET(cpuid); /* td->td_oncpu is invalid */
1228145256Sjkoshy
1229147191Sjkoshy	PMCDBG(CSW,SWI,1, "cpu=%d proc=%p (%d, %s) pp=%p", cpu, p,
1230147191Sjkoshy	    p->p_pid, p->p_comm, pp);
1231145256Sjkoshy
1232183266Sjkoshy	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
1233147191Sjkoshy	    ("[pmc,%d] wierd CPU id %d", __LINE__, cpu));
1234145256Sjkoshy
1235147191Sjkoshy	pc = pmc_pcpu[cpu];
1236145256Sjkoshy
1237147191Sjkoshy	for (ri = 0; ri < md->pmd_npmc; ri++) {
1238145256Sjkoshy
1239147191Sjkoshy		if ((pm = pp->pp_pmcs[ri].pp_pmc) == NULL)
1240147191Sjkoshy			continue;
1241147191Sjkoshy
1242147191Sjkoshy		KASSERT(PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm)),
1243147191Sjkoshy		    ("[pmc,%d] Target PMC in non-virtual mode (%d)",
1244147191Sjkoshy			__LINE__, PMC_TO_MODE(pm)));
1245147191Sjkoshy
1246147191Sjkoshy		KASSERT(PMC_TO_ROWINDEX(pm) == ri,
1247147191Sjkoshy		    ("[pmc,%d] Row index mismatch pmc %d != ri %d",
1248147191Sjkoshy			__LINE__, PMC_TO_ROWINDEX(pm), ri));
1249147191Sjkoshy
1250145256Sjkoshy		/*
1251147191Sjkoshy		 * Only PMCs that are marked as 'RUNNING' need
1252147191Sjkoshy		 * be placed on hardware.
1253145256Sjkoshy		 */
1254145256Sjkoshy
1255147191Sjkoshy		if (pm->pm_state != PMC_STATE_RUNNING)
1256147191Sjkoshy			continue;
1257145256Sjkoshy
1258147191Sjkoshy		/* increment PMC runcount */
1259208861Sfabient		atomic_add_rel_int(&pm->pm_runcount, 1);
1260145256Sjkoshy
1261147191Sjkoshy		/* configure the HWPMC we are going to use. */
1262184802Sjkoshy		pcd = pmc_ri_to_classdep(md, ri, &adjri);
1263184802Sjkoshy		pcd->pcd_config_pmc(cpu, adjri, pm);
1264145256Sjkoshy
1265147191Sjkoshy		phw = pc->pc_hwpmcs[ri];
1266145256Sjkoshy
1267147191Sjkoshy		KASSERT(phw != NULL,
1268147191Sjkoshy		    ("[pmc,%d] null hw pointer", __LINE__));
1269145256Sjkoshy
1270147191Sjkoshy		KASSERT(phw->phw_pmc == pm,
1271147191Sjkoshy		    ("[pmc,%d] hw->pmc %p != pmc %p", __LINE__,
1272147191Sjkoshy			phw->phw_pmc, pm));
1273145256Sjkoshy
1274147191Sjkoshy		/*
1275147191Sjkoshy		 * Write out saved value and start the PMC.
1276147191Sjkoshy		 *
1277147191Sjkoshy		 * Sampling PMCs use a per-process value, while
1278147191Sjkoshy		 * counting mode PMCs use a per-pmc value that is
1279147191Sjkoshy		 * inherited across descendants.
1280147191Sjkoshy		 */
1281147191Sjkoshy		if (PMC_TO_MODE(pm) == PMC_MODE_TS) {
1282147191Sjkoshy			mtx_pool_lock_spin(pmc_mtxpool, pm);
1283147191Sjkoshy			newvalue = PMC_PCPU_SAVED(cpu,ri) =
1284147191Sjkoshy			    pp->pp_pmcs[ri].pp_pmcval;
1285147191Sjkoshy			mtx_pool_unlock_spin(pmc_mtxpool, pm);
1286147191Sjkoshy		} else {
1287147191Sjkoshy			KASSERT(PMC_TO_MODE(pm) == PMC_MODE_TC,
1288147191Sjkoshy			    ("[pmc,%d] illegal mode=%d", __LINE__,
1289147191Sjkoshy			    PMC_TO_MODE(pm)));
1290147191Sjkoshy			mtx_pool_lock_spin(pmc_mtxpool, pm);
1291147191Sjkoshy			newvalue = PMC_PCPU_SAVED(cpu, ri) =
1292147191Sjkoshy			    pm->pm_gv.pm_savedvalue;
1293147191Sjkoshy			mtx_pool_unlock_spin(pmc_mtxpool, pm);
1294147191Sjkoshy		}
1295145256Sjkoshy
1296147191Sjkoshy		PMCDBG(CSW,SWI,1,"cpu=%d ri=%d new=%jd", cpu, ri, newvalue);
1297145256Sjkoshy
1298184802Sjkoshy		pcd->pcd_write_pmc(cpu, adjri, newvalue);
1299184802Sjkoshy		pcd->pcd_start_pmc(cpu, adjri);
1300147191Sjkoshy	}
1301145256Sjkoshy
1302147191Sjkoshy	/*
1303147191Sjkoshy	 * perform any other architecture/cpu dependent thread
1304147191Sjkoshy	 * switch-in actions.
1305147191Sjkoshy	 */
1306145256Sjkoshy
1307147191Sjkoshy	(void) (*md->pmd_switch_in)(pc, pp);
1308145256Sjkoshy
1309147191Sjkoshy	critical_exit();
1310145256Sjkoshy
1311147191Sjkoshy}
1312145256Sjkoshy
1313147191Sjkoshy/*
1314147191Sjkoshy * Thread context switch OUT.
1315147191Sjkoshy */
1316145256Sjkoshy
1317147191Sjkoshystatic void
1318147191Sjkoshypmc_process_csw_out(struct thread *td)
1319147191Sjkoshy{
1320147191Sjkoshy	int cpu;
1321184802Sjkoshy	int64_t tmp;
1322147191Sjkoshy	struct pmc *pm;
1323147191Sjkoshy	struct proc *p;
1324184802Sjkoshy	enum pmc_mode mode;
1325147191Sjkoshy	struct pmc_cpu *pc;
1326184802Sjkoshy	pmc_value_t newvalue;
1327184802Sjkoshy	unsigned int adjri, ri;
1328147191Sjkoshy	struct pmc_process *pp;
1329184802Sjkoshy	struct pmc_classdep *pcd;
1330145256Sjkoshy
1331184802Sjkoshy
1332147191Sjkoshy	/*
1333147191Sjkoshy	 * Locate our process descriptor; this may be NULL if
1334147191Sjkoshy	 * this process is exiting and we have already removed
1335147191Sjkoshy	 * the process from the target process table.
1336147191Sjkoshy	 *
1337147191Sjkoshy	 * Note that due to kernel preemption, multiple
1338147191Sjkoshy	 * context switches may happen while the process is
1339147191Sjkoshy	 * exiting.
1340147191Sjkoshy	 *
1341147191Sjkoshy	 * Note also that if the target process cannot be
1342147191Sjkoshy	 * found we still need to deconfigure any PMCs that
1343147191Sjkoshy	 * are currently running on hardware.
1344147191Sjkoshy	 */
1345145256Sjkoshy
1346147191Sjkoshy	p = td->td_proc;
1347147191Sjkoshy	pp = pmc_find_process_descriptor(p, PMC_FLAG_NONE);
1348145256Sjkoshy
1349147191Sjkoshy	/*
1350147191Sjkoshy	 * save PMCs
1351147191Sjkoshy	 */
1352145256Sjkoshy
1353147191Sjkoshy	critical_enter();
1354145774Sjkoshy
1355147191Sjkoshy	cpu = PCPU_GET(cpuid); /* td->td_oncpu is invalid */
1356145256Sjkoshy
1357147191Sjkoshy	PMCDBG(CSW,SWO,1, "cpu=%d proc=%p (%d, %s) pp=%p", cpu, p,
1358147191Sjkoshy	    p->p_pid, p->p_comm, pp);
1359145615Sjkoshy
1360183266Sjkoshy	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
1361147191Sjkoshy	    ("[pmc,%d wierd CPU id %d", __LINE__, cpu));
1362145615Sjkoshy
1363147191Sjkoshy	pc = pmc_pcpu[cpu];
1364145615Sjkoshy
1365147191Sjkoshy	/*
1366147191Sjkoshy	 * When a PMC gets unlinked from a target PMC, it will
1367147191Sjkoshy	 * be removed from the target's pp_pmc[] array.
1368147191Sjkoshy	 *
1369147191Sjkoshy	 * However, on a MP system, the target could have been
1370147191Sjkoshy	 * executing on another CPU at the time of the unlink.
1371147191Sjkoshy	 * So, at context switch OUT time, we need to look at
1372147191Sjkoshy	 * the hardware to determine if a PMC is scheduled on
1373147191Sjkoshy	 * it.
1374147191Sjkoshy	 */
1375145256Sjkoshy
1376147191Sjkoshy	for (ri = 0; ri < md->pmd_npmc; ri++) {
1377145256Sjkoshy
1378184802Sjkoshy		pcd = pmc_ri_to_classdep(md, ri, &adjri);
1379184802Sjkoshy		pm  = NULL;
1380184802Sjkoshy		(void) (*pcd->pcd_get_config)(cpu, adjri, &pm);
1381145256Sjkoshy
1382147191Sjkoshy		if (pm == NULL)	/* nothing at this row index */
1383147191Sjkoshy			continue;
1384145256Sjkoshy
1385147191Sjkoshy		mode = PMC_TO_MODE(pm);
1386147191Sjkoshy		if (!PMC_IS_VIRTUAL_MODE(mode))
1387147191Sjkoshy			continue; /* not a process virtual PMC */
1388145774Sjkoshy
1389147191Sjkoshy		KASSERT(PMC_TO_ROWINDEX(pm) == ri,
1390147191Sjkoshy		    ("[pmc,%d] ri mismatch pmc(%d) ri(%d)",
1391147191Sjkoshy			__LINE__, PMC_TO_ROWINDEX(pm), ri));
1392145256Sjkoshy
1393147191Sjkoshy		/* Stop hardware if not already stopped */
1394147867Sjkoshy		if (pm->pm_stalled == 0)
1395184802Sjkoshy			pcd->pcd_stop_pmc(cpu, adjri);
1396147191Sjkoshy
1397147191Sjkoshy		/* reduce this PMC's runcount */
1398208861Sfabient		atomic_subtract_rel_int(&pm->pm_runcount, 1);
1399147191Sjkoshy
1400145256Sjkoshy		/*
1401147191Sjkoshy		 * If this PMC is associated with this process,
1402147191Sjkoshy		 * save the reading.
1403145256Sjkoshy		 */
1404145256Sjkoshy
1405147191Sjkoshy		if (pp != NULL && pp->pp_pmcs[ri].pp_pmc != NULL) {
1406147191Sjkoshy
1407147191Sjkoshy			KASSERT(pm == pp->pp_pmcs[ri].pp_pmc,
1408147191Sjkoshy			    ("[pmc,%d] pm %p != pp_pmcs[%d] %p", __LINE__,
1409147191Sjkoshy				pm, ri, pp->pp_pmcs[ri].pp_pmc));
1410147191Sjkoshy
1411147191Sjkoshy			KASSERT(pp->pp_refcnt > 0,
1412147191Sjkoshy			    ("[pmc,%d] pp refcnt = %d", __LINE__,
1413147191Sjkoshy				pp->pp_refcnt));
1414147191Sjkoshy
1415184802Sjkoshy			pcd->pcd_read_pmc(cpu, adjri, &newvalue);
1416147191Sjkoshy
1417147191Sjkoshy			tmp = newvalue - PMC_PCPU_SAVED(cpu,ri);
1418147191Sjkoshy
1419199972Semaste			PMCDBG(CSW,SWO,1,"cpu=%d ri=%d tmp=%jd", cpu, ri,
1420147191Sjkoshy			    tmp);
1421147191Sjkoshy
1422147191Sjkoshy			if (mode == PMC_MODE_TS) {
1423147191Sjkoshy
1424147191Sjkoshy				/*
1425147191Sjkoshy				 * For sampling process-virtual PMCs,
1426147191Sjkoshy				 * we expect the count to be
1427147191Sjkoshy				 * decreasing as the 'value'
1428147191Sjkoshy				 * programmed into the PMC is the
1429147191Sjkoshy				 * number of events to be seen till
1430147191Sjkoshy				 * the next sampling interrupt.
1431147191Sjkoshy				 */
1432147191Sjkoshy				if (tmp < 0)
1433147191Sjkoshy					tmp += pm->pm_sc.pm_reloadcount;
1434147191Sjkoshy				mtx_pool_lock_spin(pmc_mtxpool, pm);
1435147191Sjkoshy				pp->pp_pmcs[ri].pp_pmcval -= tmp;
1436147191Sjkoshy				if ((int64_t) pp->pp_pmcs[ri].pp_pmcval < 0)
1437147191Sjkoshy					pp->pp_pmcs[ri].pp_pmcval +=
1438147191Sjkoshy					    pm->pm_sc.pm_reloadcount;
1439147191Sjkoshy				mtx_pool_unlock_spin(pmc_mtxpool, pm);
1440147191Sjkoshy
1441147191Sjkoshy			} else {
1442147191Sjkoshy
1443147191Sjkoshy				/*
1444147191Sjkoshy				 * For counting process-virtual PMCs,
1445147191Sjkoshy				 * we expect the count to be
1446147191Sjkoshy				 * increasing monotonically, modulo a 64
1447147191Sjkoshy				 * bit wraparound.
1448147191Sjkoshy				 */
1449147191Sjkoshy				KASSERT((int64_t) tmp >= 0,
1450147191Sjkoshy				    ("[pmc,%d] negative increment cpu=%d "
1451147191Sjkoshy				     "ri=%d newvalue=%jx saved=%jx "
1452147191Sjkoshy				     "incr=%jx", __LINE__, cpu, ri,
1453147191Sjkoshy				     newvalue, PMC_PCPU_SAVED(cpu,ri), tmp));
1454147191Sjkoshy
1455147191Sjkoshy				mtx_pool_lock_spin(pmc_mtxpool, pm);
1456147191Sjkoshy				pm->pm_gv.pm_savedvalue += tmp;
1457147191Sjkoshy				pp->pp_pmcs[ri].pp_pmcval += tmp;
1458147191Sjkoshy				mtx_pool_unlock_spin(pmc_mtxpool, pm);
1459147191Sjkoshy
1460147191Sjkoshy				if (pm->pm_flags & PMC_F_LOG_PROCCSW)
1461147191Sjkoshy					pmclog_process_proccsw(pm, pp, tmp);
1462147191Sjkoshy			}
1463145256Sjkoshy		}
1464145256Sjkoshy
1465147191Sjkoshy		/* mark hardware as free */
1466184802Sjkoshy		pcd->pcd_config_pmc(cpu, adjri, NULL);
1467145256Sjkoshy	}
1468145256Sjkoshy
1469145256Sjkoshy	/*
1470147191Sjkoshy	 * perform any other architecture/cpu dependent thread
1471147191Sjkoshy	 * switch out functions.
1472147191Sjkoshy	 */
1473147191Sjkoshy
1474147191Sjkoshy	(void) (*md->pmd_switch_out)(pc, pp);
1475147191Sjkoshy
1476147191Sjkoshy	critical_exit();
1477147191Sjkoshy}
1478147191Sjkoshy
1479147191Sjkoshy/*
1480157144Sjkoshy * A mapping change for a process.
1481157144Sjkoshy */
1482157144Sjkoshy
1483157144Sjkoshystatic void
1484157144Sjkoshypmc_process_mmap(struct thread *td, struct pmckern_map_in *pkm)
1485157144Sjkoshy{
1486157144Sjkoshy	int ri;
1487157144Sjkoshy	pid_t pid;
1488157144Sjkoshy	char *fullpath, *freepath;
1489157144Sjkoshy	const struct pmc *pm;
1490157144Sjkoshy	struct pmc_owner *po;
1491157144Sjkoshy	const struct pmc_process *pp;
1492157144Sjkoshy
1493157144Sjkoshy	freepath = fullpath = NULL;
1494157144Sjkoshy	pmc_getfilename((struct vnode *) pkm->pm_file, &fullpath, &freepath);
1495157144Sjkoshy
1496157144Sjkoshy	pid = td->td_proc->p_pid;
1497157144Sjkoshy
1498157144Sjkoshy	/* Inform owners of all system-wide sampling PMCs. */
1499157144Sjkoshy	LIST_FOREACH(po, &pmc_ss_owners, po_ssnext)
1500157144Sjkoshy	    if (po->po_flags & PMC_PO_OWNS_LOGFILE)
1501157144Sjkoshy		pmclog_process_map_in(po, pid, pkm->pm_address, fullpath);
1502157144Sjkoshy
1503157144Sjkoshy	if ((pp = pmc_find_process_descriptor(td->td_proc, 0)) == NULL)
1504157144Sjkoshy		goto done;
1505157144Sjkoshy
1506157144Sjkoshy	/*
1507157144Sjkoshy	 * Inform sampling PMC owners tracking this process.
1508157144Sjkoshy	 */
1509157144Sjkoshy	for (ri = 0; ri < md->pmd_npmc; ri++)
1510157144Sjkoshy		if ((pm = pp->pp_pmcs[ri].pp_pmc) != NULL &&
1511157144Sjkoshy		    PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
1512157144Sjkoshy			pmclog_process_map_in(pm->pm_owner,
1513157144Sjkoshy			    pid, pkm->pm_address, fullpath);
1514157144Sjkoshy
1515157144Sjkoshy  done:
1516157144Sjkoshy	if (freepath)
1517184205Sdes		free(freepath, M_TEMP);
1518157144Sjkoshy}
1519157144Sjkoshy
1520157144Sjkoshy
1521157144Sjkoshy/*
1522157144Sjkoshy * Log an munmap request.
1523157144Sjkoshy */
1524157144Sjkoshy
1525157144Sjkoshystatic void
1526157144Sjkoshypmc_process_munmap(struct thread *td, struct pmckern_map_out *pkm)
1527157144Sjkoshy{
1528157144Sjkoshy	int ri;
1529157144Sjkoshy	pid_t pid;
1530157144Sjkoshy	struct pmc_owner *po;
1531157144Sjkoshy	const struct pmc *pm;
1532157144Sjkoshy	const struct pmc_process *pp;
1533157144Sjkoshy
1534157144Sjkoshy	pid = td->td_proc->p_pid;
1535157144Sjkoshy
1536157144Sjkoshy	LIST_FOREACH(po, &pmc_ss_owners, po_ssnext)
1537157144Sjkoshy	    if (po->po_flags & PMC_PO_OWNS_LOGFILE)
1538157144Sjkoshy		pmclog_process_map_out(po, pid, pkm->pm_address,
1539157144Sjkoshy		    pkm->pm_address + pkm->pm_size);
1540157144Sjkoshy
1541157144Sjkoshy	if ((pp = pmc_find_process_descriptor(td->td_proc, 0)) == NULL)
1542157144Sjkoshy		return;
1543157144Sjkoshy
1544157144Sjkoshy	for (ri = 0; ri < md->pmd_npmc; ri++)
1545157144Sjkoshy		if ((pm = pp->pp_pmcs[ri].pp_pmc) != NULL &&
1546157144Sjkoshy		    PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
1547157651Sjkoshy			pmclog_process_map_out(pm->pm_owner, pid,
1548157651Sjkoshy			    pkm->pm_address, pkm->pm_address + pkm->pm_size);
1549157144Sjkoshy}
1550157144Sjkoshy
1551157144Sjkoshy/*
1552174395Sjkoshy * Log mapping information about the kernel.
1553174395Sjkoshy */
1554174395Sjkoshy
1555174395Sjkoshystatic void
1556174395Sjkoshypmc_log_kernel_mappings(struct pmc *pm)
1557174395Sjkoshy{
1558174395Sjkoshy	struct pmc_owner *po;
1559174395Sjkoshy	struct pmckern_map_in *km, *kmbase;
1560174395Sjkoshy
1561174395Sjkoshy	sx_assert(&pmc_sx, SX_LOCKED);
1562174395Sjkoshy	KASSERT(PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)),
1563174395Sjkoshy	    ("[pmc,%d] non-sampling PMC (%p) desires mapping information",
1564174395Sjkoshy		__LINE__, (void *) pm));
1565174395Sjkoshy
1566174395Sjkoshy	po = pm->pm_owner;
1567174395Sjkoshy
1568174395Sjkoshy	if (po->po_flags & PMC_PO_INITIAL_MAPPINGS_DONE)
1569174395Sjkoshy		return;
1570174395Sjkoshy
1571174395Sjkoshy	/*
1572174395Sjkoshy	 * Log the current set of kernel modules.
1573174395Sjkoshy	 */
1574174395Sjkoshy	kmbase = linker_hwpmc_list_objects();
1575174395Sjkoshy	for (km = kmbase; km->pm_file != NULL; km++) {
1576174395Sjkoshy		PMCDBG(LOG,REG,1,"%s %p", (char *) km->pm_file,
1577174395Sjkoshy		    (void *) km->pm_address);
1578174395Sjkoshy		pmclog_process_map_in(po, (pid_t) -1, km->pm_address,
1579174395Sjkoshy		    km->pm_file);
1580174395Sjkoshy	}
1581184205Sdes	free(kmbase, M_LINKER);
1582174395Sjkoshy
1583174395Sjkoshy	po->po_flags |= PMC_PO_INITIAL_MAPPINGS_DONE;
1584174395Sjkoshy}
1585174395Sjkoshy
1586174395Sjkoshy/*
1587174395Sjkoshy * Log the mappings for a single process.
1588174395Sjkoshy */
1589174395Sjkoshy
1590174395Sjkoshystatic void
1591174395Sjkoshypmc_log_process_mappings(struct pmc_owner *po, struct proc *p)
1592174395Sjkoshy{
1593201021Sjkoshy	vm_map_t map;
1594201021Sjkoshy	struct vnode *vp;
1595201021Sjkoshy	struct vmspace *vm;
1596201021Sjkoshy	vm_map_entry_t entry;
1597201021Sjkoshy	vm_offset_t last_end;
1598201021Sjkoshy	u_int last_timestamp;
1599201021Sjkoshy	struct vnode *last_vp;
1600201021Sjkoshy	vm_offset_t start_addr;
1601201021Sjkoshy	vm_object_t obj, lobj, tobj;
1602201021Sjkoshy	char *fullpath, *freepath;
1603201021Sjkoshy
1604201021Sjkoshy	last_vp = NULL;
1605201021Sjkoshy	last_end = (vm_offset_t) 0;
1606201021Sjkoshy	fullpath = freepath = NULL;
1607201021Sjkoshy
1608201021Sjkoshy	if ((vm = vmspace_acquire_ref(p)) == NULL)
1609201021Sjkoshy		return;
1610201021Sjkoshy
1611201021Sjkoshy	map = &vm->vm_map;
1612201021Sjkoshy	vm_map_lock_read(map);
1613201021Sjkoshy
1614201021Sjkoshy	for (entry = map->header.next; entry != &map->header; entry = entry->next) {
1615201021Sjkoshy
1616201021Sjkoshy		if (entry == NULL) {
1617201021Sjkoshy			PMCDBG(LOG,OPS,2, "hwpmc: vm_map entry unexpectedly "
1618201021Sjkoshy			    "NULL! pid=%d vm_map=%p\n", p->p_pid, map);
1619201021Sjkoshy			break;
1620201021Sjkoshy		}
1621201021Sjkoshy
1622201021Sjkoshy		/*
1623201021Sjkoshy		 * We only care about executable map entries.
1624201021Sjkoshy		 */
1625201021Sjkoshy		if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) ||
1626201021Sjkoshy		    !(entry->protection & VM_PROT_EXECUTE) ||
1627201021Sjkoshy		    (entry->object.vm_object == NULL)) {
1628201021Sjkoshy			continue;
1629201021Sjkoshy		}
1630201021Sjkoshy
1631201021Sjkoshy		obj = entry->object.vm_object;
1632251423Salc		VM_OBJECT_RLOCK(obj);
1633201021Sjkoshy
1634201021Sjkoshy		/*
1635201021Sjkoshy		 * Walk the backing_object list to find the base
1636201021Sjkoshy		 * (non-shadowed) vm_object.
1637201021Sjkoshy		 */
1638201021Sjkoshy		for (lobj = tobj = obj; tobj != NULL; tobj = tobj->backing_object) {
1639201021Sjkoshy			if (tobj != obj)
1640251423Salc				VM_OBJECT_RLOCK(tobj);
1641201021Sjkoshy			if (lobj != obj)
1642251423Salc				VM_OBJECT_RUNLOCK(lobj);
1643201021Sjkoshy			lobj = tobj;
1644201021Sjkoshy		}
1645201021Sjkoshy
1646201021Sjkoshy		/*
1647201021Sjkoshy		 * At this point lobj is the base vm_object and it is locked.
1648201021Sjkoshy		 */
1649201021Sjkoshy		if (lobj == NULL) {
1650201021Sjkoshy			PMCDBG(LOG,OPS,2, "hwpmc: lobj unexpectedly NULL! pid=%d "
1651201021Sjkoshy			    "vm_map=%p vm_obj=%p\n", p->p_pid, map, obj);
1652251423Salc			VM_OBJECT_RUNLOCK(obj);
1653201021Sjkoshy			continue;
1654201021Sjkoshy		}
1655201021Sjkoshy
1656201021Sjkoshy		if (lobj->type != OBJT_VNODE || lobj->handle == NULL) {
1657201021Sjkoshy			if (lobj != obj)
1658251423Salc				VM_OBJECT_RUNLOCK(lobj);
1659251423Salc			VM_OBJECT_RUNLOCK(obj);
1660201021Sjkoshy			continue;
1661201021Sjkoshy		}
1662201021Sjkoshy
1663201021Sjkoshy		/*
1664201021Sjkoshy		 * Skip contiguous regions that point to the same
1665201021Sjkoshy		 * vnode, so we don't emit redundant MAP-IN
1666201021Sjkoshy		 * directives.
1667201021Sjkoshy		 */
1668201021Sjkoshy		if (entry->start == last_end && lobj->handle == last_vp) {
1669201021Sjkoshy			last_end = entry->end;
1670201021Sjkoshy			if (lobj != obj)
1671251423Salc				VM_OBJECT_RUNLOCK(lobj);
1672251423Salc			VM_OBJECT_RUNLOCK(obj);
1673201021Sjkoshy			continue;
1674201021Sjkoshy		}
1675201021Sjkoshy
1676201021Sjkoshy		/*
1677201021Sjkoshy		 * We don't want to keep the proc's vm_map or this
1678201021Sjkoshy		 * vm_object locked while we walk the pathname, since
1679201021Sjkoshy		 * vn_fullpath() can sleep.  However, if we drop the
1680201021Sjkoshy		 * lock, it's possible for concurrent activity to
1681201021Sjkoshy		 * modify the vm_map list.  To protect against this,
1682201021Sjkoshy		 * we save the vm_map timestamp before we release the
1683201021Sjkoshy		 * lock, and check it after we reacquire the lock
1684201021Sjkoshy		 * below.
1685201021Sjkoshy		 */
1686201021Sjkoshy		start_addr = entry->start;
1687201021Sjkoshy		last_end = entry->end;
1688201021Sjkoshy		last_timestamp = map->timestamp;
1689201021Sjkoshy		vm_map_unlock_read(map);
1690201021Sjkoshy
1691201021Sjkoshy		vp = lobj->handle;
1692201021Sjkoshy		vref(vp);
1693201021Sjkoshy		if (lobj != obj)
1694251423Salc			VM_OBJECT_RUNLOCK(lobj);
1695201021Sjkoshy
1696251423Salc		VM_OBJECT_RUNLOCK(obj);
1697201021Sjkoshy
1698201021Sjkoshy		freepath = NULL;
1699201021Sjkoshy		pmc_getfilename(vp, &fullpath, &freepath);
1700201021Sjkoshy		last_vp = vp;
1701201151Sjkoshy
1702201021Sjkoshy		vrele(vp);
1703201151Sjkoshy
1704201021Sjkoshy		vp = NULL;
1705201021Sjkoshy		pmclog_process_map_in(po, p->p_pid, start_addr, fullpath);
1706201021Sjkoshy		if (freepath)
1707201021Sjkoshy			free(freepath, M_TEMP);
1708201021Sjkoshy
1709201021Sjkoshy		vm_map_lock_read(map);
1710201021Sjkoshy
1711201021Sjkoshy		/*
1712201021Sjkoshy		 * If our saved timestamp doesn't match, this means
1713201021Sjkoshy		 * that the vm_map was modified out from under us and
1714201021Sjkoshy		 * we can't trust our current "entry" pointer.  Do a
1715201021Sjkoshy		 * new lookup for this entry.  If there is no entry
1716201021Sjkoshy		 * for this address range, vm_map_lookup_entry() will
1717201021Sjkoshy		 * return the previous one, so we always want to go to
1718201021Sjkoshy		 * entry->next on the next loop iteration.
1719201021Sjkoshy		 *
1720201021Sjkoshy		 * There is an edge condition here that can occur if
1721201021Sjkoshy		 * there is no entry at or before this address.  In
1722201021Sjkoshy		 * this situation, vm_map_lookup_entry returns
1723201021Sjkoshy		 * &map->header, which would cause our loop to abort
1724201021Sjkoshy		 * without processing the rest of the map.  However,
1725201021Sjkoshy		 * in practice this will never happen for process
1726201021Sjkoshy		 * vm_map.  This is because the executable's text
1727201021Sjkoshy		 * segment is the first mapping in the proc's address
1728201021Sjkoshy		 * space, and this mapping is never removed until the
1729201021Sjkoshy		 * process exits, so there will always be a non-header
1730201021Sjkoshy		 * entry at or before the requested address for
1731201021Sjkoshy		 * vm_map_lookup_entry to return.
1732201021Sjkoshy		 */
1733201021Sjkoshy		if (map->timestamp != last_timestamp)
1734201021Sjkoshy			vm_map_lookup_entry(map, last_end - 1, &entry);
1735201021Sjkoshy	}
1736201021Sjkoshy
1737201021Sjkoshy	vm_map_unlock_read(map);
1738201021Sjkoshy	vmspace_free(vm);
1739201021Sjkoshy	return;
1740174395Sjkoshy}
1741174395Sjkoshy
1742174395Sjkoshy/*
1743174395Sjkoshy * Log mappings for all processes in the system.
1744174395Sjkoshy */
1745174395Sjkoshy
1746174395Sjkoshystatic void
1747174395Sjkoshypmc_log_all_process_mappings(struct pmc_owner *po)
1748174395Sjkoshy{
1749174395Sjkoshy	struct proc *p, *top;
1750174395Sjkoshy
1751174395Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
1752174395Sjkoshy
1753174395Sjkoshy	if ((p = pfind(1)) == NULL)
1754174395Sjkoshy		panic("[pmc,%d] Cannot find init", __LINE__);
1755174395Sjkoshy
1756174395Sjkoshy	PROC_UNLOCK(p);
1757174395Sjkoshy
1758174395Sjkoshy	sx_slock(&proctree_lock);
1759174395Sjkoshy
1760174395Sjkoshy	top = p;
1761174395Sjkoshy
1762174395Sjkoshy	for (;;) {
1763174395Sjkoshy		pmc_log_process_mappings(po, p);
1764174395Sjkoshy		if (!LIST_EMPTY(&p->p_children))
1765174395Sjkoshy			p = LIST_FIRST(&p->p_children);
1766174395Sjkoshy		else for (;;) {
1767174395Sjkoshy			if (p == top)
1768174395Sjkoshy				goto done;
1769174395Sjkoshy			if (LIST_NEXT(p, p_sibling)) {
1770174395Sjkoshy				p = LIST_NEXT(p, p_sibling);
1771174395Sjkoshy				break;
1772174395Sjkoshy			}
1773174395Sjkoshy			p = p->p_pptr;
1774174395Sjkoshy		}
1775174395Sjkoshy	}
1776174395Sjkoshy done:
1777174395Sjkoshy	sx_sunlock(&proctree_lock);
1778174395Sjkoshy}
1779174395Sjkoshy
1780174395Sjkoshy/*
1781147191Sjkoshy * The 'hook' invoked from the kernel proper
1782147191Sjkoshy */
1783147191Sjkoshy
1784147191Sjkoshy
1785153110Sru#ifdef	DEBUG
1786147191Sjkoshyconst char *pmc_hooknames[] = {
1787157144Sjkoshy	/* these strings correspond to PMC_FN_* in <sys/pmckern.h> */
1788147191Sjkoshy	"",
1789147191Sjkoshy	"EXEC",
1790147191Sjkoshy	"CSW-IN",
1791147191Sjkoshy	"CSW-OUT",
1792157144Sjkoshy	"SAMPLE",
1793254813Smarkj	"UNUSED1",
1794254813Smarkj	"UNUSED2",
1795157144Sjkoshy	"MMAP",
1796174395Sjkoshy	"MUNMAP",
1797233628Sfabient	"CALLCHAIN-NMI",
1798233628Sfabient	"CALLCHAIN-SOFT",
1799233628Sfabient	"SOFTSAMPLING"
1800147191Sjkoshy};
1801147191Sjkoshy#endif
1802147191Sjkoshy
1803147191Sjkoshystatic int
1804147191Sjkoshypmc_hook_handler(struct thread *td, int function, void *arg)
1805147191Sjkoshy{
1806147191Sjkoshy
1807147191Sjkoshy	PMCDBG(MOD,PMH,1, "hook td=%p func=%d \"%s\" arg=%p", td, function,
1808147191Sjkoshy	    pmc_hooknames[function], arg);
1809147191Sjkoshy
1810147191Sjkoshy	switch (function)
1811147191Sjkoshy	{
1812147191Sjkoshy
1813147191Sjkoshy	/*
1814145256Sjkoshy	 * Process exec()
1815145256Sjkoshy	 */
1816145256Sjkoshy
1817145256Sjkoshy	case PMC_FN_PROCESS_EXEC:
1818145256Sjkoshy	{
1819147191Sjkoshy		char *fullpath, *freepath;
1820145256Sjkoshy		unsigned int ri;
1821147191Sjkoshy		int is_using_hwpmcs;
1822145256Sjkoshy		struct pmc *pm;
1823145256Sjkoshy		struct proc *p;
1824145256Sjkoshy		struct pmc_owner *po;
1825145256Sjkoshy		struct pmc_process *pp;
1826147708Sjkoshy		struct pmckern_procexec *pk;
1827145256Sjkoshy
1828145256Sjkoshy		sx_assert(&pmc_sx, SX_XLOCKED);
1829145256Sjkoshy
1830147191Sjkoshy		p = td->td_proc;
1831147708Sjkoshy		pmc_getfilename(p->p_textvp, &fullpath, &freepath);
1832147191Sjkoshy
1833147708Sjkoshy		pk = (struct pmckern_procexec *) arg;
1834147708Sjkoshy
1835147191Sjkoshy		/* Inform owners of SS mode PMCs of the exec event. */
1836147191Sjkoshy		LIST_FOREACH(po, &pmc_ss_owners, po_ssnext)
1837147191Sjkoshy		    if (po->po_flags & PMC_PO_OWNS_LOGFILE)
1838147708Sjkoshy			    pmclog_process_procexec(po, PMC_ID_INVALID,
1839147708Sjkoshy				p->p_pid, pk->pm_entryaddr, fullpath);
1840147191Sjkoshy
1841147191Sjkoshy		PROC_LOCK(p);
1842147191Sjkoshy		is_using_hwpmcs = p->p_flag & P_HWPMC;
1843147191Sjkoshy		PROC_UNLOCK(p);
1844147191Sjkoshy
1845147191Sjkoshy		if (!is_using_hwpmcs) {
1846147191Sjkoshy			if (freepath)
1847184205Sdes				free(freepath, M_TEMP);
1848147191Sjkoshy			break;
1849147191Sjkoshy		}
1850147191Sjkoshy
1851145256Sjkoshy		/*
1852145256Sjkoshy		 * PMCs are not inherited across an exec():  remove any
1853145256Sjkoshy		 * PMCs that this process is the owner of.
1854145256Sjkoshy		 */
1855145256Sjkoshy
1856145256Sjkoshy		if ((po = pmc_find_owner_descriptor(p)) != NULL) {
1857145256Sjkoshy			pmc_remove_owner(po);
1858147191Sjkoshy			pmc_destroy_owner_descriptor(po);
1859145256Sjkoshy		}
1860145256Sjkoshy
1861145256Sjkoshy		/*
1862154483Sjkoshy		 * If the process being exec'ed is not the target of any
1863154483Sjkoshy		 * PMC, we are done.
1864145256Sjkoshy		 */
1865154483Sjkoshy		if ((pp = pmc_find_process_descriptor(p, 0)) == NULL) {
1866154483Sjkoshy			if (freepath)
1867184205Sdes				free(freepath, M_TEMP);
1868145256Sjkoshy			break;
1869154483Sjkoshy		}
1870145256Sjkoshy
1871147191Sjkoshy		/*
1872147191Sjkoshy		 * Log the exec event to all monitoring owners.  Skip
1873147191Sjkoshy		 * owners who have already recieved the event because
1874154483Sjkoshy		 * they had system sampling PMCs active.
1875147191Sjkoshy		 */
1876147191Sjkoshy		for (ri = 0; ri < md->pmd_npmc; ri++)
1877147191Sjkoshy			if ((pm = pp->pp_pmcs[ri].pp_pmc) != NULL) {
1878147191Sjkoshy				po = pm->pm_owner;
1879147191Sjkoshy				if (po->po_sscount == 0 &&
1880147191Sjkoshy				    po->po_flags & PMC_PO_OWNS_LOGFILE)
1881147708Sjkoshy					pmclog_process_procexec(po, pm->pm_id,
1882147708Sjkoshy					    p->p_pid, pk->pm_entryaddr,
1883147191Sjkoshy					    fullpath);
1884147191Sjkoshy			}
1885147191Sjkoshy
1886147191Sjkoshy		if (freepath)
1887184205Sdes			free(freepath, M_TEMP);
1888147191Sjkoshy
1889145256Sjkoshy
1890145256Sjkoshy		PMCDBG(PRC,EXC,1, "exec proc=%p (%d, %s) cred-changed=%d",
1891147708Sjkoshy		    p, p->p_pid, p->p_comm, pk->pm_credentialschanged);
1892145256Sjkoshy
1893147708Sjkoshy		if (pk->pm_credentialschanged == 0) /* no change */
1894145256Sjkoshy			break;
1895145256Sjkoshy
1896145256Sjkoshy		/*
1897145256Sjkoshy		 * If the newly exec()'ed process has a different credential
1898145256Sjkoshy		 * than before, allow it to be the target of a PMC only if
1899145256Sjkoshy		 * the PMC's owner has sufficient priviledge.
1900145256Sjkoshy		 */
1901145256Sjkoshy
1902145256Sjkoshy		for (ri = 0; ri < md->pmd_npmc; ri++)
1903145256Sjkoshy			if ((pm = pp->pp_pmcs[ri].pp_pmc) != NULL)
1904145256Sjkoshy				if (pmc_can_attach(pm, td->td_proc) != 0)
1905145256Sjkoshy					pmc_detach_one_process(td->td_proc,
1906145256Sjkoshy					    pm, PMC_FLAG_NONE);
1907145256Sjkoshy
1908198343Sfabient		KASSERT(pp->pp_refcnt >= 0 && pp->pp_refcnt <= (int) md->pmd_npmc,
1909145256Sjkoshy		    ("[pmc,%d] Illegal ref count %d on pp %p", __LINE__,
1910145256Sjkoshy			pp->pp_refcnt, pp));
1911145256Sjkoshy
1912145256Sjkoshy		/*
1913145256Sjkoshy		 * If this process is no longer the target of any
1914145256Sjkoshy		 * PMCs, we can remove the process entry and free
1915145256Sjkoshy		 * up space.
1916145256Sjkoshy		 */
1917145256Sjkoshy
1918145256Sjkoshy		if (pp->pp_refcnt == 0) {
1919145256Sjkoshy			pmc_remove_process_descriptor(pp);
1920184205Sdes			free(pp, M_PMC);
1921147191Sjkoshy			break;
1922145256Sjkoshy		}
1923145256Sjkoshy
1924145256Sjkoshy	}
1925145256Sjkoshy	break;
1926145256Sjkoshy
1927145256Sjkoshy	case PMC_FN_CSW_IN:
1928147191Sjkoshy		pmc_process_csw_in(td);
1929147191Sjkoshy		break;
1930145256Sjkoshy
1931147191Sjkoshy	case PMC_FN_CSW_OUT:
1932147191Sjkoshy		pmc_process_csw_out(td);
1933147191Sjkoshy		break;
1934145256Sjkoshy
1935145256Sjkoshy	/*
1936147191Sjkoshy	 * Process accumulated PC samples.
1937147191Sjkoshy	 *
1938147191Sjkoshy	 * This function is expected to be called by hardclock() for
1939147191Sjkoshy	 * each CPU that has accumulated PC samples.
1940147191Sjkoshy	 *
1941147191Sjkoshy	 * This function is to be executed on the CPU whose samples
1942147191Sjkoshy	 * are being processed.
1943145256Sjkoshy	 */
1944147191Sjkoshy	case PMC_FN_DO_SAMPLES:
1945145256Sjkoshy
1946145256Sjkoshy		/*
1947147191Sjkoshy		 * Clear the cpu specific bit in the CPU mask before
1948147191Sjkoshy		 * do the rest of the processing.  If the NMI handler
1949147191Sjkoshy		 * gets invoked after the "atomic_clear_int()" call
1950147191Sjkoshy		 * below but before "pmc_process_samples()" gets
1951147191Sjkoshy		 * around to processing the interrupt, then we will
1952147191Sjkoshy		 * come back here at the next hardclock() tick (and
1953147191Sjkoshy		 * may find nothing to do if "pmc_process_samples()"
1954147191Sjkoshy		 * had already processed the interrupt).  We don't
1955147191Sjkoshy		 * lose the interrupt sample.
1956145256Sjkoshy		 */
1957222813Sattilio		CPU_CLR_ATOMIC(PCPU_GET(cpuid), &pmc_cpumask);
1958233628Sfabient		pmc_process_samples(PCPU_GET(cpuid), PMC_HR);
1959233628Sfabient		pmc_process_samples(PCPU_GET(cpuid), PMC_SR);
1960147191Sjkoshy		break;
1961145256Sjkoshy
1962157144Sjkoshy	case PMC_FN_MMAP:
1963157144Sjkoshy		sx_assert(&pmc_sx, SX_LOCKED);
1964157144Sjkoshy		pmc_process_mmap(td, (struct pmckern_map_in *) arg);
1965157144Sjkoshy		break;
1966157144Sjkoshy
1967157144Sjkoshy	case PMC_FN_MUNMAP:
1968157144Sjkoshy		sx_assert(&pmc_sx, SX_LOCKED);
1969157144Sjkoshy		pmc_process_munmap(td, (struct pmckern_map_out *) arg);
1970157144Sjkoshy		break;
1971157144Sjkoshy
1972174395Sjkoshy	case PMC_FN_USER_CALLCHAIN:
1973174395Sjkoshy		/*
1974174395Sjkoshy		 * Record a call chain.
1975174395Sjkoshy		 */
1976186037Sjkoshy		KASSERT(td == curthread, ("[pmc,%d] td != curthread",
1977186037Sjkoshy		    __LINE__));
1978233628Sfabient
1979233628Sfabient		pmc_capture_user_callchain(PCPU_GET(cpuid), PMC_HR,
1980174395Sjkoshy		    (struct trapframe *) arg);
1981186037Sjkoshy		td->td_pflags &= ~TDP_CALLCHAIN;
1982174395Sjkoshy		break;
1983174395Sjkoshy
1984233628Sfabient	case PMC_FN_USER_CALLCHAIN_SOFT:
1985233628Sfabient		/*
1986233628Sfabient		 * Record a call chain.
1987233628Sfabient		 */
1988233628Sfabient		KASSERT(td == curthread, ("[pmc,%d] td != curthread",
1989233628Sfabient		    __LINE__));
1990233628Sfabient		pmc_capture_user_callchain(PCPU_GET(cpuid), PMC_SR,
1991233628Sfabient		    (struct trapframe *) arg);
1992233628Sfabient		td->td_pflags &= ~TDP_CALLCHAIN;
1993233628Sfabient		break;
1994233628Sfabient
1995233628Sfabient	case PMC_FN_SOFT_SAMPLING:
1996233628Sfabient		/*
1997233628Sfabient		 * Call soft PMC sampling intr.
1998233628Sfabient		 */
1999233628Sfabient		pmc_soft_intr((struct pmckern_soft *) arg);
2000233628Sfabient		break;
2001233628Sfabient
2002145256Sjkoshy	default:
2003153110Sru#ifdef	DEBUG
2004145256Sjkoshy		KASSERT(0, ("[pmc,%d] unknown hook %d\n", __LINE__, function));
2005145256Sjkoshy#endif
2006145256Sjkoshy		break;
2007145256Sjkoshy
2008145256Sjkoshy	}
2009145256Sjkoshy
2010145256Sjkoshy	return 0;
2011145256Sjkoshy}
2012145256Sjkoshy
2013145256Sjkoshy/*
2014145256Sjkoshy * allocate a 'struct pmc_owner' descriptor in the owner hash table.
2015145256Sjkoshy */
2016145256Sjkoshy
2017145256Sjkoshystatic struct pmc_owner *
2018145256Sjkoshypmc_allocate_owner_descriptor(struct proc *p)
2019145256Sjkoshy{
2020145256Sjkoshy	uint32_t hindex;
2021145256Sjkoshy	struct pmc_owner *po;
2022145256Sjkoshy	struct pmc_ownerhash *poh;
2023145256Sjkoshy
2024145256Sjkoshy	hindex = PMC_HASH_PTR(p, pmc_ownerhashmask);
2025145256Sjkoshy	poh = &pmc_ownerhash[hindex];
2026145256Sjkoshy
2027145256Sjkoshy	/* allocate space for N pointers and one descriptor struct */
2028184802Sjkoshy	po = malloc(sizeof(struct pmc_owner), M_PMC, M_WAITOK|M_ZERO);
2029145256Sjkoshy	po->po_owner = p;
2030145256Sjkoshy	LIST_INSERT_HEAD(poh, po, po_next); /* insert into hash table */
2031145256Sjkoshy
2032147191Sjkoshy	TAILQ_INIT(&po->po_logbuffers);
2033168856Sjkoshy	mtx_init(&po->po_mtx, "pmc-owner-mtx", "pmc-per-proc", MTX_SPIN);
2034147191Sjkoshy
2035145256Sjkoshy	PMCDBG(OWN,ALL,1, "allocate-owner proc=%p (%d, %s) pmc-owner=%p",
2036145256Sjkoshy	    p, p->p_pid, p->p_comm, po);
2037145256Sjkoshy
2038145256Sjkoshy	return po;
2039145256Sjkoshy}
2040145256Sjkoshy
2041147191Sjkoshystatic void
2042147191Sjkoshypmc_destroy_owner_descriptor(struct pmc_owner *po)
2043147191Sjkoshy{
2044147191Sjkoshy
2045147191Sjkoshy	PMCDBG(OWN,REL,1, "destroy-owner po=%p proc=%p (%d, %s)",
2046147191Sjkoshy	    po, po->po_owner, po->po_owner->p_pid, po->po_owner->p_comm);
2047147191Sjkoshy
2048147191Sjkoshy	mtx_destroy(&po->po_mtx);
2049184205Sdes	free(po, M_PMC);
2050147191Sjkoshy}
2051147191Sjkoshy
2052145256Sjkoshy/*
2053145256Sjkoshy * find the descriptor corresponding to process 'p', adding or removing it
2054145256Sjkoshy * as specified by 'mode'.
2055145256Sjkoshy */
2056145256Sjkoshy
2057145256Sjkoshystatic struct pmc_process *
2058145256Sjkoshypmc_find_process_descriptor(struct proc *p, uint32_t mode)
2059145256Sjkoshy{
2060145256Sjkoshy	uint32_t hindex;
2061145256Sjkoshy	struct pmc_process *pp, *ppnew;
2062145256Sjkoshy	struct pmc_processhash *pph;
2063145256Sjkoshy
2064145256Sjkoshy	hindex = PMC_HASH_PTR(p, pmc_processhashmask);
2065145256Sjkoshy	pph = &pmc_processhash[hindex];
2066145256Sjkoshy
2067145256Sjkoshy	ppnew = NULL;
2068145256Sjkoshy
2069145256Sjkoshy	/*
2070145256Sjkoshy	 * Pre-allocate memory in the FIND_ALLOCATE case since we
2071145256Sjkoshy	 * cannot call malloc(9) once we hold a spin lock.
2072145256Sjkoshy	 */
2073184802Sjkoshy	if (mode & PMC_FLAG_ALLOCATE)
2074184214Sdes		ppnew = malloc(sizeof(struct pmc_process) + md->pmd_npmc *
2075184802Sjkoshy		    sizeof(struct pmc_targetstate), M_PMC, M_WAITOK|M_ZERO);
2076145256Sjkoshy
2077145256Sjkoshy	mtx_lock_spin(&pmc_processhash_mtx);
2078145256Sjkoshy	LIST_FOREACH(pp, pph, pp_next)
2079145256Sjkoshy	    if (pp->pp_proc == p)
2080145256Sjkoshy		    break;
2081145256Sjkoshy
2082145256Sjkoshy	if ((mode & PMC_FLAG_REMOVE) && pp != NULL)
2083145256Sjkoshy		LIST_REMOVE(pp, pp_next);
2084145256Sjkoshy
2085145256Sjkoshy	if ((mode & PMC_FLAG_ALLOCATE) && pp == NULL &&
2086145256Sjkoshy	    ppnew != NULL) {
2087145256Sjkoshy		ppnew->pp_proc = p;
2088145256Sjkoshy		LIST_INSERT_HEAD(pph, ppnew, pp_next);
2089145256Sjkoshy		pp = ppnew;
2090145256Sjkoshy		ppnew = NULL;
2091145256Sjkoshy	}
2092145256Sjkoshy	mtx_unlock_spin(&pmc_processhash_mtx);
2093145256Sjkoshy
2094145256Sjkoshy	if (pp != NULL && ppnew != NULL)
2095184205Sdes		free(ppnew, M_PMC);
2096145256Sjkoshy
2097145256Sjkoshy	return pp;
2098145256Sjkoshy}
2099145256Sjkoshy
2100145256Sjkoshy/*
2101145256Sjkoshy * remove a process descriptor from the process hash table.
2102145256Sjkoshy */
2103145256Sjkoshy
2104145256Sjkoshystatic void
2105145256Sjkoshypmc_remove_process_descriptor(struct pmc_process *pp)
2106145256Sjkoshy{
2107145256Sjkoshy	KASSERT(pp->pp_refcnt == 0,
2108145256Sjkoshy	    ("[pmc,%d] Removing process descriptor %p with count %d",
2109145256Sjkoshy		__LINE__, pp, pp->pp_refcnt));
2110145256Sjkoshy
2111145256Sjkoshy	mtx_lock_spin(&pmc_processhash_mtx);
2112145256Sjkoshy	LIST_REMOVE(pp, pp_next);
2113145256Sjkoshy	mtx_unlock_spin(&pmc_processhash_mtx);
2114145256Sjkoshy}
2115145256Sjkoshy
2116145256Sjkoshy
2117145256Sjkoshy/*
2118145256Sjkoshy * find an owner descriptor corresponding to proc 'p'
2119145256Sjkoshy */
2120145256Sjkoshy
2121145256Sjkoshystatic struct pmc_owner *
2122145256Sjkoshypmc_find_owner_descriptor(struct proc *p)
2123145256Sjkoshy{
2124145256Sjkoshy	uint32_t hindex;
2125145256Sjkoshy	struct pmc_owner *po;
2126145256Sjkoshy	struct pmc_ownerhash *poh;
2127145256Sjkoshy
2128145256Sjkoshy	hindex = PMC_HASH_PTR(p, pmc_ownerhashmask);
2129145256Sjkoshy	poh = &pmc_ownerhash[hindex];
2130145256Sjkoshy
2131145256Sjkoshy	po = NULL;
2132145256Sjkoshy	LIST_FOREACH(po, poh, po_next)
2133145256Sjkoshy	    if (po->po_owner == p)
2134145256Sjkoshy		    break;
2135145256Sjkoshy
2136145256Sjkoshy	PMCDBG(OWN,FND,1, "find-owner proc=%p (%d, %s) hindex=0x%x -> "
2137145256Sjkoshy	    "pmc-owner=%p", p, p->p_pid, p->p_comm, hindex, po);
2138145256Sjkoshy
2139145256Sjkoshy	return po;
2140145256Sjkoshy}
2141145256Sjkoshy
2142145256Sjkoshy/*
2143145256Sjkoshy * pmc_allocate_pmc_descriptor
2144145256Sjkoshy *
2145145256Sjkoshy * Allocate a pmc descriptor and initialize its
2146145256Sjkoshy * fields.
2147145256Sjkoshy */
2148145256Sjkoshy
2149145256Sjkoshystatic struct pmc *
2150145256Sjkoshypmc_allocate_pmc_descriptor(void)
2151145256Sjkoshy{
2152145256Sjkoshy	struct pmc *pmc;
2153145256Sjkoshy
2154184802Sjkoshy	pmc = malloc(sizeof(struct pmc), M_PMC, M_WAITOK|M_ZERO);
2155145256Sjkoshy
2156145256Sjkoshy	PMCDBG(PMC,ALL,1, "allocate-pmc -> pmc=%p", pmc);
2157145256Sjkoshy
2158145256Sjkoshy	return pmc;
2159145256Sjkoshy}
2160145256Sjkoshy
2161145256Sjkoshy/*
2162145256Sjkoshy * Destroy a pmc descriptor.
2163145256Sjkoshy */
2164145256Sjkoshy
2165145256Sjkoshystatic void
2166145256Sjkoshypmc_destroy_pmc_descriptor(struct pmc *pm)
2167145256Sjkoshy{
2168145256Sjkoshy	(void) pm;
2169145256Sjkoshy
2170153110Sru#ifdef	DEBUG
2171145256Sjkoshy	KASSERT(pm->pm_state == PMC_STATE_DELETED ||
2172145256Sjkoshy	    pm->pm_state == PMC_STATE_FREE,
2173145256Sjkoshy	    ("[pmc,%d] destroying non-deleted PMC", __LINE__));
2174145256Sjkoshy	KASSERT(LIST_EMPTY(&pm->pm_targets),
2175145256Sjkoshy	    ("[pmc,%d] destroying pmc with targets", __LINE__));
2176145256Sjkoshy	KASSERT(pm->pm_owner == NULL,
2177145256Sjkoshy	    ("[pmc,%d] destroying pmc attached to an owner", __LINE__));
2178145256Sjkoshy	KASSERT(pm->pm_runcount == 0,
2179145256Sjkoshy	    ("[pmc,%d] pmc has non-zero run count %d", __LINE__,
2180145256Sjkoshy		pm->pm_runcount));
2181145256Sjkoshy#endif
2182145256Sjkoshy}
2183145256Sjkoshy
2184147191Sjkoshystatic void
2185147191Sjkoshypmc_wait_for_pmc_idle(struct pmc *pm)
2186147191Sjkoshy{
2187233628Sfabient#ifdef DEBUG
2188147191Sjkoshy	volatile int maxloop;
2189147191Sjkoshy
2190183266Sjkoshy	maxloop = 100 * pmc_cpu_max();
2191147191Sjkoshy#endif
2192147191Sjkoshy	/*
2193147191Sjkoshy	 * Loop (with a forced context switch) till the PMC's runcount
2194147191Sjkoshy	 * comes down to zero.
2195147191Sjkoshy	 */
2196147191Sjkoshy	while (atomic_load_acq_32(&pm->pm_runcount) > 0) {
2197233628Sfabient#ifdef DEBUG
2198147191Sjkoshy		maxloop--;
2199147191Sjkoshy		KASSERT(maxloop > 0,
2200147191Sjkoshy		    ("[pmc,%d] (ri%d, rc%d) waiting too long for "
2201147191Sjkoshy			"pmc to be free", __LINE__,
2202147191Sjkoshy			PMC_TO_ROWINDEX(pm), pm->pm_runcount));
2203147191Sjkoshy#endif
2204147191Sjkoshy		pmc_force_context_switch();
2205147191Sjkoshy	}
2206147191Sjkoshy}
2207147191Sjkoshy
2208145256Sjkoshy/*
2209145256Sjkoshy * This function does the following things:
2210145256Sjkoshy *
2211145256Sjkoshy *  - detaches the PMC from hardware
2212145256Sjkoshy *  - unlinks all target threads that were attached to it
2213145256Sjkoshy *  - removes the PMC from its owner's list
2214145256Sjkoshy *  - destroy's the PMC private mutex
2215145256Sjkoshy *
2216145256Sjkoshy * Once this function completes, the given pmc pointer can be safely
2217145256Sjkoshy * FREE'd by the caller.
2218145256Sjkoshy */
2219145256Sjkoshy
2220145256Sjkoshystatic void
2221145256Sjkoshypmc_release_pmc_descriptor(struct pmc *pm)
2222145256Sjkoshy{
2223145774Sjkoshy	enum pmc_mode mode;
2224145256Sjkoshy	struct pmc_hw *phw;
2225184802Sjkoshy	u_int adjri, ri, cpu;
2226147191Sjkoshy	struct pmc_owner *po;
2227184802Sjkoshy	struct pmc_binding pb;
2228145256Sjkoshy	struct pmc_process *pp;
2229184802Sjkoshy	struct pmc_classdep *pcd;
2230145256Sjkoshy	struct pmc_target *ptgt, *tmp;
2231145256Sjkoshy
2232145256Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
2233145256Sjkoshy
2234145256Sjkoshy	KASSERT(pm, ("[pmc,%d] null pmc", __LINE__));
2235145256Sjkoshy
2236145774Sjkoshy	ri   = PMC_TO_ROWINDEX(pm);
2237184802Sjkoshy	pcd  = pmc_ri_to_classdep(md, ri, &adjri);
2238145774Sjkoshy	mode = PMC_TO_MODE(pm);
2239145256Sjkoshy
2240145256Sjkoshy	PMCDBG(PMC,REL,1, "release-pmc pmc=%p ri=%d mode=%d", pm, ri,
2241145774Sjkoshy	    mode);
2242145256Sjkoshy
2243145256Sjkoshy	/*
2244145256Sjkoshy	 * First, we take the PMC off hardware.
2245145256Sjkoshy	 */
2246145301Simp	cpu = 0;
2247145774Sjkoshy	if (PMC_IS_SYSTEM_MODE(mode)) {
2248145256Sjkoshy
2249145256Sjkoshy		/*
2250145256Sjkoshy		 * A system mode PMC runs on a specific CPU.  Switch
2251145256Sjkoshy		 * to this CPU and turn hardware off.
2252145256Sjkoshy		 */
2253145256Sjkoshy		pmc_save_cpu_binding(&pb);
2254145256Sjkoshy
2255145774Sjkoshy		cpu = PMC_TO_CPU(pm);
2256145256Sjkoshy
2257147191Sjkoshy		pmc_select_cpu(cpu);
2258145256Sjkoshy
2259147191Sjkoshy		/* switch off non-stalled CPUs */
2260147191Sjkoshy		if (pm->pm_state == PMC_STATE_RUNNING &&
2261147867Sjkoshy		    pm->pm_stalled == 0) {
2262145256Sjkoshy
2263145256Sjkoshy			phw = pmc_pcpu[cpu]->pc_hwpmcs[ri];
2264145256Sjkoshy
2265145256Sjkoshy			KASSERT(phw->phw_pmc == pm,
2266145256Sjkoshy			    ("[pmc, %d] pmc ptr ri(%d) hw(%p) pm(%p)",
2267145256Sjkoshy				__LINE__, ri, phw->phw_pmc, pm));
2268145256Sjkoshy			PMCDBG(PMC,REL,2, "stopping cpu=%d ri=%d", cpu, ri);
2269145256Sjkoshy
2270145256Sjkoshy			critical_enter();
2271184802Sjkoshy			pcd->pcd_stop_pmc(cpu, adjri);
2272145256Sjkoshy			critical_exit();
2273145256Sjkoshy		}
2274145256Sjkoshy
2275145256Sjkoshy		PMCDBG(PMC,REL,2, "decfg cpu=%d ri=%d", cpu, ri);
2276145256Sjkoshy
2277145256Sjkoshy		critical_enter();
2278184802Sjkoshy		pcd->pcd_config_pmc(cpu, adjri, NULL);
2279145256Sjkoshy		critical_exit();
2280145256Sjkoshy
2281147191Sjkoshy		/* adjust the global and process count of SS mode PMCs */
2282147191Sjkoshy		if (mode == PMC_MODE_SS && pm->pm_state == PMC_STATE_RUNNING) {
2283147191Sjkoshy			po = pm->pm_owner;
2284147191Sjkoshy			po->po_sscount--;
2285147191Sjkoshy			if (po->po_sscount == 0) {
2286147191Sjkoshy				atomic_subtract_rel_int(&pmc_ss_count, 1);
2287147191Sjkoshy				LIST_REMOVE(po, po_ssnext);
2288147191Sjkoshy			}
2289147191Sjkoshy		}
2290147191Sjkoshy
2291145256Sjkoshy		pm->pm_state = PMC_STATE_DELETED;
2292145256Sjkoshy
2293145256Sjkoshy		pmc_restore_cpu_binding(&pb);
2294145256Sjkoshy
2295147191Sjkoshy		/*
2296147191Sjkoshy		 * We could have references to this PMC structure in
2297147191Sjkoshy		 * the per-cpu sample queues.  Wait for the queue to
2298147191Sjkoshy		 * drain.
2299147191Sjkoshy		 */
2300147191Sjkoshy		pmc_wait_for_pmc_idle(pm);
2301147191Sjkoshy
2302145774Sjkoshy	} else if (PMC_IS_VIRTUAL_MODE(mode)) {
2303145256Sjkoshy
2304145256Sjkoshy		/*
2305145256Sjkoshy		 * A virtual PMC could be running on multiple CPUs at
2306145256Sjkoshy		 * a given instant.
2307145256Sjkoshy		 *
2308145256Sjkoshy		 * By marking its state as DELETED, we ensure that
2309145256Sjkoshy		 * this PMC is never further scheduled on hardware.
2310145256Sjkoshy		 *
2311145256Sjkoshy		 * Then we wait till all CPUs are done with this PMC.
2312145256Sjkoshy		 */
2313145256Sjkoshy		pm->pm_state = PMC_STATE_DELETED;
2314145256Sjkoshy
2315145256Sjkoshy
2316147191Sjkoshy		/* Wait for the PMCs runcount to come to zero. */
2317147191Sjkoshy		pmc_wait_for_pmc_idle(pm);
2318145256Sjkoshy
2319145256Sjkoshy		/*
2320145256Sjkoshy		 * At this point the PMC is off all CPUs and cannot be
2321145256Sjkoshy		 * freshly scheduled onto a CPU.  It is now safe to
2322145256Sjkoshy		 * unlink all targets from this PMC.  If a
2323145256Sjkoshy		 * process-record's refcount falls to zero, we remove
2324145256Sjkoshy		 * it from the hash table.  The module-wide SX lock
2325145256Sjkoshy		 * protects us from races.
2326145256Sjkoshy		 */
2327145256Sjkoshy		LIST_FOREACH_SAFE(ptgt, &pm->pm_targets, pt_next, tmp) {
2328145256Sjkoshy			pp = ptgt->pt_process;
2329145256Sjkoshy			pmc_unlink_target_process(pm, pp); /* frees 'ptgt' */
2330145256Sjkoshy
2331145256Sjkoshy			PMCDBG(PMC,REL,3, "pp->refcnt=%d", pp->pp_refcnt);
2332145256Sjkoshy
2333145256Sjkoshy			/*
2334145256Sjkoshy			 * If the target process record shows that no
2335145256Sjkoshy			 * PMCs are attached to it, reclaim its space.
2336145256Sjkoshy			 */
2337145256Sjkoshy
2338145256Sjkoshy			if (pp->pp_refcnt == 0) {
2339145256Sjkoshy				pmc_remove_process_descriptor(pp);
2340184205Sdes				free(pp, M_PMC);
2341145256Sjkoshy			}
2342145256Sjkoshy		}
2343145256Sjkoshy
2344145256Sjkoshy		cpu = curthread->td_oncpu; /* setup cpu for pmd_release() */
2345145256Sjkoshy
2346145256Sjkoshy	}
2347145256Sjkoshy
2348145256Sjkoshy	/*
2349145256Sjkoshy	 * Release any MD resources
2350145256Sjkoshy	 */
2351184802Sjkoshy	(void) pcd->pcd_release_pmc(cpu, adjri, pm);
2352145256Sjkoshy
2353145256Sjkoshy	/*
2354145256Sjkoshy	 * Update row disposition
2355145256Sjkoshy	 */
2356145256Sjkoshy
2357145774Sjkoshy	if (PMC_IS_SYSTEM_MODE(PMC_TO_MODE(pm)))
2358145256Sjkoshy		PMC_UNMARK_ROW_STANDALONE(ri);
2359145256Sjkoshy	else
2360145256Sjkoshy		PMC_UNMARK_ROW_THREAD(ri);
2361145256Sjkoshy
2362145256Sjkoshy	/* unlink from the owner's list */
2363147191Sjkoshy	if (pm->pm_owner) {
2364147191Sjkoshy		LIST_REMOVE(pm, pm_next);
2365147191Sjkoshy		pm->pm_owner = NULL;
2366147191Sjkoshy	}
2367145256Sjkoshy
2368145256Sjkoshy	pmc_destroy_pmc_descriptor(pm);
2369145256Sjkoshy}
2370145256Sjkoshy
2371145256Sjkoshy/*
2372145256Sjkoshy * Register an owner and a pmc.
2373145256Sjkoshy */
2374145256Sjkoshy
2375145256Sjkoshystatic int
2376145256Sjkoshypmc_register_owner(struct proc *p, struct pmc *pmc)
2377145256Sjkoshy{
2378145256Sjkoshy	struct pmc_owner *po;
2379145256Sjkoshy
2380145256Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
2381145256Sjkoshy
2382145774Sjkoshy	if ((po = pmc_find_owner_descriptor(p)) == NULL)
2383147191Sjkoshy		if ((po = pmc_allocate_owner_descriptor(p)) == NULL)
2384145256Sjkoshy			return ENOMEM;
2385145256Sjkoshy
2386145256Sjkoshy	KASSERT(pmc->pm_owner == NULL,
2387145256Sjkoshy	    ("[pmc,%d] attempting to own an initialized PMC", __LINE__));
2388145256Sjkoshy	pmc->pm_owner  = po;
2389145256Sjkoshy
2390147191Sjkoshy	LIST_INSERT_HEAD(&po->po_pmcs, pmc, pm_next);
2391145256Sjkoshy
2392145256Sjkoshy	PROC_LOCK(p);
2393145256Sjkoshy	p->p_flag |= P_HWPMC;
2394145256Sjkoshy	PROC_UNLOCK(p);
2395145256Sjkoshy
2396147191Sjkoshy	if (po->po_flags & PMC_PO_OWNS_LOGFILE)
2397147191Sjkoshy		pmclog_process_pmcallocate(pmc);
2398145256Sjkoshy
2399147191Sjkoshy	PMCDBG(PMC,REG,1, "register-owner pmc-owner=%p pmc=%p",
2400147191Sjkoshy	    po, pmc);
2401147191Sjkoshy
2402145256Sjkoshy	return 0;
2403145256Sjkoshy}
2404145256Sjkoshy
2405145256Sjkoshy/*
2406145256Sjkoshy * Return the current row disposition:
2407145256Sjkoshy * == 0 => FREE
2408145256Sjkoshy *  > 0 => PROCESS MODE
2409145256Sjkoshy *  < 0 => SYSTEM MODE
2410145256Sjkoshy */
2411145256Sjkoshy
2412145256Sjkoshyint
2413145256Sjkoshypmc_getrowdisp(int ri)
2414145256Sjkoshy{
2415145256Sjkoshy	return pmc_pmcdisp[ri];
2416145256Sjkoshy}
2417145256Sjkoshy
2418145256Sjkoshy/*
2419145256Sjkoshy * Check if a PMC at row index 'ri' can be allocated to the current
2420145256Sjkoshy * process.
2421145256Sjkoshy *
2422145256Sjkoshy * Allocation can fail if:
2423145256Sjkoshy *   - the current process is already being profiled by a PMC at index 'ri',
2424145256Sjkoshy *     attached to it via OP_PMCATTACH.
2425145256Sjkoshy *   - the current process has already allocated a PMC at index 'ri'
2426145256Sjkoshy *     via OP_ALLOCATE.
2427145256Sjkoshy */
2428145256Sjkoshy
2429145256Sjkoshystatic int
2430145774Sjkoshypmc_can_allocate_rowindex(struct proc *p, unsigned int ri, int cpu)
2431145256Sjkoshy{
2432145774Sjkoshy	enum pmc_mode mode;
2433145774Sjkoshy	struct pmc *pm;
2434145256Sjkoshy	struct pmc_owner *po;
2435145256Sjkoshy	struct pmc_process *pp;
2436145256Sjkoshy
2437145774Sjkoshy	PMCDBG(PMC,ALR,1, "can-allocate-rowindex proc=%p (%d, %s) ri=%d "
2438145774Sjkoshy	    "cpu=%d", p, p->p_pid, p->p_comm, ri, cpu);
2439145256Sjkoshy
2440145774Sjkoshy	/*
2441145774Sjkoshy	 * We shouldn't have already allocated a process-mode PMC at
2442145774Sjkoshy	 * row index 'ri'.
2443145774Sjkoshy	 *
2444145774Sjkoshy	 * We shouldn't have allocated a system-wide PMC on the same
2445145774Sjkoshy	 * CPU and same RI.
2446145774Sjkoshy	 */
2447145256Sjkoshy	if ((po = pmc_find_owner_descriptor(p)) != NULL)
2448147191Sjkoshy		LIST_FOREACH(pm, &po->po_pmcs, pm_next) {
2449145774Sjkoshy		    if (PMC_TO_ROWINDEX(pm) == ri) {
2450145774Sjkoshy			    mode = PMC_TO_MODE(pm);
2451145774Sjkoshy			    if (PMC_IS_VIRTUAL_MODE(mode))
2452145774Sjkoshy				    return EEXIST;
2453145774Sjkoshy			    if (PMC_IS_SYSTEM_MODE(mode) &&
2454145774Sjkoshy				(int) PMC_TO_CPU(pm) == cpu)
2455145774Sjkoshy				    return EEXIST;
2456145774Sjkoshy		    }
2457145774Sjkoshy	        }
2458145256Sjkoshy
2459145774Sjkoshy	/*
2460145774Sjkoshy	 * We also shouldn't be the target of any PMC at this index
2461145774Sjkoshy	 * since otherwise a PMC_ATTACH to ourselves will fail.
2462145774Sjkoshy	 */
2463145256Sjkoshy	if ((pp = pmc_find_process_descriptor(p, 0)) != NULL)
2464145256Sjkoshy		if (pp->pp_pmcs[ri].pp_pmc)
2465145256Sjkoshy			return EEXIST;
2466145256Sjkoshy
2467145256Sjkoshy	PMCDBG(PMC,ALR,2, "can-allocate-rowindex proc=%p (%d, %s) ri=%d ok",
2468145256Sjkoshy	    p, p->p_pid, p->p_comm, ri);
2469145256Sjkoshy
2470145256Sjkoshy	return 0;
2471145256Sjkoshy}
2472145256Sjkoshy
2473145256Sjkoshy/*
2474145256Sjkoshy * Check if a given PMC at row index 'ri' can be currently used in
2475145256Sjkoshy * mode 'mode'.
2476145256Sjkoshy */
2477145256Sjkoshy
2478145256Sjkoshystatic int
2479145256Sjkoshypmc_can_allocate_row(int ri, enum pmc_mode mode)
2480145256Sjkoshy{
2481145256Sjkoshy	enum pmc_disp	disp;
2482145256Sjkoshy
2483145256Sjkoshy	sx_assert(&pmc_sx, SX_XLOCKED);
2484145256Sjkoshy
2485145256Sjkoshy	PMCDBG(PMC,ALR,1, "can-allocate-row ri=%d mode=%d", ri, mode);
2486145256Sjkoshy
2487145256Sjkoshy	if (PMC_IS_SYSTEM_MODE(mode))
2488145256Sjkoshy		disp = PMC_DISP_STANDALONE;
2489145256Sjkoshy	else
2490145256Sjkoshy		disp = PMC_DISP_THREAD;
2491145256Sjkoshy
2492145256Sjkoshy	/*
2493145256Sjkoshy	 * check disposition for PMC row 'ri':
2494145256Sjkoshy	 *
2495145256Sjkoshy	 * Expected disposition		Row-disposition		Result
2496145256Sjkoshy	 *
2497145256Sjkoshy	 * STANDALONE			STANDALONE or FREE	proceed
2498145256Sjkoshy	 * STANDALONE			THREAD			fail
2499145256Sjkoshy	 * THREAD			THREAD or FREE		proceed
2500145256Sjkoshy	 * THREAD			STANDALONE		fail
2501145256Sjkoshy	 */
2502145256Sjkoshy
2503145256Sjkoshy	if (!PMC_ROW_DISP_IS_FREE(ri) &&
2504145256Sjkoshy	    !(disp == PMC_DISP_THREAD && PMC_ROW_DISP_IS_THREAD(ri)) &&
2505145256Sjkoshy	    !(disp == PMC_DISP_STANDALONE && PMC_ROW_DISP_IS_STANDALONE(ri)))
2506145256Sjkoshy		return EBUSY;
2507145256Sjkoshy
2508145256Sjkoshy	/*
2509145256Sjkoshy	 * All OK
2510145256Sjkoshy	 */
2511145256Sjkoshy
2512145256Sjkoshy	PMCDBG(PMC,ALR,2, "can-allocate-row ri=%d mode=%d ok", ri, mode);
2513145256Sjkoshy
2514145256Sjkoshy	return 0;
2515145256Sjkoshy
2516145256Sjkoshy}
2517145256Sjkoshy
2518145256Sjkoshy/*
2519145774Sjkoshy * Find a PMC descriptor with user handle 'pmcid' for thread 'td'.
2520145256Sjkoshy */
2521145256Sjkoshy
2522145256Sjkoshystatic struct pmc *
2523145256Sjkoshypmc_find_pmc_descriptor_in_process(struct pmc_owner *po, pmc_id_t pmcid)
2524145256Sjkoshy{
2525147191Sjkoshy	struct pmc *pm;
2526145256Sjkoshy
2527145774Sjkoshy	KASSERT(PMC_ID_TO_ROWINDEX(pmcid) < md->pmd_npmc,
2528145774Sjkoshy	    ("[pmc,%d] Illegal pmc index %d (max %d)", __LINE__,
2529145774Sjkoshy		PMC_ID_TO_ROWINDEX(pmcid), md->pmd_npmc));
2530145256Sjkoshy
2531147191Sjkoshy	LIST_FOREACH(pm, &po->po_pmcs, pm_next)
2532147191Sjkoshy	    if (pm->pm_id == pmcid)
2533147191Sjkoshy		    return pm;
2534145256Sjkoshy
2535145256Sjkoshy	return NULL;
2536145256Sjkoshy}
2537145256Sjkoshy
2538145256Sjkoshystatic int
2539145256Sjkoshypmc_find_pmc(pmc_id_t pmcid, struct pmc **pmc)
2540145256Sjkoshy{
2541145256Sjkoshy
2542145256Sjkoshy	struct pmc *pm;
2543145256Sjkoshy	struct pmc_owner *po;
2544145256Sjkoshy
2545145256Sjkoshy	PMCDBG(PMC,FND,1, "find-pmc id=%d", pmcid);
2546145256Sjkoshy
2547145256Sjkoshy	if ((po = pmc_find_owner_descriptor(curthread->td_proc)) == NULL)
2548145256Sjkoshy		return ESRCH;
2549145256Sjkoshy
2550145256Sjkoshy	if ((pm = pmc_find_pmc_descriptor_in_process(po, pmcid)) == NULL)
2551145256Sjkoshy		return EINVAL;
2552145256Sjkoshy
2553145256Sjkoshy	PMCDBG(PMC,FND,2, "find-pmc id=%d -> pmc=%p", pmcid, pm);
2554145256Sjkoshy
2555145256Sjkoshy	*pmc = pm;
2556145256Sjkoshy	return 0;
2557145256Sjkoshy}
2558145256Sjkoshy
2559145256Sjkoshy/*
2560145256Sjkoshy * Start a PMC.
2561145256Sjkoshy */
2562145256Sjkoshy
2563145256Sjkoshystatic int
2564145256Sjkoshypmc_start(struct pmc *pm)
2565145256Sjkoshy{
2566145774Sjkoshy	enum pmc_mode mode;
2567147191Sjkoshy	struct pmc_owner *po;
2568145256Sjkoshy	struct pmc_binding pb;
2569184802Sjkoshy	struct pmc_classdep *pcd;
2570184802Sjkoshy	int adjri, error, cpu, ri;
2571145256Sjkoshy
2572145256Sjkoshy	KASSERT(pm != NULL,
2573145256Sjkoshy	    ("[pmc,%d] null pm", __LINE__));
2574145256Sjkoshy
2575145774Sjkoshy	mode = PMC_TO_MODE(pm);
2576145774Sjkoshy	ri   = PMC_TO_ROWINDEX(pm);
2577184802Sjkoshy	pcd  = pmc_ri_to_classdep(md, ri, &adjri);
2578184802Sjkoshy
2579145774Sjkoshy	error = 0;
2580145256Sjkoshy
2581145774Sjkoshy	PMCDBG(PMC,OPS,1, "start pmc=%p mode=%d ri=%d", pm, mode, ri);
2582145774Sjkoshy
2583147191Sjkoshy	po = pm->pm_owner;
2584145256Sjkoshy
2585174395Sjkoshy	/*
2586174395Sjkoshy	 * Disallow PMCSTART if a logfile is required but has not been
2587174395Sjkoshy	 * configured yet.
2588174395Sjkoshy	 */
2589174395Sjkoshy	if ((pm->pm_flags & PMC_F_NEEDS_LOGFILE) &&
2590174395Sjkoshy	    (po->po_flags & PMC_PO_OWNS_LOGFILE) == 0)
2591184802Sjkoshy		return (EDOOFUS);	/* programming error */
2592174395Sjkoshy
2593174395Sjkoshy	/*
2594174395Sjkoshy	 * If this is a sampling mode PMC, log mapping information for
2595174395Sjkoshy	 * the kernel modules that are currently loaded.
2596174395Sjkoshy	 */
2597174395Sjkoshy	if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
2598174395Sjkoshy	    pmc_log_kernel_mappings(pm);
2599174395Sjkoshy
2600145774Sjkoshy	if (PMC_IS_VIRTUAL_MODE(mode)) {
2601145256Sjkoshy
2602145256Sjkoshy		/*
2603147191Sjkoshy		 * If a PMCATTACH has never been done on this PMC,
2604147191Sjkoshy		 * attach it to its owner process.
2605145256Sjkoshy		 */
2606145256Sjkoshy
2607145256Sjkoshy		if (LIST_EMPTY(&pm->pm_targets))
2608147191Sjkoshy			error = (pm->pm_flags & PMC_F_ATTACH_DONE) ? ESRCH :
2609147191Sjkoshy			    pmc_attach_process(po->po_owner, pm);
2610145256Sjkoshy
2611145774Sjkoshy		/*
2612147191Sjkoshy		 * If the PMC is attached to its owner, then force a context
2613147191Sjkoshy		 * switch to ensure that the MD state gets set correctly.
2614145256Sjkoshy		 */
2615145256Sjkoshy
2616147191Sjkoshy		if (error == 0) {
2617147191Sjkoshy			pm->pm_state = PMC_STATE_RUNNING;
2618147191Sjkoshy			if (pm->pm_flags & PMC_F_ATTACHED_TO_OWNER)
2619147191Sjkoshy				pmc_force_context_switch();
2620147191Sjkoshy		}
2621147191Sjkoshy
2622184802Sjkoshy		return (error);
2623147191Sjkoshy	}
2624145256Sjkoshy
2625147191Sjkoshy
2626147191Sjkoshy	/*
2627147191Sjkoshy	 * A system-wide PMC.
2628174395Sjkoshy	 *
2629147191Sjkoshy	 * Add the owner to the global list if this is a system-wide
2630147191Sjkoshy	 * sampling PMC.
2631147191Sjkoshy	 */
2632147191Sjkoshy
2633147191Sjkoshy	if (mode == PMC_MODE_SS) {
2634147191Sjkoshy		if (po->po_sscount == 0) {
2635147191Sjkoshy			LIST_INSERT_HEAD(&pmc_ss_owners, po, po_ssnext);
2636147191Sjkoshy			atomic_add_rel_int(&pmc_ss_count, 1);
2637147191Sjkoshy			PMCDBG(PMC,OPS,1, "po=%p in global list", po);
2638147191Sjkoshy		}
2639147191Sjkoshy		po->po_sscount++;
2640145256Sjkoshy
2641207484Srstone		/*
2642207484Srstone		 * Log mapping information for all existing processes in the
2643207484Srstone		 * system.  Subsequent mappings are logged as they happen;
2644207484Srstone		 * see pmc_process_mmap().
2645207484Srstone		 */
2646207484Srstone		if (po->po_logprocmaps == 0) {
2647207484Srstone			pmc_log_all_process_mappings(po);
2648207484Srstone			po->po_logprocmaps = 1;
2649207484Srstone		}
2650201021Sjkoshy	}
2651157144Sjkoshy
2652145256Sjkoshy	/*
2653147191Sjkoshy	 * Move to the CPU associated with this
2654145256Sjkoshy	 * PMC, and start the hardware.
2655145256Sjkoshy	 */
2656145256Sjkoshy
2657145256Sjkoshy	pmc_save_cpu_binding(&pb);
2658145256Sjkoshy
2659145774Sjkoshy	cpu = PMC_TO_CPU(pm);
2660145256Sjkoshy
2661183266Sjkoshy	if (!pmc_cpu_is_active(cpu))
2662184802Sjkoshy		return (ENXIO);
2663145256Sjkoshy
2664145256Sjkoshy	pmc_select_cpu(cpu);
2665145256Sjkoshy
2666145256Sjkoshy	/*
2667145256Sjkoshy	 * global PMCs are configured at allocation time
2668145256Sjkoshy	 * so write out the initial value and start the PMC.
2669145256Sjkoshy	 */
2670145256Sjkoshy
2671147191Sjkoshy	pm->pm_state = PMC_STATE_RUNNING;
2672147191Sjkoshy
2673145774Sjkoshy	critical_enter();
2674184802Sjkoshy	if ((error = pcd->pcd_write_pmc(cpu, adjri,
2675145774Sjkoshy		 PMC_IS_SAMPLING_MODE(mode) ?
2676145256Sjkoshy		 pm->pm_sc.pm_reloadcount :
2677145256Sjkoshy		 pm->pm_sc.pm_initial)) == 0)
2678184802Sjkoshy		error = pcd->pcd_start_pmc(cpu, adjri);
2679145774Sjkoshy	critical_exit();
2680145256Sjkoshy
2681145256Sjkoshy	pmc_restore_cpu_binding(&pb);
2682145256Sjkoshy
2683184802Sjkoshy	return (error);
2684145256Sjkoshy}
2685145256Sjkoshy
2686145256Sjkoshy/*
2687145256Sjkoshy * Stop a PMC.
2688145256Sjkoshy */
2689145256Sjkoshy
2690145256Sjkoshystatic int
2691145256Sjkoshypmc_stop(struct pmc *pm)
2692145256Sjkoshy{
2693147191Sjkoshy	struct pmc_owner *po;
2694145256Sjkoshy	struct pmc_binding pb;
2695184802Sjkoshy	struct pmc_classdep *pcd;
2696184802Sjkoshy	int adjri, cpu, error, ri;
2697145256Sjkoshy
2698145256Sjkoshy	KASSERT(pm != NULL, ("[pmc,%d] null pmc", __LINE__));
2699145256Sjkoshy
2700145774Sjkoshy	PMCDBG(PMC,OPS,1, "stop pmc=%p mode=%d ri=%d", pm,
2701145774Sjkoshy	    PMC_TO_MODE(pm), PMC_TO_ROWINDEX(pm));
2702145256Sjkoshy
2703145256Sjkoshy	pm->pm_state = PMC_STATE_STOPPED;
2704145256Sjkoshy
2705145256Sjkoshy	/*
2706145256Sjkoshy	 * If the PMC is a virtual mode one, changing the state to
2707145256Sjkoshy	 * non-RUNNING is enough to ensure that the PMC never gets
2708145256Sjkoshy	 * scheduled.
2709145256Sjkoshy	 *
2710145256Sjkoshy	 * If this PMC is current running on a CPU, then it will
2711145256Sjkoshy	 * handled correctly at the time its target process is context
2712145256Sjkoshy	 * switched out.
2713145256Sjkoshy	 */
2714145256Sjkoshy
2715145774Sjkoshy	if (PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm)))
2716145256Sjkoshy		return 0;
2717145256Sjkoshy
2718145256Sjkoshy	/*
2719145256Sjkoshy	 * A system-mode PMC.  Move to the CPU associated with
2720145256Sjkoshy	 * this PMC, and stop the hardware.  We update the
2721145256Sjkoshy	 * 'initial count' so that a subsequent PMCSTART will
2722145256Sjkoshy	 * resume counting from the current hardware count.
2723145256Sjkoshy	 */
2724145256Sjkoshy
2725145256Sjkoshy	pmc_save_cpu_binding(&pb);
2726145256Sjkoshy
2727145774Sjkoshy	cpu = PMC_TO_CPU(pm);
2728145256Sjkoshy
2729183266Sjkoshy	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
2730145774Sjkoshy	    ("[pmc,%d] illegal cpu=%d", __LINE__, cpu));
2731145774Sjkoshy
2732183266Sjkoshy	if (!pmc_cpu_is_active(cpu))
2733145256Sjkoshy		return ENXIO;
2734145256Sjkoshy
2735145256Sjkoshy	pmc_select_cpu(cpu);
2736145256Sjkoshy
2737145774Sjkoshy	ri = PMC_TO_ROWINDEX(pm);
2738184802Sjkoshy	pcd = pmc_ri_to_classdep(md, ri, &adjri);
2739145256Sjkoshy
2740145774Sjkoshy	critical_enter();
2741184802Sjkoshy	if ((error = pcd->pcd_stop_pmc(cpu, adjri)) == 0)
2742184802Sjkoshy		error = pcd->pcd_read_pmc(cpu, adjri, &pm->pm_sc.pm_initial);
2743145774Sjkoshy	critical_exit();
2744145774Sjkoshy
2745145256Sjkoshy	pmc_restore_cpu_binding(&pb);
2746145256Sjkoshy
2747147191Sjkoshy	po = pm->pm_owner;
2748147191Sjkoshy
2749147191Sjkoshy	/* remove this owner from the global list of SS PMC owners */
2750147191Sjkoshy	if (PMC_TO_MODE(pm) == PMC_MODE_SS) {
2751147191Sjkoshy		po->po_sscount--;
2752147191Sjkoshy		if (po->po_sscount == 0) {
2753147191Sjkoshy			atomic_subtract_rel_int(&pmc_ss_count, 1);
2754147191Sjkoshy			LIST_REMOVE(po, po_ssnext);
2755147191Sjkoshy			PMCDBG(PMC,OPS,2,"po=%p removed from global list", po);
2756147191Sjkoshy		}
2757147191Sjkoshy	}
2758147191Sjkoshy
2759184802Sjkoshy	return (error);
2760145256Sjkoshy}
2761145256Sjkoshy
2762145256Sjkoshy
2763153110Sru#ifdef	DEBUG
2764145256Sjkoshystatic const char *pmc_op_to_name[] = {
2765145256Sjkoshy#undef	__PMC_OP
2766145256Sjkoshy#define	__PMC_OP(N, D)	#N ,
2767145256Sjkoshy	__PMC_OPS()
2768145256Sjkoshy	NULL
2769145256Sjkoshy};
2770145256Sjkoshy#endif
2771145256Sjkoshy
2772145256Sjkoshy/*
2773145256Sjkoshy * The syscall interface
2774145256Sjkoshy */
2775145256Sjkoshy
2776145256Sjkoshy#define	PMC_GET_SX_XLOCK(...) do {		\
2777145256Sjkoshy	sx_xlock(&pmc_sx);			\
2778145256Sjkoshy	if (pmc_hook == NULL) {			\
2779145256Sjkoshy		sx_xunlock(&pmc_sx);		\
2780145256Sjkoshy		return __VA_ARGS__;		\
2781145256Sjkoshy	}					\
2782145256Sjkoshy} while (0)
2783145256Sjkoshy
2784145256Sjkoshy#define	PMC_DOWNGRADE_SX() do {			\
2785145256Sjkoshy	sx_downgrade(&pmc_sx);			\
2786145256Sjkoshy	is_sx_downgraded = 1;			\
2787145256Sjkoshy} while (0)
2788145256Sjkoshy
2789145256Sjkoshystatic int
2790145256Sjkoshypmc_syscall_handler(struct thread *td, void *syscall_args)
2791145256Sjkoshy{
2792195005Sattilio	int error, is_sx_downgraded, is_sx_locked, op;
2793145256Sjkoshy	struct pmc_syscall_args *c;
2794145256Sjkoshy	void *arg;
2795145256Sjkoshy
2796145256Sjkoshy	PMC_GET_SX_XLOCK(ENOSYS);
2797145256Sjkoshy
2798147191Sjkoshy	DROP_GIANT();
2799147191Sjkoshy
2800145256Sjkoshy	is_sx_downgraded = 0;
2801195005Sattilio	is_sx_locked = 1;
2802145256Sjkoshy
2803145256Sjkoshy	c = (struct pmc_syscall_args *) syscall_args;
2804145256Sjkoshy
2805145256Sjkoshy	op = c->pmop_code;
2806145256Sjkoshy	arg = c->pmop_data;
2807145256Sjkoshy
2808145256Sjkoshy	PMCDBG(MOD,PMS,1, "syscall op=%d \"%s\" arg=%p", op,
2809145256Sjkoshy	    pmc_op_to_name[op], arg);
2810145256Sjkoshy
2811145256Sjkoshy	error = 0;
2812145256Sjkoshy	atomic_add_int(&pmc_stats.pm_syscalls, 1);
2813145256Sjkoshy
2814145256Sjkoshy	switch(op)
2815145256Sjkoshy	{
2816145256Sjkoshy
2817145256Sjkoshy
2818145256Sjkoshy	/*
2819145256Sjkoshy	 * Configure a log file.
2820145256Sjkoshy	 *
2821145256Sjkoshy	 * XXX This OP will be reworked.
2822145256Sjkoshy	 */
2823145256Sjkoshy
2824145256Sjkoshy	case PMC_OP_CONFIGURELOG:
2825145256Sjkoshy	{
2826157144Sjkoshy		struct proc *p;
2827156466Sjkoshy		struct pmc *pm;
2828145256Sjkoshy		struct pmc_owner *po;
2829145256Sjkoshy		struct pmc_op_configurelog cl;
2830145256Sjkoshy
2831145256Sjkoshy		sx_assert(&pmc_sx, SX_XLOCKED);
2832145256Sjkoshy
2833145256Sjkoshy		if ((error = copyin(arg, &cl, sizeof(cl))) != 0)
2834145256Sjkoshy			break;
2835145256Sjkoshy
2836145256Sjkoshy		/* mark this process as owning a log file */
2837145256Sjkoshy		p = td->td_proc;
2838145256Sjkoshy		if ((po = pmc_find_owner_descriptor(p)) == NULL)
2839147191Sjkoshy			if ((po = pmc_allocate_owner_descriptor(p)) == NULL) {
2840147191Sjkoshy				error = ENOMEM;
2841147191Sjkoshy				break;
2842147191Sjkoshy			}
2843145256Sjkoshy
2844147191Sjkoshy		/*
2845147191Sjkoshy		 * If a valid fd was passed in, try to configure that,
2846147191Sjkoshy		 * otherwise if 'fd' was less than zero and there was
2847147191Sjkoshy		 * a log file configured, flush its buffers and
2848147191Sjkoshy		 * de-configure it.
2849147191Sjkoshy		 */
2850195005Sattilio		if (cl.pm_logfd >= 0) {
2851195005Sattilio			sx_xunlock(&pmc_sx);
2852195005Sattilio			is_sx_locked = 0;
2853185363Sjkoshy			error = pmclog_configure_log(md, po, cl.pm_logfd);
2854195005Sattilio		} else if (po->po_flags & PMC_PO_OWNS_LOGFILE) {
2855147191Sjkoshy			pmclog_process_closelog(po);
2856226514Sfabient			error = pmclog_close(po);
2857156466Sjkoshy			if (error == 0) {
2858156466Sjkoshy				LIST_FOREACH(pm, &po->po_pmcs, pm_next)
2859156834Sjkoshy				    if (pm->pm_flags & PMC_F_NEEDS_LOGFILE &&
2860156834Sjkoshy					pm->pm_state == PMC_STATE_RUNNING)
2861156466Sjkoshy					    pmc_stop(pm);
2862147191Sjkoshy				error = pmclog_deconfigure_log(po);
2863156466Sjkoshy			}
2864147191Sjkoshy		} else
2865147191Sjkoshy			error = EINVAL;
2866157144Sjkoshy
2867157144Sjkoshy		if (error)
2868157144Sjkoshy			break;
2869147191Sjkoshy	}
2870147191Sjkoshy	break;
2871147191Sjkoshy
2872147191Sjkoshy	/*
2873147191Sjkoshy	 * Flush a log file.
2874147191Sjkoshy	 */
2875147191Sjkoshy
2876147191Sjkoshy	case PMC_OP_FLUSHLOG:
2877147191Sjkoshy	{
2878147191Sjkoshy		struct pmc_owner *po;
2879147191Sjkoshy
2880147191Sjkoshy		sx_assert(&pmc_sx, SX_XLOCKED);
2881147191Sjkoshy
2882147191Sjkoshy		if ((po = pmc_find_owner_descriptor(td->td_proc)) == NULL) {
2883147191Sjkoshy			error = EINVAL;
2884145256Sjkoshy			break;
2885147191Sjkoshy		}
2886145256Sjkoshy
2887147191Sjkoshy		error = pmclog_flush(po);
2888145256Sjkoshy	}
2889145256Sjkoshy	break;
2890145256Sjkoshy
2891145256Sjkoshy	/*
2892226514Sfabient	 * Close a log file.
2893226514Sfabient	 */
2894226514Sfabient
2895226514Sfabient	case PMC_OP_CLOSELOG:
2896226514Sfabient	{
2897226514Sfabient		struct pmc_owner *po;
2898226514Sfabient
2899226514Sfabient		sx_assert(&pmc_sx, SX_XLOCKED);
2900226514Sfabient
2901226514Sfabient		if ((po = pmc_find_owner_descriptor(td->td_proc)) == NULL) {
2902226514Sfabient			error = EINVAL;
2903226514Sfabient			break;
2904226514Sfabient		}
2905226514Sfabient
2906226514Sfabient		error = pmclog_close(po);
2907226514Sfabient	}
2908226514Sfabient	break;
2909226514Sfabient
2910226514Sfabient	/*
2911145256Sjkoshy	 * Retrieve hardware configuration.
2912145256Sjkoshy	 */
2913145256Sjkoshy
2914145256Sjkoshy	case PMC_OP_GETCPUINFO:	/* CPU information */
2915145256Sjkoshy	{
2916145256Sjkoshy		struct pmc_op_getcpuinfo gci;
2917184802Sjkoshy		struct pmc_classinfo *pci;
2918184802Sjkoshy		struct pmc_classdep *pcd;
2919184802Sjkoshy		int cl;
2920145256Sjkoshy
2921145256Sjkoshy		gci.pm_cputype = md->pmd_cputype;
2922183266Sjkoshy		gci.pm_ncpu    = pmc_cpu_max();
2923145256Sjkoshy		gci.pm_npmc    = md->pmd_npmc;
2924145256Sjkoshy		gci.pm_nclass  = md->pmd_nclass;
2925184802Sjkoshy		pci = gci.pm_classes;
2926184802Sjkoshy		pcd = md->pmd_classdep;
2927184802Sjkoshy		for (cl = 0; cl < md->pmd_nclass; cl++, pci++, pcd++) {
2928184802Sjkoshy			pci->pm_caps  = pcd->pcd_caps;
2929184802Sjkoshy			pci->pm_class = pcd->pcd_class;
2930184802Sjkoshy			pci->pm_width = pcd->pcd_width;
2931184802Sjkoshy			pci->pm_num   = pcd->pcd_num;
2932184802Sjkoshy		}
2933145256Sjkoshy		error = copyout(&gci, arg, sizeof(gci));
2934145256Sjkoshy	}
2935145256Sjkoshy	break;
2936145256Sjkoshy
2937233628Sfabient	/*
2938233628Sfabient	 * Retrieve soft events list.
2939233628Sfabient	 */
2940233628Sfabient	case PMC_OP_GETDYNEVENTINFO:
2941233628Sfabient	{
2942233628Sfabient		enum pmc_class			cl;
2943233628Sfabient		enum pmc_event			ev;
2944233628Sfabient		struct pmc_op_getdyneventinfo	*gei;
2945233628Sfabient		struct pmc_dyn_event_descr	dev;
2946233628Sfabient		struct pmc_soft			*ps;
2947233628Sfabient		uint32_t			nevent;
2948145256Sjkoshy
2949233628Sfabient		sx_assert(&pmc_sx, SX_LOCKED);
2950233628Sfabient
2951233628Sfabient		gei = (struct pmc_op_getdyneventinfo *) arg;
2952233628Sfabient
2953233628Sfabient		if ((error = copyin(&gei->pm_class, &cl, sizeof(cl))) != 0)
2954233628Sfabient			break;
2955233628Sfabient
2956233628Sfabient		/* Only SOFT class is dynamic. */
2957233628Sfabient		if (cl != PMC_CLASS_SOFT) {
2958233628Sfabient			error = EINVAL;
2959233628Sfabient			break;
2960233628Sfabient		}
2961233628Sfabient
2962233628Sfabient		nevent = 0;
2963245339Ssbruno		for (ev = PMC_EV_SOFT_FIRST; (int)ev <= PMC_EV_SOFT_LAST; ev++) {
2964233628Sfabient			ps = pmc_soft_ev_acquire(ev);
2965233628Sfabient			if (ps == NULL)
2966233628Sfabient				continue;
2967233628Sfabient			bcopy(&ps->ps_ev, &dev, sizeof(dev));
2968233628Sfabient			pmc_soft_ev_release(ps);
2969233628Sfabient
2970233628Sfabient			error = copyout(&dev,
2971233628Sfabient			    &gei->pm_events[nevent],
2972233628Sfabient			    sizeof(struct pmc_dyn_event_descr));
2973233628Sfabient			if (error != 0)
2974233628Sfabient				break;
2975233628Sfabient			nevent++;
2976233628Sfabient		}
2977233628Sfabient		if (error != 0)
2978233628Sfabient			break;
2979233628Sfabient
2980233628Sfabient		error = copyout(&nevent, &gei->pm_nevent,
2981233628Sfabient		    sizeof(nevent));
2982233628Sfabient	}
2983233628Sfabient	break;
2984233628Sfabient
2985145256Sjkoshy	/*
2986145256Sjkoshy	 * Get module statistics
2987145256Sjkoshy	 */
2988145256Sjkoshy
2989145256Sjkoshy	case PMC_OP_GETDRIVERSTATS:
2990145256Sjkoshy	{
2991145256Sjkoshy		struct pmc_op_getdriverstats gms;
2992145256Sjkoshy
2993145256Sjkoshy		bcopy(&pmc_stats, &gms, sizeof(gms));
2994145256Sjkoshy		error = copyout(&gms, arg, sizeof(gms));
2995145256Sjkoshy	}
2996145256Sjkoshy	break;
2997145256Sjkoshy
2998145256Sjkoshy
2999145256Sjkoshy	/*
3000145256Sjkoshy	 * Retrieve module version number
3001145256Sjkoshy	 */
3002145256Sjkoshy
3003145256Sjkoshy	case PMC_OP_GETMODULEVERSION:
3004145256Sjkoshy	{
3005147191Sjkoshy		uint32_t cv, modv;
3006147191Sjkoshy
3007147191Sjkoshy		/* retrieve the client's idea of the ABI version */
3008147191Sjkoshy		if ((error = copyin(arg, &cv, sizeof(uint32_t))) != 0)
3009147191Sjkoshy			break;
3010147191Sjkoshy		/* don't service clients newer than our driver */
3011147191Sjkoshy		modv = PMC_VERSION;
3012147191Sjkoshy		if ((cv & 0xFFFF0000) > (modv & 0xFFFF0000)) {
3013147191Sjkoshy			error = EPROGMISMATCH;
3014147191Sjkoshy			break;
3015147191Sjkoshy		}
3016147191Sjkoshy		error = copyout(&modv, arg, sizeof(int));
3017145256Sjkoshy	}
3018145256Sjkoshy	break;
3019145256Sjkoshy
3020145256Sjkoshy
3021145256Sjkoshy	/*
3022145256Sjkoshy	 * Retrieve the state of all the PMCs on a given
3023145256Sjkoshy	 * CPU.
3024145256Sjkoshy	 */
3025145256Sjkoshy
3026145256Sjkoshy	case PMC_OP_GETPMCINFO:
3027145256Sjkoshy	{
3028184802Sjkoshy		int ari;
3029184802Sjkoshy		struct pmc *pm;
3030184802Sjkoshy		size_t pmcinfo_size;
3031145256Sjkoshy		uint32_t cpu, n, npmc;
3032184802Sjkoshy		struct pmc_owner *po;
3033184802Sjkoshy		struct pmc_binding pb;
3034184802Sjkoshy		struct pmc_classdep *pcd;
3035145256Sjkoshy		struct pmc_info *p, *pmcinfo;
3036145256Sjkoshy		struct pmc_op_getpmcinfo *gpi;
3037145256Sjkoshy
3038145256Sjkoshy		PMC_DOWNGRADE_SX();
3039145256Sjkoshy
3040145256Sjkoshy		gpi = (struct pmc_op_getpmcinfo *) arg;
3041145256Sjkoshy
3042145256Sjkoshy		if ((error = copyin(&gpi->pm_cpu, &cpu, sizeof(cpu))) != 0)
3043145256Sjkoshy			break;
3044145256Sjkoshy
3045183266Sjkoshy		if (cpu >= pmc_cpu_max()) {
3046145256Sjkoshy			error = EINVAL;
3047145256Sjkoshy			break;
3048145256Sjkoshy		}
3049145256Sjkoshy
3050183266Sjkoshy		if (!pmc_cpu_is_active(cpu)) {
3051145256Sjkoshy			error = ENXIO;
3052145256Sjkoshy			break;
3053145256Sjkoshy		}
3054145256Sjkoshy
3055145256Sjkoshy		/* switch to CPU 'cpu' */
3056145256Sjkoshy		pmc_save_cpu_binding(&pb);
3057145256Sjkoshy		pmc_select_cpu(cpu);
3058145256Sjkoshy
3059145256Sjkoshy		npmc = md->pmd_npmc;
3060145256Sjkoshy
3061145256Sjkoshy		pmcinfo_size = npmc * sizeof(struct pmc_info);
3062184214Sdes		pmcinfo = malloc(pmcinfo_size, M_PMC, M_WAITOK);
3063145256Sjkoshy
3064145256Sjkoshy		p = pmcinfo;
3065145256Sjkoshy
3066145256Sjkoshy		for (n = 0; n < md->pmd_npmc; n++, p++) {
3067145256Sjkoshy
3068184802Sjkoshy			pcd = pmc_ri_to_classdep(md, n, &ari);
3069184802Sjkoshy
3070184802Sjkoshy			KASSERT(pcd != NULL,
3071184802Sjkoshy			    ("[pmc,%d] null pcd ri=%d", __LINE__, n));
3072184802Sjkoshy
3073184802Sjkoshy			if ((error = pcd->pcd_describe(cpu, ari, p, &pm)) != 0)
3074145256Sjkoshy				break;
3075145256Sjkoshy
3076145256Sjkoshy			if (PMC_ROW_DISP_IS_STANDALONE(n))
3077145256Sjkoshy				p->pm_rowdisp = PMC_DISP_STANDALONE;
3078145256Sjkoshy			else if (PMC_ROW_DISP_IS_THREAD(n))
3079145256Sjkoshy				p->pm_rowdisp = PMC_DISP_THREAD;
3080145256Sjkoshy			else
3081145256Sjkoshy				p->pm_rowdisp = PMC_DISP_FREE;
3082145256Sjkoshy
3083145256Sjkoshy			p->pm_ownerpid = -1;
3084145256Sjkoshy
3085145256Sjkoshy			if (pm == NULL)	/* no PMC associated */
3086145256Sjkoshy				continue;
3087145256Sjkoshy
3088145256Sjkoshy			po = pm->pm_owner;
3089145256Sjkoshy
3090145256Sjkoshy			KASSERT(po->po_owner != NULL,
3091145256Sjkoshy			    ("[pmc,%d] pmc_owner had a null proc pointer",
3092145256Sjkoshy				__LINE__));
3093145256Sjkoshy
3094145256Sjkoshy			p->pm_ownerpid = po->po_owner->p_pid;
3095145774Sjkoshy			p->pm_mode     = PMC_TO_MODE(pm);
3096145256Sjkoshy			p->pm_event    = pm->pm_event;
3097145256Sjkoshy			p->pm_flags    = pm->pm_flags;
3098145256Sjkoshy
3099145774Sjkoshy			if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
3100145256Sjkoshy				p->pm_reloadcount =
3101145256Sjkoshy				    pm->pm_sc.pm_reloadcount;
3102145256Sjkoshy		}
3103145256Sjkoshy
3104145256Sjkoshy		pmc_restore_cpu_binding(&pb);
3105145256Sjkoshy
3106145256Sjkoshy		/* now copy out the PMC info collected */
3107145256Sjkoshy		if (error == 0)
3108145256Sjkoshy			error = copyout(pmcinfo, &gpi->pm_pmcs, pmcinfo_size);
3109145256Sjkoshy
3110184205Sdes		free(pmcinfo, M_PMC);
3111145256Sjkoshy	}
3112145256Sjkoshy	break;
3113145256Sjkoshy
3114145256Sjkoshy
3115145256Sjkoshy	/*
3116145256Sjkoshy	 * Set the administrative state of a PMC.  I.e. whether
3117145256Sjkoshy	 * the PMC is to be used or not.
3118145256Sjkoshy	 */
3119145256Sjkoshy
3120145256Sjkoshy	case PMC_OP_PMCADMIN:
3121145256Sjkoshy	{
3122145256Sjkoshy		int cpu, ri;
3123145256Sjkoshy		enum pmc_state request;
3124145256Sjkoshy		struct pmc_cpu *pc;
3125145256Sjkoshy		struct pmc_hw *phw;
3126145256Sjkoshy		struct pmc_op_pmcadmin pma;
3127145256Sjkoshy		struct pmc_binding pb;
3128145256Sjkoshy
3129145256Sjkoshy		sx_assert(&pmc_sx, SX_XLOCKED);
3130145256Sjkoshy
3131145256Sjkoshy		KASSERT(td == curthread,
3132145256Sjkoshy		    ("[pmc,%d] td != curthread", __LINE__));
3133145256Sjkoshy
3134164033Srwatson		error = priv_check(td, PRIV_PMC_MANAGE);
3135164033Srwatson		if (error)
3136145256Sjkoshy			break;
3137145256Sjkoshy
3138145256Sjkoshy		if ((error = copyin(arg, &pma, sizeof(pma))) != 0)
3139145256Sjkoshy			break;
3140145256Sjkoshy
3141145256Sjkoshy		cpu = pma.pm_cpu;
3142145256Sjkoshy
3143183266Sjkoshy		if (cpu < 0 || cpu >= (int) pmc_cpu_max()) {
3144145256Sjkoshy			error = EINVAL;
3145145256Sjkoshy			break;
3146145256Sjkoshy		}
3147145256Sjkoshy
3148183266Sjkoshy		if (!pmc_cpu_is_active(cpu)) {
3149145256Sjkoshy			error = ENXIO;
3150145256Sjkoshy			break;
3151145256Sjkoshy		}
3152145256Sjkoshy
3153145256Sjkoshy		request = pma.pm_state;
3154145256Sjkoshy
3155145256Sjkoshy		if (request != PMC_STATE_DISABLED &&
3156145256Sjkoshy		    request != PMC_STATE_FREE) {
3157145256Sjkoshy			error = EINVAL;
3158145256Sjkoshy			break;
3159145256Sjkoshy		}
3160145256Sjkoshy
3161145256Sjkoshy		ri = pma.pm_pmc; /* pmc id == row index */
3162145256Sjkoshy		if (ri < 0 || ri >= (int) md->pmd_npmc) {
3163145256Sjkoshy			error = EINVAL;
3164145256Sjkoshy			break;
3165145256Sjkoshy		}
3166145256Sjkoshy
3167145256Sjkoshy		/*
3168145256Sjkoshy		 * We can't disable a PMC with a row-index allocated
3169145256Sjkoshy		 * for process virtual PMCs.
3170145256Sjkoshy		 */
3171145256Sjkoshy
3172145256Sjkoshy		if (PMC_ROW_DISP_IS_THREAD(ri) &&
3173145256Sjkoshy		    request == PMC_STATE_DISABLED) {
3174145256Sjkoshy			error = EBUSY;
3175145256Sjkoshy			break;
3176145256Sjkoshy		}
3177145256Sjkoshy
3178145256Sjkoshy		/*
3179145256Sjkoshy		 * otherwise, this PMC on this CPU is either free or
3180145256Sjkoshy		 * in system-wide mode.
3181145256Sjkoshy		 */
3182145256Sjkoshy
3183145256Sjkoshy		pmc_save_cpu_binding(&pb);
3184145256Sjkoshy		pmc_select_cpu(cpu);
3185145256Sjkoshy
3186145256Sjkoshy		pc  = pmc_pcpu[cpu];
3187145256Sjkoshy		phw = pc->pc_hwpmcs[ri];
3188145256Sjkoshy
3189145256Sjkoshy		/*
3190145256Sjkoshy		 * XXX do we need some kind of 'forced' disable?
3191145256Sjkoshy		 */
3192145256Sjkoshy
3193145256Sjkoshy		if (phw->phw_pmc == NULL) {
3194145256Sjkoshy			if (request == PMC_STATE_DISABLED &&
3195145256Sjkoshy			    (phw->phw_state & PMC_PHW_FLAG_IS_ENABLED)) {
3196145256Sjkoshy				phw->phw_state &= ~PMC_PHW_FLAG_IS_ENABLED;
3197145256Sjkoshy				PMC_MARK_ROW_STANDALONE(ri);
3198145256Sjkoshy			} else if (request == PMC_STATE_FREE &&
3199145256Sjkoshy			    (phw->phw_state & PMC_PHW_FLAG_IS_ENABLED) == 0) {
3200145256Sjkoshy				phw->phw_state |=  PMC_PHW_FLAG_IS_ENABLED;
3201145256Sjkoshy				PMC_UNMARK_ROW_STANDALONE(ri);
3202145256Sjkoshy			}
3203145256Sjkoshy			/* other cases are a no-op */
3204145256Sjkoshy		} else
3205145256Sjkoshy			error = EBUSY;
3206145256Sjkoshy
3207145256Sjkoshy		pmc_restore_cpu_binding(&pb);
3208145256Sjkoshy	}
3209145256Sjkoshy	break;
3210145256Sjkoshy
3211145256Sjkoshy
3212145256Sjkoshy	/*
3213145256Sjkoshy	 * Allocate a PMC.
3214145256Sjkoshy	 */
3215145256Sjkoshy
3216145256Sjkoshy	case PMC_OP_PMCALLOCATE:
3217145256Sjkoshy	{
3218184802Sjkoshy		int adjri, n;
3219184802Sjkoshy		u_int cpu;
3220145256Sjkoshy		uint32_t caps;
3221184802Sjkoshy		struct pmc *pmc;
3222145256Sjkoshy		enum pmc_mode mode;
3223145774Sjkoshy		struct pmc_hw *phw;
3224184802Sjkoshy		struct pmc_binding pb;
3225184802Sjkoshy		struct pmc_classdep *pcd;
3226145256Sjkoshy		struct pmc_op_pmcallocate pa;
3227145256Sjkoshy
3228145256Sjkoshy		if ((error = copyin(arg, &pa, sizeof(pa))) != 0)
3229145256Sjkoshy			break;
3230145256Sjkoshy
3231145256Sjkoshy		caps = pa.pm_caps;
3232145256Sjkoshy		mode = pa.pm_mode;
3233145256Sjkoshy		cpu  = pa.pm_cpu;
3234145256Sjkoshy
3235145256Sjkoshy		if ((mode != PMC_MODE_SS  &&  mode != PMC_MODE_SC  &&
3236145256Sjkoshy		     mode != PMC_MODE_TS  &&  mode != PMC_MODE_TC) ||
3237183266Sjkoshy		    (cpu != (u_int) PMC_CPU_ANY && cpu >= pmc_cpu_max())) {
3238145256Sjkoshy			error = EINVAL;
3239145256Sjkoshy			break;
3240145256Sjkoshy		}
3241145256Sjkoshy
3242145256Sjkoshy		/*
3243145256Sjkoshy		 * Virtual PMCs should only ask for a default CPU.
3244145256Sjkoshy		 * System mode PMCs need to specify a non-default CPU.
3245145256Sjkoshy		 */
3246145256Sjkoshy
3247145256Sjkoshy		if ((PMC_IS_VIRTUAL_MODE(mode) && cpu != (u_int) PMC_CPU_ANY) ||
3248145256Sjkoshy		    (PMC_IS_SYSTEM_MODE(mode) && cpu == (u_int) PMC_CPU_ANY)) {
3249145256Sjkoshy			error = EINVAL;
3250145256Sjkoshy			break;
3251145256Sjkoshy		}
3252145256Sjkoshy
3253145256Sjkoshy		/*
3254183266Sjkoshy		 * Check that an inactive CPU is not being asked for.
3255145256Sjkoshy		 */
3256145256Sjkoshy
3257183266Sjkoshy		if (PMC_IS_SYSTEM_MODE(mode) && !pmc_cpu_is_active(cpu)) {
3258145256Sjkoshy			error = ENXIO;
3259145256Sjkoshy			break;
3260145256Sjkoshy		}
3261145256Sjkoshy
3262145256Sjkoshy		/*
3263145256Sjkoshy		 * Refuse an allocation for a system-wide PMC if this
3264145256Sjkoshy		 * process has been jailed, or if this process lacks
3265145256Sjkoshy		 * super-user credentials and the sysctl tunable
3266145256Sjkoshy		 * 'security.bsd.unprivileged_syspmcs' is zero.
3267145256Sjkoshy		 */
3268145256Sjkoshy
3269145256Sjkoshy		if (PMC_IS_SYSTEM_MODE(mode)) {
3270164033Srwatson			if (jailed(curthread->td_ucred)) {
3271145256Sjkoshy				error = EPERM;
3272164033Srwatson				break;
3273164033Srwatson			}
3274164033Srwatson			if (!pmc_unprivileged_syspmcs) {
3275164033Srwatson				error = priv_check(curthread,
3276164033Srwatson				    PRIV_PMC_SYSTEM);
3277164033Srwatson				if (error)
3278164033Srwatson					break;
3279164033Srwatson			}
3280145256Sjkoshy		}
3281145256Sjkoshy
3282145256Sjkoshy		/*
3283145256Sjkoshy		 * Look for valid values for 'pm_flags'
3284145256Sjkoshy		 */
3285145256Sjkoshy
3286147191Sjkoshy		if ((pa.pm_flags & ~(PMC_F_DESCENDANTS | PMC_F_LOG_PROCCSW |
3287174395Sjkoshy		    PMC_F_LOG_PROCEXIT | PMC_F_CALLCHAIN)) != 0) {
3288145256Sjkoshy			error = EINVAL;
3289145256Sjkoshy			break;
3290145256Sjkoshy		}
3291145256Sjkoshy
3292147191Sjkoshy		/* process logging options are not allowed for system PMCs */
3293147191Sjkoshy		if (PMC_IS_SYSTEM_MODE(mode) && (pa.pm_flags &
3294147191Sjkoshy		    (PMC_F_LOG_PROCCSW | PMC_F_LOG_PROCEXIT))) {
3295147191Sjkoshy			error = EINVAL;
3296147191Sjkoshy			break;
3297147191Sjkoshy		}
3298147191Sjkoshy
3299145256Sjkoshy		/*
3300145256Sjkoshy		 * All sampling mode PMCs need to be able to interrupt the
3301145256Sjkoshy		 * CPU.
3302145256Sjkoshy		 */
3303147191Sjkoshy		if (PMC_IS_SAMPLING_MODE(mode))
3304145256Sjkoshy			caps |= PMC_CAP_INTERRUPT;
3305145256Sjkoshy
3306149374Sjkoshy		/* A valid class specifier should have been passed in. */
3307149374Sjkoshy		for (n = 0; n < md->pmd_nclass; n++)
3308184802Sjkoshy			if (md->pmd_classdep[n].pcd_class == pa.pm_class)
3309149374Sjkoshy				break;
3310149374Sjkoshy		if (n == md->pmd_nclass) {
3311149374Sjkoshy			error = EINVAL;
3312149374Sjkoshy			break;
3313149374Sjkoshy		}
3314149374Sjkoshy
3315149374Sjkoshy		/* The requested PMC capabilities should be feasible. */
3316184802Sjkoshy		if ((md->pmd_classdep[n].pcd_caps & caps) != caps) {
3317149374Sjkoshy			error = EOPNOTSUPP;
3318149374Sjkoshy			break;
3319149374Sjkoshy		}
3320149374Sjkoshy
3321145256Sjkoshy		PMCDBG(PMC,ALL,2, "event=%d caps=0x%x mode=%d cpu=%d",
3322145256Sjkoshy		    pa.pm_ev, caps, mode, cpu);
3323145256Sjkoshy
3324145256Sjkoshy		pmc = pmc_allocate_pmc_descriptor();
3325145774Sjkoshy		pmc->pm_id    = PMC_ID_MAKE_ID(cpu,pa.pm_mode,pa.pm_class,
3326145774Sjkoshy		    PMC_ID_INVALID);
3327145256Sjkoshy		pmc->pm_event = pa.pm_ev;
3328145256Sjkoshy		pmc->pm_state = PMC_STATE_FREE;
3329145256Sjkoshy		pmc->pm_caps  = caps;
3330145256Sjkoshy		pmc->pm_flags = pa.pm_flags;
3331145256Sjkoshy
3332145256Sjkoshy		/* switch thread to CPU 'cpu' */
3333145256Sjkoshy		pmc_save_cpu_binding(&pb);
3334145256Sjkoshy
3335145256Sjkoshy#define	PMC_IS_SHAREABLE_PMC(cpu, n)				\
3336145256Sjkoshy	(pmc_pcpu[(cpu)]->pc_hwpmcs[(n)]->phw_state &		\
3337145256Sjkoshy	 PMC_PHW_FLAG_IS_SHAREABLE)
3338145256Sjkoshy#define	PMC_IS_UNALLOCATED(cpu, n)				\
3339145256Sjkoshy	(pmc_pcpu[(cpu)]->pc_hwpmcs[(n)]->phw_pmc == NULL)
3340145256Sjkoshy
3341145256Sjkoshy		if (PMC_IS_SYSTEM_MODE(mode)) {
3342145256Sjkoshy			pmc_select_cpu(cpu);
3343184802Sjkoshy			for (n = 0; n < (int) md->pmd_npmc; n++) {
3344184802Sjkoshy				pcd = pmc_ri_to_classdep(md, n, &adjri);
3345145256Sjkoshy				if (pmc_can_allocate_row(n, mode) == 0 &&
3346145256Sjkoshy				    pmc_can_allocate_rowindex(
3347145774Sjkoshy					    curthread->td_proc, n, cpu) == 0 &&
3348145256Sjkoshy				    (PMC_IS_UNALLOCATED(cpu, n) ||
3349145256Sjkoshy				     PMC_IS_SHAREABLE_PMC(cpu, n)) &&
3350184802Sjkoshy				    pcd->pcd_allocate_pmc(cpu, adjri, pmc,
3351145256Sjkoshy					&pa) == 0)
3352145256Sjkoshy					break;
3353184802Sjkoshy			}
3354145256Sjkoshy		} else {
3355145256Sjkoshy			/* Process virtual mode */
3356145256Sjkoshy			for (n = 0; n < (int) md->pmd_npmc; n++) {
3357184802Sjkoshy				pcd = pmc_ri_to_classdep(md, n, &adjri);
3358145256Sjkoshy				if (pmc_can_allocate_row(n, mode) == 0 &&
3359145256Sjkoshy				    pmc_can_allocate_rowindex(
3360145774Sjkoshy					    curthread->td_proc, n,
3361145774Sjkoshy					    PMC_CPU_ANY) == 0 &&
3362184802Sjkoshy				    pcd->pcd_allocate_pmc(curthread->td_oncpu,
3363184802Sjkoshy					adjri, pmc, &pa) == 0)
3364145256Sjkoshy					break;
3365145256Sjkoshy			}
3366145256Sjkoshy		}
3367145256Sjkoshy
3368145256Sjkoshy#undef	PMC_IS_UNALLOCATED
3369145256Sjkoshy#undef	PMC_IS_SHAREABLE_PMC
3370145256Sjkoshy
3371145256Sjkoshy		pmc_restore_cpu_binding(&pb);
3372145256Sjkoshy
3373145256Sjkoshy		if (n == (int) md->pmd_npmc) {
3374145256Sjkoshy			pmc_destroy_pmc_descriptor(pmc);
3375184205Sdes			free(pmc, M_PMC);
3376145256Sjkoshy			pmc = NULL;
3377145256Sjkoshy			error = EINVAL;
3378145256Sjkoshy			break;
3379145256Sjkoshy		}
3380145256Sjkoshy
3381145774Sjkoshy		/* Fill in the correct value in the ID field */
3382145774Sjkoshy		pmc->pm_id = PMC_ID_MAKE_ID(cpu,mode,pa.pm_class,n);
3383145256Sjkoshy
3384145774Sjkoshy		PMCDBG(PMC,ALL,2, "ev=%d class=%d mode=%d n=%d -> pmcid=%x",
3385145774Sjkoshy		    pmc->pm_event, pa.pm_class, mode, n, pmc->pm_id);
3386145774Sjkoshy
3387147191Sjkoshy		/* Process mode PMCs with logging enabled need log files */
3388147191Sjkoshy		if (pmc->pm_flags & (PMC_F_LOG_PROCEXIT | PMC_F_LOG_PROCCSW))
3389147191Sjkoshy			pmc->pm_flags |= PMC_F_NEEDS_LOGFILE;
3390147191Sjkoshy
3391147191Sjkoshy		/* All system mode sampling PMCs require a log file */
3392147191Sjkoshy		if (PMC_IS_SAMPLING_MODE(mode) && PMC_IS_SYSTEM_MODE(mode))
3393147191Sjkoshy			pmc->pm_flags |= PMC_F_NEEDS_LOGFILE;
3394147191Sjkoshy
3395145256Sjkoshy		/*
3396145256Sjkoshy		 * Configure global pmc's immediately
3397145256Sjkoshy		 */
3398145256Sjkoshy
3399145774Sjkoshy		if (PMC_IS_SYSTEM_MODE(PMC_TO_MODE(pmc))) {
3400145774Sjkoshy
3401145774Sjkoshy			pmc_save_cpu_binding(&pb);
3402145774Sjkoshy			pmc_select_cpu(cpu);
3403145774Sjkoshy
3404145774Sjkoshy			phw = pmc_pcpu[cpu]->pc_hwpmcs[n];
3405184802Sjkoshy			pcd = pmc_ri_to_classdep(md, n, &adjri);
3406145774Sjkoshy
3407145774Sjkoshy			if ((phw->phw_state & PMC_PHW_FLAG_IS_ENABLED) == 0 ||
3408184802Sjkoshy			    (error = pcd->pcd_config_pmc(cpu, adjri, pmc)) != 0) {
3409184802Sjkoshy				(void) pcd->pcd_release_pmc(cpu, adjri, pmc);
3410145256Sjkoshy				pmc_destroy_pmc_descriptor(pmc);
3411184205Sdes				free(pmc, M_PMC);
3412145256Sjkoshy				pmc = NULL;
3413145774Sjkoshy				pmc_restore_cpu_binding(&pb);
3414145774Sjkoshy				error = EPERM;
3415145256Sjkoshy				break;
3416145256Sjkoshy			}
3417145256Sjkoshy
3418145774Sjkoshy			pmc_restore_cpu_binding(&pb);
3419145774Sjkoshy		}
3420145256Sjkoshy
3421145256Sjkoshy		pmc->pm_state    = PMC_STATE_ALLOCATED;
3422145256Sjkoshy
3423145256Sjkoshy		/*
3424145256Sjkoshy		 * mark row disposition
3425145256Sjkoshy		 */
3426145256Sjkoshy
3427145256Sjkoshy		if (PMC_IS_SYSTEM_MODE(mode))
3428145256Sjkoshy			PMC_MARK_ROW_STANDALONE(n);
3429145256Sjkoshy		else
3430145256Sjkoshy			PMC_MARK_ROW_THREAD(n);
3431145256Sjkoshy
3432145256Sjkoshy		/*
3433145256Sjkoshy		 * Register this PMC with the current thread as its owner.
3434145256Sjkoshy		 */
3435145256Sjkoshy
3436145256Sjkoshy		if ((error =
3437145256Sjkoshy		    pmc_register_owner(curthread->td_proc, pmc)) != 0) {
3438145256Sjkoshy			pmc_release_pmc_descriptor(pmc);
3439184205Sdes			free(pmc, M_PMC);
3440145256Sjkoshy			pmc = NULL;
3441145256Sjkoshy			break;
3442145256Sjkoshy		}
3443145256Sjkoshy
3444145256Sjkoshy		/*
3445145256Sjkoshy		 * Return the allocated index.
3446145256Sjkoshy		 */
3447145256Sjkoshy
3448145774Sjkoshy		pa.pm_pmcid = pmc->pm_id;
3449145256Sjkoshy
3450145256Sjkoshy		error = copyout(&pa, arg, sizeof(pa));
3451145256Sjkoshy	}
3452145256Sjkoshy	break;
3453145256Sjkoshy
3454145256Sjkoshy
3455145256Sjkoshy	/*
3456145256Sjkoshy	 * Attach a PMC to a process.
3457145256Sjkoshy	 */
3458145256Sjkoshy
3459145256Sjkoshy	case PMC_OP_PMCATTACH:
3460145256Sjkoshy	{
3461145256Sjkoshy		struct pmc *pm;
3462145256Sjkoshy		struct proc *p;
3463145256Sjkoshy		struct pmc_op_pmcattach a;
3464145256Sjkoshy
3465145256Sjkoshy		sx_assert(&pmc_sx, SX_XLOCKED);
3466145256Sjkoshy
3467145256Sjkoshy		if ((error = copyin(arg, &a, sizeof(a))) != 0)
3468145256Sjkoshy			break;
3469145256Sjkoshy
3470145256Sjkoshy		if (a.pm_pid < 0) {
3471145256Sjkoshy			error = EINVAL;
3472145256Sjkoshy			break;
3473145256Sjkoshy		} else if (a.pm_pid == 0)
3474145256Sjkoshy			a.pm_pid = td->td_proc->p_pid;
3475145256Sjkoshy
3476145256Sjkoshy		if ((error = pmc_find_pmc(a.pm_pmc, &pm)) != 0)
3477145256Sjkoshy			break;
3478145256Sjkoshy
3479145774Sjkoshy		if (PMC_IS_SYSTEM_MODE(PMC_TO_MODE(pm))) {
3480145256Sjkoshy			error = EINVAL;
3481145256Sjkoshy			break;
3482145256Sjkoshy		}
3483145256Sjkoshy
3484145256Sjkoshy		/* PMCs may be (re)attached only when allocated or stopped */
3485145256Sjkoshy		if (pm->pm_state == PMC_STATE_RUNNING) {
3486145256Sjkoshy			error = EBUSY;
3487145256Sjkoshy			break;
3488145256Sjkoshy		} else if (pm->pm_state != PMC_STATE_ALLOCATED &&
3489145256Sjkoshy		    pm->pm_state != PMC_STATE_STOPPED) {
3490145256Sjkoshy			error = EINVAL;
3491145256Sjkoshy			break;
3492145256Sjkoshy		}
3493145256Sjkoshy
3494145256Sjkoshy		/* lookup pid */
3495145256Sjkoshy		if ((p = pfind(a.pm_pid)) == NULL) {
3496145256Sjkoshy			error = ESRCH;
3497145256Sjkoshy			break;
3498145256Sjkoshy		}
3499145256Sjkoshy
3500145256Sjkoshy		/*
3501145256Sjkoshy		 * Ignore processes that are working on exiting.
3502145256Sjkoshy		 */
3503145256Sjkoshy		if (p->p_flag & P_WEXIT) {
3504145256Sjkoshy			error = ESRCH;
3505145256Sjkoshy			PROC_UNLOCK(p);	/* pfind() returns a locked process */
3506145256Sjkoshy			break;
3507145256Sjkoshy		}
3508145256Sjkoshy
3509145256Sjkoshy		/*
3510145256Sjkoshy		 * we are allowed to attach a PMC to a process if
3511145256Sjkoshy		 * we can debug it.
3512145256Sjkoshy		 */
3513145256Sjkoshy		error = p_candebug(curthread, p);
3514145256Sjkoshy
3515145256Sjkoshy		PROC_UNLOCK(p);
3516145256Sjkoshy
3517145256Sjkoshy		if (error == 0)
3518145256Sjkoshy			error = pmc_attach_process(p, pm);
3519145256Sjkoshy	}
3520145256Sjkoshy	break;
3521145256Sjkoshy
3522145256Sjkoshy
3523145256Sjkoshy	/*
3524145256Sjkoshy	 * Detach an attached PMC from a process.
3525145256Sjkoshy	 */
3526145256Sjkoshy
3527145256Sjkoshy	case PMC_OP_PMCDETACH:
3528145256Sjkoshy	{
3529145256Sjkoshy		struct pmc *pm;
3530145256Sjkoshy		struct proc *p;
3531145256Sjkoshy		struct pmc_op_pmcattach a;
3532145256Sjkoshy
3533145256Sjkoshy		if ((error = copyin(arg, &a, sizeof(a))) != 0)
3534145256Sjkoshy			break;
3535145256Sjkoshy
3536145256Sjkoshy		if (a.pm_pid < 0) {
3537145256Sjkoshy			error = EINVAL;
3538145256Sjkoshy			break;
3539145256Sjkoshy		} else if (a.pm_pid == 0)
3540145256Sjkoshy			a.pm_pid = td->td_proc->p_pid;
3541145256Sjkoshy
3542145256Sjkoshy		if ((error = pmc_find_pmc(a.pm_pmc, &pm)) != 0)
3543145256Sjkoshy			break;
3544145256Sjkoshy
3545145256Sjkoshy		if ((p = pfind(a.pm_pid)) == NULL) {
3546145256Sjkoshy			error = ESRCH;
3547145256Sjkoshy			break;
3548145256Sjkoshy		}
3549145256Sjkoshy
3550145256Sjkoshy		/*
3551145256Sjkoshy		 * Treat processes that are in the process of exiting
3552145256Sjkoshy		 * as if they were not present.
3553145256Sjkoshy		 */
3554145256Sjkoshy
3555145256Sjkoshy		if (p->p_flag & P_WEXIT)
3556145256Sjkoshy			error = ESRCH;
3557145256Sjkoshy
3558145256Sjkoshy		PROC_UNLOCK(p);	/* pfind() returns a locked process */
3559145256Sjkoshy
3560145256Sjkoshy		if (error == 0)
3561145256Sjkoshy			error = pmc_detach_process(p, pm);
3562145256Sjkoshy	}
3563145256Sjkoshy	break;
3564145256Sjkoshy
3565145256Sjkoshy
3566145256Sjkoshy	/*
3567147191Sjkoshy	 * Retrieve the MSR number associated with the counter
3568147191Sjkoshy	 * 'pmc_id'.  This allows processes to directly use RDPMC
3569147191Sjkoshy	 * instructions to read their PMCs, without the overhead of a
3570147191Sjkoshy	 * system call.
3571147191Sjkoshy	 */
3572147191Sjkoshy
3573147191Sjkoshy	case PMC_OP_PMCGETMSR:
3574147191Sjkoshy	{
3575184802Sjkoshy		int adjri, ri;
3576184802Sjkoshy		struct pmc *pm;
3577147191Sjkoshy		struct pmc_target *pt;
3578147191Sjkoshy		struct pmc_op_getmsr gm;
3579184802Sjkoshy		struct pmc_classdep *pcd;
3580147191Sjkoshy
3581147191Sjkoshy		PMC_DOWNGRADE_SX();
3582147191Sjkoshy
3583147191Sjkoshy		if ((error = copyin(arg, &gm, sizeof(gm))) != 0)
3584147191Sjkoshy			break;
3585147191Sjkoshy
3586147191Sjkoshy		if ((error = pmc_find_pmc(gm.pm_pmcid, &pm)) != 0)
3587147191Sjkoshy			break;
3588147191Sjkoshy
3589147191Sjkoshy		/*
3590147191Sjkoshy		 * The allocated PMC has to be a process virtual PMC,
3591147191Sjkoshy		 * i.e., of type MODE_T[CS].  Global PMCs can only be
3592147191Sjkoshy		 * read using the PMCREAD operation since they may be
3593147191Sjkoshy		 * allocated on a different CPU than the one we could
3594147191Sjkoshy		 * be running on at the time of the RDPMC instruction.
3595147191Sjkoshy		 *
3596147191Sjkoshy		 * The GETMSR operation is not allowed for PMCs that
3597147191Sjkoshy		 * are inherited across processes.
3598147191Sjkoshy		 */
3599147191Sjkoshy
3600147191Sjkoshy		if (!PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm)) ||
3601147191Sjkoshy		    (pm->pm_flags & PMC_F_DESCENDANTS)) {
3602147191Sjkoshy			error = EINVAL;
3603147191Sjkoshy			break;
3604147191Sjkoshy		}
3605147191Sjkoshy
3606147191Sjkoshy		/*
3607147191Sjkoshy		 * It only makes sense to use a RDPMC (or its
3608147191Sjkoshy		 * equivalent instruction on non-x86 architectures) on
3609147191Sjkoshy		 * a process that has allocated and attached a PMC to
3610147191Sjkoshy		 * itself.  Conversely the PMC is only allowed to have
3611147191Sjkoshy		 * one process attached to it -- its owner.
3612147191Sjkoshy		 */
3613147191Sjkoshy
3614147191Sjkoshy		if ((pt = LIST_FIRST(&pm->pm_targets)) == NULL ||
3615147191Sjkoshy		    LIST_NEXT(pt, pt_next) != NULL ||
3616147191Sjkoshy		    pt->pt_process->pp_proc != pm->pm_owner->po_owner) {
3617147191Sjkoshy			error = EINVAL;
3618147191Sjkoshy			break;
3619147191Sjkoshy		}
3620147191Sjkoshy
3621147191Sjkoshy		ri = PMC_TO_ROWINDEX(pm);
3622184802Sjkoshy		pcd = pmc_ri_to_classdep(md, ri, &adjri);
3623147191Sjkoshy
3624184802Sjkoshy		/* PMC class has no 'GETMSR' support */
3625184802Sjkoshy		if (pcd->pcd_get_msr == NULL) {
3626184802Sjkoshy			error = ENOSYS;
3627147191Sjkoshy			break;
3628184802Sjkoshy		}
3629147191Sjkoshy
3630184802Sjkoshy		if ((error = (*pcd->pcd_get_msr)(adjri, &gm.pm_msr)) < 0)
3631184802Sjkoshy			break;
3632184802Sjkoshy
3633147191Sjkoshy		if ((error = copyout(&gm, arg, sizeof(gm))) < 0)
3634147191Sjkoshy			break;
3635147191Sjkoshy
3636147191Sjkoshy		/*
3637147191Sjkoshy		 * Mark our process as using MSRs.  Update machine
3638147191Sjkoshy		 * state using a forced context switch.
3639147191Sjkoshy		 */
3640147191Sjkoshy
3641147191Sjkoshy		pt->pt_process->pp_flags |= PMC_PP_ENABLE_MSR_ACCESS;
3642147191Sjkoshy		pmc_force_context_switch();
3643147191Sjkoshy
3644147191Sjkoshy	}
3645147191Sjkoshy	break;
3646147191Sjkoshy
3647147191Sjkoshy	/*
3648145256Sjkoshy	 * Release an allocated PMC
3649145256Sjkoshy	 */
3650145256Sjkoshy
3651145256Sjkoshy	case PMC_OP_PMCRELEASE:
3652145256Sjkoshy	{
3653145256Sjkoshy		pmc_id_t pmcid;
3654145256Sjkoshy		struct pmc *pm;
3655145256Sjkoshy		struct pmc_owner *po;
3656145256Sjkoshy		struct pmc_op_simple sp;
3657145256Sjkoshy
3658145256Sjkoshy		/*
3659145256Sjkoshy		 * Find PMC pointer for the named PMC.
3660145256Sjkoshy		 *
3661145256Sjkoshy		 * Use pmc_release_pmc_descriptor() to switch off the
3662145256Sjkoshy		 * PMC, remove all its target threads, and remove the
3663145256Sjkoshy		 * PMC from its owner's list.
3664145256Sjkoshy		 *
3665145256Sjkoshy		 * Remove the owner record if this is the last PMC
3666145256Sjkoshy		 * owned.
3667145256Sjkoshy		 *
3668145256Sjkoshy		 * Free up space.
3669145256Sjkoshy		 */
3670145256Sjkoshy
3671145256Sjkoshy		if ((error = copyin(arg, &sp, sizeof(sp))) != 0)
3672145256Sjkoshy			break;
3673145256Sjkoshy
3674145256Sjkoshy		pmcid = sp.pm_pmcid;
3675145256Sjkoshy
3676145256Sjkoshy		if ((error = pmc_find_pmc(pmcid, &pm)) != 0)
3677145256Sjkoshy			break;
3678145256Sjkoshy
3679145256Sjkoshy		po = pm->pm_owner;
3680145256Sjkoshy		pmc_release_pmc_descriptor(pm);
3681145256Sjkoshy		pmc_maybe_remove_owner(po);
3682145256Sjkoshy
3683184205Sdes		free(pm, M_PMC);
3684145256Sjkoshy	}
3685145256Sjkoshy	break;
3686145256Sjkoshy
3687145256Sjkoshy
3688145256Sjkoshy	/*
3689145256Sjkoshy	 * Read and/or write a PMC.
3690145256Sjkoshy	 */
3691145256Sjkoshy
3692145256Sjkoshy	case PMC_OP_PMCRW:
3693145256Sjkoshy	{
3694184802Sjkoshy		int adjri;
3695184802Sjkoshy		struct pmc *pm;
3696145256Sjkoshy		uint32_t cpu, ri;
3697184802Sjkoshy		pmc_value_t oldvalue;
3698184802Sjkoshy		struct pmc_binding pb;
3699184802Sjkoshy		struct pmc_op_pmcrw prw;
3700184802Sjkoshy		struct pmc_classdep *pcd;
3701145256Sjkoshy		struct pmc_op_pmcrw *pprw;
3702145256Sjkoshy
3703145256Sjkoshy		PMC_DOWNGRADE_SX();
3704145256Sjkoshy
3705145256Sjkoshy		if ((error = copyin(arg, &prw, sizeof(prw))) != 0)
3706145256Sjkoshy			break;
3707145256Sjkoshy
3708145301Simp		ri = 0;
3709145256Sjkoshy		PMCDBG(PMC,OPS,1, "rw id=%d flags=0x%x", prw.pm_pmcid,
3710145256Sjkoshy		    prw.pm_flags);
3711145256Sjkoshy
3712145256Sjkoshy		/* must have at least one flag set */
3713145256Sjkoshy		if ((prw.pm_flags & (PMC_F_OLDVALUE|PMC_F_NEWVALUE)) == 0) {
3714145256Sjkoshy			error = EINVAL;
3715145256Sjkoshy			break;
3716145256Sjkoshy		}
3717145256Sjkoshy
3718145256Sjkoshy		/* locate pmc descriptor */
3719145256Sjkoshy		if ((error = pmc_find_pmc(prw.pm_pmcid, &pm)) != 0)
3720145256Sjkoshy			break;
3721145256Sjkoshy
3722145256Sjkoshy		/* Can't read a PMC that hasn't been started. */
3723145256Sjkoshy		if (pm->pm_state != PMC_STATE_ALLOCATED &&
3724145256Sjkoshy		    pm->pm_state != PMC_STATE_STOPPED &&
3725145256Sjkoshy		    pm->pm_state != PMC_STATE_RUNNING) {
3726145256Sjkoshy			error = EINVAL;
3727145256Sjkoshy			break;
3728145256Sjkoshy		}
3729145256Sjkoshy
3730145256Sjkoshy		/* writing a new value is allowed only for 'STOPPED' pmcs */
3731145256Sjkoshy		if (pm->pm_state == PMC_STATE_RUNNING &&
3732145256Sjkoshy		    (prw.pm_flags & PMC_F_NEWVALUE)) {
3733145256Sjkoshy			error = EBUSY;
3734145256Sjkoshy			break;
3735145256Sjkoshy		}
3736145256Sjkoshy
3737145774Sjkoshy		if (PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm))) {
3738145256Sjkoshy
3739145774Sjkoshy			/*
3740145774Sjkoshy			 * If this PMC is attached to its owner (i.e.,
3741145774Sjkoshy			 * the process requesting this operation) and
3742145774Sjkoshy			 * is running, then attempt to get an
3743145774Sjkoshy			 * upto-date reading from hardware for a READ.
3744145774Sjkoshy			 * Writes are only allowed when the PMC is
3745145774Sjkoshy			 * stopped, so only update the saved value
3746145774Sjkoshy			 * field.
3747145774Sjkoshy			 *
3748145774Sjkoshy			 * If the PMC is not running, or is not
3749145774Sjkoshy			 * attached to its owner, read/write to the
3750145774Sjkoshy			 * savedvalue field.
3751145774Sjkoshy			 */
3752145774Sjkoshy
3753145774Sjkoshy			ri = PMC_TO_ROWINDEX(pm);
3754184802Sjkoshy			pcd = pmc_ri_to_classdep(md, ri, &adjri);
3755145774Sjkoshy
3756145256Sjkoshy			mtx_pool_lock_spin(pmc_mtxpool, pm);
3757145774Sjkoshy			cpu = curthread->td_oncpu;
3758145774Sjkoshy
3759145774Sjkoshy			if (prw.pm_flags & PMC_F_OLDVALUE) {
3760145774Sjkoshy				if ((pm->pm_flags & PMC_F_ATTACHED_TO_OWNER) &&
3761145774Sjkoshy				    (pm->pm_state == PMC_STATE_RUNNING))
3762184802Sjkoshy					error = (*pcd->pcd_read_pmc)(cpu, adjri,
3763145774Sjkoshy					    &oldvalue);
3764145774Sjkoshy				else
3765145774Sjkoshy					oldvalue = pm->pm_gv.pm_savedvalue;
3766145774Sjkoshy			}
3767145256Sjkoshy			if (prw.pm_flags & PMC_F_NEWVALUE)
3768145256Sjkoshy				pm->pm_gv.pm_savedvalue = prw.pm_value;
3769145774Sjkoshy
3770145256Sjkoshy			mtx_pool_unlock_spin(pmc_mtxpool, pm);
3771145256Sjkoshy
3772145256Sjkoshy		} else { /* System mode PMCs */
3773145774Sjkoshy			cpu = PMC_TO_CPU(pm);
3774145774Sjkoshy			ri  = PMC_TO_ROWINDEX(pm);
3775184802Sjkoshy			pcd = pmc_ri_to_classdep(md, ri, &adjri);
3776145256Sjkoshy
3777183266Sjkoshy			if (!pmc_cpu_is_active(cpu)) {
3778145256Sjkoshy				error = ENXIO;
3779145256Sjkoshy				break;
3780145256Sjkoshy			}
3781145256Sjkoshy
3782145256Sjkoshy			/* move this thread to CPU 'cpu' */
3783145256Sjkoshy			pmc_save_cpu_binding(&pb);
3784145256Sjkoshy			pmc_select_cpu(cpu);
3785145256Sjkoshy
3786145774Sjkoshy			critical_enter();
3787145256Sjkoshy			/* save old value */
3788145256Sjkoshy			if (prw.pm_flags & PMC_F_OLDVALUE)
3789184802Sjkoshy				if ((error = (*pcd->pcd_read_pmc)(cpu, adjri,
3790145256Sjkoshy					 &oldvalue)))
3791145256Sjkoshy					goto error;
3792145256Sjkoshy			/* write out new value */
3793145256Sjkoshy			if (prw.pm_flags & PMC_F_NEWVALUE)
3794184802Sjkoshy				error = (*pcd->pcd_write_pmc)(cpu, adjri,
3795145256Sjkoshy				    prw.pm_value);
3796145256Sjkoshy		error:
3797145774Sjkoshy			critical_exit();
3798145256Sjkoshy			pmc_restore_cpu_binding(&pb);
3799145256Sjkoshy			if (error)
3800145256Sjkoshy				break;
3801145256Sjkoshy		}
3802145256Sjkoshy
3803145256Sjkoshy		pprw = (struct pmc_op_pmcrw *) arg;
3804145256Sjkoshy
3805153110Sru#ifdef	DEBUG
3806145256Sjkoshy		if (prw.pm_flags & PMC_F_NEWVALUE)
3807145256Sjkoshy			PMCDBG(PMC,OPS,2, "rw id=%d new %jx -> old %jx",
3808145256Sjkoshy			    ri, prw.pm_value, oldvalue);
3809156778Sjkoshy		else if (prw.pm_flags & PMC_F_OLDVALUE)
3810145256Sjkoshy			PMCDBG(PMC,OPS,2, "rw id=%d -> old %jx", ri, oldvalue);
3811145256Sjkoshy#endif
3812145256Sjkoshy
3813145256Sjkoshy		/* return old value if requested */
3814145256Sjkoshy		if (prw.pm_flags & PMC_F_OLDVALUE)
3815145256Sjkoshy			if ((error = copyout(&oldvalue, &pprw->pm_value,
3816145256Sjkoshy				 sizeof(prw.pm_value))))
3817145256Sjkoshy				break;
3818145256Sjkoshy
3819145256Sjkoshy	}
3820145256Sjkoshy	break;
3821145256Sjkoshy
3822145256Sjkoshy
3823145256Sjkoshy	/*
3824145256Sjkoshy	 * Set the sampling rate for a sampling mode PMC and the
3825145256Sjkoshy	 * initial count for a counting mode PMC.
3826145256Sjkoshy	 */
3827145256Sjkoshy
3828145256Sjkoshy	case PMC_OP_PMCSETCOUNT:
3829145256Sjkoshy	{
3830145256Sjkoshy		struct pmc *pm;
3831145256Sjkoshy		struct pmc_op_pmcsetcount sc;
3832145256Sjkoshy
3833145256Sjkoshy		PMC_DOWNGRADE_SX();
3834145256Sjkoshy
3835145256Sjkoshy		if ((error = copyin(arg, &sc, sizeof(sc))) != 0)
3836145256Sjkoshy			break;
3837145256Sjkoshy
3838145256Sjkoshy		if ((error = pmc_find_pmc(sc.pm_pmcid, &pm)) != 0)
3839145256Sjkoshy			break;
3840145256Sjkoshy
3841145256Sjkoshy		if (pm->pm_state == PMC_STATE_RUNNING) {
3842145256Sjkoshy			error = EBUSY;
3843145256Sjkoshy			break;
3844145256Sjkoshy		}
3845145256Sjkoshy
3846145774Sjkoshy		if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
3847145256Sjkoshy			pm->pm_sc.pm_reloadcount = sc.pm_count;
3848145256Sjkoshy		else
3849145256Sjkoshy			pm->pm_sc.pm_initial = sc.pm_count;
3850145256Sjkoshy	}
3851145256Sjkoshy	break;
3852145256Sjkoshy
3853145256Sjkoshy
3854145256Sjkoshy	/*
3855145256Sjkoshy	 * Start a PMC.
3856145256Sjkoshy	 */
3857145256Sjkoshy
3858145256Sjkoshy	case PMC_OP_PMCSTART:
3859145256Sjkoshy	{
3860145256Sjkoshy		pmc_id_t pmcid;
3861145256Sjkoshy		struct pmc *pm;
3862145256Sjkoshy		struct pmc_op_simple sp;
3863145256Sjkoshy
3864145256Sjkoshy		sx_assert(&pmc_sx, SX_XLOCKED);
3865145256Sjkoshy
3866145256Sjkoshy		if ((error = copyin(arg, &sp, sizeof(sp))) != 0)
3867145256Sjkoshy			break;
3868145256Sjkoshy
3869145256Sjkoshy		pmcid = sp.pm_pmcid;
3870145256Sjkoshy
3871145256Sjkoshy		if ((error = pmc_find_pmc(pmcid, &pm)) != 0)
3872145256Sjkoshy			break;
3873145256Sjkoshy
3874145774Sjkoshy		KASSERT(pmcid == pm->pm_id,
3875145774Sjkoshy		    ("[pmc,%d] pmcid %x != id %x", __LINE__,
3876145774Sjkoshy			pm->pm_id, pmcid));
3877145256Sjkoshy
3878145256Sjkoshy		if (pm->pm_state == PMC_STATE_RUNNING) /* already running */
3879145256Sjkoshy			break;
3880145256Sjkoshy		else if (pm->pm_state != PMC_STATE_STOPPED &&
3881145256Sjkoshy		    pm->pm_state != PMC_STATE_ALLOCATED) {
3882145256Sjkoshy			error = EINVAL;
3883145256Sjkoshy			break;
3884145256Sjkoshy		}
3885145256Sjkoshy
3886145256Sjkoshy		error = pmc_start(pm);
3887145256Sjkoshy	}
3888145256Sjkoshy	break;
3889145256Sjkoshy
3890145256Sjkoshy
3891145256Sjkoshy	/*
3892145256Sjkoshy	 * Stop a PMC.
3893145256Sjkoshy	 */
3894145256Sjkoshy
3895145256Sjkoshy	case PMC_OP_PMCSTOP:
3896145256Sjkoshy	{
3897145256Sjkoshy		pmc_id_t pmcid;
3898145256Sjkoshy		struct pmc *pm;
3899145256Sjkoshy		struct pmc_op_simple sp;
3900145256Sjkoshy
3901145256Sjkoshy		PMC_DOWNGRADE_SX();
3902145256Sjkoshy
3903145256Sjkoshy		if ((error = copyin(arg, &sp, sizeof(sp))) != 0)
3904145256Sjkoshy			break;
3905145256Sjkoshy
3906145256Sjkoshy		pmcid = sp.pm_pmcid;
3907145256Sjkoshy
3908145256Sjkoshy		/*
3909145256Sjkoshy		 * Mark the PMC as inactive and invoke the MD stop
3910145256Sjkoshy		 * routines if needed.
3911145256Sjkoshy		 */
3912145256Sjkoshy
3913145256Sjkoshy		if ((error = pmc_find_pmc(pmcid, &pm)) != 0)
3914145256Sjkoshy			break;
3915145256Sjkoshy
3916145774Sjkoshy		KASSERT(pmcid == pm->pm_id,
3917145774Sjkoshy		    ("[pmc,%d] pmc id %x != pmcid %x", __LINE__,
3918145774Sjkoshy			pm->pm_id, pmcid));
3919145256Sjkoshy
3920145256Sjkoshy		if (pm->pm_state == PMC_STATE_STOPPED) /* already stopped */
3921145256Sjkoshy			break;
3922145256Sjkoshy		else if (pm->pm_state != PMC_STATE_RUNNING) {
3923145256Sjkoshy			error = EINVAL;
3924145256Sjkoshy			break;
3925145256Sjkoshy		}
3926145256Sjkoshy
3927145256Sjkoshy		error = pmc_stop(pm);
3928145256Sjkoshy	}
3929145256Sjkoshy	break;
3930145256Sjkoshy
3931145256Sjkoshy
3932145256Sjkoshy	/*
3933147867Sjkoshy	 * Write a user supplied value to the log file.
3934145256Sjkoshy	 */
3935145256Sjkoshy
3936145256Sjkoshy	case PMC_OP_WRITELOG:
3937145256Sjkoshy	{
3938147191Sjkoshy		struct pmc_op_writelog wl;
3939147191Sjkoshy		struct pmc_owner *po;
3940145256Sjkoshy
3941145256Sjkoshy		PMC_DOWNGRADE_SX();
3942145256Sjkoshy
3943147191Sjkoshy		if ((error = copyin(arg, &wl, sizeof(wl))) != 0)
3944145256Sjkoshy			break;
3945145256Sjkoshy
3946147191Sjkoshy		if ((po = pmc_find_owner_descriptor(td->td_proc)) == NULL) {
3947145256Sjkoshy			error = EINVAL;
3948145256Sjkoshy			break;
3949145256Sjkoshy		}
3950145256Sjkoshy
3951147191Sjkoshy		if ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0) {
3952145774Sjkoshy			error = EINVAL;
3953145774Sjkoshy			break;
3954145774Sjkoshy		}
3955145774Sjkoshy
3956147191Sjkoshy		error = pmclog_process_userlog(po, &wl);
3957145256Sjkoshy	}
3958145256Sjkoshy	break;
3959145256Sjkoshy
3960147191Sjkoshy
3961145256Sjkoshy	default:
3962145256Sjkoshy		error = EINVAL;
3963145256Sjkoshy		break;
3964145256Sjkoshy	}
3965145256Sjkoshy
3966195005Sattilio	if (is_sx_locked != 0) {
3967195005Sattilio		if (is_sx_downgraded)
3968195005Sattilio			sx_sunlock(&pmc_sx);
3969195005Sattilio		else
3970195005Sattilio			sx_xunlock(&pmc_sx);
3971195005Sattilio	}
3972145256Sjkoshy
3973145256Sjkoshy	if (error)
3974145256Sjkoshy		atomic_add_int(&pmc_stats.pm_syscall_errors, 1);
3975145256Sjkoshy
3976147191Sjkoshy	PICKUP_GIANT();
3977147191Sjkoshy
3978145256Sjkoshy	return error;
3979145256Sjkoshy}
3980145256Sjkoshy
3981145256Sjkoshy/*
3982145256Sjkoshy * Helper functions
3983145256Sjkoshy */
3984145256Sjkoshy
3985147191Sjkoshy
3986145256Sjkoshy/*
3987174395Sjkoshy * Mark the thread as needing callchain capture and post an AST.  The
3988174395Sjkoshy * actual callchain capture will be done in a context where it is safe
3989174395Sjkoshy * to take page faults.
3990174395Sjkoshy */
3991174395Sjkoshy
3992174395Sjkoshystatic void
3993186037Sjkoshypmc_post_callchain_callback(void)
3994174395Sjkoshy{
3995174395Sjkoshy	struct thread *td;
3996174395Sjkoshy
3997174395Sjkoshy	td = curthread;
3998174395Sjkoshy
3999205998Sfabient	/*
4000205998Sfabient	 * If there is multiple PMCs for the same interrupt ignore new post
4001205998Sfabient	 */
4002205998Sfabient	if (td->td_pflags & TDP_CALLCHAIN)
4003205998Sfabient		return;
4004186037Sjkoshy
4005174395Sjkoshy	/*
4006186037Sjkoshy	 * Mark this thread as needing callchain capture.
4007186037Sjkoshy	 * `td->td_pflags' will be safe to touch because this thread
4008186037Sjkoshy	 * was in user space when it was interrupted.
4009174395Sjkoshy	 */
4010174395Sjkoshy	td->td_pflags |= TDP_CALLCHAIN;
4011174395Sjkoshy
4012174395Sjkoshy	/*
4013186037Sjkoshy	 * Don't let this thread migrate between CPUs until callchain
4014186037Sjkoshy	 * capture completes.
4015174395Sjkoshy	 */
4016186037Sjkoshy	sched_pin();
4017174395Sjkoshy
4018174395Sjkoshy	return;
4019174395Sjkoshy}
4020174395Sjkoshy
4021174395Sjkoshy/*
4022147191Sjkoshy * Interrupt processing.
4023147191Sjkoshy *
4024174395Sjkoshy * Find a free slot in the per-cpu array of samples and capture the
4025174395Sjkoshy * current callchain there.  If a sample was successfully added, a bit
4026174395Sjkoshy * is set in mask 'pmc_cpumask' denoting that the DO_SAMPLES hook
4027174395Sjkoshy * needs to be invoked from the clock handler.
4028147191Sjkoshy *
4029147191Sjkoshy * This function is meant to be called from an NMI handler.  It cannot
4030147191Sjkoshy * use any of the locking primitives supplied by the OS.
4031145256Sjkoshy */
4032145256Sjkoshy
4033147191Sjkoshyint
4034233628Sfabientpmc_process_interrupt(int cpu, int ring, struct pmc *pm, struct trapframe *tf,
4035174395Sjkoshy    int inuserspace)
4036145256Sjkoshy{
4037174395Sjkoshy	int error, callchaindepth;
4038147191Sjkoshy	struct thread *td;
4039147191Sjkoshy	struct pmc_sample *ps;
4040147191Sjkoshy	struct pmc_samplebuffer *psb;
4041145256Sjkoshy
4042147191Sjkoshy	error = 0;
4043145256Sjkoshy
4044174395Sjkoshy	/*
4045174395Sjkoshy	 * Allocate space for a sample buffer.
4046174395Sjkoshy	 */
4047233628Sfabient	psb = pmc_pcpu[cpu]->pc_sb[ring];
4048145256Sjkoshy
4049147191Sjkoshy	ps = psb->ps_write;
4050174395Sjkoshy	if (ps->ps_nsamples) {	/* in use, reader hasn't caught up */
4051147867Sjkoshy		pm->pm_stalled = 1;
4052147191Sjkoshy		atomic_add_int(&pmc_stats.pm_intr_bufferfull, 1);
4053174395Sjkoshy		PMCDBG(SAM,INT,1,"(spc) cpu=%d pm=%p tf=%p um=%d wr=%d rd=%d",
4054174395Sjkoshy		    cpu, pm, (void *) tf, inuserspace,
4055147191Sjkoshy		    (int) (psb->ps_write - psb->ps_samples),
4056147191Sjkoshy		    (int) (psb->ps_read - psb->ps_samples));
4057147191Sjkoshy		error = ENOMEM;
4058147191Sjkoshy		goto done;
4059147191Sjkoshy	}
4060145256Sjkoshy
4061174395Sjkoshy
4062174395Sjkoshy	/* Fill in entry. */
4063174395Sjkoshy	PMCDBG(SAM,INT,1,"cpu=%d pm=%p tf=%p um=%d wr=%d rd=%d", cpu, pm,
4064174395Sjkoshy	    (void *) tf, inuserspace,
4065147191Sjkoshy	    (int) (psb->ps_write - psb->ps_samples),
4066147191Sjkoshy	    (int) (psb->ps_read - psb->ps_samples));
4067145256Sjkoshy
4068186037Sjkoshy	KASSERT(pm->pm_runcount >= 0,
4069186037Sjkoshy	    ("[pmc,%d] pm=%p runcount %d", __LINE__, (void *) pm,
4070186037Sjkoshy		pm->pm_runcount));
4071186037Sjkoshy
4072208861Sfabient	atomic_add_rel_int(&pm->pm_runcount, 1);	/* hold onto PMC */
4073233628Sfabient
4074147191Sjkoshy	ps->ps_pmc = pm;
4075147191Sjkoshy	if ((td = curthread) && td->td_proc)
4076147191Sjkoshy		ps->ps_pid = td->td_proc->p_pid;
4077147191Sjkoshy	else
4078147191Sjkoshy		ps->ps_pid = -1;
4079174395Sjkoshy	ps->ps_cpu = cpu;
4080186037Sjkoshy	ps->ps_td = td;
4081174395Sjkoshy	ps->ps_flags = inuserspace ? PMC_CC_F_USERSPACE : 0;
4082145256Sjkoshy
4083174395Sjkoshy	callchaindepth = (pm->pm_flags & PMC_F_CALLCHAIN) ?
4084174395Sjkoshy	    pmc_callchaindepth : 1;
4085174395Sjkoshy
4086174395Sjkoshy	if (callchaindepth == 1)
4087174395Sjkoshy		ps->ps_pc[0] = PMC_TRAPFRAME_TO_PC(tf);
4088174395Sjkoshy	else {
4089174395Sjkoshy		/*
4090174395Sjkoshy		 * Kernel stack traversals can be done immediately,
4091174395Sjkoshy		 * while we defer to an AST for user space traversals.
4092174395Sjkoshy		 */
4093233628Sfabient		if (!inuserspace) {
4094174395Sjkoshy			callchaindepth =
4095174395Sjkoshy			    pmc_save_kernel_callchain(ps->ps_pc,
4096174395Sjkoshy				callchaindepth, tf);
4097233628Sfabient		} else {
4098186037Sjkoshy			pmc_post_callchain_callback();
4099174395Sjkoshy			callchaindepth = PMC_SAMPLE_INUSE;
4100174395Sjkoshy		}
4101174395Sjkoshy	}
4102174395Sjkoshy
4103174395Sjkoshy	ps->ps_nsamples = callchaindepth;	/* mark entry as in use */
4104174395Sjkoshy
4105147191Sjkoshy	/* increment write pointer, modulo ring buffer size */
4106147191Sjkoshy	ps++;
4107147191Sjkoshy	if (ps == psb->ps_fence)
4108147191Sjkoshy		psb->ps_write = psb->ps_samples;
4109147191Sjkoshy	else
4110147191Sjkoshy		psb->ps_write = ps;
4111145256Sjkoshy
4112147191Sjkoshy done:
4113147191Sjkoshy	/* mark CPU as needing processing */
4114222813Sattilio	CPU_SET_ATOMIC(cpu, &pmc_cpumask);
4115147191Sjkoshy
4116174395Sjkoshy	return (error);
4117145256Sjkoshy}
4118145256Sjkoshy
4119174395Sjkoshy/*
4120174395Sjkoshy * Capture a user call chain.  This function will be called from ast()
4121174395Sjkoshy * before control returns to userland and before the process gets
4122174395Sjkoshy * rescheduled.
4123174395Sjkoshy */
4124147191Sjkoshy
4125174395Sjkoshystatic void
4126233628Sfabientpmc_capture_user_callchain(int cpu, int ring, struct trapframe *tf)
4127174395Sjkoshy{
4128174395Sjkoshy	int i;
4129174395Sjkoshy	struct pmc *pm;
4130186037Sjkoshy	struct thread *td;
4131174395Sjkoshy	struct pmc_sample *ps;
4132174395Sjkoshy	struct pmc_samplebuffer *psb;
4133186037Sjkoshy#ifdef	INVARIANTS
4134186037Sjkoshy	int ncallchains;
4135186037Sjkoshy#endif
4136174395Sjkoshy
4137233628Sfabient	psb = pmc_pcpu[cpu]->pc_sb[ring];
4138186037Sjkoshy	td = curthread;
4139174395Sjkoshy
4140186037Sjkoshy	KASSERT(td->td_pflags & TDP_CALLCHAIN,
4141186037Sjkoshy	    ("[pmc,%d] Retrieving callchain for thread that doesn't want it",
4142186037Sjkoshy		__LINE__));
4143186037Sjkoshy
4144186037Sjkoshy#ifdef	INVARIANTS
4145186037Sjkoshy	ncallchains = 0;
4146186037Sjkoshy#endif
4147186037Sjkoshy
4148174395Sjkoshy	/*
4149174395Sjkoshy	 * Iterate through all deferred callchain requests.
4150174395Sjkoshy	 */
4151174395Sjkoshy
4152186037Sjkoshy	ps = psb->ps_samples;
4153186037Sjkoshy	for (i = 0; i < pmc_nsamples; i++, ps++) {
4154174395Sjkoshy
4155174395Sjkoshy		if (ps->ps_nsamples != PMC_SAMPLE_INUSE)
4156174395Sjkoshy			continue;
4157186037Sjkoshy		if (ps->ps_td != td)
4158186037Sjkoshy			continue;
4159174395Sjkoshy
4160186037Sjkoshy		KASSERT(ps->ps_cpu == cpu,
4161186037Sjkoshy		    ("[pmc,%d] cpu mismatch ps_cpu=%d pcpu=%d", __LINE__,
4162186037Sjkoshy			ps->ps_cpu, PCPU_GET(cpuid)));
4163186037Sjkoshy
4164174395Sjkoshy		pm = ps->ps_pmc;
4165174395Sjkoshy
4166174395Sjkoshy		KASSERT(pm->pm_flags & PMC_F_CALLCHAIN,
4167174395Sjkoshy		    ("[pmc,%d] Retrieving callchain for PMC that doesn't "
4168174395Sjkoshy			"want it", __LINE__));
4169174395Sjkoshy
4170186037Sjkoshy		KASSERT(pm->pm_runcount > 0,
4171186037Sjkoshy		    ("[pmc,%d] runcount %d", __LINE__, pm->pm_runcount));
4172186037Sjkoshy
4173174395Sjkoshy		/*
4174174395Sjkoshy		 * Retrieve the callchain and mark the sample buffer
4175174395Sjkoshy		 * as 'processable' by the timer tick sweep code.
4176174395Sjkoshy		 */
4177174395Sjkoshy		ps->ps_nsamples = pmc_save_user_callchain(ps->ps_pc,
4178174395Sjkoshy		    pmc_callchaindepth, tf);
4179186037Sjkoshy
4180186037Sjkoshy#ifdef	INVARIANTS
4181186037Sjkoshy		ncallchains++;
4182186037Sjkoshy#endif
4183174395Sjkoshy	}
4184174395Sjkoshy
4185186037Sjkoshy	KASSERT(ncallchains > 0,
4186186037Sjkoshy	    ("[pmc,%d] cpu %d didn't find a sample to collect", __LINE__,
4187186037Sjkoshy		cpu));
4188186037Sjkoshy
4189242361Sattilio	KASSERT(td->td_pinned == 1,
4190233628Sfabient	    ("[pmc,%d] invalid td_pinned value", __LINE__));
4191233628Sfabient	sched_unpin();	/* Can migrate safely now. */
4192233628Sfabient
4193174395Sjkoshy	return;
4194174395Sjkoshy}
4195174395Sjkoshy
4196145256Sjkoshy/*
4197147191Sjkoshy * Process saved PC samples.
4198145256Sjkoshy */
4199145256Sjkoshy
4200145256Sjkoshystatic void
4201233628Sfabientpmc_process_samples(int cpu, int ring)
4202145256Sjkoshy{
4203147191Sjkoshy	struct pmc *pm;
4204185363Sjkoshy	int adjri, n;
4205147191Sjkoshy	struct thread *td;
4206147191Sjkoshy	struct pmc_owner *po;
4207147191Sjkoshy	struct pmc_sample *ps;
4208184802Sjkoshy	struct pmc_classdep *pcd;
4209147191Sjkoshy	struct pmc_samplebuffer *psb;
4210145256Sjkoshy
4211147191Sjkoshy	KASSERT(PCPU_GET(cpuid) == cpu,
4212147191Sjkoshy	    ("[pmc,%d] not on the correct CPU pcpu=%d cpu=%d", __LINE__,
4213147191Sjkoshy		PCPU_GET(cpuid), cpu));
4214145256Sjkoshy
4215233628Sfabient	psb = pmc_pcpu[cpu]->pc_sb[ring];
4216147191Sjkoshy
4217147191Sjkoshy	for (n = 0; n < pmc_nsamples; n++) { /* bound on #iterations */
4218147191Sjkoshy
4219147191Sjkoshy		ps = psb->ps_read;
4220174395Sjkoshy		if (ps->ps_nsamples == PMC_SAMPLE_FREE)
4221147191Sjkoshy			break;
4222147191Sjkoshy
4223147191Sjkoshy		pm = ps->ps_pmc;
4224186037Sjkoshy
4225186037Sjkoshy		KASSERT(pm->pm_runcount > 0,
4226186037Sjkoshy		    ("[pmc,%d] pm=%p runcount %d", __LINE__, (void *) pm,
4227186037Sjkoshy			pm->pm_runcount));
4228186037Sjkoshy
4229147191Sjkoshy		po = pm->pm_owner;
4230147191Sjkoshy
4231147191Sjkoshy		KASSERT(PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)),
4232147191Sjkoshy		    ("[pmc,%d] pmc=%p non-sampling mode=%d", __LINE__,
4233147191Sjkoshy			pm, PMC_TO_MODE(pm)));
4234147191Sjkoshy
4235147191Sjkoshy		/* Ignore PMCs that have been switched off */
4236147191Sjkoshy		if (pm->pm_state != PMC_STATE_RUNNING)
4237147191Sjkoshy			goto entrydone;
4238147191Sjkoshy
4239233628Sfabient		/* If there is a pending AST wait for completion */
4240233628Sfabient		if (ps->ps_nsamples == PMC_SAMPLE_INUSE) {
4241233628Sfabient			/* Need a rescan at a later time. */
4242233628Sfabient			CPU_SET_ATOMIC(cpu, &pmc_cpumask);
4243233628Sfabient			break;
4244233628Sfabient		}
4245233628Sfabient
4246174395Sjkoshy		PMCDBG(SAM,OPS,1,"cpu=%d pm=%p n=%d fl=%x wr=%d rd=%d", cpu,
4247174395Sjkoshy		    pm, ps->ps_nsamples, ps->ps_flags,
4248147191Sjkoshy		    (int) (psb->ps_write - psb->ps_samples),
4249147191Sjkoshy		    (int) (psb->ps_read - psb->ps_samples));
4250147191Sjkoshy
4251147191Sjkoshy		/*
4252147191Sjkoshy		 * If this is a process-mode PMC that is attached to
4253147191Sjkoshy		 * its owner, and if the PC is in user mode, update
4254147191Sjkoshy		 * profiling statistics like timer-based profiling
4255147191Sjkoshy		 * would have done.
4256147191Sjkoshy		 */
4257147191Sjkoshy		if (pm->pm_flags & PMC_F_ATTACHED_TO_OWNER) {
4258174395Sjkoshy			if (ps->ps_flags & PMC_CC_F_USERSPACE) {
4259147191Sjkoshy				td = FIRST_THREAD_IN_PROC(po->po_owner);
4260174395Sjkoshy				addupc_intr(td, ps->ps_pc[0], 1);
4261147191Sjkoshy			}
4262147191Sjkoshy			goto entrydone;
4263147191Sjkoshy		}
4264147191Sjkoshy
4265147191Sjkoshy		/*
4266147191Sjkoshy		 * Otherwise, this is either a sampling mode PMC that
4267147191Sjkoshy		 * is attached to a different process than its owner,
4268147191Sjkoshy		 * or a system-wide sampling PMC.  Dispatch a log
4269147191Sjkoshy		 * entry to the PMC's owner process.
4270147191Sjkoshy		 */
4271174395Sjkoshy		pmclog_process_callchain(pm, ps);
4272147191Sjkoshy
4273147191Sjkoshy	entrydone:
4274233628Sfabient		ps->ps_nsamples = 0; /* mark entry as free */
4275208861Sfabient		atomic_subtract_rel_int(&pm->pm_runcount, 1);
4276147191Sjkoshy
4277147191Sjkoshy		/* increment read pointer, modulo sample size */
4278147191Sjkoshy		if (++ps == psb->ps_fence)
4279147191Sjkoshy			psb->ps_read = psb->ps_samples;
4280147191Sjkoshy		else
4281147191Sjkoshy			psb->ps_read = ps;
4282147191Sjkoshy	}
4283147191Sjkoshy
4284147191Sjkoshy	atomic_add_int(&pmc_stats.pm_log_sweeps, 1);
4285147191Sjkoshy
4286147191Sjkoshy	/* Do not re-enable stalled PMCs if we failed to process any samples */
4287147191Sjkoshy	if (n == 0)
4288147191Sjkoshy		return;
4289147191Sjkoshy
4290147191Sjkoshy	/*
4291147191Sjkoshy	 * Restart any stalled sampling PMCs on this CPU.
4292147191Sjkoshy	 *
4293147867Sjkoshy	 * If the NMI handler sets the pm_stalled field of a PMC after
4294147867Sjkoshy	 * the check below, we'll end up processing the stalled PMC at
4295147867Sjkoshy	 * the next hardclock tick.
4296147191Sjkoshy	 */
4297147191Sjkoshy	for (n = 0; n < md->pmd_npmc; n++) {
4298184802Sjkoshy		pcd = pmc_ri_to_classdep(md, n, &adjri);
4299184802Sjkoshy		KASSERT(pcd != NULL,
4300184802Sjkoshy		    ("[pmc,%d] null pcd ri=%d", __LINE__, n));
4301184802Sjkoshy		(void) (*pcd->pcd_get_config)(cpu,adjri,&pm);
4302184802Sjkoshy
4303147191Sjkoshy		if (pm == NULL ||			 /* !cfg'ed */
4304147191Sjkoshy		    pm->pm_state != PMC_STATE_RUNNING || /* !active */
4305147191Sjkoshy		    !PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)) || /* !sampling */
4306147867Sjkoshy		    pm->pm_stalled == 0) /* !stalled */
4307147191Sjkoshy			continue;
4308147191Sjkoshy
4309147867Sjkoshy		pm->pm_stalled = 0;
4310184802Sjkoshy		(*pcd->pcd_start_pmc)(cpu, adjri);
4311147191Sjkoshy	}
4312145256Sjkoshy}
4313145256Sjkoshy
4314145256Sjkoshy/*
4315145256Sjkoshy * Event handlers.
4316145256Sjkoshy */
4317145256Sjkoshy
4318145256Sjkoshy/*
4319145256Sjkoshy * Handle a process exit.
4320145256Sjkoshy *
4321147191Sjkoshy * Remove this process from all hash tables.  If this process
4322147191Sjkoshy * owned any PMCs, turn off those PMCs and deallocate them,
4323147191Sjkoshy * removing any associations with target processes.
4324147191Sjkoshy *
4325147191Sjkoshy * This function will be called by the last 'thread' of a
4326147191Sjkoshy * process.
4327147191Sjkoshy *
4328145256Sjkoshy * XXX This eventhandler gets called early in the exit process.
4329145256Sjkoshy * Consider using a 'hook' invocation from thread_exit() or equivalent
4330145256Sjkoshy * spot.  Another negative is that kse_exit doesn't seem to call
4331145256Sjkoshy * exit1() [??].
4332147191Sjkoshy *
4333145256Sjkoshy */
4334145256Sjkoshy
4335145256Sjkoshystatic void
4336145256Sjkoshypmc_process_exit(void *arg __unused, struct proc *p)
4337145256Sjkoshy{
4338184802Sjkoshy	struct pmc *pm;
4339184802Sjkoshy	int adjri, cpu;
4340184802Sjkoshy	unsigned int ri;
4341145256Sjkoshy	int is_using_hwpmcs;
4342184802Sjkoshy	struct pmc_owner *po;
4343147191Sjkoshy	struct pmc_process *pp;
4344184802Sjkoshy	struct pmc_classdep *pcd;
4345147191Sjkoshy	pmc_value_t newvalue, tmp;
4346145256Sjkoshy
4347145256Sjkoshy	PROC_LOCK(p);
4348145256Sjkoshy	is_using_hwpmcs = p->p_flag & P_HWPMC;
4349145256Sjkoshy	PROC_UNLOCK(p);
4350145256Sjkoshy
4351147191Sjkoshy	/*
4352147191Sjkoshy	 * Log a sysexit event to all SS PMC owners.
4353147191Sjkoshy	 */
4354147191Sjkoshy	LIST_FOREACH(po, &pmc_ss_owners, po_ssnext)
4355147191Sjkoshy	    if (po->po_flags & PMC_PO_OWNS_LOGFILE)
4356147191Sjkoshy		    pmclog_process_sysexit(po, p->p_pid);
4357145256Sjkoshy
4358147191Sjkoshy	if (!is_using_hwpmcs)
4359147191Sjkoshy		return;
4360147191Sjkoshy
4361147191Sjkoshy	PMC_GET_SX_XLOCK();
4362147191Sjkoshy	PMCDBG(PRC,EXT,1,"process-exit proc=%p (%d, %s)", p, p->p_pid,
4363147191Sjkoshy	    p->p_comm);
4364147191Sjkoshy
4365147191Sjkoshy	/*
4366147191Sjkoshy	 * Since this code is invoked by the last thread in an exiting
4367147191Sjkoshy	 * process, we would have context switched IN at some prior
4368147191Sjkoshy	 * point.  However, with PREEMPTION, kernel mode context
4369147191Sjkoshy	 * switches may happen any time, so we want to disable a
4370147191Sjkoshy	 * context switch OUT till we get any PMCs targetting this
4371147191Sjkoshy	 * process off the hardware.
4372147191Sjkoshy	 *
4373147191Sjkoshy	 * We also need to atomically remove this process'
4374147191Sjkoshy	 * entry from our target process hash table, using
4375147191Sjkoshy	 * PMC_FLAG_REMOVE.
4376147191Sjkoshy	 */
4377147191Sjkoshy	PMCDBG(PRC,EXT,1, "process-exit proc=%p (%d, %s)", p, p->p_pid,
4378147191Sjkoshy	    p->p_comm);
4379147191Sjkoshy
4380147191Sjkoshy	critical_enter(); /* no preemption */
4381147191Sjkoshy
4382147191Sjkoshy	cpu = curthread->td_oncpu;
4383147191Sjkoshy
4384147191Sjkoshy	if ((pp = pmc_find_process_descriptor(p,
4385147191Sjkoshy		 PMC_FLAG_REMOVE)) != NULL) {
4386147191Sjkoshy
4387147191Sjkoshy		PMCDBG(PRC,EXT,2,
4388147191Sjkoshy		    "process-exit proc=%p pmc-process=%p", p, pp);
4389147191Sjkoshy
4390147191Sjkoshy		/*
4391147191Sjkoshy		 * The exiting process could the target of
4392147191Sjkoshy		 * some PMCs which will be running on
4393147191Sjkoshy		 * currently executing CPU.
4394147191Sjkoshy		 *
4395147191Sjkoshy		 * We need to turn these PMCs off like we
4396147191Sjkoshy		 * would do at context switch OUT time.
4397147191Sjkoshy		 */
4398147191Sjkoshy		for (ri = 0; ri < md->pmd_npmc; ri++) {
4399147191Sjkoshy
4400147191Sjkoshy			/*
4401147191Sjkoshy			 * Pick up the pmc pointer from hardware
4402147191Sjkoshy			 * state similar to the CSW_OUT code.
4403147191Sjkoshy			 */
4404147191Sjkoshy			pm = NULL;
4405147191Sjkoshy
4406184802Sjkoshy			pcd = pmc_ri_to_classdep(md, ri, &adjri);
4407184802Sjkoshy
4408184802Sjkoshy			(void) (*pcd->pcd_get_config)(cpu, adjri, &pm);
4409184802Sjkoshy
4410147191Sjkoshy			PMCDBG(PRC,EXT,2, "ri=%d pm=%p", ri, pm);
4411147191Sjkoshy
4412147191Sjkoshy			if (pm == NULL ||
4413147191Sjkoshy			    !PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm)))
4414147191Sjkoshy				continue;
4415147191Sjkoshy
4416147191Sjkoshy			PMCDBG(PRC,EXT,2, "ppmcs[%d]=%p pm=%p "
4417147191Sjkoshy			    "state=%d", ri, pp->pp_pmcs[ri].pp_pmc,
4418147191Sjkoshy			    pm, pm->pm_state);
4419147191Sjkoshy
4420147191Sjkoshy			KASSERT(PMC_TO_ROWINDEX(pm) == ri,
4421147191Sjkoshy			    ("[pmc,%d] ri mismatch pmc(%d) ri(%d)",
4422147191Sjkoshy				__LINE__, PMC_TO_ROWINDEX(pm), ri));
4423147191Sjkoshy
4424147191Sjkoshy			KASSERT(pm == pp->pp_pmcs[ri].pp_pmc,
4425147191Sjkoshy			    ("[pmc,%d] pm %p != pp_pmcs[%d] %p",
4426147191Sjkoshy				__LINE__, pm, ri, pp->pp_pmcs[ri].pp_pmc));
4427147191Sjkoshy
4428184802Sjkoshy			(void) pcd->pcd_stop_pmc(cpu, adjri);
4429147191Sjkoshy
4430147191Sjkoshy			KASSERT(pm->pm_runcount > 0,
4431147191Sjkoshy			    ("[pmc,%d] bad runcount ri %d rc %d",
4432147191Sjkoshy				__LINE__, ri, pm->pm_runcount));
4433147191Sjkoshy
4434147867Sjkoshy			/* Stop hardware only if it is actually running */
4435147191Sjkoshy			if (pm->pm_state == PMC_STATE_RUNNING &&
4436147867Sjkoshy			    pm->pm_stalled == 0) {
4437184802Sjkoshy				pcd->pcd_read_pmc(cpu, adjri, &newvalue);
4438147191Sjkoshy				tmp = newvalue -
4439147191Sjkoshy				    PMC_PCPU_SAVED(cpu,ri);
4440147191Sjkoshy
4441147191Sjkoshy				mtx_pool_lock_spin(pmc_mtxpool, pm);
4442147191Sjkoshy				pm->pm_gv.pm_savedvalue += tmp;
4443147191Sjkoshy				pp->pp_pmcs[ri].pp_pmcval += tmp;
4444147191Sjkoshy				mtx_pool_unlock_spin(pmc_mtxpool, pm);
4445147191Sjkoshy			}
4446147191Sjkoshy
4447208861Sfabient			atomic_subtract_rel_int(&pm->pm_runcount,1);
4448147191Sjkoshy
4449147191Sjkoshy			KASSERT((int) pm->pm_runcount >= 0,
4450147191Sjkoshy			    ("[pmc,%d] runcount is %d", __LINE__, ri));
4451147191Sjkoshy
4452184802Sjkoshy			(void) pcd->pcd_config_pmc(cpu, adjri, NULL);
4453147191Sjkoshy		}
4454147191Sjkoshy
4455147191Sjkoshy		/*
4456147191Sjkoshy		 * Inform the MD layer of this pseudo "context switch
4457147191Sjkoshy		 * out"
4458147191Sjkoshy		 */
4459147191Sjkoshy		(void) md->pmd_switch_out(pmc_pcpu[cpu], pp);
4460147191Sjkoshy
4461147191Sjkoshy		critical_exit(); /* ok to be pre-empted now */
4462147191Sjkoshy
4463147191Sjkoshy		/*
4464147191Sjkoshy		 * Unlink this process from the PMCs that are
4465147191Sjkoshy		 * targetting it.  This will send a signal to
4466147191Sjkoshy		 * all PMC owner's whose PMCs are orphaned.
4467147191Sjkoshy		 *
4468147191Sjkoshy		 * Log PMC value at exit time if requested.
4469147191Sjkoshy		 */
4470147191Sjkoshy		for (ri = 0; ri < md->pmd_npmc; ri++)
4471147191Sjkoshy			if ((pm = pp->pp_pmcs[ri].pp_pmc) != NULL) {
4472147867Sjkoshy				if (pm->pm_flags & PMC_F_NEEDS_LOGFILE &&
4473147867Sjkoshy				    PMC_IS_COUNTING_MODE(PMC_TO_MODE(pm)))
4474147191Sjkoshy					pmclog_process_procexit(pm, pp);
4475147191Sjkoshy				pmc_unlink_target_process(pm, pp);
4476147191Sjkoshy			}
4477184205Sdes		free(pp, M_PMC);
4478147191Sjkoshy
4479147191Sjkoshy	} else
4480147191Sjkoshy		critical_exit(); /* pp == NULL */
4481147191Sjkoshy
4482147191Sjkoshy
4483147191Sjkoshy	/*
4484147191Sjkoshy	 * If the process owned PMCs, free them up and free up
4485147191Sjkoshy	 * memory.
4486147191Sjkoshy	 */
4487147191Sjkoshy	if ((po = pmc_find_owner_descriptor(p)) != NULL) {
4488147191Sjkoshy		pmc_remove_owner(po);
4489147191Sjkoshy		pmc_destroy_owner_descriptor(po);
4490145256Sjkoshy	}
4491147191Sjkoshy
4492147191Sjkoshy	sx_xunlock(&pmc_sx);
4493145256Sjkoshy}
4494145256Sjkoshy
4495145256Sjkoshy/*
4496145256Sjkoshy * Handle a process fork.
4497145256Sjkoshy *
4498145256Sjkoshy * If the parent process 'p1' is under HWPMC monitoring, then copy
4499145256Sjkoshy * over any attached PMCs that have 'do_descendants' semantics.
4500145256Sjkoshy */
4501145256Sjkoshy
4502145256Sjkoshystatic void
4503147191Sjkoshypmc_process_fork(void *arg __unused, struct proc *p1, struct proc *newproc,
4504145256Sjkoshy    int flags)
4505145256Sjkoshy{
4506145256Sjkoshy	int is_using_hwpmcs;
4507147191Sjkoshy	unsigned int ri;
4508147191Sjkoshy	uint32_t do_descendants;
4509147191Sjkoshy	struct pmc *pm;
4510147191Sjkoshy	struct pmc_owner *po;
4511147191Sjkoshy	struct pmc_process *ppnew, *ppold;
4512145256Sjkoshy
4513145256Sjkoshy	(void) flags;		/* unused parameter */
4514145256Sjkoshy
4515145256Sjkoshy	PROC_LOCK(p1);
4516145256Sjkoshy	is_using_hwpmcs = p1->p_flag & P_HWPMC;
4517145256Sjkoshy	PROC_UNLOCK(p1);
4518145256Sjkoshy
4519147191Sjkoshy	/*
4520147191Sjkoshy	 * If there are system-wide sampling PMCs active, we need to
4521147191Sjkoshy	 * log all fork events to their owner's logs.
4522147191Sjkoshy	 */
4523147191Sjkoshy
4524147191Sjkoshy	LIST_FOREACH(po, &pmc_ss_owners, po_ssnext)
4525147191Sjkoshy	    if (po->po_flags & PMC_PO_OWNS_LOGFILE)
4526147191Sjkoshy		    pmclog_process_procfork(po, p1->p_pid, newproc->p_pid);
4527147191Sjkoshy
4528147191Sjkoshy	if (!is_using_hwpmcs)
4529147191Sjkoshy		return;
4530147191Sjkoshy
4531147191Sjkoshy	PMC_GET_SX_XLOCK();
4532147191Sjkoshy	PMCDBG(PMC,FRK,1, "process-fork proc=%p (%d, %s) -> %p", p1,
4533147191Sjkoshy	    p1->p_pid, p1->p_comm, newproc);
4534147191Sjkoshy
4535147191Sjkoshy	/*
4536147191Sjkoshy	 * If the parent process (curthread->td_proc) is a
4537147191Sjkoshy	 * target of any PMCs, look for PMCs that are to be
4538147191Sjkoshy	 * inherited, and link these into the new process
4539147191Sjkoshy	 * descriptor.
4540147191Sjkoshy	 */
4541147191Sjkoshy	if ((ppold = pmc_find_process_descriptor(curthread->td_proc,
4542147191Sjkoshy		 PMC_FLAG_NONE)) == NULL)
4543147191Sjkoshy		goto done;		/* nothing to do */
4544147191Sjkoshy
4545147191Sjkoshy	do_descendants = 0;
4546147191Sjkoshy	for (ri = 0; ri < md->pmd_npmc; ri++)
4547147191Sjkoshy		if ((pm = ppold->pp_pmcs[ri].pp_pmc) != NULL)
4548147191Sjkoshy			do_descendants |= pm->pm_flags & PMC_F_DESCENDANTS;
4549147191Sjkoshy	if (do_descendants == 0) /* nothing to do */
4550147191Sjkoshy		goto done;
4551147191Sjkoshy
4552147191Sjkoshy	/* allocate a descriptor for the new process  */
4553147191Sjkoshy	if ((ppnew = pmc_find_process_descriptor(newproc,
4554147191Sjkoshy		 PMC_FLAG_ALLOCATE)) == NULL)
4555147191Sjkoshy		goto done;
4556147191Sjkoshy
4557147191Sjkoshy	/*
4558147191Sjkoshy	 * Run through all PMCs that were targeting the old process
4559147191Sjkoshy	 * and which specified F_DESCENDANTS and attach them to the
4560147191Sjkoshy	 * new process.
4561147191Sjkoshy	 *
4562147191Sjkoshy	 * Log the fork event to all owners of PMCs attached to this
4563147191Sjkoshy	 * process, if not already logged.
4564147191Sjkoshy	 */
4565147191Sjkoshy	for (ri = 0; ri < md->pmd_npmc; ri++)
4566147191Sjkoshy		if ((pm = ppold->pp_pmcs[ri].pp_pmc) != NULL &&
4567147191Sjkoshy		    (pm->pm_flags & PMC_F_DESCENDANTS)) {
4568147191Sjkoshy			pmc_link_target_process(pm, ppnew);
4569147191Sjkoshy			po = pm->pm_owner;
4570147191Sjkoshy			if (po->po_sscount == 0 &&
4571147191Sjkoshy			    po->po_flags & PMC_PO_OWNS_LOGFILE)
4572147191Sjkoshy				pmclog_process_procfork(po, p1->p_pid,
4573147191Sjkoshy				    newproc->p_pid);
4574147191Sjkoshy		}
4575147191Sjkoshy
4576147191Sjkoshy	/*
4577147191Sjkoshy	 * Now mark the new process as being tracked by this driver.
4578147191Sjkoshy	 */
4579147191Sjkoshy	PROC_LOCK(newproc);
4580147191Sjkoshy	newproc->p_flag |= P_HWPMC;
4581147191Sjkoshy	PROC_UNLOCK(newproc);
4582147191Sjkoshy
4583147191Sjkoshy done:
4584147191Sjkoshy	sx_xunlock(&pmc_sx);
4585145256Sjkoshy}
4586145256Sjkoshy
4587254813Smarkjstatic void
4588254813Smarkjpmc_kld_load(void *arg __unused, linker_file_t lf)
4589254813Smarkj{
4590254813Smarkj	struct pmc_owner *po;
4591145256Sjkoshy
4592254813Smarkj	sx_slock(&pmc_sx);
4593254813Smarkj
4594254813Smarkj	/*
4595254813Smarkj	 * Notify owners of system sampling PMCs about KLD operations.
4596254813Smarkj	 */
4597254813Smarkj	LIST_FOREACH(po, &pmc_ss_owners, po_ssnext)
4598254813Smarkj		if (po->po_flags & PMC_PO_OWNS_LOGFILE)
4599254813Smarkj			pmclog_process_map_in(po, (pid_t) -1,
4600254813Smarkj			    (uintfptr_t) lf->address, lf->filename);
4601254813Smarkj
4602254813Smarkj	/*
4603254813Smarkj	 * TODO: Notify owners of (all) process-sampling PMCs too.
4604254813Smarkj	 */
4605254813Smarkj
4606254813Smarkj	sx_sunlock(&pmc_sx);
4607254813Smarkj}
4608254813Smarkj
4609254813Smarkjstatic void
4610254813Smarkjpmc_kld_unload(void *arg __unused, const char *filename __unused,
4611254813Smarkj    caddr_t address, size_t size)
4612254813Smarkj{
4613254813Smarkj	struct pmc_owner *po;
4614254813Smarkj
4615254813Smarkj	sx_slock(&pmc_sx);
4616254813Smarkj
4617254813Smarkj	LIST_FOREACH(po, &pmc_ss_owners, po_ssnext)
4618254813Smarkj		if (po->po_flags & PMC_PO_OWNS_LOGFILE)
4619254813Smarkj			pmclog_process_map_out(po, (pid_t) -1,
4620254813Smarkj			    (uintfptr_t) address, (uintfptr_t) address + size);
4621254813Smarkj
4622254813Smarkj	/*
4623254813Smarkj	 * TODO: Notify owners of process-sampling PMCs.
4624254813Smarkj	 */
4625254813Smarkj
4626254813Smarkj	sx_sunlock(&pmc_sx);
4627254813Smarkj}
4628254813Smarkj
4629145256Sjkoshy/*
4630145256Sjkoshy * initialization
4631145256Sjkoshy */
4632145256Sjkoshy
4633145256Sjkoshystatic const char *pmc_name_of_pmcclass[] = {
4634145256Sjkoshy#undef	__PMC_CLASS
4635145256Sjkoshy#define	__PMC_CLASS(N) #N ,
4636145256Sjkoshy	__PMC_CLASSES()
4637145256Sjkoshy};
4638145256Sjkoshy
4639233628Sfabient/*
4640233628Sfabient * Base class initializer: allocate structure and set default classes.
4641233628Sfabient */
4642233628Sfabientstruct pmc_mdep *
4643233628Sfabientpmc_mdep_alloc(int nclasses)
4644233628Sfabient{
4645233628Sfabient	struct pmc_mdep *md;
4646233628Sfabient	int	n;
4647233628Sfabient
4648233628Sfabient	/* SOFT + md classes */
4649233628Sfabient	n = 1 + nclasses;
4650233628Sfabient	md = malloc(sizeof(struct pmc_mdep) + n *
4651233628Sfabient	    sizeof(struct pmc_classdep), M_PMC, M_WAITOK|M_ZERO);
4652250105Sdavide	md->pmd_nclass = n;
4653233628Sfabient
4654250105Sdavide	/* Add base class. */
4655250105Sdavide	pmc_soft_initialize(md);
4656233628Sfabient	return md;
4657233628Sfabient}
4658233628Sfabient
4659233628Sfabientvoid
4660233628Sfabientpmc_mdep_free(struct pmc_mdep *md)
4661233628Sfabient{
4662233628Sfabient	pmc_soft_finalize(md);
4663233628Sfabient	free(md, M_PMC);
4664233628Sfabient}
4665233628Sfabient
4666145256Sjkoshystatic int
4667233628Sfabientgeneric_switch_in(struct pmc_cpu *pc, struct pmc_process *pp)
4668233628Sfabient{
4669233628Sfabient	(void) pc; (void) pp;
4670233628Sfabient
4671233628Sfabient	return (0);
4672233628Sfabient}
4673233628Sfabient
4674233628Sfabientstatic int
4675233628Sfabientgeneric_switch_out(struct pmc_cpu *pc, struct pmc_process *pp)
4676233628Sfabient{
4677233628Sfabient	(void) pc; (void) pp;
4678233628Sfabient
4679233628Sfabient	return (0);
4680233628Sfabient}
4681233628Sfabient
4682233628Sfabientstatic struct pmc_mdep *
4683233628Sfabientpmc_generic_cpu_initialize(void)
4684233628Sfabient{
4685233628Sfabient	struct pmc_mdep *md;
4686233628Sfabient
4687233628Sfabient	md = pmc_mdep_alloc(0);
4688233628Sfabient
4689233628Sfabient	md->pmd_cputype    = PMC_CPU_GENERIC;
4690233628Sfabient
4691233628Sfabient	md->pmd_pcpu_init  = NULL;
4692233628Sfabient	md->pmd_pcpu_fini  = NULL;
4693233628Sfabient	md->pmd_switch_in  = generic_switch_in;
4694233628Sfabient	md->pmd_switch_out = generic_switch_out;
4695233628Sfabient
4696233628Sfabient	return (md);
4697233628Sfabient}
4698233628Sfabient
4699233628Sfabientstatic void
4700233628Sfabientpmc_generic_cpu_finalize(struct pmc_mdep *md)
4701233628Sfabient{
4702233628Sfabient	(void) md;
4703233628Sfabient}
4704233628Sfabient
4705233628Sfabient
4706233628Sfabientstatic int
4707145256Sjkoshypmc_initialize(void)
4708145256Sjkoshy{
4709184802Sjkoshy	int c, cpu, error, n, ri;
4710183266Sjkoshy	unsigned int maxcpu;
4711145256Sjkoshy	struct pmc_binding pb;
4712174395Sjkoshy	struct pmc_sample *ps;
4713184802Sjkoshy	struct pmc_classdep *pcd;
4714147191Sjkoshy	struct pmc_samplebuffer *sb;
4715145256Sjkoshy
4716145256Sjkoshy	md = NULL;
4717145256Sjkoshy	error = 0;
4718145256Sjkoshy
4719153110Sru#ifdef	DEBUG
4720145256Sjkoshy	/* parse debug flags first */
4721145256Sjkoshy	if (TUNABLE_STR_FETCH(PMC_SYSCTL_NAME_PREFIX "debugflags",
4722145256Sjkoshy		pmc_debugstr, sizeof(pmc_debugstr)))
4723145256Sjkoshy		pmc_debugflags_parse(pmc_debugstr,
4724145256Sjkoshy		    pmc_debugstr+strlen(pmc_debugstr));
4725145256Sjkoshy#endif
4726145256Sjkoshy
4727145256Sjkoshy	PMCDBG(MOD,INI,0, "PMC Initialize (version %x)", PMC_VERSION);
4728145256Sjkoshy
4729148562Sjkoshy	/* check kernel version */
4730148562Sjkoshy	if (pmc_kernel_version != PMC_VERSION) {
4731148562Sjkoshy		if (pmc_kernel_version == 0)
4732148562Sjkoshy			printf("hwpmc: this kernel has not been compiled with "
4733148562Sjkoshy			    "'options HWPMC_HOOKS'.\n");
4734148562Sjkoshy		else
4735148562Sjkoshy			printf("hwpmc: kernel version (0x%x) does not match "
4736148562Sjkoshy			    "module version (0x%x).\n", pmc_kernel_version,
4737148562Sjkoshy			    PMC_VERSION);
4738148562Sjkoshy		return EPROGMISMATCH;
4739148562Sjkoshy	}
4740148562Sjkoshy
4741145256Sjkoshy	/*
4742145256Sjkoshy	 * check sysctl parameters
4743145256Sjkoshy	 */
4744145256Sjkoshy
4745145256Sjkoshy	if (pmc_hashsize <= 0) {
4746174395Sjkoshy		(void) printf("hwpmc: tunable \"hashsize\"=%d must be "
4747174395Sjkoshy		    "greater than zero.\n", pmc_hashsize);
4748145256Sjkoshy		pmc_hashsize = PMC_HASH_SIZE;
4749145256Sjkoshy	}
4750145256Sjkoshy
4751147191Sjkoshy	if (pmc_nsamples <= 0 || pmc_nsamples > 65535) {
4752174395Sjkoshy		(void) printf("hwpmc: tunable \"nsamples\"=%d out of "
4753174395Sjkoshy		    "range.\n", pmc_nsamples);
4754147191Sjkoshy		pmc_nsamples = PMC_NSAMPLES;
4755147191Sjkoshy	}
4756145256Sjkoshy
4757174395Sjkoshy	if (pmc_callchaindepth <= 0 ||
4758174395Sjkoshy	    pmc_callchaindepth > PMC_CALLCHAIN_DEPTH_MAX) {
4759174395Sjkoshy		(void) printf("hwpmc: tunable \"callchaindepth\"=%d out of "
4760174395Sjkoshy		    "range.\n", pmc_callchaindepth);
4761174395Sjkoshy		pmc_callchaindepth = PMC_CALLCHAIN_DEPTH;
4762174395Sjkoshy	}
4763174395Sjkoshy
4764147191Sjkoshy	md = pmc_md_initialize();
4765233628Sfabient	if (md == NULL) {
4766233628Sfabient		/* Default to generic CPU. */
4767233628Sfabient		md = pmc_generic_cpu_initialize();
4768233628Sfabient		if (md == NULL)
4769233628Sfabient			return (ENOSYS);
4770233628Sfabient        }
4771147191Sjkoshy
4772184802Sjkoshy	KASSERT(md->pmd_nclass >= 1 && md->pmd_npmc >= 1,
4773184802Sjkoshy	    ("[pmc,%d] no classes or pmcs", __LINE__));
4774184802Sjkoshy
4775184802Sjkoshy	/* Compute the map from row-indices to classdep pointers. */
4776184802Sjkoshy	pmc_rowindex_to_classdep = malloc(sizeof(struct pmc_classdep *) *
4777184802Sjkoshy	    md->pmd_npmc, M_PMC, M_WAITOK|M_ZERO);
4778184802Sjkoshy
4779184802Sjkoshy	for (n = 0; n < md->pmd_npmc; n++)
4780184802Sjkoshy		pmc_rowindex_to_classdep[n] = NULL;
4781184802Sjkoshy	for (ri = c = 0; c < md->pmd_nclass; c++) {
4782184802Sjkoshy		pcd = &md->pmd_classdep[c];
4783184802Sjkoshy		for (n = 0; n < pcd->pcd_num; n++, ri++)
4784184802Sjkoshy			pmc_rowindex_to_classdep[ri] = pcd;
4785184802Sjkoshy	}
4786184802Sjkoshy
4787184802Sjkoshy	KASSERT(ri == md->pmd_npmc,
4788184802Sjkoshy	    ("[pmc,%d] npmc miscomputed: ri=%d, md->npmc=%d", __LINE__,
4789184802Sjkoshy	    ri, md->pmd_npmc));
4790184802Sjkoshy
4791183266Sjkoshy	maxcpu = pmc_cpu_max();
4792183266Sjkoshy
4793145256Sjkoshy	/* allocate space for the per-cpu array */
4794184802Sjkoshy	pmc_pcpu = malloc(maxcpu * sizeof(struct pmc_cpu *), M_PMC,
4795184802Sjkoshy	    M_WAITOK|M_ZERO);
4796145256Sjkoshy
4797145256Sjkoshy	/* per-cpu 'saved values' for managing process-mode PMCs */
4798184214Sdes	pmc_pcpu_saved = malloc(sizeof(pmc_value_t) * maxcpu * md->pmd_npmc,
4799184214Sdes	    M_PMC, M_WAITOK);
4800145256Sjkoshy
4801183266Sjkoshy	/* Perform CPU-dependent initialization. */
4802145256Sjkoshy	pmc_save_cpu_binding(&pb);
4803184802Sjkoshy	error = 0;
4804184802Sjkoshy	for (cpu = 0; error == 0 && cpu < maxcpu; cpu++) {
4805183266Sjkoshy		if (!pmc_cpu_is_active(cpu))
4806145256Sjkoshy			continue;
4807145256Sjkoshy		pmc_select_cpu(cpu);
4808184802Sjkoshy		pmc_pcpu[cpu] = malloc(sizeof(struct pmc_cpu) +
4809184802Sjkoshy		    md->pmd_npmc * sizeof(struct pmc_hw *), M_PMC,
4810184802Sjkoshy		    M_WAITOK|M_ZERO);
4811184802Sjkoshy		if (md->pmd_pcpu_init)
4812185363Sjkoshy			error = md->pmd_pcpu_init(md, cpu);
4813184802Sjkoshy		for (n = 0; error == 0 && n < md->pmd_nclass; n++)
4814184802Sjkoshy			error = md->pmd_classdep[n].pcd_pcpu_init(md, cpu);
4815145256Sjkoshy	}
4816145256Sjkoshy	pmc_restore_cpu_binding(&pb);
4817145256Sjkoshy
4818184802Sjkoshy	if (error)
4819184802Sjkoshy		return (error);
4820145256Sjkoshy
4821147191Sjkoshy	/* allocate space for the sample array */
4822183266Sjkoshy	for (cpu = 0; cpu < maxcpu; cpu++) {
4823183266Sjkoshy		if (!pmc_cpu_is_active(cpu))
4824147191Sjkoshy			continue;
4825184802Sjkoshy
4826184214Sdes		sb = malloc(sizeof(struct pmc_samplebuffer) +
4827147191Sjkoshy		    pmc_nsamples * sizeof(struct pmc_sample), M_PMC,
4828147191Sjkoshy		    M_WAITOK|M_ZERO);
4829147191Sjkoshy		sb->ps_read = sb->ps_write = sb->ps_samples;
4830153735Sjkoshy		sb->ps_fence = sb->ps_samples + pmc_nsamples;
4831184802Sjkoshy
4832147191Sjkoshy		KASSERT(pmc_pcpu[cpu] != NULL,
4833147191Sjkoshy		    ("[pmc,%d] cpu=%d Null per-cpu data", __LINE__, cpu));
4834147191Sjkoshy
4835184802Sjkoshy		sb->ps_callchains = malloc(pmc_callchaindepth * pmc_nsamples *
4836184802Sjkoshy		    sizeof(uintptr_t), M_PMC, M_WAITOK|M_ZERO);
4837174395Sjkoshy
4838174395Sjkoshy		for (n = 0, ps = sb->ps_samples; n < pmc_nsamples; n++, ps++)
4839174395Sjkoshy			ps->ps_pc = sb->ps_callchains +
4840174395Sjkoshy			    (n * pmc_callchaindepth);
4841174395Sjkoshy
4842233628Sfabient		pmc_pcpu[cpu]->pc_sb[PMC_HR] = sb;
4843233628Sfabient
4844233628Sfabient		sb = malloc(sizeof(struct pmc_samplebuffer) +
4845233628Sfabient		    pmc_nsamples * sizeof(struct pmc_sample), M_PMC,
4846233628Sfabient		    M_WAITOK|M_ZERO);
4847233628Sfabient		sb->ps_read = sb->ps_write = sb->ps_samples;
4848233628Sfabient		sb->ps_fence = sb->ps_samples + pmc_nsamples;
4849233628Sfabient
4850233628Sfabient		KASSERT(pmc_pcpu[cpu] != NULL,
4851233628Sfabient		    ("[pmc,%d] cpu=%d Null per-cpu data", __LINE__, cpu));
4852233628Sfabient
4853233628Sfabient		sb->ps_callchains = malloc(pmc_callchaindepth * pmc_nsamples *
4854233628Sfabient		    sizeof(uintptr_t), M_PMC, M_WAITOK|M_ZERO);
4855233628Sfabient
4856233628Sfabient		for (n = 0, ps = sb->ps_samples; n < pmc_nsamples; n++, ps++)
4857233628Sfabient			ps->ps_pc = sb->ps_callchains +
4858233628Sfabient			    (n * pmc_callchaindepth);
4859233628Sfabient
4860233628Sfabient		pmc_pcpu[cpu]->pc_sb[PMC_SR] = sb;
4861147191Sjkoshy	}
4862147191Sjkoshy
4863145256Sjkoshy	/* allocate space for the row disposition array */
4864145256Sjkoshy	pmc_pmcdisp = malloc(sizeof(enum pmc_mode) * md->pmd_npmc,
4865145256Sjkoshy	    M_PMC, M_WAITOK|M_ZERO);
4866145256Sjkoshy
4867145256Sjkoshy	/* mark all PMCs as available */
4868145256Sjkoshy	for (n = 0; n < (int) md->pmd_npmc; n++)
4869145256Sjkoshy		PMC_MARK_ROW_FREE(n);
4870145256Sjkoshy
4871145256Sjkoshy	/* allocate thread hash tables */
4872145256Sjkoshy	pmc_ownerhash = hashinit(pmc_hashsize, M_PMC,
4873145256Sjkoshy	    &pmc_ownerhashmask);
4874145256Sjkoshy
4875145256Sjkoshy	pmc_processhash = hashinit(pmc_hashsize, M_PMC,
4876145256Sjkoshy	    &pmc_processhashmask);
4877168856Sjkoshy	mtx_init(&pmc_processhash_mtx, "pmc-process-hash", "pmc-leaf",
4878168856Sjkoshy	    MTX_SPIN);
4879145256Sjkoshy
4880147191Sjkoshy	LIST_INIT(&pmc_ss_owners);
4881147191Sjkoshy	pmc_ss_count = 0;
4882147191Sjkoshy
4883145256Sjkoshy	/* allocate a pool of spin mutexes */
4884168856Sjkoshy	pmc_mtxpool = mtx_pool_create("pmc-leaf", pmc_mtxpool_size,
4885168856Sjkoshy	    MTX_SPIN);
4886145256Sjkoshy
4887145256Sjkoshy	PMCDBG(MOD,INI,1, "pmc_ownerhash=%p, mask=0x%lx "
4888145256Sjkoshy	    "targethash=%p mask=0x%lx", pmc_ownerhash, pmc_ownerhashmask,
4889145256Sjkoshy	    pmc_processhash, pmc_processhashmask);
4890145256Sjkoshy
4891145256Sjkoshy	/* register process {exit,fork,exec} handlers */
4892145256Sjkoshy	pmc_exit_tag = EVENTHANDLER_REGISTER(process_exit,
4893145256Sjkoshy	    pmc_process_exit, NULL, EVENTHANDLER_PRI_ANY);
4894145256Sjkoshy	pmc_fork_tag = EVENTHANDLER_REGISTER(process_fork,
4895145256Sjkoshy	    pmc_process_fork, NULL, EVENTHANDLER_PRI_ANY);
4896145256Sjkoshy
4897254813Smarkj	/* register kld event handlers */
4898254813Smarkj	pmc_kld_load_tag = EVENTHANDLER_REGISTER(kld_load, pmc_kld_load,
4899254813Smarkj	    NULL, EVENTHANDLER_PRI_ANY);
4900254813Smarkj	pmc_kld_unload_tag = EVENTHANDLER_REGISTER(kld_unload, pmc_kld_unload,
4901254813Smarkj	    NULL, EVENTHANDLER_PRI_ANY);
4902254813Smarkj
4903147191Sjkoshy	/* initialize logging */
4904147191Sjkoshy	pmclog_initialize();
4905147191Sjkoshy
4906145256Sjkoshy	/* set hook functions */
4907145256Sjkoshy	pmc_intr = md->pmd_intr;
4908145256Sjkoshy	pmc_hook = pmc_hook_handler;
4909145256Sjkoshy
4910145256Sjkoshy	if (error == 0) {
4911145256Sjkoshy		printf(PMC_MODULE_NAME ":");
4912149373Sjkoshy		for (n = 0; n < (int) md->pmd_nclass; n++) {
4913184802Sjkoshy			pcd = &md->pmd_classdep[n];
4914184997Sjkoshy			printf(" %s/%d/%d/0x%b",
4915184802Sjkoshy			    pmc_name_of_pmcclass[pcd->pcd_class],
4916184802Sjkoshy			    pcd->pcd_num,
4917184997Sjkoshy			    pcd->pcd_width,
4918184802Sjkoshy			    pcd->pcd_caps,
4919149373Sjkoshy			    "\20"
4920149373Sjkoshy			    "\1INT\2USR\3SYS\4EDG\5THR"
4921149373Sjkoshy			    "\6REA\7WRI\10INV\11QUA\12PRC"
4922149373Sjkoshy			    "\13TAG\14CSC");
4923149373Sjkoshy		}
4924145256Sjkoshy		printf("\n");
4925145256Sjkoshy	}
4926145256Sjkoshy
4927184802Sjkoshy	return (error);
4928145256Sjkoshy}
4929145256Sjkoshy
4930145256Sjkoshy/* prepare to be unloaded */
4931145256Sjkoshystatic void
4932145256Sjkoshypmc_cleanup(void)
4933145256Sjkoshy{
4934184802Sjkoshy	int c, cpu;
4935183266Sjkoshy	unsigned int maxcpu;
4936145256Sjkoshy	struct pmc_ownerhash *ph;
4937145256Sjkoshy	struct pmc_owner *po, *tmp;
4938145256Sjkoshy	struct pmc_binding pb;
4939153110Sru#ifdef	DEBUG
4940145256Sjkoshy	struct pmc_processhash *prh;
4941145256Sjkoshy#endif
4942145256Sjkoshy
4943145256Sjkoshy	PMCDBG(MOD,INI,0, "%s", "cleanup");
4944145256Sjkoshy
4945147191Sjkoshy	/* switch off sampling */
4946222813Sattilio	CPU_ZERO(&pmc_cpumask);
4947147191Sjkoshy	pmc_intr = NULL;
4948145256Sjkoshy
4949145256Sjkoshy	sx_xlock(&pmc_sx);
4950145256Sjkoshy	if (pmc_hook == NULL) {	/* being unloaded already */
4951145256Sjkoshy		sx_xunlock(&pmc_sx);
4952145256Sjkoshy		return;
4953145256Sjkoshy	}
4954145256Sjkoshy
4955145256Sjkoshy	pmc_hook = NULL; /* prevent new threads from entering module */
4956145256Sjkoshy
4957145256Sjkoshy	/* deregister event handlers */
4958145256Sjkoshy	EVENTHANDLER_DEREGISTER(process_fork, pmc_fork_tag);
4959145256Sjkoshy	EVENTHANDLER_DEREGISTER(process_exit, pmc_exit_tag);
4960254813Smarkj	EVENTHANDLER_DEREGISTER(kld_load, pmc_kld_load_tag);
4961254813Smarkj	EVENTHANDLER_DEREGISTER(kld_unload, pmc_kld_unload_tag);
4962145256Sjkoshy
4963145256Sjkoshy	/* send SIGBUS to all owner threads, free up allocations */
4964145256Sjkoshy	if (pmc_ownerhash)
4965145256Sjkoshy		for (ph = pmc_ownerhash;
4966145256Sjkoshy		     ph <= &pmc_ownerhash[pmc_ownerhashmask];
4967145256Sjkoshy		     ph++) {
4968145256Sjkoshy			LIST_FOREACH_SAFE(po, ph, po_next, tmp) {
4969145256Sjkoshy				pmc_remove_owner(po);
4970145256Sjkoshy
4971145256Sjkoshy				/* send SIGBUS to owner processes */
4972145256Sjkoshy				PMCDBG(MOD,INI,2, "cleanup signal proc=%p "
4973145256Sjkoshy				    "(%d, %s)", po->po_owner,
4974145256Sjkoshy				    po->po_owner->p_pid,
4975145256Sjkoshy				    po->po_owner->p_comm);
4976145256Sjkoshy
4977145256Sjkoshy				PROC_LOCK(po->po_owner);
4978225617Skmacy				kern_psignal(po->po_owner, SIGBUS);
4979145256Sjkoshy				PROC_UNLOCK(po->po_owner);
4980147191Sjkoshy
4981147191Sjkoshy				pmc_destroy_owner_descriptor(po);
4982145256Sjkoshy			}
4983145256Sjkoshy		}
4984145256Sjkoshy
4985145256Sjkoshy	/* reclaim allocated data structures */
4986145256Sjkoshy	if (pmc_mtxpool)
4987145256Sjkoshy		mtx_pool_destroy(&pmc_mtxpool);
4988145256Sjkoshy
4989145256Sjkoshy	mtx_destroy(&pmc_processhash_mtx);
4990145256Sjkoshy	if (pmc_processhash) {
4991153110Sru#ifdef	DEBUG
4992145256Sjkoshy		struct pmc_process *pp;
4993145256Sjkoshy
4994145256Sjkoshy		PMCDBG(MOD,INI,3, "%s", "destroy process hash");
4995145256Sjkoshy		for (prh = pmc_processhash;
4996145256Sjkoshy		     prh <= &pmc_processhash[pmc_processhashmask];
4997145256Sjkoshy		     prh++)
4998145256Sjkoshy			LIST_FOREACH(pp, prh, pp_next)
4999145256Sjkoshy			    PMCDBG(MOD,INI,3, "pid=%d", pp->pp_proc->p_pid);
5000145256Sjkoshy#endif
5001145256Sjkoshy
5002145256Sjkoshy		hashdestroy(pmc_processhash, M_PMC, pmc_processhashmask);
5003145256Sjkoshy		pmc_processhash = NULL;
5004145256Sjkoshy	}
5005145256Sjkoshy
5006145256Sjkoshy	if (pmc_ownerhash) {
5007145256Sjkoshy		PMCDBG(MOD,INI,3, "%s", "destroy owner hash");
5008145256Sjkoshy		hashdestroy(pmc_ownerhash, M_PMC, pmc_ownerhashmask);
5009145256Sjkoshy		pmc_ownerhash = NULL;
5010145256Sjkoshy	}
5011145256Sjkoshy
5012147191Sjkoshy	KASSERT(LIST_EMPTY(&pmc_ss_owners),
5013147191Sjkoshy	    ("[pmc,%d] Global SS owner list not empty", __LINE__));
5014147191Sjkoshy	KASSERT(pmc_ss_count == 0,
5015147191Sjkoshy	    ("[pmc,%d] Global SS count not empty", __LINE__));
5016147191Sjkoshy
5017184802Sjkoshy 	/* do processor and pmc-class dependent cleanup */
5018183266Sjkoshy	maxcpu = pmc_cpu_max();
5019153735Sjkoshy
5020145256Sjkoshy	PMCDBG(MOD,INI,3, "%s", "md cleanup");
5021145256Sjkoshy	if (md) {
5022145256Sjkoshy		pmc_save_cpu_binding(&pb);
5023183266Sjkoshy		for (cpu = 0; cpu < maxcpu; cpu++) {
5024145256Sjkoshy			PMCDBG(MOD,INI,1,"pmc-cleanup cpu=%d pcs=%p",
5025145256Sjkoshy			    cpu, pmc_pcpu[cpu]);
5026183266Sjkoshy			if (!pmc_cpu_is_active(cpu) || pmc_pcpu[cpu] == NULL)
5027145256Sjkoshy				continue;
5028145256Sjkoshy			pmc_select_cpu(cpu);
5029184802Sjkoshy			for (c = 0; c < md->pmd_nclass; c++)
5030184802Sjkoshy				md->pmd_classdep[c].pcd_pcpu_fini(md, cpu);
5031184802Sjkoshy			if (md->pmd_pcpu_fini)
5032185363Sjkoshy				md->pmd_pcpu_fini(md, cpu);
5033145256Sjkoshy		}
5034184994Sjkoshy
5035233628Sfabient		if (md->pmd_cputype == PMC_CPU_GENERIC)
5036233628Sfabient			pmc_generic_cpu_finalize(md);
5037233628Sfabient		else
5038233628Sfabient			pmc_md_finalize(md);
5039184994Sjkoshy
5040233628Sfabient		pmc_mdep_free(md);
5041145256Sjkoshy		md = NULL;
5042145256Sjkoshy		pmc_restore_cpu_binding(&pb);
5043145256Sjkoshy	}
5044145256Sjkoshy
5045184802Sjkoshy	/* Free per-cpu descriptors. */
5046184802Sjkoshy	for (cpu = 0; cpu < maxcpu; cpu++) {
5047184802Sjkoshy		if (!pmc_cpu_is_active(cpu))
5048184802Sjkoshy			continue;
5049233628Sfabient		KASSERT(pmc_pcpu[cpu]->pc_sb[PMC_HR] != NULL,
5050233628Sfabient		    ("[pmc,%d] Null hw cpu sample buffer cpu=%d", __LINE__,
5051184802Sjkoshy			cpu));
5052233628Sfabient		KASSERT(pmc_pcpu[cpu]->pc_sb[PMC_SR] != NULL,
5053233628Sfabient		    ("[pmc,%d] Null sw cpu sample buffer cpu=%d", __LINE__,
5054233628Sfabient			cpu));
5055233628Sfabient		free(pmc_pcpu[cpu]->pc_sb[PMC_HR]->ps_callchains, M_PMC);
5056233628Sfabient		free(pmc_pcpu[cpu]->pc_sb[PMC_HR], M_PMC);
5057233628Sfabient		free(pmc_pcpu[cpu]->pc_sb[PMC_SR]->ps_callchains, M_PMC);
5058233628Sfabient		free(pmc_pcpu[cpu]->pc_sb[PMC_SR], M_PMC);
5059184802Sjkoshy		free(pmc_pcpu[cpu], M_PMC);
5060184802Sjkoshy	}
5061184802Sjkoshy
5062184205Sdes	free(pmc_pcpu, M_PMC);
5063145256Sjkoshy	pmc_pcpu = NULL;
5064145256Sjkoshy
5065184205Sdes	free(pmc_pcpu_saved, M_PMC);
5066145256Sjkoshy	pmc_pcpu_saved = NULL;
5067145256Sjkoshy
5068145256Sjkoshy	if (pmc_pmcdisp) {
5069184205Sdes		free(pmc_pmcdisp, M_PMC);
5070145256Sjkoshy		pmc_pmcdisp = NULL;
5071145256Sjkoshy	}
5072145256Sjkoshy
5073184802Sjkoshy	if (pmc_rowindex_to_classdep) {
5074184802Sjkoshy		free(pmc_rowindex_to_classdep, M_PMC);
5075184802Sjkoshy		pmc_rowindex_to_classdep = NULL;
5076184802Sjkoshy	}
5077184802Sjkoshy
5078147191Sjkoshy	pmclog_shutdown();
5079147191Sjkoshy
5080145256Sjkoshy	sx_xunlock(&pmc_sx); 	/* we are done */
5081145256Sjkoshy}
5082145256Sjkoshy
5083145256Sjkoshy/*
5084145256Sjkoshy * The function called at load/unload.
5085145256Sjkoshy */
5086145256Sjkoshy
5087145256Sjkoshystatic int
5088145256Sjkoshyload (struct module *module __unused, int cmd, void *arg __unused)
5089145256Sjkoshy{
5090145256Sjkoshy	int error;
5091145256Sjkoshy
5092145256Sjkoshy	error = 0;
5093145256Sjkoshy
5094145256Sjkoshy	switch (cmd) {
5095145256Sjkoshy	case MOD_LOAD :
5096145256Sjkoshy		/* initialize the subsystem */
5097145256Sjkoshy		error = pmc_initialize();
5098145256Sjkoshy		if (error != 0)
5099145256Sjkoshy			break;
5100183266Sjkoshy		PMCDBG(MOD,INI,1, "syscall=%d maxcpu=%d",
5101183266Sjkoshy		    pmc_syscall_num, pmc_cpu_max());
5102145256Sjkoshy		break;
5103145256Sjkoshy
5104145256Sjkoshy
5105145256Sjkoshy	case MOD_UNLOAD :
5106145256Sjkoshy	case MOD_SHUTDOWN:
5107145256Sjkoshy		pmc_cleanup();
5108145256Sjkoshy		PMCDBG(MOD,INI,1, "%s", "unloaded");
5109145256Sjkoshy		break;
5110145256Sjkoshy
5111145256Sjkoshy	default :
5112145256Sjkoshy		error = EINVAL;	/* XXX should panic(9) */
5113145256Sjkoshy		break;
5114145256Sjkoshy	}
5115145256Sjkoshy
5116145256Sjkoshy	return error;
5117145256Sjkoshy}
5118145256Sjkoshy
5119145256Sjkoshy/* memory pool */
5120145256SjkoshyMALLOC_DEFINE(M_PMC, "pmc", "Memory space for the PMC module");
5121