6 #include <machine/syscall.h>
11 static long handle_frontend_syscall(long which
, long arg0
, long arg1
, long arg2
)
13 volatile uint64_t magic_mem
[8] __attribute__((aligned(64)));
19 write_csr(tohost
, (long)magic_mem
);
20 while (swap_csr(fromhost
, 0) == 0);
24 // In setStats, we might trap reading uarch-specific counters.
25 // The trap handler will skip over the instruction and write 0,
26 // but only if v0 is the destination register.
27 #define read_csr_safe(reg) ({ register long __tmp asm("v0"); \
28 asm volatile ("csrr %0, " #reg : "=r"(__tmp)); \
31 #define NUM_COUNTERS 18
32 static long counters
[NUM_COUNTERS
];
33 static char* counter_names
[NUM_COUNTERS
];
34 static int handle_stats(int enable
)
36 //use csrs to set stats register
44 #define READ_CTR(name) do { \
45 while (i >= NUM_COUNTERS) ; \
46 long csr = read_csr_safe(name); \
47 if (!enable) { csr -= counters[i]; counter_names[i] = #name; } \
48 counters[i++] = csr; \
50 READ_CTR(cycle
); READ_CTR(instret
);
51 READ_CTR(uarch0
); READ_CTR(uarch1
); READ_CTR(uarch2
); READ_CTR(uarch3
);
52 READ_CTR(uarch4
); READ_CTR(uarch5
); READ_CTR(uarch6
); READ_CTR(uarch7
);
53 READ_CTR(uarch8
); READ_CTR(uarch9
); READ_CTR(uarch10
); READ_CTR(uarch11
);
54 READ_CTR(uarch12
); READ_CTR(uarch13
); READ_CTR(uarch14
); READ_CTR(uarch15
);
65 static void tohost_exit(int code
)
67 write_csr(tohost
, (code
<< 1) | 1);
71 long handle_trap(long cause
, long epc
, long regs
[32])
74 asm volatile ("lw %0, 1f; j 2f; 1: csrr v0, stats; 2:" : "=r"(csr_insn
));
77 if (cause
== CAUSE_ILLEGAL_INSTRUCTION
&&
78 (*(int*)epc
& csr_insn
) == csr_insn
)
80 else if (cause
!= CAUSE_SYSCALL
)
82 else if (regs
[16] == SYS_exit
)
83 tohost_exit(regs
[18]);
84 else if (regs
[16] == SYS_stats
)
85 sys_ret
= handle_stats(regs
[18]);
87 sys_ret
= handle_frontend_syscall(regs
[16], regs
[18], regs
[19], regs
[20]);
93 static long syscall(long num
, long arg0
, long arg1
, long arg2
)
95 register long v0
asm("v0") = num
;
96 register long a0
asm("a0") = arg0
;
97 register long a1
asm("a1") = arg1
;
98 register long a2
asm("a2") = arg2
;
99 asm volatile ("scall" : "+r"(v0
) : "r"(a0
), "r"(a1
), "r"(a2
) : "s0");
105 syscall(SYS_exit
, code
, 0, 0);
108 void setStats(int enable
)
110 syscall(SYS_stats
, enable
, 0, 0);
113 void printstr(const char* s
)
115 syscall(SYS_write
, 1, (long)s
, strlen(s
));
118 void __attribute__((weak
)) thread_entry(int cid
, int nc
)
120 // multi-threaded programs override this function.
121 // for the case of single-threaded programs, only let core 0 proceed.
125 int __attribute__((weak
)) main(int argc
, char** argv
)
127 // single-threaded programs override this function.
128 printstr("Implement main(), foo!\n");
132 void _init(int cid
, int nc
)
134 thread_entry(cid
, nc
);
136 // only single-threaded programs should ever get here.
137 int ret
= main(0, 0);
139 char buf
[NUM_COUNTERS
* 32] __attribute__((aligned(64)));
141 for (int i
= 0; i
< NUM_COUNTERS
; i
++)
143 pbuf
+= sprintf(pbuf
, "%s = %d\n", counter_names
[i
], counters
[i
]);
153 static char buf
[64] __attribute__((aligned(64)));
154 static int buflen
= 0;
158 if (ch
== '\n' || buflen
== sizeof(buf
))
160 syscall(SYS_write
, 1, (long)buf
, buflen
);
167 void printhex(uint64_t x
)
171 for (i
= 0; i
< 16; i
++)
173 str
[15-i
] = (x
& 0xF) + ((x
& 0xF) < 10 ? '0' : 'a'-10);
181 static inline void printnum(void (*putch
)(int, void**), void **putdat
,
182 unsigned long long num
, unsigned base
, int width
, int padc
)
184 unsigned digs
[sizeof(num
)*CHAR_BIT
];
189 digs
[pos
++] = num
% base
;
195 while (width
-- > pos
)
199 putch(digs
[pos
] + (digs
[pos
] >= 10 ? 'a' - 10 : '0'), putdat
);
202 static unsigned long long getuint(va_list *ap
, int lflag
)
205 return va_arg(*ap
, unsigned long long);
207 return va_arg(*ap
, unsigned long);
209 return va_arg(*ap
, unsigned int);
212 static long long getint(va_list *ap
, int lflag
)
215 return va_arg(*ap
, long long);
217 return va_arg(*ap
, long);
219 return va_arg(*ap
, int);
222 static void vprintfmt(void (*putch
)(int, void**), void **putdat
, const char *fmt
, va_list ap
)
224 register const char* p
;
225 const char* last_fmt
;
226 register int ch
, err
;
227 unsigned long long num
;
228 int base
, lflag
, width
, precision
, altflag
;
232 while ((ch
= *(unsigned char *) fmt
) != '%') {
240 // Process a %-escape sequence
248 switch (ch
= *(unsigned char *) fmt
++) {
250 // flag to pad on the right
255 // flag to pad with 0's instead of spaces
270 for (precision
= 0; ; ++fmt
) {
271 precision
= precision
* 10 + ch
- '0';
273 if (ch
< '0' || ch
> '9')
276 goto process_precision
;
279 precision
= va_arg(ap
, int);
280 goto process_precision
;
293 width
= precision
, precision
= -1;
296 // long flag (doubled for long long)
303 putch(va_arg(ap
, int), putdat
);
308 if ((p
= va_arg(ap
, char *)) == NULL
)
310 if (width
> 0 && padc
!= '-')
311 for (width
-= strnlen(p
, precision
); width
> 0; width
--)
313 for (; (ch
= *p
) != '\0' && (precision
< 0 || --precision
>= 0); width
--) {
317 for (; width
> 0; width
--)
323 num
= getint(&ap
, lflag
);
324 if ((long long) num
< 0) {
326 num
= -(long long) num
;
334 goto unsigned_number
;
338 // should do something with padding so it's always 3 octits
340 goto unsigned_number
;
344 static_assert(sizeof(long) == sizeof(void*));
348 /* fall through to 'x' */
350 // (unsigned) hexadecimal
354 num
= getuint(&ap
, lflag
);
356 printnum(putch
, putdat
, num
, base
, width
, padc
);
359 // escaped '%' character
364 // unrecognized escape sequence - just print it literally
373 int printf(const char* fmt
, ...)
378 vprintfmt((void*)putchar
, 0, fmt
, ap
);
381 return 0; // incorrect return value, but who cares, anyway?
384 int sprintf(char* str
, const char* fmt
, ...)
390 void sprintf_putch(int ch
, void** data
)
392 char** pstr
= (char**)data
;
397 vprintfmt(sprintf_putch
, (void**)&str
, fmt
, ap
);