1 // Copyright (C) 2019 The Android Open Source Project 2 // Copyright (C) 2019 Google Inc. 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 #pragma once 16 17 #include "host-common/AddressSpaceService.h" 18 19 #include <memory> 20 21 #include <inttypes.h> 22 namespace android { 23 24 namespace base { 25 26 class Stream; 27 28 } // namespace base 29 30 class HostAddressSpaceDevice { 31 public: 32 HostAddressSpaceDevice(); 33 static HostAddressSpaceDevice* get(); 34 35 void initialize(); 36 37 void clear(); 38 39 uint32_t open(); 40 void close(uint32_t handle); 41 42 uint64_t allocBlock(uint32_t handle, size_t size, uint64_t* physAddr); 43 void freeBlock(uint32_t handle, uint64_t off); 44 void setHostAddr(uint32_t handle, size_t off, void* hva); 45 46 void setHostAddrByPhysAddr(uint64_t physAddr, void* hva); 47 void unsetHostAddrByPhysAddr(uint64_t physAddr); 48 void* getHostAddr(uint64_t physAddr); 49 uint64_t offsetToPhysAddr(uint64_t offset) const; 50 51 void ping(uint32_t handle, emulation::AddressSpaceDevicePingInfo* pingInfo); 52 53 int claimShared(uint32_t handle, uint64_t off, uint64_t size); 54 int unclaimShared(uint32_t handle, uint64_t off); 55 56 void saveSnapshot(base::Stream* stream); 57 void loadSnapshot(base::Stream* stream); 58 59 // Callbacks for AddressSpaceHwFuncs. 60 static int allocSharedHostRegion(uint64_t page_aligned_size, uint64_t* offset); 61 static int freeSharedHostRegion(uint64_t offset); 62 static int allocSharedHostRegionLocked(uint64_t page_aligned_size, uint64_t* offset); 63 static int freeSharedHostRegionLocked(uint64_t offset); 64 static int allocSharedHostRegionFixedLocked(uint64_t page_aligned_size, uint64_t offset); 65 static uint64_t getPhysAddrStart(); 66 static uint64_t getPhysAddrStartLocked(); 67 static uint32_t getGuestPageSize(); 68 69 private: 70 class Impl; 71 static Impl* getImpl(); 72 73 bool mInitialized = false; 74 std::unique_ptr<Impl> mImpl; 75 }; 76 77 } // namespace android 78