2011-08-05 08:25:00 -04:00
|
|
|
/******************************************************************************
|
|
|
|
|
|
|
|
COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011.
|
|
|
|
|
|
|
|
This file is part of Sortix.
|
|
|
|
|
|
|
|
Sortix is free software: you can redistribute it and/or modify it under the
|
|
|
|
terms of the GNU General Public License as published by the Free Software
|
|
|
|
Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
version.
|
|
|
|
|
|
|
|
Sortix is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
with Sortix. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
syscall.h
|
|
|
|
Handles system calls from userspace safely.
|
|
|
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#include "platform.h"
|
2011-11-22 11:26:47 -05:00
|
|
|
#include <libmaxsi/error.h>
|
2011-08-05 08:25:00 -04:00
|
|
|
#include "syscall.h"
|
2011-10-26 18:05:20 -04:00
|
|
|
#include "syscallnum.h"
|
2011-08-05 08:25:00 -04:00
|
|
|
#include "panic.h"
|
2011-11-22 11:26:47 -05:00
|
|
|
#include "process.h"
|
2011-10-26 18:05:20 -04:00
|
|
|
#include "thread.h"
|
|
|
|
#include "scheduler.h"
|
|
|
|
|
2011-11-22 11:26:47 -05:00
|
|
|
using namespace Maxsi;
|
2011-08-05 08:25:00 -04:00
|
|
|
|
|
|
|
namespace Sortix
|
|
|
|
{
|
|
|
|
namespace Syscall
|
|
|
|
{
|
2011-10-26 18:05:20 -04:00
|
|
|
extern "C"
|
2011-08-05 08:25:00 -04:00
|
|
|
{
|
2011-10-26 18:05:20 -04:00
|
|
|
CPU::SyscallRegisters* syscall_state_ptr;
|
2011-11-06 17:06:32 -05:00
|
|
|
unsigned system_was_incomplete;
|
2011-10-26 18:05:20 -04:00
|
|
|
size_t SYSCALL_MAX = SYSCALL_MAX_NUM;
|
|
|
|
void* syscall_list[SYSCALL_MAX_NUM];
|
2011-08-05 08:25:00 -04:00
|
|
|
}
|
|
|
|
|
2011-10-26 18:05:20 -04:00
|
|
|
int BadSyscall()
|
2011-08-05 08:25:00 -04:00
|
|
|
{
|
2011-10-26 18:05:20 -04:00
|
|
|
// TODO: Send signal, set errno, or crash/abort process?
|
|
|
|
return -1;
|
|
|
|
}
|
2011-08-05 08:25:00 -04:00
|
|
|
|
|
|
|
void Init()
|
|
|
|
{
|
2011-10-26 18:05:20 -04:00
|
|
|
for ( size_t i = 0; i < SYSCALL_MAX; i++ )
|
|
|
|
{
|
|
|
|
syscall_list[i] = (void*) BadSyscall;
|
|
|
|
}
|
2011-08-05 08:25:00 -04:00
|
|
|
}
|
|
|
|
|
2011-10-26 18:05:20 -04:00
|
|
|
void Register(size_t index, void* funcptr)
|
2011-08-05 08:25:00 -04:00
|
|
|
{
|
2011-10-26 18:05:20 -04:00
|
|
|
if ( SYSCALL_MAX <= index )
|
|
|
|
{
|
|
|
|
PanicF("attempted to register syscall 0x%p to index %zu, but "
|
|
|
|
"SYSCALL_MAX = %zu", funcptr, index, SYSCALL_MAX);
|
|
|
|
}
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2011-10-26 18:05:20 -04:00
|
|
|
syscall_list[index] = funcptr;
|
|
|
|
}
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2011-10-26 18:05:20 -04:00
|
|
|
void Incomplete()
|
|
|
|
{
|
2011-11-06 17:06:32 -05:00
|
|
|
Thread* thread = CurrentThread();
|
|
|
|
|
2011-10-26 18:05:20 -04:00
|
|
|
system_was_incomplete = 1;
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2011-10-26 18:05:20 -04:00
|
|
|
CPU::InterruptRegisters* regs = InterruptRegs();
|
2011-11-06 17:06:32 -05:00
|
|
|
thread->SaveRegisters(regs);
|
|
|
|
Scheduler::SetThreadState(thread, Thread::State::BLOCKING);
|
2011-09-21 14:52:29 -04:00
|
|
|
Scheduler::Switch(regs);
|
2011-10-26 18:05:20 -04:00
|
|
|
}
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2011-11-05 15:03:17 -04:00
|
|
|
void AsIs()
|
|
|
|
{
|
|
|
|
system_was_incomplete = 1;
|
|
|
|
}
|
|
|
|
|
2011-11-06 17:06:32 -05:00
|
|
|
void ScheduleResumption(Thread* thread)
|
|
|
|
{
|
|
|
|
Scheduler::SetThreadState(thread, Thread::State::RUNNABLE);
|
|
|
|
}
|
|
|
|
|
2011-11-22 11:26:47 -05:00
|
|
|
extern "C" void update_userspace_errno()
|
|
|
|
{
|
|
|
|
int error = Error::Last();
|
|
|
|
if ( !error ) { return; }
|
|
|
|
Process* process = CurrentProcess();
|
|
|
|
if ( !process->errno ) { return; }
|
|
|
|
// TODO: Validate that process->errno is in userspace memory!
|
|
|
|
*process->errno = error;
|
|
|
|
}
|
|
|
|
|
2011-11-06 17:06:32 -05:00
|
|
|
extern "C" size_t resume_syscall(void* scfunc, size_t scsize, size_t* scstate);
|
|
|
|
|
|
|
|
void Resume(CPU::InterruptRegisters* regs)
|
|
|
|
{
|
|
|
|
Thread* thread = CurrentThread();
|
|
|
|
|
|
|
|
syscall_state_ptr = (CPU::SyscallRegisters*) regs;
|
|
|
|
|
|
|
|
ASSERT(thread->scfunc);
|
|
|
|
|
|
|
|
size_t* scstate = thread->scstate;
|
|
|
|
size_t scsize = thread->scsize;
|
|
|
|
void* scfunc = thread->scfunc;
|
|
|
|
|
|
|
|
system_was_incomplete = 0;
|
2011-11-22 11:26:47 -05:00
|
|
|
Error::Set(0);
|
2011-11-06 17:06:32 -05:00
|
|
|
|
|
|
|
size_t result = resume_syscall(scfunc, scsize, scstate);
|
|
|
|
|
|
|
|
bool incomplete = (system_was_incomplete);
|
|
|
|
|
|
|
|
system_was_incomplete = 1;
|
|
|
|
|
|
|
|
if ( !incomplete )
|
|
|
|
{
|
|
|
|
syscall_state_ptr->result = result;
|
2011-11-22 11:26:47 -05:00
|
|
|
update_userspace_errno();
|
2011-11-06 17:06:32 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Incomplete();
|
|
|
|
}
|
|
|
|
|
2011-10-26 18:05:20 -04:00
|
|
|
CPU::InterruptRegisters* InterruptRegs()
|
|
|
|
{
|
|
|
|
return (CPU::InterruptRegisters*) syscall_state_ptr;
|
|
|
|
}
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2011-10-26 18:05:20 -04:00
|
|
|
CPU::SyscallRegisters* SyscallRegs()
|
|
|
|
{
|
|
|
|
return syscall_state_ptr;
|
2011-08-05 08:25:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|