aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoman Kiryanov <rkir@google.com>2024-01-24 06:49:09 +0000
committerAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>2024-01-24 06:49:09 +0000
commitd593592d0f55c16fad46a895159694ac0da4b43b (patch)
tree29f436ad147972b4db8978599bfbb8e384433706
parentbfce65632bded916d8e953a6d88a667adb56357c (diff)
parent9b46aa71fa161732eedd7ae90d97503d15cd155e (diff)
downloadaemu-d593592d0f55c16fad46a895159694ac0da4b43b.tar.gz
Support variable guest page size in goldfish_address_space am: 9b46aa71fa
Original change: https://android-review.googlesource.com/c/platform/hardware/google/aemu/+/2924907 Change-Id: I167a0945fd100b22febf0a325dcc2b1d76130ac3 Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
-rw-r--r--host-common/address_space_device.cpp3
-rw-r--r--host-common/address_space_host_memory_allocator.cpp19
-rw-r--r--host-common/address_space_host_memory_allocator_unittests.cpp32
-rw-r--r--host-common/address_space_shared_slots_host_memory_allocator.cpp15
-rw-r--r--host-common/include/host-common/address_space_device.h1
-rw-r--r--host-common/include/host-common/address_space_host_memory_allocator.h4
-rw-r--r--host-common/testing/HostAddressSpace.cpp5
-rw-r--r--host-common/testing/HostAddressSpace.h1
8 files changed, 60 insertions, 20 deletions
diff --git a/host-common/address_space_device.cpp b/host-common/address_space_device.cpp
index 48a3052..c7951be 100644
--- a/host-common/address_space_device.cpp
+++ b/host-common/address_space_device.cpp
@@ -363,7 +363,8 @@ private:
return nullptr;
case AddressSpaceDeviceType::HostMemoryAllocator:
return DeviceContextPtr(new AddressSpaceHostMemoryAllocatorContext(
- get_address_space_device_control_ops()));
+ get_address_space_device_control_ops(),
+ get_address_space_device_hw_funcs()));
case AddressSpaceDeviceType::SharedSlotsHostMemoryAllocator:
return DeviceContextPtr(new AddressSpaceSharedSlotsHostMemoryAllocatorContext(
get_address_space_device_control_ops(),
diff --git a/host-common/address_space_host_memory_allocator.cpp b/host-common/address_space_host_memory_allocator.cpp
index 8db3bbe..e61df31 100644
--- a/host-common/address_space_host_memory_allocator.cpp
+++ b/host-common/address_space_host_memory_allocator.cpp
@@ -21,11 +21,16 @@
namespace android {
namespace emulation {
+namespace {
+size_t align(size_t value, size_t alignment) {
+ return (value + alignment - 1) & (~(alignment - 1));
+}
+}
AddressSpaceHostMemoryAllocatorContext::AddressSpaceHostMemoryAllocatorContext(
- const address_space_device_control_ops *ops)
- : m_ops(ops) {
-}
+ const address_space_device_control_ops *ops, const AddressSpaceHwFuncs* hw)
+ : m_ops(ops),
+ m_hw(hw) {}
AddressSpaceHostMemoryAllocatorContext::~AddressSpaceHostMemoryAllocatorContext() {
clear();
@@ -54,13 +59,13 @@ void AddressSpaceHostMemoryAllocatorContext::perform(AddressSpaceDevicePingInfo
void *AddressSpaceHostMemoryAllocatorContext::allocate_impl(const uint64_t phys_addr,
const uint64_t size) {
#if defined(__APPLE__) && defined(__arm64__)
- constexpr uint64_t alignment = 16384;
+ constexpr uint64_t k_alloc_alignment = 16384;
#else
- constexpr uint64_t alignment = 4096;
+ constexpr uint64_t k_alloc_alignment = 4096;
#endif
- const uint64_t aligned_size = ((size + alignment - 1) / alignment) * alignment;
+ const uint64_t aligned_size = align(size, (*m_hw->getGuestPageSize)());
- void *host_ptr = android::aligned_buf_alloc(alignment, aligned_size);
+ void *host_ptr = android::aligned_buf_alloc(k_alloc_alignment, aligned_size);
if (host_ptr) {
auto r = m_paddr2ptr.insert({phys_addr, {host_ptr, aligned_size}});
if (r.second) {
diff --git a/host-common/address_space_host_memory_allocator_unittests.cpp b/host-common/address_space_host_memory_allocator_unittests.cpp
index 77f0517..8ec3fa2 100644
--- a/host-common/address_space_host_memory_allocator_unittests.cpp
+++ b/host-common/address_space_host_memory_allocator_unittests.cpp
@@ -29,6 +29,10 @@ int empty_add_memory_mapping(uint64_t gpa, void *ptr, uint64_t size) {
int empty_remove_memory_mapping(uint64_t gpa, void *ptr, uint64_t size) { return 1; }
+uint32_t getGuestPageSize() {
+ return 4096;
+}
+
struct address_space_device_control_ops create_address_space_device_control_ops() {
struct address_space_device_control_ops ops = {};
@@ -38,6 +42,14 @@ struct address_space_device_control_ops create_address_space_device_control_ops(
return ops;
}
+AddressSpaceHwFuncs create_address_space_device_hw_funcs() {
+ AddressSpaceHwFuncs hw_funcs = {};
+
+ hw_funcs.getGuestPageSize = &getGuestPageSize;
+
+ return hw_funcs;
+}
+
AddressSpaceDevicePingInfo createAllocateRequest(uint64_t phys_addr) {
AddressSpaceDevicePingInfo req = {};
@@ -64,7 +76,9 @@ TEST(AddressSpaceHostMemoryAllocatorContext, getDeviceType) {
struct address_space_device_control_ops ops =
create_address_space_device_control_ops();
- AddressSpaceHostMemoryAllocatorContext ctx(&ops);
+ AddressSpaceHwFuncs hw_funcs = create_address_space_device_hw_funcs();
+
+ AddressSpaceHostMemoryAllocatorContext ctx(&ops, &hw_funcs);
EXPECT_EQ(ctx.getDeviceType(), AddressSpaceDeviceType::HostMemoryAllocator);
}
@@ -73,7 +87,9 @@ TEST(AddressSpaceHostMemoryAllocatorContext, AllocateDeallocate) {
struct address_space_device_control_ops ops =
create_address_space_device_control_ops();
- AddressSpaceHostMemoryAllocatorContext ctx(&ops);
+ AddressSpaceHwFuncs hw_funcs = create_address_space_device_hw_funcs();
+
+ AddressSpaceHostMemoryAllocatorContext ctx(&ops, &hw_funcs);
AddressSpaceDevicePingInfo req;
@@ -90,7 +106,9 @@ TEST(AddressSpaceHostMemoryAllocatorContext, AllocateSamePhysAddr) {
struct address_space_device_control_ops ops =
create_address_space_device_control_ops();
- AddressSpaceHostMemoryAllocatorContext ctx(&ops);
+ AddressSpaceHwFuncs hw_funcs = create_address_space_device_hw_funcs();
+
+ AddressSpaceHostMemoryAllocatorContext ctx(&ops, &hw_funcs);
AddressSpaceDevicePingInfo req;
@@ -127,7 +145,9 @@ TEST(AddressSpaceHostMemoryAllocatorContext, AllocateMappingFail) {
struct address_space_device_control_ops ops =
create_address_space_device_control_ops();
- AddressSpaceHostMemoryAllocatorContext ctx(&ops);
+ AddressSpaceHwFuncs hw_funcs = create_address_space_device_hw_funcs();
+
+ AddressSpaceHostMemoryAllocatorContext ctx(&ops, &hw_funcs);
AddressSpaceDevicePingInfo req;
@@ -140,7 +160,9 @@ TEST(AddressSpaceHostMemoryAllocatorContext, UnallocateTwice) {
struct address_space_device_control_ops ops =
create_address_space_device_control_ops();
- AddressSpaceHostMemoryAllocatorContext ctx(&ops);
+ AddressSpaceHwFuncs hw_funcs = create_address_space_device_hw_funcs();
+
+ AddressSpaceHostMemoryAllocatorContext ctx(&ops, &hw_funcs);
AddressSpaceDevicePingInfo req;
diff --git a/host-common/address_space_shared_slots_host_memory_allocator.cpp b/host-common/address_space_shared_slots_host_memory_allocator.cpp
index 7136048..c6d74e2 100644
--- a/host-common/address_space_shared_slots_host_memory_allocator.cpp
+++ b/host-common/address_space_shared_slots_host_memory_allocator.cpp
@@ -27,6 +27,10 @@
namespace android {
namespace emulation {
namespace {
+size_t align(size_t value, size_t alignment) {
+ return (value + alignment - 1) & (~(alignment - 1));
+}
+
typedef AddressSpaceSharedSlotsHostMemoryAllocatorContext ASSSHMAC;
typedef ASSSHMAC::MemBlock MemBlock;
typedef MemBlock::FreeSubblocks_t FreeSubblocks_t;
@@ -35,9 +39,9 @@ using base::AutoLock;
using base::Lock;
#if defined(__APPLE__) && defined(__arm64__)
-constexpr uint32_t kAlignment = 16384;
+constexpr uint32_t kAllocAlignment = 16384;
#else
-constexpr uint32_t kAlignment = 4096;
+constexpr uint32_t kAllocAlignment = 4096;
#endif
uint64_t allocateAddressSpaceBlock(const AddressSpaceHwFuncs* hw, uint32_t size) {
@@ -89,7 +93,7 @@ std::pair<uint64_t, MemBlock*> translatePhysAddr(uint64_t p) {
MemBlock::MemBlock(const address_space_device_control_ops* o, const AddressSpaceHwFuncs* h, uint32_t sz)
: ops(o), hw(h) {
- bits = android::aligned_buf_alloc(kAlignment, sz);
+ bits = android::aligned_buf_alloc(kAllocAlignment, sz);
bitsSize = sz;
physBase = allocateAddressSpaceBlock(hw, sz);
if (!physBase) {
@@ -247,7 +251,7 @@ bool MemBlock::load(base::Stream* stream,
MemBlock* block) {
const uint64_t physBaseLoaded = stream->getBe64();
const uint32_t bitsSize = stream->getBe32();
- void* const bits = android::aligned_buf_alloc(kAlignment, bitsSize);
+ void* const bits = android::aligned_buf_alloc(kAllocAlignment, bitsSize);
if (!bits) {
return false;
}
@@ -324,8 +328,7 @@ void AddressSpaceSharedSlotsHostMemoryAllocatorContext::perform(AddressSpaceDevi
uint64_t
AddressSpaceSharedSlotsHostMemoryAllocatorContext::allocate(
AddressSpaceDevicePingInfo *info) {
- const uint32_t alignedSize =
- ((info->size + kAlignment - 1) / kAlignment) * kAlignment;
+ const uint32_t alignedSize = align(info->size, (*m_hw->getGuestPageSize)());
AutoLock lock(g_blocksLock);
for (auto& kv : g_blocks) {
diff --git a/host-common/include/host-common/address_space_device.h b/host-common/include/host-common/address_space_device.h
index 22bbbe7..ae5f58f 100644
--- a/host-common/include/host-common/address_space_device.h
+++ b/host-common/include/host-common/address_space_device.h
@@ -97,6 +97,7 @@ struct AddressSpaceHwFuncs {
* are relative to. */
uint64_t (*getPhysAddrStart)(void);
uint64_t (*getPhysAddrStartLocked)(void);
+ uint32_t (*getGuestPageSize)(void);
/* Version of allocSharedHostRegionLocked but for a fixed offset */
int (*allocSharedHostRegionFixedLocked)(uint64_t page_aligned_size, uint64_t offset);
diff --git a/host-common/include/host-common/address_space_host_memory_allocator.h b/host-common/include/host-common/address_space_host_memory_allocator.h
index a2419c3..c87213c 100644
--- a/host-common/include/host-common/address_space_host_memory_allocator.h
+++ b/host-common/include/host-common/address_space_host_memory_allocator.h
@@ -28,7 +28,8 @@ public:
Unallocate = 2
};
- AddressSpaceHostMemoryAllocatorContext(const address_space_device_control_ops *ops);
+ AddressSpaceHostMemoryAllocatorContext(const address_space_device_control_ops *ops,
+ const AddressSpaceHwFuncs* hw);
~AddressSpaceHostMemoryAllocatorContext();
void perform(AddressSpaceDevicePingInfo *info) override;
@@ -51,6 +52,7 @@ private:
std::unordered_map<uint64_t, std::pair<void *, size_t>> m_paddr2ptr;
const address_space_device_control_ops *m_ops; // do not save/load
+ const AddressSpaceHwFuncs* m_hw;
};
} // namespace emulation
diff --git a/host-common/testing/HostAddressSpace.cpp b/host-common/testing/HostAddressSpace.cpp
index a401150..bdde0a0 100644
--- a/host-common/testing/HostAddressSpace.cpp
+++ b/host-common/testing/HostAddressSpace.cpp
@@ -479,6 +479,10 @@ uint64_t HostAddressSpaceDevice::getPhysAddrStart() {
return HostAddressSpaceDevice::getImpl()->getPhysAddrStart();
}
+uint32_t HostAddressSpaceDevice::getGuestPageSize() {
+ return 4096;
+}
+
int HostAddressSpaceDevice::allocSharedHostRegionFixedLocked(uint64_t page_aligned_size, uint64_t offset) {
return HostAddressSpaceDevice::getImpl()->allocSharedHostRegionFixedLocked(page_aligned_size, offset);
}
@@ -490,6 +494,7 @@ static const AddressSpaceHwFuncs sAddressSpaceHwFuncs = {
&HostAddressSpaceDevice::freeSharedHostRegionLocked,
&HostAddressSpaceDevice::getPhysAddrStart,
&HostAddressSpaceDevice::getPhysAddrStart,
+ &HostAddressSpaceDevice::getGuestPageSize,
&HostAddressSpaceDevice::allocSharedHostRegionFixedLocked,
};
diff --git a/host-common/testing/HostAddressSpace.h b/host-common/testing/HostAddressSpace.h
index 3890717..4daf7dd 100644
--- a/host-common/testing/HostAddressSpace.h
+++ b/host-common/testing/HostAddressSpace.h
@@ -64,6 +64,7 @@ public:
static int allocSharedHostRegionFixedLocked(uint64_t page_aligned_size, uint64_t offset);
static uint64_t getPhysAddrStart();
static uint64_t getPhysAddrStartLocked();
+ static uint32_t getGuestPageSize();
private:
class Impl;