• 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 #ifndef GTASK_CORE_GTASK_INNER_H
13 #define GTASK_CORE_GTASK_INNER_H
14 
15 #include <dlist.h>
16 #include <ipclib.h>
17 #include "mem_page_ops.h" /* paddr_t */
18 #include "ta_framework.h"
19 #include "sys_timer.h"
20 #include <drv.h>
21 
22 #define GT_ERR_OK             0
23 #define GT_ERR_END_CMD        (-1)
24 #define GT_ERR_CMD_NO_HANDLED (-2)
25 
26 #define CMD_TYPE_NS_TO_SECURE     0x1
27 #define CMD_TYPE_SECURE_TO_SECURE 0x2
28 #define CMD_TYPE_SECURE_CONFIG    0xf
29 
30 #define MAX_SESSION_ID TA_SESSION_MAX
31 
32 #define GT_SHARED_CMD_QUEUES_SIZE 0x1000
33 
34 #define TA_REGION_RELEASE   0U
35 #define TA_REGION_FOR_REUSE 1U
36 
37 #define DEFAULT_TASK_PRIO     10
38 #define DEFAULT_MSG_QUEUE_NUM 2
39 #define LOAD_ALL_TASKS        NULL
40 #define PER_USER_RANGE        (100000) /* multi user range */
41 
42 /* judge return value is error */
43 #define is_err_ret(result) \
44     (((result == TEE_SUCCESS) || (result == TEE_PENDING) || (result == TEE_PENDING2)) ? false : true)
45 
46 #define MAX_SMC_CMD 18
47 
48 /* support linux DECLEAR_BITMAP ops */
49 #undef DIV_ROUND_UP
50 #define DIV_ROUND_UP(n, d) (((n) + (d)-1) / (d))
51 #undef BITS_PER_BYTE
52 #define BITS_PER_BYTE 8
53 #undef BITS_PER_LONG
54 #define BITS_PER_LONG 64
55 #undef BITS_TO_LONGS
56 #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(uint64_t))
57 #undef BIT_MASK
58 #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
59 #undef BIT_WORD
60 #define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
61 #undef DECLEAR_BITMAP
62 #define DECLEAR_BITMAP(name, bits) uint64_t name[BITS_TO_LONGS(bits)]
63 
set_bit(int nr,volatile uint64_t * addr)64 static inline void set_bit(int nr, volatile uint64_t *addr)
65 {
66     uint64_t mask = BIT_MASK(nr);
67     uint64_t *p   = ((uint64_t *)addr) + BIT_WORD(nr);
68     *p |= mask;
69 }
70 
clear_bit(int nr,volatile uint64_t * addr)71 static inline void clear_bit(int nr, volatile uint64_t *addr)
72 {
73     uint64_t mask = BIT_MASK(nr);
74     uint64_t *p   = ((uint64_t *)addr) + BIT_WORD(nr);
75     *p &= ~mask;
76 }
77 
test_bit(int nr,const volatile uint64_t * addr)78 static inline int test_bit(int nr, const volatile uint64_t *addr)
79 {
80     return 1UL & (addr[BIT_WORD(nr)] >> (nr & (BITS_PER_LONG - 1)));
81 }
82 
83 typedef struct {
84     DECLEAR_BITMAP(in_bitmap, MAX_SMC_CMD);
85     DECLEAR_BITMAP(doing_bitmap, MAX_SMC_CMD);
86     DECLEAR_BITMAP(out_bitmap, MAX_SMC_CMD);
87 
88     uint32_t smc_lock;
89     volatile uint32_t last_in;
90     smc_cmd_t in[MAX_SMC_CMD];
91     volatile uint32_t last_out;
92     smc_cmd_t out[MAX_SMC_CMD];
93 } nwd_cmd_t;
94 
95 #define MAGIC_SIZE   16
96 #define MAGIC_STRING "Trusted-magic"
97 
98 #define SHIFT_OFFSET 32
99 #define PARAM_CNT 5
100 
101 /*
102  * One encrypted block, which is aligned with CIPHER_BLOCK_BYTESIZE bytes
103  * Head + Payload + Padding
104  */
105 struct encryption_head {
106     int8_t magic[MAGIC_SIZE];
107     uint32_t payload_len;
108 };
109 
110 #define NUM_OF_SO                   1
111 #define KIND_OF_SO                  2
112 #define MAX_SHA_256_SZ              32
113 #define HASH_PLAINTEXT_SIZE         (MAX_SHA_256_SZ + sizeof(struct encryption_head))
114 #define CIPHER_KEY_BYTESIZE         32 /* AES-256 key size */
115 #define CIPHER_BLOCK_BYTESIZE       16 /* AES-CBC cipher block size */
116 #define HASH_PLAINTEXT_ALIGNED_SIZE ALIGN_UP(HASH_PLAINTEXT_SIZE, CIPHER_BLOCK_BYTESIZE)
117 #define IV_BYTESIZE                 16 /* AES-CBC encryption initialization vector size */
118 
119 struct ta2ta_info_t {
120     int handle;
121     bool is_load_worked; /* indicate if sec file agent has worked */
122 };
123 
124 #define INVALID_SERVICE_INDEX  (-1)
125 #define ERROR_SESSION_ID       (-1)
126 #define INVALID_SESSION_HANDLE (-1)
127 
128 #define NOTIFY_MEM_SIZE   (4 * 1024)
129 
130 /* session context contain of service index(high 16 bits) and session id(low 16 bits) */
131 #define set_session_context_bit(index, id)    ((index << 16) | (id & 0x0000ffff))
132 #define service_index_of_context(context) (context >> 16)
133 #define session_id_of_context(context)    (context & 0x0000ffff)
134 
135 #define GLOBAL_TSK_ID 0
136 #define SMCMGR_PID    6
137 #define REET_TSK_ID   SMCMGR_PID
138 #define TASK_ID_NULL    0xFFFFFFFF
139 
140 struct command_state {
141     uint32_t cmd_id;
142     uint32_t dev_id;
143     uint32_t ta2ta_from_taskid;
144     bool ta2ta_start;
145     bool ta2ta_switch;
146     bool ta2ta_done;
147     bool started;
148 };
149 
150 struct service_attr {
151     bool build_in;
152     bool ta_64bit;
153     uint32_t img_type;
154 };
155 
156 /* list head of agent */
157 struct agent_control {
158     uint32_t id;
159     /* State of the agent, locked by TA */
160     bool locked;
161     uint64_t buffer;
162     paddr_t phys_buffer;
163     uint32_t size;
164     struct session_struct *locking_session;
165     /* The sessions waiting list */
166     struct dlist_node waiting_sessions;
167     /* Part of the global agent list */
168     struct dlist_node list;
169     /* Part of the locked agents in the session object */
170     struct dlist_node session_list;
171 };
172 
173 #endif /* GTASK_CORE_GTASK_INNER_H */
174