1 // Copyright (C) 2019 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 #include "testing/HostAddressSpace.h"
15
16 #include "base/AlignedBuf.h"
17 #include "host-common/AndroidAgentFactory.h"
18 #include "host-common/address_space_device.h"
19 #include "host-common/address_space_device.hpp"
20
21 #include <gtest/gtest.h>
22
23 namespace android {
24
25 class HostAddressSpaceTest : public ::testing::Test {
26 protected:
SetUpTestCase()27 static void SetUpTestCase() {
28 emulation::goldfish_address_space_set_vm_operations(getConsoleAgents()->vm);
29 }
30
TearDownTestCase()31 static void TearDownTestCase() { }
32
SetUp()33 void SetUp() override {
34 mDevice = HostAddressSpaceDevice::get();
35 }
36
TearDown()37 void TearDown() override {
38 if (mDevice) mDevice->clear();
39 // mDevice is singleton, no need to tear down
40 }
41 HostAddressSpaceDevice* mDevice = nullptr;
42 };
43
44 // Tests the constructor.
TEST_F(HostAddressSpaceTest,Basic)45 TEST_F(HostAddressSpaceTest, Basic) { }
46
47 // Tests open/close.
TEST_F(HostAddressSpaceTest,OpenClose)48 TEST_F(HostAddressSpaceTest, OpenClose) {
49 uint32_t handle = mDevice->open();
50 EXPECT_NE(0, handle);
51 mDevice->close(handle);
52 }
53
54 // Tests host interface existing
TEST_F(HostAddressSpaceTest,FunctionPointers)55 TEST_F(HostAddressSpaceTest, FunctionPointers) {
56 EXPECT_NE(nullptr,
57 get_address_space_device_hw_funcs());
58 EXPECT_NE(nullptr,
59 get_address_space_device_hw_funcs()->allocSharedHostRegion);
60 EXPECT_NE(nullptr,
61 get_address_space_device_hw_funcs()->allocSharedHostRegionLocked);
62 EXPECT_NE(nullptr,
63 get_address_space_device_hw_funcs()->freeSharedHostRegion);
64 EXPECT_NE(nullptr,
65 get_address_space_device_hw_funcs()->freeSharedHostRegionLocked);
66 EXPECT_NE(nullptr,
67 get_address_space_device_hw_funcs()->getPhysAddrStart);
68 EXPECT_NE(nullptr,
69 get_address_space_device_hw_funcs()->getPhysAddrStartLocked);
70 }
71
72 // Tests allocation and freeing of host shared regions.
TEST_F(HostAddressSpaceTest,HostSharedAllocs)73 TEST_F(HostAddressSpaceTest, HostSharedAllocs) {
74 auto hwFuncs = get_address_space_device_hw_funcs();
75
76 // Test that passing nullptr as the offset results in an error.
77 int allocRes = hwFuncs->allocSharedHostRegion(4096, nullptr);
78
79 EXPECT_EQ(-EINVAL, allocRes);
80
81 uint64_t offset;
82 allocRes = hwFuncs->allocSharedHostRegion(4096, &offset);
83
84 EXPECT_EQ(0, allocRes);
85
86 int freeRes = hwFuncs->freeSharedHostRegion(offset);
87
88 EXPECT_EQ(0, freeRes);
89
90 freeRes = hwFuncs->freeSharedHostRegion(offset);
91
92 EXPECT_EQ(-EINVAL, freeRes);
93 }
94
95 // Tests the interface to get the starting phys addr.
TEST_F(HostAddressSpaceTest,HostGetStartingPhysAddr)96 TEST_F(HostAddressSpaceTest, HostGetStartingPhysAddr) {
97 auto hwFuncs = get_address_space_device_hw_funcs();
98 auto physStart = hwFuncs->getPhysAddrStart();
99 EXPECT_NE(0, physStart);
100 }
101
102 // Tests host interface APIs Locked* versions.
TEST_F(HostAddressSpaceTest,HostInterfaceLocked)103 TEST_F(HostAddressSpaceTest, HostInterfaceLocked) {
104 auto hwFuncs = get_address_space_device_hw_funcs();
105
106 // Test that passing nullptr as the offset results in an error.
107 int allocRes = hwFuncs->allocSharedHostRegionLocked(4096, nullptr);
108
109 EXPECT_EQ(-EINVAL, allocRes);
110
111 uint64_t offset;
112 allocRes = hwFuncs->allocSharedHostRegionLocked(4096, &offset);
113
114 EXPECT_EQ(0, allocRes);
115
116 int freeRes = hwFuncs->freeSharedHostRegionLocked(offset);
117
118 EXPECT_EQ(0, freeRes);
119
120 freeRes = hwFuncs->freeSharedHostRegionLocked(offset);
121
122 EXPECT_EQ(-EINVAL, freeRes);
123
124 auto physStart = hwFuncs->getPhysAddrStartLocked();
125 EXPECT_NE(0, physStart);
126 }
127
128 // Tests claiming/unclaiming shared regions.
129 // Test that two different handles can share the same offset.
TEST_F(HostAddressSpaceTest,SharedRegionClaiming)130 TEST_F(HostAddressSpaceTest, SharedRegionClaiming) {
131 auto hwFuncs = get_address_space_device_hw_funcs();
132 uint32_t handle = mDevice->open();
133 uint32_t handle2 = mDevice->open();
134
135 EXPECT_NE(0, handle);
136
137 uint64_t offset;
138 int allocRes = hwFuncs->allocSharedHostRegion(4096, &offset);
139 EXPECT_EQ(0, allocRes);
140
141 int claimRes = mDevice->claimShared(handle, offset, 4096);
142 EXPECT_EQ(0, claimRes);
143 claimRes = mDevice->claimShared(handle2, offset, 4096);
144 EXPECT_EQ(0, claimRes);
145
146 int unclaimRes = mDevice->unclaimShared(handle, offset);
147 EXPECT_EQ(0, unclaimRes);
148 unclaimRes = mDevice->unclaimShared(handle2, offset);
149 EXPECT_EQ(0, unclaimRes);
150
151 int freeRes = hwFuncs->freeSharedHostRegion(offset);
152 EXPECT_EQ(0, freeRes);
153
154 mDevice->close(handle);
155 mDevice->close(handle2);
156 }
157
158 // Tests error cases when claiming/unclaiming shared regions;
159 // a region must have been created via allocSharedHostRegionLocked,
160 // and the same offset cannot be claimed or unclaimed twice with the same handle.
TEST_F(HostAddressSpaceTest,SharedRegionClaimingNegative)161 TEST_F(HostAddressSpaceTest, SharedRegionClaimingNegative) {
162 auto hwFuncs = get_address_space_device_hw_funcs();
163 uint32_t handle = mDevice->open();
164
165 EXPECT_NE(0, handle);
166
167 uint64_t offset;
168 int allocRes = hwFuncs->allocSharedHostRegion(4096, &offset);
169 EXPECT_EQ(0, allocRes);
170
171 int claimRes = mDevice->claimShared(handle, offset - 1, 4096);
172 EXPECT_EQ(-EINVAL, claimRes);
173 claimRes = mDevice->claimShared(handle, offset, 4097);
174 EXPECT_EQ(-EINVAL, claimRes);
175 claimRes = mDevice->claimShared(handle, offset, 4096);
176 EXPECT_EQ(0, claimRes);
177 claimRes = mDevice->claimShared(handle, offset, 4096);
178 EXPECT_EQ(-EINVAL, claimRes);
179
180 int unclaimRes = mDevice->unclaimShared(handle, offset + 1);
181 EXPECT_EQ(-EINVAL, unclaimRes);
182 unclaimRes = mDevice->unclaimShared(handle, offset - 1);
183 EXPECT_EQ(-EINVAL, unclaimRes);
184 unclaimRes = mDevice->unclaimShared(handle, offset);
185 EXPECT_EQ(0, unclaimRes);
186 unclaimRes = mDevice->unclaimShared(handle, offset);
187 EXPECT_EQ(-EINVAL, unclaimRes);
188
189 int freeRes = hwFuncs->freeSharedHostRegion(offset);
190 EXPECT_EQ(0, freeRes);
191
192 mDevice->close(handle);
193 }
194
195 // Tests the HVA association API.
TEST_F(HostAddressSpaceTest,HostAddressMapping)196 TEST_F(HostAddressSpaceTest, HostAddressMapping) {
197 auto hwFuncs = get_address_space_device_hw_funcs();
198 auto buf = aligned_buf_alloc(4096, 4096);
199 uint32_t handle = mDevice->open();
200
201 EXPECT_NE(0, handle);
202
203 uint64_t offset;
204 int allocRes = hwFuncs->allocSharedHostRegion(4096, &offset);
205 EXPECT_EQ(0, allocRes);
206
207 uint64_t physAddr = hwFuncs->getPhysAddrStart() + offset;
208 mDevice->setHostAddrByPhysAddr(physAddr, buf);
209
210 EXPECT_EQ(buf, mDevice->getHostAddr(physAddr));
211
212 int claimRes = mDevice->claimShared(handle, offset, 4096);
213 EXPECT_EQ(0, claimRes);
214
215 int unclaimRes = mDevice->unclaimShared(handle, offset);
216 EXPECT_EQ(0, unclaimRes);
217
218 int freeRes = hwFuncs->freeSharedHostRegion(offset);
219 EXPECT_EQ(0, freeRes);
220
221 aligned_buf_free(buf);
222 mDevice->close(handle);
223 }
224
225 } // namespace android
226