17777dab0Sopenharmony_ci/*
27777dab0Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
37777dab0Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
47777dab0Sopenharmony_ci * you may not use this file except in compliance with the License.
57777dab0Sopenharmony_ci * You may obtain a copy of the License at
67777dab0Sopenharmony_ci *
77777dab0Sopenharmony_ci *    http://www.apache.org/licenses/LICENSE-2.0
87777dab0Sopenharmony_ci *
97777dab0Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
107777dab0Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
117777dab0Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
127777dab0Sopenharmony_ci * See the License for the specific language governing permissions and
137777dab0Sopenharmony_ci * limitations under the License.
147777dab0Sopenharmony_ci */
157777dab0Sopenharmony_ci
167777dab0Sopenharmony_ci
177777dab0Sopenharmony_ci#ifndef TEE_MEM_MGMT_API_H
187777dab0Sopenharmony_ci#define TEE_MEM_MGMT_API_H
197777dab0Sopenharmony_ci
207777dab0Sopenharmony_ci/**
217777dab0Sopenharmony_ci * @addtogroup TeeTrusted
227777dab0Sopenharmony_ci * @{
237777dab0Sopenharmony_ci *
247777dab0Sopenharmony_ci * @brief TEE(Trusted Excution Environment) API.
257777dab0Sopenharmony_ci * Provides security capability APIs such as trusted storage, encryption and decryption,
267777dab0Sopenharmony_ci * and trusted time for trusted application development.
277777dab0Sopenharmony_ci *
287777dab0Sopenharmony_ci * @since 12
297777dab0Sopenharmony_ci */
307777dab0Sopenharmony_ci
317777dab0Sopenharmony_ci/**
327777dab0Sopenharmony_ci * @file tee_mem_mgmt_api.h
337777dab0Sopenharmony_ci *
347777dab0Sopenharmony_ci * @brief Provides APIs for memory management.
357777dab0Sopenharmony_ci *
367777dab0Sopenharmony_ci * @library NA
377777dab0Sopenharmony_ci * @kit TEEKit
387777dab0Sopenharmony_ci * @syscap SystemCapability.Tee.TeeClient
397777dab0Sopenharmony_ci * @since 12
407777dab0Sopenharmony_ci * @version 1.0
417777dab0Sopenharmony_ci */
427777dab0Sopenharmony_ci
437777dab0Sopenharmony_ci#include "tee_defines.h"
447777dab0Sopenharmony_ci#include "tee_mem_monitoring_api.h"
457777dab0Sopenharmony_ci
467777dab0Sopenharmony_ci#ifdef __cplusplus
477777dab0Sopenharmony_ciextern "C" {
487777dab0Sopenharmony_ci#endif
497777dab0Sopenharmony_ci
507777dab0Sopenharmony_ci/*
517777dab0Sopenharmony_ci * The definitions below are defined by Global Platform or Platform SDK released previously
527777dab0Sopenharmony_ci * for compatibility.
537777dab0Sopenharmony_ci * Do not make any change to the content below.
547777dab0Sopenharmony_ci */
557777dab0Sopenharmony_ci#ifndef ZERO_SIZE_PTR
567777dab0Sopenharmony_ci#define ZERO_SIZE_PTR       ((void *)16)
577777dab0Sopenharmony_ci#define zero_or_null_ptr(x) ((unsigned long)(x) <= (unsigned long)ZERO_SIZE_PTR)
587777dab0Sopenharmony_ci#endif
597777dab0Sopenharmony_ci
607777dab0Sopenharmony_cienum MALLOC_HINT {
617777dab0Sopenharmony_ci    ZERO           = 0,
627777dab0Sopenharmony_ci    NOT_ZERO       = 1,
637777dab0Sopenharmony_ci    ALIGN_004      = 0x80000002,
647777dab0Sopenharmony_ci    /* Buffer alignment */
657777dab0Sopenharmony_ci    ALIGN_008      = 0x80000003,
667777dab0Sopenharmony_ci    ALIGN_016      = 0x80000004,
677777dab0Sopenharmony_ci    ALIGN_032      = 0x80000005,
687777dab0Sopenharmony_ci    ALIGN_064      = 0x80000006,
697777dab0Sopenharmony_ci    ALIGN_128      = 0x80000007,
707777dab0Sopenharmony_ci    ALIGN_256      = 0x80000008,
717777dab0Sopenharmony_ci    ALIGN_004_ZERO = 0x80000012,
727777dab0Sopenharmony_ci    /* The buffer is 8-byte aligned and initialized to zeros. */
737777dab0Sopenharmony_ci    ALIGN_008_ZERO = 0x80000013,
747777dab0Sopenharmony_ci    ALIGN_016_ZERO = 0x80000014,
757777dab0Sopenharmony_ci    ALIGN_032_ZERO = 0x80000015,
767777dab0Sopenharmony_ci    ALIGN_064_ZERO = 0x80000016,
777777dab0Sopenharmony_ci    ALIGN_128_ZERO = 0x80000017,
787777dab0Sopenharmony_ci    ALIGN_256_ZERO = 0x80000018,
797777dab0Sopenharmony_ci};
807777dab0Sopenharmony_ci
817777dab0Sopenharmony_ci#define TEE_MALLOC_FILL_ZERO 0x00000000
827777dab0Sopenharmony_ci#define TEE_MALLOC_NO_FILL   0x00000001
837777dab0Sopenharmony_ci#define TEE_MALLOC_NO_SHARE  0x00000002
847777dab0Sopenharmony_ci
857777dab0Sopenharmony_ci#define TEE_MEMORY_ACCESS_READ      0x00000001
867777dab0Sopenharmony_ci#define TEE_MEMORY_ACCESS_WRITE     0x00000002
877777dab0Sopenharmony_ci#define TEE_MEMORY_ACCESS_ANY_OWNER 0x00000004
887777dab0Sopenharmony_ci
897777dab0Sopenharmony_ci#if defined(API_LEVEL) && (API_LEVEL >= API_LEVEL1_2)
907777dab0Sopenharmony_ci/**
917777dab0Sopenharmony_ci * @brief Fills <b>x</b> into the first <b>size</b> bytes of the buffer.
927777dab0Sopenharmony_ci *
937777dab0Sopenharmony_ci * @param buffer Indicates the pointer to the buffer.
947777dab0Sopenharmony_ci * @param x Indicates the value to fill.
957777dab0Sopenharmony_ci * @param size Indicates the number of bytes to fill.
967777dab0Sopenharmony_ci *
977777dab0Sopenharmony_ci * @since 12
987777dab0Sopenharmony_ci * @version 1.0
997777dab0Sopenharmony_ci */
1007777dab0Sopenharmony_civoid TEE_MemFill(void *buffer, uint8_t x, size_t size);
1017777dab0Sopenharmony_ci#else
1027777dab0Sopenharmony_ci/**
1037777dab0Sopenharmony_ci * @brief Fills <b>x</b> into the first <b>size</b> bytes of the buffer.
1047777dab0Sopenharmony_ci *
1057777dab0Sopenharmony_ci * @param buffer Indicates the pointer to the buffer.
1067777dab0Sopenharmony_ci * @param x Indicates the value to fill.
1077777dab0Sopenharmony_ci * @param size Indicates the number of bytes to fill.
1087777dab0Sopenharmony_ci *
1097777dab0Sopenharmony_ci * @since 12
1107777dab0Sopenharmony_ci * @version 1.0
1117777dab0Sopenharmony_ci */
1127777dab0Sopenharmony_civoid TEE_MemFill(void *buffer, uint32_t x, size_t size);
1137777dab0Sopenharmony_ci#endif
1147777dab0Sopenharmony_ci
1157777dab0Sopenharmony_ci/**
1167777dab0Sopenharmony_ci * @brief Copies bytes.
1177777dab0Sopenharmony_ci *
1187777dab0Sopenharmony_ci * @param dest Indicates the pointer to the buffer that holds the bytes copied.
1197777dab0Sopenharmony_ci * @param src Indicates the pointer to the buffer that holds the bytes to copy.
1207777dab0Sopenharmony_ci * @param size Indicates the number of bytes to copy.
1217777dab0Sopenharmony_ci *
1227777dab0Sopenharmony_ci * @since 12
1237777dab0Sopenharmony_ci * @version 1.0
1247777dab0Sopenharmony_ci */
1257777dab0Sopenharmony_civoid TEE_MemMove(void *dest, const void *src, size_t size);
1267777dab0Sopenharmony_ci
1277777dab0Sopenharmony_ci/**
1287777dab0Sopenharmony_ci * @brief Allocates space of the specified size for an object.
1297777dab0Sopenharmony_ci *
1307777dab0Sopenharmony_ci * @param size Indicates the size of the memory to be allocated.
1317777dab0Sopenharmony_ci * @param hint Indicates a hint to the allocator. The value <b>0</b> indicates that the memory block
1327777dab0Sopenharmony_ci * returned is filled with "\0".
1337777dab0Sopenharmony_ci *
1347777dab0Sopenharmony_ci * @return Returns a pointer to the newly allocated space if the operation is successful.
1357777dab0Sopenharmony_ci * @return Returns a <b>NULL</b> pointer if the allocation fails.
1367777dab0Sopenharmony_ci *
1377777dab0Sopenharmony_ci * @since 12
1387777dab0Sopenharmony_ci * @version 1.0
1397777dab0Sopenharmony_ci */
1407777dab0Sopenharmony_civoid *TEE_Malloc(size_t size, uint32_t hint);
1417777dab0Sopenharmony_ci
1427777dab0Sopenharmony_ci /**
1437777dab0Sopenharmony_ci * @brief Releases the memory allocated by <b>TEE_Malloc</b>.
1447777dab0Sopenharmony_ci *
1457777dab0Sopenharmony_ci * If the buffer is a <b>NULL</b> pointer, <b>TEE_Free</b> does nothing.
1467777dab0Sopenharmony_ci * The buffer to be released must have been allocated by <b>TEE_Malloc</b> or <b>TEE_Realloc</b> and cannot be
1477777dab0Sopenharmony_ci * released repeatedly. Otherwise, unexpected result may be caused.
1487777dab0Sopenharmony_ci *
1497777dab0Sopenharmony_ci * @param buffer Indicates the pointer to the memory to release.
1507777dab0Sopenharmony_ci *
1517777dab0Sopenharmony_ci * @since 12
1527777dab0Sopenharmony_ci * @version 1.0
1537777dab0Sopenharmony_ci */
1547777dab0Sopenharmony_civoid TEE_Free(void *buffer);
1557777dab0Sopenharmony_ci
1567777dab0Sopenharmony_ci/**
1577777dab0Sopenharmony_ci * @brief Reallocates memory.
1587777dab0Sopenharmony_ci *
1597777dab0Sopenharmony_ci * If <b>new_size</b> is greater than the old size, the content of the original memory does not change
1607777dab0Sopenharmony_ci * and the space in excess of the old size contains unspecified content.
1617777dab0Sopenharmony_ci * If the new size of the memory object requires movement of the object, the space for the previous
1627777dab0Sopenharmony_ci * instantiation of the object is deallocated.
1637777dab0Sopenharmony_ci * If the space cannot be allocated, the original object remains allocated and this function
1647777dab0Sopenharmony_ci * returns a <b>NULL</b> pointer.
1657777dab0Sopenharmony_ci * If the buffer is <b>NULL</b>, this function is equivalent to <b>TEE_Malloc</b>.
1667777dab0Sopenharmony_ci *
1677777dab0Sopenharmony_ci * @param buffer Indicates the pointer to the memory to reallocate.
1687777dab0Sopenharmony_ci * @param new_size Indicates the new size required.
1697777dab0Sopenharmony_ci *
1707777dab0Sopenharmony_ci * @return Returns a pointer to the allocated memory if the operation is successful.
1717777dab0Sopenharmony_ci * @return Returns a <b>NULL</b> pointer if the operation fails.
1727777dab0Sopenharmony_ci *
1737777dab0Sopenharmony_ci * @since 12
1747777dab0Sopenharmony_ci * @version 1.0
1757777dab0Sopenharmony_ci */
1767777dab0Sopenharmony_civoid *TEE_Realloc(void *buffer, size_t new_size);
1777777dab0Sopenharmony_ci
1787777dab0Sopenharmony_ci/**
1797777dab0Sopenharmony_ci * @brief Compares memory content from the beginning.
1807777dab0Sopenharmony_ci *
1817777dab0Sopenharmony_ci * @param buffer1 Indicates the pointer to the first buffer.
1827777dab0Sopenharmony_ci * @param buffer2 Indicates the pointer to the second buffer.
1837777dab0Sopenharmony_ci * @param size Indicates the number of the bytes to compare.
1847777dab0Sopenharmony_ci *
1857777dab0Sopenharmony_ci * @return Returns <b>–1</b> if buffer1 < buffer2.
1867777dab0Sopenharmony_ci * @return Returns <b>0</b> if buffer1 == buffer2.
1877777dab0Sopenharmony_ci * @return Returns <b>1</b> if buffer1 > buffer2.
1887777dab0Sopenharmony_ci *
1897777dab0Sopenharmony_ci * @since 12
1907777dab0Sopenharmony_ci * @version 1.0
1917777dab0Sopenharmony_ci */
1927777dab0Sopenharmony_ciint32_t TEE_MemCompare(const void *buffer1, const void *buffer2, size_t size);
1937777dab0Sopenharmony_ci
1947777dab0Sopenharmony_ci/**
1957777dab0Sopenharmony_ci * @brief Checks whether this TA has the requested permissions to access a buffer.
1967777dab0Sopenharmony_ci *
1977777dab0Sopenharmony_ci * @param accessFlags Indicates the access permissions to check.
1987777dab0Sopenharmony_ci * @param buffer Indicates the pointer to the target buffer.
1997777dab0Sopenharmony_ci * @param size Indicates the size of the buffer to check.
2007777dab0Sopenharmony_ci *
2017777dab0Sopenharmony_ci * @return Returns <b>TEE_SUCCESS</b> if the TA has the requested permissions.
2027777dab0Sopenharmony_ci * @return Returns <b>TEE_ERROR_ACCESS_DENIED</b> otherwise.
2037777dab0Sopenharmony_ci */
2047777dab0Sopenharmony_ciTEE_Result TEE_CheckMemoryAccessRights(uint32_t accessFlags, const void *buffer, size_t size);
2057777dab0Sopenharmony_ci
2067777dab0Sopenharmony_ci/**
2077777dab0Sopenharmony_ci * @brief Sets the TA instance data pointer.
2087777dab0Sopenharmony_ci *
2097777dab0Sopenharmony_ci * @param instanceData Indicates the pointer to the global TA instance data.
2107777dab0Sopenharmony_ci *
2117777dab0Sopenharmony_ci * @since 12
2127777dab0Sopenharmony_ci * @version 1.0
2137777dab0Sopenharmony_ci */
2147777dab0Sopenharmony_civoid TEE_SetInstanceData(void *instanceData);
2157777dab0Sopenharmony_ci
2167777dab0Sopenharmony_ci/**
2177777dab0Sopenharmony_ci * @brief Obtains the instance data pointer set by the TA using <b>TEE_SetInstanceData</b>.
2187777dab0Sopenharmony_ci *
2197777dab0Sopenharmony_ci * @return Returns the pointer to the instance data set by <b>TEE_SetInstanceData</b>
2207777dab0Sopenharmony_ci * @return or <b>NULL</b> if no instance data pointer has been set.
2217777dab0Sopenharmony_ci *
2227777dab0Sopenharmony_ci * @since 12
2237777dab0Sopenharmony_ci * @version 1.0
2247777dab0Sopenharmony_ci */
2257777dab0Sopenharmony_civoid *TEE_GetInstanceData(void);
2267777dab0Sopenharmony_ci
2277777dab0Sopenharmony_ci#ifdef __cplusplus
2287777dab0Sopenharmony_ci}
2297777dab0Sopenharmony_ci#endif
2307777dab0Sopenharmony_ci/** @} */
2317777dab0Sopenharmony_ci#endif