Skip to content

Commit

Permalink
Simplified interface of memoryspaces, finished the shared memory spac…
Browse files Browse the repository at this point in the history
…e contexts. Still need to update rest of code as a result. Then kernel changes are done
  • Loading branch information
Meulengracht committed Dec 17, 2018
1 parent bc37238 commit 4916a47
Show file tree
Hide file tree
Showing 25 changed files with 237 additions and 196 deletions.
4 changes: 2 additions & 2 deletions kernel/acpi/hpet.c
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,8 @@ HpInitialize(
HpetController.TickMinimum = Table->MinimumTick;

// Map the address
Status = CreateSystemMemorySpaceMapping(GetCurrentSystemMemorySpace(),
&HpetController.BaseAddress, &HpetController.BaseAddress, GetSystemMemoryPageSize(),
Status = CreateMemorySpaceMapping(GetCurrentMemorySpace(),
&HpetController.BaseAddress, &HpetController.BaseAddress, GetMemorySpacePageSize(),
MAPPING_PROVIDED | MAPPING_PERSISTENT | MAPPING_NOCACHE | MAPPING_KERNEL, __MASK);
if (Status != OsSuccess) {
ERROR("Failed to map address for hpet.");
Expand Down
6 changes: 3 additions & 3 deletions kernel/arch/x86/components/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ PageSynchronizationHandler(
_In_ void* Context)
{
// Variables
SystemMemorySpace_t *Current = GetCurrentSystemMemorySpace();
SystemMemorySpace_t *Current = GetCurrentMemorySpace();
_CRT_UNUSED(NotUsed);
_CRT_UNUSED(Context);

Expand Down Expand Up @@ -245,11 +245,11 @@ SynchronizePageRegion(
SyncData.ParentPagingData = NULL; // Everyone must update
}
else {
if (SystemMemorySpace->Parent == NULL) {
if (SystemMemorySpace->ParentHandle == UUID_INVALID) {
SyncData.ParentPagingData = SystemMemorySpace; // Children of us must update
}
else {
SyncData.ParentPagingData = SystemMemorySpace->Parent; // Parent and siblings!
SyncData.ParentPagingData = LookupHandle(SystemMemorySpace->ParentHandle); // Parent and siblings!
}
}
SyncData.Address = Address;
Expand Down
4 changes: 2 additions & 2 deletions kernel/arch/x86/components/smp.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ SmpApplicationCoreEntry(void)
IdtInstall();

// Switch into NUMA memory space if any, otherwise nothing happens
SwitchSystemMemorySpace(GetCurrentSystemMemorySpace());
SwitchMemorySpace(GetCurrentMemorySpace());
InitializeLocalApicForApplicationCore();

// Install the TSS before any multitasking
Expand Down Expand Up @@ -107,6 +107,6 @@ CpuSmpInitialize(void)
uint32_t EntryCode = (uint32_t)(uint32_t*)SmpApplicationCoreEntry;

*(CodePointer - 1) = EntryCode;
*(CodePointer - 2) = GetCurrentSystemMemorySpace()->Data[MEMORY_SPACE_CR3];
*(CodePointer - 2) = GetCurrentMemorySpace()->Data[MEMORY_SPACE_CR3];
memcpy((void*)MEMORY_LOCATION_TRAMPOLINE_CODE, (char*)__GlbTramplineCode, __GlbTramplineCode_length);
}
4 changes: 2 additions & 2 deletions kernel/arch/x86/components/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ ThreadingImpersonate(
Current->Flags |= THREADING_IMPERSONATION;
}
TssUpdateIo(Cpu, (uint8_t*)Thread->MemorySpace->Data[MEMORY_SPACE_IOMAP]);
SwitchSystemMemorySpace(Thread->MemorySpace);
SwitchMemorySpace(Thread->MemorySpace);
}

/* _GetNextRunnableThread
Expand Down Expand Up @@ -240,7 +240,7 @@ _GetNextRunnableThread(
Thread->Data[THREAD_DATA_FLAGS] &= ~X86_THREAD_USEDFPU; // Clear the FPU used flag

// Load thread-specific resources
SwitchSystemMemorySpace(Thread->MemorySpace);
SwitchMemorySpace(Thread->MemorySpace);
TssUpdateThreadStack(Cpu, (uintptr_t)Thread->Contexts[THREADING_CONTEXT_LEVEL0]);
TssUpdateIo(Cpu, (uint8_t*)Thread->MemorySpace->Data[MEMORY_SPACE_IOMAP]);
set_ts(); // Set task switch bit so we get faults on fpu instructions
Expand Down
8 changes: 4 additions & 4 deletions kernel/arch/x86/interrupts/apic/apicinit.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ ParseIoApic(

// Relocate the io-apic
Original = Controller->MemoryAddress;
CreateSystemMemorySpaceMapping(GetCurrentSystemMemorySpace(),
&Original, &Updated, GetSystemMemoryPageSize(),
CreateMemorySpaceMapping(GetCurrentMemorySpace(),
&Original, &Updated, GetMemorySpacePageSize(),
MAPPING_NOCACHE | MAPPING_KERNEL | MAPPING_PERSISTENT | MAPPING_PROVIDED, __MASK);
Controller->MemoryAddress = Updated + (Original & 0xFFF);

Expand Down Expand Up @@ -441,8 +441,8 @@ ApicInitialize(void)

// Perform the remap
TRACE(" > local apic at 0x%x", OriginalApAddress);
CreateSystemMemorySpaceMapping(GetCurrentSystemMemorySpace(),
&OriginalApAddress, &UpdatedApAddress, GetSystemMemoryPageSize(),
CreateMemorySpaceMapping(GetCurrentMemorySpace(),
&OriginalApAddress, &UpdatedApAddress, GetMemorySpacePageSize(),
MAPPING_NOCACHE | MAPPING_KERNEL | MAPPING_PERSISTENT | MAPPING_PROVIDED, __MASK);
GlbLocalApicBase = UpdatedApAddress + (OriginalApAddress & 0xFFF);
BspApicId = (ApicReadLocal(APIC_PROCESSOR_ID) >> 24) & 0xFF;
Expand Down
2 changes: 1 addition & 1 deletion kernel/arch/x86/interrupts/interrupts.c
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ ExceptionEntry(
// Bit 2 - write access
// Bit 4 - user/kernel
WRITELINE("page-fault address: 0x%x, error-code 0x%x", Address, Registers->ErrorCode);
WRITELINE("existing mapping for address: 0x%x", GetSystemMemoryMapping(GetCurrentSystemMemorySpace(), Address));
WRITELINE("existing mapping for address: 0x%x", GetMemorySpaceMapping(GetCurrentMemorySpace(), Address));
}

// Locate which module
Expand Down
2 changes: 1 addition & 1 deletion kernel/arch/x86/x32/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ ContextCreate(
}

// Map in the context
CreateSystemMemorySpaceMapping(GetCurrentSystemMemorySpace(), NULL, &ContextAddress,
CreateMemorySpaceMapping(GetCurrentMemorySpace(), NULL, &ContextAddress,
PAGE_SIZE, MAPPING_USERSPACE | MAPPING_FIXED, __MASK);
}
else {
Expand Down
11 changes: 6 additions & 5 deletions kernel/arch/x86/x32/memory/virtualmemory.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,15 @@ MmVirtualGetMasterTable(

// If there is no parent then we ignore it as we don't have to synchronize with kernel directory.
// We always have the shared page-tables mapped. The address must be below the thread-specific space
if (MemorySpace->Parent != NULL) {
if (MemorySpace->ParentHandle != UUID_INVALID) {
if (Address < MEMORY_LOCATION_RING3_THREAD_START) {
Parent = (PageDirectory_t*)MemorySpace->Parent->Data[MEMORY_SPACE_DIRECTORY];
SystemMemorySpace_t* MemorySpaceParent = (SystemMemorySpace_t*)LookupHandle(SystemMemorySpace->ParentHandle);
Parent = (PageDirectory_t*)MemorySpaceParent->Data[MEMORY_SPACE_DIRECTORY];
}
}

// Update the provided pointers
*IsCurrent = (MemorySpace == GetCurrentSystemMemorySpace()) ? 1 : 0;
*IsCurrent = (MemorySpace == GetCurrentMemorySpace()) ? 1 : 0;
*ParentDirectory = Parent;
return Directory;
}
Expand Down Expand Up @@ -219,7 +220,7 @@ CloneVirtualSpace(
_In_ int Inherit)
{
// Variables
PageDirectory_t *SystemDirectory = (PageDirectory_t*)GetDomainSystemMemorySpace()->Data[MEMORY_SPACE_DIRECTORY];
PageDirectory_t *SystemDirectory = (PageDirectory_t*)GetDomainMemorySpace()->Data[MEMORY_SPACE_DIRECTORY];
PageDirectory_t *ParentDirectory = NULL;
PageDirectory_t *PageDirectory;
uintptr_t PhysicalAddress;
Expand Down Expand Up @@ -332,7 +333,7 @@ DestroyVirtualSpace(
kfree(Pd);

// Free the resources allocated specifically for this
if (SystemMemorySpace->Parent == NULL) {
if (SystemMemorySpace->ParentHandle == UUID_INVALID) {
kfree((void*)SystemMemorySpace->Data[MEMORY_SPACE_IOMAP]);
}
return OsSuccess;
Expand Down
2 changes: 1 addition & 1 deletion kernel/arch/x86/x64/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ ContextCreate(
}

// Map in the context
CreateSystemMemorySpaceMapping(GetCurrentSystemMemorySpace(), NULL, &ContextAddress,
CreateMemorySpaceMapping(GetCurrentMemorySpace(), NULL, &ContextAddress,
PAGE_SIZE, MAPPING_USERSPACE | MAPPING_FIXED, __MASK);
}
else {
Expand Down
11 changes: 6 additions & 5 deletions kernel/arch/x86/x64/memory/virtualmemory.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,15 @@ MmVirtualGetMasterTable(

// If there is no parent then we ignore it as we don't have to synchronize with kernel directory.
// We always have the shared page-tables mapped. The address must be below the thread-specific space
if (MemorySpace->Parent != NULL) {
if (MemorySpace->ParentHandle != UUID_INVALID) {
if (Address < MEMORY_LOCATION_RING3_THREAD_START) {
Parent = (PageMasterTable_t*)MemorySpace->Parent->Data[MEMORY_SPACE_DIRECTORY];
SystemMemorySpace_t* MemorySpaceParent = (SystemMemorySpace_t*)LookupHandle(SystemMemorySpace->ParentHandle);
Parent = (PageMasterTable_t*)MemorySpaceParent->Parent->Data[MEMORY_SPACE_DIRECTORY];
}
}

// Update the provided pointers
*IsCurrent = (MemorySpace == GetCurrentSystemMemorySpace()) ? 1 : 0;
*IsCurrent = (MemorySpace == GetCurrentMemorySpace()) ? 1 : 0;
*ParentDirectory = Parent;
return Directory;
}
Expand Down Expand Up @@ -327,7 +328,7 @@ CloneVirtualSpace(
PageDirectoryTable_t *SystemDirectoryTable = NULL;
PageDirectoryTable_t *DirectoryTable = NULL;
PageDirectory_t *Directory = NULL;
PageMasterTable_t *SystemMasterTable = (PageMasterTable_t*)GetDomainSystemMemorySpace()->Data[MEMORY_SPACE_DIRECTORY];
PageMasterTable_t *SystemMasterTable = (PageMasterTable_t*)GetDomainMemorySpace()->Data[MEMORY_SPACE_DIRECTORY];
PageMasterTable_t *ParentMasterTable = NULL;
PageMasterTable_t *PageMasterTable;
uintptr_t PhysicalAddress;
Expand Down Expand Up @@ -511,7 +512,7 @@ DestroyVirtualSpace(
kfree(Current);

// Free the resources allocated specifically for this
if (SystemMemorySpace->Parent == NULL) {
if (SystemMemorySpace->ParentHandle == UUID_INVALID) {
kfree((void*)SystemMemorySpace->Data[MEMORY_SPACE_IOMAP]);
}
return OsSuccess;
Expand Down
26 changes: 13 additions & 13 deletions kernel/debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,21 +230,21 @@ DebugStackTrace(
// Derive stack pointer from the argument
uintptr_t* StackPtr;
uintptr_t StackLmt;
uintptr_t PageMask = ~(GetSystemMemoryPageSize() - 1);
uintptr_t PageMask = ~(GetMemorySpacePageSize() - 1);
size_t Itr = MaxFrames;

// Use local or given?
if (Context == NULL) {
StackPtr = (uintptr_t*)&MaxFrames;
StackLmt = ((uintptr_t)StackPtr & PageMask) + GetSystemMemoryPageSize();
StackLmt = ((uintptr_t)StackPtr & PageMask) + GetMemorySpacePageSize();
}
else if (CONTEXT_USERSP(Context) != 0) {
StackPtr = (uintptr_t*)CONTEXT_USERSP(Context);
StackLmt = (CONTEXT_USERSP(Context) & PageMask) + GetSystemMemoryPageSize();
StackLmt = (CONTEXT_USERSP(Context) & PageMask) + GetMemorySpacePageSize();
}
else {
StackPtr = (uintptr_t*)CONTEXT_SP(Context);
StackLmt = (CONTEXT_SP(Context) & PageMask) + GetSystemMemoryPageSize();
StackLmt = (CONTEXT_SP(Context) & PageMask) + GetMemorySpacePageSize();
}

while (Itr && (uintptr_t)StackPtr < StackLmt) {
Expand Down Expand Up @@ -336,7 +336,7 @@ DebugPageMemorySpaceHandlers(
_In_ Context_t* Context,
_In_ uintptr_t Address)
{
SystemMemorySpace_t* Space = GetCurrentSystemMemorySpace();
SystemMemorySpace_t* Space = GetCurrentMemorySpace();
OsStatus_t Status = OsError;

foreach(Node, Space->MemoryHandlers) {
Expand All @@ -357,7 +357,7 @@ DebugPageFaultProcessHeapMemory(
_In_ Context_t* Context,
_In_ uintptr_t Address)
{
SystemMemorySpace_t* Space = GetCurrentSystemMemorySpace();
SystemMemorySpace_t* Space = GetCurrentMemorySpace();
Flags_t PageFlags = MAPPING_USERSPACE | MAPPING_FIXED;

if (Space->HeapSpace != NULL) {
Expand All @@ -370,10 +370,10 @@ DebugPageFaultProcessHeapMemory(
if (BlockBitmapValidateState(Space->HeapSpace, Address, 1) == OsSuccess) {
uintptr_t ExistingPhysical = ValidateDeviceIoMemoryAddress(Address);
if (ExistingPhysical != 0) {
return CreateSystemMemorySpaceMapping(Space, &ExistingPhysical, &Address,
GetSystemMemoryPageSize(), PageFlags | MAPPING_NOCACHE | MAPPING_PROVIDED, __MASK);
return CreateMemorySpaceMapping(Space, &ExistingPhysical, &Address,
GetMemorySpacePageSize(), PageFlags | MAPPING_NOCACHE | MAPPING_PROVIDED, __MASK);
}
return CreateSystemMemorySpaceMapping(Space, NULL, &Address, GetSystemMemoryPageSize(), PageFlags, __MASK);
return CreateMemorySpaceMapping(Space, NULL, &Address, GetMemorySpacePageSize(), PageFlags, __MASK);
}
}
return OsSuccess;
Expand All @@ -387,8 +387,8 @@ DebugPageFaultKernelHeapMemory(
_In_ uintptr_t Address)
{
if (HeapValidateAddress(HeapGetKernel(), Address) == OsSuccess) {
return CreateSystemMemorySpaceMapping(GetCurrentSystemMemorySpace(), NULL, &Address,
GetSystemMemoryPageSize(), MAPPING_FIXED, __MASK);
return CreateMemorySpaceMapping(GetCurrentMemorySpace(), NULL, &Address,
GetMemorySpacePageSize(), MAPPING_FIXED, __MASK);
}
return OsError;
}
Expand All @@ -400,8 +400,8 @@ DebugPageFaultThreadMemory(
_In_ Context_t* Context,
_In_ uintptr_t Address)
{
return CreateSystemMemorySpaceMapping(GetCurrentSystemMemorySpace(), NULL, &Address,
GetSystemMemoryPageSize(), MAPPING_USERSPACE | MAPPING_FIXED, __MASK);
return CreateMemorySpaceMapping(GetCurrentMemorySpace(), NULL, &Address,
GetMemorySpacePageSize(), MAPPING_USERSPACE | MAPPING_FIXED, __MASK);
}

/* DebugInstallPageFaultHandlers
Expand Down
22 changes: 11 additions & 11 deletions kernel/deviceio.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ AcquireSystemDeviceIo(
case DeviceIoMemoryBased: {
uintptr_t BaseAddress = SystemIo->Io.Access.Memory.PhysicalBase;
uintptr_t MappedAddress;
size_t PageSize = GetSystemMemoryPageSize();
size_t PageSize = GetMemorySpacePageSize();
size_t Length = SystemIo->Io.Access.Memory.Length + (BaseAddress % PageSize);

MappedAddress = AllocateBlocksInBlockmap(GetCurrentSystemMemorySpace()->HeapSpace, __MASK, Length);
MappedAddress = AllocateBlocksInBlockmap(GetCurrentMemorySpace()->HeapSpace, __MASK, Length);
if (MappedAddress == 0) {
ERROR(" > failed to allocate heap memory for mapping");
break;
Expand All @@ -136,7 +136,7 @@ AcquireSystemDeviceIo(

case DeviceIoPortBased: {
for (size_t i = 0; i < SystemIo->Io.Access.Port.Length; i++) {
SetDirectIoAccess(CoreId, GetCurrentSystemMemorySpace(), ((uint16_t)(SystemIo->Io.Access.Port.Base + i)), 1);
SetDirectIoAccess(CoreId, GetCurrentMemorySpace(), ((uint16_t)(SystemIo->Io.Access.Port.Base + i)), 1);
}
return OsSuccess;
} break;
Expand Down Expand Up @@ -182,15 +182,15 @@ ReleaseSystemDeviceIo(
switch (SystemIo->Io.Type) {
case DeviceIoMemoryBased: {
uintptr_t BaseAddress = SystemIo->Io.Access.Memory.PhysicalBase;
size_t PageSize = GetSystemMemoryPageSize();
size_t PageSize = GetMemorySpacePageSize();
size_t Length = SystemIo->Io.Access.Memory.Length + (BaseAddress % PageSize);
ReleaseBlockmapRegion(GetCurrentSystemMemorySpace()->HeapSpace, SystemIo->MappedAddress, Length);
RemoveSystemMemoryMapping(GetCurrentSystemMemorySpace(), SystemIo->MappedAddress, Length);
ReleaseBlockmapRegion(GetCurrentMemorySpace()->HeapSpace, SystemIo->MappedAddress, Length);
RemoveMemorySpaceMapping(GetCurrentMemorySpace(), SystemIo->MappedAddress, Length);
} break;

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

Expand Down Expand Up @@ -224,9 +224,9 @@ CreateKernelSystemDeviceIo(
switch (SystemIo->Io.Type) {
case DeviceIoMemoryBased: {
uintptr_t BaseAddress = SystemIo->Io.Access.Memory.PhysicalBase;
size_t PageSize = GetSystemMemoryPageSize();
size_t PageSize = GetMemorySpacePageSize();
size_t Length = SystemIo->Io.Access.Memory.Length + (BaseAddress % PageSize);
OsStatus_t Status = CreateSystemMemorySpaceMapping(GetCurrentSystemMemorySpace(),
OsStatus_t Status = CreateMemorySpaceMapping(GetCurrentMemorySpace(),
&BaseAddress, &SystemIo->Io.Access.Memory.VirtualBase, Length,
MAPPING_NOCACHE | MAPPING_PROVIDED | MAPPING_KERNEL | MAPPING_PERSISTENT, __MASK);
if (Status != OsSuccess) {
Expand Down Expand Up @@ -261,9 +261,9 @@ ReleaseKernelSystemDeviceIo(
switch (SystemIo->Io.Type) {
case DeviceIoMemoryBased: {
uintptr_t BaseAddress = SystemIo->Io.Access.Memory.PhysicalBase;
size_t PageSize = GetSystemMemoryPageSize();
size_t PageSize = GetMemorySpacePageSize();
size_t Length = SystemIo->Io.Access.Memory.Length + (BaseAddress % PageSize);
RemoveSystemMemoryMapping(GetCurrentSystemMemorySpace(), SystemIo->Io.Access.Memory.VirtualBase, Length);
RemoveMemorySpaceMapping(GetCurrentMemorySpace(), SystemIo->Io.Access.Memory.VirtualBase, Length);
SystemIo->Io.Access.Memory.VirtualBase = 0;
} break;

Expand Down
2 changes: 1 addition & 1 deletion kernel/handle.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ static _Atomic(UUId_t) IdGenerator = 1;
static HandleDestructorFn HandleDestructors[HandleTypeCount] = {
NULL, // Generic - Ignore
DestroyMemoryBuffer,
DestroySystemMemorySpace,
DestroyMemorySpace,
DestroySystemPipe
};

Expand Down
Loading

0 comments on commit 4916a47

Please sign in to comment.