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 extern struct iovec
*lock_iovec(int type
, abi_ulong target_addr
, int count
,
27 extern void unlock_iovec(struct iovec
*vec
, abi_ulong target_addr
, int count
,
30 ssize_t
safe_read(int fd
, void *buf
, size_t nbytes
);
31 ssize_t
safe_pread(int fd
, void *buf
, size_t nbytes
, off_t offset
);
32 ssize_t
safe_readv(int fd
, const struct iovec
*iov
, int iovcnt
);
33 ssize_t
safe_preadv(int fd
, const struct iovec
*iov
, int iovcnt
, off_t offset
);
35 ssize_t
safe_write(int fd
, void *buf
, size_t nbytes
);
36 ssize_t
safe_pwrite(int fd
, void *buf
, size_t nbytes
, off_t offset
);
37 ssize_t
safe_writev(int fd
, const struct iovec
*iov
, int iovcnt
);
38 ssize_t
safe_pwritev(int fd
, const struct iovec
*iov
, int iovcnt
, off_t offset
);
41 static abi_long
do_bsd_read(abi_long arg1
, abi_long arg2
, abi_long arg3
)
46 p
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
48 return -TARGET_EFAULT
;
50 ret
= get_errno(safe_read(arg1
, p
, arg3
));
51 unlock_user(p
, arg2
, ret
);
57 static abi_long
do_bsd_pread(void *cpu_env
, abi_long arg1
,
58 abi_long arg2
, abi_long arg3
, abi_long arg4
, abi_long arg5
, abi_long arg6
)
63 p
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
65 return -TARGET_EFAULT
;
67 if (regpairs_aligned(cpu_env
) != 0) {
71 ret
= get_errno(safe_pread(arg1
, p
, arg3
, target_arg64(arg4
, arg5
)));
72 unlock_user(p
, arg2
, ret
);
78 static abi_long
do_bsd_readv(abi_long arg1
, abi_long arg2
, abi_long arg3
)
81 struct iovec
*vec
= lock_iovec(VERIFY_WRITE
, arg2
, arg3
, 0);
84 ret
= get_errno(safe_readv(arg1
, vec
, arg3
));
85 unlock_iovec(vec
, arg2
, arg3
, 1);
87 ret
= -host_to_target_errno(errno
);
94 static abi_long
do_bsd_preadv(void *cpu_env
, abi_long arg1
,
95 abi_long arg2
, abi_long arg3
, abi_long arg4
, abi_long arg5
, abi_long arg6
)
98 struct iovec
*vec
= lock_iovec(VERIFY_WRITE
, arg2
, arg3
, 1);
101 if (regpairs_aligned(cpu_env
) != 0) {
105 ret
= get_errno(safe_preadv(arg1
, vec
, arg3
, target_arg64(arg4
, arg5
)));
106 unlock_iovec(vec
, arg2
, arg3
, 0);
108 ret
= -host_to_target_errno(errno
);
115 static abi_long
do_bsd_write(abi_long arg1
, abi_long arg2
, abi_long arg3
)
117 abi_long nbytes
, ret
;
120 /* nbytes < 0 implies that it was larger than SIZE_MAX. */
123 return -TARGET_EINVAL
;
125 p
= lock_user(VERIFY_READ
, arg2
, nbytes
, 1);
127 return -TARGET_EFAULT
;
129 ret
= get_errno(safe_write(arg1
, p
, arg3
));
130 unlock_user(p
, arg2
, 0);
136 static abi_long
do_bsd_pwrite(void *cpu_env
, abi_long arg1
,
137 abi_long arg2
, abi_long arg3
, abi_long arg4
, abi_long arg5
, abi_long arg6
)
142 p
= lock_user(VERIFY_READ
, arg2
, arg3
, 1);
144 return -TARGET_EFAULT
;
146 if (regpairs_aligned(cpu_env
) != 0) {
150 ret
= get_errno(safe_pwrite(arg1
, p
, arg3
, target_arg64(arg4
, arg5
)));
151 unlock_user(p
, arg2
, 0);
157 static abi_long
do_bsd_writev(abi_long arg1
, abi_long arg2
, abi_long arg3
)
160 struct iovec
*vec
= lock_iovec(VERIFY_READ
, arg2
, arg3
, 1);
163 ret
= get_errno(safe_writev(arg1
, vec
, arg3
));
164 unlock_iovec(vec
, arg2
, arg3
, 0);
166 ret
= -host_to_target_errno(errno
);
173 static abi_long
do_bsd_pwritev(void *cpu_env
, abi_long arg1
,
174 abi_long arg2
, abi_long arg3
, abi_long arg4
, abi_long arg5
, abi_long arg6
)
177 struct iovec
*vec
= lock_iovec(VERIFY_READ
, arg2
, arg3
, 1);
180 if (regpairs_aligned(cpu_env
) != 0) {
184 ret
= get_errno(safe_pwritev(arg1
, vec
, arg3
, target_arg64(arg4
, arg5
)));
185 unlock_iovec(vec
, arg2
, arg3
, 0);
187 ret
= -host_to_target_errno(errno
);
193 #endif /* BSD_FILE_H */