xref: /freebsd/stand/libsa/tslog.c (revision 7c43148a974877188a930e4078a164f83da8e652)
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/types.h>
28e193d3baSColin Percival 
29e6caac71SColin Percival #if defined(__amd64__) || defined(__i386__)
30e193d3baSColin Percival #include <machine/cpufunc.h>
31e6caac71SColin Percival #elif defined(__aarch64__)
32e6caac71SColin Percival #include <machine/armreg.h>
33e6caac71SColin Percival #endif
34e193d3baSColin Percival 
35e193d3baSColin Percival #include <stand.h>
36e193d3baSColin Percival 
37e193d3baSColin Percival /* Buffer for holding tslog data in string format. */
38e193d3baSColin Percival static char * tslog_buf = NULL;
39e193d3baSColin Percival static size_t tslog_buflen = 0;
40e193d3baSColin Percival static size_t tslog_bufpos = 0;
41e193d3baSColin Percival 
42*242923ebSColin Percival static size_t
tsccat(char * buf,uint64_t tsc)43*242923ebSColin Percival tsccat(char * buf, uint64_t tsc)
44*242923ebSColin Percival {
45*242923ebSColin Percival 	size_t len;
46*242923ebSColin Percival 
47*242923ebSColin Percival 	/* Handle upper digits. */
48*242923ebSColin Percival 	if (tsc >= 10)
49*242923ebSColin Percival 		len = tsccat(buf, tsc / 10);
50*242923ebSColin Percival 	else
51*242923ebSColin Percival 		len = 0;
52*242923ebSColin Percival 
53*242923ebSColin Percival 	/* Write the last digit. */
54*242923ebSColin Percival 	buf[len] = "0123456789"[tsc % 10];
55*242923ebSColin Percival 
56*242923ebSColin Percival 	/* Return the length written. */
57*242923ebSColin Percival 	return (len + 1);
58*242923ebSColin Percival }
59*242923ebSColin Percival 
60e193d3baSColin Percival void
tslog_setbuf(void * buf,size_t len)61e193d3baSColin Percival tslog_setbuf(void * buf, size_t len)
62e193d3baSColin Percival {
63e193d3baSColin Percival 
64e193d3baSColin Percival 	tslog_buf = (char *)buf;
65e193d3baSColin Percival 	tslog_buflen = len;
66e193d3baSColin Percival 	tslog_bufpos = 0;
67e193d3baSColin Percival }
68e193d3baSColin Percival 
69e193d3baSColin Percival void
tslog_getbuf(void ** buf,size_t * len)70e193d3baSColin Percival tslog_getbuf(void ** buf, size_t * len)
71e193d3baSColin Percival {
72e193d3baSColin Percival 
73e193d3baSColin Percival 	*buf = (void *)tslog_buf;
74e193d3baSColin Percival 	*len = tslog_bufpos;
75e193d3baSColin Percival }
76e193d3baSColin Percival 
77e193d3baSColin Percival void
tslog(const char * type,const char * f,const char * s)78e193d3baSColin Percival tslog(const char * type, const char * f, const char * s)
79e193d3baSColin Percival {
80e193d3baSColin Percival #if defined(__amd64__) || defined(__i386__)
81e193d3baSColin Percival 	uint64_t tsc = rdtsc();
82e6caac71SColin Percival #elif defined(__aarch64__)
83e6caac71SColin Percival 	uint64_t tsc = READ_SPECIALREG(cntvct_el0);
84e193d3baSColin Percival #else
85e193d3baSColin Percival 	uint64_t tsc = 0;
86e193d3baSColin Percival #endif
87e193d3baSColin Percival 
88e193d3baSColin Percival 	/* If we have no buffer, do nothing. */
89e193d3baSColin Percival 	if (tslog_buf == NULL)
90e193d3baSColin Percival 		return;
91e193d3baSColin Percival 
92*242923ebSColin Percival 	/* Check that we have enough space. */
93*242923ebSColin Percival 	if (tslog_buflen - tslog_bufpos < 32 + strlen(type) + strlen(f) +
94*242923ebSColin Percival 	    (s ? strlen(s) : 0))
95*242923ebSColin Percival 		return;
96*242923ebSColin Percival 
97*242923ebSColin Percival 	/* Append to existing buffer. */
98*242923ebSColin Percival 	strcpy(&tslog_buf[tslog_bufpos], "0x0 ");
99*242923ebSColin Percival 	tslog_bufpos += 4;
100*242923ebSColin Percival 	tslog_bufpos += tsccat(&tslog_buf[tslog_bufpos], tsc);
101*242923ebSColin Percival 	strcpy(&tslog_buf[tslog_bufpos], " ");
102*242923ebSColin Percival 	tslog_bufpos += 1;
103*242923ebSColin Percival 	strcpy(&tslog_buf[tslog_bufpos], type);
104*242923ebSColin Percival 	tslog_bufpos += strlen(type);
105*242923ebSColin Percival 	strcpy(&tslog_buf[tslog_bufpos], " ");
106*242923ebSColin Percival 	tslog_bufpos += 1;
107*242923ebSColin Percival 	strcpy(&tslog_buf[tslog_bufpos], f);
108*242923ebSColin Percival 	tslog_bufpos += strlen(f);
109*242923ebSColin Percival 	if (s != NULL) {
110*242923ebSColin Percival 		strcpy(&tslog_buf[tslog_bufpos], " ");
111*242923ebSColin Percival 		tslog_bufpos += 1;
112*242923ebSColin Percival 		strcpy(&tslog_buf[tslog_bufpos], s);
113*242923ebSColin Percival 		tslog_bufpos += strlen(s);
114*242923ebSColin Percival 	}
115*242923ebSColin Percival 	strcpy(&tslog_buf[tslog_bufpos], "\n");
116*242923ebSColin Percival 	tslog_bufpos += 1;
117e193d3baSColin Percival }
118