mm_flag.h 2.9 KB

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