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