sortix-mirror/sortix/syscall.cpp

157 lines
3.5 KiB
C++
Raw Normal View History

2011-08-05 12:25:00 +00: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 <sortix/kernel/platform.h>
#include <libmaxsi/error.h>
2011-08-05 12:25:00 +00:00
#include "syscall.h"
#include <sortix/syscallnum.h>
#include <sortix/kernel/panic.h>
#include "process.h"
#include "thread.h"
#include "scheduler.h"
using namespace Maxsi;
2011-08-05 12:25:00 +00:00
namespace Sortix
{
namespace Syscall
{
extern "C"
2011-08-05 12:25:00 +00:00
{
CPU::SyscallRegisters* syscall_state_ptr;
unsigned system_was_incomplete;
size_t SYSCALL_MAX;
volatile void* syscall_list[SYSCALL_MAX_NUM];
2011-08-05 12:25:00 +00:00
}
int BadSyscall()
2011-08-05 12:25:00 +00:00
{
// TODO: Send signal, set errno, or crash/abort process?
return -1;
}
2011-08-05 12:25:00 +00:00
void Init()
{
SYSCALL_MAX = SYSCALL_MAX_NUM;
for ( size_t i = 0; i < SYSCALL_MAX_NUM; i++ )
{
syscall_list[i] = (void*) BadSyscall;
}
2011-08-05 12:25:00 +00:00
}
void Register(size_t index, void* funcptr)
2011-08-05 12:25:00 +00:00
{
if ( SYSCALL_MAX_NUM <= index )
{
PanicF("attempted to register syscall 0x%p to index %zu, but "
"SYSCALL_MAX_NYN = %zu", funcptr, index, SYSCALL_MAX_NUM);
}
2011-08-05 12:25:00 +00:00
syscall_list[index] = funcptr;
}
2011-08-05 12:25:00 +00:00
void Incomplete()
{
Thread* thread = CurrentThread();
system_was_incomplete = 1;
2011-08-05 12:25:00 +00:00
CPU::InterruptRegisters* regs = InterruptRegs();
thread->SaveRegisters(regs);
Scheduler::SetThreadState(thread, Thread::State::BLOCKING);
Implemented the fork() system call and what it needed to work properly. This commit got completely out of control. Added the fork(), getpid(), getppid(), sleep(), usleep() system calls, and aliases in the Maxsi:: namespace. Fixed a bug where zero-byte allocation would fail. Worked on the DescriptorTable class which now works and can fork. Got rid of some massive print-registers statements and replaced them with the portable InterruptRegisters::LogRegisters() function. Removed the SysExecuteOld function and replaced it with Process::Execute(). Rewrote the boot sequence in kernel.cpp such that it now loads the system idle process 'idle' as PID 0, and the initization process 'init' as PID 1. Rewrote the SIGINT hack. Processes now maintain a family-tree structure and keep track of their threads. PIDs are now allocated using a simple hack. Virtual memory per-process can now be allocated using a simple hack. Processes can now be forked. Fixed the Process::Execute function such that it now resets the stack pointer to where the stack actually is - not just a magic value. Removed the old and ugly Process::_endcodesection hack. Rewrote the scheduler into a much cleaner and faster version. Debug code is now moved to designated functions. The noop kernel-thread has been replaced by a simple user-space infinite-loop program 'idle'. The Thread class has been seperated from the Scheduler except in Scheduler- related code. Thread::{Save,Load}Registers has been improved and has been moved to $(CPU)/thread.cpp. Threads can now be forked. A new CreateThread function creates threads properly and portably. Added a MicrosecondsSinceBoot() function. Fixed a crucial bug in MemoryManagement::Fork(). Added an 'idle' user-space program that is a noop infinite loop, which is used by the scheduler when there is nothing to do. Rewrote the 'init' program such that it now forks off a shell, instead of becoming the shell. Added the $$ (current PID) and $PPID (parent PPID) variables to the shell.
2011-09-21 18:52:29 +00:00
Scheduler::Switch(regs);
}
2011-08-05 12:25:00 +00:00
void Yield()
{
Panic("Syscall::Yield() is not implemented because it caused "
"instability and other issues.");
}
void AsIs()
{
system_was_incomplete = 1;
}
void ScheduleResumption(Thread* thread)
{
Scheduler::SetThreadState(thread, Thread::State::RUNNABLE);
}
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;
}
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;
Error::Set(0);
size_t result = resume_syscall(scfunc, scsize, scstate);
bool incomplete = (system_was_incomplete);
system_was_incomplete = 1;
if ( !incomplete )
{
syscall_state_ptr->result = result;
update_userspace_errno();
return;
}
Incomplete();
}
CPU::InterruptRegisters* InterruptRegs()
{
return (CPU::InterruptRegisters*) syscall_state_ptr;
}
2011-08-05 12:25:00 +00:00
CPU::SyscallRegisters* SyscallRegs()
{
return syscall_state_ptr;
2011-08-05 12:25:00 +00:00
}
}
}