Syscall.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. //kprintf("syscall: sleep(%d)\n", arg1);
  59. current->sys$sleep(arg1);
  60. break;
  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::PosixOpen:
  66. //kprintf("syscall: open('%s', %u)\n", arg1, arg2);
  67. return current->sys$open((const char*)arg1, (size_t)arg2);
  68. case Syscall::PosixClose:
  69. //kprintf("syscall: close(%d)\n", arg1);
  70. return current->sys$close((int)arg1);
  71. case Syscall::PosixRead:
  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::PosixSeek:
  75. // FIXME: This has the wrong signature, should be like lseek()
  76. kprintf("syscall: seek(%d, %d)\n", arg1, arg2);
  77. return current->sys$seek((int)arg1, (int)arg2);
  78. case Syscall::PosixKill:
  79. kprintf("syscall: kill(%d, %d)\n", arg1, arg2);
  80. return current->sys$kill((pid_t)arg1, (int)arg2);
  81. case Syscall::PosixGetuid:
  82. return current->sys$getuid();
  83. case Syscall::PosixGetgid:
  84. return current->sys$getgid();
  85. case Syscall::PosixGetpid:
  86. return current->sys$getpid();
  87. case Syscall::PosixWaitpid:
  88. return current->sys$waitpid((pid_t)arg1);
  89. case Syscall::PosixMmap:
  90. return (dword)current->sys$mmap((void*)arg1, (size_t)arg2);
  91. case Syscall::PosixMunmap:
  92. return current->sys$munmap((void*)arg1, (size_t)arg2);
  93. case Syscall::PosixExit:
  94. cli();
  95. locker.unlock();
  96. current->sys$exit((int)arg1);
  97. ASSERT_NOT_REACHED();
  98. return 0;
  99. default:
  100. kprintf("int0x80: Unknown function %x requested {%x, %x, %x}\n", function, arg1, arg2, arg3);
  101. break;
  102. }
  103. return 0;
  104. }
  105. }
  106. void syscall_entry()
  107. {
  108. auto& regs = *syscallRegDump;
  109. DWORD function = regs.eax;
  110. DWORD arg1 = regs.edx;
  111. DWORD arg2 = regs.ecx;
  112. DWORD arg3 = regs.ebx;
  113. regs.eax = Syscall::handle(function, arg1, arg2, arg3);
  114. }