mm_flag.h 2.9 KB
Newer Older
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
/*
 * 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__

/**
 * @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 Indicate a possible COW mapping
     */
    MMF_MAP_PRIVATE = _DEF_FLAG(0),
    MMF_COW = _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),

50 51 52
    /**
     * @brief Allocate the mapping using "huge" pages
     */
53 54
    MMF_HUGEPAGE = _DEF_FLAG(4),

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

58
    /** internal reserved flags */
S
Shell 已提交
59 60
    MMF_STATIC_ALLOC = _DEF_FLAG(6),

61 62 63 64 65 66 67
    /**
     * @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),

68 69 70 71
    /**
     * @brief a non-locked memory can be swapped out when required, this is
     * reserved for future
     */
72
    MMF_NONLOCKED = _DEF_FLAG(8),
73 74 75 76

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

    __MMF_INVALID,
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
};

#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)
95
 *          MMF_CREATE(MMF_MAP_FIXED, 0x2000)
96 97 98
 *
 * Direct use of flag is also acceptable: (MMF_MAP_FIXED | MMF_PREFETCH)
 */
99 100 101
#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)))                             \
102 103 104 105
           : (MMF_SET_CNTL((mm_flag_t)0, (cntl) & ~MMF_REQUEST_ALIGN)))

#undef _DEF_FLAG
#endif /* __MM_FLAG_H__ */