1 // SPDX-License-Identifier: (BSD-2-Clause AND BSD-3-Clause)
2 /*
3 * Copyright (c) 2016, Linaro Limited
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*
30 * Portions of this file are adapted from glibc:
31 * gmon/gmon.c
32 * gmon/mcount.c
33 *
34 *-
35 * Copyright (c) 1983, 1992, 1993, 2011
36 * The Regents of the University of California. All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * SUCH DAMAGE.
61 */
62
63 #include <assert.h>
64 #include <compiler.h>
65 #include <inttypes.h>
66 #include <malloc.h>
67 #include <stdint.h>
68 #include <string.h>
69 #include <tee_api_private.h>
70 #include <tee_internal_api_extensions.h>
71 #include <trace.h>
72 #include <user_ta_header.h>
73 #include <utee_types.h>
74 #include "gmon.h"
75 #include "gmon_out.h"
76 #include "gprof_pta.h"
77
78 static void *gprof_buf;
79 static size_t gprof_buf_len;
80
81 #if defined(ARM32)
82 #define MCOUNT_SYM __gnu_mcount_nc
83 #elif defined(ARM64)
84 #define MCOUNT_SYM _mcount
85 #endif
86
dummy(void)87 static void dummy(void) {}
88 void (*MCOUNT_SYM)(void) __weak = dummy;
89
ta_instrumented(void)90 static bool ta_instrumented(void)
91 {
92 /*
93 * Return true if the mcount function is called somewhere (and therefore
94 * profiling should be initialized).
95 * Since gprof is not supported with shared libraries, checking if
96 * mcount is called is the same as checking if it is present in the
97 * TA binary, because the function would be eliminated at link time if
98 * not used.
99 */
100 return dummy != MCOUNT_SYM;
101 }
102
103 #undef MCOUNT_SYM
104
gprof_alloc(size_t len)105 static void *gprof_alloc(size_t len)
106 {
107 assert(!gprof_buf);
108 gprof_buf = tee_map_zi(len, TEE_MEMORY_ACCESS_ANY_OWNER);
109 gprof_buf_len = len;
110 return gprof_buf;
111 }
112
113 static struct gmonparam _gmonparam = { GMON_PROF_OFF };
114
115 static uint32_t _gprof_file_id; /* File id returned by tee-supplicant */
116
117 static int _gprof_s_scale;
118 #define SCALE_1_TO_1 0x10000L
119
120 /* Adjust PC so that gprof can locate it in the TA ELF file */
adjust_pc(unsigned long pc)121 static unsigned long __noprof adjust_pc(unsigned long pc)
122 {
123 return pc - (unsigned long)__text_start + sizeof(struct ta_head);
124 }
125
__utee_gprof_init(void)126 void __utee_gprof_init(void)
127 {
128 unsigned long lowpc;
129 unsigned long highpc;
130 struct gmonparam *p = &_gmonparam;
131 size_t bufsize;
132 TEE_Result res;
133 char *cp;
134
135 if (!ta_instrumented())
136 return;
137
138 lowpc = adjust_pc((unsigned long)__text_start);
139 highpc = adjust_pc((unsigned long)__text_end);
140
141 /*
142 * Round lowpc and highpc to multiples of the density we're using
143 * so the rest of the scaling (here and in gprof) stays in ints.
144 */
145 p->lowpc = ROUNDDOWN(lowpc, HISTFRACTION * sizeof(HISTCOUNTER));
146 p->highpc = ROUNDUP(highpc, HISTFRACTION * sizeof(HISTCOUNTER));
147 p->textsize = p->highpc - p->lowpc;
148 p->kcountsize = ROUNDUP(p->textsize / HISTFRACTION, sizeof(*p->froms));
149 p->hashfraction = HASHFRACTION;
150 p->log_hashfraction = -1;
151 /*
152 * The following test must be kept in sync with the corresponding
153 * test in __mcount_internal
154 */
155 if ((HASHFRACTION & (HASHFRACTION - 1)) == 0) {
156 /*
157 * If HASHFRACTION is a power of two, mcount can use shifting
158 * instead of integer division. Precompute shift amount.
159 */
160 p->log_hashfraction = __builtin_ffs(p->hashfraction *
161 sizeof(*p->froms)) - 1;
162 }
163 p->fromssize = p->textsize / HASHFRACTION;
164 p->tolimit = p->textsize * ARCDENSITY / 100;
165 if (p->tolimit < MINARCS)
166 p->tolimit = MINARCS;
167 else if (p->tolimit > MAXARCS)
168 p->tolimit = MAXARCS;
169 p->tossize = p->tolimit * sizeof(struct tostruct);
170
171 bufsize = p->kcountsize + p->fromssize + p->tossize;
172
173 IMSG("gprof: initializing");
174 DMSG("TA text size: %zu, gprof buffer size: %zu",
175 __text_end - __text_start, bufsize);
176
177 cp = gprof_alloc(bufsize);
178 if (!cp) {
179 EMSG("gprof: could not allocate profiling buffer");
180 p->tos = NULL;
181 p->state = GMON_PROF_ERROR;
182 return;
183 }
184
185 p->tos = (struct tostruct *)cp;
186 cp += p->tossize;
187 p->kcount = (HISTCOUNTER *)cp;
188 cp += p->kcountsize;
189 p->froms = (ARCINDEX *)cp;
190
191 p->tos[0].link = 0;
192
193 if (p->kcountsize < p->textsize)
194 _gprof_s_scale = ((float)p->kcountsize / p->textsize) *
195 SCALE_1_TO_1;
196 else
197 _gprof_s_scale = SCALE_1_TO_1;
198
199 res = __pta_gprof_pc_sampling_start(p->kcount, p->kcountsize,
200 p->lowpc +
201 ((unsigned long)__text_start -
202 sizeof(struct ta_head)),
203 _gprof_s_scale);
204 if (res != TEE_SUCCESS)
205 EMSG("gprof: could not start PC sampling (0x%08x)", res);
206
207 p->state = GMON_PROF_ON;
208 }
209
_gprof_write_buf(void * buf,size_t size)210 static void _gprof_write_buf(void *buf, size_t size)
211 {
212 TEE_Result res;
213
214 res = __pta_gprof_send(buf, size, &_gprof_file_id);
215 if (res != TEE_SUCCESS)
216 EMSG("gprof: could not send gprof data (0x%08x)", res);
217 }
218
_gprof_write_header(void)219 static void _gprof_write_header(void)
220 {
221 struct gmon_hdr ghdr;
222 size_t size = sizeof(struct gmon_hdr);
223
224 memcpy(&ghdr.cookie[0], GMON_MAGIC, sizeof(ghdr.cookie));
225 ghdr.version = GMON_VERSION;
226 memset(ghdr.spare, '\0', sizeof(ghdr.spare));
227
228 _gprof_write_buf(&ghdr, size);
229 }
230
_gprof_write_hist(void)231 static void _gprof_write_hist(void)
232 {
233 struct out_record {
234 uint8_t tag;
235 struct gmon_hist_hdr hist_hdr;
236 } __packed out = {
237 .tag = GMON_TAG_TIME_HIST,
238 .hist_hdr = {
239 .low_pc = _gmonparam.lowpc,
240 .high_pc = _gmonparam.highpc,
241 .hist_size = _gmonparam.kcountsize/sizeof(HISTCOUNTER),
242 .prof_rate = _gmonparam.prof_rate,
243 .dimen = "seconds",
244 .dimen_abbrev = 's',
245 }
246 };
247
248 _gprof_write_buf(&out, sizeof(out));
249 _gprof_write_buf(_gmonparam.kcount, _gmonparam.kcountsize);
250 }
251
_gprof_write_call_graph(void)252 static void _gprof_write_call_graph(void)
253 {
254 #define NARCS_PER_WRITE 16
255 struct out_record {
256 uint8_t tag;
257 uint8_t data[sizeof(struct gmon_cg_arc_record)];
258 } out[NARCS_PER_WRITE];
259 struct gmon_cg_arc_record arc;
260 ARCINDEX from_index, to_index;
261 unsigned long from_len;
262 unsigned long frompc;
263 int nfilled = 0;
264
265 from_len = _gmonparam.fromssize / sizeof(*_gmonparam.froms);
266
267 for (from_index = 0; from_index < from_len; ++from_index) {
268
269 if (_gmonparam.froms[from_index] == 0)
270 continue;
271
272 frompc = _gmonparam.lowpc;
273 frompc += (from_index * _gmonparam.hashfraction
274 * sizeof(*_gmonparam.froms));
275 for (to_index = _gmonparam.froms[from_index];
276 to_index != 0;
277 to_index = _gmonparam.tos[to_index].link) {
278
279 arc.from_pc = frompc;
280 arc.self_pc = _gmonparam.tos[to_index].selfpc;
281 arc.count = _gmonparam.tos[to_index].count;
282
283 out[nfilled].tag = GMON_TAG_CG_ARC;
284 memcpy(out[nfilled].data, &arc, sizeof(arc));
285
286 if (++nfilled == NARCS_PER_WRITE) {
287 _gprof_write_buf(out, sizeof(out));
288 nfilled = 0;
289 }
290 }
291 }
292 if (nfilled > 0)
293 _gprof_write_buf(out, nfilled * sizeof(out[0]));
294 }
295
296 /* Stop profiling and send profile data in gmon.out format to Normal World */
__utee_gprof_fini(void)297 void __utee_gprof_fini(void)
298 {
299 TEE_Result res;
300
301 if (_gmonparam.state != GMON_PROF_ON)
302 return;
303
304 /* Stop call graph tracing */
305 _gmonparam.state = GMON_PROF_OFF_EXITING;
306
307 /* Stop TA sampling */
308 res = __pta_gprof_pc_sampling_stop(&_gmonparam.prof_rate);
309
310 _gprof_write_header();
311 if (res == TEE_SUCCESS)
312 _gprof_write_hist();
313 _gprof_write_call_graph();
314
315 __pta_gprof_fini();
316
317 if (gprof_buf) {
318 res = tee_unmap(gprof_buf, gprof_buf_len);
319 assert(!res);
320 gprof_buf = NULL;
321 }
322 }
323
324 /*
325 * Called from the assembly stub (_mcount or __gnu_mcount_nc).
326 *
327 * __mcount_internal updates data structures that represent traversals of the
328 * program's call graph edges. frompc and selfpc are the return
329 * address and function address that represents the given call graph edge.
330 */
__mcount_internal(unsigned long frompc,unsigned long selfpc)331 void __noprof __mcount_internal(unsigned long frompc, unsigned long selfpc)
332 {
333 ARCINDEX *frompcindex;
334 struct tostruct *top, *prevtop;
335 struct gmonparam *p;
336 ARCINDEX toindex;
337 int i;
338
339 p = &_gmonparam;
340
341 /*
342 * Check that we are profiling and that we aren't recursively invoked.
343 */
344 if (p->state != GMON_PROF_ON)
345 return;
346 p->state = GMON_PROF_BUSY;
347
348 frompc = adjust_pc(frompc);
349 selfpc = adjust_pc(selfpc);
350
351 /* Check that frompcindex is a reasonable pc value. */
352 frompc -= p->lowpc;
353 if (frompc > p->textsize)
354 goto done;
355
356 /* Note: keep in sync. with the initialization function above */
357 if ((HASHFRACTION & (HASHFRACTION - 1)) == 0) {
358 /* Avoid integer divide if possible */
359 i = frompc >> p->log_hashfraction;
360 } else {
361 i = frompc / (p->hashfraction * sizeof(*p->froms));
362 }
363 frompcindex = &p->froms[i];
364 toindex = *frompcindex;
365 if (toindex == 0) {
366 /* First time traversing this arc */
367 toindex = ++p->tos[0].link;
368 if (toindex >= p->tolimit) {
369 /* Halt further profiling */
370 goto overflow;
371 }
372
373 *frompcindex = toindex;
374 top = &p->tos[toindex];
375 top->selfpc = selfpc;
376 top->count = 1;
377 top->link = 0;
378 goto done;
379 }
380 top = &p->tos[toindex];
381 if (top->selfpc == selfpc) {
382 /* Arc at front of chain; usual case */
383 top->count++;
384 goto done;
385 }
386 /*
387 * Have to go looking down chain for it.
388 * top points to what we are looking at,
389 * prevtop points to previous top.
390 * we know it is not at the head of the chain.
391 */
392 for (;;) {
393 if (top->link == 0) {
394 /*
395 * top is end of the chain and none of the chain
396 * had top->selfpc == selfpc.
397 * so we allocate a new tostruct
398 * and link it to the head of the chain.
399 */
400 toindex = ++p->tos[0].link;
401 if (toindex >= p->tolimit)
402 goto overflow;
403
404 top = &p->tos[toindex];
405 top->selfpc = selfpc;
406 top->count = 1;
407 top->link = *frompcindex;
408 *frompcindex = toindex;
409 goto done;
410 }
411 /*
412 * Otherwise, check the next arc on the chain.
413 */
414 prevtop = top;
415 top = &p->tos[top->link];
416 if (top->selfpc == selfpc) {
417 /*
418 * There it is. Increment its count, move it to the
419 * head of the chain.
420 */
421 top->count++;
422 toindex = prevtop->link;
423 prevtop->link = top->link;
424 top->link = *frompcindex;
425 *frompcindex = toindex;
426 goto done;
427 }
428 }
429 done:
430 p->state = GMON_PROF_ON;
431 return;
432 overflow:
433 p->state = GMON_PROF_ERROR;
434 }
435