2011-09-21 14:52:29 -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/>.
|
|
|
|
|
|
|
|
thread.cpp
|
|
|
|
Describes a thread belonging to a process.
|
|
|
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#include "platform.h"
|
2011-09-15 16:38:40 -04:00
|
|
|
#include <libmaxsi/error.h>
|
2011-09-21 14:52:29 -04:00
|
|
|
#include <libmaxsi/memory.h>
|
2012-02-10 07:27:11 -05:00
|
|
|
#include "event.h"
|
2011-09-21 14:52:29 -04:00
|
|
|
#include "process.h"
|
|
|
|
#include "thread.h"
|
|
|
|
#include "scheduler.h"
|
|
|
|
#include "memorymanagement.h"
|
|
|
|
#include "time.h"
|
2011-09-15 16:38:40 -04:00
|
|
|
#include "syscall.h"
|
|
|
|
|
|
|
|
using namespace Maxsi;
|
2011-09-21 14:52:29 -04:00
|
|
|
|
|
|
|
namespace Sortix
|
|
|
|
{
|
|
|
|
Thread::Thread()
|
|
|
|
{
|
|
|
|
id = 0; // TODO: Make a thread id.
|
|
|
|
process = NULL;
|
|
|
|
prevsibling = NULL;
|
|
|
|
nextsibling = NULL;
|
2012-01-18 11:10:44 -05:00
|
|
|
event = NULL;
|
|
|
|
eventnextwaiting = NULL;
|
2011-09-21 14:52:29 -04:00
|
|
|
sleepuntil = 0;
|
2011-11-05 13:52:11 -04:00
|
|
|
nextsleepingthread = NULL;
|
2011-09-21 14:52:29 -04:00
|
|
|
schedulerlistprev = NULL;
|
|
|
|
schedulerlistnext = NULL;
|
|
|
|
state = NONE;
|
|
|
|
Maxsi::Memory::Set(®isters, 0, sizeof(registers));
|
2011-11-06 17:06:32 -05:00
|
|
|
ready = false;
|
|
|
|
scfunc = NULL;
|
2011-09-15 16:38:40 -04:00
|
|
|
currentsignal = NULL;
|
|
|
|
sighandler = NULL;
|
2012-02-10 07:27:11 -05:00
|
|
|
pidbackup = -1;
|
2012-03-02 07:51:03 -05:00
|
|
|
addrspacebackup = 0UL;
|
2012-02-10 07:27:11 -05:00
|
|
|
terminated = false;
|
2011-11-06 16:00:29 -05:00
|
|
|
ResetCallbacks();
|
2011-09-21 14:52:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Thread::Thread(const Thread* forkfrom)
|
|
|
|
{
|
|
|
|
id = forkfrom->id;
|
|
|
|
process = NULL;
|
|
|
|
prevsibling = NULL;
|
|
|
|
nextsibling = NULL;
|
|
|
|
state = forkfrom->state;
|
2012-01-18 11:10:44 -05:00
|
|
|
event = NULL;
|
|
|
|
eventnextwaiting = NULL;
|
2011-09-21 14:52:29 -04:00
|
|
|
sleepuntil = forkfrom->sleepuntil;
|
|
|
|
Maxsi::Memory::Copy(®isters, &forkfrom->registers, sizeof(registers));
|
|
|
|
ready = false;
|
|
|
|
stackpos = forkfrom->stackpos;
|
|
|
|
stacksize = forkfrom->stacksize;
|
2011-11-05 13:52:11 -04:00
|
|
|
nextsleepingthread = NULL;
|
|
|
|
schedulerlistprev = NULL;
|
|
|
|
schedulerlistnext = NULL;
|
2011-11-06 17:06:32 -05:00
|
|
|
scfunc = NULL;
|
2011-09-15 16:38:40 -04:00
|
|
|
sighandler = forkfrom->sighandler;
|
2012-02-10 07:27:11 -05:00
|
|
|
pidbackup = -1;
|
2012-03-02 07:51:03 -05:00
|
|
|
addrspacebackup = 0UL;
|
2012-02-10 07:27:11 -05:00
|
|
|
terminated = false;
|
2011-11-06 16:00:29 -05:00
|
|
|
ResetCallbacks();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Thread::ResetCallbacks()
|
|
|
|
{
|
|
|
|
onchildprocessexit = NULL;
|
2011-09-21 14:52:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Thread::~Thread()
|
|
|
|
{
|
|
|
|
ASSERT(CurrentProcess() == process);
|
2011-11-05 13:52:11 -04:00
|
|
|
ASSERT(nextsleepingthread == NULL);
|
2011-09-21 14:52:29 -04:00
|
|
|
|
2012-02-10 07:27:11 -05:00
|
|
|
if ( event ) { event->Unregister(this); }
|
|
|
|
|
2011-09-15 16:38:40 -04:00
|
|
|
// Delete information about signals being processed.
|
|
|
|
while ( currentsignal )
|
|
|
|
{
|
|
|
|
Signal* todelete = currentsignal;
|
|
|
|
currentsignal = currentsignal->nextsignal;
|
|
|
|
delete todelete;
|
|
|
|
}
|
|
|
|
|
2011-09-21 14:52:29 -04:00
|
|
|
Memory::UnmapRangeUser(stackpos, stacksize);
|
2012-02-10 07:27:11 -05:00
|
|
|
|
|
|
|
terminated = true;
|
2011-09-21 14:52:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Thread* Thread::Fork()
|
|
|
|
{
|
|
|
|
ASSERT(ready);
|
|
|
|
|
2011-09-15 16:38:40 -04:00
|
|
|
Signal* clonesignal = NULL;
|
|
|
|
if ( currentsignal )
|
|
|
|
{
|
|
|
|
clonesignal = currentsignal->Fork();
|
|
|
|
if ( !clonesignal ) { return NULL; }
|
|
|
|
}
|
|
|
|
|
2011-09-21 14:52:29 -04:00
|
|
|
Thread* clone = new Thread(this);
|
2011-09-15 16:38:40 -04:00
|
|
|
if ( !clone )
|
|
|
|
{
|
|
|
|
while ( clonesignal )
|
|
|
|
{
|
|
|
|
Signal* todelete = clonesignal;
|
|
|
|
clonesignal = clonesignal->nextsignal;
|
|
|
|
delete todelete;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
clone->currentsignal = clonesignal;
|
2011-09-21 14:52:29 -04:00
|
|
|
|
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CreateThreadCPU(Thread* thread, addr_t entry);
|
|
|
|
|
|
|
|
Thread* CreateThread(addr_t entry, size_t stacksize)
|
|
|
|
{
|
|
|
|
Process* process = CurrentProcess();
|
|
|
|
|
|
|
|
if ( stacksize == 0 ) { stacksize = process->DefaultStackSize(); }
|
|
|
|
|
|
|
|
// TODO: Find some unused virtual address space of the needed size
|
|
|
|
// somewhere in the current process.
|
|
|
|
addr_t stackpos = process->AllocVirtualAddr(stacksize);
|
|
|
|
if ( !stackpos ) { return NULL; }
|
|
|
|
|
|
|
|
if ( !Memory::MapRangeUser(stackpos, stacksize) )
|
|
|
|
{
|
|
|
|
// TODO: Free the reserved virtual memory area.
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Thread* thread = new Thread();
|
|
|
|
if ( !thread )
|
|
|
|
{
|
|
|
|
Memory::UnmapRangeUser(stackpos, stacksize);
|
|
|
|
// TODO: Free the reserved virtual memory area.
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
thread->stackpos = stackpos;
|
|
|
|
thread->stacksize = stacksize;
|
|
|
|
|
|
|
|
// Set up the thread state registers.
|
|
|
|
CreateThreadCPU(thread, entry);
|
|
|
|
|
|
|
|
// Create the family tree.
|
|
|
|
thread->process = process;
|
|
|
|
Thread* firsty = process->firstthread;
|
|
|
|
if ( firsty ) { firsty->prevsibling = thread; }
|
|
|
|
thread->nextsibling = firsty;
|
|
|
|
process->firstthread = thread;
|
|
|
|
|
|
|
|
thread->Ready();
|
|
|
|
|
|
|
|
Scheduler::SetThreadState(thread, Thread::State::RUNNABLE);
|
|
|
|
|
|
|
|
return thread;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Thread::Ready()
|
|
|
|
{
|
|
|
|
if ( ready ) { return; }
|
|
|
|
ready = true;
|
|
|
|
|
2012-02-10 07:27:11 -05:00
|
|
|
this->pidbackup = process->pid;
|
2012-03-02 07:51:03 -05:00
|
|
|
this->addrspacebackup = process->addrspace;
|
2012-02-10 07:27:11 -05:00
|
|
|
|
2011-09-21 14:52:29 -04:00
|
|
|
if ( Time::MicrosecondsSinceBoot() < sleepuntil )
|
|
|
|
{
|
|
|
|
uintmax_t howlong = sleepuntil - Time::MicrosecondsSinceBoot();
|
|
|
|
Scheduler::PutThreadToSleep(this, howlong);
|
|
|
|
}
|
|
|
|
else if ( state == State::RUNNABLE )
|
|
|
|
{
|
|
|
|
state = State::NONE; // Since we are in no linked list.
|
|
|
|
Scheduler::SetThreadState(this, State::RUNNABLE);
|
|
|
|
}
|
|
|
|
}
|
2011-09-15 16:38:40 -04:00
|
|
|
|
|
|
|
void Thread::HandleSignal(CPU::InterruptRegisters* regs)
|
|
|
|
{
|
|
|
|
Signal* override = signalqueue.Pop(currentsignal);
|
|
|
|
if ( !override ) { return; }
|
|
|
|
if ( !sighandler ) { delete override; return; }
|
|
|
|
|
|
|
|
if ( override->signum == SIGKILL )
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
override->nextsignal = currentsignal;
|
|
|
|
Maxsi::Memory::Copy(&override->regs, regs, sizeof(override->regs));
|
|
|
|
currentsignal = override;
|
|
|
|
|
|
|
|
HandleSignalCPU(regs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SysSigReturn()
|
|
|
|
{
|
|
|
|
Thread* thread = CurrentThread();
|
|
|
|
if ( !thread->currentsignal ) { return; }
|
|
|
|
|
|
|
|
CPU::InterruptRegisters* dest = Syscall::InterruptRegs();
|
|
|
|
CPU::InterruptRegisters* src = &thread->currentsignal->regs;
|
|
|
|
|
|
|
|
Maxsi::Memory::Copy(dest, src, sizeof(CPU::InterruptRegisters));
|
|
|
|
thread->currentsignal = thread->currentsignal->nextsignal;
|
|
|
|
Syscall::AsIs();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SysRegisterSignalHandler(sighandler_t sighandler)
|
|
|
|
{
|
|
|
|
CurrentThread()->sighandler = sighandler;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SysKill(pid_t pid, int signum)
|
|
|
|
{
|
|
|
|
if ( signum < 0 || 128 <= signum ) { Error::Set(EINVAL); return -1; }
|
|
|
|
|
|
|
|
// Protect the system idle process.
|
|
|
|
if ( pid == 0 ) { Error::Set(EPERM); return -1; }
|
|
|
|
|
|
|
|
Process* process = Process::Get(pid);
|
|
|
|
if ( !process ) { Error::Set(ESRCH); return -1; }
|
|
|
|
|
|
|
|
// TODO: Protect init?
|
|
|
|
// TODO: Check for permission.
|
|
|
|
// TODO: Check for zombies.
|
|
|
|
|
|
|
|
Thread* currentthread = CurrentThread();
|
|
|
|
Thread* thread = NULL;
|
|
|
|
if ( currentthread->process->pid == pid ) { thread = currentthread; }
|
|
|
|
if ( !thread ) { thread = process->firstthread; }
|
|
|
|
if ( !thread ) { Error::Set(ESRCH); return -1; /* TODO: Zombie? */ }
|
|
|
|
|
|
|
|
// TODO: If thread is not runnable, wake it and runs its handler?
|
|
|
|
if ( !thread->signalqueue.Push(signum) )
|
|
|
|
{
|
|
|
|
// TODO: Possibly kill the process?
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( thread == currentthread )
|
|
|
|
{
|
|
|
|
Syscall::SyscallRegs()->result = 0;
|
|
|
|
thread->HandleSignal(Syscall::InterruptRegs());
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Thread::Init()
|
|
|
|
{
|
|
|
|
Syscall::Register(SYSCALL_KILL, (void*) SysKill);
|
|
|
|
Syscall::Register(SYSCALL_REGISTER_SIGNAL_HANDLER, (void*) SysRegisterSignalHandler);
|
|
|
|
Syscall::Register(SYSCALL_SIGRETURN, (void*) SysSigReturn);
|
|
|
|
}
|
2011-09-21 14:52:29 -04:00
|
|
|
}
|