xref: /optee_os/lib/libutee/include/user_ta_header.h (revision 9d224046e71083e0fe3e8c26c22f729399c2c950)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2014, STMicroelectronics International N.V.
4  * Copyright (c) 2018, Linaro Limited.
5  */
6 
7 #ifndef USER_TA_HEADER_H
8 #define USER_TA_HEADER_H
9 
10 #include <tee_api_types.h>
11 #include <util.h>
12 
13 #define TA_FLAG_USER_MODE		0	 /* Deprecated, was (1 << 0) */
14 #define TA_FLAG_EXEC_DDR		0	 /* Deprecated, was (1 << 1) */
15 #define TA_FLAG_SINGLE_INSTANCE		(1 << 2)
16 #define TA_FLAG_MULTI_SESSION		(1 << 3)
17 #define TA_FLAG_INSTANCE_KEEP_ALIVE	(1 << 4) /* remains after last close */
18 #define TA_FLAG_SECURE_DATA_PATH	(1 << 5) /* accesses SDP memory */
19 #define TA_FLAG_REMAP_SUPPORT		0	 /* Deprecated, was (1 << 6) */
20 #define TA_FLAG_CACHE_MAINTENANCE	(1 << 7) /* use cache flush syscall */
21 	/*
22 	 * TA instance can execute multiple sessions concurrently
23 	 * (pseudo-TAs only).
24 	 */
25 #define TA_FLAG_CONCURRENT		(1 << 8)
26 	/*
27 	 * Device enumeration is done in two stages by the normal world, first
28 	 * before the tee-supplicant has started and then once more when the
29 	 * tee-supplicant is started. The flags below control if the TA should
30 	 * be reported in the first or second or case.
31 	 */
32 #define TA_FLAG_DEVICE_ENUM		(1 << 9)  /* without tee-supplicant */
33 #define TA_FLAG_DEVICE_ENUM_SUPP	(1 << 10) /* with tee-supplicant */
34 
35 #define TA_FLAGS_MASK			GENMASK_32(10, 0)
36 
37 struct ta_head {
38 	TEE_UUID uuid;
39 	uint32_t stack_size;
40 	uint32_t flags;
41 	uint64_t depr_entry;
42 };
43 
44 #if defined(CFG_FTRACE_SUPPORT)
45 #define FTRACE_RETFUNC_DEPTH		50
46 union compat_ptr {
47 	uint64_t ptr64;
48 	struct {
49 		uint32_t lo;
50 		uint32_t hi;
51 	} ptr32;
52 };
53 
54 struct __ftrace_info {
55 	union compat_ptr buf_start;
56 	union compat_ptr buf_end;
57 	union compat_ptr ret_ptr;
58 };
59 
60 struct ftrace_buf {
61 	uint64_t ret_func_ptr;	/* __ftrace_return pointer */
62 	uint64_t ret_stack[FTRACE_RETFUNC_DEPTH]; /* Return stack */
63 	uint32_t ret_idx;	/* Return stack index */
64 	uint32_t lr_idx;	/* lr index used for stack unwinding */
65 	uint64_t begin_time[FTRACE_RETFUNC_DEPTH]; /* Timestamp */
66 	uint64_t suspend_time;	/* Suspend timestamp */
67 	uint32_t curr_size;	/* Size of ftrace buffer */
68 	uint32_t max_size;	/* Max allowed size of ftrace buffer */
69 	uint32_t head_off;	/* Ftrace buffer header offset */
70 	uint32_t buf_off;	/* Ftrace buffer offset */
71 	bool syscall_trace_enabled; /* Some syscalls are never traced */
72 	bool syscall_trace_suspended; /* By foreign interrupt or RPC */
73 };
74 
75 /* Defined by the linker script */
76 extern struct ftrace_buf __ftrace_buf_start;
77 extern uint8_t __ftrace_buf_end[];
78 
79 unsigned long ftrace_return(void);
80 void __ftrace_return(void);
81 #endif
82 
83 /*
84  * Pointers to ELF initialization and finalization functions are extracted by
85  * ldelf and stored on the TA heap. They can be accessed via the TA global
86  * variable __init_fini_info::ifs, but the functions are meant to called via
87  * __utee_call_elf_init_fn() and __utee_call_elf_fini_fn().
88  */
89 
90 struct __init_fini {
91 	uint32_t flags;
92 	uint16_t init_size;
93 	uint16_t fini_size;
94 
95 	void (**init)(void); /* @init_size entries */
96 	void (**fini)(void); /* @fini_size entries */
97 };
98 
99 #define __IFS_VALID		BIT(0)
100 #define __IFS_INIT_HAS_RUN	BIT(1)
101 #define __IFS_FINI_HAS_RUN	BIT(2)
102 
103 struct __init_fini_info {
104 	uint32_t reserved;
105 	uint16_t size;
106 	uint16_t pad;
107 	struct __init_fini *ifs; /* @size entries */
108 };
109 
110 /* 32-bit variants for a 64-bit ldelf to access a 32-bit TA */
111 
112 struct __init_fini32 {
113 	uint32_t flags;
114 	uint16_t init_size;
115 	uint16_t fini_size;
116 	uint32_t init;
117 	uint32_t fini;
118 };
119 
120 struct __init_fini_info32 {
121 	uint32_t reserved;
122 	uint16_t size;
123 	uint16_t pad;
124 	uint32_t ifs;
125 };
126 
127 void __utee_call_elf_init_fn(void);
128 void __utee_call_elf_fini_fn(void);
129 
130 void __utee_tcb_init(void);
131 
132 /*
133  * Information about the ELF objects loaded by the application
134  */
135 
136 struct __elf_phdr_info {
137 	uint32_t reserved;
138 	uint16_t count;
139 	uint8_t reserved2;
140 	char zero;
141 	struct dl_phdr_info *dlpi; /* @count entries */
142 };
143 
144 /* 32-bit variant for a 64-bit ldelf to access a 32-bit TA */
145 struct __elf_phdr_info32 {
146 	uint32_t reserved;
147 	uint16_t count;
148 	uint8_t reserved2;
149 	char zero;
150 	uint32_t dlpi;
151 };
152 
153 extern struct __elf_phdr_info __elf_phdr_info;
154 
155 #define TA_PROP_STR_SINGLE_INSTANCE	"gpd.ta.singleInstance"
156 #define TA_PROP_STR_MULTI_SESSION	"gpd.ta.multiSession"
157 #define TA_PROP_STR_KEEP_ALIVE		"gpd.ta.instanceKeepAlive"
158 #define TA_PROP_STR_DATA_SIZE		"gpd.ta.dataSize"
159 #define TA_PROP_STR_STACK_SIZE		"gpd.ta.stackSize"
160 #define TA_PROP_STR_VERSION		"gpd.ta.version"
161 #define TA_PROP_STR_DESCRIPTION		"gpd.ta.description"
162 #define TA_PROP_STR_UNSAFE_PARAM	"op-tee.unsafe_param"
163 #define TA_PROP_STR_REMAP		"op-tee.remap"
164 #define TA_PROP_STR_CACHE_SYNC		"op-tee.cache_sync"
165 
166 enum user_ta_prop_type {
167 	USER_TA_PROP_TYPE_BOOL,	/* bool */
168 	USER_TA_PROP_TYPE_U32,	/* uint32_t */
169 	USER_TA_PROP_TYPE_UUID,	/* TEE_UUID */
170 	USER_TA_PROP_TYPE_IDENTITY,	/* TEE_Identity */
171 	USER_TA_PROP_TYPE_STRING,	/* zero terminated string of char */
172 	USER_TA_PROP_TYPE_BINARY_BLOCK,	/* zero terminated base64 coded string */
173 };
174 
175 enum user_ta_core_service_id {
176 	USER_TA_CORE_ENTRY_MATH_INIT = 0x00000010,
177 	USER_TA_CORE_ENTRY_GARBAGE = 0x00000011,
178 	USER_TA_CORE_ENTRY_CLOSESESSION = 0x00000012,
179 };
180 
181 struct user_ta_property {
182 	const char *name;
183 	enum user_ta_prop_type type;
184 	const void *value;
185 };
186 
187 extern const struct user_ta_property ta_props[];
188 extern const size_t ta_num_props;
189 
190 /* Needed by TEE_CheckMemoryAccessRights() */
191 extern uint32_t ta_param_types;
192 extern TEE_Param ta_params[TEE_NUM_PARAMS];
193 
194 /* Trusted Application Function header */
195 typedef struct ta_func_head {
196 	uint32_t cmd_id;	/* Trusted Application Function ID */
197 	uint32_t start;		/* offset to start func */
198 } ta_func_head_t;
199 
200 int tahead_get_trace_level(void);
201 
202 #endif /* USER_TA_HEADER_H */
203