xref: /freebsd/stand/libsa/tslog.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
1 /*-
2  * Copyright (c) 2021 Colin Percival
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
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 #include <sys/types.h>
29 
30 #if defined(__amd64__) || defined(__i386__)
31 #include <machine/cpufunc.h>
32 #elif defined(__aarch64__)
33 #include <machine/armreg.h>
34 #endif
35 
36 #include <stand.h>
37 
38 /* Buffer for holding tslog data in string format. */
39 static char * tslog_buf = NULL;
40 static size_t tslog_buflen = 0;
41 static size_t tslog_bufpos = 0;
42 
43 static size_t
44 tsccat(char * buf, uint64_t tsc)
45 {
46 	size_t len;
47 
48 	/* Handle upper digits. */
49 	if (tsc >= 10)
50 		len = tsccat(buf, tsc / 10);
51 	else
52 		len = 0;
53 
54 	/* Write the last digit. */
55 	buf[len] = "0123456789"[tsc % 10];
56 
57 	/* Return the length written. */
58 	return (len + 1);
59 }
60 
61 void
62 tslog_setbuf(void * buf, size_t len)
63 {
64 
65 	tslog_buf = (char *)buf;
66 	tslog_buflen = len;
67 	tslog_bufpos = 0;
68 }
69 
70 void
71 tslog_getbuf(void ** buf, size_t * len)
72 {
73 
74 	*buf = (void *)tslog_buf;
75 	*len = tslog_bufpos;
76 }
77 
78 void
79 tslog(const char * type, const char * f, const char * s)
80 {
81 #if defined(__amd64__) || defined(__i386__)
82 	uint64_t tsc = rdtsc();
83 #elif defined(__aarch64__)
84 	uint64_t tsc = READ_SPECIALREG(cntvct_el0);
85 #else
86 	uint64_t tsc = 0;
87 #endif
88 
89 	/* If we have no buffer, do nothing. */
90 	if (tslog_buf == NULL)
91 		return;
92 
93 	/* Check that we have enough space. */
94 	if (tslog_buflen - tslog_bufpos < 32 + strlen(type) + strlen(f) +
95 	    (s ? strlen(s) : 0))
96 		return;
97 
98 	/* Append to existing buffer. */
99 	strcpy(&tslog_buf[tslog_bufpos], "0x0 ");
100 	tslog_bufpos += 4;
101 	tslog_bufpos += tsccat(&tslog_buf[tslog_bufpos], tsc);
102 	strcpy(&tslog_buf[tslog_bufpos], " ");
103 	tslog_bufpos += 1;
104 	strcpy(&tslog_buf[tslog_bufpos], type);
105 	tslog_bufpos += strlen(type);
106 	strcpy(&tslog_buf[tslog_bufpos], " ");
107 	tslog_bufpos += 1;
108 	strcpy(&tslog_buf[tslog_bufpos], f);
109 	tslog_bufpos += strlen(f);
110 	if (s != NULL) {
111 		strcpy(&tslog_buf[tslog_bufpos], " ");
112 		tslog_bufpos += 1;
113 		strcpy(&tslog_buf[tslog_bufpos], s);
114 		tslog_bufpos += strlen(s);
115 	}
116 	strcpy(&tslog_buf[tslog_bufpos], "\n");
117 	tslog_bufpos += 1;
118 }
119