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/>.
29 #include <sys/types.h>
31 #include <sys/syscall.h>
32 #include <sys/param.h>
33 #include <sys/sysctl.h>
37 #include "qemu-common.h"
41 static abi_ulong target_brk
;
42 static abi_ulong target_original_brk
;
44 static inline abi_long
get_errno(abi_long ret
)
47 /* XXX need to translate host -> target errnos here */
53 #define target_to_host_bitmask(x, tbl) (x)
55 static inline int is_error(abi_long ret
)
57 return (abi_ulong
)ret
>= (abi_ulong
)(-4096);
60 void target_set_brk(abi_ulong new_brk
)
62 target_original_brk
= target_brk
= HOST_PAGE_ALIGN(new_brk
);
65 /* do_obreak() must return target errnos. */
66 static abi_long
do_obreak(abi_ulong new_brk
)
74 if (new_brk
< target_original_brk
)
75 return -TARGET_EINVAL
;
77 brk_page
= HOST_PAGE_ALIGN(target_brk
);
79 /* If the new brk is less than this, set it and we're done... */
80 if (new_brk
< brk_page
) {
85 /* We need to allocate more memory after the brk... */
86 new_alloc_size
= HOST_PAGE_ALIGN(new_brk
- brk_page
+ 1);
87 mapped_addr
= get_errno(target_mmap(brk_page
, new_alloc_size
,
89 MAP_ANON
|MAP_FIXED
|MAP_PRIVATE
, -1, 0));
91 if (!is_error(mapped_addr
))
99 #if defined(TARGET_I386)
100 static abi_long
do_freebsd_sysarch(CPUX86State
*env
, int op
, abi_ulong parms
)
108 case TARGET_FREEBSD_I386_SET_GSBASE
:
109 case TARGET_FREEBSD_I386_SET_FSBASE
:
110 if (op
== TARGET_FREEBSD_I386_SET_GSBASE
)
112 case TARGET_FREEBSD_AMD64_SET_GSBASE
:
113 case TARGET_FREEBSD_AMD64_SET_FSBASE
:
114 if (op
== TARGET_FREEBSD_AMD64_SET_GSBASE
)
119 if (get_user(val
, parms
, abi_ulong
))
120 return -TARGET_EFAULT
;
121 cpu_x86_load_seg(env
, idx
, 0);
122 env
->segs
[idx
].base
= val
;
125 case TARGET_FREEBSD_I386_GET_GSBASE
:
126 case TARGET_FREEBSD_I386_GET_FSBASE
:
127 if (op
== TARGET_FREEBSD_I386_GET_GSBASE
)
129 case TARGET_FREEBSD_AMD64_GET_GSBASE
:
130 case TARGET_FREEBSD_AMD64_GET_FSBASE
:
131 if (op
== TARGET_FREEBSD_AMD64_GET_GSBASE
)
136 val
= env
->segs
[idx
].base
;
137 if (put_user(val
, parms
, abi_ulong
))
138 return -TARGET_EFAULT
;
140 /* XXX handle the others... */
142 ret
= -TARGET_EINVAL
;
150 static abi_long
do_freebsd_sysarch(void *env
, int op
, abi_ulong parms
)
153 * TARGET_FREEBSD_SPARC_UTRAP_INSTALL,
154 * TARGET_FREEBSD_SPARC_SIGTRAMP_INSTALL
156 return -TARGET_EINVAL
;
162 * XXX this uses the undocumented oidfmt interface to find the kind of
163 * a requested sysctl, see /sys/kern/kern_sysctl.c:sysctl_sysctl_oidfmt()
164 * (this is mostly copied from src/sbin/sysctl/sysctl.c)
167 oidfmt(int *oid
, int len
, char *fmt
, uint32_t *kind
)
169 int qoid
[CTL_MAXNAME
+2];
176 memcpy(qoid
+ 2, oid
, len
* sizeof(int));
179 i
= sysctl(qoid
, len
+ 2, buf
, &j
, 0, 0);
184 *kind
= *(uint32_t *)buf
;
187 strcpy(fmt
, (char *)(buf
+ sizeof(uint32_t)));
192 * try and convert sysctl return data for the target.
193 * XXX doesn't handle CTLTYPE_OPAQUE and CTLTYPE_STRUCT.
195 static int sysctl_oldcvt(void *holdp
, size_t holdlen
, uint32_t kind
)
197 switch (kind
& CTLTYPE
) {
200 *(uint32_t *)holdp
= tswap32(*(uint32_t *)holdp
);
205 *(uint32_t *)holdp
= tswap32(*(long *)holdp
);
209 *(uint64_t *)holdp
= tswap64(*(long *)holdp
);
211 *(uint64_t *)holdp
= tswap64(*(unsigned long *)holdp
);
220 *(uint64_t *)holdp
= tswap64(*(uint64_t *)holdp
);
231 /* XXX this needs to be emulated on non-FreeBSD hosts... */
232 static abi_long
do_freebsd_sysctl(abi_ulong namep
, int32_t namelen
, abi_ulong oldp
,
233 abi_ulong oldlenp
, abi_ulong newp
, abi_ulong newlen
)
236 void *hnamep
, *holdp
, *hnewp
= NULL
;
238 abi_ulong oldlen
= 0;
239 int32_t *snamep
= g_malloc(sizeof(int32_t) * namelen
), *p
, *q
, i
;
243 get_user_ual(oldlen
, oldlenp
);
244 if (!(hnamep
= lock_user(VERIFY_READ
, namep
, namelen
, 1)))
245 return -TARGET_EFAULT
;
246 if (newp
&& !(hnewp
= lock_user(VERIFY_READ
, newp
, newlen
, 1)))
247 return -TARGET_EFAULT
;
248 if (!(holdp
= lock_user(VERIFY_WRITE
, oldp
, oldlen
, 0)))
249 return -TARGET_EFAULT
;
251 for (p
= hnamep
, q
= snamep
, i
= 0; i
< namelen
; p
++, i
++)
253 oidfmt(snamep
, namelen
, NULL
, &kind
);
255 ret
= get_errno(sysctl(snamep
, namelen
, holdp
, &holdlen
, hnewp
, newlen
));
257 sysctl_oldcvt(holdp
, holdlen
, kind
);
258 put_user_ual(holdlen
, oldlenp
);
259 unlock_user(hnamep
, namep
, 0);
260 unlock_user(holdp
, oldp
, holdlen
);
262 unlock_user(hnewp
, newp
, 0);
269 * lock_iovec()/unlock_iovec() have a return code of 0 for success where
270 * other lock functions have a return code of 0 for failure.
272 static abi_long
lock_iovec(int type
, struct iovec
*vec
, abi_ulong target_addr
,
275 struct target_iovec
*target_vec
;
279 target_vec
= lock_user(VERIFY_READ
, target_addr
, count
* sizeof(struct target_iovec
), 1);
281 return -TARGET_EFAULT
;
282 for(i
= 0;i
< count
; i
++) {
283 base
= tswapl(target_vec
[i
].iov_base
);
284 vec
[i
].iov_len
= tswapl(target_vec
[i
].iov_len
);
285 if (vec
[i
].iov_len
!= 0) {
286 vec
[i
].iov_base
= lock_user(type
, base
, vec
[i
].iov_len
, copy
);
287 /* Don't check lock_user return value. We must call writev even
288 if a element has invalid base address. */
290 /* zero length pointer is ignored */
291 vec
[i
].iov_base
= NULL
;
294 unlock_user (target_vec
, target_addr
, 0);
298 static abi_long
unlock_iovec(struct iovec
*vec
, abi_ulong target_addr
,
301 struct target_iovec
*target_vec
;
305 target_vec
= lock_user(VERIFY_READ
, target_addr
, count
* sizeof(struct target_iovec
), 1);
307 return -TARGET_EFAULT
;
308 for(i
= 0;i
< count
; i
++) {
309 if (target_vec
[i
].iov_base
) {
310 base
= tswapl(target_vec
[i
].iov_base
);
311 unlock_user(vec
[i
].iov_base
, base
, copy
? vec
[i
].iov_len
: 0);
314 unlock_user (target_vec
, target_addr
, 0);
319 /* do_syscall() should always have a single exit point at the end so
320 that actions, such as logging of syscall results, can be performed.
321 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
322 abi_long
do_freebsd_syscall(void *cpu_env
, int num
, abi_long arg1
,
323 abi_long arg2
, abi_long arg3
, abi_long arg4
,
324 abi_long arg5
, abi_long arg6
, abi_long arg7
,
331 gemu_log("freebsd syscall %d\n", num
);
334 print_freebsd_syscall(num
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
337 case TARGET_FREEBSD_NR_exit
:
341 gdb_exit(cpu_env
, arg1
);
342 /* XXX: should free thread stack and CPU env */
344 ret
= 0; /* avoid warning */
346 case TARGET_FREEBSD_NR_read
:
347 if (!(p
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0)))
349 ret
= get_errno(read(arg1
, p
, arg3
));
350 unlock_user(p
, arg2
, ret
);
352 case TARGET_FREEBSD_NR_write
:
353 if (!(p
= lock_user(VERIFY_READ
, arg2
, arg3
, 1)))
355 ret
= get_errno(write(arg1
, p
, arg3
));
356 unlock_user(p
, arg2
, 0);
358 case TARGET_FREEBSD_NR_writev
:
363 vec
= alloca(count
* sizeof(struct iovec
));
364 if (lock_iovec(VERIFY_READ
, vec
, arg2
, count
, 1) < 0)
366 ret
= get_errno(writev(arg1
, vec
, count
));
367 unlock_iovec(vec
, arg2
, count
, 0);
370 case TARGET_FREEBSD_NR_open
:
371 if (!(p
= lock_user_string(arg1
)))
373 ret
= get_errno(open(path(p
),
374 target_to_host_bitmask(arg2
, fcntl_flags_tbl
),
376 unlock_user(p
, arg1
, 0);
378 case TARGET_FREEBSD_NR_mmap
:
379 ret
= get_errno(target_mmap(arg1
, arg2
, arg3
,
380 target_to_host_bitmask(arg4
, mmap_flags_tbl
),
384 case TARGET_FREEBSD_NR_mprotect
:
385 ret
= get_errno(target_mprotect(arg1
, arg2
, arg3
));
387 case TARGET_FREEBSD_NR_break
:
388 ret
= do_obreak(arg1
);
391 case TARGET_FREEBSD_NR___sysctl
:
392 ret
= do_freebsd_sysctl(arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
395 case TARGET_FREEBSD_NR_sysarch
:
396 ret
= do_freebsd_sysarch(cpu_env
, arg1
, arg2
);
398 case TARGET_FREEBSD_NR_syscall
:
399 case TARGET_FREEBSD_NR___syscall
:
400 ret
= do_freebsd_syscall(cpu_env
,arg1
& 0xffff,arg2
,arg3
,arg4
,arg5
,arg6
,arg7
,arg8
,0);
403 ret
= get_errno(syscall(num
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
, arg7
, arg8
));
408 gemu_log(" = %ld\n", ret
);
411 print_freebsd_syscall_ret(num
, ret
);
414 ret
= -TARGET_EFAULT
;
418 abi_long
do_netbsd_syscall(void *cpu_env
, int num
, abi_long arg1
,
419 abi_long arg2
, abi_long arg3
, abi_long arg4
,
420 abi_long arg5
, abi_long arg6
)
426 gemu_log("netbsd syscall %d\n", num
);
429 print_netbsd_syscall(num
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
432 case TARGET_NETBSD_NR_exit
:
436 gdb_exit(cpu_env
, arg1
);
437 /* XXX: should free thread stack and CPU env */
439 ret
= 0; /* avoid warning */
441 case TARGET_NETBSD_NR_read
:
442 if (!(p
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0)))
444 ret
= get_errno(read(arg1
, p
, arg3
));
445 unlock_user(p
, arg2
, ret
);
447 case TARGET_NETBSD_NR_write
:
448 if (!(p
= lock_user(VERIFY_READ
, arg2
, arg3
, 1)))
450 ret
= get_errno(write(arg1
, p
, arg3
));
451 unlock_user(p
, arg2
, 0);
453 case TARGET_NETBSD_NR_open
:
454 if (!(p
= lock_user_string(arg1
)))
456 ret
= get_errno(open(path(p
),
457 target_to_host_bitmask(arg2
, fcntl_flags_tbl
),
459 unlock_user(p
, arg1
, 0);
461 case TARGET_NETBSD_NR_mmap
:
462 ret
= get_errno(target_mmap(arg1
, arg2
, arg3
,
463 target_to_host_bitmask(arg4
, mmap_flags_tbl
),
467 case TARGET_NETBSD_NR_mprotect
:
468 ret
= get_errno(target_mprotect(arg1
, arg2
, arg3
));
470 case TARGET_NETBSD_NR_syscall
:
471 case TARGET_NETBSD_NR___syscall
:
472 ret
= do_netbsd_syscall(cpu_env
,arg1
& 0xffff,arg2
,arg3
,arg4
,arg5
,arg6
,0);
475 ret
= syscall(num
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
480 gemu_log(" = %ld\n", ret
);
483 print_netbsd_syscall_ret(num
, ret
);
486 ret
= -TARGET_EFAULT
;
490 abi_long
do_openbsd_syscall(void *cpu_env
, int num
, abi_long arg1
,
491 abi_long arg2
, abi_long arg3
, abi_long arg4
,
492 abi_long arg5
, abi_long arg6
)
498 gemu_log("openbsd syscall %d\n", num
);
501 print_openbsd_syscall(num
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
504 case TARGET_OPENBSD_NR_exit
:
508 gdb_exit(cpu_env
, arg1
);
509 /* XXX: should free thread stack and CPU env */
511 ret
= 0; /* avoid warning */
513 case TARGET_OPENBSD_NR_read
:
514 if (!(p
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0)))
516 ret
= get_errno(read(arg1
, p
, arg3
));
517 unlock_user(p
, arg2
, ret
);
519 case TARGET_OPENBSD_NR_write
:
520 if (!(p
= lock_user(VERIFY_READ
, arg2
, arg3
, 1)))
522 ret
= get_errno(write(arg1
, p
, arg3
));
523 unlock_user(p
, arg2
, 0);
525 case TARGET_OPENBSD_NR_open
:
526 if (!(p
= lock_user_string(arg1
)))
528 ret
= get_errno(open(path(p
),
529 target_to_host_bitmask(arg2
, fcntl_flags_tbl
),
531 unlock_user(p
, arg1
, 0);
533 case TARGET_OPENBSD_NR_mmap
:
534 ret
= get_errno(target_mmap(arg1
, arg2
, arg3
,
535 target_to_host_bitmask(arg4
, mmap_flags_tbl
),
539 case TARGET_OPENBSD_NR_mprotect
:
540 ret
= get_errno(target_mprotect(arg1
, arg2
, arg3
));
542 case TARGET_OPENBSD_NR_syscall
:
543 case TARGET_OPENBSD_NR___syscall
:
544 ret
= do_openbsd_syscall(cpu_env
,arg1
& 0xffff,arg2
,arg3
,arg4
,arg5
,arg6
,0);
547 ret
= syscall(num
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
552 gemu_log(" = %ld\n", ret
);
555 print_openbsd_syscall_ret(num
, ret
);
558 ret
= -TARGET_EFAULT
;
562 void syscall_init(void)