xref: /openbmc/qemu/bsd-user/bsd-file.h (revision 65c6c4c8)
1c5c84d16SWarner Losh /*
2c5c84d16SWarner Losh  *  file related system call shims and definitions
3c5c84d16SWarner Losh  *
4c5c84d16SWarner Losh  *  Copyright (c) 2013 Stacey D. Son
5c5c84d16SWarner Losh  *
6c5c84d16SWarner Losh  *  This program is free software; you can redistribute it and/or modify
7c5c84d16SWarner Losh  *  it under the terms of the GNU General Public License as published by
8c5c84d16SWarner Losh  *  the Free Software Foundation; either version 2 of the License, or
9c5c84d16SWarner Losh  *  (at your option) any later version.
10c5c84d16SWarner Losh  *
11c5c84d16SWarner Losh  *  This program is distributed in the hope that it will be useful,
12c5c84d16SWarner Losh  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13c5c84d16SWarner Losh  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14c5c84d16SWarner Losh  *  GNU General Public License for more details.
15c5c84d16SWarner Losh  *
16c5c84d16SWarner Losh  *  You should have received a copy of the GNU General Public License
17c5c84d16SWarner Losh  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
18c5c84d16SWarner Losh  */
19c5c84d16SWarner Losh 
209c092804SMarkus Armbruster #ifndef BSD_FILE_H
219c092804SMarkus Armbruster #define BSD_FILE_H
22c5c84d16SWarner Losh 
23c5c84d16SWarner Losh #include "qemu/path.h"
24c5c84d16SWarner Losh 
2577d3522bSWarner Losh #define LOCK_PATH(p, arg)                   \
2677d3522bSWarner Losh do {                                        \
2777d3522bSWarner Losh     (p) = lock_user_string(arg);            \
2877d3522bSWarner Losh     if ((p) == NULL) {                      \
2977d3522bSWarner Losh         return -TARGET_EFAULT;              \
3077d3522bSWarner Losh     }                                       \
3177d3522bSWarner Losh } while (0)
3277d3522bSWarner Losh 
3377d3522bSWarner Losh #define UNLOCK_PATH(p, arg)     unlock_user(p, arg, 0)
3477d3522bSWarner Losh 
3577d3522bSWarner Losh 
36c5c84d16SWarner Losh extern struct iovec *lock_iovec(int type, abi_ulong target_addr, int count,
37c5c84d16SWarner Losh         int copy);
38c5c84d16SWarner Losh extern void unlock_iovec(struct iovec *vec, abi_ulong target_addr, int count,
39c5c84d16SWarner Losh         int copy);
40c5c84d16SWarner Losh 
4177d3522bSWarner Losh int safe_open(const char *path, int flags, mode_t mode);
4277d3522bSWarner Losh int safe_openat(int fd, const char *path, int flags, mode_t mode);
4377d3522bSWarner Losh 
4480da1b00SWarner Losh ssize_t safe_read(int fd, void *buf, size_t nbytes);
4580da1b00SWarner Losh ssize_t safe_pread(int fd, void *buf, size_t nbytes, off_t offset);
4680da1b00SWarner Losh ssize_t safe_readv(int fd, const struct iovec *iov, int iovcnt);
4780da1b00SWarner Losh ssize_t safe_preadv(int fd, const struct iovec *iov, int iovcnt, off_t offset);
4880da1b00SWarner Losh 
49770d8abaSWarner Losh ssize_t safe_write(int fd, void *buf, size_t nbytes);
50770d8abaSWarner Losh ssize_t safe_pwrite(int fd, void *buf, size_t nbytes, off_t offset);
51770d8abaSWarner Losh ssize_t safe_writev(int fd, const struct iovec *iov, int iovcnt);
52770d8abaSWarner Losh ssize_t safe_pwritev(int fd, const struct iovec *iov, int iovcnt, off_t offset);
53770d8abaSWarner Losh 
5480da1b00SWarner Losh /* read(2) */
5580da1b00SWarner Losh static abi_long do_bsd_read(abi_long arg1, abi_long arg2, abi_long arg3)
5680da1b00SWarner Losh {
5780da1b00SWarner Losh     abi_long ret;
5880da1b00SWarner Losh     void *p;
5980da1b00SWarner Losh 
6080da1b00SWarner Losh     p = lock_user(VERIFY_WRITE, arg2, arg3, 0);
6180da1b00SWarner Losh     if (p == NULL) {
6280da1b00SWarner Losh         return -TARGET_EFAULT;
6380da1b00SWarner Losh     }
6480da1b00SWarner Losh     ret = get_errno(safe_read(arg1, p, arg3));
6580da1b00SWarner Losh     unlock_user(p, arg2, ret);
6680da1b00SWarner Losh 
6780da1b00SWarner Losh     return ret;
6880da1b00SWarner Losh }
6980da1b00SWarner Losh 
7080da1b00SWarner Losh /* pread(2) */
7180da1b00SWarner Losh static abi_long do_bsd_pread(void *cpu_env, abi_long arg1,
7280da1b00SWarner Losh     abi_long arg2, abi_long arg3, abi_long arg4, abi_long arg5, abi_long arg6)
7380da1b00SWarner Losh {
7480da1b00SWarner Losh     abi_long ret;
7580da1b00SWarner Losh     void *p;
7680da1b00SWarner Losh 
7780da1b00SWarner Losh     p = lock_user(VERIFY_WRITE, arg2, arg3, 0);
7880da1b00SWarner Losh     if (p == NULL) {
7980da1b00SWarner Losh         return -TARGET_EFAULT;
8080da1b00SWarner Losh     }
8180da1b00SWarner Losh     if (regpairs_aligned(cpu_env) != 0) {
8280da1b00SWarner Losh         arg4 = arg5;
8380da1b00SWarner Losh         arg5 = arg6;
8480da1b00SWarner Losh     }
8580da1b00SWarner Losh     ret = get_errno(safe_pread(arg1, p, arg3, target_arg64(arg4, arg5)));
8680da1b00SWarner Losh     unlock_user(p, arg2, ret);
8780da1b00SWarner Losh 
8880da1b00SWarner Losh     return ret;
8980da1b00SWarner Losh }
9080da1b00SWarner Losh 
9180da1b00SWarner Losh /* readv(2) */
9280da1b00SWarner Losh static abi_long do_bsd_readv(abi_long arg1, abi_long arg2, abi_long arg3)
9380da1b00SWarner Losh {
9480da1b00SWarner Losh     abi_long ret;
9580da1b00SWarner Losh     struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
9680da1b00SWarner Losh 
9780da1b00SWarner Losh     if (vec != NULL) {
9880da1b00SWarner Losh         ret = get_errno(safe_readv(arg1, vec, arg3));
9980da1b00SWarner Losh         unlock_iovec(vec, arg2, arg3, 1);
10080da1b00SWarner Losh     } else {
10180da1b00SWarner Losh         ret = -host_to_target_errno(errno);
10280da1b00SWarner Losh     }
10380da1b00SWarner Losh 
10480da1b00SWarner Losh     return ret;
10580da1b00SWarner Losh }
10680da1b00SWarner Losh 
10780da1b00SWarner Losh /* preadv(2) */
10880da1b00SWarner Losh static abi_long do_bsd_preadv(void *cpu_env, abi_long arg1,
10980da1b00SWarner Losh     abi_long arg2, abi_long arg3, abi_long arg4, abi_long arg5, abi_long arg6)
11080da1b00SWarner Losh {
11180da1b00SWarner Losh     abi_long ret;
11280da1b00SWarner Losh     struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 1);
11380da1b00SWarner Losh 
11480da1b00SWarner Losh     if (vec != NULL) {
11580da1b00SWarner Losh         if (regpairs_aligned(cpu_env) != 0) {
11680da1b00SWarner Losh             arg4 = arg5;
11780da1b00SWarner Losh             arg5 = arg6;
11880da1b00SWarner Losh         }
11980da1b00SWarner Losh         ret = get_errno(safe_preadv(arg1, vec, arg3, target_arg64(arg4, arg5)));
12080da1b00SWarner Losh         unlock_iovec(vec, arg2, arg3, 0);
12180da1b00SWarner Losh     } else {
12280da1b00SWarner Losh         ret = -host_to_target_errno(errno);
12380da1b00SWarner Losh     }
12480da1b00SWarner Losh 
12580da1b00SWarner Losh     return ret;
12680da1b00SWarner Losh }
12780da1b00SWarner Losh 
128770d8abaSWarner Losh /* write(2) */
129770d8abaSWarner Losh static abi_long do_bsd_write(abi_long arg1, abi_long arg2, abi_long arg3)
130770d8abaSWarner Losh {
131770d8abaSWarner Losh     abi_long nbytes, ret;
132770d8abaSWarner Losh     void *p;
133770d8abaSWarner Losh 
134770d8abaSWarner Losh     /* nbytes < 0 implies that it was larger than SIZE_MAX. */
135770d8abaSWarner Losh     nbytes = arg3;
136770d8abaSWarner Losh     if (nbytes < 0) {
137770d8abaSWarner Losh         return -TARGET_EINVAL;
138770d8abaSWarner Losh     }
139770d8abaSWarner Losh     p = lock_user(VERIFY_READ, arg2, nbytes, 1);
140770d8abaSWarner Losh     if (p == NULL) {
141770d8abaSWarner Losh         return -TARGET_EFAULT;
142770d8abaSWarner Losh     }
143770d8abaSWarner Losh     ret = get_errno(safe_write(arg1, p, arg3));
144770d8abaSWarner Losh     unlock_user(p, arg2, 0);
145770d8abaSWarner Losh 
146770d8abaSWarner Losh     return ret;
147770d8abaSWarner Losh }
148770d8abaSWarner Losh 
149770d8abaSWarner Losh /* pwrite(2) */
150770d8abaSWarner Losh static abi_long do_bsd_pwrite(void *cpu_env, abi_long arg1,
151770d8abaSWarner Losh     abi_long arg2, abi_long arg3, abi_long arg4, abi_long arg5, abi_long arg6)
152770d8abaSWarner Losh {
153770d8abaSWarner Losh     abi_long ret;
154770d8abaSWarner Losh     void *p;
155770d8abaSWarner Losh 
156770d8abaSWarner Losh     p = lock_user(VERIFY_READ, arg2, arg3, 1);
157770d8abaSWarner Losh     if (p == NULL) {
158770d8abaSWarner Losh         return -TARGET_EFAULT;
159770d8abaSWarner Losh     }
160770d8abaSWarner Losh     if (regpairs_aligned(cpu_env) != 0) {
161770d8abaSWarner Losh         arg4 = arg5;
162770d8abaSWarner Losh         arg5 = arg6;
163770d8abaSWarner Losh     }
164770d8abaSWarner Losh     ret = get_errno(safe_pwrite(arg1, p, arg3, target_arg64(arg4, arg5)));
165770d8abaSWarner Losh     unlock_user(p, arg2, 0);
166770d8abaSWarner Losh 
167770d8abaSWarner Losh     return ret;
168770d8abaSWarner Losh }
169770d8abaSWarner Losh 
170770d8abaSWarner Losh /* writev(2) */
171770d8abaSWarner Losh static abi_long do_bsd_writev(abi_long arg1, abi_long arg2, abi_long arg3)
172770d8abaSWarner Losh {
173770d8abaSWarner Losh     abi_long ret;
174770d8abaSWarner Losh     struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
175770d8abaSWarner Losh 
176770d8abaSWarner Losh     if (vec != NULL) {
177770d8abaSWarner Losh         ret = get_errno(safe_writev(arg1, vec, arg3));
178770d8abaSWarner Losh         unlock_iovec(vec, arg2, arg3, 0);
179770d8abaSWarner Losh     } else {
180770d8abaSWarner Losh         ret = -host_to_target_errno(errno);
181770d8abaSWarner Losh     }
182770d8abaSWarner Losh 
183770d8abaSWarner Losh     return ret;
184770d8abaSWarner Losh }
185770d8abaSWarner Losh 
186770d8abaSWarner Losh /* pwritev(2) */
187770d8abaSWarner Losh static abi_long do_bsd_pwritev(void *cpu_env, abi_long arg1,
188770d8abaSWarner Losh     abi_long arg2, abi_long arg3, abi_long arg4, abi_long arg5, abi_long arg6)
189770d8abaSWarner Losh {
190770d8abaSWarner Losh     abi_long ret;
191770d8abaSWarner Losh     struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
192770d8abaSWarner Losh 
193770d8abaSWarner Losh     if (vec != NULL) {
194770d8abaSWarner Losh         if (regpairs_aligned(cpu_env) != 0) {
195770d8abaSWarner Losh             arg4 = arg5;
196770d8abaSWarner Losh             arg5 = arg6;
197770d8abaSWarner Losh         }
198770d8abaSWarner Losh         ret = get_errno(safe_pwritev(arg1, vec, arg3, target_arg64(arg4, arg5)));
199770d8abaSWarner Losh         unlock_iovec(vec, arg2, arg3, 0);
200770d8abaSWarner Losh     } else {
201770d8abaSWarner Losh         ret = -host_to_target_errno(errno);
202770d8abaSWarner Losh     }
203770d8abaSWarner Losh 
204770d8abaSWarner Losh     return ret;
205770d8abaSWarner Losh }
206770d8abaSWarner Losh 
20777d3522bSWarner Losh /* open(2) */
20877d3522bSWarner Losh static abi_long do_bsd_open(abi_long arg1, abi_long arg2, abi_long arg3)
20977d3522bSWarner Losh {
21077d3522bSWarner Losh     abi_long ret;
21177d3522bSWarner Losh     void *p;
21277d3522bSWarner Losh 
21377d3522bSWarner Losh     LOCK_PATH(p, arg1);
21477d3522bSWarner Losh     ret = get_errno(safe_open(path(p), target_to_host_bitmask(arg2,
21577d3522bSWarner Losh                 fcntl_flags_tbl), arg3));
21677d3522bSWarner Losh     UNLOCK_PATH(p, arg1);
21777d3522bSWarner Losh 
21877d3522bSWarner Losh     return ret;
21977d3522bSWarner Losh }
22077d3522bSWarner Losh 
22177d3522bSWarner Losh /* openat(2) */
22277d3522bSWarner Losh static abi_long do_bsd_openat(abi_long arg1, abi_long arg2,
22377d3522bSWarner Losh         abi_long arg3, abi_long arg4)
22477d3522bSWarner Losh {
22577d3522bSWarner Losh     abi_long ret;
22677d3522bSWarner Losh     void *p;
22777d3522bSWarner Losh 
22877d3522bSWarner Losh     LOCK_PATH(p, arg2);
22977d3522bSWarner Losh     ret = get_errno(safe_openat(arg1, path(p),
23077d3522bSWarner Losh                 target_to_host_bitmask(arg3, fcntl_flags_tbl), arg4));
23177d3522bSWarner Losh     UNLOCK_PATH(p, arg2);
23277d3522bSWarner Losh 
23377d3522bSWarner Losh     return ret;
23477d3522bSWarner Losh }
23577d3522bSWarner Losh 
23677d3522bSWarner Losh /* close(2) */
23777d3522bSWarner Losh static inline abi_long do_bsd_close(abi_long arg1)
23877d3522bSWarner Losh {
23977d3522bSWarner Losh     return get_errno(close(arg1));
24077d3522bSWarner Losh }
24177d3522bSWarner Losh 
242a2ba6c7bSWarner Losh /* fdatasync(2) */
243a2ba6c7bSWarner Losh static abi_long do_bsd_fdatasync(abi_long arg1)
244a2ba6c7bSWarner Losh {
245a2ba6c7bSWarner Losh     return get_errno(fdatasync(arg1));
246a2ba6c7bSWarner Losh }
247a2ba6c7bSWarner Losh 
248a2ba6c7bSWarner Losh /* fsync(2) */
249a2ba6c7bSWarner Losh static abi_long do_bsd_fsync(abi_long arg1)
250a2ba6c7bSWarner Losh {
251a2ba6c7bSWarner Losh     return get_errno(fsync(arg1));
252a2ba6c7bSWarner Losh }
253a2ba6c7bSWarner Losh 
254a2ba6c7bSWarner Losh /* closefrom(2) */
255a2ba6c7bSWarner Losh static abi_long do_bsd_closefrom(abi_long arg1)
256a2ba6c7bSWarner Losh {
257a2ba6c7bSWarner Losh     closefrom(arg1);  /* returns void */
258a2ba6c7bSWarner Losh     return get_errno(0);
259a2ba6c7bSWarner Losh }
260a2ba6c7bSWarner Losh 
261*65c6c4c8SWarner Losh /* revoke(2) */
262*65c6c4c8SWarner Losh static abi_long do_bsd_revoke(abi_long arg1)
263*65c6c4c8SWarner Losh {
264*65c6c4c8SWarner Losh     abi_long ret;
265*65c6c4c8SWarner Losh     void *p;
266*65c6c4c8SWarner Losh 
267*65c6c4c8SWarner Losh     LOCK_PATH(p, arg1);
268*65c6c4c8SWarner Losh     ret = get_errno(revoke(p)); /* XXX path(p)? */
269*65c6c4c8SWarner Losh     UNLOCK_PATH(p, arg1);
270*65c6c4c8SWarner Losh 
271*65c6c4c8SWarner Losh     return ret;
272*65c6c4c8SWarner Losh }
273*65c6c4c8SWarner Losh 
274*65c6c4c8SWarner Losh /* access(2) */
275*65c6c4c8SWarner Losh static abi_long do_bsd_access(abi_long arg1, abi_long arg2)
276*65c6c4c8SWarner Losh {
277*65c6c4c8SWarner Losh     abi_long ret;
278*65c6c4c8SWarner Losh     void *p;
279*65c6c4c8SWarner Losh 
280*65c6c4c8SWarner Losh     LOCK_PATH(p, arg1);
281*65c6c4c8SWarner Losh     ret = get_errno(access(path(p), arg2));
282*65c6c4c8SWarner Losh     UNLOCK_PATH(p, arg1);
283*65c6c4c8SWarner Losh 
284*65c6c4c8SWarner Losh     return ret;
285*65c6c4c8SWarner Losh }
286*65c6c4c8SWarner Losh 
287*65c6c4c8SWarner Losh /* eaccess(2) */
288*65c6c4c8SWarner Losh static abi_long do_bsd_eaccess(abi_long arg1, abi_long arg2)
289*65c6c4c8SWarner Losh {
290*65c6c4c8SWarner Losh     abi_long ret;
291*65c6c4c8SWarner Losh     void *p;
292*65c6c4c8SWarner Losh 
293*65c6c4c8SWarner Losh     LOCK_PATH(p, arg1);
294*65c6c4c8SWarner Losh     ret = get_errno(eaccess(path(p), arg2));
295*65c6c4c8SWarner Losh     UNLOCK_PATH(p, arg1);
296*65c6c4c8SWarner Losh 
297*65c6c4c8SWarner Losh     return ret;
298*65c6c4c8SWarner Losh }
299*65c6c4c8SWarner Losh 
300*65c6c4c8SWarner Losh /* faccessat(2) */
301*65c6c4c8SWarner Losh static abi_long do_bsd_faccessat(abi_long arg1, abi_long arg2,
302*65c6c4c8SWarner Losh         abi_long arg3, abi_long arg4)
303*65c6c4c8SWarner Losh {
304*65c6c4c8SWarner Losh     abi_long ret;
305*65c6c4c8SWarner Losh     void *p;
306*65c6c4c8SWarner Losh 
307*65c6c4c8SWarner Losh     LOCK_PATH(p, arg2);
308*65c6c4c8SWarner Losh     ret = get_errno(faccessat(arg1, p, arg3, arg4)); /* XXX path(p)? */
309*65c6c4c8SWarner Losh     UNLOCK_PATH(p, arg2);
310*65c6c4c8SWarner Losh 
311*65c6c4c8SWarner Losh     return ret;
312*65c6c4c8SWarner Losh }
313*65c6c4c8SWarner Losh 
3149c092804SMarkus Armbruster #endif /* BSD_FILE_H */
315