ea264ba60ad36d5ea719dbcd82280c5ae73ea9f6
1 // See LICENSE for license details.
12 #define SYS_stats 1234
14 extern volatile uint64_t tohost
;
15 extern volatile uint64_t fromhost
;
17 static long handle_frontend_syscall(long which
, long arg0
, long arg1
, long arg2
)
19 volatile uint64_t magic_mem
[8] __attribute__((aligned(64)));
26 tohost
= (uintptr_t)magic_mem
;
35 #define NUM_COUNTERS 2
36 static long counters
[NUM_COUNTERS
];
37 static char* counter_names
[NUM_COUNTERS
];
39 static int handle_stats(int enable
)
42 #define READ_CTR(name) do { \
43 while (i >= NUM_COUNTERS) ; \
44 long csr = read_csr(name); \
45 if (!enable) { csr -= counters[i]; counter_names[i] = #name; } \
46 counters[i++] = csr; \
56 void __attribute__((noreturn
)) tohost_exit(long code
)
58 tohost
= (code
<< 1) | 1;
62 long handle_trap(long cause
, long epc
, long regs
[32])
64 if (cause
!= CAUSE_MACHINE_ECALL
)
66 else if (regs
[17] == SYS_exit
)
67 tohost_exit(regs
[10]);
68 else if (regs
[17] == SYS_stats
)
69 regs
[10] = handle_stats(regs
[10]);
71 regs
[10] = handle_frontend_syscall(regs
[17], regs
[10], regs
[11], regs
[12]);
73 return epc
+ ((*(unsigned short*)epc
& 3) == 3 ? 4 : 2);
76 static long syscall(long num
, long arg0
, long arg1
, long arg2
)
78 register long a7
asm("a7") = num
;
79 register long a0
asm("a0") = arg0
;
80 register long a1
asm("a1") = arg1
;
81 register long a2
asm("a2") = arg2
;
82 asm volatile ("scall" : "+r"(a0
) : "r"(a1
), "r"(a2
), "r"(a7
));
88 syscall(SYS_exit
, code
, 0, 0);
92 void setStats(int enable
)
94 syscall(SYS_stats
, enable
, 0, 0);
97 void printstr(const char* s
)
99 syscall(SYS_write
, 1, (long)s
, strlen(s
));
102 void __attribute__((weak
)) thread_entry(int cid
, int nc
)
104 // multi-threaded programs override this function.
105 // for the case of single-threaded programs, only let core 0 proceed.
109 int __attribute__((weak
)) main(int argc
, char** argv
)
111 // single-threaded programs override this function.
112 printstr("Implement main(), foo!\n");
116 static void init_tls()
118 register void* thread_pointer
asm("tp");
119 extern char _tls_data
;
120 extern __thread
char _tdata_begin
, _tdata_end
, _tbss_end
;
121 size_t tdata_size
= &_tdata_end
- &_tdata_begin
;
122 memcpy(thread_pointer
, &_tls_data
, tdata_size
);
123 size_t tbss_size
= &_tbss_end
- &_tdata_end
;
124 memset(thread_pointer
+ tdata_size
, 0, tbss_size
);
127 void _init(int cid
, int nc
)
130 thread_entry(cid
, nc
);
132 // only single-threaded programs should ever get here.
133 int ret
= main(0, 0);
135 char buf
[NUM_COUNTERS
* 32] __attribute__((aligned(64)));
137 for (int i
= 0; i
< NUM_COUNTERS
; i
++)
139 pbuf
+= sprintf(pbuf
, "%s = %d\n", counter_names
[i
], counters
[i
]);
149 static __thread
char buf
[64] __attribute__((aligned(64)));
150 static __thread
int buflen
= 0;
154 if (ch
== '\n' || buflen
== sizeof(buf
))
156 syscall(SYS_write
, 1, (long)buf
, buflen
);
163 void printhex(uint64_t x
)
167 for (i
= 0; i
< 16; i
++)
169 str
[15-i
] = (x
& 0xF) + ((x
& 0xF) < 10 ? '0' : 'a'-10);
177 static inline void printnum(void (*putch
)(int, void**), void **putdat
,
178 unsigned long long num
, unsigned base
, int width
, int padc
)
180 unsigned digs
[sizeof(num
)*CHAR_BIT
];
185 digs
[pos
++] = num
% base
;
191 while (width
-- > pos
)
195 putch(digs
[pos
] + (digs
[pos
] >= 10 ? 'a' - 10 : '0'), putdat
);
198 static unsigned long long getuint(va_list *ap
, int lflag
)
201 return va_arg(*ap
, unsigned long long);
203 return va_arg(*ap
, unsigned long);
205 return va_arg(*ap
, unsigned int);
208 static long long getint(va_list *ap
, int lflag
)
211 return va_arg(*ap
, long long);
213 return va_arg(*ap
, long);
215 return va_arg(*ap
, int);
218 static void vprintfmt(void (*putch
)(int, void**), void **putdat
, const char *fmt
, va_list ap
)
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
;
228 while ((ch
= *(unsigned char *) fmt
) != '%') {
236 // Process a %-escape sequence
244 switch (ch
= *(unsigned char *) fmt
++) {
246 // flag to pad on the right
251 // flag to pad with 0's instead of spaces
266 for (precision
= 0; ; ++fmt
) {
267 precision
= precision
* 10 + ch
- '0';
269 if (ch
< '0' || ch
> '9')
272 goto process_precision
;
275 precision
= va_arg(ap
, int);
276 goto process_precision
;
289 width
= precision
, precision
= -1;
292 // long flag (doubled for long long)
299 putch(va_arg(ap
, int), putdat
);
304 if ((p
= va_arg(ap
, char *)) == NULL
)
306 if (width
> 0 && padc
!= '-')
307 for (width
-= strnlen(p
, precision
); width
> 0; width
--)
309 for (; (ch
= *p
) != '\0' && (precision
< 0 || --precision
>= 0); width
--) {
313 for (; width
> 0; width
--)
319 num
= getint(&ap
, lflag
);
320 if ((long long) num
< 0) {
322 num
= -(long long) num
;
330 goto unsigned_number
;
334 // should do something with padding so it's always 3 octits
336 goto unsigned_number
;
340 static_assert(sizeof(long) == sizeof(void*));
344 /* fall through to 'x' */
346 // (unsigned) hexadecimal
350 num
= getuint(&ap
, lflag
);
352 printnum(putch
, putdat
, num
, base
, width
, padc
);
355 // escaped '%' character
360 // unrecognized escape sequence - just print it literally
369 int printf(const char* fmt
, ...)
374 vprintfmt((void*)putchar
, 0, fmt
, ap
);
377 return 0; // incorrect return value, but who cares, anyway?
380 int sprintf(char* str
, const char* fmt
, ...)
386 void sprintf_putch(int ch
, void** data
)
388 char** pstr
= (char**)data
;
393 vprintfmt(sprintf_putch
, (void**)&str
, fmt
, ap
);
400 void* memcpy(void* dest
, const void* src
, size_t len
)
402 if ((((uintptr_t)dest
| (uintptr_t)src
| len
) & (sizeof(uintptr_t)-1)) == 0) {
403 const uintptr_t* s
= src
;
405 while (d
< (uintptr_t*)(dest
+ len
))
410 while (d
< (char*)(dest
+ len
))
416 void* memset(void* dest
, int byte
, size_t len
)
418 if ((((uintptr_t)dest
| len
) & (sizeof(uintptr_t)-1)) == 0) {
419 uintptr_t word
= byte
& 0xFF;
422 word
|= word
<< 16 << 16;
425 while (d
< (uintptr_t*)(dest
+ len
))
429 while (d
< (char*)(dest
+ len
))
435 size_t strlen(const char *s
)
443 size_t strnlen(const char *s
, size_t n
)
451 int strcmp(const char* s1
, const char* s2
)
453 unsigned char c1
, c2
;
458 } while (c1
!= 0 && c1
== c2
);
463 char* strcpy(char* dest
, const char* src
)
466 while ((*d
++ = *src
++))
471 long atol(const char* str
)
479 if (*str
== '-' || *str
== '+') {
489 return sign
? -res
: res
;