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