1 // Copyright 2016 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "host-common/address_space_host_memory_allocator.h"
16 #include <gtest/gtest.h>
17 
18 namespace android {
19 namespace emulation {
20 
21 namespace {
22 constexpr uint64_t BAD_GPA = 0x1234000;
23 constexpr uint64_t GOOD_GPA_1 = 0x10001000;
24 constexpr uint64_t GOOD_GPA_2 = 0x20002000;
25 
empty_add_memory_mapping(uint64_t gpa,void * ptr,uint64_t size)26 int empty_add_memory_mapping(uint64_t gpa, void *ptr, uint64_t size) {
27     return (gpa == BAD_GPA) ? 0 : 1;
28 }
29 
empty_remove_memory_mapping(uint64_t gpa,void * ptr,uint64_t size)30 int empty_remove_memory_mapping(uint64_t gpa, void *ptr, uint64_t size) { return 1; }
31 
getGuestPageSize()32 uint32_t getGuestPageSize() {
33     return 4096;
34 }
35 
create_address_space_device_control_ops()36 struct address_space_device_control_ops create_address_space_device_control_ops() {
37     struct address_space_device_control_ops ops = {};
38 
39     ops.add_memory_mapping = &empty_add_memory_mapping;
40     ops.remove_memory_mapping = &empty_remove_memory_mapping;
41 
42     return ops;
43 }
44 
create_address_space_device_hw_funcs()45 AddressSpaceHwFuncs create_address_space_device_hw_funcs() {
46     AddressSpaceHwFuncs hw_funcs = {};
47 
48     hw_funcs.getGuestPageSize = &getGuestPageSize;
49 
50     return hw_funcs;
51 }
52 
createAllocateRequest(uint64_t phys_addr)53 AddressSpaceDevicePingInfo createAllocateRequest(uint64_t phys_addr) {
54     AddressSpaceDevicePingInfo req = {};
55 
56     req.metadata = static_cast<uint64_t>(
57         AddressSpaceHostMemoryAllocatorContext::HostMemoryAllocatorCommand::Allocate);
58     req.phys_addr = phys_addr;
59     req.size = 1;
60 
61     return req;
62 }
63 
createUnallocateRequest(uint64_t phys_addr)64 AddressSpaceDevicePingInfo createUnallocateRequest(uint64_t phys_addr) {
65     AddressSpaceDevicePingInfo req = {};
66 
67     req.metadata = static_cast<uint64_t>(
68         AddressSpaceHostMemoryAllocatorContext::HostMemoryAllocatorCommand::Unallocate);
69     req.phys_addr = phys_addr;
70 
71     return req;
72 }
73 }  // namespace
74 
TEST(AddressSpaceHostMemoryAllocatorContext,getDeviceType)75 TEST(AddressSpaceHostMemoryAllocatorContext, getDeviceType) {
76     struct address_space_device_control_ops ops =
77         create_address_space_device_control_ops();
78 
79     AddressSpaceHwFuncs hw_funcs = create_address_space_device_hw_funcs();
80 
81     AddressSpaceHostMemoryAllocatorContext ctx(&ops, &hw_funcs);
82 
83     EXPECT_EQ(ctx.getDeviceType(), AddressSpaceDeviceType::HostMemoryAllocator);
84 }
85 
TEST(AddressSpaceHostMemoryAllocatorContext,AllocateDeallocate)86 TEST(AddressSpaceHostMemoryAllocatorContext, AllocateDeallocate) {
87     struct address_space_device_control_ops ops =
88         create_address_space_device_control_ops();
89 
90     AddressSpaceHwFuncs hw_funcs = create_address_space_device_hw_funcs();
91 
92     AddressSpaceHostMemoryAllocatorContext ctx(&ops, &hw_funcs);
93 
94     AddressSpaceDevicePingInfo req;
95 
96     req = createAllocateRequest(GOOD_GPA_1);
97     ctx.perform(&req);
98     EXPECT_EQ(req.metadata, 0);
99 
100     req = createUnallocateRequest(GOOD_GPA_1);
101     ctx.perform(&req);
102     EXPECT_EQ(req.metadata, 0);
103 }
104 
TEST(AddressSpaceHostMemoryAllocatorContext,AllocateSamePhysAddr)105 TEST(AddressSpaceHostMemoryAllocatorContext, AllocateSamePhysAddr) {
106     struct address_space_device_control_ops ops =
107         create_address_space_device_control_ops();
108 
109     AddressSpaceHwFuncs hw_funcs = create_address_space_device_hw_funcs();
110 
111     AddressSpaceHostMemoryAllocatorContext ctx(&ops, &hw_funcs);
112 
113     AddressSpaceDevicePingInfo req;
114 
115     req = createAllocateRequest(GOOD_GPA_1);
116     ctx.perform(&req);
117     EXPECT_EQ(req.metadata, 0);
118 
119     req = createAllocateRequest(GOOD_GPA_1);
120     ctx.perform(&req);
121     EXPECT_NE(req.metadata, 0);
122 
123     req = createAllocateRequest(GOOD_GPA_2);
124     ctx.perform(&req);
125     EXPECT_EQ(req.metadata, 0);
126 
127     req = createUnallocateRequest(GOOD_GPA_2);
128     ctx.perform(&req);
129     EXPECT_EQ(req.metadata, 0);
130 
131     req = createUnallocateRequest(GOOD_GPA_1);
132     ctx.perform(&req);
133     EXPECT_EQ(req.metadata, 0);
134 
135     req = createAllocateRequest(GOOD_GPA_1);
136     ctx.perform(&req);
137     EXPECT_EQ(req.metadata, 0);
138 
139     req = createUnallocateRequest(GOOD_GPA_1);
140     ctx.perform(&req);
141     EXPECT_EQ(req.metadata, 0);
142 }
143 
TEST(AddressSpaceHostMemoryAllocatorContext,AllocateMappingFail)144 TEST(AddressSpaceHostMemoryAllocatorContext, AllocateMappingFail) {
145     struct address_space_device_control_ops ops =
146         create_address_space_device_control_ops();
147 
148     AddressSpaceHwFuncs hw_funcs = create_address_space_device_hw_funcs();
149 
150     AddressSpaceHostMemoryAllocatorContext ctx(&ops, &hw_funcs);
151 
152     AddressSpaceDevicePingInfo req;
153 
154     req = createAllocateRequest(BAD_GPA);
155     ctx.perform(&req);
156     EXPECT_NE(req.metadata, 0);
157 }
158 
TEST(AddressSpaceHostMemoryAllocatorContext,UnallocateTwice)159 TEST(AddressSpaceHostMemoryAllocatorContext, UnallocateTwice) {
160     struct address_space_device_control_ops ops =
161         create_address_space_device_control_ops();
162 
163     AddressSpaceHwFuncs hw_funcs = create_address_space_device_hw_funcs();
164 
165     AddressSpaceHostMemoryAllocatorContext ctx(&ops, &hw_funcs);
166 
167     AddressSpaceDevicePingInfo req;
168 
169     req = createAllocateRequest(GOOD_GPA_1);
170     ctx.perform(&req);
171     EXPECT_EQ(req.metadata, 0);
172 
173     req = createUnallocateRequest(GOOD_GPA_1);
174     ctx.perform(&req);
175     EXPECT_EQ(req.metadata, 0);
176 
177     req = createUnallocateRequest(GOOD_GPA_1);
178     ctx.perform(&req);
179     EXPECT_NE(req.metadata, 0);
180 }
181 
182 }  // namespace emulation
183 } // namespace android
184