1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <sys/mman.h>
18
19 #include <lk/err_ptr.h>
20 #include <stddef.h>
21 #include <stdint.h>
22 #include <trusty/sys/mman.h>
23 #include <trusty_syscalls.h>
24 #ifdef HWASAN_ENABLED
25 #include <lib/hwasan/hwasan_shadow.h>
26 #endif /* HWASAN_ENABLED */
27
28 #include <trusty_log.h>
29 #define TLOG_TAG "mman"
30
mmap(void * uaddr,size_t size,int prot,int flags,int handle,off_t offset)31 void* mmap(void* uaddr,
32 size_t size,
33 int prot,
34 int flags,
35 int handle,
36 off_t offset) {
37 void* result;
38
39 if (offset != 0) {
40 return MAP_FAILED;
41 }
42
43 /*
44 * or the flags together for now since the syscall doesn't have enough
45 * arguments and now that we have real mappable handles, we have to dispatch
46 * on the flags to switch between regions and handles
47 */
48 result = (void*)_trusty_mmap(uaddr, size, (uint32_t)prot | flags,
49 (int32_t)handle);
50 if (IS_ERR(result)) {
51 return MAP_FAILED;
52 }
53 #ifdef HWASAN_ENABLED
54 /*
55 * Assume _trusty_mmap() call above gives us a valid region of memory that
56 * is mapped into user address space. For such regions hwasan_tag_memory()
57 * can not fail.
58 */
59 result = hwasan_tag_memory(result, size);
60 #endif
61 return result;
62 }
63
munmap(void * uaddr,size_t size)64 int munmap(void* uaddr, size_t size) {
65 #ifdef HWASAN_ENABLED
66 /* HWASan memory will be unmapped. No need to worry about untagging it. */
67 uaddr = hwasan_remove_ptr_tag(uaddr);
68 #endif
69 return _trusty_munmap(uaddr, size);
70 }
71
prepare_dma(void * uaddr,uint32_t size,uint32_t flags,struct dma_pmem * pmem)72 int prepare_dma(void* uaddr,
73 uint32_t size,
74 uint32_t flags,
75 struct dma_pmem* pmem) {
76 #ifdef HWASAN_ENABLED
77 uaddr = hwasan_remove_ptr_tag(uaddr);
78 #endif
79 return _trusty_prepare_dma(uaddr, size, flags, pmem);
80 }
81
finish_dma(void * uaddr,uint32_t size,uint32_t flags)82 int finish_dma(void* uaddr, uint32_t size, uint32_t flags) {
83 #ifdef HWASAN_ENABLED
84 uaddr = hwasan_remove_ptr_tag(uaddr);
85 #endif
86 return _trusty_finish_dma(uaddr, size, flags);
87 }
88
prepare_input_output_dma(void * input,uint32_t input_len,void * output,uint32_t output_len,struct dma_pmem * input_pmem,struct dma_pmem * output_pmem)89 int prepare_input_output_dma(void* input,
90 uint32_t input_len,
91 void* output,
92 uint32_t output_len,
93 struct dma_pmem* input_pmem,
94 struct dma_pmem* output_pmem) {
95 if (input == output && input_len != output_len) {
96 return ERR_INVALID_ARGS;
97 }
98
99 int rc = NO_ERROR;
100 if (input == output && input_len == output_len) {
101 rc = prepare_dma(input, input_len, DMA_FLAG_BIDIRECTION, input_pmem);
102 if (rc < 0) {
103 TLOGE("Couldn't prepare input/output dma - rc(%d)\n", rc);
104 return rc;
105 }
106 *output_pmem = *input_pmem;
107 } else {
108 rc = prepare_dma(input, input_len, DMA_FLAG_TO_DEVICE, input_pmem);
109 if (rc < 0) {
110 TLOGE("Couldn't prepare input dma - rc(%d)\n", rc);
111 return rc;
112 }
113 rc = prepare_dma(output, output_len, DMA_FLAG_FROM_DEVICE, output_pmem);
114 if (rc < 0) {
115 TLOGE("Couldn't prepare output dma - rc(%d)\n", rc);
116 finish_dma(input, input_len, DMA_FLAG_TO_DEVICE); // Clean DMA
117 return rc;
118 }
119 }
120 return NO_ERROR;
121 }
122
finish_input_output_dma(void * input,uint32_t input_len,void * output,uint32_t output_len)123 int finish_input_output_dma(void* input,
124 uint32_t input_len,
125 void* output,
126 uint32_t output_len) {
127 if (input == output && input_len != output_len) {
128 return ERR_INVALID_ARGS;
129 }
130
131 if (input == output && input_len == output_len) {
132 return finish_dma(input, input_len, DMA_FLAG_BIDIRECTION);
133 } else {
134 int rc_in = finish_dma(input, input_len, DMA_FLAG_TO_DEVICE);
135 int rc_out = finish_dma(output, output_len, DMA_FLAG_FROM_DEVICE);
136
137 /* Return an error if it exists */
138 return rc_in == NO_ERROR ? rc_out : rc_in;
139 }
140 }
141