Skip to content

Commit

Permalink
Update some of the existing system call, more work on how systems are…
Browse files Browse the repository at this point in the history
… going to work now that processes no longer exists in os
  • Loading branch information
Meulengracht committed Dec 5, 2018
1 parent 5fc83dc commit 6a22549
Show file tree
Hide file tree
Showing 13 changed files with 212 additions and 230 deletions.
3 changes: 3 additions & 0 deletions kernel/include/modules/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ typedef struct _MString MString_t;

#define MODULE_FILESYSTEM 0x01010101
#define MODULE_BUS 0x02020202
#define MODULE_INITIAL_STACK 0x1000
#define MODULE_MAX_STACK (4 << 20)

typedef enum _SystemModuleType {
FileResource = 0,
Expand All @@ -47,6 +49,7 @@ typedef struct _SystemModule {
size_t Length;

// Used by Module/Service type
clock_t StartedAt;
MString_t* WorkingDirectory;
MString_t* BaseDirectory;
UUId_t PrimaryThreadId;
Expand Down
80 changes: 0 additions & 80 deletions kernel/include/modules/process.h

This file was deleted.

8 changes: 7 additions & 1 deletion kernel/memoryspace.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
*/
#define __MODULE "MSPC"

#include <process/process.h>
#include <component/cpu.h>
#include <system/utils.h>
#include <memoryspace.h>
Expand Down Expand Up @@ -105,6 +104,13 @@ CreateSystemMemorySpace(
}
}
}

// If we are root, create the memory bitmaps
if (MemorySpace->ParentHandle == UUID_INVALID) {
CreateBlockmap(0, GetMachine()->MemoryMap.UserHeap.Start,
GetMachine()->MemoryMap.UserHeap.Start + GetMachine()->MemoryMap.UserHeap.Length,
GetMachine()->MemoryGranularity, &MemorySpace->HeapSpace);
}
CloneVirtualSpace(MemorySpace->Parent, MemorySpace, (Flags & MEMORY_SPACE_INHERIT) ? 1 : 0);
*Handle = CreateHandle(HandleTypeMemorySpace, 0, MemorySpace);
}
Expand Down
13 changes: 7 additions & 6 deletions kernel/modules/manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,15 @@ RegisterModule(
memset(Module, 0, sizeof(SystemModule_t));
Module->ListHeader.Key.Value.Integer = (int)Type;

Module->Data = Data;
Module->Path = MStringCreate("rd:/", StrUTF8);
MStringAppendString(Module->Path, Path);
Module->Data = Data;

Module->VendorId = VendorId;
Module->DeviceId = DeviceId;
Module->DeviceClass = DeviceClass;
Module->DeviceSubclass = DeviceSubclass;
Module->VendorId = VendorId;
Module->DeviceId = DeviceId;
Module->DeviceClass = DeviceClass;
Module->DeviceSubclass = DeviceSubclass;
Module->PrimaryThreadId = UUID_INVALID;
return CollectionAppend(&Modules, &Module->ListHeader);
}

