context.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Copyright (c) 2016-2020, Arm Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #ifndef CONTEXT_H
  7. #define CONTEXT_H
  8. #include <lib/utils_def.h>
  9. /*******************************************************************************
  10. * Constants that allow assembler code to access members of and the 'regs'
  11. * structure at their correct offsets.
  12. ******************************************************************************/
  13. #define CTX_REGS_OFFSET U(0x0)
  14. #define CTX_GPREG_R0 U(0x0)
  15. #define CTX_GPREG_R1 U(0x4)
  16. #define CTX_GPREG_R2 U(0x8)
  17. #define CTX_GPREG_R3 U(0xC)
  18. #define CTX_LR U(0x10)
  19. #define CTX_SCR U(0x14)
  20. #define CTX_SPSR U(0x18)
  21. #define CTX_NS_SCTLR U(0x1C)
  22. #define CTX_REGS_END U(0x20)
  23. #ifndef __ASSEMBLER__
  24. #include <stdint.h>
  25. #include <lib/cassert.h>
  26. /*
  27. * Common constants to help define the 'cpu_context' structure and its
  28. * members below.
  29. */
  30. #define WORD_SHIFT U(2)
  31. #define DEFINE_REG_STRUCT(name, num_regs) \
  32. typedef struct name { \
  33. uint32_t ctx_regs[num_regs]; \
  34. } __aligned(8) name##_t
  35. /* Constants to determine the size of individual context structures */
  36. #define CTX_REG_ALL (CTX_REGS_END >> WORD_SHIFT)
  37. DEFINE_REG_STRUCT(regs, CTX_REG_ALL);
  38. #undef CTX_REG_ALL
  39. #define read_ctx_reg(ctx, offset) ((ctx)->ctx_regs[offset >> WORD_SHIFT])
  40. #define write_ctx_reg(ctx, offset, val) (((ctx)->ctx_regs[offset >> WORD_SHIFT]) \
  41. = val)
  42. typedef struct cpu_context {
  43. regs_t regs_ctx;
  44. } cpu_context_t;
  45. /* Macros to access members of the 'cpu_context_t' structure */
  46. #define get_regs_ctx(h) (&((cpu_context_t *) h)->regs_ctx)
  47. /*
  48. * Compile time assertions related to the 'cpu_context' structure to
  49. * ensure that the assembler and the compiler view of the offsets of
  50. * the structure members is the same.
  51. */
  52. CASSERT(CTX_REGS_OFFSET == __builtin_offsetof(cpu_context_t, regs_ctx),
  53. assert_core_context_regs_offset_mismatch);
  54. #endif /* __ASSEMBLER__ */
  55. #endif /* CONTEXT_H */