pmc_mdep.h revision 184802
1131087Smarcel/*-
2131087Smarcel * Copyright (c) 2003-2005,2008 Joseph Koshy
3131087Smarcel * Copyright (c) 2007 The FreeBSD Foundation
4131087Smarcel * All rights reserved.
5131087Smarcel *
6131087Smarcel * Portions of this software were developed by A. Joseph Koshy under
7131087Smarcel * sponsorship from the FreeBSD Foundation and Google, Inc.
8131087Smarcel *
9131087Smarcel * Redistribution and use in source and binary forms, with or without
10131087Smarcel * modification, are permitted provided that the following conditions
11131087Smarcel * are met:
12131087Smarcel * 1. Redistributions of source code must retain the above copyright
13131087Smarcel *    notice, this list of conditions and the following disclaimer.
14131087Smarcel * 2. Redistributions in binary form must reproduce the above copyright
15131087Smarcel *    notice, this list of conditions and the following disclaimer in the
16131087Smarcel *    documentation and/or other materials provided with the distribution.
17131087Smarcel *
18131087Smarcel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19131087Smarcel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20131087Smarcel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21131087Smarcel * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22131087Smarcel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23131087Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24131087Smarcel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25131087Smarcel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26131087Smarcel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27131087Smarcel * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28131087Smarcel * SUCH DAMAGE.
29131087Smarcel *
30131087Smarcel * $FreeBSD: head/sys/i386/include/pmc_mdep.h 184802 2008-11-09 17:37:54Z jkoshy $
31131087Smarcel */
32131087Smarcel
33131087Smarcel#ifndef _MACHINE_PMC_MDEP_H
34131087Smarcel#define	_MACHINE_PMC_MDEP_H 1
35131087Smarcel
36131087Smarcel#ifdef	_KERNEL
37131087Smarcelstruct pmc_mdep;
38131087Smarcel#endif
39131087Smarcel
40131087Smarcel/*
41131087Smarcel * On the i386 platform we support the following PMCs.
42131087Smarcel *
43131087Smarcel * TSC		The timestamp counter
44131087Smarcel * K7		AMD Athlon XP/MP and other 32 bit processors.
45131087Smarcel * K8		AMD Athlon64 and Opteron PMCs in 32 bit mode.
46131087Smarcel * PIV		Intel P4/HTT and P4/EMT64
47131087Smarcel * PPRO		Intel Pentium Pro, Pentium-II, Pentium-III, Celeron and
48131087Smarcel *		Pentium-M processors
49131087Smarcel * PENTIUM	Intel Pentium MMX.
50131087Smarcel * IAP		Intel Core/Core2/Atom programmable PMCs.
51131087Smarcel * IAF		Intel fixed-function PMCs.
52131087Smarcel */
53131087Smarcel
54131087Smarcel#include <dev/hwpmc/hwpmc_amd.h> /* K7 and K8 */
55131087Smarcel#include <dev/hwpmc/hwpmc_piv.h>
56131087Smarcel#include <dev/hwpmc/hwpmc_ppro.h>
57131087Smarcel#include <dev/hwpmc/hwpmc_pentium.h>
58131087Smarcel#include <dev/hwpmc/hwpmc_tsc.h>
59131087Smarcel
60131087Smarcel/*
61131087Smarcel * Intel processors implementing V2 and later of the Intel performance
62131087Smarcel * measurement architecture have PMCs of the following classes: TSC,
63131087Smarcel * IAF and IAP.
64131087Smarcel */
65131087Smarcel#define	PMC_MDEP_CLASS_INDEX_TSC	0
66131087Smarcel#define	PMC_MDEP_CLASS_INDEX_K7		1
67131087Smarcel#define	PMC_MDEP_CLASS_INDEX_K8		1
68131087Smarcel#define	PMC_MDEP_CLASS_INDEX_P4		1
69131087Smarcel#define	PMC_MDEP_CLASS_INDEX_P5		1
70131087Smarcel#define	PMC_MDEP_CLASS_INDEX_P6		1
71131087Smarcel#define	PMC_MDEP_CLASS_INDEX_IAF	1
72131087Smarcel#define	PMC_MDEP_CLASS_INDEX_IAP	2
73131087Smarcel
74131087Smarcel/*
75131087Smarcel * Architecture specific extensions to <sys/pmc.h> structures.
76131087Smarcel */
77131087Smarcel
78131087Smarcelunion pmc_md_op_pmcallocate  {
79131087Smarcel	struct pmc_md_amd_op_pmcallocate	pm_amd;
80131087Smarcel 	struct pmc_md_ppro_op_pmcallocate	pm_ppro;
81131087Smarcel	struct pmc_md_pentium_op_pmcallocate	pm_pentium;
82131087Smarcel	struct pmc_md_p4_op_pmcallocate		pm_p4;
83131087Smarcel	uint64_t				__pad[4];
84131087Smarcel};
85131087Smarcel
86131087Smarcel/* Logging */
87131087Smarcel#define	PMCLOG_READADDR		PMCLOG_READ32
88131087Smarcel#define	PMCLOG_EMITADDR		PMCLOG_EMIT32
89131087Smarcel
90131087Smarcel#ifdef _KERNEL
91131087Smarcel
92131087Smarcel/* MD extension for 'struct pmc' */
93131087Smarcelunion pmc_md_pmc  {
94131087Smarcel	struct pmc_md_amd_pmc	pm_amd;
95131087Smarcel	struct pmc_md_ppro_pmc	pm_ppro;
96131087Smarcel	struct pmc_md_pentium_pmc pm_pentium;
97131087Smarcel	struct pmc_md_p4_pmc	pm_p4;
98131087Smarcel};
99131087Smarcel
100131087Smarcelstruct pmc;
101131087Smarcelstruct pmc_mdep;
102131087Smarcel
103131087Smarcel#define	PMC_TRAPFRAME_TO_PC(TF)	((TF)->tf_eip)
104131087Smarcel#define	PMC_TRAPFRAME_TO_FP(TF)	((TF)->tf_ebp)
105131087Smarcel
106131087Smarcel/*
107131087Smarcel * The layout of the stack frame on entry into the NMI handler depends on
108131087Smarcel * whether a privilege level change (and consequent stack switch) was
109131087Smarcel * required for entry.
110131087Smarcel *
111131087Smarcel * When processing an interrupt when in user mode, the processor switches
112131087Smarcel * stacks, and saves the user mode stack pointer on the kernel stack.  The
113131087Smarcel * user mode stack pointer is then available to the interrupt handler
114131087Smarcel * at frame->tf_esp.
115131087Smarcel *
116131087Smarcel * When processing an interrupt while in kernel mode, the processor
117131087Smarcel * continues to use the existing (kernel) stack.  Therefore we determine
118131087Smarcel * the stack pointer for the interrupted kernel procedure by adding an
119131087Smarcel * offset to the current frame pointer.
120131087Smarcel */
121131087Smarcel
122131087Smarcel#define	PMC_TRAPFRAME_TO_USER_SP(TF)	((TF)->tf_esp)
123131087Smarcel#define	PMC_TRAPFRAME_TO_KERNEL_SP(TF)	((uintptr_t) &((TF)->tf_esp))
124131087Smarcel
125131087Smarcel#define	PMC_IN_KERNEL_STACK(S,START,END)		\
126131087Smarcel	((S) >= (START) && (S) < (END))
127131087Smarcel#define	PMC_IN_KERNEL(va) (((va) >= USRSTACK) &&	\
128131087Smarcel	((va) < VM_MAX_KERNEL_ADDRESS))
129131087Smarcel
130131087Smarcel#define	PMC_IN_USERSPACE(va) ((va) <= VM_MAXUSER_ADDRESS)
131131087Smarcel
132131087Smarcel#define	PMC_IN_TRAP_HANDLER(PC) 			\
133131087Smarcel	((PC) >= (uintptr_t) start_exceptions &&	\
134131087Smarcel	 (PC) < (uintptr_t) end_exceptions)
135131087Smarcel
136131087Smarcel#define	PMC_AT_FUNCTION_PROLOGUE_PUSH_BP(I)		\
137131087Smarcel	(((I) & 0x00ffffff) == 0xe58955) /* pushl %ebp; movl %esp,%ebp */
138131087Smarcel#define	PMC_AT_FUNCTION_PROLOGUE_MOV_SP_BP(I)		\
139131087Smarcel	(((I) & 0x0000ffff) == 0xe589)	/* movl %esp,%ebp */
140131087Smarcel#define	PMC_AT_FUNCTION_EPILOGUE_RET(I)			\
141131087Smarcel	(((I) & 0xFF) == 0xC3)		   /* ret */
142131087Smarcel
143131087Smarcel/*
144131087Smarcel * Prototypes
145131087Smarcel */
146131087Smarcel
147131087Smarcelvoid	start_exceptions(void), end_exceptions(void);
148131087Smarcelvoid	pmc_x86_lapic_enable_pmc_interrupt(void);
149131087Smarcel
150131087Smarcelstruct pmc_mdep *pmc_amd_initialize(void);
151131087Smarcelvoid	pmc_amd_finalize(struct pmc_mdep *_md);
152131087Smarcelstruct pmc_mdep *pmc_intel_initialize(void);
153131087Smarcelvoid	pmc_intel_finalize(struct pmc_mdep *_md);
154131087Smarcel
155131087Smarcel#endif /* _KERNEL */
156131087Smarcel#endif /* _MACHINE_PMC_MDEP_H */
157131087Smarcel