aboutsummaryrefslogtreecommitdiff
path: root/shared/GoldfishAddressSpace/include/goldfish_address_space_host.impl
blob: d1fa4a3450f71532f879d263dc6031d9f964e67d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
// Copyright (C) 2019 The Android Open Source Project
// Copyright (C) 2019 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "android/emulation/hostdevices/HostAddressSpace.h"

#include <memory>

#if PLATFORM_SDK_VERSION < 26
#include <cutils/log.h>
#else
#include <log/log.h>
#endif

#include <errno.h>
#include "goldfish_address_space.h"

namespace {

const int HOST_MEMORY_ALLOCATOR_COMMAND_ALLOCATE_ID = 1;
const int HOST_MEMORY_ALLOCATOR_COMMAND_UNALLOCATE_ID = 2;

}  // namsepace

using android::HostAddressSpaceDevice;
using android::emulation::AddressSpaceDevicePingInfo;

GoldfishAddressSpaceBlockProvider::GoldfishAddressSpaceBlockProvider(GoldfishAddressSpaceSubdeviceType subdevice)
    : m_handle(HostAddressSpaceDevice::get()->open())
{
    if ((subdevice != GoldfishAddressSpaceSubdeviceType::NoSubdevice) && is_opened()) {
        AddressSpaceDevicePingInfo request;
        ::memset(&request, 0, sizeof(request));
        request.metadata = subdevice;

        HostAddressSpaceDevice::get()->ping(m_handle, &request);
    }
}

GoldfishAddressSpaceBlockProvider::~GoldfishAddressSpaceBlockProvider()
{
    if (is_opened()) {
        HostAddressSpaceDevice::get()->close(m_handle);
    }
}

bool GoldfishAddressSpaceBlockProvider::is_opened() const
{
    return m_handle > 0;
}

void GoldfishAddressSpaceBlockProvider::close()
{
    if (is_opened()) {
        HostAddressSpaceDevice::get()->close(m_handle);
        m_handle = 0;
    }
}

address_space_handle_t GoldfishAddressSpaceBlockProvider::release()
{
    address_space_handle_t handle = m_handle;
    m_handle = 0;
    return handle;
}

void GoldfishAddressSpaceBlockProvider::closeHandle(address_space_handle_t handle)
{
    HostAddressSpaceDevice::get()->close(handle);
}

GoldfishAddressSpaceBlock::GoldfishAddressSpaceBlock()
    : m_handle(0)
    , m_mmaped_ptr(NULL)
    , m_phys_addr(0)
    , m_host_addr(0)
    , m_offset(0)
    , m_size(0)
    , m_is_shared_mapping(false) {}

GoldfishAddressSpaceBlock::~GoldfishAddressSpaceBlock()
{
    destroy();
}

GoldfishAddressSpaceBlock &GoldfishAddressSpaceBlock::operator=(const GoldfishAddressSpaceBlock &rhs)
{
    m_mmaped_ptr = rhs.m_mmaped_ptr;
    m_phys_addr = rhs.m_phys_addr;
    m_host_addr = rhs.m_host_addr;
    m_offset = rhs.m_offset;
    m_size = rhs.m_size;
    m_is_shared_mapping = rhs.m_is_shared_mapping;
    m_handle = rhs.m_handle;
    return *this;
}

bool GoldfishAddressSpaceBlock::allocate(GoldfishAddressSpaceBlockProvider *provider, size_t size)
{
    ALOGD("%s: Ask for block of size 0x%llx\n", __func__,
         (unsigned long long)size);

    destroy();

    if (!provider->is_opened()) {
        return false;
    }

    m_size = size;
    m_offset =
        HostAddressSpaceDevice::get()->allocBlock(
            provider->m_handle, size, &m_phys_addr);
    m_handle = provider->m_handle;
    m_is_shared_mapping = false;

    return true;
}

