xref: /freebsd/sys/dev/hwpmc/hwpmc_mod.c (revision 64038db825d64fb4827fc8ee264ea0fa1a046d82)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2003-2008 Joseph Koshy
5  * Copyright (c) 2007 The FreeBSD Foundation
6  * Copyright (c) 2018 Matthew Macy
7  * All rights reserved.
8  *
9  * Portions of this software were developed by A. Joseph Koshy under
10  * sponsorship from the FreeBSD Foundation and Google, Inc.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/domainset.h>
37 #include <sys/eventhandler.h>
38 #include <sys/jail.h>
39 #include <sys/kernel.h>
40 #include <sys/kthread.h>
41 #include <sys/limits.h>
42 #include <sys/lock.h>
43 #include <sys/malloc.h>
44 #include <sys/module.h>
45 #include <sys/mount.h>
46 #include <sys/mutex.h>
47 #include <sys/pmc.h>
48 #include <sys/pmckern.h>
49 #include <sys/pmclog.h>
50 #include <sys/priv.h>
51 #include <sys/proc.h>
52 #include <sys/queue.h>
53 #include <sys/resourcevar.h>
54 #include <sys/rwlock.h>
55 #include <sys/sched.h>
56 #include <sys/signalvar.h>
57 #include <sys/smp.h>
58 #include <sys/sx.h>
59 #include <sys/sysctl.h>
60 #include <sys/sysent.h>
61 #include <sys/syslog.h>
62 #include <sys/taskqueue.h>
63 #include <sys/vnode.h>
64 
65 #define	EXTERR_CATEGORY	EXTERR_CAT_HWPMC_MOD
66 #include <sys/exterrvar.h>
67 
68 #include <sys/linker.h>		/* needs to be after <sys/malloc.h> */
69 
70 #include <machine/atomic.h>
71 #include <machine/md_var.h>
72 
73 #include <vm/vm.h>
74 #include <vm/vm_extern.h>
75 #include <vm/pmap.h>
76 #include <vm/vm_map.h>
77 #include <vm/vm_object.h>
78 
79 #include "hwpmc_soft.h"
80 
81 #define PMC_EPOCH_ENTER()						\
82     struct epoch_tracker pmc_et;					\
83     epoch_enter_preempt(global_epoch_preempt, &pmc_et)
84 
85 #define PMC_EPOCH_EXIT()						\
86     epoch_exit_preempt(global_epoch_preempt, &pmc_et)
87 
88 /*
89  * Types
90  */
91 
92 enum pmc_flags {
93 	PMC_FLAG_NONE	  = 0x00, /* do nothing */
94 	PMC_FLAG_REMOVE   = 0x01, /* atomically remove entry from hash */
95 	PMC_FLAG_ALLOCATE = 0x02, /* add entry to hash if not found */
96 	PMC_FLAG_NOWAIT   = 0x04, /* do not wait for mallocs */
97 };
98 
99 /*
100  * The offset in sysent where the syscall is allocated.
101  */
102 static int pmc_syscall_num = NO_SYSCALL;
103 
104 struct pmc_cpu		**pmc_pcpu;	 /* per-cpu state */
105 pmc_value_t		*pmc_pcpu_saved; /* saved PMC values: CSW handling */
106 
107 #define	PMC_PCPU_SAVED(C, R)	pmc_pcpu_saved[(R) + md->pmd_npmc * (C)]
108 
109 struct mtx_pool		*pmc_mtxpool;
110 static int		*pmc_pmcdisp;	 /* PMC row dispositions */
111 
112 #define	PMC_ROW_DISP_IS_FREE(R)		(pmc_pmcdisp[(R)] == 0)
113 #define	PMC_ROW_DISP_IS_THREAD(R)	(pmc_pmcdisp[(R)] > 0)
114 #define	PMC_ROW_DISP_IS_STANDALONE(R)	(pmc_pmcdisp[(R)] < 0)
115 
116 #define	PMC_MARK_ROW_FREE(R) do {					  \
117 	pmc_pmcdisp[(R)] = 0;						  \
118 } while (0)
119 
120 #define	PMC_MARK_ROW_STANDALONE(R) do {					  \
121 	KASSERT(pmc_pmcdisp[(R)] <= 0, ("[pmc,%d] row disposition error", \
122 		    __LINE__));						  \
123 	atomic_add_int(&pmc_pmcdisp[(R)], -1);				  \
124 	KASSERT(pmc_pmcdisp[(R)] >= (-pmc_cpu_max_active()),		  \
125 		("[pmc,%d] row disposition error", __LINE__));		  \
126 } while (0)
127 
128 #define	PMC_UNMARK_ROW_STANDALONE(R) do { 				  \
129 	atomic_add_int(&pmc_pmcdisp[(R)], 1);				  \
130 	KASSERT(pmc_pmcdisp[(R)] <= 0, ("[pmc,%d] row disposition error", \
131 		    __LINE__));						  \
132 } while (0)
133 
134 #define	PMC_MARK_ROW_THREAD(R) do {					  \
135 	KASSERT(pmc_pmcdisp[(R)] >= 0, ("[pmc,%d] row disposition error", \
136 		    __LINE__));						  \
137 	atomic_add_int(&pmc_pmcdisp[(R)], 1);				  \
138 } while (0)
139 
140 #define	PMC_UNMARK_ROW_THREAD(R) do {					  \
141 	atomic_add_int(&pmc_pmcdisp[(R)], -1);				  \
142 	KASSERT(pmc_pmcdisp[(R)] >= 0, ("[pmc,%d] row disposition error", \
143 		    __LINE__));						  \
144 } while (0)
145 
146 /* various event handlers */
147 static eventhandler_tag	pmc_exit_tag, pmc_fork_tag, pmc_kld_load_tag,
148     pmc_kld_unload_tag;
149 
150 /* Module statistics */
151 struct pmc_driverstats pmc_stats;
152 
153 /* Machine/processor dependent operations */
154 static struct pmc_mdep  *md;
155 
156 /*
157  * Hash tables mapping owner processes and target threads to PMCs.
158  */
159 struct mtx pmc_processhash_mtx;		/* spin mutex */
160 static u_long pmc_processhashmask;
161 static LIST_HEAD(pmc_processhash, pmc_process) *pmc_processhash;
162 
163 /*
164  * Hash table of PMC owner descriptors.  This table is protected by
165  * the shared PMC "sx" lock.
166  */
167 static u_long pmc_ownerhashmask;
168 static LIST_HEAD(pmc_ownerhash, pmc_owner) *pmc_ownerhash;
169 
170 /*
171  * List of PMC owners with system-wide sampling PMCs.
172  */
173 static CK_LIST_HEAD(, pmc_owner) pmc_ss_owners;
174 
175 /*
176  * List of free thread entries. This is protected by the spin
177  * mutex.
178  */
179 static struct mtx pmc_threadfreelist_mtx;	/* spin mutex */
180 static LIST_HEAD(, pmc_thread) pmc_threadfreelist;
181 static int pmc_threadfreelist_entries = 0;
182 #define	THREADENTRY_SIZE	(sizeof(struct pmc_thread) +		\
183     (md->pmd_npmc * sizeof(struct pmc_threadpmcstate)))
184 
185 /*
186  * Task to free thread descriptors
187  */
188 static struct task free_task;
189 
190 /*
191  * A map of row indices to classdep structures.
192  */
193 static struct pmc_classdep **pmc_rowindex_to_classdep;
194 
195 /*
196  * Prototypes
197  */
198 
199 #ifdef HWPMC_DEBUG
200 static int	pmc_debugflags_sysctl_handler(SYSCTL_HANDLER_ARGS);
201 static int	pmc_debugflags_parse(char *newstr, char *fence);
202 #endif
203 
204 static void	pmc_multipart_add(struct pmc_sample *ps, int type,
205     int length);
206 static void	pmc_multipart_copydata(struct pmc_sample *ps,
207     struct pmc_multipart *mp);
208 
209 static int	load(struct module *module, int cmd, void *arg);
210 static int	pmc_add_sample(ring_type_t ring, struct pmc *pm,
211     struct trapframe *tf, struct pmc_multipart *mp);
212 static void	pmc_add_thread_descriptors_from_proc(struct proc *p,
213     struct pmc_process *pp);
214 static int	pmc_attach_process(struct proc *p, struct pmc *pm);
215 static struct pmc *pmc_allocate_pmc_descriptor(void);
216 static struct pmc_owner *pmc_allocate_owner_descriptor(struct proc *p);
217 static int	pmc_attach_one_process(struct proc *p, struct pmc *pm);
218 static bool	pmc_can_allocate_row(int ri, enum pmc_mode mode);
219 static bool	pmc_can_allocate_rowindex(struct proc *p, unsigned int ri,
220     int cpu);
221 static bool	pmc_can_attach(struct pmc *pm, struct proc *p);
222 static void	pmc_capture_user_callchain(int cpu, int soft,
223     struct trapframe *tf);
224 static void	pmc_cleanup(void);
225 static int	pmc_detach_process(struct proc *p, struct pmc *pm);
226 static int	pmc_detach_one_process(struct proc *p, struct pmc *pm,
227     int flags);
228 static void	pmc_destroy_owner_descriptor(struct pmc_owner *po);
229 static void	pmc_destroy_pmc_descriptor(struct pmc *pm);
230 static void	pmc_destroy_process_descriptor(struct pmc_process *pp);
231 static void	pmc_reclaim_pmc_from_cpu(struct pmc *pm,
232     struct pmc_process *pp, int cpu);
233 static struct pmc_owner *pmc_find_owner_descriptor(struct proc *p);
234 static int	pmc_find_pmc(pmc_id_t pmcid, struct pmc **pm);
235 static struct pmc *pmc_find_pmc_descriptor_in_process(struct pmc_owner *po,
236     pmc_id_t pmc);
237 static struct pmc_process *pmc_find_process_descriptor(struct proc *p,
238     uint32_t mode);
239 static struct pmc_thread *pmc_find_thread_descriptor(struct pmc_process *pp,
240     struct thread *td, uint32_t mode);
241 static void	pmc_force_context_switch(void);
242 static void	pmc_link_target_process(struct pmc *pm,
243     struct pmc_process *pp);
244 static void	pmc_log_all_process_mappings(struct pmc_owner *po);
245 static void	pmc_log_kernel_mappings(struct pmc *pm);
246 static void	pmc_log_process_mappings(struct pmc_owner *po, struct proc *p);
247 static void	pmc_maybe_remove_owner(struct pmc_owner *po);
248 static void	pmc_post_callchain_callback(void);
249 static void	pmc_process_allproc(struct pmc *pm);
250 static void	pmc_process_csw_in(struct thread *td);
251 static void	pmc_process_csw_out(struct thread *td);
252 static void	pmc_process_exec(struct thread *td,
253     struct pmckern_procexec *pk);
254 static void	pmc_process_exit(void *arg, struct proc *p);
255 static void	pmc_process_fork(void *arg, struct proc *p1,
256     struct proc *p2, int n);
257 static void	pmc_process_proccreate(struct proc *p);
258 static void	pmc_process_samples(int cpu, ring_type_t soft);
259 static void	pmc_process_threadcreate(struct thread *td);
260 static void	pmc_process_threadexit(struct thread *td);
261 static void	pmc_process_thread_add(struct thread *td);
262 static void	pmc_process_thread_delete(struct thread *td);
263 static void	pmc_process_thread_userret(struct thread *td);
264 static void	pmc_release_pmc_descriptor(struct pmc *pmc);
265 static void	pmc_remove_owner(struct pmc_owner *po);
266 static void	pmc_remove_process_descriptor(struct pmc_process *pp);
267 static int	pmc_start(struct pmc *pm);
268 static int	pmc_stop(struct pmc *pm);
269 static int	pmc_syscall_handler(struct thread *td, void *syscall_args);
270 static struct pmc_thread *pmc_thread_descriptor_pool_alloc(void);
271 static void	pmc_thread_descriptor_pool_drain(void);
272 static void	pmc_thread_descriptor_pool_free(struct pmc_thread *pt);
273 static void	pmc_unlink_target_process(struct pmc *pmc,
274     struct pmc_process *pp);
275 
276 static int	generic_switch_in(struct pmc_cpu *pc, struct pmc_process *pp);
277 static int	generic_switch_out(struct pmc_cpu *pc, struct pmc_process *pp);
278 static struct pmc_mdep *pmc_generic_cpu_initialize(void);
279 static void	pmc_generic_cpu_finalize(struct pmc_mdep *md);
280 
281 /*
282  * Kernel tunables and sysctl(8) interface.
283  */
284 
285 SYSCTL_DECL(_kern_hwpmc);
286 SYSCTL_NODE(_kern_hwpmc, OID_AUTO, stats, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
287     "HWPMC stats");
288 
289 /* Stats. */
290 SYSCTL_COUNTER_U64(_kern_hwpmc_stats, OID_AUTO, intr_ignored, CTLFLAG_RW,
291     &pmc_stats.pm_intr_ignored,
292     "# of interrupts ignored");
293 SYSCTL_COUNTER_U64(_kern_hwpmc_stats, OID_AUTO, intr_processed, CTLFLAG_RW,
294     &pmc_stats.pm_intr_processed,
295     "# of interrupts processed");
296 SYSCTL_COUNTER_U64(_kern_hwpmc_stats, OID_AUTO, intr_bufferfull, CTLFLAG_RW,
297     &pmc_stats.pm_intr_bufferfull,
298     "# of interrupts where buffer was full");
299 SYSCTL_COUNTER_U64(_kern_hwpmc_stats, OID_AUTO, syscalls, CTLFLAG_RW,
300     &pmc_stats.pm_syscalls,
301     "# of syscalls");
302 SYSCTL_COUNTER_U64(_kern_hwpmc_stats, OID_AUTO, syscall_errors, CTLFLAG_RW,
303     &pmc_stats.pm_syscall_errors,
304     "# of syscall_errors");
305 SYSCTL_COUNTER_U64(_kern_hwpmc_stats, OID_AUTO, buffer_requests, CTLFLAG_RW,
306     &pmc_stats.pm_buffer_requests,
307     "# of buffer requests");
308 SYSCTL_COUNTER_U64(_kern_hwpmc_stats, OID_AUTO, buffer_requests_failed,
309     CTLFLAG_RW, &pmc_stats.pm_buffer_requests_failed,
310     "# of buffer requests which failed");
311 SYSCTL_COUNTER_U64(_kern_hwpmc_stats, OID_AUTO, log_sweeps, CTLFLAG_RW,
312     &pmc_stats.pm_log_sweeps,
313     "# of times samples were processed");
314 SYSCTL_COUNTER_U64(_kern_hwpmc_stats, OID_AUTO, merges, CTLFLAG_RW,
315     &pmc_stats.pm_merges,
316     "# of times kernel stack was found for user trace");
317 SYSCTL_COUNTER_U64(_kern_hwpmc_stats, OID_AUTO, overwrites, CTLFLAG_RW,
318     &pmc_stats.pm_overwrites,
319     "# of times a sample was overwritten before being logged");
320 
321 static int pmc_callchaindepth = PMC_CALLCHAIN_DEPTH;
322 SYSCTL_INT(_kern_hwpmc, OID_AUTO, callchaindepth, CTLFLAG_RDTUN,
323     &pmc_callchaindepth, 0,
324     "depth of call chain records");
325 
326 char pmc_cpuid[PMC_CPUID_LEN];
327 SYSCTL_STRING(_kern_hwpmc, OID_AUTO, cpuid, CTLFLAG_RD,
328     pmc_cpuid, 0,
329     "cpu version string");
330 
331 #ifdef HWPMC_DEBUG
332 struct pmc_debugflags pmc_debugflags = PMC_DEBUG_DEFAULT_FLAGS;
333 char	pmc_debugstr[PMC_DEBUG_STRSIZE];
334 TUNABLE_STR(PMC_SYSCTL_NAME_PREFIX "debugflags", pmc_debugstr,
335     sizeof(pmc_debugstr));
336 SYSCTL_PROC(_kern_hwpmc, OID_AUTO, debugflags,
337     CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_NOFETCH | CTLFLAG_MPSAFE,
338     0, 0, pmc_debugflags_sysctl_handler, "A",
339     "debug flags");
340 #endif
341 
342 /*
343  * kern.hwpmc.hashsize -- determines the number of rows in the
344  * of the hash table used to look up threads
345  */
346 static int pmc_hashsize = PMC_HASH_SIZE;
347 SYSCTL_INT(_kern_hwpmc, OID_AUTO, hashsize, CTLFLAG_RDTUN,
348     &pmc_hashsize, 0,
349     "rows in hash tables");
350 
351 /*
352  * kern.hwpmc.nsamples --- number of PC samples/callchain stacks per CPU
353  */
354 static int pmc_nsamples = PMC_NSAMPLES;
355 SYSCTL_INT(_kern_hwpmc, OID_AUTO, nsamples, CTLFLAG_RDTUN,
356     &pmc_nsamples, 0,
357     "number of PC samples per CPU");
358 
359 static uint64_t pmc_sample_mask = PMC_NSAMPLES - 1;
360 
361 /*
362  * kern.hwpmc.mtxpoolsize -- number of mutexes in the mutex pool.
363  */
364 static int pmc_mtxpool_size = PMC_MTXPOOL_SIZE;
365 SYSCTL_INT(_kern_hwpmc, OID_AUTO, mtxpoolsize, CTLFLAG_RDTUN,
366     &pmc_mtxpool_size, 0,
367     "size of spin mutex pool");
368 
369 /*
370  * kern.hwpmc.threadfreelist_entries -- number of free entries
371  */
372 SYSCTL_INT(_kern_hwpmc, OID_AUTO, threadfreelist_entries, CTLFLAG_RD,
373     &pmc_threadfreelist_entries, 0,
374     "number of available thread entries");
375 
376 /*
377  * kern.hwpmc.threadfreelist_max -- maximum number of free entries
378  */
379 static int pmc_threadfreelist_max = PMC_THREADLIST_MAX;
380 SYSCTL_INT(_kern_hwpmc, OID_AUTO, threadfreelist_max, CTLFLAG_RW,
381     &pmc_threadfreelist_max, 0,
382     "maximum number of available thread entries before freeing some");
383 
384 /*
385  * kern.hwpmc.mincount -- minimum sample count
386  */
387 static u_int pmc_mincount = 1000;
388 SYSCTL_INT(_kern_hwpmc, OID_AUTO, mincount, CTLFLAG_RWTUN,
389     &pmc_mincount, 0,
390     "minimum count for sampling counters");
391 
392 /*
393  * security.bsd.unprivileged_syspmcs -- allow non-root processes to
394  * allocate system-wide PMCs.
395  *
396  * Allowing unprivileged processes to allocate system PMCs is convenient
397  * if system-wide measurements need to be taken concurrently with other
398  * per-process measurements.  This feature is turned off by default.
399  */
400 static int pmc_unprivileged_syspmcs = 0;
401 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_syspmcs, CTLFLAG_RWTUN,
402     &pmc_unprivileged_syspmcs, 0,
403     "allow unprivileged process to allocate system PMCs");
404 
405 /*
406  * Hash function.  Discard the lower 2 bits of the pointer since
407  * these are always zero for our uses.  The hash multiplier is
408  * round((2^LONG_BIT) * ((sqrt(5)-1)/2)).
409  */
410 #if	LONG_BIT == 64
411 #define	_PMC_HM		11400714819323198486u
412 #elif	LONG_BIT == 32
413 #define	_PMC_HM		2654435769u
414 #else
415 #error 	Must know the size of 'long' to compile
416 #endif
417 
418 #define	PMC_HASH_PTR(P,M)	((((unsigned long) (P) >> 2) * _PMC_HM) & (M))
419 
420 /*
421  * Syscall structures
422  */
423 
424 /* The `sysent' for the new syscall */
425 static struct sysent pmc_sysent = {
426 	.sy_narg =	2,
427 	.sy_call =	pmc_syscall_handler,
428 };
429 
430 static struct syscall_module_data pmc_syscall_mod = {
431 	.chainevh =	load,
432 	.chainarg =	NULL,
433 	.offset =	&pmc_syscall_num,
434 	.new_sysent =	&pmc_sysent,
435 	.old_sysent =	{ .sy_narg = 0, .sy_call = NULL },
436 	.flags =	SY_THR_STATIC_KLD,
437 };
438 
439 static moduledata_t pmc_mod = {
440 	.name =		PMC_MODULE_NAME,
441 	.evhand =	syscall_module_handler,
442 	.priv =		&pmc_syscall_mod,
443 };
444 
445 #ifdef EARLY_AP_STARTUP
446 DECLARE_MODULE(pmc, pmc_mod, SI_SUB_SYSCALLS, SI_ORDER_ANY);
447 #else
448 DECLARE_MODULE(pmc, pmc_mod, SI_SUB_SMP, SI_ORDER_ANY);
449 #endif
450 MODULE_VERSION(pmc, PMC_VERSION);
451 
452 #ifdef HWPMC_DEBUG
453 enum pmc_dbgparse_state {
454 	PMCDS_WS,		/* in whitespace */
455 	PMCDS_MAJOR,		/* seen a major keyword */
456 	PMCDS_MINOR
457 };
458 
459 static int
460 pmc_debugflags_parse(char *newstr, char *fence)
461 {
462 	struct pmc_debugflags *tmpflags;
463 	size_t kwlen;
464 	char c, *p, *q;
465 	int error, *newbits, tmp;
466 	int found;
467 
468 	tmpflags = malloc(sizeof(*tmpflags), M_PMC, M_WAITOK | M_ZERO);
469 
470 	error = 0;
471 	for (p = newstr; p < fence && (c = *p); p++) {
472 		/* skip white space */
473 		if (c == ' ' || c == '\t')
474 			continue;
475 
476 		/* look for a keyword followed by "=" */
477 		for (q = p; p < fence && (c = *p) && c != '='; p++)
478 			;
479 		if (c != '=') {
480 			error = EINVAL;
481 			goto done;
482 		}
483 
484 		kwlen = p - q;
485 		newbits = NULL;
486 
487 		/* lookup flag group name */
488 #define	DBG_SET_FLAG_MAJ(S,F)						\
489 		if (kwlen == sizeof(S)-1 && strncmp(q, S, kwlen) == 0)	\
490 			newbits = &tmpflags->pdb_ ## F;
491 
492 		DBG_SET_FLAG_MAJ("cpu",		CPU);
493 		DBG_SET_FLAG_MAJ("csw",		CSW);
494 		DBG_SET_FLAG_MAJ("logging",	LOG);
495 		DBG_SET_FLAG_MAJ("module",	MOD);
496 		DBG_SET_FLAG_MAJ("md", 		MDP);
497 		DBG_SET_FLAG_MAJ("owner",	OWN);
498 		DBG_SET_FLAG_MAJ("pmc",		PMC);
499 		DBG_SET_FLAG_MAJ("process",	PRC);
500 		DBG_SET_FLAG_MAJ("sampling", 	SAM);
501 #undef DBG_SET_FLAG_MAJ
502 
503 		if (newbits == NULL) {
504 			error = EINVAL;
505 			goto done;
506 		}
507 
508 		p++;		/* skip the '=' */
509 
510 		/* Now parse the individual flags */
511 		tmp = 0;
512 	newflag:
513 		for (q = p; p < fence && (c = *p); p++)
514 			if (c == ' ' || c == '\t' || c == ',')
515 				break;
516 
517 		/* p == fence or c == ws or c == "," or c == 0 */
518 
519 		if ((kwlen = p - q) == 0) {
520 			*newbits = tmp;
521 			continue;
522 		}
523 
524 		found = 0;
525 #define	DBG_SET_FLAG_MIN(S,F)						\
526 		if (kwlen == sizeof(S)-1 && strncmp(q, S, kwlen) == 0)	\
527 			tmp |= found = (1 << PMC_DEBUG_MIN_ ## F)
528 
529 		/* a '*' denotes all possible flags in the group */
530 		if (kwlen == 1 && *q == '*')
531 			tmp = found = ~0;
532 		/* look for individual flag names */
533 		DBG_SET_FLAG_MIN("allocaterow", ALR);
534 		DBG_SET_FLAG_MIN("allocate",	ALL);
535 		DBG_SET_FLAG_MIN("attach",	ATT);
536 		DBG_SET_FLAG_MIN("bind",	BND);
537 		DBG_SET_FLAG_MIN("config",	CFG);
538 		DBG_SET_FLAG_MIN("exec",	EXC);
539 		DBG_SET_FLAG_MIN("exit",	EXT);
540 		DBG_SET_FLAG_MIN("find",	FND);
541 		DBG_SET_FLAG_MIN("flush",	FLS);
542 		DBG_SET_FLAG_MIN("fork",	FRK);
543 		DBG_SET_FLAG_MIN("getbuf",	GTB);
544 		DBG_SET_FLAG_MIN("hook",	PMH);
545 		DBG_SET_FLAG_MIN("init",	INI);
546 		DBG_SET_FLAG_MIN("intr",	INT);
547 		DBG_SET_FLAG_MIN("linktarget",	TLK);
548 		DBG_SET_FLAG_MIN("mayberemove", OMR);
549 		DBG_SET_FLAG_MIN("ops",		OPS);
550 		DBG_SET_FLAG_MIN("read",	REA);
551 		DBG_SET_FLAG_MIN("register",	REG);
552 		DBG_SET_FLAG_MIN("release",	REL);
553 		DBG_SET_FLAG_MIN("remove",	ORM);
554 		DBG_SET_FLAG_MIN("sample",	SAM);
555 		DBG_SET_FLAG_MIN("scheduleio",	SIO);
556 		DBG_SET_FLAG_MIN("select",	SEL);
557 		DBG_SET_FLAG_MIN("signal",	SIG);
558 		DBG_SET_FLAG_MIN("swi",		SWI);
559 		DBG_SET_FLAG_MIN("swo",		SWO);
560 		DBG_SET_FLAG_MIN("start",	STA);
561 		DBG_SET_FLAG_MIN("stop",	STO);
562 		DBG_SET_FLAG_MIN("syscall",	PMS);
563 		DBG_SET_FLAG_MIN("unlinktarget", TUL);
564 		DBG_SET_FLAG_MIN("write",	WRI);
565 #undef DBG_SET_FLAG_MIN
566 		if (found == 0) {
567 			/* unrecognized flag name */
568 			error = EINVAL;
569 			goto done;
570 		}
571 
572 		if (c == 0 || c == ' ' || c == '\t') {	/* end of flag group */
573 			*newbits = tmp;
574 			continue;
575 		}
576 
577 		p++;
578 		goto newflag;
579 	}
580 
581 	/* save the new flag set */
582 	bcopy(tmpflags, &pmc_debugflags, sizeof(pmc_debugflags));
583 done:
584 	free(tmpflags, M_PMC);
585 	return (error);
586 }
587 
588 static int
589 pmc_debugflags_sysctl_handler(SYSCTL_HANDLER_ARGS)
590 {
591 	char *fence, *newstr;
592 	int error;
593 	u_int n;
594 
595 	n = sizeof(pmc_debugstr);
596 	newstr = malloc(n, M_PMC, M_WAITOK | M_ZERO);
597 	strlcpy(newstr, pmc_debugstr, n);
598 
599 	error = sysctl_handle_string(oidp, newstr, n, req);
600 
601 	/* if there is a new string, parse and copy it */
602 	if (error == 0 && req->newptr != NULL) {
603 		fence = newstr + (n < req->newlen ? n : req->newlen + 1);
604 		error = pmc_debugflags_parse(newstr, fence);
605 		if (error == 0)
606 			strlcpy(pmc_debugstr, newstr, sizeof(pmc_debugstr));
607 	}
608 	free(newstr, M_PMC);
609 
610 	return (error);
611 }
612 #endif
613 
614 /*
615  * Map a row index to a classdep structure and return the adjusted row
616  * index for the PMC class index.
617  */
618 static struct pmc_classdep *
619 pmc_ri_to_classdep(struct pmc_mdep *md __unused, int ri, int *adjri)
620 {
621 	struct pmc_classdep *pcd;
622 
623 	KASSERT(ri >= 0 && ri < md->pmd_npmc,
624 	    ("[pmc,%d] illegal row-index %d", __LINE__, ri));
625 
626 	pcd = pmc_rowindex_to_classdep[ri];
627 	KASSERT(pcd != NULL,
628 	    ("[pmc,%d] ri %d null pcd", __LINE__, ri));
629 
630 	*adjri = ri - pcd->pcd_ri;
631 	KASSERT(*adjri >= 0 && *adjri < pcd->pcd_num,
632 	    ("[pmc,%d] adjusted row-index %d", __LINE__, *adjri));
633 
634 	return (pcd);
635 }
636 
637 /*
638  * Concurrency Control
639  *
640  * The driver manages the following data structures:
641  *
642  *   - target process descriptors, one per target process
643  *   - owner process descriptors (and attached lists), one per owner process
644  *   - lookup hash tables for owner and target processes
645  *   - PMC descriptors (and attached lists)
646  *   - per-cpu hardware state
647  *   - the 'hook' variable through which the kernel calls into
648  *     this module
649  *   - the machine hardware state (managed by the MD layer)
650  *
651  * These data structures are accessed from:
652  *
653  * - thread context-switch code
654  * - interrupt handlers (possibly on multiple cpus)
655  * - kernel threads on multiple cpus running on behalf of user
656  *   processes doing system calls
657  * - this driver's private kernel threads
658  *
659  * = Locks and Locking strategy =
660  *
661  * The driver uses four locking strategies for its operation:
662  *
663  * - The global SX lock "pmc_sx" is used to protect internal
664  *   data structures.
665  *
666  *   Calls into the module by syscall() start with this lock being
667  *   held in exclusive mode.  Depending on the requested operation,
668  *   the lock may be downgraded to 'shared' mode to allow more
669  *   concurrent readers into the module.  Calls into the module from
670  *   other parts of the kernel acquire the lock in shared mode.
671  *
672  *   This SX lock is held in exclusive mode for any operations that
673  *   modify the linkages between the driver's internal data structures.
674  *
675  *   The 'pmc_hook' function pointer is also protected by this lock.
676  *   It is only examined with the sx lock held in exclusive mode.  The
677  *   kernel module is allowed to be unloaded only with the sx lock held
678  *   in exclusive mode.  In normal syscall handling, after acquiring the
679  *   pmc_sx lock we first check that 'pmc_hook' is non-null before
680  *   proceeding.  This prevents races between the thread unloading the module
681  *   and other threads seeking to use the module.
682  *
683  * - Lookups of target process structures and owner process structures
684  *   cannot use the global "pmc_sx" SX lock because these lookups need
685  *   to happen during context switches and in other critical sections
686  *   where sleeping is not allowed.  We protect these lookup tables
687  *   with their own private spin-mutexes, "pmc_processhash_mtx" and
688  *   "pmc_ownerhash_mtx".
689  *
690  * - Interrupt handlers work in a lock free manner.  At interrupt
691  *   time, handlers look at the PMC pointer (phw->phw_pmc) configured
692  *   when the PMC was started.  If this pointer is NULL, the interrupt
693  *   is ignored after updating driver statistics.  We ensure that this
694  *   pointer is set (using an atomic operation if necessary) before the
695  *   PMC hardware is started.  Conversely, this pointer is unset atomically
696  *   only after the PMC hardware is stopped.
697  *
698  *   We ensure that everything needed for the operation of an
699  *   interrupt handler is available without it needing to acquire any
700  *   locks.  We also ensure that a PMC's software state is destroyed only
701  *   after the PMC is taken off hardware (on all CPUs).
702  *
703  * - Context-switch handling with process-private PMCs needs more
704  *   care.
705  *
706  *   A given process may be the target of multiple PMCs.  For example,
707  *   PMCATTACH and PMCDETACH may be requested by a process on one CPU
708  *   while the target process is running on another.  A PMC could also
709  *   be getting released because its owner is exiting.  We tackle
710  *   these situations in the following manner:
711  *
712  *   - each target process structure 'pmc_process' has an array
713  *     of 'struct pmc *' pointers, one for each hardware PMC.
714  *
715  *   - At context switch IN time, each "target" PMC in RUNNING state
716  *     gets started on hardware and a pointer to each PMC is copied into
717  *     the per-cpu phw array.  The 'runcount' for the PMC is
718  *     incremented.
719  *
720  *   - At context switch OUT time, all process-virtual PMCs are stopped
721  *     on hardware.  The saved value is added to the PMCs value field
722  *     only if the PMC is in a non-deleted state (the PMCs state could
723  *     have changed during the current time slice).
724  *
725  *     Note that since in-between a switch IN on a processor and a switch
726  *     OUT, the PMC could have been released on another CPU.  Therefore
727  *     context switch OUT always looks at the hardware state to turn
728  *     OFF PMCs and will update a PMC's saved value only if reachable
729  *     from the target process record.
730  *
731  *   - OP PMCRELEASE could be called on a PMC at any time (the PMC could
732  *     be attached to many processes at the time of the call and could
733  *     be active on multiple CPUs).
734  *
735  *     We prevent further scheduling of the PMC by marking it as in
736  *     state 'DELETED'.  If the runcount of the PMC is non-zero then
737  *     this PMC is currently running on a CPU somewhere.  The thread
738  *     doing the PMCRELEASE operation waits by repeatedly doing a
739  *     pause() till the runcount comes to zero.
740  *
741  * The contents of a PMC descriptor (struct pmc) are protected using
742  * a spin-mutex.  In order to save space, we use a mutex pool.
743  *
744  * In terms of lock types used by witness(4), we use:
745  * - Type "pmc-sx", used by the global SX lock.
746  * - Type "pmc-sleep", for sleep mutexes used by logger threads.
747  * - Type "pmc-per-proc", for protecting PMC owner descriptors.
748  * - Type "pmc-leaf", used for all other spin mutexes.
749  */
750 
751 /*
752  * Save the CPU binding of the current kthread.
753  */
754 void
755 pmc_save_cpu_binding(struct pmc_binding *pb)
756 {
757 	PMCDBG0(CPU,BND,2, "save-cpu");
758 	thread_lock(curthread);
759 	pb->pb_bound = sched_is_bound(curthread);
760 	pb->pb_cpu   = curthread->td_oncpu;
761 	pb->pb_priority = curthread->td_priority;
762 	thread_unlock(curthread);
763 	PMCDBG1(CPU,BND,2, "save-cpu cpu=%d", pb->pb_cpu);
764 }
765 
766 /*
767  * Restore the CPU binding of the current thread.
768  */
769 void
770 pmc_restore_cpu_binding(struct pmc_binding *pb)
771 {
772 	PMCDBG2(CPU,BND,2, "restore-cpu curcpu=%d restore=%d",
773 	    curthread->td_oncpu, pb->pb_cpu);
774 	thread_lock(curthread);
775 	sched_bind(curthread, pb->pb_cpu);
776 	if (!pb->pb_bound)
777 		sched_unbind(curthread);
778 	sched_prio(curthread, pb->pb_priority);
779 	thread_unlock(curthread);
780 	PMCDBG0(CPU,BND,2, "restore-cpu done");
781 }
782 
783 /*
784  * Move execution over to the specified CPU and bind it there.
785  */
786 void
787 pmc_select_cpu(int cpu)
788 {
789 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
790 	    ("[pmc,%d] bad cpu number %d", __LINE__, cpu));
791 
792 	/* Never move to an inactive CPU. */
793 	KASSERT(pmc_cpu_is_active(cpu), ("[pmc,%d] selecting inactive "
794 	    "CPU %d", __LINE__, cpu));
795 
796 	PMCDBG1(CPU,SEL,2, "select-cpu cpu=%d", cpu);
797 	thread_lock(curthread);
798 	sched_prio(curthread, PRI_MIN);
799 	sched_bind(curthread, cpu);
800 	thread_unlock(curthread);
801 
802 	KASSERT(curthread->td_oncpu == cpu,
803 	    ("[pmc,%d] CPU not bound [cpu=%d, curr=%d]", __LINE__,
804 		cpu, curthread->td_oncpu));
805 
806 	PMCDBG1(CPU,SEL,2, "select-cpu cpu=%d ok", cpu);
807 }
808 
809 /*
810  * Force a context switch.
811  *
812  * We do this by pause'ing for 1 tick -- invoking mi_switch() is not
813  * guaranteed to force a context switch.
814  */
815 static void
816 pmc_force_context_switch(void)
817 {
818 
819 	pause("pmcctx", 1);
820 }
821 
822 uint64_t
823 pmc_rdtsc(void)
824 {
825 #if defined(__i386__)
826 	/* Unfortunately get_cyclecount on i386 uses cpu_ticks. */
827 	return (rdtsc());
828 #else
829 	return (get_cyclecount());
830 #endif
831 }
832 
833 /*
834  * Get the file name for an executable.  This is a simple wrapper
835  * around vn_fullpath(9).
836  */
837 static void
838 pmc_getfilename(struct vnode *v, char **fullpath, char **freepath)
839 {
840 
841 	*fullpath = "unknown";
842 	*freepath = NULL;
843 	vn_fullpath(v, fullpath, freepath);
844 }
845 
846 /*
847  * Remove a process owning PMCs.
848  */
849 void
850 pmc_remove_owner(struct pmc_owner *po)
851 {
852 	struct pmc *pm, *tmp;
853 
854 	sx_assert(&pmc_sx, SX_XLOCKED);
855 
856 	PMCDBG1(OWN,ORM,1, "remove-owner po=%p", po);
857 
858 	/* Remove descriptor from the owner hash table */
859 	LIST_REMOVE(po, po_next);
860 
861 	/* release all owned PMC descriptors */
862 	LIST_FOREACH_SAFE(pm, &po->po_pmcs, pm_next, tmp) {
863 		PMCDBG1(OWN,ORM,2, "pmc=%p", pm);
864 		KASSERT(pm->pm_owner == po,
865 		    ("[pmc,%d] owner %p != po %p", __LINE__, pm->pm_owner, po));
866 
867 		pmc_release_pmc_descriptor(pm);	/* will unlink from the list */
868 		pmc_destroy_pmc_descriptor(pm);
869 	}
870 
871 	KASSERT(po->po_sscount == 0,
872 	    ("[pmc,%d] SS count not zero", __LINE__));
873 	KASSERT(LIST_EMPTY(&po->po_pmcs),
874 	    ("[pmc,%d] PMC list not empty", __LINE__));
875 
876 	/* de-configure the log file if present */
877 	if (po->po_flags & PMC_PO_OWNS_LOGFILE)
878 		pmclog_deconfigure_log(po);
879 }
880 
881 /*
882  * Remove an owner process record if all conditions are met.
883  */
884 static void
885 pmc_maybe_remove_owner(struct pmc_owner *po)
886 {
887 
888 	PMCDBG1(OWN,OMR,1, "maybe-remove-owner po=%p", po);
889 
890 	/*
891 	 * Remove owner record if
892 	 * - this process does not own any PMCs
893 	 * - this process has not allocated a system-wide sampling buffer
894 	 */
895 	if (LIST_EMPTY(&po->po_pmcs) &&
896 	    ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0)) {
897 		pmc_remove_owner(po);
898 		pmc_destroy_owner_descriptor(po);
899 	}
900 }
901 
902 /*
903  * Add an association between a target process and a PMC.
904  */
905 static void
906 pmc_link_target_process(struct pmc *pm, struct pmc_process *pp)
907 {
908 	struct pmc_target *pt;
909 	struct pmc_thread *pt_td __diagused;
910 	int ri;
911 
912 	sx_assert(&pmc_sx, SX_XLOCKED);
913 	KASSERT(pm != NULL && pp != NULL,
914 	    ("[pmc,%d] Null pm %p or pp %p", __LINE__, pm, pp));
915 	KASSERT(PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm)),
916 	    ("[pmc,%d] Attaching a non-process-virtual pmc=%p to pid=%d",
917 		__LINE__, pm, pp->pp_proc->p_pid));
918 	KASSERT(pp->pp_refcnt >= 0 && pp->pp_refcnt <= ((int) md->pmd_npmc - 1),
919 	    ("[pmc,%d] Illegal reference count %d for process record %p",
920 		__LINE__, pp->pp_refcnt, (void *) pp));
921 
922 	ri = PMC_TO_ROWINDEX(pm);
923 
924 	PMCDBG3(PRC,TLK,1, "link-target pmc=%p ri=%d pmc-process=%p",
925 	    pm, ri, pp);
926 
927 #ifdef HWPMC_DEBUG
928 	LIST_FOREACH(pt, &pm->pm_targets, pt_next) {
929 		if (pt->pt_process == pp)
930 			KASSERT(0, ("[pmc,%d] pp %p already in pmc %p targets",
931 			    __LINE__, pp, pm));
932 	}
933 #endif
934 	pt = malloc(sizeof(struct pmc_target), M_PMC, M_WAITOK | M_ZERO);
935 	pt->pt_process = pp;
936 
937 	LIST_INSERT_HEAD(&pm->pm_targets, pt, pt_next);
938 
939 	atomic_store_rel_ptr((uintptr_t *)&pp->pp_pmcs[ri].pp_pmc,
940 	    (uintptr_t)pm);
941 
942 	if (pm->pm_owner->po_owner == pp->pp_proc)
943 		pm->pm_flags |= PMC_F_ATTACHED_TO_OWNER;
944 
945 	/*
946 	 * Initialize the per-process values at this row index.
947 	 */
948 	pp->pp_pmcs[ri].pp_pmcval = PMC_TO_MODE(pm) == PMC_MODE_TS ?
949 	    pm->pm_sc.pm_reloadcount : 0;
950 	pp->pp_refcnt++;
951 
952 #ifdef INVARIANTS
953 	/* Confirm that the per-thread values at this row index are cleared. */
954 	if (PMC_TO_MODE(pm) == PMC_MODE_TS) {
955 		mtx_lock_spin(pp->pp_tdslock);
956 		LIST_FOREACH(pt_td, &pp->pp_tds, pt_next) {
957 			KASSERT(pt_td->pt_pmcs[ri].pt_pmcval == (pmc_value_t) 0,
958 			    ("[pmc,%d] pt_pmcval not cleared for pid=%d at "
959 			    "ri=%d", __LINE__, pp->pp_proc->p_pid, ri));
960 		}
961 		mtx_unlock_spin(pp->pp_tdslock);
962 	}
963 #endif
964 }
965 
966 /*
967  * Removes the association between a target process and a PMC.
968  */
969 static void
970 pmc_unlink_target_process(struct pmc *pm, struct pmc_process *pp)
971 {
972 	int ri;
973 	struct proc *p;
974 	struct pmc_target *ptgt;
975 	struct pmc_thread *pt;
976 
977 	sx_assert(&pmc_sx, SX_XLOCKED);
978 
979 	KASSERT(pm != NULL && pp != NULL,
980 	    ("[pmc,%d] Null pm %p or pp %p", __LINE__, pm, pp));
981 
982 	KASSERT(pp->pp_refcnt >= 1 && pp->pp_refcnt <= (int) md->pmd_npmc,
983 	    ("[pmc,%d] Illegal ref count %d on process record %p",
984 		__LINE__, pp->pp_refcnt, (void *) pp));
985 
986 	ri = PMC_TO_ROWINDEX(pm);
987 
988 	PMCDBG3(PRC,TUL,1, "unlink-target pmc=%p ri=%d pmc-process=%p",
989 	    pm, ri, pp);
990 
991 	KASSERT(pp->pp_pmcs[ri].pp_pmc == pm,
992 	    ("[pmc,%d] PMC ri %d mismatch pmc %p pp->[ri] %p", __LINE__,
993 		ri, pm, pp->pp_pmcs[ri].pp_pmc));
994 
995 	pp->pp_pmcs[ri].pp_pmc = NULL;
996 	pp->pp_pmcs[ri].pp_pmcval = (pmc_value_t)0;
997 
998 	/* Clear the per-thread values at this row index. */
999 	if (PMC_TO_MODE(pm) == PMC_MODE_TS) {
1000 		mtx_lock_spin(pp->pp_tdslock);
1001 		LIST_FOREACH(pt, &pp->pp_tds, pt_next)
1002 			pt->pt_pmcs[ri].pt_pmcval = (pmc_value_t)0;
1003 		mtx_unlock_spin(pp->pp_tdslock);
1004 	}
1005 
1006 	/* Remove owner-specific flags */
1007 	if (pm->pm_owner->po_owner == pp->pp_proc) {
1008 		pp->pp_flags &= ~PMC_PP_ENABLE_MSR_ACCESS;
1009 		pm->pm_flags &= ~PMC_F_ATTACHED_TO_OWNER;
1010 	}
1011 
1012 	pp->pp_refcnt--;
1013 
1014 	/* Remove the target process from the PMC structure */
1015 	LIST_FOREACH(ptgt, &pm->pm_targets, pt_next)
1016 		if (ptgt->pt_process == pp)
1017 			break;
1018 
1019 	KASSERT(ptgt != NULL, ("[pmc,%d] process %p (pp: %p) not found "
1020 		    "in pmc %p", __LINE__, pp->pp_proc, pp, pm));
1021 
1022 	LIST_REMOVE(ptgt, pt_next);
1023 	free(ptgt, M_PMC);
1024 
1025 	/* if the PMC now lacks targets, send the owner a SIGIO */
1026 	if (LIST_EMPTY(&pm->pm_targets)) {
1027 		p = pm->pm_owner->po_owner;
1028 		PROC_LOCK(p);
1029 		kern_psignal(p, SIGIO);
1030 		PROC_UNLOCK(p);
1031 
1032 		PMCDBG2(PRC,SIG,2, "signalling proc=%p signal=%d", p, SIGIO);
1033 	}
1034 }
1035 
1036 /*
1037  * Check if PMC 'pm' may be attached to target process 't'.
1038  */
1039 
1040 static bool
1041 pmc_can_attach(struct pmc *pm, struct proc *t)
1042 {
1043 	struct proc *o;		/* pmc owner */
1044 	struct ucred *oc, *tc;	/* owner, target credentials */
1045 	bool decline_attach;
1046 
1047 	/*
1048 	 * A PMC's owner can always attach that PMC to itself.
1049 	 */
1050 
1051 	if ((o = pm->pm_owner->po_owner) == t)
1052 		return (true);
1053 
1054 	PROC_LOCK(o);
1055 	oc = o->p_ucred;
1056 	crhold(oc);
1057 	PROC_UNLOCK(o);
1058 
1059 	PROC_LOCK(t);
1060 	tc = t->p_ucred;
1061 	crhold(tc);
1062 	PROC_UNLOCK(t);
1063 
1064 	/*
1065 	 * The effective uid of the PMC owner should match at least one
1066 	 * of the {effective,real,saved} uids of the target process.
1067 	 */
1068 
1069 	decline_attach = oc->cr_uid != tc->cr_uid &&
1070 	    oc->cr_uid != tc->cr_svuid &&
1071 	    oc->cr_uid != tc->cr_ruid;
1072 
1073 	/*
1074 	 * Every one of the target's group ids, must be in the owner's
1075 	 * group list.
1076 	 */
1077 	for (int i = 0; !decline_attach && i < tc->cr_ngroups; i++)
1078 		decline_attach = !groupmember(tc->cr_groups[i], oc);
1079 	if (!decline_attach)
1080 		decline_attach = !groupmember(tc->cr_gid, oc) ||
1081 		    !groupmember(tc->cr_rgid, oc) ||
1082 		    !groupmember(tc->cr_svgid, oc);
1083 
1084 	crfree(tc);
1085 	crfree(oc);
1086 
1087 	return (!decline_attach);
1088 }
1089 
1090 /*
1091  * Attach a process to a PMC.
1092  */
1093 static int
1094 pmc_attach_one_process(struct proc *p, struct pmc *pm)
1095 {
1096 	int ri, error;
1097 	char *fullpath, *freepath;
1098 	struct pmc_process	*pp;
1099 
1100 	sx_assert(&pmc_sx, SX_XLOCKED);
1101 
1102 	PMCDBG5(PRC,ATT,2, "attach-one pm=%p ri=%d proc=%p (%d, %s)", pm,
1103 	    PMC_TO_ROWINDEX(pm), p, p->p_pid, p->p_comm);
1104 
1105 	/*
1106 	 * Locate the process descriptor corresponding to process 'p',
1107 	 * allocating space as needed.
1108 	 *
1109 	 * Verify that rowindex 'pm_rowindex' is free in the process
1110 	 * descriptor.
1111 	 *
1112 	 * If not, allocate space for a descriptor and link the
1113 	 * process descriptor and PMC.
1114 	 */
1115 	ri = PMC_TO_ROWINDEX(pm);
1116 
1117 	/* mark process as using HWPMCs */
1118 	PROC_LOCK(p);
1119 	p->p_flag |= P_HWPMC;
1120 	PROC_UNLOCK(p);
1121 
1122 	if ((pp = pmc_find_process_descriptor(p, PMC_FLAG_ALLOCATE)) == NULL) {
1123 		error = ENOMEM;
1124 		goto fail;
1125 	}
1126 
1127 	if (pp->pp_pmcs[ri].pp_pmc == pm) {/* already present at slot [ri] */
1128 		error = EEXIST;
1129 		goto fail;
1130 	}
1131 
1132 	if (pp->pp_pmcs[ri].pp_pmc != NULL) {
1133 		error = EBUSY;
1134 		goto fail;
1135 	}
1136 
1137 	pmc_link_target_process(pm, pp);
1138 
1139 	if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)) &&
1140 	    (pm->pm_flags & PMC_F_ATTACHED_TO_OWNER) == 0)
1141 		pm->pm_flags |= PMC_F_NEEDS_LOGFILE;
1142 
1143 	pm->pm_flags |= PMC_F_ATTACH_DONE; /* mark as attached */
1144 
1145 	/* issue an attach event to a configured log file */
1146 	if (pm->pm_owner->po_flags & PMC_PO_OWNS_LOGFILE) {
1147 		if (p->p_flag & P_KPROC) {
1148 			fullpath = kernelname;
1149 			freepath = NULL;
1150 		} else {
1151 			pmc_getfilename(p->p_textvp, &fullpath, &freepath);
1152 			pmclog_process_pmcattach(pm, p->p_pid, fullpath);
1153 		}
1154 		free(freepath, M_TEMP);
1155 		if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
1156 			pmc_log_process_mappings(pm->pm_owner, p);
1157 	}
1158 
1159 	return (0);
1160 fail:
1161 	PROC_LOCK(p);
1162 	p->p_flag &= ~P_HWPMC;
1163 	PROC_UNLOCK(p);
1164 	return (error);
1165 }
1166 
1167 /*
1168  * Attach a process and optionally its children
1169  */
1170 static int
1171 pmc_attach_process(struct proc *p, struct pmc *pm)
1172 {
1173 	int error;
1174 	struct proc *top;
1175 
1176 	sx_assert(&pmc_sx, SX_XLOCKED);
1177 
1178 	PMCDBG5(PRC,ATT,1, "attach pm=%p ri=%d proc=%p (%d, %s)", pm,
1179 	    PMC_TO_ROWINDEX(pm), p, p->p_pid, p->p_comm);
1180 
1181 	/*
1182 	 * If this PMC successfully allowed a GETMSR operation
1183 	 * in the past, disallow further ATTACHes.
1184 	 */
1185 	if ((pm->pm_flags & PMC_PP_ENABLE_MSR_ACCESS) != 0)
1186 		return (EPERM);
1187 
1188 	if ((pm->pm_flags & PMC_F_DESCENDANTS) == 0)
1189 		return (pmc_attach_one_process(p, pm));
1190 
1191 	/*
1192 	 * Traverse all child processes, attaching them to
1193 	 * this PMC.
1194 	 */
1195 	sx_slock(&proctree_lock);
1196 
1197 	top = p;
1198 	for (;;) {
1199 		if ((error = pmc_attach_one_process(p, pm)) != 0)
1200 			break;
1201 		if (!LIST_EMPTY(&p->p_children))
1202 			p = LIST_FIRST(&p->p_children);
1203 		else for (;;) {
1204 			if (p == top)
1205 				goto done;
1206 			if (LIST_NEXT(p, p_sibling)) {
1207 				p = LIST_NEXT(p, p_sibling);
1208 				break;
1209 			}
1210 			p = p->p_pptr;
1211 		}
1212 	}
1213 
1214 	if (error != 0)
1215 		(void)pmc_detach_process(top, pm);
1216 
1217 done:
1218 	sx_sunlock(&proctree_lock);
1219 	return (error);
1220 }
1221 
1222 /*
1223  * Detach a process from a PMC.  If there are no other PMCs tracking
1224  * this process, remove the process structure from its hash table.  If
1225  * 'flags' contains PMC_FLAG_REMOVE, then free the process structure.
1226  */
1227 static int
1228 pmc_detach_one_process(struct proc *p, struct pmc *pm, int flags)
1229 {
1230 	int ri;
1231 	struct pmc_process *pp;
1232 
1233 	sx_assert(&pmc_sx, SX_XLOCKED);
1234 
1235 	KASSERT(pm != NULL,
1236 	    ("[pmc,%d] null pm pointer", __LINE__));
1237 
1238 	ri = PMC_TO_ROWINDEX(pm);
1239 
1240 	PMCDBG6(PRC,ATT,2, "detach-one pm=%p ri=%d proc=%p (%d, %s) flags=0x%x",
1241 	    pm, ri, p, p->p_pid, p->p_comm, flags);
1242 
1243 	if ((pp = pmc_find_process_descriptor(p, 0)) == NULL)
1244 		return (ESRCH);
1245 
1246 	if (pp->pp_pmcs[ri].pp_pmc != pm)
1247 		return (EINVAL);
1248 
1249 	/*
1250 	 * If this is a process-virtual PMC that is still loaded on the
1251 	 * hardware of the CPU we are running on (the common case when a
1252 	 * process detaches a PMC from itself), take it off and drop its
1253 	 * runcount reference now.  The reference is otherwise only
1254 	 * dropped by the switch-out reclaim, which the scheduler stops
1255 	 * calling once P_HWPMC is cleared below - leaking it and later
1256 	 * wedging pmc_wait_for_pmc_idle() at release time.
1257 	 */
1258 	if (PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm))) {
1259 		critical_enter();
1260 		pmc_reclaim_pmc_from_cpu(pm, pp, curthread->td_oncpu);
1261 		critical_exit();
1262 	}
1263 
1264 	pmc_unlink_target_process(pm, pp);
1265 
1266 	/* Issue a detach entry if a log file is configured */
1267 	if (pm->pm_owner->po_flags & PMC_PO_OWNS_LOGFILE)
1268 		pmclog_process_pmcdetach(pm, p->p_pid);
1269 
1270 	/*
1271 	 * If there are no PMCs targeting this process, we remove its
1272 	 * descriptor from the target hash table and unset the P_HWPMC
1273 	 * flag in the struct proc.
1274 	 */
1275 	KASSERT(pp->pp_refcnt >= 0 && pp->pp_refcnt <= (int) md->pmd_npmc,
1276 	    ("[pmc,%d] Illegal refcnt %d for process struct %p",
1277 		__LINE__, pp->pp_refcnt, pp));
1278 
1279 	if (pp->pp_refcnt != 0)	/* still a target of some PMC */
1280 		return (0);
1281 
1282 	/*
1283 	 * This detach removed the process' last PMC and we are about to
1284 	 * clear P_HWPMC.  If the detached PMC was its last target and is
1285 	 * still loaded on other CPUs (e.g. sibling threads of a
1286 	 * multi-threaded target, or a target running on another CPU),
1287 	 * drain those references first: the target is already unlinked so
1288 	 * it cannot reload the PMC, and P_HWPMC is still set so those
1289 	 * CPUs' switch-out reclaim still runs.  Bounded by the target
1290 	 * threads being scheduled out.
1291 	 */
1292 	if (PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm)) &&
1293 	    LIST_EMPTY(&pm->pm_targets)) {
1294 		while (counter_u64_fetch(pm->pm_runcount) > 0)
1295 			pmc_force_context_switch();
1296 	}
1297 
1298 	pmc_remove_process_descriptor(pp);
1299 
1300 	if (flags & PMC_FLAG_REMOVE)
1301 		pmc_destroy_process_descriptor(pp);
1302 
1303 	PROC_LOCK(p);
1304 	p->p_flag &= ~P_HWPMC;
1305 	PROC_UNLOCK(p);
1306 
1307 	return (0);
1308 }
1309 
1310 /*
1311  * Detach a process and optionally its descendants from a PMC.
1312  */
1313 static int
1314 pmc_detach_process(struct proc *p, struct pmc *pm)
1315 {
1316 	struct proc *top;
1317 
1318 	sx_assert(&pmc_sx, SX_XLOCKED);
1319 
1320 	PMCDBG5(PRC,ATT,1, "detach pm=%p ri=%d proc=%p (%d, %s)", pm,
1321 	    PMC_TO_ROWINDEX(pm), p, p->p_pid, p->p_comm);
1322 
1323 	if ((pm->pm_flags & PMC_F_DESCENDANTS) == 0)
1324 		return (pmc_detach_one_process(p, pm, PMC_FLAG_REMOVE));
1325 
1326 	/*
1327 	 * Traverse all children, detaching them from this PMC.  We
1328 	 * ignore errors since we could be detaching a PMC from a
1329 	 * partially attached proc tree.
1330 	 */
1331 	sx_slock(&proctree_lock);
1332 
1333 	top = p;
1334 	for (;;) {
1335 		(void)pmc_detach_one_process(p, pm, PMC_FLAG_REMOVE);
1336 
1337 		if (!LIST_EMPTY(&p->p_children)) {
1338 			p = LIST_FIRST(&p->p_children);
1339 		} else {
1340 			for (;;) {
1341 				if (p == top)
1342 					goto done;
1343 				if (LIST_NEXT(p, p_sibling)) {
1344 					p = LIST_NEXT(p, p_sibling);
1345 					break;
1346 				}
1347 				p = p->p_pptr;
1348 			}
1349 		}
1350 	}
1351 done:
1352 	sx_sunlock(&proctree_lock);
1353 	if (LIST_EMPTY(&pm->pm_targets))
1354 		pm->pm_flags &= ~PMC_F_ATTACH_DONE;
1355 
1356 	return (0);
1357 }
1358 
1359 /*
1360  * Handle events after an exec() for a process:
1361  *  - Inform log owners of the new exec() event
1362  *  - Release any PMCs owned by the process before the exec()
1363  *  - Detach PMCs from the target if required
1364  */
1365 static void
1366 pmc_process_exec(struct thread *td, struct pmckern_procexec *pk)
1367 {
1368 	struct pmc *pm;
1369 	struct pmc_owner *po;
1370 	struct pmc_process *pp;
1371 	struct proc *p;
1372 	char *fullpath, *freepath;
1373 	u_int ri;
1374 	bool is_using_hwpmcs;
1375 
1376 	sx_assert(&pmc_sx, SX_XLOCKED);
1377 
1378 	p = td->td_proc;
1379 	pmc_getfilename(p->p_textvp, &fullpath, &freepath);
1380 
1381 	PMC_EPOCH_ENTER();
1382 	/* Inform owners of SS mode PMCs of the exec event. */
1383 	CK_LIST_FOREACH(po, &pmc_ss_owners, po_ssnext) {
1384 		if ((po->po_flags & PMC_PO_OWNS_LOGFILE) != 0) {
1385 			pmclog_process_procexec(po, PMC_ID_INVALID, p->p_pid,
1386 			    pk->pm_baseaddr, pk->pm_dynaddr, fullpath);
1387 		}
1388 	}
1389 	PMC_EPOCH_EXIT();
1390 
1391 	PROC_LOCK(p);
1392 	is_using_hwpmcs = (p->p_flag & P_HWPMC) != 0;
1393 	PROC_UNLOCK(p);
1394 
1395 	if (!is_using_hwpmcs) {
1396 		if (freepath != NULL)
1397 			free(freepath, M_TEMP);
1398 		return;
1399 	}
1400 
1401 	/*
1402 	 * PMCs are not inherited across an exec(): remove any PMCs that this
1403 	 * process is the owner of.
1404 	 */
1405 	if ((po = pmc_find_owner_descriptor(p)) != NULL) {
1406 		pmc_remove_owner(po);
1407 		pmc_destroy_owner_descriptor(po);
1408 	}
1409 
1410 	/*
1411 	 * If the process being exec'ed is not the target of any PMC, we are
1412 	 * done.
1413 	 */
1414 	if ((pp = pmc_find_process_descriptor(p, 0)) == NULL) {
1415 		if (freepath != NULL)
1416 			free(freepath, M_TEMP);
1417 		return;
1418 	}
1419 
1420 	/*
1421 	 * Log the exec event to all monitoring owners. Skip owners who have
1422 	 * already received the event because they had system sampling PMCs
1423 	 * active.
1424 	 */
1425 	for (ri = 0; ri < md->pmd_npmc; ri++) {
1426 		if ((pm = pp->pp_pmcs[ri].pp_pmc) == NULL)
1427 			continue;
1428 
1429 		po = pm->pm_owner;
1430 		if (po->po_sscount == 0 &&
1431 		    (po->po_flags & PMC_PO_OWNS_LOGFILE) != 0) {
1432 			pmclog_process_procexec(po, pm->pm_id, p->p_pid,
1433 			    pk->pm_baseaddr, pk->pm_dynaddr, fullpath);
1434 		}
1435 	}
1436 
1437 	if (freepath != NULL)
1438 		free(freepath, M_TEMP);
1439 
1440 	PMCDBG4(PRC,EXC,1, "exec proc=%p (%d, %s) cred-changed=%d",
1441 	    p, p->p_pid, p->p_comm, pk->pm_credentialschanged);
1442 
1443 	if (pk->pm_credentialschanged == 0) /* no change */
1444 		return;
1445 
1446 	/*
1447 	 * If the newly exec()'ed process has a different credential
1448 	 * than before, allow it to be the target of a PMC only if
1449 	 * the PMC's owner has sufficient privilege.
1450 	 */
1451 	for (ri = 0; ri < md->pmd_npmc; ri++) {
1452 		if ((pm = pp->pp_pmcs[ri].pp_pmc) != NULL) {
1453 			if (pmc_can_attach(pm, td->td_proc)) {
1454 				pmc_detach_one_process(td->td_proc, pm,
1455 				    PMC_FLAG_NONE);
1456 			}
1457 		}
1458 	}
1459 
1460 	KASSERT(pp->pp_refcnt >= 0 && pp->pp_refcnt <= md->pmd_npmc,
1461 	    ("[pmc,%d] Illegal ref count %u on pp %p", __LINE__,
1462 		pp->pp_refcnt, pp));
1463 
1464 	/*
1465 	 * If this process is no longer the target of any
1466 	 * PMCs, we can remove the process entry and free
1467 	 * up space.
1468 	 */
1469 	if (pp->pp_refcnt == 0) {
1470 		pmc_remove_process_descriptor(pp);
1471 		pmc_destroy_process_descriptor(pp);
1472 	}
1473 }
1474 
1475 /*
1476  * Thread context switch IN.
1477  */
1478 static void
1479 pmc_process_csw_in(struct thread *td)
1480 {
1481 	struct pmc *pm;
1482 	struct pmc_classdep *pcd;
1483 	struct pmc_cpu *pc;
1484 	struct pmc_hw *phw __diagused;
1485 	struct pmc_process *pp;
1486 	struct pmc_thread *pt;
1487 	struct proc *p;
1488 	pmc_value_t newvalue;
1489 	int cpu;
1490 	u_int adjri, ri;
1491 
1492 	p = td->td_proc;
1493 	pt = NULL;
1494 	if ((pp = pmc_find_process_descriptor(p, PMC_FLAG_NONE)) == NULL)
1495 		return;
1496 
1497 	KASSERT(pp->pp_proc == td->td_proc,
1498 	    ("[pmc,%d] not my thread state", __LINE__));
1499 
1500 	critical_enter(); /* no preemption from this point */
1501 
1502 	cpu = PCPU_GET(cpuid); /* td->td_oncpu is invalid */
1503 
1504 	PMCDBG5(CSW,SWI,1, "cpu=%d proc=%p (%d, %s) pp=%p", cpu, p,
1505 	    p->p_pid, p->p_comm, pp);
1506 
1507 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
1508 	    ("[pmc,%d] weird CPU id %d", __LINE__, cpu));
1509 
1510 	pc = pmc_pcpu[cpu];
1511 	for (ri = 0; ri < md->pmd_npmc; ri++) {
1512 		if ((pm = pp->pp_pmcs[ri].pp_pmc) == NULL)
1513 			continue;
1514 
1515 		KASSERT(PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm)),
1516 		    ("[pmc,%d] Target PMC in non-virtual mode (%d)",
1517 		    __LINE__, PMC_TO_MODE(pm)));
1518 		KASSERT(PMC_TO_ROWINDEX(pm) == ri,
1519 		    ("[pmc,%d] Row index mismatch pmc %d != ri %d",
1520 		    __LINE__, PMC_TO_ROWINDEX(pm), ri));
1521 
1522 		/*
1523 		 * Only PMCs that are marked as 'RUNNING' need
1524 		 * be placed on hardware.
1525 		 */
1526 		if (pm->pm_state != PMC_STATE_RUNNING)
1527 			continue;
1528 
1529 		KASSERT(counter_u64_fetch(pm->pm_runcount) >= 0,
1530 		    ("[pmc,%d] pm=%p runcount %ju", __LINE__, pm,
1531 		    (uintmax_t)counter_u64_fetch(pm->pm_runcount)));
1532 
1533 		/* increment PMC runcount */
1534 		counter_u64_add(pm->pm_runcount, 1);
1535 
1536 		/* configure the HWPMC we are going to use. */
1537 		pcd = pmc_ri_to_classdep(md, ri, &adjri);
1538 		(void)pcd->pcd_config_pmc(cpu, adjri, pm);
1539 
1540 		phw = pc->pc_hwpmcs[ri];
1541 
1542 		KASSERT(phw != NULL,
1543 		    ("[pmc,%d] null hw pointer", __LINE__));
1544 
1545 		KASSERT(phw->phw_pmc == pm,
1546 		    ("[pmc,%d] hw->pmc %p != pmc %p", __LINE__,
1547 			phw->phw_pmc, pm));
1548 
1549 		/*
1550 		 * Write out saved value and start the PMC.
1551 		 *
1552 		 * Sampling PMCs use a per-thread value, while
1553 		 * counting mode PMCs use a per-pmc value that is
1554 		 * inherited across descendants.
1555 		 */
1556 		if (PMC_TO_MODE(pm) == PMC_MODE_TS) {
1557 			if (pt == NULL)
1558 				pt = pmc_find_thread_descriptor(pp, td,
1559 				    PMC_FLAG_NONE);
1560 
1561 			KASSERT(pt != NULL,
1562 			    ("[pmc,%d] No thread found for td=%p", __LINE__,
1563 			    td));
1564 
1565 			mtx_pool_lock_spin(pmc_mtxpool, pm);
1566 
1567 			/*
1568 			 * If we have a thread descriptor, use the per-thread
1569 			 * counter in the descriptor. If not, we will use
1570 			 * a per-process counter.
1571 			 *
1572 			 * TODO: Remove the per-process "safety net" once
1573 			 * we have thoroughly tested that we don't hit the
1574 			 * above assert.
1575 			 */
1576 			if (pt != NULL) {
1577 				if (pt->pt_pmcs[ri].pt_pmcval > 0)
1578 					newvalue = pt->pt_pmcs[ri].pt_pmcval;
1579 				else
1580 					newvalue = pm->pm_sc.pm_reloadcount;
1581 			} else {
1582 				/*
1583 				 * Use the saved value calculated after the most
1584 				 * recent time a thread using the shared counter
1585 				 * switched out. Reset the saved count in case
1586 				 * another thread from this process switches in
1587 				 * before any threads switch out.
1588 				 */
1589 				newvalue = pp->pp_pmcs[ri].pp_pmcval;
1590 				pp->pp_pmcs[ri].pp_pmcval =
1591 				    pm->pm_sc.pm_reloadcount;
1592 			}
1593 			mtx_pool_unlock_spin(pmc_mtxpool, pm);
1594 			KASSERT(newvalue > 0 && newvalue <=
1595 			    pm->pm_sc.pm_reloadcount,
1596 			    ("[pmc,%d] pmcval outside of expected range cpu=%d "
1597 			    "ri=%d pmcval=%jx pm_reloadcount=%jx", __LINE__,
1598 			    cpu, ri, newvalue, pm->pm_sc.pm_reloadcount));
1599 		} else {
1600 			KASSERT(PMC_TO_MODE(pm) == PMC_MODE_TC,
1601 			    ("[pmc,%d] illegal mode=%d", __LINE__,
1602 			    PMC_TO_MODE(pm)));
1603 			mtx_pool_lock_spin(pmc_mtxpool, pm);
1604 			newvalue = PMC_PCPU_SAVED(cpu, ri) =
1605 			    pm->pm_gv.pm_savedvalue;
1606 			mtx_pool_unlock_spin(pmc_mtxpool, pm);
1607 		}
1608 
1609 		PMCDBG3(CSW,SWI,1,"cpu=%d ri=%d new=%jd", cpu, ri, newvalue);
1610 
1611 		(void)pcd->pcd_write_pmc(cpu, adjri, pm, newvalue);
1612 
1613 		/* If a sampling mode PMC, reset stalled state. */
1614 		if (PMC_TO_MODE(pm) == PMC_MODE_TS)
1615 			pm->pm_pcpu_state[cpu].pps_stalled = 0;
1616 
1617 		/* Indicate that we desire this to run. */
1618 		pm->pm_pcpu_state[cpu].pps_cpustate = 1;
1619 
1620 		/* Start the PMC. */
1621 		(void)pcd->pcd_start_pmc(cpu, adjri, pm);
1622 	}
1623 
1624 	/*
1625 	 * Perform any other architecture/cpu dependent thread
1626 	 * switch-in actions.
1627 	 */
1628 	(void)(*md->pmd_switch_in)(pc, pp);
1629 
1630 	critical_exit();
1631 }
1632 
1633 /*
1634  * Compute the change in a counter's value since it was last written.
1635  * The hardware counter is only pcd_width bits wide and wraps around,
1636  * while the value seeded into it may occupy the full 64-bit range, so
1637  * take the difference modulo the counter width.
1638  */
1639 static pmc_value_t
1640 pmc_delta(const struct pmc_classdep *pcd, pmc_value_t newvalue,
1641     pmc_value_t oldvalue)
1642 {
1643 	pmc_value_t delta;
1644 
1645 	delta = newvalue - oldvalue;
1646 	if (pcd->pcd_width < 64)
1647 		delta &= ((pmc_value_t)1 << pcd->pcd_width) - 1;
1648 	return (delta);
1649 }
1650 
1651 /*
1652  * Take a process-virtual PMC off the hardware of 'cpu' if it is
1653  * currently loaded there for process 'pp', accumulating its final
1654  * count and dropping its runcount reference.  This is the same reclaim
1655  * that context switch out and process exit perform, factored out so it
1656  * can also run when a target is detached while the PMC may still be
1657  * live: the runcount reference is decremented by the switch-out reclaim
1658  * only, which the scheduler gates on P_HWPMC, so a detach that clears
1659  * P_HWPMC without draining would leak the reference and later wedge
1660  * pmc_wait_for_pmc_idle().  Must be called in a critical section.
1661  */
1662 static void
1663 pmc_reclaim_pmc_from_cpu(struct pmc *pm, struct pmc_process *pp, int cpu)
1664 {
1665 	struct pmc_classdep *pcd;
1666 	struct pmc *phw_pm;
1667 	pmc_value_t newvalue, tmp;
1668 	u_int adjri, ri;
1669 
1670 	ri = PMC_TO_ROWINDEX(pm);
1671 	pcd = pmc_ri_to_classdep(md, ri, &adjri);
1672 
1673 	/* Only reclaim if this PMC is actually loaded on this CPU. */
1674 	phw_pm = NULL;
1675 	(void)(*pcd->pcd_get_config)(cpu, adjri, &phw_pm);
1676 	if (phw_pm != pm)
1677 		return;
1678 
1679 	KASSERT(counter_u64_fetch(pm->pm_runcount) > 0,
1680 	    ("[pmc,%d] pm=%p runcount %ju", __LINE__, pm,
1681 	    (uintmax_t)counter_u64_fetch(pm->pm_runcount)));
1682 
1683 	if (pm->pm_pcpu_state[cpu].pps_cpustate) {
1684 		pm->pm_pcpu_state[cpu].pps_cpustate = 0;
1685 		if (pm->pm_pcpu_state[cpu].pps_stalled == 0) {
1686 			(void)pcd->pcd_stop_pmc(cpu, adjri, pm);
1687 
1688 			if (PMC_TO_MODE(pm) == PMC_MODE_TC) {
1689 				(void)pcd->pcd_read_pmc(cpu, adjri, pm,
1690 				    &newvalue);
1691 				tmp = pmc_delta(pcd, newvalue,
1692 				    PMC_PCPU_SAVED(cpu, ri));
1693 
1694 				mtx_pool_lock_spin(pmc_mtxpool, pm);
1695 				pm->pm_gv.pm_savedvalue += tmp;
1696 				pp->pp_pmcs[ri].pp_pmcval += tmp;
1697 				mtx_pool_unlock_spin(pmc_mtxpool, pm);
1698 			}
1699 		}
1700 	}
1701 
1702 	counter_u64_add(pm->pm_runcount, -1);
1703 	(void)pcd->pcd_config_pmc(cpu, adjri, NULL);
1704 }
1705 
1706 /*
1707  * Thread context switch OUT.
1708  */
1709 static void
1710 pmc_process_csw_out(struct thread *td)
1711 {
1712 	struct pmc *pm;
1713 	struct pmc_classdep *pcd;
1714 	struct pmc_cpu *pc;
1715 	struct pmc_process *pp;
1716 	struct pmc_thread *pt = NULL;
1717 	struct proc *p;
1718 	pmc_value_t newvalue, tmp;
1719 	enum pmc_mode mode;
1720 	int cpu;
1721 	u_int adjri, ri;
1722 
1723 	/*
1724 	 * Locate our process descriptor; this may be NULL if
1725 	 * this process is exiting and we have already removed
1726 	 * the process from the target process table.
1727 	 *
1728 	 * Note that due to kernel preemption, multiple
1729 	 * context switches may happen while the process is
1730 	 * exiting.
1731 	 *
1732 	 * Note also that if the target process cannot be
1733 	 * found we still need to deconfigure any PMCs that
1734 	 * are currently running on hardware.
1735 	 */
1736 	p = td->td_proc;
1737 	pp = pmc_find_process_descriptor(p, PMC_FLAG_NONE);
1738 
1739 	critical_enter();
1740 
1741 	cpu = PCPU_GET(cpuid); /* td->td_oncpu is invalid */
1742 
1743 	PMCDBG5(CSW,SWO,1, "cpu=%d proc=%p (%d, %s) pp=%p", cpu, p,
1744 	    p->p_pid, p->p_comm, pp);
1745 
1746 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
1747 	    ("[pmc,%d weird CPU id %d", __LINE__, cpu));
1748 
1749 	pc = pmc_pcpu[cpu];
1750 
1751 	/*
1752 	 * When a PMC gets unlinked from a target PMC, it will
1753 	 * be removed from the target's pp_pmc[] array.
1754 	 *
1755 	 * However, on a MP system, the target could have been
1756 	 * executing on another CPU at the time of the unlink.
1757 	 * So, at context switch OUT time, we need to look at
1758 	 * the hardware to determine if a PMC is scheduled on
1759 	 * it.
1760 	 */
1761 	for (ri = 0; ri < md->pmd_npmc; ri++) {
1762 		pcd = pmc_ri_to_classdep(md, ri, &adjri);
1763 		pm  = NULL;
1764 		(void)(*pcd->pcd_get_config)(cpu, adjri, &pm);
1765 
1766 		if (pm == NULL)	/* nothing at this row index */
1767 			continue;
1768 
1769 		mode = PMC_TO_MODE(pm);
1770 		if (!PMC_IS_VIRTUAL_MODE(mode))
1771 			continue; /* not a process virtual PMC */
1772 
1773 		KASSERT(PMC_TO_ROWINDEX(pm) == ri,
1774 		    ("[pmc,%d] ri mismatch pmc(%d) ri(%d)",
1775 			__LINE__, PMC_TO_ROWINDEX(pm), ri));
1776 
1777 		/*
1778 		 * Change desired state, and then stop if not stalled.
1779 		 * This two-step dance should avoid race conditions where
1780 		 * an interrupt re-enables the PMC after this code has
1781 		 * already checked the pm_stalled flag.
1782 		 */
1783 		pm->pm_pcpu_state[cpu].pps_cpustate = 0;
1784 		if (pm->pm_pcpu_state[cpu].pps_stalled == 0)
1785 			(void)pcd->pcd_stop_pmc(cpu, adjri, pm);
1786 
1787 		KASSERT(counter_u64_fetch(pm->pm_runcount) > 0,
1788 		    ("[pmc,%d] pm=%p runcount %ju", __LINE__, pm,
1789 		    (uintmax_t)counter_u64_fetch(pm->pm_runcount)));
1790 
1791 		/* reduce this PMC's runcount */
1792 		counter_u64_add(pm->pm_runcount, -1);
1793 
1794 		/*
1795 		 * If this PMC is associated with this process,
1796 		 * save the reading.
1797 		 */
1798 		if (pm->pm_state != PMC_STATE_DELETED && pp != NULL &&
1799 		    pp->pp_pmcs[ri].pp_pmc != NULL) {
1800 			KASSERT(pm == pp->pp_pmcs[ri].pp_pmc,
1801 			    ("[pmc,%d] pm %p != pp_pmcs[%d] %p", __LINE__,
1802 				pm, ri, pp->pp_pmcs[ri].pp_pmc));
1803 			KASSERT(pp->pp_refcnt > 0,
1804 			    ("[pmc,%d] pp refcnt = %d", __LINE__,
1805 				pp->pp_refcnt));
1806 
1807 			(void)pcd->pcd_read_pmc(cpu, adjri, pm, &newvalue);
1808 
1809 			if (mode == PMC_MODE_TS) {
1810 				PMCDBG3(CSW,SWO,1,"cpu=%d ri=%d val=%jd (samp)",
1811 				    cpu, ri, newvalue);
1812 
1813 				if (pt == NULL)
1814 					pt = pmc_find_thread_descriptor(pp, td,
1815 					    PMC_FLAG_NONE);
1816 
1817 				KASSERT(pt != NULL,
1818 				    ("[pmc,%d] No thread found for td=%p",
1819 				    __LINE__, td));
1820 
1821 				mtx_pool_lock_spin(pmc_mtxpool, pm);
1822 
1823 				/*
1824 				 * If we have a thread descriptor, save the
1825 				 * per-thread counter in the descriptor. If not,
1826 				 * we will update the per-process counter.
1827 				 *
1828 				 * TODO: Remove the per-process "safety net"
1829 				 * once we have thoroughly tested that we
1830 				 * don't hit the above assert.
1831 				 */
1832 				if (pt != NULL) {
1833 					pt->pt_pmcs[ri].pt_pmcval = newvalue;
1834 				} else {
1835 					/*
1836 					 * For sampling process-virtual PMCs,
1837 					 * newvalue is the number of events to
1838 					 * be seen until the next sampling
1839 					 * interrupt. We can just add the events
1840 					 * left from this invocation to the
1841 					 * counter, then adjust in case we
1842 					 * overflow our range.
1843 					 *
1844 					 * (Recall that we reload the counter
1845 					 * every time we use it.)
1846 					 */
1847 					pp->pp_pmcs[ri].pp_pmcval += newvalue;
1848 					if (pp->pp_pmcs[ri].pp_pmcval >
1849 					    pm->pm_sc.pm_reloadcount) {
1850 						pp->pp_pmcs[ri].pp_pmcval -=
1851 						    pm->pm_sc.pm_reloadcount;
1852 					}
1853 				}
1854 				mtx_pool_unlock_spin(pmc_mtxpool, pm);
1855 			} else {
1856 				/*
1857 				 * For counting process-virtual PMCs, the
1858 				 * hardware counter's value increases
1859 				 * monotonically modulo the counter width;
1860 				 * pmc_delta() recovers the increment even
1861 				 * when the counter wrapped during the run.
1862 				 */
1863 				tmp = pmc_delta(pcd, newvalue,
1864 				    PMC_PCPU_SAVED(cpu, ri));
1865 
1866 				PMCDBG3(CSW,SWO,1,"cpu=%d ri=%d tmp=%jd (count)",
1867 				    cpu, ri, tmp);
1868 
1869 				mtx_pool_lock_spin(pmc_mtxpool, pm);
1870 				pm->pm_gv.pm_savedvalue += tmp;
1871 				pp->pp_pmcs[ri].pp_pmcval += tmp;
1872 				mtx_pool_unlock_spin(pmc_mtxpool, pm);
1873 
1874 				if (pm->pm_flags & PMC_F_LOG_PROCCSW)
1875 					pmclog_process_proccsw(pm, pp, tmp, td);
1876 			}
1877 		}
1878 
1879 		/* Mark hardware as free. */
1880 		(void)pcd->pcd_config_pmc(cpu, adjri, NULL);
1881 	}
1882 
1883 	/*
1884 	 * Perform any other architecture/cpu dependent thread
1885 	 * switch out functions.
1886 	 */
1887 	(void)(*md->pmd_switch_out)(pc, pp);
1888 
1889 	critical_exit();
1890 }
1891 
1892 /*
1893  * A new thread for a process.
1894  */
1895 static void
1896 pmc_process_thread_add(struct thread *td)
1897 {
1898 	struct pmc_process *pmc;
1899 
1900 	pmc = pmc_find_process_descriptor(td->td_proc, PMC_FLAG_NONE);
1901 	if (pmc != NULL)
1902 		pmc_find_thread_descriptor(pmc, td, PMC_FLAG_ALLOCATE);
1903 }
1904 
1905 /*
1906  * A thread delete for a process.
1907  */
1908 static void
1909 pmc_process_thread_delete(struct thread *td)
1910 {
1911 	struct pmc_process *pmc;
1912 
1913 	pmc = pmc_find_process_descriptor(td->td_proc, PMC_FLAG_NONE);
1914 	if (pmc != NULL)
1915 		pmc_thread_descriptor_pool_free(pmc_find_thread_descriptor(pmc,
1916 		    td, PMC_FLAG_REMOVE));
1917 }
1918 
1919 /*
1920  * A userret() call for a thread.
1921  */
1922 static void
1923 pmc_process_thread_userret(struct thread *td)
1924 {
1925 	sched_pin();
1926 	pmc_capture_user_callchain(curcpu, PMC_UR, td->td_frame);
1927 	sched_unpin();
1928 }
1929 
1930 /*
1931  * A mapping change for a process.
1932  */
1933 static void
1934 pmc_process_mmap(struct thread *td, struct pmckern_map_in *pkm)
1935 {
1936 	const struct pmc *pm;
1937 	const struct pmc_process *pp;
1938 	struct pmc_owner *po;
1939 	char *fullpath, *freepath;
1940 	pid_t pid;
1941 	int ri;
1942 
1943 	MPASS(!in_epoch(global_epoch_preempt));
1944 
1945 	freepath = fullpath = NULL;
1946 	pmc_getfilename((struct vnode *)pkm->pm_file, &fullpath, &freepath);
1947 
1948 	pid = td->td_proc->p_pid;
1949 
1950 	PMC_EPOCH_ENTER();
1951 	/* Inform owners of all system-wide sampling PMCs. */
1952 	CK_LIST_FOREACH(po, &pmc_ss_owners, po_ssnext) {
1953 		if (po->po_flags & PMC_PO_OWNS_LOGFILE)
1954 			pmclog_process_map_in(po, pid, pkm->pm_address,
1955 			    fullpath);
1956 	}
1957 
1958 	if ((pp = pmc_find_process_descriptor(td->td_proc, 0)) == NULL)
1959 		goto done;
1960 
1961 	/*
1962 	 * Inform sampling PMC owners tracking this process.
1963 	 */
1964 	for (ri = 0; ri < md->pmd_npmc; ri++) {
1965 		if ((pm = pp->pp_pmcs[ri].pp_pmc) != NULL &&
1966 		    PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm))) {
1967 			pmclog_process_map_in(pm->pm_owner,
1968 			    pid, pkm->pm_address, fullpath);
1969 		}
1970 	}
1971 
1972 done:
1973 	if (freepath != NULL)
1974 		free(freepath, M_TEMP);
1975 	PMC_EPOCH_EXIT();
1976 }
1977 
1978 /*
1979  * Log an munmap request.
1980  */
1981 static void
1982 pmc_process_munmap(struct thread *td, struct pmckern_map_out *pkm)
1983 {
1984 	const struct pmc *pm;
1985 	const struct pmc_process *pp;
1986 	struct pmc_owner *po;
1987 	pid_t pid;
1988 	int ri;
1989 
1990 	pid = td->td_proc->p_pid;
1991 
1992 	PMC_EPOCH_ENTER();
1993 	CK_LIST_FOREACH(po, &pmc_ss_owners, po_ssnext) {
1994 		if (po->po_flags & PMC_PO_OWNS_LOGFILE)
1995 			pmclog_process_map_out(po, pid, pkm->pm_address,
1996 			    pkm->pm_address + pkm->pm_size);
1997 	}
1998 	PMC_EPOCH_EXIT();
1999 
2000 	if ((pp = pmc_find_process_descriptor(td->td_proc, 0)) == NULL)
2001 		return;
2002 
2003 	for (ri = 0; ri < md->pmd_npmc; ri++) {
2004 		pm = pp->pp_pmcs[ri].pp_pmc;
2005 		if (pm != NULL && PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm))) {
2006 			pmclog_process_map_out(pm->pm_owner, pid,
2007 			    pkm->pm_address, pkm->pm_address + pkm->pm_size);
2008 		}
2009 	}
2010 }
2011 
2012 /*
2013  * Log mapping information about the kernel.
2014  */
2015 static void
2016 pmc_log_kernel_mappings(struct pmc *pm)
2017 {
2018 	struct pmc_owner *po;
2019 	struct pmckern_map_in *km, *kmbase;
2020 
2021 	MPASS(in_epoch(global_epoch_preempt) || sx_xlocked(&pmc_sx));
2022 	KASSERT(PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)),
2023 	    ("[pmc,%d] non-sampling PMC (%p) desires mapping information",
2024 		__LINE__, (void *) pm));
2025 
2026 	po = pm->pm_owner;
2027 	if ((po->po_flags & PMC_PO_INITIAL_MAPPINGS_DONE) != 0)
2028 		return;
2029 
2030 	if (PMC_TO_MODE(pm) == PMC_MODE_SS)
2031 		pmc_process_allproc(pm);
2032 
2033 	/*
2034 	 * Log the current set of kernel modules.
2035 	 */
2036 	kmbase = linker_hwpmc_list_objects();
2037 	for (km = kmbase; km->pm_file != NULL; km++) {
2038 		PMCDBG2(LOG,REG,1,"%s %p", (char *)km->pm_file,
2039 		    (void *)km->pm_address);
2040 		pmclog_process_map_in(po, (pid_t)-1, km->pm_address,
2041 		    km->pm_file);
2042 	}
2043 	free(kmbase, M_LINKER);
2044 
2045 	po->po_flags |= PMC_PO_INITIAL_MAPPINGS_DONE;
2046 }
2047 
2048 /*
2049  * Log the mappings for a single process.
2050  */
2051 static void
2052 pmc_log_process_mappings(struct pmc_owner *po, struct proc *p)
2053 {
2054 	vm_map_t map;
2055 	vm_map_entry_t entry;
2056 	vm_object_t obj, lobj, tobj;
2057 	vm_offset_t last_end;
2058 	vm_offset_t start_addr;
2059 	struct vnode *vp, *last_vp;
2060 	struct vmspace *vm;
2061 	char *fullpath, *freepath;
2062 	u_int last_timestamp;
2063 
2064 	last_vp = NULL;
2065 	last_end = (vm_offset_t)0;
2066 	fullpath = freepath = NULL;
2067 
2068 	if ((vm = vmspace_acquire_ref(p)) == NULL)
2069 		return;
2070 
2071 	map = &vm->vm_map;
2072 	vm_map_lock_read(map);
2073 	VM_MAP_ENTRY_FOREACH(entry, map) {
2074 		if (entry == NULL) {
2075 			PMCDBG2(LOG,OPS,2, "hwpmc: vm_map entry unexpectedly "
2076 			    "NULL! pid=%d vm_map=%p\n", p->p_pid, map);
2077 			break;
2078 		}
2079 
2080 		/*
2081 		 * We only care about executable map entries.
2082 		 */
2083 		if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0 ||
2084 		    (entry->protection & VM_PROT_EXECUTE) == 0 ||
2085 		    entry->object.vm_object == NULL) {
2086 			continue;
2087 		}
2088 
2089 		obj = entry->object.vm_object;
2090 		VM_OBJECT_RLOCK(obj);
2091 
2092 		/*
2093 		 * Walk the backing_object list to find the base (non-shadowed)
2094 		 * vm_object.
2095 		 */
2096 		for (lobj = tobj = obj; tobj != NULL;
2097 		    tobj = tobj->backing_object) {
2098 			if (tobj != obj)
2099 				VM_OBJECT_RLOCK(tobj);
2100 			if (lobj != obj)
2101 				VM_OBJECT_RUNLOCK(lobj);
2102 			lobj = tobj;
2103 		}
2104 
2105 		/*
2106 		 * At this point lobj is the base vm_object and it is locked.
2107 		 */
2108 		if (lobj == NULL) {
2109 			PMCDBG3(LOG,OPS,2,
2110 			    "hwpmc: lobj unexpectedly NULL! pid=%d "
2111 			    "vm_map=%p vm_obj=%p\n", p->p_pid, map, obj);
2112 			VM_OBJECT_RUNLOCK(obj);
2113 			continue;
2114 		}
2115 
2116 		vp = vm_object_vnode(lobj);
2117 		if (vp == NULL) {
2118 			if (lobj != obj)
2119 				VM_OBJECT_RUNLOCK(lobj);
2120 			VM_OBJECT_RUNLOCK(obj);
2121 			continue;
2122 		}
2123 
2124 		/*
2125 		 * Skip contiguous regions that point to the same vnode, so we
2126 		 * don't emit redundant MAP-IN directives.
2127 		 */
2128 		if (entry->start == last_end && vp == last_vp) {
2129 			last_end = entry->end;
2130 			if (lobj != obj)
2131 				VM_OBJECT_RUNLOCK(lobj);
2132 			VM_OBJECT_RUNLOCK(obj);
2133 			continue;
2134 		}
2135 
2136 		/*
2137 		 * We don't want to keep the proc's vm_map or this vm_object
2138 		 * locked while we walk the pathname, since vn_fullpath() can
2139 		 * sleep.  However, if we drop the lock, it's possible for
2140 		 * concurrent activity to modify the vm_map list.  To protect
2141 		 * against this, we save the vm_map timestamp before we release
2142 		 * the lock, and check it after we reacquire the lock below.
2143 		 */
2144 		start_addr = entry->start;
2145 		last_end = entry->end;
2146 		last_timestamp = map->timestamp;
2147 		vm_map_unlock_read(map);
2148 
2149 		vref(vp);
2150 		if (lobj != obj)
2151 			VM_OBJECT_RUNLOCK(lobj);
2152 		VM_OBJECT_RUNLOCK(obj);
2153 
2154 		freepath = NULL;
2155 		pmc_getfilename(vp, &fullpath, &freepath);
2156 		last_vp = vp;
2157 
2158 		vrele(vp);
2159 
2160 		vp = NULL;
2161 		pmclog_process_map_in(po, p->p_pid, start_addr, fullpath);
2162 		if (freepath != NULL)
2163 			free(freepath, M_TEMP);
2164 
2165 		vm_map_lock_read(map);
2166 
2167 		/*
2168 		 * If our saved timestamp doesn't match, this means
2169 		 * that the vm_map was modified out from under us and
2170 		 * we can't trust our current "entry" pointer.  Do a
2171 		 * new lookup for this entry.  If there is no entry
2172 		 * for this address range, vm_map_lookup_entry() will
2173 		 * return the previous one, so we always want to go to
2174 		 * the next entry on the next loop iteration.
2175 		 *
2176 		 * There is an edge condition here that can occur if
2177 		 * there is no entry at or before this address.  In
2178 		 * this situation, vm_map_lookup_entry returns
2179 		 * &map->header, which would cause our loop to abort
2180 		 * without processing the rest of the map.  However,
2181 		 * in practice this will never happen for process
2182 		 * vm_map.  This is because the executable's text
2183 		 * segment is the first mapping in the proc's address
2184 		 * space, and this mapping is never removed until the
2185 		 * process exits, so there will always be a non-header
2186 		 * entry at or before the requested address for
2187 		 * vm_map_lookup_entry to return.
2188 		 */
2189 		if (map->timestamp != last_timestamp)
2190 			vm_map_lookup_entry(map, last_end - 1, &entry);
2191 	}
2192 
2193 	vm_map_unlock_read(map);
2194 	vmspace_free(vm);
2195 	return;
2196 }
2197 
2198 /*
2199  * Log mappings for all processes in the system.
2200  */
2201 static void
2202 pmc_log_all_process_mappings(struct pmc_owner *po)
2203 {
2204 	struct proc *p, *top;
2205 
2206 	sx_assert(&pmc_sx, SX_XLOCKED);
2207 
2208 	if ((p = pfind(1)) == NULL)
2209 		panic("[pmc,%d] Cannot find init", __LINE__);
2210 
2211 	PROC_UNLOCK(p);
2212 
2213 	sx_slock(&proctree_lock);
2214 
2215 	top = p;
2216 	for (;;) {
2217 		pmc_log_process_mappings(po, p);
2218 		if (!LIST_EMPTY(&p->p_children))
2219 			p = LIST_FIRST(&p->p_children);
2220 		else for (;;) {
2221 			if (p == top)
2222 				goto done;
2223 			if (LIST_NEXT(p, p_sibling)) {
2224 				p = LIST_NEXT(p, p_sibling);
2225 				break;
2226 			}
2227 			p = p->p_pptr;
2228 		}
2229 	}
2230 done:
2231 	sx_sunlock(&proctree_lock);
2232 }
2233 
2234 #ifdef HWPMC_DEBUG
2235 const char *pmc_hooknames[] = {
2236 	/* these strings correspond to PMC_FN_* in <sys/pmckern.h> */
2237 	"",
2238 	"EXEC",
2239 	"CSW-IN",
2240 	"CSW-OUT",
2241 	"SAMPLE",
2242 	"UNUSED1",
2243 	"UNUSED2",
2244 	"MMAP",
2245 	"MUNMAP",
2246 	"CALLCHAIN-NMI",
2247 	"CALLCHAIN-SOFT",
2248 	"SOFTSAMPLING",
2249 	"THR-CREATE",
2250 	"THR-EXIT",
2251 	"THR-USERRET",
2252 	"THR-CREATE-LOG",
2253 	"THR-EXIT-LOG",
2254 	"PROC-CREATE-LOG"
2255 };
2256 #endif
2257 
2258 /*
2259  * The 'hook' invoked from the kernel proper
2260  */
2261 static int
2262 pmc_hook_handler(struct thread *td, int function, void *arg)
2263 {
2264 	int cpu;
2265 
2266 	PMCDBG4(MOD,PMH,1, "hook td=%p func=%d \"%s\" arg=%p", td, function,
2267 	    pmc_hooknames[function], arg);
2268 
2269 	switch (function) {
2270 	case PMC_FN_PROCESS_EXEC:
2271 		pmc_process_exec(td, (struct pmckern_procexec *)arg);
2272 		break;
2273 
2274 	case PMC_FN_CSW_IN:
2275 		pmc_process_csw_in(td);
2276 		break;
2277 
2278 	case PMC_FN_CSW_OUT:
2279 		pmc_process_csw_out(td);
2280 		break;
2281 
2282 	/*
2283 	 * Process accumulated PC samples.
2284 	 *
2285 	 * This function is expected to be called by hardclock() for
2286 	 * each CPU that has accumulated PC samples.
2287 	 *
2288 	 * This function is to be executed on the CPU whose samples
2289 	 * are being processed.
2290 	 */
2291 	case PMC_FN_DO_SAMPLES:
2292 		/*
2293 		 * Clear the cpu specific bit in the CPU mask before
2294 		 * do the rest of the processing.  If the NMI handler
2295 		 * gets invoked after the "atomic_clear_int()" call
2296 		 * below but before "pmc_process_samples()" gets
2297 		 * around to processing the interrupt, then we will
2298 		 * come back here at the next hardclock() tick (and
2299 		 * may find nothing to do if "pmc_process_samples()"
2300 		 * had already processed the interrupt).  We don't
2301 		 * lose the interrupt sample.
2302 		 */
2303 		DPCPU_SET(pmc_sampled, 0);
2304 		cpu = PCPU_GET(cpuid);
2305 		pmc_process_samples(cpu, PMC_HR);
2306 		pmc_process_samples(cpu, PMC_SR);
2307 		pmc_process_samples(cpu, PMC_UR);
2308 		break;
2309 
2310 	case PMC_FN_MMAP:
2311 		pmc_process_mmap(td, (struct pmckern_map_in *)arg);
2312 		break;
2313 
2314 	case PMC_FN_MUNMAP:
2315 		MPASS(in_epoch(global_epoch_preempt) || sx_xlocked(&pmc_sx));
2316 		pmc_process_munmap(td, (struct pmckern_map_out *)arg);
2317 		break;
2318 
2319 	case PMC_FN_PROC_CREATE_LOG:
2320 		pmc_process_proccreate((struct proc *)arg);
2321 		break;
2322 
2323 	case PMC_FN_USER_CALLCHAIN:
2324 		/*
2325 		 * Record a call chain.
2326 		 */
2327 		KASSERT(td == curthread, ("[pmc,%d] td != curthread",
2328 		    __LINE__));
2329 
2330 		pmc_capture_user_callchain(PCPU_GET(cpuid), PMC_HR,
2331 		    (struct trapframe *)arg);
2332 
2333 		KASSERT(td->td_pinned == 1,
2334 		    ("[pmc,%d] invalid td_pinned value", __LINE__));
2335 		sched_unpin();  /* Can migrate safely now. */
2336 
2337 		td->td_pflags &= ~TDP_CALLCHAIN;
2338 		break;
2339 
2340 	case PMC_FN_USER_CALLCHAIN_SOFT:
2341 		/*
2342 		 * Record a call chain.
2343 		 */
2344 		KASSERT(td == curthread, ("[pmc,%d] td != curthread",
2345 		    __LINE__));
2346 
2347 		cpu = PCPU_GET(cpuid);
2348 		pmc_capture_user_callchain(cpu, PMC_SR,
2349 		    (struct trapframe *) arg);
2350 
2351 		KASSERT(td->td_pinned == 1,
2352 		    ("[pmc,%d] invalid td_pinned value", __LINE__));
2353 
2354 		sched_unpin();  /* Can migrate safely now. */
2355 
2356 		td->td_pflags &= ~TDP_CALLCHAIN;
2357 		break;
2358 
2359 	case PMC_FN_SOFT_SAMPLING:
2360 		/*
2361 		 * Call soft PMC sampling intr.
2362 		 */
2363 		pmc_soft_intr((struct pmckern_soft *)arg);
2364 		break;
2365 
2366 	case PMC_FN_THR_CREATE:
2367 		pmc_process_thread_add(td);
2368 		pmc_process_threadcreate(td);
2369 		break;
2370 
2371 	case PMC_FN_THR_CREATE_LOG:
2372 		pmc_process_threadcreate(td);
2373 		break;
2374 
2375 	case PMC_FN_THR_EXIT:
2376 		KASSERT(td == curthread, ("[pmc,%d] td != curthread",
2377 		    __LINE__));
2378 		pmc_process_thread_delete(td);
2379 		pmc_process_threadexit(td);
2380 		break;
2381 	case PMC_FN_THR_EXIT_LOG:
2382 		pmc_process_threadexit(td);
2383 		break;
2384 	case PMC_FN_THR_USERRET:
2385 		KASSERT(td == curthread, ("[pmc,%d] td != curthread",
2386 		    __LINE__));
2387 		pmc_process_thread_userret(td);
2388 		break;
2389 	default:
2390 #ifdef HWPMC_DEBUG
2391 		KASSERT(0, ("[pmc,%d] unknown hook %d\n", __LINE__, function));
2392 #endif
2393 		break;
2394 	}
2395 
2396 	return (0);
2397 }
2398 
2399 /*
2400  * Allocate a 'struct pmc_owner' descriptor in the owner hash table.
2401  */
2402 static struct pmc_owner *
2403 pmc_allocate_owner_descriptor(struct proc *p)
2404 {
2405 	struct pmc_owner *po;
2406 	struct pmc_ownerhash *poh;
2407 	uint32_t hindex;
2408 
2409 	hindex = PMC_HASH_PTR(p, pmc_ownerhashmask);
2410 	poh = &pmc_ownerhash[hindex];
2411 
2412 	/* Allocate space for N pointers and one descriptor struct. */
2413 	po = malloc(sizeof(struct pmc_owner), M_PMC, M_WAITOK | M_ZERO);
2414 	po->po_owner = p;
2415 	LIST_INSERT_HEAD(poh, po, po_next); /* insert into hash table */
2416 
2417 	TAILQ_INIT(&po->po_logbuffers);
2418 	mtx_init(&po->po_mtx, "pmc-owner-mtx", "pmc-per-proc", MTX_SPIN);
2419 
2420 	PMCDBG4(OWN,ALL,1, "allocate-owner proc=%p (%d, %s) pmc-owner=%p",
2421 	    p, p->p_pid, p->p_comm, po);
2422 
2423 	return (po);
2424 }
2425 
2426 static void
2427 pmc_destroy_owner_descriptor(struct pmc_owner *po)
2428 {
2429 
2430 	PMCDBG4(OWN,REL,1, "destroy-owner po=%p proc=%p (%d, %s)",
2431 	    po, po->po_owner, po->po_owner->p_pid, po->po_owner->p_comm);
2432 
2433 	mtx_destroy(&po->po_mtx);
2434 	free(po, M_PMC);
2435 }
2436 
2437 /*
2438  * Allocate a thread descriptor from the free pool.
2439  *
2440  * NOTE: This *can* return NULL.
2441  */
2442 static struct pmc_thread *
2443 pmc_thread_descriptor_pool_alloc(void)
2444 {
2445 	struct pmc_thread *pt;
2446 
2447 	mtx_lock_spin(&pmc_threadfreelist_mtx);
2448 	if ((pt = LIST_FIRST(&pmc_threadfreelist)) != NULL) {
2449 		LIST_REMOVE(pt, pt_next);
2450 		pmc_threadfreelist_entries--;
2451 	}
2452 	mtx_unlock_spin(&pmc_threadfreelist_mtx);
2453 
2454 	return (pt);
2455 }
2456 
2457 /*
2458  * Add a thread descriptor to the free pool. We use this instead of free()
2459  * to maintain a cache of free entries. Additionally, we can safely call
2460  * this function when we cannot call free(), such as in a critical section.
2461  */
2462 static void
2463 pmc_thread_descriptor_pool_free(struct pmc_thread *pt)
2464 {
2465 
2466 	if (pt == NULL)
2467 		return;
2468 
2469 	memset(pt, 0, THREADENTRY_SIZE);
2470 	mtx_lock_spin(&pmc_threadfreelist_mtx);
2471 	LIST_INSERT_HEAD(&pmc_threadfreelist, pt, pt_next);
2472 	pmc_threadfreelist_entries++;
2473 	if (pmc_threadfreelist_entries > pmc_threadfreelist_max)
2474 		taskqueue_enqueue(taskqueue_fast, &free_task);
2475 	mtx_unlock_spin(&pmc_threadfreelist_mtx);
2476 }
2477 
2478 /*
2479  * An asynchronous task to manage the free list.
2480  */
2481 static void
2482 pmc_thread_descriptor_pool_free_task(void *arg __unused, int pending __unused)
2483 {
2484 	struct pmc_thread *pt;
2485 	LIST_HEAD(, pmc_thread) tmplist;
2486 	int delta;
2487 
2488 	LIST_INIT(&tmplist);
2489 
2490 	/* Determine what changes, if any, we need to make. */
2491 	mtx_lock_spin(&pmc_threadfreelist_mtx);
2492 	delta = pmc_threadfreelist_entries - pmc_threadfreelist_max;
2493 	while (delta > 0 && (pt = LIST_FIRST(&pmc_threadfreelist)) != NULL) {
2494 		delta--;
2495 		pmc_threadfreelist_entries--;
2496 		LIST_REMOVE(pt, pt_next);
2497 		LIST_INSERT_HEAD(&tmplist, pt, pt_next);
2498 	}
2499 	mtx_unlock_spin(&pmc_threadfreelist_mtx);
2500 
2501 	/* If there are entries to free, free them. */
2502 	while (!LIST_EMPTY(&tmplist)) {
2503 		pt = LIST_FIRST(&tmplist);
2504 		LIST_REMOVE(pt, pt_next);
2505 		free(pt, M_PMC);
2506 	}
2507 }
2508 
2509 /*
2510  * Drain the thread free pool, freeing all allocations.
2511  */
2512 static void
2513 pmc_thread_descriptor_pool_drain(void)
2514 {
2515 	struct pmc_thread *pt, *next;
2516 
2517 	LIST_FOREACH_SAFE(pt, &pmc_threadfreelist, pt_next, next) {
2518 		LIST_REMOVE(pt, pt_next);
2519 		free(pt, M_PMC);
2520 	}
2521 }
2522 
2523 /*
2524  * find the descriptor corresponding to thread 'td', adding or removing it
2525  * as specified by 'mode'.
2526  *
2527  * Note that this supports additional mode flags in addition to those
2528  * supported by pmc_find_process_descriptor():
2529  * PMC_FLAG_NOWAIT: Causes the function to not wait for mallocs.
2530  *     This makes it safe to call while holding certain other locks.
2531  */
2532 static struct pmc_thread *
2533 pmc_find_thread_descriptor(struct pmc_process *pp, struct thread *td,
2534     uint32_t mode)
2535 {
2536 	struct pmc_thread *pt = NULL, *ptnew = NULL;
2537 	int wait_flag;
2538 
2539 	KASSERT(td != NULL, ("[pmc,%d] called to add NULL td", __LINE__));
2540 
2541 	/*
2542 	 * Pre-allocate memory in the PMC_FLAG_ALLOCATE case prior to
2543 	 * acquiring the lock.
2544 	 */
2545 	if ((mode & PMC_FLAG_ALLOCATE) != 0) {
2546 		if ((ptnew = pmc_thread_descriptor_pool_alloc()) == NULL) {
2547 			wait_flag = M_WAITOK;
2548 			if ((mode & PMC_FLAG_NOWAIT) != 0 ||
2549 			    in_epoch(global_epoch_preempt))
2550 				wait_flag = M_NOWAIT;
2551 
2552 			ptnew = malloc(THREADENTRY_SIZE, M_PMC,
2553 			    wait_flag | M_ZERO);
2554 		}
2555 	}
2556 
2557 	mtx_lock_spin(pp->pp_tdslock);
2558 	LIST_FOREACH(pt, &pp->pp_tds, pt_next) {
2559 		if (pt->pt_td == td)
2560 			break;
2561 	}
2562 
2563 	if ((mode & PMC_FLAG_REMOVE) != 0 && pt != NULL)
2564 		LIST_REMOVE(pt, pt_next);
2565 
2566 	if ((mode & PMC_FLAG_ALLOCATE) != 0 && pt == NULL && ptnew != NULL) {
2567 		pt = ptnew;
2568 		ptnew = NULL;
2569 		pt->pt_td = td;
2570 		LIST_INSERT_HEAD(&pp->pp_tds, pt, pt_next);
2571 	}
2572 
2573 	mtx_unlock_spin(pp->pp_tdslock);
2574 
2575 	if (ptnew != NULL) {
2576 		free(ptnew, M_PMC);
2577 	}
2578 
2579 	return (pt);
2580 }
2581 
2582 /*
2583  * Try to add thread descriptors for each thread in a process.
2584  */
2585 static void
2586 pmc_add_thread_descriptors_from_proc(struct proc *p, struct pmc_process *pp)
2587 {
2588 	struct pmc_thread **tdlist;
2589 	struct thread *curtd;
2590 	int i, tdcnt, tdlistsz;
2591 
2592 	KASSERT(!PROC_LOCKED(p), ("[pmc,%d] proc unexpectedly locked",
2593 	    __LINE__));
2594 	tdcnt = 32;
2595 restart:
2596 	tdlistsz = roundup2(tdcnt, 32);
2597 
2598 	tdcnt = 0;
2599 	tdlist = malloc(sizeof(struct pmc_thread *) * tdlistsz, M_TEMP,
2600 	    M_WAITOK);
2601 
2602 	PROC_LOCK(p);
2603 	FOREACH_THREAD_IN_PROC(p, curtd)
2604 		tdcnt++;
2605 	if (tdcnt >= tdlistsz) {
2606 		PROC_UNLOCK(p);
2607 		free(tdlist, M_TEMP);
2608 		goto restart;
2609 	}
2610 
2611 	/*
2612 	 * Try to add each thread to the list without sleeping. If unable,
2613 	 * add to a queue to retry after dropping the process lock.
2614 	 */
2615 	tdcnt = 0;
2616 	FOREACH_THREAD_IN_PROC(p, curtd) {
2617 		tdlist[tdcnt] = pmc_find_thread_descriptor(pp, curtd,
2618 		    PMC_FLAG_ALLOCATE | PMC_FLAG_NOWAIT);
2619 		if (tdlist[tdcnt] == NULL) {
2620 			PROC_UNLOCK(p);
2621 			for (i = 0; i <= tdcnt; i++)
2622 				pmc_thread_descriptor_pool_free(tdlist[i]);
2623 			free(tdlist, M_TEMP);
2624 			goto restart;
2625 		}
2626 		tdcnt++;
2627 	}
2628 	PROC_UNLOCK(p);
2629 	free(tdlist, M_TEMP);
2630 }
2631 
2632 /*
2633  * Find the descriptor corresponding to process 'p', adding or removing it
2634  * as specified by 'mode'.
2635  */
2636 static struct pmc_process *
2637 pmc_find_process_descriptor(struct proc *p, uint32_t mode)
2638 {
2639 	struct pmc_process *pp, *ppnew;
2640 	struct pmc_processhash *pph;
2641 	uint32_t hindex;
2642 
2643 	hindex = PMC_HASH_PTR(p, pmc_processhashmask);
2644 	pph = &pmc_processhash[hindex];
2645 
2646 	ppnew = NULL;
2647 
2648 	/*
2649 	 * Pre-allocate memory in the PMC_FLAG_ALLOCATE case since we
2650 	 * cannot call malloc(9) once we hold a spin lock.
2651 	 */
2652 	if ((mode & PMC_FLAG_ALLOCATE) != 0)
2653 		ppnew = malloc(sizeof(struct pmc_process) + md->pmd_npmc *
2654 		    sizeof(struct pmc_targetstate), M_PMC, M_WAITOK | M_ZERO);
2655 
2656 	mtx_lock_spin(&pmc_processhash_mtx);
2657 	LIST_FOREACH(pp, pph, pp_next) {
2658 		if (pp->pp_proc == p)
2659 			break;
2660 	}
2661 
2662 	if ((mode & PMC_FLAG_REMOVE) != 0 && pp != NULL)
2663 		LIST_REMOVE(pp, pp_next);
2664 
2665 	if ((mode & PMC_FLAG_ALLOCATE) != 0 && pp == NULL && ppnew != NULL) {
2666 		ppnew->pp_proc = p;
2667 		LIST_INIT(&ppnew->pp_tds);
2668 		ppnew->pp_tdslock = mtx_pool_find(pmc_mtxpool, ppnew);
2669 		LIST_INSERT_HEAD(pph, ppnew, pp_next);
2670 		mtx_unlock_spin(&pmc_processhash_mtx);
2671 		pp = ppnew;
2672 		ppnew = NULL;
2673 
2674 		/* Add thread descriptors for this process' current threads. */
2675 		pmc_add_thread_descriptors_from_proc(p, pp);
2676 	} else
2677 		mtx_unlock_spin(&pmc_processhash_mtx);
2678 
2679 	if (ppnew != NULL)
2680 		free(ppnew, M_PMC);
2681 	return (pp);
2682 }
2683 
2684 /*
2685  * Remove a process descriptor from the process hash table.
2686  */
2687 static void
2688 pmc_remove_process_descriptor(struct pmc_process *pp)
2689 {
2690 	KASSERT(pp->pp_refcnt == 0,
2691 	    ("[pmc,%d] Removing process descriptor %p with count %d",
2692 	     __LINE__, pp, pp->pp_refcnt));
2693 
2694 	mtx_lock_spin(&pmc_processhash_mtx);
2695 	LIST_REMOVE(pp, pp_next);
2696 	mtx_unlock_spin(&pmc_processhash_mtx);
2697 }
2698 
2699 /*
2700  * Destroy a process descriptor.
2701  */
2702 static void
2703 pmc_destroy_process_descriptor(struct pmc_process *pp)
2704 {
2705 	struct pmc_thread *pmc_td;
2706 
2707 	while ((pmc_td = LIST_FIRST(&pp->pp_tds)) != NULL) {
2708 		LIST_REMOVE(pmc_td, pt_next);
2709 		pmc_thread_descriptor_pool_free(pmc_td);
2710 	}
2711 	free(pp, M_PMC);
2712 }
2713 
2714 /*
2715  * Find an owner descriptor corresponding to proc 'p'.
2716  */
2717 static struct pmc_owner *
2718 pmc_find_owner_descriptor(struct proc *p)
2719 {
2720 	struct pmc_owner *po;
2721 	struct pmc_ownerhash *poh;
2722 	uint32_t hindex;
2723 
2724 	hindex = PMC_HASH_PTR(p, pmc_ownerhashmask);
2725 	poh = &pmc_ownerhash[hindex];
2726 
2727 	po = NULL;
2728 	LIST_FOREACH(po, poh, po_next) {
2729 		if (po->po_owner == p)
2730 			break;
2731 	}
2732 
2733 	PMCDBG5(OWN,FND,1, "find-owner proc=%p (%d, %s) hindex=0x%x -> "
2734 	    "pmc-owner=%p", p, p->p_pid, p->p_comm, hindex, po);
2735 
2736 	return (po);
2737 }
2738 
2739 /*
2740  * Allocate a pmc descriptor and initialize its fields.
2741  */
2742 static struct pmc *
2743 pmc_allocate_pmc_descriptor(void)
2744 {
2745 	struct pmc *pmc;
2746 
2747 	pmc = malloc(sizeof(struct pmc), M_PMC, M_WAITOK | M_ZERO);
2748 	pmc->pm_runcount = counter_u64_alloc(M_WAITOK);
2749 	pmc->pm_pcpu_state = malloc(sizeof(struct pmc_pcpu_state) * mp_ncpus,
2750 	    M_PMC, M_WAITOK | M_ZERO);
2751 	PMCDBG1(PMC,ALL,1, "allocate-pmc -> pmc=%p", pmc);
2752 
2753 	return (pmc);
2754 }
2755 
2756 /*
2757  * Destroy a pmc descriptor.
2758  */
2759 static void
2760 pmc_destroy_pmc_descriptor(struct pmc *pm)
2761 {
2762 
2763 	KASSERT(pm->pm_state == PMC_STATE_DELETED ||
2764 	    pm->pm_state == PMC_STATE_FREE,
2765 	    ("[pmc,%d] destroying non-deleted PMC", __LINE__));
2766 	KASSERT(LIST_EMPTY(&pm->pm_targets),
2767 	    ("[pmc,%d] destroying pmc with targets", __LINE__));
2768 	KASSERT(pm->pm_owner == NULL,
2769 	    ("[pmc,%d] destroying pmc attached to an owner", __LINE__));
2770 	KASSERT(counter_u64_fetch(pm->pm_runcount) == 0,
2771 	    ("[pmc,%d] pmc has non-zero run count %ju", __LINE__,
2772 	    (uintmax_t)counter_u64_fetch(pm->pm_runcount)));
2773 
2774 	counter_u64_free(pm->pm_runcount);
2775 	free(pm->pm_pcpu_state, M_PMC);
2776 	free(pm, M_PMC);
2777 }
2778 
2779 static void
2780 pmc_wait_for_pmc_idle(struct pmc *pm)
2781 {
2782 #ifdef INVARIANTS
2783 	volatile int maxloop;
2784 
2785 	maxloop = 100 * pmc_cpu_max();
2786 #endif
2787 	/*
2788 	 * Loop (with a forced context switch) till the PMC's runcount
2789 	 * comes down to zero.
2790 	 */
2791 	pmclog_flush(pm->pm_owner, 1);
2792 	while (counter_u64_fetch(pm->pm_runcount) > 0) {
2793 		pmclog_flush(pm->pm_owner, 1);
2794 #ifdef INVARIANTS
2795 		maxloop--;
2796 		KASSERT(maxloop > 0,
2797 		    ("[pmc,%d] (ri%d, rc%ju) waiting too long for "
2798 		     "pmc to be free", __LINE__, PMC_TO_ROWINDEX(pm),
2799 		     (uintmax_t)counter_u64_fetch(pm->pm_runcount)));
2800 #endif
2801 		pmc_force_context_switch();
2802 	}
2803 }
2804 
2805 /*
2806  * This function does the following things:
2807  *
2808  *  - detaches the PMC from hardware
2809  *  - unlinks all target threads that were attached to it
2810  *  - removes the PMC from its owner's list
2811  *  - destroys the PMC private mutex
2812  *
2813  * Once this function completes, the given pmc pointer can be freed by
2814  * calling pmc_destroy_pmc_descriptor().
2815  */
2816 static void
2817 pmc_release_pmc_descriptor(struct pmc *pm)
2818 {
2819 	struct pmc_binding pb;
2820 	struct pmc_classdep *pcd;
2821 	struct pmc_hw *phw __diagused;
2822 	struct pmc_owner *po;
2823 	struct pmc_process *pp;
2824 	struct pmc_target *ptgt, *tmp;
2825 	enum pmc_mode mode;
2826 	u_int adjri, ri, cpu;
2827 
2828 	sx_assert(&pmc_sx, SX_XLOCKED);
2829 	KASSERT(pm, ("[pmc,%d] null pmc", __LINE__));
2830 
2831 	ri   = PMC_TO_ROWINDEX(pm);
2832 	pcd  = pmc_ri_to_classdep(md, ri, &adjri);
2833 	mode = PMC_TO_MODE(pm);
2834 
2835 	PMCDBG3(PMC,REL,1, "release-pmc pmc=%p ri=%d mode=%d", pm, ri,
2836 	    mode);
2837 
2838 	/*
2839 	 * First, we take the PMC off hardware.
2840 	 */
2841 	cpu = 0;
2842 	if (PMC_IS_SYSTEM_MODE(mode)) {
2843 		/*
2844 		 * A system mode PMC runs on a specific CPU. Switch
2845 		 * to this CPU and turn hardware off.
2846 		 */
2847 		pmc_save_cpu_binding(&pb);
2848 		cpu = PMC_TO_CPU(pm);
2849 		pmc_select_cpu(cpu);
2850 
2851 		/* switch off non-stalled CPUs */
2852 		pm->pm_pcpu_state[cpu].pps_cpustate = 0;
2853 		if (pm->pm_state == PMC_STATE_RUNNING &&
2854 			pm->pm_pcpu_state[cpu].pps_stalled == 0) {
2855 
2856 			phw = pmc_pcpu[cpu]->pc_hwpmcs[ri];
2857 
2858 			KASSERT(phw->phw_pmc == pm,
2859 			    ("[pmc, %d] pmc ptr ri(%d) hw(%p) pm(%p)",
2860 				__LINE__, ri, phw->phw_pmc, pm));
2861 			PMCDBG2(PMC,REL,2, "stopping cpu=%d ri=%d", cpu, ri);
2862 
2863 			critical_enter();
2864 			(void)pcd->pcd_stop_pmc(cpu, adjri, pm);
2865 			critical_exit();
2866 		}
2867 
2868 		PMCDBG2(PMC,REL,2, "decfg cpu=%d ri=%d", cpu, ri);
2869 
2870 		critical_enter();
2871 		(void)pcd->pcd_config_pmc(cpu, adjri, NULL);
2872 		critical_exit();
2873 
2874 		/* adjust the global and process count of SS mode PMCs */
2875 		if (mode == PMC_MODE_SS && pm->pm_state == PMC_STATE_RUNNING) {
2876 			po = pm->pm_owner;
2877 			po->po_sscount--;
2878 			if (po->po_sscount == 0) {
2879 				atomic_subtract_rel_int(&pmc_ss_count, 1);
2880 				CK_LIST_REMOVE(po, po_ssnext);
2881 				epoch_wait_preempt(global_epoch_preempt);
2882 			}
2883 		}
2884 		pm->pm_state = PMC_STATE_DELETED;
2885 
2886 		pmc_restore_cpu_binding(&pb);
2887 
2888 		/*
2889 		 * We could have references to this PMC structure in the
2890 		 * per-cpu sample queues.  Wait for the queue to drain.
2891 		 */
2892 		pmc_wait_for_pmc_idle(pm);
2893 
2894 	} else if (PMC_IS_VIRTUAL_MODE(mode)) {
2895 		/*
2896 		 * A virtual PMC could be running on multiple CPUs at a given
2897 		 * instant.
2898 		 *
2899 		 * By marking its state as DELETED, we ensure that this PMC is
2900 		 * never further scheduled on hardware.
2901 		 *
2902 		 * Then we wait till all CPUs are done with this PMC.
2903 		 */
2904 		pm->pm_state = PMC_STATE_DELETED;
2905 
2906 		/* Wait for the PMCs runcount to come to zero. */
2907 		pmc_wait_for_pmc_idle(pm);
2908 
2909 		/*
2910 		 * At this point the PMC is off all CPUs and cannot be freshly
2911 		 * scheduled onto a CPU. It is now safe to unlink all targets
2912 		 * from this PMC. If a process-record's refcount falls to zero,
2913 		 * we remove it from the hash table. The module-wide SX lock
2914 		 * protects us from races.
2915 		 */
2916 		LIST_FOREACH_SAFE(ptgt, &pm->pm_targets, pt_next, tmp) {
2917 			pp = ptgt->pt_process;
2918 			pmc_unlink_target_process(pm, pp); /* frees 'ptgt' */
2919 
2920 			PMCDBG1(PMC,REL,3, "pp->refcnt=%d", pp->pp_refcnt);
2921 
2922 			/*
2923 			 * If the target process record shows that no PMCs are
2924 			 * attached to it, reclaim its space.
2925 			 */
2926 			if (pp->pp_refcnt == 0) {
2927 				pmc_remove_process_descriptor(pp);
2928 				pmc_destroy_process_descriptor(pp);
2929 			}
2930 		}
2931 
2932 		cpu = curthread->td_oncpu; /* setup cpu for pmd_release() */
2933 	}
2934 
2935 	/*
2936 	 * Release any MD resources.
2937 	 */
2938 	(void)pcd->pcd_release_pmc(cpu, adjri, pm);
2939 
2940 	/*
2941 	 * Update row disposition.
2942 	 */
2943 	if (PMC_IS_SYSTEM_MODE(PMC_TO_MODE(pm)))
2944 		PMC_UNMARK_ROW_STANDALONE(ri);
2945 	else
2946 		PMC_UNMARK_ROW_THREAD(ri);
2947 
2948 	/* Unlink from the owner's list. */
2949 	if (pm->pm_owner != NULL) {
2950 		LIST_REMOVE(pm, pm_next);
2951 		pm->pm_owner = NULL;
2952 	}
2953 }
2954 
2955 /*
2956  * Register an owner and a pmc.
2957  */
2958 static int
2959 pmc_register_owner(struct proc *p, struct pmc *pmc)
2960 {
2961 	struct pmc_owner *po;
2962 
2963 	sx_assert(&pmc_sx, SX_XLOCKED);
2964 
2965 	if ((po = pmc_find_owner_descriptor(p)) == NULL) {
2966 		if ((po = pmc_allocate_owner_descriptor(p)) == NULL)
2967 			return (ENOMEM);
2968 	}
2969 
2970 	KASSERT(pmc->pm_owner == NULL,
2971 	    ("[pmc,%d] attempting to own an initialized PMC", __LINE__));
2972 	pmc->pm_owner = po;
2973 
2974 	LIST_INSERT_HEAD(&po->po_pmcs, pmc, pm_next);
2975 
2976 	PROC_LOCK(p);
2977 	p->p_flag |= P_HWPMC;
2978 	PROC_UNLOCK(p);
2979 
2980 	if ((po->po_flags & PMC_PO_OWNS_LOGFILE) != 0)
2981 		pmclog_process_pmcallocate(pmc);
2982 
2983 	PMCDBG2(PMC,REG,1, "register-owner pmc-owner=%p pmc=%p",
2984 	    po, pmc);
2985 
2986 	return (0);
2987 }
2988 
2989 /*
2990  * Return the current row disposition:
2991  * == 0 => FREE
2992  *  > 0 => PROCESS MODE
2993  *  < 0 => SYSTEM MODE
2994  */
2995 int
2996 pmc_getrowdisp(int ri)
2997 {
2998 	return (pmc_pmcdisp[ri]);
2999 }
3000 
3001 /*
3002  * Check if a PMC at row index 'ri' can be allocated to the current
3003  * process.
3004  *
3005  * Allocation can fail if:
3006  *   - the current process is already being profiled by a PMC at index 'ri',
3007  *     attached to it via OP_PMCATTACH.
3008  *   - the current process has already allocated a PMC at index 'ri'
3009  *     via OP_ALLOCATE.
3010  */
3011 static bool
3012 pmc_can_allocate_rowindex(struct proc *p, unsigned int ri, int cpu)
3013 {
3014 	struct pmc *pm;
3015 	struct pmc_owner *po;
3016 	struct pmc_process *pp;
3017 	enum pmc_mode mode;
3018 
3019 	PMCDBG5(PMC,ALR,1, "can-allocate-rowindex proc=%p (%d, %s) ri=%d "
3020 	    "cpu=%d", p, p->p_pid, p->p_comm, ri, cpu);
3021 
3022 	/*
3023 	 * We shouldn't have already allocated a process-mode PMC at
3024 	 * row index 'ri'.
3025 	 *
3026 	 * We shouldn't have allocated a system-wide PMC on the same
3027 	 * CPU and same RI.
3028 	 */
3029 	if ((po = pmc_find_owner_descriptor(p)) != NULL) {
3030 		LIST_FOREACH(pm, &po->po_pmcs, pm_next) {
3031 			if (PMC_TO_ROWINDEX(pm) == ri) {
3032 				mode = PMC_TO_MODE(pm);
3033 				if (PMC_IS_VIRTUAL_MODE(mode))
3034 					return (false);
3035 				if (PMC_IS_SYSTEM_MODE(mode) &&
3036 				    PMC_TO_CPU(pm) == cpu)
3037 					return (false);
3038 			}
3039 		}
3040 	}
3041 
3042 	/*
3043 	 * We also shouldn't be the target of any PMC at this index
3044 	 * since otherwise a PMC_ATTACH to ourselves will fail.
3045 	 */
3046 	if ((pp = pmc_find_process_descriptor(p, 0)) != NULL)
3047 		if (pp->pp_pmcs[ri].pp_pmc != NULL)
3048 			return (false);
3049 
3050 	PMCDBG4(PMC,ALR,2, "can-allocate-rowindex proc=%p (%d, %s) ri=%d ok",
3051 	    p, p->p_pid, p->p_comm, ri);
3052 	return (true);
3053 }
3054 
3055 /*
3056  * Check if a given PMC at row index 'ri' can be currently used in
3057  * mode 'mode'.
3058  */
3059 static bool
3060 pmc_can_allocate_row(int ri, enum pmc_mode mode)
3061 {
3062 	enum pmc_disp disp;
3063 
3064 	sx_assert(&pmc_sx, SX_XLOCKED);
3065 
3066 	PMCDBG2(PMC,ALR,1, "can-allocate-row ri=%d mode=%d", ri, mode);
3067 
3068 	if (PMC_IS_SYSTEM_MODE(mode))
3069 		disp = PMC_DISP_STANDALONE;
3070 	else
3071 		disp = PMC_DISP_THREAD;
3072 
3073 	/*
3074 	 * check disposition for PMC row 'ri':
3075 	 *
3076 	 * Expected disposition		Row-disposition		Result
3077 	 *
3078 	 * STANDALONE			STANDALONE or FREE	proceed
3079 	 * STANDALONE			THREAD			fail
3080 	 * THREAD			THREAD or FREE		proceed
3081 	 * THREAD			STANDALONE		fail
3082 	 */
3083 	if (!PMC_ROW_DISP_IS_FREE(ri) &&
3084 	    !(disp == PMC_DISP_THREAD && PMC_ROW_DISP_IS_THREAD(ri)) &&
3085 	    !(disp == PMC_DISP_STANDALONE && PMC_ROW_DISP_IS_STANDALONE(ri)))
3086 		return (false);
3087 
3088 	/*
3089 	 * All OK
3090 	 */
3091 	PMCDBG2(PMC,ALR,2, "can-allocate-row ri=%d mode=%d ok", ri, mode);
3092 	return (true);
3093 }
3094 
3095 /*
3096  * Find a PMC descriptor with user handle 'pmcid' for thread 'td'.
3097  */
3098 static struct pmc *
3099 pmc_find_pmc_descriptor_in_process(struct pmc_owner *po, pmc_id_t pmcid)
3100 {
3101 	struct pmc *pm;
3102 
3103 	KASSERT(PMC_ID_TO_ROWINDEX(pmcid) < md->pmd_npmc,
3104 	    ("[pmc,%d] Illegal pmc index %d (max %d)", __LINE__,
3105 	    PMC_ID_TO_ROWINDEX(pmcid), md->pmd_npmc));
3106 
3107 	LIST_FOREACH(pm, &po->po_pmcs, pm_next) {
3108 		if (pm->pm_id == pmcid)
3109 			return (pm);
3110 	}
3111 
3112 	return (NULL);
3113 }
3114 
3115 static int
3116 pmc_find_pmc(pmc_id_t pmcid, struct pmc **pmc)
3117 {
3118 	struct pmc *pm, *opm;
3119 	struct pmc_owner *po;
3120 	struct pmc_process *pp;
3121 
3122 	PMCDBG1(PMC,FND,1, "find-pmc id=%d", pmcid);
3123 	if (PMC_ID_TO_ROWINDEX(pmcid) >= md->pmd_npmc)
3124 		return (EINVAL);
3125 
3126 	if ((po = pmc_find_owner_descriptor(curthread->td_proc)) == NULL) {
3127 		/*
3128 		 * In case of PMC_F_DESCENDANTS child processes we will not find
3129 		 * the current process in the owners hash list.  Find the owner
3130 		 * process first and from there lookup the po.
3131 		 */
3132 		pp = pmc_find_process_descriptor(curthread->td_proc,
3133 		    PMC_FLAG_NONE);
3134 		if (pp == NULL)
3135 			return (ESRCH);
3136 		opm = pp->pp_pmcs[PMC_ID_TO_ROWINDEX(pmcid)].pp_pmc;
3137 		if (opm == NULL)
3138 			return (ESRCH);
3139 		if ((opm->pm_flags &
3140 		    (PMC_F_ATTACHED_TO_OWNER | PMC_F_DESCENDANTS)) !=
3141 		    (PMC_F_ATTACHED_TO_OWNER | PMC_F_DESCENDANTS))
3142 			return (ESRCH);
3143 
3144 		po = opm->pm_owner;
3145 	}
3146 
3147 	if ((pm = pmc_find_pmc_descriptor_in_process(po, pmcid)) == NULL)
3148 		return (EINVAL);
3149 
3150 	PMCDBG2(PMC,FND,2, "find-pmc id=%d -> pmc=%p", pmcid, pm);
3151 
3152 	*pmc = pm;
3153 	return (0);
3154 }
3155 
3156 /*
3157  * Start a PMC.
3158  */
3159 static int
3160 pmc_start(struct pmc *pm)
3161 {
3162 	struct pmc_binding pb;
3163 	struct pmc_classdep *pcd;
3164 	struct pmc_owner *po;
3165 	pmc_value_t v;
3166 	enum pmc_mode mode;
3167 	int adjri, error, cpu, ri;
3168 
3169 	KASSERT(pm != NULL,
3170 	    ("[pmc,%d] null pm", __LINE__));
3171 
3172 	mode = PMC_TO_MODE(pm);
3173 	ri   = PMC_TO_ROWINDEX(pm);
3174 	pcd  = pmc_ri_to_classdep(md, ri, &adjri);
3175 
3176 	error = 0;
3177 	po = pm->pm_owner;
3178 
3179 	PMCDBG3(PMC,OPS,1, "start pmc=%p mode=%d ri=%d", pm, mode, ri);
3180 
3181 	po = pm->pm_owner;
3182 
3183 	/*
3184 	 * Disallow PMCSTART if a logfile is required but has not been
3185 	 * configured yet.
3186 	 */
3187 	if ((pm->pm_flags & PMC_F_NEEDS_LOGFILE) != 0 &&
3188 	    (po->po_flags & PMC_PO_OWNS_LOGFILE) == 0)
3189 		return (EDOOFUS);	/* programming error */
3190 
3191 	/*
3192 	 * If this is a sampling mode PMC, log mapping information for
3193 	 * the kernel modules that are currently loaded.
3194 	 */
3195 	if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
3196 		pmc_log_kernel_mappings(pm);
3197 
3198 	if (PMC_IS_VIRTUAL_MODE(mode)) {
3199 		/*
3200 		 * If a PMCATTACH has never been done on this PMC,
3201 		 * attach it to its owner process.
3202 		 */
3203 		if (LIST_EMPTY(&pm->pm_targets)) {
3204 			error = (pm->pm_flags & PMC_F_ATTACH_DONE) != 0 ?
3205 			    ESRCH : pmc_attach_process(po->po_owner, pm);
3206 		}
3207 
3208 		/*
3209 		 * If the PMC is attached to its owner, then force a context
3210 		 * switch to ensure that the MD state gets set correctly.
3211 		 */
3212 		if (error == 0) {
3213 			pm->pm_state = PMC_STATE_RUNNING;
3214 			if ((pm->pm_flags & PMC_F_ATTACHED_TO_OWNER) != 0)
3215 				pmc_force_context_switch();
3216 		}
3217 
3218 		return (error);
3219 	}
3220 
3221 	/*
3222 	 * A system-wide PMC.
3223 	 *
3224 	 * Add the owner to the global list if this is a system-wide
3225 	 * sampling PMC.
3226 	 */
3227 	if (mode == PMC_MODE_SS) {
3228 		/*
3229 		 * Log mapping information for all existing processes in the
3230 		 * system.  Subsequent mappings are logged as they happen;
3231 		 * see pmc_process_mmap().
3232 		 */
3233 		if (po->po_logprocmaps == 0) {
3234 			pmc_log_all_process_mappings(po);
3235 			po->po_logprocmaps = 1;
3236 		}
3237 		po->po_sscount++;
3238 		if (po->po_sscount == 1) {
3239 			atomic_add_rel_int(&pmc_ss_count, 1);
3240 			CK_LIST_INSERT_HEAD(&pmc_ss_owners, po, po_ssnext);
3241 			PMCDBG1(PMC,OPS,1, "po=%p in global list", po);
3242 		}
3243 	}
3244 
3245 	/*
3246 	 * Move to the CPU associated with this
3247 	 * PMC, and start the hardware.
3248 	 */
3249 	pmc_save_cpu_binding(&pb);
3250 	cpu = PMC_TO_CPU(pm);
3251 	if (!pmc_cpu_is_active(cpu)) {
3252 		return (EXTERROR(ENXIO, "PMC CPU %ju is not active for start",
3253 		    (uintmax_t)cpu));
3254 	}
3255 	pmc_select_cpu(cpu);
3256 
3257 	/*
3258 	 * global PMCs are configured at allocation time
3259 	 * so write out the initial value and start the PMC.
3260 	 */
3261 	pm->pm_state = PMC_STATE_RUNNING;
3262 
3263 	critical_enter();
3264 	v = PMC_IS_SAMPLING_MODE(mode) ? pm->pm_sc.pm_reloadcount :
3265 	    pm->pm_sc.pm_initial;
3266 	if ((error = pcd->pcd_write_pmc(cpu, adjri, pm, v)) == 0) {
3267 		/* If a sampling mode PMC, reset stalled state. */
3268 		if (PMC_IS_SAMPLING_MODE(mode))
3269 			pm->pm_pcpu_state[cpu].pps_stalled = 0;
3270 
3271 		/* Indicate that we desire this to run. Start it. */
3272 		pm->pm_pcpu_state[cpu].pps_cpustate = 1;
3273 		error = pcd->pcd_start_pmc(cpu, adjri, pm);
3274 	}
3275 	critical_exit();
3276 
3277 	pmc_restore_cpu_binding(&pb);
3278 	return (error);
3279 }
3280 
3281 /*
3282  * Stop a PMC.
3283  */
3284 static int
3285 pmc_stop(struct pmc *pm)
3286 {
3287 	struct pmc_binding pb;
3288 	struct pmc_classdep *pcd;
3289 	struct pmc_owner *po;
3290 	int adjri, cpu, error, ri;
3291 
3292 	KASSERT(pm != NULL, ("[pmc,%d] null pmc", __LINE__));
3293 
3294 	PMCDBG3(PMC,OPS,1, "stop pmc=%p mode=%d ri=%d", pm, PMC_TO_MODE(pm),
3295 	    PMC_TO_ROWINDEX(pm));
3296 
3297 	pm->pm_state = PMC_STATE_STOPPED;
3298 
3299 	/*
3300 	 * If the PMC is a virtual mode one, changing the state to non-RUNNING
3301 	 * is enough to ensure that the PMC never gets scheduled.
3302 	 *
3303 	 * If this PMC is current running on a CPU, then it will handled
3304 	 * correctly at the time its target process is context switched out.
3305 	 */
3306 	if (PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm)))
3307 		return (0);
3308 
3309 	/*
3310 	 * A system-mode PMC. Move to the CPU associated with this PMC, and
3311 	 * stop the hardware. We update the 'initial count' so that a
3312 	 * subsequent PMCSTART will resume counting from the current hardware
3313 	 * count.
3314 	 */
3315 	pmc_save_cpu_binding(&pb);
3316 
3317 	cpu = PMC_TO_CPU(pm);
3318 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
3319 	    ("[pmc,%d] illegal cpu=%d", __LINE__, cpu));
3320 	if (!pmc_cpu_is_active(cpu)) {
3321 		return (EXTERROR(ENXIO, "PMC CPU %ju is not active for stop",
3322 		    (uintmax_t)cpu));
3323 	}
3324 	pmc_select_cpu(cpu);
3325 
3326 	ri = PMC_TO_ROWINDEX(pm);
3327 	pcd = pmc_ri_to_classdep(md, ri, &adjri);
3328 
3329 	pm->pm_pcpu_state[cpu].pps_cpustate = 0;
3330 	critical_enter();
3331 	if ((error = pcd->pcd_stop_pmc(cpu, adjri, pm)) == 0) {
3332 		error = pcd->pcd_read_pmc(cpu, adjri, pm,
3333 		    &pm->pm_sc.pm_initial);
3334 	}
3335 	critical_exit();
3336 
3337 	pmc_restore_cpu_binding(&pb);
3338 
3339 	/* Remove this owner from the global list of SS PMC owners. */
3340 	po = pm->pm_owner;
3341 	if (PMC_TO_MODE(pm) == PMC_MODE_SS) {
3342 		po->po_sscount--;
3343 		if (po->po_sscount == 0) {
3344 			atomic_subtract_rel_int(&pmc_ss_count, 1);
3345 			CK_LIST_REMOVE(po, po_ssnext);
3346 			epoch_wait_preempt(global_epoch_preempt);
3347 			PMCDBG1(PMC,OPS,2,"po=%p removed from global list", po);
3348 		}
3349 	}
3350 
3351 	return (error);
3352 }
3353 
3354 static struct pmc_classdep *
3355 pmc_class_to_classdep(enum pmc_class class)
3356 {
3357 	int n;
3358 
3359 	for (n = 0; n < md->pmd_nclass; n++) {
3360 		if (md->pmd_classdep[n].pcd_class == class)
3361 			return (&md->pmd_classdep[n]);
3362 	}
3363 	return (NULL);
3364 }
3365 
3366 #if defined(HWPMC_DEBUG) && defined(KTR)
3367 static const char *pmc_op_to_name[] = {
3368 #undef	__PMC_OP
3369 #define	__PMC_OP(N, D)	#N ,
3370 	__PMC_OPS()
3371 	NULL
3372 };
3373 #endif
3374 
3375 /*
3376  * The syscall interface
3377  */
3378 
3379 #define	PMC_GET_SX_XLOCK(...) do {		\
3380 	sx_xlock(&pmc_sx);			\
3381 	if (pmc_hook == NULL) {			\
3382 		sx_xunlock(&pmc_sx);		\
3383 		return __VA_ARGS__;		\
3384 	}					\
3385 } while (0)
3386 
3387 #define	PMC_DOWNGRADE_SX() do {			\
3388 	sx_downgrade(&pmc_sx);			\
3389 	is_sx_downgraded = true;		\
3390 } while (0)
3391 
3392 /*
3393  * Main body of PMC_OP_PMCALLOCATE.
3394  */
3395 static int
3396 pmc_do_op_pmcallocate(struct thread *td, struct pmc_op_pmcallocate *pa)
3397 {
3398 	struct proc *p;
3399 	struct pmc *pmc;
3400 	struct pmc_binding pb;
3401 	struct pmc_classdep *pcd;
3402 	struct pmc_hw *phw;
3403 	enum pmc_mode mode;
3404 	enum pmc_class class;
3405 	uint32_t caps, flags;
3406 	u_int cpu;
3407 	int adjri, n;
3408 	int error;
3409 
3410 	class = pa->pm_class;
3411 	caps  = pa->pm_caps;
3412 	flags = pa->pm_flags;
3413 	mode  = pa->pm_mode;
3414 	cpu   = pa->pm_cpu;
3415 
3416 	p = td->td_proc;
3417 	/* Requested mode must exist. */
3418 	if ((mode != PMC_MODE_SS && mode != PMC_MODE_SC &&
3419 	     mode != PMC_MODE_TS && mode != PMC_MODE_TC))
3420 		return (EXTERROR(EINVAL, "Invalid PMC mode %ju",
3421 		    (uintmax_t)mode));
3422 
3423 	/* Requested CPU must be valid. */
3424 	if (cpu != PMC_CPU_ANY && cpu >= pmc_cpu_max())
3425 		return (EXTERROR(EINVAL, "Invalid PMC CPU %ju",
3426 		    (uintmax_t)cpu));
3427 
3428 	/*
3429 	 * Virtual PMCs should only ask for a default CPU.
3430 	 * System mode PMCs need to specify a non-default CPU.
3431 	 */
3432 	if ((PMC_IS_VIRTUAL_MODE(mode) && cpu != PMC_CPU_ANY) ||
3433 	    (PMC_IS_SYSTEM_MODE(mode) && cpu == PMC_CPU_ANY)) {
3434 		if (PMC_IS_VIRTUAL_MODE(mode)) {
3435 			return (EXTERROR(EINVAL,
3436 			    "PMC mode %ju requires the default CPU",
3437 			    (uintmax_t)mode));
3438 		}
3439 		return (EXTERROR(EINVAL,
3440 		    "PMC mode %ju requires an explicit CPU",
3441 		    (uintmax_t)mode));
3442 	}
3443 
3444 	/*
3445 	 * Check that an inactive CPU is not being asked for.
3446 	 */
3447 	if (PMC_IS_SYSTEM_MODE(mode) && !pmc_cpu_is_active(cpu))
3448 		return (EXTERROR(ENXIO, "PMC CPU %ju is not active",
3449 		    (uintmax_t)cpu));
3450 
3451 	/*
3452 	 * Refuse an allocation for a system-wide PMC if this process has been
3453 	 * jailed, or if this process lacks super-user credentials and the
3454 	 * sysctl tunable 'security.bsd.unprivileged_syspmcs' is zero.
3455 	 */
3456 	if (PMC_IS_SYSTEM_MODE(mode)) {
3457 		if (jailed(td->td_ucred))
3458 			return (EPERM);
3459 		if (!pmc_unprivileged_syspmcs) {
3460 			error = priv_check(td, PRIV_PMC_SYSTEM);
3461 			if (error != 0)
3462 				return (error);
3463 		}
3464 	}
3465 
3466 	/*
3467 	 * Look for valid values for 'pm_flags'.
3468 	 */
3469 	if ((flags & ~(PMC_F_DESCENDANTS | PMC_F_LOG_PROCCSW |
3470 	    PMC_F_LOG_PROCEXIT | PMC_F_CALLCHAIN | PMC_F_USERCALLCHAIN |
3471 	    PMC_F_EV_PMU)) != 0)
3472 		return (EXTERROR(EINVAL, "Invalid PMC flags %#jx",
3473 		    (uintmax_t)flags));
3474 
3475 	/* PMC_F_USERCALLCHAIN is only valid with PMC_F_CALLCHAIN. */
3476 	if ((flags & (PMC_F_CALLCHAIN | PMC_F_USERCALLCHAIN)) ==
3477 	    PMC_F_USERCALLCHAIN)
3478 		return (EXTERROR(EINVAL,
3479 		    "PMC_F_USERCALLCHAIN requires PMC_F_CALLCHAIN"));
3480 
3481 	/* PMC_F_USERCALLCHAIN is only valid for sampling mode. */
3482 	if ((flags & PMC_F_USERCALLCHAIN) != 0 && mode != PMC_MODE_TS &&
3483 	    mode != PMC_MODE_SS)
3484 		return (EXTERROR(EINVAL,
3485 		    "PMC_F_USERCALLCHAIN requires sampling mode"));
3486 
3487 	/* Process logging options are not allowed for system PMCs. */
3488 	if (PMC_IS_SYSTEM_MODE(mode) &&
3489 	    (flags & (PMC_F_LOG_PROCCSW | PMC_F_LOG_PROCEXIT)) != 0)
3490 		return (EXTERROR(EINVAL,
3491 		    "Process logging flags are not valid for system PMCs"));
3492 
3493 	/*
3494 	 * All sampling mode PMCs need to be able to interrupt the CPU.
3495 	 */
3496 	if (PMC_IS_SAMPLING_MODE(mode))
3497 		caps |= PMC_CAP_INTERRUPT;
3498 
3499 	/* A valid class specifier should have been passed in. */
3500 	pcd = pmc_class_to_classdep(class);
3501 	if (pcd == NULL)
3502 		return (EXTERROR(EINVAL, "Invalid PMC class %ju",
3503 		    (uintmax_t)class));
3504 
3505 	/* The requested PMC capabilities should be feasible. */
3506 	if ((pcd->pcd_caps & caps) != caps)
3507 		return (EXTERROR(EOPNOTSUPP,
3508 		    "Requested PMC capabilities %#jx are not supported",
3509 		    (uintmax_t)caps));
3510 
3511 	PMCDBG4(PMC,ALL,2, "event=%d caps=0x%x mode=%d cpu=%d", pa->pm_ev,
3512 	    caps, mode, cpu);
3513 
3514 	pmc = pmc_allocate_pmc_descriptor();
3515 	pmc->pm_id    = PMC_ID_MAKE_ID(cpu, pa->pm_mode, class, PMC_ID_INVALID);
3516 	pmc->pm_event = pa->pm_ev;
3517 	pmc->pm_state = PMC_STATE_FREE;
3518 	pmc->pm_caps  = caps;
3519 	pmc->pm_flags = flags;
3520 
3521 	/* XXX set lower bound on sampling for process counters */
3522 	if (PMC_IS_SAMPLING_MODE(mode)) {
3523 		/*
3524 		 * Don't permit requested sample rate to be less than
3525 		 * pmc_mincount.
3526 		 */
3527 		if (pa->pm_count < MAX(1, pmc_mincount))
3528 			log(LOG_WARNING, "pmcallocate: passed sample "
3529 			    "rate %ju - setting to %u\n",
3530 			    (uintmax_t)pa->pm_count,
3531 			    MAX(1, pmc_mincount));
3532 		pmc->pm_sc.pm_reloadcount = MAX(MAX(1, pmc_mincount),
3533 		    pa->pm_count);
3534 	} else
3535 		pmc->pm_sc.pm_initial = pa->pm_count;
3536 
3537 	/* switch thread to CPU 'cpu' */
3538 	pmc_save_cpu_binding(&pb);
3539 
3540 #define	PMC_IS_SHAREABLE_PMC(cpu, n)				\
3541 	(pmc_pcpu[(cpu)]->pc_hwpmcs[(n)]->phw_state &		\
3542 	 PMC_PHW_FLAG_IS_SHAREABLE)
3543 #define	PMC_IS_UNALLOCATED(cpu, n)				\
3544 	(pmc_pcpu[(cpu)]->pc_hwpmcs[(n)]->phw_pmc == NULL)
3545 
3546 	if (PMC_IS_SYSTEM_MODE(mode)) {
3547 		pmc_select_cpu(cpu);
3548 		for (n = pcd->pcd_ri; n < md->pmd_npmc; n++) {
3549 			pcd = pmc_ri_to_classdep(md, n, &adjri);
3550 
3551 			if (!pmc_can_allocate_row(n, mode) ||
3552 			    !pmc_can_allocate_rowindex(p, n, cpu))
3553 				continue;
3554 			if (!PMC_IS_UNALLOCATED(cpu, n) &&
3555 			    !PMC_IS_SHAREABLE_PMC(cpu, n))
3556 				continue;
3557 
3558 			if (pcd->pcd_allocate_pmc(cpu, adjri, pmc, pa) == 0) {
3559 				/* Success. */
3560 				break;
3561 			}
3562 		}
3563 	} else {
3564 		/* Process virtual mode */
3565 		for (n = pcd->pcd_ri; n < md->pmd_npmc; n++) {
3566 			pcd = pmc_ri_to_classdep(md, n, &adjri);
3567 
3568 			if (!pmc_can_allocate_row(n, mode) ||
3569 			    !pmc_can_allocate_rowindex(p, n, PMC_CPU_ANY))
3570 				continue;
3571 
3572 			if (pcd->pcd_allocate_pmc(td->td_oncpu, adjri, pmc,
3573 			    pa) == 0) {
3574 				/* Success. */
3575 				break;
3576 			}
3577 		}
3578 	}
3579 
3580 #undef	PMC_IS_UNALLOCATED
3581 #undef	PMC_IS_SHAREABLE_PMC
3582 
3583 	pmc_restore_cpu_binding(&pb);
3584 
3585 	if (n == md->pmd_npmc) {
3586 		pmc_destroy_pmc_descriptor(pmc);
3587 		/* Preserve a more specific error from the class allocator. */
3588 		if ((td->td_pflags2 & TDP2_EXTERR) != 0)
3589 			return (EINVAL);
3590 		return (EXTERROR(EINVAL,
3591 		    "No PMC row accepted the allocation request"));
3592 	}
3593 
3594 	/* Fill in the correct value in the ID field. */
3595 	pmc->pm_id = PMC_ID_MAKE_ID(cpu, mode, class, n);
3596 
3597 	PMCDBG5(PMC,ALL,2, "ev=%d class=%d mode=%d n=%d -> pmcid=%x",
3598 	    pmc->pm_event, class, mode, n, pmc->pm_id);
3599 
3600 	/* Process mode PMCs with logging enabled need log files. */
3601 	if ((pmc->pm_flags & (PMC_F_LOG_PROCEXIT | PMC_F_LOG_PROCCSW)) != 0)
3602 		pmc->pm_flags |= PMC_F_NEEDS_LOGFILE;
3603 
3604 	/* All system mode sampling PMCs require a log file. */
3605 	if (PMC_IS_SAMPLING_MODE(mode) && PMC_IS_SYSTEM_MODE(mode))
3606 		pmc->pm_flags |= PMC_F_NEEDS_LOGFILE;
3607 
3608 	/*
3609 	 * Configure global pmc's immediately.
3610 	 */
3611 	if (PMC_IS_SYSTEM_MODE(PMC_TO_MODE(pmc))) {
3612 		pmc_save_cpu_binding(&pb);
3613 		pmc_select_cpu(cpu);
3614 
3615 		phw = pmc_pcpu[cpu]->pc_hwpmcs[n];
3616 		pcd = pmc_ri_to_classdep(md, n, &adjri);
3617 
3618 		if ((phw->phw_state & PMC_PHW_FLAG_IS_ENABLED) == 0) {
3619 			(void)pcd->pcd_release_pmc(cpu, adjri, pmc);
3620 			pmc_destroy_pmc_descriptor(pmc);
3621 			pmc_restore_cpu_binding(&pb);
3622 			return (EXTERROR(EPERM,
3623 			    "PMC row %ju on CPU %ju is not enabled",
3624 			    (uintmax_t)n, (uintmax_t)cpu));
3625 		}
3626 		if ((error = pcd->pcd_config_pmc(cpu, adjri, pmc)) != 0) {
3627 			(void)pcd->pcd_release_pmc(cpu, adjri, pmc);
3628 			pmc_destroy_pmc_descriptor(pmc);
3629 			pmc_restore_cpu_binding(&pb);
3630 			return (EXTERROR(EPERM,
3631 			    "PMC configuration failed for row %ju on CPU %ju",
3632 			    (uintmax_t)n, (uintmax_t)cpu));
3633 		}
3634 
3635 		pmc_restore_cpu_binding(&pb);
3636 	}
3637 
3638 	pmc->pm_state = PMC_STATE_ALLOCATED;
3639 	pmc->pm_class = class;
3640 
3641 	/*
3642 	 * Mark row disposition.
3643 	 */
3644 	if (PMC_IS_SYSTEM_MODE(mode))
3645 		PMC_MARK_ROW_STANDALONE(n);
3646 	else
3647 		PMC_MARK_ROW_THREAD(n);
3648 
3649 	/*
3650 	 * Register this PMC with the current thread as its owner.
3651 	 */
3652 	error = pmc_register_owner(p, pmc);
3653 	if (error != 0) {
3654 		pmc_release_pmc_descriptor(pmc);
3655 		pmc_destroy_pmc_descriptor(pmc);
3656 		return (EXTERROR(error, "Failed to register PMC owner"));
3657 	}
3658 
3659 	/*
3660 	 * Return the allocated index.
3661 	 */
3662 	pa->pm_pmcid = pmc->pm_id;
3663 	return (0);
3664 }
3665 
3666 /*
3667  * Main body of PMC_OP_PMCATTACH.
3668  */
3669 static int
3670 pmc_do_op_pmcattach(struct thread *td, struct pmc_op_pmcattach a)
3671 {
3672 	struct pmc *pm;
3673 	struct proc *p;
3674 	int error;
3675 
3676 	sx_assert(&pmc_sx, SX_XLOCKED);
3677 
3678 	if (a.pm_pid < 0) {
3679 		return (EXTERROR(EINVAL, "Invalid PMC attach pid %jd",
3680 		    (intmax_t)a.pm_pid));
3681 	} else if (a.pm_pid == 0) {
3682 		a.pm_pid = td->td_proc->p_pid;
3683 	}
3684 
3685 	error = pmc_find_pmc(a.pm_pmc, &pm);
3686 	if (error != 0)
3687 		return (error);
3688 
3689 	if (PMC_IS_SYSTEM_MODE(PMC_TO_MODE(pm)))
3690 		return (EXTERROR(EINVAL,
3691 		    "Cannot attach a system-mode PMC to a process"));
3692 
3693 	/* PMCs may be (re)attached only when allocated or stopped */
3694 	if (pm->pm_state == PMC_STATE_RUNNING) {
3695 		return (EXTERROR(EBUSY,
3696 		    "PMC must be stopped before attach"));
3697 	} else if (pm->pm_state != PMC_STATE_ALLOCATED &&
3698 	    pm->pm_state != PMC_STATE_STOPPED) {
3699 		return (EXTERROR(EINVAL,
3700 		    "PMC state %ju does not allow attach",
3701 		    (uintmax_t)pm->pm_state));
3702 	}
3703 
3704 	/* lookup pid */
3705 	if ((p = pfind(a.pm_pid)) == NULL)
3706 		return (ESRCH);
3707 
3708 	/*
3709 	 * Ignore processes that are working on exiting.
3710 	 */
3711 	if ((p->p_flag & P_WEXIT) != 0) {
3712 		PROC_UNLOCK(p);	/* pfind() returns a locked process */
3713 		return (ESRCH);
3714 	}
3715 
3716 	/*
3717 	 * We are allowed to attach a PMC to a process if we can debug it.
3718 	 */
3719 	error = p_candebug(curthread, p);
3720 
3721 	PROC_UNLOCK(p);
3722 
3723 	if (error == 0)
3724 		error = pmc_attach_process(p, pm);
3725 
3726 	return (error);
3727 }
3728 
3729 /*
3730  * Main body of PMC_OP_PMCDETACH.
3731  */
3732 static int
3733 pmc_do_op_pmcdetach(struct thread *td, struct pmc_op_pmcattach a)
3734 {
3735 	struct pmc *pm;
3736 	struct proc *p;
3737 	int error;
3738 
3739 	if (a.pm_pid < 0) {
3740 		return (EXTERROR(EINVAL, "Invalid PMC detach pid %jd",
3741 		    (intmax_t)a.pm_pid));
3742 	} else if (a.pm_pid == 0)
3743 		a.pm_pid = td->td_proc->p_pid;
3744 
3745 	error = pmc_find_pmc(a.pm_pmc, &pm);
3746 	if (error != 0)
3747 		return (error);
3748 
3749 	if ((p = pfind(a.pm_pid)) == NULL)
3750 		return (ESRCH);
3751 
3752 	/*
3753 	 * Treat processes that are in the process of exiting as if they were
3754 	 * not present.
3755 	 */
3756 	if ((p->p_flag & P_WEXIT) != 0) {
3757 		PROC_UNLOCK(p);
3758 		return (ESRCH);
3759 	}
3760 
3761 	PROC_UNLOCK(p);	/* pfind() returns a locked process */
3762 
3763 	if (error == 0)
3764 		error = pmc_detach_process(p, pm);
3765 
3766 	return (error);
3767 }
3768 
3769 /*
3770  * Main body of PMC_OP_PMCRELEASE.
3771  */
3772 static int
3773 pmc_do_op_pmcrelease(pmc_id_t pmcid)
3774 {
3775 	struct pmc_owner *po;
3776 	struct pmc *pm;
3777 	int error;
3778 
3779 	/*
3780 	 * Find PMC pointer for the named PMC.
3781 	 *
3782 	 * Use pmc_release_pmc_descriptor() to switch off the
3783 	 * PMC, remove all its target threads, and remove the
3784 	 * PMC from its owner's list.
3785 	 *
3786 	 * Remove the owner record if this is the last PMC
3787 	 * owned.
3788 	 *
3789 	 * Free up space.
3790 	 */
3791 	error = pmc_find_pmc(pmcid, &pm);
3792 	if (error != 0)
3793 		return (error);
3794 
3795 	po = pm->pm_owner;
3796 	pmc_release_pmc_descriptor(pm);
3797 	pmc_maybe_remove_owner(po);
3798 	pmc_destroy_pmc_descriptor(pm);
3799 
3800 	return (error);
3801 }
3802 
3803 /*
3804  * Main body of PMC_OP_PMCRW.
3805  */
3806 static int
3807 pmc_do_op_pmcrw(const struct pmc_op_pmcrw *prw, pmc_value_t *valp)
3808 {
3809 	struct pmc_binding pb;
3810 	struct pmc_classdep *pcd;
3811 	struct pmc *pm;
3812 	u_int cpu, ri, adjri;
3813 	int error;
3814 
3815 	PMCDBG2(PMC,OPS,1, "rw id=%d flags=0x%x", prw->pm_pmcid, prw->pm_flags);
3816 
3817 	/* Must have at least one flag set. */
3818 	if ((prw->pm_flags & (PMC_F_OLDVALUE | PMC_F_NEWVALUE)) == 0)
3819 		return (EXTERROR(EINVAL,
3820 		    "PMCRW requires PMC_F_OLDVALUE and/or PMC_F_NEWVALUE"));
3821 
3822 	/* Locate PMC descriptor. */
3823 	error = pmc_find_pmc(prw->pm_pmcid, &pm);
3824 	if (error != 0)
3825 		return (error);
3826 
3827 	/* Can't read a PMC that hasn't been started. */
3828 	if (pm->pm_state != PMC_STATE_ALLOCATED &&
3829 	    pm->pm_state != PMC_STATE_STOPPED &&
3830 	    pm->pm_state != PMC_STATE_RUNNING)
3831 		return (EXTERROR(EINVAL,
3832 		    "PMC state %ju does not allow read/write",
3833 		    (uintmax_t)pm->pm_state));
3834 
3835 	/* Writing a new value is allowed only for 'STOPPED' PMCs. */
3836 	if (pm->pm_state == PMC_STATE_RUNNING &&
3837 	    (prw->pm_flags & PMC_F_NEWVALUE) != 0)
3838 		return (EXTERROR(EBUSY,
3839 		    "Cannot write a PMC while it is running"));
3840 
3841 	if (PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm))) {
3842 		/*
3843 		 * If this PMC is attached to its owner (i.e., the process
3844 		 * requesting this operation) and is running, then attempt to
3845 		 * get an upto-date reading from hardware for a READ. Writes
3846 		 * are only allowed when the PMC is stopped, so only update the
3847 		 * saved value field.
3848 		 *
3849 		 * If the PMC is not running, or is not attached to its owner,
3850 		 * read/write to the savedvalue field.
3851 		 */
3852 
3853 		ri = PMC_TO_ROWINDEX(pm);
3854 		pcd = pmc_ri_to_classdep(md, ri, &adjri);
3855 
3856 		mtx_pool_lock_spin(pmc_mtxpool, pm);
3857 		cpu = curthread->td_oncpu;
3858 
3859 		if ((prw->pm_flags & PMC_F_OLDVALUE) != 0) {
3860 			if ((pm->pm_flags & PMC_F_ATTACHED_TO_OWNER) &&
3861 			    (pm->pm_state == PMC_STATE_RUNNING)) {
3862 				error = (*pcd->pcd_read_pmc)(cpu, adjri, pm,
3863 				    valp);
3864 			} else {
3865 				*valp = pm->pm_gv.pm_savedvalue;
3866 			}
3867 		}
3868 
3869 		if ((prw->pm_flags & PMC_F_NEWVALUE) != 0)
3870 			pm->pm_gv.pm_savedvalue = prw->pm_value;
3871 
3872 		mtx_pool_unlock_spin(pmc_mtxpool, pm);
3873 	} else { /* System mode PMCs */
3874 		cpu = PMC_TO_CPU(pm);
3875 		ri  = PMC_TO_ROWINDEX(pm);
3876 		pcd = pmc_ri_to_classdep(md, ri, &adjri);
3877 
3878 		if (!pmc_cpu_is_active(cpu))
3879 			return (EXTERROR(ENXIO,
3880 			    "PMC CPU %ju is not active for read/write",
3881 			    (uintmax_t)cpu));
3882 
3883 		/* Move this thread to CPU 'cpu'. */
3884 		pmc_save_cpu_binding(&pb);
3885 		pmc_select_cpu(cpu);
3886 		critical_enter();
3887 
3888 		/* Save old value. */
3889 		if ((prw->pm_flags & PMC_F_OLDVALUE) != 0)
3890 			error = (*pcd->pcd_read_pmc)(cpu, adjri, pm, valp);
3891 
3892 		/* Write out new value. */
3893 		if (error == 0 && (prw->pm_flags & PMC_F_NEWVALUE) != 0)
3894 			error = (*pcd->pcd_write_pmc)(cpu, adjri, pm,
3895 			    prw->pm_value);
3896 
3897 		critical_exit();
3898 		pmc_restore_cpu_binding(&pb);
3899 		if (error != 0)
3900 			return (error);
3901 	}
3902 
3903 #ifdef HWPMC_DEBUG
3904 	if ((prw->pm_flags & PMC_F_NEWVALUE) != 0)
3905 		PMCDBG3(PMC,OPS,2, "rw id=%d new %jx -> old %jx",
3906 		    ri, prw->pm_value, *valp);
3907 	else
3908 		PMCDBG2(PMC,OPS,2, "rw id=%d -> old %jx", ri, *valp);
3909 #endif
3910 	return (error);
3911 }
3912 
3913 static int
3914 pmc_syscall_handler(struct thread *td, void *syscall_args)
3915 {
3916 	struct pmc_syscall_args *c;
3917 	void *pmclog_proc_handle;
3918 	void *arg;
3919 	int error, op;
3920 	bool is_sx_downgraded;
3921 
3922 	c = (struct pmc_syscall_args *)syscall_args;
3923 	op = c->pmop_code;
3924 	arg = c->pmop_data;
3925 
3926 	/* PMC isn't set up yet */
3927 	if (pmc_hook == NULL)
3928 		return (EINVAL);
3929 
3930 	if (op == PMC_OP_CONFIGURELOG) {
3931 		/*
3932 		 * We cannot create the logging process inside
3933 		 * pmclog_configure_log() because there is a LOR
3934 		 * between pmc_sx and process structure locks.
3935 		 * Instead, pre-create the process and ignite the loop
3936 		 * if everything is fine, otherwise direct the process
3937 		 * to exit.
3938 		 */
3939 		error = pmclog_proc_create(td, &pmclog_proc_handle);
3940 		if (error != 0)
3941 			goto done_syscall;
3942 	}
3943 
3944 	PMC_GET_SX_XLOCK(ENOSYS);
3945 	is_sx_downgraded = false;
3946 	PMCDBG3(MOD,PMS,1, "syscall op=%d \"%s\" arg=%p", op,
3947 	    pmc_op_to_name[op], arg);
3948 
3949 	error = 0;
3950 	counter_u64_add(pmc_stats.pm_syscalls, 1);
3951 
3952 	switch (op) {
3953 
3954 
3955 	/*
3956 	 * Configure a log file.
3957 	 *
3958 	 * XXX This OP will be reworked.
3959 	 */
3960 
3961 	case PMC_OP_CONFIGURELOG:
3962 	{
3963 		struct proc *p;
3964 		struct pmc *pm;
3965 		struct pmc_owner *po;
3966 		struct pmc_op_configurelog cl;
3967 
3968 		if ((error = copyin(arg, &cl, sizeof(cl))) != 0) {
3969 			pmclog_proc_ignite(pmclog_proc_handle, NULL);
3970 			break;
3971 		}
3972 
3973 		/* No flags currently implemented */
3974 		if (cl.pm_flags != 0) {
3975 			pmclog_proc_ignite(pmclog_proc_handle, NULL);
3976 			error = EINVAL;
3977 			break;
3978 		}
3979 
3980 		/* mark this process as owning a log file */
3981 		p = td->td_proc;
3982 		if ((po = pmc_find_owner_descriptor(p)) == NULL)
3983 			if ((po = pmc_allocate_owner_descriptor(p)) == NULL) {
3984 				pmclog_proc_ignite(pmclog_proc_handle, NULL);
3985 				error = ENOMEM;
3986 				break;
3987 			}
3988 
3989 		/*
3990 		 * If a valid fd was passed in, try to configure that,
3991 		 * otherwise if 'fd' was less than zero and there was
3992 		 * a log file configured, flush its buffers and
3993 		 * de-configure it.
3994 		 */
3995 		if (cl.pm_logfd >= 0) {
3996 			error = pmclog_configure_log(md, po, cl.pm_logfd);
3997 			pmclog_proc_ignite(pmclog_proc_handle, error == 0 ?
3998 			    po : NULL);
3999 		} else if (po->po_flags & PMC_PO_OWNS_LOGFILE) {
4000 			pmclog_proc_ignite(pmclog_proc_handle, NULL);
4001 			error = pmclog_close(po);
4002 			if (error == 0) {
4003 				LIST_FOREACH(pm, &po->po_pmcs, pm_next)
4004 				    if (pm->pm_flags & PMC_F_NEEDS_LOGFILE &&
4005 					pm->pm_state == PMC_STATE_RUNNING)
4006 					    pmc_stop(pm);
4007 				error = pmclog_deconfigure_log(po);
4008 			}
4009 		} else {
4010 			pmclog_proc_ignite(pmclog_proc_handle, NULL);
4011 			error = EINVAL;
4012 		}
4013 	}
4014 	break;
4015 
4016 	/*
4017 	 * Flush a log file.
4018 	 */
4019 
4020 	case PMC_OP_FLUSHLOG:
4021 	{
4022 		struct pmc_owner *po;
4023 
4024 		sx_assert(&pmc_sx, SX_XLOCKED);
4025 
4026 		if ((po = pmc_find_owner_descriptor(td->td_proc)) == NULL) {
4027 			error = EINVAL;
4028 			break;
4029 		}
4030 
4031 		error = pmclog_flush(po, 0);
4032 	}
4033 	break;
4034 
4035 	/*
4036 	 * Close a log file.
4037 	 */
4038 
4039 	case PMC_OP_CLOSELOG:
4040 	{
4041 		struct pmc_owner *po;
4042 
4043 		sx_assert(&pmc_sx, SX_XLOCKED);
4044 
4045 		if ((po = pmc_find_owner_descriptor(td->td_proc)) == NULL) {
4046 			error = EINVAL;
4047 			break;
4048 		}
4049 
4050 		error = pmclog_close(po);
4051 	}
4052 	break;
4053 
4054 	/*
4055 	 * Retrieve hardware configuration.
4056 	 */
4057 
4058 	case PMC_OP_GETCPUINFO:	/* CPU information */
4059 	{
4060 		struct pmc_op_getcpuinfo gci;
4061 		struct pmc_classinfo *pci;
4062 		struct pmc_classdep *pcd;
4063 		int cl;
4064 
4065 		memset(&gci, 0, sizeof(gci));
4066 		gci.pm_cputype = md->pmd_cputype;
4067 		gci.pm_ncpu    = pmc_cpu_max();
4068 		gci.pm_npmc    = md->pmd_npmc;
4069 		gci.pm_nclass  = md->pmd_nclass;
4070 		pci = gci.pm_classes;
4071 		pcd = md->pmd_classdep;
4072 		for (cl = 0; cl < md->pmd_nclass; cl++, pci++, pcd++) {
4073 			pci->pm_caps  = pcd->pcd_caps;
4074 			pci->pm_class = pcd->pcd_class;
4075 			pci->pm_width = pcd->pcd_width;
4076 			pci->pm_num   = pcd->pcd_num;
4077 		}
4078 		error = copyout(&gci, arg, sizeof(gci));
4079 	}
4080 	break;
4081 
4082 	/*
4083 	 * Retrieve soft events list.
4084 	 */
4085 	case PMC_OP_GETDYNEVENTINFO:
4086 	{
4087 		enum pmc_class			cl;
4088 		enum pmc_event			ev;
4089 		struct pmc_op_getdyneventinfo	*gei;
4090 		struct pmc_dyn_event_descr	dev;
4091 		struct pmc_soft			*ps;
4092 		uint32_t			nevent;
4093 
4094 		sx_assert(&pmc_sx, SX_LOCKED);
4095 
4096 		gei = (struct pmc_op_getdyneventinfo *) arg;
4097 
4098 		if ((error = copyin(&gei->pm_class, &cl, sizeof(cl))) != 0)
4099 			break;
4100 
4101 		/* Only SOFT class is dynamic. */
4102 		if (cl != PMC_CLASS_SOFT) {
4103 			error = EINVAL;
4104 			break;
4105 		}
4106 
4107 		nevent = 0;
4108 		for (ev = PMC_EV_SOFT_FIRST; (int)ev <= PMC_EV_SOFT_LAST; ev++) {
4109 			ps = pmc_soft_ev_acquire(ev);
4110 			if (ps == NULL)
4111 				continue;
4112 			bcopy(&ps->ps_ev, &dev, sizeof(dev));
4113 			pmc_soft_ev_release(ps);
4114 
4115 			error = copyout(&dev,
4116 			    &gei->pm_events[nevent],
4117 			    sizeof(struct pmc_dyn_event_descr));
4118 			if (error != 0)
4119 				break;
4120 			nevent++;
4121 		}
4122 		if (error != 0)
4123 			break;
4124 
4125 		error = copyout(&nevent, &gei->pm_nevent,
4126 		    sizeof(nevent));
4127 	}
4128 	break;
4129 
4130 	/*
4131 	 * Get module statistics
4132 	 */
4133 
4134 	case PMC_OP_GETDRIVERSTATS:
4135 	{
4136 		struct pmc_op_getdriverstats gms;
4137 #define CFETCH(a, b, field) a.field = counter_u64_fetch(b.field)
4138 		CFETCH(gms, pmc_stats, pm_intr_ignored);
4139 		CFETCH(gms, pmc_stats, pm_intr_processed);
4140 		CFETCH(gms, pmc_stats, pm_intr_bufferfull);
4141 		CFETCH(gms, pmc_stats, pm_syscalls);
4142 		CFETCH(gms, pmc_stats, pm_syscall_errors);
4143 		CFETCH(gms, pmc_stats, pm_buffer_requests);
4144 		CFETCH(gms, pmc_stats, pm_buffer_requests_failed);
4145 		CFETCH(gms, pmc_stats, pm_log_sweeps);
4146 #undef CFETCH
4147 		error = copyout(&gms, arg, sizeof(gms));
4148 	}
4149 	break;
4150 
4151 
4152 	/*
4153 	 * Retrieve module version number
4154 	 */
4155 
4156 	case PMC_OP_GETMODULEVERSION:
4157 	{
4158 		uint32_t cv, modv;
4159 
4160 		/* retrieve the client's idea of the ABI version */
4161 		if ((error = copyin(arg, &cv, sizeof(uint32_t))) != 0)
4162 			break;
4163 		/* don't service clients newer than our driver */
4164 		modv = PMC_VERSION;
4165 		if ((cv & 0xFFFF0000) > (modv & 0xFFFF0000)) {
4166 			error = EPROGMISMATCH;
4167 			break;
4168 		}
4169 		error = copyout(&modv, arg, sizeof(int));
4170 	}
4171 	break;
4172 
4173 
4174 	/*
4175 	 * Retrieve the state of all the PMCs on a given
4176 	 * CPU.
4177 	 */
4178 
4179 	case PMC_OP_GETPMCINFO:
4180 	{
4181 		int ari;
4182 		struct pmc *pm;
4183 		size_t pmcinfo_size;
4184 		uint32_t cpu, n, npmc;
4185 		struct pmc_owner *po;
4186 		struct pmc_binding pb;
4187 		struct pmc_classdep *pcd;
4188 		struct pmc_info *p, *pmcinfo;
4189 		struct pmc_op_getpmcinfo *gpi;
4190 
4191 		PMC_DOWNGRADE_SX();
4192 
4193 		gpi = (struct pmc_op_getpmcinfo *) arg;
4194 
4195 		if ((error = copyin(&gpi->pm_cpu, &cpu, sizeof(cpu))) != 0)
4196 			break;
4197 
4198 		if (cpu >= pmc_cpu_max()) {
4199 			error = EINVAL;
4200 			break;
4201 		}
4202 
4203 		if (!pmc_cpu_is_active(cpu)) {
4204 			error = ENXIO;
4205 			break;
4206 		}
4207 
4208 		/* switch to CPU 'cpu' */
4209 		pmc_save_cpu_binding(&pb);
4210 		pmc_select_cpu(cpu);
4211 
4212 		npmc = md->pmd_npmc;
4213 
4214 		pmcinfo_size = npmc * sizeof(struct pmc_info);
4215 		pmcinfo = malloc(pmcinfo_size, M_PMC, M_WAITOK | M_ZERO);
4216 
4217 		p = pmcinfo;
4218 
4219 		for (n = 0; n < md->pmd_npmc; n++, p++) {
4220 
4221 			pcd = pmc_ri_to_classdep(md, n, &ari);
4222 
4223 			KASSERT(pcd != NULL,
4224 			    ("[pmc,%d] null pcd ri=%d", __LINE__, n));
4225 
4226 			if ((error = pcd->pcd_describe(cpu, ari, p, &pm)) != 0)
4227 				break;
4228 
4229 			if (PMC_ROW_DISP_IS_STANDALONE(n))
4230 				p->pm_rowdisp = PMC_DISP_STANDALONE;
4231 			else if (PMC_ROW_DISP_IS_THREAD(n))
4232 				p->pm_rowdisp = PMC_DISP_THREAD;
4233 			else
4234 				p->pm_rowdisp = PMC_DISP_FREE;
4235 
4236 			p->pm_ownerpid = -1;
4237 
4238 			if (pm == NULL)	/* no PMC associated */
4239 				continue;
4240 
4241 			po = pm->pm_owner;
4242 
4243 			KASSERT(po->po_owner != NULL,
4244 			    ("[pmc,%d] pmc_owner had a null proc pointer",
4245 				__LINE__));
4246 
4247 			p->pm_ownerpid = po->po_owner->p_pid;
4248 			p->pm_mode     = PMC_TO_MODE(pm);
4249 			p->pm_event    = pm->pm_event;
4250 			p->pm_flags    = pm->pm_flags;
4251 
4252 			if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
4253 				p->pm_reloadcount =
4254 				    pm->pm_sc.pm_reloadcount;
4255 		}
4256 
4257 		pmc_restore_cpu_binding(&pb);
4258 
4259 		/* now copy out the PMC info collected */
4260 		if (error == 0)
4261 			error = copyout(pmcinfo, &gpi->pm_pmcs, pmcinfo_size);
4262 
4263 		free(pmcinfo, M_PMC);
4264 	}
4265 	break;
4266 
4267 
4268 	/*
4269 	 * Set the administrative state of a PMC.  I.e. whether
4270 	 * the PMC is to be used or not.
4271 	 */
4272 
4273 	case PMC_OP_PMCADMIN:
4274 	{
4275 		int cpu, ri;
4276 		enum pmc_state request;
4277 		struct pmc_cpu *pc;
4278 		struct pmc_hw *phw;
4279 		struct pmc_op_pmcadmin pma;
4280 		struct pmc_binding pb;
4281 
4282 		sx_assert(&pmc_sx, SX_XLOCKED);
4283 
4284 		KASSERT(td == curthread,
4285 		    ("[pmc,%d] td != curthread", __LINE__));
4286 
4287 		error = priv_check(td, PRIV_PMC_MANAGE);
4288 		if (error)
4289 			break;
4290 
4291 		if ((error = copyin(arg, &pma, sizeof(pma))) != 0)
4292 			break;
4293 
4294 		cpu = pma.pm_cpu;
4295 
4296 		if (cpu < 0 || cpu >= (int) pmc_cpu_max()) {
4297 			error = EINVAL;
4298 			break;
4299 		}
4300 
4301 		if (!pmc_cpu_is_active(cpu)) {
4302 			error = ENXIO;
4303 			break;
4304 		}
4305 
4306 		request = pma.pm_state;
4307 
4308 		if (request != PMC_STATE_DISABLED &&
4309 		    request != PMC_STATE_FREE) {
4310 			error = EINVAL;
4311 			break;
4312 		}
4313 
4314 		ri = pma.pm_pmc; /* pmc id == row index */
4315 		if (ri < 0 || ri >= (int) md->pmd_npmc) {
4316 			error = EINVAL;
4317 			break;
4318 		}
4319 
4320 		/*
4321 		 * We can't disable a PMC with a row-index allocated
4322 		 * for process virtual PMCs.
4323 		 */
4324 
4325 		if (PMC_ROW_DISP_IS_THREAD(ri) &&
4326 		    request == PMC_STATE_DISABLED) {
4327 			error = EBUSY;
4328 			break;
4329 		}
4330 
4331 		/*
4332 		 * otherwise, this PMC on this CPU is either free or
4333 		 * in system-wide mode.
4334 		 */
4335 
4336 		pmc_save_cpu_binding(&pb);
4337 		pmc_select_cpu(cpu);
4338 
4339 		pc  = pmc_pcpu[cpu];
4340 		phw = pc->pc_hwpmcs[ri];
4341 
4342 		/*
4343 		 * XXX do we need some kind of 'forced' disable?
4344 		 */
4345 
4346 		if (phw->phw_pmc == NULL) {
4347 			if (request == PMC_STATE_DISABLED &&
4348 			    (phw->phw_state & PMC_PHW_FLAG_IS_ENABLED)) {
4349 				phw->phw_state &= ~PMC_PHW_FLAG_IS_ENABLED;
4350 				PMC_MARK_ROW_STANDALONE(ri);
4351 			} else if (request == PMC_STATE_FREE &&
4352 			    (phw->phw_state & PMC_PHW_FLAG_IS_ENABLED) == 0) {
4353 				phw->phw_state |=  PMC_PHW_FLAG_IS_ENABLED;
4354 				PMC_UNMARK_ROW_STANDALONE(ri);
4355 			}
4356 			/* other cases are a no-op */
4357 		} else
4358 			error = EBUSY;
4359 
4360 		pmc_restore_cpu_binding(&pb);
4361 	}
4362 	break;
4363 
4364 
4365 	/*
4366 	 * Allocate a PMC.
4367 	 */
4368 	case PMC_OP_PMCALLOCATE:
4369 	{
4370 		struct pmc_op_pmcallocate pa;
4371 
4372 		error = copyin(arg, &pa, sizeof(pa));
4373 		if (error != 0)
4374 			break;
4375 
4376 		error = pmc_do_op_pmcallocate(td, &pa);
4377 		if (error != 0)
4378 			break;
4379 
4380 		error = copyout(&pa, arg, sizeof(pa));
4381 	}
4382 	break;
4383 
4384 	/*
4385 	 * Attach a PMC to a process.
4386 	 */
4387 	case PMC_OP_PMCATTACH:
4388 	{
4389 		struct pmc_op_pmcattach a;
4390 
4391 		error = copyin(arg, &a, sizeof(a));
4392 		if (error != 0)
4393 			break;
4394 
4395 		error = pmc_do_op_pmcattach(td, a);
4396 	}
4397 	break;
4398 
4399 	/*
4400 	 * Detach an attached PMC from a process.
4401 	 */
4402 	case PMC_OP_PMCDETACH:
4403 	{
4404 		struct pmc_op_pmcattach a;
4405 
4406 		error = copyin(arg, &a, sizeof(a));
4407 		if (error != 0)
4408 			break;
4409 
4410 		error = pmc_do_op_pmcdetach(td, a);
4411 	}
4412 	break;
4413 
4414 
4415 	/*
4416 	 * Retrieve the MSR number associated with the counter
4417 	 * 'pmc_id'.  This allows processes to directly use RDPMC
4418 	 * instructions to read their PMCs, without the overhead of a
4419 	 * system call.
4420 	 */
4421 
4422 	case PMC_OP_PMCGETMSR:
4423 	{
4424 		int adjri, ri;
4425 		struct pmc *pm;
4426 		struct pmc_target *pt;
4427 		struct pmc_op_getmsr gm;
4428 		struct pmc_classdep *pcd;
4429 
4430 		PMC_DOWNGRADE_SX();
4431 
4432 		if ((error = copyin(arg, &gm, sizeof(gm))) != 0)
4433 			break;
4434 
4435 		if ((error = pmc_find_pmc(gm.pm_pmcid, &pm)) != 0)
4436 			break;
4437 
4438 		/*
4439 		 * The allocated PMC has to be a process virtual PMC,
4440 		 * i.e., of type MODE_T[CS].  Global PMCs can only be
4441 		 * read using the PMCREAD operation since they may be
4442 		 * allocated on a different CPU than the one we could
4443 		 * be running on at the time of the RDPMC instruction.
4444 		 *
4445 		 * The GETMSR operation is not allowed for PMCs that
4446 		 * are inherited across processes.
4447 		 */
4448 
4449 		if (!PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm)) ||
4450 		    (pm->pm_flags & PMC_F_DESCENDANTS)) {
4451 			error = EINVAL;
4452 			break;
4453 		}
4454 
4455 		/*
4456 		 * It only makes sense to use a RDPMC (or its
4457 		 * equivalent instruction on non-x86 architectures) on
4458 		 * a process that has allocated and attached a PMC to
4459 		 * itself.  Conversely the PMC is only allowed to have
4460 		 * one process attached to it -- its owner.
4461 		 */
4462 
4463 		if ((pt = LIST_FIRST(&pm->pm_targets)) == NULL ||
4464 		    LIST_NEXT(pt, pt_next) != NULL ||
4465 		    pt->pt_process->pp_proc != pm->pm_owner->po_owner) {
4466 			error = EINVAL;
4467 			break;
4468 		}
4469 
4470 		ri = PMC_TO_ROWINDEX(pm);
4471 		pcd = pmc_ri_to_classdep(md, ri, &adjri);
4472 
4473 		/* PMC class has no 'GETMSR' support */
4474 		if (pcd->pcd_get_msr == NULL) {
4475 			error = ENOSYS;
4476 			break;
4477 		}
4478 
4479 		if ((error = (*pcd->pcd_get_msr)(adjri, &gm.pm_msr)) < 0)
4480 			break;
4481 
4482 		if ((error = copyout(&gm, arg, sizeof(gm))) < 0)
4483 			break;
4484 
4485 		/*
4486 		 * Mark our process as using MSRs.  Update machine
4487 		 * state using a forced context switch.
4488 		 */
4489 
4490 		pt->pt_process->pp_flags |= PMC_PP_ENABLE_MSR_ACCESS;
4491 		pmc_force_context_switch();
4492 
4493 	}
4494 	break;
4495 
4496 	/*
4497 	 * Release an allocated PMC.
4498 	 */
4499 	case PMC_OP_PMCRELEASE:
4500 	{
4501 		struct pmc_op_simple sp;
4502 
4503 		error = copyin(arg, &sp, sizeof(sp));
4504 		if (error != 0)
4505 			break;
4506 
4507 		error = pmc_do_op_pmcrelease(sp.pm_pmcid);
4508 	}
4509 	break;
4510 
4511 	/*
4512 	 * Read and/or write a PMC.
4513 	 */
4514 	case PMC_OP_PMCRW:
4515 	{
4516 		struct pmc_op_pmcrw prw;
4517 		struct pmc_op_pmcrw *pprw;
4518 		pmc_value_t oldvalue;
4519 
4520 		PMC_DOWNGRADE_SX();
4521 
4522 		error = copyin(arg, &prw, sizeof(prw));
4523 		if (error != 0)
4524 			break;
4525 
4526 		error = pmc_do_op_pmcrw(&prw, &oldvalue);
4527 		if (error != 0)
4528 			break;
4529 
4530 		/* Return old value if requested. */
4531 		if ((prw.pm_flags & PMC_F_OLDVALUE) != 0) {
4532 			pprw = arg;
4533 			error = copyout(&oldvalue, &pprw->pm_value,
4534 			    sizeof(prw.pm_value));
4535 		}
4536 	}
4537 	break;
4538 
4539 
4540 	/*
4541 	 * Set the sampling rate for a sampling mode PMC and the
4542 	 * initial count for a counting mode PMC.
4543 	 */
4544 
4545 	case PMC_OP_PMCSETCOUNT:
4546 	{
4547 		struct pmc *pm;
4548 		struct pmc_op_pmcsetcount sc;
4549 
4550 		PMC_DOWNGRADE_SX();
4551 
4552 		if ((error = copyin(arg, &sc, sizeof(sc))) != 0)
4553 			break;
4554 
4555 		if ((error = pmc_find_pmc(sc.pm_pmcid, &pm)) != 0)
4556 			break;
4557 
4558 		if (pm->pm_state == PMC_STATE_RUNNING) {
4559 			error = EBUSY;
4560 			break;
4561 		}
4562 
4563 		if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm))) {
4564 			/*
4565 			 * Don't permit requested sample rate to be
4566 			 * less than pmc_mincount.
4567 			 */
4568 			if (sc.pm_count < MAX(1, pmc_mincount))
4569 				log(LOG_WARNING, "pmcsetcount: passed sample "
4570 				    "rate %ju - setting to %u\n",
4571 				    (uintmax_t)sc.pm_count,
4572 				    MAX(1, pmc_mincount));
4573 			pm->pm_sc.pm_reloadcount = MAX(MAX(1, pmc_mincount),
4574 			    sc.pm_count);
4575 		} else
4576 			pm->pm_sc.pm_initial = sc.pm_count;
4577 	}
4578 	break;
4579 
4580 
4581 	/*
4582 	 * Start a PMC.
4583 	 */
4584 
4585 	case PMC_OP_PMCSTART:
4586 	{
4587 		pmc_id_t pmcid;
4588 		struct pmc *pm;
4589 		struct pmc_op_simple sp;
4590 
4591 		sx_assert(&pmc_sx, SX_XLOCKED);
4592 
4593 		if ((error = copyin(arg, &sp, sizeof(sp))) != 0)
4594 			break;
4595 
4596 		pmcid = sp.pm_pmcid;
4597 
4598 		if ((error = pmc_find_pmc(pmcid, &pm)) != 0)
4599 			break;
4600 
4601 		KASSERT(pmcid == pm->pm_id,
4602 		    ("[pmc,%d] pmcid %x != id %x", __LINE__,
4603 			pm->pm_id, pmcid));
4604 
4605 		if (pm->pm_state == PMC_STATE_RUNNING) /* already running */
4606 			break;
4607 		else if (pm->pm_state != PMC_STATE_STOPPED &&
4608 		    pm->pm_state != PMC_STATE_ALLOCATED) {
4609 			error = EINVAL;
4610 			break;
4611 		}
4612 
4613 		error = pmc_start(pm);
4614 	}
4615 	break;
4616 
4617 
4618 	/*
4619 	 * Stop a PMC.
4620 	 */
4621 
4622 	case PMC_OP_PMCSTOP:
4623 	{
4624 		pmc_id_t pmcid;
4625 		struct pmc *pm;
4626 		struct pmc_op_simple sp;
4627 
4628 		PMC_DOWNGRADE_SX();
4629 
4630 		if ((error = copyin(arg, &sp, sizeof(sp))) != 0)
4631 			break;
4632 
4633 		pmcid = sp.pm_pmcid;
4634 
4635 		/*
4636 		 * Mark the PMC as inactive and invoke the MD stop
4637 		 * routines if needed.
4638 		 */
4639 
4640 		if ((error = pmc_find_pmc(pmcid, &pm)) != 0)
4641 			break;
4642 
4643 		KASSERT(pmcid == pm->pm_id,
4644 		    ("[pmc,%d] pmc id %x != pmcid %x", __LINE__,
4645 			pm->pm_id, pmcid));
4646 
4647 		if (pm->pm_state == PMC_STATE_STOPPED) /* already stopped */
4648 			break;
4649 		else if (pm->pm_state != PMC_STATE_RUNNING) {
4650 			error = EINVAL;
4651 			break;
4652 		}
4653 
4654 		error = pmc_stop(pm);
4655 	}
4656 	break;
4657 
4658 
4659 	/*
4660 	 * Write a user supplied value to the log file.
4661 	 */
4662 
4663 	case PMC_OP_WRITELOG:
4664 	{
4665 		struct pmc_op_writelog wl;
4666 		struct pmc_owner *po;
4667 
4668 		PMC_DOWNGRADE_SX();
4669 
4670 		if ((error = copyin(arg, &wl, sizeof(wl))) != 0)
4671 			break;
4672 
4673 		if ((po = pmc_find_owner_descriptor(td->td_proc)) == NULL) {
4674 			error = EINVAL;
4675 			break;
4676 		}
4677 
4678 		if ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0) {
4679 			error = EINVAL;
4680 			break;
4681 		}
4682 
4683 		error = pmclog_process_userlog(po, &wl);
4684 	}
4685 	break;
4686 
4687 	/*
4688 	 * Get the PMC capabilities
4689 	 */
4690 
4691 	case PMC_OP_GETCAPS:
4692 	{
4693 		struct pmc_op_caps c;
4694 		struct pmc *pm;
4695 		struct pmc_classdep *pcd;
4696 		pmc_id_t pmcid;
4697 		int adjri, ri;
4698 
4699 		PMC_DOWNGRADE_SX();
4700 
4701 		if ((error = copyin(arg, &c, sizeof(c))) != 0)
4702 			break;
4703 
4704 		pmcid = c.pm_pmcid;
4705 
4706 		if ((error = pmc_find_pmc(pmcid, &pm)) != 0)
4707 			break;
4708 
4709 		KASSERT(pmcid == pm->pm_id,
4710 		    ("[pmc,%d] pmc id %x != pmcid %x", __LINE__,
4711 			pm->pm_id, pmcid));
4712 
4713 		ri = PMC_TO_ROWINDEX(pm);
4714 		pcd = pmc_ri_to_classdep(md, ri, &adjri);
4715 
4716 		/*
4717 		 * If PMC class has no GETCAPS return the class capabilities
4718 		 * otherwise get the per counter capabilities.
4719 		 */
4720 		if (pcd->pcd_get_caps == NULL) {
4721 			c.pm_caps = pcd->pcd_caps;
4722 		} else {
4723 			error = (*pcd->pcd_get_caps)(adjri, &c.pm_caps);
4724 			if (error < 0)
4725 				break;
4726 		}
4727 
4728 		if ((error = copyout(&c, arg, sizeof(c))) < 0)
4729 			break;
4730 	}
4731 	break;
4732 
4733 	default:
4734 		error = EINVAL;
4735 		break;
4736 	}
4737 
4738 	if (is_sx_downgraded)
4739 		sx_sunlock(&pmc_sx);
4740 	else
4741 		sx_xunlock(&pmc_sx);
4742 done_syscall:
4743 	if (error)
4744 		counter_u64_add(pmc_stats.pm_syscall_errors, 1);
4745 
4746 	return (error);
4747 }
4748 
4749 /*
4750  * Helper functions
4751  */
4752 
4753 /*
4754  * Mark the thread as needing callchain capture and post an AST.  The
4755  * actual callchain capture will be done in a context where it is safe
4756  * to take page faults.
4757  */
4758 static void
4759 pmc_post_callchain_callback(void)
4760 {
4761 	struct thread *td;
4762 
4763 	td = curthread;
4764 
4765 	/*
4766 	 * If there is multiple PMCs for the same interrupt ignore new post
4767 	 */
4768 	if ((td->td_pflags & TDP_CALLCHAIN) != 0)
4769 		return;
4770 
4771 	/*
4772 	 * Mark this thread as needing callchain capture.
4773 	 * `td->td_pflags' will be safe to touch because this thread
4774 	 * was in user space when it was interrupted.
4775 	 */
4776 	td->td_pflags |= TDP_CALLCHAIN;
4777 
4778 	/*
4779 	 * Don't let this thread migrate between CPUs until callchain
4780 	 * capture completes.
4781 	 */
4782 	sched_pin();
4783 
4784 	return;
4785 }
4786 
4787 static void
4788 pmc_multipart_add(struct pmc_sample *ps, int type, int length)
4789 {
4790 	int i;
4791 	uint8_t *hdr;
4792 
4793 	MPASS(ps->ps_pc != NULL);
4794 	MPASS(ps->ps_nsamples_actual != 0);
4795 
4796 	hdr = (uint8_t *)ps->ps_pc;
4797 
4798 	for (i = 0; i < PMC_MULTIPART_HEADER_ENTRIES; i++) {
4799 		if (hdr[2 * i] == PMC_CC_MULTIPART_NONE) {
4800 			hdr[2 * i] = type;
4801 			hdr[2 * i + 1] = length;
4802 			ps->ps_nsamples_actual += length;
4803 			return;
4804 		}
4805 	}
4806 
4807 	KASSERT(false, ("Too many parts in the multipart header!"));
4808 }
4809 
4810 static void
4811 pmc_multipart_copydata(struct pmc_sample *ps, struct pmc_multipart *mp)
4812 {
4813 	int i, scale;
4814 	uint64_t *ps_pc;
4815 
4816 	MPASS(ps->ps_pc != NULL);
4817 	MPASS(ps->ps_nsamples_actual != 0);
4818 
4819 	ps_pc = (uint64_t *)ps->ps_pc;
4820 
4821 	for (i = 0; i < mp->pl_length; i++)
4822 		ps_pc[i + 1] = mp->pl_mpdata[i];
4823 
4824 	scale = sizeof(uint64_t) / sizeof(uintptr_t);
4825 	pmc_multipart_add(ps, mp->pl_type, scale * mp->pl_length);
4826 }
4827 
4828 /*
4829  * Find a free slot in the per-cpu array of samples and capture the
4830  * current callchain there.  If a sample was successfully added, a bit
4831  * is set in mask 'pmc_cpumask' denoting that the DO_SAMPLES hook
4832  * needs to be invoked from the clock handler.
4833  *
4834  * This function is meant to be called from an NMI handler.  It cannot
4835  * use any of the locking primitives supplied by the OS.
4836  */
4837 static int
4838 pmc_add_sample(ring_type_t ring, struct pmc *pm, struct trapframe *tf,
4839     struct pmc_multipart *mp)
4840 {
4841 	struct pmc_sample *ps;
4842 	struct pmc_samplebuffer *psb;
4843 	struct thread *td;
4844 	int error, cpu, callchaindepth;
4845 	bool inuserspace;
4846 
4847 	error = 0;
4848 
4849 	/*
4850 	 * Allocate space for a sample buffer.
4851 	 */
4852 	cpu = curcpu;
4853 	psb = pmc_pcpu[cpu]->pc_sb[ring];
4854 	inuserspace = TRAPF_USERMODE(tf);
4855 	ps = PMC_PROD_SAMPLE(psb);
4856 	if (psb->ps_considx != psb->ps_prodidx &&
4857 		ps->ps_nsamples) {	/* in use, reader hasn't caught up */
4858 		pm->pm_pcpu_state[cpu].pps_stalled = 1;
4859 		counter_u64_add(pmc_stats.pm_intr_bufferfull, 1);
4860 		PMCDBG6(SAM,INT,1,"(spc) cpu=%d pm=%p tf=%p um=%d wr=%d rd=%d",
4861 		    cpu, pm, tf, inuserspace,
4862 		    (int)(psb->ps_prodidx & pmc_sample_mask),
4863 		    (int)(psb->ps_considx & pmc_sample_mask));
4864 		callchaindepth = 1;
4865 		error = ENOMEM;
4866 		goto done;
4867 	}
4868 
4869 	/* Fill in entry. */
4870 	PMCDBG6(SAM,INT,1,"cpu=%d pm=%p tf=%p um=%d wr=%d rd=%d", cpu, pm, tf,
4871 	    inuserspace, (int)(psb->ps_prodidx & pmc_sample_mask),
4872 	    (int)(psb->ps_considx & pmc_sample_mask));
4873 
4874 	td = curthread;
4875 	ps->ps_pmc = pm;
4876 	ps->ps_td = td;
4877 	ps->ps_pid = td->td_proc->p_pid;
4878 	ps->ps_tid = td->td_tid;
4879 	ps->ps_tsc = pmc_rdtsc();
4880 	ps->ps_ticks = ticks;
4881 	ps->ps_cpu = cpu;
4882 	ps->ps_flags = inuserspace ? PMC_CC_F_USERSPACE : 0;
4883 	ps->ps_nsamples_actual = 0;
4884 
4885 	callchaindepth = (pm->pm_flags & PMC_F_CALLCHAIN) ?
4886 	    pmc_callchaindepth : 1;
4887 
4888 	MPASS(ps->ps_pc != NULL);
4889 
4890 	if (mp != NULL) {
4891 		/* Set multipart flag, clear header and copy data */
4892 		ps->ps_flags |= PMC_CC_F_MULTIPART;
4893 		ps->ps_pc[0] = 0;
4894 		ps->ps_nsamples_actual = 1;
4895 		pmc_multipart_copydata(ps, mp);
4896 	}
4897 
4898 	if (callchaindepth == 1) {
4899 		ps->ps_pc[ps->ps_nsamples_actual] = PMC_TRAPFRAME_TO_PC(tf);
4900 	} else {
4901 		/*
4902 		 * Kernel stack traversals can be done immediately, while we
4903 		 * defer to an AST for user space traversals.
4904 		 */
4905 		if (!inuserspace) {
4906 			callchaindepth = pmc_save_kernel_callchain(
4907 			    ps->ps_pc + ps->ps_nsamples_actual,
4908 			    callchaindepth - ps->ps_nsamples_actual, tf);
4909 			callchaindepth += ps->ps_nsamples_actual;
4910 		} else {
4911 			pmc_post_callchain_callback();
4912 			callchaindepth = PMC_USER_CALLCHAIN_PENDING;
4913 		}
4914 	}
4915 
4916 	ps->ps_nsamples = callchaindepth; /* mark entry as in-use */
4917 	if (ring == PMC_UR) {
4918 		ps->ps_nsamples_actual = ps->ps_nsamples;
4919 		ps->ps_nsamples = PMC_USER_CALLCHAIN_PENDING;
4920 	}
4921 
4922 	KASSERT(counter_u64_fetch(pm->pm_runcount) >= 0,
4923 	    ("[pmc,%d] pm=%p runcount %ju", __LINE__, pm,
4924 	    (uintmax_t)counter_u64_fetch(pm->pm_runcount)));
4925 
4926 	counter_u64_add(pm->pm_runcount, 1);	/* hold onto PMC */
4927 	/* increment write pointer */
4928 	psb->ps_prodidx++;
4929 done:
4930 	/* mark CPU as needing processing */
4931 	if (callchaindepth != PMC_USER_CALLCHAIN_PENDING)
4932 		DPCPU_SET(pmc_sampled, 1);
4933 
4934 	return (error);
4935 }
4936 
4937 /*
4938  * Interrupt processing.
4939  *
4940  * This function may be called from an NMI handler. It cannot use any of the
4941  * locking primitives supplied by the OS.
4942  */
4943 int
4944 pmc_process_interrupt_mp(int ring, struct pmc *pm, struct trapframe *tf,
4945     struct pmc_multipart *mp)
4946 {
4947 	struct thread *td;
4948 
4949 	td = curthread;
4950 	if ((pm->pm_flags & PMC_F_USERCALLCHAIN) &&
4951 	    (td->td_proc->p_flag & P_KPROC) == 0 && !TRAPF_USERMODE(tf)) {
4952 		atomic_add_int(&td->td_pmcpend, 1);
4953 		return (pmc_add_sample(PMC_UR, pm, tf, mp));
4954 	}
4955 	return (pmc_add_sample(ring, pm, tf, mp));
4956 }
4957 
4958 int
4959 pmc_process_interrupt(int ring, struct pmc *pm, struct trapframe *tf)
4960 {
4961 	return (pmc_process_interrupt_mp(ring, pm, tf, NULL));
4962 }
4963 
4964 /*
4965  * Capture a user call chain. This function will be called from ast()
4966  * before control returns to userland and before the process gets
4967  * rescheduled.
4968  */
4969 static void
4970 pmc_capture_user_callchain(int cpu, int ring, struct trapframe *tf)
4971 {
4972 	struct pmc *pm;
4973 	struct pmc_sample *ps;
4974 	struct pmc_samplebuffer *psb;
4975 	struct thread *td;
4976 	uint64_t considx, prodidx;
4977 	int nsamples, nrecords, pass, iter;
4978 	int start_ticks __diagused;
4979 
4980 	psb = pmc_pcpu[cpu]->pc_sb[ring];
4981 	td = curthread;
4982 	nrecords = INT_MAX;
4983 	pass = 0;
4984 	start_ticks = ticks;
4985 
4986 	KASSERT(td->td_pflags & TDP_CALLCHAIN,
4987 	    ("[pmc,%d] Retrieving callchain for thread that doesn't want it",
4988 	    __LINE__));
4989 restart:
4990 	if (ring == PMC_UR)
4991 		nrecords = atomic_readandclear_32(&td->td_pmcpend);
4992 
4993 	for (iter = 0, considx = psb->ps_considx, prodidx = psb->ps_prodidx;
4994 	    considx < prodidx && iter < pmc_nsamples; considx++, iter++) {
4995 		ps = PMC_CONS_SAMPLE_OFF(psb, considx);
4996 
4997 		/*
4998 		 * Iterate through all deferred callchain requests. Walk from
4999 		 * the current read pointer to the current write pointer.
5000 		 */
5001 #ifdef INVARIANTS
5002 		if (ps->ps_nsamples == PMC_SAMPLE_FREE) {
5003 			continue;
5004 		}
5005 #endif
5006 		if (ps->ps_td != td ||
5007 		    ps->ps_nsamples != PMC_USER_CALLCHAIN_PENDING ||
5008 		    ps->ps_pmc->pm_state != PMC_STATE_RUNNING)
5009 			continue;
5010 
5011 		KASSERT(ps->ps_cpu == cpu,
5012 		    ("[pmc,%d] cpu mismatch ps_cpu=%d pcpu=%d", __LINE__,
5013 		    ps->ps_cpu, PCPU_GET(cpuid)));
5014 
5015 		pm = ps->ps_pmc;
5016 		KASSERT(pm->pm_flags & PMC_F_CALLCHAIN,
5017 		    ("[pmc,%d] Retrieving callchain for PMC that doesn't "
5018 		    "want it", __LINE__));
5019 		KASSERT(counter_u64_fetch(pm->pm_runcount) > 0,
5020 		    ("[pmc,%d] runcount %ju", __LINE__,
5021 		    (uintmax_t)counter_u64_fetch(pm->pm_runcount)));
5022 
5023 		if (ring == PMC_UR) {
5024 			counter_u64_add(pmc_stats.pm_merges, 1);
5025 		}
5026 		nsamples = ps->ps_nsamples_actual;
5027 
5028 		/*
5029 		 * Retrieve the callchain and mark the sample buffer
5030 		 * as 'processable' by the timer tick sweep code.
5031 		 */
5032 		if (__predict_true(nsamples < pmc_callchaindepth - 1))
5033 			nsamples += pmc_save_user_callchain(ps->ps_pc + nsamples,
5034 			    pmc_callchaindepth - nsamples - 1, tf);
5035 
5036 		/*
5037 		 * We have to prevent hardclock from potentially overwriting
5038 		 * this sample between when we read the value and when we set
5039 		 * it.
5040 		 */
5041 		spinlock_enter();
5042 
5043 		/*
5044 		 * Verify that the sample hasn't been dropped in the meantime.
5045 		 */
5046 		if (ps->ps_nsamples == PMC_USER_CALLCHAIN_PENDING) {
5047 			ps->ps_nsamples = nsamples;
5048 			/*
5049 			 * If we couldn't get a sample, simply drop the
5050 			 * reference.
5051 			 */
5052 			if (nsamples == 0)
5053 				counter_u64_add(pm->pm_runcount, -1);
5054 		}
5055 		spinlock_exit();
5056 		if (nrecords-- == 1)
5057 			break;
5058 	}
5059 	if (__predict_false(ring == PMC_UR && td->td_pmcpend)) {
5060 		if (pass == 0) {
5061 			pass = 1;
5062 			goto restart;
5063 		}
5064 		/* only collect samples for this part once */
5065 		td->td_pmcpend = 0;
5066 	}
5067 
5068 #ifdef INVARIANTS
5069 	if ((ticks - start_ticks) > hz)
5070 		log(LOG_ERR, "%s took %d ticks\n", __func__, (ticks - start_ticks));
5071 #endif
5072 	/* mark CPU as needing processing */
5073 	DPCPU_SET(pmc_sampled, 1);
5074 }
5075 
5076 /*
5077  * Process saved PC samples.
5078  */
5079 static void
5080 pmc_process_samples(int cpu, ring_type_t ring)
5081 {
5082 	struct pmc *pm;
5083 	struct thread *td;
5084 	struct pmc_owner *po;
5085 	struct pmc_sample *ps;
5086 	struct pmc_classdep *pcd;
5087 	struct pmc_samplebuffer *psb;
5088 	uint64_t delta __diagused;
5089 	int adjri, n;
5090 
5091 	KASSERT(PCPU_GET(cpuid) == cpu,
5092 	    ("[pmc,%d] not on the correct CPU pcpu=%d cpu=%d", __LINE__,
5093 		PCPU_GET(cpuid), cpu));
5094 
5095 	psb = pmc_pcpu[cpu]->pc_sb[ring];
5096 	delta = psb->ps_prodidx - psb->ps_considx;
5097 	MPASS(delta <= pmc_nsamples);
5098 	MPASS(psb->ps_considx <= psb->ps_prodidx);
5099 	for (n = 0; psb->ps_considx < psb->ps_prodidx; psb->ps_considx++, n++) {
5100 		ps = PMC_CONS_SAMPLE(psb);
5101 
5102 		if (__predict_false(ps->ps_nsamples == PMC_SAMPLE_FREE))
5103 			continue;
5104 
5105 		/* skip non-running samples */
5106 		pm = ps->ps_pmc;
5107 		if (pm->pm_state != PMC_STATE_RUNNING)
5108 			goto entrydone;
5109 
5110 		KASSERT(counter_u64_fetch(pm->pm_runcount) > 0,
5111 		    ("[pmc,%d] pm=%p runcount %ju", __LINE__, pm,
5112 		    (uintmax_t)counter_u64_fetch(pm->pm_runcount)));
5113 		KASSERT(PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)),
5114 		    ("[pmc,%d] pmc=%p non-sampling mode=%d", __LINE__,
5115 		    pm, PMC_TO_MODE(pm)));
5116 
5117 		po = pm->pm_owner;
5118 
5119 		/* If there is a pending AST wait for completion */
5120 		if (ps->ps_nsamples == PMC_USER_CALLCHAIN_PENDING) {
5121 			/*
5122 			 * If we've been waiting more than 1 tick to
5123 			 * collect a callchain for this record then
5124 			 * drop it and move on.
5125 			 */
5126 			if (ticks - ps->ps_ticks > 1) {
5127 				/*
5128 				 * Track how often we hit this as it will
5129 				 * preferentially lose user samples
5130 				 * for long running system calls.
5131 				 */
5132 				counter_u64_add(pmc_stats.pm_overwrites, 1);
5133 				goto entrydone;
5134 			}
5135 			/* Need a rescan at a later time. */
5136 			DPCPU_SET(pmc_sampled, 1);
5137 			break;
5138 		}
5139 
5140 		PMCDBG6(SAM,OPS,1,"cpu=%d pm=%p n=%d fl=%x wr=%d rd=%d", cpu,
5141 		    pm, ps->ps_nsamples, ps->ps_flags,
5142 		    (int)(psb->ps_prodidx & pmc_sample_mask),
5143 		    (int)(psb->ps_considx & pmc_sample_mask));
5144 
5145 		/*
5146 		 * If this is a process-mode PMC that is attached to
5147 		 * its owner, and if the PC is in user mode, update
5148 		 * profiling statistics like timer-based profiling
5149 		 * would have done.
5150 		 *
5151 		 * Otherwise, this is either a sampling-mode PMC that
5152 		 * is attached to a different process than its owner,
5153 		 * or a system-wide sampling PMC. Dispatch a log
5154 		 * entry to the PMC's owner process.
5155 		 */
5156 		if (pm->pm_flags & PMC_F_ATTACHED_TO_OWNER) {
5157 			if (ps->ps_flags & PMC_CC_F_USERSPACE) {
5158 				td = FIRST_THREAD_IN_PROC(po->po_owner);
5159 				addupc_intr(td, ps->ps_pc[0], 1);
5160 			}
5161 		} else
5162 			pmclog_process_callchain(pm, ps);
5163 
5164 entrydone:
5165 		ps->ps_nsamples = 0; /* mark entry as free */
5166 		KASSERT(counter_u64_fetch(pm->pm_runcount) > 0,
5167 		    ("[pmc,%d] pm=%p runcount %ju", __LINE__, pm,
5168 		    (uintmax_t)counter_u64_fetch(pm->pm_runcount)));
5169 
5170 		counter_u64_add(pm->pm_runcount, -1);
5171 	}
5172 
5173 	counter_u64_add(pmc_stats.pm_log_sweeps, 1);
5174 
5175 	/* Do not re-enable stalled PMCs if we failed to process any samples */
5176 	if (n == 0)
5177 		return;
5178 
5179 	/*
5180 	 * Restart any stalled sampling PMCs on this CPU.
5181 	 *
5182 	 * If the NMI handler sets the pm_stalled field of a PMC after
5183 	 * the check below, we'll end up processing the stalled PMC at
5184 	 * the next hardclock tick.
5185 	 */
5186 	for (n = 0; n < md->pmd_npmc; n++) {
5187 		pcd = pmc_ri_to_classdep(md, n, &adjri);
5188 		KASSERT(pcd != NULL,
5189 		    ("[pmc,%d] null pcd ri=%d", __LINE__, n));
5190 		(void)(*pcd->pcd_get_config)(cpu, adjri, &pm);
5191 
5192 		if (pm == NULL ||				/* !cfg'ed */
5193 		    pm->pm_state != PMC_STATE_RUNNING ||	/* !active */
5194 		    !PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)) ||	/* !sampling */
5195 		    !pm->pm_pcpu_state[cpu].pps_cpustate ||	/* !desired */
5196 		    !pm->pm_pcpu_state[cpu].pps_stalled)	/* !stalled */
5197 			continue;
5198 
5199 		pm->pm_pcpu_state[cpu].pps_stalled = 0;
5200 		(void)(*pcd->pcd_start_pmc)(cpu, adjri, pm);
5201 	}
5202 }
5203 
5204 /*
5205  * Event handlers.
5206  */
5207 
5208 /*
5209  * Handle a process exit.
5210  *
5211  * Remove this process from all hash tables.  If this process
5212  * owned any PMCs, turn off those PMCs and deallocate them,
5213  * removing any associations with target processes.
5214  *
5215  * This function will be called by the last 'thread' of a
5216  * process.
5217  *
5218  * XXX This eventhandler gets called early in the exit process.
5219  * Consider using a 'hook' invocation from thread_exit() or equivalent
5220  * spot.  Another negative is that kse_exit doesn't seem to call
5221  * exit1() [??].
5222  */
5223 static void
5224 pmc_process_exit(void *arg __unused, struct proc *p)
5225 {
5226 	struct pmc *pm;
5227 	struct pmc_owner *po;
5228 	struct pmc_process *pp;
5229 	struct pmc_classdep *pcd;
5230 	pmc_value_t newvalue, tmp;
5231 	int ri, adjri, cpu;
5232 	bool is_using_hwpmcs;
5233 
5234 	PROC_LOCK(p);
5235 	is_using_hwpmcs = (p->p_flag & P_HWPMC) != 0;
5236 	PROC_UNLOCK(p);
5237 
5238 	/*
5239 	 * Log a sysexit event to all SS PMC owners.
5240 	 */
5241 	PMC_EPOCH_ENTER();
5242 	CK_LIST_FOREACH(po, &pmc_ss_owners, po_ssnext) {
5243 		if ((po->po_flags & PMC_PO_OWNS_LOGFILE) != 0)
5244 			pmclog_process_sysexit(po, p->p_pid);
5245 	}
5246 	PMC_EPOCH_EXIT();
5247 
5248 	PMC_GET_SX_XLOCK();
5249 	PMCDBG3(PRC,EXT,1,"process-exit proc=%p (%d, %s)", p, p->p_pid,
5250 	    p->p_comm);
5251 
5252 	if (!is_using_hwpmcs)
5253 		goto out;
5254 
5255 	/*
5256 	 * Since this code is invoked by the last thread in an exiting process,
5257 	 * we would have context switched IN at some prior point. However, with
5258 	 * PREEMPTION, kernel mode context switches may happen any time, so we
5259 	 * want to disable a context switch OUT till we get any PMCs targeting
5260 	 * this process off the hardware.
5261 	 *
5262 	 * We also need to atomically remove this process' entry from our
5263 	 * target process hash table, using PMC_FLAG_REMOVE.
5264 	 */
5265 	PMCDBG3(PRC,EXT,1, "process-exit proc=%p (%d, %s)", p, p->p_pid,
5266 	    p->p_comm);
5267 
5268 	critical_enter(); /* no preemption */
5269 
5270 	cpu = curthread->td_oncpu;
5271 
5272 	pp = pmc_find_process_descriptor(p, PMC_FLAG_REMOVE);
5273 	if (pp == NULL) {
5274 		critical_exit();
5275 		goto out;
5276 	}
5277 
5278 	PMCDBG2(PRC,EXT,2, "process-exit proc=%p pmc-process=%p", p, pp);
5279 
5280 	/*
5281 	 * The exiting process could be the target of some PMCs which will be
5282 	 * running on currently executing CPU.
5283 	 *
5284 	 * We need to turn these PMCs off like we would do at context switch
5285 	 * OUT time.
5286 	 */
5287 	for (ri = 0; ri < md->pmd_npmc; ri++) {
5288 		/*
5289 		 * Pick up the pmc pointer from hardware state similar to the
5290 		 * CSW_OUT code.
5291 		 */
5292 		pm = NULL;
5293 		pcd = pmc_ri_to_classdep(md, ri, &adjri);
5294 
5295 		(void)(*pcd->pcd_get_config)(cpu, adjri, &pm);
5296 
5297 		PMCDBG2(PRC,EXT,2, "ri=%d pm=%p", ri, pm);
5298 
5299 		if (pm == NULL || !PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm)))
5300 			continue;
5301 
5302 		PMCDBG4(PRC,EXT,2, "ppmcs[%d]=%p pm=%p state=%d", ri,
5303 		    pp->pp_pmcs[ri].pp_pmc, pm, pm->pm_state);
5304 
5305 		KASSERT(PMC_TO_ROWINDEX(pm) == ri,
5306 		    ("[pmc,%d] ri mismatch pmc(%d) ri(%d)", __LINE__,
5307 		    PMC_TO_ROWINDEX(pm), ri));
5308 		KASSERT(pm == pp->pp_pmcs[ri].pp_pmc,
5309 		    ("[pmc,%d] pm %p != pp_pmcs[%d] %p", __LINE__, pm, ri,
5310 		    pp->pp_pmcs[ri].pp_pmc));
5311 		KASSERT(counter_u64_fetch(pm->pm_runcount) > 0,
5312 		    ("[pmc,%d] bad runcount ri %d rc %ju", __LINE__, ri,
5313 		    (uintmax_t)counter_u64_fetch(pm->pm_runcount)));
5314 
5315 		/*
5316 		 * Change desired state, and then stop if not stalled. This
5317 		 * two-step dance should avoid race conditions where an
5318 		 * interrupt re-enables the PMC after this code has already
5319 		 * checked the pm_stalled flag.
5320 		 */
5321 		if (pm->pm_pcpu_state[cpu].pps_cpustate) {
5322 			pm->pm_pcpu_state[cpu].pps_cpustate = 0;
5323 			if (!pm->pm_pcpu_state[cpu].pps_stalled) {
5324 				(void)pcd->pcd_stop_pmc(cpu, adjri, pm);
5325 
5326 				if (PMC_TO_MODE(pm) == PMC_MODE_TC) {
5327 					pcd->pcd_read_pmc(cpu, adjri, pm,
5328 					    &newvalue);
5329 					tmp = pmc_delta(pcd, newvalue,
5330 					    PMC_PCPU_SAVED(cpu, ri));
5331 
5332 					mtx_pool_lock_spin(pmc_mtxpool, pm);
5333 					pm->pm_gv.pm_savedvalue += tmp;
5334 					pp->pp_pmcs[ri].pp_pmcval += tmp;
5335 					mtx_pool_unlock_spin(pmc_mtxpool, pm);
5336 				}
5337 			}
5338 		}
5339 
5340 		KASSERT(counter_u64_fetch(pm->pm_runcount) > 0,
5341 		    ("[pmc,%d] runcount is %d", __LINE__, ri));
5342 
5343 		counter_u64_add(pm->pm_runcount, -1);
5344 		(void)pcd->pcd_config_pmc(cpu, adjri, NULL);
5345 	}
5346 
5347 	/*
5348 	 * Inform the MD layer of this pseudo "context switch out".
5349 	 */
5350 	(void)md->pmd_switch_out(pmc_pcpu[cpu], pp);
5351 
5352 	critical_exit(); /* ok to be pre-empted now */
5353 
5354 	/*
5355 	 * Unlink this process from the PMCs that are targeting it. This will
5356 	 * send a signal to all PMC owner's whose PMCs are orphaned.
5357 	 *
5358 	 * Log PMC value at exit time if requested.
5359 	 */
5360 	for (ri = 0; ri < md->pmd_npmc; ri++) {
5361 		if ((pm = pp->pp_pmcs[ri].pp_pmc) != NULL) {
5362 			if ((pm->pm_flags & PMC_F_NEEDS_LOGFILE) != 0 &&
5363 			    PMC_IS_COUNTING_MODE(PMC_TO_MODE(pm))) {
5364 				pmclog_process_procexit(pm, pp);
5365 			}
5366 			pmc_unlink_target_process(pm, pp);
5367 		}
5368 	}
5369 	free(pp, M_PMC);
5370 
5371 out:
5372 	/*
5373 	 * If the process owned PMCs, free them up and free up memory.
5374 	 */
5375 	if ((po = pmc_find_owner_descriptor(p)) != NULL) {
5376 		if ((po->po_flags & PMC_PO_OWNS_LOGFILE) != 0)
5377 			pmclog_close(po);
5378 		pmc_remove_owner(po);
5379 		pmc_destroy_owner_descriptor(po);
5380 	}
5381 
5382 	sx_xunlock(&pmc_sx);
5383 }
5384 
5385 /*
5386  * Handle a process fork.
5387  *
5388  * If the parent process 'p1' is under HWPMC monitoring, then copy
5389  * over any attached PMCs that have 'do_descendants' semantics.
5390  */
5391 static void
5392 pmc_process_fork(void *arg __unused, struct proc *p1, struct proc *newproc,
5393     int flags __unused)
5394 {
5395 	struct pmc *pm;
5396 	struct pmc_owner *po;
5397 	struct pmc_process *ppnew, *ppold;
5398 	unsigned int ri;
5399 	bool is_using_hwpmcs, do_descendants;
5400 
5401 	PROC_LOCK(p1);
5402 	is_using_hwpmcs = (p1->p_flag & P_HWPMC) != 0;
5403 	PROC_UNLOCK(p1);
5404 
5405 	/*
5406 	 * If there are system-wide sampling PMCs active, we need to
5407 	 * log all fork events to their owner's logs.
5408 	 */
5409 	PMC_EPOCH_ENTER();
5410 	CK_LIST_FOREACH(po, &pmc_ss_owners, po_ssnext) {
5411 		if (po->po_flags & PMC_PO_OWNS_LOGFILE) {
5412 			pmclog_process_procfork(po, p1->p_pid, newproc->p_pid);
5413 			pmclog_process_proccreate(po, newproc, 1);
5414 		}
5415 	}
5416 	PMC_EPOCH_EXIT();
5417 
5418 	if (!is_using_hwpmcs)
5419 		return;
5420 
5421 	PMC_GET_SX_XLOCK();
5422 	PMCDBG4(PMC,FRK,1, "process-fork proc=%p (%d, %s) -> %p", p1,
5423 	    p1->p_pid, p1->p_comm, newproc);
5424 
5425 	/*
5426 	 * If the parent process (curthread->td_proc) is a
5427 	 * target of any PMCs, look for PMCs that are to be
5428 	 * inherited, and link these into the new process
5429 	 * descriptor.
5430 	 */
5431 	ppold = pmc_find_process_descriptor(curthread->td_proc, PMC_FLAG_NONE);
5432 	if (ppold == NULL)
5433 		goto done; /* nothing to do */
5434 
5435 	do_descendants = false;
5436 	for (ri = 0; ri < md->pmd_npmc; ri++) {
5437 		if ((pm = ppold->pp_pmcs[ri].pp_pmc) != NULL &&
5438 		    (pm->pm_flags & PMC_F_DESCENDANTS) != 0) {
5439 			do_descendants = true;
5440 			break;
5441 		}
5442 	}
5443 	if (!do_descendants) /* nothing to do */
5444 		goto done;
5445 
5446 	/*
5447 	 * Now mark the new process as being tracked by this driver.
5448 	 */
5449 	PROC_LOCK(newproc);
5450 	newproc->p_flag |= P_HWPMC;
5451 	PROC_UNLOCK(newproc);
5452 
5453 	/* Allocate a descriptor for the new process. */
5454 	ppnew = pmc_find_process_descriptor(newproc, PMC_FLAG_ALLOCATE);
5455 	if (ppnew == NULL)
5456 		goto done;
5457 
5458 	/*
5459 	 * Run through all PMCs that were targeting the old process
5460 	 * and which specified F_DESCENDANTS and attach them to the
5461 	 * new process.
5462 	 *
5463 	 * Log the fork event to all owners of PMCs attached to this
5464 	 * process, if not already logged.
5465 	 */
5466 	for (ri = 0; ri < md->pmd_npmc; ri++) {
5467 		if ((pm = ppold->pp_pmcs[ri].pp_pmc) != NULL &&
5468 		    (pm->pm_flags & PMC_F_DESCENDANTS) != 0) {
5469 			pmc_link_target_process(pm, ppnew);
5470 			po = pm->pm_owner;
5471 			if (po->po_sscount == 0 &&
5472 			    (po->po_flags & PMC_PO_OWNS_LOGFILE) != 0) {
5473 				pmclog_process_procfork(po, p1->p_pid,
5474 				    newproc->p_pid);
5475 			}
5476 		}
5477 	}
5478 
5479 done:
5480 	sx_xunlock(&pmc_sx);
5481 }
5482 
5483 static void
5484 pmc_process_threadcreate(struct thread *td)
5485 {
5486 	struct pmc_owner *po;
5487 
5488 	PMC_EPOCH_ENTER();
5489 	CK_LIST_FOREACH(po, &pmc_ss_owners, po_ssnext) {
5490 		if ((po->po_flags & PMC_PO_OWNS_LOGFILE) != 0)
5491 			pmclog_process_threadcreate(po, td, 1);
5492 	}
5493 	PMC_EPOCH_EXIT();
5494 }
5495 
5496 static void
5497 pmc_process_threadexit(struct thread *td)
5498 {
5499 	struct pmc_owner *po;
5500 
5501 	PMC_EPOCH_ENTER();
5502 	CK_LIST_FOREACH(po, &pmc_ss_owners, po_ssnext) {
5503 		if ((po->po_flags & PMC_PO_OWNS_LOGFILE) != 0)
5504 			pmclog_process_threadexit(po, td);
5505 	}
5506 	PMC_EPOCH_EXIT();
5507 }
5508 
5509 static void
5510 pmc_process_proccreate(struct proc *p)
5511 {
5512 	struct pmc_owner *po;
5513 
5514 	PMC_EPOCH_ENTER();
5515 	CK_LIST_FOREACH(po, &pmc_ss_owners, po_ssnext) {
5516 		if ((po->po_flags & PMC_PO_OWNS_LOGFILE) != 0)
5517 			pmclog_process_proccreate(po, p, 1 /* sync */);
5518 	}
5519 	PMC_EPOCH_EXIT();
5520 }
5521 
5522 static void
5523 pmc_process_allproc(struct pmc *pm)
5524 {
5525 	struct pmc_owner *po;
5526 	struct thread *td;
5527 	struct proc *p;
5528 
5529 	po = pm->pm_owner;
5530 	if ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0)
5531 		return;
5532 
5533 	sx_slock(&allproc_lock);
5534 	FOREACH_PROC_IN_SYSTEM(p) {
5535 		pmclog_process_proccreate(po, p, 0 /* sync */);
5536 		PROC_LOCK(p);
5537 		FOREACH_THREAD_IN_PROC(p, td)
5538 			pmclog_process_threadcreate(po, td, 0 /* sync */);
5539 		PROC_UNLOCK(p);
5540 	}
5541 	sx_sunlock(&allproc_lock);
5542 	pmclog_flush(po, 0);
5543 }
5544 
5545 static void
5546 pmc_kld_load(void *arg __unused, linker_file_t lf)
5547 {
5548 	struct pmc_owner *po;
5549 
5550 	/*
5551 	 * Notify owners of system sampling PMCs about KLD operations.
5552 	 */
5553 	PMC_EPOCH_ENTER();
5554 	CK_LIST_FOREACH(po, &pmc_ss_owners, po_ssnext) {
5555 		if (po->po_flags & PMC_PO_OWNS_LOGFILE)
5556 			pmclog_process_map_in(po, (pid_t) -1,
5557 			    (uintfptr_t) lf->address, lf->pathname);
5558 	}
5559 	PMC_EPOCH_EXIT();
5560 
5561 	/*
5562 	 * TODO: Notify owners of (all) process-sampling PMCs too.
5563 	 */
5564 }
5565 
5566 static void
5567 pmc_kld_unload(void *arg __unused, const char *filename __unused,
5568     caddr_t address, size_t size)
5569 {
5570 	struct pmc_owner *po;
5571 
5572 	PMC_EPOCH_ENTER();
5573 	CK_LIST_FOREACH(po, &pmc_ss_owners, po_ssnext) {
5574 		if ((po->po_flags & PMC_PO_OWNS_LOGFILE) != 0) {
5575 			pmclog_process_map_out(po, (pid_t)-1,
5576 			    (uintfptr_t)address, (uintfptr_t)address + size);
5577 		}
5578 	}
5579 	PMC_EPOCH_EXIT();
5580 
5581 	/*
5582 	 * TODO: Notify owners of process-sampling PMCs.
5583 	 */
5584 }
5585 
5586 /*
5587  * initialization
5588  */
5589 static const char *
5590 pmc_name_of_pmcclass(enum pmc_class class)
5591 {
5592 
5593 	switch (class) {
5594 #undef	__PMC_CLASS
5595 #define	__PMC_CLASS(S,V,D)						\
5596 	case PMC_CLASS_##S:						\
5597 		return #S;
5598 	__PMC_CLASSES();
5599 	default:
5600 		return ("<unknown>");
5601 	}
5602 }
5603 
5604 /*
5605  * Base class initializer: allocate structure and set default classes.
5606  */
5607 struct pmc_mdep *
5608 pmc_mdep_alloc(int nclasses)
5609 {
5610 	struct pmc_mdep *md;
5611 	int n;
5612 
5613 	/* SOFT + md classes */
5614 	n = 1 + nclasses;
5615 	md = malloc(sizeof(struct pmc_mdep) + n * sizeof(struct pmc_classdep),
5616 	    M_PMC, M_WAITOK | M_ZERO);
5617 	md->pmd_nclass = n;
5618 
5619 	/* Default methods */
5620 	md->pmd_switch_in = generic_switch_in;
5621 	md->pmd_switch_out = generic_switch_out;
5622 
5623 	/* Add base class. */
5624 	pmc_soft_initialize(md);
5625 	return (md);
5626 }
5627 
5628 void
5629 pmc_mdep_free(struct pmc_mdep *md)
5630 {
5631 	pmc_soft_finalize(md);
5632 	free(md, M_PMC);
5633 }
5634 
5635 static int
5636 generic_switch_in(struct pmc_cpu *pc __unused, struct pmc_process *pp __unused)
5637 {
5638 
5639 	return (0);
5640 }
5641 
5642 static int
5643 generic_switch_out(struct pmc_cpu *pc __unused, struct pmc_process *pp __unused)
5644 {
5645 
5646 	return (0);
5647 }
5648 
5649 static struct pmc_mdep *
5650 pmc_generic_cpu_initialize(void)
5651 {
5652 	struct pmc_mdep *md;
5653 
5654 	md = pmc_mdep_alloc(0);
5655 
5656 	md->pmd_cputype = PMC_CPU_GENERIC;
5657 
5658 	return (md);
5659 }
5660 
5661 static void
5662 pmc_generic_cpu_finalize(struct pmc_mdep *md __unused)
5663 {
5664 
5665 }
5666 
5667 static int
5668 pmc_initialize(void)
5669 {
5670 	struct pcpu *pc;
5671 	struct pmc_binding pb;
5672 	struct pmc_classdep *pcd;
5673 	struct pmc_sample *ps;
5674 	struct pmc_samplebuffer *sb;
5675 	int c, cpu, error, n, ri;
5676 	u_int maxcpu, domain;
5677 
5678 	md = NULL;
5679 	error = 0;
5680 
5681 	pmc_stats.pm_intr_ignored = counter_u64_alloc(M_WAITOK);
5682 	pmc_stats.pm_intr_processed = counter_u64_alloc(M_WAITOK);
5683 	pmc_stats.pm_intr_bufferfull = counter_u64_alloc(M_WAITOK);
5684 	pmc_stats.pm_syscalls = counter_u64_alloc(M_WAITOK);
5685 	pmc_stats.pm_syscall_errors = counter_u64_alloc(M_WAITOK);
5686 	pmc_stats.pm_buffer_requests = counter_u64_alloc(M_WAITOK);
5687 	pmc_stats.pm_buffer_requests_failed = counter_u64_alloc(M_WAITOK);
5688 	pmc_stats.pm_log_sweeps = counter_u64_alloc(M_WAITOK);
5689 	pmc_stats.pm_merges = counter_u64_alloc(M_WAITOK);
5690 	pmc_stats.pm_overwrites = counter_u64_alloc(M_WAITOK);
5691 
5692 #ifdef HWPMC_DEBUG
5693 	/* parse debug flags first */
5694 	if (TUNABLE_STR_FETCH(PMC_SYSCTL_NAME_PREFIX "debugflags",
5695 	    pmc_debugstr, sizeof(pmc_debugstr))) {
5696 		pmc_debugflags_parse(pmc_debugstr, pmc_debugstr +
5697 		    strlen(pmc_debugstr));
5698 	}
5699 #endif
5700 
5701 	PMCDBG1(MOD,INI,0, "PMC Initialize (version %x)", PMC_VERSION);
5702 
5703 	/* check kernel version */
5704 	if (pmc_kernel_version != PMC_VERSION) {
5705 		if (pmc_kernel_version == 0)
5706 			printf("hwpmc: this kernel has not been compiled with "
5707 			    "'options HWPMC_HOOKS'.\n");
5708 		else
5709 			printf("hwpmc: kernel version (0x%x) does not match "
5710 			    "module version (0x%x).\n", pmc_kernel_version,
5711 			    PMC_VERSION);
5712 		return (EPROGMISMATCH);
5713 	}
5714 
5715 	/*
5716 	 * check sysctl parameters
5717 	 */
5718 	if (pmc_hashsize <= 0) {
5719 		printf("hwpmc: tunable \"hashsize\"=%d must be "
5720 		    "greater than zero.\n", pmc_hashsize);
5721 		pmc_hashsize = PMC_HASH_SIZE;
5722 	}
5723 
5724 	if (pmc_nsamples <= 0 || pmc_nsamples > 65535) {
5725 		printf("hwpmc: tunable \"nsamples\"=%d out of "
5726 		    "range.\n", pmc_nsamples);
5727 		pmc_nsamples = PMC_NSAMPLES;
5728 	}
5729 	pmc_sample_mask = pmc_nsamples - 1;
5730 
5731 	if (pmc_callchaindepth <= 0 ||
5732 	    pmc_callchaindepth > PMC_CALLCHAIN_DEPTH_MAX) {
5733 		printf("hwpmc: tunable \"callchaindepth\"=%d out of "
5734 		    "range - using %d.\n", pmc_callchaindepth,
5735 		    PMC_CALLCHAIN_DEPTH_MAX);
5736 		pmc_callchaindepth = PMC_CALLCHAIN_DEPTH_MAX;
5737 	}
5738 
5739 	md = pmc_md_initialize();
5740 	if (md == NULL) {
5741 		/* Default to generic CPU. */
5742 		md = pmc_generic_cpu_initialize();
5743 		if (md == NULL)
5744 			return (ENOSYS);
5745         }
5746 
5747 	/*
5748 	 * Refresh classes base ri. Optional classes may come in different
5749 	 * order.
5750 	 */
5751 	for (ri = c = 0; c < md->pmd_nclass; c++) {
5752 		pcd = &md->pmd_classdep[c];
5753 		pcd->pcd_ri = ri;
5754 		ri += pcd->pcd_num;
5755 	}
5756 
5757 	KASSERT(md->pmd_nclass >= 1 && md->pmd_npmc >= 1,
5758 	    ("[pmc,%d] no classes or pmcs", __LINE__));
5759 
5760 	/* Compute the map from row-indices to classdep pointers. */
5761 	pmc_rowindex_to_classdep = malloc(sizeof(struct pmc_classdep *) *
5762 	    md->pmd_npmc, M_PMC, M_WAITOK | M_ZERO);
5763 
5764 	for (n = 0; n < md->pmd_npmc; n++)
5765 		pmc_rowindex_to_classdep[n] = NULL;
5766 
5767 	for (ri = c = 0; c < md->pmd_nclass; c++) {
5768 		pcd = &md->pmd_classdep[c];
5769 		for (n = 0; n < pcd->pcd_num; n++, ri++)
5770 			pmc_rowindex_to_classdep[ri] = pcd;
5771 	}
5772 
5773 	KASSERT(ri == md->pmd_npmc,
5774 	    ("[pmc,%d] npmc miscomputed: ri=%d, md->npmc=%d", __LINE__,
5775 	    ri, md->pmd_npmc));
5776 
5777 	maxcpu = pmc_cpu_max();
5778 
5779 	/* allocate space for the per-cpu array */
5780 	pmc_pcpu = malloc(maxcpu * sizeof(struct pmc_cpu *), M_PMC,
5781 	    M_WAITOK | M_ZERO);
5782 
5783 	/* per-cpu 'saved values' for managing process-mode PMCs */
5784 	pmc_pcpu_saved = malloc(sizeof(pmc_value_t) * maxcpu * md->pmd_npmc,
5785 	    M_PMC, M_WAITOK);
5786 
5787 	/* Perform CPU-dependent initialization. */
5788 	pmc_save_cpu_binding(&pb);
5789 	error = 0;
5790 	for (cpu = 0; error == 0 && cpu < maxcpu; cpu++) {
5791 		if (!pmc_cpu_is_active(cpu))
5792 			continue;
5793 		pmc_select_cpu(cpu);
5794 		pmc_pcpu[cpu] = malloc(sizeof(struct pmc_cpu) +
5795 		    md->pmd_npmc * sizeof(struct pmc_hw *), M_PMC,
5796 		    M_WAITOK | M_ZERO);
5797 		for (n = 0; error == 0 && n < md->pmd_nclass; n++)
5798 			if (md->pmd_classdep[n].pcd_num > 0)
5799 				error = md->pmd_classdep[n].pcd_pcpu_init(md,
5800 				    cpu);
5801 	}
5802 	pmc_restore_cpu_binding(&pb);
5803 
5804 	if (error != 0)
5805 		return (error);
5806 
5807 	/* allocate space for the sample array */
5808 	for (cpu = 0; cpu < maxcpu; cpu++) {
5809 		if (!pmc_cpu_is_active(cpu))
5810 			continue;
5811 		pc = pcpu_find(cpu);
5812 		domain = pc->pc_domain;
5813 		sb = malloc_domainset(sizeof(struct pmc_samplebuffer) +
5814 		    pmc_nsamples * sizeof(struct pmc_sample), M_PMC,
5815 		    DOMAINSET_PREF(domain), M_WAITOK | M_ZERO);
5816 
5817 		KASSERT(pmc_pcpu[cpu] != NULL,
5818 		    ("[pmc,%d] cpu=%d Null per-cpu data", __LINE__, cpu));
5819 
5820 		sb->ps_callchains = malloc_domainset(pmc_callchaindepth *
5821 		    pmc_nsamples * sizeof(uintptr_t), M_PMC,
5822 		    DOMAINSET_PREF(domain), M_WAITOK | M_ZERO);
5823 
5824 		for (n = 0, ps = sb->ps_samples; n < pmc_nsamples; n++, ps++)
5825 			ps->ps_pc = sb->ps_callchains +
5826 			    (n * pmc_callchaindepth);
5827 
5828 		pmc_pcpu[cpu]->pc_sb[PMC_HR] = sb;
5829 
5830 		sb = malloc_domainset(sizeof(struct pmc_samplebuffer) +
5831 		    pmc_nsamples * sizeof(struct pmc_sample), M_PMC,
5832 		    DOMAINSET_PREF(domain), M_WAITOK | M_ZERO);
5833 
5834 		sb->ps_callchains = malloc_domainset(pmc_callchaindepth *
5835 		    pmc_nsamples * sizeof(uintptr_t), M_PMC,
5836 		    DOMAINSET_PREF(domain), M_WAITOK | M_ZERO);
5837 		for (n = 0, ps = sb->ps_samples; n < pmc_nsamples; n++, ps++)
5838 			ps->ps_pc = sb->ps_callchains +
5839 			    (n * pmc_callchaindepth);
5840 
5841 		pmc_pcpu[cpu]->pc_sb[PMC_SR] = sb;
5842 
5843 		sb = malloc_domainset(sizeof(struct pmc_samplebuffer) +
5844 		    pmc_nsamples * sizeof(struct pmc_sample), M_PMC,
5845 		    DOMAINSET_PREF(domain), M_WAITOK | M_ZERO);
5846 		sb->ps_callchains = malloc_domainset(pmc_callchaindepth *
5847 		    pmc_nsamples * sizeof(uintptr_t), M_PMC,
5848 		    DOMAINSET_PREF(domain), M_WAITOK | M_ZERO);
5849 		for (n = 0, ps = sb->ps_samples; n < pmc_nsamples; n++, ps++)
5850 			ps->ps_pc = sb->ps_callchains + n * pmc_callchaindepth;
5851 
5852 		pmc_pcpu[cpu]->pc_sb[PMC_UR] = sb;
5853 	}
5854 
5855 	/* allocate space for the row disposition array */
5856 	pmc_pmcdisp = malloc(sizeof(enum pmc_mode) * md->pmd_npmc,
5857 	    M_PMC, M_WAITOK | M_ZERO);
5858 
5859 	/* mark all PMCs as available */
5860 	for (n = 0; n < md->pmd_npmc; n++)
5861 		PMC_MARK_ROW_FREE(n);
5862 
5863 	/* allocate thread hash tables */
5864 	pmc_ownerhash = hashinit(pmc_hashsize, M_PMC,
5865 	    &pmc_ownerhashmask);
5866 
5867 	pmc_processhash = hashinit(pmc_hashsize, M_PMC,
5868 	    &pmc_processhashmask);
5869 	mtx_init(&pmc_processhash_mtx, "pmc-process-hash", "pmc-leaf",
5870 	    MTX_SPIN);
5871 
5872 	CK_LIST_INIT(&pmc_ss_owners);
5873 	pmc_ss_count = 0;
5874 
5875 	/* allocate a pool of spin mutexes */
5876 	pmc_mtxpool = mtx_pool_create("pmc-leaf", pmc_mtxpool_size,
5877 	    MTX_SPIN);
5878 
5879 	PMCDBG4(MOD,INI,1, "pmc_ownerhash=%p, mask=0x%lx "
5880 	    "targethash=%p mask=0x%lx", pmc_ownerhash, pmc_ownerhashmask,
5881 	    pmc_processhash, pmc_processhashmask);
5882 
5883 	/* Initialize a spin mutex for the thread free list. */
5884 	mtx_init(&pmc_threadfreelist_mtx, "pmc-threadfreelist", "pmc-leaf",
5885 	    MTX_SPIN);
5886 
5887 	/* Initialize the task to prune the thread free list. */
5888 	TASK_INIT(&free_task, 0, pmc_thread_descriptor_pool_free_task, NULL);
5889 
5890 	/* register process {exit,fork,exec} handlers */
5891 	pmc_exit_tag = EVENTHANDLER_REGISTER(process_exit,
5892 	    pmc_process_exit, NULL, EVENTHANDLER_PRI_ANY);
5893 	pmc_fork_tag = EVENTHANDLER_REGISTER(process_fork,
5894 	    pmc_process_fork, NULL, EVENTHANDLER_PRI_ANY);
5895 
5896 	/* register kld event handlers */
5897 	pmc_kld_load_tag = EVENTHANDLER_REGISTER(kld_load, pmc_kld_load,
5898 	    NULL, EVENTHANDLER_PRI_ANY);
5899 	pmc_kld_unload_tag = EVENTHANDLER_REGISTER(kld_unload, pmc_kld_unload,
5900 	    NULL, EVENTHANDLER_PRI_ANY);
5901 
5902 	/* initialize logging */
5903 	pmclog_initialize();
5904 
5905 	/* set hook functions */
5906 	pmc_intr = md->pmd_intr;
5907 	wmb();
5908 	pmc_hook = pmc_hook_handler;
5909 
5910 	if (error == 0) {
5911 		printf(PMC_MODULE_NAME ":");
5912 		for (n = 0; n < md->pmd_nclass; n++) {
5913 			if (md->pmd_classdep[n].pcd_num == 0)
5914 				continue;
5915 			pcd = &md->pmd_classdep[n];
5916 			printf(" %s/%d/%d/0x%b",
5917 			    pmc_name_of_pmcclass(pcd->pcd_class),
5918 			    pcd->pcd_num,
5919 			    pcd->pcd_width,
5920 			    pcd->pcd_caps,
5921 			    "\20"
5922 			    "\1INT\2USR\3SYS\4EDG\5THR"
5923 			    "\6REA\7WRI\10INV\11QUA\12PRC"
5924 			    "\13TAG\14CSC");
5925 		}
5926 		printf("\n");
5927 	}
5928 
5929 	return (error);
5930 }
5931 
5932 /* prepare to be unloaded */
5933 static void
5934 pmc_cleanup(void)
5935 {
5936 	struct pmc_binding pb;
5937 	struct pmc_owner *po, *tmp;
5938 	struct pmc_ownerhash *ph;
5939 	struct pmc_processhash *prh __pmcdbg_used;
5940 	u_int maxcpu;
5941 	int cpu, c;
5942 
5943 	PMCDBG0(MOD,INI,0, "cleanup");
5944 
5945 	/* switch off sampling */
5946 	CPU_FOREACH(cpu)
5947 		DPCPU_ID_SET(cpu, pmc_sampled, 0);
5948 	pmc_intr = NULL;
5949 
5950 	sx_xlock(&pmc_sx);
5951 	if (pmc_hook == NULL) {	/* being unloaded already */
5952 		sx_xunlock(&pmc_sx);
5953 		return;
5954 	}
5955 
5956 	pmc_hook = NULL; /* prevent new threads from entering module */
5957 
5958 	/* deregister event handlers */
5959 	EVENTHANDLER_DEREGISTER(process_fork, pmc_fork_tag);
5960 	EVENTHANDLER_DEREGISTER(process_exit, pmc_exit_tag);
5961 	EVENTHANDLER_DEREGISTER(kld_load, pmc_kld_load_tag);
5962 	EVENTHANDLER_DEREGISTER(kld_unload, pmc_kld_unload_tag);
5963 
5964 	/* send SIGBUS to all owner threads, free up allocations */
5965 	if (pmc_ownerhash != NULL) {
5966 		for (ph = pmc_ownerhash;
5967 		     ph <= &pmc_ownerhash[pmc_ownerhashmask];
5968 		     ph++) {
5969 			LIST_FOREACH_SAFE(po, ph, po_next, tmp) {
5970 				pmc_remove_owner(po);
5971 
5972 				PMCDBG3(MOD,INI,2,
5973 				    "cleanup signal proc=%p (%d, %s)",
5974 				    po->po_owner, po->po_owner->p_pid,
5975 				    po->po_owner->p_comm);
5976 
5977 				PROC_LOCK(po->po_owner);
5978 				kern_psignal(po->po_owner, SIGBUS);
5979 				PROC_UNLOCK(po->po_owner);
5980 
5981 				pmc_destroy_owner_descriptor(po);
5982 			}
5983 		}
5984 	}
5985 
5986 	/* reclaim allocated data structures */
5987 	taskqueue_drain(taskqueue_fast, &free_task);
5988 	mtx_destroy(&pmc_threadfreelist_mtx);
5989 	pmc_thread_descriptor_pool_drain();
5990 
5991 	if (pmc_mtxpool != NULL)
5992 		mtx_pool_destroy(&pmc_mtxpool);
5993 
5994 	mtx_destroy(&pmc_processhash_mtx);
5995 	if (pmc_processhash != NULL) {
5996 #ifdef HWPMC_DEBUG
5997 		struct pmc_process *pp;
5998 
5999 		PMCDBG0(MOD,INI,3, "destroy process hash");
6000 		for (prh = pmc_processhash;
6001 		     prh <= &pmc_processhash[pmc_processhashmask];
6002 		     prh++)
6003 			LIST_FOREACH(pp, prh, pp_next)
6004 			    PMCDBG1(MOD,INI,3, "pid=%d", pp->pp_proc->p_pid);
6005 #endif
6006 
6007 		hashdestroy(pmc_processhash, M_PMC, pmc_processhashmask);
6008 		pmc_processhash = NULL;
6009 	}
6010 
6011 	if (pmc_ownerhash != NULL) {
6012 		PMCDBG0(MOD,INI,3, "destroy owner hash");
6013 		hashdestroy(pmc_ownerhash, M_PMC, pmc_ownerhashmask);
6014 		pmc_ownerhash = NULL;
6015 	}
6016 
6017 	KASSERT(CK_LIST_EMPTY(&pmc_ss_owners),
6018 	    ("[pmc,%d] Global SS owner list not empty", __LINE__));
6019 	KASSERT(pmc_ss_count == 0,
6020 	    ("[pmc,%d] Global SS count not empty", __LINE__));
6021 
6022  	/* do processor and pmc-class dependent cleanup */
6023 	maxcpu = pmc_cpu_max();
6024 
6025 	PMCDBG0(MOD,INI,3, "md cleanup");
6026 	if (md) {
6027 		pmc_save_cpu_binding(&pb);
6028 		for (cpu = 0; cpu < maxcpu; cpu++) {
6029 			PMCDBG2(MOD,INI,1,"pmc-cleanup cpu=%d pcs=%p",
6030 			    cpu, pmc_pcpu[cpu]);
6031 			if (!pmc_cpu_is_active(cpu) || pmc_pcpu[cpu] == NULL)
6032 				continue;
6033 
6034 			pmc_select_cpu(cpu);
6035 			for (c = 0; c < md->pmd_nclass; c++) {
6036 				if (md->pmd_classdep[c].pcd_num > 0) {
6037 					md->pmd_classdep[c].pcd_pcpu_fini(md,
6038 					    cpu);
6039 				}
6040 			}
6041 		}
6042 
6043 		if (md->pmd_cputype == PMC_CPU_GENERIC)
6044 			pmc_generic_cpu_finalize(md);
6045 		else
6046 			pmc_md_finalize(md);
6047 
6048 		pmc_mdep_free(md);
6049 		md = NULL;
6050 		pmc_restore_cpu_binding(&pb);
6051 	}
6052 
6053 	/* Free per-cpu descriptors. */
6054 	for (cpu = 0; cpu < maxcpu; cpu++) {
6055 		if (!pmc_cpu_is_active(cpu))
6056 			continue;
6057 		KASSERT(pmc_pcpu[cpu]->pc_sb[PMC_HR] != NULL,
6058 		    ("[pmc,%d] Null hw cpu sample buffer cpu=%d", __LINE__,
6059 			cpu));
6060 		KASSERT(pmc_pcpu[cpu]->pc_sb[PMC_SR] != NULL,
6061 		    ("[pmc,%d] Null sw cpu sample buffer cpu=%d", __LINE__,
6062 			cpu));
6063 		KASSERT(pmc_pcpu[cpu]->pc_sb[PMC_UR] != NULL,
6064 		    ("[pmc,%d] Null userret cpu sample buffer cpu=%d", __LINE__,
6065 			cpu));
6066 		free(pmc_pcpu[cpu]->pc_sb[PMC_HR]->ps_callchains, M_PMC);
6067 		free(pmc_pcpu[cpu]->pc_sb[PMC_HR], M_PMC);
6068 		free(pmc_pcpu[cpu]->pc_sb[PMC_SR]->ps_callchains, M_PMC);
6069 		free(pmc_pcpu[cpu]->pc_sb[PMC_SR], M_PMC);
6070 		free(pmc_pcpu[cpu]->pc_sb[PMC_UR]->ps_callchains, M_PMC);
6071 		free(pmc_pcpu[cpu]->pc_sb[PMC_UR], M_PMC);
6072 		free(pmc_pcpu[cpu], M_PMC);
6073 	}
6074 
6075 	free(pmc_pcpu, M_PMC);
6076 	pmc_pcpu = NULL;
6077 
6078 	free(pmc_pcpu_saved, M_PMC);
6079 	pmc_pcpu_saved = NULL;
6080 
6081 	if (pmc_pmcdisp != NULL) {
6082 		free(pmc_pmcdisp, M_PMC);
6083 		pmc_pmcdisp = NULL;
6084 	}
6085 
6086 	if (pmc_rowindex_to_classdep != NULL) {
6087 		free(pmc_rowindex_to_classdep, M_PMC);
6088 		pmc_rowindex_to_classdep = NULL;
6089 	}
6090 
6091 	pmclog_shutdown();
6092 	counter_u64_free(pmc_stats.pm_intr_ignored);
6093 	counter_u64_free(pmc_stats.pm_intr_processed);
6094 	counter_u64_free(pmc_stats.pm_intr_bufferfull);
6095 	counter_u64_free(pmc_stats.pm_syscalls);
6096 	counter_u64_free(pmc_stats.pm_syscall_errors);
6097 	counter_u64_free(pmc_stats.pm_buffer_requests);
6098 	counter_u64_free(pmc_stats.pm_buffer_requests_failed);
6099 	counter_u64_free(pmc_stats.pm_log_sweeps);
6100 	counter_u64_free(pmc_stats.pm_merges);
6101 	counter_u64_free(pmc_stats.pm_overwrites);
6102 	sx_xunlock(&pmc_sx);	/* we are done */
6103 }
6104 
6105 /*
6106  * The function called at load/unload.
6107  */
6108 static int
6109 load(struct module *module __unused, int cmd, void *arg __unused)
6110 {
6111 	int error;
6112 
6113 	error = 0;
6114 
6115 	switch (cmd) {
6116 	case MOD_LOAD:
6117 		/* initialize the subsystem */
6118 		error = pmc_initialize();
6119 		if (error != 0)
6120 			break;
6121 		PMCDBG2(MOD,INI,1, "syscall=%d maxcpu=%d", pmc_syscall_num,
6122 		    pmc_cpu_max());
6123 		break;
6124 	case MOD_UNLOAD:
6125 	case MOD_SHUTDOWN:
6126 		pmc_cleanup();
6127 		PMCDBG0(MOD,INI,1, "unloaded");
6128 		break;
6129 	default:
6130 		error = EINVAL;
6131 		break;
6132 	}
6133 
6134 	return (error);
6135 }
6136