更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
los_vm_common.h
浏览该文件的文档.
1/*!
2 * @file los_vm_common.h
3 * @brief
4 * @link
5 @verbatim
6 @note_pic
7 鸿蒙虚拟内存-用户空间图 从 USER_ASPACE_BASE 至 USER_ASPACE_TOP_MAX
8 鸿蒙源码分析系列篇: https://blog.csdn.net/kuangyufei
9 https://my.oschina.net/u/3751245
10
11 | /\ |
12 | || |
13 |---------------------------|内核空间结束位置KERNEL_ASPACE_BASE + KERNEL_ASPACE_SIZE
14 | |
15 | 内核空间 |
16 | |
17 | |
18 |---------------------------|内核空间开始位置 KERNEL_ASPACE_BASE
19 | |
20 | 16M 预留 |
21 |---------------------------|用户空间栈顶 USER_ASPACE_TOP_MAX = USER_ASPACE_BASE + USER_ASPACE_SIZE
22 | |
23 | stack区 自上而下 |
24 | |
25 | || |
26 | || |
27 | || |
28 | \/ |
29 | |
30 |---------------------------|映射区结束位置 USER_MAP_BASE + USER_MAP_SIZE
31 | 映射区 (文件,匿名,I/O映射) |
32 | |
33 | |
34 | 共享库 .so |
35 | |
36 | L1/L2页表 |
37 |---------------------------|映射区开始位置 USER_MAP_BASE = (USER_ASPACE_TOP_MAX >> 1)
38 | |
39 | |
40 | /\ |
41 | || |
42 | || |
43 | || |
44 | |
45 | heap区 自下而上 |
46 | |
47 |---------------------------|用户空间堆区开始位置 USER_HEAP_BASE = USER_ASPACE_TOP_MAX >> 2
48 | |
49 | .bss |
50 | .data |
51 | .text |
52 |---------------------------|用户空间开始位置 USER_ASPACE_BASE = 0x01000000UL
53 | |
54 | 16M预留 |
55 |---------------------------|虚拟内存开始位置 0x00000000
56
57 @endverbatim
58 * @version
59 * @author weharmonyos.com | 鸿蒙研究站 | 每天死磕一点点
60 * @date 2021-11-30
61 */
62/*
63 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
64 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
65 *
66 * Redistribution and use in source and binary forms, with or without modification,
67 * are permitted provided that the following conditions are met:
68 *
69 * 1. Redistributions of source code must retain the above copyright notice, this list of
70 * conditions and the following disclaimer.
71 *
72 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
73 * of conditions and the following disclaimer in the documentation and/or other materials
74 * provided with the distribution.
75 *
76 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
77 * to endorse or promote products derived from this software without specific prior written
78 * permission.
79 *
80 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
81 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
82 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
83 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
84 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
85 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
86 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
87 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
88 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
89 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
90 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
91 */
92
93/**
94 * @defgroup los_vm_map vm common definition
95 * @ingroup kernel
96 */
97
98#ifndef __LOS_VM_COMMON_H__
99#define __LOS_VM_COMMON_H__
100
101#ifdef __cplusplus
102#if __cplusplus
103extern "C" {
104#endif /* __cplusplus */
105#endif /* __cplusplus */
106
107
108/* user address space, defaults to below kernel space with a 16MB guard gap on either side */
109#ifndef USER_ASPACE_BASE ///< 用户地址空间,默认为低于内核空间,两侧各有16MB的保护间隙
110#define USER_ASPACE_BASE ((vaddr_t)0x01000000UL) ///< 用户空间基地址 从16M位置开始 user_ram : ORIGIN = 0x1000000, LENGTH = 0x100000
111#endif
112#ifndef USER_ASPACE_SIZE
113#define USER_ASPACE_SIZE ((vaddr_t)KERNEL_ASPACE_BASE - USER_ASPACE_BASE - 0x01000000UL)///< 用户空间 < 内核空间 2个16M
114#endif
115
116#define USER_ASPACE_TOP_MAX ((vaddr_t)(USER_ASPACE_BASE + USER_ASPACE_SIZE))///< 用户空间顶部位置
117#define USER_HEAP_BASE ((vaddr_t)(USER_ASPACE_TOP_MAX >> 2)) ///< 堆的开始地址
118#define USER_MAP_BASE ((vaddr_t)(USER_ASPACE_TOP_MAX >> 1)) ///< 用户映射区开始地址
119#define USER_MAP_SIZE ((vaddr_t)(USER_ASPACE_SIZE >> 3)) ///< 用户空间映射大小 = 1/8 用户空间
120
121#ifndef PAGE_SIZE
122#define PAGE_SIZE (0x1000U) ///< 页大小4K
123#endif
124#define PAGE_MASK (~(PAGE_SIZE - 1)) ///< 页掩码,用于页内偏移地址的计算
125#define PAGE_SHIFT (12)///< 12位 - 4K 偏移
126
127#define KB (1024UL)
128#define MB (1024UL * 1024UL)
129#define GB (1024UL * 1024UL * 1024UL)
130
131/**
132 * @brief
133 * @verbatim
134 圆整通常被理解为为满足某种要求而进行的数据修正。按照修正后的数据在数值上是否比原数据大,
135 又可分为向上圆整和向下圆整。它们很像对模拟信号进行采样,对一定范围的数据向一个固定的数据靠拢。
136 举例理解:
137 ROUNDUP(7,4) = 8 ,ROUNDUP(8,4) = 8 ,ROUNDUP(9,4) = 12
138 ROUNDDOWN(7,4) = 4 ,ROUNDDOWN(8,4) = 8 ,ROUNDDOWN(9,4) = 8
139 ROUNDOFFSET(7,4) = 3 ,ROUNDOFFSET(8,4) = 0 ,ROUNDOFFSET(9,4) = 1
140
141 * @endverbatim
142 */
143#define ROUNDUP(a, b) (((a) + ((b) - 1)) & ~((b) - 1)) ///< 向上圆整
144#define ROUNDDOWN(a, b) ((a) & ~((b) - 1)) ///< 向下圆整
145#define ROUNDOFFSET(a, b) ((a) & ((b) - 1)) ///< 圆整偏移
146#define MIN2(a, b) (((a) < (b)) ? (a) : (b)) ///< 找到最小值
147
148#define IS_ALIGNED(a, b) (!(((UINTPTR)(a)) & (((UINTPTR)(b)) - 1)))///< 是否按指定的参数对齐
149#define IS_PAGE_ALIGNED(x) IS_ALIGNED(x, PAGE_SIZE) ///< 是否按页大小对齐 4K
150#define IS_SECTION_ALIGNED(x) IS_ALIGNED(x, SECTION_SIZE)///< 是否按段大小对齐
151//虚拟内存的异常提示
152#define LOS_ERRNO_VM_NO_ERROR (0)
153#define LOS_ERRNO_VM_GENERIC (-1)
154#define LOS_ERRNO_VM_NOT_FOUND (-2)
155#define LOS_ERRNO_VM_NOT_READY (-3)
156#define LOS_ERRNO_VM_NO_MSG (-4)
157#define LOS_ERRNO_VM_NO_MEMORY (-5)
158#define LOS_ERRNO_VM_ALREADY_STARTED (-6)
159#define LOS_ERRNO_VM_NOT_VALID (-7)
160#define LOS_ERRNO_VM_INVALID_ARGS (-8)
161#define LOS_ERRNO_VM_NOT_ENOUGH_BUFFER (-9)
162#define LOS_ERRNO_VM_NOT_SUSPENDED (-10)
163#define LOS_ERRNO_VM_OBJECT_DESTROYED (-11)
164#define LOS_ERRNO_VM_NOT_BLOCKED (-12)
165#define LOS_ERRNO_VM_TIMED_OUT (-13)
166#define LOS_ERRNO_VM_ALREADY_EXISTS (-14)
167#define LOS_ERRNO_VM_CHANNEL_CLOSED (-15)
168#define LOS_ERRNO_VM_OFFLINE (-16)
169#define LOS_ERRNO_VM_NOT_ALLOWED (-17)
170#define LOS_ERRNO_VM_BAD_PATH (-18)
171#define LOS_ERRNO_VM_ALREADY_MOUNTED (-19)
172#define LOS_ERRNO_VM_IO (-20)
173#define LOS_ERRNO_VM_NOT_DIR (-21)
174#define LOS_ERRNO_VM_NOT_FILE (-22)
175#define LOS_ERRNO_VM_RECURSE_TOO_DEEP (-23)
176#define LOS_ERRNO_VM_NOT_SUPPORTED (-24)
177#define LOS_ERRNO_VM_TOO_BIG (-25)
178#define LOS_ERRNO_VM_CANCELLED (-26)
179#define LOS_ERRNO_VM_NOT_IMPLEMENTED (-27)
180#define LOS_ERRNO_VM_CHECKSUM_FAIL (-28)
181#define LOS_ERRNO_VM_CRC_FAIL (-29)
182#define LOS_ERRNO_VM_CMD_UNKNOWN (-30)
183#define LOS_ERRNO_VM_BAD_STATE (-31)
184#define LOS_ERRNO_VM_BAD_LEN (-32)
185#define LOS_ERRNO_VM_BUSY (-33)
186#define LOS_ERRNO_VM_THREAD_DETACHED (-34)
187#define LOS_ERRNO_VM_I2C_NACK (-35)
188#define LOS_ERRNO_VM_ALREADY_EXPIRED (-36)
189#define LOS_ERRNO_VM_OUT_OF_RANGE (-37)
190#define LOS_ERRNO_VM_NOT_CONFIGURED (-38)
191#define LOS_ERRNO_VM_NOT_MOUNTED (-39)
192#define LOS_ERRNO_VM_FAULT (-40)
193#define LOS_ERRNO_VM_NO_RESOURCES (-41)
194#define LOS_ERRNO_VM_BAD_HANDLE (-42)
195#define LOS_ERRNO_VM_ACCESS_DENIED (-43)
196#define LOS_ERRNO_VM_PARTIAL_WRITE (-44)
197#define LOS_ERRNO_VM_LOCK (-45)
198#define LOS_ERRNO_VM_MAP_FAILED (-46)
199
200#define VM_ERR(args...) do { \
201 PRINT_ERR("%s %d ", __FUNCTION__, __LINE__); \
202 if (PRINT_LEVEL == LOS_DEBUG_LEVEL) { \
203 PRINTK(args); \
204 } \
205 PRINTK("\n"); \
206} while (0)
207
208#ifdef __cplusplus
209#if __cplusplus
210}
211#endif /* __cplusplus */
212#endif /* __cplusplus */
213
214#endif /* __LOS_VM_COMMON_H__ */