chtssi.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Isidoro Orabona.
  3. This file is part of ChibiOS.
  4. ChibiOS is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. ChibiOS is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. /**
  16. * @file chtssi.h
  17. * @brief tssi module macros and structures.
  18. *
  19. * @addtogroup TSSI
  20. * @{
  21. */
  22. #ifndef CHTSSI_H
  23. #define CHTSSI_H
  24. #include "ch.h"
  25. #include "ccportab.h"
  26. /*===========================================================================*/
  27. /* Module constants. */
  28. /*===========================================================================*/
  29. /* TSSI interface version. This code is returned also at run time by
  30. calling the internal service TS_HND_VERSION.*/
  31. #define TSSI_VERSION 0x01000000 /* 00 major, 000 minor, 000 build.*/
  32. /* Service registry errors as returned by smc.*/
  33. #define SMC_SVC_OK (int32_t)0 /* No error.*/
  34. #define SMC_SVC_INTR (int32_t)-4 /* Service interrupted.*/
  35. #define SMC_SVC_NOENT (int32_t)-2 /* No existent service.*/
  36. #define SMC_SVC_INVALID (int32_t)-22 /* Invalid service
  37. parameter(s).*/
  38. #define SMC_SVC_BADH (int32_t)-9 /* Invalid service handle.*/
  39. #define SMC_SVC_EXIST (int32_t)-17 /* Service already exists.*/
  40. #define SMC_SVC_NHND (int32_t)-23 /* No more services or
  41. service resources.*/
  42. #define SMC_SVC_BUSY (int32_t)-16 /* Service busy.*/
  43. /* Special trusted service handles.*/
  44. #define TS_HND_TRAMP ((ts_state_t *)0) /* Trampoline service handle.*/
  45. #define TS_HND_DISCOVERY ((ts_state_t *)1) /* Discovery service handle.*/
  46. #define TS_HND_STQRY ((ts_state_t *)2) /* Query status service handle.*/
  47. #define TS_HND_IDLE ((ts_state_t *)3) /* Idle service handle.*/
  48. #define TS_HND_VERSION ((ts_state_t *)4) /* Get version service handle.*/
  49. /* Fast call service bitmask.
  50. Service handles that contain this mask access to
  51. the fast call table.*/
  52. #define TS_FASTCALL_MASK 0xFFFF0000
  53. /* Services events event mask.*/
  54. #define EVT_DAEMON_REQ_ATN EVENT_MASK(0)
  55. /*===========================================================================*/
  56. /* Module pre-compile time settings. */
  57. /*===========================================================================*/
  58. /**
  59. * @name TSSI module settings.
  60. * @{
  61. */
  62. /**
  63. * @brief Max number of services.
  64. */
  65. #define TS_MAX_SVCS 64
  66. /**
  67. * @brief Max smc call timeout, in microseconds.
  68. */
  69. #define TS_MAX_TMO 10000
  70. /**
  71. * @brief Secure and non secure memory address spaces.
  72. */
  73. #if !defined(NSEC_MEMORY_START_ADDR)
  74. #define NSEC_MEMORY_START_ADDR ((uint8_t *)0x20000000)
  75. #endif
  76. #if !defined(NSEC_MEMORY_EXE_OFFSET)
  77. #define NSEC_MEMORY_EXE_OFFSET ((uint32_t) 0x00000000)
  78. #endif
  79. #if !defined(NSEC_MEMORY_END_ADDR)
  80. #define NSEC_MEMORY_END_ADDR ((uint8_t *)0x27000000)
  81. #endif
  82. #if !defined(SEC_MEMORY_START_ADDR)
  83. #define SEC_MEMORY_START_ADDR ((uint8_t *)0x27000000)
  84. #endif
  85. #if !defined(SEC_MEMORY_SIZE)
  86. #define SEC_MEMORY_SIZE ((size_t)0x1000000)
  87. #endif
  88. /** @} */
  89. /*===========================================================================*/
  90. /* Derived constants and error checks. */
  91. /*===========================================================================*/
  92. /*===========================================================================*/
  93. /* Module data structures and types. */
  94. /*===========================================================================*/
  95. typedef uint8_t * ts_params_area_t;
  96. typedef struct tssi_service_state {
  97. uint32_t ts_status;
  98. thread_reference_t ts_thdp;
  99. ts_params_area_t ts_datap;
  100. uint32_t ts_datalen;
  101. } ts_state_t;
  102. /**
  103. * @brief Fast call function.
  104. */
  105. typedef msg_t (*fcfunc_t)(ts_params_area_t ts_datap, uint32_t ts_datalen);
  106. /**
  107. * @brief Type of a fast call descriptor.
  108. */
  109. typedef struct {
  110. /**
  111. * @brief Fast call service name.
  112. */
  113. const char *name;
  114. /* The code identifying the service.
  115. Used for checking purpose, it must correspond to the
  116. order that the service lists in the descriptor table.*/
  117. uint32_t code;
  118. /**
  119. * @brief Fast call function pointer.
  120. */
  121. fcfunc_t funcp;
  122. } fc_descriptor_t;
  123. /*===========================================================================*/
  124. /* Module macros. */
  125. /*===========================================================================*/
  126. /**
  127. * @name Services tables definition macros.
  128. * @{
  129. */
  130. /**
  131. * @brief Table of the runtime state of the services.
  132. */
  133. #define TS_STATE_TABLE \
  134. ts_state_t ts_state[TS_MAX_SVCS] = {0};
  135. /**
  136. * @brief Accessor to the runtime state of service i.
  137. */
  138. #define TS_STATE(i) (&ts_state[i])
  139. /**
  140. * @brief Start of user service table.
  141. */
  142. #define TS_CONF_TABLE_BEGIN \
  143. const thread_descriptor_t ts_configs[TS_MAX_SVCS] = {
  144. /**
  145. * @brief Entry of user services table.
  146. */
  147. #define TS_CONF_TABLE_ENTRY(name, wap, prio, funcp, tsstatep) \
  148. {name, wap, ((stkalign_t *)(wap)) + (sizeof (wap) / sizeof(stkalign_t)), \
  149. prio, funcp, tsstatep},
  150. /**
  151. * @brief End of user services table.
  152. */
  153. #define TS_CONF_TABLE_END \
  154. };
  155. /**
  156. * @brief Accessor to the service table entry i.
  157. */
  158. #define TS_CONF_TABLE(i) (&ts_configs[i])
  159. /**
  160. * @brief Trusted services base prio.
  161. */
  162. #define TS_BASE_PRIO (NORMALPRIO+1)
  163. /**
  164. * @brief Set the service status.
  165. * @note The service sets the status at a value representing the status
  166. * of the completion of the request. This value is
  167. * service dependent.
  168. */
  169. #define TS_SET_STATUS(svcp, newst) (((ts_state_t *)svcp)->ts_status = newst)
  170. /**
  171. * @brief Get the pointer to the client shared memory.
  172. * @note The client sets the data field at the start address
  173. * of a shared memory allocated from the non secure memory space.
  174. */
  175. #define TS_GET_DATA(svcp) ((char *)((ts_state_t *)svcp)->ts_datap)
  176. /**
  177. * @brief Get the size of the client shared memory.
  178. * @note The client sets the datalen field to the size
  179. * of a shared memory allocated from the non secure memory space.
  180. */
  181. #define TS_GET_DATALEN(svcp) (((ts_state_t *)svcp)->ts_datalen)
  182. /** @} */
  183. /**
  184. * @name Fast call table definition macros.
  185. * @note Fast call services run at max priority level, so it is
  186. * mandatory that they last less time as possible.
  187. * @note Fast call services should be invoked using
  188. * the tsInvoke0 function in order to optimize the
  189. * performances.
  190. * @note Fast call services don't have a runtime state, so
  191. * the response management is in charge to the higher levels.
  192. * @{
  193. */
  194. /**
  195. * @brief Start of user fast call service table.
  196. */
  197. #define TS_FC_CONF_TABLE_BEGIN \
  198. const fc_descriptor_t ts_fc_configs[] = {
  199. /**
  200. * @brief Entry of user fast call services table.
  201. */
  202. #define TS_FC_CONF_TABLE_ENTRY(name, code, funcp) \
  203. {name, code, funcp},
  204. /**
  205. * @brief End of user fast call services table.
  206. */
  207. #define TS_FC_CONF_TABLE_END \
  208. };
  209. /**
  210. * @brief Accessor to the fast call service table entry i.
  211. */
  212. #define TS_FC_CONF_TABLE(i) (&ts_fc_configs[i])
  213. /**
  214. * @brief Number of entries in the fast call service table.
  215. */
  216. #define TS_FC_CONF_TABLE_N (sizeof ts_fc_configs / sizeof ts_fc_configs[0])
  217. /** @} */
  218. /*===========================================================================*/
  219. /* External declarations. */
  220. /*===========================================================================*/
  221. #ifdef __cplusplus
  222. extern "C" {
  223. #endif
  224. extern event_source_t tsEventSource;
  225. extern ts_state_t ts_state[];
  226. extern const thread_descriptor_t ts_configs[];
  227. extern thread_reference_t _ns_thread;
  228. CC_NO_RETURN void _ns_trampoline(uint8_t *addr);
  229. CC_NO_RETURN void tssiInit(void);
  230. msg_t tssiWaitRequest(ts_state_t *svcp);
  231. bool tsIsAddrSpaceValid(void *addr, size_t size);
  232. #ifdef __cplusplus
  233. }
  234. #endif
  235. /*===========================================================================*/
  236. /* Module inline functions. */
  237. /*===========================================================================*/
  238. #endif /* CHTSSI_H */