pvpanic: pass configurable ioport to seabios
[qemu/ar7.git] / bsd-user / syscall.c
blob69e3466a086f44ce657a64559e16fdea4c678583
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 <stdlib.h>
20 #include <stdio.h>
21 #include <stdint.h>
22 #include <stdarg.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <time.h>
28 #include <limits.h>
29 #include <sys/types.h>
30 #include <sys/mman.h>
31 #include <sys/syscall.h>
32 #include <sys/param.h>
33 #include <sys/sysctl.h>
34 #include <utime.h>
36 #include "qemu.h"
37 #include "qemu-common.h"
39 //#define DEBUG
41 static abi_ulong target_brk;
42 static abi_ulong target_original_brk;
44 static inline abi_long get_errno(abi_long ret)
46 if (ret == -1)
47 /* XXX need to translate host -> target errnos here */
48 return -(errno);
49 else
50 return ret;
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)
68 abi_ulong brk_page;
69 abi_long mapped_addr;
70 int new_alloc_size;
72 if (!new_brk)
73 return 0;
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) {
81 target_brk = new_brk;
82 return 0;
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,
88 PROT_READ|PROT_WRITE,
89 MAP_ANON|MAP_FIXED|MAP_PRIVATE, -1, 0));
91 if (!is_error(mapped_addr))
92 target_brk = new_brk;
93 else
94 return mapped_addr;
96 return 0;
99 #if defined(TARGET_I386)
100 static abi_long do_freebsd_sysarch(CPUX86State *env, int op, abi_ulong parms)
102 abi_long ret = 0;
103 abi_ulong val;
104 int idx;
106 switch(op) {
107 #ifdef TARGET_ABI32
108 case TARGET_FREEBSD_I386_SET_GSBASE:
109 case TARGET_FREEBSD_I386_SET_FSBASE:
110 if (op == TARGET_FREEBSD_I386_SET_GSBASE)
111 #else
112 case TARGET_FREEBSD_AMD64_SET_GSBASE:
113 case TARGET_FREEBSD_AMD64_SET_FSBASE:
114 if (op == TARGET_FREEBSD_AMD64_SET_GSBASE)
115 #endif
116 idx = R_GS;
117 else
118 idx = R_FS;
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;
123 break;
124 #ifdef TARGET_ABI32
125 case TARGET_FREEBSD_I386_GET_GSBASE:
126 case TARGET_FREEBSD_I386_GET_FSBASE:
127 if (op == TARGET_FREEBSD_I386_GET_GSBASE)
128 #else
129 case TARGET_FREEBSD_AMD64_GET_GSBASE:
130 case TARGET_FREEBSD_AMD64_GET_FSBASE:
131 if (op == TARGET_FREEBSD_AMD64_GET_GSBASE)
132 #endif
133 idx = R_GS;
134 else
135 idx = R_FS;
136 val = env->segs[idx].base;
137 if (put_user(val, parms, abi_ulong))
138 return -TARGET_EFAULT;
139 break;
140 /* XXX handle the others... */
141 default:
142 ret = -TARGET_EINVAL;
143 break;
145 return ret;
147 #endif
149 #ifdef TARGET_SPARC
150 static abi_long do_freebsd_sysarch(void *env, int op, abi_ulong parms)
152 /* XXX handle
153 * TARGET_FREEBSD_SPARC_UTRAP_INSTALL,
154 * TARGET_FREEBSD_SPARC_SIGTRAMP_INSTALL
156 return -TARGET_EINVAL;
158 #endif
160 #ifdef __FreeBSD__
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)
166 static int
167 oidfmt(int *oid, int len, char *fmt, uint32_t *kind)
169 int qoid[CTL_MAXNAME+2];
170 uint8_t buf[BUFSIZ];
171 int i;
172 size_t j;
174 qoid[0] = 0;
175 qoid[1] = 4;
176 memcpy(qoid + 2, oid, len * sizeof(int));
178 j = sizeof(buf);
179 i = sysctl(qoid, len + 2, buf, &j, 0, 0);
180 if (i)
181 return i;
183 if (kind)
184 *kind = *(uint32_t *)buf;
186 if (fmt)
187 strcpy(fmt, (char *)(buf + sizeof(uint32_t)));
188 return (0);
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) {
198 case CTLTYPE_INT:
199 case CTLTYPE_UINT:
200 *(uint32_t *)holdp = tswap32(*(uint32_t *)holdp);
201 break;
202 #ifdef TARGET_ABI32
203 case CTLTYPE_LONG:
204 case CTLTYPE_ULONG:
205 *(uint32_t *)holdp = tswap32(*(long *)holdp);
206 break;
207 #else
208 case CTLTYPE_LONG:
209 *(uint64_t *)holdp = tswap64(*(long *)holdp);
210 case CTLTYPE_ULONG:
211 *(uint64_t *)holdp = tswap64(*(unsigned long *)holdp);
212 break;
213 #endif
214 #if !defined(__FreeBSD_version) || __FreeBSD_version < 900031
215 case CTLTYPE_QUAD:
216 #else
217 case CTLTYPE_U64:
218 #endif
219 *(uint64_t *)holdp = tswap64(*(uint64_t *)holdp);
220 break;
221 case CTLTYPE_STRING:
222 break;
223 default:
224 /* XXX unhandled */
225 return -1;
227 return 0;
230 /* XXX this needs to be emulated on non-FreeBSD hosts... */
231 static abi_long do_freebsd_sysctl(abi_ulong namep, int32_t namelen, abi_ulong oldp,
232 abi_ulong oldlenp, abi_ulong newp, abi_ulong newlen)
234 abi_long ret;
235 void *hnamep, *holdp, *hnewp = NULL;
236 size_t holdlen;
237 abi_ulong oldlen = 0;
238 int32_t *snamep = g_malloc(sizeof(int32_t) * namelen), *p, *q, i;
239 uint32_t kind = 0;
241 if (oldlenp)
242 get_user_ual(oldlen, oldlenp);
243 if (!(hnamep = lock_user(VERIFY_READ, namep, namelen, 1)))
244 return -TARGET_EFAULT;
245 if (newp && !(hnewp = lock_user(VERIFY_READ, newp, newlen, 1)))
246 return -TARGET_EFAULT;
247 if (!(holdp = lock_user(VERIFY_WRITE, oldp, oldlen, 0)))
248 return -TARGET_EFAULT;
249 holdlen = oldlen;
250 for (p = hnamep, q = snamep, i = 0; i < namelen; p++, i++)
251 *q++ = tswap32(*p);
252 oidfmt(snamep, namelen, NULL, &kind);
253 /* XXX swap hnewp */
254 ret = get_errno(sysctl(snamep, namelen, holdp, &holdlen, hnewp, newlen));
255 if (!ret)
256 sysctl_oldcvt(holdp, holdlen, kind);
257 put_user_ual(holdlen, oldlenp);
258 unlock_user(hnamep, namep, 0);
259 unlock_user(holdp, oldp, holdlen);
260 if (hnewp)
261 unlock_user(hnewp, newp, 0);
262 g_free(snamep);
263 return ret;
265 #endif
267 /* FIXME
268 * lock_iovec()/unlock_iovec() have a return code of 0 for success where
269 * other lock functions have a return code of 0 for failure.
271 static abi_long lock_iovec(int type, struct iovec *vec, abi_ulong target_addr,
272 int count, int copy)
274 struct target_iovec *target_vec;
275 abi_ulong base;
276 int i;
278 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
279 if (!target_vec)
280 return -TARGET_EFAULT;
281 for(i = 0;i < count; i++) {
282 base = tswapl(target_vec[i].iov_base);
283 vec[i].iov_len = tswapl(target_vec[i].iov_len);
284 if (vec[i].iov_len != 0) {
285 vec[i].iov_base = lock_user(type, base, vec[i].iov_len, copy);
286 /* Don't check lock_user return value. We must call writev even
287 if a element has invalid base address. */
288 } else {
289 /* zero length pointer is ignored */
290 vec[i].iov_base = NULL;
293 unlock_user (target_vec, target_addr, 0);
294 return 0;
297 static abi_long unlock_iovec(struct iovec *vec, abi_ulong target_addr,
298 int count, int copy)
300 struct target_iovec *target_vec;
301 abi_ulong base;
302 int i;
304 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
305 if (!target_vec)
306 return -TARGET_EFAULT;
307 for(i = 0;i < count; i++) {
308 if (target_vec[i].iov_base) {
309 base = tswapl(target_vec[i].iov_base);
310 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
313 unlock_user (target_vec, target_addr, 0);
315 return 0;
318 /* do_syscall() should always have a single exit point at the end so
319 that actions, such as logging of syscall results, can be performed.
320 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
321 abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1,
322 abi_long arg2, abi_long arg3, abi_long arg4,
323 abi_long arg5, abi_long arg6, abi_long arg7,
324 abi_long arg8)
326 abi_long ret;
327 void *p;
329 #ifdef DEBUG
330 gemu_log("freebsd syscall %d\n", num);
331 #endif
332 if(do_strace)
333 print_freebsd_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
335 switch(num) {
336 case TARGET_FREEBSD_NR_exit:
337 #ifdef TARGET_GPROF
338 _mcleanup();
339 #endif
340 gdb_exit(cpu_env, arg1);
341 /* XXX: should free thread stack and CPU env */
342 _exit(arg1);
343 ret = 0; /* avoid warning */
344 break;
345 case TARGET_FREEBSD_NR_read:
346 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
347 goto efault;
348 ret = get_errno(read(arg1, p, arg3));
349 unlock_user(p, arg2, ret);
350 break;
351 case TARGET_FREEBSD_NR_write:
352 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
353 goto efault;
354 ret = get_errno(write(arg1, p, arg3));
355 unlock_user(p, arg2, 0);
356 break;
357 case TARGET_FREEBSD_NR_writev:
359 int count = arg3;
360 struct iovec *vec;
362 vec = alloca(count * sizeof(struct iovec));
363 if (lock_iovec(VERIFY_READ, vec, arg2, count, 1) < 0)
364 goto efault;
365 ret = get_errno(writev(arg1, vec, count));
366 unlock_iovec(vec, arg2, count, 0);
368 break;
369 case TARGET_FREEBSD_NR_open:
370 if (!(p = lock_user_string(arg1)))
371 goto efault;
372 ret = get_errno(open(path(p),
373 target_to_host_bitmask(arg2, fcntl_flags_tbl),
374 arg3));
375 unlock_user(p, arg1, 0);
376 break;
377 case TARGET_FREEBSD_NR_mmap:
378 ret = get_errno(target_mmap(arg1, arg2, arg3,
379 target_to_host_bitmask(arg4, mmap_flags_tbl),
380 arg5,
381 arg6));
382 break;
383 case TARGET_FREEBSD_NR_mprotect:
384 ret = get_errno(target_mprotect(arg1, arg2, arg3));
385 break;
386 case TARGET_FREEBSD_NR_break:
387 ret = do_obreak(arg1);
388 break;
389 #ifdef __FreeBSD__
390 case TARGET_FREEBSD_NR___sysctl:
391 ret = do_freebsd_sysctl(arg1, arg2, arg3, arg4, arg5, arg6);
392 break;
393 #endif
394 case TARGET_FREEBSD_NR_sysarch:
395 ret = do_freebsd_sysarch(cpu_env, arg1, arg2);
396 break;
397 case TARGET_FREEBSD_NR_syscall:
398 case TARGET_FREEBSD_NR___syscall:
399 ret = do_freebsd_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,arg7,arg8,0);
400 break;
401 default:
402 ret = get_errno(syscall(num, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8));
403 break;
405 fail:
406 #ifdef DEBUG
407 gemu_log(" = %ld\n", ret);
408 #endif
409 if (do_strace)
410 print_freebsd_syscall_ret(num, ret);
411 return ret;
412 efault:
413 ret = -TARGET_EFAULT;
414 goto fail;
417 abi_long do_netbsd_syscall(void *cpu_env, int num, abi_long arg1,
418 abi_long arg2, abi_long arg3, abi_long arg4,
419 abi_long arg5, abi_long arg6)
421 abi_long ret;
422 void *p;
424 #ifdef DEBUG
425 gemu_log("netbsd syscall %d\n", num);
426 #endif
427 if(do_strace)
428 print_netbsd_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
430 switch(num) {
431 case TARGET_NETBSD_NR_exit:
432 #ifdef TARGET_GPROF
433 _mcleanup();
434 #endif
435 gdb_exit(cpu_env, arg1);
436 /* XXX: should free thread stack and CPU env */
437 _exit(arg1);
438 ret = 0; /* avoid warning */
439 break;
440 case TARGET_NETBSD_NR_read:
441 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
442 goto efault;
443 ret = get_errno(read(arg1, p, arg3));
444 unlock_user(p, arg2, ret);
445 break;
446 case TARGET_NETBSD_NR_write:
447 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
448 goto efault;
449 ret = get_errno(write(arg1, p, arg3));
450 unlock_user(p, arg2, 0);
451 break;
452 case TARGET_NETBSD_NR_open:
453 if (!(p = lock_user_string(arg1)))
454 goto efault;
455 ret = get_errno(open(path(p),
456 target_to_host_bitmask(arg2, fcntl_flags_tbl),
457 arg3));
458 unlock_user(p, arg1, 0);
459 break;
460 case TARGET_NETBSD_NR_mmap:
461 ret = get_errno(target_mmap(arg1, arg2, arg3,
462 target_to_host_bitmask(arg4, mmap_flags_tbl),
463 arg5,
464 arg6));
465 break;
466 case TARGET_NETBSD_NR_mprotect:
467 ret = get_errno(target_mprotect(arg1, arg2, arg3));
468 break;
469 case TARGET_NETBSD_NR_syscall:
470 case TARGET_NETBSD_NR___syscall:
471 ret = do_netbsd_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
472 break;
473 default:
474 ret = syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
475 break;
477 fail:
478 #ifdef DEBUG
479 gemu_log(" = %ld\n", ret);
480 #endif
481 if (do_strace)
482 print_netbsd_syscall_ret(num, ret);
483 return ret;
484 efault:
485 ret = -TARGET_EFAULT;
486 goto fail;
489 abi_long do_openbsd_syscall(void *cpu_env, int num, abi_long arg1,
490 abi_long arg2, abi_long arg3, abi_long arg4,
491 abi_long arg5, abi_long arg6)
493 abi_long ret;
494 void *p;
496 #ifdef DEBUG
497 gemu_log("openbsd syscall %d\n", num);
498 #endif
499 if(do_strace)
500 print_openbsd_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
502 switch(num) {
503 case TARGET_OPENBSD_NR_exit:
504 #ifdef TARGET_GPROF
505 _mcleanup();
506 #endif
507 gdb_exit(cpu_env, arg1);
508 /* XXX: should free thread stack and CPU env */
509 _exit(arg1);
510 ret = 0; /* avoid warning */
511 break;
512 case TARGET_OPENBSD_NR_read:
513 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
514 goto efault;
515 ret = get_errno(read(arg1, p, arg3));
516 unlock_user(p, arg2, ret);
517 break;
518 case TARGET_OPENBSD_NR_write:
519 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
520 goto efault;
521 ret = get_errno(write(arg1, p, arg3));
522 unlock_user(p, arg2, 0);
523 break;
524 case TARGET_OPENBSD_NR_open:
525 if (!(p = lock_user_string(arg1)))
526 goto efault;
527 ret = get_errno(open(path(p),
528 target_to_host_bitmask(arg2, fcntl_flags_tbl),
529 arg3));
530 unlock_user(p, arg1, 0);
531 break;
532 case TARGET_OPENBSD_NR_mmap:
533 ret = get_errno(target_mmap(arg1, arg2, arg3,
534 target_to_host_bitmask(arg4, mmap_flags_tbl),
535 arg5,
536 arg6));
537 break;
538 case TARGET_OPENBSD_NR_mprotect:
539 ret = get_errno(target_mprotect(arg1, arg2, arg3));
540 break;
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);
544 break;
545 default:
546 ret = syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
547 break;
549 fail:
550 #ifdef DEBUG
551 gemu_log(" = %ld\n", ret);
552 #endif
553 if (do_strace)
554 print_openbsd_syscall_ret(num, ret);
555 return ret;
556 efault:
557 ret = -TARGET_EFAULT;
558 goto fail;
561 void syscall_init(void)