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"
32 static abi_ulong target_brk
;
33 static abi_ulong target_original_brk
;
35 static inline abi_long
get_errno(abi_long ret
)
38 /* XXX need to translate host -> target errnos here */
44 #define target_to_host_bitmask(x, tbl) (x)
46 static inline int is_error(abi_long ret
)
48 return (abi_ulong
)ret
>= (abi_ulong
)(-4096);
51 void target_set_brk(abi_ulong new_brk
)
53 target_original_brk
= target_brk
= HOST_PAGE_ALIGN(new_brk
);
56 /* do_obreak() must return target errnos. */
57 static abi_long
do_obreak(abi_ulong new_brk
)
65 if (new_brk
< target_original_brk
)
66 return -TARGET_EINVAL
;
68 brk_page
= HOST_PAGE_ALIGN(target_brk
);
70 /* If the new brk is less than this, set it and we're done... */
71 if (new_brk
< brk_page
) {
76 /* We need to allocate more memory after the brk... */
77 new_alloc_size
= HOST_PAGE_ALIGN(new_brk
- brk_page
+ 1);
78 mapped_addr
= get_errno(target_mmap(brk_page
, new_alloc_size
,
80 MAP_ANON
|MAP_FIXED
|MAP_PRIVATE
, -1, 0));
82 if (!is_error(mapped_addr
))
90 #if defined(TARGET_I386)
91 static abi_long
do_freebsd_sysarch(CPUX86State
*env
, int op
, abi_ulong parms
)
99 case TARGET_FREEBSD_I386_SET_GSBASE
:
100 case TARGET_FREEBSD_I386_SET_FSBASE
:
101 if (op
== TARGET_FREEBSD_I386_SET_GSBASE
)
103 case TARGET_FREEBSD_AMD64_SET_GSBASE
:
104 case TARGET_FREEBSD_AMD64_SET_FSBASE
:
105 if (op
== TARGET_FREEBSD_AMD64_SET_GSBASE
)
110 if (get_user(val
, parms
, abi_ulong
))
111 return -TARGET_EFAULT
;
112 cpu_x86_load_seg(env
, idx
, 0);
113 env
->segs
[idx
].base
= val
;
116 case TARGET_FREEBSD_I386_GET_GSBASE
:
117 case TARGET_FREEBSD_I386_GET_FSBASE
:
118 if (op
== TARGET_FREEBSD_I386_GET_GSBASE
)
120 case TARGET_FREEBSD_AMD64_GET_GSBASE
:
121 case TARGET_FREEBSD_AMD64_GET_FSBASE
:
122 if (op
== TARGET_FREEBSD_AMD64_GET_GSBASE
)
127 val
= env
->segs
[idx
].base
;
128 if (put_user(val
, parms
, abi_ulong
))
129 return -TARGET_EFAULT
;
131 /* XXX handle the others... */
133 ret
= -TARGET_EINVAL
;
141 static abi_long
do_freebsd_sysarch(void *env
, int op
, abi_ulong parms
)
144 * TARGET_FREEBSD_SPARC_UTRAP_INSTALL,
145 * TARGET_FREEBSD_SPARC_SIGTRAMP_INSTALL
147 return -TARGET_EINVAL
;
153 * XXX this uses the undocumented oidfmt interface to find the kind of
154 * a requested sysctl, see /sys/kern/kern_sysctl.c:sysctl_sysctl_oidfmt()
155 * (this is mostly copied from src/sbin/sysctl/sysctl.c)
158 oidfmt(int *oid
, int len
, char *fmt
, uint32_t *kind
)
160 int qoid
[CTL_MAXNAME
+2];
167 memcpy(qoid
+ 2, oid
, len
* sizeof(int));
170 i
= sysctl(qoid
, len
+ 2, buf
, &j
, 0, 0);
175 *kind
= *(uint32_t *)buf
;
178 strcpy(fmt
, (char *)(buf
+ sizeof(uint32_t)));
183 * try and convert sysctl return data for the target.
184 * XXX doesn't handle CTLTYPE_OPAQUE and CTLTYPE_STRUCT.
186 static int sysctl_oldcvt(void *holdp
, size_t holdlen
, uint32_t kind
)
188 switch (kind
& CTLTYPE
) {
191 *(uint32_t *)holdp
= tswap32(*(uint32_t *)holdp
);
196 *(uint32_t *)holdp
= tswap32(*(long *)holdp
);
200 *(uint64_t *)holdp
= tswap64(*(long *)holdp
);
202 *(uint64_t *)holdp
= tswap64(*(unsigned long *)holdp
);
211 *(uint64_t *)holdp
= tswap64(*(uint64_t *)holdp
);
222 /* XXX this needs to be emulated on non-FreeBSD hosts... */
223 static abi_long
do_freebsd_sysctl(abi_ulong namep
, int32_t namelen
, abi_ulong oldp
,
224 abi_ulong oldlenp
, abi_ulong newp
, abi_ulong newlen
)
227 void *hnamep
, *holdp
, *hnewp
= NULL
;
229 abi_ulong oldlen
= 0;
230 int32_t *snamep
= g_malloc(sizeof(int32_t) * namelen
), *p
, *q
, i
;
234 get_user_ual(oldlen
, oldlenp
);
235 if (!(hnamep
= lock_user(VERIFY_READ
, namep
, namelen
, 1)))
236 return -TARGET_EFAULT
;
237 if (newp
&& !(hnewp
= lock_user(VERIFY_READ
, newp
, newlen
, 1)))
238 return -TARGET_EFAULT
;
239 if (!(holdp
= lock_user(VERIFY_WRITE
, oldp
, oldlen
, 0)))
240 return -TARGET_EFAULT
;
242 for (p
= hnamep
, q
= snamep
, i
= 0; i
< namelen
; p
++, i
++)
244 oidfmt(snamep
, namelen
, NULL
, &kind
);
246 ret
= get_errno(sysctl(snamep
, namelen
, holdp
, &holdlen
, hnewp
, newlen
));
248 sysctl_oldcvt(holdp
, holdlen
, kind
);
249 put_user_ual(holdlen
, oldlenp
);
250 unlock_user(hnamep
, namep
, 0);
251 unlock_user(holdp
, oldp
, holdlen
);
253 unlock_user(hnewp
, newp
, 0);
260 * lock_iovec()/unlock_iovec() have a return code of 0 for success where
261 * other lock functions have a return code of 0 for failure.
263 static abi_long
lock_iovec(int type
, struct iovec
*vec
, abi_ulong target_addr
,
266 struct target_iovec
*target_vec
;
270 target_vec
= lock_user(VERIFY_READ
, target_addr
, count
* sizeof(struct target_iovec
), 1);
272 return -TARGET_EFAULT
;
273 for(i
= 0;i
< count
; i
++) {
274 base
= tswapl(target_vec
[i
].iov_base
);
275 vec
[i
].iov_len
= tswapl(target_vec
[i
].iov_len
);
276 if (vec
[i
].iov_len
!= 0) {
277 vec
[i
].iov_base
= lock_user(type
, base
, vec
[i
].iov_len
, copy
);
278 /* Don't check lock_user return value. We must call writev even
279 if a element has invalid base address. */
281 /* zero length pointer is ignored */
282 vec
[i
].iov_base
= NULL
;
285 unlock_user (target_vec
, target_addr
, 0);
289 static abi_long
unlock_iovec(struct iovec
*vec
, abi_ulong target_addr
,
292 struct target_iovec
*target_vec
;
296 target_vec
= lock_user(VERIFY_READ
, target_addr
, count
* sizeof(struct target_iovec
), 1);
298 return -TARGET_EFAULT
;
299 for(i
= 0;i
< count
; i
++) {
300 if (target_vec
[i
].iov_base
) {
301 base
= tswapl(target_vec
[i
].iov_base
);
302 unlock_user(vec
[i
].iov_base
, base
, copy
? vec
[i
].iov_len
: 0);
305 unlock_user (target_vec
, target_addr
, 0);
310 /* do_syscall() should always have a single exit point at the end so
311 that actions, such as logging of syscall results, can be performed.
312 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
313 abi_long
do_freebsd_syscall(void *cpu_env
, int num
, abi_long arg1
,
314 abi_long arg2
, abi_long arg3
, abi_long arg4
,
315 abi_long arg5
, abi_long arg6
, abi_long arg7
,
318 CPUState
*cpu
= env_cpu(cpu_env
);
323 gemu_log("freebsd syscall %d\n", num
);
325 trace_guest_user_syscall(cpu
, num
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
, arg7
, arg8
);
327 print_freebsd_syscall(num
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
330 case TARGET_FREEBSD_NR_exit
:
334 gdb_exit(cpu_env
, arg1
);
335 /* XXX: should free thread stack and CPU env */
337 ret
= 0; /* avoid warning */
339 case TARGET_FREEBSD_NR_read
:
340 if (!(p
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0)))
342 ret
= get_errno(read(arg1
, p
, arg3
));
343 unlock_user(p
, arg2
, ret
);
345 case TARGET_FREEBSD_NR_write
:
346 if (!(p
= lock_user(VERIFY_READ
, arg2
, arg3
, 1)))
348 ret
= get_errno(write(arg1
, p
, arg3
));
349 unlock_user(p
, arg2
, 0);
351 case TARGET_FREEBSD_NR_writev
:
356 vec
= alloca(count
* sizeof(struct iovec
));
357 if (lock_iovec(VERIFY_READ
, vec
, arg2
, count
, 1) < 0)
359 ret
= get_errno(writev(arg1
, vec
, count
));
360 unlock_iovec(vec
, arg2
, count
, 0);
363 case TARGET_FREEBSD_NR_open
:
364 if (!(p
= lock_user_string(arg1
)))
366 ret
= get_errno(open(path(p
),
367 target_to_host_bitmask(arg2
, fcntl_flags_tbl
),
369 unlock_user(p
, arg1
, 0);
371 case TARGET_FREEBSD_NR_mmap
:
372 ret
= get_errno(target_mmap(arg1
, arg2
, arg3
,
373 target_to_host_bitmask(arg4
, mmap_flags_tbl
),
377 case TARGET_FREEBSD_NR_mprotect
:
378 ret
= get_errno(target_mprotect(arg1
, arg2
, arg3
));
380 case TARGET_FREEBSD_NR_break
:
381 ret
= do_obreak(arg1
);
384 case TARGET_FREEBSD_NR___sysctl
:
385 ret
= do_freebsd_sysctl(arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
388 case TARGET_FREEBSD_NR_sysarch
:
389 ret
= do_freebsd_sysarch(cpu_env
, arg1
, arg2
);
391 case TARGET_FREEBSD_NR_syscall
:
392 case TARGET_FREEBSD_NR___syscall
:
393 ret
= do_freebsd_syscall(cpu_env
,arg1
& 0xffff,arg2
,arg3
,arg4
,arg5
,arg6
,arg7
,arg8
,0);
396 ret
= get_errno(syscall(num
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
, arg7
, arg8
));
401 gemu_log(" = %ld\n", ret
);
404 print_freebsd_syscall_ret(num
, ret
);
405 trace_guest_user_syscall_ret(cpu
, num
, ret
);
408 ret
= -TARGET_EFAULT
;
412 abi_long
do_netbsd_syscall(void *cpu_env
, int num
, abi_long arg1
,
413 abi_long arg2
, abi_long arg3
, abi_long arg4
,
414 abi_long arg5
, abi_long arg6
)
416 CPUState
*cpu
= env_cpu(cpu_env
);
421 gemu_log("netbsd syscall %d\n", num
);
423 trace_guest_user_syscall(cpu
, num
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
, 0, 0);
425 print_netbsd_syscall(num
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
428 case TARGET_NETBSD_NR_exit
:
432 gdb_exit(cpu_env
, arg1
);
433 /* XXX: should free thread stack and CPU env */
435 ret
= 0; /* avoid warning */
437 case TARGET_NETBSD_NR_read
:
438 if (!(p
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0)))
440 ret
= get_errno(read(arg1
, p
, arg3
));
441 unlock_user(p
, arg2
, ret
);
443 case TARGET_NETBSD_NR_write
:
444 if (!(p
= lock_user(VERIFY_READ
, arg2
, arg3
, 1)))
446 ret
= get_errno(write(arg1
, p
, arg3
));
447 unlock_user(p
, arg2
, 0);
449 case TARGET_NETBSD_NR_open
:
450 if (!(p
= lock_user_string(arg1
)))
452 ret
= get_errno(open(path(p
),
453 target_to_host_bitmask(arg2
, fcntl_flags_tbl
),
455 unlock_user(p
, arg1
, 0);
457 case TARGET_NETBSD_NR_mmap
:
458 ret
= get_errno(target_mmap(arg1
, arg2
, arg3
,
459 target_to_host_bitmask(arg4
, mmap_flags_tbl
),
463 case TARGET_NETBSD_NR_mprotect
:
464 ret
= get_errno(target_mprotect(arg1
, arg2
, arg3
));
466 case TARGET_NETBSD_NR_syscall
:
467 case TARGET_NETBSD_NR___syscall
:
468 ret
= do_netbsd_syscall(cpu_env
,arg1
& 0xffff,arg2
,arg3
,arg4
,arg5
,arg6
,0);
471 ret
= syscall(num
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
476 gemu_log(" = %ld\n", ret
);
479 print_netbsd_syscall_ret(num
, ret
);
480 trace_guest_user_syscall_ret(cpu
, num
, ret
);
483 ret
= -TARGET_EFAULT
;
487 abi_long
do_openbsd_syscall(void *cpu_env
, int num
, abi_long arg1
,
488 abi_long arg2
, abi_long arg3
, abi_long arg4
,
489 abi_long arg5
, abi_long arg6
)
491 CPUState
*cpu
= env_cpu(cpu_env
);
496 gemu_log("openbsd syscall %d\n", num
);
498 trace_guest_user_syscall(cpu
, num
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
, 0, 0);
500 print_openbsd_syscall(num
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
503 case TARGET_OPENBSD_NR_exit
:
507 gdb_exit(cpu_env
, arg1
);
508 /* XXX: should free thread stack and CPU env */
510 ret
= 0; /* avoid warning */
512 case TARGET_OPENBSD_NR_read
:
513 if (!(p
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0)))
515 ret
= get_errno(read(arg1
, p
, arg3
));
516 unlock_user(p
, arg2
, ret
);
518 case TARGET_OPENBSD_NR_write
:
519 if (!(p
= lock_user(VERIFY_READ
, arg2
, arg3
, 1)))
521 ret
= get_errno(write(arg1
, p
, arg3
));
522 unlock_user(p
, arg2
, 0);
524 case TARGET_OPENBSD_NR_open
:
525 if (!(p
= lock_user_string(arg1
)))
527 ret
= get_errno(open(path(p
),
528 target_to_host_bitmask(arg2
, fcntl_flags_tbl
),
530 unlock_user(p
, arg1
, 0);
532 case TARGET_OPENBSD_NR_mmap
:
533 ret
= get_errno(target_mmap(arg1
, arg2
, arg3
,
534 target_to_host_bitmask(arg4
, mmap_flags_tbl
),
538 case TARGET_OPENBSD_NR_mprotect
:
539 ret
= get_errno(target_mprotect(arg1
, arg2
, arg3
));
541 case TARGET_OPENBSD_NR_syscall
:
542 case TARGET_OPENBSD_NR___syscall
:
543 ret
= do_openbsd_syscall(cpu_env
,arg1
& 0xffff,arg2
,arg3
,arg4
,arg5
,arg6
,0);
546 ret
= syscall(num
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
551 gemu_log(" = %ld\n", ret
);
554 print_openbsd_syscall_ret(num
, ret
);
555 trace_guest_user_syscall_ret(cpu
, num
, ret
);
558 ret
= -TARGET_EFAULT
;
562 void syscall_init(void)