nrf_svci.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /**
  2. * Copyright (c) 2016 - 2019, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. /** @file
  41. *
  42. * @defgroup sdk_nrf_svci Supervisor instructions with indirect number
  43. * @{
  44. * @ingroup app_common
  45. *
  46. * @brief Macros to create Supervisor instructions using indirect number.
  47. */
  48. #ifndef NRF_SVCI_H__
  49. #define NRF_SVCI_H__
  50. #include "stdint.h"
  51. #include "compiler_abstraction.h"
  52. #include "app_util.h"
  53. #ifdef __cplusplus
  54. extern "C" {
  55. #endif
  56. #define NRF_SVCI_SVC_NUM 0 /**< SVC number used for all SVCI functions. */
  57. #define NRF_SVCI_SVC_NUM_INVALID (0xFFFFFFFF) /**< Invalid SVCI number. */
  58. #ifdef __cplusplus
  59. #define GCC_CAST_CPP (uint16_t)
  60. #else
  61. #define GCC_CAST_CPP
  62. #endif
  63. #if (__LINT__ != 1)
  64. #if defined (__CC_ARM)
  65. #define SVCI_DECL(svci_num, return_type, function_name, ...) \
  66. return_type __svc_indirect(NRF_SVCI_SVC_NUM) \
  67. svci_ ## function_name(uint32_t _svci_num, ##__VA_ARGS__);
  68. #define SVCI_DECL_0(svci_num, return_type, function_name) \
  69. return_type __svc_indirect(NRF_SVCI_SVC_NUM) \
  70. svci_ ## function_name(uint32_t _svci_num);
  71. #define SVCI_0(svci_num, return_type, function_name) \
  72. SVCI_DECL_0(svci_num, return_type, function_name) \
  73. static __INLINE return_type function_name(void) \
  74. { \
  75. return svci_ ## function_name(svci_num); \
  76. }
  77. #define SVCI_1(svci_num, return_type, function_name, p0t, p0n) \
  78. SVCI_DECL(svci_num, return_type, function_name, p0t p0n) \
  79. static __INLINE return_type function_name(p0t p0n) \
  80. { \
  81. return svci_ ## function_name(svci_num, p0n); \
  82. }
  83. #define SVCI_2(svci_num, return_type, function_name, p0t, p0n, p1t, p1n) \
  84. SVCI_DECL(svci_num, return_type, function_name, p0t p0n, p1t p1n) \
  85. static __INLINE return_type function_name(p0t p0n, p1t p1n) \
  86. { \
  87. return svci_ ## function_name(svci_num, p0n, p1n); \
  88. }
  89. #define SVCI_3(svci_num, return_type, function_name, p0t, p0n, p1t, p1n, p2t, p2n) \
  90. SVCI_DECL(svci_num, return_type, function_name, p0t p0n, p1t p1n, p2t p2n) \
  91. static __INLINE return_type function_name(p0t p0n, p1t p1n, p2t p2n) \
  92. { \
  93. return svci_ ## function_name(svci_num, p0n, p1n, p2n); \
  94. }
  95. #define SVCI_4(svci_num, return_type, function_name, p0t, p0n, p1t, p1n, p2t, p2n, p3t, p3n) \
  96. SVCI_DECL(svci_num, return_type, function_name, p0t p0n, p1t p1n, p2t p2n, p3t p3n) \
  97. static __INLINE return_type function_name(p0t p0n, p1t p1n, p2t p2n, p3t p3n) \
  98. { \
  99. return svci_ ## function_name(svci_num, p0n, p1n, p2n, p3n); \
  100. }
  101. #else
  102. #if defined (__GNUC__)
  103. #define SVCI_DECL_0(svci_num, return_type, function_name) \
  104. _Pragma("GCC diagnostic push") \
  105. _Pragma("GCC diagnostic ignored \"-Wreturn-type\"") \
  106. __attribute__((naked, unused)) \
  107. static return_type svci_ ## function_name(void) \
  108. { \
  109. /* Do the SuperVisor call by using svc instruction with \
  110. R12 containing the SVCI number */ \
  111. __ASM __volatile \
  112. ( \
  113. " ldr r12, =%0 \n" \
  114. " svc %1 \n" \
  115. " bx lr \n" \
  116. " .ltorg" \
  117. : /* output */ \
  118. : /* input */ \
  119. "X"(svci_num), \
  120. "I"(GCC_CAST_CPP NRF_SVCI_SVC_NUM) \
  121. : /* clobbers */ \
  122. "r12" \
  123. ); \
  124. } \
  125. _Pragma("GCC diagnostic pop")
  126. #define SVCI_DECL(svci_num, return_type, function_name, ...) \
  127. _Pragma("GCC diagnostic push") \
  128. _Pragma("GCC diagnostic ignored \"-Wreturn-type\"") \
  129. __attribute__((naked, unused)) \
  130. static return_type svci_ ## function_name(__VA_ARGS__) \
  131. { \
  132. /* Do the SuperVisor call by using svc instruction with \
  133. R12 containing the SVCI number */ \
  134. __ASM __volatile \
  135. ( \
  136. " ldr.w r12, =%0 \n" \
  137. " svc %1 \n" \
  138. " bx lr \n" \
  139. " .ltorg" \
  140. : /* output */ \
  141. : /* input */ \
  142. "X"(svci_num), \
  143. "I"(GCC_CAST_CPP NRF_SVCI_SVC_NUM) \
  144. : /* clobbers */ \
  145. "r12" \
  146. ); \
  147. } \
  148. _Pragma("GCC diagnostic pop")
  149. #elif defined (__ICCARM__)
  150. #define SVCI_DECL_0(svci_num, return_type, function_name) \
  151. /* Suppress return value warning. */ \
  152. _Pragma("diag_suppress=Pe940") \
  153. static __root return_type svci_ ## function_name(void) \
  154. { \
  155. /* Do the SuperVisor call by using svc instruction with \
  156. R12 containing the SVCI number */ \
  157. __ASM volatile \
  158. ( \
  159. " mov r12, %0 \n" \
  160. " svc %1 \n" \
  161. : /*no output*/ \
  162. : "r" (svci_num), "I" (NRF_SVCI_SVC_NUM) \
  163. : \
  164. ); \
  165. }
  166. #define SVCI_DECL(svci_num, return_type, function_name, ...) \
  167. /* Suppress return value warning. */ \
  168. _Pragma("diag_suppress=Pe940") \
  169. static __root return_type svci_ ## function_name(__VA_ARGS__) \
  170. { \
  171. /* We stack r0-r3 as r0 is used to set high register (r12) \
  172. This CODE MUST BE IN ITS OWN __ASM BLOCK! */ \
  173. __ASM volatile ( "push {r0, r1, r2, r3}\n\t" ); \
  174. /* Set R12 to the svc_number, this will use r0 as indirect \
  175. * storage. Pop r0-r3 to reset value before SVCI. */ \
  176. __ASM volatile \
  177. ( \
  178. " mov r12, %0 \n" \
  179. " pop {r0, r1, r2, r3} \n" \
  180. : /*no output */ \
  181. : "r" (svci_num) \
  182. : \
  183. ); \
  184. /* Do the SuperVisor call by using svc instruction with \
  185. R12 containing the SVCI number */ \
  186. __ASM volatile \
  187. ( \
  188. " svc %0 \n" \
  189. " bx lr \n" \
  190. : /*no output*/ \
  191. : "I" (NRF_SVCI_SVC_NUM) \
  192. : \
  193. ); \
  194. }
  195. #else // Not defined (__ICCARM__) or defined (__GNUC__)
  196. #error Unsupported compiler for SVCI interface
  197. #endif // Not defined (__ICCARM__) or defined (__GNUC__)
  198. #define SVCI_0(svci_num, return_type, function_name) \
  199. SVCI_DECL_0(svci_num, return_type, function_name) \
  200. static __INLINE return_type function_name(void) \
  201. { \
  202. return svci_ ## function_name(); \
  203. }
  204. #define SVCI_1(svci_num, return_type, function_name, p0t, p0n) \
  205. SVCI_DECL(svci_num, return_type, function_name, p0t p0n) \
  206. static __INLINE return_type function_name(p0t p0n) \
  207. { \
  208. return svci_ ## function_name(p0n); \
  209. }
  210. #define SVCI_2(svci_num, return_type, function_name, p0t, p0n, p1t, p1n) \
  211. SVCI_DECL(svci_num, return_type, function_name, p0t p0n, p1t p1n) \
  212. static __INLINE return_type function_name(p0t p0n, p1t p1n) \
  213. { \
  214. return svci_ ## function_name(p0n, p1n); \
  215. }
  216. #define SVCI_3(svci_num, return_type, function_name, p0t, p0n, p1t, p1n, p2t, p2n) \
  217. SVCI_DECL(svci_num, return_type, function_name, p0t p0n, p1t p1n, p2t p2n) \
  218. static __INLINE return_type function_name(p0t p0n, p1t p1n, p2t p2n) \
  219. { \
  220. return svci_ ## function_name(p0n, p1n, p2n); \
  221. }
  222. #define SVCI_4(svci_num, return_type, function_name, p0t, p0n, p1t, p1n, p2t, p2n, p3t, p3n) \
  223. SVCI_DECL(svci_num, return_type, function_name, p0t p0n, p1t p1n, p2t p2n, p3t p3n) \
  224. static __INLINE return_type function_name(p0t p0n, p1t p1n, p2t p2n, p3t p3n) \
  225. { \
  226. return svci_ ## function_name(p0n, p1n, p2n, p3n); \
  227. }
  228. #endif // Not __CC_ARM
  229. #define VA_NARGS_IMPL(_0, _1, _2, _3, _4, _5, _6, _7, _8, N, ...) N
  230. #define VA_NARGS(...) VA_NARGS_IMPL(__VA_ARGS__, 4, 4, 3, 3, 2, 2, 1, 1, 0)
  231. #ifdef SVCALL_INDIRECT_AS_NORMAL_FUNCTION
  232. #define SVCI(svci_num, return_type, function_name, ...) \
  233. return_type function_name(##__VA_ARGS__)
  234. #else
  235. #define SVCI_IMPLI(count, svci_num, return_type, function_name, ...) \
  236. SVCI##_##count (svci_num, return_type, function_name, ##__VA_ARGS__)
  237. #define SVCI_IMPL(count, svci_num, return_type, function_name, ...) \
  238. SVCI_IMPLI(count, svci_num, return_type, function_name, ##__VA_ARGS__)
  239. #define SVCI(svci_num, return_type, function_name, ...) \
  240. SVCI_IMPL(VA_NARGS(__VA_ARGS__), svci_num, return_type, function_name, ##__VA_ARGS__)
  241. #endif // SVCALL_INDIRECT_AS_NORMAL_FUNCTION
  242. #else // (__LINT__ == 1)
  243. #define SVCI(svci_num, return_type, function_name, ...)
  244. #endif
  245. #ifdef __cplusplus
  246. }
  247. #endif
  248. #endif // NRF_SVCI_H__
  249. /** @} */