smccc_helpers.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (c) 2016-2019, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #ifndef SMCCC_HELPERS_H
  7. #define SMCCC_HELPERS_H
  8. #include <lib/smccc.h>
  9. /* These are offsets to registers in smc_ctx_t */
  10. #define SMC_CTX_GPREG_R0 U(0x0)
  11. #define SMC_CTX_GPREG_R1 U(0x4)
  12. #define SMC_CTX_GPREG_R2 U(0x8)
  13. #define SMC_CTX_GPREG_R3 U(0xC)
  14. #define SMC_CTX_GPREG_R4 U(0x10)
  15. #define SMC_CTX_GPREG_R5 U(0x14)
  16. #define SMC_CTX_SP_USR U(0x34)
  17. #define SMC_CTX_SPSR_MON U(0x78)
  18. #define SMC_CTX_SP_MON U(0x7C)
  19. #define SMC_CTX_LR_MON U(0x80)
  20. #define SMC_CTX_SCR U(0x84)
  21. #define SMC_CTX_PMCR U(0x88)
  22. #define SMC_CTX_SIZE U(0x90)
  23. #ifndef __ASSEMBLER__
  24. #include <stdint.h>
  25. #include <lib/cassert.h>
  26. /*
  27. * The generic structure to save arguments and callee saved registers during
  28. * an SMC. Also this structure is used to store the result return values after
  29. * the completion of SMC service.
  30. */
  31. typedef struct smc_ctx {
  32. u_register_t r0;
  33. u_register_t r1;
  34. u_register_t r2;
  35. u_register_t r3;
  36. u_register_t r4;
  37. u_register_t r5;
  38. u_register_t r6;
  39. u_register_t r7;
  40. u_register_t r8;
  41. u_register_t r9;
  42. u_register_t r10;
  43. u_register_t r11;
  44. u_register_t r12;
  45. /* spsr_usr doesn't exist */
  46. u_register_t sp_usr;
  47. u_register_t lr_usr;
  48. u_register_t spsr_irq;
  49. u_register_t sp_irq;
  50. u_register_t lr_irq;
  51. u_register_t spsr_fiq;
  52. u_register_t sp_fiq;
  53. u_register_t lr_fiq;
  54. u_register_t spsr_svc;
  55. u_register_t sp_svc;
  56. u_register_t lr_svc;
  57. u_register_t spsr_abt;
  58. u_register_t sp_abt;
  59. u_register_t lr_abt;
  60. u_register_t spsr_und;
  61. u_register_t sp_und;
  62. u_register_t lr_und;
  63. u_register_t spsr_mon;
  64. /*
  65. * `sp_mon` will point to the C runtime stack in monitor mode. But prior
  66. * to exit from SMC, this will point to the `smc_ctx_t` so that
  67. * on next entry due to SMC, the `smc_ctx_t` can be easily accessed.
  68. */
  69. u_register_t sp_mon;
  70. u_register_t lr_mon;
  71. u_register_t scr;
  72. u_register_t pmcr;
  73. /*
  74. * The workaround for CVE-2017-5715 requires storing information in
  75. * the bottom 3 bits of the stack pointer. Add a padding field to
  76. * force the size of the struct to be a multiple of 8.
  77. */
  78. u_register_t pad;
  79. } smc_ctx_t __aligned(8);
  80. /*
  81. * Compile time assertions related to the 'smc_context' structure to
  82. * ensure that the assembler and the compiler view of the offsets of
  83. * the structure members is the same.
  84. */
  85. CASSERT(SMC_CTX_GPREG_R0 == __builtin_offsetof(smc_ctx_t, r0),
  86. assert_smc_ctx_greg_r0_offset_mismatch);
  87. CASSERT(SMC_CTX_GPREG_R1 == __builtin_offsetof(smc_ctx_t, r1),
  88. assert_smc_ctx_greg_r1_offset_mismatch);
  89. CASSERT(SMC_CTX_GPREG_R2 == __builtin_offsetof(smc_ctx_t, r2),
  90. assert_smc_ctx_greg_r2_offset_mismatch);
  91. CASSERT(SMC_CTX_GPREG_R3 == __builtin_offsetof(smc_ctx_t, r3),
  92. assert_smc_ctx_greg_r3_offset_mismatch);
  93. CASSERT(SMC_CTX_GPREG_R4 == __builtin_offsetof(smc_ctx_t, r4),
  94. assert_smc_ctx_greg_r4_offset_mismatch);
  95. CASSERT(SMC_CTX_SP_USR == __builtin_offsetof(smc_ctx_t, sp_usr),
  96. assert_smc_ctx_sp_usr_offset_mismatch);
  97. CASSERT(SMC_CTX_LR_MON == __builtin_offsetof(smc_ctx_t, lr_mon),
  98. assert_smc_ctx_lr_mon_offset_mismatch);
  99. CASSERT(SMC_CTX_SPSR_MON == __builtin_offsetof(smc_ctx_t, spsr_mon),
  100. assert_smc_ctx_spsr_mon_offset_mismatch);
  101. CASSERT((sizeof(smc_ctx_t) & 0x7U) == 0U, assert_smc_ctx_not_aligned);
  102. CASSERT(SMC_CTX_SIZE == sizeof(smc_ctx_t), assert_smc_ctx_size_mismatch);
  103. /* Convenience macros to return from SMC handler */
  104. #define SMC_RET0(_h) { \
  105. return (uintptr_t)(_h); \
  106. }
  107. #define SMC_RET1(_h, _r0) { \
  108. ((smc_ctx_t *)(_h))->r0 = (_r0); \
  109. SMC_RET0(_h); \
  110. }
  111. #define SMC_RET2(_h, _r0, _r1) { \
  112. ((smc_ctx_t *)(_h))->r1 = (_r1); \
  113. SMC_RET1(_h, (_r0)); \
  114. }
  115. #define SMC_RET3(_h, _r0, _r1, _r2) { \
  116. ((smc_ctx_t *)(_h))->r2 = (_r2); \
  117. SMC_RET2(_h, (_r0), (_r1)); \
  118. }
  119. #define SMC_RET4(_h, _r0, _r1, _r2, _r3) { \
  120. ((smc_ctx_t *)(_h))->r3 = (_r3); \
  121. SMC_RET3(_h, (_r0), (_r1), (_r2)); \
  122. }
  123. #define SMC_RET5(_h, _r0, _r1, _r2, _r3, _r4) { \
  124. ((smc_ctx_t *)(_h))->r4 = (_r4); \
  125. SMC_RET4(_h, (_r0), (_r1), (_r2), (_r3)); \
  126. }
  127. #define SMC_RET6(_h, _r0, _r1, _r2, _r3, _r4, _r5) { \
  128. ((smc_ctx_t *)(_h))->r5 = (_r5); \
  129. SMC_RET5(_h, (_r0), (_r1), (_r2), (_r3), (_r4)); \
  130. }
  131. #define SMC_RET7(_h, _r0, _r1, _r2, _r3, _r4, _r5, _r6) { \
  132. ((smc_ctx_t *)(_h))->r6 = (_r6); \
  133. SMC_RET6(_h, (_r0), (_r1), (_r2), (_r3), (_r4), (_r5)); \
  134. }
  135. #define SMC_RET8(_h, _r0, _r1, _r2, _r3, _r4, _r5, _r6, _r7) { \
  136. ((smc_ctx_t *)(_h))->r7 = (_r7); \
  137. SMC_RET7(_h, (_r0), (_r1), (_r2), (_r3), (_r4), (_r5), (_r6)); \
  138. }
  139. /*
  140. * Helper macro to retrieve the SMC parameters from smc_ctx_t.
  141. */
  142. #define get_smc_params_from_ctx(_hdl, _r1, _r2, _r3, _r4) { \
  143. _r1 = ((smc_ctx_t *)_hdl)->r1; \
  144. _r2 = ((smc_ctx_t *)_hdl)->r2; \
  145. _r3 = ((smc_ctx_t *)_hdl)->r3; \
  146. _r4 = ((smc_ctx_t *)_hdl)->r4; \
  147. }
  148. /* ------------------------------------------------------------------------
  149. * Helper APIs for setting and retrieving appropriate `smc_ctx_t`.
  150. * These functions need to implemented by the BL including this library.
  151. * ------------------------------------------------------------------------
  152. */
  153. /* Get the pointer to `smc_ctx_t` corresponding to the security state. */
  154. void *smc_get_ctx(unsigned int security_state);
  155. /* Set the next `smc_ctx_t` corresponding to the security state. */
  156. void smc_set_next_ctx(unsigned int security_state);
  157. /* Get the pointer to next `smc_ctx_t` already set by `smc_set_next_ctx()`. */
  158. void *smc_get_next_ctx(void);
  159. #endif /*__ASSEMBLER__*/
  160. #endif /* SMCCC_HELPERS_H */