summaryrefslogtreecommitdiff
path: root/components/mm/mm_flag.h
blob: 6d58027b362b6137afb0d76163402a2f9b36fd48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
 * Copyright (c) 2006-2022, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-11-23     WangXiaoyao  the first version
 */
#ifndef __MM_FLAG_H__
#define __MM_FLAG_H__

#include <rtthread.h>

/**
 * @brief mm_flag_t
 * |max ------- 7|6 ----- 0|
 * |   control   |  align  |
 *
 * there should be no more than 25 flags
 */
typedef unsigned long mm_flag_t;

#define _MMF_CNTL_SHIFT  7
#define _MMF_ALIGN_MASK  0x7f
#define _MMF_CNTL_MASK   (~((1 << _MMF_CNTL_SHIFT) - 1))
#define _DEF_FLAG(index) (1 << (_MMF_CNTL_SHIFT + (index)))

enum mm_flag_cntl
{
    /**
     * @brief Modifications to the mapped data shall be visible only to the
     * aspace only and shall not change the underlying object. It is
     * unspecified whether modifications to the underlying object done after
     * the MAP_PRIVATE mapping is established are visible through the
     * MAP_PRIVATE mapping.
     */
    MMF_MAP_PRIVATE = _DEF_FLAG(0),

    /**
     * @brief Same as MMF_MAP_PRIVATE, except the modification after mapping is
     * invisible to the varea
     */
    MMF_MAP_PRIVATE_DONT_SYNC = _DEF_FLAG(1),

    /**
     * @brief [POSIX MAP_FIXED] When MAP_FIXED is set in the flags argument, the
     * implementation is informed that the value of pa shall be addr, exactly.
     * If a MAP_FIXED request is successful, the mapping established
     * by mmap() replaces any previous mappings for the pages in the range
     * [pa,pa+len) of the process.
     */
    MMF_MAP_FIXED = _DEF_FLAG(2),

    /**
     * @brief The backup page frame is allocated and setted only until it is
     * truly necessary by the user
     */
    MMF_PREFETCH = _DEF_FLAG(3),

    /**
     * @brief Allocate the mapping using "huge" pages
     */
    MMF_HUGEPAGE = _DEF_FLAG(4),

    /** internal reserved flags */
    MMF_TEXT = _DEF_FLAG(5),

    /** internal reserved flags */
    MMF_STATIC_ALLOC = _DEF_FLAG(6),

    /**
     * @brief Shared mapping. Updates to the mapping are visible to other
     * processes mapping the same region, and are carried through to the
     * underlying file.
     */
    MMF_MAP_SHARED = _DEF_FLAG(7),

    /**
     * @brief a non-locked memory can be swapped out when required, this is
     * reserved for future
     */
    MMF_NONLOCKED = _DEF_FLAG(8),

    /**
     * @brief An alignment is specified in flags that the mapping must admit
     */
    MMF_REQUEST_ALIGN = _DEF_FLAG(9),

    __MMF_INVALID,
};

#define MMF_GET_ALIGN(src) ((src & _MMF_ALIGN_MASK))
#define MMF_SET_ALIGN(src, align)                                              \
    ((src & ~_MMF_ALIGN_MASK) | (__builtin_ffsl(align) - 1))

#define MMF_GET_CNTL(src)         (src & _MMF_CNTL_MASK)
#define MMF_TEST_CNTL(src, flag)  (src & flag)
#define MMF_SET_CNTL(src, flag)   ((src) | (flag))
#define MMF_CLEAR_CNTL(src, flag) ((src) & ~(flag))

/**
 * @brief Create Flags
 *
 * example: MMF_CREATE(0, 0)
 *          MMF_CREATE(MMF_MAP_FIXED, 0x2000)
 *
 * Direct use of flag is also acceptable: (MMF_MAP_FIXED | MMF_PREFETCH)
 */
#define MMF_CREATE(cntl, align)                                                 \
    ((align) ? (MMF_SET_CNTL((mm_flag_t)0, (cntl) | MMF_REQUEST_ALIGN) |        \
              MMF_SET_ALIGN((mm_flag_t)0, (align)))                             \
           : (MMF_SET_CNTL((mm_flag_t)0, (cntl) & ~MMF_REQUEST_ALIGN)))

#undef _DEF_FLAG
#endif /* __MM_FLAG_H__ */