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);
112 void setStats(int enable
)
114 syscall(SYS_stats
, enable
, 0, 0);
117 void printstr(const char* s
)
119 syscall(SYS_write
, 1, (long)s
, strlen(s
));
122 void __attribute__((weak
)) thread_entry(int cid
, int nc
)
124 // multi-threaded programs override this function.
125 // for the case of single-threaded programs, only let core 0 proceed.
129 int __attribute__((weak
)) main(int argc
, char** argv
)
131 // single-threaded programs override this function.
132 printstr("Implement main(), foo!\n");
136 void _init(int cid
, int nc
)
138 thread_entry(cid
, nc
);
140 // only single-threaded programs should ever get here.
141 int ret
= main(0, 0);
143 char buf
[NUM_COUNTERS
* 32] __attribute__((aligned(64)));
145 for (int i
= 0; i
< NUM_COUNTERS
; i
++)
147 pbuf
+= sprintf(pbuf
, "%s = %d\n", counter_names
[i
], counters
[i
]);
157 static __thread
char buf
[64] __attribute__((aligned(64)));
158 static __thread
int buflen
= 0;
162 if (ch
== '\n' || buflen
== sizeof(buf
))
164 syscall(SYS_write
, 1, (long)buf
, buflen
);
171 void printhex(uint64_t x
)
175 for (i
= 0; i
< 16; i
++)
177 str
[15-i
] = (x
& 0xF) + ((x
& 0xF) < 10 ? '0' : 'a'-10);
185 static inline void printnum(void (*putch
)(int, void**), void **putdat
,
186 unsigned long long num
, unsigned base
, int width
, int padc
)
188 unsigned digs
[sizeof(num
)*CHAR_BIT
];
193 digs
[pos
++] = num
% base
;
199 while (width
-- > pos
)
203 putch(digs
[pos
] + (digs
[pos
] >= 10 ? 'a' - 10 : '0'), putdat
);
206 static unsigned long long getuint(va_list *ap
, int lflag
)
209 return va_arg(*ap
, unsigned long long);
211 return va_arg(*ap
, unsigned long);
213 return va_arg(*ap
, unsigned int);
216 static long long getint(va_list *ap
, int lflag
)
219 return va_arg(*ap
, long long);
221 return va_arg(*ap
, long);
223 return va_arg(*ap
, int);
226 static void vprintfmt(void (*putch
)(int, void**), void **putdat
, const char *fmt
, va_list ap
)
228 register const char* p
;
229 const char* last_fmt
;
230 register int ch
, err
;
231 unsigned long long num
;
232 int base
, lflag
, width
, precision
, altflag
;
236 while ((ch
= *(unsigned char *) fmt
) != '%') {
244 // Process a %-escape sequence
252 switch (ch
= *(unsigned char *) fmt
++) {
254 // flag to pad on the right
259 // flag to pad with 0's instead of spaces
274 for (precision
= 0; ; ++fmt
) {
275 precision
= precision
* 10 + ch
- '0';
277 if (ch
< '0' || ch
> '9')
280 goto process_precision
;
283 precision
= va_arg(ap
, int);
284 goto process_precision
;
297 width
= precision
, precision
= -1;
300 // long flag (doubled for long long)
307 putch(va_arg(ap
, int), putdat
);
312 if ((p
= va_arg(ap
, char *)) == NULL
)
314 if (width
> 0 && padc
!= '-')
315 for (width
-= strnlen(p
, precision
); width
> 0; width
--)
317 for (; (ch
= *p
) != '\0' && (precision
< 0 || --precision
>= 0); width
--) {
321 for (; width
> 0; width
--)
327 num
= getint(&ap
, lflag
);
328 if ((long long) num
< 0) {
330 num
= -(long long) num
;
338 goto unsigned_number
;
342 // should do something with padding so it's always 3 octits
344 goto unsigned_number
;
348 static_assert(sizeof(long) == sizeof(void*));
352 /* fall through to 'x' */
354 // (unsigned) hexadecimal
358 num
= getuint(&ap
, lflag
);
360 printnum(putch
, putdat
, num
, base
, width
, padc
);
363 // escaped '%' character
368 // unrecognized escape sequence - just print it literally
377 int printf(const char* fmt
, ...)
382 vprintfmt((void*)putchar
, 0, fmt
, ap
);
385 return 0; // incorrect return value, but who cares, anyway?
388 int sprintf(char* str
, const char* fmt
, ...)
394 void sprintf_putch(int ch
, void** data
)
396 char** pstr
= (char**)data
;
401 vprintfmt(sprintf_putch
, (void**)&str
, fmt
, ap
);