syscalls.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. // See LICENSE for license details.
  2. #include <stdint.h>
  3. #include <string.h>
  4. #include <stdarg.h>
  5. #include <stdio.h>
  6. #include <limits.h>
  7. #include "util.h"
  8. #define SYS_write 64
  9. #define SYS_exit 93
  10. #define SYS_stats 1234
  11. extern volatile uint64_t tohost;
  12. extern volatile uint64_t fromhost;
  13. static uintptr_t handle_frontend_syscall(uintptr_t which, uint64_t arg0, uint64_t arg1, uint64_t arg2)
  14. {
  15. volatile uint64_t magic_mem[8] __attribute__((aligned(64)));
  16. magic_mem[0] = which;
  17. magic_mem[1] = arg0;
  18. magic_mem[2] = arg1;
  19. magic_mem[3] = arg2;
  20. __sync_synchronize();
  21. tohost = (uintptr_t)magic_mem;
  22. while (fromhost == 0)
  23. ;
  24. fromhost = 0;
  25. __sync_synchronize();
  26. return magic_mem[0];
  27. }
  28. #define NUM_COUNTERS 2
  29. static uintptr_t counters[NUM_COUNTERS];
  30. static char* counter_names[NUM_COUNTERS];
  31. static int handle_stats(int enable)
  32. {
  33. int i = 0;
  34. #define READ_CTR(name) do { \
  35. while (i >= NUM_COUNTERS) ; \
  36. uintptr_t csr = read_csr(name); \
  37. if (!enable) { csr -= counters[i]; counter_names[i] = #name; } \
  38. counters[i++] = csr; \
  39. } while (0)
  40. READ_CTR(mcycle);
  41. READ_CTR(minstret);
  42. #undef READ_CTR
  43. return 0;
  44. }
  45. void __attribute__((noreturn)) tohost_exit(uintptr_t code)
  46. {
  47. tohost = (code << 1) | 1;
  48. while (1);
  49. }
  50. uintptr_t handle_trap(uintptr_t cause, uintptr_t epc, uintptr_t regs[32])
  51. {
  52. if (cause != CAUSE_MACHINE_ECALL)
  53. tohost_exit(1337);
  54. else if (regs[17] == SYS_exit)
  55. tohost_exit(regs[10]);
  56. else if (regs[17] == SYS_stats)
  57. regs[10] = handle_stats(regs[10]);
  58. else
  59. regs[10] = handle_frontend_syscall(regs[17], regs[10], regs[11], regs[12]);
  60. return epc + ((*(unsigned short*)epc & 3) == 3 ? 4 : 2);
  61. }
  62. static uintptr_t syscall(uintptr_t num, uintptr_t arg0, uintptr_t arg1, uintptr_t arg2)
  63. {
  64. register uintptr_t a7 asm("a7") = num;
  65. register uintptr_t a0 asm("a0") = arg0;
  66. register uintptr_t a1 asm("a1") = arg1;
  67. register uintptr_t a2 asm("a2") = arg2;
  68. asm volatile ("scall" : "+r"(a0) : "r"(a1), "r"(a2), "r"(a7));
  69. return a0;
  70. }
  71. void exit(int code)
  72. {
  73. syscall(SYS_exit, code, 0, 0);
  74. while (1);
  75. }
  76. void setStats(int enable)
  77. {
  78. syscall(SYS_stats, enable, 0, 0);
  79. }
  80. void printstr(const char* s)
  81. {
  82. syscall(SYS_write, 1, (uintptr_t)s, strlen(s));
  83. }
  84. void __attribute__((weak)) thread_entry(int cid, int nc)
  85. {
  86. // multi-threaded programs override this function.
  87. // for the case of single-threaded programs, only let core 0 proceed.
  88. while (cid != 0);
  89. }
  90. int __attribute__((weak)) main(int argc, char** argv)
  91. {
  92. // single-threaded programs override this function.
  93. printstr("Implement main(), foo!\n");
  94. return -1;
  95. }
  96. static void init_tls()
  97. {
  98. register void* thread_pointer asm("tp");
  99. extern char _tls_data;
  100. extern __thread char _tdata_begin, _tdata_end, _tbss_end;
  101. size_t tdata_size = &_tdata_end - &_tdata_begin;
  102. memcpy(thread_pointer, &_tls_data, tdata_size);
  103. size_t tbss_size = &_tbss_end - &_tdata_end;
  104. memset(thread_pointer + tdata_size, 0, tbss_size);
  105. }
  106. void _init(int cid, int nc)
  107. {
  108. init_tls();
  109. thread_entry(cid, nc);
  110. // only single-threaded programs should ever get here.
  111. int ret = main(0, 0);
  112. char buf[NUM_COUNTERS * 32] __attribute__((aligned(64)));
  113. char* pbuf = buf;
  114. for (int i = 0; i < NUM_COUNTERS; i++)
  115. if (counters[i])
  116. pbuf += sprintf(pbuf, "%s = %d\n", counter_names[i], counters[i]);
  117. if (pbuf != buf)
  118. printstr(buf);
  119. exit(ret);
  120. }
  121. #undef putchar
  122. int putchar(int ch)
  123. {
  124. static __thread char buf[64] __attribute__((aligned(64)));
  125. static __thread int buflen = 0;
  126. buf[buflen++] = ch;
  127. if (ch == '\n' || buflen == sizeof(buf))
  128. {
  129. syscall(SYS_write, 1, (uintptr_t)buf, buflen);
  130. buflen = 0;
  131. }
  132. return 0;
  133. }
  134. void printhex(uint64_t x)
  135. {
  136. char str[17];
  137. int i;
  138. for (i = 0; i < 16; i++)
  139. {
  140. str[15-i] = (x & 0xF) + ((x & 0xF) < 10 ? '0' : 'a'-10);
  141. x >>= 4;
  142. }
  143. str[16] = 0;
  144. printstr(str);
  145. }
  146. static inline void printnum(void (*putch)(int, void**), void **putdat,
  147. unsigned long long num, unsigned base, int width, int padc)
  148. {
  149. unsigned digs[sizeof(num)*CHAR_BIT];
  150. int pos = 0;
  151. while (1)
  152. {
  153. digs[pos++] = num % base;
  154. if (num < base)
  155. break;
  156. num /= base;
  157. }
  158. while (width-- > pos)
  159. putch(padc, putdat);
  160. while (pos-- > 0)
  161. putch(digs[pos] + (digs[pos] >= 10 ? 'a' - 10 : '0'), putdat);
  162. }
  163. static unsigned long long getuint(va_list *ap, int lflag)
  164. {
  165. if (lflag >= 2)
  166. return va_arg(*ap, unsigned long long);
  167. else if (lflag)
  168. return va_arg(*ap, unsigned long);
  169. else
  170. return va_arg(*ap, unsigned int);
  171. }
  172. static long long getint(va_list *ap, int lflag)
  173. {
  174. if (lflag >= 2)
  175. return va_arg(*ap, long long);
  176. else if (lflag)
  177. return va_arg(*ap, long);
  178. else
  179. return va_arg(*ap, int);
  180. }
  181. static void vprintfmt(void (*putch)(int, void**), void **putdat, const char *fmt, va_list ap)
  182. {
  183. register const char* p;
  184. const char* last_fmt;
  185. register int ch, err;
  186. unsigned long long num;
  187. int base, lflag, width, precision, altflag;
  188. char padc;
  189. while (1) {
  190. while ((ch = *(unsigned char *) fmt) != '%') {
  191. if (ch == '\0')
  192. return;
  193. fmt++;
  194. putch(ch, putdat);
  195. }
  196. fmt++;
  197. // Process a %-escape sequence
  198. last_fmt = fmt;
  199. padc = ' ';
  200. width = -1;
  201. precision = -1;
  202. lflag = 0;
  203. altflag = 0;
  204. reswitch:
  205. switch (ch = *(unsigned char *) fmt++) {
  206. // flag to pad on the right
  207. case '-':
  208. padc = '-';
  209. goto reswitch;
  210. // flag to pad with 0's instead of spaces
  211. case '0':
  212. padc = '0';
  213. goto reswitch;
  214. // width field
  215. case '1':
  216. case '2':
  217. case '3':
  218. case '4':
  219. case '5':
  220. case '6':
  221. case '7':
  222. case '8':
  223. case '9':
  224. for (precision = 0; ; ++fmt) {
  225. precision = precision * 10 + ch - '0';
  226. ch = *fmt;
  227. if (ch < '0' || ch > '9')
  228. break;
  229. }
  230. goto process_precision;
  231. case '*':
  232. precision = va_arg(ap, int);
  233. goto process_precision;
  234. case '.':
  235. if (width < 0)
  236. width = 0;
  237. goto reswitch;
  238. case '#':
  239. altflag = 1;
  240. goto reswitch;
  241. process_precision:
  242. if (width < 0)
  243. width = precision, precision = -1;
  244. goto reswitch;
  245. // long flag (doubled for long long)
  246. case 'l':
  247. lflag++;
  248. goto reswitch;
  249. // character
  250. case 'c':
  251. putch(va_arg(ap, int), putdat);
  252. break;
  253. // string
  254. case 's':
  255. if ((p = va_arg(ap, char *)) == NULL)
  256. p = "(null)";
  257. if (width > 0 && padc != '-')
  258. for (width -= strnlen(p, precision); width > 0; width--)
  259. putch(padc, putdat);
  260. for (; (ch = *p) != '\0' && (precision < 0 || --precision >= 0); width--) {
  261. putch(ch, putdat);
  262. p++;
  263. }
  264. for (; width > 0; width--)
  265. putch(' ', putdat);
  266. break;
  267. // (signed) decimal
  268. case 'd':
  269. num = getint(&ap, lflag);
  270. if ((long long) num < 0) {
  271. putch('-', putdat);
  272. num = -(long long) num;
  273. }
  274. base = 10;
  275. goto signed_number;
  276. // unsigned decimal
  277. case 'u':
  278. base = 10;
  279. goto unsigned_number;
  280. // (unsigned) octal
  281. case 'o':
  282. // should do something with padding so it's always 3 octits
  283. base = 8;
  284. goto unsigned_number;
  285. // pointer
  286. case 'p':
  287. static_assert(sizeof(long) == sizeof(void*));
  288. lflag = 1;
  289. putch('0', putdat);
  290. putch('x', putdat);
  291. /* fall through to 'x' */
  292. // (unsigned) hexadecimal
  293. case 'x':
  294. base = 16;
  295. unsigned_number:
  296. num = getuint(&ap, lflag);
  297. signed_number:
  298. printnum(putch, putdat, num, base, width, padc);
  299. break;
  300. // escaped '%' character
  301. case '%':
  302. putch(ch, putdat);
  303. break;
  304. // unrecognized escape sequence - just print it literally
  305. default:
  306. putch('%', putdat);
  307. fmt = last_fmt;
  308. break;
  309. }
  310. }
  311. }
  312. int printf(const char* fmt, ...)
  313. {
  314. va_list ap;
  315. va_start(ap, fmt);
  316. vprintfmt((void*)putchar, 0, fmt, ap);
  317. va_end(ap);
  318. return 0; // incorrect return value, but who cares, anyway?
  319. }
  320. int sprintf(char* str, const char* fmt, ...)
  321. {
  322. va_list ap;
  323. char* str0 = str;
  324. va_start(ap, fmt);
  325. void sprintf_putch(int ch, void** data)
  326. {
  327. char** pstr = (char**)data;
  328. **pstr = ch;
  329. (*pstr)++;
  330. }
  331. vprintfmt(sprintf_putch, (void**)&str, fmt, ap);
  332. *str = 0;
  333. va_end(ap);
  334. return str - str0;
  335. }
  336. void* memcpy(void* dest, const void* src, size_t len)
  337. {
  338. if ((((uintptr_t)dest | (uintptr_t)src | len) & (sizeof(uintptr_t)-1)) == 0) {
  339. const uintptr_t* s = src;
  340. uintptr_t *d = dest;
  341. while (d < (uintptr_t*)(dest + len))
  342. *d++ = *s++;
  343. } else {
  344. const char* s = src;
  345. char *d = dest;
  346. while (d < (char*)(dest + len))
  347. *d++ = *s++;
  348. }
  349. return dest;
  350. }
  351. void* memset(void* dest, int byte, size_t len)
  352. {
  353. if ((((uintptr_t)dest | len) & (sizeof(uintptr_t)-1)) == 0) {
  354. uintptr_t word = byte & 0xFF;
  355. word |= word << 8;
  356. word |= word << 16;
  357. word |= word << 16 << 16;
  358. uintptr_t *d = dest;
  359. while (d < (uintptr_t*)(dest + len))
  360. *d++ = word;
  361. } else {
  362. char *d = dest;
  363. while (d < (char*)(dest + len))
  364. *d++ = byte;
  365. }
  366. return dest;
  367. }
  368. size_t strlen(const char *s)
  369. {
  370. const char *p = s;
  371. while (*p)
  372. p++;
  373. return p - s;
  374. }
  375. size_t strnlen(const char *s, size_t n)
  376. {
  377. const char *p = s;
  378. while (n-- && *p)
  379. p++;
  380. return p - s;
  381. }
  382. int strcmp(const char* s1, const char* s2)
  383. {
  384. unsigned char c1, c2;
  385. do {
  386. c1 = *s1++;
  387. c2 = *s2++;
  388. } while (c1 != 0 && c1 == c2);
  389. return c1 - c2;
  390. }
  391. char* strcpy(char* dest, const char* src)
  392. {
  393. char* d = dest;
  394. while ((*d++ = *src++))
  395. ;
  396. return dest;
  397. }
  398. long atol(const char* str)
  399. {
  400. long res = 0;
  401. int sign = 0;
  402. while (*str == ' ')
  403. str++;
  404. if (*str == '-' || *str == '+') {
  405. sign = *str == '-';
  406. str++;
  407. }
  408. while (*str) {
  409. res *= 10;
  410. res += *str++ - '0';
  411. }
  412. return sign ? -res : res;
  413. }
  414. #define HEAPSZ (1<<20)
  415. static uint64_t heap_mem[HEAPSZ];
  416. static size_t allocated = 0;
  417. void *malloc(size_t size)
  418. {
  419. if ((HEAPSZ-allocated)*8 >= size) {
  420. void *r = (void*)(&heap_mem[allocated]);
  421. allocated += (size+7)/8;
  422. return r;
  423. }
  424. return NULL;
  425. }