1e193d3baSColin Percival /*- 2e193d3baSColin Percival * Copyright (c) 2021 Colin Percival 3e193d3baSColin Percival * All rights reserved. 4e193d3baSColin Percival * 5e193d3baSColin Percival * Redistribution and use in source and binary forms, with or without 6e193d3baSColin Percival * modification, are permitted provided that the following conditions 7e193d3baSColin Percival * are met: 8e193d3baSColin Percival * 1. Redistributions of source code must retain the above copyright 9e193d3baSColin Percival * notice, this list of conditions and the following disclaimer. 10e193d3baSColin Percival * 2. Redistributions in binary form must reproduce the above copyright 11e193d3baSColin Percival * notice, this list of conditions and the following disclaimer in the 12e193d3baSColin Percival * documentation and/or other materials provided with the distribution. 13e193d3baSColin Percival * 14e193d3baSColin Percival * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15e193d3baSColin Percival * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16e193d3baSColin Percival * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17e193d3baSColin Percival * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18e193d3baSColin Percival * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19e193d3baSColin Percival * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20e193d3baSColin Percival * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21e193d3baSColin Percival * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22e193d3baSColin Percival * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23e193d3baSColin Percival * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24e193d3baSColin Percival * SUCH DAMAGE. 25e193d3baSColin Percival */ 26e193d3baSColin Percival 27e193d3baSColin Percival #include <sys/cdefs.h> 28e193d3baSColin Percival __FBSDID("$FreeBSD$"); 29e193d3baSColin Percival 30e193d3baSColin Percival #include <sys/types.h> 31e193d3baSColin Percival 32*e6caac71SColin Percival #if defined(__amd64__) || defined(__i386__) 33e193d3baSColin Percival #include <machine/cpufunc.h> 34*e6caac71SColin Percival #elif defined(__aarch64__) 35*e6caac71SColin Percival #include <machine/armreg.h> 36*e6caac71SColin Percival #endif 37e193d3baSColin Percival 38e193d3baSColin Percival #include <stand.h> 39e193d3baSColin Percival 40e193d3baSColin Percival /* Buffer for holding tslog data in string format. */ 41e193d3baSColin Percival static char * tslog_buf = NULL; 42e193d3baSColin Percival static size_t tslog_buflen = 0; 43e193d3baSColin Percival static size_t tslog_bufpos = 0; 44e193d3baSColin Percival 45e193d3baSColin Percival void 46e193d3baSColin Percival tslog_setbuf(void * buf, size_t len) 47e193d3baSColin Percival { 48e193d3baSColin Percival 49e193d3baSColin Percival tslog_buf = (char *)buf; 50e193d3baSColin Percival tslog_buflen = len; 51e193d3baSColin Percival tslog_bufpos = 0; 52e193d3baSColin Percival } 53e193d3baSColin Percival 54e193d3baSColin Percival void 55e193d3baSColin Percival tslog_getbuf(void ** buf, size_t * len) 56e193d3baSColin Percival { 57e193d3baSColin Percival 58e193d3baSColin Percival *buf = (void *)tslog_buf; 59e193d3baSColin Percival *len = tslog_bufpos; 60e193d3baSColin Percival } 61e193d3baSColin Percival 62e193d3baSColin Percival void 63e193d3baSColin Percival tslog(const char * type, const char * f, const char * s) 64e193d3baSColin Percival { 65e193d3baSColin Percival #if defined(__amd64__) || defined(__i386__) 66e193d3baSColin Percival uint64_t tsc = rdtsc(); 67*e6caac71SColin Percival #elif defined(__aarch64__) 68*e6caac71SColin Percival uint64_t tsc = READ_SPECIALREG(cntvct_el0); 69e193d3baSColin Percival #else 70e193d3baSColin Percival uint64_t tsc = 0; 71e193d3baSColin Percival #endif 72e193d3baSColin Percival int len; 73e193d3baSColin Percival 74e193d3baSColin Percival /* If we have no buffer, do nothing. */ 75e193d3baSColin Percival if (tslog_buf == NULL) 76e193d3baSColin Percival return; 77e193d3baSColin Percival 78e193d3baSColin Percival /* Append to existing buffer, if we have enough space. */ 79e193d3baSColin Percival len = snprintf(&tslog_buf[tslog_bufpos], 80e193d3baSColin Percival tslog_buflen - tslog_bufpos, "0x0 %llu %s %s%s%s\n", 81e193d3baSColin Percival (unsigned long long)tsc, type, f, s ? " " : "", s ? s : ""); 82e193d3baSColin Percival if ((len > 0) && (tslog_bufpos + len <= tslog_buflen)) 83e193d3baSColin Percival tslog_bufpos += len; 84e193d3baSColin Percival } 85