1 /*
2 * Copyright (C) 2022 Huawei Technologies Co., Ltd.
3 * Licensed under the Mulan PSL v2.
4 * You can use this software according to the terms and conditions of the Mulan PSL v2.
5 * You may obtain a copy of Mulan PSL v2 at:
6 * http://license.coscl.org.cn/MulanPSL2
7 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
8 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
9 * PURPOSE.
10 * See the Mulan PSL v2 for more details.
11 */
12 #include <sys/mman.h>
13 #include <malloc.h>
14 #include <securec.h>
15 #include <mem_ops.h>
16 #include <tee_log.h>
17 #include <ipclib.h>
18 #include <mem_page_ops.h>
19 #include <drv.h>
20 #include <tee_sharemem_ops.h>
21
copy_task_param_check(uint64_t src,uint32_t src_size,uint64_t dst,uint32_t dst_size)22 static int32_t copy_task_param_check(uint64_t src, uint32_t src_size, uint64_t dst, uint32_t dst_size)
23 {
24 if (src == 0 || dst == 0 || src_size == 0 || dst_size == 0 || src_size > dst_size) {
25 tloge("invalid param src size:0x%x dst size:0x%x\n", src_size, dst_size);
26 return -1;
27 }
28
29 if (src + src_size < src) {
30 tloge("invalid src buffer size:0x%x\n", src_size);
31 return -1;
32 }
33
34 if (dst + dst_size < dst) {
35 tloge("invalid dst buffer size:0x%x\n", dst_size);
36 return -1;
37 }
38
39 return 0;
40 }
41
copy_from_sharemem(uint32_t src_task,uint64_t src,uint32_t src_size,uintptr_t dst,uint32_t dst_size)42 int32_t copy_from_sharemem(uint32_t src_task, uint64_t src, uint32_t src_size, uintptr_t dst, uint32_t dst_size)
43 {
44 int32_t ret;
45 uint64_t temp_dst;
46
47 ret = copy_task_param_check(src, src_size, dst, dst_size);
48 if (ret != 0)
49 return -1;
50
51 ret = map_sharemem(src_task, src, src_size, &temp_dst);
52 if (ret != 0) {
53 tloge("map sharemem failed, src_task:0x%x\n", src_task);
54 return -1;
55 }
56
57 ret = memcpy_s((void *)dst, dst_size, (void *)(uintptr_t)temp_dst, src_size);
58 if (ret != EOK) {
59 tloge("copy buffer from sharemem failed\n");
60 if (unmap_sharemem((void *)(uintptr_t)temp_dst, src_size) != 0)
61 tloge("unmap temp dst failed in from sharemem\n");
62 return -1;
63 }
64
65 if (unmap_sharemem((void *)(uintptr_t)temp_dst, src_size) != 0) {
66 tloge("something wrong, unmap temp dst failed in from sharemem\n");
67 return -1;
68 }
69
70 return 0;
71 }
72
copy_to_sharemem(uintptr_t src,uint32_t src_size,uint32_t dst_task,uint64_t dst,uint32_t dst_size)73 int32_t copy_to_sharemem(uintptr_t src, uint32_t src_size, uint32_t dst_task, uint64_t dst, uint32_t dst_size)
74 {
75 int32_t ret;
76 uint64_t temp_dst;
77
78 ret = copy_task_param_check(src, src_size, dst, dst_size);
79 if (ret != 0)
80 return -1;
81
82 ret = map_sharemem(dst_task, dst, dst_size, &temp_dst);
83 if (ret != 0) {
84 tloge("map sharemem failed, dst_task:0x%x\n", dst_task);
85 return -1;
86 }
87
88 ret = memcpy_s((void *)(uintptr_t)temp_dst, dst_size, (void *)src, src_size);
89 if (ret != EOK) {
90 tloge("copy buffer to sharemem failed\n");
91 if (unmap_sharemem((void *)(uintptr_t)temp_dst, dst_size) != 0)
92 tloge("unmap temp dst failed in to sharemem\n");
93 return -1;
94 }
95
96 if (unmap_sharemem((void *)(uintptr_t)temp_dst, dst_size) != 0) {
97 tloge("something wrong, unmap temp dst failed in to sharemem\n");
98 return -1;
99 }
100
101 return 0;
102 }
103
tee_alloc_coherent_sharemem_aux(const struct tee_uuid * uuid,uint32_t size)104 void *tee_alloc_coherent_sharemem_aux(const struct tee_uuid *uuid, uint32_t size)
105 {
106 (void)uuid;
107 (void)size;
108 return NULL;
109 }
110
tee_alloc_sharemem_aux(const struct tee_uuid * uuid,uint32_t size)111 void *tee_alloc_sharemem_aux(const struct tee_uuid *uuid, uint32_t size)
112 {
113 return alloc_sharemem_aux(uuid, size);
114 }
115
tee_free_sharemem(void * addr,uint32_t size)116 uint32_t tee_free_sharemem(void *addr, uint32_t size)
117 {
118 return free_sharemem(addr, size);
119 }