1 /* 2 * Copyright (C) 2018 Google, Inc. 3 * 4 * This software is licensed under the terms of the GNU General Public 5 * License version 2, as published by the Free Software Foundation, and 6 * may be copied, distributed, and modified under those terms. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 */ 14 15 #ifndef ANDROID_INCLUDE_HARDWARE_GOLDFISH_ADDRESS_SPACE_H 16 #define ANDROID_INCLUDE_HARDWARE_GOLDFISH_ADDRESS_SPACE_H 17 18 #include <inttypes.h> 19 #include <stddef.h> 20 21 class GoldfishAddressSpaceBlock; 22 23 #ifdef HOST_BUILD 24 class GoldfishAddressSpaceBlockProvider { 25 public: 26 GoldfishAddressSpaceBlockProvider(); 27 ~GoldfishAddressSpaceBlockProvider(); 28 29 uint64_t allocPhys(size_t size); 30 void freePhys(uint64_t phys); 31 32 private: 33 void* mAlloc; 34 35 friend class GoldfishAddressSpaceBlock; 36 }; 37 #else 38 class GoldfishAddressSpaceBlockProvider { 39 public: 40 GoldfishAddressSpaceBlockProvider(); 41 ~GoldfishAddressSpaceBlockProvider(); 42 43 private: 44 GoldfishAddressSpaceBlockProvider(const GoldfishAddressSpaceBlockProvider &rhs); 45 GoldfishAddressSpaceBlockProvider &operator=(const GoldfishAddressSpaceBlockProvider &rhs); 46 47 bool is_opened(); 48 #ifdef __Fuchsia__ 49 uint32_t m_channel; 50 #else 51 int m_fd; 52 #endif 53 54 friend class GoldfishAddressSpaceBlock; 55 }; 56 #endif 57 58 class GoldfishAddressSpaceBlock { 59 public: 60 GoldfishAddressSpaceBlock(); 61 ~GoldfishAddressSpaceBlock(); 62 63 bool allocate(GoldfishAddressSpaceBlockProvider *provider, size_t size); 64 uint64_t physAddr() const; 65 uint64_t hostAddr() const; 66 void *mmap(uint64_t opaque); 67 void *guestPtr() const; 68 void replace(GoldfishAddressSpaceBlock *x); 69 70 private: 71 void destroy(); 72 GoldfishAddressSpaceBlock &operator=(const GoldfishAddressSpaceBlock &); 73 74 #ifdef HOST_BUILD 75 bool m_alloced; 76 void *m_guest_ptr; 77 uint64_t m_phys_addr; 78 GoldfishAddressSpaceBlockProvider* m_provider; 79 #else 80 #ifdef __Fuchsia__ 81 uint32_t m_vmo; 82 uint32_t m_channel; 83 #else 84 int m_fd; 85 #endif 86 void *m_mmaped_ptr; 87 uint64_t m_phys_addr; 88 uint64_t m_host_addr; 89 uint64_t m_offset; 90 size_t m_size; 91 #endif 92 }; 93 94 #endif 95