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