Skip to content

Commit

Permalink
Only thing left now is to update the process sys calls, and restructu…
Browse files Browse the repository at this point in the history
…re the syscall table! Then we can start testing again
  • Loading branch information
Meulengracht committed Dec 13, 2018
1 parent 712d07e commit 6f0eaa8
Show file tree
Hide file tree
Showing 21 changed files with 249 additions and 279 deletions.
2 changes: 1 addition & 1 deletion kernel/arch/x86/components/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,6 @@ _ThreadingSwitch(
set_ts(); // Set task switch bit so we get faults on fpu instructions

// Handle any signals pending for thread
SignalHandle(Thread->Id);
SignalProcess(Thread->Id);
return Context;
}
136 changes: 57 additions & 79 deletions kernel/debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,17 @@
#define __MODULE "DBGI"
//#define __TRACE

#include "../librt/libds/pe/pe.h"
#include <modules/manager.h>
#include <system/utils.h>
#include <memoryspace.h>
#include <scheduler.h>
#include <interrupts.h>
#include <deviceio.h>
#include <machine.h>
#include <handle.h>
#include <stdio.h>
#include <debug.h>
#include <heap.h>
#include <arch.h>

/* Page-fault handlers for different page-fault areas.
* Static storage to only allow a maximum handlers. */
Expand All @@ -48,17 +49,10 @@ static struct MCorePageFaultHandler_t {
* to this event handler */
OsStatus_t
DebugSingleStep(
_In_ Context_t *Context)
_In_ Context_t* Context)
{
// Variables

// Trace
TRACE("DebugSingleStep(IP 0x%x)", CONTEXT_IP(Context));
// @todo

_CRT_UNUSED(Context);

// Done
return OsSuccess;
}

Expand All @@ -68,17 +62,10 @@ DebugSingleStep(
* to this event handler */
OsStatus_t
DebugBreakpoint(
_In_ Context_t *Context)
_In_ Context_t* Context)
{
// Variables

// Trace
TRACE("DebugBreakpoint(IP 0x%x)", CONTEXT_IP(Context));
// @todo

_CRT_UNUSED(Context);

// Done
return OsSuccess;
}

