pmckern.h revision 157144
1145256Sjkoshy/*-
2157144Sjkoshy * Copyright (c) 2003-2006, Joseph Koshy
3145256Sjkoshy * All rights reserved.
4145256Sjkoshy *
5145256Sjkoshy * Redistribution and use in source and binary forms, with or without
6145256Sjkoshy * modification, are permitted provided that the following conditions
7145256Sjkoshy * are met:
8145256Sjkoshy * 1. Redistributions of source code must retain the above copyright
9145256Sjkoshy *    notice, this list of conditions and the following disclaimer.
10145256Sjkoshy * 2. Redistributions in binary form must reproduce the above copyright
11145256Sjkoshy *    notice, this list of conditions and the following disclaimer in the
12145256Sjkoshy *    documentation and/or other materials provided with the distribution.
13145256Sjkoshy *
14145256Sjkoshy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15145256Sjkoshy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16145256Sjkoshy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17145256Sjkoshy * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18145256Sjkoshy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19145256Sjkoshy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20145256Sjkoshy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21145256Sjkoshy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22145256Sjkoshy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23145256Sjkoshy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24145256Sjkoshy * SUCH DAMAGE.
25145256Sjkoshy *
26145256Sjkoshy * $FreeBSD: head/sys/sys/pmckern.h 157144 2006-03-26 12:20:54Z jkoshy $
27145256Sjkoshy */
28145256Sjkoshy
29145256Sjkoshy/*
30145256Sjkoshy * PMC interface used by the base kernel.
31145256Sjkoshy */
32145256Sjkoshy
33145256Sjkoshy#ifndef _SYS_PMCKERN_H_
34145256Sjkoshy#define _SYS_PMCKERN_H_
35145256Sjkoshy
36145256Sjkoshy#include <sys/param.h>
37145256Sjkoshy#include <sys/kernel.h>
38145256Sjkoshy#include <sys/lock.h>
39145256Sjkoshy#include <sys/proc.h>
40145256Sjkoshy#include <sys/sx.h>
41145256Sjkoshy
42146799Sjkoshy#define	PMC_FN_PROCESS_EXEC		1
43146799Sjkoshy#define	PMC_FN_CSW_IN			2
44146799Sjkoshy#define	PMC_FN_CSW_OUT			3
45146799Sjkoshy#define	PMC_FN_DO_SAMPLES		4
46157144Sjkoshy#define	PMC_FN_KLD_LOAD			5
47157144Sjkoshy#define	PMC_FN_KLD_UNLOAD		6
48157144Sjkoshy#define	PMC_FN_MMAP			7
49157144Sjkoshy#define	PMC_FN_MUNMAP			8
50145256Sjkoshy
51147708Sjkoshystruct pmckern_procexec {
52147708Sjkoshy	int		pm_credentialschanged;
53157144Sjkoshy	uintfptr_t	pm_entryaddr;
54147708Sjkoshy};
55147708Sjkoshy
56157144Sjkoshystruct pmckern_map_in {
57157144Sjkoshy	void		*pm_file;	/* filename or vnode pointer */
58157144Sjkoshy	uintfptr_t	pm_address;	/* address object is loaded at */
59157144Sjkoshy};
60157144Sjkoshy
61157144Sjkoshystruct pmckern_map_out {
62157144Sjkoshy	uintfptr_t	pm_address;	/* start address of region */
63157144Sjkoshy	size_t		pm_size;	/* size of unmapped region */
64157144Sjkoshy};
65157144Sjkoshy
66145256Sjkoshy/* hook */
67145256Sjkoshyextern int (*pmc_hook)(struct thread *_td, int _function, void *_arg);
68146799Sjkoshyextern int (*pmc_intr)(int _cpu, uintptr_t _pc, int _usermode);
69145256Sjkoshy
70145256Sjkoshy/* SX lock protecting the hook */
71145256Sjkoshyextern struct sx pmc_sx;
72145256Sjkoshy
73146799Sjkoshy/* Per-cpu flags indicating availability of sampling data */
74147191Sjkoshyextern volatile cpumask_t pmc_cpumask;
75146799Sjkoshy
76147191Sjkoshy/* Count of system-wide sampling PMCs in existence */
77147191Sjkoshyextern volatile int pmc_ss_count;
78147191Sjkoshy
79148562Sjkoshy/* kernel version number */
80148562Sjkoshyextern const int pmc_kernel_version;
81148562Sjkoshy
82146799Sjkoshy/* Hook invocation; for use within the kernel */
83145256Sjkoshy#define	PMC_CALL_HOOK(t, cmd, arg)		\
84145256Sjkoshydo {						\
85145256Sjkoshy	sx_slock(&pmc_sx);			\
86145256Sjkoshy	if (pmc_hook != NULL)			\
87145256Sjkoshy		(pmc_hook)((t), (cmd), (arg));	\
88145256Sjkoshy	sx_sunlock(&pmc_sx);			\
89145256Sjkoshy} while (0)
90145256Sjkoshy
91146799Sjkoshy/* Hook invocation that needs an exclusive lock */
92145256Sjkoshy#define	PMC_CALL_HOOK_X(t, cmd, arg)		\
93145256Sjkoshydo {						\
94145256Sjkoshy	sx_xlock(&pmc_sx);			\
95145256Sjkoshy	if (pmc_hook != NULL)			\
96145256Sjkoshy		(pmc_hook)((t), (cmd), (arg));	\
97145256Sjkoshy	sx_xunlock(&pmc_sx);			\
98145256Sjkoshy} while (0)
99145256Sjkoshy
100146799Sjkoshy/*
101146799Sjkoshy * Some hook invocations (e.g., from context switch and clock handling
102146799Sjkoshy * code) need to be lock-free.
103146799Sjkoshy */
104146799Sjkoshy#define	PMC_CALL_HOOK_UNLOCKED(t, cmd, arg)	\
105145256Sjkoshydo {						\
106145256Sjkoshy	if (pmc_hook != NULL)			\
107146799Sjkoshy		(pmc_hook)((t), (cmd), (arg));	\
108145256Sjkoshy} while (0)
109145256Sjkoshy
110146799Sjkoshy#define	PMC_SWITCH_CONTEXT(t,cmd)	PMC_CALL_HOOK_UNLOCKED(t,cmd,NULL)
111145256Sjkoshy
112146799Sjkoshy/* Check if a process is using HWPMCs.*/
113145256Sjkoshy#define PMC_PROC_IS_USING_PMCS(p)				\
114145256Sjkoshy	(__predict_false(atomic_load_acq_int(&(p)->p_flag) &	\
115145256Sjkoshy	    P_HWPMC))
116145256Sjkoshy
117147191Sjkoshy#define	PMC_SYSTEM_SAMPLING_ACTIVE()		(pmc_ss_count > 0)
118147191Sjkoshy
119146799Sjkoshy/* Check if a CPU has recorded samples. */
120146799Sjkoshy#define	PMC_CPU_HAS_SAMPLES(C)	(__predict_false(pmc_cpumask & (1 << (C))))
121146799Sjkoshy
122145256Sjkoshy/* helper functions */
123145256Sjkoshyint	pmc_cpu_is_disabled(int _cpu);
124145256Sjkoshyint	pmc_cpu_is_logical(int _cpu);
125145256Sjkoshy
126145256Sjkoshy#endif /* _SYS_PMCKERN_H_ */
127