Syscall.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. #include "i386.h"
  2. #include "Process.h"
  3. #include "Syscall.h"
  4. #include "Console.h"
  5. #include "Scheduler.h"
  6. #include <Kernel/ProcessTracer.h>
  7. extern "C" void syscall_trap_entry(RegisterDump&);
  8. extern "C" void syscall_trap_handler();
  9. extern volatile RegisterDump* syscallRegDump;
  10. asm(
  11. ".globl syscall_trap_handler \n"
  12. "syscall_trap_handler:\n"
  13. " pusha\n"
  14. " pushw %ds\n"
  15. " pushw %es\n"
  16. " pushw %fs\n"
  17. " pushw %gs\n"
  18. " pushw %ss\n"
  19. " pushw %ss\n"
  20. " pushw %ss\n"
  21. " pushw %ss\n"
  22. " pushw %ss\n"
  23. " popw %ds\n"
  24. " popw %es\n"
  25. " popw %fs\n"
  26. " popw %gs\n"
  27. " mov %esp, %eax\n"
  28. " call syscall_trap_entry\n"
  29. " popw %gs\n"
  30. " popw %gs\n"
  31. " popw %fs\n"
  32. " popw %es\n"
  33. " popw %ds\n"
  34. " popa\n"
  35. " iret\n"
  36. );
  37. namespace Syscall {
  38. void initialize()
  39. {
  40. register_user_callable_interrupt_handler(0x82, syscall_trap_handler);
  41. kprintf("Syscall: int 0x82 handler installed\n");
  42. }
  43. int sync()
  44. {
  45. VFS::the().sync();
  46. return 0;
  47. }
  48. static dword handle(RegisterDump& regs, dword function, dword arg1, dword arg2, dword arg3)
  49. {
  50. current->process().did_syscall();
  51. ASSERT_INTERRUPTS_ENABLED();
  52. switch (function) {
  53. case Syscall::SC_yield:
  54. Scheduler::yield();
  55. break;
  56. case Syscall::SC_donate:
  57. return current->process().sys$donate((int)arg1);
  58. case Syscall::SC_gettid:
  59. return current->process().sys$gettid();
  60. case Syscall::SC_putch:
  61. Console::the().put_char(arg1 & 0xff);
  62. break;
  63. case Syscall::SC_sleep:
  64. return current->process().sys$sleep((unsigned)arg1);
  65. case Syscall::SC_usleep:
  66. return current->process().sys$usleep((unsigned)arg1);
  67. case Syscall::SC_gettimeofday:
  68. return current->process().sys$gettimeofday((timeval*)arg1);
  69. case Syscall::SC_get_dir_entries:
  70. return current->process().sys$get_dir_entries((int)arg1, (void*)arg2, (size_t)arg3);
  71. case Syscall::SC_lstat:
  72. return current->process().sys$lstat((const char*)arg1, (stat*)arg2);
  73. case Syscall::SC_stat:
  74. return current->process().sys$stat((const char*)arg1, (stat*)arg2);
  75. case Syscall::SC_getcwd:
  76. return current->process().sys$getcwd((char*)arg1, (size_t)arg2);
  77. case Syscall::SC_open:
  78. return current->process().sys$open((const char*)arg1, (int)arg2, (mode_t)arg3);
  79. case Syscall::SC_write:
  80. return current->process().sys$write((int)arg1, (const byte*)arg2, (ssize_t)arg3);
  81. case Syscall::SC_close:
  82. return current->process().sys$close((int)arg1);
  83. case Syscall::SC_read:
  84. return current->process().sys$read((int)arg1, (byte*)arg2, (ssize_t)arg3);
  85. case Syscall::SC_lseek:
  86. return current->process().sys$lseek((int)arg1, (off_t)arg2, (int)arg3);
  87. case Syscall::SC_kill:
  88. return current->process().sys$kill((pid_t)arg1, (int)arg2);
  89. case Syscall::SC_getuid:
  90. return current->process().sys$getuid();
  91. case Syscall::SC_getgid:
  92. return current->process().sys$getgid();
  93. case Syscall::SC_getpid:
  94. return current->process().sys$getpid();
  95. case Syscall::SC_getppid:
  96. return current->process().sys$getppid();
  97. case Syscall::SC_waitpid:
  98. return current->process().sys$waitpid((pid_t)arg1, (int*)arg2, (int)arg3);
  99. case Syscall::SC_mmap:
  100. return (dword)current->process().sys$mmap((const SC_mmap_params*)arg1);
  101. case Syscall::SC_select:
  102. return current->process().sys$select((const SC_select_params*)arg1);
  103. case Syscall::SC_poll:
  104. return current->process().sys$poll((pollfd*)arg1, (int)arg2, (int)arg3);
  105. case Syscall::SC_munmap:
  106. return current->process().sys$munmap((void*)arg1, (size_t)arg2);
  107. case Syscall::SC_gethostname:
  108. return current->process().sys$gethostname((char*)arg1, (size_t)arg2);
  109. case Syscall::SC_exit:
  110. cli();
  111. if (auto* tracer = current->process().tracer())
  112. tracer->did_syscall(function, arg1, arg2, arg3, 0);
  113. current->process().sys$exit((int)arg1);
  114. ASSERT_NOT_REACHED();
  115. return 0;
  116. case Syscall::SC_exit_thread:
  117. cli();
  118. if (auto* tracer = current->process().tracer())
  119. tracer->did_syscall(function, arg1, arg2, arg3, 0);
  120. current->process().sys$exit_thread((int)arg1);
  121. ASSERT_NOT_REACHED();
  122. break;
  123. case Syscall::SC_chdir:
  124. return current->process().sys$chdir((const char*)arg1);
  125. case Syscall::SC_uname:
  126. return current->process().sys$uname((utsname*)arg1);
  127. case Syscall::SC_set_mmap_name:
  128. return current->process().sys$set_mmap_name((void*)arg1, (size_t)arg2, (const char*)arg3);
  129. case Syscall::SC_readlink:
  130. return current->process().sys$readlink((const char*)arg1, (char*)arg2, (size_t)arg3);
  131. case Syscall::SC_ttyname_r:
  132. return current->process().sys$ttyname_r((int)arg1, (char*)arg2, (size_t)arg3);
  133. case Syscall::SC_ptsname_r:
  134. return current->process().sys$ptsname_r((int)arg1, (char*)arg2, (size_t)arg3);
  135. case Syscall::SC_setsid:
  136. return current->process().sys$setsid();
  137. case Syscall::SC_getsid:
  138. return current->process().sys$getsid((pid_t)arg1);
  139. case Syscall::SC_setpgid:
  140. return current->process().sys$setpgid((pid_t)arg1, (pid_t)arg2);
  141. case Syscall::SC_getpgid:
  142. return current->process().sys$getpgid((pid_t)arg1);
  143. case Syscall::SC_getpgrp:
  144. return current->process().sys$getpgrp();
  145. case Syscall::SC_fork:
  146. return current->process().sys$fork(regs);
  147. case Syscall::SC_execve:
  148. return current->process().sys$execve((const char*)arg1, (const char**)arg2, (const char**)arg3);
  149. case Syscall::SC_geteuid:
  150. return current->process().sys$geteuid();
  151. case Syscall::SC_getegid:
  152. return current->process().sys$getegid();
  153. case Syscall::SC_isatty:
  154. return current->process().sys$isatty((int)arg1);
  155. case Syscall::SC_getdtablesize:
  156. return current->process().sys$getdtablesize();
  157. case Syscall::SC_dup:
  158. return current->process().sys$dup((int)arg1);
  159. case Syscall::SC_dup2:
  160. return current->process().sys$dup2((int)arg1, (int)arg2);
  161. case Syscall::SC_sigaction:
  162. return current->process().sys$sigaction((int)arg1, (const sigaction*)arg2, (sigaction*)arg3);
  163. case Syscall::SC_umask:
  164. return current->process().sys$umask((mode_t)arg1);
  165. case Syscall::SC_getgroups:
  166. return current->process().sys$getgroups((ssize_t)arg1, (gid_t*)arg2);
  167. case Syscall::SC_setgroups:
  168. return current->process().sys$setgroups((ssize_t)arg1, (const gid_t*)arg2);
  169. case Syscall::SC_sigreturn:
  170. if (auto* tracer = current->process().tracer())
  171. tracer->did_syscall(function, arg1, arg2, arg3, 0);
  172. current->process().sys$sigreturn();
  173. ASSERT_NOT_REACHED();
  174. return 0;
  175. case Syscall::SC_sigprocmask:
  176. return current->process().sys$sigprocmask((int)arg1, (const sigset_t*)arg2, (sigset_t*)arg3);
  177. case Syscall::SC_pipe:
  178. return current->process().sys$pipe((int*)arg1);
  179. case Syscall::SC_killpg:
  180. return current->process().sys$killpg((int)arg1, (int)arg2);
  181. case Syscall::SC_setuid:
  182. return current->process().sys$setuid((uid_t)arg1);
  183. case Syscall::SC_setgid:
  184. return current->process().sys$setgid((gid_t)arg1);
  185. case Syscall::SC_alarm:
  186. return current->process().sys$alarm((unsigned)arg1);
  187. case Syscall::SC_access:
  188. return current->process().sys$access((const char*)arg1, (int)arg2);
  189. case Syscall::SC_fcntl:
  190. return current->process().sys$fcntl((int)arg1, (int)arg2, (dword)arg3);
  191. case Syscall::SC_ioctl:
  192. return current->process().sys$ioctl((int)arg1, (unsigned)arg2, (unsigned)arg3);
  193. case Syscall::SC_fstat:
  194. return current->process().sys$fstat((int)arg1, (stat*)arg2);
  195. case Syscall::SC_mkdir:
  196. return current->process().sys$mkdir((const char*)arg1, (mode_t)arg2);
  197. case Syscall::SC_times:
  198. return current->process().sys$times((tms*)arg1);
  199. case Syscall::SC_utime:
  200. return current->process().sys$utime((const char*)arg1, (const utimbuf*)arg2);
  201. case Syscall::SC_sync:
  202. return sync();
  203. case Syscall::SC_link:
  204. return current->process().sys$link((const char*)arg1, (const char*)arg2);
  205. case Syscall::SC_unlink:
  206. return current->process().sys$unlink((const char*)arg1);
  207. case Syscall::SC_symlink:
  208. return current->process().sys$symlink((const char*)arg1, (const char*)arg2);
  209. case Syscall::SC_read_tsc:
  210. return current->process().sys$read_tsc((dword*)arg1, (dword*)arg2);
  211. case Syscall::SC_rmdir:
  212. return current->process().sys$rmdir((const char*)arg1);
  213. case Syscall::SC_chmod:
  214. return current->process().sys$chmod((const char*)arg1, (mode_t)arg2);
  215. case Syscall::SC_fchmod:
  216. return current->process().sys$fchmod((int)arg1, (mode_t)arg2);
  217. case Syscall::SC_socket:
  218. return current->process().sys$socket((int)arg1, (int)arg2, (int)arg3);
  219. case Syscall::SC_bind:
  220. return current->process().sys$bind((int)arg1, (const sockaddr*)arg2, (socklen_t)arg3);
  221. case Syscall::SC_listen:
  222. return current->process().sys$listen((int)arg1, (int)arg2);
  223. case Syscall::SC_accept:
  224. return current->process().sys$accept((int)arg1, (sockaddr*)arg2, (socklen_t*)arg3);
  225. case Syscall::SC_connect:
  226. return current->process().sys$connect((int)arg1, (const sockaddr*)arg2, (socklen_t)arg3);
  227. case Syscall::SC_create_shared_buffer:
  228. return current->process().sys$create_shared_buffer((pid_t)arg1, (size_t)arg2, (void**)arg3);
  229. case Syscall::SC_get_shared_buffer:
  230. return (dword)current->process().sys$get_shared_buffer((int)arg1);
  231. case Syscall::SC_release_shared_buffer:
  232. return current->process().sys$release_shared_buffer((int)arg1);
  233. case Syscall::SC_chown:
  234. return current->process().sys$chown((const char*)arg1, (uid_t)arg2, (gid_t)arg3);
  235. case Syscall::SC_restore_signal_mask:
  236. return current->process().sys$restore_signal_mask((dword)arg1);
  237. case Syscall::SC_seal_shared_buffer:
  238. return current->process().sys$seal_shared_buffer((int)arg1);
  239. case Syscall::SC_get_shared_buffer_size:
  240. return current->process().sys$get_shared_buffer_size((int)arg1);
  241. case Syscall::SC_sendto:
  242. return current->process().sys$sendto((const SC_sendto_params*)arg1);
  243. case Syscall::SC_recvfrom:
  244. return current->process().sys$recvfrom((const SC_recvfrom_params*)arg1);
  245. case Syscall::SC_getsockopt:
  246. return current->process().sys$getsockopt((const SC_getsockopt_params*)arg1);
  247. case Syscall::SC_setsockopt:
  248. return current->process().sys$setsockopt((const SC_setsockopt_params*)arg1);
  249. case Syscall::SC_create_thread:
  250. return current->process().sys$create_thread((int(*)(void*))arg1, (void*)arg2);
  251. case Syscall::SC_rename:
  252. return current->process().sys$rename((const char*)arg1, (const char*)arg2);
  253. case Syscall::SC_shm_open:
  254. return current->process().sys$shm_open((const char*)arg1, (int)arg2, (mode_t)arg3);
  255. case Syscall::SC_shm_close:
  256. return current->process().sys$shm_unlink((const char*)arg1);
  257. case Syscall::SC_ftruncate:
  258. return current->process().sys$ftruncate((int)arg1, (off_t)arg2);
  259. case Syscall::SC_systrace:
  260. return current->process().sys$systrace((pid_t)arg1);
  261. case Syscall::SC_mknod:
  262. return current->process().sys$mknod((const char*)arg1, (mode_t)arg2, (dev_t)arg3);
  263. case Syscall::SC_writev:
  264. return current->process().sys$writev((int)arg1, (const struct iovec*)arg2, (int)arg3);
  265. default:
  266. kprintf("<%u> int0x82: Unknown function %u requested {%x, %x, %x}\n", current->process().pid(), function, arg1, arg2, arg3);
  267. break;
  268. }
  269. return 0;
  270. }
  271. }
  272. void syscall_trap_entry(RegisterDump& regs)
  273. {
  274. current->process().big_lock().lock();
  275. dword function = regs.eax;
  276. dword arg1 = regs.edx;
  277. dword arg2 = regs.ecx;
  278. dword arg3 = regs.ebx;
  279. regs.eax = Syscall::handle(regs, function, arg1, arg2, arg3);
  280. if (auto* tracer = current->process().tracer())
  281. tracer->did_syscall(function, arg1, arg2, arg3, regs.eax);
  282. current->process().big_lock().unlock();
  283. }