]> CyberLeo.Net >> Repos - FreeBSD/releng/9.1.git/blob - sys/sys/pmckern.h
Fix Denial of Service vulnerability in named(8). [13:07]
[FreeBSD/releng/9.1.git] / sys / sys / pmckern.h
1 /*-
2  * Copyright (c) 2003-2007, Joseph Koshy
3  * Copyright (c) 2007 The FreeBSD Foundation
4  * All rights reserved.
5  *
6  * Portions of this software were developed by A. Joseph Koshy under
7  * sponsorship from the FreeBSD Foundation and Google, Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * $FreeBSD$
31  */
32
33 /*
34  * PMC interface used by the base kernel.
35  */
36
37 #ifndef _SYS_PMCKERN_H_
38 #define _SYS_PMCKERN_H_
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/lock.h>
44 #include <sys/proc.h>
45 #include <sys/sx.h>
46 #include <sys/pmc.h>
47
48 #include <machine/cpufunc.h>
49
50 #define PMC_FN_PROCESS_EXEC             1
51 #define PMC_FN_CSW_IN                   2
52 #define PMC_FN_CSW_OUT                  3
53 #define PMC_FN_DO_SAMPLES               4
54 #define PMC_FN_KLD_LOAD                 5
55 #define PMC_FN_KLD_UNLOAD               6
56 #define PMC_FN_MMAP                     7
57 #define PMC_FN_MUNMAP                   8
58 #define PMC_FN_USER_CALLCHAIN           9
59 #define PMC_FN_USER_CALLCHAIN_SOFT      10
60 #define PMC_FN_SOFT_SAMPLING            11
61
62 #define PMC_HR  0       /* Hardware ring buffer */
63 #define PMC_SR  1       /* Software ring buffer */
64
65 struct pmckern_procexec {
66         int             pm_credentialschanged;
67         uintfptr_t      pm_entryaddr;
68 };
69
70 struct pmckern_map_in {
71         void            *pm_file;       /* filename or vnode pointer */
72         uintfptr_t      pm_address;     /* address object is loaded at */
73 };
74
75 struct pmckern_map_out {
76         uintfptr_t      pm_address;     /* start address of region */
77         size_t          pm_size;        /* size of unmapped region */
78 };
79
80 struct pmckern_soft {
81         enum pmc_event          pm_ev;
82         int                     pm_cpu;
83         struct trapframe        *pm_tf;
84 };
85
86 /*
87  * Soft PMC.
88  */
89
90 #define PMC_SOFT_DEFINE(prov, mod, func, name)                                  \
91         struct pmc_soft pmc_##prov##_##mod##_##func##_##name =                  \
92             { 0, { #prov "_" #mod "_" #func "." #name, 0 } };                   \
93         SYSINIT(pmc_##prov##_##mod##_##func##_##name##_init, SI_SUB_KDTRACE,    \
94             SI_ORDER_SECOND + 1, pmc_soft_ev_register,                          \
95             &pmc_##prov##_##mod##_##func##_##name );                            \
96         SYSUNINIT(pmc_##prov##_##mod##_##func##_##name##_uninit,                \
97             SI_SUB_KDTRACE, SI_ORDER_SECOND + 1, pmc_soft_ev_deregister,        \
98             &pmc_##prov##_##mod##_##func##_##name )
99
100 #define PMC_SOFT_DECLARE(prov, mod, func, name)                                 \
101         extern struct pmc_soft pmc_##prov##_##mod##_##func##_##name
102
103 /*
104  * PMC_SOFT_CALL can be used anywhere in the kernel.
105  * Require md defined PMC_FAKE_TRAPFRAME.
106  */
107 #ifdef PMC_FAKE_TRAPFRAME
108 #define PMC_SOFT_CALL(pr, mo, fu, na)                                           \
109 do {                                                                            \
110         if (pmc_##pr##_##mo##_##fu##_##na.ps_running) {                         \
111                 struct pmckern_soft ks;                                         \
112                 register_t intr;                                                \
113                 intr = intr_disable();                                          \
114                 PMC_FAKE_TRAPFRAME(&pmc_tf[curcpu]);                            \
115                 ks.pm_ev = pmc_##pr##_##mo##_##fu##_##na.ps_ev.pm_ev_code;      \
116                 ks.pm_cpu = PCPU_GET(cpuid);                                    \
117                 ks.pm_tf = &pmc_tf[curcpu];                                     \
118                 PMC_CALL_HOOK_UNLOCKED(curthread,                               \
119                     PMC_FN_SOFT_SAMPLING, (void *) &ks);                        \
120                 intr_restore(intr);                                             \
121         }                                                                       \
122 } while (0)
123 #else
124 #define PMC_SOFT_CALL(pr, mo, fu, na)                                           \
125 do {                                                                            \
126 } while (0)
127 #endif
128
129 /*
130  * PMC_SOFT_CALL_TF need to be used carefully.
131  * Userland capture will be done during AST processing.
132  */
133 #define PMC_SOFT_CALL_TF(pr, mo, fu, na, tf)                                    \
134 do {                                                                            \
135         if (pmc_##pr##_##mo##_##fu##_##na.ps_running) {                         \
136                 struct pmckern_soft ks;                                         \
137                 register_t intr;                                                \
138                 intr = intr_disable();                                          \
139                 ks.pm_ev = pmc_##pr##_##mo##_##fu##_##na.ps_ev.pm_ev_code;      \
140                 ks.pm_cpu = PCPU_GET(cpuid);                                    \
141                 ks.pm_tf = tf;                                                  \
142                 PMC_CALL_HOOK_UNLOCKED(curthread,                               \
143                     PMC_FN_SOFT_SAMPLING, (void *) &ks);                        \
144                 intr_restore(intr);                                             \
145         }                                                                       \
146 } while (0)
147
148 struct pmc_soft {
149         int                             ps_running;
150         struct pmc_dyn_event_descr      ps_ev;
151 };
152
153 /* hook */
154 extern int (*pmc_hook)(struct thread *_td, int _function, void *_arg);
155 extern int (*pmc_intr)(int _cpu, struct trapframe *_frame);
156
157 /* SX lock protecting the hook */
158 extern struct sx pmc_sx;
159
160 /* Per-cpu flags indicating availability of sampling data */
161 extern volatile cpuset_t pmc_cpumask;
162
163 /* Count of system-wide sampling PMCs in existence */
164 extern volatile int pmc_ss_count;
165
166 /* kernel version number */
167 extern const int pmc_kernel_version;
168
169 /* PMC soft per cpu trapframe */
170 extern struct trapframe pmc_tf[MAXCPU];
171
172 /* Hook invocation; for use within the kernel */
173 #define PMC_CALL_HOOK(t, cmd, arg)              \
174 do {                                            \
175         sx_slock(&pmc_sx);                      \
176         if (pmc_hook != NULL)                   \
177                 (pmc_hook)((t), (cmd), (arg));  \
178         sx_sunlock(&pmc_sx);                    \
179 } while (0)
180
181 /* Hook invocation that needs an exclusive lock */
182 #define PMC_CALL_HOOK_X(t, cmd, arg)            \
183 do {                                            \
184         sx_xlock(&pmc_sx);                      \
185         if (pmc_hook != NULL)                   \
186                 (pmc_hook)((t), (cmd), (arg));  \
187         sx_xunlock(&pmc_sx);                    \
188 } while (0)
189
190 /*
191  * Some hook invocations (e.g., from context switch and clock handling
192  * code) need to be lock-free.
193  */
194 #define PMC_CALL_HOOK_UNLOCKED(t, cmd, arg)     \
195 do {                                            \
196         if (pmc_hook != NULL)                   \
197                 (pmc_hook)((t), (cmd), (arg));  \
198 } while (0)
199
200 #define PMC_SWITCH_CONTEXT(t,cmd)       PMC_CALL_HOOK_UNLOCKED(t,cmd,NULL)
201
202 /* Check if a process is using HWPMCs.*/
203 #define PMC_PROC_IS_USING_PMCS(p)                               \
204         (__predict_false(atomic_load_acq_int(&(p)->p_flag) &    \
205             P_HWPMC))
206
207 /* Check if a thread have pending user capture. */
208 #define PMC_IS_PENDING_CALLCHAIN(p)                             \
209         (__predict_false((p)->td_pflags & TDP_CALLCHAIN))
210
211 #define PMC_SYSTEM_SAMPLING_ACTIVE()            (pmc_ss_count > 0)
212
213 /* Check if a CPU has recorded samples. */
214 #define PMC_CPU_HAS_SAMPLES(C)  (__predict_false(CPU_ISSET(C, &pmc_cpumask)))
215
216 /*
217  * Helper functions.
218  */
219 int             pmc_cpu_is_disabled(int _cpu);  /* deprecated */
220 int             pmc_cpu_is_active(int _cpu);
221 int             pmc_cpu_is_present(int _cpu);
222 int             pmc_cpu_is_primary(int _cpu);
223 unsigned int    pmc_cpu_max(void);
224
225 #ifdef  INVARIANTS
226 int             pmc_cpu_max_active(void);
227 #endif
228
229 /*
230  * Soft events functions.
231  */
232 void pmc_soft_ev_register(struct pmc_soft *ps);
233 void pmc_soft_ev_deregister(struct pmc_soft *ps);
234 struct pmc_soft *pmc_soft_ev_acquire(enum pmc_event ev);
235 void pmc_soft_ev_release(struct pmc_soft *ps);
236
237 #endif /* _SYS_PMCKERN_H_ */