| /* |
| * Copyright (c) 2020-2021 The Linux Foundation. All rights reserved. |
| * |
| * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved. |
| * |
| * Redistribution and use in source and binary forms, with or without |
| * modification, are permitted provided that the following conditions are |
| * met: |
| * * Redistributions of source code must retain the above copyright |
| * notice, this list of conditions and the following disclaimer. |
| * * Redistributions in binary form must reproduce the above |
| * copyright notice, this list of conditions and the following |
| * disclaimer in the documentation and/or other materials provided |
| * with the distribution. |
| * * Neither the name of The Linux Foundation nor the names of its |
| * contributors may be used to endorse or promote products derived |
| * from this software without specific prior written permission. |
| * |
| * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| */ |
| |
| #ifndef __MEMBUF_WRAPPER_H__ |
| #define __MEMBUF_WRAPPER_H__ |
| |
| #include <stdio.h> |
| |
| #include <mutex> |
| #include <bitset> |
| #include <map> |
| |
| using std::mutex; |
| |
| struct UsageHints { |
| union { |
| struct { |
| uint32_t trusted_ui : 1; //!< Transfer memory from secure display heap to TUI carveout heap |
| //!<.when this bit is set |
| }; |
| uint64_t hints; |
| }; |
| }; |
| |
| enum VmType { |
| kVmTypePrimary, |
| kVmTypeTrusted, |
| kVmTypeCpPixel, |
| kVmTypeMax, |
| }; |
| |
| enum VmPermission { |
| kVmPermissionRead, |
| kVmPermissionWrite, |
| kVmPermissionExecute, |
| kVmPermissionMax, |
| }; |
| |
| typedef std::map<VmType, std::bitset<kVmPermissionMax>> VmParams; |
| |
| class MemBuf { |
| public: |
| /*! @brief Allocate and transfer a chunk of memory from src VM to dst VM |
| |
| @param[in] hints - \link UsageHints \endlink |
| @param[in] size - Size of the memory to be trasferred to destination VM |
| |
| @param[out] heap_fd - A file descriptor representing the memory that was allocated |
| from the source VM and added to the current VM. Calling close() on this file |
| descriptor will deallocate the memory from the current VM, and return it to the source VM |
| |
| @return 0 on success otherwise errno |
| */ |
| virtual int TransferHeap(const UsageHints &hints, uint32_t size, int *heap_fd) = 0; |
| |
| /*! @brief Return the memory to the src VM |
| |
| @param[out] heap_fd - A file descriptor representing the memory to be closed |
| |
| @return 0 on success otherwise errno |
| */ |
| virtual void ReturnHeap(int heap_fd) = 0; |
| |
| /*! @brief Export the buffer fd from primary VM to trusted VM. The buffer must not be mmap'ed by |
| any process prior to invoking this function. The buffer must also be a cached buffer from a |
| non-secure contiguous heap |
| |
| @param[in] buf_fd - Allocated dma/ion buffer fd to be exported |
| @param[out] export_fd - An fd that corresponds to the buffer that was exported. This fd |
| must be kept open until it is no longer required to export the memory to another VM. |
| |
| @param[out] memparcel_hdl -The handle associated with the memparcel that was created by |
| granting access to the buffer fd for the trusted VM |
| |
| @return 0 on success otherwise errno |
| */ |
| virtual int Export(int buf_fd, int *export_fd, int64_t *memparcel_hdl) = 0; |
| |
| /*! @brief Allocate and transfer a chunk of memory from primary VM to secondary VM |
| |
| @param[in] memparcel_hdl - The handle that corresponds to the memparcel we are importing. |
| @param[out] import_fd - file descriptor of the buffer that the dst VM can use to |
| access the buffer. This fd must be closed to release the memory. |
| |
| @return 0 on success otherwise errno |
| */ |
| virtual int Import(int64_t memparcel_hdl, int *import_fd) = 0; |
| |
| /*! @brief Export the buffer fd from one VM to other VM. The buffer must not be mmap'ed by any |
| process prior to invoking this function. |
| |
| @param[in] buf_fd - Allocated buffer fd to be exported |
| @param[in] vm_param - List of vm type where the buffer to be assigned and its corresponding |
| permission bits. |
| @param[in] share - Allocated buffer to be lend or shared to other VM |
| |
| @param[out] memparcel_hdl -The handle associated with the memparcel that was created by |
| granting access to the buffer fd for the dst VM |
| |
| @return 0 on success otherwise errno |
| */ |
| virtual int Export(int buf_fd, const VmParams &vm_params, bool share, int64_t *memparcel_hdl) = 0; |
| |
| /*! @brief Allocate and transfer a chunk of memory from src VM to dst VM |
| |
| @param[in] memparcel_hdl - The handle that corresponds to the memparcel we are importing. |
| @param[in] vm_params - List of vm type where the buffer to be assigned and its corresponding |
| permission. It should be the same parameter passed while exporting. |
| @param[out] import_fd - file descriptor of the buffer that the dst VM can use to |
| access the buffer. This fd must be closed to release the memory. |
| |
| @return 0 on success otherwise errno |
| */ |
| virtual int Import(int64_t memparcel_hdl, const VmParams &vm_params, int *import_fd) = 0; |
| |
| /*! @brief Allocate a file descriptor which represents a suggestion from |
| userspace that a large amount of memory will be needed in the near |
| future. If the kernel decides to honor this suggestion, there is no |
| guarantee of exclusive access to this memory by the requesting process |
| Closing the file descriptor indicates the memory is no longer required. |
| |
| @param[in] size - The requested size of memory to be made free. |
| @param[in] name - A unique name for debug purposes. |
| |
| @return fd on success otherwise errno |
| */ |
| |
| virtual int MemorySizeHint(uint64_t size, const std::string& name) = 0; |
| virtual ~MemBuf() { } |
| |
| static int GetInstance(MemBuf **mem_buf_hnd); |
| static void PutInstance(); |
| }; |
| |
| #endif // __MEMBUF_WRAPPER_H__ |