1 // Copyright 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 #pragma once 15 16 #include "base/Compiler.h" 17 #include "base/StaticMap.h" 18 #include "base/export.h" 19 #include "vm_operations.h" 20 21 #include <atomic> 22 23 #include <inttypes.h> 24 25 // A global mapping from opaque host memory IDs to host virtual 26 // addresses/sizes. This is so that the guest doesn't have to know the host 27 // virtual address to be able to map them. However, we do also provide a 28 // mechanism for obtaining the offsets into page for such buffers (as the guest 29 // does need to know those). 30 // 31 // This is currently used only in conjunction with virtio-gpu-next and Vulkan / 32 // address space device, though there are possible other consumers of this, so 33 // it becomes a global object. It exports methods into VmOperations. 34 35 namespace android { 36 namespace emulation { 37 38 class HostmemIdMapping { 39 public: 40 HostmemIdMapping() = default; 41 42 AEMU_EXPORT static HostmemIdMapping* get(); 43 44 using Id = uint64_t; 45 using Entry = HostmemEntry; 46 47 static const Id kInvalidHostmemId; 48 49 // Returns kInvalidHostmemId if hva or size is 0. 50 AEMU_EXPORT Id add(uint64_t hva, uint64_t size, bool register_fixed = false, uint64_t fixed_id = 0); 51 52 // No-op if kInvalidHostmemId or a nonexistent entry 53 // is referenced. 54 AEMU_EXPORT void remove(Id id); 55 56 // If id == kInvalidHostmemId or not found in map, 57 // returns entry with id == kInvalidHostmemId, 58 // hva == 0, and size == 0. 59 AEMU_EXPORT Entry get(Id id) const; 60 61 // Restores to starting state where there are no entries. 62 AEMU_EXPORT void clear(); 63 64 private: 65 std::atomic<Id> mCurrentId {1}; 66 base::StaticMap<Id, Entry> mEntries; 67 DISALLOW_COPY_ASSIGN_AND_MOVE(HostmemIdMapping); 68 }; 69 70 } // namespace android 71 } // namespace emulation 72 73 // C interface for use with vm operations 74 extern "C" { 75 76 AEMU_EXPORT uint64_t android_emulation_hostmem_register(uint64_t hva, uint64_t size, uint32_t register_fixed, uint64_t fixed_id); 77 AEMU_EXPORT void android_emulation_hostmem_unregister(uint64_t id); 78 AEMU_EXPORT HostmemEntry android_emulation_hostmem_get_info(uint64_t id); 79 80 } // extern "C" 81