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