xref: /optee_os/core/include/kernel/pseudo_ta.h (revision 956c2d50d60109a6053e14da6ff97d6243ea5d65)
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 
to_pseudo_ta_ctx(struct ts_ctx * ctx)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 /*
63  * Setup session context for a pseudo TA
64  * @uuid: Pseudo TA UUID
65  * @s: Session for which to setup a pseudo TA context
66  *
67  * This function must be called with tee_ta_mutex locked.
68  */
69 TEE_Result tee_ta_init_pseudo_ta_session(const TEE_UUID *uuid,
70 			struct tee_ta_session *s);
71 
72 /*
73  * Helper functions for PTAs to support calls from a TA when CFG_PAN=y
74  */
75 
is_caller_ta_with_pan(void)76 static inline bool is_caller_ta_with_pan(void)
77 {
78 	struct ts_session *s = NULL;
79 
80 	if (!IS_ENABLED(CFG_PAN))
81 		return false;
82 	s = ts_get_calling_session();
83 	return s && is_user_ta_ctx(s->ctx);
84 }
85 
86 /*
87  * If caller is a TA and PAN is enabled, allocate bounce buffers for each
88  * memref in @params and build @bparams, then make *@oparams point to @bparams.
89  * Otherwise just make *@oparams point to @params.
90  */
91 TEE_Result to_bounce_params(uint32_t param_types,
92 			    TEE_Param params[TEE_NUM_PARAMS],
93 			    TEE_Param bparams[TEE_NUM_PARAMS],
94 			    TEE_Param **oparams);
95 
96 /*
97  * If @eparams == @bparams, copy data from @bparams to @params. Otherwise, do
98  * nothing.
99  */
100 TEE_Result from_bounce_params(uint32_t param_types,
101 			      TEE_Param params[TEE_NUM_PARAMS],
102 			      TEE_Param bparams[TEE_NUM_PARAMS],
103 			      TEE_Param *eparams);
104 
105 #endif /* __KERNEL_PSEUDO_TA_H */
106 
107