pmckern.h revision 174395
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: head/sys/sys/pmckern.h 174395 2007-12-07 08:20:17Z jkoshy $
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/kernel.h>
42#include <sys/lock.h>
43#include <sys/proc.h>
44#include <sys/sx.h>
45
46#define	PMC_FN_PROCESS_EXEC		1
47#define	PMC_FN_CSW_IN			2
48#define	PMC_FN_CSW_OUT			3
49#define	PMC_FN_DO_SAMPLES		4
50#define	PMC_FN_KLD_LOAD			5
51#define	PMC_FN_KLD_UNLOAD		6
52#define	PMC_FN_MMAP			7
53#define	PMC_FN_MUNMAP			8
54#define	PMC_FN_USER_CALLCHAIN		9
55
56struct pmckern_procexec {
57	int		pm_credentialschanged;
58	uintfptr_t	pm_entryaddr;
59};
60
61struct pmckern_map_in {
62	void		*pm_file;	/* filename or vnode pointer */
63	uintfptr_t	pm_address;	/* address object is loaded at */
64};
65
66struct pmckern_map_out {
67	uintfptr_t	pm_address;	/* start address of region */
68	size_t		pm_size;	/* size of unmapped region */
69};
70
71/* hook */
72extern int (*pmc_hook)(struct thread *_td, int _function, void *_arg);
73extern int (*pmc_intr)(int _cpu, struct trapframe *_frame);
74
75/* SX lock protecting the hook */
76extern struct sx pmc_sx;
77
78/* Per-cpu flags indicating availability of sampling data */
79extern volatile cpumask_t pmc_cpumask;
80
81/* Count of system-wide sampling PMCs in existence */
82extern volatile int pmc_ss_count;
83
84/* kernel version number */
85extern const int pmc_kernel_version;
86
87/* Hook invocation; for use within the kernel */
88#define	PMC_CALL_HOOK(t, cmd, arg)		\
89do {						\
90	sx_slock(&pmc_sx);			\
91	if (pmc_hook != NULL)			\
92		(pmc_hook)((t), (cmd), (arg));	\
93	sx_sunlock(&pmc_sx);			\
94} while (0)
95
96/* Hook invocation that needs an exclusive lock */
97#define	PMC_CALL_HOOK_X(t, cmd, arg)		\
98do {						\
99	sx_xlock(&pmc_sx);			\
100	if (pmc_hook != NULL)			\
101		(pmc_hook)((t), (cmd), (arg));	\
102	sx_xunlock(&pmc_sx);			\
103} while (0)
104
105/*
106 * Some hook invocations (e.g., from context switch and clock handling
107 * code) need to be lock-free.
108 */
109#define	PMC_CALL_HOOK_UNLOCKED(t, cmd, arg)	\
110do {						\
111	if (pmc_hook != NULL)			\
112		(pmc_hook)((t), (cmd), (arg));	\
113} while (0)
114
115#define	PMC_SWITCH_CONTEXT(t,cmd)	PMC_CALL_HOOK_UNLOCKED(t,cmd,NULL)
116
117/* Check if a process is using HWPMCs.*/
118#define PMC_PROC_IS_USING_PMCS(p)				\
119	(__predict_false(atomic_load_acq_int(&(p)->p_flag) &	\
120	    P_HWPMC))
121
122#define	PMC_SYSTEM_SAMPLING_ACTIVE()		(pmc_ss_count > 0)
123
124/* Check if a CPU has recorded samples. */
125#define	PMC_CPU_HAS_SAMPLES(C)	(__predict_false(pmc_cpumask & (1 << (C))))
126
127/* helper functions */
128int	pmc_cpu_is_disabled(int _cpu);
129int	pmc_cpu_is_logical(int _cpu);
130
131#endif /* _SYS_PMCKERN_H_ */
132