1 // See LICENSE for license details.
12 #define SYS_stats 1234
14 // initialized in crt.S
17 static long handle_frontend_syscall(long which
, long arg0
, long arg1
, long arg2
)
19 volatile uint64_t magic_mem
[8] __attribute__((aligned(64)));
25 write_csr(mtohost
, (long)magic_mem
);
26 while (swap_csr(mfromhost
, 0) == 0);
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)); \
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
)
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; \
49 READ_CTR(mcycle
); READ_CTR(minstret
);
50 READ_CTR(0xcc0); READ_CTR(0xcc1); READ_CTR(0xcc2); READ_CTR(0xcc3);
51 READ_CTR(0xcc4); READ_CTR(0xcc5); READ_CTR(0xcc6); READ_CTR(0xcc7);
52 READ_CTR(0xcc8); READ_CTR(0xcc9); READ_CTR(0xcca); READ_CTR(0xccb);
53 READ_CTR(0xccc); READ_CTR(0xccd); READ_CTR(0xcce); READ_CTR(0xccf);
58 void tohost_exit(long code
)
60 write_csr(mtohost
, (code
<< 1) | 1);
64 long handle_trap(long cause
, long epc
, long regs
[32])
67 asm ("jal %0, 1f; csrr a0, 0xcc0; 1:" : "=r"(csr_insn
));
70 if (cause
== CAUSE_ILLEGAL_INSTRUCTION
&&
71 (*(int*)epc
& *csr_insn
) == *csr_insn
)
73 else if (cause
!= CAUSE_MACHINE_ECALL
)
75 else if (regs
[17] == SYS_exit
)
76 tohost_exit(regs
[10]);
77 else if (regs
[17] == SYS_stats
)
78 sys_ret
= handle_stats(regs
[10]);
80 sys_ret
= handle_frontend_syscall(regs
[17], regs
[10], regs
[11], regs
[12]);
86 static long syscall(long num
, long arg0
, long arg1
, long arg2
)
88 register long a7
asm("a7") = num
;
89 register long a0
asm("a0") = arg0
;
90 register long a1
asm("a1") = arg1
;
91 register long a2
asm("a2") = arg2
;
92 asm volatile ("scall" : "+r"(a0
) : "r"(a1
), "r"(a2
), "r"(a7
));
98 syscall(SYS_exit
, code
, 0, 0);
102 void setStats(int enable
)
104 syscall(SYS_stats
, enable
, 0, 0);
107 void printstr(const char* s
)
109 syscall(SYS_write
, 1, (long)s
, strlen(s
));
112 void __attribute__((weak
)) thread_entry(int cid
, int nc
)
114 // multi-threaded programs override this function.
115 // for the case of single-threaded programs, only let core 0 proceed.
119 int __attribute__((weak
)) main(int argc
, char** argv
)
121 // single-threaded programs override this function.
122 printstr("Implement main(), foo!\n");
126 static void init_tls()
128 register void* thread_pointer
asm("tp");
129 extern char _tls_data
;
130 extern __thread
char _tdata_begin
, _tdata_end
, _tbss_end
;
131 size_t tdata_size
= &_tdata_end
- &_tdata_begin
;
132 memcpy(thread_pointer
, &_tls_data
, tdata_size
);
133 size_t tbss_size
= &_tbss_end
- &_tdata_end
;
134 memset(thread_pointer
+ tdata_size
, 0, tbss_size
);
137 void _init(int cid
, int nc
)
140 thread_entry(cid
, nc
);
142 // only single-threaded programs should ever get here.
143 int ret
= main(0, 0);
145 char buf
[NUM_COUNTERS
* 32] __attribute__((aligned(64)));
147 for (int i
= 0; i
< NUM_COUNTERS
; i
++)
149 pbuf
+= sprintf(pbuf
, "%s = %d\n", counter_names
[i
], counters
[i
]);
159 static __thread
char buf
[64] __attribute__((aligned(64)));
160 static __thread
int buflen
= 0;
164 if (ch
== '\n' || buflen
== sizeof(buf
))
166 syscall(SYS_write
, 1, (long)buf
, buflen
);
173 void printhex(uint64_t x
)
177 for (i
= 0; i
< 16; i
++)
179 str
[15-i
] = (x
& 0xF) + ((x
& 0xF) < 10 ? '0' : 'a'-10);
187 static inline void printnum(void (*putch
)(int, void**), void **putdat
,
188 unsigned long long num
, unsigned base
, int width
, int padc
)
190 unsigned digs
[sizeof(num
)*CHAR_BIT
];
195 digs
[pos
++] = num
% base
;
201 while (width
-- > pos
)
205 putch(digs
[pos
] + (digs
[pos
] >= 10 ? 'a' - 10 : '0'), putdat
);
208 static unsigned long long getuint(va_list *ap
, int lflag
)
211 return va_arg(*ap
, unsigned long long);
213 return va_arg(*ap
, unsigned long);
215 return va_arg(*ap
, unsigned int);
218 static long long getint(va_list *ap
, int lflag
)
221 return va_arg(*ap
, long long);
223 return va_arg(*ap
, long);
225 return va_arg(*ap
, int);
228 static void vprintfmt(void (*putch
)(int, void**), void **putdat
, const char *fmt
, va_list ap
)
230 register const char* p
;
231 const char* last_fmt
;
232 register int ch
, err
;
233 unsigned long long num
;
234 int base
, lflag
, width
, precision
, altflag
;
238 while ((ch
= *(unsigned char *) fmt
) != '%') {
246 // Process a %-escape sequence
254 switch (ch
= *(unsigned char *) fmt
++) {
256 // flag to pad on the right
261 // flag to pad with 0's instead of spaces
276 for (precision
= 0; ; ++fmt
) {
277 precision
= precision
* 10 + ch
- '0';
279 if (ch
< '0' || ch
> '9')
282 goto process_precision
;
285 precision
= va_arg(ap
, int);
286 goto process_precision
;
299 width
= precision
, precision
= -1;
302 // long flag (doubled for long long)
309 putch(va_arg(ap
, int), putdat
);
314 if ((p
= va_arg(ap
, char *)) == NULL
)
316 if (width
> 0 && padc
!= '-')
317 for (width
-= strnlen(p
, precision
); width
> 0; width
--)
319 for (; (ch
= *p
) != '\0' && (precision
< 0 || --precision
>= 0); width
--) {
323 for (; width
> 0; width
--)
329 num
= getint(&ap
, lflag
);
330 if ((long long) num
< 0) {
332 num
= -(long long) num
;
340 goto unsigned_number
;
344 // should do something with padding so it's always 3 octits
346 goto unsigned_number
;
350 static_assert(sizeof(long) == sizeof(void*));
354 /* fall through to 'x' */
356 // (unsigned) hexadecimal
360 num
= getuint(&ap
, lflag
);
362 printnum(putch
, putdat
, num
, base
, width
, padc
);
365 // escaped '%' character
370 // unrecognized escape sequence - just print it literally
379 int printf(const char* fmt
, ...)
384 vprintfmt((void*)putchar
, 0, fmt
, ap
);
387 return 0; // incorrect return value, but who cares, anyway?
390 int sprintf(char* str
, const char* fmt
, ...)
396 void sprintf_putch(int ch
, void** data
)
398 char** pstr
= (char**)data
;
403 vprintfmt(sprintf_putch
, (void**)&str
, fmt
, ap
);