/* Host implementation of the tiny ring-3 syscall ABI. * * This is the whole "platform abstraction layer": the browser, ngui, uhttp * and the image/font code keep calling u_syscall*(), but here they land in * normal Linux calls instead of int 0x80. */ #include #include #include #include #include #include #include #include "host.h" #include "fs.h" #include "net.h" #include "proc.h" #include "syscall.h" #include "window.h" static void *argp(int a) { return (void *)(uintptr_t)(unsigned int)a; } static unsigned int host_ticks(void) { struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); return (unsigned int)((unsigned long long)ts.tv_sec * 1000ull + (unsigned long long)ts.tv_nsec / 1000000ull); } static int rtc_now(struct sys_datetime *dt) { time_t t = time(0); struct tm tm; if (!gmtime_r(&t, &tm)) return -1; dt->year = (unsigned int)(tm.tm_year + 1900); dt->month = (unsigned int)(tm.tm_mon + 1); dt->day = (unsigned int)tm.tm_mday; dt->hour = (unsigned int)tm.tm_hour; dt->minute = (unsigned int)tm.tm_min; dt->second = (unsigned int)tm.tm_sec; dt->wday = (unsigned int)tm.tm_wday; return 0; } static int unsupported(int num) { fprintf(stderr, "browser-host: unsupported syscall %d\n", num); return -1; } int u_syscall1(int num, int a1); int u_syscall3(int num, int a1, int a2, int a3); int u_syscall0(int num) { return u_syscall1(num, 0); } int u_syscall1(int num, int a1) { return u_syscall3(num, a1, 0, 0); } int u_syscall2(int num, int a1, int a2) { return u_syscall3(num, a1, a2, 0); } int u_syscall3(int num, int a1, int a2, int a3) { switch (num) { case SYS_PUTSTR: { const char *s = (const char *)argp(a1); if (s) fputs(s, stdout); return 0; } case SYS_GET_TICKS_MS: return (int)host_ticks(); case SYS_SLEEP_MS: host_window_pump((unsigned int)a1); return 0; case SYS_EXIT: fflush(stdout); fflush(stderr); exit(a1); return 0; case SYS_READ_FILE: return host_fs_read((const char *)argp(a1), argp(a2), (unsigned int)a3); case SYS_WRITE_FILE: return host_fs_write((const char *)argp(a1), argp(a2), (unsigned int)a3); case SYS_LIST_DIR: return host_fs_list((const char *)argp(a1), (unsigned int)a2, argp(a3)); case SYS_UNLINK: return host_fs_unlink((const char *)argp(a1)); case SYS_MKDIR: return host_fs_mkdir((const char *)argp(a1)); case SYS_RMDIR: return host_fs_rmdir((const char *)argp(a1)); case SYS_GETCWD: { char *buf = (char *)argp(a1); if (!buf || !a2) return -1; return getcwd(buf, (size_t)a2) ? 0 : -1; } case SYS_CHDIR: { char host[HOST_PATH_MAX]; if (host_guest_to_host((const char *)argp(a1), host, sizeof(host)) != 0) return -1; return chdir(host) == 0 ? 0 : -1; } case SYS_FS_STAT: { struct sys_stat *st = (struct sys_stat *)argp(a2); if (!st) return -1; memset(st, 0, sizeof(*st)); if (host_fs_stat((const char *)argp(a1), &st->size, &st->is_dir) != 0) return -1; return 0; } case SYS_RTC_HMS: { struct sys_time *out = (struct sys_time *)argp(a1); struct sys_datetime dt; if (!out || rtc_now(&dt) != 0) return -1; out->hour = dt.hour; out->minute = dt.minute; out->second = dt.second; return 0; } case SYS_RTC_GET: { struct sys_datetime *out = (struct sys_datetime *)argp(a1); if (!out) return -1; return rtc_now(out); } case SYS_RTC_SET: return -1; case SYS_KEY_POLL: return host_window_key(); case SYS_WIN_KEY_WAIT: return host_window_key_wait((unsigned int)a1); case SYS_WIN_CREATE: { const struct win_create *in = (const struct win_create *)argp(a1); struct win_info *out = (struct win_info *)argp(a2); int w, h; if (!in || !out) return -1; w = host_opts.width ? host_opts.width : (int)in->w; h = host_opts.height ? host_opts.height : (int)in->h; if (w <= 0 || h <= 0) return -1; if (host_window_open((unsigned int)w, (unsigned int)h, in->title) != 0) return -1; out->fb = host_window_fb(); out->width = host_window_width(); out->height = host_window_height(); out->id = 1; return 0; } case SYS_WIN_PRESENT: host_window_present(0, 0, host_window_width(), host_window_height()); return 0; case SYS_WIN_CLOSE: host_window_close(); return 0; case SYS_WIN_INFO: { struct win_info *out = (struct win_info *)argp(a1); if (!out || !host_window_fb()) return -1; out->fb = host_window_fb(); out->width = host_window_width(); out->height = host_window_height(); out->id = 1; return 0; } case SYS_WIN_ACK_RESIZE: return 0; case SYS_WIN_POINTER: return host_window_pointer((struct win_pointer *)argp(a1)); case SYS_WIN_EVENT: return host_window_event((struct win_event *)argp(a1)); case SYS_WIN_SCROLL: return host_window_scroll((struct win_scroll *)argp(a1)); case SYS_WIN_RESIZE_SELF: return -1; case SYS_SCREEN_INFO: { struct sys_screen *out = (struct sys_screen *)argp(a1); if (!out) return -1; return host_window_screen(&out->width, &out->height); } case SYS_TTY_CREATE: return host_tty_create(); case SYS_TTY_READ: return host_tty_read(a1, (char *)argp(a2), (unsigned int)a3); case SYS_SPAWN: { const struct sys_spawn *sp = (const struct sys_spawn *)argp(a1); if (!sp) return -1; return host_spawn(sp->path, sp->arg, (int)sp->tty, sp->cwd); } case SYS_ALIVE: return host_alive((unsigned int)a1); case SYS_KILL: return host_kill((unsigned int)a1); case SYS_GETPID: return (int)getpid(); case SYS_NET_INFO: { struct sys_netinfo *out = (struct sys_netinfo *)argp(a1); if (!out) return 0; host_net_info(out); return 1; } case SYS_NET_CONFIGURED: return 1; case SYS_NET_ACQUIRE: case SYS_NET_RELEASE: return 0; case SYS_NET_NEXT_HOP: { const unsigned char *dst = (const unsigned char *)argp(a1); unsigned char *hop = (unsigned char *)argp(a2); if (dst && hop) memcpy(hop, dst, 4); return 0; } case SYS_NET_ARP: { unsigned char *mac = (unsigned char *)argp(a2); if (mac) { mac[0] = 0x02; mac[1] = mac[2] = mac[3] = mac[4] = 0; mac[5] = 0x01; } return 0; } case SYS_DNS_RESOLVE: return host_net_dns((const char *)argp(a1), (unsigned char *)argp(a2)); case SYS_TCP_CONNECT: return host_net_connect((const unsigned char *)argp(a1), (const unsigned char *)argp(a2), (unsigned int)a3); case SYS_TCP_PEER_MSS: return host_net_peer_mss(); case SYS_TCP_SEND: return host_net_send(argp(a1), (unsigned int)a2); case SYS_TCP_RECV: return host_net_recv(argp(a1), (unsigned int)a2, (unsigned int)a3); case SYS_TCP_CLOSE: host_net_close(); return 0; case SYS_NET_GET_IP: { unsigned char *ip = (unsigned char *)argp(a1); if (ip) { ip[0] = 127; ip[1] = ip[2] = 0; ip[3] = 1; } return 0; } case SYS_NET_GET_MASK: { unsigned char *ip = (unsigned char *)argp(a1); if (ip) memset(ip, 0xFF, 4); return 0; } case SYS_NET_GET_GW: case SYS_NET_GET_DNS: { unsigned char *ip = (unsigned char *)argp(a1); if (ip) { ip[0] = 127; ip[1] = ip[2] = 0; ip[3] = 1; } return 0; } case SYS_NET_MAC: { unsigned char *mac = (unsigned char *)argp(a1); if (mac) { mac[0] = 0x02; mac[1] = mac[2] = mac[3] = mac[4] = 0; mac[5] = 0x01; } return 0; } case SYS_NET_PRINT_INFO: fprintf(stderr, "browser-host: netdev host (linux)\n"); return 0; case SYS_NETLOG: { const char *s = (const char *)argp(a1); if (s) fprintf(stderr, "%s\n", s); return 0; } default: return unsupported(num); } }