2018-10-16 06:01:38 -03:00
|
|
|
#include "i386.h"
|
2018-11-01 09:15:46 -03:00
|
|
|
#include "Process.h"
|
2018-10-16 06:01:38 -03:00
|
|
|
#include "Syscall.h"
|
2018-10-23 10:19:02 -03:00
|
|
|
#include "Console.h"
|
2018-11-07 19:15:02 -02:00
|
|
|
#include "Scheduler.h"
|
2018-10-16 06:01:38 -03:00
|
|
|
|
2018-10-30 20:23:46 -03:00
|
|
|
extern "C" void syscall_entry(RegisterDump&);
|
2018-10-16 06:01:38 -03:00
|
|
|
extern "C" void syscall_ISR();
|
|
|
|
|
extern volatile RegisterDump* syscallRegDump;
|
|
|
|
|
|
2018-11-09 09:20:44 -02:00
|
|
|
asm(
|
2018-10-16 06:01:38 -03:00
|
|
|
".globl syscall_ISR \n"
|
|
|
|
|
"syscall_ISR:\n"
|
|
|
|
|
" pusha\n"
|
|
|
|
|
" pushw %ds\n"
|
|
|
|
|
" pushw %es\n"
|
|
|
|
|
" pushw %fs\n"
|
|
|
|
|
" pushw %gs\n"
|
|
|
|
|
" pushw %ss\n"
|
|
|
|
|
" pushw %ss\n"
|
|
|
|
|
" pushw %ss\n"
|
|
|
|
|
" pushw %ss\n"
|
2018-10-23 05:12:50 -03:00
|
|
|
" pushw %ss\n"
|
2018-10-16 06:01:38 -03:00
|
|
|
" popw %ds\n"
|
|
|
|
|
" popw %es\n"
|
|
|
|
|
" popw %fs\n"
|
|
|
|
|
" popw %gs\n"
|
2018-10-30 20:23:46 -03:00
|
|
|
" mov %esp, %eax\n"
|
2018-10-16 06:01:38 -03:00
|
|
|
" call syscall_entry\n"
|
|
|
|
|
" popw %gs\n"
|
2018-10-23 05:12:50 -03:00
|
|
|
" popw %gs\n"
|
2018-10-16 06:01:38 -03:00
|
|
|
" popw %fs\n"
|
|
|
|
|
" popw %es\n"
|
|
|
|
|
" popw %ds\n"
|
|
|
|
|
" popa\n"
|
|
|
|
|
" iret\n"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
namespace Syscall {
|
|
|
|
|
|
|
|
|
|
void initialize()
|
|
|
|
|
{
|
2018-12-02 21:39:25 -02:00
|
|
|
register_user_callable_interrupt_handler(0x80, syscall_ISR);
|
2018-10-16 06:01:38 -03:00
|
|
|
kprintf("syscall: int 0x80 handler installed\n");
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-16 21:11:08 -02:00
|
|
|
static dword handle(RegisterDump& regs, dword function, dword arg1, dword arg2, dword arg3)
|
2018-10-16 06:01:38 -03:00
|
|
|
{
|
2018-10-29 17:54:11 -03:00
|
|
|
ASSERT_INTERRUPTS_ENABLED();
|
2018-10-16 06:01:38 -03:00
|
|
|
switch (function) {
|
2018-11-06 10:23:22 -02:00
|
|
|
case Syscall::SC_yield:
|
2018-11-07 19:15:02 -02:00
|
|
|
Scheduler::yield();
|
2018-10-16 06:01:38 -03:00
|
|
|
break;
|
2018-11-06 10:23:22 -02:00
|
|
|
case Syscall::SC_putch:
|
2018-12-02 22:38:22 -02:00
|
|
|
Console::the().put_char(arg1 & 0xff);
|
2018-10-16 06:01:38 -03:00
|
|
|
break;
|
2018-11-06 10:23:22 -02:00
|
|
|
case Syscall::SC_sleep:
|
2018-11-07 18:19:47 -02:00
|
|
|
return current->sys$sleep((unsigned)arg1);
|
2018-11-06 10:23:22 -02:00
|
|
|
case Syscall::SC_gettimeofday:
|
2018-10-25 12:29:49 -03:00
|
|
|
return current->sys$gettimeofday((timeval*)arg1);
|
2018-11-06 10:23:22 -02:00
|
|
|
case Syscall::SC_get_dir_entries:
|
2018-10-24 07:43:52 -03:00
|
|
|
return current->sys$get_dir_entries((int)arg1, (void*)arg2, (size_t)arg3);
|
2018-11-06 10:23:22 -02:00
|
|
|
case Syscall::SC_lstat:
|
2018-10-26 19:29:31 -03:00
|
|
|
return current->sys$lstat((const char*)arg1, (Unix::stat*)arg2);
|
2018-11-06 10:23:22 -02:00
|
|
|
case Syscall::SC_stat:
|
2018-10-31 06:14:56 -03:00
|
|
|
return current->sys$stat((const char*)arg1, (Unix::stat*)arg2);
|
2018-11-06 10:23:22 -02:00
|
|
|
case Syscall::SC_getcwd:
|
2018-10-24 09:28:22 -03:00
|
|
|
return current->sys$getcwd((char*)arg1, (size_t)arg2);
|
2018-11-06 10:23:22 -02:00
|
|
|
case Syscall::SC_open:
|
2018-10-29 17:54:11 -03:00
|
|
|
return current->sys$open((const char*)arg1, (int)arg2);
|
2018-11-06 10:23:22 -02:00
|
|
|
case Syscall::SC_write:
|
2018-10-30 11:33:37 -03:00
|
|
|
return current->sys$write((int)arg1, (const void*)arg2, (size_t)arg3);
|
2018-11-06 10:23:22 -02:00
|
|
|
case Syscall::SC_close:
|
2018-10-23 05:12:50 -03:00
|
|
|
//kprintf("syscall: close(%d)\n", arg1);
|
2018-10-16 06:01:38 -03:00
|
|
|
return current->sys$close((int)arg1);
|
2018-11-06 10:23:22 -02:00
|
|
|
case Syscall::SC_read:
|
2018-10-23 05:12:50 -03:00
|
|
|
//kprintf("syscall: read(%d, %p, %u)\n", arg1, arg2, arg3);
|
2018-10-16 06:01:38 -03:00
|
|
|
return current->sys$read((int)arg1, (void*)arg2, (size_t)arg3);
|
2018-11-06 10:23:22 -02:00
|
|
|
case Syscall::SC_lseek:
|
2018-10-31 13:50:43 -03:00
|
|
|
return current->sys$lseek((int)arg1, (off_t)arg2, (int)arg3);
|
2018-11-06 10:23:22 -02:00
|
|
|
case Syscall::SC_kill:
|
2018-10-16 06:01:38 -03:00
|
|
|
return current->sys$kill((pid_t)arg1, (int)arg2);
|
2018-11-06 10:23:22 -02:00
|
|
|
case Syscall::SC_getuid:
|
2018-10-16 06:01:38 -03:00
|
|
|
return current->sys$getuid();
|
2018-11-06 10:23:22 -02:00
|
|
|
case Syscall::SC_getgid:
|
2018-10-22 08:55:11 -03:00
|
|
|
return current->sys$getgid();
|
2018-11-06 10:23:22 -02:00
|
|
|
case Syscall::SC_getpid:
|
2018-10-22 08:55:11 -03:00
|
|
|
return current->sys$getpid();
|
2018-11-06 10:33:06 -02:00
|
|
|
case Syscall::SC_getppid:
|
|
|
|
|
return current->sys$getppid();
|
2018-11-06 10:23:22 -02:00
|
|
|
case Syscall::SC_waitpid:
|
2018-10-26 20:24:22 -03:00
|
|
|
return current->sys$waitpid((pid_t)arg1, (int*)arg2, (int)arg3);
|
2018-11-06 10:23:22 -02:00
|
|
|
case Syscall::SC_mmap:
|
2018-11-08 08:37:01 -02:00
|
|
|
return (dword)current->sys$mmap((const SC_mmap_params*)arg1);
|
2018-11-06 10:23:22 -02:00
|
|
|
case Syscall::SC_munmap:
|
2018-10-24 04:48:24 -03:00
|
|
|
return current->sys$munmap((void*)arg1, (size_t)arg2);
|
2018-11-06 10:23:22 -02:00
|
|
|
case Syscall::SC_gethostname:
|
2018-10-26 04:54:29 -03:00
|
|
|
return current->sys$gethostname((char*)arg1, (size_t)arg2);
|
2018-11-06 10:23:22 -02:00
|
|
|
case Syscall::SC_exit:
|
2018-10-23 10:19:02 -03:00
|
|
|
cli();
|
2018-10-22 06:43:55 -03:00
|
|
|
current->sys$exit((int)arg1);
|
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
|
return 0;
|
2018-11-06 10:23:22 -02:00
|
|
|
case Syscall::SC_get_arguments:
|
2018-10-26 06:16:56 -03:00
|
|
|
return current->sys$get_arguments((int*)arg1, (char***)arg2);
|
2018-11-06 10:23:22 -02:00
|
|
|
case Syscall::SC_get_environment:
|
2018-10-31 13:50:43 -03:00
|
|
|
return current->sys$get_environment((char***)arg1);
|
2018-11-06 10:23:22 -02:00
|
|
|
case Syscall::SC_chdir:
|
2018-10-26 09:24:11 -03:00
|
|
|
return current->sys$chdir((const char*)arg1);
|
2018-11-06 10:23:22 -02:00
|
|
|
case Syscall::SC_uname:
|
2018-10-26 09:56:21 -03:00
|
|
|
return current->sys$uname((utsname*)arg1);
|
2018-11-06 10:23:22 -02:00
|
|
|
case Syscall::SC_set_mmap_name:
|
2018-10-28 05:57:22 -03:00
|
|
|
return current->sys$set_mmap_name((void*)arg1, (size_t)arg2, (const char*)arg3);
|
2018-11-06 10:23:22 -02:00
|
|
|
case Syscall::SC_readlink:
|
2018-10-28 10:11:51 -03:00
|
|
|
return current->sys$readlink((const char*)arg1, (char*)arg2, (size_t)arg3);
|
2018-11-06 10:23:22 -02:00
|
|
|
case Syscall::SC_ttyname_r:
|
2018-10-30 18:03:02 -03:00
|
|
|
return current->sys$ttyname_r((int)arg1, (char*)arg2, (size_t)arg3);
|
2018-11-06 10:23:22 -02:00
|
|
|
case Syscall::SC_setsid:
|
2018-11-02 08:56:51 -03:00
|
|
|
return current->sys$setsid();
|
2018-11-06 10:23:22 -02:00
|
|
|
case Syscall::SC_getsid:
|
2018-11-02 08:56:51 -03:00
|
|
|
return current->sys$getsid((pid_t)arg1);
|
2018-11-06 10:23:22 -02:00
|
|
|
case Syscall::SC_setpgid:
|
2018-11-02 08:56:51 -03:00
|
|
|
return current->sys$setpgid((pid_t)arg1, (pid_t)arg2);
|
2018-11-06 10:23:22 -02:00
|
|
|
case Syscall::SC_getpgid:
|
2018-11-02 08:56:51 -03:00
|
|
|
return current->sys$getpgid((pid_t)arg1);
|
2018-11-06 10:23:22 -02:00
|
|
|
case Syscall::SC_getpgrp:
|
2018-11-02 08:56:51 -03:00
|
|
|
return current->sys$getpgrp();
|
2018-11-06 10:23:22 -02:00
|
|
|
case Syscall::SC_fork:
|
2018-11-02 16:41:58 -03:00
|
|
|
return current->sys$fork(regs);
|
2018-11-06 10:23:22 -02:00
|
|
|
case Syscall::SC_execve:
|
2018-11-02 21:49:40 -03:00
|
|
|
return current->sys$execve((const char*)arg1, (const char**)arg2, (const char**)arg3);
|
2018-11-06 10:23:22 -02:00
|
|
|
case Syscall::SC_geteuid:
|
2018-11-05 12:04:19 -02:00
|
|
|
return current->sys$geteuid();
|
2018-11-06 10:23:22 -02:00
|
|
|
case Syscall::SC_getegid:
|
2018-11-05 12:04:19 -02:00
|
|
|
return current->sys$getegid();
|
2018-11-06 10:23:22 -02:00
|
|
|
case Syscall::SC_isatty:
|
2018-11-05 15:16:00 -02:00
|
|
|
return current->sys$isatty((int)arg1);
|
2018-11-06 10:23:22 -02:00
|
|
|
case Syscall::SC_getdtablesize:
|
2018-11-05 16:01:22 -02:00
|
|
|
return current->sys$getdtablesize();
|
2018-11-06 10:23:22 -02:00
|
|
|
case Syscall::SC_dup:
|
2018-11-05 16:01:22 -02:00
|
|
|
return current->sys$dup((int)arg1);
|
2018-11-06 10:23:22 -02:00
|
|
|
case Syscall::SC_dup2:
|
2018-11-05 16:01:22 -02:00
|
|
|
return current->sys$dup2((int)arg1, (int)arg2);
|
2018-11-06 10:23:22 -02:00
|
|
|
case Syscall::SC_sigaction:
|
2018-11-06 07:46:40 -02:00
|
|
|
return current->sys$sigaction((int)arg1, (const Unix::sigaction*)arg2, (Unix::sigaction*)arg3);
|
2018-11-06 10:40:23 -02:00
|
|
|
case Syscall::SC_umask:
|
|
|
|
|
return current->sys$umask((mode_t)arg1);
|
2018-11-06 22:38:51 -02:00
|
|
|
case Syscall::SC_getgroups:
|
|
|
|
|
return current->sys$getgroups((int)arg1, (gid_t*)arg2);
|
|
|
|
|
case Syscall::SC_setgroups:
|
|
|
|
|
return current->sys$setgroups((size_t)arg1, (const gid_t*)arg2);
|
2018-11-07 18:19:47 -02:00
|
|
|
case Syscall::SC_sigreturn:
|
|
|
|
|
current->sys$sigreturn();
|
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
|
return 0;
|
2018-11-10 20:29:07 -02:00
|
|
|
case Syscall::SC_sigprocmask:
|
|
|
|
|
return current->sys$sigprocmask((int)arg1, (const Unix::sigset_t*)arg2, (Unix::sigset_t*)arg3);
|
2018-11-10 21:20:53 -02:00
|
|
|
case Syscall::SC_pipe:
|
|
|
|
|
return current->sys$pipe((int*)arg1);
|
|
|
|
|
case Syscall::SC_killpg:
|
|
|
|
|
return current->sys$killpg((int)arg1, (int)arg2);
|
|
|
|
|
case Syscall::SC_setuid:
|
|
|
|
|
return current->sys$setuid((uid_t)arg1);
|
|
|
|
|
case Syscall::SC_setgid:
|
|
|
|
|
return current->sys$setgid((gid_t)arg1);
|
|
|
|
|
case Syscall::SC_alarm:
|
|
|
|
|
return current->sys$alarm((unsigned)arg1);
|
|
|
|
|
case Syscall::SC_access:
|
|
|
|
|
return current->sys$access((const char*)arg1, (int)arg2);
|
2018-11-11 07:38:33 -02:00
|
|
|
case Syscall::SC_fcntl:
|
|
|
|
|
return current->sys$fcntl((int)arg1, (int)arg2, (dword)arg3);
|
2018-11-16 10:11:21 -02:00
|
|
|
case Syscall::SC_ioctl:
|
|
|
|
|
return current->sys$ioctl((int)arg1, (unsigned)arg2, (unsigned)arg3);
|
2018-11-16 21:52:29 -02:00
|
|
|
case Syscall::SC_fstat:
|
|
|
|
|
return current->sys$fstat((int)arg1, (Unix::stat*)arg2);
|
2018-11-18 11:57:41 -02:00
|
|
|
case Syscall::SC_mkdir:
|
|
|
|
|
return current->sys$mkdir((const char*)arg1, (mode_t)arg2);
|
2018-12-02 22:12:26 -02:00
|
|
|
case Syscall::SC_times:
|
|
|
|
|
return current->sys$times((Unix::tms*)arg1);
|
2018-10-16 06:01:38 -03:00
|
|
|
default:
|
2018-11-16 21:52:29 -02:00
|
|
|
kprintf("<%u> int0x80: Unknown function %u requested {%x, %x, %x}\n", current->pid(), function, arg1, arg2, arg3);
|
2018-10-16 06:01:38 -03:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-30 20:23:46 -03:00
|
|
|
void syscall_entry(RegisterDump& regs)
|
2018-10-16 06:01:38 -03:00
|
|
|
{
|
2018-11-16 21:11:08 -02:00
|
|
|
dword function = regs.eax;
|
|
|
|
|
dword arg1 = regs.edx;
|
|
|
|
|
dword arg2 = regs.ecx;
|
|
|
|
|
dword arg3 = regs.ebx;
|
2018-11-02 16:41:58 -03:00
|
|
|
regs.eax = Syscall::handle(regs, function, arg1, arg2, arg3);
|
2018-10-16 06:01:38 -03:00
|
|
|
}
|
2018-11-02 16:41:58 -03:00
|
|
|
|