更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
user_copy.c
浏览该文件的文档.
1/*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include "user_copy.h"
33#include "arm_user_copy.h"
34#include "arm_user_clear.h"
35#include "securec.h"
36#include "los_memory.h"
37#include "los_vm_map.h"
38
39
40
41/**
42 * @brief
43 * @verbatim
44 从用户空间拷贝到内核空间
45 请思考个问题,系统调用时为什么一定要copy_from_user ? 为什么不直接用memcpy?
46 https://mp.weixin.qq.com/s/H3nXlOpP_XyF7M-1B4qreQ
47 * @endverbatim
48 * @param dst
49 * @param src
50 * @param len
51 * @return size_t
52 */
53size_t arch_copy_from_user(void *dst, const void *src, size_t len)
54{
55 return LOS_ArchCopyFromUser(dst, src, len);
56}
57
58size_t LOS_ArchCopyFromUser(void *dst, const void *src, size_t len)
59{
60 if (!LOS_IsUserAddressRange((VADDR_T)(UINTPTR)src, len)) {//[src,src+len]在内核空间
61 return len;
62 }
63
64 return _arm_user_copy(dst, src, len);//完成从用户空间到内核空间的拷贝
65}
66///拷贝到用户空间
67size_t arch_copy_to_user(void *dst, const void *src, size_t len)
68{
69 return LOS_ArchCopyToUser(dst, src, len);//
70}
71
72/**
73 * @brief 从内核空间拷贝到用户空间
74 * @param dst 必须在用户空间
75 * @param src 必须在内核空间
76 * @param len
77 * @return size_t
78 */
79size_t LOS_ArchCopyToUser(void *dst, const void *src, size_t len)
80{//先判断地址是不是在用户空间
81 if (!LOS_IsUserAddressRange((VADDR_T)(UINTPTR)dst, len)) {//[dest,dest+count] 不在用户空间
82 return len;//必须在用户空间
83 }
84
85 return _arm_user_copy(dst, src, len);//完成从内核空间到用户空间的拷贝
86}
87///将内核数据拷贝到用户空间
88INT32 LOS_CopyFromKernel(VOID *dest, UINT32 max, const VOID *src, UINT32 count)
89{
90 INT32 ret;
91
92 if (!LOS_IsUserAddressRange((VADDR_T)(UINTPTR)dest, count)) {//[dest,dest+count] 不在用户空间
93 ret = memcpy_s(dest, max, src, count);
94 } else {//[dest,dest+count] 在用户空间
95 ret = ((max >= count) ? _arm_user_copy(dest, src, count) : ERANGE_AND_RESET);//用户空间copy
96 }
97
98 return ret;
99}
100///将用户空间的数据拷贝到内核空间
101INT32 LOS_CopyToKernel(VOID *dest, UINT32 max, const VOID *src, UINT32 count)
102{
103 INT32 ret;
104
105 if (!LOS_IsUserAddressRange((vaddr_t)(UINTPTR)src, count)) {//[src,src+count] 在内核空间的情况
106 ret = memcpy_s(dest, max, src, count);
107 } else {//[src,src+count] 在内核空间的情况
108 ret = ((max >= count) ? _arm_user_copy(dest, src, count) : ERANGE_AND_RESET);
109 }
110
111 return ret;
112}
113///清除用户空间数据
114INT32 LOS_UserMemClear(unsigned char *buf, UINT32 len)
115{
116 INT32 ret = 0;
117 if (!LOS_IsUserAddressRange((vaddr_t)(UINTPTR)buf, len)) {//[buf,buf+len] 不在用户空间
118 (VOID)memset_s(buf, len, 0, len);//清0
119 } else {//在用户空间
120 if (_arm_clear_user(buf, len)) {
121 return -EFAULT;
122 }
123 }
124 return ret;
125}
126
127
size_t _arm_clear_user(void *addr, size_t bytes)
汇编代码实现 见于..\third_party\FreeBSD\lib\libc\arm\string\hw_user_copy.S 用户空间 <—> 内核空间的拷贝实现函数,...
size_t _arm_user_copy(void *dst, const void *src, size_t len)
signed int INT32
Definition: los_typedef.h:60
unsigned long vaddr_t
Definition: los_typedef.h:206
unsigned long VADDR_T
Definition: los_typedef.h:208
unsigned long UINTPTR
Definition: los_typedef.h:68
unsigned int UINT32
Definition: los_typedef.h:57
STATIC INLINE BOOL LOS_IsUserAddressRange(VADDR_T vaddr, size_t len)
虚拟地址[vaddr,vaddr + len]是否在用户空间
Definition: los_vm_map.h:281
size_t LOS_ArchCopyToUser(void *dst, const void *src, size_t len)
从内核空间拷贝到用户空间
Definition: user_copy.c:79
INT32 LOS_UserMemClear(unsigned char *buf, UINT32 len)
清除用户空间数据
Definition: user_copy.c:114
size_t arch_copy_from_user(void *dst, const void *src, size_t len)
Definition: user_copy.c:53
size_t LOS_ArchCopyFromUser(void *dst, const void *src, size_t len)
Definition: user_copy.c:58
INT32 LOS_CopyToKernel(VOID *dest, UINT32 max, const VOID *src, UINT32 count)
将用户空间的数据拷贝到内核空间
Definition: user_copy.c:101
INT32 LOS_CopyFromKernel(VOID *dest, UINT32 max, const VOID *src, UINT32 count)
将内核数据拷贝到用户空间
Definition: user_copy.c:88
size_t arch_copy_to_user(void *dst, const void *src, size_t len)
拷贝到用户空间
Definition: user_copy.c:67