Skip to content

Commit

Permalink
More work to cleanup the usage of processes. Finished the pipe interf…
Browse files Browse the repository at this point in the history
…ace changes. Damn the process system is deeply integrated into the kernel. Bad choice.
  • Loading branch information
Meulengracht committed Dec 10, 2018
1 parent 6a22549 commit 37a48f8
Show file tree
Hide file tree
Showing 22 changed files with 255 additions and 353 deletions.
19 changes: 6 additions & 13 deletions kernel/arch/x86/components/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
*/
#define __MODULE "XTIF"

#include <process/process.h>
#include <system/thread.h>
#include <system/utils.h>
#include <threading.h>
Expand Down Expand Up @@ -165,13 +164,10 @@ OsStatus_t
ThreadingSignalDispatch(
_In_ MCoreThread_t* Thread)
{
SystemProcess_t* Process = (SystemProcess_t*)LookupHandle(Thread->ProcessHandle);
assert(Process != NULL);

// Now we can enter the signal context
// handler, we cannot return from this function
Thread->Contexts[THREADING_CONTEXT_SIGNAL1] = ContextCreate(Thread->Flags,
THREADING_CONTEXT_SIGNAL1, Process->SignalHandler,
THREADING_CONTEXT_SIGNAL1, Thread->MemorySpace->SignalHandler,
MEMORY_LOCATION_SIGNAL_RET, Thread->ActiveSignal.Signal, 0);
TssUpdateThreadStack(CpuGetCurrentId(), (uintptr_t)Thread->Contexts[THREADING_CONTEXT_SIGNAL0]);
enter_thread(Thread->Contexts[THREADING_CONTEXT_SIGNAL1]);
Expand All @@ -186,12 +182,11 @@ void
ThreadingImpersonate(
_In_ MCoreThread_t *Thread)
{
MCoreThread_t *Current;
UUId_t Cpu;

// Instantiate values
Cpu = CpuGetCurrentId();
Current = ThreadingGetCurrentThread(Cpu);
MCoreThread_t* Current;
UUId_t Cpu;

Cpu = CpuGetCurrentId();
Current = ThreadingGetCurrentThread(Cpu);

// If we impersonate ourself, leave
if (Current == Thread) {
Expand All @@ -200,8 +195,6 @@ ThreadingImpersonate(
else {
Current->Flags |= THREADING_IMPERSONATION;
}

// Load resources
TssUpdateIo(Cpu, (uint8_t*)Thread->MemorySpace->Data[MEMORY_SPACE_IOMAP]);
SwitchSystemMemorySpace(Thread->MemorySpace);
}
Expand Down
31 changes: 11 additions & 20 deletions kernel/arch/x86/interrupts/interrupts.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#include <system/thread.h>
#include <system/utils.h>
#include <ds/collection.h>
#include <process/process.h>
#include <acpiinterface.h>
#include <interrupts.h>
#include <threading.h>
Expand Down Expand Up @@ -401,45 +400,37 @@ InterruptEntry(
* If the signal is blocked the process/thread is killed. */
OsStatus_t
ExceptionSignal(
_In_ Context_t *Registers,
_In_ int Signal)
_In_ Context_t* Registers,
_In_ int Signal)
{
MCoreThread_t *Thread = NULL;
UUId_t Cpu = CpuGetCurrentId();
UUId_t CoreId = CpuGetCurrentId();
MCoreThread_t* Thread = ThreadingGetCurrentThread(CoreId);

// Debug
TRACE("ExceptionSignal(Signal %i)", Signal);

// Sanitize if user-process
#ifdef __OSCONFIG_DISABLE_SIGNALLING
if (Signal >= 0) {
#else
if (GetCurrentProcess() == NULL) {
if (Thread->MemorySpace->SignalHandler == 0) {
#endif
return OsError;
}

// Lookup current thread
Thread = ThreadingGetCurrentThread(Cpu);

// Initialize signal
Thread->ActiveSignal.Ignorable = 0;
Thread->ActiveSignal.Signal = Signal;
Thread->ActiveSignal.Context = Registers;

// Dispatch
Thread->ActiveSignal.Signal = Signal;
Thread->ActiveSignal.Context = Registers;
return ThreadingSignalDispatch(Thread);
}

/* ExceptionEntry
* Common entry for all exceptions */
void
ExceptionEntry(
_In_ Context_t *Registers)
_In_ Context_t* Registers)
{
MCoreThread_t *Thread = NULL;
uintptr_t Address = __MASK;
int IssueFixed = 0;
MCoreThread_t*Thread = NULL;
uintptr_t Address = __MASK;
int IssueFixed = 0;

// Handle IRQ
if (Registers->Irq == 0) { // Divide By Zero (Non-math instruction)
Expand Down
15 changes: 6 additions & 9 deletions kernel/debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
#define __MODULE "DBGI"
//#define __TRACE

#include <process/phoenix.h>
#include <process/process.h>
#include <process/pe.h>
#include <system/utils.h>
#include <memoryspace.h>
#include <scheduler.h>
Expand Down Expand Up @@ -201,17 +198,17 @@ DebugPanic(
* Retrieves the module (Executable) at the given address */
OsStatus_t
DebugGetModuleByAddress(
_In_ SystemProcess_t* Process,
_In_ uintptr_t Address,
_Out_ uintptr_t* Base,
_Out_ char** Name)
_In_ SystemModule_t* Module,
_In_ uintptr_t Address,
_Out_ uintptr_t* Base,
_Out_ char** Name)
{
// Validate that the address is within userspace
if (Address >= MEMORY_LOCATION_RING3_CODE && Address < MEMORY_LOCATION_RING3_CODE_END) {
// Sanitize whether or not a process was running
if (Process != NULL && Process->Executable != NULL) {
uintptr_t PmBase = Process->Executable->VirtualAddress;
char *PmName = (char*)MStringRaw(Process->Executable->Name);
uintptr_t PmBase = Process->Executable->VirtualAddress;
char *PmName = (char*)MStringRaw(Process->Executable->Name);

// Was it not main executable?
if (Address > (Process->Executable->CodeBase + Process->Executable->CodeSize)) {
Expand Down
6 changes: 3 additions & 3 deletions kernel/handle.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@
#include <heap.h>

// Include all the systems that we have to cleanup
#include <process/process.h>
#include <memorybuffer.h>
#include <memoryspace.h>
#include <pipe.h>

static Collection_t SystemHandles = COLLECTION_INIT(KeyId);
static _Atomic(UUId_t) IdGenerator = 1;
static HandleDestructorFn HandleDestructors[HandleTypeCount] = {
DestroyMemoryBuffer,
DestroyProcess,
DestroySystemMemorySpace
DestroySystemMemorySpace,
DestroySystemPipe
};

/* CreateHandle
Expand Down
15 changes: 7 additions & 8 deletions kernel/include/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@
#ifndef _DEBUG_H_
#define _DEBUG_H_

#include <os/osdefs.h>
#include <os/context.h>
#include <component/memory.h>
#include <modules/module.h>
#include <os/context.h>
#include <os/osdefs.h>
#include <log.h>

typedef struct _SystemProcess SystemProcess_t;

/* Sanitize the module definition, must be
* present to identify the source-code that include this */
#ifndef __MODULE
Expand Down Expand Up @@ -108,10 +107,10 @@ DebugPanic(
* Retrieves the module (Executable) at the given address */
KERNELAPI OsStatus_t KERNELABI
DebugGetModuleByAddress(
_In_ SystemProcess_t* Process,
_In_ uintptr_t Address,
_Out_ uintptr_t* Base,
_Out_ char** Name);
_In_ SystemModule_t* Module,
_In_ uintptr_t Address,
_Out_ uintptr_t* Base,
_Out_ char** Name);

/* DebugStackTrace
* Performs a verbose stack trace in the current context
Expand Down
2 changes: 1 addition & 1 deletion kernel/include/handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@

typedef enum _SystemHandleType {
HandleTypeMemoryBuffer = 0,
HandleTypeProcess,
HandleTypeMemorySpace,
HandleTypePipe,

HandleTypeCount
} SystemHandleType_t;
Expand Down
2 changes: 1 addition & 1 deletion kernel/include/interrupts.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ typedef struct _SystemInterrupt {
DeviceInterrupt_t Interrupt;
FastInterruptResourceTable_t KernelResources;
UUId_t Id;
UUId_t ProcessHandle;
UUId_t PipeHandle;
UUId_t Thread;
Flags_t Flags;
int Source;
Expand Down
1 change: 1 addition & 0 deletions kernel/include/memoryspace.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ typedef struct _SystemMemorySpace {
struct _SystemMemorySpace* Parent;
UUId_t ParentHandle;
BlockBitmap_t* HeapSpace;
uintptr_t SignalHandler;
} SystemMemorySpace_t;

/* InitializeSystemMemorySpace
Expand Down
1 change: 1 addition & 0 deletions kernel/include/modules/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ typedef enum _SystemModuleType {

typedef struct _SystemModule {
CollectionItem_t ListHeader;
UUId_t Handle;
MString_t* Path;
const void* Data;
size_t Length;
Expand Down
2 changes: 1 addition & 1 deletion kernel/include/pipe.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ ConstructSystemPipe(
* Destroys a pipe and wakes up all sleeping threads, then frees all resources allocated */
KERNELAPI void KERNELABI
DestroySystemPipe(
_In_ SystemPipe_t* Pipe);
_In_ void* Resource);

/* ReadSystemPipe
* Performs raw reading that can only be used on pipes opened in SPSC mode. */
Expand Down
1 change: 0 additions & 1 deletion kernel/include/threading.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ typedef struct _MCoreThread {
CollectionItem_t CollectionHeader;

UUId_t ParentThreadId;
UUId_t ProcessHandle;
UUId_t Id;
const char* Name;
Flags_t Flags;
Expand Down
32 changes: 18 additions & 14 deletions kernel/modules/manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@

OsStatus_t PhoenixFileHandler(void *UserData);

static Collection_t Modules = COLLECTION_INIT(KeyInteger);
static UUId_t GcFileHandleId = 0;
static Collection_t Modules = COLLECTION_INIT(KeyInteger);
static UUId_t GcFileHandleId = 0;
static UUId_t ModuleIdGenerator = 1;

/* InitializeModuleManager
* Initializes the static storage needed for the module manager, and registers a garbage collector. */
Expand Down Expand Up @@ -65,8 +66,9 @@ RegisterModule(
memset(Module, 0, sizeof(SystemModule_t));
Module->ListHeader.Key.Value.Integer = (int)Type;

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

Module->VendorId = VendorId;
Expand Down Expand Up @@ -189,17 +191,19 @@ GetModule(
{
foreach(Node, &Modules) {
SystemModule_t* Module = (SystemModule_t*)Node;
// Should we check vendor-id && device-id?
if (VendorId != 0 && DeviceId != 0) {
if (Module->VendorId == VendorId && Module->DeviceId == DeviceId) {
return Module;
if (Module->PrimaryThreadId != UUID_INVALID) {
// Should we check vendor-id && device-id?
if (VendorId != 0 && DeviceId != 0) {
if (Module->VendorId == VendorId && Module->DeviceId == DeviceId) {
return Module;
}
}
}

// Skip all fixed-vendor ids
if (Module->VendorId != 0xFFEF) {
if (Module->DeviceClass == DeviceClass && Module->DeviceSubclass == DeviceSubclass) {
return Module;
// Skip all fixed-vendor ids
if (Module->VendorId != 0xFFEF) {
if (Module->DeviceClass == DeviceClass && Module->DeviceSubclass == DeviceSubclass) {
return Module;
}
}
}
}
Expand Down Expand Up @@ -233,7 +237,7 @@ SetModuleAlias(
Module->Alias = Alias;
return OsSuccess;
}
return OsInvalidPermission;
return OsInvalidPermissions;
}

/* GetModuleByAlias
Expand Down
14 changes: 9 additions & 5 deletions kernel/modules/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#define __MODULE "PROC"
//#define __TRACE

#include "../../librt/libds/pe/pe.h"
#include <modules/manager.h>
#include <modules/module.h>
#include <system/utils.h>
Expand All @@ -35,6 +36,7 @@

typedef struct _SystemModulePackage {
SystemModule_t* Module;
MString_t* ModuleName;
const void* FileBuffer;
size_t FileBufferLength;
int FileBufferDynamic;
Expand All @@ -58,19 +60,21 @@ ModuleThreadEntry(
// Setup base address for code data
TRACE("Loading PE-image into memory (buffer 0x%x, size %u)",
Package->FileBuffer, Package->FileBufferLength);
Status = PeLoadImage(NULL, Process->Name, Package->Module->Path, (uint8_t*)Package->FileBuffer,
Status = PeLoadImage(NULL, Package->ModuleName, 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);
Package->Module->PrimaryThreadId = UUID_INVALID;
}

if (Package->FileBufferDynamic) {
kfree(Package->FileBuffer);
}
MStringDestroy(Package->ModuleName);
kfree(Package);

if (Status == OsSuccess) {
Expand All @@ -89,7 +93,6 @@ SpawnModule(
SystemModulePackage_t* Package;
int Index;
OsStatus_t Status;
MString_t* Name;

assert(Module != NULL);
assert(Module->Executable == NULL);
Expand Down Expand Up @@ -123,11 +126,12 @@ SpawnModule(
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,
Package->ModuleName = MStringSubString(Module->Path, Index + 1, -1);
Status = CreateThread(MStringRaw(Package->ModuleName), ModuleThreadEntry, Package,
THREADING_USERMODE, UUID_INVALID, &Module->PrimaryThreadId);
MStringDestroy(Name);
if (Status != OsSuccess) {
// @todo cleanup everything?
MStringDestroy(Package->ModuleName);
kfree(Package);
return Status;
}
Expand Down
4 changes: 2 additions & 2 deletions kernel/pipe.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ ConstructSystemPipe(
* Destroys a pipe and wakes up all sleeping threads, then frees all resources allocated */
void
DestroySystemPipe(
_In_ SystemPipe_t* Pipe)
_In_ void* Resource)
{
// @todo pipe synchronization with threads waiting
// for data in pipe.
kfree(Pipe);
kfree(Resource);
}

/////////////////////////////////////////////////////////////////////////
Expand Down
Loading

0 comments on commit 37a48f8

Please sign in to comment.