Syscall.cpp 4.3 KB

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