privilege.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* Copyright 2019 SiFive, Inc */
  2. /* SPDX-License-Identifier: Apache-2.0 */
  3. #ifndef METAL__PRIVILEGE_H
  4. #define METAL__PRIVILEGE_H
  5. /*!
  6. * @file metal/privilege.h
  7. *
  8. * @brief API for manipulating the privilege mode of a RISC-V system
  9. *
  10. * Additional information about privilege modes on RISC-V systems can be found
  11. * by reading the RISC-V Privileged Architecture Specification v1.10.
  12. */
  13. #include <stdint.h>
  14. enum metal_privilege_mode {
  15. METAL_PRIVILEGE_USER = 0,
  16. METAL_PRIVILEGE_SUPERVISOR = 1,
  17. METAL_PRIVILEGE_MACHINE = 3,
  18. };
  19. #if __riscv_xlen == 32
  20. typedef uint32_t metal_xreg_t;
  21. #elif __riscv_xlen == 64
  22. typedef uint64_t metal_xreg_t;
  23. #endif
  24. #if __riscv_flen == 32
  25. typedef uint32_t metal_freg_t;
  26. #elif __riscv_flen == 64
  27. typedef uint64_t metal_freg_t;
  28. #endif
  29. struct metal_register_file {
  30. metal_xreg_t ra;
  31. metal_xreg_t sp;
  32. metal_xreg_t gp;
  33. metal_xreg_t tp;
  34. metal_xreg_t t0;
  35. metal_xreg_t t1;
  36. metal_xreg_t t2;
  37. metal_xreg_t s0;
  38. metal_xreg_t s1;
  39. metal_xreg_t a0;
  40. metal_xreg_t a1;
  41. metal_xreg_t a2;
  42. metal_xreg_t a3;
  43. metal_xreg_t a4;
  44. metal_xreg_t a5;
  45. #ifndef __riscv_32e
  46. metal_xreg_t a6;
  47. metal_xreg_t a7;
  48. metal_xreg_t s2;
  49. metal_xreg_t s3;
  50. metal_xreg_t s4;
  51. metal_xreg_t s5;
  52. metal_xreg_t s6;
  53. metal_xreg_t s7;
  54. metal_xreg_t s8;
  55. metal_xreg_t s9;
  56. metal_xreg_t s10;
  57. metal_xreg_t s11;
  58. metal_xreg_t t3;
  59. metal_xreg_t t4;
  60. metal_xreg_t t5;
  61. metal_xreg_t t6;
  62. #endif /* __riscv_32e */
  63. #ifdef __riscv_flen
  64. metal_freg_t ft0;
  65. metal_freg_t ft1;
  66. metal_freg_t ft2;
  67. metal_freg_t ft3;
  68. metal_freg_t ft4;
  69. metal_freg_t ft5;
  70. metal_freg_t ft6;
  71. metal_freg_t ft7;
  72. metal_freg_t fs0;
  73. metal_freg_t fs1;
  74. metal_freg_t fa0;
  75. metal_freg_t fa1;
  76. metal_freg_t fa2;
  77. metal_freg_t fa3;
  78. metal_freg_t fa4;
  79. metal_freg_t fa5;
  80. metal_freg_t fa6;
  81. metal_freg_t fa7;
  82. metal_freg_t fs2;
  83. metal_freg_t fs3;
  84. metal_freg_t fs4;
  85. metal_freg_t fs5;
  86. metal_freg_t fs6;
  87. metal_freg_t fs7;
  88. metal_freg_t fs8;
  89. metal_freg_t fs9;
  90. metal_freg_t fs10;
  91. metal_freg_t fs11;
  92. metal_freg_t ft8;
  93. metal_freg_t ft9;
  94. metal_freg_t ft10;
  95. metal_freg_t ft11;
  96. #endif /* __riscv_flen */
  97. };
  98. typedef void (*metal_privilege_entry_point_t)(void);
  99. void metal_privilege_drop_to_mode(enum metal_privilege_mode mode,
  100. struct metal_register_file regfile,
  101. metal_privilege_entry_point_t entry_point);
  102. #endif