4 * Copyright (c) 2003 - 2008 Fabrice Bellard
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/>.
19 #include "qemu/osdep.h"
20 #include "qemu/cutils.h"
21 #include "qemu/path.h"
22 #include <sys/syscall.h>
23 #include <sys/param.h>
24 #include <sys/sysctl.h>
28 #include "qemu-common.h"
29 #include "user/syscall-trace.h"
33 static abi_ulong target_brk;
34 static abi_ulong target_original_brk;
36 abi_long get_errno(abi_long ret)
39 /* XXX need to translate host -> target errnos here */
45 #define target_to_host_bitmask(x, tbl) (x)
47 bool is_error(abi_long ret)
49 return (abi_ulong)ret >= (abi_ulong)(-4096);
52 void target_set_brk(abi_ulong new_brk)
54 target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
57 /* do_obreak() must return target errnos. */
58 static abi_long do_obreak(abi_ulong new_brk)
66 if (new_brk < target_original_brk)
67 return -TARGET_EINVAL;
69 brk_page = HOST_PAGE_ALIGN(target_brk);
71 /* If the new brk is less than this, set it and we're done... */
72 if (new_brk < brk_page) {
77 /* We need to allocate more memory after the brk... */
78 new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page + 1);
79 mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
81 MAP_ANON|MAP_FIXED|MAP_PRIVATE, -1, 0));
83 if (!is_error(mapped_addr))
93 * XXX this uses the undocumented oidfmt interface to find the kind of
94 * a requested sysctl, see /sys/kern/kern_sysctl.c:sysctl_sysctl_oidfmt()
95 * (this is mostly copied from src/sbin/sysctl/sysctl.c)
98 oidfmt(int *oid, int len, char *fmt, uint32_t *kind)
100 int qoid[CTL_MAXNAME+2];
107 memcpy(qoid + 2, oid, len * sizeof(int));
110 i = sysctl(qoid, len + 2, buf, &j, 0, 0);
115 *kind = *(uint32_t *)buf;
118 strcpy(fmt, (char *)(buf + sizeof(uint32_t)));
123 * try and convert sysctl return data for the target.
124 * XXX doesn't handle CTLTYPE_OPAQUE and CTLTYPE_STRUCT.
126 static int sysctl_oldcvt(void *holdp, size_t holdlen, uint32_t kind)
128 switch (kind & CTLTYPE) {
131 *(uint32_t *)holdp = tswap32(*(uint32_t *)holdp);
136 *(uint32_t *)holdp = tswap32(*(long *)holdp);
140 *(uint64_t *)holdp = tswap64(*(long *)holdp);
143 *(uint64_t *)holdp = tswap64(*(unsigned long *)holdp);
152 *(uint64_t *)holdp = tswap64(*(uint64_t *)holdp);
163 /* XXX this needs to be emulated on non-FreeBSD hosts... */
164 static abi_long do_freebsd_sysctl(abi_ulong namep, int32_t namelen, abi_ulong oldp,
165 abi_ulong oldlenp, abi_ulong newp, abi_ulong newlen)
168 void *hnamep, *holdp, *hnewp = NULL;
170 abi_ulong oldlen = 0;
171 int32_t *snamep = g_malloc(sizeof(int32_t) * namelen), *p, *q, i;
175 get_user_ual(oldlen, oldlenp);
176 if (!(hnamep = lock_user(VERIFY_READ, namep, namelen, 1)))
177 return -TARGET_EFAULT;
178 if (newp && !(hnewp = lock_user(VERIFY_READ, newp, newlen, 1)))
179 return -TARGET_EFAULT;
180 if (!(holdp = lock_user(VERIFY_WRITE, oldp, oldlen, 0)))
181 return -TARGET_EFAULT;
183 for (p = hnamep, q = snamep, i = 0; i < namelen; p++, i++)
185 oidfmt(snamep, namelen, NULL, &kind);
187 ret = get_errno(sysctl(snamep, namelen, holdp, &holdlen, hnewp, newlen));
189 sysctl_oldcvt(holdp, holdlen, kind);
190 put_user_ual(holdlen, oldlenp);
191 unlock_user(hnamep, namep, 0);
192 unlock_user(holdp, oldp, holdlen);
194 unlock_user(hnewp, newp, 0);
201 * lock_iovec()/unlock_iovec() have a return code of 0 for success where
202 * other lock functions have a return code of 0 for failure.
204 static abi_long lock_iovec(int type, struct iovec *vec, abi_ulong target_addr,
207 struct target_iovec *target_vec;
211 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
213 return -TARGET_EFAULT;
214 for (i = 0;i < count; i++) {
215 base = tswapl(target_vec[i].iov_base);
216 vec[i].iov_len = tswapl(target_vec[i].iov_len);
217 if (vec[i].iov_len != 0) {
218 vec[i].iov_base = lock_user(type, base, vec[i].iov_len, copy);
219 /* Don't check lock_user return value. We must call writev even
220 if a element has invalid base address. */
222 /* zero length pointer is ignored */
223 vec[i].iov_base = NULL;
226 unlock_user (target_vec, target_addr, 0);
230 static abi_long unlock_iovec(struct iovec *vec, abi_ulong target_addr,
233 struct target_iovec *target_vec;
237 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
239 return -TARGET_EFAULT;
240 for (i = 0;i < count; i++) {
241 if (target_vec[i].iov_base) {
242 base = tswapl(target_vec[i].iov_base);
243 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
246 unlock_user (target_vec, target_addr, 0);
251 /* do_syscall() should always have a single exit point at the end so
252 that actions, such as logging of syscall results, can be performed.
253 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
254 abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1,
255 abi_long arg2, abi_long arg3, abi_long arg4,
256 abi_long arg5, abi_long arg6, abi_long arg7,
259 CPUState *cpu = env_cpu(cpu_env);
264 gemu_log("freebsd syscall %d\n", num);
266 record_syscall_start(cpu, num, arg1, arg2, arg3, arg4, arg5, arg6, 0, 0);
269 print_freebsd_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
272 case TARGET_FREEBSD_NR_exit:
277 qemu_plugin_user_exit();
278 /* XXX: should free thread stack and CPU env */
280 ret = 0; /* avoid warning */
282 case TARGET_FREEBSD_NR_read:
283 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
285 ret = get_errno(read(arg1, p, arg3));
286 unlock_user(p, arg2, ret);
288 case TARGET_FREEBSD_NR_write:
289 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
291 ret = get_errno(write(arg1, p, arg3));
292 unlock_user(p, arg2, 0);
294 case TARGET_FREEBSD_NR_writev:
299 vec = alloca(count * sizeof(struct iovec));
300 if (lock_iovec(VERIFY_READ, vec, arg2, count, 1) < 0)
302 ret = get_errno(writev(arg1, vec, count));
303 unlock_iovec(vec, arg2, count, 0);
306 case TARGET_FREEBSD_NR_open:
307 if (!(p = lock_user_string(arg1)))
309 ret = get_errno(open(path(p),
310 target_to_host_bitmask(arg2, fcntl_flags_tbl),
312 unlock_user(p, arg1, 0);
314 case TARGET_FREEBSD_NR_mmap:
315 ret = get_errno(target_mmap(arg1, arg2, arg3,
316 target_to_host_bitmask(arg4, mmap_flags_tbl),
320 case TARGET_FREEBSD_NR_mprotect:
321 ret = get_errno(target_mprotect(arg1, arg2, arg3));
323 case TARGET_FREEBSD_NR_break:
324 ret = do_obreak(arg1);
327 case TARGET_FREEBSD_NR___sysctl:
328 ret = do_freebsd_sysctl(arg1, arg2, arg3, arg4, arg5, arg6);
331 case TARGET_FREEBSD_NR_sysarch:
332 ret = do_freebsd_sysarch(cpu_env, arg1, arg2);
334 case TARGET_FREEBSD_NR_syscall:
335 case TARGET_FREEBSD_NR___syscall:
336 ret = do_freebsd_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,arg7,arg8,0);
339 ret = get_errno(syscall(num, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8));
344 gemu_log(" = %ld\n", ret);
347 print_freebsd_syscall_ret(num, ret);
349 record_syscall_return(cpu, num, ret);
352 ret = -TARGET_EFAULT;
356 abi_long do_netbsd_syscall(void *cpu_env, int num, abi_long arg1,
357 abi_long arg2, abi_long arg3, abi_long arg4,
358 abi_long arg5, abi_long arg6)
360 CPUState *cpu = env_cpu(cpu_env);
365 gemu_log("netbsd syscall %d\n", num);
368 record_syscall_start(cpu, num, arg1, arg2, arg3, arg4, arg5, arg6, 0, 0);
371 print_netbsd_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
374 case TARGET_NETBSD_NR_exit:
379 qemu_plugin_user_exit();
380 /* XXX: should free thread stack and CPU env */
382 ret = 0; /* avoid warning */
384 case TARGET_NETBSD_NR_read:
385 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
387 ret = get_errno(read(arg1, p, arg3));
388 unlock_user(p, arg2, ret);
390 case TARGET_NETBSD_NR_write:
391 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
393 ret = get_errno(write(arg1, p, arg3));
394 unlock_user(p, arg2, 0);
396 case TARGET_NETBSD_NR_open:
397 if (!(p = lock_user_string(arg1)))
399 ret = get_errno(open(path(p),
400 target_to_host_bitmask(arg2, fcntl_flags_tbl),
402 unlock_user(p, arg1, 0);
404 case TARGET_NETBSD_NR_mmap:
405 ret = get_errno(target_mmap(arg1, arg2, arg3,
406 target_to_host_bitmask(arg4, mmap_flags_tbl),
410 case TARGET_NETBSD_NR_mprotect:
411 ret = get_errno(target_mprotect(arg1, arg2, arg3));
413 case TARGET_NETBSD_NR_syscall:
414 case TARGET_NETBSD_NR___syscall:
415 ret = do_netbsd_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
418 ret = syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
423 gemu_log(" = %ld\n", ret);
426 print_netbsd_syscall_ret(num, ret);
428 record_syscall_return(cpu, num, ret);
431 ret = -TARGET_EFAULT;
435 abi_long do_openbsd_syscall(void *cpu_env, int num, abi_long arg1,
436 abi_long arg2, abi_long arg3, abi_long arg4,
437 abi_long arg5, abi_long arg6)
439 CPUState *cpu = env_cpu(cpu_env);
444 gemu_log("openbsd syscall %d\n", num);
447 record_syscall_start(cpu, num, arg1, arg2, arg3, arg4, arg5, arg6, 0, 0);
450 print_openbsd_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
453 case TARGET_OPENBSD_NR_exit:
458 qemu_plugin_user_exit();
459 /* XXX: should free thread stack and CPU env */
461 ret = 0; /* avoid warning */
463 case TARGET_OPENBSD_NR_read:
464 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
466 ret = get_errno(read(arg1, p, arg3));
467 unlock_user(p, arg2, ret);
469 case TARGET_OPENBSD_NR_write:
470 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
472 ret = get_errno(write(arg1, p, arg3));
473 unlock_user(p, arg2, 0);
475 case TARGET_OPENBSD_NR_open:
476 if (!(p = lock_user_string(arg1)))
478 ret = get_errno(open(path(p),
479 target_to_host_bitmask(arg2, fcntl_flags_tbl),
481 unlock_user(p, arg1, 0);
483 case TARGET_OPENBSD_NR_mmap:
484 ret = get_errno(target_mmap(arg1, arg2, arg3,
485 target_to_host_bitmask(arg4, mmap_flags_tbl),
489 case TARGET_OPENBSD_NR_mprotect:
490 ret = get_errno(target_mprotect(arg1, arg2, arg3));
492 case TARGET_OPENBSD_NR_syscall:
493 case TARGET_OPENBSD_NR___syscall:
494 ret = do_openbsd_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
497 ret = syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
502 gemu_log(" = %ld\n", ret);
505 print_openbsd_syscall_ret(num, ret);
507 record_syscall_return(cpu, num, ret);
510 ret = -TARGET_EFAULT;
514 void syscall_init(void)