6 #include <machine/syscall.h>
10 #define static_assert(cond) switch(0) { case 0: case !!(long)(cond): ; }
12 static long handle_frontend_syscall(long which
, long arg0
, long arg1
, long arg2
)
14 volatile uint64_t magic_mem
[8] __attribute__((aligned(64)));
20 write_csr(tohost
, (long)magic_mem
);
21 while (swap_csr(fromhost
, 0) == 0);
25 // In setStats, we might trap reading uarch-specific counters.
26 // The trap handler will skip over the instruction and write 0,
27 // but only if v0 is the destination register.
28 #define read_csr_safe(reg) ({ register long __tmp asm("v0"); \
29 asm volatile ("csrr %0, " #reg : "=r"(__tmp)); \
32 #define NUM_COUNTERS 18
33 static long counters
[NUM_COUNTERS
];
34 static char* counter_names
[NUM_COUNTERS
];
35 static int handle_stats(int enable
)
38 #define READ_CTR(name) do { \
39 while (i >= NUM_COUNTERS) ; \
40 long csr = read_csr_safe(name); \
41 if (!enable) { csr -= counters[i]; counter_names[i] = #name; } \
42 counters[i++] = csr; \
44 READ_CTR(cycle
); READ_CTR(instret
);
45 READ_CTR(uarch0
); READ_CTR(uarch1
); READ_CTR(uarch2
); READ_CTR(uarch3
);
46 READ_CTR(uarch4
); READ_CTR(uarch5
); READ_CTR(uarch6
); READ_CTR(uarch7
);
47 READ_CTR(uarch8
); READ_CTR(uarch9
); READ_CTR(uarch10
); READ_CTR(uarch11
);
48 READ_CTR(uarch12
); READ_CTR(uarch13
); READ_CTR(uarch14
); READ_CTR(uarch15
);
53 static void tohost_exit(int code
)
55 write_csr(tohost
, (code
<< 1) | 1);
59 long handle_trap(long cause
, long epc
, long regs
[32])
62 asm volatile ("lw %0, 1f; j 2f; 1: csrr v0, uarch0; 2:" : "=r"(csr_insn
));
65 if (cause
== CAUSE_ILLEGAL_INSTRUCTION
&&
66 (*(int*)epc
& csr_insn
) == csr_insn
)
68 else if (cause
!= CAUSE_SYSCALL
)
70 else if (regs
[16] == SYS_exit
)
71 tohost_exit(regs
[18]);
72 else if (regs
[16] == SYS_stats
)
73 sys_ret
= handle_stats(regs
[18]);
75 sys_ret
= handle_frontend_syscall(regs
[16], regs
[18], regs
[19], regs
[20]);
81 static long syscall(long num
, long arg0
, long arg1
, long arg2
)
83 register long v0
asm("v0") = num
;
84 register long a0
asm("a0") = arg0
;
85 register long a1
asm("a1") = arg1
;
86 register long a2
asm("a2") = arg2
;
87 asm volatile ("scall" : "+r"(v0
) : "r"(a0
), "r"(a1
), "r"(a2
) : "s0");
93 syscall(SYS_exit
, code
, 0, 0);
96 void setStats(int enable
)
98 syscall(SYS_stats
, enable
, 0, 0);
101 void printstr(const char* s
)
103 syscall(SYS_write
, 1, (long)s
, strlen(s
));
106 void __attribute__((weak
)) thread_entry(int cid
, int nc
)
108 // multi-threaded programs override this function.
109 // for the case of single-threaded programs, only let core 0 proceed.
113 int __attribute__((weak
)) main(int argc
, char** argv
)
115 // single-threaded programs override this function.
116 printstr("Implement main(), foo!\n");
120 void _init(int cid
, int nc
)
122 thread_entry(cid
, nc
);
124 // only single-threaded programs should ever get here.
125 int ret
= main(0, 0);
127 char buf
[NUM_COUNTERS
* 32] __attribute__((aligned(64)));
129 for (int i
= 0; i
< NUM_COUNTERS
; i
++)
131 pbuf
+= sprintf(pbuf
, "%s = %d\n", counter_names
[i
], counters
[i
]);
141 static char buf
[64] __attribute__((aligned(64)));
142 static int buflen
= 0;
146 if (ch
== '\n' || buflen
== sizeof(buf
))
148 syscall(SYS_write
, 1, (long)buf
, buflen
);
155 void printhex(uint64_t x
)
159 for (i
= 0; i
< 16; i
++)
161 str
[15-i
] = (x
& 0xF) + ((x
& 0xF) < 10 ? '0' : 'a'-10);
169 static inline void printnum(void (*putch
)(int, void**), void **putdat
,
170 unsigned long long num
, unsigned base
, int width
, int padc
)
172 unsigned digs
[sizeof(num
)*CHAR_BIT
];
177 digs
[pos
++] = num
% base
;
183 while (width
-- > pos
)
187 putch(digs
[pos
] + (digs
[pos
] >= 10 ? 'a' - 10 : '0'), putdat
);
190 static unsigned long long getuint(va_list *ap
, int lflag
)
193 return va_arg(*ap
, unsigned long long);
195 return va_arg(*ap
, unsigned long);
197 return va_arg(*ap
, unsigned int);
200 static long long getint(va_list *ap
, int lflag
)
203 return va_arg(*ap
, long long);
205 return va_arg(*ap
, long);
207 return va_arg(*ap
, int);
210 static void vprintfmt(void (*putch
)(int, void**), void **putdat
, const char *fmt
, va_list ap
)
212 register const char* p
;
213 const char* last_fmt
;
214 register int ch
, err
;
215 unsigned long long num
;
216 int base
, lflag
, width
, precision
, altflag
;
220 while ((ch
= *(unsigned char *) fmt
) != '%') {
228 // Process a %-escape sequence
236 switch (ch
= *(unsigned char *) fmt
++) {
238 // flag to pad on the right
243 // flag to pad with 0's instead of spaces
258 for (precision
= 0; ; ++fmt
) {
259 precision
= precision
* 10 + ch
- '0';
261 if (ch
< '0' || ch
> '9')
264 goto process_precision
;
267 precision
= va_arg(ap
, int);
268 goto process_precision
;
281 width
= precision
, precision
= -1;
284 // long flag (doubled for long long)
291 putch(va_arg(ap
, int), putdat
);
296 if ((p
= va_arg(ap
, char *)) == NULL
)
298 if (width
> 0 && padc
!= '-')
299 for (width
-= strnlen(p
, precision
); width
> 0; width
--)
301 for (; (ch
= *p
) != '\0' && (precision
< 0 || --precision
>= 0); width
--) {
305 for (; width
> 0; width
--)
311 num
= getint(&ap
, lflag
);
312 if ((long long) num
< 0) {
314 num
= -(long long) num
;
322 goto unsigned_number
;
326 // should do something with padding so it's always 3 octits
328 goto unsigned_number
;
332 static_assert(sizeof(long) == sizeof(void*));
336 /* fall through to 'x' */
338 // (unsigned) hexadecimal
342 num
= getuint(&ap
, lflag
);
344 printnum(putch
, putdat
, num
, base
, width
, padc
);
347 // escaped '%' character
352 // unrecognized escape sequence - just print it literally
361 int printf(const char* fmt
, ...)
366 vprintfmt((void*)putchar
, 0, fmt
, ap
);
369 return 0; // incorrect return value, but who cares, anyway?
372 int sprintf(char* str
, const char* fmt
, ...)
378 void sprintf_putch(int ch
, void** data
)
380 char** pstr
= (char**)data
;
385 vprintfmt(sprintf_putch
, (void**)&str
, fmt
, ap
);