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