Merge remote-tracking branch 'remotes/cody/tags/block-pull-request' into staging
[qemu/rayw.git] / bsd-user / syscall.c
blob35f784cb6cd65f38ccf9c3499877456e484c4104
1 /*
2 * BSD syscalls
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 <sys/mman.h>
21 #include <sys/syscall.h>
22 #include <sys/param.h>
23 #include <sys/sysctl.h>
24 #include <utime.h>
26 #include "qemu.h"
27 #include "qemu-common.h"
29 //#define DEBUG
31 static abi_ulong target_brk;
32 static abi_ulong target_original_brk;
34 static inline abi_long get_errno(abi_long ret)
36 if (ret == -1)
37 /* XXX need to translate host -> target errnos here */
38 return -(errno);
39 else
40 return ret;
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)
58 abi_ulong brk_page;
59 abi_long mapped_addr;
60 int new_alloc_size;
62 if (!new_brk)
63 return 0;
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) {
71 target_brk = new_brk;
72 return 0;
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,
78 PROT_READ|PROT_WRITE,
79 MAP_ANON|MAP_FIXED|MAP_PRIVATE, -1, 0));
81 if (!is_error(mapped_addr))
82 target_brk = new_brk;
83 else
84 return mapped_addr;
86 return 0;
89 #if defined(TARGET_I386)
90 static abi_long do_freebsd_sysarch(CPUX86State *env, int op, abi_ulong parms)
92 abi_long ret = 0;
93 abi_ulong val;
94 int idx;
96 switch(op) {
97 #ifdef TARGET_ABI32
98 case TARGET_FREEBSD_I386_SET_GSBASE:
99 case TARGET_FREEBSD_I386_SET_FSBASE:
100 if (op == TARGET_FREEBSD_I386_SET_GSBASE)
101 #else
102 case TARGET_FREEBSD_AMD64_SET_GSBASE:
103 case TARGET_FREEBSD_AMD64_SET_FSBASE:
104 if (op == TARGET_FREEBSD_AMD64_SET_GSBASE)
105 #endif
106 idx = R_GS;
107 else
108 idx = R_FS;
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;
113 break;
114 #ifdef TARGET_ABI32
115 case TARGET_FREEBSD_I386_GET_GSBASE:
116 case TARGET_FREEBSD_I386_GET_FSBASE:
117 if (op == TARGET_FREEBSD_I386_GET_GSBASE)
118 #else
119 case TARGET_FREEBSD_AMD64_GET_GSBASE:
120 case TARGET_FREEBSD_AMD64_GET_FSBASE:
121 if (op == TARGET_FREEBSD_AMD64_GET_GSBASE)
122 #endif
123 idx = R_GS;
124 else
125 idx = R_FS;
126 val = env->segs[idx].base;
127 if (put_user(val, parms, abi_ulong))
128 return -TARGET_EFAULT;
129 break;
130 /* XXX handle the others... */
131 default:
132 ret = -TARGET_EINVAL;
133 break;
135 return ret;
137 #endif
139 #ifdef TARGET_SPARC
140 static abi_long do_freebsd_sysarch(void *env, int op, abi_ulong parms)
142 /* XXX handle
143 * TARGET_FREEBSD_SPARC_UTRAP_INSTALL,
144 * TARGET_FREEBSD_SPARC_SIGTRAMP_INSTALL
146 return -TARGET_EINVAL;
148 #endif
150 #ifdef __FreeBSD__
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)
156 static int
157 oidfmt(int *oid, int len, char *fmt, uint32_t *kind)
159 int qoid[CTL_MAXNAME+2];
160 uint8_t buf[BUFSIZ];
161 int i;
162 size_t j;
164 qoid[0] = 0;
165 qoid[1] = 4;
166 memcpy(qoid + 2, oid, len * sizeof(int));
168 j = sizeof(buf);
169 i = sysctl(qoid, len + 2, buf, &j, 0, 0);
170 if (i)
171 return i;
173 if (kind)
174 *kind = *(uint32_t *)buf;
176 if (fmt)
177 strcpy(fmt, (char *)(buf + sizeof(uint32_t)));
178 return (0);
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) {
188 case CTLTYPE_INT:
189 case CTLTYPE_UINT:
190 *(uint32_t *)holdp = tswap32(*(uint32_t *)holdp);
191 break;
192 #ifdef TARGET_ABI32
193 case CTLTYPE_LONG:
194 case CTLTYPE_ULONG:
195 *(uint32_t *)holdp = tswap32(*(long *)holdp);
196 break;
197 #else
198 case CTLTYPE_LONG:
199 *(uint64_t *)holdp = tswap64(*(long *)holdp);
200 case CTLTYPE_ULONG:
201 *(uint64_t *)holdp = tswap64(*(unsigned long *)holdp);
202 break;
203 #endif
204 #ifdef CTLTYPE_U64
205 case CTLTYPE_S64:
206 case CTLTYPE_U64:
207 #else
208 case CTLTYPE_QUAD:
209 #endif
210 *(uint64_t *)holdp = tswap64(*(uint64_t *)holdp);
211 break;
212 case CTLTYPE_STRING:
213 break;
214 default:
215 /* XXX unhandled */
216 return -1;
218 return 0;
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)
225 abi_long ret;
226 void *hnamep, *holdp, *hnewp = NULL;
227 size_t holdlen;
228 abi_ulong oldlen = 0;
229 int32_t *snamep = g_malloc(sizeof(int32_t) * namelen), *p, *q, i;
230 uint32_t kind = 0;
232 if (oldlenp)
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;
240 holdlen = oldlen;
241 for (p = hnamep, q = snamep, i = 0; i < namelen; p++, i++)
242 *q++ = tswap32(*p);
243 oidfmt(snamep, namelen, NULL, &kind);
244 /* XXX swap hnewp */
245 ret = get_errno(sysctl(snamep, namelen, holdp, &holdlen, hnewp, newlen));
246 if (!ret)
247 sysctl_oldcvt(holdp, holdlen, kind);
248 put_user_ual(holdlen, oldlenp);
249 unlock_user(hnamep, namep, 0);
250 unlock_user(holdp, oldp, holdlen);
251 if (hnewp)
252 unlock_user(hnewp, newp, 0);
253 g_free(snamep);
254 return ret;
256 #endif
258 /* FIXME
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,
263 int count, int copy)
265 struct target_iovec *target_vec;
266 abi_ulong base;
267 int i;
269 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
270 if (!target_vec)
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. */
279 } else {
280 /* zero length pointer is ignored */
281 vec[i].iov_base = NULL;
284 unlock_user (target_vec, target_addr, 0);
285 return 0;
288 static abi_long unlock_iovec(struct iovec *vec, abi_ulong target_addr,
289 int count, int copy)
291 struct target_iovec *target_vec;
292 abi_ulong base;
293 int i;
295 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
296 if (!target_vec)
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);
306 return 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,
315 abi_long arg8)
317 abi_long ret;
318 void *p;
320 #ifdef DEBUG
321 gemu_log("freebsd syscall %d\n", num);
322 #endif
323 if(do_strace)
324 print_freebsd_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
326 switch(num) {
327 case TARGET_FREEBSD_NR_exit:
328 #ifdef TARGET_GPROF
329 _mcleanup();
330 #endif
331 gdb_exit(cpu_env, arg1);
332 /* XXX: should free thread stack and CPU env */
333 _exit(arg1);
334 ret = 0; /* avoid warning */
335 break;
336 case TARGET_FREEBSD_NR_read:
337 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
338 goto efault;
339 ret = get_errno(read(arg1, p, arg3));
340 unlock_user(p, arg2, ret);
341 break;
342 case TARGET_FREEBSD_NR_write:
343 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
344 goto efault;
345 ret = get_errno(write(arg1, p, arg3));
346 unlock_user(p, arg2, 0);
347 break;
348 case TARGET_FREEBSD_NR_writev:
350 int count = arg3;
351 struct iovec *vec;
353 vec = alloca(count * sizeof(struct iovec));
354 if (lock_iovec(VERIFY_READ, vec, arg2, count, 1) < 0)
355 goto efault;
356 ret = get_errno(writev(arg1, vec, count));
357 unlock_iovec(vec, arg2, count, 0);
359 break;
360 case TARGET_FREEBSD_NR_open:
361 if (!(p = lock_user_string(arg1)))
362 goto efault;
363 ret = get_errno(open(path(p),
364 target_to_host_bitmask(arg2, fcntl_flags_tbl),
365 arg3));
366 unlock_user(p, arg1, 0);
367 break;
368 case TARGET_FREEBSD_NR_mmap:
369 ret = get_errno(target_mmap(arg1, arg2, arg3,
370 target_to_host_bitmask(arg4, mmap_flags_tbl),
371 arg5,
372 arg6));
373 break;
374 case TARGET_FREEBSD_NR_mprotect:
375 ret = get_errno(target_mprotect(arg1, arg2, arg3));
376 break;
377 case TARGET_FREEBSD_NR_break:
378 ret = do_obreak(arg1);
379 break;
380 #ifdef __FreeBSD__
381 case TARGET_FREEBSD_NR___sysctl:
382 ret = do_freebsd_sysctl(arg1, arg2, arg3, arg4, arg5, arg6);
383 break;
384 #endif
385 case TARGET_FREEBSD_NR_sysarch:
386 ret = do_freebsd_sysarch(cpu_env, arg1, arg2);
387 break;
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);
391 break;
392 default:
393 ret = get_errno(syscall(num, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8));
394 break;
396 fail:
397 #ifdef DEBUG
398 gemu_log(" = %ld\n", ret);
399 #endif
400 if (do_strace)
401 print_freebsd_syscall_ret(num, ret);
402 return ret;
403 efault:
404 ret = -TARGET_EFAULT;
405 goto fail;
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)
412 abi_long ret;
413 void *p;
415 #ifdef DEBUG
416 gemu_log("netbsd syscall %d\n", num);
417 #endif
418 if(do_strace)
419 print_netbsd_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
421 switch(num) {
422 case TARGET_NETBSD_NR_exit:
423 #ifdef TARGET_GPROF
424 _mcleanup();
425 #endif
426 gdb_exit(cpu_env, arg1);
427 /* XXX: should free thread stack and CPU env */
428 _exit(arg1);
429 ret = 0; /* avoid warning */
430 break;
431 case TARGET_NETBSD_NR_read:
432 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
433 goto efault;
434 ret = get_errno(read(arg1, p, arg3));
435 unlock_user(p, arg2, ret);
436 break;
437 case TARGET_NETBSD_NR_write:
438 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
439 goto efault;
440 ret = get_errno(write(arg1, p, arg3));
441 unlock_user(p, arg2, 0);
442 break;
443 case TARGET_NETBSD_NR_open:
444 if (!(p = lock_user_string(arg1)))
445 goto efault;
446 ret = get_errno(open(path(p),
447 target_to_host_bitmask(arg2, fcntl_flags_tbl),
448 arg3));
449 unlock_user(p, arg1, 0);
450 break;
451 case TARGET_NETBSD_NR_mmap:
452 ret = get_errno(target_mmap(arg1, arg2, arg3,
453 target_to_host_bitmask(arg4, mmap_flags_tbl),
454 arg5,
455 arg6));
456 break;
457 case TARGET_NETBSD_NR_mprotect:
458 ret = get_errno(target_mprotect(arg1, arg2, arg3));
459 break;
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);
463 break;
464 default:
465 ret = syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
466 break;
468 fail:
469 #ifdef DEBUG
470 gemu_log(" = %ld\n", ret);
471 #endif
472 if (do_strace)
473 print_netbsd_syscall_ret(num, ret);
474 return ret;
475 efault:
476 ret = -TARGET_EFAULT;
477 goto fail;
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)
484 abi_long ret;
485 void *p;
487 #ifdef DEBUG
488 gemu_log("openbsd syscall %d\n", num);
489 #endif
490 if(do_strace)
491 print_openbsd_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
493 switch(num) {
494 case TARGET_OPENBSD_NR_exit:
495 #ifdef TARGET_GPROF
496 _mcleanup();
497 #endif
498 gdb_exit(cpu_env, arg1);
499 /* XXX: should free thread stack and CPU env */
500 _exit(arg1);
501 ret = 0; /* avoid warning */
502 break;
503 case TARGET_OPENBSD_NR_read:
504 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
505 goto efault;
506 ret = get_errno(read(arg1, p, arg3));
507 unlock_user(p, arg2, ret);
508 break;
509 case TARGET_OPENBSD_NR_write:
510 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
511 goto efault;
512 ret = get_errno(write(arg1, p, arg3));
513 unlock_user(p, arg2, 0);
514 break;
515 case TARGET_OPENBSD_NR_open:
516 if (!(p = lock_user_string(arg1)))
517 goto efault;
518 ret = get_errno(open(path(p),
519 target_to_host_bitmask(arg2, fcntl_flags_tbl),
520 arg3));
521 unlock_user(p, arg1, 0);
522 break;
523 case TARGET_OPENBSD_NR_mmap:
524 ret = get_errno(target_mmap(arg1, arg2, arg3,
525 target_to_host_bitmask(arg4, mmap_flags_tbl),
526 arg5,
527 arg6));
528 break;
529 case TARGET_OPENBSD_NR_mprotect:
530 ret = get_errno(target_mprotect(arg1, arg2, arg3));
531 break;
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);
535 break;
536 default:
537 ret = syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
538 break;
540 fail:
541 #ifdef DEBUG
542 gemu_log(" = %ld\n", ret);
543 #endif
544 if (do_strace)
545 print_openbsd_syscall_ret(num, ret);
546 return ret;
547 efault:
548 ret = -TARGET_EFAULT;
549 goto fail;
552 void syscall_init(void)