3f15aac8d446b93cdc092c6ec85293dd2cbc6986
1 // See LICENSE for license details.
12 #define SYS_stats 1234
16 extern volatile uint64_t tohost
;
17 extern volatile uint64_t fromhost
;
19 static uintptr_t handle_frontend_syscall(uintptr_t which
, uint64_t arg0
, uint64_t arg1
, uint64_t arg2
)
21 volatile uint64_t magic_mem
[8] __attribute__((aligned(64)));
28 tohost
= (uintptr_t)magic_mem
;
37 #define NUM_COUNTERS 2
38 static uintptr_t counters
[NUM_COUNTERS
];
39 static char* counter_names
[NUM_COUNTERS
];
41 static int handle_stats(int enable
)
44 #define READ_CTR(name) do { \
45 while (i >= NUM_COUNTERS) ; \
46 uintptr_t csr = read_csr(name); \
47 if (!enable) { csr -= counters[i]; counter_names[i] = #name; } \
48 counters[i++] = csr; \
58 void __attribute__((noreturn
)) tohost_exit(uintptr_t code
)
60 tohost
= (code
<< 1) | 1;
64 uintptr_t handle_trap(uintptr_t cause
, uintptr_t epc
, uintptr_t regs
[32])
66 if (cause
!= CAUSE_MACHINE_ECALL
)
68 else if (regs
[17] == SYS_exit
)
69 tohost_exit(regs
[10]);
70 else if (regs
[17] == SYS_stats
)
71 regs
[10] = handle_stats(regs
[10]);
73 regs
[10] = handle_frontend_syscall(regs
[17], regs
[10], regs
[11], regs
[12]);
75 return epc
+ ((*(unsigned short*)epc
& 3) == 3 ? 4 : 2);
78 static uintptr_t syscall(uintptr_t num
, uintptr_t arg0
, uintptr_t arg1
, uintptr_t arg2
)
80 register uintptr_t a7
asm("a7") = num
;
81 register uintptr_t a0
asm("a0") = arg0
;
82 register uintptr_t a1
asm("a1") = arg1
;
83 register uintptr_t a2
asm("a2") = arg2
;
84 asm volatile ("scall" : "+r"(a0
) : "r"(a1
), "r"(a2
), "r"(a7
));
90 syscall(SYS_exit
, code
, 0, 0);
94 void setStats(int enable
)
96 syscall(SYS_stats
, enable
, 0, 0);
99 void printstr(const char* s
)
101 syscall(SYS_write
, 1, (uintptr_t)s
, strlen(s
));
104 void __attribute__((weak
)) thread_entry(int cid
, int nc
)
106 // multi-threaded programs override this function.
107 // for the case of single-threaded programs, only let core 0 proceed.
111 int __attribute__((weak
)) main(int argc
, char** argv
)
113 // single-threaded programs override this function.
114 printstr("Implement main(), foo!\n");
118 static void init_tls()
120 register void* thread_pointer
asm("tp");
121 extern char _tls_data
;
122 extern __thread
char _tdata_begin
, _tdata_end
, _tbss_end
;
123 size_t tdata_size
= &_tdata_end
- &_tdata_begin
;
124 memcpy(thread_pointer
, &_tls_data
, tdata_size
);
125 size_t tbss_size
= &_tbss_end
- &_tdata_end
;
126 memset(thread_pointer
+ tdata_size
, 0, tbss_size
);
129 void _init(int cid
, int nc
)
132 thread_entry(cid
, nc
);
134 // only single-threaded programs should ever get here.
135 int ret
= main(0, 0);
137 char buf
[NUM_COUNTERS
* 32] __attribute__((aligned(64)));
139 for (int i
= 0; i
< NUM_COUNTERS
; i
++)
141 pbuf
+= sprintf(pbuf
, "%s = %d\n", counter_names
[i
], counters
[i
]);
151 static __thread
char buf
[64] __attribute__((aligned(64)));
152 static __thread
int buflen
= 0;
156 if (ch
== '\n' || buflen
== sizeof(buf
))
158 syscall(SYS_write
, 1, (uintptr_t)buf
, buflen
);
165 void printhex(uint64_t x
)
169 for (i
= 0; i
< 16; i
++)
171 str
[15-i
] = (x
& 0xF) + ((x
& 0xF) < 10 ? '0' : 'a'-10);
179 static inline void printnum(void (*putch
)(int, void**), void **putdat
,
180 unsigned long long num
, unsigned base
, int width
, int padc
)
182 unsigned digs
[sizeof(num
)*CHAR_BIT
];
187 digs
[pos
++] = num
% base
;
193 while (width
-- > pos
)
197 putch(digs
[pos
] + (digs
[pos
] >= 10 ? 'a' - 10 : '0'), putdat
);
200 static unsigned long long getuint(va_list *ap
, int lflag
)
203 return va_arg(*ap
, unsigned long long);
205 return va_arg(*ap
, unsigned long);
207 return va_arg(*ap
, unsigned int);
210 static long long getint(va_list *ap
, int lflag
)
213 return va_arg(*ap
, long long);
215 return va_arg(*ap
, long);
217 return va_arg(*ap
, int);
220 static void vprintfmt(void (*putch
)(int, void**), void **putdat
, const char *fmt
, va_list ap
)
222 register const char* p
;
223 const char* last_fmt
;
224 register int ch
, err
;
225 unsigned long long num
;
226 int base
, lflag
, width
, precision
, altflag
;
230 while ((ch
= *(unsigned char *) fmt
) != '%') {
238 // Process a %-escape sequence
246 switch (ch
= *(unsigned char *) fmt
++) {
248 // flag to pad on the right
253 // flag to pad with 0's instead of spaces
268 for (precision
= 0; ; ++fmt
) {
269 precision
= precision
* 10 + ch
- '0';
271 if (ch
< '0' || ch
> '9')
274 goto process_precision
;
277 precision
= va_arg(ap
, int);
278 goto process_precision
;
291 width
= precision
, precision
= -1;
294 // long flag (doubled for long long)
301 putch(va_arg(ap
, int), putdat
);
306 if ((p
= va_arg(ap
, char *)) == NULL
)
308 if (width
> 0 && padc
!= '-')
309 for (width
-= strnlen(p
, precision
); width
> 0; width
--)
311 for (; (ch
= *p
) != '\0' && (precision
< 0 || --precision
>= 0); width
--) {
315 for (; width
> 0; width
--)
321 num
= getint(&ap
, lflag
);
322 if ((long long) num
< 0) {
324 num
= -(long long) num
;
332 goto unsigned_number
;
336 // should do something with padding so it's always 3 octits
338 goto unsigned_number
;
342 static_assert(sizeof(long) == sizeof(void*));
346 /* fall through to 'x' */
348 // (unsigned) hexadecimal
352 num
= getuint(&ap
, lflag
);
354 printnum(putch
, putdat
, num
, base
, width
, padc
);
357 // escaped '%' character
362 // unrecognized escape sequence - just print it literally
371 int printf(const char* fmt
, ...)
376 vprintfmt((void*)putchar
, 0, fmt
, ap
);
379 return 0; // incorrect return value, but who cares, anyway?
382 int sprintf(char* str
, const char* fmt
, ...)
388 void sprintf_putch(int ch
, void** data
)
390 char** pstr
= (char**)data
;
395 vprintfmt(sprintf_putch
, (void**)&str
, fmt
, ap
);
402 void* memcpy(void* dest
, const void* src
, size_t len
)
404 if ((((uintptr_t)dest
| (uintptr_t)src
| len
) & (sizeof(uintptr_t)-1)) == 0) {
405 const uintptr_t* s
= src
;
407 while (d
< (uintptr_t*)(dest
+ len
))
412 while (d
< (char*)(dest
+ len
))
418 void* memset(void* dest
, int byte
, size_t len
)
420 if ((((uintptr_t)dest
| len
) & (sizeof(uintptr_t)-1)) == 0) {
421 uintptr_t word
= byte
& 0xFF;
424 word
|= word
<< 16 << 16;
427 while (d
< (uintptr_t*)(dest
+ len
))
431 while (d
< (char*)(dest
+ len
))
437 size_t strlen(const char *s
)
445 size_t strnlen(const char *s
, size_t n
)
453 int strcmp(const char* s1
, const char* s2
)
455 unsigned char c1
, c2
;
460 } while (c1
!= 0 && c1
== c2
);
465 char* strcpy(char* dest
, const char* src
)
468 while ((*d
++ = *src
++))
473 long atol(const char* str
)
481 if (*str
== '-' || *str
== '+') {
491 return sign
? -res
: res
;