]>
git.proxmox.com Git - mirror_qemu.git/blob - bsd-user/bsd-file.h
2 * file related system call shims and definitions
4 * Copyright (c) 2013 Stacey D. Son
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.
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.
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/>.
23 #include "qemu/path.h"
25 #define LOCK_PATH(p, arg) \
27 (p) = lock_user_string(arg); \
29 return -TARGET_EFAULT; \
33 #define UNLOCK_PATH(p, arg) unlock_user(p, arg, 0)
36 extern struct iovec
*lock_iovec(int type
, abi_ulong target_addr
, int count
,
38 extern void unlock_iovec(struct iovec
*vec
, abi_ulong target_addr
, int count
,
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
);
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
);
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
);
55 static abi_long
do_bsd_read(abi_long arg1
, abi_long arg2
, abi_long arg3
)
60 p
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
62 return -TARGET_EFAULT
;
64 ret
= get_errno(safe_read(arg1
, p
, arg3
));
65 unlock_user(p
, arg2
, ret
);
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
)
77 p
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
79 return -TARGET_EFAULT
;
81 if (regpairs_aligned(cpu_env
) != 0) {
85 ret
= get_errno(safe_pread(arg1
, p
, arg3
, target_arg64(arg4
, arg5
)));
86 unlock_user(p
, arg2
, ret
);
92 static abi_long
do_bsd_readv(abi_long arg1
, abi_long arg2
, abi_long arg3
)
95 struct iovec
*vec
= lock_iovec(VERIFY_WRITE
, arg2
, arg3
, 0);
98 ret
= get_errno(safe_readv(arg1
, vec
, arg3
));
99 unlock_iovec(vec
, arg2
, arg3
, 1);
101 ret
= -host_to_target_errno(errno
);
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
)
112 struct iovec
*vec
= lock_iovec(VERIFY_WRITE
, arg2
, arg3
, 1);
115 if (regpairs_aligned(cpu_env
) != 0) {
119 ret
= get_errno(safe_preadv(arg1
, vec
, arg3
, target_arg64(arg4
, arg5
)));
120 unlock_iovec(vec
, arg2
, arg3
, 0);
122 ret
= -host_to_target_errno(errno
);
129 static abi_long
do_bsd_write(abi_long arg1
, abi_long arg2
, abi_long arg3
)
131 abi_long nbytes
, ret
;
134 /* nbytes < 0 implies that it was larger than SIZE_MAX. */
137 return -TARGET_EINVAL
;
139 p
= lock_user(VERIFY_READ
, arg2
, nbytes
, 1);
141 return -TARGET_EFAULT
;
143 ret
= get_errno(safe_write(arg1
, p
, arg3
));
144 unlock_user(p
, arg2
, 0);
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
)
156 p
= lock_user(VERIFY_READ
, arg2
, arg3
, 1);
158 return -TARGET_EFAULT
;
160 if (regpairs_aligned(cpu_env
) != 0) {
164 ret
= get_errno(safe_pwrite(arg1
, p
, arg3
, target_arg64(arg4
, arg5
)));
165 unlock_user(p
, arg2
, 0);
171 static abi_long
do_bsd_writev(abi_long arg1
, abi_long arg2
, abi_long arg3
)
174 struct iovec
*vec
= lock_iovec(VERIFY_READ
, arg2
, arg3
, 1);
177 ret
= get_errno(safe_writev(arg1
, vec
, arg3
));
178 unlock_iovec(vec
, arg2
, arg3
, 0);
180 ret
= -host_to_target_errno(errno
);
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
)
191 struct iovec
*vec
= lock_iovec(VERIFY_READ
, arg2
, arg3
, 1);
194 if (regpairs_aligned(cpu_env
) != 0) {
198 ret
= get_errno(safe_pwritev(arg1
, vec
, arg3
, target_arg64(arg4
, arg5
)));
199 unlock_iovec(vec
, arg2
, arg3
, 0);
201 ret
= -host_to_target_errno(errno
);
208 static abi_long
do_bsd_open(abi_long arg1
, abi_long arg2
, abi_long arg3
)
214 ret
= get_errno(safe_open(path(p
), target_to_host_bitmask(arg2
,
215 fcntl_flags_tbl
), arg3
));
216 UNLOCK_PATH(p
, arg1
);
222 static abi_long
do_bsd_openat(abi_long arg1
, abi_long arg2
,
223 abi_long arg3
, abi_long arg4
)
229 ret
= get_errno(safe_openat(arg1
, path(p
),
230 target_to_host_bitmask(arg3
, fcntl_flags_tbl
), arg4
));
231 UNLOCK_PATH(p
, arg2
);
237 static inline abi_long
do_bsd_close(abi_long arg1
)
239 return get_errno(close(arg1
));
242 #endif /* BSD_FILE_H */