1
0
Fork 0
mirror of https://gitlab.com/sortix/sortix.git synced 2023-02-13 20:55:38 -05:00

Processes now remember the memory segments it has loaded.

This commit is contained in:
Jonas 'Sortie' Termansen 2011-08-27 16:46:00 +02:00
parent b9484c24f2
commit 5c86cb4abd
8 changed files with 144 additions and 71 deletions

View file

@ -44,7 +44,7 @@ DEFINES:=$(DEFINES) -DINITRD
CPPFLAGSRELEASE=-s -O3 CPPFLAGSRELEASE=-s -O3
CPPFLAGSDEBUG= CPPFLAGSDEBUG=
CPPFLAGS=-I.. -I. $(CPUDEFINES) $(CPUFLAGS) -std=gnu++0x -Wall -Wextra -nostdlib -fno-builtin -nostartfiles -nodefaultlibs -fno-exceptions -fno-rtti -fno-stack-protector $(DEFINES) $(CPPFLAGSRELEASE) CPPFLAGS=-I.. -I. $(CPUDEFINES) $(CPUFLAGS) -std=gnu++0x -Wall -Wextra -nostdlib -fno-builtin -nostartfiles -nodefaultlibs -fno-exceptions -fno-rtti -fno-stack-protector $(DEFINES) $(CPPFLAGSRELEASE)
OBJS=$(CPUOBJS) kernel.o descriptor_tables.o isr.o time.o log.o iprintable.o panic.o keyboard.o memorymanagement.o scheduler.o syscall.o application.o pong.o sound.o pci.o uart.o conway.o test.o http.o vgaterminal.o serialterminal.o descriptors.o device.o vga.o elf.o ../libmaxsi/libmaxsi-sortix.a OBJS=$(CPUOBJS) kernel.o descriptor_tables.o isr.o time.o log.o iprintable.o panic.o keyboard.o memorymanagement.o scheduler.o syscall.o application.o pong.o sound.o pci.o uart.o conway.o test.o http.o vgaterminal.o serialterminal.o descriptors.o device.o vga.o elf.o process.o ../libmaxsi/libmaxsi-sortix.a
JSOBJS:=$(subst .o,-js.o,$(OBJS)) JSOBJS:=$(subst .o,-js.o,$(OBJS))
all: sortix.bin all: sortix.bin

View file

