nrf_svc_handler.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. #include <stdbool.h>
  41. #include <stdint.h>
  42. #include "nrf_svc_function.h"
  43. #include "nrf_error.h"
  44. //lint -esym(526, svc_dataBase) -esym(526, svc_dataLimit)
  45. NRF_SECTION_DEF(svc_data, const nrf_svc_func_t);
  46. #define SVC_DATA_SECTION_ITEM_GET(i) NRF_SECTION_ITEM_GET(svc_data, nrf_svc_func_reg_t, (i))
  47. #define SVC_DATA_SECTION_ITEM_COUNT NRF_SECTION_ITEM_COUNT(svc_data, nrf_svc_func_reg_t)
  48. #ifdef __GNUC__
  49. // Prevent GCC from removing this function (called from assembly branch)
  50. void nrf_svc_handler_c(uint32_t* p_svc_args) __attribute__((used));
  51. #endif
  52. /**@brief Function for handling second stage of Supervisor Calls (SVC).
  53. *
  54. * @details The function will use loop through the registered svc functions stored
  55. * in the named section "svc_data" and will call the registered function
  56. * if the svn_num corresponds with the registration.
  57. *
  58. * @param[in] p_svc_args Argument list for the SVC.
  59. *
  60. * @return This function returns by updating p_svc_arsg[0]. This will be reported back to the caller of SVC
  61. * @ref NRF_ERROR_SVC_HANDLER_MISSING is returned if no SVC handler is implemented for the
  62. * provided svc_num.
  63. */
  64. void nrf_svc_handler_c(uint32_t* p_svc_args)
  65. {
  66. uint32_t const num_funcs = SVC_DATA_SECTION_ITEM_COUNT;
  67. bool handled = false;
  68. uint8_t const svc_num = ((uint8_t *)p_svc_args[6])[-2];
  69. uint32_t svci_num = NRF_SVCI_SVC_NUM_INVALID;
  70. if (svc_num == NRF_SVCI_SVC_NUM)
  71. {
  72. // load the stacked R12 as the svci_num
  73. svci_num = p_svc_args[4];
  74. }
  75. for (uint32_t i = 0; i < num_funcs; i++)
  76. {
  77. nrf_svc_func_reg_t const * func_reg = SVC_DATA_SECTION_ITEM_GET(i);
  78. if (func_reg->svc_num != svc_num)
  79. {
  80. continue;
  81. }
  82. if (svci_num != NRF_SVCI_SVC_NUM_INVALID && func_reg->svci_num != svci_num)
  83. {
  84. continue;
  85. }
  86. // Return value is placed in R0
  87. p_svc_args[0] = func_reg->func_ptr(p_svc_args[0], p_svc_args[1], p_svc_args[2], p_svc_args[3]);
  88. handled = true;
  89. break;
  90. }
  91. if (handled == false)
  92. {
  93. // Return value is placed in R0
  94. p_svc_args[0] = NRF_ERROR_SVC_HANDLER_MISSING;
  95. }
  96. }
  97. /**@brief Function for handling the first stage of Supervisor Calls (SVC) in assembly.
  98. *
  99. * @details The function will use the link register (LR) to determine the stack (PSP or MSP) to be
  100. * used and then decode the SVC number afterwards. After decoding the SVC number,
  101. * @ref C_SVC_Handler is called for further processing of the SVC.
  102. */
  103. #if defined ( __CC_ARM )
  104. __ASM void SVC_Handler(void)
  105. {
  106. tst lr, #4 ; Test bit 2 of EXT_RETURN to see if MSP or PSP is used
  107. ite eq ;
  108. mrseq r0, MSP ; If equal, copy stack pointer from MSP
  109. mrsne r0, PSP ; If not equal, copy stack pointer from PSP
  110. B __cpp(nrf_svc_handler_c) ; Call C-implementation of handler. Exception stack frame in R0
  111. ALIGN 4 ; Protect with alignment
  112. }
  113. #elif defined ( __GNUC__ )
  114. void __attribute__((naked)) SVC_Handler(void)
  115. {
  116. __ASM volatile
  117. (
  118. "tst lr, #4\t\n" // Test bit 2 of EXT_RETURN to see if MSP or PSP is used
  119. "ite eq\t\n" //
  120. "mrseq r0, MSP\t\n" // Move MSP into R0.
  121. "mrsne r0, PSP\t\n" // Move PSP into R0.
  122. "b nrf_svc_handler_c\t\n" // Call C-implementation of handler. Exception stack frame in R0
  123. ".align\t\n" // Protect with alignment
  124. );
  125. }
  126. #elif defined ( __ICCARM__ )
  127. void SVC_Handler(void)
  128. {
  129. __ASM volatile
  130. (
  131. "tst lr, #4\t\n" // Test bit in link register responsible for stack indication.
  132. "ite eq\t\n" // Test bit 2 of EXT_RETURN to see if MSP or PSP is used
  133. "mrseq r0, MSP\t\n" // Move MSP into R0.
  134. "mrsne r0, PSP\t\n" // Move PSP into R0.
  135. "b nrf_svc_handler_c\t\n" : // Call C-implementation of handler. Exception stack frame in R0
  136. );
  137. }
  138. #else
  139. #error Compiler not supported.
  140. #endif