1// See LICENSE for license details. 2 3#include "syscall.h" 4#include "pk.h" 5#include "file.h" 6#include "frontend.h" 7#include "mmap.h" 8#include "boot.h" 9#include <string.h> 10#include <errno.h> 11 12typedef long (*syscall_t)(long, long, long, long, long, long, long); 13 14#define CLOCK_FREQ 1000000000 15 16void sys_exit(int code) 17{ 18 if (current.cycle0) { 19 size_t dt = rdtime() - current.time0; 20 size_t dc = rdcycle() - current.cycle0; 21 size_t di = rdinstret() - current.instret0; 22 23 printk("%ld ticks\n", dt); 24 printk("%ld cycles\n", dc); 25 printk("%ld instructions\n", di); 26 printk("%d.%d%d CPI\n", dc/di, 10ULL*dc/di % 10, (100ULL*dc + di/2)/di % 10); 27 } 28 shutdown(code); 29} 30 31ssize_t sys_read(int fd, char* buf, size_t n) 32{ 33 ssize_t r = -EBADF; 34 file_t* f = file_get(fd); 35 36 if (f) 37 { 38 r = file_read(f, buf, n); 39 file_decref(f); 40 } 41 42 return r; 43} 44 45ssize_t sys_pread(int fd, char* buf, size_t n, off_t offset) 46{ 47 ssize_t r = -EBADF; 48 file_t* f = file_get(fd); 49 50 if (f) 51 { 52 r = file_pread(f, buf, n, offset); 53 file_decref(f); 54 } 55 56 return r; 57} 58 59ssize_t sys_write(int fd, const char* buf, size_t n) 60{ 61 ssize_t r = -EBADF; 62 file_t* f = file_get(fd); 63 64 if (f) 65 { 66 r = file_write(f, buf, n); 67 file_decref(f); 68 } 69 70 return r; 71} 72 73static int at_kfd(int dirfd) 74{ 75 if (dirfd == AT_FDCWD) 76 return AT_FDCWD; 77 file_t* dir = file_get(dirfd); 78 if (dir == NULL) 79 return -1; 80 return dir->kfd; 81} 82 83int sys_openat(int dirfd, const char* name, int flags, int mode) 84{ 85 int kfd = at_kfd(dirfd); 86 if (kfd != -1) { 87 file_t* file = file_openat(kfd, name, flags, mode); 88 if (IS_ERR_VALUE(file)) 89 return PTR_ERR(file); 90 91 int fd = file_dup(file); 92 if (fd < 0) { 93 file_decref(file); 94 return -ENOMEM; 95 } 96 97 return fd; 98 } 99 return -EBADF; 100} 101 102int sys_open(const char* name, int flags, int mode) 103{ 104 return sys_openat(AT_FDCWD, name, flags, mode); 105} 106 107int sys_close(int fd) 108{ 109 int ret = fd_close(fd); 110 if (ret < 0) 111 return -EBADF; 112 return ret; 113} 114 115int sys_renameat(int old_fd, const char *old_path, int new_fd, const char *new_path) { 116 int old_kfd = at_kfd(old_fd); 117 int new_kfd = at_kfd(new_fd); 118 if(old_kfd != -1 && new_kfd != -1) { 119 size_t old_size = strlen(old_path)+1; 120 size_t new_size = strlen(new_path)+1; 121 return frontend_syscall(SYS_renameat, old_kfd, va2pa(old_path), old_size, 122 new_kfd, va2pa(new_path), new_size, 0); 123 } 124 return -EBADF; 125} 126 127int sys_fstat(int fd, void* st) 128{ 129 int r = -EBADF; 130 file_t* f = file_get(fd); 131 132 if (f) 133 { 134 r = file_stat(f, st); 135 file_decref(f); 136 } 137 138 return r; 139} 140 141int sys_fcntl(int fd, int cmd, int arg) 142{ 143 int r = -EBADF; 144 file_t* f = file_get(fd); 145 146 if (f) 147 { 148 r = frontend_syscall(SYS_fcntl, f->kfd, cmd, arg, 0, 0, 0, 0); 149 file_decref(f); 150 } 151 152 return r; 153} 154 155int sys_ftruncate(int fd, off_t len) 156{ 157 int r = -EBADF; 158 file_t* f = file_get(fd); 159 160 if (f) 161 { 162 r = file_truncate(f, len); 163 file_decref(f); 164 } 165 166 return r; 167} 168 169int sys_dup(int fd) 170{ 171 int r = -EBADF; 172 file_t* f = file_get(fd); 173 174 if (f) 175 { 176 r = file_dup(f); 177 file_decref(f); 178 } 179 180 return r; 181} 182 183ssize_t sys_lseek(int fd, size_t ptr, int dir) 184{ 185 ssize_t r = -EBADF; 186 file_t* f = file_get(fd); 187 188 if (f) 189 { 190 r = file_lseek(f, ptr, dir); 191 file_decref(f); 192 } 193 194 return r; 195} 196 197long sys_lstat(const char* name, void* st) 198{ 199 struct frontend_stat buf; 200 size_t name_size = strlen(name)+1; 201 long ret = frontend_syscall(SYS_lstat, va2pa(name), name_size, va2pa(&buf), 0, 0, 0, 0); 202 copy_stat(st, &buf); 203 return ret; 204} 205 206long sys_fstatat(int dirfd, const char* name, void* st, int flags) 207{ 208 int kfd = at_kfd(dirfd); 209 if (kfd != -1) { 210 struct frontend_stat buf; 211 size_t name_size = strlen(name)+1; 212 long ret = frontend_syscall(SYS_fstatat, kfd, va2pa(name), name_size, va2pa(&buf), flags, 0, 0); 213 copy_stat(st, &buf); 214 return ret; 215 } 216 return -EBADF; 217} 218 219long sys_stat(const char* name, void* st) 220{ 221 return sys_fstatat(AT_FDCWD, name, st, 0); 222} 223 224long sys_faccessat(int dirfd, const char *name, int mode) 225{ 226 int kfd = at_kfd(dirfd); 227 if (kfd != -1) { 228 size_t name_size = strlen(name)+1; 229 return frontend_syscall(SYS_faccessat, kfd, va2pa(name), name_size, mode, 0, 0, 0); 230 } 231 return -EBADF; 232} 233 234long sys_access(const char *name, int mode) 235{ 236 return sys_faccessat(AT_FDCWD, name, mode); 237} 238 239long sys_linkat(int old_dirfd, const char* old_name, int new_dirfd, const char* new_name, int flags) 240{ 241 int old_kfd = at_kfd(old_dirfd); 242 int new_kfd = at_kfd(new_dirfd); 243 if (old_kfd != -1 && new_kfd != -1) { 244 size_t old_size = strlen(old_name)+1; 245 size_t new_size = strlen(new_name)+1; 246 return frontend_syscall(SYS_linkat, old_kfd, va2pa(old_name), old_size, 247 new_kfd, va2pa(new_name), new_size, 248 flags); 249 } 250 return -EBADF; 251} 252 253long sys_link(const char* old_name, const char* new_name) 254{ 255 return sys_linkat(AT_FDCWD, old_name, AT_FDCWD, new_name, 0); 256} 257 258long sys_unlinkat(int dirfd, const char* name, int flags) 259{ 260 int kfd = at_kfd(dirfd); 261 if (kfd != -1) { 262 size_t name_size = strlen(name)+1; 263 return frontend_syscall(SYS_unlinkat, kfd, va2pa(name), name_size, flags, 0, 0, 0); 264 } 265 return -EBADF; 266} 267 268long sys_unlink(const char* name) 269{ 270 return sys_unlinkat(AT_FDCWD, name, 0); 271} 272 273long sys_mkdirat(int dirfd, const char* name, int mode) 274{ 275 int kfd = at_kfd(dirfd); 276 if (kfd != -1) { 277 size_t name_size = strlen(name)+1; 278 return frontend_syscall(SYS_mkdirat, kfd, va2pa(name), name_size, mode, 0, 0, 0); 279 } 280 return -EBADF; 281} 282 283long sys_mkdir(const char* name, int mode) 284{ 285 return sys_mkdirat(AT_FDCWD, name, mode); 286} 287 288long sys_getcwd(const char* buf, size_t size) 289{ 290 populate_mapping(buf, size, PROT_WRITE); 291 return frontend_syscall(SYS_getcwd, va2pa(buf), size, 0, 0, 0, 0, 0); 292} 293 294size_t sys_brk(size_t pos) 295{ 296 return do_brk(pos); 297} 298 299int sys_uname(void* buf) 300{ 301 const int sz = 65; 302 strcpy(buf + 0*sz, "Proxy Kernel"); 303 strcpy(buf + 1*sz, ""); 304 strcpy(buf + 2*sz, "4.15.0"); 305 strcpy(buf + 3*sz, ""); 306 strcpy(buf + 4*sz, ""); 307 strcpy(buf + 5*sz, ""); 308 return 0; 309} 310 311pid_t sys_getpid() 312{ 313 return 0; 314} 315 316int sys_getuid() 317{ 318 return 0; 319} 320 321uintptr_t sys_mmap(uintptr_t addr, size_t length, int prot, int flags, int fd, off_t offset) 322{ 323#if __riscv_xlen == 32 324 if (offset != (offset << 12 >> 12)) 325 return -ENXIO; 326 offset <<= 12; 327#endif 328 return do_mmap(addr, length, prot, flags, fd, offset); 329} 330 331int sys_munmap(uintptr_t addr, size_t length) 332{ 333 return do_munmap(addr, length); 334} 335 336uintptr_t sys_mremap(uintptr_t addr, size_t old_size, size_t new_size, int flags) 337{ 338 return do_mremap(addr, old_size, new_size, flags); 339} 340 341uintptr_t sys_mprotect(uintptr_t addr, size_t length, int prot) 342{ 343 return do_mprotect(addr, length, prot); 344} 345 346int sys_rt_sigaction(int sig, const void* act, void* oact, size_t sssz) 347{ 348 if (oact) 349 memset(oact, 0, sizeof(long) * 3); 350 351 return 0; 352} 353 354long sys_time(long* loc) 355{ 356 uintptr_t t = rdcycle() / CLOCK_FREQ; 357 if (loc) 358 *loc = t; 359 return t; 360} 361 362int sys_times(long* loc) 363{ 364 uintptr_t t = rdcycle(); 365 kassert(CLOCK_FREQ % 1000000 == 0); 366 loc[0] = t / (CLOCK_FREQ / 1000000); 367 loc[1] = 0; 368 loc[2] = 0; 369 loc[3] = 0; 370 371 return 0; 372} 373 374int sys_gettimeofday(long* loc) 375{ 376 uintptr_t t = rdcycle(); 377 loc[0] = t / CLOCK_FREQ; 378 loc[1] = (t % CLOCK_FREQ) / (CLOCK_FREQ / 1000000); 379 380 return 0; 381} 382 383long sys_clock_gettime(int clk_id, long *loc) 384{ 385 uintptr_t t = rdcycle(); 386 loc[0] = t / CLOCK_FREQ; 387 loc[1] = (t % CLOCK_FREQ) / (CLOCK_FREQ / 1000000000); 388 389 return 0; 390} 391 392ssize_t sys_writev(int fd, const long* iov, int cnt) 393{ 394 ssize_t ret = 0; 395 for (int i = 0; i < cnt; i++) 396 { 397 ssize_t r = sys_write(fd, (void*)iov[2*i], iov[2*i+1]); 398 if (r < 0) 399 return r; 400 ret += r; 401 } 402 return ret; 403} 404 405int sys_chdir(const char *path) 406{ 407 return frontend_syscall(SYS_chdir, va2pa(path), 0, 0, 0, 0, 0, 0); 408} 409 410int sys_getdents(int fd, void* dirbuf, int count) 411{ 412 return 0; //stub 413} 414 415static int sys_stub_success() 416{ 417 return 0; 418} 419 420static int sys_stub_nosys() 421{ 422 return -ENOSYS; 423} 424 425long do_syscall(long a0, long a1, long a2, long a3, long a4, long a5, unsigned long n) 426{ 427 const static void* syscall_table[] = { 428 [SYS_exit] = sys_exit, 429 [SYS_exit_group] = sys_exit, 430 [SYS_read] = sys_read, 431 [SYS_pread] = sys_pread, 432 [SYS_write] = sys_write, 433 [SYS_openat] = sys_openat, 434 [SYS_close] = sys_close, 435 [SYS_fstat] = sys_fstat, 436 [SYS_lseek] = sys_lseek, 437 [SYS_fstatat] = sys_fstatat, 438 [SYS_linkat] = sys_linkat, 439 [SYS_unlinkat] = sys_unlinkat, 440 [SYS_mkdirat] = sys_mkdirat, 441 [SYS_renameat] = sys_renameat, 442 [SYS_getcwd] = sys_getcwd, 443 [SYS_brk] = sys_brk, 444 [SYS_uname] = sys_uname, 445 [SYS_getpid] = sys_getpid, 446 [SYS_getuid] = sys_getuid, 447 [SYS_geteuid] = sys_getuid, 448 [SYS_getgid] = sys_getuid, 449 [SYS_getegid] = sys_getuid, 450 [SYS_mmap] = sys_mmap, 451 [SYS_munmap] = sys_munmap, 452 [SYS_mremap] = sys_mremap, 453 [SYS_mprotect] = sys_mprotect, 454 [SYS_prlimit64] = sys_stub_nosys, 455 [SYS_rt_sigaction] = sys_rt_sigaction, 456 [SYS_gettimeofday] = sys_gettimeofday, 457 [SYS_times] = sys_times, 458 [SYS_writev] = sys_writev, 459 [SYS_faccessat] = sys_faccessat, 460 [SYS_fcntl] = sys_fcntl, 461 [SYS_ftruncate] = sys_ftruncate, 462 [SYS_getdents] = sys_getdents, 463 [SYS_dup] = sys_dup, 464 [SYS_readlinkat] = sys_stub_nosys, 465 [SYS_rt_sigprocmask] = sys_stub_success, 466 [SYS_ioctl] = sys_stub_nosys, 467 [SYS_clock_gettime] = sys_clock_gettime, 468 [SYS_getrusage] = sys_stub_nosys, 469 [SYS_getrlimit] = sys_stub_nosys, 470 [SYS_setrlimit] = sys_stub_nosys, 471 [SYS_chdir] = sys_chdir, 472 [SYS_set_tid_address] = sys_stub_nosys, 473 [SYS_set_robust_list] = sys_stub_nosys, 474 [SYS_madvise] = sys_stub_nosys, 475 }; 476 477 const static void* old_syscall_table[] = { 478 [-OLD_SYSCALL_THRESHOLD + SYS_open] = sys_open, 479 [-OLD_SYSCALL_THRESHOLD + SYS_link] = sys_link, 480 [-OLD_SYSCALL_THRESHOLD + SYS_unlink] = sys_unlink, 481 [-OLD_SYSCALL_THRESHOLD + SYS_mkdir] = sys_mkdir, 482 [-OLD_SYSCALL_THRESHOLD + SYS_access] = sys_access, 483 [-OLD_SYSCALL_THRESHOLD + SYS_stat] = sys_stat, 484 [-OLD_SYSCALL_THRESHOLD + SYS_lstat] = sys_lstat, 485 [-OLD_SYSCALL_THRESHOLD + SYS_time] = sys_time, 486 }; 487 488 syscall_t f = 0; 489 490 if (n < ARRAY_SIZE(syscall_table)) 491 f = syscall_table[n]; 492 else if (n - OLD_SYSCALL_THRESHOLD < ARRAY_SIZE(old_syscall_table)) 493 f = old_syscall_table[n - OLD_SYSCALL_THRESHOLD]; 494 495 if (!f) 496 panic("bad syscall #%ld!",n); 497 498 return f(a0, a1, a2, a3, a4, a5, n); 499} 500