Syscall.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include "i386.h"
  2. #include "VGA.h"
  3. #include "Task.h"
  4. #include "Syscall.h"
  5. struct RegisterDump {
  6. WORD gs;
  7. WORD fs;
  8. WORD es;
  9. WORD ds;
  10. DWORD edi;
  11. DWORD esi;
  12. DWORD ebp;
  13. DWORD esp;
  14. DWORD ebx;
  15. DWORD edx;
  16. DWORD ecx;
  17. DWORD eax;
  18. DWORD eip;
  19. WORD cs;
  20. WORD __csPadding;
  21. DWORD eflags;
  22. } PACKED;
  23. extern "C" void syscall_entry();
  24. extern "C" void syscall_ISR();
  25. extern volatile RegisterDump* syscallRegDump;
  26. asm(
  27. ".globl syscall_ISR \n"
  28. ".globl syscallRegDump \n"
  29. "syscallRegDump: \n"
  30. ".long 0\n"
  31. "syscall_ISR:\n"
  32. " pusha\n"
  33. " pushw %ds\n"
  34. " pushw %es\n"
  35. " pushw %fs\n"
  36. " pushw %gs\n"
  37. " pushw %ss\n"
  38. " pushw %ss\n"
  39. " pushw %ss\n"
  40. " pushw %ss\n"
  41. " popw %ds\n"
  42. " popw %es\n"
  43. " popw %fs\n"
  44. " popw %gs\n"
  45. " mov %esp, syscallRegDump\n"
  46. " call syscall_entry\n"
  47. " popw %gs\n"
  48. " popw %fs\n"
  49. " popw %es\n"
  50. " popw %ds\n"
  51. " popa\n"
  52. " iret\n"
  53. );
  54. namespace Syscall {
  55. void initialize()
  56. {
  57. registerUserCallableInterruptHandler(0x80, syscall_ISR);
  58. kprintf("syscall: int 0x80 handler installed\n");
  59. }
  60. DWORD invoke(DWORD function)
  61. {
  62. DWORD result;
  63. asm("int $0x80":"=a"(result):"a"(function));
  64. return result;
  65. }
  66. DWORD invoke(DWORD function, DWORD arg1)
  67. {
  68. DWORD result;
  69. asm("int $0x80":"=a"(result):"a"(function),"d"(arg1));
  70. return result;
  71. }
  72. DWORD invoke(DWORD function, DWORD arg1, DWORD arg2)
  73. {
  74. DWORD result;
  75. asm("int $0x80":"=a"(result):"a"(function),"d"(arg1),"c"(arg2));
  76. return result;
  77. }
  78. DWORD invoke(DWORD function, DWORD arg1, DWORD arg2, DWORD arg3)
  79. {
  80. DWORD result;
  81. asm volatile("int $0x80":"=a"(result):"a"(function),"d"(arg1),"c"(arg2),"b"(arg3));
  82. return result;
  83. }
  84. DWORD handle(DWORD function, DWORD arg1, DWORD arg2, DWORD arg3)
  85. {
  86. switch (function) {
  87. case Syscall::Yield:
  88. yield();
  89. break;
  90. case 0x1235: // putch
  91. kprintf( "%c", arg1 & 0xFF );
  92. break;
  93. case Syscall::Sleep:
  94. //kprintf("syscall: sleep(%d)\n", arg1);
  95. current->sys$sleep(arg1);
  96. break;
  97. case Syscall::PosixOpen:
  98. Task::checkSanity("syscall");
  99. kprintf("syscall: open('%s', %u)\n", arg1, arg2);
  100. return current->sys$open((const char*)arg1, (size_t)arg2);
  101. case Syscall::PosixClose:
  102. kprintf("syscall: close(%d)\n", arg1);
  103. return current->sys$close((int)arg1);
  104. case Syscall::PosixRead:
  105. kprintf("syscall: read(%d, %p, %u)\n", arg1, arg2, arg3);
  106. return current->sys$read((int)arg1, (void*)arg2, (size_t)arg3);
  107. case Syscall::PosixSeek:
  108. // FIXME: This has the wrong signature, should be like lseek()
  109. kprintf("syscall: seek(%d, %p, %u)\n", arg1, arg2, arg3);
  110. return current->sys$read((int)arg1, (void*)arg2, (size_t)arg3);
  111. case Syscall::PosixKill:
  112. kprintf("syscall: kill(%d, %d)\n", arg1, arg2);
  113. return current->sys$kill((pid_t)arg1, (int)arg2);
  114. case Syscall::PosixGetuid:
  115. return current->sys$getuid();
  116. default:
  117. kprintf("int0x80: Unknown function %x requested {%x, %x, %x}\n", function, arg1, arg2, arg3);
  118. break;
  119. }
  120. return 0;
  121. }
  122. }
  123. void syscall_entry()
  124. {
  125. auto& regs = *syscallRegDump;
  126. DWORD function = regs.eax;
  127. DWORD arg1 = regs.edx;
  128. DWORD arg2 = regs.ecx;
  129. DWORD arg3 = regs.ebx;
  130. regs.eax = Syscall::handle(function, arg1, arg2, arg3);
  131. }