Skip to content

Commit

Permalink
introduce new flexible memory allocator
Browse files Browse the repository at this point in the history
Previous "custom" allocator was non-replaceable and hard-coded.
The "custom allocator" term has been replaced with "hypervisor allocator", as it more closely reflects when is this allocator used.

Most importantly, this change allows anyone to write their own allocator/heap manager (or use existing one, such as dlmalloc) and bind it as the hypervisor allocator.
  • Loading branch information
wbenny committed Aug 1, 2019
1 parent 82b7070 commit d8eb913
Show file tree
Hide file tree
Showing 16 changed files with 927 additions and 511 deletions.
7 changes: 6 additions & 1 deletion src/hvpp/hvpp.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@
<ClCompile Include="hvpp\hvpp.cpp" />
<ClCompile Include="hvpp\hypervisor.cpp" />
<ClCompile Include="hvpp\ia32\memory.cpp" />
<ClCompile Include="hvpp\lib\mm\memory_allocator\hypervisor_memory_allocator.cpp" />
<ClCompile Include="hvpp\lib\mm\memory_allocator\system_memory_allocator.cpp" />
<ClCompile Include="hvpp\lib\mm\memory_allocator\win32\system_memory_allocator.cpp" />
<ClCompile Include="hvpp\lib\mm\memory_mapper.cpp" />
<ClCompile Include="hvpp\lib\mm\memory_translator.cpp" />
<ClCompile Include="hvpp\lib\mm\win32\memory_mapper.cpp" />
Expand All @@ -128,7 +131,6 @@
<ClCompile Include="hvpp\lib\win32\debugger.cpp" />
<ClCompile Include="hvpp\lib\win32\device.cpp" />
<ClCompile Include="hvpp\lib\win32\log.cpp" />
<ClCompile Include="hvpp\lib\win32\mm.cpp" />
<ClCompile Include="hvpp\lib\win32\mp.cpp" />
<ClCompile Include="hvpp\lib\win32\tracelog.cpp">
<ConformanceMode Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</ConformanceMode>
Expand All @@ -146,6 +148,9 @@
<ClInclude Include="hvpp\config.h" />
<ClInclude Include="hvpp\ept.h" />
<ClInclude Include="hvpp\hypervisor.h" />
<ClInclude Include="hvpp\lib\mm\memory_allocator.h" />
<ClInclude Include="hvpp\lib\mm\memory_allocator\hypervisor_memory_allocator.h" />
<ClInclude Include="hvpp\lib\mm\memory_allocator\system_memory_allocator.h" />
<ClInclude Include="hvpp\lib\mm\memory_mapper.h" />
<ClInclude Include="hvpp\lib\mm\memory_translator.h" />
<ClInclude Include="hvpp\lib\mm\mtrr_descriptor.h" />
Expand Down
30 changes: 27 additions & 3 deletions src/hvpp/hvpp.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@
<Filter Include="Source Files\hvpp\lib\mm\win32">
<UniqueIdentifier>{c53a048f-d7da-48c6-88a5-fa1ed9bfc1ed}</UniqueIdentifier>
</Filter>
<Filter Include="Header Files\hvpp\lib\mm\memory_allocator">
<UniqueIdentifier>{feefc873-6b26-444a-a474-21d23ab84328}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\hvpp\lib\mm\memory_allocator">
<UniqueIdentifier>{63686d7b-f1d9-4d9b-85af-efede73a8e9f}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\hvpp\lib\mm\memory_allocator\win32">
<UniqueIdentifier>{095769d8-145a-47c6-abfa-d8a363b91f9b}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="hvpp\lib\mm.cpp">
Expand Down Expand Up @@ -114,9 +123,6 @@
<ClCompile Include="hvpp\lib\log.cpp">
<Filter>Source Files\hvpp\lib</Filter>
</ClCompile>
<ClCompile Include="hvpp\lib\win32\mm.cpp">
<Filter>Source Files\hvpp\lib\win32</Filter>
</ClCompile>
<ClCompile Include="hvpp\lib\driver.cpp">
<Filter>Source Files\hvpp\lib</Filter>
</ClCompile>
Expand Down Expand Up @@ -162,6 +168,15 @@
<ClCompile Include="hvpp\lib\mm\win32\physical_memory_descriptor.cpp">
<Filter>Source Files\hvpp\lib\mm\win32</Filter>
</ClCompile>
<ClCompile Include="hvpp\lib\mm\memory_allocator\hypervisor_memory_allocator.cpp">
<Filter>Source Files\hvpp\lib\mm\memory_allocator</Filter>
</ClCompile>
<ClCompile Include="hvpp\lib\mm\memory_allocator\system_memory_allocator.cpp">
<Filter>Source Files\hvpp\lib\mm\memory_allocator</Filter>
</ClCompile>
<ClCompile Include="hvpp\lib\mm\memory_allocator\win32\system_memory_allocator.cpp">
<Filter>Source Files\hvpp\lib\mm\memory_allocator\win32</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="hvpp\lib\bitmap.h">
Expand Down Expand Up @@ -344,6 +359,15 @@
<ClInclude Include="hvpp\lib\deque.h">
<Filter>Header Files\hvpp\lib</Filter>
</ClInclude>
<ClInclude Include="hvpp\lib\mm\memory_allocator\hypervisor_memory_allocator.h">
<Filter>Header Files\hvpp\lib\mm\memory_allocator</Filter>
</ClInclude>
<ClInclude Include="hvpp\lib\mm\memory_allocator\system_memory_allocator.h">
<Filter>Header Files\hvpp\lib\mm\memory_allocator</Filter>
</ClInclude>
<ClInclude Include="hvpp\lib\mm\memory_allocator.h">
<Filter>Header Files\hvpp\lib\mm</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<MASM Include="hvpp\ia32\context.asm">
Expand Down
15 changes: 15 additions & 0 deletions src/hvpp/hvpp/hvpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,19 @@ HvppInitialize(
return STATUS_INSUFFICIENT_RESOURCES;
}

