1 /*
2  * Copyright (c) 2013, Google, Inc. All rights reserved
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files
6  * (the "Software"), to deal in the Software without restriction,
7  * including without limitation the rights to use, copy, modify, merge,
8  * publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 
24 #ifndef __LIB_TRUSTY_UIO_H
25 #define __LIB_TRUSTY_UIO_H
26 
27 #include <kernel/usercopy.h>
28 #include <sys/types.h>
29 
30 struct iovec_kern {
31     void* iov_base;
32     size_t iov_len;
33 };
34 
35 struct iovec_user {
36     user_addr_t iov_base;
37     user_size_t iov_len;
38 };
39 
40 struct iovec_iter {
41     uint iov_index;
42     uint iov_cnt;
43     size_t data_offset;
44 };
45 
iovec_iter_create(uint iov_cnt)46 static inline struct iovec_iter iovec_iter_create(uint iov_cnt) {
47     struct iovec_iter iter = {.iov_cnt = iov_cnt};
48     return iter;
49 }
50 
iovec_iter_has_next(const struct iovec_iter * iter)51 static inline int iovec_iter_has_next(const struct iovec_iter* iter) {
52     return iter->iov_index < iter->iov_cnt;
53 }
54 
55 ssize_t membuf_to_kern_iovec(const struct iovec_kern* iov,
56                              uint iov_cnt,
57                              const uint8_t* buf,
58                              size_t len);
59 
60 ssize_t kern_iovec_to_membuf(uint8_t* buf,
61                              size_t len,
62                              const struct iovec_kern* iov,
63                              uint iov_cnt);
64 
65 ssize_t membuf_to_user_iovec(user_addr_t iov_uaddr,
66                              uint iov_cnt,
67                              const uint8_t* buf,
68                              size_t cb);
69 
70 ssize_t user_iovec_to_membuf(uint8_t* buf,
71                              size_t len,
72                              user_addr_t iov_uaddr,
73                              uint iov_cnt);
74 
75 ssize_t user_iovec_to_membuf_iter(uint8_t* buf,
76                                   size_t len,
77                                   user_addr_t iov_uaddr,
78                                   struct iovec_iter* iter);
79 #endif
80