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"
21 #include <sys/syscall.h>
22 #include <sys/param.h>
23 #include <sys/sysctl.h>
27 #include "qemu-common.h"
31 static abi_ulong target_brk
;
32 static abi_ulong target_original_brk
;
34 static inline abi_long
get_errno(abi_long ret
)
37 /* XXX need to translate host -> target errnos here */
43 #define target_to_host_bitmask(x, tbl) (x)
45 static inline int is_error(abi_long ret
)
47 return (abi_ulong
)ret
>= (abi_ulong
)(-4096);
50 void target_set_brk(abi_ulong new_brk
)
52 target_original_brk
= target_brk
= HOST_PAGE_ALIGN(new_brk
);
55 /* do_obreak() must return target errnos. */
56 static abi_long
do_obreak(abi_ulong new_brk
)
64 if (new_brk
< target_original_brk
)
65 return -TARGET_EINVAL
;
67 brk_page
= HOST_PAGE_ALIGN(target_brk
);
69 /* If the new brk is less than this, set it and we're done... */
70 if (new_brk
< brk_page
) {
75 /* We need to allocate more memory after the brk... */
76 new_alloc_size
= HOST_PAGE_ALIGN(new_brk
- brk_page
+ 1);
77 mapped_addr
= get_errno(target_mmap(brk_page
, new_alloc_size
,
79 MAP_ANON
|MAP_FIXED
|MAP_PRIVATE
, -1, 0));
81 if (!is_error(mapped_addr
))
89 #if defined(TARGET_I386)
90 static abi_long
do_freebsd_sysarch(CPUX86State
*env
, int op
, abi_ulong parms
)
98 case TARGET_FREEBSD_I386_SET_GSBASE
:
99 case TARGET_FREEBSD_I386_SET_FSBASE
:
100 if (op
== TARGET_FREEBSD_I386_SET_GSBASE
)
102 case TARGET_FREEBSD_AMD64_SET_GSBASE
:
103 case TARGET_FREEBSD_AMD64_SET_FSBASE
:
104 if (op
== TARGET_FREEBSD_AMD64_SET_GSBASE
)
109 if (get_user(val
, parms
, abi_ulong
))
110 return -TARGET_EFAULT
;
111 cpu_x86_load_seg(env
, idx
, 0);
112 env
->segs
[idx
].base
= val
;
115 case TARGET_FREEBSD_I386_GET_GSBASE
:
116 case TARGET_FREEBSD_I386_GET_FSBASE
:
117 if (op
== TARGET_FREEBSD_I386_GET_GSBASE
)
119 case TARGET_FREEBSD_AMD64_GET_GSBASE
:
120 case TARGET_FREEBSD_AMD64_GET_FSBASE
:
121 if (op
== TARGET_FREEBSD_AMD64_GET_GSBASE
)
126 val
= env
->segs
[idx
].base
;
127 if (put_user(val
, parms
, abi_ulong
))
128 return -TARGET_EFAULT
;
130 /* XXX handle the others... */
132 ret
= -TARGET_EINVAL
;
140 static abi_long
do_freebsd_sysarch(void *env
, int op
, abi_ulong parms
)
143 * TARGET_FREEBSD_SPARC_UTRAP_INSTALL,
144 * TARGET_FREEBSD_SPARC_SIGTRAMP_INSTALL
146 return -TARGET_EINVAL
;
152 * XXX this uses the undocumented oidfmt interface to find the kind of
153 * a requested sysctl, see /sys/kern/kern_sysctl.c:sysctl_sysctl_oidfmt()
154 * (this is mostly copied from src/sbin/sysctl/sysctl.c)
157 oidfmt(int *oid
, int len
, char *fmt
, uint32_t *kind
)
159 int qoid
[CTL_MAXNAME
+2];
166 memcpy(qoid
+ 2, oid
, len
* sizeof(int));
169 i
= sysctl(qoid
, len
+ 2, buf
, &j
, 0, 0);
174 *kind
= *(uint32_t *)buf
;
177 strcpy(fmt
, (char *)(buf
+ sizeof(uint32_t)));
182 * try and convert sysctl return data for the target.
183 * XXX doesn't handle CTLTYPE_OPAQUE and CTLTYPE_STRUCT.
185 static int sysctl_oldcvt(void *holdp
, size_t holdlen
, uint32_t kind
)
187 switch (kind
& CTLTYPE
) {
190 *(uint32_t *)holdp
= tswap32(*(uint32_t *)holdp
);
195 *(uint32_t *)holdp
= tswap32(*(long *)holdp
);
199 *(uint64_t *)holdp
= tswap64(*(long *)holdp
);
201 *(uint64_t *)holdp
= tswap64(*(unsigned long *)holdp
);
210 *(uint64_t *)holdp
= tswap64(*(uint64_t *)holdp
);
221 /* XXX this needs to be emulated on non-FreeBSD hosts... */
222 static abi_long
do_freebsd_sysctl(abi_ulong namep
, int32_t namelen
, abi_ulong oldp
,
223 abi_ulong oldlenp
, abi_ulong newp
, abi_ulong newlen
)
226 void *hnamep
, *holdp
, *hnewp
= NULL
;
228 abi_ulong oldlen
= 0;
229 int32_t *snamep
= g_malloc(sizeof(int32_t) * namelen
), *p
, *q
, i
;
233 get_user_ual(oldlen
, oldlenp
);
234 if (!(hnamep
= lock_user(VERIFY_READ
, namep
, namelen
, 1)))
235 return -TARGET_EFAULT
;
236 if (newp
&& !(hnewp
= lock_user(VERIFY_READ
, newp
, newlen
, 1)))
237 return -TARGET_EFAULT
;
238 if (!(holdp
= lock_user(VERIFY_WRITE
, oldp
, oldlen
, 0)))
239 return -TARGET_EFAULT
;
241 for (p
= hnamep
, q
= snamep
, i
= 0; i
< namelen
; p
++, i
++)
243 oidfmt(snamep
, namelen
, NULL
, &kind
);
245 ret
= get_errno(sysctl(snamep
, namelen
, holdp
, &holdlen
, hnewp
, newlen
));
247 sysctl_oldcvt(holdp
, holdlen
, kind
);
248 put_user_ual(holdlen
, oldlenp
);
249 unlock_user(hnamep
, namep
, 0);
250 unlock_user(holdp
, oldp
, holdlen
);
252 unlock_user(hnewp
, newp
, 0);
259 * lock_iovec()/unlock_iovec() have a return code of 0 for success where
260 * other lock functions have a return code of 0 for failure.
262 static abi_long
lock_iovec(int type
, struct iovec
*vec
, abi_ulong target_addr
,
265 struct target_iovec
*target_vec
;
269 target_vec
= lock_user(VERIFY_READ
, target_addr
, count
* sizeof(struct target_iovec
), 1);
271 return -TARGET_EFAULT
;
272 for(i
= 0;i
< count
; i
++) {
273 base
= tswapl(target_vec
[i
].iov_base
);
274 vec
[i
].iov_len
= tswapl(target_vec
[i
].iov_len
);
275 if (vec
[i
].iov_len
!= 0) {
276 vec
[i
].iov_base
= lock_user(type
, base
, vec
[i
].iov_len
, copy
);
277 /* Don't check lock_user return value. We must call writev even
278 if a element has invalid base address. */
280 /* zero length pointer is ignored */
281 vec
[i
].iov_base
= NULL
;
284 unlock_user (target_vec
, target_addr
, 0);
288 static abi_long
unlock_iovec(struct iovec
*vec
, abi_ulong target_addr
,
291 struct target_iovec
*target_vec
;
295 target_vec
= lock_user(VERIFY_READ
, target_addr
, count
* sizeof(struct target_iovec
), 1);
297 return -TARGET_EFAULT
;
298 for(i
= 0;i
< count
; i
++) {
299 if (target_vec
[i
].iov_base
) {
300 base
= tswapl(target_vec
[i
].iov_base
);
301 unlock_user(vec
[i
].iov_base
, base
, copy
? vec
[i
].iov_len
: 0);
304 unlock_user (target_vec
, target_addr
, 0);
309 /* do_syscall() should always have a single exit point at the end so
310 that actions, such as logging of syscall results, can be performed.
311 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
312 abi_long
do_freebsd_syscall(void *cpu_env
, int num
, abi_long arg1
,
313 abi_long arg2
, abi_long arg3
, abi_long arg4
,
314 abi_long arg5
, abi_long arg6
, abi_long arg7
,
321 gemu_log("freebsd syscall %d\n", num
);
324 print_freebsd_syscall(num
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
327 case TARGET_FREEBSD_NR_exit
:
331 gdb_exit(cpu_env
, arg1
);
332 /* XXX: should free thread stack and CPU env */
334 ret
= 0; /* avoid warning */
336 case TARGET_FREEBSD_NR_read
:
337 if (!(p
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0)))
339 ret
= get_errno(read(arg1
, p
, arg3
));
340 unlock_user(p
, arg2
, ret
);
342 case TARGET_FREEBSD_NR_write
:
343 if (!(p
= lock_user(VERIFY_READ
, arg2
, arg3
, 1)))
345 ret
= get_errno(write(arg1
, p
, arg3
));
346 unlock_user(p
, arg2
, 0);
348 case TARGET_FREEBSD_NR_writev
:
353 vec
= alloca(count
* sizeof(struct iovec
));
354 if (lock_iovec(VERIFY_READ
, vec
, arg2
, count
, 1) < 0)
356 ret
= get_errno(writev(arg1
, vec
, count
));
357 unlock_iovec(vec
, arg2
, count
, 0);
360 case TARGET_FREEBSD_NR_open
:
361 if (!(p
= lock_user_string(arg1
)))
363 ret
= get_errno(open(path(p
),
364 target_to_host_bitmask(arg2
, fcntl_flags_tbl
),
366 unlock_user(p
, arg1
, 0);
368 case TARGET_FREEBSD_NR_mmap
:
369 ret
= get_errno(target_mmap(arg1
, arg2
, arg3
,
370 target_to_host_bitmask(arg4
, mmap_flags_tbl
),
374 case TARGET_FREEBSD_NR_mprotect
:
375 ret
= get_errno(target_mprotect(arg1
, arg2
, arg3
));
377 case TARGET_FREEBSD_NR_break
:
378 ret
= do_obreak(arg1
);
381 case TARGET_FREEBSD_NR___sysctl
:
382 ret
= do_freebsd_sysctl(arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
385 case TARGET_FREEBSD_NR_sysarch
:
386 ret
= do_freebsd_sysarch(cpu_env
, arg1
, arg2
);
388 case TARGET_FREEBSD_NR_syscall
:
389 case TARGET_FREEBSD_NR___syscall
:
390 ret
= do_freebsd_syscall(cpu_env
,arg1
& 0xffff,arg2
,arg3
,arg4
,arg5
,arg6
,arg7
,arg8
,0);
393 ret
= get_errno(syscall(num
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
, arg7
, arg8
));
398 gemu_log(" = %ld\n", ret
);
401 print_freebsd_syscall_ret(num
, ret
);
404 ret
= -TARGET_EFAULT
;
408 abi_long
do_netbsd_syscall(void *cpu_env
, int num
, abi_long arg1
,
409 abi_long arg2
, abi_long arg3
, abi_long arg4
,
410 abi_long arg5
, abi_long arg6
)
416 gemu_log("netbsd syscall %d\n", num
);
419 print_netbsd_syscall(num
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
422 case TARGET_NETBSD_NR_exit
:
426 gdb_exit(cpu_env
, arg1
);
427 /* XXX: should free thread stack and CPU env */
429 ret
= 0; /* avoid warning */
431 case TARGET_NETBSD_NR_read
:
432 if (!(p
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0)))
434 ret
= get_errno(read(arg1
, p
, arg3
));
435 unlock_user(p
, arg2
, ret
);
437 case TARGET_NETBSD_NR_write
:
438 if (!(p
= lock_user(VERIFY_READ
, arg2
, arg3
, 1)))
440 ret
= get_errno(write(arg1
, p
, arg3
));
441 unlock_user(p
, arg2
, 0);
443 case TARGET_NETBSD_NR_open
:
444 if (!(p
= lock_user_string(arg1
)))
446 ret
= get_errno(open(path(p
),
447 target_to_host_bitmask(arg2
, fcntl_flags_tbl
),
449 unlock_user(p
, arg1
, 0);
451 case TARGET_NETBSD_NR_mmap
:
452 ret
= get_errno(target_mmap(arg1
, arg2
, arg3
,
453 target_to_host_bitmask(arg4
, mmap_flags_tbl
),
457 case TARGET_NETBSD_NR_mprotect
:
458 ret
= get_errno(target_mprotect(arg1
, arg2
, arg3
));
460 case TARGET_NETBSD_NR_syscall
:
461 case TARGET_NETBSD_NR___syscall
:
462 ret
= do_netbsd_syscall(cpu_env
,arg1
& 0xffff,arg2
,arg3
,arg4
,arg5
,arg6
,0);
465 ret
= syscall(num
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
470 gemu_log(" = %ld\n", ret
);
473 print_netbsd_syscall_ret(num
, ret
);
476 ret
= -TARGET_EFAULT
;
480 abi_long
do_openbsd_syscall(void *cpu_env
, int num
, abi_long arg1
,
481 abi_long arg2
, abi_long arg3
, abi_long arg4
,
482 abi_long arg5
, abi_long arg6
)
488 gemu_log("openbsd syscall %d\n", num
);
491 print_openbsd_syscall(num
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
494 case TARGET_OPENBSD_NR_exit
:
498 gdb_exit(cpu_env
, arg1
);
499 /* XXX: should free thread stack and CPU env */
501 ret
= 0; /* avoid warning */
503 case TARGET_OPENBSD_NR_read
:
504 if (!(p
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0)))
506 ret
= get_errno(read(arg1
, p
, arg3
));
507 unlock_user(p
, arg2
, ret
);
509 case TARGET_OPENBSD_NR_write
:
510 if (!(p
= lock_user(VERIFY_READ
, arg2
, arg3
, 1)))
512 ret
= get_errno(write(arg1
, p
, arg3
));
513 unlock_user(p
, arg2
, 0);
515 case TARGET_OPENBSD_NR_open
:
516 if (!(p
= lock_user_string(arg1
)))
518 ret
= get_errno(open(path(p
),
519 target_to_host_bitmask(arg2
, fcntl_flags_tbl
),
521 unlock_user(p
, arg1
, 0);
523 case TARGET_OPENBSD_NR_mmap
:
524 ret
= get_errno(target_mmap(arg1
, arg2
, arg3
,
525 target_to_host_bitmask(arg4
, mmap_flags_tbl
),
529 case TARGET_OPENBSD_NR_mprotect
:
530 ret
= get_errno(target_mprotect(arg1
, arg2
, arg3
));
532 case TARGET_OPENBSD_NR_syscall
:
533 case TARGET_OPENBSD_NR___syscall
:
534 ret
= do_openbsd_syscall(cpu_env
,arg1
& 0xffff,arg2
,arg3
,arg4
,arg5
,arg6
,0);
537 ret
= syscall(num
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
542 gemu_log(" = %ld\n", ret
);
545 print_openbsd_syscall_ret(num
, ret
);
548 ret
= -TARGET_EFAULT
;
552 void syscall_init(void)