riscv_mmu.h 5.5 KB

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