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