bool GoldfishAddressSpaceBlock::claimShared(GoldfishAddressSpaceBlockProvider *provider, uint64_t offset, uint64_t size)
{
    ALOGD("%s: Ask to claim region [0x%llx 0x%llx]\n", __func__,
         (unsigned long long)offset,
         (unsigned long long)offset + size);

    destroy();

    if (!provider->is_opened()) {
        return false;
    }

    int claimRes = HostAddressSpaceDevice::get()->claimShared(
            provider->m_handle, offset, size);

    if (claimRes) {
        ALOGE("%s: failed to claim shared region. Error: %d\n", __func__, claimRes);
        return false;
    }

    m_size = size;
    m_offset = offset;
    m_handle = provider->m_handle;
    m_is_shared_mapping = true;
    m_phys_addr = HostAddressSpaceDevice::get()->offsetToPhysAddr(m_offset);

    return true;
}

uint64_t GoldfishAddressSpaceBlock::physAddr() const
{
    return m_phys_addr;
}

uint64_t GoldfishAddressSpaceBlock::hostAddr() const
{
    return m_host_addr;
}

// In the host implementation:
// mmap: is done by interpreting |host_addr| as the actual host address.
void *GoldfishAddressSpaceBlock::mmap(uint64_t host_addr)
{
    if (m_size == 0) {
        ALOGE("%s: called with zero size\n", __func__);
        return NULL;
    }

    if (m_mmaped_ptr != nullptr) {
        ALOGE("'mmap' called for an already mmaped address block 0x%llx %d", (unsigned long long)m_mmaped_ptr, nullptr == m_mmaped_ptr);
        ::abort();
    }

    m_mmaped_ptr = (void*)(uintptr_t)(host_addr & (~(PAGE_SIZE - 1)));
    m_host_addr = host_addr;

    return guestPtr();
}

void *GoldfishAddressSpaceBlock::guestPtr() const
{
    return reinterpret_cast<char *>(m_mmaped_ptr) + (m_host_addr & (PAGE_SIZE - 1));
}

void GoldfishAddressSpaceBlock::destroy()
{
    if (m_mmaped_ptr && m_size) {
        m_mmaped_ptr = NULL;
    }

    if (m_size) {
        if (m_is_shared_mapping) {
            HostAddressSpaceDevice::get()->unclaimShared(m_handle, m_offset);
        } else {
            HostAddressSpaceDevice::get()->freeBlock(m_handle, m_offset);
        }
        m_phys_addr = 0;
        m_host_addr = 0;
        m_offset = 0;
        m_size = 0;
    }
}

void GoldfishAddressSpaceBlock::release()
{
    m_handle = 0;
    m_mmaped_ptr = NULL;
    m_phys_addr = 0;
    m_host_addr = 0;
    m_offset = 0;
    m_size = 0;
}

int GoldfishAddressSpaceBlock::memoryMap(void *addr,
                                         size_t,
                                         address_space_handle_t,
                                         uint64_t,
                                         void** dst) {
    *dst = addr;
    return 0;
}

void GoldfishAddressSpaceBlock::memoryUnmap(void *ptr, size_t size) {}

GoldfishAddressSpaceHostMemoryAllocator::GoldfishAddressSpaceHostMemoryAllocator(bool useSharedSlots)
  : m_provider(useSharedSlots
        ? GoldfishAddressSpaceSubdeviceType::SharedSlotsHostMemoryAllocator
        : GoldfishAddressSpaceSubdeviceType::HostMemoryAllocator),
    m_useSharedSlots(useSharedSlots)
{}

bool GoldfishAddressSpaceHostMemoryAllocator::is_opened() const { return m_provider.is_opened(); }

long GoldfishAddressSpaceHostMemoryAllocator::hostMalloc(GoldfishAddressSpaceBlock *block, size_t size)
{
    if (size == 0) {
        return -EINVAL;
    }
    if (block->size() > 0) {
        return -EINVAL;
    }
    if (!m_provider.is_opened()) {
        return -ENODEV;
    }

    AddressSpaceDevicePingInfo request;
    if (m_useSharedSlots) {
        ::memset(&request, 0, sizeof(request));
        request.size = block->size();
        request.metadata = HOST_MEMORY_ALLOCATOR_COMMAND_ALLOCATE_ID;

        HostAddressSpaceDevice::get()->ping(m_provider.m_handle, &request);

        block->claimShared(&m_provider, request.phys_addr, request.size);

        void *hostPtr = HostAddressSpaceDevice::get()->getHostAddr(block->physAddr());
        block->mmap(static_cast<uint64_t>(reinterpret_cast<uintptr_t>(hostPtr)));
    } else {
        if (!block->allocate(&m_provider, size)) {
            return -ENOMEM;
        }

        ::memset(&request, 0, sizeof(request));
        request.phys_addr = block->physAddr();
        request.size = block->size();
        request.metadata = HOST_MEMORY_ALLOCATOR_COMMAND_ALLOCATE_ID;

        HostAddressSpaceDevice::get()->ping(m_provider.m_handle, &request);

        void *hostPtr = HostAddressSpaceDevice::get()->getHostAddr(block->physAddr());
        block->mmap(static_cast<uint64_t>(reinterpret_cast<uintptr_t>(hostPtr)));
    }

    return 0;
}

