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_CLIENT_INNER_H_
14#define _TEE_CLIENT_INNER_H_
15
16#include <pthread.h>
17#include <unistd.h>
18#include "tee_client_constants.h"
19#include "tee_client_list.h"
20#include "tee_client_type.h"
21#include "tee_log.h"
22
23#ifdef __cplusplus
24extern "C" {
25#endif
26
27#define IS_TEMP_MEM(paramType)                                                              \
28    (((paramType) == TEEC_MEMREF_TEMP_INPUT) || ((paramType) == TEEC_MEMREF_TEMP_OUTPUT) || \
29     ((paramType) == TEEC_MEMREF_TEMP_INOUT))
30
31#define IS_PARTIAL_MEM(paramType)                                                        \
32    (((paramType) == TEEC_MEMREF_WHOLE) || ((paramType) == TEEC_MEMREF_PARTIAL_INPUT) || \
33     ((paramType) == TEEC_MEMREF_PARTIAL_OUTPUT) || ((paramType) == TEEC_MEMREF_PARTIAL_INOUT))
34
35#define IS_VALUE_MEM(paramType) \
36    (((paramType) == TEEC_VALUE_INPUT) || ((paramType) == TEEC_VALUE_OUTPUT) || ((paramType) == TEEC_VALUE_INOUT))
37
38#define CHECK_ERR_RETURN(val, ref, ret)                                                        \
39    do {                                                                                       \
40        if ((val) != (ref)) {                                                                  \
41            tloge("%{public}d: error: %{public}d\n", __LINE__, (int)val); \
42            return ret;                                                                        \
43        }                                                                                      \
44    } while (0)
45
46#define CHECK_ERR_NO_RETURN(val, ref)                                                          \
47    do {                                                                                       \
48        if ((val) != (ref)) {                                                                  \
49            tloge("%{public}d: error: %{public}d\n", __LINE__, (int)val); \
50            return;                                                                            \
51        }                                                                                      \
52    } while (0)
53
54#define CHECK_ERR_GOTO(val, ref, label)                                                        \
55    do {                                                                                       \
56        if ((val) != (ref)) {                                                                  \
57            tloge("%{public}d: error: %{public}d\n", __LINE__, (int)val); \
58            goto label;                                                                        \
59        }                                                                                      \
60    } while (0)
61
62
63#define MAX_CXTCNT_ONECA 16                 /* one ca only can get 16 contexts */
64#define MAX_TA_PATH_LEN 256
65#define PARAM_SIZE_LIMIT (0x400000 + 0x100) /* 0x100 is for share mem flag etc */
66#define NUM_OF_SHAREMEM_BITMAP 8
67
68#ifndef ZERO_SIZE_PTR
69#define ZERO_SIZE_PTR   ((void *)16)
70#endif
71
72#define BUFF_LEN_MAX 4096
73
74#ifndef PAGE_SIZE
75#define PAGE_SIZE getpagesize()
76#endif
77
78typedef struct {
79    int32_t fd;                    /* file descriptor */
80    struct ListNode session_list;  /* session list  */
81    struct ListNode shrd_mem_list; /* share memory list */
82    struct {
83        void *buffer;
84        sem_t buffer_barrier;
85    } share_buffer;
86    uint8_t shm_bitmap[NUM_OF_SHAREMEM_BITMAP];
87    struct ListNode c_node; /* context list node  */
88    uint32_t ops_cnt;
89    pthread_mutex_t sessionLock;
90    pthread_mutex_t shrMemLock;
91    pthread_mutex_t shrMemBitMapLock;
92    bool callFromService; /* true:from Service, false:from native */
93} TEEC_ContextInner;
94
95typedef struct {
96    void *buffer;              /* memory pointer */
97    uint32_t size;             /* memory size */
98    uint32_t flags;            /* memory flag, distinguish between input and output, range in #TEEC_SharedMemCtl */
99    uint32_t ops_cnt;          /* memoty operation cnt */
100    bool is_allocated;         /* memory allocated flag, distinguish between registered or distributed */
101    struct ListNode head;      /* head of shared memory list */
102    TEEC_ContextInner *context; /* point to its own TEE environment */
103    uint32_t offset;
104} TEEC_SharedMemoryInner;
105
106typedef struct {
107    const uint8_t *taPath;
108    FILE *taFp;
109} TaFileInfo;
110
111#ifdef __cplusplus
112}
113#endif
114#endif
115