1
0
Fork 0
mirror of https://gitlab.com/sortix/sortix.git synced 2023-02-13 20:55:38 -05:00
sortix--sortix/sortix/descriptors.cpp
Jonas 'Sortie' Termansen 2afe9d1fd6 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-11-01 01:00:20 +01:00

130 lines
3 KiB
C++

/******************************************************************************
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/>.
descriptors.cpp
Handles file descriptors, socket descriptors, and whatnot for each process.
******************************************************************************/
#include "platform.h"
#include <libmaxsi/memory.h>
#include "descriptors.h"
using namespace Maxsi;
namespace Sortix
{
// When in doubt use brute-force. This class could easily be optimized.
Device* const reserveddevideptr = (Device*) 0x1UL;
DescriptorTable::DescriptorTable()
{
numdevices = 0;
devices = NULL;
}
DescriptorTable::~DescriptorTable()
{
for ( int i = 0; i < numdevices; i++ )
{
if ( devices[i] == NULL || devices[i] == reserveddevideptr ) { continue; }
// TODO: unref any device here!
}
delete[] devices;
}
int DescriptorTable::Allocate(Device* object)
{
for ( int i = 0; i < numdevices; i++ )
{
if ( devices[i] == NULL )
{
devices[i] = object;
return i;
}
}
int newlistlength = numdevices*2;
if ( newlistlength == 0 ) { newlistlength = 8; }
Device** newlist = new Device*[newlistlength];
if ( newlist == NULL ) { return -1; }
if ( devices != NULL )
{
Memory::Copy(newlist, devices, sizeof(Device*) * numdevices);
}
Memory::Set(newlist + numdevices, 0, sizeof(Device*) * (newlistlength-numdevices));
delete[] devices;
devices = newlist;
numdevices = newlistlength;
return Allocate(object);
}
void DescriptorTable::Free(int index)
{
ASSERT(index < index);
ASSERT(devices[index] != NULL);
if ( devices[index] != reserveddevideptr )
{
// TODO: Unref device here?
}
devices[index] = NULL;
}
int DescriptorTable::Reserve()
{
return Allocate(reserveddevideptr);
}
void DescriptorTable::UseReservation(int index, Device* object)
{
ASSERT(index < index);
ASSERT(devices[index] != NULL);
ASSERT(devices[index] == reserveddevideptr);
devices[index] = object;
}
bool DescriptorTable::Fork(DescriptorTable* forkinto)
{
Device** newlist = new Device*[numdevices];
if ( newlist == NULL ) { return false; }
Memory::Copy(newlist, devices, sizeof(Device*) * numdevices);
// TODO: Possibly deal with a potential O_CLOFORK!
ASSERT(forkinto->devices == NULL);
forkinto->devices = newlist;
forkinto->numdevices = numdevices;
return true;
}
}