• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
24 extern "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 IS_SHARED_MEM(paramType) \
39     ((paramType) == TEEC_MEMREF_SHARED_INOUT)
40 
41 #define MAX_SHAREDMEM_LEN 0x10000000
42 
43 #define CHECK_ERR_RETURN(val, ref, ret)                                                        \
44     do {                                                                                       \
45         if ((val) != (ref)) {                                                                  \
46             tloge("%" PUBLIC "d: error: %" PUBLIC "d\n", __LINE__, (int)(val)); \
47             return ret;                                                                        \
48         }                                                                                      \
49     } while (0)
50 
51 #define CHECK_ERR_NO_RETURN(val, ref)                                                          \
52     do {                                                                                       \
53         if ((val) != (ref)) {                                                                  \
54             tloge("%" PUBLIC "d: error: %" PUBLIC "d\n", __LINE__, (int)(val)); \
55             return;                                                                            \
56         }                                                                                      \
57     } while (0)
58 
59 #define CHECK_ERR_GOTO(val, ref, label)                                                        \
60     do {                                                                                       \
61         if ((val) != (ref)) {                                                                  \
62             tloge("%" PUBLIC "d: error: %" PUBLIC "d\n", __LINE__, (int)(val)); \
63             goto label;                                                                        \
64         }                                                                                      \
65     } while (0)
66 
67 
68 #define MAX_CXTCNT_ONECA 16                 /* one ca only can get 16 contexts */
69 #define MAX_TA_PATH_LEN 256
70 #define PARAM_SIZE_LIMIT (0x400000 + 0x100) /* 0x100 is for share mem flag etc */
71 #define NUM_OF_SHAREMEM_BITMAP 8
72 
73 #ifndef ZERO_SIZE_PTR
74 #define ZERO_SIZE_PTR   ((void *)16)
75 #endif
76 
77 #define BUFF_LEN_MAX 4096
78 
79 #ifndef PAGE_SIZE
80 #define PAGE_SIZE getpagesize()
81 #endif
82 
83 typedef struct {
84     int32_t fd;                    /* file descriptor */
85     struct ListNode session_list;  /* session list  */
86     struct ListNode shrd_mem_list; /* share memory list */
87     struct {
88         void *buffer;
89         sem_t buffer_barrier;
90     } share_buffer;
91     uint8_t shm_bitmap[NUM_OF_SHAREMEM_BITMAP];
92     struct ListNode c_node; /* context list node  */
93     uint32_t ops_cnt;
94     pthread_mutex_t sessionLock;
95     pthread_mutex_t shrMemLock;
96     pthread_mutex_t shrMemBitMapLock;
97     bool callFromService; /* true:from Service, false:from native */
98 } TEEC_ContextInner;
99 
100 typedef struct {
101     void *buffer;              /* memory pointer */
102     uint32_t size;             /* memory size */
103     uint32_t flags;            /* memory flag, distinguish between input and output, range in #TEEC_SharedMemCtl */
104     uint32_t ops_cnt;          /* memoty operation cnt */
105     bool is_allocated;         /* memory allocated flag, distinguish between registered or distributed */
106     struct ListNode head;      /* head of shared memory list */
107     TEEC_ContextInner *context; /* point to its own TEE environment */
108     uint32_t offset;
109 } TEEC_SharedMemoryInner;
110 
111 typedef struct {
112     const uint8_t *taPath;
113     FILE *taFp;
114 } TaFileInfo;
115 
116 typedef TEEC_Result (*InitializeContextFunc)(const char *name, TEEC_Context *context);
117 typedef void (*FinalizeContextFunc)(TEEC_Context *context);
118 typedef TEEC_Result (*OpenSessionFunc)(TEEC_Context *context, TEEC_Session *session, const TEEC_UUID *destination,
119     uint32_t connectionMethod, const void *connectionData, TEEC_Operation *operation, uint32_t *returnOrigin);
120 typedef void (*CloseSessionFunc)(TEEC_Session *session);
121 typedef TEEC_Result (*InvokeCommandFunc)(TEEC_Session *session, uint32_t commandID,
122     TEEC_Operation *operation, uint32_t *returnOrigin);
123 
124 typedef struct {
125     InitializeContextFunc initializeContext;
126     FinalizeContextFunc finalizeContext;
127     OpenSessionFunc openSession;
128     CloseSessionFunc closeSession;
129     InvokeCommandFunc invokeCommand;
130 } TEEC_FuncMap;
131 
132 #ifdef __cplusplus
133 }
134 #endif
135 #endif
136