void GoldfishAddressSpaceHostMemoryAllocator::hostFree(GoldfishAddressSpaceBlock *block)
{
    if (block->size() == 0) {
        return;
    }

    if (!m_provider.is_opened()) {
        ALOGE("%s: device is not available", __func__);
        ::abort();
    }

    if (block->guestPtr()) {
        AddressSpaceDevicePingInfo request;
        ::memset(&request, 0, sizeof(request));
        request.phys_addr = block->physAddr();
        request.metadata = HOST_MEMORY_ALLOCATOR_COMMAND_UNALLOCATE_ID;

        HostAddressSpaceDevice::get()->ping(m_provider.m_handle, &request);
    }

    block->replace(NULL);
}

address_space_handle_t goldfish_address_space_open() {
    return HostAddressSpaceDevice::get()->open();
}

void goldfish_address_space_close(address_space_handle_t handle) {
    HostAddressSpaceDevice::get()->close(handle);
}

bool goldfish_address_space_allocate(
    address_space_handle_t handle,
    size_t size, uint64_t* phys_addr, uint64_t* offset) {

    *offset =
        HostAddressSpaceDevice::get()->allocBlock(
            handle, size, phys_addr);

    return true;
}

bool goldfish_address_space_free(
    address_space_handle_t handle, uint64_t offset) {
    HostAddressSpaceDevice::get()->freeBlock(handle, offset);
    return true;
}

bool goldfish_address_space_claim_shared(
    address_space_handle_t handle, uint64_t offset, uint64_t size) {

    int claimRes = HostAddressSpaceDevice::get()->claimShared(
        handle, offset, size);

    if (claimRes) {
        ALOGE("%s: failed to claim shared region. Error: %d\n", __func__, claimRes);
        return false;
    }

    return true;
}

bool goldfish_address_space_unclaim_shared(
        address_space_handle_t handle, uint64_t offset) {
    HostAddressSpaceDevice::get()->unclaimShared(handle, offset);
    return true;
}

// pgoff is the offset into the page to return in the result
void* goldfish_address_space_map(
    address_space_handle_t handle,
    uint64_t offset, uint64_t size,
    uint64_t pgoff) {

    (void)size;

    void* res = HostAddressSpaceDevice::get()->
        getHostAddr(
            HostAddressSpaceDevice::get()->offsetToPhysAddr(offset));

    if (!res) {
        ALOGE("%s: failed to map. errno: %d\n", __func__, errno);
        return nullptr;
    }

    return (void*)(((char*)res) + (uintptr_t)(pgoff & (PAGE_SIZE - 1)));
}

// same address space
void goldfish_address_space_unmap(void*, uint64_t) { }

bool goldfish_address_space_set_subdevice_type(
    address_space_handle_t handle, GoldfishAddressSpaceSubdeviceType type,
    address_space_handle_t* handle_out) {
    struct address_space_ping request;
    request.metadata = (uint64_t)type;
    *handle_out = handle;
    return goldfish_address_space_ping(handle, &request);
}

bool goldfish_address_space_ping(
    address_space_handle_t handle,
    struct address_space_ping* ping) {

    AddressSpaceDevicePingInfo* asHostPingInfo =
        reinterpret_cast<AddressSpaceDevicePingInfo*>(ping);

    HostAddressSpaceDevice::get()->ping(handle, asHostPingInfo);

    return true;
}