pmc_mdep.h revision 302408
1266328Sian/*-
2266328Sian * Copyright (c) 2003-2005,2008 Joseph Koshy
3249997Swkoszek * Copyright (c) 2007 The FreeBSD Foundation
4266328Sian * All rights reserved.
5266328Sian *
6249997Swkoszek * Portions of this software were developed by A. Joseph Koshy under
7266328Sian * sponsorship from the FreeBSD Foundation and Google, Inc.
8249997Swkoszek *
9266328Sian * Redistribution and use in source and binary forms, with or without
10266328Sian * modification, are permitted provided that the following conditions
11266328Sian * are met:
12266328Sian * 1. Redistributions of source code must retain the above copyright
13249997Swkoszek *    notice, this list of conditions and the following disclaimer.
14266328Sian * 2. Redistributions in binary form must reproduce the above copyright
15266328Sian *    notice, this list of conditions and the following disclaimer in the
16266328Sian *    documentation and/or other materials provided with the distribution.
17266328Sian *
18249997Swkoszek * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19266328Sian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20249997Swkoszek * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21249997Swkoszek * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22249997Swkoszek * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23249997Swkoszek * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24249997Swkoszek * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25249997Swkoszek * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26249997Swkoszek * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27249997Swkoszek * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28266328Sian * SUCH DAMAGE.
29266328Sian *
30266328Sian * $FreeBSD: stable/11/sys/i386/include/pmc_mdep.h 285041 2015-07-02 14:37:21Z kib $
31266328Sian */
32266328Sian
33266328Sian#ifndef _MACHINE_PMC_MDEP_H
34266328Sian#define	_MACHINE_PMC_MDEP_H 1
35249997Swkoszek
36249997Swkoszek#ifdef	_KERNEL
37266328Sianstruct pmc_mdep;
38266328Sian#endif
39266328Sian
40266328Sian/*
41249997Swkoszek * On the i386 platform we support the following PMCs.
42249997Swkoszek *
43249997Swkoszek * TSC		The timestamp counter
44266328Sian * K7		AMD Athlon XP/MP and other 32 bit processors.
45266328Sian * K8		AMD Athlon64 and Opteron PMCs in 32 bit mode.
46266331Sian * PIV		Intel P4/HTT and P4/EMT64
47266328Sian * PPRO		Intel Pentium Pro, Pentium-II, Pentium-III, Celeron and
48266328Sian *		Pentium-M processors
49266328Sian * PENTIUM	Intel Pentium MMX.
50266328Sian * IAP		Intel Core/Core2/Atom programmable PMCs.
51266328Sian * IAF		Intel fixed-function PMCs.
52266328Sian * UCP		Intel Uncore programmable PMCs.
53266328Sian * UCF		Intel Uncore fixed-function PMCs.
54266328Sian */
55266328Sian
56266328Sian#include <dev/hwpmc/hwpmc_amd.h> /* K7 and K8 */
57266331Sian#include <dev/hwpmc/hwpmc_core.h>
58266328Sian#include <dev/hwpmc/hwpmc_piv.h>
59266379Sian#include <dev/hwpmc/hwpmc_ppro.h>
60249997Swkoszek#include <dev/hwpmc/hwpmc_pentium.h>
61249997Swkoszek#include <dev/hwpmc/hwpmc_tsc.h>
62268813Simp#include <dev/hwpmc/hwpmc_uncore.h>
63249997Swkoszek
64249997Swkoszek/*
65249997Swkoszek * Intel processors implementing V2 and later of the Intel performance
66249997Swkoszek * measurement architecture have PMCs of the following classes: TSC,
67266328Sian * IAF, IAP, UCF and UCP.
68266328Sian */
69266328Sian#define	PMC_MDEP_CLASS_INDEX_TSC	1
70266328Sian#define	PMC_MDEP_CLASS_INDEX_K7		2
71249997Swkoszek#define	PMC_MDEP_CLASS_INDEX_K8		2
72249997Swkoszek#define	PMC_MDEP_CLASS_INDEX_P4		2
73249997Swkoszek#define	PMC_MDEP_CLASS_INDEX_P5		2
74249997Swkoszek#define	PMC_MDEP_CLASS_INDEX_P6		2
75249997Swkoszek#define	PMC_MDEP_CLASS_INDEX_IAP	2
76249997Swkoszek#define	PMC_MDEP_CLASS_INDEX_IAF	3
77249997Swkoszek#define	PMC_MDEP_CLASS_INDEX_UCP	4
78249997Swkoszek#define	PMC_MDEP_CLASS_INDEX_UCF	5
79249997Swkoszek
80249997Swkoszek/*
81249997Swkoszek * Architecture specific extensions to <sys/pmc.h> structures.
82249997Swkoszek */
83249997Swkoszek
84249997Swkoszekunion pmc_md_op_pmcallocate  {
85249997Swkoszek	struct pmc_md_amd_op_pmcallocate	pm_amd;
86249997Swkoszek	struct pmc_md_iaf_op_pmcallocate	pm_iaf;
87249997Swkoszek	struct pmc_md_iap_op_pmcallocate	pm_iap;
88249997Swkoszek	struct pmc_md_ucf_op_pmcallocate	pm_ucf;
89249997Swkoszek	struct pmc_md_ucp_op_pmcallocate	pm_ucp;
90249997Swkoszek	struct pmc_md_p4_op_pmcallocate		pm_p4;
91249997Swkoszek	struct pmc_md_pentium_op_pmcallocate	pm_pentium;
92249997Swkoszek	struct pmc_md_ppro_op_pmcallocate	pm_ppro;
93249997Swkoszek	uint64_t				__pad[4];
94249997Swkoszek};
95249997Swkoszek
96249997Swkoszek/* Logging */
97249997Swkoszek#define	PMCLOG_READADDR		PMCLOG_READ32
98249997Swkoszek#define	PMCLOG_EMITADDR		PMCLOG_EMIT32
99249997Swkoszek
100249997Swkoszek#ifdef _KERNEL
101249997Swkoszek
102249997Swkoszek/* MD extension for 'struct pmc' */
103249997Swkoszekunion pmc_md_pmc  {
104	struct pmc_md_amd_pmc	pm_amd;
105	struct pmc_md_iaf_pmc	pm_iaf;
106	struct pmc_md_iap_pmc	pm_iap;
107	struct pmc_md_ucf_pmc	pm_ucf;
108	struct pmc_md_ucp_pmc	pm_ucp;
109	struct pmc_md_p4_pmc	pm_p4;
110	struct pmc_md_pentium_pmc pm_pentium;
111	struct pmc_md_ppro_pmc	pm_ppro;
112};
113
114struct pmc;
115struct pmc_mdep;
116
117#define	PMC_TRAPFRAME_TO_PC(TF)	((TF)->tf_eip)
118#define	PMC_TRAPFRAME_TO_FP(TF)	((TF)->tf_ebp)
119
120/*
121 * The layout of the stack frame on entry into the NMI handler depends on
122 * whether a privilege level change (and consequent stack switch) was
123 * required for entry.
124 *
125 * When processing an interrupt when in user mode, the processor switches
126 * stacks, and saves the user mode stack pointer on the kernel stack.  The
127 * user mode stack pointer is then available to the interrupt handler
128 * at frame->tf_esp.
129 *
130 * When processing an interrupt while in kernel mode, the processor
131 * continues to use the existing (kernel) stack.  Therefore we determine
132 * the stack pointer for the interrupted kernel procedure by adding an
133 * offset to the current frame pointer.
134 */
135
136#define	PMC_TRAPFRAME_TO_USER_SP(TF)	((TF)->tf_esp)
137#define	PMC_TRAPFRAME_TO_KERNEL_SP(TF)	((uintptr_t) &((TF)->tf_esp))
138
139#define	PMC_IN_KERNEL_STACK(S,START,END)		\
140	((S) >= (START) && (S) < (END))
141#define	PMC_IN_KERNEL(va)	INKERNEL(va)
142
143#define	PMC_IN_USERSPACE(va) ((va) <= VM_MAXUSER_ADDRESS)
144
145#define	PMC_IN_TRAP_HANDLER(PC) 			\
146	((PC) >= (uintptr_t) start_exceptions &&	\
147	 (PC) < (uintptr_t) end_exceptions)
148
149#define	PMC_AT_FUNCTION_PROLOGUE_PUSH_BP(I)		\
150	(((I) & 0x00ffffff) == 0xe58955) /* pushl %ebp; movl %esp,%ebp */
151#define	PMC_AT_FUNCTION_PROLOGUE_MOV_SP_BP(I)		\
152	(((I) & 0x0000ffff) == 0xe589)	/* movl %esp,%ebp */
153#define	PMC_AT_FUNCTION_EPILOGUE_RET(I)			\
154	(((I) & 0xFF) == 0xC3)		   /* ret */
155
156/* Build a fake kernel trapframe from current instruction pointer. */
157#define PMC_FAKE_TRAPFRAME(TF)						\
158	do {								\
159	(TF)->tf_cs = 0; (TF)->tf_eflags = 0;				\
160	__asm __volatile("movl %%ebp,%0" : "=r" ((TF)->tf_ebp));	\
161	__asm __volatile("movl %%esp,%0" : "=r" ((TF)->tf_esp));	\
162	__asm __volatile("call 1f \n\t1: pop %0" : "=r"((TF)->tf_eip));	\
163	} while (0)
164
165/*
166 * Prototypes
167 */
168
169void	start_exceptions(void), end_exceptions(void);
170
171struct pmc_mdep *pmc_amd_initialize(void);
172void	pmc_amd_finalize(struct pmc_mdep *_md);
173struct pmc_mdep *pmc_intel_initialize(void);
174void	pmc_intel_finalize(struct pmc_mdep *_md);
175
176#endif /* _KERNEL */
177#endif /* _MACHINE_PMC_MDEP_H */
178