Syscall.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #include "i386.h"
  2. #include "Process.h"
  3. #include "Syscall.h"
  4. #include "Console.h"
  5. #include "Scheduler.h"
  6. extern "C" void syscall_entry(RegisterDump&);
  7. extern "C" void syscall_ISR();
  8. extern volatile RegisterDump* syscallRegDump;
  9. asm(
  10. ".globl syscall_ISR \n"
  11. "syscall_ISR:\n"
  12. " pusha\n"
  13. " pushw %ds\n"
  14. " pushw %es\n"
  15. " pushw %fs\n"
  16. " pushw %gs\n"
  17. " pushw %ss\n"
  18. " pushw %ss\n"
  19. " pushw %ss\n"
  20. " pushw %ss\n"
  21. " pushw %ss\n"
  22. " popw %ds\n"
  23. " popw %es\n"
  24. " popw %fs\n"
  25. " popw %gs\n"
  26. " mov %esp, %eax\n"
  27. " call syscall_entry\n"
  28. " popw %gs\n"
  29. " popw %gs\n"
  30. " popw %fs\n"
  31. " popw %es\n"
  32. " popw %ds\n"
  33. " popa\n"
  34. " iret\n"
  35. );
  36. namespace Syscall {
  37. void initialize()
  38. {
  39. registerUserCallableInterruptHandler(0x80, syscall_ISR);
  40. kprintf("syscall: int 0x80 handler installed\n");
  41. }
  42. static DWORD handle(RegisterDump& regs, DWORD function, DWORD arg1, DWORD arg2, DWORD arg3)
  43. {
  44. ASSERT_INTERRUPTS_ENABLED();
  45. switch (function) {
  46. case Syscall::SC_yield:
  47. Scheduler::yield();
  48. break;
  49. case Syscall::SC_putch:
  50. Console::the().putChar(arg1 & 0xff);
  51. break;
  52. case Syscall::SC_sleep:
  53. return current->sys$sleep((unsigned)arg1);
  54. case Syscall::SC_gettimeofday:
  55. return current->sys$gettimeofday((timeval*)arg1);
  56. case Syscall::SC_get_dir_entries:
  57. return current->sys$get_dir_entries((int)arg1, (void*)arg2, (size_t)arg3);
  58. case Syscall::SC_lstat:
  59. return current->sys$lstat((const char*)arg1, (Unix::stat*)arg2);
  60. case Syscall::SC_stat:
  61. return current->sys$stat((const char*)arg1, (Unix::stat*)arg2);
  62. case Syscall::SC_getcwd:
  63. return current->sys$getcwd((char*)arg1, (size_t)arg2);
  64. case Syscall::SC_open:
  65. return current->sys$open((const char*)arg1, (int)arg2);
  66. case Syscall::SC_write:
  67. return current->sys$write((int)arg1, (const void*)arg2, (size_t)arg3);
  68. case Syscall::SC_close:
  69. //kprintf("syscall: close(%d)\n", arg1);
  70. return current->sys$close((int)arg1);
  71. case Syscall::SC_read:
  72. //kprintf("syscall: read(%d, %p, %u)\n", arg1, arg2, arg3);
  73. return current->sys$read((int)arg1, (void*)arg2, (size_t)arg3);
  74. case Syscall::SC_lseek:
  75. return current->sys$lseek((int)arg1, (off_t)arg2, (int)arg3);
  76. case Syscall::SC_kill:
  77. return current->sys$kill((pid_t)arg1, (int)arg2);
  78. case Syscall::SC_getuid:
  79. return current->sys$getuid();
  80. case Syscall::SC_getgid:
  81. return current->sys$getgid();
  82. case Syscall::SC_getpid:
  83. return current->sys$getpid();
  84. case Syscall::SC_getppid:
  85. return current->sys$getppid();
  86. case Syscall::SC_waitpid:
  87. return current->sys$waitpid((pid_t)arg1, (int*)arg2, (int)arg3);
  88. case Syscall::SC_mmap:
  89. return (dword)current->sys$mmap((const SC_mmap_params*)arg1);
  90. case Syscall::SC_munmap:
  91. return current->sys$munmap((void*)arg1, (size_t)arg2);
  92. case Syscall::SC_gethostname:
  93. return current->sys$gethostname((char*)arg1, (size_t)arg2);
  94. case Syscall::SC_exit:
  95. cli();
  96. current->sys$exit((int)arg1);
  97. ASSERT_NOT_REACHED();
  98. return 0;
  99. case Syscall::SC_get_arguments:
  100. return current->sys$get_arguments((int*)arg1, (char***)arg2);
  101. case Syscall::SC_get_environment:
  102. return current->sys$get_environment((char***)arg1);
  103. case Syscall::SC_chdir:
  104. return current->sys$chdir((const char*)arg1);
  105. case Syscall::SC_uname:
  106. return current->sys$uname((utsname*)arg1);
  107. case Syscall::SC_set_mmap_name:
  108. return current->sys$set_mmap_name((void*)arg1, (size_t)arg2, (const char*)arg3);
  109. case Syscall::SC_readlink:
  110. return current->sys$readlink((const char*)arg1, (char*)arg2, (size_t)arg3);
  111. case Syscall::SC_ttyname_r:
  112. return current->sys$ttyname_r((int)arg1, (char*)arg2, (size_t)arg3);
  113. case Syscall::SC_setsid:
  114. return current->sys$setsid();
  115. case Syscall::SC_getsid:
  116. return current->sys$getsid((pid_t)arg1);
  117. case Syscall::SC_setpgid:
  118. return current->sys$setpgid((pid_t)arg1, (pid_t)arg2);
  119. case Syscall::SC_getpgid:
  120. return current->sys$getpgid((pid_t)arg1);
  121. case Syscall::SC_getpgrp:
  122. return current->sys$getpgrp();
  123. case Syscall::SC_tcgetpgrp:
  124. return current->sys$tcgetpgrp((int)arg1);
  125. case Syscall::SC_tcsetpgrp:
  126. return current->sys$tcsetpgrp((int)arg1, (pid_t)arg2);
  127. case Syscall::SC_fork:
  128. return current->sys$fork(regs);
  129. case Syscall::SC_execve:
  130. return current->sys$execve((const char*)arg1, (const char**)arg2, (const char**)arg3);
  131. case Syscall::SC_geteuid:
  132. return current->sys$geteuid();
  133. case Syscall::SC_getegid:
  134. return current->sys$getegid();
  135. case Syscall::SC_signal:
  136. return (dword)current->sys$signal((int)arg1, (Unix::sighandler_t)arg2);
  137. case Syscall::SC_isatty:
  138. return current->sys$isatty((int)arg1);
  139. case Syscall::SC_getdtablesize:
  140. return current->sys$getdtablesize();
  141. case Syscall::SC_dup:
  142. return current->sys$dup((int)arg1);
  143. case Syscall::SC_dup2:
  144. return current->sys$dup2((int)arg1, (int)arg2);
  145. case Syscall::SC_sigaction:
  146. return current->sys$sigaction((int)arg1, (const Unix::sigaction*)arg2, (Unix::sigaction*)arg3);
  147. case Syscall::SC_umask:
  148. return current->sys$umask((mode_t)arg1);
  149. case Syscall::SC_getgroups:
  150. return current->sys$getgroups((int)arg1, (gid_t*)arg2);
  151. case Syscall::SC_setgroups:
  152. return current->sys$setgroups((size_t)arg1, (const gid_t*)arg2);
  153. case Syscall::SC_sigreturn:
  154. current->sys$sigreturn();
  155. ASSERT_NOT_REACHED();
  156. return 0;
  157. default:
  158. kprintf("<%u> int0x80: Unknown function %x requested {%x, %x, %x}\n", current->pid(), function, arg1, arg2, arg3);
  159. break;
  160. }
  161. return 0;
  162. }
  163. }
  164. void syscall_entry(RegisterDump& regs)
  165. {
  166. DWORD function = regs.eax;
  167. DWORD arg1 = regs.edx;
  168. DWORD arg2 = regs.ecx;
  169. DWORD arg3 = regs.ebx;
  170. regs.eax = Syscall::handle(regs, function, arg1, arg2, arg3);
  171. }