1 /* 2 * Copyright (C) 2022 Huawei Technologies Co., Ltd. 3 * Licensed under the Mulan PSL v2. 4 * You can use this software according to the terms and conditions of the Mulan PSL v2. 5 * You may obtain a copy of Mulan PSL v2 at: 6 * http://license.coscl.org.cn/MulanPSL2 7 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR 8 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR 9 * PURPOSE. 10 * See the Mulan PSL v2 for more details. 11 */ 12 13 #ifndef TEE_MEM_MGMT_API_H 14 #define TEE_MEM_MGMT_API_H 15 16 #include "tee_defines.h" 17 18 /* 19 * below definitions are defined by Global Platform or Platform SDK released previously 20 * for compatibility: 21 * don't make any change to the content below 22 */ 23 #ifndef ZERO_SIZE_PTR 24 #define ZERO_SIZE_PTR ((void *)16) 25 #define zero_or_null_ptr(x) ((unsigned long)(x) <= (unsigned long)ZERO_SIZE_PTR) 26 #endif 27 28 enum MALLOC_HINT { 29 ZERO = 0, 30 NOT_ZERO = 1, 31 ALIGN_004 = 0x80000002, /* buf align */ 32 ALIGN_008 = 0x80000003, 33 ALIGN_016 = 0x80000004, 34 ALIGN_032 = 0x80000005, 35 ALIGN_064 = 0x80000006, 36 ALIGN_128 = 0x80000007, 37 ALIGN_256 = 0x80000008, 38 ALIGN_004_ZERO = 0x80000012, /* buf align and set to zero */ 39 ALIGN_008_ZERO = 0x80000013, 40 ALIGN_016_ZERO = 0x80000014, 41 ALIGN_032_ZERO = 0x80000015, 42 ALIGN_064_ZERO = 0x80000016, 43 ALIGN_128_ZERO = 0x80000017, 44 ALIGN_256_ZERO = 0x80000018, 45 }; 46 47 #define TEE_MALLOC_FILL_ZERO 0x00000000 48 #define TEE_MALLOC_NO_FILL 0x00000001 49 #define TEE_MALLOC_NO_SHARE 0x00000002 50 51 #define TEE_MEMORY_ACCESS_READ 0x00000001 52 #define TEE_MEMORY_ACCESS_WRITE 0x00000002 53 #define TEE_MEMORY_ACCESS_ANY_OWNER 0x00000004 54 55 /* 56 * fill the first size bytes of buffer with x. 57 * 58 * @param buffer [OUT] the pointer of buffer 59 * @param x [IN] fill value 60 * @param size [IN] number of bytes 61 * 62 * @return void 63 */ 64 #if defined(API_LEVEL) && (API_LEVEL >= API_LEVEL1_2) 65 void TEE_MemFill(void *buffer, uint8_t x, size_t size); 66 #else 67 void TEE_MemFill(void *buffer, uint32_t x, size_t size); 68 #endif 69 70 /* 71 * copy size bytes from src to dest 72 * 73 * @param dest [OUT] dest buffer pointer 74 * @param src [IN] src buffer pointer 75 * @param size [IN] number of bytes 76 * 77 * @return void 78 */ 79 void TEE_MemMove(void *dest, const void *src, size_t size); 80 81 /* 82 * alloc memory of size bytes with hint value 83 * The pointer returned will be compatible to any C basic data type 84 * 85 * @param size [IN] size of memory that will be allocated 86 * @param hint [IN] a flag, 0 mean that the memory returned will filled with '\0' 87 * 88 * @return a pointer to the new allocated memory 89 * @return NULL means failed when allocated 90 */ 91 void *TEE_Malloc(size_t size, uint32_t hint); 92 93 /* 94 * release the memory which allocated by TEE_Malloc 95 * If buffer equals to NULL,TEE_Free will do nothing 96 * Caller should make sure that the buffer is created by TEE_Malloc or TEE_Realloc 97 * and should NOT free one memory twice, operation result is unpredictable 98 * 99 * @param buffer [IN] pointer to memory 100 * 101 * @return none 102 */ 103 void TEE_Free(void *buffer); 104 105 /* 106 * If new size larger than old size, the contents of old memory will not changed, 107 * the remained memory are random bytes 108 * There will be a new allocate action when modify the size of memory 109 * If allocated failed, old memory will be returned and this function will return NULL 110 * If buffer equals to NULL, this function is same to TEE_Malloc 111 * 112 * @param buffer [IN] pointer to memory 113 * @param new_size [IN] new size 114 * 115 * @return pointer to new memory, should NOT be NULL 116 * @return NULL means failed 117 */ 118 void *TEE_Realloc(void *buffer, size_t new_size); 119 120 /* 121 * compare of memory contents 122 * 123 * @param buffer1 [IN] first pointer 124 * @param buffer2 [IN] second pointer 125 * @param size [IN] bytes size to be compared 126 * 127 * @return -1 buffer1 < buffer2 128 * @return 0 buffer1 == buffer2 129 * @return 1 buffer1 > buffer2 130 */ 131 int32_t TEE_MemCompare(const void *buffer1, const void *buffer2, size_t size); 132 133 /** 134 * @brief Checks whether this TA has the requested permissions to access a buffer. 135 * 136 * @param accessFlags Indicates the access permissions to check. 137 * @param buffer Indicates the pointer to the target buffer. 138 * @param size Indicates the size of the buffer to check. 139 * 140 * @return Returns <b>TEE_SUCCESS</b> if the TA has the requested permissions. 141 * @return Returns <b>TEE_ERROR_ACCESS_DENIED</b> otherwise. 142 */ 143 TEE_Result TEE_CheckMemoryAccessRights(uint32_t accessFlags, const void *buffer, size_t size); 144 145 /* 146 * A global variable used to share in different Session of same Instance 147 * 148 * @param instanceData [IN] Address of the global variable 149 * 150 * @return void 151 */ 152 void TEE_SetInstanceData(void *instanceData); 153 154 /* 155 * get the pointer which set by TEE_SetInstanceData 156 * 157 * @return a pointer to the variable which set by TEE_SetInstanceData, the pointer should NOT be NULL 158 * @return NULL no InstanceData is set 159 */ 160 void *TEE_GetInstanceData(void); 161 162 #endif 163