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_H_ 13 #define __GTASK_CORE_H_ 14 15 #include <dlist.h> 16 #include <ta_lib_img_unpack.h> 17 #include "gtask_msg.h" 18 #include <tee_secfile_load_agent.h> 19 20 #define MAX_STACK_SIZE (8 * 1024 * 1024) 21 22 #define MAX_TA2TA_LEVEL 1 23 #define UINT32_BIT_NUM 32 24 25 #define get_index_by_uint32(n) ((n) / (UINT32_BIT_NUM)) 26 #define get_bit_by_uint32(n) ((n) - ((get_index_by_uint32(n)) * (UINT32_BIT_NUM))) 27 28 typedef union { 29 struct { 30 unsigned int buffer; 31 unsigned int size; 32 } memref; 33 struct { 34 unsigned int a; 35 unsigned int b; 36 } value; 37 struct { 38 unsigned int buffer; 39 unsigned int size; 40 } sharedmem; 41 } tee_param_32; 42 43 typedef union { 44 struct { 45 uint64_t buffer; 46 uint64_t size; 47 } memref; 48 struct { 49 unsigned int a; 50 unsigned int b; 51 } value; 52 struct { 53 uint64_t buffer; 54 uint64_t size; 55 } sharedmem; 56 } tee_param_64; 57 58 struct tee_operation_g32 { 59 uint32_t p_type; 60 tee_param_32 p[TEE_PARAM_NUM]; 61 uint32_t p_h_addr[TEE_PARAM_NUM]; 62 }; 63 64 typedef struct tee_operation_g32 tee_operation_gtask; 65 66 union tee_param_gt { 67 tee_param_32 *param_32; 68 tee_param_64 *param_64; 69 }; 70 71 struct pam_node { 72 tee_operation_gtask op; 73 bool param_type; /* indicate gtask mapped to TA's type: true--64bit false--32bit */ 74 void *p_for_ta; /* gtask malloc it */ 75 void *p_vaddr_gt_tee[TEE_PARAM_NUM]; /* virt tee mem addr of param mems mapping for gt */ 76 void *p_vaddr_gt_ree[TEE_PARAM_NUM]; /* virt ree mem addr of param mems mapping for gt */ 77 }; 78 79 enum secfile_type_t { 80 LOAD_TA = 0, 81 LOAD_SERVICE, 82 LOAD_LIB, 83 LOAD_DYNAMIC_DRV, 84 LOAD_PATCH, 85 LOAD_TYPE_MAX, 86 }; 87 88 struct lib_info { 89 char name[LIB_NAME_MAX]; 90 struct lib_info *next; 91 TEE_Time load_elf_time; 92 tee_img_type_t type; 93 }; 94 95 #define SESSION_ID_LEN 8 96 struct session_struct { 97 struct dlist_node session_list; 98 uint32_t task_id; 99 uint32_t session_id; 100 uint64_t session_context; 101 uint32_t login_method; 102 char name[SERVICE_NAME_MAX + SESSION_ID_LEN]; /* for task name = "service_name + session_id" */ 103 uint32_t ta2ta_from_taskid; /* creator of internal session when TA call TA */ 104 bool cancelable; /* dedicate if this session is calling cacnelable func(TEE_Wait) */ 105 bool agent_pending; 106 /* A list of locks of agents */ 107 struct dlist_node locked_agents; 108 /* Place on waiting list for the agent */ 109 struct dlist_node waiting_agent; 110 uint32_t cmd_type; /* Cmd type, n->s or s->s */ 111 smc_cmd_t *cmd; /* Pointer to cmd that will be contain the answer */ 112 smc_cmd_t cmd_in; /* Incomming smc cmd copy */ 113 void *oper_addr; /* virt addr of ns op's mem */ 114 struct pam_node *pam_node; /* pam node corresponding to one ns op */ 115 struct dlist_node map_mem; /* map mem, ns->gtask or gtask -> TA */ 116 uint32_t ta2ta_level; /* indicate while level in ta2ta */ 117 int ta2ta_handle; /* ta2ta_handle in tee_core_api.c */ 118 bool wait_ta_back_msg; /* filter duplicate msg from ta */ 119 int32_t session_status; 120 struct dlist_node child_ta_sess_head; 121 struct dlist_node child_ta_sess_list; 122 }; 123 124 #define ELF_NOT_EXIST 0 125 #define ELF_EXIST 1 126 127 struct service_struct { 128 struct dlist_node service_list; 129 char name[SERVICE_NAME_MAX]; 130 struct ta_property property; /* other_buff is malloced by gtask */ 131 uint32_t index; 132 struct dlist_node session_head; 133 int ref_cnt; /* indicate the number of ca or ta which lock this service */ 134 uint32_t session_count; 135 /* if we use more than 32 sessions , we should use more u32 bitmap */ 136 uint32_t session_bitmap[TA_SESSION_MAX / UINT32_BIT_NUM + 1]; 137 uint32_t init_build; /* for call cinit00 1 service 1 time */ 138 uint32_t elf_state; /* indicating if elf is exist */ 139 uint32_t service_thread; /* service thread task_id */ 140 smc_cmd_t cmd_in; /* Incomming smc cmd copy, it will be only used by gtask */ 141 bool ta_64bit; /* true: TA is 64bit; false: TA is 32bit */ 142 bool first_open; /* indicate if it's the first time for this serivice to open session */ 143 struct lib_info lib_list_head; 144 TEE_Time load_elf_time; 145 bool is_service_dead; /* true: TA has exception */ 146 bool is_dyn_conf_registed; /* true: dyn conf has been registed */ 147 tee_img_type_t img_type; 148 }; 149 150 struct aescbc_info { 151 uint8_t *in; 152 uint32_t in_len; 153 uint8_t *out; 154 uint32_t out_len; 155 uint8_t *key; 156 }; 157 158 struct pagelist_info { 159 uint64_t page_num; 160 uint64_t page_size; 161 uint64_t sharedmem_offset; 162 uint64_t sharedmem_size; 163 }; 164 165 struct session_struct *find_session_by_ca_pid(uint32_t ca_pid, struct service_struct **service, 166 struct session_struct **session); 167 void process_release_service_for_reuse(struct service_struct *service); 168 void gt_wait_process(uint32_t task_id); 169 #endif 170