Syscall.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include "i386.h"
  2. #include "VGA.h"
  3. #include "Task.h"
  4. #include "Syscall.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. " popw %ds\n"
  24. " popw %es\n"
  25. " popw %fs\n"
  26. " popw %gs\n"
  27. " mov %esp, syscallRegDump\n"
  28. " call syscall_entry\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. DWORD handle(DWORD function, DWORD arg1, DWORD arg2, DWORD arg3)
  43. {
  44. switch (function) {
  45. case Syscall::Yield:
  46. yield();
  47. break;
  48. case Syscall::PutCharacter:
  49. kprintf("%c", arg1 & 0xff);
  50. break;
  51. case Syscall::Sleep:
  52. //kprintf("syscall: sleep(%d)\n", arg1);
  53. current->sys$sleep(arg1);
  54. break;
  55. case Syscall::PosixOpen:
  56. Task::checkSanity("syscall");
  57. kprintf("syscall: open('%s', %u)\n", arg1, arg2);
  58. return current->sys$open((const char*)arg1, (size_t)arg2);
  59. case Syscall::PosixClose:
  60. kprintf("syscall: close(%d)\n", arg1);
  61. return current->sys$close((int)arg1);
  62. case Syscall::PosixRead:
  63. kprintf("syscall: read(%d, %p, %u)\n", arg1, arg2, arg3);
  64. return current->sys$read((int)arg1, (void*)arg2, (size_t)arg3);
  65. case Syscall::PosixSeek:
  66. // FIXME: This has the wrong signature, should be like lseek()
  67. kprintf("syscall: seek(%d, %p, %u)\n", arg1, arg2, arg3);
  68. return current->sys$read((int)arg1, (void*)arg2, (size_t)arg3);
  69. case Syscall::PosixKill:
  70. kprintf("syscall: kill(%d, %d)\n", arg1, arg2);
  71. return current->sys$kill((pid_t)arg1, (int)arg2);
  72. case Syscall::PosixGetuid:
  73. return current->sys$getuid();
  74. case Syscall::PosixExit:
  75. current->sys$exit((int)arg1);
  76. ASSERT_NOT_REACHED();
  77. return 0;
  78. default:
  79. kprintf("int0x80: Unknown function %x requested {%x, %x, %x}\n", function, arg1, arg2, arg3);
  80. break;
  81. }
  82. return 0;
  83. }
  84. }
  85. void syscall_entry()
  86. {
  87. auto& regs = *syscallRegDump;
  88. DWORD function = regs.eax;
  89. DWORD arg1 = regs.edx;
  90. DWORD arg2 = regs.ecx;
  91. DWORD arg3 = regs.ebx;
  92. regs.eax = Syscall::handle(function, arg1, arg2, arg3);
  93. }