pmc_mdep.h revision 285041
1219820Sjeff/*-
2219820Sjeff * Copyright (c) 2003-2005,2008 Joseph Koshy
3219820Sjeff * Copyright (c) 2007 The FreeBSD Foundation
4219820Sjeff * All rights reserved.
5219820Sjeff *
6219820Sjeff * Portions of this software were developed by A. Joseph Koshy under
7219820Sjeff * sponsorship from the FreeBSD Foundation and Google, Inc.
8219820Sjeff *
9219820Sjeff * Redistribution and use in source and binary forms, with or without
10219820Sjeff * modification, are permitted provided that the following conditions
11219820Sjeff * are met:
12219820Sjeff * 1. Redistributions of source code must retain the above copyright
13219820Sjeff *    notice, this list of conditions and the following disclaimer.
14219820Sjeff * 2. Redistributions in binary form must reproduce the above copyright
15219820Sjeff *    notice, this list of conditions and the following disclaimer in the
16219820Sjeff *    documentation and/or other materials provided with the distribution.
17219820Sjeff *
18219820Sjeff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19219820Sjeff * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20219820Sjeff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21219820Sjeff * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22219820Sjeff * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23219820Sjeff * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24219820Sjeff * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25219820Sjeff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26219820Sjeff * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27219820Sjeff * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28219820Sjeff * SUCH DAMAGE.
29219820Sjeff *
30219820Sjeff * $FreeBSD: head/sys/i386/include/pmc_mdep.h 285041 2015-07-02 14:37:21Z kib $
31219820Sjeff */
32219820Sjeff
33219820Sjeff#ifndef _MACHINE_PMC_MDEP_H
34219820Sjeff#define	_MACHINE_PMC_MDEP_H 1
35219820Sjeff
36219820Sjeff#ifdef	_KERNEL
37219820Sjeffstruct pmc_mdep;
38219820Sjeff#endif
39219820Sjeff
40219820Sjeff/*
41219820Sjeff * On the i386 platform we support the following PMCs.
42219820Sjeff *
43219820Sjeff * TSC		The timestamp counter
44219820Sjeff * K7		AMD Athlon XP/MP and other 32 bit processors.
45219820Sjeff * K8		AMD Athlon64 and Opteron PMCs in 32 bit mode.
46219820Sjeff * PIV		Intel P4/HTT and P4/EMT64
47219820Sjeff * PPRO		Intel Pentium Pro, Pentium-II, Pentium-III, Celeron and
48219820Sjeff *		Pentium-M processors
49219820Sjeff * PENTIUM	Intel Pentium MMX.
50219820Sjeff * IAP		Intel Core/Core2/Atom programmable PMCs.
51219820Sjeff * IAF		Intel fixed-function PMCs.
52219820Sjeff * UCP		Intel Uncore programmable PMCs.
53219820Sjeff * UCF		Intel Uncore fixed-function PMCs.
54219820Sjeff */
55219820Sjeff
56219820Sjeff#include <dev/hwpmc/hwpmc_amd.h> /* K7 and K8 */
57219820Sjeff#include <dev/hwpmc/hwpmc_core.h>
58219820Sjeff#include <dev/hwpmc/hwpmc_piv.h>
59219820Sjeff#include <dev/hwpmc/hwpmc_ppro.h>
60219820Sjeff#include <dev/hwpmc/hwpmc_pentium.h>
61219820Sjeff#include <dev/hwpmc/hwpmc_tsc.h>
62219820Sjeff#include <dev/hwpmc/hwpmc_uncore.h>
63219820Sjeff
64219820Sjeff/*
65219820Sjeff * Intel processors implementing V2 and later of the Intel performance
66219820Sjeff * measurement architecture have PMCs of the following classes: TSC,
67219820Sjeff * IAF, IAP, UCF and UCP.
68219820Sjeff */
69219820Sjeff#define	PMC_MDEP_CLASS_INDEX_TSC	1
70219820Sjeff#define	PMC_MDEP_CLASS_INDEX_K7		2
71219820Sjeff#define	PMC_MDEP_CLASS_INDEX_K8		2
72219820Sjeff#define	PMC_MDEP_CLASS_INDEX_P4		2
73219820Sjeff#define	PMC_MDEP_CLASS_INDEX_P5		2
74219820Sjeff#define	PMC_MDEP_CLASS_INDEX_P6		2
75219820Sjeff#define	PMC_MDEP_CLASS_INDEX_IAP	2
76219820Sjeff#define	PMC_MDEP_CLASS_INDEX_IAF	3
77219820Sjeff#define	PMC_MDEP_CLASS_INDEX_UCP	4
78219820Sjeff#define	PMC_MDEP_CLASS_INDEX_UCF	5
79219820Sjeff
80219820Sjeff/*
81219820Sjeff * Architecture specific extensions to <sys/pmc.h> structures.
82219820Sjeff */
83219820Sjeff
84219820Sjeffunion pmc_md_op_pmcallocate  {
85219820Sjeff	struct pmc_md_amd_op_pmcallocate	pm_amd;
86219820Sjeff	struct pmc_md_iaf_op_pmcallocate	pm_iaf;
87219820Sjeff	struct pmc_md_iap_op_pmcallocate	pm_iap;
88219820Sjeff	struct pmc_md_ucf_op_pmcallocate	pm_ucf;
89219820Sjeff	struct pmc_md_ucp_op_pmcallocate	pm_ucp;
90219820Sjeff	struct pmc_md_p4_op_pmcallocate		pm_p4;
91219820Sjeff	struct pmc_md_pentium_op_pmcallocate	pm_pentium;
92219820Sjeff	struct pmc_md_ppro_op_pmcallocate	pm_ppro;
93219820Sjeff	uint64_t				__pad[4];
94219820Sjeff};
95219820Sjeff
96219820Sjeff/* Logging */
97219820Sjeff#define	PMCLOG_READADDR		PMCLOG_READ32
98219820Sjeff#define	PMCLOG_EMITADDR		PMCLOG_EMIT32
99219820Sjeff
100219820Sjeff#ifdef _KERNEL
101219820Sjeff
102219820Sjeff/* MD extension for 'struct pmc' */
103219820Sjeffunion pmc_md_pmc  {
104219820Sjeff	struct pmc_md_amd_pmc	pm_amd;
105219820Sjeff	struct pmc_md_iaf_pmc	pm_iaf;
106219820Sjeff	struct pmc_md_iap_pmc	pm_iap;
107219820Sjeff	struct pmc_md_ucf_pmc	pm_ucf;
108219820Sjeff	struct pmc_md_ucp_pmc	pm_ucp;
109219820Sjeff	struct pmc_md_p4_pmc	pm_p4;
110219820Sjeff	struct pmc_md_pentium_pmc pm_pentium;
111219820Sjeff	struct pmc_md_ppro_pmc	pm_ppro;
112219820Sjeff};
113219820Sjeff
114219820Sjeffstruct pmc;
115219820Sjeffstruct pmc_mdep;
116219820Sjeff
117219820Sjeff#define	PMC_TRAPFRAME_TO_PC(TF)	((TF)->tf_eip)
118219820Sjeff#define	PMC_TRAPFRAME_TO_FP(TF)	((TF)->tf_ebp)
119219820Sjeff
120219820Sjeff/*
121219820Sjeff * The layout of the stack frame on entry into the NMI handler depends on
122219820Sjeff * whether a privilege level change (and consequent stack switch) was
123219820Sjeff * required for entry.
124219820Sjeff *
125219820Sjeff * When processing an interrupt when in user mode, the processor switches
126219820Sjeff * stacks, and saves the user mode stack pointer on the kernel stack.  The
127219820Sjeff * user mode stack pointer is then available to the interrupt handler
128219820Sjeff * at frame->tf_esp.
129219820Sjeff *
130219820Sjeff * When processing an interrupt while in kernel mode, the processor
131219820Sjeff * continues to use the existing (kernel) stack.  Therefore we determine
132219820Sjeff * the stack pointer for the interrupted kernel procedure by adding an
133219820Sjeff * offset to the current frame pointer.
134219820Sjeff */
135219820Sjeff
136219820Sjeff#define	PMC_TRAPFRAME_TO_USER_SP(TF)	((TF)->tf_esp)
137219820Sjeff#define	PMC_TRAPFRAME_TO_KERNEL_SP(TF)	((uintptr_t) &((TF)->tf_esp))
138219820Sjeff
139219820Sjeff#define	PMC_IN_KERNEL_STACK(S,START,END)		\
140219820Sjeff	((S) >= (START) && (S) < (END))
141219820Sjeff#define	PMC_IN_KERNEL(va)	INKERNEL(va)
142219820Sjeff
143219820Sjeff#define	PMC_IN_USERSPACE(va) ((va) <= VM_MAXUSER_ADDRESS)
144219820Sjeff
145219820Sjeff#define	PMC_IN_TRAP_HANDLER(PC) 			\
146219820Sjeff	((PC) >= (uintptr_t) start_exceptions &&	\
147219820Sjeff	 (PC) < (uintptr_t) end_exceptions)
148219820Sjeff
149219820Sjeff#define	PMC_AT_FUNCTION_PROLOGUE_PUSH_BP(I)		\
150219820Sjeff	(((I) & 0x00ffffff) == 0xe58955) /* pushl %ebp; movl %esp,%ebp */
151219820Sjeff#define	PMC_AT_FUNCTION_PROLOGUE_MOV_SP_BP(I)		\
152219820Sjeff	(((I) & 0x0000ffff) == 0xe589)	/* movl %esp,%ebp */
153219820Sjeff#define	PMC_AT_FUNCTION_EPILOGUE_RET(I)			\
154219820Sjeff	(((I) & 0xFF) == 0xC3)		   /* ret */
155219820Sjeff
156219820Sjeff/* Build a fake kernel trapframe from current instruction pointer. */
157219820Sjeff#define PMC_FAKE_TRAPFRAME(TF)						\
158219820Sjeff	do {								\
159219820Sjeff	(TF)->tf_cs = 0; (TF)->tf_eflags = 0;				\
160219820Sjeff	__asm __volatile("movl %%ebp,%0" : "=r" ((TF)->tf_ebp));	\
161219820Sjeff	__asm __volatile("movl %%esp,%0" : "=r" ((TF)->tf_esp));	\
162219820Sjeff	__asm __volatile("call 1f \n\t1: pop %0" : "=r"((TF)->tf_eip));	\
163219820Sjeff	} while (0)
164219820Sjeff
165219820Sjeff/*
166219820Sjeff * Prototypes
167219820Sjeff */
168219820Sjeff
169219820Sjeffvoid	start_exceptions(void), end_exceptions(void);
170219820Sjeff
171219820Sjeffstruct pmc_mdep *pmc_amd_initialize(void);
172219820Sjeffvoid	pmc_amd_finalize(struct pmc_mdep *_md);
173219820Sjeffstruct pmc_mdep *pmc_intel_initialize(void);
174219820Sjeffvoid	pmc_intel_finalize(struct pmc_mdep *_md);
175219820Sjeff
176219820Sjeff#endif /* _KERNEL */
177219820Sjeff#endif /* _MACHINE_PMC_MDEP_H */
178219820Sjeff