1 /* 2 * file related system call shims and definitions 3 * 4 * Copyright (c) 2013 Stacey D. Son 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 #ifndef BSD_FILE_H 21 #define BSD_FILE_H 22 23 #include "qemu/path.h" 24 25 #define LOCK_PATH(p, arg) \ 26 do { \ 27 (p) = lock_user_string(arg); \ 28 if ((p) == NULL) { \ 29 return -TARGET_EFAULT; \ 30 } \ 31 } while (0) 32 33 #define UNLOCK_PATH(p, arg) unlock_user(p, arg, 0) 34 35 36 extern struct iovec *lock_iovec(int type, abi_ulong target_addr, int count, 37 int copy); 38 extern void unlock_iovec(struct iovec *vec, abi_ulong target_addr, int count, 39 int copy); 40 41 int safe_open(const char *path, int flags, mode_t mode); 42 int safe_openat(int fd, const char *path, int flags, mode_t mode); 43 44 ssize_t safe_read(int fd, void *buf, size_t nbytes); 45 ssize_t safe_pread(int fd, void *buf, size_t nbytes, off_t offset); 46 ssize_t safe_readv(int fd, const struct iovec *iov, int iovcnt); 47 ssize_t safe_preadv(int fd, const struct iovec *iov, int iovcnt, off_t offset); 48 49 ssize_t safe_write(int fd, void *buf, size_t nbytes); 50 ssize_t safe_pwrite(int fd, void *buf, size_t nbytes, off_t offset); 51 ssize_t safe_writev(int fd, const struct iovec *iov, int iovcnt); 52 ssize_t safe_pwritev(int fd, const struct iovec *iov, int iovcnt, off_t offset); 53 54 /* read(2) */ 55 static abi_long do_bsd_read(abi_long arg1, abi_long arg2, abi_long arg3) 56 { 57 abi_long ret; 58 void *p; 59 60 p = lock_user(VERIFY_WRITE, arg2, arg3, 0); 61 if (p == NULL) { 62 return -TARGET_EFAULT; 63 } 64 ret = get_errno(safe_read(arg1, p, arg3)); 65 unlock_user(p, arg2, ret); 66 67 return ret; 68 } 69 70 /* pread(2) */ 71 static abi_long do_bsd_pread(void *cpu_env, abi_long arg1, 72 abi_long arg2, abi_long arg3, abi_long arg4, abi_long arg5, abi_long arg6) 73 { 74 abi_long ret; 75 void *p; 76 77 p = lock_user(VERIFY_WRITE, arg2, arg3, 0); 78 if (p == NULL) { 79 return -TARGET_EFAULT; 80 } 81 if (regpairs_aligned(cpu_env) != 0) { 82 arg4 = arg5; 83 arg5 = arg6; 84 } 85 ret = get_errno(safe_pread(arg1, p, arg3, target_arg64(arg4, arg5))); 86 unlock_user(p, arg2, ret); 87 88 return ret; 89 } 90 91 /* readv(2) */ 92 static abi_long do_bsd_readv(abi_long arg1, abi_long arg2, abi_long arg3) 93 { 94 abi_long ret; 95 struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0); 96 97 if (vec != NULL) { 98 ret = get_errno(safe_readv(arg1, vec, arg3)); 99 unlock_iovec(vec, arg2, arg3, 1); 100 } else { 101 ret = -host_to_target_errno(errno); 102 } 103 104 return ret; 105 } 106 107 /* preadv(2) */ 108 static abi_long do_bsd_preadv(void *cpu_env, abi_long arg1, 109 abi_long arg2, abi_long arg3, abi_long arg4, abi_long arg5, abi_long arg6) 110 { 111 abi_long ret; 112 struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 1); 113 114 if (vec != NULL) { 115 if (regpairs_aligned(cpu_env) != 0) { 116 arg4 = arg5; 117 arg5 = arg6; 118 } 119 ret = get_errno(safe_preadv(arg1, vec, arg3, target_arg64(arg4, arg5))); 120 unlock_iovec(vec, arg2, arg3, 0); 121 } else { 122 ret = -host_to_target_errno(errno); 123 } 124 125 return ret; 126 } 127 128 /* write(2) */ 129 static abi_long do_bsd_write(abi_long arg1, abi_long arg2, abi_long arg3) 130 { 131 abi_long nbytes, ret; 132 void *p; 133 134 /* nbytes < 0 implies that it was larger than SIZE_MAX. */ 135 nbytes = arg3; 136 if (nbytes < 0) { 137 return -TARGET_EINVAL; 138 } 139 p = lock_user(VERIFY_READ, arg2, nbytes, 1); 140 if (p == NULL) { 141 return -TARGET_EFAULT; 142 } 143 ret = get_errno(safe_write(arg1, p, arg3)); 144 unlock_user(p, arg2, 0); 145 146 return ret; 147 } 148 149 /* pwrite(2) */ 150 static abi_long do_bsd_pwrite(void *cpu_env, abi_long arg1, 151 abi_long arg2, abi_long arg3, abi_long arg4, abi_long arg5, abi_long arg6) 152 { 153 abi_long ret; 154 void *p; 155 156 p = lock_user(VERIFY_READ, arg2, arg3, 1); 157 if (p == NULL) { 158 return -TARGET_EFAULT; 159 } 160 if (regpairs_aligned(cpu_env) != 0) { 161 arg4 = arg5; 162 arg5 = arg6; 163 } 164 ret = get_errno(safe_pwrite(arg1, p, arg3, target_arg64(arg4, arg5))); 165 unlock_user(p, arg2, 0); 166 167 return ret; 168 } 169 170 /* writev(2) */ 171 static abi_long do_bsd_writev(abi_long arg1, abi_long arg2, abi_long arg3) 172 { 173 abi_long ret; 174 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1); 175 176 if (vec != NULL) { 177 ret = get_errno(safe_writev(arg1, vec, arg3)); 178 unlock_iovec(vec, arg2, arg3, 0); 179 } else { 180 ret = -host_to_target_errno(errno); 181 } 182 183 return ret; 184 } 185 186 /* pwritev(2) */ 187 static abi_long do_bsd_pwritev(void *cpu_env, abi_long arg1, 188 abi_long arg2, abi_long arg3, abi_long arg4, abi_long arg5, abi_long arg6) 189 { 190 abi_long ret; 191 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1); 192 193 if (vec != NULL) { 194 if (regpairs_aligned(cpu_env) != 0) { 195 arg4 = arg5; 196 arg5 = arg6; 197 } 198 ret = get_errno(safe_pwritev(arg1, vec, arg3, target_arg64(arg4, arg5))); 199 unlock_iovec(vec, arg2, arg3, 0); 200 } else { 201 ret = -host_to_target_errno(errno); 202 } 203 204 return ret; 205 } 206 207 /* open(2) */ 208 static abi_long do_bsd_open(abi_long arg1, abi_long arg2, abi_long arg3) 209 { 210 abi_long ret; 211 void *p; 212 213 LOCK_PATH(p, arg1); 214 ret = get_errno(safe_open(path(p), target_to_host_bitmask(arg2, 215 fcntl_flags_tbl), arg3)); 216 UNLOCK_PATH(p, arg1); 217 218 return ret; 219 } 220 221 /* openat(2) */ 222 static abi_long do_bsd_openat(abi_long arg1, abi_long arg2, 223 abi_long arg3, abi_long arg4) 224 { 225 abi_long ret; 226 void *p; 227 228 LOCK_PATH(p, arg2); 229 ret = get_errno(safe_openat(arg1, path(p), 230 target_to_host_bitmask(arg3, fcntl_flags_tbl), arg4)); 231 UNLOCK_PATH(p, arg2); 232 233 return ret; 234 } 235 236 /* close(2) */ 237 static inline abi_long do_bsd_close(abi_long arg1) 238 { 239 return get_errno(close(arg1)); 240 } 241 242 #endif /* BSD_FILE_H */ 243