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 BIT32(0) */
14 #define TA_FLAG_EXEC_DDR		0	 /* Deprecated, was BIT32(1) */
15 #define TA_FLAG_SINGLE_INSTANCE		BIT32(2)
16 #define TA_FLAG_MULTI_SESSION		BIT32(3)
17 #define TA_FLAG_INSTANCE_KEEP_ALIVE	BIT32(4) /* remains after last close */
18 #define TA_FLAG_SECURE_DATA_PATH	BIT32(5) /* accesses SDP memory */
19 #define TA_FLAG_REMAP_SUPPORT		0	 /* Deprecated, was BIT32(6) */
20 #define TA_FLAG_CACHE_MAINTENANCE	BIT32(7) /* use cache flush syscall */
21 	/*
22 	 * TA instance can execute multiple sessions concurrently
23 	 * (pseudo-TAs only).
24 	 */
25 #define TA_FLAG_CONCURRENT		BIT32(8)
26 	/*
27 	 * Device enumeration is initiated at multiple stages by the normal
28 	 * world:
29 	 * 1. First when the kernel driver has initialized
30 	 * 2. When RPMB is available via inkernel RPMB routing
31 	 * 3. When the tee-supplicant is started
32 	 *
33 	 * The flags below control at which stage a TA will be enumerated:
34 	 * TA_FLAG_DEVICE_ENUM - at stage 1
35 	 * TA_FLAG_DEVICE_ENUM_TEE_STORAGE_PRIVATE -
36 	 *      when secure storage is available, at stage 2 or 3 depending
37 	 *      on whether TEE_STORAGE_PRIVATE is using RPMB FS
38 	 *      (CFG_REE_FS=n CFG_RPMB_FS=y) or REE FS (CFG_REE_FS=y). The
39 	 *      former utilizes in kernel RPMB routing, and the latter
40 	 *      depends on tee-supplicant to access secure storage.
41 	 * TA_FLAG_DEVICE_ENUM_SUPP - at stage 3
42 	 *
43 	 * The TA is enumerated at stage 2 if
44 	 * TA_FLAG_DEVICE_ENUM_TEE_STORAGE_PRIVATE is set and
45 	 * TEE_STORAGE_PRIVATE is using RPMB FS, or if it's using REE FS it
46 	 * will be enumerated at stage 3.
47 	 */
48 #define TA_FLAG_DEVICE_ENUM		BIT32(9)  /* without tee-supplicant */
49 #define TA_FLAG_DEVICE_ENUM_SUPP	BIT32(10) /* with tee-supplicant */
50 	/* See also "gpd.ta.doesNotCloseHandleOnCorruptObject" */
51 #define TA_FLAG_DONT_CLOSE_HANDLE_ON_CORRUPT_OBJECT \
52 					BIT32(11)
53 #define TA_FLAG_DEVICE_ENUM_TEE_STORAGE_PRIVATE	\
54 					BIT32(12) /* with TEE_STORAGE_PRIVATE */
55 
56 #define TA_FLAGS_MASK			GENMASK_32(12, 0)
57 
58 struct ta_head {
59 	TEE_UUID uuid;
60 	uint32_t stack_size;
61 	uint32_t flags;
62 	uint64_t depr_entry;
63 };
64 
65 #if defined(CFG_FTRACE_SUPPORT)
66 #define FTRACE_RETFUNC_DEPTH		50
67 union compat_ptr {
68 	uint64_t ptr64;
69 	struct {
70 		uint32_t lo;
71 		uint32_t hi;
72 	} ptr32;
73 };
74 
75 struct __ftrace_info {
76 	union compat_ptr buf_start;
77 	union compat_ptr buf_end;
78 	union compat_ptr ret_ptr;
79 };
80 
81 struct ftrace_buf {
82 	uint64_t ret_func_ptr;	/* __ftrace_return pointer */
83 	uint64_t ret_stack[FTRACE_RETFUNC_DEPTH]; /* Return stack */
84 	uint32_t ret_idx;	/* Return stack index */
85 	uint32_t lr_idx;	/* lr index used for stack unwinding */
86 	uint64_t begin_time[FTRACE_RETFUNC_DEPTH]; /* Timestamp */
87 	uint64_t suspend_time;	/* Suspend timestamp */
88 	uint32_t curr_idx;	/* Current entry in the (circular) buffer */
89 	uint32_t max_size;	/* Max allowed size of ftrace buffer */
90 	uint32_t head_off;	/* Ftrace buffer header offset */
91 	uint32_t buf_off;	/* Ftrace buffer offset */
92 	bool syscall_trace_enabled; /* Some syscalls are never traced */
93 	bool syscall_trace_suspended; /* By foreign interrupt or RPC */
94 	bool overflow;		/* Circular buffer has wrapped */
95 };
96 
97 /* Defined by the linker script */
98 extern struct ftrace_buf __ftrace_buf_start;
99 extern uint8_t __ftrace_buf_end[];
100 
101 unsigned long ftrace_return(void);
102 void __ftrace_return(void);
103 #endif
104 
105 void __utee_call_elf_init_fn(void);
106 void __utee_call_elf_fini_fn(void);
107 
108 void __utee_tcb_init(void);
109 
110 /*
111  * Information about the ELF objects loaded by the application
112  */
113 
114 struct __elf_phdr_info {
115 	uint32_t reserved;
116 	uint16_t count;
117 	uint8_t reserved2;
118 	char zero;
119 	struct dl_phdr_info *dlpi; /* @count entries */
120 };
121 
122 /* 32-bit variant for a 64-bit ldelf to access a 32-bit TA */
123 struct __elf_phdr_info32 {
124 	uint32_t reserved;
125 	uint16_t count;
126 	uint8_t reserved2;
127 	char zero;
128 	uint32_t dlpi;
129 };
130 
131 extern struct __elf_phdr_info __elf_phdr_info;
132 
133 #define TA_PROP_STR_SINGLE_INSTANCE	"gpd.ta.singleInstance"
134 #define TA_PROP_STR_MULTI_SESSION	"gpd.ta.multiSession"
135 #define TA_PROP_STR_KEEP_ALIVE		"gpd.ta.instanceKeepAlive"
136 #define TA_PROP_STR_DATA_SIZE		"gpd.ta.dataSize"
137 #define TA_PROP_STR_STACK_SIZE		"gpd.ta.stackSize"
138 #define TA_PROP_STR_VERSION		"gpd.ta.version"
139 #define TA_PROP_STR_DESCRIPTION		"gpd.ta.description"
140 #define TA_PROP_STR_ENDIAN		"gpd.ta.endian"
141 #define TA_PROP_STR_DOES_NOT_CLOSE_HANDLE_ON_CORRUPT_OBJECT \
142 	"gpd.ta.doesNotCloseHandleOnCorruptObject"
143 
144 enum user_ta_prop_type {
145 	USER_TA_PROP_TYPE_BOOL,	/* bool */
146 	USER_TA_PROP_TYPE_U32,	/* uint32_t */
147 	USER_TA_PROP_TYPE_UUID,	/* TEE_UUID */
148 	USER_TA_PROP_TYPE_IDENTITY,	/* TEE_Identity */
149 	USER_TA_PROP_TYPE_STRING,	/* zero terminated string of char */
150 	USER_TA_PROP_TYPE_BINARY_BLOCK,	/* zero terminated base64 coded string */
151 	USER_TA_PROP_TYPE_U64,	/* uint64_t */
152 	USER_TA_PROP_TYPE_INVALID,	/* invalid value */
153 };
154 
155 struct user_ta_property {
156 	const char *name;
157 	enum user_ta_prop_type type;
158 	const void *value;
159 };
160 
161 extern const struct user_ta_property ta_props[];
162 extern const size_t ta_num_props;
163 
164 extern uint8_t __ta_no_share_heap[];
165 extern const size_t __ta_no_share_heap_size;
166 /* Needed by TEE_CheckMemoryAccessRights() */
167 extern uint32_t ta_param_types;
168 extern TEE_Param ta_params[TEE_NUM_PARAMS];
169 extern struct malloc_ctx *__ta_no_share_malloc_ctx;
170 
171 int tahead_get_trace_level(void);
172 
173 #endif /* USER_TA_HEADER_H */
174