sortix-mirror/sortix/process.cpp

141 lines
3.3 KiB
C++
Raw Normal View History

/******************************************************************************
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/>.
process.cpp
Describes a process belonging to a subsystem.
******************************************************************************/
#include "platform.h"
#include <libmaxsi/memory.h>
#include <libmaxsi/string.h>
#include "process.h"
#include "memorymanagement.h"
2011-08-27 18:57:39 +00:00
#include "initrd.h"
#include "elf.h"
#include "syscall.h"
using namespace Maxsi;
namespace Sortix
{
bool ProcessSegment::Intersects(ProcessSegment* segments)
{
for ( ProcessSegment* tmp = segments; tmp != NULL; tmp = tmp->next )
{
if ( tmp->position < position + size &&
position < tmp->position + tmp->size )
{
return true;
}
}
if ( next ) { return next->Intersects(segments); }
return false;
}
Process::Process(addr_t addrspace)
{
_addrspace = addrspace;
_endcodesection = 0x400000UL;
segments = NULL;
sigint = false;
}
Process::~Process()
{
ResetAddressSpace();
// Avoid memory leaks.
ASSERT(segments == NULL);
// TODO: Delete address space!
}
void Process::ResetAddressSpace()
{
ProcessSegment* tmp = segments;
while ( tmp != NULL )
{
Memory::UnmapRangeUser(tmp->position, tmp->size);
ProcessSegment* todelete = tmp;
tmp = tmp->next;
delete todelete;
}
segments = NULL;
}
2011-08-27 18:57:39 +00:00
int SysExecute(const char* programname)
{
// TODO: Validate that filepath is a user-space readable string!
size_t programsize = 0;
byte* program = InitRD::Open(programname, &programsize);
if ( !program ) { return -1; }
addr_t entry = ELF::Construct(CurrentProcess(), program, programsize);
if ( !entry )
{
Log::PrintF("Could not create process '%s'", programname);
if ( String::Compare(programname, "sh") == 0 )
{
Panic("Couldn't create the shell process");
}
return SysExecute("sh");
}
// This is a hacky way to set up the thread!
CPU::InterruptRegisters* regs = Syscall::InterruptRegs();
regs->eip = entry;
regs->useresp = 0x80000000UL;
regs->ebp = 0x80000000UL;
return 0;
}
void SysExecuteOld(CPU::InterruptRegisters* R)
2011-08-27 18:57:39 +00:00
{
2011-09-08 19:09:14 +00:00
#ifdef PLATFORM_X86
2011-08-27 18:57:39 +00:00
const char* programname = (const char*) R->ebx;
2011-08-27 18:57:39 +00:00
size_t programsize = 0;
byte* program = InitRD::Open(programname, &programsize);
if ( program == NULL ) { R->eax = -1; return; }
addr_t entry = ELF::Construct(CurrentProcess(), program, programsize);
if ( entry == 0 )
{
PanicF("Could not create process '%s'", programname);
}
// This is a hacky way to set up the thread!
R->eip = entry;
R->useresp = 0x80000000UL;
R->ebp = 0x80000000UL;
2011-09-08 19:09:14 +00:00
#endif
2011-08-27 18:57:39 +00:00
}
void Process::Init()
{
Syscall::Register(SYSCALL_EXEC, (void*) SysExecute);
}
}