xref: /freebsd/sys/amd64/include/pmc_mdep.h (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
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$
31  */
32 
33 /* Machine dependent interfaces */
34 
35 #ifndef _MACHINE_PMC_MDEP_H
36 #define	_MACHINE_PMC_MDEP_H 1
37 
38 #include <dev/hwpmc/hwpmc_amd.h>
39 #include <dev/hwpmc/hwpmc_piv.h>
40 
41 union pmc_md_op_pmcallocate  {
42 	struct pmc_md_amd_op_pmcallocate	pm_amd;
43 	struct pmc_md_p4_op_pmcallocate		pm_p4;
44 	uint64_t				__pad[4];
45 };
46 
47 /* Logging */
48 #define	PMCLOG_READADDR		PMCLOG_READ64
49 #define	PMCLOG_EMITADDR		PMCLOG_EMIT64
50 
51 #ifdef	_KERNEL
52 
53 union pmc_md_pmc {
54 	struct pmc_md_amd_pmc	pm_amd;
55 	struct pmc_md_p4_pmc	pm_p4;
56 };
57 
58 struct pmc;
59 
60 #define	PMC_TRAPFRAME_TO_PC(TF)	((TF)->tf_rip)
61 #define	PMC_TRAPFRAME_TO_FP(TF)	((TF)->tf_rbp)
62 #define	PMC_TRAPFRAME_TO_SP(TF)	((TF)->tf_rsp)
63 
64 #define	PMC_AT_FUNCTION_PROLOGUE_PUSH_BP(I)		\
65 	(((I) & 0xffffffff) == 0xe5894855) /* pushq %rbp; movq %rsp,%rbp */
66 #define	PMC_AT_FUNCTION_PROLOGUE_MOV_SP_BP(I)		\
67 	(((I) & 0x00ffffff) == 0x00e58948) /* movq %rsp,%rbp */
68 #define	PMC_AT_FUNCTION_EPILOGUE_RET(I)			\
69 	(((I) & 0xFF) == 0xC3)		   /* ret */
70 
71 #define	PMC_IN_TRAP_HANDLER(PC) 			\
72 	((PC) >= (uintptr_t) start_exceptions &&	\
73 	 (PC) < (uintptr_t) end_exceptions)
74 
75 #define	PMC_IN_KERNEL_STACK(S,START,END)		\
76 	((S) >= (START) && (S) < (END))
77 #define	PMC_IN_KERNEL(va) (((va) >= DMAP_MIN_ADDRESS &&		\
78 	(va) < DMAP_MAX_ADDRESS) || ((va) >= KERNBASE &&	\
79 	(va) < VM_MAX_KERNEL_ADDRESS))
80 
81 #define	PMC_IN_USERSPACE(va) ((va) <= VM_MAXUSER_ADDRESS)
82 
83 /*
84  * Prototypes
85  */
86 
87 void	start_exceptions(void), end_exceptions(void);
88 void	pmc_x86_lapic_enable_pmc_interrupt(void);
89 
90 #endif
91 #endif /* _MACHINE_PMC_MDEP_H */
92