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)
35 #define LOCK_PATH2(p1, arg1, p2, arg2) \
37 (p1) = lock_user_string(arg1); \
39 return -TARGET_EFAULT; \
41 (p2) = lock_user_string(arg2); \
43 unlock_user(p1, arg1, 0); \
44 return -TARGET_EFAULT; \
48 #define UNLOCK_PATH2(p1, arg1, p2, arg2) \
50 unlock_user(p2, arg2, 0); \
51 unlock_user(p1, arg1, 0); \
54 struct iovec *lock_iovec(int type, abi_ulong target_addr, int count, int copy);
55 void unlock_iovec(struct iovec *vec, abi_ulong target_addr, int count, int copy);
57 int safe_open(const char *path, int flags, mode_t mode);
58 int safe_openat(int fd, const char *path, int flags, mode_t mode);
60 ssize_t safe_read(int fd, void *buf, size_t nbytes);
61 ssize_t safe_pread(int fd, void *buf, size_t nbytes, off_t offset);
62 ssize_t safe_readv(int fd, const struct iovec *iov, int iovcnt);
63 ssize_t safe_preadv(int fd, const struct iovec *iov, int iovcnt, off_t offset);
65 ssize_t safe_write(int fd, void *buf, size_t nbytes);
66 ssize_t safe_pwrite(int fd, void *buf, size_t nbytes, off_t offset);
67 ssize_t safe_writev(int fd, const struct iovec *iov, int iovcnt);
68 ssize_t safe_pwritev(int fd, const struct iovec *iov, int iovcnt, off_t offset);
71 static abi_long do_bsd_read(abi_long arg1, abi_long arg2, abi_long arg3)
76 p = lock_user(VERIFY_WRITE, arg2, arg3, 0);
78 return -TARGET_EFAULT;
80 ret = get_errno(safe_read(arg1, p, arg3));
81 unlock_user(p, arg2, ret);
87 static abi_long do_bsd_pread(void *cpu_env, abi_long arg1,
88 abi_long arg2, abi_long arg3, abi_long arg4, abi_long arg5, abi_long arg6)
93 p = lock_user(VERIFY_WRITE, arg2, arg3, 0);
95 return -TARGET_EFAULT;
97 if (regpairs_aligned(cpu_env) != 0) {
101 ret = get_errno(safe_pread(arg1, p, arg3, target_arg64(arg4, arg5)));
102 unlock_user(p, arg2, ret);
108 static abi_long do_bsd_readv(abi_long arg1, abi_long arg2, abi_long arg3)
111 struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
114 ret = get_errno(safe_readv(arg1, vec, arg3));
115 unlock_iovec(vec, arg2, arg3, 1);
117 ret = -host_to_target_errno(errno);
124 static abi_long do_bsd_preadv(void *cpu_env, abi_long arg1,
125 abi_long arg2, abi_long arg3, abi_long arg4, abi_long arg5, abi_long arg6)
128 struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 1);
131 if (regpairs_aligned(cpu_env) != 0) {
135 ret = get_errno(safe_preadv(arg1, vec, arg3, target_arg64(arg4, arg5)));
136 unlock_iovec(vec, arg2, arg3, 0);
138 ret = -host_to_target_errno(errno);
145 static abi_long do_bsd_write(abi_long arg1, abi_long arg2, abi_long arg3)
147 abi_long nbytes, ret;
150 /* nbytes < 0 implies that it was larger than SIZE_MAX. */
153 return -TARGET_EINVAL;
155 p = lock_user(VERIFY_READ, arg2, nbytes, 1);
157 return -TARGET_EFAULT;
159 ret = get_errno(safe_write(arg1, p, arg3));
160 unlock_user(p, arg2, 0);
166 static abi_long do_bsd_pwrite(void *cpu_env, abi_long arg1,
167 abi_long arg2, abi_long arg3, abi_long arg4, abi_long arg5, abi_long arg6)
172 p = lock_user(VERIFY_READ, arg2, arg3, 1);
174 return -TARGET_EFAULT;
176 if (regpairs_aligned(cpu_env) != 0) {
180 ret = get_errno(safe_pwrite(arg1, p, arg3, target_arg64(arg4, arg5)));
181 unlock_user(p, arg2, 0);
187 static abi_long do_bsd_writev(abi_long arg1, abi_long arg2, abi_long arg3)
190 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
193 ret = get_errno(safe_writev(arg1, vec, arg3));
194 unlock_iovec(vec, arg2, arg3, 0);
196 ret = -host_to_target_errno(errno);
203 static abi_long do_bsd_pwritev(void *cpu_env, abi_long arg1,
204 abi_long arg2, abi_long arg3, abi_long arg4, abi_long arg5, abi_long arg6)
207 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
210 if (regpairs_aligned(cpu_env) != 0) {
214 ret = get_errno(safe_pwritev(arg1, vec, arg3, target_arg64(arg4, arg5)));
215 unlock_iovec(vec, arg2, arg3, 0);
217 ret = -host_to_target_errno(errno);
224 static abi_long do_bsd_open(abi_long arg1, abi_long arg2, abi_long arg3)
230 ret = get_errno(safe_open(path(p), target_to_host_bitmask(arg2,
231 fcntl_flags_tbl), arg3));
232 UNLOCK_PATH(p, arg1);
238 static abi_long do_bsd_openat(abi_long arg1, abi_long arg2,
239 abi_long arg3, abi_long arg4)
245 ret = get_errno(safe_openat(arg1, path(p),
246 target_to_host_bitmask(arg3, fcntl_flags_tbl), arg4));
247 UNLOCK_PATH(p, arg2);
253 static abi_long do_bsd_close(abi_long arg1)
255 return get_errno(close(arg1));
259 static abi_long do_bsd_fdatasync(abi_long arg1)
261 return get_errno(fdatasync(arg1));
265 static abi_long do_bsd_fsync(abi_long arg1)
267 return get_errno(fsync(arg1));
271 static abi_long do_bsd_closefrom(abi_long arg1)
273 closefrom(arg1); /* returns void */
278 static abi_long do_bsd_revoke(abi_long arg1)
284 ret = get_errno(revoke(p)); /* XXX path(p)? */
285 UNLOCK_PATH(p, arg1);
291 static abi_long do_bsd_access(abi_long arg1, abi_long arg2)
297 ret = get_errno(access(path(p), arg2));
298 UNLOCK_PATH(p, arg1);
304 static abi_long do_bsd_eaccess(abi_long arg1, abi_long arg2)
310 ret = get_errno(eaccess(path(p), arg2));
311 UNLOCK_PATH(p, arg1);
317 static abi_long do_bsd_faccessat(abi_long arg1, abi_long arg2,
318 abi_long arg3, abi_long arg4)
324 ret = get_errno(faccessat(arg1, p, arg3, arg4)); /* XXX path(p)? */
325 UNLOCK_PATH(p, arg2);
331 static abi_long do_bsd_chdir(abi_long arg1)
337 ret = get_errno(chdir(p)); /* XXX path(p)? */
338 UNLOCK_PATH(p, arg1);
344 static abi_long do_bsd_fchdir(abi_long arg1)
346 return get_errno(fchdir(arg1));
350 static abi_long do_bsd_rename(abi_long arg1, abi_long arg2)
355 LOCK_PATH2(p1, arg1, p2, arg2);
356 ret = get_errno(rename(p1, p2)); /* XXX path(p1), path(p2) */
357 UNLOCK_PATH2(p1, arg1, p2, arg2);
363 static abi_long do_bsd_renameat(abi_long arg1, abi_long arg2,
364 abi_long arg3, abi_long arg4)
369 LOCK_PATH2(p1, arg2, p2, arg4);
370 ret = get_errno(renameat(arg1, p1, arg3, p2));
371 UNLOCK_PATH2(p1, arg2, p2, arg4);
377 static abi_long do_bsd_link(abi_long arg1, abi_long arg2)
382 LOCK_PATH2(p1, arg1, p2, arg2);
383 ret = get_errno(link(p1, p2)); /* XXX path(p1), path(p2) */
384 UNLOCK_PATH2(p1, arg1, p2, arg2);
390 static abi_long do_bsd_linkat(abi_long arg1, abi_long arg2,
391 abi_long arg3, abi_long arg4, abi_long arg5)
396 LOCK_PATH2(p1, arg2, p2, arg4);
397 ret = get_errno(linkat(arg1, p1, arg3, p2, arg5));
398 UNLOCK_PATH2(p1, arg2, p2, arg4);
404 static abi_long do_bsd_unlink(abi_long arg1)
410 ret = get_errno(unlink(p)); /* XXX path(p) */
411 UNLOCK_PATH(p, arg1);
417 static abi_long do_bsd_unlinkat(abi_long arg1, abi_long arg2,
424 ret = get_errno(unlinkat(arg1, p, arg3)); /* XXX path(p) */
425 UNLOCK_PATH(p, arg2);
431 static abi_long do_bsd_mkdir(abi_long arg1, abi_long arg2)
437 ret = get_errno(mkdir(p, arg2)); /* XXX path(p) */
438 UNLOCK_PATH(p, arg1);
444 static abi_long do_bsd_mkdirat(abi_long arg1, abi_long arg2,
451 ret = get_errno(mkdirat(arg1, p, arg3));
452 UNLOCK_PATH(p, arg2);
458 static abi_long do_bsd_rmdir(abi_long arg1)
464 ret = get_errno(rmdir(p)); /* XXX path(p)? */
465 UNLOCK_PATH(p, arg1);
470 /* undocumented __getcwd(char *buf, size_t len) system call */
471 static abi_long do_bsd___getcwd(abi_long arg1, abi_long arg2)
476 p = lock_user(VERIFY_WRITE, arg1, arg2, 0);
478 return -TARGET_EFAULT;
480 ret = safe_syscall(SYS___getcwd, p, arg2);
481 unlock_user(p, arg1, ret == 0 ? strlen(p) + 1 : 0);
483 return get_errno(ret);
487 static abi_long do_bsd_dup(abi_long arg1)
489 return get_errno(dup(arg1));
493 static abi_long do_bsd_dup2(abi_long arg1, abi_long arg2)
495 return get_errno(dup2(arg1, arg2));
499 static abi_long do_bsd_truncate(void *cpu_env, abi_long arg1,
500 abi_long arg2, abi_long arg3, abi_long arg4)
506 if (regpairs_aligned(cpu_env) != 0) {
510 ret = get_errno(truncate(p, target_arg64(arg2, arg3)));
511 UNLOCK_PATH(p, arg1);
517 static abi_long do_bsd_ftruncate(void *cpu_env, abi_long arg1,
518 abi_long arg2, abi_long arg3, abi_long arg4)
520 if (regpairs_aligned(cpu_env) != 0) {
524 return get_errno(ftruncate(arg1, target_arg64(arg2, arg3)));
528 static abi_long do_bsd_acct(abi_long arg1)
534 ret = get_errno(acct(NULL));
537 ret = get_errno(acct(path(p)));
538 UNLOCK_PATH(p, arg1);
544 static abi_long do_bsd_sync(void)
551 static abi_long do_bsd_mount(abi_long arg1, abi_long arg2, abi_long arg3,
557 LOCK_PATH2(p1, arg1, p2, arg2);
559 * XXX arg4 should be locked, but it isn't clear how to do that since it may
560 * be not be a NULL-terminated string.
563 ret = get_errno(mount(p1, p2, arg3, NULL)); /* XXX path(p2)? */
565 ret = get_errno(mount(p1, p2, arg3, g2h_untagged(arg4))); /* XXX path(p2)? */
567 UNLOCK_PATH2(p1, arg1, p2, arg2);
573 static abi_long do_bsd_unmount(abi_long arg1, abi_long arg2)
579 ret = get_errno(unmount(p, arg2)); /* XXX path(p)? */
580 UNLOCK_PATH(p, arg1);
586 static abi_long do_bsd_nmount(abi_long arg1, abi_long count,
590 struct iovec *vec = lock_iovec(VERIFY_READ, arg1, count, 1);
593 ret = get_errno(nmount(vec, count, flags));
594 unlock_iovec(vec, arg1, count, 0);
596 return -TARGET_EFAULT;
603 static abi_long do_bsd_symlink(abi_long arg1, abi_long arg2)
608 LOCK_PATH2(p1, arg1, p2, arg2);
609 ret = get_errno(symlink(p1, p2)); /* XXX path(p1), path(p2) */
610 UNLOCK_PATH2(p1, arg1, p2, arg2);
616 static abi_long do_bsd_symlinkat(abi_long arg1, abi_long arg2,
622 LOCK_PATH2(p1, arg1, p2, arg3);
623 ret = get_errno(symlinkat(p1, arg2, p2)); /* XXX path(p1), path(p2) */
624 UNLOCK_PATH2(p1, arg1, p2, arg3);
630 static abi_long do_bsd_readlink(CPUArchState *env, abi_long arg1,
631 abi_long arg2, abi_long arg3)
637 p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
639 UNLOCK_PATH(p1, arg1);
640 return -TARGET_EFAULT;
642 if (strcmp(p1, "/proc/curproc/file") == 0) {
643 CPUState *cpu = env_cpu(env);
644 TaskState *ts = (TaskState *)cpu->opaque;
645 strncpy(p2, ts->bprm->fullpath, arg3);
646 ret = MIN((abi_long)strlen(ts->bprm->fullpath), arg3);
648 ret = get_errno(readlink(path(p1), p2, arg3));
650 unlock_user(p2, arg2, ret);
651 UNLOCK_PATH(p1, arg1);
657 static abi_long do_bsd_readlinkat(abi_long arg1, abi_long arg2,
658 abi_long arg3, abi_long arg4)
664 p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
666 UNLOCK_PATH(p1, arg2);
667 return -TARGET_EFAULT;
669 ret = get_errno(readlinkat(arg1, p1, p2, arg4));
670 unlock_user(p2, arg3, ret);
671 UNLOCK_PATH(p1, arg2);
677 static abi_long do_bsd_chmod(abi_long arg1, abi_long arg2)
683 ret = get_errno(chmod(p, arg2)); /* XXX path(p)? */
684 UNLOCK_PATH(p, arg1);
690 static abi_long do_bsd_fchmod(abi_long arg1, abi_long arg2)
692 return get_errno(fchmod(arg1, arg2));
696 static abi_long do_bsd_lchmod(abi_long arg1, abi_long arg2)
702 ret = get_errno(lchmod(p, arg2)); /* XXX path(p)? */
703 UNLOCK_PATH(p, arg1);
709 static abi_long do_bsd_fchmodat(abi_long arg1, abi_long arg2,
710 abi_long arg3, abi_long arg4)
716 ret = get_errno(fchmodat(arg1, p, arg3, arg4));
717 UNLOCK_PATH(p, arg2);
722 /* pre-ino64 mknod(2) */
723 static abi_long do_bsd_freebsd11_mknod(abi_long arg1, abi_long arg2, abi_long arg3)
729 ret = get_errno(syscall(SYS_freebsd11_mknod, p, arg2, arg3));
730 UNLOCK_PATH(p, arg1);
735 /* pre-ino64 mknodat(2) */
736 static abi_long do_bsd_freebsd11_mknodat(abi_long arg1, abi_long arg2,
737 abi_long arg3, abi_long arg4)
743 ret = get_errno(syscall(SYS_freebsd11_mknodat, arg1, p, arg3, arg4));
744 UNLOCK_PATH(p, arg2);
749 /* post-ino64 mknodat(2) */
750 static abi_long do_bsd_mknodat(void *cpu_env, abi_long arg1,
751 abi_long arg2, abi_long arg3, abi_long arg4, abi_long arg5,
758 /* 32-bit arch's use two 32 registers for 64 bit return value */
759 if (regpairs_aligned(cpu_env) != 0) {
760 ret = get_errno(mknodat(arg1, p, arg3, target_arg64(arg5, arg6)));
762 ret = get_errno(mknodat(arg1, p, arg3, target_arg64(arg4, arg5)));
764 UNLOCK_PATH(p, arg2);
770 static abi_long do_bsd_chown(abi_long arg1, abi_long arg2, abi_long arg3)
776 ret = get_errno(chown(p, arg2, arg3)); /* XXX path(p)? */
777 UNLOCK_PATH(p, arg1);
783 static abi_long do_bsd_fchown(abi_long arg1, abi_long arg2,
786 return get_errno(fchown(arg1, arg2, arg3));
790 static abi_long do_bsd_lchown(abi_long arg1, abi_long arg2,
797 ret = get_errno(lchown(p, arg2, arg3)); /* XXX path(p)? */
798 UNLOCK_PATH(p, arg1);
804 static abi_long do_bsd_fchownat(abi_long arg1, abi_long arg2,
805 abi_long arg3, abi_long arg4, abi_long arg5)
811 ret = get_errno(fchownat(arg1, p, arg3, arg4, arg5)); /* XXX path(p)? */
812 UNLOCK_PATH(p, arg2);
818 static abi_long do_bsd_chflags(abi_long arg1, abi_long arg2)
824 ret = get_errno(chflags(p, arg2)); /* XXX path(p)? */
825 UNLOCK_PATH(p, arg1);
831 static abi_long do_bsd_lchflags(abi_long arg1, abi_long arg2)
837 ret = get_errno(lchflags(p, arg2)); /* XXX path(p)? */
838 UNLOCK_PATH(p, arg1);
844 static abi_long do_bsd_fchflags(abi_long arg1, abi_long arg2)
846 return get_errno(fchflags(arg1, arg2));
850 static abi_long do_bsd_chroot(abi_long arg1)
856 ret = get_errno(chroot(p)); /* XXX path(p)? */
857 UNLOCK_PATH(p, arg1);
863 static abi_long do_bsd_flock(abi_long arg1, abi_long arg2)
865 return get_errno(flock(arg1, arg2));
869 static abi_long do_bsd_mkfifo(abi_long arg1, abi_long arg2)
875 ret = get_errno(mkfifo(p, arg2)); /* XXX path(p)? */
876 UNLOCK_PATH(p, arg1);
882 static abi_long do_bsd_mkfifoat(abi_long arg1, abi_long arg2,
889 ret = get_errno(mkfifoat(arg1, p, arg3));
890 UNLOCK_PATH(p, arg2);
896 static abi_long do_bsd_pathconf(abi_long arg1, abi_long arg2)
902 ret = get_errno(pathconf(p, arg2)); /* XXX path(p)? */
903 UNLOCK_PATH(p, arg1);
909 static abi_long do_bsd_lpathconf(abi_long arg1, abi_long arg2)
915 ret = get_errno(lpathconf(p, arg2)); /* XXX path(p)? */
916 UNLOCK_PATH(p, arg1);
922 static abi_long do_bsd_fpathconf(abi_long arg1, abi_long arg2)
924 return get_errno(fpathconf(arg1, arg2));
928 static abi_long do_bsd_undelete(abi_long arg1)
934 ret = get_errno(undelete(p)); /* XXX path(p)? */
935 UNLOCK_PATH(p, arg1);
940 #endif /* BSD_FILE_H */