xref: /arm-trusted-firmware/common/runtime_svc.c (revision 91f16700b400a8c0651d24a598fc48ee2997a0d7)
1*91f16700Schasinglulu /*
2*91f16700Schasinglulu  * Copyright (c) 2013-2019, ARM Limited and Contributors. All rights reserved.
3*91f16700Schasinglulu  *
4*91f16700Schasinglulu  * SPDX-License-Identifier: BSD-3-Clause
5*91f16700Schasinglulu  */
6*91f16700Schasinglulu 
7*91f16700Schasinglulu #include <assert.h>
8*91f16700Schasinglulu #include <errno.h>
9*91f16700Schasinglulu #include <string.h>
10*91f16700Schasinglulu 
11*91f16700Schasinglulu #include <common/debug.h>
12*91f16700Schasinglulu #include <common/runtime_svc.h>
13*91f16700Schasinglulu 
14*91f16700Schasinglulu /*******************************************************************************
15*91f16700Schasinglulu  * The 'rt_svc_descs' array holds the runtime service descriptors exported by
16*91f16700Schasinglulu  * services by placing them in the 'rt_svc_descs' linker section.
17*91f16700Schasinglulu  * The 'rt_svc_descs_indices' array holds the index of a descriptor in the
18*91f16700Schasinglulu  * 'rt_svc_descs' array. When an SMC arrives, the OEN[29:24] bits and the call
19*91f16700Schasinglulu  * type[31] bit in the function id are combined to get an index into the
20*91f16700Schasinglulu  * 'rt_svc_descs_indices' array. This gives the index of the descriptor in the
21*91f16700Schasinglulu  * 'rt_svc_descs' array which contains the SMC handler.
22*91f16700Schasinglulu  ******************************************************************************/
23*91f16700Schasinglulu uint8_t rt_svc_descs_indices[MAX_RT_SVCS];
24*91f16700Schasinglulu 
25*91f16700Schasinglulu #define RT_SVC_DECS_NUM		((RT_SVC_DESCS_END - RT_SVC_DESCS_START)\
26*91f16700Schasinglulu 					/ sizeof(rt_svc_desc_t))
27*91f16700Schasinglulu 
28*91f16700Schasinglulu /*******************************************************************************
29*91f16700Schasinglulu  * Function to invoke the registered `handle` corresponding to the smc_fid in
30*91f16700Schasinglulu  * AArch32 mode.
31*91f16700Schasinglulu  ******************************************************************************/
32*91f16700Schasinglulu uintptr_t handle_runtime_svc(uint32_t smc_fid,
33*91f16700Schasinglulu 			     void *cookie,
34*91f16700Schasinglulu 			     void *handle,
35*91f16700Schasinglulu 			     unsigned int flags)
36*91f16700Schasinglulu {
37*91f16700Schasinglulu 	u_register_t x1, x2, x3, x4;
38*91f16700Schasinglulu 	unsigned int index;
39*91f16700Schasinglulu 	unsigned int idx;
40*91f16700Schasinglulu 	const rt_svc_desc_t *rt_svc_descs;
41*91f16700Schasinglulu 
42*91f16700Schasinglulu 	assert(handle != NULL);
43*91f16700Schasinglulu 	idx = get_unique_oen_from_smc_fid(smc_fid);
44*91f16700Schasinglulu 	assert(idx < MAX_RT_SVCS);
45*91f16700Schasinglulu 
46*91f16700Schasinglulu 	index = rt_svc_descs_indices[idx];
47*91f16700Schasinglulu 	if (index >= RT_SVC_DECS_NUM)
48*91f16700Schasinglulu 		SMC_RET1(handle, SMC_UNK);
49*91f16700Schasinglulu 
50*91f16700Schasinglulu 	rt_svc_descs = (rt_svc_desc_t *) RT_SVC_DESCS_START;
51*91f16700Schasinglulu 
52*91f16700Schasinglulu 	get_smc_params_from_ctx(handle, x1, x2, x3, x4);
53*91f16700Schasinglulu 
54*91f16700Schasinglulu 	return rt_svc_descs[index].handle(smc_fid, x1, x2, x3, x4, cookie,
55*91f16700Schasinglulu 						handle, flags);
56*91f16700Schasinglulu }
57*91f16700Schasinglulu 
58*91f16700Schasinglulu /*******************************************************************************
59*91f16700Schasinglulu  * Simple routine to sanity check a runtime service descriptor before using it
60*91f16700Schasinglulu  ******************************************************************************/
61*91f16700Schasinglulu static int32_t validate_rt_svc_desc(const rt_svc_desc_t *desc)
62*91f16700Schasinglulu {
63*91f16700Schasinglulu 	if (desc == NULL)
64*91f16700Schasinglulu 		return -EINVAL;
65*91f16700Schasinglulu 
66*91f16700Schasinglulu 	if (desc->start_oen > desc->end_oen)
67*91f16700Schasinglulu 		return -EINVAL;
68*91f16700Schasinglulu 
69*91f16700Schasinglulu 	if (desc->end_oen >= OEN_LIMIT)
70*91f16700Schasinglulu 		return -EINVAL;
71*91f16700Schasinglulu 
72*91f16700Schasinglulu 	if ((desc->call_type != SMC_TYPE_FAST) &&
73*91f16700Schasinglulu 	    (desc->call_type != SMC_TYPE_YIELD))
74*91f16700Schasinglulu 		return -EINVAL;
75*91f16700Schasinglulu 
76*91f16700Schasinglulu 	/* A runtime service having no init or handle function doesn't make sense */
77*91f16700Schasinglulu 	if ((desc->init == NULL) && (desc->handle == NULL))
78*91f16700Schasinglulu 		return -EINVAL;
79*91f16700Schasinglulu 
80*91f16700Schasinglulu 	return 0;
81*91f16700Schasinglulu }
82*91f16700Schasinglulu 
83*91f16700Schasinglulu /*******************************************************************************
84*91f16700Schasinglulu  * This function calls the initialisation routine in the descriptor exported by
85*91f16700Schasinglulu  * a runtime service. Once a descriptor has been validated, its start & end
86*91f16700Schasinglulu  * owning entity numbers and the call type are combined to form a unique oen.
87*91f16700Schasinglulu  * The unique oen is used as an index into the 'rt_svc_descs_indices' array.
88*91f16700Schasinglulu  * The index of the runtime service descriptor is stored at this index.
89*91f16700Schasinglulu  ******************************************************************************/
90*91f16700Schasinglulu void __init runtime_svc_init(void)
91*91f16700Schasinglulu {
92*91f16700Schasinglulu 	int rc = 0;
93*91f16700Schasinglulu 	uint8_t index, start_idx, end_idx;
94*91f16700Schasinglulu 	rt_svc_desc_t *rt_svc_descs;
95*91f16700Schasinglulu 
96*91f16700Schasinglulu 	/* Assert the number of descriptors detected are less than maximum indices */
97*91f16700Schasinglulu 	assert((RT_SVC_DESCS_END >= RT_SVC_DESCS_START) &&
98*91f16700Schasinglulu 			(RT_SVC_DECS_NUM < MAX_RT_SVCS));
99*91f16700Schasinglulu 
100*91f16700Schasinglulu 	/* If no runtime services are implemented then simply bail out */
101*91f16700Schasinglulu 	if (RT_SVC_DECS_NUM == 0U)
102*91f16700Schasinglulu 		return;
103*91f16700Schasinglulu 
104*91f16700Schasinglulu 	/* Initialise internal variables to invalid state */
105*91f16700Schasinglulu 	(void)memset(rt_svc_descs_indices, -1, sizeof(rt_svc_descs_indices));
106*91f16700Schasinglulu 
107*91f16700Schasinglulu 	rt_svc_descs = (rt_svc_desc_t *) RT_SVC_DESCS_START;
108*91f16700Schasinglulu 	for (index = 0U; index < RT_SVC_DECS_NUM; index++) {
109*91f16700Schasinglulu 		rt_svc_desc_t *service = &rt_svc_descs[index];
110*91f16700Schasinglulu 
111*91f16700Schasinglulu 		/*
112*91f16700Schasinglulu 		 * An invalid descriptor is an error condition since it is
113*91f16700Schasinglulu 		 * difficult to predict the system behaviour in the absence
114*91f16700Schasinglulu 		 * of this service.
115*91f16700Schasinglulu 		 */
116*91f16700Schasinglulu 		rc = validate_rt_svc_desc(service);
117*91f16700Schasinglulu 		if (rc != 0) {
118*91f16700Schasinglulu 			ERROR("Invalid runtime service descriptor %p\n",
119*91f16700Schasinglulu 				(void *) service);
120*91f16700Schasinglulu 			panic();
121*91f16700Schasinglulu 		}
122*91f16700Schasinglulu 
123*91f16700Schasinglulu 		/*
124*91f16700Schasinglulu 		 * The runtime service may have separate rt_svc_desc_t
125*91f16700Schasinglulu 		 * for its fast smc and yielding smc. Since the service itself
126*91f16700Schasinglulu 		 * need to be initialized only once, only one of them will have
127*91f16700Schasinglulu 		 * an initialisation routine defined. Call the initialisation
128*91f16700Schasinglulu 		 * routine for this runtime service, if it is defined.
129*91f16700Schasinglulu 		 */
130*91f16700Schasinglulu 		if (service->init != NULL) {
131*91f16700Schasinglulu 			rc = service->init();
132*91f16700Schasinglulu 			if (rc != 0) {
133*91f16700Schasinglulu 				ERROR("Error initializing runtime service %s\n",
134*91f16700Schasinglulu 						service->name);
135*91f16700Schasinglulu 				continue;
136*91f16700Schasinglulu 			}
137*91f16700Schasinglulu 		}
138*91f16700Schasinglulu 
139*91f16700Schasinglulu 		/*
140*91f16700Schasinglulu 		 * Fill the indices corresponding to the start and end
141*91f16700Schasinglulu 		 * owning entity numbers with the index of the
142*91f16700Schasinglulu 		 * descriptor which will handle the SMCs for this owning
143*91f16700Schasinglulu 		 * entity range.
144*91f16700Schasinglulu 		 */
145*91f16700Schasinglulu 		start_idx = (uint8_t)get_unique_oen(service->start_oen,
146*91f16700Schasinglulu 						    service->call_type);
147*91f16700Schasinglulu 		end_idx = (uint8_t)get_unique_oen(service->end_oen,
148*91f16700Schasinglulu 						  service->call_type);
149*91f16700Schasinglulu 		assert(start_idx <= end_idx);
150*91f16700Schasinglulu 		assert(end_idx < MAX_RT_SVCS);
151*91f16700Schasinglulu 		for (; start_idx <= end_idx; start_idx++)
152*91f16700Schasinglulu 			rt_svc_descs_indices[start_idx] = index;
153*91f16700Schasinglulu 	}
154*91f16700Schasinglulu }
155