]>
Commit | Line | Data |
---|---|---|
66eed099 WL |
1 | /* |
2 | * BSD syscalls | |
3 | * | |
4 | * Copyright (c) 2003-2008 Fabrice Bellard | |
5 | * Copyright (c) 2013-2014 Stacey D. Son | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, see <http://www.gnu.org/licenses/>. | |
19 | */ | |
20 | ||
21 | /* | |
22 | * We need the FreeBSD "legacy" definitions. Rust needs the FreeBSD 11 system | |
23 | * calls since it doesn't use libc at all, so we have to emulate that despite | |
24 | * FreeBSD 11 being EOL'd. | |
25 | */ | |
26 | #define _WANT_FREEBSD11_STAT | |
27 | #define _WANT_FREEBSD11_STATFS | |
28 | #define _WANT_FREEBSD11_DIRENT | |
29 | #define _WANT_KERNEL_ERRNO | |
30 | #define _WANT_SEMUN | |
31 | #include "qemu/osdep.h" | |
32 | #include "qemu/cutils.h" | |
33 | #include "qemu/path.h" | |
34 | #include <sys/syscall.h> | |
35 | #include <sys/param.h> | |
36 | #include <sys/sysctl.h> | |
37 | #include <utime.h> | |
38 | ||
39 | #include "qemu.h" | |
66eed099 WL |
40 | #include "signal-common.h" |
41 | #include "user/syscall-trace.h" | |
42 | ||
c5c84d16 | 43 | #include "bsd-file.h" |
9554d330 | 44 | #include "bsd-proc.h" |
c5c84d16 | 45 | |
80da1b00 | 46 | /* I/O */ |
77d3522b WL |
47 | safe_syscall3(int, open, const char *, path, int, flags, mode_t, mode); |
48 | safe_syscall4(int, openat, int, fd, const char *, path, int, flags, mode_t, | |
49 | mode); | |
50 | ||
80da1b00 WL |
51 | safe_syscall3(ssize_t, read, int, fd, void *, buf, size_t, nbytes); |
52 | safe_syscall4(ssize_t, pread, int, fd, void *, buf, size_t, nbytes, off_t, | |
53 | offset); | |
54 | safe_syscall3(ssize_t, readv, int, fd, const struct iovec *, iov, int, iovcnt); | |
55 | safe_syscall4(ssize_t, preadv, int, fd, const struct iovec *, iov, int, iovcnt, | |
56 | off_t, offset); | |
57 | ||
770d8aba WL |
58 | safe_syscall3(ssize_t, write, int, fd, void *, buf, size_t, nbytes); |
59 | safe_syscall4(ssize_t, pwrite, int, fd, void *, buf, size_t, nbytes, off_t, | |
60 | offset); | |
61 | safe_syscall3(ssize_t, writev, int, fd, const struct iovec *, iov, int, iovcnt); | |
62 | safe_syscall4(ssize_t, pwritev, int, fd, const struct iovec *, iov, int, iovcnt, | |
63 | off_t, offset); | |
64 | ||
66eed099 WL |
65 | void target_set_brk(abi_ulong new_brk) |
66 | { | |
67 | } | |
68 | ||
deeff83b WL |
69 | /* |
70 | * errno conversion. | |
71 | */ | |
72 | abi_long get_errno(abi_long ret) | |
66eed099 | 73 | { |
deeff83b WL |
74 | if (ret == -1) { |
75 | return -host_to_target_errno(errno); | |
76 | } else { | |
77 | return ret; | |
78 | } | |
79 | } | |
66eed099 | 80 | |
deeff83b WL |
81 | int host_to_target_errno(int err) |
82 | { | |
83 | /* | |
84 | * All the BSDs have the property that the error numbers are uniform across | |
85 | * all architectures for a given BSD, though they may vary between different | |
86 | * BSDs. | |
87 | */ | |
88 | return err; | |
89 | } | |
90 | ||
91 | bool is_error(abi_long ret) | |
92 | { | |
66eed099 WL |
93 | return (abi_ulong)ret >= (abi_ulong)(-4096); |
94 | } | |
95 | ||
1ed771b2 WL |
96 | /* |
97 | * Unlocks a iovec. Unlike unlock_iovec, it assumes the tvec array itself is | |
98 | * already locked from target_addr. It will be unlocked as well as all the iovec | |
99 | * elements. | |
100 | */ | |
101 | static void helper_unlock_iovec(struct target_iovec *target_vec, | |
102 | abi_ulong target_addr, struct iovec *vec, | |
103 | int count, int copy) | |
104 | { | |
105 | for (int i = 0; i < count; i++) { | |
106 | abi_ulong base = tswapal(target_vec[i].iov_base); | |
107 | ||
108 | if (vec[i].iov_base) { | |
109 | unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0); | |
110 | } | |
111 | } | |
112 | unlock_user(target_vec, target_addr, 0); | |
113 | } | |
114 | ||
115 | struct iovec *lock_iovec(int type, abi_ulong target_addr, | |
116 | int count, int copy) | |
117 | { | |
118 | struct target_iovec *target_vec; | |
119 | struct iovec *vec; | |
120 | abi_ulong total_len, max_len; | |
121 | int i; | |
122 | int err = 0; | |
123 | ||
124 | if (count == 0) { | |
125 | errno = 0; | |
126 | return NULL; | |
127 | } | |
128 | if (count < 0 || count > IOV_MAX) { | |
129 | errno = EINVAL; | |
130 | return NULL; | |
131 | } | |
132 | ||
133 | vec = g_try_new0(struct iovec, count); | |
134 | if (vec == NULL) { | |
135 | errno = ENOMEM; | |
136 | return NULL; | |
137 | } | |
138 | ||
139 | target_vec = lock_user(VERIFY_READ, target_addr, | |
140 | count * sizeof(struct target_iovec), 1); | |
141 | if (target_vec == NULL) { | |
142 | err = EFAULT; | |
143 | goto fail2; | |
144 | } | |
145 | ||
146 | max_len = 0x7fffffff & MIN(TARGET_PAGE_MASK, PAGE_MASK); | |
147 | total_len = 0; | |
148 | ||
149 | for (i = 0; i < count; i++) { | |
150 | abi_ulong base = tswapal(target_vec[i].iov_base); | |
151 | abi_long len = tswapal(target_vec[i].iov_len); | |
152 | ||
153 | if (len < 0) { | |
154 | err = EINVAL; | |
155 | goto fail; | |
156 | } else if (len == 0) { | |
157 | /* Zero length pointer is ignored. */ | |
158 | vec[i].iov_base = 0; | |
159 | } else { | |
160 | vec[i].iov_base = lock_user(type, base, len, copy); | |
161 | /* | |
162 | * If the first buffer pointer is bad, this is a fault. But | |
163 | * subsequent bad buffers will result in a partial write; this is | |
164 | * realized by filling the vector with null pointers and zero | |
165 | * lengths. | |
166 | */ | |
167 | if (!vec[i].iov_base) { | |
168 | if (i == 0) { | |
169 | err = EFAULT; | |
170 | goto fail; | |
171 | } else { | |
172 | /* | |
173 | * Fail all the subsequent addresses, they are already | |
174 | * zero'd. | |
175 | */ | |
176 | goto out; | |
177 | } | |
178 | } | |
179 | if (len > max_len - total_len) { | |
180 | len = max_len - total_len; | |
181 | } | |
182 | } | |
183 | vec[i].iov_len = len; | |
184 | total_len += len; | |
185 | } | |
186 | out: | |
187 | unlock_user(target_vec, target_addr, 0); | |
188 | return vec; | |
189 | ||
190 | fail: | |
191 | helper_unlock_iovec(target_vec, target_addr, vec, i, copy); | |
192 | fail2: | |
193 | g_free(vec); | |
194 | errno = err; | |
195 | return NULL; | |
196 | } | |
197 | ||
883808d8 WL |
198 | void unlock_iovec(struct iovec *vec, abi_ulong target_addr, |
199 | int count, int copy) | |
200 | { | |
201 | struct target_iovec *target_vec; | |
202 | ||
203 | target_vec = lock_user(VERIFY_READ, target_addr, | |
204 | count * sizeof(struct target_iovec), 1); | |
205 | if (target_vec) { | |
206 | helper_unlock_iovec(target_vec, target_addr, vec, count, copy); | |
207 | } | |
208 | ||
209 | g_free(vec); | |
210 | } | |
211 | ||
66eed099 | 212 | /* |
db697887 WL |
213 | * All errnos that freebsd_syscall() returns must be -TARGET_<errcode>. |
214 | */ | |
215 | static abi_long freebsd_syscall(void *cpu_env, int num, abi_long arg1, | |
216 | abi_long arg2, abi_long arg3, abi_long arg4, | |
217 | abi_long arg5, abi_long arg6, abi_long arg7, | |
218 | abi_long arg8) | |
219 | { | |
220 | abi_long ret; | |
221 | ||
222 | switch (num) { | |
9554d330 WL |
223 | /* |
224 | * process system calls | |
225 | */ | |
226 | case TARGET_FREEBSD_NR_exit: /* exit(2) */ | |
227 | ret = do_bsd_exit(cpu_env, arg1); | |
228 | break; | |
80da1b00 WL |
229 | |
230 | /* | |
231 | * File system calls. | |
232 | */ | |
233 | case TARGET_FREEBSD_NR_read: /* read(2) */ | |
234 | ret = do_bsd_read(arg1, arg2, arg3); | |
235 | break; | |
236 | ||
237 | case TARGET_FREEBSD_NR_pread: /* pread(2) */ | |
238 | ret = do_bsd_pread(cpu_env, arg1, arg2, arg3, arg4, arg5, arg6); | |
239 | break; | |
240 | ||
241 | case TARGET_FREEBSD_NR_readv: /* readv(2) */ | |
242 | ret = do_bsd_readv(arg1, arg2, arg3); | |
243 | break; | |
244 | ||
245 | case TARGET_FREEBSD_NR_preadv: /* preadv(2) */ | |
246 | ret = do_bsd_preadv(cpu_env, arg1, arg2, arg3, arg4, arg5, arg6); | |
770d8aba WL |
247 | |
248 | case TARGET_FREEBSD_NR_write: /* write(2) */ | |
249 | ret = do_bsd_write(arg1, arg2, arg3); | |
250 | break; | |
251 | ||
252 | case TARGET_FREEBSD_NR_pwrite: /* pwrite(2) */ | |
253 | ret = do_bsd_pwrite(cpu_env, arg1, arg2, arg3, arg4, arg5, arg6); | |
254 | break; | |
255 | ||
256 | case TARGET_FREEBSD_NR_writev: /* writev(2) */ | |
257 | ret = do_bsd_writev(arg1, arg2, arg3); | |
258 | break; | |
259 | ||
260 | case TARGET_FREEBSD_NR_pwritev: /* pwritev(2) */ | |
261 | ret = do_bsd_pwritev(cpu_env, arg1, arg2, arg3, arg4, arg5, arg6); | |
80da1b00 WL |
262 | break; |
263 | ||
77d3522b WL |
264 | case TARGET_FREEBSD_NR_open: /* open(2) */ |
265 | ret = do_bsd_open(arg1, arg2, arg3); | |
266 | break; | |
267 | ||
268 | case TARGET_FREEBSD_NR_openat: /* openat(2) */ | |
269 | ret = do_bsd_openat(arg1, arg2, arg3, arg4); | |
270 | break; | |
271 | ||
272 | case TARGET_FREEBSD_NR_close: /* close(2) */ | |
273 | ret = do_bsd_close(arg1); | |
274 | break; | |
275 | ||
a2ba6c7b WL |
276 | case TARGET_FREEBSD_NR_fdatasync: /* fdatasync(2) */ |
277 | ret = do_bsd_fdatasync(arg1); | |
278 | break; | |
279 | ||
280 | case TARGET_FREEBSD_NR_fsync: /* fsync(2) */ | |
281 | ret = do_bsd_fsync(arg1); | |
282 | break; | |
283 | ||
284 | case TARGET_FREEBSD_NR_freebsd12_closefrom: /* closefrom(2) */ | |
285 | ret = do_bsd_closefrom(arg1); | |
286 | break; | |
287 | ||
65c6c4c8 WL |
288 | case TARGET_FREEBSD_NR_revoke: /* revoke(2) */ |
289 | ret = do_bsd_revoke(arg1); | |
290 | break; | |
291 | ||
292 | case TARGET_FREEBSD_NR_access: /* access(2) */ | |
293 | ret = do_bsd_access(arg1, arg2); | |
294 | break; | |
295 | ||
296 | case TARGET_FREEBSD_NR_eaccess: /* eaccess(2) */ | |
297 | ret = do_bsd_eaccess(arg1, arg2); | |
298 | break; | |
299 | ||
300 | case TARGET_FREEBSD_NR_faccessat: /* faccessat(2) */ | |
301 | ret = do_bsd_faccessat(arg1, arg2, arg3, arg4); | |
302 | break; | |
303 | ||
390f547e WL |
304 | case TARGET_FREEBSD_NR_chdir: /* chdir(2) */ |
305 | ret = do_bsd_chdir(arg1); | |
306 | break; | |
307 | ||
308 | case TARGET_FREEBSD_NR_fchdir: /* fchdir(2) */ | |
309 | ret = do_bsd_fchdir(arg1); | |
310 | break; | |
311 | ||
ab5fd2d9 WL |
312 | case TARGET_FREEBSD_NR_rename: /* rename(2) */ |
313 | ret = do_bsd_rename(arg1, arg2); | |
314 | break; | |
315 | ||
316 | case TARGET_FREEBSD_NR_renameat: /* renameat(2) */ | |
317 | ret = do_bsd_renameat(arg1, arg2, arg3, arg4); | |
318 | break; | |
319 | ||
2d3b7e01 WL |
320 | case TARGET_FREEBSD_NR_link: /* link(2) */ |
321 | ret = do_bsd_link(arg1, arg2); | |
322 | break; | |
323 | ||
324 | case TARGET_FREEBSD_NR_linkat: /* linkat(2) */ | |
325 | ret = do_bsd_linkat(arg1, arg2, arg3, arg4, arg5); | |
326 | break; | |
327 | ||
328 | case TARGET_FREEBSD_NR_unlink: /* unlink(2) */ | |
329 | ret = do_bsd_unlink(arg1); | |
330 | break; | |
331 | ||
332 | case TARGET_FREEBSD_NR_unlinkat: /* unlinkat(2) */ | |
333 | ret = do_bsd_unlinkat(arg1, arg2, arg3); | |
334 | break; | |
335 | ||
1ffbd5e7 WL |
336 | case TARGET_FREEBSD_NR_mkdir: /* mkdir(2) */ |
337 | ret = do_bsd_mkdir(arg1, arg2); | |
338 | break; | |
339 | ||
340 | case TARGET_FREEBSD_NR_mkdirat: /* mkdirat(2) */ | |
341 | ret = do_bsd_mkdirat(arg1, arg2, arg3); | |
342 | break; | |
343 | ||
6af8f76a WL |
344 | case TARGET_FREEBSD_NR_rmdir: /* rmdir(2) (XXX no rmdirat()?) */ |
345 | ret = do_bsd_rmdir(arg1); | |
346 | break; | |
347 | ||
348 | case TARGET_FREEBSD_NR___getcwd: /* undocumented __getcwd() */ | |
349 | ret = do_bsd___getcwd(arg1, arg2); | |
350 | break; | |
351 | ||
a15699ac WL |
352 | case TARGET_FREEBSD_NR_dup: /* dup(2) */ |
353 | ret = do_bsd_dup(arg1); | |
354 | break; | |
355 | ||
356 | case TARGET_FREEBSD_NR_dup2: /* dup2(2) */ | |
357 | ret = do_bsd_dup2(arg1, arg2); | |
358 | break; | |
359 | ||
4b795b14 WL |
360 | case TARGET_FREEBSD_NR_truncate: /* truncate(2) */ |
361 | ret = do_bsd_truncate(cpu_env, arg1, arg2, arg3, arg4); | |
362 | break; | |
363 | ||
364 | case TARGET_FREEBSD_NR_ftruncate: /* ftruncate(2) */ | |
365 | ret = do_bsd_ftruncate(cpu_env, arg1, arg2, arg3, arg4); | |
366 | break; | |
367 | ||
db697887 WL |
368 | default: |
369 | qemu_log_mask(LOG_UNIMP, "Unsupported syscall: %d\n", num); | |
370 | ret = -TARGET_ENOSYS; | |
371 | break; | |
372 | } | |
373 | ||
374 | return ret; | |
375 | } | |
376 | ||
377 | /* | |
378 | * do_freebsd_syscall() should always have a single exit point at the end so | |
379 | * that actions, such as logging of syscall results, can be performed. This | |
380 | * as a wrapper around freebsd_syscall() so that actually happens. Since | |
381 | * that is a singleton, modern compilers will inline it anyway... | |
66eed099 WL |
382 | */ |
383 | abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1, | |
384 | abi_long arg2, abi_long arg3, abi_long arg4, | |
385 | abi_long arg5, abi_long arg6, abi_long arg7, | |
386 | abi_long arg8) | |
387 | { | |
db697887 WL |
388 | CPUState *cpu = env_cpu(cpu_env); |
389 | int ret; | |
390 | ||
391 | trace_guest_user_syscall(cpu, num, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8); | |
392 | if (do_strace) { | |
393 | print_freebsd_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6); | |
394 | } | |
395 | ||
396 | ret = freebsd_syscall(cpu_env, num, arg1, arg2, arg3, arg4, arg5, arg6, | |
397 | arg7, arg8); | |
398 | if (do_strace) { | |
399 | print_freebsd_syscall_ret(num, ret); | |
400 | } | |
401 | trace_guest_user_syscall_ret(cpu, num, ret); | |
402 | ||
403 | return ret; | |
66eed099 WL |
404 | } |
405 | ||
406 | void syscall_init(void) | |
407 | { | |
408 | } |