Use new toolchain and calling convention
[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 void _init(int cid, int nc)
129 {
130 thread_entry(cid, nc);
131
132 // only single-threaded programs should ever get here.
133 int ret = main(0, 0);
134
135 char buf[NUM_COUNTERS * 32] __attribute__((aligned(64)));
136 char* pbuf = buf;
137 for (int i = 0; i < NUM_COUNTERS; i++)
138 if (counters[i])
139 pbuf += sprintf(pbuf, "%s = %d\n", counter_names[i], counters[i]);
140 if (pbuf != buf)
141 printstr(buf);
142
143 exit(ret);
144 }
145
146 #undef putchar
147 int putchar(int ch)
148 {
149 static __thread char buf[64] __attribute__((aligned(64)));
150 static __thread int buflen = 0;
151
152 buf[buflen++] = ch;
153
154 if (ch == '\n' || buflen == sizeof(buf))
155 {
156 syscall(SYS_write, 1, (long)buf, buflen);
157 buflen = 0;
158 }
159
160 return 0;
161 }
162
163 void printhex(uint64_t x)
164 {
165 char str[17];
166 int i;
167 for (i = 0; i < 16; i++)
168 {
169 str[15-i] = (x & 0xF) + ((x & 0xF) < 10 ? '0' : 'a'-10);
170 x >>= 4;
171 }
172 str[16] = 0;
173
174 printstr(str);
175 }
176
177 static inline void printnum(void (*putch)(int, void**), void **putdat,
178 unsigned long long num, unsigned base, int width, int padc)
179 {
180 unsigned digs[sizeof(num)*CHAR_BIT];
181 int pos = 0;
182
183 while (1)
184 {
185 digs[pos++] = num % base;
186 if (num < base)
187 break;
188 num /= base;
189 }
190
191 while (width-- > pos)
192 putch(padc, putdat);
193
194 while (pos-- > 0)
195 putch(digs[pos] + (digs[pos] >= 10 ? 'a' - 10 : '0'), putdat);
196 }
197
198 static unsigned long long getuint(va_list *ap, int lflag)
199 {
200 if (lflag >= 2)
201 return va_arg(*ap, unsigned long long);
202 else if (lflag)
203 return va_arg(*ap, unsigned long);
204 else
205 return va_arg(*ap, unsigned int);
206 }
207
208 static long long getint(va_list *ap, int lflag)
209 {
210 if (lflag >= 2)
211 return va_arg(*ap, long long);
212 else if (lflag)
213 return va_arg(*ap, long);
214 else
215 return va_arg(*ap, int);
216 }
217
218 static void vprintfmt(void (*putch)(int, void**), void **putdat, const char *fmt, va_list ap)
219 {
220 register const char* p;
221 const char* last_fmt;
222 register int ch, err;
223 unsigned long long num;
224 int base, lflag, width, precision, altflag;
225 char padc;
226
227 while (1) {
228 while ((ch = *(unsigned char *) fmt) != '%') {
229 if (ch == '\0')
230 return;
231 fmt++;
232 putch(ch, putdat);
233 }
234 fmt++;
235
236 // Process a %-escape sequence
237 last_fmt = fmt;
238 padc = ' ';
239 width = -1;
240 precision = -1;
241 lflag = 0;
242 altflag = 0;
243 reswitch:
244 switch (ch = *(unsigned char *) fmt++) {
245
246 // flag to pad on the right
247 case '-':
248 padc = '-';
249 goto reswitch;
250
251 // flag to pad with 0's instead of spaces
252 case '0':
253 padc = '0';
254 goto reswitch;
255
256 // width field
257 case '1':
258 case '2':
259 case '3':
260 case '4':
261 case '5':
262 case '6':
263 case '7':
264 case '8':
265 case '9':
266 for (precision = 0; ; ++fmt) {
267 precision = precision * 10 + ch - '0';
268 ch = *fmt;
269 if (ch < '0' || ch > '9')
270 break;
271 }
272 goto process_precision;
273
274 case '*':
275 precision = va_arg(ap, int);
276 goto process_precision;
277
278 case '.':
279 if (width < 0)
280 width = 0;
281 goto reswitch;
282
283 case '#':
284 altflag = 1;
285 goto reswitch;
286
287 process_precision:
288 if (width < 0)
289 width = precision, precision = -1;
290 goto reswitch;
291
292 // long flag (doubled for long long)
293 case 'l':
294 lflag++;
295 goto reswitch;
296
297 // character
298 case 'c':
299 putch(va_arg(ap, int), putdat);
300 break;
301
302 // string
303 case 's':
304 if ((p = va_arg(ap, char *)) == NULL)
305 p = "(null)";
306 if (width > 0 && padc != '-')
307 for (width -= strnlen(p, precision); width > 0; width--)
308 putch(padc, putdat);
309 for (; (ch = *p) != '\0' && (precision < 0 || --precision >= 0); width--) {
310 putch(ch, putdat);
311 p++;
312 }
313 for (; width > 0; width--)
314 putch(' ', putdat);
315 break;
316
317 // (signed) decimal
318 case 'd':
319 num = getint(&ap, lflag);
320 if ((long long) num < 0) {
321 putch('-', putdat);
322 num = -(long long) num;
323 }
324 base = 10;
325 goto signed_number;
326
327 // unsigned decimal
328 case 'u':
329 base = 10;
330 goto unsigned_number;
331
332 // (unsigned) octal
333 case 'o':
334 // should do something with padding so it's always 3 octits
335 base = 8;
336 goto unsigned_number;
337
338 // pointer
339 case 'p':
340 static_assert(sizeof(long) == sizeof(void*));
341 lflag = 1;
342 putch('0', putdat);
343 putch('x', putdat);
344 /* fall through to 'x' */
345
346 // (unsigned) hexadecimal
347 case 'x':
348 base = 16;
349 unsigned_number:
350 num = getuint(&ap, lflag);
351 signed_number:
352 printnum(putch, putdat, num, base, width, padc);
353 break;
354
355 // escaped '%' character
356 case '%':
357 putch(ch, putdat);
358 break;
359
360 // unrecognized escape sequence - just print it literally
361 default:
362 putch('%', putdat);
363 fmt = last_fmt;
364 break;
365 }
366 }
367 }
368
369 int printf(const char* fmt, ...)
370 {
371 va_list ap;
372 va_start(ap, fmt);
373
374 vprintfmt((void*)putchar, 0, fmt, ap);
375
376 va_end(ap);
377 return 0; // incorrect return value, but who cares, anyway?
378 }
379
380 int sprintf(char* str, const char* fmt, ...)
381 {
382 va_list ap;
383 char* str0 = str;
384 va_start(ap, fmt);
385
386 void sprintf_putch(int ch, void** data)
387 {
388 char** pstr = (char**)data;
389 **pstr = ch;
390 (*pstr)++;
391 }
392
393 vprintfmt(sprintf_putch, (void**)&str, fmt, ap);
394 *str = 0;
395
396 va_end(ap);
397 return str - str0;
398 }