mm_private.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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_PRIVATE_H__
  11. #define __MM_PRIVATE_H__
  12. #include "mm_aspace.h"
  13. #include "mm_fault.h"
  14. #include "mm_flag.h"
  15. #include "mm_page.h"
  16. #include <rtdef.h>
  17. #include <stddef.h>
  18. /**
  19. * @brief DATA STRUCTURE & API USED INTERNALLY
  20. *
  21. * This is mainly a wrapper layer to actual data structure.
  22. * In this way, we can switch to any BST we like by adding new
  23. * wrapper code.
  24. * Every BST must satisfy the API to support MM
  25. *
  26. * *INFO: varea range convention
  27. * For API, a range is specified by a base and its length.
  28. * This provides a clear interface without ambiguity.
  29. * For implementation, a range is specified by [start, end] tuple
  30. * where both start and end are inclusive.
  31. */
  32. #define VAREA_NOT_STATIC(varea) (!((varea)->flag & MMF_STATIC_ALLOC))
  33. #define VAREA_NAME(varea) \
  34. ((!varea->mem_obj || !varea->mem_obj->get_name) \
  35. ? "unknow" \
  36. : varea->mem_obj->get_name(varea))
  37. #define VAREA_IS_WRITABLE(varea) \
  38. (rt_hw_mmu_attr_test_perm(varea->attr, \
  39. RT_HW_MMU_PROT_USER | RT_HW_MMU_PROT_WRITE))
  40. #define VAREA_VA_TO_OFFSET(varea, va) \
  41. ((varea)->offset + MM_PA_TO_OFF((long)(va) - (long)(varea)->start))
  42. struct _mm_range
  43. {
  44. void *start;
  45. void *end;
  46. };
  47. /**
  48. * @brief
  49. *
  50. * @param aspace
  51. * @return rt_err_t
  52. */
  53. rt_err_t _aspace_bst_init(struct rt_aspace *aspace);
  54. /**
  55. * @brief Retrieve any varea if start in [varea->start, varea->end]
  56. *
  57. * @param aspace
  58. * @param start
  59. * @return struct rt_varea*
  60. */
  61. struct rt_varea *_aspace_bst_search(struct rt_aspace *aspace, void *start);
  62. /**
  63. * @brief Retrieve lowest varea satisfies (varea->start >= start)
  64. *
  65. * @param aspace
  66. * @param length
  67. * @param struct _mm_range
  68. * @return struct rt_varea*
  69. */
  70. struct rt_varea *_aspace_bst_search_exceed(struct rt_aspace *aspace,
  71. void *start);
  72. /**
  73. * @brief Retrieve any varea overlaps a specified address range
  74. *
  75. * @param aspace
  76. * @param start
  77. * @param length
  78. * @return struct rt_varea*
  79. */
  80. struct rt_varea *_aspace_bst_search_overlap(struct rt_aspace *aspace,
  81. struct _mm_range range);
  82. /**
  83. * @brief Insert a varea into the bst
  84. *
  85. * @param aspace
  86. * @param varea
  87. */
  88. void _aspace_bst_insert(struct rt_aspace *aspace, struct rt_varea *varea);
  89. /**
  90. * @brief Remove a varea from the bst
  91. *
  92. * @param aspace
  93. * @param varea
  94. */
  95. void _aspace_bst_remove(struct rt_aspace *aspace, struct rt_varea *varea);
  96. int rt_varea_fix_private_locked(rt_varea_t ex_varea, void *pa,
  97. struct rt_aspace_fault_msg *msg,
  98. rt_bool_t dont_copy);
  99. int rt_varea_map_with_msg(rt_varea_t varea, struct rt_aspace_fault_msg *msg);
  100. void _varea_uninstall_locked(rt_varea_t varea);
  101. int _mm_aspace_map(rt_aspace_t aspace, rt_varea_t *pvarea, void **addr,
  102. rt_size_t length, rt_size_t attr, mm_flag_t flags,
  103. rt_mem_obj_t mem_obj, rt_size_t offset);
  104. rt_inline rt_bool_t rt_varea_is_private_locked(rt_varea_t varea)
  105. {
  106. rt_base_t flags = varea->flag;
  107. return !!(
  108. (flags & (MMF_MAP_PRIVATE | MMF_MAP_PRIVATE_DONT_SYNC))
  109. && (varea->aspace->private_object != varea->mem_obj)
  110. );
  111. }
  112. rt_err_t rt_aspace_anon_ref_dec(rt_mem_obj_t aobj);
  113. rt_err_t rt_aspace_page_get_phy(rt_aspace_t aspace, void *page_va, void *buffer);
  114. rt_err_t rt_aspace_page_put_phy(rt_aspace_t aspace, void *page_va, void *buffer);
  115. #endif /* __MM_PRIVATE_H__ */