riscv_mmu.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. * 2021-05-03 lizhirui porting to c906
  10. * 2023-10-12 Shell Add permission control API
  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. /* C-SKY extend */
  19. #define PTE_SEC (1UL << 59) /* Security */
  20. #define PTE_SHARE (1UL << 60) /* Shareable */
  21. #define PTE_BUF (1UL << 61) /* Bufferable */
  22. #define PTE_CACHE (1UL << 62) /* Cacheable */
  23. #define PTE_SO (1UL << 63) /* Strong Order */
  24. #define PAGE_OFFSET_SHIFT 0
  25. #define PAGE_OFFSET_BIT 12
  26. #define PAGE_SIZE __SIZE(PAGE_OFFSET_BIT)
  27. #define PAGE_OFFSET_MASK __MASK(PAGE_OFFSET_BIT)
  28. #define VPN0_SHIFT (PAGE_OFFSET_SHIFT + PAGE_OFFSET_BIT)
  29. #define VPN0_BIT 9
  30. #define VPN1_SHIFT (VPN0_SHIFT + VPN0_BIT)
  31. #define VPN1_BIT 9
  32. #define VPN2_SHIFT (VPN1_SHIFT + VPN1_BIT)
  33. #define VPN2_BIT 9
  34. #define PPN0_SHIFT (PAGE_OFFSET_SHIFT + PAGE_OFFSET_BIT)
  35. #define PPN0_BIT 9
  36. #define PPN1_SHIFT (PPN0_SHIFT + PPN0_BIT)
  37. #define PPN1_BIT 9
  38. #define PPN2_SHIFT (PPN1_SHIFT + PPN1_BIT)
  39. #define PPN2_BIT 26
  40. #define L1_PAGE_SIZE __SIZE(PAGE_OFFSET_BIT + VPN0_BIT + VPN1_BIT)
  41. #define L2_PAGE_SIZE __SIZE(PAGE_OFFSET_BIT + VPN0_BIT)
  42. #define L3_PAGE_SIZE __SIZE(PAGE_OFFSET_BIT)
  43. #define ARCH_ADDRESS_WIDTH_BITS 64
  44. #define PHYSICAL_ADDRESS_WIDTH_BITS 56
  45. #define PAGE_ATTR_NEXT_LEVEL (0)
  46. #define PAGE_ATTR_RWX (PTE_X | PTE_W | PTE_R)
  47. #define PAGE_ATTR_READONLY (PTE_R)
  48. #define PAGE_ATTR_XN (PTE_W | PTE_R)
  49. #define PAGE_ATTR_READEXECUTE (PTE_X | PTE_R)
  50. #define PAGE_ATTR_USER (PTE_U)
  51. #define PAGE_ATTR_SYSTEM (0)
  52. #define PAGE_ATTR_CB (PTE_BUF | PTE_CACHE)
  53. #define PAGE_ATTR_DEV (PTE_SO)
  54. #define PAGE_DEFAULT_ATTR_LEAF (PTE_SHARE | PTE_BUF | PTE_CACHE | PTE_A | PTE_D | PTE_U | PAGE_ATTR_RWX | PTE_V)
  55. #define PAGE_DEFAULT_ATTR_NEXT (PTE_SHARE | PTE_BUF | PTE_CACHE | PTE_A | PTE_D | PTE_V)
  56. #define PAGE_IS_LEAF(pte) __MASKVALUE(pte, PAGE_ATTR_RWX)
  57. #define PTE_USED(pte) __MASKVALUE(pte, PTE_V)
  58. #define PTE_WRAP(attr) (attr | PTE_A | PTE_D)
  59. /**
  60. * encoding of SATP (Supervisor Address Translation and Protection register)
  61. */
  62. #define SATP_MODE_OFFSET 60
  63. #define SATP_MODE_BARE 0
  64. #define SATP_MODE_SV39 8
  65. #define SATP_MODE_SV48 9
  66. #define SATP_MODE_SV57 10
  67. #define SATP_MODE_SV64 11
  68. #define PPN_BITS 44
  69. #define ARCH_VADDR_WIDTH 39
  70. #define SATP_MODE SATP_MODE_SV39
  71. #define MMU_MAP_K_DEVICE PTE_WRAP(PAGE_ATTR_DEV | PTE_G | PAGE_ATTR_XN | PTE_V)
  72. #define MMU_MAP_K_RWCB PTE_WRAP(PAGE_ATTR_CB | PTE_G | PAGE_ATTR_RWX | PTE_V)
  73. #define MMU_MAP_K_RW PTE_WRAP(PTE_G | PAGE_ATTR_RWX | PTE_V)
  74. #define MMU_MAP_U_RWCB PTE_WRAP(PAGE_ATTR_CB | PTE_U | PAGE_ATTR_RWX | PTE_V)
  75. #define MMU_MAP_U_ROCB PTE_WRAP(PAGE_ATTR_CB | PTE_U | PAGE_ATTR_READONLY | PTE_V)
  76. #define MMU_MAP_U_RWCB_XN PTE_WRAP(PAGE_ATTR_CB | PTE_U | PAGE_ATTR_XN | PTE_V)
  77. #define MMU_MAP_U_RW PTE_WRAP(PTE_U | PAGE_ATTR_RWX | PTE_V)
  78. #define MMU_MAP_TRACE(attr) (attr)
  79. #define PTE_XWR_MASK 0xe
  80. #define ARCH_PAGE_SIZE PAGE_SIZE
  81. #define ARCH_PAGE_MASK (ARCH_PAGE_SIZE - 1)
  82. #define ARCH_PAGE_SHIFT PAGE_OFFSET_BIT
  83. #define ARCH_INDEX_WIDTH 9
  84. #define ARCH_INDEX_SIZE (1ul << ARCH_INDEX_WIDTH)
  85. #define ARCH_INDEX_MASK (ARCH_INDEX_SIZE - 1)
  86. #define ARCH_MAP_FAILED ((void *)0x8000000000000000)
  87. void mmu_set_pagetable(rt_ubase_t addr);
  88. void mmu_enable_user_page_access(void);
  89. void mmu_disable_user_page_access(void);
  90. #define RT_HW_MMU_PROT_READ 1
  91. #define RT_HW_MMU_PROT_WRITE 2
  92. #define RT_HW_MMU_PROT_EXECUTE 4
  93. #define RT_HW_MMU_PROT_KERNEL 8
  94. #define RT_HW_MMU_PROT_USER 16
  95. #define RT_HW_MMU_PROT_CACHE 32
  96. /**
  97. * @brief Remove permission from attribution
  98. *
  99. * @param attr architecture specified mmu attribution
  100. * @param prot protect that will be removed
  101. * @return size_t returned attribution
  102. */
  103. rt_inline size_t rt_hw_mmu_attr_rm_perm(size_t attr, rt_base_t prot)
  104. {
  105. switch (prot)
  106. {
  107. /* remove write permission for user */
  108. case RT_HW_MMU_PROT_WRITE | RT_HW_MMU_PROT_USER:
  109. default:
  110. RT_ASSERT(0);
  111. }
  112. return attr;
  113. }
  114. /**
  115. * @brief Add permission from attribution
  116. *
  117. * @param attr architecture specified mmu attribution
  118. * @param prot protect that will be added
  119. * @return size_t returned attribution
  120. */
  121. rt_inline size_t rt_hw_mmu_attr_add_perm(size_t attr, rt_base_t prot)
  122. {
  123. switch (prot)
  124. {
  125. /* add write permission for user */
  126. case RT_HW_MMU_PROT_WRITE | RT_HW_MMU_PROT_USER:
  127. default:
  128. RT_ASSERT(0);
  129. }
  130. return attr;
  131. }
  132. /**
  133. * @brief Test permission from attribution
  134. *
  135. * @param attr architecture specified mmu attribution
  136. * @param prot protect that will be test
  137. * @return rt_bool_t RT_TRUE if the prot is allowed, otherwise RT_FALSE
  138. */
  139. rt_inline rt_bool_t rt_hw_mmu_attr_test_perm(size_t attr, rt_base_t prot)
  140. {
  141. rt_bool_t rc = 0;
  142. switch (prot)
  143. {
  144. /* test write permission for user */
  145. case RT_HW_MMU_PROT_WRITE | RT_HW_MMU_PROT_USER:
  146. default:
  147. RT_ASSERT(0);
  148. }
  149. return rc;
  150. }
  151. #endif