mm_aspace.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-11-14 WangXiaoyao the first version
  9. */
  10. #ifndef __MM_ASPACE_H__
  11. #define __MM_ASPACE_H__
  12. #include <rthw.h>
  13. #include <rtthread.h>
  14. #include <stddef.h>
  15. #include "avl_adpt.h"
  16. #include "mm_fault.h"
  17. #include "mm_flag.h"
  18. #define MM_PAGE_SHIFT 12
  19. #define MM_PA_TO_OFF(pa) ((uintptr_t)(pa) >> MM_PAGE_SHIFT)
  20. #define PV_OFFSET (rt_kmem_pvoff())
  21. #ifndef RT_USING_SMP
  22. typedef rt_spinlock_t mm_spinlock;
  23. #define MM_PGTBL_LOCK_INIT(aspace)
  24. #define MM_PGTBL_LOCK(aspace) (rt_hw_spin_lock(&((aspace)->pgtbl_lock)))
  25. #define MM_PGTBL_UNLOCK(aspace) (rt_hw_spin_unlock(&((aspace)->pgtbl_lock)))
  26. #else
  27. typedef struct rt_spinlock mm_spinlock;
  28. #define MM_PGTBL_LOCK_INIT(aspace) (rt_spin_lock_init(&((aspace)->pgtbl_lock)))
  29. #define MM_PGTBL_LOCK(aspace) (rt_spin_lock(&((aspace)->pgtbl_lock)))
  30. #define MM_PGTBL_UNLOCK(aspace) (rt_spin_unlock(&((aspace)->pgtbl_lock)))
  31. #endif /* RT_USING_SMP */
  32. struct rt_aspace;
  33. struct rt_varea;
  34. struct rt_mem_obj;
  35. extern struct rt_aspace rt_kernel_space;
  36. typedef struct rt_aspace
  37. {
  38. void *start;
  39. rt_size_t size;
  40. void *page_table;
  41. mm_spinlock pgtbl_lock;
  42. struct _aspace_tree tree;
  43. struct rt_mutex bst_lock;
  44. rt_uint64_t asid;
  45. } *rt_aspace_t;
  46. typedef struct rt_varea
  47. {
  48. void *start;
  49. rt_size_t size;
  50. rt_size_t offset;
  51. rt_size_t attr;
  52. rt_size_t flag;
  53. struct rt_aspace *aspace;
  54. struct rt_mem_obj *mem_obj;
  55. struct _aspace_node node;
  56. struct rt_page *frames;
  57. void *data;
  58. } *rt_varea_t;
  59. typedef struct rt_mm_va_hint
  60. {
  61. void *limit_start;
  62. rt_size_t limit_range_size;
  63. void *prefer;
  64. const rt_size_t map_size;
  65. mm_flag_t flags;
  66. } *rt_mm_va_hint_t;
  67. typedef struct rt_mem_obj
  68. {
  69. void (*hint_free)(rt_mm_va_hint_t hint);
  70. void (*on_page_fault)(struct rt_varea *varea, struct rt_aspace_fault_msg *msg);
  71. /* do pre open bushiness like inc a ref */
  72. void (*on_varea_open)(struct rt_varea *varea);
  73. /* do post close bushiness like def a ref */
  74. void (*on_varea_close)(struct rt_varea *varea);
  75. void (*on_page_offload)(struct rt_varea *varea, void *vaddr, rt_size_t size);
  76. const char *(*get_name)(rt_varea_t varea);
  77. } *rt_mem_obj_t;
  78. extern struct rt_mem_obj rt_mm_dummy_mapper;
  79. enum rt_mmu_cntl
  80. {
  81. MMU_CNTL_NONCACHE,
  82. MMU_CNTL_CACHE,
  83. MMU_CNTL_READONLY,
  84. MMU_CNTL_READWRITE,
  85. MMU_CNTL_DUMMY_END,
  86. };
  87. /**
  88. * @brief Lock to access page table of address space
  89. */
  90. #define WR_LOCK(aspace) \
  91. rt_thread_self() ? rt_mutex_take(&(aspace)->bst_lock, RT_WAITING_FOREVER) \
  92. : 0
  93. #define WR_UNLOCK(aspace) \
  94. rt_thread_self() ? rt_mutex_release(&(aspace)->bst_lock) : 0
  95. #define RD_LOCK(aspace) WR_LOCK(aspace)
  96. #define RD_UNLOCK(aspace) WR_UNLOCK(aspace)
  97. rt_aspace_t rt_aspace_create(void *start, rt_size_t length, void *pgtbl);
  98. rt_err_t rt_aspace_init(rt_aspace_t aspace, void *start, rt_size_t length, void *pgtbl);
  99. void rt_aspace_delete(rt_aspace_t aspace);
  100. void rt_aspace_detach(rt_aspace_t aspace);
  101. /**
  102. * @brief Memory Map on Virtual Address Space to Mappable Object
  103. * *INFO There is no restriction to use NULL address(physical/virtual).
  104. * Vaddr passing in addr must be page aligned. If vaddr is RT_NULL,
  105. * a suitable address will be chose automatically.
  106. *
  107. * @param aspace target virtual address space
  108. * @param addr virtual address of the mapping
  109. * @param length length of mapping region
  110. * @param attr MMU attribution
  111. * @param flags desired memory protection and behaviour of the mapping
  112. * @param mem_obj memory map backing store object
  113. * @param offset offset of mapping in 4KB page for mem_obj
  114. * @return int E_OK on success, with addr set to vaddr of mapping
  115. * E_INVAL
  116. */
  117. int rt_aspace_map(rt_aspace_t aspace, void **addr, rt_size_t length, rt_size_t attr,
  118. mm_flag_t flags, rt_mem_obj_t mem_obj, rt_size_t offset);
  119. /** no malloc routines call */
  120. int rt_aspace_map_static(rt_aspace_t aspace, rt_varea_t varea, void **addr,
  121. rt_size_t length, rt_size_t attr, mm_flag_t flags,
  122. rt_mem_obj_t mem_obj, rt_size_t offset);
  123. /**
  124. * @brief Memory Map on Virtual Address Space to Physical Memory
  125. *
  126. * @param aspace target virtual address space
  127. * @param hint hint of mapping va
  128. * @param attr MMU attribution
  129. * @param pa_off (physical address >> 12)
  130. * @param ret_va pointer to the location to store va
  131. * @return int E_OK on success, with ret_va set to vaddr of mapping
  132. * E_INVAL
  133. */
  134. int rt_aspace_map_phy(rt_aspace_t aspace, rt_mm_va_hint_t hint, rt_size_t attr,
  135. rt_size_t pa_off, void **ret_va);
  136. /** no malloc routines call */
  137. int rt_aspace_map_phy_static(rt_aspace_t aspace, rt_varea_t varea,
  138. rt_mm_va_hint_t hint, rt_size_t attr, rt_size_t pa_off,
  139. void **ret_va);
  140. /**
  141. * @brief Remove any mappings overlap the range [addr, addr + bytes)
  142. *
  143. * @param aspace
  144. * @param addr
  145. * @return int
  146. */
  147. int rt_aspace_unmap(rt_aspace_t aspace, void *addr);
  148. int rt_aspace_control(rt_aspace_t aspace, void *addr, enum rt_mmu_cntl cmd);
  149. int rt_aspace_load_page(rt_aspace_t aspace, void *addr, rt_size_t npage);
  150. int rt_aspace_offload_page(rt_aspace_t aspace, void *addr, rt_size_t npage);
  151. int rt_aspace_traversal(rt_aspace_t aspace,
  152. int (*fn)(rt_varea_t varea, void *arg), void *arg);
  153. void rt_aspace_print_all(rt_aspace_t aspace);
  154. /**
  155. * @brief Map one page to varea
  156. *
  157. * @param varea target varea
  158. * @param addr user address
  159. * @param page the page frame to be mapped
  160. * @return int
  161. */
  162. int rt_varea_map_page(rt_varea_t varea, void *vaddr, void *page);
  163. /**
  164. * @brief Map a range of physical address to varea
  165. *
  166. * @param varea target varea
  167. * @param vaddr user address
  168. * @param paddr physical address
  169. * @param length map range
  170. * @return int
  171. */
  172. int rt_varea_map_range(rt_varea_t varea, void *vaddr, void *paddr, rt_size_t length);
  173. /**
  174. * @brief Insert page to page manager of varea
  175. * The page will be freed by varea on uninstall automatically
  176. *
  177. * @param varea target varea
  178. * @param page_addr the page frame to be added
  179. */
  180. void rt_varea_pgmgr_insert(rt_varea_t varea, void *page_addr);
  181. rt_ubase_t rt_kmem_pvoff(void);
  182. void rt_kmem_pvoff_set(rt_ubase_t pvoff);
  183. int rt_kmem_map_phy(void *va, void *pa, rt_size_t length, rt_size_t attr);
  184. void *rt_kmem_v2p(void *vaddr);
  185. void rt_kmem_list(void);
  186. #endif /* __MM_ASPACE_H__ */