6 #include <machine/syscall.h>
11 // initialized in crt.S
14 static long handle_frontend_syscall(long which
, long arg0
, long arg1
, long arg2
)
16 volatile uint64_t magic_mem
[8] __attribute__((aligned(64)));
22 write_csr(tohost
, (long)magic_mem
);
23 while (swap_csr(fromhost
, 0) == 0);
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 v0 is the destination register.
30 #define read_csr_safe(reg) ({ register long __tmp asm("v0"); \
31 asm volatile ("csrr %0, " #reg : "=r"(__tmp)); \
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
)
39 //use csrs to set stats register
47 #define READ_CTR(name) do { \
48 while (i >= NUM_COUNTERS) ; \
49 long csr = read_csr_safe(name); \
50 if (!enable) { csr -= counters[i]; counter_names[i] = #name; } \
51 counters[i++] = csr; \
53 READ_CTR(cycle
); READ_CTR(instret
);
54 READ_CTR(uarch0
); READ_CTR(uarch1
); READ_CTR(uarch2
); READ_CTR(uarch3
);
55 READ_CTR(uarch4
); READ_CTR(uarch5
); READ_CTR(uarch6
); READ_CTR(uarch7
);
56 READ_CTR(uarch8
); READ_CTR(uarch9
); READ_CTR(uarch10
); READ_CTR(uarch11
);
57 READ_CTR(uarch12
); READ_CTR(uarch13
); READ_CTR(uarch14
); READ_CTR(uarch15
);
68 static void tohost_exit(int code
)
70 write_csr(tohost
, (code
<< 1) | 1);
74 long handle_trap(long cause
, long epc
, long regs
[32])
77 asm volatile ("lw %0, 1f; j 2f; 1: csrr v0, stats; 2:" : "=r"(csr_insn
));
80 if (cause
== CAUSE_ILLEGAL_INSTRUCTION
&&
81 (*(int*)epc
& csr_insn
) == csr_insn
)
83 else if (cause
!= CAUSE_SYSCALL
)
85 else if (regs
[16] == SYS_exit
)
86 tohost_exit(regs
[18]);
87 else if (regs
[16] == SYS_stats
)
88 sys_ret
= handle_stats(regs
[18]);
90 sys_ret
= handle_frontend_syscall(regs
[16], regs
[18], regs
[19], regs
[20]);
96 static long syscall(long num
, long arg0
, long arg1
, long arg2
)
98 register long v0
asm("v0") = num
;
99 register long a0
asm("a0") = arg0
;
100 register long a1
asm("a1") = arg1
;
101 register long a2
asm("a2") = arg2
;
102 asm volatile ("scall" : "+r"(v0
) : "r"(a0
), "r"(a1
), "r"(a2
) : "s0");
108 syscall(SYS_exit
, code
, 0, 0);
111 void setStats(int enable
)
113 syscall(SYS_stats
, enable
, 0, 0);
116 void printstr(const char* s
)
118 syscall(SYS_write
, 1, (long)s
, strlen(s
));
121 void __attribute__((weak
)) thread_entry(int cid
, int nc
)
123 // multi-threaded programs override this function.
124 // for the case of single-threaded programs, only let core 0 proceed.
128 int __attribute__((weak
)) main(int argc
, char** argv
)
130 // single-threaded programs override this function.
131 printstr("Implement main(), foo!\n");
135 void _init(int cid
, int nc
)
137 thread_entry(cid
, nc
);
139 // only single-threaded programs should ever get here.
140 int ret
= main(0, 0);
142 char buf
[NUM_COUNTERS
* 32] __attribute__((aligned(64)));
144 for (int i
= 0; i
< NUM_COUNTERS
; i
++)
146 pbuf
+= sprintf(pbuf
, "%s = %d\n", counter_names
[i
], counters
[i
]);
156 static __thread
char buf
[64] __attribute__((aligned(64)));
157 static __thread
int buflen
= 0;
161 if (ch
== '\n' || buflen
== sizeof(buf
))
163 syscall(SYS_write
, 1, (long)buf
, buflen
);
170 void printhex(uint64_t x
)
174 for (i
= 0; i
< 16; i
++)
176 str
[15-i
] = (x
& 0xF) + ((x
& 0xF) < 10 ? '0' : 'a'-10);
184 static inline void printnum(void (*putch
)(int, void**), void **putdat
,
185 unsigned long long num
, unsigned base
, int width
, int padc
)
187 unsigned digs
[sizeof(num
)*CHAR_BIT
];
192 digs
[pos
++] = num
% base
;
198 while (width
-- > pos
)
202 putch(digs
[pos
] + (digs
[pos
] >= 10 ? 'a' - 10 : '0'), putdat
);
205 static unsigned long long getuint(va_list *ap
, int lflag
)
208 return va_arg(*ap
, unsigned long long);
210 return va_arg(*ap
, unsigned long);
212 return va_arg(*ap
, unsigned int);
215 static long long getint(va_list *ap
, int lflag
)
218 return va_arg(*ap
, long long);
220 return va_arg(*ap
, long);
222 return va_arg(*ap
, int);
225 static void vprintfmt(void (*putch
)(int, void**), void **putdat
, const char *fmt
, va_list ap
)
227 register const char* p
;
228 const char* last_fmt
;
229 register int ch
, err
;
230 unsigned long long num
;
231 int base
, lflag
, width
, precision
, altflag
;
235 while ((ch
= *(unsigned char *) fmt
) != '%') {
243 // Process a %-escape sequence
251 switch (ch
= *(unsigned char *) fmt
++) {
253 // flag to pad on the right
258 // flag to pad with 0's instead of spaces
273 for (precision
= 0; ; ++fmt
) {
274 precision
= precision
* 10 + ch
- '0';
276 if (ch
< '0' || ch
> '9')
279 goto process_precision
;
282 precision
= va_arg(ap
, int);
283 goto process_precision
;
296 width
= precision
, precision
= -1;
299 // long flag (doubled for long long)
306 putch(va_arg(ap
, int), putdat
);
311 if ((p
= va_arg(ap
, char *)) == NULL
)
313 if (width
> 0 && padc
!= '-')
314 for (width
-= strnlen(p
, precision
); width
> 0; width
--)
316 for (; (ch
= *p
) != '\0' && (precision
< 0 || --precision
>= 0); width
--) {
320 for (; width
> 0; width
--)
326 num
= getint(&ap
, lflag
);
327 if ((long long) num
< 0) {
329 num
= -(long long) num
;
337 goto unsigned_number
;
341 // should do something with padding so it's always 3 octits
343 goto unsigned_number
;
347 static_assert(sizeof(long) == sizeof(void*));
351 /* fall through to 'x' */
353 // (unsigned) hexadecimal
357 num
= getuint(&ap
, lflag
);
359 printnum(putch
, putdat
, num
, base
, width
, padc
);
362 // escaped '%' character
367 // unrecognized escape sequence - just print it literally
376 int printf(const char* fmt
, ...)
381 vprintfmt((void*)putchar
, 0, fmt
, ap
);
384 return 0; // incorrect return value, but who cares, anyway?
387 int sprintf(char* str
, const char* fmt
, ...)
393 void sprintf_putch(int ch
, void** data
)
395 char** pstr
= (char**)data
;
400 vprintfmt(sprintf_putch
, (void**)&str
, fmt
, ap
);