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 static inline 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 static inline int 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
))
91 #if defined(TARGET_I386)
92 static abi_long
do_freebsd_sysarch(CPUX86State
*env
, int op
, abi_ulong parms
)
100 case TARGET_FREEBSD_I386_SET_GSBASE
:
101 case TARGET_FREEBSD_I386_SET_FSBASE
:
102 if (op
== TARGET_FREEBSD_I386_SET_GSBASE
)
104 case TARGET_FREEBSD_AMD64_SET_GSBASE
:
105 case TARGET_FREEBSD_AMD64_SET_FSBASE
:
106 if (op
== TARGET_FREEBSD_AMD64_SET_GSBASE
)
111 if (get_user(val
, parms
, abi_ulong
))
112 return -TARGET_EFAULT
;
113 cpu_x86_load_seg(env
, idx
, 0);
114 env
->segs
[idx
].base
= val
;
117 case TARGET_FREEBSD_I386_GET_GSBASE
:
118 case TARGET_FREEBSD_I386_GET_FSBASE
:
119 if (op
== TARGET_FREEBSD_I386_GET_GSBASE
)
121 case TARGET_FREEBSD_AMD64_GET_GSBASE
:
122 case TARGET_FREEBSD_AMD64_GET_FSBASE
:
123 if (op
== TARGET_FREEBSD_AMD64_GET_GSBASE
)
128 val
= env
->segs
[idx
].base
;
129 if (put_user(val
, parms
, abi_ulong
))
130 return -TARGET_EFAULT
;
132 /* XXX handle the others... */
134 ret
= -TARGET_EINVAL
;
142 static abi_long
do_freebsd_sysarch(void *env
, int op
, abi_ulong parms
)
145 * TARGET_FREEBSD_SPARC_UTRAP_INSTALL,
146 * TARGET_FREEBSD_SPARC_SIGTRAMP_INSTALL
148 return -TARGET_EINVAL
;
154 * XXX this uses the undocumented oidfmt interface to find the kind of
155 * a requested sysctl, see /sys/kern/kern_sysctl.c:sysctl_sysctl_oidfmt()
156 * (this is mostly copied from src/sbin/sysctl/sysctl.c)
159 oidfmt(int *oid
, int len
, char *fmt
, uint32_t *kind
)
161 int qoid
[CTL_MAXNAME
+2];
168 memcpy(qoid
+ 2, oid
, len
* sizeof(int));
171 i
= sysctl(qoid
, len
+ 2, buf
, &j
, 0, 0);
176 *kind
= *(uint32_t *)buf
;
179 strcpy(fmt
, (char *)(buf
+ sizeof(uint32_t)));
184 * try and convert sysctl return data for the target.
185 * XXX doesn't handle CTLTYPE_OPAQUE and CTLTYPE_STRUCT.
187 static int sysctl_oldcvt(void *holdp
, size_t holdlen
, uint32_t kind
)
189 switch (kind
& CTLTYPE
) {
192 *(uint32_t *)holdp
= tswap32(*(uint32_t *)holdp
);
197 *(uint32_t *)holdp
= tswap32(*(long *)holdp
);
201 *(uint64_t *)holdp
= tswap64(*(long *)holdp
);
203 *(uint64_t *)holdp
= tswap64(*(unsigned long *)holdp
);
212 *(uint64_t *)holdp
= tswap64(*(uint64_t *)holdp
);
223 /* XXX this needs to be emulated on non-FreeBSD hosts... */
224 static abi_long
do_freebsd_sysctl(abi_ulong namep
, int32_t namelen
, abi_ulong oldp
,
225 abi_ulong oldlenp
, abi_ulong newp
, abi_ulong newlen
)
228 void *hnamep
, *holdp
, *hnewp
= NULL
;
230 abi_ulong oldlen
= 0;
231 int32_t *snamep
= g_malloc(sizeof(int32_t) * namelen
), *p
, *q
, i
;
235 get_user_ual(oldlen
, oldlenp
);
236 if (!(hnamep
= lock_user(VERIFY_READ
, namep
, namelen
, 1)))
237 return -TARGET_EFAULT
;
238 if (newp
&& !(hnewp
= lock_user(VERIFY_READ
, newp
, newlen
, 1)))
239 return -TARGET_EFAULT
;
240 if (!(holdp
= lock_user(VERIFY_WRITE
, oldp
, oldlen
, 0)))
241 return -TARGET_EFAULT
;
243 for (p
= hnamep
, q
= snamep
, i
= 0; i
< namelen
; p
++, i
++)
245 oidfmt(snamep
, namelen
, NULL
, &kind
);
247 ret
= get_errno(sysctl(snamep
, namelen
, holdp
, &holdlen
, hnewp
, newlen
));
249 sysctl_oldcvt(holdp
, holdlen
, kind
);
250 put_user_ual(holdlen
, oldlenp
);
251 unlock_user(hnamep
, namep
, 0);
252 unlock_user(holdp
, oldp
, holdlen
);
254 unlock_user(hnewp
, newp
, 0);
261 * lock_iovec()/unlock_iovec() have a return code of 0 for success where
262 * other lock functions have a return code of 0 for failure.
264 static abi_long
lock_iovec(int type
, struct iovec
*vec
, abi_ulong target_addr
,
267 struct target_iovec
*target_vec
;
271 target_vec
= lock_user(VERIFY_READ
, target_addr
, count
* sizeof(struct target_iovec
), 1);
273 return -TARGET_EFAULT
;
274 for(i
= 0;i
< count
; i
++) {
275 base
= tswapl(target_vec
[i
].iov_base
);
276 vec
[i
].iov_len
= tswapl(target_vec
[i
].iov_len
);
277 if (vec
[i
].iov_len
!= 0) {
278 vec
[i
].iov_base
= lock_user(type
, base
, vec
[i
].iov_len
, copy
);
279 /* Don't check lock_user return value. We must call writev even
280 if a element has invalid base address. */
282 /* zero length pointer is ignored */
283 vec
[i
].iov_base
= NULL
;
286 unlock_user (target_vec
, target_addr
, 0);
290 static abi_long
unlock_iovec(struct iovec
*vec
, abi_ulong target_addr
,
293 struct target_iovec
*target_vec
;
297 target_vec
= lock_user(VERIFY_READ
, target_addr
, count
* sizeof(struct target_iovec
), 1);
299 return -TARGET_EFAULT
;
300 for(i
= 0;i
< count
; i
++) {
301 if (target_vec
[i
].iov_base
) {
302 base
= tswapl(target_vec
[i
].iov_base
);
303 unlock_user(vec
[i
].iov_base
, base
, copy
? vec
[i
].iov_len
: 0);
306 unlock_user (target_vec
, target_addr
, 0);
311 /* do_syscall() should always have a single exit point at the end so
312 that actions, such as logging of syscall results, can be performed.
313 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
314 abi_long
do_freebsd_syscall(void *cpu_env
, int num
, abi_long arg1
,
315 abi_long arg2
, abi_long arg3
, abi_long arg4
,
316 abi_long arg5
, abi_long arg6
, abi_long arg7
,
319 CPUState
*cpu
= env_cpu(cpu_env
);
324 gemu_log("freebsd syscall %d\n", num
);
326 record_syscall_start(cpu
, num
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
, 0, 0);
329 print_freebsd_syscall(num
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
332 case TARGET_FREEBSD_NR_exit
:
337 qemu_plugin_atexit_cb();
338 /* XXX: should free thread stack and CPU env */
340 ret
= 0; /* avoid warning */
342 case TARGET_FREEBSD_NR_read
:
343 if (!(p
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0)))
345 ret
= get_errno(read(arg1
, p
, arg3
));
346 unlock_user(p
, arg2
, ret
);
348 case TARGET_FREEBSD_NR_write
:
349 if (!(p
= lock_user(VERIFY_READ
, arg2
, arg3
, 1)))
351 ret
= get_errno(write(arg1
, p
, arg3
));
352 unlock_user(p
, arg2
, 0);
354 case TARGET_FREEBSD_NR_writev
:
359 vec
= alloca(count
* sizeof(struct iovec
));
360 if (lock_iovec(VERIFY_READ
, vec
, arg2
, count
, 1) < 0)
362 ret
= get_errno(writev(arg1
, vec
, count
));
363 unlock_iovec(vec
, arg2
, count
, 0);
366 case TARGET_FREEBSD_NR_open
:
367 if (!(p
= lock_user_string(arg1
)))
369 ret
= get_errno(open(path(p
),
370 target_to_host_bitmask(arg2
, fcntl_flags_tbl
),
372 unlock_user(p
, arg1
, 0);
374 case TARGET_FREEBSD_NR_mmap
:
375 ret
= get_errno(target_mmap(arg1
, arg2
, arg3
,
376 target_to_host_bitmask(arg4
, mmap_flags_tbl
),
380 case TARGET_FREEBSD_NR_mprotect
:
381 ret
= get_errno(target_mprotect(arg1
, arg2
, arg3
));
383 case TARGET_FREEBSD_NR_break
:
384 ret
= do_obreak(arg1
);
387 case TARGET_FREEBSD_NR___sysctl
:
388 ret
= do_freebsd_sysctl(arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
391 case TARGET_FREEBSD_NR_sysarch
:
392 ret
= do_freebsd_sysarch(cpu_env
, arg1
, arg2
);
394 case TARGET_FREEBSD_NR_syscall
:
395 case TARGET_FREEBSD_NR___syscall
:
396 ret
= do_freebsd_syscall(cpu_env
,arg1
& 0xffff,arg2
,arg3
,arg4
,arg5
,arg6
,arg7
,arg8
,0);
399 ret
= get_errno(syscall(num
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
, arg7
, arg8
));
404 gemu_log(" = %ld\n", ret
);
407 print_freebsd_syscall_ret(num
, ret
);
409 record_syscall_return(cpu
, num
, ret
);
412 ret
= -TARGET_EFAULT
;
416 abi_long
do_netbsd_syscall(void *cpu_env
, int num
, abi_long arg1
,
417 abi_long arg2
, abi_long arg3
, abi_long arg4
,
418 abi_long arg5
, abi_long arg6
)
420 CPUState
*cpu
= env_cpu(cpu_env
);
425 gemu_log("netbsd syscall %d\n", num
);
428 record_syscall_start(cpu
, num
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
, 0, 0);
431 print_netbsd_syscall(num
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
434 case TARGET_NETBSD_NR_exit
:
439 qemu_plugin_atexit_cb();
440 /* XXX: should free thread stack and CPU env */
442 ret
= 0; /* avoid warning */
444 case TARGET_NETBSD_NR_read
:
445 if (!(p
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0)))
447 ret
= get_errno(read(arg1
, p
, arg3
));
448 unlock_user(p
, arg2
, ret
);
450 case TARGET_NETBSD_NR_write
:
451 if (!(p
= lock_user(VERIFY_READ
, arg2
, arg3
, 1)))
453 ret
= get_errno(write(arg1
, p
, arg3
));
454 unlock_user(p
, arg2
, 0);
456 case TARGET_NETBSD_NR_open
:
457 if (!(p
= lock_user_string(arg1
)))
459 ret
= get_errno(open(path(p
),
460 target_to_host_bitmask(arg2
, fcntl_flags_tbl
),
462 unlock_user(p
, arg1
, 0);
464 case TARGET_NETBSD_NR_mmap
:
465 ret
= get_errno(target_mmap(arg1
, arg2
, arg3
,
466 target_to_host_bitmask(arg4
, mmap_flags_tbl
),
470 case TARGET_NETBSD_NR_mprotect
:
471 ret
= get_errno(target_mprotect(arg1
, arg2
, arg3
));
473 case TARGET_NETBSD_NR_syscall
:
474 case TARGET_NETBSD_NR___syscall
:
475 ret
= do_netbsd_syscall(cpu_env
,arg1
& 0xffff,arg2
,arg3
,arg4
,arg5
,arg6
,0);
478 ret
= syscall(num
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
483 gemu_log(" = %ld\n", ret
);
486 print_netbsd_syscall_ret(num
, ret
);
488 record_syscall_return(cpu
, num
, ret
);
491 ret
= -TARGET_EFAULT
;
495 abi_long
do_openbsd_syscall(void *cpu_env
, int num
, abi_long arg1
,
496 abi_long arg2
, abi_long arg3
, abi_long arg4
,
497 abi_long arg5
, abi_long arg6
)
499 CPUState
*cpu
= env_cpu(cpu_env
);
504 gemu_log("openbsd syscall %d\n", num
);
507 record_syscall_start(cpu
, num
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
, 0, 0);
510 print_openbsd_syscall(num
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
513 case TARGET_OPENBSD_NR_exit
:
518 qemu_plugin_atexit_cb();
519 /* XXX: should free thread stack and CPU env */
521 ret
= 0; /* avoid warning */
523 case TARGET_OPENBSD_NR_read
:
524 if (!(p
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0)))
526 ret
= get_errno(read(arg1
, p
, arg3
));
527 unlock_user(p
, arg2
, ret
);
529 case TARGET_OPENBSD_NR_write
:
530 if (!(p
= lock_user(VERIFY_READ
, arg2
, arg3
, 1)))
532 ret
= get_errno(write(arg1
, p
, arg3
));
533 unlock_user(p
, arg2
, 0);
535 case TARGET_OPENBSD_NR_open
:
536 if (!(p
= lock_user_string(arg1
)))
538 ret
= get_errno(open(path(p
),
539 target_to_host_bitmask(arg2
, fcntl_flags_tbl
),
541 unlock_user(p
, arg1
, 0);
543 case TARGET_OPENBSD_NR_mmap
:
544 ret
= get_errno(target_mmap(arg1
, arg2
, arg3
,
545 target_to_host_bitmask(arg4
, mmap_flags_tbl
),
549 case TARGET_OPENBSD_NR_mprotect
:
550 ret
= get_errno(target_mprotect(arg1
, arg2
, arg3
));
552 case TARGET_OPENBSD_NR_syscall
:
553 case TARGET_OPENBSD_NR___syscall
:
554 ret
= do_openbsd_syscall(cpu_env
,arg1
& 0xffff,arg2
,arg3
,arg4
,arg5
,arg6
,0);
557 ret
= syscall(num
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
562 gemu_log(" = %ld\n", ret
);
565 print_openbsd_syscall_ret(num
, ret
);
567 record_syscall_return(cpu
, num
, ret
);
570 ret
= -TARGET_EFAULT
;
574 void syscall_init(void)