Expand All @@ -91,7 +78,6 @@ DebugPageFault(
_In_ Context_t* Context,
_In_ uintptr_t Address)
{
// Trace
TRACE("DebugPageFault(IP 0x%x, Address 0x%x)", CONTEXT_IP(Context), Address);
for (int i = 0; i < 8; i++) {
if (PageFaultHandlers[i].AreaHandler == NULL) {
Expand Down Expand Up @@ -131,10 +117,10 @@ DebugHaltAllProcessorCores(
* return again */
OsStatus_t
DebugPanic(
_In_ int FatalityScope,
_In_ Context_t* Context,
_In_ const char* Module,
_In_ const char* Message, ...)
_In_ int FatalityScope,
_In_ Context_t* Context,
_In_ const char* Module,
_In_ const char* Message, ...)
{
MCoreThread_t *CurrentThread;
char MessageBuffer[256];
Expand Down Expand Up @@ -174,8 +160,6 @@ DebugPanic(
}
}
ThreadingDebugPrint();

// Stack trace
DebugStackTrace(Context, 8);

// Handle based on the scope of the fatality
Expand Down Expand Up @@ -205,7 +189,8 @@ DebugGetModuleByAddress(
_Out_ char** Name)
{
// Validate that the address is within userspace
if (Address >= MEMORY_LOCATION_RING3_CODE && Address < MEMORY_LOCATION_RING3_CODE_END) {
if (Address >= GetMachine()->MemoryMap.UserCode.Start &&
Address < (GetMachine()->MemoryMap.UserCode.Start + GetMachine()->MemoryMap.UserCode.Length)) {
// Sanitize whether or not a process was running
if (Module != NULL && Module->Executable != NULL) {
uintptr_t PmBase = Module->Executable->VirtualAddress;
Expand Down Expand Up @@ -243,10 +228,10 @@ DebugStackTrace(
_In_ size_t MaxFrames)
{
// Derive stack pointer from the argument
uintptr_t *StackPtr = NULL;
uintptr_t StackLmt = 0;
uintptr_t PageMask = ~(GetSystemMemoryPageSize() - 1);
size_t Itr = MaxFrames;
uintptr_t* StackPtr;
uintptr_t StackLmt;
uintptr_t PageMask = ~(GetSystemMemoryPageSize() - 1);
size_t Itr = MaxFrames;

// Use local or given?
if (Context == NULL) {
Expand All @@ -255,7 +240,7 @@ DebugStackTrace(
}
else if (CONTEXT_USERSP(Context) != 0) {
StackPtr = (uintptr_t*)CONTEXT_USERSP(Context);
StackLmt = MEMORY_LOCATION_RING3_STACK_START;
StackLmt = (CONTEXT_USERSP(Context) & PageMask) + GetSystemMemoryPageSize();
}
else {
StackPtr = (uintptr_t*)CONTEXT_SP(Context);
Expand All @@ -266,9 +251,15 @@ DebugStackTrace(
uintptr_t Value = StackPtr[0];
uintptr_t Base = 0;
char *Name = NULL;
if (DebugGetModuleByAddress(GetCurrentModule(), Value, &Base, &Name) == OsSuccess) {
uintptr_t Diff = Value - Base;
WRITELINE("%u - 0x%x (%s)", MaxFrames - Itr, Diff, Name);
if (Value >= GetMachine()->MemoryMap.UserCode.Start &&
Value < (GetMachine()->MemoryMap.UserCode.Start + GetMachine()->MemoryMap.UserCode.Length)) {
if (DebugGetModuleByAddress(GetCurrentModule(), Value, &Base, &Name) == OsSuccess) {
WRITELINE("%u - 0x%x (%s)", MaxFrames - Itr, (Value - Base), Name);

}
else {
WRITELINE("%u - 0x%x", MaxFrames - Itr, Value);
}
Itr--;
}
StackPtr++;
Expand Down Expand Up @@ -338,65 +329,40 @@ DebugMemory(
return OsSuccess;
}

/* DebugPageFaultKernelHeapMemory
* Checks for memory access that was (kernel) heap related and valid */
/* DebugPageMemorySpaceHandlers
* Checks for memory access that was memory handler related and valid */
OsStatus_t
DebugPageFaultKernelHeapMemory(
DebugPageMemorySpaceHandlers(
_In_ Context_t* Context,
_In_ uintptr_t Address)
{
if (HeapValidateAddress(HeapGetKernel(), Address) == OsSuccess) {
// Try to map it in and return the result
return CreateSystemMemorySpaceMapping(GetCurrentSystemMemorySpace(), NULL, &Address,
GetSystemMemoryPageSize(), MAPPING_FIXED, __MASK);
}
return OsError;
}

/* DebugPageFaultFileMappings
* Checks for memory access that was file mapped related and valid */
OsStatus_t
DebugPageFaultFileMappings(
_In_ Context_t* Context,
_In_ uintptr_t Address)
{
SystemMemorySpace_t* Space = GetCurrentSystemMemorySpace();
SystemFileMappingEvent_t* Event;
SystemFileMapping_t* Mapping;

// Iterate file-mappings
foreach(Node, Space->FileMappings) {
Mapping = (SystemFileMapping_t*)Node->Data;
if (ISINRANGE(Address, Mapping->BufferObject.Address, (Mapping->BufferObject.Address + Mapping->Length) - 1)) {
// Oh, woah, file-mapping
Event = (SystemFileMappingEvent_t*)kmalloc(sizeof(SystemFileMappingEvent_t));
Event->MemorySpace = Space;
Event->Address = Address;

RegisterFileMappingEvent(Event);
SchedulerThreadSleep((uintptr_t*)Event, 0);
if (Event->Result != OsSuccess) {
// what? @todo
}
kfree(Event);
return OsSuccess; // Indicate event was handled
SystemMemorySpace_t* Space = GetCurrentSystemMemorySpace();
OsStatus_t Status = OsError;

foreach(Node, Space->MemoryHandlers) {
SystemMemoryMappingHandler_t* Handler = (SystemMemoryMappingHandler_t*)Node;
if (ISINRANGE(Address, Handler->Address, (Handler->Address + Handler->Length) - 1)) {
__KernelInterruptDriver(__SESSIONMANAGER_TARGET, 0, (void*)Handler->Handle);
Status = WaitForHandles(&Handler->Handle, 1, 1, 0);
break;
}
}
return OsError;
return Status;
}

/* DebugPageFaultHeapMemory
/* DebugPageFaultProcessHeapMemory
* Checks for memory access that was (process) heap related and valid */
OsStatus_t
DebugPageFaultHeapMemory(
DebugPageFaultProcessHeapMemory(
_In_ Context_t* Context,
_In_ uintptr_t Address)
{
SystemMemorySpace_t* Space = GetCurrentSystemMemorySpace();
Flags_t PageFlags = MAPPING_USERSPACE | MAPPING_FIXED;

if (Space->HeapSpace != NULL) {
if (DebugPageFaultFileMappings(Context, Address) == OsSuccess) {
if (Space->MemoryHandlers != NULL &&
DebugPageMemorySpaceHandlers(Context, Address) == OsSuccess) {
return OsSuccess;
}

Expand All @@ -413,14 +379,27 @@ DebugPageFaultHeapMemory(
return OsSuccess;
}

/* DebugPageFaultKernelHeapMemory
* Checks for memory access that was (kernel) heap related and valid */
OsStatus_t
DebugPageFaultKernelHeapMemory(
_In_ Context_t* Context,
_In_ uintptr_t Address)
{
if (HeapValidateAddress(HeapGetKernel(), Address) == OsSuccess) {
return CreateSystemMemorySpaceMapping(GetCurrentSystemMemorySpace(), NULL, &Address,
GetSystemMemoryPageSize(), MAPPING_FIXED, __MASK);
}
return OsError;
}

/* DebugPageFaultThreadMemory
* Checks for memory access that was io-space related and valid */
OsStatus_t
DebugPageFaultThreadMemory(
_In_ Context_t* Context,
_In_ uintptr_t Address)
{
// Try to map it in and return the result
return CreateSystemMemorySpaceMapping(GetCurrentSystemMemorySpace(), NULL, &Address,
GetSystemMemoryPageSize(), MAPPING_USERSPACE | MAPPING_FIXED, __MASK);
}
Expand All @@ -431,7 +410,6 @@ OsStatus_t
DebugInstallPageFaultHandlers(
_In_ SystemMemoryMap_t* MemoryMap)
{
// Debug
TRACE("DebugInstallPageFaultHandlers()");

// Heap memory handler
Expand All @@ -442,7 +420,7 @@ DebugInstallPageFaultHandlers(
// Process heap memory handler
PageFaultHandlers[1].AreaStart = MemoryMap->UserHeap.Start;
PageFaultHandlers[1].AreaEnd = MemoryMap->UserHeap.Start + MemoryMap->UserHeap.Length;
PageFaultHandlers[1].AreaHandler = DebugPageFaultHeapMemory;
PageFaultHandlers[1].AreaHandler = DebugPageFaultProcessHeapMemory;

// Thread-specific memory handler
PageFaultHandlers[2].AreaStart = MemoryMap->ThreadArea.Start;
Expand Down
26 changes: 21 additions & 5 deletions kernel/handle.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
static Collection_t SystemHandles = COLLECTION_INIT(KeyId);
static _Atomic(UUId_t) IdGenerator = 1;
static HandleDestructorFn HandleDestructors[HandleTypeCount] = {
NULL, // Generic - Ignore
DestroyMemoryBuffer,
DestroySystemMemorySpace,
DestroySystemPipe
Expand Down Expand Up @@ -143,21 +144,36 @@ DestroyHandle(
SchedulerHandleSignalAll((uintptr_t*)Handle);
ThreadingYield();
}
Status = HandleDestructors[Instance->Type](Instance->Resource);
Status = (HandleDestructors[Instance->Type] != NULL) ? HandleDestructors[Instance->Type](Instance->Resource) : OsSuccess;
kfree(Instance);
}
return Status;
}

/* SignalHandle
* Signals a handle and wakes a given number of sleepers. */
OsStatus_t
SignalHandle(
_In_ UUId_t Handle,
_In_ int Count)
{
for (int i = 0; i < Count; i++) {
if (SchedulerHandleSignal((uintptr_t*)Handle) != OsSuccess) {
return OsError;
}
}
return OsSuccess;
}

/* WaitForHandles
* Waits for either of the given handles to signal. The handles that are passed must
* support the SYNCHRONIZE capability to be waited for. */
OsStatus_t
WaitForHandles(
_In_ UUId_t* Handles,
_In_ size_t HandleCount,
_In_ int WaitForAll,
_In_ size_t Timeout)
_In_ UUId_t* Handles,
_In_ size_t HandleCount,
_In_ int WaitForAll,
_In_ size_t Timeout)
{
// @todo multi sync in scheduler
assert(Handles != NULL);
Expand Down
32 changes: 20 additions & 12 deletions kernel/include/handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
#include <ds/collection.h>

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

Expand All @@ -53,31 +54,38 @@ typedef struct _SystemHandle {
* Allocates a new handle for a system resource with a reference of 1. */
KERNELAPI UUId_t KERNELABI
CreateHandle(
_In_ SystemHandleType_t Type,
_In_ SystemHandleCapability_t Capabilities,
_In_ void* Resource);
_In_ SystemHandleType_t Type,
_In_ SystemHandleCapability_t Capabilities,
_In_ void* Resource);

/* DestroyHandle
* Reduces the reference count of the given handle, and cleans up the handle on
* reaching 0 references. */
KERNELAPI OsStatus_t KERNELABI
DestroyHandle(
_In_ UUId_t Handle);

/* AcquireHandle
* Acquires the handle given for the calling process. This can fail if the handle
* turns out to be invalid, otherwise the resource will be returned. */
KERNELAPI void* KERNELABI
AcquireHandle(
_In_ UUId_t Handle);
_In_ UUId_t Handle);

/* LookupHandle
* Retrieves the handle given for the calling process. This can fail if the handle
* turns out to be invalid, otherwise the resource will be returned. */
KERNELAPI void* KERNELABI
LookupHandle(
_In_ UUId_t Handle);
_In_ UUId_t Handle);

/* DestroyHandle
* Reduces the reference count of the given handle, and cleans up the handle on
* reaching 0 references. */
/* SignalHandle
* Signals a handle and wakes a given number of sleepers. */
KERNELAPI OsStatus_t KERNELABI
DestroyHandle(
_In_ UUId_t Handle);

SignalHandle(
_In_ UUId_t Handle,
_In_ int Count);

/* WaitForHandles
* Waits for either of the given handles to signal. The handles that are passed must
* support the SYNCHRONIZE capability to be waited for. */
Expand Down
Loading

0 comments on commit 6f0eaa8

Please sign in to comment.