mm_flag.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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-23 WangXiaoyao the first version
  9. */
  10. #ifndef __MM_FLAG_H__
  11. #define __MM_FLAG_H__
  12. #include <rtthread.h>
  13. /**
  14. * @brief mm_flag_t
  15. * |max ------- 7|6 ----- 0|
  16. * | control | align |
  17. *
  18. * there should be no more than 25 flags
  19. */
  20. typedef unsigned long mm_flag_t;
  21. #define _MMF_CNTL_SHIFT 7
  22. #define _MMF_ALIGN_MASK 0x7f
  23. #define _MMF_CNTL_MASK (~((1 << _MMF_CNTL_SHIFT) - 1))
  24. #define _DEF_FLAG(index) (1 << (_MMF_CNTL_SHIFT + (index)))
  25. enum mm_flag_cntl
  26. {
  27. /**
  28. * @brief Modifications to the mapped data shall be visible only to the
  29. * aspace only and shall not change the underlying object. It is
  30. * unspecified whether modifications to the underlying object done after
  31. * the MAP_PRIVATE mapping is established are visible through the
  32. * MAP_PRIVATE mapping.
  33. */
  34. MMF_MAP_PRIVATE = _DEF_FLAG(0),
  35. /**
  36. * @brief Same as MMF_MAP_PRIVATE, except the modification after mapping is
  37. * invisible to the varea
  38. */
  39. MMF_MAP_PRIVATE_DONT_SYNC = _DEF_FLAG(1),
  40. /**
  41. * @brief [POSIX MAP_FIXED] When MAP_FIXED is set in the flags argument, the
  42. * implementation is informed that the value of pa shall be addr, exactly.
  43. * If a MAP_FIXED request is successful, the mapping established
  44. * by mmap() replaces any previous mappings for the pages in the range
  45. * [pa,pa+len) of the process.
  46. */
  47. MMF_MAP_FIXED = _DEF_FLAG(2),
  48. /**
  49. * @brief The backup page frame is allocated and setted only until it is
  50. * truly necessary by the user
  51. */
  52. MMF_PREFETCH = _DEF_FLAG(3),
  53. /**
  54. * @brief Allocate the mapping using "huge" pages
  55. */
  56. MMF_HUGEPAGE = _DEF_FLAG(4),
  57. /** internal reserved flags */
  58. MMF_TEXT = _DEF_FLAG(5),
  59. /** internal reserved flags */
  60. MMF_STATIC_ALLOC = _DEF_FLAG(6),
  61. /**
  62. * @brief Shared mapping. Updates to the mapping are visible to other
  63. * processes mapping the same region, and are carried through to the
  64. * underlying file.
  65. */
  66. MMF_MAP_SHARED = _DEF_FLAG(7),
  67. /**
  68. * @brief a non-locked memory can be swapped out when required, this is
  69. * reserved for future
  70. */
  71. MMF_NONLOCKED = _DEF_FLAG(8),
  72. /**
  73. * @brief An alignment is specified in flags that the mapping must admit
  74. */
  75. MMF_REQUEST_ALIGN = _DEF_FLAG(9),
  76. __MMF_INVALID,
  77. };
  78. #define MMF_GET_ALIGN(src) ((src & _MMF_ALIGN_MASK))
  79. #define MMF_SET_ALIGN(src, align) \
  80. ((src & ~_MMF_ALIGN_MASK) | (__builtin_ffsl(align) - 1))
  81. #define MMF_GET_CNTL(src) (src & _MMF_CNTL_MASK)
  82. #define MMF_TEST_CNTL(src, flag) (src & flag)
  83. #define MMF_SET_CNTL(src, flag) ((src) | (flag))
  84. #define MMF_CLEAR_CNTL(src, flag) ((src) & ~(flag))
  85. /**
  86. * @brief Create Flags
  87. *
  88. * example: MMF_CREATE(0, 0)
  89. * MMF_CREATE(MMF_MAP_FIXED, 0x2000)
  90. *
  91. * Direct use of flag is also acceptable: (MMF_MAP_FIXED | MMF_PREFETCH)
  92. */
  93. #define MMF_CREATE(cntl, align) \
  94. ((align) ? (MMF_SET_CNTL((mm_flag_t)0, (cntl) | MMF_REQUEST_ALIGN) | \
  95. MMF_SET_ALIGN((mm_flag_t)0, (align))) \
  96. : (MMF_SET_CNTL((mm_flag_t)0, (cntl) & ~MMF_REQUEST_ALIGN)))
  97. #undef _DEF_FLAG
  98. #endif /* __MM_FLAG_H__ */