xref: /optee_os/core/include/kernel/pseudo_ta.h (revision 19a31ec40245ae01a9adcd206eec2a4bb4479fc9)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2015, Linaro Limited
4  */
5 #ifndef __KERNEL_PSEUDO_TA_H
6 #define __KERNEL_PSEUDO_TA_H
7 
8 #include <assert.h>
9 #include <compiler.h>
10 #include <config.h>
11 #include <kernel/tee_ta_manager.h>
12 #include <kernel/user_ta.h>
13 #include <scattered_array.h>
14 #include <tee_api_types.h>
15 #include <user_ta_header.h>
16 #include <util.h>
17 
18 #define PTA_MANDATORY_FLAGS	(TA_FLAG_SINGLE_INSTANCE | \
19 				TA_FLAG_MULTI_SESSION | \
20 				TA_FLAG_INSTANCE_KEEP_ALIVE)
21 
22 #define PTA_ALLOWED_FLAGS	(PTA_MANDATORY_FLAGS | \
23 				 TA_FLAG_SECURE_DATA_PATH | \
24 				 TA_FLAG_CONCURRENT | \
25 				 TA_FLAG_DEVICE_ENUM)
26 
27 #define PTA_DEFAULT_FLAGS	PTA_MANDATORY_FLAGS
28 
29 struct pseudo_ta_head {
30 	TEE_UUID uuid;
31 	const char *name;
32 	uint32_t flags;
33 
34 	TEE_Result (*create_entry_point)(void);
35 	void (*destroy_entry_point)(void);
36 	TEE_Result (*open_session_entry_point)(uint32_t nParamTypes,
37 			TEE_Param pParams[TEE_NUM_PARAMS],
38 			void **ppSessionContext);
39 	void (*close_session_entry_point)(void *pSessionContext);
40 	TEE_Result (*invoke_command_entry_point)(void *pSessionContext,
41 			uint32_t nCommandID, uint32_t nParamTypes,
42 			TEE_Param pParams[TEE_NUM_PARAMS]);
43 };
44 
45 #define pseudo_ta_register(...)	\
46 	SCATTERED_ARRAY_DEFINE_PG_ITEM(pseudo_tas, struct pseudo_ta_head) = \
47 		{ __VA_ARGS__ }
48 
49 struct pseudo_ta_ctx {
50 	const struct pseudo_ta_head *pseudo_ta;
51 	struct tee_ta_ctx ctx;
52 };
53 
54 bool is_pseudo_ta_ctx(struct ts_ctx *ctx);
55 
56 static inline struct pseudo_ta_ctx *to_pseudo_ta_ctx(struct ts_ctx *ctx)
57 {
58 	assert(is_pseudo_ta_ctx(ctx));
59 	return container_of(ctx, struct pseudo_ta_ctx, ctx.ts_ctx);
60 }
61 
62 TEE_Result tee_ta_init_pseudo_ta_session(const TEE_UUID *uuid,
63 			struct tee_ta_session *s);
64 
65 /*
66  * Helper functions for PTAs to support calls from a TA when CFG_PAN=y
67  */
68 
69 static inline bool is_caller_ta_with_pan(void)
70 {
71 	struct ts_session *s = NULL;
72 
73 	if (!IS_ENABLED(CFG_PAN))
74 		return false;
75 	s = ts_get_calling_session();
76 	return s && is_user_ta_ctx(s->ctx);
77 }
78 
79 /*
80  * If caller is a TA and PAN is enabled, allocate bounce buffers for each
81  * memref in @params and build @bparams, then make *@oparams point to @bparams.
82  * Otherwise just make *@oparams point to @params.
83  */
84 TEE_Result to_bounce_params(uint32_t param_types,
85 			    TEE_Param params[TEE_NUM_PARAMS],
86 			    TEE_Param bparams[TEE_NUM_PARAMS],
87 			    TEE_Param **oparams);
88 
89 /*
90  * If @eparams == @bparams, copy data from @bparams to @params. Otherwise, do
91  * nothing.
92  */
93 TEE_Result from_bounce_params(uint32_t param_types,
94 			      TEE_Param params[TEE_NUM_PARAMS],
95 			      TEE_Param bparams[TEE_NUM_PARAMS],
96 			      TEE_Param *eparams);
97 
98 #endif /* __KERNEL_PSEUDO_TA_H */
99 
100