@ -27,6 +27,7 @@
#include "elf.h" #include "elf.h"
#include "memorymanagement.h" #include "memorymanagement.h"
#include "panic.h" #include "panic.h"
#include "process.h"
using namespace Maxsi; using namespace Maxsi;
@ -34,7 +35,7 @@ namespace Sortix
{ {
namespace ELF namespace ELF
{ {
addr_t Construct32(const void* file, size_t filelen) addr_t Construct32(Process* process, const void* file, size_t filelen)
{ {
if ( filelen < sizeof(Header32) ) { return 0; } if ( filelen < sizeof(Header32) ) { return 0; }
const Header32* header = (const Header32*) file; const Header32* header = (const Header32*) file;
@ -57,6 +58,9 @@ namespace Sortix
size_t neededfilelen = phtbloffset + numprogheaders * phsize; size_t neededfilelen = phtbloffset + numprogheaders * phsize;
if ( filelen < neededfilelen ) { return 0; } if ( filelen < neededfilelen ) { return 0; }
// Reset the current address space.
process->ResetAddressSpace();
// Create all the segments in the final process. // Create all the segments in the final process.
// TODO: Handle errors on bad/malicious input or out-of-mem! // TODO: Handle errors on bad/malicious input or out-of-mem!
for ( uint16_t i = 0; i < numprogheaders; i++ ) for ( uint16_t i = 0; i < numprogheaders; i++ )
@ -70,11 +74,28 @@ namespace Sortix
ASSERT(pht->offset + pht->filesize < filelen); ASSERT(pht->offset + pht->filesize < filelen);
ASSERT(pht->filesize <= pht->memorysize); ASSERT(pht->filesize <= pht->memorysize);
ProcessSegment* segment = new ProcessSegment;
if ( segment == NULL ) { return 0; }
segment->position = mapto;
segment->size = Page::AlignUp(mapbytes);
if ( segment->Intersects(process->segments) )
{
delete segment;
return 0;
}
if ( !VirtualMemory::MapRangeUser(mapto, mapbytes) ) if ( !VirtualMemory::MapRangeUser(mapto, mapbytes) )
{ {
PanicF("Could not user-map at %p and %zu bytes onwards", mapto, mapbytes); return 0;
} }
// Insert our newly allocated memory into the processes segment
// list such that it can be reclaimed later.
if ( process->segments ) { process->segments->prev = segment;}
segment->next = process->segments;
process->segments = segment;
// Copy as much data as possible and memset the rest to 0. // Copy as much data as possible and memset the rest to 0.
byte* memdest = (byte*) virtualaddr; byte* memdest = (byte*) virtualaddr;
byte* memsource = (byte*) ( (addr_t)file + pht->offset); byte* memsource = (byte*) ( (addr_t)file + pht->offset);
@ -82,18 +103,15 @@ namespace Sortix
Memory::Set(memdest + pht->filesize, 0, pht->memorysize - pht->filesize); Memory::Set(memdest + pht->filesize, 0, pht->memorysize - pht->filesize);
} }
// MEMORY LEAK: There is no system in place to delete the sections
// once the process has terminated.
return entry; return entry;
} }
addr_t Construct64(const void* /*file*/, size_t /*filelen*/) addr_t Construct64(Process* /*process*/, const void* /*file*/, size_t /*filelen*/)
{ {
return 0; return 0;
} }
addr_t Construct(const void* file, size_t filelen) addr_t Construct(Process* process, const void* file, size_t filelen)
{ {
if ( filelen < sizeof(Header) ) { return 0; } if ( filelen < sizeof(Header) ) { return 0; }
const Header* header = (const Header*) file; const Header* header = (const Header*) file;
@ -107,9 +125,9 @@ namespace Sortix
switch ( header->fileclass ) switch ( header->fileclass )
{ {
case CLASS32: case CLASS32:
return Construct32(file, filelen); return Construct32(process, file, filelen);
case CLASS64: case CLASS64:
return Construct64(file, filelen); return Construct64(process, file, filelen);
default: default:
return 0; return 0;
} }

View file

@ -27,6 +27,8 @@
namespace Sortix namespace Sortix
{ {
class Process;
namespace ELF namespace ELF
{ {
struct Header struct Header
@ -117,7 +119,7 @@ namespace Sortix
// Reads the elf file into the current address space and returns the // Reads the elf file into the current address space and returns the
// entry address of the program, or 0 upon failure. // entry address of the program, or 0 upon failure.
addr_t Construct(const void* file, size_t filelen); addr_t Construct(Process* process, const void* file, size_t filelen);
} }
} }

View file

@ -252,7 +252,7 @@ namespace Sortix
if ( initrd != NULL ) if ( initrd != NULL )
{ {
initstart = (Thread::Entry) ELF::Construct(initrd, initrdsize); initstart = (Thread::Entry) ELF::Construct(process, initrd, initrdsize);
if ( initstart == NULL ) if ( initstart == NULL )
{ {
Panic("kernel.cpp: Could not construct ELF program"); Panic("kernel.cpp: Could not construct ELF program");

78
sortix/process.cpp Normal file
View file

@ -0,0 +1,78 @@
/******************************************************************************
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 "process.h"
#include "memorymanagement.h"
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;
}
Process::~Process()
{
ResetAddressSpace();
// Avoid memory leaks.
ASSERT(segments == NULL);
// TODO: Delete address space!
}
void Process::ResetAddressSpace()
{
ProcessSegment* tmp = segments;
while ( tmp != NULL )
{
VirtualMemory::UnmapRangeUser(tmp->position, tmp->size);
ProcessSegment* todelete = tmp;
tmp = tmp->next;
delete todelete;
}
segments = NULL;
}
}

View file

@ -31,40 +31,50 @@ namespace Sortix
{ {
class Thread; class Thread;
class Process; class Process;
class MountPoint; struct ProcessSegment;
class User;
class System; struct ProcessSegment
class DevBuffer; {
public:
ProcessSegment() : prev(NULL), next(NULL) { }
public:
ProcessSegment* prev;
ProcessSegment* next;
addr_t position;
size_t size;
public:
bool Intersects(ProcessSegment* segments);
};
class Process class Process
{ {
public: public:
// TODO: Make this possible from an async syscall! Process(addr_t addrspace);
//bool CreateProcess(System* system, DevBuffer* image); ~Process();
private:
addr_t _addrspace;
public: public:
DescriptorTable _descs; DescriptorTable descriptors;
ProcessSegment* segments;
private:
MountPoint* _rootFS;
User* _user;
System* _system;
public: public:
MountPoint* GetRootFS() { _rootFS; } addr_t _endcodesection; // HACK
User* GetUser() { _user; }
System* GetSystem() { _system; }
private: public:
Process* _parent; addr_t GetAddressSpace() { return _addrspace; }
Process* _child; void ResetAddressSpace();
Process* _prevSibling;
Process* _nextSibling;
private: public:
Thread* _firstThread; bool IsSane() { return _addrspace != 0; }
}; };
Process* CurrentProcess();
} }
#endif #endif

View file

@ -38,17 +38,6 @@ namespace Sortix
{ {
const bool LOG_SWITCHING = false; const bool LOG_SWITCHING = false;
Process::Process(addr_t addrspace)
{
_addrspace = addrspace;
_endcodesection = 0x400000UL;
}
Process::~Process()
{
// TODO: Delete address space!
}
namespace Scheduler namespace Scheduler
{ {
// This is a very small thread that does absoluting nothing! // This is a very small thread that does absoluting nothing!

View file

@ -25,35 +25,12 @@
#ifndef SORTIX_SCHEDULER_H #ifndef SORTIX_SCHEDULER_H
#define SORTIX_SCHEDULER_H #define SORTIX_SCHEDULER_H
#include "process.h"
#include "descriptors.h" #include "descriptors.h"
namespace Sortix namespace Sortix
{ {
class Thread; class Thread;
class Process;
class Process
{
public:
Process(addr_t addrspace);
~Process();
private:
addr_t _addrspace;
public:
DescriptorTable descriptors;
public:
addr_t _endcodesection; // HACK
public:
addr_t GetAddressSpace() { return _addrspace; }
public:
bool IsSane() { return _addrspace != 0; }
};
class Thread class Thread
{ {
@ -138,7 +115,6 @@ namespace Sortix
// Scheduling // Scheduling
Thread* CurrentThread(); Thread* CurrentThread();
Process* CurrentProcess();
} }
#endif #endif