riscv_mmu.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (c) 2006-2021, 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 L1_PAGE_SIZE __SIZE(PAGE_OFFSET_BIT + VPN0_BIT + VPN1_BIT)
  34. #define L2_PAGE_SIZE __SIZE(PAGE_OFFSET_BIT + VPN0_BIT)
  35. #define L3_PAGE_SIZE __SIZE(PAGE_OFFSET_BIT)
  36. #define ARCH_ADDRESS_WIDTH_BITS 64
  37. #define PHYSICAL_ADDRESS_WIDTH_BITS 56
  38. #define PAGE_ATTR_NEXT_LEVEL (0)
  39. #define PAGE_ATTR_RWX (PTE_X | PTE_W | PTE_R)
  40. #define PAGE_ATTR_READONLY (PTE_R)
  41. #define PAGE_ATTR_READEXECUTE (PTE_X | PTE_R)
  42. #define PAGE_ATTR_USER (PTE_U)
  43. #define PAGE_ATTR_SYSTEM (0)
  44. #define PAGE_DEFAULT_ATTR_LEAF (PAGE_ATTR_RWX | PAGE_ATTR_USER | PTE_V | PTE_G)
  45. #define PAGE_DEFAULT_ATTR_NEXT (PAGE_ATTR_NEXT_LEVEL | PTE_V | PTE_G)
  46. #define PAGE_IS_LEAF(pte) __MASKVALUE(pte, PAGE_ATTR_RWX)
  47. #define PTE_USED(pte) __MASKVALUE(pte, PTE_V)
  48. /**
  49. * encoding of SATP (Supervisor Address Translation and Protection register)
  50. */
  51. #define SATP_MODE_OFFSET 60
  52. #define SATP_MODE_BARE 0
  53. #define SATP_MODE_SV39 8
  54. #define SATP_MODE_SV48 9
  55. #define SATP_MODE_SV57 10
  56. #define SATP_MODE_SV64 11
  57. #define ARCH_VADDR_WIDTH 39
  58. #define SATP_MODE SATP_MODE_SV39
  59. #define MMU_MAP_K_DEVICE (PTE_G | PTE_W | PTE_R | PTE_V)
  60. #define MMU_MAP_K_RWCB (PTE_G | PTE_X | PTE_W | PTE_R | PTE_V)
  61. #define MMU_MAP_K_RW (PTE_G | PTE_X | PTE_W | PTE_R | PTE_V)
  62. #define MMU_MAP_U_RWCB (PTE_U | PTE_X | PTE_W | PTE_R | PTE_V)
  63. #define MMU_MAP_U_RWCB_XN (PTE_U | PTE_W | PTE_R | PTE_V)
  64. #define MMU_MAP_U_RW (PTE_U | PTE_X | PTE_W | PTE_R | PTE_V)
  65. #define PTE_XWR_MASK 0xe
  66. #define ARCH_PAGE_SIZE PAGE_SIZE
  67. #define ARCH_PAGE_MASK (ARCH_PAGE_SIZE - 1)
  68. #define ARCH_PAGE_SHIFT PAGE_OFFSET_BIT
  69. #define ARCH_INDEX_WIDTH 9
  70. #define ARCH_INDEX_SIZE (1ul << ARCH_INDEX_WIDTH)
  71. #define ARCH_INDEX_MASK (ARCH_INDEX_SIZE - 1)
  72. #define ARCH_MAP_FAILED ((void *)0x8000000000000000)
  73. void mmu_set_pagetable(rt_ubase_t addr);
  74. void mmu_enable_user_page_access(void);
  75. void mmu_disable_user_page_access(void);
  76. #define RT_HW_MMU_PROT_READ 1
  77. #define RT_HW_MMU_PROT_WRITE 2
  78. #define RT_HW_MMU_PROT_EXECUTE 4
  79. #define RT_HW_MMU_PROT_KERNEL 8
  80. #define RT_HW_MMU_PROT_USER 16
  81. #define RT_HW_MMU_PROT_CACHE 32
  82. /**
  83. * @brief Remove permission from attribution
  84. *
  85. * @param attr architecture specified mmu attribution
  86. * @param prot protect that will be removed
  87. * @return size_t returned attribution
  88. */
  89. rt_inline size_t rt_hw_mmu_attr_rm_perm(size_t attr, rt_base_t prot)
  90. {
  91. switch (prot)
  92. {
  93. /* remove write permission for user */
  94. case RT_HW_MMU_PROT_WRITE | RT_HW_MMU_PROT_USER:
  95. attr &= ~PTE_W;
  96. break;
  97. default:
  98. RT_ASSERT(0);
  99. }
  100. return attr;
  101. }
  102. /**
  103. * @brief Add permission from attribution
  104. *
  105. * @param attr architecture specified mmu attribution
  106. * @param prot protect that will be added
  107. * @return size_t returned attribution
  108. */
  109. rt_inline size_t rt_hw_mmu_attr_add_perm(size_t attr, rt_base_t prot)
  110. {
  111. switch (prot)
  112. {
  113. /* add write permission for user */
  114. case RT_HW_MMU_PROT_WRITE | RT_HW_MMU_PROT_USER:
  115. attr |= PTE_W;
  116. break;
  117. default:
  118. RT_ASSERT(0);
  119. }
  120. return attr;
  121. }
  122. /**
  123. * @brief Test permission from attribution
  124. *
  125. * @param attr architecture specified mmu attribution
  126. * @param prot protect that will be test
  127. * @return rt_bool_t RT_TRUE if the prot is allowed, otherwise RT_FALSE
  128. */
  129. rt_inline rt_bool_t rt_hw_mmu_attr_test_perm(size_t attr, rt_base_t prot)
  130. {
  131. rt_bool_t rc = 0;
  132. switch (prot)
  133. {
  134. /* test write permission for user */
  135. case RT_HW_MMU_PROT_WRITE | RT_HW_MMU_PROT_USER:
  136. rc = !!(attr & PTE_W);
  137. break;
  138. default:
  139. RT_ASSERT(0);
  140. }
  141. return rc;
  142. }
  143. #endif