secure_init.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * FreeRTOS Kernel V10.4.6
  3. * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * SPDX-License-Identifier: MIT
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  8. * this software and associated documentation files (the "Software"), to deal in
  9. * the Software without restriction, including without limitation the rights to
  10. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  11. * the Software, and to permit persons to whom the Software is furnished to do so,
  12. * subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in all
  15. * copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  19. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  20. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  21. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * https://www.FreeRTOS.org
  25. * https://github.com/FreeRTOS
  26. *
  27. */
  28. /* Standard includes. */
  29. #include <stdint.h>
  30. /* Secure init includes. */
  31. #include "secure_init.h"
  32. /* Secure port macros. */
  33. #include "secure_port_macros.h"
  34. /**
  35. * @brief Constants required to manipulate the SCB.
  36. */
  37. #define secureinitSCB_AIRCR ( ( volatile uint32_t * ) 0xe000ed0c ) /* Application Interrupt and Reset Control Register. */
  38. #define secureinitSCB_AIRCR_VECTKEY_POS ( 16UL )
  39. #define secureinitSCB_AIRCR_VECTKEY_MASK ( 0xFFFFUL << secureinitSCB_AIRCR_VECTKEY_POS )
  40. #define secureinitSCB_AIRCR_PRIS_POS ( 14UL )
  41. #define secureinitSCB_AIRCR_PRIS_MASK ( 1UL << secureinitSCB_AIRCR_PRIS_POS )
  42. /**
  43. * @brief Constants required to manipulate the FPU.
  44. */
  45. #define secureinitFPCCR ( ( volatile uint32_t * ) 0xe000ef34 ) /* Floating Point Context Control Register. */
  46. #define secureinitFPCCR_LSPENS_POS ( 29UL )
  47. #define secureinitFPCCR_LSPENS_MASK ( 1UL << secureinitFPCCR_LSPENS_POS )
  48. #define secureinitFPCCR_TS_POS ( 26UL )
  49. #define secureinitFPCCR_TS_MASK ( 1UL << secureinitFPCCR_TS_POS )
  50. #define secureinitNSACR ( ( volatile uint32_t * ) 0xe000ed8c ) /* Non-secure Access Control Register. */
  51. #define secureinitNSACR_CP10_POS ( 10UL )
  52. #define secureinitNSACR_CP10_MASK ( 1UL << secureinitNSACR_CP10_POS )
  53. #define secureinitNSACR_CP11_POS ( 11UL )
  54. #define secureinitNSACR_CP11_MASK ( 1UL << secureinitNSACR_CP11_POS )
  55. /*-----------------------------------------------------------*/
  56. secureportNON_SECURE_CALLABLE void SecureInit_DePrioritizeNSExceptions( void )
  57. {
  58. uint32_t ulIPSR;
  59. /* Read the Interrupt Program Status Register (IPSR) value. */
  60. secureportREAD_IPSR( ulIPSR );
  61. /* Do nothing if the processor is running in the Thread Mode. IPSR is zero
  62. * when the processor is running in the Thread Mode. */
  63. if( ulIPSR != 0 )
  64. {
  65. *( secureinitSCB_AIRCR ) = ( *( secureinitSCB_AIRCR ) & ~( secureinitSCB_AIRCR_VECTKEY_MASK | secureinitSCB_AIRCR_PRIS_MASK ) ) |
  66. ( ( 0x05FAUL << secureinitSCB_AIRCR_VECTKEY_POS ) & secureinitSCB_AIRCR_VECTKEY_MASK ) |
  67. ( ( 0x1UL << secureinitSCB_AIRCR_PRIS_POS ) & secureinitSCB_AIRCR_PRIS_MASK );
  68. }
  69. }
  70. /*-----------------------------------------------------------*/
  71. secureportNON_SECURE_CALLABLE void SecureInit_EnableNSFPUAccess( void )
  72. {
  73. uint32_t ulIPSR;
  74. /* Read the Interrupt Program Status Register (IPSR) value. */
  75. secureportREAD_IPSR( ulIPSR );
  76. /* Do nothing if the processor is running in the Thread Mode. IPSR is zero
  77. * when the processor is running in the Thread Mode. */
  78. if( ulIPSR != 0 )
  79. {
  80. /* CP10 = 1 ==> Non-secure access to the Floating Point Unit is
  81. * permitted. CP11 should be programmed to the same value as CP10. */
  82. *( secureinitNSACR ) |= ( secureinitNSACR_CP10_MASK | secureinitNSACR_CP11_MASK );
  83. /* LSPENS = 0 ==> LSPEN is writable fron non-secure state. This ensures
  84. * that we can enable/disable lazy stacking in port.c file. */
  85. *( secureinitFPCCR ) &= ~( secureinitFPCCR_LSPENS_MASK );
  86. /* TS = 1 ==> Treat FP registers as secure i.e. callee saved FP
  87. * registers (S16-S31) are also pushed to stack on exception entry and
  88. * restored on exception return. */
  89. *( secureinitFPCCR ) |= ( secureinitFPCCR_TS_MASK );
  90. }
  91. }
  92. /*-----------------------------------------------------------*/