Skip to content

Commit

Permalink
#163 changes to how memory spaces are created and managed in preparat…
Browse files Browse the repository at this point in the history
…ion for moving process management into userspace
  • Loading branch information
Meulengracht committed Nov 12, 2018
1 parent be4b3d7 commit 3e7e683
Show file tree
Hide file tree
Showing 16 changed files with 460 additions and 267 deletions.
6 changes: 1 addition & 5 deletions kernel/arch/x86/x32/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,7 @@ ContextCreate(
ExtraSegment = GDT_EXTRA_SEGMENT + 0x03;

// Now select the correct run-mode segments
if (THREADING_RUNMODE(ThreadFlags) == THREADING_DRIVERMODE) {
CodeSegment = GDT_PCODE_SEGMENT + 0x03;
StackSegment = DataSegment = GDT_PDATA_SEGMENT + 0x03;
}
else if (THREADING_RUNMODE(ThreadFlags) == THREADING_USERMODE) {
if (THREADING_RUNMODE(ThreadFlags) == THREADING_USERMODE) {
CodeSegment = GDT_UCODE_SEGMENT + 0x03;
StackSegment = DataSegment = GDT_UDATA_SEGMENT + 0x03;
}
Expand Down
2 changes: 1 addition & 1 deletion kernel/arch/x86/x32/memory/virtualmemory.c
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ CloneVirtualSpace(
// Create new resources for the happy new parent :-)
if (MemorySpaceParent == NULL) {
MemorySpace->Data[MEMORY_SPACE_IOMAP] = (uintptr_t)kmalloc(GDT_IOMAP_SIZE);
if (MemorySpace->Flags & (MEMORY_SPACE_APPLICATION | MEMORY_SPACE_SERVICE)) {
if (MemorySpace->Flags & MEMORY_SPACE_APPLICATION) {
memset((void*)MemorySpace->Data[MEMORY_SPACE_IOMAP], 0xFF, GDT_IOMAP_SIZE);
}
else {
Expand Down
6 changes: 1 addition & 5 deletions kernel/arch/x86/x64/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,7 @@ ContextCreate(
ExtraSegment = GDT_EXTRA_SEGMENT + 0x03;

// Now select the correct run-mode segments
if (THREADING_RUNMODE(ThreadFlags) == THREADING_DRIVERMODE) {
CodeSegment = GDT_UCODE_SEGMENT + 0x03;
StackSegment = DataSegment = GDT_UDATA_SEGMENT + 0x03;
}
else if (THREADING_RUNMODE(ThreadFlags) == THREADING_USERMODE) {
if (THREADING_RUNMODE(ThreadFlags) == THREADING_USERMODE) {
CodeSegment = GDT_UCODE_SEGMENT + 0x03;
StackSegment = DataSegment = GDT_UDATA_SEGMENT + 0x03;
}
Expand Down
2 changes: 1 addition & 1 deletion kernel/arch/x86/x64/memory/virtualmemory.c
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ CloneVirtualSpace(
// Create new resources for the happy new parent :-)
if (MemorySpaceParent == NULL) {
MemorySpace->Data[MEMORY_SPACE_IOMAP] = (uintptr_t)kmalloc(GDT_IOMAP_SIZE);
if (MemorySpace->Flags & (MEMORY_SPACE_APPLICATION | MEMORY_SPACE_SERVICE)) {
if (MemorySpace->Flags & MEMORY_SPACE_APPLICATION) {
memset((void*)MemorySpace->Data[MEMORY_SPACE_IOMAP], 0xFF, GDT_IOMAP_SIZE);
}
else {
Expand Down
6 changes: 4 additions & 2 deletions kernel/handle.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@
// Include all the systems that we have to cleanup
#include <process/process.h>
#include <memorybuffer.h>
#include <memoryspace.h>

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

/* CreateHandle
Expand Down Expand Up @@ -89,7 +91,7 @@ AcquireHandle(

PreviousReferences = atomic_fetch_add(&Instance->References, 1);
if (PreviousReferences == 0) {
// Special case, to fix race-conditioning. If the reference
// Special case, to prevent race-conditioning. If the reference
// count ever reach 0 this was called on cleanup.
return NULL;
}
Expand Down
1 change: 1 addition & 0 deletions kernel/include/handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
typedef enum _SystemHandleType {
HandleTypeMemoryBuffer = 0,
HandleTypeProcess,
HandleTypeMemorySpace,

HandleTypeCount
} SystemHandleType_t;
Expand Down
75 changes: 37 additions & 38 deletions kernel/include/memoryspace.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#define __MEMORY_SPACE_INTERFACE__

#include <os/osdefs.h>
#include <criticalsection.h>

/* SystemMemorySpace Definitions
* Definitions, bit definitions and magic constants for memory spaces */
Expand All @@ -35,7 +34,6 @@
* Definitions, bit definitions and magic constants for memory spaces */
#define MEMORY_SPACE_INHERIT 0x00000001
#define MEMORY_SPACE_APPLICATION 0x00000002
#define MEMORY_SPACE_SERVICE 0x00000004

/* SystemMemorySpace (Spaces) Definitions
* Definitions, bit definitions and magic constants for memory spaces */
Expand Down Expand Up @@ -64,16 +62,11 @@
#define MAPPING_LEGACY 0x80000000 // (Virtual) Mapping is for legacy memory devices
#define MAPPING_VMODE_MASK 0xF0000000

/* SystemMemorySpace
* Represents a virtual memory space that can be used with MMIO
* functionality on the architecture. */
typedef struct _SystemMemorySpace {
CriticalSection_t SyncObject;
UUId_t Id;
_Atomic(int) References;
Flags_t Flags;
uintptr_t Data[MEMORY_DATACOUNT];
struct _SystemMemorySpace* Parent;
UUId_t ParentHandle;
//blockmap spaces[max-spaces]
} SystemMemorySpace_t;

Expand All @@ -82,28 +75,28 @@ typedef struct _SystemMemorySpace {
* system memory space which is the default space the cpu should use for kernel operation. */
KERNELAPI OsStatus_t KERNELABI
InitializeSystemMemorySpace(
_In_ SystemMemorySpace_t* SystemMemorySpace);
_In_ SystemMemorySpace_t* SystemMemorySpace);

/* CreateSystemMemorySpace
* Initialize a new memory space, depending on what user is requesting we
* might recycle a already existing address space */
KERNELAPI SystemMemorySpace_t* KERNELABI
KERNELAPI OsStatus_t KERNELABI
CreateSystemMemorySpace(
_In_ Flags_t Flags);
_In_ Flags_t Flags,
_Out_ UUId_t* Handle);

/* ReleaseSystemMemorySpace
* Destroy and release all resources related to an address space,
* only if there is no more references */
/* DestroySystemMemorySpace
* Callback invoked by the handle system when references on a process reaches zero */
KERNELAPI OsStatus_t KERNELABI
ReleaseSystemMemorySpace(
_In_ SystemMemorySpace_t* SystemMemorySpace);
DestroySystemMemorySpace(
_In_ void* Resource);

/* SwitchSystemMemorySpace
* Switches the current address space out with the the address space provided
* for the current cpu */
KERNELAPI OsStatus_t KERNELABI
SwitchSystemMemorySpace(
_In_ SystemMemorySpace_t* SystemMemorySpace);
_In_ SystemMemorySpace_t* SystemMemorySpace);

/* GetCurrentSystemMemorySpace
* Returns the current address space if there is no active threads or threading
Expand All @@ -117,51 +110,57 @@ GetCurrentSystemMemorySpace(void);
KERNELAPI SystemMemorySpace_t* KERNELABI
GetDomainSystemMemorySpace(void);

/* GetCurrentSystemMemorySpaceHandle
* Returns the current address space if there is no active threads or threading
* is not setup it returns the kernel address space */
KERNELAPI UUId_t KERNELABI
GetCurrentSystemMemorySpaceHandle(void);

/* ChangeSystemMemorySpaceProtection
* Changes the protection parameters for the given memory region.
* The region must already be mapped and the size will be rounded up
* to a multiple of the page-size. */
KERNELAPI OsStatus_t KERNELABI
ChangeSystemMemorySpaceProtection(
_In_ SystemMemorySpace_t* SystemMemorySpace,
_InOut_Opt_ VirtualAddress_t VirtualAddress,
_In_ size_t Size,
_In_ Flags_t Flags,
_Out_ Flags_t* PreviousFlags);
_In_ SystemMemorySpace_t* SystemMemorySpace,
_InOut_Opt_ VirtualAddress_t VirtualAddress,
_In_ size_t Size,
_In_ Flags_t Flags,
_Out_ Flags_t* PreviousFlags);

/* CreateSystemMemorySpaceMapping
* Maps the given virtual address into the given address space
* uses the given physical pages instead of automatic allocation
* It returns the start address of the allocated physical region */
KERNELAPI OsStatus_t KERNELABI
CreateSystemMemorySpaceMapping(
_In_ SystemMemorySpace_t* SystemMemorySpace,
_InOut_Opt_ PhysicalAddress_t* PhysicalAddress,
_InOut_Opt_ VirtualAddress_t* VirtualAddress,
_In_ size_t Size,
_In_ Flags_t Flags,
_In_ uintptr_t Mask);
_In_ SystemMemorySpace_t* SystemMemorySpace,
_InOut_Opt_ PhysicalAddress_t* PhysicalAddress,
_InOut_Opt_ VirtualAddress_t* VirtualAddress,
_In_ size_t Size,
_In_ Flags_t Flags,
_In_ uintptr_t Mask);

/* CloneSystemMemorySpaceMapping
* Clones a region of memory mappings into the address space provided. The new mapping
* will automatically be marked PERSISTANT and PROVIDED. */
KERNELAPI OsStatus_t KERNELABI
CloneSystemMemorySpaceMapping(
_In_ SystemMemorySpace_t* SourceSpace,
_In_ SystemMemorySpace_t* DestinationSpace,
_In_ VirtualAddress_t SourceAddress,
_InOut_Opt_ VirtualAddress_t* DestinationAddress,
_In_ size_t Size,
_In_ Flags_t Flags,
_In_ uintptr_t Mask);
_In_ SystemMemorySpace_t* SourceSpace,
_In_ SystemMemorySpace_t* DestinationSpace,
_In_ VirtualAddress_t SourceAddress,
_InOut_Opt_ VirtualAddress_t* DestinationAddress,
_In_ size_t Size,
_In_ Flags_t Flags,
_In_ uintptr_t Mask);

/* RemoveSystemMemoryMapping
* Unmaps a virtual memory region from an address space */
KERNELAPI OsStatus_t KERNELABI
RemoveSystemMemoryMapping(
_In_ SystemMemorySpace_t* SystemMemorySpace,
_In_ VirtualAddress_t Address,
_In_ size_t Size);
_In_ SystemMemorySpace_t* SystemMemorySpace,
_In_ VirtualAddress_t Address,
_In_ size_t Size);

/* GetSystemMemoryMapping
* Retrieves a physical mapping from an address space determined
Expand Down
24 changes: 13 additions & 11 deletions kernel/include/threading.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,11 @@ typedef void(*ThreadEntry_t)(void*);
* runtime mode, which is either:
* 0 => Kernel
* 1 => User
* 2 => Driver
* 2 => Reserved
* 3 => Reserved
* Bit 3: If it's currently in switch-mode */
#define THREADING_KERNELMODE 0x00000000
#define THREADING_USERMODE 0x00000001
#define THREADING_DRIVERMODE 0x00000002
#define THREADING_SWITCHMODE 0x00000004
#define THREADING_MODEMASK 0x00000003
#define THREADING_RUNMODE(Flags) (Flags & THREADING_MODEMASK)
Expand Down Expand Up @@ -100,6 +99,7 @@ typedef struct _MCoreThread {

SystemPipe_t* Pipe;
SystemMemorySpace_t* MemorySpace;
UUId_t MemorySpaceHandle;

ThreadEntry_t Function;
void* Arguments;
Expand Down Expand Up @@ -135,15 +135,17 @@ ThreadingInitialize(void);
KERNELAPI OsStatus_t KERNELABI
ThreadingEnable(void);

/* ThreadingCreateThread
* Creates a new thread with the given paramaters and it is immediately
* queued up for execution. */
KERNELAPI UUId_t KERNELABI
ThreadingCreateThread(
_In_ const char* Name,
_In_ ThreadEntry_t Function,
_In_ void* Arguments,
_In_ Flags_t Flags);
/* CreateThread
* Creates a new thread that will execute the given function as soon as possible. The
* thread can be supplied with arguments, mode and a custom memory space. */
KERNELAPI OsStatus_t KERNELABI
CreateThread(
_In_ const char* Name,
_In_ ThreadEntry_t Function,
_In_ void* Arguments,
_In_ Flags_t Flags,
_In_ UUId_t MemorySpaceHandle,
_Out_ UUId_t* Handle);

/* ThreadingTerminateThread
* Marks the thread with the given id for finished, and it will be cleaned up
Expand Down
Loading

0 comments on commit 3e7e683

Please sign in to comment.