syscall.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /* Host implementation of the tiny ring-3 syscall ABI.
  2. *
  3. * This is the whole "platform abstraction layer": the browser, ngui, uhttp
  4. * and the image/font code keep calling u_syscall*(), but here they land in
  5. * normal Linux calls instead of int 0x80. */
  6. #include <stdint.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <sys/stat.h>
  11. #include <time.h>
  12. #include <unistd.h>
  13. #include "host.h"
  14. #include "fs.h"
  15. #include "net.h"
  16. #include "proc.h"
  17. #include "syscall.h"
  18. #include "window.h"
  19. static void *argp(int a)
  20. {
  21. return (void *)(uintptr_t)(unsigned int)a;
  22. }
  23. static unsigned int host_ticks(void)
  24. {
  25. struct timespec ts;
  26. clock_gettime(CLOCK_MONOTONIC, &ts);
  27. return (unsigned int)((unsigned long long)ts.tv_sec * 1000ull +
  28. (unsigned long long)ts.tv_nsec / 1000000ull);
  29. }
  30. static int rtc_now(struct sys_datetime *dt)
  31. {
  32. time_t t = time(0);
  33. struct tm tm;
  34. if (!gmtime_r(&t, &tm))
  35. return -1;
  36. dt->year = (unsigned int)(tm.tm_year + 1900);
  37. dt->month = (unsigned int)(tm.tm_mon + 1);
  38. dt->day = (unsigned int)tm.tm_mday;
  39. dt->hour = (unsigned int)tm.tm_hour;
  40. dt->minute = (unsigned int)tm.tm_min;
  41. dt->second = (unsigned int)tm.tm_sec;
  42. dt->wday = (unsigned int)tm.tm_wday;
  43. return 0;
  44. }
  45. static int unsupported(int num)
  46. {
  47. fprintf(stderr, "browser-host: unsupported syscall %d\n", num);
  48. return -1;
  49. }
  50. int u_syscall1(int num, int a1);
  51. int u_syscall3(int num, int a1, int a2, int a3);
  52. int u_syscall0(int num)
  53. {
  54. return u_syscall1(num, 0);
  55. }
  56. int u_syscall1(int num, int a1)
  57. {
  58. return u_syscall3(num, a1, 0, 0);
  59. }
  60. int u_syscall2(int num, int a1, int a2)
  61. {
  62. return u_syscall3(num, a1, a2, 0);
  63. }
  64. int u_syscall3(int num, int a1, int a2, int a3)
  65. {
  66. switch (num) {
  67. case SYS_PUTSTR: {
  68. const char *s = (const char *)argp(a1);
  69. if (s)
  70. fputs(s, stdout);
  71. return 0;
  72. }
  73. case SYS_GET_TICKS_MS:
  74. return (int)host_ticks();
  75. case SYS_SLEEP_MS:
  76. host_window_pump((unsigned int)a1);
  77. return 0;
  78. case SYS_EXIT:
  79. fflush(stdout);
  80. fflush(stderr);
  81. exit(a1);
  82. return 0;
  83. case SYS_READ_FILE:
  84. return host_fs_read((const char *)argp(a1), argp(a2),
  85. (unsigned int)a3);
  86. case SYS_WRITE_FILE:
  87. return host_fs_write((const char *)argp(a1), argp(a2),
  88. (unsigned int)a3);
  89. case SYS_LIST_DIR:
  90. return host_fs_list((const char *)argp(a1), (unsigned int)a2,
  91. argp(a3));
  92. case SYS_UNLINK:
  93. return host_fs_unlink((const char *)argp(a1));
  94. case SYS_MKDIR:
  95. return host_fs_mkdir((const char *)argp(a1));
  96. case SYS_RMDIR:
  97. return host_fs_rmdir((const char *)argp(a1));
  98. case SYS_GETCWD: {
  99. char *buf = (char *)argp(a1);
  100. if (!buf || !a2)
  101. return -1;
  102. return getcwd(buf, (size_t)a2) ? 0 : -1;
  103. }
  104. case SYS_CHDIR: {
  105. char host[HOST_PATH_MAX];
  106. if (host_guest_to_host((const char *)argp(a1), host,
  107. sizeof(host)) != 0)
  108. return -1;
  109. return chdir(host) == 0 ? 0 : -1;
  110. }
  111. case SYS_FS_STAT: {
  112. struct sys_stat *st = (struct sys_stat *)argp(a2);
  113. if (!st)
  114. return -1;
  115. memset(st, 0, sizeof(*st));
  116. if (host_fs_stat((const char *)argp(a1), &st->size,
  117. &st->is_dir) != 0)
  118. return -1;
  119. return 0;
  120. }
  121. case SYS_RTC_HMS: {
  122. struct sys_time *out = (struct sys_time *)argp(a1);
  123. struct sys_datetime dt;
  124. if (!out || rtc_now(&dt) != 0)
  125. return -1;
  126. out->hour = dt.hour;
  127. out->minute = dt.minute;
  128. out->second = dt.second;
  129. return 0;
  130. }
  131. case SYS_RTC_GET: {
  132. struct sys_datetime *out = (struct sys_datetime *)argp(a1);
  133. if (!out)
  134. return -1;
  135. return rtc_now(out);
  136. }
  137. case SYS_RTC_SET:
  138. return -1;
  139. case SYS_KEY_POLL:
  140. return host_window_key();
  141. case SYS_WIN_KEY_WAIT:
  142. return host_window_key_wait((unsigned int)a1);
  143. case SYS_WIN_CREATE: {
  144. const struct win_create *in =
  145. (const struct win_create *)argp(a1);
  146. struct win_info *out = (struct win_info *)argp(a2);
  147. int w, h;
  148. if (!in || !out)
  149. return -1;
  150. w = host_opts.width ? host_opts.width : (int)in->w;
  151. h = host_opts.height ? host_opts.height : (int)in->h;
  152. if (w <= 0 || h <= 0)
  153. return -1;
  154. if (host_window_open((unsigned int)w, (unsigned int)h,
  155. in->title) != 0)
  156. return -1;
  157. out->fb = host_window_fb();
  158. out->width = host_window_width();
  159. out->height = host_window_height();
  160. out->id = 1;
  161. return 0;
  162. }
  163. case SYS_WIN_PRESENT:
  164. host_window_present(0, 0, host_window_width(),
  165. host_window_height());
  166. return 0;
  167. case SYS_WIN_CLOSE:
  168. host_window_close();
  169. return 0;
  170. case SYS_WIN_INFO: {
  171. struct win_info *out = (struct win_info *)argp(a1);
  172. if (!out || !host_window_fb())
  173. return -1;
  174. out->fb = host_window_fb();
  175. out->width = host_window_width();
  176. out->height = host_window_height();
  177. out->id = 1;
  178. return 0;
  179. }
  180. case SYS_WIN_ACK_RESIZE:
  181. return 0;
  182. case SYS_WIN_POINTER:
  183. return host_window_pointer((struct win_pointer *)argp(a1));
  184. case SYS_WIN_EVENT:
  185. return host_window_event((struct win_event *)argp(a1));
  186. case SYS_WIN_SCROLL:
  187. return host_window_scroll((struct win_scroll *)argp(a1));
  188. case SYS_WIN_RESIZE_SELF:
  189. return -1;
  190. case SYS_WIN_SET_CURSOR:
  191. return host_window_set_cursor((int)a1);
  192. case SYS_WM_SET_CURSOR:
  193. return -1;
  194. case SYS_SCREEN_INFO: {
  195. struct sys_screen *out = (struct sys_screen *)argp(a1);
  196. if (!out)
  197. return -1;
  198. return host_window_screen(&out->width, &out->height);
  199. }
  200. case SYS_TTY_CREATE:
  201. return host_tty_create();
  202. case SYS_TTY_READ:
  203. return host_tty_read(a1, (char *)argp(a2),
  204. (unsigned int)a3);
  205. case SYS_SPAWN: {
  206. const struct sys_spawn *sp =
  207. (const struct sys_spawn *)argp(a1);
  208. if (!sp)
  209. return -1;
  210. return host_spawn(sp->path, sp->arg, (int)sp->tty, sp->cwd);
  211. }
  212. case SYS_ALIVE:
  213. return host_alive((unsigned int)a1);
  214. case SYS_KILL:
  215. return host_kill((unsigned int)a1);
  216. case SYS_GETPID:
  217. return (int)getpid();
  218. case SYS_NET_INFO: {
  219. struct sys_netinfo *out = (struct sys_netinfo *)argp(a1);
  220. if (!out)
  221. return 0;
  222. host_net_info(out);
  223. return 1;
  224. }
  225. case SYS_NET_CONFIGURED:
  226. return 1;
  227. case SYS_NET_ACQUIRE:
  228. case SYS_NET_RELEASE:
  229. return 0;
  230. case SYS_NET_NEXT_HOP: {
  231. const unsigned char *dst = (const unsigned char *)argp(a1);
  232. unsigned char *hop = (unsigned char *)argp(a2);
  233. if (dst && hop)
  234. memcpy(hop, dst, 4);
  235. return 0;
  236. }
  237. case SYS_NET_ARP: {
  238. unsigned char *mac = (unsigned char *)argp(a2);
  239. if (mac) {
  240. mac[0] = 0x02;
  241. mac[1] = mac[2] = mac[3] = mac[4] = 0;
  242. mac[5] = 0x01;
  243. }
  244. return 0;
  245. }
  246. case SYS_DNS_RESOLVE:
  247. return host_net_dns((const char *)argp(a1),
  248. (unsigned char *)argp(a2));
  249. case SYS_TCP_CONNECT:
  250. return host_net_connect((const unsigned char *)argp(a1),
  251. (const unsigned char *)argp(a2),
  252. (unsigned int)a3);
  253. case SYS_TCP_PEER_MSS:
  254. return host_net_peer_mss();
  255. case SYS_TCP_SEND:
  256. return host_net_send(argp(a1), (unsigned int)a2);
  257. case SYS_TCP_RECV:
  258. return host_net_recv(argp(a1), (unsigned int)a2,
  259. (unsigned int)a3);
  260. case SYS_TCP_CLOSE:
  261. host_net_close();
  262. return 0;
  263. case SYS_NET_GET_IP: {
  264. unsigned char *ip = (unsigned char *)argp(a1);
  265. if (ip) {
  266. ip[0] = 127;
  267. ip[1] = ip[2] = 0;
  268. ip[3] = 1;
  269. }
  270. return 0;
  271. }
  272. case SYS_NET_GET_MASK: {
  273. unsigned char *ip = (unsigned char *)argp(a1);
  274. if (ip)
  275. memset(ip, 0xFF, 4);
  276. return 0;
  277. }
  278. case SYS_NET_GET_GW:
  279. case SYS_NET_GET_DNS: {
  280. unsigned char *ip = (unsigned char *)argp(a1);
  281. if (ip) {
  282. ip[0] = 127;
  283. ip[1] = ip[2] = 0;
  284. ip[3] = 1;
  285. }
  286. return 0;
  287. }
  288. case SYS_NET_MAC: {
  289. unsigned char *mac = (unsigned char *)argp(a1);
  290. if (mac) {
  291. mac[0] = 0x02;
  292. mac[1] = mac[2] = mac[3] = mac[4] = 0;
  293. mac[5] = 0x01;
  294. }
  295. return 0;
  296. }
  297. case SYS_NET_PRINT_INFO:
  298. fprintf(stderr, "browser-host: netdev host (linux)\n");
  299. return 0;
  300. case SYS_NETLOG: {
  301. const char *s = (const char *)argp(a1);
  302. if (s)
  303. fprintf(stderr, "%s\n", s);
  304. return 0;
  305. }
  306. default:
  307. return unsupported(num);
  308. }
  309. }