xref: /freebsd/sys/dev/hwpmc/hwpmc_arm64_md.c (revision ba3c1f5972d7b90feb6e6da47905ff2757e0fe57)
1 /*-
2  * Copyright (c) 2015 Ruslan Bukin <br@bsdpad.com>
3  * All rights reserved.
4  *
5  * This software was developed by the University of Cambridge Computer
6  * Laboratory with support from ARM Ltd.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/pmc.h>
36 
37 #include <vm/vm.h>
38 #include <vm/pmap.h>
39 
40 #include <machine/cpu.h>
41 #include <machine/md_var.h>
42 #include <machine/pmc_mdep.h>
43 #include <machine/stack.h>
44 #include <machine/vmparam.h>
45 
46 struct pmc_mdep *
47 pmc_md_initialize(void)
48 {
49 
50 	return (pmc_arm64_initialize());
51 }
52 
53 void
54 pmc_md_finalize(struct pmc_mdep *md)
55 {
56 
57 	pmc_arm64_finalize(md);
58 }
59 
60 int
61 pmc_save_kernel_callchain(uintptr_t *cc, int maxsamples, struct trapframe *tf)
62 {
63 	struct unwind_state frame;
64 	int count;
65 
66 	KASSERT(TRAPF_USERMODE(tf) == 0,("[arm64,%d] not a kernel backtrace",
67 	    __LINE__));
68 
69 	frame.pc = PMC_TRAPFRAME_TO_PC(tf);
70 	*cc++ = frame.pc;
71 
72 	if (maxsamples <= 1)
73 		return (1);
74 
75 	frame.fp = PMC_TRAPFRAME_TO_FP(tf);
76 	if (!PMC_IN_KERNEL(frame.pc) || !PMC_IN_KERNEL_STACK(frame.fp))
77 		return (1);
78 
79 	for (count = 1; count < maxsamples; count++) {
80 		if (!unwind_frame(curthread, &frame))
81 			break;
82 		if (!PMC_IN_KERNEL(frame.pc))
83 			break;
84 		*cc++ = frame.pc;
85 	}
86 
87 	return (count);
88 }
89 
90 int
91 pmc_save_user_callchain(uintptr_t *cc, int maxsamples,
92     struct trapframe *tf)
93 {
94 	uintptr_t pc, r, oldfp, fp;
95 	int count;
96 
97 	KASSERT(TRAPF_USERMODE(tf), ("[arm64,%d] Not a user trap frame tf=%p",
98 	    __LINE__, (void *) tf));
99 
100 	pc = PMC_TRAPFRAME_TO_PC(tf);
101 	*cc++ = pc;
102 
103 	if (maxsamples <= 1)
104 		return (1);
105 
106 	oldfp = fp = PMC_TRAPFRAME_TO_FP(tf);
107 
108 	if (!PMC_IN_USERSPACE(pc) ||
109 	    !PMC_IN_USERSPACE(fp))
110 		return (1);
111 
112 	for (count = 1; count < maxsamples; count++) {
113 		/* Use saved lr as pc. */
114 		r = fp + sizeof(uintptr_t);
115 		if (copyin((void *)r, &pc, sizeof(pc)) != 0)
116 			break;
117 		if (!PMC_IN_USERSPACE(pc))
118 			break;
119 
120 		*cc++ = pc;
121 
122 		/* Switch to next frame up */
123 		oldfp = fp;
124 		r = fp;
125 		if (copyin((void *)r, &fp, sizeof(fp)) != 0)
126 			break;
127 		if (fp < oldfp || !PMC_IN_USERSPACE(fp))
128 			break;
129 	}
130 
131 	return (count);
132 }
133