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(tohost
, (long)magic_mem
);
26 while (swap_csr(fromhost
, 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
)
42 //use csrs to set stats register
44 asm volatile ("csrrs a0, stats, 1" ::: "a0");
46 #define READ_CTR(name) do { \
47 while (i >= NUM_COUNTERS) ; \
48 long csr = read_csr_safe(name); \
49 if (!enable) { csr -= counters[i]; counter_names[i] = #name; } \
50 counters[i++] = csr; \
52 READ_CTR(cycle
); READ_CTR(instret
);
53 READ_CTR(uarch0
); READ_CTR(uarch1
); READ_CTR(uarch2
); READ_CTR(uarch3
);
54 READ_CTR(uarch4
); READ_CTR(uarch5
); READ_CTR(uarch6
); READ_CTR(uarch7
);
55 READ_CTR(uarch8
); READ_CTR(uarch9
); READ_CTR(uarch10
); READ_CTR(uarch11
);
56 READ_CTR(uarch12
); READ_CTR(uarch13
); READ_CTR(uarch14
); READ_CTR(uarch15
);
59 asm volatile ("csrrc a0, stats, 1" ::: "a0");
63 static void tohost_exit(int code
)
65 write_csr(tohost
, (code
<< 1) | 1);
69 long handle_trap(long cause
, long epc
, long regs
[32])
72 asm ("jal %0, 1f; csrr a0, stats; 1:" : "=r"(csr_insn
));
75 if (cause
== CAUSE_ILLEGAL_INSTRUCTION
&&
76 (*(int*)epc
& *csr_insn
) == *csr_insn
)
78 else if (cause
!= CAUSE_ECALL
)
80 else if (regs
[17] == SYS_exit
)
81 tohost_exit(regs
[10]);
82 else if (regs
[17] == SYS_stats
)
83 sys_ret
= handle_stats(regs
[10]);
85 sys_ret
= handle_frontend_syscall(regs
[17], regs
[10], regs
[11], regs
[12]);
91 static long syscall(long num
, long arg0
, long arg1
, long arg2
)
93 register long a7
asm("a7") = num
;
94 register long a0
asm("a0") = arg0
;
95 register long a1
asm("a1") = arg1
;
96 register long a2
asm("a2") = arg2
;
97 asm volatile ("scall" : "+r"(a0
) : "r"(a1
), "r"(a2
), "r"(a7
));
103 syscall(SYS_exit
, code
, 0, 0);
107 void setStats(int enable
)
109 syscall(SYS_stats
, enable
, 0, 0);
112 void printstr(const char* s
)
114 syscall(SYS_write
, 1, (long)s
, strlen(s
));
117 void __attribute__((weak
)) thread_entry(int cid
, int nc
)
119 // multi-threaded programs override this function.
120 // for the case of single-threaded programs, only let core 0 proceed.
124 int __attribute__((weak
)) main(int argc
, char** argv
)
126 // single-threaded programs override this function.
127 printstr("Implement main(), foo!\n");
131 static void init_tls()
133 register void* thread_pointer
asm("tp");
134 extern char _tls_data
;
135 extern __thread
char _tdata_begin
, _tdata_end
, _tbss_end
;
136 size_t tdata_size
= &_tdata_end
- &_tdata_begin
;
137 memcpy(thread_pointer
, &_tls_data
, tdata_size
);
138 size_t tbss_size
= &_tbss_end
- &_tdata_end
;
139 memset(thread_pointer
+ tdata_size
, 0, tbss_size
);
142 void _init(int cid
, int nc
)
145 thread_entry(cid
, nc
);
147 // only single-threaded programs should ever get here.
148 int ret
= main(0, 0);
150 char buf
[NUM_COUNTERS
* 32] __attribute__((aligned(64)));
152 for (int i
= 0; i
< NUM_COUNTERS
; i
++)
154 pbuf
+= sprintf(pbuf
, "%s = %d\n", counter_names
[i
], counters
[i
]);
164 static __thread
char buf
[64] __attribute__((aligned(64)));
165 static __thread
int buflen
= 0;
169 if (ch
== '\n' || buflen
== sizeof(buf
))
171 syscall(SYS_write
, 1, (long)buf
, buflen
);
178 void printhex(uint64_t x
)
182 for (i
= 0; i
< 16; i
++)
184 str
[15-i
] = (x
& 0xF) + ((x
& 0xF) < 10 ? '0' : 'a'-10);
192 static inline void printnum(void (*putch
)(int, void**), void **putdat
,
193 unsigned long long num
, unsigned base
, int width
, int padc
)
195 unsigned digs
[sizeof(num
)*CHAR_BIT
];
200 digs
[pos
++] = num
% base
;
206 while (width
-- > pos
)
210 putch(digs
[pos
] + (digs
[pos
] >= 10 ? 'a' - 10 : '0'), putdat
);
213 static unsigned long long getuint(va_list *ap
, int lflag
)
216 return va_arg(*ap
, unsigned long long);
218 return va_arg(*ap
, unsigned long);
220 return va_arg(*ap
, unsigned int);
223 static long long getint(va_list *ap
, int lflag
)
226 return va_arg(*ap
, long long);
228 return va_arg(*ap
, long);
230 return va_arg(*ap
, int);
233 static void vprintfmt(void (*putch
)(int, void**), void **putdat
, const char *fmt
, va_list ap
)
235 register const char* p
;
236 const char* last_fmt
;
237 register int ch
, err
;
238 unsigned long long num
;
239 int base
, lflag
, width
, precision
, altflag
;
243 while ((ch
= *(unsigned char *) fmt
) != '%') {
251 // Process a %-escape sequence
259 switch (ch
= *(unsigned char *) fmt
++) {
261 // flag to pad on the right
266 // flag to pad with 0's instead of spaces
281 for (precision
= 0; ; ++fmt
) {
282 precision
= precision
* 10 + ch
- '0';
284 if (ch
< '0' || ch
> '9')
287 goto process_precision
;
290 precision
= va_arg(ap
, int);
291 goto process_precision
;
304 width
= precision
, precision
= -1;
307 // long flag (doubled for long long)
314 putch(va_arg(ap
, int), putdat
);
319 if ((p
= va_arg(ap
, char *)) == NULL
)
321 if (width
> 0 && padc
!= '-')
322 for (width
-= strnlen(p
, precision
); width
> 0; width
--)
324 for (; (ch
= *p
) != '\0' && (precision
< 0 || --precision
>= 0); width
--) {
328 for (; width
> 0; width
--)
334 num
= getint(&ap
, lflag
);
335 if ((long long) num
< 0) {
337 num
= -(long long) num
;
345 goto unsigned_number
;
349 // should do something with padding so it's always 3 octits
351 goto unsigned_number
;
355 static_assert(sizeof(long) == sizeof(void*));
359 /* fall through to 'x' */
361 // (unsigned) hexadecimal
365 num
= getuint(&ap
, lflag
);
367 printnum(putch
, putdat
, num
, base
, width
, padc
);
370 // escaped '%' character
375 // unrecognized escape sequence - just print it literally
384 int printf(const char* fmt
, ...)
389 vprintfmt((void*)putchar
, 0, fmt
, ap
);
392 return 0; // incorrect return value, but who cares, anyway?
395 int sprintf(char* str
, const char* fmt
, ...)
401 void sprintf_putch(int ch
, void** data
)
403 char** pstr
= (char**)data
;
408 vprintfmt(sprintf_putch
, (void**)&str
, fmt
, ap
);