if (auto err = driver::common::system_allocator_default_initialize())
{
driver::common::destroy();
return STATUS_INSUFFICIENT_RESOURCES;
}

if (auto err = driver::common::hypervisor_allocator_default_initialize())
{
driver::common::system_allocator_default_destroy();
driver::common::destroy();
return STATUS_INSUFFICIENT_RESOURCES;
}

return STATUS_SUCCESS;
}

Expand All @@ -232,6 +245,8 @@ HvppDestroy(
//

driver::common::destroy();
driver::common::hypervisor_allocator_default_destroy();
driver::common::system_allocator_default_destroy();
}

NTSTATUS
Expand Down
25 changes: 24 additions & 1 deletion src/hvpp/hvpp/hypervisor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "ia32/cpuid/cpuid_eax_01.h"
#include "lib/assert.h"
#include "lib/driver.h"
#include "lib/log.h"
#include "lib/mm.h"
#include "lib/mp.h"
Expand Down Expand Up @@ -102,6 +103,28 @@ namespace hvpp::hypervisor
vcpu_t(handler);
});

//
// Check if hypervisor-allocator has been set.
//
// If the driver is calling `hypervisor::start()' from
// the `driver::initialize()' method without manually
// setting the allocator, then the allocator will be empty.
//
if (!mm::hypervisor_allocator())
{
//
// Hypervisor-allocator has not been set - create default one.
//
// Note:
// Default hypervisor allocator is automatically destroyed
// in the `driver::common::destroy()' function.
//
if (auto err = driver::common::hypervisor_allocator_default_initialize())
{
return err;
}
}

//
// Check that CPU supports all required features to
// run this hypervisor.
Expand Down Expand Up @@ -160,7 +183,7 @@ namespace hvpp::hypervisor
// Destroy array of VCPUs.
//
std::destroy_n(global.vcpu_list, mp::cpu_count());
delete global.vcpu_list;
delete static_cast<void*>(global.vcpu_list);

global.vcpu_list = nullptr;

Expand Down
Loading

0 comments on commit d8eb913

Please sign in to comment.