riscv_mmu.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright (c) 2006-2024, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-01-30 lizhirui first version
  9. * 2023-10-12 Shell Add permission control API
  10. */
  11. #ifndef __RISCV_MMU_H__
  12. #define __RISCV_MMU_H__
  13. #include <rtthread.h>
  14. #include <rthw.h>
  15. #include "riscv.h"
  16. #undef PAGE_SIZE
  17. #define PAGE_OFFSET_SHIFT 0
  18. #define PAGE_OFFSET_BIT 12
  19. #define PAGE_SIZE __SIZE(PAGE_OFFSET_BIT)
  20. #define PAGE_OFFSET_MASK __MASK(PAGE_OFFSET_BIT)
  21. #define VPN0_SHIFT (PAGE_OFFSET_SHIFT + PAGE_OFFSET_BIT)
  22. #define VPN0_BIT 9
  23. #define VPN1_SHIFT (VPN0_SHIFT + VPN0_BIT)
  24. #define VPN1_BIT 9
  25. #define VPN2_SHIFT (VPN1_SHIFT + VPN1_BIT)
  26. #define VPN2_BIT 9
  27. #define PPN0_SHIFT (PAGE_OFFSET_SHIFT + PAGE_OFFSET_BIT)
  28. #define PPN0_BIT 9
  29. #define PPN1_SHIFT (PPN0_SHIFT + PPN0_BIT)
  30. #define PPN1_BIT 9
  31. #define PPN2_SHIFT (PPN1_SHIFT + PPN1_BIT)
  32. #define PPN2_BIT 26
  33. #define PPN_BITS (PPN0_BIT + PPN1_BIT + PPN2_BIT)
  34. #define L1_PAGE_SIZE __SIZE(PAGE_OFFSET_BIT + VPN0_BIT + VPN1_BIT)
  35. #define L2_PAGE_SIZE __SIZE(PAGE_OFFSET_BIT + VPN0_BIT)
  36. #define L3_PAGE_SIZE __SIZE(PAGE_OFFSET_BIT)
  37. #define ARCH_ADDRESS_WIDTH_BITS 64
  38. #define PHYSICAL_ADDRESS_WIDTH_BITS 56
  39. #define PAGE_ATTR_NEXT_LEVEL (0)
  40. #define PAGE_ATTR_RWX (PTE_X | PTE_W | PTE_R)
  41. #define PAGE_ATTR_READONLY (PTE_R)
  42. #define PAGE_ATTR_READEXECUTE (PTE_X | PTE_R)
  43. #define PAGE_ATTR_USER (PTE_U)
  44. #define PAGE_ATTR_SYSTEM (0)
  45. #define PAGE_DEFAULT_ATTR_LEAF (PAGE_ATTR_RWX | PAGE_ATTR_USER | PTE_V | PTE_G)
  46. #define PAGE_DEFAULT_ATTR_NEXT (PAGE_ATTR_NEXT_LEVEL | PTE_V | PTE_G)
  47. #define PAGE_IS_LEAF(pte) __MASKVALUE(pte, PAGE_ATTR_RWX)
  48. #define PTE_USED(pte) __MASKVALUE(pte, PTE_V)
  49. /**
  50. * encoding of SATP (Supervisor Address Translation and Protection register)
  51. */
  52. #define SATP_MODE_OFFSET 60
  53. #define SATP_MODE_BARE 0
  54. #define SATP_MODE_SV39 8
  55. #define SATP_MODE_SV48 9
  56. #define SATP_MODE_SV57 10
  57. #define SATP_MODE_SV64 11
  58. #define ARCH_VADDR_WIDTH 39
  59. #define SATP_MODE SATP_MODE_SV39
  60. #define MMU_MAP_K_DEVICE (PTE_G | PTE_W | PTE_R | PTE_V)
  61. #define MMU_MAP_K_RWCB (PTE_G | PTE_X | PTE_W | PTE_R | PTE_V)
  62. #define MMU_MAP_K_RW (PTE_G | PTE_X | PTE_W | PTE_R | PTE_V)
  63. #define MMU_MAP_U_RWCB (PTE_U | PTE_X | PTE_W | PTE_R | PTE_V)
  64. #define MMU_MAP_U_RWCB_XN (PTE_U | PTE_W | PTE_R | PTE_V)
  65. #define MMU_MAP_U_RW (PTE_U | PTE_X | PTE_W | PTE_R | PTE_V)
  66. #define MMU_MAP_EARLY (PAGE_ATTR_RWX | PTE_G | PTE_V)
  67. #define PTE_XWR_MASK 0xe
  68. #define ARCH_PAGE_SIZE PAGE_SIZE
  69. #define ARCH_PAGE_MASK (ARCH_PAGE_SIZE - 1)
  70. #define ARCH_PAGE_SHIFT PAGE_OFFSET_BIT
  71. #define ARCH_INDEX_WIDTH 9
  72. #define ARCH_INDEX_SIZE (1ul << ARCH_INDEX_WIDTH)
  73. #define ARCH_INDEX_MASK (ARCH_INDEX_SIZE - 1)
  74. #define ARCH_MAP_FAILED ((void *)-1)
  75. void mmu_set_pagetable(rt_ubase_t addr);
  76. void mmu_enable_user_page_access(void);
  77. void mmu_disable_user_page_access(void);
  78. #define RT_HW_MMU_PROT_READ 1
  79. #define RT_HW_MMU_PROT_WRITE 2
  80. #define RT_HW_MMU_PROT_EXECUTE 4
  81. #define RT_HW_MMU_PROT_KERNEL 8
  82. #define RT_HW_MMU_PROT_USER 16
  83. #define RT_HW_MMU_PROT_CACHE 32
  84. void rt_hw_asid_init(void);
  85. struct rt_aspace;
  86. void rt_hw_asid_switch_pgtbl(struct rt_aspace *aspace, rt_ubase_t pgtbl);
  87. /**
  88. * @brief Remove permission from attribution
  89. *
  90. * @param attr architecture specified mmu attribution
  91. * @param prot protect that will be removed
  92. * @return size_t returned attribution
  93. */
  94. rt_inline size_t rt_hw_mmu_attr_rm_perm(size_t attr, rt_base_t prot)
  95. {
  96. switch (prot)
  97. {
  98. /* remove write permission for user */
  99. case RT_HW_MMU_PROT_WRITE | RT_HW_MMU_PROT_USER:
  100. attr &= ~PTE_W;
  101. break;
  102. /* remove write permission for kernel */
  103. case RT_HW_MMU_PROT_WRITE | RT_HW_MMU_PROT_KERNEL:
  104. attr &= ~PTE_W;
  105. break;
  106. default:
  107. RT_ASSERT(0);
  108. }
  109. return attr;
  110. }
  111. /**
  112. * @brief Add permission from attribution
  113. *
  114. * @param attr architecture specified mmu attribution
  115. * @param prot protect that will be added
  116. * @return size_t returned attribution
  117. */
  118. rt_inline size_t rt_hw_mmu_attr_add_perm(size_t attr, rt_base_t prot)
  119. {
  120. switch (prot)
  121. {
  122. /* add write permission for user */
  123. case RT_HW_MMU_PROT_WRITE | RT_HW_MMU_PROT_USER:
  124. attr |= (PTE_R | PTE_W | PTE_U);
  125. break;
  126. default:
  127. RT_ASSERT(0);
  128. }
  129. return attr;
  130. }
  131. /**
  132. * @brief Test permission from attribution
  133. *
  134. * @param attr architecture specified mmu attribution
  135. * @param prot protect that will be test
  136. * @return rt_bool_t RT_TRUE if the prot is allowed, otherwise RT_FALSE
  137. */
  138. rt_inline rt_bool_t rt_hw_mmu_attr_test_perm(size_t attr, rt_base_t prot)
  139. {
  140. rt_bool_t rc = 0;
  141. switch (prot & ~RT_HW_MMU_PROT_USER)
  142. {
  143. /* test write permission for user */
  144. case RT_HW_MMU_PROT_WRITE:
  145. rc = ((attr & PTE_W) && (attr & PTE_R));
  146. break;
  147. case RT_HW_MMU_PROT_READ:
  148. rc = !!(attr & PTE_R);
  149. break;
  150. case RT_HW_MMU_PROT_EXECUTE:
  151. rc = !!(attr & PTE_X);
  152. break;
  153. default:
  154. RT_ASSERT(0);
  155. }
  156. if (rc && (prot & RT_HW_MMU_PROT_USER))
  157. {
  158. rc = !!(attr & PTE_U);
  159. }
  160. return rc;
  161. }
  162. #endif