10 #define SYS_stats 1234
12 // initialized in crt.S
15 static long handle_frontend_syscall(long which
, long arg0
, long arg1
, long arg2
)
17 volatile uint64_t magic_mem
[8] __attribute__((aligned(64)));
23 write_csr(tohost
, (long)magic_mem
);
24 while (swap_csr(fromhost
, 0) == 0);
28 // In setStats, we might trap reading uarch-specific counters.
29 // The trap handler will skip over the instruction and write 0,
30 // but only if a0 is the destination register.
31 #define read_csr_safe(reg) ({ register long __tmp asm("a0"); \
32 asm volatile ("csrr %0, " #reg : "=r"(__tmp)); \
35 #define NUM_COUNTERS 18
36 static long counters
[NUM_COUNTERS
];
37 static char* counter_names
[NUM_COUNTERS
];
38 static int handle_stats(int enable
)
40 //use csrs to set stats register
42 asm volatile ("csrrs a0, stats, 1" ::: "a0");
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
);
57 asm volatile ("csrrc a0, stats, 1" ::: "a0");
61 static void tohost_exit(int code
)
63 write_csr(tohost
, (code
<< 1) | 1);
67 long handle_trap(long cause
, long epc
, long regs
[32])
70 asm ("jal %0, 1f; csrr a0, stats; 1:" : "=r"(csr_insn
));
73 if (cause
== CAUSE_ILLEGAL_INSTRUCTION
&&
74 (*(int*)epc
& *csr_insn
) == *csr_insn
)
76 else if (cause
!= CAUSE_SYSCALL
)
78 else if (regs
[17] == SYS_exit
)
79 tohost_exit(regs
[10]);
80 else if (regs
[17] == SYS_stats
)
81 sys_ret
= handle_stats(regs
[10]);
83 sys_ret
= handle_frontend_syscall(regs
[17], regs
[10], regs
[11], regs
[12]);
89 static long syscall(long num
, long arg0
, long arg1
, long arg2
)
91 register long a7
asm("a7") = num
;
92 register long a0
asm("a0") = arg0
;
93 register long a1
asm("a1") = arg1
;
94 register long a2
asm("a2") = arg2
;
95 asm volatile ("scall" : "+r"(a0
) : "r"(a1
), "r"(a2
), "r"(a7
));
101 syscall(SYS_exit
, code
, 0, 0);
105 void setStats(int enable
)
107 syscall(SYS_stats
, enable
, 0, 0);
110 void printstr(const char* s
)
112 syscall(SYS_write
, 1, (long)s
, strlen(s
));
115 void __attribute__((weak
)) thread_entry(int cid
, int nc
)
117 // multi-threaded programs override this function.
118 // for the case of single-threaded programs, only let core 0 proceed.
122 int __attribute__((weak
)) main(int argc
, char** argv
)
124 // single-threaded programs override this function.
125 printstr("Implement main(), foo!\n");
129 static void init_tls()
131 register void* thread_pointer
asm("tp");
132 extern char _tls_data
;
133 extern __thread
char _tdata_begin
, _tdata_end
, _tbss_end
;
134 size_t tdata_size
= &_tdata_end
- &_tdata_begin
;
135 memcpy(thread_pointer
, &_tls_data
, tdata_size
);
136 size_t tbss_size
= &_tbss_end
- &_tdata_end
;
137 memset(thread_pointer
+ tdata_size
, 0, tbss_size
);
140 void _init(int cid
, int nc
)
143 thread_entry(cid
, nc
);
145 // only single-threaded programs should ever get here.
146 int ret
= main(0, 0);
148 char buf
[NUM_COUNTERS
* 32] __attribute__((aligned(64)));
150 for (int i
= 0; i
< NUM_COUNTERS
; i
++)
152 pbuf
+= sprintf(pbuf
, "%s = %d\n", counter_names
[i
], counters
[i
]);
162 static __thread
char buf
[64] __attribute__((aligned(64)));
163 static __thread
int buflen
= 0;
167 if (ch
== '\n' || buflen
== sizeof(buf
))
169 syscall(SYS_write
, 1, (long)buf
, buflen
);
176 void printhex(uint64_t x
)
180 for (i
= 0; i
< 16; i
++)
182 str
[15-i
] = (x
& 0xF) + ((x
& 0xF) < 10 ? '0' : 'a'-10);
190 static inline void printnum(void (*putch
)(int, void**), void **putdat
,
191 unsigned long long num
, unsigned base
, int width
, int padc
)
193 unsigned digs
[sizeof(num
)*CHAR_BIT
];
198 digs
[pos
++] = num
% base
;
204 while (width
-- > pos
)
208 putch(digs
[pos
] + (digs
[pos
] >= 10 ? 'a' - 10 : '0'), putdat
);
211 static unsigned long long getuint(va_list *ap
, int lflag
)
214 return va_arg(*ap
, unsigned long long);
216 return va_arg(*ap
, unsigned long);
218 return va_arg(*ap
, unsigned int);
221 static long long getint(va_list *ap
, int lflag
)
224 return va_arg(*ap
, long long);
226 return va_arg(*ap
, long);
228 return va_arg(*ap
, int);
231 static void vprintfmt(void (*putch
)(int, void**), void **putdat
, const char *fmt
, va_list ap
)
233 register const char* p
;
234 const char* last_fmt
;
235 register int ch
, err
;
236 unsigned long long num
;
237 int base
, lflag
, width
, precision
, altflag
;
241 while ((ch
= *(unsigned char *) fmt
) != '%') {
249 // Process a %-escape sequence
257 switch (ch
= *(unsigned char *) fmt
++) {
259 // flag to pad on the right
264 // flag to pad with 0's instead of spaces
279 for (precision
= 0; ; ++fmt
) {
280 precision
= precision
* 10 + ch
- '0';
282 if (ch
< '0' || ch
> '9')
285 goto process_precision
;
288 precision
= va_arg(ap
, int);
289 goto process_precision
;
302 width
= precision
, precision
= -1;
305 // long flag (doubled for long long)
312 putch(va_arg(ap
, int), putdat
);
317 if ((p
= va_arg(ap
, char *)) == NULL
)
319 if (width
> 0 && padc
!= '-')
320 for (width
-= strnlen(p
, precision
); width
> 0; width
--)
322 for (; (ch
= *p
) != '\0' && (precision
< 0 || --precision
>= 0); width
--) {
326 for (; width
> 0; width
--)
332 num
= getint(&ap
, lflag
);
333 if ((long long) num
< 0) {
335 num
= -(long long) num
;
343 goto unsigned_number
;
347 // should do something with padding so it's always 3 octits
349 goto unsigned_number
;
353 static_assert(sizeof(long) == sizeof(void*));
357 /* fall through to 'x' */
359 // (unsigned) hexadecimal
363 num
= getuint(&ap
, lflag
);
365 printnum(putch
, putdat
, num
, base
, width
, padc
);
368 // escaped '%' character
373 // unrecognized escape sequence - just print it literally
382 int printf(const char* fmt
, ...)
387 vprintfmt((void*)putchar
, 0, fmt
, ap
);
390 return 0; // incorrect return value, but who cares, anyway?
393 int sprintf(char* str
, const char* fmt
, ...)
399 void sprintf_putch(int ch
, void** data
)
401 char** pstr
= (char**)data
;
406 vprintfmt(sprintf_putch
, (void**)&str
, fmt
, ap
);