Syscall.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #include "i386.h"
  2. #include "Task.h"
  3. #include "Syscall.h"
  4. #include "Console.h"
  5. #include <AK/Lock.h>
  6. extern "C" void syscall_entry();
  7. extern "C" void syscall_ISR();
  8. extern volatile RegisterDump* syscallRegDump;
  9. asm(
  10. ".globl syscall_ISR \n"
  11. ".globl syscallRegDump \n"
  12. "syscallRegDump: \n"
  13. ".long 0\n"
  14. "syscall_ISR:\n"
  15. " pusha\n"
  16. " pushw %ds\n"
  17. " pushw %es\n"
  18. " pushw %fs\n"
  19. " pushw %gs\n"
  20. " pushw %ss\n"
  21. " pushw %ss\n"
  22. " pushw %ss\n"
  23. " pushw %ss\n"
  24. " pushw %ss\n"
  25. " popw %ds\n"
  26. " popw %es\n"
  27. " popw %fs\n"
  28. " popw %gs\n"
  29. " mov %esp, syscallRegDump\n"
  30. " call syscall_entry\n"
  31. " popw %gs\n"
  32. " popw %gs\n"
  33. " popw %fs\n"
  34. " popw %es\n"
  35. " popw %ds\n"
  36. " popa\n"
  37. " iret\n"
  38. );
  39. namespace Syscall {
  40. static SpinLock* s_lock;
  41. void initialize()
  42. {
  43. s_lock = new SpinLock;
  44. registerUserCallableInterruptHandler(0x80, syscall_ISR);
  45. kprintf("syscall: int 0x80 handler installed\n");
  46. }
  47. DWORD handle(DWORD function, DWORD arg1, DWORD arg2, DWORD arg3)
  48. {
  49. Locker locker(*s_lock);
  50. switch (function) {
  51. case Syscall::Yield:
  52. yield();
  53. break;
  54. case Syscall::PutCharacter:
  55. Console::the().putChar(arg1 & 0xff);
  56. break;
  57. case Syscall::Sleep:
  58. return current->sys$sleep(arg1);
  59. case Syscall::PosixGettimeofday:
  60. return current->sys$gettimeofday((timeval*)arg1);
  61. case Syscall::Spawn:
  62. return current->sys$spawn((const char*)arg1);
  63. case Syscall::GetDirEntries:
  64. return current->sys$get_dir_entries((int)arg1, (void*)arg2, (size_t)arg3);
  65. case Syscall::PosixLstat:
  66. return current->sys$lstat((const char*)arg1, (void*)arg2);
  67. case Syscall::PosixGetcwd:
  68. return current->sys$getcwd((char*)arg1, (size_t)arg2);
  69. case Syscall::PosixOpen:
  70. //kprintf("syscall: open('%s', %u)\n", arg1, arg2);
  71. return current->sys$open((const char*)arg1, (size_t)arg2);
  72. case Syscall::PosixClose:
  73. //kprintf("syscall: close(%d)\n", arg1);
  74. return current->sys$close((int)arg1);
  75. case Syscall::PosixRead:
  76. //kprintf("syscall: read(%d, %p, %u)\n", arg1, arg2, arg3);
  77. return current->sys$read((int)arg1, (void*)arg2, (size_t)arg3);
  78. case Syscall::PosixSeek:
  79. // FIXME: This has the wrong signature, should be like lseek()
  80. kprintf("syscall: seek(%d, %d)\n", arg1, arg2);
  81. return current->sys$seek((int)arg1, (int)arg2);
  82. case Syscall::PosixKill:
  83. kprintf("syscall: kill(%d, %d)\n", arg1, arg2);
  84. return current->sys$kill((pid_t)arg1, (int)arg2);
  85. case Syscall::PosixGetuid:
  86. return current->sys$getuid();
  87. case Syscall::PosixGetgid:
  88. return current->sys$getgid();
  89. case Syscall::PosixGetpid:
  90. return current->sys$getpid();
  91. case Syscall::PosixWaitpid:
  92. return current->sys$waitpid((pid_t)arg1);
  93. case Syscall::PosixMmap:
  94. return (dword)current->sys$mmap((void*)arg1, (size_t)arg2);
  95. case Syscall::PosixMunmap:
  96. return current->sys$munmap((void*)arg1, (size_t)arg2);
  97. case Syscall::PosixGethostname:
  98. return current->sys$gethostname((char*)arg1, (size_t)arg2);
  99. case Syscall::PosixExit:
  100. cli();
  101. locker.unlock();
  102. current->sys$exit((int)arg1);
  103. ASSERT_NOT_REACHED();
  104. return 0;
  105. default:
  106. kprintf("int0x80: Unknown function %x requested {%x, %x, %x}\n", function, arg1, arg2, arg3);
  107. break;
  108. }
  109. return 0;
  110. }
  111. }
  112. void syscall_entry()
  113. {
  114. auto& regs = *syscallRegDump;
  115. DWORD function = regs.eax;
  116. DWORD arg1 = regs.edx;
  117. DWORD arg2 = regs.ecx;
  118. DWORD arg3 = regs.ebx;
  119. regs.eax = Syscall::handle(function, arg1, arg2, arg3);
  120. }