Skip to content

Commit

Permalink
Fixed an issue in scheduler code, finished updating code to use new p…
Browse files Browse the repository at this point in the history
…rocess system, discovered a small issue with passing arguments that are not string data to services.
  • Loading branch information
Meulengracht committed Oct 29, 2018
1 parent 3fed2dd commit f712fbd
Show file tree
Hide file tree
Showing 30 changed files with 211 additions and 268 deletions.
2 changes: 1 addition & 1 deletion config/common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ config_flags += -D__OSCONFIG_ENABLE_MULTIPROCESSORS # Use all cores
#config_flags += -D__OSCONFIG_PROCESS_SINGLELOAD # No simuoultanous process loading
config_flags += -D__OSCONFIG_DEBUGCONSOLE # Enable debug console on startup instead of splash
#config_flags += -D__OSCONFIG_DEBUGMODE # Enable debug mode, this enables the debug terminal
config_flags += -D__OSCONFIG_RUN_CPPTESTS # Enables user-mode testing programs for the c/c++ suite.
#config_flags += -D__OSCONFIG_RUN_CPPTESTS # Enables user-mode testing programs for the c/c++ suite.
#config_flags += -D__OSCONFIG_TEST_KERNEL # Enable kernel-mode testing suites of the operating system

# Driver Configuration
Expand Down
30 changes: 14 additions & 16 deletions kernel/arch/x86/components/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -401,15 +401,14 @@ SetVirtualPageMapping(
_In_ VirtualAddress_t vAddress,
_In_ Flags_t Flags)
{
// Variabes
PAGE_MASTER_LEVEL *ParentDirectory;
PAGE_MASTER_LEVEL *Directory;
PageTable_t *Table;
uint32_t Mapping;
Flags_t ConvertedFlags;
int IsCurrent, Update;

OsStatus_t Status = OsSuccess;
PAGE_MASTER_LEVEL* ParentDirectory;
PAGE_MASTER_LEVEL* Directory;
PageTable_t* Table;
uintptr_t Mapping;
Flags_t ConvertedFlags;
int Update;
int IsCurrent;
OsStatus_t Status = OsSuccess;

ConvertedFlags = ConvertSystemSpaceToPaging(Flags);
Directory = MmVirtualGetMasterTable(MemorySpace, (vAddress & PAGE_MASK), &ParentDirectory, &IsCurrent);
Expand Down Expand Up @@ -465,17 +464,16 @@ ClearVirtualPageMapping(
_In_ SystemMemorySpace_t* MemorySpace,
_In_ VirtualAddress_t Address)
{
// Variabes
PAGE_MASTER_LEVEL *ParentDirectory;
PAGE_MASTER_LEVEL *Directory;
PageTable_t *Table;
uint32_t Mapping;
int IsCurrent, Update;
PAGE_MASTER_LEVEL* ParentDirectory;
PAGE_MASTER_LEVEL* Directory;
PageTable_t* Table;
uintptr_t Mapping;
int Update;
int IsCurrent;

Directory = MmVirtualGetMasterTable(MemorySpace, Address, &ParentDirectory, &IsCurrent);
Table = MmVirtualGetTable(ParentDirectory, Directory, Address, IsCurrent, 0, 0, &Update);

// Did the page-table exist?
if (Table == NULL) {
return OsError;
}
Expand Down
5 changes: 0 additions & 5 deletions kernel/arch/x86/components/smp.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,12 @@ StartApplicationCore(
void
CpuSmpInitialize(void)
{
// Variables
uint32_t *CodePointer = (uint32_t*)((uint8_t*)(&__GlbTramplineCode[0]) + __GlbTramplineCode_length);
uint32_t EntryCode = (uint32_t)(uint32_t*)SmpApplicationCoreEntry;

// Debug
TRACE("CpuSmpInitialize(%i)", GetMachine()->Processor.NumberOfCores);

// Initialize variables
*(CodePointer - 1) = EntryCode;
*(CodePointer - 2) = GetCurrentSystemMemorySpace()->Data[MEMORY_SPACE_CR3];

// Initialize the trampoline code in memory
memcpy((void*)MEMORY_LOCATION_TRAMPOLINE_CODE, (char*)__GlbTramplineCode, __GlbTramplineCode_length);
}
3 changes: 3 additions & 0 deletions kernel/debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
//#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 @@ -174,6 +176,7 @@ DebugPanic(
}
}
ThreadingDebugPrint();
for(;;);

// Stack trace
DebugStackTrace(Context, 8);
Expand Down
64 changes: 31 additions & 33 deletions kernel/deviceio.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@
//#define __TRACE

#include <ds/collection.h>
#include <process/server.h>
#include <process/process.h>
#include <system/utils.h>
#include <system/io.h>
#include <memoryspace.h>
#include <system/io.h>
#include <threading.h>
#include <deviceio.h>
#include <debug.h>
#include <heap.h>
Expand All @@ -43,14 +44,11 @@ RegisterSystemDeviceIo(
_In_ DeviceIo_t* IoSpace)
{
SystemDeviceIo_t* SystemIo;

// Debugging
TRACE("RegisterSystemDeviceIo(Type %u)", IoSpace->Type);

// Before doing anything, we should do a over-lap
// check before trying to register this


// Allocate a new system only copy of the io-space
// as we don't want anyone to edit our copy
SystemIo = (SystemDeviceIo_t*)kmalloc(sizeof(SystemDeviceIo_t));
Expand All @@ -59,8 +57,6 @@ RegisterSystemDeviceIo(

IoSpace->Id = atomic_fetch_add(&IoSpaceIdGenerator, 1);
memcpy(&SystemIo->Io, IoSpace, sizeof(DeviceIo_t));

// Add to list
SystemIo->Header.Key.Value = (int)IoSpace->Id;
return CollectionAppend(&IoSpaces, &SystemIo->Header);
}
Expand Down Expand Up @@ -96,29 +92,28 @@ OsStatus_t
AcquireSystemDeviceIo(
_In_ DeviceIo_t* IoSpace)
{
SystemDeviceIo_t* SystemIo;
MCoreServer_t *Server;
DataKey_t Key;
UUId_t Cpu;
SystemDeviceIo_t* SystemIo;
SystemProcess_t* Service = GetCurrentProcess();
DataKey_t Key;
UUId_t CoreId = CpuGetCurrentId();
assert(IoSpace != NULL);

// Debugging
TRACE("AcquireSystemDeviceIo(Id %u)", IoSpace->Id);

// Lookup the system copy to validate this requested operation
Server = PhoenixGetCurrentServer();
Cpu = CpuGetCurrentId();
Key.Value = (int)IoSpace->Id;
SystemIo = (SystemDeviceIo_t*)CollectionGetNodeByKey(&IoSpaces, Key, 0);

// Sanitize the system copy
if (Server == NULL || SystemIo == NULL || SystemIo->Owner != UUID_INVALID) {
if (Server == NULL) {
if (Service == NULL || Service->Type != ProcessService ||
SystemIo == NULL || SystemIo->Owner != UUID_INVALID) {
if (Service == NULL || Service->Type != ProcessService) {
ERROR(" > non-server process tried to acquire io-space");
}
ERROR(" > failed to find the requested io-space, id %u", IoSpace->Id);
return OsError;
}
SystemIo->Owner = ThreadingGetCurrentThread(Cpu)->AshId;
SystemIo->Owner = ThreadingGetCurrentThread(CoreId)->ProcessHandle;

switch (SystemIo->Io.Type) {
case DeviceIoMemoryBased: {
Expand All @@ -127,7 +122,7 @@ AcquireSystemDeviceIo(
size_t PageSize = GetSystemMemoryPageSize();
size_t Length = SystemIo->Io.Access.Memory.Length + (BaseAddress % PageSize);

MappedAddress = AllocateBlocksInBlockmap(Server->Base.Heap, __MASK, Length);
MappedAddress = AllocateBlocksInBlockmap(Service->Heap, __MASK, Length);
if (MappedAddress == 0) {
ERROR(" > failed to allocate heap memory for mapping");
break;
Expand All @@ -142,7 +137,7 @@ AcquireSystemDeviceIo(

case DeviceIoPortBased: {
for (size_t i = 0; i < SystemIo->Io.Access.Port.Length; i++) {
SetDirectIoAccess(Cpu, GetCurrentSystemMemorySpace(), ((uint16_t)(SystemIo->Io.Access.Port.Base + i)), 1);
SetDirectIoAccess(CoreId, GetCurrentSystemMemorySpace(), ((uint16_t)(SystemIo->Io.Access.Port.Base + i)), 1);
}
return OsSuccess;
} break;
Expand All @@ -162,22 +157,26 @@ OsStatus_t
ReleaseSystemDeviceIo(
_In_ DeviceIo_t* IoSpace)
{
SystemDeviceIo_t* SystemIo;
MCoreServer_t *Server;
DataKey_t Key;
UUId_t Cpu;
SystemDeviceIo_t* SystemIo;
SystemProcess_t* Service = GetCurrentProcess();
DataKey_t Key;
UUId_t CoreId = CpuGetCurrentId();
assert(IoSpace != NULL);

// Debugging
TRACE("ReleaseSystemDeviceIo(Id %u)", IoSpace->Id);

// Lookup the system copy to validate this requested operation
Server = PhoenixGetCurrentServer();
Cpu = CpuGetCurrentId();
Key.Value = (int)IoSpace->Id;
SystemIo = (SystemDeviceIo_t*)CollectionGetNodeByKey(&IoSpaces, Key, 0);

// Sanitize the system copy and do some security checks
if (Server == NULL || SystemIo == NULL || SystemIo->Owner != Server->Base.Id) {
if (Service == NULL || Service->Type != ProcessService ||
SystemIo == NULL || SystemIo->Owner != ThreadingGetCurrentThread(CoreId)->ProcessHandle) {
if (Service == NULL || Service->Type != ProcessService) {
ERROR(" > non-server process tried to acquire io-space");
}
ERROR(" > failed to find the requested io-space, id %u", IoSpace->Id);
return OsError;
}

Expand All @@ -186,13 +185,13 @@ ReleaseSystemDeviceIo(
uintptr_t BaseAddress = SystemIo->Io.Access.Memory.PhysicalBase;
size_t PageSize = GetSystemMemoryPageSize();
size_t Length = SystemIo->Io.Access.Memory.Length + (BaseAddress % PageSize);
ReleaseBlockmapRegion(Server->Base.Heap, SystemIo->MappedAddress, Length);
ReleaseBlockmapRegion(Service->Heap, SystemIo->MappedAddress, Length);
RemoveSystemMemoryMapping(GetCurrentSystemMemorySpace(), SystemIo->MappedAddress, Length);
} break;

case DeviceIoPortBased: {
for (size_t i = 0; i < SystemIo->Io.Access.Port.Length; i++) {
SetDirectIoAccess(Cpu, GetCurrentSystemMemorySpace(), ((uint16_t)(SystemIo->Io.Access.Port.Base + i)), 0);
SetDirectIoAccess(CoreId, GetCurrentSystemMemorySpace(), ((uint16_t)(SystemIo->Io.Access.Port.Base + i)), 0);
}
} break;

Expand Down Expand Up @@ -284,13 +283,13 @@ uintptr_t
ValidateDeviceIoMemoryAddress(
_In_ uintptr_t Address)
{
UUId_t ProcessId = ThreadingGetCurrentThread(CpuGetCurrentId())->AshId;
UUId_t Handle = ThreadingGetCurrentThread(CpuGetCurrentId())->ProcessHandle;

// Debugging
TRACE("ValidateDeviceIoMemoryAddress(Process %u, Address 0x%x)", ProcessId, Address);
TRACE("ValidateDeviceIoMemoryAddress(Process %u, Address 0x%x)", Handle, Address);

// Sanitize the id
if (ProcessId == UUID_INVALID) {
if (Handle == UUID_INVALID) {
return 0;
}

Expand All @@ -302,8 +301,7 @@ ValidateDeviceIoMemoryAddress(
// Two things has to be true before the io-space
// is valid, it has to belong to the right process
// and be in range
if (IoSpace->Owner == ProcessId &&
IoSpace->Io.Type == DeviceIoMemoryBased &&
if (IoSpace->Owner == Handle && IoSpace->Io.Type == DeviceIoMemoryBased &&
(Address >= VirtualBase && Address < (VirtualBase + IoSpace->Io.Access.Memory.Length))) {
return IoSpace->Io.Access.Memory.PhysicalBase + (Address - VirtualBase);
}
Expand Down
27 changes: 6 additions & 21 deletions kernel/garbagecollector.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ UUId_t
GcRegister(
_In_ GcHandler_t Handler)
{
// Variables
DataKey_t Key;
Key.Value = (int)atomic_fetch_add(&GcIdGenerator, 1);
CollectionAppend(&GcHandlers, CollectionCreateNode(Key, (void*)Handler));
Expand All @@ -80,10 +79,7 @@ OsStatus_t
GcUnregister(
_In_ UUId_t Handler)
{
// Variables
DataKey_t Key;

// Setup the key
Key.Value = (int)Handler;
if (CollectionGetDataByKey(&GcHandlers, Key, 0) == NULL) {
return OsError;
Expand All @@ -96,20 +92,15 @@ GcUnregister(
OsStatus_t
GcSignal(
_In_ UUId_t Handler,
_In_ void *Data)
_In_ void* Data)
{
// Variables
DataKey_t Key;

// Setup the key
Key.Value = (int)Handler;

// Sanitize the status of the gc
if (CollectionGetDataByKey(&GcHandlers, Key, 0) == NULL) {
return OsError;
}

// Create a new event
CollectionAppend(&GcEvents, CollectionCreateNode(Key, Data));
SlimSemaphoreSignal(&GlbGcEventLock, 1);
return OsSuccess;
Expand All @@ -119,23 +110,19 @@ GcSignal(
* The event-handler thread */
void
GcWorker(
_In_Opt_ void* Args)
_In_Opt_ void* Args)
{
// Variables
CollectionItem_t *eNode = NULL;
GcHandler_t Handler;
int Run = 1;
CollectionItem_t* eNode;
GcHandler_t Handler;
int Run = 1;

// Unused arg
_CRT_UNUSED(Args);

// Run as long as the OS runs
while (Run) {
// Wait for next event
SlimSemaphoreWait(&GlbGcEventLock, 0);
eNode = CollectionPopFront(&GcEvents);

// Sanitize the node
eNode = CollectionPopFront(&GcEvents);
if (eNode == NULL) {
continue;
}
Expand All @@ -145,8 +132,6 @@ GcWorker(
CollectionDestroyNode(&GcEvents, eNode);
continue;
}

// Call the handler
Handler(eNode->Data);
CollectionDestroyNode(&GcEvents, eNode);
}
Expand Down
2 changes: 1 addition & 1 deletion kernel/handle.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
#include <heap.h>

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

static Collection_t Handles = COLLECTION_INIT(KeyInteger);
static _Atomic(UUId_t) IdGenerator = 1;
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 Ash;
UUId_t ProcessHandle;
UUId_t Thread;
Flags_t Flags;
int Source;
Expand Down
Loading

0 comments on commit f712fbd

Please sign in to comment.