2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2003-2007, Joseph Koshy
5 * Copyright (c) 2007 The FreeBSD Foundation
8 * Portions of this software were developed by A. Joseph Koshy under
9 * sponsorship from the FreeBSD Foundation and Google, Inc.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * PMC interface used by the base kernel.
39 #ifndef _SYS_PMCKERN_H_
40 #define _SYS_PMCKERN_H_
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
50 #include <machine/cpufunc.h>
52 #define PMC_FN_PROCESS_EXEC 1
53 #define PMC_FN_CSW_IN 2
54 #define PMC_FN_CSW_OUT 3
55 #define PMC_FN_DO_SAMPLES 4
56 #define PMC_FN_UNUSED1 5
57 #define PMC_FN_UNUSED2 6
59 #define PMC_FN_MUNMAP 8
60 #define PMC_FN_USER_CALLCHAIN 9
61 #define PMC_FN_USER_CALLCHAIN_SOFT 10
62 #define PMC_FN_SOFT_SAMPLING 11
64 #define PMC_HR 0 /* Hardware ring buffer */
65 #define PMC_SR 1 /* Software ring buffer */
67 struct pmckern_procexec {
68 int pm_credentialschanged;
69 uintfptr_t pm_entryaddr;
72 struct pmckern_map_in {
73 void *pm_file; /* filename or vnode pointer */
74 uintfptr_t pm_address; /* address object is loaded at */
77 struct pmckern_map_out {
78 uintfptr_t pm_address; /* start address of region */
79 size_t pm_size; /* size of unmapped region */
85 struct trapframe *pm_tf;
92 #define PMC_SOFT_DEFINE_EX(prov, mod, func, name, alloc, release) \
93 struct pmc_soft pmc_##prov##_##mod##_##func##_##name = \
94 { 0, alloc, release, { #prov "_" #mod "_" #func "." #name, 0 } }; \
95 SYSINIT(pmc_##prov##_##mod##_##func##_##name##_init, SI_SUB_KDTRACE, \
96 SI_ORDER_SECOND + 1, pmc_soft_ev_register, \
97 &pmc_##prov##_##mod##_##func##_##name ); \
98 SYSUNINIT(pmc_##prov##_##mod##_##func##_##name##_uninit, \
99 SI_SUB_KDTRACE, SI_ORDER_SECOND + 1, pmc_soft_ev_deregister, \
100 &pmc_##prov##_##mod##_##func##_##name )
102 #define PMC_SOFT_DEFINE(prov, mod, func, name) \
103 PMC_SOFT_DEFINE_EX(prov, mod, func, name, NULL, NULL)
105 #define PMC_SOFT_DECLARE(prov, mod, func, name) \
106 extern struct pmc_soft pmc_##prov##_##mod##_##func##_##name
109 * PMC_SOFT_CALL can be used anywhere in the kernel.
110 * Require md defined PMC_FAKE_TRAPFRAME.
112 #ifdef PMC_FAKE_TRAPFRAME
113 #define PMC_SOFT_CALL(pr, mo, fu, na) \
115 if (__predict_false(pmc_##pr##_##mo##_##fu##_##na.ps_running)) { \
116 struct pmckern_soft ks; \
118 intr = intr_disable(); \
119 PMC_FAKE_TRAPFRAME(&pmc_tf[curcpu]); \
120 ks.pm_ev = pmc_##pr##_##mo##_##fu##_##na.ps_ev.pm_ev_code; \
121 ks.pm_cpu = PCPU_GET(cpuid); \
122 ks.pm_tf = &pmc_tf[curcpu]; \
123 PMC_CALL_HOOK_UNLOCKED(curthread, \
124 PMC_FN_SOFT_SAMPLING, (void *) &ks); \
125 intr_restore(intr); \
129 #define PMC_SOFT_CALL(pr, mo, fu, na) \
135 * PMC_SOFT_CALL_TF need to be used carefully.
136 * Userland capture will be done during AST processing.
138 #define PMC_SOFT_CALL_TF(pr, mo, fu, na, tf) \
140 if (__predict_false(pmc_##pr##_##mo##_##fu##_##na.ps_running)) { \
141 struct pmckern_soft ks; \
143 intr = intr_disable(); \
144 ks.pm_ev = pmc_##pr##_##mo##_##fu##_##na.ps_ev.pm_ev_code; \
145 ks.pm_cpu = PCPU_GET(cpuid); \
147 PMC_CALL_HOOK_UNLOCKED(curthread, \
148 PMC_FN_SOFT_SAMPLING, (void *) &ks); \
149 intr_restore(intr); \
155 void (*ps_alloc)(void);
156 void (*ps_release)(void);
157 struct pmc_dyn_event_descr ps_ev;
161 extern int (*pmc_hook)(struct thread *_td, int _function, void *_arg);
162 extern int (*pmc_intr)(int _cpu, struct trapframe *_frame);
164 /* SX lock protecting the hook */
165 extern struct sx pmc_sx;
167 /* Per-cpu flags indicating availability of sampling data */
168 extern volatile cpuset_t pmc_cpumask;
170 /* Count of system-wide sampling PMCs in existence */
171 extern volatile int pmc_ss_count;
173 /* kernel version number */
174 extern const int pmc_kernel_version;
176 /* PMC soft per cpu trapframe */
177 extern struct trapframe pmc_tf[MAXCPU];
179 /* Quick check if preparatory work is necessary */
180 #define PMC_HOOK_INSTALLED(cmd) __predict_false(pmc_hook != NULL)
182 /* Hook invocation; for use within the kernel */
183 #define PMC_CALL_HOOK(t, cmd, arg) \
186 if (pmc_hook != NULL) \
187 (pmc_hook)((t), (cmd), (arg)); \
188 sx_sunlock(&pmc_sx); \
191 /* Hook invocation that needs an exclusive lock */
192 #define PMC_CALL_HOOK_X(t, cmd, arg) \
195 if (pmc_hook != NULL) \
196 (pmc_hook)((t), (cmd), (arg)); \
197 sx_xunlock(&pmc_sx); \
201 * Some hook invocations (e.g., from context switch and clock handling
202 * code) need to be lock-free.
204 #define PMC_CALL_HOOK_UNLOCKED(t, cmd, arg) \
206 if (pmc_hook != NULL) \
207 (pmc_hook)((t), (cmd), (arg)); \
210 #define PMC_SWITCH_CONTEXT(t,cmd) PMC_CALL_HOOK_UNLOCKED(t,cmd,NULL)
212 /* Check if a process is using HWPMCs.*/
213 #define PMC_PROC_IS_USING_PMCS(p) \
214 (__predict_false(p->p_flag & P_HWPMC))
216 /* Check if a thread have pending user capture. */
217 #define PMC_IS_PENDING_CALLCHAIN(p) \
218 (__predict_false((p)->td_pflags & TDP_CALLCHAIN))
220 #define PMC_SYSTEM_SAMPLING_ACTIVE() (pmc_ss_count > 0)
222 /* Check if a CPU has recorded samples. */
223 #define PMC_CPU_HAS_SAMPLES(C) (__predict_false(CPU_ISSET(C, &pmc_cpumask)))
228 int pmc_cpu_is_disabled(int _cpu); /* deprecated */
229 int pmc_cpu_is_active(int _cpu);
230 int pmc_cpu_is_present(int _cpu);
231 int pmc_cpu_is_primary(int _cpu);
232 unsigned int pmc_cpu_max(void);
235 int pmc_cpu_max_active(void);
239 * Soft events functions.
241 void pmc_soft_ev_register(struct pmc_soft *ps);
242 void pmc_soft_ev_deregister(struct pmc_soft *ps);
243 struct pmc_soft *pmc_soft_ev_acquire(enum pmc_event ev);
244 void pmc_soft_ev_release(struct pmc_soft *ps);
246 #endif /* _SYS_PMCKERN_H_ */