Initialize static TLS for the benchmarks
[riscv-tests.git] / benchmarks / common / syscalls.c
1 #include <stdint.h>
2 #include <string.h>
3 #include <stdarg.h>
4 #include <stdio.h>
5 #include <limits.h>
6 #include <machine/syscall.h>
7 #include "util.h"
8
9 #define SYS_stats 1234
10
11 // initialized in crt.S
12 int have_vec;
13
14 static long handle_frontend_syscall(long which, long arg0, long arg1, long arg2)
15 {
16 volatile uint64_t magic_mem[8] __attribute__((aligned(64)));
17 magic_mem[0] = which;
18 magic_mem[1] = arg0;
19 magic_mem[2] = arg1;
20 magic_mem[3] = arg2;
21 __sync_synchronize();
22 write_csr(tohost, (long)magic_mem);
23 while (swap_csr(fromhost, 0) == 0);
24 return magic_mem[0];
25 }
26
27 // In setStats, we might trap reading uarch-specific counters.
28 // The trap handler will skip over the instruction and write 0,
29 // but only if a0 is the destination register.
30 #define read_csr_safe(reg) ({ register long __tmp asm("a0"); \
31 asm volatile ("csrr %0, " #reg : "=r"(__tmp)); \
32 __tmp; })
33
34 #define NUM_COUNTERS 18
35 static long counters[NUM_COUNTERS];
36 static char* counter_names[NUM_COUNTERS];
37 static int handle_stats(int enable)
38 {
39 //use csrs to set stats register
40 if (enable)
41 asm volatile ("csrrs a0, stats, 1" ::: "a0");
42 int i = 0;
43 #define READ_CTR(name) do { \
44 while (i >= NUM_COUNTERS) ; \
45 long csr = read_csr_safe(name); \
46 if (!enable) { csr -= counters[i]; counter_names[i] = #name; } \
47 counters[i++] = csr; \
48 } while (0)
49 READ_CTR(cycle); READ_CTR(instret);
50 READ_CTR(uarch0); READ_CTR(uarch1); READ_CTR(uarch2); READ_CTR(uarch3);
51 READ_CTR(uarch4); READ_CTR(uarch5); READ_CTR(uarch6); READ_CTR(uarch7);
52 READ_CTR(uarch8); READ_CTR(uarch9); READ_CTR(uarch10); READ_CTR(uarch11);
53 READ_CTR(uarch12); READ_CTR(uarch13); READ_CTR(uarch14); READ_CTR(uarch15);
54 #undef READ_CTR
55 if (!enable)
56 asm volatile ("csrrc a0, stats, 1" ::: "a0");
57 return 0;
58 }
59
60 static void tohost_exit(int code)
61 {
62 write_csr(tohost, (code << 1) | 1);
63 while (1);
64 }
65
66 long handle_trap(long cause, long epc, long regs[32])
67 {
68 int* csr_insn;
69 asm ("jal %0, 1f; csrr a0, stats; 1:" : "=r"(csr_insn));
70 long sys_ret = 0;
71
72 if (cause == CAUSE_ILLEGAL_INSTRUCTION &&
73 (*(int*)epc & *csr_insn) == *csr_insn)
74 ;
75 else if (cause != CAUSE_SYSCALL)
76 tohost_exit(1337);
77 else if (regs[17] == SYS_exit)
78 tohost_exit(regs[10]);
79 else if (regs[17] == SYS_stats)
80 sys_ret = handle_stats(regs[10]);
81 else
82 sys_ret = handle_frontend_syscall(regs[17], regs[10], regs[11], regs[12]);
83
84 regs[10] = sys_ret;
85 return epc+4;
86 }
87
88 static long syscall(long num, long arg0, long arg1, long arg2)
89 {
90 register long a7 asm("a7") = num;
91 register long a0 asm("a0") = arg0;
92 register long a1 asm("a1") = arg1;
93 register long a2 asm("a2") = arg2;
94 asm volatile ("scall" : "+r"(a0) : "r"(a1), "r"(a2), "r"(a7));
95 return a0;
96 }
97
98 void exit(int code)
99 {
100 syscall(SYS_exit, code, 0, 0);
101 while (1);
102 }
103
104 void setStats(int enable)
105 {
106 syscall(SYS_stats, enable, 0, 0);
107 }
108
109 void printstr(const char* s)
110 {
111 syscall(SYS_write, 1, (long)s, strlen(s));
112 }
113
114 void __attribute__((weak)) thread_entry(int cid, int nc)
115 {
116 // multi-threaded programs override this function.
117 // for the case of single-threaded programs, only let core 0 proceed.
118 while (cid != 0);
119 }
120
121 int __attribute__((weak)) main(int argc, char** argv)
122 {
123 // single-threaded programs override this function.
124 printstr("Implement main(), foo!\n");
125 return -1;
126 }
127
128 static void init_tls()
129 {
130 register void* thread_pointer asm("tp");
131 extern char _tls_data;
132 extern __thread char _tdata_begin, _tdata_end, _tbss_end;
133 size_t tdata_size = &_tdata_end - &_tdata_begin;
134 memcpy(thread_pointer, &_tls_data, tdata_size);
135 size_t tbss_size = &_tbss_end - &_tdata_end;
136 memset(thread_pointer + tdata_size, 0, tbss_size);
137 }
138
139 void _init(int cid, int nc)
140 {
141 init_tls();
142 thread_entry(cid, nc);
143
144 // only single-threaded programs should ever get here.
145 int ret = main(0, 0);
146
147 char buf[NUM_COUNTERS * 32] __attribute__((aligned(64)));
148 char* pbuf = buf;
149 for (int i = 0; i < NUM_COUNTERS; i++)
150 if (counters[i])
151 pbuf += sprintf(pbuf, "%s = %d\n", counter_names[i], counters[i]);
152 if (pbuf != buf)
153 printstr(buf);
154
155 exit(ret);
156 }
157
158 #undef putchar
159 int putchar(int ch)
160 {
161 static __thread char buf[64] __attribute__((aligned(64)));
162 static __thread int buflen = 0;
163
164 buf[buflen++] = ch;
165
166 if (ch == '\n' || buflen == sizeof(buf))
167 {
168 syscall(SYS_write, 1, (long)buf, buflen);
169 buflen = 0;
170 }
171
172 return 0;
173 }
174
175 void printhex(uint64_t x)
176 {
177 char str[17];
178 int i;
179 for (i = 0; i < 16; i++)
180 {
181 str[15-i] = (x & 0xF) + ((x & 0xF) < 10 ? '0' : 'a'-10);
182 x >>= 4;
183 }
184 str[16] = 0;
185
186 printstr(str);
187 }
188
189 static inline void printnum(void (*putch)(int, void**), void **putdat,
190 unsigned long long num, unsigned base, int width, int padc)
191 {
192 unsigned digs[sizeof(num)*CHAR_BIT];
193 int pos = 0;
194
195 while (1)
196 {
197 digs[pos++] = num % base;
198 if (num < base)
199 break;
200 num /= base;
201 }
202
203 while (width-- > pos)
204 putch(padc, putdat);
205
206 while (pos-- > 0)
207 putch(digs[pos] + (digs[pos] >= 10 ? 'a' - 10 : '0'), putdat);
208 }
209
210 static unsigned long long getuint(va_list *ap, int lflag)
211 {
212 if (lflag >= 2)
213 return va_arg(*ap, unsigned long long);
214 else if (lflag)
215 return va_arg(*ap, unsigned long);
216 else
217 return va_arg(*ap, unsigned int);
218 }
219
220 static long long getint(va_list *ap, int lflag)
221 {
222 if (lflag >= 2)
223 return va_arg(*ap, long long);
224 else if (lflag)
225 return va_arg(*ap, long);
226 else
227 return va_arg(*ap, int);
228 }
229
230 static void vprintfmt(void (*putch)(int, void**), void **putdat, const char *fmt, va_list ap)
231 {
232 register const char* p;
233 const char* last_fmt;
234 register int ch, err;
235 unsigned long long num;
236 int base, lflag, width, precision, altflag;
237 char padc;
238
239 while (1) {
240 while ((ch = *(unsigned char *) fmt) != '%') {
241 if (ch == '\0')
242 return;
243 fmt++;
244 putch(ch, putdat);
245 }
246 fmt++;
247
248 // Process a %-escape sequence
249 last_fmt = fmt;
250 padc = ' ';
251 width = -1;
252 precision = -1;
253 lflag = 0;
254 altflag = 0;
255 reswitch:
256 switch (ch = *(unsigned char *) fmt++) {
257
258 // flag to pad on the right
259 case '-':
260 padc = '-';
261 goto reswitch;
262
263 // flag to pad with 0's instead of spaces
264 case '0':
265 padc = '0';
266 goto reswitch;
267
268 // width field
269 case '1':
270 case '2':
271 case '3':
272 case '4':
273 case '5':
274 case '6':
275 case '7':
276 case '8':
277 case '9':
278 for (precision = 0; ; ++fmt) {
279 precision = precision * 10 + ch - '0';
280 ch = *fmt;
281 if (ch < '0' || ch > '9')
282 break;
283 }
284 goto process_precision;
285
286 case '*':
287 precision = va_arg(ap, int);
288 goto process_precision;
289
290 case '.':
291 if (width < 0)
292 width = 0;
293 goto reswitch;
294
295 case '#':
296 altflag = 1;
297 goto reswitch;
298
299 process_precision:
300 if (width < 0)
301 width = precision, precision = -1;
302 goto reswitch;
303
304 // long flag (doubled for long long)
305 case 'l':
306 lflag++;
307 goto reswitch;
308
309 // character
310 case 'c':
311 putch(va_arg(ap, int), putdat);
312 break;
313
314 // string
315 case 's':
316 if ((p = va_arg(ap, char *)) == NULL)
317 p = "(null)";
318 if (width > 0 && padc != '-')
319 for (width -= strnlen(p, precision); width > 0; width--)
320 putch(padc, putdat);
321 for (; (ch = *p) != '\0' && (precision < 0 || --precision >= 0); width--) {
322 putch(ch, putdat);
323 p++;
324 }
325 for (; width > 0; width--)
326 putch(' ', putdat);
327 break;
328
329 // (signed) decimal
330 case 'd':
331 num = getint(&ap, lflag);
332 if ((long long) num < 0) {
333 putch('-', putdat);
334 num = -(long long) num;
335 }
336 base = 10;
337 goto signed_number;
338
339 // unsigned decimal
340 case 'u':
341 base = 10;
342 goto unsigned_number;
343
344 // (unsigned) octal
345 case 'o':
346 // should do something with padding so it's always 3 octits
347 base = 8;
348 goto unsigned_number;
349
350 // pointer
351 case 'p':
352 static_assert(sizeof(long) == sizeof(void*));
353 lflag = 1;
354 putch('0', putdat);
355 putch('x', putdat);
356 /* fall through to 'x' */
357
358 // (unsigned) hexadecimal
359 case 'x':
360 base = 16;
361 unsigned_number:
362 num = getuint(&ap, lflag);
363 signed_number:
364 printnum(putch, putdat, num, base, width, padc);
365 break;
366
367 // escaped '%' character
368 case '%':
369 putch(ch, putdat);
370 break;
371
372 // unrecognized escape sequence - just print it literally
373 default:
374 putch('%', putdat);
375 fmt = last_fmt;
376 break;
377 }
378 }
379 }
380
381 int printf(const char* fmt, ...)
382 {
383 va_list ap;
384 va_start(ap, fmt);
385
386 vprintfmt((void*)putchar, 0, fmt, ap);
387
388 va_end(ap);
389 return 0; // incorrect return value, but who cares, anyway?
390 }
391
392 int sprintf(char* str, const char* fmt, ...)
393 {
394 va_list ap;
395 char* str0 = str;
396 va_start(ap, fmt);
397
398 void sprintf_putch(int ch, void** data)
399 {
400 char** pstr = (char**)data;
401 **pstr = ch;
402 (*pstr)++;
403 }
404
405 vprintfmt(sprintf_putch, (void**)&str, fmt, ap);
406 *str = 0;
407
408 va_end(ap);
409 return str - str0;
410 }