arch.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright (c) 2013-2014 Wind River Systems, Inc.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file
  8. * @brief ARM AArch32 specific kernel interface header
  9. *
  10. * This header contains the ARM AArch32 specific kernel interface. It is
  11. * included by the kernel interface architecture-abstraction header
  12. * (include/arm/cpu.h)
  13. */
  14. #ifndef ZEPHYR_INCLUDE_ARCH_ARM_AARCH32_ARCH_H_
  15. #define ZEPHYR_INCLUDE_ARCH_ARM_AARCH32_ARCH_H_
  16. /* Add include for DTS generated information */
  17. #include <devicetree.h>
  18. /* ARM GPRs are often designated by two different names */
  19. #define sys_define_gpr_with_alias(name1, name2) union { uint32_t name1, name2; }
  20. #include <arch/arm/aarch32/thread.h>
  21. #include <arch/arm/aarch32/exc.h>
  22. #include <arch/arm/aarch32/irq.h>
  23. #include <arch/arm/aarch32/error.h>
  24. #include <arch/arm/aarch32/misc.h>
  25. #include <arch/common/addr_types.h>
  26. #include <arch/common/ffs.h>
  27. #include <arch/arm/aarch32/nmi.h>
  28. #include <arch/arm/aarch32/asm_inline.h>
  29. #include <arch/common/sys_bitops.h>
  30. #ifdef CONFIG_CPU_CORTEX_M
  31. #include <arch/arm/aarch32/cortex_m/cpu.h>
  32. #include <arch/arm/aarch32/cortex_m/memory_map.h>
  33. #include <arch/common/sys_io.h>
  34. #elif defined(CONFIG_CPU_CORTEX_R)
  35. #include <arch/arm/aarch32/cortex_a_r/cpu.h>
  36. #include <arch/arm/aarch32/cortex_a_r/sys_io.h>
  37. #include <arch/arm/aarch32/cortex_a_r/timer.h>
  38. #endif
  39. #ifdef __cplusplus
  40. extern "C" {
  41. #endif
  42. /**
  43. * @brief Declare the ARCH_STACK_PTR_ALIGN
  44. *
  45. * Denotes the required alignment of the stack pointer on public API
  46. * boundaries
  47. *
  48. */
  49. #ifdef CONFIG_STACK_ALIGN_DOUBLE_WORD
  50. #define ARCH_STACK_PTR_ALIGN 8
  51. #else
  52. #define ARCH_STACK_PTR_ALIGN 4
  53. #endif
  54. /**
  55. * @brief Declare the minimum alignment for a thread stack
  56. *
  57. * Denotes the minimum required alignment of a thread stack.
  58. *
  59. * Note:
  60. * User thread stacks must respect the minimum MPU region
  61. * alignment requirement.
  62. */
  63. #if defined(CONFIG_USERSPACE)
  64. #define Z_THREAD_MIN_STACK_ALIGN CONFIG_ARM_MPU_REGION_MIN_ALIGN_AND_SIZE
  65. #else
  66. #define Z_THREAD_MIN_STACK_ALIGN ARCH_STACK_PTR_ALIGN
  67. #endif
  68. /**
  69. * @brief Declare a minimum MPU guard alignment and size
  70. *
  71. * This specifies the minimum MPU guard alignment/size for the MPU. This
  72. * will be used to denote the guard section of the stack, if it exists.
  73. *
  74. * One key note is that this guard results in extra bytes being added to
  75. * the stack. APIs which give the stack ptr and stack size will take this
  76. * guard size into account.
  77. *
  78. * Stack is allocated, but initial stack pointer is at the end
  79. * (highest address). Stack grows down to the actual allocation
  80. * address (lowest address). Stack guard, if present, will comprise
  81. * the lowest MPU_GUARD_ALIGN_AND_SIZE bytes of the stack.
  82. *
  83. * As the stack grows down, it will reach the end of the stack when it
  84. * encounters either the stack guard region, or the stack allocation
  85. * address.
  86. *
  87. * ----------------------- <---- Stack allocation address + stack size +
  88. * | | MPU_GUARD_ALIGN_AND_SIZE
  89. * | Some thread data | <---- Defined when thread is created
  90. * | ... |
  91. * |---------------------| <---- Actual initial stack ptr
  92. * | Initial Stack Ptr | aligned to ARCH_STACK_PTR_ALIGN
  93. * | ... |
  94. * | ... |
  95. * | ... |
  96. * | ... |
  97. * | ... |
  98. * | ... |
  99. * | ... |
  100. * | ... |
  101. * | Stack Ends |
  102. * |---------------------- <---- Stack Buffer Ptr from API
  103. * | MPU Guard, |
  104. * | if present |
  105. * ----------------------- <---- Stack Allocation address
  106. *
  107. */
  108. #if defined(CONFIG_MPU_STACK_GUARD)
  109. #define MPU_GUARD_ALIGN_AND_SIZE CONFIG_ARM_MPU_REGION_MIN_ALIGN_AND_SIZE
  110. #else
  111. #define MPU_GUARD_ALIGN_AND_SIZE 0
  112. #endif
  113. /**
  114. * @brief Declare the MPU guard alignment and size for a thread stack
  115. * that is using the Floating Point services.
  116. *
  117. * For threads that are using the Floating Point services under Shared
  118. * Registers (CONFIG_FPU_SHARING=y) mode, the exception stack frame may
  119. * contain both the basic stack frame and the FP caller-saved context,
  120. * upon exception entry. Therefore, a wide guard region is required to
  121. * guarantee that stack-overflow detection will always be successful.
  122. */
  123. #if defined(CONFIG_FPU) && defined(CONFIG_FPU_SHARING) \
  124. && defined(CONFIG_MPU_STACK_GUARD)
  125. #define MPU_GUARD_ALIGN_AND_SIZE_FLOAT CONFIG_MPU_STACK_GUARD_MIN_SIZE_FLOAT
  126. #else
  127. #define MPU_GUARD_ALIGN_AND_SIZE_FLOAT 0
  128. #endif
  129. /**
  130. * @brief Define alignment of an MPU guard
  131. *
  132. * Minimum alignment of the start address of an MPU guard, depending on
  133. * whether the MPU architecture enforces a size (and power-of-two) alignment
  134. * requirement.
  135. */
  136. #if defined(CONFIG_MPU_REQUIRES_POWER_OF_TWO_ALIGNMENT)
  137. #define Z_MPU_GUARD_ALIGN (MAX(MPU_GUARD_ALIGN_AND_SIZE, \
  138. MPU_GUARD_ALIGN_AND_SIZE_FLOAT))
  139. #else
  140. #define Z_MPU_GUARD_ALIGN MPU_GUARD_ALIGN_AND_SIZE
  141. #endif
  142. #if defined(CONFIG_USERSPACE) && \
  143. defined(CONFIG_MPU_REQUIRES_POWER_OF_TWO_ALIGNMENT)
  144. /* This MPU requires regions to be sized to a power of two, and aligned to
  145. * their own size. Since an MPU region must be able to cover the entire
  146. * user-accessible stack buffer, we size/align to match. The privilege
  147. * mode stack is generated elsewhere in memory.
  148. */
  149. #define ARCH_THREAD_STACK_OBJ_ALIGN(size) Z_POW2_CEIL(size)
  150. #define ARCH_THREAD_STACK_SIZE_ADJUST(size) Z_POW2_CEIL(size)
  151. #else
  152. #define ARCH_THREAD_STACK_OBJ_ALIGN(size) MAX(Z_THREAD_MIN_STACK_ALIGN, \
  153. Z_MPU_GUARD_ALIGN)
  154. #ifdef CONFIG_USERSPACE
  155. #define ARCH_THREAD_STACK_SIZE_ADJUST(size) \
  156. ROUND_UP(size, CONFIG_ARM_MPU_REGION_MIN_ALIGN_AND_SIZE)
  157. #endif
  158. #endif
  159. #ifdef CONFIG_MPU_STACK_GUARD
  160. /* Kernel-only stacks need an MPU guard region programmed at the beginning of
  161. * the stack object, so align the object appropriately.
  162. */
  163. #define ARCH_KERNEL_STACK_RESERVED MPU_GUARD_ALIGN_AND_SIZE
  164. #define ARCH_KERNEL_STACK_OBJ_ALIGN Z_MPU_GUARD_ALIGN
  165. #endif
  166. /* On arm, all MPU guards are carve-outs. */
  167. #define ARCH_THREAD_STACK_RESERVED 0
  168. /* Legacy case: retain containing extern "C" with C++ */
  169. #ifdef CONFIG_ARM_MPU
  170. #ifdef CONFIG_CPU_HAS_ARM_MPU
  171. #include <arch/arm/aarch32/mpu/arm_mpu.h>
  172. #endif /* CONFIG_CPU_HAS_ARM_MPU */
  173. #ifdef CONFIG_CPU_HAS_NXP_MPU
  174. #include <arch/arm/aarch32/mpu/nxp_mpu.h>
  175. #endif /* CONFIG_CPU_HAS_NXP_MPU */
  176. #endif /* CONFIG_ARM_MPU */
  177. #ifdef __cplusplus
  178. }
  179. #endif
  180. #endif /* ZEPHYR_INCLUDE_ARCH_ARM_AARCH32_ARCH_H_ */