Expand Down Expand Up @@ -213,7 +214,7 @@ GetCurrentModule(void)
UUId_t ThreadId = ThreadingGetCurrentThreadId();
foreach(Node, &Modules) {
SystemModule_t* Module = (SystemModule_t*)Node;
if (Module->PrimaryThreadId == ThreadId) {
if (Module->PrimaryThreadId == ThreadId /* || IsChildOf(Module->PrimaryThreadId)*/) {
return Module;
}
}
Expand Down
121 changes: 65 additions & 56 deletions kernel/modules/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,62 +33,49 @@
#include <debug.h>
#include <heap.h>

typedef struct _SystemProcessPackage {
UUId_t ProcessHandle;
uint8_t* FileBuffer;
size_t FileBufferLength;
} SystemProcessPackage_t;
typedef struct _SystemModulePackage {
SystemModule_t* Module;
const void* FileBuffer;
size_t FileBufferLength;
int FileBufferDynamic;
} SystemModulePackage_t;

/* ProcessThreadEntry
* This is the standard ash-boot function
* which simply sets up the ash and jumps to userland */
/* ModuleThreadEntry
* Bootstraps the module, by relocating the image correctly into the module's address
* space and handles operatings that must be done on the same thread. */
void
ProcessThreadEntry(
_In_ void* Context)
ModuleThreadEntry(
_In_ void* Context)
{
SystemProcessPackage_t* Package = (SystemProcessPackage_t*)Context;
SystemProcess_t* Process = (SystemProcess_t*)LookupHandle(Package->ProcessHandle);
UUId_t CurrentCpu = CpuGetCurrentId();
MCoreThread_t* Thread = ThreadingGetCurrentThread(CurrentCpu);
uintptr_t BaseAddress;
SystemModulePackage_t* Package = (SystemModulePackage_t*)Context;
UUId_t CurrentCpu = CpuGetCurrentId();
MCoreThread_t* Thread = ThreadingGetCurrentThread(CurrentCpu);
OsStatus_t Status;

assert(Package != NULL);
assert(Process != NULL);
assert(Thread != NULL);

// Argument when calling a new process is just NULL
Thread->ParentThreadId = UUID_INVALID;
Thread->ProcessHandle = Package->ProcessHandle;

// Update currently running thread, by nulling parent we mark
// it as a standalone thread, which make sure it's not a part of a killable chain
Process->MainThreadId = Thread->Id;
Process->MemorySpace = GetCurrentSystemMemorySpace();
TimersGetSystemTick(&Process->StartedAt);
TimersGetSystemTick(&Package->Module->StartedAt);

// Setup base address for code data
TRACE("Loading PE-image into memory (buffer 0x%x, size %u)",
Package->FileBuffer, Package->FileBufferLength);
BaseAddress = GetMachine()->MemoryMap.UserCode.Start;
Process->Executable = PeLoadImage(NULL, Process->Name, Package->FileBuffer,
Package->FileBufferLength, &BaseAddress, Package->LoadedFromInitRD);
Process->NextLoadingAddress = BaseAddress;

// Update entry functions
assert(Process->Executable != NULL);
Thread->Function = (ThreadEntry_t)Process->Executable->EntryAddress;
Thread->Arguments = NULL;

if (!Package->LoadedFromInitRD) {
Status = PeLoadImage(NULL, Process->Name, Package->Module->Path, (uint8_t*)Package->FileBuffer,
Package->FileBufferLength, &Package->Module->Executable);
if (Status == OsSuccess) {
Thread->Function = (ThreadEntry_t)Package->Module->Executable->EntryAddress;
Thread->Arguments = NULL;
}
else {
ERROR("Failed to bootstrap pe image: %u", Status);
}
if (Package->FileBufferDynamic) {
kfree(Package->FileBuffer);
}
kfree(Package);

// Initialize the memory bitmaps
CreateBlockmap(0, GetMachine()->MemoryMap.UserHeap.Start,
GetMachine()->MemoryMap.UserHeap.Start + GetMachine()->MemoryMap.UserHeap.Length,
GetMachine()->MemoryGranularity, &Process->Heap);
ThreadingSwitchLevel();

if (Status == OsSuccess) {
ThreadingSwitchLevel();
}
}

/* SpawnModule
Expand All @@ -99,29 +86,51 @@ SpawnModule(
_In_ const void* Data,
_In_ size_t Length)
{
SystemProcessPackage_t* Package;
int Index;
UUId_t ThreadId;
SystemModulePackage_t* Package;
int Index;
OsStatus_t Status;
MString_t* Name;

assert(Module != NULL);
assert(Module->Executable == NULL);

Package = (SystemModulePackage_t*)kmalloc(sizeof(SystemModulePackage_t));
Package->FileBuffer = Data;
Package->FileBufferLength = Length;
Package->Module = Module;
Package->FileBufferDynamic = 1;

// If no data is passed the data stored initially in module structure
// must be present
if (Data == NULL || Length == 0) {
assert(Module->Data != NULL && Module->Length != 0);
Package->FileBuffer = Module->Data;
Package->FileBufferLength = Module->Length;
Package->FileBufferDynamic = 0;
}

Status = PeValidateImageBuffer((uint8_t*)Package->FileBuffer, Package->FileBufferLength);
if (Status != OsSuccess) {
kfree(Package);
return Status;
}

// Create initial resources
Module->Rpc = CreateSystemPipe(PIPE_MPMC | PIPE_STRUCTURED_BUFFER, PIPE_DEFAULT_ENTRYCOUNT);

// Split path, even if a / is not found
// it won't fail, since -1 + 1 = 0, so we just copy the entire string
Index = MStringFindReverse(Process->Path, '/', 0);
Process->WorkingDirectory = MStringSubString(Process->Path, 0, Index);
Process->BaseDirectory = MStringSubString(Process->Path, 0, Index);
Process->Name = MStringSubString(Process->Path, Index + 1, -1);
Process->Pipes = CollectionCreate(KeyInteger);
Process->FileMappings = CollectionCreate(KeyInteger);
Process->Type = Type;
CreateThread(MStringRaw(Process->Name), ProcessThreadEntry, Package, THREADING_USERMODE, UUID_INVALID, &ThreadId);
ThreadingDetachThread(ThreadId);
Index = MStringFindReverse(Module->Path, '/', 0);
Module->WorkingDirectory = MStringSubString(Module->Path, 0, Index);
Module->BaseDirectory = MStringSubString(Module->Path, 0, Index);
Name = MStringSubString(Module->Path, Index + 1, -1);
Status = CreateThread(MStringRaw(Name), ModuleThreadEntry, Package,
THREADING_USERMODE, UUID_INVALID, &Module->PrimaryThreadId);
MStringDestroy(Name);
if (Status != OsSuccess) {
kfree(Package);
return Status;
}
ThreadingDetachThread(Module->PrimaryThreadId);
return OsSuccess;
}
Loading

0 comments on commit 6a22549

Please sign in to comment.