xref: /freebsd/sys/dev/hwpmc/hwpmc_arm64_md.c (revision c03c5b1c)
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/pmc.h>
35 #include <sys/proc.h>
36 #include <sys/systm.h>
37 
38 #include <machine/cpu.h>
39 #include <machine/md_var.h>
40 #include <machine/pmc_mdep.h>
41 #include <machine/stack.h>
42 
43 #include <vm/vm.h>
44 #include <vm/vm_param.h>
45 #include <vm/pmap.h>
46 
47 struct pmc_mdep *
48 pmc_md_initialize()
49 {
50 
51 	return (pmc_arm64_initialize());
52 }
53 
54 void
55 pmc_md_finalize(struct pmc_mdep *md)
56 {
57 
58 	pmc_arm64_finalize(md);
59 }
60 
61 int
62 pmc_save_kernel_callchain(uintptr_t *cc, int maxsamples,
63     struct trapframe *tf)
64 {
65 	uintptr_t pc, r, stackstart, stackend, fp;
66 	struct thread *td;
67 	int count;
68 
69 	KASSERT(TRAPF_USERMODE(tf) == 0,("[arm64,%d] not a kernel backtrace",
70 	    __LINE__));
71 
72 	td = curthread;
73 	pc = PMC_TRAPFRAME_TO_PC(tf);
74 	*cc++ = pc;
75 
76 	if (maxsamples <= 1)
77 		return (1);
78 
79 	stackstart = (uintptr_t) td->td_kstack;
80 	stackend = (uintptr_t) td->td_kstack + td->td_kstack_pages * PAGE_SIZE;
81 	fp = PMC_TRAPFRAME_TO_FP(tf);
82 
83 	if (!PMC_IN_KERNEL(pc) ||
84 	    !PMC_IN_KERNEL_STACK(fp, stackstart, stackend))
85 		return (1);
86 
87 	for (count = 1; count < maxsamples; count++) {
88 		/* Use saved lr as pc. */
89 		r = fp + sizeof(uintptr_t);
90 		if (!PMC_IN_KERNEL_STACK(r, stackstart, stackend))
91 			break;
92 		pc = *(uintptr_t *)r;
93 		if (!PMC_IN_KERNEL(pc))
94 			break;
95 
96 		*cc++ = pc;
97 
98 		/* Switch to next frame up */
99 		r = fp;
100 		if (!PMC_IN_KERNEL_STACK(r, stackstart, stackend))
101 			break;
102 		fp = *(uintptr_t *)r;
103 		if (!PMC_IN_KERNEL_STACK(fp, stackstart, stackend))
104 			break;
105 	}
106 
107 	return (count);
108 }
109 
110 int
111 pmc_save_user_callchain(uintptr_t *cc, int maxsamples,
112     struct trapframe *tf)
113 {
114 	uintptr_t pc, r, oldfp, fp;
115 	struct thread *td;
116 	int count;
117 
118 	KASSERT(TRAPF_USERMODE(tf), ("[arm64,%d] Not a user trap frame tf=%p",
119 	    __LINE__, (void *) tf));
120 
121 	td = curthread;
122 	pc = PMC_TRAPFRAME_TO_PC(tf);
123 	*cc++ = pc;
124 
125 	if (maxsamples <= 1)
126 		return (1);
127 
128 	oldfp = fp = PMC_TRAPFRAME_TO_FP(tf);
129 
130 	if (!PMC_IN_USERSPACE(pc) ||
131 	    !PMC_IN_USERSPACE(fp))
132 		return (1);
133 
134 	for (count = 1; count < maxsamples; count++) {
135 		/* Use saved lr as pc. */
136 		r = fp + sizeof(uintptr_t);
137 		if (copyin((void *)r, &pc, sizeof(pc)) != 0)
138 			break;
139 		if (!PMC_IN_USERSPACE(pc))
140 			break;
141 
142 		*cc++ = pc;
143 
144 		/* Switch to next frame up */
145 		oldfp = fp;
146 		r = fp;
147 		if (copyin((void *)r, &fp, sizeof(fp)) != 0)
148 			break;
149 		if (fp < oldfp || !PMC_IN_USERSPACE(fp))
150 			break;
151 	}
152 
153 	return (count);
154 }
155