xref: /optee_os/core/include/kernel/ts_manager.h (revision 6cfa381e534b362afbd103f526b132048e54ba47)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2014, STMicroelectronics International N.V.
4  * Copyright (c) 2017-2020, Linaro Limited
5  * Copyright (c) 2020, Arm Limited
6  */
7 
8 #ifndef __KERNEL_TS_MANAGER_H
9 #define __KERNEL_TS_MANAGER_H
10 
11 #include <sys/queue.h>
12 #include <tee_api_types.h>
13 
14 struct ts_ctx {
15 	TEE_UUID uuid;
16 	const struct ts_ops *ops;
17 };
18 
19 struct thread_scall_regs;
20 struct ts_session {
21 	TAILQ_ENTRY(ts_session) link_tsd;
22 	struct ts_ctx *ctx;	/* Generic TS context */
23 #if defined(CFG_TA_GPROF_SUPPORT)
24 	struct sample_buf *sbuf; /* Profiling data (PC sampling) */
25 #endif
26 #if defined(CFG_FTRACE_SUPPORT)
27 	struct ftrace_buf *fbuf; /* ftrace buffer */
28 #endif
29 	/*
30 	 * Used by PTAs to store session specific information, or used by ldelf
31 	 * syscalls to store handlers of opened TA/SP binaries.
32 	 */
33 	void *user_ctx;
34 	bool (*handle_scall)(struct thread_scall_regs *regs);
35 };
36 
37 enum ts_gprof_status {
38 	TS_GPROF_SUSPEND,
39 	TS_GPROF_RESUME,
40 };
41 
42 /*
43  * struct ts_ops - Trusted Service operations
44  * The operations below are called when:
45  * @enter_open_session():	opening a session to the service
46  * @enter_invoke_cmd():		invoking a command in the service
47  * @enter_close_session():	closing a session to the service
48  * @dump_mem_stats():		dumping heap state of the service
49  * @dump_state():		dumping active memory mappings
50  * @dump_ftrace():		dumping the ftrace data via RPC
51  * @release_state():		the service has panicked and as much state
52  *				as possible need to be released
53  * @destroy():			freeing the struct ts_ctx removing all
54  *				trace of the service
55  * @get_instance_id():		a unique ID of the service is needed
56  * @handle_scall():		handling a syscall from the service
57  * @gprof_set_status():		updating the gprof status of the service
58  */
59 struct ts_ops {
60 	TEE_Result (*enter_open_session)(struct ts_session *s);
61 	TEE_Result (*enter_invoke_cmd)(struct ts_session *s, uint32_t cmd);
62 	void (*enter_close_session)(struct ts_session *s);
63 #if defined(CFG_TA_STATS)
64 	TEE_Result (*dump_mem_stats)(struct ts_session *s);
65 #endif
66 	void (*dump_state)(struct ts_ctx *ctx);
67 	void (*dump_ftrace)(struct ts_ctx *ctx);
68 	void (*release_state)(struct ts_ctx *ctx);
69 	void (*destroy)(struct ts_ctx *ctx);
70 	uint32_t (*get_instance_id)(struct ts_ctx *ctx);
71 	bool (*handle_scall)(struct thread_scall_regs *regs);
72 #ifdef CFG_TA_GPROF_SUPPORT
73 	void (*gprof_set_status)(enum ts_gprof_status status);
74 #endif
75 };
76 
77 struct ts_session *ts_get_current_session(void);
78 struct ts_session *ts_get_current_session_may_fail(void);
79 
80 void ts_push_current_session(struct ts_session *sess);
81 struct ts_session *ts_pop_current_session(void);
82 struct ts_session *ts_get_calling_session(void);
83 
84 #endif /*__KERNEL_TS_MANAGER_H*/
85